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