asfur/network.h
Lorenzo Torres 238eff5bb3 Add unit test suite and complete protocol documentation
- Add test framework (test.h) with assertion macros and test runner
- Add comprehensive unit tests (test_network.c) covering:
  - Packet parsing and error handling
  - User registration and authentication
  - Room operations (create, join, leave, delete, list)
  - Direct messaging functionality
  - Message broadcasting
  - Password hashing
- Update Makefile with 'make test' target
- Rewrite PROTOCOL file with complete specification:
  - All 14 packet types with data layouts
  - All error codes with descriptions
  - Typical usage flows
2026-01-08 15:51:01 +01:00

117 lines
2 KiB
C

#ifndef NETWORK_H
#define NETWORK_H
#include <stdint.h>
struct client;
typedef enum {
PACKET_ERROR = 0,
PACKET_REGISTER = 1,
PACKET_AUTHENTICATE = 2,
PACKET_JOIN = 3,
PACKET_TEXT = 4,
PACKET_LEAVE = 5,
PACKET_OK = 6,
PACKET_DM_OPEN = 7,
PACKET_DM_ROOM = 8,
PACKET_CREATE_ROOM = 9,
PACKET_DELETE_ROOM = 10,
PACKET_ROOM_CREATED = 11,
PACKET_LIST_ROOMS = 12,
PACKET_ROOM_LIST = 13
} packet_type;
typedef enum {
ERR_OK = 0,
ERR_UNKNOWN = 1,
ERR_INVALID_PACKET = 2,
ERR_NOT_AUTHENTICATED = 3,
ERR_ALREADY_REGISTERED = 4,
ERR_INVALID_CREDENTIALS = 5,
ERR_REGISTRATION_DISABLED = 6,
ERR_DATABASE = 7,
ERR_USER_NOT_FOUND = 8,
ERR_ACCESS_DENIED = 9,
ERR_ROOM_NOT_FOUND = 10,
ERR_ROOM_NAME_TAKEN = 11,
ERR_NOT_ROOM_OWNER = 12
} error_code;
struct packet_header {
uint16_t size;
uint8_t type;
};
struct packet_register {
char username[20];
char password[100];
};
struct packet_auth {
char username[20];
char password[100];
};
struct packet_join {
uint64_t room_id;
};
struct packet_leave {
uint64_t room_id;
};
struct packet_text {
uint64_t room_id;
char message[];
};
struct packet_error {
uint8_t code;
};
struct packet_text_broadcast {
uint64_t room_id;
char username[32];
char message[];
};
struct packet_dm_open {
char username[20];
};
struct packet_dm_room {
uint64_t room_id;
};
struct packet_create_room {
char name[32];
};
struct packet_delete_room {
uint64_t room_id;
};
struct packet_room_created {
uint64_t room_id;
char name[32];
};
struct room_list_entry {
uint64_t room_id;
char name[32];
char owner[20];
};
struct packet_room_list {
uint32_t count;
struct room_list_entry rooms[];
};
int network_init(void);
void network_shutdown(void);
void network_handle_data(struct client *client, const char *data, size_t len);
void network_client_add(struct client *client);
void network_client_remove(struct client *client);
#endif