-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathminimal_browser.c
454 lines (332 loc) · 12.1 KB
/
minimal_browser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//
// Created by olivier on 11/09/15.
//
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
// IF_NAMESIZE
#include <net/if.h>
#include <avahi-common/simple-watch.h>
#include <avahi-common/error.h>
#include <avahi-common/malloc.h>
#include <avahi-common/domain.h>
#include <avahi-common/llist.h>
#include <avahi-client/client.h>
#include <avahi-client/lookup.h>
#define NO_FAIL 1
#define VERBOSE 0
#define _PARSE 1
#define SERVICE_TYPE "_rtp._tcp"
#define IGNORE_LOCAL 0
#define RESOLVE 1
#define TERMINATE_CACHE 0
#define TERMINATE_ALL_FOR_NOW 1
typedef struct ServiceInfo ServiceInfo;
struct ServiceInfo {
AvahiIfIndex interface;
AvahiProtocol protocol;
char *name, *type, *domain;
AvahiServiceResolver *resolver;
AVAHI_LLIST_FIELDS(ServiceInfo, info);
};
static AvahiSimplePoll *simple_poll = NULL;
static AvahiClient *client = NULL;
static int n_all_for_now = 0, n_cache_exhausted = 0, n_resolving = 0;
static AvahiStringList *browsed_types = NULL;
static ServiceInfo *services = NULL;
static int n_columns = 80;
static int browsing = 0;
static void print_service_line(char c, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain, int nl) {
char ifname[IF_NAMESIZE];
if (_PARSE) {
char sn[AVAHI_DOMAIN_NAME_MAX], *e = sn;
size_t l = sizeof(sn);
printf("%c;%s;%s;%s;%s;%s%s",
c,
interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : "n/a",
protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : "n/a",
avahi_escape_label(name, strlen(name), &e, &l), type, domain, nl ? "\n" : "");
} else {
char label[AVAHI_LABEL_MAX];
//make_printable(name, label);
printf("%c %6s %4s %-*s %-20s %s\n",
c,
interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : "n/a",
protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : "n/a",
n_columns-35, label, type, domain);
}
fflush(stdout);
}
void check_terminate() {
assert(n_all_for_now >= 0);
assert(n_cache_exhausted >= 0);
assert(n_resolving >= 0);
if (n_all_for_now <= 0 && n_resolving <= 0) {
if (VERBOSE) {
printf(": All for now\n");
n_all_for_now++; /* Make sure that this event is not repeated */
}
if(TERMINATE_ALL_FOR_NOW)
avahi_simple_poll_quit(simple_poll);
}
if (n_cache_exhausted <= 0 && n_resolving <= 0) {
if (VERBOSE) {
printf(": Cache exhausted\n");
n_cache_exhausted++; /* Make sure that this event is not repeated */
}
if (TERMINATE_CACHE)
avahi_simple_poll_quit(simple_poll);
}
}
static void service_resolver_callback(
AvahiServiceResolver *r,
AvahiIfIndex interface,
AvahiProtocol protocol,
AvahiResolverEvent event,
const char *name,
const char *type,
const char *domain,
const char *host_name,
const AvahiAddress *a,
uint16_t port,
AvahiStringList *txt,
AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
void *userdata) {
ServiceInfo *i = userdata;
assert(r);
assert(i);
switch (event) {
case AVAHI_RESOLVER_FOUND: {
char address[AVAHI_ADDRESS_STR_MAX], *t;
avahi_address_snprint(address, sizeof(address), a);
t = avahi_string_list_to_string(txt);
print_service_line('=', interface, protocol, name, type, domain, 0);
if (_PARSE)
printf(";%s;%s;%u;%s\n",
host_name,
address,
port,
t);
else
printf(" hostname = [%s]\n"
" address = [%s]\n"
" port = [%u]\n"
" txt = [%s]\n",
host_name,
address,
port,
t);
avahi_free(t);
break;
}
case AVAHI_RESOLVER_FAILURE:
fprintf(stderr, "Failed to resolve service '%s' of type '%s' in domain '%s': %s\n", name, type, domain, avahi_strerror(avahi_client_errno(client)));
break;
}
avahi_service_resolver_free(i->resolver);
i->resolver = NULL;
assert(n_resolving > 0);
n_resolving--;
check_terminate();
fflush(stdout);
}
static ServiceInfo *add_service(AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) {
ServiceInfo *i;
i = avahi_new(ServiceInfo, 1);
if (RESOLVE) {
if (!(i->resolver = avahi_service_resolver_new(client, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, service_resolver_callback, i))) {
avahi_free(i);
fprintf(stderr, "Failed to resolve service '%s' of type '%s' in domain '%s': %s\n", name, type, domain, avahi_strerror(avahi_client_errno(client)));
return NULL;
}
n_resolving++;
} else
i->resolver = NULL;
i->interface = interface;
i->protocol = protocol;
i->name = avahi_strdup(name);
i->type = avahi_strdup(type);
i->domain = avahi_strdup(domain);
AVAHI_LLIST_PREPEND(ServiceInfo, info, services, i);
return i;
}
void remove_service(ServiceInfo *i) {
assert(i);
AVAHI_LLIST_REMOVE(ServiceInfo, info, services, i);
if (i->resolver)
avahi_service_resolver_free(i->resolver);
avahi_free(i->name);
avahi_free(i->type);
avahi_free(i->domain);
avahi_free(i);
}
static ServiceInfo *find_service(AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) {
ServiceInfo *i;
for (i = services; i; i = i->info_next)
if (i->interface == interface &&
i->protocol == protocol &&
strcasecmp(i->name, name) == 0 &&
avahi_domain_equal(i->type, type) &&
avahi_domain_equal(i->domain, domain))
return i;
return NULL;
}
static void service_browser_callback(
AvahiServiceBrowser *b,
AvahiIfIndex interface,
AvahiProtocol protocol,
AvahiBrowserEvent event,
const char *name,
const char *type,
const char *domain,
AvahiLookupResultFlags flags,
void *userdata) {
//Config *c = userdata;
assert(b);
//assert(c);
switch (event) {
case AVAHI_BROWSER_NEW: {
if (IGNORE_LOCAL && (flags & AVAHI_LOOKUP_RESULT_LOCAL))
break;
if (find_service(interface, protocol, name, type, domain))
return;
add_service(interface, protocol, name, type, domain);
//print_service_line('+', interface, protocol, name, type, domain, 1);
break;
}
case AVAHI_BROWSER_REMOVE: {
ServiceInfo *info;
if (!(info = find_service(interface, protocol, name, type, domain)))
return;
remove_service(info);
//print_service_line(c, '-', interface, protocol, name, type, domain, 1);
break;
}
case AVAHI_BROWSER_FAILURE:
fprintf(stderr, "service_browser failed: %s\n", avahi_strerror(avahi_client_errno(client)));
avahi_simple_poll_quit(simple_poll);
break;
case AVAHI_BROWSER_CACHE_EXHAUSTED:
n_cache_exhausted --;
check_terminate();
break;
case AVAHI_BROWSER_ALL_FOR_NOW:
n_all_for_now --;
check_terminate();
break;
}
}
static void browse_service_type(const char *stype, const char *domain) {
AvahiServiceBrowser *b;
AvahiStringList *i;
assert(client);
assert(stype);
for (i = browsed_types; i; i = i->next)
if (avahi_domain_equal(stype, (char*) i->text)){
fprintf(stderr, "Already browsed");
return;}
if (!(b = avahi_service_browser_new(
client,
AVAHI_IF_UNSPEC,
AVAHI_PROTO_UNSPEC,
stype,
domain,
0,
service_browser_callback,
NULL))) {
fprintf(stderr, "avahi_service_browser_new() failed: %s\n", avahi_strerror(avahi_client_errno(client)));
avahi_simple_poll_quit(simple_poll);
}
browsed_types = avahi_string_list_add(browsed_types, stype);
n_all_for_now++;
n_cache_exhausted++;
}
static int start() {
assert(!browsing);
if (VERBOSE) {
const char *version, *hn;
if (!(version = avahi_client_get_version_string(client))) {
fprintf(stderr, "Failed to query version string: %s\n", avahi_strerror(avahi_client_errno(client)));
return -1;
}
if (!(hn = avahi_client_get_host_name_fqdn(client))) {
fprintf(stderr, "Failed to query host name: %s\n", avahi_strerror(avahi_client_errno(client)));
return -1;
}
fprintf(stderr, "Server version: %s; Host name: %s\n", version, hn);
/* Translators: This is a column heading with abbreviations for
* Event (+/-), Network Interface, Protocol (IPv4/v6), Domain */
fprintf(stderr, "E Ifce Prot %-*s %-20s Domain\n", n_columns-35, "Name", "Type");
}
browse_service_type(avahi_strdup(SERVICE_TYPE), NULL);
browsing = 1;
return 0;
}
static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
// Config *config = userdata;
/* This function might be called when avahi_client_new() has not
* returned yet.*/
client = c;
switch (state) {
case AVAHI_CLIENT_FAILURE:
if (NO_FAIL && avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
int error;
/* We have been disconnected, so let reconnect */
fprintf(stderr, "Disconnected, reconnecting ...\n");
avahi_client_free(client);
client = NULL;
avahi_string_list_free(browsed_types);
browsed_types = NULL;
while (services)
remove_service(services);
browsing = 0;
if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), AVAHI_CLIENT_NO_FAIL, client_callback, NULL, &error))) {
fprintf(stderr, "Failed to create client object: %s\n", avahi_strerror(error));
avahi_simple_poll_quit(simple_poll);
}
} else {
fprintf(stderr, "Client failure, exiting: %s\n", avahi_strerror(avahi_client_errno(c)));
avahi_simple_poll_quit(simple_poll);
}
break;
case AVAHI_CLIENT_S_REGISTERING:
case AVAHI_CLIENT_S_RUNNING:
case AVAHI_CLIENT_S_COLLISION:
if (!browsing)
if (start() < 0)
avahi_simple_poll_quit(simple_poll);
break;
case AVAHI_CLIENT_CONNECTING:
//if (config->verbose && !config->parsable)
if(VERBOSE)
fprintf(stderr, "Waiting for daemon ...\n");
break;
}
}
int main(){
int ret = 1, error;
if (!(simple_poll = avahi_simple_poll_new())) {
fprintf(stderr, "Failed to create simple poll object.\n");
goto fail;
}
/* if (sigint_install(simple_poll) < 0)
goto fail;*/
if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), AVAHI_CLIENT_NO_FAIL, client_callback, NULL, &error))) {
fprintf(stderr, "Failed to create client object: %s\n", avahi_strerror(error));
goto fail;
}
avahi_simple_poll_loop(simple_poll);
ret = 0;
fail:
while (services)
remove_service(services);
if (client)
avahi_client_free(client);
//sigint_uninstall();
if (simple_poll)
avahi_simple_poll_free(simple_poll);
//avahi_free(config.domain);
//avahi_free(config.stype);
avahi_string_list_free(browsed_types);
return ret;
}