• <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>
            隨筆 - 298  文章 - 377  trackbacks - 0
            <2007年8月>
            2930311234
            567891011
            12131415161718
            19202122232425
            2627282930311
            2345678

            常用鏈接

            留言簿(34)

            隨筆分類(lèi)

            隨筆檔案

            文章檔案

            相冊(cè)

            收藏夾

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            NAT
            今天碰到了NAT的問(wèn)題,來(lái)到了這里。盡管注冊(cè)過(guò)程繁瑣,我還是堅(jiān)持到底了。這個(gè)問(wèn)題解決了,一定寫(xiě)下心得,讓需要幫助的人能夠受益。

            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 穿透網(wǎng)關(guān)的說(shuō)明,至于TCP如何穿透網(wǎng)關(guān),如下所示。

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

            上文對(duì)于單向的NAT應(yīng)該不成問(wèn)題,但是對(duì)于對(duì)稱(chēng)的NAT,由于我們考慮了一個(gè)生存周期的問(wèn)題,雖然對(duì)稱(chēng)的NAT會(huì)反復(fù)變換端口,但是在生存周期內(nèi)應(yīng)該不會(huì)變的,我這么認(rèn)為,沒(méi)有試過(guò)。
            posted on 2007-08-17 13:15 聶文龍 閱讀(517) 評(píng)論(0)  編輯 收藏 引用 所屬分類(lèi): net work
            久久综合色区| 久久综合狠狠综合久久| 精品久久久久久亚洲精品 | 麻豆精品久久精品色综合| 国内精品伊人久久久久| 一本大道加勒比久久综合| 久久se精品一区精品二区国产| 国产精品99久久久久久宅男| 日本久久中文字幕| 亚洲色大成网站www久久九| 国产精品免费看久久久| 久久无码人妻一区二区三区| 久久精品成人欧美大片| 久久久久久九九99精品| 亚洲国产精品狼友中文久久久 | 国产午夜久久影院| 国内精品伊人久久久久777| 99久久精品国产高清一区二区| 亚洲&#228;v永久无码精品天堂久久| 亚洲国产精品无码久久一线| 久久精品国产精品亚洲人人| 青青青青久久精品国产 | 亚洲狠狠婷婷综合久久蜜芽 | 伊人久久大香线蕉无码麻豆| 久久99热只有频精品8| 国产精品久久久久无码av| 久久精品国产亚洲AV蜜臀色欲| 久久婷婷五月综合成人D啪| 污污内射久久一区二区欧美日韩| 欧美国产精品久久高清| 欧美大战日韩91综合一区婷婷久久青草| 国内精品伊人久久久久AV影院| 亚洲午夜精品久久久久久人妖| 日本久久久精品中文字幕| 久久久久香蕉视频| 97精品依人久久久大香线蕉97 | 久久久精品免费国产四虎| 久久精品国产色蜜蜜麻豆| 午夜肉伦伦影院久久精品免费看国产一区二区三区 | 久久综合九色综合精品| 99久久人人爽亚洲精品美女|