packet.h (1886B)
1 #ifndef PACKET_H 2 #define PACKET_H 3 4 #include <string.h> 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <stdint.h> 8 #include <stdarg.h> 9 #include <unistd.h> 10 #include <errno.h> 11 #include <signal.h> 12 #include <sys/socket.h> 13 #include <arpa/inet.h> 14 #include <netdb.h> 15 #include <sodium.h> 16 17 #define DEBUG 1 18 #define DOMAIN "127.0.0.1" 19 #define PORT 20247 20 #define MAX_CONNECTION 5 21 #define MAX_MESSAGE_LENGTH 8192 22 #define ERROR_LENGTH 26 23 24 #define ZSM_TYP_KEY 0x1 25 #define ZSM_TYP_SEND_MESSAGE 0x2 26 #define ZSM_TYP_UPDATE_MESSAGE 0x3 27 #define ZSM_TYP_DELETE_MESSAGE 0x4 28 #define ZSM_TYP_PRESENCE 0x5 29 #define ZSM_TYP_TYPING 0x6 30 #define ZSM_TYP_ERROR 0x7 /* Error message */ 31 #define ZSM_TYP_B 0x8 32 33 #define ZSM_STA_SUCCESS 0x1 34 #define ZSM_STA_INVALID_TYPE 0x2 35 #define ZSM_STA_INVALID_LENGTH 0x3 36 #define ZSM_STA_TOO_LONG 0x4 37 #define ZSM_STA_READING_SOCKET 0x5 38 #define ZSM_STA_WRITING_SOCKET 0x6 39 #define ZSM_STA_UNKNOWN_USER 0x7 40 #define ZSM_STA_MEMORY_ALLOCATION 0x8 41 #define ZSM_STA_WRONG_KEY_LENGTH 0x9 42 43 #define PUBLIC_KEY_SIZE crypto_kx_PUBLICKEYBYTES 44 #define PRIVATE_KEY_SIZE crypto_kx_SECRETKEYBYTES 45 #define SHARED_KEY_SIZE crypto_kx_SESSIONKEYBYTES 46 #define NONCE_SIZE crypto_aead_xchacha20poly1305_ietf_NPUBBYTES 47 #define ADDITIONAL_SIZE crypto_aead_xchacha20poly1305_ietf_ABYTES 48 49 typedef struct message { 50 uint8_t option; 51 uint8_t type; 52 unsigned long long length; 53 unsigned char *data; 54 } message; 55 56 /* Utilities functions */ 57 void error(int fatal, const char *fmt, ...); 58 void *memalloc(size_t size); 59 void *estrdup(void *str); 60 unsigned char *get_public_key(int sockfd); 61 int send_public_key(int sockfd, unsigned char *pk); 62 void print_packet(message *msg); 63 int recv_packet(message *msg, int fd); 64 message *create_error_packet(int code); 65 message *create_packet(uint8_t option, uint8_t type, uint32_t length, char *data); 66 int send_packet(message *msg, int fd); 67 void free_packet(message *msg); 68 69 #endif