]> git.meshlink.io Git - catta/blob - avahi-utils/avahi-set-host-name.c
fix avahi_netlink_new to allow multiple netlinks per process
[catta] / avahi-utils / avahi-set-host-name.c
1 /***
2   This file is part of avahi.
3
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.
8
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.
13
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
17   USA.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <getopt.h>
27 #include <assert.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <locale.h>
32
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>
39
40 #include "sigint.h"
41
42 typedef enum {
43     COMMAND_UNSPEC,
44     COMMAND_HELP,
45     COMMAND_VERSION
46 } Command;
47
48 typedef struct Config {
49     int verbose;
50     Command command;
51 } Config;
52
53 static AvahiSimplePoll *simple_poll = NULL;
54 static AvahiClient *client = NULL;
55
56 static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
57     switch (state) {
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);
61             break;
62
63         case AVAHI_CLIENT_S_REGISTERING:
64         case AVAHI_CLIENT_S_RUNNING:
65         case AVAHI_CLIENT_S_COLLISION:
66         case AVAHI_CLIENT_CONNECTING:
67             ;
68     }
69 }
70
71 static void help(FILE *f, const char *argv0) {
72     fprintf(f,
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"),
77             argv0);
78 }
79
80 static int parse_command_line(Config *c, int argc, char *argv[]) {
81     int o;
82
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' },
87         { NULL, 0, NULL, 0 }
88     };
89
90     assert(c);
91
92     c->command = COMMAND_UNSPEC;
93     c->verbose = 0;
94
95     while ((o = getopt_long(argc, argv, "hVv", long_options, NULL)) >= 0) {
96
97         switch(o) {
98             case 'h':
99                 c->command = COMMAND_HELP;
100                 break;
101             case 'V':
102                 c->command = COMMAND_VERSION;
103                 break;
104             case 'v':
105                 c->verbose = 1;
106                 break;
107             default:
108                 return -1;
109         }
110     }
111
112     if (c->command == COMMAND_UNSPEC) {
113         if (optind != argc-1) {
114             fprintf(stderr, _("Invalid number of arguments, expecting exactly one.\n"));
115             return -1;
116         }
117     }
118
119     return 0;
120 }
121
122 int main(int argc, char *argv[]) {
123     int ret = 1, error;
124     Config config;
125     const char *argv0;
126
127     avahi_init_i18n();
128     setlocale(LC_ALL, "");
129
130     if ((argv0 = strrchr(argv[0], '/')))
131         argv0++;
132     else
133         argv0 = argv[0];
134
135     if (parse_command_line(&config, argc, argv) < 0)
136         goto fail;
137
138     switch (config.command) {
139         case COMMAND_HELP:
140             help(stdout, argv0);
141             ret = 0;
142             break;
143
144         case COMMAND_VERSION:
145             printf("%s "PACKAGE_VERSION"\n", argv0);
146             ret = 0;
147             break;
148
149         case COMMAND_UNSPEC:
150
151             if (!(simple_poll = avahi_simple_poll_new())) {
152                 fprintf(stderr, _("Failed to create simple poll object.\n"));
153                 goto fail;
154             }
155
156             if (sigint_install(simple_poll) < 0)
157                 goto fail;
158
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));
161                 goto fail;
162             }
163
164             if (config.verbose) {
165                 const char *version, *hn;
166
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)));
169                     goto fail;
170                 }
171
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)));
174                     goto fail;
175                 }
176
177                 fprintf(stderr, _("Server version: %s; Host name: %s\n"), version, hn);
178             }
179
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)));
182                 goto fail;
183             }
184
185             if (config.verbose) {
186                 const char *hn;
187
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)));
190                     goto fail;
191                 }
192
193                 fprintf(stderr, _("Host name successfully changed to %s\n"), hn);
194             }
195
196             ret = 0;
197             break;
198     }
199
200 fail:
201
202     if (client)
203         avahi_client_free(client);
204
205     sigint_uninstall();
206
207     if (simple_poll)
208         avahi_simple_poll_free(simple_poll);
209
210     return ret;
211 }