diff --git a/sources/src/functions.c b/sources/src/functions.c index a6ed2911ca9ceb2da39f3211b08f441f75ae66f4..6a01f1d7e9ef4bb1848e04c60c77bf69d9acd957 100644 --- a/sources/src/functions.c +++ b/sources/src/functions.c @@ -207,6 +207,18 @@ void *readUdp(void *arg) socklen_t len = sizeof(r_addr); struct sockaddr recv_addr; + // Set a timeout for the recvfrom function + struct timeval timeout; + timeout.tv_sec = 2; // 5 seconds timeout + timeout.tv_usec = 0; + + if (setsockopt(udp_sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0) + { + perror("setsockopt failed"); + SHOULD_EXIT = 1; + return NULL; + } + for (;;) { @@ -216,12 +228,22 @@ void *readUdp(void *arg) printf("Controller message recieved %s\n", bytes_read); processSmcp1Request(buffer, &recv_addr, bytes_read); } - else if (bytes_read < 0 && PROXY_INIT_COMPLETE) + else if (bytes_read < 0) { - perror("recvfrom failed"); - printf("ERROR : UDP-SOCKET COMMUNICATION FAILED, CLOSING PROGRAM\n"); - SHOULD_EXIT = 1; - break; + if (errno == EWOULDBLOCK || errno == EAGAIN) + { + // printf("recvfrom timeout, no data received.\n"); + } + else + { + perror("recvfrom failed"); + if (PROXY_INIT_COMPLETE) + { + printf("ERROR : UDP-SOCKET COMMUNICATION FAILED, CLOSING PROGRAM\n"); + SHOULD_EXIT = 1; + break; + } + } } if (SHOULD_EXIT && PROXY_INIT_COMPLETE)