hosts-ping: moving averages added
[svn/Prometheus-QoS/.git] / ipv6subnets.c
1 #include "cll1-0.6.2.h"
2
3 /* ====== iptables indexes are used to reduce complexity to log(N) ===== */
4
5 char *very_ugly_ipv6_code(char *inip, int bitmask, int format_as_chainname)
6 {
7 int colon=0, n, h;
8 char *ip,*outip,*outptr,*fmt;
9
10 duplicate(inip,ip);
11 /* debug printf("(%s,%d) -> ",ip,bitmask); */
12
13 if(ip && *ip && bitmask>=0 && bitmask<=64)
14 {
15 string(outip,strlen(ip)+10); /* assertion: 10>strlen("_%d_%d") */
16 }
17 else
18 {
19 /* should never exit here */
20 return "undefined";
21 }
22 outptr=outip;
23 while(ip && *ip)
24 {
25 if(*ip==':')
26 {
27 if(colon<(bitmask/16-1))
28 {
29 if(format_as_chainname)
30 {
31 *outptr='_';
32 }
33 else
34 {
35 *outptr=':';
36 }
37 outptr++;
38 colon++;
39 }
40 else
41 {
42 char *cutcolon=strchr(ip+1,':');
43 if(cutcolon)
44 {
45 *cutcolon = '\0';
46 }
47
48 if(format_as_chainname)
49 {
50 fmt = "_%x_%d";
51 }
52 else
53 {
54 fmt = ":%x";
55 }
56
57 if(bitmask%16)
58 {
59 sscanf(ip+1, "%x", &h);
60 /* printf("[debug - %s scanned hexa as %x]\n",ip+1,h);*/
61 n = h-h%(1<<(16-bitmask%16));
62 }
63 else
64 {
65 n = 0;
66 }
67
68 /* printf("%d/%d => [_%d_%d]\n",h,bitmask,n,bitmask); */
69 sprintf(outptr,fmt,n,bitmask);
70 if(!format_as_chainname)
71 {
72 strcat(outip,"::"); /* ahem :-) */
73 }
74 /* debug printf("[%s]\n",outip); */
75 return outip;
76 }
77 }
78 else
79 {
80 *outptr=*ip;
81 outptr++;
82 }
83 ip++;
84 }
85 /*should never exit here*/
86 *outptr='\0';
87 return outip;
88 }
89
90 char *index6_id(char *ip,int bitmask)
91 {
92 return very_ugly_ipv6_code(ip,bitmask,1);
93 }
94
95 char *subnet6_id(char *ip,int bitmask)
96 {
97 return very_ugly_ipv6_code(ip,bitmask,0);
98 }
This page took 0.442665 seconds and 4 git commands to generate.