summaryrefslogtreecommitdiffstats
path: root/contrib/bind/tests/test_cidr.c
blob: f4845f00d9238ecf8e2a8e1b9f055782fa43c926 (plain)
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
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>

static void test(const char *);

int
main() {
	test("192.5.4.0/23");
	test("192.5.4.0");
	test("192.5.5.1");
	test("192.5.5.1/23");
	test("192.5.5.1/24");
	test("192.5.5.1/28");
	test("192.5.5.1/32");
	return (0);
}

static void
test(const char *input) {
	int bits;
	u_char temp[sizeof (struct in_addr)];
	char output[sizeof "255.255.255.255/32"];

	memset(temp, 0x5e, sizeof temp);
	if (inet_cidr_pton(AF_INET, input, temp, &bits) < 0) {
		perror(input);
		exit(1);
	}
	if (inet_cidr_ntop(AF_INET, temp, bits, output, sizeof output)==NULL){
		perror("inet_cidr_ntop");
		exit(1);
	}
	printf("input '%s', temp '%x %x %x %x', bits %d, output '%s'\n",
	       input, temp[0], temp[1], temp[2], temp[3], bits, output);
}
OpenPOWER on IntegriCloud