#pragma once #include "types.h" #include #include #include // clang-format off /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ##################################### # FLAGS # ##################################### # SIZE # ##################################### # 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 --------------------------------------------------------- [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 #define MPTP_VERSION_SUPPORTED 0 #define MPTP_CODE_MAX 3 // 2 bits #define MPTP_CHUNK_MAX 256 #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; uint16_t size; } lm_mptp_header_t; typedef struct lm_mptp { lm_mptp_header_t header; char data[MPTP_CHUNK_MAX]; } lm_mptp_t; #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_IS_REQUEST(m) (MPTP_FLAGS_TYPE(m) == 0) #define MPTP_IS_LAST(m) (MPTP_FLAGS_LAST(m) == 1) bool lm_mptp_packet_init(lm_mptp_t *packet, bool is_request, uint8_t code, bool is_last); int lm_mptp_socket(char *addr, uint16_t port, struct sockaddr *saddr); bool lm_mptp_verify(lm_mptp_t *packet); void lm_mptp_close(int sock); 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); 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 *adrr);