青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

隨筆 - 298  文章 - 377  trackbacks - 0
<2016年6月>
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

常用鏈接

留言簿(34)

隨筆分類

隨筆檔案

文章檔案

相冊

收藏夾

搜索

  •  

最新評論

閱讀排行榜

評論排行榜

NAT
今天碰到了NAT的問題,來到了這里。盡管注冊過程繁瑣,我還是堅持到底了。這個問題解決了,一定寫下心得,讓需要幫助的人能夠受益。

NAT

I saw a post on a Delphi BBS, one is asking:" How do I develop a software acting like QQ, It can punching through the Gateway so that two different private net's users can exchange data through UDP trans? "

I was confused by the same problem at that time. I was developing a C/S software, and the Client will be behind the Gateway. How do the Server send data to the client behiding the Gateway?

I knew NAT then. Cone NAT and Symentric NAT.

In a few hours, I knew that It's easy to resolve the problem. 

According to my comprehension. NAT does so:

NAT will use the same public IP for all private net users and will assign(binding or temporary.Here done in Cone NAT) different Ports for different sessions.
All the applications outside the private net dont know the private net users but they know this NAT's public IP and theses different Ports.
Meanwhile the NAT knows whom each port identified. 

That's all.

A little complex for UDP. it has no connection. How the public IPs send UDP to the private net users? they dont konw them at all, they can only konw the NAT's public IP. It seems this is the focus point.
Chaos? But it's really easy if you know this truth: Only if a private client send UDP data to a public IP through the NAT, the NAT will identify the private client through assigning a random UDP port for this client in a certain period. Then in this period, if somebody outside the private net send data to this UDP port of the NAT's public IP, NAT will retransfer all the incoming data to the JUST user it identified according to this UDP port.

Done!

It's all for the so said "UDP punching hole". It's so easy, isn't it?

Of course, till now, we just resolve half problem of the question we mentioned most above. Now we know how the users ouside the private net send UDP data to the private net user. It all depends on such a resume: The outside user has konwn the NAT IP and the UDP port that NAT uses to identify the private net's client. How did he know the port? Of course the private net's user send UDP data first to tell him. WOWOWO wait! Here comes another problem? 
No doult, If the outside user has a public IP, so the private net's client can easily send UDP data to the outside user, when the the user received the UDP data, surely it will know the UDP port the NAT assigned for the private net's client.
BUT WHAT if the outside user is also a private net's user? How did this private net's user send the first identity-comfirming UDP packet to another private net's user? It dont know THAT private net's NAT's IP and the UDP Port that NAT assigned to that private user. It seems like a circle, right? 

I think you have dropped into a never ending circle: i want to touch you but you dont know me, and you want to touch me but i also dont know you. HOW SHOULD YOU DO NOW?

Always when people are in trouble, they prayed : "Oh God Bless." YUP, WHY NOT LET GOD HELP YOU?

If God can tell me your NAT's public IP and your NAT's port that your NAT used to identify you, Any thing is resolved right? And the same, If God can tell you my NAT's public IP and the UDP Port my NAT used to identify me, you can also touch me right?

But where is the God. My teach ever told me that God is created by people. I think maybe it's right now.

Let's Suppose, if A Server konws your NAT's public IP and the UDP Port your NAT used to identify you. And IT also knows my NAT's public IP and the UDP port that my NAT assigned for me. If it will tell you my NAT's infomation and also tell me your NAT's information. It will be the JUST GOD. right?

You cant touch me directly, but you can touch the GOD. How to do this, I has described at the beginning of the article. The only extra thing that the God will do is to tell you my infomation and tell me yours, then the Left things are ours, i know you now and you know me, too. i can send UDP data to your NAT's public IP and your NAT's UDP port it assigned for you, then you can received my data. So does you. God bless, the problem resolved.

Got it?

Demonstrate in Figures Below:

C/S Model:

             Server(S)
        IP:211.111.111.111
            Port:54326
                |
                |
                |
                |
               NAT
         IP:61.167.12.199
     Port:1818 for Private User1
     Port:1822 for Private User(n)
                |
                |
                |
----------------------------
|               |                |
Private User1  ...       Private User(n)
IP:192.168.0.192         IP:192.168.0.198
Port: 2011               Port:1037



Private Peer to Private Peer Model:

                                                Server(S)
                                           IP:211.111.111.111
                                               Port:54326
                                                   |
                --------------------------------------------------------------------
                |                                  |                               |
                |                                  |                               |
                |                                  |                               |
                |                                  |                               |
               NAT1                              ......                          NAT(n)
         IP:61.167.12.199                                                   IP:202.192.112.122
     Port:1818 for Private User1                                         Port:60031 for Private Peer1
     Port:1822 for Private User(n)                                       Port:60033 for Private Peer(n)
                |                                                                  |
                |                                                                  |
                |                                                                  |
----------------------------                                ----------------------------
|               |                |                                 |               |                |    
Private User1  ...  Private User(n)    Private Peer1      ...      Private Peer(n)
IP:192.168.0.192 IP:192.168.0.198 IP:192.198.198.22   IP:192.198.198.60
Port: 2011               Port:1037            Port:1047                         Port:1077

(Surely it does work for multi-layer NATs.)

Here is a Code snippet:

#define NC_UDP_PORT 54326

// Server:

SOCKET  m_socUDP = WSASocket(AF_INET,SOCK_DGRAM,0,NULL,0,WSA_FLAG_OVERLAPPED);

SOCKADDR_IN saiLocal;
saiLocal.sin_family = AF_INET;
saiLocal.sin_port = htons(NC_UDP_PORT);
saiLocal.sin_addr.S_un.S_addr = inet_addr(pszLocalAddr);
if (bind(m_socUDP,(SOCKADDR *)&saiLocal,sizeof(saiLocal))==SOCKET_ERROR) return NC_E_UDP_BIND|WSAGetLastError();

SOCKADDR_IN saiFrom;
int fromlen=sizeof(SOCKADDR_IN);
BYTE *buf=(BYTE *)malloc(len);
if(buf==NULL) return NULL;

len=recvfrom(m_socUDP,(char *)buf,len,MSG_PEEK,(SOCKADDR *)&saiFrom,&fromlen);
if(len<0) { free(buf); return NULL; }

len=sendto(m_socUDP,(char *)buf,len,0,(SOCKADDR*)&saiFrom,&fromlen);

// Client:

SOCKADDR_IN saiLocal;
saiLocal.sin_port = htons(0);
saiLocal.sin_addr.S_un.S_addr = inet_addr(pszLocalAddr);
int len=sizeof(saiLocal);
if (bind(m_socUDP, (SOCKADDR *)&saiLocal, len  == SOCKET_ERROR) return -1;

SOCKADDR_IN saiServer;
memset(&saiServer,0,sizeof(saiServer));
saiServer.sin_family = AF_INET;
saiServer.sin_addr.S_un.S_addr = inet_addr(pszServerAddr);
saiServer.sin_port = htons(NC_UDP_PORT);
sendto(pThis->m_socUDP,(const char*)buf,len,0,(SOCKADDR*)&saiServer,len);

SOCKADDR_IN saiFrom;
memset(&saiFrom,0,len);
saiFrom.sin_family = AF_INET;
nRet = recvfrom(pThis->m_socUDP,buf,len,0,(SOCKADDR*)&sai_From,&len);

以上是 UDP 穿透網關的說明,至于TCP如何穿透網關,如下所示。

TCP在兩方都在NAT之后的客戶端很難建立連接,至少我不知道怎么連,可以用UDP代替,但是如果只有一方在NAT之后,可以嘗試逆向連接,讓服務器通知內網的用戶去連另一個公網的客戶,但是公網的客戶卻無法連接到內網的客戶,這也只對一方有意義。

上文對于單向的NAT應該不成問題,但是對于對稱的NAT,由于我們考慮了一個生存周期的問題,雖然對稱的NAT會反復變換端口,但是在生存周期內應該不會變的,我這么認為,沒有試過。
posted on 2007-08-17 13:15 聶文龍 閱讀(524) 評論(0)  編輯 收藏 引用 所屬分類: net work
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            蜜臀a∨国产成人精品| 欧美日韩美女在线| 久久久久成人网| 欧美一区二区大片| 亚洲欧美另类国产| 久久精品理论片| 免费亚洲视频| 亚洲精品男同| 一区二区三区你懂的| 亚洲午夜精品久久| 久久成人资源| 欧美大片在线看| 国产精品国产三级国产专区53| 国产精品永久免费观看| 国产一区二区三区直播精品电影| 国产亚洲精品成人av久久ww| 在线看国产一区| 久热精品在线视频| 麻豆精品视频在线观看| 欧美电影在线播放| 99国产精品久久久久久久成人热| 亚洲午夜精品网| 老巨人导航500精品| 欧美人与禽猛交乱配| 国产精品三级久久久久久电影| 国产日本欧美视频| 亚洲日本免费| 久久精品亚洲国产奇米99| 欧美高清视频一区| 亚洲欧美日韩另类| 欧美精品999| 国产一级一区二区| 一区二区日韩欧美| 欧美激情一区二区三区 | 久久久久久久一区二区| 欧美激情成人在线视频| 国产欧美一区在线| 99精品视频一区| 噜噜噜91成人网| 日韩一级大片| 欧美大尺度在线观看| 国产精品看片资源| 亚洲人体影院| 美日韩精品免费| 香蕉久久夜色精品国产| 欧美日韩国产免费观看| 在线欧美亚洲| 老司机一区二区| 亚洲午夜性刺激影院| 欧美成人国产| 亚洲电影av| 久热精品在线| 欧美在线观看网站| 国产精品一区二区三区久久| 亚洲性视频网站| 亚洲国产婷婷香蕉久久久久久99| 久久精品成人欧美大片古装| 国产精品日韩电影| 亚洲嫩草精品久久| 一本大道av伊人久久综合| 欧美大胆a视频| 亚洲人成啪啪网站| 亚洲第一中文字幕| 免费h精品视频在线播放| 国产主播一区二区| 久热国产精品| 久久久噜噜噜久噜久久| 又紧又大又爽精品一区二区| 久久亚洲电影| 亚洲激情国产精品| 欧美激情精品久久久久久| 麻豆国产va免费精品高清在线| 国产综合欧美| 欧美大片免费| 欧美精品久久久久久久| 欧美特黄一级| 亚洲欧美福利一区二区| 一本色道久久综合亚洲精品婷婷 | 午夜精品久久久久久久99黑人| 国产精品电影观看| 欧美亚洲在线观看| 久久福利电影| 亚洲精品国产视频| 日韩一级免费| 国产九九精品| 女仆av观看一区| 亚洲欧美精品在线观看| 国内精品久久久久影院 日本资源 国内精品久久久久伊人av | 激情成人av在线| 亚洲国产精品久久久久婷婷老年| 欧美激情精品久久久久久蜜臀 | 久久riav二区三区| 久久亚洲风情| 亚洲欧美一区二区视频| 久久欧美肥婆一二区| 日韩视频在线观看国产| 亚洲欧美在线一区| 亚洲激情av| 夜夜嗨av一区二区三区| 国产中文一区二区| 亚洲美洲欧洲综合国产一区| 国产精品嫩草影院一区二区| 久久精品国产免费| 欧美激情第六页| 久久精品一区| 欧美三级网址| 欧美激情片在线观看| 国产九色精品成人porny| 欧美成人精品影院| 国产精品免费网站| 国产精品美女久久久久av超清 | 亚洲高清在线观看| 亚洲午夜精品一区二区| 亚洲精品影视| 久久国产精品久久久| 国产精品毛片一区二区三区| 亚洲大片在线观看| 红桃视频国产精品| 亚洲男人第一av网站| 日韩一区二区电影网| 久久久国产一区二区| 欧美一区二区日韩一区二区| 欧美精品福利| 亚洲狠狠丁香婷婷综合久久久| 国内精品久久久久久影视8| 亚洲视频一二区| 一区二区三区四区蜜桃| 亚洲精品一区二区三区蜜桃久 | 亚洲精品中文字幕在线观看| 一区二区在线观看av| 亚洲免费综合| 欧美一区三区二区在线观看| 欧美日韩免费观看一区二区三区| 欧美电影资源| 亚洲福利久久| 蜜乳av另类精品一区二区| 麻豆精品视频在线观看视频| 在线观看三级视频欧美| 亚洲激情六月丁香| 亚洲另类黄色| 欧美精品自拍偷拍动漫精品| 欧美激情日韩| 一区二区精品在线观看| 欧美性一二三区| 男男成人高潮片免费网站| 国产人久久人人人人爽| 亚洲欧美综合网| 欧美在线视频播放| 国产一区二区中文字幕免费看| 一本色道久久88综合亚洲精品ⅰ| 亚洲中午字幕| 欧美吻胸吃奶大尺度电影| 亚洲日本va午夜在线电影| 国产日韩精品电影| 午夜精品久久久久久99热| 一本色道久久99精品综合 | 亚洲一区制服诱惑| 一区二区高清| 欧美一区二区免费观在线| 欧美在线精品免播放器视频| 国产精品久久久久久久久久尿| 午夜欧美大尺度福利影院在线看 | 91久久精品www人人做人人爽| 欧美影院一区| 亚洲国产成人高清精品| 亚洲三级网站| 欧美激情2020午夜免费观看| 亚洲人午夜精品| 国内久久婷婷综合| 免费观看不卡av| 欧美黄免费看| 日韩一级裸体免费视频| 欧美日韩综合视频| 一区二区三区国产精品| 性色av一区二区三区红粉影视| 国产日韩精品在线| 久久久欧美一区二区| 一本色道久久综合亚洲精品婷婷| 欧美二区不卡| 99re66热这里只有精品3直播| 亚洲欧美日韩专区| 亚洲第一精品久久忘忧草社区| 久久久久久夜| 亚洲欧洲日产国码二区| 午夜视频一区在线观看| 亚洲高清精品中出| 欧美日韩精品是欧美日韩精品| 亚洲欧美激情诱惑| 国产日韩综合| 亚洲午夜免费视频| 久久久久国产精品一区二区| 亚洲福利电影| 国产精品国色综合久久| 欧美在线观看一二区| 亚洲欧洲在线视频| 久久精品99国产精品日本| 国产精品国产三级国产aⅴ浪潮| 欧美高清在线一区二区| 亚洲免费一区二区| 狠狠色狠狠色综合日日五|