Class PcpPortUtil


  • public final class PcpPortUtil
    extends Object
    Utility class for Port Control Protocol (PCP)-related stuff.
    See Also:
    RFC 6887
    • Method Detail

      • buildMappingRequestMessage

        public static byte[] buildMappingRequestMessage​(Duration lifetime,
                                                        InetAddress clientAddress,
                                                        byte[] nonce,
                                                        int protocol,
                                                        int port,
                                                        InetAddress externalAddress)
              All MAP opcode requests have 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
              +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
              |  Version = 2  |R|   Opcode    |         Reserved              |
              +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
              |                 Requested Lifetime (32 bits)                  |
              +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
              |                                                               |
              |            PCP Client's IP Address (128 bits)                 |
              |                                                               |
              |                                                               |
              +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
              |                                                               |
              |                 Mapping Nonce (96 bits)                       |
              |                                                               |
              +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
              |   Protocol    |          Reserved (24 bits)                   |
              +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
              |        Internal Port          |    Suggested External Port    |
              +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
              |                                                               |
              |           Suggested External IP Address (128 bits)            |
              |                                                               |
              |                                                               |
              +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
        
            These fields are described below:
        
            Version:  This document specifies protocol version 2.  PCP clients
               and servers compliant with this document use the value 2.  This
               field is used for version negotiation as described in Section 9.
        
            R: Indicates Request (0) or Response (1).
        
            Opcode:  A 7-bit value specifying the operation to be performed.  MAP
               and PEER Opcodes are defined in Sections 11 and 12.
        
            Reserved:  16 reserved bits.  MUST be zero on transmission and MUST
               be ignored on reception.
        
            Requested Lifetime:  An unsigned 32-bit integer, in seconds, ranging
               from 0 to 2^32-1 seconds.  Requested lifetime of this
               mapping, in seconds.  The value 0 indicates "delete".
        
            PCP Client's IP Address:  The source IPv4 or IPv6 address in the IP
               header used by the PCP client when sending this PCP request.  An
               IPv4 address is represented using an IPv4-mapped IPv6 address.
               The PCP Client IP Address in the PCP message header is used to
               detect an unexpected NAT on the path between the PCP client and
               the PCP-controlled NAT or firewall device.  See Section 8.1.
        
            Mapping Nonce:  Random value chosen by the PCP client.  See
               Section 11.2, "Generating a MAP Request".  Zero is a legal value
               (but unlikely, occurring in roughly one in 2^96 requests).
        
            Protocol:  Upper-layer protocol associated with this Opcode.  Values
               are taken from the IANA protocol registry [proto_numbers].  For
               example, this field contains 6 (TCP) if the Opcode is intended to
               create a TCP mapping.  This field contains 17 (UDP) if the Opcode
               is intended to create a UDP mapping.  The value 0 has a special
               meaning for 'all protocols'.
        
            Reserved:  24 reserved bits, MUST be sent as 0 and MUST be ignored
               when received.
        
            Internal Port:  Internal port for the mapping.  The value 0 indicates
               'all ports', and is legal when the lifetime is zero (a delete
               request), if the protocol does not use 16-bit port numbers, or the
               client is requesting 'all ports'.  If the protocol is zero
               (meaning 'all protocols'), then internal port MUST be zero on
               transmission and MUST be ignored on reception.
        
            Suggested External Port:  Suggested external port for the mapping.
               This is useful for refreshing a mapping, especially after the PCP
               server loses state.  If the PCP client does not know the external
               port, or does not have a preference, it MUST use 0.
        
            Suggested External IP Address:  Suggested external IPv4 or IPv6
               address.  This is useful for refreshing a mapping, especially
               after the PCP server loses state.  If the PCP client does not know
               the external address, or does not have a preference, it MUST use
               the address-family-specific all-zeros address (see Section 5).
         
        Throws:
        IllegalArgumentException - if port is not in range of [0, 2^16 - 1]