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