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