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>
39 #include <avahi-core/log.h>
40 #include <avahi-core/lookup.h>
41 #include <avahi-core/dns-srv-rr.h>
43 #include "simple-protocol.h"
46 #define BUFFER_SIZE (20*1024)
48 #define CLIENTS_MAX 50
50 typedef struct Client Client;
51 typedef struct Server Server;
55 CLIENT_RESOLVE_HOSTNAME,
56 CLIENT_RESOLVE_ADDRESS,
57 CLIENT_BROWSE_DNS_SERVERS,
69 char inbuf[BUFFER_SIZE], outbuf[BUFFER_SIZE];
70 size_t inbuf_length, outbuf_length;
72 AvahiSHostNameResolver *host_name_resolver;
73 AvahiSAddressResolver *address_resolver;
74 AvahiSDNSServerBrowser *dns_server_browser;
76 AvahiProtocol afquery;
78 AVAHI_LLIST_FIELDS(Client, clients);
82 const AvahiPoll *poll_api;
85 AVAHI_LLIST_HEAD(Client, clients);
91 static Server *server = NULL;
93 static void client_work(AvahiWatch *watch, int fd, AvahiWatchEvent events, void *userdata);
95 static void client_free(Client *c) {
98 assert(c->server->n_clients >= 1);
99 c->server->n_clients--;
101 if (c->host_name_resolver)
102 avahi_s_host_name_resolver_free(c->host_name_resolver);
104 if (c->address_resolver)
105 avahi_s_address_resolver_free(c->address_resolver);
107 if (c->dns_server_browser)
108 avahi_s_dns_server_browser_free(c->dns_server_browser);
110 c->server->poll_api->watch_free(c->watch);
113 AVAHI_LLIST_REMOVE(Client, clients, c->server->clients, c);
117 static void client_new(Server *s, int fd) {
122 c = avahi_new(Client, 1);
125 c->state = CLIENT_IDLE;
127 c->inbuf_length = c->outbuf_length = 0;
129 c->host_name_resolver = NULL;
130 c->address_resolver = NULL;
131 c->dns_server_browser = NULL;
133 c->watch = s->poll_api->watch_new(s->poll_api, fd, AVAHI_WATCH_IN, client_work, c);
135 AVAHI_LLIST_PREPEND(Client, clients, s->clients, c);
139 static void client_output(Client *c, const uint8_t*data, size_t size) {
148 k = sizeof(c->outbuf) - c->outbuf_length;
149 m = size > k ? k : size;
151 memcpy(c->outbuf + c->outbuf_length, data, m);
152 c->outbuf_length += m;
154 server->poll_api->watch_update(c->watch, AVAHI_WATCH_OUT);
157 static void client_output_printf(Client *c, const char *format, ...) {
161 va_start(ap, format);
162 t = avahi_strdup_vprintf(format, ap);
165 client_output(c, (uint8_t*) t, strlen(t));
169 static void host_name_resolver_callback(
170 AvahiSHostNameResolver *r,
172 AvahiProtocol protocol,
173 AvahiResolverEvent event,
174 const char *hostname,
175 const AvahiAddress *a,
176 AvahiLookupResultFlags flags,
179 Client *c = userdata;
183 if (event == AVAHI_RESOLVER_FAILURE)
184 client_output_printf(c, "%+i %s\n", avahi_server_errno(avahi_server), avahi_strerror(avahi_server_errno(avahi_server)));
185 else if (event == AVAHI_RESOLVER_FOUND) {
186 char t[AVAHI_ADDRESS_STR_MAX];
187 avahi_address_snprint(t, sizeof(t), a);
188 client_output_printf(c, "+ %i %u %s %s\n", iface, protocol, hostname, t);
191 c->state = CLIENT_DEAD;
194 static void address_resolver_callback(
195 AvahiSAddressResolver *r,
197 AvahiProtocol protocol,
198 AvahiResolverEvent event,
199 const AvahiAddress *a,
200 const char *hostname,
201 AvahiLookupResultFlags flags,
204 Client *c = userdata;
208 if (event == AVAHI_RESOLVER_FAILURE)
209 client_output_printf(c, "%+i %s\n", avahi_server_errno(avahi_server), avahi_strerror(avahi_server_errno(avahi_server)));
210 else if (event == AVAHI_RESOLVER_FOUND)
211 client_output_printf(c, "+ %i %u %s\n", iface, protocol, hostname);
213 c->state = CLIENT_DEAD;
216 static void dns_server_browser_callback(
217 AvahiSDNSServerBrowser *b,
218 AvahiIfIndex interface,
219 AvahiProtocol protocol,
220 AvahiBrowserEvent event,
221 const char *host_name,
222 const AvahiAddress *a,
224 AvahiLookupResultFlags flags,
227 Client *c = userdata;
228 char t[AVAHI_ADDRESS_STR_MAX];
236 case AVAHI_BROWSER_FAILURE:
237 client_output_printf(c, "%+i %s\n", avahi_server_errno(avahi_server), avahi_strerror(avahi_server_errno(avahi_server)));
238 c->state = CLIENT_DEAD;
241 case AVAHI_BROWSER_ALL_FOR_NOW:
242 case AVAHI_BROWSER_CACHE_EXHAUSTED:
245 case AVAHI_BROWSER_NEW:
246 case AVAHI_BROWSER_REMOVE:
248 avahi_address_snprint(t, sizeof(t), a);
249 client_output_printf(c, "%c %i %u %s %u\n", event == AVAHI_BROWSER_NEW ? '>' : '<', interface, protocol, t, port);
254 static void handle_line(Client *c, const char *s) {
255 char cmd[64], arg[64];
261 if (c->state != CLIENT_IDLE)
264 if ((n_args = sscanf(s, "%63s %63s", cmd, arg)) < 1 ) {
265 client_output_printf(c, "%+i Failed to parse command, try \"HELP\".\n", AVAHI_ERR_INVALID_OPERATION);
266 c->state = CLIENT_DEAD;
270 if (strcmp(cmd, "HELP") == 0) {
271 client_output_printf(c,
272 "+ Available commands are:\n"
273 "+ RESOLVE-HOSTNAME <hostname>\n"
274 "+ RESOLVE-HOSTNAME-IPV6 <hostname>\n"
275 "+ RESOLVE-HOSTNAME-IPV4 <hostname>\n"
276 "+ RESOLVE-ADDRESS <address>\n"
277 "+ BROWSE-DNS-SERVERS\n"
278 "+ BROWSE-DNS-SERVERS-IPV4\n"
279 "+ BROWSE-DNS-SERVERS-IPV6\n");
280 c->state = CLIENT_DEAD; }
281 else if (strcmp(cmd, "FUCK") == 0 && n_args == 1) {
282 client_output_printf(c, "+ FUCK: Go fuck yourself!\n");
283 c->state = CLIENT_DEAD;
284 } else if (strcmp(cmd, "RESOLVE-HOSTNAME-IPV4") == 0 && n_args == 2) {
285 c->state = CLIENT_RESOLVE_HOSTNAME;
286 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)))
289 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
290 } else if (strcmp(cmd, "RESOLVE-HOSTNAME-IPV6") == 0 && n_args == 2) {
291 c->state = CLIENT_RESOLVE_HOSTNAME;
292 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)))
295 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
296 } else if (strcmp(cmd, "RESOLVE-HOSTNAME") == 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_UNSPEC, AVAHI_LOOKUP_USE_MULTICAST, host_name_resolver_callback, c)))
301 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
302 } else if (strcmp(cmd, "RESOLVE-ADDRESS") == 0 && n_args == 2) {
305 if (!(avahi_address_parse(arg, AVAHI_PROTO_UNSPEC, &addr))) {
306 client_output_printf(c, "%+i Failed to parse address \"%s\".\n", AVAHI_ERR_INVALID_ADDRESS, arg);
307 c->state = CLIENT_DEAD;
309 c->state = CLIENT_RESOLVE_ADDRESS;
310 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)))
314 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
316 } else if (strcmp(cmd, "BROWSE-DNS-SERVERS-IPV4") == 0 && n_args == 1) {
317 c->state = CLIENT_BROWSE_DNS_SERVERS;
318 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)))
320 client_output_printf(c, "+ Browsing ...\n");
322 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
324 } else if (strcmp(cmd, "BROWSE-DNS-SERVERS-IPV6") == 0 && n_args == 1) {
325 c->state = CLIENT_BROWSE_DNS_SERVERS;
326 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)))
328 client_output_printf(c, "+ Browsing ...\n");
330 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
332 } else if (strcmp(cmd, "BROWSE-DNS-SERVERS") == 0 && n_args == 1) {
333 c->state = CLIENT_BROWSE_DNS_SERVERS;
334 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)))
336 client_output_printf(c, "+ Browsing ...\n");
338 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
341 client_output_printf(c, "%+i Invalid command \"%s\", try \"HELP\".\n", AVAHI_ERR_INVALID_OPERATION, cmd);
342 c->state = CLIENT_DEAD;
344 avahi_log_debug(__FILE__": Got invalid request '%s'.", cmd);
350 client_output_printf(c, "%+i %s\n", avahi_server_errno(avahi_server), avahi_strerror(avahi_server_errno(avahi_server)));
351 c->state = CLIENT_DEAD;
354 static void handle_input(Client *c) {
361 if (!(e = memchr(c->inbuf, '\n', c->inbuf_length)))
364 k = e - (char*) c->inbuf;
367 handle_line(c, c->inbuf);
368 c->inbuf_length -= k + 1;
369 memmove(c->inbuf, e+1, c->inbuf_length);
373 static void client_work(AvahiWatch *watch, int fd, AvahiWatchEvent events, void *userdata) {
374 Client *c = userdata;
378 if ((events & AVAHI_WATCH_IN) && c->inbuf_length < sizeof(c->inbuf)) {
381 if ((r = read(c->fd, c->inbuf + c->inbuf_length, sizeof(c->inbuf) - c->inbuf_length)) <= 0) {
383 avahi_log_warn("read(): %s", strerror(errno));
388 c->inbuf_length += r;
389 assert(c->inbuf_length <= sizeof(c->inbuf));
394 if ((events & AVAHI_WATCH_OUT) && c->outbuf_length > 0) {
397 if ((r = write(c->fd, c->outbuf, c->outbuf_length)) < 0) {
398 avahi_log_warn("write(): %s", strerror(errno));
403 assert((size_t) r <= c->outbuf_length);
404 c->outbuf_length -= r;
406 if (c->outbuf_length)
407 memmove(c->outbuf, c->outbuf + r, c->outbuf_length - r);
409 if (c->outbuf_length == 0 && c->state == CLIENT_DEAD) {
415 c->server->poll_api->watch_update(
417 (c->outbuf_length > 0 ? AVAHI_WATCH_OUT : 0) |
418 (c->inbuf_length < sizeof(c->inbuf) ? AVAHI_WATCH_IN : 0));
421 static void server_work(AvahiWatch *watch, int fd, AvahiWatchEvent events, void *userdata) {
422 Server *s = userdata;
426 if (events & AVAHI_WATCH_IN) {
429 if ((cfd = accept(fd, NULL, NULL)) < 0)
430 avahi_log_error("accept(): %s", strerror(errno));
436 int simple_protocol_setup(const AvahiPoll *poll_api) {
437 struct sockaddr_un sa;
442 server = avahi_new(Server, 1);
443 server->poll_api = poll_api;
444 server->bind_successful = 0;
446 server->n_clients = 0;
447 AVAHI_LLIST_HEAD_INIT(Client, server->clients);
448 server->watch = NULL;
452 if ((server->fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
453 avahi_log_warn("socket(PF_LOCAL, SOCK_STREAM, 0): %s", strerror(errno));
457 memset(&sa, 0, sizeof(sa));
458 sa.sun_family = AF_LOCAL;
459 strncpy(sa.sun_path, AVAHI_SOCKET, sizeof(sa.sun_path)-1);
461 /* We simply remove existing UNIX sockets under this name. The
462 Avahi daemons makes sure that it runs only once on a host,
463 therefore sockets that already exist are stale and may be
464 removed without any ill effects */
466 unlink(AVAHI_SOCKET);
468 if (bind(server->fd, &sa, sizeof(sa)) < 0) {
469 avahi_log_warn("bind(): %s", strerror(errno));
473 server->bind_successful = 1;
475 if (listen(server->fd, 2) < 0) {
476 avahi_log_warn("listen(): %s", strerror(errno));
482 server->watch = poll_api->watch_new(poll_api, server->fd, AVAHI_WATCH_IN, server_work, server);
489 simple_protocol_shutdown();
494 void simple_protocol_shutdown(void) {
498 if (server->bind_successful)
499 unlink(AVAHI_SOCKET);
501 while (server->clients)
502 client_free(server->clients);
505 server->poll_api->watch_free(server->watch);
516 void simple_protocol_restart_queries(void) {
519 /* Restart queries in case of local domain name changes */
523 for (c = server->clients; c; c = c->clients_next)
524 if (c->state == CLIENT_BROWSE_DNS_SERVERS && c->dns_server_browser) {
525 avahi_s_dns_server_browser_free(c->dns_server_browser);
526 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);