new: finishing up base mptp functions

This commit is contained in:
ngn
2024-06-22 04:18:00 +03:00
parent cb66106e50
commit 19c98b763d
8 changed files with 322 additions and 160 deletions

View File

@ -21,9 +21,11 @@ typedef enum lm_error {
LM_ERR_MPTPConnectFail = 17,
LM_ERR_MPTPRecvFail = 18,
LM_ERR_MPTPSendFail = 19,
LM_ERR_MPTPChunkFail = 29,
LM_ERR_MPTPBadChunk = 29,
LM_ERR_MPTPSetsockopt = 30,
LM_ERR_MPTPTimeout = 31,
LM_ERR_MPTPBindFail = 32,
LM_ERR_ArgNULL = 33,
} lm_error_t;
typedef struct lm_error_desc {

View File

@ -1,5 +1,6 @@
#pragma once
#include "types.h"
#include <netdb.h>
#include <stddef.h>
#include <stdint.h>
@ -7,56 +8,60 @@
/*
0 1 2 3 4 5 6 7
###############
# FLAGS #
###############
# SIZE #
# #
###############
# DATA #
#.............#
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#####################################
# FLAGS #
#####################################
# SIZE #
#####################################
# DATA #
#...................................#
[8 bits] FLAGS
[16 bits] FLAGS
=========================================================
| 8 bits used for specifying the version
---------------------------------------------------------
4 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
---------------------------------------------------------
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
---------------------------------------------------------
2 bits used for request/response codes
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
3- LIST: requests the package list for certain pool
4- PULL: requests a certain package from a certain pool
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 response
|
| 0- no stay connected, im not done responsing yet
| 1- yes, im done, disconnect
|
| always 1 for the requests
---------------------------------------------------------
1 bit used to specify if this the last request/response
| 2 bits is reserved for future use
---------------------------------------------------------
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
---------------------------------------------------------
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
| Plaintext data used for that specific request/response,
| max value for SIZE is 65535, so this section may have
| 65535 bytes total
---------------------------------------------------------
*/
@ -66,7 +71,7 @@
#define MPTP_VERSION_SUPPORTED 0
#define MPTP_VERSION_MAX 15 // 4 bits
#define MPTP_CODE_MAX 4 // 2 bits
#define MPTP_CHUNK_MAX 4096
#define MPTP_CHUNK_MAX 256
#define MPTP_TIMEOUT 10
#define MPTP_REQUEST 0
@ -86,24 +91,34 @@ typedef enum lm_mptp_response {
MPTP_S2C_WHAT = 3,
} lm_mptp_response_t;
typedef struct lm_mptp {
uint8_t flags;
typedef struct lm_mptp_header {
uint16_t flags;
uint16_t size;
char *data;
} lm_mptp_header_t;
typedef struct lm_mptp {
lm_mptp_header_t header;
char data[MPTP_CHUNK_MAX];
} lm_mptp_t;
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)
#define MPTP_FLAGS_VERSION(m) ((m)->header.flags & 0xFF00) >> 8
#define MPTP_FLAGS_REQUEST(m) ((m)->header.flags & 0x0080) >> 1
#define MPTP_FLAGS_TYPE(m) ((m)->header.flags & 0x0070) >> 4
#define MPTP_FLAGS_LAST(m) ((m)->header.flags & 0x0008) >> 3
int lm_mptp_connect(lm_url_t *url);
#define MPTP_IS_REQUEST(m) MPTP_FLAGS_REQUEST(m) == 0
#define MPTP_IS_LAST(m) MPTP_FLAGS_LAST(m) == 1
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_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);
void lm_mptp_close(int sock);
bool lm_mptp_send(int sock, lm_mptp_t *packet);
bool lm_mptp_send_data(int sock, char *data, size_t size);
int lm_mptp_client_connect(char *addr, uint16_t port);
bool lm_mptp_client_verify(int sock, lm_mptp_t *packet); // not implemented
bool lm_mptp_client_send(int sock, lm_mptp_t *packet);
bool lm_mptp_client_recv(int sock, lm_mptp_t *packet);
void lm_mptp_disconnect(int sock);
int lm_mptp_server_listen(char *addr, uint16_t port);
bool lm_mptp_server_verify(int sock, lm_mptp_t *packet, struct sockaddr *addr); // not implemented
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);