Skip to content

Commit 8d418a6

Browse files
author
wardlin
committed
改为同步收集结果
1 parent d2399ec commit 8d418a6

File tree

9 files changed

+137
-450
lines changed

9 files changed

+137
-450
lines changed

icmp.go

+13-22
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ package ztrace
33
import (
44
"encoding/binary"
55
"net"
6-
"sync/atomic"
76
"time"
87

98
"github.com/sirupsen/logrus"
109
"golang.org/x/net/ipv4"
1110
)
1211

1312
func (t *TraceRoute) SendIPv4ICMP() error {
14-
key := GetHash(t.netSrcAddr.To4(), t.netDstAddr.To4(), 65535, 65535, 1)
13+
key := GetHash(t.NetSrcAddr.To4(), t.NetDstAddr.To4(), 65535, 65535, 1)
1514
db := NewStatsDB(key)
1615

1716
t.DB.Store(key, db)
1817
go db.Cache.Run()
19-
conn, err := net.ListenPacket("ip4:icmp", t.netSrcAddr.String())
18+
19+
conn, err := net.ListenPacket("ip4:icmp", t.NetSrcAddr.String())
2020
if err != nil {
2121
logrus.Error(err)
2222
return err
@@ -36,27 +36,23 @@ func (t *TraceRoute) SendIPv4ICMP() error {
3636
for ttl := 1; ttl <= int(t.MaxTTL); ttl++ {
3737
hdr, payload := t.BuildIPv4ICMP(uint8(ttl), id, id, 0)
3838
rSocket.WriteTo(hdr, payload, nil)
39-
report := &SendMetric{
39+
40+
m := &SendMetric{
4041
FlowKey: key,
4142
ID: uint32(hdr.ID),
4243
TTL: uint8(ttl),
4344
TimeStamp: time.Now(),
4445
}
45-
t.SendChan <- report
4646
id = (id + 1) % mod
4747

48-
//logrus.Info("send icmp ttl:", ttl)
49-
50-
//atomic.AddUint64(db.SendCnt, 1)
48+
t.RecordSend(m)
5149
}
5250

5351
return nil
5452
}
5553

5654
func (t *TraceRoute) ListenIPv4ICMP() error {
57-
defer t.Stop()
58-
59-
laddr := &net.IPAddr{IP: t.netSrcAddr}
55+
laddr := &net.IPAddr{IP: t.NetSrcAddr}
6056

6157
var err error
6258
t.recvICMPConn, err = net.ListenIP("ip4:icmp", laddr)
@@ -67,32 +63,24 @@ func (t *TraceRoute) ListenIPv4ICMP() error {
6763
}
6864
defer t.recvICMPConn.Close()
6965

70-
//设置超时时间
7166
t.recvICMPConn.SetReadDeadline(time.Now().Add(t.Timeout))
7267

7368
for {
74-
if atomic.LoadInt32(t.stopSignal) == 1 {
75-
break
76-
}
77-
7869
buf := make([]byte, 1500)
7970
n, raddr, err := t.recvICMPConn.ReadFrom(buf)
8071
if err != nil {
81-
//logrus.Error("recvICMPConn.ReadFrom failed:", err)
8272
break
8373
}
8474

8575
icmpType := buf[0]
86-
//logrus.Info(raddr, "|", icmpType, "|", n)
8776

88-
//TTL Exceeded or Port Unreachable
8977
if (icmpType == 11 || (icmpType == 3 && buf[1] == 3)) && (n >= 36) {
9078
id := binary.BigEndian.Uint16(buf[32:34])
9179

9280
dstip := net.IP(buf[24:28])
9381
srcip := net.IP(buf[20:24])
9482

95-
if dstip.Equal(t.netDstAddr) {
83+
if dstip.Equal(t.NetDstAddr) {
9684
key := GetHash(srcip, dstip, 65535, 65535, 1)
9785

9886
m := &RecvMetric{
@@ -101,12 +89,15 @@ func (t *TraceRoute) ListenIPv4ICMP() error {
10189
RespAddr: raddr.String(),
10290
TimeStamp: time.Now(),
10391
}
104-
//logrus.Info("recv icmp ttl:", id)
10592

106-
t.RecvChan <- m
93+
if t.RecordRecv(m) {
94+
break
95+
}
10796
}
10897
}
10998
}
11099

100+
t.Statistics()
101+
111102
return nil
112103
}

ipv6.go

+2-14
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ func (t *TraceRoute) TraceIpv6ICMP() error {
1919
return err
2020
}
2121

22-
//TODO:获取本地ipv6地址
23-
2422
icmp6Sock, err := net.ListenPacket("ip6:ipv6-icmp", t.SrcAddr)
2523
if err != nil {
2624
logrus.Error("Could not set a listening ICMP6 socket: ", err)
@@ -79,12 +77,8 @@ func (t *TraceRoute) TraceIpv6ICMP() error {
7977

8078
n, _, node, err := ipv6Sock.ReadFrom(buf)
8179

82-
//收集一个响应包的数据
8380
hop := map[string]interface{}{}
84-
85-
if err != nil {
86-
//fmt.Printf("%d %40s\n", i, "*")
87-
} else {
81+
if err == nil {
8882
answer, err := icmp.ParseMessage(58, buf[:n])
8983

9084
if err != nil {
@@ -95,7 +89,6 @@ func (t *TraceRoute) TraceIpv6ICMP() error {
9589
timeCost := time.Since(timeNow)
9690

9791
if answer.Type == ipv6.ICMPTypeTimeExceeded {
98-
//fmt.Printf("%d %40s %40s\n", i, node.String(), timeCost)
9992

10093
hop = map[string]interface{}{
10194
"rtt": fmt.Sprintf("%s", timeCost),
@@ -104,7 +97,6 @@ func (t *TraceRoute) TraceIpv6ICMP() error {
10497

10598
hopData.Details = append(hopData.Details, hop)
10699
} else if answer.Type == ipv6.ICMPTypeEchoReply {
107-
//fmt.Printf("%d %40s %40s\n", i, node.String(), timeCost)
108100
hop = map[string]interface{}{
109101
"rtt": fmt.Sprintf("%s", timeCost),
110102
"saddr": node.String(),
@@ -113,15 +105,11 @@ func (t *TraceRoute) TraceIpv6ICMP() error {
113105

114106
isDest = true
115107
break
116-
} else {
117-
//fmt.Printf("%d %40s\n", i, "*")
118108
}
119-
120109
}
121-
122110
}
123111

124-
t.hops = append(t.hops, hopData)
112+
t.Hops = append(t.Hops, hopData)
125113

126114
if isDest {
127115
break

packet.go

+10-17
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ func (t *TCPHeader) checksum(ip *ipv4.Header, payload []byte) {
116116
t.Checksum = checkSum(b.Bytes())
117117
}
118118

119-
// BuildIPv4UDPkt 构造UDP探测包,可设置TTL
120119
func (t *TraceRoute) BuildIPv4UDPkt(srcPort uint16, dstPort uint16, ttl uint8, id uint16, tos int) (*ipv4.Header, []byte) {
121120
iph := &ipv4.Header{
122121
Version: ipv4.Version,
@@ -129,8 +128,8 @@ func (t *TraceRoute) BuildIPv4UDPkt(srcPort uint16, dstPort uint16, ttl uint8, i
129128
TTL: int(ttl),
130129
Protocol: 17,
131130
Checksum: 0,
132-
Src: t.netSrcAddr,
133-
Dst: t.netDstAddr,
131+
Src: t.NetSrcAddr,
132+
Dst: t.NetDstAddr,
134133
}
135134

136135
h, err := iph.Marshal()
@@ -158,7 +157,6 @@ func (t *TraceRoute) BuildIPv4UDPkt(srcPort uint16, dstPort uint16, ttl uint8, i
158157
return iph, buf.Bytes()
159158
}
160159

161-
// BuildIPv4TCPSYN 构造TCP探测包,可设置TTL
162160
func (t *TraceRoute) BuildIPv4TCPSYN(srcPort uint16, dstPort uint16, ttl uint8, seq uint32, tos int) (*ipv4.Header, []byte) {
163161
iph := &ipv4.Header{
164162
Version: ipv4.Version,
@@ -171,8 +169,8 @@ func (t *TraceRoute) BuildIPv4TCPSYN(srcPort uint16, dstPort uint16, ttl uint8,
171169
TTL: int(ttl),
172170
Protocol: 6,
173171
Checksum: 0,
174-
Src: t.netSrcAddr,
175-
Dst: t.netDstAddr,
172+
Src: t.NetSrcAddr,
173+
Dst: t.NetDstAddr,
176174
}
177175

178176
h, err := iph.Marshal()
@@ -193,7 +191,6 @@ func (t *TraceRoute) BuildIPv4TCPSYN(srcPort uint16, dstPort uint16, ttl uint8,
193191
Urgent: 0,
194192
}
195193

196-
//payload is TCP Optionheader
197194
payload := []byte{0x02, 0x04, 0x05, 0xb4, 0x04, 0x02, 0x08, 0x0a, 0x7f, 0x73, 0xf9, 0x3a, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x07}
198195
tcp.checksum(iph, payload)
199196

@@ -215,8 +212,8 @@ func (t *TraceRoute) BuildIPv4TCPPRST(srcPort uint16, dstPort uint16, ttl uint8,
215212
TTL: int(ttl),
216213
Protocol: 6,
217214
Checksum: 0,
218-
Src: t.netSrcAddr,
219-
Dst: t.netDstAddr,
215+
Src: t.NetSrcAddr,
216+
Dst: t.NetDstAddr,
220217
}
221218

222219
h, err := iph.Marshal()
@@ -264,8 +261,8 @@ func (t *TraceRoute) BuildIPv4ICMP(ttl uint8, id, seq uint16, tos int) (*ipv4.He
264261
TTL: int(ttl),
265262
Protocol: 1,
266263
Checksum: 0,
267-
Src: t.netSrcAddr,
268-
Dst: t.netDstAddr,
264+
Src: t.NetSrcAddr,
265+
Dst: t.NetDstAddr,
269266
}
270267

271268
h, err := iph.Marshal()
@@ -276,7 +273,7 @@ func (t *TraceRoute) BuildIPv4ICMP(ttl uint8, id, seq uint16, tos int) (*ipv4.He
276273
iph.Checksum = int(checkSum(h))
277274

278275
icmp := ICMPHeader{
279-
IType: 8, //Echo
276+
IType: 8,
280277
ICode: 0,
281278
Checksum: 0,
282279
ID: id,
@@ -301,16 +298,14 @@ func (t *TraceRoute) BuildIPv4ICMP(ttl uint8, id, seq uint16, tos int) (*ipv4.He
301298

302299
func (t *TraceRoute) dnsResolve(hostName string, dst *net.IPAddr) (net.IP, error) {
303300
isIPv6 := false
304-
if t.af == "ip6"{
301+
if t.Af == "ip6"{
305302
isIPv6 = true
306303
}
307304

308305
ipAddr := net.ParseIP(hostName)
309306
if isIPv6 && ipAddr.To16() != nil {
310-
//logrus.Info("Using the provided ipv6 address ", ipAddr," for tracing")
311307
dst.IP = ipAddr
312308
} else if !isIPv6 && ipAddr.To4() != nil {
313-
//logrus.Info("Using the provided ipv4 address ", ipAddr, " for tracing")
314309
dst.IP = ipAddr
315310
} else {
316311
ips, err := net.LookupIP(hostName)
@@ -323,11 +318,9 @@ func (t *TraceRoute) dnsResolve(hostName string, dst *net.IPAddr) (net.IP, error
323318
for _, ip := range ips {
324319
if isIPv6 && ip.To16() != nil {
325320
dst.IP = ip
326-
//logrus.Info(hostName, " resolved to ", ip, ", using this ipv6 address for tracing")
327321
break
328322
} else if !isIPv6 && ip.To4() != nil {
329323
dst.IP = ip
330-
//logrus.Info(hostName, " resolved to ", ip, ", using this ipv4 address for tracing")
331324
break
332325
}
333326
}

probe.go

-53
This file was deleted.

0 commit comments

Comments
 (0)