-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.c
275 lines (239 loc) · 6.72 KB
/
main.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* This is the vstt Sourcecode - http://wendzel.de
*
* Copyright (c) 2006,2016 Steffen Wendzel, www.wendzel.de
* - All rights reserved.
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#include "main.h"
/* global connection config */
struct conn_info *ci;
int start_s2f;
int s2f_port;
int s2f_servermode;
enum protocol my_proto;
/* must be equal to enum protocol: */
char *protocols[] = { "icmp", "pop3", "none" };
void
usage(void)
{
extern char *__progname;
fprintf(stderr,
"usage: %s -p proto -r my_port -t peer_port -a peer_addr"
" [-m my_addr] [-c <s2f_port> [-s]]\n", __progname);
fprintf(stderr,
"\nPossible values for [protocol] are:\n"
" * none * icmp * pop3\n"
"\nThe given port values are ignored when using them\n"
"with protocols that doesn't support them (like the\n"
"ICMP protocol)\n\n");
exit(1);
}
void
err_out(char *s)
{
fprintf(stderr, "%s", s);
exit(1);
}
void
run_s2f()
{
char run_str[0xff+1] = { '\0' };
snprintf(run_str, 0xff, S2F_PATH " %s -p %i\n",
(s2f_servermode == 1 ? "-s" : ""),
s2f_port);
system(run_str);
exit(0);
}
int
main(int argc, char *argv[])
{
int ch;
int verbose;
int i;
char *peer_addr, *my_addr;
int my_port, peer_port;
my_proto = P_max;
verbose = 0;
start_s2f = 0;
s2f_servermode = 0;
s2f_port = 0;
peer_addr = my_addr = NULL;
ci = (struct conn_info *) calloc((size_t)1,
sizeof(struct conn_info));
if (!ci)
err(1, "calloc");
my_port = peer_port = 0;
/* setup the internal pointers */
ci->sa_my = (struct sockaddr_in *) &ci->ss_my;
ci->sa6_my = (struct sockaddr_in6 *) &ci->ss_my;
ci->sa_peer = (struct sockaddr_in *) &ci->ss_peer;
ci->sa6_peer = (struct sockaddr_in6 *) &ci->ss_peer;
while ((ch = getopt(argc, argv, "hp:r:t:a:m:c:svV")) != -1) {
switch (ch) {
case 'p':
for (i = 0; i < P_max; i++)
if (strcmp(protocols[i], optarg) == 0)
my_proto = i;
break;
case 'r':
my_port = atoi(optarg);
break;
case 't':
peer_port = atoi(optarg);
break;
case 'c':
s2f_port = atoi(optarg);
start_s2f = 1;
break;
case 'a':
if (!(peer_addr = calloc(strlen(optarg) + 1,
sizeof(char))))
err(1, "calloc");
strncpy(peer_addr, optarg, strlen(optarg));
break;
case 'm':
if (!(my_addr = calloc(strlen(optarg) +1,
sizeof(char))))
err(1, "calloc");
strncpy(my_addr, optarg, strlen(optarg));
break;
case 's':
s2f_servermode = 1;
break;
case 'v':
verbose = 1;
break;
case 'V':
printf("vstt v. " VSTT_VER " (C) 2016 by "
"Steffen Wendzel, www. wendzel.de\n");
exit(0);
/* NOTREACHED */
case 'h':
default:
usage();
/* NOTREACHED */
}
}
/* check s2f parameters */
if (start_s2f) {
FILE *fp;
if (s2f_port <= 0 || s2f_port > 0xffff) {
fprintf(stderr, "s2f port out of range.\n");
exit(1);
}
/* check if s2f is present */
fp = fopen(S2F_PATH, "r");
if (!fp)
err(1, S2F_PATH);
fclose(fp);
}
/* check vstt parameters */
if (my_proto == P_pop3)
printf("please note, that POP3 tunneling is still in "
"alpha quality!\n");
if (my_proto == P_max)
err_out("I miss: '-p proto' or given value isn't "
"supported.\n");
if (my_port <= 0 || my_port > 0xffff)
err_out("I miss: '-r my_port' or port out of range.\n");
if (peer_port <= 0 || peer_port > 0xffff)
err_out("I miss: '-t peer_port' or port out of "
"range.\n");
if (peer_addr == NULL)
err_out("I miss: '-a peer_address'\n");
if (my_addr == NULL)
err_out("I miss: '-m my_address'\n");
/* set addr and check+set addr family */
if (inet_pton(AF_INET, peer_addr,
(struct sockaddr_in *) &ci->sa_peer->sin_addr)) {
ci->family = AF_INET;
ci->sa_peer->sin_family = AF_INET;
ci->sa_my->sin_family = AF_INET;
if (!inet_pton(AF_INET, my_addr,
(struct sockaddr_in *) &ci->sa_my->sin_addr))
err(1, "inet_pton(AF_INET)");
} else if (inet_pton(AF_INET6, peer_addr,
(struct sockaddr_in *) &ci->sa6_peer->sin6_addr)) {
ci->family = AF_INET6;
ci->sa6_peer->sin6_family = AF_INET6;
ci->sa6_my->sin6_family = AF_INET6;
if (!inet_pton(AF_INET6, my_addr,
(struct sockaddr_in *) &ci->sa6_my->sin6_addr))
err(1, "inet_pton(AF_INET6)");
} else {
fprintf(stderr, "I'm unable to use this address. "
"neither inet_pton(AF_INET)\nnor"
"inet_pton(AF_INET6, ...) was successfully.\n");
exit(1);
}
/* set the ports in the sock* structs */
if (ci->family == AF_INET) {
ci->sa_my->sin_port = htons(my_port);
ci->sa_peer->sin_port = htons(peer_port);
} else {
ci->sa6_my->sin6_port = htons(my_port);
ci->sa6_peer->sin6_port = htons(peer_port);
}
/* check the protocol support! */
if (ci->family == AF_INET6) {
if (my_proto == P_icmp) {
fprintf(stderr,
"ICMP is only support for IPv4. sry\n"
"The plan is to change this within\n"
"one of the next releases.\n");
exit(1);
}
}
if (verbose) {
printf("vstt starting ... this is our config:\n");
printf("protocol : %s\n", protocols[my_proto]);
printf("my recv port : %i\n", my_port);
printf("peer port : %i\n", peer_port);
printf("peer address : %s\n", peer_addr);
printf("proto family : %s\n", (ci->family == AF_INET ?
"IPv4" : "IPv6"));
}
/* check if the proto is already implemented and initialize the
* socket base system
*/
switch (my_proto) {
case P_none:
init_none();
break;
case P_pop3:
init_pop3();
break;
case P_icmp:
//init_icmp();
break;
case P_max:
default:
printf("TODO: protocol not implemented\n");
exit(1);
}
fork_childs();
return 0;
}