void CTCPIPSystemSrvr::send_error(short error, short error_detail, const CEE_handle_def *call_id_) { long tag; short fnum; _cc_status cc; short wcount; long wtimeout = 10 * 100; if (m_nSocketFnum < 0) goto bailout; m_rhdr.error = error; m_rhdr.error_detail = error_detail; m_rhdr.hdr_type = SRVR_TRANSPORT_ERROR; memcpy(m_IObuffer,&m_rhdr,sizeof(m_rhdr)); if (m_rhdr.swap == SWAP_YES) HEADER_swap((HEADER*)m_IObuffer); if (send_nw2(m_nSocketFnum, m_IObuffer, sizeof(HEADER), 0, 1) <0) goto bailout; fnum = m_nSocketFnum; cc = AWAITIOX(&fnum,,&wcount, &tag, wtimeout); if (!_status_eq(cc)) goto bailout; bailout: DEALLOCATE_TEMP_MEMORY(&m_call_id); }
int WRITE_TCPIP_RESPONSE(CTCPIPSystemSrvr* pnode, unsigned long message_length) { HEADER* hdr; char* buffer; short length; unsigned long total_length; total_length = message_length; short retcode = 0; short tx_handle[10]; memset(&tx_handle[0],0,20); retcode = TMF_GETTXHANDLE_(&tx_handle[0]); if (retcode == 0) TMF_BEGINTAG_FROM_TXHANDLE_(&tx_handle[0], &pnode->m_trans_begin_tag); else pnode->m_trans_begin_tag = 0; RESET_ERRORS((long)pnode); buffer = pnode->m_wbuffer; hdr = (HEADER*)pnode->m_wbuffer; memcpy(hdr, &pnode->m_rhdr, sizeof(HEADER)); hdr->total_length = message_length - sizeof(HEADER); if (hdr->compress_ind == COMP_YES && hdr->total_length > MIN_LENGTH_FOR_COMPRESSION) { total_length -= sizeof(HEADER); DoCompression(pnode, hdr, (unsigned char*)(buffer+sizeof(HEADER)), (unsigned long&)total_length); total_length += sizeof(HEADER); } else hdr->compress_type = 0; memcpy(&pnode->m_whdr, hdr, sizeof(HEADER)); if (hdr->swap == SWAP_YES) HEADER_swap(hdr); while (total_length > 0) { if (total_length > TCPI_SRVR_SEND_BUFFER) length = TCPI_SRVR_SEND_BUFFER; else length = total_length; if (pnode->do_write(buffer, length, WRITE_TIMEOUT)== false) { pnode->send_error(SRVR_ERR_WRITE_OPERATION,0, NULL); return 1; } total_length -= length; buffer += length; } return 0; }
void traceBUFFER(int operation, HEADER* header,char* buffer, long count) { HEADER hdr; memcpy(&hdr, header, sizeof(HEADER)); if (operation == WRITE_REQUEST_FIRST) { if (header->swap == 'Y') HEADER_swap(&hdr); hex_out( sizeof(HEADER), (char*)&hdr, "HEADER" ); } else if (operation == READ_RESPONSE_FIRST) { hex_out( sizeof(HEADER), (char*)&hdr, "HEADER" ); } hex_out( count, buffer, "DATA" ); }
void CTCPIPSystemSrvr::send_error(short error, short error_detail, const CEE_handle_def *call_id_) { if (m_nSocketFnum >= 0) { m_rhdr.error = error; m_rhdr.error_detail = error_detail; m_rhdr.hdr_type = SRVR_TRANSPORT_ERROR; memcpy(m_IObuffer,&m_rhdr,sizeof(m_rhdr)); if (m_rhdr.swap == SWAP_YES) HEADER_swap((HEADER*)m_IObuffer); if (send(m_nSocketFnum, m_IObuffer, sizeof(HEADER), 0) <= 0) { // Error in send error_cleanup(); } } if (GTransport.m_TempMemory_list.m_list_length) DEALLOCATE_TEMP_MEMORY(&m_call_id); }
int WRITE_TCPIP_RESPONSE(CTCPIPSystemSrvr* pnode, unsigned long message_length) { SRVRTRACE_ENTER(FILE_TSS+16); HEADER* hdr; char* buffer; int length; unsigned long total_length; total_length = message_length; short retcode = 0; short tx_handle[10]; memset(&tx_handle[0],0,20); retcode = GETTRANSID(&tx_handle[0]); if (retcode == 0) pnode->m_trans_begin_tag = tx_handle[0]; else pnode->m_trans_begin_tag = 0; if(GTransport.m_error_list.m_list_length) RESET_ERRORS((long)pnode); buffer = pnode->m_wbuffer; hdr = (HEADER*)pnode->m_wbuffer; memcpy(hdr, &pnode->m_rhdr, sizeof(HEADER)); hdr->total_length = message_length - sizeof(HEADER); if (hdr->compress_ind == COMP_YES && hdr->total_length > MIN_LENGTH_FOR_COMPRESSION) { total_length -= sizeof(HEADER); DoCompression(pnode, hdr, (unsigned char*)(buffer+sizeof(HEADER)), (unsigned long&)total_length); total_length += sizeof(HEADER); } else hdr->compress_type = COMP_NO_COMPRESSION; memcpy(&pnode->m_whdr, hdr, sizeof(HEADER)); if (hdr->swap == SWAP_YES) HEADER_swap(hdr); while (total_length > 0) { if (total_length > MAX_TCP_BUFFER_LENGTH) length = MAX_TCP_BUFFER_LENGTH; else length = total_length; if (pnode->do_write(buffer, length, pnode->m_whdr.operation_id, WRITE_TIMEOUT)== false) { // if there is error writing to a socket, makes no sense trying to write an error response to the socket // (if there is a send error, we'll actually cleanup the tcp/ip session, so there is no pnode either) SRVRTRACE_EXIT(FILE_TSS+16); return 1; } total_length -= length; buffer += length; } SRVRTRACE_EXIT(FILE_TSS+16); return 0; }
bool READ_TCPIP_REQUEST(CTCPIPSystemSrvr* pnode) { SRVRTRACE_ENTER(FILE_TSS+15); HEADER* hdr; HEADER swappedHdr; char* buffer; int length; unsigned long total_length; if(GTransport.m_error_list.m_list_length) RESET_ERRORS((long)pnode); length = pnode->m_rlength; if (pnode->do_read(true, hdr, buffer, length, READ_TIMEOUT)== false) { // if there is error writing to a socket, makes no sense trying to write an error response to the socket // (if there is a send error, we'll actually cleanup the tcp/ip session, so there is no pnode either) SRVRTRACE_EXIT(FILE_TSS+15); return false; } if (hdr->signature != SIGNATURE) { memcpy(&swappedHdr,hdr,sizeof(HEADER)); HEADER_swap(&swappedHdr); if(swappedHdr.signature != SIGNATURE) { pnode->send_error(SRVR_ERR_WRONG_MESSAGE_FORMAT, 0, NULL); SRVRTRACE_EXIT(FILE_TSS+15); return false; } else memcpy(&pnode->m_rhdr,&swappedHdr,sizeof(HEADER)); } else memcpy(&pnode->m_rhdr,hdr,sizeof(HEADER)); if(pnode->m_rhdr.operation_id == AS_API_GETOBJREF) { if(pnode->m_rhdr.version == CLIENT_HEADER_VERSION_LE) pnode->m_rhdr.swap = SWAP_NO; else if (pnode->m_rhdr.version == CLIENT_HEADER_VERSION_BE) pnode->m_rhdr.swap = SWAP_YES; else { // reject older clients pnode->send_error(SRVR_ERR_WRONG_MESSAGE_FORMAT, 0, NULL); SRVRTRACE_EXIT(FILE_TSS+15); return false; } pnode->m_rhdr.version = SERVER_HEADER_VERSION_LE; } hdr = &pnode->m_rhdr; if (pnode->m_rhdr.compress_ind == COMP_YES && pnode->m_rhdr.compress_type != COMP_NO_COMPRESSION) total_length = hdr->cmp_length; else total_length = hdr->total_length; if(pnode->r_allocate(total_length) == NULL) { pnode->send_error(SRVR_ERR_MEMORY_ALLOCATE,0, NULL); SRVRTRACE_EXIT(FILE_TSS+15); return false; } if (length < 0 || length > total_length) { pnode->send_error(SRVR_ERR_WRONG_MESSAGE_FORMAT,0, NULL); SRVRTRACE_EXIT(FILE_TSS+15); return false; } memcpy(pnode->m_rbuffer, buffer, length); pnode->m_curptr = pnode->m_rbuffer + length; total_length -= length; while(total_length > 0) { if (pnode->do_read(false, hdr, buffer, length, READ_TIMEOUT)== false) { // if there is error writing to a socket, makes no sense trying to write an error response to the socket // (if there is a send error, we'll actually cleanup the tcp/ip session, so there is no pnode either) SRVRTRACE_EXIT(FILE_TSS+15); return false; } if (length < 0 || length > total_length) { pnode->send_error(SRVR_ERR_WRONG_MESSAGE_FORMAT,0, NULL); SRVRTRACE_EXIT(FILE_TSS+15); return false; } memcpy(pnode->m_curptr, buffer, length); pnode->m_curptr += length; total_length -= length; } if (pnode->m_rhdr.compress_ind == COMP_YES && pnode->m_rhdr.compress_type != COMP_NO_COMPRESSION) { SRVRTRACE_EXIT(FILE_TSS+15); return DoExpand(pnode, pnode->m_rhdr, (unsigned char *)pnode->m_rbuffer, total_length); } SRVRTRACE_EXIT(FILE_TSS+15); return true; }
bool WINAPI TCPIPDoWriteRead(void* ipTCPIPSystem, HEADER*& hdr, char*& buffer, int& bufcount, unsigned int timeout) { HEADER close_hdr; HEADER* error_hdr; short error = 0; short error_detail = 0; int wcount; int data_length = 0; CTCPIPSystemDrvr* pTCPIPSystem = (CTCPIPSystemDrvr*)ipTCPIPSystem; if (pTCPIPSystem->m_hSocket == NULL) { (fpTCPIPSET_ERROR)((long)pTCPIPSystem, PC, TCPIP, pTCPIPSystem->odbcAPI, E_DRIVER, pTCPIPSystem->m_object_ref, O_DO_WRITE_READ, F_CHECKCONNECTION, 999, (int)0); return false; } switch(hdr->hdr_type) { default: case WRITE_REQUEST_FIRST: memcpy(pTCPIPSystem->m_IObuffer,hdr,sizeof(HEADER)); HEADER_swap((HEADER*)pTCPIPSystem->m_IObuffer); data_length = sizeof(HEADER); case WRITE_REQUEST_NEXT: if (buffer != NULL) memcpy(pTCPIPSystem->m_IObuffer + data_length, buffer, bufcount); data_length += bufcount; if ((wcount = send_nblk(pTCPIPSystem->m_hSocket, pTCPIPSystem->m_IObuffer, data_length, 0, timeout, pTCPIPSystem)) == SOCKET_ERROR|| wcount == 0) { TCPIPCloseSession (ipTCPIPSystem); return false; } break; case READ_RESPONSE_FIRST: case READ_RESPONSE_NEXT: if ((wcount = recv_nblk(pTCPIPSystem->m_hSocket, pTCPIPSystem->m_IObuffer, MAX_TCP_BUFFER_LENGTH, 0, timeout, pTCPIPSystem)) == SOCKET_ERROR || wcount == 0) { TCPIPCloseSession (ipTCPIPSystem); return false; } if (hdr->hdr_type == READ_RESPONSE_FIRST && wcount < sizeof HEADER) { if ((wcount = recv_header(pTCPIPSystem->m_hSocket, pTCPIPSystem->m_IObuffer, wcount, MAX_TCP_BUFFER_LENGTH, sizeof HEADER, 0, timeout, pTCPIPSystem)) == SOCKET_ERROR || wcount == 0) { TCPIPCloseSession (ipTCPIPSystem); return false; } } break; case CLOSE_TCPIP_SESSION: memcpy(&close_hdr,hdr,sizeof(HEADER)); HEADER_swap((HEADER*)&close_hdr); data_length = sizeof(HEADER); send_close_session(pTCPIPSystem->m_hSocket, (char*)&close_hdr, data_length, 0, 30, pTCPIPSystem); return true; } bufcount = 0; switch(hdr->hdr_type) { case WRITE_REQUEST_FIRST: bufcount -= sizeof(HEADER); case WRITE_REQUEST_NEXT: bufcount += wcount; if (bufcount < 0) { error = DRVR_ERR_INCORRECT_LENGTH; (fpTCPIPSET_ERROR)((long)pTCPIPSystem, PC, TCPIP, pTCPIPSystem->odbcAPI, E_DRIVER, pTCPIPSystem->m_object_ref, O_DO_WRITE_READ, F_SEND, error, bufcount); TCPIPCloseSession (ipTCPIPSystem); return false; } break; case READ_RESPONSE_FIRST: bufcount = wcount - sizeof(HEADER); if (bufcount < 0) { error = DRVR_ERR_INCORRECT_LENGTH; (fpTCPIPSET_ERROR)((long)pTCPIPSystem, PC, TCPIP, pTCPIPSystem->odbcAPI, E_DRIVER, pTCPIPSystem->m_object_ref, O_DO_WRITE_READ, F_RECV, error, bufcount); TCPIPCloseSession (ipTCPIPSystem); return false; } buffer = pTCPIPSystem->m_IObuffer + sizeof(HEADER); hdr = (HEADER*)pTCPIPSystem->m_IObuffer; if (hdr->signature != SIGNATURE) { error = DRVR_ERR_WRONGSIGNATURE; (fpTCPIPSET_ERROR)((long)pTCPIPSystem, PC, TCPIP, pTCPIPSystem->odbcAPI, E_DRIVER, pTCPIPSystem->m_object_ref, O_DO_WRITE_READ, F_RECV, error, (int)0); TCPIPCloseSession (ipTCPIPSystem); return false; } if (hdr->error != 0 ) { error = DRVR_ERR_ERROR_FROM_SERVER; (fpTCPIPSET_ERROR)((long)pTCPIPSystem, PC, TCPIP, pTCPIPSystem->odbcAPI, E_DRIVER, pTCPIPSystem->m_object_ref, O_DO_WRITE_READ, F_RECV, hdr->error, hdr->error_detail); TCPIPCloseSession (ipTCPIPSystem); return false; } break; case READ_RESPONSE_NEXT: bufcount = wcount; memcpy(buffer, pTCPIPSystem->m_IObuffer, bufcount); break; case SRVR_TRANSPORT_ERROR: error_hdr = (HEADER*)(pTCPIPSystem->m_IObuffer); error = error_hdr->error; error_detail = error_hdr->error_detail; (fpTCPIPSET_ERROR)((long)pTCPIPSystem, PC, TCPIP, pTCPIPSystem->odbcAPI, E_DRIVER, pTCPIPSystem->m_object_ref, O_DO_WRITE_READ, F_SRVR_TRANSPORT_ERROR, error, error_detail); TCPIPCloseSession (ipTCPIPSystem); break; default: error = 1000; (fpTCPIPSET_ERROR)((long)pTCPIPSystem, PC, TCPIP, pTCPIPSystem->odbcAPI, E_DRIVER, pTCPIPSystem->m_object_ref, O_DO_WRITE_READ, F_HDR_TYPE, error, (int)0); TCPIPCloseSession (ipTCPIPSystem); break; } return error == 0; }
bool WINAPI TCPIPDoWriteRead(void* ipTCPIPSystem, HEADER*& hdr, char*& buffer, int& bufcount, unsigned int timeout) { HEADER close_hdr; HEADER* error_hdr; short error = 0; short error_detail = 0; int wcount; int data_length = 0; CTCPIPUnixDrvr* pTCPIPSystem = (CTCPIPUnixDrvr*)ipTCPIPSystem; if (pTCPIPSystem->m_hSocket == INVALID_SOCKET) { (fpTCPIPSET_ERROR)((long)pTCPIPSystem, UNX, TCPIP, pTCPIPSystem->odbcAPI, E_DRIVER, pTCPIPSystem->m_object_ref, O_DO_WRITE_READ, F_CHECKCONNECTION, 999, (int)0, "Invalid Socket"); return false; } switch(hdr->hdr_type) { default: case WRITE_REQUEST_FIRST: memcpy(pTCPIPSystem->m_IObuffer,hdr,sizeof(HEADER)); if(pTCPIPSystem->swap() == SWAP_YES) HEADER_swap((HEADER*)pTCPIPSystem->m_IObuffer); data_length = sizeof(HEADER); case WRITE_REQUEST_NEXT: if (buffer != NULL) memcpy(pTCPIPSystem->m_IObuffer + data_length, buffer, bufcount); data_length += bufcount; if ((wcount = send_nblk(pTCPIPSystem->m_hSocket, pTCPIPSystem->m_IObuffer, data_length, 0, timeout, pTCPIPSystem)) == SOCKET_ERROR|| wcount == 0) { (fpTCPIPSET_ERROR)((long)pTCPIPSystem, UNX, TCPIP, pTCPIPSystem->odbcAPI, E_DRIVER, pTCPIPSystem->m_object_ref, O_DO_WRITE_READ, F_SEND, 0, wcount, "WRITE_REQUEST_FIRST/NEXT Error"); TCPIPCloseSession (ipTCPIPSystem); return false; } break; case READ_RESPONSE_FIRST: case READ_RESPONSE_NEXT: wcount = recv_nblk(pTCPIPSystem->m_hSocket, pTCPIPSystem->m_IObuffer, MAX_TCP_BUFFER_LENGTH, 0, timeout, pTCPIPSystem); /* * recv_nblk returns a negative -ETIMEDOUT value in case of a timeout * for anything other than a connect/disconnect/getobject reference or a stop server request, we need * to send a Cancel request and wait for either a graceful cancel completion or a communication link failure * because of the process_stop on the MXOSRVR * A negative ETIMEDOUT value is returned -- otherwise the caller will interpret it as that many bytes read (all *NIX has a +ve value for ETIMEDOUT) */ if(wcount == -ETIMEDOUT) return false; if(wcount == SOCKET_ERROR || wcount == 0) { (fpTCPIPSET_ERROR)((long)pTCPIPSystem, UNX, TCPIP, pTCPIPSystem->odbcAPI, E_DRIVER, pTCPIPSystem->m_object_ref, O_DO_WRITE_READ, F_RECV, 0, wcount, "READ_RESPONSE_FIRST/NEXT Error"); TCPIPCloseSession (ipTCPIPSystem); return false; } if ((hdr->hdr_type == READ_RESPONSE_FIRST) && (wcount < sizeof(HEADER))) { if ((wcount = recv_header(pTCPIPSystem->m_hSocket, pTCPIPSystem->m_IObuffer, wcount, MAX_TCP_BUFFER_LENGTH, sizeof(HEADER), 0, timeout, pTCPIPSystem)) == SOCKET_ERROR || wcount == 0) { (fpTCPIPSET_ERROR)((long)pTCPIPSystem, UNX, TCPIP, pTCPIPSystem->odbcAPI, E_DRIVER, pTCPIPSystem->m_object_ref, O_DO_WRITE_READ, F_RECV, 0, wcount, "READ_RESPONSE_FIRST/NEXT recv_header() Error"); TCPIPCloseSession (ipTCPIPSystem); return false; } } break; case CLOSE_TCPIP_SESSION: memcpy(&close_hdr,hdr,sizeof(HEADER)); if(pTCPIPSystem->swap() == SWAP_YES) HEADER_swap((HEADER*)&close_hdr); data_length = sizeof(HEADER); send_close_session(pTCPIPSystem->m_hSocket, (char*)&close_hdr, data_length, 0, 30, pTCPIPSystem); return true; } bufcount = 0; switch(hdr->hdr_type) { case WRITE_REQUEST_FIRST: bufcount -= sizeof(HEADER); case WRITE_REQUEST_NEXT: bufcount += wcount; if (bufcount < 0) { error = DRVR_ERR_INCORRECT_LENGTH; (fpTCPIPSET_ERROR)((long)pTCPIPSystem, UNX, TCPIP, pTCPIPSystem->odbcAPI, E_DRIVER, pTCPIPSystem->m_object_ref, O_DO_WRITE_READ, F_SEND, error, bufcount, "Driver Incorrect Length Error"); TCPIPCloseSession (ipTCPIPSystem); return false; } break; case READ_RESPONSE_FIRST: bufcount = wcount - sizeof(HEADER); if (bufcount < 0) { error = DRVR_ERR_INCORRECT_LENGTH; (fpTCPIPSET_ERROR)((long)pTCPIPSystem, UNX, TCPIP, pTCPIPSystem->odbcAPI, E_DRIVER, pTCPIPSystem->m_object_ref, O_DO_WRITE_READ, F_RECV, error, bufcount, "Driver Incorrect Length Error"); TCPIPCloseSession (ipTCPIPSystem); return false; } buffer = pTCPIPSystem->m_IObuffer + sizeof(HEADER); hdr = (HEADER*)pTCPIPSystem->m_IObuffer; if (hdr->signature != SIGNATURE) { error = DRVR_ERR_WRONGSIGNATURE; (fpTCPIPSET_ERROR)((long)pTCPIPSystem, UNX, TCPIP, pTCPIPSystem->odbcAPI, E_DRIVER, pTCPIPSystem->m_object_ref, O_DO_WRITE_READ, F_RECV, error, (int)0, "Wrong Socket Signature"); TCPIPCloseSession (ipTCPIPSystem); return false; } if (hdr->error != 0 ) { error = DRVR_ERR_ERROR_FROM_SERVER; (fpTCPIPSET_ERROR)((long)pTCPIPSystem, UNX, TCPIP, pTCPIPSystem->odbcAPI, E_DRIVER, pTCPIPSystem->m_object_ref, O_DO_WRITE_READ, F_RECV, hdr->error, hdr->error_detail, "Error From Server"); TCPIPCloseSession (ipTCPIPSystem); return false; } break; case READ_RESPONSE_NEXT: bufcount = wcount; memcpy(buffer, pTCPIPSystem->m_IObuffer, bufcount); break; case SRVR_TRANSPORT_ERROR: error_hdr = (HEADER*)(pTCPIPSystem->m_IObuffer); error = error_hdr->error; error_detail = error_hdr->error_detail; (fpTCPIPSET_ERROR)((long)pTCPIPSystem, UNX, TCPIP, pTCPIPSystem->odbcAPI, E_DRIVER, pTCPIPSystem->m_object_ref, O_DO_WRITE_READ, F_SRVR_TRANSPORT_ERROR, error, error_detail, "Server Transport Error"); TCPIPCloseSession (ipTCPIPSystem); break; default: error = 1000; (fpTCPIPSET_ERROR)((long)pTCPIPSystem, PC, TCPIP, pTCPIPSystem->odbcAPI, E_DRIVER, pTCPIPSystem->m_object_ref, O_DO_WRITE_READ, F_HDR_TYPE, error, (int)0, "Unknown Header Type" ); TCPIPCloseSession (ipTCPIPSystem); break; } return error == 0; }