mirror of
https://github.com/ChronosX88/nesca.git
synced 2024-11-23 18:52:19 +00:00
Thread pooling refac
This commit is contained in:
parent
e144b2823d
commit
9b920a6e39
64
Threader.cpp
64
Threader.cpp
@ -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;
|
||||
}
|
||||
|
28
Threader.h
28
Threader.h
@ -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
|
||||
|
@ -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];
|
||||
|
@ -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)
|
||||
|
@ -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,19 +882,87 @@ 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;
|
||||
|
||||
ConInc();
|
||||
|
||||
for(int i = 0; i <= overallPorts; ++i)
|
||||
@ -904,7 +974,7 @@ void *_connect(void* ss)
|
||||
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};
|
||||
|
||||
@ -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;
|
||||
@ -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;
|
||||
|
||||
#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);
|
||||
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(saveStartIP, ipVec[0].c_str());
|
||||
//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);
|
||||
}
|
||||
}
|
||||
@ -2399,11 +2472,12 @@ 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));
|
||||
|
||||
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;
|
||||
@ -2635,13 +2709,15 @@ 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));
|
||||
#else
|
||||
tAddr.s_addr = ntohl(tAddr.s_addr);
|
||||
ipVec.push_back(inet_ntoa(tAddr));
|
||||
#endif
|
||||
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());
|
||||
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);
|
||||
}
|
||||
}
|
||||
@ -2678,32 +2750,32 @@ 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));
|
||||
|
||||
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;
|
||||
#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;
|
||||
};
|
||||
@ -2725,8 +2797,6 @@ stt->doEmitionThreads(QString::number(0) + "/" + QString::number(gThreads));
|
||||
Sleep(2000);
|
||||
};
|
||||
|
||||
nCleanup();
|
||||
|
||||
stt->doEmitionGreenFoundData("Done. Saved: " + QString::number(saved) + "; Alive: " + QString::number(found) + ".");
|
||||
stt->doEmitionChangeParsed(QString::number(saved) + "/" + QString::number(found));
|
||||
stt->doEmitionChangeStatus("Idle");
|
||||
@ -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];
|
||||
|
Loading…
Reference in New Issue
Block a user