From f58db7ea8d198edf7ebb1330197bff3136891196 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sun, 14 Feb 2021 08:09:08 +0100 Subject: [PATCH] Add a helper function to retrieve socket type from protocol. --- src/net.c | 4 ++++ src/net.h | 1 + src/rinetd.c | 6 ++---- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/net.c b/src/net.c index 6688bed..045f23c 100644 --- a/src/net.c +++ b/src/net.c @@ -32,6 +32,10 @@ void setSocketDefaults(SOCKET fd) { #endif } +int getSocketType(int protocol) { + return protocol == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM; +} + uint16_t getPort(struct addrinfo* ai) { switch (ai->ai_family) { case AF_INET: diff --git a/src/net.h b/src/net.h index c486959..c41c4d0 100644 --- a/src/net.h +++ b/src/net.h @@ -81,4 +81,5 @@ static inline int GetLastError(void) { #endif /* _WIN32 */ void setSocketDefaults(SOCKET fd); +int getSocketType(int protocol); uint16_t getPort(struct addrinfo* ai); diff --git a/src/rinetd.c b/src/rinetd.c index 0a0d7a4..3197a29 100644 --- a/src/rinetd.c +++ b/src/rinetd.c @@ -278,7 +278,7 @@ void addServer(char *bindAddress, char *bindPort, int bindProtocol, { .ai_family = AF_UNSPEC, .ai_protocol = bindProtocol, - .ai_socktype = bindProtocol == IPPROTO_TCP ? SOCK_STREAM : SOCK_DGRAM, + .ai_socktype = getSocketType(bindProtocol), .ai_flags = AI_PASSIVE, }; struct addrinfo *servinfo; @@ -733,9 +733,7 @@ static void handleAccept(ServerInfo const *srv) This, too, is nonblocking. Why wait for anything when you don't have to? */ struct sockaddr_in saddr; - cnx->local.fd = socket(PF_INET, - srv->toProtocol == IPPROTO_TCP ? SOCK_STREAM : SOCK_DGRAM, - srv->toProtocol); + cnx->local.fd = socket(PF_INET, getSocketType(srv->toProtocol), srv->toProtocol); if (cnx->local.fd == INVALID_SOCKET) { syslog(LOG_ERR, "socket(): %m\n"); if (cnx->remote.protocol == IPPROTO_TCP)