mirror of
https://github.com/taigrr/go-fastping
synced 2025-01-18 05:03:15 -08:00
Added payload size
This commit is contained in:
parent
47b4079cf0
commit
c6c53385e0
19
fastping.go
19
fastping.go
@ -47,6 +47,15 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func byteSliceOfSize(n int) []byte {
|
||||||
|
b := make([]byte, n)
|
||||||
|
for i := 0; i < len(b); i++ {
|
||||||
|
b[i] = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
func timeToBytes(t time.Time) []byte {
|
func timeToBytes(t time.Time) []byte {
|
||||||
nsec := t.UnixNano()
|
nsec := t.UnixNano()
|
||||||
b := make([]byte, 8)
|
b := make([]byte, 8)
|
||||||
@ -100,6 +109,9 @@ type Pinger struct {
|
|||||||
hasIPv6 bool
|
hasIPv6 bool
|
||||||
ctx *context
|
ctx *context
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
|
|
||||||
|
// Size in bytes of the payload to send
|
||||||
|
Size int
|
||||||
// Number of (nano,milli)seconds of an idle timeout. Once it passed,
|
// Number of (nano,milli)seconds of an idle timeout. Once it passed,
|
||||||
// the library calls an idle callback function. It is also used for an
|
// the library calls an idle callback function. It is also used for an
|
||||||
// interval time of RunLoop() method
|
// interval time of RunLoop() method
|
||||||
@ -122,6 +134,7 @@ func NewPinger() *Pinger {
|
|||||||
addrs: make(map[string]*net.IPAddr),
|
addrs: make(map[string]*net.IPAddr),
|
||||||
hasIPv4: false,
|
hasIPv4: false,
|
||||||
hasIPv6: false,
|
hasIPv6: false,
|
||||||
|
Size: 64,
|
||||||
MaxRTT: time.Second,
|
MaxRTT: time.Second,
|
||||||
OnRecv: nil,
|
OnRecv: nil,
|
||||||
OnIdle: nil,
|
OnIdle: nil,
|
||||||
@ -389,12 +402,14 @@ func (p *Pinger) sendICMP(conn, conn6 *net.IPConn) (map[string]*net.IPAddr, erro
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
t := timeToBytes(time.Now())
|
||||||
|
|
||||||
p.mu.Lock()
|
p.mu.Lock()
|
||||||
bytes, err := (&icmpMessage{
|
bytes, err := (&icmpMessage{
|
||||||
Type: typ, Code: 0,
|
Type: typ, Code: 0,
|
||||||
Body: &icmpEcho{
|
Body: &icmpEcho{
|
||||||
ID: p.id, Seq: p.seq,
|
ID: p.id, Seq: p.seq,
|
||||||
Data: timeToBytes(time.Now()),
|
Data: append(t, byteSliceOfSize(p.Size-len(t))...),
|
||||||
},
|
},
|
||||||
}).Marshal()
|
}).Marshal()
|
||||||
p.mu.Unlock()
|
p.mu.Unlock()
|
||||||
@ -500,7 +515,7 @@ func (p *Pinger) procRecv(recv *packet, queue map[string]*net.IPAddr) {
|
|||||||
case *icmpEcho:
|
case *icmpEcho:
|
||||||
p.mu.Lock()
|
p.mu.Lock()
|
||||||
if pkt.ID == p.id && pkt.Seq == p.seq {
|
if pkt.ID == p.id && pkt.Seq == p.seq {
|
||||||
rtt = time.Since(bytesToTime(pkt.Data))
|
rtt = time.Since(bytesToTime(pkt.Data[:8]))
|
||||||
}
|
}
|
||||||
p.mu.Unlock()
|
p.mu.Unlock()
|
||||||
default:
|
default:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user