Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass the data with the message in case there is a payload #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions fastping.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// os.Exit(1)
// }
// p.AddIPAddr(ra)
// p.OnRecv = func(addr *net.IPAddr, rtt time.Duration) {
// p.OnRecv = func(addr *net.IPAddr, rtt time.Duration, data []byte) {
// fmt.Printf("IP Addr: %s receive, RTT: %v\n", addr.String(), rtt)
// }
// p.OnIdle = func() {
Expand Down Expand Up @@ -145,7 +145,7 @@ type Pinger struct {
MaxRTT time.Duration
// OnRecv is called with a response packet's source address and its
// elapsed time when Pinger receives a response packet.
OnRecv func(*net.IPAddr, time.Duration)
OnRecv func(*net.IPAddr, time.Duration, []byte)
// OnIdle is called when MaxRTT time passed
OnIdle func()
// If Debug is true, it prints debug messages to stdout.
Expand Down Expand Up @@ -297,7 +297,7 @@ func (p *Pinger) RemoveIPAddr(ip *net.IPAddr) {
func (p *Pinger) AddHandler(event string, handler interface{}) error {
switch event {
case "receive":
if hdl, ok := handler.(func(*net.IPAddr, time.Duration)); ok {
if hdl, ok := handler.(func(*net.IPAddr, time.Duration, []byte)); ok {
p.mu.Lock()
p.OnRecv = hdl
p.mu.Unlock()
Expand Down Expand Up @@ -646,11 +646,13 @@ func (p *Pinger) procRecv(recv *packet, queue map[string]*net.IPAddr) {
}

var rtt time.Duration
var payload []byte
switch pkt := m.Body.(type) {
case *icmp.Echo:
p.mu.Lock()
if pkt.ID == p.id && pkt.Seq == p.seq {
rtt = time.Since(bytesToTime(pkt.Data[:TimeSliceLength]))
payload = pkt.Data
}
p.mu.Unlock()
default:
Expand All @@ -663,7 +665,7 @@ func (p *Pinger) procRecv(recv *packet, queue map[string]*net.IPAddr) {
handler := p.OnRecv
p.mu.Unlock()
if handler != nil {
handler(ipaddr, rtt)
handler(ipaddr, rtt, payload)
}
}
}
Expand Down