parent
a892d519a6
commit
a27429304e
14 changed files with 61 additions and 71 deletions
|
|
@ -38,11 +38,11 @@ when defined(windows) or defined(nimdoc):
|
|||
|
||||
if domain == Domain.AF_INET6:
|
||||
var saddr: Sockaddr_in6
|
||||
saddr.sin6_family = int16(toInt(domain))
|
||||
saddr.sin6_family = uint16(toInt(domain))
|
||||
doBind(saddr)
|
||||
else:
|
||||
var saddr: Sockaddr_in
|
||||
saddr.sin_family = int16(toInt(domain))
|
||||
saddr.sin_family = uint16(toInt(domain))
|
||||
doBind(saddr)
|
||||
|
||||
proc doConnect(socket: AsyncFD, addrInfo: ptr AddrInfo): Future[void] =
|
||||
|
|
|
|||
|
|
@ -112,7 +112,7 @@ proc `==`*(a, b: Port): bool {.borrow.}
|
|||
proc `$`*(p: Port): string {.borrow.}
|
||||
## returns the port number as a string
|
||||
|
||||
proc toInt*(domain: Domain): cshort
|
||||
proc toInt*(domain: Domain): cint
|
||||
## Converts the Domain enum to a platform-dependent ``cint``.
|
||||
|
||||
proc toInt*(typ: SockType): cint
|
||||
|
|
@ -122,12 +122,12 @@ proc toInt*(p: Protocol): cint
|
|||
## Converts the Protocol enum to a platform-dependent ``cint``.
|
||||
|
||||
when not useWinVersion:
|
||||
proc toInt(domain: Domain): cshort =
|
||||
proc toInt(domain: Domain): cint =
|
||||
case domain
|
||||
of AF_UNSPEC: result = posix.AF_UNSPEC.cshort
|
||||
of AF_UNIX: result = posix.AF_UNIX.cshort
|
||||
of AF_INET: result = posix.AF_INET.cshort
|
||||
of AF_INET6: result = posix.AF_INET6.cshort
|
||||
of AF_UNSPEC: result = posix.AF_UNSPEC.cint
|
||||
of AF_UNIX: result = posix.AF_UNIX.cint
|
||||
of AF_INET: result = posix.AF_INET.cint
|
||||
of AF_INET6: result = posix.AF_INET6.cint
|
||||
|
||||
proc toKnownDomain*(family: cint): Option[Domain] =
|
||||
## Converts the platform-dependent ``cint`` to the Domain or none(),
|
||||
|
|
@ -155,8 +155,8 @@ when not useWinVersion:
|
|||
of IPPROTO_ICMP: result = posix.IPPROTO_ICMP
|
||||
|
||||
else:
|
||||
proc toInt(domain: Domain): cshort =
|
||||
result = toU16(ord(domain))
|
||||
proc toInt(domain: Domain): cint =
|
||||
result = toU32(ord(domain)).cint
|
||||
|
||||
proc toKnownDomain*(family: cint): Option[Domain] =
|
||||
## Converts the platform-dependent ``cint`` to the Domain or none(),
|
||||
|
|
@ -456,9 +456,9 @@ proc getSockDomain*(socket: SocketHandle): Domain =
|
|||
if getsockname(socket, cast[ptr SockAddr](addr(name)),
|
||||
addr(namelen)) == -1'i32:
|
||||
raiseOSError(osLastError())
|
||||
if name.sin6_family == nativeAfInet:
|
||||
if name.sin6_family.cint == nativeAfInet:
|
||||
result = AF_INET
|
||||
elif name.sin6_family == nativeAfInet6:
|
||||
elif name.sin6_family.cint == nativeAfInet6:
|
||||
result = AF_INET6
|
||||
else:
|
||||
raiseOSError(osLastError(), "unknown socket family in getSockFamily")
|
||||
|
|
@ -466,9 +466,9 @@ proc getSockDomain*(socket: SocketHandle): Domain =
|
|||
|
||||
proc getAddrString*(sockAddr: ptr SockAddr): string =
|
||||
## return the string representation of address within sockAddr
|
||||
if sockAddr.sa_family == nativeAfInet:
|
||||
if sockAddr.sa_family.cint == nativeAfInet:
|
||||
result = $inet_ntoa(cast[ptr Sockaddr_in](sockAddr).sin_addr)
|
||||
elif sockAddr.sa_family == nativeAfInet6:
|
||||
elif sockAddr.sa_family.cint == nativeAfInet6:
|
||||
let addrLen = when not useWinVersion: posix.INET6_ADDRSTRLEN
|
||||
else: 46 # it's actually 46 in both cases
|
||||
result = newString(addrLen)
|
||||
|
|
@ -491,9 +491,9 @@ proc getSockName*(socket: SocketHandle): Port =
|
|||
## returns the socket's associated port number.
|
||||
var name: Sockaddr_in
|
||||
when useWinVersion:
|
||||
name.sin_family = int16(ord(AF_INET))
|
||||
name.sin_family = uint16(ord(AF_INET))
|
||||
else:
|
||||
name.sin_family = posix.AF_INET
|
||||
name.sin_family = uint16(posix.AF_INET)
|
||||
#name.sin_port = htons(cint16(port))
|
||||
#name.sin_addr.s_addr = htonl(INADDR_ANY)
|
||||
var namelen = sizeof(name).SockLen
|
||||
|
|
@ -510,9 +510,9 @@ proc getLocalAddr*(socket: SocketHandle, domain: Domain): (string, Port) =
|
|||
of AF_INET:
|
||||
var name: Sockaddr_in
|
||||
when useWinVersion:
|
||||
name.sin_family = int16(ord(AF_INET))
|
||||
name.sin_family = uint16(ord(AF_INET))
|
||||
else:
|
||||
name.sin_family = posix.AF_INET
|
||||
name.sin_family = uint16(posix.AF_INET)
|
||||
var namelen = sizeof(name).SockLen
|
||||
if getsockname(socket, cast[ptr SockAddr](addr(name)),
|
||||
addr(namelen)) == -1'i32:
|
||||
|
|
@ -522,9 +522,9 @@ proc getLocalAddr*(socket: SocketHandle, domain: Domain): (string, Port) =
|
|||
of AF_INET6:
|
||||
var name: Sockaddr_in6
|
||||
when useWinVersion:
|
||||
name.sin6_family = int16(ord(AF_INET6))
|
||||
name.sin6_family = uint16(ord(AF_INET6))
|
||||
else:
|
||||
name.sin6_family = posix.AF_INET6
|
||||
name.sin6_family = uint16(posix.AF_INET6)
|
||||
var namelen = sizeof(name).SockLen
|
||||
if getsockname(socket, cast[ptr SockAddr](addr(name)),
|
||||
addr(namelen)) == -1'i32:
|
||||
|
|
@ -547,9 +547,9 @@ proc getPeerAddr*(socket: SocketHandle, domain: Domain): (string, Port) =
|
|||
of AF_INET:
|
||||
var name: Sockaddr_in
|
||||
when useWinVersion:
|
||||
name.sin_family = int16(ord(AF_INET))
|
||||
name.sin_family = uint16(ord(AF_INET))
|
||||
else:
|
||||
name.sin_family = posix.AF_INET
|
||||
name.sin_family = uint16(posix.AF_INET)
|
||||
var namelen = sizeof(name).SockLen
|
||||
if getpeername(socket, cast[ptr SockAddr](addr(name)),
|
||||
addr(namelen)) == -1'i32:
|
||||
|
|
@ -559,9 +559,9 @@ proc getPeerAddr*(socket: SocketHandle, domain: Domain): (string, Port) =
|
|||
of AF_INET6:
|
||||
var name: Sockaddr_in6
|
||||
when useWinVersion:
|
||||
name.sin6_family = int16(ord(AF_INET6))
|
||||
name.sin6_family = uint16(ord(AF_INET6))
|
||||
else:
|
||||
name.sin6_family = posix.AF_INET6
|
||||
name.sin6_family = uint16(posix.AF_INET6)
|
||||
var namelen = sizeof(name).SockLen
|
||||
if getpeername(socket, cast[ptr SockAddr](addr(name)),
|
||||
addr(namelen)) == -1'i32:
|
||||
|
|
|
|||
|
|
@ -423,13 +423,13 @@ proc toSockAddr*(address: IpAddress, port: Port, sa: var Sockaddr_storage,
|
|||
|
||||
proc fromSockAddrAux(sa: ptr Sockaddr_storage, sl: Socklen,
|
||||
address: var IpAddress, port: var Port) =
|
||||
if sa.ss_family.int == toInt(AF_INET) and sl == sizeof(Sockaddr_in).Socklen:
|
||||
if sa.ss_family.cint == toInt(AF_INET) and sl == sizeof(Sockaddr_in).Socklen:
|
||||
address = IpAddress(family: IpAddressFamily.IPv4)
|
||||
let s = cast[ptr Sockaddr_in](sa)
|
||||
copyMem(addr address.address_v4[0], addr s.sin_addr,
|
||||
sizeof(address.address_v4))
|
||||
port = ntohs(s.sin_port).Port
|
||||
elif sa.ss_family.int == toInt(AF_INET6) and
|
||||
elif sa.ss_family.cint == toInt(AF_INET6) and
|
||||
sl == sizeof(Sockaddr_in6).Socklen:
|
||||
address = IpAddress(family: IpAddressFamily.IPv6)
|
||||
let s = cast[ptr Sockaddr_in6](sa)
|
||||
|
|
@ -758,10 +758,7 @@ proc bindAddr*(socket: Socket, port = Port(0), address = "") {.
|
|||
|
||||
if address == "":
|
||||
var name: Sockaddr_in
|
||||
when useWinVersion:
|
||||
name.sin_family = toInt(AF_INET).int16
|
||||
else:
|
||||
name.sin_family = toInt(AF_INET)
|
||||
name.sin_family = toInt(AF_INET).uint16
|
||||
name.sin_port = htons(port.uint16)
|
||||
name.sin_addr.s_addr = htonl(INADDR_ANY)
|
||||
if bindAddr(socket.fd, cast[ptr SockAddr](addr(name)),
|
||||
|
|
@ -952,7 +949,7 @@ proc setSockOpt*(socket: Socket, opt: SOBool, value: bool, level = SOL_SOCKET) {
|
|||
|
||||
when defined(posix) and not defined(nimdoc):
|
||||
proc makeUnixAddr(path: string): Sockaddr_un =
|
||||
result.sun_family = AF_UNIX.toInt
|
||||
result.sun_family = AF_UNIX.uint16
|
||||
if path.len >= Sockaddr_un_path_length:
|
||||
raise newException(ValueError, "socket path too long")
|
||||
copyMem(addr result.sun_path, path.cstring, path.len + 1)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue