From 3435eab40d6141c7d103609684e0af75ad098c13 Mon Sep 17 00:00:00 2001 From: cora48 Date: Wed, 18 Mar 2015 17:28:39 +0300 Subject: [PATCH] Thread pooling refac --- Threader.cpp | 64 ++++++++-- Threader.h | 28 +++-- mainResources.h | 4 - nesca.pro.user | 2 +- nesca_3.cpp | 2 + nesca_startModule.cpp | 280 ++++++++++++++++++++++++++---------------- 6 files changed, 249 insertions(+), 131 deletions(-) diff --git a/Threader.cpp b/Threader.cpp index 995e338..f222d12 100644 --- a/Threader.cpp +++ b/Threader.cpp @@ -1,21 +1,59 @@ #include -std::vector Threader::threadPool; +std::vector Threader::threadPool; +int Threader::threadPoolSize; +bool Threader::mayRun; +void *Threader::lFunc; -void Threader::createThreadPool(int poolSize, void *func, ST *st) { - for(int i = 0; i < poolSize; ++i) { - pthread_t thrc; - pthread_create(&thrc, NULL, (void *(*)(void*))func, st); - - ThreadStruct threadStruct { - &thrc, - false - }; - - threadPool.push_back(threadStruct); +void Threader::recreateThreadPool(int poolSize) { + if(mayRun) { + createThreadPool(poolSize, (void* (*)(int))lFunc); } } -void Threader::fireThread(ST *st) { +void Threader::createThreadPool(int poolSize, void *func(int)) { + cleanUp(); + lFunc = (void*)func; + threadPoolSize = poolSize; + for(int i = 0; i < threadPoolSize; ++i) { + char *res = NULL; + threadPool.push_back(res); + std::thread thr12(func, i); + thr12.detach(); +//#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) +// _beginthread((void(*)(void*))_connect, 0, (void *)i); +//#else +// pthread_t thrc; +// pthread_create(&thrc, NULL, (void *(*)(void*))func, (void *)i); +//#endif + Sleep(1); + } + mayRun = true; +} + +int Threader::getFreeDataSlotId() { + for(int i = 0; i != threadPoolSize; ++i) { + if(threadPool[i] == NULL) return i; + } + return -1; +} + +int Threader::getFreeThreadId() { + int res; + while((res = getFreeDataSlotId()) < 0) Sleep(10); + return res; +} + +void Threader::cleanUp() { + mayRun = false; + //for(int i = 0; i != threadPoolSize; ++i) { + //if(threadPool[i] != NULL) delete threadPool[i]; + //} + threadPool.clear(); +} + +void Threader::fireThread(char *res) { + //while(!mayRun) sleep(10); + threadPool[getFreeThreadId()] = res; } diff --git a/Threader.h b/Threader.h index 55dae0d..3eced75 100644 --- a/Threader.h +++ b/Threader.h @@ -2,20 +2,30 @@ #define THREADER_H #include +#include -struct ThreadStruct{ - pthread_t *handler; - bool busy; -}; +//typedef struct { +// char argv[MAX_ADDR_LEN] = {0}; +//} ST; class Threader { -private: - static std::vector threadPool; +private: + static int threadPoolSize; + static void* lFunc; public: - static void createThreadPool(int poolSize, void *func, ST *st); - static void fireThread(ST *st); - static pthread_t getFreeThread(); + static std::vector threadPool; + static bool mayRun; + +private: + static int getFree(); + static int getFreeDataSlotId(); + static int getFreeThreadId(); +public: + static void recreateThreadPool(int poolSize); + static void createThreadPool(int poolSize, void *func(int)); + static void fireThread(char *st); + static void cleanUp(); }; #endif // THREADER_H diff --git a/mainResources.h b/mainResources.h index 0f7e36c..f6c67e9 100644 --- a/mainResources.h +++ b/mainResources.h @@ -88,10 +88,6 @@ typedef int BOOL; #define COOKIE_MAX_SIZE 1024 #define RESULT_DIR_NAME "./result_files-" __DATE__ -typedef struct { - char argv[MAX_ADDR_LEN]; -} ST; - struct PathStr{ char codepage[32]; char headr[TITLE_MAX_SIZE]; diff --git a/nesca.pro.user b/nesca.pro.user index d6b82d9..e27e0d7 100644 --- a/nesca.pro.user +++ b/nesca.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId diff --git a/nesca_3.cpp b/nesca_3.cpp index e3f97b8..7fef636 100644 --- a/nesca_3.cpp +++ b/nesca_3.cpp @@ -26,6 +26,7 @@ #include "progressbardrawer.h" #include "externFunctions.h" #include "externData.h" +#include "Threader.h" QDate date = QDate::currentDate(); int ver = 100*(100*(date.year()%100) + date.month()) + date.day(); @@ -3571,6 +3572,7 @@ void nesca_3::ChangeLabelTO_ValueChanged(QString str) void nesca_3::ChangeLabelThreads_ValueChanged(QString str) { gThreads = str.toInt(); + //Threader::recreateThreadPool(gThreads); } void nesca_3::PingTO_ChangeValue(QString str) diff --git a/nesca_startModule.cpp b/nesca_startModule.cpp index 7cbac1f..b82c623 100644 --- a/nesca_startModule.cpp +++ b/nesca_startModule.cpp @@ -3,8 +3,10 @@ #include "externData.h" #include "externFunctions.h" #include "Connector.h" +#include "Threader.h" +#include -ST *st = NULL; +//ST *st = NULL; QJsonArray *jsonArr = new QJsonArray(); @@ -880,31 +882,99 @@ unsigned long int numOfIps(int ipsstart[], int ipsend[]) // return res; //} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +//#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) +//void _connect(int ss) +//#else +//void _connect(int ss) +//#endif +//{ +// while(globalScanFlag) { +// while(Threader::threadPool[ss] == NULL) { +// if(!globalScanFlag) return; +// Sleep(10); +// } + +// ++ipCounter; +// //char ip[MAX_ADDR_LEN] = {0}; +// //strcpy(ip, Threader::threadPool[ss]); +// //char hostLog[256] = {0}; +// //strcpy(hostLog, GetHost(ip)); +// //delete Threader::threadPool[ss]; + +// ConInc(); + +// for(int i = 0; i <= overallPorts; ++i) +// { +// if(globalScanFlag == false) break; +// //if(Connector::_ConnectToPort( ip, portArr[i], "" ) == -2) break; +// //if(Connector::_ConnectToPort( Threader::threadPool[ss], portArr[i], "" ) == -2) break; +// }; + +// ConDec(); + +// Threader::threadPool[ss] = NULL; +// } +//} + #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) -void _connect(void* ss) +void _connect(char* ip) #else -void *_connect(void* ss) +void _connect(char* ip) #endif -{ - ++ipCounter; - char ip[MAX_ADDR_LEN] = {0}; - strcpy(ip, ((ST*)ss)->argv); - //char hostLog[256] = {0}; - //strcpy(hostLog, GetHost(ip)); - delete (ST*)ss; +{ + ++ipCounter; + ConInc(); - ConInc(); + for(int i = 0; i <= overallPorts; ++i) + { + if(globalScanFlag == false) break; + if(Connector::_ConnectToPort( ip, portArr[i], "" ) == -2) break; + }; - for(int i = 0; i <= overallPorts; ++i) - { - if(globalScanFlag == false) break; - if(Connector::_ConnectToPort( ip, portArr[i], "" ) == -2) break; - }; - - ConDec(); + ConDec(); } -void targetAndIPWriter(long long unsigned int target, char *buff) +void targetAndIPWriter(long long unsigned int target, const char *buff) { char curIPBuff[256] = {0}, targetNPers[32] = {0}; @@ -1585,7 +1655,7 @@ int fInit(int InitMode, char *gR) if(ipsend[i] > 255) { - stt->doEmitionRedFoundData("[Error] Incorrect range."); + stt->doEmitionRedFoundData("[Error] Incorrect range."); stt->doEmitionKillSttThread(); return -1; }; @@ -1605,7 +1675,7 @@ int fInit(int InitMode, char *gR) ) ) { - stt->doEmitionRedFoundData("[Error] Incorrect range."); + stt->doEmitionRedFoundData("[Error] Incorrect range."); stt->doEmitionKillSttThread(); return -1; }; @@ -2229,14 +2299,14 @@ int _GetDNSFromMask(char *mask, char *saveMask, char *saveMaskEnder) } else { - if(globalScanFlag == false) return 0; strcpy(endIP2, saveMask); - st = new ST(); - ZeroMemory(st->argv, sizeof(st->argv)); + //st = new ST(); + //ZeroMemory(st->argv, sizeof(st->argv)); ZeroMemory(iip, sizeof(iip)); while(cons >= gThreads && globalScanFlag) Sleep(300); + if(globalScanFlag == false) return 0; strcpy(iip, mask); strcpy(saveStartIP, iip); @@ -2244,19 +2314,24 @@ int _GetDNSFromMask(char *mask, char *saveMask, char *saveMaskEnder) ++indexIP; - strcpy(st->argv, iip); + char res[256] = {0}; + strcpy(res, iip); - targetAndIPWriter(--gTargets, st->argv); + targetAndIPWriter(--gTargets, res); -#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) - if(globalScanFlag) _beginthread( (void(*)(void*))_connect, 0, st ); -#else - if(globalScanFlag) - { - pthread_t thrc; - pthread_create(&thrc, NULL, (void *(*)(void*))&_connect, st ); - }; -#endif +// std::thread thr(_connect, res); +// thr.detach(); + + //Threader::fireThread(st); +//#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) +// if(globalScanFlag) _beginthread( (void(*)(void*))_connect, 0, st ); +//#else +// if(globalScanFlag) +// { +// pthread_t thrc; +// pthread_create(&thrc, NULL, (void *(*)(void*))&_connect, st ); +// }; +//#endif Sleep(gThreadDelay); }; @@ -2269,6 +2344,8 @@ int startScan(char* args) OpenSSL_add_all_algorithms(); /* Load cryptos, et.al. */ SSL_load_error_strings(); /* Bring in and register error messages */ +// Threader::createThreadPool(gThreads, (void* (*)(int))_connect); + horLineFlag = false; flCounter = 0; PieAnomC1 = 0, PieWF = 0, PieBA = 0, PieSusp = 0, PieLowl = 0, PieSSH = 0; @@ -2309,7 +2386,7 @@ int startScan(char* args) int resInit = fInit(gMode, gRange); if(resInit == -1 ) { - stt->doEmitionRedFoundData("[Error] fInit failure"); + stt->doEmitionRedFoundData("[Error] fInit failure"); stt->doEmitionKillSttThread(); return -1; @@ -2350,11 +2427,11 @@ stt->doEmitionThreads(QString::number(0) + "/" + QString::number(gThreads)); struct in_addr tAddr; for (unsigned long i = ip1; i <= ip2; ++i) { + if (globalScanFlag == false) break; unsigned long offset = ip2 - i; - tAddr.s_addr = i; - + tAddr.s_addr = i; #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) ipVec.push_back(std::to_string(tAddr.S_un.S_un_b.s_b4) + "." + std::to_string(tAddr.S_un.S_un_b.s_b3) @@ -2362,32 +2439,28 @@ stt->doEmitionThreads(QString::number(0) + "/" + QString::number(gThreads)); + "." + std::to_string(tAddr.S_un.S_un_b.s_b1)); #else tAddr.s_addr = ntohl(tAddr.s_addr); - const char *ipStr = inet_ntoa(tAddr); - ipVec.push_back((char*)ipStr); + ipVec.push_back(inet_ntoa(tAddr)); #endif if (ipVec.size() >= (offset < 1000 ? offset : 1000)) { std::random_shuffle(ipVec.begin(), ipVec.end()); while (ipVec.size() != 0) { - if (globalScanFlag == false) goto haters_gonna_hate_IPM; - st = new ST(); - ZeroMemory(st->argv, sizeof(st->argv)); - while (cons >= gThreads && globalScanFlag) Sleep(500); - ++indexIP; - strcpy(st->argv, ipVec[0].c_str()); - strcpy(saveStartIP, ipVec[0].c_str()); + if (globalScanFlag == false) goto haters_gonna_hate_IPM; + //st = new ST(); + //ZeroMemory(st->argv, MAX_ADDR_LEN); + ++indexIP; + //strcpy(st->argv, ipVec[0].c_str()); + strcpy(res, ipVec[0].c_str()); + strcpy(saveStartIP, res); ipVec.erase(ipVec.begin()); - targetAndIPWriter(gTargets--, st->argv); + targetAndIPWriter(gTargets--, res); -#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) - _beginthread((void(*)(void*))_connect, 0, st); -#else - pthread_t thrc; - pthread_create(&thrc, NULL, (void *(*)(void*))&_connect, st); -#endif + std::thread thr(_connect, res); + thr.detach(); + //Threader::fireThread(res); Sleep(gThreadDelay); } } @@ -2398,12 +2471,13 @@ stt->doEmitionThreads(QString::number(0) + "/" + QString::number(gThreads)); } case false: { struct in_addr tAddr; - for (unsigned long i = ip1; i <= ip2; ++i) { - if (globalScanFlag == false) break; - st = new ST(); - ZeroMemory(st->argv, sizeof(st->argv)); - ZeroMemory(res, sizeof(res)); + for (unsigned long i = ip1; i <= ip2; ++i) { + while (cons >= gThreads && globalScanFlag) Sleep(500); + if (globalScanFlag == false) break; + //st = new ST(); + //ZeroMemory(st->argv, sizeof(st->argv)); + ZeroMemory(res, sizeof(res)); ++indexIP; tAddr.s_addr = i; @@ -2413,19 +2487,19 @@ stt->doEmitionThreads(QString::number(0) + "/" + QString::number(gThreads)); + "." + std::to_string(tAddr.S_un.S_un_b.s_b2) + "." + std::to_string(tAddr.S_un.S_un_b.s_b1)).c_str()); #else + tAddr.s_addr = ntohl(tAddr.s_addr); strcpy(res, inet_ntoa(tAddr)); #endif - strcpy(st->argv, res); + //strcpy(st->argv, res); strcpy(saveStartIP, res); - targetAndIPWriter(gTargets--, st->argv); + targetAndIPWriter(gTargets--, res); + +// std::thread thr(_connect, res); +// thr.detach(); + + //Threader::fireThread(st); -#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) - _beginthread((void(*)(void*))_connect, 0, st); -#else - pthread_t thrc; - pthread_create(&thrc, NULL, (void *(*)(void*))&_connect, st); -#endif Sleep(gThreadDelay); } break; @@ -2578,7 +2652,7 @@ stt->doEmitionThreads(QString::number(0) + "/" + QString::number(gThreads)); if (flCounter == 0) { stt->doEmitionRedFoundData("Empty IP list."); - globalScanFlag = false; + globalScanFlag = false; stt->doEmitionKillSttThread(); return -1; @@ -2626,8 +2700,8 @@ stt->doEmitionThreads(QString::number(0) + "/" + QString::number(gThreads)); strcat(metaRange, "."); strcat(metaRange, std::to_string(ipsendfl[gC][3]).c_str()); - unsigned long ip1 = (ipsstartfl[gC][0] * 16777216) + (ipsstartfl[gC][1] * 65536) + (ipsstartfl[gC][2] * 256) + ipsstartfl[gC][3]; - unsigned long ip2 = (ipsendfl[gC][0] * 16777216) + (ipsendfl[gC][1] * 65536) + (ipsendfl[gC][2] * 256) + ipsendfl[gC][3]; + unsigned long ip1 = (ipsstartfl[gC][0] * 16777216) + (ipsstartfl[gC][1] * 65536) + (ipsstartfl[gC][2] * 256) + ipsstartfl[gC][3]; + unsigned long ip2 = (ipsendfl[gC][0] * 16777216) + (ipsendfl[gC][1] * 65536) + (ipsendfl[gC][2] * 256) + ipsendfl[gC][3]; switch (gShuffle) { case true: { @@ -2635,39 +2709,37 @@ stt->doEmitionThreads(QString::number(0) + "/" + QString::number(gThreads)); struct in_addr tAddr; for (unsigned long i = ip1; i <= ip2; ++i) { + if (globalScanFlag == false) break; - int offset = ip2 - i; + unsigned long offset = ip2 - i; tAddr.s_addr = i; #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) - ipVec.push_back(std::to_string(tAddr.S_un.S_un_b.s_b4) + "." + std::to_string(tAddr.S_un.S_un_b.s_b3) + "." + std::to_string(tAddr.S_un.S_un_b.s_b2) + "." + std::to_string(tAddr.S_un.S_un_b.s_b1)); + ipVec.push_back(std::to_string(tAddr.S_un.S_un_b.s_b4) + "." + std::to_string(tAddr.S_un.S_un_b.s_b3) + "." + std::to_string(tAddr.S_un.S_un_b.s_b2) + "." + std::to_string(tAddr.S_un.S_un_b.s_b1)); #else - ipVec.push_back(inet_ntoa(tAddr)); + tAddr.s_addr = ntohl(tAddr.s_addr); + ipVec.push_back(inet_ntoa(tAddr)); #endif if (ipVec.size() >= (offset < 1000 ? offset : 1000)) { std::random_shuffle(ipVec.begin(), ipVec.end()); while (ipVec.size() != 0) { - if (globalScanFlag == false) goto haters_gonna_hate_IM; - - st = new ST(); - ZeroMemory(st->argv, sizeof(st->argv)); - while (cons >= gThreads && globalScanFlag) Sleep(500); + if (globalScanFlag == false) goto haters_gonna_hate_IM; + //st = new ST(); + //ZeroMemory(st->argv, MAX_ADDR_LEN); ++indexIP; - strcpy(st->argv, ipVec[0].c_str()); - strcpy(saveStartIP, ipVec[0].c_str()); + strcpy(res, ipVec[0].c_str()); + strcpy(saveStartIP, res); ipVec.erase(ipVec.begin()); - targetAndIPWriter(gTargets--, st->argv); + targetAndIPWriter(gTargets--, res); -#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) - _beginthread((void(*)(void*))_connect, 0, st); -#else - pthread_t thrc; - pthread_create(&thrc, NULL, (void *(*)(void*))&_connect, st); -#endif +// std::thread thr(_connect, res); +// thr.detach(); + + //Threader::fireThread(st); Sleep(gThreadDelay); } } @@ -2677,33 +2749,33 @@ stt->doEmitionThreads(QString::number(0) + "/" + QString::number(gThreads)); } case false: { struct in_addr tAddr; - for (unsigned long i = ip1; i <= ip2; ++i) { - if (globalScanFlag == false) break; - st = new ST(); - ZeroMemory(st->argv, sizeof(st->argv)); - ZeroMemory(res, sizeof(res)); + for (unsigned long i = ip1; i <= ip2; ++i) { + while (cons >= gThreads && globalScanFlag) Sleep(500); + if (globalScanFlag == false) break; + //st = new ST(); + //ZeroMemory(st->argv, sizeof(st->argv)); + ZeroMemory(res, sizeof(res)); ++indexIP; - tAddr.s_addr = i; + tAddr.s_addr = i; #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) strcpy(res, (std::to_string(tAddr.S_un.S_un_b.s_b4) + "." + std::to_string(tAddr.S_un.S_un_b.s_b3) + "." + std::to_string(tAddr.S_un.S_un_b.s_b2) + "." + std::to_string(tAddr.S_un.S_un_b.s_b1)).c_str()); #else + tAddr.s_addr = ntohl(tAddr.s_addr); strcpy(res, inet_ntoa(tAddr)); #endif - strcpy(st->argv, res); + //strcpy(st->argv, res); strcpy(saveStartIP, res); - targetAndIPWriter(gTargets--, st->argv); + targetAndIPWriter(gTargets--, res); + +// std::thread thr(_connect, res); +// thr.detach(); + + //Threader::fireThread(st); -#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) - _beginthread((void(*)(void*))_connect, 0, st); -#else - pthread_t thrc; - pthread_create(&thrc, NULL, (void *(*)(void*))&_connect, st); -#endif Sleep(gThreadDelay); - } break; }; @@ -2724,8 +2796,6 @@ stt->doEmitionThreads(QString::number(0) + "/" + QString::number(gThreads)); while(cons > 0 || jsonArr->size() > 0) { Sleep(2000); }; - - nCleanup(); stt->doEmitionGreenFoundData("Done. Saved: " + QString::number(saved) + "; Alive: " + QString::number(found) + "."); stt->doEmitionChangeParsed(QString::number(saved) + "/" + QString::number(found)); @@ -2734,6 +2804,8 @@ stt->doEmitionThreads(QString::number(0) + "/" + QString::number(gThreads)); } void nCleanup(){ + Threader::cleanUp(); + if(loginLst != NULL) { for(int i = 0; i < MaxLogin; ++i) delete []loginLst[i];