tools/testing/selftests/net/reuseport_addr_any.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/net/reuseport_addr_any.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/net/reuseport_addr_any.c- Extension
.c- Size
- 6372 bytes
- Lines
- 245
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
arpa/inet.herrno.herror.hlinux/in.hlinux/unistd.hstdbool.hstdio.hstdlib.hstring.hsys/epoll.hsys/types.hsys/socket.hunistd.h
Detected Declarations
function build_rcv_fdfunction connect_and_sendfunction receive_oncefunction testfunction run_one_testfunction test_protofunction main
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
/* Test that sockets listening on a specific address are preferred
* over sockets listening on addr_any.
*/
#define _GNU_SOURCE
#include <arpa/inet.h>
#include <errno.h>
#include <error.h>
#include <linux/in.h>
#include <linux/unistd.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
static const char *IP4_ADDR = "127.0.0.1";
static const char *IP6_ADDR = "::1";
static const char *IP4_MAPPED6 = "::ffff:127.0.0.1";
static const int PORT = 8888;
static void build_rcv_fd(int family, int proto, int *rcv_fds, int count,
const char *addr_str)
{
struct sockaddr_in addr4 = {0};
struct sockaddr_in6 addr6 = {0};
struct sockaddr *addr;
int opt, i, sz;
memset(&addr, 0, sizeof(addr));
switch (family) {
case AF_INET:
addr4.sin_family = family;
if (!addr_str)
addr4.sin_addr.s_addr = htonl(INADDR_ANY);
else if (!inet_pton(family, addr_str, &addr4.sin_addr.s_addr))
error(1, errno, "inet_pton failed: %s", addr_str);
addr4.sin_port = htons(PORT);
sz = sizeof(addr4);
addr = (struct sockaddr *)&addr4;
break;
case AF_INET6:
addr6.sin6_family = AF_INET6;
if (!addr_str)
addr6.sin6_addr = in6addr_any;
else if (!inet_pton(family, addr_str, &addr6.sin6_addr))
error(1, errno, "inet_pton failed: %s", addr_str);
addr6.sin6_port = htons(PORT);
sz = sizeof(addr6);
addr = (struct sockaddr *)&addr6;
break;
default:
error(1, 0, "Unsupported family %d", family);
/* clang does not recognize error() above as terminating
* the program, so it complains that saddr, sz are
* not initialized when this code path is taken. Silence it.
*/
return;
}
for (i = 0; i < count; ++i) {
rcv_fds[i] = socket(family, proto, 0);
if (rcv_fds[i] < 0)
error(1, errno, "failed to create receive socket");
opt = 1;
if (setsockopt(rcv_fds[i], SOL_SOCKET, SO_REUSEPORT, &opt,
sizeof(opt)))
error(1, errno, "failed to set SO_REUSEPORT");
if (bind(rcv_fds[i], addr, sz))
error(1, errno, "failed to bind receive socket");
if (proto == SOCK_STREAM && listen(rcv_fds[i], 10))
error(1, errno, "tcp: failed to listen on receive port");
}
}
static int connect_and_send(int family, int proto)
{
struct sockaddr_in saddr4 = {0};
struct sockaddr_in daddr4 = {0};
Annotation
- Immediate include surface: `arpa/inet.h`, `errno.h`, `error.h`, `linux/in.h`, `linux/unistd.h`, `stdbool.h`, `stdio.h`, `stdlib.h`.
- Detected declarations: `function build_rcv_fd`, `function connect_and_send`, `function receive_once`, `function test`, `function run_one_test`, `function test_proto`, `function main`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.