2014-04-07 21:47:39 +00:00
|
|
|
/*
|
2019-08-23 16:23:39 +00:00
|
|
|
* Copyright (c)2019 ZeroTier, Inc.
|
2014-04-07 21:47:39 +00:00
|
|
|
*
|
2019-08-23 16:23:39 +00:00
|
|
|
* Use of this software is governed by the Business Source License included
|
|
|
|
* in the LICENSE.TXT file in the project's root directory.
|
2014-04-07 21:47:39 +00:00
|
|
|
*
|
2019-08-23 16:23:39 +00:00
|
|
|
* Change Date: 2023-01-01
|
2014-04-07 21:47:39 +00:00
|
|
|
*
|
2019-08-23 16:23:39 +00:00
|
|
|
* On the date above, in accordance with the Business Source License, use
|
|
|
|
* of this software will be governed by version 2.0 of the Apache License.
|
2014-04-07 21:47:39 +00:00
|
|
|
*/
|
2019-08-23 16:23:39 +00:00
|
|
|
/****/
|
2014-04-07 21:47:39 +00:00
|
|
|
|
2014-04-08 19:00:21 +00:00
|
|
|
#ifndef ZT_WINDOWSETHERNETTAP_HPP
|
|
|
|
#define ZT_WINDOWSETHERNETTAP_HPP
|
2014-04-07 21:47:39 +00:00
|
|
|
|
2019-08-23 16:34:26 +00:00
|
|
|
#include "../node/Constants.hpp"
|
|
|
|
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
|
2014-04-07 21:47:39 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2014-08-05 04:48:59 +00:00
|
|
|
#include <ifdef.h>
|
|
|
|
|
2014-04-07 21:47:39 +00:00
|
|
|
#include <string>
|
|
|
|
#include <queue>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
2014-08-05 04:48:59 +00:00
|
|
|
#include "../node/Mutex.hpp"
|
|
|
|
#include "../node/MulticastGroup.hpp"
|
2015-07-30 21:10:32 +00:00
|
|
|
#include "../node/InetAddress.hpp"
|
2015-04-24 22:05:28 +00:00
|
|
|
#include "../osdep/Thread.hpp"
|
2019-08-07 23:14:12 +00:00
|
|
|
#include "EthernetTap.hpp"
|
2014-04-07 21:47:39 +00:00
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
2019-08-07 23:14:12 +00:00
|
|
|
class WindowsEthernetTap : public EthernetTap
|
2014-04-07 21:47:39 +00:00
|
|
|
{
|
|
|
|
public:
|
2015-07-30 18:31:38 +00:00
|
|
|
/**
|
|
|
|
* Installs a new instance of the ZT tap driver
|
|
|
|
*
|
|
|
|
* @param pathToInf Path to zttap driver .inf file
|
2015-08-13 22:52:54 +00:00
|
|
|
* @param deviceInstanceId Buffer to fill with device instance ID on success (and if SetupDiGetDeviceInstanceIdA succeeds, which it should)
|
2015-07-30 18:31:38 +00:00
|
|
|
* @return Empty string on success, otherwise an error message
|
|
|
|
*/
|
2015-08-13 22:52:54 +00:00
|
|
|
static std::string addNewPersistentTapDevice(const char *pathToInf,std::string &deviceInstanceId);
|
2015-07-30 18:31:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Uninstalls all persistent tap devices that have legacy drivers
|
|
|
|
*
|
|
|
|
* @return Empty string on success, otherwise an error message
|
|
|
|
*/
|
|
|
|
static std::string destroyAllLegacyPersistentTapDevices();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uninstalls all persistent tap devices on the system
|
|
|
|
*
|
|
|
|
* @return Empty string on success, otherwise an error message
|
|
|
|
*/
|
|
|
|
static std::string destroyAllPersistentTapDevices();
|
|
|
|
|
|
|
|
/**
|
2018-06-08 00:25:27 +00:00
|
|
|
* Uninstalls a specific persistent tap device by instance ID
|
2015-07-30 18:31:38 +00:00
|
|
|
*
|
|
|
|
* @param instanceId Device instance ID
|
|
|
|
* @return Empty string on success, otherwise an error message
|
|
|
|
*/
|
|
|
|
static std::string deletePersistentTapDevice(const char *instanceId);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable a persistent tap device by instance ID
|
|
|
|
*
|
|
|
|
* @param instanceId Device instance ID
|
|
|
|
* @param enabled Enable device?
|
|
|
|
* @return True if device was found and disabled
|
|
|
|
*/
|
|
|
|
static bool setPersistentTapDeviceState(const char *instanceId,bool enabled);
|
|
|
|
|
2014-04-08 19:00:21 +00:00
|
|
|
WindowsEthernetTap(
|
2015-04-24 22:05:28 +00:00
|
|
|
const char *hp,
|
2014-04-07 21:47:39 +00:00
|
|
|
const MAC &mac,
|
|
|
|
unsigned int mtu,
|
2014-08-05 04:48:59 +00:00
|
|
|
unsigned int metric,
|
|
|
|
uint64_t nwid,
|
|
|
|
const char *friendlyName,
|
2017-03-28 00:03:17 +00:00
|
|
|
void (*handler)(void *,void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int),
|
2014-08-05 04:48:59 +00:00
|
|
|
void *arg);
|
2014-04-07 21:47:39 +00:00
|
|
|
|
2019-08-07 23:14:12 +00:00
|
|
|
virtual ~WindowsEthernetTap();
|
2014-04-07 21:47:39 +00:00
|
|
|
|
2019-08-07 23:14:12 +00:00
|
|
|
virtual void setEnabled(bool en);
|
|
|
|
virtual bool enabled() const;
|
|
|
|
virtual bool addIp(const InetAddress &ip);
|
|
|
|
virtual bool removeIp(const InetAddress &ip);
|
|
|
|
virtual std::vector<InetAddress> ips() const;
|
|
|
|
virtual void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
|
|
|
|
virtual std::string deviceName() const;
|
|
|
|
virtual void setFriendlyName(const char *friendlyName);
|
|
|
|
virtual void scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed);
|
|
|
|
virtual void setMtu(unsigned int mtu);
|
2014-04-07 21:47:39 +00:00
|
|
|
|
2014-08-05 04:48:59 +00:00
|
|
|
inline const NET_LUID &luid() const { return _deviceLuid; }
|
|
|
|
inline const GUID &guid() const { return _deviceGuid; }
|
|
|
|
inline const std::string &instanceId() const { return _deviceInstanceId; }
|
2016-06-21 20:54:03 +00:00
|
|
|
NET_IFINDEX interfaceIndex() const;
|
2014-08-05 04:48:59 +00:00
|
|
|
|
2014-04-07 21:47:39 +00:00
|
|
|
void threadMain()
|
|
|
|
throw();
|
|
|
|
|
2019-08-07 23:14:12 +00:00
|
|
|
bool isInitialized() const { return _initialized; };
|
2017-01-05 19:43:26 +00:00
|
|
|
|
2014-04-07 21:47:39 +00:00
|
|
|
private:
|
2014-08-05 04:48:59 +00:00
|
|
|
NET_IFINDEX _getDeviceIndex(); // throws on failure
|
2014-08-13 00:20:34 +00:00
|
|
|
std::vector<std::string> _getRegistryIPv4Value(const char *regKey);
|
|
|
|
void _setRegistryIPv4Value(const char *regKey,const std::vector<std::string> &value);
|
2015-07-30 21:10:32 +00:00
|
|
|
void _syncIps();
|
2014-08-05 04:48:59 +00:00
|
|
|
|
2017-03-28 00:03:17 +00:00
|
|
|
void (*_handler)(void *,void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int);
|
2014-04-07 21:47:39 +00:00
|
|
|
void *_arg;
|
2015-04-24 22:05:28 +00:00
|
|
|
MAC _mac;
|
2014-08-13 00:20:34 +00:00
|
|
|
uint64_t _nwid;
|
2017-05-05 02:50:02 +00:00
|
|
|
volatile unsigned int _mtu;
|
2014-04-07 21:47:39 +00:00
|
|
|
Thread _thread;
|
|
|
|
|
2014-04-08 22:47:33 +00:00
|
|
|
volatile HANDLE _tap;
|
2014-04-07 21:47:39 +00:00
|
|
|
HANDLE _injectSemaphore;
|
2014-08-05 04:48:59 +00:00
|
|
|
|
2014-04-07 21:47:39 +00:00
|
|
|
GUID _deviceGuid;
|
2014-08-05 04:48:59 +00:00
|
|
|
NET_LUID _deviceLuid;
|
2015-06-12 14:02:04 +00:00
|
|
|
std::string _netCfgInstanceId;
|
|
|
|
std::string _deviceInstanceId;
|
2017-05-05 02:31:50 +00:00
|
|
|
std::string _mySubkeyName;
|
2014-08-05 04:48:59 +00:00
|
|
|
|
2017-10-16 19:16:05 +00:00
|
|
|
std::string _friendlyName;
|
|
|
|
|
2015-07-30 21:10:32 +00:00
|
|
|
std::vector<InetAddress> _assignedIps; // IPs assigned with addIp
|
|
|
|
Mutex _assignedIps_m;
|
|
|
|
|
2015-04-24 22:05:28 +00:00
|
|
|
std::vector<MulticastGroup> _multicastGroups;
|
|
|
|
|
2018-01-31 00:07:41 +00:00
|
|
|
struct _InjectPending
|
|
|
|
{
|
|
|
|
unsigned int len;
|
|
|
|
char data[ZT_MAX_MTU + 32];
|
|
|
|
};
|
|
|
|
std::queue<_InjectPending> _injectPending;
|
2014-04-07 21:47:39 +00:00
|
|
|
Mutex _injectPending_m;
|
2014-08-05 04:48:59 +00:00
|
|
|
|
|
|
|
std::string _pathToHelpers;
|
|
|
|
|
2014-04-07 21:47:39 +00:00
|
|
|
volatile bool _run;
|
|
|
|
volatile bool _initialized;
|
2014-04-08 19:00:21 +00:00
|
|
|
volatile bool _enabled;
|
2014-04-07 21:47:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|
2019-08-23 16:34:26 +00:00
|
|
|
|
|
|
|
#endif
|