tunsafe-clang15/network_win32_api.h
Ludvig Strigeus cf92ac7a0c Updates for TunSafe 1.4-rc1
1.Subfolders in the Config/ directory now show up as submenus.
2.Added a way to run TunSafe as a Windows Service.
  Foreground Mode: The service will disconnect when TunSafe closes.
  Background Mode: The service will stay connected in the background.
  No longer required to run the TunSafe client as Admin as long as
  the service is running.
3.New config setting [Interface].ExcludedIPs to configure IPs that
  should not be routed through TunSafe.
4.Can now automatically start TunSafe when Windows starts
5.New UI with tabs and graphs
6.Cache DNS queries to ensure DNS will succeed if connection fails
7.Recreate tray icon when explorer.exe restarts
8.Renamed window title to TunSafe instead of TunSafe VPN Client
9.Main window is now resizable
10.Disallow roaming endpoint when using AllowedIPs=0.0.0.0/0
   Only the original endpoint is added in the routing table so
   this would result in an endless loop of packets.
11.Display approximate Wireguard framing overhead in stats
12.Preparations for protocol handling with multiple threads
13.Delete the routes we made when disconnecting
14.Fix error message about unable to delete a route when connecting
2018-08-12 03:30:06 +02:00

120 lines
3.2 KiB
C++

// SPDX-License-Identifier: AGPL-1.0-only
// Copyright (C) 2018 Ludvig Strigeus <info@tunsafe.com>. All Rights Reserved.
#pragma once
#include "stdafx.h"
#include "tunsafe_types.h"
#include "wireguard.h"
#include <functional>
struct StatsCollector {
public:
enum {
CHANNELS = 2,
TIMEVALS = 4,
};
StatsCollector() { Init(); }
void AddSamples(float data[CHANNELS]);
struct TimeSeries {
float *data;
int size;
int shift;
};
const TimeSeries *GetTimeSeries(int channel, int timeval) { return &accum_[channel][timeval].data; }
private:
struct Accumulator {
float acc;
int acc_count;
int acc_max;
bool dirty;
TimeSeries data;
};
void Init();
static void AddToGraphDataSource(StatsCollector::TimeSeries *ts, float value);
static void AddToAccumulators(StatsCollector::Accumulator *acc, float rval);
Accumulator accum_[CHANNELS][TIMEVALS];
};
struct LinearizedGraph {
uint32 total_size;
uint32 graph_type;
uint8 num_charts;
uint8 reserved[7];
};
class TunsafeBackend {
public:
// All codes < 0 are permanent errors
enum StatusCode {
kStatusStopped = 0,
kStatusInitializing = 1,
kStatusConnecting = 2,
kStatusConnected = 3,
kStatusReconnecting = 4,
kStatusTunRetrying = 10,
kErrorInitialize = -1,
kErrorTunPermanent = -2,
kErrorServiceLost = -3,
};
static bool IsPermanentError(StatusCode status) {
return (int32)status < 0;
}
class Delegate {
public:
virtual ~Delegate();
virtual void OnGetStats(const WgProcessorStats &stats) = 0;
virtual void OnGraphAvailable() = 0;
virtual void OnStateChanged() = 0;
virtual void OnClearLog() = 0;
virtual void OnLogLine(const char **s) = 0;
virtual void OnStatusCode(TunsafeBackend::StatusCode status) = 0;
// This function is needed for CreateTunsafeBackendDelegateThreaded,
// It's expected to be called on the main thread and then all callbacks will arrive
// on the right thread.
virtual void DoWork();
};
TunsafeBackend();
virtual ~TunsafeBackend();
// Setup/teardown the connection to the local service (if any)
virtual bool Initialize() = 0;
virtual void Teardown() = 0;
virtual void Start(const char *config_file) = 0;
virtual void Stop() = 0;
virtual void RequestStats(bool enable) = 0;
virtual void ResetStats() = 0;
virtual InternetBlockState GetInternetBlockState(bool *is_activated) = 0;
virtual void SetInternetBlockState(InternetBlockState s) = 0;
virtual void SetServiceStartupFlags(uint32 flags) = 0;
virtual std::string GetConfigFileName() = 0;
virtual LinearizedGraph *GetGraph(int type) = 0;
bool is_started() { return is_started_; }
bool is_remote() { return is_remote_; }
const uint8 *public_key() { return public_key_; }
StatusCode status() { return status_; }
uint32 GetIP() { return ipv4_ip_; }
protected:
bool is_started_;
bool is_remote_;
StatusCode status_;
uint32 ipv4_ip_;
uint8 public_key_[32];
};
TunsafeBackend *CreateNativeTunsafeBackend(TunsafeBackend::Delegate *delegate);
TunsafeBackend::Delegate *CreateTunsafeBackendDelegateThreaded(TunsafeBackend::Delegate *delegate, const std::function<void(void)> &callback);
extern int tpq_last_qsize;
extern int g_tun_reads, g_tun_writes;