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); } }
void mi_datagram_server(int rx_sock, int tx_sock) { struct mi_root *mi_cmd; struct mi_root *mi_rpl; struct mi_handler *hdl; struct mi_cmd * f; datagram_stream dtgram; int ret, len; ret = 0; f = 0; while(1){/*read the datagram*/ memset(mi_buf, 0, DATAGRAM_SOCK_BUF_SIZE); reply_addr_len = sizeof(reply_addr); /* get the client's address */ ret = recvfrom(rx_sock, mi_buf, DATAGRAM_SOCK_BUF_SIZE, 0, (struct sockaddr*)&reply_addr, &reply_addr_len); if (ret == -1) { LM_ERR("recvfrom: (%d) %s\n", errno, strerror(errno)); if ((errno == EINTR) || (errno == EAGAIN) || (errno == EWOULDBLOCK) || (errno == ECONNREFUSED)) { LM_DBG("got %d (%s), going on\n", errno, strerror(errno)); continue; } LM_DBG("error in recvfrom\n"); continue; } if(ret == 0) continue; LM_DBG("received %.*s\n", ret, mi_buf); if(ret> DATAGRAM_SOCK_BUF_SIZE){ LM_ERR("buffer overflow\n"); continue; } LM_DBG("mi_buf is %s and we have received %i bytes\n",mi_buf, ret); dtgram.start = mi_buf; dtgram.len = ret; dtgram.current = dtgram.start; ret = identify_command(&dtgram, &f); /*analyze the command--from the first line*/ if(ret != 0) { LM_ERR("command not available\n"); mi_send_dgram(tx_sock, MI_COMMAND_NOT_AVAILABLE, MI_COMMAND_AVAILABLE_LEN, (struct sockaddr* )&reply_addr, reply_addr_len, mi_socket_timeout); continue; } LM_DBG("we have a valid command \n"); /* if asyncron cmd, build the async handler */ if (f->flags&MI_ASYNC_RPL_FLAG) { hdl = build_async_handler(mi_socket_domain, &reply_addr, reply_addr_len, tx_sock); if (hdl==0) { LM_ERR("failed to build async handler\n"); mi_send_dgram(tx_sock, MI_INTERNAL_ERROR, MI_INTERNAL_ERROR_LEN,(struct sockaddr* )&reply_addr, reply_addr_len, mi_socket_timeout); continue; } } else{ hdl = 0; } LM_DBG("after identifing the command, the received datagram is %s\n", dtgram.current); /*if no params required*/ if (f->flags&MI_NO_INPUT_FLAG) { LM_DBG("the command has no params\n"); mi_cmd = 0; } else { LM_DBG("parsing the command's params\n"); mi_cmd = mi_datagram_parse_tree(&dtgram); if (mi_cmd==NULL){ LM_ERR("failed to parse the MI tree\n"); mi_send_dgram(tx_sock, MI_PARSE_ERROR, MI_PARSE_ERROR_LEN, (struct sockaddr* )&reply_addr, reply_addr_len, mi_socket_timeout); free_async_handler(hdl); continue; } mi_cmd->async_hdl = hdl; } LM_DBG("done parsing the mi tree\n"); if ( (mi_rpl=run_mi_cmd(f, mi_cmd, (mi_flush_f *)mi_datagram_flush_tree, &dtgram))==0 ) { /*error while running the command*/ LM_ERR("failed to process the command\n"); mi_send_dgram(tx_sock, MI_COMMAND_FAILED, MI_COMMAND_FAILED_LEN, (struct sockaddr* )&reply_addr, reply_addr_len, mi_socket_timeout); goto failure; } /*the command exited well*/ LM_DBG("command process (%s)succeded\n",f->name.s); if (mi_rpl!=MI_ROOT_ASYNC_RPL) { if(mi_datagram_write_tree(&dtgram , mi_rpl) != 0){ LM_ERR("failed to build the response \n"); goto failure; } len = dtgram.current - dtgram.start; ret = mi_send_dgram(tx_sock, dtgram.start,len, (struct sockaddr* )&reply_addr, reply_addr_len, mi_socket_timeout); if (ret>0){ LM_DBG("the response: %s has been sent in %i octets\n", dtgram.start, ret); }else{ LM_ERR("failed to send the response: %s (%d)\n", strerror(errno), errno); } free_mi_tree( mi_rpl ); free_async_handler(hdl); if (mi_cmd) free_mi_tree( mi_cmd ); }else { if (mi_cmd) free_mi_tree( mi_cmd ); } continue; failure: free_async_handler(hdl); /* destroy request tree */ if (mi_cmd) free_mi_tree( mi_cmd ); /* destroy the reply tree */ if (mi_rpl) free_mi_tree(mi_rpl); continue; } }
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(); } }