Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix UDP Socket Binding Issue on Linux for IPv4 and IPv6 #582

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions net/connUDP.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,13 @@ func (c *UDPConn) writeTo(raddr *net.UDPAddr, cm *ControlMessage, buffer []byte)
// because the connection is already established.
return c.connection.Write(buffer)
}
// On Linux, UDP network binds both IPv6 and IPv4 addresses to the same socket.
// When receiving a packet from an IPv4 address, we cannot send a packet from an IPv6 address.
// Therefore, we wrap the connection using an IPv4 packet connection (packetConn).
if !IsIPv6(raddr.IP) && c.packetConn.IsIPv6() {
pc := packetConnIPv4{packetConn: ipv4.NewPacketConn(c.connection)}
return pc.WriteTo(buffer, cm, raddr)
}
return c.packetConn.WriteTo(buffer, cm, raddr)
}

Expand Down
78 changes: 52 additions & 26 deletions net/connUDP_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package net
import (
"context"
"net"
"runtime"
"strconv"
"sync"
"testing"
Expand All @@ -19,53 +20,63 @@ const (
)

func TestUDPConnWriteWithContext(t *testing.T) {
peerAddr := "127.0.0.1:2154"
iface := getInterfaceIndex(t)
ifaceIpv4 := getIfaceAddr(t, iface, true)
peerAddr := ifaceIpv4.String() + ":2154"
b, err := net.ResolveUDPAddr(udpNetwork, peerAddr)
require.NoError(t, err)

ctxCanceled, ctxCancel := context.WithCancel(context.Background())
ctxCancel()

type args struct {
ctx context.Context
udpAddr *net.UDPAddr
buffer []byte
ctx context.Context
listenNetwork string
udpAddr *net.UDPAddr
buffer []byte
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "valid",
name: "valid - udp4 network",
args: args{
ctx: context.Background(),
udpAddr: b,
buffer: []byte("hello world"),
ctx: context.Background(),
listenNetwork: udp4Network,
udpAddr: b,
buffer: []byte("hello world"),
},
},
{
name: "cancelled",
args: args{
ctx: ctxCanceled,
buffer: []byte("hello world"),
ctx: ctxCanceled,
listenNetwork: udp4Network,
buffer: []byte("hello world"),
},
wantErr: true,
},
}
if runtime.GOOS == "linux" {
tests = append(tests, struct {
name string
args args
wantErr bool
}{
name: "valid - udp network",
args: args{
ctx: context.Background(),
listenNetwork: udpNetwork,
udpAddr: b,
buffer: []byte("hello world"),
},
})
}

a, err := net.ResolveUDPAddr(udpNetwork, "127.0.0.1:")
require.NoError(t, err)
l1, err := net.ListenUDP(udpNetwork, a)
require.NoError(t, err)
c1 := NewUDPConn(udpNetwork, l1, WithErrors(func(err error) { t.Log(err) }))
defer func() {
errC := c1.Close()
require.NoError(t, errC)
}()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

l2, err := net.ListenUDP(udpNetwork, b)
require.NoError(t, err)
c2 := NewUDPConn(udpNetwork, l2, WithErrors(func(err error) { t.Log(err) }))
Expand All @@ -84,7 +95,19 @@ func TestUDPConnWriteWithContext(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err = c1.WriteWithContext(tt.args.ctx, tt.args.udpAddr, tt.args.buffer)
a, err := net.ResolveUDPAddr(tt.args.listenNetwork, ":")
require.NoError(t, err)
l1, err := net.ListenUDP(tt.args.listenNetwork, a)
require.NoError(t, err)
c1 := NewUDPConn(tt.args.listenNetwork, l1, WithErrors(func(err error) { t.Log(err) }))
defer func() {
errC := c1.Close()
require.NoError(t, errC)
}()

err = c1.WriteWithOptions(tt.args.buffer, WithContext(ctx), WithRemoteAddr(tt.args.udpAddr), WithControlMessage(&ControlMessage{
IfIndex: iface.Index,
}))

c1.LocalAddr()
c1.RemoteAddr()
Expand Down Expand Up @@ -312,18 +335,21 @@ func isActiveMulticastInterface(iface net.Interface) bool {
return false
}

func TestUDPConnWriteToAddr(t *testing.T) {
func getInterfaceIndex(t *testing.T) net.Interface {
ifaces, err := net.Interfaces()
require.NoError(t, err)
var iface net.Interface
for _, i := range ifaces {
t.Logf("interface name:%v, flags: %v", i.Name, i.Flags)
if isActiveMulticastInterface(i) {
iface = i
break
return i
}
}
require.NotEmpty(t, iface)
require.Fail(t, "No suitable interface found")
return net.Interface{}
}

func TestUDPConnWriteToAddr(t *testing.T) {
iface := getInterfaceIndex(t)
type args struct {
iface *net.Interface
src net.IP
Expand Down
Loading