-
Notifications
You must be signed in to change notification settings - Fork 19
/
ipcalc-reverse.c
126 lines (111 loc) · 3.71 KB
/
ipcalc-reverse.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* Copyright (c) 2015 Red Hat, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Nikos Mavrogiannopoulos <[email protected]>
*/
#define _GNU_SOURCE /* asprintf */
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdarg.h>
#include "ipcalc.h"
/* draft-ietf-dnsop-rfc2317bis-00 replaces that legacy style
*/
#undef USE_RFC2317_STYLE
char *calc_reverse_dns4(struct in_addr ip, unsigned prefix, struct in_addr network, struct in_addr broadcast)
{
char *str = NULL;
int ret = -1;
unsigned byte1 = (ntohl(ip.s_addr) >> 24) & 0xff;
unsigned byte2 = (ntohl(ip.s_addr) >> 16) & 0xff;
unsigned byte3 = (ntohl(ip.s_addr) >> 8) & 0xff;
unsigned byte4 = (ntohl(ip.s_addr)) & 0xff;
#ifdef USE_RFC2317_STYLE
if (prefix == 32) {
ret = asprintf(&str, "%u.%u.%u.%u.in-addr.arpa.", byte4, byte3, byte2, byte1);
} else if (prefix == 24) {
ret = asprintf(&str, "%u.%u.%u.in-addr.arpa.", byte3, byte2, byte1);
} else if (prefix == 16) {
ret = asprintf(&str, "%u.%u.in-addr.arpa.", byte2, byte1);
} else if (prefix == 8) {
ret = asprintf(&str, "%u.in-addr.arpa.", byte1);
} else if (prefix > 24) {
ret = asprintf(&str, "%u/%u.%u.%u.%u.in-addr.arpa.", byte4, prefix, byte3, byte2, byte1);
} else if (prefix > 16) {
ret = asprintf(&str, "%u/%u.%u.%u.in-addr.arpa.", byte3, prefix, byte2, byte1);
} else if (prefix > 8) {
ret = asprintf(&str, "%u/%u.%u.in-addr.arpa.", byte2, prefix, byte1);
}
#else
if (prefix == 32) {
ret = asprintf(&str, "%u.%u.%u.%u.in-addr.arpa.", byte4, byte3, byte2, byte1);
} else if (prefix == 24) {
ret = asprintf(&str, "%u.%u.%u.in-addr.arpa.", byte3, byte2, byte1);
} else if (prefix == 16) {
ret = asprintf(&str, "%u.%u.in-addr.arpa.", byte2, byte1);
} else if (prefix == 8) {
ret = asprintf(&str, "%u.in-addr.arpa.", byte1);
} else if (prefix > 24) {
unsigned min = (ntohl(network.s_addr)) & 0xff;
unsigned max = (ntohl(broadcast.s_addr)) & 0xff;
ret = asprintf(&str, "%u-%u.%u.%u.%u.in-addr.arpa.", min, max, byte3, byte2, byte1);
} else if (prefix > 16) {
unsigned min = (ntohl(network.s_addr) >> 8) & 0xff;
unsigned max = (ntohl(broadcast.s_addr) >> 8) & 0xff;
ret = asprintf(&str, "%u-%u.%u.%u.in-addr.arpa.", min, max, byte2, byte1);
} else if (prefix > 8) {
unsigned min = (ntohl(network.s_addr) >> 16) & 0xff;
unsigned max = (ntohl(broadcast.s_addr) >> 16) & 0xff;
ret = asprintf(&str, "%u-%u.%u.in-addr.arpa.", min, max, byte1);
}
#endif
if (ret == -1)
return NULL;
return str;
}
static char hexchar(unsigned int val)
{
if (val < 10)
return '0' + val;
if (val < 16)
return 'a' + val - 10;
abort();
}
char *calc_reverse_dns6(struct in6_addr *ip, unsigned prefix)
{
unsigned i, j = 0;
char str[256];
unsigned max = prefix/8;
if (prefix % 4 != 0)
return NULL;
if (prefix % 8 == 4) {
str[j++] = hexchar(ip->s6_addr[(prefix+4)/8-1] >> 4);
str[j++] = '.';
}
for (i=0;i<max;i++) {
str[j++] = hexchar(ip->s6_addr[max-1-i] & 0xf);
str[j++] = '.';
str[j++] = hexchar(ip->s6_addr[max-1-i] >> 4);
str[j++] = '.';
}
strcpy(&str[j], "ip6.arpa.");
return strdup(str);
}