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/socket.h>
35 #include <avahi-common/llist.h>
36 #include <avahi-common/malloc.h>
37 #include <avahi-common/error.h>
38 #include <avahi-core/log.h>
39 #include <avahi-core/lookup.h>
41 #include "simple-protocol.h"
44 #define BUFFER_SIZE (20*1024)
46 #define CLIENTS_MAX 50
48 typedef struct Client Client;
49 typedef struct Server Server;
53 CLIENT_RESOLVE_HOSTNAME,
54 CLIENT_RESOLVE_ADDRESS,
55 CLIENT_BROWSE_DNS_SERVERS,
67 char inbuf[BUFFER_SIZE], outbuf[BUFFER_SIZE];
68 size_t inbuf_length, outbuf_length;
70 AvahiSHostNameResolver *host_name_resolver;
71 AvahiSAddressResolver *address_resolver;
72 AvahiSDNSServerBrowser *dns_server_browser;
74 AvahiProtocol afquery;
76 AVAHI_LLIST_FIELDS(Client, clients);
80 const AvahiPoll *poll_api;
83 AVAHI_LLIST_HEAD(Client, clients);
89 static Server *server = NULL;
91 static void client_work(AvahiWatch *watch, int fd, AvahiWatchEvent events, void *userdata);
93 static void client_free(Client *c) {
96 assert(c->server->n_clients >= 1);
97 c->server->n_clients--;
99 if (c->host_name_resolver)
100 avahi_s_host_name_resolver_free(c->host_name_resolver);
102 if (c->address_resolver)
103 avahi_s_address_resolver_free(c->address_resolver);
105 if (c->dns_server_browser)
106 avahi_s_dns_server_browser_free(c->dns_server_browser);
108 c->server->poll_api->watch_free(c->watch);
111 AVAHI_LLIST_REMOVE(Client, clients, c->server->clients, c);
115 static void client_new(Server *s, int fd) {
120 c = avahi_new(Client, 1);
123 c->state = CLIENT_IDLE;
125 c->inbuf_length = c->outbuf_length = 0;
127 c->host_name_resolver = NULL;
128 c->address_resolver = NULL;
129 c->dns_server_browser = NULL;
131 c->watch = s->poll_api->watch_new(s->poll_api, fd, AVAHI_WATCH_IN, client_work, c);
133 AVAHI_LLIST_PREPEND(Client, clients, s->clients, c);
137 static void client_output(Client *c, const uint8_t*data, size_t size) {
146 k = sizeof(c->outbuf) - c->outbuf_length;
147 m = size > k ? k : size;
149 memcpy(c->outbuf + c->outbuf_length, data, m);
150 c->outbuf_length += m;
152 server->poll_api->watch_update(c->watch, AVAHI_WATCH_OUT);
155 static void client_output_printf(Client *c, const char *format, ...) {
159 va_start(ap, format);
160 t = avahi_strdup_vprintf(format, ap);
163 client_output(c, (uint8_t*) t, strlen(t));
168 static void host_name_resolver_callback(
169 AvahiSHostNameResolver *r,
171 AvahiProtocol protocol,
172 AvahiResolverEvent event,
173 const char *hostname,
174 const AvahiAddress *a,
175 AvahiLookupResultFlags flags,
178 Client *c = userdata;
182 if (event == AVAHI_RESOLVER_TIMEOUT)
183 client_output_printf(c, "%+i Query timed out\n", AVAHI_ERR_TIMEOUT);
184 else if (event == AVAHI_RESOLVER_FAILURE)
185 client_output_printf(c, "%+i Query failed\n", AVAHI_ERR_FAILURE);
186 else if (event == AVAHI_RESOLVER_NOT_FOUND)
187 client_output_printf(c, "%+i Query timed out\n", AVAHI_ERR_NOT_FOUND);
188 else if (event == AVAHI_RESOLVER_FOUND) {
190 avahi_address_snprint(t, sizeof(t), a);
191 client_output_printf(c, "+ %i %u %s %s\n", iface, protocol, hostname, t);
194 c->state = CLIENT_DEAD;
197 static void address_resolver_callback(
198 AvahiSAddressResolver *r,
200 AvahiProtocol protocol,
201 AvahiResolverEvent event,
202 const AvahiAddress *a,
203 const char *hostname,
204 AvahiLookupResultFlags flags,
207 Client *c = userdata;
211 if (event == AVAHI_RESOLVER_TIMEOUT)
212 client_output_printf(c, "%+i Query timed out\n", AVAHI_ERR_TIMEOUT);
213 else if (event == AVAHI_RESOLVER_FAILURE)
214 client_output_printf(c, "%+i Query failed\n", AVAHI_ERR_FAILURE);
215 else if (event == AVAHI_RESOLVER_NOT_FOUND)
216 client_output_printf(c, "%+i Not found\n", AVAHI_ERR_NOT_FOUND);
217 else if (event == AVAHI_RESOLVER_FOUND)
218 client_output_printf(c, "+ %i %u %s\n", iface, protocol, hostname);
220 c->state = CLIENT_DEAD;
223 static void dns_server_browser_callback(
224 AvahiSDNSServerBrowser *b,
225 AvahiIfIndex interface,
226 AvahiProtocol protocol,
227 AvahiBrowserEvent event,
228 const char *host_name,
229 const AvahiAddress *a,
231 AvahiLookupResultFlags flags,
234 Client *c = userdata;
243 case AVAHI_BROWSER_FAILURE:
244 client_output_printf(c, "%+i Query failed\n", AVAHI_ERR_FAILURE);
245 c->state = CLIENT_DEAD;
248 case AVAHI_BROWSER_NOT_FOUND:
249 client_output_printf(c, "%+i Not found\n", AVAHI_ERR_FAILURE);
250 c->state = CLIENT_DEAD;
253 case AVAHI_BROWSER_ALL_FOR_NOW:
254 case AVAHI_BROWSER_CACHE_EXHAUSTED:
257 case AVAHI_BROWSER_NEW:
258 case AVAHI_BROWSER_REMOVE:
260 avahi_address_snprint(t, sizeof(t), a);
261 client_output_printf(c, "%c %i %u %s %u\n", event == AVAHI_BROWSER_NEW ? '>' : '<', interface, protocol, t, port);
266 static void handle_line(Client *c, const char *s) {
267 char cmd[64], arg[64];
273 if (c->state != CLIENT_IDLE)
276 if ((n_args = sscanf(s, "%63s %63s", cmd, arg)) < 1 ) {
277 client_output_printf(c, "%+i Failed to parse command, try \"HELP\".\n", AVAHI_ERR_INVALID_OPERATION);
278 c->state = CLIENT_DEAD;
282 if (strcmp(cmd, "HELP") == 0) {
283 client_output_printf(c,
284 "+ Available commands are:\n"
285 "+ RESOLVE-HOSTNAME <hostname>\n"
286 "+ RESOLVE-HOSTNAME-IPV6 <hostname>\n"
287 "+ RESOLVE-HOSTNAME-IPV4 <hostname>\n"
288 "+ RESOLVE-ADDRESS <address>\n"
289 "+ BROWSE-DNS-SERVERS\n"
290 "+ BROWSE-DNS-SERVERS-IPV4\n"
291 "+ BROWSE-DNS-SERVERS-IPV6\n");
292 c->state = CLIENT_DEAD; }
293 else if (strcmp(cmd, "FUCK") == 0 && n_args == 1) {
294 client_output_printf(c, "+ FUCK: Go fuck yourself!\n");
295 c->state = CLIENT_DEAD;
296 } else if (strcmp(cmd, "RESOLVE-HOSTNAME-IPV4") == 0 && n_args == 2) {
297 c->state = CLIENT_RESOLVE_HOSTNAME;
298 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, 0, host_name_resolver_callback, c)))
301 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
302 } else if (strcmp(cmd, "RESOLVE-HOSTNAME-IPV6") == 0 && n_args == 2) {
303 c->state = CLIENT_RESOLVE_HOSTNAME;
304 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, 0, host_name_resolver_callback, c)))
307 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
308 } else if (strcmp(cmd, "RESOLVE-HOSTNAME") == 0 && n_args == 2) {
309 c->state = CLIENT_RESOLVE_HOSTNAME;
310 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, 0, host_name_resolver_callback, c)))
313 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
314 } else if (strcmp(cmd, "RESOLVE-ADDRESS") == 0 && n_args == 2) {
317 if (!(avahi_address_parse(arg, AVAHI_PROTO_UNSPEC, &addr))) {
318 client_output_printf(c, "%+i Failed to parse address \"%s\".\n", AVAHI_ERR_INVALID_ADDRESS, arg);
319 c->state = CLIENT_DEAD;
321 c->state = CLIENT_RESOLVE_ADDRESS;
322 if (!(c->address_resolver = avahi_s_address_resolver_new(avahi_server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, &addr, 0, address_resolver_callback, c)))
326 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
328 } else if (strcmp(cmd, "BROWSE-DNS-SERVERS-IPV4") == 0 && n_args == 1) {
329 c->state = CLIENT_BROWSE_DNS_SERVERS;
330 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, 0, dns_server_browser_callback, c)))
332 client_output_printf(c, "+ Browsing ...\n");
334 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
336 } else if (strcmp(cmd, "BROWSE-DNS-SERVERS-IPV6") == 0 && n_args == 1) {
337 c->state = CLIENT_BROWSE_DNS_SERVERS;
338 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, 0, dns_server_browser_callback, c)))
340 client_output_printf(c, "+ Browsing ...\n");
342 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
344 } else if (strcmp(cmd, "BROWSE-DNS-SERVERS") == 0 && n_args == 1) {
345 c->state = CLIENT_BROWSE_DNS_SERVERS;
346 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, 0, dns_server_browser_callback, c)))
348 client_output_printf(c, "+ Browsing ...\n");
350 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
353 client_output_printf(c, "%+i Invalid command \"%s\", try \"HELP\".\n", AVAHI_ERR_INVALID_OPERATION, cmd);
354 c->state = CLIENT_DEAD;
356 avahi_log_debug(__FILE__": Got invalid request '%s'.", cmd);
362 client_output_printf(c, "%+i %s\n", avahi_server_errno(avahi_server), avahi_strerror(avahi_server_errno(avahi_server)));
363 c->state = CLIENT_DEAD;
366 static void handle_input(Client *c) {
373 if (!(e = memchr(c->inbuf, '\n', c->inbuf_length)))
376 k = e - (char*) c->inbuf;
379 handle_line(c, c->inbuf);
380 c->inbuf_length -= k + 1;
381 memmove(c->inbuf, e+1, c->inbuf_length);
385 static void client_work(AvahiWatch *watch, int fd, AvahiWatchEvent events, void *userdata) {
386 Client *c = userdata;
390 if ((events & AVAHI_WATCH_IN) && c->inbuf_length < sizeof(c->inbuf)) {
393 if ((r = read(c->fd, c->inbuf + c->inbuf_length, sizeof(c->inbuf) - c->inbuf_length)) <= 0) {
395 avahi_log_warn("read(): %s", strerror(errno));
400 c->inbuf_length += r;
401 assert(c->inbuf_length <= sizeof(c->inbuf));
406 if ((events & AVAHI_WATCH_OUT) && c->outbuf_length > 0) {
409 if ((r = write(c->fd, c->outbuf, c->outbuf_length)) < 0) {
410 avahi_log_warn("write(): %s", strerror(errno));
415 assert((size_t) r <= c->outbuf_length);
416 c->outbuf_length -= r;
418 if (c->outbuf_length)
419 memmove(c->outbuf, c->outbuf + r, c->outbuf_length - r);
421 if (c->outbuf_length == 0 && c->state == CLIENT_DEAD) {
427 c->server->poll_api->watch_update(
429 (c->outbuf_length > 0 ? AVAHI_WATCH_OUT : 0) |
430 (c->inbuf_length < sizeof(c->inbuf) ? AVAHI_WATCH_IN : 0));
433 static void server_work(AvahiWatch *watch, int fd, AvahiWatchEvent events, void *userdata) {
434 Server *s = userdata;
438 if (events & AVAHI_WATCH_IN) {
441 if ((cfd = accept(fd, NULL, NULL)) < 0)
442 avahi_log_error("accept(): %s", strerror(errno));
448 int simple_protocol_setup(const AvahiPoll *poll_api) {
449 struct sockaddr_un sa;
454 server = avahi_new(Server, 1);
455 server->poll_api = poll_api;
456 server->bind_successful = 0;
458 server->n_clients = 0;
459 AVAHI_LLIST_HEAD_INIT(Client, server->clients);
460 server->watch = NULL;
464 if ((server->fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
465 avahi_log_warn("socket(PF_LOCAL, SOCK_STREAM, 0): %s", strerror(errno));
469 memset(&sa, 0, sizeof(sa));
470 sa.sun_family = AF_LOCAL;
471 strncpy(sa.sun_path, AVAHI_SOCKET, sizeof(sa.sun_path)-1);
473 /* We simply remove existing UNIX sockets under this name. The
474 Avahi daemons makes sure that it runs only once on a host,
475 therefore sockets that already exist are stale and may be
476 removed without any ill effects */
478 unlink(AVAHI_SOCKET);
480 if (bind(server->fd, &sa, sizeof(sa)) < 0) {
481 avahi_log_warn("bind(): %s", strerror(errno));
485 server->bind_successful = 1;
487 if (listen(server->fd, 2) < 0) {
488 avahi_log_warn("listen(): %s", strerror(errno));
494 server->watch = poll_api->watch_new(poll_api, server->fd, AVAHI_WATCH_IN, server_work, server);
501 simple_protocol_shutdown();
506 void simple_protocol_shutdown(void) {
510 if (server->bind_successful)
511 unlink(AVAHI_SOCKET);
513 while (server->clients)
514 client_free(server->clients);
517 server->poll_api->watch_free(server->watch);
528 void simple_protocol_restart_queries(void) {
531 /* Restart queries in case of local domain name changes */
535 for (c = server->clients; c; c = c->clients_next)
536 if (c->state == CLIENT_BROWSE_DNS_SERVERS && c->dns_server_browser) {
537 avahi_s_dns_server_browser_free(c->dns_server_browser);
538 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, 0, dns_server_browser_callback, c);