Skip to content

Commit 2e9a1b3

Browse files
committed
fix for windows/macos
1 parent 164d34e commit 2e9a1b3

File tree

5 files changed

+37
-19
lines changed

5 files changed

+37
-19
lines changed

net/connUDP.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ type UDPConn struct {
2424
}
2525

2626
type ControlMessage struct {
27-
Dst net.IP // destination address, receiving only
28-
Src net.IP // source address, specifying only
27+
// For connection oriented packetConn the ControlMessage fields are ignored, only linux supports set control message.
28+
29+
Dst net.IP // destination address of the packet
30+
Src net.IP // source address of the packet
2931
IfIndex int // interface index, 0 means any interface
3032
}
3133

@@ -524,6 +526,16 @@ func (c *UDPConn) writeMulticast(ctx context.Context, raddr *net.UDPAddr, buffer
524526
return nil
525527
}
526528

529+
func (c *UDPConn) writeTo(raddr *net.UDPAddr, cm *ControlMessage, buffer []byte) (int, error) {
530+
if !supportsOverrideRemoteAddr(c.connection) {
531+
// If the remote address is set, we can use it as the destination address
532+
// because the connection is already established.
533+
// Note: Overwriting the destination address is only supported on Linux.
534+
return c.connection.Write(buffer)
535+
}
536+
return c.packetConn.WriteTo(buffer, cm, raddr)
537+
}
538+
527539
// WriteWithContext writes data with context.
528540
func (c *UDPConn) WriteWithContext(ctx context.Context, raddr *net.UDPAddr, cm *ControlMessage, buffer []byte) error {
529541
if raddr == nil {
@@ -538,7 +550,7 @@ func (c *UDPConn) WriteWithContext(ctx context.Context, raddr *net.UDPAddr, cm *
538550
if c.closed.Load() {
539551
return ErrConnectionIsClosed
540552
}
541-
n, err := c.packetConn.WriteTo(buffer, cm, raddr)
553+
n, err := c.writeTo(raddr, cm, buffer)
542554
if err != nil {
543555
return err
544556
}

net/connUDP_internal_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,20 +369,25 @@ func TestUDPConnWriteToAddr(t *testing.T) {
369369
}
370370
}
371371

372-
func TestPacketConnIPv4ReadFrom(t *testing.T) {
372+
func TestPacketConnReadFrom(t *testing.T) {
373373
readUDP4Conn, err := net.ListenUDP("udp4", &net.UDPAddr{Port: 1234})
374374
require.NoError(t, err)
375375
defer func() {
376376
errC := readUDP4Conn.Close()
377377
require.NoError(t, errC)
378378
}()
379+
380+
require.Nil(t, readUDP4Conn.RemoteAddr())
381+
379382
writeUDP4Conn, err := net.DialUDP("udp4", nil, readUDP4Conn.LocalAddr().(*net.UDPAddr))
380383
require.NoError(t, err)
381384
defer func() {
382385
errC := writeUDP4Conn.Close()
383386
require.NoError(t, errC)
384387
}()
385388

389+
require.NotNil(t, writeUDP4Conn.RemoteAddr())
390+
386391
readUDP6Conn, err := net.ListenUDP("udp6", &net.UDPAddr{Port: 1235})
387392
require.NoError(t, err)
388393
defer func() {

net/supportsOverrideRemoteAddr.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//go:build !linux
2+
3+
package net
4+
5+
import "net"
6+
7+
func supportsOverrideRemoteAddr(c *net.UDPConn) bool {
8+
return c.RemoteAddr() == nil
9+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package net
2+
3+
import "net"
4+
5+
func supportsOverrideRemoteAddr(*net.UDPConn) bool {
6+
return true
7+
}

net/udp.go

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)