diff options
| author | Shulhan <ms@kilabit.info> | 2019-03-26 02:07:42 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2019-03-26 02:09:48 +0700 |
| commit | a1b4db8bbecd4f68b81ae77cc25cecea0fcbff8d (patch) | |
| tree | be85ce95374eda77630bb8a3f8bb6cb45e46fff3 | |
| parent | f3dbaadd04c589c974e29948a9390038e3031825 (diff) | |
| download | pakakeh.go-a1b4db8bbecd4f68b81ae77cc25cecea0fcbff8d.tar.xz | |
net: change return value of ParseIPPort to include hostname
Previously, if parameter address in ParseIPPort is hostname instead of
IP address it will return an error.
This commit change the return value to hostname without any error. So,
instead of error it will return the hostname in address.
This changes affect dns library when creating NewTCPClient and
NewUDPClient.
| -rw-r--r-- | lib/dns/tcpclient.go | 9 | ||||
| -rw-r--r-- | lib/dns/udpclient.go | 7 | ||||
| -rw-r--r-- | lib/dns/udpclientpool_test.go | 2 | ||||
| -rw-r--r-- | lib/net/parser.go | 27 | ||||
| -rw-r--r-- | lib/net/parser_test.go | 46 |
5 files changed, 52 insertions, 39 deletions
diff --git a/lib/dns/tcpclient.go b/lib/dns/tcpclient.go index 42eefbaf..66d7b5c2 100644 --- a/lib/dns/tcpclient.go +++ b/lib/dns/tcpclient.go @@ -5,6 +5,7 @@ package dns import ( + "fmt" "net" "time" @@ -29,9 +30,9 @@ type TCPClient struct { // server. Default port is 53, if not set. // func NewTCPClient(nameserver string) (*TCPClient, error) { - remoteIP, remotePort, err := libnet.ParseIPPort(nameserver, DefaultPort) - if err != nil { - return nil, err + _, remoteIP, remotePort := libnet.ParseIPPort(nameserver, DefaultPort) + if remoteIP == nil { + return nil, fmt.Errorf("dns: invalid address '%s'", nameserver) } raddr := &net.TCPAddr{ @@ -44,7 +45,7 @@ func NewTCPClient(nameserver string) (*TCPClient, error) { addr: raddr, } - err = cl.Connect(raddr) + err := cl.Connect(raddr) if err != nil { return nil, err } diff --git a/lib/dns/udpclient.go b/lib/dns/udpclient.go index bb730e12..e2561e73 100644 --- a/lib/dns/udpclient.go +++ b/lib/dns/udpclient.go @@ -5,6 +5,7 @@ package dns import ( + "fmt" "net" "sync" "time" @@ -36,9 +37,9 @@ type UDPClient struct { func NewUDPClient(nameserver string) (cl *UDPClient, err error) { network := "udp" - remoteIP, remotePort, err := libnet.ParseIPPort(nameserver, DefaultPort) - if err != nil { - return + _, remoteIP, remotePort := libnet.ParseIPPort(nameserver, DefaultPort) + if remoteIP == nil { + return nil, fmt.Errorf("dns: invalid address '%s'", nameserver) } laddr := &net.UDPAddr{IP: nil, Port: 0} diff --git a/lib/dns/udpclientpool_test.go b/lib/dns/udpclientpool_test.go index da7aff7d..14c22dfe 100644 --- a/lib/dns/udpclientpool_test.go +++ b/lib/dns/udpclientpool_test.go @@ -25,7 +25,7 @@ func TestNewUDPClientPool(t *testing.T) { testServerAddress, "notipaddress", }, - expErr: "invalid host address", + expErr: "dns: invalid address 'notipaddress'", }, { desc: "With single name server", ns: []string{ diff --git a/lib/net/parser.go b/lib/net/parser.go index f4858df3..12289dab 100644 --- a/lib/net/parser.go +++ b/lib/net/parser.go @@ -5,16 +5,18 @@ package net import ( + "fmt" "net" "strconv" ) // // ParseIPPort parse address into IP and port. -// In case of port is empty or invalid, it will set to default port value in -// defPort. +// If address is not an IP address, it will the address as hostname (without +// port number if its exist) with nil on ip. +// In case of port is empty or invalid, it will set to defPort. // -func ParseIPPort(address string, defPort uint16) (ip net.IP, port uint16, err error) { +func ParseIPPort(address string, defPort uint16) (hostname string, ip net.IP, port uint16) { var iport int shost, sport, err := net.SplitHostPort(address) @@ -24,9 +26,8 @@ func ParseIPPort(address string, defPort uint16) (ip net.IP, port uint16, err er ip = net.ParseIP(shost) if ip == nil { - return nil, 0, ErrHostAddress + hostname = shost } - if len(sport) > 0 { iport, err = strconv.Atoi(sport) if err != nil { @@ -39,17 +40,18 @@ func ParseIPPort(address string, defPort uint16) (ip net.IP, port uint16, err er port = defPort } - return ip, port, nil + return hostname, ip, port } // // ParseUDPAddr parse IP address into standard library UDP address. +// If address is not contains IP address, it will return nil with error. // In case of port is empty, it will set to default port value in defPort. // func ParseUDPAddr(address string, defPort uint16) (udp *net.UDPAddr, err error) { - ip, port, err := ParseIPPort(address, defPort) - if err != nil { - return + _, ip, port := ParseIPPort(address, defPort) + if ip == nil { + return nil, fmt.Errorf("net: invalid IP address: %s", address) } udp = &net.UDPAddr{ @@ -62,12 +64,13 @@ func ParseUDPAddr(address string, defPort uint16) (udp *net.UDPAddr, err error) // // ParseTCPAddr parse IP address into standard library TCP address. +// If address is not contains IP address, it will return nil with error. // In case of port is empty, it will set to default port value in defPort. // func ParseTCPAddr(address string, defPort uint16) (udp *net.TCPAddr, err error) { - ip, port, err := ParseIPPort(address, defPort) - if err != nil { - return + _, ip, port := ParseIPPort(address, defPort) + if ip == nil { + return nil, fmt.Errorf("net: invalid IP address: %s", address) } udp = &net.TCPAddr{ diff --git a/lib/net/parser_test.go b/lib/net/parser_test.go index 49df3c20..cff75625 100644 --- a/lib/net/parser_test.go +++ b/lib/net/parser_test.go @@ -15,49 +15,57 @@ func TestParseIPPort(t *testing.T) { localIP := net.ParseIP("127.0.0.1") cases := []struct { - desc string - address string - expErr error - expIP net.IP - expPort uint16 + desc string + address string + expHostname string + expIP net.IP + expPort uint16 }{{ - desc: "Empty address", - expErr: ErrHostAddress, + desc: "Empty address", + expPort: 1, + }, { + desc: "With hostname", + address: "address", + expHostname: "address", + expPort: 1, }, { - desc: "Invalid address", - address: "address", - expErr: ErrHostAddress, + desc: "With hostname and port", + address: "address:555", + expHostname: "address", + expPort: 555, }, { - desc: "Empty port", + desc: "With empty port", address: "127.0.0.1", expIP: localIP, expPort: 1, }, { - desc: "Invalid port", + desc: "With invalid port", address: "127.0.0.1:a", expIP: localIP, expPort: 1, }, { - desc: "Invalid port < 0", + desc: "With invalid port < 0", address: "127.0.0.1:-1", expIP: localIP, expPort: 1, }, { - desc: "Invalid port > 65535", + desc: "With invalid port > 65535", address: "127.0.0.1:65536", expIP: localIP, expPort: 1, + }, { + desc: "With valid port", + address: "127.0.0.1:555", + expIP: localIP, + expPort: 555, }} for _, c := range cases { t.Log(c.desc) - ip, port, err := ParseIPPort(c.address, 1) - if err != nil { - test.Assert(t, "error", c.expErr, err, true) - continue - } + hostname, ip, port := ParseIPPort(c.address, 1) + test.Assert(t, "hostname", c.expHostname, hostname, true) test.Assert(t, "ip", c.expIP, ip, true) test.Assert(t, "port", c.expPort, port, true) } |
