Example #1
0
func (ch *controlChan) validatePacket(pkt *packet.ControlPacket) error {
	nSet := 0
	if msg := pkt.GetOpenChannel(); msg != nil {
		nSet++
	}
	if msg := pkt.GetChannelResult(); msg != nil {
		nSet++
	}
	if msg := pkt.GetKeepAlive(); msg != nil {
		nSet++
	}
	if msg := pkt.GetEnableFeatures(); msg != nil {
		nSet++
	}
	if msg := pkt.GetFeaturesEnabled(); msg != nil {
		nSet++
	}
	if nSet != 1 {
		return fmt.Errorf("has %d fields set", nSet)
	}
	return nil
}
Example #2
0
func (ch *controlChan) onPacket(rawPkt []byte) (err error) {
	var ctrlPkt packet.ControlPacket
	if err = proto.Unmarshal(rawPkt, &ctrlPkt); err != nil {
		return
	}
	if err = ch.validatePacket(&ctrlPkt); err != nil {
		return fmt.Errorf("ctrlChan: %v", err)
	}

	if msg := ctrlPkt.GetOpenChannel(); msg != nil {
		err = ch.onOpenChannelMsg(msg)
	} else if msg := ctrlPkt.GetChannelResult(); msg != nil {
		err = ch.onChannelResultMsg(msg)
	} else if msg := ctrlPkt.GetKeepAlive(); msg != nil {
		err = ch.onKeepAlive(msg)
	} else if msg := ctrlPkt.GetEnableFeatures(); msg != nil {
		err = ch.onEnableFeatures(msg)
	} else if msg := ctrlPkt.GetFeaturesEnabled(); msg != nil {
		err = ch.onFeaturesEnabled(msg)
	} else {
		// This should *NEVER* happen since the validation ensures that at
		// least one of the fields is set.
		return fmt.Errorf("BUG: unhandled ctrl channel packet")
	}
	return
}