Sending UDPv6 locally in golang

若如初见. 提交于 2019-12-12 03:52:42

问题


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.



来源:https://stackoverflow.com/questions/42175773/sending-udpv6-locally-in-golang

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!