new: debug and version macros, cleaning up client/server APIs

This commit is contained in:
ngn 2024-06-28 20:51:56 +03:00
parent 6c2f34e8d5
commit 3b19e2840b
25 changed files with 231 additions and 128 deletions

View File

@ -11,6 +11,7 @@ HDRS = $(wildcard include/*.h)
CFLAGS = -O3 -march=native -fstack-protector-strong -fcf-protection=full -fstack-clash-protection CFLAGS = -O3 -march=native -fstack-protector-strong -fcf-protection=full -fstack-clash-protection
LIBS = -larchive -linih -lgpgme LIBS = -larchive -linih -lgpgme
DEBUG = 0
VERSION = 24.00 VERSION = 24.00
all: dist/libmp.so $(PO_OUTS) all: dist/libmp.so $(PO_OUTS)
@ -24,7 +25,7 @@ dist/%.o: src/%.c
mkdir -p dist/pkg mkdir -p dist/pkg
mkdir -p dist/mptp mkdir -p dist/mptp
mkdir -p dist/pool 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 locale/%.mo: locale/%.po
msgfmt $^ -o $@ msgfmt $^ -o $@

View File

@ -15,7 +15,6 @@ int main(int argc, char *argv[]) {
lm_ctx_t ctx; lm_ctx_t ctx;
lm_ctx_init(&ctx); lm_ctx_init(&ctx);
ctx.debug = true;
if (!lm_ctx_set_data(&ctx, DATA_DIR)) { if (!lm_ctx_set_data(&ctx, DATA_DIR)) {
printf("failed to set data dir: %s (%d)\n", lm_strerror(), lm_error()); 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; goto end;
} }
lm_ctx_pools_test(&ctx);
if (!lm_ctx_pools_load(&ctx, true, NULL, NULL)) { if (!lm_ctx_pools_load(&ctx, true, NULL, NULL)) {
printf("failed to load pools: %s (%d)\n", lm_strerror(), lm_error()); printf("failed to load pools: %s (%d)\n", lm_strerror(), lm_error());
goto end; goto end;

View File

@ -15,8 +15,6 @@ int main(int argc, char *argv[]) {
lm_pool_t *pool; lm_pool_t *pool;
lm_ctx_init(&ctx); lm_ctx_init(&ctx);
ctx.debug = true;
if ((pool = lm_ctx_pools_add(&ctx, "test", "mptp://127.0.0.1:5858")) == NULL) { 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()); printf("failed to add pool: %s (%d)\n", lm_strerror(), lm_error());
goto end; goto end;

4
examples/tests/INFO Normal file
View File

@ -0,0 +1,4 @@
[base]
size = 314073
maintainer = ngn
pubkey = F9E70878C2FB389AEC2BA34CA3654DF5AD9F641D

BIN
examples/tests/LIST Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,4 +0,0 @@
[test]
size = 18221
author = ngn
pubkey = F9E70878C2FB389AEC2BA34CA3654DF5AD9F641

Binary file not shown.

Binary file not shown.

View File

@ -22,6 +22,8 @@
// clang-format on // clang-format on
#define LM_VERSION "24.00"
#include "ctx.h" #include "ctx.h"
#include "error.h" #include "error.h"
#include "pool.h" #include "pool.h"

View File

@ -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); int lm_mptp_server_listen(char *addr, uint16_t port);
bool lm_mptp_server_verify(lm_mptp_t *packet); 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_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);

View File

@ -21,6 +21,10 @@ void lm_pool_test(lm_pool_t *pool);
void lm_pool_free(lm_pool_t *pool); void lm_pool_free(lm_pool_t *pool);
bool lm_pool_add(lm_pool_t *pool, lm_pkg_t *pkg); 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_load(lm_pool_t *pool, char *file);
bool lm_pool_info_get(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); void lm_pool_info_free(lm_pool_t *pool);

View File

@ -16,12 +16,17 @@ typedef struct lm_pool_info {
char *maintainer; char *maintainer;
char *pubkey; char *pubkey;
size_t size; size_t size;
char *file;
} lm_pool_info_t; } lm_pool_info_t;
typedef struct lm_pool_paths {
char *list;
char *info;
} lm_pool_paths_t;
typedef struct lm_pool { typedef struct lm_pool {
struct lm_pool *next; struct lm_pool *next;
lm_pool_info_t info; lm_pool_info_t info;
lm_pool_paths_t paths;
lm_url_t url; lm_url_t url;
lm_pkg_t *pkg; lm_pkg_t *pkg;
bool available; bool available;
@ -29,10 +34,9 @@ typedef struct lm_pool {
} lm_pool_t; } lm_pool_t;
typedef struct lm_ctx { typedef struct lm_ctx {
lm_pool_t *pools; // pool list lm_pool_t *pools; // pool list
char *root; // root path for package installtion
char *root; // root path for package installtion char *temp; // temp path
char *temp; // temp path char *data; // package database path
char *data; // package database path const char *version; // libmp version (read-only)
bool debug; // is debug output enabled?
} lm_ctx_t; } lm_ctx_t;

View File

@ -1,12 +1,13 @@
#pragma once #pragma once
#include "types.h" #include "types.h"
#include <libintl.h> #include <libintl.h>
#include <netinet/in.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#define _(x) gettext(x) #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 parse_host(char *addr, char *host, uint16_t *port);
bool contains(char *str, char s); bool contains(char *str, char s);
bool eq(char *s1, char *s2); bool eq(char *s1, char *s2);
@ -21,3 +22,5 @@ bool is_file(char *path);
bool is_dir(char *path); bool is_dir(char *path);
bool can_read(char *path); bool can_read(char *path);
bool can_write(char *path); bool can_write(char *path);
bool mkdir_ifnot(char *path);
void sockaddr_to_str(struct sockaddr *addr, char *str);

View File

@ -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-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" "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"

View File

@ -17,7 +17,7 @@ void lm_ctx_init(lm_ctx_t *ctx) {
textdomain("libmp"); textdomain("libmp");
bzero(ctx, sizeof(lm_ctx_t)); bzero(ctx, sizeof(lm_ctx_t));
ctx->debug = false; ctx->version = LM_VERSION;
} }
bool lm_ctx_set_temp(lm_ctx_t *ctx, char *dir){ 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){ bool lm_ctx_set_data(lm_ctx_t *ctx, char *dir){
if(!exists(dir)) if(!mkdir_ifnot(dir)){
goto mkdir; 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)){ if(!is_dir(dir)){
lm_error_set(LM_ERR_CtxDataNotDir); lm_error_set(LM_ERR_CtxDataNotDir);
@ -74,23 +84,6 @@ bool lm_ctx_set_data(lm_ctx_t *ctx, char *dir){
return false; 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); ctx->data = strdup(dir);
return true; return true;
} }

View File

@ -16,8 +16,8 @@ lm_pool_t *lm_ctx_pools_add(lm_ctx_t *ctx, char *name, char *url) {
if (NULL == pool) if (NULL == pool)
return false; return false;
pdebug(ctx, __func__, "pool name is %s", pool->name); pdebug(__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 URL is %s://%s:%d%s", pool->url.protocol, pool->url.host, pool->url.port, pool->url.path);
if (NULL == ctx->pools) { if (NULL == ctx->pools) {
ctx->pools = pool; ctx->pools = pool;
@ -53,7 +53,7 @@ void lm_ctx_pools_test(lm_ctx_t *ctx) {
while (NULL != cur) { while (NULL != cur) {
lm_pool_test(cur); lm_pool_test(cur);
if (!cur->available) 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; cur = cur->next;
} }
} }
@ -63,7 +63,7 @@ void lm_ctx_pools_get_info(lm_ctx_t *ctx) {
while (NULL != cur) { while (NULL != cur) {
lm_pool_test(cur); lm_pool_test(cur);
if (!cur->available) 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; 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); snprintf(poolp, sizeof(poolp), "%s/pools/%s", ctx->data, pool->name);
size_t poolp_sz = strlen(poolp); size_t poolp_sz = strlen(poolp);
if(mkdir(poolp, 0700) < 0 && errno != EEXIST){ if(!mkdir_ifnot(poolp)){
lm_error_set(LM_ERR_CtxDataFailMkdir); lm_error_set(LM_ERR_CtxDataFailMkdir);
return false; return false;
} }
@ -114,12 +114,12 @@ bool lm_ctx_pool_load(lm_ctx_t *ctx, lm_pool_t *pool, bool force_update){
goto update; goto update;
if(!lm_pool_info_load(pool, infop)){ 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; goto update;
} }
if(!lm_pool_info_load(pool, listp)){ 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; goto update;
} }
@ -127,13 +127,18 @@ update:
unlink(infop); unlink(infop);
unlink(listp); 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)){ 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; return false;
} }
if(!lm_pool_list_get(pool, listp)){ 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; 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); bool status = lm_ctx_pool_load(ctx, cur, force_update);
if(!status) if(!status)
pdebug(ctx, __func__, "failed to load pool: %s", cur->name); pdebug(__func__, "failed to load pool: %s", cur->name);
if(NULL != callback) if(NULL != callback)
callback(ctx, cur, status, data); 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)) { while (lm_mptp_server_recv(sock, &packet, &saddr)) {
if (!lm_mptp_server_verify(&packet)) { 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; 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]; char path[packet.header.data_size + 1];
if (!lm_mptp_get_data(&packet, path)) { 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; continue;
} }
if (!lm_mptp_get_host(&packet, hostname)) { 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; continue;
} }
lm_pool_t *pool = lm_ctx_pools_by_url(ctx, hostname, path); lm_pool_t *pool = lm_ctx_pools_by_url(ctx, hostname, path);
if (NULL == pool) { 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_init(&packet, false, MPTP_S2C_BRUH, true);
lm_mptp_server_send(sock, &packet, &saddr); lm_mptp_server_send(sock, &packet, &saddr);
continue; continue;
@ -223,6 +228,7 @@ bool lm_ctx_pools_serve(lm_ctx_t *ctx, char *addr, uint8_t threads) {
arg->pool = pool; arg->pool = pool;
arg->sock = sock; arg->sock = sock;
pdebug(__func__, "adding new connection to the pool");
lm_thpool_add(&tp, lm_pool_serve_thread, arg); lm_thpool_add(&tp, lm_pool_serve_thread, arg);
} }

58
src/mptp/utils.c Normal file
View File

@ -0,0 +1,58 @@
#include "../../include/mptp.h"
#include <stdio.h>
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;
}

View File

@ -43,7 +43,7 @@ bool lm_pool_info_load(lm_pool_t *pool, char *file) {
return false; return false;
} }
pool->info.file = strdup(file); pool->paths.info = strdup(file);
return true; return true;
} }
@ -57,42 +57,29 @@ bool lm_pool_info_get(lm_pool_t *pool, char *file) {
if(NULL == pool->url.path) if(NULL == pool->url.path)
return false; return false;
FILE *info = fopen(file, "a");
int sock = lm_mptp_client_connect(pool->url.host, pool->url.port); int sock = lm_mptp_client_connect(pool->url.host, pool->url.port);
lm_mptp_t packet; lm_mptp_t packet;
bool ret = false; bool ret = false;
if(NULL == info)
goto end;
lm_mptp_init(&packet, true, MPTP_C2S_INFO, true); lm_mptp_init(&packet, true, MPTP_C2S_INFO, true);
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))
goto end; goto end;
while(lm_mptp_client_recv(sock, &packet)){ if(!lm_mptp_recvfile(sock, file))
if(!lm_mptp_client_verify(&packet)) goto end;
goto end;
if(MPTP_IS_LAST(&packet))
break;
if(fwrite(packet.data, 1, packet.header.data_size, info)==0)
goto end;
}
ret = true; ret = true;
end: end:
fclose(info); lm_mptp_close(sock);
if(ret) if(ret)
ret = lm_pool_info_load(pool, file); ret = lm_pool_info_load(pool, file);
return ret; return ret;
} }
void lm_pool_info_free(lm_pool_t *pool) { void lm_pool_info_free(lm_pool_t *pool) {
free(pool->info.file);
free(pool->info.pubkey); free(pool->info.pubkey);
free(pool->info.maintainer); free(pool->info.maintainer);
bzero(&pool->info, sizeof(pool->info)); bzero(&pool->info, sizeof(pool->info));

View File

@ -20,19 +20,34 @@ bool lm_pool_list_load(lm_pool_t *pool, char *file){
return false; return false;
} }
size_t file_len = strlen(file), ent_len = 0; size_t file_len = strlen(file);
char filecp[file_len+1], *dir = NULL; 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; struct dirent *ent = NULL;
size_t ent_len = 0;
DIR *dirfd = NULL; DIR *dirfd = NULL;
bool ret = false; bool ret = false;
memcpy(filecp, file, file_len+1); if(!extract_archive(extract_dir, file))
dir = dirname(filecp);
if(!extract_archive(dir, file))
goto end; goto end;
if((dirfd = opendir(dir)) == NULL){ if((dirfd = opendir(extract_dir)) == NULL){
lm_error_set(LM_ERR_PoolListDirFail); lm_error_set(LM_ERR_PoolListDirFail);
goto end; goto end;
} }
@ -40,8 +55,8 @@ bool lm_pool_list_load(lm_pool_t *pool, char *file){
while((ent = readdir(dirfd)) != NULL){ while((ent = readdir(dirfd)) != NULL){
ent_len = strlen(ent->d_name); ent_len = strlen(ent->d_name);
char datap[ent_len+file_len+10]; char datap[ent_len+sizeof(extract_dir)+10];
snprintf(datap, sizeof(datap), "%s/%s/DATA", dir, ent->d_name); snprintf(datap, sizeof(datap), "%s/%s/DATA", extract_dir, ent->d_name);
lm_pkg_t *pkg = lm_pkg_new(); 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) if(NULL == pool->url.path)
return false; return false;
FILE *info = fopen(file, "a");
int sock = lm_mptp_client_connect(pool->url.host, pool->url.port); int sock = lm_mptp_client_connect(pool->url.host, pool->url.port);
lm_mptp_t packet; lm_mptp_t packet;
bool ret = false; bool ret = false;
if(NULL == info)
goto end;
lm_mptp_init(&packet, true, MPTP_C2S_LIST, true); lm_mptp_init(&packet, true, MPTP_C2S_LIST, true);
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))
goto end; goto end;
if(!lm_mptp_client_recv(sock, &packet)) if(!lm_mptp_recvfile(sock, file))
goto end; 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; ret = true;
end: end:
fclose(info); lm_mptp_close(sock);
if(ret) if(ret)
ret = lm_pool_list_load(pool, file); ret = lm_pool_list_load(pool, file);
return ret; return ret;

27
src/pool/paths.c Normal file
View File

@ -0,0 +1,27 @@
#include "../../include/types.h"
#include <stdlib.h>
#include <string.h>
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);
}

View File

@ -8,10 +8,7 @@
lm_pool_t *lm_pool_new(char *name, char *url) { lm_pool_t *lm_pool_new(char *name, char *url) {
lm_pool_t *pool = malloc(sizeof(lm_pool_t)); lm_pool_t *pool = malloc(sizeof(lm_pool_t));
bzero(pool, 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->available = true;
pool->name = name; pool->name = name;

View File

@ -2,42 +2,36 @@
#include "../../include/util.h" #include "../../include/util.h"
#include "../../include/pool.h" #include "../../include/pool.h"
#include <strings.h>
#include <stdlib.h> #include <stdlib.h>
void lm_pool_serve(lm_pool_t *pool, lm_mptp_t *packet, int sock, struct sockaddr *addr) { 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)) { switch (MPTP_FLAGS_CODE(packet)) {
// response PING with PONG
case MPTP_C2S_PING: case MPTP_C2S_PING:
pdebug(__func__, "(%s) PING from %s returning PONG", pool->name, ipaddr);
lm_mptp_init(packet, false, MPTP_S2C_PONG, true); 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: case MPTP_C2S_INFO:
if (NULL == pool->info.file) { pdebug(__func__, "(%s) INFO from %s attempting to send info", pool->name, ipaddr);
lm_mptp_init(packet, false, MPTP_S2C_BRUH, true); lm_mptp_sendfile(sock, addr, pool->paths.info);
goto end; break;
}
FILE *info = fopen(pool->info.file, "r"); // when LIST file is requested, send the file
size_t read = 0; 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) { void lm_pool_serve_thread(void *_arg) {

View File

@ -3,23 +3,27 @@
#include "../include/types.h" #include "../include/types.h"
#include <archive.h> #include <archive.h>
#include <arpa/inet.h>
#include <errno.h>
#include <error.h> #include <error.h>
#include <linux/limits.h> #include <linux/limits.h>
#include <netinet/in.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/socket.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
void pdebug(lm_ctx_t *ctx, const char *func, const char *fmt, ...) { void pdebug(const char *func, const char *fmt, ...) {
if (!ctx->debug) if (LM_DEBUG != 1)
return; return;
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
printf("[DEBUG] %s: ", func); printf("[libmp debug] %s: ", func);
vprintf(fmt, args); vprintf(fmt, args);
printf("\n"); printf("\n");
@ -228,3 +232,24 @@ bool can_read(char *path) {
bool can_write(char *path) { bool can_write(char *path) {
return access(path, W_OK) == 0; 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;
}
}