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