new: debug and version macros, cleaning up client/server APIs
This commit is contained in:
parent
6c2f34e8d5
commit
3b19e2840b
3
Makefile
3
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 $@
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
4
examples/tests/INFO
Normal file
4
examples/tests/INFO
Normal file
@ -0,0 +1,4 @@
|
||||
[base]
|
||||
size = 314073
|
||||
maintainer = ngn
|
||||
pubkey = F9E70878C2FB389AEC2BA34CA3654DF5AD9F641D
|
BIN
examples/tests/LIST
Normal file
BIN
examples/tests/LIST
Normal file
Binary file not shown.
BIN
examples/tests/grep_3.11.mpf
Normal file
BIN
examples/tests/grep_3.11.mpf
Normal file
Binary file not shown.
BIN
examples/tests/grep_3.11.mpf.sig
Normal file
BIN
examples/tests/grep_3.11.mpf.sig
Normal file
Binary file not shown.
@ -1,4 +0,0 @@
|
||||
[test]
|
||||
size = 18221
|
||||
author = ngn
|
||||
pubkey = F9E70878C2FB389AEC2BA34CA3654DF5AD9F641
|
BIN
examples/tests/which_2.21.mpf
Normal file
BIN
examples/tests/which_2.21.mpf
Normal file
Binary file not shown.
BIN
examples/tests/which_2.21.mpf.sig
Normal file
BIN
examples/tests/which_2.21.mpf.sig
Normal file
Binary file not shown.
@ -22,6 +22,8 @@
|
||||
|
||||
// clang-format on
|
||||
|
||||
#define LM_VERSION "24.00"
|
||||
|
||||
#include "ctx.h"
|
||||
#include "error.h"
|
||||
#include "pool.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);
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
|
@ -1,12 +1,13 @@
|
||||
#pragma once
|
||||
#include "types.h"
|
||||
#include <libintl.h>
|
||||
#include <netinet/in.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#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);
|
||||
|
@ -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 <EMAIL@ADDRESS>\n"
|
||||
"Language-Team: LANGUAGE <LL@li.org>\n"
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
58
src/mptp/utils.c
Normal file
58
src/mptp/utils.c
Normal 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;
|
||||
}
|
@ -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));
|
||||
|
@ -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;
|
||||
|
27
src/pool/paths.c
Normal file
27
src/pool/paths.c
Normal 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);
|
||||
}
|
@ -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;
|
||||
|
@ -2,42 +2,36 @@
|
||||
#include "../../include/util.h"
|
||||
#include "../../include/pool.h"
|
||||
|
||||
#include <strings.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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) {
|
||||
|
31
src/util.c
31
src/util.c
@ -3,23 +3,27 @@
|
||||
#include "../include/types.h"
|
||||
|
||||
#include <archive.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
#include <error.h>
|
||||
#include <linux/limits.h>
|
||||
#include <netinet/in.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user