Class: Bitcoin::Message::NetworkAddr
- Defined in:
- lib/bitcoin/message/network_addr.rb
Instance Attribute Summary collapse
-
#ip_addr ⇒ Object
IPAddr.
-
#port ⇒ Object
Returns the value of attribute port.
-
#services ⇒ Object
The services the node advertised in its version message.
-
#skip_time ⇒ Object
readonly
Returns the value of attribute skip_time.
-
#time ⇒ Object
unix time.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(ip: '127.0.0.1', port: Bitcoin.chain_params.default_port, services: DEFAULT_SERVICE_FLAGS, time: Time.now.to_i) ⇒ NetworkAddr
constructor
A new instance of NetworkAddr.
- #ip ⇒ Object
- #to_payload(skip_time = false) ⇒ Object
Constructor Details
#initialize(ip: '127.0.0.1', port: Bitcoin.chain_params.default_port, services: DEFAULT_SERVICE_FLAGS, time: Time.now.to_i) ⇒ NetworkAddr
Returns a new instance of NetworkAddr.
23 24 25 26 27 28 |
# File 'lib/bitcoin/message/network_addr.rb', line 23 def initialize(ip: '127.0.0.1', port: Bitcoin.chain_params.default_port, services: DEFAULT_SERVICE_FLAGS, time: Time.now.to_i) @time = time @ip_addr = IPAddr.new(ip) @port = port @services = services end |
Instance Attribute Details
#ip_addr ⇒ Object
IPAddr
17 18 19 |
# File 'lib/bitcoin/message/network_addr.rb', line 17 def ip_addr @ip_addr end |
#port ⇒ Object
Returns the value of attribute port.
19 20 21 |
# File 'lib/bitcoin/message/network_addr.rb', line 19 def port @port end |
#services ⇒ Object
The services the node advertised in its version message.
15 16 17 |
# File 'lib/bitcoin/message/network_addr.rb', line 15 def services @services end |
#skip_time ⇒ Object (readonly)
Returns the value of attribute skip_time.
21 22 23 |
# File 'lib/bitcoin/message/network_addr.rb', line 21 def skip_time @skip_time 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.
12 13 14 |
# File 'lib/bitcoin/message/network_addr.rb', line 12 def time @time end |
Class Method Details
.local_addr ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/bitcoin/message/network_addr.rb', line 41 def self.local_addr addr = new addr.ip_addr = IPAddr.new('127.0.0.1') addr.port = Bitcoin.chain_params.default_port addr.services = DEFAULT_SERVICE_FLAGS addr end |
.parse_from_payload(payload) ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/bitcoin/message/network_addr.rb', line 30 def self.parse_from_payload(payload) buf = payload.is_a?(String) ? StringIO.new(payload) : payload has_time = buf.size > 26 addr = new(time: nil) addr.time = buf.read(4).unpack('V').first if has_time addr.services = buf.read(8).unpack('Q').first addr.ip_addr = IPAddr::new_ntoh(buf.read(16)) addr.port = buf.read(2).unpack('n').first addr end |
Instance Method Details
#ip ⇒ Object
49 50 51 |
# File 'lib/bitcoin/message/network_addr.rb', line 49 def ip ip_addr.ipv4_mapped? ? ip_addr.native : ip_addr.to_s end |
#to_payload(skip_time = false) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/bitcoin/message/network_addr.rb', line 53 def to_payload(skip_time = false) p = '' p << [time].pack('V') unless skip_time addr = ip_addr.ipv4? ? ip_addr.ipv4_mapped : ip_addr p << [services].pack('Q') << addr.hton << [port].pack('n') end |