update: add path section to MPTP packet
This commit is contained in:
@ -65,9 +65,9 @@ bool lm_ctx_download(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_download_callback_t ca
|
||||
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_new(&packet, true, MPTP_C2S_PULL, true);
|
||||
lm_mptp_set_host(&packet, pool->url.host);
|
||||
lm_mptp_set_data(&packet, path, strlen(path));
|
||||
lm_mptp_set_path(&packet, path);
|
||||
|
||||
if(!lm_mptp_client_send(sock, &packet))
|
||||
goto end;
|
||||
@ -83,6 +83,7 @@ bool lm_ctx_download(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_download_callback_t ca
|
||||
|
||||
ret = true;
|
||||
end:
|
||||
lm_mptp_free(&packet);
|
||||
lm_mptp_close(sock);
|
||||
return ret;
|
||||
}
|
||||
|
@ -28,6 +28,9 @@ void __lm_ctx_serve_thread(void *_arg) {
|
||||
bool success = false;
|
||||
lm_mptp_t packet;
|
||||
|
||||
lm_mptp_init(&packet);
|
||||
lm_error_clear();
|
||||
|
||||
if(!lm_mptp_server_recv(arg->sock, &packet)){
|
||||
pdebug(__func__, "%x: failed to receive packet (%s)", arg->addr, lm_strerror());
|
||||
return lm_mptp_close(arg->sock);
|
||||
@ -39,14 +42,14 @@ void __lm_ctx_serve_thread(void *_arg) {
|
||||
}
|
||||
|
||||
char hostname[packet.header.host_size + 1]; // +1 for NULL terminator
|
||||
char path[packet.header.data_size + 1], *ppath = path;
|
||||
char path[packet.header.path_size + 1], *ppath = path;
|
||||
|
||||
if (!lm_mptp_get_host(&packet, hostname)) {
|
||||
pdebug(__func__, "%x: closing connection, failed to get hostname (%s)", arg->addr, lm_strerror());
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!lm_mptp_get_data(&packet, path)) {
|
||||
if (!lm_mptp_get_path(&packet, path)) {
|
||||
pdebug(__func__, "%x: closing connection, failed to get path (%s)", arg->addr, lm_strerror());
|
||||
goto end;
|
||||
}
|
||||
@ -60,21 +63,21 @@ void __lm_ctx_serve_thread(void *_arg) {
|
||||
|
||||
if (NULL == pool) {
|
||||
pdebug(__func__, "%x: unknown pool (%s), closing connection", arg->addr, hostname);
|
||||
lm_mptp_init(&packet, false, MPTP_S2C_BRUH, true);
|
||||
lm_mptp_new(&packet, false, MPTP_S2C_BRUH, true);
|
||||
lm_mptp_server_send(arg->sock, &packet);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if(lm_pool_path_is_empty(pool)){
|
||||
pdebug(__func__, "%x: requested pool (%s) have empty paths, closing connection", arg->addr, pool->name);
|
||||
lm_mptp_init(&packet, false, MPTP_S2C_BRUH, true);
|
||||
lm_mptp_new(&packet, false, MPTP_S2C_BRUH, true);
|
||||
lm_mptp_server_send(arg->sock, &packet);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if(!pool->loaded){
|
||||
pdebug(__func__, "%x: requested pool (%s) is not loaded, closing connection", arg->addr, pool->name);
|
||||
lm_mptp_init(&packet, false, MPTP_S2C_BRUH, true);
|
||||
lm_mptp_new(&packet, false, MPTP_S2C_BRUH, true);
|
||||
lm_mptp_server_send(arg->sock, &packet);
|
||||
goto end;
|
||||
}
|
||||
@ -85,7 +88,7 @@ void __lm_ctx_serve_thread(void *_arg) {
|
||||
// response PING with PONG
|
||||
case MPTP_C2S_PING:
|
||||
pdebug(__func__, "PING %s: returning PONG", pool->name);
|
||||
lm_mptp_init(&packet, false, MPTP_S2C_PONG, true);
|
||||
lm_mptp_new(&packet, false, MPTP_S2C_PONG, true);
|
||||
lm_mptp_server_send(arg->sock, &packet);
|
||||
break;
|
||||
|
||||
@ -106,16 +109,16 @@ void __lm_ctx_serve_thread(void *_arg) {
|
||||
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);
|
||||
if(packet.header.path_size <= 0){
|
||||
lm_mptp_new(&packet, false, MPTP_S2C_WHAT, true);
|
||||
lm_mptp_server_send(arg->sock, &packet);
|
||||
break;
|
||||
}
|
||||
|
||||
pdebug(__func__, "PULL %s: attempting to send package archive and signature", pool->name);
|
||||
char path[packet.header.data_size + 1], *package = path;
|
||||
char path[packet.header.path_size + 1], *package = path;
|
||||
|
||||
if(!lm_mptp_get_data(&packet, path)){
|
||||
if(!lm_mptp_get_path(&packet, path)){
|
||||
// we should never be able to get here, if we do theres definetly a bug
|
||||
pdebug(__func__, "PULL %s: skipping, failed to get path (%s)", pool->name, lm_strerror());
|
||||
break;
|
||||
@ -123,7 +126,7 @@ void __lm_ctx_serve_thread(void *_arg) {
|
||||
|
||||
// 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_new(&packet, false, MPTP_S2C_WHAT, true);
|
||||
lm_mptp_server_send(arg->sock, &packet);
|
||||
break;
|
||||
}
|
||||
@ -134,14 +137,14 @@ void __lm_ctx_serve_thread(void *_arg) {
|
||||
|
||||
// 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_new(&packet, false, MPTP_S2C_WHAT, true);
|
||||
lm_mptp_server_send(arg->sock, &packet);
|
||||
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_new(&packet, false, MPTP_S2C_BRUH, true);
|
||||
lm_mptp_server_send(arg->sock, &packet);
|
||||
break;
|
||||
}
|
||||
|
@ -40,6 +40,7 @@ void lm_error_set(lm_error_t code, ...) {
|
||||
{.code = LM_ERR_MPTPSendFail, .desc = _("failed send MPTP data to host") },
|
||||
{.code = LM_ERR_MPTPBadData, .desc = _("MPTP data size is invalid") },
|
||||
{.code = LM_ERR_MPTPBadHost, .desc = _("MPTP host size is invalid") },
|
||||
{.code = LM_ERR_MPTPBadPath, .desc = _("MPTP path size is invalid") },
|
||||
{.code = LM_ERR_MPTPSetsockopt, .desc = _("failed to set MPTP socket options") },
|
||||
{.code = LM_ERR_MPTPTimeout, .desc = _("MPTP connection timed out") },
|
||||
{.code = LM_ERR_MPTPBindFail, .desc = _("failed to bind MPTP socket: %s") },
|
||||
|
@ -28,15 +28,19 @@ int lm_mptp_client_connect(char *addr, uint16_t port) {
|
||||
}
|
||||
|
||||
bool lm_mptp_client_verify(lm_mptp_t *packet) {
|
||||
if (!lm_mptp_verify(packet))
|
||||
if (!lm_mptp_verify(packet)){
|
||||
pdebug(__func__, "failed to verify the packet: %s", lm_strerror());
|
||||
return false;
|
||||
}
|
||||
|
||||
if (MPTP_IS_REQUEST(packet)) {
|
||||
pdebug(__func__, "MPTP packet is a request");
|
||||
lm_error_set(LM_ERR_MPTPNotResponse);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (packet->header.host_size != 0) {
|
||||
pdebug(__func__, "MPTP response has host section");
|
||||
lm_error_set(LM_ERR_MPTPBadHost);
|
||||
return false;
|
||||
}
|
||||
@ -47,42 +51,59 @@ bool lm_mptp_client_verify(lm_mptp_t *packet) {
|
||||
bool lm_mptp_client_send(int sock, lm_mptp_t *packet) {
|
||||
if (NULL == packet) {
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
lm_mptp_free(packet);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (MPTP_FLAGS_VERSION(packet) != MPTP_VERSION_SUPPORTED) {
|
||||
lm_error_set(LM_ERR_MPTPBadVersion);
|
||||
lm_mptp_free(packet);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (packet->header.data_size > MPTP_DATA_MAX) {
|
||||
lm_error_set(LM_ERR_MPTPBadData);
|
||||
lm_mptp_free(packet);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (packet->header.host_size > MPTP_HOST_MAX || packet->header.host_size <= 0) {
|
||||
lm_error_set(LM_ERR_MPTPBadHost);
|
||||
lm_mptp_free(packet);
|
||||
return false;
|
||||
}
|
||||
|
||||
char buffer[sizeof(packet->header) + packet->header.host_size + packet->header.data_size];
|
||||
char buffer[
|
||||
sizeof(packet->header) +
|
||||
packet->header.host_size +
|
||||
packet->header.path_size +
|
||||
packet->header.data_size
|
||||
];
|
||||
ssize_t total = sizeof(buffer), used = 0, buflen = total;
|
||||
|
||||
packet->header.flags = htons(packet->header.flags);
|
||||
bool ret = false;
|
||||
|
||||
copy_to_buffer(buffer, &packet->header, sizeof(packet->header), &total, &used);
|
||||
copy_to_buffer(buffer, packet->host, packet->header.host_size, &total, &used);
|
||||
copy_to_buffer(buffer, packet->path, packet->header.path_size, &total, &used);
|
||||
copy_to_buffer(buffer, packet->data, packet->header.data_size, &total, &used);
|
||||
|
||||
|
||||
packet->header.flags = htons(packet->header.flags);
|
||||
//packet->header.host_size = htons(packet->header.host_size);
|
||||
//packet->header.path_size = htons(packet->header.path_size);
|
||||
packet->header.data_size = htons(packet->header.data_size);
|
||||
|
||||
if (send(sock, buffer, sizeof(buffer), MSG_MORE) < 0) {
|
||||
lm_error_set(LM_ERR_MPTPSendFail);
|
||||
return false;
|
||||
goto end;
|
||||
}
|
||||
|
||||
pdebug(__func__, "printing the packet dump (%lu bytes)", buflen);
|
||||
pdebug_binary(buffer, buflen);
|
||||
ret = true;
|
||||
|
||||
return true;
|
||||
end:
|
||||
lm_mptp_free(packet);
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool lm_mptp_client_recv(int sock, lm_mptp_t *packet) {
|
||||
@ -91,7 +112,7 @@ bool lm_mptp_client_recv(int sock, lm_mptp_t *packet) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bzero(packet, sizeof(lm_mptp_t));
|
||||
lm_mptp_free(packet);
|
||||
|
||||
if(!lm_mptp_recv(sock, packet)){
|
||||
pdebug(__func__, "failed to receive the packet: %s", lm_strerror());
|
||||
|
105
src/mptp/mptp.c
105
src/mptp/mptp.c
@ -5,13 +5,17 @@
|
||||
#include <errno.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
bool lm_mptp_init(lm_mptp_t *packet, bool is_request, uint8_t code, bool is_last) {
|
||||
bzero(&packet->header, sizeof(packet->header));
|
||||
bzero(packet->data, MPTP_DATA_MAX);
|
||||
bzero(packet->host, MPTP_HOST_MAX);
|
||||
void lm_mptp_init(lm_mptp_t *packet){
|
||||
bzero(packet, sizeof(lm_mptp_t));
|
||||
}
|
||||
|
||||
bool lm_mptp_new(lm_mptp_t *packet, bool is_request, uint8_t code, bool is_last) {
|
||||
lm_mptp_init(packet);
|
||||
|
||||
if (code > MPTP_CODE_MAX) {
|
||||
lm_error_set(LM_ERR_MPTPBadCode);
|
||||
@ -35,6 +39,17 @@ bool lm_mptp_init(lm_mptp_t *packet, bool is_request, uint8_t code, bool is_last
|
||||
return true;
|
||||
}
|
||||
|
||||
void lm_mptp_free(lm_mptp_t *packet) {
|
||||
if(NULL == packet)
|
||||
return;
|
||||
|
||||
free(packet->host);
|
||||
free(packet->path);
|
||||
free(packet->data);
|
||||
|
||||
lm_mptp_init(packet);
|
||||
}
|
||||
|
||||
bool lm_mptp_verify(lm_mptp_t *packet) {
|
||||
if (NULL == packet) {
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
@ -46,6 +61,16 @@ bool lm_mptp_verify(lm_mptp_t *packet) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (packet->header.host_size > MPTP_HOST_MAX || packet->header.host_size < 0) {
|
||||
lm_error_set(LM_ERR_MPTPBadHost);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (packet->header.path_size > MPTP_PATH_MAX || packet->header.path_size < 0) {
|
||||
lm_error_set(LM_ERR_MPTPBadPath);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (packet->header.data_size > MPTP_DATA_MAX || packet->header.data_size < 0) {
|
||||
lm_error_set(LM_ERR_MPTPBadData);
|
||||
return false;
|
||||
@ -161,9 +186,13 @@ bool lm_mptp_set_host(lm_mptp_t *packet, char *host) {
|
||||
return false;
|
||||
}
|
||||
|
||||
free(packet->host);
|
||||
packet->host = malloc(size);
|
||||
|
||||
// do NOT copy the NULL terminator
|
||||
packet->header.host_size = size;
|
||||
memcpy(packet->host, host, size);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -179,14 +208,49 @@ bool lm_mptp_get_host(lm_mptp_t *packet, char *host) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool lm_mptp_set_path(lm_mptp_t *packet, char *path) {
|
||||
size_t size = strlen(path);
|
||||
if (size > MPTP_PATH_MAX || size < 0) {
|
||||
lm_error_set(LM_ERR_MPTPBadHost);
|
||||
return false;
|
||||
}
|
||||
|
||||
free(packet->path);
|
||||
packet->path = malloc(size);
|
||||
|
||||
// do NOT copy the NULL terminator
|
||||
packet->header.path_size = size;
|
||||
memcpy(packet->path, path, size);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool lm_mptp_get_path(lm_mptp_t *packet, char *path) {
|
||||
if (packet->header.path_size > MPTP_PATH_MAX || packet->header.path_size < 0) {
|
||||
path = NULL;
|
||||
lm_error_set(LM_ERR_BadHost);
|
||||
return false;
|
||||
}
|
||||
|
||||
memcpy(path, packet->path, packet->header.path_size);
|
||||
path[packet->header.path_size] = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool lm_mptp_set_data(lm_mptp_t *packet, char *data, size_t size) {
|
||||
if (size > MPTP_DATA_MAX || size < 0) {
|
||||
lm_error_set(LM_ERR_MPTPBadData);
|
||||
return false;
|
||||
}
|
||||
|
||||
packet->header.data_size = size;
|
||||
mempcpy(packet->data, data, size);
|
||||
free(packet->data);
|
||||
packet->data = malloc(size);
|
||||
|
||||
if(NULL != data){
|
||||
packet->header.data_size = size;
|
||||
mempcpy(packet->data, data, size);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -202,12 +266,6 @@ bool lm_mptp_get_data(lm_mptp_t *packet, char *data) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void lm_mptp_copy(lm_mptp_t *dst, lm_mptp_t *src) {
|
||||
memcpy(&dst->header, &src->header, sizeof(dst->header));
|
||||
memcpy(&dst->host, &src->data, sizeof(src->host));
|
||||
memcpy(&dst->data, &src->data, sizeof(src->data));
|
||||
}
|
||||
|
||||
bool lm_mptp_recv(int sock, lm_mptp_t *packet) {
|
||||
if (recv(sock, &packet->header, sizeof(packet->header), MSG_WAITALL) <= 0) {
|
||||
if (ETIMEDOUT == errno || EAGAIN == errno) {
|
||||
@ -217,12 +275,14 @@ bool lm_mptp_recv(int sock, lm_mptp_t *packet) {
|
||||
lm_error_set(LM_ERR_MPTPRecvFail, strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
packet->header.flags = ntohs(packet->header.flags);
|
||||
// packet->header.host_size = ntohs(packet->header.host_size);
|
||||
// packet->header.data_size = ntohs(packet->header.data_size);
|
||||
|
||||
/*packet->header.flags = ntohs(packet->header.flags);
|
||||
packet->header.host_size = ntohs(packet->header.host_size);
|
||||
packet->header.path_size = ntohs(packet->header.path_size);
|
||||
packet->header.data_size = ntohs(packet->header.data_size);*/
|
||||
|
||||
if (packet->header.host_size <= MPTP_HOST_MAX && packet->header.host_size != 0){
|
||||
packet->host = malloc(packet->header.host_size);
|
||||
if(recv(sock, packet->host, packet->header.host_size, MSG_WAITALL) <= 0){
|
||||
if (ETIMEDOUT == errno || EAGAIN == errno) {
|
||||
lm_error_set(LM_ERR_MPTPTimeout);
|
||||
@ -233,7 +293,20 @@ bool lm_mptp_recv(int sock, lm_mptp_t *packet) {
|
||||
}
|
||||
}
|
||||
|
||||
if (packet->header.path_size <= MPTP_PATH_MAX && packet->header.path_size != 0){
|
||||
packet->path = malloc(packet->header.path_size);
|
||||
if(recv(sock, packet->path, packet->header.path_size, MSG_WAITALL) <= 0){
|
||||
if (ETIMEDOUT == errno || EAGAIN == errno) {
|
||||
lm_error_set(LM_ERR_MPTPTimeout);
|
||||
return false;
|
||||
}
|
||||
lm_error_set(LM_ERR_MPTPRecvFail, strerror(errno));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (packet->header.data_size <= MPTP_DATA_MAX && packet->header.data_size != 0){
|
||||
packet->data = malloc(packet->header.data_size);
|
||||
if(recv(sock, packet->data, packet->header.data_size, MSG_WAITALL) <= 0){
|
||||
if (ETIMEDOUT == errno || EAGAIN == errno) {
|
||||
lm_error_set(LM_ERR_MPTPTimeout);
|
||||
|
@ -53,20 +53,19 @@ void lm_mptp_server_close(int sock){
|
||||
}
|
||||
|
||||
bool lm_mptp_server_verify(lm_mptp_t *packet) {
|
||||
if (!lm_mptp_verify(packet))
|
||||
if (!lm_mptp_verify(packet)){
|
||||
pdebug(__func__, "failed to verify the packet: %s", lm_strerror());
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!MPTP_IS_REQUEST(packet)) {
|
||||
pdebug(__func__, "MPTP packet is not request");
|
||||
lm_error_set(LM_ERR_MPTPNotRequest);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (packet->header.host_size > MPTP_HOST_MAX || packet->header.host_size <= 0) {
|
||||
lm_error_set(LM_ERR_MPTPBadHost);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!MPTP_IS_LAST(packet)) {
|
||||
pdebug(__func__, "MPTP packet is not the last");
|
||||
lm_error_set(LM_ERR_MPTPNotLast);
|
||||
return false;
|
||||
}
|
||||
@ -80,7 +79,7 @@ bool lm_mptp_server_recv(int sock, lm_mptp_t *packet) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bzero(packet, sizeof(lm_mptp_t));
|
||||
lm_mptp_free(packet);
|
||||
|
||||
if(!lm_mptp_recv(sock, packet)){
|
||||
pdebug(__func__, "failed to receive the packet: %s", lm_strerror());
|
||||
@ -93,40 +92,57 @@ bool lm_mptp_server_recv(int sock, lm_mptp_t *packet) {
|
||||
bool lm_mptp_server_send(int sock, lm_mptp_t *packet) {
|
||||
if (NULL == packet) {
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
lm_mptp_free(packet);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (MPTP_FLAGS_VERSION(packet) != MPTP_VERSION_SUPPORTED) {
|
||||
lm_error_set(LM_ERR_MPTPBadVersion);
|
||||
lm_mptp_free(packet);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (packet->header.data_size > MPTP_DATA_MAX) {
|
||||
lm_error_set(LM_ERR_MPTPBadData);
|
||||
lm_mptp_free(packet);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (packet->header.host_size != 0) {
|
||||
lm_error_set(LM_ERR_MPTPBadHost);
|
||||
lm_mptp_free(packet);
|
||||
return false;
|
||||
}
|
||||
|
||||
char buffer[sizeof(packet->header) + packet->header.host_size + packet->header.data_size];
|
||||
char buffer[
|
||||
sizeof(packet->header) +
|
||||
packet->header.host_size +
|
||||
packet->header.path_size +
|
||||
packet->header.data_size
|
||||
];
|
||||
ssize_t total = sizeof(buffer), used = 0, buflen = total;
|
||||
|
||||
packet->header.flags = htons(packet->header.flags);
|
||||
bool ret = false;
|
||||
|
||||
copy_to_buffer(buffer, &packet->header, sizeof(packet->header), &total, &used);
|
||||
copy_to_buffer(buffer, packet->host, packet->header.host_size, &total, &used);
|
||||
copy_to_buffer(buffer, packet->path, packet->header.path_size, &total, &used);
|
||||
copy_to_buffer(buffer, packet->data, packet->header.data_size, &total, &used);
|
||||
|
||||
packet->header.flags = htons(packet->header.flags);
|
||||
//packet->header.host_size = htons(packet->header.host_size);
|
||||
//packet->header.path_size = htons(packet->header.path_size);
|
||||
packet->header.data_size = htons(packet->header.data_size);
|
||||
|
||||
if (send(sock, buffer, buflen, MSG_MORE) < 0) {
|
||||
lm_error_set(LM_ERR_MPTPSendFail);
|
||||
return false;
|
||||
goto end;
|
||||
}
|
||||
|
||||
pdebug(__func__, "printing the packet dump (%lu bytes)", buflen);
|
||||
pdebug_binary(buffer, buflen);
|
||||
ret = true;
|
||||
|
||||
return true;
|
||||
end:
|
||||
lm_mptp_free(packet);
|
||||
return ret;
|
||||
}
|
||||
|
@ -23,6 +23,8 @@ bool lm_mptp_sendfile(int sock, char *path, lm_mptp_transfer_callback_t callback
|
||||
size_t read = 0;
|
||||
struct stat st;
|
||||
int size = -1;
|
||||
|
||||
lm_mptp_init(&packet);
|
||||
|
||||
if(NULL == file){
|
||||
pdebug(__func__, "failed to open file: %s", path);
|
||||
@ -38,7 +40,9 @@ bool lm_mptp_sendfile(int sock, char *path, lm_mptp_transfer_callback_t callback
|
||||
|
||||
total = st.st_size;
|
||||
|
||||
lm_mptp_init(&packet, false, MPTP_S2C_COOL, false);
|
||||
lm_mptp_new(&packet, false, MPTP_S2C_COOL, false);
|
||||
lm_mptp_set_data(&packet, NULL, digits(st.st_size));
|
||||
|
||||
if((size = snprintf(packet.data, MPTP_DATA_MAX, "%lu", st.st_size)) <= 0){
|
||||
pdebug(__func__, "snprintf for stat size failed: %s", path);
|
||||
lm_error_set(LM_ERR_SendSnprintfFail);
|
||||
@ -52,7 +56,8 @@ bool lm_mptp_sendfile(int sock, char *path, lm_mptp_transfer_callback_t callback
|
||||
goto end_1;
|
||||
|
||||
// clear the packet
|
||||
lm_mptp_init(&packet, false, MPTP_S2C_COOL, false);
|
||||
lm_mptp_new(&packet, false, MPTP_S2C_COOL, false);
|
||||
lm_mptp_set_data(&packet, NULL, MPTP_DATA_MAX);
|
||||
|
||||
while ((read = fread(packet.data, 1, MPTP_DATA_MAX, file)) > 0) {
|
||||
packet.header.data_size = read;
|
||||
@ -68,7 +73,8 @@ bool lm_mptp_sendfile(int sock, char *path, lm_mptp_transfer_callback_t callback
|
||||
if(NULL != callback && !callback(path, current, st.st_size, data))
|
||||
goto end_1;
|
||||
|
||||
lm_mptp_init(&packet, false, MPTP_S2C_COOL, false);
|
||||
lm_mptp_free(&packet);
|
||||
lm_mptp_new(&packet, false, MPTP_S2C_COOL, false);
|
||||
}
|
||||
|
||||
if(current != total){
|
||||
@ -78,14 +84,16 @@ bool lm_mptp_sendfile(int sock, char *path, lm_mptp_transfer_callback_t callback
|
||||
}
|
||||
|
||||
pdebug(__func__, "completed sending %s, sending last packet", path);
|
||||
lm_mptp_init(&packet, false, MPTP_S2C_COOL, true);
|
||||
lm_mptp_new(&packet, false, MPTP_S2C_COOL, true);
|
||||
lm_mptp_server_send(sock, &packet);
|
||||
ret = true;
|
||||
goto end_2;
|
||||
|
||||
end_1:
|
||||
lm_mptp_init(&packet, false, MPTP_S2C_BRUH, true);
|
||||
lm_mptp_free(&packet);
|
||||
lm_mptp_new(&packet, false, MPTP_S2C_BRUH, true);
|
||||
lm_mptp_server_send(sock, &packet);
|
||||
lm_mptp_free(&packet);
|
||||
end_2:
|
||||
if(NULL != file)
|
||||
fclose(file);
|
||||
@ -108,6 +116,8 @@ bool lm_mptp_recvfile(int sock, char *path, lm_mptp_transfer_callback_t callback
|
||||
bool ret = false;
|
||||
lm_mptp_t packet;
|
||||
|
||||
lm_mptp_init(&packet);
|
||||
|
||||
if(NULL == file){
|
||||
pdebug(__func__, "failed to open file: %s", path);
|
||||
lm_error_set(LM_ERR_RecvOpenFail);
|
||||
@ -176,6 +186,7 @@ bool lm_mptp_recvfile(int sock, char *path, lm_mptp_transfer_callback_t callback
|
||||
|
||||
ret = true;
|
||||
end:
|
||||
lm_mptp_free(&packet);
|
||||
if(NULL != file)
|
||||
fclose(file);
|
||||
return ret;
|
||||
|
@ -106,9 +106,9 @@ bool lm_pool_info_download(lm_pool_t *pool, lm_mptp_transfer_callback_t callback
|
||||
if((sock = lm_mptp_client_connect(pool->url.host, pool->url.port)) < 0)
|
||||
return false;
|
||||
|
||||
lm_mptp_init(&packet, true, MPTP_C2S_INFO, true);
|
||||
lm_mptp_new(&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));
|
||||
lm_mptp_set_path(&packet, pool->url.path);
|
||||
|
||||
if(!lm_mptp_client_send(sock, &packet)){
|
||||
pdebug(__func__, "info file request failed for %s: %s", pool->name, lm_strerror());
|
||||
@ -122,6 +122,7 @@ bool lm_pool_info_download(lm_pool_t *pool, lm_mptp_transfer_callback_t callback
|
||||
|
||||
ret = true;
|
||||
end:
|
||||
lm_mptp_free(&packet);
|
||||
lm_mptp_close(sock);
|
||||
if(ret)
|
||||
ret = lm_pool_info_load(pool);
|
||||
|
@ -119,9 +119,9 @@ bool lm_pool_list_download(lm_pool_t *pool, char *dir, lm_mptp_transfer_callback
|
||||
if((sock = lm_mptp_client_connect(pool->url.host, pool->url.port)) < 0)
|
||||
return false;
|
||||
|
||||
lm_mptp_init(&packet, true, MPTP_C2S_LIST, true);
|
||||
lm_mptp_new(&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));
|
||||
lm_mptp_set_path(&packet, pool->url.path);
|
||||
|
||||
if(!lm_mptp_client_send(sock, &packet)){
|
||||
pdebug(__func__, "list file request failed for %s: %s", pool->name, lm_strerror());
|
||||
@ -135,6 +135,7 @@ bool lm_pool_list_download(lm_pool_t *pool, char *dir, lm_mptp_transfer_callback
|
||||
|
||||
ret = true;
|
||||
end:
|
||||
lm_mptp_free(&packet);
|
||||
lm_mptp_close(sock);
|
||||
if(ret)
|
||||
ret = lm_pool_list_load(pool, dir);
|
||||
|
@ -30,10 +30,10 @@ lm_pool_t *lm_pool_new(char *name, char *url) {
|
||||
|
||||
void lm_pool_test(lm_pool_t *pool) {
|
||||
lm_mptp_t packet;
|
||||
lm_mptp_init(&packet, true, MPTP_C2S_PING, true);
|
||||
lm_mptp_new(&packet, true, MPTP_C2S_PING, true);
|
||||
|
||||
lm_mptp_set_host(&packet, pool->url.host);
|
||||
lm_mptp_set_data(&packet, pool->url.path, strlen(pool->url.path));
|
||||
lm_mptp_set_path(&packet, pool->url.path);
|
||||
|
||||
int sock = lm_mptp_client_connect(pool->url.host, pool->url.port);
|
||||
if (sock == -1) {
|
||||
@ -60,6 +60,7 @@ void lm_pool_test(lm_pool_t *pool) {
|
||||
if(!pool->available)
|
||||
lm_error_set(LM_ERR_PoolTestNotPong);
|
||||
end:
|
||||
lm_mptp_free(&packet);
|
||||
lm_mptp_close(sock);
|
||||
return;
|
||||
}
|
||||
|
16
src/util.c
16
src/util.c
@ -108,7 +108,9 @@ bool copy_to_buffer(void *buffer, void *src, size_t size, ssize_t *total, ssize_
|
||||
if (*used == 0)
|
||||
bzero(buffer, *total);
|
||||
|
||||
memcpy(buffer + *used, src, size);
|
||||
if (NULL != buffer && NULL != src && size > 0)
|
||||
memcpy(buffer + *used, src, size);
|
||||
|
||||
*used += size;
|
||||
return true;
|
||||
}
|
||||
@ -120,7 +122,9 @@ bool copy_from_buffer(void *dst, void *buffer, size_t size, ssize_t *total, ssiz
|
||||
if (*used == 0)
|
||||
bzero(dst, size);
|
||||
|
||||
memcpy(dst, buffer + *used, size);
|
||||
if (NULL != buffer && NULL != dst && size > 0)
|
||||
memcpy(dst, buffer + *used, size);
|
||||
|
||||
*used += size;
|
||||
return true;
|
||||
}
|
||||
@ -582,3 +586,11 @@ bool is_dir_empty(char *p) {
|
||||
closedir(fd);
|
||||
return empty;
|
||||
}
|
||||
|
||||
int digits(int n) {
|
||||
if (n < 0)
|
||||
return digits((n == INT_MIN) ? INT_MAX : -n);
|
||||
if (n < 10)
|
||||
return 1;
|
||||
return 1 + digits(n / 10);
|
||||
}
|
||||
|
Reference in New Issue
Block a user