libmp/include/mptp.h

167 lines
5.5 KiB
C

#pragma once
#include <netdb.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <sys/types.h>
// clang-format off
/*
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#####################################
# FLAGS #
#####################################
# HOST SIZE | PATH SIZE #
#####################################
# DATA SIZE #
#####################################
# HOST #
# PATH #
# DATA #
#...................................#
[16 bits] FLAGS
=========================================================
| 8 bits used for specifying the version
---------------------------------------------------------
| 1 bit used to specify if this is a request or response
|
| 0- it's a request
| 1- it's a response
---------------------------------------------------------
| 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
---------------------------------------------------------
| 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
---------------------------------------------------------
| 2 bits is reserved for future use
---------------------------------------------------------
[8 bits] HOST SIZE
=========================================================
| All bits used for specifying HOST size, always zero
| for responses
---------------------------------------------------------
[8 bits] DATA SIZE
=========================================================
| 8 bits used for specifying DATA size
---------------------------------------------------------
[...] HOST
---------------------------------------------------------
| Plaintext server hostname, max size is 255 octets
| see the SIZE section, always empty for responses
---------------------------------------------------------
[...] DATA
---------------------------------------------------------
| Plaintext data used for that specific request/response,
| max size is 255 octets, see the SIZE section
---------------------------------------------------------
*/
// clang-format on
#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
#define MPTP_TIMEOUT 10
#define MPTP_REQUEST 0
#define MPTP_RESPONSE 1
typedef enum lm_mptp_request {
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 {
MPTP_S2C_PONG = 0,
MPTP_S2C_COOL = 1,
MPTP_S2C_BRUH = 2,
MPTP_S2C_WHAT = 3,
} lm_mptp_response_t;
typedef struct lm_mptp_header {
uint16_t flags;
uint8_t host_size;
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, *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 >> 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)
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);
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);
bool lm_mptp_server_send(int sock, lm_mptp_t *packet);
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);