Skip to content

NAT-PMP support #7

@offbynull

Description

@offbynull

Would it be possible to add NAT-PMP support to this library? The protocol seems stupid easy -- much less convoluted than UPnP IGD. http://miniupnp.free.fr/nat-pmp.html has a good overview of the protocol. Here's an example that I jut wrote that grabs your exposed IP address and opens a port for 60 seconds (tested on my home router):

    public static void main(String[] args) throws Throwable {
        DatagramSocket datagramSocket = new DatagramSocket(10000 + new Random().nextInt(55535));

        DatagramPacket publicAddressRequest = new DatagramPacket(new byte[] { 0, 0 }, 2, InetAddress.getByName("192.168.1.1"), 5351);
        datagramSocket.send(publicAddressRequest);

        ByteBuffer publicAddressResponseBuffer = ByteBuffer.allocate(12);
        DatagramPacket publicAddressResponse = new DatagramPacket(publicAddressResponseBuffer.array(), publicAddressResponseBuffer.capacity());
        datagramSocket.receive(publicAddressResponse);

        System.out.println(Integer.toHexString(publicAddressResponseBuffer.getInt(8))); // my ip

        DatagramPacket newPortMappingRequest = new DatagramPacket(new byte[] { 
            0, // version
            1, // udp (for tcp choose 2)
            0, 0, // reserved
            0x10, 0x00, // private port
            0x10, 0x00, // public port
            0, 0, 0, 60 // time to live for mapping
        }, 12, InetAddress.getByName("192.168.1.1"), 5351);
        datagramSocket.send(newPortMappingRequest);

        ByteBuffer newPortMappingBuffer = ByteBuffer.allocate(12);
        DatagramPacket newPortMappingResponse = new DatagramPacket(newPortMappingBuffer.array(), newPortMappingBuffer.capacity());
        datagramSocket.receive(newPortMappingResponse);

        System.out.println(Integer.toHexString(publicAddressResponseBuffer.getShort(2))); // result code
    }

The only issue with this is that there doesn't seem to be a standard way of getting the default gateway address in Java (192.168.1.1 in the example).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions