congestvpn/UdpVpnServer.cpp

61 lines
1.6 KiB
C++
Raw Permalink Normal View History

#include <string.h>
2020-07-22 19:38:14 +02:00
#include <stdio.h>
#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;
}
void UdpVpnServer::acquire_peer(
2020-06-26 19:41:55 +02:00
VpnDataPacket& packet,
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-07-22 17:04:22 +02:00
packet.set_peer(_peer);
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));
}
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);
}
debugf("> Listening on port %d\n", port);
}