在 GO 中获取传入的 TTL [英] Getting incoming TTL in GO
本文介绍了在 GO 中获取传入的 TTL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在为我正在进行的一个小项目而苦苦挣扎.我想在 GO 中实现功能,允许我在传出的 UDP 数据包上设置 IP 标头 TTL,然后我发送该数据包并在另一端查看接收到的 TTL.我尝试使用net"库提供的许多连接,但到目前为止没有成功(我可以设置 TTL,但我无法读取它).谁能建议一种发送和接收 UDP 数据包的方法,以提供对数据包 TTL 字段的访问权限?
I'm struggling with a small project I am working on. I want to implement functionality in GO that allows me to set the IP header TTL on an outgoing UDP packet that I then send and view the received TTL on the other end. I have a tried using a number of connections provided by the 'net' library and have so far had no success (I can set the TTL but I can't read it). Can anyone suggest a way of sending and receiving UDP packets that provide access to the packet's TTL field?
推荐答案
设置TTL:
var conn *icmp.PacketConn
// for IPv4
conn.IPv4PacketConn().SetTTL(p.ttl)
conn.IPv4PacketConn().SetControlMessage(ipv4.FlagTTL, true)
// for IPv6
conn.IPv6PacketConn().SetHopLimit(p.ttl)
conn.IPv6PacketConn().SetControlMessage(ipv6.FlagHopLimit, true)
接收 TTL:
var ttl, n int
bytes := make([]byte, 512)
if IPv4 {
var cm *ipv4.ControlMessage
n, cm, _, err = conn.IPv4PacketConn().ReadFrom(bytes)
if cm != nil {
ttl = cm.TTL
}
} else {
var cm *ipv6.ControlMessage
n, cm, _, err = conn.IPv6PacketConn().ReadFrom(bytes)
if cm != nil {
ttl = cm.HopLimit
}
}
这篇关于在 GO 中获取传入的 TTL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文