AceInfinity
Emeritus, Contributor
IPv6 Reverse Zone calculation example I wrote in C just today:
More information and examples can be seen on the subnet calculator here: IPv4/IPv6 subnet calculator and addressing planner :thumbsup2:
I'm not using all of the stuff in this code including some macros, I decided to avoid them.
Code:
[NO-PARSE]#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <limits.h>
#include <assert.h>
#define UNUSED(x) (void)(x)
#define BIT_COUNT(x) (sizeof(x) * CHAR_BIT)
#define BIT_MASK(x) ((1 << x) - 1)
#define UBOUND(l) ((l) - 1)
#define IPV6_FIELD_COUNT 8
#define IPV6_FIELD_WIDTH 16
void ipv6_reverse_zone(char *ipv6)
{
size_t n = 0;
long int l = 0;
char *offset = ipv6;
uint16_t fields[IPV6_FIELD_COUNT] = { 0 };
do
{
if (*offset == ':') { ++offset; break; }
l = strtol(offset, &offset, 16);
/* assert(l != 0); */
fields[n] = l & 0xffff; /* store 16-bits */
}
while (++n < IPV6_FIELD_COUNT && *offset++ == ':');
if (*offset == '/')
{
l = strtol(offset + 1, NULL, 10);
assert(l > 0 && l <= 128);
if (l % 4 != 0) l = (l + 4) & -4; /* round CIDR to nearest nibble-boundary */
do
{
--l;
printf("%x.", (fields[l / IPV6_FIELD_WIDTH] >> (UBOUND(IPV6_FIELD_WIDTH)
- (l % IPV6_FIELD_WIDTH))) & UBOUND(IPV6_FIELD_WIDTH));
}
while ((l -= 3) > 0);
puts("ip6.arpa");
}
}
int main(void)
{
ipv6_reverse_zone("2a06:1280:2000::/64");
ipv6_reverse_zone("2a06:1280:2000::/1");
ipv6_reverse_zone("2001:db8::/29");
ipv6_reverse_zone("2a06:1280:2000::/128");
ipv6_reverse_zone("2a06:1280:2000::/8");
return 0;
}[/NO-PARSE]
More information and examples can be seen on the subnet calculator here: IPv4/IPv6 subnet calculator and addressing planner :thumbsup2:
I'm not using all of the stuff in this code including some macros, I decided to avoid them.