]> git.meshlink.io Git - catta/blob - avahi-daemon/simple-protocol.c
Add debug message when executing simple protocol commands
[catta] / avahi-daemon / simple-protocol.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 <assert.h>
27 #include <string.h>
28 #include <sys/socket.h>
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <sys/un.h>
32 #include <errno.h>
33 #include <fcntl.h>
34
35 #include <avahi-common/llist.h>
36 #include <avahi-common/malloc.h>
37 #include <avahi-common/error.h>
38 #include <avahi-core/log.h>
39
40 #include "simple-protocol.h"
41 #include "main.h"
42
43 #define BUFFER_SIZE (20*1024)
44
45 #define CLIENTS_MAX 50
46
47 typedef struct Client Client;
48 typedef struct Server Server;
49
50 typedef enum {
51     CLIENT_IDLE,
52     CLIENT_RESOLVE_HOSTNAME,
53     CLIENT_RESOLVE_ADDRESS,
54     CLIENT_BROWSE_DNS_SERVERS,
55     CLIENT_DEAD
56 } ClientState;
57
58 struct Client {
59     Server *server;
60
61     ClientState state;
62     
63     int fd;
64     AvahiWatch *watch;
65
66     char inbuf[BUFFER_SIZE], outbuf[BUFFER_SIZE];
67     size_t inbuf_length, outbuf_length;
68
69     AvahiSHostNameResolver *host_name_resolver;
70     AvahiSAddressResolver *address_resolver;
71     AvahiSDNSServerBrowser *dns_server_browser;
72
73     AvahiProtocol afquery;
74     
75     AVAHI_LLIST_FIELDS(Client, clients);
76 };
77
78 struct Server {
79     const AvahiPoll *poll_api;
80     int fd;
81     AvahiWatch *watch;
82     AVAHI_LLIST_HEAD(Client, clients);
83
84     unsigned n_clients;
85     int bind_successful;
86 };
87
88 static Server *server = NULL;
89
90 static void client_work(AvahiWatch *watch, int fd, AvahiWatchEvent events, void *userdata);
91
92 static void client_free(Client *c) {
93     assert(c);
94
95     assert(c->server->n_clients >= 1);
96     c->server->n_clients--;
97
98     if (c->host_name_resolver)
99         avahi_s_host_name_resolver_free(c->host_name_resolver);
100
101     if (c->address_resolver)
102         avahi_s_address_resolver_free(c->address_resolver);
103
104     if (c->dns_server_browser)
105         avahi_s_dns_server_browser_free(c->dns_server_browser);
106
107     c->server->poll_api->watch_free(c->watch);
108     close(c->fd);
109     
110     AVAHI_LLIST_REMOVE(Client, clients, c->server->clients, c);
111     avahi_free(c);
112 }
113
114 static void client_new(Server *s, int fd) {
115     Client *c;
116
117     assert(fd >= 0);
118
119     c = avahi_new(Client, 1);
120     c->server = s;
121     c->fd = fd;
122     c->state = CLIENT_IDLE;
123
124     c->inbuf_length = c->outbuf_length = 0;
125
126     c->host_name_resolver = NULL;
127     c->address_resolver = NULL;
128     c->dns_server_browser = NULL;
129
130     c->watch = s->poll_api->watch_new(s->poll_api, fd, AVAHI_WATCH_IN, client_work, c);
131
132     AVAHI_LLIST_PREPEND(Client, clients, s->clients, c);
133     s->n_clients++;
134 }
135
136 static void client_output(Client *c, const uint8_t*data, size_t size) {
137     size_t k, m;
138     
139     assert(c);
140     assert(data);
141
142     if (!size)
143         return;
144
145     k = sizeof(c->outbuf) - c->outbuf_length;
146     m = size > k ? k : size;
147
148     memcpy(c->outbuf + c->outbuf_length, data, m);
149     c->outbuf_length += m;
150
151     server->poll_api->watch_update(c->watch, AVAHI_WATCH_OUT);
152 }
153
154 static void client_output_printf(Client *c, const char *format, ...) {
155     char *t;
156     va_list ap;
157     
158     va_start(ap, format);
159     t = avahi_strdup_vprintf(format, ap);
160     va_end(ap);
161
162     client_output(c, (uint8_t*) t, strlen(t));
163     avahi_free(t);
164 }
165
166
167 static void host_name_resolver_callback(
168     AvahiSHostNameResolver *r,
169     AvahiIfIndex iface,
170     AvahiProtocol protocol,
171     AvahiResolverEvent event,
172     const char *hostname,
173     const AvahiAddress *a,
174     void* userdata) {
175
176     Client *c = userdata;
177     
178     assert(c);
179
180     if (event == AVAHI_RESOLVER_TIMEOUT)
181         client_output_printf(c, "%+i Query timed out\n", AVAHI_ERR_TIMEOUT);
182     else {
183         char t[64];
184         avahi_address_snprint(t, sizeof(t), a);
185         client_output_printf(c, "+ %i %u %s %s\n", iface, protocol, hostname, t);
186     }
187
188     c->state = CLIENT_DEAD;
189 }
190
191 static void address_resolver_callback(
192     AvahiSAddressResolver *r,
193     AvahiIfIndex iface,
194     AvahiProtocol protocol,
195     AvahiResolverEvent event,
196     const AvahiAddress *a,
197     const char *hostname,
198     void* userdata) {
199     
200     Client *c = userdata;
201     
202     assert(c);
203
204     if (event == AVAHI_RESOLVER_TIMEOUT)
205         client_output_printf(c, "%+i Query timed out\n", AVAHI_ERR_TIMEOUT);
206     else 
207         client_output_printf(c, "+ %i %u %s\n", iface, protocol, hostname);
208
209     c->state = CLIENT_DEAD;
210 }
211
212 static void dns_server_browser_callback(
213     AvahiSDNSServerBrowser *b,
214     AvahiIfIndex interface,
215     AvahiProtocol protocol,
216     AvahiBrowserEvent event,
217     const char *host_name,
218     const AvahiAddress *a,
219     uint16_t port,
220     void* userdata) {
221     
222     Client *c = userdata;
223     char t[64];
224     
225     assert(c);
226
227     if (!a)
228         return;
229
230     avahi_address_snprint(t, sizeof(t), a);
231     client_output_printf(c, "%c %i %u %s %u\n", event == AVAHI_BROWSER_NEW ? '>' : '<',  interface, protocol, t, port);
232 }
233
234 static void handle_line(Client *c, const char *s) {
235     char cmd[64], arg[64];
236     int n_args;
237
238     assert(c);
239     assert(s);
240
241     if (c->state != CLIENT_IDLE)
242         return;
243
244     if ((n_args = sscanf(s, "%63s %63s", cmd, arg)) < 1 ) {
245         client_output_printf(c, "%+i Failed to parse command, try \"HELP\".\n", AVAHI_ERR_INVALID_OPERATION);
246         c->state = CLIENT_DEAD;
247         return;
248     }
249
250     if (strcmp(cmd, "HELP") == 0) {
251         client_output_printf(c,
252                              "+ Available commands are:\n"
253                              "+      RESOLVE-HOSTNAME <hostname>\n"
254                              "+      RESOLVE-HOSTNAME-IPV6 <hostname>\n"
255                              "+      RESOLVE-HOSTNAME-IPV4 <hostname>\n"
256                              "+      RESOLVE-ADDRESS <address>\n"
257                              "+      BROWSE-DNS-SERVERS\n"
258                              "+      BROWSE-DNS-SERVERS-IPV4\n"
259                              "+      BROWSE-DNS-SERVERS-IPV6\n");
260         c->state = CLIENT_DEAD; }
261     else if (strcmp(cmd, "FUCK") == 0 && n_args == 1) {
262         client_output_printf(c, "+ FUCK: Go fuck yourself!\n");
263         c->state = CLIENT_DEAD;
264     } else if (strcmp(cmd, "RESOLVE-HOSTNAME-IPV4") == 0 && n_args == 2) {
265         c->state = CLIENT_RESOLVE_HOSTNAME;
266         if (!(c->host_name_resolver = avahi_s_host_name_resolver_new(avahi_server, -1, AF_UNSPEC, arg, c->afquery = AF_INET, host_name_resolver_callback, c)))
267             goto fail;
268
269         avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
270     } else if (strcmp(cmd, "RESOLVE-HOSTNAME-IPV6") == 0 && n_args == 2) {
271         c->state = CLIENT_RESOLVE_HOSTNAME;
272         if (!(c->host_name_resolver = avahi_s_host_name_resolver_new(avahi_server, -1, AF_UNSPEC, arg, c->afquery = AF_INET6, host_name_resolver_callback, c)))
273             goto fail;
274
275         avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
276     } else if (strcmp(cmd, "RESOLVE-HOSTNAME") == 0 && n_args == 2) {
277         c->state = CLIENT_RESOLVE_HOSTNAME;
278         if (!(c->host_name_resolver = avahi_s_host_name_resolver_new(avahi_server, -1, AF_UNSPEC, arg, c->afquery = AF_UNSPEC, host_name_resolver_callback, c)))
279             goto fail;
280
281         avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
282     } else if (strcmp(cmd, "RESOLVE-ADDRESS") == 0 && n_args == 2) {
283         AvahiAddress addr;
284         
285         if (!(avahi_address_parse(arg, AF_UNSPEC, &addr))) {
286             client_output_printf(c, "%+i Failed to parse address \"%s\".\n", AVAHI_ERR_INVALID_ADDRESS, arg);
287             c->state = CLIENT_DEAD;
288         } else {
289             c->state = CLIENT_RESOLVE_ADDRESS;
290             if (!(c->address_resolver = avahi_s_address_resolver_new(avahi_server, -1, AF_UNSPEC, &addr, address_resolver_callback, c)))
291                 goto fail;
292         }
293
294         avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
295
296     } else if (strcmp(cmd, "BROWSE-DNS-SERVERS-IPV4") == 0 && n_args == 1) {
297         c->state = CLIENT_BROWSE_DNS_SERVERS;
298         if (!(c->dns_server_browser = avahi_s_dns_server_browser_new(avahi_server, -1, AF_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, c->afquery = AF_INET, dns_server_browser_callback, c)))
299             goto fail;
300         client_output_printf(c, "+ Browsing ...\n");
301
302         avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
303
304     } else if (strcmp(cmd, "BROWSE-DNS-SERVERS-IPV6") == 0 && n_args == 1) {
305         c->state = CLIENT_BROWSE_DNS_SERVERS;
306         if (!(c->dns_server_browser = avahi_s_dns_server_browser_new(avahi_server, -1, AF_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, c->afquery = AF_INET6, dns_server_browser_callback, c)))
307             goto fail;
308         client_output_printf(c, "+ Browsing ...\n");
309
310         avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
311
312     } else if (strcmp(cmd, "BROWSE-DNS-SERVERS") == 0 && n_args == 1) {
313         c->state = CLIENT_BROWSE_DNS_SERVERS;
314         if (!(c->dns_server_browser = avahi_s_dns_server_browser_new(avahi_server, -1, AF_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, c->afquery = AF_UNSPEC, dns_server_browser_callback, c)))
315             goto fail;
316         client_output_printf(c, "+ Browsing ...\n");
317
318         avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
319
320     } else {
321         client_output_printf(c, "%+i Invalid command \"%s\", try \"HELP\".\n", AVAHI_ERR_INVALID_OPERATION, cmd);
322         c->state = CLIENT_DEAD;
323
324         avahi_log_debug(__FILE__": Got invalid request '%s'.", cmd);
325     }
326
327     return;
328
329 fail:
330     client_output_printf(c, "%+i %s\n", avahi_server_errno(avahi_server), avahi_strerror(avahi_server_errno(avahi_server)));
331     c->state = CLIENT_DEAD;
332 }
333
334 static void handle_input(Client *c) {
335     assert(c);
336
337     for (;;) {
338         char *e;
339         size_t k;
340
341         if (!(e = memchr(c->inbuf, '\n', c->inbuf_length)))
342             break;
343
344         k = e - (char*) c->inbuf;
345         *e = 0;
346         
347         handle_line(c, c->inbuf);
348         c->inbuf_length -= k + 1;
349         memmove(c->inbuf, e+1, c->inbuf_length);
350     }
351 }
352
353 static void client_work(AvahiWatch *watch, int fd, AvahiWatchEvent events, void *userdata) {
354     Client *c = userdata;
355
356     assert(c);
357
358     if ((events & AVAHI_WATCH_IN) && c->inbuf_length < sizeof(c->inbuf)) {
359         ssize_t r;
360         
361         if ((r = read(c->fd, c->inbuf + c->inbuf_length, sizeof(c->inbuf) - c->inbuf_length)) <= 0) {
362             if (r < 0)
363                 avahi_log_warn("read(): %s", strerror(errno));
364             client_free(c);
365             return;
366         }
367
368         c->inbuf_length += r;
369         assert(c->inbuf_length <= sizeof(c->inbuf));
370
371         handle_input(c);
372     }
373
374     if ((events & AVAHI_WATCH_OUT) && c->outbuf_length > 0) {
375         ssize_t r;
376
377         if ((r = write(c->fd, c->outbuf, c->outbuf_length)) < 0) {
378             avahi_log_warn("write(): %s", strerror(errno));
379             client_free(c);
380             return;
381         }
382
383         assert((size_t) r <= c->outbuf_length);
384         c->outbuf_length -= r;
385         
386         if (c->outbuf_length)
387             memmove(c->outbuf, c->outbuf + r, c->outbuf_length - r);
388
389         if (c->outbuf_length == 0 && c->state == CLIENT_DEAD) {
390             client_free(c);
391             return;
392         }
393     }
394
395     c->server->poll_api->watch_update(
396         watch, 
397         (c->outbuf_length > 0 ? AVAHI_WATCH_OUT : 0) |
398         (c->inbuf_length < sizeof(c->inbuf) ? AVAHI_WATCH_IN : 0));
399 }
400
401 static void server_work(AvahiWatch *watch, int fd, AvahiWatchEvent events, void *userdata) {
402     Server *s = userdata;
403
404     assert(s);
405
406     if (events & AVAHI_WATCH_IN) {
407         int cfd;
408
409         if ((cfd = accept(fd, NULL, NULL)) < 0)
410             avahi_log_error("accept(): %s", strerror(errno));
411         else
412             client_new(s, cfd);
413     }
414 }
415     
416 int simple_protocol_setup(const AvahiPoll *poll_api) {
417     struct sockaddr_un sa;
418     mode_t u;
419
420     assert(!server);
421
422     server = avahi_new(Server, 1);
423     server->poll_api = poll_api;
424     server->bind_successful = 0;
425     server->fd = -1;
426     server->n_clients = 0;
427     AVAHI_LLIST_HEAD_INIT(Client, server->clients);
428     server->watch = NULL;
429     
430     u = umask(0000);
431
432     if ((server->fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
433         avahi_log_warn("socket(PF_LOCAL, SOCK_STREAM, 0): %s", strerror(errno));
434         goto fail;
435     }
436
437     memset(&sa, 0, sizeof(sa));
438     sa.sun_family = AF_LOCAL;
439     strncpy(sa.sun_path, AVAHI_SOCKET, sizeof(sa.sun_path)-1);
440
441     /* We simply remove existing UNIX sockets under this name. The
442        Avahi daemons makes sure that it runs only once on a host,
443        therefore sockets that already exist are stale and may be
444        removed without any ill effects */
445
446     unlink(AVAHI_SOCKET);
447     
448     if (bind(server->fd, &sa, sizeof(sa)) < 0) {
449         avahi_log_warn("bind(): %s", strerror(errno));
450         goto fail;
451     }
452
453     server->bind_successful = 1;
454     
455     if (listen(server->fd, 2) < 0) {
456         avahi_log_warn("listen(): %s", strerror(errno));
457         goto fail;
458     }
459
460     umask(u);
461
462     server->watch = poll_api->watch_new(poll_api, server->fd, AVAHI_WATCH_IN, server_work, server);
463     
464     return 0;
465
466 fail:
467     
468     umask(u);
469     simple_protocol_shutdown();
470
471     return -1;
472 }
473
474 void simple_protocol_shutdown(void) {
475
476     if (server) {
477
478         if (server->bind_successful)
479             unlink(AVAHI_SOCKET);
480
481         while (server->clients)
482             client_free(server->clients);
483
484         if (server->watch)
485             server->poll_api->watch_free(server->watch);
486         
487         if (server->fd >= 0)
488             close(server->fd);
489
490         avahi_free(server);
491         
492         server = NULL;
493     }
494 }
495
496 void simple_protocol_restart_queries(void) {
497     Client *c;
498
499     /* Restart queries in case of local domain name changes */
500     
501     assert(server);
502
503     for (c = server->clients; c; c = c->clients_next)
504         if (c->state == CLIENT_BROWSE_DNS_SERVERS && c->dns_server_browser) {
505             avahi_s_dns_server_browser_free(c->dns_server_browser);
506             c->dns_server_browser = avahi_s_dns_server_browser_new(avahi_server, -1, AF_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, c->afquery, dns_server_browser_callback, c);
507         }
508 }
509
510