Finally pooled threads

This commit is contained in:
cora48 2015-03-20 17:28:51 +03:00
parent a2ecb97f4d
commit 9b9ee92fda
10 changed files with 708 additions and 875 deletions

View File

@ -61,7 +61,7 @@ int _pingMyTarget(char *ip)
};
}
#else
int _pingMyTarget(char *ip)
int _pingMyTarget(const char *ip)
{
FILE *pipe = popen(("ping -w " + std::to_string(gPingTimeout) + " " + ip).c_str(), "r");
if(!pipe) {
@ -85,7 +85,7 @@ int _pingMyTarget(char *ip)
}
#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};
ZeroMemory(hostStr, sizeof(hostStr));
@ -133,7 +133,7 @@ int _sshConnect(char *user, char *pass, char *host, int port)
return 0;
}
char _get_ssh_banner(char *ip, int port)
char _get_ssh_banner(const char *ip, int port)
{
char recvBuff[256] = {0};
std::string buffer;
@ -149,7 +149,7 @@ char _get_ssh_banner(char *ip, int port)
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;
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;
}
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 pass[32] = {0};
@ -210,12 +210,12 @@ int _EstablishSSHConnection(char *host, int port, std::string *buffer, const cha
QString strIP;
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)
{
return _EstablishSSHConnection(ip, port, buffer, &banner);
return _EstablishSSHConnection(ip.c_str(), port, buffer, &banner);
};
return -1;
}
@ -245,7 +245,7 @@ static size_t nWriteCallback(void *contents, size_t size, size_t nmemb, void *us
return size * nmemb;
}
int Connector::nConnect(const char *ip, const int port, std::string *buffer,
int Connector::nConnect(const char* ip, const int port, std::string *buffer,
const char *postData,
const std::vector<std::string> *customHeaders,
const std::string *lpString){
@ -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());
};
if(curl_easy_perform(curl) == CURLE_OK) {
//if(curl_easy_perform(curl) == CURLE_OK) {
if(1) {
curl_easy_cleanup(curl);
} else {
curl_easy_cleanup(curl);
@ -324,11 +325,11 @@ int Connector::nConnect(const char *ip, const int port, std::string *buffer,
return buffer->size();
}
int Connector::_ConnectToPort(char *ip, int port, char *hl)
int Connector::_ConnectToPort(string ip, int port, char *hl)
{
if(gPingNScan)
{
if(_pingMyTarget(ip) == 0)
if(_pingMyTarget(ip.c_str()) == 0)
{
return -2;
};
@ -337,8 +338,8 @@ int Connector::_ConnectToPort(char *ip, int port, char *hl)
std::string buffer;
int size = 0;
if(port == 22) size = _SSHLobby(ip, port, &buffer);
else size = nConnect(ip, port, &buffer);
if(port == 22) size = _SSHLobby(ip.c_str(), port, &buffer);
else size = nConnect(ip.c_str(), port, &buffer);
// if(size > 0)
// {

View File

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

View File

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

View File

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

View File

@ -8,6 +8,7 @@
#define IRC_CHAN "iskopasi_lab03"
#define CSSOCKET(Socket) shutdown(Socket, SD_BOTH); closesocket(Socket); Socket = -1;
#ifndef CP_UTF8
#define CP_UTF8 65001
#endif
@ -15,6 +16,10 @@
#define CP_ACP 0
#endif
#ifndef MAX_ADDR_LEN
#define MAX_ADDR_LEN 128
#endif
extern QJsonArray *jsonArr;
extern unsigned long long gTargetsOverall;
@ -37,10 +42,12 @@ extern int found, indexIP, gMode,
extern unsigned int Activity;
extern char trcSrv[256], trcScr[256], trcProxy[128], trcPersKey[64],
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],
gVER[16],
gProxyIP[64], gProxyPort[8];
gProxyIP[64], gProxyPort[8],
currentIP[MAX_ADDR_LEN],
finalIP[32];
struct pl{
int loginCounter;

View File

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

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 3.2.1, 2015-03-19T17:34:03. -->
<!-- Written by QtCreator 3.2.1, 2015-03-20T15:45:12. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>

View File

@ -58,6 +58,7 @@ int nickFlag;
int offlineFlag;
bool OnlineMsgSentFlag = false;
int globalPinger = 0;
int nesca_3::savedTabIndex = 0;
bool dFlag = false;
bool startFlag = false;
@ -745,10 +746,10 @@ void nesca_3::slotAddPolyLine()
if(ME2ScanFlag)
{
QPainterPath path;
if (DrawerTh_ME2Scanner::polyVect.count() > 0)
if (DrawerTh_ME2Scanner::polyVect.size() > 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]);
};
@ -761,7 +762,7 @@ void nesca_3::slotAddPolyLine()
int u = 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;
itm = DrawerTh_ME2Scanner::itmList[i];
@ -771,7 +772,7 @@ void nesca_3::slotAddPolyLine()
u -= 1;
};
while (DrawerTh_ME2Scanner::itmList.count() > 38)
while (DrawerTh_ME2Scanner::itmList.size() > 38)
{
sceneGraph->removeItem(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)) );
}
void nesca_3::saveOptions()
{
ZeroMemory(saveStartIP, sizeof(saveStartIP));
void _LoadPersInfoToLocalVars(int savedTabIndex) {
ZeroMemory(endIP2, sizeof(endIP2));
ZeroMemory(top_level_domain, sizeof(top_level_domain));
ZeroMemory(gPorts, sizeof(gPorts));
@ -2950,12 +2949,16 @@ void nesca_3::saveOptions()
{
gMode = 0;
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) strcpy(endIP2, ui->ipLine->text().toLocal8Bit().data());
if(ui->ipLine->text().indexOf("/") < 0) {
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
{
@ -2964,10 +2967,6 @@ void nesca_3::saveOptions()
strcpy(endIP2, ui->ipLine->text().toLocal8Bit().data());
strcat(endIP2, "-");
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;
gThreads = ui->lineEditThread->text().toInt();
strcpy(saveStartIP, ui->lineEditStartIPDNS->text().toLocal8Bit().data());
strcpy(endIP2, ui->lineEditStartIPDNS->text().toLocal8Bit().data());
strcpy(top_level_domain, ui->lineILVL->text().toLocal8Bit().data());
strncpy(gPorts, ("-p" + ui->lineEditPort->text()).toLocal8Bit().data(), 65536);
@ -3000,7 +2998,11 @@ void nesca_3::saveOptions()
gProxyIP[ui->systemProxyIP->text().size()] = '\0';
strncpy(gProxyPort, ui->systemProxyPort->text().toLocal8Bit().data(), 8);
gProxyPort[ui->systemProxyPort->text().size()] = '\0';
}
void nesca_3::saveOptions()
{
_LoadPersInfoToLocalVars(savedTabIndex);
_SaveBackupToFile();
}
@ -3175,7 +3177,7 @@ void RestoreSession()
};
fclose(resFile);
_LoadPersInfoToLocalVars(nesca_3::savedTabIndex);
stt->doEmitionGreenFoundData("Previous session loaded.");
};
}

View File

@ -50,9 +50,10 @@ public:
void ChangeLabelIpRange_Value(QString str);
void ChangeLabelIPS_Value(QString str);
void newListItem(QString str);
static int perc;
private:
int savedTabIndex;
public:
static int perc;
static int savedTabIndex;
protected:
bool eventFilter(QObject* obj, QEvent *event);
void run();
@ -129,7 +130,7 @@ protected:
void logoLabelClicked();
void startScanButtonClicked();
void startScanButtonClickedDNS();
void saveOptions();
void saveOptions();
void ChangeTrackerOK(bool val);
void ChangeShuffle(bool val);
void ChangeLabelThreads_ValueChanged(QString);

File diff suppressed because it is too large Load Diff