From 3f912eb4ada67610eee21d3749ba7d5cfb083359 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Tue, 11 Feb 2014 14:21:59 -0800 Subject: Fix for GitHub issue #37: remember *nix device names. --- node/Network.cpp | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) (limited to 'node/Network.cpp') diff --git a/node/Network.cpp b/node/Network.cpp index db252f73..19190207 100644 --- a/node/Network.cpp +++ b/node/Network.cpp @@ -75,7 +75,7 @@ Network::~Network() } } -SharedPtr Network::newInstance(const RuntimeEnvironment *renv,uint64_t id) +SharedPtr Network::newInstance(const RuntimeEnvironment *renv,NodeConfig *nc,uint64_t id) { /* We construct Network via a static method to ensure that it is immediately * wrapped in a SharedPtr<>. Otherwise if there is traffic on the Ethernet @@ -85,6 +85,7 @@ SharedPtr Network::newInstance(const RuntimeEnvironment *renv,uint64_t SharedPtr nw(new Network()); nw->_id = id; + nw->_nc = nc; nw->_mac = renv->identity.address().toMAC(); nw->_r = renv; nw->_tap = (EthernetTap *)0; @@ -269,12 +270,31 @@ void Network::_pushMembershipCertificate(const Address &peer,bool force,uint64_t void Network::threadMain() throw() { + // Setup thread -- this exits when tap is constructed. It's here + // because opening the tap can take some time on some platforms. + try { - // Setup thread -- this exits when tap is constructed. It's here - // because opening the tap can take some time on some platforms. - char tag[32]; +#ifdef __WINDOWS__ + // Windows tags interfaces by their network IDs, which are shoved into the + // registry to mark persistent instance of the tap device. + char tag[24]; Utils::snprintf(tag,sizeof(tag),"%.16llx",(unsigned long long)_id); +#else + // Unix tries to get the same device name next time, if possible. + std::string tagstr; + char lcentry[128]; + Utils::snprintf(lcentry,sizeof(lcentry),"_dev_for_%.16llx",(unsigned long long)_id); + tagstr = _nc->getLocalConfig(lcentry); + const char *tag = (tagstr.length() > 0) ? tagstr.c_str() : (const char *)0; +#endif + _tap = new EthernetTap(_r,tag,_mac,ZT_IF_MTU,&_CBhandleTapData,this); + +#ifndef __WINDOWS__ + std::string dn(_tap->deviceName()); + if ((!tag)||(dn != tag)) + _nc->putLocalConfig(lcentry,dn); +#endif } catch (std::exception &exc) { LOG("network %.16llx failed to initialize: %s",_id,exc.what()); _netconfFailure = NETCONF_FAILURE_INIT_FAILED; -- cgit v1.2.3