commit 62e7ef2
Beni Keller
·
2024-12-26 07:23:03 +0000 UTC
parent 2384a54
Adding support for IPv6
1 files changed,
+34,
-1
+34,
-1
1@@ -54,7 +54,39 @@ int xs_socket_server(const char *addr, const char *serv)
2 /* opens a server socket by service name (or port as string) */
3 {
4 int rs = -1;
5- struct sockaddr_in host;
6+#ifndef WITHOUT_GETADDRINFO
7+ struct addrinfo *res;
8+ struct addrinfo hints;
9+ int status;
10+
11+ memset(&hints, '\0', sizeof(hints));
12+
13+ hints.ai_family = AF_UNSPEC;
14+ hints.ai_socktype = SOCK_STREAM;
15+
16+ if (getaddrinfo(addr, serv, &hints, &res) != 0) {
17+ goto end;
18+ }
19+
20+ rs = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
21+
22+ /* reuse addr */
23+ int i = 1;
24+ setsockopt(rs, SOL_SOCKET, SO_REUSEADDR, (char *)&i, sizeof(i));
25+
26+ status = bind(rs, res->ai_addr, res->ai_addrlen);
27+ if (status == -1) {
28+ fprintf(stderr, "Error binding with status %d\n", status);
29+ close(rs);
30+ rs = -1;
31+ }
32+ else {
33+
34+ listen(rs, SOMAXCONN);
35+ }
36+
37+#else /* WITHOUT_GETADDRINFO */
38+ struct sockaddr_in host;
39
40 memset(&host, '\0', sizeof(host));
41
42@@ -89,6 +121,7 @@ int xs_socket_server(const char *addr, const char *serv)
43 listen(rs, SOMAXCONN);
44 }
45
46+#endif /* WITHOUT_GETADDRINFO */
47 end:
48 return rs;
49 }