]> Sergey Matveev's repositories - btrtrc.git/commitdiff
dht: Rename newDHTAddr->NewAddr, and clean up the interface
authorMatt Joiner <anacrolix@gmail.com>
Tue, 23 Feb 2016 15:02:33 +0000 (02:02 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Tue, 23 Feb 2016 15:02:33 +0000 (02:02 +1100)
dht/addr.go
dht/announce.go
dht/compactNodeInfo.go
dht/dht.go
dht/dht_test.go
dht/msg_test.go
dht/nodeinfo.go
dht/server.go

index d36739c0414995f9e8f95ff06732d98dfe6cc104..2495b150c94583e2a595f1e08574ca24ec635e36 100644 (file)
@@ -1,27 +1,17 @@
 package dht
 
-import (
-       "net"
-
-       "github.com/anacrolix/missinggo"
-)
+import "net"
 
 // Used internally to refer to node network addresses.
 type Addr interface {
-       net.Addr
        UDPAddr() *net.UDPAddr
-       IP() net.IP
+       String() string
 }
 
 // Speeds up some of the commonly called Addr methods.
 type cachedAddr struct {
-       a  net.Addr
+       ua net.UDPAddr
        s  string
-       ip net.IP
-}
-
-func (ca cachedAddr) Network() string {
-       return ca.a.Network()
 }
 
 func (ca cachedAddr) String() string {
@@ -29,13 +19,12 @@ func (ca cachedAddr) String() string {
 }
 
 func (ca cachedAddr) UDPAddr() *net.UDPAddr {
-       return ca.a.(*net.UDPAddr)
-}
-
-func (ca cachedAddr) IP() net.IP {
-       return ca.ip
+       return &ca.ua
 }
 
-func newDHTAddr(addr net.Addr) Addr {
-       return cachedAddr{addr, addr.String(), missinggo.AddrIP(addr)}
+func NewAddr(ua *net.UDPAddr) Addr {
+       return cachedAddr{
+               ua: *ua,
+               s:  ua.String(),
+       }
 }
index 021d077ee7a2a685f768a22ef404d76c06415788..7fc3da2f14239b19aee9f6a9c88d7e209905028d 100644 (file)
@@ -6,7 +6,6 @@ import (
        "log"
        "time"
 
-       "github.com/anacrolix/missinggo"
        "github.com/anacrolix/sync"
        "github.com/willf/bloom"
 
@@ -56,7 +55,7 @@ func (s *Server) Announce(infoHash string, port int, impliedPort bool) (*Announc
                        return nil, err
                }
                for _, addr := range addrs {
-                       startAddrs = append(startAddrs, newDHTAddr(addr))
+                       startAddrs = append(startAddrs, NewAddr(addr))
                }
        }
        disc := &Announce{
@@ -97,7 +96,7 @@ func (s *Server) Announce(infoHash string, port int, impliedPort bool) (*Announc
 }
 
 func (me *Announce) gotNodeAddr(addr Addr) {
-       if missinggo.AddrPort(addr) == 0 {
+       if addr.UDPAddr().Port == 0 {
                // Not a contactable address.
                return
        }
@@ -150,7 +149,7 @@ func (me *Announce) maybeAnnouncePeer(to Addr, token, peerId string) {
                if len(peerId) != 20 {
                        return
                }
-               if !NodeIdSecure(peerId, to.IP()) {
+               if !NodeIdSecure(peerId, to.UDPAddr().IP) {
                        return
                }
        }
index e12bb0fcb37ced69439a643f8dfdf8f4248d76fa..19f9d57ed452b9fdb352aebada3f88822897900f 100644 (file)
@@ -42,7 +42,7 @@ func (me CompactIPv4NodeInfo) MarshalBencode() (ret []byte, err error) {
                        err = errors.New("nil addr in node info")
                        return
                }
-               buf.Write(ni.Addr.IP().To4())
+               buf.Write(ni.Addr.UDPAddr().IP.To4())
                binary.Write(&buf, binary.BigEndian, uint16(ni.Addr.UDPAddr().Port))
        }
        return bencode.Marshal(buf.Bytes())
index 412f0c01a03c7cdef699874f4202f66a462c5fda..2d1d736fa7cf8f6d1ed11fba5ef6ca0f564172fe 100644 (file)
@@ -139,7 +139,7 @@ func (n *node) IsSecure() bool {
        if n.id.IsUnset() {
                return false
        }
-       return NodeIdSecure(n.id.ByteString(), n.addr.IP())
+       return NodeIdSecure(n.id.ByteString(), n.addr.UDPAddr().IP)
 }
 
 func (n *node) idString() string {
index a1870a7ced3e23169c40afd547f9c5320eeafbdb..345da41c808c4a7e5e67d87fb4a37989da49161c 100644 (file)
@@ -24,7 +24,7 @@ func TestMarshalCompactNodeInfo(t *testing.T) {
        }
        addr, err := net.ResolveUDPAddr("udp4", "1.2.3.4:5")
        require.NoError(t, err)
-       cni.Addr = newDHTAddr(addr)
+       cni.Addr = NewAddr(addr)
        var b [CompactIPv4NodeInfoLen]byte
        err = cni.PutCompact(b[:])
        require.NoError(t, err)
index 7c84623470f41159ba354bca5e0288df0c2b3df1..ca446de397f6f3515e410f045b013b28cb05d880 100644 (file)
@@ -46,7 +46,7 @@ func TestMarshalUnmarshalMsg(t *testing.T) {
                R: &Return{
                        Nodes: CompactIPv4NodeInfo{
                                NodeInfo{
-                                       Addr: newDHTAddr(&net.UDPAddr{
+                                       Addr: NewAddr(&net.UDPAddr{
                                                IP:   net.IPv4(1, 2, 3, 4).To4(),
                                                Port: 0x1234,
                                        }),
index f46a40deb3f23a03fc42d80d0832d0b68c0725fc..7cec211672d0a03b7eabab29cc94eebcedbdbd5a 100644 (file)
@@ -22,14 +22,14 @@ func (ni *NodeInfo) PutCompact(b []byte) error {
        if n := copy(b[:], ni.ID[:]); n != 20 {
                panic(n)
        }
-       ip := missinggo.AddrIP(ni.Addr).To4()
+       ip := ni.Addr.UDPAddr().IP.To4()
        if len(ip) != 4 {
                return errors.New("expected ipv4 address")
        }
        if n := copy(b[20:], ip); n != 4 {
                panic(n)
        }
-       binary.BigEndian.PutUint16(b[24:], uint16(missinggo.AddrPort(ni.Addr)))
+       binary.BigEndian.PutUint16(b[24:], uint16(ni.Addr.UDPAddr().Port))
        return nil
 }
 
@@ -38,7 +38,7 @@ func (cni *NodeInfo) UnmarshalCompactIPv4(b []byte) error {
                return errors.New("expected 26 bytes")
        }
        missinggo.CopyExact(cni.ID[:], b[:20])
-       cni.Addr = newDHTAddr(&net.UDPAddr{
+       cni.Addr = NewAddr(&net.UDPAddr{
                IP:   append(make([]byte, 0, 4), b[20:24]...),
                Port: int(binary.BigEndian.Uint16(b[24:26])),
        })
index db51b7078fce6f886f282f8b79399a8d4c44926a..8a9b5adeecc93a0baf4ae9063f0efac65f6df422 100644 (file)
@@ -217,7 +217,7 @@ func (s *Server) serve() error {
                        readBlocked.Add(1)
                        continue
                }
-               s.processPacket(b[:n], newDHTAddr(addr))
+               s.processPacket(b[:n], NewAddr(addr.(*net.UDPAddr)))
        }
 }
 
@@ -469,7 +469,7 @@ func (s *Server) query(node Addr, q string, a map[string]interface{}, onResponse
 func (s *Server) Ping(node *net.UDPAddr) (*Transaction, error) {
        s.mu.Lock()
        defer s.mu.Unlock()
-       return s.query(newDHTAddr(node), "ping", nil, nil)
+       return s.query(NewAddr(node), "ping", nil, nil)
 }
 
 func (s *Server) announcePeer(node Addr, infoHash string, port int, token string, impliedPort bool) (err error) {
@@ -505,11 +505,11 @@ func (s *Server) liftNodes(d Msg) {
                return
        }
        for _, cni := range d.R.Nodes {
-               if missinggo.AddrPort(cni.Addr) == 0 {
+               if cni.Addr.UDPAddr().Port == 0 {
                        // TODO: Why would people even do this?
                        continue
                }
-               if s.ipBlocked(missinggo.AddrIP(cni.Addr)) {
+               if s.ipBlocked(cni.Addr.UDPAddr().IP) {
                        continue
                }
                n := s.getNode(cni.Addr, string(cni.ID[:]))
@@ -549,7 +549,7 @@ func (s *Server) addRootNodes() error {
                        continue
                }
                s.nodes[addr.String()] = &node{
-                       addr: newDHTAddr(addr),
+                       addr: NewAddr(addr),
                }
        }
        return nil