Class: Bitcoin::Message::NetworkAddr
- Defined in:
- lib/bitcoin/message/network_addr.rb
Instance Attribute Summary collapse
-
#ip ⇒ Object
Returns the value of attribute ip.
-
#port ⇒ Object
Returns the value of attribute port.
-
#services ⇒ Object
The services the node advertised in its version message.
-
#time ⇒ Object
unix time.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ NetworkAddr
constructor
A new instance of NetworkAddr.
- #to_payload ⇒ Object
Constructor Details
#initialize ⇒ NetworkAddr
Returns a new instance of NetworkAddr.
19 20 21 22 |
# File 'lib/bitcoin/message/network_addr.rb', line 19 def initialize @time = Time.now.to_i @services = Bitcoin::Message::SERVICE_FLAGS[:network] end |
Instance Attribute Details
#ip ⇒ Object
Returns the value of attribute ip.
15 16 17 |
# File 'lib/bitcoin/message/network_addr.rb', line 15 def ip @ip end |
#port ⇒ Object
Returns the value of attribute port.
17 18 19 |
# File 'lib/bitcoin/message/network_addr.rb', line 17 def port @port end |
#services ⇒ Object
The services the node advertised in its version message.
13 14 15 |
# File 'lib/bitcoin/message/network_addr.rb', line 13 def services @services end |
#time ⇒ Object
unix time. Nodes advertising their own IP address set this to the current time. Nodes advertising IP addresses they’ve connected to set this to the last time they connected to that node. Other nodes just relaying the IP address should not change the time. Nodes can use the time field to avoid relaying old addr messages.
10 11 12 |
# File 'lib/bitcoin/message/network_addr.rb', line 10 def time @time end |
Class Method Details
.parse_from_payload(payload) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/bitcoin/message/network_addr.rb', line 24 def self.parse_from_payload(payload) buf = payload.is_a?(String) ? StringIO.new(payload) : payload addr = new addr.time = buf.read(4).unpack('V').first addr.services = buf.read(8).unpack('Q').first ip = IPAddr::new_ntoh(buf.read(16)) addr.ip = ip.ipv4_mapped? ? ip.native : ip.to_s addr.port = buf.read(2).unpack('n').first addr end |
Instance Method Details
#to_payload ⇒ Object
35 36 37 38 39 |
# File 'lib/bitcoin/message/network_addr.rb', line 35 def to_payload ip_addr = IPAddr.new (ip) ip_addr = ip_addr.ipv4_mapped if ip_addr.ipv4? [time, services].pack('VQ') << ip_addr.hton << [port].pack('n') end |