Skip to content

Commit

Permalink
minor improvement/refactoring
Browse files Browse the repository at this point in the history
Move queues setup to its own func.
Don't declare some dns vars inside for loops.
  • Loading branch information
gustavo-iniguez-goya committed Jan 18, 2024
1 parent 164696f commit 379d010
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 26 deletions.
2 changes: 1 addition & 1 deletion daemon/dns/ebpfhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func spawnDNSWorker(id int, channel chan []byte, exitChannel chan bool) {

log.Debug("dns worker initialized #%d", id)
var event nameLookupEvent
var ip net.IP
for {
select {

Expand All @@ -188,7 +189,6 @@ func spawnDNSWorker(id int, channel chan []byte, exitChannel chan bool) {
}
// Convert C string (null-terminated) to Go string
host := string(event.Host[:bytes.IndexByte(event.Host[:], 0)])
var ip net.IP
// 2 -> AF_INET (ipv4)
if event.AddrType == 2 {
ip = net.IP(event.IP[:4])
Expand Down
57 changes: 32 additions & 25 deletions daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,30 @@ func overwriteLogging() bool {
return debug || warning || important || errorlog || logFile != "" || logMicro
}

func setupQueues() {
// prepare the queue
var err error
queue, err = netfilter.NewQueue(uint16(queueNum))
if err != nil {
msg := fmt.Sprintf("Error creating queue #%d: %s", queueNum, err)
uiClient.SendWarningAlert(msg)
log.Warning("Is opensnitchd already running?")
log.Fatal(msg)
}
pktChan = queue.Packets()

repeatQueueNum = queueNum + 1

repeatQueue, err = netfilter.NewQueue(uint16(repeatQueueNum))
if err != nil {
msg := fmt.Sprintf("Error creating repeat queue #%d: %s", repeatQueueNum, err)
uiClient.SendErrorAlert(msg)
log.Warning("Is opensnitchd already running?")
log.Warning(msg)
}
repeatPktChan = repeatQueue.Packets()
}

func setupLogging() {
golog.SetOutput(ioutil.Discard)
if debug {
Expand Down Expand Up @@ -290,6 +314,7 @@ func initSystemdResolvedMonitor() {
return
}
go func() {
var ip net.IP
for {
select {
case exit := <-resolvMonitor.Exit():
Expand All @@ -313,14 +338,13 @@ func initSystemdResolvedMonitor() {
log.Debug("systemd-resolved, excluding answer: %#v", a)
continue
}
domain := a.RR.Key.Name
ip := net.IP(a.RR.Address)
log.Debug("%d systemd-resolved monitor response: %s -> %s", i, domain, ip)
ip = net.IP(a.RR.Address)
log.Debug("%d systemd-resolved monitor response: %s -> %s", i, a.RR.Key.Name, a.RR.Address)
if a.RR.Key.Type == systemd.DNSTypeCNAME {
log.Debug("systemd-resolved CNAME >> %s -> %s", a.RR.Name, domain)
dns.Track(a.RR.Name, domain)
log.Debug("systemd-resolved CNAME >> %s -> %s", a.RR.Name, a.RR.Key.Name)
dns.Track(a.RR.Name, a.RR.Key.Name /*domain*/)
} else {
dns.Track(ip.String(), domain)
dns.Track(ip.String(), a.RR.Key.Name /*domain*/)
}
}
}
Expand Down Expand Up @@ -536,6 +560,7 @@ func main() {
if err != nil {
log.Fatal("%s", err)
}

if err == nil && cfg.Rules.Path != "" {
rulesPath = cfg.Rules.Path
}
Expand All @@ -561,26 +586,8 @@ func main() {
loggerMgr = loggers.NewLoggerManager()
uiClient = ui.NewClient(uiSocket, configFile, stats, rules, loggerMgr)

// prepare the queue
setupWorkers()
queue, err := netfilter.NewQueue(uint16(queueNum))
if err != nil {
msg := fmt.Sprintf("Error creating queue #%d: %s", queueNum, err)
uiClient.SendWarningAlert(msg)
log.Warning("Is opensnitchd already running?")
log.Fatal(msg)
}
pktChan = queue.Packets()

repeatQueueNum = queueNum + 1
repeatQueue, rqerr := netfilter.NewQueue(uint16(repeatQueueNum))
if rqerr != nil {
msg := fmt.Sprintf("Error creating repeat queue #%d: %s", repeatQueueNum, rqerr)
uiClient.SendErrorAlert(msg)
log.Warning("Is opensnitchd already running?")
log.Warning(msg)
}
repeatPktChan = repeatQueue.Packets()
setupQueues()

fwConfigPath := fwConfigFile
if fwConfigPath == "" {
Expand Down

0 comments on commit 379d010

Please sign in to comment.