zsm/include/packet.h

84 lines
2.2 KiB
C
Raw Normal View History

2024-04-30 18:17:17 +02:00
#ifndef PACKET_H
#define PACKET_H
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <stdbool.h>
2024-04-30 18:17:17 +02:00
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <time.h>
2024-04-30 18:17:17 +02:00
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/epoll.h>
2024-04-30 18:17:17 +02:00
#include <arpa/inet.h>
#include <netinet/in.h>
2024-04-30 18:17:17 +02:00
#include <netdb.h>
#include <fcntl.h>
#include <pthread.h>
2024-04-30 18:17:17 +02:00
#include <sodium.h>
#include <libgen.h>
#include <wchar.h>
#include "config.h"
2024-04-30 18:17:17 +02:00
#define ZSM_TYP_AUTH 0x1
#define ZSM_TYP_MESSAGE 0x2
2024-04-30 18:17:17 +02:00
#define ZSM_TYP_UPDATE_MESSAGE 0x3
#define ZSM_TYP_DELETE_MESSAGE 0x4
#define ZSM_TYP_ERROR 0x5
#define ZSM_TYP_INFO 0x6
2024-04-30 18:17:17 +02:00
#define ZSM_STA_SUCCESS 0x1
#define ZSM_STA_INVALID_TYPE 0x2
#define ZSM_STA_INVALID_LENGTH 0x3
#define ZSM_STA_TOO_LONG 0x4
#define ZSM_STA_READING_SOCKET 0x5
#define ZSM_STA_WRITING_SOCKET 0x6
#define ZSM_STA_UNKNOWN_USER 0x7
#define ZSM_STA_MEMORY_ALLOCATION 0x8
#define ZSM_STA_ERROR_ENCRYPT 0x9
#define ZSM_STA_ERROR_DECRYPT 0xA
#define ZSM_STA_ERROR_AUTHENTICATE 0xB
#define ZSM_STA_ERROR_INTEGRITY 0xC
#define ZSM_STA_UNAUTHORISED 0xD
#define ZSM_STA_AUTHORISED 0xE
#define ZSM_STA_CLOSED_CONNECTION 0xF
2024-04-30 18:17:17 +02:00
#define ADDRESS_SIZE MAX_NAME + 1 + 255 /* 1 for @, 255 for domain, defined in RFC 5321, Section 4.5.3.1.2 */
#define HASH_SIZE crypto_generichash_BYTES
2024-04-30 18:17:17 +02:00
#define NONCE_SIZE crypto_aead_xchacha20poly1305_ietf_NPUBBYTES
#define ADDITIONAL_SIZE crypto_aead_xchacha20poly1305_ietf_ABYTES
#define MAX_MESSAGE_LENGTH MAX_DATA_LENGTH - MAX_NAME * 2 - NONCE_SIZE
2024-04-30 18:17:17 +02:00
typedef struct packet_t {
uint8_t status;
2024-04-30 18:17:17 +02:00
uint8_t type;
uint32_t length;
uint8_t *data;
uint8_t *signature;
} packet_t;
typedef struct message_t {
uint8_t author[MAX_NAME];
uint8_t recipient[MAX_NAME];
uint8_t *content;
time_t creation;
} message_t;
#include "key.h"
2024-04-30 18:17:17 +02:00
/* Utilities functions */
int recv_packet(packet_t *pkt, int fd, uint8_t required_type);
2024-09-20 18:16:59 +02:00
packet_t *create_packet(uint8_t status, uint8_t type, uint32_t length, uint8_t *data, uint8_t *signature);
int send_packet(packet_t *msg, int fd);
void free_packet(packet_t *msg);
int verify_packet(packet_t *pkt, int fd);
uint8_t *create_signature(uint8_t *data, uint32_t length, secret_key *sk);
2024-04-30 18:17:17 +02:00
#endif