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-common/i18n.h>
40 #include <avahi-client/client.h>
50 typedef struct Config {
55 static AvahiSimplePoll *simple_poll = NULL;
56 static AvahiClient *client = NULL;
58 static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
60 case AVAHI_CLIENT_FAILURE:
61 fprintf(stderr, _("Client failure, exiting: %s\n"), avahi_strerror(avahi_client_errno(c)));
62 avahi_simple_poll_quit(simple_poll);
65 case AVAHI_CLIENT_S_REGISTERING:
66 case AVAHI_CLIENT_S_RUNNING:
67 case AVAHI_CLIENT_S_COLLISION:
68 case AVAHI_CLIENT_CONNECTING:
73 static void help(FILE *f, const char *argv0) {
75 _("%s [options] <new host name>\n\n"
76 " -h --help Show this help\n"
77 " -V --version Show version\n"
78 " -v --verbose Enable verbose mode\n"),
82 static int parse_command_line(Config *c, int argc, char *argv[]) {
85 static const struct option long_options[] = {
86 { "help", no_argument, NULL, 'h' },
87 { "version", no_argument, NULL, 'V' },
88 { "verbose", no_argument, NULL, 'v' },
94 c->command = COMMAND_UNSPEC;
97 while ((o = getopt_long(argc, argv, "hVv", long_options, NULL)) >= 0) {
101 c->command = COMMAND_HELP;
104 c->command = COMMAND_VERSION;
114 if (c->command == COMMAND_UNSPEC) {
115 if (optind != argc-1) {
116 fprintf(stderr, _("Invalid number of arguments, expecting exactly one.\n"));
124 int main(int argc, char *argv[]) {
130 setlocale(LC_ALL, "");
132 if ((argv0 = strrchr(argv[0], '/')))
137 if (parse_command_line(&config, argc, argv) < 0)
140 switch (config.command) {
146 case COMMAND_VERSION:
147 printf("%s "PACKAGE_VERSION"\n", argv0);
153 if (!(simple_poll = avahi_simple_poll_new())) {
154 fprintf(stderr, _("Failed to create simple poll object.\n"));
158 if (sigint_install(simple_poll) < 0)
161 if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), 0, client_callback, NULL, &error))) {
162 fprintf(stderr, _("Failed to create client object: %s\n"), avahi_strerror(error));
166 if (config.verbose) {
167 const char *version, *hn;
169 if (!(version = avahi_client_get_version_string(client))) {
170 fprintf(stderr, _("Failed to query version string: %s\n"), avahi_strerror(avahi_client_errno(client)));
174 if (!(hn = avahi_client_get_host_name_fqdn(client))) {
175 fprintf(stderr, _("Failed to query host name: %s\n"), avahi_strerror(avahi_client_errno(client)));
179 fprintf(stderr, _("Server version: %s; Host name: %s\n"), version, hn);
182 if (avahi_client_set_host_name(client, argv[optind]) < 0) {
183 fprintf(stderr, _("Failed to create host name resolver: %s\n"), avahi_strerror(avahi_client_errno(client)));
187 if (config.verbose) {
190 if (!(hn = avahi_client_get_host_name_fqdn(client))) {
191 fprintf(stderr, _("Failed to query host name: %s\n"), avahi_strerror(avahi_client_errno(client)));
195 fprintf(stderr, _("Host name successfully changed to %s\n"), hn);
205 avahi_client_free(client);
210 avahi_simple_poll_free(simple_poll);