// 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
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
}
// 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
}
--- /dev/null
+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)
+}