This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
state.go
161 lines (140 loc) · 3.8 KB
/
state.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
package zerotrace
import (
"errors"
"fmt"
"net"
"sync"
"time"
)
const (
reqTimeout = time.Second * 3
ipidTimeout = time.Second * 10
)
// tracePkts represents a trace packet that we send to the client to determine
// the network-level RTT.
type tracePkt struct {
ttl uint8
ipID uint16
sent time.Time
recvd time.Time
recvdFrom net.IP
}
// respPkt represents a packet that we received in response to a trace packet.
// For simplicity, we re-use the trace packet here; in particular, the "recvd"
// and "recvdFrom" fields.
type respPkt tracePkt
// isAnswered returns true if the given trace packet has seen a response.
func (p *tracePkt) isAnswered() bool {
return !p.sent.IsZero() && !p.recvd.IsZero()
}
// String implements the Stringer interface.
func (p *tracePkt) String() string {
return fmt.Sprintf("%s (TTL=%d, IP ID=%d)",
p.recvdFrom, p.ttl, p.ipID,
)
}
// trState represents our traceroute state machine. We keep track of the
// following:
// 1. The IP address of the client that is the target of our traceroute.
// 2. Packets that we sent and received as part of the traceroute.
// 3. The IP IDs that we use as part of the traceroute.
type trState struct {
sync.Mutex // Guard tracePkts.
dstAddr net.IP
tracePkts map[uint16]*tracePkt
}
// newTrState returns a new traceroute state object.
func newTrState(dstAddr net.IP) *trState {
return &trState{
dstAddr: dstAddr,
tracePkts: make(map[uint16]*tracePkt),
}
}
// AddTracePkt adds to the state map a trace packet.
func (s *trState) addTracePkt(p *tracePkt) {
s.Lock()
defer s.Unlock()
s.tracePkts[p.ipID] = p
}
// AddRespPkt adds to the state map a packet that we got in response to a
// previously-sent trace packet.
func (s *trState) addRespPkt(p *respPkt) {
s.Lock()
defer s.Unlock()
tracePkt, exists := s.tracePkts[p.ipID]
if !exists {
return
}
// Mark the trace packet as "received".
tracePkt.recvd = p.recvd
tracePkt.recvdFrom = p.recvdFrom
}
// isFinished returns true if our state indicates that the 0trace scan is
// finished. That's the case when we haven't received any response packets
// since the timeout.
func (s *trState) isFinished() bool {
s.Lock()
defer s.Unlock()
now := time.Now().UTC()
for _, p := range s.tracePkts {
if p.isAnswered() {
continue
}
if now.Sub(p.sent) < reqTimeout {
return false
}
}
return true
}
// summary returns a printable string summary of the current traceroute state.
func (s *trState) summary() string {
s.Lock()
defer s.Unlock()
numRcvd := 0
for _, p := range s.tracePkts {
if p.isAnswered() {
numRcvd++
}
}
return fmt.Sprintf("%d pkts sent; %d pkts received so far.",
len(s.tracePkts), numRcvd)
}
// calcRTT determines the RTT between us and the client by looking for the
// trace packet that was answered by the client itself *or* for the trace
// packet that made it the farthest to the client (i.e., the packet whose TTL
// is the highest).
func (s *trState) calcRTT() (time.Duration, error) {
s.Lock()
defer s.Unlock()
var closestPkt *tracePkt
for _, p := range s.tracePkts {
if !p.isAnswered() {
continue
}
if closestPkt == nil {
closestPkt = p
}
// If we got a response from the target itself, we're done.
if p.recvdFrom.Equal(s.dstAddr) {
l.Println("Got response packet from the target itself.")
closestPkt = p
break
}
if p.ttl > closestPkt.ttl {
closestPkt = p
}
// If the TTL is identical, pick the packet whose RTT is the lowest.
if p.ttl == closestPkt.ttl {
closestPktRTT := closestPkt.recvd.Sub(closestPkt.sent)
pRTT := p.recvd.Sub(p.sent)
if pRTT < closestPktRTT {
closestPkt = p
}
}
}
if closestPkt != nil {
l.Printf("Closest response packet from: %s", closestPkt)
return closestPkt.recvd.Sub(closestPkt.sent), nil
}
return time.Duration(0), errors.New("no response packets")
}