diff --git a/core/backends/lan/mdns_wrapper.cpp b/core/backends/lan/mdns_wrapper.cpp index 5739251e9..20660722e 100644 --- a/core/backends/lan/mdns_wrapper.cpp +++ b/core/backends/lan/mdns_wrapper.cpp @@ -59,7 +59,7 @@ static sockaddr_in qHostAddressToSockaddr(QHostAddress hostAddress) { Q_ASSERT(hostAddress.protocol() == QAbstractSocket::IPv4Protocol); sockaddr_in socketAddress; - memset(&socketAddress, 0, sizeof(socketAddress)); + memset(&socketAddress, 0, sizeof(struct sockaddr_in)); socketAddress.sin_family = AF_INET; socketAddress.sin_addr.s_addr = htonl(hostAddress.toIPv4Address()); return socketAddress; @@ -69,7 +69,7 @@ static sockaddr_in6 qHostAddressToSockaddr6(QHostAddress hostAddress) { Q_ASSERT(hostAddress.protocol() == QAbstractSocket::IPv6Protocol); sockaddr_in6 socketAddress; - memset(&socketAddress, 0, sizeof(socketAddress)); + memset(&socketAddress, 0, sizeof(struct sockaddr_in6)); socketAddress.sin6_family = AF_INET6; Q_IPV6ADDR ipv6Address = hostAddress.toIPv6Address(); for (int i = 0; i < 16; ++i) { @@ -182,12 +182,20 @@ int Discoverer::listenForQueryResponses() qCDebug(KDECONNECT_CORE) << "Opening socket for address" << sourceAddress; struct sockaddr_in saddr = qHostAddressToSockaddr(sourceAddress); int socket = mdns_socket_open_ipv4(&saddr); - sockets.append(socket); + if (socket >= 0) { + sockets.append(socket); + } else { + qCDebug(KDECONNECT_CORE) << "Couldn't open socket"; + } } else if (sourceAddress.protocol() == QAbstractSocket::IPv6Protocol && sourceAddress != QHostAddress::LocalHostIPv6) { qCDebug(KDECONNECT_CORE) << "Opening socket for address6" << sourceAddress; struct sockaddr_in6 saddr = qHostAddressToSockaddr6(sourceAddress); int socket = mdns_socket_open_ipv6(&saddr); - sockets.append(socket); + if (socket >= 0) { + sockets.append(socket); + } else { + qCDebug(KDECONNECT_CORE) << "Couldn't open socket"; + } } } } @@ -361,6 +369,8 @@ static int service_callback(int sock, const struct sockaddr* from, size_t addrle mdns_string_t nameMdnsString = mdns_string_extract(data, size, &name_offset, nameBuffer, sizeof(nameBuffer)); QByteArray name = QByteArray(nameMdnsString.str, nameMdnsString.length); + int ret = 0; + if (name == dnsSdName) { if ((record_type == MDNS_RECORDTYPE_PTR) || (record_type == MDNS_RECORDTYPE_ANY)) { // The PTR query was for the DNS-SD domain, send answer with a PTR record for the service name we advertise. @@ -369,11 +379,11 @@ static int service_callback(int sock, const struct sockaddr* from, size_t addrle uint16_t unicast = (rclass & MDNS_UNICAST_RESPONSE); if (unicast) { - mdns_query_answer_unicast(sock, from, addrlen, sendbuffer, sizeof(sendbuffer), query_id, + ret = mdns_query_answer_unicast(sock, from, addrlen, sendbuffer, sizeof(sendbuffer), query_id, (mdns_record_type_t)record_type, nameMdnsString.str, nameMdnsString.length, answer, nullptr, 0, nullptr, 0); } else { - mdns_query_answer_multicast(sock, sendbuffer, sizeof(sendbuffer), answer, nullptr, 0, nullptr, 0); + ret = mdns_query_answer_multicast(sock, sendbuffer, sizeof(sendbuffer), answer, nullptr, 0, nullptr, 0); } } } else if (name == self.serviceType) { @@ -399,11 +409,11 @@ static int service_callback(int sock, const struct sockaddr* from, size_t addrle uint16_t unicast = (rclass & MDNS_UNICAST_RESPONSE); if (unicast) { - mdns_query_answer_unicast(sock, from, addrlen, sendbuffer, sizeof(sendbuffer), query_id, + ret = mdns_query_answer_unicast(sock, from, addrlen, sendbuffer, sizeof(sendbuffer), query_id, (mdns_record_type_t)record_type, nameMdnsString.str, nameMdnsString.length, answer, nullptr, 0, additional.constData(), additional.length()); } else { - mdns_query_answer_multicast(sock, sendbuffer, sizeof(sendbuffer), answer, nullptr, 0, + ret = mdns_query_answer_multicast(sock, sendbuffer, sizeof(sendbuffer), answer, nullptr, 0, additional.constData(), additional.length()); } } @@ -429,11 +439,11 @@ static int service_callback(int sock, const struct sockaddr* from, size_t addrle uint16_t unicast = (rclass & MDNS_UNICAST_RESPONSE); if (unicast) { - mdns_query_answer_unicast(sock, from, addrlen, sendbuffer, sizeof(sendbuffer), query_id, + ret = mdns_query_answer_unicast(sock, from, addrlen, sendbuffer, sizeof(sendbuffer), query_id, (mdns_record_type_t)record_type, nameMdnsString.str, nameMdnsString.length, answer, nullptr, 0, additional.constData(), additional.length()); } else { - mdns_query_answer_multicast(sock, sendbuffer, sizeof(sendbuffer), answer, nullptr, 0, + ret = mdns_query_answer_multicast(sock, sendbuffer, sizeof(sendbuffer), answer, nullptr, 0, additional.constData(), additional.length()); } } @@ -455,11 +465,11 @@ static int service_callback(int sock, const struct sockaddr* from, size_t addrle uint16_t unicast = (rclass & MDNS_UNICAST_RESPONSE); if (unicast) { - mdns_query_answer_unicast(sock, from, addrlen, sendbuffer, sizeof(sendbuffer), query_id, + ret = mdns_query_answer_unicast(sock, from, addrlen, sendbuffer, sizeof(sendbuffer), query_id, (mdns_record_type_t)record_type, nameMdnsString.str, nameMdnsString.length, answer, nullptr, 0, additional.constData(), additional.length()); } else { - mdns_query_answer_multicast(sock, sendbuffer, sizeof(sendbuffer), answer, nullptr, 0, + ret = mdns_query_answer_multicast(sock, sendbuffer, sizeof(sendbuffer), answer, nullptr, 0, additional.constData(), additional.length()); } } else if (((record_type == MDNS_RECORDTYPE_AAAA) || (record_type == MDNS_RECORDTYPE_ANY)) && !self.addressesV6.empty()) { @@ -479,16 +489,19 @@ static int service_callback(int sock, const struct sockaddr* from, size_t addrle uint16_t unicast = (rclass & MDNS_UNICAST_RESPONSE); if (unicast) { - mdns_query_answer_unicast(sock, from, addrlen, sendbuffer, sizeof(sendbuffer), query_id, + ret = mdns_query_answer_unicast(sock, from, addrlen, sendbuffer, sizeof(sendbuffer), query_id, (mdns_record_type_t)record_type, nameMdnsString.str, nameMdnsString.length, answer, nullptr, 0, additional.constData(), additional.length()); } else { - mdns_query_answer_multicast(sock, sendbuffer, sizeof(sendbuffer), answer, nullptr, 0, + ret = mdns_query_answer_multicast(sock, sendbuffer, sizeof(sendbuffer), answer, nullptr, 0, additional.constData(), additional.length()); } } } // else request is not for me - return 0; + if (ret < 0) { + qCWarning(KDECONNECT_CORE) << "Error sending MDNS query response"; + } + return ret; } // Open sockets to listen to incoming mDNS queries on port 5353