]> git.meshlink.io Git - catta/blob - avahi-daemon/simple-protocol.c
* add initial implmenentation of a "simple protocol" for usage with nss-mdns
[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
40 #define BUFFER_SIZE (10*1024)
41
42 #define UNIX_SOCKET_PATH "/tmp/avahi"
43 #define UNIX_SOCKET UNIX_SOCKET_PATH"/socket"
44
45 #define CLIENTS_MAX 50
46
47 typedef struct Client Client;
48 typedef struct Server Server;
49
50 struct Client {
51     Server *server;
52     
53     gint fd;
54     GPollFD poll_fd;
55
56     gchar inbuf[BUFFER_SIZE], outbuf[BUFFER_SIZE];
57     guint inbuf_length, outbuf_length;
58     
59     AVAHI_LLIST_FIELDS(Client, clients);
60 };
61
62 struct Server {
63     GSource source;
64     GMainContext *context;
65     GPollFD poll_fd;
66     gint fd;
67     AVAHI_LLIST_HEAD(Client, clients);
68
69     guint n_clients;
70 };
71
72 static Server *server = NULL;
73
74 static void client_free(Client *c) {
75     g_assert(c);
76
77     g_assert(c->server->n_clients >= 1);
78     c->server->n_clients--;
79     
80     g_source_remove_poll(&c->server->source, &c->poll_fd);
81     close(c->fd);
82     AVAHI_LLIST_REMOVE(Client, clients, c->server->clients, c);
83     g_free(c);
84 }
85
86 static void client_new(Server *s, int fd) {
87     Client *c;
88
89     g_assert(fd >= 0);
90
91     c = g_new(Client, 1);
92     c->server = s;
93     c->fd = fd;
94
95     c->inbuf_length = c->outbuf_length = 0;
96
97     memset(&c->poll_fd, 0, sizeof(GPollFD));
98     c->poll_fd.fd = fd;
99     c->poll_fd.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
100     g_source_add_poll(&s->source, &c->poll_fd);
101
102     AVAHI_LLIST_PREPEND(Client, clients, s->clients, c);
103     s->n_clients++;
104 }
105
106 static void client_output(Client *c, const guint8*data, guint size) {
107     guint k, m;
108     
109     g_assert(c);
110     g_assert(data);
111
112     if (!size)
113         return;
114
115     k = sizeof(c->outbuf) - c->outbuf_length;
116     m = size > k ? k : size;
117
118     memcpy(c->outbuf + c->outbuf_length, data, m);
119     c->outbuf_length += m;
120 }
121
122 static void handle_line(Client *c, const gchar *s) {
123     gchar t[256];
124
125     g_assert(c);
126     g_assert(s);
127
128     snprintf(t, sizeof(t), "you said <%s>\n", s);
129     client_output(c, (guint8*) t, strlen(t));
130 }
131
132 static void handle_input(Client *c) {
133     g_assert(c);
134
135     for (;;) {
136         gchar *e;
137         guint k;
138
139         if (!(e = memchr(c->inbuf, '\n', c->inbuf_length)))
140             break;
141
142         k = e - (gchar*) c->inbuf;
143         *e = 0;
144         
145         handle_line(c, c->inbuf);
146         c->inbuf_length -= k + 1;
147         memmove(c->inbuf, e+1, c->inbuf_length);
148     }
149 }
150
151 static void client_work(Client *c) {
152     g_assert(c);
153
154     if ((c->poll_fd.revents & G_IO_IN) && c->inbuf_length < sizeof(c->inbuf)) {
155         ssize_t r;
156         
157         if ((r = read(c->fd, c->inbuf + c->inbuf_length, sizeof(c->inbuf) - c->inbuf_length)) <= 0) {
158             if (r < 0)
159                 g_warning("read(): %s", strerror(errno));
160             client_free(c);
161             return;
162         }
163
164         c->inbuf_length += r;
165         g_assert(c->inbuf_length <= sizeof(c->inbuf));
166
167         handle_input(c);
168     }
169
170     if ((c->poll_fd.revents & G_IO_OUT) && c->outbuf_length > 0) {
171         ssize_t r;
172
173         if ((r = write(c->fd, c->outbuf, c->outbuf_length)) < 0) {
174             g_warning("write(): %s", strerror(errno));
175             client_free(c);
176             return;
177         }
178
179         g_assert((guint) r <= c->outbuf_length);
180         c->outbuf_length -= r;
181         
182         if (c->outbuf_length)
183             memmove(c->outbuf, c->outbuf + r, c->outbuf_length - r);
184     }
185
186     c->poll_fd.events =
187         G_IO_ERR |
188         G_IO_HUP |
189         (c->outbuf_length > 0 ? G_IO_OUT : 0) |
190         (c->inbuf_length < sizeof(c->inbuf) ? G_IO_IN : 0);
191 }
192
193 static gboolean prepare_func(GSource *source, gint *timeout) {
194     g_assert(source);
195     g_assert(timeout);
196     
197     *timeout = -1;
198     return FALSE;
199 }
200
201 static gboolean check_func(GSource *source) {
202     Server *s = (Server*) source;
203     Client *c;
204     
205     g_assert(s);
206
207     if (s->poll_fd.revents)
208         return TRUE;
209     
210     for (c = s->clients; c; c = c->clients_next)
211         if (c->poll_fd.revents)
212             return TRUE;
213
214     return FALSE;
215 }
216
217 static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer user_data) {
218     Server *s = (Server*) source;
219     Client *c, *n;
220     
221     g_assert(s);
222
223     if (s->poll_fd.revents & G_IO_IN) {
224         gint fd;
225
226         if ((fd = accept(s->fd, NULL, NULL)) < 0)
227             g_warning("accept(): %s", strerror(errno));
228         else
229             client_new(s, fd);
230     } else if (s->poll_fd.revents)
231         g_error("Invalid revents");
232
233     for (c = s->clients; c; c = n) {
234         n = c->clients_next;
235         if (c->poll_fd.revents)
236             client_work(c);
237     }
238     
239     return TRUE;
240 }
241
242 int simple_protocol_setup(GMainContext *c) {
243     struct sockaddr_un sa;
244     mode_t u;
245
246     static GSourceFuncs source_funcs = {
247         prepare_func,
248         check_func,
249         dispatch_func,
250         NULL,
251         NULL,
252         NULL
253     };
254     
255     g_assert(!server);
256
257     server = (Server*) g_source_new(&source_funcs, sizeof(Server));
258     server->fd = -1;
259     AVAHI_LLIST_HEAD_INIT(Client, server->clients);
260     if (c)
261         g_main_context_ref(server->context = c);
262     else
263         server->context = g_main_context_default();
264     server->clients = NULL;
265
266     u = umask(0000);
267
268     if (mkdir(UNIX_SOCKET_PATH, 0755) < 0 && errno != EEXIST) {
269         g_warning("mkdir(): %s", strerror(errno));
270         goto fail;
271     }
272     
273     if ((server->fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
274         g_warning("socket(PF_LOCAL, SOCK_STREAM, 0): %s", strerror(errno));
275         goto fail;
276     }
277
278     memset(&sa, 0, sizeof(sa));
279     sa.sun_family = AF_LOCAL;
280     strncpy(sa.sun_path, UNIX_SOCKET, sizeof(sa.sun_path)-1);
281
282     if (bind(server->fd, &sa, sizeof(sa)) < 0) {
283         g_warning("bind(): %s", strerror(errno));
284         goto fail;
285     }
286     
287     if (listen(server->fd, 2) < 0) {
288         g_warning("listen(): %s", strerror(errno));
289         goto fail;
290     }
291
292     umask(u);
293
294     memset(&server->poll_fd, 0, sizeof(GPollFD));
295     server->poll_fd.fd = server->fd;
296     server->poll_fd.events = G_IO_IN|G_IO_ERR;
297     g_source_add_poll(&server->source, &server->poll_fd);
298
299     g_source_attach(&server->source, server->context);
300     
301     return 0;
302
303 fail:
304     
305     umask(u);
306     simple_protocol_shutdown();
307
308     return -1;
309 }
310
311 void simple_protocol_shutdown(void) {
312
313     if (server) {
314
315         while (server->clients)
316             client_free(server->clients);
317         
318         if (server->fd >= 0) {
319             unlink(UNIX_SOCKET_PATH);
320             close(server->fd);
321         }
322
323         g_main_context_unref(server->context);
324         g_source_destroy(&server->source);
325         g_source_unref(&server->source);
326
327         server = NULL;
328     }
329 }