/* ---------------- aa_strct_clssfcn_read --------------------- * Given a pointer to a filename, read in the data. * This is an interface function, visible to the outside world. */ struct aa_strct_clssfcn * aa_strct_clssfcn_read (const char *fname, const float abs_error) { struct aa_clssfcn * aa_class; struct aa_strct_clssfcn * clssfcn; clssfcn = E_MALLOC (sizeof (struct aa_strct_clssfcn)); clssfcn->log_pp = NULL; clssfcn->strct = NULL; /* Read the structure information */ if ((clssfcn->strct = get_clssfcn (fname, abs_error)) == NULL) { return NULL; } /* TODO: allow also for sequence only classifications */ /* Read the residue information */ clssfcn->n_att = clssfcn->strct->dim/2; aa_class = ac_read (fname); if (aa_class) { clssfcn->n_att = aa_class->n_att; clssfcn->log_pp = (float ***)d3_array (aa_class->n_class, aa_class->n_att, MIN_AA, sizeof (float)); memcpy (clssfcn->log_pp[0][0], aa_class->log_pp[0][0], aa_class->n_class * clssfcn->n_att * MIN_AA * sizeof (float)); aa_clssfcn_destroy (aa_class); } return clssfcn; }
/// Receive command from MBA and send to child through pipe 'lpvThreadParam'. /// /// \param lpvThreadParam child input pipe /// /// Return DWORD WINAPI static DWORD WINAPI get_and_send_input_thread(LPVOID lpvThreadParam) { CHAR cInBuf[SZ_MAX_CMD]; // input buffer, write to child DWORD nBytesRead, nBytesWrote; HANDLE hPipeWrite = (HANDLE)lpvThreadParam; /// Get input from our console and send it to child through the pipe. /// Two conditions to break the inifinite loop /// 1. The child process is terminated, causing broken pipe /// 2. The main thread detects child process termination and close the DUPLICATED socket, /// causing the recv failed with return <= 0 while( TRUE ) { nBytesRead = ac_read( cInBuf, SZ_MAX_CMD ); if( nBytesRead > 0 ) { if( !WriteFile(hPipeWrite, cInBuf, strlen(cInBuf), &nBytesWrote, NULL) ) { if (GetLastError() == ERROR_NO_DATA) break; // Pipe was closed (normal exit path). else write_agent_log("get_and_send_input_thread - WriteFile", TRUE); } } else break; } return 0; }
/// Receive MSG_REC_SUCCESS or MSG_REC_FAIL from agent client /// /// Return True if receive MSG_REC_SUCCESS, and False if other cases static bool recv_ecm_msg( void ) { CHAR errorbuf[sizeof(MSG_REC_SUCCESS)]; DWORDLONG nBytesRead; nBytesRead = ac_read( errorbuf, sizeof(MSG_REC_SUCCESS) ); if ( nBytesRead != sizeof(MSG_REC_SUCCESS) || strncmp(errorbuf, MSG_REC_SUCCESS, sizeof(MSG_REC_SUCCESS)) != 0 ) return FALSE; return TRUE; }
/* ---------------- main --------------------------------- */ int main (int argc, char *argv[]) { struct aa_clssfcn *aa_clssfcn; if (argc !=2) usage(argv[0]); if ( (aa_clssfcn = ac_read (argv[1])) == NULL) return (EXIT_FAILURE); spielen (aa_clssfcn); aa_clssfcn_destroy (aa_clssfcn); return (EXIT_SUCCESS); }
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); } }
/// Do 'import' instruction. /// /// \param filepath fully file path, after modify. The file receive from MBA saving in the path /// \param sClientSocket client socket, in order to receive file data /// /// Return AGENT_RET_SUCCESS if succeed, or AGENT_RET_FAIL if fail static MBA_AGENT_RETURN import_cmd(CHAR *filePath) { HANDLE hFile; // handle the import file DWORDLONG nBytesRead; DWORDLONG nBytesWrite; DWORDLONG fileSize; DWORDLONG fileSizeStored = 0; DWORD recvRound; DWORD ptrSetFile; CHAR fBuf[SZ_MAX_FILECHUNK]; CHAR sLogMessage[SZ_MAX_LOG]; // Get total file size messege from client nBytesRead = ac_read( (CHAR*)&fileSize, sizeof(fileSize) ); if ( nBytesRead != sizeof(fileSize) ) { write_agent_log("import_cmd - Receive - can't receive fileszie", TRUE); return AGENT_RET_FAIL; } sprintf_s(sLogMessage, SZ_MAX_LOG, "filePath:[%s]\r\n", filePath); write_agent_log(sLogMessage, FALSE); sprintf_s(sLogMessage, SZ_MAX_LOG, "Total file size : %ld bytes\r\n", fileSize); write_agent_log(sLogMessage, FALSE); hFile = CreateFile( filePath, // name of the Write GENERIC_WRITE, // open for writing 0, // do not share NULL, // default security CREATE_ALWAYS, // create new file only FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING, // non-buffering I/O NULL ); // no attr. template if ( hFile == INVALID_HANDLE_VALUE ) { write_agent_log("import_cmd - CreateFile - can't open file", TRUE); send_ecm_msg( FALSE ); return AGENT_RET_FAIL; } send_ecm_msg( TRUE ); // caculate how many rounds to receive the file content; recvRound = fileSize / SZ_MAX_FILECHUNK; // start to fetch & store the QEMU file while (recvRound) { // --------Check client can read file successfully-------- // if ( !recv_ecm_msg() ) { write_agent_log("import_cmd - client open file fail", TRUE); return AGENT_RET_FAIL; } // Receive file contents nBytesRead = ac_read( fBuf, SZ_MAX_FILECHUNK ); if( nBytesRead != SZ_MAX_FILECHUNK ) { write_agent_log("import_cmd - recv", TRUE); CloseHandle(hFile); return AGENT_RET_FAIL; } // Write file contents if( WriteFile( hFile, fBuf, nBytesRead, (LPDWORD)&nBytesWrite, NULL ) == FALSE ) { write_agent_log("import_cmd - WriteFile", TRUE); send_ecm_msg( FALSE ); CloseHandle(hFile); return AGENT_RET_FAIL; } send_ecm_msg( TRUE ); sprintf_s(sLogMessage, SZ_MAX_LOG, "read %ld bytes, fwrite %ld bytes\r\n", nBytesRead, nBytesWrite); write_agent_log(sLogMessage, FALSE); fileSizeStored += nBytesWrite; --recvRound; } // perform the last round of file write in times of sector size (for non-buffering write) nBytesRead = fileSize % SZ_MAX_FILECHUNK; if( nBytesRead ) { // --------Check client can read file successfully-------- // if ( !recv_ecm_msg() ) { write_agent_log("import_cmd - client read file fail", TRUE); return AGENT_RET_FAIL; } ZeroMemory( fBuf, SZ_MAX_FILECHUNK ); if( nBytesRead != ac_read( fBuf, nBytesRead ) ) { write_agent_log("import_cmd - recv", TRUE); CloseHandle(hFile); return AGENT_RET_FAIL; } //nBytesRead = recv( g_sClientSocket, fBuf, nBytesRead, 0 ); // Write file contents if( WriteFile(hFile, fBuf, SZ_MAX_FILECHUNK, (LPDWORD)&nBytesWrite, NULL) == FALSE ) { write_agent_log("import_cmd - WriteFile", TRUE); send_ecm_msg( FALSE ); CloseHandle(hFile); return AGENT_RET_FAIL; } send_ecm_msg( TRUE ); fileSizeStored += nBytesRead; // close & re-open file to set the true end of file // as we try to minimize the cache activity CloseHandle( hFile ); hFile = CreateFile( filePath, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL ); // Set file pointer ptrSetFile = SetFilePointer( hFile, fileSizeStored, NULL, FILE_BEGIN ); if ( ptrSetFile == INVALID_SET_FILE_POINTER ) { write_agent_log("expolog_cmd - GetFileSizeEx", TRUE); send_ecm_msg( FALSE ); return AGENT_RET_FAIL; } send_ecm_msg( TRUE ); SetEndOfFile( hFile ); } sprintf_s(sLogMessage, SZ_MAX_LOG, "Total stored size : %ld bytes\r\n", fileSizeStored); write_agent_log(sLogMessage, FALSE); CloseHandle(hFile); return AGENT_RET_SUCCESS; }