Skip to content

Commit 5fd8fce

Browse files
committed
dns-sd: avahi: Code cleanup
- Fix misplaced \n in debug print - Simplify new_discovery_data() - List one function parameter per line, as these functions take a lot of arguments - Remove __notused, since we always compile with -Wno-unused-parameter - Avoid a(b(c(d))), this is C, not Lisp - Replace "i = 0; while(xxx) { ... ; i++ }" with a for loop - Put arguments of long log messages to a new line - Properly align code with tabs and complete with chars Signed-off-by: Paul Cercueil <[email protected]>
1 parent b391071 commit 5fd8fce

File tree

1 file changed

+122
-94
lines changed

1 file changed

+122
-94
lines changed

dns_sd_avahi.c

Lines changed: 122 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -33,54 +33,57 @@
3333
* potential clients on the network
3434
*/
3535

36-
static int new_discovery_data(struct dns_sd_discovery_data **data)
36+
static struct dns_sd_discovery_data *new_discovery_data(void)
3737
{
3838
struct dns_sd_discovery_data *d;
3939

40-
d = zalloc(sizeof(struct dns_sd_discovery_data));
40+
d = zalloc(sizeof(*d));
4141
if (!d)
42-
return -ENOMEM;
42+
return NULL;
4343

44-
d->address = zalloc(sizeof(struct AvahiAddress));
44+
d->address = zalloc(sizeof(*d->address));
4545
if (!d->address) {
4646
free(d);
47-
return -ENOMEM;
47+
return NULL;
4848
}
4949

50-
*data = d;
51-
return 0;
50+
return d;
5251
}
5352

54-
static void avahi_process_resolved(struct dns_sd_discovery_data *ddata, const AvahiAddress *addr,
55-
const char *host_name, const uint16_t port)
53+
static void avahi_process_resolved(struct dns_sd_discovery_data *ddata,
54+
const AvahiAddress *addr,
55+
const char *host_name,
56+
const uint16_t port)
5657
{
57-
/* Avahi is multi-threaded, so lock the list */
58-
iio_mutex_lock(ddata->lock);
59-
ddata->resolved++;
60-
61-
/* Find empty data to store things*/
62-
while (ddata->next)
63-
ddata = ddata->next;
64-
65-
/* link a new placeholder to the list */
66-
avahi_address_snprint(ddata->addr_str,
67-
sizeof(ddata->addr_str), addr);
68-
memcpy(ddata->address, addr, sizeof(*ddata->address)); /* Flawfinder: ignore */
69-
ddata->port = port;
70-
ddata->hostname = strdup(host_name);
71-
ddata->resolved = true;
72-
/* link a new, empty placeholder to the list */
73-
if (!new_discovery_data(&ddata->next)) {
74-
/* duplicate poll & lock info,
75-
* since we don't know which might be discarded */
76-
ddata->next->poll = ddata->poll;
77-
ddata->next->lock = ddata->lock;
78-
} else {
79-
IIO_ERROR("Avahi Resolver : memory failure\n");
80-
}
81-
iio_mutex_unlock(ddata->lock);
82-
83-
IIO_DEBUG("\t\t%s:%u (%s)\n", host_name, port, ddata->addr_str);
58+
/* Avahi is multi-threaded, so lock the list */
59+
iio_mutex_lock(ddata->lock);
60+
ddata->resolved++;
61+
62+
/* Find empty data to store things*/
63+
while (ddata->next)
64+
ddata = ddata->next;
65+
66+
/* link a new placeholder to the list */
67+
avahi_address_snprint(ddata->addr_str,
68+
sizeof(ddata->addr_str), addr);
69+
memcpy(ddata->address, addr, sizeof(*ddata->address)); /* Flawfinder: ignore */
70+
ddata->port = port;
71+
ddata->hostname = strdup(host_name);
72+
ddata->resolved = true;
73+
74+
/* link a new, empty placeholder to the list */
75+
ddata->next = new_discovery_data();
76+
if (ddata->next) {
77+
/* duplicate poll & lock info,
78+
* since we don't know which might be discarded */
79+
ddata->next->poll = ddata->poll;
80+
ddata->next->lock = ddata->lock;
81+
} else {
82+
IIO_ERROR("Avahi Resolver : memory failure\n");
83+
}
84+
iio_mutex_unlock(ddata->lock);
85+
86+
IIO_DEBUG("\t\t%s:%u (%s)\n", host_name, port, ddata->addr_str);
8487
}
8588

8689
/*
@@ -90,14 +93,22 @@ static void avahi_process_resolved(struct dns_sd_discovery_data *ddata, const Av
9093
*/
9194

9295
static void __avahi_resolver_cb(AvahiServiceResolver *resolver,
93-
__notused AvahiIfIndex iface, __notused AvahiProtocol proto,
94-
AvahiResolverEvent event, const char *name,
95-
const char *type, const char *domain,
96-
const char *host_name, const AvahiAddress *address,
97-
uint16_t port, AvahiStringList *txt,
98-
__notused AvahiLookupResultFlags flags, void *d)
96+
AvahiIfIndex iface,
97+
AvahiProtocol proto,
98+
AvahiResolverEvent event,
99+
const char *name,
100+
const char *type,
101+
const char *domain,
102+
const char *host_name,
103+
const AvahiAddress *address,
104+
uint16_t port,
105+
AvahiStringList *txt,
106+
AvahiLookupResultFlags flags,
107+
void *d)
99108
{
100-
struct dns_sd_discovery_data *ddata = (struct dns_sd_discovery_data *) d;
109+
struct dns_sd_discovery_data *ddata = d;
110+
AvahiClient *client;
111+
int err;
101112

102113
if (!resolver) {
103114
IIO_ERROR("Fatal Error in Avahi Resolver\n");
@@ -106,36 +117,44 @@ static void __avahi_resolver_cb(AvahiServiceResolver *resolver,
106117

107118
switch(event) {
108119
case AVAHI_RESOLVER_FAILURE:
120+
client = avahi_service_resolver_get_client(resolver);
121+
err = avahi_client_errno(client);
122+
109123
IIO_ERROR("Avahi Resolver: Failed resolve service '%s' "
110-
"of type '%s' in domain '%s': %s\n",
111-
name, type, domain,
112-
avahi_strerror(
113-
avahi_client_errno(
114-
avahi_service_resolver_get_client(
115-
resolver))));
124+
"of type '%s' in domain '%s': %s\n",
125+
name, type, domain,
126+
avahi_strerror(err));
116127
break;
117128
case AVAHI_RESOLVER_FOUND: {
118129
avahi_process_resolved(ddata, address, host_name, port);
119130
IIO_DEBUG("Avahi Resolver : service '%s' of type '%s' in domain '%s':\n",
120-
name, type, domain);
131+
name, type, domain);
121132
break;
122-
}
133+
}
123134
}
124135
avahi_service_resolver_free(resolver);
125136
}
126137

127-
static void avahi_host_resolver(AvahiHostNameResolver *resolver, __notused AvahiIfIndex iface,
128-
__notused AvahiProtocol proto, AvahiResolverEvent event,
129-
const char *host_name, const AvahiAddress *address,
130-
__notused AvahiLookupResultFlags flags, void *d)
138+
static void avahi_host_resolver(AvahiHostNameResolver *resolver,
139+
AvahiIfIndex iface,
140+
AvahiProtocol proto,
141+
AvahiResolverEvent event,
142+
const char *host_name,
143+
const AvahiAddress *address,
144+
AvahiLookupResultFlags flags,
145+
void *d)
131146
{
132-
struct dns_sd_discovery_data *ddata = (struct dns_sd_discovery_data *) d;
147+
struct dns_sd_discovery_data *ddata = d;
148+
AvahiClient *client;
149+
int err;
133150

134151
switch(event) {
135152
case AVAHI_RESOLVER_FAILURE:
136-
IIO_ERROR("Avahi Resolver: Failed to resolve host '%s' : %s\n", host_name,
137-
avahi_strerror(avahi_client_errno(
138-
avahi_host_name_resolver_get_client(resolver))));
153+
client = avahi_host_name_resolver_get_client(resolver);
154+
err = avahi_client_errno(client);
155+
156+
IIO_ERROR("Avahi Resolver: Failed to resolve host '%s' : %s\n",
157+
host_name, avahi_strerror(err));
139158
break;
140159
case AVAHI_RESOLVER_FOUND:
141160
avahi_process_resolved(ddata, address, host_name, IIOD_PORT);
@@ -147,14 +166,22 @@ static void avahi_host_resolver(AvahiHostNameResolver *resolver, __notused Avahi
147166
}
148167

149168
static void __avahi_browser_cb(AvahiServiceBrowser *browser,
150-
AvahiIfIndex iface, AvahiProtocol proto,
151-
AvahiBrowserEvent event, const char *name,
152-
const char *type, const char *domain,
153-
__notused AvahiLookupResultFlags flags, void *d)
169+
AvahiIfIndex iface,
170+
AvahiProtocol proto,
171+
AvahiBrowserEvent event,
172+
const char *name,
173+
const char *type,
174+
const char *domain,
175+
AvahiLookupResultFlags flags,
176+
void *d)
154177
{
155178
struct dns_sd_discovery_data *ddata = (struct dns_sd_discovery_data *) d;
156179
struct AvahiClient *client = avahi_service_browser_get_client(browser);
157-
int i;
180+
unsigned int i;
181+
struct timespec ts;
182+
183+
ts.tv_sec = 0;
184+
ts.tv_nsec = 5e6; /* 5ms in ns */
158185

159186
if (!browser) {
160187
IIO_ERROR("Fatal Error in Avahi Browser\n");
@@ -163,18 +190,16 @@ static void __avahi_browser_cb(AvahiServiceBrowser *browser,
163190

164191
switch (event) {
165192
case AVAHI_BROWSER_REMOVE:
166-
IIO_DEBUG("Avahi Browser : REMOVE : "
167-
"service '%s' of type '%s' in domain '%s'\n",
168-
name, type, domain);
193+
IIO_DEBUG("Avahi Browser : REMOVE : service '%s' of type '%s' in domain '%s'\n",
194+
name, type, domain);
169195
break;
170196
case AVAHI_BROWSER_NEW:
171-
IIO_DEBUG("Avahi Browser : NEW: "
172-
"service '%s' of type '%s' in domain '%s'\n",
173-
name, type, domain);
197+
IIO_DEBUG("Avahi Browser : NEW: service '%s' of type '%s' in domain '%s'\n",
198+
name, type, domain);
174199
if(!avahi_service_resolver_new(client, iface,
175-
proto, name, type, domain,
176-
AVAHI_PROTO_UNSPEC, 0,
177-
__avahi_resolver_cb, d)) {
200+
proto, name, type, domain,
201+
AVAHI_PROTO_UNSPEC, 0,
202+
__avahi_resolver_cb, d)) {
178203
IIO_ERROR("Failed to resolve service '%s\n", name);
179204
} else {
180205
iio_mutex_lock(ddata->lock);
@@ -183,18 +208,13 @@ static void __avahi_browser_cb(AvahiServiceBrowser *browser,
183208
}
184209
break;
185210
case AVAHI_BROWSER_ALL_FOR_NOW:
186-
/* Wait for a max of 1 second */
187-
i = 0;
188211
IIO_DEBUG("Avahi Browser : ALL_FOR_NOW Browser : %d, Resolved : %d\n",
189-
ddata->found, ddata->resolved);
212+
ddata->found, ddata->resolved);
213+
190214
/* 200 * 5ms = wait 1 second */
191-
while ((ddata->found != ddata->resolved) && i <= 200) {
192-
struct timespec ts;
193-
ts.tv_sec = 0;
194-
ts.tv_nsec = 5e6; /* 5ms in ns*/
215+
for (i = 0; ddata->found != ddata->resolved && i <= 200; i++)
195216
nanosleep(&ts, NULL);
196-
i++;
197-
}
217+
198218
avahi_simple_poll_quit(ddata->poll);
199219
break;
200220
case AVAHI_BROWSER_FAILURE:
@@ -220,7 +240,8 @@ int dnssd_find_hosts(struct dns_sd_discovery_data **ddata)
220240
AvahiServiceBrowser *browser;
221241
int ret = 0;
222242

223-
if (new_discovery_data(&d) < 0)
243+
d = new_discovery_data();
244+
if (!d)
224245
return -ENOMEM;
225246

226247
d->lock = iio_mutex_create();
@@ -237,20 +258,21 @@ int dnssd_find_hosts(struct dns_sd_discovery_data **ddata)
237258
}
238259

239260
client = avahi_client_new(avahi_simple_poll_get(d->poll),
240-
0, NULL, NULL, &ret);
261+
0, NULL, NULL, &ret);
241262
if (!client) {
242263
IIO_ERROR("Unable to create Avahi DNS-SD client :%s\n",
243-
avahi_strerror(ret));
264+
avahi_strerror(ret));
244265
goto err_free_poll;
245266
}
246267

247268
browser = avahi_service_browser_new(client,
248-
AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
249-
"_iio._tcp", NULL, 0, __avahi_browser_cb, d);
269+
AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
270+
"_iio._tcp", NULL, 0,
271+
__avahi_browser_cb, d);
250272
if (!browser) {
251273
ret = avahi_client_errno(client);
252274
IIO_ERROR("Unable to create Avahi DNS-SD browser: %s\n",
253-
avahi_strerror(ret));
275+
avahi_strerror(ret));
254276
goto err_free_client;
255277
}
256278

@@ -274,7 +296,8 @@ int dnssd_find_hosts(struct dns_sd_discovery_data **ddata)
274296
return ret;
275297
}
276298

277-
static void avahi_resolve_host(struct dns_sd_discovery_data *d, const char *hostname,
299+
static void avahi_resolve_host(struct dns_sd_discovery_data *d,
300+
const char *hostname,
278301
const AvahiProtocol proto)
279302
{
280303
AvahiClient *client;
@@ -287,19 +310,21 @@ static void avahi_resolve_host(struct dns_sd_discovery_data *d, const char *host
287310

288311
client = avahi_client_new(avahi_simple_poll_get(d->poll), 0, NULL, NULL, &ret);
289312
if (!client) {
290-
IIO_ERROR("Unable to create Avahi DNS-SD client :%s\n", avahi_strerror(ret));
313+
IIO_ERROR("Unable to create Avahi DNS-SD client :%s\n",
314+
avahi_strerror(ret));
291315
goto err_free_poll;
292316
}
293317

294318
resolver = avahi_host_name_resolver_new(client, AVAHI_IF_UNSPEC, proto, hostname, proto, 0,
295319
avahi_host_resolver, d);
296320
if (!resolver) {
297321
ret = avahi_client_errno(client);
298-
IIO_ERROR("Unable to create Avahi DNS-SD browser: %s\n", avahi_strerror(ret));
322+
IIO_ERROR("Unable to create Avahi DNS-SD browser: %s\n",
323+
avahi_strerror(ret));
299324
goto err_free_client;
300325
}
301326

302-
IIO_DEBUG("Trying to resolve host: %s\n, proto: %d", hostname, proto);
327+
IIO_DEBUG("Trying to resolve host: %s, proto: %d\n", hostname, proto);
303328
avahi_simple_poll_loop(d->poll);
304329

305330
err_free_client:
@@ -308,15 +333,18 @@ static void avahi_resolve_host(struct dns_sd_discovery_data *d, const char *host
308333
avahi_simple_poll_free(d->poll);
309334
}
310335

311-
int dnssd_resolve_host(const char *hostname, char *ip_addr, const int addr_len)
336+
int dnssd_resolve_host(const char *hostname,
337+
char *ip_addr,
338+
const int addr_len)
312339
{
313340
struct dns_sd_discovery_data *d;
314341
int ret = 0;
315342

316343
if (!hostname || hostname[0] == '\0')
317344
return -EINVAL;
318345

319-
if (new_discovery_data(&d) < 0)
346+
d = new_discovery_data();
347+
if (!d)
320348
return -ENOMEM;
321349

322350
d->lock = iio_mutex_create();

0 commit comments

Comments
 (0)