From 6f1ac1cbbe622d1416632645d4d8f0630b70266b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rafa=C5=82=20D=C5=82ugo=C5=82=C4=99cki?= Date: Wed, 10 Jul 2013 14:44:30 +0200 Subject: [PATCH] Fixes vlp-12. Replaced custom config file reader with libconfig implementation. Created stub class for future changes in config reader. --- Makefile.am | 4 +- configure.ac | 4 +- src/global/AppConfiguration.cpp | 9 +++ src/global/AppConfiguration.h | 10 ++++ src/kernel/kernel.cpp | 94 +++++++++++++++++++++---------- src/net/lognet.cpp | 99 +++++++++++++++++++++------------ 6 files changed, 150 insertions(+), 70 deletions(-) create mode 100644 src/global/AppConfiguration.cpp create mode 100644 src/global/AppConfiguration.h diff --git a/Makefile.am b/Makefile.am index e8624a5..f802c53 100644 --- a/Makefile.am +++ b/Makefile.am @@ -37,7 +37,7 @@ src/graph/loggraph.moc: clean-loggraph-extra: rm -f src/graph/*.moc.cpp -bin_lognet_SOURCES = src/net/lognet.cpp src/net/lognet.moc.cpp +bin_lognet_SOURCES = src/net/lognet.cpp src/net/lognet.moc.cpp src/global/AppConfiguration.cpp bin_lognet_CPPFLAGS = $(bin_lognet_CFLAGS) bin_lognet_LDADD = $(bin_lognet_LIBS) src/net/lognet.moc.cpp: @@ -46,7 +46,7 @@ src/net/lognet.moc.cpp: clean-lognet-extra: rm -f src/net/*.moc.cpp -bin_logker_SOURCES = src/kernel/kernel.cpp src/kernel/kernel.moc +bin_logker_SOURCES = src/kernel/kernel.cpp src/kernel/kernel.moc src/global/AppConfiguration.cpp bin_logker_CPPFLAGS = $(bin_logker_CFLAGS) bin_logker_LDADD = $(bin_logker_LIBS) src/kernel/kernel.moc: diff --git a/configure.ac b/configure.ac index b1be6df..2146fb1 100644 --- a/configure.ac +++ b/configure.ac @@ -12,8 +12,8 @@ AC_CONFIG_FILES([Makefile]) # libqt3-mt-dev:i386 # qt3-dev-tools:i386 PKG_CHECK_MODULES([bin_loggraph], [qt-mt < 4.0]) -PKG_CHECK_MODULES([bin_lognet], [qt-mt < 4.0]) -PKG_CHECK_MODULES([bin_logker], [qt-mt < 4.0]) +PKG_CHECK_MODULES([bin_lognet], [qt-mt < 4.0 libconfig]) +PKG_CHECK_MODULES([bin_logker], [qt-mt < 4.0 libconfig]) #PKG_CHECK_MODULES([bin_logint], [qt-mt < 4.0]) PKG_CHECK_MODULES([bin_logedit], [qt-mt < 4.0]) PKG_CHECK_MODULES([bin_lgconfig], [qt-mt < 4.0]) diff --git a/src/global/AppConfiguration.cpp b/src/global/AppConfiguration.cpp new file mode 100644 index 0000000..b29b76e --- /dev/null +++ b/src/global/AppConfiguration.cpp @@ -0,0 +1,9 @@ +#include "AppConfiguration.h" + +void AppConfiguration::error(config_t *cfg) +{ + fprintf(stderr, "%s: In file %s, line %d\n", + config_error_text(cfg), + config_error_file(cfg), + config_error_line(cfg)); +} diff --git a/src/global/AppConfiguration.h b/src/global/AppConfiguration.h new file mode 100644 index 0000000..23e2eb0 --- /dev/null +++ b/src/global/AppConfiguration.h @@ -0,0 +1,10 @@ +#include + +/** + * Class for maintaining application configuration. + */ +class AppConfiguration { +protected: +public: + static void error(config_t *cfg); +}; diff --git a/src/kernel/kernel.cpp b/src/kernel/kernel.cpp index ad259d3..7e83ebc 100644 --- a/src/kernel/kernel.cpp +++ b/src/kernel/kernel.cpp @@ -71,6 +71,9 @@ #include "socu.h" #include +#include +#include "AppConfiguration.h" + #define GPATH "loggr" #define IPATH "logi" #define NPATH "logn" @@ -181,7 +184,7 @@ private: bool info_messages; - void LoadConfig(); + void LoadConfig(char *); void RunGraphModule(char*); void RunNetModule(); InterpEntry *findINTbySocket(int); @@ -262,7 +265,7 @@ QKernel::QKernel() freeINTid = 1; ActiveConnections = 0; strcpy(LockPasswd,""); - LoadConfig(); + LoadConfig("vlp.cfg"); RunNetModule(); Net_Notify = new QSocketNotifier(net_sock,QSocketNotifier::Read,this); @@ -289,36 +292,69 @@ void QKernel::n_impl() /* ########### load configuration from file ############# */ -void QKernel::LoadConfig() +void QKernel::LoadConfig(char * fname) { -QFile f("vlp.cfg"); - QString line; - QString val; - int br=0; + config_t cfg; + const char *str; - if (!f.exists()) - { - WriteMessage("Cannot load configuration file!");sleep(2);exit(3); + config_init(&cfg); + + /* Hack for checking if file exists without using external libs.*/ + FILE * file = fopen(fname, "r"); + if (!file) { + fprintf(stderr, "Error: Cannot load configuration file %s!\n", fname); + exit(3); } - f.open(IO_ReadOnly); - br = f.readLine(line,256); - while (br>0) - { - QStringList l = QStringList::split("=",line,FALSE); - QStringList::Iterator it = l.begin(); - line = *it; - ++it; - val = *it; - val = val.stripWhiteSpace(); - if (line == "node_number") {NodeNumber = val.toInt();}; - if (line == "type") {if (val=="explicit") ConType=1; else - ConType = 2; }; - if (line == "host" ) {ConnectList.append(new ConnectEntry((char*)val.ascii()));}; - if (line == "progdir") { strcpy(progdir,val.ascii());}; - if (line == "homedir") { strcpy(HomeDir,val.ascii());}; - br = f.readLine(line,256); - } - f.close(); + /* File exists, so file has been locked. Release it. */ + fclose(file); + + /* Read the file. If there is an error, report it and exit. */ + if(!config_read_file(&cfg, fname)) + { + AppConfiguration::error(&cfg); + config_destroy(&cfg); + exit(3);/* from original code. */ + } + + if(!config_lookup_int(&cfg, "node_number", &NodeNumber)) + { + AppConfiguration::error(&cfg); + config_destroy(&cfg); + exit(3); + } + + if(config_lookup_string(&cfg, "type", &str)){ + ConType = (strcmp(str, "explicit") == 0) ? 1 : 2; + } + else { + AppConfiguration::error(&cfg); + } + + + if(config_lookup_string(&cfg, "host", &str)) { + char host[255]; + strcpy(host, str);//FIXME: buffer overflow + ConnectList.append(new ConnectEntry(host)); + } + else { + AppConfiguration::error(&cfg); + } + + if(config_lookup_string(&cfg, "progdir", &str)){ + strcpy(progdir, str);//FIXME: buffer overflow + } + else { + AppConfiguration::error(&cfg); + } + + if(config_lookup_string(&cfg, "homedir", &str)){ + strcpy(HomeDir, str);//FIXME: buffer overflow + } + else { + AppConfiguration::error(&cfg); + } + + config_destroy(&cfg); } /* +++++++++++++++++++++++++++++++++++++++++++++++ */ diff --git a/src/net/lognet.cpp b/src/net/lognet.cpp index dda6733..fce3aee 100644 --- a/src/net/lognet.cpp +++ b/src/net/lognet.cpp @@ -22,6 +22,9 @@ #include #include +#include +#include "AppConfiguration.h" + #define REMOTE_PATH "REMOTE" #define MAXLINKS 30 #define LOGPORT 3600 @@ -183,44 +186,66 @@ NETMOD::NETMOD(char *kernel_name) void NETMOD::load_config(char *fname) { - QFile f(fname); - QString line; - QString val; - int br=0,on,k=0; - NETlink *pomlink; + config_t cfg; + const char *str; + int on,k=0; + NETlink *pomlink; - if (!f.exists()) - { - write_at_console("Cannot load configuration file!");sleep(2);exit(3); - } - f.open(IO_ReadOnly); - br = f.readLine(line,256); - while (br>0) - { - QStringList l = QStringList::split("=",line,FALSE); - QStringList::Iterator it = l.begin(); - line = *it; - ++it; - val = *it; - val = val.stripWhiteSpace(); - if (line == "node_number") {MyNode = val.toInt();}; - if (line == "host" ) { - k++; - pomlink = new NETlink; - strcpy(pomlink->addr,val.ascii()); - pomlink->connected = FALSE; - pomlink->sock = socket(AF_INET, SOCK_STREAM, 0); - fcntl(pomlink->sock, F_SETFL,O_NONBLOCK | fcntl(pomlink->sock,F_GETFL,0)); - on=1; - setsockopt(pomlink->sock,IPPROTO_TCP,TCP_NODELAY,(char*)&on,sizeof(on)); - Links.append(pomlink); - to_connect++; - }; - br = f.readLine(line,256); - } - f.close(); - if (k==0) all_connected=TRUE; - if (MyNode==-1) {write_at_console("Node number must be specified");exit(1);}; + config_init(&cfg); + + + /* Hack for checking if file exists without using external libs.*/ + FILE * file = fopen(fname, "r"); + if (!file) { + fprintf(stderr, "Error: Cannot load configuration file %s!\n", fname); + write_at_console("Cannot load configuration file!"); + exit(3); + } + /* File exists, so file has been locked. Release it. */ + fclose(file); + + /* Read the file. If there is an error, report it and exit. */ + if(!config_read_file(&cfg, fname)) + { + AppConfiguration::error(&cfg); + config_destroy(&cfg); + exit(3);/* from original code. */ + } + + if(config_lookup_int(&cfg, "node_number", &MyNode)) + { + if (MyNode==-1) { + write_at_console("Node number must be specified"); + config_destroy(&cfg); + exit(1); + }; + } + else + { + AppConfiguration::error(&cfg); + config_destroy(&cfg); + exit(1); + } + + if(config_lookup_string(&cfg, "host", &str)) { + k++; + pomlink = new NETlink; + strcpy(pomlink->addr, str); + pomlink->connected = FALSE; + pomlink->sock = socket(AF_INET, SOCK_STREAM, 0); + fcntl(pomlink->sock, F_SETFL,O_NONBLOCK | fcntl(pomlink->sock,F_GETFL,0)); + on=1; + setsockopt(pomlink->sock,IPPROTO_TCP,TCP_NODELAY,(char*)&on,sizeof(on)); + Links.append(pomlink); + to_connect++; + } + else { + AppConfiguration::error(&cfg); + } + + config_destroy(&cfg); + + if (k==0) all_connected=TRUE; } -- 2.30.2