setsockopt was `return 0;` and getsockopt wrote a zero for whatever was asked. The comment said it was so a server setting SO_REUSEADDR and treating failure as fatal would not fail. That trade is the wrong way round: a caller told "no" adapts, a caller told "yes" changes its behaviour on the strength of it -- sets SO_RCVTIMEO and then blocks forever in a read it believes will time out, sets TCP_NODELAY and reports latency numbers that measure Nagle. The failure is silent, remote from the call, and indistinguishable from a bug in the stack. Six options are honoured now and everything else is ENOPROTOOPT. SO_REUSEADDR, SO_KEEPALIVE, SO_BROADCAST and TCP_NODELAY reach lwIP's pcb; SO_SNDBUF/SO_RCVBUF are accepted and report the real size on read. SO_REUSEADDR is implemented rather than refused precisely because of the original comment's concern -- it is the one option a server really does set and really does check. accept4 becomes its own table entry rather than an alias for accept. It has to be: accept takes three arguments, so a shared handler reading a fourth register reads whatever the caller last left there and invents a SOCK_NONBLOCK out of stack litter. SOCK_CLOEXEC and SOCK_NONBLOCK are now honoured by socket(), socketpair() and accept4() -- a dropped CLOEXEC leaks a listening socket across every exec, and a dropped NONBLOCK has the caller block forever in what it believes is a poll loop. ICMP ping sockets: socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP), on lwIP's raw API, with the callback stripping the variable-length IP header. Only echo replies are eaten, and that restriction is load-bearing -- raw_input runs before lwIP's own icmp_input, so consuming every ICMP message would swallow the echo *requests* and this machine would silently stop answering pings from outside. Loopback, so 127.0.0.1 is a real destination. LWIP_NETIF_LOOPBACK is the half that is easy to miss: without it a packet addressed to an interface's own address goes out of the wire and never comes back. With NO_SYS=1 nothing drains the loopback queue, so the network thread calls netif_poll_all(). cmd/ping is first-party rather than toybox's, and the reason is a limitation not a preference: toybox reads replies with recvmsg(2) and wants the TTL through an IP_RECVTTL control message, and SchOS has neither. recvmsg is item 1 of docs/plan/foundations.md, where it is wanted for SCM_RIGHTS anyway, and should not be rushed to make one applet run. Two bugs found on the way. Every AF_INET socket failed with ENFILE until DHCP bound, because inet_new() demanded an *address* before handing out a socket and reported it as "file table overflow"; net_is_ready() (lwIP up) is now distinct from net_is_up() (DHCP bound). And that bug was hiding a bad test: socktest asserted "AF_INET is refused, not faked", true when written, false since AF_INET landed, and green the whole time because the suite runs before DHCP binds and the ENFILE made the refusal look real. Fixing the kernel is what turned it red. Verified: ping to 127.0.0.1, localhost, the gateway, 1.1.1.1 and 9.9.9.9, all 0% loss; socktest ALL CHECKS PASSED with ten new checks, including a setsockopt round trip and an unknown option that must be refused -- a permissive stub cannot pass both at once. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VQ7Qx8E1q1M1bbCq8QLHS2
133 lines
3.9 KiB
C
133 lines
3.9 KiB
C
#pragma once
|
|
|
|
#include "types.h"
|
|
|
|
// Unix domain sockets.
|
|
//
|
|
// These exist for Schcom. A compositor and its clients have to exchange
|
|
// messages, and a pipe cannot do it: a pipe is one-directional, has no
|
|
// notion of a connection, and gives no way for a server to be *found* by a
|
|
// client that starts later. A socket with a name in the filesystem is
|
|
// exactly the missing piece.
|
|
//
|
|
// AF_INET arrived later, and exactly as planned: underneath the same syscalls
|
|
// rather than reshaping them. A socket carries its domain and every entry point
|
|
// dispatches on it -- kernel/net/unix.c for AF_UNIX, kernel/net/inet.c (lwIP)
|
|
// for AF_INET.
|
|
|
|
#define AF_UNIX 1
|
|
#define AF_LOCAL 1
|
|
#define AF_INET 2
|
|
|
|
#define SOCK_STREAM 1
|
|
#define SOCK_DGRAM 2
|
|
|
|
// Flags ORed into socket()'s and accept4()'s `type`/`flags` argument. Linux's
|
|
// values, because a musl program passes Linux's constants.
|
|
#define SOCK_NONBLOCK 0x800 // O_NONBLOCK
|
|
#define SOCK_CLOEXEC 0x80000 // O_CLOEXEC
|
|
|
|
// setsockopt/getsockopt levels and names, Linux's numbering.
|
|
//
|
|
// Only what is listed here is accepted; anything else is answered with
|
|
// ENOPROTOOPT rather than a success it did not earn. See the comment above
|
|
// sys_setsockopt for why that is worth the risk of failing a caller that never
|
|
// checked before.
|
|
#define SOL_SOCKET 1
|
|
#define IPPROTO_ICMP 1
|
|
#define IPPROTO_TCP 6
|
|
|
|
#define SO_REUSEADDR 2
|
|
#define SO_TYPE 3
|
|
#define SO_ERROR 4
|
|
#define SO_BROADCAST 6
|
|
#define SO_SNDBUF 7
|
|
#define SO_RCVBUF 8
|
|
#define SO_KEEPALIVE 9
|
|
#define SO_ACCEPTCONN 30
|
|
|
|
#define TCP_NODELAY 1
|
|
|
|
#define UNIX_PATH_MAX 108
|
|
|
|
#define SHUT_RD 0
|
|
#define SHUT_WR 1
|
|
#define SHUT_RDWR 2
|
|
|
|
struct sockaddr_un {
|
|
uint16 sun_family;
|
|
char sun_path[UNIX_PATH_MAX];
|
|
};
|
|
|
|
// The generic form syscalls take. Same shape as Linux's, so a musl-linked
|
|
// program's struct lines up byte for byte.
|
|
struct sockaddr {
|
|
uint16 sa_family;
|
|
char sa_data[UNIX_PATH_MAX];
|
|
};
|
|
|
|
// The AF_INET address. Same layout as Linux's, so a musl-linked program's
|
|
// struct lines up byte for byte. sin_port and sin_addr are network byte order
|
|
// and stay that way: lwIP wants them so, and converting on the way in and out
|
|
// again would be two swaps that cancel.
|
|
struct sockaddr_in {
|
|
uint16 sin_family;
|
|
uint16 sin_port;
|
|
uint32 sin_addr;
|
|
char sin_zero[8];
|
|
};
|
|
|
|
#ifdef _KERNEL_SOCKET_INTERNALS
|
|
|
|
#define NSOCK 32 // sockets in the system
|
|
#define SOCKBUF 4096 // receive buffer per connected socket
|
|
#define NBACKLOG 8
|
|
|
|
enum sockstate {
|
|
SS_FREE = 0,
|
|
SS_UNBOUND,
|
|
SS_BOUND,
|
|
SS_LISTENING,
|
|
SS_CONNECTED,
|
|
};
|
|
|
|
// Every field here is protected by the socket table's single lock in
|
|
// kernel/net/unix.c -- there is deliberately no per-socket lock; see the
|
|
// comment at the top of that file for why.
|
|
struct socket {
|
|
int ref;
|
|
int type;
|
|
int state;
|
|
int domain; // AF_UNIX or AF_INET
|
|
|
|
// For AF_INET: the lwIP-backed socket in kernel/net/inet.c. Opaque here so
|
|
// that socket.h does not have to drag lwIP's headers into every file that
|
|
// wants a struct socket.
|
|
void *inet;
|
|
|
|
char name[UNIX_PATH_MAX]; // the bound path, for a listener
|
|
|
|
// A connected socket's peer. Writing to a socket appends to the *peer's*
|
|
// buffer, which is what makes one buffer per socket enough for a
|
|
// bidirectional connection.
|
|
struct socket *peer;
|
|
int peer_closed;
|
|
|
|
char data[SOCKBUF];
|
|
uint nread, nwrite;
|
|
|
|
// Connections waiting to be accepted, on a listener.
|
|
struct socket *queue[NBACKLOG];
|
|
int nqueue;
|
|
|
|
// Socket options that are genuinely honoured. Kept here rather than only in
|
|
// lwIP's pcb because getsockopt has to answer for an AF_UNIX socket too, and
|
|
// because a flag set before bind() has to survive until there is a pcb to
|
|
// apply it to.
|
|
int o_reuseaddr;
|
|
int o_keepalive;
|
|
int o_broadcast;
|
|
int o_nodelay;
|
|
};
|
|
|
|
#endif
|