Sys: clock, now, sleep, random; TCP, UDP and listening sockets clock and now by clock_gettime; sleep until a deadline on the monotonic clock, again after EINTR; random by getrandom. connect, listen, connectTcp, connectUdp and listenTcp share one socket routine, close-on- exec, servers with SO_REUSEADDR; listen is now exported and doesn't remove what is at its path. Addresses as numbers: read by hand on Linux, by inet_pton over C. accept by accept4. Both Sys files, each tested. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
| @@ -37,7 +37,7 @@ | ||
| 37 | 37 | child with its `Status`, process groups and the terminal's |
| 38 | 38 | foreground, `pid`; signals caught on a file descriptor. Rat |
| 39 | 39 | breaks here: `spawn` changes. |
| 40 | -- [ ] 4. `Sys`, the rest: `clock`, `now`, `sleep`, `random`; | |
| 40 | +- [x] 4. `Sys`, the rest: `clock`, `now`, `sleep`, `random`; | |
| 41 | 41 | `connectTcp`, `connectUdp`, `listen`, `listenTcp`, `accept`. |
| 42 | 42 | - [ ] 5. `Str.encode`, `Str.decode`, `Str.toInt`; `Map.remove` and |
| 43 | 43 | `Map.next`; `Sort`; `Math`; `Fmt.real`. Each module's `main` |
| @@ -38,6 +38,12 @@ | ||
| 38 | 38 | extern proc bind*(fd: I32; addr: ^U8; len: U32): I32; |
| 39 | 39 | extern proc listen*(fd, backlog: I32): I32; |
| 40 | 40 | extern proc accept*(fd: I32; addr: ^U8; len: ^U32): I32; |
| 41 | +extern proc accept4*(fd: I32; addr: ^U8; len: ^U32; flags: I32): I32; | |
| 42 | +extern proc setsockopt*(fd, level, name: I32; value: ^U8; len: U32): I32; | |
| 43 | +extern proc inet_pton*(family: I32; src, dst: ^U8): I32; | |
| 44 | +extern proc clock_gettime*(clock: I32; t: ^U8): I32; | |
| 45 | +extern proc clock_nanosleep*(clock, flags: I32; t, left: ^U8): I32; | |
| 46 | +extern proc getrandom*(buf: ^U8; n: U64; flags: U32): I64; | |
| 41 | 47 | extern proc getenv*(name: ^U8): ^U8; |
| 42 | 48 | extern proc getcwd*(buf: ^U8; n: U64): ^U8; |
| 43 | 49 | extern proc chdir*(path: ^U8): I32; |
| @@ -62,6 +62,14 @@ | ||
| 62 | 62 | family: U16; |
| 63 | 63 | path: [108]U8; |
| 64 | 64 | end; |
| 65 | + (* struct sockaddr_in or sockaddr_in6: AF_INET and the IPv4 address | |
| 66 | + in data[0..4], or AF_INET6 and the IPv6 one in data[4..20] *) | |
| 67 | + Sockaddr = record | |
| 68 | + family: U16; | |
| 69 | + port: [2]U8; | |
| 70 | + data: [24]U8; | |
| 71 | + end; | |
| 72 | + Timespec = record sec, nsec: I64; end; | |
| 65 | 73 | |
| 66 | 74 | var |
| 67 | 75 | cwd: [4096]U8; (* what workDir gives *) |
| @@ -334,22 +342,220 @@ | ||
| 334 | 342 | return SYSCALL(7, ADR(p), 1, ms) > 0; (* poll *) |
| 335 | 343 | end; |
| 336 | 344 | |
| 337 | -(* A stream socket connected to the Unix socket at path: its file | |
| 345 | +(* The clock that only goes forward, in nanoseconds: for how long | |
| 346 | + something takes. *) | |
| 347 | +proc clock*: I64; | |
| 348 | +var t: Timespec; | |
| 349 | +begin | |
| 350 | + SYSCALL(228, 1, ADR(t)); (* clock_gettime: CLOCK_MONOTONIC *) | |
| 351 | + return t.sec * 1000000000 + t.nsec; | |
| 352 | +end; | |
| 353 | + | |
| 354 | +(* The time in nanoseconds since 1970-01-01, UTC. *) | |
| 355 | +proc now*: I64; | |
| 356 | +var t: Timespec; | |
| 357 | +begin | |
| 358 | + SYSCALL(228, 0, ADR(t)); (* clock_gettime: CLOCK_REALTIME *) | |
| 359 | + return t.sec * 1000000000 + t.nsec; | |
| 360 | +end; | |
| 361 | + | |
| 362 | +(* Waits ms milliseconds. *) | |
| 363 | +proc sleep*(ms: I64); | |
| 364 | +var t: Timespec; at: I64; | |
| 365 | +begin | |
| 366 | + at := clock() + ms * 1000000; | |
| 367 | + t := {sec: at div 1000000000, nsec: at mod 1000000000}; | |
| 368 | + repeat until SYSCALL(230, 1, 1, ADR(t), 0) # -4; (* clock_nanosleep until t, again after EINTR *) | |
| 369 | +end; | |
| 370 | + | |
| 371 | +(* Fills b with random bytes, from the kernel. *) | |
| 372 | +proc random*(b: []U8); | |
| 373 | +var n, r: I64; | |
| 374 | +begin | |
| 375 | + n := 0; | |
| 376 | + while n < LEN(b) do (* a signal may cut it short *) | |
| 377 | + r := SYSCALL(318, ADR(b[n]), LEN(b) - n, 0); (* getrandom *) | |
| 378 | + if r > 0 then INC(n, r); end; | |
| 379 | + end; | |
| 380 | +end; | |
| 381 | + | |
| 382 | +(* A socket of family and kind, connected to the address at a, of size | |
| 383 | + bytes, or, if serving, bound to it and taking connections: its file | |
| 338 | 384 | descriptor, or minus an errno. It is closed in a program exec |
| 339 | 385 | starts. *) |
| 340 | -proc connect*(path: []U8): I64; | |
| 341 | -var a: SockaddrUn; fd, r, i: I64; | |
| 386 | +proc socket(family, kind: I64; a: ^U8; size: I64; serving: BOOL): I64; | |
| 387 | +var fd, r, one: I64; | |
| 342 | 388 | begin |
| 343 | - if LEN(path) >= LEN(a.path) then return -36; end; (* ENAMETOOLONG *) | |
| 344 | - a.family := 1; (* AF_UNIX *) | |
| 345 | - for i := 0 to LEN(path) - 1 do a.path[i] := path[i]; end; | |
| 346 | - fd := SYSCALL(41, 1, 80001H, 0); (* socket: AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC *) | |
| 389 | + fd := SYSCALL(41, family, kind | 80000H, 0); (* socket, SOCK_CLOEXEC *) | |
| 347 | 390 | if fd < 0 then return fd; end; |
| 348 | - r := SYSCALL(42, fd, ADR(a), SIZE(SockaddrUn)); (* connect *) | |
| 391 | + if serving then | |
| 392 | + one := 1; (* so that a server can start again at once *) | |
| 393 | + SYSCALL(54, fd, 1, 2, ADR(one), 4); (* setsockopt: SOL_SOCKET, SO_REUSEADDR *) | |
| 394 | + r := SYSCALL(49, fd, a, size); (* bind *) | |
| 395 | + if r = 0 then r := SYSCALL(50, fd, 4096); end; (* listen *) | |
| 396 | + else | |
| 397 | + r := SYSCALL(42, fd, a, size); (* connect *) | |
| 398 | + end; | |
| 349 | 399 | if r < 0 then close(fd); return r; end; |
| 350 | 400 | return fd; |
| 351 | 401 | end; |
| 352 | 402 | |
| 403 | +(* path as a Unix socket's address in a, or false if it is too long. *) | |
| 404 | +proc unixAddr(path: []U8; a: ^SockaddrUn): BOOL; | |
| 405 | +var i: I64; | |
| 406 | +begin | |
| 407 | + if LEN(path) >= LEN(a^.path) then return false; end; | |
| 408 | + a^.family := 1; (* AF_UNIX *) | |
| 409 | + for i := 0 to LEN(path) - 1 do a^.path[i] := path[i]; end; | |
| 410 | + return true; | |
| 411 | +end; | |
| 412 | + | |
| 413 | +(* A stream socket connected to the Unix socket at path. *) | |
| 414 | +proc connect*(path: []U8): I64; | |
| 415 | +var a: SockaddrUn; | |
| 416 | +begin | |
| 417 | + if not unixAddr(path, ADR(a)) then return -36; end; (* ENAMETOOLONG *) | |
| 418 | + return socket(1, 1, ^U8(ADR(a)), SIZE(SockaddrUn), false); (* SOCK_STREAM *) | |
| 419 | +end; | |
| 420 | + | |
| 421 | +(* A stream socket made at path, which must not be there yet, taking | |
| 422 | + connections. *) | |
| 423 | +proc listen*(path: []U8): I64; | |
| 424 | +var a: SockaddrUn; | |
| 425 | +begin | |
| 426 | + if not unixAddr(path, ADR(a)) then return -36; end; | |
| 427 | + return socket(1, 1, ^U8(ADR(a)), SIZE(SockaddrUn), true); | |
| 428 | +end; | |
| 429 | + | |
| 430 | +(* The value of the hexadecimal digit c, or -1 if it isn't one. *) | |
| 431 | +proc hexDigit(c: U8): I64; | |
| 432 | +begin | |
| 433 | + if (c >= '0') and (c <= '9') then return I64(c - '0'); end; | |
| 434 | + if (c >= 'a') and (c <= 'f') then return I64(c - 'a') + 10; end; | |
| 435 | + if (c >= 'A') and (c <= 'F') then return I64(c - 'A') + 10; end; | |
| 436 | + return -1; | |
| 437 | +end; | |
| 438 | + | |
| 439 | +(* The IPv4 address s, four numbers and dots, in out's 4 bytes, or false | |
| 440 | + if it isn't one. *) | |
| 441 | +proc ipv4(s, out: []U8): BOOL; | |
| 442 | +var b: [4]U8; n, i, v, d: I64; | |
| 443 | +begin | |
| 444 | + i := 0; | |
| 445 | + for n := 0 to 3 do | |
| 446 | + if n > 0 then | |
| 447 | + if (i = LEN(s)) or (s[i] # '.') then return false; end; | |
| 448 | + INC(i); | |
| 449 | + end; | |
| 450 | + v := 0; | |
| 451 | + d := 0; | |
| 452 | + while (i < LEN(s)) and (d < 3) and (s[i] >= '0') and (s[i] <= '9') do | |
| 453 | + v := v * 10 + I64(s[i] - '0'); | |
| 454 | + INC(i); | |
| 455 | + INC(d); | |
| 456 | + end; | |
| 457 | + if (d = 0) or (v > 255) then return false; end; | |
| 458 | + b[n] := U8(v); | |
| 459 | + end; | |
| 460 | + if i # LEN(s) then return false; end; | |
| 461 | + for i := 0 to 3 do out[i] := b[i]; end; | |
| 462 | + return true; | |
| 463 | +end; | |
| 464 | + | |
| 465 | +(* The IPv6 address s, groups of hexadecimal digits and colons, one :: | |
| 466 | + standing for groups of zeros, in out's 16 bytes, or false if it isn't | |
| 467 | + one. *) | |
| 468 | +proc ipv6(s, out: []U8): BOOL; | |
| 469 | +var g: [8]I64; n, gap, i, v, d, k: I64; | |
| 470 | +begin | |
| 471 | + n := 0; | |
| 472 | + gap := -1; (* the group the :: is before *) | |
| 473 | + i := 0; | |
| 474 | + if (LEN(s) >= 2) and (s[0..2] = "::") then gap := 0; i := 2; end; | |
| 475 | + while i < LEN(s) do | |
| 476 | + if n = 8 then return false; end; | |
| 477 | + v := 0; | |
| 478 | + d := 0; | |
| 479 | + while (i < LEN(s)) and (d < 5) and (hexDigit(s[i]) >= 0) do | |
| 480 | + v := v * 16 + hexDigit(s[i]); | |
| 481 | + INC(i); | |
| 482 | + INC(d); | |
| 483 | + end; | |
| 484 | + if (d = 0) or (d > 4) then return false; end; | |
| 485 | + g[n] := v; | |
| 486 | + INC(n); | |
| 487 | + if i < LEN(s) then | |
| 488 | + if s[i] # ':' then return false; end; | |
| 489 | + INC(i); | |
| 490 | + if i = LEN(s) then return false; end; | |
| 491 | + if s[i] = ':' then | |
| 492 | + if gap >= 0 then return false; end; | |
| 493 | + gap := n; | |
| 494 | + INC(i); | |
| 495 | + end; | |
| 496 | + end; | |
| 497 | + end; | |
| 498 | + if ((gap < 0) and (n < 8)) or ((gap >= 0) and (n = 8)) then return false; end; | |
| 499 | + for i := 0 to 15 do out[i] := 0; end; | |
| 500 | + for i := 0 to n - 1 do | |
| 501 | + k := i; | |
| 502 | + if (gap >= 0) and (i >= gap) then k := i + 8 - n; end; (* after the zeros *) | |
| 503 | + out[2 * k] := U8(g[i] shr 8); | |
| 504 | + out[2 * k + 1] := U8(g[i] & 255); | |
| 505 | + end; | |
| 506 | + return true; | |
| 507 | +end; | |
| 508 | + | |
| 509 | +(* addr, an IPv4 or IPv6 address as numbers, and port, in a: the size | |
| 510 | + of its struct sockaddr_in or sockaddr_in6, or 0 if either is | |
| 511 | + wrong. *) | |
| 512 | +proc inetAddr(addr: []U8; port: I64; a: ^Sockaddr): I64; | |
| 513 | +begin | |
| 514 | + if (port < 0) or (port > 65535) then return 0; end; | |
| 515 | + a^.port[0] := U8(port shr 8); (* in the network's order, the high byte first *) | |
| 516 | + a^.port[1] := U8(port & 255); | |
| 517 | + if ipv4(addr, a^.data[0..4]) then a^.family := 2; return 16; end; (* AF_INET *) | |
| 518 | + if ipv6(addr, a^.data[4..20]) then a^.family := 10; return 28; end; (* AF_INET6 *) | |
| 519 | + return 0; | |
| 520 | +end; | |
| 521 | + | |
| 522 | +(* A socket of kind at addr and port, as socket makes it; -22 (EINVAL) | |
| 523 | + if they are wrong. *) | |
| 524 | +proc inet(addr: []U8; port, kind: I64; serving: BOOL): I64; | |
| 525 | +var a: Sockaddr; size: I64; | |
| 526 | +begin | |
| 527 | + size := inetAddr(addr, port, ADR(a)); | |
| 528 | + if size = 0 then return -22; end; | |
| 529 | + return socket(I64(a.family), kind, ^U8(ADR(a)), size, serving); | |
| 530 | +end; | |
| 531 | + | |
| 532 | +(* A stream socket connected to port at addr, an IPv4 or IPv6 address | |
| 533 | + as numbers. *) | |
| 534 | +proc connectTcp*(addr: []U8; port: I64): I64; | |
| 535 | +begin | |
| 536 | + return inet(addr, port, 1, false); (* SOCK_STREAM *) | |
| 537 | +end; | |
| 538 | + | |
| 539 | +(* A socket of datagrams to and from port at addr. *) | |
| 540 | +proc connectUdp*(addr: []U8; port: I64): I64; | |
| 541 | +begin | |
| 542 | + return inet(addr, port, 2, false); (* SOCK_DGRAM *) | |
| 543 | +end; | |
| 544 | + | |
| 545 | +(* A stream socket taking connections on port at addr; "0.0.0.0" or | |
| 546 | + "::" is every address the machine has. *) | |
| 547 | +proc listenTcp*(addr: []U8; port: I64): I64; | |
| 548 | +begin | |
| 549 | + return inet(addr, port, 1, true); | |
| 550 | +end; | |
| 551 | + | |
| 552 | +(* The next connection to the socket fd, waiting for one: a socket of | |
| 553 | + its own, closed in a program exec starts; or minus an errno. *) | |
| 554 | +proc accept*(fd: I64): I64; | |
| 555 | +begin | |
| 556 | + return SYSCALL(288, fd, 0, 0, 80000H); (* accept4, SOCK_CLOEXEC *) | |
| 557 | +end; | |
| 558 | + | |
| 353 | 559 | (* Writes s to the socket fd, as write does, but returns -32 (EPIPE) |
| 354 | 560 | where write would stop the program with SIGPIPE: when the other end |
| 355 | 561 | has closed it. *) |
| @@ -672,21 +878,6 @@ | ||
| 672 | 878 | SYSCALL(231, status); |
| 673 | 879 | end; |
| 674 | 880 | |
| 675 | -(* A socket listening at path, for the test: its fd. *) | |
| 676 | -proc listen(path: []U8): I64; | |
| 677 | -var a: SockaddrUn; fd, i: I64; | |
| 678 | -begin | |
| 679 | - remove(path); | |
| 680 | - a.family := 1; | |
| 681 | - for i := 0 to LEN(path) - 1 do a.path[i] := path[i]; end; | |
| 682 | - fd := SYSCALL(41, 1, 1, 0); (* socket *) | |
| 683 | - if (fd < 0) or (SYSCALL(49, fd, ADR(a), SIZE(SockaddrUn)) # 0) (* bind *) | |
| 684 | - or (SYSCALL(50, fd, 1) # 0) then (* listen *) | |
| 685 | - exit(2); | |
| 686 | - end; | |
| 687 | - return fd; | |
| 688 | -end; | |
| 689 | - | |
| 690 | 881 | (* The test of programs: one on a pseudo-terminal of 100 by 30 prints |
| 691 | 882 | its arguments, a variable put in place and its size, and leads the |
| 692 | 883 | terminal's foreground until it ends with status 3; one in a |
| @@ -762,9 +953,50 @@ | ||
| 762 | 953 | if ignore(sigInt, false) # 0 then exit(2); end; |
| 763 | 954 | end; |
| 764 | 955 | |
| 956 | +(* The test of time, random bytes and the network: a sleep the clock | |
| 957 | + sees; addresses read and refused; a TCP connection on the loopback; | |
| 958 | + and a datagram to a port where no one listens, refused. *) | |
| 959 | +proc testNet; | |
| 960 | +var t, l, c, a, u: I64; x, y: [16]U8; s, s6: Sockaddr; | |
| 961 | +begin | |
| 962 | + t := clock(); | |
| 963 | + sleep(20); | |
| 964 | + t := clock() - t; | |
| 965 | + if (t < 20000000) or (t > 2000000000) or (now() < 1700000000 * 1000000000) then exit(2); end; | |
| 966 | + random(x); | |
| 967 | + random(y); | |
| 968 | + if x[0..16] = y[0..16] then exit(2); end; | |
| 969 | + if (inetAddr("10.0.0.255", 80, ADR(s)) # 16) or (s.port[0] # 0) or (s.port[1] # 80) | |
| 970 | + or (s.data[0] # 10) or (s.data[2] # 0) or (s.data[3] # 255) then | |
| 971 | + exit(2); | |
| 972 | + end; | |
| 973 | + if (inetAddr("1:aBcd::ff", 258, ADR(s6)) # 28) or (s6.port[0] # 1) or (s6.port[1] # 2) | |
| 974 | + or (s6.data[0] # 0) or (s6.data[5] # 1) or (s6.data[6] # 0ABH) or (s6.data[7] # 0CDH) | |
| 975 | + or (s6.data[18] # 0) or (s6.data[19] # 0FFH) then | |
| 976 | + exit(2); | |
| 977 | + end; | |
| 978 | + if (inetAddr("::", 0, ADR(s6)) # 28) or (inetAddr("1:2:3:4:5:6:7:8", 0, ADR(s6)) # 28) then exit(2); end; | |
| 979 | + if (connectTcp("1.2.3", 80) # -22) or (connectTcp("1.2.3.256", 80) # -22) or (connectTcp("1::2::3", 80) # -22) | |
| 980 | + or (connectTcp("1:2:3:4:5:6:7:8:9", 80) # -22) or (connectTcp("1:", 80) # -22) | |
| 981 | + or (connectTcp("12345::", 80) # -22) or (connectTcp("::1", 65536) # -22) then | |
| 982 | + exit(2); | |
| 983 | + end; | |
| 984 | + l := listenTcp("127.0.0.1", 47391); | |
| 985 | + c := connectTcp("127.0.0.1", 47391); | |
| 986 | + if (l < 0) or (c < 0) then exit(2); end; | |
| 987 | + a := accept(l); | |
| 988 | + if (a < 0) or (send(c, "hi") # 2) or (read(a, x) # 2) or (x[0..2] # "hi") then exit(2); end; | |
| 989 | + close(a); | |
| 990 | + close(c); | |
| 991 | + close(l); | |
| 992 | + u := connectUdp("127.0.0.1", 47391); | |
| 993 | + if (u < 0) or (send(u, "x") # 1) or not ready(u, 1000) or (read(u, x) # -111) then exit(2); end; (* ECONNREFUSED *) | |
| 994 | + close(u); | |
| 995 | +end; | |
| 996 | + | |
| 765 | 997 | (* The module's test: a file written, read back and closed; a |
| 766 | 998 | directory made, listed and emptied; no terminal; a socket, sent to |
| 767 | - after its end. *) | |
| 999 | + after its end; programs; time and the network. *) | |
| 768 | 1000 | proc main*; |
| 769 | 1001 | var fd, n, err, w, h, l, c, a: I64; buf: [16]U8; p: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL; |
| 770 | 1002 | begin |
| @@ -848,12 +1080,14 @@ | ||
| 848 | 1080 | (* a socket: nothing at a path, then a listener that a connection |
| 849 | 1081 | makes ready, and a byte from it to the connection *) |
| 850 | 1082 | if connect("/tmp/raz-no-such-socket") # -2 then exit(2); end; (* ENOENT *) |
| 1083 | + remove("/tmp/raz-sys-socket"); | |
| 851 | 1084 | l := listen("/tmp/raz-sys-socket"); |
| 1085 | + if (l < 0) or (listen("/tmp/raz-sys-socket") # -98) then exit(2); end; (* EADDRINUSE: there already *) | |
| 852 | 1086 | c := connect("/tmp/raz-sys-socket"); |
| 853 | 1087 | if c < 0 then exit(2); end; |
| 854 | 1088 | if readyOf([c], 0) # -1 then exit(2); end; |
| 855 | 1089 | if readyOf([c, l], 1000) # l then exit(2); end; |
| 856 | - a := SYSCALL(43, l, 0, 0); (* accept *) | |
| 1090 | + a := accept(l); | |
| 857 | 1091 | if (a < 0) or (send(a, "x") # 1) or (readyOf([l, c], 1000) # c) then exit(2); end; |
| 858 | 1092 | if (read(c, buf) # 1) or (buf[0] # 'x') then exit(2); end; |
| 859 | 1093 | close(a); |
| @@ -863,6 +1097,7 @@ | ||
| 863 | 1097 | close(l); |
| 864 | 1098 | remove("/tmp/raz-sys-socket"); |
| 865 | 1099 | testPrograms; |
| 1100 | + testNet; | |
| 866 | 1101 | end; |
| 867 | 1102 | |
| 868 | 1103 | end Sys. |
| @@ -5,7 +5,8 @@ | ||
| 5 | 5 | It keeps to POSIX's names, and raw mode is cfmakeraw's. What is |
| 6 | 6 | glibc's, on x86-64: where a name lies in struct dirent; struct stat; |
| 7 | 7 | open's flags; the numbers of the requests TIOCGWINSZ, TIOCSWINSZ and |
| 8 | - TIOCSCTTY; close_range; and struct sockaddr_un, which on the BSDs starts with a | |
| 8 | + TIOCSCTTY; close_range; SOCK_CLOEXEC, AF_INET6 and SO_REUSEADDR; and | |
| 9 | + struct sockaddr_un and sockaddr_in, which on the BSDs start with a | |
| 9 | 10 | byte of length. A C library that differs needs a Sys of its own. *) |
| 10 | 11 | module Sys; |
| 11 | 12 | import C; |
| @@ -76,6 +77,14 @@ | ||
| 76 | 77 | family: U16; |
| 77 | 78 | path: [108]U8; |
| 78 | 79 | end; |
| 80 | + (* struct sockaddr_in or sockaddr_in6: AF_INET and the IPv4 address | |
| 81 | + in data[0..4], or AF_INET6 and the IPv6 one in data[4..20] *) | |
| 82 | + Sockaddr = record | |
| 83 | + family: U16; | |
| 84 | + port: [2]U8; | |
| 85 | + data: [24]U8; | |
| 86 | + end; | |
| 87 | + Timespec = record sec, nsec: I64; end; | |
| 79 | 88 | |
| 80 | 89 | var |
| 81 | 90 | cwd: [4096]U8; (* what workDir gives *) |
| @@ -348,17 +357,65 @@ | ||
| 348 | 357 | return r > 0; |
| 349 | 358 | end; |
| 350 | 359 | |
| 351 | -(* A stream socket connected to the Unix socket at path: its file | |
| 352 | - descriptor, or minus an errno. *) | |
| 353 | -proc connect*(path: []U8): I64; | |
| 354 | -var a: SockaddrUn; fd: I32; i, err: I64; | |
| 360 | +(* The time on clock, in nanoseconds. *) | |
| 361 | +proc time(clock: I32): I64; | |
| 362 | +var t: Timespec; | |
| 363 | +begin | |
| 364 | + C.clock_gettime(clock, ^U8(ADR(t))); | |
| 365 | + return t.sec * 1000000000 + t.nsec; | |
| 366 | +end; | |
| 367 | + | |
| 368 | +(* The clock that only goes forward, in nanoseconds: for how long | |
| 369 | + something takes. *) | |
| 370 | +proc clock*: I64; | |
| 371 | +begin | |
| 372 | + return time(1); (* CLOCK_MONOTONIC *) | |
| 373 | +end; | |
| 374 | + | |
| 375 | +(* The time in nanoseconds since 1970-01-01, UTC. *) | |
| 376 | +proc now*: I64; | |
| 377 | +begin | |
| 378 | + return time(0); (* CLOCK_REALTIME *) | |
| 379 | +end; | |
| 380 | + | |
| 381 | +(* Waits ms milliseconds. *) | |
| 382 | +proc sleep*(ms: I64); | |
| 383 | +var t: Timespec; at: I64; | |
| 384 | +begin | |
| 385 | + at := clock() + ms * 1000000; | |
| 386 | + t := {sec: at div 1000000000, nsec: at mod 1000000000}; | |
| 387 | + repeat until C.clock_nanosleep(1, 1, ^U8(ADR(t)), nil) # 4; (* until t, again after EINTR *) | |
| 388 | +end; | |
| 389 | + | |
| 390 | +(* Fills b with random bytes, from the kernel. *) | |
| 391 | +proc random*(b: []U8); | |
| 392 | +var n, r: I64; | |
| 355 | 393 | begin |
| 356 | - if LEN(path) >= LEN(a.path) then return -36; end; (* ENAMETOOLONG *) | |
| 357 | - a.family := 1; (* AF_UNIX *) | |
| 358 | - for i := 0 to LEN(path) - 1 do a.path[i] := path[i]; end; | |
| 359 | - fd := C.socket(1, 1, 0); (* AF_UNIX, SOCK_STREAM *) | |
| 394 | + n := 0; | |
| 395 | + while n < LEN(b) do (* a signal may cut it short *) | |
| 396 | + r := C.getrandom(ADR(b[n]), U64(LEN(b) - n), 0); | |
| 397 | + if r > 0 then INC(n, r); end; | |
| 398 | + end; | |
| 399 | +end; | |
| 400 | + | |
| 401 | +(* A socket of family and kind, connected to the address at a, of size | |
| 402 | + bytes, or, if serving, bound to it and taking connections: its file | |
| 403 | + descriptor, or minus an errno. It is closed in a program exec | |
| 404 | + starts. *) | |
| 405 | +proc socket(family, kind: I64; a: ^U8; size: I64; serving: BOOL): I64; | |
| 406 | +var fd, r, one: I32; err: I64; | |
| 407 | +begin | |
| 408 | + fd := C.socket(I32(family), I32(kind) | 80000H, 0); (* SOCK_CLOEXEC *) | |
| 360 | 409 | if fd < 0 then return failed(); end; |
| 361 | - if C.connect(fd, ^U8(ADR(a)), U32(SIZE(SockaddrUn))) # 0 then | |
| 410 | + if serving then | |
| 411 | + one := 1; (* so that a server can start again at once *) | |
| 412 | + C.setsockopt(fd, 1, 2, ^U8(ADR(one)), 4); (* SOL_SOCKET, SO_REUSEADDR *) | |
| 413 | + r := C.bind(fd, a, U32(size)); | |
| 414 | + if r = 0 then r := C.listen(fd, 4096); end; | |
| 415 | + else | |
| 416 | + r := C.connect(fd, a, U32(size)); | |
| 417 | + end; | |
| 418 | + if r # 0 then | |
| 362 | 419 | err := failed(); (* before close can change errno *) |
| 363 | 420 | C.close(fd); |
| 364 | 421 | return err; |
| @@ -366,6 +423,87 @@ | ||
| 366 | 423 | return I64(fd); |
| 367 | 424 | end; |
| 368 | 425 | |
| 426 | +(* path as a Unix socket's address in a, or false if it is too long. *) | |
| 427 | +proc unixAddr(path: []U8; a: ^SockaddrUn): BOOL; | |
| 428 | +var i: I64; | |
| 429 | +begin | |
| 430 | + if LEN(path) >= LEN(a^.path) then return false; end; | |
| 431 | + a^.family := 1; (* AF_UNIX *) | |
| 432 | + for i := 0 to LEN(path) - 1 do a^.path[i] := path[i]; end; | |
| 433 | + return true; | |
| 434 | +end; | |
| 435 | + | |
| 436 | +(* A stream socket connected to the Unix socket at path. *) | |
| 437 | +proc connect*(path: []U8): I64; | |
| 438 | +var a: SockaddrUn; | |
| 439 | +begin | |
| 440 | + if not unixAddr(path, ADR(a)) then return -36; end; (* ENAMETOOLONG *) | |
| 441 | + return socket(1, 1, ^U8(ADR(a)), SIZE(SockaddrUn), false); (* SOCK_STREAM *) | |
| 442 | +end; | |
| 443 | + | |
| 444 | +(* A stream socket made at path, which must not be there yet, taking | |
| 445 | + connections. *) | |
| 446 | +proc listen*(path: []U8): I64; | |
| 447 | +var a: SockaddrUn; | |
| 448 | +begin | |
| 449 | + if not unixAddr(path, ADR(a)) then return -36; end; | |
| 450 | + return socket(1, 1, ^U8(ADR(a)), SIZE(SockaddrUn), true); | |
| 451 | +end; | |
| 452 | + | |
| 453 | +(* addr, an IPv4 or IPv6 address as numbers, and port, in a: the size | |
| 454 | + of its struct sockaddr_in or sockaddr_in6, or 0 if either is | |
| 455 | + wrong. *) | |
| 456 | +proc inetAddr(addr: []U8; port: I64; a: ^Sockaddr): I64; | |
| 457 | +var s: [64]U8; | |
| 458 | +begin | |
| 459 | + if (port < 0) or (port > 65535) or not cstr(addr, s) then return 0; end; | |
| 460 | + a^.port[0] := U8(port shr 8); (* in the network's order, the high byte first *) | |
| 461 | + a^.port[1] := U8(port & 255); | |
| 462 | + if C.inet_pton(2, ADR(s[0]), ADR(a^.data[0])) = 1 then a^.family := 2; return 16; end; (* AF_INET *) | |
| 463 | + if C.inet_pton(10, ADR(s[0]), ADR(a^.data[4])) = 1 then a^.family := 10; return 28; end; (* AF_INET6 *) | |
| 464 | + return 0; | |
| 465 | +end; | |
| 466 | + | |
| 467 | +(* A socket of kind at addr and port, as socket makes it; -22 (EINVAL) | |
| 468 | + if they are wrong. *) | |
| 469 | +proc inet(addr: []U8; port, kind: I64; serving: BOOL): I64; | |
| 470 | +var a: Sockaddr; size: I64; | |
| 471 | +begin | |
| 472 | + size := inetAddr(addr, port, ADR(a)); | |
| 473 | + if size = 0 then return -22; end; | |
| 474 | + return socket(I64(a.family), kind, ^U8(ADR(a)), size, serving); | |
| 475 | +end; | |
| 476 | + | |
| 477 | +(* A stream socket connected to port at addr, an IPv4 or IPv6 address | |
| 478 | + as numbers. *) | |
| 479 | +proc connectTcp*(addr: []U8; port: I64): I64; | |
| 480 | +begin | |
| 481 | + return inet(addr, port, 1, false); (* SOCK_STREAM *) | |
| 482 | +end; | |
| 483 | + | |
| 484 | +(* A socket of datagrams to and from port at addr. *) | |
| 485 | +proc connectUdp*(addr: []U8; port: I64): I64; | |
| 486 | +begin | |
| 487 | + return inet(addr, port, 2, false); (* SOCK_DGRAM *) | |
| 488 | +end; | |
| 489 | + | |
| 490 | +(* A stream socket taking connections on port at addr; "0.0.0.0" or | |
| 491 | + "::" is every address the machine has. *) | |
| 492 | +proc listenTcp*(addr: []U8; port: I64): I64; | |
| 493 | +begin | |
| 494 | + return inet(addr, port, 1, true); | |
| 495 | +end; | |
| 496 | + | |
| 497 | +(* The next connection to the socket fd, waiting for one: a socket of | |
| 498 | + its own, closed in a program exec starts; or minus an errno. *) | |
| 499 | +proc accept*(fd: I64): I64; | |
| 500 | +var r: I32; | |
| 501 | +begin | |
| 502 | + r := C.accept4(I32(fd), nil, nil, 80000H); (* SOCK_CLOEXEC *) | |
| 503 | + if r < 0 then return failed(); end; | |
| 504 | + return I64(r); | |
| 505 | +end; | |
| 506 | + | |
| 369 | 507 | (* Writes s to the socket fd, as write does, but returns -32 (EPIPE) |
| 370 | 508 | where write would stop the program with SIGPIPE: when the other end |
| 371 | 509 | has closed it. *) |
| @@ -703,20 +841,6 @@ | ||
| 703 | 841 | C._exit(I32(status)); |
| 704 | 842 | end; |
| 705 | 843 | |
| 706 | -(* A socket listening at path, for the test: its fd. *) | |
| 707 | -proc listen(path: []U8): I64; | |
| 708 | -var a: SockaddrUn; fd: I32; i: I64; | |
| 709 | -begin | |
| 710 | - remove(path); | |
| 711 | - a.family := 1; | |
| 712 | - for i := 0 to LEN(path) - 1 do a.path[i] := path[i]; end; | |
| 713 | - fd := C.socket(1, 1, 0); | |
| 714 | - if (fd < 0) or (C.bind(fd, ^U8(ADR(a)), U32(SIZE(SockaddrUn))) # 0) or (C.listen(fd, 1) # 0) then | |
| 715 | - exit(2); | |
| 716 | - end; | |
| 717 | - return I64(fd); | |
| 718 | -end; | |
| 719 | - | |
| 720 | 844 | (* The test of programs: one on a pseudo-terminal of 100 by 30 prints |
| 721 | 845 | its arguments, a variable put in place and its size, and leads the |
| 722 | 846 | terminal's foreground until it ends with status 3; one in a |
| @@ -792,9 +916,50 @@ | ||
| 792 | 916 | if ignore(sigInt, false) # 0 then exit(2); end; |
| 793 | 917 | end; |
| 794 | 918 | |
| 919 | +(* The test of time, random bytes and the network: a sleep the clock | |
| 920 | + sees; addresses read and refused; a TCP connection on the loopback; | |
| 921 | + and a datagram to a port where no one listens, refused. *) | |
| 922 | +proc testNet; | |
| 923 | +var t, l, c, a, u: I64; x, y: [16]U8; s, s6: Sockaddr; | |
| 924 | +begin | |
| 925 | + t := clock(); | |
| 926 | + sleep(20); | |
| 927 | + t := clock() - t; | |
| 928 | + if (t < 20000000) or (t > 2000000000) or (now() < 1700000000 * 1000000000) then exit(2); end; | |
| 929 | + random(x); | |
| 930 | + random(y); | |
| 931 | + if x[0..16] = y[0..16] then exit(2); end; | |
| 932 | + if (inetAddr("10.0.0.255", 80, ADR(s)) # 16) or (s.port[0] # 0) or (s.port[1] # 80) | |
| 933 | + or (s.data[0] # 10) or (s.data[2] # 0) or (s.data[3] # 255) then | |
| 934 | + exit(2); | |
| 935 | + end; | |
| 936 | + if (inetAddr("1:aBcd::ff", 258, ADR(s6)) # 28) or (s6.port[0] # 1) or (s6.port[1] # 2) | |
| 937 | + or (s6.data[0] # 0) or (s6.data[5] # 1) or (s6.data[6] # 0ABH) or (s6.data[7] # 0CDH) | |
| 938 | + or (s6.data[18] # 0) or (s6.data[19] # 0FFH) then | |
| 939 | + exit(2); | |
| 940 | + end; | |
| 941 | + if (inetAddr("::", 0, ADR(s6)) # 28) or (inetAddr("1:2:3:4:5:6:7:8", 0, ADR(s6)) # 28) then exit(2); end; | |
| 942 | + if (connectTcp("1.2.3", 80) # -22) or (connectTcp("1.2.3.256", 80) # -22) or (connectTcp("1::2::3", 80) # -22) | |
| 943 | + or (connectTcp("1:2:3:4:5:6:7:8:9", 80) # -22) or (connectTcp("1:", 80) # -22) | |
| 944 | + or (connectTcp("12345::", 80) # -22) or (connectTcp("::1", 65536) # -22) then | |
| 945 | + exit(2); | |
| 946 | + end; | |
| 947 | + l := listenTcp("127.0.0.1", 47391); | |
| 948 | + c := connectTcp("127.0.0.1", 47391); | |
| 949 | + if (l < 0) or (c < 0) then exit(2); end; | |
| 950 | + a := accept(l); | |
| 951 | + if (a < 0) or (send(c, "hi") # 2) or (read(a, x) # 2) or (x[0..2] # "hi") then exit(2); end; | |
| 952 | + close(a); | |
| 953 | + close(c); | |
| 954 | + close(l); | |
| 955 | + u := connectUdp("127.0.0.1", 47391); | |
| 956 | + if (u < 0) or (send(u, "x") # 1) or not ready(u, 1000) or (read(u, x) # -111) then exit(2); end; (* ECONNREFUSED *) | |
| 957 | + close(u); | |
| 958 | +end; | |
| 959 | + | |
| 795 | 960 | (* The module's test, as Linux's Sys has it: a file written, read back |
| 796 | 961 | and closed; a directory made, listed and emptied; no terminal; a |
| 797 | - socket, sent to after its end. *) | |
| 962 | + socket, sent to after its end; programs; time and the network. *) | |
| 798 | 963 | proc main*; |
| 799 | 964 | var fd, n, err, w, h, l, c, a: I64; buf: [16]U8; p: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL; |
| 800 | 965 | begin |
| @@ -878,12 +1043,14 @@ | ||
| 878 | 1043 | (* a socket: nothing at a path, then a listener that a connection |
| 879 | 1044 | makes ready, and a byte from it to the connection *) |
| 880 | 1045 | if connect("/tmp/raz-no-such-socket") # -2 then exit(2); end; (* ENOENT *) |
| 1046 | + remove("/tmp/raz-sys-socket"); | |
| 881 | 1047 | l := listen("/tmp/raz-sys-socket"); |
| 1048 | + if (l < 0) or (listen("/tmp/raz-sys-socket") # -98) then exit(2); end; (* EADDRINUSE: there already *) | |
| 882 | 1049 | c := connect("/tmp/raz-sys-socket"); |
| 883 | 1050 | if c < 0 then exit(2); end; |
| 884 | 1051 | if readyOf([c], 0) # -1 then exit(2); end; |
| 885 | 1052 | if readyOf([c, l], 1000) # l then exit(2); end; |
| 886 | - a := I64(C.accept(I32(l), nil, nil)); | |
| 1053 | + a := accept(l); | |
| 887 | 1054 | if (a < 0) or (send(a, "x") # 1) or (readyOf([l, c], 1000) # c) then exit(2); end; |
| 888 | 1055 | if (read(c, buf) # 1) or (buf[0] # 'x') then exit(2); end; |
| 889 | 1056 | close(a); |
| @@ -893,6 +1060,7 @@ | ||
| 893 | 1060 | close(l); |
| 894 | 1061 | remove("/tmp/raz-sys-socket"); |
| 895 | 1062 | testPrograms; |
| 1063 | + testNet; | |
| 896 | 1064 | end; |
| 897 | 1065 | |
| 898 | 1066 | end Sys. |
| @@ -1,5 +1,5 @@ | ||
| 1 | -lib/Sys.raz:11:8: error: module C is in test/err/mods/clash/C.raz and lib/C.raz | |
| 2 | - 11 | import C; | |
| 1 | +lib/Sys.raz:12:8: error: module C is in test/err/mods/clash/C.raz and lib/C.raz | |
| 2 | + 12 | import C; | |
| 3 | 3 | | ^ |
| 4 | 4 | help: a program has one module of each name; a symbolic link to one of these files chooses it |
| 5 | 5 | raz: 1 error |