]> git.meshlink.io Git - catta/blob - avahi-utils/avahi-publish.c
c9c6bc3761f89620e97feded97cabc0c3b7911f0
[catta] / avahi-utils / avahi-publish.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5
6   avahi is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as
8   published by the Free Software Foundation; either version 2.1 of the
9   License, or (at your option) any later version.
10
11   avahi is distributed in the hope that it will be useful, but WITHOUT
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14   Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with avahi; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <getopt.h>
29 #include <assert.h>
30 #include <string.h>
31 #include <sys/types.h>
32 #include <errno.h>
33 #include <locale.h>
34
35 #include <avahi-common/simple-watch.h>
36 #include <avahi-common/error.h>
37 #include <avahi-common/malloc.h>
38 #include <avahi-common/alternative.h>
39 #include <avahi-common/i18n.h>
40 #include <avahi-client/client.h>
41 #include <avahi-client/publish.h>
42
43 #include "sigint.h"
44
45 typedef enum {
46     COMMAND_UNSPEC,
47     COMMAND_HELP,
48     COMMAND_VERSION,
49     COMMAND_PUBLISH_SERVICE,
50     COMMAND_PUBLISH_ADDRESS
51 } Command;
52
53 typedef struct Config {
54     int verbose, no_fail, no_reverse;
55     Command command;
56     char *name, *stype, *domain, *host;
57     uint16_t port;
58     AvahiStringList *txt, *subtypes;
59     AvahiAddress address;
60 } Config;
61
62 static AvahiSimplePoll *simple_poll = NULL;
63 static AvahiClient *client = NULL;
64 static AvahiEntryGroup *entry_group = NULL;
65
66 static int register_stuff(Config *config);
67
68 static void entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
69     Config *config = userdata;
70
71     assert(g);
72     assert(config);
73
74     switch (state) {
75
76         case AVAHI_ENTRY_GROUP_ESTABLISHED:
77
78             fprintf(stderr, _("Established under name '%s'\n"), config->name);
79             break;
80
81         case AVAHI_ENTRY_GROUP_FAILURE:
82
83             fprintf(stderr, _("Failed to register: %s\n"), avahi_strerror(avahi_client_errno(client)));
84             break;
85
86         case AVAHI_ENTRY_GROUP_COLLISION: {
87             char *n;
88
89             if (config->command == COMMAND_PUBLISH_SERVICE)
90                 n = avahi_alternative_service_name(config->name);
91             else {
92                 assert(config->command == COMMAND_PUBLISH_ADDRESS);
93                 n = avahi_alternative_host_name(config->name);
94             }
95
96             fprintf(stderr, _("Name collision, picking new name '%s'.\n"), n);
97             avahi_free(config->name);
98             config->name = n;
99
100             register_stuff(config);
101
102             break;
103         }
104
105         case AVAHI_ENTRY_GROUP_UNCOMMITED:
106         case AVAHI_ENTRY_GROUP_REGISTERING:
107             ;
108     }
109 }
110
111 static int register_stuff(Config *config) {
112     assert(config);
113
114     if (!entry_group) {
115         if (!(entry_group = avahi_entry_group_new(client, entry_group_callback, config))) {
116             fprintf(stderr, _("Failed to create entry group: %s\n"), avahi_strerror(avahi_client_errno(client)));
117             return -1;
118         }
119     }
120
121     assert(avahi_entry_group_is_empty(entry_group));
122
123     if (config->command == COMMAND_PUBLISH_ADDRESS) {
124
125         if (avahi_entry_group_add_address(entry_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, config->no_reverse ? AVAHI_PUBLISH_NO_REVERSE : 0, config->name, &config->address) < 0) {
126             fprintf(stderr, _("Failed to add address: %s\n"), avahi_strerror(avahi_client_errno(client)));
127             return -1;
128         }
129
130     } else {
131         AvahiStringList *i;
132
133         assert(config->command == COMMAND_PUBLISH_SERVICE);
134
135         if (avahi_entry_group_add_service_strlst(entry_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, config->name, config->stype, config->domain, config->host, config->port, config->txt) < 0) {
136             fprintf(stderr, _("Failed to add service: %s\n"), avahi_strerror(avahi_client_errno(client)));
137             return -1;
138         }
139
140         for (i = config->subtypes; i; i = i->next)
141             if (avahi_entry_group_add_service_subtype(entry_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, config->name, config->stype, config->domain, (char*) i->text) < 0) {
142                 fprintf(stderr, _("Failed to add subtype '%s': %s\n"), i->text, avahi_strerror(avahi_client_errno(client)));
143                 return -1;
144             }
145     }
146
147     avahi_entry_group_commit(entry_group);
148
149     return 0;
150 }
151
152 static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
153     Config *config = userdata;
154
155     client = c;
156
157     switch (state) {
158         case AVAHI_CLIENT_FAILURE:
159
160             if (config->no_fail && avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
161                 int error;
162
163                 /* We have been disconnected, so let reconnect */
164
165                 fprintf(stderr, _("Disconnected, reconnecting ...\n"));
166
167                 avahi_client_free(client);
168                 client = NULL;
169                 entry_group = NULL;
170
171                 if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), AVAHI_CLIENT_NO_FAIL, client_callback, config, &error))) {
172                     fprintf(stderr, _("Failed to create client object: %s\n"), avahi_strerror(error));
173                     avahi_simple_poll_quit(simple_poll);
174                 }
175
176             } else {
177                 fprintf(stderr, _("Client failure, exiting: %s\n"), avahi_strerror(avahi_client_errno(c)));
178                 avahi_simple_poll_quit(simple_poll);
179             }
180
181             break;
182
183         case AVAHI_CLIENT_S_RUNNING:
184
185             if (register_stuff(config) < 0)
186                 avahi_simple_poll_quit(simple_poll);
187
188             break;
189
190         case AVAHI_CLIENT_S_COLLISION:
191
192             if (config->verbose)
193                 fprintf(stderr, _("Host name conflict\n"));
194
195             /* Fall through */
196
197         case AVAHI_CLIENT_S_REGISTERING:
198
199             if (entry_group) {
200                 avahi_entry_group_free(entry_group);
201                 entry_group = NULL;
202             }
203             break;
204
205         case AVAHI_CLIENT_CONNECTING:
206
207             if (config->verbose)
208                 fprintf(stderr, _("Waiting for daemon ...\n"));
209
210             break;
211
212             ;
213     }
214 }
215
216 static void help(FILE *f, const char *argv0) {
217     fprintf(f,
218             _("%s [options] %s <name> <type> <port> [<txt ...>]\n"
219               "%s [options] %s <host-name> <address>\n\n"
220               "    -h --help            Show this help\n"
221               "    -V --version         Show version\n"
222               "    -s --service         Publish service\n"
223               "    -a --address         Publish address\n"
224               "    -v --verbose         Enable verbose mode\n"
225               "    -d --domain=DOMAIN   Domain to publish service in\n"
226               "    -H --host=DOMAIN     Host where service resides\n"
227               "       --subtype=SUBTYPE An additional subtype to register this service with\n"
228               "    -R --no-reverse      Do not publish reverse entry with address\n"
229               "    -f --no-fail         Don't fail if the daemon is not available\n"),
230               argv0, strstr(argv0, "service") ? "[-s]" : "-s",
231               argv0, strstr(argv0, "address") ? "[-a]" : "-a");
232 }
233
234 static int parse_command_line(Config *c, const char *argv0, int argc, char *argv[]) {
235     int o;
236
237     enum {
238         ARG_SUBTYPE = 256
239     };
240
241     static const struct option long_options[] = {
242         { "help",           no_argument,       NULL, 'h' },
243         { "version",        no_argument,       NULL, 'V' },
244         { "service",        no_argument,       NULL, 's' },
245         { "address",        no_argument,       NULL, 'a' },
246         { "verbose",        no_argument,       NULL, 'v' },
247         { "domain",         required_argument, NULL, 'd' },
248         { "host",           required_argument, NULL, 'H' },
249         { "subtype",        required_argument, NULL, ARG_SUBTYPE},
250         { "no-reverse",     no_argument,       NULL, 'R' },
251         { "no-fail",        no_argument,       NULL, 'f' },
252         { NULL, 0, NULL, 0 }
253     };
254
255     assert(c);
256
257     c->command = strstr(argv0, "address") ? COMMAND_PUBLISH_ADDRESS : (strstr(argv0, "service") ? COMMAND_PUBLISH_SERVICE : COMMAND_UNSPEC);
258     c->verbose = c->no_fail = c->no_reverse = 0;
259     c->host = c->name = c->domain = c->stype = NULL;
260     c->port = 0;
261     c->txt = c->subtypes = NULL;
262
263     while ((o = getopt_long(argc, argv, "hVsavRd:H:f", long_options, NULL)) >= 0) {
264
265         switch(o) {
266             case 'h':
267                 c->command = COMMAND_HELP;
268                 break;
269             case 'V':
270                 c->command = COMMAND_VERSION;
271                 break;
272             case 's':
273                 c->command = COMMAND_PUBLISH_SERVICE;
274                 break;
275             case 'a':
276                 c->command = COMMAND_PUBLISH_ADDRESS;
277                 break;
278             case 'v':
279                 c->verbose = 1;
280                 break;
281             case 'R':
282                 c->no_reverse = 1;
283                 break;
284             case 'd':
285                 avahi_free(c->domain);
286                 c->domain = avahi_strdup(optarg);
287                 break;
288             case 'H':
289                 avahi_free(c->host);
290                 c->host = avahi_strdup(optarg);
291                 break;
292             case 'f':
293                 c->no_fail = 1;
294                 break;
295             case ARG_SUBTYPE:
296                 c->subtypes = avahi_string_list_add(c->subtypes, optarg);
297                 break;
298             default:
299                 return -1;
300         }
301     }
302
303     if (c->command == COMMAND_PUBLISH_ADDRESS) {
304         if (optind+2 !=  argc) {
305             fprintf(stderr, _("Bad number of arguments\n"));
306             return -1;
307         }
308
309         avahi_free(c->name);
310         c->name = avahi_strdup(argv[optind]);
311         avahi_address_parse(argv[optind+1], AVAHI_PROTO_UNSPEC, &c->address);
312
313     } else if (c->command == COMMAND_PUBLISH_SERVICE) {
314
315         char *e;
316         long int p;
317         int i;
318
319         if (optind+3 > argc) {
320             fprintf(stderr, _("Bad number of arguments\n"));
321             return -1;
322         }
323
324         c->name = avahi_strdup(argv[optind]);
325         c->stype = avahi_strdup(argv[optind+1]);
326
327         errno = 0;
328         p = strtol(argv[optind+2], &e, 0);
329
330         if (errno != 0 || *e || p < 0 || p > 0xFFFF) {
331             fprintf(stderr, _("Failed to parse port number: %s\n"), argv[optind+2]);
332             return -1;
333         }
334
335         c->port = p;
336
337         for (i = optind+3; i < argc; i++)
338             c->txt = avahi_string_list_add(c->txt, argv[i]);
339     }
340
341     return 0;
342 }
343
344 int main(int argc, char *argv[]) {
345     int ret = 1, error;
346     Config config;
347     const char *argv0;
348
349     avahi_init_i18n();
350     setlocale(LC_ALL, "");
351
352     if ((argv0 = strrchr(argv[0], '/')))
353         argv0++;
354     else
355         argv0 = argv[0];
356
357     if (parse_command_line(&config, argv0, argc, argv) < 0)
358         goto fail;
359
360     switch (config.command) {
361         case COMMAND_UNSPEC:
362             ret = 1;
363             fprintf(stderr, _("No command specified.\n"));
364             break;
365
366         case COMMAND_HELP:
367             help(stdout, argv0);
368             ret = 0;
369             break;
370
371         case COMMAND_VERSION:
372             printf("%s "PACKAGE_VERSION"\n", argv0);
373             ret = 0;
374             break;
375
376         case COMMAND_PUBLISH_SERVICE:
377         case COMMAND_PUBLISH_ADDRESS:
378
379             if (!(simple_poll = avahi_simple_poll_new())) {
380                 fprintf(stderr, _("Failed to create simple poll object.\n"));
381                 goto fail;
382             }
383
384             if (sigint_install(simple_poll) < 0)
385                 goto fail;
386
387             if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), config.no_fail ? AVAHI_CLIENT_NO_FAIL : 0, client_callback, &config, &error))) {
388                 fprintf(stderr, _("Failed to create client object: %s\n"), avahi_strerror(error));
389                 goto fail;
390             }
391
392             if (avahi_client_get_state(client) != AVAHI_CLIENT_CONNECTING && config.verbose) {
393                 const char *version, *hn;
394
395                 if (!(version = avahi_client_get_version_string(client))) {
396                     fprintf(stderr, _("Failed to query version string: %s\n"), avahi_strerror(avahi_client_errno(client)));
397                     goto fail;
398                 }
399
400                 if (!(hn = avahi_client_get_host_name_fqdn(client))) {
401                     fprintf(stderr, _("Failed to query host name: %s\n"), avahi_strerror(avahi_client_errno(client)));
402                     goto fail;
403                 }
404
405                 fprintf(stderr, _("Server version: %s; Host name: %s\n"), version, hn);
406             }
407
408             avahi_simple_poll_loop(simple_poll);
409             ret = 0;
410             break;
411     }
412
413 fail:
414
415     if (client)
416         avahi_client_free(client);
417
418     sigint_uninstall();
419
420     if (simple_poll)
421         avahi_simple_poll_free(simple_poll);
422
423     avahi_free(config.host);
424     avahi_free(config.name);
425     avahi_free(config.stype);
426     avahi_free(config.domain);
427     avahi_string_list_free(config.subtypes);
428     avahi_string_list_free(config.txt);
429
430     return ret;
431 }