752e78adf06589948d14f87898a913da931cc91a
[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
29 #include <udebug.h>
30 #include <libubus.h>
31 #include <libubox/uloop.h>
32
33 #include "dns.h"
34 #include "ubus.h"
35 #include "util.h"
36 #include "cache.h"
37 #include "service.h"
38 #include "announce.h"
39 #include "interface.h"
40
41 int cfg_proto = 0;
42 int cfg_no_subnet = 0;
43
44 static struct udebug ud;
45 static struct udebug_buf udb;
46 static bool udebug_enabled;
47
48 static void
49 umdns_udebug_vprintf(const char *format, va_list ap)
50 {
51 if (!udebug_enabled)
52 return;
53
54 udebug_entry_init(&udb);
55 udebug_entry_vprintf(&udb, format, ap);
56 udebug_entry_add(&udb);
57 }
58
59 void umdns_udebug_printf(const char *format, ...)
60 {
61 va_list ap;
62
63 va_start(ap, format);
64 umdns_udebug_vprintf(format, ap);
65 va_end(ap);
66 }
67
68 void umdns_udebug_set_enabled(bool val)
69 {
70 static const struct udebug_buf_meta meta = {
71 .name = "umdns_log",
72 .format = UDEBUG_FORMAT_STRING,
73 };
74
75 if (udebug_enabled == val)
76 return;
77
78 udebug_enabled = val;
79 if (!val) {
80 udebug_buf_free(&udb);
81 udebug_free(&ud);
82 return;
83 }
84
85 udebug_init(&ud);
86 udebug_auto_connect(&ud, NULL);
87 udebug_buf_init(&udb, 1024, 64 * 1024);
88 udebug_buf_add(&ud, &udb, &meta);
89 }
90
91 static void
92 signal_shutdown(int signal)
93 {
94 uloop_end();
95 }
96
97 int
98 main(int argc, char **argv)
99 {
100 int ch, ttl;
101
102 uloop_init();
103
104 while ((ch = getopt(argc, argv, "t:i:d46n")) != -1) {
105 switch (ch) {
106 case 't':
107 ttl = atoi(optarg);
108 if (ttl > 0)
109 announce_ttl = ttl;
110 else
111 fprintf(stderr, "invalid ttl\n");
112 break;
113 case 'd':
114 debug++;
115 break;
116 case 'i':
117 interface_add(optarg);
118 break;
119 case '4':
120 cfg_proto = 4;
121 break;
122 case '6':
123 cfg_proto = 6;
124 break;
125 case 'n':
126 cfg_no_subnet = 1;
127 break;
128
129 default:
130 return -1;
131 }
132 }
133
134 signal(SIGPIPE, SIG_IGN);
135 signal(SIGTERM, signal_shutdown);
136 signal(SIGKILL, signal_shutdown);
137
138 if (cache_init())
139 return -1;
140
141 ubus_startup();
142
143 service_init(0);
144
145 uloop_run();
146 uloop_done();
147
148 interface_shutdown();
149 cache_cleanup(NULL);
150 service_cleanup();
151 vlist_flush(&interfaces);
152
153 return 0;
154 }