2020-07-22 15:52:55 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2020-06-02 13:08:23 +02:00
|
|
|
#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"
|
2020-06-05 16:14:49 +02:00
|
|
|
#include "UdpVpnServer.hpp"
|
|
|
|
#include "UdpVpnClient.hpp"
|
2020-06-04 11:47:00 +02:00
|
|
|
#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();
|
|
|
|
}
|
|
|
|
|
2020-06-26 19:41:55 +02:00
|
|
|
void dump_sig_handler(int /*signal*/) {
|
|
|
|
vpn_instance->dump_requested();
|
|
|
|
}
|
|
|
|
|
2020-06-04 11:47:00 +02:00
|
|
|
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
|
|
|
|
2020-06-28 23:29:39 +02:00
|
|
|
srand(time(NULL)); // FIXME something more secure if we ever need crypto
|
|
|
|
|
2020-06-03 14:54:26 +02:00
|
|
|
signal(SIGINT, stop_sig_handler);
|
2020-06-26 19:41:55 +02:00
|
|
|
signal(SIGUSR1, dump_sig_handler);
|
2020-06-04 11:47:00 +02:00
|
|
|
|
2020-06-02 13:08:23 +02:00
|
|
|
try {
|
2020-06-05 16:14:49 +02:00
|
|
|
if(program_options.listen && program_options.has_peer) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: Cannot be a server and a client at the same time "
|
|
|
|
"-- provide either -l or -s.\n");
|
|
|
|
return 1;
|
2020-06-04 11:47:00 +02:00
|
|
|
}
|
2020-06-05 16:14:49 +02:00
|
|
|
if(program_options.listen) {
|
|
|
|
vpn_instance = new UdpVpnServer(
|
|
|
|
program_options.bind_addr, program_options.bind_port);
|
|
|
|
} else if(program_options.has_peer) {
|
2020-06-04 11:47:00 +02:00
|
|
|
if(program_options.server_port == 0) {
|
|
|
|
fprintf(stderr,
|
2020-06-05 16:14:49 +02:00
|
|
|
"ERROR: A client instance must be given a server port "
|
|
|
|
"-- please provide -p.\n");
|
|
|
|
return 1;
|
2020-06-03 14:54:26 +02:00
|
|
|
}
|
2020-06-05 16:14:49 +02:00
|
|
|
|
|
|
|
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_instance = new UdpVpnClient(server_addr);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: Must be either a server or a client "
|
|
|
|
"-- provide either -l or -s.\n");
|
|
|
|
return 1;
|
2020-06-03 14:54:26 +02:00
|
|
|
}
|
2020-06-04 11:47:00 +02:00
|
|
|
|
|
|
|
printf("Starting to listen...\n");
|
2020-06-05 16:14:49 +02:00
|
|
|
vpn_instance->run();
|
|
|
|
|
|
|
|
delete vpn_instance;
|
2020-06-04 11:47:00 +02:00
|
|
|
|
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;
|
|
|
|
}
|