4 This file is part of avahi.
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.
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.
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
27 #include <sys/socket.h>
36 #include <avahi-core/llist.h>
38 #include "simple-protocol.h"
40 #define BUFFER_SIZE (10*1024)
42 #define UNIX_SOCKET_PATH "/tmp/avahi"
43 #define UNIX_SOCKET UNIX_SOCKET_PATH"/socket"
45 #define CLIENTS_MAX 50
47 typedef struct Client Client;
48 typedef struct Server Server;
56 gchar inbuf[BUFFER_SIZE], outbuf[BUFFER_SIZE];
57 guint inbuf_length, outbuf_length;
59 AVAHI_LLIST_FIELDS(Client, clients);
64 GMainContext *context;
67 AVAHI_LLIST_HEAD(Client, clients);
72 static Server *server = NULL;
74 static void client_free(Client *c) {
77 g_assert(c->server->n_clients >= 1);
78 c->server->n_clients--;
80 g_source_remove_poll(&c->server->source, &c->poll_fd);
82 AVAHI_LLIST_REMOVE(Client, clients, c->server->clients, c);
86 static void client_new(Server *s, int fd) {
95 c->inbuf_length = c->outbuf_length = 0;
97 memset(&c->poll_fd, 0, sizeof(GPollFD));
99 c->poll_fd.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
100 g_source_add_poll(&s->source, &c->poll_fd);
102 AVAHI_LLIST_PREPEND(Client, clients, s->clients, c);
106 static void client_output(Client *c, const guint8*data, guint size) {
115 k = sizeof(c->outbuf) - c->outbuf_length;
116 m = size > k ? k : size;
118 memcpy(c->outbuf + c->outbuf_length, data, m);
119 c->outbuf_length += m;
122 static void handle_line(Client *c, const gchar *s) {
128 snprintf(t, sizeof(t), "you said <%s>\n", s);
129 client_output(c, (guint8*) t, strlen(t));
132 static void handle_input(Client *c) {
139 if (!(e = memchr(c->inbuf, '\n', c->inbuf_length)))
142 k = e - (gchar*) c->inbuf;
145 handle_line(c, c->inbuf);
146 c->inbuf_length -= k + 1;
147 memmove(c->inbuf, e+1, c->inbuf_length);
151 static void client_work(Client *c) {
154 if ((c->poll_fd.revents & G_IO_IN) && c->inbuf_length < sizeof(c->inbuf)) {
157 if ((r = read(c->fd, c->inbuf + c->inbuf_length, sizeof(c->inbuf) - c->inbuf_length)) <= 0) {
159 g_warning("read(): %s", strerror(errno));
164 c->inbuf_length += r;
165 g_assert(c->inbuf_length <= sizeof(c->inbuf));
170 if ((c->poll_fd.revents & G_IO_OUT) && c->outbuf_length > 0) {
173 if ((r = write(c->fd, c->outbuf, c->outbuf_length)) < 0) {
174 g_warning("write(): %s", strerror(errno));
179 g_assert((guint) r <= c->outbuf_length);
180 c->outbuf_length -= r;
182 if (c->outbuf_length)
183 memmove(c->outbuf, c->outbuf + r, c->outbuf_length - r);
189 (c->outbuf_length > 0 ? G_IO_OUT : 0) |
190 (c->inbuf_length < sizeof(c->inbuf) ? G_IO_IN : 0);
193 static gboolean prepare_func(GSource *source, gint *timeout) {
201 static gboolean check_func(GSource *source) {
202 Server *s = (Server*) source;
207 if (s->poll_fd.revents)
210 for (c = s->clients; c; c = c->clients_next)
211 if (c->poll_fd.revents)
217 static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer user_data) {
218 Server *s = (Server*) source;
223 if (s->poll_fd.revents & G_IO_IN) {
226 if ((fd = accept(s->fd, NULL, NULL)) < 0)
227 g_warning("accept(): %s", strerror(errno));
230 } else if (s->poll_fd.revents)
231 g_error("Invalid revents");
233 for (c = s->clients; c; c = n) {
235 if (c->poll_fd.revents)
242 int simple_protocol_setup(GMainContext *c) {
243 struct sockaddr_un sa;
246 static GSourceFuncs source_funcs = {
257 server = (Server*) g_source_new(&source_funcs, sizeof(Server));
259 AVAHI_LLIST_HEAD_INIT(Client, server->clients);
261 g_main_context_ref(server->context = c);
263 server->context = g_main_context_default();
264 server->clients = NULL;
268 if (mkdir(UNIX_SOCKET_PATH, 0755) < 0 && errno != EEXIST) {
269 g_warning("mkdir(): %s", strerror(errno));
273 if ((server->fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
274 g_warning("socket(PF_LOCAL, SOCK_STREAM, 0): %s", strerror(errno));
278 memset(&sa, 0, sizeof(sa));
279 sa.sun_family = AF_LOCAL;
280 strncpy(sa.sun_path, UNIX_SOCKET, sizeof(sa.sun_path)-1);
282 if (bind(server->fd, &sa, sizeof(sa)) < 0) {
283 g_warning("bind(): %s", strerror(errno));
287 if (listen(server->fd, 2) < 0) {
288 g_warning("listen(): %s", strerror(errno));
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);
299 g_source_attach(&server->source, server->context);
306 simple_protocol_shutdown();
311 void simple_protocol_shutdown(void) {
315 while (server->clients)
316 client_free(server->clients);
318 if (server->fd >= 0) {
319 unlink(UNIX_SOCKET_PATH);
323 g_main_context_unref(server->context);
324 g_source_destroy(&server->source);
325 g_source_unref(&server->source);