listen system only when bind addr not specified

pull/1511/head
Darien Raymond 2019-01-12 09:12:48 +01:00
parent 40796b9f9c
commit ef739c2127
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
1 changed files with 6 additions and 2 deletions

View File

@ -39,8 +39,12 @@ func resolveSrcAddr(network net.Network, src net.Address) net.Addr {
} }
} }
func hasBindAddr(sockopt *SocketConfig) bool {
return len(sockopt.BindAddress) > 0 && sockopt.BindPort > 0
}
func (d *DefaultSystemDialer) Dial(ctx context.Context, src net.Address, dest net.Destination, sockopt *SocketConfig) (net.Conn, error) { func (d *DefaultSystemDialer) Dial(ctx context.Context, src net.Address, dest net.Destination, sockopt *SocketConfig) (net.Conn, error) {
if dest.Network == net.Network_UDP { if dest.Network == net.Network_UDP && !hasBindAddr(sockopt) {
srcAddr := resolveSrcAddr(net.Network_UDP, src) srcAddr := resolveSrcAddr(net.Network_UDP, src)
if srcAddr == nil { if srcAddr == nil {
srcAddr = &net.UDPAddr{ srcAddr = &net.UDPAddr{
@ -75,7 +79,7 @@ func (d *DefaultSystemDialer) Dial(ctx context.Context, src net.Address, dest ne
if err := applyOutboundSocketOptions(network, address, fd, sockopt); err != nil { if err := applyOutboundSocketOptions(network, address, fd, sockopt); err != nil {
newError("failed to apply socket options").Base(err).WriteToLog(session.ExportIDToError(ctx)) newError("failed to apply socket options").Base(err).WriteToLog(session.ExportIDToError(ctx))
} }
if dest.Network == net.Network_UDP && len(sockopt.BindAddress) > 0 && sockopt.BindPort > 0 { if dest.Network == net.Network_UDP && hasBindAddr(sockopt) {
if err := bindAddr(fd, sockopt.BindAddress, sockopt.BindPort); err != nil { if err := bindAddr(fd, sockopt.BindAddress, sockopt.BindPort); err != nil {
newError("failed to bind source address to ", sockopt.BindAddress).Base(err).WriteToLog(session.ExportIDToError(ctx)) newError("failed to bind source address to ", sockopt.BindAddress).Base(err).WriteToLog(session.ExportIDToError(ctx))
} }