diff --git a/examples/server/main.c b/examples/server/main.c index ec8f0a8..c93de44 100644 --- a/examples/server/main.c +++ b/examples/server/main.c @@ -26,7 +26,7 @@ int main(int argc, char *argv[]) { 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()); goto end; } diff --git a/include/ctx.h b/include/ctx.h index 849548f..4661082 100644 --- a/include/ctx.h +++ b/include/ctx.h @@ -3,6 +3,7 @@ #include "mptp.h" #include "pool.h" +#include #include 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 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 -bool lm_ctx_serve( - lm_ctx_t *ctx, char *addr, uint8_t threads, lm_ctx_serve_callback_t callback, void *data); // serves all the pools - // +bool lm_ctx_serve(lm_ctx_t *ctx, char *addr, uint8_t threads, __sighandler_t handler, lm_ctx_serve_callback_t callback, + void *data); // serves all the pools + /* ############################## ## temp directory fucntions ## ############################## */ diff --git a/include/mptp.h b/include/mptp.h index edd78a0..9c7c074 100644 --- a/include/mptp.h +++ b/include/mptp.h @@ -107,7 +107,7 @@ typedef struct lm_mptp_header { uint16_t flags; uint8_t host_size; uint8_t data_size; -} lm_mptp_header_t; +} __attribute__((packed)) lm_mptp_header_t; typedef struct lm_mptp { 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); -#define MPTP_FLAGS_VERSION(m) (((m)->header.flags & 0xFF00) >> 8) -#define MPTP_FLAGS_TYPE(m) (((m)->header.flags & 0x0080) >> 1) -#define MPTP_FLAGS_CODE(m) (((m)->header.flags & 0x0070) >> 4) -#define MPTP_FLAGS_LAST(m) (((m)->header.flags & 0x0008) >> 3) +#define MPTP_FLAGS_VERSION(m) (((m)->header.flags >> 8) & 0xFF) +#define MPTP_FLAGS_TYPE(m) (((m)->header.flags >> 7) & 0x01) +#define MPTP_FLAGS_CODE(m) (((m)->header.flags >> 3) & 0x0F) +#define MPTP_FLAGS_LAST(m) (((m)->header.flags >> 2) & 0x01) #define MPTP_IS_REQUEST(m) (MPTP_FLAGS_TYPE(m) == 0) #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_data(lm_mptp_t *packet, char *data); 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); bool lm_mptp_verify(lm_mptp_t *packet); void lm_mptp_close(int sock); diff --git a/include/util.h b/include/util.h index 5d5b15b..8720023 100644 --- a/include/util.h +++ b/include/util.h @@ -8,6 +8,9 @@ #include #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 eq(char *s1, char *s2); @@ -33,6 +36,8 @@ bool package_version_valid(char *name); bool package_name_valid(char *name); 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 copy_blocks(struct archive *w, struct archive *r); bool extract_archive(char *dst, char *src); diff --git a/locale/tr/LC_MESSAGES/libmp.po b/locale/tr/LC_MESSAGES/libmp.po index 59026b9..1c60de6 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-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" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -102,7 +102,8 @@ msgid "failed to connect to the MPTP host" msgstr "" #: src/error.c:39 -msgid "failed receive MPTP data from host" +#, c-format +msgid "failed receive MPTP data from host: %s" msgstr "" #: src/error.c:40 @@ -128,7 +129,8 @@ msgid "MPTP connection timed out" msgstr "" #: src/error.c:45 -msgid "failed to bind MPTP socket" +#, c-format +msgid "failed to bind MPTP socket: %s" msgstr "" #: src/error.c:46 diff --git a/src/ctx/serve.c b/src/ctx/serve.c index dfa2b03..07e776c 100644 --- a/src/ctx/serve.c +++ b/src/ctx/serve.c @@ -6,11 +6,15 @@ #include #include +#include #include #include #include #include +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; } diff --git a/src/error.c b/src/error.c index 1b86248..9cea151 100644 --- a/src/error.c +++ b/src/error.c @@ -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") }, diff --git a/src/mptp/client.c b/src/mptp/client.c index 83f094b..f2d42c8 100644 --- a/src/mptp/client.c +++ b/src/mptp/client.c @@ -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; } diff --git a/src/mptp/mptp.c b/src/mptp/mptp.c index ab44ed6..9149c48 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 @@ -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); diff --git a/src/mptp/server.c b/src/mptp/server.c index 0000af6..dc08f74 100644 --- a/src/mptp/server.c +++ b/src/mptp/server.c @@ -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; diff --git a/src/mptp/util.c b/src/mptp/util.c index 39e30fe..22297b1 100644 --- a/src/mptp/util.c +++ b/src/mptp/util.c @@ -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; } diff --git a/src/pool/info.c b/src/pool/info.c index d42d3b8..8954a9d 100644 --- a/src/pool/info.c +++ b/src/pool/info.c @@ -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: diff --git a/src/pool/list.c b/src/pool/list.c index a99a365..b53c18e 100644 --- a/src/pool/list.c +++ b/src/pool/list.c @@ -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: diff --git a/src/util.c b/src/util.c index e7da499..b3e18bd 100644 --- a/src/util.c +++ b/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;