

9 changed files with 272 additions and 94 deletions
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright (C) 2015 Martine Lenders <mlenders@inf.fu-berlin.de> |
||||
* |
||||
* This file is subject to the terms and conditions of the GNU Lesser |
||||
* General Public License v2.1. See the file LICENSE in the top level |
||||
* directory for more details. |
||||
*/ |
||||
|
||||
/**
|
||||
* @defgroup net_ipv4 IPv4 |
||||
* @ingroup net |
||||
* @brief IPv4 types and helper functions |
||||
* @{ |
||||
* |
||||
* @file |
||||
* @brief IPv4 type and helper function definitions |
||||
* |
||||
* @author Martine Lenders <mlenders@inf.fu-berlin.de> |
||||
*/ |
||||
#ifndef IPV4_H_ |
||||
#define IPV4_H_ |
||||
|
||||
#include "net/ipv4/addr.h" |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
|
||||
#ifdef __cplusplus |
||||
} |
||||
#endif |
||||
|
||||
#endif /* IPV4_H_ */ |
||||
/** @} */ |
@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright (C) 2015 Martine Lenders <mlenders@inf.fu-berlin.de> |
||||
* |
||||
* This file is subject to the terms and conditions of the GNU Lesser |
||||
* General Public License v2.1. See the file LICENSE in the top level |
||||
* directory for more details. |
||||
*/ |
||||
|
||||
/**
|
||||
* @defgroup net_ipv4_addr IPv4 addresses |
||||
* @ingroup net_ipv4 |
||||
* @brief IPv4 address types and helper function |
||||
* @{ |
||||
* |
||||
* @file |
||||
* @brief IPv6 address type and helper functions definitions |
||||
* |
||||
* @author Martine Lenders <mlenders@inf.fu-berlin.de> |
||||
*/ |
||||
#ifndef IPV4_ADDR_H_ |
||||
#define IPV4_ADDR_H_ |
||||
|
||||
#include <stdbool.h> |
||||
#include <stdint.h> |
||||
|
||||
#include "byteorder.h" |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
/**
|
||||
* @brief Maximum length of an IPv4 address as string. |
||||
*/ |
||||
#define IPV4_ADDR_MAX_STR_LEN (sizeof("255.255.255.255")) |
||||
|
||||
/**
|
||||
* @brief Data type to represent an IPv4 address. |
||||
*/ |
||||
typedef union { |
||||
uint8_t u8[4]; /**< as 4 8-bit unsigned integer */ |
||||
network_uint32_t u32; /**< as 32-bit unsigned integer */ |
||||
} ipv4_addr_t; |
||||
|
||||
/**
|
||||
* @brief Checks if two IPv4 addresses are equal. |
||||
* |
||||
* @param[in] a An IPv4 address. |
||||
* @param[in] b Another IPv4 address. |
||||
* |
||||
* @return true, if @p a and @p b are equal |
||||
* @return false, otherwise. |
||||
*/ |
||||
static inline bool ipv4_addr_equal(ipv4_addr_t *a, ipv4_addr_t *b) |
||||
{ |
||||
return (a->u32.u32 == b->u32.u32); |
||||
} |
||||
|
||||
/**
|
||||
* @brief Converts an IPv4 address to its string representation |
||||
* |
||||
* @param[out] result The resulting string representation of at least |
||||
* @ref IPV4_ADDR_MAX_STR_LEN. |
||||
* @param[in] addr An IPv4 address |
||||
* @param[in] result_len Length of @p result |
||||
* |
||||
* @return @p result, on success |
||||
* @return NULL, if @p result_len was lesser than IPV4_ADDR_MAX_STR_LEN |
||||
* @return NULL, if @p result or @p addr was NULL |
||||
*/ |
||||
char *ipv4_addr_to_str(char *result, const ipv4_addr_t *addr, uint8_t result_len); |
||||
|
||||
/**
|
||||
* @brief Converts an IPv4 address string representation to a byte-represented |
||||
* IPv4 address |
||||
* |
||||
* @param[in] result The resulting byte representation |
||||
* @param[in] addr An IPv4 address string representation |
||||
* |
||||
* @return @p result, on success |
||||
* @return NULL, if @p addr was malformed |
||||
* @return NULL, if @p result or @p addr was NULL |
||||
*/ |
||||
ipv4_addr_t *ipv4_addr_from_str(ipv4_addr_t *result, const char *addr); |
||||
|
||||
#ifdef __cplusplus |
||||
} |
||||
#endif |
||||
|
||||
#endif /* IPV4_ADDR_H_ */ |
||||
/** @} */ |
@ -0,0 +1,3 @@
|
||||
MODULE = ipv4_addr
|
||||
|
||||
include $(RIOTBASE)/Makefile.base |
@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright (C) 2015 Martine Lenders <mlenders@inf.fu-berlin.de> |
||||
* |
||||
* This file is subject to the terms and conditions of the GNU Lesser |
||||
* General Public License v2.1. See the file LICENSE in the top level |
||||
* directory for more details. |
||||
*/ |
||||
|
||||
/**
|
||||
* @{ |
||||
* |
||||
* @file |
||||
*/ |
||||
|
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
|
||||
#include "net/ipv4/addr.h" |
||||
|
||||
#define DEC "0123456789" |
||||
|
||||
/* based on inet_pton4() by Paul Vixie */ |
||||
ipv4_addr_t *ipv4_addr_from_str(ipv4_addr_t *result, const char *addr) |
||||
{ |
||||
uint8_t saw_digit, octets, ch; |
||||
uint8_t tmp[sizeof(ipv4_addr_t)], *tp; |
||||
|
||||
if ((result == NULL) || (addr == NULL)) { |
||||
return NULL; |
||||
} |
||||
|
||||
saw_digit = 0; |
||||
octets = 0; |
||||
*(tp = tmp) = 0; |
||||
|
||||
while ((ch = *addr++) != '\0') { |
||||
const char *pch; |
||||
|
||||
if ((pch = strchr(DEC, ch)) != NULL) { |
||||
uint16_t new = *tp * 10 + (uint16_t)(pch - DEC); |
||||
|
||||
if (new > 255) { |
||||
return NULL; |
||||
} |
||||
|
||||
*tp = new; |
||||
|
||||
if (!saw_digit) { |
||||
if (++octets > 4) { |
||||
return NULL; |
||||
} |
||||
|
||||
saw_digit = 1; |
||||
} |
||||
} |
||||
else if (ch == '.' && saw_digit) { |
||||
if (octets == 4) { |
||||
return NULL; |
||||
} |
||||
|
||||
*++tp = 0; |
||||
saw_digit = 0; |
||||
} |
||||
else { |
||||
return NULL; |
||||
} |
||||
} |
||||
|
||||
if (octets < 4) { |
||||
return NULL; |
||||
} |
||||
|
||||
memcpy(result, tmp, sizeof(ipv4_addr_t)); |
||||
return result; |
||||
} |
||||
|
||||
|
||||
/** @} */ |
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright (C) 2015 Martine Lenders <mlenders@inf.fu-berlin.de> |
||||
* |
||||
* This file is subject to the terms and conditions of the GNU Lesser |
||||
* General Public License v2.1. See the file LICENSE in the top level |
||||
* directory for more details. |
||||
*/ |
||||
|
||||
/**
|
||||
* @{ |
||||
* |
||||
* @file |
||||
*/ |
||||
|
||||
#include <stdlib.h> |
||||
|
||||
#include "net/ipv4/addr.h" |
||||
|
||||
/* based on inet_ntop4() by Paul Vixie */ |
||||
char *ipv4_addr_to_str(char *result, const ipv4_addr_t *addr, uint8_t result_len) |
||||
{ |
||||
uint8_t n = 0; |
||||
char *next = result; |
||||
|
||||
if ((result == NULL) || (addr == NULL) || (result_len < IPV4_ADDR_MAX_STR_LEN)) { |
||||
return NULL; |
||||
} |
||||
|
||||
do { |
||||
uint8_t u = addr->u8[n]; |
||||
|
||||
if (u > 99) { |
||||
*next++ = '0' + u / 100; |
||||
u %= 100; |
||||
*next++ = '0' + u / 10; |
||||
u %= 10; |
||||
} |
||||
else if (u > 9) { |
||||
*next++ = '0' + u / 10; |
||||
u %= 10; |
||||
} |
||||
|
||||
*next++ = '0' + u; |
||||
*next++ = '.'; |
||||
n++; |
||||
} while (n < 4); |
||||
|
||||
*--next = '\0'; |
||||
return result; |
||||
} |
||||
|
||||
/** @} */ |
Loading…
Reference in new issue