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

UDP RemoteAddr() returns always (wrong) IPv6 #35

Closed
darinkes opened this issue Nov 11, 2019 · 6 comments
Closed

UDP RemoteAddr() returns always (wrong) IPv6 #35

darinkes opened this issue Nov 11, 2019 · 6 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@darinkes
Copy link

Describe the bug
gnet.Conn.RemoteAddr() returns always IPv6 for UDP.
E.g. [::7f00:1]:52815 instead of 127.0.0.1:52815

To Reproduce

func (this *Server) React(c gnet.Conn) (out []byte, action gnet.Action) {
        data := c.Read()
        log.Debugf("DATA from %v to %v: %v", c.RemoteAddr(), c.LocalAddr(), data)
        c.ResetBuffer()
        return
}

func (this *Server) listen() error {                                                                                                                                                              
        listenAddr := fmt.Sprintf("udp://%s", this.cfg.Bind)                                                                                                                                      
        err := gnet.Serve(this, listenAddr, gnet.WithMulticore(true), gnet.WithReusePort(true))
        if err != nil {                                                                                                                                                                                   return fmt.Errorf("Failed to create listening udp socket: %v", err)
        }

        return nil
}   

Expected behavior
Correct RemoteAddr

System Info (please complete the following information):

  • OS (e.g. Linux): Linux
  • Go Version (e.g. Go 1.13): go1.12.10 linux/amd64
  • gnet Version (e.g. v1.0.0-beta.3): gnet@v1.0.0-rc.3

Additional context

in eventloop.go SockaddrToUDPAddr gets always called with sa6 = unix.SockaddrInet6

func (lp *loop) loopUDPIn(fd int) error {
        c := &conn{
                fd:            fd,
                localAddr:     lp.svr.ln.lnaddr,
                remoteAddr:    netpoll.SockaddrToUDPAddr(&sa6),
                inboundBuffer: lp.svr.bytesPool.Get().(*ringbuffer.RingBuffer),
        }
}

in netpoll/socktoaddr.go

func SockaddrToUDPAddr(sa unix.Sockaddr) *net.UDPAddr {
        case *unix.SockaddrInet6:
                return &net.UDPAddr{IP: ip, Port: sa.Port, Zone: zone}
        }
}

So you always get an SockaddrInet converted to SockaddrInet6 for UDP.

@darinkes darinkes added the bug Something isn't working label Nov 11, 2019
@panjf2000
Copy link
Owner

panjf2000 commented Nov 11, 2019

Hi @darinkes
I believe that Go will give priority to the IPv6 when the server supports both IPv4 and IPv6, have you ever tried "udp4"?

@darinkes
Copy link
Author

According to tcpdump its an IPv4 connection from 127.0.0.1 to 127.0.0.1.
E.g. listenAddr is "udp://127.0.0.1:5555"
Works fine with net.ListenUDP("udp", listenAddr), which also gives a correct RemoteAddr()

[::7f00:1] is the result of mangled data cause of the bug in eventloop.go

udp4 does not seem to exist:

listen udp4 127.0.0.1:5555: address 127.0.0.1:5555: unexpected address type

@panjf2000
Copy link
Owner

panjf2000 commented Nov 11, 2019

I've run a test with UDP on my linux server and printed some logs:
image
It seems that everything goes well, so could you make sure that the listenAddr := fmt.Sprintf("udp://%s", this.cfg.Bind) passes the right arguments to gnet?Maybe you print the whole string of it and check it out?

@panjf2000
Copy link
Owner

panjf2000 commented Nov 11, 2019

BTY, you could update to the latest code of gnet and try it again, let's see whether the issue still exists.

@darinkes
Copy link
Author

func (this *Server) OnInitComplete(srv gnet.Server) (action gnet.Action) {
        log.Debugf("Server is listening on %s (multi-cores: %t, loops: %d)", srv.Addr.String(), srv.Multicore, srv.NumLoops)
        return
}

func (this *Server) React(c gnet.Conn) (out []byte, action gnet.Action) {
        log.Debugf("%v -> %v", c.RemoteAddr(), c.LocalAddr())
}

Results in

2019-11-11 15:25:15 [DEBUG] (udp/server): Server is listening on 127.0.0.1:5555 (multi-cores: true, loops: 24)
2019-11-11 15:25:16 [DEBUG] (udp/server): [::7f00:1]:55170 -> 127.0.0.1:5555

I saw you fixed it in master now: ec7bd36 👍
Thats nearly the same fix I also applied in my local gnet checkout and it works.

@darinkes
Copy link
Author

checked out master and now it shows

2019-11-11 15:44:00 [DEBUG] (udp/server): 127.0.0.1:44772 -> 127.0.0.1:5555

Thanks for fixing this so quick

@panjf2000 panjf2000 added this to the v1 milestone Mar 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants