DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
ip.cpp File Reference

DetIp implementation: RFC 4291 text parsing, RFC 5952 canonical formatting, scope classification. Pure, hand-rolled (no stdlib parsing), host-identical. More...

#include "network_drivers/network/ip.h"
#include <string.h>

Go to the source code of this file.

Functions

bool det_ip_parse (const char *s, DetIp *out)
 Parse an IPv4 or IPv6 textual address (RFC 4291 §2.2) into out.
 
size_t det_ip_format (const DetIp *ip, char *out, size_t cap)
 Format ip into out as its RFC 5952 canonical text.
 
bool det_ip_is_v4_mapped (const DetIp *ip)
 True if ip is an IPv4-mapped IPv6 address (::ffff:a.b.c.d, RFC 4291 §2.5.5.2).
 
DetIpScope det_ip_classify (const DetIp *ip)
 Classify ip into a DetIpScope.
 
bool det_ip_equal (const DetIp *a, const DetIp *b)
 True if a and b are the same family and address.
 
DetIp det_ip_from_v4_octets (uint8_t a, uint8_t b, uint8_t c, uint8_t d)
 Build a v4 DetIp from four octets (a.b.c.d).
 
DetIp det_ip_from_v6_bytes (const uint8_t bytes[16])
 Build a v6 DetIp from 16 address bytes in network (big-endian) order.
 
uint32_t det_ip_to_v4_be (const DetIp *ip)
 The v4 address as a big-endian (network-order) uint32 (a<<24 | b<<16 | c<<8 | d).
 
bool det_ip_is_unspecified (const DetIp *ip)
 True if ip is empty (DetIpFamily::DET_IP_NONE) or the all-zero unspecified address (0.0.0.0 / ::).
 
bool det_ip_prefix_match (const DetIp *addr, const DetIp *net, uint8_t prefix_len)
 CIDR containment: is addr inside the net / prefix_len block?
 

Detailed Description

DetIp implementation: RFC 4291 text parsing, RFC 5952 canonical formatting, scope classification. Pure, hand-rolled (no stdlib parsing), host-identical.

The public entry points (det_ip_parse / det_ip_format / det_ip_classify) are thin: the work is split into small single-purpose helpers in the anonymous namespace below - one per concern (parse a hextet, assemble the 16 bytes, find the zero run to compress, classify one family).

Definition in file ip.cpp.

Function Documentation

◆ det_ip_parse()

bool det_ip_parse ( const char *  s,
DetIp out 
)

Parse an IPv4 or IPv6 textual address (RFC 4291 §2.2) into out.

Returns
true on success (out->family set to DetIpFamily::DET_IP_V4/V6), false if s is malformed.

Definition at line 350 of file ip.cpp.

References DetIp::bytes, DET_IP_V4, DET_IP_V6, and DetIp::family.

Referenced by listener_ip_allow_add_cidr().

◆ det_ip_format()

size_t det_ip_format ( const DetIp ip,
char *  out,
size_t  cap 
)

Format ip into out as its RFC 5952 canonical text.

Returns
the length written (excluding the NUL), or 0 if ip is empty or cap is too small (need up to DET_IP_STR_MAX).

Definition at line 388 of file ip.cpp.

References DetIp::bytes, det_ip_is_v4_mapped(), DET_IP_STR_MAX, DET_IP_V4, DET_IP_V6, and DetIp::family.

◆ det_ip_is_v4_mapped()

bool det_ip_is_v4_mapped ( const DetIp ip)

True if ip is an IPv4-mapped IPv6 address (::ffff:a.b.c.d, RFC 4291 §2.5.5.2).

Definition at line 443 of file ip.cpp.

References DetIp::bytes, DET_IP_V6, and DetIp::family.

Referenced by det_ip_format(), and det_ip_to_v4_be().

◆ det_ip_classify()

DetIpScope det_ip_classify ( const DetIp ip)

Classify ip into a DetIpScope.

Definition at line 448 of file ip.cpp.

References DetIp::bytes, DET_IP_SCOPE_UNSPECIFIED, DET_IP_V4, DET_IP_V6, and DetIp::family.

◆ det_ip_equal()

bool det_ip_equal ( const DetIp a,
const DetIp b 
)

True if a and b are the same family and address.

Definition at line 459 of file ip.cpp.

References DetIp::bytes, DET_IP_V4, DET_IP_V6, and DetIp::family.

Referenced by listener_accept_allowed_ip().

◆ det_ip_from_v4_octets()

DetIp det_ip_from_v4_octets ( uint8_t  a,
uint8_t  b,
uint8_t  c,
uint8_t  d 
)

Build a v4 DetIp from four octets (a.b.c.d).

Definition at line 473 of file ip.cpp.

References DetIp::bytes, DET_IP_V4, and DetIp::family.

Referenced by det_lwip_to_detip().

◆ det_ip_from_v6_bytes()

DetIp det_ip_from_v6_bytes ( const uint8_t  bytes[16])

Build a v6 DetIp from 16 address bytes in network (big-endian) order.

Definition at line 485 of file ip.cpp.

References DetIp::bytes, DET_IP_V6, and DetIp::family.

Referenced by det_lwip_to_detip().

◆ det_ip_to_v4_be()

uint32_t det_ip_to_v4_be ( const DetIp ip)

The v4 address as a big-endian (network-order) uint32 (a<<24 | b<<16 | c<<8 | d).

Returns
0 if ip is not a v4 (or v4-mapped) address.

Definition at line 493 of file ip.cpp.

References DetIp::bytes, det_ip_is_v4_mapped(), DET_IP_V4, and DetIp::family.

◆ det_ip_is_unspecified()

bool det_ip_is_unspecified ( const DetIp ip)

True if ip is empty (DetIpFamily::DET_IP_NONE) or the all-zero unspecified address (0.0.0.0 / ::).

Definition at line 505 of file ip.cpp.

References DetIp::bytes, DET_IP_NONE, DET_IP_V4, and DetIp::family.

Referenced by listener_accept_allowed_ip().

◆ det_ip_prefix_match()

bool det_ip_prefix_match ( const DetIp addr,
const DetIp net,
uint8_t  prefix_len 
)

CIDR containment: is addr inside the net / prefix_len block?

The two must be the same family. prefix_len is 0..32 for v4, 0..128 for v6; the top prefix_len bits of the address bytes must match net (a prefix of 0 matches everything). This is the standard v4/v6 allowlist match.

Returns
true if addr is covered; false on a family mismatch or an out-of-range prefix.

Definition at line 516 of file ip.cpp.

References DetIp::bytes, DET_IP_V4, DET_IP_V6, and DetIp::family.

Referenced by listener_ip_allowed().