From a86300c58fe29e9a8626f935f0b3ed25e844b0aa Mon Sep 17 00:00:00 2001
From: Adam Ierymenko <adam.ierymenko@gmail.com>
Date: Mon, 6 Apr 2015 15:47:57 -0700
Subject: [PATCH] Network build fixes and cleanup of remaining internal
 references to _tap

---
 include/ZeroTierOne.h   | 11 ++++++++---
 node/MAC.hpp            | 11 -----------
 node/MulticastGroup.hpp |  2 +-
 node/Network.cpp        | 23 ++++++++++-------------
 node/Network.hpp        | 20 +++-----------------
 5 files changed, 22 insertions(+), 45 deletions(-)

diff --git a/include/ZeroTierOne.h b/include/ZeroTierOne.h
index 41efcee5a..298b8157c 100644
--- a/include/ZeroTierOne.h
+++ b/include/ZeroTierOne.h
@@ -252,12 +252,12 @@ enum ZT1_VirtualNetworkStatus
 	/**
 	 * Waiting for network configuration (also means revision == 0)
 	 */
-	ZT1_NETWORK_STATUS_WAITING = 0,
+	ZT1_NETWORK_STATUS_REQUESTING_CONFIGURATION = 0,
 
 	/**
 	 * Configuration received and we are authorized
 	 */
-	ZT1_NETWORK_STATUS_AUTHORIZED = 1,
+	ZT1_NETWORK_STATUS_OK = 1,
 
 	/**
 	 * Netconf master told us 'nope'
@@ -267,7 +267,12 @@ enum ZT1_VirtualNetworkStatus
 	/**
 	 * Netconf master exists, but this virtual network does not
 	 */
-	ZT1_NETWORK_STATUS_NOT_FOUND = 3
+	ZT1_NETWORK_STATUS_NOT_FOUND = 3,
+
+	/**
+	 * Initialization of network failed or other internal error
+	 */
+	ZT1_NETWORK_STATUS_INITIALIZATION_FAILED = 4
 };
 
 /**
diff --git a/node/MAC.hpp b/node/MAC.hpp
index 3981c77d8..a377ddfd7 100644
--- a/node/MAC.hpp
+++ b/node/MAC.hpp
@@ -48,17 +48,6 @@ public:
 	MAC() throw() : _m(0ULL) {}
 	MAC(const MAC &m) throw() : _m(m._m) {}
 
-	/**
-	 * @param octet Single octet to fill entire MAC with (e.g. 0xff for broadcast)
-	 */
-	MAC(const unsigned char octet) throw() :
-		_m( ((((uint64_t)octet) & 0xffULL) << 40) |
-		    ((((uint64_t)octet) & 0xffULL) << 32) |
-		    ((((uint64_t)octet) & 0xffULL) << 24) |
-		    ((((uint64_t)octet) & 0xffULL) << 16) |
-		    ((((uint64_t)octet) & 0xffULL) << 8) |
-		    (((uint64_t)octet) & 0xffULL) ) {}
-
 	MAC(const unsigned char a,const unsigned char b,const unsigned char c,const unsigned char d,const unsigned char e,const unsigned char f) throw() :
 		_m( ((((uint64_t)a) & 0xffULL) << 40) |
 		    ((((uint64_t)b) & 0xffULL) << 32) |
diff --git a/node/MulticastGroup.hpp b/node/MulticastGroup.hpp
index 858554383..8bc65f1e8 100644
--- a/node/MulticastGroup.hpp
+++ b/node/MulticastGroup.hpp
@@ -93,7 +93,7 @@ public:
 			// the Multicast Group ADI field. Making V4 ARP work is basically why
 			// ADI was added, as well as handling other things that want mindless
 			// Ethernet broadcast to all.
-			return MulticastGroup(MAC((unsigned char)0xff),Utils::ntoh(*((const uint32_t *)ip.rawIpData())));
+			return MulticastGroup(MAC(0xffffffffffffULL),Utils::ntoh(*((const uint32_t *)ip.rawIpData())));
 		} else if (ip.isV6()) {
 			// IPv6 is better designed in this respect. We can compute the IPv6
 			// multicast address directly from the IP address, and it gives us
diff --git a/node/Network.cpp b/node/Network.cpp
index c81504898..dc6b87a04 100644
--- a/node/Network.cpp
+++ b/node/Network.cpp
@@ -40,7 +40,7 @@
 
 namespace ZeroTier {
 
-const ZeroTier::MulticastGroup Network::BROADCAST(ZeroTier::MAC(0xff),0);
+const ZeroTier::MulticastGroup Network::BROADCAST(ZeroTier::MAC(0xffffffffffffULL),0);
 
 Network::Network(const RuntimeEnvironment *renv,uint64_t nwid) :
 	RR(renv),
@@ -113,7 +113,7 @@ Network::~Network()
 		Utils::snprintf(n,sizeof(n),"networks.d/%.16llx.mcerts",_id);
 		Mutex::Lock _l(_lock);
 
-		if ((!_config)||(_config.isPublic())||(_membershipCertificates.size() == 0)) {
+		if ((!_config)||(_config->isPublic())||(_membershipCertificates.size() == 0)) {
 			RR->node->dataStoreDelete(n);
 			return;
 		}
@@ -141,8 +141,8 @@ public:
 		if ( ( (p->hasActiveDirectPath(_now)) && (_network->isAllowed(p->address())) ) || (std::find(_supernodeAddresses.begin(),_supernodeAddresses.end(),p->address()) != _supernodeAddresses.end()) ) {
 			Packet outp(p->address(),RR->identity.address(),Packet::VERB_MULTICAST_LIKE);
 
-			std::set<MulticastGroup> mgs(_network->multicastGroups());
-			for(std::set<MulticastGroup>::iterator mg(mgs.begin());mg!=mgs.end();++mg) {
+			std::vector<MulticastGroup> mgs(_network->multicastGroups());
+			for(std::vector<MulticastGroup>::iterator mg(mgs.begin());mg!=mgs.end();++mg) {
 				if ((outp.size() + 18) > ZT_UDP_DEFAULT_PAYLOAD_MTU) {
 					outp.armor(p->key(),true);
 					p->send(RR,outp.data(),outp.size(),_now);
@@ -194,7 +194,7 @@ bool Network::applyConfiguration(const SharedPtr<NetworkConfig> &conf)
 int Network::setConfiguration(const Dictionary &conf,bool saveToDisk)
 {
 	try {
-		SharedPtr<NetworkConfig> newConfig(new NetworkConfig(conf)); // throws if invalid
+		const SharedPtr<NetworkConfig> newConfig(new NetworkConfig(conf)); // throws if invalid
 		{
 			Mutex::Lock _l(_lock);
 			if ((_config)&&(*_config == *newConfig))
@@ -368,19 +368,18 @@ void Network::clean()
 	}
 }
 
-Network::Status Network::status() const
+ZT1_VirtualNetworkStatus Network::status() const
 {
 	Mutex::Lock _l(_lock);
 	switch(_netconfFailure) {
 		case NETCONF_FAILURE_ACCESS_DENIED:
-			return NETWORK_ACCESS_DENIED;
+			return ZT1_NETWORK_STATUS_ACCESS_DENIED;
 		case NETCONF_FAILURE_NOT_FOUND:
-			return NETWORK_NOT_FOUND;
+			return ZT1_NETWORK_STATUS_NOT_FOUND;
 		case NETCONF_FAILURE_NONE:
-			return ((_lastConfigUpdate > 0) ? ((_tap) ? NETWORK_OK : NETWORK_INITIALIZING) : NETWORK_WAITING_FOR_FIRST_AUTOCONF);
-		//case NETCONF_FAILURE_INIT_FAILED:
+			return ((_lastConfigUpdate > 0) ? ZT1_NETWORK_STATUS_OK : ZT1_NETWORK_STATUS_REQUESTING_CONFIGURATION);
 		default:
-			return NETWORK_INITIALIZATION_FAILED;
+			return ZT1_NETWORK_STATUS_INITIALIZATION_FAILED;
 	}
 }
 
@@ -413,8 +412,6 @@ void Network::setEnabled(bool enabled)
 {
 	Mutex::Lock _l(_lock);
 	_enabled = enabled;
-	if (_tap)
-		_tap->setEnabled(enabled);
 }
 
 void Network::destroy()
diff --git a/node/Network.hpp b/node/Network.hpp
index c9e621289..5693ff49f 100644
--- a/node/Network.hpp
+++ b/node/Network.hpp
@@ -30,6 +30,8 @@
 
 #include <stdint.h>
 
+#include "../include/ZeroTierOne.h"
+
 #include <string>
 #include <map>
 #include <vector>
@@ -46,8 +48,6 @@
 #include "MulticastGroup.hpp"
 #include "MAC.hpp"
 #include "Dictionary.hpp"
-#include "Identity.hpp"
-#include "InetAddress.hpp"
 #include "BandwidthAccount.hpp"
 #include "Multicaster.hpp"
 #include "NetworkConfig.hpp"
@@ -78,20 +78,6 @@ public:
 	 */
 	static const MulticastGroup BROADCAST;
 
-	/**
-	 * Possible network states
-	 */
-	enum Status
-	{
-		NETWORK_INITIALIZING = 0,               // Creating tap device and setting up state
-		NETWORK_WAITING_FOR_FIRST_AUTOCONF = 1, // Waiting for initial setup with netconf master
-		NETWORK_OK = 2,                         // Network is up, seems to be working
-		NETWORK_ACCESS_DENIED = 3,              // Netconf node reported permission denied
-		NETWORK_NOT_FOUND = 4,                  // Netconf node reported network not found
-		NETWORK_INITIALIZATION_FAILED = 5,      // Cannot initialize device (OS/installation problem?)
-		NETWORK_NO_MORE_DEVICES = 6             // OS cannot create any more tap devices (some OSes have a limit)
-	};
-
 	/**
 	 * @return Network ID
 	 */
@@ -206,7 +192,7 @@ public:
 	/**
 	 * @return Status of this network
 	 */
-	Status status() const;
+	ZT1_VirtualNetworkStatus status() const;
 
 	/**
 	 * Update and check multicast rate balance for a multicast group