Thread pooling refac

This commit is contained in:
cora48 2015-03-18 17:28:39 +03:00
parent f1e8b3a568
commit 3435eab40d
6 changed files with 249 additions and 131 deletions

View File

@ -1,21 +1,59 @@
#include <Threader.h> #include <Threader.h>
std::vector<ThreadStruct> Threader::threadPool; std::vector<char*> Threader::threadPool;
int Threader::threadPoolSize;
bool Threader::mayRun;
void *Threader::lFunc;
void Threader::createThreadPool(int poolSize, void *func, ST *st) { void Threader::recreateThreadPool(int poolSize) {
for(int i = 0; i < poolSize; ++i) { if(mayRun) {
pthread_t thrc; createThreadPool(poolSize, (void* (*)(int))lFunc);
pthread_create(&thrc, NULL, (void *(*)(void*))func, st);
ThreadStruct threadStruct {
&thrc,
false
};
threadPool.push_back(threadStruct);
} }
} }
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;
} }

View File

@ -2,20 +2,30 @@
#define THREADER_H #define THREADER_H
#include <mainResources.h> #include <mainResources.h>
#include <thread>
struct ThreadStruct{ //typedef struct {
pthread_t *handler; // char argv[MAX_ADDR_LEN] = {0};
bool busy; //} ST;
};
class Threader { class Threader {
private:
static std::vector<ThreadStruct> threadPool;
private:
static int threadPoolSize;
static void* lFunc;
public: public:
static void createThreadPool(int poolSize, void *func, ST *st); static std::vector<char *> threadPool;
static void fireThread(ST *st); static bool mayRun;
static pthread_t getFreeThread();
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 #endif // THREADER_H

View File

@ -88,10 +88,6 @@ typedef int BOOL;
#define COOKIE_MAX_SIZE 1024 #define COOKIE_MAX_SIZE 1024
#define RESULT_DIR_NAME "./result_files-" __DATE__ #define RESULT_DIR_NAME "./result_files-" __DATE__
typedef struct {
char argv[MAX_ADDR_LEN];
} ST;
struct PathStr{ struct PathStr{
char codepage[32]; char codepage[32];
char headr[TITLE_MAX_SIZE]; char headr[TITLE_MAX_SIZE];

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject> <!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 3.2.1, 2015-03-17T17:30:18. --> <!-- Written by QtCreator 3.2.1, 2015-03-18T17:28:02. -->
<qtcreator> <qtcreator>
<data> <data>
<variable>EnvironmentId</variable> <variable>EnvironmentId</variable>

View File

@ -26,6 +26,7 @@
#include "progressbardrawer.h" #include "progressbardrawer.h"
#include "externFunctions.h" #include "externFunctions.h"
#include "externData.h" #include "externData.h"
#include "Threader.h"
QDate date = QDate::currentDate(); QDate date = QDate::currentDate();
int ver = 100*(100*(date.year()%100) + date.month()) + date.day(); 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) void nesca_3::ChangeLabelThreads_ValueChanged(QString str)
{ {
gThreads = str.toInt(); gThreads = str.toInt();
//Threader::recreateThreadPool(gThreads);
} }
void nesca_3::PingTO_ChangeValue(QString str) void nesca_3::PingTO_ChangeValue(QString str)

View File

@ -3,8 +3,10 @@
#include "externData.h" #include "externData.h"
#include "externFunctions.h" #include "externFunctions.h"
#include "Connector.h" #include "Connector.h"
#include "Threader.h"
#include <thread>
ST *st = NULL; //ST *st = NULL;
QJsonArray *jsonArr = new QJsonArray(); QJsonArray *jsonArr = new QJsonArray();
@ -880,19 +882,87 @@ unsigned long int numOfIps(int ipsstart[], int ipsend[])
// return res; // 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__) #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
void _connect(void* ss) void _connect(char* ip)
#else #else
void *_connect(void* ss) void _connect(char* ip)
#endif #endif
{ {
++ipCounter; ++ipCounter;
char ip[MAX_ADDR_LEN] = {0};
strcpy(ip, ((ST*)ss)->argv);
//char hostLog[256] = {0};
//strcpy(hostLog, GetHost(ip));
delete (ST*)ss;
ConInc(); ConInc();
for(int i = 0; i <= overallPorts; ++i) for(int i = 0; i <= overallPorts; ++i)
@ -904,7 +974,7 @@ void *_connect(void* ss)
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}; char curIPBuff[256] = {0}, targetNPers[32] = {0};
@ -2229,14 +2299,14 @@ int _GetDNSFromMask(char *mask, char *saveMask, char *saveMaskEnder)
} }
else else
{ {
if(globalScanFlag == false) return 0;
strcpy(endIP2, saveMask); strcpy(endIP2, saveMask);
st = new ST(); //st = new ST();
ZeroMemory(st->argv, sizeof(st->argv)); //ZeroMemory(st->argv, sizeof(st->argv));
ZeroMemory(iip, sizeof(iip)); ZeroMemory(iip, sizeof(iip));
while(cons >= gThreads && globalScanFlag) Sleep(300); while(cons >= gThreads && globalScanFlag) Sleep(300);
if(globalScanFlag == false) return 0;
strcpy(iip, mask); strcpy(iip, mask);
strcpy(saveStartIP, iip); strcpy(saveStartIP, iip);
@ -2244,19 +2314,24 @@ int _GetDNSFromMask(char *mask, char *saveMask, char *saveMaskEnder)
++indexIP; ++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__) // std::thread thr(_connect, res);
if(globalScanFlag) _beginthread( (void(*)(void*))_connect, 0, st ); // thr.detach();
#else
if(globalScanFlag) //Threader::fireThread(st);
{ //#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
pthread_t thrc; // if(globalScanFlag) _beginthread( (void(*)(void*))_connect, 0, st );
pthread_create(&thrc, NULL, (void *(*)(void*))&_connect, st ); //#else
}; // if(globalScanFlag)
#endif // {
// pthread_t thrc;
// pthread_create(&thrc, NULL, (void *(*)(void*))&_connect, st );
// };
//#endif
Sleep(gThreadDelay); Sleep(gThreadDelay);
}; };
@ -2269,6 +2344,8 @@ int startScan(char* args)
OpenSSL_add_all_algorithms(); /* Load cryptos, et.al. */ OpenSSL_add_all_algorithms(); /* Load cryptos, et.al. */
SSL_load_error_strings(); /* Bring in and register error messages */ SSL_load_error_strings(); /* Bring in and register error messages */
// Threader::createThreadPool(gThreads, (void* (*)(int))_connect);
horLineFlag = false; horLineFlag = false;
flCounter = 0; flCounter = 0;
PieAnomC1 = 0, PieWF = 0, PieBA = 0, PieSusp = 0, PieLowl = 0, PieSSH = 0; PieAnomC1 = 0, PieWF = 0, PieBA = 0, PieSusp = 0, PieLowl = 0, PieSSH = 0;
@ -2350,11 +2427,11 @@ stt->doEmitionThreads(QString::number(0) + "/" + QString::number(gThreads));
struct in_addr tAddr; struct in_addr tAddr;
for (unsigned long i = ip1; i <= ip2; ++i) { for (unsigned long i = ip1; i <= ip2; ++i) {
if (globalScanFlag == false) break; if (globalScanFlag == false) break;
unsigned long offset = ip2 - i; unsigned long offset = ip2 - i;
tAddr.s_addr = i; tAddr.s_addr = i;
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
ipVec.push_back(std::to_string(tAddr.S_un.S_un_b.s_b4) 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_b3)
@ -2362,32 +2439,28 @@ stt->doEmitionThreads(QString::number(0) + "/" + QString::number(gThreads));
+ "." + std::to_string(tAddr.S_un.S_un_b.s_b1)); + "." + std::to_string(tAddr.S_un.S_un_b.s_b1));
#else #else
tAddr.s_addr = ntohl(tAddr.s_addr); tAddr.s_addr = ntohl(tAddr.s_addr);
const char *ipStr = inet_ntoa(tAddr); ipVec.push_back(inet_ntoa(tAddr));
ipVec.push_back((char*)ipStr);
#endif #endif
if (ipVec.size() >= (offset < 1000 ? offset : 1000)) { if (ipVec.size() >= (offset < 1000 ? offset : 1000)) {
std::random_shuffle(ipVec.begin(), ipVec.end()); std::random_shuffle(ipVec.begin(), ipVec.end());
while (ipVec.size() != 0) { 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); while (cons >= gThreads && globalScanFlag) Sleep(500);
if (globalScanFlag == false) goto haters_gonna_hate_IPM;
//st = new ST();
//ZeroMemory(st->argv, MAX_ADDR_LEN);
++indexIP; ++indexIP;
strcpy(st->argv, ipVec[0].c_str()); //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()); ipVec.erase(ipVec.begin());
targetAndIPWriter(gTargets--, st->argv); targetAndIPWriter(gTargets--, res);
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) std::thread thr(_connect, res);
_beginthread((void(*)(void*))_connect, 0, st); thr.detach();
#else //Threader::fireThread(res);
pthread_t thrc;
pthread_create(&thrc, NULL, (void *(*)(void*))&_connect, st);
#endif
Sleep(gThreadDelay); Sleep(gThreadDelay);
} }
} }
@ -2399,11 +2472,12 @@ stt->doEmitionThreads(QString::number(0) + "/" + QString::number(gThreads));
case false: { case false: {
struct in_addr tAddr; struct in_addr tAddr;
for (unsigned long i = ip1; i <= ip2; ++i) { 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));
while (cons >= gThreads && globalScanFlag) Sleep(500); while (cons >= gThreads && globalScanFlag) Sleep(500);
if (globalScanFlag == false) break;
//st = new ST();
//ZeroMemory(st->argv, sizeof(st->argv));
ZeroMemory(res, sizeof(res));
++indexIP; ++indexIP;
tAddr.s_addr = i; 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_b2)
+ "." + std::to_string(tAddr.S_un.S_un_b.s_b1)).c_str()); + "." + std::to_string(tAddr.S_un.S_un_b.s_b1)).c_str());
#else #else
tAddr.s_addr = ntohl(tAddr.s_addr);
strcpy(res, inet_ntoa(tAddr)); strcpy(res, inet_ntoa(tAddr));
#endif #endif
strcpy(st->argv, res); //strcpy(st->argv, res);
strcpy(saveStartIP, 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); Sleep(gThreadDelay);
} }
break; break;
@ -2635,13 +2709,15 @@ stt->doEmitionThreads(QString::number(0) + "/" + QString::number(gThreads));
struct in_addr tAddr; struct in_addr tAddr;
for (unsigned long i = ip1; i <= ip2; ++i) { for (unsigned long i = ip1; i <= ip2; ++i) {
if (globalScanFlag == false) break; if (globalScanFlag == false) break;
int offset = ip2 - i; unsigned long offset = ip2 - i;
tAddr.s_addr = i; tAddr.s_addr = i;
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) #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 #else
tAddr.s_addr = ntohl(tAddr.s_addr);
ipVec.push_back(inet_ntoa(tAddr)); ipVec.push_back(inet_ntoa(tAddr));
#endif #endif
if (ipVec.size() >= (offset < 1000 ? offset : 1000)) { if (ipVec.size() >= (offset < 1000 ? offset : 1000)) {
@ -2649,25 +2725,21 @@ stt->doEmitionThreads(QString::number(0) + "/" + QString::number(gThreads));
std::random_shuffle(ipVec.begin(), ipVec.end()); std::random_shuffle(ipVec.begin(), ipVec.end());
while (ipVec.size() != 0) { 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); while (cons >= gThreads && globalScanFlag) Sleep(500);
if (globalScanFlag == false) goto haters_gonna_hate_IM;
//st = new ST();
//ZeroMemory(st->argv, MAX_ADDR_LEN);
++indexIP; ++indexIP;
strcpy(st->argv, ipVec[0].c_str()); strcpy(res, ipVec[0].c_str());
strcpy(saveStartIP, ipVec[0].c_str()); strcpy(saveStartIP, res);
ipVec.erase(ipVec.begin()); ipVec.erase(ipVec.begin());
targetAndIPWriter(gTargets--, st->argv); targetAndIPWriter(gTargets--, res);
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) // std::thread thr(_connect, res);
_beginthread((void(*)(void*))_connect, 0, st); // thr.detach();
#else
pthread_t thrc; //Threader::fireThread(st);
pthread_create(&thrc, NULL, (void *(*)(void*))&_connect, st);
#endif
Sleep(gThreadDelay); Sleep(gThreadDelay);
} }
} }
@ -2678,32 +2750,32 @@ stt->doEmitionThreads(QString::number(0) + "/" + QString::number(gThreads));
case false: { case false: {
struct in_addr tAddr; struct in_addr tAddr;
for (unsigned long i = ip1; i <= ip2; ++i) { 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));
while (cons >= gThreads && globalScanFlag) Sleep(500); while (cons >= gThreads && globalScanFlag) Sleep(500);
if (globalScanFlag == false) break;
//st = new ST();
//ZeroMemory(st->argv, sizeof(st->argv));
ZeroMemory(res, sizeof(res));
++indexIP; ++indexIP;
tAddr.s_addr = i; tAddr.s_addr = i;
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) #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()); 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 #else
tAddr.s_addr = ntohl(tAddr.s_addr);
strcpy(res, inet_ntoa(tAddr)); strcpy(res, inet_ntoa(tAddr));
#endif #endif
strcpy(st->argv, res); //strcpy(st->argv, res);
strcpy(saveStartIP, 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); Sleep(gThreadDelay);
} }
break; break;
}; };
@ -2725,8 +2797,6 @@ stt->doEmitionThreads(QString::number(0) + "/" + QString::number(gThreads));
Sleep(2000); Sleep(2000);
}; };
nCleanup();
stt->doEmitionGreenFoundData("Done. Saved: " + QString::number(saved) + "; Alive: " + QString::number(found) + "."); stt->doEmitionGreenFoundData("Done. Saved: " + QString::number(saved) + "; Alive: " + QString::number(found) + ".");
stt->doEmitionChangeParsed(QString::number(saved) + "/" + QString::number(found)); stt->doEmitionChangeParsed(QString::number(saved) + "/" + QString::number(found));
stt->doEmitionChangeStatus("Idle"); stt->doEmitionChangeStatus("Idle");
@ -2734,6 +2804,8 @@ stt->doEmitionThreads(QString::number(0) + "/" + QString::number(gThreads));
} }
void nCleanup(){ void nCleanup(){
Threader::cleanUp();
if(loginLst != NULL) if(loginLst != NULL)
{ {
for(int i = 0; i < MaxLogin; ++i) delete []loginLst[i]; for(int i = 0; i < MaxLogin; ++i) delete []loginLst[i];