From 5e3aad2b31a11856f8923910a506860c9ce8a0ed Mon Sep 17 00:00:00 2001 From: iacore Date: Tue, 2 Apr 2024 13:08:52 +0000 Subject: [PATCH] std/socket: Use 0 as default protocol number --- lib/posix/posix_freertos_consts.nim | 1 + lib/posix/posix_linux_amd64_consts.nim | 1 + lib/posix/posix_nintendoswitch_consts.nim | 1 + lib/posix/posix_other_consts.nim | 1 + lib/pure/nativesockets.nim | 8 +++++--- lib/pure/net.nim | 9 +++------ sol-nimskull.nim | 0 7 files changed, 12 insertions(+), 9 deletions(-) create mode 100644 sol-nimskull.nim diff --git a/lib/posix/posix_freertos_consts.nim b/lib/posix/posix_freertos_consts.nim index efe87aacfce..f9f0fc4ea50 100644 --- a/lib/posix/posix_freertos_consts.nim +++ b/lib/posix/posix_freertos_consts.nim @@ -199,6 +199,7 @@ var LWIP_NSC_IPV6_ADDR_STATE_CHANGED* {.importc: "LWIP_NSC_IPV6_ADDR_STATE_CHANG var IF_NAMESIZE* {.importc: "IF_NAMESIZE", header: "".}: cint # +const IPPROTO_DEFAULT* = cint(0) var IPPROTO_IP* {.importc: "IPPROTO_IP", header: "".}: cint var IPPROTO_IPV6* {.importc: "IPPROTO_IPV6", header: "".}: cint var IPPROTO_ICMP* {.importc: "IPPROTO_ICMP", header: "".}: cint diff --git a/lib/posix/posix_linux_amd64_consts.nim b/lib/posix/posix_linux_amd64_consts.nim index f3230f71d6a..f6c32b47770 100644 --- a/lib/posix/posix_linux_amd64_consts.nim +++ b/lib/posix/posix_linux_amd64_consts.nim @@ -305,6 +305,7 @@ const EAI_OVERFLOW* = cint(-12) const IF_NAMESIZE* = cint(16) # +const IPPROTO_DEFAULT* = cint(0) const IPPROTO_IP* = cint(0) const IPPROTO_IPV6* = cint(41) const IPPROTO_ICMP* = cint(1) diff --git a/lib/posix/posix_nintendoswitch_consts.nim b/lib/posix/posix_nintendoswitch_consts.nim index 8cd8824b535..ef5a0a35d15 100644 --- a/lib/posix/posix_nintendoswitch_consts.nim +++ b/lib/posix/posix_nintendoswitch_consts.nim @@ -234,6 +234,7 @@ const EAI_OVERFLOW* = cint(14) const IF_NAMESIZE* = cint(16) # +const IPPROTO_DEFAULT* = cint(0) const IPPROTO_IP* = cint(0) const IPPROTO_IPV6* = cint(41) const IPPROTO_ICMP* = cint(1) diff --git a/lib/posix/posix_other_consts.nim b/lib/posix/posix_other_consts.nim index f43407b4064..de4b66792b1 100644 --- a/lib/posix/posix_other_consts.nim +++ b/lib/posix/posix_other_consts.nim @@ -301,6 +301,7 @@ var EAI_OVERFLOW* {.importc: "EAI_OVERFLOW", header: "".}: cint var IF_NAMESIZE* {.importc: "IF_NAMESIZE", header: "".}: cint # +const IPPROTO_DEFAULT* = cint(0) var IPPROTO_IP* {.importc: "IPPROTO_IP", header: "".}: cint var IPPROTO_IPV6* {.importc: "IPPROTO_IPV6", header: "".}: cint var IPPROTO_ICMP* {.importc: "IPPROTO_ICMP", header: "".}: cint diff --git a/lib/pure/nativesockets.nim b/lib/pure/nativesockets.nim index 7be720cd034..b009375214c 100644 --- a/lib/pure/nativesockets.nim +++ b/lib/pure/nativesockets.nim @@ -69,6 +69,7 @@ type SOCK_SEQPACKET = 5 ## reliable sequenced packet service Protocol* = enum ## third argument to `socket` proc + IPPROTO_DEFAULT = 0, IPPROTO_TCP = 6, ## Transmission control protocol. IPPROTO_UDP = 17, ## User datagram protocol. IPPROTO_IP, ## Internet protocol. @@ -153,6 +154,7 @@ when not useWinVersion: proc toInt(p: Protocol): cint = case p + of IPPROTO_DEFAULT: result = posix.IPPROTO_DEFAULT of IPPROTO_TCP: result = posix.IPPROTO_TCP of IPPROTO_UDP: result = posix.IPPROTO_UDP of IPPROTO_IP: result = posix.IPPROTO_IP @@ -178,7 +180,7 @@ else: proc toInt(p: Protocol): cint = case p - of IPPROTO_IP: + of IPPROTO_IP, IPPROTO_DEFAULT: result = 0.cint of IPPROTO_ICMP: result = 1.cint @@ -254,7 +256,7 @@ proc createNativeSocket*(domain: cint, sockType: cint, protocol: cint, proc createNativeSocket*(domain: Domain = AF_INET, sockType: SockType = SOCK_STREAM, - protocol: Protocol = IPPROTO_TCP, + protocol: Protocol = IPPROTO_DEFAULT, inheritable: bool = defined(nimInheritHandles)): SocketHandle = ## Creates a new socket; returns `osInvalidSocket` if an error occurs. ## @@ -278,7 +280,7 @@ proc listen*(socket: SocketHandle, backlog = SOMAXCONN): cint {.tags: [ proc getAddrInfo*(address: string, port: Port, domain: Domain = AF_INET, sockType: SockType = SOCK_STREAM, - protocol: Protocol = IPPROTO_TCP): ptr AddrInfo = + protocol: Protocol = IPPROTO_DEFAULT): ptr AddrInfo = ## ## ## .. warning:: The resulting `ptr AddrInfo` must be freed using `freeAddrInfo`! diff --git a/lib/pure/net.nim b/lib/pure/net.nim index da22913e8d1..fef2818ccb7 100644 --- a/lib/pure/net.nim +++ b/lib/pure/net.nim @@ -237,7 +237,7 @@ proc toOSFlags*(socketFlags: set[SocketFlag]): cint = proc newSocket*(fd: SocketHandle, domain: Domain = AF_INET, sockType: SockType = SOCK_STREAM, - protocol: Protocol = IPPROTO_TCP, buffered = true): owned(Socket) = + protocol: Protocol = IPPROTO_DEFAULT, buffered = true): owned(Socket) = ## Creates a new socket as specified by the params. assert fd != osInvalidSocket result = Socket( @@ -269,7 +269,7 @@ proc newSocket*(domain, sockType, protocol: cint, buffered = true, buffered) proc newSocket*(domain: Domain = AF_INET, sockType: SockType = SOCK_STREAM, - protocol: Protocol = IPPROTO_TCP, buffered = true, + protocol: Protocol = IPPROTO_DEFAULT, buffered = true, inheritable = defined(nimInheritHandles)): owned(Socket) = ## Creates a new socket. ## @@ -1650,7 +1650,6 @@ proc recvFrom*[T: string | IpAddress](socket: Socket, data: var string, length: else: raiseOSError(osLastError()) - assert(socket.protocol != IPPROTO_TCP, "Cannot `recvFrom` on a TCP socket") # TODO: Buffered sockets data.setLen(length) @@ -1732,7 +1731,6 @@ proc sendTo*(socket: Socket, address: string, port: Port, data: pointer, ## which is defined below. ## ## **Note:** This proc is not available for SSL sockets. - assert(socket.protocol != IPPROTO_TCP, "Cannot `sendTo` on a TCP socket") assert(not socket.isClosed, "Cannot `sendTo` on a closed socket") var aiList = getAddrInfo(address, port, af, socket.sockType, socket.protocol) # try all possibilities: @@ -1777,7 +1775,6 @@ proc sendTo*(socket: Socket, address: IpAddress, port: Port, ## If an error occurs an OSError exception will be raised. ## ## This is the high-level version of the above `sendTo` function. - assert(socket.protocol != IPPROTO_TCP, "Cannot `sendTo` on a TCP socket") assert(not socket.isClosed, "Cannot `sendTo` on a closed socket") var sa: Sockaddr_storage @@ -1915,7 +1912,7 @@ proc `$`*(address: IpAddress): string = printedLastGroup = true proc dial*(address: string, port: Port, - protocol = IPPROTO_TCP, buffered = true): owned(Socket) + protocol = IPPROTO_DEFAULT, buffered = true): owned(Socket) {.tags: [ReadIOEffect, WriteIOEffect].} = ## Establishes connection to the specified `address`:`port` pair via the ## specified protocol. The procedure iterates through possible diff --git a/sol-nimskull.nim b/sol-nimskull.nim new file mode 100644 index 00000000000..e69de29bb2d