]> git.meshlink.io Git - catta/blob - avahi-daemon/simple-protocol.c
* recreate DNS query in simple protocol on host or domain name 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 <string.h>
27 #include <sys/socket.h>
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <sys/un.h>
31 #include <errno.h>
32 #include <fcntl.h>
33
34 #include <glib.h>
35
36 #include <avahi-core/llist.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 UNIX_SOCKET AVAHI_RUNTIME_DIR "/socket"
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     gint fd;
65     GPollFD poll_fd;
66
67     gchar inbuf[BUFFER_SIZE], outbuf[BUFFER_SIZE];
68     guint inbuf_length, outbuf_length;
69
70     AvahiHostNameResolver *host_name_resolver;
71     AvahiAddressResolver *address_resolver;
72     AvahiDNSServerBrowser *dns_server_browser;
73
74     AvahiProtocol afquery;
75     
76     AVAHI_LLIST_FIELDS(Client, clients);
77 };
78
79 struct Server {
80     GSource source;
81     GMainContext *context;
82     GPollFD poll_fd;
83     gint fd;
84     AVAHI_LLIST_HEAD(Client, clients);
85
86     guint n_clients;
87     gboolean bind_successful;
88 };
89
90 static Server *server = NULL;
91
92 static void client_free(Client *c) {
93     g_assert(c);
94
95     g_assert(c->server->n_clients >= 1);
96     c->server->n_clients--;
97
98     if (c->host_name_resolver)
99         avahi_host_name_resolver_free(c->host_name_resolver);
100
101     if (c->address_resolver)
102         avahi_address_resolver_free(c->address_resolver);
103
104     if (c->dns_server_browser)
105         avahi_dns_server_browser_free(c->dns_server_browser);
106     
107     g_source_remove_poll(&c->server->source, &c->poll_fd);
108     close(c->fd);
109     AVAHI_LLIST_REMOVE(Client, clients, c->server->clients, c);
110     g_free(c);
111 }
112
113 static void client_new(Server *s, int fd) {
114     Client *c;
115
116     g_assert(fd >= 0);
117
118     c = g_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     memset(&c->poll_fd, 0, sizeof(GPollFD));
130     c->poll_fd.fd = fd;
131     c->poll_fd.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
132     g_source_add_poll(&s->source, &c->poll_fd);
133
134     AVAHI_LLIST_PREPEND(Client, clients, s->clients, c);
135     s->n_clients++;
136 }
137
138 static void client_output(Client *c, const guint8*data, guint size) {
139     guint k, m;
140     
141     g_assert(c);
142     g_assert(data);
143
144     if (!size)
145         return;
146
147     k = sizeof(c->outbuf) - c->outbuf_length;
148     m = size > k ? k : size;
149
150     memcpy(c->outbuf + c->outbuf_length, data, m);
151     c->outbuf_length += m;
152
153     c->poll_fd.events |= G_IO_OUT;
154 }
155
156 static void client_output_printf(Client *c, gchar *format, ...) {
157     gchar *t;
158     va_list ap;
159     
160     va_start(ap, format);
161     t = g_strdup_vprintf(format, ap);
162     va_end(ap);
163
164     client_output(c, (guint8*) t, strlen(t));
165     g_free(t);
166 }
167
168
169 static void host_name_resolver_callback(AvahiHostNameResolver *r, gint iface, guchar protocol, AvahiBrowserEvent event, const gchar *hostname, const AvahiAddress *a, gpointer userdata) {
170     Client *c = userdata;
171     
172     g_assert(c);
173
174
175     if (event == AVAHI_RESOLVER_TIMEOUT)
176         client_output_printf(c, "- Query timed out\n");
177     else {
178         gchar t[64];
179         avahi_address_snprint(t, sizeof(t), a);
180         client_output_printf(c, "+ %i %u %s %s\n", iface, protocol, hostname, t);
181     }
182
183     c->state = CLIENT_DEAD;
184 }
185
186 static void address_resolver_callback(AvahiAddressResolver *r, gint iface, guchar protocol, AvahiBrowserEvent event, const AvahiAddress *a, const gchar *hostname, gpointer userdata) {
187     Client *c = userdata;
188     
189     
190     g_assert(c);
191
192     if (event == AVAHI_RESOLVER_TIMEOUT)
193         client_output_printf(c, "- Query timed out\n");
194     else 
195         client_output_printf(c, "+ %i %u %s\n", iface, protocol, hostname);
196
197     c->state = CLIENT_DEAD;
198 }
199
200 static void dns_server_browser_callback(AvahiDNSServerBrowser *b, gint interface, guchar protocol, AvahiBrowserEvent event, const gchar *host_name, const AvahiAddress *a, guint16 port, gpointer userdata) {
201     Client *c = userdata;
202     gchar t[64];
203     
204     g_assert(c);
205
206     if (!a)
207         return;
208
209     avahi_address_snprint(t, sizeof(t), a);
210     client_output_printf(c, "%c %i %u %s %u\n", event == AVAHI_BROWSER_NEW ? '>' : '<',  interface, protocol, t, port);
211 }
212
213 static void handle_line(Client *c, const gchar *s) {
214     gchar cmd[64], arg[64];
215     gint n_args;
216
217     g_assert(c);
218     g_assert(s);
219
220     if (c->state != CLIENT_IDLE)
221         return;
222
223     if ((n_args = sscanf(s, "%63s %63s", cmd, arg)) < 1 ) {
224         client_output_printf(c, "- Failed to parse command, try \"HELP\".\n");
225         c->state = CLIENT_DEAD;
226         return;
227     }
228
229     if (strcmp(cmd, "HELP") == 0) {
230         client_output_printf(c,
231                              "+ Available commands are:\n"
232                              "+      RESOLVE-HOSTNAME <hostname>\n"
233                              "+      RESOLVE-HOSTNAME-IPV6 <hostname>\n"
234                              "+      RESOLVE-HOSTNAME-IPV4 <hostname>\n"
235                              "+      RESOLVE-ADDRESS <address>\n"
236                              "+      BROWSE-DNS-SERVERS\n"
237                              "+      BROWSE-DNS-SERVERS-IPV4\n"
238                              "+      BROWSE-DNS-SERVERS-IPV6\n");
239         c->state = CLIENT_DEAD; }
240     else if (strcmp(cmd, "FUCK") == 0 && n_args == 1) {
241         client_output_printf(c, "FUCK: Go fuck yourself!\n");
242         c->state = CLIENT_DEAD;
243     } else if (strcmp(cmd, "RESOLVE-HOSTNAME-IPV4") == 0 && n_args == 2) {
244         c->state = CLIENT_RESOLVE_HOSTNAME;
245         c->host_name_resolver = avahi_host_name_resolver_new(avahi_server, -1, AF_UNSPEC, arg, c->afquery = AF_INET, host_name_resolver_callback, c);
246     } else if (strcmp(cmd, "RESOLVE-HOSTNAME-IPV6") == 0 && n_args == 2) {
247         c->state = CLIENT_RESOLVE_HOSTNAME;
248         c->host_name_resolver = avahi_host_name_resolver_new(avahi_server, -1, AF_UNSPEC, arg, c->afquery = AF_INET6, host_name_resolver_callback, c);
249     } else if (strcmp(cmd, "RESOLVE-HOSTNAME") == 0 && n_args == 2) {
250         c->state = CLIENT_RESOLVE_HOSTNAME;
251         c->host_name_resolver = avahi_host_name_resolver_new(avahi_server, -1, AF_UNSPEC, arg, c->afquery = AF_UNSPEC, host_name_resolver_callback, c);
252     } else if (strcmp(cmd, "RESOLVE-ADDRESS") == 0 && n_args == 2) {
253         AvahiAddress addr;
254         
255         if (!(avahi_address_parse(arg, AF_UNSPEC, &addr))) {
256             client_output_printf(c, "- Failed to parse address \"%s\".\n", arg);
257             c->state = CLIENT_DEAD;
258         } else {
259             c->state = CLIENT_RESOLVE_ADDRESS;
260             c->address_resolver = avahi_address_resolver_new(avahi_server, -1, AF_UNSPEC, &addr, address_resolver_callback, c);
261         }
262     } else if (strcmp(cmd, "BROWSE-DNS-SERVERS-IPV4") == 0 && n_args == 1) {
263         c->state = CLIENT_BROWSE_DNS_SERVERS;
264         c->dns_server_browser = avahi_dns_server_browser_new(avahi_server, -1, AF_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, c->afquery = AF_INET, dns_server_browser_callback, c);
265         client_output_printf(c, "+ Browsing ...\n");
266     } else if (strcmp(cmd, "BROWSE-DNS-SERVERS-IPV6") == 0 && n_args == 1) {
267         c->state = CLIENT_BROWSE_DNS_SERVERS;
268         c->dns_server_browser = avahi_dns_server_browser_new(avahi_server, -1, AF_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, c->afquery = AF_INET6, dns_server_browser_callback, c);
269         client_output_printf(c, "+ Browsing ...\n");
270     } else if (strcmp(cmd, "BROWSE-DNS-SERVERS") == 0 && n_args == 1) {
271         c->state = CLIENT_BROWSE_DNS_SERVERS;
272         c->dns_server_browser = avahi_dns_server_browser_new(avahi_server, -1, AF_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, c->afquery = AF_UNSPEC, dns_server_browser_callback, c);
273         client_output_printf(c, "+ Browsing ...\n");
274     } else {
275         client_output_printf(c, "- Invalid command \"%s\", try \"HELP\".\n", cmd);
276         c->state = CLIENT_DEAD;
277     }
278 }
279
280 static void handle_input(Client *c) {
281     g_assert(c);
282
283     for (;;) {
284         gchar *e;
285         guint k;
286
287         if (!(e = memchr(c->inbuf, '\n', c->inbuf_length)))
288             break;
289
290         k = e - (gchar*) c->inbuf;
291         *e = 0;
292         
293         handle_line(c, c->inbuf);
294         c->inbuf_length -= k + 1;
295         memmove(c->inbuf, e+1, c->inbuf_length);
296     }
297 }
298
299 static void client_work(Client *c) {
300     g_assert(c);
301
302     if ((c->poll_fd.revents & G_IO_IN) && c->inbuf_length < sizeof(c->inbuf)) {
303         ssize_t r;
304         
305         if ((r = read(c->fd, c->inbuf + c->inbuf_length, sizeof(c->inbuf) - c->inbuf_length)) <= 0) {
306             if (r < 0)
307                 avahi_log_warn("read(): %s", strerror(errno));
308             client_free(c);
309             return;
310         }
311
312         c->inbuf_length += r;
313         g_assert(c->inbuf_length <= sizeof(c->inbuf));
314
315         handle_input(c);
316     }
317
318     if ((c->poll_fd.revents & G_IO_OUT) && c->outbuf_length > 0) {
319         ssize_t r;
320
321         if ((r = write(c->fd, c->outbuf, c->outbuf_length)) < 0) {
322             avahi_log_warn("write(): %s", strerror(errno));
323             client_free(c);
324             return;
325         }
326
327         g_assert((guint) r <= c->outbuf_length);
328         c->outbuf_length -= r;
329         
330         if (c->outbuf_length)
331             memmove(c->outbuf, c->outbuf + r, c->outbuf_length - r);
332
333         if (c->outbuf_length == 0 && c->state == CLIENT_DEAD) {
334             client_free(c);
335             return;
336         }
337     }
338
339     c->poll_fd.events =
340         G_IO_ERR |
341         G_IO_HUP |
342         (c->outbuf_length > 0 ? G_IO_OUT : 0) |
343         (c->inbuf_length < sizeof(c->inbuf) ? G_IO_IN : 0);
344 }
345
346 static gboolean prepare_func(GSource *source, gint *timeout) {
347     g_assert(source);
348     g_assert(timeout);
349     
350     *timeout = -1;
351     return FALSE;
352 }
353
354 static gboolean check_func(GSource *source) {
355     Server *s = (Server*) source;
356     Client *c;
357     
358     g_assert(s);
359
360     if (s->poll_fd.revents)
361         return TRUE;
362     
363     for (c = s->clients; c; c = c->clients_next)
364         if (c->poll_fd.revents)
365             return TRUE;
366
367     return FALSE;
368 }
369
370 static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer user_data) {
371     Server *s = (Server*) source;
372     Client *c, *n;
373     
374     g_assert(s);
375
376     if (s->poll_fd.revents & G_IO_IN) {
377         gint fd;
378
379         if ((fd = accept(s->fd, NULL, NULL)) < 0)
380             avahi_log_warn("accept(): %s", strerror(errno));
381         else
382             client_new(s, fd);
383     } else if (s->poll_fd.revents)
384         g_error("Invalid revents");
385
386     for (c = s->clients; c; c = n) {
387         n = c->clients_next;
388         if (c->poll_fd.revents)
389             client_work(c);
390     }
391     
392     return TRUE;
393 }
394
395 int simple_protocol_setup(GMainContext *c) {
396     struct sockaddr_un sa;
397     mode_t u;
398
399     static GSourceFuncs source_funcs = {
400         prepare_func,
401         check_func,
402         dispatch_func,
403         NULL,
404         NULL,
405         NULL
406     };
407     
408     g_assert(!server);
409
410     server = (Server*) g_source_new(&source_funcs, sizeof(Server));
411     server->bind_successful = FALSE;
412     server->fd = -1;
413     AVAHI_LLIST_HEAD_INIT(Client, server->clients);
414     g_main_context_ref(server->context = (c ? c : g_main_context_default()));
415     server->clients = NULL;
416
417     u = umask(0000);
418
419     if ((server->fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
420         avahi_log_warn("socket(PF_LOCAL, SOCK_STREAM, 0): %s", strerror(errno));
421         goto fail;
422     }
423
424     memset(&sa, 0, sizeof(sa));
425     sa.sun_family = AF_LOCAL;
426     strncpy(sa.sun_path, UNIX_SOCKET, sizeof(sa.sun_path)-1);
427
428     /* We simply remove existing UNIX sockets under this name. The
429        Avahi daemons makes sure that it runs only once on a host,
430        therefore sockets that already exist are stale and may be
431        removed without any ill effects */
432
433     unlink(UNIX_SOCKET);
434     
435     if (bind(server->fd, &sa, sizeof(sa)) < 0) {
436         avahi_log_warn("bind(): %s", strerror(errno));
437         goto fail;
438     }
439
440     server->bind_successful = TRUE;
441     
442     if (listen(server->fd, 2) < 0) {
443         avahi_log_warn("listen(): %s", strerror(errno));
444         goto fail;
445     }
446
447     umask(u);
448
449     memset(&server->poll_fd, 0, sizeof(GPollFD));
450     server->poll_fd.fd = server->fd;
451     server->poll_fd.events = G_IO_IN|G_IO_ERR;
452     g_source_add_poll(&server->source, &server->poll_fd);
453
454     g_source_attach(&server->source, server->context);
455     
456     return 0;
457
458 fail:
459     
460     umask(u);
461     simple_protocol_shutdown();
462
463     return -1;
464 }
465
466 void simple_protocol_shutdown(void) {
467
468     if (server) {
469
470         while (server->clients)
471             client_free(server->clients);
472
473         if (server->bind_successful)
474             unlink(UNIX_SOCKET);
475         
476         if (server->fd >= 0)
477             close(server->fd);
478
479         g_main_context_unref(server->context);
480         
481         g_source_destroy(&server->source);
482         g_source_unref(&server->source);
483
484         server = NULL;
485     }
486 }
487
488 void simple_protocol_restart_queries(void) {
489     Client *c;
490
491     /* Restart queries in case of local domain name changes */
492     
493     g_assert(server);
494
495     for (c = server->clients; c; c = c->clients_next)
496         if (c->state == CLIENT_BROWSE_DNS_SERVERS && c->dns_server_browser) {
497             avahi_dns_server_browser_free(c->dns_server_browser);
498             c->dns_server_browser = avahi_dns_server_browser_new(avahi_server, -1, AF_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, c->afquery, dns_server_browser_callback, c);
499         }
500 }
501
502