Avoid using v6-mapped-v4

This commit is contained in:
Jason A. Donenfeld 2018-05-18 05:02:35 +02:00
parent 52d797ce1a
commit 125976edce
2 changed files with 14 additions and 4 deletions

View File

@ -58,6 +58,10 @@ func parseEndpoint(s string) (*net.UDPAddr, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
ip4 := addr.IP.To4()
if ip4 != nil {
addr.IP = ip4
}
return addr, err return addr, err
} }

View File

@ -45,7 +45,10 @@ func (e *NativeEndpoint) SrcIP() net.IP {
func (e *NativeEndpoint) DstToBytes() []byte { func (e *NativeEndpoint) DstToBytes() []byte {
addr := (*net.UDPAddr)(e) addr := (*net.UDPAddr)(e)
out := addr.IP out := addr.IP.To4()
if out == nil {
out = addr.IP
}
out = append(out, byte(addr.Port&0xff)) out = append(out, byte(addr.Port&0xff))
out = append(out, byte((addr.Port>>8)&0xff)) out = append(out, byte((addr.Port>>8)&0xff))
return out return out
@ -112,6 +115,9 @@ func (bind *NativeBind) Close() error {
func (bind *NativeBind) ReceiveIPv4(buff []byte) (int, Endpoint, error) { func (bind *NativeBind) ReceiveIPv4(buff []byte) (int, Endpoint, error) {
n, endpoint, err := bind.ipv4.ReadFromUDP(buff) n, endpoint, err := bind.ipv4.ReadFromUDP(buff)
if endpoint != nil {
endpoint.IP = endpoint.IP.To4()
}
return n, (*NativeEndpoint)(endpoint), err return n, (*NativeEndpoint)(endpoint), err
} }
@ -123,10 +129,10 @@ func (bind *NativeBind) ReceiveIPv6(buff []byte) (int, Endpoint, error) {
func (bind *NativeBind) Send(buff []byte, endpoint Endpoint) error { func (bind *NativeBind) Send(buff []byte, endpoint Endpoint) error {
var err error var err error
nend := endpoint.(*NativeEndpoint) nend := endpoint.(*NativeEndpoint)
if nend.IP.To16() != nil { if nend.IP.To4() != nil {
_, err = bind.ipv6.WriteToUDP(buff, (*net.UDPAddr)(nend))
} else {
_, err = bind.ipv4.WriteToUDP(buff, (*net.UDPAddr)(nend)) _, err = bind.ipv4.WriteToUDP(buff, (*net.UDPAddr)(nend))
} else {
_, err = bind.ipv6.WriteToUDP(buff, (*net.UDPAddr)(nend))
} }
return err return err
} }