forked from liudf0716/xkcptun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commandline.c
248 lines (213 loc) · 7.22 KB
/
commandline.c
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/********************************************************************\
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 2 of *
* the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License*
* along with this program; if not, contact: *
* *
* Free Software Foundation Voice: +1-617-542-5942 *
* 59 Temple Place - Suite 330 Fax: +1-617-542-2652 *
* Boston, MA 02111-1307, USA [email protected] *
* *
\********************************************************************/
/** @file commandline.c
@brief Command line argument handling
@author Copyright (C) 2004 Philippe April <[email protected]>
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <syslog.h>
#include <getopt.h>
#include "xkcp_config.h"
#include "commandline.h"
#include "debug.h"
#include "version.h"
static struct option long_options[] = {
{ "key", required_argument, NULL, GETOPT_VAL_KEY},
{ "crypt", required_argument, NULL, GETOPT_VAL_CRYPT},
{ "mode", required_argument, NULL, GETOPT_VAL_MODE},
{ "mtu", required_argument, NULL, GETOPT_VAL_MTU},
{ "sndwnd", required_argument, NULL, GETOPT_VAL_SNDWND},
{ "rcvwnd", required_argument, NULL, GETOPT_VAL_RCVWND},
{ "dshard", required_argument, NULL, GETOPT_VAL_DATASHARD},
{ "pshard", required_argument, NULL, GETOPT_VAL_PARITYSHARD},
{ "dscp", required_argument, NULL, GETOPT_VAL_DSCP},
{ "nocomp", no_argument, NULL, GETOPT_VAL_NOCOMP},
{ "acknodelay", no_argument, NULL, GETOPT_VAL_ACKNODELAY},
{ "nodelay", no_argument, NULL, GETOPT_VAL_NODELAY},
{ "interval", required_argument, NULL, GETOPT_VAL_INTERVAL},
{ "resend", required_argument, NULL, GETOPT_VAL_RESEND},
{ "nc", required_argument, NULL, GETOPT_VAL_NC},
{ "sockbuf", required_argument, NULL, GETOPT_VAL_SOCKBUF},
{ "keepalive", required_argument, NULL, GETOPT_VAL_KEEPALIVE},
{ "syslog", no_argument, NULL, GETOPT_VAL_SYSLOG},
{ "help", no_argument, NULL, GETOPT_VAL_HELP},
{ "version", no_argument, NULL, GETOPT_VAL_VERSION},
{ NULL, 0, NULL, 0 }
};
/** @internal
* @brief Print usage
*
* Prints usage
*/
void
usage(const char *appname)
{
fprintf(stdout, "Usage: %s [options]\n", appname);
fprintf(stdout, "\n");
fprintf(stdout, "options:\n");
fprintf(stdout, " -c <filename> Use this config file\n");
fprintf(stdout, " -f Run in foreground\n");
fprintf(stdout, " -h --help Print usage\n");
fprintf(stdout, " -v --version Print version information\n");
fprintf(stdout, " -d <level> Debug level\n");
fprintf(stdout, " --syslog Log to syslog\n\n");
fprintf(stdout, " -i <interface> Interface to use (default br-lan)\n");
fprintf(stdout, " -l <port> Port number of your local server (default: 9088)\n");
fprintf(stdout, " -s <host> Host name or IP address of your remote server \n");
fprintf(stdout, " -p <port> Port number of your remote server (default: 9089)\n");
fprintf(stdout, " -k <string> Pre-shared secret between client and server\n");
fprintf(stdout, " -e <string> Encrypt method: none\n");
fprintf(stdout, " -m <string> Profiles: fast3, fast2, fast, normal (default: \"fast\")\n");
fprintf(stdout, " -M --mtu <num> MTU of your network interface\n");
fprintf(stdout, " -S --sndwnd <num> Send window size(num of packets) (default: 512)\n");
fprintf(stdout, " -R --rcvwnd <num> Receive window size(num of packets) (default: 512)\n");
fprintf(stdout, " -D --dshard <num> Reed-solomon erasure coding - datashard (default: 10)\n");
fprintf(stdout, " -P --pshard <num> Reed-solomon erasure coding - parityshard (default: 3)\n");
fprintf(stdout, " -N --nocomp Disable compression\n");
fprintf(stdout, " -A --acknodelay Ack no delay\n");
fprintf(stdout, " -L --nodelay No delay\n");
fprintf(stdout, " -T --interval <num>\n");
fprintf(stdout, " -K --keepalive <num>\n");
fprintf(stdout, "\n");
}
/** Uses getopt() to parse the command line and set configuration values
* also populates restartargv
*/
void
parse_commandline(int argc, char **argv)
{
int c;
struct xkcp_config *config = xkcp_get_config();
struct xkcp_param *param = &config->param;
while (-1 != (c = getopt_long(argc, argv, "Ac:D:d:e:fhi:K:k:L:l:M:m:NP:p:R:S:s:T:v",
long_options, NULL)))
switch (c) {
case GETOPT_VAL_HELP:
case 'h':
usage(argv[0]);
exit(1);
break;
case 'c':
if (optarg) {
free(config->config_file);
config->config_file = strdup(optarg);
}
break;
case 'f':
config->daemon = 0;
debugconf.log_stderr = 1;
break;
case 'd':
if (optarg) {
debugconf.debuglevel = atoi(optarg);
}
break;
case 'i':
free(param->local_interface);
param->local_interface = strdup(optarg);
break;
case 'l':
param->local_port = atoi(optarg);
break;
case 's':
free(param->remote_addr);
param->remote_addr = strdup(optarg);
break;
case 'p':
param->remote_port = atoi(optarg);
break;
case 'k':
free(param->key);
param->key = strdup(optarg);
break;
case 'e':
free(param->crypt);
param->crypt = strdup(optarg);
break;
case 'm':
free(param->mode);
param->mode = strdup(optarg);
break;
case GETOPT_VAL_MTU:
case 'M':
param->mtu= atoi(optarg);
break;
case GETOPT_VAL_SNDWND:
case 'S':
param->sndwnd= atoi(optarg);
break;
case GETOPT_VAL_RCVWND:
case 'R':
param->rcvwnd= atoi(optarg);
break;
case GETOPT_VAL_DATASHARD:
case 'D':
param->data_shard= atoi(optarg);
break;
case GETOPT_VAL_PARITYSHARD:
case 'P':
param->parity_shard= atoi(optarg);
break;
case GETOPT_VAL_NOCOMP:
case 'N':
param->nocomp = 1;
break;
case GETOPT_VAL_ACKNODELAY:
case 'A':
param->ack_nodelay = 1;
break;
case GETOPT_VAL_NODELAY:
case 'L':
param->nodelay = 1;
break;
case GETOPT_VAL_INTERVAL:
param->interval = atoi(optarg);
break;
case GETOPT_VAL_KEEPALIVE:
param->keepalive = atoi(optarg);
break;
case GETOPT_VAL_SYSLOG:
debugconf.log_syslog = 1;
debugconf.log_stderr = 0;
break;
case GETOPT_VAL_VERSION:
case 'v':
fprintf(stdout, "This is %s version " VERSION "\n", argv[0]);
exit(1);
break;
default:
usage(argv[0]);
exit(1);
break;
}
if (NULL != config->config_file)
if (xkcp_parse_param(config->config_file)) {
debug(LOG_ERR, "xkcp_parse_param failed \n");
usage(argv[0]);
exit(0);
}
if (!param->remote_addr) {
usage(argv[0]);
exit(0);
}
}