]> git.meshlink.io Git - catta/blob - avahi-utils/avahi-publish.c
Enough is enough! I have had it with these motherf**ng gcc on this motherf**ng shut...
[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     while ((o = getopt_long(argc, argv, "hVsavd:H:f", long_options, NULL)) >= 0) {
260
261         switch(o) {
262             case 'h':
263                 c->command = COMMAND_HELP;
264                 break;
265             case 'V':
266                 c->command = COMMAND_VERSION;
267                 break;
268             case 's':
269                 c->command = COMMAND_PUBLISH_SERVICE;
270                 break;
271             case 'a':
272                 c->command = COMMAND_PUBLISH_ADDRESS;
273                 break;
274             case 'v':
275                 c->verbose = 1;
276                 break;
277             case 'd':
278                 avahi_free(c->domain);
279                 c->domain = avahi_strdup(optarg);
280                 break;
281             case 'H':
282                 avahi_free(c->host);
283                 c->host = avahi_strdup(optarg);
284                 break;
285             case 'f':
286                 c->no_fail = 1;
287                 break;
288             case ARG_SUBTYPE:
289                 c->subtypes = avahi_string_list_add(c->subtypes, optarg);
290                 break;
291             default:
292                 return -1;
293         }
294     }
295
296     if (c->command == COMMAND_PUBLISH_ADDRESS) {
297         if (optind+2 !=  argc) {
298             fprintf(stderr, "Bad number of arguments\n");
299             return -1;
300         }
301
302         avahi_free(c->name);
303         c->name = avahi_strdup(argv[optind]);
304         avahi_address_parse(argv[optind+1], AVAHI_PROTO_UNSPEC, &c->address);
305         
306     } else if (c->command == COMMAND_PUBLISH_SERVICE) {
307
308         char *e;
309         long int p;
310         int i;
311         
312         if (optind+3 > argc) {
313             fprintf(stderr, "Bad number of arguments\n");
314             return -1;
315         }
316
317         c->name = avahi_strdup(argv[optind]);
318         c->stype = avahi_strdup(argv[optind+1]);
319
320         errno = 0;
321         p = strtol(argv[optind+2], &e, 0);
322
323         if (errno != 0 || *e || p < 0 || p > 0xFFFF) {
324             fprintf(stderr, "Failed to parse port number: %s\n", argv[optind+2]);
325             return -1;
326         }
327
328         c->port = p;
329             
330         for (i = optind+3; i < argc; i++)
331             c->txt = avahi_string_list_add(c->txt, argv[i]);
332     }
333         
334     return 0;
335 }
336
337 int main(int argc, char *argv[]) {
338     int ret = 1, error;
339     Config config;
340     const char *argv0;
341
342     if ((argv0 = strrchr(argv[0], '/')))
343         argv0++;
344     else
345         argv0 = argv[0];
346
347     if (parse_command_line(&config, argv0, argc, argv) < 0)
348         goto fail;
349
350     switch (config.command) {
351         case COMMAND_UNSPEC:
352             ret = 1;
353             fprintf(stderr, "No command specified.\n");
354             break;
355             
356         case COMMAND_HELP:
357             help(stdout, argv0);
358             ret = 0;
359             break;
360             
361         case COMMAND_VERSION:
362             printf("%s "PACKAGE_VERSION"\n", argv0);
363             ret = 0;
364             break;
365
366         case COMMAND_PUBLISH_SERVICE:
367         case COMMAND_PUBLISH_ADDRESS:
368             
369             if (!(simple_poll = avahi_simple_poll_new())) {
370                 fprintf(stderr, "Failed to create simple poll object.\n");
371                 goto fail;
372             }
373             
374             if (sigint_install(simple_poll) < 0)
375                 goto fail;
376             
377             if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), config.no_fail ? AVAHI_CLIENT_NO_FAIL : 0, client_callback, &config, &error))) {
378                 fprintf(stderr, "Failed to create client object: %s\n", avahi_strerror(error));
379                 goto fail;
380             }
381
382             if (avahi_client_get_state(client) != AVAHI_CLIENT_CONNECTING && config.verbose) {
383                 const char *version, *hn;
384
385                 if (!(version = avahi_client_get_version_string(client))) {
386                     fprintf(stderr, "Failed to query version string: %s\n", avahi_strerror(avahi_client_errno(client)));
387                     goto fail;
388                 }
389
390                 if (!(hn = avahi_client_get_host_name_fqdn(client))) {
391                     fprintf(stderr, "Failed to query host name: %s\n", avahi_strerror(avahi_client_errno(client)));
392                     goto fail;
393                 }
394                 
395                 fprintf(stderr, "Server version: %s; Host name: %s\n", version, hn);
396             }
397
398             avahi_simple_poll_loop(simple_poll);
399             ret = 0;
400             break;
401     }
402     
403 fail:
404
405     if (client)
406         avahi_client_free(client);
407
408     sigint_uninstall();
409     
410     if (simple_poll)
411         avahi_simple_poll_free(simple_poll);
412
413     avahi_free(config.host);
414     avahi_free(config.name);
415     avahi_free(config.stype);
416     avahi_free(config.domain);
417     avahi_string_list_free(config.subtypes);
418     avahi_string_list_free(config.txt);
419
420     return ret;
421 }