MCPcopy
hub / github.com/petertodd/python-bitcoinlib / CAddress

Class CAddress

bitcoin/net.py:30–70  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

28
29
30class CAddress(Serializable):
31 def __init__(self, protover=PROTO_VERSION):
32 self.protover = protover
33 self.nTime = 0
34 self.nServices = 1
35 self.pchReserved = IPV4_COMPAT
36 self.ip = "0.0.0.0"
37 self.port = 0
38
39 @classmethod
40 def stream_deserialize(cls, f, without_time=False):
41 c = cls()
42 if c.protover >= CADDR_TIME_VERSION and not without_time:
43 c.nTime = struct.unpack(b"<I", ser_read(f, 4))[0]
44 c.nServices = struct.unpack(b"<Q", ser_read(f, 8))[0]
45
46 packedIP = ser_read(f, 16)
47
48 if bytes(packedIP[0:12]) == IPV4_COMPAT: # IPv4
49 c.ip = socket.inet_ntop(socket.AF_INET, packedIP[12:16])
50 else: #IPv6
51 c.ip = socket.inet_ntop(socket.AF_INET6, packedIP)
52
53 c.port = struct.unpack(b">H", ser_read(f, 2))[0]
54 return c
55
56 def stream_serialize(self, f, without_time=False):
57 if self.protover >= CADDR_TIME_VERSION and not without_time:
58 f.write(struct.pack(b"<I", self.nTime))
59 f.write(struct.pack(b"<Q", self.nServices))
60
61 if ":" in self.ip: # determine if address is IPv6
62 f.write(socket.inet_pton(socket.AF_INET6, self.ip))
63 else:
64 f.write(self.pchReserved)
65 f.write(socket.inet_pton(socket.AF_INET, self.ip))
66
67 f.write(struct.pack(b">H", self.port))
68
69 def __repr__(self):
70 return "CAddress(nTime=%d nServices=%i ip=%s port=%i)" % (self.nTime, self.nServices, self.ip, self.port)
71
72
73class CInv(Serializable):

Callers 6

addr_pktFunction · 0.90
__init__Method · 0.85

Calls

no outgoing calls

Tested by 4