From 8b5a222b2fc98cbcc98c509b03b0f793390ce080 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Wed, 14 Oct 2026 15:11:56 +0100 Subject: [PATCH] Sys: datagrams served, a socket's port, connecting within a time listenUdp, receive and reply; localPort (getsockname); and connectTcpWithin, a non-blocking connect and a wait to write, -110 past ms. Both Sys files, and their tests: servers on port 0, a ping answered, a refused connection. The report is 2026-10.24. --- diff --git a/doc/raz-lib.md b/doc/raz-lib.md index 3962d59..15f182b 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -256,7 +256,7 @@ proc connectTcpWithin*(addr: []U8; port, ms: INT): INT; (* connectTcp, or -110 (ETIMEDOUT) after ms *) proc listenUdp*(addr: []U8; port: INT): INT; (* a socket of datagrams on port at addr *) proc receive*(fd: INT; buf: []U8): (INT, Peer); (* the next datagram, in buf, and who sent it *) -proc reply*(fd: INT; to: Peer; s: []U8): INT; (* s, as one datagram, to to *) +proc reply*(fd: INT; peer: Peer; s: []U8): INT; (* s, as one datagram, to peer *) proc localPort*(fd: INT): INT; (* the port the socket fd is bound to *) proc sendFds*(fd: INT; s: []U8; fds: []INT): INT; (* send, and with it fds, to a Unix socket *) proc receiveFds*(fd: INT; buf: []U8; fds: []INT): (INT, INT); (* read, and the fds that came with it *) diff --git a/lib/C.raz b/lib/C.raz index 3913474..8018eb9 100644 --- a/lib/C.raz +++ b/lib/C.raz @@ -42,6 +42,10 @@ extern proc accept*(fd: I32; addr: ^U8; len: ^U32): I32; extern proc accept4*(fd: I32; addr: ^U8; len: ^U32; flags: I32): I32; extern proc setsockopt*(fd, level, name: I32; value: ^U8; len: U32): I32; +extern proc getsockopt*(fd, level, name: I32; value: ^U8; len: ^U32): I32; +extern proc getsockname*(fd: I32; addr: ^U8; len: ^U32): I32; +extern proc recvfrom*(fd: I32; buf: ^U8; n: UINT; flags: I32; addr: ^U8; len: ^U32): INT; +extern proc sendto*(fd: I32; buf: ^U8; n: UINT; flags: I32; addr: ^U8; len: U32): INT; extern proc inet_pton*(family: I32; src, dst: ^U8): I32; extern proc clock_gettime*(clock: I32; t: ^U8): I32; extern proc clock_nanosleep*(clock, flags: I32; t, left: ^U8): I32; diff --git a/lib/Sys.linux-x86_64.raz b/lib/Sys.linux-x86_64.raz index 7a2533b..a205ab4 100644 --- a/lib/Sys.linux-x86_64.raz +++ b/lib/Sys.linux-x86_64.raz @@ -73,6 +73,12 @@ port: [2]U8; data: [24]U8; end; + (* Whom a datagram came from, as receive says and reply takes: the + address recvfrom fills in, and its size. *) + Peer* = record + a: Sockaddr; + size: INT; + end; Timespec = record sec, nsec: I64; end; var @@ -411,7 +417,9 @@ one := 1; (* so that a server can start again at once *) _ := SYSCALL(54, fd, 1, 2, ADR(one), 4); (* setsockopt: SOL_SOCKET, SO_REUSEADDR *) r := SYSCALL(49, fd, a, size); (* bind *) - if r = 0 then r := SYSCALL(50, fd, 4096); end; (* listen *) + if (r = 0) and (kind = 1) then (* a stream socket takes connections; *) + r := SYSCALL(50, fd, 4096); (* one of datagrams only receives *) + end; else r := SYSCALL(42, fd, a, size); (* connect *) end; @@ -567,6 +575,13 @@ return inet(addr, port, 1, true); end; +(* A socket of datagrams on port at addr, from anyone, for receive and + reply. *) +proc listenUdp*(addr: []U8; port: INT): INT; +begin + return inet(addr, port, 2, true); +end; + (* The next connection to the socket fd, waiting for one: a socket of its own, closed in a program exec starts; or minus an errno. *) proc accept*(fd: INT): INT; @@ -631,6 +646,69 @@ return SYSCALL(72, fd, 4, flags); (* F_SETFL *) end; +(* The address of b's first byte, or nil if it has none, for a system + call that takes an address and a length. *) +proc bytes(b: []U8): ^U8; +begin + if LEN(b) = 0 then return nil; end; + return ADR(b[0]); +end; + +(* A stream socket connected to port at addr, as connectTcp makes it, + or -110 (ETIMEDOUT) if it isn't connected within ms milliseconds. + It starts to connect without waiting, then waits for the socket to + be writable, which it is once connected or refused, and asks which. *) +proc connectTcpWithin*(addr: []U8; port, ms: INT): INT; +var a: Sockaddr; size, fd, r: INT; err, errSize: I32; +begin + size := inetAddr(addr, port, ADR(a)); + if size = 0 then return -22; end; + fd := SYSCALL(41, INT(a.family), 1 | 80800H, 0); (* socket: SOCK_STREAM, SOCK_CLOEXEC, SOCK_NONBLOCK *) + if fd < 0 then return fd; end; + r := SYSCALL(42, fd, ADR(a), size); (* connect *) + if r = -115 then (* EINPROGRESS *) + r := -110; + if readyFor([], [fd], ms) = fd then + errSize := 4; + r := SYSCALL(55, fd, 1, 4, ADR(err), ADR(errSize)); (* getsockopt: SOL_SOCKET, SO_ERROR *) + if r = 0 then r := -INT(err); end; + end; + end; + if r = 0 then r := setBlocking(fd, true); end; + if r < 0 then close(fd); return r; end; + return fd; +end; + +(* The port the socket fd is bound to, or minus an errno: for a socket + made on port 0, the one the system chose. *) +proc localPort*(fd: INT): INT; +var a: Sockaddr; size: I32; r: INT; +begin + size := I32(SIZE(Sockaddr)); + r := SYSCALL(51, fd, ADR(a), ADR(size)); (* getsockname *) + if r < 0 then return r; end; + return (INT(a.port[0]) shl 8) | INT(a.port[1]); +end; + +(* The next datagram to the socket fd, waiting for one: its first + LEN(buf) bytes in buf, the rest lost, and who sent it. The number is + the bytes in buf, or minus an errno. *) +proc receive*(fd: INT; buf: []U8): (INT, Peer); +var p: Peer; size: I32; n: INT; +begin + size := I32(SIZE(Sockaddr)); + n := SYSCALL(45, fd, bytes(buf), LEN(buf), 0, ADR(p.a), ADR(size)); (* recvfrom *) + p.size := INT(size); + return n, p; +end; + +(* Sends s, as one datagram, from the socket fd to peer, as receive + gave it: the bytes sent, or minus an errno. *) +proc reply*(fd: INT; peer: Peer; s: []U8): INT; +begin + return SYSCALL(44, fd, bytes(s), LEN(s), 4000H, ADR(peer.a), peer.size); (* sendto, MSG_NOSIGNAL *) +end; + (* The value of the environment variable name; "" if it isn't set. The environment follows the arguments and their nil. *) proc env*(name: []U8): []U8; @@ -1014,9 +1092,10 @@ (* The test of time, random bytes and the network: a sleep the clock sees; addresses read and refused; a TCP connection on the loopback; - and a datagram to a port where no one listens, refused. *) + a datagram to a port where no one listens, refused; servers on + ports the system chooses, of streams and of datagrams. *) proc testNet; -var t: I64; l, c, a, u: INT; x, y: [16]U8; s, s6: Sockaddr; +var t: I64; l, c, a, u, port, n: INT; x, y: [16]U8; s, s6: Sockaddr; peer: Peer; begin t := clock(); sleep(20); @@ -1051,6 +1130,28 @@ u := connectUdp("127.0.0.1", 47391); if (u < 0) or (send(u, "x") # 1) or not ready(u, 1000) or (read(u, x) # -111) then exit(2); end; (* ECONNREFUSED *) close(u); + (* a server on a port the system chooses, and a connection to it + within a time; then none, where no one listens *) + l := listenTcp("127.0.0.1", 0); + port := localPort(l); + if (l < 0) or (port <= 0) then exit(2); end; + c := connectTcpWithin("127.0.0.1", port, 1000); + a := accept(l); + if (c < 0) or (a < 0) or (send(c, "hi") # 2) or (read(a, x) # 2) then exit(2); end; + close(a); + close(c); + close(l); + if connectTcpWithin("127.0.0.1", port, 1000) # -111 then exit(2); end; + (* a datagram to a server, and its answer *) + l := listenUdp("127.0.0.1", 0); + port := localPort(l); + u := connectUdp("127.0.0.1", port); + if (l < 0) or (port <= 0) or (u < 0) or (send(u, "ping") # 4) then exit(2); end; + n, peer := receive(l, x[0..2]); + if (n # 2) or (x[0..2] # "pi") or (reply(l, peer, "pong") # 4) then exit(2); end; + if (read(u, x) # 4) or (x[0..4] # "pong") then exit(2); end; + close(u); + close(l); end; (* The module's test: a file written, read back and closed; a diff --git a/lib/Sys.raz b/lib/Sys.raz index ad0fa83..a68db6f 100644 --- a/lib/Sys.raz +++ b/lib/Sys.raz @@ -88,6 +88,12 @@ port: [2]U8; data: [24]U8; end; + (* Whom a datagram came from, as receive says and reply takes: the + address recvfrom fills in, and its size. *) + Peer* = record + a: Sockaddr; + size: INT; + end; Timespec = record sec, nsec: I64; end; var @@ -427,7 +433,9 @@ one := 1; (* so that a server can start again at once *) _ := C.setsockopt(fd, 1, 2, ^U8(ADR(one)), 4); (* SOL_SOCKET, SO_REUSEADDR *) r := C.bind(fd, a, U32(size)); - if r = 0 then r := C.listen(fd, 4096); end; + if (r = 0) and (kind = 1) then (* a stream socket takes connections; *) + r := C.listen(fd, 4096); (* one of datagrams only receives *) + end; else r := C.connect(fd, a, U32(size)); end; @@ -509,6 +517,13 @@ return inet(addr, port, 1, true); end; +(* A socket of datagrams on port at addr, from anyone, for receive and + reply. *) +proc listenUdp*(addr: []U8; port: INT): INT; +begin + return inet(addr, port, 2, true); +end; + (* The next connection to the socket fd, waiting for one: a socket of its own, closed in a program exec starts; or minus an errno. *) proc accept*(fd: INT): INT; @@ -581,6 +596,76 @@ return 0; end; +(* The address of b's first byte, or nil if it has none, for a call + that takes an address and a length. *) +proc bytes(b: []U8): ^U8; +begin + if LEN(b) = 0 then return nil; end; + return ADR(b[0]); +end; + +(* A stream socket connected to port at addr, as connectTcp makes it, + or -110 (ETIMEDOUT) if it isn't connected within ms milliseconds. + It starts to connect without waiting, then waits for the socket to + be writable, which it is once connected or refused, and asks which. *) +proc connectTcpWithin*(addr: []U8; port, ms: INT): INT; +var a: Sockaddr; size, r: INT; fd, err: I32; errSize: U32; +begin + size := inetAddr(addr, port, ADR(a)); + if size = 0 then return -22; end; + fd := C.socket(I32(a.family), 1 | 80800H, 0); (* SOCK_STREAM, SOCK_CLOEXEC, SOCK_NONBLOCK *) + if fd < 0 then return failed(); end; + r := 0; + if C.connect(fd, ^U8(ADR(a)), U32(size)) # 0 then r := failed(); end; + if r = -115 then (* EINPROGRESS *) + r := -110; + if readyFor([], [INT(fd)], ms) = INT(fd) then + errSize := 4; + if C.getsockopt(fd, 1, 4, ^U8(ADR(err)), ADR(errSize)) = 0 then (* SOL_SOCKET, SO_ERROR *) + r := -INT(err); + else + r := failed(); + end; + end; + end; + if r = 0 then r := setBlocking(INT(fd), true); end; + if r < 0 then _ := C.close(fd); return r; end; + return INT(fd); +end; + +(* The port the socket fd is bound to, or minus an errno: for a socket + made on port 0, the one the system chose. *) +proc localPort*(fd: INT): INT; +var a: Sockaddr; size: U32; +begin + size := U32(SIZE(Sockaddr)); + if C.getsockname(I32(fd), ^U8(ADR(a)), ADR(size)) # 0 then return failed(); end; + return (INT(a.port[0]) shl 8) | INT(a.port[1]); +end; + +(* The next datagram to the socket fd, waiting for one: its first + LEN(buf) bytes in buf, the rest lost, and who sent it. The number is + the bytes in buf, or minus an errno. *) +proc receive*(fd: INT; buf: []U8): (INT, Peer); +var p: Peer; size: U32; n: INT; +begin + size := U32(SIZE(Sockaddr)); + n := C.recvfrom(I32(fd), bytes(buf), UINT(LEN(buf)), 0, ^U8(ADR(p.a)), ADR(size)); + if n < 0 then n := failed(); end; + p.size := INT(size); + return n, p; +end; + +(* Sends s, as one datagram, from the socket fd to peer, as receive + gave it: the bytes sent, or minus an errno. *) +proc reply*(fd: INT; peer: Peer; s: []U8): INT; +var r: INT; +begin + r := C.sendto(I32(fd), bytes(s), UINT(LEN(s)), 4000H, ^U8(ADR(peer.a)), U32(peer.size)); (* MSG_NOSIGNAL *) + if r < 0 then return failed(); end; + return r; +end; + (* The value of the environment variable name; "" if it isn't set. *) proc env*(name: []U8): []U8; var a: [4096]U8; s: ^U8; n: INT; @@ -977,9 +1062,10 @@ (* The test of time, random bytes and the network: a sleep the clock sees; addresses read and refused; a TCP connection on the loopback; - and a datagram to a port where no one listens, refused. *) + a datagram to a port where no one listens, refused; servers on + ports the system chooses, of streams and of datagrams. *) proc testNet; -var t: I64; l, c, a, u: INT; x, y: [16]U8; s, s6: Sockaddr; +var t: I64; l, c, a, u, port, n: INT; x, y: [16]U8; s, s6: Sockaddr; peer: Peer; begin t := clock(); sleep(20); @@ -1014,6 +1100,28 @@ u := connectUdp("127.0.0.1", 47391); if (u < 0) or (send(u, "x") # 1) or not ready(u, 1000) or (read(u, x) # -111) then exit(2); end; (* ECONNREFUSED *) close(u); + (* a server on a port the system chooses, and a connection to it + within a time; then none, where no one listens *) + l := listenTcp("127.0.0.1", 0); + port := localPort(l); + if (l < 0) or (port <= 0) then exit(2); end; + c := connectTcpWithin("127.0.0.1", port, 1000); + a := accept(l); + if (c < 0) or (a < 0) or (send(c, "hi") # 2) or (read(a, x) # 2) then exit(2); end; + close(a); + close(c); + close(l); + if connectTcpWithin("127.0.0.1", port, 1000) # -111 then exit(2); end; + (* a datagram to a server, and its answer *) + l := listenUdp("127.0.0.1", 0); + port := localPort(l); + u := connectUdp("127.0.0.1", port); + if (l < 0) or (port <= 0) or (u < 0) or (send(u, "ping") # 4) then exit(2); end; + n, peer := receive(l, x[0..2]); + if (n # 2) or (x[0..2] # "pi") or (reply(l, peer, "pong") # 4) then exit(2); end; + if (read(u, x) # 4) or (x[0..4] # "pong") then exit(2); end; + close(u); + close(l); end; (* The module's test, as Linux's Sys has it: a file written, read back diff --git a/src/Raz.raz b/src/Raz.raz index 52beac2..6145e75 100644 --- a/src/Raz.raz +++ b/src/Raz.raz @@ -29,7 +29,7 @@ import Sys, Out, Version, Files, Fmt, Diag, Scan, Sym, Chunk, X64, Gen, Parse, Link, Elf; const - Report = "2026-10.23"; (* the version of doc/raz.md it follows *) + Report = "2026-10.24"; (* the version of doc/raz.md it follows *) MemfdCreate = 319; (* the system call *) MfdCloexec = 1; (* an ELF still executes: the kernel opens it first *) Execve = 59;