2020-06-02 13:08:23 +02:00
|
|
|
#include <cstdio>
|
|
|
|
#include <unistd.h>
|
2020-06-04 11:47:00 +02:00
|
|
|
#include <arpa/inet.h>
|
2020-06-03 14:54:26 +02:00
|
|
|
#include <signal.h>
|
2020-06-04 11:47:00 +02:00
|
|
|
#include <string.h>
|
2020-06-02 13:08:23 +02:00
|
|
|
|
2020-06-04 11:47:00 +02:00
|
|
|
#include "UdpVpn.hpp"
|
|
|
|
#include "util.hpp"
|
2020-06-02 13:08:23 +02:00
|
|
|
|
2020-06-04 11:47:00 +02:00
|
|
|
struct ProgOptions {
|
|
|
|
bool listen;
|
|
|
|
in6_addr bind_addr;
|
|
|
|
in_port_t bind_port;
|
|
|
|
|
|
|
|
bool has_peer;
|
|
|
|
in6_addr server_addr;
|
|
|
|
in_port_t server_port;
|
|
|
|
};
|
|
|
|
|
|
|
|
static UdpVpn* vpn_instance = nullptr;
|
2020-06-03 14:54:26 +02:00
|
|
|
|
|
|
|
void stop_sig_handler(int signal) {
|
|
|
|
printf("Received signal %d. Stopping.\n", signal);
|
2020-06-04 11:47:00 +02:00
|
|
|
if(vpn_instance != nullptr)
|
|
|
|
vpn_instance->stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool parse_options(int argc, char** argv, ProgOptions& opts) {
|
|
|
|
int option;
|
|
|
|
memset(&opts, 0, sizeof(opts));
|
|
|
|
|
|
|
|
while((option = getopt(argc, argv, ":d:l:b:s:p:")) >= 0) {
|
|
|
|
switch(option) {
|
|
|
|
case 'd': // debug
|
|
|
|
debug = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'l': // listen, aka "call bind()"
|
|
|
|
opts.listen = true;
|
|
|
|
opts.bind_port = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'b': // bind to address
|
|
|
|
inet_pton(AF_INET6, optarg, &opts.bind_addr);
|
|
|
|
break;
|
|
|
|
case 's': // server -- initial peer
|
|
|
|
opts.has_peer = true;
|
|
|
|
inet_pton(AF_INET6, optarg, &opts.server_addr);
|
|
|
|
break;
|
|
|
|
case 'p': // port -- initial peer address
|
|
|
|
opts.server_port = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case ':':
|
|
|
|
fprintf(stderr, "Option %c requires a value.\n", optopt);
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
case '?':
|
|
|
|
fprintf(stderr, "Unknown option %c.\n", optopt);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2020-06-03 14:54:26 +02:00
|
|
|
}
|
|
|
|
|
2020-06-04 11:47:00 +02:00
|
|
|
int main(int argc, char** argv) {
|
|
|
|
debug = 0;
|
|
|
|
ProgOptions program_options;
|
|
|
|
parse_options(argc, argv, program_options);
|
|
|
|
|
|
|
|
printf("==== Options ====\n");
|
|
|
|
printf("Debug level: %d\n", debug);
|
|
|
|
printf("Bind socket: %d\n", program_options.listen);
|
|
|
|
if(program_options.listen) {
|
|
|
|
char bind_addr_str[INET6_ADDRSTRLEN];
|
|
|
|
inet_ntop(
|
|
|
|
AF_INET6, &(program_options.bind_addr),
|
|
|
|
bind_addr_str, INET6_ADDRSTRLEN);
|
|
|
|
printf("\taddr: %s\n", bind_addr_str);
|
|
|
|
printf("\tport: %d\n", program_options.bind_port);
|
|
|
|
}
|
|
|
|
printf("Start with peer: %d\n", program_options.has_peer);
|
|
|
|
if(program_options.has_peer) {
|
|
|
|
char peer_addr_str[INET6_ADDRSTRLEN];
|
|
|
|
inet_ntop(
|
|
|
|
AF_INET6, &(program_options.server_addr),
|
|
|
|
peer_addr_str, INET6_ADDRSTRLEN);
|
|
|
|
printf("\taddr: %s\n", peer_addr_str);
|
|
|
|
printf("\tport: %d\n", program_options.server_port);
|
|
|
|
}
|
|
|
|
printf("=== END OPTIONS ==\n\n");
|
2020-06-03 14:54:26 +02:00
|
|
|
|
|
|
|
signal(SIGINT, stop_sig_handler);
|
2020-06-04 11:47:00 +02:00
|
|
|
|
2020-06-02 13:08:23 +02:00
|
|
|
try {
|
2020-06-04 11:47:00 +02:00
|
|
|
UdpVpn vpn;
|
|
|
|
vpn_instance = &vpn;
|
|
|
|
|
|
|
|
if(program_options.listen) {
|
|
|
|
vpn.bind(program_options.bind_addr, program_options.bind_port);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(program_options.has_peer) {
|
|
|
|
if(program_options.server_port == 0) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Initial peer set without a port -- ignoring.\n");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
struct sockaddr_in6 server_addr;
|
|
|
|
server_addr.sin6_family = AF_INET6;
|
|
|
|
memcpy(&server_addr.sin6_addr,
|
|
|
|
&program_options.server_addr,
|
|
|
|
sizeof(server_addr.sin6_addr));
|
|
|
|
server_addr.sin6_port = htons(program_options.server_port);
|
|
|
|
vpn.set_peer(server_addr);
|
2020-06-03 14:54:26 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-04 11:47:00 +02:00
|
|
|
|
|
|
|
printf("Starting to listen...\n");
|
|
|
|
vpn.run();
|
|
|
|
vpn_instance = nullptr;
|
|
|
|
|
2020-06-02 13:08:23 +02:00
|
|
|
printf("Shutting down.\n");
|
|
|
|
} catch(const TunDevice::InitializationError& exn) {
|
2020-06-04 11:47:00 +02:00
|
|
|
fprintf(stderr, "TUN INIT ERROR: %s\n", exn.what());
|
|
|
|
} catch(const TunDevice::NetError& exn) {
|
|
|
|
fprintf(stderr, "TUN NET ERROR: %s\n", exn.what());
|
|
|
|
} catch(const UdpVpn::InitializationError& exn) {
|
|
|
|
fprintf(stderr, "VPN INIT ERROR: %s\n", exn.what());
|
|
|
|
} catch(const UdpVpn::NetError& exn) {
|
|
|
|
fprintf(stderr, "VPN NET ERROR: %s\n", exn.what());
|
2020-06-02 13:08:23 +02:00
|
|
|
}
|
2020-06-04 11:47:00 +02:00
|
|
|
|
2020-06-02 13:08:23 +02:00
|
|
|
return 0;
|
|
|
|
}
|