Skip to content

Commit

Permalink
net: disable SIO_UDP_CONNRESET behavior on windows.
Browse files Browse the repository at this point in the history
Fixes golang#5834.

LGTM=alex.brainman
R=golang-codereviews, bradfitz, alex.brainman, mikioh.mikioh, in60jp, iant
CC=golang-codereviews
https://golang.org/cl/149510043
  • Loading branch information
picoHz authored and alexbrainman committed Oct 9, 2014
1 parent 7abc8c4 commit 3114bd6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/net/fd_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,18 @@ func (fd *netFD) init() error {
fd.skipSyncNotif = true
}
}
// Disable SIO_UDP_CONNRESET behavior.
// http://support.microsoft.com/kb/263823
switch fd.net {
case "udp", "udp4", "udp6":
ret := uint32(0)
flag := uint32(0)
size := uint32(unsafe.Sizeof(flag))
err := syscall.WSAIoctl(fd.sysfd, syscall.SIO_UDP_CONNRESET, (*byte)(unsafe.Pointer(&flag)), size, nil, 0, &ret, nil, 0)
if err != nil {
return os.NewSyscallError("WSAIoctl", err)
}
}
fd.rop.mode = 'r'
fd.wop.mode = 'w'
fd.rop.fd = fd
Expand Down
36 changes: 36 additions & 0 deletions src/net/udp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"runtime"
"strings"
"testing"
"time"
)

func TestResolveUDPAddr(t *testing.T) {
Expand All @@ -34,6 +35,41 @@ func TestResolveUDPAddr(t *testing.T) {
}
}

func TestReadFromUDP(t *testing.T) {
ra, err := ResolveUDPAddr("udp", "127.0.0.1:7")
if err != nil {
t.Fatal(err)
}

la, err := ResolveUDPAddr("udp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}

c, err := ListenUDP("udp", la)
if err != nil {
t.Fatal(err)
}
defer c.Close()

_, err = c.WriteToUDP([]byte("a"), ra)
if err != nil {
t.Fatal(err)
}

err = c.SetDeadline(time.Now().Add(100 * time.Millisecond))
if err != nil {
t.Fatal(err)
}
b := make([]byte, 1)
_, _, err = c.ReadFromUDP(b)
if err == nil {
t.Fatal("ReadFromUDP should fail")
} else if !isTimeout(err) {
t.Fatal(err)
}
}

func TestWriteToUDP(t *testing.T) {
switch runtime.GOOS {
case "plan9":
Expand Down
1 change: 1 addition & 0 deletions src/syscall/ztypes_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ const (
IOC_WS2 = 0x08000000
SIO_GET_EXTENSION_FUNCTION_POINTER = IOC_INOUT | IOC_WS2 | 6
SIO_KEEPALIVE_VALS = IOC_IN | IOC_VENDOR | 4
SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12

// cf. http://support.microsoft.com/default.aspx?scid=kb;en-us;257460

Expand Down

0 comments on commit 3114bd6

Please sign in to comment.