From 9c4e6c084bd9f22cb9a6c1130b60f776d4ddcb7b Mon Sep 17 00:00:00 2001 From: john Date: Thu, 28 Nov 2019 21:27:03 -0500 Subject: [PATCH] More optimizations --- Jmakefile | 2 +- ban2fail.c | 12 +-- ban2fail.h | 3 + iptables.c | 2 +- logFile.c | 2 +- logType.c | 6 +- makefile | 223 ----------------------------------------------------- maxoff.c | 3 +- 8 files changed, 17 insertions(+), 236 deletions(-) delete mode 100644 makefile diff --git a/Jmakefile b/Jmakefile index 780c763..19effa9 100644 --- a/Jmakefile +++ b/Jmakefile @@ -3,7 +3,7 @@ libsDir := $(baseDir)/libs projectName := ban2fail versions := debug release cc_exe := ban2fail -install_dir := /usr/local/bin +#install_dir := /usr/local/bin ######################################## diff --git a/ban2fail.c b/ban2fail.c index 745d870..16703bf 100644 --- a/ban2fail.c +++ b/ban2fail.c @@ -95,7 +95,7 @@ struct Global G= { .version= { .major= 0, .minor= 11, - .patch= 7 + .patch= 8 }, .bitTuples.flags= GlobalFlagBitTuples @@ -164,10 +164,10 @@ main(int argc, char **argv) MAP_constructor(&G.logType_map, 10, 10); // local - MAP_constructor(&S.addr2logEntry_map, 1000, 200); + MAP_constructor(&S.addr2logEntry_map, N_ADDRESSES_HINT/10, 10); - PTRVEC_constructor(&S.toBlock_vec, 100000); - PTRVEC_constructor(&S.toUnblock_vec, 100000); + PTRVEC_constructor(&S.toBlock_vec, N_ADDRESSES_HINT); + PTRVEC_constructor(&S.toUnblock_vec, N_ADDRESSES_HINT); { /*=== Parse command line arguments ===*/ int c, errflg= 0; @@ -377,7 +377,7 @@ main(int argc, char **argv) if(G.flags & GLB_LONG_LISTING_FLG) { MAP map; - MAP_constructor(&map, 1000, 100); + MAP_constructor(&map, N_ADDRESSES_HINT/10, 10); unsigned nOffFound= 0, nAddrFound; @@ -467,7 +467,7 @@ main(int argc, char **argv) /* Map for indexing cntryStat objects */ static MAP byCntry_map; - MAP_sinit(&byCntry_map, 100, 100); + MAP_sinit(&byCntry_map, 100, 10); /* Build index by trawling existing by-address map */ MAP_visitAllEntries(&S.addr2logEntry_map, (int(*)(void*,void*))map_byCountries, &byCntry_map); diff --git a/ban2fail.h b/ban2fail.h index e9d38a9..e8fb6ff 100644 --- a/ban2fail.h +++ b/ban2fail.h @@ -35,6 +35,9 @@ */ #define IPTABLES_BATCH_SZ 100 +/* For sizing maps and vectors, this a starting point */ +#define N_ADDRESSES_HINT 10000 + /* Where to find stuff */ #define CONFIGFILE "/etc/ban2fail/ban2fail.cfg" #define LOCKPATH "/run/lock/ban2fail" diff --git a/iptables.c b/iptables.c index 7875075..a903bed 100644 --- a/iptables.c +++ b/iptables.c @@ -44,7 +44,7 @@ initialize (void) { S.is_init= 1; - MAP_constructor(&S.addr_map, 1000, 200); + MAP_constructor(&S.addr_map, N_ADDRESSES_HINT/10, 10); const static struct ipv { const char *cmd, diff --git a/logFile.c b/logFile.c index 62ea769..4c595df 100644 --- a/logFile.c +++ b/logFile.c @@ -47,7 +47,7 @@ common_constructor(LOGFILE *self) */ { memset(self, 0, sizeof(*self)); - MAP_constructor(&self->addr2logEntry_map, 1000, 200); + MAP_constructor(&self->addr2logEntry_map, N_ADDRESSES_HINT/10, 10); } LOGFILE* diff --git a/logType.c b/logType.c index d726d81..df77ba2 100644 --- a/logType.c +++ b/logType.c @@ -192,7 +192,7 @@ LOGTYPE_proto_constructor(LOGTYPE *self, const struct logProtoType *proto) } if(G.flags & GLB_LONG_LISTING_FLG) { - ez_fprintf(G.listing_fh, "Scanning \"%s\" ...", log_fname); + ez_fprintf(G.listing_fh, "Scanning \"%s\"... ", log_fname); fflush(G.listing_fh); } @@ -233,7 +233,7 @@ LOGTYPE_proto_constructor(LOGTYPE *self, const struct logProtoType *proto) LOGFILE_addressCount(f, &nAddrFound); if(G.flags & GLB_LONG_LISTING_FLG) { - ez_fprintf(G.listing_fh, " found %u offenses (%u addresses)\n", nOffFound, nAddrFound); + ez_fprintf(G.listing_fh, "found %u offenses (%u addresses)\n", nOffFound, nAddrFound); fflush(G.listing_fh); } @@ -476,7 +476,7 @@ LOGTYPE_addressCount(LOGTYPE *self) { /* We'll need a map in which to collect unique addresses */ static MAP smap; - MAP_sinit(&smap, 1000, 100); + MAP_sinit(&smap, N_ADDRESSES_HINT/10, 10); /* Collect results for all LOGILE objects we own */ MAP_visitAllEntries(&self->file_map, (int(*)(void*,void*))LOGFILE_map_addr, &smap); diff --git a/makefile b/makefile deleted file mode 100644 index 0e0c36b..0000000 --- a/makefile +++ /dev/null @@ -1,223 +0,0 @@ -baseDir := ~ -libsDir := $(baseDir)/libs -projectName := ban2fail -versions := debug release -cc_exe := ban2fail -install_dir := /usr/local/bin - - -######################################## -# Set up sources & libraries here. # -######################################## - -ifeq ($(exe), ban2fail) -src := \ - ban2fail.c \ - cfgmap.c \ - cntry.c \ - ez_dirent.c \ - ez_gzfile.c \ - ez_stdio.c \ - ez_stdlib.c \ - iptables.c \ - logType.c \ - logEntry.c \ - logFile.c \ - map.c \ - maxoff.c \ - ptrvec.c \ - str.c \ - util.c \ - - libs := z crypto GeoIP -endif - -######################################## -# Set up custom compile flags here. # -######################################## -ifeq ($(version),debug) -local_cppflags := $(local_cppflags) -D_DEBUG -DDEBUG -std=gnu99 -local_codeflags := -g2 -O0 -Wreturn-type -Wformat -Wchar-subscripts -Wparentheses -Wcast-qual -Wmissing-declarations -local_ldflags := $(local_ldflags) -L$(libsDir)/$(version) -endif - -ifeq ($(version),release) -local_cppflags := $(local_cppflags) -DNDEBUG -std=gnu99 -local_codeflags := -g0 -O3 -Wreturn-type -Wformat -Wchar-subscripts -Wparentheses -Wcast-qual -Wmissing-declarations -local_ldflags := $(local_ldflags) -L$(libsDir)/$(version) -endif - -makefile := makefile -ifndef version -.PHONY : all clean tidy debug release -all : debug release -debug : - @$(MAKE) version=debug exe=ban2fail mainType=CC --no-builtin-rules -f $(makefile) --no-print-directory -release : - @$(MAKE) version=release exe=ban2fail mainType=CC --no-builtin-rules -f $(makefile) --no-print-directory -install : release - @strip release/ban2fail - @cp release/ban2fail $(install_dir)/ -clean : - $(RM) -r $(versions) core *.bak *.tab.h *.tab.c *.yy.c *.yy.h -tidy : - $(RM) $(foreach vs, $(versions), $(vs)/*.o $(vs)/*.d) core *.bak -endif -.DELETE_ON_ERROR : - -ifdef version -roots := \ - $(patsubst %.cc, %, $(filter %.cc, $(src)))\ - $(patsubst %.cxx, %, $(filter %.cxx, $(src)))\ - $(patsubst %.cpp, %, $(filter %.cpp, $(src)))\ - $(patsubst %.C, %, $(filter %.C, $(src)))\ - $(patsubst %.c, %, $(filter %.c, $(src)))\ - $(patsubst %.f, %, $(filter %.f, $(src)))\ - $(patsubst %.for, %, $(filter %.for, $(src)))\ - $(patsubst %.sal, %, $(filter %.sal, $(src)))\ - $(patsubst %.asm, %, $(filter %.asm, $(src)))\ - $(patsubst %.h, qt_%, $(filter %.h, $(src))) - -yacc_roots := $(patsubst %.y, %.tab, $(filter %.y, $(src))) -lex_roots := $(patsubst %.l, %.yy, $(filter %.l, $(src))) -obj := $(patsubst %, $(version)/%.o, $(roots) $(yacc_roots) $(lex_roots)) -dep := $(patsubst %, $(version)/%.d, $(roots) $(yacc_roots) $(lex_roots)) - - -ifdef exe #>>>>>>>>>>>> We are building an executable <<<<<<<<<<<<<<<< - -ifndef mainType -$(version)/$(exe) : $(obj) - @echo 'THE VARIABLE "mainType" MUST BE DEFINED TO: CXX or CC or FC' -endif - -ifeq ($(mainType),CXX) -$(version)/$(exe) : $(obj) - $(CXX) $(LDFLAGS) $(local_ldflags) $(obj) $(patsubst %, -l%, $(libs)) -o $@ -endif # ifeq CXX - -ifeq ($(mainType),CC) -$(version)/$(exe) : $(obj) - $(CC) $(LDFLAGS) $(local_ldflags) $(obj) $(patsubst %, -l%, $(libs)) -o $@ -endif # ifeq CC - -ifeq ($(mainType),FC) -$(version)/$(exe) : $(obj) - $(FC) $(LDFLAGS) $(local_ldflags) $(obj) $(patsubst %, -l%, $(libs)) -o $@ -endif # ifeq FC -endif # ifdef exe - - -ifdef library #>>>>>>>>>>>> We are building a library <<<<<<<<<<<<<<<< -ifeq ($(libType),STATIC) -ifdef libsDir -$(libsDir)/$(version)/lib$(library).a : $(version)/lib$(library).a - @[ -d $(libsDir)/$(version) ] || mkdir -p $(libsDir)/$(version) - @ln -f -s `pwd`/$(version)/lib$(library).a $(libsDir)/$(version)/lib$(library).a - -endif # ifdef libsDir - -$(version)/lib$(library).a : $(obj) - $(AR) $(ARFLAGS) $@ $(obj) -endif # ifeq STATIC - -ifeq ($(libType),SHARED) -ifdef libsDir -$(libsDir)/$(version)/lib$(library) : $(version)/lib$(library) - @[ -d $(libsDir)/$(version) ] || mkdir -p $(libsDir)/$(version) - @ln -f -s `pwd`/$(version)/lib$(library) $(libsDir)/$(version)/lib$(library) - -endif # ifdef libsDir -$(version)/lib$(library) : $(obj) - g++ -shared -Wl,-soname,lib$(library) -o $@ $(obj) - -local_codeflags += -fno-strength-reduce -fPIC -endif # ifeq SHARED - -endif # ifdef library -#>>>>>>>>>>>>>>>>>>>> Finished library specific stuff <<<<<<<<<<<<<<<<< - -# yacc stuff -yacc_h_output := $(patsubst %, %.h, $(yacc_roots)) -yacc_c_output := $(patsubst %, %.c, $(yacc_roots)) -yacc_output := $(yacc_h_output) $(yacc_c_output) - -%.tab.c : %.y - bison -d $< -%.tab.h : %.y - bison -d $< - -# lex stuff -lex_h_output := $(patsubst %, %.h, $(lex_roots)) -lex_c_output := $(patsubst %, %.c, $(lex_roots)) -lex_output := $(lex_h_output) $(lex_c_output) - -%.yy.c: %.l - flex -o $*.yy.c --header-file=$*.yy.h $< -%.yy.h: %.l - flex -o $*.yy.c --header-file=$*.yy.h $< - -# Make sure the build directory exists -$(dep) : | $(version) - -$(version) : - @mkdir $(version) - -# Dependency files rule -$(dep) : $(yacc_output) $(lex_output) - -# Recipes to build .d files -$(version)/%.d: %.cc - @set -e; $(CXX) -M $(CPPFLAGS) $(local_cppflags) $< \ - | sed 's/\($*\)\.o[ :]*/$(version)\/\1.o $(version)\/\1.d : /' > $@ -$(version)/%.d: %.cxx - @set -e; $(CXX) -M $(CPPFLAGS) $(local_cppflags) $< \ - | sed 's/\($*\)\.o[ :]*/$(version)\/\1.o $(version)\/\1.d : /' > $@ -$(version)/%.d: %.cpp - @set -e; $(CXX) -M $(CPPFLAGS) $(local_cppflags) $< \ - | sed 's/\($*\)\.o[ :]*/$(version)\/\1.o $(version)\/\1.d : /' > $@ -$(version)/%.d: %.C - @set -e; $(CXX) -M $(CPPFLAGS) $(local_cppflags) $< \ - | sed 's/\($*\)\.o[ :]*/$(version)\/\1.o $(version)\/\1.d : /' > $@ -$(version)/%.d: %.c - @set -e; $(CC) -M $(CPPFLAGS) $(local_cppflags) $< \ - | sed 's/\($*\)\.o[ :]*/$(version)\/\1.o $(version)\/\1.d : /' > $@ - -$(version)/%.d: %.f - @echo $(patsubst %.f, $(version)/%.o, $<) : $< > $@ - -$(version)/%.d: %.for - @echo $(patsubst %.for, $(version)/%.o, $<) : $< > $@ - -$(version)/qt_%.d: %.h - @echo $(patsubst %.h, $(version)/qt_%.cxx, $<) : $< > $@ - -$(version)/%.d: %.sal - @echo $(patsubst %.sal, $(version)/%.s, $<) : $< > $@ - -$(version)/%.d: %.asm - @echo $(patsubst %.asm, $(version)/%.s, $<) : $< > $@ - -# The .d files contain specific prerequisite dependencies --include $(patsubst %, $(version)/%.d, $(roots) $(yacc_roots) $(lex_roots)) - -# Recipes to build object files -$(version)/%.o: %.cc - $(CXX) -c $(CXXFLAGS) $(local_codeflags) $(CPPFLAGS) $(local_cppflags) $< -o $@ -$(version)/%.o: %.cxx - $(CXX) -c $(CXXFLAGS) $(local_codeflags) $(CPPFLAGS) $(local_cppflags) $< -o $@ -$(version)/%.o: %.cpp - $(CXX) -c $(CXXFLAGS) $(local_codeflags) $(CPPFLAGS) $(local_cppflags) $< -o $@ -$(version)/%.o: %.C - $(CXX) -c $(CXXFLAGS) $(local_codeflags) $(CPPFLAGS) $(local_cppflags) $< -o $@ -$(version)/%.o: %.c - $(CC) -c $(CCFLAGS) $(local_codeflags) $(CPPFLAGS) $(local_cppflags) $< -o $@ -$(version)/%.o: %.f - $(FC) -c $(FFLAGS) $(local_codeflags) $< -o $@ -$(version)/%.o: %.for - $(FC) -c $(FFLAGS) $(local_codeflags) $< -o $@ -$(version)/qt_%.o: %.h - $(QTDIR)/bin/moc $< -o $(version)/qt_$*.cxx - $(CXX) -c $(CXXFLAGS) $(local_codeflags) $(CPPFLAGS) $(local_cppflags) $(version)/qt_$*.cxx -o $(version)/qt_$*.o - -endif # version diff --git a/maxoff.c b/maxoff.c index 17a7d28..544c383 100644 --- a/maxoff.c +++ b/maxoff.c @@ -18,6 +18,7 @@ ***************************************************************************/ #include +#include "ban2fail.h" #include "cntry.h" #include "maxoff.h" @@ -35,7 +36,7 @@ initialize(void) { S.is_init= 1; MAP_constructor(&S.cntry_map, 10, 10); - MAP_constructor(&S.addr_map, 1000, 200); + MAP_constructor(&S.addr_map, N_ADDRESSES_HINT/10, 10); } // Compiler doesn't like that we use integers in place of item pointers */