protocol

protocol tests
Log | Files | Refs | Submodules | README | git clone https://git.ne02ptzero.me/git/protocol

commit c79364416e1eaae01ff9840e369382464d931ecb
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Thu, 23 Feb 2017 17:25:19 +0100

Add(Tree): And some basic functions

Diffstat:
AMakefile | 21+++++++++++++++++++++
Ainc/package.h | 147+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ainc/protocol_test.h | 8++++++++
Asrc/main.c | 16++++++++++++++++
Asrc/main.o | 0
Asrc/package.c | 139+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/package.o | 0
7 files changed, 331 insertions(+), 0 deletions(-)

diff --git a/Makefile b/Makefile @@ -0,0 +1,21 @@ +NAME = protocol_tests +CC = gcc +CFLAGS = -Wall -Werror -Wextra -I inc/ -I lib/inc/ -g -O3 +LFLAGS = -L../lib/ -lmorphux +SRCS = $(wildcard src/*.c) +OBJS = $(SRCS:%.c=%.o) + +all: $(NAME) + +$(NAME): $(OBJS) + $(CC) $(CFLAGS) $(OBJS) -o $(NAME) $(LFLAGS) + +clean: + rm -f $(OBJS) + +fclean: clean + rm -f $(NAME) + +re: fclean all + +.PHONY: re fclean clean diff --git a/inc/package.h b/inc/package.h @@ -0,0 +1,147 @@ +#ifndef PACKAGE_H +# define PACKAGE_H + +# include <morphux.h> +# include "protocol_test.h" + +/* Header */ +typedef struct package_s { + u8_t type; + u16_t size; + u8_t next_pkg_len; + char *next_pkg; + u8_t number; + mlist_t *payload; +} SF_PACKED package_t; + +/* Type field of the header */ +typedef enum package_type_e { + PKG_TYPE_AUTH = 1, + PKG_TYPE_AUTH_ACK, + PKG_TYPE_ERROR, + PKG_TYPE_REQ_GET_PKG, + PKG_TYPE_REQ_GET_FILE, + PKG_TYPE_REQ_GET_NEWS, + PKG_TYPE_REQ_CAT, + PKG_TYPE_REQ_UPD, + PKG_TYPE_RESP_PKG, + PKG_TYPE_RESP_FILE, + PKG_TYPE_RESP_NEWS, + PKG_TYPE_RESP_CAT, + PKG_TYPE_END +} package_type_t; + +/* Payloads */ + +typedef struct auth_s { + u8_t mpm_major_version; + u8_t mpm_minor_version; +} SF_PACKED auth_t; + +typedef struct auth_ack_s { + u8_t mpm_major_version; + u8_t mpm_minor_version; +} SF_PACKED auth_ack_t; + +typedef struct error_s { + u8_t error_type; + u16_t error_len; + char *err; +} SF_PACKED error_t; + +/* Error possible types */ +typedef enum error_type_e { + ERR_SERVER_FAULT = 1, + ERR_MALFORMED_PACKET, + ERR_RES_NOT_FOUND, + ERR_END +} SF_PACKED error_type_t; + +typedef struct req_get_pkg_s { + u64_t id; + u8_t state; + u16_t name_len; + u16_t categ_len; + u16_t version_len; + char *name; + char *category; + char *version; +} SF_PACKED req_get_pkg_t; + +/* Stability of packages */ +typedef enum package_state_e { + PKG_STABLE = 1, + PKG_UNSTABLE, + PKG_DEV, + PKG_END +} package_state_t; + +typedef struct req_get_file_s { + u64_t id; + u16_t path_len; + char *path; +} SF_PACKED req_get_file_t; + +typedef struct req_get_news_s { + time_t last_request; + u16_t pkgs_ids_size; + u64_t *pkgs_ids; +} SF_PACKED req_get_news_t; + +typedef struct req_get_cat_s { + u16_t cat_len; + u64_t *categories; +} SF_PACKED req_get_cat_t; + +typedef struct req_get_upd_s { + u64_t pkg_len; + u64_t *packages; +} SF_PACKED req_get_upd_t; + +typedef struct resp_pkg_s { + u64_t id; + float comp_time; + float inst_size; + float arch_size; + u8_t state; + u16_t name_len; + u16_t category_len; + u16_t version_len; + u16_t archive_len; + u16_t checksum_len; + u16_t dependencies_size; + char *name; + char *category; + char *version; + char *archive; + char *checksum; + u64_t *dependencies; +} SF_PACKED resp_pkg_t; + +typedef struct resp_file_s { + u64_t id; + u8_t type; + u64_t parent_id; + u16_t path_len; + char *path; +} SF_PACKED resp_file_t; + +typedef struct resp_news_s { + u64_t id; + u64_t parent_id; + u16_t author_len; + u16_t author_mail_len; + u16_t text_len; + char *author; + char *author_mail; + char *text; +} SF_PACKED resp_news_t; + +typedef struct resp_cat_s { + u64_t id; + u64_t parent_id; + u16_t name_len; + char *name; +} SF_PACKED resp_cat_t; + +#endif /* PACKAGE_H */ diff --git a/inc/protocol_test.h b/inc/protocol_test.h @@ -0,0 +1,8 @@ +#ifndef PROTOCOL_TEST_H +# define PROTOCOL_TEST_H + +# include <morphux.h> + +# define SF_PACKED __attribute__((packed)) + +#endif /* PROTOCOL_TEST_H */ diff --git a/src/main.c b/src/main.c @@ -0,0 +1,16 @@ +#include "protocol_test.h" +#include "package.h" + +int main(int ac, char **av) { + if (ac == 1) + m_panic("Can't do without an argument\n"); + + if (strcmp(av[1], "client") == 0) { + /* Client */ + } else if (strcmp(av[1], "server") == 0) { + /* Server */ + } else { + m_panic("Unknown argument: %s\n", av[1]); + } + return 0; +} diff --git a/src/main.o b/src/main.o Binary files differ. diff --git a/src/package.c b/src/package.c @@ -0,0 +1,139 @@ +#include "package.h" + + +static size_t read_header(package_t *pkg, void *data) { + size_t count = 0; + + memcpy(&pkg->type, data + count, sizeof(pkg->type)); + count += sizeof(pkg->type); + memcpy(&pkg->size, data + count, sizeof(pkg->size)); + count += sizeof(pkg->size); + memcpy(&pkg->next_pkg_len, data + count, sizeof(pkg->next_pkg_len)); + count += sizeof(pkg->next_pkg_len); + + if (pkg->next_pkg_len != 0) { + pkg->next_pkg = malloc(sizeof(char) * pkg->next_pkg_len); + memcpy(pkg->next_pkg, data + count, pkg->next_pkg_len); + count += pkg->next_pkg_len; + } + return count; +} + +static bool check_type(package_t *pkg) { + if (pkg->type >= PKG_TYPE_END) + return false; + return true; +} + +static size_t read_payload_auth(package_t *pkg, void *data) { + size_t count = 0; + auth_t *auth; + + auth = malloc(sizeof(auth_t)); + assert(auth != NULL); + memcpy(&auth->mpm_major_version, data + count, sizeof(auth->mpm_major_version)); + count += sizeof(auth->mpm_major_version); + memcpy(&auth->mpm_minor_version, data + count, sizeof(auth->mpm_minor_version)); + count += sizeof(auth->mpm_minor_version); + list_add(pkg->payload, auth, sizeof(auth_t)); + return count; +} + +static size_t read_payload_auth_ack(package_t *pkg, void *data) { + size_t count = 0; + auth_ack_t *auth; + + auth = malloc(sizeof(auth_t)); + assert(auth != NULL); + memcpy(&auth->mpm_major_version, data + count, sizeof(auth->mpm_major_version)); + count += sizeof(auth->mpm_major_version); + memcpy(&auth->mpm_minor_version, data + count, sizeof(auth->mpm_minor_version)); + count += sizeof(auth->mpm_minor_version); + list_add(pkg->payload, auth, sizeof(auth_ack_t)); + return count; +} + +static size_t read_payload_error(package_t *pkg, void *data) { + size_t count = 0; + error_t *err; + + err = malloc(sizeof(error_t)); + assert(err != NULL); + memcpy(&err->error_type, data + count, sizeof(err->error_type)); + count += sizeof(err->error_type); + memcpy(&err->error_len, data + count, sizeof(err->error_len)); + count += sizeof(err->error_len); + + err->err = malloc(sizeof(char) * err->error_len); + assert(err->err != NULL); + memcpy(err->err, data + count, err->error_len); + count += err->error_len; + list_add(pkg->payload, err, sizeof(error_t)); + return count; +} + +static size_t read_payload_get_package(package_t *pkg, void *data) { + size_t count = 0; + req_get_pkg_t *req; + + req = malloc(sizeof(req_get_pkg_t)); + assert(req != NULL); + + memcpy(&req->id, data + count, sizeof(req->id)); + count += sizeof(req->id); + memcpy(&req->state, data + count, sizeof(req->state)); + count += sizeof(req->state); + memcpy(&req->name_len, data + count, sizeof(req->name_len)); + count += sizeof(req->name_len); + memcpy(&req->categ_len, data + count, sizeof(req->categ_len)); + count += sizeof(req->categ_len); + memcpy(&req->version_len, data + count, sizeof(req->version_len)); + count += sizeof(req->version_len); + + memcpy(req->name, data + count, req->name_len); + count += req->name_len; + memcpy(req->category, data + count, req->categ_len); + count += req->categ_len; + memcpy(req->version, data + count, req->version_len); + count += req->version_len; + + list_add(pkg->payload, req, sizeof(req_get_pkg_t)); + return count; +} + +typedef size_t (*payload_callback)(package_t *, void *); +static const payload_callback arr[] = { + &read_payload_auth, + &read_payload_auth_ack, + &read_payload_error, + &read_payload_get_package, + NULL +}; + +static size_t read_payload(package_t *pkg, void *data) { + return arr[pkg->type](pkg, data); +} + +package_t *read_pkg(void *data) { + package_t *pkg; + size_t count; + u8_t number; + + pkg = malloc(sizeof(package_t)); + assert(pkg != NULL); + pkg->payload = NULL; + count = read_header(pkg, data); + + if (!check_type(pkg)) + return NULL; + + data += count; + memcpy(&number, data, sizeof(u8_t)); + data += sizeof(u8_t); + while (number) { + data += read_payload(pkg, data); + number--; + } + + return pkg; +} diff --git a/src/package.o b/src/package.o Binary files differ.