X-Git-Url: http://git.harvie.cz/?p=svn%2FPrometheus-QoS%2F.git;a=blobdiff_plain;f=ipv6subnets.c;fp=ipv6subnets.c;h=ce8f7092370a00fbe5a5a00af4c764f449b8b175;hp=0000000000000000000000000000000000000000;hb=0b9c3c198771800cc6c5fbacfc0272549626bcfd;hpb=f64d54310ae39276c552b80183ae2320789bb4be diff --git a/ipv6subnets.c b/ipv6subnets.c new file mode 100644 index 0000000..ce8f709 --- /dev/null +++ b/ipv6subnets.c @@ -0,0 +1,98 @@ +#include "cll1-0.6.2.h" + +/* ====== iptables indexes are used to reduce complexity to log(N) ===== */ + +char *very_ugly_ipv6_code(char *inip, int bitmask, int format_as_chainname) +{ + int colon=0, n, h; + char *ip,*outip,*outptr,*fmt; + + duplicate(inip,ip); + /* debug printf("(%s,%d) -> ",ip,bitmask); */ + + if(ip && *ip && bitmask>=0 && bitmask<=64) + { + string(outip,strlen(ip)+10); /* assertion: 10>strlen("_%d_%d") */ + } + else + { + /* should never exit here */ + return "undefined"; + } + outptr=outip; + while(ip && *ip) + { + if(*ip==':') + { + if(colon<(bitmask/16-1)) + { + if(format_as_chainname) + { + *outptr='_'; + } + else + { + *outptr=':'; + } + outptr++; + colon++; + } + else + { + char *cutcolon=strchr(ip+1,':'); + if(cutcolon) + { + *cutcolon = '\0'; + } + + if(format_as_chainname) + { + fmt = "_%x_%d"; + } + else + { + fmt = ":%x"; + } + + if(bitmask%16) + { + sscanf(ip+1, "%x", &h); +/* printf("[debug - %s scanned hexa as %x]\n",ip+1,h);*/ + n = h-h%(1<<(16-bitmask%16)); + } + else + { + n = 0; + } + +/* printf("%d/%d => [_%d_%d]\n",h,bitmask,n,bitmask); */ + sprintf(outptr,fmt,n,bitmask); + if(!format_as_chainname) + { + strcat(outip,"::"); /* ahem :-) */ + } + /* debug printf("[%s]\n",outip); */ + return outip; + } + } + else + { + *outptr=*ip; + outptr++; + } + ip++; + } + /*should never exit here*/ + *outptr='\0'; + return outip; +} + +char *index6_id(char *ip,int bitmask) +{ + return very_ugly_ipv6_code(ip,bitmask,1); +} + +char *subnet6_id(char *ip,int bitmask) +{ + return very_ugly_ipv6_code(ip,bitmask,0); +}