class NatPMP::Client

Defined in:

natpmp.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(gateway_ip : URI, autoconnect : Bool = true) #

[View source]
def self.new(gateway_ip : String, autoconnect : Bool = true) #

Creates a new NAT-PMP Client, it's only able to connect trough IPV4 so if you supply a IPV6 address, it will fail; By default, it connects automatically to the NAT-PMP server, you can change this by setting autoconnect to false like this: client = NatPMP::Client.new("192.168.1.1", false), that way, you can change the socket properties like [email protected] to your liking before connecting.

client = NatPMP::Client.new("192.168.1.1")

[View source]

Instance Method Detail

def connect : Nil #

Connects to the NAT-PMP server, you don't need to call this function unless you have setted autoconnect is false on the constructor.


[View source]
def destroy_mapping(internal_port : UInt16, operation : UInt8) : Tuple(UInt8, UInt8, UInt16, UInt32, UInt16, UInt16, UInt32) #

Destroys a mapping in the NAT-PMP server

More details about how destroying a mapping works here: RFC 6886 - 3.4. Destoying a Mapping

# Destroys the mapping with internal port 25565, TCP
client.destroy_mapping(25565, 2) # => {0, 130, 0, 22758, 25565, 0, 0}
# Destroys the mapping with internal port 25565, UDP
client.destroy_mapping(25565, 1) # => {0, 130, 0, 22758, 25565, 0, 0}

[View source]
def request_mapping(internal_port : UInt16, external_port : UInt16, operation : UInt8, lifetime : UInt32 = 7200) : Tuple(UInt8, UInt8, UInt16, UInt32, UInt16, UInt16, UInt32) #

Requests a mapping to the NAT-PMP server

More details about how requesting a mapping works here: RFC 6886 - 3.3. Requesting a Mapping

# Maps the internal port 25565 to external port 25565, TCP, with a lifetime
# of 7200 seconds (the default defined by the RFC)
client.request_mapping(25565, 25565, 2) # => {0, 130, 0, 22758, 25565, 25565, 7200}
# The same as above, but with a lifetime of 60 seconds
client.request_mapping(25565, 25565, 2, 60) # => {0, 130, 0, 22758, 25565, 25565, 60}
# Maps the internal port 25565 to external port 25565, UDP, with a lifetime
# of 7200 seconds (the default defined by the RFC)
client.request_mapping(25565, 25565, 1) # => {0, 129, 0, 22758, 25565, 25565, 7200}
# The same as above, but with a lifetime of 60 seconds
client.request_mapping(25565, 25565, 1, 60) # => {0, 129, 0, 22758, 25565, 25565, 60}

[View source]
def send_external_address_request : Tuple(UInt8, UInt8, UInt16, UInt32, String | Nil) #

Returns the external address response as a Tuple(UInt8, UInt8, UInt16, UInt32, String | Nil)

res = client.send_external_address_request # => {0, 128, 0, 177060, "104.0.0.0"}
version = res[0]
operation = res[1]
result_code = res[2]
epoch = res[3]
external_address = res[4]

[View source]
def send_external_address_request_as_bytes : Bytes #

Returns the external address response as a Slice(UInt8)

client.send_external_address_request_as_bytes # => Bytes[0, 128, 0, 0, 0, 0, 88, 230, 104, 0, 0, 0]

[View source]