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