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

@ -1,7 +1,11 @@
CC = gcc
all: ../dist/example_pool
all: ../dist/example_pool ../dist/example_server
../dist/example_pool: pool/*.c ../dist/libmp.so
mkdir -pv ../dist
$(CC) -L../dist $< -lmp -o $@
$(CC) -L../dist $< -lmp -o $@
../dist/example_server: server/*.c ../dist/libmp.so
mkdir -pv ../dist
$(CC) -L../dist $< -lmp -o $@

52
examples/server/main.c Normal file
View File

@ -0,0 +1,52 @@
#include "../../include/error.h"
#include "../../include/mptp.h"
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
int main(int argc, char *argv[]) {
int ret = EXIT_FAILURE;
if (argc != 3) {
printf("usage: %s <host> <port>\n", argv[0]);
return ret;
}
int porti = atoi(argv[2]);
if (porti <= 0 || porti > UINT16_MAX) {
printf("bad port number\n");
return ret;
}
int sock = lm_mptp_server_listen(argv[1], porti);
if (sock < 0) {
printf("failed to start the server: %s\n", lm_strerror());
return ret;
}
lm_mptp_t packet;
struct sockaddr addr;
while (lm_mptp_server_recv(sock, &packet, &addr)) {
switch (MPTP_FLAGS_TYPE(&packet)) {
case MPTP_C2S_PING:
bzero(&packet, sizeof(packet));
lm_mptp_packet_init(&packet, false, MPTP_S2C_PONG, true);
lm_mptp_server_send(sock, &packet, &addr);
break;
default:
bzero(&packet, sizeof(packet));
lm_mptp_packet_init(&packet, false, MPTP_S2C_WHAT, true);
lm_mptp_server_send(sock, &packet, &addr);
break;
}
}
ret = EXIT_SUCCESS;
end:
lm_mptp_close(sock);
return ret;
}

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);

View File

@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-06-21 04:42+0300\n"
"POT-Creation-Date: 2024-06-22 04:17+0300\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@ -79,7 +79,7 @@ msgid "invalid MPTP URL"
msgstr ""
#: src/error.c:28
msgid "failed to resolve hostname for MPTP communication"
msgid "failed to resolve hostname for MPTP connection"
msgstr ""
#: src/error.c:29
@ -110,3 +110,11 @@ msgstr ""
#: src/error.c:35
msgid "MPTP connection timed out"
msgstr ""
#: src/error.c:36
msgid "failed to bind MPTP socket"
msgstr ""
#: src/error.c:37
msgid "required argument is a NULL pointer"
msgstr ""

View File

@ -25,14 +25,16 @@ char *lm_strerror() {
{.code = LM_ERR_MPTPBadVersion, .desc = _("unsupported MPTP version") },
{.code = LM_ERR_MPTPBadCode, .desc = _("invalid MPTP request/response code") },
{.code = LM_ERR_MPTPBadUrl, .desc = _("invalid MPTP URL") },
{.code = LM_ERR_MPTPHostFail, .desc = _("failed to resolve hostname for MPTP communication") },
{.code = LM_ERR_MPTPHostFail, .desc = _("failed to resolve hostname for MPTP connection") },
{.code = LM_ERR_MPTPSocketFail, .desc = _("failed to create a MPTP socket") },
{.code = LM_ERR_MPTPConnectFail, .desc = _("failed to connect to the MPTP host") },
{.code = LM_ERR_MPTPRecvFail, .desc = _("failed receive MPTP data from host") },
{.code = LM_ERR_MPTPSendFail, .desc = _("failed send MPTP data to host") },
{.code = LM_ERR_MPTPChunkFail, .desc = _("MPTP data chunk is too large") },
{.code = LM_ERR_MPTPBadChunk, .desc = _("MPTP data chunk is too large") },
{.code = LM_ERR_MPTPSetsockopt, .desc = _("failed to set MPTP socket options") },
{.code = LM_ERR_MPTPTimeout, .desc = _("MPTP connection timed out") },
{.code = LM_ERR_MPTPBindFail, .desc = _("failed to bind MPTP socket") },
{.code = LM_ERR_ArgNULL, .desc = _("required argument is a NULL pointer") },
};
for (int i = 0; i < sizeof(errors) / sizeof(lm_error_desc_t); i++) {

View File

@ -1,78 +1,113 @@
#include "../include/mptp.h"
#include "../include/error.h"
#include "../include/url.h"
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/socket.h>
#include <unistd.h>
bool lm_mptp_init(lm_mptp_t *packet, bool is_request, uint8_t code, bool is_last) {
packet->data = NULL;
packet->flags = 0;
packet->size = 0;
bool lm_mptp_packet_init(lm_mptp_t *packet, bool is_request, uint8_t code, bool is_last) {
packet->header.flags = 0;
packet->header.size = 0;
bzero(packet->data, MPTP_CHUNK_MAX);
if (code > MPTP_CODE_MAX) {
lm_error_set(LM_ERR_MPTPBadCode);
return false;
}
// X-X-X-X-0-0-0-0
packet->flags |= (MPTP_VERSION_SUPPORTED << 4);
packet->header.flags |= (MPTP_VERSION_SUPPORTED << 8);
// 0-0-0-0-X-0-0-0
if (is_request)
packet->flags |= (MPTP_REQUEST << 3);
packet->header.flags |= (MPTP_REQUEST << 7);
else
packet->flags |= (MPTP_RESPONSE << 3);
packet->header.flags |= (MPTP_REQUEST << 7);
// 0-0-0-0-0-X-X-0
packet->flags |= (code << 1);
packet->header.flags |= (code << 4);
// 0-0-0-0-0-0-0-X
if (is_last)
packet->flags |= 1;
if (is_last || is_request)
packet->header.flags |= (1 << 3);
else
packet->flags |= 0;
packet->header.flags |= (0 << 3);
return true;
}
int lm_mptp_connect(lm_url_t *url) {
if (NULL == url || NULL == url->host) {
lm_error_set(LM_ERR_MPTPBadUrl);
int lm_mptp_socket(char *addr, uint16_t port, struct sockaddr *saddr) {
if (NULL == addr || NULL == saddr) {
lm_error_set(LM_ERR_ArgNULL);
return -1;
}
int sock;
struct sockaddr_in addr;
struct hostent *ent = gethostbyname(url->host);
if (NULL == ent) {
struct addrinfo hints, *res, *cur;
int sock = 0, status = 0, family = -1;
bzero(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
if ((status = getaddrinfo(addr, NULL, &hints, &res)) < 0) {
lm_error_set(LM_ERR_MPTPHostFail);
return -1;
}
if (ent->h_addrtype != AF_INET && ent->h_addrtype != AF_INET6) {
for (cur = res; cur != NULL; cur = cur->ai_next) {
switch (cur->ai_family) {
case AF_INET:
family = cur->ai_family;
struct sockaddr_in *ipv4 = (struct sockaddr_in *)cur->ai_addr;
ipv4->sin_port = htons(port);
memcpy(saddr, cur->ai_addr, sizeof(struct sockaddr));
break;
case AF_INET6:
family = cur->ai_family;
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)cur->ai_addr;
ipv6->sin6_port = htons(port);
memcpy(saddr, cur->ai_addr, sizeof(struct sockaddr));
break;
}
if (family != -1)
break;
}
freeaddrinfo(res);
if (family == -1) {
lm_error_set(LM_ERR_MPTPHostFail);
return -1;
}
if (NULL == ent->h_addr_list[0]) {
lm_error_set(LM_ERR_MPTPHostFail);
return -1;
}
addr.sin_addr.s_addr = *(long *)(ent->h_addr_list[0]);
addr.sin_family = ent->h_addrtype;
addr.sin_port = htons(url->port);
if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
if ((sock = socket(family, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
lm_error_set(LM_ERR_MPTPSocketFail);
return -1;
}
return sock;
}
void lm_mptp_close(int sock) {
close(sock);
}
int lm_mptp_client_connect(char *addr, uint16_t port) {
struct sockaddr saddr;
int sock;
bzero(&saddr, sizeof(saddr));
if ((sock = lm_mptp_socket(addr, port, &saddr)) < 0)
return -1;
struct timeval timeout;
bzero(&timeout, sizeof(timeout));
@ -80,13 +115,13 @@ int lm_mptp_connect(lm_url_t *url) {
timeout.tv_usec = 0;
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0) {
lm_mptp_disconnect(sock);
lm_error_set(LM_ERR_MPTPSetsockopt);
lm_mptp_close(sock);
return -1;
}
if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
lm_mptp_disconnect(sock);
if (connect(sock, &saddr, sizeof(saddr)) < 0) {
lm_mptp_close(sock);
lm_error_set(LM_ERR_MPTPConnectFail);
return -1;
}
@ -94,102 +129,146 @@ int lm_mptp_connect(lm_url_t *url) {
return sock;
}
bool lm_mptp_send(int sock, lm_mptp_t *packet) {
bool lm_mptp_client_send(int sock, lm_mptp_t *packet) {
if (NULL == packet) {
lm_error_set(LM_ERR_ArgNULL);
return false;
}
if (MPTP_FLAGS_VERSION(packet) != MPTP_VERSION_SUPPORTED) {
lm_error_set(LM_ERR_MPTPBadVersion);
return false;
}
size_t size = sizeof(packet->flags) + sizeof(packet->size);
if (packet->header.size > MPTP_CHUNK_MAX) {
lm_error_set(LM_ERR_MPTPBadChunk);
return false;
}
if (NULL == packet->data || packet->size <= 0) {
if (send(sock, packet, size, 0) < 0) {
lm_error_set(LM_ERR_MPTPSendFail);
char buffer[sizeof(packet->header) + packet->header.size];
bzero(buffer, sizeof(buffer));
if (packet->header.size > 0)
memcpy(buffer + sizeof(packet->header), packet->data, packet->header.size);
packet->header.flags = htons(packet->header.flags);
packet->header.size = htons(packet->header.size);
memcpy(buffer, &packet->header, sizeof(packet->header));
if (send(sock, buffer, sizeof(buffer), 0) < 0) {
lm_error_set(LM_ERR_MPTPSendFail);
return false;
}
return true;
}
bool lm_mptp_client_recv(int sock, lm_mptp_t *packet) {
if (NULL == packet) {
lm_error_set(LM_ERR_ArgNULL);
return false;
}
char buffer[sizeof(packet->header) + MPTP_CHUNK_MAX];
bzero(buffer, sizeof(buffer));
if (recv(sock, buffer, sizeof(buffer), 0) < 0) {
if (ETIMEDOUT == errno || EAGAIN == errno) {
lm_error_set(LM_ERR_MPTPTimeout);
return false;
}
lm_error_set(LM_ERR_MPTPRecvFail);
return false;
}
memcpy(&packet->header, buffer, sizeof(packet->header));
packet->header.flags = ntohs(packet->header.flags);
packet->header.size = ntohs(packet->header.size);
if (packet->header.size > MPTP_CHUNK_MAX)
return true;
}
if (send(sock, packet, size, 0) < 0) {
lm_error_set(LM_ERR_MPTPSendFail);
return false;
}
if (send(sock, packet->data, packet->size, 0) < 0) {
lm_error_set(LM_ERR_MPTPSendFail);
return false;
}
memcpy(packet->data, buffer + sizeof(packet->header), packet->header.size);
return true;
}
bool lm_mptp_send_data(int sock, char *data, size_t size) {
if (size > MPTP_CHUNK_MAX) {
lm_error_set(LM_ERR_MPTPChunkFail);
int lm_mptp_server_listen(char *addr, uint16_t port) {
struct sockaddr saddr;
int sock;
bzero(&saddr, sizeof(saddr));
if ((sock = lm_mptp_socket(addr, port, &saddr)) < 0)
return -1;
if (bind(sock, &saddr, sizeof(struct sockaddr)) < 0) {
lm_mptp_close(sock);
lm_error_set(LM_ERR_MPTPBindFail);
return -1;
}
return sock;
}
bool lm_mptp_server_recv(int sock, lm_mptp_t *packet, struct sockaddr *addr) {
if (NULL == packet || NULL == addr) {
lm_error_set(LM_ERR_ArgNULL);
return false;
}
if (send(sock, data, size, 0) < 0) {
lm_error_set(LM_ERR_MPTPSendFail);
socklen_t socklen = sizeof(struct sockaddr);
char buffer[sizeof(packet->header) + MPTP_CHUNK_MAX];
bzero(buffer, sizeof(buffer));
bzero(packet, sizeof(lm_mptp_t));
if (recvfrom(sock, buffer, sizeof(buffer), 0, addr, &socklen) <= 0) {
lm_error_set(LM_ERR_MPTPRecvFail);
return false;
}
memcpy(&packet->header, buffer, sizeof(packet->header));
packet->header.flags = ntohs(packet->header.flags);
packet->header.size = ntohs(packet->header.size);
if (packet->header.size > MPTP_CHUNK_MAX)
return true;
memcpy(packet->data, buffer + sizeof(packet->header), sizeof(packet->header));
return true;
}
bool lm_mptp_recv(int sock, lm_mptp_t *packet) {
if (recv(sock, &packet->flags, sizeof(packet->flags), 0) < 0)
goto recvfail;
if (recv(sock, &packet->size, sizeof(packet->size), 0) < 0)
goto recvfail;
bool lm_mptp_server_send(int sock, lm_mptp_t *packet, struct sockaddr *addr) {
if (NULL == packet) {
lm_error_set(LM_ERR_ArgNULL);
return false;
}
if (MPTP_FLAGS_VERSION(packet) != MPTP_VERSION_SUPPORTED) {
lm_error_set(LM_ERR_MPTPBadVersion);
return false;
}
if (NULL == packet->data)
return true;
if (packet->size <= 0)
return true;
if (packet->size > MPTP_CHUNK_MAX) {
lm_error_set(LM_ERR_MPTPChunkFail);
if (packet->header.size > MPTP_CHUNK_MAX) {
lm_error_set(LM_ERR_MPTPBadChunk);
return false;
}
if (recv(sock, &packet->data, packet->size, 0) < 0)
goto recvfail;
socklen_t addrlen = sizeof(struct sockaddr);
char buffer[sizeof(packet->header.size) + packet->header.size];
return true;
bzero(buffer, sizeof(buffer));
recvfail:
if (errno == ETIMEDOUT || errno == EAGAIN)
lm_error_set(LM_ERR_MPTPTimeout);
else
lm_error_set(LM_ERR_MPTPRecvFail);
return false;
}
packet->header.flags = htons(packet->header.flags);
packet->header.size = htons(packet->header.size);
bool lm_mptp_recv_data(int sock, char *data, size_t size) {
if (size > MPTP_CHUNK_MAX) {
lm_error_set(LM_ERR_MPTPChunkFail);
return false;
}
memcpy(buffer, &packet->header, sizeof(packet->header));
memcpy(buffer + sizeof(packet->header), packet->data, packet->header.size);
if (recv(sock, data, size, 0) < 0) {
if (errno == ETIMEDOUT)
lm_error_set(LM_ERR_MPTPTimeout);
else
lm_error_set(LM_ERR_MPTPRecvFail);
if (sendto(sock, buffer, sizeof(buffer), 0, addr, addrlen) < 0) {
lm_error_set(LM_ERR_MPTPSendFail);
return false;
}
return true;
}
void lm_mptp_disconnect(int sock) {
close(sock);
}

View File

@ -30,27 +30,27 @@ lm_pool_t *lm_pool_new(char *name, char *url) {
void lm_pool_test(lm_pool_t *pool) {
lm_mptp_t packet;
lm_mptp_init(&packet, true, MPTP_C2S_PING, true);
lm_mptp_packet_init(&packet, true, MPTP_C2S_PING, true);
int sock = lm_mptp_connect(&pool->url);
int sock = lm_mptp_client_connect(pool->url.host, pool->url.port);
if (sock == -1) {
pool->available = false;
return;
}
if (!lm_mptp_send(sock, &packet)) {
if (!lm_mptp_client_send(sock, &packet)) {
pool->available = false;
goto end;
}
if (!lm_mptp_recv(sock, &packet)) {
if (!lm_mptp_client_recv(sock, &packet)) {
pool->available = false;
goto end;
}
pool->available = true;
pool->available = MPTP_FLAGS_TYPE(&packet) == MPTP_S2C_PONG;
end:
lm_mptp_disconnect(sock);
lm_mptp_close(sock);
return;
}