13 #include <immintrin.h>
15 #include <bsd/string.h>
16 #include <net/ethernet.h>
22 #error "Do not include this file directly use cne_inet.h instead."
31 inet_addr_cmp(
struct in_addr *c1,
struct in_addr *c2)
33 return c1->s_addr == c2->s_addr;
38 inet_addr_copy(
struct in_addr *t,
struct in_addr *f)
40 t->s_addr = f->s_addr;
45 inet_addr_swap(uint32_t *t, uint32_t *f)
56 __mask_size(uint32_t mask)
60 else if (mask == 0xFF000000)
62 else if (mask == 0xFFFF0000)
64 else if (mask == 0xFFFFFF00)
66 else if (mask == 0xFFFFFFFF)
70 for (i = 0; i < 32; i++)
71 if ((mask & (1 << (31 - i))) == 0)
78 static __inline__ uint32_t
79 __size_to_mask(
int len)
99 for (i = 0; i < len; i++)
100 mask |= (1 << (31 - i));
108 inet_ntop4(
char *buff,
int len,
struct in_addr *ip_addr,
struct in_addr *mask)
111 char b[IP4_ADDR_STRLEN] = {0};
113 if (!buff || len < IP4_ADDR_STRLEN)
116 memset(buff, 0, len);
118 if (inet_ntop(AF_INET, ip_addr, b,
sizeof(b)) == NULL)
121 if (mask && mask->s_addr != 0xFFFFFFFF) {
125 snprintf(lbuf,
sizeof(lbuf),
"/%u", __mask_size(mask->s_addr));
126 n = strlcpy(buff, b, len);
129 strlcat(buff, lbuf, len);
131 strlcpy(buff, b, len);
137 static inline uint64_t
138 inet_mtoh64(
struct ether_addr *eaddr, uint64_t *value)
150 inet_h64tom(uint64_t value,
struct ether_addr *eaddr)
163 inet_addr_mask_cmp(
struct in_addr *c1,
struct in_addr *c2,
struct in_addr *n1)
170 val = (c1->s_addr & n1->s_addr) == (c2->s_addr & n1->s_addr);
177 in_caddr_zero(
struct in_caddr *f)
179 struct in_caddr z = {0};
180 *f = *(
struct in_caddr *)&z;
185 in_caddr_copy(
struct in_caddr *t,
struct in_caddr *f)
192 in_caddr_compare(
struct in_caddr *p1,
struct in_caddr *p2)
194 #ifdef USE_AVX_INSTRUCTIONS
197 v1 = _mm256_loadu_si256((__m256i
const *)p1);
198 v2 = _mm256_loadu_si256((__m256i
const *)p2);
200 return vec_equal(v1, v2);
202 return (p1->cin_len == p2->cin_len) && (p1->cin_family == p2->cin_family) &&
203 (p1->cin_addr.s_addr == p2->cin_addr.s_addr) && (p1->cin_port == p2->cin_port);
207 #define in_caddr_eq(p1, p2) in_caddr_compare((p1), (p2))
211 in_caddr_neq(
struct in_caddr *p1,
struct in_caddr *p2)
213 return !in_caddr_compare(p1, p2);
218 in_caddr_gt(
struct in_caddr *p1,
struct in_caddr *p2)
220 return memcmp(p1, p2, p1->cin_len) > 0;
225 in_caddr_lt(
struct in_caddr *p1,
struct in_caddr *p2)
227 return memcmp(p1, p2, p1->cin_len) < 0;
231 in_caddr_and(
struct in_caddr *p1,
struct in_caddr *p2)
233 p1->cin_addr.s_addr &= p2->cin_addr.s_addr;
237 in_caddr_mask(
struct in_caddr *na,
struct in_caddr *da,
struct in_caddr *ma)
239 na->cin_addr.s_addr = da->cin_addr.s_addr & ma->cin_addr.s_addr;
244 in_caddr_create(
struct in_caddr *sa,
struct in_addr *pa,
int type,
int len,
int port)
246 uint32_t ip = pa->s_addr;
251 len = cne_numbytes(ip);
254 sa->cin_family = type;
256 sa->cin_addr.s_addr = ip;
261 in_caddr_init(
struct in_caddr *sa,
int type,
int len,
int port)
265 sa->cin_family = type;
271 in_caddr_update(
struct in_caddr *sa,
int type,
int len,
int port)
274 sa->cin_family = type;
uint8_t ether_addr_octet[ETH_ALEN]