From 51bfbc964e778fd0fb00562931ca51c53946f5c7 Mon Sep 17 00:00:00 2001 From: Jonathan White Date: Tue, 24 May 2016 02:12:33 -0400 Subject: [PATCH] Fix IPv6 support and warn when binding fails Thanks to @eugenesan --- src/http/Server.cpp | 67 +++++++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 26 deletions(-) diff --git a/src/http/Server.cpp b/src/http/Server.cpp index e54a9995..53261aed 100644 --- a/src/http/Server.cpp +++ b/src/http/Server.cpp @@ -323,16 +323,18 @@ void Server::request_completed(void *, struct MHD_Connection *, void Server::start(void) { - if (m_started) return; + bool nohost = true; + struct sockaddr_in6 as; + struct sockaddr_in6 *ss = &as; + + if (m_started) + return; int port = HttpSettings::httpPort(); - struct sockaddr_in as; - struct sockaddr_in *ss = &as; - bool nohost = true; - QHostInfo info = QHostInfo::fromName(HttpSettings::httpHost()); if (!info.addresses().isEmpty()) { + unsigned int flags = MHD_USE_SELECT_INTERNALLY; QHostAddress address = info.addresses().first(); if (address.protocol() == QAbstractSocket::IPv4Protocol) { @@ -342,32 +344,45 @@ void Server::start(void) addr->sin_port = htons(HttpSettings::httpPort()); addr->sin_addr.s_addr = htonl(address.toIPv4Address()); nohost = false; -#ifdef MHD_USE_IPv6 - } else { - struct sockaddr_in6* addr = (sockaddr_in6*)ss; - memset(addr, 0, sizeof(struct sockaddr_in6)); - addr->sin6_family = AF_INET6; - addr->sin6_port = htons(HttpSettings::httpPort()); - memcpy(&addr->sin6_addr, address.toIPv6Address().c, 16); - nohost = false; -#endif + } else if (MHD_YES == MHD_is_feature_supported(MHD_FEATURE_IPv6)) { + struct sockaddr_in6* addr = (struct sockaddr_in6*)ss; + memset(addr, 0, sizeof(struct sockaddr_in6)); + addr->sin6_family = AF_INET6; + addr->sin6_port = htons(HttpSettings::httpPort()); + memcpy(&addr->sin6_addr, address.toIPv6Address().c, 16); + nohost = false; + flags |= MHD_USE_IPv6; } - daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, port, NULL, NULL, - &this->request_handler_wrapper, this, - MHD_OPTION_NOTIFY_COMPLETED, - this->request_completed, NULL, - MHD_OPTION_SOCK_ADDR, - ss, - MHD_OPTION_END); + if (nohost) { + qWarning("HTTPPlugin: Faled to get configured host!"); + } else { + if (NULL == (daemon = MHD_start_daemon(flags, port, NULL, NULL, + &this->request_handler_wrapper, this, + MHD_OPTION_NOTIFY_COMPLETED, + this->request_completed, NULL, + MHD_OPTION_SOCK_ADDR, + ss, + MHD_OPTION_END))) { + nohost = true; + qWarning("HTTPPlugin: Failed to bind to configured host!"); + } else { + nohost = false; + //qWarning("HTTPPlugin: Binded to configured host."); + } + } } if (nohost) { - daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, port, NULL, NULL, - &this->request_handler_wrapper, this, - MHD_OPTION_NOTIFY_COMPLETED, - this->request_completed, NULL, - MHD_OPTION_END); + if (NULL == (daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, port, NULL, NULL, + &this->request_handler_wrapper, this, + MHD_OPTION_NOTIFY_COMPLETED, + this->request_completed, NULL, + MHD_OPTION_END))) { + qWarning("HTTPPlugin: Fatal! Failed to bind to both configured and default hosts!"); + } else { + qWarning("HTTPPlugin: Bound to fallback address 0.0.0.0/:::!"); + } } m_started = true;