libmp/include/mptp.h

167 lines
5.5 KiB
C
Raw Permalink Normal View History

2024-06-20 22:36:56 +00:00
#pragma once
2024-06-22 01:18:00 +00:00
#include <netdb.h>
2024-07-31 18:16:19 +00:00
#include <stdbool.h>
2024-06-21 01:42:51 +00:00
#include <stddef.h>
#include <stdint.h>
2024-07-31 18:16:19 +00:00
#include <sys/types.h>
// clang-format off
/*
2024-06-22 01:18:00 +00:00
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#####################################
# FLAGS #
#####################################
# HOST SIZE | PATH SIZE #
#####################################
# DATA SIZE #
2024-06-22 01:18:00 +00:00
#####################################
# HOST #
# PATH #
2024-06-22 01:18:00 +00:00
# DATA #
#...................................#
[16 bits] FLAGS
=========================================================
| 8 bits used for specifying the version
2024-06-20 22:36:56 +00:00
---------------------------------------------------------
2024-06-22 01:18:00 +00:00
| 1 bit used to specify if this is a request or response
|
| 0- it's a request
| 1- it's a response
2024-06-20 22:36:56 +00:00
---------------------------------------------------------
2024-06-22 01:18:00 +00:00
| 4 bits used for request/response codes
|
| We have 4 types of different requests
| 0- PING: checks if the server is avaliable
| 1- INFO: gets information about the pool(s) the server is hosting
| 2- LIST: requests the package list for certain pool
| 3- PULL: requests a certain package from a certain pool
|
| And 4 types of responses
| 0- PONG: response for the ping request
| 1- COOL: everything is fine, here's the data you requested
| 2- BRUH: cannot access to the data you requested, or version mismatch
| 3- WHAT: bad request
2024-06-20 22:36:56 +00:00
---------------------------------------------------------
2024-06-22 01:18:00 +00:00
| 1 bit used to specify if this the last response
|
| 0- no stay connected, im not done responsing yet
| 1- yes, im done, disconnect
|
| always 1 for the requests
2024-06-20 22:36:56 +00:00
---------------------------------------------------------
2024-06-22 01:18:00 +00:00
| 2 bits is reserved for future use
2024-06-21 01:42:51 +00:00
---------------------------------------------------------
2024-06-20 22:36:56 +00:00
[8 bits] HOST SIZE
2024-06-22 01:18:00 +00:00
=========================================================
2024-06-25 18:21:15 +00:00
| All bits used for specifying HOST size, always zero
| for responses
2024-06-20 22:36:56 +00:00
---------------------------------------------------------
2024-06-22 01:18:00 +00:00
[8 bits] DATA SIZE
=========================================================
| 8 bits used for specifying DATA size
---------------------------------------------------------
[...] HOST
---------------------------------------------------------
| Plaintext server hostname, max size is 255 octets
2024-06-25 18:21:15 +00:00
| see the SIZE section, always empty for responses
---------------------------------------------------------
2024-06-20 22:36:56 +00:00
[...] DATA
---------------------------------------------------------
2024-06-22 01:18:00 +00:00
| Plaintext data used for that specific request/response,
| max size is 255 octets, see the SIZE section
2024-06-20 22:36:56 +00:00
---------------------------------------------------------
*/
// clang-format on
2024-06-20 22:36:56 +00:00
#define MPTP_VERSION_SUPPORTED 0
#define MPTP_CODE_MAX 3
#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
2024-06-21 01:42:51 +00:00
#define MPTP_TIMEOUT 10
#define MPTP_REQUEST 0
#define MPTP_RESPONSE 1
2024-06-20 22:36:56 +00:00
typedef enum lm_mptp_request {
2024-06-20 22:36:56 +00:00
MPTP_C2S_PING = 0,
MPTP_C2S_INFO = 1,
MPTP_C2S_LIST = 2,
MPTP_C2S_PULL = 3,
} lm_mptp_request_t;
typedef enum lm_mptp_response {
2024-06-20 22:36:56 +00:00
MPTP_S2C_PONG = 0,
MPTP_S2C_COOL = 1,
MPTP_S2C_BRUH = 2,
MPTP_S2C_WHAT = 3,
} lm_mptp_response_t;
2024-06-22 01:18:00 +00:00
typedef struct lm_mptp_header {
uint16_t flags;
uint8_t host_size;
uint8_t path_size;
uint16_t data_size;
2024-08-07 23:30:51 +00:00
} __attribute__((packed)) lm_mptp_header_t;
2024-06-22 01:18:00 +00:00
typedef struct lm_mptp {
lm_mptp_header_t header;
char *host, *path, *data;
2024-06-20 22:36:56 +00:00
} lm_mptp_t;
typedef bool (*lm_mptp_transfer_callback_t)(char *path, size_t current, size_t total, void *data);
2024-08-07 23:30:51 +00:00
#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)
2024-06-21 01:42:51 +00:00
2024-06-22 04:03:17 +00:00
#define MPTP_IS_REQUEST(m) (MPTP_FLAGS_TYPE(m) == 0)
#define MPTP_IS_LAST(m) (MPTP_FLAGS_LAST(m) == 1)
2024-06-21 01:42:51 +00:00
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);
2024-06-22 01:18:00 +00:00
int lm_mptp_socket(char *addr, uint16_t port, struct sockaddr *saddr);
2024-08-07 23:30:51 +00:00
bool lm_mptp_socket_opts(int sock);
2024-06-25 18:21:15 +00:00
void lm_mptp_copy(lm_mptp_t *dst, lm_mptp_t *src);
2024-06-22 04:03:17 +00:00
bool lm_mptp_verify(lm_mptp_t *packet);
2024-06-22 01:18:00 +00:00
void lm_mptp_close(int sock);
2024-08-08 12:38:26 +00:00
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);
2024-06-21 01:42:51 +00:00
2024-06-22 01:18:00 +00:00
int lm_mptp_client_connect(char *addr, uint16_t port);
2024-06-22 04:03:17 +00:00
bool lm_mptp_client_verify(lm_mptp_t *packet);
2024-06-22 01:18:00 +00:00
bool lm_mptp_client_send(int sock, lm_mptp_t *packet);
bool lm_mptp_client_recv(int sock, lm_mptp_t *packet);
2024-06-20 22:36:56 +00:00
2024-06-22 01:18:00 +00:00
int lm_mptp_server_listen(char *addr, uint16_t port);
2024-08-07 01:49:33 +00:00
int lm_mptp_server_accept(int sock, struct sockaddr *addr);
void lm_mptp_server_close(int sock);
2024-06-22 04:03:17 +00:00
bool lm_mptp_server_verify(lm_mptp_t *packet);
2024-08-07 01:49:33 +00:00
bool lm_mptp_server_recv(int sock, lm_mptp_t *packet);
bool lm_mptp_server_send(int sock, lm_mptp_t *packet);
2024-08-07 01:49:33 +00:00
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);