libiio  0.25
Library for interfacing with IIO devices
dns_sd.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /*
3  * libiio - Library for interfacing industrial I/O (IIO) devices
4  *
5  * Copyright (C) 2014-2020 Analog Devices, Inc.
6  * Author: Paul Cercueil
7  * Robin Getz
8  */
9 
10 #ifndef __IIO_DNS_SD_H
11 #define __IIO_DNS_SD_H
12 
13 #include <stdbool.h>
14 #include <stdint.h>
15 
16 #ifdef _WIN32
17 #include <winsock2.h>
18 #include <ntddndis.h>
19 #include <netioapi.h>
20 #else
21 #include <net/if.h>
22 #include <sys/param.h>
23 #endif
24 
25 /* IPv6 Max = 4*8 + 7 + 1 for '%' + interface length */
26 #define DNS_SD_ADDRESS_STR_MAX (40 + IF_NAMESIZE)
27 #define FQDN_LEN (255) /* RFC 1035 */
28 
29 /* MacOS doesn't include ENOMEDIUM (No medium found) like Linux does */
30 #ifndef ENOMEDIUM
31 #define ENOMEDIUM ENOENT
32 #endif
33 
34 /* Used everywhere */
35 #define IIOD_PORT 30431
36 
37 struct addrinfo;
38 struct AvahiSimplePoll;
39 struct AvahiAddress;
40 
41 /* Common structure which all dns_sd_[*] files fill out
42  * Anything that is dynamically allocated (malloc) needs to be managed
43  */
44 struct dns_sd_discovery_data {
45  struct iio_mutex *lock;
46  struct AvahiSimplePoll *poll;
47  struct AvahiAddress *address;
48  uint16_t found, resolved;
49  char addr_str[DNS_SD_ADDRESS_STR_MAX];
50  char *hostname;
51  uint16_t port, iface;
52  struct dns_sd_discovery_data *next;
53 };
54 
55 
56 /* This functions is implemented in network.c, but used in dns_sd.c
57  */
58 int create_socket(const struct addrinfo *addrinfo);
59 
60 /* These functions are common, and implemented in dns_sd_[*].c based on the
61  * implementations: avahi (linux), bonjour (mac), or ServiceDiscovery (Win10)
62  */
63 
64 /* Resolves all IIO hosts on the available networks, and passes back a linked list */
65 int dnssd_find_hosts(struct dns_sd_discovery_data ** ddata);
66 
67 /* Deallocates complete list of discovery data */
68 void dnssd_free_all_discovery_data(struct dns_sd_discovery_data *d);
69 
70 /* These functions are common, and found in dns_sd.c, but are used in the
71  * dns_sd_[*].c implementations or network.c
72  */
73 
74 /* Passed back the first (random) IIOD service resolved by DNS DS. */
75 int dnssd_discover_host(char *addr_str, size_t addr_len, uint16_t *port);
76 
77 /* remove duplicates from the list */
78 void remove_dup_discovery_data(struct dns_sd_discovery_data **ddata);
79 
80 /* port knocks */
81 void port_knock_discovery_data(struct dns_sd_discovery_data **ddata);
82 
83 /* Use dnssd to resolve a given hostname */
84 int dnssd_resolve_host(const char *hostname, char *ip_addr, const int addr_len);
85 
86 #endif /* __IIO_DNS_SD_H */