Compare commits
21 Commits
Author | SHA1 | Date | |
---|---|---|---|
70b760c0f8 | |||
3be2056c21 | |||
1404da3c6c | |||
f5d8514a27 | |||
6fdc283cc4 | |||
cb9f5da339 | |||
7fc63f3693 | |||
89fe97ee8e | |||
544df2a4ab | |||
d75b8b9c1b | |||
bee0073e58 | |||
518dd4fb8d | |||
cdb997215b | |||
8080299d7d | |||
ae83552e96 | |||
7e7cd68a1e | |||
65a9d7610b | |||
d7dd578fc4 | |||
edc732ac69 | |||
75fa38f1d7 | |||
865141177a |
1
Makefile
1
Makefile
@ -12,7 +12,6 @@ CFLAGS = -O3 -fstack-protector-strong -fcf-protection=full -fstack-clash-protec
|
||||
LIBS = -lpthread -lcrypto -larchive -linih -lgpgme -lsqlite3
|
||||
|
||||
DEBUG = 0
|
||||
VERSION = 24.00
|
||||
|
||||
all: dist dist/libmp.so $(PO_OUTS)
|
||||
|
||||
|
@ -39,7 +39,7 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
printf("resolving package: %s (%s)...\n", pkg->data.name, pkg->data.version);
|
||||
|
||||
if ((list = lm_ctx_resolve(&ctx, pkg, NULL)) == NULL) {
|
||||
if ((list = lm_ctx_resolve(&ctx, pkg, true, NULL)) == NULL) {
|
||||
printf("failed to resolve package: %s (%d)\n", lm_strerror(), lm_error());
|
||||
goto end;
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
lm_ctx_sync(&ctx, false, NULL, NULL);
|
||||
|
||||
if (!lm_ctx_serve(&ctx, argv[1], 10, NULL, NULL)) {
|
||||
if (!lm_ctx_serve(&ctx, argv[1], 10, NULL, NULL, NULL)) {
|
||||
printf("failed to serve the pools: %s (%d)\n", lm_strerror(), lm_error());
|
||||
goto end;
|
||||
}
|
||||
|
@ -22,7 +22,7 @@
|
||||
|
||||
// clang-format on
|
||||
|
||||
#define LM_VERSION "24.00"
|
||||
#define LM_VERSION "24.01"
|
||||
|
||||
#include "ctx.h"
|
||||
#include "error.h"
|
||||
|
@ -3,6 +3,7 @@
|
||||
#include "mptp.h"
|
||||
#include "pool.h"
|
||||
|
||||
#include <signal.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef struct lm_ctx {
|
||||
@ -73,8 +74,8 @@ lm_ctx_list_t *lm_ctx_list(lm_ctx_t *ctx, lm_ctx_list_t *list); // returns the s
|
||||
lm_pkg_t *lm_ctx_list_next(lm_ctx_list_t *list); // returns the next package in the list
|
||||
void lm_ctx_list_free(lm_ctx_list_t *list); // frees the package list state
|
||||
|
||||
lm_ctx_resolve_list_t *lm_ctx_resolve(
|
||||
lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_resolve_list_t *list); // resolves a package and returns a list of packages
|
||||
lm_ctx_resolve_list_t *lm_ctx_resolve(lm_ctx_t *ctx, lm_pkg_t *pkg, bool resolve_depends,
|
||||
lm_ctx_resolve_list_t *list); // resolves a package and returns a list of packages
|
||||
lm_pkg_t *lm_ctx_resolve_next(lm_ctx_resolve_list_t *list); // returns the next package in the list
|
||||
void lm_ctx_resolve_free(lm_ctx_resolve_list_t *list); // frees the resolved list returned by lm_ctx_resolve
|
||||
|
||||
@ -95,9 +96,9 @@ bool lm_ctx_check(
|
||||
lm_ctx_t *ctx, lm_entry_t *entry, lm_ctx_check_callback_t callback, void *data); // checks a single package
|
||||
size_t lm_ctx_sync(lm_ctx_t *ctx, bool do_update, lm_ctx_sync_callback_t callback, void *data); // syncs all the pools
|
||||
void lm_ctx_ping(lm_ctx_t *ctx, lm_ctx_ping_callback_t callback, void *data); // pings all the pools
|
||||
bool lm_ctx_serve(
|
||||
lm_ctx_t *ctx, char *addr, uint8_t threads, lm_ctx_serve_callback_t callback, void *data); // serves all the pools
|
||||
//
|
||||
bool lm_ctx_serve(lm_ctx_t *ctx, char *addr, uint8_t threads, __sighandler_t handler, lm_ctx_serve_callback_t callback,
|
||||
void *data); // serves all the pools
|
||||
|
||||
/* ##############################
|
||||
## temp directory fucntions ##
|
||||
############################## */
|
||||
|
@ -145,6 +145,14 @@ typedef enum lm_error {
|
||||
LM_ERR_FileNotExist = 143,
|
||||
LM_ERR_FileNotLink = 144,
|
||||
LM_ERR_ArchiveSetFail = 145,
|
||||
LM_ERR_ChdirFail = 146,
|
||||
LM_ERR_ExtractRootChdirFail = 147,
|
||||
LM_ERR_ExtractOldChdirFail = 148,
|
||||
LM_ERR_MPTPAcceptFail = 149,
|
||||
LM_ERR_MPTPListenFail = 150,
|
||||
LM_ERR_PoolInfoBadName = 151,
|
||||
LM_ERR_PoolInfoUnknown = 152,
|
||||
LM_ERR_MPTPBadPath = 153,
|
||||
} lm_error_t;
|
||||
|
||||
typedef struct lm_error_desc {
|
||||
|
@ -13,9 +13,12 @@
|
||||
#####################################
|
||||
# FLAGS #
|
||||
#####################################
|
||||
# HOST SIZE | DATA SIZE #
|
||||
# HOST SIZE | PATH SIZE #
|
||||
#####################################
|
||||
# DATA SIZE #
|
||||
#####################################
|
||||
# HOST #
|
||||
# PATH #
|
||||
# DATA #
|
||||
#...................................#
|
||||
|
||||
@ -82,7 +85,8 @@
|
||||
|
||||
#define MPTP_VERSION_SUPPORTED 0
|
||||
#define MPTP_CODE_MAX 3
|
||||
#define MPTP_DATA_MAX UINT8_MAX
|
||||
#define MPTP_DATA_MAX 4096 // generally this is the page size on x86_64 linux
|
||||
#define MPTP_PATH_MAX UINT8_MAX
|
||||
#define MPTP_HOST_MAX UINT8_MAX
|
||||
#define MPTP_TIMEOUT 10
|
||||
|
||||
@ -106,34 +110,45 @@ typedef enum lm_mptp_response {
|
||||
typedef struct lm_mptp_header {
|
||||
uint16_t flags;
|
||||
uint8_t host_size;
|
||||
uint8_t data_size;
|
||||
} lm_mptp_header_t;
|
||||
uint8_t path_size;
|
||||
uint16_t data_size;
|
||||
} __attribute__((packed)) lm_mptp_header_t;
|
||||
|
||||
typedef struct lm_mptp {
|
||||
lm_mptp_header_t header;
|
||||
char host[MPTP_DATA_MAX];
|
||||
char data[MPTP_DATA_MAX];
|
||||
char *host, *path, *data;
|
||||
} lm_mptp_t;
|
||||
|
||||
typedef bool (*lm_mptp_transfer_callback_t)(char *path, size_t current, size_t total, void *data);
|
||||
|
||||
#define MPTP_FLAGS_VERSION(m) (((m)->header.flags & 0xFF00) >> 8)
|
||||
#define MPTP_FLAGS_TYPE(m) (((m)->header.flags & 0x0080) >> 1)
|
||||
#define MPTP_FLAGS_CODE(m) (((m)->header.flags & 0x0070) >> 4)
|
||||
#define MPTP_FLAGS_LAST(m) (((m)->header.flags & 0x0008) >> 3)
|
||||
#define MPTP_FLAGS_VERSION(m) (((m)->header.flags >> 8) & 0xFF)
|
||||
#define MPTP_FLAGS_TYPE(m) (((m)->header.flags >> 7) & 0x01)
|
||||
#define MPTP_FLAGS_CODE(m) (((m)->header.flags >> 3) & 0x0F)
|
||||
#define MPTP_FLAGS_LAST(m) (((m)->header.flags >> 2) & 0x01)
|
||||
|
||||
#define MPTP_IS_REQUEST(m) (MPTP_FLAGS_TYPE(m) == 0)
|
||||
#define MPTP_IS_LAST(m) (MPTP_FLAGS_LAST(m) == 1)
|
||||
|
||||
bool lm_mptp_init(lm_mptp_t *packet, bool is_request, uint8_t code, bool is_last);
|
||||
bool lm_mptp_set_data(lm_mptp_t *packet, char *data, size_t size);
|
||||
bool lm_mptp_set_host(lm_mptp_t *packet, char *host);
|
||||
bool lm_mptp_get_host(lm_mptp_t *packet, char *host);
|
||||
bool lm_mptp_get_data(lm_mptp_t *packet, char *data);
|
||||
void lm_mptp_init(lm_mptp_t *packet);
|
||||
bool lm_mptp_new(lm_mptp_t *packet, bool is_request, uint8_t code, bool is_last);
|
||||
void lm_mptp_free(lm_mptp_t *packet);
|
||||
|
||||
int lm_mptp_socket(char *addr, uint16_t port, struct sockaddr *saddr);
|
||||
bool lm_mptp_socket_opts(int sock);
|
||||
void lm_mptp_copy(lm_mptp_t *dst, lm_mptp_t *src);
|
||||
bool lm_mptp_verify(lm_mptp_t *packet);
|
||||
void lm_mptp_close(int sock);
|
||||
bool lm_mptp_recv(int sock, lm_mptp_t *packet);
|
||||
void lm_mptp_free(lm_mptp_t *packet);
|
||||
|
||||
bool lm_mptp_set_data(lm_mptp_t *packet, char *data, size_t size);
|
||||
bool lm_mptp_get_data(lm_mptp_t *packet, char *data);
|
||||
|
||||
bool lm_mptp_set_host(lm_mptp_t *packet, char *host);
|
||||
bool lm_mptp_get_host(lm_mptp_t *packet, char *host);
|
||||
|
||||
bool lm_mptp_set_path(lm_mptp_t *packet, char *path);
|
||||
bool lm_mptp_get_path(lm_mptp_t *packet, char *path);
|
||||
|
||||
int lm_mptp_client_connect(char *addr, uint16_t port);
|
||||
bool lm_mptp_client_verify(lm_mptp_t *packet);
|
||||
@ -141,9 +156,11 @@ bool lm_mptp_client_send(int sock, lm_mptp_t *packet);
|
||||
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_accept(int sock, struct sockaddr *addr);
|
||||
void lm_mptp_server_close(int sock);
|
||||
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 *addr);
|
||||
bool lm_mptp_server_recv(int sock, lm_mptp_t *packet);
|
||||
bool lm_mptp_server_send(int sock, lm_mptp_t *packet);
|
||||
|
||||
bool lm_mptp_sendfile(int sock, struct sockaddr *addr, char *path, lm_mptp_transfer_callback_t callback, void *data);
|
||||
bool lm_mptp_sendfile(int sock, char *path, lm_mptp_transfer_callback_t callback, void *data);
|
||||
bool lm_mptp_recvfile(int sock, char *path, lm_mptp_transfer_callback_t callback, void *data);
|
||||
|
@ -8,6 +8,9 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#define _(x) gettext(x)
|
||||
#define BINARY(b) \
|
||||
(b & 128 ? '1' : '0'), (b & 64 ? '1' : '0'), (b & 32 ? '1' : '0'), (b & 16 ? '1' : '0'), (b & 8 ? '1' : '0'), \
|
||||
(b & 4 ? '1' : '0'), (b & 2 ? '1' : '0'), (b & 1 ? '1' : '0')
|
||||
|
||||
bool contains(char *str, char s);
|
||||
bool eq(char *s1, char *s2);
|
||||
@ -27,12 +30,15 @@ bool exists(char *path, struct stat *st);
|
||||
bool is_empty(char *p);
|
||||
bool is_dir_empty(char *p);
|
||||
bool rmrf(char *p);
|
||||
int digits(int n);
|
||||
|
||||
bool package_parse(char *package, char *name, char *version);
|
||||
bool package_version_valid(char *name);
|
||||
bool package_name_valid(char *name);
|
||||
|
||||
void pdebug(const char *func, const char *fmt, ...);
|
||||
void pdebug_binary(char *data, size_t len);
|
||||
|
||||
bool parse_host(char *addr, char *host, uint16_t *port);
|
||||
bool copy_blocks(struct archive *w, struct archive *r);
|
||||
bool extract_archive(char *dst, char *src);
|
||||
|
@ -8,7 +8,7 @@ msgid ""
|
||||
msgstr ""
|
||||
"Project-Id-Version: PACKAGE VERSION\n"
|
||||
"Report-Msgid-Bugs-To: \n"
|
||||
"POT-Creation-Date: 2024-08-06 04:34+0300\n"
|
||||
"POT-Creation-Date: 2024-08-10 00:05+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"
|
||||
@ -102,7 +102,8 @@ msgid "failed to connect to the MPTP host"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:39
|
||||
msgid "failed receive MPTP data from host"
|
||||
#, c-format
|
||||
msgid "failed receive MPTP data from host: %s"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:40
|
||||
@ -120,500 +121,539 @@ msgid "MPTP host size is invalid"
|
||||
msgstr "URL path is too large"
|
||||
|
||||
#: src/error.c:43
|
||||
#, fuzzy
|
||||
msgid "MPTP path size is invalid"
|
||||
msgstr "URL path is too large"
|
||||
|
||||
#: src/error.c:44
|
||||
msgid "failed to set MPTP socket options"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:44
|
||||
#: src/error.c:45
|
||||
msgid "MPTP connection timed out"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:45
|
||||
msgid "failed to bind MPTP socket"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:46
|
||||
msgid "required argument is a NULL pointer or 0"
|
||||
#, c-format
|
||||
msgid "failed to bind MPTP socket: %s"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:47
|
||||
msgid "not a MPTP request"
|
||||
msgid "required argument is a NULL pointer or 0"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:48
|
||||
msgid "not a MPTP request"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:49
|
||||
msgid "not a MPTP response"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:49 src/error.c:50
|
||||
#: src/error.c:50 src/error.c:51
|
||||
msgid "MPTP request last flag is not set"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:51
|
||||
#: src/error.c:52
|
||||
msgid "host port not specified"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:52
|
||||
#: src/error.c:53
|
||||
msgid "pool info is badly formatted or is not complete"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:53
|
||||
#: src/error.c:54
|
||||
msgid "failed to write block from archive"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:54
|
||||
#: src/error.c:55
|
||||
msgid "failed to read block from archive"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:55
|
||||
#: src/error.c:56
|
||||
msgid "failed to open archive"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:56
|
||||
#: src/error.c:57
|
||||
msgid "failed to write archive header"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:57
|
||||
#: src/error.c:58
|
||||
msgid "failed to finish writing the archive entry"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:58
|
||||
#: src/error.c:59
|
||||
msgid "failed to create new archive reader/writer"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:59
|
||||
#: src/error.c:60
|
||||
msgid "failed to resolve full path for archive file"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:60
|
||||
#: src/error.c:61
|
||||
msgid "failed to read the next header of the archive"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:61
|
||||
#: src/error.c:62
|
||||
msgid "failed to obtain current working directory"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:62
|
||||
#: src/error.c:63
|
||||
msgid "failed to open extracted pool list directory"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:63
|
||||
#: src/error.c:64
|
||||
msgid "failed to read access the pool list file"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:64
|
||||
#: src/error.c:65
|
||||
msgid "failed to read access the pool info file"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:65
|
||||
#: src/error.c:66
|
||||
msgid "failed to parse package data"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:66
|
||||
#: src/error.c:67
|
||||
#, fuzzy
|
||||
msgid "package name is invalid"
|
||||
msgstr "URL hostname is too large"
|
||||
|
||||
#: src/error.c:67
|
||||
#: src/error.c:68
|
||||
msgid "data path is not set with in the ctx"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:68
|
||||
#: src/error.c:69
|
||||
msgid "temp path is not set with in the ctx"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:69
|
||||
msgid "root path is not set with in the ctx"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:70
|
||||
#, c-format
|
||||
msgid "failed to set the ctx temp director to %s: %s"
|
||||
msgid "root path is not set with in the ctx"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:71
|
||||
#, c-format
|
||||
msgid "failed to set the ctx root directory to %s: %s"
|
||||
msgid "failed to set the ctx temp director to %s: %s"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:72
|
||||
#, c-format
|
||||
msgid "failed to set the ctx data directory to %s: %s"
|
||||
msgid "failed to set the ctx root directory to %s: %s"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:73
|
||||
msgid "pool did not respond ping with pong"
|
||||
#, c-format
|
||||
msgid "failed to set the ctx data directory to %s: %s"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:74
|
||||
msgid "package file and directory paths are empty"
|
||||
msgid "pool did not respond ping with pong"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:75
|
||||
msgid "failed to to open target file for sending"
|
||||
msgid "package file and directory paths are empty"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:76
|
||||
msgid "failed to to delete target file for receiving"
|
||||
msgid "failed to to open target file for sending"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:77
|
||||
msgid "failed to to open target file for receiving"
|
||||
msgid "failed to to delete target file for receiving"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:78
|
||||
msgid "got a bad response code for receiving the target file"
|
||||
msgid "failed to to open target file for receiving"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:79
|
||||
msgid "failed to write to the target file for receiving"
|
||||
msgid "got a bad response code for receiving the target file"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:80
|
||||
msgid "failed to write to the target file for receiving"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:81
|
||||
#, fuzzy
|
||||
msgid "package not found"
|
||||
msgstr "URL hostname is too large"
|
||||
|
||||
#: src/error.c:81
|
||||
#: src/error.c:82
|
||||
msgid "failed to access to the database file/directory"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:82
|
||||
#: src/error.c:83
|
||||
msgid "failed to open SQLite database"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:83
|
||||
#: src/error.c:84
|
||||
msgid "failed to create table in SQLite database"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:84
|
||||
#: src/error.c:85
|
||||
msgid "failed to prepare statement for SQLite database"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:85
|
||||
#: src/error.c:86
|
||||
msgid "failed to insert to the table in SQLite database"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:86
|
||||
#: src/error.c:87
|
||||
msgid "failed to select from the table in SQLite database"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:87
|
||||
#: src/error.c:88
|
||||
msgid "failed to delete from the table in SQLite database"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:88
|
||||
#: src/error.c:89
|
||||
msgid "failed to find entry in SQLite database"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:89
|
||||
#: src/error.c:90
|
||||
msgid "failed to init GPG for package verification"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:90
|
||||
#: src/error.c:91
|
||||
msgid "failed to import signature to GPG for package verification"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:91
|
||||
#: src/error.c:92
|
||||
msgid "failed to import archive to GPG for package verification"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:92
|
||||
#: src/error.c:93
|
||||
msgid "package signature verification failed with zero matches"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:93
|
||||
#: src/error.c:94
|
||||
msgid "package signature verification failed with zero results"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:94
|
||||
#: src/error.c:95
|
||||
msgid "pool file and directory paths are empty"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:95
|
||||
#: src/error.c:96
|
||||
msgid "pool is not avaliable for connection"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:96
|
||||
#: src/error.c:97
|
||||
msgid "pool URL is empty or invalid"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:97
|
||||
#: src/error.c:98
|
||||
msgid "pool directory path is not accessible"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:98
|
||||
#: src/error.c:99
|
||||
msgid "pool directory sub-paths are not accessible"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:99
|
||||
#: src/error.c:100
|
||||
msgid "package file list not found in the database"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:100
|
||||
#: src/error.c:101
|
||||
msgid "failed to open package file list in the database"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:101
|
||||
#: src/error.c:102
|
||||
msgid "failed to access package file list database directory"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:102
|
||||
#: src/error.c:103
|
||||
msgid "failed to remove package file list from the database"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:103
|
||||
#: src/error.c:104
|
||||
msgid "failed to write to the file list in the database"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:104
|
||||
#: src/error.c:105
|
||||
msgid "package keep list not found in the database"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:105
|
||||
#: src/error.c:106
|
||||
msgid "failed to open package keep list in the database"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:106
|
||||
#: src/error.c:107
|
||||
msgid "failed to access package keep list database directory"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:107
|
||||
msgid "failed to remove package keep list from the database"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:108
|
||||
#, c-format
|
||||
msgid "failed to find %s (dependency of %s)"
|
||||
msgid "failed to remove package keep list from the database"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:109
|
||||
#, c-format
|
||||
msgid "failed to download %s for installation: %s"
|
||||
msgid "failed to find %s (dependency of %s)"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:110
|
||||
#, c-format
|
||||
msgid "failed to download %s for installation: %s"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:111
|
||||
#, fuzzy
|
||||
msgid "package is not downloaded"
|
||||
msgstr "URL hostname is too large"
|
||||
|
||||
#: src/error.c:111 src/error.c:112
|
||||
#: src/error.c:112 src/error.c:113
|
||||
msgid "failed to remove downloaded package"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:113
|
||||
#: src/error.c:114
|
||||
msgid "failed to open the destination file"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:114
|
||||
#: src/error.c:115
|
||||
msgid "failed to open the source file"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:115 src/error.c:116
|
||||
#: src/error.c:116 src/error.c:117
|
||||
msgid "failed to write to the destination file"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:117
|
||||
#: src/error.c:118
|
||||
msgid "package does not have associated pool"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:118
|
||||
#: src/error.c:119
|
||||
msgid "failed to create specified temp directory"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:119
|
||||
#: src/error.c:120
|
||||
msgid "package archive does not contain required files"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:120
|
||||
#: src/error.c:121
|
||||
msgid "package data does not match with target package"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:121
|
||||
#: src/error.c:122
|
||||
#, c-format
|
||||
msgid "failed to update changes file for package: %s"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:122
|
||||
#: src/error.c:123
|
||||
msgid "failed to access package hashes file"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:123
|
||||
#: src/error.c:124
|
||||
msgid "failed to remove package changes file from the database"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:124
|
||||
#: src/error.c:125
|
||||
msgid "failed to stat target file for sending"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:125
|
||||
#: src/error.c:126
|
||||
msgid "failed to format target file size for sending"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:126
|
||||
#: src/error.c:127
|
||||
msgid "failed to read target file size for sending"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:127
|
||||
#: src/error.c:128
|
||||
msgid "failed to parse target file size for receiving"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:128
|
||||
#: src/error.c:129
|
||||
msgid "target file is not fully received"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:129
|
||||
msgid "failed to stat for target extract archive"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:130
|
||||
#, c-format
|
||||
msgid "failed to add package file (%s) to the database: %s"
|
||||
msgid "failed to stat for target extract archive"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:131
|
||||
#, c-format
|
||||
msgid "failed to extract package files: %s"
|
||||
msgid "failed to add package file (%s) to the database: %s"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:132
|
||||
#, c-format
|
||||
msgid "failed to add package to the database: %s"
|
||||
msgid "failed to extract package files: %s"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:133
|
||||
#, c-format
|
||||
msgid "failed to add package to the database: %s"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:134
|
||||
#, fuzzy
|
||||
msgid "package is already installed"
|
||||
msgstr "URL hostname is too large"
|
||||
|
||||
#: src/error.c:134
|
||||
#: src/error.c:135
|
||||
#, fuzzy
|
||||
msgid "package is not installed"
|
||||
msgstr "URL hostname is too large"
|
||||
|
||||
#: src/error.c:135
|
||||
#: src/error.c:136
|
||||
#, c-format
|
||||
msgid "failed to remove package file (%s): %s"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:136
|
||||
#: src/error.c:137
|
||||
#, c-format
|
||||
msgid "failed to remove package from the database: %s"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:137
|
||||
#: src/error.c:138
|
||||
#, c-format
|
||||
msgid "failed to remove package files from the database: %s"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:138
|
||||
#: src/error.c:139
|
||||
#, c-format
|
||||
msgid "failed to remove changes file for package: %s"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:139
|
||||
#: src/error.c:140
|
||||
msgid "failed to get current directory for running install"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:140
|
||||
#: src/error.c:141
|
||||
msgid "failed change directory to root for running install"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:141
|
||||
#: src/error.c:142
|
||||
msgid "failed run install spawn command"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:143
|
||||
#: src/error.c:144
|
||||
msgid "failed to change directory to old directory after running install"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:144
|
||||
msgid "install script returned a bad status code"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:145
|
||||
#, c-format
|
||||
msgid "failed to run the package install script: %s"
|
||||
msgid "install script returned a bad status code"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:146
|
||||
#, c-format
|
||||
msgid "failed to save the package install script: %s"
|
||||
msgid "failed to run the package install script: %s"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:147
|
||||
#, c-format
|
||||
msgid "removing package breaks %s"
|
||||
msgid "failed to save the package install script: %s"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:148
|
||||
#, c-format
|
||||
msgid "removing package breaks %s"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:149
|
||||
#, fuzzy
|
||||
msgid "package is already up-to-date"
|
||||
msgstr "URL hostname is too large"
|
||||
|
||||
#: src/error.c:149
|
||||
#: src/error.c:150
|
||||
msgid "failed to open file for hashing"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:150
|
||||
msgid "failed create digest for hashing"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:151
|
||||
#, c-format
|
||||
msgid "failed to get hash of %s: %s"
|
||||
msgid "failed create digest for hashing"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:152
|
||||
#, c-format
|
||||
msgid "file hash does not match for %s"
|
||||
msgid "failed to get hash of %s: %s"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:153
|
||||
#, c-format
|
||||
msgid "file hash does not match for %s"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:154
|
||||
#, fuzzy
|
||||
msgid "pool info is not loaded"
|
||||
msgstr "URL hostname is too large"
|
||||
|
||||
#: src/error.c:154
|
||||
#: src/error.c:155
|
||||
msgid "pool list is empty"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:155
|
||||
#: src/error.c:156
|
||||
msgid "package changes file not found in the database"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:156
|
||||
#: src/error.c:157
|
||||
msgid "failed to change mod of the changes file"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:157
|
||||
#: src/error.c:158
|
||||
msgid "failed to create install script save directory"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:158
|
||||
#: src/error.c:159
|
||||
msgid "directory does not have write permissions"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:159
|
||||
#: src/error.c:160
|
||||
msgid "specified path is not a directory"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:160
|
||||
#: src/error.c:161
|
||||
msgid "failed to create the specified directory"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:161
|
||||
msgid "specified list extraction directory is not accessible"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:162
|
||||
#, c-format
|
||||
msgid "file does not exist: %s"
|
||||
msgid "specified list extraction directory is not accessible"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:163
|
||||
#, c-format
|
||||
msgid "file is a symbolic link: %s"
|
||||
msgid "file does not exist: %s"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:164
|
||||
#, c-format
|
||||
msgid "file is a symbolic link: %s"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:165
|
||||
msgid "failed to set the package archive"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:166
|
||||
#, c-format
|
||||
msgid "failed change directory: %s"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:167
|
||||
msgid "failed to change directory to root during extraction"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:168
|
||||
msgid "failed to change directory back from root during extraction"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:169
|
||||
#, c-format
|
||||
msgid "failed to accept the MPTP connection: %s"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:170
|
||||
#, c-format
|
||||
msgid "failed to listen the MPTP socket: %s"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:171
|
||||
#, c-format
|
||||
msgid "pool name (%s) doesn't match with: %s"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:172
|
||||
#, c-format
|
||||
msgid "unknown key in the configuration: %s"
|
||||
msgstr ""
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -20,8 +20,7 @@ bool __lm_ctx_save_install(lm_ctx_t *ctx, lm_pkg_t *pkg, char *install_path){
|
||||
return false;
|
||||
}
|
||||
|
||||
// no need to save empty install script :)
|
||||
if(is_empty(install_path))
|
||||
if(!exists(install_path, NULL) || is_empty(install_path))
|
||||
return true;
|
||||
|
||||
char script_name[strlen(pkg->data.name)+15];
|
||||
@ -42,8 +41,7 @@ bool __lm_ctx_save_install(lm_ctx_t *ctx, lm_pkg_t *pkg, char *install_path){
|
||||
}
|
||||
|
||||
bool __lm_ctx_run_install(char *root, char *install_path) {
|
||||
// no need to run empty install script :)
|
||||
if(is_empty(install_path))
|
||||
if(!exists(install_path, NULL) || is_empty(install_path))
|
||||
return true;
|
||||
|
||||
char *args[] = {"/bin/bash", install_path, NULL}, *oldpwd = NULL;
|
||||
@ -107,7 +105,10 @@ bool __lm_ctx_extract_files(lm_ctx_t *ctx, lm_pkg_t *pkg, char *files, lm_ctx_in
|
||||
goto end;
|
||||
}
|
||||
|
||||
chdir(ctx->root);
|
||||
if(chdir(ctx->root) < 0){
|
||||
lm_error_set(LM_ERR_ExtractRootChdirFail);
|
||||
goto end;
|
||||
}
|
||||
|
||||
flags = ARCHIVE_EXTRACT_PERM;
|
||||
flags |= ARCHIVE_EXTRACT_UNLINK;
|
||||
@ -202,7 +203,10 @@ end:
|
||||
}
|
||||
|
||||
if (NULL != oldpwd) {
|
||||
chdir(oldpwd);
|
||||
if(chdir(oldpwd) < 0){
|
||||
lm_error_set(LM_ERR_ExtractOldChdirFail);
|
||||
ret = false;
|
||||
}
|
||||
free(oldpwd);
|
||||
}
|
||||
|
||||
|
@ -7,11 +7,11 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
bool __lm_ctx_resolve(lm_ctx_t *ctx, lm_ctx_resolve_list_t *list, lm_pkg_t *pkg){
|
||||
bool __lm_ctx_resolve(lm_ctx_t *ctx, lm_ctx_resolve_list_t *list, lm_pkg_t *pkg, bool resolve_depends){
|
||||
if(pkglist_contains(list->packages, pkg))
|
||||
return true;
|
||||
|
||||
if(NULL == pkg->data.depends)
|
||||
if(!resolve_depends || NULL == pkg->data.depends)
|
||||
goto end;
|
||||
|
||||
list->resolving = pkglist_add_top(list->resolving, pkg);
|
||||
@ -34,7 +34,7 @@ bool __lm_ctx_resolve(lm_ctx_t *ctx, lm_ctx_resolve_list_t *list, lm_pkg_t *pkg)
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!__lm_ctx_resolve(ctx, list, depend))
|
||||
if(!__lm_ctx_resolve(ctx, list, depend, resolve_depends))
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -45,7 +45,7 @@ end:
|
||||
return true;
|
||||
}
|
||||
|
||||
lm_ctx_resolve_list_t *lm_ctx_resolve(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_resolve_list_t *list){
|
||||
lm_ctx_resolve_list_t *lm_ctx_resolve(lm_ctx_t *ctx, lm_pkg_t *pkg, bool resolve_depends, lm_ctx_resolve_list_t *list){
|
||||
if(NULL == ctx || NULL == pkg){
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
return NULL;
|
||||
@ -56,7 +56,7 @@ lm_ctx_resolve_list_t *lm_ctx_resolve(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_resol
|
||||
bzero(list, sizeof(lm_ctx_resolve_list_t));
|
||||
}
|
||||
|
||||
if(!__lm_ctx_resolve(ctx, list, pkg)){
|
||||
if(!__lm_ctx_resolve(ctx, list, pkg, resolve_depends)){
|
||||
pkglist_free(list->resolving);
|
||||
pkglist_free(list->packages);
|
||||
free(list);
|
||||
|
208
src/ctx/serve.c
208
src/ctx/serve.c
@ -6,40 +6,102 @@
|
||||
|
||||
#include <errno.h>
|
||||
#include <libgen.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
int __serve_sock = 0;
|
||||
lm_thpool_t __serve_tp;
|
||||
|
||||
struct lm_ctx_serve_thread_arg {
|
||||
lm_ctx_serve_callback_t callback;
|
||||
int sock;
|
||||
struct sockaddr addr;
|
||||
lm_mptp_t packet;
|
||||
lm_pool_t *pool;
|
||||
lm_ctx_serve_callback_t callback;
|
||||
lm_ctx_t *ctx;
|
||||
void *data;
|
||||
};
|
||||
|
||||
void __lm_ctx_serve(lm_pool_t *pool, lm_mptp_t *packet, int sock, struct sockaddr *addr) {
|
||||
switch (MPTP_FLAGS_CODE(packet)) {
|
||||
void __lm_ctx_serve_thread(void *_arg) {
|
||||
struct lm_ctx_serve_thread_arg *arg = _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);
|
||||
}
|
||||
|
||||
if (!lm_mptp_server_verify(&packet)) {
|
||||
pdebug(__func__, "%x: closing connection, failed to verify (%s)", arg->addr, lm_strerror());
|
||||
return lm_mptp_close(arg->sock);
|
||||
}
|
||||
|
||||
char hostname[packet.header.host_size + 1]; // +1 for NULL terminator
|
||||
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_path(&packet, path)) {
|
||||
pdebug(__func__, "%x: closing connection, failed to get path (%s)", arg->addr, lm_strerror());
|
||||
goto end;
|
||||
}
|
||||
|
||||
if(MPTP_FLAGS_CODE(&packet) == MPTP_C2S_PULL && (ppath = dirname(path)) == NULL){
|
||||
pdebug(__func__, "%x: closing connection, failed to get dirname (%s)", strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
|
||||
lm_pool_t *pool = lm_ctx_pool_by_url(arg->ctx, hostname, ppath);
|
||||
|
||||
if (NULL == pool) {
|
||||
pdebug(__func__, "%x: unknown pool (%s), closing connection", arg->addr, hostname);
|
||||
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_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_new(&packet, false, MPTP_S2C_BRUH, true);
|
||||
lm_mptp_server_send(arg->sock, &packet);
|
||||
goto end;
|
||||
}
|
||||
|
||||
success = true;
|
||||
|
||||
switch (MPTP_FLAGS_CODE(&packet)) {
|
||||
// response PING with PONG
|
||||
case MPTP_C2S_PING:
|
||||
pdebug(__func__, "(%s) PING: returning PONG", pool->name);
|
||||
lm_mptp_init(packet, false, MPTP_S2C_PONG, true);
|
||||
lm_mptp_server_send(sock, packet, addr);
|
||||
pdebug(__func__, "PING %s: returning PONG", pool->name);
|
||||
lm_mptp_new(&packet, false, MPTP_S2C_PONG, true);
|
||||
lm_mptp_server_send(arg->sock, &packet);
|
||||
break;
|
||||
|
||||
// when INFO file is requested, send the file
|
||||
case MPTP_C2S_INFO:
|
||||
pdebug(__func__, "(%s) INFO: attempting to send info", pool->name);
|
||||
lm_mptp_sendfile(sock, addr, pool->info_file, NULL, NULL);
|
||||
pdebug(__func__, "INFO %s: attempting to send info (%s)", pool->name, pool->info_file);
|
||||
lm_mptp_sendfile(arg->sock, pool->info_file, NULL, NULL);
|
||||
break;
|
||||
|
||||
// when LIST file is requested, send the file
|
||||
case MPTP_C2S_LIST:
|
||||
pdebug(__func__, "(%s) LIST: attempting to send list", pool->name);
|
||||
lm_mptp_sendfile(sock, addr, pool->list_file, NULL, NULL);
|
||||
pdebug(__func__, "LIST %s: attempting to send list (%s)", pool->name, pool->list_file);
|
||||
lm_mptp_sendfile(arg->sock, pool->list_file, NULL, NULL);
|
||||
break;
|
||||
|
||||
// when the request code is PULL, send the requested package archive and
|
||||
@ -47,25 +109,25 @@ void __lm_ctx_serve(lm_pool_t *pool, lm_mptp_t *packet, int sock, struct sockadd
|
||||
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);
|
||||
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__, "(%s) PULL: attempting to send package archive and signature", pool->name);
|
||||
char path[packet->header.data_size + 1], *package = path;
|
||||
pdebug(__func__, "PULL %s: attempting to send package archive and signature", pool->name);
|
||||
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__, "(%s) PULL: skipping, failed to get path: %s", pool->name, lm_strerror());
|
||||
pdebug(__func__, "PULL %s: skipping, failed to get path (%s)", pool->name, 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);
|
||||
lm_mptp_new(&packet, false, MPTP_S2C_WHAT, true);
|
||||
lm_mptp_server_send(arg->sock, &packet);
|
||||
break;
|
||||
}
|
||||
|
||||
@ -75,34 +137,40 @@ void __lm_ctx_serve(lm_pool_t *pool, lm_mptp_t *packet, int sock, struct sockadd
|
||||
|
||||
// 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);
|
||||
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_server_send(sock, packet, addr);
|
||||
lm_mptp_new(&packet, false, MPTP_S2C_BRUH, true);
|
||||
lm_mptp_server_send(arg->sock, &packet);
|
||||
break;
|
||||
}
|
||||
|
||||
// send package archive and the signature file
|
||||
lm_mptp_sendfile(sock, addr, pkg->archive, NULL, NULL);
|
||||
lm_mptp_sendfile(sock, addr, pkg->signature, NULL, NULL);
|
||||
if(!lm_mptp_sendfile(arg->sock, pkg->archive, NULL, NULL))
|
||||
goto end; // if we fail to send the archive no need to send the signature
|
||||
lm_mptp_sendfile(arg->sock, pkg->signature, NULL, NULL);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void __lm_ctx_serve_thread(void *_arg) {
|
||||
struct lm_ctx_serve_thread_arg *arg = _arg;
|
||||
if(NULL != arg->callback && !arg->callback(arg->pool, &arg->packet, &arg->addr, arg->data))
|
||||
return free(arg);
|
||||
__lm_ctx_serve(arg->pool, &arg->packet, arg->sock, &arg->addr);
|
||||
end:
|
||||
lm_mptp_close(arg->sock);
|
||||
if(success && NULL != arg->callback)
|
||||
arg->callback(pool, &packet, &arg->addr, arg->data);
|
||||
free(arg);
|
||||
}
|
||||
|
||||
bool lm_ctx_serve(lm_ctx_t *ctx, char *addr, uint8_t threads, lm_ctx_serve_callback_t callback, void *data){
|
||||
void __lm_ctx_serve_signal(int sig){
|
||||
pdebug(__func__, "received interrupt, stopping the server");
|
||||
lm_thpool_stop(&__serve_tp);
|
||||
lm_mptp_server_close(__serve_sock);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
bool lm_ctx_serve(lm_ctx_t *ctx, char *addr, uint8_t threads, __sighandler_t handler, lm_ctx_serve_callback_t callback, void *data){
|
||||
if (NULL == addr || threads < 0) {
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
return false;
|
||||
@ -110,12 +178,10 @@ bool lm_ctx_serve(lm_ctx_t *ctx, char *addr, uint8_t threads, lm_ctx_serve_callb
|
||||
|
||||
char host[strlen(addr)+1];
|
||||
struct sockaddr saddr;
|
||||
lm_mptp_t packet;
|
||||
uint16_t port;
|
||||
int sock;
|
||||
lm_thpool_t tp;
|
||||
int c = -1;
|
||||
|
||||
lm_thpool_init(&tp, threads);
|
||||
lm_thpool_init(&__serve_tp, threads);
|
||||
|
||||
if (!parse_host(addr, host, &port))
|
||||
return false;
|
||||
@ -125,69 +191,29 @@ bool lm_ctx_serve(lm_ctx_t *ctx, char *addr, uint8_t threads, lm_ctx_serve_callb
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((sock = lm_mptp_server_listen(host, port)) < 0)
|
||||
if ((__serve_sock = lm_mptp_server_listen(host, port)) < 0)
|
||||
return false;
|
||||
|
||||
while (lm_mptp_server_recv(sock, &packet, &saddr)) {
|
||||
if (!lm_mptp_server_verify(&packet)) {
|
||||
pdebug(__func__, "skipping packet, failed to verify: %s", lm_strerror());
|
||||
continue;
|
||||
}
|
||||
if(NULL == handler)
|
||||
handler = __lm_ctx_serve_signal;
|
||||
|
||||
char hostname[packet.header.host_size + 1]; // +1 for NULL terminator
|
||||
char path[packet.header.data_size + 1], *ppath = path;
|
||||
|
||||
if (!lm_mptp_get_host(&packet, hostname)) {
|
||||
pdebug(__func__, "skipping packet, failed to get hostname: %s", lm_strerror());
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!lm_mptp_get_data(&packet, path)) {
|
||||
pdebug(__func__, "skipping packet, failed to get path: %s", lm_strerror());
|
||||
continue;
|
||||
}
|
||||
|
||||
if(MPTP_FLAGS_CODE(&packet) == MPTP_C2S_PULL && (ppath = dirname(path)) == NULL){
|
||||
pdebug(__func__, "skipping packet, failed to get dirname: %s", strerror(errno));
|
||||
continue;
|
||||
}
|
||||
|
||||
lm_pool_t *pool = lm_ctx_pool_by_url(ctx, hostname, ppath);
|
||||
|
||||
if (NULL == pool) {
|
||||
pdebug(__func__, "unknown pool (%s), closing connection", hostname);
|
||||
lm_mptp_init(&packet, false, MPTP_S2C_BRUH, true);
|
||||
lm_mptp_server_send(sock, &packet, &saddr);
|
||||
continue;
|
||||
}
|
||||
|
||||
if(lm_pool_path_is_empty(pool)){
|
||||
pdebug(__func__, "requested pool (%s) have empty paths, closing connection", pool->name);
|
||||
lm_mptp_init(&packet, false, MPTP_S2C_BRUH, true);
|
||||
lm_mptp_server_send(sock, &packet, &saddr);
|
||||
continue;
|
||||
}
|
||||
|
||||
if(!pool->loaded){
|
||||
pdebug(__func__, "requested pool (%s) is not loaded, closing connection", pool->name);
|
||||
lm_mptp_init(&packet, false, MPTP_S2C_BRUH, true);
|
||||
lm_mptp_server_send(sock, &packet, &saddr);
|
||||
continue;
|
||||
}
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
signal(SIGINT, handler);
|
||||
|
||||
while ((c = lm_mptp_server_accept(__serve_sock, &saddr)) != -1) {
|
||||
struct lm_ctx_serve_thread_arg *arg = malloc(sizeof(struct lm_ctx_serve_thread_arg));
|
||||
|
||||
memcpy(&arg->addr, &saddr, sizeof(struct sockaddr));
|
||||
lm_mptp_copy(&arg->packet, &packet);
|
||||
|
||||
arg->callback = callback;
|
||||
arg->data = data;
|
||||
arg->pool = pool;
|
||||
arg->sock = sock;
|
||||
arg->ctx = ctx;
|
||||
arg->sock = c;
|
||||
|
||||
pdebug(__func__, "adding new connection to the pool");
|
||||
lm_thpool_add(&tp, __lm_ctx_serve_thread, arg);
|
||||
lm_thpool_add(&__serve_tp, __lm_ctx_serve_thread, arg);
|
||||
}
|
||||
|
||||
lm_thpool_stop(&tp);
|
||||
return true;
|
||||
lm_thpool_stop(&__serve_tp);
|
||||
// error set by mptp_server_accept
|
||||
return false;
|
||||
}
|
||||
|
14
src/error.c
14
src/error.c
@ -36,13 +36,14 @@ void lm_error_set(lm_error_t code, ...) {
|
||||
{.code = LM_ERR_MPTPHostFail, .desc = _("failed to resolve hostname for MPTP connection") },
|
||||
{.code = LM_ERR_MPTPSocketFail, .desc = _("failed to create a MPTP socket") },
|
||||
{.code = LM_ERR_MPTPConnectFail, .desc = _("failed to connect to the MPTP host") },
|
||||
{.code = LM_ERR_MPTPRecvFail, .desc = _("failed receive MPTP data from host") },
|
||||
{.code = LM_ERR_MPTPRecvFail, .desc = _("failed receive MPTP data from host: %s") },
|
||||
{.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") },
|
||||
{.code = LM_ERR_MPTPBindFail, .desc = _("failed to bind MPTP socket: %s") },
|
||||
{.code = LM_ERR_ArgNULL, .desc = _("required argument is a NULL pointer or 0") },
|
||||
{.code = LM_ERR_MPTPNotRequest, .desc = _("not a MPTP request") },
|
||||
{.code = LM_ERR_MPTPNotResponse, .desc = _("not a MPTP response") },
|
||||
@ -87,7 +88,7 @@ void lm_error_set(lm_error_t code, ...) {
|
||||
{.code = LM_ERR_DbSqlDeleteFail, .desc = _("failed to delete from the table in SQLite database") },
|
||||
{.code = LM_ERR_DbSqlNotFound, .desc = _("failed to find entry in SQLite database") },
|
||||
{.code = LM_ERR_PkgGPGFail, .desc = _("failed to init GPG for package verification") },
|
||||
{.code = LM_ERR_PkgGPGSigFail, .desc = _("failed to import signature to GPG for package verification")},
|
||||
{.code = LM_ERR_PkgGPGSigFail, .desc = _("failed to import signature to GPG for package verification") },
|
||||
{.code = LM_ERR_PkgGPGArchiveFail, .desc = _("failed to import archive to GPG for package verification") },
|
||||
{.code = LM_ERR_PkgSigNoMatch, .desc = _("package signature verification failed with zero matches") },
|
||||
{.code = LM_ERR_PkgSigNoResult, .desc = _("package signature verification failed with zero results") },
|
||||
@ -162,6 +163,13 @@ void lm_error_set(lm_error_t code, ...) {
|
||||
{.code = LM_ERR_FileNotExist, .desc = _("file does not exist: %s") },
|
||||
{.code = LM_ERR_FileNotLink, .desc = _("file is a symbolic link: %s") },
|
||||
{.code = LM_ERR_ArchiveSetFail, .desc = _("failed to set the package archive") },
|
||||
{.code = LM_ERR_ChdirFail, .desc = _("failed change directory: %s") },
|
||||
{.code = LM_ERR_ExtractRootChdirFail, .desc = _("failed to change directory to root during extraction") },
|
||||
{.code = LM_ERR_ExtractOldChdirFail, .desc = _("failed to change directory back from root during extraction")},
|
||||
{.code = LM_ERR_MPTPAcceptFail, .desc = _("failed to accept the MPTP connection: %s") },
|
||||
{.code = LM_ERR_MPTPListenFail, .desc = _("failed to listen the MPTP socket: %s") },
|
||||
{.code = LM_ERR_PoolInfoBadName, .desc = _("pool name (%s) doesn't match with: %s") },
|
||||
{.code = LM_ERR_PoolInfoUnknown, .desc = _("unknown key in the configuration: %s") },
|
||||
};
|
||||
|
||||
char *fmt = NULL;
|
||||
|
@ -15,17 +15,8 @@ int lm_mptp_client_connect(char *addr, uint16_t port) {
|
||||
if ((sock = lm_mptp_socket(addr, port, &saddr)) < 0)
|
||||
return -1;
|
||||
|
||||
struct timeval timeout;
|
||||
bzero(&timeout, sizeof(timeout));
|
||||
|
||||
timeout.tv_sec = MPTP_TIMEOUT;
|
||||
timeout.tv_usec = 0;
|
||||
|
||||
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0) {
|
||||
lm_error_set(LM_ERR_MPTPSetsockopt);
|
||||
lm_mptp_close(sock);
|
||||
if(!lm_mptp_socket_opts(sock))
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (connect(sock, &saddr, sizeof(saddr)) < 0) {
|
||||
lm_mptp_close(sock);
|
||||
@ -37,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;
|
||||
}
|
||||
@ -56,39 +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];
|
||||
ssize_t total = sizeof(buffer), used = 0;
|
||||
|
||||
packet->header.flags = htons(packet->header.flags);
|
||||
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;
|
||||
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);
|
||||
|
||||
if (send(sock, buffer, sizeof(buffer), 0) < 0) {
|
||||
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;
|
||||
}
|
||||
|
||||
return true;
|
||||
pdebug(__func__, "printing the packet dump (%lu bytes)", buflen);
|
||||
pdebug_binary(buffer, buflen);
|
||||
ret = true;
|
||||
|
||||
end:
|
||||
lm_mptp_free(packet);
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool lm_mptp_client_recv(int sock, lm_mptp_t *packet) {
|
||||
@ -97,31 +112,15 @@ bool lm_mptp_client_recv(int sock, lm_mptp_t *packet) {
|
||||
return false;
|
||||
}
|
||||
|
||||
char buffer[sizeof(packet->header) + MPTP_HOST_MAX + MPTP_DATA_MAX];
|
||||
ssize_t total = sizeof(buffer), used = 0;
|
||||
lm_mptp_free(packet);
|
||||
|
||||
bzero(buffer, sizeof(buffer));
|
||||
bzero(packet, sizeof(lm_mptp_t));
|
||||
|
||||
if (recv(sock, buffer, sizeof(buffer), 0) < 0) {
|
||||
if (ETIMEDOUT == errno || EAGAIN == errno) {
|
||||
lm_error_set(LM_ERR_MPTPTimeout);
|
||||
return false;
|
||||
}
|
||||
lm_error_set(LM_ERR_MPTPRecvFail);
|
||||
return false;
|
||||
if(!lm_mptp_recv(sock, packet)){
|
||||
pdebug(__func__, "failed to receive the packet: %s", lm_strerror());
|
||||
return false; // error set by function
|
||||
}
|
||||
|
||||
copy_from_buffer(&packet->header, buffer, sizeof(packet->header), &total, &used);
|
||||
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);
|
||||
|
||||
if (packet->header.host_size <= MPTP_HOST_MAX)
|
||||
copy_from_buffer(&packet->host, buffer, packet->header.host_size, &total, &used);
|
||||
|
||||
if (packet->header.data_size <= MPTP_DATA_MAX)
|
||||
copy_from_buffer(&packet->data, buffer, packet->header.data_size, &total, &used);
|
||||
pdebug(__func__, "printing the header dump");
|
||||
pdebug_binary((char*)&packet->header, sizeof(packet->header));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
176
src/mptp/mptp.c
176
src/mptp/mptp.c
@ -2,17 +2,20 @@
|
||||
#include "../../include/mptp.h"
|
||||
#include "../../include/url.h"
|
||||
|
||||
#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) {
|
||||
packet->header.flags = 0;
|
||||
packet->header.data_size = 0;
|
||||
packet->header.host_size = 0;
|
||||
void lm_mptp_init(lm_mptp_t *packet){
|
||||
bzero(packet, sizeof(lm_mptp_t));
|
||||
}
|
||||
|
||||
bzero(packet->data, MPTP_DATA_MAX);
|
||||
bzero(packet->host, MPTP_HOST_MAX);
|
||||
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);
|
||||
@ -26,16 +29,27 @@ bool lm_mptp_init(lm_mptp_t *packet, bool is_request, uint8_t code, bool is_last
|
||||
else
|
||||
packet->header.flags |= (MPTP_RESPONSE << 7);
|
||||
|
||||
packet->header.flags |= (code << 4);
|
||||
packet->header.flags |= (code << 3);
|
||||
|
||||
if (is_last || is_request)
|
||||
packet->header.flags |= (1 << 3);
|
||||
packet->header.flags |= (1 << 2);
|
||||
else
|
||||
packet->header.flags |= (0 << 3);
|
||||
packet->header.flags |= (0 << 2);
|
||||
|
||||
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);
|
||||
@ -47,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;
|
||||
@ -60,6 +84,41 @@ bool lm_mptp_verify(lm_mptp_t *packet) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool lm_mptp_socket_opts(int sock){
|
||||
struct timeval timeout;
|
||||
int flags = 1;
|
||||
|
||||
bzero(&timeout, sizeof(timeout));
|
||||
timeout.tv_sec = MPTP_TIMEOUT;
|
||||
timeout.tv_usec = 0;
|
||||
|
||||
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0) {
|
||||
lm_error_set(LM_ERR_MPTPSetsockopt);
|
||||
lm_mptp_close(sock);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)) < 0) {
|
||||
lm_error_set(LM_ERR_MPTPSetsockopt);
|
||||
lm_mptp_close(sock);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &flags, sizeof(int)) < 0) {
|
||||
lm_error_set(LM_ERR_MPTPSetsockopt);
|
||||
lm_mptp_close(sock);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &flags, sizeof(int)) < 0) {
|
||||
lm_error_set(LM_ERR_MPTPSetsockopt);
|
||||
lm_mptp_close(sock);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int lm_mptp_socket(char *addr, uint16_t port, struct sockaddr *saddr) {
|
||||
if (NULL == addr || NULL == saddr) {
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
@ -71,7 +130,7 @@ int lm_mptp_socket(char *addr, uint16_t port, struct sockaddr *saddr) {
|
||||
|
||||
bzero(&hints, sizeof(hints));
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_DGRAM;
|
||||
hints.ai_socktype = AF_INET;
|
||||
|
||||
if ((status = getaddrinfo(addr, NULL, &hints, &res)) < 0) {
|
||||
lm_error_set(LM_ERR_MPTPHostFail);
|
||||
@ -108,7 +167,7 @@ int lm_mptp_socket(char *addr, uint16_t port, struct sockaddr *saddr) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((sock = socket(family, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
|
||||
if ((sock = socket(family, SOCK_STREAM, 0)) < 0) {
|
||||
lm_error_set(LM_ERR_MPTPSocketFail);
|
||||
return -1;
|
||||
}
|
||||
@ -127,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;
|
||||
}
|
||||
|
||||
@ -145,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;
|
||||
}
|
||||
|
||||
free(packet->data);
|
||||
packet->data = malloc(size);
|
||||
|
||||
if(NULL != data){
|
||||
packet->header.data_size = size;
|
||||
mempcpy(packet->data, data, size);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -168,8 +266,56 @@ 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) {
|
||||
lm_error_set(LM_ERR_MPTPTimeout);
|
||||
return false;
|
||||
}
|
||||
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.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);
|
||||
return false;
|
||||
}
|
||||
lm_error_set(LM_ERR_MPTPRecvFail, strerror(errno));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
return false;
|
||||
}
|
||||
lm_error_set(LM_ERR_MPTPRecvFail, strerror(errno));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -2,8 +2,10 @@
|
||||
#include "../../include/mptp.h"
|
||||
#include "../../include/util.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
int lm_mptp_server_listen(char *addr, uint16_t port) {
|
||||
struct sockaddr saddr;
|
||||
@ -16,28 +18,54 @@ int lm_mptp_server_listen(char *addr, uint16_t port) {
|
||||
|
||||
if (bind(sock, &saddr, sizeof(struct sockaddr)) < 0) {
|
||||
lm_mptp_close(sock);
|
||||
lm_error_set(LM_ERR_MPTPBindFail);
|
||||
lm_error_set(LM_ERR_MPTPBindFail, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(listen(sock, SOMAXCONN) < 0){
|
||||
lm_mptp_close(sock);
|
||||
lm_error_set(LM_ERR_MPTPListenFail, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
return sock;
|
||||
}
|
||||
|
||||
int lm_mptp_server_accept(int sock, struct sockaddr *addr){
|
||||
socklen_t sl = sizeof(struct sockaddr);
|
||||
int s = -1;
|
||||
|
||||
if((s = accept(sock, addr, &sl)) < 0){
|
||||
lm_error_set(LM_ERR_MPTPAcceptFail, strerror(errno));
|
||||
s = -1;
|
||||
}
|
||||
|
||||
if(!lm_mptp_socket_opts(s)){
|
||||
close(s);
|
||||
s = -1;
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
void lm_mptp_server_close(int sock){
|
||||
close(sock); // you didn't see that comming, did you?
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
@ -45,73 +73,76 @@ bool lm_mptp_server_verify(lm_mptp_t *packet) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool lm_mptp_server_recv(int sock, lm_mptp_t *packet, struct sockaddr *addr) {
|
||||
if (NULL == packet || NULL == addr) {
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
return false;
|
||||
}
|
||||
|
||||
char buffer[sizeof(packet->header) + MPTP_HOST_MAX + MPTP_DATA_MAX];
|
||||
socklen_t socklen = sizeof(struct sockaddr);
|
||||
ssize_t total = sizeof(buffer), used = 0;
|
||||
|
||||
bzero(buffer, sizeof(buffer));
|
||||
bzero(packet, sizeof(lm_mptp_t));
|
||||
|
||||
if (recvfrom(sock, buffer, sizeof(buffer), 0, addr, &socklen) <= 0) {
|
||||
lm_error_set(LM_ERR_MPTPRecvFail);
|
||||
return false;
|
||||
}
|
||||
|
||||
copy_from_buffer(&packet->header, buffer, sizeof(packet->header), &total, &used);
|
||||
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);
|
||||
|
||||
if (packet->header.host_size <= MPTP_HOST_MAX)
|
||||
copy_from_buffer(&packet->host, buffer, packet->header.host_size, &total, &used);
|
||||
|
||||
if (packet->header.data_size <= MPTP_DATA_MAX)
|
||||
copy_from_buffer(&packet->data, buffer, packet->header.data_size, &total, &used);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool lm_mptp_server_send(int sock, lm_mptp_t *packet, struct sockaddr *addr) {
|
||||
bool lm_mptp_server_recv(int sock, lm_mptp_t *packet) {
|
||||
if (NULL == packet) {
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
return false;
|
||||
}
|
||||
|
||||
lm_mptp_free(packet);
|
||||
|
||||
if(!lm_mptp_recv(sock, packet)){
|
||||
pdebug(__func__, "failed to receive the packet: %s", lm_strerror());
|
||||
return false; // error set by function
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
socklen_t addrlen = sizeof(struct sockaddr);
|
||||
char buffer[sizeof(packet->header) + packet->header.host_size + packet->header.data_size];
|
||||
ssize_t total = sizeof(buffer), used = 0;
|
||||
|
||||
packet->header.flags = htons(packet->header.flags);
|
||||
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;
|
||||
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);
|
||||
|
||||
if (sendto(sock, buffer, sizeof(buffer), 0, addr, addrlen) < 0) {
|
||||
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;
|
||||
}
|
||||
|
||||
return true;
|
||||
pdebug(__func__, "printing the packet dump (%lu bytes)", buflen);
|
||||
pdebug_binary(buffer, buflen);
|
||||
ret = true;
|
||||
|
||||
end:
|
||||
lm_mptp_free(packet);
|
||||
return ret;
|
||||
}
|
||||
|
@ -3,14 +3,15 @@
|
||||
#include "../../include/util.h"
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <errno.h>
|
||||
#include <error.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <error.h>
|
||||
|
||||
bool lm_mptp_sendfile(int sock, struct sockaddr *addr, char *path, lm_mptp_transfer_callback_t callback, void *data){
|
||||
if (NULL == path || NULL == addr){
|
||||
bool lm_mptp_sendfile(int sock, char *path, lm_mptp_transfer_callback_t callback, void *data){
|
||||
if (NULL == path){
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
return false;
|
||||
}
|
||||
@ -24,68 +25,81 @@ bool lm_mptp_sendfile(int sock, struct sockaddr *addr, char *path, lm_mptp_trans
|
||||
struct stat st;
|
||||
int size = -1;
|
||||
|
||||
lm_mptp_init(&packet);
|
||||
|
||||
if(NULL == file){
|
||||
pdebug(__func__, "failed to open file: %s", path);
|
||||
lm_error_set(LM_ERR_SendOpenFail);
|
||||
lm_mptp_init(&packet, false, MPTP_S2C_BRUH, true);
|
||||
goto end;
|
||||
goto end_1;
|
||||
}
|
||||
|
||||
if(fstat(fileno(file), &st)<0){
|
||||
pdebug(__func__, "failed to stat file: %s", path);
|
||||
lm_error_set(LM_ERR_SendStatFail);
|
||||
lm_mptp_init(&packet, false, MPTP_S2C_BRUH, true);
|
||||
goto end;
|
||||
goto end_1;
|
||||
}
|
||||
|
||||
total = st.st_size;
|
||||
|
||||
lm_mptp_init(&packet, false, MPTP_S2C_COOL, false);
|
||||
if((size = snprintf(packet.data, MPTP_DATA_MAX, "%lu", st.st_size)) <= 0){
|
||||
lm_mptp_new(&packet, false, MPTP_S2C_COOL, false);
|
||||
lm_mptp_set_data(&packet, NULL, digits(total));
|
||||
|
||||
if((size = snprintf(packet.data, MPTP_DATA_MAX, "%lu", total)) <= 0){
|
||||
pdebug(__func__, "snprintf for stat size failed: %s", path);
|
||||
lm_error_set(LM_ERR_SendSnprintfFail);
|
||||
lm_mptp_init(&packet, false, MPTP_S2C_BRUH, true);
|
||||
goto end;
|
||||
goto end_1;
|
||||
}
|
||||
|
||||
packet.header.data_size = size;
|
||||
lm_mptp_server_send(sock, &packet, addr);
|
||||
lm_mptp_server_send(sock, &packet);
|
||||
|
||||
if(NULL != callback)
|
||||
if(!callback(path, current, total, data))
|
||||
goto end;
|
||||
if(NULL != callback && !callback(path, current, total, data))
|
||||
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;
|
||||
if(!lm_mptp_server_send(sock, &packet, addr))
|
||||
goto end;
|
||||
pdebug(__func__, "sending the %lu/%lu of %s", current+read, total, path);
|
||||
|
||||
if(!lm_mptp_server_send(sock, &packet)){
|
||||
pdebug(__func__, "failed to send packet for %s (left at %lu/%lu): %s", path, current, total, lm_strerror());
|
||||
goto end_2;
|
||||
}
|
||||
|
||||
current += read;
|
||||
|
||||
if(NULL != callback)
|
||||
if(!callback(path, current, st.st_size, data))
|
||||
goto end;
|
||||
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);
|
||||
lm_mptp_set_data(&packet, NULL, MPTP_DATA_MAX);
|
||||
}
|
||||
|
||||
if(current != total){
|
||||
pdebug(__func__, "failed read the entire file (left at %lu/%lu): %s", current, total, path);
|
||||
lm_error_set(LM_ERR_SendReadFail);
|
||||
lm_mptp_init(&packet, false, MPTP_S2C_BRUH, true);
|
||||
goto end;
|
||||
goto end_1;
|
||||
}
|
||||
|
||||
lm_mptp_init(&packet, false, MPTP_S2C_COOL, true);
|
||||
pdebug(__func__, "completed sending %s, sending last packet", path);
|
||||
lm_mptp_new(&packet, false, MPTP_S2C_COOL, true);
|
||||
lm_mptp_server_send(sock, &packet);
|
||||
ret = true;
|
||||
goto end_2;
|
||||
|
||||
end:
|
||||
end_1:
|
||||
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);
|
||||
lm_mptp_server_send(sock, &packet, addr);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -105,6 +119,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);
|
||||
@ -133,18 +149,24 @@ bool lm_mptp_recvfile(int sock, char *path, lm_mptp_transfer_callback_t callback
|
||||
goto end;
|
||||
|
||||
while(lm_mptp_client_recv(sock, &packet)){
|
||||
if(!lm_mptp_client_verify(&packet))
|
||||
if(!lm_mptp_client_verify(&packet)){
|
||||
pdebug(__func__, "failed to verify the packet for %s (%lu/%lu)", path, current, total);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if(MPTP_FLAGS_CODE(&packet) != MPTP_S2C_COOL){
|
||||
pdebug(__func__, "server responded with bad status code for %s (%lu/%lu)", path, current, total);
|
||||
lm_error_set(LM_ERR_RecvBadCode);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if(MPTP_IS_LAST(&packet))
|
||||
if(MPTP_IS_LAST(&packet)){
|
||||
pdebug(__func__, "received the last packet for %s (%lu/%lu)", path, current, total);
|
||||
break;
|
||||
}
|
||||
|
||||
if(fwrite(packet.data, 1, packet.header.data_size, file)==0){
|
||||
pdebug(__func__, "failed to write received data for %s (%lu/%lu)", path, current, total);
|
||||
lm_error_set(LM_ERR_RecvWriteFail);
|
||||
goto end;
|
||||
}
|
||||
@ -157,6 +179,9 @@ bool lm_mptp_recvfile(int sock, char *path, lm_mptp_transfer_callback_t callback
|
||||
}
|
||||
|
||||
if(current != total){
|
||||
if(MPTP_IS_LAST(&packet))
|
||||
pdebug(__func__, "failed to receive the entire file (left at %lu/%lu), got the last packet: %s", current, total, path);
|
||||
else
|
||||
pdebug(__func__, "failed to receive the entire file (left at %lu/%lu): %s (%s)", current, total, path, lm_strerror());
|
||||
lm_error_set(LM_ERR_RecvNotCompleted);
|
||||
goto end;
|
||||
@ -164,6 +189,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;
|
||||
|
@ -42,7 +42,6 @@ lm_pkg_files_t *lm_package_extract(lm_pkg_t *pkg, char *target){
|
||||
if(!__lm_package_extract_check(files->data_file) ||
|
||||
!__lm_package_extract_check(files->hashes_file) ||
|
||||
!__lm_package_extract_check(files->changes_file) ||
|
||||
!__lm_package_extract_check(files->install_file) ||
|
||||
!__lm_package_extract_check(files->files_archive)){
|
||||
goto end;
|
||||
}
|
||||
|
@ -12,17 +12,26 @@ int lm_pool_info_handler(void *data, const char *_section, const char *_key, con
|
||||
char *section = (char *)_section, *value = (char *)_value, *key = (char *)_key;
|
||||
lm_pool_t *pool = data;
|
||||
|
||||
if (!eq(pool->name, section))
|
||||
if (!eq(pool->name, section)){
|
||||
pdebug(__func__, "pool name (%s) doesn't match: %s", pool->name, section);
|
||||
lm_error_set(LM_ERR_PoolInfoBadName, pool->name, section);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (eq(key, POOL_INFO_SIZE))
|
||||
pool->info.size = atol(value);
|
||||
|
||||
else if (eq(key, POOL_INFO_MAINTAINER))
|
||||
pool->info.maintainer = strdup(value);
|
||||
|
||||
else if (eq(key, POOL_INFO_PUBKEY))
|
||||
pool->info.pubkey = strdup(value);
|
||||
else
|
||||
|
||||
else{
|
||||
pdebug(__func__, "pool info contains unknown key: %s", key);
|
||||
lm_error_set(LM_ERR_PoolInfoUnknown, key);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
@ -45,7 +54,10 @@ bool lm_pool_info_load(lm_pool_t *pool) {
|
||||
return false;
|
||||
}
|
||||
|
||||
lm_error_clear();
|
||||
|
||||
if (ini_parse(pool->info_file, lm_pool_info_handler, pool) < 0) {
|
||||
if(lm_error() == LM_ERR_NoError)
|
||||
lm_error_set(LM_ERR_PoolInfoBad);
|
||||
return false;
|
||||
}
|
||||
@ -53,6 +65,7 @@ bool lm_pool_info_load(lm_pool_t *pool) {
|
||||
if(pool->info.size <= 0 ||
|
||||
pool->info.pubkey == NULL ||
|
||||
pool->info.maintainer == NULL){
|
||||
if(lm_error() == LM_ERR_NoError)
|
||||
lm_error_set(LM_ERR_PoolInfoBad);
|
||||
return false;
|
||||
}
|
||||
@ -93,18 +106,23 @@ 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))
|
||||
if(!lm_mptp_client_send(sock, &packet)){
|
||||
pdebug(__func__, "info file request failed for %s: %s", pool->name, lm_strerror());
|
||||
goto end;
|
||||
}
|
||||
|
||||
if(!lm_mptp_recvfile(sock, pool->info_file, callback, data))
|
||||
if(!lm_mptp_recvfile(sock, pool->info_file, callback, data)){
|
||||
pdebug(__func__, "recvfile failed for %s: %s", pool->name, lm_strerror());
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = true;
|
||||
end:
|
||||
lm_mptp_free(&packet);
|
||||
lm_mptp_close(sock);
|
||||
if(ret)
|
||||
ret = lm_pool_info_load(pool);
|
||||
|
@ -119,18 +119,23 @@ 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))
|
||||
if(!lm_mptp_client_send(sock, &packet)){
|
||||
pdebug(__func__, "list file request failed for %s: %s", pool->name, lm_strerror());
|
||||
goto end;
|
||||
}
|
||||
|
||||
if(!lm_mptp_recvfile(sock, pool->list_file, callback, data))
|
||||
if(!lm_mptp_recvfile(sock, pool->list_file, callback, data)){
|
||||
pdebug(__func__, "recvfile failed for %s: %s", pool->name, lm_strerror());
|
||||
goto end;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
35
src/util.c
35
src/util.c
@ -29,6 +29,19 @@ void pdebug(const char *func, const char *fmt, ...) {
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void pdebug_binary(char *data, size_t len) {
|
||||
if (LM_DEBUG != 1)
|
||||
return;
|
||||
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
if (i != 0 && i % 4 == 0)
|
||||
printf("\n");
|
||||
printf("%c%c%c%c%c%c%c%c ", BINARY(data[i]));
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
bool eq(char *s1, char *s2) {
|
||||
if (NULL == s1 || NULL == s2)
|
||||
return false;
|
||||
@ -95,7 +108,9 @@ bool copy_to_buffer(void *buffer, void *src, size_t size, ssize_t *total, ssize_
|
||||
if (*used == 0)
|
||||
bzero(buffer, *total);
|
||||
|
||||
if (NULL != buffer && NULL != src && size > 0)
|
||||
memcpy(buffer + *used, src, size);
|
||||
|
||||
*used += size;
|
||||
return true;
|
||||
}
|
||||
@ -107,7 +122,9 @@ bool copy_from_buffer(void *dst, void *buffer, size_t size, ssize_t *total, ssiz
|
||||
if (*used == 0)
|
||||
bzero(dst, size);
|
||||
|
||||
if (NULL != buffer && NULL != dst && size > 0)
|
||||
memcpy(dst, buffer + *used, size);
|
||||
|
||||
*used += size;
|
||||
return true;
|
||||
}
|
||||
@ -154,7 +171,10 @@ bool extract_archive(char *dst, char *src) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
chdir(dst);
|
||||
if (chdir(dst) < 0) {
|
||||
lm_error_set(LM_ERR_ChdirFail, dst);
|
||||
goto end;
|
||||
}
|
||||
|
||||
flags = ARCHIVE_EXTRACT_PERM;
|
||||
flags |= ARCHIVE_EXTRACT_UNLINK;
|
||||
@ -214,7 +234,10 @@ end:
|
||||
}
|
||||
|
||||
if (NULL != oldpwd) {
|
||||
chdir(oldpwd);
|
||||
if (chdir(oldpwd) < 0) {
|
||||
lm_error_set(LM_ERR_ChdirFail, oldpwd);
|
||||
ret = false;
|
||||
}
|
||||
free(oldpwd);
|
||||
}
|
||||
|
||||
@ -563,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