Files
akvorado/inlet/flow/input/udp/socket_linux.go
Vincent Bernat 49b42f6055
Some checks failed
CI / 🤖 Check dependabot status (push) Has been cancelled
CI / 🐧 Test on Linux (${{ github.ref_type == 'tag' }}, misc) (push) Has been cancelled
CI / 🐧 Test on Linux (coverage) (push) Has been cancelled
CI / 🐧 Test on Linux (regular) (push) Has been cancelled
CI / ❄️ Build on Nix (push) Has been cancelled
CI / 🍏 Build and test on macOS (push) Has been cancelled
CI / 🧪 End-to-end testing (push) Has been cancelled
CI / 🔍 Upload code coverage (push) Has been cancelled
CI / 🔬 Test only Go (push) Has been cancelled
CI / 🔬 Test only JS (${{ needs.dependabot.outputs.package-ecosystem }}, 20) (push) Has been cancelled
CI / 🔬 Test only JS (${{ needs.dependabot.outputs.package-ecosystem }}, 22) (push) Has been cancelled
CI / 🔬 Test only JS (${{ needs.dependabot.outputs.package-ecosystem }}, 24) (push) Has been cancelled
CI / ⚖️ Check licenses (push) Has been cancelled
CI / 🐋 Build Docker images (push) Has been cancelled
CI / 🐋 Tag Docker images (push) Has been cancelled
CI / 🚀 Publish release (push) Has been cancelled
Update Nix dependency hashes / Update dependency hashes (push) Has been cancelled
Update Go toolchain / Update Go toolchain (push) Has been cancelled
Update Nix flake.lock / Update Nix lockfile (asn2org) (push) Has been cancelled
Update Nix flake.lock / Update Nix lockfile (iana-assignments) (push) Has been cancelled
Update Nix flake.lock / Update Nix lockfile (nixpkgs) (push) Has been cancelled
inlet/flow: avoid repeating common socket options for Linux and others
Also, fix the description for `SO_REUSEADDR`.
2025-11-02 15:16:18 +01:00

52 lines
1.4 KiB
Go

// SPDX-FileCopyrightText: 2022 Free Mobile
// SPDX-License-Identifier: AGPL-3.0-only
//go:build linux
package udp
import (
"syscall"
"time"
"unsafe"
"golang.org/x/sys/unix"
)
var (
oobLength = syscall.CmsgLen(4) + syscall.CmsgLen(16) // uint32 + 2*int64
udpSocketOptions = append(commonUDPSocketOptions, socketOption{
// Get the number of dropped packets
Name: "SO_RXQ_OVFL",
Level: unix.SOL_SOCKET,
Option: unix.SO_RXQ_OVFL,
}, socketOption{
// Ask the kernel to timestamp incoming packets
Name: "SO_TIMESTAMP_NEW",
Level: unix.SOL_SOCKET,
Option: unix.SO_TIMESTAMP_NEW | unix.SOF_TIMESTAMPING_RX_SOFTWARE,
})
)
// parseSocketControlMessage parses b and extract the number of drops
// returned (SO_RXQ_OVFL).
func parseSocketControlMessage(b []byte) (oobMessage, error) {
result := oobMessage{}
cmsgs, err := syscall.ParseSocketControlMessage(b)
if err != nil {
return result, err
}
for _, cmsg := range cmsgs {
// We know that cmsg.Data is correctly aligned for the data it contains, so we can cast it.
if cmsg.Header.Level == unix.SOL_SOCKET && cmsg.Header.Type == unix.SO_RXQ_OVFL {
result.Drops = *(*uint32)(unsafe.Pointer(&cmsg.Data[0]))
} else if cmsg.Header.Level == unix.SOL_SOCKET && cmsg.Header.Type == unix.SO_TIMESTAMP_NEW {
// We only are interested in the current second.
result.Received = time.Unix(*(*int64)(unsafe.Pointer(&cmsg.Data[0])), 0)
}
}
return result, nil
}