Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "fix: same TCP connection appears twice" #633

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
64 changes: 39 additions & 25 deletions net_ip_socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,26 @@
package procfs

import (
"bufio"
"encoding/hex"
"fmt"
"io"
"net"
"os"
"strconv"
"strings"
)

const (
// readLimit is used by io.LimitReader while reading the content of the
// /proc/net/udp{,6} files. The number of lines inside such a file is dynamic
// as each line represents a single used socket.
// In theory, the number of available sockets is 65535 (2^16 - 1) per IP.
// With e.g. 150 Byte per line and the maximum number of 65535,
// the reader needs to handle 150 Byte * 65535 =~ 10 MB for a single IP.
readLimit = 4294967296 // Byte -> 4 GiB
)

// This contains generic data structures for both udp and tcp sockets.
type (
// NetIPSocket represents the contents of /proc/net/{t,u}dp{,6} file without the header.
Expand Down Expand Up @@ -62,50 +74,49 @@ type (
)

func newNetIPSocket(file string) (NetIPSocket, error) {
var netIPSocket NetIPSocket
isUDP := strings.Contains(file, "udp")
content, err := os.ReadFile(file)
f, err := os.Open(file)
if err != nil {
return nil, err
}
lines := strings.Split(string(content), "\n")
if len(lines) < 1 {
return nil, ErrFileParse
}
defer f.Close()

for _, line := range lines[1:] {
fields := strings.Fields(line)
if len(fields) == 0 {
continue
}
var netIPSocket NetIPSocket
isUDP := strings.Contains(file, "udp")

lr := io.LimitReader(f, readLimit)
s := bufio.NewScanner(lr)
s.Scan() // skip first line with headers
for s.Scan() {
fields := strings.Fields(s.Text())
line, err := parseNetIPSocketLine(fields, isUDP)
if err != nil {
return nil, err
}
netIPSocket = append(netIPSocket, line)
}
if err := s.Err(); err != nil {
return nil, err
}
return netIPSocket, nil
}

// newNetIPSocketSummary creates a new NetIPSocket{,6} from the contents of the given file.
func newNetIPSocketSummary(file string) (*NetIPSocketSummary, error) {
var netIPSocketSummary NetIPSocketSummary
var udpPacketDrops uint64
isUDP := strings.Contains(file, "udp")
content, err := os.ReadFile(file)
f, err := os.Open(file)
if err != nil {
return nil, err
}
lines := strings.Split(string(content), "\n")
if len(lines) < 1 {
return nil, ErrFileParse
}
defer f.Close()

for _, line := range lines[1:] {
fields := strings.Fields(line)
if len(fields) == 0 {
continue
}
var netIPSocketSummary NetIPSocketSummary
var udpPacketDrops uint64
isUDP := strings.Contains(file, "udp")

lr := io.LimitReader(f, readLimit)
s := bufio.NewScanner(lr)
s.Scan() // skip first line with headers
for s.Scan() {
fields := strings.Fields(s.Text())
line, err := parseNetIPSocketLine(fields, isUDP)
if err != nil {
return nil, err
Expand All @@ -118,6 +129,9 @@ func newNetIPSocketSummary(file string) (*NetIPSocketSummary, error) {
netIPSocketSummary.Drops = &udpPacketDrops
}
}
if err := s.Err(); err != nil {
return nil, err
}
return &netIPSocketSummary, nil
}

Expand Down