setup mptp headers and types

This commit is contained in:
ngn 2024-06-21 01:36:56 +03:00
parent 637b8c02e6
commit 82cbd147b2
15 changed files with 266 additions and 151 deletions

View File

@ -1,4 +1,4 @@
#include "types.h"
#include "pool.h"
#include "error.h" #include "error.h"
#include "libmp.h" #include "libmp.h"
#include "pool.h"
#include "types.h"

93
include/mptp.h Normal file
View File

@ -0,0 +1,93 @@
#pragma once
#include <stdint.h>
#include "types.h"
/*
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#####################################
# FLAGS #
#####################################
# SIZE #
# #
#####################################
# DATA #
#...................................#
[8 bits] FLAGS
---------------------------------------------------------
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
---------------------------------------------------------
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 request/response
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
---------------------------------------------------------
*/
#define MPTP_VERSION_SUPPORTED 0
#define MPTP_VERSION_MAX 15 // 4 bits
#define MPTP_CODE_MAX 4 // 2 bits
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 {
uint8_t flags;
uint16_t size;
char *data;
} lm_mptp_t;
void lm_mptp_flags_set(lm_mptp_t *packet, uint8_t version, 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 socket, lm_mptp_t *packet);
bool lm_mptp_send(int socket, lm_mptp_t *packet);
void lm_mptp_disconnect(int socket);

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include <stdbool.h>
#include "types.h" #include "types.h"
#include <stdbool.h>
bool lm_ctx_pool_add(lm_ctx_t *ctx, char *name, char *url); bool lm_ctx_pool_add(lm_ctx_t *ctx, char *name, char *url);
bool lm_ctx_pool_del(lm_ctx_t *ctx, char *name); bool lm_ctx_pool_del(lm_ctx_t *ctx, char *name);

View File

@ -17,4 +17,3 @@ typedef struct lm_ctx {
char *data; // package database path char *data; // package database path
bool debug; // is debug output enabled? bool debug; // is debug output enabled?
} lm_ctx_t; } lm_ctx_t;

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#include <stdbool.h>
#include <libintl.h>
#include "types.h" #include "types.h"
#include <libintl.h>
#include <stdbool.h>
#define _(x) gettext(x) #define _(x) gettext(x)

View File

@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: PACKAGE VERSION\n" "Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-06-20 03:33+0300\n" "POT-Creation-Date: 2024-06-20 20:00+0300\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
@ -19,7 +19,7 @@ msgstr ""
#: src/error.c:13 #: src/error.c:13
msgid "no error" msgid "no error"
msgstr "no error" msgstr "hata yok"
#: src/error.c:14 #: src/error.c:14
#, fuzzy #, fuzzy
@ -60,7 +60,7 @@ msgstr "URL protocol port number is unknown"
#: src/error.c:23 #: src/error.c:23
msgid "URL is incomplete" msgid "URL is incomplete"
msgstr "URL is incomplete" msgstr "URL tamamlanmamış"
#: src/error.c:24 #: src/error.c:24
msgid "pool does not support the specified protocol" msgid "pool does not support the specified protocol"

View File

@ -24,8 +24,8 @@
#include "../include/libmp.h" #include "../include/libmp.h"
#include "../include/pool.h" #include "../include/pool.h"
#include <locale.h>
#include <libintl.h> #include <libintl.h>
#include <locale.h>
#include <stdlib.h> #include <stdlib.h>
void lm_ctx_init(lm_ctx_t *ctx) { void lm_ctx_init(lm_ctx_t *ctx) {

View File

@ -1,6 +1,6 @@
#include "../include/pool.h"
#include "../include/error.h" #include "../include/error.h"
#include "../include/util.h" #include "../include/util.h"
#include "../include/pool.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -32,7 +32,7 @@ bool lm_ctx_pool_add(lm_ctx_t *ctx, char *name, char *url){
return false; return false;
pdebug(ctx, __func__, "pool name is %s", pool->name); pdebug(ctx, __func__, "pool name is %s", pool->name);
pdebug(ctx, __func__, "pool URL is %s://%s%s", pool->url.protocol, pool->url.host, pool->url.path); pdebug(ctx, __func__, "pool URL is %s://%s:%d%s", pool->url.protocol, pool->url.host, pool->url.port, pool->url.path);
if (NULL == ctx->pools) { if (NULL == ctx->pools) {
ctx->pools = pool; ctx->pools = pool;

0
src/protocol.c Normal file
View File

View File

@ -1,10 +1,9 @@
#include "../include/url.h"
#include "../include/error.h" #include "../include/error.h"
#include "../include/util.h" #include "../include/util.h"
#include "../include/url.h"
#include <assert.h> #include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
char valid_path[] = "-._~:/?#[]@!$&'()*+,;%="; char valid_path[] = "-._~:/?#[]@!$&'()*+,;%=";
@ -47,7 +46,7 @@ bool lm_url_parse(lm_url_t *url, char *str){
} }
lm_state_t state = URL_PROTOCOL_0; lm_state_t state = URL_PROTOCOL_0;
char buffer[strl+1]; // temporary buffer char buffer[strl + 1], *save; // temporary buffer, strok_r save pointer
bool ret = false; // return value bool ret = false; // return value
// clear out the temporary buffer // clear out the temporary buffer
@ -105,7 +104,8 @@ bool lm_url_parse(lm_url_t *url, char *str){
goto end; goto end;
} }
if (!is_letter(buffer[index]) && !is_digit(buffer[index]) && buffer[index] != '.' && buffer[index] != ':' && buffer[index] != '/'){ if (!is_letter(buffer[index]) && !is_digit(buffer[index]) && buffer[index] != '.' && buffer[index] != ':' &&
buffer[index] != '/') {
lm_error_set(LM_ERR_URLBadChar); lm_error_set(LM_ERR_URLBadChar);
goto end; goto end;
} }
@ -182,6 +182,29 @@ bool lm_url_parse(lm_url_t *url, char *str){
goto end; goto end;
} }
if (strtok_r(url->host, ":", &save) == NULL) {
lm_error_set(LM_ERR_URLBadHost);
goto end;
}
char *portc = strtok_r(NULL, ":", &save);
if (NULL == portc) {
url->port = lm_url_default(url->protocol);
if (url->port == 0) {
lm_error_set(LM_ERR_URLPortUnknown);
goto end;
}
ret = true;
goto end;
}
int port = atoi(portc);
if (port <= 0 || port > UINT16_MAX) {
lm_error_set(LM_ERR_URLBadPort);
goto end;
}
url->port = port;
ret = true; ret = true;
end: end:

View File

@ -1,9 +1,9 @@
#include "../include/util.h" #include "../include/util.h"
#include "../include/types.h" #include "../include/types.h"
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdio.h>
void pdebug(lm_ctx_t *ctx, const char *func, const char *fmt, ...) { void pdebug(lm_ctx_t *ctx, const char *func, const char *fmt, ...) {
if (!ctx->debug) if (!ctx->debug)