2 This file is part of avahi.
4 avahi is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 avahi is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include <sys/types.h>
30 #include <sys/socket.h>
33 #include <avahi-common/simple-watch.h>
34 #include <avahi-common/error.h>
35 #include <avahi-common/malloc.h>
36 #include <avahi-common/domain.h>
37 #include <avahi-common/i18n.h>
38 #include <avahi-client/client.h>
48 typedef struct Config {
53 static AvahiSimplePoll *simple_poll = NULL;
54 static AvahiClient *client = NULL;
56 static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
58 case AVAHI_CLIENT_FAILURE:
59 fprintf(stderr, _("Client failure, exiting: %s\n"), avahi_strerror(avahi_client_errno(c)));
60 avahi_simple_poll_quit(simple_poll);
63 case AVAHI_CLIENT_S_REGISTERING:
64 case AVAHI_CLIENT_S_RUNNING:
65 case AVAHI_CLIENT_S_COLLISION:
66 case AVAHI_CLIENT_CONNECTING:
71 static void help(FILE *f, const char *argv0) {
73 _("%s [options] <new host name>\n\n"
74 " -h --help Show this help\n"
75 " -V --version Show version\n"
76 " -v --verbose Enable verbose mode\n"),
80 static int parse_command_line(Config *c, int argc, char *argv[]) {
83 static const struct option long_options[] = {
84 { "help", no_argument, NULL, 'h' },
85 { "version", no_argument, NULL, 'V' },
86 { "verbose", no_argument, NULL, 'v' },
92 c->command = COMMAND_UNSPEC;
95 while ((o = getopt_long(argc, argv, "hVv", long_options, NULL)) >= 0) {
99 c->command = COMMAND_HELP;
102 c->command = COMMAND_VERSION;
112 if (c->command == COMMAND_UNSPEC) {
113 if (optind != argc-1) {
114 fprintf(stderr, _("Invalid number of arguments, expecting exactly one.\n"));
122 int main(int argc, char *argv[]) {
128 setlocale(LC_ALL, "");
130 if ((argv0 = strrchr(argv[0], '/')))
135 if (parse_command_line(&config, argc, argv) < 0)
138 switch (config.command) {
144 case COMMAND_VERSION:
145 printf("%s "PACKAGE_VERSION"\n", argv0);
151 if (!(simple_poll = avahi_simple_poll_new())) {
152 fprintf(stderr, _("Failed to create simple poll object.\n"));
156 if (sigint_install(simple_poll) < 0)
159 if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), 0, client_callback, NULL, &error))) {
160 fprintf(stderr, _("Failed to create client object: %s\n"), avahi_strerror(error));
164 if (config.verbose) {
165 const char *version, *hn;
167 if (!(version = avahi_client_get_version_string(client))) {
168 fprintf(stderr, _("Failed to query version string: %s\n"), avahi_strerror(avahi_client_errno(client)));
172 if (!(hn = avahi_client_get_host_name_fqdn(client))) {
173 fprintf(stderr, _("Failed to query host name: %s\n"), avahi_strerror(avahi_client_errno(client)));
177 fprintf(stderr, _("Server version: %s; Host name: %s\n"), version, hn);
180 if (avahi_client_set_host_name(client, argv[optind]) < 0) {
181 fprintf(stderr, _("Failed to create host name resolver: %s\n"), avahi_strerror(avahi_client_errno(client)));
185 if (config.verbose) {
188 if (!(hn = avahi_client_get_host_name_fqdn(client))) {
189 fprintf(stderr, _("Failed to query host name: %s\n"), avahi_strerror(avahi_client_errno(client)));
193 fprintf(stderr, _("Host name successfully changed to %s\n"), hn);
203 avahi_client_free(client);
208 avahi_simple_poll_free(simple_poll);