Add a helper function to retrieve socket type from protocol.

This commit is contained in:
Sam Hocevar 2021-02-14 08:09:08 +01:00
parent 7ef50421c0
commit f58db7ea8d
3 changed files with 7 additions and 4 deletions

View File

@ -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:

View File

@ -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);

View File

@ -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)