ipv4 helper macro
This commit is contained in:
1 parent
ecdbef8b45
commit
07b27b7d9d
2 files changed
+41
No files matched your search
@@ -0,0 +1,21 @@
|
|||||||
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if !defined(__BYTE_ORDER__)
|
||||||
|
#error "__BYTE_ORDER__ not defined"
|
||||||
|
#elif !defined(__ORDER_BIG_ENDIAN__)
|
||||||
|
#error "__ORDER_BIG_ENDIAN__ not defined"
|
||||||
|
#elif !defined(__ORDER_LITTLE_ENDIAN__)
|
||||||
|
#error "__ORDER_LITTLE_ENDIAN__ not defined"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
||||||
|
#define TLSL_BIG_ENDIAN
|
||||||
|
#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
||||||
|
#define TLSL_LITTLE_ENDIAN
|
||||||
|
#else
|
||||||
|
#error "__BYTE_ORDER__ invalid"
|
||||||
|
#endif
|
||||||
@@ -7,6 +7,7 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
#include "str.h"
|
#include "str.h"
|
||||||
|
#include "endian.h"
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
TLSL_L3PROTO_IPV4,
|
TLSL_L3PROTO_IPV4,
|
||||||
@@ -29,5 +30,24 @@ struct tlsl_ip_addr {
|
|||||||
uint8_t l3proto;
|
uint8_t l3proto;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#if defined(TLSL_BIG_ENDIAN)
|
||||||
|
#define TLSL_IPV4_ADDR_U32(BYTE0, BYTE1, BYTE2, BYTE3) \
|
||||||
|
(((uint32_t)(BYTE0)) \
|
||||||
|
| ((uint32_t)(BYTE1) << 8) \
|
||||||
|
| ((uint32_t)(BYTE1) << 16) \
|
||||||
|
| ((uint32_t)(BYTE1) << 24))
|
||||||
|
#elif defined(TLSL_LITTLE_ENDIAN)
|
||||||
|
#define TLSL_IPV4_ADDR_U32(BYTE0, BYTE1, BYTE2, BYTE3) \
|
||||||
|
(((uint32_t)(BYTE0) << 24) \
|
||||||
|
| ((uint32_t)(BYTE1) << 16) \
|
||||||
|
| ((uint32_t)(BYTE2) << 8) \
|
||||||
|
| ((uint32_t)(BYTE3)))
|
||||||
|
#else
|
||||||
|
#error "Byte order not set"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define TLSL_IPV4_ADDR(BYTE0, BYTE1, BYTE2, BYTE3) \
|
||||||
|
((struct tlsl_ipv4_addr) { .addr = TLSL_IPV4_ADDR_U32(BYTE0, BYTE1, BYTE2, BYTE3) })
|
||||||
|
|
||||||
int tlsl_ip_addr_parse(const char *s, struct tlsl_ip_addr *out);
|
int tlsl_ip_addr_parse(const char *s, struct tlsl_ip_addr *out);
|
||||||
str_buf_t tlsl_ip_addr_fmt(const struct tlsl_ip_addr *a);
|
str_buf_t tlsl_ip_addr_fmt(const struct tlsl_ip_addr *a);
|
||||||
Reference in new issue
Block a user