Ejemplo n.º 1
0
// In a get_peers response, the addresses of torrent clients involved with the
// queried info-hash.
func (m Msg) Values() (vs []Peer) {
	v := func() interface{} {
		defer func() {
			recover()
		}()
		return m["r"].(map[string]interface{})["values"]
	}()
	if v == nil {
		return
	}
	vl, ok := v.([]interface{})
	if !ok {
		if missinggo.CryHeard() {
			log.Printf(`unexpected krpc "values" field: %#v`, v)
		}
		return
	}
	vs = make([]Peer, 0, len(vl))
	for _, i := range vl {
		s, ok := i.(string)
		if !ok {
			panic(i)
		}
		// Because it's a list of strings, we can let the length of the string
		// determine the IP version of the compact peer.
		var cp util.CompactPeer
		err := cp.UnmarshalBinary([]byte(s))
		if err != nil {
			log.Printf("error decoding values list element: %s", err)
			continue
		}
		vs = append(vs, Peer{cp.IP[:], int(cp.Port)})
	}
	return
}
Ejemplo n.º 2
0
Archivo: dht.go Proyecto: gbjk/torrent
// In a get_peers response, the addresses of torrent clients involved with the
// queried info-hash.
func (m Msg) Values() (vs []util.CompactPeer) {
	r, ok := m["r"]
	if !ok {
		return
	}
	rd, ok := r.(map[string]interface{})
	if !ok {
		return
	}
	v, ok := rd["values"]
	if !ok {
		return
	}
	vl, ok := v.([]interface{})
	if !ok {
		log.Printf("unexpected krpc values type: %T", v)
		return
	}
	vs = make([]util.CompactPeer, 0, len(vl))
	for _, i := range vl {
		s, ok := i.(string)
		if !ok {
			panic(i)
		}
		var cp util.CompactPeer
		err := cp.UnmarshalBinary([]byte(s))
		if err != nil {
			log.Printf("error decoding values list element: %s", err)
			continue
		}
		vs = append(vs, cp)
	}
	return
}