]> Sergey Matveev's repositories - btrtrc.git/commitdiff
dht: Rename dHTAddr to Addr
authorMatt Joiner <anacrolix@gmail.com>
Tue, 23 Feb 2016 14:50:15 +0000 (01:50 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Tue, 23 Feb 2016 14:50:15 +0000 (01:50 +1100)
dht/addr.go
dht/announce.go
dht/dht.go
dht/nodeinfo.go
dht/server.go
dht/transaction.go

index a0abf96a8a2a386472b3f949263bc1cee0ef4ff1..d36739c0414995f9e8f95ff06732d98dfe6cc104 100644 (file)
@@ -7,7 +7,7 @@ import (
 )
 
 // Used internally to refer to node network addresses.
-type dHTAddr interface {
+type Addr interface {
        net.Addr
        UDPAddr() *net.UDPAddr
        IP() net.IP
@@ -36,6 +36,6 @@ func (ca cachedAddr) IP() net.IP {
        return ca.ip
 }
 
-func newDHTAddr(addr net.Addr) dHTAddr {
+func newDHTAddr(addr net.Addr) Addr {
        return cachedAddr{addr, addr.String(), missinggo.AddrIP(addr)}
 }
index aa964eb7ed4abd8ff1453ce0e5f5c419088f0cef..021d077ee7a2a685f768a22ef404d76c06415788 100644 (file)
@@ -43,7 +43,7 @@ func (me *Announce) NumContacted() int {
 // specified.
 func (s *Server) Announce(infoHash string, port int, impliedPort bool) (*Announce, error) {
        s.mu.Lock()
-       startAddrs := func() (ret []dHTAddr) {
+       startAddrs := func() (ret []Addr) {
                for _, n := range s.closestGoodNodes(160, infoHash) {
                        ret = append(ret, n.addr)
                }
@@ -96,7 +96,7 @@ func (s *Server) Announce(infoHash string, port int, impliedPort bool) (*Announc
        return disc, nil
 }
 
-func (me *Announce) gotNodeAddr(addr dHTAddr) {
+func (me *Announce) gotNodeAddr(addr Addr) {
        if missinggo.AddrPort(addr) == 0 {
                // Not a contactable address.
                return
@@ -116,7 +116,7 @@ func (me *Announce) gotNodeAddr(addr dHTAddr) {
        me.contact(addr)
 }
 
-func (me *Announce) contact(addr dHTAddr) {
+func (me *Announce) contact(addr Addr) {
        me.numContacted++
        me.triedAddrs.Add([]byte(addr.String()))
        if err := me.getPeers(addr); err != nil {
@@ -143,7 +143,7 @@ func (me *Announce) closingCh() chan struct{} {
 }
 
 // Announce to a peer, if appropriate.
-func (me *Announce) maybeAnnouncePeer(to dHTAddr, token, peerId string) {
+func (me *Announce) maybeAnnouncePeer(to Addr, token, peerId string) {
        me.server.mu.Lock()
        defer me.server.mu.Unlock()
        if !me.server.config.NoSecurity {
@@ -160,7 +160,7 @@ func (me *Announce) maybeAnnouncePeer(to dHTAddr, token, peerId string) {
        }
 }
 
-func (me *Announce) getPeers(addr dHTAddr) error {
+func (me *Announce) getPeers(addr Addr) error {
        me.server.mu.Lock()
        defer me.server.mu.Unlock()
        t, err := me.server.getPeers(addr, me.infoHash)
index 499d59bff99f974bbd84b4c1f9a0eac936e8bb4c..412f0c01a03c7cdef699874f4202f66a462c5fda 100644 (file)
@@ -126,7 +126,7 @@ func (nid *nodeID) ByteString() string {
 }
 
 type node struct {
-       addr          dHTAddr
+       addr          Addr
        id            nodeID
        announceToken string
 
index 84887c7e33cfe0e082852b3b46e1b372efd562f2..f46a40deb3f23a03fc42d80d0832d0b68c0725fc 100644 (file)
@@ -13,7 +13,7 @@ const CompactIPv4NodeInfoLen = 26
 
 type NodeInfo struct {
        ID   [20]byte
-       Addr dHTAddr
+       Addr Addr
 }
 
 // Writes the node info to its compact binary representation in b. See
index 9b9c7ca229aff1d466f78ab3a909fb1aced8d80c..db51b7078fce6f886f282f8b79399a8d4c44926a 100644 (file)
@@ -148,7 +148,7 @@ func (s *Server) init() (err error) {
        return
 }
 
-func (s *Server) processPacket(b []byte, addr dHTAddr) {
+func (s *Server) processPacket(b []byte, addr Addr) {
        if len(b) < 2 || b[0] != 'd' || b[len(b)-1] != 'e' {
                // KRPC messages are bencoded dicts.
                readNotKRPCDict.Add(1)
@@ -248,7 +248,7 @@ func (s *Server) nodeByID(id string) *node {
        return nil
 }
 
-func (s *Server) handleQuery(source dHTAddr, m Msg) {
+func (s *Server) handleQuery(source Addr, m Msg) {
        node := s.getNode(source, m.SenderID())
        node.lastGotQuery = time.Now()
        if s.config.OnQuery != nil {
@@ -309,7 +309,7 @@ func (s *Server) handleQuery(source dHTAddr, m Msg) {
        }
 }
 
-func (s *Server) reply(addr dHTAddr, t string, r Return) {
+func (s *Server) reply(addr Addr, t string, r Return) {
        r.ID = s.ID()
        m := Msg{
                T: t,
@@ -328,7 +328,7 @@ func (s *Server) reply(addr dHTAddr, t string, r Return) {
 
 // Returns a node struct for the addr. It is taken from the table or created
 // and possibly added if required and meets validity constraints.
-func (s *Server) getNode(addr dHTAddr, id string) (n *node) {
+func (s *Server) getNode(addr Addr, id string) (n *node) {
        addrStr := addr.String()
        n = s.nodes[addrStr]
        if n != nil {
@@ -357,7 +357,7 @@ func (s *Server) getNode(addr dHTAddr, id string) (n *node) {
        return
 }
 
-func (s *Server) nodeTimedOut(addr dHTAddr) {
+func (s *Server) nodeTimedOut(addr Addr) {
        node, ok := s.nodes[addr.String()]
        if !ok {
                return
@@ -371,7 +371,7 @@ func (s *Server) nodeTimedOut(addr dHTAddr) {
        delete(s.nodes, addr.String())
 }
 
-func (s *Server) writeToNode(b []byte, node dHTAddr) (err error) {
+func (s *Server) writeToNode(b []byte, node Addr) (err error) {
        if list := s.ipBlockList; list != nil {
                if r, ok := list.Lookup(missinggo.AddrIP(node.UDPAddr())); ok {
                        err = fmt.Errorf("write to %s blocked: %s", node, r.Description)
@@ -390,7 +390,7 @@ func (s *Server) writeToNode(b []byte, node dHTAddr) (err error) {
        return
 }
 
-func (s *Server) findResponseTransaction(transactionID string, sourceNode dHTAddr) *Transaction {
+func (s *Server) findResponseTransaction(transactionID string, sourceNode Addr) *Transaction {
        return s.transactions[transactionKey{
                sourceNode.String(),
                transactionID}]
@@ -423,7 +423,7 @@ func (s *Server) ID() string {
        return s.id
 }
 
-func (s *Server) query(node dHTAddr, q string, a map[string]interface{}, onResponse func(Msg)) (t *Transaction, err error) {
+func (s *Server) query(node Addr, q string, a map[string]interface{}, onResponse func(Msg)) (t *Transaction, err error) {
        tid := s.nextTransactionID()
        if a == nil {
                a = make(map[string]interface{}, 1)
@@ -472,7 +472,7 @@ func (s *Server) Ping(node *net.UDPAddr) (*Transaction, error) {
        return s.query(newDHTAddr(node), "ping", nil, nil)
 }
 
-func (s *Server) announcePeer(node dHTAddr, infoHash string, port int, token string, impliedPort bool) (err error) {
+func (s *Server) announcePeer(node Addr, infoHash string, port int, token string, impliedPort bool) (err error) {
        if port == 0 && !impliedPort {
                return errors.New("nothing to announce")
        }
@@ -518,7 +518,7 @@ func (s *Server) liftNodes(d Msg) {
 }
 
 // Sends a find_node query to addr. targetID is the node we're looking for.
-func (s *Server) findNode(addr dHTAddr, targetID string) (t *Transaction, err error) {
+func (s *Server) findNode(addr Addr, targetID string) (t *Transaction, err error) {
        t, err = s.query(addr, "find_node", map[string]interface{}{"target": targetID}, func(d Msg) {
                // Scrape peers from the response to put in the server's table before
                // handing the response back to the caller.
@@ -678,7 +678,7 @@ func (s *Server) setDefaults() (err error) {
        return
 }
 
-func (s *Server) getPeers(addr dHTAddr, infoHash string) (t *Transaction, err error) {
+func (s *Server) getPeers(addr Addr, infoHash string) (t *Transaction, err error) {
        if len(infoHash) != 20 {
                err = fmt.Errorf("infohash has bad length")
                return
@@ -714,7 +714,7 @@ func (s *Server) closestNodes(k int, target nodeID, filter func(*node) bool) []*
        return ret
 }
 
-func (me *Server) badNode(addr dHTAddr) {
+func (me *Server) badNode(addr Addr) {
        me.badNodes.Add([]byte(addr.String()))
        delete(me.nodes, addr.String())
 }
index c4c7b3a451627e340f66651dee7fd039a2ac02f4..f7503d071c751403f4a369dc3ce000dcbe8f6278 100644 (file)
@@ -9,7 +9,7 @@ import (
 // query message and a response message.
 type Transaction struct {
        mu             sync.Mutex
-       remoteAddr     dHTAddr
+       remoteAddr     Addr
        t              string
        response       chan Msg
        onResponse     func(Msg) // Called with the server locked.