From c766dff2e9837e61fbf927a29ec4be9acaec5b95 Mon Sep 17 00:00:00 2001 From: Egor Date: Fri, 13 Jun 2025 03:07:00 +0300 Subject: [PATCH] consistently use uv_os_sock_t and -1 --- src/admin.cpp | 2 +- src/common.h | 2 +- src/irc.cpp | 14 +++++++------- src/main.cpp | 48 ++++++++++++++++++++++++------------------------ src/net.cpp | 24 ++++++++++++------------ src/netpoll.cpp | 4 ++-- src/netsock.h | 2 -- 7 files changed, 47 insertions(+), 49 deletions(-) diff --git a/src/admin.cpp b/src/admin.cpp index fac2a8a..894bd5a 100644 --- a/src/admin.cpp +++ b/src/admin.cpp @@ -116,7 +116,7 @@ static int client_index(lua_State *L) #define string_getter(c, s) if (!strcmp(key, #s)) return lua_pushstring(L, c->s), 1 #define integer_getter(c, s) if (!strcmp(key, #s)) return lua_pushinteger(L, c->s), 1 #define map_getter(c, s, t) if (!strcmp(key, #s)) return map_push(L, &c->s, t), 1 - integer_getter(cl, FD); + integer_getter(cl, fd); /* cctx */ integer_getter(cl, type); if (!strcmp(key, "next")) { diff --git a/src/common.h b/src/common.h index 4eeb41f..7b18e08 100644 --- a/src/common.h +++ b/src/common.h @@ -38,7 +38,7 @@ struct tls; #define CAP_ACCOUNTNOTIFY 256 struct client_data { char *key; // case-mapped nick - SOCKET FD; + uv_os_sock_t fd; struct tls *cctx; char type; struct client_data *next; diff --git a/src/irc.cpp b/src/irc.cpp index 349c0bc..d75b028 100644 --- a/src/irc.cpp +++ b/src/irc.cpp @@ -14,7 +14,7 @@ stringset autojoin = {}; const char *hostname = 0; void sendbuf(struct client_data *client) { - fprintf(stderr, "SERVER %" PRISocket ": %.*s", client->FD, (int)databuf.len, (char*)databuf.buf); + fprintf(stderr, "SERVER %" PRISocket ": %.*s", client->fd, (int)databuf.len, (char*)databuf.buf); sendClient(client, databuf.buf, databuf.len); } static void chanlog_time(time_t timestamp) { @@ -119,12 +119,12 @@ int parsePacket(struct client_data *client) { if (end > p && end[-1] == '\r') *--end = '\n'; if (memchr(p, '\0', end-p)) { - fprintf(stderr, "Client %" PRISocket " sent a NULL\n", client->FD); + fprintf(stderr, "Client %" PRISocket " sent a NULL\n", client->fd); killConnection(client, 0); return 1; // null detected } if (memchr(p, '\r', end-p)) { - fprintf(stderr, "Client %" PRISocket " sent an extra CR\n", client->FD); + fprintf(stderr, "Client %" PRISocket " sent an extra CR\n", client->fd); killConnection(client, 0); return 1; // internal \r detected } @@ -169,7 +169,7 @@ int parsePacket(struct client_data *client) { } args[argc] = NULL; } - fprintf(stderr, "parsed %" PRISocket ": %s", client->FD, command); + fprintf(stderr, "parsed %" PRISocket ": %s", client->fd, command); if (strcmp(command, "PASS") != 0 && strcmp(command, "OPER") != 0 && strcmp(command, "NEWACC") != 0 && strcmp(command, "AUTHENTICATE") != 0 && strcmp(command, "WEBIRC") != 0) { for (int i = 0; i < argc; i++){ @@ -875,7 +875,7 @@ saslcleanup: struct client_data *cl = findClient(nick); if (!cl) return err_nosuchnick(client, nick), sendbuf(client), 0; - if (client->FD != cl->FD) + if (client != cl) return err_usersdontmatch(client, "Can't view mode for other users"), sendbuf(client), 0; rpl_umodeis(client), sendbuf(client); } else { @@ -890,7 +890,7 @@ saslcleanup: struct client_data *cl = findClient(nick); if (!cl) return err_nosuchnick(client, nick), sendbuf(client), 0; - if (client->FD != cl->FD) + if (client != cl) return err_usersdontmatch(client, "Can't change mode for other users"), sendbuf(client), 0; int is_plus; char *p = args[1]; @@ -1242,7 +1242,7 @@ void newUser(struct client_data *client) { #ifdef HAVE_LIBTLS // TODO: move this when we add sasl if (client->cctx && tls_peer_cert_provided(client->cctx)) - fprintf(stderr, "CertFP %" PRISocket ": %s\n", client->FD, tls_peer_cert_hash(client->cctx)); + fprintf(stderr, "CertFP %" PRISocket ": %s\n", client->fd, tls_peer_cert_hash(client->cctx)); #endif rpl_welcome(client), sendbuf(client); rpl_yourhost(client), sendbuf(client); diff --git a/src/main.cpp b/src/main.cpp index 3670f22..b7fb208 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,7 +16,7 @@ struct client_data *clients = NULL, *dyingclients = NULL; FILE *fchanlog = NULL; struct listener { - SOCKET sock; + uv_os_sock_t fd; struct tls *ctx; }; @@ -51,18 +51,18 @@ static listener create_listen_sock(const char *ip, const char *port, struct tls if (err) fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(err)), exit(1); - SOCKET sock = socket(ai.ai_family, ai.ai_socktype | SOCK_CLOEXEC | SOCK_NONBLOCK, ai.ai_protocol); - if(sock == INVALID_SOCKET) + uv_os_sock_t fd = socket(ai.ai_family, ai.ai_socktype | SOCK_CLOEXEC | SOCK_NONBLOCK, ai.ai_protocol); + if(fd == -1) net_perror("socket"), exit(1); - if (setsockint(sock, SOL_SOCKET, SO_REUSEADDR, 1) == -1) + if (setsockint(fd, SOL_SOCKET, SO_REUSEADDR, 1) == -1) net_perror("setsockopt(SO_REUSEADDR)"), exit(1); - if (ai.ai_family == AF_INET6 && setsockint(sock, IPPROTO_IPV6, IPV6_V6ONLY, 1) == -1) + if (ai.ai_family == AF_INET6 && setsockint(fd, IPPROTO_IPV6, IPV6_V6ONLY, 1) == -1) net_perror("setsockopt(IPV6_V6ONLY)"), exit(1); - if (bind(sock, ai.ai_addr, ai.ai_addrlen) == -1) + if (bind(fd, ai.ai_addr, ai.ai_addrlen) == -1) net_perror("bind"), exit(1); - if (listen(sock, SOMAXCONN) == -1) + if (listen(fd, SOMAXCONN) == -1) net_perror("listen"), exit(1); - return listener{sock, ctx}; + return listener{fd, ctx}; } static void accept_from_sock(struct listener *listener) @@ -70,8 +70,8 @@ static void accept_from_sock(struct listener *listener) for (;;) { struct sockaddr_storage addr; socklen_t len = sizeof(addr); - SOCKET newSocket = accept4(listener->sock, (struct sockaddr *)&addr, &len, SOCK_CLOEXEC | SOCK_NONBLOCK); - if (newSocket == INVALID_SOCKET) { + uv_os_sock_t fd = accept4(listener->fd, (struct sockaddr *)&addr, &len, SOCK_CLOEXEC | SOCK_NONBLOCK); + if (fd == -1) { if (!net_wouldblock()) net_perror("accept4"); break; @@ -79,15 +79,15 @@ static void accept_from_sock(struct listener *listener) { // enable keepalive - setsockint(newSocket, SOL_SOCKET, SO_KEEPALIVE, 1); + setsockint(fd, SOL_SOCKET, SO_KEEPALIVE, 1); #ifdef TCP_KEEPIDLE - setsockint(newSocket, IPPROTO_TCP, TCP_KEEPIDLE, 30); + setsockint(fd, IPPROTO_TCP, TCP_KEEPIDLE, 30); #endif #ifdef TCP_KEEPINTVL - setsockint(newSocket, IPPROTO_TCP, TCP_KEEPINTVL, 1); + setsockint(fd, IPPROTO_TCP, TCP_KEEPINTVL, 1); #endif #ifdef TCP_KEEPCNT - setsockint(newSocket, IPPROTO_TCP, TCP_KEEPCNT, 10); + setsockint(fd, IPPROTO_TCP, TCP_KEEPCNT, 10); #endif } @@ -96,7 +96,7 @@ static void accept_from_sock(struct listener *listener) { struct sockaddr_storage sockname; socklen_t socknamelen = sizeof(sockname); - if (getsockname(newSocket, (struct sockaddr *)&sockname, &socknamelen) == -1) + if (getsockname(fd, (struct sockaddr *)&sockname, &socknamelen) == -1) net_perror("getsockname"), abortf("getsockname failure during accept"); if (sockname.ss_family == AF_INET) { struct in_addr *ia = &((struct sockaddr_in *)&sockname)->sin_addr; @@ -130,13 +130,13 @@ static void accept_from_sock(struct listener *listener) strcpy_lit(client_addr, "?"); port = 0; } - fprintf(stderr, "Connection accepted from Socket: %" PRISocket ", IP: %s PORT: %d\n", newSocket, + fprintf(stderr, "Connection accepted from Socket: %" PRISocket ", IP: %s PORT: %d\n", fd, client_addr, port); struct tls *cctx = NULL; if (listener->ctx) { #ifdef HAVE_LIBTLS - if (tls_accept_socket(listener->ctx, &cctx, newSocket) == -1) { - closesocket(newSocket); + if (tls_accept_socket(listener->ctx, &cctx, fd) == -1) { + closesocket(fd); fprintf(stderr, "tls_accept_socket: %s\n", tls_error(listener->ctx)); continue; } @@ -144,7 +144,7 @@ static void accept_from_sock(struct listener *listener) } // Add to list of sockets struct client_data *cl = allocClient(); - cl->FD = newSocket; + cl->fd = fd; cl->cctx = cctx; cl->type = addr.ss_family == AF_INET6 ? 6 : 4; cl->peername = mystrdup(client_addr); @@ -161,7 +161,7 @@ static void accept_from_sock(struct listener *listener) } if (!cl->hostname) abortf("strdup fail (hostname)\n"); - netpoll::add(cl->FD, SOCKTYPE_CLIENT, cl); + netpoll::add(cl->fd, SOCKTYPE_CLIENT, cl); update_poll_reg(cl); copy_strf(&databuf, ":%s NOTICE * :Hello welcome to PatchyIRCD!\r\n", hostname); sendbuf(cl); @@ -288,14 +288,14 @@ int main(int argc, char *argv[]) // NOTE: don't push_back to listeners beyond this point for (listener &listener : listeners) { - netpoll::add(listener.sock, SOCKTYPE_LISTENER, &listener); - netpoll::reg(listener.sock, UV_READABLE); + netpoll::add(listener.fd, SOCKTYPE_LISTENER, &listener); + netpoll::reg(listener.fd, UV_READABLE); } FILE *f = fopen(getdatafile("init.txt"), "rbe"); if (f) { struct client_data *fake = allocClient(); - fake->FD = INVALID_SOCKET; + fake->fd = -1; fake->umode |= UMODE_OPER | UMODE_SECURE; fake->hostname = mystrdup("localhost"); if (!fake->hostname) @@ -355,7 +355,7 @@ int main(int argc, char *argv[]) uv_signal_start_oneshot(&uvsigint, [](uv_signal_t *handle, int) { fprintf(stderr, "Cleaning up...\n"); for (listener &listener : listeners) { - netpoll::del_and_close(listener.sock); + netpoll::del_and_close(listener.fd); } for (struct client_data *cl = clients; cl;) { struct client_data *next = cl->next; diff --git a/src/net.cpp b/src/net.cpp index 38cb7f1..1b97a44 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -13,7 +13,7 @@ struct client_data *allocClient(void) void update_poll_reg(struct client_data *cl) { - if (cl->FD == INVALID_SOCKET) + if (cl->fd == -1) return; int nreg = 0; /* Shorthand: @@ -32,7 +32,7 @@ void update_poll_reg(struct client_data *cl) nreg |= UV_WRITABLE; } if (nreg != cl->poll_reg) { - netpoll::reg(cl->FD, nreg); + netpoll::reg(cl->fd, nreg); cl->poll_reg = nreg; } } @@ -46,7 +46,7 @@ void killConnection(struct client_data *cl, int byChoice) cl->dying = 1; job_kill(cl); if (cl->peername) - fprintf(stderr, "Client disconnected from Socket: %" PRISocket ", IP: %s PORT: %d\n", cl->FD, cl->peername, cl->peerport); + fprintf(stderr, "Client disconnected from Socket: %" PRISocket ", IP: %s PORT: %d\n", cl->fd, cl->peername, cl->peerport); if (!byChoice && cl->nick && cl->username) { msg_quit(cl, "", "Has left the building (Forced)"); sendbuf_visible(cl); @@ -63,8 +63,8 @@ void killConnection(struct client_data *cl, int byChoice) } cl->next = dyingclients; dyingclients = cl; - if (cl->FD != INVALID_SOCKET) - netpoll::change_type(cl->FD, SOCKTYPE_DYING); + if (cl->fd != -1) + netpoll::change_type(cl->fd, SOCKTYPE_DYING); update_poll_reg(cl); /* Part all channels */ for (auto chan : cl->channels.pop_view()) @@ -93,7 +93,7 @@ void killConnectionPart2(struct client_data *cl) } cl->want_read_for_close = 0; cl->want_write_for_close = 0; - if (cl->FD != INVALID_SOCKET) { + if (cl->fd != -1) { #ifdef HAVE_LIBTLS if (cl->cctx) { int rv = tls_close(cl->cctx); @@ -108,14 +108,14 @@ void killConnectionPart2(struct client_data *cl) return; } if (rv == -1) { - fprintf(stderr, "tls_close (%" PRISocket "): %s\n", cl->FD, tls_error(cl->cctx)); + fprintf(stderr, "tls_close (%" PRISocket "): %s\n", cl->fd, tls_error(cl->cctx)); } tls_free(cl->cctx); cl->cctx = NULL; } #endif - shutdown(cl->FD, SHUT_RDWR); - netpoll::del_and_close(cl->FD); + shutdown(cl->fd, SHUT_RDWR); + netpoll::del_and_close(cl->fd); } /* Remove the client from the dyingclients chain */ for (struct client_data **next = &dyingclients; *next; next = &(*next)->next) { @@ -179,7 +179,7 @@ void recvProc(struct client_data *cl) { else #endif { - bytesRead = recv(cl->FD,cl->buf + cl->bufLen,sizeof(cl->buf) - cl->bufLen, 0); + bytesRead = recv(cl->fd, cl->buf + cl->bufLen,sizeof(cl->buf) - cl->bufLen, 0); switch(bytesRead){ // Disconnect case 0: @@ -220,7 +220,7 @@ static int sendClientRaw(struct client_data *cl, const char *buf, size_t len) { cl->want_read_for_write = 0; cl->want_write_for_write = 0; - if (cl->FD == (SOCKET)INVALID_SOCKET) + if (cl->fd == -1) return 0; #ifdef HAVE_LIBTLS if (cl->cctx) { @@ -242,7 +242,7 @@ static int sendClientRaw(struct client_data *cl, const char *buf, size_t len) #endif { while (len) { - int rv = send(cl->FD, buf, len, 0); + int rv = send(cl->fd, buf, len, 0); if (!rv) return -1; if (rv == -1) { diff --git a/src/netpoll.cpp b/src/netpoll.cpp index ba6abd6..2f03008 100644 --- a/src/netpoll.cpp +++ b/src/netpoll.cpp @@ -77,8 +77,8 @@ bool get(event &out) static std::vector::iterator search(uv_os_sock_t fd, bool add=false) { - if (fd == INVALID_SOCKET) - abortf("netpoll::search: INVALID_SOCKET\n"); + if (fd == -1) + abortf("netpoll::search: invalid socket\n"); auto it = std::partition_point(fds.begin(), fds.end(), [fd](const auto &p){ return p->fd < fd; }); bool found = it != fds.end() && (*it)->fd == fd; if (found == add) diff --git a/src/netsock.h b/src/netsock.h index e5fc000..f60c7bf 100644 --- a/src/netsock.h +++ b/src/netsock.h @@ -50,9 +50,7 @@ #include #include #include -typedef int SOCKET; #define PRISocket "d" -#define INVALID_SOCKET (-1) #define net_perror(p) (perror((p))) #define net_wouldblock() (errno == EAGAIN || errno == EWOULDBLOCK) #define closesocket close -- 2.56.0