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