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";
48 parse_answer(struct uloop_fd *u, uint8_t *buffer, int len, uint8_t **b, int *rlen, int cache)
50 char *name = dns_consume_name(buffer, len, b, rlen);
55 fprintf(stderr, "dropping: bad question\n");
59 a = dns_consume_answer(b, rlen);
61 fprintf(stderr, "dropping: bad question\n");
66 if (a->rdlength > *rlen) {
67 fprintf(stderr, "dropping: bad question\n");
75 cache_answer(u, buffer, len, name, a, rdata);
81 parse_question(struct uloop_fd *u, char *name, struct dns_question *q)
85 DBG(1, "Q -> %s %s\n", dns_type_string(q->type), name);
89 host = service_name("local");
90 if (!strcmp(name, host))
91 service_reply(u, NULL);
95 service_announce_services(u, name);
96 service_reply(u, name);
101 host = strstr(name, ".local");
104 if (!strcmp(hostname, name))
105 service_reply_a(u, q->type);
111 read_socket(struct uloop_fd *u, unsigned int events)
113 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(announce_fd, name, q);
157 if (!(h->flags & FLAG_RESPONSE))
160 while (h->answers-- > 0)
161 parse_answer(u, buffer, len, &b, &rlen, 1);
163 while (h->authority-- > 0)
164 parse_answer(u, buffer, len, &b, &rlen, 0);
166 while (h->additional-- > 0)
167 parse_answer(u, buffer, len, &b, &rlen, 1);
171 reconnect_socket(struct uloop_timeout *timeout)
175 listener.fd = usock(USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK, MCAST_ADDR, "5353");
177 if (!iface_ip || listener.fd < 0) {
178 fprintf(stderr, "failed to add listener: %s\n", strerror(errno));
179 uloop_timeout_set(&reconnect, 1000);
181 if (socket_setup(listener.fd, iface_ip)) {
182 uloop_timeout_set(&reconnect, 1000);
187 uloop_fd_add(&listener, ULOOP_READ);
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);
230 iface_index = get_iface_index(iface_name);
233 fprintf(stderr, "failed to read index for %s\n", iface_name);
237 fprintf(stderr, "interface %s has ip %s and index %d\n", iface_name, iface_ip, iface_index);
245 listener.cb = read_socket;
246 reconnect.cb = reconnect_socket;
249 uloop_timeout_set(&reconnect, 100);