a6c99b50cad954eb3668ed5028d19311a40b728c
[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
42 static struct uloop_timeout reconnect;
43 char *iface_name = "eth0";
44 const char *iface_ip;
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
173         if (iface_ip)
174                 listener.fd = usock(USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK, MCAST_ADDR, "5353");
175
176         if (!iface_ip || listener.fd < 0) {
177                 fprintf(stderr, "failed to add listener: %s\n", strerror(errno));
178                 uloop_timeout_set(&reconnect, 1000);
179         } else {
180                 if (socket_setup(listener.fd, iface_ip)) {
181                         uloop_timeout_set(&reconnect, 1000);
182                         listener.fd = -1;
183                         return;
184                 }
185
186                 uloop_fd_add(&listener, ULOOP_READ);
187                 sleep(5);
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);
191         }
192 }
193
194 int
195 main(int argc, char **argv)
196 {
197         int ch, ttl;
198
199         while ((ch = getopt(argc, argv, "h:t:i:d")) != -1) {
200                 switch (ch) {
201                 case 'h':
202                         hostname = optarg;
203                         break;
204                 case 't':
205                         ttl = atoi(optarg);
206                         if (ttl > 0)
207                                 announce_ttl = ttl;
208                         else
209                                 fprintf(stderr, "invalid ttl\n");
210                         break;
211                 case 'd':
212                         debug++;
213                         break;
214                 case 'i':
215                         iface_name = optarg;
216                         break;
217                 }
218         }
219
220         if (!iface_name)
221                 return -1;
222
223         iface_ip = get_iface_ipv4(iface_name);
224
225         if (!iface_ip) {
226                 fprintf(stderr, "failed to read ip for %s\n", iface_name);
227                 return -1;
228         }
229         fprintf(stderr, "interface %s has ip %s\n", iface_name, iface_ip);
230         signal_setup();
231
232         if (dns_init())
233                 return -1;
234
235         if (cache_init())
236                 return -1;
237
238         service_init();
239
240         listener.cb = read_socket;
241         reconnect.cb = reconnect_socket;
242
243         uloop_init();
244         uloop_timeout_set(&reconnect, 100);
245         ubus_startup();
246         uloop_run();
247         uloop_done();
248
249         dns_cleanup();
250         cache_cleanup();
251         service_cleanup();
252
253         return 0;
254 }