]> Sergey Matveev's repositories - btrtrc.git/blobdiff - ipport.go
Drop support for go 1.20
[btrtrc.git] / ipport.go
index d65624c34d139b6c760f59adab4c7c840dc285d8..a85a97fc10422229ba7cdaececd7fbe5d4b5abbc 100644 (file)
--- a/ipport.go
+++ b/ipport.go
@@ -13,13 +13,14 @@ func addrPortOrZero(addr net.Addr) int {
        case *net.TCPAddr:
                return raw.Port
        default:
+               // Consider a unix socket on Windows with a name like "C:notanint".
                _, port, err := net.SplitHostPort(addr.String())
                if err != nil {
                        return 0
                }
-               i64, err := strconv.ParseInt(port, 0, 0)
+               i64, err := strconv.ParseUint(port, 0, 16)
                if err != nil {
-                       panic(err)
+                       return 0
                }
                return int(i64)
        }
@@ -56,12 +57,15 @@ func (me ipPortAddr) String() string {
        return net.JoinHostPort(me.IP.String(), strconv.FormatInt(int64(me.Port), 10))
 }
 
-func tryIpPortFromNetAddr(na net.Addr) (ret ipPortAddr, ok bool) {
-       ret.IP = addrIpOrNil(na)
-       if ret.IP == nil {
-               return
+func tryIpPortFromNetAddr(addr PeerRemoteAddr) (ipPortAddr, bool) {
+       ok := true
+       host, port, err := net.SplitHostPort(addr.String())
+       if err != nil {
+               ok = false
+       }
+       portI64, err := strconv.ParseInt(port, 10, 0)
+       if err != nil {
+               ok = false
        }
-       ret.Port = addrPortOrZero(na)
-       ok = true
-       return
+       return ipPortAddr{net.ParseIP(host), int(portI64)}, ok
 }