Sending UDPv6 locally in golang

2019-08-17 03:16发布

问题:

I need to send a UDPv6 datagram being able to track this message by a local receiver (or via tcpdump).

daddr, err = net.ResolveUDPAddr("udp6", "[address]:port")
if err != nil {
    return err
}

conn, err := net.DialUDP("udp6", nil, daddr)
if err != nil {
    return err
}
defer conn.Close()

conn.Write(...)

Unlike IPv4 this code doesn't work with IPv6. For example, when I try to send a datagram to a multicast address, e.g. to [FF01::DB8:0:0]:5000, I get connect: invalid argument. The same happens when I try to send it to [fe80::20c:29ff:fee1:d66]:5000 (my IPv6 address according to ifconfig).

回答1:

In both cases (link-local and interface-local multicast) you have forgotten to specify the scope ID. Without this, it is impossible to determine which interface to use, and you get an Invalid argument error from the operating system.

net.UDPAddr uses the Zone field to store the scope ID. You need to ensure that you have provided one, either by setting Zone explicitly or by using the percent-suffix notation.



标签: go udp ipv6