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; }