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