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>
37 #include <avahi-common/llist.h>
38 #include <avahi-common/malloc.h>
39 #include <avahi-common/error.h>
41 #include <avahi-core/log.h>
42 #include <avahi-core/lookup.h>
43 #include <avahi-core/dns-srv-rr.h>
45 #include "simple-protocol.h"
47 #include "sd-daemon.h"
54 #define AF_LOCAL AF_UNIX
57 #define PF_LOCAL PF_UNIX
60 #define BUFFER_SIZE (20*1024)
62 #define CLIENTS_MAX 50
64 typedef struct Client Client;
65 typedef struct Server Server;
69 CLIENT_RESOLVE_HOSTNAME,
70 CLIENT_RESOLVE_ADDRESS,
71 CLIENT_BROWSE_DNS_SERVERS,
83 char inbuf[BUFFER_SIZE], outbuf[BUFFER_SIZE];
84 size_t inbuf_length, outbuf_length;
86 AvahiSHostNameResolver *host_name_resolver;
87 AvahiSAddressResolver *address_resolver;
88 AvahiSDNSServerBrowser *dns_server_browser;
90 AvahiProtocol afquery;
92 AVAHI_LLIST_FIELDS(Client, clients);
96 const AvahiPoll *poll_api;
99 AVAHI_LLIST_HEAD(Client, clients);
105 static Server *server = NULL;
107 static void client_work(AvahiWatch *watch, int fd, AvahiWatchEvent events, void *userdata);
109 static void client_free(Client *c) {
112 assert(c->server->n_clients >= 1);
113 c->server->n_clients--;
115 if (c->host_name_resolver)
116 avahi_s_host_name_resolver_free(c->host_name_resolver);
118 if (c->address_resolver)
119 avahi_s_address_resolver_free(c->address_resolver);
121 if (c->dns_server_browser)
122 avahi_s_dns_server_browser_free(c->dns_server_browser);
124 c->server->poll_api->watch_free(c->watch);
127 AVAHI_LLIST_REMOVE(Client, clients, c->server->clients, c);
131 static void client_new(Server *s, int fd) {
136 c = avahi_new(Client, 1);
139 c->state = CLIENT_IDLE;
141 c->inbuf_length = c->outbuf_length = 0;
143 c->host_name_resolver = NULL;
144 c->address_resolver = NULL;
145 c->dns_server_browser = NULL;
147 c->watch = s->poll_api->watch_new(s->poll_api, fd, AVAHI_WATCH_IN, client_work, c);
149 AVAHI_LLIST_PREPEND(Client, clients, s->clients, c);
153 static void client_output(Client *c, const uint8_t*data, size_t size) {
162 k = sizeof(c->outbuf) - c->outbuf_length;
163 m = size > k ? k : size;
165 memcpy(c->outbuf + c->outbuf_length, data, m);
166 c->outbuf_length += m;
168 server->poll_api->watch_update(c->watch, AVAHI_WATCH_OUT);
171 static void client_output_printf(Client *c, const char *format, ...) {
175 va_start(ap, format);
176 t = avahi_strdup_vprintf(format, ap);
179 client_output(c, (uint8_t*) t, strlen(t));
183 static void host_name_resolver_callback(
184 AVAHI_GCC_UNUSED AvahiSHostNameResolver *r,
186 AvahiProtocol protocol,
187 AvahiResolverEvent event,
188 const char *hostname,
189 const AvahiAddress *a,
190 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
193 Client *c = userdata;
197 if (event == AVAHI_RESOLVER_FAILURE)
198 client_output_printf(c, "%+i %s\n", avahi_server_errno(avahi_server), avahi_strerror(avahi_server_errno(avahi_server)));
199 else if (event == AVAHI_RESOLVER_FOUND) {
200 char t[AVAHI_ADDRESS_STR_MAX];
201 avahi_address_snprint(t, sizeof(t), a);
202 client_output_printf(c, "+ %i %u %s %s\n", iface, protocol, hostname, t);
205 c->state = CLIENT_DEAD;
208 static void address_resolver_callback(
209 AVAHI_GCC_UNUSED AvahiSAddressResolver *r,
211 AvahiProtocol protocol,
212 AvahiResolverEvent event,
213 AVAHI_GCC_UNUSED const AvahiAddress *a,
214 const char *hostname,
215 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
218 Client *c = userdata;
222 if (event == AVAHI_RESOLVER_FAILURE)
223 client_output_printf(c, "%+i %s\n", avahi_server_errno(avahi_server), avahi_strerror(avahi_server_errno(avahi_server)));
224 else if (event == AVAHI_RESOLVER_FOUND)
225 client_output_printf(c, "+ %i %u %s\n", iface, protocol, hostname);
227 c->state = CLIENT_DEAD;
230 static void dns_server_browser_callback(
231 AVAHI_GCC_UNUSED AvahiSDNSServerBrowser *b,
232 AvahiIfIndex interface,
233 AvahiProtocol protocol,
234 AvahiBrowserEvent event,
235 AVAHI_GCC_UNUSED const char *host_name,
236 const AvahiAddress *a,
238 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
241 Client *c = userdata;
242 char t[AVAHI_ADDRESS_STR_MAX];
250 case AVAHI_BROWSER_FAILURE:
251 client_output_printf(c, "%+i %s\n", avahi_server_errno(avahi_server), avahi_strerror(avahi_server_errno(avahi_server)));
252 c->state = CLIENT_DEAD;
255 case AVAHI_BROWSER_ALL_FOR_NOW:
256 case AVAHI_BROWSER_CACHE_EXHAUSTED:
259 case AVAHI_BROWSER_NEW:
260 case AVAHI_BROWSER_REMOVE:
262 avahi_address_snprint(t, sizeof(t), a);
263 client_output_printf(c, "%c %i %u %s %u\n", event == AVAHI_BROWSER_NEW ? '>' : '<', interface, protocol, t, port);
268 static void handle_line(Client *c, const char *s) {
269 char cmd[64], arg[64];
275 if (c->state != CLIENT_IDLE)
278 if ((n_args = sscanf(s, "%63s %63s", cmd, arg)) < 1 ) {
279 client_output_printf(c, "%+i Failed to parse command, try \"HELP\".\n", AVAHI_ERR_INVALID_OPERATION);
280 c->state = CLIENT_DEAD;
284 if (strcmp(cmd, "HELP") == 0) {
285 client_output_printf(c,
286 "+ Available commands are:\n"
287 "+ RESOLVE-HOSTNAME <hostname>\n"
288 "+ RESOLVE-HOSTNAME-IPV6 <hostname>\n"
289 "+ RESOLVE-HOSTNAME-IPV4 <hostname>\n"
290 "+ RESOLVE-ADDRESS <address>\n"
291 "+ BROWSE-DNS-SERVERS\n"
292 "+ BROWSE-DNS-SERVERS-IPV4\n"
293 "+ BROWSE-DNS-SERVERS-IPV6\n");
294 c->state = CLIENT_DEAD; }
295 else if (strcmp(cmd, "FUCK") == 0 && n_args == 1) {
296 client_output_printf(c, "+ FUCK: Go fuck yourself!\n");
297 c->state = CLIENT_DEAD;
298 } else if (strcmp(cmd, "RESOLVE-HOSTNAME-IPV4") == 0 && n_args == 2) {
299 c->state = CLIENT_RESOLVE_HOSTNAME;
300 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)))
303 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
304 } else if (strcmp(cmd, "RESOLVE-HOSTNAME-IPV6") == 0 && n_args == 2) {
305 c->state = CLIENT_RESOLVE_HOSTNAME;
306 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)))
309 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
310 } else if (strcmp(cmd, "RESOLVE-HOSTNAME") == 0 && n_args == 2) {
311 c->state = CLIENT_RESOLVE_HOSTNAME;
312 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)))
315 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
316 } else if (strcmp(cmd, "RESOLVE-ADDRESS") == 0 && n_args == 2) {
319 if (!(avahi_address_parse(arg, AVAHI_PROTO_UNSPEC, &addr))) {
320 client_output_printf(c, "%+i Failed to parse address \"%s\".\n", AVAHI_ERR_INVALID_ADDRESS, arg);
321 c->state = CLIENT_DEAD;
323 c->state = CLIENT_RESOLVE_ADDRESS;
324 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)))
328 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
330 } else if (strcmp(cmd, "BROWSE-DNS-SERVERS-IPV4") == 0 && n_args == 1) {
331 c->state = CLIENT_BROWSE_DNS_SERVERS;
332 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)))
334 client_output_printf(c, "+ Browsing ...\n");
336 avahi_log_debug(__FILE__": Got %s request.", cmd);
338 } else if (strcmp(cmd, "BROWSE-DNS-SERVERS-IPV6") == 0 && n_args == 1) {
339 c->state = CLIENT_BROWSE_DNS_SERVERS;
340 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)))
342 client_output_printf(c, "+ Browsing ...\n");
344 avahi_log_debug(__FILE__": Got %s request.", cmd);
346 } else if (strcmp(cmd, "BROWSE-DNS-SERVERS") == 0 && n_args == 1) {
347 c->state = CLIENT_BROWSE_DNS_SERVERS;
348 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)))
350 client_output_printf(c, "+ Browsing ...\n");
352 avahi_log_debug(__FILE__": Got %s request.", cmd);
355 client_output_printf(c, "%+i Invalid command \"%s\", try \"HELP\".\n", AVAHI_ERR_INVALID_OPERATION, cmd);
356 c->state = CLIENT_DEAD;
358 avahi_log_debug(__FILE__": Got invalid request '%s'.", cmd);
364 client_output_printf(c, "%+i %s\n", avahi_server_errno(avahi_server), avahi_strerror(avahi_server_errno(avahi_server)));
365 c->state = CLIENT_DEAD;
368 static void handle_input(Client *c) {
375 if (!(e = memchr(c->inbuf, '\n', c->inbuf_length)))
378 k = e - (char*) c->inbuf;
381 handle_line(c, c->inbuf);
382 c->inbuf_length -= k + 1;
383 memmove(c->inbuf, e+1, c->inbuf_length);
387 static void client_work(AvahiWatch *watch, AVAHI_GCC_UNUSED int fd, AvahiWatchEvent events, void *userdata) {
388 Client *c = userdata;
392 if ((events & AVAHI_WATCH_IN) && c->inbuf_length < sizeof(c->inbuf)) {
395 if ((r = read(c->fd, c->inbuf + c->inbuf_length, sizeof(c->inbuf) - c->inbuf_length)) <= 0) {
397 avahi_log_warn("read(): %s", strerror(errno));
402 c->inbuf_length += r;
403 assert(c->inbuf_length <= sizeof(c->inbuf));
408 if ((events & AVAHI_WATCH_OUT) && c->outbuf_length > 0) {
411 if ((r = write(c->fd, c->outbuf, c->outbuf_length)) < 0) {
412 avahi_log_warn("write(): %s", strerror(errno));
417 assert((size_t) r <= c->outbuf_length);
418 c->outbuf_length -= r;
420 if (c->outbuf_length)
421 memmove(c->outbuf, c->outbuf + r, c->outbuf_length - r);
423 if (c->outbuf_length == 0 && c->state == CLIENT_DEAD) {
429 c->server->poll_api->watch_update(
431 (c->outbuf_length > 0 ? AVAHI_WATCH_OUT : 0) |
432 (c->inbuf_length < sizeof(c->inbuf) ? AVAHI_WATCH_IN : 0));
435 static void server_work(AVAHI_GCC_UNUSED AvahiWatch *watch, int fd, AvahiWatchEvent events, void *userdata) {
436 Server *s = userdata;
440 if (events & AVAHI_WATCH_IN) {
443 if ((cfd = accept(fd, NULL, NULL)) < 0)
444 avahi_log_error("accept(): %s", strerror(errno));
450 int simple_protocol_setup(const AvahiPoll *poll_api) {
451 struct sockaddr_un sa;
457 server = avahi_new(Server, 1);
458 server->poll_api = poll_api;
459 server->remove_socket = 0;
461 server->n_clients = 0;
462 AVAHI_LLIST_HEAD_INIT(Client, server->clients);
463 server->watch = NULL;
467 if ((n = sd_listen_fds(1)) < 0) {
468 avahi_log_warn("Failed to acquire systemd file descriptors: %s", strerror(-n));
473 avahi_log_warn("Too many systemd file descriptors passed.");
480 if ((r = sd_is_socket(AF_LOCAL, SOCK_STREAM, 1, 0)) < 0) {
481 avahi_log_warn("Passed systemd file descriptor is of wrong type: %s", strerror(-r));
485 server->fd = SD_LISTEN_FDS_START;
489 if ((server->fd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) {
490 avahi_log_warn("socket(AF_LOCAL, SOCK_STREAM, 0): %s", strerror(errno));
494 memset(&sa, 0, sizeof(sa));
495 sa.sun_family = AF_LOCAL;
496 strncpy(sa.sun_path, AVAHI_SOCKET, sizeof(sa.sun_path)-1);
498 /* We simply remove existing UNIX sockets under this name. The
499 Avahi daemon makes sure that it runs only once on a host,
500 therefore sockets that already exist are stale and may be
501 removed without any ill effects */
503 unlink(AVAHI_SOCKET);
505 if (bind(server->fd, (struct sockaddr*) &sa, sizeof(sa)) < 0) {
506 avahi_log_warn("bind(): %s", strerror(errno));
510 server->remove_socket = 1;
512 if (listen(server->fd, SOMAXCONN) < 0) {
513 avahi_log_warn("listen(): %s", strerror(errno));
520 server->watch = poll_api->watch_new(poll_api, server->fd, AVAHI_WATCH_IN, server_work, server);
527 simple_protocol_shutdown();
532 void simple_protocol_shutdown(void) {
536 if (server->remove_socket)
538 avahi_chroot_helper_unlink(AVAHI_SOCKET);
540 unlink(AVAHI_SOCKET);
543 while (server->clients)
544 client_free(server->clients);
547 server->poll_api->watch_free(server->watch);
558 void simple_protocol_restart_queries(void) {
561 /* Restart queries in case of local domain name changes */
565 for (c = server->clients; c; c = c->clients_next)
566 if (c->state == CLIENT_BROWSE_DNS_SERVERS && c->dns_server_browser) {
567 avahi_s_dns_server_browser_free(c->dns_server_browser);
568 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);