diff --git a/transport/internet/system_dialer.go b/transport/internet/system_dialer.go index 6588e7ea..c3a07976 100644 --- a/transport/internet/system_dialer.go +++ b/transport/internet/system_dialer.go @@ -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) { - if dest.Network == net.Network_UDP { + if dest.Network == net.Network_UDP && !hasBindAddr(sockopt) { srcAddr := resolveSrcAddr(net.Network_UDP, src) if srcAddr == nil { 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 { 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 { newError("failed to bind source address to ", sockopt.BindAddress).Base(err).WriteToLog(session.ExportIDToError(ctx)) }