diff -r 226bfd584075 -r 144bdc33fdb6 src/server/util/socket.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/util/socket.c Sun Feb 15 12:24:38 2026 +0100 @@ -0,0 +1,104 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2026 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "socket.h" + +#include +#include +#include +#include +#include + +int util_server_socket_local(short *port) { + int socket_fd = socket(AF_INET, SOCK_STREAM, 0); + if(socket_fd < 0) { + return -1; + } + + short socket_port = 0; + if(port) { + socket_port = *port; + } + + struct sockaddr_in addr; + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + addr.sin_port = htons(socket_port); + + if(bind(socket_fd, (struct sockaddr*)&addr, sizeof(addr))) { + close(socket_fd); + return -1; + } + + socklen_t len = sizeof(addr); + if(getsockname(socket_fd, (struct sockaddr *)&addr, &len) < 0) { + close(socket_fd); + return -1; + } + + if(port) { + *port = ntohs(addr.sin_port); + } + + return socket_fd; +} + +int util_socket_connect_local(short port) { + int fd = socket(AF_INET, SOCK_STREAM, 0); + if(fd < 0) { + return -1; + } + + struct sockaddr_in addr; + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + addr.sin_port = htons(port); + + if(connect(fd, (struct sockaddr*)&addr, sizeof(addr))) { + close(fd); + return -1; + } + + return fd; +} + +int util_socket_setnonblock(int fd, int nonblock) { + int flags; + if ((flags = fcntl(fd, F_GETFL, 0)) == -1) { + flags = 0; + } + if(nonblock) { + flags |= O_NONBLOCK; + } else { + flags &= ~O_NONBLOCK; + } + if (fcntl(fd, F_SETFL, flags) != 0) { + return 1; + } + return 0; +}