Skip to content

Commit 9786096

Browse files
nunojsapcercuei
authored andcommitted
dns-sd: add function to process a discovered avahi host
The need of a dedicated function to process an avahi host will be visible in the next patch where another user of this function will appear. Otherwise, we would be just replicating code. Signed-off-by: Nuno Sa <[email protected]>
1 parent 8e7f5d7 commit 9786096

File tree

1 file changed

+33
-28
lines changed

1 file changed

+33
-28
lines changed

dns_sd_avahi.c

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,38 @@ void dnssd_free_discovery_data(struct dns_sd_discovery_data *d)
5858
free(d);
5959
}
6060

61+
static void avahi_process_resolved(struct dns_sd_discovery_data *ddata, const AvahiAddress *addr,
62+
const char *host_name, const uint16_t port)
63+
{
64+
/* Avahi is multi-threaded, so lock the list */
65+
iio_mutex_lock(ddata->lock);
66+
ddata->resolved++;
67+
68+
/* Find empty data to store things*/
69+
while (ddata->next)
70+
ddata = ddata->next;
71+
72+
/* link a new placeholder to the list */
73+
avahi_address_snprint(ddata->addr_str,
74+
sizeof(ddata->addr_str), addr);
75+
memcpy(ddata->address, addr, sizeof(*ddata->address)); /* Flawfinder: ignore */
76+
ddata->port = port;
77+
ddata->hostname = strdup(host_name);
78+
ddata->resolved = true;
79+
/* link a new, empty placeholder to the list */
80+
if (!new_discovery_data(&ddata->next)) {
81+
/* duplicate poll & lock info,
82+
* since we don't know which might be discarded */
83+
ddata->next->poll = ddata->poll;
84+
ddata->next->lock = ddata->lock;
85+
} else {
86+
IIO_ERROR("Avahi Resolver : memory failure\n");
87+
}
88+
iio_mutex_unlock(ddata->lock);
89+
90+
IIO_DEBUG("\t\t%s:%u (%s)\n", host_name, port, ddata->addr_str);
91+
}
92+
6193
/*
6294
* libavahi callbacks for browser and resolver
6395
* for more info, check out libavahi docs at:
@@ -90,36 +122,9 @@ static void __avahi_resolver_cb(AvahiServiceResolver *resolver,
90122
resolver))));
91123
break;
92124
case AVAHI_RESOLVER_FOUND: {
93-
/* Avahi is multi-threaded, so lock the list */
94-
iio_mutex_lock(ddata->lock);
95-
ddata->resolved++;
96-
97-
/* Find empty data to store things*/
98-
while (ddata->next)
99-
ddata = ddata->next;
100-
101-
/* link a new placeholder to the list */
102-
avahi_address_snprint(ddata->addr_str,
103-
sizeof(ddata->addr_str), address);
104-
memcpy(ddata->address, address, sizeof(*address));
105-
ddata->port = port;
106-
ddata->hostname = strdup(host_name);
107-
ddata->resolved = true;
108-
/* link a new, empty placeholder to the list */
109-
if (!new_discovery_data(&ddata->next)) {
110-
/* duplicate poll & lock info,
111-
* since we don't know which might be discarded */
112-
ddata->next->poll = ddata->poll;
113-
ddata->next->lock = ddata->lock;
114-
} else {
115-
IIO_ERROR("Avahi Resolver : memory failure\n");
116-
}
117-
iio_mutex_unlock(ddata->lock);
118-
125+
avahi_process_resolved(ddata, address, host_name, port);
119126
IIO_DEBUG("Avahi Resolver : service '%s' of type '%s' in domain '%s':\n",
120127
name, type, domain);
121-
IIO_DEBUG("\t\t%s:%u (%s)\n", host_name, port, ddata->addr_str);
122-
123128
break;
124129
}
125130
}

0 commit comments

Comments
 (0)