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