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"
47 #define BUFFER_SIZE (20*1024)
49 #define CLIENTS_MAX 50
51 typedef struct Client Client;
52 typedef struct Server Server;
56 CLIENT_RESOLVE_HOSTNAME,
57 CLIENT_RESOLVE_ADDRESS,
58 CLIENT_BROWSE_DNS_SERVERS,
70 char inbuf[BUFFER_SIZE], outbuf[BUFFER_SIZE];
71 size_t inbuf_length, outbuf_length;
73 AvahiSHostNameResolver *host_name_resolver;
74 AvahiSAddressResolver *address_resolver;
75 AvahiSDNSServerBrowser *dns_server_browser;
77 AvahiProtocol afquery;
79 AVAHI_LLIST_FIELDS(Client, clients);
83 const AvahiPoll *poll_api;
86 AVAHI_LLIST_HEAD(Client, clients);
92 static Server *server = NULL;
94 static void client_work(AvahiWatch *watch, int fd, AvahiWatchEvent events, void *userdata);
96 static void client_free(Client *c) {
99 assert(c->server->n_clients >= 1);
100 c->server->n_clients--;
102 if (c->host_name_resolver)
103 avahi_s_host_name_resolver_free(c->host_name_resolver);
105 if (c->address_resolver)
106 avahi_s_address_resolver_free(c->address_resolver);
108 if (c->dns_server_browser)
109 avahi_s_dns_server_browser_free(c->dns_server_browser);
111 c->server->poll_api->watch_free(c->watch);
114 AVAHI_LLIST_REMOVE(Client, clients, c->server->clients, c);
118 static void client_new(Server *s, int fd) {
123 c = avahi_new(Client, 1);
126 c->state = CLIENT_IDLE;
128 c->inbuf_length = c->outbuf_length = 0;
130 c->host_name_resolver = NULL;
131 c->address_resolver = NULL;
132 c->dns_server_browser = NULL;
134 c->watch = s->poll_api->watch_new(s->poll_api, fd, AVAHI_WATCH_IN, client_work, c);
136 AVAHI_LLIST_PREPEND(Client, clients, s->clients, c);
140 static void client_output(Client *c, const uint8_t*data, size_t size) {
149 k = sizeof(c->outbuf) - c->outbuf_length;
150 m = size > k ? k : size;
152 memcpy(c->outbuf + c->outbuf_length, data, m);
153 c->outbuf_length += m;
155 server->poll_api->watch_update(c->watch, AVAHI_WATCH_OUT);
158 static void client_output_printf(Client *c, const char *format, ...) {
162 va_start(ap, format);
163 t = avahi_strdup_vprintf(format, ap);
166 client_output(c, (uint8_t*) t, strlen(t));
170 static void host_name_resolver_callback(
171 AVAHI_GCC_UNUSED AvahiSHostNameResolver *r,
173 AvahiProtocol protocol,
174 AvahiResolverEvent event,
175 const char *hostname,
176 const AvahiAddress *a,
177 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
180 Client *c = userdata;
184 if (event == AVAHI_RESOLVER_FAILURE)
185 client_output_printf(c, "%+i %s\n", avahi_server_errno(avahi_server), avahi_strerror(avahi_server_errno(avahi_server)));
186 else if (event == AVAHI_RESOLVER_FOUND) {
187 char t[AVAHI_ADDRESS_STR_MAX];
188 avahi_address_snprint(t, sizeof(t), a);
189 client_output_printf(c, "+ %i %u %s %s\n", iface, protocol, hostname, t);
192 c->state = CLIENT_DEAD;
195 static void address_resolver_callback(
196 AVAHI_GCC_UNUSED AvahiSAddressResolver *r,
198 AvahiProtocol protocol,
199 AvahiResolverEvent event,
200 AVAHI_GCC_UNUSED const AvahiAddress *a,
201 const char *hostname,
202 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
205 Client *c = userdata;
209 if (event == AVAHI_RESOLVER_FAILURE)
210 client_output_printf(c, "%+i %s\n", avahi_server_errno(avahi_server), avahi_strerror(avahi_server_errno(avahi_server)));
211 else if (event == AVAHI_RESOLVER_FOUND)
212 client_output_printf(c, "+ %i %u %s\n", iface, protocol, hostname);
214 c->state = CLIENT_DEAD;
217 static void dns_server_browser_callback(
218 AVAHI_GCC_UNUSED AvahiSDNSServerBrowser *b,
219 AvahiIfIndex interface,
220 AvahiProtocol protocol,
221 AvahiBrowserEvent event,
222 AVAHI_GCC_UNUSED const char *host_name,
223 const AvahiAddress *a,
225 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
228 Client *c = userdata;
229 char t[AVAHI_ADDRESS_STR_MAX];
237 case AVAHI_BROWSER_FAILURE:
238 client_output_printf(c, "%+i %s\n", avahi_server_errno(avahi_server), avahi_strerror(avahi_server_errno(avahi_server)));
239 c->state = CLIENT_DEAD;
242 case AVAHI_BROWSER_ALL_FOR_NOW:
243 case AVAHI_BROWSER_CACHE_EXHAUSTED:
246 case AVAHI_BROWSER_NEW:
247 case AVAHI_BROWSER_REMOVE:
249 avahi_address_snprint(t, sizeof(t), a);
250 client_output_printf(c, "%c %i %u %s %u\n", event == AVAHI_BROWSER_NEW ? '>' : '<', interface, protocol, t, port);
255 static void handle_line(Client *c, const char *s) {
256 char cmd[64], arg[64];
262 if (c->state != CLIENT_IDLE)
265 if ((n_args = sscanf(s, "%63s %63s", cmd, arg)) < 1 ) {
266 client_output_printf(c, "%+i Failed to parse command, try \"HELP\".\n", AVAHI_ERR_INVALID_OPERATION);
267 c->state = CLIENT_DEAD;
271 if (strcmp(cmd, "HELP") == 0) {
272 client_output_printf(c,
273 "+ Available commands are:\n"
274 "+ RESOLVE-HOSTNAME <hostname>\n"
275 "+ RESOLVE-HOSTNAME-IPV6 <hostname>\n"
276 "+ RESOLVE-HOSTNAME-IPV4 <hostname>\n"
277 "+ RESOLVE-ADDRESS <address>\n"
278 "+ BROWSE-DNS-SERVERS\n"
279 "+ BROWSE-DNS-SERVERS-IPV4\n"
280 "+ BROWSE-DNS-SERVERS-IPV6\n");
281 c->state = CLIENT_DEAD; }
282 else if (strcmp(cmd, "FUCK") == 0 && n_args == 1) {
283 client_output_printf(c, "+ FUCK: Go fuck yourself!\n");
284 c->state = CLIENT_DEAD;
285 } else if (strcmp(cmd, "RESOLVE-HOSTNAME-IPV4") == 0 && n_args == 2) {
286 c->state = CLIENT_RESOLVE_HOSTNAME;
287 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)))
290 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
291 } else if (strcmp(cmd, "RESOLVE-HOSTNAME-IPV6") == 0 && n_args == 2) {
292 c->state = CLIENT_RESOLVE_HOSTNAME;
293 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)))
296 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
297 } else if (strcmp(cmd, "RESOLVE-HOSTNAME") == 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_UNSPEC, 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-ADDRESS") == 0 && n_args == 2) {
306 if (!(avahi_address_parse(arg, AVAHI_PROTO_UNSPEC, &addr))) {
307 client_output_printf(c, "%+i Failed to parse address \"%s\".\n", AVAHI_ERR_INVALID_ADDRESS, arg);
308 c->state = CLIENT_DEAD;
310 c->state = CLIENT_RESOLVE_ADDRESS;
311 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)))
315 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
317 } else if (strcmp(cmd, "BROWSE-DNS-SERVERS-IPV4") == 0 && n_args == 1) {
318 c->state = CLIENT_BROWSE_DNS_SERVERS;
319 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)))
321 client_output_printf(c, "+ Browsing ...\n");
323 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
325 } else if (strcmp(cmd, "BROWSE-DNS-SERVERS-IPV6") == 0 && n_args == 1) {
326 c->state = CLIENT_BROWSE_DNS_SERVERS;
327 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)))
329 client_output_printf(c, "+ Browsing ...\n");
331 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
333 } else if (strcmp(cmd, "BROWSE-DNS-SERVERS") == 0 && n_args == 1) {
334 c->state = CLIENT_BROWSE_DNS_SERVERS;
335 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)))
337 client_output_printf(c, "+ Browsing ...\n");
339 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
342 client_output_printf(c, "%+i Invalid command \"%s\", try \"HELP\".\n", AVAHI_ERR_INVALID_OPERATION, cmd);
343 c->state = CLIENT_DEAD;
345 avahi_log_debug(__FILE__": Got invalid request '%s'.", cmd);
351 client_output_printf(c, "%+i %s\n", avahi_server_errno(avahi_server), avahi_strerror(avahi_server_errno(avahi_server)));
352 c->state = CLIENT_DEAD;
355 static void handle_input(Client *c) {
362 if (!(e = memchr(c->inbuf, '\n', c->inbuf_length)))
365 k = e - (char*) c->inbuf;
368 handle_line(c, c->inbuf);
369 c->inbuf_length -= k + 1;
370 memmove(c->inbuf, e+1, c->inbuf_length);
374 static void client_work(AvahiWatch *watch, AVAHI_GCC_UNUSED int fd, AvahiWatchEvent events, void *userdata) {
375 Client *c = userdata;
379 if ((events & AVAHI_WATCH_IN) && c->inbuf_length < sizeof(c->inbuf)) {
382 if ((r = read(c->fd, c->inbuf + c->inbuf_length, sizeof(c->inbuf) - c->inbuf_length)) <= 0) {
384 avahi_log_warn("read(): %s", strerror(errno));
389 c->inbuf_length += r;
390 assert(c->inbuf_length <= sizeof(c->inbuf));
395 if ((events & AVAHI_WATCH_OUT) && c->outbuf_length > 0) {
398 if ((r = write(c->fd, c->outbuf, c->outbuf_length)) < 0) {
399 avahi_log_warn("write(): %s", strerror(errno));
404 assert((size_t) r <= c->outbuf_length);
405 c->outbuf_length -= r;
407 if (c->outbuf_length)
408 memmove(c->outbuf, c->outbuf + r, c->outbuf_length - r);
410 if (c->outbuf_length == 0 && c->state == CLIENT_DEAD) {
416 c->server->poll_api->watch_update(
418 (c->outbuf_length > 0 ? AVAHI_WATCH_OUT : 0) |
419 (c->inbuf_length < sizeof(c->inbuf) ? AVAHI_WATCH_IN : 0));
422 static void server_work(AVAHI_GCC_UNUSED AvahiWatch *watch, int fd, AvahiWatchEvent events, void *userdata) {
423 Server *s = userdata;
427 if (events & AVAHI_WATCH_IN) {
430 if ((cfd = accept(fd, NULL, NULL)) < 0)
431 avahi_log_error("accept(): %s", strerror(errno));
437 int simple_protocol_setup(const AvahiPoll *poll_api) {
438 struct sockaddr_un sa;
443 server = avahi_new(Server, 1);
444 server->poll_api = poll_api;
445 server->bind_successful = 0;
447 server->n_clients = 0;
448 AVAHI_LLIST_HEAD_INIT(Client, server->clients);
449 server->watch = NULL;
453 if ((server->fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
454 avahi_log_warn("socket(PF_LOCAL, SOCK_STREAM, 0): %s", strerror(errno));
458 memset(&sa, 0, sizeof(sa));
459 sa.sun_family = AF_LOCAL;
460 strncpy(sa.sun_path, AVAHI_SOCKET, sizeof(sa.sun_path)-1);
462 /* We simply remove existing UNIX sockets under this name. The
463 Avahi daemons makes sure that it runs only once on a host,
464 therefore sockets that already exist are stale and may be
465 removed without any ill effects */
467 unlink(AVAHI_SOCKET);
469 if (bind(server->fd, &sa, sizeof(sa)) < 0) {
470 avahi_log_warn("bind(): %s", strerror(errno));
474 server->bind_successful = 1;
476 if (listen(server->fd, 2) < 0) {
477 avahi_log_warn("listen(): %s", strerror(errno));
483 server->watch = poll_api->watch_new(poll_api, server->fd, AVAHI_WATCH_IN, server_work, server);
490 simple_protocol_shutdown();
495 void simple_protocol_shutdown(void) {
499 if (server->bind_successful)
500 unlink(AVAHI_SOCKET);
502 while (server->clients)
503 client_free(server->clients);
506 server->poll_api->watch_free(server->watch);
517 void simple_protocol_restart_queries(void) {
520 /* Restart queries in case of local domain name changes */
524 for (c = server->clients; c; c = c->clients_next)
525 if (c->state == CLIENT_BROWSE_DNS_SERVERS && c->dns_server_browser) {
526 avahi_s_dns_server_browser_free(c->dns_server_browser);
527 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);