Skip to content

proposal: net: add Dialer.LocalAddr4 and Dialer.LocalAddr6 #78444

Description

@database64128

Proposal Details

net.Dialer has a LocalAddr field for specifying the local address to bind to. It gained support for RFC 6555 Happy Eyeballs v1 years ago, and this was subsequently enabled by default.

Unfortunately, these two features have an implicit, undocumented incompatibility. Setting a LocalAddr causes Happy Eyeballs to be disabled, because a single net.Addr cannot represent both IPv4 and IPv6.

To allow specifying local IPv4 and IPv6 addresses while keeping Happy Eyeballs active, I propose we add the following fields to net.Dialer:

type Dialer struct {
	// ...

	// LocalAddr4 specifies the local IPv4 address to use when
	// dialing an IPv4 address.
	//
	//  - If either LocalAddr4 or LocalAddr6 is specified, LocalAddr
	//    is ignored.
	//  - If only one of LocalAddr4 or LocalAddr6 is specified,
	//    the other address family is disabled.
	//  - If both LocalAddr4 and LocalAddr6 are specified, RFC 6555
	//    Happy Eyeballs will use these local addresses.
	LocalAddr4 netip.AddrPort

	// LocalAddr6 specifies the local IPv6 address to use when
	// dialing an IPv6 address.
	//
	//  - If either LocalAddr4 or LocalAddr6 is specified, LocalAddr
	//    is ignored.
	//  - If only one of LocalAddr4 or LocalAddr6 is specified,
	//    the other address family is disabled.
	//  - If both LocalAddr4 and LocalAddr6 are specified, RFC 6555
	//    Happy Eyeballs will use these local addresses.
	LocalAddr6 netip.AddrPort
}

Using netip.AddrPort instead of net.Addr aligns with the new context-aware dialer methods introduced in #49097.

The old LocalAddr is ignored when either LocalAddr4 or LocalAddr6 is set. This is similar to how the newer ControlContext field overrides Control.

Metadata

Metadata

Assignees

No one assigned

    Labels

    LibraryProposalIssues describing a requested change to the Go standard library or x/ libraries, but not to a toolProposal

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions