store ip address as struct in_addr instead of a string
[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/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 char *iface_name = "eth0";
42
43 int
44 main(int argc, char **argv)
45 {
46         int ch, ttl;
47
48         while ((ch = getopt(argc, argv, "h:t:i:d")) != -1) {
49                 switch (ch) {
50                 case 'h':
51                         hostname = optarg;
52                         break;
53                 case 't':
54                         ttl = atoi(optarg);
55                         if (ttl > 0)
56                                 announce_ttl = ttl;
57                         else
58                                 fprintf(stderr, "invalid ttl\n");
59                         break;
60                 case 'd':
61                         debug++;
62                         break;
63                 case 'i':
64                         iface_name = optarg;
65                         break;
66                 }
67         }
68
69         if (!iface_name)
70                 return -1;
71
72         uloop_init();
73
74         if (interface_add(iface_name)) {
75                 fprintf(stderr, "Failed to add interface %s\n", iface_name);
76                 return -1;
77         }
78
79         signal_setup();
80
81         if (cache_init())
82                 return -1;
83
84         service_init();
85
86         ubus_startup();
87         uloop_run();
88         uloop_done();
89
90         cache_cleanup();
91         service_cleanup();
92         vlist_flush(&interfaces);
93
94         return 0;
95 }