new: debug and version macros, cleaning up client/server APIs
This commit is contained in:
@ -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) {
|
||||
|
Reference in New Issue
Block a user