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