summaryrefslogtreecommitdiffstats
path: root/avahi-daemon/simple-protocol.c
blob: 9a1b1e2da66ac966193e351ac075b520f8619914 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/* $Id$ */

/***
  This file is part of avahi.
 
  avahi is free software; you can redistribute it and/or modify it
  under the terms of the GNU Lesser General Public License as
  published by the Free Software Foundation; either version 2.1 of the
  License, or (at your option) any later version.
 
  avahi is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
  Public License for more details.
 
  You should have received a copy of the GNU Lesser General Public
  License along with avahi; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>
#include <sys/socket.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/un.h>
#include <errno.h>
#include <fcntl.h>

#include <glib.h>

#include <avahi-core/llist.h>
#include <avahi-core/log.h>

#include "simple-protocol.h"
#include "main.h"

#define BUFFER_SIZE (20*1024)

#define CLIENTS_MAX 50

typedef struct Client Client;
typedef struct Server Server;

typedef enum {
    CLIENT_IDLE,
    CLIENT_RESOLVE_HOSTNAME,
    CLIENT_RESOLVE_ADDRESS,
    CLIENT_BROWSE_DNS_SERVERS,
    CLIENT_DEAD
} ClientState;

struct Client {
    Server *server;

    ClientState state;
    
    gint fd;
    GPollFD poll_fd;

    gchar inbuf[BUFFER_SIZE], outbuf[BUFFER_SIZE];
    guint inbuf_length, outbuf_length;

    AvahiHostNameResolver *host_name_resolver;
    AvahiAddressResolver *address_resolver;
    AvahiDNSServerBrowser *dns_server_browser;

    AvahiProtocol afquery;
    
    AVAHI_LLIST_FIELDS(Client, clients);
};

struct Server {
    GSource source;
    GMainContext *context;
    GPollFD poll_fd;
    gint fd;
    AVAHI_LLIST_HEAD(Client, clients);

    guint n_clients;
    gboolean bind_successful;
};

static Server *server = NULL;

static void client_free(Client *c) {
    g_assert(c);

    g_assert(c->server->n_clients >= 1);
    c->server->n_clients--;

    if (c->host_name_resolver)
        avahi_host_name_resolver_free(c->host_name_resolver);

    if (c->address_resolver)
        avahi_address_resolver_free(c->address_resolver);

    if (c->dns_server_browser)
        avahi_dns_server_browser_free(c->dns_server_browser);
    
    g_source_remove_poll(&c->server->source, &c->poll_fd);
    close(c->fd);
    AVAHI_LLIST_REMOVE(Client, clients, c->server->clients, c);
    g_free(c);
}

static void client_new(Server *s, int fd) {
    Client *c;

    g_assert(fd >= 0);

    c = g_new(Client, 1);
    c->server = s;
    c->fd = fd;
    c->state = CLIENT_IDLE;

    c->inbuf_length = c->outbuf_length = 0;

    c->host_name_resolver = NULL;
    c->address_resolver = NULL;
    c->dns_server_browser = NULL;

    memset(&c->poll_fd, 0, sizeof(GPollFD));
    c->poll_fd.fd = fd;
    c->poll_fd.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
    g_source_add_poll(&s->source, &c->poll_fd);

    AVAHI_LLIST_PREPEND(Client, clients, s->clients, c);
    s->n_clients++;
}

static void client_output(Client *c, const guint8*data, guint size) {
    guint k, m;
    
    g_assert(c);
    g_assert(data);

    if (!size)
        return;

    k = sizeof(c->outbuf) - c->outbuf_length;
    m = size > k ? k : size;

    memcpy(c->outbuf + c->outbuf_length, data, m);
    c->outbuf_length += m;

    c->poll_fd.events |= G_IO_OUT;
}

static void client_output_printf(Client *c, const gchar *format, ...) {
    gchar *t;
    va_list ap;
    
    va_start(ap, format);
    t = g_strdup_vprintf(format, ap);
    va_end(ap);

    client_output(c, (guint8*) t, strlen(t));
    g_free(t);
}


static void host_name_resolver_callback(AvahiHostNameResolver *r, gint iface, guchar protocol, AvahiBrowserEvent event, const gchar *hostname, const AvahiAddress *a, gpointer userdata) {
    Client *c = userdata;
    
    g_assert(c);


    if (event == AVAHI_RESOLVER_TIMEOUT)
        client_output_printf(c, "- Query timed out\n");
    else {
        gchar t[64];
        avahi_address_snprint(t, sizeof(t), a);
        client_output_printf(c, "+ %i %u %s %s\n", iface, protocol, hostname, t);
    }

    c->state = CLIENT_DEAD;
}

static void address_resolver_callback(AvahiAddressResolver *r, gint iface, guchar protocol, AvahiBrowserEvent event, const AvahiAddress *a, const gchar *hostname, gpointer userdata) {
    Client *c = userdata;
    
    
    g_assert(c);

    if (event == AVAHI_RESOLVER_TIMEOUT)
        client_output_printf(c, "- Query timed out\n");
    else 
        client_output_printf(c, "+ %i %u %s\n", iface, protocol, hostname);

    c->state = CLIENT_DEAD;
}

static void dns_server_browser_callback(AvahiDNSServerBrowser *b, gint interface, guchar protocol, AvahiBrowserEvent event, const gchar *host_name, const AvahiAddress *a, guint16 port, gpointer userdata) {
    Client *c = userdata;
    gchar t[64];
    
    g_assert(c);

    if (!a)
        return;

    avahi_address_snprint(t, sizeof(t), a);
    client_output_printf(c, "%c %i %u %s %u\n", event == AVAHI_BROWSER_NEW ? '>' : '<',  interface, protocol, t, port);
}

static void handle_line(Client *c, const gchar *s) {
    gchar cmd[64], arg[64];
    gint n_args;

    g_assert(c);
    g_assert(s);

    if (c->state != CLIENT_IDLE)
        return;

    if ((n_args = sscanf(s, "%63s %63s", cmd, arg)) < 1 ) {
        client_output_printf(c, "- Failed to parse command, try \"HELP\".\n");
        c->state = CLIENT_DEAD;
        return;
    }

    if (strcmp(cmd, "HELP") == 0) {
        client_output_printf(c,
                             "+ Available commands are:\n"
                             "+      RESOLVE-HOSTNAME <hostname>\n"
                             "+      RESOLVE-HOSTNAME-IPV6 <hostname>\n"
                             "+      RESOLVE-HOSTNAME-IPV4 <hostname>\n"
                             "+      RESOLVE-ADDRESS <address>\n"
                             "+      BROWSE-DNS-SERVERS\n"
                             "+      BROWSE-DNS-SERVERS-IPV4\n"
                             "+      BROWSE-DNS-SERVERS-IPV6\n");
        c->state = CLIENT_DEAD; }
    else if (strcmp(cmd, "FUCK") == 0 && n_args == 1) {
        client_output_printf(c, "FUCK: Go fuck yourself!\n");
        c->state = CLIENT_DEAD;
    } else if (strcmp(cmd, "RESOLVE-HOSTNAME-IPV4") == 0 && n_args == 2) {
        c->state = CLIENT_RESOLVE_HOSTNAME;
        c->host_name_resolver = avahi_host_name_resolver_new(avahi_server, -1, AF_UNSPEC, arg, c->afquery = AF_INET, host_name_resolver_callback, c);
    } else if (strcmp(cmd, "RESOLVE-HOSTNAME-IPV6") == 0 && n_args == 2) {
        c->state = CLIENT_RESOLVE_HOSTNAME;
        c->host_name_resolver = avahi_host_name_resolver_new(avahi_server, -1, AF_UNSPEC, arg, c->afquery = AF_INET6, host_name_resolver_callback, c);
    } else if (strcmp(cmd, "RESOLVE-HOSTNAME") == 0 && n_args == 2) {
        c->state = CLIENT_RESOLVE_HOSTNAME;
        c->host_name_resolver = avahi_host_name_resolver_new(avahi_server, -1, AF_UNSPEC, arg, c->afquery = AF_UNSPEC, host_name_resolver_callback, c);
    } else if (strcmp(cmd, "RESOLVE-ADDRESS") == 0 && n_args == 2) {
        AvahiAddress addr;
        
        if (!(avahi_address_parse(arg, AF_UNSPEC, &addr))) {
            client_output_printf(c, "- Failed to parse address \"%s\".\n", arg);
            c->state = CLIENT_DEAD;
        } else {
            c->state = CLIENT_RESOLVE_ADDRESS;
            c->address_resolver = avahi_address_resolver_new(avahi_server, -1, AF_UNSPEC, &addr, address_resolver_callback, c);
        }
    } else if (strcmp(cmd, "BROWSE-DNS-SERVERS-IPV4") == 0 && n_args == 1) {
        c->state = CLIENT_BROWSE_DNS_SERVERS;
        c->dns_server_browser = avahi_dns_server_browser_new(avahi_server, -1, AF_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, c->afquery = AF_INET, dns_server_browser_callback, c);
        client_output_printf(c, "+ Browsing ...\n");
    } else if (strcmp(cmd, "BROWSE-DNS-SERVERS-IPV6") == 0 && n_args == 1) {
        c->state = CLIENT_BROWSE_DNS_SERVERS;
        c->dns_server_browser = avahi_dns_server_browser_new(avahi_server, -1, AF_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, c->afquery = AF_INET6, dns_server_browser_callback, c);
        client_output_printf(c, "+ Browsing ...\n");
    } else if (strcmp(cmd, "BROWSE-DNS-SERVERS") == 0 && n_args == 1) {
        c->state = CLIENT_BROWSE_DNS_SERVERS;
        c->dns_server_browser = avahi_dns_server_browser_new(avahi_server, -1, AF_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, c->afquery = AF_UNSPEC, dns_server_browser_callback, c);
        client_output_printf(c, "+ Browsing ...\n");
    } else {
        client_output_printf(c, "- Invalid command \"%s\", try \"HELP\".\n", cmd);
        c->state = CLIENT_DEAD;
    }
}

static void handle_input(Client *c) {
    g_assert(c);

    for (;;) {
        gchar *e;
        guint k;

        if (!(e = memchr(c->inbuf, '\n', c->inbuf_length)))
            break;

        k = e - (gchar*) c->inbuf;
        *e = 0;
        
        handle_line(c, c->inbuf);
        c->inbuf_length -= k + 1;
        memmove(c->inbuf, e+1, c->inbuf_length);
    }
}

static void client_work(Client *c) {
    g_assert(c);

    if ((c->poll_fd.revents & G_IO_IN) && c->inbuf_length < sizeof(c->inbuf)) {
        ssize_t r;
        
        if ((r = read(c->fd, c->inbuf + c->inbuf_length, sizeof(c->inbuf) - c->inbuf_length)) <= 0) {
            if (r < 0)
                avahi_log_warn("read(): %s", strerror(errno));
            client_free(c);
            return;
        }

        c->inbuf_length += r;
        g_assert(c->inbuf_length <= sizeof(c->inbuf));

        handle_input(c);
    }

    if ((c->poll_fd.revents & G_IO_OUT) && c->outbuf_length > 0) {
        ssize_t r;

        if ((r = write(c->fd, c->outbuf, c->outbuf_length)) < 0) {
            avahi_log_warn("write(): %s", strerror(errno));
            client_free(c);
            return;
        }

        g_assert((guint) r <= c->outbuf_length);
        c->outbuf_length -= r;
        
        if (c->outbuf_length)
            memmove(c->outbuf, c->outbuf + r, c->outbuf_length - r);

        if (c->outbuf_length == 0 && c->state == CLIENT_DEAD) {
            client_free(c);
            return;
        }
    }

    c->poll_fd.events =
        G_IO_ERR |
        G_IO_HUP |
        (c->outbuf_length > 0 ? G_IO_OUT : 0) |
        (c->inbuf_length < sizeof(c->inbuf) ? G_IO_IN : 0);
}

static gboolean prepare_func(GSource *source, gint *timeout) {
    g_assert(source);
    g_assert(timeout);
    
    *timeout = -1;
    return FALSE;
}

static gboolean check_func(GSource *source) {
    Server *s = (Server*) source;
    Client *c;
    
    g_assert(s);

    if (s->poll_fd.revents)
        return TRUE;
    
    for (c = s->clients; c; c = c->clients_next)
        if (c->poll_fd.revents)
            return TRUE;

    return FALSE;
}

static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer user_data) {
    Server *s = (Server*) source;
    Client *c, *n;
    
    g_assert(s);

    if (s->poll_fd.revents & G_IO_IN) {
        gint fd;

        if ((fd = accept(s->fd, NULL, NULL)) < 0)
            avahi_log_warn("accept(): %s", strerror(errno));
        else
            client_new(s, fd);
    } else if (s->poll_fd.revents)
        g_error("Invalid revents");

    for (c = s->clients; c; c = n) {
        n = c->clients_next;
        if (c->poll_fd.revents)
            client_work(c);
    }
    
    return TRUE;
}

int simple_protocol_setup(GMainContext *c) {
    struct sockaddr_un sa;
    mode_t u;

    static GSourceFuncs source_funcs = {
        prepare_func,
        check_func,
        dispatch_func,
        NULL,
        NULL,
        NULL
    };
    
    g_assert(!server);

    server = (Server*) g_source_new(&source_funcs, sizeof(Server));
    server->bind_successful = FALSE;
    server->fd = -1;
    AVAHI_LLIST_HEAD_INIT(Client, server->clients);
    g_main_context_ref(server->context = (c ? c : g_main_context_default()));
    server->clients = NULL;

    u = umask(0000);

    if ((server->fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
        avahi_log_warn("socket(PF_LOCAL, SOCK_STREAM, 0): %s", strerror(errno));
        goto fail;
    }

    memset(&sa, 0, sizeof(sa));
    sa.sun_family = AF_LOCAL;
    strncpy(sa.sun_path, AVAHI_SOCKET, sizeof(sa.sun_path)-1);

    /* We simply remove existing UNIX sockets under this name. The
       Avahi daemons makes sure that it runs only once on a host,
       therefore sockets that already exist are stale and may be
       removed without any ill effects */

    unlink(AVAHI_SOCKET);
    
    if (bind(server->fd, &sa, sizeof(sa)) < 0) {
        avahi_log_warn("bind(): %s", strerror(errno));
        goto fail;
    }

    server->bind_successful = TRUE;
    
    if (listen(server->fd, 2) < 0) {
        avahi_log_warn("listen(): %s", strerror(errno));
        goto fail;
    }

    umask(u);

    memset(&server->poll_fd, 0, sizeof(GPollFD));
    server->poll_fd.fd = server->fd;
    server->poll_fd.events = G_IO_IN|G_IO_ERR;
    g_source_add_poll(&server->source, &server->poll_fd);

    g_source_attach(&server->source, server->context);
    
    return 0;

fail:
    
    umask(u);
    simple_protocol_shutdown();

    return -1;
}

void simple_protocol_shutdown(void) {

    if (server) {

        while (server->clients)
            client_free(server->clients);

        if (server->bind_successful)
            unlink(AVAHI_SOCKET);
        
        if (server->fd >= 0)
            close(server->fd);

        g_main_context_unref(server->context);
        
        g_source_destroy(&server->source);
        g_source_unref(&server->source);

        server = NULL;
    }
}

void simple_protocol_restart_queries(void) {
    Client *c;

    /* Restart queries in case of local domain name changes */
    
    g_assert(server);

    for (c = server->clients; c; c = c->clients_next)
        if (c->state == CLIENT_BROWSE_DNS_SERVERS && c->dns_server_browser) {
            avahi_dns_server_browser_free(c->dns_server_browser);
            c->dns_server_browser = avahi_dns_server_browser_new(avahi_server, -1, AF_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, c->afquery, dns_server_browser_callback, c);
        }
}