diff options
| author | Felix Hanley <felix@userspace.com.au> | 2018-02-21 04:20:06 +0000 |
|---|---|---|
| committer | Felix Hanley <felix@userspace.com.au> | 2018-02-21 04:21:39 +0000 |
| commit | e9adf3a2bf8b81615275a6705b7957e43753f0ec (patch) | |
| tree | 1eaeb5081f3914a8ffa936d96ad1f1548c9aeb2f /dht/messages.go | |
| parent | 020a8f9ec7e541d284ddb65111aafe42547927e5 (diff) | |
| download | dhtsearch-e9adf3a2bf8b81615275a6705b7957e43753f0ec.tar.gz dhtsearch-e9adf3a2bf8b81615275a6705b7957e43753f0ec.tar.bz2 | |
Seperate shared packages
Diffstat (limited to 'dht/messages.go')
| -rw-r--r-- | dht/messages.go | 38 |
1 files changed, 15 insertions, 23 deletions
diff --git a/dht/messages.go b/dht/messages.go index 023e27d..9305b6f 100644 --- a/dht/messages.go +++ b/dht/messages.go @@ -3,28 +3,29 @@ package dht import ( "fmt" "net" - //"strings" + + "github.com/felix/dhtsearch/krpc" ) func (n *Node) onPingQuery(rn remoteNode, msg map[string]interface{}) error { - t, err := getStringKey(msg, "t") + t, err := krpc.GetString(msg, "t") if err != nil { return err } - n.queueMsg(rn, makeResponse(t, map[string]interface{}{ + n.queueMsg(rn, krpc.MakeResponse(t, map[string]interface{}{ "id": string(n.id), })) return nil } func (n *Node) onGetPeersQuery(rn remoteNode, msg map[string]interface{}) error { - a, err := getMapKey(msg, "a") + a, err := krpc.GetMap(msg, "a") if err != nil { return err } // This is the ih of the torrent - torrent, err := getStringKey(a, "info_hash") + torrent, err := krpc.GetString(a, "info_hash") if err != nil { return err } @@ -40,7 +41,7 @@ func (n *Node) onGetPeersQuery(rn remoteNode, msg map[string]interface{}) error nodes := n.rTable.get(8) compactNS := []string{} for _, rn := range nodes { - ns := encodeCompactNodeAddr(rn.address.String()) + ns := encodeCompactNodeAddr(rn.addr.String()) if ns == "" { n.log.Warn("failed to compact node", "address", rn.address.String()) continue @@ -50,7 +51,7 @@ func (n *Node) onGetPeersQuery(rn remoteNode, msg map[string]interface{}) error */ t := msg["t"].(string) - n.queueMsg(rn, makeResponse(t, map[string]interface{}{ + n.queueMsg(rn, krpc.MakeResponse(t, map[string]interface{}{ "id": string(neighbour), "token": token, "nodes": "", @@ -60,7 +61,7 @@ func (n *Node) onGetPeersQuery(rn remoteNode, msg map[string]interface{}) error //nodes := n.rTable.get(50) /* fmt.Printf("sending get_peers for %s to %d nodes\n", *th, len(nodes)) - q := makeQuery(newTransactionID(), "get_peers", map[string]interface{}{ + q := krpc.MakeQuery(newTransactionID(), "get_peers", map[string]interface{}{ "id": string(id), "info_hash": string(*th), }) @@ -72,22 +73,17 @@ func (n *Node) onGetPeersQuery(rn remoteNode, msg map[string]interface{}) error } func (n *Node) onAnnouncePeerQuery(rn remoteNode, msg map[string]interface{}) error { - a, err := getMapKey(msg, "a") + a, err := krpc.GetMap(msg, "a") if err != nil { return err } - err = checkKeys(a, [][]string{ - {"info_hash", "string"}, - {"port", "int"}, - {"token", "string"}, - }) n.log.Debug("announce_peer", "source", rn) - if impliedPort, err := getIntKey(a, "implied_port"); err == nil { + if impliedPort, err := krpc.GetInt(a, "implied_port"); err == nil { if impliedPort != 0 { // Use the port in the message - host, _, err := net.SplitHostPort(rn.address.String()) + host, _, err := net.SplitHostPort(rn.addr.String()) if err != nil { return err } @@ -96,13 +92,13 @@ func (n *Node) onAnnouncePeerQuery(rn remoteNode, msg map[string]interface{}) er return fmt.Errorf("ignoring port 0") } addr, err := net.ResolveUDPAddr(n.family, fmt.Sprintf("%s:%d", host, newPort)) - rn = remoteNode{address: addr, id: rn.id} + rn = remoteNode{addr: addr, id: rn.id} } } // TODO do we reply? - ihStr, err := getStringKey(a, "info_hash") + ihStr, err := krpc.GetString(a, "info_hash") if err != nil { return err } @@ -111,8 +107,7 @@ func (n *Node) onAnnouncePeerQuery(rn remoteNode, msg map[string]interface{}) er n.log.Warn("invalid torrent", "infohash", ihStr) } - p := Peer{Node: rn, Infohash: *ih} - n.log.Info("anounce_peer", p) + p := Peer{Addr: rn.addr, ID: rn.id, Infohash: *ih} if n.OnAnnouncePeer != nil { go n.OnAnnouncePeer(p) } @@ -121,9 +116,6 @@ func (n *Node) onAnnouncePeerQuery(rn remoteNode, msg map[string]interface{}) er func (n *Node) onFindNodeResponse(rn remoteNode, msg map[string]interface{}) { r := msg["r"].(map[string]interface{}) - if err := checkKey(r, "id", "string"); err != nil { - return - } nodes := r["nodes"].(string) n.processFindNodeResults(rn, nodes) } |
