本文整理汇总了Golang中golang.org/x/net/icmp.PacketConn.ReadFrom方法的典型用法代码示例。如果您正苦于以下问题:Golang PacketConn.ReadFrom方法的具体用法?Golang PacketConn.ReadFrom怎么用?Golang PacketConn.ReadFrom使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类golang.org/x/net/icmp.PacketConn
的用法示例。
在下文中一共展示了PacketConn.ReadFrom方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: recvICMP
func (p *Pinger) recvICMP(conn *icmp.PacketConn, ctx *context, wg *sync.WaitGroup) {
for {
select {
case <-ctx.stop:
wg.Done()
return
default:
}
bytes := make([]byte, 512)
conn.SetReadDeadline(time.Now().Add(time.Millisecond * 100))
_, ra, err := conn.ReadFrom(bytes)
if err != nil {
if neterr, ok := err.(*net.OpError); ok {
if neterr.Timeout() {
continue
} else {
// prevent 2x close in different threads
p.mu.Lock()
if ctx.err == nil {
close(ctx.done)
}
ctx.err = err
p.mu.Unlock()
wg.Done()
return
}
}
}
p.procRecv(bytes, ra, ctx)
}
}
示例2: recvICMP
func (p *Pinger) recvICMP(
conn *icmp.PacketConn,
recv chan<- *packet,
wg *sync.WaitGroup,
) {
defer wg.Done()
for {
select {
case <-p.done:
return
default:
bytes := make([]byte, 512)
conn.SetReadDeadline(time.Now().Add(time.Millisecond * 100))
n, _, err := conn.ReadFrom(bytes)
if err != nil {
if neterr, ok := err.(*net.OpError); ok {
if neterr.Timeout() {
// Read timeout
continue
} else {
close(p.done)
return
}
}
}
recv <- &packet{bytes: bytes, nbytes: n}
}
}
}
示例3: recvICMP
func (p *Pinger) recvICMP(conn *icmp.PacketConn, recv chan<- *packet, ctx *context, wg *sync.WaitGroup) {
p.debugln("recvICMP(): Start")
for {
select {
case <-ctx.stop:
p.debugln("recvICMP(): <-ctx.stop")
wg.Done()
p.debugln("recvICMP(): wg.Done()")
return
default:
}
bytes := make([]byte, 512)
conn.SetReadDeadline(time.Now().Add(time.Millisecond * 100))
p.debugln("recvICMP(): ReadFrom Start")
_, ra, err := conn.ReadFrom(bytes)
p.debugln("recvICMP(): ReadFrom End")
if err != nil {
if neterr, ok := err.(*net.OpError); ok {
if neterr.Timeout() {
p.debugln("recvICMP(): Read Timeout")
continue
} else {
p.debugln("recvICMP(): OpError happen", err)
p.mu.Lock()
ctx.err = err
p.mu.Unlock()
p.debugln("recvICMP(): close(ctx.done)")
close(ctx.done)
p.debugln("recvICMP(): wg.Done()")
wg.Done()
return
}
}
}
p.debugln("recvICMP(): p.recv <- packet")
select {
case recv <- &packet{bytes: bytes, addr: ra}:
case <-ctx.stop:
p.debugln("recvICMP(): <-ctx.stop")
wg.Done()
p.debugln("recvICMP(): wg.Done()")
return
}
}
}
示例4: ping
func ping(listener *icmp.PacketConn, message icmp.Message, raddr net.Addr, timeout time.Duration) (Pong, error) {
data, err := message.Marshal(nil)
if err != nil {
return Pong{}, err
}
_, err = listener.WriteTo(data, raddr)
if err != nil {
return Pong{}, err
}
now := time.Now()
done := make(chan Pong)
go func() {
for {
buf := make([]byte, 10000)
// bufio
n, peer, err := listener.ReadFrom(buf)
if err != nil {
return
}
since := time.Since(now)
input, err := icmp.ParseMessage(protocolICMP, buf[:n])
if err != nil {
return
}
if input.Type != ipv4.ICMPTypeEchoReply {
continue
}
echo := input.Body.(*icmp.Echo)
pong := Pong{
Peer: peer,
ID: echo.ID,
Seq: echo.Seq,
Data: echo.Data,
Size: n,
RTT: since,
}
done <- pong
return
}
}()
select {
case pong := <-done:
return pong, nil
case <-time.After(timeout):
return Pong{}, errors.New("Timeout")
}
}
示例5: testDNS
// This checks if we can convert an URL to an IP
func testDNS(pr *pingResponse, pi pingInfo, conn *icmp.PacketConn) {
start := time.Now()
msg := createMessage()
msg_bytes, err := msg.Marshal(nil)
if err != nil {
emsg := "Could not marshal the message to []byte."
logger.WriteString(emsg)
pr.err = errors.New(emsg)
return
}
ip, _ := net.LookupHost(pi.externalurl)
if _, err := conn.WriteTo(msg_bytes, &net.UDPAddr{IP: net.ParseIP(ip[0]), Zone: "en0"}); err != nil {
emsg := "Could not write to the internal ip address: " + ip[0]
logger.WriteString(emsg)
pr.external_url = false
pr.err = errors.New(emsg)
return
}
pr.external_url = true
response := make([]byte, 1500)
count, peer, err := conn.ReadFrom(response)
if err != nil {
emsg := "Could not read the response."
logger.WriteString(emsg)
pr.external_url = false
pr.err = errors.New(emsg)
return
}
_, err = icmp.ParseMessage(protocolICMP, response[:count])
if err != nil {
emsg := "Could not parse the message received."
logger.WriteString(emsg)
pr.external_url = false
pr.err = errors.New(emsg)
return
}
logger.WriteString("Response " + strconv.Itoa(sequence) + " received from " + peer.String() +
" after " + time.Now().Sub(start).String())
}
示例6: 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
//.........这里部分代码省略.........
示例7: runICMPRecv
func (l *icmpLoop) runICMPRecv(conn *icmp.PacketConn, proto int) {
for {
bytes := make([]byte, 512)
conn.SetReadDeadline(time.Now().Add(1 * time.Second))
_, addr, err := conn.ReadFrom(bytes)
if err != nil {
if neterr, ok := err.(*net.OpError); ok {
if neterr.Timeout() {
continue
} else {
// TODO: report error and quit loop
return
}
}
}
ts := time.Now()
m, err := icmp.ParseMessage(proto, bytes)
if err != nil {
continue
}
// process echo reply only
if m.Type != ipv4.ICMPTypeEchoReply && m.Type != ipv6.ICMPTypeEchoReply {
continue
}
echo, ok := m.Body.(*icmp.Echo)
if !ok {
continue
}
id := requestID{
addr: addr.String(),
proto: proto,
id: echo.ID,
seq: echo.Seq,
}
l.mutex.Lock()
ctx := l.requests[id]
if ctx != nil {
delete(l.requests, id)
}
l.mutex.Unlock()
// no return context available for echo reply -> handle next message
if ctx == nil {
continue
}
ctx.result <- requestResult{
packet: packet{
ts: ts,
addr: addr,
Type: m.Type,
Code: m.Code,
Checksum: m.Checksum,
Echo: *echo,
},
}
}
}
示例8: 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)
//.........这里部分代码省略.........