Skip to content

Commit

Permalink
🐛 handle case where ipv4 or ipv6 is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-rock committed Feb 24, 2024
1 parent faf8ca0 commit c84b9a0
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions providers/os/resources/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"io"
"net/netip"
"os"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -242,13 +243,16 @@ func (p *mqlPorts) processesBySocket() (map[int64]*mqlProcess, error) {
return processes.BySocketID, err
}

// See:
// - socket/address parsing: https://wiki.christophchamp.com/index.php?title=Unix_sockets
// parseProcNet parses the proc filesystem
// See socket/address parsing: https://wiki.christophchamp.com/index.php?title=Unix_sockets
func (p *mqlPorts) parseProcNet(path string, protocol string, users map[int64]*mqlUser) ([]interface{}, error) {
conn := p.MqlRuntime.Connection.(shared.Connection)
fs := conn.FileSystem()
stat, err := fs.Stat(path)
if err != nil {
// if the file does not exist, we just return nil since no ports are open then
if errors.Is(err, os.ErrNotExist) {
return nil, nil
} else if err != nil {
return nil, errors.New("cannot access stat for " + path)
}
if stat.IsDir() {
Expand Down Expand Up @@ -379,6 +383,8 @@ func (p *mqlPorts) listLinux() ([]interface{}, error) {
return nil, err
}

// check if kernel supports /proc/net/tcp4

var ports []interface{}
tcpPorts, err := p.parseProcNet("/proc/net/tcp", "tcp4", users)
if err != nil {
Expand All @@ -392,6 +398,8 @@ func (p *mqlPorts) listLinux() ([]interface{}, error) {
}
ports = append(ports, udpPorts...)

// check if kernel supports /proc/net/tcp6

tcpPortsV6, err := p.parseProcNet("/proc/net/tcp6", "tcp6", users)
if err != nil {
return nil, err
Expand Down

0 comments on commit c84b9a0

Please sign in to comment.