diff --git a/dns_sd_windows.c b/dns_sd_windows.c index 0a108c500..03248c420 100644 --- a/dns_sd_windows.c +++ b/dns_sd_windows.c @@ -1,8 +1,22 @@ -// Based on the example provided here: https://github.com/mjansson/mdns - -#ifdef _WIN32 -# define _CRT_SECURE_NO_WARNINGS 1 -#endif +/* + * libiio - Library for interfacing industrial I/O (IIO) devices + * + * Copyright (C) 2014-2020 Analog Devices, Inc. + * Author: Adrian Suciu + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * Based on https://github.com/mjansson/mdns/blob/ce2e4f789f06429008925ff8f18c22036e60201e/mdns.c + * which is Licensed under Public Domain + */ #include #include @@ -11,11 +25,8 @@ #include "network.h" #include "debug.h" -#ifdef _WIN32 # include -#else -# include -#endif + static int new_discovery_data(struct dns_sd_discovery_data** data) @@ -42,7 +53,6 @@ open_client_sockets(int* sockets, int max_sockets) { // Thus we need to open one socket for each interface and address family int num_sockets = 0; -#ifdef _WIN32 IP_ADAPTER_ADDRESSES* adapter_address = 0; ULONG address_size = 8000; unsigned int ret; @@ -66,8 +76,6 @@ open_client_sockets(int* sockets, int max_sockets) { return num_sockets; } - /*int first_ipv4 = 1; - int first_ipv6 = 1;*/ for (PIP_ADAPTER_ADDRESSES adapter = adapter_address; adapter; adapter = adapter->Next) { if (adapter->TunnelType == TUNNEL_TYPE_TEREDO) continue; @@ -113,16 +121,10 @@ open_client_sockets(int* sockets, int max_sockets) { } free(adapter_address); -#endif for (int isock = 0; isock < num_sockets; ++isock) { -#ifdef _WIN32 unsigned long param = 1; ioctlsocket(sockets[isock], FIONBIO, ¶m); -#else - const int flags = fcntl(sockets[isock], F_GETFL, 0); - fcntl(sockets[isock], F_SETFL, flags | O_NONBLOCK); -#endif } return num_sockets; @@ -167,10 +169,10 @@ query_callback(int sock, const struct sockaddr* from, size_t addrlen, if (srv.name.length > 1) { dd->hostname = malloc(srv.name.length); - strncpy(dd->hostname, srv.name.str, srv.name.length); + iio_strlcpy(dd->hostname, srv.name.str, srv.name.length); dd->hostname[srv.name.length - 1] = 0; } - strcpy(dd->addr_str, addrbuffer); + iio_strlcpy(dd->addr_str, addrbuffer, strlen(addrbuffer)); dd->port = srv.port; IIO_DEBUG("DNS SD: added %s (%s:%d)\n", dd->hostname, dd->addr_str, dd->port); @@ -185,8 +187,7 @@ query_callback(int sock, const struct sockaddr* from, size_t addrlen, int dnssd_find_hosts(struct dns_sd_discovery_data** ddata) { -#ifdef _WIN32 - const char* hostname = "dummy-host"; + WORD versionWanted = MAKEWORD(1, 1); WSADATA wsaData; if (WSAStartup(versionWanted, &wsaData)) { @@ -194,12 +195,6 @@ int dnssd_find_hosts(struct dns_sd_discovery_data** ddata) return -1; } - char hostname_buffer[128]; - DWORD hostname_size = (DWORD)sizeof(hostname_buffer); - if (GetComputerNameA(hostname_buffer, &hostname_size)) - hostname = hostname_buffer; -#endif - struct dns_sd_discovery_data* d; IIO_DEBUG("DNS SD: Start service discovery.\n"); @@ -211,7 +206,7 @@ int dnssd_find_hosts(struct dns_sd_discovery_data** ddata) size_t capacity = 2048; void* buffer = malloc(capacity); - const char service[] = "_iio._tcp.local."; + const char service[] = "_iio._tcp.local"; IIO_DEBUG("Sending DNS-SD discovery\n"); @@ -226,7 +221,7 @@ int dnssd_find_hosts(struct dns_sd_discovery_data** ddata) IIO_DEBUG("Sending mDNS query: %s\n", service); for (int isock = 0; isock < num_sockets; ++isock) { - transaction_id[isock] = mdns_query_send(sockets[isock], MDNS_RECORDTYPE_PTR, service, strlen(service), buffer, + transaction_id[isock] = mdns_query_send(sockets[isock], MDNS_RECORDTYPE_PTR, service, sizeof(service)-1, buffer, capacity); if (transaction_id[isock] <= 0) IIO_ERROR("Failed to send mDNS query: %s\n", strerror(errno)); @@ -256,9 +251,7 @@ int dnssd_find_hosts(struct dns_sd_discovery_data** ddata) mdns_socket_close(sockets[isock]); IIO_DEBUG("Closed socket%s\n", num_sockets ? "s" : ""); -#ifdef _WIN32 WSACleanup(); -#endif return 0; } diff --git a/mdns.h b/mdns.h index 7d4001547..cf893f7c0 100644 --- a/mdns.h +++ b/mdns.h @@ -1,4 +1,5 @@ -/* mdns.h - mDNS/DNS-SD library - Public Domain - 2017 Mattias Jansson +/* https://github.com/mjansson/mdns/blob/9744d37b4d9d1e5b97acf57c333d224d1a85acbe/mdns.h + * mdns.h - mDNS/DNS-SD library - Public Domain - 2017 Mattias Jansson * * This library provides a cross-platform mDNS and DNS-SD library in C. * The implementation is based on RFC 6762 and RFC 6763.