Version 1.1 - server : automatic creation of DB; some improvements
[tunnel6.git] / server / src / main.c
blob4b7c3b5f9bddf4ac3465bfadeda7d0f2a777bac5
1 /*
2 * tunnel6
3 * Copyright (C) 2010 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <getopt.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include "db.h"
25 #include "poll.h"
26 #include "sniff.h"
27 #include "admin.h"
28 #include "config.h"
29 #include "tunnel.h"
30 #include "packet.h"
31 #include "signal.h"
32 #include "daemon.h"
33 #include "defaults.h"
34 #include "heartbeat.h"
36 int cont;
38 int init ()
40 if (heartbeat_init () == -1)
41 return -1;
43 if (db_init () == -1)
44 return -1;
46 if (packet_init () == -1)
47 return -1;
49 if (tunnel_init (config.port) == -1)
50 return -1;
52 if (sniff_init (config.interface, config.ipv6) == -1)
53 return -1;
55 if (poll_init () == -1)
56 return -1;
58 if (signal_init () == -1)
59 return -1;
61 cont = 1;
63 return 0;
66 int loop ()
68 for (; cont;) {
69 if (db_reload () == -1)
70 return -1;
72 if (poll_loop () == -1)
73 return -1;
75 if (heartbeat_loop () == -1)
76 return -1;
79 return 0;
82 void usage ()
84 printf ("TUNNEL6 server\nUsage:\n"
85 "\t-h : help\n"
86 "\t-v : version\n"
87 "\t-d : start as daemon\n"
88 "\t-f pidfile : specify pid file\n"
89 "\t-p port : specify listening port\n"
90 "\t-i interface : specify interface\n"
91 "\t-l stdout : remap stdout to specified file\n"
92 "\t-a ipv6 : specify listening address\n"
93 "\t-A name password ipv6 [routedprefix/len] : add DB record\n"
94 "\t-U name p;i;r value : update DB record\n"
95 "\t-G name : show DB record\n");
98 void parseargs (int argc, char **argv)
100 int opt;
102 while ((opt = getopt (argc, argv, "hvdp:f:i:l:a:A:U:G:")) > 0) {
103 switch (opt) {
104 case '?':
105 case 'h':
106 usage ();
107 exit (0);
108 case 'v':
109 printf ("TUNNEL6 server - version " DEFAULT_VERSION "\n");
110 exit (0);
111 case 'd':
112 config.daemon = 1;
113 break;
114 case 'p':
115 config.port = (unsigned short) atoi (optarg);
116 break;
117 case 'f':
118 config.pidfile = strdup (optarg);
119 break;
120 case 'i':
121 config.interface = strdup (optarg);
122 break;
123 case 'l':
124 stdout = fopen (optarg, "a+");
126 if (!stdout) {
127 perror ("> ERROR -> Log file");
128 exit (-1);
131 break;
132 case 'a':
133 config.ipv6 = strdup (optarg);
134 break;
135 case 'A':
136 exit (admin_db_add (argc, argv));
137 case 'U':
138 exit (admin_db_update (argc, argv));
139 case 'G':
140 exit (admin_db_get (argc, argv));
145 int main (int argc, char **argv)
147 /* not an daemon by default */
148 config.daemon = 0;
149 /* set default pid file */
150 config.pidfile = DEFAULT_PIDFILE;
151 /* network interface */
152 config.interface = DEFAULT_ETHDEV;
153 /* listen port */
154 config.port = DEFAULT_PORT;
155 /* set automatic ipv6 selection */
156 config.ipv6 = 0;
157 /* set default backend */
158 config.backend = 0;
160 config_init ();
162 parseargs (argc, argv);
164 /* unbuffer stdout */
165 setvbuf (stdout, (char *) NULL, _IOLBF, 0);
167 printf ("TUNNEL6 server v%s by ZeXx86\n", DEFAULT_VERSION);
169 if (config.daemon)
170 daemonize (config.pidfile);
172 printf ("Device: %s\n"
173 "Port: %u\n", config.interface, config.port);
175 if (init () == -1)
176 return -1;
178 loop ();
180 printf ("> tunnel closed\n");
182 db_disconnect_all ();
184 return 0;