Loading...
Searching...
No Matches
addr.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 Martine Lenders <mlenders@inf.fu-berlin.de>
3 *
4 * This file is subject to the terms and conditions of the GNU Lesser
5 * General Public License v2.1. See the file LICENSE in the top level
6 * directory for more details.
7 */
8
9#pragma once
10
22
23#include <stdbool.h>
24#include <stdint.h>
25#include <stddef.h>
26
27#include "byteorder.h"
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
36#define IPV4_ADDR_MAX_STR_LEN (sizeof("255.255.255.255"))
37
48#define IPV4_ADDR_INIT(a, b, c, d) { .u8 = {a, b, c, d} }
49
53#define IPV4_ADDR_LOOPBACK IPV4_ADDR_INIT(127, 0, 0, 1)
54
58typedef union {
59 uint8_t u8[4];
62
72static inline bool ipv4_addr_equal(const ipv4_addr_t *a, const ipv4_addr_t *b)
73{
74 return (a->u32.u32 == b->u32.u32);
75}
76
89static inline bool ipv4_addr_is_multicast(const ipv4_addr_t *addr)
90{
91 return (addr->u8[0] >= 0xE0 && addr->u8[0] <= 0xEF);
92}
93
106char *ipv4_addr_to_str(char *result, const ipv4_addr_t *addr, uint8_t result_len);
107
119ipv4_addr_t *ipv4_addr_from_str(ipv4_addr_t *result, const char *addr);
120
135ipv4_addr_t *ipv4_addr_from_buf(ipv4_addr_t *result, const char *addr,
136 size_t addr_len);
137
143void ipv4_addr_print(const ipv4_addr_t *addr);
144
145#ifdef __cplusplus
146}
147#endif
148
Functions to work with different byte orders.
be_uint32_t network_uint32_t
A 32 bit integer in network byte order.
Definition byteorder.h:112
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.
void ipv4_addr_print(const ipv4_addr_t *addr)
Print IPv4 address to stdout.
static bool ipv4_addr_is_multicast(const ipv4_addr_t *addr)
Check if addr is a multicast address.
Definition addr.h:89
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:72
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.
uint32_t u32
32 bit representation
Definition byteorder.h:84
Data type to represent an IPv4 address.
Definition addr.h:58
uint8_t u8[4]
as 4 8-bit unsigned integer
Definition addr.h:59
network_uint32_t u32
as 32-bit unsigned integer
Definition addr.h:60