Skip to content

Commit

Permalink
Avoid net.ResolveUDPAddr in snet.UDPAddrFromString
Browse files Browse the repository at this point in the history
Parse the host IP, port directly instead of using net.ResolveUDPAddr,
which also resolves hostnames. Hostnames should not be considered inside
SCION addresses.

Fixes scionproto#3654
  • Loading branch information
matzf committed Feb 10, 2020
1 parent ab879cf commit 878aec1
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions go/lib/snet/udpaddr.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"net"
"regexp"
"strconv"
"strings"

"github.com/scionproto/scion/go/lib/addr"
Expand Down Expand Up @@ -65,14 +66,20 @@ func UDPAddrFromString(s string) (*UDPAddr, error) {
if ip := net.ParseIP(strings.Trim(rawHost, "[]")); ip != nil {
return &UDPAddr{IA: ia, Host: &net.UDPAddr{IP: ip, Port: 0}}, nil
}
udpAddr, err := net.ResolveUDPAddr("udp", rawHost)

rawIP, rawPort, err := net.SplitHostPort(rawHost)
if err != nil {
return nil, err
}
if udpAddr.IP == nil {
ip := net.ParseIP(rawIP)
if ip == nil {
return nil, serrors.New("invalid address: no IP specified", "host", rawHost)
}
return &UDPAddr{IA: ia, Host: udpAddr}, nil
port, err := strconv.Atoi(rawPort)
if err != nil || 0 > port || port > 65535 {
return nil, serrors.New("invalid port", "host", rawHost)
}
return &UDPAddr{IA: ia, Host: &net.UDPAddr{IP: ip, Port: port}}, nil
}

// Network implements net.Addr interface.
Expand Down

0 comments on commit 878aec1

Please sign in to comment.