-
Notifications
You must be signed in to change notification settings - Fork 38
/
latency.go
257 lines (218 loc) · 6.11 KB
/
latency.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
/*
Copyright 2013-2014 Graham King
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
For full license details see <http://www.gnu.org/licenses/>.
*/
package main
import (
"flag"
"fmt"
"log"
"math/rand"
"net"
"os"
"strconv"
"strings"
"sync"
"time"
)
var (
ifaceParam = flag.String("i", "", "Interface (e.g. eth0, wlan1, etc)")
helpParam = flag.Bool("h", false, "Print help")
portParam = flag.Int("p", 80, "Port to test against (default 80)")
autoParam = flag.Bool("a", false, "Measure latency to several well known addresses")
defaultHosts = map[string]string{
// Busiest sites on the Internet, according to Wolfram Alpha
"Google": "google.com",
"Facebook": "facebook.com",
"Baidu": "baidu.com",
// Various locations, thanks Linode
"West Coast, USA": "speedtest.fremont.linode.com",
"East Coast, USA": "speedtest.newark.linode.com",
"London, UK": "speedtest.london.linode.com",
"Tokyo, JP": "speedtest.tokyo.linode.com",
// Other continents
"New Zealand": "nzdsl.co.nz",
"South Africa": "speedtest.mybroadband.co.za",
}
)
func main() {
flag.Parse()
if *helpParam {
printHelp()
os.Exit(1)
}
iface := *ifaceParam
if iface == "" {
iface = chooseInterface()
if iface == "" {
fmt.Println("Could not decide which net interface to use.")
fmt.Println("Specify it with -i <iface> param")
os.Exit(1)
}
}
localAddr := interfaceAddress(iface)
laddr := strings.Split(localAddr.String(), "/")[0] // Clean addresses like 192.168.1.30/24
port := uint16(*portParam)
if *autoParam {
autoTest(laddr, port)
return
}
if len(flag.Args()) == 0 {
fmt.Println("Missing remote address")
printHelp()
os.Exit(1)
}
remoteHost := flag.Arg(0)
fmt.Println("Measuring round-trip latency from", laddr, "to", remoteHost, "on port", port)
fmt.Printf("Latency: %v\n", latency(laddr, remoteHost, port))
}
func autoTest(localAddr string, port uint16) {
for name, host := range defaultHosts {
fmt.Printf("%15s: %v\n", name, latency(localAddr, host, port))
}
}
func latency(localAddr string, remoteHost string, port uint16) time.Duration {
var wg sync.WaitGroup
wg.Add(1)
var receiveTime time.Time
addrs, err := net.LookupHost(remoteHost)
if err != nil {
log.Fatalf("Error resolving %s. %s\n", remoteHost, err)
}
remoteAddr := addrs[0]
go func() {
receiveTime = receiveSynAck(localAddr, remoteAddr)
wg.Done()
}()
time.Sleep(1 * time.Millisecond)
sendTime := sendSyn(localAddr, remoteAddr, port)
wg.Wait()
return receiveTime.Sub(sendTime)
}
func chooseInterface() string {
interfaces, err := net.Interfaces()
if err != nil {
log.Fatalf("net.Interfaces: %s", err)
}
for _, iface := range interfaces {
// Skip loopback
if iface.Name == "lo" {
continue
}
addrs, err := iface.Addrs()
// Skip if error getting addresses
if err != nil {
log.Println("Error get addresses for interfaces %s. %s", iface.Name, err)
continue
}
if len(addrs) > 0 {
// This one will do
return iface.Name
}
}
return ""
}
func interfaceAddress(ifaceName string) net.Addr {
iface, err := net.InterfaceByName(ifaceName)
if err != nil {
log.Fatalf("net.InterfaceByName for %s. %s", ifaceName, err)
}
addrs, err := iface.Addrs()
if err != nil {
log.Fatalf("iface.Addrs: %s", err)
}
return addrs[0]
}
func printHelp() {
help := `
USAGE: latency [-h] [-a] [-i iface] [-p port] <remote>
Where 'remote' is an ip address or host name.
Default port is 80
-h: Help
-a: Run auto test against several well known sites
`
fmt.Println(help)
}
func sendSyn(laddr, raddr string, port uint16) time.Time {
packet := TCPHeader{
Source: 0xaa47, // Random ephemeral port
Destination: port,
SeqNum: rand.Uint32(),
AckNum: 0,
DataOffset: 5, // 4 bits
Reserved: 0, // 3 bits
ECN: 0, // 3 bits
Ctrl: 2, // 6 bits (000010, SYN bit set)
Window: 0xaaaa, // The amount of data that it is able to accept in bytes
Checksum: 0, // Kernel will set this if it's 0
Urgent: 0,
Options: []TCPOption{},
}
data := packet.Marshal()
packet.Checksum = Csum(data, to4byte(laddr), to4byte(raddr))
data = packet.Marshal()
//fmt.Printf("% x\n", data)
conn, err := net.Dial("ip4:tcp", raddr)
if err != nil {
log.Fatalf("Dial: %s\n", err)
}
sendTime := time.Now()
numWrote, err := conn.Write(data)
if err != nil {
log.Fatalf("Write: %s\n", err)
}
if numWrote != len(data) {
log.Fatalf("Short write. Wrote %d/%d bytes\n", numWrote, len(data))
}
conn.Close()
return sendTime
}
func to4byte(addr string) [4]byte {
parts := strings.Split(addr, ".")
b0, err := strconv.Atoi(parts[0])
if err != nil {
log.Fatalf("to4byte: %s (latency works with IPv4 addresses only, but not IPv6!)\n", err)
}
b1, _ := strconv.Atoi(parts[1])
b2, _ := strconv.Atoi(parts[2])
b3, _ := strconv.Atoi(parts[3])
return [4]byte{byte(b0), byte(b1), byte(b2), byte(b3)}
}
func receiveSynAck(localAddress, remoteAddress string) time.Time {
netaddr, err := net.ResolveIPAddr("ip4", localAddress)
if err != nil {
log.Fatalf("net.ResolveIPAddr: %s. %s\n", localAddress, netaddr)
}
conn, err := net.ListenIP("ip4:tcp", netaddr)
if err != nil {
log.Fatalf("ListenIP: %s\n", err)
}
var receiveTime time.Time
for {
buf := make([]byte, 1024)
numRead, raddr, err := conn.ReadFrom(buf)
if err != nil {
log.Fatalf("ReadFrom: %s\n", err)
}
if raddr.String() != remoteAddress {
// this is not the packet we are looking for
continue
}
receiveTime = time.Now()
//fmt.Printf("Received: % x\n", buf[:numRead])
tcp := NewTCPHeader(buf[:numRead])
// Closed port gets RST, open port gets SYN ACK
if tcp.HasFlag(RST) || (tcp.HasFlag(SYN) && tcp.HasFlag(ACK)) {
break
}
}
return receiveTime
}