Skip to content

Commit

Permalink
Fix: macOS udp find process should use unspecified fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamacro committed Jul 15, 2022
1 parent 3b90e18 commit be8d63b
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions component/process/process_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ func findProcessName(network string, ip net.IP, port int) (string, error) {
// rup8(sizeof(xtcpcb_n))
itemSize += 208
}

var fallbackUDPProcess string
// skip the first xinpgen(24 bytes) block
for i := 24; i+itemSize <= len(buf); i += itemSize {
// offset of xinpcb_n and xsocket_n
Expand All @@ -57,25 +59,36 @@ func findProcessName(network string, ip net.IP, port int) (string, error) {
// xinpcb_n.inp_vflag
flag := buf[inp+44]

var srcIP net.IP
var (
srcIP net.IP
srcIsIPv4 bool
)
switch {
case flag&0x1 > 0 && isIPv4:
// ipv4
srcIP = net.IP(buf[inp+76 : inp+80])
srcIsIPv4 = true
case flag&0x2 > 0 && !isIPv4:
// ipv6
srcIP = net.IP(buf[inp+64 : inp+80])
default:
continue
}

if !ip.Equal(srcIP) {
continue
if ip.Equal(srcIP) {
// xsocket_n.so_last_pid
pid := readNativeUint32(buf[so+68 : so+72])
return getExecPathFromPID(pid)
}

// xsocket_n.so_last_pid
pid := readNativeUint32(buf[so+68 : so+72])
return getExecPathFromPID(pid)
// udp packet connection may be not equal with srcIP
if network == UDP && srcIP.IsUnspecified() && isIPv4 == srcIsIPv4 {
fallbackUDPProcess, _ = getExecPathFromPID(readNativeUint32(buf[so+68 : so+72]))
}
}

if network == UDP && fallbackUDPProcess != "" {
return fallbackUDPProcess, nil
}

return "", ErrNotFound
Expand Down

0 comments on commit be8d63b

Please sign in to comment.