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>
42 static struct uloop_timeout reconnect;
43 char *iface_name = "eth0";
47 parse_answer(struct uloop_fd *u, 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(u, buffer, len, name, a, rdata);
80 parse_question(struct uloop_fd *u, 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(u, NULL);
94 service_announce_services(u, name);
95 service_reply(u, name);
100 host = strstr(name, ".local");
103 if (!strcmp(hostname, name))
104 service_reply_a(u, q->type);
110 read_socket(struct uloop_fd *u, unsigned int events)
112 uint8_t buffer[8 * 1024];
114 struct dns_header *h;
121 uloop_timeout_set(&reconnect, 1000);
125 rlen = len = read(u->fd, buffer, sizeof(buffer));
127 fprintf(stderr, "read failed: %s\n", strerror(errno));
131 h = dns_consume_header(&b, &rlen);
133 fprintf(stderr, "dropping: bad header\n");
137 while (h->questions-- > 0) {
138 char *name = dns_consume_name(buffer, len, &b, &rlen);
139 struct dns_question *q;
142 fprintf(stderr, "dropping: bad name\n");
146 q = dns_consume_question(&b, &rlen);
148 fprintf(stderr, "dropping: bad question\n");
152 if (!(h->flags & FLAG_RESPONSE))
153 parse_question(announce_fd, name, q);
156 if (!(h->flags & FLAG_RESPONSE))
159 while (h->answers-- > 0)
160 parse_answer(u, buffer, len, &b, &rlen, 1);
162 while (h->authority-- > 0)
163 parse_answer(u, buffer, len, &b, &rlen, 0);
165 while (h->additional-- > 0)
166 parse_answer(u, buffer, len, &b, &rlen, 1);
170 reconnect_socket(struct uloop_timeout *timeout)
174 listener.fd = usock(USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK, MCAST_ADDR, "5353");
176 if (!iface_ip || listener.fd < 0) {
177 fprintf(stderr, "failed to add listener: %s\n", strerror(errno));
178 uloop_timeout_set(&reconnect, 1000);
180 if (socket_setup(listener.fd, iface_ip)) {
181 uloop_timeout_set(&reconnect, 1000);
186 uloop_fd_add(&listener, ULOOP_READ);
188 dns_send_question(&listener, "_services._dns-sd._tcp.local", TYPE_PTR);
189 dns_send_question(&listener, "_services._dns-sd._udp.local", TYPE_PTR);
190 announce_init(&listener);
195 main(int argc, char **argv)
199 while ((ch = getopt(argc, argv, "h:t:i:d")) != -1) {
209 fprintf(stderr, "invalid ttl\n");
223 iface_ip = get_iface_ipv4(iface_name);
226 fprintf(stderr, "failed to read ip for %s\n", iface_name);
229 fprintf(stderr, "interface %s has ip %s\n", iface_name, iface_ip);
240 listener.cb = read_socket;
241 reconnect.cb = reconnect_socket;
244 uloop_timeout_set(&reconnect, 100);