Debian Codegraph @

network/pcap.go

raw symbols SCIP
1 package network
2
3 import (
4 "fmt"
5 "time"
6
7 "github.com/evilsocket/islazy/tui"
8 "github.com/google/gopacket/pcap"
9 )
10
11 const (
12 PCAP_DEFAULT_SETRF = false
13 PCAP_DEFAULT_SNAPLEN = 65536
14 PCAP_DEFAULT_BUFSIZE = 2_097_152
15 PCAP_DEFAULT_PROMISC = true
16 PCAP_DEFAULT_TIMEOUT = pcap.BlockForever
17 )
18
19 var CAPTURE_DEFAULTS = CaptureOptions{
20 Monitor: PCAP_DEFAULT_SETRF,
21 Snaplen: PCAP_DEFAULT_SNAPLEN,
22 Bufsize: PCAP_DEFAULT_BUFSIZE,
23 Promisc: PCAP_DEFAULT_PROMISC,
24 Timeout: PCAP_DEFAULT_TIMEOUT,
25 }
26
27 type CaptureOptions struct {
28 Monitor bool
29 Snaplen int
30 Bufsize int
31 Promisc bool
32 Timeout time.Duration
33 }
34
35 func CaptureWithOptions(ifName string, options CaptureOptions) (*pcap.Handle, error) {
36 Debug("creating capture for '%s' with options: %+v", ifName, options)
37
38 ihandle, err := pcap.NewInactiveHandle(ifName)
39 if err != nil {
40 return nil, fmt.Errorf("error while opening interface %s: %s", ifName, err)
41 }
42 defer ihandle.CleanUp()
43
44 if options.Monitor {
45 if err = ihandle.SetRFMon(true); err != nil {
46 return nil, fmt.Errorf("error while setting interface %s in monitor mode: %s", tui.Bold(ifName), err)
47 }
48 }
49
50 if err = ihandle.SetSnapLen(options.Snaplen); err != nil {
51 return nil, fmt.Errorf("error while settng snapshot length: %s", err)
52 } else if err = ihandle.SetBufferSize(options.Bufsize); err != nil {
53 return nil, fmt.Errorf("error while settng buffer size: %s", err)
54 } else if err = ihandle.SetPromisc(options.Promisc); err != nil {
55 return nil, fmt.Errorf("error while settng promiscuous mode to %v: %s", options.Promisc, err)
56 } else if err = ihandle.SetTimeout(options.Timeout); err != nil {
57 return nil, fmt.Errorf("error while settng snapshot length: %s", err)
58 }
59
60 return ihandle.Activate()
61 }
62
63 func Capture(ifName string) (*pcap.Handle, error) {
64 return CaptureWithOptions(ifName, CAPTURE_DEFAULTS)
65 }
66
67 func CaptureWithTimeout(ifName string, timeout time.Duration) (*pcap.Handle, error) {
68 var opts = CAPTURE_DEFAULTS
69 opts.Timeout = timeout
70 return CaptureWithOptions(ifName, opts)
71 }