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
28 #include <sys/types.h>
29 #include <sys/socket.h>
36 #include <avahi-common/llist.h>
37 #include <avahi-common/malloc.h>
38 #include <avahi-common/error.h>
40 #include <avahi-core/log.h>
41 #include <avahi-core/lookup.h>
42 #include <avahi-core/dns-srv-rr.h>
44 #include "simple-protocol.h"
51 #define BUFFER_SIZE (20*1024)
53 #define CLIENTS_MAX 50
55 typedef struct Client Client;
56 typedef struct Server Server;
60 CLIENT_RESOLVE_HOSTNAME,
61 CLIENT_RESOLVE_ADDRESS,
62 CLIENT_BROWSE_DNS_SERVERS,
74 char inbuf[BUFFER_SIZE], outbuf[BUFFER_SIZE];
75 size_t inbuf_length, outbuf_length;
77 AvahiSHostNameResolver *host_name_resolver;
78 AvahiSAddressResolver *address_resolver;
79 AvahiSDNSServerBrowser *dns_server_browser;
81 AvahiProtocol afquery;
83 AVAHI_LLIST_FIELDS(Client, clients);
87 const AvahiPoll *poll_api;
90 AVAHI_LLIST_HEAD(Client, clients);
96 static Server *server = NULL;
98 static void client_work(AvahiWatch *watch, int fd, AvahiWatchEvent events, void *userdata);
100 static void client_free(Client *c) {
103 assert(c->server->n_clients >= 1);
104 c->server->n_clients--;
106 if (c->host_name_resolver)
107 avahi_s_host_name_resolver_free(c->host_name_resolver);
109 if (c->address_resolver)
110 avahi_s_address_resolver_free(c->address_resolver);
112 if (c->dns_server_browser)
113 avahi_s_dns_server_browser_free(c->dns_server_browser);
115 c->server->poll_api->watch_free(c->watch);
118 AVAHI_LLIST_REMOVE(Client, clients, c->server->clients, c);
122 static void client_new(Server *s, int fd) {
127 c = avahi_new(Client, 1);
130 c->state = CLIENT_IDLE;
132 c->inbuf_length = c->outbuf_length = 0;
134 c->host_name_resolver = NULL;
135 c->address_resolver = NULL;
136 c->dns_server_browser = NULL;
138 c->watch = s->poll_api->watch_new(s->poll_api, fd, AVAHI_WATCH_IN, client_work, c);
140 AVAHI_LLIST_PREPEND(Client, clients, s->clients, c);
144 static void client_output(Client *c, const uint8_t*data, size_t size) {
153 k = sizeof(c->outbuf) - c->outbuf_length;
154 m = size > k ? k : size;
156 memcpy(c->outbuf + c->outbuf_length, data, m);
157 c->outbuf_length += m;
159 server->poll_api->watch_update(c->watch, AVAHI_WATCH_OUT);
162 static void client_output_printf(Client *c, const char *format, ...) {
166 va_start(ap, format);
167 t = avahi_strdup_vprintf(format, ap);
170 client_output(c, (uint8_t*) t, strlen(t));
174 static void host_name_resolver_callback(
175 AVAHI_GCC_UNUSED AvahiSHostNameResolver *r,
177 AvahiProtocol protocol,
178 AvahiResolverEvent event,
179 const char *hostname,
180 const AvahiAddress *a,
181 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
184 Client *c = userdata;
188 if (event == AVAHI_RESOLVER_FAILURE)
189 client_output_printf(c, "%+i %s\n", avahi_server_errno(avahi_server), avahi_strerror(avahi_server_errno(avahi_server)));
190 else if (event == AVAHI_RESOLVER_FOUND) {
191 char t[AVAHI_ADDRESS_STR_MAX];
192 avahi_address_snprint(t, sizeof(t), a);
193 client_output_printf(c, "+ %i %u %s %s\n", iface, protocol, hostname, t);
196 c->state = CLIENT_DEAD;
199 static void address_resolver_callback(
200 AVAHI_GCC_UNUSED AvahiSAddressResolver *r,
202 AvahiProtocol protocol,
203 AvahiResolverEvent event,
204 AVAHI_GCC_UNUSED const AvahiAddress *a,
205 const char *hostname,
206 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
209 Client *c = userdata;
213 if (event == AVAHI_RESOLVER_FAILURE)
214 client_output_printf(c, "%+i %s\n", avahi_server_errno(avahi_server), avahi_strerror(avahi_server_errno(avahi_server)));
215 else if (event == AVAHI_RESOLVER_FOUND)
216 client_output_printf(c, "+ %i %u %s\n", iface, protocol, hostname);
218 c->state = CLIENT_DEAD;
221 static void dns_server_browser_callback(
222 AVAHI_GCC_UNUSED AvahiSDNSServerBrowser *b,
223 AvahiIfIndex interface,
224 AvahiProtocol protocol,
225 AvahiBrowserEvent event,
226 AVAHI_GCC_UNUSED const char *host_name,
227 const AvahiAddress *a,
229 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
232 Client *c = userdata;
233 char t[AVAHI_ADDRESS_STR_MAX];
241 case AVAHI_BROWSER_FAILURE:
242 client_output_printf(c, "%+i %s\n", avahi_server_errno(avahi_server), avahi_strerror(avahi_server_errno(avahi_server)));
243 c->state = CLIENT_DEAD;
246 case AVAHI_BROWSER_ALL_FOR_NOW:
247 case AVAHI_BROWSER_CACHE_EXHAUSTED:
250 case AVAHI_BROWSER_NEW:
251 case AVAHI_BROWSER_REMOVE:
253 avahi_address_snprint(t, sizeof(t), a);
254 client_output_printf(c, "%c %i %u %s %u\n", event == AVAHI_BROWSER_NEW ? '>' : '<', interface, protocol, t, port);
259 static void handle_line(Client *c, const char *s) {
260 char cmd[64], arg[64];
266 if (c->state != CLIENT_IDLE)
269 if ((n_args = sscanf(s, "%63s %63s", cmd, arg)) < 1 ) {
270 client_output_printf(c, "%+i Failed to parse command, try \"HELP\".\n", AVAHI_ERR_INVALID_OPERATION);
271 c->state = CLIENT_DEAD;
275 if (strcmp(cmd, "HELP") == 0) {
276 client_output_printf(c,
277 "+ Available commands are:\n"
278 "+ RESOLVE-HOSTNAME <hostname>\n"
279 "+ RESOLVE-HOSTNAME-IPV6 <hostname>\n"
280 "+ RESOLVE-HOSTNAME-IPV4 <hostname>\n"
281 "+ RESOLVE-ADDRESS <address>\n"
282 "+ BROWSE-DNS-SERVERS\n"
283 "+ BROWSE-DNS-SERVERS-IPV4\n"
284 "+ BROWSE-DNS-SERVERS-IPV6\n");
285 c->state = CLIENT_DEAD; }
286 else if (strcmp(cmd, "FUCK") == 0 && n_args == 1) {
287 client_output_printf(c, "+ FUCK: Go fuck yourself!\n");
288 c->state = CLIENT_DEAD;
289 } else if (strcmp(cmd, "RESOLVE-HOSTNAME-IPV4") == 0 && n_args == 2) {
290 c->state = CLIENT_RESOLVE_HOSTNAME;
291 if (!(c->host_name_resolver = avahi_s_host_name_resolver_new(avahi_server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, arg, c->afquery = AVAHI_PROTO_INET, AVAHI_LOOKUP_USE_MULTICAST, host_name_resolver_callback, c)))
294 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
295 } else if (strcmp(cmd, "RESOLVE-HOSTNAME-IPV6") == 0 && n_args == 2) {
296 c->state = CLIENT_RESOLVE_HOSTNAME;
297 if (!(c->host_name_resolver = avahi_s_host_name_resolver_new(avahi_server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, arg, c->afquery = AVAHI_PROTO_INET6, AVAHI_LOOKUP_USE_MULTICAST, host_name_resolver_callback, c)))
300 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
301 } else if (strcmp(cmd, "RESOLVE-HOSTNAME") == 0 && n_args == 2) {
302 c->state = CLIENT_RESOLVE_HOSTNAME;
303 if (!(c->host_name_resolver = avahi_s_host_name_resolver_new(avahi_server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, arg, c->afquery = AVAHI_PROTO_UNSPEC, AVAHI_LOOKUP_USE_MULTICAST, host_name_resolver_callback, c)))
306 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
307 } else if (strcmp(cmd, "RESOLVE-ADDRESS") == 0 && n_args == 2) {
310 if (!(avahi_address_parse(arg, AVAHI_PROTO_UNSPEC, &addr))) {
311 client_output_printf(c, "%+i Failed to parse address \"%s\".\n", AVAHI_ERR_INVALID_ADDRESS, arg);
312 c->state = CLIENT_DEAD;
314 c->state = CLIENT_RESOLVE_ADDRESS;
315 if (!(c->address_resolver = avahi_s_address_resolver_new(avahi_server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, &addr, AVAHI_LOOKUP_USE_MULTICAST, address_resolver_callback, c)))
319 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
321 } else if (strcmp(cmd, "BROWSE-DNS-SERVERS-IPV4") == 0 && n_args == 1) {
322 c->state = CLIENT_BROWSE_DNS_SERVERS;
323 if (!(c->dns_server_browser = avahi_s_dns_server_browser_new(avahi_server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, c->afquery = AVAHI_PROTO_INET, AVAHI_LOOKUP_USE_MULTICAST, dns_server_browser_callback, c)))
325 client_output_printf(c, "+ Browsing ...\n");
327 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
329 } else if (strcmp(cmd, "BROWSE-DNS-SERVERS-IPV6") == 0 && n_args == 1) {
330 c->state = CLIENT_BROWSE_DNS_SERVERS;
331 if (!(c->dns_server_browser = avahi_s_dns_server_browser_new(avahi_server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, c->afquery = AVAHI_PROTO_INET6, AVAHI_LOOKUP_USE_MULTICAST, dns_server_browser_callback, c)))
333 client_output_printf(c, "+ Browsing ...\n");
335 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
337 } else if (strcmp(cmd, "BROWSE-DNS-SERVERS") == 0 && n_args == 1) {
338 c->state = CLIENT_BROWSE_DNS_SERVERS;
339 if (!(c->dns_server_browser = avahi_s_dns_server_browser_new(avahi_server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, c->afquery = AVAHI_PROTO_UNSPEC, AVAHI_LOOKUP_USE_MULTICAST, dns_server_browser_callback, c)))
341 client_output_printf(c, "+ Browsing ...\n");
343 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
346 client_output_printf(c, "%+i Invalid command \"%s\", try \"HELP\".\n", AVAHI_ERR_INVALID_OPERATION, cmd);
347 c->state = CLIENT_DEAD;
349 avahi_log_debug(__FILE__": Got invalid request '%s'.", cmd);
355 client_output_printf(c, "%+i %s\n", avahi_server_errno(avahi_server), avahi_strerror(avahi_server_errno(avahi_server)));
356 c->state = CLIENT_DEAD;
359 static void handle_input(Client *c) {
366 if (!(e = memchr(c->inbuf, '\n', c->inbuf_length)))
369 k = e - (char*) c->inbuf;
372 handle_line(c, c->inbuf);
373 c->inbuf_length -= k + 1;
374 memmove(c->inbuf, e+1, c->inbuf_length);
378 static void client_work(AvahiWatch *watch, AVAHI_GCC_UNUSED int fd, AvahiWatchEvent events, void *userdata) {
379 Client *c = userdata;
383 if ((events & AVAHI_WATCH_IN) && c->inbuf_length < sizeof(c->inbuf)) {
386 if ((r = read(c->fd, c->inbuf + c->inbuf_length, sizeof(c->inbuf) - c->inbuf_length)) <= 0) {
388 avahi_log_warn("read(): %s", strerror(errno));
393 c->inbuf_length += r;
394 assert(c->inbuf_length <= sizeof(c->inbuf));
399 if ((events & AVAHI_WATCH_OUT) && c->outbuf_length > 0) {
402 if ((r = write(c->fd, c->outbuf, c->outbuf_length)) < 0) {
403 avahi_log_warn("write(): %s", strerror(errno));
408 assert((size_t) r <= c->outbuf_length);
409 c->outbuf_length -= r;
411 if (c->outbuf_length)
412 memmove(c->outbuf, c->outbuf + r, c->outbuf_length - r);
414 if (c->outbuf_length == 0 && c->state == CLIENT_DEAD) {
420 c->server->poll_api->watch_update(
422 (c->outbuf_length > 0 ? AVAHI_WATCH_OUT : 0) |
423 (c->inbuf_length < sizeof(c->inbuf) ? AVAHI_WATCH_IN : 0));
426 static void server_work(AVAHI_GCC_UNUSED AvahiWatch *watch, int fd, AvahiWatchEvent events, void *userdata) {
427 Server *s = userdata;
431 if (events & AVAHI_WATCH_IN) {
434 if ((cfd = accept(fd, NULL, NULL)) < 0)
435 avahi_log_error("accept(): %s", strerror(errno));
441 int simple_protocol_setup(const AvahiPoll *poll_api) {
442 struct sockaddr_un sa;
447 server = avahi_new(Server, 1);
448 server->poll_api = poll_api;
449 server->bind_successful = 0;
451 server->n_clients = 0;
452 AVAHI_LLIST_HEAD_INIT(Client, server->clients);
453 server->watch = NULL;
457 if ((server->fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
458 avahi_log_warn("socket(PF_LOCAL, SOCK_STREAM, 0): %s", strerror(errno));
462 memset(&sa, 0, sizeof(sa));
463 sa.sun_family = AF_LOCAL;
464 strncpy(sa.sun_path, AVAHI_SOCKET, sizeof(sa.sun_path)-1);
466 /* We simply remove existing UNIX sockets under this name. The
467 Avahi daemons makes sure that it runs only once on a host,
468 therefore sockets that already exist are stale and may be
469 removed without any ill effects */
471 unlink(AVAHI_SOCKET);
473 if (bind(server->fd, &sa, sizeof(sa)) < 0) {
474 avahi_log_warn("bind(): %s", strerror(errno));
478 server->bind_successful = 1;
480 if (listen(server->fd, 2) < 0) {
481 avahi_log_warn("listen(): %s", strerror(errno));
487 server->watch = poll_api->watch_new(poll_api, server->fd, AVAHI_WATCH_IN, server_work, server);
494 simple_protocol_shutdown();
499 void simple_protocol_shutdown(void) {
503 if (server->bind_successful)
505 avahi_chroot_helper_unlink(AVAHI_SOCKET);
507 unlink(AVAHI_SOCKET);
510 while (server->clients)
511 client_free(server->clients);
514 server->poll_api->watch_free(server->watch);
525 void simple_protocol_restart_queries(void) {
528 /* Restart queries in case of local domain name changes */
532 for (c = server->clients; c; c = c->clients_next)
533 if (c->state == CLIENT_BROWSE_DNS_SERVERS && c->dns_server_browser) {
534 avahi_s_dns_server_browser_free(c->dns_server_browser);
535 c->dns_server_browser = avahi_s_dns_server_browser_new(avahi_server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, c->afquery, AVAHI_LOOKUP_USE_MULTICAST, dns_server_browser_callback, c);