func handleQueryV2(conn *net.UDPConn, addr *net.UDPAddr, buf []byte) { var pkt discover.QueryV2 err := pkt.UnmarshalXDR(buf) if err != nil { log.Println("QueryV2 Unmarshal:", err) log.Println(hex.Dump(buf)) return } if debug { log.Printf("<- %v %#v", addr, pkt) } var id protocol.NodeID if len(pkt.NodeID) == 32 { // Raw node ID copy(id[:], pkt.NodeID) } else { id.UnmarshalText(pkt.NodeID) } lock.Lock() node, ok := nodes[id] queries++ lock.Unlock() if ok && len(node.addresses) > 0 { ann := discover.AnnounceV2{ Magic: discover.AnnouncementMagicV2, This: discover.Node{ ID: pkt.NodeID, }, } for _, addr := range node.addresses { ann.This.Addresses = append(ann.This.Addresses, discover.Address{IP: addr.ip, Port: addr.port}) } if debug { log.Printf("-> %v %#v", addr, pkt) } tb := ann.MarshalXDR() _, _, err = conn.WriteMsgUDP(tb, nil, addr) if err != nil { log.Println("QueryV2 response write:", err) } lock.Lock() answered++ lock.Unlock() } }
func handleAnnounceV2(addr *net.UDPAddr, buf []byte) { var pkt discover.AnnounceV2 err := pkt.UnmarshalXDR(buf) if err != nil && err != io.EOF { log.Println("AnnounceV2 Unmarshal:", err) log.Println(hex.Dump(buf)) return } if debug { log.Printf("<- %v %#v", addr, pkt) } lock.Lock() announces++ lock.Unlock() ip := addr.IP.To4() if ip == nil { ip = addr.IP.To16() } var addrs []address for _, addr := range pkt.This.Addresses { tip := addr.IP if len(tip) == 0 { tip = ip } addrs = append(addrs, address{ ip: tip, port: addr.Port, }) } node := node{ addresses: addrs, updated: time.Now(), } var id protocol.NodeID if len(pkt.This.ID) == 32 { // Raw node ID copy(id[:], pkt.This.ID) } else { id.UnmarshalText(pkt.This.ID) } lock.Lock() nodes[id] = node lock.Unlock() }
func handleAnnounceV2(addr *net.UDPAddr, buf []byte) { var pkt discover.AnnounceV2 err := pkt.UnmarshalXDR(buf) if err != nil { log.Println("AnnounceV2 Unmarshal:", err) log.Println(hex.Dump(buf)) return } if debug { log.Printf("<- %v %#v", addr, pkt) } lock.Lock() announces++ lock.Unlock() ip := addr.IP.To4() if ip == nil { ip = addr.IP.To16() } var addrs []Address for _, addr := range pkt.Addresses { tip := addr.IP if len(tip) == 0 { tip = ip } addrs = append(addrs, Address{ IP: tip, Port: addr.Port, }) } node := Node{ Addresses: addrs, Updated: time.Now(), } lock.Lock() nodes[pkt.NodeID] = node lock.Unlock() }