Ejemplo n.º 1
0
void server_mainloop( SOCKET server_socket ) 
{
    SOCKET sClientSocket;
    
    int iResult,                        // system information with error message
        iSendResult,                    // system information with error message after send function
        command_code;
    MBA_AGENT_RETURN command_result;
    
    CHAR sLogMessage[SZ_MAX_LOG];
    CHAR sRecvbuf[SZ_MAX_CMD << 1];          // receive buffer from client
    CHAR sCommand_line[SZ_MAX_CMD << 1];     // fully command line
                                             // we allocate two times more space for the string manipulation
    // Accept connections
    while( true ) {
        
        g_sClientSocket = server_socket;

        sprintf_s(sLogMessage, SZ_MAX_LOG, "[SYSTEM] Start to get commands...\r\n\r\n");
        write_agent_log(sLogMessage, FALSE);
        
        slen = sizeof(clientaddr);

        // Receive until the peer shuts down the connection
        do {
            
            ZeroMemory(sRecvbuf, sizeof(sRecvbuf));
            
            iResult = ac_read( sRecvbuf, SZ_MAX_CMD );
            
            if (iResult > 0) {
            
                sprintf_s(sLogMessage, SZ_MAX_LOG, "Bytes received: 0x%08x, Received message: %s\r\n", iResult, sRecvbuf);
                write_agent_log(sLogMessage, FALSE);
                
                command_code = identify_command(sRecvbuf);
                
                if (command_code > 0) {
                    ZeroMemory(sCommand_line, sizeof(sCommand_line));
                    memcpy(sCommand_line, &sRecvbuf[5], SZ_MAX_CMD - 5);
                }

                switch (command_code) {
                    
                    case MBA_CMD_EXEC :
                        command_result = execute_cmd(sCommand_line);
                        break;

                    case MBA_CMD_INVO :
                        command_result = invoke_cmd(sCommand_line);
                        break;

                    case MBA_CMD_EXPO :
                        command_result = export_cmd(sCommand_line);
                        break;

                    case MBA_CMD_IMPO :
                        command_result = import_cmd(sCommand_line);
                        break;

                    case MBA_CMD_LOGF :
                        command_result = expolog_cmd();
                        break;
                        
                    case MBA_CMD_SYNC :
                        command_result = sync_cmd();
                        break;

                    case MBA_CMD_UNKNOWN :
                    default:
                        write_agent_log("main - [COMMAND ERROR] Unknown command", TRUE);
                        command_result = AGENT_RET_SUCCESS;    
                        break;
                }
        
                if ( command_result == AGENT_RET_SUCCESS ) {
                
                    // Echo the buffer back to the sender
                    iSendResult = ac_write( MSG_ACK_PREFIX, sizeof(MSG_ACK_PREFIX) );
                    if (iSendResult == SOCKET_ERROR) {
                        write_agent_log("main - Send to server_socket", TRUE);
                        break;
                    }
                            
                    iSendResult = ac_write( sRecvbuf, SZ_MAX_CMD );
                    if (iSendResult == SOCKET_ERROR) {
                        write_agent_log("main - Send to server_socket", TRUE);
                        break;
                    }
                }
                else 
                    iResult = 1;
                        
                sprintf_s(sLogMessage, SZ_MAX_LOG, "Bytes sent: %d\r\n\r\n", iSendResult);
                write_agent_log(sLogMessage, FALSE);
            }
            else if (GetLastError() == 0) {
                write_agent_log("Receive nothing from server_socket", FALSE);
                break;
            }
            else {
                write_agent_log("Receive from server_socket", TRUE);
                break;
            }
        } while (iResult > 0);
        
        // shutdown the connection since we're done
        iResult = shutdown(server_socket, SD_SEND);
        if (iResult != -1 && iResult == SOCKET_ERROR) {
            closesocket(server_socket);
            write_agent_log("Main - shutdown server_socket", TRUE);
        }
        closesocket(server_socket);
        sprintf_s(sLogMessage, SZ_MAX_LOG, "[SYSTEM] Connection closing...\r\n");
        write_agent_log(sLogMessage, FALSE);  
    }
}
Ejemplo n.º 2
0
void server_mainloop( SOCKET sListenSocket ) 
{
    SOCKET sClientSocket;
    
    int iResult,                        // system information with error message
        iSendResult,                    // system information with error message after send function
        command_code;
    MBA_AGENT_RETURN command_result;
    
    char sRecvbuf[SZ_MAX_CMD << 1];          // receive buffer from client
    char sCommand_line[SZ_MAX_CMD << 1];     // fully command line
                                             // we allocate two times more space for the string manipulation
    // Accept connections
    while( true ) {
        
        // Accept a client socket
        sClientSocket = accept(sListenSocket, NULL, NULL);
        if (sClientSocket == INVALID_SOCKET) {
            display_error("main - accept", FALSE);
            break;
        }
        
        sprintf_s(g_sLogMessage, SZ_MAX_LOG, "[SYSTEM] Connection starting...\r\n");
        write_log();
        
        g_sClientSocket = sClientSocket;

        // Receive until the peer shuts down the connection
        do {
            
            ZeroMemory(sRecvbuf, sizeof(sRecvbuf));
            iResult = recv(sClientSocket, sRecvbuf, SZ_MAX_CMD, MSG_WAITALL);
            
            if (iResult > 0) {
            
                sprintf_s(g_sLogMessage, SZ_MAX_LOG, "Bytes received: 0x%08x, Received message: %s\r\n", iResult, sRecvbuf);
                write_log();
                
                command_code = identify_command(sRecvbuf);
                
                if (command_code > 0) {
                    ZeroMemory(sCommand_line, sizeof(sCommand_line));
                    memcpy(sCommand_line, &sRecvbuf[5], SZ_MAX_CMD - 5);
                }

                switch (command_code) {
                    
                    case MBA_CMD_EXEC :
                        command_result = execute_cmd(sCommand_line);
                        break;

                    case MBA_CMD_INVO :
                        command_result = invoke_cmd(sCommand_line);
                        break;

                    case MBA_CMD_EXPO :
                        command_result = export_cmd(sCommand_line, sClientSocket);
                        break;

                    case MBA_CMD_IMPO :
                        command_result = import_cmd(sCommand_line, sClientSocket);
                        break;

                    case MBA_CMD_LOGF :
                        command_result = export_log( sClientSocket );
                        break;

            case MBA_CMD_UNKNOWN :
            default:
            display_error("main - [COMMAND ERROR] Unknown command", TRUE);
            command_result = AGENT_RET_SUCCESS;    
                        break;
                }
        
        if ( command_result == AGENT_RET_SUCCESS ) {
        
                    // Echo the buffer back to the sender
                    iSendResult = send(sClientSocket, MSG_ACK_PREFIX, sizeof(MSG_ACK_PREFIX), 0);
                    if (iSendResult == SOCKET_ERROR) {
                        display_error("main - Send to sClientSocket", FALSE);
                        break;
                    }
                
                    iSendResult = send(sClientSocket, sRecvbuf, SZ_MAX_CMD, 0);
            if (iSendResult == SOCKET_ERROR) {
                            display_error("main - Send to sClientSocket", FALSE);
                            break;
                    }
        }
        else 
            iResult = 1;
                
                sprintf_s(g_sLogMessage, SZ_MAX_LOG, "Bytes sent: %d\r\n", iSendResult);
                write_log();
            }
            else if (GetLastError() == 0)
                break;
            else {
                display_error("Receive from sClientSocket", FALSE);
                break;
            }
        } while (iResult > 0);

        // shutdown the connection since we're done
        iResult = shutdown(sClientSocket, SD_SEND);
        if (iResult != -1 && iResult == SOCKET_ERROR) {
            closesocket(sClientSocket);
            display_error("Main - shutdown sClientSocket", FALSE);
        }

        closesocket(sClientSocket);
        sprintf_s(g_sLogMessage, SZ_MAX_LOG, "[SYSTEM] Connection closing...\r\n");
        write_log();  
    }    
}