Class NatPmpUtil

java.lang.Object
org.drasyl.util.protocol.NatPmpUtil

public final class NatPmpUtil extends Object
Utility class for NAT Port Mapping Protocol (NAT-PMP)-related stuff.
See Also:
  • Field Details

  • Method Details

    • buildExternalAddressRequestMessage

      public static byte[] buildExternalAddressRequestMessage()
          To determine the external address, the client behind the NAT sends
          the following UDP payload to port 5351 of the configured gateway
          address:
      
           0                   1
           0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
          +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
          | Vers = 0      | OP = 0        |
          +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       
    • buildMappingRequestMessage

      public static byte[] buildMappingRequestMessage(int internalPort, int externalPort, Duration lifetime)
          To create a mapping, the client sends a UDP packet to port 5351 of
          the gateway's internal IP address with the following format:
      
           0                   1                   2                   3
           0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
          +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
          | Vers = 0      | OP = x        | Reserved                      |
          +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
          | Internal Port                 | Suggested External Port       |
          +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
          | Requested Port Mapping Lifetime in Seconds                    |
          +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      
          Opcodes supported:
          1 - Map UDP
          2 - Map TCP
      
          The Reserved field MUST be set to zero on transmission and MUST be
          ignored on reception.
      
          The Ports and Lifetime are transmitted in the traditional network
          byte order (i.e., most significant byte first).
      
          The Internal Port is set to the local port on which the client is
          listening.
       
      Throws:
      IllegalArgumentException - if internalPort or externalPort is not in range of [0, 2^16)
    • readMessage

      public static NatPmpUtil.Message readMessage(InputStream inputStream) throws IOException
      Throws:
      IOException