util: constify memdup()
[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 int iface_index;
46
47 static int
48 parse_answer(struct uloop_fd *u, uint8_t *buffer, int len, uint8_t **b, int *rlen, int cache)
49 {
50         char *name = dns_consume_name(buffer, len, b, rlen);
51         struct dns_answer *a;
52         uint8_t *rdata;
53
54         if (!name) {
55                 fprintf(stderr, "dropping: bad question\n");
56                 return -1;
57         }
58
59         a = dns_consume_answer(b, rlen);
60         if (!a) {
61                 fprintf(stderr, "dropping: bad question\n");
62                 return -1;
63         }
64
65         rdata = *b;
66         if (a->rdlength > *rlen) {
67                 fprintf(stderr, "dropping: bad question\n");
68                 return -1;
69         }
70
71         *rlen -= a->rdlength;
72         *b += a->rdlength;
73
74         if (cache)
75                 cache_answer(u, buffer, len, name, a, rdata);
76
77         return 0;
78 }
79
80 static void
81 parse_question(struct uloop_fd *u, char *name, struct dns_question *q)
82 {
83         char *host;
84
85         DBG(1, "Q -> %s %s\n", dns_type_string(q->type), name);
86
87         switch (q->type) {
88         case TYPE_ANY:
89                 host = service_name("local");
90                 if (!strcmp(name, host))
91                         service_reply(u, NULL);
92                 break;
93
94         case TYPE_PTR:
95                 service_announce_services(u, name);
96                 service_reply(u, name);
97                 break;
98
99         case TYPE_AAAA:
100         case TYPE_A:
101                 host = strstr(name, ".local");
102                 if (host)
103                         *host = '\0';
104                 if (!strcmp(hostname, name))
105                         service_reply_a(u, q->type);
106                 break;
107         };
108 }
109
110 static void
111 read_socket(struct uloop_fd *u, unsigned int events)
112 {
113         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(announce_fd, name, q);
155         }
156
157         if (!(h->flags & FLAG_RESPONSE))
158                 return;
159
160         while (h->answers-- > 0)
161                 parse_answer(u, buffer, len, &b, &rlen, 1);
162
163         while (h->authority-- > 0)
164                 parse_answer(u, buffer, len, &b, &rlen, 0);
165
166         while (h->additional-- > 0)
167                 parse_answer(u, buffer, len, &b, &rlen, 1);
168 }
169
170 static void
171 reconnect_socket(struct uloop_timeout *timeout)
172 {
173
174         if (iface_ip)
175                 listener.fd = usock(USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK, MCAST_ADDR, "5353");
176
177         if (!iface_ip || listener.fd < 0) {
178                 fprintf(stderr, "failed to add listener: %s\n", strerror(errno));
179                 uloop_timeout_set(&reconnect, 1000);
180         } else {
181                 if (socket_setup(listener.fd, iface_ip)) {
182                         uloop_timeout_set(&reconnect, 1000);
183                         listener.fd = -1;
184                         return;
185                 }
186
187                 uloop_fd_add(&listener, ULOOP_READ);
188                 sleep(5);
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
230         iface_index = get_iface_index(iface_name);
231
232         if (!iface_index) {
233                 fprintf(stderr, "failed to read index for %s\n", iface_name);
234                 return -1;
235         }
236
237         fprintf(stderr, "interface %s has ip %s and index %d\n", iface_name, iface_ip, iface_index);
238         signal_setup();
239
240         if (cache_init())
241                 return -1;
242
243         service_init();
244
245         listener.cb = read_socket;
246         reconnect.cb = reconnect_socket;
247
248         uloop_init();
249         uloop_timeout_set(&reconnect, 100);
250         ubus_startup();
251         uloop_run();
252         uloop_done();
253
254         cache_cleanup();
255         service_cleanup();
256
257         return 0;
258 }