From 7e7cd68a1e4a72213ad7e894d4f128333a69ead4 Mon Sep 17 00:00:00 2001 From: ngn Date: Thu, 8 Aug 2024 15:38:26 +0300 Subject: [PATCH] fix: packet receive issues --- include/mptp.h | 1 + locale/tr/LC_MESSAGES/libmp.po | 2 +- src/mptp/client.c | 28 +++--------------------- src/mptp/mptp.c | 40 ++++++++++++++++++++++++++++++++++ src/mptp/server.c | 21 +++--------------- 5 files changed, 48 insertions(+), 44 deletions(-) diff --git a/include/mptp.h b/include/mptp.h index 9c7c074..950deba 100644 --- a/include/mptp.h +++ b/include/mptp.h @@ -135,6 +135,7 @@ bool lm_mptp_socket_opts(int sock); void lm_mptp_copy(lm_mptp_t *dst, lm_mptp_t *src); bool lm_mptp_verify(lm_mptp_t *packet); void lm_mptp_close(int sock); +bool lm_mptp_recv(int sock, lm_mptp_t *packet); int lm_mptp_client_connect(char *addr, uint16_t port); bool lm_mptp_client_verify(lm_mptp_t *packet); diff --git a/locale/tr/LC_MESSAGES/libmp.po b/locale/tr/LC_MESSAGES/libmp.po index 1c60de6..d5a3a4b 100644 --- a/locale/tr/LC_MESSAGES/libmp.po +++ b/locale/tr/LC_MESSAGES/libmp.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-08-08 02:21+0300\n" +"POT-Creation-Date: 2024-08-08 15:34+0300\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" diff --git a/src/mptp/client.c b/src/mptp/client.c index f2d42c8..5f44ccb 100644 --- a/src/mptp/client.c +++ b/src/mptp/client.c @@ -91,34 +91,12 @@ bool lm_mptp_client_recv(int sock, lm_mptp_t *packet) { return false; } - char buffer[sizeof(packet->header) + MPTP_HOST_MAX + MPTP_DATA_MAX]; - ssize_t total = sizeof(buffer), used = 0; - - bzero(buffer, sizeof(buffer)); bzero(packet, sizeof(lm_mptp_t)); - if (recv(sock, buffer, sizeof(buffer), 0) <= 0) { - if (ETIMEDOUT == errno || EAGAIN == errno) { - lm_error_set(LM_ERR_MPTPTimeout); - return false; - } - lm_error_set(LM_ERR_MPTPRecvFail, strerror(errno)); - return false; + if(!lm_mptp_recv(sock, packet)){ + pdebug(__func__, "failed to receive the packet: %s", lm_strerror()); + return false; // error set by function } - copy_from_buffer(&packet->header, buffer, sizeof(packet->header), &total, &used); - packet->header.flags = ntohs(packet->header.flags); - // packet->header.host_size = ntohs(packet->header.host_size); - // packet->header.data_size = ntohs(packet->header.data_size); - - if (packet->header.host_size <= MPTP_HOST_MAX) - copy_from_buffer(&packet->host, buffer, packet->header.host_size, &total, &used); - - if (packet->header.data_size <= MPTP_DATA_MAX) - copy_from_buffer(&packet->data, buffer, packet->header.data_size, &total, &used); - - pdebug(__func__, "printing the packet dump"); - pdebug_binary(buffer, sizeof(buffer)); - return true; } diff --git a/src/mptp/mptp.c b/src/mptp/mptp.c index 9149c48..9509cfe 100644 --- a/src/mptp/mptp.c +++ b/src/mptp/mptp.c @@ -2,6 +2,7 @@ #include "../../include/mptp.h" #include "../../include/url.h" +#include #include #include #include @@ -209,3 +210,42 @@ void lm_mptp_copy(lm_mptp_t *dst, lm_mptp_t *src) { memcpy(&dst->host, &src->data, sizeof(src->host)); memcpy(&dst->data, &src->data, sizeof(src->data)); } + +bool lm_mptp_recv(int sock, lm_mptp_t *packet) { + if (recv(sock, &packet->header, sizeof(packet->header), 0) <= 0) { + if (ETIMEDOUT == errno || EAGAIN == errno) { + lm_error_set(LM_ERR_MPTPTimeout); + return false; + } + lm_error_set(LM_ERR_MPTPRecvFail, strerror(errno)); + return false; + } + + packet->header.flags = ntohs(packet->header.flags); + // packet->header.host_size = ntohs(packet->header.host_size); + // packet->header.data_size = ntohs(packet->header.data_size); + + if (packet->header.host_size <= MPTP_HOST_MAX && packet->header.host_size != 0){ + if(recv(sock, packet->host, packet->header.host_size, 0) <= 0){ + if (ETIMEDOUT == errno || EAGAIN == errno) { + lm_error_set(LM_ERR_MPTPTimeout); + return false; + } + lm_error_set(LM_ERR_MPTPRecvFail, strerror(errno)); + return false; + } + } + + if (packet->header.data_size <= MPTP_DATA_MAX && packet->header.data_size != 0){ + if(recv(sock, packet->data, packet->header.data_size, 0) <= 0){ + if (ETIMEDOUT == errno || EAGAIN == errno) { + lm_error_set(LM_ERR_MPTPTimeout); + return false; + } + lm_error_set(LM_ERR_MPTPRecvFail, strerror(errno)); + return false; + } + } + + return true; +} diff --git a/src/mptp/server.c b/src/mptp/server.c index dc08f74..370a460 100644 --- a/src/mptp/server.c +++ b/src/mptp/server.c @@ -80,28 +80,13 @@ bool lm_mptp_server_recv(int sock, lm_mptp_t *packet) { return false; } - char buffer[sizeof(packet->header) + MPTP_HOST_MAX + MPTP_DATA_MAX]; - ssize_t total = sizeof(buffer), used = 0; - - bzero(buffer, sizeof(buffer)); bzero(packet, sizeof(lm_mptp_t)); - if (recv(sock, buffer, sizeof(buffer), 0) <= 0) { - lm_error_set(LM_ERR_MPTPRecvFail); - return false; + if(!lm_mptp_recv(sock, packet)){ + pdebug(__func__, "failed to receive the packet: %s", lm_strerror()); + return false; // error set by function } - copy_from_buffer(&packet->header, buffer, sizeof(packet->header), &total, &used); - packet->header.flags = ntohs(packet->header.flags); - // packet->header.host_size = ntohs(packet->header.host_size); - // packet->header.data_size = ntohs(packet->header.data_size); - - if (packet->header.host_size <= MPTP_HOST_MAX) - copy_from_buffer(&packet->host, buffer, packet->header.host_size, &total, &used); - - if (packet->header.data_size <= MPTP_DATA_MAX) - copy_from_buffer(&packet->data, buffer, packet->header.data_size, &total, &used); - return true; }