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>
32 #include <sys/socket.h>
35 #include <avahi-common/simple-watch.h>
36 #include <avahi-common/error.h>
37 #include <avahi-common/malloc.h>
38 #include <avahi-common/domain.h>
39 #include <avahi-client/client.h>
49 typedef struct Config {
54 static AvahiSimplePoll *simple_poll = NULL;
55 static AvahiClient *client = NULL;
57 static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
59 case AVAHI_CLIENT_FAILURE:
60 fprintf(stderr, "Client failure, exiting: %s\n", avahi_strerror(avahi_client_errno(c)));
61 avahi_simple_poll_quit(simple_poll);
64 case AVAHI_CLIENT_S_REGISTERING:
65 case AVAHI_CLIENT_S_RUNNING:
66 case AVAHI_CLIENT_S_COLLISION:
67 case AVAHI_CLIENT_CONNECTING:
72 static void help(FILE *f, const char *argv0) {
74 "%s [options] <new host name>\n\n"
75 " -h --help Show this help\n"
76 " -V --version Show version\n"
77 " -v --verbose Enable verbose mode\n",
81 static int parse_command_line(Config *c, int argc, char *argv[]) {
84 static const struct option long_options[] = {
85 { "help", no_argument, NULL, 'h' },
86 { "version", no_argument, NULL, 'V' },
87 { "verbose", no_argument, NULL, 'v' },
93 c->command = COMMAND_UNSPEC;
96 while ((o = getopt_long(argc, argv, "hVv", long_options, NULL)) >= 0) {
100 c->command = COMMAND_HELP;
103 c->command = COMMAND_VERSION;
113 if (c->command == COMMAND_UNSPEC) {
114 if (optind != argc-1) {
115 fprintf(stderr, "Invalid number of arguments, expecting exactly one.\n");
123 int main(int argc, char *argv[]) {
128 if ((argv0 = strrchr(argv[0], '/')))
133 if (parse_command_line(&config, argc, argv) < 0)
136 switch (config.command) {
142 case COMMAND_VERSION:
143 printf("%s "PACKAGE_VERSION"\n", argv0);
149 if (!(simple_poll = avahi_simple_poll_new())) {
150 fprintf(stderr, "Failed to create simple poll object.\n");
154 if (sigint_install(simple_poll) < 0)
157 if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), 0, client_callback, NULL, &error))) {
158 fprintf(stderr, "Failed to create client object: %s\n", avahi_strerror(error));
162 if (config.verbose) {
163 const char *version, *hn;
165 if (!(version = avahi_client_get_version_string(client))) {
166 fprintf(stderr, "Failed to query version string: %s\n", avahi_strerror(avahi_client_errno(client)));
170 if (!(hn = avahi_client_get_host_name_fqdn(client))) {
171 fprintf(stderr, "Failed to query host name: %s\n", avahi_strerror(avahi_client_errno(client)));
175 fprintf(stderr, "Server version: %s; Host name: %s\n", version, hn);
178 if (avahi_client_set_host_name(client, argv[optind]) < 0) {
179 fprintf(stderr, "Failed to create host name resolver: %s\n", avahi_strerror(avahi_client_errno(client)));
183 if (config.verbose) {
186 if (!(hn = avahi_client_get_host_name_fqdn(client))) {
187 fprintf(stderr, "Failed to query host name: %s\n", avahi_strerror(avahi_client_errno(client)));
191 fprintf(stderr, "Host name successfully changed to %s\n", hn);
201 avahi_client_free(client);
206 avahi_simple_poll_free(simple_poll);