fix: packet receive issues

This commit is contained in:
ngn 2024-08-08 15:38:26 +03:00
parent 65a9d7610b
commit 7e7cd68a1e
5 changed files with 48 additions and 44 deletions

View File

@ -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);

View File

@ -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 <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"

View File

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

View File

@ -2,6 +2,7 @@
#include "../../include/mptp.h"
#include "../../include/url.h"
#include <errno.h>
#include <netinet/tcp.h>
#include <stdbool.h>
#include <string.h>
@ -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;
}

View File

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