Utils::now() removal and a bunch of compile fixes.

This commit is contained in:
Adam Ierymenko 2015-04-08 15:26:45 -07:00
parent ee2f51f48e
commit bf2ff964e1
9 changed files with 67 additions and 49 deletions

View file

@ -69,11 +69,12 @@ public:
* @param preload Initial balance to place in account * @param preload Initial balance to place in account
* @param maxb Maximum allowed balance (> 0) * @param maxb Maximum allowed balance (> 0)
* @param acc Rate of accrual in bytes per second * @param acc Rate of accrual in bytes per second
* @param now Current time
*/ */
BandwidthAccount(uint32_t preload,uint32_t maxb,uint32_t acc) BandwidthAccount(uint32_t preload,uint32_t maxb,uint32_t acc,uint64_t now)
throw() throw()
{ {
init(preload,maxb,acc); init(preload,maxb,acc,now);
} }
/** /**
@ -82,11 +83,12 @@ public:
* @param preload Initial balance to place in account * @param preload Initial balance to place in account
* @param maxb Maximum allowed balance (> 0) * @param maxb Maximum allowed balance (> 0)
* @param acc Rate of accrual in bytes per second * @param acc Rate of accrual in bytes per second
* @param now Current time
*/ */
inline void init(uint32_t preload,uint32_t maxb,uint32_t acc) inline void init(uint32_t preload,uint32_t maxb,uint32_t acc,uint64_t now)
throw() throw()
{ {
_lastTime = Utils::nowf(); _lastTime = ((double)now / 1000.0);
_balance = preload; _balance = preload;
_maxBalance = maxb; _maxBalance = maxb;
_accrual = acc; _accrual = acc;
@ -95,15 +97,16 @@ public:
/** /**
* Update and retrieve balance of this account * Update and retrieve balance of this account
* *
* @param now Current time
* @return New balance updated from current clock * @return New balance updated from current clock
*/ */
inline uint32_t update() inline uint32_t update(uint64_t now)
throw() throw()
{ {
double lt = _lastTime; double lt = _lastTime;
double now = Utils::nowf(); double nowf = ((double)now / 1000.0);
_lastTime = now; _lastTime = nowf;
return (_balance = std::min(_maxBalance,(uint32_t)round((double)_balance + ((double)_accrual * (now - lt))))); return (_balance = std::min(_maxBalance,(uint32_t)round((double)_balance + ((double)_accrual * (nowf - lt)))));
} }
/** /**
@ -113,12 +116,13 @@ public:
* balance is updated and false is returned. * balance is updated and false is returned.
* *
* @param amt Amount to deduct * @param amt Amount to deduct
* @param now Current time
* @return True if amount fit within balance and was deducted * @return True if amount fit within balance and was deducted
*/ */
inline bool deduct(uint32_t amt) inline bool deduct(uint32_t amt,uint64_t now)
throw() throw()
{ {
if (update() >= amt) { if (update(now) >= amt) {
_balance -= amt; _balance -= amt;
return true; return true;
} }

View file

@ -75,13 +75,13 @@ void Dictionary::fromString(const char *s,unsigned int maxlen)
(*this)[keyBuf]; (*this)[keyBuf];
} }
bool Dictionary::sign(const Identity &id) bool Dictionary::sign(const Identity &id,uint64_t now)
{ {
try { try {
// Sign identity and timestamp fields too. If there's an existing // Sign identity and timestamp fields too. If there's an existing
// signature, _mkSigBuf() ignores it. // signature, _mkSigBuf() ignores it.
char nows[32]; char nows[32];
Utils::snprintf(nows,sizeof(nows),"%llx",(unsigned long long)Utils::now()); Utils::snprintf(nows,sizeof(nows),"%llx",(unsigned long long)now);
(*this)[ZT_DICTIONARY_SIGNATURE_IDENTITY] = id.toString(false); (*this)[ZT_DICTIONARY_SIGNATURE_IDENTITY] = id.toString(false);
(*this)[ZT_DICTIONARY_SIGNATURE_TIMESTAMP] = nows; (*this)[ZT_DICTIONARY_SIGNATURE_TIMESTAMP] = nows;

View file

@ -289,9 +289,10 @@ public:
* Add or update signature fields with a signature of all other keys and values * Add or update signature fields with a signature of all other keys and values
* *
* @param with Identity to sign with (must have secret key) * @param with Identity to sign with (must have secret key)
* @param now Current time
* @return True on success * @return True on success
*/ */
bool sign(const Identity &id); bool sign(const Identity &id,uint64_t now);
/** /**
* Verify signature against an identity * Verify signature against an identity

View file

@ -74,11 +74,12 @@ public:
* @param len Packet length * @param len Packet length
* @param remoteAddress Address from which packet came * @param remoteAddress Address from which packet came
* @param linkDesperation Link desperation for link over which packet was received * @param linkDesperation Link desperation for link over which packet was received
* @param now Current time
* @throws std::out_of_range Range error processing packet * @throws std::out_of_range Range error processing packet
*/ */
IncomingPacket(const void *data,unsigned int len,const InetAddress &remoteAddress,unsigned int linkDesperation) : IncomingPacket(const void *data,unsigned int len,const InetAddress &remoteAddress,unsigned int linkDesperation,uint64_t now) :
Packet(data,len), Packet(data,len),
_receiveTime(Utils::now()), _receiveTime(now),
_remoteAddress(remoteAddress), _remoteAddress(remoteAddress),
_linkDesperation(linkDesperation), _linkDesperation(linkDesperation),
__refCount() __refCount()

View file

@ -350,7 +350,7 @@ bool Network::isAllowed(const Address &peer) const
void Network::clean() void Network::clean()
{ {
uint64_t now = Utils::now(); const uint64_t now = RR->node->now();
Mutex::Lock _l(_lock); Mutex::Lock _l(_lock);
if (_destroyed) if (_destroyed)
@ -386,6 +386,20 @@ void Network::clean()
} }
} }
bool Network::updateAndCheckMulticastBalance(const MulticastGroup &mg,unsigned int bytes)
{
const uint64_t now = RR->node->now();
Mutex::Lock _l(_lock);
if (!_config)
return false;
std::map< MulticastGroup,BandwidthAccount >::iterator bal(_multicastRateAccounts.find(mg));
if (bal == _multicastRateAccounts.end()) {
NetworkConfig::MulticastRate r(_config->multicastRate(mg));
bal = _multicastRateAccounts.insert(std::pair< MulticastGroup,BandwidthAccount >(mg,BandwidthAccount(r.preload,r.maxBalance,r.accrual,now))).first;
}
return bal->second.deduct(bytes,now);
}
void Network::learnBridgeRoute(const MAC &mac,const Address &addr) void Network::learnBridgeRoute(const MAC &mac,const Address &addr)
{ {
Mutex::Lock _l(_lock); Mutex::Lock _l(_lock);
@ -488,7 +502,7 @@ class _AnnounceMulticastGroupsToPeersWithActiveDirectPaths
public: public:
_AnnounceMulticastGroupsToPeersWithActiveDirectPaths(const RuntimeEnvironment *renv,Network *nw) : _AnnounceMulticastGroupsToPeersWithActiveDirectPaths(const RuntimeEnvironment *renv,Network *nw) :
RR(renv), RR(renv),
_now(Utils::now()), _now(renv->node->now()),
_network(nw), _network(nw),
_supernodeAddresses(renv->topology->supernodeAddresses()) _supernodeAddresses(renv->topology->supernodeAddresses())
{} {}

View file

@ -40,7 +40,6 @@
#include "Constants.hpp" #include "Constants.hpp"
#include "NonCopyable.hpp" #include "NonCopyable.hpp"
#include "Utils.hpp"
#include "Address.hpp" #include "Address.hpp"
#include "Mutex.hpp" #include "Mutex.hpp"
#include "SharedPtr.hpp" #include "SharedPtr.hpp"
@ -230,18 +229,7 @@ public:
* @param bytes Size of packet * @param bytes Size of packet
* @return True if packet is within budget * @return True if packet is within budget
*/ */
inline bool updateAndCheckMulticastBalance(const MulticastGroup &mg,unsigned int bytes) bool updateAndCheckMulticastBalance(const MulticastGroup &mg,unsigned int bytes);
{
Mutex::Lock _l(_lock);
if (!_config)
return false;
std::map< MulticastGroup,BandwidthAccount >::iterator bal(_multicastRateAccounts.find(mg));
if (bal == _multicastRateAccounts.end()) {
NetworkConfig::MulticastRate r(_config->multicastRate(mg));
bal = _multicastRateAccounts.insert(std::pair< MulticastGroup,BandwidthAccount >(mg,BandwidthAccount(r.preload,r.maxBalance,r.accrual))).first;
}
return bal->second.deduct(bytes);
}
/** /**
* Get current network config or throw exception * Get current network config or throw exception

View file

@ -43,7 +43,7 @@ SharedPtr<NetworkConfig> NetworkConfig::createTestNetworkConfig(const Address &s
memset(nc->_etWhitelist,0,sizeof(nc->_etWhitelist)); memset(nc->_etWhitelist,0,sizeof(nc->_etWhitelist));
nc->_etWhitelist[0] |= 1; // allow all nc->_etWhitelist[0] |= 1; // allow all
nc->_nwid = ZT_TEST_NETWORK_ID; nc->_nwid = ZT_TEST_NETWORK_ID;
nc->_timestamp = Utils::now(); nc->_timestamp = 1;
nc->_revision = 1; nc->_revision = 1;
nc->_issuedTo = self; nc->_issuedTo = self;
nc->_multicastLimit = ZT_MULTICAST_DEFAULT_LIMIT; nc->_multicastLimit = ZT_MULTICAST_DEFAULT_LIMIT;

View file

@ -106,7 +106,7 @@ void OutboundMulticast::sendOnly(const RuntimeEnvironment *RR,const Address &toA
{ {
if (_haveCom) { if (_haveCom) {
SharedPtr<Network> network(RR->nc->network(_nwid)); SharedPtr<Network> network(RR->nc->network(_nwid));
if (network->peerNeedsOurMembershipCertificate(toAddr,Utils::now())) { if (network->peerNeedsOurMembershipCertificate(toAddr,RR->node->now())) {
_packetWithCom.newInitializationVector(); _packetWithCom.newInitializationVector();
_packetWithCom.setDestination(toAddr); _packetWithCom.setDestination(toAddr);
//TRACE(">>MC %.16llx -> %s (with COM)",(unsigned long long)this,toAddr.toString().c_str()); //TRACE(">>MC %.16llx -> %s (with COM)",(unsigned long long)this,toAddr.toString().c_str());

View file

@ -91,7 +91,7 @@ void Switch::onLocalEthernet(const SharedPtr<Network> &network,const MAC &from,c
* Note: even when we introduce a more purposeful binding of the main UDP port, this can * Note: even when we introduce a more purposeful binding of the main UDP port, this can
* still happen because Windows likes to send broadcasts over interfaces that have little * still happen because Windows likes to send broadcasts over interfaces that have little
* to do with their intended target audience. :P */ * to do with their intended target audience. :P */
if (!RR->antiRec->checkEthernetFrame(data.data(),data.size())) { if (!RR->antiRec->checkEthernetFrame(data,len)) {
TRACE("%.16llx: rejected recursively addressed ZeroTier packet by tail match (type %s, length: %u)",network->id(),etherTypeName(etherType),data.size()); TRACE("%.16llx: rejected recursively addressed ZeroTier packet by tail match (type %s, length: %u)",network->id(),etherTypeName(etherType),data.size());
return; return;
} }
@ -115,14 +115,24 @@ void Switch::onLocalEthernet(const SharedPtr<Network> &network,const MAC &from,c
if (to.isMulticast()) { if (to.isMulticast()) {
// Destination is a multicast address (including broadcast) // Destination is a multicast address (including broadcast)
uint64_t now = Utils::now(); const uint64_t now = RR->node->now();
MulticastGroup mg(to,0); MulticastGroup mg(to,0);
if (to.isBroadcast()) { if (to.isBroadcast()) {
if ((etherType == ZT_ETHERTYPE_ARP)&&(data.size() >= 28)&&(data[2] == 0x08)&&(data[3] == 0x00)&&(data[4] == 6)&&(data[5] == 4)&&(data[7] == 0x01)) { if (
(etherType == ZT_ETHERTYPE_ARP)&&
(len >= 28)&&
(
(((const unsigned char *)data)[2] == 0x08)&&
(((const unsigned char *)data)[3] == 0x00)&&
(((const unsigned char *)data)[4] == 6)&&
(((const unsigned char *)data)[5] == 4)&&
(((const unsigned char *)data)[7] == 0x01)
)
) {
// Cram IPv4 IP into ADI field to make IPv4 ARP broadcast channel specific and scalable // Cram IPv4 IP into ADI field to make IPv4 ARP broadcast channel specific and scalable
// Also: enableBroadcast() does not apply to ARP since it's required for IPv4 // Also: enableBroadcast() does not apply to ARP since it's required for IPv4
mg = MulticastGroup::deriveMulticastGroupForAddressResolution(InetAddress(data.field(24,4),4,0)); mg = MulticastGroup::deriveMulticastGroupForAddressResolution(InetAddress(((const unsigned char *)data) + 24,4,0));
} else if (!nconf->enableBroadcast()) { } else if (!nconf->enableBroadcast()) {
// Don't transmit broadcasts if this network doesn't want them // Don't transmit broadcasts if this network doesn't want them
TRACE("%.16llx: dropped broadcast since ff:ff:ff:ff:ff:ff is not enabled",network->id()); TRACE("%.16llx: dropped broadcast since ff:ff:ff:ff:ff:ff is not enabled",network->id());
@ -138,7 +148,7 @@ void Switch::onLocalEthernet(const SharedPtr<Network> &network,const MAC &from,c
network->learnBridgedMulticastGroup(mg,now); network->learnBridgedMulticastGroup(mg,now);
// Check multicast/broadcast bandwidth quotas and reject if quota exceeded // Check multicast/broadcast bandwidth quotas and reject if quota exceeded
if (!network->updateAndCheckMulticastBalance(mg,data.size())) { if (!network->updateAndCheckMulticastBalance(mg,len)) {
TRACE("%.16llx: didn't multicast %d bytes, quota exceeded for multicast group %s",network->id(),(int)data.size(),mg.toString().c_str()); TRACE("%.16llx: didn't multicast %d bytes, quota exceeded for multicast group %s",network->id(),(int)data.size(),mg.toString().c_str());
return; return;
} }
@ -154,8 +164,8 @@ void Switch::onLocalEthernet(const SharedPtr<Network> &network,const MAC &from,c
mg, mg,
(fromBridged) ? from : MAC(), (fromBridged) ? from : MAC(),
etherType, etherType,
data.data(), data,
data.size()); len);
return; return;
} }
@ -165,7 +175,7 @@ void Switch::onLocalEthernet(const SharedPtr<Network> &network,const MAC &from,c
Address toZT(to.toAddress(network->id())); Address toZT(to.toAddress(network->id()));
if (network->isAllowed(toZT)) { if (network->isAllowed(toZT)) {
if (network->peerNeedsOurMembershipCertificate(toZT,Utils::now())) { if (network->peerNeedsOurMembershipCertificate(toZT,RR->node->now())) {
// TODO: once there are no more <1.0.0 nodes around, we can // TODO: once there are no more <1.0.0 nodes around, we can
// bundle this with EXT_FRAME instead of sending two packets. // bundle this with EXT_FRAME instead of sending two packets.
Packet outp(toZT,RR->identity.address(),Packet::VERB_NETWORK_MEMBERSHIP_CERTIFICATE); Packet outp(toZT,RR->identity.address(),Packet::VERB_NETWORK_MEMBERSHIP_CERTIFICATE);
@ -181,7 +191,7 @@ void Switch::onLocalEthernet(const SharedPtr<Network> &network,const MAC &from,c
to.appendTo(outp); to.appendTo(outp);
from.appendTo(outp); from.appendTo(outp);
outp.append((uint16_t)etherType); outp.append((uint16_t)etherType);
outp.append(data); outp.append(data,len);
outp.compress(); outp.compress();
send(outp,true); send(outp,true);
} else { } else {
@ -189,7 +199,7 @@ void Switch::onLocalEthernet(const SharedPtr<Network> &network,const MAC &from,c
Packet outp(toZT,RR->identity.address(),Packet::VERB_FRAME); Packet outp(toZT,RR->identity.address(),Packet::VERB_FRAME);
outp.append(network->id()); outp.append(network->id());
outp.append((uint16_t)etherType); outp.append((uint16_t)etherType);
outp.append(data); outp.append(data,len);
outp.compress(); outp.compress();
send(outp,true); send(outp,true);
} }
@ -245,7 +255,7 @@ void Switch::onLocalEthernet(const SharedPtr<Network> &network,const MAC &from,c
to.appendTo(outp); to.appendTo(outp);
from.appendTo(outp); from.appendTo(outp);
outp.append((uint16_t)etherType); outp.append((uint16_t)etherType);
outp.append(data); outp.append(data,len);
outp.compress(); outp.compress();
send(outp,true); send(outp,true);
} }
@ -261,7 +271,7 @@ void Switch::send(const Packet &packet,bool encrypt)
if (!_trySend(packet,encrypt)) { if (!_trySend(packet,encrypt)) {
Mutex::Lock _l(_txQueue_m); Mutex::Lock _l(_txQueue_m);
_txQueue.insert(std::pair< Address,TXQueueEntry >(packet.destination(),TXQueueEntry(Utils::now(),packet,encrypt))); _txQueue.insert(std::pair< Address,TXQueueEntry >(packet.destination(),TXQueueEntry(RR->node->now(),packet,encrypt)));
} }
} }
@ -277,7 +287,7 @@ bool Switch::unite(const Address &p1,const Address &p2,bool force)
if (!p2p) if (!p2p)
return false; return false;
uint64_t now = Utils::now(); const uint64_t now = RR->node->now();
std::pair<InetAddress,InetAddress> cg(Peer::findCommonGround(*p1p,*p2p,now)); std::pair<InetAddress,InetAddress> cg(Peer::findCommonGround(*p1p,*p2p,now));
if (!(cg.first)) if (!(cg.first))
@ -375,7 +385,7 @@ void Switch::requestWhois(const Address &addr)
Mutex::Lock _l(_outstandingWhoisRequests_m); Mutex::Lock _l(_outstandingWhoisRequests_m);
std::pair< std::map< Address,WhoisRequest >::iterator,bool > entry(_outstandingWhoisRequests.insert(std::pair<Address,WhoisRequest>(addr,WhoisRequest()))); std::pair< std::map< Address,WhoisRequest >::iterator,bool > entry(_outstandingWhoisRequests.insert(std::pair<Address,WhoisRequest>(addr,WhoisRequest())));
if ((inserted = entry.second)) if ((inserted = entry.second))
entry.first->second.lastSent = Utils::now(); entry.first->second.lastSent = RR->node->now();
entry.first->second.retries = 0; // reset retry count if entry already existed entry.first->second.retries = 0; // reset retry count if entry already existed
} }
if (inserted) if (inserted)
@ -597,7 +607,7 @@ void Switch::_handleRemotePacketFragment(const InetAddress &fromAddr,int linkDes
// We received a Packet::Fragment without its head, so queue it and wait // We received a Packet::Fragment without its head, so queue it and wait
DefragQueueEntry &dq = _defragQueue[pid]; DefragQueueEntry &dq = _defragQueue[pid];
dq.creationTime = Utils::now(); dq.creationTime = RR->node->now();
dq.frags[fno - 1] = fragment; dq.frags[fno - 1] = fragment;
dq.totalFragments = tf; // total fragment count is known dq.totalFragments = tf; // total fragment count is known
dq.haveFragments = 1 << fno; // we have only this fragment dq.haveFragments = 1 << fno; // we have only this fragment
@ -630,7 +640,7 @@ void Switch::_handleRemotePacketFragment(const InetAddress &fromAddr,int linkDes
void Switch::_handleRemotePacketHead(const InetAddress &fromAddr,int linkDesperation,const void *data,unsigned int len) void Switch::_handleRemotePacketHead(const InetAddress &fromAddr,int linkDesperation,const void *data,unsigned int len)
{ {
SharedPtr<IncomingPacket> packet(new IncomingPacket(data,len,fromAddr,linkDesperation)); SharedPtr<IncomingPacket> packet(new IncomingPacket(data,len,fromAddr,linkDesperation,RR->node->now()));
Address source(packet->source()); Address source(packet->source());
Address destination(packet->destination()); Address destination(packet->destination());
@ -664,7 +674,7 @@ void Switch::_handleRemotePacketHead(const InetAddress &fromAddr,int linkDespera
if (dqe == _defragQueue.end()) { if (dqe == _defragQueue.end()) {
// If we have no other fragments yet, create an entry and save the head // If we have no other fragments yet, create an entry and save the head
DefragQueueEntry &dq = _defragQueue[pid]; DefragQueueEntry &dq = _defragQueue[pid];
dq.creationTime = Utils::now(); dq.creationTime = RR->node->now();
dq.frag0 = packet; dq.frag0 = packet;
dq.totalFragments = 0; // 0 == unknown, waiting for Packet::Fragment dq.totalFragments = 0; // 0 == unknown, waiting for Packet::Fragment
dq.haveFragments = 1; // head is first bit (left to right) dq.haveFragments = 1; // head is first bit (left to right)