conn: unify code in StdNetBind.Send

The sending code is identical for ipv4 and ipv6;
select the conn, then use it.

Signed-off-by: Josh Bleecher Snyder <josharian@gmail.com>
This commit is contained in:
Josh Bleecher Snyder 2021-03-29 13:11:11 -07:00
parent 02e419ed8a
commit 822f5a6d70

View file

@ -161,22 +161,21 @@ func (bind *StdNetBind) Send(buff []byte, endpoint Endpoint) error {
if !ok { if !ok {
return ErrWrongEndpointType return ErrWrongEndpointType
} }
var conn *net.UDPConn
var blackhole bool
if nend.IP.To4() != nil { if nend.IP.To4() != nil {
if bind.ipv4 == nil { blackhole = bind.blackhole4
return syscall.EAFNOSUPPORT conn = bind.ipv4
}
if bind.blackhole4 {
return nil
}
_, err = bind.ipv4.WriteToUDP(buff, (*net.UDPAddr)(nend))
} else { } else {
if bind.ipv6 == nil { blackhole = bind.blackhole6
return syscall.EAFNOSUPPORT conn = bind.ipv6
} }
if bind.blackhole6 { if blackhole {
return nil return nil
} }
_, err = bind.ipv6.WriteToUDP(buff, (*net.UDPAddr)(nend)) if conn == nil {
return syscall.EAFNOSUPPORT
} }
_, err = conn.WriteToUDP(buff, (*net.UDPAddr)(nend))
return err return err
} }