2018-08-08 06:12:38 -05:00
|
|
|
// SPDX-License-Identifier: AGPL-1.0-only
|
|
|
|
// Copyright (C) 2018 Ludvig Strigeus <info@tunsafe.com>. All Rights Reserved.
|
|
|
|
#ifndef TINYVPN_NETAPI_H_
|
|
|
|
#define TINYVPN_NETAPI_H_
|
|
|
|
|
|
|
|
#include "tunsafe_types.h"
|
2018-10-21 09:00:15 -05:00
|
|
|
#include "tunsafe_ipaddr.h"
|
2018-08-08 06:12:38 -05:00
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#if !defined(OS_WIN)
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#pragma warning (disable: 4200)
|
|
|
|
|
2018-10-29 18:31:33 -05:00
|
|
|
struct QueuedItem;
|
|
|
|
|
|
|
|
struct QueuedItemCallback {
|
|
|
|
virtual void OnQueuedItemEvent(QueuedItem *ow, uintptr_t extra) = 0;
|
|
|
|
virtual void OnQueuedItemDelete(QueuedItem *ow) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct QueuedItem {
|
2018-08-08 06:12:38 -05:00
|
|
|
union {
|
|
|
|
#if defined(OS_WIN)
|
2018-10-29 18:31:33 -05:00
|
|
|
// NOTE: This must be at offset 0 for SLIST to work
|
2018-08-08 06:12:38 -05:00
|
|
|
SLIST_ENTRY list_entry;
|
2018-10-29 18:31:33 -05:00
|
|
|
OVERLAPPED overlapped;
|
2018-08-08 06:12:38 -05:00
|
|
|
#endif
|
2018-10-29 18:31:33 -05:00
|
|
|
QueuedItem *queue_next;
|
2018-08-08 06:12:38 -05:00
|
|
|
};
|
2018-10-29 18:31:33 -05:00
|
|
|
QueuedItemCallback *queue_cb;
|
|
|
|
};
|
2018-08-08 06:12:38 -05:00
|
|
|
|
2018-10-29 18:31:33 -05:00
|
|
|
#define Packet_NEXT(p) (*(Packet**)&(p)->queue_next)
|
2018-08-08 06:12:38 -05:00
|
|
|
|
2018-11-16 08:07:52 -06:00
|
|
|
// Protocol types used in the Endpoint thing
|
|
|
|
enum {
|
|
|
|
// The standard wireguard protocol
|
|
|
|
kPacketProtocolUdp = 1,
|
|
|
|
|
|
|
|
// Wireguard UDP framed inside of TCP
|
|
|
|
kPacketProtocolTcp = 2,
|
|
|
|
|
|
|
|
// This is OR:ed with the value in case it's an incoming connection
|
|
|
|
// and it's not possible to connect back to it, e.g. incoming tcp
|
|
|
|
kPacketProtocolIncomingConnection = 0x80,
|
|
|
|
};
|
|
|
|
|
2018-10-29 18:31:33 -05:00
|
|
|
struct Packet : QueuedItem {
|
2018-08-08 06:12:38 -05:00
|
|
|
int sin_size;
|
2018-10-29 18:31:33 -05:00
|
|
|
unsigned int size;
|
|
|
|
|
2018-10-29 20:19:20 -05:00
|
|
|
byte *data;
|
2018-10-29 18:31:33 -05:00
|
|
|
uint8 userdata;
|
2018-11-16 08:07:52 -06:00
|
|
|
uint8 protocol; // which protocol is this packet for/from
|
2018-10-29 20:19:20 -05:00
|
|
|
IpAddr addr; // Optionally set to target/source of the packet
|
2018-11-17 12:14:05 -06:00
|
|
|
|
2018-08-08 06:12:38 -05:00
|
|
|
enum {
|
2018-11-17 12:14:05 -06:00
|
|
|
// there's always this much data before data_buf, to allow for header expansion
|
|
|
|
// in front.
|
2018-08-08 06:12:38 -05:00
|
|
|
HEADROOM_BEFORE = 64,
|
|
|
|
};
|
2018-11-17 12:14:05 -06:00
|
|
|
|
2018-12-10 16:12:57 -06:00
|
|
|
|
|
|
|
#ifdef PACKET_EXTENSION_FIELDS
|
|
|
|
PACKET_EXTENSION_FIELDS
|
|
|
|
#endif // PACKET_EXTENSION_FIELDS
|
|
|
|
|
|
|
|
|
2018-11-17 12:14:05 -06:00
|
|
|
byte data_pre[HEADROOM_BEFORE];
|
|
|
|
byte data_buf[0];
|
|
|
|
|
|
|
|
void Reset() {
|
|
|
|
data = data_buf;
|
|
|
|
size = 0;
|
|
|
|
}
|
2018-08-08 06:12:38 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
kPacketAllocSize = 2048 - 16,
|
2018-11-17 12:14:05 -06:00
|
|
|
kPacketCapacity = kPacketAllocSize - sizeof(Packet),
|
2018-08-08 06:12:38 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
void FreePacket(Packet *packet);
|
|
|
|
void FreePackets(Packet *packet, Packet **end, int count);
|
2018-11-17 12:14:05 -06:00
|
|
|
void FreePacketList(Packet *packet);
|
2018-08-08 06:12:38 -05:00
|
|
|
Packet *AllocPacket();
|
|
|
|
void FreeAllPackets();
|
|
|
|
|
|
|
|
class TunInterface {
|
|
|
|
public:
|
|
|
|
struct PrePostCommands {
|
|
|
|
std::vector<std::string> pre_up;
|
|
|
|
std::vector<std::string> post_up;
|
|
|
|
std::vector<std::string> pre_down;
|
|
|
|
std::vector<std::string> post_down;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TunConfig {
|
|
|
|
// no, yes(firewall), yes(route), yes(both), 255(default)
|
|
|
|
uint8 internet_blocking;
|
|
|
|
|
2018-10-22 17:48:20 -05:00
|
|
|
bool block_dns_on_adapters;
|
2018-08-08 06:12:38 -05:00
|
|
|
|
|
|
|
// Set mtu
|
|
|
|
int mtu;
|
|
|
|
|
2018-10-22 17:48:20 -05:00
|
|
|
// The ipv6 and ipv4 addresses
|
|
|
|
std::vector<WgCidrAddr> addresses;
|
2018-08-08 06:12:38 -05:00
|
|
|
|
2018-10-22 17:48:20 -05:00
|
|
|
// Set this to configure DNS server
|
|
|
|
std::vector<IpAddr> dns;
|
2018-08-08 06:12:38 -05:00
|
|
|
|
|
|
|
// This holds all cidr addresses to add as additional routing entries
|
2018-10-22 17:48:20 -05:00
|
|
|
std::vector<WgCidrAddr> included_routes;
|
2018-08-08 06:12:38 -05:00
|
|
|
|
2018-08-11 20:27:14 -05:00
|
|
|
// This holds all the ips to exclude
|
2018-10-22 17:48:20 -05:00
|
|
|
std::vector<WgCidrAddr> excluded_routes;
|
2018-08-11 20:27:14 -05:00
|
|
|
|
2018-08-08 06:12:38 -05:00
|
|
|
// This holds the pre/post commands
|
|
|
|
PrePostCommands pre_post_commands;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TunConfigOut {
|
|
|
|
bool enable_neighbor_discovery_spoofing;
|
|
|
|
uint8 neighbor_discovery_spoofing_mac[6];
|
|
|
|
};
|
|
|
|
|
2018-09-15 11:22:05 -05:00
|
|
|
virtual bool Configure(const TunConfig &&config, TunConfigOut *out) = 0;
|
2018-08-08 06:12:38 -05:00
|
|
|
virtual void WriteTunPacket(Packet *packet) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class UdpInterface {
|
|
|
|
public:
|
2018-11-17 12:14:05 -06:00
|
|
|
virtual bool Configure(int listen_port_udp, int listen_port_tcp) = 0;
|
2018-08-08 06:12:38 -05:00
|
|
|
virtual void WriteUdpPacket(Packet *packet) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern bool g_allow_pre_post;
|
|
|
|
|
|
|
|
#endif // TINYVPN_NETAPI_H_
|