-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathipv6subnets.c
More file actions
98 lines (89 loc) · 1.59 KB
/
ipv6subnets.c
File metadata and controls
98 lines (89 loc) · 1.59 KB
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
#include "cll1-0.6.2.h"
/* ====== iptables indexes are used to reduce complexity to log(N) ===== */
char *very_ugly_ipv6_code(char *inip, int bitmask, int format_as_chainname)
{
int colon=0, n, h;
char *ip,*outip,*outptr,*fmt;
duplicate(inip,ip);
/* debug printf("(%s,%d) -> ",ip,bitmask); */
if(ip && *ip && bitmask>=0 && bitmask<=64)
{
string(outip,strlen(ip)+10); /* assertion: 10>strlen("_%d_%d") */
}
else
{
/* should never exit here */
return "undefined";
}
outptr=outip;
while(ip && *ip)
{
if(*ip==':')
{
if(colon<(bitmask/16-1))
{
if(format_as_chainname)
{
*outptr='_';
}
else
{
*outptr=':';
}
outptr++;
colon++;
}
else
{
char *cutcolon=strchr(ip+1,':');
if(cutcolon)
{
*cutcolon = '\0';
}
if(format_as_chainname)
{
fmt = "_%x_%d";
}
else
{
fmt = ":%x";
}
if(bitmask%16)
{
sscanf(ip+1, "%x", &h);
/* printf("[debug - %s scanned hexa as %x]\n",ip+1,h);*/
n = h-h%(1<<(16-bitmask%16));
}
else
{
n = 0;
}
/* printf("%d/%d => [_%d_%d]\n",h,bitmask,n,bitmask); */
sprintf(outptr,fmt,n,bitmask);
if(!format_as_chainname)
{
strcat(outip,"::"); /* ahem :-) */
}
/* debug printf("[%s]\n",outip); */
return outip;
}
}
else
{
*outptr=*ip;
outptr++;
}
ip++;
}
/*should never exit here*/
*outptr='\0';
return outip;
}
char *index6_id(char *ip,int bitmask)
{
return very_ugly_ipv6_code(ip,bitmask,1);
}
char *subnet6_id(char *ip,int bitmask)
{
return very_ugly_ipv6_code(ip,bitmask,0);
}