Skip to content

Commit

Permalink
std/socket: Use 0 as default protocol number
Browse files Browse the repository at this point in the history
  • Loading branch information
iacore committed Apr 2, 2024
1 parent 7fd704b commit 65b485e
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 9 deletions.
1 change: 1 addition & 0 deletions lib/posix/posix_freertos_consts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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: "<net/if.h>".}: cint

# <sys/socket.h>
const IPPROTO_DEFAULT* = cint(0)
var IPPROTO_IP* {.importc: "IPPROTO_IP", header: "<sys/socket.h>".}: cint
var IPPROTO_IPV6* {.importc: "IPPROTO_IPV6", header: "<sys/socket.h>".}: cint
var IPPROTO_ICMP* {.importc: "IPPROTO_ICMP", header: "<sys/socket.h>".}: cint
Expand Down
1 change: 1 addition & 0 deletions lib/posix/posix_linux_amd64_consts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ const EAI_OVERFLOW* = cint(-12)
const IF_NAMESIZE* = cint(16)

# <netinet/in.h>
const IPPROTO_DEFAULT* = cint(0)
const IPPROTO_IP* = cint(0)
const IPPROTO_IPV6* = cint(41)
const IPPROTO_ICMP* = cint(1)
Expand Down
1 change: 1 addition & 0 deletions lib/posix/posix_nintendoswitch_consts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ const EAI_OVERFLOW* = cint(14)
const IF_NAMESIZE* = cint(16)

# <netinet/in.h>
const IPPROTO_DEFAULT* = cint(0)
const IPPROTO_IP* = cint(0)
const IPPROTO_IPV6* = cint(41)
const IPPROTO_ICMP* = cint(1)
Expand Down
1 change: 1 addition & 0 deletions lib/posix/posix_other_consts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ var EAI_OVERFLOW* {.importc: "EAI_OVERFLOW", header: "<netdb.h>".}: cint
var IF_NAMESIZE* {.importc: "IF_NAMESIZE", header: "<net/if.h>".}: cint

# <netinet/in.h>
const IPPROTO_DEFAULT* = cint(0)
var IPPROTO_IP* {.importc: "IPPROTO_IP", header: "<netinet/in.h>".}: cint
var IPPROTO_IPV6* {.importc: "IPPROTO_IPV6", header: "<netinet/in.h>".}: cint
var IPPROTO_ICMP* {.importc: "IPPROTO_ICMP", header: "<netinet/in.h>".}: cint
Expand Down
8 changes: 5 additions & 3 deletions lib/pure/nativesockets.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
##
Expand All @@ -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`!
Expand Down
9 changes: 3 additions & 6 deletions lib/pure/net.nim
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,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(
Expand Down Expand Up @@ -265,7 +265,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.
##
Expand Down Expand Up @@ -1644,7 +1644,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)

Expand Down Expand Up @@ -1726,7 +1725,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:
Expand Down Expand Up @@ -1771,7 +1769,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
Expand Down Expand Up @@ -1909,7 +1906,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
Expand Down
Empty file added sol-nimskull.nim
Empty file.

0 comments on commit 65b485e

Please sign in to comment.