2024-01-20 20:57:08 +00:00
|
|
|
{.deprecated: "use std/net".}
|
|
|
|
|
2020-06-05 17:05:22 +03:00
|
|
|
import std/net as stdNet
|
|
|
|
export stdNet
|
|
|
|
|
|
|
|
type
|
2023-11-08 01:55:31 +00:00
|
|
|
ValidIpAddress* {.requiresInit, deprecated.} = object
|
2020-06-05 19:19:06 +03:00
|
|
|
value: IpAddress
|
2020-06-05 17:05:22 +03:00
|
|
|
|
2023-07-15 17:17:27 +00:00
|
|
|
{.push raises: [].}
|
2020-06-05 17:05:22 +03:00
|
|
|
|
2023-06-15 12:21:51 +00:00
|
|
|
func ipv4*(address: array[4, byte]): ValidIpAddress =
|
2020-06-05 19:19:06 +03:00
|
|
|
ValidIpAddress(value: IpAddress(family: IPv4, address_v4: address))
|
2020-06-05 17:05:22 +03:00
|
|
|
|
|
|
|
template ipv4*(a, b, c, d: byte): ValidIpAddress =
|
|
|
|
ipv4([a, b, c, d])
|
|
|
|
|
2023-06-15 12:21:51 +00:00
|
|
|
func ipv6*(address: array[16, byte]): ValidIpAddress =
|
2020-06-05 19:19:06 +03:00
|
|
|
ValidIpAddress(value: IpAddress(family: IPv6, address_v6: address))
|
2020-06-05 17:05:22 +03:00
|
|
|
|
|
|
|
template family*(a: ValidIpAddress): IpAddressFamily =
|
2020-06-05 19:19:06 +03:00
|
|
|
a.value.family
|
2020-06-05 17:05:22 +03:00
|
|
|
|
|
|
|
template address_v4*(a: ValidIpAddress): array[4, byte] =
|
2020-06-05 19:19:06 +03:00
|
|
|
a.value.address_v4
|
2020-06-05 17:05:22 +03:00
|
|
|
|
|
|
|
template address_v6*(a: ValidIpAddress): array[16, byte] =
|
2020-06-05 19:19:06 +03:00
|
|
|
a.value.address_v6
|
2020-06-05 17:05:22 +03:00
|
|
|
|
|
|
|
template `$`*(a: ValidIpAddress): string =
|
2020-06-05 19:19:06 +03:00
|
|
|
$a.value
|
2020-06-05 17:05:22 +03:00
|
|
|
|
|
|
|
func init*(T: type ValidIpAddress, str: string): T
|
|
|
|
{.raises: [ValueError].} =
|
2020-06-05 19:19:06 +03:00
|
|
|
ValidIpAddress(value: stdNet.parseIpAddress(str))
|
|
|
|
|
2020-06-09 10:21:28 +02:00
|
|
|
func init*(T: type ValidIpAddress, ip: IpAddress): T =
|
2020-06-05 19:19:06 +03:00
|
|
|
ValidIpAddress(value: ip)
|
|
|
|
|
|
|
|
converter toNormalIp*(ip: ValidIpAddress): IpAddress =
|
|
|
|
ip.value
|
|
|
|
|
|
|
|
func default*(T: type ValidIpAddress): T {.error.}
|