From 3f7e7e8a88e4edb0a62def5efbe204d22bbff7a4 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Fri, 3 Oct 2014 18:42:41 -0700 Subject: [PATCH] Do not multicast to self. --- node/IncomingPacket.cpp | 4 ++-- node/Multicaster.cpp | 28 ++++++++++++++++++---------- node/Multicaster.hpp | 3 ++- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/node/IncomingPacket.cpp b/node/IncomingPacket.cpp index 01db2db48..72ced7faf 100644 --- a/node/IncomingPacket.cpp +++ b/node/IncomingPacket.cpp @@ -925,7 +925,7 @@ bool IncomingPacket::_doMULTICAST_GATHER(const RuntimeEnvironment *RR,const Shar outp.append(nwid); mg.mac().appendTo(outp); outp.append((uint32_t)mg.adi()); - if (RR->mc->gather(RR,nwid,mg,outp,gatherLimit)) { + if (RR->mc->gather(RR,peer->address(),nwid,mg,outp,gatherLimit)) { outp.armor(peer->key(),true); _fromSock->send(_remoteAddress,outp.data(),outp.size()); } @@ -1003,7 +1003,7 @@ bool IncomingPacket::_doMULTICAST_FRAME(const RuntimeEnvironment *RR,const Share to.mac().appendTo(outp); outp.append((uint32_t)to.adi()); outp.append((unsigned char)0x01); // flag 0x01 = contains gather results - if (RR->mc->gather(RR,nwid,to,outp,gatherLimit)) { + if (RR->mc->gather(RR,peer->address(),nwid,to,outp,gatherLimit)) { outp.armor(peer->key(),true); _fromSock->send(_remoteAddress,outp.data(),outp.size()); } diff --git a/node/Multicaster.cpp b/node/Multicaster.cpp index cca9d0356..b3c0abd83 100644 --- a/node/Multicaster.cpp +++ b/node/Multicaster.cpp @@ -49,10 +49,10 @@ Multicaster::~Multicaster() { } -unsigned int Multicaster::gather(const RuntimeEnvironment *RR,uint64_t nwid,MulticastGroup &mg,Packet &appendTo,unsigned int limit) const +unsigned int Multicaster::gather(const RuntimeEnvironment *RR,const Address &queryingPeer,uint64_t nwid,MulticastGroup &mg,Packet &appendTo,unsigned int limit) const { unsigned char *p; - unsigned int n = 0,i,rptr; + unsigned int n = 0,i,rptr,skipped = 0; uint64_t a,done[(ZT_PROTO_MAX_PACKET_LENGTH / 5) + 1]; Mutex::Lock _l(_groups_m); @@ -90,16 +90,20 @@ restart_member_scan: // Log that we've picked this one done[n++] = a; - // Append to packet - p = (unsigned char *)appendTo.appendField(ZT_ADDRESS_LENGTH); - *(p++) = (unsigned char)((a >> 32) & 0xff); - *(p++) = (unsigned char)((a >> 24) & 0xff); - *(p++) = (unsigned char)((a >> 16) & 0xff); - *(p++) = (unsigned char)((a >> 8) & 0xff); - *p = (unsigned char)(a & 0xff); + if (queryingPeer.toInt() == a) { + ++skipped; + } else { + // Append to packet + p = (unsigned char *)appendTo.appendField(ZT_ADDRESS_LENGTH); + *(p++) = (unsigned char)((a >> 32) & 0xff); + *(p++) = (unsigned char)((a >> 24) & 0xff); + *(p++) = (unsigned char)((a >> 16) & 0xff); + *(p++) = (unsigned char)((a >> 8) & 0xff); + *p = (unsigned char)(a & 0xff); + } } - appendTo.setAt(nAt,(uint16_t)n); + appendTo.setAt(nAt,(uint16_t)(n - skipped)); return n; } @@ -249,6 +253,10 @@ void Multicaster::_add(uint64_t now,uint64_t nwid,MulticastGroupStatus &gs,const { // assumes _groups_m is locked + // Do not add self -- even if someone else returns it + if (member == RR->identity.address()) + return; + // Update timestamp and learnedFrom if existing for(std::vector::iterator m(gs.members.begin());m!=gs.members.end();++m) { if (m->address == member) { diff --git a/node/Multicaster.hpp b/node/Multicaster.hpp index ae62456cf..1fba5f64b 100644 --- a/node/Multicaster.hpp +++ b/node/Multicaster.hpp @@ -109,6 +109,7 @@ public: * If zero is returned, the first two fields will still have been appended. * * @param RR Runtime environment + * @param queryingPeer Peer asking for gather (to skip in results) * @param nwid Network ID * @param mg Multicast group * @param appendTo Packet to append to @@ -116,7 +117,7 @@ public: * @return Number of addresses appended * @throws std::out_of_range Buffer overflow writing to packet */ - unsigned int gather(const RuntimeEnvironment *RR,uint64_t nwid,MulticastGroup &mg,Packet &appendTo,unsigned int limit) const; + unsigned int gather(const RuntimeEnvironment *RR,const Address &queryingPeer,uint64_t nwid,MulticastGroup &mg,Packet &appendTo,unsigned int limit) const; /** * Send a multicast