File: ipv6-prefix.c

package info (click to toggle)
ocserv 1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,924 kB
  • sloc: ansic: 49,159; sh: 12,767; makefile: 414; xml: 29
file content (114 lines) | stat: -rw-r--r-- 3,048 bytes parent folder | download
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
/*
 * Copyright (C) 2015 Red Hat, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>

#include "../src/ip-util.h"
#include "../src/ip-util.c"

static char *my_ipv6_prefix_to_mask(char str[MAX_IP_STR], unsigned int prefix)
{
	struct in6_addr in;

	if (ipv6_prefix_to_mask(&in, prefix) == 0)
		return NULL;

	if (inet_ntop(AF_INET6, &in, str, MAX_IP_STR) == NULL)
		return NULL;

	return str;
}

int main(void)
{
	char *p;
	char str[MAX_IP_STR];

	p = my_ipv6_prefix_to_mask(str, 128);
	if (p == NULL ||
	    strcmp(p, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff") != 0) {
		fprintf(stderr, "error in %d: %s\n", __LINE__, p);
		exit(1);
	}

	p = my_ipv6_prefix_to_mask(str, 127);
	if (p == NULL ||
	    strcmp(p, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe") != 0) {
		fprintf(stderr, "error in %d: %s\n", __LINE__, p);
		exit(1);
	}

	p = my_ipv6_prefix_to_mask(str, 97);
	if (p == NULL ||
	    strcmp(p, "ffff:ffff:ffff:ffff:ffff:ffff:8000:0") != 0) {
		fprintf(stderr, "error in %d: %s\n", __LINE__, p);
		exit(1);
	}

	p = my_ipv6_prefix_to_mask(str, 96);
	if (p == NULL || strcmp(p, "ffff:ffff:ffff:ffff:ffff:ffff::") != 0) {
		fprintf(stderr, "error in %d: %s\n", __LINE__, p);
		exit(1);
	}

	p = my_ipv6_prefix_to_mask(str, 95);
	if (p == NULL || strcmp(p, "ffff:ffff:ffff:ffff:ffff:fffe::") != 0) {
		fprintf(stderr, "error in %d: %s\n", __LINE__, p);
		exit(1);
	}

	p = my_ipv6_prefix_to_mask(str, 67);
	if (p == NULL || strcmp(p, "ffff:ffff:ffff:ffff:e000::") != 0) {
		fprintf(stderr, "error in %d: %s\n", __LINE__, p);
		exit(1);
	}

	p = my_ipv6_prefix_to_mask(str, 64);
	if (p == NULL || strcmp(p, "ffff:ffff:ffff:ffff::") != 0) {
		fprintf(stderr, "error in %d: %s\n", __LINE__, p);
		exit(1);
	}

	p = my_ipv6_prefix_to_mask(str, 59);
	if (p == NULL || strcmp(p, "ffff:ffff:ffff:ffe0::") != 0) {
		fprintf(stderr, "error in %d: %s\n", __LINE__, p);
		exit(1);
	}

	p = my_ipv6_prefix_to_mask(str, 48);
	if (p == NULL || strcmp(p, "ffff:ffff:ffff::") != 0) {
		fprintf(stderr, "error in %d: %s\n", __LINE__, p);
		exit(1);
	}

	p = my_ipv6_prefix_to_mask(str, 32);
	if (p == NULL || strcmp(p, "ffff:ffff::") != 0) {
		fprintf(stderr, "error in %d: %s\n", __LINE__, p);
		exit(1);
	}

	p = my_ipv6_prefix_to_mask(str, 12);
	if (p == NULL || strcmp(p, "fff0::") != 0) {
		fprintf(stderr, "error in %d: %s\n", __LINE__, p);
		exit(1);
	}

	return 0;
}