Commit 116adaa6 authored by Steven Allen's avatar Steven Allen

set the source IP, but not port, when not using the reuseport dialer

parent 75e68219
......@@ -50,8 +50,6 @@ func (t *TcpTransport) Dialer(laddr ma.Multiaddr, opts ...tpt.DialOpt) (tpt.Dial
if found {
return d, nil
}
var base manet.Dialer
var doReuse bool
for _, o := range opts {
switch o := o.(type) {
......@@ -62,7 +60,7 @@ func (t *TcpTransport) Dialer(laddr ma.Multiaddr, opts ...tpt.DialOpt) (tpt.Dial
}
}
tcpd, err := t.newTcpDialer(base, laddr, doReuse)
tcpd, err := t.newTcpDialer(laddr, doReuse)
if err != nil {
return nil, err
}
......@@ -136,11 +134,23 @@ type tcpDialer struct {
var _ tpt.Dialer = &tcpDialer{}
func (t *TcpTransport) newTcpDialer(base manet.Dialer, laddr ma.Multiaddr, doReuse bool) (*tcpDialer, error) {
func maddrToTcp(addr ma.Multiaddr) (*net.TCPAddr, error) {
la, err := manet.ToNetAddr(addr)
if err != nil {
return nil, err // something wrong with addr.
}
latcp, ok := la.(*net.TCPAddr)
if !ok {
return nil, fmt.Errorf("not a tcp multiaddr: %s", addr)
}
return latcp, nil
}
func (t *TcpTransport) newTcpDialer(laddr ma.Multiaddr, doReuse bool) (*tcpDialer, error) {
// get the local net.Addr manually
la, err := manet.ToNetAddr(laddr)
la, err := maddrToTcp(laddr)
if err != nil {
return nil, err // something wrong with laddr.
return nil, err
}
var pattern mafmt.Pattern
......@@ -152,31 +162,30 @@ func (t *TcpTransport) newTcpDialer(base manet.Dialer, laddr ma.Multiaddr, doReu
return nil, fmt.Errorf("local addr did not match TCP4 or TCP6: %s", laddr)
}
// Ignore the port when constructing the default (non-reuseport) dialer.
labase := *la
labase.Port = 0
dialer := &tcpDialer{
laddr: laddr,
pattern: pattern,
madialer: manet.Dialer{
Dialer: net.Dialer{
LocalAddr: &labase,
},
},
transport: t,
}
if doReuse && ReuseportIsAvailable() {
rd := reuseport.Dialer{
dialer.doReuse = true
dialer.rd = reuseport.Dialer{
D: net.Dialer{
LocalAddr: la,
Timeout: base.Timeout,
},
}
return &tcpDialer{
doReuse: true,
laddr: laddr,
rd: rd,
madialer: base,
transport: t,
pattern: pattern,
}, nil
}
return &tcpDialer{
doReuse: false,
laddr: laddr,
pattern: pattern,
madialer: base,
transport: t,
}, nil
return dialer, nil
}
func (d *tcpDialer) Dial(raddr ma.Multiaddr) (tpt.Conn, error) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment