diff options
author | tocariimaa <87833010+tocariimaa@users.noreply.github.com> | 2024-09-19 05:19:59 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-19 07:19:59 +0200 |
commit | 84f5060e9478d1f627aadc1ff90228dc2f297028 (patch) | |
tree | 0623b69202bfafc0ebe588d9265c4a9087f8d3d8 | |
parent | ff005ad7dc8b4f3a36a1eb0e18ebd4cc5e062883 (diff) | |
download | Nim-84f5060e9478d1f627aadc1ff90228dc2f297028.tar.gz |
Create `IPPROTO_NONE` alias & Add test for Unix socket (#24139)
closes #24116
-rw-r--r-- | lib/pure/nativesockets.nim | 2 | ||||
-rw-r--r-- | lib/pure/net.nim | 2 | ||||
-rw-r--r-- | tests/stdlib/tunixsocket.nim | 35 | ||||
-rw-r--r-- | tests/stdlib/unixsockettest.nim | 26 |
4 files changed, 64 insertions, 1 deletions
diff --git a/lib/pure/nativesockets.nim b/lib/pure/nativesockets.nim index 20ea9d77a..656c98a20 100644 --- a/lib/pure/nativesockets.nim +++ b/lib/pure/nativesockets.nim @@ -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 diff --git a/lib/pure/net.nim b/lib/pure/net.nim index 3324ed9b1..24c94b651 100644 --- a/lib/pure/net.nim +++ b/lib/pure/net.nim @@ -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 diff --git a/tests/stdlib/tunixsocket.nim b/tests/stdlib/tunixsocket.nim new file mode 100644 index 000000000..636fd08c6 --- /dev/null +++ b/tests/stdlib/tunixsocket.nim @@ -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() diff --git a/tests/stdlib/unixsockettest.nim b/tests/stdlib/unixsockettest.nim new file mode 100644 index 000000000..8f95d0808 --- /dev/null +++ b/tests/stdlib/unixsockettest.nim @@ -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) |