diff options
| author | 2024-12-26 08:23:03 +0100 | |
|---|---|---|
| committer | 2024-12-27 10:12:25 +0100 | |
| commit | 62e7ef21705d7c401b6382bfc5d8a56cc4b0aa4d (patch) | |
| tree | 3dd90a9aceb6a38f57aa63e979ff84f9b4fdb33d /xs_socket.h | |
| parent | Make xmpp and mailto URLs clickable. (diff) | |
| download | snac2-62e7ef21705d7c401b6382bfc5d8a56cc4b0aa4d.tar.gz snac2-62e7ef21705d7c401b6382bfc5d8a56cc4b0aa4d.tar.xz snac2-62e7ef21705d7c401b6382bfc5d8a56cc4b0aa4d.zip | |
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) | |||
| 54 | /* opens a server socket by service name (or port as string) */ | 54 | /* opens a server socket by service name (or port as string) */ |
| 55 | { | 55 | { |
| 56 | int rs = -1; | 56 | int rs = -1; |
| 57 | struct sockaddr_in host; | 57 | #ifndef WITHOUT_GETADDRINFO |
| 58 | struct addrinfo *res; | ||
| 59 | struct addrinfo hints; | ||
| 60 | int status; | ||
| 61 | |||
| 62 | memset(&hints, '\0', sizeof(hints)); | ||
| 63 | |||
| 64 | hints.ai_family = AF_UNSPEC; | ||
| 65 | hints.ai_socktype = SOCK_STREAM; | ||
| 66 | |||
| 67 | if (getaddrinfo(addr, serv, &hints, &res) != 0) { | ||
| 68 | goto end; | ||
| 69 | } | ||
| 70 | |||
| 71 | rs = socket(res->ai_family, res->ai_socktype, res->ai_protocol); | ||
| 72 | |||
| 73 | /* reuse addr */ | ||
| 74 | int i = 1; | ||
| 75 | setsockopt(rs, SOL_SOCKET, SO_REUSEADDR, (char *)&i, sizeof(i)); | ||
| 76 | |||
| 77 | status = bind(rs, res->ai_addr, res->ai_addrlen); | ||
| 78 | if (status == -1) { | ||
| 79 | fprintf(stderr, "Error binding with status %d\n", status); | ||
| 80 | close(rs); | ||
| 81 | rs = -1; | ||
| 82 | } | ||
| 83 | else { | ||
| 84 | |||
| 85 | listen(rs, SOMAXCONN); | ||
| 86 | } | ||
| 87 | |||
| 88 | #else /* WITHOUT_GETADDRINFO */ | ||
| 89 | struct sockaddr_in host; | ||
| 58 | 90 | ||
| 59 | memset(&host, '\0', sizeof(host)); | 91 | memset(&host, '\0', sizeof(host)); |
| 60 | 92 | ||
| @@ -89,6 +121,7 @@ int xs_socket_server(const char *addr, const char *serv) | |||
| 89 | listen(rs, SOMAXCONN); | 121 | listen(rs, SOMAXCONN); |
| 90 | } | 122 | } |
| 91 | 123 | ||
| 124 | #endif /* WITHOUT_GETADDRINFO */ | ||
| 92 | end: | 125 | end: |
| 93 | return rs; | 126 | return rs; |
| 94 | } | 127 | } |