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));
167 static void host_name_resolver_callback(
168 AvahiSHostNameResolver *r,
170 AvahiProtocol protocol,
171 AvahiResolverEvent event,
172 const char *hostname,
173 const AvahiAddress *a,
174 AvahiLookupResultFlags flags,
177 Client *c = userdata;
181 if (event == AVAHI_RESOLVER_FAILURE)
182 client_output_printf(c, "%+i %s\n", avahi_server_errno(avahi_server), avahi_strerror(avahi_server_errno(avahi_server)));
183 else if (event == AVAHI_RESOLVER_FOUND) {
184 char t[AVAHI_ADDRESS_STR_MAX];
185 avahi_address_snprint(t, sizeof(t), a);
186 client_output_printf(c, "+ %i %u %s %s\n", iface, protocol, hostname, t);
189 c->state = CLIENT_DEAD;
192 static void address_resolver_callback(
193 AvahiSAddressResolver *r,
195 AvahiProtocol protocol,
196 AvahiResolverEvent event,
197 const AvahiAddress *a,
198 const char *hostname,
199 AvahiLookupResultFlags flags,
202 Client *c = userdata;
206 if (event == AVAHI_RESOLVER_FAILURE)
207 client_output_printf(c, "%+i %s\n", avahi_server_errno(avahi_server), avahi_strerror(avahi_server_errno(avahi_server)));
208 else if (event == AVAHI_RESOLVER_FOUND)
209 client_output_printf(c, "+ %i %u %s\n", iface, protocol, hostname);
211 c->state = CLIENT_DEAD;
214 static void dns_server_browser_callback(
215 AvahiSDNSServerBrowser *b,
216 AvahiIfIndex interface,
217 AvahiProtocol protocol,
218 AvahiBrowserEvent event,
219 const char *host_name,
220 const AvahiAddress *a,
222 AvahiLookupResultFlags flags,
225 Client *c = userdata;
226 char t[AVAHI_ADDRESS_STR_MAX];
234 case AVAHI_BROWSER_FAILURE:
235 client_output_printf(c, "%+i %s\n", avahi_server_errno(avahi_server), avahi_strerror(avahi_server_errno(avahi_server)));
236 c->state = CLIENT_DEAD;
239 case AVAHI_BROWSER_ALL_FOR_NOW:
240 case AVAHI_BROWSER_CACHE_EXHAUSTED:
243 case AVAHI_BROWSER_NEW:
244 case AVAHI_BROWSER_REMOVE:
246 avahi_address_snprint(t, sizeof(t), a);
247 client_output_printf(c, "%c %i %u %s %u\n", event == AVAHI_BROWSER_NEW ? '>' : '<', interface, protocol, t, port);
252 static void handle_line(Client *c, const char *s) {
253 char cmd[64], arg[64];
259 if (c->state != CLIENT_IDLE)
262 if ((n_args = sscanf(s, "%63s %63s", cmd, arg)) < 1 ) {
263 client_output_printf(c, "%+i Failed to parse command, try \"HELP\".\n", AVAHI_ERR_INVALID_OPERATION);
264 c->state = CLIENT_DEAD;
268 if (strcmp(cmd, "HELP") == 0) {
269 client_output_printf(c,
270 "+ Available commands are:\n"
271 "+ RESOLVE-HOSTNAME <hostname>\n"
272 "+ RESOLVE-HOSTNAME-IPV6 <hostname>\n"
273 "+ RESOLVE-HOSTNAME-IPV4 <hostname>\n"
274 "+ RESOLVE-ADDRESS <address>\n"
275 "+ BROWSE-DNS-SERVERS\n"
276 "+ BROWSE-DNS-SERVERS-IPV4\n"
277 "+ BROWSE-DNS-SERVERS-IPV6\n");
278 c->state = CLIENT_DEAD; }
279 else if (strcmp(cmd, "FUCK") == 0 && n_args == 1) {
280 client_output_printf(c, "+ FUCK: Go fuck yourself!\n");
281 c->state = CLIENT_DEAD;
282 } else if (strcmp(cmd, "RESOLVE-HOSTNAME-IPV4") == 0 && n_args == 2) {
283 c->state = CLIENT_RESOLVE_HOSTNAME;
284 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)))
287 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
288 } else if (strcmp(cmd, "RESOLVE-HOSTNAME-IPV6") == 0 && n_args == 2) {
289 c->state = CLIENT_RESOLVE_HOSTNAME;
290 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)))
293 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
294 } else if (strcmp(cmd, "RESOLVE-HOSTNAME") == 0 && n_args == 2) {
295 c->state = CLIENT_RESOLVE_HOSTNAME;
296 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)))
299 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
300 } else if (strcmp(cmd, "RESOLVE-ADDRESS") == 0 && n_args == 2) {
303 if (!(avahi_address_parse(arg, AVAHI_PROTO_UNSPEC, &addr))) {
304 client_output_printf(c, "%+i Failed to parse address \"%s\".\n", AVAHI_ERR_INVALID_ADDRESS, arg);
305 c->state = CLIENT_DEAD;
307 c->state = CLIENT_RESOLVE_ADDRESS;
308 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)))
312 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
314 } else if (strcmp(cmd, "BROWSE-DNS-SERVERS-IPV4") == 0 && n_args == 1) {
315 c->state = CLIENT_BROWSE_DNS_SERVERS;
316 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)))
318 client_output_printf(c, "+ Browsing ...\n");
320 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
322 } else if (strcmp(cmd, "BROWSE-DNS-SERVERS-IPV6") == 0 && n_args == 1) {
323 c->state = CLIENT_BROWSE_DNS_SERVERS;
324 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)))
326 client_output_printf(c, "+ Browsing ...\n");
328 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
330 } else if (strcmp(cmd, "BROWSE-DNS-SERVERS") == 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_UNSPEC, AVAHI_LOOKUP_USE_MULTICAST, dns_server_browser_callback, c)))
334 client_output_printf(c, "+ Browsing ...\n");
336 avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
339 client_output_printf(c, "%+i Invalid command \"%s\", try \"HELP\".\n", AVAHI_ERR_INVALID_OPERATION, cmd);
340 c->state = CLIENT_DEAD;
342 avahi_log_debug(__FILE__": Got invalid request '%s'.", cmd);
348 client_output_printf(c, "%+i %s\n", avahi_server_errno(avahi_server), avahi_strerror(avahi_server_errno(avahi_server)));
349 c->state = CLIENT_DEAD;
352 static void handle_input(Client *c) {
359 if (!(e = memchr(c->inbuf, '\n', c->inbuf_length)))
362 k = e - (char*) c->inbuf;
365 handle_line(c, c->inbuf);
366 c->inbuf_length -= k + 1;
367 memmove(c->inbuf, e+1, c->inbuf_length);
371 static void client_work(AvahiWatch *watch, int fd, AvahiWatchEvent events, void *userdata) {
372 Client *c = userdata;
376 if ((events & AVAHI_WATCH_IN) && c->inbuf_length < sizeof(c->inbuf)) {
379 if ((r = read(c->fd, c->inbuf + c->inbuf_length, sizeof(c->inbuf) - c->inbuf_length)) <= 0) {
381 avahi_log_warn("read(): %s", strerror(errno));
386 c->inbuf_length += r;
387 assert(c->inbuf_length <= sizeof(c->inbuf));
392 if ((events & AVAHI_WATCH_OUT) && c->outbuf_length > 0) {
395 if ((r = write(c->fd, c->outbuf, c->outbuf_length)) < 0) {
396 avahi_log_warn("write(): %s", strerror(errno));
401 assert((size_t) r <= c->outbuf_length);
402 c->outbuf_length -= r;
404 if (c->outbuf_length)
405 memmove(c->outbuf, c->outbuf + r, c->outbuf_length - r);
407 if (c->outbuf_length == 0 && c->state == CLIENT_DEAD) {
413 c->server->poll_api->watch_update(
415 (c->outbuf_length > 0 ? AVAHI_WATCH_OUT : 0) |
416 (c->inbuf_length < sizeof(c->inbuf) ? AVAHI_WATCH_IN : 0));
419 static void server_work(AvahiWatch *watch, int fd, AvahiWatchEvent events, void *userdata) {
420 Server *s = userdata;
424 if (events & AVAHI_WATCH_IN) {
427 if ((cfd = accept(fd, NULL, NULL)) < 0)
428 avahi_log_error("accept(): %s", strerror(errno));
434 int simple_protocol_setup(const AvahiPoll *poll_api) {
435 struct sockaddr_un sa;
440 server = avahi_new(Server, 1);
441 server->poll_api = poll_api;
442 server->bind_successful = 0;
444 server->n_clients = 0;
445 AVAHI_LLIST_HEAD_INIT(Client, server->clients);
446 server->watch = NULL;
450 if ((server->fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
451 avahi_log_warn("socket(PF_LOCAL, SOCK_STREAM, 0): %s", strerror(errno));
455 memset(&sa, 0, sizeof(sa));
456 sa.sun_family = AF_LOCAL;
457 strncpy(sa.sun_path, AVAHI_SOCKET, sizeof(sa.sun_path)-1);
459 /* We simply remove existing UNIX sockets under this name. The
460 Avahi daemons makes sure that it runs only once on a host,
461 therefore sockets that already exist are stale and may be
462 removed without any ill effects */
464 unlink(AVAHI_SOCKET);
466 if (bind(server->fd, &sa, sizeof(sa)) < 0) {
467 avahi_log_warn("bind(): %s", strerror(errno));
471 server->bind_successful = 1;
473 if (listen(server->fd, 2) < 0) {
474 avahi_log_warn("listen(): %s", strerror(errno));
480 server->watch = poll_api->watch_new(poll_api, server->fd, AVAHI_WATCH_IN, server_work, server);
487 simple_protocol_shutdown();
492 void simple_protocol_shutdown(void) {
496 if (server->bind_successful)
497 unlink(AVAHI_SOCKET);
499 while (server->clients)
500 client_free(server->clients);
503 server->poll_api->watch_free(server->watch);
514 void simple_protocol_restart_queries(void) {
517 /* Restart queries in case of local domain name changes */
521 for (c = server->clients; c; c = c->clients_next)
522 if (c->state == CLIENT_BROWSE_DNS_SERVERS && c->dns_server_browser) {
523 avahi_s_dns_server_browser_free(c->dns_server_browser);
524 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);