Skip to content

Instantly share code, notes, and snippets.

@0xa
Created June 8, 2013 20:12
Show Gist options
  • Select an option

  • Save 0xa/5736416 to your computer and use it in GitHub Desktop.

Select an option

Save 0xa/5736416 to your computer and use it in GitHub Desktop.
Generate AAAA and PTR DNS fields for reverses of an IPv6 range.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
const char *reverse_root = "ip.vpn.xomg.net.";
unsigned short ip6_range[8] = {0x2001, 0x41d0, 0x8, 0x144d, 0xcc, 0, 0, 0};
const char ip6_netmask = 7;
const char ip6_ignore_subs = 0;
const char *zone_file = "db.zone";
const char *reverse_file = "db.reverse";
const char *zone_header =
"$TTL 60\n" \
"@ IN SOA whiterun.xomg.net. dns-admin+vpn.xomg.net. (%s 10800 3600 2419200 604800)\n" \
"@ IN NS whiterun.xomg.net.\n" \
"\n";
const char *reverse_header =
"$TTL 60\n" \
"@ IN SOA whiterun.xomg.net. dns-admin+vpn.xomg.net. (%s 10800 3600 2419200 604800)\n" \
"@ IN NS whiterun.xomg.net.\n" \
"\n";
FILE *zone_fh, *reverse_fh;
char *zone_format;
char *fqdn = NULL;
char *iprhd = NULL;
char *iphex = NULL;
void write_address(unsigned short *ip) {
int pos = 0;
for (char n=(7-ip6_ignore_subs); n >= ip6_netmask; n--) {
pos += sprintf(fqdn+pos, "%x.", ip[n]);
}
strcpy(fqdn+pos, reverse_root);
fprintf(zone_fh, "%s IN AAAA %x:%x:%x:%x:%x:%x:%x:%x\n", fqdn,
ip[0],ip[1],ip[2],ip[3],ip[4],ip[5],ip[6],ip[7]);
// xxxxxxxxxxxx... // no . or :, only hex.
sprintf(iphex, "%04x%04x%04x%04x%04x%04x%04x%04x",
ip[0],ip[1],ip[2],ip[3],ip[4],ip[5],ip[6],ip[7]);
for (int i=31, j=0; i >= 0; i--, j+=2) {
iprhd[j] = iphex[i];
iprhd[j+1] = '.';
}
iprhd[64] = 0;
fprintf(reverse_fh, "%sip6.arpa. IN PTR %s\n", iprhd, fqdn);
}
int loop(int netmask, unsigned short *ip) {
// init to NET:0:0...
for (short n=netmask; n < 8; n++) {
ip[netmask] = 0;
}
for (; ip[netmask] < 0xffff; ip[netmask]++) {
if (netmask == 7-ip6_ignore_subs) {
write_address(ip);
} else {
printf("%x:%x:%x:%x:%x:%x:%x:%x/%d\n",
ip[0],ip[1],ip[2],ip[3],ip[4],ip[5],ip[6],ip[7],
netmask*16);
loop(netmask+1, ip);
}
}
}
int main() {
// That should not really be here, but...
fqdn = (char*) malloc(sizeof(char)*(8-ip6_netmask)*4 + 2 + strlen(reverse_root));
iprhd = (char*) malloc(sizeof(char) * 65);
iphex = (char*) malloc(sizeof(char) * 33);
zone_fh = fopen(zone_file, "wb");
reverse_fh = fopen(reverse_file, "wb");
// Write headers
char date[32];
time_t rawtime;
time (&rawtime);
struct tm * timeinfo = localtime (&rawtime);
strftime((char*)&date, 32, "%Y%m%d%S", timeinfo);
fprintf(zone_fh, zone_header, date);
fprintf(reverse_fh, reverse_header, date);
// Start loop
loop(ip6_netmask, ip6_range);
fclose(zone_fh);
fclose(reverse_fh);
free(fqdn);
free(iprhd);
free(iphex);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment