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>
37 #include <avahi-core/log.h>
39 #include "simple-protocol.h"
42 #define BUFFER_SIZE (20*1024)
44 #define CLIENTS_MAX 50
46 typedef struct Client Client;
47 typedef struct Server Server;
51 CLIENT_RESOLVE_HOSTNAME,
52 CLIENT_RESOLVE_ADDRESS,
53 CLIENT_BROWSE_DNS_SERVERS,
65 gchar inbuf[BUFFER_SIZE], outbuf[BUFFER_SIZE];
66 guint inbuf_length, outbuf_length;
68 AvahiHostNameResolver *host_name_resolver;
69 AvahiAddressResolver *address_resolver;
70 AvahiDNSServerBrowser *dns_server_browser;
72 AvahiProtocol afquery;
74 AVAHI_LLIST_FIELDS(Client, clients);
79 GMainContext *context;
82 AVAHI_LLIST_HEAD(Client, clients);
85 gboolean bind_successful;
88 static Server *server = NULL;
90 static void client_free(Client *c) {
93 g_assert(c->server->n_clients >= 1);
94 c->server->n_clients--;
96 if (c->host_name_resolver)
97 avahi_host_name_resolver_free(c->host_name_resolver);
99 if (c->address_resolver)
100 avahi_address_resolver_free(c->address_resolver);
102 if (c->dns_server_browser)
103 avahi_dns_server_browser_free(c->dns_server_browser);
105 g_source_remove_poll(&c->server->source, &c->poll_fd);
107 AVAHI_LLIST_REMOVE(Client, clients, c->server->clients, c);
111 static void client_new(Server *s, int fd) {
116 c = g_new(Client, 1);
119 c->state = CLIENT_IDLE;
121 c->inbuf_length = c->outbuf_length = 0;
123 c->host_name_resolver = NULL;
124 c->address_resolver = NULL;
125 c->dns_server_browser = NULL;
127 memset(&c->poll_fd, 0, sizeof(GPollFD));
129 c->poll_fd.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
130 g_source_add_poll(&s->source, &c->poll_fd);
132 AVAHI_LLIST_PREPEND(Client, clients, s->clients, c);
136 static void client_output(Client *c, const guint8*data, guint size) {
145 k = sizeof(c->outbuf) - c->outbuf_length;
146 m = size > k ? k : size;
148 memcpy(c->outbuf + c->outbuf_length, data, m);
149 c->outbuf_length += m;
151 c->poll_fd.events |= G_IO_OUT;
154 static void client_output_printf(Client *c, const gchar *format, ...) {
158 va_start(ap, format);
159 t = g_strdup_vprintf(format, ap);
162 client_output(c, (guint8*) t, strlen(t));
167 static void host_name_resolver_callback(AvahiHostNameResolver *r, AvahiIfIndex iface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *hostname, const AvahiAddress *a, gpointer userdata) {
168 Client *c = userdata;
173 if (event == AVAHI_RESOLVER_TIMEOUT)
174 client_output_printf(c, "%+i Query timed out\n", AVAHI_ERR_TIMEOUT);
177 avahi_address_snprint(t, sizeof(t), a);
178 client_output_printf(c, "+ %i %u %s %s\n", iface, protocol, hostname, t);
181 c->state = CLIENT_DEAD;
184 static void address_resolver_callback(AvahiAddressResolver *r, AvahiIfIndex iface, AvahiProtocol protocol, AvahiBrowserEvent event, const AvahiAddress *a, const gchar *hostname, gpointer userdata) {
185 Client *c = userdata;
190 if (event == AVAHI_RESOLVER_TIMEOUT)
191 client_output_printf(c, "%+i Query timed out\n", AVAHI_ERR_TIMEOUT);
193 client_output_printf(c, "+ %i %u %s\n", iface, protocol, hostname);
195 c->state = CLIENT_DEAD;
198 static void dns_server_browser_callback(AvahiDNSServerBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *host_name, const AvahiAddress *a, guint16 port, gpointer userdata) {
199 Client *c = userdata;
207 avahi_address_snprint(t, sizeof(t), a);
208 client_output_printf(c, "%c %i %u %s %u\n", event == AVAHI_BROWSER_NEW ? '>' : '<', interface, protocol, t, port);
211 static void handle_line(Client *c, const gchar *s) {
212 gchar cmd[64], arg[64];
218 if (c->state != CLIENT_IDLE)
221 if ((n_args = sscanf(s, "%63s %63s", cmd, arg)) < 1 ) {
222 client_output_printf(c, "%+i Failed to parse command, try \"HELP\".\n", AVAHI_ERR_INVALID_OPERATION);
223 c->state = CLIENT_DEAD;
227 if (strcmp(cmd, "HELP") == 0) {
228 client_output_printf(c,
229 "+ Available commands are:\n"
230 "+ RESOLVE-HOSTNAME <hostname>\n"
231 "+ RESOLVE-HOSTNAME-IPV6 <hostname>\n"
232 "+ RESOLVE-HOSTNAME-IPV4 <hostname>\n"
233 "+ RESOLVE-ADDRESS <address>\n"
234 "+ BROWSE-DNS-SERVERS\n"
235 "+ BROWSE-DNS-SERVERS-IPV4\n"
236 "+ BROWSE-DNS-SERVERS-IPV6\n");
237 c->state = CLIENT_DEAD; }
238 else if (strcmp(cmd, "FUCK") == 0 && n_args == 1) {
239 client_output_printf(c, "+ FUCK: Go fuck yourself!\n");
240 c->state = CLIENT_DEAD;
241 } else if (strcmp(cmd, "RESOLVE-HOSTNAME-IPV4") == 0 && n_args == 2) {
242 c->state = CLIENT_RESOLVE_HOSTNAME;
243 if (!(c->host_name_resolver = avahi_host_name_resolver_new(avahi_server, -1, AF_UNSPEC, arg, c->afquery = AF_INET, host_name_resolver_callback, c)))
245 } else if (strcmp(cmd, "RESOLVE-HOSTNAME-IPV6") == 0 && n_args == 2) {
246 c->state = CLIENT_RESOLVE_HOSTNAME;
247 if (!(c->host_name_resolver = avahi_host_name_resolver_new(avahi_server, -1, AF_UNSPEC, arg, c->afquery = AF_INET6, host_name_resolver_callback, c)))
249 } else if (strcmp(cmd, "RESOLVE-HOSTNAME") == 0 && n_args == 2) {
250 c->state = CLIENT_RESOLVE_HOSTNAME;
251 if (!(c->host_name_resolver = avahi_host_name_resolver_new(avahi_server, -1, AF_UNSPEC, arg, c->afquery = AF_UNSPEC, host_name_resolver_callback, c)))
253 } else if (strcmp(cmd, "RESOLVE-ADDRESS") == 0 && n_args == 2) {
256 if (!(avahi_address_parse(arg, AF_UNSPEC, &addr))) {
257 client_output_printf(c, "%+i Failed to parse address \"%s\".\n", AVAHI_ERR_INVALID_ADDRESS, arg);
258 c->state = CLIENT_DEAD;
260 c->state = CLIENT_RESOLVE_ADDRESS;
261 if (!(c->address_resolver = avahi_address_resolver_new(avahi_server, -1, AF_UNSPEC, &addr, address_resolver_callback, c)))
264 } else if (strcmp(cmd, "BROWSE-DNS-SERVERS-IPV4") == 0 && n_args == 1) {
265 c->state = CLIENT_BROWSE_DNS_SERVERS;
266 if (!(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)))
268 client_output_printf(c, "+ Browsing ...\n");
269 } else if (strcmp(cmd, "BROWSE-DNS-SERVERS-IPV6") == 0 && n_args == 1) {
270 c->state = CLIENT_BROWSE_DNS_SERVERS;
271 if (!(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)))
273 client_output_printf(c, "+ Browsing ...\n");
274 } else if (strcmp(cmd, "BROWSE-DNS-SERVERS") == 0 && n_args == 1) {
275 c->state = CLIENT_BROWSE_DNS_SERVERS;
276 if (!(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)))
278 client_output_printf(c, "+ Browsing ...\n");
280 client_output_printf(c, "%+i Invalid command \"%s\", try \"HELP\".\n", AVAHI_ERR_INVALID_OPERATION, cmd);
281 c->state = CLIENT_DEAD;
287 client_output_printf(c, "%+i %s\n", avahi_server_errno(avahi_server), avahi_strerror(avahi_server_errno(avahi_server)));
288 c->state = CLIENT_DEAD;
291 static void handle_input(Client *c) {
298 if (!(e = memchr(c->inbuf, '\n', c->inbuf_length)))
301 k = e - (gchar*) c->inbuf;
304 handle_line(c, c->inbuf);
305 c->inbuf_length -= k + 1;
306 memmove(c->inbuf, e+1, c->inbuf_length);
310 static void client_work(Client *c) {
313 if ((c->poll_fd.revents & G_IO_IN) && c->inbuf_length < sizeof(c->inbuf)) {
316 if ((r = read(c->fd, c->inbuf + c->inbuf_length, sizeof(c->inbuf) - c->inbuf_length)) <= 0) {
318 avahi_log_warn("read(): %s", strerror(errno));
323 c->inbuf_length += r;
324 g_assert(c->inbuf_length <= sizeof(c->inbuf));
329 if ((c->poll_fd.revents & G_IO_OUT) && c->outbuf_length > 0) {
332 if ((r = write(c->fd, c->outbuf, c->outbuf_length)) < 0) {
333 avahi_log_warn("write(): %s", strerror(errno));
338 g_assert((guint) r <= c->outbuf_length);
339 c->outbuf_length -= r;
341 if (c->outbuf_length)
342 memmove(c->outbuf, c->outbuf + r, c->outbuf_length - r);
344 if (c->outbuf_length == 0 && c->state == CLIENT_DEAD) {
353 (c->outbuf_length > 0 ? G_IO_OUT : 0) |
354 (c->inbuf_length < sizeof(c->inbuf) ? G_IO_IN : 0);
357 static gboolean prepare_func(GSource *source, gint *timeout) {
365 static gboolean check_func(GSource *source) {
366 Server *s = (Server*) source;
371 if (s->poll_fd.revents)
374 for (c = s->clients; c; c = c->clients_next)
375 if (c->poll_fd.revents)
381 static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer user_data) {
382 Server *s = (Server*) source;
387 if (s->poll_fd.revents & G_IO_IN) {
390 if ((fd = accept(s->fd, NULL, NULL)) < 0)
391 avahi_log_warn("accept(): %s", strerror(errno));
394 } else if (s->poll_fd.revents)
395 g_error("Invalid revents");
397 for (c = s->clients; c; c = n) {
399 if (c->poll_fd.revents)
406 int simple_protocol_setup(GMainContext *c) {
407 struct sockaddr_un sa;
410 static GSourceFuncs source_funcs = {
421 server = (Server*) g_source_new(&source_funcs, sizeof(Server));
422 server->bind_successful = FALSE;
424 AVAHI_LLIST_HEAD_INIT(Client, server->clients);
425 g_main_context_ref(server->context = (c ? c : g_main_context_default()));
426 server->clients = NULL;
430 if ((server->fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
431 avahi_log_warn("socket(PF_LOCAL, SOCK_STREAM, 0): %s", strerror(errno));
435 memset(&sa, 0, sizeof(sa));
436 sa.sun_family = AF_LOCAL;
437 strncpy(sa.sun_path, AVAHI_SOCKET, sizeof(sa.sun_path)-1);
439 /* We simply remove existing UNIX sockets under this name. The
440 Avahi daemons makes sure that it runs only once on a host,
441 therefore sockets that already exist are stale and may be
442 removed without any ill effects */
444 unlink(AVAHI_SOCKET);
446 if (bind(server->fd, &sa, sizeof(sa)) < 0) {
447 avahi_log_warn("bind(): %s", strerror(errno));
451 server->bind_successful = TRUE;
453 if (listen(server->fd, 2) < 0) {
454 avahi_log_warn("listen(): %s", strerror(errno));
460 memset(&server->poll_fd, 0, sizeof(GPollFD));
461 server->poll_fd.fd = server->fd;
462 server->poll_fd.events = G_IO_IN|G_IO_ERR;
463 g_source_add_poll(&server->source, &server->poll_fd);
465 g_source_attach(&server->source, server->context);
472 simple_protocol_shutdown();
477 void simple_protocol_shutdown(void) {
481 while (server->clients)
482 client_free(server->clients);
484 if (server->bind_successful)
485 unlink(AVAHI_SOCKET);
490 g_main_context_unref(server->context);
492 g_source_destroy(&server->source);
493 g_source_unref(&server->source);
499 void simple_protocol_restart_queries(void) {
502 /* Restart queries in case of local domain name changes */
506 for (c = server->clients; c; c = c->clients_next)
507 if (c->state == CLIENT_BROWSE_DNS_SERVERS && c->dns_server_browser) {
508 avahi_dns_server_browser_free(c->dns_server_browser);
509 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);