Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added semaphore for exit file #289

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package config

import (
"sync"

"github.com/negbie/logp"
)

var Cfg Config

var WgExitGroup sync.WaitGroup

type Config struct {
Iface *InterfacesConfig
Logging *logp.Logging
Expand Down
11 changes: 11 additions & 0 deletions decoder/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,17 @@ func (d *Decoder) SendPingHEPPacket() {
PacketQueue <- pkt
}

func (d *Decoder) SendExitHEPPacket() {

var data = []byte{0x48, 0x45, 0x50, 0x33, 0x3, 0xa}
pkt := &Packet{
Version: 255,
Payload: data,
}

PacketQueue <- pkt
}

func stb(s string) []byte {
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
var res []byte
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/sipcapture/heplify/sniffer"
)

const version = "heplify 1.66.2"
const version = "heplify 1.66.3"

func createFlags() {

Expand Down
9 changes: 9 additions & 0 deletions publish/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ func (pub *Publisher) Start(pq chan *decoder.Packet) {
}
pub.setHEPPing(msg)
logp.Debug("publisher", "sent hep ping from collector")
} else if pkt.Version == 255 {
//this is EXIT
logp.Info("received exit signal")
if config.Cfg.Iface.EOFExit {
logp.Info("exiting...")
config.WgExitGroup.Done()
return
}
break
} else {

if scriptEnable {
Expand Down
15 changes: 15 additions & 0 deletions sniffer/sniffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type Worker interface {
OnPacket(data []byte, ci *gopacket.CaptureInfo)
OnHEPPacket(data []byte)
SendPingHEPPacket()
SendExitHEPPacket()
}

type stats struct {
Expand Down Expand Up @@ -106,6 +107,10 @@ func (mw *MainWorker) SendPingHEPPacket() {
mw.decoder.SendPingHEPPacket()
}

func (mw *MainWorker) SendExitHEPPacket() {
mw.decoder.SendExitHEPPacket()
}

func (sniffer *SnifferSetup) setFromConfig() error {
var err error

Expand Down Expand Up @@ -334,6 +339,11 @@ func (sniffer *SnifferSetup) SendPing() error {
return nil
}

func (sniffer *SnifferSetup) SendExitPacket() error {
sniffer.worker.SendExitHEPPacket()
return nil
}

func (sniffer *SnifferSetup) Run() error {
var (
loopCount = 1
Expand Down Expand Up @@ -462,6 +472,11 @@ LOOP:
logp.Debug("sniffer", "End of file")

if sniffer.config.EOFExit {
logp.Info("EOFExit enabled.Prepare exit...")
sniffer.worker.SendExitHEPPacket()
config.WgExitGroup.Add(1)
config.WgExitGroup.Wait()
logp.Info("Sent all packets, exiting...")
sniffer.Close()
os.Exit(0)
}
Expand Down
Loading