-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
tcping.go
913 lines (764 loc) · 24.3 KB
/
tcping.go
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
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
package main
import (
"bufio"
"context"
"flag"
"math/rand"
"net"
"net/netip"
"os"
"os/signal"
"regexp"
"strconv"
"strings"
"syscall"
"time"
"github.com/google/go-github/v45/github"
)
var version = "" // set at compile time
const (
owner = "pouriyajamshidi"
repo = "tcping"
dnsTimeout = 2 * time.Second
)
// printer is a set of methods for printers to implement.
//
// Printers should NOT modify any existing data nor do any calculations.
// They should only perform visual operations on given data.
type printer interface {
// printStart should print the first message, after the program starts.
// This message is printed only once, at the very beginning.
printStart(hostname string, port uint16)
// printProbeSuccess should print a message after each successful probe.
// hostname could be empty, meaning it's pinging an address.
// streak is the number of successful consecutive probes.
printProbeSuccess(localAddr string, userInput userInput, streak uint, rtt float32)
// printProbeFail should print a message after each failed probe.
// hostname could be empty, meaning it's pinging an address.
// streak is the number of successful consecutive probes.
printProbeFail(userInput userInput, streak uint)
// printRetryingToResolve should print a message with the hostname
// it is trying to resolve an ip for.
//
// This is only being printed when the -r flag is applied.
printRetryingToResolve(hostname string)
// printTotalDownTime should print a downtime duration.
//
// This is being called when host was unavailable for some time
// but the latest probe was successful (became available).
printTotalDownTime(downtime time.Duration)
// printStatistics should print a message with
// helpful statistics information.
//
// This is being called on exit and when user hits "Enter".
printStatistics(s tcping)
// printVersion should print the current version.
printVersion()
// printInfo should a message, which is not directly related
// to the pinging and serves as a helpful information.
//
// Example of such: new version with -u flag.
printInfo(format string, args ...any)
// printError should print an error message.
// Printer should also apply \n to the given string, if needed.
printError(format string, args ...any)
}
type tcping struct {
printer // printer holds the chosen printer implementation for outputting information and data.
startTime time.Time
endTime time.Time
startOfUptime time.Time
startOfDowntime time.Time
lastSuccessfulProbe time.Time
lastUnsuccessfulProbe time.Time
ticker *time.Ticker // ticker is used to handle time between probes.
longestUptime longestTime
longestDowntime longestTime
rtt []float32
hostnameChanges []hostnameChange
userInput userInput
ongoingSuccessfulProbes uint
ongoingUnsuccessfulProbes uint
totalDowntime time.Duration
totalUptime time.Duration
totalSuccessfulProbes uint
totalUnsuccessfulProbes uint
retriedHostnameLookups uint
rttResults rttResult
destWasDown bool // destWasDown is used to determine the duration of a downtime
destIsIP bool // destIsIP suppresses printing the IP information twice when hostname is not provided
}
type userInput struct {
ip netip.Addr
hostname string
networkInterface networkInterface
retryHostnameLookupAfter uint // Retry resolving target's hostname after a certain number of failed requests
probesBeforeQuit uint
timeout time.Duration
intervalBetweenProbes time.Duration
port uint16
useIPv4 bool
useIPv6 bool
shouldRetryResolve bool
showFailuresOnly bool
showLocalAddress bool
}
type genericUserInputArgs struct {
retryResolve *uint
probesBeforeQuit *uint
timeout *float64
secondsBetweenProbes *float64
intName *string
showFailuresOnly *bool
showLocalAddress *bool
args []string
}
type networkInterface struct {
remoteAddr *net.TCPAddr
dialer net.Dialer
use bool
}
type longestTime struct {
start time.Time
end time.Time
duration time.Duration
}
type rttResult struct {
min float32
max float32
average float32
hasResults bool
}
type hostnameChange struct {
Addr netip.Addr `json:"addr,omitempty"`
When time.Time `json:"when,omitempty"`
}
// signalHandler catches SIGINT and SIGTERM then prints tcping stats
func signalHandler(tcping *tcping) {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigChan
shutdown(tcping)
}()
}
// monitorSTDIN checks stdin to see whether the 'Enter' key was pressed
func monitorSTDIN(stdinChan chan bool) {
reader := bufio.NewReader(os.Stdin)
for {
input, _ := reader.ReadString('\n')
if input == "\n" || input == "\r" || input == "\r\n" {
stdinChan <- true
}
}
}
// printStats is a helper method for printStatistics
// for the current printer.
//
// This should be used instead, as it makes
// all the necessary calculations beforehand.
func (t *tcping) printStats() {
if t.destWasDown {
calcLongestDowntime(t, time.Since(t.startOfDowntime))
} else {
calcLongestUptime(t, time.Since(t.startOfUptime))
}
t.rttResults = calcMinAvgMaxRttTime(t.rtt)
t.printStatistics(*t)
}
// shutdown calculates endTime, prints statistics and calls os.Exit(0).
// This should be used as the main exit-point.
func shutdown(tcping *tcping) {
tcping.endTime = time.Now()
tcping.printStats()
// if the printer type is `database`, close the it before exiting
if db, ok := tcping.printer.(*database); ok {
db.conn.Close()
}
os.Exit(0)
}
// usage prints how tcping should be run
func usage() {
executableName := os.Args[0]
colorLightCyan("\nTCPING version %s\n\n", version)
colorRed("Try running %s like:\n", executableName)
colorRed("%s <hostname/ip> <port number>. For example:\n", executableName)
colorRed("%s www.example.com 443\n", executableName)
colorYellow("\n[optional flags]\n")
flag.VisitAll(func(f *flag.Flag) {
flagName := f.Name
if len(f.Name) > 1 {
flagName = "-" + flagName
}
colorYellow(" -%s : %s\n", flagName, f.Usage)
})
os.Exit(1)
}
// setPrinter selects the printer
func setPrinter(tcping *tcping, outputJSON, prettyJSON *bool, noColor *bool, timeStamp *bool, outputDb *string, args []string) {
if *prettyJSON && !*outputJSON {
colorRed("--pretty has no effect without the -j flag.")
usage()
}
if *outputJSON {
tcping.printer = newJSONPrinter(*prettyJSON)
} else if *outputDb != "" {
tcping.printer = newDB(*outputDb, args)
} else if *noColor == true {
tcping.printer = newPlanePrinter(timeStamp)
} else {
tcping.printer = newColorPrinter(timeStamp)
}
}
// showVersion displays the version and exits
func showVersion(tcping *tcping) {
tcping.printVersion()
os.Exit(0)
}
// setIPFlags ensures that either IPv4 or IPv6 is specified by the user and not both and sets it
func setIPFlags(tcping *tcping, ip4, ip6 *bool) {
if *ip4 && *ip6 {
tcping.printError("Only one IP version can be specified")
usage()
}
if *ip4 {
tcping.userInput.useIPv4 = true
}
if *ip6 {
tcping.userInput.useIPv6 = true
}
}
// setPort validates and sets the TCP/UDP port range
func setPort(tcping *tcping, args []string) {
port, err := strconv.ParseUint(args[1], 10, 16)
if err != nil {
tcping.printError("Invalid port number: %s", args[1])
os.Exit(1)
}
if port < 1 || port > 65535 {
tcping.printError("Port should be in 1..65535 range")
os.Exit(1)
}
tcping.userInput.port = uint16(port)
}
// setGenericArgs assigns the generic flags after sanity checks
func setGenericArgs(tcping *tcping, genericArgs genericUserInputArgs) {
if *genericArgs.retryResolve > 0 {
tcping.userInput.retryHostnameLookupAfter = *genericArgs.retryResolve
}
tcping.userInput.hostname = genericArgs.args[0]
tcping.userInput.ip = resolveHostname(tcping)
tcping.startTime = time.Now()
tcping.userInput.probesBeforeQuit = *genericArgs.probesBeforeQuit
tcping.userInput.timeout = secondsToDuration(*genericArgs.timeout)
tcping.userInput.intervalBetweenProbes = secondsToDuration(*genericArgs.secondsBetweenProbes)
if tcping.userInput.intervalBetweenProbes < 2*time.Millisecond {
tcping.printError("Wait interval should be more than 2 ms")
os.Exit(1)
}
// this serves as a default starting value for tracking IP changes.
tcping.hostnameChanges = []hostnameChange{
{tcping.userInput.ip, time.Now()},
}
if tcping.userInput.hostname == tcping.userInput.ip.String() {
tcping.destIsIP = true
}
if tcping.userInput.retryHostnameLookupAfter > 0 && !tcping.destIsIP {
tcping.userInput.shouldRetryResolve = true
}
if *genericArgs.intName != "" {
tcping.userInput.networkInterface = newNetworkInterface(tcping, *genericArgs.intName)
}
tcping.userInput.showFailuresOnly = *genericArgs.showFailuresOnly
tcping.userInput.showLocalAddress = *genericArgs.showLocalAddress
}
// processUserInput gets and validate user input
func processUserInput(tcping *tcping) {
useIPv4 := flag.Bool("4", false, "only use IPv4.")
useIPv6 := flag.Bool("6", false, "only use IPv6.")
retryHostnameResolveAfter := flag.Uint("r", 0, "retry resolving target's hostname after <n> number of failed probes. e.g. -r 10 to retry after 10 failed probes.")
probesBeforeQuit := flag.Uint("c", 0, "stop after <n> probes, regardless of the result. By default, no limit will be applied.")
outputJSON := flag.Bool("j", false, "output in JSON format.")
prettyJSON := flag.Bool("pretty", false, "use indentation when using json output format. No effect without the '-j' flag.")
noColor := flag.Bool("no-color", false, "do not colorize output.")
showTimestamp := flag.Bool("D", false, "show timestamp in output.")
showVer := flag.Bool("v", false, "show version.")
checkUpdates := flag.Bool("u", false, "check for updates and exit.")
secondsBetweenProbes := flag.Float64("i", 1, "interval between sending probes. Real number allowed with dot as a decimal separator. The default is one second")
timeout := flag.Float64("t", 1, "time to wait for a response, in seconds. Real number allowed. 0 means infinite timeout.")
outputDB := flag.String("db", "", "path and file name to store tcping output to sqlite database.")
interfaceName := flag.String("I", "", "interface name or address.")
showLocalAddress := flag.Bool("show-local-address", false, "Show source address and port used for probe.")
showFailuresOnly := flag.Bool("show-failures-only", false, "Show only the failed probes.")
showHelp := flag.Bool("h", false, "show help message.")
flag.CommandLine.Usage = usage
permuteArgs(os.Args[1:])
flag.Parse()
// validation for flag and args
args := flag.Args()
// we need to set printers first, because they're used for
// error reporting and other output.
setPrinter(tcping, outputJSON, prettyJSON, noColor, showTimestamp, outputDB, args)
// Handle -v flag
if *showVer {
showVersion(tcping)
}
// Handle -h flag
if *showHelp {
usage()
}
// Handle -u flag
if *checkUpdates {
checkForUpdates(tcping)
}
// host and port must be specified
if len(args) != 2 {
usage()
}
// Check whether both the ipv4 and ipv6 flags are attempted set if ony one, error otherwise.
setIPFlags(tcping, useIPv4, useIPv6)
// Check if the port is valid and set it.
setPort(tcping, args)
// set generic args
genericArgs := genericUserInputArgs{
retryResolve: retryHostnameResolveAfter,
probesBeforeQuit: probesBeforeQuit,
timeout: timeout,
secondsBetweenProbes: secondsBetweenProbes,
intName: interfaceName,
showFailuresOnly: showFailuresOnly,
showLocalAddress: showLocalAddress,
args: args,
}
setGenericArgs(tcping, genericArgs)
}
/*
permuteArgs permute args for flag parsing stops just before the first non-flag argument.
see: https://pkg.go.dev/flag
*/
func permuteArgs(args []string) {
var flagArgs []string
var nonFlagArgs []string
for i := 0; i < len(args); i++ {
v := args[i]
if v[0] == '-' {
var optionName string
if v[1] == '-' {
optionName = v[2:]
} else {
optionName = v[1:]
}
switch optionName {
case "c":
fallthrough
case "t":
fallthrough
case "db":
fallthrough
case "I":
fallthrough
case "i":
fallthrough
case "r":
/* out of index */
if len(args) <= i+1 {
usage()
}
/* the next flag has come */
optionVal := args[i+1]
if optionVal[0] == '-' {
usage()
}
flagArgs = append(flagArgs, args[i:i+2]...)
i++
default:
flagArgs = append(flagArgs, args[i])
}
} else {
nonFlagArgs = append(nonFlagArgs, args[i])
}
}
permutedArgs := append(flagArgs, nonFlagArgs...)
/* replace args */
for i := 0; i < len(args); i++ {
args[i] = permutedArgs[i]
}
}
// newNetworkInterface uses the 1st ip address of the interface
// if any err occurs it calls `tcpStats.printError` and exits with status code 1.
// or return `networkInterface`
func newNetworkInterface(tcping *tcping, netInterface string) networkInterface {
var interfaceAddress net.IP
interfaceAddress = net.ParseIP(netInterface)
if interfaceAddress == nil {
ief, err := net.InterfaceByName(netInterface)
if err != nil {
tcping.printError("Interface %s not found", netInterface)
os.Exit(1)
}
addrs, err := ief.Addrs()
if err != nil {
tcping.printError("Unable to get Interface addresses")
os.Exit(1)
}
// Iterating through the available addresses to identify valid IP configurations
for _, addr := range addrs {
if ip := addr.(*net.IPNet).IP; ip != nil {
// netip.Addr
nipAddr, err := netip.ParseAddr(ip.String())
if err != nil {
continue
}
if nipAddr.Is4() && !tcping.userInput.useIPv6 {
interfaceAddress = ip
break
} else if nipAddr.Is6() && !tcping.userInput.useIPv4 {
if nipAddr.IsLinkLocalUnicast() {
continue
}
interfaceAddress = ip
break
}
}
}
if interfaceAddress == nil {
tcping.printError("Unable to get Interface's IP Address")
os.Exit(1)
}
}
// Initializing a networkInterface struct and setting the 'use' field to true
ni := networkInterface{
use: true,
}
ni.remoteAddr = &net.TCPAddr{
IP: net.ParseIP(tcping.userInput.ip.String()),
Port: int(tcping.userInput.port),
}
localAddr := &net.TCPAddr{
IP: interfaceAddress,
}
ni.dialer = net.Dialer{
LocalAddr: localAddr,
Timeout: tcping.userInput.timeout, // Set the timeout duration
}
return ni
}
// compareVersions is used to compare tcping versions
func compareVersions(v1, v2 string) int {
parts1 := strings.Split(v1, ".")
parts2 := strings.Split(v2, ".")
for i := 0; i < len(parts1) && i < len(parts2); i++ {
n1, _ := strconv.Atoi(parts1[i])
n2, _ := strconv.Atoi(parts2[i])
if n1 < n2 {
return -1
}
if n1 > n2 {
return 1
}
}
// for cases in which version numbers differ in length
if len(parts1) < len(parts2) {
return -1
}
if len(parts1) > len(parts2) {
return 1
}
return 0
}
// checkForUpdates checks for newer versions of tcping
func checkForUpdates(tcping *tcping) {
c := github.NewClient(nil)
/* unauthenticated requests from the same IP are limited to 60 per hour. */
latestRelease, _, err := c.Repositories.GetLatestRelease(context.Background(), owner, repo)
if err != nil {
tcping.printError("Failed to check for updates %s", err.Error())
os.Exit(1)
}
reg := `^v?(\d+\.\d+\.\d+)$`
latestTagName := latestRelease.GetTagName()
latestVersion := regexp.MustCompile(reg).FindStringSubmatch(latestTagName)
if len(latestVersion) == 0 {
tcping.printError("Failed to check for updates. The version name does not match the rule: %s", latestTagName)
os.Exit(1)
}
comparison := compareVersions(version, latestVersion[1])
if comparison < 0 {
tcping.printInfo("Found newer version %s", latestVersion[1])
tcping.printInfo("Please update TCPING from the URL below:")
tcping.printInfo("https://github.com/%s/%s/releases/tag/%s",
owner, repo, latestTagName)
} else if comparison > 0 {
tcping.printInfo("Current version %s is newer than the latest release %s",
version, latestVersion[1])
} else {
tcping.printInfo("You have the latest version: %s", version)
}
os.Exit(0)
}
// selectResolvedIP returns a single IPv4 or IPv6 address from the net.IP slice of resolved addresses
func selectResolvedIP(tcping *tcping, ipAddrs []netip.Addr) netip.Addr {
var index int
var ipList []netip.Addr
var ip netip.Addr
switch {
case tcping.userInput.useIPv4:
for _, ip := range ipAddrs {
if ip.Is4() {
ipList = append(ipList, ip)
}
}
if len(ipList) == 0 {
tcping.printError("Failed to find IPv4 address for %s", tcping.userInput.hostname)
os.Exit(1)
}
if len(ipList) > 1 {
index = rand.Intn(len(ipList))
} else {
index = 0
}
ip, _ = netip.ParseAddr(ipList[index].Unmap().String())
case tcping.userInput.useIPv6:
for _, ip := range ipAddrs {
if ip.Is6() {
ipList = append(ipList, ip)
}
}
if len(ipList) == 0 {
tcping.printError("Failed to find IPv6 address for %s", tcping.userInput.hostname)
os.Exit(1)
}
if len(ipList) > 1 {
index = rand.Intn(len(ipList))
} else {
index = 0
}
ip, _ = netip.ParseAddr(ipList[index].Unmap().String())
default:
if len(ipAddrs) > 1 {
index = rand.Intn(len(ipAddrs))
} else {
index = 0
}
ip, _ = netip.ParseAddr(ipAddrs[index].Unmap().String())
}
return ip
}
// resolveHostname handles hostname resolution with a timeout value of a second
func resolveHostname(tcping *tcping) netip.Addr {
ip, err := netip.ParseAddr(tcping.userInput.hostname)
if err == nil {
return ip
}
ctx, cancel := context.WithTimeout(context.Background(), dnsTimeout)
defer cancel()
ipAddrs, err := net.DefaultResolver.LookupNetIP(ctx, "ip", tcping.userInput.hostname)
// Prevent tcping to exit if it has been running for a while
if err != nil && (tcping.totalSuccessfulProbes != 0 || tcping.totalUnsuccessfulProbes != 0) {
return tcping.userInput.ip
} else if err != nil {
tcping.printError("Failed to resolve %s: %s", tcping.userInput.hostname, err)
os.Exit(1)
}
return selectResolvedIP(tcping, ipAddrs)
}
// retryResolveHostname retries resolving a hostname after certain number of failures
func retryResolveHostname(tcping *tcping) {
if tcping.ongoingUnsuccessfulProbes >= tcping.userInput.retryHostnameLookupAfter {
tcping.printRetryingToResolve(tcping.userInput.hostname)
tcping.userInput.ip = resolveHostname(tcping)
tcping.ongoingUnsuccessfulProbes = 0
tcping.retriedHostnameLookups += 1
// At this point hostnameChanges should have len > 0, but just in case
if len(tcping.hostnameChanges) == 0 {
return
}
lastAddr := tcping.hostnameChanges[len(tcping.hostnameChanges)-1].Addr
if lastAddr != tcping.userInput.ip {
tcping.hostnameChanges = append(tcping.hostnameChanges, hostnameChange{
Addr: tcping.userInput.ip,
When: time.Now(),
})
}
}
}
// newLongestTime creates LongestTime structure
func newLongestTime(startTime time.Time, duration time.Duration) longestTime {
return longestTime{
start: startTime,
end: startTime.Add(duration),
duration: duration,
}
}
// calcMinAvgMaxRttTime calculates min, avg and max RTT values
func calcMinAvgMaxRttTime(timeArr []float32) rttResult {
var sum float32
var result rttResult
arrLen := len(timeArr)
// rttResults.min = ^uint(0.0)
if arrLen > 0 {
result.min = timeArr[0]
}
for i := 0; i < arrLen; i++ {
sum += timeArr[i]
if timeArr[i] > result.max {
result.max = timeArr[i]
}
if timeArr[i] < result.min {
result.min = timeArr[i]
}
}
if arrLen > 0 {
result.hasResults = true
result.average = sum / float32(arrLen)
}
return result
}
// calcLongestUptime calculates the longest uptime and sets it to tcpStats.
func calcLongestUptime(tcping *tcping, duration time.Duration) {
if tcping.startOfUptime.IsZero() || duration == 0 {
return
}
longestUptime := newLongestTime(tcping.startOfUptime, duration)
// It means it is the first time we're calling this function
if tcping.longestUptime.end.IsZero() {
tcping.longestUptime = longestUptime
return
}
if longestUptime.duration >= tcping.longestUptime.duration {
tcping.longestUptime = longestUptime
}
}
// calcLongestDowntime calculates the longest downtime and sets it to tcpStats.
func calcLongestDowntime(tcping *tcping, duration time.Duration) {
if tcping.startOfDowntime.IsZero() || duration == 0 {
return
}
longestDowntime := newLongestTime(tcping.startOfDowntime, duration)
// It means it is the first time we're calling this function
if tcping.longestDowntime.end.IsZero() {
tcping.longestDowntime = longestDowntime
return
}
if longestDowntime.duration >= tcping.longestDowntime.duration {
tcping.longestDowntime = longestDowntime
}
}
// nanoToMillisecond returns an amount of milliseconds from nanoseconds.
// Using duration.Milliseconds() is not an option, because it drops
// decimal points, returning an int.
func nanoToMillisecond(nano int64) float32 {
return float32(nano) / float32(time.Millisecond)
}
// secondsToDuration returns the corresponding duration from seconds expressed with a float.
func secondsToDuration(seconds float64) time.Duration {
return time.Duration(1000*seconds) * time.Millisecond
}
// maxDuration is the implementation of the math.Max function for time.Duration types.
// returns the longest duration of x or y.
func maxDuration(x, y time.Duration) time.Duration {
if x > y {
return x
}
return y
}
// handleConnError processes failed probes
func (t *tcping) handleConnError(connTime time.Time, elapsed time.Duration) {
if !t.destWasDown {
t.startOfDowntime = connTime
uptime := t.startOfDowntime.Sub(t.startOfUptime)
calcLongestUptime(t, uptime)
t.startOfUptime = time.Time{}
t.destWasDown = true
}
t.totalDowntime += elapsed
t.lastUnsuccessfulProbe = connTime
t.totalUnsuccessfulProbes += 1
t.ongoingUnsuccessfulProbes += 1
t.printProbeFail(
t.userInput,
t.ongoingUnsuccessfulProbes,
)
}
// handleConnSuccess processes successful probes
func (t *tcping) handleConnSuccess(localAddr string, rtt float32, connTime time.Time, elapsed time.Duration) {
if t.destWasDown {
t.startOfUptime = connTime
downtime := t.startOfUptime.Sub(t.startOfDowntime)
calcLongestDowntime(t, downtime)
t.printTotalDownTime(downtime)
t.startOfDowntime = time.Time{}
t.destWasDown = false
t.ongoingUnsuccessfulProbes = 0
t.ongoingSuccessfulProbes = 0
}
if t.startOfUptime.IsZero() {
t.startOfUptime = connTime
}
t.totalUptime += elapsed
t.lastSuccessfulProbe = connTime
t.totalSuccessfulProbes += 1
t.ongoingSuccessfulProbes += 1
t.rtt = append(t.rtt, rtt)
if !t.userInput.showFailuresOnly {
t.printProbeSuccess(
localAddr,
t.userInput,
t.ongoingSuccessfulProbes,
rtt,
)
}
}
// tcpProbe pings a host, TCP style
func tcpProbe(tcping *tcping) {
var err error
var conn net.Conn
connStart := time.Now()
if tcping.userInput.networkInterface.use {
// dialer already contains the timeout value
conn, err = tcping.userInput.networkInterface.dialer.Dial("tcp", tcping.userInput.networkInterface.remoteAddr.String())
} else {
IPAndPort := netip.AddrPortFrom(tcping.userInput.ip, tcping.userInput.port)
conn, err = net.DialTimeout("tcp", IPAndPort.String(), tcping.userInput.timeout)
}
connDuration := time.Since(connStart)
rtt := nanoToMillisecond(connDuration.Nanoseconds())
elapsed := maxDuration(connDuration, tcping.userInput.intervalBetweenProbes)
if err != nil {
tcping.handleConnError(connStart, elapsed)
} else {
tcping.handleConnSuccess(conn.LocalAddr().String(), rtt, connStart, elapsed)
conn.Close()
}
<-tcping.ticker.C
}
func main() {
tcping := &tcping{}
processUserInput(tcping)
tcping.ticker = time.NewTicker(tcping.userInput.intervalBetweenProbes)
defer tcping.ticker.Stop()
signalHandler(tcping)
tcping.printStart(tcping.userInput.hostname, tcping.userInput.port)
stdinchan := make(chan bool)
go monitorSTDIN(stdinchan)
var probeCount uint
for {
if tcping.userInput.shouldRetryResolve {
retryResolveHostname(tcping)
}
tcpProbe(tcping)
select {
case pressedEnter := <-stdinchan:
if pressedEnter {
tcping.printStats()
}
default:
}
if tcping.userInput.probesBeforeQuit != 0 {
probeCount++
if probeCount == tcping.userInput.probesBeforeQuit {
shutdown(tcping)
}
}
}
}