示例#1
0
bool CServerWorkFlow::Accept(SOCKET hListenSock)
{
	CSocket socket;
	SOCKET hOldSock = socket.Detach();
	socket.Attach(hListenSock);
	BOOL bAccept = socket.Accept(*m_pServerSocket);
	socket.Detach();
	socket.Attach(hOldSock);
	return (TRUE == bAccept);
}
示例#2
0
/**
 * listens and accepts connections on the server
 */
BOOL maliciousot::ConnectionManagerServer::setup_connection() {
    
    int num_connections = m_num_of_threads+1;

    cerr << "ConnectionManagerServer->setup_connection() started." << endl;
    
    // try to bind() and then listen
    if ((!m_sockets[0].Socket()) || 
	(!m_sockets[0].Bind(m_port, m_address)) ||
	(!m_sockets[0].Listen()) ) {
	goto listen_failure;
    }
    
    for(int i = 0; i<num_connections; i++) { //twice the actual number, due to double sockets for OT
	CSocket sock;
      
	// try: CSocket sock = accept()
	if(!m_sockets[0].Accept(sock)) {
	    cerr << "Error in accept" << endl;
	    goto listen_failure;
	}

	// cerr << "Server: accept succeded i = " << i << endl;
    
	// receive the other side thread id (the first thing that is sent on the socket)
	UINT threadID;
	sock.Receive(&threadID, sizeof(int));

	// cerr << "Server: received threadID = " << threadID << endl;
    
	// ??
	if(threadID >= num_connections) {
	    // cerr << "Server: threadID >= num_connections, num_connections = " << num_connections << endl;
	    sock.Close();
	    i--;
	    continue;
	}

	// locate the socket appropriately
	// cerr << "Server: attaching socket to threadID = " << threadID << endl;
	m_sockets[threadID].AttachFrom(sock);
	sock.Detach();
    }

    cerr << "ConnectionManagerServer->setup_connection() ended." << endl;
    
    return TRUE;

 listen_failure:
    cerr << "Listen failed" << endl;
    return FALSE;
}
示例#3
0
BOOL Listen() {
#ifndef BATCH
    cout << "Listening: " << m_nAddr << ":" << m_nPort
         << ", with size: " << m_nNumOTThreads << endl;
#endif
    if (!m_vSocket->Socket()) {
        goto listen_failure;
    }
    if (!m_vSocket->Bind(m_nPort, m_nAddr)) {
        goto listen_failure;
    }
    if (!m_vSocket->Listen()) {
        goto listen_failure;
    }

    for (int i = 0; i < 1;
         i++) // twice the actual number, due to double sockets for OT
    {
        CSocket sock;
        // cout << "New round! " << endl;
        if (!m_vSocket->Accept(sock)) {
            cerr << "Error in accept" << endl;
            goto listen_failure;
        }

        UINT threadID;
        sock.Receive(&threadID, sizeof(int));

        if (threadID >= 1) {
            sock.Close();
            i--;
            continue;
        }

#ifndef BATCH
        cout << " (" << m_nPID << ") (" << threadID << ") connection accepted"
             << endl;
#endif
        // locate the socket appropriately
        m_vSocket->AttachFrom(sock);
        sock.Detach();
    }

#ifndef BATCH
    cout << "Listening finished" << endl;
#endif
    return TRUE;

listen_failure:
    cout << "Listen failed" << endl;
    return FALSE;
}
示例#4
0
void CIPServer::OnAccept(int nErrorCode)
{
	CCardSwipeServiceApp* pApp = (CCardSwipeServiceApp*)AfxGetApp();
	CSocket newSocket;
	SOCKADDR saddr;
	char ch[96];
	CString sAddress;
	CString sIPAddress, sError;
	long nCardReaderId = -1;
	int iReturn;

	this->Accept(newSocket);
	memset(&saddr,0,sizeof(SOCKADDR));
	int iLen = sizeof(saddr);
	newSocket.GetSockName(&saddr,&iLen);
	// get dotted ip address
	lstrcpyA(ch,inet_ntoa(((SOCKADDR_IN*)&saddr)->sin_addr));
	sIPAddress = ch;
	sAddress.Format("%s:%d",ch,((SOCKADDR_IN*)&saddr)->sin_port);

	// is this address in the database?
	OysterClassLibrary::IOysterPtr pOyster;
	OysterClassLibrary::ICardReaderPtr pCardReader;

	HRESULT hr = pOyster.CreateInstance(OysterClassLibrary::CLSID_Oyster);
	if (hr != ERROR_SUCCESS)
	{
		GetLastErrorString(sError,hr);
		throw CGeneralException("CIPServer::OnAccept","Could not create OysterClassLibrary object (%d): %s",hr,sError);
	}

	memset(ch,0,sizeof(ch));
	iReturn = newSocket.Receive(ch,sizeof(ch));
	if (iReturn <= 0)
	{
		// either closed or an error occured
		GetError(sError,iReturn);
		throw CGeneralException("CIPServer::OnAccept","Receive function failed (%d): %s",iReturn,sError);
	}
	nCardReaderId = (long)&ch[0];
	pCardReader = pOyster->GetCardReaderById(nCardReaderId);
	if (pCardReader)
	{
		CVirtualCardReaderSocket* pNewSocket = new CVirtualCardReaderSocket(nCardReaderId);
		pNewSocket->Attach(newSocket.m_hSocket);
		newSocket.Detach();
		pNewSocket->Send(ECMD_COMMANDSTRING[ECMD_CONNECTSUCCESS],strlen(ECMD_COMMANDSTRING[ECMD_CONNECTSUCCESS]));
		pApp->m_mapCardSwipeIdToCardSwipe
		pApp->m_vClients.push_back(pNewSocket);
		DebugTell(_T("New client connection from %s\n"),(LPCTSTR)sAddress);
	}
	else
	{
		// invalid connection, send notification
		newSocket.Send(ECMD_COMMANDSTRING[ECMD_ERRORCONNECT],strlen(ECMD_COMMANDSTRING[ECMD_ERRORCONNECT]));
	}
	pOyster.Release();

	UNREFERENCED_PARAMETER(nErrorCode);

	CSocket::OnAccept(nErrorCode);
}
示例#5
0
	void CServerHandshake::Run()
	{
		VERIFY(AfxSocketInit());

		CSocket socket; // führen Handshake innerhalb des Threads synchron aus
		socket.Attach(m_hSocket);

		clock_t end = clock() + HANDSHAKE_TIMEOUT * CLOCKS_PER_SEC;

		// (1.) auf eingehenden Identifizierungs-String (HANDSHAKE_CLIENTID) + Versionsnummer
		// vom Client warten
		UINT nCount = strlen(HANDSHAKE_CLIENTID) + 1 + sizeof(UINT) + 1;
		if (!RequestData(socket, nCount, end))
		{
			DEBUG_MSG("client timed out, disconnected");
			SetExitCode(1);
			return;
		}

		// Daten in Puffer übertragen
		BYTE *lpBuf = new BYTE[nCount];
		int nDone = socket.Receive(lpBuf, nCount);
		if (nDone == SOCKET_ERROR) goto sockerror;
		else if (nDone < nCount) goto error;

		// Pufferinhalt prüfen
		if (memcmp(lpBuf, HANDSHAKE_CLIENTID, strlen(HANDSHAKE_CLIENTID)) != 0
			|| lpBuf[strlen(HANDSHAKE_CLIENTID)] != 0
			|| lpBuf[nCount - 1] != 0)
		{
			DEBUG_MSG("client is not a BotE client, disconnected");
			delete[] lpBuf;
			SetExitCode(1);
			return;
		}

		// Puffer erfolgreich geprüft, Versionsnummer extrahieren
		UINT nClientVersion;
		memcpy(&nClientVersion, lpBuf + (strlen(HANDSHAKE_CLIENTID) + 1), sizeof(UINT));
		TRACE("server: client has version %u\n", nClientVersion);
		delete[] lpBuf;
		lpBuf = NULL;

		// abbrechen, wenn andere Versionsnummer
		if (nClientVersion != m_nServerVersion)
		{
			DEBUG_MSG("client has incompatible version, disconnected");
			SetExitCode(1);
			return;
		}

		// (2.) Server-Identifizierung + Versionsnummer senden
		nDone = socket.Send(HANDSHAKE_SERVERID, strlen(HANDSHAKE_SERVERID));
		if (nDone == SOCKET_ERROR) goto sockerror;
		else if (nDone < strlen(HANDSHAKE_SERVERID)) goto error;

		lpBuf = new BYTE[nCount = 1 + sizeof(UINT) + 1];
		lpBuf[0] = lpBuf[nCount - 1] = 0;
		memcpy(&lpBuf[1], &m_nServerVersion, sizeof(UINT));

		nDone = socket.Send(lpBuf, nCount);
		if (nDone == SOCKET_ERROR) goto sockerror;
		else if (nDone < nCount) goto error;

		delete[] lpBuf;
		lpBuf = NULL;

		// (3.a) Bestätigung vom Client abwarten
		int nLen = 3 + sizeof(UINT);
		if (!RequestData(socket, nLen, end))
		{
			DEBUG_MSG("client timed out, disconnected");
			SetExitCode(1);
			return;
		}

		// Bestätigung prüfen
		lpBuf = new BYTE[3];
		nDone = socket.Receive(lpBuf, 3);
		if (nDone == SOCKET_ERROR) goto sockerror;
		else if (nDone < 3) goto error;

		if (memcmp(lpBuf, "OK\0", 3) != 0)
		{
			DEBUG_MSG("client did not confirm, disconnected");
			delete[] lpBuf;
			SetExitCode(1);
			return;
		}

		delete[] lpBuf;

		// Länge des Benutzernamens ermitteln
		lpBuf = new BYTE[sizeof(UINT)];
		nDone = socket.Receive(lpBuf, sizeof(UINT));
		if (nDone == SOCKET_ERROR) goto sockerror;
		else if (nDone < sizeof(UINT)) goto error;

		UINT nStrLen = 0;
		memcpy(&nStrLen, lpBuf, sizeof(UINT));

		delete[] lpBuf;

		// (3.b) Benutzername empfangen
		if (!RequestData(socket, nStrLen, end))
		{
			DEBUG_MSG("client timed out, disconnected");
			SetExitCode(1);
			return;
		}

		// Benutzername lesen
		lpBuf = new BYTE[nStrLen + 1];
		nDone = socket.Receive(lpBuf, nStrLen);
		if (nDone == SOCKET_ERROR) goto sockerror;
		else if (nDone < nStrLen) goto error;

		{
			lpBuf[nStrLen] = 0;
			CString strUserName((char *)lpBuf);
			strUserName.Trim();

			// dem Server einen neuen Client melden; verwenden kleinere Versionsnummer zur
			// Kommunikation
			server.OnNewClient(socket.Detach(), min(m_nServerVersion, nClientVersion), strUserName);
		}

		delete[] lpBuf;
		return;

	sockerror:
		server.OnSocketError(socket.GetLastError());
	error:
		if (lpBuf) delete[] lpBuf;
		SetExitCode(1);
	}