-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatter.h
280 lines (202 loc) · 7.65 KB
/
chatter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <error.h>
#include <errno.h>
#include <stdarg.h>
#include <time.h>
#include <stdlib.h>
#include <openssl/sha.h>
#include <openssl/rand.h>
#include <fcntl.h>
#include <poll.h>
#include "threadpool.h"
#include "debug.h"
#define SA struct sockaddr
#define SAin struct sockaddr_in
#define MAX_MSG_SIZE 1024
#define MAX_RECV MAX_MSG_SIZE
#define MAX_SEND MAX_MSG_SIZE
#define MAX_CMD 64
#define MAX_USERNAME 32
#define MAX_PASSWORD 32
#define MAX_USERS 64
#define MAX_ROOMS 64
typedef struct room room_t;
/* ----------------- vars.c ----------------- */
/* ------------------------------------------ */
extern const char *server_help_message;
extern const char *client_help_message;
extern const char *password_rules;
extern const char *space;
extern const char *rn;
extern const char *r;
extern const char *sprn;
extern const char *accounts_filename;
// #define ERROR_MSG(error_code) error_messages[error_code]
// enum errors_enum {ERR00, ERR01, ERR02, ERR10, ERR11, ERR20, ERR30, ERR40, ERR41, ERR60, ERR61, ERR62, ERR100};
// extern const char *error_messages[];
// extern const int error_codes[];
/* ----------------- login.c ---------------- */
/* ------------------------------------------ */
extern char motd[MAX_MSG_SIZE];
void iam_login_server(int connfd, char *client_username);
void iamnew_login_server(int connfd, char *client_username);
void *login_thread_func(void *arg);
/* ------------------------------------------ */
/* ------------------------------------------ */
enum verbs_enum {
ALOHA, AHOLA, IAM, IAMNEW, HI, HINEW, AUTH,
PASS, NEWPASS, ERR, BYE, MSG,
CREATER, RETAERC,
CREATEP, PETAERC,
LISTR, RTSIL,
LISTU, UTSIL,
JOIN,NIOJ,
JOINP,PNIOJ,
LEAVE,EVAEL,
KICK,KCIK,
TELL,LLET,
ECHO,ECHOP, ECHOR,
NOP, QUIT
};
extern const char *verbs[];
/* ------------------------------------------ */
/* ------------------------------------------ */
/* ---------------- errors.c ---------------- */
/* ------------------------------------------ */
typedef struct error_message error_message_t;
struct error_message
{
const char *msg;
int code;
};
extern const error_message_t error_messages[];
extern const size_t num_errors;
const char * get_error(int err_code);
/* ------------------------------------------ */
/* ------------------------------------------ */
/* ---------------- locks.c ----------------- */
/* ------------------------------------------ */
extern pthread_mutex_t user_account_mutex;
extern pthread_mutex_t user_info_mutex;
void init_user_mutexes();
void lock_user_accounts(int track);
void unlock_user_accounts(int track);
void lock_user_info(int track);
void unlock_user_info(int track);
extern pthread_mutex_t room_mutex;
void init_room_mutexes();
void lock_rooms(int track);
void unlock_rooms(int track);
void lock_room_members(int track);
void unlock_room_members(int track);
/* ------------------------------------------ */
/* ------------------------------------------ */
/* --------------- chatter.c ---------------- */
/* ------------------------------------------ */
extern char print_buff[MAX_MSG_SIZE];
int send_data(int connfd, int verb, char *data);
int send_data_custom(int connfd, char *data);
int recv_data(int connfd, char *recvbuff);
int expect_data(char *recvbuff, char *message_data, int *error_code, int num_verbs, ...);
void send_error(int connfd, int error, char *message, bool close_connection);
void print_error(char *recvbuff, char *message_data, int error_code);
int tokenize(char *string, const char *delim);
char *get_token(char *string, const char *delim, int count);
/* ------------------------------------------ */
/* ------------------------------------------ */
/* ----------------- util.c ----------------- */
/* ------------------------------------------ */
void strip_char(char *str, char strip);
unsigned int randint();
char *inet4_ntop(char *dst, unsigned int addr);
/* ------------------------------------------ */
/* ------------------------------------------ */
/* ----------------- auth.c ----------------- */
/* ------------------------------------------ */
typedef struct user_account user_account_t;
struct user_account {
char username[MAX_USERNAME+1];
char salt[(sizeof(int)*2) + 1];
char password_hash[(SHA_DIGEST_LENGTH*2)+1];
};
extern user_account_t user_accounts[MAX_USERS]; // in-memory representation of user database
extern size_t num_user_accounts; // how many are valid and should be considered
typedef struct user_info user_info_t;
struct user_info {
char username[MAX_USERNAME+1];
int connfd;
bool ready;
room_t *room;
user_info_t *next;
};
extern user_info_t *user_infos; // logged in users, and their socket descriptors
bool check_password(char *password); // check if password fits criterion
bool user_exists(char *username); // does such a user exist in database?
int create_user(char* username, char* password); // create a new user in the database
int fill_user_slot(char *username); // create temporary user in database during user creation
int release_user_slot(char *username); // upon user creation failure, release the slot
int authenticate_user(char *username, char *password); // check the user's password against database
user_account_t* get_user_accounts(); // read in all users from database to memory
int free_user_accounts(); // zero out the user account structs
int write_user_accounts(); // write the user accounts in memory to disk
user_info_t* login_user(char *username, int connfd); //login the user
int logout_user(char *username); //logout the user, close the connection if not closed
user_info_t* user_logged_in(char *username); //see if user is logged in
user_info_t* get_user_byfd(int connfd); //get the user info by the socket fd
user_info_t* get_user_byname(char *username);
int get_num_users();
int mark_user_ready(char *username);
/* ------------------------------------------ */
/* ------------------------------------------ */
/* ----------------- rooms.c ---------------- */
/* ------------------------------------------ */
typedef struct room_member room_member_t;
struct room_member {
user_info_t *user;
bool owner;
room_member_t *next;
};
struct room {
char room_name[MAX_USERNAME];
bool private_room;
int room_id;
room_member_t *room_members;
char password[MAX_PASSWORD]; //no hashing
room_t *next;
};
extern room_t *rooms;
extern size_t num_rooms;
extern int room_inc_id;
int create_room(char *room_name, user_info_t *user, bool private_room, char *password);
room_t *get_room(char *room_name);
room_t *get_room_by_id(int room_id);
int free_room_members(room_member_t *room_members);
int add_room_member(room_t *room, user_info_t *user, char *password);
int remove_room_member(room_t *room, char *username);
int remove_user_from_rooms(char *username);
int close_room(room_t *room);
int list_rooms(char *sendbuff);
void list_users(room_t *room, char *sendbuff);
int check_room(room_t *room);
int close_room_by_name(char *room_name);
/* ------------------------------------------ */
/* ------------------------------------------ */
/* --------------- echo.c ----------------- */
/* ------------------------------------------ */
extern bool echo_mode;
extern bool echo_flag;
extern bool echo_running;
extern pthread_t echo_thread;
extern pthread_attr_t echo_thread_attr;
void spawn_echo_thread();
void *echo_thread_func(void *arg);
void echo_all_room(room_t *room, int verb, char *message_data);
/* ------------------------------------------ */
/* ------------------------------------------ */