-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
mini_bbs.c.disabled
488 lines (398 loc) · 12.2 KB
/
mini_bbs.c.disabled
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include <uvm/syscalls.h>
#include <uvm/utils.h>
#define MAX_NAME_LEN 32
#define MAX_MSG_LEN 2048
#define MAX_USERS 512
#define MAX_MESSAGES 4096
// User states
#define STATE_DEFAULT 0
#define STATE_PICK_NAME 1
#define STATE_WRITE_MSG 2
#define STATE_READ_MSGS 3
typedef struct
{
// Associated TCP socket
u64 socket_id;
// User name
char name[MAX_NAME_LEN];
// Current state
int state;
// Current index
int cur_index;
// Buffer for message writing
char msg_buf[MAX_MSG_LEN];
} user_t;
typedef struct
{
u64 timestamp;
// We copy the username because user_t structs are ephemeral
char username[MAX_NAME_LEN];
char text[MAX_MSG_LEN];
} message_t;
user_t users[MAX_USERS];
message_t messages[MAX_MESSAGES];
// Number of online users
size_t num_users = 0;
// Number of messages posted
size_t num_messages = 0;
// TCP socket to listen for new connections
u64 listen_sock;
// Process start time
u64 start_time;
// Find a user by socket id
user_t* find_user(u64 socket_id)
{
for (int i = 0; i < MAX_USERS; ++i)
{
user_t* p_user = &users[i];
if (p_user->socket_id == socket_id)
{
return p_user;
}
}
return NULL;
}
// Allocate a new user object
user_t* alloc_user(u64 client_sock)
{
if (num_users >= MAX_USERS)
return NULL;
// Try to find a free user struct
user_t* p_user = find_user(0);
if (!p_user)
return NULL;
memset(p_user, 0, sizeof(user_t));
p_user->socket_id = client_sock;
// Increment the number of users connected
++num_users;
return p_user;
}
void free_user(user_t* p_user)
{
memset(p_user, 0, sizeof(user_t));
--num_users;
}
// Strip trailing whitespace from a string
void rstrip_ws(char* buf)
{
int len = (int)strlen(buf);
for (int i = len - 1; i > 0; --i)
{
if (isspace(buf[i]))
{
buf[i] = '\0';
}
else
{
break;
}
}
}
void post_message(user_t* p_user)
{
// If we're at capacity, drop the first message, shift the messages back
if (num_messages == MAX_MESSAGES)
{
for (int i = 0; i < num_messages - 1; ++i)
{
memcpy(&messages[i], &messages[i+1], sizeof(message_t));
}
--num_messages;
}
// Find a free message struct
size_t msg_idx = num_messages;
++num_messages;
assert(num_messages <= MAX_MESSAGES);
message_t* p_message = &messages[msg_idx];
// Initialize the message struct
p_message->timestamp = time_current_ms();
memcpy(p_message->username, p_user->name, MAX_NAME_LEN);
memcpy(p_message->text, p_user->msg_buf, sizeof(p_user->msg_buf));
}
void write_str(u64 socket_id, char* str)
{
net_write(socket_id, str, strlen(str));
}
void write_int(u64 socket_id, int val)
{
char buf[32];
itoa(val, buf, 10);
write_str(socket_id, buf);
}
void view_cur_message(user_t* p_user)
{
u64 socket_id = p_user->socket_id;
write_str(socket_id, "\n");
write_str(socket_id, "##################################################################\n");
write_str(socket_id, "Reading message ");
write_int(socket_id, (int)p_user->cur_index + 1);
write_str(socket_id, "/");
write_int(socket_id, (int)num_messages);
write_str(socket_id, ". Enter (N) for next message, (H) for help.\n");
write_str(socket_id, "##################################################################\n");
write_str(socket_id, "\n");
assert(p_user->cur_index < num_messages);
message_t* p_message = &messages[p_user->cur_index];
int n_seconds = (int)((time_current_ms() - p_message->timestamp) / 1000);
write_str(socket_id, "@");
write_str(socket_id, p_message->username);
write_str(socket_id, " wrote ");
write_int(socket_id, n_seconds);
write_str(socket_id, " seconds ago:\n");
write_str(socket_id, p_message->text);
write_str(socket_id, "\n");
write_str(socket_id, "\n");
}
void write_help(u64 socket_id)
{
char* help_msg = (
"\n"
"Available commands:\n"
"(R) Read messages\n"
"(P) Post new message\n"
"(H) Help\n"
"(A) About\n"
"(E) Exit\n"
"\n"
);
write_str(socket_id, help_msg);
}
void write_post_help(u64 socket_id)
{
write_str(socket_id, "\n");
write_str(socket_id, "##############################################################\n");
write_str(socket_id, "You may now write your message.\n");
write_str(socket_id, "You can include newlines and paragraphs in your message.\n");
write_str(socket_id, "Simply write Done and then press return to signal you are\n");
write_str(socket_id, "done writing. The maximum message length is 4096 characters.\n");
write_str(socket_id, "##############################################################\n");
write_str(socket_id, "\n");
}
void on_new_conn(u64 listen_sock)
{
puts("got new connection");
char client_addr[128];
u64 client_sock = net_accept(listen_sock, client_addr, sizeof(client_addr), on_incoming_data);
printf("client address: %s\n", client_addr);
user_t* p_user = alloc_user(client_sock);
if (!p_user)
{
puts("Could not find free user struct");
write_str(client_sock, "Too many users connected. Please come back later! :)\n");
net_close(client_sock);
return;
}
char* welcome_msg = (
"\n"
"##########################################################\n"
"#\n"
"# Welcome to the UVM Mini BBS!\n"
"#\n"
"# Visit the GitHub repository for more info:\n"
"# https://github.com/maximecb/uvm\n"
"#\n"
"##########################################################\n"
);
write_str(client_sock, welcome_msg);
// Print uptime
u64 uptime = (time_current_ms() - start_time) / 1000;
char uptime_str[32];
itoa((int)uptime, uptime_str, 10);
write_str(client_sock, "\nUptime is ");
write_str(client_sock, uptime_str);
write_str(client_sock, " seconds since the last restart or crash\n");
// Print users online
char num_users_str[32];
itoa((int)num_users, num_users_str, 10);
write_str(client_sock, num_users_str);
write_str(client_sock, " user(s) currently connected\n");
// Print messages posted
char num_msgs_str[32];
itoa((int)num_messages, num_msgs_str, 10);
write_str(client_sock, num_msgs_str);
write_str(client_sock, " message(s) stored in memory\n");
write_str(client_sock, "\nEnter a command and then press the return key\n");
write_help(client_sock);
}
void on_incoming_data(u64 socket_id, u64 num_bytes)
{
printf("received %d bytes of incoming data\n", num_bytes);
char read_buf[2048];
memset(read_buf, 0, sizeof(read_buf));
// If we receive a ton of bytes at once, this is likely
// malicious, close the socket and ignore it.
if (num_bytes > sizeof(read_buf) - 1)
{
net_close(socket_id);
return;
}
net_read(socket_id, read_buf, sizeof(read_buf) - 1);
// If the buffer contains control characters, ignore it
for (int i = 0; i < sizeof(read_buf); ++i)
{
char ch = read_buf[i];
if (ch == 0)
break;
if (!isprint(ch) && ch != '\r' && ch != '\n')
return;
}
// Find the user associated with this socket
user_t* p_user = find_user(socket_id);
if (!p_user)
{
puts("could not find user for socket id");
return;
}
puts(read_buf);
if (p_user->state == STATE_PICK_NAME)
{
// Strip trailing whitespace
rstrip_ws(read_buf);
size_t len = strlen(read_buf);
if (len == 0)
{
write_str(socket_id, "Username too short. Try again:\n");
return;
}
if (len > MAX_NAME_LEN - 1)
{
write_str(socket_id, "Username too long. Try again:\n");
return;
}
for (int i = 0; i < len; ++i)
{
if (!isalnum(read_buf[i]) && read_buf[i] != '_')
{
write_str(socket_id, "Username should only contain alphanumeric characters and underscores. Try again:\n");
return;
}
}
strncpy(p_user->name, read_buf, len);
write_str(socket_id, "Welcome ");
write_str(socket_id, p_user->name);
write_str(socket_id, "!\n");
p_user->state = STATE_WRITE_MSG;
write_post_help(socket_id);
return;
}
if (p_user->state == STATE_WRITE_MSG)
{
// Strip trailing whitespace from the buffer
rstrip_ws(read_buf);
if (strcasecmp(read_buf, "done") == 0)
{
// Strip trailing whitespace in the message buffer
rstrip_ws(p_user->msg_buf);
// If this is an empty message, do nothing
if (strlen(p_user->msg_buf) > 0)
{
post_message(p_user);
write_str(socket_id, "\n");
write_str(socket_id, "Message posted! :)\n");
write_help(socket_id);
}
else
{
write_str(socket_id, "\n");
write_str(socket_id, "Skipping empty message.\n");
write_help(socket_id);
}
p_user->state = STATE_DEFAULT;
memset(p_user->msg_buf, 0, sizeof(p_user->msg_buf));
return;
}
// Add back a newline on the message buffer because
// we stripped whitespace earlier
strncat(p_user->msg_buf, "\n", 1);
// Append new line to the user's message buffer
size_t msg_len = strlen(p_user->msg_buf);
size_t len = strlen(read_buf);
size_t max_chars = MAX_MSG_LEN - 1 - msg_len;
strncat(p_user->msg_buf, read_buf, max_chars);
printf("strlen(p_user->msg_buf)=%d\n", strlen(p_user->msg_buf));
return;
}
char ch = toupper(read_buf[0]);
// About
if (ch == 'A')
{
write_str(
socket_id,
"\n"
"~~~ UVM Mini BBS ~~~\n"
"An ephemeral BBS experiment to test out the UVM networking API,\n"
"and for me to practice C/socket programming.\n"
"The port number is 9001, because it's over nine thousands.\n"
"Messages are stored in RAM only, so if the server crashes\n"
"or restarts, everything is cleared.\n"
"Visit https://github.com/maximecb/uvm for more! :)\n"
"\n"
);
return;
}
// Exit/Quit
if (ch == 'E' || ch == 'Q')
{
puts("Got exit command");
char* response = "Goodbye!\n";
net_write(socket_id, response, strlen(response));
net_close(socket_id);
free_user(socket_id);
return;
}
// Post message
if (ch == 'P')
{
if (strlen(p_user->name) == 0)
{
p_user->state = STATE_PICK_NAME;
write_str(socket_id, "\n");
write_str(socket_id, "Please choose a username and then press the return key:\n");
}
else
{
p_user->state = STATE_WRITE_MSG;
write_post_help(socket_id);
}
return;
}
// Read message
if (ch == 'R')
{
if (num_messages == 0)
{
write_str(socket_id, "\n");
write_str(socket_id, "There are no messages to read. Enter (P) to write one!\n");
write_str(socket_id, "\n");
return;
}
p_user->state = STATE_READ_MSGS;
p_user->cur_index = (int)num_messages - 1;
view_cur_message(p_user);
return;
}
// Next message
if (ch == 'N' && p_user->state == STATE_READ_MSGS)
{
if (p_user->cur_index > 0)
p_user->cur_index = p_user->cur_index - 1;
else
p_user->cur_index = (int)num_messages - 1;
view_cur_message(p_user);
return;
}
//write_str(socket_id, "\nUnknown command\n\n");
write_help(socket_id);
}
void main()
{
start_time = time_current_ms();
puts("Starting TCP server");
listen_sock = net_listen("0.0.0.0:9001", on_new_conn);
enable_event_loop();
}