summaryrefslogtreecommitdiff
path: root/src/VirtualTap.hpp
diff options
context:
space:
mode:
authorJoseph Henry <[email protected]>2020-05-01 19:15:38 -0700
committerJoseph Henry <[email protected]>2020-05-01 19:15:38 -0700
commita0b50530d37d9c13d30a68bf1d4686485be36327 (patch)
tree50251ec3ef7f18468ec3eb2d379d835ea8c64d1f /src/VirtualTap.hpp
parent2c709277b9632bd8e3af8b66f51d3f5a53f84e8e (diff)
Add portability and consistency fixes for C API, remove cruft, slight internal restructuring1.3.3
Diffstat (limited to 'src/VirtualTap.hpp')
-rw-r--r--src/VirtualTap.hpp195
1 files changed, 142 insertions, 53 deletions
diff --git a/src/VirtualTap.hpp b/src/VirtualTap.hpp
index 3703e96..486225c 100644
--- a/src/VirtualTap.hpp
+++ b/src/VirtualTap.hpp
@@ -14,38 +14,30 @@
/**
* @file
*
- * Virtual Ethernet tap device
+ * Header for virtual ethernet tap device and combined network stack driver
*/
-#ifndef LIBZT_VIRTUALTAP_HPP
-#define LIBZT_VIRTUALTAP_HPP
+#ifndef ZT_VIRTUAL_TAP_HPP
+#define ZT_VIRTUAL_TAP_HPP
-#ifndef _MSC_VER
-extern int errno;
-#endif
+#include "lwip/err.h"
+
+#define ZTS_LWIP_DRIVER_THREAD_NAME "NetworkStackThread"
+#include "MAC.hpp"
#include "Phy.hpp"
#include "Thread.hpp"
-#include "InetAddress.hpp"
-#include "MulticastGroup.hpp"
-#include "Mutex.hpp"
-
-#include "Options.h"
-
-#if defined(_WIN32)
-#include <WinSock2.h>
-#include <Windows.h>
-#include <IPHlpApi.h>
-#include <Ifdef.h>
-#endif
namespace ZeroTier {
class Mutex;
+class MAC;
+class MulticastGroup;
+struct InetAddress;
/**
- * A virtual tap device. The ZeroTier core service creates one of these for each
- * virtual network joined. It will be destroyed upon leave().
+ * A virtual tap device. The ZeroTier Node Service will create one per
+ * joined network. It will be destroyed upon leave().
*/
class VirtualTap
{
@@ -84,18 +76,18 @@ public:
bool hasIpv6Addr();
/**
- * Adds an address to the userspace stack interface associated with this VirtualTap
+ * Adds an address to the user-space stack interface associated with this VirtualTap
* - Starts VirtualTap main thread ONLY if successful
*/
bool addIp(const InetAddress &ip);
/**
- * Removes an address from the userspace stack interface associated with this VirtualTap
+ * Removes an address from the user-space stack interface associated with this VirtualTap
*/
bool removeIp(const InetAddress &ip);
/**
- * Presents data to the userspace stack
+ * Presents data to the user-space stack
*/
void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,
unsigned int len);
@@ -106,11 +98,6 @@ public:
std::string deviceName() const;
/**
- * Get Node ID (ZT address)
- */
- std::string nodeId() const;
-
- /**
* Set friendly name
*/
void setFriendlyName(const char *friendlyName);
@@ -152,10 +139,6 @@ public:
void (*_handler)(void *, void *, uint64_t, const MAC &, const MAC &, unsigned int, unsigned int,
const void *, unsigned int);
- //////////////////////////////////////////////////////////////////////////////
- // Lower-level lwIP netif handling and traffic handling readiness //
- //////////////////////////////////////////////////////////////////////////////
-
void *netif4 = NULL;
void *netif6 = NULL;
@@ -164,20 +147,10 @@ public:
*/
uint64_t _lastConfigUpdateTime = 0;
- /**
- * The last time that a callback notification was sent to the user application signalling
- * that this interface is ready to process traffic.
- */
- uint64_t _lastReadyReportTime = 0;
-
void lastConfigUpdate(uint64_t lastConfigUpdateTime);
int _networkStatus = 0;
- //////////////////////////////////////////////////////////////////////////////
- // Vars //
- //////////////////////////////////////////////////////////////////////////////
-
std::vector<std::pair<InetAddress, InetAddress> > routes;
char vtap_full_name[64];
@@ -205,17 +178,6 @@ public:
std::vector<MulticastGroup> _multicastGroups;
Mutex _multicastGroups_m;
- /*
- * Timestamp of last run of housekeeping
- * SEE: ZT_HOUSEKEEPING_INTERVAL in ZeroTier.h
- */
- uint64_t last_housekeeping_ts = 0;
-
- /**
- * Performs miscellaneous background tasks
- */
- void Housekeeping();
-
//////////////////////////////////////////////////////////////////////////////
// Not used in this implementation //
//////////////////////////////////////////////////////////////////////////////
@@ -228,8 +190,135 @@ public:
void phyOnTcpClose(PhySocket *sock,void **uptr);
void phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len);
void phyOnTcpWritable(PhySocket *sock,void **uptr);
+ void phyOnUnixClose(PhySocket *sock,void **uptr);
};
+/**
+ * @brief Return whether a given netif's NETIF_FLAG_UP flag is set
+ *
+ * @usage This is a convenience function to encapsulate a macro
+ */
+bool _lwip_is_netif_up(void *netif);
+
+/**
+ * @brief Increase the delay multiplier for the main driver loop
+ *
+ * @usage This should be called when we know the stack won't be used by any virtual taps
+ */
+void _lwip_hibernate_driver();
+
+/**
+ * @brief Decrease the delay multiplier for the main driver loop
+ *
+ * @usage This should be called when at least one virtual tap is active
+ */
+void _lwip_wake_driver();
+
+/**
+ * Returns whether the lwIP network stack is up and ready to process traffic
+ */
+bool _lwip_is_up();
+
+/**
+ * @brief Initialize network stack semaphores, threads, and timers.
+ *
+ * @usage This is called during the initial setup of each VirtualTap but is only allowed to execute once
+ * @return
+ */
+void _lwip_driver_init();
+
+/**
+ * @brief Shutdown the stack as completely as possible (not officially supported by lwIP)
+ *
+ * @usage This is to be called after it is determined that no further network activity will take place.
+ * The tcpip thread will be stopped, all interfaces will be brought down and all resources will
+ * be deallocated. A full application restart will be required to bring the stack back online.
+ * @return
+ */
+void _lwip_driver_shutdown();
+
+/**
+ * @brief Requests that a netif be brought down and removed.
+ *
+ * @return
+ */
+void _lwip_remove_netif(void *netif);
+
+/**
+ * @brief Initialize and start the DNS client
+ *
+ * @usage Called after lwip_driver_init()
+ * @return
+ */
+void _lwip_dns_init();
+
+/**
+ * @brief Starts DHCP timers
+ *
+ * @usage lwip_driver_init()
+ * @return
+ */
+void _lwip_start_dhcp(void *netif);
+
+/**
+ * @brief Called when the status of a netif changes:
+ * - Interface is up/down (ZTS_EVENT_NETIF_UP, ZTS_EVENT_NETIF_DOWN)
+ * - Address changes while up (ZTS_EVENT_NETIF_NEW_ADDRESS)
+ */
+#if LWIP_NETIF_STATUS_CALLBACK
+static void _netif_status_callback(struct netif *netif);
+#endif
+
+/**
+ * @brief Called when a netif is removed (ZTS_EVENT_NETIF_INTERFACE_REMOVED)
+ */
+#if LWIP_NETIF_REMOVE_CALLBACK
+static void _netif_remove_callback(struct netif *netif);
+#endif
+
+/**
+ * @brief Called when a link is brought up or down (ZTS_EVENT_NETIF_LINK_UP, ZTS_EVENT_NETIF_LINK_DOWN)
+ */
+#if LWIP_NETIF_LINK_CALLBACK
+static void _netif_link_callback(struct netif *netif);
+#endif
+
+/**
+ * @brief Set up an interface in the network stack for the VirtualTap.
+ *
+ * @param
+ * @param tapref Reference to VirtualTap that will be responsible for sending and receiving data
+ * @param ip Virtual IP address for this ZeroTier VirtualTap interface
+ * @return
+ */
+void _lwip_init_interface(void *tapref, const InetAddress &ip);
+
+/**
+ * @brief Called from the stack, outbound ethernet frames from the network stack enter the ZeroTier virtual wire here.
+ *
+ * @usage This shall only be called from the stack or the stack driver. Not the application thread.
+ * @param netif Transmits an outgoing Ethernet fram from the network stack onto the ZeroTier virtual wire
+ * @param p A pointer to the beginning of a chain pf struct pbufs
+ * @return
+ */
+err_t _lwip_eth_tx(struct netif *netif, struct pbuf *p);
+
+/**
+ * @brief Receives incoming Ethernet frames from the ZeroTier virtual wire
+ *
+ * @usage This shall be called from the VirtualTap's I/O thread (via VirtualTap::put())
+ * @param tap Pointer to VirtualTap from which this data comes
+ * @param from Origin address (virtual ZeroTier hardware address)
+ * @param to Intended destination address (virtual ZeroTier hardware address)
+ * @param etherType Protocol type
+ * @param data Pointer to Ethernet frame
+ * @param len Length of Ethernet frame
+ * @return
+ */
+void _lwip_eth_rx(VirtualTap *tap, const MAC &from, const MAC &to, unsigned int etherType,
+ const void *data, unsigned int len);
+
} // namespace ZeroTier
#endif // _H
+