1 2 3 4 5 6 7 8 9 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552
crate::ix!();
/**
| Network address.
|
*/
#[derive(Debug,Serialize,Deserialize,Clone,Hash)]
pub struct NetAddr {
/**
| Raw representation of the network address.
|
| In network byte order (big endian) for
| IPv4 and IPv6.
|
*/
pub addr: PreVector<u8,ADDR_IPV6_SIZE>,
/**
| Network to which this address belongs.
|
*/
pub net: Network,
/**
| Scope id if scoped/link-local IPV6
| address.
|
| See https://tools.ietf.org/html/rfc4007
|
*/
pub scope_id: u32,
}
impl Default for NetAddr {
/**
| Construct an unspecified IPv6 network
| address (::/128).
|
| -----------
| @note
|
| This address is considered invalid
| by
|
| CNetAddr::IsValid()
|
*/
fn default() -> Self {
Self {
addr: PreVector::with_capacity(ADDR_IPV6_SIZE),
net: Network::NET_IPV6,
scope_id: 0,
}
}
}
pub mod net_addr {
use super::*;
/**
| BIP155 network ids recognized by this
| software.
|
*/
#[repr(u8)]
pub enum BIP155Network {
IPV4 = 1,
IPV6 = 2,
TORV2 = 3,
TORV3 = 4,
I2P = 5,
CJDNS = 6,
}
/**
| Size of CNetAddr when serialized as
| ADDRv1 (pre-BIP155) (in bytes).
|
*/
pub const V1_SERIALIZATION_SIZE: usize = ADDR_IPV6_SIZE;
/**
| Maximum size of an address as defined
| in BIP155 (in bytes).
|
| This is only the size of the address,
| not the entire CNetAddr object when
| serialized.
|
*/
pub const MAX_ADDRV2_SIZE: usize = 512;
}
//-------------------------------------------[.cpp/bitcoin/src/netaddress.cpp]
impl NetAddr {
/**
| Whether this address should be relayed
| to other peers even if we can't reach
| it ourselves.
|
*/
pub fn is_relayable(&self) -> bool {
todo!();
/*
return IsIPv4() || IsIPv6() || IsTor() || IsI2P();
*/
}
/**
| Serialize to a stream.
|
*/
pub fn serialize<Stream>(&self, s: &mut Stream) {
todo!();
/*
if (s.GetVersion() & ADDRV2_FORMAT) {
SerializeV2Stream(s);
} else {
SerializeV1Stream(s);
}
*/
}
/**
| Unserialize from a stream.
|
*/
pub fn unserialize<Stream>(&mut self, s: &mut Stream) {
todo!();
/*
if (s.GetVersion() & ADDRV2_FORMAT) {
UnserializeV2Stream(s);
} else {
UnserializeV1Stream(s);
}
*/
}
/**
| Serialize in pre-ADDRv2/BIP155 format
| to an array.
|
*/
pub fn serialize_v1array(&self, arr: &mut [u8; net_addr::V1_SERIALIZATION_SIZE]) {
todo!();
/*
size_t prefix_size;
switch (m_net) {
case NET_IPV6:
assert(m_addr.size() == sizeof(arr));
memcpy(arr, m_addr.data(), m_addr.size());
return;
case NET_IPV4:
prefix_size = sizeof(IPV4_IN_IPV6_PREFIX);
assert(prefix_size + m_addr.size() == sizeof(arr));
memcpy(arr, IPV4_IN_IPV6_PREFIX.data(), prefix_size);
memcpy(arr + prefix_size, m_addr.data(), m_addr.size());
return;
case NET_INTERNAL:
prefix_size = sizeof(INTERNAL_IN_IPV6_PREFIX);
assert(prefix_size + m_addr.size() == sizeof(arr));
memcpy(arr, INTERNAL_IN_IPV6_PREFIX.data(), prefix_size);
memcpy(arr + prefix_size, m_addr.data(), m_addr.size());
return;
case NET_ONION:
case NET_I2P:
case NET_CJDNS:
break;
case NET_UNROUTABLE:
case NET_MAX:
assert(false);
} // no default case, so the compiler can warn about missing cases
// Serialize ONION, I2P and CJDNS as all-zeros.
memset(arr, 0x0, V1_SERIALIZATION_SIZE);
*/
}
/**
| Serialize in pre-ADDRv2/BIP155 format
| to a stream.
|
*/
pub fn serialize_v1stream<Stream>(&self, s: &mut Stream) {
todo!();
/*
uint8_t serialized[V1_SERIALIZATION_SIZE];
SerializeV1Array(serialized);
s << serialized;
*/
}
/**
| Serialize as ADDRv2 / BIP155.
|
*/
pub fn serialize_v2stream<Stream>(&self, s: &mut Stream) {
todo!();
/*
if (IsInternal()) {
// Serialize NET_INTERNAL as embedded in IPv6. We need to
// serialize such addresses from addrman.
s << static_cast<uint8_t>(BIP155Network::IPV6);
s << COMPACTSIZE(ADDR_IPV6_SIZE);
SerializeV1Stream(s);
return;
}
s << static_cast<uint8_t>(GetBIP155Network());
s << m_addr;
*/
}
/**
| Unserialize from a pre-ADDRv2/BIP155
| format from an array.
|
*/
pub fn unserialize_v1array(&mut self, arr: &mut [u8; net_addr::V1_SERIALIZATION_SIZE]) {
todo!();
/*
// Use SetLegacyIPv6() so that m_net is set correctly. For example
// ::FFFF:0102:0304 should be set as m_net=NET_IPV4 (1.2.3.4).
SetLegacyIPv6(arr);
*/
}
/**
| Unserialize from a pre-ADDRv2/BIP155
| format from a stream.
|
*/
pub fn unserialize_v1stream<Stream>(&mut self, s: &mut Stream) {
todo!();
/*
uint8_t serialized[V1_SERIALIZATION_SIZE];
s >> serialized;
UnserializeV1Array(serialized);
*/
}
/**
| Unserialize from a ADDRv2 / BIP155 format.
|
*/
pub fn unserialize_v2stream<Stream>(&mut self, s: &mut Stream) {
todo!();
/*
uint8_t bip155_net;
s >> bip155_net;
size_t address_size;
s >> COMPACTSIZE(address_size);
if (address_size > MAX_ADDRV2_SIZE) {
throw std::ios_base::failure(strprintf(
"Address too long: %u > %u", address_size, MAX_ADDRV2_SIZE));
}
m_scope_id = 0;
if (SetNetFromBIP155Network(bip155_net, address_size)) {
m_addr.resize(address_size);
s >> MakeSpan(m_addr);
if (m_net != NET_IPV6) {
return;
}
// Do some special checks on IPv6 addresses.
// Recognize NET_INTERNAL embedded in IPv6, such addresses are not
// gossiped but could be coming from addrman, when unserializing from
// disk.
if (HasPrefix(m_addr, INTERNAL_IN_IPV6_PREFIX)) {
m_net = NET_INTERNAL;
memmove(m_addr.data(), m_addr.data() + INTERNAL_IN_IPV6_PREFIX.size(),
ADDR_INTERNAL_SIZE);
m_addr.resize(ADDR_INTERNAL_SIZE);
return;
}
if (!HasPrefix(m_addr, IPV4_IN_IPV6_PREFIX) &&
!HasPrefix(m_addr, TORV2_IN_IPV6_PREFIX)) {
return;
}
// IPv4 and TORv2 are not supposed to be embedded in IPv6 (like in V1
// encoding). Unserialize as !IsValid(), thus ignoring them.
} else {
// If we receive an unknown BIP155 network id (from the future?) then
// ignore the address - unserialize as !IsValid().
s.ignore(address_size);
}
// Mimic a default-constructed CNetAddr object which is !IsValid() and thus
// will not be gossiped, but continue reading next addresses from the stream.
m_net = NET_IPV6;
m_addr.assign(ADDR_IPV6_SIZE, 0x0);
*/
}
/**
| Get the BIP155 network id of this address.
|
| Must not be called for IsInternal()
| objects.
|
|
| -----------
| @return
|
| BIP155 network id, except TORV2 which
| is no longer supported.
|
*/
pub fn get_bip155network(&self) -> net_addr::BIP155Network {
todo!();
/*
switch (m_net) {
case NET_IPV4:
return BIP155Network::IPV4;
case NET_IPV6:
return BIP155Network::IPV6;
case NET_ONION:
return BIP155Network::TORV3;
case NET_I2P:
return BIP155Network::I2P;
case NET_CJDNS:
return BIP155Network::CJDNS;
case NET_INTERNAL: // should have been handled before calling this function
case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
case NET_MAX: // m_net is never and should not be set to NET_MAX
assert(false);
} // no default case, so the compiler can warn about missing cases
assert(false);
*/
}
/**
| Set `m_net` from the provided BIP155
| network id and size after validation.
|
| -----------
| @return
|
| true the network was recognized, is
| valid and `m_net` was set
| ----------
| @return
|
| false not recognised (from future?)
| and should be silently ignored @throws
| std::ios_base::failure if the network
| is one of the BIP155 founding networks
| (id 1..6) with wrong address size.
|
*/
pub fn set_net_from_bip155network(&mut self,
possible_bip155_net: u8,
address_size: usize) -> bool {
todo!();
/*
switch (possible_bip155_net) {
case BIP155Network::IPV4:
if (address_size == ADDR_IPV4_SIZE) {
m_net = NET_IPV4;
return true;
}
throw std::ios_base::failure(
strprintf("BIP155 IPv4 address with length %u (should be %u)", address_size,
ADDR_IPV4_SIZE));
case BIP155Network::IPV6:
if (address_size == ADDR_IPV6_SIZE) {
m_net = NET_IPV6;
return true;
}
throw std::ios_base::failure(
strprintf("BIP155 IPv6 address with length %u (should be %u)", address_size,
ADDR_IPV6_SIZE));
case BIP155Network::TORV3:
if (address_size == ADDR_TORV3_SIZE) {
m_net = NET_ONION;
return true;
}
throw std::ios_base::failure(
strprintf("BIP155 TORv3 address with length %u (should be %u)", address_size,
ADDR_TORV3_SIZE));
case BIP155Network::I2P:
if (address_size == ADDR_I2P_SIZE) {
m_net = NET_I2P;
return true;
}
throw std::ios_base::failure(
strprintf("BIP155 I2P address with length %u (should be %u)", address_size,
ADDR_I2P_SIZE));
case BIP155Network::CJDNS:
if (address_size == ADDR_CJDNS_SIZE) {
m_net = NET_CJDNS;
return true;
}
throw std::ios_base::failure(
strprintf("BIP155 CJDNS address with length %u (should be %u)", address_size,
ADDR_CJDNS_SIZE));
}
// Don't throw on addresses with unknown network ids (maybe from the future).
// Instead silently drop them and have the unserialization code consume
// subsequent ones which may be known to us.
return false;
*/
}
pub fn setip(&mut self, ip_in: &NetAddr) {
todo!();
/*
// Size check.
switch (ipIn.m_net) {
case NET_IPV4:
assert(ipIn.m_addr.size() == ADDR_IPV4_SIZE);
break;
case NET_IPV6:
assert(ipIn.m_addr.size() == ADDR_IPV6_SIZE);
break;
case NET_ONION:
assert(ipIn.m_addr.size() == ADDR_TORV3_SIZE);
break;
case NET_I2P:
assert(ipIn.m_addr.size() == ADDR_I2P_SIZE);
break;
case NET_CJDNS:
assert(ipIn.m_addr.size() == ADDR_CJDNS_SIZE);
break;
case NET_INTERNAL:
assert(ipIn.m_addr.size() == ADDR_INTERNAL_SIZE);
break;
case NET_UNROUTABLE:
case NET_MAX:
assert(false);
} // no default case, so the compiler can warn about missing cases
m_net = ipIn.m_net;
m_addr = ipIn.m_addr;
*/
}
/**
| Set from a legacy IPv6 address.
|
| Legacy IPv6 address may be a normal IPv6
| address, or another address (e.g. IPv4)
| disguised as IPv6.
|
| This encoding is used in the legacy `addr`
| encoding.
|
*/
pub fn set_legacy_ipv6(&mut self, ipv6: &[u8]) {
todo!();
/*
assert(ipv6.size() == ADDR_IPV6_SIZE);
size_t skip{0};
if (HasPrefix(ipv6, IPV4_IN_IPV6_PREFIX)) {
// IPv4-in-IPv6
m_net = NET_IPV4;
skip = sizeof(IPV4_IN_IPV6_PREFIX);
} else if (HasPrefix(ipv6, TORV2_IN_IPV6_PREFIX)) {
// TORv2-in-IPv6 (unsupported). Unserialize as !IsValid(), thus ignoring them.
// Mimic a default-constructed CNetAddr object which is !IsValid() and thus
// will not be gossiped, but continue reading next addresses from the stream.
m_net = NET_IPV6;
m_addr.assign(ADDR_IPV6_SIZE, 0x0);
return;
} else if (HasPrefix(ipv6, INTERNAL_IN_IPV6_PREFIX)) {
// Internal-in-IPv6
m_net = NET_INTERNAL;
skip = sizeof(INTERNAL_IN_IPV6_PREFIX);
} else {
// IPv6
m_net = NET_IPV6;
}
m_addr.assign(ipv6.begin() + skip, ipv6.end());
*/
}
/**
| Create an "internal" address that represents
| a name or FQDN. AddrMan uses these fake
| addresses to keep track of which DNS
| seeds were used.
|
|
| -----------
| @return
|
| Whether or not the operation was successful.
| @see NET_INTERNAL, INTERNAL_IN_IPV6_PREFIX,
| CNetAddr::IsInternal(), CNetAddr::IsRFC4193()
|
*/
pub fn set_internal(&mut self, name: &str) -> bool {
todo!();
/*
if (name.empty()) {
return false;
}
m_net = NET_INTERNAL;
unsigned char hash[32] = {};
CSHA256().Write((const unsigned char*)name.data(), name.size()).Finalize(hash);
m_addr.assign(hash, hash + ADDR_INTERNAL_SIZE);
return true;
*/
}
}
impl PartialEq<NetAddr> for NetAddr {
#[inline] fn eq(&self, other: &NetAddr) -> bool {
todo!();
/*
return a.m_net == b.m_net && a.m_addr == b.m_addr;
*/
}
}
impl Eq for NetAddr {}
impl Ord for NetAddr {
#[inline] fn cmp(&self, other: &NetAddr) -> Ordering {
todo!();
/*
return std::tie(a.m_net, a.m_addr) < std::tie(b.m_net, b.m_addr);
*/
}
}
impl PartialOrd<NetAddr> for NetAddr {
#[inline] fn partial_cmp(&self, other: &NetAddr) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl From<&InAddr> for NetAddr {
fn from(ipv_4addr: &InAddr) -> Self {
todo!();
/*
m_net = NET_IPV4;
const uint8_t* ptr = reinterpret_cast<const uint8_t*>(&ipv4Addr);
m_addr.assign(ptr, ptr + ADDR_IPV4_SIZE);
*/
}
}
impl CheckIsReachable for NetAddr {
/**
| @return
|
| true if the address is in a reachable
| network, false otherwise
|
*/
fn is_reachable(&self) -> bool {
self.get_network().is_reachable()
}
}
impl NetAddr {
/**
| Parse a Tor or I2P address and set this
| object to it.
|
| -----------
| @param[in] addr
|
| Address to parse, for example pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion
| or ukeu3k5oycgaauneqgtnvselmt4yemvoilkln7jpvamvfx7dnkdq.b32.i2p.
|
| -----------
| @return
|
| Whether the operation was successful.
| @see CNetAddr::IsTor(), CNetAddr::IsI2P()
|
*/
pub fn set_special(&mut self, addr: &String) -> bool {
todo!();
/*
if (!ValidAsCString(addr)) {
return false;
}
if (SetTor(addr)) {
return true;
}
if (SetI2P(addr)) {
return true;
}
return false;
*/
}
/**
| Parse a Tor address and set this object
| to it.
|
| -----------
| @param[in] addr
|
| Address to parse, must be a valid C string,
| for example pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion.
|
| -----------
| @return
|
| Whether the operation was successful.
| @see CNetAddr::IsTor()
|
*/
pub fn set_tor(&mut self, addr: &String) -> bool {
todo!();
/*
static const char* suffix{".onion"};
static constexpr size_t suffix_len{6};
if (addr.size() <= suffix_len || addr.substr(addr.size() - suffix_len) != suffix) {
return false;
}
bool invalid;
const auto& input = DecodeBase32(addr.substr(0, addr.size() - suffix_len).c_str(), &invalid);
if (invalid) {
return false;
}
if (input.size() == torv3::TOTAL_LEN) {
Span<const uint8_t> input_pubkey{input.data(), ADDR_TORV3_SIZE};
Span<const uint8_t> input_checksum{input.data() + ADDR_TORV3_SIZE, torv3::CHECKSUM_LEN};
Span<const uint8_t> input_version{input.data() + ADDR_TORV3_SIZE + torv3::CHECKSUM_LEN, sizeof(torv3::VERSION)};
if (input_version != torv3::VERSION) {
return false;
}
uint8_t calculated_checksum[torv3::CHECKSUM_LEN];
torv3::Checksum(input_pubkey, calculated_checksum);
if (input_checksum != calculated_checksum) {
return false;
}
m_net = NET_ONION;
m_addr.assign(input_pubkey.begin(), input_pubkey.end());
return true;
}
return false;
*/
}
/**
| Parse an I2P address and set this object
| to it.
|
| -----------
| @param[in] addr
|
| Address to parse, must be a valid C string,
| for example ukeu3k5oycgaauneqgtnvselmt4yemvoilkln7jpvamvfx7dnkdq.b32.i2p.
|
| -----------
| @return
|
| Whether the operation was successful.
| @see CNetAddr::IsI2P()
|
*/
pub fn seti2p(&mut self, addr: &String) -> bool {
todo!();
/*
// I2P addresses that we support consist of 52 base32 characters + ".b32.i2p".
static constexpr size_t b32_len{52};
static const char* suffix{".b32.i2p"};
static constexpr size_t suffix_len{8};
if (addr.size() != b32_len + suffix_len || ToLower(addr.substr(b32_len)) != suffix) {
return false;
}
// Remove the ".b32.i2p" suffix and pad to a multiple of 8 chars, so DecodeBase32()
// can decode it.
const std::string b32_padded = addr.substr(0, b32_len) + "====";
bool invalid;
const auto& address_bytes = DecodeBase32(b32_padded.c_str(), &invalid);
if (invalid || address_bytes.size() != ADDR_I2P_SIZE) {
return false;
}
m_net = NET_I2P;
m_addr.assign(address_bytes.begin(), address_bytes.end());
return true;
*/
}
pub fn new(
ipv_6addr: &In6Addr,
scope: Option<u32>) -> Self {
let scope: u32 = scope.unwrap_or(0);
todo!();
/*
SetLegacyIPv6(Span<const uint8_t>(reinterpret_cast<const uint8_t*>(&ipv6Addr), sizeof(ipv6Addr)));
m_scope_id = scope;
*/
}
/**
| INADDR_ANY equivalent
|
*/
pub fn is_bind_any(&self) -> bool {
todo!();
/*
if (!IsIPv4() && !IsIPv6()) {
return false;
}
return std::all_of(m_addr.begin(), m_addr.end(), [](uint8_t b) { return b == 0; });
*/
}
/**
| IPv4 mapped address (::FFFF:0:0/96,
| 0.0.0.0/0)
|
*/
pub fn is_ipv4(&self) -> bool {
todo!();
/*
return m_net == NET_IPV4;
*/
}
/**
| IPv6 address (not mapped IPv4, not Tor)
|
*/
pub fn is_ipv6(&self) -> bool {
todo!();
/*
return m_net == NET_IPV6;
*/
}
/**
| IPv4 private networks (10.0.0.0/8,
| 192.168.0.0/16, 172.16.0.0/12)
|
*/
pub fn isrfc1918(&self) -> bool {
todo!();
/*
return IsIPv4() && (
m_addr[0] == 10 ||
(m_addr[0] == 192 && m_addr[1] == 168) ||
(m_addr[0] == 172 && m_addr[1] >= 16 && m_addr[1] <= 31));
*/
}
/**
| IPv4 inter-network communications
| (198.18.0.0/15)
|
*/
pub fn isrfc2544(&self) -> bool {
todo!();
/*
return IsIPv4() && m_addr[0] == 198 && (m_addr[1] == 18 || m_addr[1] == 19);
*/
}
/**
| IPv4 autoconfig (169.254.0.0/16)
|
*/
pub fn isrfc3927(&self) -> bool {
todo!();
/*
return IsIPv4() && HasPrefix(m_addr, std::array<uint8_t, 2>{169, 254});
*/
}
/**
| IPv4 ISP-level NAT (100.64.0.0/10)
|
*/
pub fn isrfc6598(&self) -> bool {
todo!();
/*
return IsIPv4() && m_addr[0] == 100 && m_addr[1] >= 64 && m_addr[1] <= 127;
*/
}
/**
| IPv4 documentation addresses (192.0.2.0/24,
| 198.51.100.0/24, 203.0.113.0/24)
|
*/
pub fn isrfc5737(&self) -> bool {
todo!();
/*
return IsIPv4() && (HasPrefix(m_addr, std::array<uint8_t, 3>{192, 0, 2}) ||
HasPrefix(m_addr, std::array<uint8_t, 3>{198, 51, 100}) ||
HasPrefix(m_addr, std::array<uint8_t, 3>{203, 0, 113}));
*/
}
/**
| IPv6 documentation address (2001:0DB8::/32)
|
*/
pub fn isrfc3849(&self) -> bool {
todo!();
/*
return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x0D, 0xB8});
*/
}
/**
| IPv6 6to4 tunnelling (2002::/16)
|
*/
pub fn isrfc3964(&self) -> bool {
todo!();
/*
return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 2>{0x20, 0x02});
*/
}
/**
| IPv6 well-known prefix for IPv4-embedded
| address (64:FF9B::/96)
|
*/
pub fn isrfc6052(&self) -> bool {
todo!();
/*
return IsIPv6() &&
HasPrefix(m_addr, std::array<uint8_t, 12>{0x00, 0x64, 0xFF, 0x9B, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
*/
}
/**
| IPv6 Teredo tunnelling (2001::/32)
|
*/
pub fn isrfc4380(&self) -> bool {
todo!();
/*
return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x00, 0x00});
*/
}
/**
| IPv6 autoconfig (FE80::/64)
|
*/
pub fn isrfc4862(&self) -> bool {
todo!();
/*
return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 8>{0xFE, 0x80, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00});
*/
}
/**
| IPv6 unique local (FC00::/7)
|
*/
pub fn isrfc4193(&self) -> bool {
todo!();
/*
return IsIPv6() && (m_addr[0] & 0xFE) == 0xFC;
*/
}
/**
| IPv6 IPv4-translated address (::FFFF:0:0:0/96)
| (actually defined in RFC2765)
|
*/
pub fn isrfc6145(&self) -> bool {
todo!();
/*
return IsIPv6() &&
HasPrefix(m_addr, std::array<uint8_t, 12>{0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00});
*/
}
/**
| IPv6 ORCHID (deprecated) (2001:10::/28)
|
*/
pub fn isrfc4843(&self) -> bool {
todo!();
/*
return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 3>{0x20, 0x01, 0x00}) &&
(m_addr[3] & 0xF0) == 0x10;
*/
}
/**
| IPv6 ORCHIDv2 (2001:20::/28)
|
*/
pub fn isrfc7343(&self) -> bool {
todo!();
/*
return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 3>{0x20, 0x01, 0x00}) &&
(m_addr[3] & 0xF0) == 0x20;
*/
}
/**
| IPv6 Hurricane Electric - https://he.net
| (2001:0470::/36)
|
*/
pub fn is_he_net(&self) -> bool {
todo!();
/*
return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x04, 0x70});
*/
}
/**
| Check whether this object represents
| a TOR address. @see CNetAddr::SetSpecial(const
| std::string &)
|
*/
pub fn is_tor(&self) -> bool {
todo!();
/*
return m_net == NET_ONION;
*/
}
/**
| Check whether this object represents
| an I2P address.
|
*/
pub fn isi2p(&self) -> bool {
todo!();
/*
return m_net == NET_I2P;
*/
}
/**
| Check whether this object represents
| a CJDNS address.
|
*/
pub fn iscjdns(&self) -> bool {
todo!();
/*
return m_net == NET_CJDNS;
*/
}
pub fn is_local(&self) -> bool {
todo!();
/*
// IPv4 loopback (127.0.0.0/8 or 0.0.0.0/8)
if (IsIPv4() && (m_addr[0] == 127 || m_addr[0] == 0)) {
return true;
}
// IPv6 loopback (::1/128)
static const unsigned char pchLocal[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
if (IsIPv6() && memcmp(m_addr.data(), pchLocal, sizeof(pchLocal)) == 0) {
return true;
}
return false;
*/
}
/**
| @note
|
| A valid address may or may not be publicly
| routable on the global internet. As
| in, the set of valid addresses is a superset
| of the set of publicly routable addresses.
| @see CNetAddr::IsRoutable()
|
| -----------
| @return
|
| Whether or not this network address
| is a valid address that @a could be used
| to refer to an actual host.
|
*/
pub fn is_valid(&self) -> bool {
todo!();
/*
// unspecified IPv6 address (::/128)
unsigned char ipNone6[16] = {};
if (IsIPv6() && memcmp(m_addr.data(), ipNone6, sizeof(ipNone6)) == 0) {
return false;
}
// CJDNS addresses always start with 0xfc
if (IsCJDNS() && (m_addr[0] != 0xFC)) {
return false;
}
// documentation IPv6 address
if (IsRFC3849())
return false;
if (IsInternal())
return false;
if (IsIPv4()) {
const uint32_t addr = ReadBE32(m_addr.data());
if (addr == INADDR_ANY || addr == INADDR_NONE) {
return false;
}
}
return true;
*/
}
/**
| @note
|
| A routable address is always valid.
| As in, the set of routable addresses
| is a subset of the set of valid addresses.
| @see CNetAddr::IsValid()
|
| -----------
| @return
|
| Whether or not this network address
| is publicly routable on the global internet.
|
*/
pub fn is_routable(&self) -> bool {
todo!();
/*
return IsValid() && !(IsRFC1918() || IsRFC2544() || IsRFC3927() || IsRFC4862() || IsRFC6598() || IsRFC5737() || IsRFC4193() || IsRFC4843() || IsRFC7343() || IsLocal() || IsInternal());
*/
}
/**
| @return
|
| Whether or not this is a dummy address
| that represents a name. @see CNetAddr::SetInternal(const
| std::string &)
|
*/
pub fn is_internal(&self) -> bool {
todo!();
/*
return m_net == NET_INTERNAL;
*/
}
/**
| Check if the current object can be serialized
| in pre-ADDRv2/BIP155 format.
|
*/
pub fn is_addr_v1compatible(&self) -> bool {
todo!();
/*
switch (m_net) {
case NET_IPV4:
case NET_IPV6:
case NET_INTERNAL:
return true;
case NET_ONION:
case NET_I2P:
case NET_CJDNS:
return false;
case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
case NET_MAX: // m_net is never and should not be set to NET_MAX
assert(false);
} // no default case, so the compiler can warn about missing cases
assert(false);
*/
}
pub fn get_network(&self) -> Network {
todo!();
/*
if (IsInternal())
return NET_INTERNAL;
if (!IsRoutable())
return NET_UNROUTABLE;
return m_net;
*/
}
pub fn to_stringip(&self) -> String {
todo!();
/*
switch (m_net) {
case NET_IPV4:
return IPv4ToString(m_addr);
case NET_IPV6:
return IPv6ToString(m_addr, m_scope_id);
case NET_ONION:
return OnionToString(m_addr);
case NET_I2P:
return EncodeBase32(m_addr, false /* don't pad with = */) + ".b32.i2p";
case NET_CJDNS:
return IPv6ToString(m_addr, 0);
case NET_INTERNAL:
return EncodeBase32(m_addr) + ".internal";
case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
case NET_MAX: // m_net is never and should not be set to NET_MAX
assert(false);
} // no default case, so the compiler can warn about missing cases
assert(false);
*/
}
pub fn to_string(&self) -> String {
todo!();
/*
return ToStringIP();
*/
}
/**
| Try to get our IPv4 address.
|
| -----------
| @param[out] pipv4Addr
|
| The in_addr struct to which to copy.
|
| -----------
| @return
|
| Whether or not the operation was successful,
| in particular, whether or not our address
| was an IPv4 address. @see CNetAddr::IsIPv4()
|
*/
pub fn get_in_addr(&self, pipv_4addr: *mut InAddr) -> bool {
todo!();
/*
if (!IsIPv4())
return false;
assert(sizeof(*pipv4Addr) == m_addr.size());
memcpy(pipv4Addr, m_addr.data(), m_addr.size());
return true;
*/
}
/**
| Try to get our IPv6 address.
|
| -----------
| @param[out] pipv6Addr
|
| The in6_addr struct to which to copy.
|
| -----------
| @return
|
| Whether or not the operation was successful,
| in particular, whether or not our address
| was an IPv6 address. @see CNetAddr::IsIPv6()
|
*/
pub fn get_in_6addr(&self, pipv_6addr: *mut In6Addr) -> bool {
todo!();
/*
if (!IsIPv6()) {
return false;
}
assert(sizeof(*pipv6Addr) == m_addr.size());
memcpy(pipv6Addr, m_addr.data(), m_addr.size());
return true;
*/
}
/**
| Whether this address has a linked IPv4
| address (see GetLinkedIPv4()).
|
*/
pub fn has_linked_ipv4(&self) -> bool {
todo!();
/*
return IsRoutable() && (IsIPv4() || IsRFC6145() || IsRFC6052() || IsRFC3964() || IsRFC4380());
*/
}
/**
| For IPv4, mapped IPv4, SIIT translated
|
| IPv4, Teredo, 6to4 tunneled addresses,
| return the relevant IPv4 address as
| a uint32.
|
*/
pub fn get_linked_ipv4(&self) -> u32 {
todo!();
/*
if (IsIPv4()) {
return ReadBE32(m_addr.data());
} else if (IsRFC6052() || IsRFC6145()) {
// mapped IPv4, SIIT translated IPv4: the IPv4 address is the last 4 bytes of the address
return ReadBE32(MakeSpan(m_addr).last(ADDR_IPV4_SIZE).data());
} else if (IsRFC3964()) {
// 6to4 tunneled IPv4: the IPv4 address is in bytes 2-6
return ReadBE32(MakeSpan(m_addr).subspan(2, ADDR_IPV4_SIZE).data());
} else if (IsRFC4380()) {
// Teredo tunneled IPv4: the IPv4 address is in the last 4 bytes of the address, but bitflipped
return ~ReadBE32(MakeSpan(m_addr).last(ADDR_IPV4_SIZE).data());
}
assert(false);
*/
}
pub fn get_net_class(&self) -> Network {
todo!();
/*
// Make sure that if we return NET_IPV6, then IsIPv6() is true. The callers expect that.
// Check for "internal" first because such addresses are also !IsRoutable()
// and we don't want to return NET_UNROUTABLE in that case.
if (IsInternal()) {
return NET_INTERNAL;
}
if (!IsRoutable()) {
return NET_UNROUTABLE;
}
if (HasLinkedIPv4()) {
return NET_IPV4;
}
return m_net;
*/
}
/**
| The AS on the BGP path to the node we use
| to diversify peers in AddrMan bucketing
| based on the AS infrastructure.
|
| The ip->AS mapping depends on how asmap is
| constructed.
*/
pub fn get_mappedas(&self, asmap: &Vec<bool>) -> u32 {
todo!();
/*
uint32_t net_class = GetNetClass();
if (asmap.size() == 0 || (net_class != NET_IPV4 && net_class != NET_IPV6)) {
return 0; // Indicates not found, safe because AS0 is reserved per RFC7607.
}
std::vector<bool> ip_bits(128);
if (HasLinkedIPv4()) {
// For lookup, treat as if it was just an IPv4 address (IPV4_IN_IPV6_PREFIX + IPv4 bits)
for (int8_t byte_i = 0; byte_i < 12; ++byte_i) {
for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
ip_bits[byte_i * 8 + bit_i] = (IPV4_IN_IPV6_PREFIX[byte_i] >> (7 - bit_i)) & 1;
}
}
uint32_t ipv4 = GetLinkedIPv4();
for (int i = 0; i < 32; ++i) {
ip_bits[96 + i] = (ipv4 >> (31 - i)) & 1;
}
} else {
// Use all 128 bits of the IPv6 address otherwise
assert(IsIPv6());
for (int8_t byte_i = 0; byte_i < 16; ++byte_i) {
uint8_t cur_byte = m_addr[byte_i];
for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
ip_bits[byte_i * 8 + bit_i] = (cur_byte >> (7 - bit_i)) & 1;
}
}
}
uint32_t mapped_as = Interpret(asmap, ip_bits);
return mapped_as;
*/
}
/**
| Get the canonical identifier of our
| network group
|
| The groups are assigned in a way where
| it should be costly for an attacker to
| obtain addresses with many different
| group identifiers, even if it is cheap
| to obtain addresses with the same identifier.
|
| -----------
| @note
|
| No two connections will be attempted
| to addresses with the same network group.
|
*/
pub fn get_group(&self, asmap: &Vec<bool>) -> Vec<u8> {
todo!();
/*
std::vector<unsigned char> vchRet;
uint32_t net_class = GetNetClass();
// If non-empty asmap is supplied and the address is IPv4/IPv6,
// return ASN to be used for bucketing.
uint32_t asn = GetMappedAS(asmap);
if (asn != 0) { // Either asmap was empty, or address has non-asmappable net class (e.g. TOR).
vchRet.push_back(NET_IPV6); // IPv4 and IPv6 with same ASN should be in the same bucket
for (int i = 0; i < 4; i++) {
vchRet.push_back((asn >> (8 * i)) & 0xFF);
}
return vchRet;
}
vchRet.push_back(net_class);
int nBits{0};
if (IsLocal()) {
// all local addresses belong to the same group
} else if (IsInternal()) {
// all internal-usage addresses get their own group
nBits = ADDR_INTERNAL_SIZE * 8;
} else if (!IsRoutable()) {
// all other unroutable addresses belong to the same group
} else if (HasLinkedIPv4()) {
// IPv4 addresses (and mapped IPv4 addresses) use /16 groups
uint32_t ipv4 = GetLinkedIPv4();
vchRet.push_back((ipv4 >> 24) & 0xFF);
vchRet.push_back((ipv4 >> 16) & 0xFF);
return vchRet;
} else if (IsTor() || IsI2P() || IsCJDNS()) {
nBits = 4;
} else if (IsHeNet()) {
// for he.net, use /36 groups
nBits = 36;
} else {
// for the rest of the IPv6 network, use /32 groups
nBits = 32;
}
// Push our address onto vchRet.
const size_t num_bytes = nBits / 8;
vchRet.insert(vchRet.end(), m_addr.begin(), m_addr.begin() + num_bytes);
nBits %= 8;
// ...for the last byte, push nBits and for the rest of the byte push 1's
if (nBits > 0) {
assert(num_bytes < m_addr.size());
vchRet.push_back(m_addr[num_bytes] | ((1 << (8 - nBits)) - 1));
}
return vchRet;
*/
}
pub fn get_addr_bytes(&self) -> Vec<u8> {
todo!();
/*
if (IsAddrV1Compatible()) {
uint8_t serialized[V1_SERIALIZATION_SIZE];
SerializeV1Array(serialized);
return {std::begin(serialized), std::end(serialized)};
}
return std::vector<unsigned char>(m_addr.begin(), m_addr.end());
*/
}
pub fn get_hash(&self) -> u64 {
todo!();
/*
uint256 hash = Hash(m_addr);
uint64_t nRet;
memcpy(&nRet, &hash, sizeof(nRet));
return nRet;
*/
}
/**
| Calculates a metric for how reachable
| (*this) is from a given partner
|
*/
pub fn get_reachability_from(&self, paddr_partner: *const NetAddr) -> i32 {
todo!();
/*
enum Reachability {
REACH_UNREACHABLE,
REACH_DEFAULT,
REACH_TEREDO,
REACH_IPV6_WEAK,
REACH_IPV4,
REACH_IPV6_STRONG,
REACH_PRIVATE
};
if (!IsRoutable() || IsInternal())
return REACH_UNREACHABLE;
int ourNet = GetExtNetwork(this);
int theirNet = GetExtNetwork(paddrPartner);
bool fTunnel = IsRFC3964() || IsRFC6052() || IsRFC6145();
switch(theirNet) {
case NET_IPV4:
switch(ourNet) {
default: return REACH_DEFAULT;
case NET_IPV4: return REACH_IPV4;
}
case NET_IPV6:
switch(ourNet) {
default: return REACH_DEFAULT;
case NET_TEREDO: return REACH_TEREDO;
case NET_IPV4: return REACH_IPV4;
case NET_IPV6: return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG; // only prefer giving our IPv6 address if it's not tunnelled
}
case NET_ONION:
switch(ourNet) {
default: return REACH_DEFAULT;
case NET_IPV4: return REACH_IPV4; // Tor users can connect to IPv4 as well
case NET_ONION: return REACH_PRIVATE;
}
case NET_I2P:
switch (ourNet) {
case NET_I2P: return REACH_PRIVATE;
default: return REACH_DEFAULT;
}
case NET_TEREDO:
switch(ourNet) {
default: return REACH_DEFAULT;
case NET_TEREDO: return REACH_TEREDO;
case NET_IPV6: return REACH_IPV6_WEAK;
case NET_IPV4: return REACH_IPV4;
}
case NET_UNKNOWN:
case NET_UNROUTABLE:
default:
switch(ourNet) {
default: return REACH_DEFAULT;
case NET_TEREDO: return REACH_TEREDO;
case NET_IPV6: return REACH_IPV6_WEAK;
case NET_IPV4: return REACH_IPV4;
case NET_ONION: return REACH_PRIVATE; // either from Tor, or don't care about our address
}
}
*/
}
}