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

x/net:multicast server cannot send a unicast packet #54520

Open
mjtansin opened this issue Aug 18, 2022 · 1 comment
Open

x/net:multicast server cannot send a unicast packet #54520

mjtansin opened this issue Aug 18, 2022 · 1 comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@mjtansin
Copy link

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

$ go version

1.18

Does this issue reproduce with the latest release?

yes

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

go env ubuntu/amd64
$ go env

What did you do?

  1. setup a udp server listening multicast traffic
  2. client sends a multicast packet "hello"
  3. server sends a packet "world" back to the client
    test code

What did you expect to see?

client will receive "world" from the server

What did you see instead?

the program runs no any error, the server received "hello", but the client cannot get "world" packet.

@gopherbot gopherbot added this to the Unreleased milestone Aug 18, 2022
@mjtansin mjtansin changed the title x/net: x/net:multicast server cannot send a unicast packet Aug 18, 2022
@joedian joedian added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Aug 18, 2022
@ZekeLu
Copy link
Contributor

ZekeLu commented Aug 30, 2022

In fact, the server DO send the unicast packet. See the tcpdump log below:

$ sudo tcpdump -#nqA --interface='any' port 1025 or icmp
tcpdump: data link type LINUX_SLL2
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on any, link-type LINUX_SLL2 (Linux cooked v2), snapshot length 262144 bytes
    1  17:25:24.264290 wlp5s0 Out IP 192.168.3.201.41397 > 224.0.0.248.1025: UDP, length 5
E..!.1@....0..............p.hello
    2  17:25:24.264349 lo    In  IP 192.168.3.201.1025 > 192.168.3.201.41397: UDP, length 6
E.."M.@.@.d ................world!
    3  17:25:24.264355 lo    In  IP 192.168.3.201 > 192.168.3.201: ICMP 192.168.3.201 udp port 41397 unreachable, length 42
E..>.Q..@.E............8....E.."M.@.@.d ................world!

The log also shows an error reported by ICMP: udp port 41397 unreachable. That's the reason why the client does not get the packet.

Here is the list of sockets we are interested in:

$ netstat -una
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
udp        0      0 192.168.3.201:41397     224.0.0.248:1025        ESTABLISHED
udp        0      0 0.0.0.0:1025            0.0.0.0:*                          

I guess that the socket 192.168.3.201:41397 <-> 224.0.0.248:1025 only receives packets from 224.0.0.248:1025. That explains why it does not receive packets from 192.168.3.201:1025.

So we can modify the client like this:

--- a/main.go
+++ b/main.go
@@ -47,13 +47,14 @@ func SetupMulticast(ifn, addr string) {
        }
    }()
    //client
-   if conn, err2 := net.DialUDP("udp4", nil /*src*/, gAddr); err2 != nil {
+   if conn, err2 := net.ListenPacket("udp4", "0.0.0.0:0"); err2 != nil {
        log.Fatal(err)
    } else {
+       p2 := ipv4.NewPacketConn(conn)
        go func() {
            for {
                log.Println("write hello...")
-               conn.Write([]byte("hello"))
+               p2.WriteTo([]byte("hello"), nil, gAddr)
 
                time.Sleep(time.Second * 2)
            }
@@ -61,11 +62,11 @@ func SetupMulticast(ifn, addr string) {
        go func() {
            b2 := make([]byte, 1500)
            for {
-               n, err := conn.Read(b2)
+               n, _, src, err := p2.ReadFrom(b2)
                if err != nil {
                    log.Panic(err)
                }
-               log.Printf("sender received response:%s\n", string(b2[:n]))
+               log.Printf("sender received response:%s. src: %s\n", string(b2[:n]), src)
            }
        }()

Here is the full demo: https://go.dev/play/p/vu5fUS_PTab

Please note that I'm not a network expert, and I'm not familiar with this package either. I'm not sure whether the suggested implementation is the best practice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants