-
Notifications
You must be signed in to change notification settings - Fork 13
/
args.c
146 lines (127 loc) · 3.73 KB
/
args.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
#include "args.h"
#include <argp.h>
#include <stdlib.h>
#include "defines.h"
const char *argp_program_version = "udp-tunnel-" VERSION_STR;
const char *argp_program_bug_address = "<[email protected]>";
static char doc[] = " \
creates a reverse UDP tunnel for an UDP srvice behind NAT\n\n \
Example usage on the inside:\n \
udp-tunnel -s wireguard.fritz.box:1234 -o jump.example.com:9999\n\n \
Example usage on the outside:\n \
udp-tunnel -l 9999";
static char args_doc[] = "";
static struct argp_option options[] = {
{
.group = 1,
.doc = "Options for running it as the inside agent:"
},
{
.name = "outside",
.arg = "host:port",
.key = 'o',
.group = 1,
.doc = "address of the outside agent"
},
{
.name = "service",
.arg = "host:port",
.key = 's',
.group = 1,
.doc = "address of the inside service"
},
{
.group = 2,
.doc = "Options for running it as the outside agent:"
},
{
.name = "listen",
.arg = "port",
.key = 'l',
.group = 2,
.doc = "listen port"
},
{
.group = 3,
.doc = "General options:"
},
{
.name = "key",
.arg = "string",
.key = 'k',
.group = 3,
.doc = "optional shared password to prevent spoofing"
},
{
.name = "keepalive",
.arg = "seconds",
.key = 't',
.group = 3,
.doc = "keepalive interval in seconds (default 25, must be the same on boths sides)"
},
{0}
};
static error_t parse_opt(int key, char *arg, struct argp_state *state){
args_parsed_t* parsed = state->input;
switch(key){
case 'l':
parsed->listenport = strtoul(arg, NULL, 10);
break;
case 's':
parsed->service = arg;
sscanf(arg, "%m[^:]:%d", &parsed->service_host, &parsed->service_port);
break;
case 'o':
parsed->outside = arg;
sscanf(arg, "%m[^:]:%d", &parsed->outside_host, &parsed->outside_port);
break;
case 'k':
parsed->secret = arg;
break;
default:
return ARGP_ERR_UNKNOWN;
case 't':
parsed->keepalive = strtoul(arg, NULL, 10);
break;
}
return 0;
}
static struct argp argp = {
.options = options,
.parser = parse_opt,
.args_doc = args_doc,
.doc = doc
};
static void error(char* s) {
fprintf(stderr, "\nusage error:\n%s\n\n", s);
argp_help(&argp, stderr, ARGP_HELP_DOC, NULL);
exit(1);
}
args_parsed_t args_parse(int argc, char* args[]) {
args_parsed_t parsed;
parsed.listenport = 0;
parsed.service = NULL;
parsed.outside = NULL;
parsed.secret = NULL;
parsed.keepalive = 25;
argp_parse(&argp, argc, args, 0, 0, &parsed);
if ((parsed.listenport > 0) && (parsed.outside != NULL)) {
error("--listen and --outside are mutually exclusive");
}
if ((parsed.listenport > 0) && (parsed.service != NULL)) {
error("--listen and --service are mutually exclusive");
}
if (((parsed.service != NULL) && (parsed.outside == 0)) || ((parsed.service == NULL) && (parsed.outside != 0))) {
error("--service and --outside must both be specified");
}
if ((parsed.listenport == 0) && (parsed.outside == NULL) && (parsed.service == 0)) {
error("too few options");
}
if (parsed.service && (parsed.service_port == 0)) {
error("something is wrong with the service address, use host:port syntax");
}
if (parsed.outside && (parsed.outside_port == 0)) {
error("something is wrong with the outside address, use host:port syntax");
}
return parsed;
}