2222#include <gr_net_compat.h>
2323#endif
2424
25+ // Address family enumeration.
2526typedef enum : uint8_t {
2627 GR_AF_UNSPEC = AF_UNSPEC ,
2728 GR_AF_IP4 = AF_INET ,
2829 GR_AF_IP6 = AF_INET6 ,
2930} addr_family_t ;
3031
32+ // Convert address family enum to string representation.
3133static inline const char * gr_af_name (addr_family_t af ) {
3234 switch (af ) {
3335 case GR_AF_UNSPEC :
@@ -40,7 +42,7 @@ static inline const char *gr_af_name(addr_family_t af) {
4042 return "?" ;
4143}
4244
43- // Custom printf specifiers
45+ // Custom printf specifiers for network addresses.
4446
4547// struct rte_ether_addr *
4648#define ETH_F "%2p"
@@ -61,20 +63,24 @@ static inline const char *gr_af_name(addr_family_t af) {
6163#define IPV4_RE "^" __IPV4_RE "$"
6264#define IPV4_NET_RE "^" __IPV4_RE __IPV4_PREFIX_RE "$"
6365
66+ // IPv4 address type (network byte order).
6467typedef uint32_t ip4_addr_t ;
6568
69+ // IPv4 network with prefix length.
6670struct ip4_net {
6771 ip4_addr_t ip ;
6872 uint8_t prefixlen ;
6973};
7074
75+ // Check if two IPv4 addresses are in the same subnet.
7176static inline bool ip4_addr_same_subnet (ip4_addr_t a , ip4_addr_t b , uint8_t prefixlen ) {
7277 ip4_addr_t mask = htonl (~(UINT32_MAX >> prefixlen ));
7378 return ((a ^ b ) & mask ) == 0 ;
7479}
7580
7681#define IPV4_ADDR_BCAST RTE_BE32(0xffffffff)
7782
83+ // Check if the provided IPv4 address is multicast.
7884static inline bool ip4_addr_is_mcast (const ip4_addr_t ip ) {
7985 const union {
8086 ip4_addr_t ip ;
@@ -83,6 +89,7 @@ static inline bool ip4_addr_is_mcast(const ip4_addr_t ip) {
8389 return addr .u8 [0 ] >= 224 && addr .u8 [0 ] <= 239 ;
8490}
8591
92+ // Parse IPv4 network string (e.g. "192.168.1.0/24") into ip4_net structure.
8693static inline int ip4_net_parse (const char * s , struct ip4_net * net , bool zero_mask ) {
8794 char * addr = NULL ;
8895 int ret = -1 ;
@@ -115,11 +122,13 @@ static inline int ip4_net_parse(const char *s, struct ip4_net *net, bool zero_ma
115122#define IPV6_RE "^" __IPV6_RE "$"
116123#define IPV6_NET_RE "^" __IPV6_RE __IPV6_PREFIX_RE "$"
117124
125+ // IPv6 network with prefix length.
118126struct ip6_net {
119127 struct rte_ipv6_addr ip ;
120128 uint8_t prefixlen ;
121129};
122130
131+ // Parse IPv6 network string (e.g. "2001:db8::/32") into ip6_net structure.
123132static inline int ip6_net_parse (const char * s , struct ip6_net * net , bool zero_mask ) {
124133 char * addr = NULL ;
125134 int ret = -1 ;
0 commit comments