1

I am behind the NAT of my WiFi router. 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.

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 . I tried using the STUN client code to get my external IP address and port number by writing the following code:

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. 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. 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. I want the external port number that the STUN server is replying to.

* Helpful information *

I have received the following STUN packet:

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?

1
  • Never heard of this library you are using. Go use JSTUN instead. jstun.javawi.de
    – selbie
    Jun 13, 2015 at 19:32

1 Answer 1

0

Here's how: https://github.com/patmooney/jstun

Run a client (which simply pings the Server and gets back the external IP and port of the request)

./run Test

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.