Repository navigation
Expand file tree
/
Copy pathpingClient.go
More file actions
897 lines (776 loc) · 20.9 KB
/
Copy pathpingClient.go
File metadata and controls
897 lines (776 loc) · 20.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
// Package pingclient is a simple but powerful ICMP echo (ping) library.
// Inspired by go-fastping and go-ping
//
// Here is a very simple example that sends and receives three packets:
//
// import ping "github.com/scientiacoder/PingClient"
//
// pingClients, err := ping.InitWithYAMLFile("config.yaml")
// if err != nil {
// log.Fatalf("%s", err)
// return
// }
// for _, pingClient := range pingClients {
// pingClient.OnRecv = func(pkt *ping.Packet) {
// fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v ttl=%v\n",
// pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt, pkt.Ttl)
// }
// }
// for _, pingClient := range pingClients {
// err := pingClient.Run()
// if err != nil {
// log.Fatalf("%s", err)
// return
// }
// }
//
// Here is an example that pings github.com and 8.8.8.8 command:
//
// pingClient := ping.New()
// pingClient.Add("github.com")
// pingClient.Add("8.8.8.8")
//
// // Listen for Ctrl-C.
// c := make(chan os.Signal, 1)
// signal.Notify(c, os.Interrupt)
// go func() {
// for _ = range c {
// pingClient.Stop()
// }
// }()
// pingClient.OnRecv = func(pkt *ping.Packet) {
// fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v ttl=%v\n",
// pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt, pkt.Ttl)
// }
// pingClient.OnFinish = func(stats []*ping.Statistics) {
// for _, stat := range stats {
// fmt.Printf("\n--- %s %s ping statistics ---\n", stat.URL, stat.IP)
// for _, pkt := range stat.PacketsInfo {
// fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v ttl=%v\n",
// pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt, pkt.Ttl)
// }
// fmt.Printf("%d packets transmitted, %d packets received, %v%% packet loss\n",
// stat.PacketsSent, stat.PacketsRecv, stat.PacketLoss)
// fmt.Printf("round-trip min/avg/max/stddev = %v/%v/%v/%v\n",
// stat.MinRtt, stat.AvgRtt, stat.MaxRtt, stat.StdDevRtt)
// }
// }
// err := pingClient.Run()
// if err != nil {
// log.Fatalf("%s", err)
// return
// }
//
// It sends ICMP Echo Request packet(s) and waits for an Echo Reply in response.
// If it receives a response, it calls the OnRecv callback. When it's finished,
// it calls the OnFinish callback.
//
// For a full ping example, see "cmd/ping.go".
//
package pingclient
import (
"bytes"
"encoding/binary"
"fmt"
"math"
"math/rand"
"net"
"runtime"
"sync"
"syscall"
"time"
"golang.org/x/net/icmp"
"golang.org/x/net/ipv4"
"golang.org/x/net/ipv6"
)
const (
timeSliceLength = 8
trackerLength = 8
protocolICMP = 1
protocolIPv6ICMP = 58
)
var (
ipv4Proto = map[string]string{"icmp": "ip4:icmp", "udp": "udp4"}
ipv6Proto = map[string]string{"icmp": "ip6:ipv6-icmp", "udp": "udp6"}
)
// New returns a new PingClient struct pointer.
func New() *PingClient {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return &PingClient{
Count: 0,
Num: 5,
Interval: time.Second,
RecordRtts: true,
Continuous: false,
Size: timeSliceLength + trackerLength,
Timeout: 5 * time.Second,
Tracker: r.Int63n(math.MaxInt64),
PacketsSent: make(map[string]int),
PacketsRecv: make(map[string]int),
PacketsInfo: make(map[string][]*Packet),
rtts: make(map[string][]time.Duration),
IPs: make([]*net.IPAddr, 0),
URLs: make([]string, 0),
IPToURL: make(map[string]string),
done: make(chan bool),
id: rand.Intn(0xffff),
sequence: rand.Intn(0xffff),
network: "ip",
protocol: "udp",
}
}
// NewPingClient returns a new PingClient struct pointer and resolves string addr
func NewPingClient(addr string) (*PingClient, error) {
p := New()
return p, p.Add(addr)
}
// NewPrivilegedPingClient returns a new raw socket PingClient struct pointer and resolves string addr
func NewPrivilegedPingClient(addr string) (*PingClient, error) {
p := New()
p.SetPrivileged(true)
return p, p.Add(addr)
}
// InitWithConfig inits list of ping clients configured by the yaml file
func InitWithConfig(conf *Config) []*PingClient {
pingClients := make([]*PingClient, 0)
for _, v := range conf.PingClientsConf {
pingClient := NewPingClientWithConfig(v)
pingClients = append(pingClients, pingClient)
}
return pingClients
}
// NewPingClientWithConfig uses struct PingClientConfig to create a new PingClient
func NewPingClientWithConfig(conf *PingClientConfig) *PingClient {
pingClient := New()
pingClient.Interval = conf.Interval
pingClient.Timeout = conf.Timeout
pingClient.IPs = conf.IPs
pingClient.URLs = conf.URLs
pingClient.Num = conf.Num
pingClient.IPToURL = conf.IPToURL
pingClient.Continuous = conf.Continuous
pingClient.SetPrivileged(conf.Privileged)
return pingClient
}
// Use simply resolves xx declared but not used issue
// for debug
func Use(args ...interface{}) {
for _, val := range args {
_ = val
}
}
// PingClient represents a packet sender/receiver.
type PingClient struct {
// Interval is the wait time between each packet send. Default is 1s.
Interval time.Duration
// Timeout specifies a timeout before ping exits, regardless of how many
// packets have been received.
Timeout time.Duration
// Count tells PingClient to stop after sending (and receiving) Count echo
// packets. If this option is not specified, PingClient will operate until
// interrupted.
Count int
// number of packets send per ip(or url) address
Num int
// Debug runs in debug mode
Debug bool
// Number of packets sent
PacketsSent map[string]int
// Number of packets received
PacketsRecv map[string]int
// Received packets info for Statistics use
PacketsInfo map[string][]*Packet
// Round trip time duration of all the packets
rtts map[string][]time.Duration
// If true, keep a record of rtts of all received packets.
// Set to false to avoid memory bloat for long running pings.
RecordRtts bool
// OnRecv is called when PingClient receives and processes a packet
OnRecv func(*Packet)
// OnFinish is called when PingClient exits
OnFinish func([]*Statistics)
// Size of packet being sent
Size int
// Tracker: Used to uniquely identify packet when non-priviledged
Tracker int64
// Source is the source IP address
Source string
// stop chan bool
done chan bool
// list of destination ping IPs
IPs []*net.IPAddr
// list of destination ping URLs
URLs []string
IPToURL map[string]string
// whether run Continuously(forever)
Continuous bool
// has Ipv4 in IPs
hasIPv4 bool
// has Ipv6 in IPs
hasIPv6 bool
id int
sequence int
// network is one of "ip", "ip4", or "ip6".
network string
// protocol is "icmp" or "udp".
protocol string
}
type packet struct {
bytes []byte
nbytes int
src net.Addr
ttl int
}
// Packet represents a received and processed ICMP echo packet.
type Packet struct {
// Rtt is the round-trip time it took to ping.
Rtt time.Duration
// IPAddr is the address of the host being pinged.
IPAddr *net.IPAddr
// IP address in string format e.g "142.250.71.78"
IP string
// NBytes is the number of bytes in the message.
Nbytes int
// Seq is the ICMP sequence number.
Seq int
// TTL is the Time To Live on the packet.
Ttl int
}
// StatisticsList is a wrapper for list of Statistics
type StatisticsList struct {
// list of Statistics
stats []*Statistics
// mutex lock for console printing use
mu sync.Mutex
}
// Statistics represent the stats of a currently running or finished
// PingClient operation.
type Statistics struct {
// PacketsRecv is the number of packets received.
PacketsRecv int
// PacketsSent is the number of packets sent.
PacketsSent int
// PacketLoss is the percentage of packets lost.
PacketLoss float64
// Received packets info for Statistics use
PacketsInfo []*Packet
// URL is the URL address of the host being pinged.
URL string
// IP address in string format e.g "142.250.71.78"
IP string
// Rtts is all of the round-trip times sent via this PingClient.
Rtts []time.Duration
// MinRtt is the minimum round-trip time sent via this PingClient.
MinRtt time.Duration
// MaxRtt is the maximum round-trip time sent via this PingClient.
MaxRtt time.Duration
// AvgRtt is the average round-trip time sent via this PingClient.
AvgRtt time.Duration
// StdDevRtt is the standard deviation of the round-trip times sent via
// this PingClient.
StdDevRtt time.Duration
}
// SetNetwork allows configuration of DNS resolution.
// * "ip" will automatically select IPv4 or IPv6.
// * "ip4" will select IPv4.
// * "ip6" will select IPv6.
func (p *PingClient) SetNetwork(n string) {
switch n {
case "ip4":
p.network = "ip4"
case "ip6":
p.network = "ip6"
default:
p.network = "ip"
}
}
// SetRecordRtts sets whether record Statistics
func (p *PingClient) SetRecordRtts(r bool) {
p.RecordRtts = r
}
// SetPrivileged sets the type of ping PingClient will send.
// false means PingClient will send an "unprivileged" UDP ping.
// true means PingClient will send a "privileged" raw ICMP ping.
// NOTE: setting to true requires that it be run with super-user privileges.
func (p *PingClient) SetPrivileged(privileged bool) {
if privileged {
p.protocol = "icmp"
} else {
p.protocol = "udp"
}
}
// Privileged returns whether PingClient is running in privileged mode.
func (p *PingClient) Privileged() bool {
return p.protocol == "icmp"
}
// Run runs the PingClient. This is a blocking function that will exit when it's
// done.
func (p *PingClient) Run() error {
var conn, conn6 *icmp.PacketConn
var err error
p.ipVersionCheck()
p.initPacketsConfig()
if p.hasIPv4 {
if conn, err = p.listen(ipv4Proto[p.protocol]); err != nil {
return err
}
if err = conn.IPv4PacketConn().SetControlMessage(ipv4.FlagTTL, true); runtime.GOOS != "windows" && err != nil {
return err
}
defer conn.Close()
}
if p.hasIPv6 {
if conn6, err = p.listen(ipv6Proto[p.protocol]); err != nil {
return err
}
if err = conn.IPv6PacketConn().SetControlMessage(ipv6.FlagHopLimit, true); runtime.GOOS != "windows" && err != nil {
return err
}
defer conn6.Close()
}
defer p.finish()
var wg sync.WaitGroup
recv := make(chan *packet, 5*len(p.IPs))
defer close(recv)
if conn != nil {
wg.Add(1)
//nolint:errcheck
go p.recvICMP(conn, recv, &wg)
}
if conn6 != nil {
wg.Add(1)
//nolint:errcheck
go p.recvICMP(conn6, recv, &wg)
}
err = p.sendICMP(conn, conn6)
if err != nil {
return err
}
timeout := time.NewTicker(p.Timeout)
if p.Continuous {
timeout.Stop()
} else {
defer timeout.Stop()
}
interval := time.NewTicker(p.Interval)
defer interval.Stop()
for {
select {
case <-p.done:
wg.Wait()
return nil
case <-interval.C:
if !p.Continuous && p.Num > 0 && All(p.PacketsSent, packetsSentFinished, p.Num) {
close(p.done)
wg.Wait()
return nil
}
err = p.sendICMP(conn, conn6)
if err != nil {
// FIXME: this logs as FATAL but continues
fmt.Println("FATAL: ", err.Error())
}
case <-timeout.C:
if All(p.PacketsSent, packetsSentFinished, p.Num) {
close(p.done)
wg.Wait()
return nil
}
case r := <-recv:
err := p.processPacket(r)
if err != nil {
// FIXME: this logs as FATAL but continues
fmt.Println("FATAL: ", err.Error())
}
}
}
}
// Stop the ping client
func (p *PingClient) Stop() {
close(p.done)
}
func (p *PingClient) finish() {
handler := p.OnFinish
if handler != nil {
s := p.Statistics()
handler(s)
}
}
func (p *PingClient) recvICMP(
conn *icmp.PacketConn,
recv chan<- *packet,
wg *sync.WaitGroup,
) error {
defer wg.Done()
for {
select {
case <-p.done:
return nil
default:
bytes := make([]byte, 512)
if err := conn.SetReadDeadline(time.Now().Add(time.Millisecond * 500)); err != nil {
return err
}
var n, ttl int
var src net.Addr
var err error
if conn.IPv4PacketConn() != nil {
var cm *ipv4.ControlMessage
n, cm, src, err = conn.IPv4PacketConn().ReadFrom(bytes)
if cm != nil {
ttl = cm.TTL
}
} else if conn.IPv6PacketConn() != nil {
var cm *ipv6.ControlMessage
n, cm, src, err = conn.IPv6PacketConn().ReadFrom(bytes)
if cm != nil {
ttl = cm.HopLimit
}
} else {
return nil
}
if err != nil {
if neterr, ok := err.(*net.OpError); ok {
if neterr.Timeout() {
// Read timeout
continue
} else {
close(p.done)
return err
}
}
}
select {
case <-p.done:
return nil
case recv <- &packet{bytes: bytes, nbytes: n, src: src, ttl: ttl}:
}
}
}
}
func (p *PingClient) processPacket(recv *packet) error {
receivedAt := time.Now()
var ipStr string
var err error
if ipStr, err = resolveIPFromAddr(recv.src); err != nil {
return err
}
var proto int
if isIPv4(parseIP(ipStr)) {
proto = protocolICMP
} else if isIPv6(parseIP(ipStr)) {
proto = protocolIPv6ICMP
} else {
return fmt.Errorf("error processPacket() checking icmp packet IP address: %s", ipStr)
}
var m *icmp.Message
if m, err = icmp.ParseMessage(proto, recv.bytes); err != nil {
return fmt.Errorf("error parsing icmp message: %s", err.Error())
}
if m.Type != ipv4.ICMPTypeEchoReply && m.Type != ipv6.ICMPTypeEchoReply {
// Not an echo reply, ignore it
return nil
}
outPkt := &Packet{
Nbytes: recv.nbytes,
IPAddr: p.findIPAddrbyString(ipStr),
IP: ipStr,
Ttl: recv.ttl,
}
switch pkt := m.Body.(type) {
case *icmp.Echo:
// If we are priviledged, we can match icmp.ID
if p.protocol == "icmp" {
// Check if reply from same ID
if pkt.ID != p.id {
return nil
}
}
if len(pkt.Data) < timeSliceLength+trackerLength {
return fmt.Errorf("insufficient data received; got: %d %v",
len(pkt.Data), pkt.Data)
}
tracker := bytesToInt(pkt.Data[timeSliceLength:])
timestamp := bytesToTime(pkt.Data[:timeSliceLength])
if tracker != p.Tracker {
return nil
}
outPkt.Rtt = receivedAt.Sub(timestamp)
outPkt.Seq = pkt.Seq
p.PacketsRecv[ipStr]++
default:
// Very bad, not sure how this can happen
return fmt.Errorf("invalid ICMP echo reply; type: '%T', '%v'", pkt, pkt)
}
if p.RecordRtts && !p.Continuous {
p.PacketsInfo[ipStr] = append(p.PacketsInfo[ipStr], outPkt)
p.rtts[ipStr] = append(p.rtts[ipStr], outPkt.Rtt)
}
handler := p.OnRecv
if handler != nil {
handler(outPkt)
}
return nil
}
func (p *PingClient) sendICMP(conn, conn6 *icmp.PacketConn) error {
wg := new(sync.WaitGroup)
for _, addr := range p.IPs {
if !p.Continuous && p.PacketsSent[addr.IP.String()] >= p.Num {
continue
}
var cn *icmp.PacketConn
var typ icmp.Type
if isIPv4(addr.IP) {
cn = conn
typ = ipv4.ICMPTypeEcho
} else if isIPv6(addr.IP) {
cn = conn6
typ = ipv6.ICMPTypeEchoRequest
} else {
continue
}
var dst net.Addr = addr
if p.protocol == "udp" {
dst = &net.UDPAddr{IP: addr.IP, Zone: addr.Zone}
}
t := append(timeToBytes(time.Now()), intToBytes(p.Tracker)...)
if remainSize := p.Size - timeSliceLength - trackerLength; remainSize > 0 {
t = append(t, bytes.Repeat([]byte{1}, remainSize)...)
}
body := &icmp.Echo{
ID: p.id,
Seq: p.sequence,
Data: t,
}
msg := &icmp.Message{
Type: typ,
Code: 0,
Body: body,
}
msgBytes, err := msg.Marshal(nil)
if err != nil {
return err
}
var ipStr string
if ipStr, err = resolveIPFromAddr(dst); err != nil {
return err
}
wg.Add(1)
go func(conn *icmp.PacketConn, dst net.Addr, ipStr string, b []byte) {
for {
if _, err := conn.WriteTo(b, dst); err != nil {
if neterr, ok := err.(*net.OpError); ok {
if neterr.Err == syscall.ENOBUFS {
continue
}
}
}
break
}
p.PacketsSent[ipStr]++
wg.Done()
}(cn, dst, ipStr, msgBytes)
}
wg.Wait()
p.sequence++
return nil
}
func (p *PingClient) listen(netProto string) (*icmp.PacketConn, error) {
conn, err := icmp.ListenPacket(netProto, p.Source)
if err != nil {
close(p.done)
return nil, err
}
return conn, nil
}
func (p *PingClient) initPacketsConfig() {
for _, addr := range p.IPs {
p.PacketsSent[addr.IP.String()] = 0
p.PacketsRecv[addr.IP.String()] = 0
p.PacketsInfo[addr.IP.String()] = make([]*Packet, 0)
p.rtts[addr.IP.String()] = make([]time.Duration, 0)
}
}
// All checks whether all the map entry satisfies the function f
func All(m map[string]int, f func(int, int) bool, num int) bool {
for _, val := range m {
if !f(val, num) {
return false
}
}
return true
}
func packetsSentFinished(sent int, num int) bool {
return sent >= num
}
/* * * * * * * * * * * * * * * * * * * * * * *
_____ _ _ _ _ _
/ ____| | | | (_) | | (_)
| (___ | |_ __ _| |_ _ ___| |_ _ ___ ___
\___ \| __/ _` | __| / __| __| |/ __/ __|
____) | || (_| | |_| \__ \ |_| | (__\__ \
|_____/ \__\__,_|\__|_|___/\__|_|\___|___/
* * * * * * * * * * * * * * * * * * * * * * */
// Statistics returns the statistics of the whole PingClient.
// This can be run while the PingClient is running or after it is finished.
// OnFinish calls this function to get it's finished statistics.
func (p *PingClient) Statistics() []*Statistics {
stats := make([]*Statistics, 0)
for _, ipAddr := range p.IPs {
s := p.StatisticsPerIP(ipAddr)
stats = append(stats, s)
}
return stats
}
// StatisticsPerIP returns the statistics of the Ping info to the given IP address.
func (p *PingClient) StatisticsPerIP(ipAddr *net.IPAddr) *Statistics {
var ipStr string = ipAddr.IP.String()
loss := float64(p.PacketsSent[ipStr]-p.PacketsRecv[ipStr]) / float64(p.PacketsSent[ipStr]) * 100
var min, max, total time.Duration
if len(p.rtts[ipStr]) > 0 {
min = p.rtts[ipStr][0]
max = p.rtts[ipStr][0]
}
for _, rt := range p.rtts[ipStr] {
if rt < min {
min = rt
}
if rt > max {
max = rt
}
total += rt
}
s := Statistics{
PacketsSent: p.PacketsSent[ipStr],
PacketsRecv: p.PacketsRecv[ipStr],
PacketsInfo: p.PacketsInfo[ipStr],
PacketLoss: loss,
Rtts: p.rtts[ipStr],
URL: p.IPToURL[ipStr],
IP: ipStr,
MaxRtt: max,
MinRtt: min,
}
if len(p.rtts[ipStr]) > 0 {
s.AvgRtt = total / time.Duration(len(p.rtts[ipStr]))
var sumsquares time.Duration
for _, rt := range p.rtts[ipStr] {
sumsquares += (rt - s.AvgRtt) * (rt - s.AvgRtt)
}
s.StdDevRtt = time.Duration(math.Sqrt(
float64(sumsquares / time.Duration(len(p.rtts[ipStr])))))
}
return &s
}
/* * * * * * * * * * * * * * * * * * * * * * *
_____ _____ _ _ _ _ _
|_ _| __ \ | | | | | (_) |
| | | |__) | | | | | |_ _| |___
| | | ___/ | | | | __| | / __|
_| |_| | | |__| | |_| | \__ \
|_____|_| \____/ \__|_|_|___/
* * * * * * * * * * * * * * * * * * * * * * */
// Add parses addr(ip format or url format) to net.IP and
// adds net.IP to pingClient
func (p *PingClient) Add(addr string) error {
if parseIP(addr) != nil {
return p.AddIPAddr(addr)
}
return p.AddURLAddr(addr)
}
// AddIPAddr adds IP address to ping client
func (p *PingClient) AddIPAddr(addr string) error {
ip := parseIP(addr)
if isIPv4(ip) {
p.IPs = append(p.IPs, &net.IPAddr{IP: ip})
} else if isIPv6(ip) {
p.IPs = append(p.IPs, &net.IPAddr{IP: ip})
} else {
return fmt.Errorf("error AddIPAddr() addr %s should be a valid IP address", addr)
}
return nil
}
// AddURLAddr resolves URL addr to IP address and adds IP address to ping client
func (p *PingClient) AddURLAddr(addr string) error {
ipAddr, err := parseURL(p.network, addr)
if err != nil {
return err
}
p.IPs = append(p.IPs, ipAddr)
p.URLs = append(p.URLs, addr)
p.IPToURL[ipAddr.IP.String()] = addr
return nil
}
func (p *PingClient) ipVersionCheck() {
for _, ipAddr := range p.IPs {
if isIPv4(ipAddr.IP) {
p.hasIPv4 = true
} else if isIPv6(ipAddr.IP) {
p.hasIPv6 = true
}
}
}
func (p *PingClient) findIPAddrbyString(s string) *net.IPAddr {
for _, ipAddr := range p.IPs {
if ipAddr.IP.String() == s {
return ipAddr
}
}
return nil
}
func resolveIPFromAddr(addr net.Addr) (string, error) {
var ipStr string
switch addr := addr.(type) {
case *net.IPAddr:
ipStr = addr.IP.String()
case *net.UDPAddr:
ipStr = addr.IP.String()
default:
return "", fmt.Errorf("error processPacket() parsing icmp packet IP address: %s", addr)
}
return ipStr, nil
}
// parse an stirng s to net.IP
// returns nil if s is not a valid ip(v4 or v6) address
func parseIP(s string) net.IP {
return net.ParseIP(s)
}
// get IP addr of given string address using DNS lookup
// e.g. getIP("github.com")
// return []IP(4-byte Ipv4 and 16-byte Ipv6, Ipv4 could also be 16-byte)
func parseURL(network string, url string) (*net.IPAddr, error) {
IPs, err := net.ResolveIPAddr(network, url)
if err != nil {
return nil, err
}
return IPs, nil
}
func isIPv4(ip net.IP) bool {
return len(ip.To4()) == net.IPv4len
}
func isIPv6(ip net.IP) bool {
return len(ip.To16()) == net.IPv6len
}
func bytesToTime(b []byte) time.Time {
var nsec int64
for i := uint8(0); i < 8; i++ {
nsec += int64(b[i]) << ((7 - i) * 8)
}
return time.Unix(nsec/1000000000, nsec%1000000000)
}
func timeToBytes(t time.Time) []byte {
nsec := t.UnixNano()
b := make([]byte, 8)
for i := uint8(0); i < 8; i++ {
b[i] = byte((nsec >> ((7 - i) * 8)) & 0xff)
}
return b
}
func bytesToInt(b []byte) int64 {
return int64(binary.BigEndian.Uint64(b))
}
func intToBytes(tracker int64) []byte {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, uint64(tracker))
return b
}