Files
akvorado/inlet/flow/input/udp/socket.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

74 lines
1.6 KiB
Go

// SPDX-FileCopyrightText: 2022 Free Mobile
// SPDX-License-Identifier: AGPL-3.0-only
package udp
import (
"fmt"
"net"
"syscall"
"time"
"akvorado/common/reporter"
"golang.org/x/sys/unix"
)
// commonUDPSocketOptions are the options common to all OS.
var commonUDPSocketOptions = []socketOption{
{
// Allow a listener to bind again to a socket that was just closed
Name: "SO_REUSEADDR",
Level: unix.SOL_SOCKET,
Option: unix.SO_REUSEADDR,
Mandatory: true,
}, {
// Allow multiple listeners to bind to the same port
Name: "SO_REUSEPORT",
Level: unix.SOL_SOCKET,
Option: unix.SO_REUSEPORT,
Mandatory: true,
},
}
type oobMessage struct {
Drops uint32
Received time.Time
}
// socketOption describes a socket option to be applied.
type socketOption struct {
Name string
Level int
Option int
Mandatory bool
}
// listenConfig configures a listening socket with udpSocketOptions
var listenConfig = func(r *reporter.Reporter, opts []socketOption, fds *[]uintptr) *net.ListenConfig {
return &net.ListenConfig{
Control: func(_, _ string, c syscall.RawConn) error {
var err error
for _, opt := range opts {
c.Control(func(fd uintptr) {
err = unix.SetsockoptInt(int(fd), opt.Level, opt.Option, 1)
})
if err != nil {
if opt.Mandatory {
return fmt.Errorf("cannot set option %s: %w", opt.Name, err)
}
r.Warn().Err(err).Msgf("cannot set option %s", opt.Name)
}
}
if fds != nil {
c.Control(func(fd uintptr) {
*fds = append(*fds, fd)
})
}
return nil
},
}
}