aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2022-08-30 11:57:30 +0200
committerEgor Tensin <Egor.Tensin@gmail.com>2022-08-30 11:57:30 +0200
commitc7a2516f5b39046d113a5bb80b64500faadcef8a (patch)
tree4853bea8fa804151ac004a127984342558cdc7cd
parentupdate command names (diff)
downloadcimple-c7a2516f5b39046d113a5bb80b64500faadcef8a.tar.gz
cimple-c7a2516f5b39046d113a5bb80b64500faadcef8a.zip
net: bind to IPv6
This makes sure we use the dual-stack feature to support both IPv4 and IPv6.
-rw-r--r--src/net.c12
1 files changed, 10 insertions, 2 deletions
diff --git a/src/net.c b/src/net.c
index 5c44c70..534807c 100644
--- a/src/net.c
+++ b/src/net.c
@@ -17,7 +17,7 @@ int net_bind(const char *port)
int socket_fd, ret = 0;
memset(&hints, 0, sizeof(hints));
- hints.ai_family = AF_UNSPEC;
+ hints.ai_family = AF_INET6;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
@@ -35,8 +35,16 @@ int net_bind(const char *port)
}
static const int yes = 1;
+ static const int no = 0;
+
+ if (it->ai_family == AF_INET6) {
+ if (setsockopt(socket_fd, IPPROTO_IPV6, IPV6_V6ONLY, &no, sizeof(no)) < 0) {
+ print_errno("setsockopt");
+ goto close_socket;
+ }
+ }
- if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1) {
+ if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
print_errno("setsockopt");
goto close_socket;
}