fix: TCP network issues
This commit is contained in:
parent
d7dd578fc4
commit
65a9d7610b
@ -26,7 +26,7 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
lm_ctx_sync(&ctx, false, NULL, NULL);
|
lm_ctx_sync(&ctx, false, NULL, NULL);
|
||||||
|
|
||||||
if (!lm_ctx_serve(&ctx, argv[1], 10, NULL, NULL)) {
|
if (!lm_ctx_serve(&ctx, argv[1], 10, NULL, NULL, NULL)) {
|
||||||
printf("failed to serve the pools: %s (%d)\n", lm_strerror(), lm_error());
|
printf("failed to serve the pools: %s (%d)\n", lm_strerror(), lm_error());
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
#include "mptp.h"
|
#include "mptp.h"
|
||||||
#include "pool.h"
|
#include "pool.h"
|
||||||
|
|
||||||
|
#include <signal.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
typedef struct lm_ctx {
|
typedef struct lm_ctx {
|
||||||
@ -95,9 +96,9 @@ bool lm_ctx_check(
|
|||||||
lm_ctx_t *ctx, lm_entry_t *entry, lm_ctx_check_callback_t callback, void *data); // checks a single package
|
lm_ctx_t *ctx, lm_entry_t *entry, lm_ctx_check_callback_t callback, void *data); // checks a single package
|
||||||
size_t lm_ctx_sync(lm_ctx_t *ctx, bool do_update, lm_ctx_sync_callback_t callback, void *data); // syncs all the pools
|
size_t lm_ctx_sync(lm_ctx_t *ctx, bool do_update, lm_ctx_sync_callback_t callback, void *data); // syncs all the pools
|
||||||
void lm_ctx_ping(lm_ctx_t *ctx, lm_ctx_ping_callback_t callback, void *data); // pings all the pools
|
void lm_ctx_ping(lm_ctx_t *ctx, lm_ctx_ping_callback_t callback, void *data); // pings all the pools
|
||||||
bool lm_ctx_serve(
|
bool lm_ctx_serve(lm_ctx_t *ctx, char *addr, uint8_t threads, __sighandler_t handler, lm_ctx_serve_callback_t callback,
|
||||||
lm_ctx_t *ctx, char *addr, uint8_t threads, lm_ctx_serve_callback_t callback, void *data); // serves all the pools
|
void *data); // serves all the pools
|
||||||
//
|
|
||||||
/* ##############################
|
/* ##############################
|
||||||
## temp directory fucntions ##
|
## temp directory fucntions ##
|
||||||
############################## */
|
############################## */
|
||||||
|
@ -107,7 +107,7 @@ typedef struct lm_mptp_header {
|
|||||||
uint16_t flags;
|
uint16_t flags;
|
||||||
uint8_t host_size;
|
uint8_t host_size;
|
||||||
uint8_t data_size;
|
uint8_t data_size;
|
||||||
} lm_mptp_header_t;
|
} __attribute__((packed)) lm_mptp_header_t;
|
||||||
|
|
||||||
typedef struct lm_mptp {
|
typedef struct lm_mptp {
|
||||||
lm_mptp_header_t header;
|
lm_mptp_header_t header;
|
||||||
@ -117,10 +117,10 @@ typedef struct lm_mptp {
|
|||||||
|
|
||||||
typedef bool (*lm_mptp_transfer_callback_t)(char *path, size_t current, size_t total, void *data);
|
typedef bool (*lm_mptp_transfer_callback_t)(char *path, size_t current, size_t total, void *data);
|
||||||
|
|
||||||
#define MPTP_FLAGS_VERSION(m) (((m)->header.flags & 0xFF00) >> 8)
|
#define MPTP_FLAGS_VERSION(m) (((m)->header.flags >> 8) & 0xFF)
|
||||||
#define MPTP_FLAGS_TYPE(m) (((m)->header.flags & 0x0080) >> 1)
|
#define MPTP_FLAGS_TYPE(m) (((m)->header.flags >> 7) & 0x01)
|
||||||
#define MPTP_FLAGS_CODE(m) (((m)->header.flags & 0x0070) >> 4)
|
#define MPTP_FLAGS_CODE(m) (((m)->header.flags >> 3) & 0x0F)
|
||||||
#define MPTP_FLAGS_LAST(m) (((m)->header.flags & 0x0008) >> 3)
|
#define MPTP_FLAGS_LAST(m) (((m)->header.flags >> 2) & 0x01)
|
||||||
|
|
||||||
#define MPTP_IS_REQUEST(m) (MPTP_FLAGS_TYPE(m) == 0)
|
#define MPTP_IS_REQUEST(m) (MPTP_FLAGS_TYPE(m) == 0)
|
||||||
#define MPTP_IS_LAST(m) (MPTP_FLAGS_LAST(m) == 1)
|
#define MPTP_IS_LAST(m) (MPTP_FLAGS_LAST(m) == 1)
|
||||||
@ -131,6 +131,7 @@ bool lm_mptp_set_host(lm_mptp_t *packet, char *host);
|
|||||||
bool lm_mptp_get_host(lm_mptp_t *packet, char *host);
|
bool lm_mptp_get_host(lm_mptp_t *packet, char *host);
|
||||||
bool lm_mptp_get_data(lm_mptp_t *packet, char *data);
|
bool lm_mptp_get_data(lm_mptp_t *packet, char *data);
|
||||||
int lm_mptp_socket(char *addr, uint16_t port, struct sockaddr *saddr);
|
int lm_mptp_socket(char *addr, uint16_t port, struct sockaddr *saddr);
|
||||||
|
bool lm_mptp_socket_opts(int sock);
|
||||||
void lm_mptp_copy(lm_mptp_t *dst, lm_mptp_t *src);
|
void lm_mptp_copy(lm_mptp_t *dst, lm_mptp_t *src);
|
||||||
bool lm_mptp_verify(lm_mptp_t *packet);
|
bool lm_mptp_verify(lm_mptp_t *packet);
|
||||||
void lm_mptp_close(int sock);
|
void lm_mptp_close(int sock);
|
||||||
|
@ -8,6 +8,9 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#define _(x) gettext(x)
|
#define _(x) gettext(x)
|
||||||
|
#define BINARY(b) \
|
||||||
|
(b & 128 ? '1' : '0'), (b & 64 ? '1' : '0'), (b & 32 ? '1' : '0'), (b & 16 ? '1' : '0'), (b & 8 ? '1' : '0'), \
|
||||||
|
(b & 4 ? '1' : '0'), (b & 2 ? '1' : '0'), (b & 1 ? '1' : '0')
|
||||||
|
|
||||||
bool contains(char *str, char s);
|
bool contains(char *str, char s);
|
||||||
bool eq(char *s1, char *s2);
|
bool eq(char *s1, char *s2);
|
||||||
@ -33,6 +36,8 @@ bool package_version_valid(char *name);
|
|||||||
bool package_name_valid(char *name);
|
bool package_name_valid(char *name);
|
||||||
|
|
||||||
void pdebug(const char *func, const char *fmt, ...);
|
void pdebug(const char *func, const char *fmt, ...);
|
||||||
|
void pdebug_binary(char *data, size_t len);
|
||||||
|
|
||||||
bool parse_host(char *addr, char *host, uint16_t *port);
|
bool parse_host(char *addr, char *host, uint16_t *port);
|
||||||
bool copy_blocks(struct archive *w, struct archive *r);
|
bool copy_blocks(struct archive *w, struct archive *r);
|
||||||
bool extract_archive(char *dst, char *src);
|
bool extract_archive(char *dst, char *src);
|
||||||
|
@ -8,7 +8,7 @@ msgid ""
|
|||||||
msgstr ""
|
msgstr ""
|
||||||
"Project-Id-Version: PACKAGE VERSION\n"
|
"Project-Id-Version: PACKAGE VERSION\n"
|
||||||
"Report-Msgid-Bugs-To: \n"
|
"Report-Msgid-Bugs-To: \n"
|
||||||
"POT-Creation-Date: 2024-08-07 05:32+0300\n"
|
"POT-Creation-Date: 2024-08-08 02:21+0300\n"
|
||||||
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
|
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
|
||||||
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
|
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
|
||||||
"Language-Team: LANGUAGE <LL@li.org>\n"
|
"Language-Team: LANGUAGE <LL@li.org>\n"
|
||||||
@ -102,7 +102,8 @@ msgid "failed to connect to the MPTP host"
|
|||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: src/error.c:39
|
#: src/error.c:39
|
||||||
msgid "failed receive MPTP data from host"
|
#, c-format
|
||||||
|
msgid "failed receive MPTP data from host: %s"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: src/error.c:40
|
#: src/error.c:40
|
||||||
@ -128,7 +129,8 @@ msgid "MPTP connection timed out"
|
|||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: src/error.c:45
|
#: src/error.c:45
|
||||||
msgid "failed to bind MPTP socket"
|
#, c-format
|
||||||
|
msgid "failed to bind MPTP socket: %s"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: src/error.c:46
|
#: src/error.c:46
|
||||||
|
@ -6,11 +6,15 @@
|
|||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <libgen.h>
|
#include <libgen.h>
|
||||||
|
#include <signal.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
int __serve_sock = 0;
|
||||||
|
lm_thpool_t __serve_tp;
|
||||||
|
|
||||||
struct lm_ctx_serve_thread_arg {
|
struct lm_ctx_serve_thread_arg {
|
||||||
lm_ctx_serve_callback_t callback;
|
lm_ctx_serve_callback_t callback;
|
||||||
int sock;
|
int sock;
|
||||||
@ -80,20 +84,20 @@ void __lm_ctx_serve_thread(void *_arg) {
|
|||||||
switch (MPTP_FLAGS_CODE(&packet)) {
|
switch (MPTP_FLAGS_CODE(&packet)) {
|
||||||
// response PING with PONG
|
// response PING with PONG
|
||||||
case MPTP_C2S_PING:
|
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_init(&packet, false, MPTP_S2C_PONG, true);
|
||||||
lm_mptp_server_send(arg->sock, &packet);
|
lm_mptp_server_send(arg->sock, &packet);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// when INFO file is requested, send the file
|
// when INFO file is requested, send the file
|
||||||
case MPTP_C2S_INFO:
|
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);
|
lm_mptp_sendfile(arg->sock, pool->info_file, NULL, NULL);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// when LIST file is requested, send the file
|
// when LIST file is requested, send the file
|
||||||
case MPTP_C2S_LIST:
|
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);
|
lm_mptp_sendfile(arg->sock, pool->list_file, NULL, NULL);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -108,12 +112,12 @@ void __lm_ctx_serve_thread(void *_arg) {
|
|||||||
break;
|
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;
|
char path[packet.header.data_size + 1], *package = path;
|
||||||
|
|
||||||
if(!lm_mptp_get_data(&packet, path)){
|
if(!lm_mptp_get_data(&packet, path)){
|
||||||
// we should never be able to get here, if we do theres definetly a bug
|
// 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;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -149,12 +153,19 @@ void __lm_ctx_serve_thread(void *_arg) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
end:
|
end:
|
||||||
|
lm_mptp_close(arg->sock);
|
||||||
if(success && NULL != arg->callback)
|
if(success && NULL != arg->callback)
|
||||||
arg->callback(pool, &packet, &arg->addr, arg->data);
|
arg->callback(pool, &packet, &arg->addr, arg->data);
|
||||||
free(arg);
|
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) {
|
if (NULL == addr || threads < 0) {
|
||||||
lm_error_set(LM_ERR_ArgNULL);
|
lm_error_set(LM_ERR_ArgNULL);
|
||||||
return false;
|
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];
|
char host[strlen(addr)+1];
|
||||||
struct sockaddr saddr;
|
struct sockaddr saddr;
|
||||||
uint16_t port;
|
uint16_t port;
|
||||||
int sock = -1, c = -1;
|
int c = -1;
|
||||||
lm_thpool_t tp;
|
|
||||||
|
|
||||||
lm_thpool_init(&tp, threads);
|
lm_thpool_init(&__serve_tp, threads);
|
||||||
|
|
||||||
if (!parse_host(addr, host, &port))
|
if (!parse_host(addr, host, &port))
|
||||||
return false;
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((sock = lm_mptp_server_listen(host, port)) < 0)
|
if ((__serve_sock = lm_mptp_server_listen(host, port)) < 0)
|
||||||
return false;
|
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));
|
struct lm_ctx_serve_thread_arg *arg = malloc(sizeof(struct lm_ctx_serve_thread_arg));
|
||||||
memcpy(&arg->addr, &saddr, sizeof(struct sockaddr));
|
memcpy(&arg->addr, &saddr, sizeof(struct sockaddr));
|
||||||
|
|
||||||
arg->ctx = ctx;
|
|
||||||
arg->callback = callback;
|
arg->callback = callback;
|
||||||
arg->data = data;
|
arg->data = data;
|
||||||
|
arg->ctx = ctx;
|
||||||
arg->sock = c;
|
arg->sock = c;
|
||||||
|
|
||||||
pdebug(__func__, "adding new connection to the pool");
|
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
|
// error set by mptp_server_accept
|
||||||
return false;
|
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_MPTPHostFail, .desc = _("failed to resolve hostname for MPTP connection") },
|
||||||
{.code = LM_ERR_MPTPSocketFail, .desc = _("failed to create a MPTP socket") },
|
{.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_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_MPTPSendFail, .desc = _("failed send MPTP data to host") },
|
||||||
{.code = LM_ERR_MPTPBadData, .desc = _("MPTP data size is invalid") },
|
{.code = LM_ERR_MPTPBadData, .desc = _("MPTP data size is invalid") },
|
||||||
{.code = LM_ERR_MPTPBadHost, .desc = _("MPTP host 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_MPTPSetsockopt, .desc = _("failed to set MPTP socket options") },
|
||||||
{.code = LM_ERR_MPTPTimeout, .desc = _("MPTP connection timed out") },
|
{.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_ArgNULL, .desc = _("required argument is a NULL pointer or 0") },
|
||||||
{.code = LM_ERR_MPTPNotRequest, .desc = _("not a MPTP request") },
|
{.code = LM_ERR_MPTPNotRequest, .desc = _("not a MPTP request") },
|
||||||
{.code = LM_ERR_MPTPNotResponse, .desc = _("not a MPTP response") },
|
{.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)
|
if ((sock = lm_mptp_socket(addr, port, &saddr)) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
struct timeval timeout;
|
if(!lm_mptp_socket_opts(sock))
|
||||||
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);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
|
|
||||||
if (connect(sock, &saddr, sizeof(saddr)) < 0) {
|
if (connect(sock, &saddr, sizeof(saddr)) < 0) {
|
||||||
lm_mptp_close(sock);
|
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->host, packet->header.host_size, &total, &used);
|
||||||
copy_to_buffer(buffer, packet->data, packet->header.data_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) {
|
if (send(sock, buffer, sizeof(buffer), 0) < 0) {
|
||||||
lm_error_set(LM_ERR_MPTPSendFail);
|
lm_error_set(LM_ERR_MPTPSendFail);
|
||||||
return false;
|
return false;
|
||||||
@ -103,12 +97,12 @@ bool lm_mptp_client_recv(int sock, lm_mptp_t *packet) {
|
|||||||
bzero(buffer, sizeof(buffer));
|
bzero(buffer, sizeof(buffer));
|
||||||
bzero(packet, sizeof(lm_mptp_t));
|
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) {
|
if (ETIMEDOUT == errno || EAGAIN == errno) {
|
||||||
lm_error_set(LM_ERR_MPTPTimeout);
|
lm_error_set(LM_ERR_MPTPTimeout);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
lm_error_set(LM_ERR_MPTPRecvFail);
|
lm_error_set(LM_ERR_MPTPRecvFail, strerror(errno));
|
||||||
return false;
|
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)
|
if (packet->header.data_size <= MPTP_DATA_MAX)
|
||||||
copy_from_buffer(&packet->data, buffer, packet->header.data_size, &total, &used);
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
#include "../../include/mptp.h"
|
#include "../../include/mptp.h"
|
||||||
#include "../../include/url.h"
|
#include "../../include/url.h"
|
||||||
|
|
||||||
|
#include <netinet/tcp.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.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
|
else
|
||||||
packet->header.flags |= (MPTP_RESPONSE << 7);
|
packet->header.flags |= (MPTP_RESPONSE << 7);
|
||||||
|
|
||||||
packet->header.flags |= (code << 4);
|
packet->header.flags |= (code << 3);
|
||||||
|
|
||||||
if (is_last || is_request)
|
if (is_last || is_request)
|
||||||
packet->header.flags |= (1 << 3);
|
packet->header.flags |= (1 << 2);
|
||||||
else
|
else
|
||||||
packet->header.flags |= (0 << 3);
|
packet->header.flags |= (0 << 2);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -60,6 +61,41 @@ bool lm_mptp_verify(lm_mptp_t *packet) {
|
|||||||
return true;
|
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) {
|
int lm_mptp_socket(char *addr, uint16_t port, struct sockaddr *saddr) {
|
||||||
if (NULL == addr || NULL == saddr) {
|
if (NULL == addr || NULL == saddr) {
|
||||||
lm_error_set(LM_ERR_ArgNULL);
|
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) {
|
if (bind(sock, &saddr, sizeof(struct sockaddr)) < 0) {
|
||||||
lm_mptp_close(sock);
|
lm_mptp_close(sock);
|
||||||
lm_error_set(LM_ERR_MPTPBindFail);
|
lm_error_set(LM_ERR_MPTPBindFail, strerror(errno));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,6 +40,11 @@ int lm_mptp_server_accept(int sock, struct sockaddr *addr){
|
|||||||
s = -1;
|
s = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!lm_mptp_socket_opts(s)){
|
||||||
|
close(s);
|
||||||
|
s = -1;
|
||||||
|
}
|
||||||
|
|
||||||
return s;
|
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->host, packet->header.host_size, &total, &used);
|
||||||
copy_to_buffer(buffer, packet->data, packet->header.data_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) {
|
if (send(sock, buffer, sizeof(buffer), 0) < 0) {
|
||||||
lm_error_set(LM_ERR_MPTPSendFail);
|
lm_error_set(LM_ERR_MPTPSendFail);
|
||||||
return false;
|
return false;
|
||||||
|
@ -85,6 +85,8 @@ bool lm_mptp_sendfile(int sock, char *path, lm_mptp_transfer_callback_t callback
|
|||||||
end:
|
end:
|
||||||
if(NULL != file)
|
if(NULL != file)
|
||||||
fclose(file);
|
fclose(file);
|
||||||
|
|
||||||
|
pdebug(__func__, "completed sending %s, sending last packet", path);
|
||||||
lm_mptp_server_send(sock, &packet);
|
lm_mptp_server_send(sock, &packet);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@ -157,7 +159,10 @@ bool lm_mptp_recvfile(int sock, char *path, lm_mptp_transfer_callback_t callback
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(current != total){
|
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);
|
lm_error_set(LM_ERR_RecvNotCompleted);
|
||||||
goto end;
|
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_host(&packet, pool->url.host);
|
||||||
lm_mptp_set_data(&packet, pool->url.path, strlen(pool->url.path));
|
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;
|
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;
|
goto end;
|
||||||
|
}
|
||||||
|
|
||||||
ret = true;
|
ret = true;
|
||||||
end:
|
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_host(&packet, pool->url.host);
|
||||||
lm_mptp_set_data(&packet, pool->url.path, strlen(pool->url.path));
|
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;
|
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;
|
goto end;
|
||||||
|
}
|
||||||
|
|
||||||
ret = true;
|
ret = true;
|
||||||
end:
|
end:
|
||||||
|
13
src/util.c
13
src/util.c
@ -29,6 +29,19 @@ void pdebug(const char *func, const char *fmt, ...) {
|
|||||||
va_end(args);
|
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) {
|
bool eq(char *s1, char *s2) {
|
||||||
if (NULL == s1 || NULL == s2)
|
if (NULL == s1 || NULL == s2)
|
||||||
return false;
|
return false;
|
||||||
|
Loading…
Reference in New Issue
Block a user