|
|
24937f |
|
|
|
24937f |
#include <cstdio></cstdio>
|
|
|
24937f |
#include <cerrno></cerrno>
|
|
|
24937f |
#include <cstring></cstring>
|
|
|
24937f |
|
|
|
24937f |
#include "tunnel.h"
|
|
|
24937f |
|
|
|
24937f |
|
|
|
24937f |
bool parseAddress(const char *str, Address &address) {
|
|
|
24937f |
address = Address();
|
|
|
24937f |
sscanf( str, "%hhu.%hhu.%hhu.%hhu:%hu",
|
|
|
24937f |
&address.ip[0], &address.ip[1], &address.ip[2], &address.ip[3],
|
|
|
24937f |
&address.port );
|
|
|
24937f |
if (!address.port) {
|
|
|
24937f |
printf("Wrong ip-address: [%s]\n", str);
|
|
|
24937f |
return false;
|
|
|
24937f |
}
|
|
|
24937f |
return true;
|
|
|
24937f |
}
|
|
|
24937f |
|
|
|
24937f |
|
|
|
24937f |
int main(int argc, char **argv) {
|
|
|
24937f |
|
|
|
24937f |
// parse args
|
|
|
24937f |
|
|
|
1d16df |
unsigned long long bytesPerSecond = 0;
|
|
|
24937f |
Address localUdp, remoteUdp, localTcp, remoteTcp;
|
|
|
1d16df |
if (argc == 6) {
|
|
|
1d16df |
sscanf(argv[2], "%llu", &bytesPerSecond);
|
|
|
1d16df |
if (!bytesPerSecond)
|
|
|
1d16df |
{ printf("Wrong bytes per second: %llu\n", bytesPerSecond); return 1; }
|
|
|
1d16df |
parseAddress(argv[3], localUdp);
|
|
|
1d16df |
parseAddress(argv[4], remoteUdp);
|
|
|
1d16df |
parseAddress(argv[5], localTcp);
|
|
|
24937f |
if (!localUdp.port || !remoteUdp.port || !localTcp.port) return 1;
|
|
|
24937f |
} else
|
|
|
1d16df |
if (argc == 5) {
|
|
|
1d16df |
sscanf(argv[2], "%llu", &bytesPerSecond);
|
|
|
1d16df |
if (!bytesPerSecond)
|
|
|
1d16df |
{ printf("Wrong bytes per second: %llu\n", bytesPerSecond); return 1; }
|
|
|
1d16df |
parseAddress(argv[3], localUdp);
|
|
|
1d16df |
parseAddress(argv[4], remoteTcp);
|
|
|
24937f |
if (!localUdp.port || !remoteTcp.port) return 1;
|
|
|
24937f |
} else {
|
|
|
1d16df |
printf("Usage: %s keyfile bytesPerSecond localUdp:port remoteUdp:port localTcp:port\n", argv[0]);
|
|
|
1d16df |
printf(" or: %s keyfile bytesPerSecond localUdp:port remoteTcp:port\n", argv[0]);
|
|
|
24937f |
return 1;
|
|
|
24937f |
}
|
|
|
24937f |
|
|
|
24937f |
// load key
|
|
|
24937f |
|
|
|
24937f |
FILE *f = fopen(argv[1], "r");
|
|
|
24937f |
if (!f) {
|
|
|
24937f |
printf("Cannot open keyfile: [%s]\n", argv[1]);
|
|
|
24937f |
return 1;
|
|
|
24937f |
}
|
|
|
24937f |
int maxSize = 1024*1024;
|
|
|
24937f |
char *key = (char*)calloc(1, maxSize);
|
|
|
24937f |
char *c = key, *e = c + maxSize - 1;
|
|
|
24937f |
int i = 0;
|
|
|
24937f |
while((i = fgetc(f)) > 0 && c < e) *c++ = i;
|
|
|
24937f |
if (i != EOF || errno) {
|
|
|
24937f |
printf("Cannot read keyfile: [%s]\n", argv[1]);
|
|
|
24937f |
memset(key, 0, maxSize);
|
|
|
24937f |
free(key);
|
|
|
24937f |
fclose(f);
|
|
|
24937f |
}
|
|
|
24937f |
fclose(f);
|
|
|
24937f |
|
|
|
24937f |
// init tunnel
|
|
|
24937f |
|
|
|
24937f |
Tunnel tunnel;
|
|
|
24937f |
|
|
|
1d16df |
tunnel.initSpeed(bytesPerSecond);
|
|
|
1d16df |
|
|
|
24937f |
if ( !tunnel.initUdpServer(localUdp, remoteTcp, key) ) {
|
|
|
24937f |
memset(key, 0, maxSize);
|
|
|
24937f |
free(key);
|
|
|
24937f |
return 1;
|
|
|
24937f |
}
|
|
|
24937f |
memset(key, 0, maxSize);
|
|
|
24937f |
free(key);
|
|
|
24937f |
|
|
|
24937f |
if ( localTcp.port && remoteUdp.port
|
|
|
24937f |
&& !tunnel.initTcpServer(localTcp, remoteUdp) )
|
|
|
24937f |
return 1;
|
|
|
24937f |
|
|
|
24937f |
tunnel.run();
|
|
|
24937f |
|
|
|
24937f |
return 0;
|
|
|
24937f |
}
|
|
|
24937f |
|