-
Notifications
You must be signed in to change notification settings - Fork 0
/
supersocket.go
162 lines (138 loc) · 4.26 KB
/
supersocket.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
package supersocket
import (
"fmt"
"log"
"sync"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
)
// SuperSocket structure contains the pointer to pcap.Handle and the interface to be used
type SuperSocket struct {
handle *pcap.Handle
iface string
}
// NewSuperSocket Given the interface and the filter opens the socket connection
func NewSuperSocket(device string, bpfFilter string) (*SuperSocket, error) {
// Open the device for capturing
handle, err := pcap.OpenLive(device, 1600, true, pcap.BlockForever)
if err != nil {
return nil, fmt.Errorf("failed to open device for capturing: %v", err)
}
// Apply the BPF filter
if bpfFilter != "" {
err = handle.SetBPFFilter(bpfFilter)
if err != nil {
handle.Close()
return nil, fmt.Errorf("failed to set BPF filter: %v", err)
}
}
return &SuperSocket{handle: handle,
iface: device}, nil
}
// Close closes the socket connection
func (ss *SuperSocket) Close() {
ss.handle.Close()
}
// Send sends the array of bytes through the supersocket
func (ss *SuperSocket) Send(packetBytes []byte) error {
return ss.handle.WritePacketData(packetBytes)
}
// Recv receives the bytes and transforms it into a gopacket.packet using the function NewPacket
func (ss *SuperSocket) Recv() (gopacket.Packet, error) {
data, _, err := ss.handle.ZeroCopyReadPacketData()
if err != nil {
return nil, fmt.Errorf("failed to read packet data: %v", err)
}
packet := gopacket.NewPacket(data, layers.LayerTypeEthernet, gopacket.Default)
return packet, nil
}
// SendMultiplePackets Given an array of bytes, sends them using goroutines.
// The maxConcurrentSends defines the maximum number of threads to be created
func (ss *SuperSocket) SendMultiplePackets(packets [][]byte, maxConcurrentSends int) error {
if maxConcurrentSends <= 0 {
maxConcurrentSends = len(packets)
}
var wg sync.WaitGroup
sem := make(chan struct{}, maxConcurrentSends)
for _, packet := range packets {
wg.Add(1)
sem <- struct{}{}
go func(p []byte) {
defer wg.Done()
defer func() { <-sem }()
err := ss.Send(p)
if err != nil {
fmt.Printf("Failed to send packet: %v\n", err)
}
}(packet)
}
wg.Wait()
return nil
}
// GetHandle returns the pointer to the pcap.Handle
func (ss *SuperSocket) GetHandle() *pcap.Handle {
return ss.handle
}
// Send this function sends the array of bytes. It doesn't need to be called on a supersocket
// structure. This functions creates the supersocket automatically, allowing to have a more
// abstract usage.
func Send(packetBytes []byte, iface string) {
superS, err := NewSuperSocket(iface, "")
if err != nil {
log.Fatal(err)
}
superS.Send(packetBytes)
superS.Close()
}
// Recv this function receives the array of bytes. It doesn't need to be called on a supersocket
// structure. This functions creates the supersocket automatically, allowing to have a more
// abstract usage.
func Recv(iface string) gopacket.Packet {
superS, err := NewSuperSocket(iface, "")
defer superS.Close()
if err != nil {
log.Fatal(err)
}
packet, err := superS.Recv()
return packet
}
// SendRecv this function Sends the array of bytes and receives its answer. It doesn't need to be called
// on a supersocket structure. This functions creates the supersocket automatically, allowing to have a more
// abstract usage.
func SendRecv(packetBytes []byte, iface string) gopacket.Packet {
superS, err := NewSuperSocket(iface, "")
if err != nil {
log.Fatal(err)
}
superS.Send(packetBytes)
packet, _ := superS.Recv()
return packet
}
// SendMultiplePackets sends multiple packets concurrently. It doesn't need to be called
// on a supersocket structure. This functions creates the supersocket automatically, allowing to have a more
// abstract usage.
func SendMultiplePackets(packets [][]byte, iface string, maxConcurrentSends int) {
ss, err := NewSuperSocket(iface, "")
if err != nil {
log.Fatal(err)
}
if maxConcurrentSends <= 0 {
maxConcurrentSends = len(packets)
}
var wg sync.WaitGroup
sem := make(chan struct{}, maxConcurrentSends)
for _, packet := range packets {
wg.Add(1)
sem <- struct{}{}
go func(p []byte) {
defer wg.Done()
defer func() { <-sem }()
err := ss.Send(p)
if err != nil {
fmt.Printf("Failed to send packet: %v\n", err)
}
}(packet)
}
wg.Wait()
}