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

net: DialUDP unexpected behavior when laddr not nil #29451

Closed
Astone-Chou opened this issue Dec 29, 2018 · 2 comments

Comments

Projects
None yet
2 participants
@Astone-Chou
Copy link

commented Dec 29, 2018

What version of Go are you using (go version)?

go version go1.9 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
GOARCH="amd64"
GOBIN="/root/Astone/git/bin"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/root/Astone/.go:/root/Astone/git"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build412440145=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

What did you do?

I wanted to implement a UDP accept.

package main

import (
	"flag"
	"fmt"
	"net"
	"time"
)

var (
	c = flag.Bool("c", false, "client")
	s = flag.Bool("s", false, "server")
)

var (
	times = 10
	port  = "19880"
)

func C() {
	addr, err := net.ResolveUDPAddr("udp", "127.0.0.1:"+port)
	if err != nil {
		panic(err)
	}
	var clt net.Conn
	clt, err = net.DialUDP("udp", nil, addr)
	if err != nil {
		panic(err)
	}
	for i := 0; i < times; i++ {
		data := fmt.Sprintf("%d:%s\n", i, "Hello")
		fmt.Print("Send:", data)
		time.Sleep(time.Second)
		_, err := clt.Write([]byte(data))
		if err != nil {
			fmt.Println("ERROR:", err)
		}
	}
}

func S() {
	laddr, err := net.ResolveUDPAddr("udp", ":"+port)
	if err != nil {
		panic(err)
	}
	svr, err := net.ListenUDP("udp", laddr)
	if err != nil {
		panic(err)
	}
	for {
		c, err := accept(svr)
		if err != nil {
			panic(err)
		}
		go serve(c)
		fmt.Println("Accept Success", c.RemoteAddr())
	}
}

func accept(svr *net.UDPConn) (*net.UDPConn, error) {
	var buffer [1024]byte
	_, raddr, err := svr.ReadFromUDP(buffer[:])
	if err != nil {
		return nil, err
	}

	// auth
	// if n != "SYN" {
	//	return errors.New("invalid")
	// }

	return net.DialUDP("udp", svr.LocalAddr().(*net.UDPAddr), raddr)
}

func serve(con *net.UDPConn) {
	var buffer [1024]byte
	for {
		n, err := con.Read(buffer[:])
		if err != nil {
			fmt.Println(err)
			return
		}
		fmt.Println(string(buffer[:n]))
	}
}

func main() {
	flag.Parse()
	switch {
	case *c:
		C()
		return
	case *s:
		S()
		return
	}
}

when i start server and client i got an panic.

panic: dial udp [::]:19880->127.0.0.1:35398: bind: address already in use

I think it because of missing SO_REUSEADDR ;
so i modify net/sock_posix.go call setDefaultListenerSockopts in (*netFD).listenDatagram and (*netFD).dial;

 if err := setDefaultListenerSockopts(fd.pfd.Sysfd); err != nil {
         return err
 }

It's seems working well. I can implement the udp accept now.
but i have to modify the source code.

What did you expect to see?

What did you see instead?

@crvv

This comment has been minimized.

Copy link
Contributor

commented Jan 2, 2019

You can use net.Dialer.Control and net.ListenConfig.Control to do what you want(setsockopt and bind).

But your code is strange and it is not portable. It doesn't work on macOS.
It surprised me that it works on Linux.

This is not a bug of Go stdlib.

@Astone-Chou

This comment has been minimized.

Copy link
Author

commented Jan 2, 2019

Thanks very much. i go it. by using SyscallConn to get RawConn.

@Astone-Chou Astone-Chou closed this Jan 2, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.