ProtoCore v0.0.1
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
ip.cpp File Reference

pc_ip 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 pc_ip_parse (const char *s, pc_ip *out)
 Parse an IPv4 or IPv6 textual address (RFC 4291 §2.2) into out.
 
size_t pc_ip_format (const pc_ip *ip, char *out, size_t cap)
 Format ip into out as its RFC 5952 canonical text.
 
bool pc_ip_is_v4_mapped (const pc_ip *ip)
 True if ip is an IPv4-mapped IPv6 address (::ffff:a.b.c.d, RFC 4291 §2.5.5.2).
 
pc_ip_scope pc_ip_classify (const pc_ip *ip)
 Classify ip into a pc_ip_scope.
 
bool pc_ip_equal (const pc_ip *a, const pc_ip *b)
 True if a and b are the same family and address.
 
pc_ip pc_ip_from_v4_octets (uint8_t a, uint8_t b, uint8_t c, uint8_t d)
 Build a v4 pc_ip from four octets (a.b.c.d).
 
pc_ip pc_ip_from_v6_bytes (const uint8_t bytes[16])
 Build a v6 pc_ip from 16 address bytes in network (big-endian) order.
 
uint32_t pc_ip_to_v4_be (const pc_ip *ip)
 The v4 address as a big-endian (network-order) uint32 (a<<24 | b<<16 | c<<8 | d).
 
bool pc_ip_is_unspecified (const pc_ip *ip)
 True if ip is empty (pc_ip_family::PC_IP_NONE) or the all-zero unspecified address (0.0.0.0 / ::).
 
bool pc_ip_prefix_match (const pc_ip *addr, const pc_ip *net, uint8_t prefix_len)
 CIDR containment: is addr inside the net / prefix_len block?
 

Detailed Description

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

The public entry points (pc_ip_parse / pc_ip_format / pc_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

◆ pc_ip_parse()

bool pc_ip_parse ( const char *  s,
pc_ip out 
)

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

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

Definition at line 436 of file ip.cpp.

References pc_ip::bytes, pc_ip::family, PC_IP_V4, and PC_IP_V6.

Referenced by listener_ip_allow_add_cidr().

◆ pc_ip_format()

size_t pc_ip_format ( const pc_ip 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 PC_IP_STR_MAX).

Definition at line 488 of file ip.cpp.

References pc_ip::bytes, pc_ip::family, pc_ip_is_v4_mapped(), PC_IP_STR_MAX, PC_IP_V4, and PC_IP_V6.

◆ pc_ip_is_v4_mapped()

bool pc_ip_is_v4_mapped ( const pc_ip ip)

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

Definition at line 563 of file ip.cpp.

References pc_ip::bytes, pc_ip::family, and PC_IP_V6.

Referenced by pc_ip_format(), and pc_ip_to_v4_be().

◆ pc_ip_classify()

pc_ip_scope pc_ip_classify ( const pc_ip ip)

Classify ip into a pc_ip_scope.

Definition at line 568 of file ip.cpp.

References pc_ip::bytes, pc_ip::family, PC_IP_SCOPE_UNSPECIFIED, PC_IP_V4, and PC_IP_V6.

◆ pc_ip_equal()

bool pc_ip_equal ( const pc_ip a,
const pc_ip b 
)

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

Definition at line 585 of file ip.cpp.

References pc_ip::bytes, pc_ip::family, PC_IP_V4, and PC_IP_V6.

Referenced by listener_accept_allowed_ip().

◆ pc_ip_from_v4_octets()

pc_ip pc_ip_from_v4_octets ( uint8_t  a,
uint8_t  b,
uint8_t  c,
uint8_t  d 
)

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

Definition at line 607 of file ip.cpp.

References pc_ip::bytes, pc_ip::family, and PC_IP_V4.

Referenced by pc_lwip_to_ip().

◆ pc_ip_from_v6_bytes()

pc_ip pc_ip_from_v6_bytes ( const uint8_t  bytes[16])

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

Definition at line 619 of file ip.cpp.

References pc_ip::bytes, pc_ip::family, and PC_IP_V6.

Referenced by pc_lwip_to_ip().

◆ pc_ip_to_v4_be()

uint32_t pc_ip_to_v4_be ( const pc_ip 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 627 of file ip.cpp.

References pc_ip::bytes, pc_ip::family, pc_ip_is_v4_mapped(), and PC_IP_V4.

◆ pc_ip_is_unspecified()

bool pc_ip_is_unspecified ( const pc_ip ip)

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

Definition at line 645 of file ip.cpp.

References pc_ip::bytes, pc_ip::family, PC_IP_NONE, and PC_IP_V4.

Referenced by listener_accept_allowed_ip().

◆ pc_ip_prefix_match()

bool pc_ip_prefix_match ( const pc_ip addr,
const pc_ip 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 662 of file ip.cpp.

References pc_ip::bytes, pc_ip::family, PC_IP_V4, and PC_IP_V6.

Referenced by listener_ip_allowed().