Пример #1
0
void CloseConnection(LPREQUEST lpReq)
{
	HTTPSTATS	stats;
	int			nRet;

	//
	// Log the event and close the socket
	//
	LogEvent(ghwnd, 
			 "Closing socket: %d",
			 lpReq->Socket);

	nRet = closesocket(lpReq->Socket);
	if (nRet == SOCKET_ERROR)
	{
		LogWinSockError(ghwnd, 
			 "closesocket()",
			 WSAGetLastError());
	}

	//
	// Give the user interface the stats
	//
	stats.dwElapsedTime = 
			(GetTickCount() - lpReq->dwConnectTime);
	stats.dwRecv = lpReq->dwRecv;
	stats.dwSend = lpReq->dwSend;
	SendMessage(ghwnd,
				guAppMsg,
				HTTP_STATS_MSG,
				(LPARAM)&stats);
}
Пример #2
0
BOOL SendBuffer(LPREQUEST lpReq, LPBYTE pBuf, DWORD dwBufSize)
{
	WSABUF			wsabuf;
	WSAOVERLAPPED	over;
	DWORD			dwRecv;
	DWORD			dwFlags;
	DWORD			dwRet;
	HANDLE			hEvents[2];
	BOOL			fPending;
	int				nRet;

	//
	// Setup the WSABUF and WSAOVERLAPPED structures
	//
	wsabuf.buf  = pBuf;
	wsabuf.len  = dwBufSize;
	over.hEvent = WSACreateEvent();

	fPending = FALSE;
	nRet = WSASend(lpReq->Socket,	// Socket
				   &wsabuf,			// WSABUF
				   1,				// Number of buffers
				   &dwRecv,			// Bytes received
				   0,				// Flags
				   &over,			// WSAOVERLAPPED
				   NULL);			// Completion function
	if (nRet != 0)
	{
		if (WSAGetLastError() == WSA_IO_PENDING)
			fPending = TRUE;
		else
		{
			LogWinSockError(ghwnd, 
							"WSASend()", 
							WSAGetLastError());
			CloseHandle(over.hEvent);
			return FALSE;
		}
	}

	//
	// If the I/O isn't finished...
	//
	if (fPending)
	{
		//
		// Wait for the request to complete 
		// or the exit event to be signaled
		//
		hEvents[0]  = over.hEvent;
		hEvents[1]  = lpReq->hExit;
		dwRet = WaitForMultipleObjects(2,
									   hEvents,
									   FALSE,
									   INFINITE);
		//
		// Was the recv event signaled?
		//
		if (dwRet != 0)
		{
			CloseHandle(over.hEvent);
			return FALSE;
		}

		//
		// Get I/O result
		//
		if (!WSAGetOverlappedResult(lpReq->Socket,
									&over,
									&dwRecv,
									FALSE,
									&dwFlags))
		{
			LogWinSockError(ghwnd, 
							"WSAGetOverlappedResult()", 
							WSAGetLastError());
			CloseHandle(over.hEvent);
			return FALSE;
		}
	}

	CloseHandle(over.hEvent);
	return TRUE;
}
Пример #3
0
BOOL RecvRequest(LPREQUEST lpReq, LPBYTE pBuf, DWORD dwBufSize)
{
	WSABUF			wsabuf;
	WSAOVERLAPPED	over;
	DWORD			dwRecv;
	DWORD			dwFlags;
	DWORD			dwRet;
	HANDLE			hEvents[2];
	BOOL			fPending;
	int				nRet;

	//
	// Zero the buffer so the recv is null-terminated
	//
	memset(pBuf, 0, dwBufSize);

	//
	// Setup the WSABUF and WSAOVERLAPPED structures
	//
	wsabuf.buf  = pBuf;
	wsabuf.len  = dwBufSize;
	over.hEvent = WSACreateEvent();

	dwFlags = 0;
	fPending = FALSE;
	nRet = WSARecv(lpReq->Socket,	// Socket
				   &wsabuf,			// WSABUF
				   1,				// Number of buffers
				   &dwRecv,			// Bytes received
				   &dwFlags,		// Flags
				   &over,			// WSAOVERLAPPED
				   NULL);			// Completion function
	if (nRet != 0)
	{
		if (WSAGetLastError() != WSA_IO_PENDING)
		{
			LogWinSockError(ghwnd, 
						    "WSARecv()", 
							WSAGetLastError());
			CloseHandle(over.hEvent);
			return FALSE;
		}
		else
			fPending = TRUE;
	}

	//
	// If the I/O isn't finished...
	//
	if (fPending)
	{
		//
		// Wait for the request to complete or the exit event 
		//
		hEvents[0]  = over.hEvent;
		hEvents[1]  = lpReq->hExit;
		dwRet = WaitForMultipleObjects(2,
									   hEvents,
									   FALSE,
									   INFINITE);
		//
		// Was the recv event signaled?
		//
		if (dwRet != 0)
		{
			CloseHandle(over.hEvent);
			return FALSE;
		}
		if (!WSAGetOverlappedResult(lpReq->Socket,
									&over,
									&dwRecv,
									FALSE,
									&dwFlags))
			CloseHandle(over.hEvent);
			return FALSE;
	}

	//
	// Recv event is complete -- keep statistics
	//
	lpReq->dwRecv += dwRecv;
	CloseHandle(over.hEvent);
	return TRUE;
}
Пример #4
0
BOOL StartHTTP(LPHTTPSERVINFO lpInfo)
{
	SOCKADDR_IN		saServer;		
	LPSERVENT		lpServEnt;		
	unsigned		ThreadAddr;
	char			szBuf[256];		
	char			szAddress[80];
	DWORD			dwAddrStrLen;
    int				nRet;			

	//
	// Save the Window handle and message
	// ID for further use
	//
	ghwnd    = lpInfo->hwnd;
	guAppMsg = lpInfo->uMsgApp;
	if (lpInfo->lpRootDir != NULL)
		strcpy(szWebRoot, lpInfo->lpRootDir);
	else
		strcpy(szWebRoot, "/WebPages");

	//
	// Create the exit signal event object
	//
	ghExit = CreateEvent(NULL,		// Security
						 TRUE,		// Manual reset
						 FALSE,		// Initial State
						 NULL);		// Name
	if (ghExit == NULL)
		return FALSE;

 	//
	// Create a TCP/IP stream socket
	//
	listenSocket = socket(AF_INET, 
						  SOCK_STREAM, 
						  IPPROTO_TCP);
	if (listenSocket == INVALID_SOCKET)
	{
		LogWinSockError(ghwnd, 
						"Could not create listen socket",
						WSAGetLastError());
		return FALSE;
	}

	//
	// If a specific port number was specified
	// then use it
	//
	if (lpInfo->nPort != 0)
		saServer.sin_port = htons(lpInfo->nPort);
	else
	{
		//
		// Find a port number
		//
		lpServEnt = getservbyname("http", "tcp");
		if (lpServEnt != NULL)
			saServer.sin_port = lpServEnt->s_port;
		else
			saServer.sin_port = htons(HTTPPORT);
	}

	//
	// Fill in the rest of the address structure
	//
	saServer.sin_family = AF_INET;
	saServer.sin_addr.s_addr = INADDR_ANY;

	//
	// bind our name to the socket
	//
	nRet = bind(listenSocket, 
				(LPSOCKADDR)&saServer, 
				sizeof(struct sockaddr));
	if (nRet == SOCKET_ERROR)
	{
		LogWinSockError(ghwnd,
						 "bind() error",
						 WSAGetLastError());
		closesocket(listenSocket);
		return FALSE;
	}

	//
	// Set the socket to listen
	//
	nRet = listen(listenSocket, SOMAXCONN);
	if (nRet == SOCKET_ERROR)
	{
		LogWinSockError(ghwnd,
						 "listen() error",
						 WSAGetLastError());
		closesocket(listenSocket);
		return FALSE;
	}

	//
	// Create the listening thread
	//
	gdwListenThread = _beginthreadex(
							NULL,		 // Security
							0,			 // Stack size
							ListenThread,// Function address
							&ghExit,	 // Argument
							0,			 // Init flag
							&ThreadAddr);// Thread address
	if (!gdwListenThread)
	{
		LogEvent(ghwnd, 
				 "Could not create listening thread: %d",
				 GetLastError());
		closesocket(listenSocket);
		return FALSE;
	}

	//
	// Display the host name and address
	//
	gethostname(szBuf, sizeof(szBuf));
	dwAddrStrLen = sizeof(szAddress);
	GetLocalAddress(szAddress, &dwAddrStrLen);
	LogEvent(ghwnd, 
			 "HTTP Server Started: %s [%s] on port %d",
			 szBuf,
			 szAddress,
			 htons(saServer.sin_port));

	return TRUE;
}
Пример #5
0
void thrdServer(LPVOID lpvThreadParam) {
	DWORD threadid;
	HANDLE threadHandle;
	SOCKET connectSocket, listenSocket;
	WORD 	versionRequested;
	WSADATA	wsaData;
	int nRet;
	sockaddr_in clientName;
	LPSocket structSocket;
	int sockaddrSize;
	sockaddr_in myserver;
	int i;
	CString outbuffer;
	char tempclock[100];
	
	//Set up Socket information
	versionRequested = MAKEWORD(2,0);
	WSAStartup(versionRequested, &wsaData);
	
	ZeroMemory(&myserver, sizeof(SOCKADDR_IN));
	myserver.sin_family = AF_INET;
	myserver.sin_port = htons((u_short) BASEPORT + gProcessID);
	myserver.sin_addr.s_addr = htonl(INADDR_ANY);
	
	listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	
	if (listenSocket == INVALID_SOCKET)
	{
		LogWinSockError("Could not create listen socket",WSAGetLastError());
		ExitThread(1);
	}

	nRet = bind(listenSocket, (SOCKADDR*) &myserver, sizeof(sockaddr_in));

	if (nRet == SOCKET_ERROR)
	{
		LogWinSockError("bind() error",WSAGetLastError());
		closesocket(listenSocket);
		ExitThread(1);
	}

	nRet = listen(listenSocket, SOMAXCONN);
	
	if (nRet == SOCKET_ERROR)
	{
		LogWinSockError("listen() error", WSAGetLastError());
		ExitThread(1);
	}

	if (gProcessID == 6) {
		for (i=1; i<= NUMBEROFPROCESSES; i++) {
			if (i != gProcessID) {
				outbuffer = "GO:";
				outbuffer += ltoa(gProcessID,tempclock, 10);
				outbuffer += ":";
				outbuffer += ltoa(myclock, tempclock, 10);
				sendMessage(outbuffer, i);
			}
		}
		startThreads();
	}

	//Loop and accept connections until told to exit
	while (!exitFlag) {
		sockaddrSize=sizeof(SOCKADDR_IN);
		connectSocket = accept(listenSocket, (SOCKADDR*) &clientName, &sockaddrSize);
		structSocket =  new Socket();
		memcpy((void*)&structSocket->Socket,&connectSocket, sizeof(connectSocket));
		if (connectSocket != INVALID_SOCKET) {
			threadHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &thrdMessage, structSocket, 0, &threadid);
			CloseHandle(threadHandle);
		}
		Sleep(200);
	}
	closesocket(listenSocket);
}
Пример #6
0
void sendMessage(CString message, int destinationProcessID) {
	
	WaitForSingleObject(sSend, INFINITE);

	SOCKET mysocket;
	sockaddr_in remote_server;
	HOSTENT *host;
	WORD 		versionRequested;
	WSADATA	wsaData;

    long nRet;
	char outbuffer[100];
	char localhostname[100] = "";
	//HOSTENT FAR * namestruct;

	//Get data from message parameter
	strcpy(outbuffer, message);

	//Set up Winsock
	versionRequested = MAKEWORD(2,0);
	WSAStartup(versionRequested, &wsaData);
	
	
	//Get host and localhost information
	host=gethostbyname("localhost");
	//host = gethostname(localhostname, sizeof(localhostname));
	
	//Fill in Sock_addr_in structure
	ZeroMemory(&remote_server, sizeof(SOCKADDR_IN));
	//We are calling the localhost so copy the address from the host structure to remote address structure
	CopyMemory(&remote_server.sin_addr, host->h_addr_list[0], sizeof(host->h_addr_list[0]));//host->h_length
	remote_server.sin_family = AF_INET;
	remote_server.sin_port = htons((u_short) BASEPORT + destinationProcessID);

	//Create Socket
    mysocket = socket(AF_INET, SOCK_STREAM, 0);

	//Connect
	nRet = connect(mysocket, (SOCKADDR*) &remote_server, sizeof(sockaddr_in));
	if (nRet == SOCKET_ERROR)
	{
		LogWinSockError("bind() error",WSAGetLastError());
		closesocket(mysocket);
		ReleaseSemaphore(sSend, 1, &nRet);
		return;
	}

	//Communcation
	//chars_read = recv(mysocket, inbuffer, 100, 0);
	LogEvent("Sending message to Process %d", destinationProcessID);
	LogEvent("Message Says:");
	LogEvent("%s", outbuffer);
	nRet = send(mysocket, outbuffer, sizeof(outbuffer), 0);
	
	if (nRet == SOCKET_ERROR)
	{
		LogWinSockError("bind() error", WSAGetLastError());
	}
	
	closesocket(mysocket);
	WSACleanup();
	ReleaseSemaphore(sSend, 1, &nRet);

}//end of sendMessage function