예제 #1
0
func (d *gossipDiscoveryImpl) handleMsgFromComm(m *proto.GossipMessage) {
	if m == nil {
		return
	}
	if m.GetAliveMsg() == nil && m.GetMemRes() == nil && m.GetMemReq() == nil {
		d.logger.Warning("Got message with wrong type (expected Alive or MembershipResponse or MembershipRequest message):", m.Content) // TODO: write only message type
		d.logger.Warning(m)
		return
	}

	d.logger.Debug("Got message:", m)
	defer d.logger.Debug("Exiting")

	// TODO: make sure somehow that the membership request is "fresh"
	if memReq := m.GetMemReq(); memReq != nil {
		d.handleAliveMessage(memReq.SelfInformation)
		// Sending a membership response to a peer may block this routine
		// in case the sending is deliberately slow (i.e attack).
		// will keep this async until I'll write a timeout detector in the comm layer
		go d.sendMemResponse(memReq.SelfInformation.Membership, memReq.Known)
		return
	}

	if alive := m.GetAliveMsg(); alive != nil {
		d.handleAliveMessage(alive)
		return
	}

	if memResp := m.GetMemRes(); memResp != nil {
		for _, am := range memResp.Alive {
			d.handleAliveMessage(am)
		}

		for _, dm := range memResp.Dead {
			if !d.crpypt.ValidateAliveMsg(dm) {
				d.logger.Warningf("Alive message isn't authentic, someone spoofed %s's identity", dm.Membership.Endpoint)
				continue
			}

			newDeadMembers := []*proto.AliveMessage{}
			d.lock.RLock()
			if _, known := d.id2Member[string(dm.Membership.PkiID)]; !known {
				newDeadMembers = append(newDeadMembers, dm)
			}
			d.lock.RUnlock()
			d.learnNewMembers([]*proto.AliveMessage{}, newDeadMembers)
		}
	}
}
예제 #2
0
func (g *gossipInstance) tryForwardMessage(msg *proto.GossipMessage) {
	g.comm.lock.Lock()

	aliveMsg := msg.GetAliveMsg()

	forward := false
	id := string(aliveMsg.Membership.PkiID)
	seqNum := aliveMsg.Timestamp.SeqNum
	if last, exists := g.comm.lastSeqs[id]; exists {
		if last < seqNum {
			g.comm.lastSeqs[id] = seqNum
			forward = true
		}
	} else {
		g.comm.lastSeqs[id] = seqNum
		forward = true
	}

	g.comm.lock.Unlock()

	if forward {
		g.comm.Gossip(msg)
	}
}
예제 #3
0
func (da *discoveryAdapter) Gossip(msg *proto.GossipMessage) {
	if msg.IsAliveMsg() && time.Now().Before(da.includeIdentityPeriod) {
		msg.GetAliveMsg().Identity = da.identity
	}
	da.gossipFunc(msg)
}