basic ipset support for qos-free-zone
[svn/Prometheus-QoS/.git] / ipv4subnets.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_ipv4_code(char *inip, int bitmask, int format_as_chainname)
6 {
7 int dot=0, n;
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<=32)
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(dot<(bitmask/8-1))
28 {
29 if(format_as_chainname)
30 {
31 *outptr='_';
32 }
33 else
34 {
35 *outptr='.';
36 }
37 outptr++;
38 dot++;
39 }
40 else
41 {
42 char *cutdot=strchr(ip+1,'.'); /*for bitmask<24*/
43 if(cutdot)
44 {
45 *cutdot = '\0';
46 }
47
48 if(format_as_chainname)
49 {
50 fmt = "_%d_%d";
51 }
52 else
53 {
54 fmt = ".%d";
55 }
56
57 if(bitmask%8)
58 {
59 n = atoi(ip+1)-atoi(ip+1)%(1<<(8-bitmask%8));
60 }
61 else
62 {
63 n = 0;
64 }
65
66 /*debug printf("%d/%d => [_%d_%d]\n",atoi(ip+1),bitmask,n,bitmask); */
67 sprintf(outptr,fmt,n,bitmask);
68 if(!format_as_chainname)
69 {
70 while(bitmask<24)
71 {
72 strcat(outip,".0");
73 bitmask+=8;
74 }
75 }
76 /* debug printf("[%s]\n",outip); */
77 return outip;
78 }
79 }
80 else
81 {
82 *outptr=*ip;
83 outptr++;
84 }
85 ip++;
86 }
87 /*should never exit here*/
88 *outptr='\0';
89 return outip;
90 }
91
92 char *index_id(char *ip, int bitmask)
93 {
94 return very_ugly_ipv4_code(ip,bitmask,1);
95 }
96
97 char *subnet_id(char *ip, int bitmask)
98 {
99 return very_ugly_ipv4_code(ip,bitmask,0);
100 }
This page took 0.28357 seconds and 4 git commands to generate.