addr.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2015 Martine Lenders <mlenders@inf.fu-berlin.de>
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
20 #include <stdbool.h>
21 #include <stdint.h>
22 #include <stddef.h>
23 
24 #include "byteorder.h"
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
33 #define IPV4_ADDR_MAX_STR_LEN (sizeof("255.255.255.255"))
34 
45 #define IPV4_ADDR_INIT(a, b, c, d) { .u8 = {a, b, c, d} }
46 
50 #define IPV4_ADDR_LOOPBACK IPV4_ADDR_INIT(127, 0, 0, 1)
51 
55 typedef union {
56  uint8_t u8[4];
58 } ipv4_addr_t;
59 
69 static inline bool ipv4_addr_equal(const ipv4_addr_t *a, const ipv4_addr_t *b)
70 {
71  return (a->u32.u32 == b->u32.u32);
72 }
73 
86 static inline bool ipv4_addr_is_multicast(const ipv4_addr_t *addr)
87 {
88  return (addr->u8[0] >= 0xE0 && addr->u8[0] <= 0xEF);
89 }
90 
103 char *ipv4_addr_to_str(char *result, const ipv4_addr_t *addr, uint8_t result_len);
104 
116 ipv4_addr_t *ipv4_addr_from_str(ipv4_addr_t *result, const char *addr);
117 
132 ipv4_addr_t *ipv4_addr_from_buf(ipv4_addr_t *result, const char *addr,
133  size_t addr_len);
134 
140 void ipv4_addr_print(const ipv4_addr_t *addr);
141 
142 #ifdef __cplusplus
143 }
144 #endif
145 
Functions to work with different byte orders.
void ipv4_addr_print(const ipv4_addr_t *addr)
Print IPv4 address to stdout.
char * ipv4_addr_to_str(char *result, const ipv4_addr_t *addr, uint8_t result_len)
Converts an IPv4 address to its string representation.
ipv4_addr_t * ipv4_addr_from_buf(ipv4_addr_t *result, const char *addr, size_t addr_len)
Converts an IPv4 address from a buffer of characters to a byte-represented IPv4 address.
static bool ipv4_addr_is_multicast(const ipv4_addr_t *addr)
Check if addr is a multicast address.
Definition: addr.h:86
static bool ipv4_addr_equal(const ipv4_addr_t *a, const ipv4_addr_t *b)
Checks if two IPv4 addresses are equal.
Definition: addr.h:69
ipv4_addr_t * ipv4_addr_from_str(ipv4_addr_t *result, const char *addr)
Converts an IPv4 address string representation to a byte-represented IPv4 address.
A 32 bit integer in big endian aka network byte order.
Definition: byteorder.h:80
uint32_t u32
32 bit representation
Definition: byteorder.h:81
Data type to represent an IPv4 address.
Definition: addr.h:55
uint8_t u8[4]
as 4 8-bit unsigned integer
Definition: addr.h:56
network_uint32_t u32
as 32-bit unsigned integer
Definition: addr.h:57