You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.6 KiB
C

// 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
#include <stdint.h>
#include "str.h"
#include "endian.h"
enum {
TLSL_L3PROTO_IPV4,
TLSL_L3PROTO_IPV6,
};
struct tlsl_ipv4_addr {
uint32_t addr;
};
struct tlsl_ipv6_addr {
uint8_t addr[16];
};
struct tlsl_ip_addr {
union {
struct tlsl_ipv4_addr v4;
struct tlsl_ipv6_addr v6;
};
uint8_t l3proto;
};
#if defined(TLSL_BIG_ENDIAN)
#define TLSL_IPV4_ADDR_U32(BYTE0, BYTE1, BYTE2, BYTE3) \
((((uint32_t)(BYTE0) & 0xff) << 24) \
| (((uint32_t)(BYTE1) & 0xff) << 16) \
| (((uint32_t)(BYTE2) & 0xff) << 8) \
| (((uint32_t)(BYTE3) & 0xff)))
#define TLSL_IPV4_NETMASK_U32(BITS) \
((~(uint32_t)0) << (32 - (BITS)))
#elif defined(TLSL_LITTLE_ENDIAN)
#define TLSL_IPV4_ADDR_U32(BYTE0, BYTE1, BYTE2, BYTE3) \
((((uint32_t)(BYTE0) & 0xff)) \
| (((uint32_t)(BYTE1) & 0xff) << 8) \
| (((uint32_t)(BYTE2) & 0xff) << 16) \
| (((uint32_t)(BYTE3) & 0xff) << 24))
#define TLSL_IPV4_NETMASK_U32(BITS) \
((~(uint32_t)0) >> (32 - (BITS)))
#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);
str_buf_t tlsl_ip_addr_fmt(const struct tlsl_ip_addr *a);