Skip to content

Commit

Permalink
Avoid using address resolver to check address validity
Browse files Browse the repository at this point in the history
  • Loading branch information
dmorn committed Nov 22, 2019
1 parent f0326c3 commit 82f1e2a
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions internal/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,24 @@ func ScanLines(r io.Reader, f func(string) error) error {
return scanner.Err()
}

type uncheckedAddr struct {
net, addr string
}

func (a uncheckedAddr) Network() string { return a.net }
func (a uncheckedAddr) String() string { return a.addr }

func ParseNetAddr(network, addr string) (net.Addr, error) {
network = strings.ToLower(network)
switch {
case strings.Contains(network, "tcp"):
return net.ResolveTCPAddr(network, addr)
case strings.Contains(network, "udp"):
return net.ResolveUDPAddr(network, addr)
default:
return nil, fmt.Errorf("unsupported network %v", network)
host, _, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
if ip := net.ParseIP(host); ip == nil {
return nil, fmt.Errorf("%v is not a valid ip address", host)
}
return uncheckedAddr{
net: network,
addr: addr,
}, nil
}

0 comments on commit 82f1e2a

Please sign in to comment.