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

隨筆 - 298  文章 - 377  trackbacks - 0
<2008年3月>
2425262728291
2345678
9101112131415
16171819202122
23242526272829
303112345

常用鏈接

留言簿(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 聶文龍 閱讀(528) 評論(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>
            国产精品福利网站| 激情欧美一区二区| 亚洲天堂av综合网| 亚洲理伦在线| 欧美日韩国产一区精品一区 | 亚洲经典三级| 欧美主播一区二区三区| 亚洲欧美在线播放| 国产视频久久| 欧美成人精品一区二区| 欧美α欧美αv大片| 99精品国产热久久91蜜凸| 99re视频这里只有精品| 国产精品嫩草99av在线| 久久精品成人一区二区三区蜜臀| 久久久久天天天天| 久久一区中文字幕| 在线一区亚洲| 亚洲在线观看免费视频| 在线 亚洲欧美在线综合一区| 亚洲国产精品一区二区第四页av| 一本久道综合久久精品| 亚洲精品一品区二品区三品区| 欧美一区二区三区免费大片| 国产亚洲午夜| 亚洲国产成人精品女人久久久| 亚洲亚洲精品三区日韩精品在线视频| 久久精品99无色码中文字幕| 亚洲高清视频在线观看| 亚洲精品久久久久久下一站| 国产精品美女久久久免费| 久久综合久久综合久久| 欧美日韩亚洲一区三区 | 欧美成人精品在线视频| 亚洲最新在线| 久久福利影视| 亚洲少妇一区| 欧美96在线丨欧| 香蕉久久精品日日躁夜夜躁| 蜜桃伊人久久| 久久精品一区二区三区不卡牛牛| 一区二区三区精品久久久| 精品不卡视频| 一区二区三区四区五区精品| 在线欧美亚洲| 午夜日韩视频| 欧美亚洲三区| 欧美日韩国产综合视频在线| 蜜臀av国产精品久久久久| 国产精品萝li| 9人人澡人人爽人人精品| 亚洲黄色成人| 久久久噜噜噜久久中文字幕色伊伊| 国产毛片久久| 亚洲色图在线视频| 99精品国产高清一区二区| 久久亚洲视频| 久久国产精品久久久| 欧美日韩播放| 亚洲第一综合天堂另类专| 黄色免费成人| 久久精品国产一区二区三| 欧美一区午夜视频在线观看| 欧美性开放视频| 一本久久a久久精品亚洲| 亚洲精选大片| 欧美不卡高清| 欧美福利一区二区三区| 1769国产精品| 免费观看国产成人| 亚洲大片一区二区三区| 激情五月婷婷综合| 久久久国产视频91| 裸体女人亚洲精品一区| 一色屋精品视频免费看| 久久久久国内| 亚洲国产激情| 亚洲一二三区在线| 国产精品黄视频| 亚洲欧美日本另类| 久久艳片www.17c.com| 伊人男人综合视频网| 久久综合中文| 亚洲国产日韩一区| 亚洲欧美99| 国产三区二区一区久久| 久久久99爱| 亚洲欧洲精品一区二区三区| 亚洲色诱最新| 国产一区二区三区四区三区四| 亚洲国内自拍| 亚洲欧美激情一区| 国产精品揄拍一区二区| 久久精品日产第一区二区| 久久在精品线影院精品国产| 亚洲欧洲综合另类| 欧美午夜精品久久久久久久| 亚洲欧美在线免费| 久久综合福利| 艳女tv在线观看国产一区| 国产精品女同互慰在线看| 久久精品国产一区二区三| 亚洲福利电影| 久久精品久久99精品久久| 亚洲精选久久| 国产亚洲精品资源在线26u| 免费久久99精品国产自| 亚洲性夜色噜噜噜7777| 女同性一区二区三区人了人一| 国产香蕉久久精品综合网| 久久一区二区三区四区五区| 亚洲国产高清一区| 久久精品日韩一区二区三区| 亚洲精品久久久久久久久久久久| 久久久久久久综合日本| 99国产精品久久久久久久久久| 亚洲成色777777女色窝| 国产精品v片在线观看不卡| 美女国产精品| 午夜精品一区二区三区在线| 亚洲人成网站在线观看播放| 久久久一区二区三区| 亚洲在线视频一区| 亚洲精品影视| 在线成人免费视频| 国产视频亚洲精品| 国产精品www| 欧美日韩中文另类| 欧美国产专区| 久久综合图片| 久久人体大胆视频| 亚洲综合欧美日韩| 国产精品99久久久久久久久久久久 | 国产一级一区二区| 亚洲视屏在线播放| 亚洲国产精品传媒在线观看| 性欧美暴力猛交另类hd| 日韩午夜电影| 日韩一级二级三级| 99精品免费视频| 亚洲精品一区二区网址| 亚洲国产黄色| 亚洲国产一成人久久精品| 1024成人网色www| 国产亚洲一区二区三区在线播放| 欧美在线观看网站| 亚洲欧美激情精品一区二区| 亚洲视频自拍偷拍| 亚洲一区三区电影在线观看| 一区二区欧美亚洲| 一区二区欧美精品| 亚洲性图久久| 性做久久久久久| 久久精品视频va| 久久免费黄色| 午夜欧美大尺度福利影院在线看| 亚洲第一黄网| 黄色欧美日韩| 在线播放豆国产99亚洲| 在线观看免费视频综合| 在线播放不卡| 99re热精品| 亚洲欧美韩国| 欧美伊人久久| 噜噜噜91成人网| 欧美激情一二三区| 一区二区三区日韩在线观看| 亚洲午夜精品一区二区三区他趣| 美女精品国产| 久久九九全国免费精品观看| 亚洲欧美日韩精品久久亚洲区| 国产日韩在线看| 精品成人乱色一区二区| 亚洲国产精品久久| 亚洲小说春色综合另类电影| 亚洲欧美成人一区二区三区| 午夜一区二区三区在线观看 | 亚洲欧美日韩天堂| 久久精品欧美日韩| 欧美影视一区| 久久午夜精品| 欧美视频四区| 狠狠狠色丁香婷婷综合激情| 亚洲精品一二区| 欧美怡红院视频| 欧美激情一区二区| 亚洲免费在线视频| 欧美黄网免费在线观看| 国产欧美日韩一区二区三区在线观看| 欧美/亚洲一区| 欧美色视频日本高清在线观看| 欧美成人免费观看| 国产麻豆精品theporn| 亚洲福利专区| 欧美影院成人| 日韩视频在线一区二区三区| 久久久久看片| 国产视频不卡| 亚洲一区二区三区乱码aⅴ蜜桃女| 亚洲精品免费在线|