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