From 99bb5d33f24f1d39aa9a4ec2d6ef8236795c90e1 Mon Sep 17 00:00:00 2001 From: Jonathan White Date: Thu, 7 Jul 2016 19:32:31 -0400 Subject: [PATCH] Incorporated fix for IPV6 remote server --- src/http/Server.cpp | 73 ++++++++++++++++++++++++++++++--------------- 1 file changed, 49 insertions(+), 24 deletions(-) diff --git a/src/http/Server.cpp b/src/http/Server.cpp index e54a9995c..2cd97045f 100644 --- a/src/http/Server.cpp +++ b/src/http/Server.cpp @@ -323,51 +323,76 @@ void Server::request_completed(void *, struct MHD_Connection *, void Server::start(void) { - if (m_started) return; + if (m_started) + return; - int port = HttpSettings::httpPort(); - - struct sockaddr_in as; - struct sockaddr_in *ss = &as; bool nohost = true; + int port = HttpSettings::httpPort(); QHostInfo info = QHostInfo::fromName(HttpSettings::httpHost()); if (!info.addresses().isEmpty()) { + void* addrx = NULL; + unsigned int flags = MHD_USE_SELECT_INTERNALLY; QHostAddress address = info.addresses().first(); if (address.protocol() == QAbstractSocket::IPv4Protocol) { - struct sockaddr_in* addr = ss; - memset(addr, 0, sizeof(struct sockaddr_in)); + struct sockaddr_in *addr = static_cast(calloc(1, sizeof(struct sockaddr_in))); + addrx = static_cast(addr); addr->sin_family = AF_INET; addr->sin_port = htons(HttpSettings::httpPort()); addr->sin_addr.s_addr = htonl(address.toIPv4Address()); nohost = false; -#ifdef MHD_USE_IPv6 + //qWarning("HTTPPlugin: IPv4 host configured"); } 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 + if (MHD_YES == MHD_is_feature_supported(MHD_FEATURE_IPv6)) { + struct sockaddr_in6 *addr = static_cast(calloc(1, sizeof(struct sockaddr_in6))); + addrx = static_cast(addr); + 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; + /* + qWarning("HTTPPlugin: IPv6 host configured and IPv6 enabled"); + } else { + qWarning("HTTPPlugin: IPv6 host configured but IPv6 support disabled!"); + */ + } } - 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, + addrx, + MHD_OPTION_END))) { + nohost = true; + qWarning("HTTPPlugin: Failed to bind to configured host!"); + } else { + nohost = false; + //qWarning("HTTPPlugin: Binded to configured host."); + } + + } + + if (addrx != NULL) + free(addrx); } if (nohost) { - daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, port, NULL, NULL, + 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); + 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;