Split EthernetTap into subclasses, work in progress...
This commit is contained in:
parent
19c4d82512
commit
6c60305a96
6 changed files with 1425 additions and 119 deletions
|
@ -31,67 +31,44 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <map>
|
||||
#include <list>
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <queue>
|
||||
#include <stdexcept>
|
||||
#include <set>
|
||||
|
||||
#include "Constants.hpp"
|
||||
#include "InetAddress.hpp"
|
||||
#include "MAC.hpp"
|
||||
#include "Mutex.hpp"
|
||||
#include "MulticastGroup.hpp"
|
||||
#include "Thread.hpp"
|
||||
#include "InetAddress.hpp"
|
||||
#include "Buffer.hpp"
|
||||
#include "Array.hpp"
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
#include <WinSock2.h>
|
||||
#include <Windows.h>
|
||||
#endif
|
||||
#include "MulticastGroup.hpp"
|
||||
#include "NonCopyable.hpp"
|
||||
|
||||
namespace ZeroTier {
|
||||
|
||||
class RuntimeEnvironment;
|
||||
|
||||
/**
|
||||
* System ethernet tap device
|
||||
* Base class for Ethernet tap device implementations
|
||||
*/
|
||||
class EthernetTap
|
||||
class EthernetTap : NonCopyable
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Construct a new TAP device
|
||||
*
|
||||
* Handler arguments: arg,from,to,etherType,data
|
||||
*
|
||||
* @param renv Runtime environment
|
||||
* @param tag A tag used to identify persistent taps at the OS layer (e.g. nwid in hex)
|
||||
* @param mac MAC address of device
|
||||
* @param mtu MTU of device
|
||||
* @param desc If non-NULL, a description (not used on all OSes)
|
||||
* @param handler Handler function to be called when data is received from the tap
|
||||
* @param arg First argument to handler function
|
||||
* @throws std::runtime_error Unable to allocate device
|
||||
*/
|
||||
EthernetTap(
|
||||
const RuntimeEnvironment *renv,
|
||||
const char *tag,
|
||||
const MAC &mac,
|
||||
unsigned int mtu,
|
||||
void (*handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &),
|
||||
void *arg)
|
||||
throw(std::runtime_error);
|
||||
virtual ~EthernetTap() {}
|
||||
|
||||
/**
|
||||
* Close tap and shut down thread
|
||||
*
|
||||
* This may block for a few seconds while thread exits.
|
||||
* @return Implementation class name (e.g. UnixEthernetTap)
|
||||
*/
|
||||
~EthernetTap();
|
||||
inline const char *implementationName() const { return _implName; }
|
||||
|
||||
/**
|
||||
* Sets whether device is 'up'
|
||||
*
|
||||
* This may do nothing on some platforms.
|
||||
*
|
||||
* @param en Is device enabled?
|
||||
*/
|
||||
virtual void setEnabled(bool en) = 0;
|
||||
|
||||
/**
|
||||
* @return Is device 'up'?
|
||||
*/
|
||||
virtual bool enabled() const = 0;
|
||||
|
||||
/**
|
||||
* Set the user display name for this connection
|
||||
|
@ -100,7 +77,7 @@ public:
|
|||
*
|
||||
* @param dn User display name
|
||||
*/
|
||||
void setDisplayName(const char *dn);
|
||||
virtual void setDisplayName(const char *dn) = 0;
|
||||
|
||||
/**
|
||||
* @return MAC address of this interface
|
||||
|
@ -118,7 +95,7 @@ public:
|
|||
* @param ip IP and netmask (netmask stored in port field)
|
||||
* @return True if IP added successfully
|
||||
*/
|
||||
bool addIP(const InetAddress &ip);
|
||||
virtual bool addIP(const InetAddress &ip) = 0;
|
||||
|
||||
/**
|
||||
* Remove an IP from this interface
|
||||
|
@ -128,12 +105,12 @@ public:
|
|||
* @param ip IP and netmask (netmask stored in port field)
|
||||
* @return True if IP removed successfully
|
||||
*/
|
||||
bool removeIP(const InetAddress &ip);
|
||||
virtual bool removeIP(const InetAddress &ip) = 0;
|
||||
|
||||
/**
|
||||
* @return All IP addresses (V4 and V6) assigned to this interface (including link-local)
|
||||
*/
|
||||
std::set<InetAddress> ips() const;
|
||||
virtual std::set<InetAddress> ips() const = 0;
|
||||
|
||||
/**
|
||||
* Set this tap's IP addresses to exactly this set of IPs
|
||||
|
@ -175,17 +152,17 @@ public:
|
|||
* @param data Frame payload
|
||||
* @param len Length of frame
|
||||
*/
|
||||
void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
|
||||
virtual void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len) = 0;
|
||||
|
||||
/**
|
||||
* @return OS-specific device or connection name
|
||||
*/
|
||||
std::string deviceName() const;
|
||||
virtual std::string deviceName() const = 0;
|
||||
|
||||
/**
|
||||
* @return OS-internal persistent device ID or empty string if not applicable to this platform or not persistent
|
||||
*/
|
||||
std::string persistentId() const;
|
||||
virtual std::string persistentId() const = 0;
|
||||
|
||||
/**
|
||||
* Fill or modify a set to contain multicast groups for this device
|
||||
|
@ -199,73 +176,17 @@ public:
|
|||
* @param groups Set to modify in place
|
||||
* @return True if set was changed since last call
|
||||
*/
|
||||
bool updateMulticastGroups(std::set<MulticastGroup> &groups);
|
||||
virtual bool updateMulticastGroups(std::set<MulticastGroup> &groups) = 0;
|
||||
|
||||
/**
|
||||
* Thread main method; do not call elsewhere
|
||||
*/
|
||||
void threadMain()
|
||||
throw();
|
||||
protected:
|
||||
EthernetTap(const char *cn,const MAC &m,unsigned int mt) :
|
||||
_implName(cn),
|
||||
_mac(m),
|
||||
_mtu(mt) {}
|
||||
|
||||
/**
|
||||
* Remove persistent tap device by device name
|
||||
*
|
||||
* This has no effect on platforms that do not have persistent taps.
|
||||
* On platforms like Windows with persistent devices the device is
|
||||
* uninstalled.
|
||||
*
|
||||
* @param _r Runtime environment
|
||||
* @param pdev Device name as returned by persistentId() while tap is running
|
||||
* @return True if a device was deleted
|
||||
*/
|
||||
static bool deletePersistentTapDevice(const RuntimeEnvironment *_r,const char *pid);
|
||||
|
||||
/**
|
||||
* Clean persistent tap devices that are not in the supplied set
|
||||
*
|
||||
* This has no effect on platforms that do not have persistent taps.
|
||||
* On platforms like Windows with persistent devices the device is
|
||||
* uninstalled.
|
||||
*
|
||||
* @param _r Runtime environment
|
||||
* @param exceptThese Devices to leave in place
|
||||
* @param alsoRemoveUnassociatedDevices If true, remove devices not associated with any network as well
|
||||
* @return Number of devices deleted or -1 if an error prevented the operation from being performed
|
||||
*/
|
||||
static int cleanPersistentTapDevices(const RuntimeEnvironment *_r,const std::set<std::string> &exceptThese,bool alsoRemoveUnassociatedDevices);
|
||||
|
||||
private:
|
||||
const MAC _mac;
|
||||
const unsigned int _mtu;
|
||||
|
||||
const RuntimeEnvironment *_r;
|
||||
|
||||
void (*_handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &);
|
||||
void *_arg;
|
||||
|
||||
Thread _thread;
|
||||
|
||||
#ifdef __UNIX_LIKE__
|
||||
char _dev[16];
|
||||
int _fd;
|
||||
int _shutdownSignalPipe[2];
|
||||
#endif
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
void _syncIpsWithRegistry(const std::set<InetAddress> &haveIps);
|
||||
|
||||
HANDLE _tap;
|
||||
OVERLAPPED _tapOvlRead,_tapOvlWrite;
|
||||
char _tapReadBuf[ZT_IF_MTU + 32];
|
||||
HANDLE _injectSemaphore;
|
||||
GUID _deviceGuid;
|
||||
std::string _myDeviceInstanceId; // NetCfgInstanceId, a GUID
|
||||
std::string _myDeviceInstanceIdPath; // DeviceInstanceID, another kind of "instance ID"
|
||||
std::queue< std::pair< Array<char,ZT_IF_MTU + 32>,unsigned int > > _injectPending;
|
||||
Mutex _injectPending_m;
|
||||
volatile bool _run;
|
||||
volatile bool _initialized;
|
||||
#endif
|
||||
const char *_implName;
|
||||
MAC _mac;
|
||||
unsigned int _mtu;
|
||||
};
|
||||
|
||||
} // namespace ZeroTier
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue