59c3032e |
1 | #include "cll1-0.6.2.h"\r |
2 | \r |
0b9c3c19 |
3 | /* ====== iptables indexes are used to reduce complexity to log(N) ===== */\r |
59c3032e |
4 | \r |
5 | char *very_ugly_ipv4_code(char *inip, int bitmask, int format_as_chainname)\r |
6 | {\r |
59c3032e |
7 | int dot=0, n;\r |
8 | char *ip,*outip,*outptr,*fmt;\r |
9 | \r |
10 | duplicate(inip,ip);\r |
11 | /* debug printf("(%s,%d) -> ",ip,bitmask); */\r |
12 | \r |
13 | if(ip && *ip && bitmask>=0 && bitmask<=32)\r |
14 | {\r |
0b9c3c19 |
15 | string(outip,strlen(ip)+10); /* assertion: 10>strlen("_%d_%d") */\r |
59c3032e |
16 | }\r |
17 | else \r |
18 | {\r |
19 | /* should never exit here */\r |
20 | return "undefined";\r |
21 | }\r |
22 | outptr=outip;\r |
23 | while(ip && *ip)\r |
24 | {\r |
25 | if(*ip=='.')\r |
26 | {\r |
27 | if(dot<(bitmask/8-1)) \r |
28 | {\r |
29 | if(format_as_chainname)\r |
30 | {\r |
31 | *outptr='_';\r |
32 | }\r |
33 | else\r |
34 | {\r |
35 | *outptr='.';\r |
36 | }\r |
37 | outptr++;\r |
38 | dot++;\r |
39 | }\r |
40 | else\r |
41 | {\r |
42 | char *cutdot=strchr(ip+1,'.'); /*for bitmask<24*/ \r |
43 | if(cutdot)\r |
44 | {\r |
45 | *cutdot = '\0';\r |
46 | }\r |
47 | \r |
48 | if(format_as_chainname)\r |
49 | {\r |
50 | fmt = "_%d_%d";\r |
51 | }\r |
52 | else\r |
53 | {\r |
54 | fmt = ".%d";\r |
55 | }\r |
56 | \r |
57 | if(bitmask%8)\r |
58 | {\r |
59 | n = atoi(ip+1)-atoi(ip+1)%(1<<(8-bitmask%8));\r |
60 | }\r |
61 | else\r |
62 | {\r |
63 | n = 0;\r |
64 | }\r |
65 | \r |
66 | /*debug printf("%d/%d => [_%d_%d]\n",atoi(ip+1),bitmask,n,bitmask); */\r |
67 | sprintf(outptr,fmt,n,bitmask);\r |
68 | if(!format_as_chainname)\r |
69 | {\r |
70 | while(bitmask<24)\r |
71 | {\r |
72 | strcat(outip,".0");\r |
73 | bitmask+=8;\r |
74 | }\r |
75 | }\r |
76 | /* debug printf("[%s]\n",outip); */\r |
77 | return outip;\r |
78 | }\r |
79 | }\r |
80 | else \r |
81 | {\r |
82 | *outptr=*ip;\r |
83 | outptr++;\r |
84 | }\r |
85 | ip++;\r |
86 | }\r |
87 | /*should never exit here*/\r |
88 | *outptr='\0';\r |
89 | return outip;\r |
90 | }\r |
91 | \r |
0b9c3c19 |
92 | char *index_id(char *ip, int bitmask)\r |
59c3032e |
93 | { \r |
94 | return very_ugly_ipv4_code(ip,bitmask,1);\r |
95 | }\r |
96 | \r |
0b9c3c19 |
97 | char *subnet_id(char *ip, int bitmask)\r |
59c3032e |
98 | {\r |
99 | return very_ugly_ipv4_code(ip,bitmask,0);\r |
100 | }\r |