| 293 | |
| 294 | |
| 295 | class CAddress: |
| 296 | __slots__ = ("net", "ip", "nServices", "port", "time") |
| 297 | |
| 298 | # see https://github.com/bitcoin/bips/blob/master/bip-0155.mediawiki |
| 299 | NET_IPV4 = 1 |
| 300 | NET_IPV6 = 2 |
| 301 | NET_TORV3 = 4 |
| 302 | NET_I2P = 5 |
| 303 | NET_CJDNS = 6 |
| 304 | |
| 305 | ADDRV2_NET_NAME = { |
| 306 | NET_IPV4: "IPv4", |
| 307 | NET_IPV6: "IPv6", |
| 308 | NET_TORV3: "TorV3", |
| 309 | NET_I2P: "I2P", |
| 310 | NET_CJDNS: "CJDNS" |
| 311 | } |
| 312 | |
| 313 | ADDRV2_ADDRESS_LENGTH = { |
| 314 | NET_IPV4: 4, |
| 315 | NET_IPV6: 16, |
| 316 | NET_TORV3: 32, |
| 317 | NET_I2P: 32, |
| 318 | NET_CJDNS: 16 |
| 319 | } |
| 320 | |
| 321 | I2P_PAD = "====" |
| 322 | |
| 323 | def __init__(self): |
| 324 | self.time = 0 |
| 325 | self.nServices = 1 |
| 326 | self.net = self.NET_IPV4 |
| 327 | self.ip = "0.0.0.0" |
| 328 | self.port = 0 |
| 329 | |
| 330 | def __eq__(self, other): |
| 331 | return self.net == other.net and self.ip == other.ip and self.nServices == other.nServices and self.port == other.port and self.time == other.time |
| 332 | |
| 333 | def deserialize(self, f, *, with_time=True): |
| 334 | """Deserialize from addrv1 format (pre-BIP155)""" |
| 335 | if with_time: |
| 336 | # VERSION messages serialize CAddress objects without time |
| 337 | self.time = int.from_bytes(f.read(4), "little") |
| 338 | self.nServices = int.from_bytes(f.read(8), "little") |
| 339 | # We only support IPv4 which means skip 12 bytes and read the next 4 as IPv4 address. |
| 340 | f.read(12) |
| 341 | self.net = self.NET_IPV4 |
| 342 | self.ip = socket.inet_ntoa(f.read(4)) |
| 343 | self.port = int.from_bytes(f.read(2), "big") |
| 344 | |
| 345 | def serialize(self, *, with_time=True): |
| 346 | """Serialize in addrv1 format (pre-BIP155)""" |
| 347 | assert_equal(self.net, self.NET_IPV4) |
| 348 | r = b"" |
| 349 | if with_time: |
| 350 | # VERSION messages serialize CAddress objects without time |
| 351 | r += self.time.to_bytes(4, "little") |
| 352 | r += self.nServices.to_bytes(8, "little") |
no outgoing calls
no test coverage detected