libiio  1.0
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 
28 /* MacOS doesn't include ENOMEDIUM (No medium found) like Linux does */
29 #ifndef ENOMEDIUM
30 #define ENOMEDIUM ENOENT
31 #endif
32 
33 /* Used everywhere */
34 #define IIOD_PORT 30431
35 
36 struct addrinfo;
37 struct AvahiSimplePoll;
38 struct AvahiAddress;
39 struct iio_context_params;
40 struct iio_scan;
41 
42 /* Common structure which all dns_sd_[*] files fill out
43  * Anything that is dynamically allocated (malloc) needs to be managed
44  */
45 struct dns_sd_discovery_data {
46  struct iio_mutex *lock;
47  struct AvahiSimplePoll *poll;
48  struct AvahiAddress *address;
49  uint16_t found, resolved;
50  char addr_str[DNS_SD_ADDRESS_STR_MAX];
51  char *hostname;
52  uint16_t port, iface;
53  struct dns_sd_discovery_data *next;
54 };
55 
56 struct dns_sd_cb_data {
57  struct dns_sd_discovery_data *d;
58  const struct iio_context_params *params;
59 };
60 
61 /* These functions are common, and implemented in dns_sd_[*].c based on the
62  * implementations: avahi (linux), bonjour (mac), or ServiceDiscovery (Win10)
63  */
64 
65 /* Resolves all IIO hosts on the available networks, and passes back a linked list */
66 int dnssd_find_hosts(const struct iio_context_params *params,
67  struct dns_sd_discovery_data **ddata);
68 
69 /* Deallocates complete list of discovery data */
70 void dnssd_free_all_discovery_data(const struct iio_context_params *params,
71  struct dns_sd_discovery_data *d);
72 
73 /* These functions are common, and found in dns_sd.c, but are used in the
74  * dns_sd_[*].c implementations or network.c
75  */
76 
77 /* Passed back the first (random) IIOD service resolved by DNS DS. */
78 int dnssd_discover_host(const struct iio_context_params *params,
79  char *addr_str, size_t addr_len, uint16_t *port);
80 
81 /* remove duplicates from the list */
82 void remove_dup_discovery_data(const struct iio_context_params *params,
83  struct dns_sd_discovery_data **ddata);
84 
85 /* port knocks */
86 void port_knock_discovery_data(const struct iio_context_params *params,
87  struct dns_sd_discovery_data **ddata);
88 
89 /* Use dnssd to resolve a given hostname */
90 int dnssd_resolve_host(const struct iio_context_params *params,
91  const char *hostname, char *ip_addr, const int addr_len);
92 
93 int dnssd_context_scan(const struct iio_context_params *params,
94  struct iio_scan *ctx, const char *args);
95 
96 #endif /* __IIO_DNS_SD_H */