Browse Source

Plumb injectPacketFromHost through to API.

Adam Ierymenko 10 years ago
parent
commit
351db7f1a0
5 changed files with 82 additions and 1 deletions
  1. 18 0
      node/Network.hpp
  2. 19 0
      node/NetworkConfig.cpp
  3. 12 1
      node/NetworkConfig.hpp
  4. 16 0
      node/Node.cpp
  5. 17 0
      node/Node.hpp

+ 18 - 0
node/Network.hpp

@@ -328,6 +328,24 @@ public:
 			t->put(from,to,etherType,data,len);
 	}
 
+	/**
+	 * Call injectPacketFromHost() on tap if it exists
+	 *
+	 * @param from Source MAC
+	 * @param to Destination MAC
+	 * @param etherType Ethernet frame type
+	 * @param data Packet data
+	 * @param len Packet length
+	 */
+	inline bool tapInjectPacketFromHost(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len)
+	{
+		Mutex::Lock _l(_lock);
+		EthernetTap *t = _tap;
+		if (t)
+			return t->injectPacketFromHost(from,to,etherType,data,len);
+		return false;
+	}
+
 	/**
 	 * @return Tap device name or empty string if still initializing
 	 */

+ 19 - 0
node/NetworkConfig.cpp

@@ -35,6 +35,25 @@ namespace ZeroTier {
 // a starting and max balance of 64k.
 const NetworkConfig::MulticastRate NetworkConfig::DEFAULT_MULTICAST_RATE(32768,32768,64);
 
+SharedPtr<NetworkConfig> NetworkConfig::createTestNetworkConfig(const Address &self)
+{
+	SharedPtr<NetworkConfig> nc(new NetworkConfig());
+
+	memset(nc->_etWhitelist,0,sizeof(nc->_etWhitelist));
+	nc->_etWhitelist[0] |= 1; // allow all
+	nc->_nwid = ZT_TEST_NETWORK_ID;
+	nc->_timestamp = Utils::now();
+	nc->_issuedTo = self;
+	nc->_multicastLimit = ZT_MULTICAST_DEFAULT_LIMIT;
+	nc->_allowPassiveBridging = false;
+	nc->_private = false;
+	nc->_enableBroadcast = true;
+	nc->_name = "ZT_TEST_NETWORK";
+	nc->_description = "Built-in dummy test network";
+
+	return nc;
+}
+
 std::set<unsigned int> NetworkConfig::allowedEtherTypes() const
 {
 	std::set<unsigned int> ets;

+ 12 - 1
node/NetworkConfig.hpp

@@ -71,9 +71,9 @@ namespace ZeroTier {
  */
 class NetworkConfig
 {
-public:
 	friend class SharedPtr<NetworkConfig>;
 
+public:
 	/**
 	 * Tuple of multicast rate parameters
 	 */
@@ -91,6 +91,17 @@ public:
 	 */
 	static const MulticastRate DEFAULT_MULTICAST_RATE;
 
+	/**
+	 * Create an instance of a NetworkConfig for the test network ID
+	 *
+	 * The test network ID is defined as ZT_TEST_NETWORK_ID. This is a
+	 * "fake" network with no real netconf master and default options.
+	 *
+	 * @param self This node's ZT address
+	 * @return Configured instance of netconf for test network ID
+	 */
+	static SharedPtr<NetworkConfig> createTestNetworkConfig(const Address &self);
+
 	/**
 	 * @param d Dictionary containing configuration
 	 * @throws std::invalid_argument Invalid configuration

+ 16 - 0
node/Node.cpp

@@ -1011,6 +1011,22 @@ bool Node::updateCheck()
 	return false;
 }
 
+bool Node::injectPacketFromHost(uint64_t nwid,const unsigned char *from,const unsigned char *to,unsigned int etherType,const void *data,unsigned int len)
+{
+	if (!running())
+		return false;
+	if ((!from)||(!to))
+		return false;
+
+	_NodeImpl *impl = (_NodeImpl *)_impl;
+	RuntimeEnvironment *RR = (RuntimeEnvironment *)&(impl->renv);
+
+	SharedPtr<Network> network(RR->nc->network(nwid));
+	if (network)
+		return network->tapInjectPacketFromHost(MAC(from,6),MAC(to,6),etherType,data,len);
+	return false;
+}
+
 class _VersionStringMaker
 {
 public:

+ 17 - 0
node/Node.hpp

@@ -227,6 +227,23 @@ public:
 	bool updateCheck()
 		throw();
 
+	/**
+	 * Inject a packet into a network's tap as if it came from the host
+	 *
+	 * This is primarily for debugging, and at the moment is only supported on
+	 * the test/dummy Ethernet tap implementation. Attempting to use it for real
+	 * devices will fail and return 'false.'
+	 *
+	 * @param nwid Network ID
+	 * @param from Source MAC address (must be 6 bytes in length)
+	 * @param to Destination MAC address (must be 6 bytes in length)
+	 * @param etherType Ethernet frame type
+	 * @param data Frame data
+	 * @param len Length of frame in bytes
+	 * @return True on success; false if not a member of network, injection not supported, or data too large
+	 */
+	bool injectPacketFromHost(uint64_t nwid,const unsigned char *from,const unsigned char *to,unsigned int etherType,const void *data,unsigned int len);
+
 	static const char *versionString() throw();
 	static unsigned int versionMajor() throw();
 	static unsigned int versionMinor() throw();