From 1b137ad3c3518435da164d94a8420f1348604410 Mon Sep 17 00:00:00 2001 From: Thulinma Date: Sun, 7 Oct 2018 16:59:55 +0200 Subject: [PATCH] Fixed all-interface IPv6 sockets being created when attempting to listen on a non-IPv6 interface address --- lib/socket.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/socket.cpp b/lib/socket.cpp index 5a76bf68..fa88f87e 100644 --- a/lib/socket.cpp +++ b/lib/socket.cpp @@ -1095,10 +1095,16 @@ bool Socket::Server::IPv6bind(int port, std::string hostname, bool nonblock){ memset(&addr, 0, sizeof(addr)); addr.sin6_family = AF_INET6; addr.sin6_port = htons(port); // set port + //set interface, 0.0.0.0 (default) is all if (hostname == "0.0.0.0" || hostname.length() == 0){ addr.sin6_addr = in6addr_any; }else{ - inet_pton(AF_INET6, hostname.c_str(), &addr.sin6_addr); // set interface, 0.0.0.0 (default) is all + if (inet_pton(AF_INET6, hostname.c_str(), &addr.sin6_addr) != 1){ + errors = strerror(errno); + HIGH_MSG("Could not convert '%s' to a valid IPv6 address", hostname.c_str()); + close(); + return false; + } } int ret = bind(sock, (sockaddr *)&addr, sizeof(addr)); // do the actual bind if (ret == 0){ @@ -1143,10 +1149,16 @@ bool Socket::Server::IPv4bind(int port, std::string hostname, bool nonblock){ memset(&addr4, 0, sizeof(addr4)); addr4.sin_family = AF_INET; addr4.sin_port = htons(port); // set port + //set interface, 0.0.0.0 (default) is all if (hostname == "0.0.0.0" || hostname.length() == 0){ addr4.sin_addr.s_addr = INADDR_ANY; }else{ - inet_pton(AF_INET, hostname.c_str(), &addr4.sin_addr); // set interface, 0.0.0.0 (default) is all + if (inet_pton(AF_INET, hostname.c_str(), &addr4.sin_addr) != 1){ + errors = strerror(errno); + HIGH_MSG("Could not convert '%s' to a valid IPv4 address", hostname.c_str()); + close(); + return false; + } } int ret = bind(sock, (sockaddr *)&addr4, sizeof(addr4)); // do the actual bind if (ret == 0){