Skip to content

Commit 5d8324e

Browse files
Paul Marksbradfitz
Paul Marks
authored andcommitted
net: add hostname warnings to all first(isIPv4) functions.
In general, these functions cannot behave correctly when given a hostname, because a hostname may represent multiple IP addresses, and first(isIPv4) chooses at most one. Updates #9334 Change-Id: Icfb629f84af4d976476385a3071270253c0000b1 Reviewed-on: https://go-review.googlesource.com/31931 Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 71cf409 commit 5d8324e

File tree

4 files changed

+15
-0
lines changed

4 files changed

+15
-0
lines changed

src/net/dial.go

+6
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,9 @@ func dialSingle(ctx context.Context, dp *dialParam, ra Addr) (c Conn, err error)
533533
// If host is omitted, as in ":8080", Listen listens on all available interfaces
534534
// instead of just the interface with the given host address.
535535
// See Dial for more details about address syntax.
536+
//
537+
// Listening on a hostname is not recommended because this creates a socket
538+
// for at most one of its IP addresses.
536539
func Listen(net, laddr string) (Listener, error) {
537540
addrs, err := DefaultResolver.resolveAddrList(context.Background(), "listen", net, laddr, nil)
538541
if err != nil {
@@ -560,6 +563,9 @@ func Listen(net, laddr string) (Listener, error) {
560563
// If host is omitted, as in ":8080", ListenPacket listens on all available interfaces
561564
// instead of just the interface with the given host address.
562565
// See Dial for the syntax of laddr.
566+
//
567+
// Listening on a hostname is not recommended because this creates a socket
568+
// for at most one of its IP addresses.
563569
func ListenPacket(net, laddr string) (PacketConn, error) {
564570
addrs, err := DefaultResolver.resolveAddrList(context.Background(), "listen", net, laddr, nil)
565571
if err != nil {

src/net/iprawsock.go

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ func (a *IPAddr) opAddr() Addr {
5252
// ResolveIPAddr parses addr as an IP address of the form "host" or
5353
// "ipv6-host%zone" and resolves the domain name on the network net,
5454
// which must be "ip", "ip4" or "ip6".
55+
//
56+
// Resolving a hostname is not recommended because this returns at most
57+
// one of its IP addresses.
5558
func ResolveIPAddr(net, addr string) (*IPAddr, error) {
5659
if net == "" { // a hint wildcard for Go 1.0 undocumented behavior
5760
net = "ip"

src/net/tcpsock.go

+3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ func (a *TCPAddr) opAddr() Addr {
5656
// "tcp6". A literal address or host name for IPv6 must be enclosed
5757
// in square brackets, as in "[::1]:80", "[ipv6-host]:http" or
5858
// "[ipv6-host%zone]:80".
59+
//
60+
// Resolving a hostname is not recommended because this returns at most
61+
// one of its IP addresses.
5962
func ResolveTCPAddr(net, addr string) (*TCPAddr, error) {
6063
switch net {
6164
case "tcp", "tcp4", "tcp6":

src/net/udpsock.go

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ func (a *UDPAddr) opAddr() Addr {
5959
// "udp6". A literal address or host name for IPv6 must be enclosed
6060
// in square brackets, as in "[::1]:80", "[ipv6-host]:http" or
6161
// "[ipv6-host%zone]:80".
62+
//
63+
// Resolving a hostname is not recommended because this returns at most
64+
// one of its IP addresses.
6265
func ResolveUDPAddr(net, addr string) (*UDPAddr, error) {
6366
switch net {
6467
case "udp", "udp4", "udp6":

0 commit comments

Comments
 (0)