tools/testing/selftests/net/ip_local_port_range.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/net/ip_local_port_range.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/net/ip_local_port_range.c- Extension
.c- Size
- 11961 bytes
- Lines
- 464
- 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
fcntl.hnetinet/ip.hkselftest_harness.h
Detected Declarations
function pack_port_rangefunction unpack_port_rangefunction get_so_domainfunction bind_to_loopback_any_portfunction get_sock_portfunction get_ip_local_port_range
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
// Copyright (c) 2023 Cloudflare
/* Test IP_LOCAL_PORT_RANGE socket option: IPv4 + IPv6, TCP + UDP.
*
* Tests assume that net.ipv4.ip_local_port_range is [40000, 49999].
* Don't run these directly but with ip_local_port_range.sh script.
*/
#include <fcntl.h>
#include <netinet/ip.h>
#include "kselftest_harness.h"
#ifndef IP_LOCAL_PORT_RANGE
#define IP_LOCAL_PORT_RANGE 51
#endif
#ifndef IPPROTO_MPTCP
#define IPPROTO_MPTCP 262
#endif
static __u32 pack_port_range(__u16 lo, __u16 hi)
{
return (hi << 16) | (lo << 0);
}
static void unpack_port_range(__u32 range, __u16 *lo, __u16 *hi)
{
*lo = range & 0xffff;
*hi = range >> 16;
}
static int get_so_domain(int fd)
{
int domain, err;
socklen_t len;
len = sizeof(domain);
err = getsockopt(fd, SOL_SOCKET, SO_DOMAIN, &domain, &len);
if (err)
return -1;
return domain;
}
static int bind_to_loopback_any_port(int fd)
{
union {
struct sockaddr sa;
struct sockaddr_in v4;
struct sockaddr_in6 v6;
} addr;
socklen_t addr_len;
memset(&addr, 0, sizeof(addr));
switch (get_so_domain(fd)) {
case AF_INET:
addr.v4.sin_family = AF_INET;
addr.v4.sin_port = htons(0);
addr.v4.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
addr_len = sizeof(addr.v4);
break;
case AF_INET6:
addr.v6.sin6_family = AF_INET6;
addr.v6.sin6_port = htons(0);
addr.v6.sin6_addr = in6addr_loopback;
addr_len = sizeof(addr.v6);
break;
default:
return -1;
}
return bind(fd, &addr.sa, addr_len);
}
static int get_sock_port(int fd)
{
union {
struct sockaddr sa;
struct sockaddr_in v4;
struct sockaddr_in6 v6;
} addr;
socklen_t addr_len;
int err;
addr_len = sizeof(addr);
memset(&addr, 0, sizeof(addr));
err = getsockname(fd, &addr.sa, &addr_len);
if (err)
Annotation
- Immediate include surface: `fcntl.h`, `netinet/ip.h`, `kselftest_harness.h`.
- Detected declarations: `function pack_port_range`, `function unpack_port_range`, `function get_so_domain`, `function bind_to_loopback_any_port`, `function get_sock_port`, `function get_ip_local_port_range`.
- 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.