Thread pooling refac

This commit is contained in:
cora48 2015-03-18 17:28:39 +03:00
parent e144b2823d
commit 9b920a6e39
5 changed files with 248 additions and 130 deletions

View File

@ -1,21 +1,59 @@
#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) {
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;
}

View File

@ -2,20 +2,30 @@
#define THREADER_H
#include <mainResources.h>
#include <thread>
struct ThreadStruct{
pthread_t *handler;
bool busy;
};
//typedef struct {
// char argv[MAX_ADDR_LEN] = {0};
//} ST;
class Threader {
private:
static std::vector<ThreadStruct> 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<char *> 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

View File

@ -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];

View File

@ -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)

View File

@ -3,8 +3,10 @@
#include "externData.h"
#include "externFunctions.h"
#include "Connector.h"
#include "Threader.h"
#include <thread>
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];