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