Browse Source

Make tap itself handle remembering sticky device to nwid mappings.

Adam Ierymenko 10 years ago
parent
commit
aeb4b42ab3
3 changed files with 27 additions and 16 deletions
  1. 3 0
      include/ZeroTierOne.h
  2. 23 15
      osdep/OSXEthernetTap.cpp
  3. 1 1
      service/One.cpp

+ 3 - 0
include/ZeroTierOne.h

@@ -448,6 +448,9 @@ typedef struct
 
 	/**
 	 * If nonzero, this network supports and allows broadcast (ff:ff:ff:ff:ff:ff) traffic
+	 *
+	 * This is really just a hint to user code. If this is true, the user can
+	 * subscribe to the broadcast group. If not, then the user shouldn't.
 	 */
 	int broadcastEnabled;
 

+ 23 - 15
osdep/OSXEthernetTap.cpp

@@ -263,6 +263,7 @@ static inline void _intl_freeifmaddrs(struct _intl_ifmaddrs *ifmp)
 #include "../node/Constants.hpp"
 #include "../node/Utils.hpp"
 #include "../node/Mutex.hpp"
+#include "../node/Dictionary.hpp"
 #include "OSUtils.hpp"
 #include "OSXEthernetTap.hpp"
 
@@ -333,14 +334,16 @@ OSXEthernetTap::OSXEthernetTap(
 	_fd(0),
 	_enabled(true)
 {
-	char devpath[64],ethaddr[64],mtustr[32],metstr[32];
+	char devpath[64],ethaddr[64],mtustr[32],metstr[32],nwids[32];
 	struct stat stattmp;
 
-	Mutex::Lock _gl(globalTapCreateLock);
+	Utils::snprintf(nwids,sizeof(nwids),"%.16llx",nwid);
 
 	if (mtu > 2800)
 		throw std::runtime_error("max tap MTU is 2800");
 
+	Mutex::Lock _gl(globalTapCreateLock);
+
 	if (stat("/dev/zt0",&stattmp)) {
 		if (homePath) {
 			long kextpid = (long)vfork();
@@ -362,20 +365,22 @@ OSXEthernetTap::OSXEthernetTap(
 
 	// Try to reopen the last device we had, if we had one and it's still unused.
 	bool recalledDevice = false;
-	/*
-	if ((desiredDevice)&&(desiredDevice[0] == 'z')&&(desiredDevice[1] == 't')) {
-		if ((strchr(desiredDevice,'/'))||(strchr(desiredDevice,'.'))) // security sanity check
-			throw std::runtime_error("invalid desiredDevice parameter");
-		Utils::snprintf(devpath,sizeof(devpath),"/dev/%s",desiredDevice);
-		if (stat(devpath,&stattmp) == 0) {
-			_fd = ::open(devpath,O_RDWR);
-			if (_fd > 0) {
-				_dev = desiredDevice;
-				recalledDevice = true;
+	std::string devmapbuf;
+	Dictionary devmap;
+	if (OSUtils::readFile((_homePath + ZT_PATH_SEPARATOR_S + "devicemap").c_str(),devmapbuf)) {
+		devmap.fromString(devmapbuf);
+		std::string desiredDevice(devmap.get(nwids,""));
+		if (desiredDevice.length() > 2) {
+			Utils::snprintf(devpath,sizeof(devpath),"/dev/%s",desiredDevice.c_str());
+			if (stat(devpath,&stattmp) == 0) {
+				_fd = ::open(devpath,O_RDWR);
+				if (_fd > 0) {
+					_dev = desiredDevice;
+					recalledDevice = true;
+				}
 			}
 		}
 	}
-	*/
 
 	// Open the first unused tap device if we didn't recall a previous one.
 	if (!recalledDevice) {
@@ -425,9 +430,12 @@ OSXEthernetTap::OSXEthernetTap(
 
 	::pipe(_shutdownSignalPipe);
 
-	_thread = Thread::start(this);
-
 	++globalTapsRunning;
+
+	devmap[nwids] = _dev;
+	OSUtils::writeFile((_homePath + ZT_PATH_SEPARATOR_S + "devicemap").c_str(),devmap.toString());
+
+	_thread = Thread::start(this);
 }
 
 OSXEthernetTap::~OSXEthernetTap()

+ 1 - 1
service/One.cpp

@@ -409,7 +409,7 @@ public:
 							StapFrameHandler,
 							(void *)this))).first;
 					} catch ( ... ) {
-						return -2;
+						return -999;
 					}
 				}
 				// fall through...