From 95fd9a4ae2717a73bc446b29a87ad5bc9f00317e Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Fri, 30 Oct 2026 02:35:32 +0100 Subject: Reduce number of per connection sys calls --- ot_socket.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'ot_socket.c') diff --git a/ot_socket.c b/ot_socket.c index 158459f..47ed26d 100644 --- a/ot_socket.c +++ b/ot_socket.c @@ -4,6 +4,7 @@ $id$ */ /* System */ +#define _GNU_SOURCE /* accept4 on glibc */ #include #include #include @@ -170,19 +171,25 @@ int socket_is_connected(int s) { return 1; } +/* Linux hands over a blocking socket whatever the listening socket was, the + BSDs and macOS inherit the listener's flags, so the accepted socket has to + be told either way. accept4 carries the flag itself, which saves the two + fcntl calls it otherwise takes. Darwin has neither accept4 nor the flag. */ int socket_accept(int s, ot_ip6 ip, uint16_t *port) { struct sockaddr_storage ss; socklen_t len = sizeof(ss); int fd; memset(&ss, 0, sizeof(ss)); +#ifdef SOCK_NONBLOCK + if ((fd = accept4(s, (struct sockaddr *)&ss, &len, SOCK_NONBLOCK)) == -1) + return -1; +#else if ((fd = accept(s, (struct sockaddr *)&ss, &len)) == -1) return -1; - - /* Linux hands over a blocking socket whatever the listening socket was, the - BSDs and macOS inherit the listener's flags, so say which one we want. */ if (set_nonblocking(fd) == -1) return close_and_fail(fd); +#endif ip_from_sockaddr(ip, port, &ss); return fd; -- cgit v1.3