mirror of
https://github.com/nim-lang/Nim.git
synced 2026-02-12 22:33:49 +00:00
Convertion procs for (IpAddress, Port) <-> (SockAddr, Socklen)
This commit is contained in:
@@ -414,6 +414,39 @@ proc isIpAddress*(address_str: string): bool {.tags: [].} =
|
||||
return false
|
||||
return true
|
||||
|
||||
proc toSockAddr*(address: IpAddress, port: Port, sa: var SockAddr, sl: var Socklen) =
|
||||
## Converts `IpAddress` and `Port` to `SockAddr` and `Socklen`
|
||||
let port = htons(uint16(port))
|
||||
case address.family
|
||||
of IpAddressFamily.IPv4:
|
||||
sl = sizeof(Sockaddr_in).Socklen
|
||||
let s = cast[ptr Sockaddr_in](addr sa)
|
||||
s.sin_family = type(s.sin_family)(AF_INET)
|
||||
s.sin_port = port
|
||||
copyMem(addr s.sin_addr, unsafeAddr address.address_v4[0], sizeof(s.sin_addr))
|
||||
of IpAddressFamily.IPv6:
|
||||
sl = sizeof(Sockaddr_in6).Socklen
|
||||
let s = cast[ptr Sockaddr_in6](addr sa)
|
||||
s.sin6_family = type(s.sin6_family)(AF_INET6)
|
||||
s.sin6_port = port
|
||||
copyMem(addr s.sin6_addr, unsafeAddr address.address_v6[0], sizeof(s.sin6_addr))
|
||||
|
||||
proc fromSockAddr*(sa: SockAddr, sl: Socklen, address: var IpAddress, port: var Port) =
|
||||
## Converts `SockAddr` and `Socklen` to `IpAddress` and `Port`. Raises
|
||||
## `ObjectConversionError` in case of invalid `sa` and `sl` arguments.
|
||||
if sa.sa_family.int == AF_INET.int and sl == sizeof(Sockaddr_in).Socklen:
|
||||
address = IpAddress(family: IpAddressFamily.IPv4)
|
||||
let s = cast[ptr Sockaddr_in](unsafeAddr sa)
|
||||
copyMem(addr address.address_v4, addr s.sin_addr, sizeof(address.address_v4))
|
||||
port = ntohs(s.sin_port).Port
|
||||
elif sa.sa_family.int == AF_INET6.int and sl == sizeof(Sockaddr_in6).Socklen:
|
||||
address = IpAddress(family: IpAddressFamily.IPv6)
|
||||
let s = cast[ptr Sockaddr_in6](unsafeAddr sa)
|
||||
copyMem(addr address.address_v6, addr s.sin6_addr, sizeof(address.address_v6))
|
||||
port = ntohs(s.sin6_port).Port
|
||||
else:
|
||||
raise newException(ObjectConversionError, "Unexpected SockAddr/Socklen")
|
||||
|
||||
when defineSsl:
|
||||
CRYPTO_malloc_init()
|
||||
doAssert SslLibraryInit() == 1
|
||||
|
||||
Reference in New Issue
Block a user