4 This file is part of avahi.
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.
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.
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
31 #include <sys/types.h>
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>
49 COMMAND_PUBLISH_SERVICE,
50 COMMAND_PUBLISH_ADDRESS
53 typedef struct Config {
56 char *name, *stype, *domain, *host;
58 AvahiStringList *txt, *subtypes;
62 static AvahiSimplePoll *simple_poll = NULL;
63 static AvahiClient *client = NULL;
64 static AvahiEntryGroup *entry_group = NULL;
66 static int register_stuff(Config *config);
68 static void entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
69 Config *config = userdata;
76 case AVAHI_ENTRY_GROUP_ESTABLISHED:
78 fprintf(stderr, _("Established under name '%s'\n"), config->name);
81 case AVAHI_ENTRY_GROUP_FAILURE:
83 fprintf(stderr, _("Failed to register: %s\n"), avahi_strerror(avahi_client_errno(client)));
86 case AVAHI_ENTRY_GROUP_COLLISION: {
89 if (config->command == COMMAND_PUBLISH_SERVICE)
90 n = avahi_alternative_service_name(config->name);
92 assert(config->command == COMMAND_PUBLISH_ADDRESS);
93 n = avahi_alternative_host_name(config->name);
96 fprintf(stderr, _("Name collision, picking new name '%s'.\n"), n);
97 avahi_free(config->name);
100 register_stuff(config);
105 case AVAHI_ENTRY_GROUP_UNCOMMITED:
106 case AVAHI_ENTRY_GROUP_REGISTERING:
111 static int register_stuff(Config *config) {
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)));
121 assert(avahi_entry_group_is_empty(entry_group));
123 if (config->command == COMMAND_PUBLISH_ADDRESS) {
125 if (avahi_entry_group_add_address(entry_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, config->name, &config->address) < 0) {
126 fprintf(stderr, _("Failed to add address: %s\n"), avahi_strerror(avahi_client_errno(client)));
133 assert(config->command == COMMAND_PUBLISH_SERVICE);
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)));
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)));
147 avahi_entry_group_commit(entry_group);
152 static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
153 Config *config = userdata;
158 case AVAHI_CLIENT_FAILURE:
160 if (config->no_fail && avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
163 /* We have been disconnected, so let reconnect */
165 fprintf(stderr, _("Disconnected, reconnecting ...\n"));
167 avahi_client_free(client);
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);
177 fprintf(stderr, _("Client failure, exiting: %s\n"), avahi_strerror(avahi_client_errno(c)));
178 avahi_simple_poll_quit(simple_poll);
183 case AVAHI_CLIENT_S_RUNNING:
185 if (register_stuff(config) < 0)
186 avahi_simple_poll_quit(simple_poll);
190 case AVAHI_CLIENT_S_COLLISION:
193 fprintf(stderr, _("Host name conflict\n"));
197 case AVAHI_CLIENT_S_REGISTERING:
200 avahi_entry_group_free(entry_group);
205 case AVAHI_CLIENT_CONNECTING:
208 fprintf(stderr, _("Waiting for daemon ...\n"));
216 static void help(FILE *f, const char *argv0) {
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 " -f --no-fail Don't fail if the daemon is not available\n"),
229 argv0, strstr(argv0, "service") ? "[-s]" : "-s",
230 argv0, strstr(argv0, "address") ? "[-a]" : "-a");
233 static int parse_command_line(Config *c, const char *argv0, int argc, char *argv[]) {
240 static const struct option long_options[] = {
241 { "help", no_argument, NULL, 'h' },
242 { "version", no_argument, NULL, 'V' },
243 { "service", no_argument, NULL, 's' },
244 { "address", no_argument, NULL, 'a' },
245 { "verbose", no_argument, NULL, 'v' },
246 { "domain", required_argument, NULL, 'd' },
247 { "host", required_argument, NULL, 'H' },
248 { "subtype", required_argument, NULL, ARG_SUBTYPE},
249 { "no-fail", no_argument, NULL, 'f' },
255 c->command = strstr(argv0, "address") ? COMMAND_PUBLISH_ADDRESS : (strstr(argv0, "service") ? COMMAND_PUBLISH_SERVICE : COMMAND_UNSPEC);
256 c->verbose = c->no_fail = 0;
257 c->host = c->name = c->domain = c->stype = NULL;
259 c->txt = c->subtypes = NULL;
261 while ((o = getopt_long(argc, argv, "hVsavd:H:f", long_options, NULL)) >= 0) {
265 c->command = COMMAND_HELP;
268 c->command = COMMAND_VERSION;
271 c->command = COMMAND_PUBLISH_SERVICE;
274 c->command = COMMAND_PUBLISH_ADDRESS;
280 avahi_free(c->domain);
281 c->domain = avahi_strdup(optarg);
285 c->host = avahi_strdup(optarg);
291 c->subtypes = avahi_string_list_add(c->subtypes, optarg);
298 if (c->command == COMMAND_PUBLISH_ADDRESS) {
299 if (optind+2 != argc) {
300 fprintf(stderr, _("Bad number of arguments\n"));
305 c->name = avahi_strdup(argv[optind]);
306 avahi_address_parse(argv[optind+1], AVAHI_PROTO_UNSPEC, &c->address);
308 } else if (c->command == COMMAND_PUBLISH_SERVICE) {
314 if (optind+3 > argc) {
315 fprintf(stderr, _("Bad number of arguments\n"));
319 c->name = avahi_strdup(argv[optind]);
320 c->stype = avahi_strdup(argv[optind+1]);
323 p = strtol(argv[optind+2], &e, 0);
325 if (errno != 0 || *e || p < 0 || p > 0xFFFF) {
326 fprintf(stderr, _("Failed to parse port number: %s\n"), argv[optind+2]);
332 for (i = optind+3; i < argc; i++)
333 c->txt = avahi_string_list_add(c->txt, argv[i]);
339 int main(int argc, char *argv[]) {
345 setlocale(LC_ALL, "");
347 if ((argv0 = strrchr(argv[0], '/')))
352 if (parse_command_line(&config, argv0, argc, argv) < 0)
355 switch (config.command) {
358 fprintf(stderr, _("No command specified.\n"));
366 case COMMAND_VERSION:
367 printf("%s "PACKAGE_VERSION"\n", argv0);
371 case COMMAND_PUBLISH_SERVICE:
372 case COMMAND_PUBLISH_ADDRESS:
374 if (!(simple_poll = avahi_simple_poll_new())) {
375 fprintf(stderr, _("Failed to create simple poll object.\n"));
379 if (sigint_install(simple_poll) < 0)
382 if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), config.no_fail ? AVAHI_CLIENT_NO_FAIL : 0, client_callback, &config, &error))) {
383 fprintf(stderr, _("Failed to create client object: %s\n"), avahi_strerror(error));
387 if (avahi_client_get_state(client) != AVAHI_CLIENT_CONNECTING && config.verbose) {
388 const char *version, *hn;
390 if (!(version = avahi_client_get_version_string(client))) {
391 fprintf(stderr, _("Failed to query version string: %s\n"), avahi_strerror(avahi_client_errno(client)));
395 if (!(hn = avahi_client_get_host_name_fqdn(client))) {
396 fprintf(stderr, _("Failed to query host name: %s\n"), avahi_strerror(avahi_client_errno(client)));
400 fprintf(stderr, _("Server version: %s; Host name: %s\n"), version, hn);
403 avahi_simple_poll_loop(simple_poll);
411 avahi_client_free(client);
416 avahi_simple_poll_free(simple_poll);
418 avahi_free(config.host);
419 avahi_free(config.name);
420 avahi_free(config.stype);
421 avahi_free(config.domain);
422 avahi_string_list_free(config.subtypes);
423 avahi_string_list_free(config.txt);