summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorLemonBoy <thatlemon@gmail.com>2018-09-19 09:24:30 +0200
committerLemonBoy <thatlemon@gmail.com>2018-09-19 12:33:11 +0200
commita27429304e77ced5b9ba5729e47d6d6c7b7dae5c (patch)
treeeff2d0b0f26c407d291a62b5ea5381c4ec5344e3 /lib
parenta892d519a60e08212ea05e99bb9f858e6673ee6b (diff)
downloadNim-a27429304e77ced5b9ba5729e47d6d6c7b7dae5c.tar.gz
Convert *_family fields to cushort
Fixes #9008
Diffstat (limited to 'lib')
-rw-r--r--lib/deprecated/pure/sockets.nim18
-rw-r--r--lib/posix/posix_linux_amd64.nim3
-rw-r--r--lib/posix/posix_linux_amd64_consts.nim8
-rw-r--r--lib/posix/posix_nintendoswitch_consts.nim8
-rw-r--r--lib/posix/posix_other.nim2
-rw-r--r--lib/posix/posix_other_consts.nim8
-rw-r--r--lib/pure/includes/asynccommon.nim4
-rw-r--r--lib/pure/nativesockets.nim44
-rw-r--r--lib/pure/net.nim11
-rw-r--r--lib/windows/winlean.nim10
10 files changed, 56 insertions, 60 deletions
diff --git a/lib/deprecated/pure/sockets.nim b/lib/deprecated/pure/sockets.nim
index 76a9044d8..ed15f67e2 100644
--- a/lib/deprecated/pure/sockets.nim
+++ b/lib/deprecated/pure/sockets.nim
@@ -223,7 +223,7 @@ template htons(x: uint16): uint16 =
   sockets.ntohs(x)
 
 when defined(Posix):
-  proc toInt(domain: Domain): TSa_Family =
+  proc toInt(domain: Domain): cint =
     case domain
     of AF_UNIX:        result = posix.AF_UNIX
     of AF_INET:        result = posix.AF_INET
@@ -463,9 +463,9 @@ proc bindAddr*(socket: Socket, port = Port(0), address = "") {.
   if address == "":
     var name: Sockaddr_in
     when defined(Windows):
-      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 = sockets.htons(uint16(port))
     name.sin_addr.s_addr = sockets.htonl(INADDR_ANY)
     if bindSocket(socket.fd, cast[ptr SockAddr](addr(name)),
@@ -485,9 +485,9 @@ proc getSockName*(socket: Socket): Port =
   ## returns the socket's associated port number.
   var name: Sockaddr_in
   when defined(Windows):
-    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
@@ -729,9 +729,9 @@ proc getHostByAddr*(ip: string): Hostent {.tags: [ReadIOEffect].} =
   when defined(windows):
     result.addrtype = Domain(s.h_addrtype)
   else:
-    if s.h_addrtype == posix.AF_INET:
+    if s.h_addrtype.cint == posix.AF_INET:
       result.addrtype = AF_INET
-    elif s.h_addrtype == posix.AF_INET6:
+    elif s.h_addrtype.cint == posix.AF_INET6:
       result.addrtype = AF_INET6
     else:
       raiseOSError(osLastError(), "unknown h_addrtype")
@@ -750,9 +750,9 @@ proc getHostByName*(name: string): Hostent {.tags: [ReadIOEffect].} =
   when defined(windows):
     result.addrtype = Domain(s.h_addrtype)
   else:
-    if s.h_addrtype == posix.AF_INET:
+    if s.h_addrtype.cint == posix.AF_INET:
       result.addrtype = AF_INET
-    elif s.h_addrtype == posix.AF_INET6:
+    elif s.h_addrtype.cint == posix.AF_INET6:
       result.addrtype = AF_INET6
     else:
       raiseOSError(osLastError(), "unknown h_addrtype")
diff --git a/lib/posix/posix_linux_amd64.nim b/lib/posix/posix_linux_amd64.nim
index 4f114d394..6e69409ea 100644
--- a/lib/posix/posix_linux_amd64.nim
+++ b/lib/posix/posix_linux_amd64.nim
@@ -440,8 +440,7 @@ const Sockaddr_un_path_length* = 108
 
 type
   Socklen* {.importc: "socklen_t", header: "<sys/socket.h>".} = cuint
-  # cushort really
-  TSa_Family* {.importc: "sa_family_t", header: "<sys/socket.h>".} = cshort
+  TSa_Family* {.importc: "sa_family_t", header: "<sys/socket.h>".} = cushort
 
   SockAddr* {.importc: "struct sockaddr", header: "<sys/socket.h>",
               pure, final.} = object ## struct sockaddr
diff --git a/lib/posix/posix_linux_amd64_consts.nim b/lib/posix/posix_linux_amd64_consts.nim
index 50b227635..c23005b1e 100644
--- a/lib/posix/posix_linux_amd64_consts.nim
+++ b/lib/posix/posix_linux_amd64_consts.nim
@@ -472,10 +472,10 @@ const MSG_NOSIGNAL* = cint(16384)
 const MSG_PEEK* = cint(2)
 const MSG_TRUNC* = cint(32)
 const MSG_WAITALL* = cint(256)
-const AF_INET* = TSa_Family(2)
-const AF_INET6* = TSa_Family(10)
-const AF_UNIX* = TSa_Family(1)
-const AF_UNSPEC* = TSa_Family(0)
+const AF_INET* = cint(2)
+const AF_INET6* = cint(10)
+const AF_UNIX* = cint(1)
+const AF_UNSPEC* = cint(0)
 const SHUT_RD* = cint(0)
 const SHUT_RDWR* = cint(2)
 const SHUT_WR* = cint(1)
diff --git a/lib/posix/posix_nintendoswitch_consts.nim b/lib/posix/posix_nintendoswitch_consts.nim
index 33470d22b..f0c0dd717 100644
--- a/lib/posix/posix_nintendoswitch_consts.nim
+++ b/lib/posix/posix_nintendoswitch_consts.nim
@@ -371,10 +371,10 @@ const MSG_NOSIGNAL* = cint(131072)
 const MSG_PEEK* = cint(2)
 const MSG_TRUNC* = cint(16)
 const MSG_WAITALL* = cint(64)
-const AF_INET* = TSa_Family(2)
-const AF_INET6* = TSa_Family(28)
-const AF_UNIX* = TSa_Family(1)
-const AF_UNSPEC* = TSa_Family(0)
+const AF_INET* = cint(2)
+const AF_INET6* = cint(28)
+const AF_UNIX* = cint(1)
+const AF_UNSPEC* = cint(0)
 const SHUT_RD* = cint(0)
 const SHUT_RDWR* = cint(2)
 const SHUT_WR* = cint(1)
diff --git a/lib/posix/posix_other.nim b/lib/posix/posix_other.nim
index 99d67824e..ba1dd89ed 100644
--- a/lib/posix/posix_other.nim
+++ b/lib/posix/posix_other.nim
@@ -410,7 +410,7 @@ else:
 
 type
   Socklen* {.importc: "socklen_t", header: "<sys/socket.h>".} = cuint
-  TSa_Family* {.importc: "sa_family_t", header: "<sys/socket.h>".} = cint
+  TSa_Family* {.importc: "sa_family_t", header: "<sys/socket.h>".} = cushort
 
   SockAddr* {.importc: "struct sockaddr", header: "<sys/socket.h>",
               pure, final.} = object ## struct sockaddr
diff --git a/lib/posix/posix_other_consts.nim b/lib/posix/posix_other_consts.nim
index 1b27fc5f6..2b4b70941 100644
--- a/lib/posix/posix_other_consts.nim
+++ b/lib/posix/posix_other_consts.nim
@@ -488,10 +488,10 @@ var SOMAXCONN* {.importc: "SOMAXCONN", header: "<sys/socket.h>".}: cint
 var MSG_PEEK* {.importc: "MSG_PEEK", header: "<sys/socket.h>".}: cint
 var MSG_TRUNC* {.importc: "MSG_TRUNC", header: "<sys/socket.h>".}: cint
 var MSG_WAITALL* {.importc: "MSG_WAITALL", header: "<sys/socket.h>".}: cint
-var AF_INET* {.importc: "AF_INET", header: "<sys/socket.h>".}: TSa_Family
-var AF_INET6* {.importc: "AF_INET6", header: "<sys/socket.h>".}: TSa_Family
-var AF_UNIX* {.importc: "AF_UNIX", header: "<sys/socket.h>".}: TSa_Family
-var AF_UNSPEC* {.importc: "AF_UNSPEC", header: "<sys/socket.h>".}: TSa_Family
+var AF_INET* {.importc: "AF_INET", header: "<sys/socket.h>".}: cint
+var AF_INET6* {.importc: "AF_INET6", header: "<sys/socket.h>".}: cint
+var AF_UNIX* {.importc: "AF_UNIX", header: "<sys/socket.h>".}: cint
+var AF_UNSPEC* {.importc: "AF_UNSPEC", header: "<sys/socket.h>".}: cint
 var SHUT_RD* {.importc: "SHUT_RD", header: "<sys/socket.h>".}: cint
 var SHUT_RDWR* {.importc: "SHUT_RDWR", header: "<sys/socket.h>".}: cint
 var SHUT_WR* {.importc: "SHUT_WR", header: "<sys/socket.h>".}: cint
diff --git a/lib/pure/includes/asynccommon.nim b/lib/pure/includes/asynccommon.nim
index 06f4958c6..13887acc9 100644
--- a/lib/pure/includes/asynccommon.nim
+++ b/lib/pure/includes/asynccommon.nim
@@ -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] =
diff --git a/lib/pure/nativesockets.nim b/lib/pure/nativesockets.nim
index d5fb0f89b..a1049d599 100644
--- a/lib/pure/nativesockets.nim
+++ b/lib/pure/nativesockets.nim
@@ -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:
diff --git a/lib/pure/net.nim b/lib/pure/net.nim
index 67cb95e2f..179fccaa3 100644
--- a/lib/pure/net.nim
+++ b/lib/pure/net.nim
@@ -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)
diff --git a/lib/windows/winlean.nim b/lib/windows/winlean.nim
index 60a6e5d9b..beb60332b 100644
--- a/lib/windows/winlean.nim
+++ b/lib/windows/winlean.nim
@@ -464,7 +464,7 @@ type
     lpVendorInfo: cstring
 
   SockAddr* {.importc: "SOCKADDR", header: "winsock2.h".} = object
-    sa_family*: int16 # unsigned
+    sa_family*: uint16
     sa_data*: array[0..13, char]
 
   PSockAddr = ptr SockAddr
@@ -474,7 +474,7 @@ type
 
   Sockaddr_in* {.importc: "SOCKADDR_IN",
                   header: "winsock2.h".} = object
-    sin_family*: int16
+    sin_family*: uint16
     sin_port*: uint16
     sin_addr*: InAddr
     sin_zero*: array[0..7, char]
@@ -484,21 +484,21 @@ type
 
   Sockaddr_in6* {.importc: "SOCKADDR_IN6",
                    header: "ws2tcpip.h".} = object
-    sin6_family*: int16
+    sin6_family*: uint16
     sin6_port*: uint16
     sin6_flowinfo*: int32 # unsigned
     sin6_addr*: In6_addr
     sin6_scope_id*: int32 # unsigned
 
   Sockaddr_in6_old* = object
-    sin6_family*: int16
+    sin6_family*: uint16
     sin6_port*: int16 # unsigned
     sin6_flowinfo*: int32 # unsigned
     sin6_addr*: In6_addr
 
   Sockaddr_storage* {.importc: "SOCKADDR_STORAGE",
                       header: "winsock2.h".} = object
-    ss_family*: int16
+    ss_family*: uint16
     ss_pad1: array[6, byte]
     ss_align: int64
     ss_pad2: array[112, byte]