new: add mptp packet verify functions

This commit is contained in:
ngn
2024-06-22 07:03:17 +03:00
parent 19c98b763d
commit c8e45097fe
12 changed files with 275 additions and 110 deletions

View File

@ -1,5 +1,4 @@
#include "../../include/error.h"
#include "../../include/mptp.h"
#include "../../include/all.h"
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
@ -7,46 +6,28 @@
int main(int argc, char *argv[]) {
int ret = EXIT_FAILURE;
if (argc != 3) {
printf("usage: %s <host> <port>\n", argv[0]);
if (argc != 2) {
printf("usage: %s <host>\n", argv[0]);
return ret;
}
int porti = atoi(argv[2]);
if (porti <= 0 || porti > UINT16_MAX) {
printf("bad port number\n");
return ret;
lm_ctx_t ctx;
lm_ctx_init(&ctx);
ctx.debug = true;
if (!lm_ctx_pool_add(&ctx, "test", NULL)) {
printf("failed to add pool: %s (%d)\n", lm_strerror(), lm_error());
goto end;
}
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;
}
if(!lm_ctx_pool_serve(&ctx, argv[1])) {
printf("failed to serve the pools: %s (%d)\n", lm_strerror(), lm_error());
goto end;
}
ret = EXIT_SUCCESS;
end:
lm_mptp_close(sock);
lm_ctx_free(&ctx);
return ret;
}