Finally pooled threads

This commit is contained in:
cora48 2015-03-20 17:28:51 +03:00
parent b2b246010e
commit 90b6c317e9
9 changed files with 707 additions and 874 deletions

View File

@ -61,7 +61,7 @@ int _pingMyTarget(char *ip)
}; };
} }
#else #else
int _pingMyTarget(char *ip) int _pingMyTarget(const char *ip)
{ {
FILE *pipe = popen(("ping -w " + std::to_string(gPingTimeout) + " " + ip).c_str(), "r"); FILE *pipe = popen(("ping -w " + std::to_string(gPingTimeout) + " " + ip).c_str(), "r");
if(!pipe) { if(!pipe) {
@ -85,7 +85,7 @@ int _pingMyTarget(char *ip)
} }
#endif #endif
int _sshConnect(char *user, char *pass, char *host, int port) int _sshConnect(char *user, char *pass, const char *host, int port)
{ {
char hostStr[128] = {0}; char hostStr[128] = {0};
ZeroMemory(hostStr, sizeof(hostStr)); ZeroMemory(hostStr, sizeof(hostStr));
@ -133,7 +133,7 @@ int _sshConnect(char *user, char *pass, char *host, int port)
return 0; return 0;
} }
char _get_ssh_banner(char *ip, int port) char _get_ssh_banner(const char *ip, int port)
{ {
char recvBuff[256] = {0}; char recvBuff[256] = {0};
std::string buffer; std::string buffer;
@ -149,7 +149,7 @@ char _get_ssh_banner(char *ip, int port)
return *recvBuff; return *recvBuff;
} }
int check_ssh_pass(char *user, char *pass, char *userPass, char *host, int port, std::string *buffer, const char *banner) int check_ssh_pass(char *user, char *pass, char *userPass, const char *host, int port, std::string *buffer, const char *banner)
{ {
int res = -1; int res = -1;
if(BALogSwitched) stt->doEmitionBAData("Probing SSH: " + QString(user) + ":" + QString(pass) + "@" + QString(host) + ":" + QString::number(port)); if(BALogSwitched) stt->doEmitionBAData("Probing SSH: " + QString(user) + ":" + QString(pass) + "@" + QString(host) + ":" + QString::number(port));
@ -167,7 +167,7 @@ int check_ssh_pass(char *user, char *pass, char *userPass, char *host, int port,
return res; return res;
} }
int _EstablishSSHConnection(char *host, int port, std::string *buffer, const char *banner) int _EstablishSSHConnection(const char* host, int port, std::string *buffer, const char *banner)
{ {
char login[32] = {0}; char login[32] = {0};
char pass[32] = {0}; char pass[32] = {0};
@ -210,12 +210,12 @@ int _EstablishSSHConnection(char *host, int port, std::string *buffer, const cha
QString strIP; QString strIP;
QString strPort; QString strPort;
int Connector::_SSHLobby(char *ip, int port, std::string *buffer) int Connector::_SSHLobby(std::string ip, int port, std::string *buffer)
{ {
const char &banner = _get_ssh_banner(ip, port); const char &banner = _get_ssh_banner(ip.c_str(), port);
if(strlen(&banner) > 0) if(strlen(&banner) > 0)
{ {
return _EstablishSSHConnection(ip, port, buffer, &banner); return _EstablishSSHConnection(ip.c_str(), port, buffer, &banner);
}; };
return -1; return -1;
} }
@ -306,7 +306,8 @@ int Connector::nConnect(const char *ip, const int port, std::string *buffer,
curl_easy_setopt(curl, CURLOPT_USERPWD, lpString->c_str()); curl_easy_setopt(curl, CURLOPT_USERPWD, lpString->c_str());
}; };
if(curl_easy_perform(curl) == CURLE_OK) { //if(curl_easy_perform(curl) == CURLE_OK) {
if(1) {
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
} else { } else {
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
@ -324,11 +325,11 @@ int Connector::nConnect(const char *ip, const int port, std::string *buffer,
return buffer->size(); return buffer->size();
} }
int Connector::_ConnectToPort(char *ip, int port, char *hl) int Connector::_ConnectToPort(string ip, int port, char *hl)
{ {
if(gPingNScan) if(gPingNScan)
{ {
if(_pingMyTarget(ip) == 0) if(_pingMyTarget(ip.c_str()) == 0)
{ {
return -2; return -2;
}; };
@ -337,8 +338,8 @@ int Connector::_ConnectToPort(char *ip, int port, char *hl)
std::string buffer; std::string buffer;
int size = 0; int size = 0;
if(port == 22) size = _SSHLobby(ip, port, &buffer); if(port == 22) size = _SSHLobby(ip.c_str(), port, &buffer);
else size = nConnect(ip, port, &buffer); else size = nConnect(ip.c_str(), port, &buffer);
// if(size > 0) // if(size > 0)
// { // {

View File

@ -21,7 +21,7 @@ public:
const char *postData = NULL, const char *postData = NULL,
const std::vector<std::string> *customHeaders = NULL, const std::vector<std::string> *customHeaders = NULL,
const string *lpString = NULL); const string *lpString = NULL);
static int _ConnectToPort(char *ip, int port, char *hl); static int _ConnectToPort(string ip, int port, char *hl);
static int _SSHLobby(char *ip, int port, std::string *buffer); static int _SSHLobby(std::string ip, int port, std::string *buffer);
}; };
#endif // CONNECTOR_H #endif // CONNECTOR_H

View File

@ -1,43 +1,29 @@
#include <Threader.h> #include <Threader.h>
std::vector<char*> Threader::threadPool;
std::vector<std::condition_variable*> cvPool;
std::vector<bool> Threader::readyPool;
int Threader::threadId = 0; int Threader::threadId = 0;
std::mutex Threader::m;
bool Threader::ready = false;
std::condition_variable Threader::cv;
std::queue<std::string> Threader::ipQueue;
int Threader::getFreeDataSlotId() { void Threader::fireThread(std::string ip, void *func(void)) {
int tps = threadPool.size();
for(int i = 0; i != (gThreads > tps ? tps : gThreads); ++i) {
if(threadPool[i] == NULL) return i;
}
return -1;
}
int Threader::getFreeThreadId() { ipQueue.push(ip);
int res;
while((res = getFreeDataSlotId()) < 0) Sleep(50);
return res;
}
void Threader::fireThread(char *res, void *func(int,std::condition_variable*)) {
if(threadId < gThreads) { if(threadId < gThreads) {
threadPool.push_back(res); ++threadId;
std::condition_variable cv; std::thread workerThread(func);
cvPool.push_back(&cv);
std::thread workerThread(func, threadId++, &cv);
workerThread.detach(); workerThread.detach();
readyPool.push_back(true);
cv.notify_one();
} else {
int id = getFreeThreadId();
threadPool[id] = res;
readyPool[id] = true;
cvPool[id]->notify_one();
}
} }
void Threader::cleanUp() { std::unique_lock<std::mutex> lk(m);
threadId = 0; ready = true;
threadPool.clear(); Threader::cv.notify_one();
cvPool.clear(); Sleep(gThreadDelay);
}
//std::queue<std::string> empty;
void Threader::cleanUp() {
//std::swap( ipQueue, empty );
ipQueue = {};
threadId = 0;
} }

View File

@ -4,26 +4,21 @@
#include <mainResources.h> #include <mainResources.h>
#include <externData.h> #include <externData.h>
#include <thread> #include <thread>
#include <chrono>
#include <future>
#include <mutex> #include <mutex>
#include <condition_variable> #include <condition_variable>
#include <queue>
class Threader { class Threader {
private:
public: public:
static std::vector<char *> threadPool;
static int threadId; static int threadId;
static std::mutex m;
static bool ready;
static std::condition_variable cv; static std::condition_variable cv;
static std::vector<bool> readyPool; static std::queue<std::string> ipQueue;
private:
static int getFreeDataSlotId();
static int getFreeThreadId();
public: public:
static void fireThread(char *st, static void fireThread(std::string ip, void *func(void));
void* func(int,std::condition_variable*));
static void cleanUp(); static void cleanUp();
}; };

View File

@ -8,6 +8,7 @@
#define IRC_CHAN "iskopasi_lab03" #define IRC_CHAN "iskopasi_lab03"
#define CSSOCKET(Socket) shutdown(Socket, SD_BOTH); closesocket(Socket); Socket = -1; #define CSSOCKET(Socket) shutdown(Socket, SD_BOTH); closesocket(Socket); Socket = -1;
#ifndef CP_UTF8 #ifndef CP_UTF8
#define CP_UTF8 65001 #define CP_UTF8 65001
#endif #endif
@ -15,6 +16,10 @@
#define CP_ACP 0 #define CP_ACP 0
#endif #endif
#ifndef MAX_ADDR_LEN
#define MAX_ADDR_LEN 128
#endif
extern QJsonArray *jsonArr; extern QJsonArray *jsonArr;
extern unsigned long long gTargetsOverall; extern unsigned long long gTargetsOverall;
@ -37,10 +42,12 @@ extern int found, indexIP, gMode,
extern unsigned int Activity; extern unsigned int Activity;
extern char trcSrv[256], trcScr[256], trcProxy[128], trcPersKey[64], extern char trcSrv[256], trcScr[256], trcProxy[128], trcPersKey[64],
ircServer[32], ircPort[32], ircProxy[64], ircProxyPort[8], ircNick[32], ircServer[32], ircPort[32], ircProxy[64], ircProxyPort[8], ircNick[32],
trcPort[32], trcSrvPortLine[32], saveStartIP[128], saveEndIP[128], trcPort[32], trcSrvPortLine[32], saveEndIP[128],
gRange[128], gFirstDom[128], gPorts[65536], endIP2[128], gRange[128], gFirstDom[128], gPorts[65536], endIP2[128],
gVER[16], gVER[16],
gProxyIP[64], gProxyPort[8]; gProxyIP[64], gProxyPort[8],
currentIP[MAX_ADDR_LEN],
finalIP[32];
struct pl{ struct pl{
int loginCounter; int loginCounter;

View File

@ -83,11 +83,14 @@ typedef int BOOL;
#endif #endif
#define MAX_ADDR_LEN 128
#define TITLE_MAX_SIZE 512 #define TITLE_MAX_SIZE 512
#define COOKIE_MAX_SIZE 1024 #define COOKIE_MAX_SIZE 1024
#define RESULT_DIR_NAME "./result_files-" __DATE__ #define RESULT_DIR_NAME "./result_files-" __DATE__
#ifndef MAX_ADDR_LEN
#define MAX_ADDR_LEN 128
#endif
struct PathStr{ struct PathStr{
char codepage[32]; char codepage[32];
char headr[TITLE_MAX_SIZE]; char headr[TITLE_MAX_SIZE];

View File

@ -58,6 +58,7 @@ int nickFlag;
int offlineFlag; int offlineFlag;
bool OnlineMsgSentFlag = false; bool OnlineMsgSentFlag = false;
int globalPinger = 0; int globalPinger = 0;
int nesca_3::savedTabIndex = 0;
bool dFlag = false; bool dFlag = false;
bool startFlag = false; bool startFlag = false;
@ -745,10 +746,10 @@ void nesca_3::slotAddPolyLine()
if(ME2ScanFlag) if(ME2ScanFlag)
{ {
QPainterPath path; QPainterPath path;
if (DrawerTh_ME2Scanner::polyVect.count() > 0) if (DrawerTh_ME2Scanner::polyVect.size() > 0)
{ {
path.moveTo(DrawerTh_ME2Scanner::polyVect[0]); path.moveTo(DrawerTh_ME2Scanner::polyVect[0]);
for (int i = 1; i < DrawerTh_ME2Scanner::polyVect.count(); ++i) for (int i = 1; i < DrawerTh_ME2Scanner::polyVect.size(); ++i)
{ {
path.lineTo(DrawerTh_ME2Scanner::polyVect[i]); path.lineTo(DrawerTh_ME2Scanner::polyVect[i]);
}; };
@ -761,7 +762,7 @@ void nesca_3::slotAddPolyLine()
int u = 0; int u = 0;
double uu = 1.0; double uu = 1.0;
for (int i = 0; i < DrawerTh_ME2Scanner::itmList.count(); ++i) for (int i = 0; i < DrawerTh_ME2Scanner::itmList.size(); ++i)
{ {
int y = u - i - 1; int y = u - i - 1;
itm = DrawerTh_ME2Scanner::itmList[i]; itm = DrawerTh_ME2Scanner::itmList[i];
@ -771,7 +772,7 @@ void nesca_3::slotAddPolyLine()
u -= 1; u -= 1;
}; };
while (DrawerTh_ME2Scanner::itmList.count() > 38) while (DrawerTh_ME2Scanner::itmList.size() > 38)
{ {
sceneGraph->removeItem(DrawerTh_ME2Scanner::itmList[38]); sceneGraph->removeItem(DrawerTh_ME2Scanner::itmList[38]);
delete DrawerTh_ME2Scanner::itmList[38]; delete DrawerTh_ME2Scanner::itmList[38];
@ -2939,9 +2940,7 @@ void nesca_3::ConnectEvrthng()
connect ( ui->tabMainWidget, SIGNAL(currentChanged(int)), this, SLOT(slotTabChanged(int)) ); connect ( ui->tabMainWidget, SIGNAL(currentChanged(int)), this, SLOT(slotTabChanged(int)) );
} }
void nesca_3::saveOptions() void _LoadPersInfoToLocalVars(int savedTabIndex) {
{
ZeroMemory(saveStartIP, sizeof(saveStartIP));
ZeroMemory(endIP2, sizeof(endIP2)); ZeroMemory(endIP2, sizeof(endIP2));
ZeroMemory(top_level_domain, sizeof(top_level_domain)); ZeroMemory(top_level_domain, sizeof(top_level_domain));
ZeroMemory(gPorts, sizeof(gPorts)); ZeroMemory(gPorts, sizeof(gPorts));
@ -2950,12 +2949,16 @@ void nesca_3::saveOptions()
{ {
gMode = 0; gMode = 0;
gThreads = ui->threadLine->text().toInt(); gThreads = ui->threadLine->text().toInt();
int indexof = ui->ipLine->text().indexOf("-"); QString targetLine = ui->ipLine->text();
if(indexof > 0) if(ui->ipLine->text().indexOf("-") > 0)
{ {
strncpy(saveStartIP, ui->ipLine->text().toLocal8Bit().data(), indexof); if(ui->ipLine->text().indexOf("/") < 0) {
if(ui->ipLine->text().indexOf("/") < 0) strcpy(endIP2, ui->ipLine->text().toLocal8Bit().data()); QList<QString> splittedTargetLine = targetLine.split("-");
strcpy(currentIP, splittedTargetLine[0].toLocal8Bit().data());
strcpy(finalIP, splittedTargetLine[1].toLocal8Bit().data());
strcpy(endIP2, ui->ipLine->text().toLocal8Bit().data());
}
} }
else else
{ {
@ -2964,10 +2967,6 @@ void nesca_3::saveOptions()
strcpy(endIP2, ui->ipLine->text().toLocal8Bit().data()); strcpy(endIP2, ui->ipLine->text().toLocal8Bit().data());
strcat(endIP2, "-"); strcat(endIP2, "-");
strcat(endIP2, ui->ipLine->text().toLocal8Bit().data()); strcat(endIP2, ui->ipLine->text().toLocal8Bit().data());
}
else
{
strncpy(saveStartIP, ui->ipLine->text().toLocal8Bit().data(), ui->ipLine->text().indexOf("/"));
}; };
}; };
@ -2979,7 +2978,6 @@ void nesca_3::saveOptions()
gMode = 1; gMode = 1;
gThreads = ui->lineEditThread->text().toInt(); gThreads = ui->lineEditThread->text().toInt();
strcpy(saveStartIP, ui->lineEditStartIPDNS->text().toLocal8Bit().data());
strcpy(endIP2, ui->lineEditStartIPDNS->text().toLocal8Bit().data()); strcpy(endIP2, ui->lineEditStartIPDNS->text().toLocal8Bit().data());
strcpy(top_level_domain, ui->lineILVL->text().toLocal8Bit().data()); strcpy(top_level_domain, ui->lineILVL->text().toLocal8Bit().data());
strncpy(gPorts, ("-p" + ui->lineEditPort->text()).toLocal8Bit().data(), 65536); strncpy(gPorts, ("-p" + ui->lineEditPort->text()).toLocal8Bit().data(), 65536);
@ -3000,7 +2998,11 @@ void nesca_3::saveOptions()
gProxyIP[ui->systemProxyIP->text().size()] = '\0'; gProxyIP[ui->systemProxyIP->text().size()] = '\0';
strncpy(gProxyPort, ui->systemProxyPort->text().toLocal8Bit().data(), 8); strncpy(gProxyPort, ui->systemProxyPort->text().toLocal8Bit().data(), 8);
gProxyPort[ui->systemProxyPort->text().size()] = '\0'; gProxyPort[ui->systemProxyPort->text().size()] = '\0';
}
void nesca_3::saveOptions()
{
_LoadPersInfoToLocalVars(savedTabIndex);
_SaveBackupToFile(); _SaveBackupToFile();
} }
@ -3175,7 +3177,7 @@ void RestoreSession()
}; };
fclose(resFile); fclose(resFile);
_LoadPersInfoToLocalVars(nesca_3::savedTabIndex);
stt->doEmitionGreenFoundData("Previous session loaded."); stt->doEmitionGreenFoundData("Previous session loaded.");
}; };
} }

View File

@ -50,9 +50,10 @@ public:
void ChangeLabelIpRange_Value(QString str); void ChangeLabelIpRange_Value(QString str);
void ChangeLabelIPS_Value(QString str); void ChangeLabelIPS_Value(QString str);
void newListItem(QString str); void newListItem(QString str);
public:
static int perc; static int perc;
private: static int savedTabIndex;
int savedTabIndex;
protected: protected:
bool eventFilter(QObject* obj, QEvent *event); bool eventFilter(QObject* obj, QEvent *event);
void run(); void run();

File diff suppressed because it is too large Load Diff