Error checking

This commit is contained in:
Albert Vaca Cintora 2023-08-12 19:21:12 +02:00
parent 1b3e8db72f
commit 5e270fea2e

View file

@ -59,7 +59,7 @@ static sockaddr_in qHostAddressToSockaddr(QHostAddress hostAddress)
{ {
Q_ASSERT(hostAddress.protocol() == QAbstractSocket::IPv4Protocol); Q_ASSERT(hostAddress.protocol() == QAbstractSocket::IPv4Protocol);
sockaddr_in socketAddress; sockaddr_in socketAddress;
memset(&socketAddress, 0, sizeof(socketAddress)); memset(&socketAddress, 0, sizeof(struct sockaddr_in));
socketAddress.sin_family = AF_INET; socketAddress.sin_family = AF_INET;
socketAddress.sin_addr.s_addr = htonl(hostAddress.toIPv4Address()); socketAddress.sin_addr.s_addr = htonl(hostAddress.toIPv4Address());
return socketAddress; return socketAddress;
@ -69,7 +69,7 @@ static sockaddr_in6 qHostAddressToSockaddr6(QHostAddress hostAddress)
{ {
Q_ASSERT(hostAddress.protocol() == QAbstractSocket::IPv6Protocol); Q_ASSERT(hostAddress.protocol() == QAbstractSocket::IPv6Protocol);
sockaddr_in6 socketAddress; sockaddr_in6 socketAddress;
memset(&socketAddress, 0, sizeof(socketAddress)); memset(&socketAddress, 0, sizeof(struct sockaddr_in6));
socketAddress.sin6_family = AF_INET6; socketAddress.sin6_family = AF_INET6;
Q_IPV6ADDR ipv6Address = hostAddress.toIPv6Address(); Q_IPV6ADDR ipv6Address = hostAddress.toIPv6Address();
for (int i = 0; i < 16; ++i) { for (int i = 0; i < 16; ++i) {
@ -182,12 +182,20 @@ int Discoverer::listenForQueryResponses()
qCDebug(KDECONNECT_CORE) << "Opening socket for address" << sourceAddress; qCDebug(KDECONNECT_CORE) << "Opening socket for address" << sourceAddress;
struct sockaddr_in saddr = qHostAddressToSockaddr(sourceAddress); struct sockaddr_in saddr = qHostAddressToSockaddr(sourceAddress);
int socket = mdns_socket_open_ipv4(&saddr); 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) { } else if (sourceAddress.protocol() == QAbstractSocket::IPv6Protocol && sourceAddress != QHostAddress::LocalHostIPv6) {
qCDebug(KDECONNECT_CORE) << "Opening socket for address6" << sourceAddress; qCDebug(KDECONNECT_CORE) << "Opening socket for address6" << sourceAddress;
struct sockaddr_in6 saddr = qHostAddressToSockaddr6(sourceAddress); struct sockaddr_in6 saddr = qHostAddressToSockaddr6(sourceAddress);
int socket = mdns_socket_open_ipv6(&saddr); 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)); mdns_string_t nameMdnsString = mdns_string_extract(data, size, &name_offset, nameBuffer, sizeof(nameBuffer));
QByteArray name = QByteArray(nameMdnsString.str, nameMdnsString.length); QByteArray name = QByteArray(nameMdnsString.str, nameMdnsString.length);
int ret = 0;
if (name == dnsSdName) { if (name == dnsSdName) {
if ((record_type == MDNS_RECORDTYPE_PTR) || (record_type == MDNS_RECORDTYPE_ANY)) { 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. // 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); uint16_t unicast = (rclass & MDNS_UNICAST_RESPONSE);
if (unicast) { 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, (mdns_record_type_t)record_type, nameMdnsString.str, nameMdnsString.length,
answer, nullptr, 0, nullptr, 0); answer, nullptr, 0, nullptr, 0);
} else { } 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) { } 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); uint16_t unicast = (rclass & MDNS_UNICAST_RESPONSE);
if (unicast) { 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, (mdns_record_type_t)record_type, nameMdnsString.str, nameMdnsString.length,
answer, nullptr, 0, additional.constData(), additional.length()); answer, nullptr, 0, additional.constData(), additional.length());
} else { } 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()); 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); uint16_t unicast = (rclass & MDNS_UNICAST_RESPONSE);
if (unicast) { 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, (mdns_record_type_t)record_type, nameMdnsString.str, nameMdnsString.length,
answer, nullptr, 0, additional.constData(), additional.length()); answer, nullptr, 0, additional.constData(), additional.length());
} else { } 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()); 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); uint16_t unicast = (rclass & MDNS_UNICAST_RESPONSE);
if (unicast) { 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, (mdns_record_type_t)record_type, nameMdnsString.str, nameMdnsString.length,
answer, nullptr, 0, additional.constData(), additional.length()); answer, nullptr, 0, additional.constData(), additional.length());
} else { } 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()); additional.constData(), additional.length());
} }
} else if (((record_type == MDNS_RECORDTYPE_AAAA) || (record_type == MDNS_RECORDTYPE_ANY)) && !self.addressesV6.empty()) { } 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); uint16_t unicast = (rclass & MDNS_UNICAST_RESPONSE);
if (unicast) { 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, (mdns_record_type_t)record_type, nameMdnsString.str, nameMdnsString.length,
answer, nullptr, 0, additional.constData(), additional.length()); answer, nullptr, 0, additional.constData(), additional.length());
} else { } 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()); additional.constData(), additional.length());
} }
} }
} // else request is not for me } // 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 // Open sockets to listen to incoming mDNS queries on port 5353