Fix/change comment format about port 5353
[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 <libubus.h>
30 #include <libubox/uloop.h>
31
32 #include "dns.h"
33 #include "ubus.h"
34 #include "util.h"
35 #include "cache.h"
36 #include "service.h"
37 #include "announce.h"
38 #include "interface.h"
39
40 int cfg_proto = 0;
41 int cfg_no_subnet = 0;
42
43 static void
44 signal_shutdown(int signal)
45 {
46         uloop_end();
47 }
48
49 int
50 main(int argc, char **argv)
51 {
52         int ch, ttl;
53
54         uloop_init();
55
56         while ((ch = getopt(argc, argv, "t:i:d46n")) != -1) {
57                 switch (ch) {
58                 case 't':
59                         ttl = atoi(optarg);
60                         if (ttl > 0)
61                                 announce_ttl = ttl;
62                         else
63                                 fprintf(stderr, "invalid ttl\n");
64                         break;
65                 case 'd':
66                         debug++;
67                         break;
68                 case 'i':
69                         interface_add(optarg);
70                         break;
71                 case '4':
72                         cfg_proto = 4;
73                         break;
74                 case '6':
75                         cfg_proto = 6;
76                         break;
77                 case 'n':
78                         cfg_no_subnet = 1;
79                         break;
80
81                 default:
82                         return -1;
83                 }
84         }
85
86         signal(SIGPIPE, SIG_IGN);
87         signal(SIGTERM, signal_shutdown);
88         signal(SIGKILL, signal_shutdown);
89
90         if (cache_init())
91                 return -1;
92
93         ubus_startup();
94
95         service_init(0);
96
97         uloop_run();
98         uloop_done();
99
100         interface_shutdown();
101         cache_cleanup(NULL);
102         service_cleanup();
103         vlist_flush(&interfaces);
104
105         return 0;
106 }