-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcap.go
216 lines (195 loc) · 5.02 KB
/
pcap.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
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"runtime"
"runtime/pprof"
"syscall"
"github.com/urbanishimwe/packet"
)
var (
conf *packet.Config
iff, expr string
pcapFile, cpuProf string
sig chan os.Signal
snap int
)
func init() {
conf = packet.DefaultConfig()
flag.StringVar(&iff, "i", "", "interface name")
flag.StringVar(&expr, "e", "", "attach bpf expression(require libpcap and to buuld with 'bpf' tag)")
flag.IntVar(&snap, "snap", 256*1024, "snapshot length(used with -e)")
flag.StringVar(&pcapFile, "f", "packet.pcap", "save packets in this pcap file")
flag.Int64Var(&conf.ReadTimeout, "read-timeout", conf.ReadTimeout, "read timeout in milliseconds")
flag.Int64Var(&conf.ReadBufferSize, "read-buffer", conf.ReadBufferSize, "read buffer size")
flag.Int64Var(&conf.ReadBufferTimeout, "read-buffer-timeout", conf.ReadBufferTimeout, "read buffer timeout in milliseconds")
flag.BoolVar(&conf.NonBlock, "non-block", conf.NonBlock, "enable non-blocking mode")
flag.BoolVar(&conf.ImmediateMode, "immediate", conf.ImmediateMode, "enable immediate mode")
flag.BoolVar(&conf.Promiscuous, "promisc", conf.NonBlock, "enable promiscuous mode")
flag.BoolVar(&conf.NoLinkLayer, "cooked", conf.NoLinkLayer, "enable cooked mode(remove link layer header)")
flag.Var((*proto)(&conf.Proto), "proto", "ethernet protocol(all, ip, ip6, arp)")
flag.Var((*direction)(&conf.Direction), "direction", "packet flow(inout, in, out)")
flag.Var((*tstampResolution)(&conf.TstampResolution), "resolution", "timestamp resolution(nano, micro)")
flag.Uint64Var(&conf.MaxNilRead, "nil-read", conf.MaxNilRead, "max nil read")
flag.StringVar(&cpuProf, "cpu-prof", "", "save cpu profile in this file")
flag.Usage = func() {
fmt.Fprintln(os.Stderr, "helper program to capture packets and save them in .pcap file format")
flag.PrintDefaults()
}
}
func main() {
flag.Parse()
setBPF()
handler, err := packet.NewHandler(iff, conf)
exitonError(err)
fd, err := initPcapFile(pcapFile, handler.Config())
exitonError(err)
cpuProfile()
go capture(fd, handler)
sig = make(chan os.Signal, 2)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
<-sig
fmt.Printf("%#v\n", *handler.Stats(true))
handler.Close()
syscall.Close(int(fd))
if cpuProf != "" {
pprof.StopCPUProfile()
}
}
func capture(fd uintptr, handler packet.Handler) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
for {
buf, info, err := handler.Read(true)
if err == nil {
if err = writePacketInfo(fd, info); err == nil {
err = writePacketData(fd, buf)
}
if err != nil {
fmt.Fprint(os.Stderr, err)
}
continue
}
// check if error is recoverable
if packet.Temporary(err) || packet.Timeout(err) {
continue
}
fmt.Fprintln(os.Stderr, err)
sig <- os.Interrupt
}
}
func setBPF() {
if expr == "" {
return
}
linktype := packet.InterfaceLinkType(iff)
if linktype != packet.LinkTypeEthernet {
linktype = DLT_RAW
}
var err error
conf.Filter, err = expr2Filter(expr, int(linktype))
exitonError(err)
}
func cpuProfile() {
if cpuProf == "" {
return
}
f, err := os.Create(cpuProf)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
pprof.StartCPUProfile(f)
}
func exitonError(err error) {
if err == nil {
return
}
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
type direction packet.Direction
var _ flag.Value = new(direction)
func (d *direction) String() string {
switch packet.Direction(*d) {
case packet.DirInOut:
return "inout"
case packet.DirIn:
return "in"
case packet.DirOut:
return "out"
default:
return fmt.Sprintf("<invalid:%d>", *d)
}
}
// Set for this function to implement flag.Setter
func (d *direction) Set(v string) error {
switch v {
case "inout":
*d = direction(packet.DirInOut)
case "in":
*d = direction(packet.DirIn)
case "out":
*d = direction(packet.DirOut)
default:
return syscall.EINVAL
}
return nil
}
type tstampResolution packet.TstampResolution
var _ flag.Value = new(tstampResolution)
func (t *tstampResolution) String() string {
switch packet.TstampResolution(*t) {
case packet.TstampNano:
return "nano"
case packet.TstampMicro:
return "micro"
default:
return fmt.Sprintf("<invalid:%d>", *t)
}
}
// Set for this function to implement flag.Setter
func (t *tstampResolution) Set(v string) error {
switch v {
case "nano":
*t = tstampResolution(packet.TstampNano)
case "micro":
*t = tstampResolution(packet.TstampMicro)
default:
return syscall.EINVAL
}
return nil
}
type proto packet.Proto
var _ flag.Value = new(proto)
func (p *proto) String() string {
switch packet.Proto(*p) {
case packet.ProtoIP:
return "ip"
case packet.ProtoIP6:
return "ip6"
case packet.ProtoARP:
return "arp"
case packet.ProtoAll:
return "all"
default:
return fmt.Sprintf("<invalid:%d>", *p)
}
}
func (p *proto) Set(v string) error {
switch v {
case "ip":
*p = proto(packet.ProtoIP)
case "ip6":
*p = proto(packet.ProtoIP6)
case "arp":
*p = proto(packet.ProtoARP)
case "all":
*p = proto(packet.ProtoAll)
default:
return syscall.EINVAL
}
return nil
}