Class: StunClient::RFC3489NATDiscovery
- Inherits:
-
Object
- Object
- StunClient::RFC3489NATDiscovery
- Defined in:
- lib/stun-client/rfc3489/discovery/nat.rb
Overview
Helper, which should determine the NAT type.
Class Method Summary collapse
-
.get_local_addresses ⇒ Array
Returns an array with the IP addresses of the local device.
Instance Method Summary collapse
-
#detect_nat ⇒ Array
Checks the NAT Type and returns an array with a corresponding description.
-
#initialize(host = nil, port = nil, protocol = :IPv4) ⇒ RFC3489NATDiscovery
constructor
Creates a new object for NAT discovery.
Constructor Details
#initialize(host = nil, port = nil, protocol = :IPv4) ⇒ RFC3489NATDiscovery
Creates a new object for NAT discovery.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/stun-client/rfc3489/discovery/nat.rb', line 27 def initialize host = nil, port = nil, protocol = :IPv4 raise ArgumentError, 'Host must be a string' if host && (! host.is_a? String) raise ArgumentError, 'Port must be a integer' if port && (! port.is_a? Integer) case protocol when :IPv6 inet = Socket::AF_INET6 addr = '::' when :IPv4 inet = Socket::AF_INET addr = '0.0.0.0' else raise ArgumentError, 'Unknown protocol' end @host = host @port = port @socket = UDPSocket.new inet @socket.do_not_reverse_lookup = true @socket.bind addr, 0 @source_port = @socket.addr[1] end |
Class Method Details
.get_local_addresses ⇒ Array
Returns an array with the IP addresses of the local device. rubocop:disable Naming/AccessorMethodName
12 13 14 |
# File 'lib/stun-client/rfc3489/discovery/nat.rb', line 12 def self.get_local_addresses return Socket.ip_address_list.map(&:ip_address) end |
Instance Method Details
#detect_nat ⇒ Array
Checks the NAT Type and returns an array with a corresponding description. In the worst case, it can take up to 38 seconds for the function to finish.
Important: Keep in mind that RFC3489 is outdated and that measurements are not always accurate.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/stun-client/rfc3489/discovery/nat.rb', line 84 def detect_nat # rubocop:disable Style/ConditionalAssignment flags = [] test1v1 = do_query @host, @port if test1v1 if test1v1[:port] == @source_port flags << :preserves_ports else flags << :random_port end if RFC3489NATDiscovery.get_local_addresses.include?(test1v1[:host]) && test1v1[:port] == @source_port flags << :no_nat test2 = do_query @host, @port, change_ip: true, change_port: true if test2 flags << :open_internet else flags << :symmetric_udp_firewall end else flags << :nat test2 = do_query @host, @port, change_ip: true, change_port: true if test2 flags << :full_cone_nat else test1v2 = do_query test1v1[:changed_host], test1v1[:changed_port].to_i if test1v2 if test1v1[:host] == test1v2[:host] && test1v1[:port] == test1v2[:port] test3 = do_query @host, @port, change_port: true if test3 flags << :restricted_nat else flags << :port_restricted_nat end else flags << :symmetric_nat end else flags << :udp_sometimes_blocked end end end else flags << :udp_blocked end # flags.sort! return flags # rubocop:enable Style/ConditionalAssignment end |