-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
135 lines (121 loc) · 3.01 KB
/
main.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
package main
import (
"bytes"
"encoding/binary"
"flag"
"fmt"
"os"
"time"
"github.com/busoc/erdle"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
)
type Coze struct {
Count int
Size int
Gaps int
Missing int
Elapsed time.Duration
}
const line = "%d cadus (expected: %d cadus), %d gaps (%s), %d missing (%.2f%%), %dKB"
func (z *Coze) Update(c *Coze) {
z.Count += c.Count
z.Size += c.Size
z.Gaps += c.Gaps
z.Missing += c.Missing
z.Elapsed += c.Elapsed
}
func main() {
// defer func() {
// if err := recover(); err != nil {
// fmt.Fprintf(os.Stderr, "unexpected error: %s\n", err)
// os.Exit(3)
// }
// }()
list := flag.Bool("l", false, "show cadus list")
diff := flag.Bool("g", false, "show cadus gaps")
flag.Parse()
if *list && *diff {
fmt.Fprintln(os.Stderr, "-l and -d flags can not be set together")
os.Exit(1)
}
var z Coze
for _, a := range flag.Args() {
h, err := pcap.OpenOffline(a)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if err := listCadus(h, &z, *list, *diff); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
}
}
ratio := float64(z.Missing) / float64(z.Count+z.Missing)
fmt.Fprintf(os.Stdout, line, z.Count, z.Count+z.Missing, z.Gaps, z.Elapsed, z.Missing, ratio*100, z.Size>>10)
fmt.Fprintln(os.Stdout)
}
func listCadus(h *pcap.Handle, c *Coze, list, gap bool) error {
d := struct {
Curr uint32
When time.Time
Elapsed time.Duration
}{}
defer h.Close()
s := gopacket.NewPacketSource(h, h.LinkType())
for {
p, err := s.NextPacket()
if err != nil {
break
}
layer := p.ApplicationLayer()
if layer == nil {
continue
}
xs := layer.Payload()
if !bytes.HasPrefix(xs, erdle.Magic) {
continue
}
c.Count++
c.Size += len(xs)
var missing uint32
curr := binary.BigEndian.Uint32(xs[6:]) >> 8
md := p.Metadata()
t := md.Timestamp.UTC()
if diff := (curr - d.Curr) & 0xFFFFFF; diff != curr && diff > 1 {
missing = diff
c.Missing += int(missing)
c.Gaps++
if !d.When.IsZero() {
c.Elapsed += t.Sub(d.When)
}
}
if !list && gap && missing > 0 {
fmt.Fprintf(os.Stdout, "%5d | %12s | %s | %s | %7d | %7d | %d\n", c.Gaps, d.Elapsed, d.When.Format(time.RFC3339), t.Format(time.RFC3339), d.Curr, curr, missing)
}
if list && !gap {
sn, dn := p.NetworkLayer().NetworkFlow().Endpoints()
sp, dp := p.TransportLayer().TransportFlow().Endpoints()
var proto string
if i := p.Layer(layers.LayerTypeIPv4); i != nil {
i := i.(*layers.IPv4)
proto = i.Protocol.String()
} else if i := p.Layer(layers.LayerTypeIPv6); i != nil {
i := i.(*layers.IPv6)
proto = i.NextHeader.String()
} else {
proto = "unknown"
}
fmt.Fprintf(os.Stdout, "%8d | %12s | %s | %s:%s | %s:%s | %s | %6d | %d\n", c.Count, d.Elapsed, t.Format(time.RFC3339), sn, sp, dn, dp, proto, len(xs), missing)
}
if !d.When.IsZero() {
d.Elapsed += t.Sub(d.When)
if d.Elapsed < 0 {
d.Elapsed = 0
}
}
d.Curr, d.When = curr, t
}
return nil
}