IpNet refactoring (#354)

Remove `result` usage.
Remove cast[T] usage.
Add more helpers for TransportAddress checks.
Add isGlobal() helper to check if TransportAddress is globally accessed IP address.
Add tests.
Add more tests for sub.
Add TransportAddress.toHex().
This commit is contained in:
Eugene Kabanov 2023-02-16 18:18:05 +02:00 committed by GitHub
parent 5d3da66e56
commit 1e743dc415
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 916 additions and 380 deletions

View File

@ -13,7 +13,7 @@ else:
{.push raises: [].}
import std/[os, strutils, nativesockets, net]
import stew/base10
import stew/[base10, endians2, byteutils]
import ../asyncloop
export net
@ -137,19 +137,16 @@ var
proc `==`*(lhs, rhs: TransportAddress): bool =
## Compare two transport addresses ``lhs`` and ``rhs``. Return ``true`` if
## addresses are equal.
if lhs.family != rhs.family:
return false
if (lhs.family != rhs.family): return false
case lhs.family
of AddressFamily.None:
true
of AddressFamily.IPv4:
equalMem(unsafeAddr lhs.address_v4[0],
unsafeAddr rhs.address_v4[0], sizeof(lhs.address_v4)) and
(lhs.port == rhs.port)
if lhs.port != rhs.port: return false
lhs.address_v4 == rhs.address_v4
of AddressFamily.IPv6:
equalMem(unsafeAddr lhs.address_v6[0],
unsafeAddr rhs.address_v6[0], sizeof(lhs.address_v6)) and
(lhs.port == rhs.port)
if lhs.port != rhs.port: return false
lhs.address_v6 == rhs.address_v6
of AddressFamily.Unix:
equalMem(unsafeAddr lhs.address_un[0],
unsafeAddr rhs.address_un[0], sizeof(lhs.address_un))
@ -195,8 +192,20 @@ proc `$`*(address: TransportAddress): string =
$cast[cstring](addr buffer)
else:
"/"
else:
"Unknown address family: " & $address.family
of AddressFamily.None:
"None"
proc toHex*(address: TransportAddress): string =
## Returns hexadecimal representation of ``address`.
case address.family
of AddressFamily.IPv4:
"0x" & address.address_v4.toHex()
of AddressFamily.IPv6:
"0x" & address.address_v6.toHex()
of AddressFamily.Unix:
"0x" & address.address_un.toHex()
of AddressFamily.None:
"None"
proc initTAddress*(address: string): TransportAddress {.
raises: [Defect, TransportAddressError].} =

File diff suppressed because it is too large Load Diff

View File

@ -145,11 +145,13 @@ suite "Network utilities test suite":
test "IPv4 networks test":
var a: TransportAddress
check:
a.isUnspecified() == true
initTAddress("0.0.0.0:0").isUnspecified() == false
a.isNone() == true
initTAddress("0.0.0.0:0").isUnspecified() == true
initTAddress("0.0.0.0:0").isNone() == false
initTAddress("0.0.0.0:0").isZero() == true
initTAddress("1.0.0.0:0").isZero() == false
initTAddress("1.0.0.0:0").isUnspecified() == false
initTAddress("127.0.0.0:0").isLoopback() == true
initTAddress("127.255.255.255:0").isLoopback() == true
@ -193,38 +195,196 @@ suite "Network utilities test suite":
initTAddress("224.0.0.0:0").isGlobalMulticast() == false
initTAddress("239.0.0.0:0").isGlobalMulticast() == false
initTAddress("239.255.255.255:0").isReserved() == false
initTAddress("240.0.0.0:0").isReserved() == true
initTAddress("250.0.0.0:0").isReserved() == true
initTAddress("255.254.254.254:0").isReserved() == true
initTAddress("198.17.255.255:0").isBenchmarking() == false
initTAddress("198.18.0.0:0").isBenchmarking() == true
initTAddress("198.18.0.1:0").isBenchmarking() == true
initTAddress("198.19.0.1:0").isBenchmarking() == true
initTAddress("198.19.255.255:0").isBenchmarking() == true
initTAddress("198.20.0.0:0").isBenchmarking() == false
initTAddress("192.0.1.255:0").isDocumentation() == false
initTAddress("192.0.2.0:0").isDocumentation() == true
initTAddress("192.0.2.255:0").isDocumentation() == true
initTAddress("192.0.3.0:0").isDocumentation() == false
initTAddress("198.51.99.255:0").isDocumentation() == false
initTAddress("198.51.100.0:0").isDocumentation() == true
initTAddress("198.51.100.255:0").isDocumentation() == true
initTAddress("198.51.101.0:0").isDocumentation() == false
initTAddress("203.0.112.255:0").isDocumentation() == false
initTAddress("203.0.113.0:0").isDocumentation() == true
initTAddress("203.0.113.255:0").isDocumentation() == true
initTAddress("203.0.114.0:0").isDocumentation() == false
initTAddress("0.0.0.0:0").isBroadcast() == false
initTAddress("127.0.0.255:0").isBroadcast() == false
initTAddress("127.0.255.255:0").isBroadcast() == false
initTAddress("127.255.255.255:0").isBroadcast() == false
initTAddress("255.255.255.255:0").isBroadcast() == true
initTAddress("100.63.255.255:0").isShared() == false
initTAddress("100.64.0.0:0").isShared() == true
initTAddress("100.64.0.1:0").isShared() == true
initTAddress("100.127.255.255:0").isShared() == true
initTAddress("100.128.0.0:0").isShared() == false
a.isGlobal() == false
initTAddress("0.0.0.0:0").isGlobal() == false
initTAddress("127.0.0.0:0").isGlobal() == false
initTAddress("10.0.0.0:0").isGlobal() == false
initTAddress("10.255.255.255:0").isGlobal() == false
initTAddress("172.16.0.0:0").isGlobal() == false
initTAddress("172.31.255.255:0").isGlobal() == false
initTAddress("192.168.0.0:0").isGlobal() == false
initTAddress("192.168.255.255:0").isGlobal() == false
initTAddress("100.64.0.0:0").isGlobal() == false
initTAddress("100.64.0.1:0").isGlobal() == false
initTAddress("100.127.255.255:0").isGlobal() == false
initTAddress("169.254.0.0:0").isGlobal() == false
initTAddress("169.254.255.255:0").isGlobal() == false
initTAddress("192.0.0.0:0").isGlobal() == false
initTAddress("192.0.0.255:0").isGlobal() == false
initTAddress("192.0.2.0:0").isGlobal() == false
initTAddress("192.0.2.255:0").isGlobal() == false
initTAddress("198.51.100.0:0").isGlobal() == false
initTAddress("198.51.100.255:0").isGlobal() == false
initTAddress("203.0.113.0:0").isGlobal() == false
initTAddress("203.0.113.255:0").isGlobal() == false
initTAddress("198.18.0.0:0").isGlobal() == false
initTAddress("198.18.0.1:0").isGlobal() == false
initTAddress("198.19.0.1:0").isGlobal() == false
initTAddress("198.19.255.255:0").isGlobal() == false
initTAddress("240.0.0.0:0").isGlobal() == false
initTAddress("250.0.0.0:0").isGlobal() == false
initTAddress("255.254.254.254:0").isGlobal() == false
initTAddress("255.255.255.255:0").isGlobal() == false
initTAddress("1.1.1.1:0").isGlobal() == true
initTAddress("8.8.8.8:0").isGlobal() == true
test "IPv6 networks test":
var a: TransportAddress
check:
initTAddress("[::]:0").isUnspecified() == false
initTAddress("[::]:0").isNone() == false
initTAddress("[::]:0").isUnspecified() == true
initTAddress("[::]:0").isZero() == true
initTAddress("[::1]:0").isZero() == false
initTAddress("[::1]:0").isUnspecified() == false
initTAddress("[::1]:0").isLoopback() == true
initTAddress("[::2]:0").isLoopback() == false
initTAddress("[FF00::]:0").isMulticast() == true
initTAddress("[FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF]:0").isMulticast() == true
initTAddress(
"[FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF]:0"
).isMulticast() == true
initTAddress("[F000::]:0").isMulticast() == false
initTAddress("[::]:0").isAnyLocal() == true
initTAddress("[::1]:0").isAnyLocal() == false
initTAddress("[FE80::]:0").isLinkLocal() == true
initTAddress("[FEBF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF]:0").isLinkLocal() == true
initTAddress(
"[FEBF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF]:0"
).isLinkLocal() == true
initTAddress("[FE7F::]:0").isLinkLocal() == false
initTAddress("[FEC0::]:0").isLinkLocal() == false
initTAddress("[FEC0::]:0").isSiteLocal() == true
initTAddress("[FEFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF]:0").isSiteLocal() == true
initTAddress(
"[FEFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF]:0"
).isSiteLocal() == true
initTAddress("[FEBF::]:0").isSiteLocal() == false
initTAddress("[FF00::]:0").isSiteLocal() == false
initTAddress("[FF0E::]:0").isGlobalMulticast() == true
initTAddress("[FFFE:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF]:0").isGlobalMulticast() == true
initTAddress(
"[FFFE:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF]:0"
).isGlobalMulticast() == true
initTAddress("[FF0D::]:0").isGlobalMulticast() == false
initTAddress("[FFFF::]:0").isGlobalMulticast() == false
initTAddress(
"[FBFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF]:0"
).isUniqueLocal() == false
initTAddress("[FC00::]:0").isUniqueLocal() == true
initTAddress(
"[FDFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF]:0"
).isUniqueLocal() == true
initTAddress("[FE00::]:0").isUniqueLocal() == false
initTAddress(
"[FE7F:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF]:0"
).isUnicastLinkLocal() == false
initTAddress("[FE80::]:0").isUnicastLinkLocal() == true
initTAddress(
"[FEBF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF]:0"
).isUnicastLinkLocal() == true
initTAddress("[FEC0::]:0").isUnicastLinkLocal() == false
initTAddress(
"[2001:0001:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF]:0"
).isBenchmarking() == false
initTAddress("[2001:0002::]:0").isBenchmarking() == true
initTAddress(
"[2001:0002:0000:FFFF:FFFF:FFFF:FFFF:FFFF]:0"
).isBenchmarking() == true
initTAddress("[2001:0002:0001::]:0").isBenchmarking() == false
initTAddress(
"[2001:0DB7:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF]:0"
).isDocumentation() == false
initTAddress("[2001:0DB8::]:0").isDocumentation() == true
initTAddress(
"[2001:0DB8:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF]:0"
).isDocumentation() == true
initTAddress("[2001:0DB9::]:0").isDocumentation() == false
a.isGlobal() == false
initTAddress("[::]:0").isGlobal() == false
initTAddress("[::1]:0").isGlobal() == false
initTAddress("[::FFFF:0000:0000]:0").isGlobal() == false
initTAddress("[::FFFF:FFFF:FFFF]:0").isGlobal() == false
initTAddress("[0064:FF9B:0001::]:0").isGlobal() == false
initTAddress(
"[0064:FF9B:0001:FFFF:FFFF:FFFF:FFFF:FFFF]:0"
).isGlobal() == false
initTAddress("[0100::]:0").isGlobal() == false
initTAddress(
"[0100:0000:0000:0000:FFFF:FFFF:FFFF:FFFF]:0"
).isGlobal() == false
initTAddress("[2001::]:0").isGlobal() == false
initTAddress(
"[2001:01FF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF]:0"
).isGlobal() == false
initTAddress("[2001:1::1]:0").isGlobal() == true
initTAddress("[2001:1::2]:0").isGlobal() == true
initTAddress("[2001:3::]:0").isGlobal() == true
initTAddress("[2001:4:112::]:0").isGlobal() == true
initTAddress("[2001:20::]:0").isGlobal() == true
initTAddress("[2001:0db8::]:0").isGlobal() == false
initTAddress(
"[2001:0DB8:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF]:0"
).isGlobal() == false
initTAddress("[FC00::]:0").isGlobal() == false
initTAddress(
"[FDFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF]:0"
).isGlobal() == false
initTAddress("[FE80::]:0").isGlobal() == false
initTAddress(
"[FEBF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF]:0"
).isGlobal() == false
initTAddress("[2606:4700:4700::1111]:0").isGlobal() == true
initTAddress("[2606:4700:4700::1001]:0").isGlobal() == true
test "IP masks test":
check:
$IpMask.init(AddressFamily.IPv4, -1) == "00000000"
@ -481,7 +641,8 @@ suite "Network utilities test suite":
route.ifIndex != 0
echo route
test "TransportAddress arithmetic operations test":
test "TransportAddress arithmetic operations test (add)":
block:
var ip4 = initTAddress("192.168.1.0:1024")
var ip6 = initTAddress("[::1]:1024")
when sizeof(int) == 8:
@ -497,3 +658,33 @@ suite "Network utilities test suite":
check:
ip4 == initTAddress("192.168.1.0:1024")
ip6 == ip6e
ip4 = initTAddress("255.255.255.255:0")
ip6 = initTAddress("[FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF]:0")
inc(ip4)
inc(ip6)
check:
ip4 == initTAddress("0.0.0.0:0")
ip6 == initTAddress("[::]:0")
test "TransportAddress arithmetic operations test (sub)":
var ip4 = initTAddress("192.168.1.0:1024")
when sizeof(int) == 8:
var ip6 = initTAddress("[::1:0000:0000:0000:0000]:1024")
ip4 = ip4 - uint(0xFFFF_FFFF_FFFF_FFFF'u64)
ip6 = ip6 - uint(0xFFFF_FFFF_FFFF_FFFF'u64)
else:
var ip6 = initTAddress("[::1:0000:0000]:1024")
ip4 = ip4 - uint(0xFFFF_FFFF'u32)
ip6 = ip6 - uint(0xFFFF_FFFF'u32)
dec(ip4)
dec(ip6)
check:
ip4 == initTAddress("192.168.1.0:1024")
ip6 == initTAddress("[::]:1024")
ip4 = initTAddress("0.0.0.0:0")
ip6 = initTAddress("[::]:0")
dec(ip4)
dec(ip6)
check:
ip4 == initTAddress("255.255.255.255:0")
ip6 == initTAddress("[FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF]:0")