2020-07-22 15:52:55 +02:00
|
|
|
#include <string.h>
|
2020-07-22 19:38:14 +02:00
|
|
|
#include <stdio.h>
|
2020-06-05 16:14:49 +02:00
|
|
|
|
|
|
|
#include "UdpVpnServer.hpp"
|
|
|
|
#include "ip_header.hpp"
|
|
|
|
|
|
|
|
UdpVpnServer::UdpVpnServer(in_port_t port) : UdpVpn() {
|
|
|
|
memset(&_bind_addr, 0, sizeof(_bind_addr));
|
|
|
|
bind(in6addr_any, port);
|
|
|
|
}
|
|
|
|
|
|
|
|
UdpVpnServer::UdpVpnServer(const struct in6_addr& bind_addr6, in_port_t port)
|
|
|
|
: UdpVpn()
|
|
|
|
{
|
|
|
|
memset(&_bind_addr, 0, sizeof(_bind_addr));
|
|
|
|
bind(bind_addr6, port);
|
|
|
|
}
|
|
|
|
|
2020-07-22 17:04:22 +02:00
|
|
|
UdpVpnServer::~UdpVpnServer() {
|
|
|
|
if(_peer != nullptr)
|
|
|
|
delete _peer;
|
|
|
|
}
|
|
|
|
|
2020-06-15 20:21:07 +02:00
|
|
|
void UdpVpnServer::acquire_peer(
|
2020-06-26 19:41:55 +02:00
|
|
|
VpnDataPacket& packet,
|
2020-06-15 20:21:07 +02:00
|
|
|
const sockaddr_in6& peer_ext_addr)
|
|
|
|
{
|
|
|
|
if(_peer)
|
|
|
|
return; // Refusing a connection if we already have one
|
|
|
|
// TODO: reset state at some point/if connection broken
|
|
|
|
|
|
|
|
if(!packet.parse_as_ipv6())
|
|
|
|
return;
|
|
|
|
const in6_addr& peer_inner_addr = packet.get_ipv6_header().source;
|
2020-07-22 17:04:22 +02:00
|
|
|
_peer = new VpnPeer(this, peer_ext_addr, peer_inner_addr);
|
2020-06-15 20:21:07 +02:00
|
|
|
|
2020-07-22 17:04:22 +02:00
|
|
|
packet.set_peer(_peer);
|
2020-06-15 20:21:07 +02:00
|
|
|
|
|
|
|
debugf("Got new peer %s:%d -- %s\n",
|
|
|
|
format_address(peer_ext_addr.sin6_addr.s6_addr),
|
|
|
|
htons(peer_ext_addr.sin6_port),
|
|
|
|
format_address(peer_inner_addr.s6_addr));
|
|
|
|
}
|
|
|
|
|
2020-06-05 16:14:49 +02:00
|
|
|
void UdpVpnServer::bind(const struct in6_addr& bind_addr6, in_port_t port) {
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
_bind_addr.sin6_family = AF_INET6;
|
|
|
|
_bind_addr.sin6_port = htons(port);
|
|
|
|
_bind_addr.sin6_addr = bind_addr6;
|
|
|
|
|
|
|
|
rc = ::bind(
|
|
|
|
_socket, (const struct sockaddr*)&_bind_addr, sizeof(_bind_addr));
|
|
|
|
if(rc < 0) {
|
2020-07-22 19:09:27 +02:00
|
|
|
perror("UdpVpn: cannot bind socket: ");
|
|
|
|
exit(1);
|
2020-06-05 16:14:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
debugf("> Listening on port %d\n", port);
|
|
|
|
}
|