-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.go
76 lines (68 loc) · 1.71 KB
/
run.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
package tcapflow
import (
"fmt"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"io"
)
func reportParseError(handler DataHandler, data []uint8) {
if r := recover(); r != nil {
handler.ParseError(data, r)
}
}
func handleSCTPData(handler DataHandler, data *layers.SCTPData, packet gopacket.Packet) {
defer reportParseError(handler, data.Payload)
switch data.PayloadProtocol {
case layers.SCTPPayloadM2UA:
HandleM2UA(handler, data, packet)
case layers.SCTPPayloadM3UA:
HandleM3UA(handler, data.Payload, packet)
case layers.SCTPPayloadM2PA:
HandleM2PA(handler, data.Payload, packet)
case layers.SCTPPayloadSUA:
HandleSUA(handler, data, packet)
}
}
func handlePacket(handler DataHandler, packet gopacket.Packet) {
for _, p := range packet.Layers() {
if data, err := p.(*layers.SCTPData); err {
handleSCTPData(handler, data, packet)
}
}
}
func RunLoop(pcapFile string, pcapDevice string, pcapFilter string, handler DataHandler) {
// Open file or live...
var handle *pcap.Handle
var err error
if len(pcapFile) > 0 {
handle, err = pcap.OpenOffline(pcapFile)
if err != nil {
fmt.Printf("ERROR: %v\n", err)
return
}
} else {
handle, err = pcap.OpenLive(pcapDevice, 0, true, pcap.BlockForever)
if err != nil {
fmt.Printf("ERROR: %v\n", err)
return
}
err = handle.SetBPFFilter(pcapFilter)
if err != nil {
fmt.Printf("ERROR: %v\n", err)
return
}
}
defer handle.Close()
// Main loop..
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for {
packet, err := packetSource.NextPacket()
if err == io.EOF {
break
} else if err == nil {
handlePacket(handler, packet)
handler.AfterOnePacket()
}
}
}