Switch Exists function of network state pkg to use packet.Info

This commit is contained in:
Daniel 2020-05-19 09:22:50 +02:00
parent d11080d997
commit ad93b19968
2 changed files with 35 additions and 36 deletions

View file

@ -4,6 +4,8 @@ import (
"context" "context"
"time" "time"
"github.com/safing/portmaster/network/packet"
"github.com/safing/portmaster/network/state" "github.com/safing/portmaster/network/state"
"github.com/safing/portbase/log" "github.com/safing/portbase/log"
@ -42,7 +44,7 @@ func cleanConnections() (activePIDs map[int]struct{}) {
now := time.Now().UTC() now := time.Now().UTC()
nowUnix := now.Unix() nowUnix := now.Unix()
deleteOlderThan := time.Now().Add(-deleteConnsAfterEndedThreshold).Unix() deleteOlderThan := now.Add(-deleteConnsAfterEndedThreshold).Unix()
// lock both together because we cannot fully guarantee in which map a connection lands // lock both together because we cannot fully guarantee in which map a connection lands
// of course every connection should land in the correct map, but this increases resilience // of course every connection should land in the correct map, but this increases resilience
@ -59,12 +61,19 @@ func cleanConnections() (activePIDs map[int]struct{}) {
switch { switch {
case conn.Ended == 0: case conn.Ended == 0:
// Step 1: check if still active // Step 1: check if still active
exists := state.Exists(conn.IPVersion, conn.IPProtocol, conn.LocalIP, conn.LocalPort, conn.Entity.IP, conn.Entity.Port, now) exists := state.Exists(&packet.Info{
if exists { Inbound: false, // src == local
activePIDs[conn.process.Pid] = struct{}{} Version: conn.IPVersion,
} else { Protocol: conn.IPProtocol,
Src: conn.LocalIP,
SrcPort: conn.LocalPort,
Dst: conn.Entity.IP,
DstPort: conn.Entity.Port,
}, now)
activePIDs[conn.process.Pid] = struct{}{}
if !exists {
// Step 2: mark end // Step 2: mark end
activePIDs[conn.process.Pid] = struct{}{}
conn.Ended = nowUnix conn.Ended = nowUnix
conn.Save() conn.Save()
} }

View file

@ -1,7 +1,6 @@
package state package state
import ( import (
"net"
"time" "time"
"github.com/safing/portmaster/network/packet" "github.com/safing/portmaster/network/packet"
@ -12,49 +11,38 @@ const (
UDPConnectionTTL = 10 * time.Minute UDPConnectionTTL = 10 * time.Minute
) )
func Exists( func Exists(pktInfo *packet.Info, now time.Time) (exists bool) {
ipVersion packet.IPVersion,
protocol packet.IPProtocol,
localIP net.IP,
localPort uint16,
remoteIP net.IP,
remotePort uint16,
now time.Time,
) (exists bool) {
switch { switch {
case ipVersion == packet.IPv4 && protocol == packet.TCP: case pktInfo.Version == packet.IPv4 && pktInfo.Protocol == packet.TCP:
tcp4Lock.Lock() tcp4Lock.Lock()
defer tcp4Lock.Unlock() defer tcp4Lock.Unlock()
return existsTCP(tcp4Connections, localIP, localPort, remoteIP, remotePort) return existsTCP(tcp4Connections, pktInfo)
case ipVersion == packet.IPv6 && protocol == packet.TCP: case pktInfo.Version == packet.IPv6 && pktInfo.Protocol == packet.TCP:
tcp6Lock.Lock() tcp6Lock.Lock()
defer tcp6Lock.Unlock() defer tcp6Lock.Unlock()
return existsTCP(tcp6Connections, localIP, localPort, remoteIP, remotePort) return existsTCP(tcp6Connections, pktInfo)
case ipVersion == packet.IPv4 && protocol == packet.UDP: case pktInfo.Version == packet.IPv4 && pktInfo.Protocol == packet.UDP:
udp4Lock.Lock() udp4Lock.Lock()
defer udp4Lock.Unlock() defer udp4Lock.Unlock()
return existsUDP(udp4Binds, udp4States, localIP, localPort, remoteIP, remotePort, now) return existsUDP(udp4Binds, udp4States, pktInfo, now)
case ipVersion == packet.IPv6 && protocol == packet.UDP: case pktInfo.Version == packet.IPv6 && pktInfo.Protocol == packet.UDP:
udp6Lock.Lock() udp6Lock.Lock()
defer udp6Lock.Unlock() defer udp6Lock.Unlock()
return existsUDP(udp6Binds, udp6States, localIP, localPort, remoteIP, remotePort, now) return existsUDP(udp6Binds, udp6States, pktInfo, now)
default: default:
return false return false
} }
} }
func existsTCP( func existsTCP(connections []*socket.ConnectionInfo, pktInfo *packet.Info) (exists bool) {
connections []*socket.ConnectionInfo, localIP := pktInfo.LocalIP()
localIP net.IP, localPort := pktInfo.LocalPort()
localPort uint16, remoteIP := pktInfo.RemoteIP()
remoteIP net.IP, remotePort := pktInfo.RemotePort()
remotePort uint16,
) (exists bool) {
// search connections // search connections
for _, socketInfo := range connections { for _, socketInfo := range connections {
@ -72,13 +60,15 @@ func existsTCP(
func existsUDP( func existsUDP(
binds []*socket.BindInfo, binds []*socket.BindInfo,
udpStates map[string]map[string]*udpState, udpStates map[string]map[string]*udpState,
localIP net.IP, pktInfo *packet.Info,
localPort uint16,
remoteIP net.IP,
remotePort uint16,
now time.Time, now time.Time,
) (exists bool) { ) (exists bool) {
localIP := pktInfo.LocalIP()
localPort := pktInfo.LocalPort()
remoteIP := pktInfo.RemoteIP()
remotePort := pktInfo.RemotePort()
connThreshhold := now.Add(-UDPConnectionTTL) connThreshhold := now.Add(-UDPConnectionTTL)
// search binds // search binds