forked from miekg/pcap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcap.go
414 lines (359 loc) · 9.39 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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// Package pcap is a wrapper around the pcap library.
package pcap
/*
#cgo LDFLAGS: -lpcap
#include <stdlib.h>
#include <pcap.h>
// Workaround for not knowing how to cast to const u_char**
int hack_pcap_next_ex(pcap_t * p, struct pcap_pkthdr **pkt_header,
u_char ** pkt_data)
{
return pcap_next_ex(p, pkt_header, (const u_char **)pkt_data);
}
void hack_pcap_dump(pcap_dumper_t * dumper, struct pcap_pkthdr *pkt_header,
u_char * pkt_data)
{
pcap_dump((u_char *)dumper, pkt_header, pkt_data);
}
*/
import "C"
import (
"errors"
"net"
"syscall"
"time"
"unsafe"
)
type Pcap struct {
cptr *C.pcap_t
}
type PcapDumper struct {
cptr *C.pcap_dumper_t
}
type pcapError struct{ string }
type Stat struct {
PacketsReceived uint32
PacketsDropped uint32
PacketsIfDropped uint32
}
type Interface struct {
Name string
Description string
Addresses []IFAddress
// TODO: add more elements
}
type IFAddress struct {
IP net.IP
Netmask net.IPMask
// TODO: add broadcast + PtP dst ?
}
func Version() string { return C.GoString(C.pcap_lib_version()) }
func (p *Pcap) Datalink() int { return int(C.pcap_datalink(p.cptr)) }
func (e *pcapError) Error() string { return e.string }
func (p *Pcap) Geterror() error { return &pcapError{C.GoString(C.pcap_geterr(p.cptr))} }
func (p *Pcap) Next() (pkt *Packet) { rv, _ := p.NextEx(); return rv }
func Create(device string) (handle *Pcap, err error) {
var buf *C.char
buf = (*C.char)(C.calloc(ERRBUF_SIZE, 1))
h := new(Pcap)
dev := C.CString(device)
defer C.free(unsafe.Pointer(dev))
h.cptr = C.pcap_create(dev, buf)
if nil == h.cptr {
handle = nil
err = &pcapError{C.GoString(buf)}
} else {
handle = h
}
C.free(unsafe.Pointer(buf))
return
}
// Set buffer size (units in bytes) on activated handle.
func (p *Pcap) SetBufferSize(sz int32) error {
if C.pcap_set_buffer_size(p.cptr, C.int(sz)) != 0 {
return p.Geterror()
}
return nil
}
// If arg p is non-zero promiscuous mode will be set on capture handle when it is activated.
func (p *Pcap) SetPromisc(promisc bool) error {
var pro int32
if promisc {
pro = 1
}
if C.pcap_set_promisc(p.cptr, C.int(pro)) != 0 {
return p.Geterror()
}
return nil
}
func (p *Pcap) SetSnapLen(s int32) error {
if C.pcap_set_snaplen(p.cptr, C.int(s)) != 0 {
return p.Geterror()
}
return nil
}
// Set read timeout (milliseconds) that will be used on a capture handle when it is activated.
func (p *Pcap) SetReadTimeout(toMs int32) error {
if C.pcap_set_timeout(p.cptr, C.int(toMs)) != 0 {
return p.Geterror()
}
return nil
}
// Activate a packet capture handle to look at packets on the network, with the options that
// were set on the handle being in effect.
func (p *Pcap) Activate() error {
if C.pcap_activate(p.cptr) != 0 {
return p.Geterror()
}
return nil
}
// OpenLive opens a device and returns a handler.
func OpenLive(device string, snaplen int32, promisc bool, timeout_ms int32) (handle *Pcap, err error) {
var buf *C.char
buf = (*C.char)(C.calloc(ERRBUF_SIZE, 1))
h := new(Pcap)
var pro int32
if promisc {
pro = 1
}
dev := C.CString(device)
defer C.free(unsafe.Pointer(dev))
h.cptr = C.pcap_open_live(dev, C.int(snaplen), C.int(pro), C.int(timeout_ms), buf)
if nil == h.cptr {
handle = nil
err = &pcapError{C.GoString(buf)}
} else {
handle = h
}
C.free(unsafe.Pointer(buf))
return
}
// Openoffline
func OpenOffline(file string) (handle *Pcap, err error) {
var buf *C.char
buf = (*C.char)(C.calloc(ERRBUF_SIZE, 1))
h := new(Pcap)
cf := C.CString(file)
defer C.free(unsafe.Pointer(cf))
h.cptr = C.pcap_open_offline(cf, buf)
if nil == h.cptr {
handle = nil
err = &pcapError{C.GoString(buf)}
} else {
handle = h
}
C.free(unsafe.Pointer(buf))
return
}
// Pcap closes a handler.
func (p *Pcap) Close() {
C.pcap_close(p.cptr)
}
func (p *Pcap) NextEx() (pkt *Packet, result int32) {
var pkthdr_ptr *C.struct_pcap_pkthdr
var pkthdr C.struct_pcap_pkthdr
var buf_ptr *C.u_char
var buf unsafe.Pointer
result = int32(C.hack_pcap_next_ex(p.cptr, &pkthdr_ptr, &buf_ptr))
buf = unsafe.Pointer(buf_ptr)
pkthdr = *pkthdr_ptr
if nil == buf {
return
}
pkt = new(Packet)
pkt.Time = time.Unix(int64(pkthdr.ts.tv_sec), int64(pkthdr.ts.tv_usec)*1000) // pcap provides usec but time.Unix requires nsec
pkt.Caplen = uint32(pkthdr.caplen)
pkt.Len = uint32(pkthdr.len)
pkt.Data = make([]byte, pkthdr.caplen)
for i := uint32(0); i < pkt.Caplen; i++ {
pkt.Data[i] = *(*byte)(unsafe.Pointer(uintptr(buf) + uintptr(i)))
}
return
}
func (p *Pcap) Getstats() (stat *Stat, err error) {
var cstats C.struct_pcap_stat
if -1 == C.pcap_stats(p.cptr, &cstats) {
return nil, p.Geterror()
}
stats := new(Stat)
stats.PacketsReceived = uint32(cstats.ps_recv)
stats.PacketsDropped = uint32(cstats.ps_drop)
stats.PacketsIfDropped = uint32(cstats.ps_ifdrop)
return stats, nil
}
func (p *Pcap) SetFilter(expr string) (err error) {
var bpf C.struct_bpf_program
cexpr := C.CString(expr)
defer C.free(unsafe.Pointer(cexpr))
if -1 == C.pcap_compile(p.cptr, &bpf, cexpr, 1, 0) {
return p.Geterror()
}
if -1 == C.pcap_setfilter(p.cptr, &bpf) {
C.pcap_freecode(&bpf)
return p.Geterror()
}
C.pcap_freecode(&bpf)
return nil
}
func (p *Pcap) SetDirection(direction string) (err error) {
var pcap_direction C.pcap_direction_t
if direction == "in" {
pcap_direction = C.PCAP_D_IN
} else if direction == "out" {
pcap_direction = C.PCAP_D_OUT
} else {
pcap_direction = C.PCAP_D_INOUT
}
if -1 == C.pcap_setdirection(p.cptr, pcap_direction) {
return p.Geterror()
}
return nil
}
func (p *Pcap) SetDataLink(dlt int) error {
if -1 == C.pcap_set_datalink(p.cptr, C.int(dlt)) {
return p.Geterror()
}
return nil
}
func DatalinkValueToName(dlt int) string {
if name := C.pcap_datalink_val_to_name(C.int(dlt)); name != nil {
return C.GoString(name)
}
return ""
}
func DatalinkValueToDescription(dlt int) string {
if desc := C.pcap_datalink_val_to_description(C.int(dlt)); desc != nil {
return C.GoString(desc)
}
return ""
}
func FindAllDevs() (ifs []Interface, err error) {
var buf *C.char
buf = (*C.char)(C.calloc(ERRBUF_SIZE, 1))
defer C.free(unsafe.Pointer(buf))
var alldevsp *C.pcap_if_t
if -1 == C.pcap_findalldevs((**C.pcap_if_t)(&alldevsp), buf) {
return nil, errors.New(C.GoString(buf))
}
defer C.pcap_freealldevs((*C.pcap_if_t)(alldevsp))
dev := alldevsp
var i uint32
for i = 0; dev != nil; dev = (*C.pcap_if_t)(dev.next) {
i++
}
ifs = make([]Interface, i)
dev = alldevsp
for j := uint32(0); dev != nil; dev = (*C.pcap_if_t)(dev.next) {
var iface Interface
iface.Name = C.GoString(dev.name)
iface.Description = C.GoString(dev.description)
iface.Addresses = findAllAddresses(dev.addresses)
// TODO: add more elements
ifs[j] = iface
j++
}
return
}
func findAllAddresses(addresses *C.struct_pcap_addr) (retval []IFAddress) {
// TODO - make it support more than IPv4 and IPv6?
retval = make([]IFAddress, 0, 1)
for curaddr := addresses; curaddr != nil; curaddr = (*C.struct_pcap_addr)(curaddr.next) {
if curaddr.addr == nil {
continue
}
var a IFAddress
var err error
if a.IP, err = sockaddrToIP((*syscall.RawSockaddr)(unsafe.Pointer(curaddr.addr))); err != nil {
continue
}
if a.Netmask, err = sockaddrToIP((*syscall.RawSockaddr)(unsafe.Pointer(curaddr.netmask))); err != nil {
continue
}
retval = append(retval, a)
}
return
}
func sockaddrToIP(rsa *syscall.RawSockaddr) (IP []byte, err error) {
switch rsa.Family {
case syscall.AF_INET:
pp := (*syscall.RawSockaddrInet4)(unsafe.Pointer(rsa))
IP = make([]byte, 4)
for i := 0; i < len(IP); i++ {
IP[i] = pp.Addr[i]
}
return
case syscall.AF_INET6:
pp := (*syscall.RawSockaddrInet6)(unsafe.Pointer(rsa))
IP = make([]byte, 16)
for i := 0; i < len(IP); i++ {
IP[i] = pp.Addr[i]
}
return
}
err = errors.New("Unsupported address type")
return
}
// Inject ...
func (p *Pcap) Inject(data []byte) (err error) {
buf := (*C.char)(C.malloc((C.size_t)(len(data))))
for i := 0; i < len(data); i++ {
*(*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(buf)) + uintptr(i))) = data[i]
}
if -1 == C.pcap_inject(p.cptr, unsafe.Pointer(buf), (C.size_t)(len(data))) {
err = p.Geterror()
}
C.free(unsafe.Pointer(buf))
return
}
func (p *Pcap) DumpOpen(ofile *string) (dumper *PcapDumper, err error) {
d := new(PcapDumper)
d.cptr = C.pcap_dump_open(p.cptr, C.CString(*ofile))
if nil == d.cptr {
return d, errors.New("Cannot open dumpfile")
}
dumper = d
return
}
func (p *Pcap) PcapLoop(pktnum int, dumper *PcapDumper) (result int32, err error) {
var pkthdr_ptr *C.struct_pcap_pkthdr
var buf_ptr *C.u_char
for i := 0; true; {
result = int32(C.hack_pcap_next_ex(p.cptr, &pkthdr_ptr, &buf_ptr))
switch result {
case 0:
continue // timeout
case 1:
// success : capturing packet
case -1:
return result, errors.New("Error in pcap next ex")
case -2:
return // reach EOF in offline mode
}
if nil == buf_ptr {
continue
}
if nil != dumper {
p.PcapDump(dumper, pkthdr_ptr, buf_ptr)
p.PcapDumpFlush(dumper)
}
if pktnum > 0 {
i++
if i >= pktnum {
break
}
}
}
return
}
func (p *Pcap) PcapDump(dumper *PcapDumper, pkthdr_ptr *C.struct_pcap_pkthdr, buf_ptr *C.u_char) {
C.hack_pcap_dump(dumper.cptr, pkthdr_ptr, buf_ptr)
}
func (p *Pcap) PcapDumpFlush(dumper *PcapDumper) error {
if -1 == C.pcap_dump_flush(dumper.cptr) {
return p.Geterror()
}
return nil
}
func (p *Pcap) PcapDumpClose(dumper *PcapDumper) {
C.pcap_dump_close(dumper.cptr)
}