-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
479 lines (373 loc) · 10 KB
/
server.c
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
/**
* Server for Chat
*
* Alexandre Martani <amartani at gmail com>
*
* References:
* - Threads: http://www.cs.cf.ac.uk/Dave/C/node32.html
*/
//#define DEBUG_THREADS
//#define DEBUG_CMD
#define DEBUG_CONNECTION
#define DEFAULT_PORT 8000
#define HEARTBEAT_TIMEOUT 20
#include "common.c"
#include "thread_helper.c"
#include <netinet/tcp.h>
// function prototypes
// --------- Data structure -----------
// ---- List of clients -----
typedef struct client_node client_node_t;
typedef struct clients_list clients_list_t;
struct client_node {
int sock;
string nick;
client_node_t *prev, *next;
pthread_t thread;
pthread_t watchdog;
};
struct clients_list {
pthread_rwlock_t *lock;
client_node_t *first, *last;
int size;
};
void initialize_clients_list();
client_node_t *insert_client(int sock);
// ---- Helper functions ----
string recv_string(int sock);
void send_string(int sock, string str);
void *do_chld(void *arg);
void select_command(int sock, client_node_t *client_node);
void set_client_socket_options(int sock);
void client_handle(int sock);
void coordinator_handle(int sock);
void disconnect_user(client_node_t *client_node);
void socket_error_handler();
void set_listening_socket_options(int sockfd);
void set_watchdog(client_node_t *client_node);
void *watchdog(void *arg);
void thread_cancel_if_not_self(pthread_t thread);
void send_message_to_destination(client_node_t *receiver, client_node_t *sender, string message);
// ---- commands ----
void cmd_set_nick(int sock, client_node_t *client_node);
void cmd_list(int sock);
void cmd_echo(int sock);
void cmd_heartbeat(client_node_t *client_node);
void cmd_quit(client_node_t *client_node);
void cmd_message(client_node_t *client_node);
void cmd_unknown(int sock);
// -------- Functions -----------
// ---- List of clients ----
clients_list_t *clients_list;
void initialize_clients_list()
{
clients_list = (clients_list_t*)malloc(sizeof(clients_list_t));
clients_list->first = NULL;
clients_list->last = NULL;
clients_list->size = 0;
// Initialize lock
clients_list->lock = new_lock();
}
client_node_t *insert_client(int sock)
{
client_node_t* new_client = (client_node_t*) malloc(sizeof(client_node_t));
new_client->sock = sock;
new_client->nick.size = 0;
new_client->nick.str = NULL;
new_client->prev = NULL;
new_client->next = NULL;
new_client->thread = pthread_self();
new_client->watchdog = 0;
#ifdef DEBUG_CONNECTION
puts("Client connecting..");
fflush(stdout);
#endif
// Lock client list for writer
wlock(clients_list->lock);
clients_list->size++;
if (clients_list->first == NULL) {
clients_list->first = new_client;
clients_list->last = new_client;
} else {
new_client->prev = clients_list->last;
clients_list->last->next = new_client;
clients_list->last = new_client;
}
// Unlock client list
unlock(clients_list->lock);
return new_client;
}
// main function
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno, clilen, res;
struct sockaddr_in serv_addr, cli_addr;
pthread_t chld_thr;
void *arg;
if (argc < 2) {
portno = DEFAULT_PORT;
} else {
portno = atoi(argv[1]);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
set_listening_socket_options(sockfd);
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd, 5);
initialize_clients_list();
// Loop forever accepting connections
while (1) {
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr,
&clilen);
if (newsockfd < 0)
error("ERROR on accept");
// Copy argument to be passed to do_chld
arg = malloc(sizeof(newsockfd));
memcpy(arg, &newsockfd, sizeof(newsockfd));
// Create new thread
res = pthread_create(&chld_thr, 0, do_chld, arg);
if (res)
{
error("ERROR creating thread");
}
}
return 0;
}
void set_listening_socket_options(int sockfd)
{
int optval;
// Avoid address in use errors
optval = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
}
// New threads calls this
void *do_chld(void *arg)
{
int sock = *((int*)arg);
char cmd;
free(arg);
recv_forced(sock, &cmd, sizeof(cmd));
switch(cmd) {
case 'C':
client_handle(sock);
break;
case 'S':
coordinator_handle(sock);
break;
default:
cmd_unknown(sock);
break;
}
}
// Set common options for client sockets
void set_client_socket_options(int sock)
{
struct timeval tv;
// Set timeout
tv.tv_sec = 0;
tv.tv_usec = 50000; /* 0.05 sec Timeout */
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval));
}
// Handle a connection with a coordinator
void coordinator_handle(int sock)
{
while (1) {
send_forced(sock, "S", 1);
sleep(3);
}
}
// Handle a connection with a client
void client_handle(int sock)
{
client_node_t *client_node = insert_client(sock);
set_client_socket_options(sock);
set_watchdog(client_node);
cmd_set_nick(sock, client_node);
while (1) {
select_command(sock, client_node);
}
}
// Listen to a command and execute
void select_command(int sock, client_node_t *client_node)
{
int n;
char command;
n = recv(sock, &command, sizeof(command), 0);
if (n < 1)
{
socket_error_handler();
return;
}
#ifdef DEBUG_CMD
printf("Recv command %c\n", command);
fflush(stdout);
#endif
switch (command) {
case 'C':
cmd_set_nick(sock, client_node);
break;
case 'L':
cmd_list(sock);
break;
case 'E':
cmd_echo(sock);
break;
case 'H':
cmd_heartbeat(client_node);
break;
case 'Q':
cmd_quit(client_node);
break;
case 'M':
cmd_message(client_node);
break;
default:
cmd_unknown(sock);
break;
}
return;
}
void socket_error_handler()
{
int serr = errno;
switch (errno)
{
case EAGAIN:
break;
default:
//perror("Socket error");
break;
}
}
// Disconnect user
void disconnect_user(client_node_t *client_node)
{
client_node_t *next, *prev;
#ifdef DEBUG_CONNECTION
printf("Disconnecting %s. -\n", client_node->nick.str);
fflush(stdout);
#endif
// Lock client list for writer
wlock(clients_list->lock);
// Kill threads
thread_cancel_if_not_self(client_node->thread);
thread_cancel_if_not_self(client_node->watchdog);
// Remove node from global list
clients_list->size --;
next = client_node->next;
prev = client_node->prev;
if (prev) prev->next = next;
if (next) next->prev = prev;
// Unlock client list
unlock(clients_list->lock);
// Close socket
close(client_node->sock);
// Free memory
free_string(client_node->nick);
free(client_node);
}
// commands
void cmd_set_nick(int sock, client_node_t *client_node)
{
string nick;
nick = recv_string(sock);
// Lock client list for writing
wlock(clients_list->lock);
free_string(client_node->nick);
client_node->nick = nick;
// Unlock
unlock(clients_list->lock);
}
void cmd_list(int sock)
{
int i;
client_node_t *client_node;
// Send command
send_forced(sock, "L", 1);
// Lock client list for reading
rlock(clients_list->lock);
// Send number of clients
send_forced(sock, &clients_list->size, sizeof(clients_list->size));
// Send nickname of each client
for (client_node = clients_list->first; client_node != NULL; client_node = client_node->next) {
send_string(sock, client_node->nick);
}
// Unlock
unlock(clients_list->lock);
}
void cmd_echo(int sock)
{
string str;
int res;
str = recv_string(sock);
send_forced(sock, "E", 1);
send_string(sock, str);
free_string(str);
}
void cmd_heartbeat(client_node_t *client_node)
{
set_watchdog(client_node);
}
void cmd_quit(client_node_t *client_node)
{
//thread_cancel_if_not_self(client_node->watchdog);
//thread_cancel_if_not_self(client_node->thread);
disconnect_user(client_node);
pthread_exit(NULL);
}
void cmd_message(client_node_t *client_node)
{
string message;
int sock = client_node->sock;
client_node_t *receiver;
message = recv_string(sock);
// Lock client list
rlock(clients_list->lock);
for (receiver = clients_list->first; receiver; receiver = receiver->next) {
if (receiver == client_node) continue;
send_message_to_destination(receiver, client_node, message);
}
// Unlock
unlock(clients_list->lock);
}
void cmd_unknown(int sock)
{
int n;
string msg = string_create("Desconhecido");
send_forced(sock, "E", 1);
send_string(sock, msg);
}
void set_watchdog(client_node_t *client_node)
{
if (client_node->watchdog) {
pthread_cancel(client_node->watchdog);
}
pthread_create(&client_node->watchdog, NULL, watchdog, (void*) client_node);
}
void *watchdog(void *arg)
{
client_node_t* client_node = (client_node_t*) arg;
sleep(HEARTBEAT_TIMEOUT);
disconnect_user(arg);
}
void thread_cancel_if_not_self(pthread_t thread)
{
if (thread == 0) return;
if (pthread_equal(thread, pthread_self()) == 0) return;
pthread_cancel(thread);
}
void send_message_to_destination(client_node_t *receiver, client_node_t *sender, string message)
{
send_forced(receiver->sock, "M", 1);
send_string(receiver->sock, sender->nick);
send_string(receiver->sock, message);
}