lib: Factor out getting IP address from net.Addr (#8538)
... and add fast paths for common cases.
This commit is contained in:
@@ -37,3 +37,15 @@ func GetLans() ([]*net.IPNet, error) {
|
||||
}
|
||||
return nets, nil
|
||||
}
|
||||
|
||||
func IPFromAddr(addr net.Addr) (net.IP, error) {
|
||||
switch a := addr.(type) {
|
||||
case *net.TCPAddr:
|
||||
return a.IP, nil
|
||||
case *net.UDPAddr:
|
||||
return a.IP, nil
|
||||
default:
|
||||
host, _, err := net.SplitHostPort(addr.String())
|
||||
return net.ParseIP(host), err
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user