libmp/include/mptp.h

110 lines
3.0 KiB
C
Raw Normal View History

2024-06-20 22:36:56 +00:00
#pragma once
#include "types.h"
2024-06-21 01:42:51 +00:00
#include <stddef.h>
#include <stdint.h>
// clang-format off
/*
0 1 2 3 4 5 6 7
###############
# FLAGS #
###############
# SIZE #
# #
###############
# DATA #
#.............#
2024-06-20 22:36:56 +00:00
[8 bits] FLAGS
---------------------------------------------------------
4 bits used for specifying the version
---------------------------------------------------------
1 bit used to specify if this is a request or response
2024-06-20 22:36:56 +00:00
0- it's a request
1- it's a response
---------------------------------------------------------
2 bits used for request/response codes
2024-06-20 22:36:56 +00:00
We have 4 types of different requests
1- PING: checks if the server is avaliable
2- INFO: gets information about the pool(s) the server is hosting
2024-06-20 22:36:56 +00:00
3- LIST: requests the package list for certain pool
4- PULL: requests a certain package from a certain pool
2024-06-20 22:36:56 +00:00
And 4 types of responses
1- PONG: response for the ping request
2- COOL: everything is fine, here's the data you requested
3- BRUH: cannot access to the data you requested, or version mismatch
4- WHAT: bad request
---------------------------------------------------------
1 bit used to specify if this the last request/response
2024-06-21 01:42:51 +00:00
---------------------------------------------------------
2024-06-20 22:36:56 +00:00
0- no stay connected, im not done responsing/requesting yet
1- yes, im done, disconnect
---------------------------------------------------------
[16 bits] SIZE
---------------------------------------------------------
All bits used for specifying the DATA size, in bytes
---------------------------------------------------------
[...] DATA
---------------------------------------------------------
Plaintext data used for that specific request/response,
max value for SIZE is 65535, so this section may have
65535 bytes total
---------------------------------------------------------
*/
// clang-format on
2024-06-20 22:36:56 +00:00
#define MPTP_VERSION_SUPPORTED 0
#define MPTP_VERSION_MAX 15 // 4 bits
#define MPTP_CODE_MAX 4 // 2 bits
2024-06-21 01:42:51 +00:00
#define MPTP_CHUNK_MAX 4096
#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;
typedef struct lm_mptp {
uint8_t flags;
2024-06-20 22:36:56 +00:00
uint16_t size;
char *data;
2024-06-20 22:36:56 +00:00
} lm_mptp_t;
2024-06-21 01:42:51 +00:00
bool lm_mptp_init(lm_mptp_t *packet, bool is_request, uint8_t code, bool is_last);
#define MPTP_FLAGS_VERSION(m) ((m->flags >> 4) & 15)
#define MPTP_FLAGS_IS_REQUEST(m) (((m->flags >> 3) & 1) == 0)
#define MPTP_FLAGS_TYPE(m) ((m->flags >> 1) & 3)
#define MPTP_FLAGS_IS_LAST(m) ((m->flags & 1) == 1)
int lm_mptp_connect(lm_url_t *url);
bool lm_mptp_recv(int sock, lm_mptp_t *packet);
bool lm_mptp_recv_data(int sock, char *data, size_t size);
bool lm_mptp_send(int sock, lm_mptp_t *packet);
bool lm_mptp_send_data(int sock, char *data, size_t size);
2024-06-20 22:36:56 +00:00
2024-06-21 01:42:51 +00:00
void lm_mptp_disconnect(int sock);