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>
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>
47 COMMAND_PUBLISH_SERVICE,
48 COMMAND_PUBLISH_ADDRESS
51 typedef struct Config {
54 char *name, *stype, *domain, *host;
56 AvahiStringList *txt, *subtypes;
60 static AvahiSimplePoll *simple_poll = NULL;
61 static AvahiClient *client = NULL;
62 static AvahiEntryGroup *entry_group = NULL;
64 static int register_stuff(Config *config);
66 static void entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
67 Config *config = userdata;
74 case AVAHI_ENTRY_GROUP_ESTABLISHED:
76 fprintf(stderr, "Established under name '%s'\n", config->name);
79 case AVAHI_ENTRY_GROUP_FAILURE:
81 fprintf(stderr, "Failed to register: %s\n", avahi_strerror(avahi_client_errno(client)));
84 case AVAHI_ENTRY_GROUP_COLLISION: {
87 if (config->command == COMMAND_PUBLISH_SERVICE)
88 n = avahi_alternative_service_name(config->name);
90 assert(config->command == COMMAND_PUBLISH_ADDRESS);
91 n = avahi_alternative_host_name(config->name);
94 fprintf(stderr, "Name collision, picking new name '%s'.\n", n);
95 avahi_free(config->name);
98 register_stuff(config);
103 case AVAHI_ENTRY_GROUP_UNCOMMITED:
104 case AVAHI_ENTRY_GROUP_REGISTERING:
109 static int register_stuff(Config *config) {
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)));
119 assert(avahi_entry_group_is_empty(entry_group));
121 if (config->command == COMMAND_PUBLISH_ADDRESS) {
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)));
131 assert(config->command == COMMAND_PUBLISH_SERVICE);
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)));
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)));
145 avahi_entry_group_commit(entry_group);
150 static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
151 Config *config = userdata;
156 case AVAHI_CLIENT_FAILURE:
158 if (config->no_fail && avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
161 /* We have been disconnected, so let reconnect */
163 fprintf(stderr, "Disconnected, reconnecting ...\n");
165 avahi_client_free(client);
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);
175 fprintf(stderr, "Client failure, exiting: %s\n", avahi_strerror(avahi_client_errno(c)));
176 avahi_simple_poll_quit(simple_poll);
181 case AVAHI_CLIENT_S_RUNNING:
183 if (register_stuff(config) < 0)
184 avahi_simple_poll_quit(simple_poll);
188 case AVAHI_CLIENT_S_COLLISION:
191 fprintf(stderr, "Host name conflict\n");
195 case AVAHI_CLIENT_S_REGISTERING:
198 avahi_entry_group_free(entry_group);
203 case AVAHI_CLIENT_CONNECTING:
206 fprintf(stderr, "Waiting for daemon ...\n");
214 static void help(FILE *f, const char *argv0) {
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");
231 static int parse_command_line(Config *c, const char *argv0, int argc, char *argv[]) {
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' },
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;
257 c->txt = c->subtypes = NULL;
260 while ((o = getopt_long(argc, argv, "hVsavd:H:f", long_options, NULL)) >= 0) {
264 c->command = COMMAND_HELP;
267 c->command = COMMAND_VERSION;
270 c->command = COMMAND_PUBLISH_SERVICE;
273 c->command = COMMAND_PUBLISH_ADDRESS;
279 avahi_free(c->domain);
280 c->domain = avahi_strdup(optarg);
284 c->host = avahi_strdup(optarg);
290 c->subtypes = avahi_string_list_add(c->subtypes, optarg);
293 fprintf(stderr, "Invalid command line argument: %c\n", o);
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[]) {
344 if ((argv0 = strrchr(argv[0], '/')))
349 if (parse_command_line(&config, argv0, argc, argv) < 0)
352 switch (config.command) {
355 fprintf(stderr, "No command specified.\n");
363 case COMMAND_VERSION:
364 printf("%s "PACKAGE_VERSION"\n", argv0);
368 case COMMAND_PUBLISH_SERVICE:
369 case COMMAND_PUBLISH_ADDRESS:
371 if (!(simple_poll = avahi_simple_poll_new())) {
372 fprintf(stderr, "Failed to create simple poll object.\n");
376 if (sigint_install(simple_poll) < 0)
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));
384 if (avahi_client_get_state(client) != AVAHI_CLIENT_CONNECTING && config.verbose) {
385 const char *version, *hn;
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)));
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)));
397 fprintf(stderr, "Server version: %s; Host name: %s\n", version, hn);
400 avahi_simple_poll_loop(simple_poll);
408 avahi_client_free(client);
413 avahi_simple_poll_free(simple_poll);
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);