]> git.meshlink.io Git - catta/blob - avahi-daemon/simple-protocol.c
* update todo list
[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     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, 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 static void host_name_resolver_callback(AvahiHostNameResolver *r, gint iface, guchar protocol, AvahiBrowserEvent event, const gchar *hostname, const AvahiAddress *a, gpointer userdata) {
167     Client *c = userdata;
168     
169     g_assert(c);
170
171
172     if (event == AVAHI_RESOLVER_TIMEOUT)
173         client_output_printf(c, "- Query timed out\n");
174     else {
175         gchar t[64];
176         avahi_address_snprint(t, sizeof(t), a);
177         client_output_printf(c, "+ %i %u %s %s\n", iface, protocol, hostname, t);
178     }
179
180     c->state = CLIENT_DEAD;
181 }
182
183 static void address_resolver_callback(AvahiAddressResolver *r, gint iface, guchar protocol, AvahiBrowserEvent event, const AvahiAddress *a, const gchar *hostname, gpointer userdata) {
184     Client *c = userdata;
185     
186     
187     g_assert(c);
188
189     if (event == AVAHI_RESOLVER_TIMEOUT)
190         client_output_printf(c, "- Query timed out\n");
191     else 
192         client_output_printf(c, "+ %i %u %s\n", iface, protocol, hostname);
193
194     c->state = CLIENT_DEAD;
195 }
196
197 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) {
198     Client *c = userdata;
199     gchar t[64];
200     
201     g_assert(c);
202
203     if (!a)
204         return;
205
206     avahi_address_snprint(t, sizeof(t), a);
207     client_output_printf(c, "%c %i %u %s %u\n", event == AVAHI_BROWSER_NEW ? '>' : '<',  interface, protocol, t, port);
208 }
209
210 static void handle_line(Client *c, const gchar *s) {
211     gchar cmd[64], arg[64];
212     gint n_args;
213
214     g_assert(c);
215     g_assert(s);
216
217     if (c->state != CLIENT_IDLE)
218         return;
219
220     if ((n_args = sscanf(s, "%63s %63s", cmd, arg)) < 1 ) {
221         client_output_printf(c, "- Failed to parse command, try \"HELP\".\n");
222         c->state = CLIENT_DEAD;
223         return;
224     }
225
226     if (strcmp(cmd, "HELP") == 0) {
227         client_output_printf(c,
228                              "+ Available commands are:\n"
229                              "+      RESOLVE-HOSTNAME <hostname>\n"
230                              "+      RESOLVE-HOSTNAME-IPV6 <hostname>\n"
231                              "+      RESOLVE-HOSTNAME-IPV4 <hostname>\n"
232                              "+      RESOLVE-ADDRESS <address>\n"
233                              "+      BROWSE-DNS-SERVERS\n"
234                              "+      BROWSE-DNS-SERVERS-IPV4\n"
235                              "+      BROWSE-DNS-SERVERS-IPV6\n");
236         c->state = CLIENT_DEAD; }
237     else if (strcmp(cmd, "FUCK") == 0 && n_args == 1) {
238         client_output_printf(c, "FUCK: Go fuck yourself!\n");
239         c->state = CLIENT_DEAD;
240     } else if (strcmp(cmd, "RESOLVE-HOSTNAME-IPV4") == 0 && n_args == 2) {
241         c->state = CLIENT_RESOLVE_HOSTNAME;
242         c->host_name_resolver = avahi_host_name_resolver_new(avahi_server, -1, AF_UNSPEC, arg, AF_INET, host_name_resolver_callback, c);
243     } else if (strcmp(cmd, "RESOLVE-HOSTNAME-IPV6") == 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, AF_INET6, host_name_resolver_callback, c);
246     } else if (strcmp(cmd, "RESOLVE-HOSTNAME") == 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, AF_UNSPEC, host_name_resolver_callback, c);
249     } else if (strcmp(cmd, "RESOLVE-ADDRESS") == 0 && n_args == 2) {
250         AvahiAddress addr;
251         
252         if (!(avahi_address_parse(arg, AF_UNSPEC, &addr))) {
253             client_output_printf(c, "- Failed to parse address \"%s\".\n", arg);
254             c->state = CLIENT_DEAD;
255         } else {
256             c->state = CLIENT_RESOLVE_ADDRESS;
257             c->address_resolver = avahi_address_resolver_new(avahi_server, -1, AF_UNSPEC, &addr, address_resolver_callback, c);
258         }
259     } else if (strcmp(cmd, "BROWSE-DNS-SERVERS-IPV4") == 0 && n_args == 1) {
260         c->state = CLIENT_BROWSE_DNS_SERVERS;
261         c->dns_server_browser = avahi_dns_server_browser_new(avahi_server, -1, AF_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, AF_INET, dns_server_browser_callback, c);
262         client_output_printf(c, "+ Browsing ...\n");
263     } else if (strcmp(cmd, "BROWSE-DNS-SERVERS-IPV6") == 0 && n_args == 1) {
264         c->state = CLIENT_BROWSE_DNS_SERVERS;
265         c->dns_server_browser = avahi_dns_server_browser_new(avahi_server, -1, AF_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, AF_INET6, dns_server_browser_callback, c);
266         client_output_printf(c, "+ Browsing ...\n");
267     } else if (strcmp(cmd, "BROWSE-DNS-SERVERS") == 0 && n_args == 1) {
268         c->state = CLIENT_BROWSE_DNS_SERVERS;
269         c->dns_server_browser = avahi_dns_server_browser_new(avahi_server, -1, AF_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, AF_UNSPEC, dns_server_browser_callback, c);
270         client_output_printf(c, "+ Browsing ...\n");
271     } else {
272         client_output_printf(c, "- Invalid command \"%s\", try \"HELP\".\n", cmd);
273         c->state = CLIENT_DEAD;
274     }
275 }
276
277 static void handle_input(Client *c) {
278     g_assert(c);
279
280     for (;;) {
281         gchar *e;
282         guint k;
283
284         if (!(e = memchr(c->inbuf, '\n', c->inbuf_length)))
285             break;
286
287         k = e - (gchar*) c->inbuf;
288         *e = 0;
289         
290         handle_line(c, c->inbuf);
291         c->inbuf_length -= k + 1;
292         memmove(c->inbuf, e+1, c->inbuf_length);
293     }
294 }
295
296 static void client_work(Client *c) {
297     g_assert(c);
298
299     if ((c->poll_fd.revents & G_IO_IN) && c->inbuf_length < sizeof(c->inbuf)) {
300         ssize_t r;
301         
302         if ((r = read(c->fd, c->inbuf + c->inbuf_length, sizeof(c->inbuf) - c->inbuf_length)) <= 0) {
303             if (r < 0)
304                 avahi_log_warn("read(): %s", strerror(errno));
305             client_free(c);
306             return;
307         }
308
309         c->inbuf_length += r;
310         g_assert(c->inbuf_length <= sizeof(c->inbuf));
311
312         handle_input(c);
313     }
314
315     if ((c->poll_fd.revents & G_IO_OUT) && c->outbuf_length > 0) {
316         ssize_t r;
317
318         if ((r = write(c->fd, c->outbuf, c->outbuf_length)) < 0) {
319             avahi_log_warn("write(): %s", strerror(errno));
320             client_free(c);
321             return;
322         }
323
324         g_assert((guint) r <= c->outbuf_length);
325         c->outbuf_length -= r;
326         
327         if (c->outbuf_length)
328             memmove(c->outbuf, c->outbuf + r, c->outbuf_length - r);
329
330         if (c->outbuf_length == 0 && c->state == CLIENT_DEAD) {
331             client_free(c);
332             return;
333         }
334     }
335
336     c->poll_fd.events =
337         G_IO_ERR |
338         G_IO_HUP |
339         (c->outbuf_length > 0 ? G_IO_OUT : 0) |
340         (c->inbuf_length < sizeof(c->inbuf) ? G_IO_IN : 0);
341 }
342
343 static gboolean prepare_func(GSource *source, gint *timeout) {
344     g_assert(source);
345     g_assert(timeout);
346     
347     *timeout = -1;
348     return FALSE;
349 }
350
351 static gboolean check_func(GSource *source) {
352     Server *s = (Server*) source;
353     Client *c;
354     
355     g_assert(s);
356
357     if (s->poll_fd.revents)
358         return TRUE;
359     
360     for (c = s->clients; c; c = c->clients_next)
361         if (c->poll_fd.revents)
362             return TRUE;
363
364     return FALSE;
365 }
366
367 static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer user_data) {
368     Server *s = (Server*) source;
369     Client *c, *n;
370     
371     g_assert(s);
372
373     if (s->poll_fd.revents & G_IO_IN) {
374         gint fd;
375
376         if ((fd = accept(s->fd, NULL, NULL)) < 0)
377             avahi_log_warn("accept(): %s", strerror(errno));
378         else
379             client_new(s, fd);
380     } else if (s->poll_fd.revents)
381         g_error("Invalid revents");
382
383     for (c = s->clients; c; c = n) {
384         n = c->clients_next;
385         if (c->poll_fd.revents)
386             client_work(c);
387     }
388     
389     return TRUE;
390 }
391
392 int simple_protocol_setup(GMainContext *c) {
393     struct sockaddr_un sa;
394     mode_t u;
395
396     static GSourceFuncs source_funcs = {
397         prepare_func,
398         check_func,
399         dispatch_func,
400         NULL,
401         NULL,
402         NULL
403     };
404     
405     g_assert(!server);
406
407     server = (Server*) g_source_new(&source_funcs, sizeof(Server));
408     server->bind_successful = FALSE;
409     server->fd = -1;
410     AVAHI_LLIST_HEAD_INIT(Client, server->clients);
411     g_main_context_ref(server->context = (c ? c : g_main_context_default()));
412     server->clients = NULL;
413
414     u = umask(0000);
415
416     if ((server->fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
417         avahi_log_warn("socket(PF_LOCAL, SOCK_STREAM, 0): %s", strerror(errno));
418         goto fail;
419     }
420
421     memset(&sa, 0, sizeof(sa));
422     sa.sun_family = AF_LOCAL;
423     strncpy(sa.sun_path, UNIX_SOCKET, sizeof(sa.sun_path)-1);
424
425     /* We simply remove existing UNIX sockets under this name. The
426        Avahi daemons makes sure that it runs only once on a host,
427        therefore sockets that already exist are stale and may be
428        removed without any ill effects */
429
430     unlink(UNIX_SOCKET);
431     
432     if (bind(server->fd, &sa, sizeof(sa)) < 0) {
433         avahi_log_warn("bind(): %s", strerror(errno));
434         goto fail;
435     }
436
437     server->bind_successful = TRUE;
438     
439     if (listen(server->fd, 2) < 0) {
440         avahi_log_warn("listen(): %s", strerror(errno));
441         goto fail;
442     }
443
444     umask(u);
445
446     memset(&server->poll_fd, 0, sizeof(GPollFD));
447     server->poll_fd.fd = server->fd;
448     server->poll_fd.events = G_IO_IN|G_IO_ERR;
449     g_source_add_poll(&server->source, &server->poll_fd);
450
451     g_source_attach(&server->source, server->context);
452     
453     return 0;
454
455 fail:
456     
457     umask(u);
458     simple_protocol_shutdown();
459
460     return -1;
461 }
462
463 void simple_protocol_shutdown(void) {
464
465     if (server) {
466
467         while (server->clients)
468             client_free(server->clients);
469
470         if (server->bind_successful)
471             unlink(UNIX_SOCKET);
472         
473         if (server->fd >= 0)
474             close(server->fd);
475
476         g_main_context_unref(server->context);
477         
478         g_source_destroy(&server->source);
479         g_source_unref(&server->source);
480
481         server = NULL;
482     }
483 }