2020-06-05 14:05:22 +00:00
|
|
|
import std/net as stdNet
|
|
|
|
export stdNet
|
|
|
|
|
|
|
|
type
|
2020-06-05 16:19:06 +00:00
|
|
|
ValidIpAddress* {.requiresInit.} = object
|
|
|
|
value: IpAddress
|
2020-06-05 14:05:22 +00:00
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
|
|
|
proc ipv4*(address: array[4, byte]): ValidIpAddress =
|
2020-06-05 16:19:06 +00:00
|
|
|
ValidIpAddress(value: IpAddress(family: IPv4, address_v4: address))
|
2020-06-05 14:05:22 +00:00
|
|
|
|
|
|
|
template ipv4*(a, b, c, d: byte): ValidIpAddress =
|
|
|
|
ipv4([a, b, c, d])
|
|
|
|
|
|
|
|
proc ipv6*(address: array[16, byte]): ValidIpAddress =
|
2020-06-05 16:19:06 +00:00
|
|
|
ValidIpAddress(value: IpAddress(family: IPv6, address_v6: address))
|
2020-06-05 14:05:22 +00:00
|
|
|
|
|
|
|
template family*(a: ValidIpAddress): IpAddressFamily =
|
2020-06-05 16:19:06 +00:00
|
|
|
a.value.family
|
2020-06-05 14:05:22 +00:00
|
|
|
|
|
|
|
template address_v4*(a: ValidIpAddress): array[4, byte] =
|
2020-06-05 16:19:06 +00:00
|
|
|
a.value.address_v4
|
2020-06-05 14:05:22 +00:00
|
|
|
|
|
|
|
template address_v6*(a: ValidIpAddress): array[16, byte] =
|
2020-06-05 16:19:06 +00:00
|
|
|
a.value.address_v6
|
2020-06-05 14:05:22 +00:00
|
|
|
|
|
|
|
template `$`*(a: ValidIpAddress): string =
|
2020-06-05 16:19:06 +00:00
|
|
|
$a.value
|
2020-06-05 14:05:22 +00:00
|
|
|
|
|
|
|
func init*(T: type ValidIpAddress, str: string): T
|
|
|
|
{.raises: [ValueError].} =
|
2020-06-05 16:19:06 +00:00
|
|
|
ValidIpAddress(value: stdNet.parseIpAddress(str))
|
|
|
|
|
|
|
|
func init*(T: type ValidIpAddress, ip: IpAddress): T
|
|
|
|
{.raises: [ValueError].} =
|
|
|
|
ValidIpAddress(value: ip)
|
|
|
|
|
|
|
|
converter toNormalIp*(ip: ValidIpAddress): IpAddress =
|
|
|
|
ip.value
|
|
|
|
|
|
|
|
func default*(T: type ValidIpAddress): T {.error.}
|
2020-06-05 14:05:22 +00:00
|
|
|
|