Skip to content

Commit

Permalink
fix for windows/macos
Browse files Browse the repository at this point in the history
  • Loading branch information
jkralik committed Nov 13, 2023
1 parent 164d34e commit 9b60602
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
12 changes: 10 additions & 2 deletions net/connUDP.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ type UDPConn struct {
}

type ControlMessage struct {
Dst net.IP // destination address, receiving only
Src net.IP // source address, specifying only
// For connection oriented packetConn the ControlMessage fields are ignored, only linux supports set control message.

Dst net.IP // destination address of the packet
Src net.IP // source address of the packet
IfIndex int // interface index, 0 means any interface
}

Expand Down Expand Up @@ -538,6 +540,12 @@ func (c *UDPConn) WriteWithContext(ctx context.Context, raddr *net.UDPAddr, cm *
if c.closed.Load() {
return ErrConnectionIsClosed
}
if !supportsSetPacketControlMessage(c.connection) {
// if remote address is set, we can use it as destination address
// because connection is already connected
// only linux supports overwriting destination address
raddr = nil
}
n, err := c.packetConn.WriteTo(buffer, cm, raddr)
if err != nil {
return err
Expand Down
7 changes: 6 additions & 1 deletion net/connUDP_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,20 +369,25 @@ func TestUDPConnWriteToAddr(t *testing.T) {
}
}

func TestPacketConnIPv4ReadFrom(t *testing.T) {
func TestPacketConnReadFrom(t *testing.T) {
readUDP4Conn, err := net.ListenUDP("udp4", &net.UDPAddr{Port: 1234})
require.NoError(t, err)
defer func() {
errC := readUDP4Conn.Close()
require.NoError(t, errC)
}()

require.Nil(t, readUDP4Conn.RemoteAddr())

writeUDP4Conn, err := net.DialUDP("udp4", nil, readUDP4Conn.LocalAddr().(*net.UDPAddr))
require.NoError(t, err)
defer func() {
errC := writeUDP4Conn.Close()
require.NoError(t, errC)
}()

require.NotNil(t, writeUDP4Conn.RemoteAddr())

readUDP6Conn, err := net.ListenUDP("udp6", &net.UDPAddr{Port: 1235})
require.NoError(t, err)
defer func() {
Expand Down
9 changes: 9 additions & 0 deletions net/supportsSetPacketControlMessage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build !linux

package net

import "net"

func supportsSetPacketControlMessage(c *net.UDPConn) bool {
return c.RemoteAddr() == nil
}
7 changes: 7 additions & 0 deletions net/supportsSetPacketControlMessage_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net

import "net"

func supportsSetPacketControlMessage(*net.UDPConn) bool {
return true
}

0 comments on commit 9b60602

Please sign in to comment.