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