-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
79 lines (69 loc) · 1.61 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
package main
import (
"flag"
"fmt"
"jakub-m/bdp/flow"
"jakub-m/bdp/packet"
"jakub-m/bdp/pcap"
"jakub-m/bdp/stats"
"log"
"os"
)
var args struct {
pcapFname string
localIP *pcap.IPv4
remoteIP *pcap.IPv4
statsMode bool
}
func init() {
var localIPString string
var remoteIPString string
flag.StringVar(&args.pcapFname, "i", "", "pcap file")
flag.StringVar(&localIPString, "l", "", "local IP (e.g. 192.168.1.2)")
flag.StringVar(&remoteIPString, "r", "", "remote IP (e.g. 123.123.123.123)")
flag.BoolVar(&args.statsMode, "s", false, "Print rudimentary flow statistics")
flag.Parse()
args.localIP = ipFromStringOrExit(localIPString)
args.remoteIP = ipFromStringOrExit(remoteIPString)
}
func ipFromStringOrExit(ipString string) *pcap.IPv4 {
if ipString == "" {
return nil
}
ip, err := pcap.IPv4FromString(ipString)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return &ip
}
func main() {
log.SetFlags(0)
log.Println("Pcap file name: ", args.pcapFname)
log.Println("Local IP: ", args.localIP)
log.Println("Remote IP: ", args.remoteIP)
file, err := os.Open(args.pcapFname)
if err != nil {
log.Fatal(err)
}
defer file.Close()
onPcapError := func(err error) bool {
log.Printf("Packet reading error: %s", err)
return true
}
// Load all the packets to memory. It can be easily converted to streaming.
packets, err := packet.LoadFromFile(file, onPcapError)
if err != nil {
log.Fatal(err)
}
if args.statsMode {
// Stats mode.
stats.ProcessPackets(packets)
} else {
// BDP mode.
err = flow.ProcessPackets(packets, args.localIP, args.remoteIP)
if err != nil {
log.Fatal(err)
}
}
}