2
0
mirror of synced 2025-02-23 14:18:13 +00:00
torrent/ipport.go
Matt Joiner d24922dc09 Add support for non-IP-based networks
Includes a test with unix sockets. Exposes AddDialer, AddListener, and reworks Peer.
2020-02-20 16:47:37 +11:00

68 lines
1.1 KiB
Go

package torrent
import (
"net"
"strconv"
)
// Extracts the port as an integer from an address string.
func addrPortOrZero(addr net.Addr) int {
switch raw := addr.(type) {
case *net.UDPAddr:
return raw.Port
case *net.TCPAddr:
return raw.Port
default:
_, port, err := net.SplitHostPort(addr.String())
if err != nil {
return 0
}
i64, err := strconv.ParseInt(port, 0, 0)
if err != nil {
panic(err)
}
return int(i64)
}
}
func addrIpOrNil(addr net.Addr) net.IP {
if addr == nil {
return nil
}
switch raw := addr.(type) {
case *net.UDPAddr:
return raw.IP
case *net.TCPAddr:
return raw.IP
default:
host, _, err := net.SplitHostPort(addr.String())
if err != nil {
return nil
}
return net.ParseIP(host)
}
}
type ipPortAddr struct {
IP net.IP
Port int
}
func (ipPortAddr) Network() string {
return ""
}
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
}
ret.Port = addrPortOrZero(na)
ok = true
return
}