Class: StunClient
- Inherits:
-
Object
- Object
- StunClient
- Defined in:
- lib/toquen/stunning.rb
Class Method Summary collapse
Instance Method Summary collapse
- #get_ip ⇒ Object
-
#initialize(host, port) ⇒ StunClient
constructor
A new instance of StunClient.
Constructor Details
#initialize(host, port) ⇒ StunClient
Returns a new instance of StunClient.
5 6 7 8 |
# File 'lib/toquen/stunning.rb', line 5 def initialize(host, port) @host = host @port = port end |
Class Method Details
.get_ip ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/toquen/stunning.rb', line 38 def self.get_ip servers = [['stun.l.google.com', 19_302], ['stun.ekiga.net', 3478], ['stunserver.org', 3478]] servers.each do |host, port| ip = StunClient.new(host, port).get_ip return ip unless ip.nil? end nil end |
Instance Method Details
#get_ip ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/toquen/stunning.rb', line 10 def get_ip Timeout.timeout(0.5) do socket = UDPSocket.new data = [0x0001, 0].pack('nn') + Random.new.bytes(16) socket.send(data, 0, @host, @port) data, = socket.recvfrom(1000) type, length = data.unpack('nn') # if not a message binding response return nil unless type == 0x0101 data = data[20..-1] until data.empty? type, length = data.unpack('nn') # if attr type is ATTR_MAPPED_ADDRESS, return it if type == 0x0001 values = data[4...4 + length].unpack('CCnCCCC') return values[3..-1] * '.' end data = data[4 + length..-1] end return nil end rescue Timeout::Error return nil end |