Beispiel #1
1
VOID MakeConsole(VOID)
{
    DWORD dwMode;
    struct _CONSOLE_SCREEN_BUFFER_INFO sbi;
    HANDLE hStd;
    FILE *fStream;

    //if (!InitializeCriticalSectionAndSpinCount(&CriticalSection, 0x00000400)) {
    //    DbgMsg("[-] InitializeCriticalSectionAndSpinCount failed : %lu\n", GetLastError());
    //}
    //InitializeCriticalSection(&CriticalSection);
    InitCriticalSection();
    if (!AllocConsole()) {
        FreeConsole();
        if (!AllocConsole()) {
            DbgMsg("[+] AllocConsole() failed : %lu\n", GetLastError());
        }
    }
    hStd = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleMode(hStd, (LPDWORD)&dwMode);
    SetConsoleMode(hStd, dwMode & 0xFFFFFFEF);
    GetConsoleScreenBufferInfo(hStd, &sbi);
    sbi.dwSize.Y = 500;
    SetConsoleScreenBufferSize(hStd, sbi.dwSize);
    freopen_s(&fStream, "conin$", "r", stdin);
    freopen_s(&fStream, "conout$", "w", stdout);
    freopen_s(&fStream, "conout$", "w", stderr);
}
/*
Initializes the TCP client.

CLITCP* pCLITCP : (IN,OUT) pointer to the data structure representing the client
char* address : (IN) server address to connect
char* port : (IN) server port to connect

Returns : EXIT_SUCCESS or EXIT_FAILURE if there is an error
*/
int InitCLITCP(CLITCP* pCLITCP, char* address, char* port)	{

	if ((pCLITCP == NULL) || (pCLITCP->initialized == 1))	{
		return EXIT_FAILURE;
	}

	pCLITCP->address = (char*)calloc(strlen(address)+1, sizeof(char));
	sprintf(pCLITCP->address, "%s", address);
	pCLITCP->port = (char*)calloc(strlen(port)+1, sizeof(char));
	sprintf(pCLITCP->port, "%s", port);
	pCLITCP->ConnectSocket = INVALID_SOCKET;
	pCLITCP->addrinf = NULL;
	pCLITCP->EndThread = 1; // The thread is not running
	pCLITCP->connected = 0; // The client is not currently connected

	if (InitNet() != EXIT_SUCCESS)	{
		free(pCLITCP->address);pCLITCP->address = NULL;
		free(pCLITCP->port);pCLITCP->port = NULL;
		return EXIT_FAILURE;
	}

	if (CreateSocketTCP(&pCLITCP->ConnectSocket, pCLITCP->address, pCLITCP->port, &pCLITCP->addrinf) != EXIT_SUCCESS)	{
		free(pCLITCP->address);pCLITCP->address = NULL;
		free(pCLITCP->port);pCLITCP->port = NULL;
		return EXIT_FAILURE;
	}

	if (SetSockOptTCP(pCLITCP->ConnectSocket, 1, 10000) != EXIT_SUCCESS)	{ // Setting timeouts on the client socket
		DestroySocketTCP(&pCLITCP->ConnectSocket, &pCLITCP->addrinf);
		free(pCLITCP->address);pCLITCP->address = NULL;
		free(pCLITCP->port);pCLITCP->port = NULL;
		return EXIT_FAILURE;
	}

	if (InitCriticalSection(&pCLITCP->CSEndThread) != EXIT_SUCCESS)	{
		DestroySocketTCP(&pCLITCP->ConnectSocket, &pCLITCP->addrinf);
		free(pCLITCP->address);pCLITCP->address = NULL;
		free(pCLITCP->port);pCLITCP->port = NULL;
		return EXIT_FAILURE;
	}

	if (InitCriticalSection(&pCLITCP->CSconnected) != EXIT_SUCCESS)	{
		DeleteCriticalSection(&pCLITCP->CSEndThread);
		DestroySocketTCP(&pCLITCP->ConnectSocket, &pCLITCP->addrinf);
		free(pCLITCP->address);pCLITCP->address = NULL;
		free(pCLITCP->port);pCLITCP->port = NULL;
		return EXIT_FAILURE;
	}

	pCLITCP->initialized = 1;

	return EXIT_SUCCESS;
}
Beispiel #3
0
EVQ_API int
evq_init (struct event_queue *evq)
{
    struct win32thr *wth = &evq->head;

    evq->ack_event = CreateEvent(NULL, FALSE, FALSE, NULL);
    if (evq->ack_event == NULL)
	return -1;

    wth->signal = CreateEvent(NULL, FALSE, FALSE, NULL);
    if (wth->signal == NULL) {
	CloseHandle(evq->ack_event);
	return -1;
    }
    wth->handles[0] = wth->signal;
    wth->evq = evq;

    InitCriticalSection(&wth->cs);

    if (is_WinNT) {
	evq->iocp.h = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 1);
    }

    evq->now = sys_milliseconds();
    return 0;
}
Beispiel #4
0
VOID Write2File(PVOID data, SSIZE_T size)
{
    static INT iInit = 0;
    FILE *fFpbin = NULL;
    HANDLE hFile = INVALID_HANDLE_VALUE;
    
    InitCriticalSection();
    EnterCriticalSection(&CriticalSection);
    if (!iInit) {
        GetCurrentDirectory(MAX_PATH - 1, bCurDir);
        sprintf_s(REAL_DBG_PATH_BIN, "%s\\%s", bCurDir, "dump_dbg.bin");
        //fopen_s(&fFpbin, REAL_DBG_PATH_BIN, "wb");
        hFile = CreateFile(REAL_DBG_PATH_BIN, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH, NULL);
        iInit = 1;
    }
    else {
        fopen_s(&fFpbin, REAL_DBG_PATH_BIN, "ab");
    }
    if (fFpbin != NULL) {
        //fwrite(data, size, 0x01, fFpbin);
        //fprintf(fFp, "%s", cpBuffer);
        //fflush(fFp);
        //fclose(fFpbin);
        DWORD lpNumberOfBytesWritten;
        WriteFile(hFile, data, (DWORD)size, &lpNumberOfBytesWritten, NULL);
    }
    //printf("%s", cpBuffer);
    LeaveCriticalSection(&CriticalSection);
}
Beispiel #5
0
static DWORD WINAPI
win32thr_wait (struct event_queue *evq)
{
    CRITICAL_SECTION *head_cs;
    HANDLE head_signal;
    struct win32thr wth;

    InitCriticalSection(&wth.cs);
    wth.state = WTHR_SLEEP;
    wth.n = 0;
    wth.tq = NULL;
    wth.handles[0] = wth.signal = ((struct win32thr_arg *) evq)->signal;
    wth.evq = evq = ((struct win32thr_arg *) evq)->evq;
    wth.next = evq->head.next;
    evq->head.next = &wth;
    head_cs = &evq->head.cs;
    head_signal = evq->head.signal;

    SetEvent(evq->ack_event);

    for (; ; ) {
	msec_t now;
	unsigned int res, n;

	WaitForSingleObject(wth.signal, INFINITE);
	if (!(n = wth.n)) break;

	EnterCriticalSection(head_cs);
	now = evq->now;
	if (--evq->nwakeup == 0)
	    SetEvent(evq->ack_event);  /* wake up poller */
	LeaveCriticalSection(head_cs);

	res = WaitForMultipleObjects(n + 1, wth.handles, FALSE,
	 timeout_get(wth.tq, TIMEOUT_INFINITE, now));
	wth.idx = res;
	res = (res == WAIT_TIMEOUT) || (res < (WAIT_OBJECT_0 + n));

	EnterCriticalSection(&wth.cs);
	if (wth.state == WTHR_ACK) {
	    SetEvent(evq->ack_event);
	    ResetEvent(wth.signal);
	}
	wth.state = res ? WTHR_READY : WTHR_SLEEP;
	LeaveCriticalSection(&wth.cs);

	if (res) {
	    EnterCriticalSection(head_cs);
	    wth.next_ready = evq->wth_ready;
	    evq->wth_ready = &wth;
	    evq->sig_ready |= (1 << EVQ_SIGEVQ);
	    SetEvent(head_signal);
	    LeaveCriticalSection(head_cs);
	}
    }
    CloseHandle(wth.signal);
    DeleteCriticalSection(&wth.cs);
    return 0;
}
Beispiel #6
0
//////////////////////////////////////////////////////////////////////
// Konstruktion/Destruktion
//////////////////////////////////////////////////////////////////////
CGateway::CGateway()
{
	m_eInputLayer = NO_LAYER;
	m_eOutputLayer = NO_LAYER;

	m_oAbortCommands = FALSE;

	m_pErrorHandling = 0;
	m_pParameterSet = 0;
	m_pCriticalSection = 0;

	InitCriticalSection();
	InitErrorHandling();
	InitParameterSet();
}
Beispiel #7
0
VOID DbgMsg(const char* szFormat, ...)
{
    static INT iInit = 0;
    va_list args;
    static FILE *fFp = NULL;
    static HANDLE hFile = INVALID_HANDLE_VALUE;
    
    //return;
    
    //va_start(args, szFormat);
    //memset(cpBuffer, 0, sizeof (cpBuffer));
    //vsprintf_s(cpBuffer, sizeof (cpBuffer) - 1, szFormat, args);
    //va_end(args);
    //printf("%s", cpBuffer);
    //return

    InitCriticalSection();
    EnterCriticalSection(&CriticalSection);
    va_start(args, szFormat);
    memset(cpBuffer, 0, sizeof (cpBuffer));
    vsprintf_s(cpBuffer, sizeof (cpBuffer) - 1, szFormat, args);
    va_end(args);
    if (!iInit) {
        GetCurrentDirectory(MAX_PATH - 1, bCurDir);
        sprintf_s(REAL_DBG_PATH, "%s\\%d_%s", bCurDir, GetCurrentProcessId(), FILE_DBG);
        //fopen_s(&fFp, REAL_DBG_PATH, "w");
        // hFile = CreateFileA(REAL_DBG_PATH, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH, NULL);
        hFile = CreateFileA(REAL_DBG_PATH, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
        if (hFile == INVALID_HANDLE_VALUE) {
            printf("[-] CreateFileA(\"%s\", ...) failed : %lu\n", REAL_DBG_PATH, GetLastError());
            system("pause");
            ExitProcess(42);
        }
        iInit = 1;
    }
    //else {
    //    fopen_s(&fFp, REAL_DBG_PATH, "a");
    //}
    if (hFile != NULL) {
        //fprintf(fFp, "%s", cpBuffer);
        ////fflush(fFp);
        //fclose(fFp);
        DWORD lpNumberOfBytesWritten;
        WriteFile(hFile, cpBuffer, (DWORD)strlen(cpBuffer), &lpNumberOfBytesWritten, NULL);
    }
    printf("%s", cpBuffer);
    LeaveCriticalSection(&CriticalSection);
}
Beispiel #8
0
bool CLock::Lock()
{
	if (!m_bCreated)
	{
		InitCriticalSection();
	}

	if (m_bCreated)
	{
		EnterCriticalSection(&m_cs);

		return true;
	}

	return false;
}
/*
Initializes the TCP server. This server will only be able to handle one client at a time.

SRVTCP* pSRVTCP : (IN,OUT) pointer to the data structure representing the server
char* address : (IN) server address
char* port : (IN) server port

Returns : EXIT_SUCCESS or EXIT_FAILURE if there is an error
*/
int InitSRVTCP(SRVTCP* pSRVTCP, char* address, char* port)	{

	if ((pSRVTCP == NULL) || (pSRVTCP->initialized == 1))	{
		return EXIT_FAILURE;
	}

	pSRVTCP->address = (char*)calloc(strlen(address)+1, sizeof(char));
	sprintf(pSRVTCP->address, "%s", address);
	pSRVTCP->port = (char*)calloc(strlen(port)+1, sizeof(char));
	sprintf(pSRVTCP->port, "%s", port);
	pSRVTCP->DontBlock = 0;
	pSRVTCP->EndThread = 1; // The thread is not running
	pSRVTCP->NbConnections = 0; // No clients are currently connected
	pSRVTCP->ListenSocket = INVALID_SOCKET;
	pSRVTCP->addrinf = NULL;
	pSRVTCP->sock_list;
	pSRVTCP->sock_set;

	if (InitNet() != EXIT_SUCCESS)	{
		free(pSRVTCP->address);pSRVTCP->address = NULL;
		free(pSRVTCP->port);pSRVTCP->port = NULL;
		return EXIT_FAILURE;
	}

	if (CreateSocketTCP(&pSRVTCP->ListenSocket, pSRVTCP->address, pSRVTCP->port, &pSRVTCP->addrinf) != EXIT_SUCCESS)	{
		free(pSRVTCP->address);pSRVTCP->address = NULL;
		free(pSRVTCP->port);pSRVTCP->port = NULL;
		return EXIT_FAILURE;
	}

	if (SetSockOptTCP(pSRVTCP->ListenSocket, 1, 10000) != EXIT_SUCCESS)	{ // Setting timeouts on the server socket
		DestroySocketTCP(&pSRVTCP->ListenSocket, &pSRVTCP->addrinf);
		free(pSRVTCP->address);pSRVTCP->address = NULL;
		free(pSRVTCP->port);pSRVTCP->port = NULL;
		return EXIT_FAILURE;
	}

	if (BindListenTCP(pSRVTCP->ListenSocket, &pSRVTCP->addrinf) != EXIT_SUCCESS)	{
		DestroySocketTCP(&pSRVTCP->ListenSocket, &pSRVTCP->addrinf);
		free(pSRVTCP->address);pSRVTCP->address = NULL;
		free(pSRVTCP->port);pSRVTCP->port = NULL;
		return EXIT_FAILURE;
	}

	if (InitCriticalSection(&pSRVTCP->CSEndThread) != EXIT_SUCCESS)	{
		DestroySocketTCP(&pSRVTCP->ListenSocket, &pSRVTCP->addrinf);
		free(pSRVTCP->address);pSRVTCP->address = NULL;
		free(pSRVTCP->port);pSRVTCP->port = NULL;
		return EXIT_FAILURE;
	}

	if (InitCriticalSection(&pSRVTCP->CSNbConnections) != EXIT_SUCCESS)	{
		DeleteCriticalSection(&pSRVTCP->CSEndThread);
		DestroySocketTCP(&pSRVTCP->ListenSocket, &pSRVTCP->addrinf);
		free(pSRVTCP->address);pSRVTCP->address = NULL;
		free(pSRVTCP->port);pSRVTCP->port = NULL;
		return EXIT_FAILURE;
	}

	if (FD_INIT(pSRVTCP->ListenSocket, &pSRVTCP->sock_list, &pSRVTCP->sock_set) != EXIT_SUCCESS)	{
		DeleteCriticalSection(&pSRVTCP->CSNbConnections);
		DeleteCriticalSection(&pSRVTCP->CSEndThread);
		DestroySocketTCP(&pSRVTCP->ListenSocket, &pSRVTCP->addrinf);
		free(pSRVTCP->address);pSRVTCP->address = NULL;
		free(pSRVTCP->port);pSRVTCP->port = NULL;
		return EXIT_FAILURE;
	}

	pSRVTCP->initialized = 1;

	return EXIT_SUCCESS;
}
/*
Initializes the TCP client.

CLITCP* pCLITCP : (IN,OUT) pointer to the data structure representing the client
char* address : (IN) server address to connect
char* port : (IN) server port to connect

Returns : EXIT_SUCCESS or EXIT_FAILURE if there is an error
*/
int InitCLITCP(CLITCP* pCLITCP, char* address, char* port)	{

	int EndThread = 1;

	if((pCLITCP == NULL) || (pCLITCP->initialized == 1))	{
		return EXIT_FAILURE;
	}

	pCLITCP->address = (char*)calloc(strlen(address)+1, sizeof(char));
	sprintf(pCLITCP->address, "%s", address);
	pCLITCP->port = (char*)calloc(strlen(port)+1, sizeof(char));
	sprintf(pCLITCP->port, "%s", port);
	pCLITCP->ConnectSocket = INVALID_SOCKET;
	pCLITCP->addrinf = NULL;
	pCLITCP->EndThread = 0;
	pCLITCP->connected = 0;

	if (
		(InitTCP(&pCLITCP->ConnectSocket, pCLITCP->address, pCLITCP->port, &pCLITCP->addrinf) == EXIT_SUCCESS)&&
//		(SetSockOptTCP(pCLITCP->ConnectSocket, 1, 10000) == EXIT_SUCCESS)&& // Setting timeouts on the client socket
		(ConnectTCP(&pCLITCP->ConnectSocket, &pCLITCP->addrinf) == EXIT_SUCCESS)//&&
//		(SetSockOptTCP(pCLITCP->ConnectSocket, 1, 10000) == EXIT_SUCCESS) // Setting timeouts on the client socket
		)	{
			fprintf(stdout, "Connection successful\n");
			pCLITCP->connected = 1;
	}
	else fprintf(stdout, "Unable to connect\n");

	if (InitCriticalSection(&pCLITCP->CSEndThread) != EXIT_SUCCESS)	{
		StopTCP(&pCLITCP->ConnectSocket, &pCLITCP->addrinf);
		free(pCLITCP->address);pCLITCP->address = NULL;
		free(pCLITCP->port);pCLITCP->port = NULL;
		return EXIT_FAILURE;
	}
	if (InitCriticalSection(&pCLITCP->CSconnected) != EXIT_SUCCESS)	{
		DelCriticalSection(&pCLITCP->CSEndThread);
		StopTCP(&pCLITCP->ConnectSocket, &pCLITCP->addrinf);
		free(pCLITCP->address);pCLITCP->address = NULL;
		free(pCLITCP->port);pCLITCP->port = NULL;
		return EXIT_FAILURE;
	}

	if (CreateDefaultThread(ConnectThrProc, pCLITCP, &pCLITCP->ThrId) != EXIT_SUCCESS)	{
		DelCriticalSection(&pCLITCP->CSEndThread);
		DelCriticalSection(&pCLITCP->CSconnected);
		StopTCP(&pCLITCP->ConnectSocket, &pCLITCP->addrinf);
		free(pCLITCP->address);pCLITCP->address = NULL;
		free(pCLITCP->port);pCLITCP->port = NULL;
		return EXIT_FAILURE;
	}
	if (SetThreadDefaultPriority(pCLITCP->ThrId, THREAD_PRIORITY_NORMAL) != EXIT_SUCCESS)	{
		EndThread = 1;
		memcpy_ts((char*)&pCLITCP->EndThread, (char*)&EndThread, sizeof(int), &pCLITCP->CSEndThread); // Thread-safe copy
		WaitForThread(pCLITCP->ThrId);
		DelCriticalSection(&pCLITCP->CSEndThread);
		DelCriticalSection(&pCLITCP->CSconnected);
		StopTCP(&pCLITCP->ConnectSocket, &pCLITCP->addrinf);
		free(pCLITCP->address);pCLITCP->address = NULL;
		free(pCLITCP->port);pCLITCP->port = NULL;
		return EXIT_FAILURE;
	}

	pCLITCP->initialized = 1;

	return EXIT_SUCCESS;
}
Beispiel #11
0
CLock::CLock()
{
	m_bCreated = false;

	InitCriticalSection();
}