| 605 | } |
| 606 | |
| 607 | EipStatus HandleDataOnTcpSocket(int socket) { |
| 608 | OPENER_TRACE_INFO("Entering HandleDataOnTcpSocket for socket: %d\n", socket); |
| 609 | int remaining_bytes = 0; |
| 610 | long data_sent = PC_OPENER_ETHERNET_BUFFER_SIZE; |
| 611 | |
| 612 | /* We will handle just one EIP packet here the rest is done by the select |
| 613 | * method which will inform us if more data is available in the socket |
| 614 | because of the current implementation of the main loop this may not be |
| 615 | the fastest way and a loop here with a non blocking socket would better |
| 616 | fit*/ |
| 617 | |
| 618 | /*Check how many data is here -- read the first four bytes from the connection */ |
| 619 | CipOctet incoming_message[PC_OPENER_ETHERNET_BUFFER_SIZE] = {0}; |
| 620 | |
| 621 | long number_of_read_bytes = recv(socket, incoming_message, 4, |
| 622 | 0); /*TODO we may have to set the socket to a non blocking socket */ |
| 623 | |
| 624 | SocketTimer *socket_timer = SocketTimerArrayGetSocketTimer( |
| 625 | g_timestamps, |
| 626 | OPENER_NUMBER_OF_SUPPORTED_SESSIONS, |
| 627 | socket); |
| 628 | if (number_of_read_bytes == 0) { |
| 629 | int error_code = GetSocketErrorNumber(); |
| 630 | char *error_message = GetErrorMessage(error_code); |
| 631 | OPENER_TRACE_ERR( |
| 632 | "networkhandler: socket: %d - connection closed by client: %d - %s\n", |
| 633 | socket, |
| 634 | error_code, |
| 635 | error_message); |
| 636 | FreeErrorMessage(error_message); |
| 637 | RemoveSocketTimerFromList(socket); |
| 638 | RemoveSession(socket); |
| 639 | return kEipStatusError; |
| 640 | } |
| 641 | if (number_of_read_bytes < 0) { |
| 642 | int error_code = GetSocketErrorNumber(); |
| 643 | if (OPENER_SOCKET_WOULD_BLOCK == error_code) { |
| 644 | return kEipStatusOk; |
| 645 | } |
| 646 | char *error_message = GetErrorMessage(error_code); |
| 647 | OPENER_TRACE_ERR("networkhandler: error on recv: %d - %s\n", |
| 648 | error_code, |
| 649 | error_message); |
| 650 | FreeErrorMessage(error_message); |
| 651 | return kEipStatusError; |
| 652 | } |
| 653 | |
| 654 | const EipUint8 *read_buffer = &incoming_message[2]; /* at this place EIP stores the data length */ |
| 655 | size_t data_size = GetIntFromMessage(&read_buffer) |
| 656 | + ENCAPSULATION_HEADER_LENGTH - 4; /* -4 is for the 4 bytes we have already read*/ |
| 657 | /* (NOTE this advances the buffer pointer) */ |
| 658 | if ( (PC_OPENER_ETHERNET_BUFFER_SIZE - 4) < data_size ) { /*TODO can this be handled in a better way?*/ |
| 659 | OPENER_TRACE_ERR( |
| 660 | "too large packet received will be ignored, will drop the data\n"); |
| 661 | /* Currently we will drop the whole packet */ |
| 662 | |
| 663 | do { |
| 664 | OPENER_TRACE_INFO( |
no test coverage detected