initial code refactoring to prepare for adding dynamic interface support
[project/mdnsd.git] / main.c
1 /*
2 * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
3 *
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
7 *
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.
12 */
13
14 #include <sys/stat.h>
15 #include <sys/types.h>
16
17 #include <time.h>
18 #include <stdio.h>
19 #include <fcntl.h>
20 #include <getopt.h>
21 #include <resolv.h>
22 #include <unistd.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>
29
30 #include <libubus.h>
31 #include <libubox/usock.h>
32 #include <libubox/uloop.h>
33 #include <libubox/avl-cmp.h>
34
35 #include "dns.h"
36 #include "ubus.h"
37 #include "util.h"
38 #include "cache.h"
39 #include "service.h"
40 #include "announce.h"
41 #include "interface.h"
42
43 static struct uloop_timeout reconnect;
44 char *iface_name = "eth0";
45
46 static int
47 parse_answer(struct uloop_fd *u, uint8_t *buffer, int len, uint8_t **b, int *rlen, int cache)
48 {
49 char *name = dns_consume_name(buffer, len, b, rlen);
50 struct dns_answer *a;
51 uint8_t *rdata;
52
53 if (!name) {
54 fprintf(stderr, "dropping: bad question\n");
55 return -1;
56 }
57
58 a = dns_consume_answer(b, rlen);
59 if (!a) {
60 fprintf(stderr, "dropping: bad question\n");
61 return -1;
62 }
63
64 rdata = *b;
65 if (a->rdlength > *rlen) {
66 fprintf(stderr, "dropping: bad question\n");
67 return -1;
68 }
69
70 *rlen -= a->rdlength;
71 *b += a->rdlength;
72
73 if (cache)
74 cache_answer(u, buffer, len, name, a, rdata);
75
76 return 0;
77 }
78
79 static void
80 parse_question(struct uloop_fd *u, char *name, struct dns_question *q)
81 {
82 char *host;
83
84 DBG(1, "Q -> %s %s\n", dns_type_string(q->type), name);
85
86 switch (q->type) {
87 case TYPE_ANY:
88 host = service_name("local");
89 if (!strcmp(name, host))
90 service_reply(u, NULL);
91 break;
92
93 case TYPE_PTR:
94 service_announce_services(u, name);
95 service_reply(u, name);
96 break;
97
98 case TYPE_AAAA:
99 case TYPE_A:
100 host = strstr(name, ".local");
101 if (host)
102 *host = '\0';
103 if (!strcmp(hostname, name))
104 service_reply_a(u, q->type);
105 break;
106 };
107 }
108
109 static void
110 read_socket(struct uloop_fd *u, unsigned int events)
111 {
112 uint8_t buffer[8 * 1024];
113 uint8_t *b = buffer;
114 struct dns_header *h;
115 int len, rlen;
116
117 if (u->eof) {
118 uloop_fd_delete(u);
119 close(u->fd);
120 u->fd = -1;
121 uloop_timeout_set(&reconnect, 1000);
122 return;
123 }
124
125 rlen = len = read(u->fd, buffer, sizeof(buffer));
126 if (len < 1) {
127 fprintf(stderr, "read failed: %s\n", strerror(errno));
128 return;
129 }
130
131 h = dns_consume_header(&b, &rlen);
132 if (!h) {
133 fprintf(stderr, "dropping: bad header\n");
134 return;
135 }
136
137 while (h->questions-- > 0) {
138 char *name = dns_consume_name(buffer, len, &b, &rlen);
139 struct dns_question *q;
140
141 if (!name) {
142 fprintf(stderr, "dropping: bad name\n");
143 return;
144 }
145
146 q = dns_consume_question(&b, &rlen);
147 if (!q) {
148 fprintf(stderr, "dropping: bad question\n");
149 return;
150 }
151
152 if (!(h->flags & FLAG_RESPONSE))
153 parse_question(announce_fd, name, q);
154 }
155
156 if (!(h->flags & FLAG_RESPONSE))
157 return;
158
159 while (h->answers-- > 0)
160 parse_answer(u, buffer, len, &b, &rlen, 1);
161
162 while (h->authority-- > 0)
163 parse_answer(u, buffer, len, &b, &rlen, 0);
164
165 while (h->additional-- > 0)
166 parse_answer(u, buffer, len, &b, &rlen, 1);
167 }
168
169 static void
170 reconnect_socket(struct uloop_timeout *timeout)
171 {
172 cur_iface->fd.fd = usock(USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK, MCAST_ADDR, "5353");
173 if (cur_iface->fd.fd < 0) {
174 fprintf(stderr, "failed to add listener: %s\n", strerror(errno));
175 uloop_timeout_set(&reconnect, 1000);
176 } else {
177 if (interface_socket_setup(cur_iface)) {
178 uloop_timeout_set(&reconnect, 1000);
179 cur_iface->fd.fd = -1;
180 return;
181 }
182
183 uloop_fd_add(&cur_iface->fd, ULOOP_READ);
184 sleep(5);
185 dns_send_question(cur_iface, "_services._dns-sd._udp.local", TYPE_PTR);
186 announce_init(&cur_iface->fd);
187 }
188 }
189
190 int
191 main(int argc, char **argv)
192 {
193 int ch, ttl;
194
195 while ((ch = getopt(argc, argv, "h:t:i:d")) != -1) {
196 switch (ch) {
197 case 'h':
198 hostname = optarg;
199 break;
200 case 't':
201 ttl = atoi(optarg);
202 if (ttl > 0)
203 announce_ttl = ttl;
204 else
205 fprintf(stderr, "invalid ttl\n");
206 break;
207 case 'd':
208 debug++;
209 break;
210 case 'i':
211 iface_name = optarg;
212 break;
213 }
214 }
215
216 if (!iface_name)
217 return -1;
218
219 uloop_init();
220
221 if (interface_add(iface_name)) {
222 fprintf(stderr, "Failed to add interface %s\n", iface_name);
223 return -1;
224 }
225
226 if (!cur_iface)
227 return -1;
228
229 signal_setup();
230
231 if (cache_init())
232 return -1;
233
234 service_init();
235
236 cur_iface->fd.cb = read_socket;
237 reconnect.cb = reconnect_socket;
238
239 uloop_timeout_set(&reconnect, 100);
240 ubus_startup();
241 uloop_run();
242 uloop_done();
243
244 cache_cleanup();
245 service_cleanup();
246
247 return 0;
248 }