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"
53 #define AF_LOCAL AF_UNIX
56 #define PF_LOCAL PF_UNIX
59 #define BUFFER_SIZE (20*1024)
61 #define CLIENTS_MAX 50
63 typedef struct Client Client;
64 typedef struct Server Server;
68 CLIENT_RESOLVE_HOSTNAME,
69 CLIENT_RESOLVE_ADDRESS,
70 CLIENT_BROWSE_DNS_SERVERS,
82 char inbuf[BUFFER_SIZE], outbuf[BUFFER_SIZE];
83 size_t inbuf_length, outbuf_length;
85 AvahiSHostNameResolver *host_name_resolver;
86 AvahiSAddressResolver *address_resolver;
87 AvahiSDNSServerBrowser *dns_server_browser;
89 AvahiProtocol afquery;
91 AVAHI_LLIST_FIELDS(Client, clients);
95 const AvahiPoll *poll_api;
98 AVAHI_LLIST_HEAD(Client, clients);
104 static Server *server = NULL;
106 static void client_work(AvahiWatch *watch, int fd, AvahiWatchEvent events, void *userdata);
108 static void client_free(Client *c) {
111 assert(c->server->n_clients >= 1);
112 c->server->n_clients--;
114 if (c->host_name_resolver)
115 avahi_s_host_name_resolver_free(c->host_name_resolver);
117 if (c->address_resolver)
118 avahi_s_address_resolver_free(c->address_resolver);
120 if (c->dns_server_browser)
121 avahi_s_dns_server_browser_free(c->dns_server_browser);
123 c->server->poll_api->watch_free(c->watch);
126 AVAHI_LLIST_REMOVE(Client, clients, c->server->clients, c);
130 static void client_new(Server *s, int fd) {
135 c = avahi_new(Client, 1);
138 c->state = CLIENT_IDLE;
140 c->inbuf_length = c->outbuf_length = 0;
142 c->host_name_resolver = NULL;
143 c->address_resolver = NULL;
144 c->dns_server_browser = NULL;
146 c->watch = s->poll_api->watch_new(s->poll_api, fd, AVAHI_WATCH_IN, client_work, c);
148 AVAHI_LLIST_PREPEND(Client, clients, s->clients, c);
152 static void client_output(Client *c, const uint8_t*data, size_t size) {
161 k = sizeof(c->outbuf) - c->outbuf_length;
162 m = size > k ? k : size;
164 memcpy(c->outbuf + c->outbuf_length, data, m);
165 c->outbuf_length += m;
167 server->poll_api->watch_update(c->watch, AVAHI_WATCH_OUT);
170 static void client_output_printf(Client *c, const char *format, ...) {
174 va_start(ap, format);
175 t = avahi_strdup_vprintf(format, ap);
178 client_output(c, (uint8_t*) t, strlen(t));
182 static void host_name_resolver_callback(
183 AVAHI_GCC_UNUSED AvahiSHostNameResolver *r,
185 AvahiProtocol protocol,
186 AvahiResolverEvent event,
187 const char *hostname,
188 const AvahiAddress *a,
189 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
192 Client *c = userdata;
196 if (event == AVAHI_RESOLVER_FAILURE)
197 client_output_printf(c, "%+i %s\n", avahi_server_errno(avahi_server), avahi_strerror(avahi_server_errno(avahi_server)));
198 else if (event == AVAHI_RESOLVER_FOUND) {
199 char t[AVAHI_ADDRESS_STR_MAX];
200 avahi_address_snprint(t, sizeof(t), a);
201 client_output_printf(c, "+ %i %u %s %s\n", iface, protocol, hostname, t);
204 c->state = CLIENT_DEAD;
207 static void address_resolver_callback(
208 AVAHI_GCC_UNUSED AvahiSAddressResolver *r,
210 AvahiProtocol protocol,
211 AvahiResolverEvent event,
212 AVAHI_GCC_UNUSED const AvahiAddress *a,
213 const char *hostname,
214 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
217 Client *c = userdata;
221 if (event == AVAHI_RESOLVER_FAILURE)
222 client_output_printf(c, "%+i %s\n", avahi_server_errno(avahi_server), avahi_strerror(avahi_server_errno(avahi_server)));
223 else if (event == AVAHI_RESOLVER_FOUND)
224 client_output_printf(c, "+ %i %u %s\n", iface, protocol, hostname);
226 c->state = CLIENT_DEAD;
229 static void dns_server_browser_callback(
230 AVAHI_GCC_UNUSED AvahiSDNSServerBrowser *b,
231 AvahiIfIndex interface,
232 AvahiProtocol protocol,
233 AvahiBrowserEvent event,
234 AVAHI_GCC_UNUSED const char *host_name,
235 const AvahiAddress *a,
237 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
240 Client *c = userdata;
241 char t[AVAHI_ADDRESS_STR_MAX];
249 case AVAHI_BROWSER_FAILURE:
250 client_output_printf(c, "%+i %s\n", avahi_server_errno(avahi_server), avahi_strerror(avahi_server_errno(avahi_server)));
251 c->state = CLIENT_DEAD;
254 case AVAHI_BROWSER_ALL_FOR_NOW:
255 case AVAHI_BROWSER_CACHE_EXHAUSTED:
258 case AVAHI_BROWSER_NEW:
259 case AVAHI_BROWSER_REMOVE:
261 avahi_address_snprint(t, sizeof(t), a);
262 client_output_printf(c, "%c %i %u %s %u\n", event == AVAHI_BROWSER_NEW ? '>' : '<', interface, protocol, t, port);
267 static void handle_line(Client *c, const char *s) {
268 char cmd[64], arg[64];
274 if (c->state != CLIENT_IDLE)
277 if ((n_args = sscanf(s, "%63s %63s", cmd, arg)) < 1 ) {
278 client_output_printf(c, "%+i Failed to parse command, try \"HELP\".\n", AVAHI_ERR_INVALID_OPERATION);
279 c->state = CLIENT_DEAD;
283 if (strcmp(cmd, "HELP") == 0) {
284 client_output_printf(c,
285 "+ Available commands are:\n"
286 "+ RESOLVE-HOSTNAME <hostname>\n"
287 "+ RESOLVE-HOSTNAME-IPV6 <hostname>\n"
288 "+ RESOLVE-HOSTNAME-IPV4 <hostname>\n"
289 "+ RESOLVE-ADDRESS <address>\n"
290 "+ BROWSE-DNS-SERVERS\n"
291 "+ BROWSE-DNS-SERVERS-IPV4\n"
292 "+ BROWSE-DNS-SERVERS-IPV6\n");
293 c->state = CLIENT_DEAD; }
294 else if (strcmp(cmd, "FUCK") == 0 && n_args == 1) {
295 client_output_printf(c, "+ FUCK: Go fuck yourself!\n");
296 c->state = CLIENT_DEAD;
297 } else if (strcmp(cmd, "RESOLVE-HOSTNAME-IPV4") == 0 && n_args == 2) {
298 c->state = CLIENT_RESOLVE_HOSTNAME;
299 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)))
302 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
303 } else if (strcmp(cmd, "RESOLVE-HOSTNAME-IPV6") == 0 && n_args == 2) {
304 c->state = CLIENT_RESOLVE_HOSTNAME;
305 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)))
308 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
309 } else if (strcmp(cmd, "RESOLVE-HOSTNAME") == 0 && n_args == 2) {
310 c->state = CLIENT_RESOLVE_HOSTNAME;
311 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)))
314 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
315 } else if (strcmp(cmd, "RESOLVE-ADDRESS") == 0 && n_args == 2) {
318 if (!(avahi_address_parse(arg, AVAHI_PROTO_UNSPEC, &addr))) {
319 client_output_printf(c, "%+i Failed to parse address \"%s\".\n", AVAHI_ERR_INVALID_ADDRESS, arg);
320 c->state = CLIENT_DEAD;
322 c->state = CLIENT_RESOLVE_ADDRESS;
323 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)))
327 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
329 } else if (strcmp(cmd, "BROWSE-DNS-SERVERS-IPV4") == 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_INET, AVAHI_LOOKUP_USE_MULTICAST, dns_server_browser_callback, c)))
333 client_output_printf(c, "+ Browsing ...\n");
335 avahi_log_debug(__FILE__": Got %s request.", cmd);
337 } else if (strcmp(cmd, "BROWSE-DNS-SERVERS-IPV6") == 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_INET6, AVAHI_LOOKUP_USE_MULTICAST, dns_server_browser_callback, c)))
341 client_output_printf(c, "+ Browsing ...\n");
343 avahi_log_debug(__FILE__": Got %s request.", cmd);
345 } else if (strcmp(cmd, "BROWSE-DNS-SERVERS") == 0 && n_args == 1) {
346 c->state = CLIENT_BROWSE_DNS_SERVERS;
347 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)))
349 client_output_printf(c, "+ Browsing ...\n");
351 avahi_log_debug(__FILE__": Got %s request.", cmd);
354 client_output_printf(c, "%+i Invalid command \"%s\", try \"HELP\".\n", AVAHI_ERR_INVALID_OPERATION, cmd);
355 c->state = CLIENT_DEAD;
357 avahi_log_debug(__FILE__": Got invalid request '%s'.", cmd);
363 client_output_printf(c, "%+i %s\n", avahi_server_errno(avahi_server), avahi_strerror(avahi_server_errno(avahi_server)));
364 c->state = CLIENT_DEAD;
367 static void handle_input(Client *c) {
374 if (!(e = memchr(c->inbuf, '\n', c->inbuf_length)))
377 k = e - (char*) c->inbuf;
380 handle_line(c, c->inbuf);
381 c->inbuf_length -= k + 1;
382 memmove(c->inbuf, e+1, c->inbuf_length);
386 static void client_work(AvahiWatch *watch, AVAHI_GCC_UNUSED int fd, AvahiWatchEvent events, void *userdata) {
387 Client *c = userdata;
391 if ((events & AVAHI_WATCH_IN) && c->inbuf_length < sizeof(c->inbuf)) {
394 if ((r = read(c->fd, c->inbuf + c->inbuf_length, sizeof(c->inbuf) - c->inbuf_length)) <= 0) {
396 avahi_log_warn("read(): %s", strerror(errno));
401 c->inbuf_length += r;
402 assert(c->inbuf_length <= sizeof(c->inbuf));
407 if ((events & AVAHI_WATCH_OUT) && c->outbuf_length > 0) {
410 if ((r = write(c->fd, c->outbuf, c->outbuf_length)) < 0) {
411 avahi_log_warn("write(): %s", strerror(errno));
416 assert((size_t) r <= c->outbuf_length);
417 c->outbuf_length -= r;
419 if (c->outbuf_length)
420 memmove(c->outbuf, c->outbuf + r, c->outbuf_length - r);
422 if (c->outbuf_length == 0 && c->state == CLIENT_DEAD) {
428 c->server->poll_api->watch_update(
430 (c->outbuf_length > 0 ? AVAHI_WATCH_OUT : 0) |
431 (c->inbuf_length < sizeof(c->inbuf) ? AVAHI_WATCH_IN : 0));
434 static void server_work(AVAHI_GCC_UNUSED AvahiWatch *watch, int fd, AvahiWatchEvent events, void *userdata) {
435 Server *s = userdata;
439 if (events & AVAHI_WATCH_IN) {
442 if ((cfd = accept(fd, NULL, NULL)) < 0)
443 avahi_log_error("accept(): %s", strerror(errno));
449 int simple_protocol_setup(const AvahiPoll *poll_api) {
450 struct sockaddr_un sa;
455 server = avahi_new(Server, 1);
456 server->poll_api = poll_api;
457 server->bind_successful = 0;
459 server->n_clients = 0;
460 AVAHI_LLIST_HEAD_INIT(Client, server->clients);
461 server->watch = NULL;
465 if ((server->fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
466 avahi_log_warn("socket(PF_LOCAL, SOCK_STREAM, 0): %s", strerror(errno));
470 memset(&sa, 0, sizeof(sa));
471 sa.sun_family = AF_LOCAL;
472 strncpy(sa.sun_path, AVAHI_SOCKET, sizeof(sa.sun_path)-1);
474 /* We simply remove existing UNIX sockets under this name. The
475 Avahi daemons makes sure that it runs only once on a host,
476 therefore sockets that already exist are stale and may be
477 removed without any ill effects */
479 unlink(AVAHI_SOCKET);
481 if (bind(server->fd, (struct sockaddr*) &sa, sizeof(sa)) < 0) {
482 avahi_log_warn("bind(): %s", strerror(errno));
486 server->bind_successful = 1;
488 if (listen(server->fd, 2) < 0) {
489 avahi_log_warn("listen(): %s", strerror(errno));
495 server->watch = poll_api->watch_new(poll_api, server->fd, AVAHI_WATCH_IN, server_work, server);
502 simple_protocol_shutdown();
507 void simple_protocol_shutdown(void) {
511 if (server->bind_successful)
513 avahi_chroot_helper_unlink(AVAHI_SOCKET);
515 unlink(AVAHI_SOCKET);
518 while (server->clients)
519 client_free(server->clients);
522 server->poll_api->watch_free(server->watch);
533 void simple_protocol_restart_queries(void) {
536 /* Restart queries in case of local domain name changes */
540 for (c = server->clients; c; c = c->clients_next)
541 if (c->state == CLIENT_BROWSE_DNS_SERVERS && c->dns_server_browser) {
542 avahi_s_dns_server_browser_free(c->dns_server_browser);
543 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);