fix: TCP network issues

This commit is contained in:
ngn
2024-08-08 02:30:51 +03:00
parent d7dd578fc4
commit 65a9d7610b
14 changed files with 139 additions and 49 deletions

View File

@ -15,17 +15,8 @@ int lm_mptp_client_connect(char *addr, uint16_t port) {
if ((sock = lm_mptp_socket(addr, port, &saddr)) < 0)
return -1;
struct timeval timeout;
bzero(&timeout, sizeof(timeout));
timeout.tv_sec = MPTP_TIMEOUT;
timeout.tv_usec = 0;
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0) {
lm_error_set(LM_ERR_MPTPSetsockopt);
lm_mptp_close(sock);
if(!lm_mptp_socket_opts(sock))
return -1;
}
if (connect(sock, &saddr, sizeof(saddr)) < 0) {
lm_mptp_close(sock);
@ -83,6 +74,9 @@ bool lm_mptp_client_send(int sock, lm_mptp_t *packet) {
copy_to_buffer(buffer, packet->host, packet->header.host_size, &total, &used);
copy_to_buffer(buffer, packet->data, packet->header.data_size, &total, &used);
pdebug(__func__, "printing the packet dump");
pdebug_binary(buffer, sizeof(buffer));
if (send(sock, buffer, sizeof(buffer), 0) < 0) {
lm_error_set(LM_ERR_MPTPSendFail);
return false;
@ -103,12 +97,12 @@ bool lm_mptp_client_recv(int sock, lm_mptp_t *packet) {
bzero(buffer, sizeof(buffer));
bzero(packet, sizeof(lm_mptp_t));
if (recv(sock, buffer, sizeof(buffer), 0) < 0) {
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);
lm_error_set(LM_ERR_MPTPRecvFail, strerror(errno));
return false;
}
@ -123,5 +117,8 @@ bool lm_mptp_client_recv(int sock, lm_mptp_t *packet) {
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 <netinet/tcp.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
@ -26,12 +27,12 @@ bool lm_mptp_init(lm_mptp_t *packet, bool is_request, uint8_t code, bool is_last
else
packet->header.flags |= (MPTP_RESPONSE << 7);
packet->header.flags |= (code << 4);
packet->header.flags |= (code << 3);
if (is_last || is_request)
packet->header.flags |= (1 << 3);
packet->header.flags |= (1 << 2);
else
packet->header.flags |= (0 << 3);
packet->header.flags |= (0 << 2);
return true;
}
@ -60,6 +61,41 @@ bool lm_mptp_verify(lm_mptp_t *packet) {
return true;
}
bool lm_mptp_socket_opts(int sock){
struct timeval timeout;
int flags = 1;
bzero(&timeout, sizeof(timeout));
timeout.tv_sec = MPTP_TIMEOUT;
timeout.tv_usec = 0;
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0) {
lm_error_set(LM_ERR_MPTPSetsockopt);
lm_mptp_close(sock);
return false;
}
if (setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)) < 0) {
lm_error_set(LM_ERR_MPTPSetsockopt);
lm_mptp_close(sock);
return false;
}
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &flags, sizeof(int)) < 0) {
lm_error_set(LM_ERR_MPTPSetsockopt);
lm_mptp_close(sock);
return false;
}
if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &flags, sizeof(int)) < 0) {
lm_error_set(LM_ERR_MPTPSetsockopt);
lm_mptp_close(sock);
return false;
}
return true;
}
int lm_mptp_socket(char *addr, uint16_t port, struct sockaddr *saddr) {
if (NULL == addr || NULL == saddr) {
lm_error_set(LM_ERR_ArgNULL);

View File

@ -18,7 +18,7 @@ int lm_mptp_server_listen(char *addr, uint16_t port) {
if (bind(sock, &saddr, sizeof(struct sockaddr)) < 0) {
lm_mptp_close(sock);
lm_error_set(LM_ERR_MPTPBindFail);
lm_error_set(LM_ERR_MPTPBindFail, strerror(errno));
return -1;
}
@ -40,6 +40,11 @@ int lm_mptp_server_accept(int sock, struct sockaddr *addr){
s = -1;
}
if(!lm_mptp_socket_opts(s)){
close(s);
s = -1;
}
return s;
}
@ -130,6 +135,9 @@ bool lm_mptp_server_send(int sock, lm_mptp_t *packet) {
copy_to_buffer(buffer, packet->host, packet->header.host_size, &total, &used);
copy_to_buffer(buffer, packet->data, packet->header.data_size, &total, &used);
pdebug(__func__, "printing the packet dump");
pdebug_binary(buffer, sizeof(buffer));
if (send(sock, buffer, sizeof(buffer), 0) < 0) {
lm_error_set(LM_ERR_MPTPSendFail);
return false;

View File

@ -85,6 +85,8 @@ bool lm_mptp_sendfile(int sock, char *path, lm_mptp_transfer_callback_t callback
end:
if(NULL != file)
fclose(file);
pdebug(__func__, "completed sending %s, sending last packet", path);
lm_mptp_server_send(sock, &packet);
return ret;
}
@ -157,7 +159,10 @@ bool lm_mptp_recvfile(int sock, char *path, lm_mptp_transfer_callback_t callback
}
if(current != total){
pdebug(__func__, "failed to receive the entire file (left at %lu/%lu): %s (%s)", current, total, path, lm_strerror());
if(MPTP_IS_LAST(&packet))
pdebug(__func__, "failed to receive the entire file (left at %lu/%lu), got the last packet: %s", current, total, path);
else
pdebug(__func__, "failed to receive the entire file (left at %lu/%lu): %s (%s)", current, total, path, lm_strerror());
lm_error_set(LM_ERR_RecvNotCompleted);
goto end;
}