diff --git a/Makefile b/Makefile index 1aef7d3..0466f95 100644 --- a/Makefile +++ b/Makefile @@ -11,6 +11,7 @@ HDRS = $(wildcard include/*.h) CFLAGS = -O3 -march=native -fstack-protector-strong -fcf-protection=full -fstack-clash-protection LIBS = -larchive -linih -lgpgme +DEBUG = 0 VERSION = 24.00 all: dist/libmp.so $(PO_OUTS) @@ -24,7 +25,7 @@ dist/%.o: src/%.c mkdir -p dist/pkg mkdir -p dist/mptp mkdir -p dist/pool - $(CC) -c -Wall -fPIC -o $@ $^ $(LIBS) $(CFLAGS) -DVERSION=\"${VERSION}\" + $(CC) -c -Wall -fPIC -o $@ $^ $(LIBS) $(CFLAGS) -DLM_VERSION=\"${VERSION}\" -DLM_DEBUG=${DEBUG} locale/%.mo: locale/%.po msgfmt $^ -o $@ diff --git a/examples/pool/main.c b/examples/pool/main.c index 8db31b1..5c9c397 100644 --- a/examples/pool/main.c +++ b/examples/pool/main.c @@ -15,7 +15,6 @@ int main(int argc, char *argv[]) { lm_ctx_t ctx; lm_ctx_init(&ctx); - ctx.debug = true; if (!lm_ctx_set_data(&ctx, DATA_DIR)) { printf("failed to set data dir: %s (%d)\n", lm_strerror(), lm_error()); @@ -27,6 +26,8 @@ int main(int argc, char *argv[]) { goto end; } + lm_ctx_pools_test(&ctx); + if (!lm_ctx_pools_load(&ctx, true, NULL, NULL)) { printf("failed to load pools: %s (%d)\n", lm_strerror(), lm_error()); goto end; diff --git a/examples/server/main.c b/examples/server/main.c index c608b1c..5c676b0 100644 --- a/examples/server/main.c +++ b/examples/server/main.c @@ -15,8 +15,6 @@ int main(int argc, char *argv[]) { lm_pool_t *pool; lm_ctx_init(&ctx); - ctx.debug = true; - if ((pool = lm_ctx_pools_add(&ctx, "test", "mptp://127.0.0.1:5858")) == NULL) { printf("failed to add pool: %s (%d)\n", lm_strerror(), lm_error()); goto end; diff --git a/examples/tests/INFO b/examples/tests/INFO new file mode 100644 index 0000000..e2a5791 --- /dev/null +++ b/examples/tests/INFO @@ -0,0 +1,4 @@ +[base] +size = 314073 +maintainer = ngn +pubkey = F9E70878C2FB389AEC2BA34CA3654DF5AD9F641D diff --git a/examples/tests/LIST b/examples/tests/LIST new file mode 100644 index 0000000..a226fc8 Binary files /dev/null and b/examples/tests/LIST differ diff --git a/examples/tests/grep_3.11.mpf b/examples/tests/grep_3.11.mpf new file mode 100644 index 0000000..316c939 Binary files /dev/null and b/examples/tests/grep_3.11.mpf differ diff --git a/examples/tests/grep_3.11.mpf.sig b/examples/tests/grep_3.11.mpf.sig new file mode 100644 index 0000000..e0a75d2 Binary files /dev/null and b/examples/tests/grep_3.11.mpf.sig differ diff --git a/examples/tests/pool/INFO b/examples/tests/pool/INFO deleted file mode 100644 index 8bac9a7..0000000 --- a/examples/tests/pool/INFO +++ /dev/null @@ -1,4 +0,0 @@ -[test] -size = 18221 -author = ngn -pubkey = F9E70878C2FB389AEC2BA34CA3654DF5AD9F641 diff --git a/examples/tests/which_2.21.mpf b/examples/tests/which_2.21.mpf new file mode 100644 index 0000000..a47fa67 Binary files /dev/null and b/examples/tests/which_2.21.mpf differ diff --git a/examples/tests/which_2.21.mpf.sig b/examples/tests/which_2.21.mpf.sig new file mode 100644 index 0000000..118f452 Binary files /dev/null and b/examples/tests/which_2.21.mpf.sig differ diff --git a/include/all.h b/include/all.h index 091440f..c78b90b 100644 --- a/include/all.h +++ b/include/all.h @@ -22,6 +22,8 @@ // clang-format on +#define LM_VERSION "24.00" + #include "ctx.h" #include "error.h" #include "pool.h" diff --git a/include/mptp.h b/include/mptp.h index 535a108..9d1cb3b 100644 --- a/include/mptp.h +++ b/include/mptp.h @@ -140,4 +140,7 @@ bool lm_mptp_client_recv(int sock, lm_mptp_t *packet); int lm_mptp_server_listen(char *addr, uint16_t port); bool lm_mptp_server_verify(lm_mptp_t *packet); bool lm_mptp_server_recv(int sock, lm_mptp_t *packet, struct sockaddr *addr); -bool lm_mptp_server_send(int sock, lm_mptp_t *packet, struct sockaddr *adrr); +bool lm_mptp_server_send(int sock, lm_mptp_t *packet, struct sockaddr *addr); + +bool lm_mptp_sendfile(int sock, struct sockaddr *addr, char *path); +bool lm_mptp_recvfile(int sock, char *path); diff --git a/include/pool.h b/include/pool.h index 5fbc2c6..e4b93be 100644 --- a/include/pool.h +++ b/include/pool.h @@ -21,6 +21,10 @@ void lm_pool_test(lm_pool_t *pool); void lm_pool_free(lm_pool_t *pool); bool lm_pool_add(lm_pool_t *pool, lm_pkg_t *pkg); +void lm_pool_paths_set_info(lm_pool_t *pool, char *info_path); +void lm_pool_paths_set_list(lm_pool_t *pool, char *list_path); +void lm_pool_paths_free(lm_pool_t *pool); + bool lm_pool_info_load(lm_pool_t *pool, char *file); bool lm_pool_info_get(lm_pool_t *pool, char *file); void lm_pool_info_free(lm_pool_t *pool); diff --git a/include/types.h b/include/types.h index 0e6e2a5..693c29a 100644 --- a/include/types.h +++ b/include/types.h @@ -16,12 +16,17 @@ typedef struct lm_pool_info { char *maintainer; char *pubkey; size_t size; - char *file; } lm_pool_info_t; +typedef struct lm_pool_paths { + char *list; + char *info; +} lm_pool_paths_t; + typedef struct lm_pool { struct lm_pool *next; lm_pool_info_t info; + lm_pool_paths_t paths; lm_url_t url; lm_pkg_t *pkg; bool available; @@ -29,10 +34,9 @@ typedef struct lm_pool { } lm_pool_t; typedef struct lm_ctx { - lm_pool_t *pools; // pool list - - char *root; // root path for package installtion - char *temp; // temp path - char *data; // package database path - bool debug; // is debug output enabled? + lm_pool_t *pools; // pool list + char *root; // root path for package installtion + char *temp; // temp path + char *data; // package database path + const char *version; // libmp version (read-only) } lm_ctx_t; diff --git a/include/util.h b/include/util.h index 895b545..1246634 100644 --- a/include/util.h +++ b/include/util.h @@ -1,12 +1,13 @@ #pragma once #include "types.h" #include +#include #include #include #define _(x) gettext(x) -void pdebug(lm_ctx_t *ctx, const char *func, const char *fmt, ...); +void pdebug(const char *func, const char *fmt, ...); bool parse_host(char *addr, char *host, uint16_t *port); bool contains(char *str, char s); bool eq(char *s1, char *s2); @@ -21,3 +22,5 @@ bool is_file(char *path); bool is_dir(char *path); bool can_read(char *path); bool can_write(char *path); +bool mkdir_ifnot(char *path); +void sockaddr_to_str(struct sockaddr *addr, char *str); diff --git a/locale/tr/LC_MESSAGES/libmp.po b/locale/tr/LC_MESSAGES/libmp.po index c0f6cb1..c2baf08 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-06-27 23:02+0300\n" +"POT-Creation-Date: 2024-06-28 20:24+0300\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" diff --git a/src/ctx/ctx.c b/src/ctx/ctx.c index 8cf86ac..fddb67f 100644 --- a/src/ctx/ctx.c +++ b/src/ctx/ctx.c @@ -17,7 +17,7 @@ void lm_ctx_init(lm_ctx_t *ctx) { textdomain("libmp"); bzero(ctx, sizeof(lm_ctx_t)); - ctx->debug = false; + ctx->version = LM_VERSION; } bool lm_ctx_set_temp(lm_ctx_t *ctx, char *dir){ @@ -61,8 +61,18 @@ bool lm_ctx_set_root(lm_ctx_t *ctx, char *dir){ } bool lm_ctx_set_data(lm_ctx_t *ctx, char *dir){ - if(!exists(dir)) - goto mkdir; + if(!mkdir_ifnot(dir)){ + lm_error_set(LM_ERR_CtxDataFailMkdir); + return false; + } + + char poolsdir[strlen(dir)+10]; + snprintf(poolsdir, sizeof(poolsdir), "%s/pools", dir); + + if(!mkdir_ifnot(poolsdir)){ + lm_error_set(LM_ERR_CtxDataFailMkdir); + return false; + } if(!is_dir(dir)){ lm_error_set(LM_ERR_CtxDataNotDir); @@ -74,23 +84,6 @@ bool lm_ctx_set_data(lm_ctx_t *ctx, char *dir){ return false; } - ctx->data = strdup(dir); - return true; - -mkdir: - if(mkdir(dir, 0700) < 0){ - lm_error_set(LM_ERR_CtxDataFailMkdir); - return false; - } - - char poolsdir[strlen(dir)+10]; - snprintf(poolsdir, sizeof(poolsdir), "%s/pools", dir); - - if(mkdir(poolsdir, 0700) < 0){ - lm_error_set(LM_ERR_CtxDataFailMkdir); - return false; - } - ctx->data = strdup(dir); return true; } diff --git a/src/ctx/pools.c b/src/ctx/pools.c index 16235f9..2cb1643 100644 --- a/src/ctx/pools.c +++ b/src/ctx/pools.c @@ -16,8 +16,8 @@ lm_pool_t *lm_ctx_pools_add(lm_ctx_t *ctx, char *name, char *url) { if (NULL == pool) return false; - pdebug(ctx, __func__, "pool name is %s", pool->name); - pdebug(ctx, __func__, "pool URL is %s://%s:%d%s", pool->url.protocol, pool->url.host, pool->url.port, pool->url.path); + pdebug(__func__, "pool name is %s", pool->name); + pdebug(__func__, "pool URL is %s://%s:%d%s", pool->url.protocol, pool->url.host, pool->url.port, pool->url.path); if (NULL == ctx->pools) { ctx->pools = pool; @@ -53,7 +53,7 @@ void lm_ctx_pools_test(lm_ctx_t *ctx) { while (NULL != cur) { lm_pool_test(cur); if (!cur->available) - pdebug(ctx, __func__, "%s is not avaliable: %s", cur->name, lm_strerror()); + pdebug(__func__, "%s is not avaliable: %s", cur->name, lm_strerror()); cur = cur->next; } } @@ -63,7 +63,7 @@ void lm_ctx_pools_get_info(lm_ctx_t *ctx) { while (NULL != cur) { lm_pool_test(cur); if (!cur->available) - pdebug(ctx, __func__, "%s is not avaliable: %s", cur->name, lm_strerror()); + pdebug(__func__, "%s is not avaliable: %s", cur->name, lm_strerror()); cur = cur->next; } } @@ -99,7 +99,7 @@ bool lm_ctx_pool_load(lm_ctx_t *ctx, lm_pool_t *pool, bool force_update){ snprintf(poolp, sizeof(poolp), "%s/pools/%s", ctx->data, pool->name); size_t poolp_sz = strlen(poolp); - if(mkdir(poolp, 0700) < 0 && errno != EEXIST){ + if(!mkdir_ifnot(poolp)){ lm_error_set(LM_ERR_CtxDataFailMkdir); return false; } @@ -114,12 +114,12 @@ bool lm_ctx_pool_load(lm_ctx_t *ctx, lm_pool_t *pool, bool force_update){ goto update; if(!lm_pool_info_load(pool, infop)){ - pdebug(ctx, __func__, "failed to load info for %s gonna try updating", pool->name); + pdebug(__func__, "failed to load info for %s gonna try updating", pool->name); goto update; } if(!lm_pool_info_load(pool, listp)){ - pdebug(ctx, __func__, "failed to load list for %s gonna try updating", pool->name); + pdebug(__func__, "failed to load list for %s gonna try updating", pool->name); goto update; } @@ -127,13 +127,18 @@ update: unlink(infop); unlink(listp); + if(!pool->available){ + pdebug(__func__, "failed get info and list for %s, pool is not avaliable"); + return false; + } + if(!lm_pool_info_get(pool, infop)){ - pdebug(ctx, __func__, "failed to get info for %s", pool->name); + pdebug(__func__, "failed to get info for %s", pool->name); return false; } if(!lm_pool_list_get(pool, listp)){ - pdebug(ctx, __func__, "failed to get list for %s", pool->name); + pdebug(__func__, "failed to get list for %s", pool->name); return false; } @@ -151,7 +156,7 @@ bool lm_ctx_pools_load(lm_ctx_t *ctx, bool force_update, lm_ctx_pools_callback_t bool status = lm_ctx_pool_load(ctx, cur, force_update); if(!status) - pdebug(ctx, __func__, "failed to load pool: %s", cur->name); + pdebug(__func__, "failed to load pool: %s", cur->name); if(NULL != callback) callback(ctx, cur, status, data); @@ -190,7 +195,7 @@ bool lm_ctx_pools_serve(lm_ctx_t *ctx, char *addr, uint8_t threads) { while (lm_mptp_server_recv(sock, &packet, &saddr)) { if (!lm_mptp_server_verify(&packet)) { - pdebug(ctx, __func__, "skipping packet, failed to verify: %s", lm_strerror()); + pdebug(__func__, "skipping packet, failed to verify: %s", lm_strerror()); continue; } @@ -198,19 +203,19 @@ bool lm_ctx_pools_serve(lm_ctx_t *ctx, char *addr, uint8_t threads) { char path[packet.header.data_size + 1]; if (!lm_mptp_get_data(&packet, path)) { - pdebug(ctx, __func__, "skipping packet, failed to get path: %s", lm_strerror()); + pdebug(__func__, "skipping packet, failed to get path: %s", lm_strerror()); continue; } if (!lm_mptp_get_host(&packet, hostname)) { - pdebug(ctx, __func__, "skipping packet, failed to get hostname: %s", lm_strerror()); + pdebug(__func__, "skipping packet, failed to get hostname: %s", lm_strerror()); continue; } lm_pool_t *pool = lm_ctx_pools_by_url(ctx, hostname, path); if (NULL == pool) { - pdebug(ctx, __func__, "unknown pool, closing connection: %s", hostname); + pdebug(__func__, "unknown pool, closing connection: %s", hostname); lm_mptp_init(&packet, false, MPTP_S2C_BRUH, true); lm_mptp_server_send(sock, &packet, &saddr); continue; @@ -223,6 +228,7 @@ bool lm_ctx_pools_serve(lm_ctx_t *ctx, char *addr, uint8_t threads) { arg->pool = pool; arg->sock = sock; + pdebug(__func__, "adding new connection to the pool"); lm_thpool_add(&tp, lm_pool_serve_thread, arg); } diff --git a/src/mptp/utils.c b/src/mptp/utils.c new file mode 100644 index 0000000..b1fc2d2 --- /dev/null +++ b/src/mptp/utils.c @@ -0,0 +1,58 @@ +#include "../../include/mptp.h" +#include + +bool lm_mptp_sendfile(int sock, struct sockaddr *addr, char *path){ + lm_mptp_t packet; + bool ret = false; + + if (NULL == path) { + lm_mptp_init(&packet, false, MPTP_S2C_BRUH, true); + goto end; + } + + FILE *file = fopen(path, "r"); + size_t read = 0; + + if(NULL == file){ + lm_mptp_init(&packet, false, MPTP_S2C_BRUH, true); + goto end; + } + + lm_mptp_init(&packet, false, MPTP_S2C_COOL, false); + + while ((read = fread(packet.data, 1, MPTP_DATA_MAX, file)) > 0) { + packet.header.data_size = read; + lm_mptp_server_send(sock, &packet, addr); + lm_mptp_init(&packet, false, MPTP_S2C_COOL, false); + } + + fclose(file); + lm_mptp_init(&packet, false, MPTP_S2C_COOL, true); + ret = true; + +end: + lm_mptp_server_send(sock, &packet, addr); + return ret; +} + +bool lm_mptp_recvfile(int sock, char *path){ + FILE *file = fopen(path, "a"); + bool ret = false; + lm_mptp_t packet; + + while(lm_mptp_client_recv(sock, &packet)){ + if(!lm_mptp_client_verify(&packet)) + goto end; + + if(MPTP_IS_LAST(&packet)) + break; + + if(fwrite(packet.data, 1, packet.header.data_size, file)==0) + goto end; + } + + ret = true; +end: + fclose(file); + return ret; +} diff --git a/src/pool/info.c b/src/pool/info.c index 9c10d99..4abb96a 100644 --- a/src/pool/info.c +++ b/src/pool/info.c @@ -43,7 +43,7 @@ bool lm_pool_info_load(lm_pool_t *pool, char *file) { return false; } - pool->info.file = strdup(file); + pool->paths.info = strdup(file); return true; } @@ -57,42 +57,29 @@ bool lm_pool_info_get(lm_pool_t *pool, char *file) { if(NULL == pool->url.path) return false; - FILE *info = fopen(file, "a"); int sock = lm_mptp_client_connect(pool->url.host, pool->url.port); lm_mptp_t packet; bool ret = false; - if(NULL == info) - goto end; - lm_mptp_init(&packet, true, MPTP_C2S_INFO, true); 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)) goto end; - - while(lm_mptp_client_recv(sock, &packet)){ - if(!lm_mptp_client_verify(&packet)) - goto end; - - if(MPTP_IS_LAST(&packet)) - break; - - if(fwrite(packet.data, 1, packet.header.data_size, info)==0) - goto end; - } + + if(!lm_mptp_recvfile(sock, file)) + goto end; ret = true; end: - fclose(info); + lm_mptp_close(sock); if(ret) ret = lm_pool_info_load(pool, file); return ret; } void lm_pool_info_free(lm_pool_t *pool) { - free(pool->info.file); free(pool->info.pubkey); free(pool->info.maintainer); bzero(&pool->info, sizeof(pool->info)); diff --git a/src/pool/list.c b/src/pool/list.c index dda887c..6f0e64e 100644 --- a/src/pool/list.c +++ b/src/pool/list.c @@ -20,19 +20,34 @@ bool lm_pool_list_load(lm_pool_t *pool, char *file){ return false; } - size_t file_len = strlen(file), ent_len = 0; - char filecp[file_len+1], *dir = NULL; + size_t file_len = strlen(file); + char file_copy[file_len + 1]; + char file_copy2[file_len + 1]; + char *list_dir = NULL, *list_name = NULL; + + memcpy(file_copy, file, file_len+1); + memcpy(file_copy2, file, file_len+1); + + list_dir = dirname(file_copy); + list_name = dirname(file_copy2); + + char extract_dir[file_len+15]; + snprintf(extract_dir, sizeof(extract_dir), "%s/%s_extracted", list_dir, list_name); + + if(!mkdir_ifnot(extract_dir)){ + lm_error_set(LM_ERR_PoolListDirFail); + return false; + } + struct dirent *ent = NULL; + size_t ent_len = 0; DIR *dirfd = NULL; bool ret = false; - memcpy(filecp, file, file_len+1); - dir = dirname(filecp); - - if(!extract_archive(dir, file)) + if(!extract_archive(extract_dir, file)) goto end; - if((dirfd = opendir(dir)) == NULL){ + if((dirfd = opendir(extract_dir)) == NULL){ lm_error_set(LM_ERR_PoolListDirFail); goto end; } @@ -40,8 +55,8 @@ bool lm_pool_list_load(lm_pool_t *pool, char *file){ while((ent = readdir(dirfd)) != NULL){ ent_len = strlen(ent->d_name); - char datap[ent_len+file_len+10]; - snprintf(datap, sizeof(datap), "%s/%s/DATA", dir, ent->d_name); + char datap[ent_len+sizeof(extract_dir)+10]; + snprintf(datap, sizeof(datap), "%s/%s/DATA", extract_dir, ent->d_name); lm_pkg_t *pkg = lm_pkg_new(); @@ -72,38 +87,23 @@ bool lm_pool_list_get(lm_pool_t *pool, char *file) { if(NULL == pool->url.path) return false; - FILE *info = fopen(file, "a"); int sock = lm_mptp_client_connect(pool->url.host, pool->url.port); lm_mptp_t packet; bool ret = false; - if(NULL == info) - goto end; - lm_mptp_init(&packet, true, MPTP_C2S_LIST, true); 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)) goto end; - - if(!lm_mptp_client_recv(sock, &packet)) + + if(!lm_mptp_recvfile(sock, file)) goto end; - while(lm_mptp_client_recv(sock, &packet)){ - if(!lm_mptp_client_verify(&packet)) - goto end; - - if(fwrite(packet.data, 1, packet.header.data_size, info)==0) - goto end; - - if(MPTP_IS_LAST(&packet)) - break; - } - ret = true; end: - fclose(info); + lm_mptp_close(sock); if(ret) ret = lm_pool_list_load(pool, file); return ret; diff --git a/src/pool/paths.c b/src/pool/paths.c new file mode 100644 index 0000000..b36f9f8 --- /dev/null +++ b/src/pool/paths.c @@ -0,0 +1,27 @@ +#include "../../include/types.h" + +#include +#include + +void lm_pool_paths_set_info(lm_pool_t *pool, char *info_path){ + free(pool->paths.info); + if(NULL == info_path){ + pool->paths.list = NULL; + return; + } + pool->paths.info = strdup(info_path); +} + +void lm_pool_paths_set_list(lm_pool_t *pool, char *list_path){ + free(pool->paths.list); + if(NULL == list_path){ + pool->paths.list = NULL; + return; + } + pool->paths.list = strdup(list_path); +} + +void lm_pool_paths_free(lm_pool_t *pool){ + free(pool->paths.info); + free(pool->paths.list); +} diff --git a/src/pool/pool.c b/src/pool/pool.c index 0661a56..f15231f 100644 --- a/src/pool/pool.c +++ b/src/pool/pool.c @@ -8,10 +8,7 @@ lm_pool_t *lm_pool_new(char *name, char *url) { lm_pool_t *pool = malloc(sizeof(lm_pool_t)); - bzero(pool, sizeof(lm_pool_t)); - bzero(&pool->info, sizeof(pool->info)); - bzero(&pool->url, sizeof(pool->url)); pool->available = true; pool->name = name; diff --git a/src/pool/serve.c b/src/pool/serve.c index 5022209..53c7d0f 100644 --- a/src/pool/serve.c +++ b/src/pool/serve.c @@ -2,42 +2,36 @@ #include "../../include/util.h" #include "../../include/pool.h" +#include #include void lm_pool_serve(lm_pool_t *pool, lm_mptp_t *packet, int sock, struct sockaddr *addr) { + char ipaddr[INET6_ADDRSTRLEN]; + bzero(ipaddr, sizeof(ipaddr)); + sockaddr_to_str(addr, ipaddr); + switch (MPTP_FLAGS_CODE(packet)) { + + // response PING with PONG case MPTP_C2S_PING: + pdebug(__func__, "(%s) PING from %s returning PONG", pool->name, ipaddr); lm_mptp_init(packet, false, MPTP_S2C_PONG, true); - goto end; + lm_mptp_server_send(sock, packet, addr); + break; + // when INFO file is requested, send the file case MPTP_C2S_INFO: - if (NULL == pool->info.file) { - lm_mptp_init(packet, false, MPTP_S2C_BRUH, true); - goto end; - } + pdebug(__func__, "(%s) INFO from %s attempting to send info", pool->name, ipaddr); + lm_mptp_sendfile(sock, addr, pool->paths.info); + break; - FILE *info = fopen(pool->info.file, "r"); - size_t read = 0; + // when LIST file is requested, send the file + case MPTP_C2S_LIST: + pdebug(__func__, "(%s) LIST from %s attempting to send list", pool->name, ipaddr); + lm_mptp_sendfile(sock, addr, pool->paths.list); + break; - if(NULL == info){ - lm_mptp_init(packet, false, MPTP_S2C_BRUH, true); - goto end; - } - - lm_mptp_init(packet, false, MPTP_S2C_COOL, false); - - while ((read = fread(packet->data, 1, MPTP_DATA_MAX, info)) > 0) { - packet->header.data_size = read; - lm_mptp_server_send(sock, packet, addr); - lm_mptp_init(packet, false, MPTP_S2C_COOL, false); - } - - fclose(info); - lm_mptp_init(packet, false, MPTP_S2C_COOL, true); } - -end: - lm_mptp_server_send(sock, packet, addr); } void lm_pool_serve_thread(void *_arg) { diff --git a/src/util.c b/src/util.c index 63081fb..71215ef 100644 --- a/src/util.c +++ b/src/util.c @@ -3,23 +3,27 @@ #include "../include/types.h" #include +#include +#include #include #include +#include #include #include #include #include +#include #include #include -void pdebug(lm_ctx_t *ctx, const char *func, const char *fmt, ...) { - if (!ctx->debug) +void pdebug(const char *func, const char *fmt, ...) { + if (LM_DEBUG != 1) return; va_list args; va_start(args, fmt); - printf("[DEBUG] %s: ", func); + printf("[libmp debug] %s: ", func); vprintf(fmt, args); printf("\n"); @@ -228,3 +232,24 @@ bool can_read(char *path) { bool can_write(char *path) { return access(path, W_OK) == 0; } + +bool mkdir_ifnot(char *path) { + return !(mkdir(path, 0700) < 0 && errno != EEXIST); +} + +void sockaddr_to_str(struct sockaddr *addr, char *str) { + struct sockaddr_in *ipv4; + struct sockaddr_in6 *ipv6; + + switch (addr->sa_family) { + case AF_INET: + ipv4 = (struct sockaddr_in *)addr; + inet_ntop(AF_INET, &ipv4->sin_addr, str, INET_ADDRSTRLEN); + break; + + case AF_INET6: + ipv6 = (struct sockaddr_in6 *)addr; + inet_ntop(AF_INET6, &ipv6->sin6_addr, str, INET6_ADDRSTRLEN); + break; + } +}