fix #809 and minor bug in pool

This commit is contained in:
Urban Ishimwe
2020-08-30 12:07:00 +02:00
parent df6c4e8d02
commit f9b0ae6d55
5 changed files with 37 additions and 20 deletions
+2 -13
View File
@@ -1,9 +1,10 @@
// +build linux
package capture
import (
"fmt"
"net"
"sync"
"time"
"unsafe"
@@ -31,18 +32,6 @@ const (
var tpacket2hdrlen = tpAlign(int(unsafe.Sizeof(unix.Tpacket2Hdr{})))
// SockRaw is a linux M'maped af_packet socket
type SockRaw struct {
mu sync.Mutex
fd int
ifindex int
snaplen int
pollTimeout uintptr
frame uint32 // current frame
buf []byte // points to the memory space of the ring buffer shared with the kernel.
loopIndex int32 // this field must filled to avoid reading packet twice on a loopback device
}
// NewSockRaw returns new M'maped sock_raw on packet version 2.
func NewSockRaw(ifi net.Interface) (*SockRaw, error) {
// sock create
+13
View File
@@ -0,0 +1,13 @@
// +build !linux
package capture
import (
"errors"
"net"
)
// NewSockRaw returns new M'maped sock_raw on packet version 2.
func NewSockRaw(_ net.Interface) (*SockRaw, error) {
return nil, errors.New("afpacket socket is only available on linux")
}
+15
View File
@@ -0,0 +1,15 @@
package capture
import "sync"
// SockRaw is a linux M'maped af_packet socket
type SockRaw struct {
mu sync.Mutex
fd int
ifindex int
snaplen int
pollTimeout uintptr
frame uint32 // current frame
buf []byte // points to the memory space of the ring buffer shared with the kernel.
loopIndex int32 // this field must filled to avoid reading packet twice on a loopback device
}
-1
View File
@@ -19,7 +19,6 @@ import (
)
var (
mode string
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
memprofile = flag.String("memprofile", "", "write memory profile to this file")
)
+7 -6
View File
@@ -153,12 +153,14 @@ func (pool *MessagePool) Handler(packet gopacket.Packet) {
}
if pckt.RST {
if ok {
m.done <- true
<-m.done
}
if m, ok = pool.pool[pckt.Dst()]; !ok {
m, ok = pool.pool[pckt.Dst()+"="+srcKey]
}
if ok {
m.done <- true
<-m.done
}
go pool.say(4, fmt.Sprintf("RST flag from %s to %s at %s\n", pckt.Src(), pckt.Dst(), pckt.Timestamp))
@@ -168,13 +170,12 @@ func (pool *MessagePool) Handler(packet gopacket.Packet) {
case ok:
pool.addPacket(m, pckt)
return
case pool.Start != nil:
if in, out = pool.Start(pckt); in || out {
break
}
return
case pckt.SYN:
in = !pckt.ACK
case pool.Start != nil:
if in, out = pool.Start(pckt); !(in || out) {
return
}
default:
return
}
@@ -212,8 +213,8 @@ func (pool *MessagePool) addPacket(m *Message, pckt *Packet) {
m.add(pckt)
switch {
case trunc >= 0:
case pool.End != nil && pool.End(m):
case pckt.FIN:
case pool.End != nil && pool.End(m):
default:
return
}