]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Add util AddrIP and AddrPort functions
authorMatt Joiner <anacrolix@gmail.com>
Mon, 17 Nov 2014 03:20:49 +0000 (21:20 -0600)
committerMatt Joiner <anacrolix@gmail.com>
Mon, 17 Nov 2014 03:20:49 +0000 (21:20 -0600)
client.go
dht/dht.go
util/addr.go [new file with mode: 0644]

index 58458087e2f081186053aefd4ae7c6764129d2ee..401f5efd11e11bc31576b570cea0ba98f5c0194c 100644 (file)
--- a/client.go
+++ b/client.go
@@ -805,15 +805,7 @@ type peerExchangeMessage struct {
 
 // Extracts the port as an integer from an address string.
 func addrPort(addr net.Addr) int {
-       _, port, err := net.SplitHostPort(addr.String())
-       if err != nil {
-               panic(err)
-       }
-       i64, err := strconv.ParseInt(port, 0, 0)
-       if err != nil {
-               panic(err)
-       }
-       return int(i64)
+       return AddrPort(addr)
 }
 
 // Processes incoming bittorrent messages. The client lock is held upon entry
index 9118df71ca967c84c6faf3d20968660abfb35a6d..5f671e606367cc3a415754ac38d31feead2635c3 100644 (file)
@@ -487,14 +487,14 @@ func (ni *NodeInfo) PutCompact(b []byte) error {
        if n := copy(b[:], ni.ID[:]); n != 20 {
                panic(n)
        }
-       ip := ni.Addr.IP.To4()
+       ip := util.AddrIP(ni.Addr).To4()
        if len(ip) != 4 {
                panic(ip)
        }
        if n := copy(b[20:], ip); n != 4 {
                panic(n)
        }
-       binary.BigEndian.PutUint16(b[24:], uint16(ni.Addr.Port))
+       binary.BigEndian.PutUint16(b[24:], uint16(util.AddrPort(ni.Addr)))
        return nil
 }
 
@@ -651,7 +651,7 @@ func (s *Server) liftNodes(d Msg) {
                // log.Print(err)
        } else {
                for _, cni := range r.Nodes {
-                       if cni.Addr.Port == 0 {
+                       if util.AddrPort(cni.Addr) == 0 {
                                // TODO: Why would people even do this?
                                continue
                        }
diff --git a/util/addr.go b/util/addr.go
new file mode 100644 (file)
index 0000000..a91dec3
--- /dev/null
@@ -0,0 +1,27 @@
+package util
+
+import (
+       "net"
+       "strconv"
+)
+
+// Extracts the port as an integer from an address string.
+func AddrPort(addr net.Addr) int {
+       _, port, err := net.SplitHostPort(addr.String())
+       if err != nil {
+               panic(err)
+       }
+       i64, err := strconv.ParseInt(port, 0, 0)
+       if err != nil {
+               panic(err)
+       }
+       return int(i64)
+}
+
+func AddrIP(addr net.Addr) net.IP {
+       host, _, err := net.SplitHostPort(addr.String())
+       if err != nil {
+               panic(err)
+       }
+       return net.ParseIP(host)
+}