From 59c3032e109320ce1b72bb2cac0d81b89fae867c Mon Sep 17 00:00:00 2001 From: xchaos Date: Thu, 11 Oct 2012 21:20:00 +0000 Subject: [PATCH] more modularization git-svn-id: https://dev.arachne.cz/repos/prometheus/trunk@200 251d49ef-1d17-4917-a970-b30cf55b089b --- Makefile | 2 +- ipv4subnets.c | 101 +++++++++++++++++++++++++++++++++++++++++++++++++ prometheus.c | 103 +++----------------------------------------------- 3 files changed, 107 insertions(+), 99 deletions(-) create mode 100644 ipv4subnets.c diff --git a/Makefile b/Makefile index 1719e10..7fcedc2 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ prefix=/usr mandir=$(prefix)/share/man sbindir=$(prefix)/sbin sysconfdir=/etc -OBJECTS=parsehosts.o parselogs.o prometheus.o +OBJECTS=parsehosts.o parselogs.o ipv4subnets.o prometheus.o HEADERS=cll1-0.6.2.h ipstruct.h main: prometheus diff --git a/ipv4subnets.c b/ipv4subnets.c new file mode 100644 index 0000000..0b40229 --- /dev/null +++ b/ipv4subnets.c @@ -0,0 +1,101 @@ +#include "cll1-0.6.2.h" + +/* ====== iptables indexes are used to reduce complexity to log8(N) ===== */ + +char *very_ugly_ipv4_code(char *inip, int bitmask, int format_as_chainname) +{ + /* warning: this function was debugged only for bitmask values 20,24,28 !!!*/ + int dot=0, n; + char *ip,*outip,*outptr,*fmt; + + duplicate(inip,ip); + /* debug printf("(%s,%d) -> ",ip,bitmask); */ + + if(ip && *ip && bitmask>=0 && bitmask<=32) + { + string(outip,strlen(ip)+10); /*fuck unicode? assertion: 10>strlen("_%d_%d") */ + } + else + { + /* should never exit here */ + return "undefined"; + } + outptr=outip; + while(ip && *ip) + { + if(*ip=='.') + { + if(dot<(bitmask/8-1)) + { + if(format_as_chainname) + { + *outptr='_'; + } + else + { + *outptr='.'; + } + outptr++; + dot++; + } + else + { + char *cutdot=strchr(ip+1,'.'); /*for bitmask<24*/ + if(cutdot) + { + *cutdot = '\0'; + } + + if(format_as_chainname) + { + fmt = "_%d_%d"; + } + else + { + fmt = ".%d"; + } + + if(bitmask%8) + { + n = atoi(ip+1)-atoi(ip+1)%(1<<(8-bitmask%8)); + } + else + { + n = 0; + } + + /*debug printf("%d/%d => [_%d_%d]\n",atoi(ip+1),bitmask,n,bitmask); */ + sprintf(outptr,fmt,n,bitmask); + if(!format_as_chainname) + { + while(bitmask<24) + { + strcat(outip,".0"); + bitmask+=8; + } + } + /* debug printf("[%s]\n",outip); */ + return outip; + } + } + else + { + *outptr=*ip; + outptr++; + } + ip++; + } + /*should never exit here*/ + *outptr='\0'; + return outip; +} + +char *index_id(char *ip,int bitmask) +{ + return very_ugly_ipv4_code(ip,bitmask,1); +} + +char *subnet_id(char *ip,int bitmask) +{ + return very_ugly_ipv4_code(ip,bitmask,0); +} diff --git a/prometheus.c b/prometheus.c index 40b8865..beaffcb 100644 --- a/prometheus.c +++ b/prometheus.c @@ -179,111 +179,18 @@ struct Index list(Index); } *idxs=NULL, *idx, *metaindex; - void TheIP(void); /* function implemented in parsehosts.c */ /* ====== iptables indexes are used to reduce complexity to log8(N) ===== */ -char *very_ugly_ipv4_code(char *inip, int bitmask, int format_as_chainname) -{ - /* warning: this function was debugged only for bitmask values 20,24,28 !!!*/ - int dot=0, n; - char *ip,*outip,*outptr,*fmt; - - duplicate(inip,ip); - /* debug printf("(%s,%d) -> ",ip,bitmask); */ +char *index_id(char *ip, int bitmask); +/* function implemented in ipv4subnets.c */ - if(ip && *ip && bitmask>=0 && bitmask<=32) - { - string(outip,strlen(ip)+10); /*fuck unicode? assertion: 10>strlen("_%d_%d") */ - } - else - { - /* should never exit here */ - return "undefined"; - } - outptr=outip; - while(ip && *ip) - { - if(*ip=='.') - { - if(dot<(bitmask/8-1)) - { - if(format_as_chainname) - { - *outptr='_'; - } - else - { - *outptr='.'; - } - outptr++; - dot++; - } - else - { - char *cutdot=strchr(ip+1,'.'); /*for bitmask<24*/ - if(cutdot) - { - *cutdot = '\0'; - } - - if(format_as_chainname) - { - fmt = "_%d_%d"; - } - else - { - fmt = ".%d"; - } - - if(bitmask%8) - { - n = atoi(ip+1)-atoi(ip+1)%(1<<(8-bitmask%8)); - } - else - { - n = 0; - } - - /*debug printf("%d/%d => [_%d_%d]\n",atoi(ip+1),bitmask,n,bitmask); */ - sprintf(outptr,fmt,n,bitmask); - if(!format_as_chainname) - { - while(bitmask<24) - { - strcat(outip,".0"); - bitmask+=8; - } - } - /* debug printf("[%s]\n",outip); */ - return outip; - } - } - else - { - *outptr=*ip; - outptr++; - } - ip++; - } - /*should never exit here*/ - *outptr='\0'; - return outip; -} - -char *index_id(char *ip,int bitmask) -{ - return very_ugly_ipv4_code(ip,bitmask,1); -} - -char *subnet_id(char *ip,int bitmask) -{ - return very_ugly_ipv4_code(ip,bitmask,0); -} +char *subnet_id(char *ip, int bitmask); +/* function implemented in ipv4subnets.c */ -/* ================= Let's parse configuration file here =================== */ +/* ================= Let's parse configuration file here ================ */ void reject_config_and_exit(char *filename) { -- 2.30.2