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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
|
/* $Id$ */
/***
This file is part of avahi.
avahi is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
avahi is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with avahi; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
#include <assert.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <locale.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>
#include "sigint.h"
#ifdef HAVE_GDBM
#include "stdb.h"
#endif
typedef enum {
COMMAND_HELP,
COMMAND_VERSION,
COMMAND_BROWSE_SERVICES,
COMMAND_BROWSE_ALL_SERVICES,
COMMAND_BROWSE_DOMAINS
} Command;
typedef struct Config {
int verbose;
int terminate_on_all_for_now;
int terminate_on_cache_exhausted;
char *domain;
char *stype;
int ignore_local;
Command command;
int resolve;
#ifdef HAVE_GDBM
int no_db_lookup;
#endif
} Config;
typedef struct ServiceInfo ServiceInfo;
struct ServiceInfo {
AvahiIfIndex interface;
AvahiProtocol protocol;
char *name, *type, *domain;
AvahiServiceResolver *resolver;
Config *config;
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 void check_terminate(Config *c) {
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 (c->verbose) {
printf(": All for now\n");
n_all_for_now++; /* Make sure that this event is not repeated */
}
if (c->terminate_on_all_for_now)
avahi_simple_poll_quit(simple_poll);
}
if (n_cache_exhausted <= 0 && n_resolving <= 0) {
if (c->verbose) {
printf(": Cache exhausted\n");
n_cache_exhausted++; /* Make sure that this event is not repeated */
}
if (c->terminate_on_cache_exhausted)
avahi_simple_poll_quit(simple_poll);
}
}
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) == 0 &&
avahi_domain_equal(i->domain, domain) == 0)
return i;
return NULL;
}
static void print_service_line(Config *config, char c, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) {
char ifname[IF_NAMESIZE];
#ifdef HAVE_GDBM
if (!config->no_db_lookup)
type = stdb_lookup(type);
#endif
printf("%c %4s %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, name, type, domain);
}
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(i->config, '=', interface, protocol, name, type, domain);
printf(" hostname = [%s]\n"
" address = [%s]\n"
" port = [%i]\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(i->config);
}
static ServiceInfo *add_service(Config *c, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) {
ServiceInfo *i;
i = avahi_new(ServiceInfo, 1);
if (c->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);
i->config = c;
AVAHI_LLIST_PREPEND(ServiceInfo, info, services, i);
return i;
}
static void remove_service(Config *c, ServiceInfo *i) {
assert(c);
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 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 (c->ignore_local && (flags & AVAHI_LOOKUP_RESULT_LOCAL))
break;
if (find_service(interface, protocol, name, type, domain))
return;
add_service(c, interface, protocol, name, type, domain);
print_service_line(c, '+', interface, protocol, name, type, domain);
break;
}
case AVAHI_BROWSER_REMOVE: {
ServiceInfo *info;
if (!(info = find_service(interface, protocol, name, type, domain)))
return;
remove_service(c, info);
print_service_line(c, '-', interface, protocol, name, type, domain);
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(c);
break;
case AVAHI_BROWSER_ALL_FOR_NOW:
n_all_for_now --;
check_terminate(c);
break;
}
}
static void browse_service_type(Config *c, const char *stype, const char *domain) {
AvahiServiceBrowser *b;
AvahiStringList *i;
assert(c);
assert(client);
assert(stype);
for (i = browsed_types; i; i = i->next)
if (avahi_domain_equal(stype, (char*) i->text))
return;
if (!(b = avahi_service_browser_new(
client,
AVAHI_IF_UNSPEC,
AVAHI_PROTO_UNSPEC,
stype,
domain,
0,
service_browser_callback,
c))) {
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 void service_type_browser_callback(
AvahiServiceTypeBrowser *b,
AVAHI_GCC_UNUSED AvahiIfIndex interface,
AVAHI_GCC_UNUSED AvahiProtocol protocol,
AvahiBrowserEvent event,
const char *type,
const char *domain,
AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
void *userdata) {
Config *c = userdata;
assert(b);
assert(c);
switch (event) {
case AVAHI_BROWSER_NEW:
browse_service_type(c, type, domain);
break;
case AVAHI_BROWSER_REMOVE:
/* We're dirty and never remove the browser again */
break;
case AVAHI_BROWSER_FAILURE:
fprintf(stderr, "service_type_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(c);
break;
case AVAHI_BROWSER_ALL_FOR_NOW:
n_all_for_now --;
check_terminate(c);
break;
}
}
static void browse_all(Config *c) {
AvahiServiceTypeBrowser *b;
assert(c);
if (!(b = avahi_service_type_browser_new(
client,
AVAHI_IF_UNSPEC,
AVAHI_PROTO_UNSPEC,
c->domain,
0,
service_type_browser_callback,
c))) {
fprintf(stderr, "avahi_service_type_browser_new() failed: %s\n", avahi_strerror(avahi_client_errno(client)));
avahi_simple_poll_quit(simple_poll);
}
n_cache_exhausted++;
n_all_for_now++;
}
static void domain_browser_callback(
AvahiDomainBrowser *b,
AVAHI_GCC_UNUSED AvahiIfIndex interface,
AVAHI_GCC_UNUSED AvahiProtocol protocol,
AvahiBrowserEvent event,
const char *domain,
AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
void *userdata) {
Config *c = userdata;
assert(b);
assert(c);
switch (event) {
case AVAHI_BROWSER_NEW:
case AVAHI_BROWSER_REMOVE: {
char ifname[IF_NAMESIZE];
printf("%c %4s %4s %s\n",
event == AVAHI_BROWSER_NEW ? '+' : '-',
interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : "n/a",
protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : "n/a",
domain);
break;
}
case AVAHI_BROWSER_FAILURE:
fprintf(stderr, "domain_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(c);
break;
case AVAHI_BROWSER_ALL_FOR_NOW:
n_all_for_now --;
check_terminate(c);
break;
}
}
static void browse_domains(Config *c) {
AvahiDomainBrowser *b;
assert(c);
if (!(b = avahi_domain_browser_new(
client,
AVAHI_IF_UNSPEC,
AVAHI_PROTO_UNSPEC,
c->domain,
AVAHI_DOMAIN_BROWSER_BROWSE,
0,
domain_browser_callback,
c))) {
fprintf(stderr, "avahi_domain_browser_new() failed: %s\n", avahi_strerror(avahi_client_errno(client)));
avahi_simple_poll_quit(simple_poll);
}
n_cache_exhausted++;
n_all_for_now++;
}
static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
switch (state) {
case AVAHI_CLIENT_FAILURE:
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:
;
}
}
static void help(FILE *f, const char *argv0) {
fprintf(f,
"%s [options] <service type>\n"
"%s [options] -a\n"
"%s [options] -D\n\n"
" -h --help Show this help\n"
" -V --version Show version\n"
" -D --browse-domains Browse for browsing domains instead of services\n"
" -a --all Show all services, regardless of the type\n"
" -d --domain=DOMAIN The domain to browse in\n"
" -v --verbose Enable verbose mode\n"
" -t --terminate Terminate after dumping a more or less complete list\n"
" -c --cache Terminate after dumping all entries from the cache\n"
" -l --ignore-local Ignore local services\n"
" -r --resolve Resolve services found\n"
#ifdef HAVE_GDBM
" -k --no-db-lookup Don't lookup service types\n"
#endif
, argv0, argv0, argv0);
}
static int parse_command_line(Config *c, int argc, char *argv[]) {
int o;
static const struct option long_options[] = {
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'V' },
{ "browse-domains", no_argument, NULL, 'D' },
{ "domain", required_argument, NULL, 'd' },
{ "all", no_argument, NULL, 'a' },
{ "verbose", no_argument, NULL, 'v' },
{ "terminate", no_argument, NULL, 't' },
{ "cache", no_argument, NULL, 'c' },
{ "ignore-local", no_argument, NULL, 'l' },
{ "resolve", no_argument, NULL, 'r' },
#ifdef HAVE_GDBM
{ "no-db-lookup", no_argument, NULL, 'k' },
#endif
{ NULL, 0, NULL, 0 }
};
assert(c);
c->command = COMMAND_BROWSE_SERVICES;
c->verbose =
c->terminate_on_cache_exhausted =
c->terminate_on_all_for_now =
c->ignore_local =
c->resolve = 0;
c->domain = c->stype = NULL;
#ifdef HAVE_GDBM
c->no_db_lookup = 0;
#endif
opterr = 0;
while ((o = getopt_long(argc, argv, "hVd:avtclrD"
#ifdef HAVE_GDBM
"k"
#endif
, long_options, NULL)) >= 0) {
switch(o) {
case 'h':
c->command = COMMAND_HELP;
break;
case 'V':
c->command = COMMAND_VERSION;
break;
case 'a':
c->command = COMMAND_BROWSE_ALL_SERVICES;
break;
case 'D':
c->command = COMMAND_BROWSE_DOMAINS;
break;
case 'd':
c->domain = avahi_strdup(optarg);
break;
case 'v':
c->verbose = 1;
break;
case 't':
c->terminate_on_all_for_now = 1;
break;
case 'c':
c->terminate_on_cache_exhausted = 1;
break;
case 'l':
c->ignore_local = 1;
break;
case 'r':
c->resolve = 1;
break;
#ifdef HAVE_GDBM
case 'k':
c->no_db_lookup = 1;
break;
#endif
default:
fprintf(stderr, "Invalid command line argument: %c\n", o);
return -1;
}
}
if (c->command == COMMAND_BROWSE_SERVICES) {
if (optind >= argc) {
fprintf(stderr, "Too few arguments\n");
return -1;
}
c->stype = avahi_strdup(argv[optind]);
optind++;
}
if (optind < argc) {
fprintf(stderr, "Too many arguments\n");
return -1;
}
return 0;
}
int main(int argc, char *argv[]) {
int ret = 1, error;
Config config;
const char *argv0;
char *ec;
setlocale(LC_ALL, "");
if ((argv0 = strrchr(argv[0], '/')))
argv0++;
else
argv0 = argv[0];
if ((ec = getenv("COLUMNS")))
n_columns = atoi(ec);
if (n_columns < 40)
n_columns = 40;
if (parse_command_line(&config, argc, argv) < 0)
goto fail;
switch (config.command) {
case COMMAND_HELP:
help(stdout, argv0);
ret = 0;
break;
case COMMAND_VERSION:
printf("%s "PACKAGE_VERSION"\n", argv0);
ret = 0;
break;
case COMMAND_BROWSE_SERVICES:
case COMMAND_BROWSE_ALL_SERVICES:
case COMMAND_BROWSE_DOMAINS:
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), client_callback, NULL, &error))) {
fprintf(stderr, "Failed to create client object: %s\n", avahi_strerror(error));
goto fail;
}
if (config.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)));
goto fail;
}
if (!(hn = avahi_client_get_host_name_fqdn(client))) {
fprintf(stderr, "Failed to query host name: %s\n", avahi_strerror(avahi_client_errno(client)));
goto fail;
}
fprintf(stderr, "Server version: %s; Host name: %s\n", version, hn);
if (config.command == COMMAND_BROWSE_DOMAINS)
fprintf(stderr, "E Ifce Prot Domain\n");
else
fprintf(stderr, "E Ifce Prot %-*s %-20s Domain\n", n_columns-35, "Name", "Type");
}
if (config.command == COMMAND_BROWSE_SERVICES)
browse_service_type(&config, config.stype, config.domain);
else if (config.command == COMMAND_BROWSE_ALL_SERVICES)
browse_all(&config);
else {
assert(config.command == COMMAND_BROWSE_DOMAINS);
browse_domains(&config);
}
avahi_simple_poll_loop(simple_poll);
ret = 0;
break;
}
fail:
while (services)
remove_service(&config, 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);
#ifdef HAVE_GDBM
stdb_shutdown();
#endif
return ret;
}
|