scrap-is-not-scrap/pic-windows.c

155 lines
3.5 KiB
C

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <assert.h>
#include <stdio.h>
#include <io.h>
#include <stdlib.h>
#include <malloc.h>
#include <fcntl.h>
#include <intrin.h>
#pragma comment(lib, "Ws2_32.lib")
#pragma comment(lib, "Mswsock.lib")
#pragma comment(lib, "AdvApi32.lib")
#define PORT 999
#define PORT_STR "999"
typedef auto (*FUNCPTR)();
int __cdecl main(int argc, const char **argv)
{
auto retVal = SOCKET_ERROR;
LPVOID picBuffer = NULL;
HANDLE fileHandle;
DWORD picBuffer_len = 0;
DWORD oldProtect;
char *fileInPath = argv[0];
char *fileOutPath = argv[0];
FUNCPTR func = NULL;
SecureZeroMemory(&fileHandle, sizeof(fileHandle));
fileHandle = CreateFile(fileInPath, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
if (INVALID_HANDLE_VALUE == fileHandle)
{
retVal = GetLastError();
goto CLEAN_UP;
}
picBuffer_len = GetFileSize(fileHandle, &picBuffer_len);
if (-1 == picBuffer_len)
{
retVal = GetLastError();
goto CLEAN_UP;
}
picBuffer = malloc(picBuffer_len);
if (NULL == picBuffer)
{
retVal = GetLastError();
goto CLEAN_UP;
}
SecureZeroMemory(picBuffer, sizeof(picBuffer_len));
retVal = VirtualProtect(picBuffer, picBuffer_len, PAGE_EXECUTE_READWRITE,
&oldProtect);
if (0 == retVal)
{
retVal = GetLastError();
goto CLEAN_UP;
}
SecureZeroMemory(&overlapped, sizeof(overlapped));
retVal =
ReadFile(fileHandle, picBuffer, picBuffer_len, &overlapped, NULL);
if (FALSE == retVal)
{
retVal = GetLastError();
goto CLEAN_UP;
}
if (fileHandle)
{
CloseHandle(fileHandle);
}
func = (FUNCPTR)picBuffer;
SecureZeroMemory(&wsaData, sizeof(wsaData));
WSAStartup(MAKEWORD(2, 2), &wsaData);
/* create sockets */
OVERLAPPED overlapped;
unsigned int listenSocket = INVALID_SOCKET;
unsigned int clientSocket = INVALID_SOCKET;
struct addrinfo *result = NULL;
struct addrinfo hints;
SOCKET socketReuse = INVALID_SOCKET;
WSABUF messageBuffer;
WSAOVERLAPPED sendOverlapped;
WSADATA wsaData;
SecureZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
retVal = getaddrinfo(NULL, PORT_STR, &hints, &result);
if (SOCKET_ERROR == retVal)
{
retVal = WSAGetLastError();
goto CLEAN_UP;
}
listenSocket = INVALID_SOCKET;
listenSocket =
socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (INVALID_SOCKET == listenSocket)
{
retVal = WSAGetLastError();
goto CLEAN_UP;
}
retVal = bind(listenSocket, result->ai_addr, (int)result->ai_addrlen);
if (SOCKET_ERROR == retVal)
{
retVal = WSAGetLastError();
goto CLEAN_UP;
}
freeaddrinfo(result);
retVal = listen(listenSocket, SOMAXCONN);
if (SOCKET_ERROR == retVal)
{
retVal = WSAGetLastError();
goto CLEAN_UP;
}
clientSocket = accept(listenSocket, NULL, NULL);
if (INVALID_SOCKET == clientSocket)
{
retVal = WSAGetLastError();
goto CLEAN_UP;
}
socketReuse = func(); /** PIC test here **/
retVal = 0;
CLEAN_UP:
if (fileHandle)
{
CloseHandle(fileHandle);
}
WSACleanup();
return retVal;
}