Create interface for socket info structs, fix caching bug

From PR Review https://github.com/safing/portmaster/pull/72
This commit is contained in:
Daniel
2020-07-10 14:09:53 +02:00
parent 3d0e01383f
commit 7c6c4552aa
5 changed files with 54 additions and 52 deletions

View File

@@ -81,7 +81,7 @@ func (table *tcpTable) lookup(pktInfo *packet.Info) (
if localPort == socketInfo.Local.Port &&
(socketInfo.Local.IP[0] == 0 || localIP.Equal(socketInfo.Local.IP)) {
table.lock.RUnlock()
return checkBindPID(socketInfo, true)
return checkPID(socketInfo, true)
}
}
@@ -90,7 +90,7 @@ func (table *tcpTable) lookup(pktInfo *packet.Info) (
if localPort == socketInfo.Local.Port &&
localIP.Equal(socketInfo.Local.IP) {
table.lock.RUnlock()
return checkConnectionPID(socketInfo, false)
return checkPID(socketInfo, false)
}
}
@@ -138,12 +138,12 @@ func (table *udpTable) lookup(pktInfo *packet.Info) (
// do not check direction if remoteIP/Port is not given
if pktInfo.RemotePort() == 0 {
return checkBindPID(socketInfo, pktInfo.Inbound)
return checkPID(socketInfo, pktInfo.Inbound)
}
// get direction and return
connInbound := table.getDirection(socketInfo, pktInfo)
return checkBindPID(socketInfo, connInbound)
return checkPID(socketInfo, connInbound)
}
}

View File

@@ -14,30 +14,10 @@ var (
getUDP6Table = proc.GetUDP6Table
)
func checkConnectionPID(socketInfo *socket.ConnectionInfo, connInbound bool) (pid int, inbound bool, err error) {
func checkPID(socketInfo socket.Info, connInbound bool) (pid int, inbound bool, err error) {
for i := 0; i <= lookupRetries; i++ {
// look for PID
pid = proc.FindConnectionPID(socketInfo)
if pid != socket.UnidentifiedProcessID {
// if we found a PID, return
break
}
// every time, except for the last iteration
if i < lookupRetries {
// we found no PID, we could have been too fast, give the kernel some time to think
// back off timer: with 3ms baseWaitTime: 3, 6, 9, 12, 15, 18, 21ms - 84ms in total
time.Sleep(time.Duration(i+1) * baseWaitTime)
}
}
return pid, connInbound, nil
}
func checkBindPID(socketInfo *socket.BindInfo, connInbound bool) (pid int, inbound bool, err error) {
for i := 0; i <= lookupRetries; i++ {
// look for PID
pid = proc.FindBindPID(socketInfo)
pid = proc.GetPID(socketInfo)
if pid != socket.UnidentifiedProcessID {
// if we found a PID, return
break