简体   繁体   English

如何从STUN请求中获取我的外部IP地址和外部端口号?

[英]How do I get my external IP address and external Port Number from a STUN request?

I am behind the NAT of my WiFi router. 我在WiFi路由器的NAT后面。 When I send a packet from port 20,000,it comes out of my WiFi router on port 56867. I want to be able to get the output port number (the 56867) programmatically and I am trying do so via the STUN protocol. 当我从端口20,000发送一个数据包时,它是从我的WiFi路由器从端口56867发出的。我希望能够以编程方式获取输出端口号(56867),而我正尝试通过STUN协议来实现。

I am using the STUN client code in package "com.sun.stun" at https://code.google.com/p/openwonderland-jvoicebridge/source/browse/branches/jp/stun/src/com/sun/stun/StunClient.java . 我在https://code.google.com/p/openwonderland-jvoicebridge/source/browse/branches/jp/stun/src/com/sun/stun的 “ com.sun.stun”包中使用STUN客户端代码/StunClient.java I tried using the STUN client code to get my external IP address and port number by writing the following code: 我尝试使用STUN客户端代码通过编写以下代码来获取我的外部IP地址和端口号:

import com.sun.stun.*;
import java.net.*;
public class Main {
    public static void main(String args[]) throws Exception {
        InetSocketAddress ii = new InetSocketAddress("stun.ekiga.net", 3478);
        DatagramSocket s = new DatagramSocket();
        System.out.println("LocalAddress(): " + s.getLocalAddress());
        System.out.println("LocalPort(): " + s.getLocalPort());
        s.setSoTimeout(10000);
        StunClient stunClient1 = new StunClient(ii, s);
        System.out.println("external InetAddress/Port: " + stunClient1.getMappedAddress().toString());
    }
}

My output looks like this: 我的输出如下所示:

run:
LocalAddress(): 0.0.0.0/0.0.0.0
LocalPort(): 32979
May 30, 2015 11:38:16 PM unmodified.com.sun.stun.StunClient run
INFO: using STUN server stun.ekiga.net/217.10.68.152:3478
external InetAddress/Port: /0.0.0.0:32979
BUILD SUCCESSFUL (total time: 0 seconds)

The problem is that stunClient1.getMappedAddress() is giving me the binding address and ephemeral port number of DatagramSocket s and I want to get my public ip and public port number. 问题是stunClient1.getMappedAddress()给了我DatagramSocket的绑定地址和临时端口号,我想获取我的公共ip和公共端口号。 Please help. 请帮忙。

* Explaination / Clarification * *说明/澄清*

When I create a DatagramSocket, the OS/Java assigns it an ephemeral port number, in this case 58100. I have a Wifi router that has NAT. 创建DatagramSocket时,OS / Java为其分配一个临时端口号,在本例中为58100。我有一个具有NAT的Wifi路由器。 When I send a packet through my router to stun.ekiga.net, the external port number that stun.ekiga.net is sending its response back to is not the same as the the internal port number. 当我通过路由器将数据包发送到stun.ekiga.net时,stun.ekiga.net将其响应发送回的外部端口号与内部端口号不同。 I want the external port number that the STUN server is replying to. 我想要STUN服务器正在答复的外部端口号。

* Helpful information * *有用的信息*

I have received the following STUN packet: 我收到了以下STUN数据包:

Length of response is: 88 bytes
STUN response type received: 257 [StunHeader.BINDING_RESPONSE]

STUN response packet in binary format: 
00000001::00000001::00000000::01000100::11011100::00001110::00110110::10101000::11011100::00001110::00110110::10101000::11011100::00001110::00110110::10101000::11011100::00001110::00110110::10101000::00000000::00000001::00000000::00001000::00000000::00000001::10000000::00010100::00000000::00000000::00000000::00000000::00000000::00000100::00000000::00001000::00000000::00000001::00001101::10010110::11011001::00001010::01000100::10011000::00000000::00000101::00000000::00001000::00000000::00000001::00001101::10010111::11011001::01110100::01111010::10001010::10000000::00100000::00000000::00001000::00000000::00000001::01011100::00011010::11011100::00001110::00110110::10101000::10000000::00100010::00000000::00010000::01010110::01101111::01110110::01101001::01100100::01100001::00101110::01101111::01110010::01100111::00100000::00110000::00101110::00111001::00110110::00000000::

STUN response packet in hexadecimal format: 
01:01:00:44:DC:0E:36:A8:DC:0E:36:A8:DC:0E:36:A8:DC:0E:36:A8:00:01:00:08:00:01:80:14:00:00:00:00:00:04:00:08:00:01:0D:96:D9:0A:44:98:00:05:00:08:00:01:0D:97:D9:74:7A:8A:80:20:00:08:00:01:5C:1A:DC:0E:36:A8:80:22:00:10:56:6F:76:69:64:61:2E:6F:72:67:20:30:2E:39:36:00:

STUN response packet in decimal format: 
1 : 1 : 0 : 68 : -36 : 14 : 54 : -88 : -36 : 14 : 54 : -88 : -36 : 14 : 54 : -88 : -36 : 14 : 54 : -88 : 0 : 1 : 0 : 8 : 0 : 1 : -128 : 20 : 0 : 0 : 0 : 0 : 0 : 4 : 0 : 8 : 0 : 1 : 13 : -106 : -39 : 10 : 68 : -104 : 0 : 5 : 0 : 8 : 0 : 1 : 13 : -105 : -39 : 116 : 122 : -118 : -128 : 32 : 0 : 8 : 0 : 1 : 92 : 26 : -36 : 14 : 54 : -88 : -128 : 34 : 0 : 16 : 86 : 111 : 118 : 105 : 100 : 97 : 46 : 111 : 114 : 103 : 32 : 48 : 46 : 57 : 54 : 0 : 

How do I extract my public/external port number from the binary data in the stun packet? 如何从电击数据包中的二进制数据中提取我的公共/外部端口号?

Here's how: https://github.com/patmooney/jstun 这是如何的: https//github.com/patmooney/jstun

Run a client (which simply pings the Server and gets back the external IP and port of the request) 运行客户端(仅对服务器执行ping操作并获取请求的外部IP和端口)

./run Test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM