use struct interface internally instead of struct uloop_fd
[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 interface *iface, 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(iface, buffer, len, name, a, rdata);
75
76         return 0;
77 }
78
79 static void
80 parse_question(struct interface *iface, 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(iface, NULL);
91                 break;
92
93         case TYPE_PTR:
94                 service_announce_services(iface, name);
95                 service_reply(iface, 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(iface, q->type);
105                 break;
106         };
107 }
108
109 static void
110 read_socket(struct uloop_fd *u, unsigned int events)
111 {
112         struct interface *iface = container_of(u, struct interface, fd);
113         static uint8_t buffer[8 * 1024];
114         uint8_t *b = buffer;
115         struct dns_header *h;
116         int len, rlen;
117
118         if (u->eof) {
119                 uloop_fd_delete(u);
120                 close(u->fd);
121                 u->fd = -1;
122                 uloop_timeout_set(&reconnect, 1000);
123                 return;
124         }
125
126         rlen = len = read(u->fd, buffer, sizeof(buffer));
127         if (len < 1) {
128                 fprintf(stderr, "read failed: %s\n", strerror(errno));
129                 return;
130         }
131
132         h = dns_consume_header(&b, &rlen);
133         if (!h) {
134                 fprintf(stderr, "dropping: bad header\n");
135                 return;
136         }
137
138         while (h->questions-- > 0) {
139                 char *name = dns_consume_name(buffer, len, &b, &rlen);
140                 struct dns_question *q;
141
142                 if (!name) {
143                         fprintf(stderr, "dropping: bad name\n");
144                         return;
145                 }
146
147                 q = dns_consume_question(&b, &rlen);
148                 if (!q) {
149                         fprintf(stderr, "dropping: bad question\n");
150                         return;
151                 }
152
153                 if (!(h->flags & FLAG_RESPONSE))
154                         parse_question(iface, name, q);
155         }
156
157         if (!(h->flags & FLAG_RESPONSE))
158                 return;
159
160         while (h->answers-- > 0)
161                 parse_answer(iface, buffer, len, &b, &rlen, 1);
162
163         while (h->authority-- > 0)
164                 parse_answer(iface, buffer, len, &b, &rlen, 0);
165
166         while (h->additional-- > 0)
167                 parse_answer(iface, buffer, len, &b, &rlen, 1);
168 }
169
170 static void
171 reconnect_socket(struct uloop_timeout *timeout)
172 {
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);
177         } else {
178                 if (interface_socket_setup(cur_iface)) {
179                         uloop_timeout_set(&reconnect, 1000);
180                         cur_iface->fd.fd = -1;
181                         return;
182                 }
183
184                 uloop_fd_add(&cur_iface->fd, ULOOP_READ);
185                 sleep(5);
186                 dns_send_question(cur_iface, "_services._dns-sd._udp.local", TYPE_PTR);
187                 announce_init();
188         }
189 }
190
191 int
192 main(int argc, char **argv)
193 {
194         int ch, ttl;
195
196         while ((ch = getopt(argc, argv, "h:t:i:d")) != -1) {
197                 switch (ch) {
198                 case 'h':
199                         hostname = optarg;
200                         break;
201                 case 't':
202                         ttl = atoi(optarg);
203                         if (ttl > 0)
204                                 announce_ttl = ttl;
205                         else
206                                 fprintf(stderr, "invalid ttl\n");
207                         break;
208                 case 'd':
209                         debug++;
210                         break;
211                 case 'i':
212                         iface_name = optarg;
213                         break;
214                 }
215         }
216
217         if (!iface_name)
218                 return -1;
219
220         uloop_init();
221
222         if (interface_add(iface_name)) {
223                 fprintf(stderr, "Failed to add interface %s\n", iface_name);
224                 return -1;
225         }
226
227         if (!cur_iface)
228                 return -1;
229
230         signal_setup();
231
232         if (cache_init())
233                 return -1;
234
235         service_init();
236
237         cur_iface->fd.cb = read_socket;
238         reconnect.cb = reconnect_socket;
239
240         uloop_timeout_set(&reconnect, 100);
241         ubus_startup();
242         uloop_run();
243         uloop_done();
244
245         cache_cleanup();
246         service_cleanup();
247
248         return 0;
249 }