diff options
| author | Beni Keller <nospam@example.com> | 2024-12-26 08:23:03 +0100 |
|---|---|---|
| committer | Beni Keller <nospam@example.com> | 2024-12-27 10:12:25 +0100 |
| commit | 62e7ef21705d7c401b6382bfc5d8a56cc4b0aa4d (patch) | |
| tree | 3dd90a9aceb6a38f57aa63e979ff84f9b4fdb33d /xs_socket.h | |
| parent | 2384a5409bc3a8a6ede4bf33bb1584a6bfae39af (diff) | |
Adding support for IPv6
Diffstat (limited to 'xs_socket.h')
| -rw-r--r-- | xs_socket.h | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/xs_socket.h b/xs_socket.h index 1bd053a..fb67b9d 100644 --- a/xs_socket.h +++ b/xs_socket.h @@ -54,7 +54,39 @@ int xs_socket_server(const char *addr, const char *serv) /* opens a server socket by service name (or port as string) */ { int rs = -1; - struct sockaddr_in host; +#ifndef WITHOUT_GETADDRINFO + struct addrinfo *res; + struct addrinfo hints; + int status; + + memset(&hints, '\0', sizeof(hints)); + + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + + if (getaddrinfo(addr, serv, &hints, &res) != 0) { + goto end; + } + + rs = socket(res->ai_family, res->ai_socktype, res->ai_protocol); + + /* reuse addr */ + int i = 1; + setsockopt(rs, SOL_SOCKET, SO_REUSEADDR, (char *)&i, sizeof(i)); + + status = bind(rs, res->ai_addr, res->ai_addrlen); + if (status == -1) { + fprintf(stderr, "Error binding with status %d\n", status); + close(rs); + rs = -1; + } + else { + + listen(rs, SOMAXCONN); + } + +#else /* WITHOUT_GETADDRINFO */ + struct sockaddr_in host; memset(&host, '\0', sizeof(host)); @@ -89,6 +121,7 @@ int xs_socket_server(const char *addr, const char *serv) listen(rs, SOMAXCONN); } +#endif /* WITHOUT_GETADDRINFO */ end: return rs; } |