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 {
return ErrWrongEndpointType
}
var conn *net.UDPConn
var blackhole bool
if nend.IP.To4() != nil {
if bind.ipv4 == nil {
return syscall.EAFNOSUPPORT
}
if bind.blackhole4 {
return nil
}
_, err = bind.ipv4.WriteToUDP(buff, (*net.UDPAddr)(nend))
blackhole = bind.blackhole4
conn = bind.ipv4
} else {
if bind.ipv6 == nil {
return syscall.EAFNOSUPPORT
blackhole = bind.blackhole6
conn = bind.ipv6
}
if bind.blackhole6 {
if blackhole {
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
}