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