mirror of
https://github.com/nim-lang/Nim.git
synced 2026-01-02 03:02:31 +00:00
Merge branch 'girvo-unix-sockets' into devel
This commit is contained in:
6
examples/unix_socket/client.nim
Normal file
6
examples/unix_socket/client.nim
Normal file
@@ -0,0 +1,6 @@
|
||||
import net
|
||||
|
||||
let sock = newSocket(AF_UNIX, SOCK_STREAM, IPPROTO_IP)
|
||||
|
||||
sock.connectUnix("sock")
|
||||
sock.send("hello\n")
|
||||
14
examples/unix_socket/server.nim
Normal file
14
examples/unix_socket/server.nim
Normal file
@@ -0,0 +1,14 @@
|
||||
import net
|
||||
|
||||
let sock = newSocket(AF_UNIX, SOCK_STREAM, IPPROTO_IP)
|
||||
sock.bindUnix("sock")
|
||||
sock.listen()
|
||||
|
||||
while true:
|
||||
var client = new(Socket)
|
||||
sock.accept(client)
|
||||
var output = ""
|
||||
output.setLen 32
|
||||
client.readLine(output)
|
||||
echo "got ", output
|
||||
client.close()
|
||||
@@ -448,6 +448,14 @@ when hasSpawnH:
|
||||
Tposix_spawn_file_actions* {.importc: "posix_spawn_file_actions_t",
|
||||
header: "<spawn.h>", final, pure.} = object
|
||||
|
||||
when defined(linux):
|
||||
# from sys/un.h
|
||||
const Sockaddr_un_path_length* = 108
|
||||
else:
|
||||
# according to http://pubs.opengroup.org/onlinepubs/009604499/basedefs/sys/un.h.html
|
||||
# this is >=92
|
||||
const Sockaddr_un_path_length* = 92
|
||||
|
||||
type
|
||||
Socklen* {.importc: "socklen_t", header: "<sys/socket.h>".} = cuint
|
||||
TSa_Family* {.importc: "sa_family_t", header: "<sys/socket.h>".} = cint
|
||||
@@ -457,6 +465,11 @@ type
|
||||
sa_family*: TSa_Family ## Address family.
|
||||
sa_data*: array [0..255, char] ## Socket address (variable-length data).
|
||||
|
||||
Sockaddr_un* {.importc: "struct sockaddr_un", header: "<sys/un.h>",
|
||||
pure, final.} = object ## struct sockaddr_un
|
||||
sun_family*: TSa_Family ## Address family.
|
||||
sun_path*: array [0..Sockaddr_un_path_length-1, char] ## Socket path
|
||||
|
||||
Sockaddr_storage* {.importc: "struct sockaddr_storage",
|
||||
header: "<sys/socket.h>",
|
||||
pure, final.} = object ## struct sockaddr_storage
|
||||
|
||||
@@ -27,7 +27,7 @@ else:
|
||||
import posix
|
||||
export fcntl, F_GETFL, O_NONBLOCK, F_SETFL, EAGAIN, EWOULDBLOCK, MSG_NOSIGNAL,
|
||||
EINTR, EINPROGRESS, ECONNRESET, EPIPE, ENETRESET
|
||||
export Sockaddr_storage
|
||||
export Sockaddr_storage, Sockaddr_un, Sockaddr_un_path_length
|
||||
|
||||
export SocketHandle, Sockaddr_in, Addrinfo, INADDR_ANY, SockAddr, SockLen,
|
||||
Sockaddr_in6,
|
||||
|
||||
@@ -68,6 +68,7 @@
|
||||
{.deadCodeElim: on.}
|
||||
import nativesockets, os, strutils, parseutils, times
|
||||
export Port, `$`, `==`
|
||||
export Domain, SockType, Protocol
|
||||
|
||||
const useWinVersion = defined(Windows) or defined(nimdoc)
|
||||
const defineSsl = defined(ssl) or defined(nimdoc)
|
||||
@@ -605,9 +606,32 @@ proc setSockOpt*(socket: Socket, opt: SOBool, value: bool, level = SOL_SOCKET) {
|
||||
var valuei = cint(if value: 1 else: 0)
|
||||
setSockOptInt(socket.fd, cint(level), toCInt(opt), valuei)
|
||||
|
||||
when defineSsl:
|
||||
when defined(posix) or defined(nimdoc):
|
||||
proc makeUnixAddr(path: string): Sockaddr_un =
|
||||
result.sun_family = AF_UNIX.toInt
|
||||
if path.len >= Sockaddr_un_path_length:
|
||||
raise newException(ValueError, "socket path too long")
|
||||
copyMem(addr result.sun_path, path.cstring, path.len + 1)
|
||||
|
||||
proc connectUnix*(socket: Socket, path: string) =
|
||||
## Connects to Unix socket on `path`.
|
||||
## This only works on Unix-style systems: Mac OS X, BSD and Linux
|
||||
var socketAddr = makeUnixAddr(path)
|
||||
if socket.fd.connect(cast[ptr SockAddr](addr socketAddr),
|
||||
sizeof(socketAddr).Socklen) != 0'i32:
|
||||
raiseOSError(osLastError())
|
||||
|
||||
proc bindUnix*(socket: Socket, path: string) =
|
||||
## Binds Unix socket to `path`.
|
||||
## This only works on Unix-style systems: Mac OS X, BSD and Linux
|
||||
var socketAddr = makeUnixAddr(path)
|
||||
if socket.fd.bindAddr(cast[ptr SockAddr](addr socketAddr),
|
||||
sizeof(socketAddr).Socklen) != 0'i32:
|
||||
raiseOSError(osLastError())
|
||||
|
||||
when defined(ssl):
|
||||
proc handshake*(socket: Socket): bool
|
||||
{.tags: [ReadIOEffect, WriteIOEffect], deprecated.} =
|
||||
{.tags: [ReadIOEffect, WriteIOEffect], deprecated.} =
|
||||
## This proc needs to be called on a socket after it connects. This is
|
||||
## only applicable when using ``connectAsync``.
|
||||
## This proc performs the SSL handshake.
|
||||
|
||||
Reference in New Issue
Block a user