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_TINYVPN_H_
|
|
|
|
#define TINYVPN_TINYVPN_H_
|
|
|
|
|
2018-08-11 20:27:14 -05:00
|
|
|
#include "netapi.h"
|
2018-10-07 12:36:52 -05:00
|
|
|
#include "tunsafe_threading.h"
|
2018-08-11 20:27:14 -05:00
|
|
|
|
2018-08-08 06:12:38 -05:00
|
|
|
class WireguardProcessor;
|
2018-08-11 20:27:14 -05:00
|
|
|
class DnsBlocker;
|
|
|
|
|
2018-10-07 12:36:52 -05:00
|
|
|
class DnsResolverCanceller {
|
|
|
|
public:
|
|
|
|
DnsResolverCanceller() : cancel_(false) {}
|
|
|
|
void Cancel();
|
|
|
|
void Reset() { cancel_ = false; }
|
|
|
|
bool is_cancelled() { return cancel_; }
|
|
|
|
public:
|
|
|
|
bool cancel_;
|
|
|
|
ConditionVariable condvar_;
|
|
|
|
};
|
|
|
|
|
2018-08-11 20:27:14 -05:00
|
|
|
class DnsResolver {
|
|
|
|
public:
|
|
|
|
explicit DnsResolver(DnsBlocker *dns_blocker);
|
|
|
|
~DnsResolver();
|
|
|
|
|
|
|
|
bool Resolve(const char *hostname, IpAddr *result);
|
|
|
|
void ClearCache();
|
2018-08-08 06:12:38 -05:00
|
|
|
|
2018-10-07 12:36:52 -05:00
|
|
|
void Cancel() { token_.Cancel(); }
|
|
|
|
void ResetCancel() { token_.Reset(); }
|
2018-08-11 20:27:14 -05:00
|
|
|
private:
|
|
|
|
struct Entry {
|
|
|
|
std::string name;
|
|
|
|
IpAddr ip;
|
|
|
|
Entry(const std::string &name, const IpAddr &ip) : name(name), ip(ip) {}
|
|
|
|
};
|
|
|
|
std::vector<Entry> cache_;
|
|
|
|
DnsBlocker *dns_blocker_;
|
2018-10-07 12:36:52 -05:00
|
|
|
DnsResolverCanceller token_;
|
2018-08-11 20:27:14 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-09-15 11:22:05 -05:00
|
|
|
class WgConfig {
|
|
|
|
public:
|
|
|
|
static bool HandleConfigurationProtocolMessage(WireguardProcessor *proc, const std::string &&message, std::string *result);
|
|
|
|
private:
|
|
|
|
static void HandleConfigurationProtocolGet(WireguardProcessor *proc, std::string *result);
|
|
|
|
};
|
|
|
|
|
2018-10-12 17:52:51 -05:00
|
|
|
bool ParseWireGuardConfigString(WireguardProcessor *wg, char *buf, size_t buf_size, DnsResolver *dns_resolver);
|
2018-08-11 20:27:14 -05:00
|
|
|
bool ParseWireGuardConfigFile(WireguardProcessor *wg, const char *filename, DnsResolver *dns_resolver);
|
2018-08-08 06:12:38 -05:00
|
|
|
|
|
|
|
#define kSizeOfAddress 64
|
|
|
|
const char *print_ip_prefix(char buf[kSizeOfAddress], int family, const void *ip, int prefixlen);
|
2018-08-11 20:27:14 -05:00
|
|
|
char *PrintIpAddr(const IpAddr &addr, char buf[kSizeOfAddress]);
|
2018-09-15 11:22:05 -05:00
|
|
|
char *PrintWgCidrAddr(const WgCidrAddr &addr, char buf[kSizeOfAddress]);
|
2018-10-07 12:36:52 -05:00
|
|
|
|
2018-09-15 11:22:05 -05:00
|
|
|
bool ParseCidrAddr(char *s, WgCidrAddr *out);
|
2018-10-07 12:36:52 -05:00
|
|
|
bool ParseSockaddrInWithPort(const char *s, IpAddr *sin, DnsResolver *resolver);
|
2018-08-08 06:12:38 -05:00
|
|
|
|
|
|
|
#endif // TINYVPN_TINYVPN_H_
|