fix: TCP network issues
This commit is contained in:
@ -6,11 +6,15 @@
|
||||
|
||||
#include <errno.h>
|
||||
#include <libgen.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
int __serve_sock = 0;
|
||||
lm_thpool_t __serve_tp;
|
||||
|
||||
struct lm_ctx_serve_thread_arg {
|
||||
lm_ctx_serve_callback_t callback;
|
||||
int sock;
|
||||
@ -80,20 +84,20 @@ void __lm_ctx_serve_thread(void *_arg) {
|
||||
switch (MPTP_FLAGS_CODE(&packet)) {
|
||||
// response PING with PONG
|
||||
case MPTP_C2S_PING:
|
||||
pdebug(__func__, "(%x) PING %s: returning PONG", arg->addr, pool->name);
|
||||
pdebug(__func__, "PING %s: returning PONG", pool->name);
|
||||
lm_mptp_init(&packet, false, MPTP_S2C_PONG, true);
|
||||
lm_mptp_server_send(arg->sock, &packet);
|
||||
break;
|
||||
|
||||
// when INFO file is requested, send the file
|
||||
case MPTP_C2S_INFO:
|
||||
pdebug(__func__, "(%x) INFO %s: attempting to send info", arg->addr, pool->name);
|
||||
pdebug(__func__, "INFO %s: attempting to send info (%s)", pool->name, pool->info_file);
|
||||
lm_mptp_sendfile(arg->sock, pool->info_file, NULL, NULL);
|
||||
break;
|
||||
|
||||
// when LIST file is requested, send the file
|
||||
case MPTP_C2S_LIST:
|
||||
pdebug(__func__, "(%x) LIST %s: attempting to send list", arg->addr, pool->name);
|
||||
pdebug(__func__, "LIST %s: attempting to send list (%s)", pool->name, pool->list_file);
|
||||
lm_mptp_sendfile(arg->sock, pool->list_file, NULL, NULL);
|
||||
break;
|
||||
|
||||
@ -108,12 +112,12 @@ void __lm_ctx_serve_thread(void *_arg) {
|
||||
break;
|
||||
}
|
||||
|
||||
pdebug(__func__, "(%x) PULL %s: attempting to send package archive and signature", arg->addr, pool->name);
|
||||
pdebug(__func__, "PULL %s: attempting to send package archive and signature", pool->name);
|
||||
char path[packet.header.data_size + 1], *package = path;
|
||||
|
||||
if(!lm_mptp_get_data(&packet, path)){
|
||||
// we should never be able to get here, if we do theres definetly a bug
|
||||
pdebug(__func__, "(%x) PULL %s: skipping, failed to get path: %s", arg->addr, pool->name, lm_strerror());
|
||||
pdebug(__func__, "PULL %s: skipping, failed to get path (%s)", pool->name, lm_strerror());
|
||||
break;
|
||||
}
|
||||
|
||||
@ -149,12 +153,19 @@ void __lm_ctx_serve_thread(void *_arg) {
|
||||
}
|
||||
|
||||
end:
|
||||
lm_mptp_close(arg->sock);
|
||||
if(success && NULL != arg->callback)
|
||||
arg->callback(pool, &packet, &arg->addr, arg->data);
|
||||
free(arg);
|
||||
}
|
||||
|
||||
bool lm_ctx_serve(lm_ctx_t *ctx, char *addr, uint8_t threads, lm_ctx_serve_callback_t callback, void *data){
|
||||
void __lm_ctx_serve_signal(int sig){
|
||||
lm_thpool_stop(&__serve_tp);
|
||||
lm_mptp_server_close(__serve_sock);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
bool lm_ctx_serve(lm_ctx_t *ctx, char *addr, uint8_t threads, __sighandler_t handler, lm_ctx_serve_callback_t callback, void *data){
|
||||
if (NULL == addr || threads < 0) {
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
return false;
|
||||
@ -163,10 +174,9 @@ bool lm_ctx_serve(lm_ctx_t *ctx, char *addr, uint8_t threads, lm_ctx_serve_callb
|
||||
char host[strlen(addr)+1];
|
||||
struct sockaddr saddr;
|
||||
uint16_t port;
|
||||
int sock = -1, c = -1;
|
||||
lm_thpool_t tp;
|
||||
int c = -1;
|
||||
|
||||
lm_thpool_init(&tp, threads);
|
||||
lm_thpool_init(&__serve_tp, threads);
|
||||
|
||||
if (!parse_host(addr, host, &port))
|
||||
return false;
|
||||
@ -176,23 +186,27 @@ bool lm_ctx_serve(lm_ctx_t *ctx, char *addr, uint8_t threads, lm_ctx_serve_callb
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((sock = lm_mptp_server_listen(host, port)) < 0)
|
||||
if ((__serve_sock = lm_mptp_server_listen(host, port)) < 0)
|
||||
return false;
|
||||
|
||||
while ((c = lm_mptp_server_accept(sock, &saddr)) != -1) {
|
||||
if(NULL == handler)
|
||||
handler = __lm_ctx_serve_signal;
|
||||
signal(SIGINT, handler);
|
||||
|
||||
while ((c = lm_mptp_server_accept(__serve_sock, &saddr)) != -1) {
|
||||
struct lm_ctx_serve_thread_arg *arg = malloc(sizeof(struct lm_ctx_serve_thread_arg));
|
||||
memcpy(&arg->addr, &saddr, sizeof(struct sockaddr));
|
||||
|
||||
arg->ctx = ctx;
|
||||
arg->callback = callback;
|
||||
arg->data = data;
|
||||
arg->ctx = ctx;
|
||||
arg->sock = c;
|
||||
|
||||
pdebug(__func__, "adding new connection to the pool");
|
||||
lm_thpool_add(&tp, __lm_ctx_serve_thread, arg);
|
||||
lm_thpool_add(&__serve_tp, __lm_ctx_serve_thread, arg);
|
||||
}
|
||||
|
||||
lm_thpool_stop(&tp);
|
||||
lm_thpool_stop(&__serve_tp);
|
||||
// error set by mptp_server_accept
|
||||
return false;
|
||||
}
|
||||
|
@ -36,13 +36,13 @@ void lm_error_set(lm_error_t code, ...) {
|
||||
{.code = LM_ERR_MPTPHostFail, .desc = _("failed to resolve hostname for MPTP connection") },
|
||||
{.code = LM_ERR_MPTPSocketFail, .desc = _("failed to create a MPTP socket") },
|
||||
{.code = LM_ERR_MPTPConnectFail, .desc = _("failed to connect to the MPTP host") },
|
||||
{.code = LM_ERR_MPTPRecvFail, .desc = _("failed receive MPTP data from host") },
|
||||
{.code = LM_ERR_MPTPRecvFail, .desc = _("failed receive MPTP data from host: %s") },
|
||||
{.code = LM_ERR_MPTPSendFail, .desc = _("failed send MPTP data to host") },
|
||||
{.code = LM_ERR_MPTPBadData, .desc = _("MPTP data size is invalid") },
|
||||
{.code = LM_ERR_MPTPBadHost, .desc = _("MPTP host size is invalid") },
|
||||
{.code = LM_ERR_MPTPSetsockopt, .desc = _("failed to set MPTP socket options") },
|
||||
{.code = LM_ERR_MPTPTimeout, .desc = _("MPTP connection timed out") },
|
||||
{.code = LM_ERR_MPTPBindFail, .desc = _("failed to bind MPTP socket") },
|
||||
{.code = LM_ERR_MPTPBindFail, .desc = _("failed to bind MPTP socket: %s") },
|
||||
{.code = LM_ERR_ArgNULL, .desc = _("required argument is a NULL pointer or 0") },
|
||||
{.code = LM_ERR_MPTPNotRequest, .desc = _("not a MPTP request") },
|
||||
{.code = LM_ERR_MPTPNotResponse, .desc = _("not a MPTP response") },
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -97,11 +97,15 @@ bool lm_pool_info_download(lm_pool_t *pool, lm_mptp_transfer_callback_t callback
|
||||
lm_mptp_set_host(&packet, pool->url.host);
|
||||
lm_mptp_set_data(&packet, pool->url.path, strlen(pool->url.path));
|
||||
|
||||
if(!lm_mptp_client_send(sock, &packet))
|
||||
if(!lm_mptp_client_send(sock, &packet)){
|
||||
pdebug(__func__, "info file request failed for %s: %s", pool->name, lm_strerror());
|
||||
goto end;
|
||||
}
|
||||
|
||||
if(!lm_mptp_recvfile(sock, pool->info_file, callback, data))
|
||||
if(!lm_mptp_recvfile(sock, pool->info_file, callback, data)){
|
||||
pdebug(__func__, "recvfile failed for %s: %s", pool->name, lm_strerror());
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = true;
|
||||
end:
|
||||
|
@ -123,11 +123,15 @@ bool lm_pool_list_download(lm_pool_t *pool, char *dir, lm_mptp_transfer_callback
|
||||
lm_mptp_set_host(&packet, pool->url.host);
|
||||
lm_mptp_set_data(&packet, pool->url.path, strlen(pool->url.path));
|
||||
|
||||
if(!lm_mptp_client_send(sock, &packet))
|
||||
if(!lm_mptp_client_send(sock, &packet)){
|
||||
pdebug(__func__, "list file request failed for %s: %s", pool->name, lm_strerror());
|
||||
goto end;
|
||||
}
|
||||
|
||||
if(!lm_mptp_recvfile(sock, pool->list_file, callback, data))
|
||||
if(!lm_mptp_recvfile(sock, pool->list_file, callback, data)){
|
||||
pdebug(__func__, "recvfile failed for %s: %s", pool->name, lm_strerror());
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = true;
|
||||
end:
|
||||
|
13
src/util.c
13
src/util.c
@ -29,6 +29,19 @@ void pdebug(const char *func, const char *fmt, ...) {
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void pdebug_binary(char *data, size_t len) {
|
||||
if (LM_DEBUG != 1)
|
||||
return;
|
||||
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
if (i != 0 && i % 4 == 0)
|
||||
printf("\n");
|
||||
printf("%c%c%c%c%c%c%c%c ", BINARY(data[i]));
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
bool eq(char *s1, char *s2) {
|
||||
if (NULL == s1 || NULL == s2)
|
||||
return false;
|
||||
|
Reference in New Issue
Block a user