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

隨筆 - 298  文章 - 377  trackbacks - 0
<2016年4月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

常用鏈接

留言簿(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>
            中文一区二区| 欧美视频二区36p| 欧美在线播放| 麻豆精品精品国产自在97香蕉| 亚洲欧美日产图| 麻豆国产精品777777在线| 国产精品不卡在线| 在线观看成人网| 亚洲自拍偷拍麻豆| 欧美激情视频一区二区三区不卡| 亚洲伊人第一页| 欧美理论视频| 亚洲国产欧美久久| 久久久久久九九九九| 在线午夜精品| 亚洲欧美日韩中文播放| 欧美久久99| 亚洲日本欧美天堂| 久久影视精品| 性色av香蕉一区二区| 国产精品露脸自拍| 亚洲尤物精选| 在线视频精品一区| 欧美日韩在线播放三区四区| 亚洲日韩欧美视频一区| 久久乐国产精品| 亚洲国产乱码最新视频 | 黄网站色欧美视频| 欧美一区二区三区四区视频| 在线亚洲电影| 国产精品mm| 在线综合视频| 日韩视频精品在线| 欧美日韩另类在线| 久久久亚洲精品一区二区三区 | 黄色成人在线网站| 欧美亚洲一区二区在线| 亚洲欧美成人精品| 国产精品美女在线| 香蕉成人啪国产精品视频综合网| 亚洲国产一区二区视频| 国产日产欧产精品推荐色| aa级大片欧美三级| 亚洲茄子视频| 欧美成人中文字幕| 亚洲国产精品成人久久综合一区| 亚洲欧美一区二区三区极速播放| 亚洲私人影吧| 国产一区二区三区观看| 免费人成精品欧美精品| 免费在线视频一区| 99精品国产一区二区青青牛奶| 亚洲三级影院| 国产精品成人一区二区三区夜夜夜 | 狠狠色狠狠色综合人人| 午夜免费久久久久| 欧美在线关看| 亚洲人www| 亚洲天堂男人| 黄色亚洲大片免费在线观看| 亚洲第一页在线| 欧美小视频在线观看| 久久久精品午夜少妇| 久久久久久久综合狠狠综合| 欧美激情亚洲激情| 久久久久这里只有精品| 亚洲国产mv| 欧美三级在线播放| 狂野欧美激情性xxxx欧美| 亚洲国产福利在线| 久久视频免费观看| 亚洲一区二区三区久久 | 亚洲欧美视频在线观看视频| 亚洲精品中文在线| 亚洲第一区在线观看| 在线观看欧美日韩国产| 亚洲一区一卡| 老司机67194精品线观看| 国产无一区二区| 亚洲视屏在线播放| 先锋影音久久| 亚洲精品黄色| 先锋影音网一区二区| 亚洲国产一区二区精品专区| 亚洲在线观看免费| aaa亚洲精品一二三区| 久久国产婷婷国产香蕉| 一区二区三区欧美视频| 免费成人av| 久久夜色精品国产| 国产精品一区久久久| 欧美高清视频| 狠狠色综合网站久久久久久久| 亚洲视频免费在线观看| 在线一区欧美| 欧美不卡一区| 蜜臀久久久99精品久久久久久| 国产精品一区二区三区四区 | 一二三区精品| 亚洲激情另类| 亚洲欧美另类在线| 亚洲伊人久久综合| 欧美精品国产一区| 亚洲丶国产丶欧美一区二区三区| 加勒比av一区二区| 久久av二区| 亚洲欧美综合一区| 国产精品成人国产乱一区| av成人福利| 国产精品99久久久久久白浆小说 | 国产精品一区二区久久久久| 欧美成人高清| 狠狠色噜噜狠狠色综合久| 久久久久久久尹人综合网亚洲| 免费看精品久久片| 野花国产精品入口| 国产欧美日韩视频一区二区| 久久国产精品99国产| 亚洲高清视频的网址| 亚洲影院污污.| 在线成人激情黄色| 欧美女同在线视频| 欧美一级专区| 亚洲日本欧美天堂| 久久精品99国产精品酒店日本| 在线播放中文字幕一区| 欧美日韩国产综合视频在线观看| 亚洲一区二区在线观看视频| 玖玖国产精品视频| 亚洲无线视频| 在线精品亚洲一区二区| 欧美视频在线观看免费网址| 欧美综合国产| 夜夜夜精品看看| 欧美a级片网| 亚洲国产精品成人一区二区| 久久av免费一区| 亚洲人成艺术| 国产精品99久久久久久有的能看| 亚洲日本va午夜在线电影| 亚洲国产成人在线| 久久九九国产| 极品少妇一区二区三区精品视频| 亚洲国产婷婷香蕉久久久久久99| 99精品视频免费观看| 久久久久免费观看| 一区二区三区国产在线| 猫咪成人在线观看| 亚洲国产乱码最新视频| 久久久久久9999| 久久在线视频在线| 精品福利免费观看| 亚洲精品一二三区| 欧美激情四色 | 亚洲精品视频在线观看网站| 欧美sm极限捆绑bd| 在线欧美亚洲| 欧美激情视频在线免费观看 欧美视频免费一| 欧美一区二区日韩一区二区| 国产欧美日韩亚州综合| 亚洲一级电影| 欧美在线|欧美| 亚洲人成网站精品片在线观看| 91久久国产综合久久蜜月精品 | 亚洲午夜精品一区二区| 亚洲自拍偷拍视频| 亚洲高清色综合| 欧美精品www| 久久久久国产精品一区三寸| 久久久久久高潮国产精品视| 亚洲电影在线播放| 欧美黄污视频| 国产视频精品xxxx| 亚洲国产精品一区二区三区 | 亚洲啪啪91| 欧美成人一区二区三区片免费| 亚洲一区二区av电影| 国产在线观看一区| 欧美一区激情| 久久久夜夜夜| 伊人色综合久久天天五月婷| 欧美激情视频一区二区三区在线播放 | 亚洲欧美国产高清| 久久精品中文字幕一区| 久久尤物电影视频在线观看| 亚洲美洲欧洲综合国产一区| 亚洲视频在线观看| 国产在线精品成人一区二区三区| 99re热这里只有精品免费视频| 国产精品亚洲综合| 亚洲欧洲免费视频| 亚洲国产欧美日韩| 午夜电影亚洲| 欧美在线观看视频在线| 国语精品一区| 欧美在线网站| 国产日本欧美一区二区| 午夜精品成人在线| 先锋影音国产一区| 国产欧美在线观看|