2 * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2.1
6 * as published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
15 #include <sys/types.h>
23 #include <sys/types.h>
24 #include <arpa/inet.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <arpa/nameser.h>
28 #include <asm/byteorder.h>
31 #include <libubox/usock.h>
32 #include <libubox/uloop.h>
33 #include <libubox/avl-cmp.h>
41 #include "interface.h"
43 static struct uloop_timeout reconnect;
44 char *iface_name = "eth0";
47 parse_answer(struct interface *iface, uint8_t *buffer, int len, uint8_t **b, int *rlen, int cache)
49 char *name = dns_consume_name(buffer, len, b, rlen);
54 fprintf(stderr, "dropping: bad question\n");
58 a = dns_consume_answer(b, rlen);
60 fprintf(stderr, "dropping: bad question\n");
65 if (a->rdlength > *rlen) {
66 fprintf(stderr, "dropping: bad question\n");
74 cache_answer(iface, buffer, len, name, a, rdata);
80 parse_question(struct interface *iface, char *name, struct dns_question *q)
84 DBG(1, "Q -> %s %s\n", dns_type_string(q->type), name);
88 host = service_name("local");
89 if (!strcmp(name, host))
90 service_reply(iface, NULL);
94 service_announce_services(iface, name);
95 service_reply(iface, name);
100 host = strstr(name, ".local");
103 if (!strcmp(hostname, name))
104 service_reply_a(iface, q->type);
110 read_socket(struct uloop_fd *u, unsigned int events)
112 struct interface *iface = container_of(u, struct interface, fd);
113 static uint8_t buffer[8 * 1024];
115 struct dns_header *h;
122 uloop_timeout_set(&reconnect, 1000);
126 rlen = len = read(u->fd, buffer, sizeof(buffer));
128 fprintf(stderr, "read failed: %s\n", strerror(errno));
132 h = dns_consume_header(&b, &rlen);
134 fprintf(stderr, "dropping: bad header\n");
138 while (h->questions-- > 0) {
139 char *name = dns_consume_name(buffer, len, &b, &rlen);
140 struct dns_question *q;
143 fprintf(stderr, "dropping: bad name\n");
147 q = dns_consume_question(&b, &rlen);
149 fprintf(stderr, "dropping: bad question\n");
153 if (!(h->flags & FLAG_RESPONSE))
154 parse_question(iface, name, q);
157 if (!(h->flags & FLAG_RESPONSE))
160 while (h->answers-- > 0)
161 parse_answer(iface, buffer, len, &b, &rlen, 1);
163 while (h->authority-- > 0)
164 parse_answer(iface, buffer, len, &b, &rlen, 0);
166 while (h->additional-- > 0)
167 parse_answer(iface, buffer, len, &b, &rlen, 1);
171 reconnect_socket(struct uloop_timeout *timeout)
173 cur_iface->fd.fd = usock(USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK, MCAST_ADDR, "5353");
174 if (cur_iface->fd.fd < 0) {
175 fprintf(stderr, "failed to add listener: %s\n", strerror(errno));
176 uloop_timeout_set(&reconnect, 1000);
178 if (interface_socket_setup(cur_iface)) {
179 uloop_timeout_set(&reconnect, 1000);
180 cur_iface->fd.fd = -1;
184 uloop_fd_add(&cur_iface->fd, ULOOP_READ);
186 dns_send_question(cur_iface, "_services._dns-sd._udp.local", TYPE_PTR);
192 main(int argc, char **argv)
196 while ((ch = getopt(argc, argv, "h:t:i:d")) != -1) {
206 fprintf(stderr, "invalid ttl\n");
222 if (interface_add(iface_name)) {
223 fprintf(stderr, "Failed to add interface %s\n", iface_name);
237 cur_iface->fd.cb = read_socket;
238 reconnect.cb = reconnect_socket;
240 uloop_timeout_set(&reconnect, 100);