New revision of the Tunnel6 client with improvements and routed prefix support
[tunnel6.git] / client / src / main.c
blob1dccc24db760c5cf444dd832df18992389e05c19
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 <stdlib.h>
21 #include <unistd.h>
22 #include <stdio.h>
23 #include "sig.h"
24 #include "ipv6.h"
25 #include "poll.h"
26 #include "proto.h"
27 #include "config.h"
28 #include "tunnel.h"
29 #include "tundev.h"
30 #include "defaults.h"
31 #include "heartbeat.h"
32 #ifdef __WIN32__
33 # include <windows.h>
34 #endif
36 int cont; /* Continue in tunnel processing ? 1 - yes / 0 - no */
38 int init (char *addr, unsigned short port, char *name, char *pwd)
40 if (tundev_init () == -1)
41 return -1;
43 if (tunnel_init (addr, port, name, pwd) == -1)
44 return -1;
46 if (ipv6_init () == -1)
47 return -1;
49 if (tundev_setup () == -1)
50 return -1;
52 if (heartbeat_init () == -1)
53 return -1;
55 if (poll_init () == -1)
56 return -1;
58 if (sig_init () == -1)
59 return -1;
61 cont = 1;
63 return 0;
66 int loop ()
68 for (; cont;) {
69 poll_loop ();
71 heartbeat_loop ();
74 return 0;
77 int syntax (char *bin)
79 printf ("Syntax error\n\t%s: name password tunnel6server\n", bin);
81 return -1;
84 int main (int argc, char **argv)
86 printf ("TUNNEL6 client by ZeXx86\n");
88 /* login name */
89 char *name = 0;
90 /* login password */
91 char *pwd = 0;
92 /* tunnel server address */
93 char *server = 0;
94 /* listen port */
95 unsigned short port = DEFAULT_PORT;
97 /* we've found config */
98 if (argc < 4) {
99 if (config_init () == -1) {
100 syntax (argv[0]);
102 return -1;
105 /* set config values */
106 name = config.name;
107 pwd = config.pwd;
108 server = config.server;
110 if (config.port)
111 port = config.port;
112 } else {
113 /* set values from parameters */
114 name = argv[1];
115 pwd = argv[2];
116 server = argv[3];
118 if (argc > 4)
119 port = (unsigned short) atoi (argv[4]);
122 printf ("User name: %s\n"
123 "Server: %s\n"
124 "Port: %u\n", name, server, port);
126 /* Initialize routines */
127 if (init (server, port, name, pwd) == -1)
128 return -1;
130 loop ();
132 printf ("> closing tunnel\n");
134 /* disconnect from server */
135 proto_cmd_disconnect2 ();
137 return 0;