Create IPPROTO_NONE alias & Add test for Unix socket (#24139)

closes #24116
This commit is contained in:
tocariimaa
2024-09-19 05:19:59 +00:00
committed by GitHub
parent ff005ad7dc
commit 84f5060e94
4 changed files with 64 additions and 1 deletions

View File

@@ -97,6 +97,8 @@ type
length*: int
addrList*: seq[string]
const IPPROTO_NONE* = IPPROTO_IP ## Use this if your socket type requires a protocol value of zero (e.g. Unix sockets).
when useWinVersion:
let
osInvalidSocket* = winlean.INVALID_SOCKET

View File

@@ -97,7 +97,7 @@ import std/nativesockets
import std/[os, strutils, times, sets, options, monotimes]
import std/ssl_config
export nativesockets.Port, nativesockets.`$`, nativesockets.`==`
export Domain, SockType, Protocol
export Domain, SockType, Protocol, IPPROTO_NONE
const useWinVersion = defined(windows) or defined(nimdoc)
const useNimNetLite = defined(nimNetLite) or defined(freertos) or defined(zephyr) or

View File

@@ -0,0 +1,35 @@
import std/[assertions, net, os, osproc]
# XXX: Make this test run on Windows too when we add support for Unix sockets on Windows
when defined(posix) and not defined(nimNetLite):
const nim = getCurrentCompilerExe()
let
dir = currentSourcePath().parentDir()
serverPath = dir / "unixsockettest"
let (_, err) = execCmdEx(nim & " c " & quoteShell(dir / "unixsockettest.nim"))
doAssert err == 0
let svproc = startProcess(serverPath, workingDir = dir)
doAssert svproc.running()
# Wait for the server to open the socket and listen from it
sleep(400)
block unixSocketSendRecv:
let
unixSocketPath = dir / "usox"
socket = newSocket(AF_UNIX, SOCK_STREAM, IPPROTO_NONE)
socket.connectUnix(unixSocketPath)
# for a blocking Unix socket this should never fail
socket.send("data sent through the socket\c\l", maxRetries = 0)
var resp: string
socket.readLine(resp)
doAssert resp == "Hello from server"
socket.send("bye\c\l")
socket.readLine(resp)
doAssert resp == "bye"
socket.close()
svproc.close()

View File

@@ -0,0 +1,26 @@
import std/[assertions, net, os]
let unixSocketPath = getCurrentDir() / "usox"
removeFile(unixSocketPath)
let socket = newSocket(AF_UNIX, SOCK_STREAM, IPPROTO_NONE)
socket.bindUnix(unixSocketPath)
socket.listen()
var
clientSocket: Socket
data: string
socket.accept(clientSocket)
clientSocket.readLine(data)
doAssert data == "data sent through the socket"
clientSocket.send("Hello from server\c\l")
clientSocket.readLine(data)
doAssert data == "bye"
clientSocket.send("bye\c\l")
clientSocket.close()
socket.close()
removeFile(unixSocketPath)