Skip to content

Commit

Permalink
optimizing raw socket
Browse files Browse the repository at this point in the history
  • Loading branch information
Urban Ishimwe committed Jun 8, 2020
1 parent 9d71900 commit 312d9e8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
21 changes: 8 additions & 13 deletions capture/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ type Listener struct {
conn net.PacketConn
pcapHandles []*pcap.Handle

quit chan bool
readyCh bool
quit chan bool
ready bool
}

type request struct {
Expand Down Expand Up @@ -353,7 +353,6 @@ func (t *Listener) readPcap() {
} else {
inactive.SetSnapLen(65536)
}
inactive.SetSnapLen(65536)
inactive.SetTimeout(t.messageExpire)
inactive.SetPromisc(true)
inactive.SetImmediateMode(t.immediateMode)
Expand Down Expand Up @@ -581,7 +580,7 @@ func (t *Listener) readPcap() {

wg.Wait()
t.Lock()
t.readyCh = true
t.ready = true
t.Unlock()
}

Expand All @@ -597,7 +596,7 @@ func (t *Listener) readPcapFile() {
}

t.Lock()
t.readyCh = true
t.ready = true
t.Unlock()
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())

Expand Down Expand Up @@ -667,19 +666,15 @@ func (t *Listener) readRAWSocket() {
}
var bufChan = make(chan *RSPacket, 1000)
t.Lock()
t.readyCh = true
t.ready = true
t.Unlock()
go func() {
buffer := 64 * 1024
if t.bufferSize > int64(buffer) {
buffer = int(t.bufferSize)
}
for {
// Re-allocate data object to avoid data collision
buf := make([]byte, buffer)
var buf [64 * 104 * 1024]byte
// Note: ReadFrom receive messages without IP header
n, addr, err := t.conn.ReadFrom(buf)
bufChan <- &RSPacket{buf, addr, err, n}
n, addr, err := t.conn.ReadFrom(buf[:])
bufChan <- &RSPacket{buf[:], addr, err, n}
}
}()
for {
Expand Down
2 changes: 1 addition & 1 deletion capture/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func TestRawListenerInput(t *testing.T) {
var req, resp *TCPMessage

listener := NewListener("", "0", EnginePcapFile, true, 10*time.Millisecond, "", "", 0, false, false)
listener := NewListener("", "0", EnginePcap, true, 10*time.Millisecond, "", "", 0, false, false)
defer listener.Close()

reqPacket := buildPacket(true, 1, 1, []byte("GET / HTTP/1.1\r\n\r\n"), time.Now())
Expand Down

0 comments on commit 312d9e8

Please sign in to comment.