-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtinytun.cpp
More file actions
141 lines (119 loc) · 3.13 KB
/
tinytun.cpp
File metadata and controls
141 lines (119 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
#include <getopt.h>
#include "crypt.h"
#include "server.h"
#include "client.h"
void usage(void)
{
fprintf(stderr, "Available options\n");
fprintf(stderr, " -k / --key KEY Specify connection key (4..16 chars)\n");
fprintf(stderr, " -s / --server PORT Run as server and listen on specified port\n");
fprintf(stderr, " -c / --client HOST:PORT Run as client and connect to specified HOST:PORT\n");
fprintf(stderr, " -t / --timeout t Set keepalive timeout (5..60 sec, client only)\n");
fprintf(stderr, " -d / --dev DEV Use specified networking interface name\n");
fprintf(stderr, " client's default is tap%%d\n");
fprintf(stderr, " server's default is none (just route packets without netif)\n");
exit(-1);
}
int main(int argc, char **argv)
{
int server_port=0;
char *client_host_port=0;
const char *key_str=0;
const char *dev=0;
int keepalive=60;
// Parsing command line options
struct option opts[]=
{
{ "server", required_argument, 0, 's' },
{ "client", required_argument, 0, 'c' },
{ "key", required_argument, 0, 'k' },
{ "dev", required_argument, 0, 'd' },
{ "timeout", required_argument, 0, 't' },
{ 0 }
};
int opt;
while ( (opt=getopt_long(argc, argv, "s:c:k:d:t:", opts, 0)) > 0)
{
switch (opt)
{
case 's':
server_port=atoi(optarg);
break;
case 'c':
client_host_port=strdup(optarg);
break;
case 'k':
key_str=strdup(optarg);
if (strlen(key_str) < 4)
{
fprintf(stderr, "Error: key must be at least 4 characters\n");
return -1;
}
// Hiding password for ps/top/etc
{
char *s=optarg;
while (*s)
(*s++)=0;
}
break;
case 'd':
dev=optarg;
break;
case 't':
if ( (sscanf(optarg, "%d", &keepalive)!=1) ||
(keepalive < 5) ||
(keepalive > 60) )
{
fprintf(stderr, "Error: incorrect timeout value\n");
}
break;
case '?':
default:
// Bad option
usage();
}
}
// Checking arguments
if ( (optind < argc) ||
(! key_str) ||
( (server_port==0) && (!client_host_port) ) ||
( (server_port>0) && (client_host_port) ) )
usage();
// Seeding random
srand(time(NULL));
// Making a key from password
makeKey128(key_str);
// Starting server
if (server_port > 0)
{
if (! start_server(dev, server_port)) return -1;
}
// Starting client
if (client_host_port)
{
const char *host=client_host_port;
int port=0;
char *sport=strchr(client_host_port, ':');
if ( (! *sport) || (sscanf(sport+1, "%d", &port)!=1) || (port < 1) || (port > 65535) )
{
fprintf(stderr, "Error: incorrect client port\n");
return -1;
}
(*sport)=0; // ':' -> nul
if (! *host)
{
// No host
fprintf(stderr, "Error: no host specified\n");
return -1;
}
if (! start_client(dev, host, port, keepalive)) return -1;
}
// Everything is ok
return 0;
}