raz / commit 8b5a222b

commit
8b5a222b2fc98cbcc98c509b03b0f793390ce080
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
32c0abb2
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.

patch · log

5 files changed, +221 -8

doc/raz-lib.md (history) modified +1 -1

@@ -256,7 +256,7 @@
256256 proc connectTcpWithin*(addr: []U8; port, ms: INT): INT; (* connectTcp, or -110 (ETIMEDOUT) after ms *)
257257 proc listenUdp*(addr: []U8; port: INT): INT; (* a socket of datagrams on port at addr *)
258258 proc receive*(fd: INT; buf: []U8): (INT, Peer); (* the next datagram, in buf, and who sent it *)
259-proc reply*(fd: INT; to: Peer; s: []U8): INT; (* s, as one datagram, to to *)
259+proc reply*(fd: INT; peer: Peer; s: []U8): INT; (* s, as one datagram, to peer *)
260260 proc localPort*(fd: INT): INT; (* the port the socket fd is bound to *)
261261 proc sendFds*(fd: INT; s: []U8; fds: []INT): INT; (* send, and with it fds, to a Unix socket *)
262262 proc receiveFds*(fd: INT; buf: []U8; fds: []INT): (INT, INT); (* read, and the fds that came with it *)

lib/C.raz (history) modified +4 -0

@@ -42,6 +42,10 @@
4242 extern proc accept*(fd: I32; addr: ^U8; len: ^U32): I32;
4343 extern proc accept4*(fd: I32; addr: ^U8; len: ^U32; flags: I32): I32;
4444 extern proc setsockopt*(fd, level, name: I32; value: ^U8; len: U32): I32;
45+extern proc getsockopt*(fd, level, name: I32; value: ^U8; len: ^U32): I32;
46+extern proc getsockname*(fd: I32; addr: ^U8; len: ^U32): I32;
47+extern proc recvfrom*(fd: I32; buf: ^U8; n: UINT; flags: I32; addr: ^U8; len: ^U32): INT;
48+extern proc sendto*(fd: I32; buf: ^U8; n: UINT; flags: I32; addr: ^U8; len: U32): INT;
4549 extern proc inet_pton*(family: I32; src, dst: ^U8): I32;
4650 extern proc clock_gettime*(clock: I32; t: ^U8): I32;
4751 extern proc clock_nanosleep*(clock, flags: I32; t, left: ^U8): I32;

lib/Sys.linux-x86_64.raz (history) modified +104 -3

@@ -73,6 +73,12 @@
7373 port: [2]U8;
7474 data: [24]U8;
7575 end;
76+ (* Whom a datagram came from, as receive says and reply takes: the
77+ address recvfrom fills in, and its size. *)
78+ Peer* = record
79+ a: Sockaddr;
80+ size: INT;
81+ end;
7682 Timespec = record sec, nsec: I64; end;
7783
7884 var
@@ -411,7 +417,9 @@
411417 one := 1; (* so that a server can start again at once *)
412418 _ := SYSCALL(54, fd, 1, 2, ADR(one), 4); (* setsockopt: SOL_SOCKET, SO_REUSEADDR *)
413419 r := SYSCALL(49, fd, a, size); (* bind *)
414- if r = 0 then r := SYSCALL(50, fd, 4096); end; (* listen *)
420+ if (r = 0) and (kind = 1) then (* a stream socket takes connections; *)
421+ r := SYSCALL(50, fd, 4096); (* one of datagrams only receives *)
422+ end;
415423 else
416424 r := SYSCALL(42, fd, a, size); (* connect *)
417425 end;
@@ -567,6 +575,13 @@
567575 return inet(addr, port, 1, true);
568576 end;
569577
578+(* A socket of datagrams on port at addr, from anyone, for receive and
579+ reply. *)
580+proc listenUdp*(addr: []U8; port: INT): INT;
581+begin
582+ return inet(addr, port, 2, true);
583+end;
584+
570585 (* The next connection to the socket fd, waiting for one: a socket of
571586 its own, closed in a program exec starts; or minus an errno. *)
572587 proc accept*(fd: INT): INT;
@@ -631,6 +646,69 @@
631646 return SYSCALL(72, fd, 4, flags); (* F_SETFL *)
632647 end;
633648
649+(* The address of b's first byte, or nil if it has none, for a system
650+ call that takes an address and a length. *)
651+proc bytes(b: []U8): ^U8;
652+begin
653+ if LEN(b) = 0 then return nil; end;
654+ return ADR(b[0]);
655+end;
656+
657+(* A stream socket connected to port at addr, as connectTcp makes it,
658+ or -110 (ETIMEDOUT) if it isn't connected within ms milliseconds.
659+ It starts to connect without waiting, then waits for the socket to
660+ be writable, which it is once connected or refused, and asks which. *)
661+proc connectTcpWithin*(addr: []U8; port, ms: INT): INT;
662+var a: Sockaddr; size, fd, r: INT; err, errSize: I32;
663+begin
664+ size := inetAddr(addr, port, ADR(a));
665+ if size = 0 then return -22; end;
666+ fd := SYSCALL(41, INT(a.family), 1 | 80800H, 0); (* socket: SOCK_STREAM, SOCK_CLOEXEC, SOCK_NONBLOCK *)
667+ if fd < 0 then return fd; end;
668+ r := SYSCALL(42, fd, ADR(a), size); (* connect *)
669+ if r = -115 then (* EINPROGRESS *)
670+ r := -110;
671+ if readyFor([], [fd], ms) = fd then
672+ errSize := 4;
673+ r := SYSCALL(55, fd, 1, 4, ADR(err), ADR(errSize)); (* getsockopt: SOL_SOCKET, SO_ERROR *)
674+ if r = 0 then r := -INT(err); end;
675+ end;
676+ end;
677+ if r = 0 then r := setBlocking(fd, true); end;
678+ if r < 0 then close(fd); return r; end;
679+ return fd;
680+end;
681+
682+(* The port the socket fd is bound to, or minus an errno: for a socket
683+ made on port 0, the one the system chose. *)
684+proc localPort*(fd: INT): INT;
685+var a: Sockaddr; size: I32; r: INT;
686+begin
687+ size := I32(SIZE(Sockaddr));
688+ r := SYSCALL(51, fd, ADR(a), ADR(size)); (* getsockname *)
689+ if r < 0 then return r; end;
690+ return (INT(a.port[0]) shl 8) | INT(a.port[1]);
691+end;
692+
693+(* The next datagram to the socket fd, waiting for one: its first
694+ LEN(buf) bytes in buf, the rest lost, and who sent it. The number is
695+ the bytes in buf, or minus an errno. *)
696+proc receive*(fd: INT; buf: []U8): (INT, Peer);
697+var p: Peer; size: I32; n: INT;
698+begin
699+ size := I32(SIZE(Sockaddr));
700+ n := SYSCALL(45, fd, bytes(buf), LEN(buf), 0, ADR(p.a), ADR(size)); (* recvfrom *)
701+ p.size := INT(size);
702+ return n, p;
703+end;
704+
705+(* Sends s, as one datagram, from the socket fd to peer, as receive
706+ gave it: the bytes sent, or minus an errno. *)
707+proc reply*(fd: INT; peer: Peer; s: []U8): INT;
708+begin
709+ return SYSCALL(44, fd, bytes(s), LEN(s), 4000H, ADR(peer.a), peer.size); (* sendto, MSG_NOSIGNAL *)
710+end;
711+
634712 (* The value of the environment variable name; "" if it isn't set. The
635713 environment follows the arguments and their nil. *)
636714 proc env*(name: []U8): []U8;
@@ -1014,9 +1092,10 @@
10141092
10151093 (* The test of time, random bytes and the network: a sleep the clock
10161094 sees; addresses read and refused; a TCP connection on the loopback;
1017- and a datagram to a port where no one listens, refused. *)
1095+ a datagram to a port where no one listens, refused; servers on
1096+ ports the system chooses, of streams and of datagrams. *)
10181097 proc testNet;
1019-var t: I64; l, c, a, u: INT; x, y: [16]U8; s, s6: Sockaddr;
1098+var t: I64; l, c, a, u, port, n: INT; x, y: [16]U8; s, s6: Sockaddr; peer: Peer;
10201099 begin
10211100 t := clock();
10221101 sleep(20);
@@ -1051,6 +1130,28 @@
10511130 u := connectUdp("127.0.0.1", 47391);
10521131 if (u < 0) or (send(u, "x") # 1) or not ready(u, 1000) or (read(u, x) # -111) then exit(2); end; (* ECONNREFUSED *)
10531132 close(u);
1133+ (* a server on a port the system chooses, and a connection to it
1134+ within a time; then none, where no one listens *)
1135+ l := listenTcp("127.0.0.1", 0);
1136+ port := localPort(l);
1137+ if (l < 0) or (port <= 0) then exit(2); end;
1138+ c := connectTcpWithin("127.0.0.1", port, 1000);
1139+ a := accept(l);
1140+ if (c < 0) or (a < 0) or (send(c, "hi") # 2) or (read(a, x) # 2) then exit(2); end;
1141+ close(a);
1142+ close(c);
1143+ close(l);
1144+ if connectTcpWithin("127.0.0.1", port, 1000) # -111 then exit(2); end;
1145+ (* a datagram to a server, and its answer *)
1146+ l := listenUdp("127.0.0.1", 0);
1147+ port := localPort(l);
1148+ u := connectUdp("127.0.0.1", port);
1149+ if (l < 0) or (port <= 0) or (u < 0) or (send(u, "ping") # 4) then exit(2); end;
1150+ n, peer := receive(l, x[0..2]);
1151+ if (n # 2) or (x[0..2] # "pi") or (reply(l, peer, "pong") # 4) then exit(2); end;
1152+ if (read(u, x) # 4) or (x[0..4] # "pong") then exit(2); end;
1153+ close(u);
1154+ close(l);
10541155 end;
10551156
10561157 (* The module's test: a file written, read back and closed; a

lib/Sys.raz (history) modified +111 -3

@@ -88,6 +88,12 @@
8888 port: [2]U8;
8989 data: [24]U8;
9090 end;
91+ (* Whom a datagram came from, as receive says and reply takes: the
92+ address recvfrom fills in, and its size. *)
93+ Peer* = record
94+ a: Sockaddr;
95+ size: INT;
96+ end;
9197 Timespec = record sec, nsec: I64; end;
9298
9399 var
@@ -427,7 +433,9 @@
427433 one := 1; (* so that a server can start again at once *)
428434 _ := C.setsockopt(fd, 1, 2, ^U8(ADR(one)), 4); (* SOL_SOCKET, SO_REUSEADDR *)
429435 r := C.bind(fd, a, U32(size));
430- if r = 0 then r := C.listen(fd, 4096); end;
436+ if (r = 0) and (kind = 1) then (* a stream socket takes connections; *)
437+ r := C.listen(fd, 4096); (* one of datagrams only receives *)
438+ end;
431439 else
432440 r := C.connect(fd, a, U32(size));
433441 end;
@@ -509,6 +517,13 @@
509517 return inet(addr, port, 1, true);
510518 end;
511519
520+(* A socket of datagrams on port at addr, from anyone, for receive and
521+ reply. *)
522+proc listenUdp*(addr: []U8; port: INT): INT;
523+begin
524+ return inet(addr, port, 2, true);
525+end;
526+
512527 (* The next connection to the socket fd, waiting for one: a socket of
513528 its own, closed in a program exec starts; or minus an errno. *)
514529 proc accept*(fd: INT): INT;
@@ -581,6 +596,76 @@
581596 return 0;
582597 end;
583598
599+(* The address of b's first byte, or nil if it has none, for a call
600+ that takes an address and a length. *)
601+proc bytes(b: []U8): ^U8;
602+begin
603+ if LEN(b) = 0 then return nil; end;
604+ return ADR(b[0]);
605+end;
606+
607+(* A stream socket connected to port at addr, as connectTcp makes it,
608+ or -110 (ETIMEDOUT) if it isn't connected within ms milliseconds.
609+ It starts to connect without waiting, then waits for the socket to
610+ be writable, which it is once connected or refused, and asks which. *)
611+proc connectTcpWithin*(addr: []U8; port, ms: INT): INT;
612+var a: Sockaddr; size, r: INT; fd, err: I32; errSize: U32;
613+begin
614+ size := inetAddr(addr, port, ADR(a));
615+ if size = 0 then return -22; end;
616+ fd := C.socket(I32(a.family), 1 | 80800H, 0); (* SOCK_STREAM, SOCK_CLOEXEC, SOCK_NONBLOCK *)
617+ if fd < 0 then return failed(); end;
618+ r := 0;
619+ if C.connect(fd, ^U8(ADR(a)), U32(size)) # 0 then r := failed(); end;
620+ if r = -115 then (* EINPROGRESS *)
621+ r := -110;
622+ if readyFor([], [INT(fd)], ms) = INT(fd) then
623+ errSize := 4;
624+ if C.getsockopt(fd, 1, 4, ^U8(ADR(err)), ADR(errSize)) = 0 then (* SOL_SOCKET, SO_ERROR *)
625+ r := -INT(err);
626+ else
627+ r := failed();
628+ end;
629+ end;
630+ end;
631+ if r = 0 then r := setBlocking(INT(fd), true); end;
632+ if r < 0 then _ := C.close(fd); return r; end;
633+ return INT(fd);
634+end;
635+
636+(* The port the socket fd is bound to, or minus an errno: for a socket
637+ made on port 0, the one the system chose. *)
638+proc localPort*(fd: INT): INT;
639+var a: Sockaddr; size: U32;
640+begin
641+ size := U32(SIZE(Sockaddr));
642+ if C.getsockname(I32(fd), ^U8(ADR(a)), ADR(size)) # 0 then return failed(); end;
643+ return (INT(a.port[0]) shl 8) | INT(a.port[1]);
644+end;
645+
646+(* The next datagram to the socket fd, waiting for one: its first
647+ LEN(buf) bytes in buf, the rest lost, and who sent it. The number is
648+ the bytes in buf, or minus an errno. *)
649+proc receive*(fd: INT; buf: []U8): (INT, Peer);
650+var p: Peer; size: U32; n: INT;
651+begin
652+ size := U32(SIZE(Sockaddr));
653+ n := C.recvfrom(I32(fd), bytes(buf), UINT(LEN(buf)), 0, ^U8(ADR(p.a)), ADR(size));
654+ if n < 0 then n := failed(); end;
655+ p.size := INT(size);
656+ return n, p;
657+end;
658+
659+(* Sends s, as one datagram, from the socket fd to peer, as receive
660+ gave it: the bytes sent, or minus an errno. *)
661+proc reply*(fd: INT; peer: Peer; s: []U8): INT;
662+var r: INT;
663+begin
664+ r := C.sendto(I32(fd), bytes(s), UINT(LEN(s)), 4000H, ^U8(ADR(peer.a)), U32(peer.size)); (* MSG_NOSIGNAL *)
665+ if r < 0 then return failed(); end;
666+ return r;
667+end;
668+
584669 (* The value of the environment variable name; "" if it isn't set. *)
585670 proc env*(name: []U8): []U8;
586671 var a: [4096]U8; s: ^U8; n: INT;
@@ -977,9 +1062,10 @@
9771062
9781063 (* The test of time, random bytes and the network: a sleep the clock
9791064 sees; addresses read and refused; a TCP connection on the loopback;
980- and a datagram to a port where no one listens, refused. *)
1065+ a datagram to a port where no one listens, refused; servers on
1066+ ports the system chooses, of streams and of datagrams. *)
9811067 proc testNet;
982-var t: I64; l, c, a, u: INT; x, y: [16]U8; s, s6: Sockaddr;
1068+var t: I64; l, c, a, u, port, n: INT; x, y: [16]U8; s, s6: Sockaddr; peer: Peer;
9831069 begin
9841070 t := clock();
9851071 sleep(20);
@@ -1014,6 +1100,28 @@
10141100 u := connectUdp("127.0.0.1", 47391);
10151101 if (u < 0) or (send(u, "x") # 1) or not ready(u, 1000) or (read(u, x) # -111) then exit(2); end; (* ECONNREFUSED *)
10161102 close(u);
1103+ (* a server on a port the system chooses, and a connection to it
1104+ within a time; then none, where no one listens *)
1105+ l := listenTcp("127.0.0.1", 0);
1106+ port := localPort(l);
1107+ if (l < 0) or (port <= 0) then exit(2); end;
1108+ c := connectTcpWithin("127.0.0.1", port, 1000);
1109+ a := accept(l);
1110+ if (c < 0) or (a < 0) or (send(c, "hi") # 2) or (read(a, x) # 2) then exit(2); end;
1111+ close(a);
1112+ close(c);
1113+ close(l);
1114+ if connectTcpWithin("127.0.0.1", port, 1000) # -111 then exit(2); end;
1115+ (* a datagram to a server, and its answer *)
1116+ l := listenUdp("127.0.0.1", 0);
1117+ port := localPort(l);
1118+ u := connectUdp("127.0.0.1", port);
1119+ if (l < 0) or (port <= 0) or (u < 0) or (send(u, "ping") # 4) then exit(2); end;
1120+ n, peer := receive(l, x[0..2]);
1121+ if (n # 2) or (x[0..2] # "pi") or (reply(l, peer, "pong") # 4) then exit(2); end;
1122+ if (read(u, x) # 4) or (x[0..4] # "pong") then exit(2); end;
1123+ close(u);
1124+ close(l);
10171125 end;
10181126
10191127 (* The module's test, as Linux's Sys has it: a file written, read back

src/Raz.raz (history) modified +1 -1

@@ -29,7 +29,7 @@
2929 import Sys, Out, Version, Files, Fmt, Diag, Scan, Sym, Chunk, X64, Gen, Parse, Link, Elf;
3030
3131 const
32- Report = "2026-10.23"; (* the version of doc/raz.md it follows *)
32+ Report = "2026-10.24"; (* the version of doc/raz.md it follows *)
3333 MemfdCreate = 319; (* the system call *)
3434 MfdCloexec = 1; (* an ELF still executes: the kernel opens it first *)
3535 Execve = 59;