Beispiel #1
0
// l: Handle a raw incoming packet. {{{2
func (dht *DHT) lRxPacket(data []byte, addr net.UDPAddr) error {
	msg, err := krpc.Decode(data)
	if err != nil {
		log.Noticee(err, "rx ignore (cannot decode)")
		return err
	}

	switch msg.Type {
	case "q":
		//log.Debugf("cl(%v) lRxQuery %v %v", dht.cfg.NodeID.ShortString(), msg, &addr)
		err = dht.lRxQuery(msg, addr)
	case "r":
		//log.Tracef("cl(%v) lRxResponse %v %v", dht.cfg.NodeID.ShortString(), msg, &addr)
		err = dht.lRxResponse(msg, addr)
	case "e":
		log.Debugf("cl(%v) lRxError %v %v", dht.cfg.NodeID.ShortString(), msg, &addr)
		err = dht.lRxError(msg, addr)
	default:
		log.Warnf("unknown packet type received: %#v (from %v)", msg.Type, &addr)
		return nil
	}

	log.Noticee(err, "rx processing error")
	return err
}
Beispiel #2
0
func TestKRPC(t *testing.T) {
	for _, tt := range tests {
		msg, err := krpc.Decode([]byte(tt.B))
		if err != nil {
			t.Fatalf("cannot decode: %v", err)
		}

		if tt.Method != "" {
			err = msg.ResponseAsMethod(tt.Method)
			if err != nil {
				t.Fatalf("cannot decode response part: %v", err)
			}
		}

		b, err := bencode.EncodeBytes(msg)
		if err != nil {
			t.Fatalf("couldn't encode: %v", err)
		}

		msg2, err := krpc.Decode(b)
		if err != nil {
			t.Fatalf("cannot decode: %v: %v %v", err, string(b), tt.B)
		}

		if tt.Method != "" {
			err = msg2.ResponseAsMethod(tt.Method)
			if err != nil {
				t.Fatalf("cannot decode response part: %v", err)
			}
		}

		if !reflect.DeepEqual(msg, msg2) {
			t.Logf("not equal after reserialization: %#v != %#v", msg, msg2)
		}
	}
}