@brief create a new UDP socket for the connection manager * * @param communication_direction Consuming or producing port * @param socket_data Data for socket creation * * @return the socket handle if successful, else kEipInvalidSocket */
| 806 | * |
| 807 | * @return the socket handle if successful, else kEipInvalidSocket */ |
| 808 | int CreateUdpSocket(UdpCommuncationDirection communication_direction, |
| 809 | struct sockaddr_in *socket_data, |
| 810 | CipUsint qos_for_socket) { |
| 811 | struct sockaddr_in peer_address; |
| 812 | int new_socket = kEipInvalidSocket; |
| 813 | |
| 814 | socklen_t peer_address_length = sizeof(struct sockaddr_in); |
| 815 | /* create a new UDP socket */ |
| 816 | if(kUdpCommuncationDirectionConsuming == communication_direction) { |
| 817 | new_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 818 | } |
| 819 | |
| 820 | if(kUdpCommuncationDirectionProducing == communication_direction) { |
| 821 | new_socket = socket(AF_INET, SOCK_RAW, IPPROTO_UDP); |
| 822 | } |
| 823 | |
| 824 | if (new_socket == kEipInvalidSocket) { |
| 825 | int error_code = GetSocketErrorNumber(); |
| 826 | char *error_message = GetErrorMessage(error_code); |
| 827 | OPENER_TRACE_ERR("networkhandler: cannot create UDP socket: %d- %s\n", |
| 828 | error_code, |
| 829 | error_message); |
| 830 | FreeErrorMessage(error_message); |
| 831 | return new_socket; |
| 832 | } |
| 833 | |
| 834 | if (SetSocketToNonBlocking(new_socket) < 0) { |
| 835 | OPENER_TRACE_ERR( |
| 836 | "error setting socket to non-blocking on new socket\n"); |
| 837 | CloseSocket(new_socket); |
| 838 | OPENER_ASSERT(false) /* This should never happen! */ |
| 839 | return kEipStatusError; |
| 840 | } |
| 841 | |
| 842 | if (SetQosOnSocket(new_socket, GetPriorityForSocket(qos_for_socket) ) != 0) { /* got error */ |
| 843 | int error_code = GetSocketErrorNumber(); |
| 844 | char *error_message = GetErrorMessage(error_code); |
| 845 | OPENER_TRACE_ERR( |
| 846 | "networkhandler: error on set QoS on socket on new socket: %d - %s\n", |
| 847 | error_code, |
| 848 | error_message); |
| 849 | FreeErrorMessage(error_message); |
| 850 | } |
| 851 | |
| 852 | OPENER_TRACE_INFO("networkhandler: UDP socket %d\n", new_socket); |
| 853 | |
| 854 | /* check if it is sending or receiving */ |
| 855 | if (communication_direction == kUdpCommuncationDirectionConsuming) { |
| 856 | int option_value = 1; |
| 857 | if (setsockopt( new_socket, SOL_SOCKET, SO_REUSEADDR, |
| 858 | (char *) &option_value, |
| 859 | sizeof(option_value) ) == -1) { |
| 860 | OPENER_TRACE_ERR( |
| 861 | "error setting socket option SO_REUSEADDR on consuming udp socket\n"); |
| 862 | CloseSocket(new_socket); |
| 863 | return kEipStatusError; |
| 864 | } |
| 865 |
no test coverage detected