Exemple #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)
		}
	}
}