]> git.meshlink.io Git - catta/blob - avahi-daemon/simple-protocol.c
* adapt to recent avahi-core API changes
[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
168 static void host_name_resolver_callback(
169     AvahiSHostNameResolver *r,
170     AvahiIfIndex iface,
171     AvahiProtocol protocol,
172     AvahiResolverEvent event,
173     const char *hostname,
174     const AvahiAddress *a,
175     AvahiLookupResultFlags flags, 
176     void* userdata) {
177
178     Client *c = userdata;
179     
180     assert(c);
181
182     if (event == AVAHI_RESOLVER_TIMEOUT)
183         client_output_printf(c, "%+i Query timed out\n", AVAHI_ERR_TIMEOUT);
184     else if (event == AVAHI_RESOLVER_FAILURE)
185         client_output_printf(c, "%+i Query failed\n", AVAHI_ERR_FAILURE);
186     else if (event == AVAHI_RESOLVER_NOT_FOUND)
187         client_output_printf(c, "%+i Query timed out\n", AVAHI_ERR_NOT_FOUND);
188     else if (event == AVAHI_RESOLVER_FOUND) {
189         char t[64];
190         avahi_address_snprint(t, sizeof(t), a);
191         client_output_printf(c, "+ %i %u %s %s\n", iface, protocol, hostname, t);
192     }
193
194     c->state = CLIENT_DEAD;
195 }
196
197 static void address_resolver_callback(
198     AvahiSAddressResolver *r,
199     AvahiIfIndex iface,
200     AvahiProtocol protocol,
201     AvahiResolverEvent event,
202     const AvahiAddress *a,
203     const char *hostname,
204     AvahiLookupResultFlags flags,
205     void* userdata) {
206     
207     Client *c = userdata;
208     
209     assert(c);
210
211     if (event == AVAHI_RESOLVER_TIMEOUT)
212         client_output_printf(c, "%+i Query timed out\n", AVAHI_ERR_TIMEOUT);
213     else if (event == AVAHI_RESOLVER_FAILURE)
214         client_output_printf(c, "%+i Query failed\n", AVAHI_ERR_FAILURE);
215     else if (event == AVAHI_RESOLVER_NOT_FOUND)
216         client_output_printf(c, "%+i Not found\n", AVAHI_ERR_NOT_FOUND);
217     else if (event == AVAHI_RESOLVER_FOUND)
218         client_output_printf(c, "+ %i %u %s\n", iface, protocol, hostname);
219
220     c->state = CLIENT_DEAD;
221 }
222
223 static void dns_server_browser_callback(
224     AvahiSDNSServerBrowser *b,
225     AvahiIfIndex interface,
226     AvahiProtocol protocol,
227     AvahiBrowserEvent event,
228     const char *host_name,
229     const AvahiAddress *a,
230     uint16_t port,
231     AvahiLookupResultFlags flags,
232     void* userdata) {
233     
234     Client *c = userdata;
235     char t[64];
236     
237     assert(c);
238
239     if (!a)
240         return;
241
242     switch (event) {
243         case AVAHI_BROWSER_FAILURE:
244             client_output_printf(c, "%+i Query failed\n", AVAHI_ERR_FAILURE);
245             c->state = CLIENT_DEAD;
246             break;
247
248         case AVAHI_BROWSER_NOT_FOUND:
249             client_output_printf(c, "%+i Not found\n", AVAHI_ERR_FAILURE);
250             c->state = CLIENT_DEAD;
251             break;
252
253         case AVAHI_BROWSER_ALL_FOR_NOW:
254         case AVAHI_BROWSER_CACHE_EXHAUSTED:
255             break;
256
257         case AVAHI_BROWSER_NEW:
258         case AVAHI_BROWSER_REMOVE:
259     
260             avahi_address_snprint(t, sizeof(t), a);
261             client_output_printf(c, "%c %i %u %s %u\n", event == AVAHI_BROWSER_NEW ? '>' : '<',  interface, protocol, t, port);
262             break;
263     }
264 }
265
266 static void handle_line(Client *c, const char *s) {
267     char cmd[64], arg[64];
268     int n_args;
269
270     assert(c);
271     assert(s);
272
273     if (c->state != CLIENT_IDLE)
274         return;
275
276     if ((n_args = sscanf(s, "%63s %63s", cmd, arg)) < 1 ) {
277         client_output_printf(c, "%+i Failed to parse command, try \"HELP\".\n", AVAHI_ERR_INVALID_OPERATION);
278         c->state = CLIENT_DEAD;
279         return;
280     }
281
282     if (strcmp(cmd, "HELP") == 0) {
283         client_output_printf(c,
284                              "+ Available commands are:\n"
285                              "+      RESOLVE-HOSTNAME <hostname>\n"
286                              "+      RESOLVE-HOSTNAME-IPV6 <hostname>\n"
287                              "+      RESOLVE-HOSTNAME-IPV4 <hostname>\n"
288                              "+      RESOLVE-ADDRESS <address>\n"
289                              "+      BROWSE-DNS-SERVERS\n"
290                              "+      BROWSE-DNS-SERVERS-IPV4\n"
291                              "+      BROWSE-DNS-SERVERS-IPV6\n");
292         c->state = CLIENT_DEAD; }
293     else if (strcmp(cmd, "FUCK") == 0 && n_args == 1) {
294         client_output_printf(c, "+ FUCK: Go fuck yourself!\n");
295         c->state = CLIENT_DEAD;
296     } else if (strcmp(cmd, "RESOLVE-HOSTNAME-IPV4") == 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_INET, 0, 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-IPV6") == 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_INET6, 0, 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-HOSTNAME") == 0 && n_args == 2) {
309         c->state = CLIENT_RESOLVE_HOSTNAME;
310         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)))
311             goto fail;
312
313         avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
314     } else if (strcmp(cmd, "RESOLVE-ADDRESS") == 0 && n_args == 2) {
315         AvahiAddress addr;
316         
317         if (!(avahi_address_parse(arg, AVAHI_PROTO_UNSPEC, &addr))) {
318             client_output_printf(c, "%+i Failed to parse address \"%s\".\n", AVAHI_ERR_INVALID_ADDRESS, arg);
319             c->state = CLIENT_DEAD;
320         } else {
321             c->state = CLIENT_RESOLVE_ADDRESS;
322             if (!(c->address_resolver = avahi_s_address_resolver_new(avahi_server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, &addr, 0, address_resolver_callback, c)))
323                 goto fail;
324         }
325
326         avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
327
328     } else if (strcmp(cmd, "BROWSE-DNS-SERVERS-IPV4") == 0 && n_args == 1) {
329         c->state = CLIENT_BROWSE_DNS_SERVERS;
330         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)))
331             goto fail;
332         client_output_printf(c, "+ Browsing ...\n");
333
334         avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
335
336     } else if (strcmp(cmd, "BROWSE-DNS-SERVERS-IPV6") == 0 && n_args == 1) {
337         c->state = CLIENT_BROWSE_DNS_SERVERS;
338         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)))
339             goto fail;
340         client_output_printf(c, "+ Browsing ...\n");
341
342         avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
343
344     } else if (strcmp(cmd, "BROWSE-DNS-SERVERS") == 0 && n_args == 1) {
345         c->state = CLIENT_BROWSE_DNS_SERVERS;
346         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)))
347             goto fail;
348         client_output_printf(c, "+ Browsing ...\n");
349
350         avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
351
352     } else {
353         client_output_printf(c, "%+i Invalid command \"%s\", try \"HELP\".\n", AVAHI_ERR_INVALID_OPERATION, cmd);
354         c->state = CLIENT_DEAD;
355
356         avahi_log_debug(__FILE__": Got invalid request '%s'.", cmd);
357     }
358
359     return;
360
361 fail:
362     client_output_printf(c, "%+i %s\n", avahi_server_errno(avahi_server), avahi_strerror(avahi_server_errno(avahi_server)));
363     c->state = CLIENT_DEAD;
364 }
365
366 static void handle_input(Client *c) {
367     assert(c);
368
369     for (;;) {
370         char *e;
371         size_t k;
372
373         if (!(e = memchr(c->inbuf, '\n', c->inbuf_length)))
374             break;
375
376         k = e - (char*) c->inbuf;
377         *e = 0;
378         
379         handle_line(c, c->inbuf);
380         c->inbuf_length -= k + 1;
381         memmove(c->inbuf, e+1, c->inbuf_length);
382     }
383 }
384
385 static void client_work(AvahiWatch *watch, int fd, AvahiWatchEvent events, void *userdata) {
386     Client *c = userdata;
387
388     assert(c);
389
390     if ((events & AVAHI_WATCH_IN) && c->inbuf_length < sizeof(c->inbuf)) {
391         ssize_t r;
392         
393         if ((r = read(c->fd, c->inbuf + c->inbuf_length, sizeof(c->inbuf) - c->inbuf_length)) <= 0) {
394             if (r < 0)
395                 avahi_log_warn("read(): %s", strerror(errno));
396             client_free(c);
397             return;
398         }
399
400         c->inbuf_length += r;
401         assert(c->inbuf_length <= sizeof(c->inbuf));
402
403         handle_input(c);
404     }
405
406     if ((events & AVAHI_WATCH_OUT) && c->outbuf_length > 0) {
407         ssize_t r;
408
409         if ((r = write(c->fd, c->outbuf, c->outbuf_length)) < 0) {
410             avahi_log_warn("write(): %s", strerror(errno));
411             client_free(c);
412             return;
413         }
414
415         assert((size_t) r <= c->outbuf_length);
416         c->outbuf_length -= r;
417         
418         if (c->outbuf_length)
419             memmove(c->outbuf, c->outbuf + r, c->outbuf_length - r);
420
421         if (c->outbuf_length == 0 && c->state == CLIENT_DEAD) {
422             client_free(c);
423             return;
424         }
425     }
426
427     c->server->poll_api->watch_update(
428         watch, 
429         (c->outbuf_length > 0 ? AVAHI_WATCH_OUT : 0) |
430         (c->inbuf_length < sizeof(c->inbuf) ? AVAHI_WATCH_IN : 0));
431 }
432
433 static void server_work(AvahiWatch *watch, int fd, AvahiWatchEvent events, void *userdata) {
434     Server *s = userdata;
435
436     assert(s);
437
438     if (events & AVAHI_WATCH_IN) {
439         int cfd;
440
441         if ((cfd = accept(fd, NULL, NULL)) < 0)
442             avahi_log_error("accept(): %s", strerror(errno));
443         else
444             client_new(s, cfd);
445     }
446 }
447     
448 int simple_protocol_setup(const AvahiPoll *poll_api) {
449     struct sockaddr_un sa;
450     mode_t u;
451
452     assert(!server);
453
454     server = avahi_new(Server, 1);
455     server->poll_api = poll_api;
456     server->bind_successful = 0;
457     server->fd = -1;
458     server->n_clients = 0;
459     AVAHI_LLIST_HEAD_INIT(Client, server->clients);
460     server->watch = NULL;
461     
462     u = umask(0000);
463
464     if ((server->fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
465         avahi_log_warn("socket(PF_LOCAL, SOCK_STREAM, 0): %s", strerror(errno));
466         goto fail;
467     }
468
469     memset(&sa, 0, sizeof(sa));
470     sa.sun_family = AF_LOCAL;
471     strncpy(sa.sun_path, AVAHI_SOCKET, sizeof(sa.sun_path)-1);
472
473     /* We simply remove existing UNIX sockets under this name. The
474        Avahi daemons makes sure that it runs only once on a host,
475        therefore sockets that already exist are stale and may be
476        removed without any ill effects */
477
478     unlink(AVAHI_SOCKET);
479     
480     if (bind(server->fd, &sa, sizeof(sa)) < 0) {
481         avahi_log_warn("bind(): %s", strerror(errno));
482         goto fail;
483     }
484
485     server->bind_successful = 1;
486     
487     if (listen(server->fd, 2) < 0) {
488         avahi_log_warn("listen(): %s", strerror(errno));
489         goto fail;
490     }
491
492     umask(u);
493
494     server->watch = poll_api->watch_new(poll_api, server->fd, AVAHI_WATCH_IN, server_work, server);
495     
496     return 0;
497
498 fail:
499     
500     umask(u);
501     simple_protocol_shutdown();
502
503     return -1;
504 }
505
506 void simple_protocol_shutdown(void) {
507
508     if (server) {
509
510         if (server->bind_successful)
511             unlink(AVAHI_SOCKET);
512
513         while (server->clients)
514             client_free(server->clients);
515
516         if (server->watch)
517             server->poll_api->watch_free(server->watch);
518         
519         if (server->fd >= 0)
520             close(server->fd);
521
522         avahi_free(server);
523         
524         server = NULL;
525     }
526 }
527
528 void simple_protocol_restart_queries(void) {
529     Client *c;
530
531     /* Restart queries in case of local domain name changes */
532     
533     assert(server);
534
535     for (c = server->clients; c; c = c->clients_next)
536         if (c->state == CLIENT_BROWSE_DNS_SERVERS && c->dns_server_browser) {
537             avahi_s_dns_server_browser_free(c->dns_server_browser);
538             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);
539         }
540 }
541
542