本文整理汇总了Golang中golang.org/x/net/icmp.Message.Type方法的典型用法代码示例。如果您正苦于以下问题:Golang Message.Type方法的具体用法?Golang Message.Type怎么用?Golang Message.Type使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类golang.org/x/net/icmp.Message
的用法示例。
在下文中一共展示了Message.Type方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: probeICMP
func probeICMP(target string, w http.ResponseWriter, module Module) (success bool) {
deadline := time.Now().Add(module.Timeout)
socket, err := icmp.ListenPacket("ip4:icmp", "0.0.0.0")
if err != nil {
log.Errorf("Error listening to socket: %s", err)
return
}
defer socket.Close()
ip, err := net.ResolveIPAddr("ip4", target)
if err != nil {
log.Errorf("Error resolving address %s: %s", target, err)
return
}
seq := getICMPSequence()
pid := os.Getpid() & 0xffff
wm := icmp.Message{
Type: ipv4.ICMPTypeEcho, Code: 0,
Body: &icmp.Echo{
ID: pid, Seq: int(seq),
Data: []byte("Prometheus Blackbox Exporter"),
},
}
wb, err := wm.Marshal(nil)
if err != nil {
log.Errorf("Error marshalling packet for %s: %s", target, err)
return
}
if _, err := socket.WriteTo(wb, ip); err != nil {
log.Errorf("Error writing to socker for %s: %s", target, err)
return
}
// Reply should be the same except for the message type.
wm.Type = ipv4.ICMPTypeEchoReply
wb, err = wm.Marshal(nil)
if err != nil {
log.Errorf("Error marshalling packet for %s: %s", target, err)
return
}
rb := make([]byte, 1500)
if err := socket.SetReadDeadline(deadline); err != nil {
log.Errorf("Error setting socket deadline for %s: %s", target, err)
return
}
for {
n, peer, err := socket.ReadFrom(rb)
if err != nil {
if nerr, ok := err.(net.Error); ok && nerr.Timeout() {
log.Infof("Timeout reading from socket for %s: %s", target, err)
return
}
log.Errorf("Error reading from socket for %s: %s", target, err)
continue
}
if peer.String() != ip.String() {
continue
}
if bytes.Compare(rb[:n], wb) == 0 {
success = true
return
}
}
return
}
示例2: Pinger
// Pinger pings requested address.
// It returns nil when IcmpEchoReplay is recived, error otherwishe.
func Pinger(address string, timeout time.Duration) (err error) {
addr, err := net.ResolveIPAddr("ip", address)
if err != nil {
return err
}
network := "ip4:icmp"
if addr.IP.To4() == nil {
network = "ip6:ipv6-icmp"
}
c, err := icmp.ListenPacket(network, address)
if err != nil {
return err
}
defer func() {
if cerr := c.Close(); cerr != nil && err == nil {
err = cerr
}
}()
wm := icmp.Message{
Type: ipv4.ICMPTypeEcho, Code: 0,
Body: &icmp.Echo{
ID: os.Getpid() & 0xffff, Seq: 1,
Data: []byte("ping"),
},
}
if addr.IP.To4() == nil {
wm.Type = ipv6.ICMPTypeEchoRequest
}
wb, err := wm.Marshal(nil)
if err != nil {
return err
}
if err := c.SetDeadline(time.Now().Add(timeout)); err != nil {
return err
}
if _, err = c.WriteTo(wb, addr); err != nil {
return err
}
rb := make([]byte, 1500)
n, _, err := c.ReadFrom(rb)
if err != nil {
return err
}
rm, err := icmp.ParseMessage(ProtocolICMP, rb[:n])
if err != nil {
return err
}
if addr.IP.To4() == nil {
if rm.Type == ipv6.ICMPTypeEchoReply {
return ErrNoImcpReplay
}
return nil
}
if rm.Type == ipv4.ICMPTypeEchoReply {
return ErrNoImcpReplay
}
return nil
}
示例3: Ping
// Sends a single ICMP echo to an IP and returns success and latency information.
// Borrowed from BrianBrazil's blackbox exporter
func Ping(ip net.IP, maxRTT time.Duration) (success bool, latency time.Duration) {
deadline := time.Now().Add(maxRTT)
var socket *icmp.PacketConn
var err error
if isIPv4(ip) {
socket, err = icmp.ListenPacket("ip4:icmp", "0.0.0.0")
} else if isIPv6(ip) {
socket, err = icmp.ListenPacket("ip6:ipv6-icmp", "::")
} else {
log.Errorln("IP did not match any known types?")
return
}
if err != nil {
log.Errorf("Error listening to socket: %s", err)
return
}
defer socket.Close()
seq := getICMPSequence()
pid := os.Getpid() & 0xffff
// Build the packet
var wm icmp.Message
if isIPv4(ip) {
wm = icmp.Message{
Type: ipv4.ICMPTypeEcho, Code: 0,
Body: &icmp.Echo{
ID: pid, Seq: int(seq),
Data: []byte("poller_exporter"),
},
}
} else if isIPv6(ip) {
wm = icmp.Message{
Type: ipv6.ICMPTypeEchoRequest, Code: 0,
Body: &icmp.Echo{
ID: pid, Seq: int(seq),
Data: []byte("poller_exporter"),
},
}
} else {
log.Errorln("IP did not match any known types?")
return
}
wb, err := wm.Marshal(nil)
if err != nil {
log.Errorf("Error marshalling packet for %s: %s", ip.String(), err)
return
}
sendTime := time.Now()
var dst *net.IPAddr
dst = &net.IPAddr{IP: ip}
if _, err := socket.WriteTo(wb, dst); err != nil {
log.Errorf("Error writing to socket for %s: %s", ip.String(), err)
return
}
// Reply should be the same except for the message type.
if isIPv4(ip) {
wm.Type = ipv4.ICMPTypeEchoReply
} else if isIPv6(ip) {
wm.Type = ipv6.ICMPTypeEchoReply
} else {
log.Errorln("IP did not match any known types?")
return
}
wb, err = wm.Marshal(nil)
if err != nil {
log.Errorf("Error marshalling packet for %s: %s", ip.String(), err)
return
}
rb := make([]byte, 1500)
if err := socket.SetReadDeadline(deadline); err != nil {
log.Errorf("Error setting socket deadline for %s: %s", ip.String(), err)
return
}
for {
n, peer, err := socket.ReadFrom(rb)
if err != nil {
if nerr, ok := err.(net.Error); ok && nerr.Timeout() {
log.Infof("Timeout reading from socket for %s: %s", ip.String(), err)
return
}
log.Errorf("Error reading from socket for %s: %s", ip.String(), err)
continue
}
if peer.String() != ip.String() {
continue
}
if bytes.Compare(rb[:n], wb) == 0 {
success = true
//.........这里部分代码省略.........
示例4: probeICMP
func probeICMP(target string, w http.ResponseWriter, module Module) (success bool) {
var (
socket *icmp.PacketConn
requestType icmp.Type
replyType icmp.Type
fallbackProtocol string
)
deadline := time.Now().Add(module.Timeout)
// Defaults to IPv4 to be compatible with older versions
if module.ICMP.Protocol == "" {
module.ICMP.Protocol = "icmp"
}
// In case of ICMP prefer IPv6 by default
if module.ICMP.Protocol == "icmp" && module.ICMP.PreferredIpProtocol == "" {
module.ICMP.PreferredIpProtocol = "ip6"
}
if module.ICMP.Protocol == "icmp4" {
module.ICMP.PreferredIpProtocol = "ip4"
fallbackProtocol = ""
} else if module.ICMP.Protocol == "icmp6" {
module.ICMP.PreferredIpProtocol = "ip6"
fallbackProtocol = ""
} else if module.ICMP.PreferredIpProtocol == "ip6" {
fallbackProtocol = "ip4"
} else {
fallbackProtocol = "ip6"
}
ip, err := net.ResolveIPAddr(module.ICMP.PreferredIpProtocol, target)
if err != nil && fallbackProtocol != "" {
ip, err = net.ResolveIPAddr(fallbackProtocol, target)
}
if err != nil {
log.Errorf("Error resolving address %s: %s", target, err)
}
if ip.IP.To4() == nil {
requestType = ipv6.ICMPTypeEchoRequest
replyType = ipv6.ICMPTypeEchoReply
socket, err = icmp.ListenPacket("ip6:ipv6-icmp", "::")
fmt.Fprintf(w, "probe_ip_protocol 6\n")
} else {
requestType = ipv4.ICMPTypeEcho
replyType = ipv4.ICMPTypeEchoReply
socket, err = icmp.ListenPacket("ip4:icmp", "0.0.0.0")
fmt.Fprintf(w, "probe_ip_protocol 4\n")
}
if err != nil {
log.Errorf("Error listening to socket: %s", err)
return
}
defer socket.Close()
if err != nil {
log.Errorf("Error resolving address %s: %s", target, err)
return
}
seq := getICMPSequence()
pid := os.Getpid() & 0xffff
wm := icmp.Message{
Type: requestType,
Code: 0,
Body: &icmp.Echo{
ID: pid, Seq: int(seq),
Data: []byte("Prometheus Blackbox Exporter"),
},
}
wb, err := wm.Marshal(nil)
if err != nil {
log.Errorf("Error marshalling packet for %s: %s", target, err)
return
}
if _, err := socket.WriteTo(wb, ip); err != nil {
log.Errorf("Error writing to socker for %s: %s", target, err)
return
}
// Reply should be the same except for the message type.
wm.Type = replyType
wb, err = wm.Marshal(nil)
if err != nil {
log.Errorf("Error marshalling packet for %s: %s", target, err)
return
}
rb := make([]byte, 1500)
if err := socket.SetReadDeadline(deadline); err != nil {
log.Errorf("Error setting socket deadline for %s: %s", target, err)
return
}
for {
n, peer, err := socket.ReadFrom(rb)
//.........这里部分代码省略.........