update: recvfile and sendfile callbacks and more ctx function implementations
This commit is contained in:
@ -60,7 +60,7 @@ bool lm_pool_info_load(lm_pool_t *pool) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool lm_pool_info_download(lm_pool_t *pool) {
|
||||
bool lm_pool_info_download(lm_pool_t *pool, lm_mptp_transfer_callback_t callback, void *data) {
|
||||
if(NULL == pool){
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
return false;
|
||||
@ -100,7 +100,7 @@ bool lm_pool_info_download(lm_pool_t *pool) {
|
||||
if(!lm_mptp_client_send(sock, &packet))
|
||||
goto end;
|
||||
|
||||
if(!lm_mptp_recvfile(sock, pool->paths.info_file))
|
||||
if(!lm_mptp_recvfile(sock, pool->paths.info_file, callback, data))
|
||||
goto end;
|
||||
|
||||
ret = true;
|
||||
|
@ -85,7 +85,7 @@ end:
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool lm_pool_list_download(lm_pool_t *pool) {
|
||||
bool lm_pool_list_download(lm_pool_t *pool, lm_mptp_transfer_callback_t callback, void *data) {
|
||||
if(NULL == pool){
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
return false;
|
||||
@ -130,7 +130,7 @@ bool lm_pool_list_download(lm_pool_t *pool) {
|
||||
if(!lm_mptp_client_send(sock, &packet))
|
||||
goto end;
|
||||
|
||||
if(!lm_mptp_recvfile(sock, pool->paths.list_file))
|
||||
if(!lm_mptp_recvfile(sock, pool->paths.list_file, callback, data))
|
||||
goto end;
|
||||
|
||||
ret = true;
|
||||
|
@ -59,46 +59,3 @@ bool lm_pool_package_add(lm_pool_t *pool, lm_pkg_t *pkg){
|
||||
pool->pkg = pkg;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool lm_pool_package_download(lm_pool_t *pool, lm_pkg_t *pkg){
|
||||
if(NULL == pool || NULL == pkg){
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(lm_package_path_is_empty(pkg)){
|
||||
lm_error_set(LM_ERR_PkgPathsEmpty);
|
||||
return false;
|
||||
}
|
||||
|
||||
char data[strlen(pool->url.path)+strlen(pkg->name)+strlen(pkg->version)+20];
|
||||
char name[strlen(pkg->name)+strlen(pkg->version)+10];
|
||||
|
||||
lm_mptp_t packet;
|
||||
bool ret = false;
|
||||
int sock = -1;
|
||||
|
||||
snprintf(name, sizeof(name), "%s_%s", pkg->name, pkg->version);
|
||||
join(data, pool->url.path, name);
|
||||
|
||||
if((sock = lm_mptp_client_connect(pool->url.host, pool->url.port)) < 0)
|
||||
return false;
|
||||
|
||||
lm_mptp_init(&packet, true, MPTP_C2S_PULL, true);
|
||||
lm_mptp_set_host(&packet, pool->url.host);
|
||||
lm_mptp_set_data(&packet, data, strlen(data));
|
||||
|
||||
if(!lm_mptp_client_send(sock, &packet))
|
||||
goto end;
|
||||
|
||||
if(!lm_mptp_recvfile(sock, pkg->paths.archive))
|
||||
goto end;
|
||||
|
||||
if(!lm_mptp_recvfile(sock, pkg->paths.signature))
|
||||
goto end;
|
||||
|
||||
ret = true;
|
||||
end:
|
||||
lm_mptp_close(sock);
|
||||
return ret;
|
||||
}
|
||||
|
@ -1,93 +0,0 @@
|
||||
#include "../../include/error.h"
|
||||
#include "../../include/util.h"
|
||||
#include "../../include/pool.h"
|
||||
|
||||
#include <libgen.h>
|
||||
#include <stdio.h>
|
||||
#include <string.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);
|
||||
lm_mptp_server_send(sock, packet, addr);
|
||||
break;
|
||||
|
||||
// when INFO file is requested, send the file
|
||||
case MPTP_C2S_INFO:
|
||||
pdebug(__func__, "(%s) INFO from %s attempting to send info", pool->name, ipaddr);
|
||||
lm_mptp_sendfile(sock, addr, pool->paths.info_file);
|
||||
break;
|
||||
|
||||
// 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_file);
|
||||
break;
|
||||
|
||||
// when the request code is PULL, send the requested package archive and
|
||||
// requested package signature
|
||||
case MPTP_C2S_PULL:
|
||||
// PULL request should contain package path,
|
||||
// if path (stored in the data field) is empty it's an invalid request
|
||||
if(packet->header.data_size <= 0){
|
||||
lm_mptp_init(packet, false, MPTP_S2C_WHAT, true);
|
||||
lm_mptp_server_send(sock, packet, addr);
|
||||
break;
|
||||
}
|
||||
|
||||
pdebug(__func__, "(%s) PULL from %s attempting to send package archive and signature", pool->name, ipaddr);
|
||||
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__, "(%s) skipping PULL from %s, failed to get path: %s", pool->name, ipaddr, lm_strerror());
|
||||
break;
|
||||
}
|
||||
|
||||
// if we can't get the package name, then theres something wrong with the request
|
||||
if((package = basename(path)) == NULL){
|
||||
lm_mptp_init(packet, false, MPTP_S2C_WHAT, true);
|
||||
lm_mptp_server_send(sock, packet, addr);
|
||||
break;
|
||||
}
|
||||
|
||||
size_t package_size = strlen(package);
|
||||
char name[package_size+1], version[package_size+1];
|
||||
lm_pkg_t *pkg = NULL;
|
||||
|
||||
// if we can't parse the package name, request is invalid
|
||||
if(!package_parse(package, name, version)){
|
||||
lm_mptp_init(packet, false, MPTP_S2C_WHAT, true);
|
||||
lm_mptp_server_send(sock, packet, addr);
|
||||
break;
|
||||
}
|
||||
|
||||
// if the package is not found in the pool, tell the client
|
||||
if((pkg = lm_pool_package_find(pool, name, version)) == NULL){
|
||||
lm_mptp_init(packet, false, MPTP_S2C_BRUH, true);
|
||||
lm_mptp_server_send(sock, packet, addr);
|
||||
break;
|
||||
}
|
||||
|
||||
// send package archive and the signature file
|
||||
lm_mptp_sendfile(sock, addr, pkg->paths.archive);
|
||||
lm_mptp_sendfile(sock, addr, pkg->paths.signature);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void lm_pool_serve_thread(void *_arg) {
|
||||
lm_pool_thread_arg_t *arg = _arg;
|
||||
lm_pool_serve(arg->pool, &arg->packet, arg->sock, &arg->addr);
|
||||
free(arg);
|
||||
}
|
Reference in New Issue
Block a user