-
Notifications
You must be signed in to change notification settings - Fork 0
/
pingwrapper.go
80 lines (69 loc) · 1.91 KB
/
pingwrapper.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
package main
import (
"log"
"net"
"regexp"
"strconv"
"strings"
)
type PingWrapperInterface interface {
Start()
Stop()
Host() string
CalcStats(int64) PWStats
}
var re_host_w_proto = regexp.MustCompile(`^(tcp|ip)([46])?://(\[?.+?\]?)(?::(\d+))?$`)
func NewPingWrapper(host string, options Options, transition_writer *TransitionWriter) PingWrapperInterface {
host_findings := re_host_w_proto.FindAllStringSubmatch(host, -1)
var found_proto, found_ip_family, found_host, found_port string
var found_port_int int
if len(host_findings) > 0 {
found_proto = host_findings[0][1]
found_ip_family = host_findings[0][2]
found_host = host_findings[0][3]
found_port = host_findings[0][4]
} else {
found_host = host
}
if found_proto == "tcp" {
if found_port == "" {
log.Fatalf("%v: tcp probing requested but no port given\n", host)
}
port, err := strconv.Atoi(found_port)
if err != nil {
log.Fatalf("%v: %v\n", host, err)
}
if port <= 0 || port > 65535 {
log.Fatalf("%v: tcp probing port invalid: %v\n", host, port)
}
found_port_int = port
return &TCPPingWrapper{
host: found_host,
ip: mustResolve(found_host, found_ip_family),
port: found_port_int,
stats: &PWStats{transition_writer: transition_writer},
}
} else if *options.system {
return &SystemPingWrapper{
host: host,
ip: mustResolve(found_host, found_ip_family),
stats: &PWStats{transition_writer: transition_writer},
ping_options: *options.system_ping_options,
}
} else {
return &ProbingWrapper{
host: host,
ip: mustResolve(found_host, found_ip_family),
privileged: *options.privileged,
stats: &PWStats{transition_writer: transition_writer},
}
}
}
func mustResolve(host string, ip_family string) *net.IPAddr {
host = strings.Trim(host, "[]")
ipaddr, err := net.ResolveIPAddr("ip"+ip_family, host)
if err != nil {
log.Fatal(err)
}
return ipaddr
}