Skip to content

Commit

Permalink
dnssd-windows: Update license, remove _WIN32 defines and use safer st…
Browse files Browse the repository at this point in the history
…r functions

Signed-off-by: Adrian Suciu <[email protected]>
  • Loading branch information
adisuciu committed May 21, 2020
1 parent 4948ad0 commit 4a55dfd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 33 deletions.
57 changes: 25 additions & 32 deletions dns_sd_windows.c
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
*
* 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 <stdio.h>
#include <errno.h>
Expand All @@ -11,11 +25,8 @@
#include "network.h"
#include "debug.h"

#ifdef _WIN32
# include <iphlpapi.h>
#else
# include <netdb.h>
#endif



static int new_discovery_data(struct dns_sd_discovery_data** data)
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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, &param);
#else
const int flags = fcntl(sockets[isock], F_GETFL, 0);
fcntl(sockets[isock], F_SETFL, flags | O_NONBLOCK);
#endif
}

return num_sockets;
Expand Down Expand Up @@ -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);
Expand All @@ -185,21 +187,14 @@ 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)) {
printf("Failed to initialize WinSock\n");
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");
Expand All @@ -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");

Expand All @@ -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));
Expand Down Expand Up @@ -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;
}
3 changes: 2 additions & 1 deletion mdns.h
Original file line number Diff line number Diff line change
@@ -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.
Expand Down

0 comments on commit 4a55dfd

Please sign in to comment.