]> git.meshlink.io Git - catta/blob - avahi-utils/avahi-publish.c
Implement avahi-publish{-address, service} in C
[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             if (entry_group) {
194                 avahi_entry_group_free(entry_group);
195                 entry_group = NULL;
196             }
197             break;
198             
199         case AVAHI_CLIENT_CONNECTING:
200             
201             if (config->verbose)
202                 fprintf(stderr, "Waiting for daemon ...\n");
203             break;
204             
205         case AVAHI_CLIENT_S_REGISTERING:
206             ;
207     }
208 }
209
210 static void help(FILE *f, const char *argv0) {
211     fprintf(f,
212             "%s [options] %s <name> <type> <port> [<txt ...>]\n"
213             "%s [options] %s <host-name> <address>\n\n"
214             "    -h --help            Show this help\n"
215             "    -V --version         Show version\n"
216             "    -s --service         Publish service\n"
217             "    -a --address         Publish address\n"
218             "    -v --verbose         Enable verbose mode\n"
219             "    -d --domain=DOMAIN   Domain to publish service in\n"
220             "    -H --host=DOMAIN     Host where service resides\n"
221             "       --subtype=SUBTYPE An additional subtype to register this service with\n"
222             "    -f --no-fail         Don't fail if the daemon is not available\n",
223             argv0, strstr(argv0, "address") ? "[-a]" : "-a",
224             argv0, strstr(argv0, "service") ? "[-s]" : "-s");
225 }
226
227 static int parse_command_line(Config *c, const char *argv0, int argc, char *argv[]) {
228     int o;
229
230     enum {
231         ARG_SUBTYPE = 256
232     };
233     
234     static const struct option long_options[] = {
235         { "help",           no_argument,       NULL, 'h' },
236         { "version",        no_argument,       NULL, 'V' },
237         { "service",        no_argument,       NULL, 's' },
238         { "address",        no_argument,       NULL, 'a' },
239         { "verbose",        no_argument,       NULL, 'v' },
240         { "domain",         required_argument, NULL, 'd' },
241         { "host",           required_argument, NULL, 'H' },
242         { "subtype",        required_argument, NULL, ARG_SUBTYPE},
243         { "no-fail",        no_argument,       NULL, 'f' },
244         { NULL, 0, NULL, 0 }
245     };
246
247     assert(c);
248
249     c->command = strstr(argv0, "address") ? COMMAND_PUBLISH_ADDRESS : (strstr(argv0, "service") ? COMMAND_PUBLISH_SERVICE : COMMAND_UNSPEC);
250     c->verbose = c->no_fail = 0;
251     c->host = c->name = c->domain = c->stype = NULL;
252     c->port = 0;
253     c->txt = c->subtypes = NULL;
254
255     opterr = 0;
256     while ((o = getopt_long(argc, argv, "hVsavd:H:f", long_options, NULL)) >= 0) {
257
258         switch(o) {
259             case 'h':
260                 c->command = COMMAND_HELP;
261                 break;
262             case 'V':
263                 c->command = COMMAND_VERSION;
264                 break;
265             case 's':
266                 c->command = COMMAND_PUBLISH_SERVICE;
267                 break;
268             case 'a':
269                 c->command = COMMAND_PUBLISH_ADDRESS;
270                 break;
271             case 'v':
272                 c->verbose = 1;
273                 break;
274             case 'd':
275                 avahi_free(c->domain);
276                 c->domain = avahi_strdup(optarg);
277                 break;
278             case 'H':
279                 avahi_free(c->host);
280                 c->host = avahi_strdup(optarg);
281                 break;
282             case 'f':
283                 c->no_fail = 1;
284                 break;
285             case ARG_SUBTYPE:
286                 c->subtypes = avahi_string_list_add(c->subtypes, optarg);
287                 break;
288             default:
289                 fprintf(stderr, "Invalid command line argument: %c\n", o);
290                 return -1;
291         }
292     }
293
294     if (c->command == COMMAND_PUBLISH_ADDRESS) {
295         if (optind+2 !=  argc) {
296             fprintf(stderr, "Bad number of arguments\n");
297             return -1;
298         }
299
300         avahi_free(c->name);
301         c->name = avahi_strdup(argv[optind]);
302         avahi_address_parse(argv[optind+1], AVAHI_PROTO_UNSPEC, &c->address);
303         
304     } else if (c->command == COMMAND_PUBLISH_SERVICE) {
305
306         char *e;
307         long int p;
308         int i;
309         
310         if (optind+3 > argc) {
311             fprintf(stderr, "Bad number of arguments\n");
312             return -1;
313         }
314
315         c->name = avahi_strdup(argv[optind]);
316         c->stype = avahi_strdup(argv[optind+1]);
317
318         errno = 0;
319         p = strtol(argv[optind+2], &e, 0);
320
321         if (errno != 0 || *e || p < 0 || p > 0xFFFF) {
322             fprintf(stderr, "Failed to parse port number: %s\n", argv[optind+2]);
323             return -1;
324         }
325             
326         for (i = optind+3; i < argc; i++)
327             c->txt = avahi_string_list_add(c->txt, argv[i]);
328     }
329         
330     return 0;
331 }
332
333 int main(int argc, char *argv[]) {
334     int ret = 1, error;
335     Config config;
336     const char *argv0;
337
338     if ((argv0 = strrchr(argv[0], '/')))
339         argv0++;
340     else
341         argv0 = argv[0];
342
343     if (parse_command_line(&config, argv0, argc, argv) < 0)
344         goto fail;
345
346     switch (config.command) {
347         case COMMAND_UNSPEC:
348             ret = 1;
349             fprintf(stderr, "No command specified.\n");
350             break;
351             
352         case COMMAND_HELP:
353             help(stdout, argv0);
354             ret = 0;
355             break;
356             
357         case COMMAND_VERSION:
358             printf("%s "PACKAGE_VERSION"\n", argv0);
359             ret = 0;
360             break;
361
362         case COMMAND_PUBLISH_SERVICE:
363         case COMMAND_PUBLISH_ADDRESS:
364             
365             if (!(simple_poll = avahi_simple_poll_new())) {
366                 fprintf(stderr, "Failed to create simple poll object.\n");
367                 goto fail;
368             }
369             
370             if (sigint_install(simple_poll) < 0)
371                 goto fail;
372             
373             if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), config.no_fail ? AVAHI_CLIENT_NO_FAIL : 0, client_callback, &config, &error))) {
374                 fprintf(stderr, "Failed to create client object: %s\n", avahi_strerror(error));
375                 goto fail;
376             }
377
378             if (avahi_client_get_state(client) != AVAHI_CLIENT_CONNECTING && config.verbose) {
379                 const char *version, *hn;
380
381                 if (!(version = avahi_client_get_version_string(client))) {
382                     fprintf(stderr, "Failed to query version string: %s\n", avahi_strerror(avahi_client_errno(client)));
383                     goto fail;
384                 }
385
386                 if (!(hn = avahi_client_get_host_name_fqdn(client))) {
387                     fprintf(stderr, "Failed to query host name: %s\n", avahi_strerror(avahi_client_errno(client)));
388                     goto fail;
389                 }
390                 
391                 fprintf(stderr, "Server version: %s; Host name: %s\n", version, hn);
392             }
393
394             avahi_simple_poll_loop(simple_poll);
395             ret = 0;
396             break;
397     }
398     
399 fail:
400
401     if (client)
402         avahi_client_free(client);
403
404     sigint_uninstall();
405     
406     if (simple_poll)
407         avahi_simple_poll_free(simple_poll);
408
409     avahi_free(config.host);
410     avahi_free(config.name);
411     avahi_free(config.stype);
412     avahi_free(config.domain);
413     avahi_string_list_free(config.subtypes);
414     avahi_string_list_free(config.txt);
415
416     return ret;
417 }