move dns packet processing to dns.c
[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 void
47 read_socket(struct uloop_fd *u, unsigned int events)
48 {
49 struct interface *iface = container_of(u, struct interface, fd);
50 static uint8_t buffer[8 * 1024];
51 int len;
52
53 if (u->eof) {
54 uloop_fd_delete(u);
55 close(u->fd);
56 u->fd = -1;
57 uloop_timeout_set(&reconnect, 1000);
58 return;
59 }
60
61 len = read(u->fd, buffer, sizeof(buffer));
62 if (len < 1) {
63 fprintf(stderr, "read failed: %s\n", strerror(errno));
64 return;
65 }
66
67 dns_handle_packet(iface, buffer, len);
68 }
69
70 static void
71 reconnect_socket(struct uloop_timeout *timeout)
72 {
73 cur_iface->fd.fd = usock(USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK, MCAST_ADDR, "5353");
74 if (cur_iface->fd.fd < 0) {
75 fprintf(stderr, "failed to add listener: %s\n", strerror(errno));
76 uloop_timeout_set(&reconnect, 1000);
77 } else {
78 if (interface_socket_setup(cur_iface)) {
79 uloop_timeout_set(&reconnect, 1000);
80 cur_iface->fd.fd = -1;
81 return;
82 }
83
84 uloop_fd_add(&cur_iface->fd, ULOOP_READ);
85 sleep(5);
86 dns_send_question(cur_iface, "_services._dns-sd._udp.local", TYPE_PTR);
87 announce_init(cur_iface);
88 }
89 }
90
91 int
92 main(int argc, char **argv)
93 {
94 int ch, ttl;
95
96 while ((ch = getopt(argc, argv, "h:t:i:d")) != -1) {
97 switch (ch) {
98 case 'h':
99 hostname = optarg;
100 break;
101 case 't':
102 ttl = atoi(optarg);
103 if (ttl > 0)
104 announce_ttl = ttl;
105 else
106 fprintf(stderr, "invalid ttl\n");
107 break;
108 case 'd':
109 debug++;
110 break;
111 case 'i':
112 iface_name = optarg;
113 break;
114 }
115 }
116
117 if (!iface_name)
118 return -1;
119
120 uloop_init();
121
122 if (interface_add(iface_name)) {
123 fprintf(stderr, "Failed to add interface %s\n", iface_name);
124 return -1;
125 }
126
127 if (!cur_iface)
128 return -1;
129
130 signal_setup();
131
132 if (cache_init())
133 return -1;
134
135 service_init();
136
137 cur_iface->fd.cb = read_socket;
138 reconnect.cb = reconnect_socket;
139
140 uloop_timeout_set(&reconnect, 100);
141 ubus_startup();
142 uloop_run();
143 uloop_done();
144
145 cache_cleanup();
146 service_cleanup();
147
148 return 0;
149 }