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.
| @@ -256,7 +256,7 @@ | ||
| 256 | 256 | proc connectTcpWithin*(addr: []U8; port, ms: INT): INT; (* connectTcp, or -110 (ETIMEDOUT) after ms *) |
| 257 | 257 | proc listenUdp*(addr: []U8; port: INT): INT; (* a socket of datagrams on port at addr *) |
| 258 | 258 | 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 *) | |
| 260 | 260 | proc localPort*(fd: INT): INT; (* the port the socket fd is bound to *) |
| 261 | 261 | proc sendFds*(fd: INT; s: []U8; fds: []INT): INT; (* send, and with it fds, to a Unix socket *) |
| 262 | 262 | proc receiveFds*(fd: INT; buf: []U8; fds: []INT): (INT, INT); (* read, and the fds that came with it *) |
| @@ -42,6 +42,10 @@ | ||
| 42 | 42 | extern proc accept*(fd: I32; addr: ^U8; len: ^U32): I32; |
| 43 | 43 | extern proc accept4*(fd: I32; addr: ^U8; len: ^U32; flags: I32): I32; |
| 44 | 44 | 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; | |
| 45 | 49 | extern proc inet_pton*(family: I32; src, dst: ^U8): I32; |
| 46 | 50 | extern proc clock_gettime*(clock: I32; t: ^U8): I32; |
| 47 | 51 | extern proc clock_nanosleep*(clock, flags: I32; t, left: ^U8): I32; |
| @@ -73,6 +73,12 @@ | ||
| 73 | 73 | port: [2]U8; |
| 74 | 74 | data: [24]U8; |
| 75 | 75 | 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; | |
| 76 | 82 | Timespec = record sec, nsec: I64; end; |
| 77 | 83 | |
| 78 | 84 | var |
| @@ -411,7 +417,9 @@ | ||
| 411 | 417 | one := 1; (* so that a server can start again at once *) |
| 412 | 418 | _ := SYSCALL(54, fd, 1, 2, ADR(one), 4); (* setsockopt: SOL_SOCKET, SO_REUSEADDR *) |
| 413 | 419 | 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; | |
| 415 | 423 | else |
| 416 | 424 | r := SYSCALL(42, fd, a, size); (* connect *) |
| 417 | 425 | end; |
| @@ -567,6 +575,13 @@ | ||
| 567 | 575 | return inet(addr, port, 1, true); |
| 568 | 576 | end; |
| 569 | 577 | |
| 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 | + | |
| 570 | 585 | (* The next connection to the socket fd, waiting for one: a socket of |
| 571 | 586 | its own, closed in a program exec starts; or minus an errno. *) |
| 572 | 587 | proc accept*(fd: INT): INT; |
| @@ -631,6 +646,69 @@ | ||
| 631 | 646 | return SYSCALL(72, fd, 4, flags); (* F_SETFL *) |
| 632 | 647 | end; |
| 633 | 648 | |
| 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 | + | |
| 634 | 712 | (* The value of the environment variable name; "" if it isn't set. The |
| 635 | 713 | environment follows the arguments and their nil. *) |
| 636 | 714 | proc env*(name: []U8): []U8; |
| @@ -1014,9 +1092,10 @@ | ||
| 1014 | 1092 | |
| 1015 | 1093 | (* The test of time, random bytes and the network: a sleep the clock |
| 1016 | 1094 | 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. *) | |
| 1018 | 1097 | 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; | |
| 1020 | 1099 | begin |
| 1021 | 1100 | t := clock(); |
| 1022 | 1101 | sleep(20); |
| @@ -1051,6 +1130,28 @@ | ||
| 1051 | 1130 | u := connectUdp("127.0.0.1", 47391); |
| 1052 | 1131 | if (u < 0) or (send(u, "x") # 1) or not ready(u, 1000) or (read(u, x) # -111) then exit(2); end; (* ECONNREFUSED *) |
| 1053 | 1132 | 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); | |
| 1054 | 1155 | end; |
| 1055 | 1156 | |
| 1056 | 1157 | (* The module's test: a file written, read back and closed; a |
| @@ -88,6 +88,12 @@ | ||
| 88 | 88 | port: [2]U8; |
| 89 | 89 | data: [24]U8; |
| 90 | 90 | 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; | |
| 91 | 97 | Timespec = record sec, nsec: I64; end; |
| 92 | 98 | |
| 93 | 99 | var |
| @@ -427,7 +433,9 @@ | ||
| 427 | 433 | one := 1; (* so that a server can start again at once *) |
| 428 | 434 | _ := C.setsockopt(fd, 1, 2, ^U8(ADR(one)), 4); (* SOL_SOCKET, SO_REUSEADDR *) |
| 429 | 435 | 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; | |
| 431 | 439 | else |
| 432 | 440 | r := C.connect(fd, a, U32(size)); |
| 433 | 441 | end; |
| @@ -509,6 +517,13 @@ | ||
| 509 | 517 | return inet(addr, port, 1, true); |
| 510 | 518 | end; |
| 511 | 519 | |
| 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 | + | |
| 512 | 527 | (* The next connection to the socket fd, waiting for one: a socket of |
| 513 | 528 | its own, closed in a program exec starts; or minus an errno. *) |
| 514 | 529 | proc accept*(fd: INT): INT; |
| @@ -581,6 +596,76 @@ | ||
| 581 | 596 | return 0; |
| 582 | 597 | end; |
| 583 | 598 | |
| 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 | + | |
| 584 | 669 | (* The value of the environment variable name; "" if it isn't set. *) |
| 585 | 670 | proc env*(name: []U8): []U8; |
| 586 | 671 | var a: [4096]U8; s: ^U8; n: INT; |
| @@ -977,9 +1062,10 @@ | ||
| 977 | 1062 | |
| 978 | 1063 | (* The test of time, random bytes and the network: a sleep the clock |
| 979 | 1064 | 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. *) | |
| 981 | 1067 | 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; | |
| 983 | 1069 | begin |
| 984 | 1070 | t := clock(); |
| 985 | 1071 | sleep(20); |
| @@ -1014,6 +1100,28 @@ | ||
| 1014 | 1100 | u := connectUdp("127.0.0.1", 47391); |
| 1015 | 1101 | if (u < 0) or (send(u, "x") # 1) or not ready(u, 1000) or (read(u, x) # -111) then exit(2); end; (* ECONNREFUSED *) |
| 1016 | 1102 | 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); | |
| 1017 | 1125 | end; |
| 1018 | 1126 | |
| 1019 | 1127 | (* The module's test, as Linux's Sys has it: a file written, read back |
| @@ -29,7 +29,7 @@ | ||
| 29 | 29 | import Sys, Out, Version, Files, Fmt, Diag, Scan, Sym, Chunk, X64, Gen, Parse, Link, Elf; |
| 30 | 30 | |
| 31 | 31 | 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 *) | |
| 33 | 33 | MemfdCreate = 319; (* the system call *) |
| 34 | 34 | MfdCloexec = 1; (* an ELF still executes: the kernel opens it first *) |
| 35 | 35 | Execve = 59; |