-
Notifications
You must be signed in to change notification settings - Fork 40
/
chat_server.c
364 lines (321 loc) · 10.9 KB
/
chat_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
/*
* Copyright 2014 - 2020
*
* Author: Yorick de Wid
* Description: Simple chatroom in C
* License: GPLv3
* Version: 1.5
*/
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
#include <sys/types.h>
#include <signal.h>
#define MAX_CLIENTS 100
#define BUFFER_SZ 2048
static _Atomic unsigned int cli_count = 0;
static int uid = 10;
/* Client structure */
typedef struct {
struct sockaddr_in addr; /* Client remote address */
int connfd; /* Connection file descriptor */
int uid; /* Client unique identifier */
char name[32]; /* Client name */
} client_t;
client_t *clients[MAX_CLIENTS];
pthread_mutex_t clients_mutex = PTHREAD_MUTEX_INITIALIZER;
static char topic[BUFFER_SZ/2];
pthread_mutex_t topic_mutex = PTHREAD_MUTEX_INITIALIZER;
/* The 'strdup' function is not available in the C standard */
char *_strdup(const char *s) {
size_t size = strlen(s) + 1;
char *p = malloc(size);
if (p) {
memcpy(p, s, size);
}
return p;
}
/* Add client to queue */
void queue_add(client_t *cl){
pthread_mutex_lock(&clients_mutex);
for (int i = 0; i < MAX_CLIENTS; ++i) {
if (!clients[i]) {
clients[i] = cl;
break;
}
}
pthread_mutex_unlock(&clients_mutex);
}
/* Delete client from queue */
void queue_delete(int uid){
pthread_mutex_lock(&clients_mutex);
for (int i = 0; i < MAX_CLIENTS; ++i) {
if (clients[i]) {
if (clients[i]->uid == uid) {
clients[i] = NULL;
break;
}
}
}
pthread_mutex_unlock(&clients_mutex);
}
/* Send message to all clients but the sender */
void send_message(char *s, int uid){
pthread_mutex_lock(&clients_mutex);
for (int i = 0; i < MAX_CLIENTS; ++i) {
if (clients[i]) {
if (clients[i]->uid != uid) {
if (write(clients[i]->connfd, s, strlen(s)) < 0) {
perror("Write to descriptor failed");
break;
}
}
}
}
pthread_mutex_unlock(&clients_mutex);
}
/* Send message to all clients */
void send_message_all(char *s){
pthread_mutex_lock(&clients_mutex);
for (int i = 0; i <MAX_CLIENTS; ++i){
if (clients[i]) {
if (write(clients[i]->connfd, s, strlen(s)) < 0) {
perror("Write to descriptor failed");
break;
}
}
}
pthread_mutex_unlock(&clients_mutex);
}
/* Send message to sender */
void send_message_self(const char *s, int connfd){
if (write(connfd, s, strlen(s)) < 0) {
perror("Write to descriptor failed");
exit(-1);
}
}
/* Send message to client */
void send_message_client(char *s, int uid){
pthread_mutex_lock(&clients_mutex);
for (int i = 0; i < MAX_CLIENTS; ++i){
if (clients[i]) {
if (clients[i]->uid == uid) {
if (write(clients[i]->connfd, s, strlen(s))<0) {
perror("Write to descriptor failed");
break;
}
}
}
}
pthread_mutex_unlock(&clients_mutex);
}
/* Send list of active clients */
void send_active_clients(int connfd){
char s[64];
pthread_mutex_lock(&clients_mutex);
for (int i = 0; i < MAX_CLIENTS; ++i){
if (clients[i]) {
sprintf(s, "<< [%d] %s\r\n", clients[i]->uid, clients[i]->name);
send_message_self(s, connfd);
}
}
pthread_mutex_unlock(&clients_mutex);
}
/* Strip CRLF */
void strip_newline(char *s){
while (*s != '\0') {
if (*s == '\r' || *s == '\n') {
*s = '\0';
}
s++;
}
}
/* Print ip address */
void print_client_addr(struct sockaddr_in addr){
printf("%d.%d.%d.%d",
addr.sin_addr.s_addr & 0xff,
(addr.sin_addr.s_addr & 0xff00) >> 8,
(addr.sin_addr.s_addr & 0xff0000) >> 16,
(addr.sin_addr.s_addr & 0xff000000) >> 24);
}
/* Handle all communication with the client */
void *handle_client(void *arg){
char buff_out[BUFFER_SZ];
char buff_in[BUFFER_SZ / 2];
int rlen;
cli_count++;
client_t *cli = (client_t *)arg;
printf("<< accept ");
print_client_addr(cli->addr);
printf(" referenced by %d\n", cli->uid);
sprintf(buff_out, "<< %s has joined\r\n", cli->name);
send_message_all(buff_out);
pthread_mutex_lock(&topic_mutex);
if (strlen(topic)) {
buff_out[0] = '\0';
sprintf(buff_out, "<< topic: %s\r\n", topic);
send_message_self(buff_out, cli->connfd);
}
pthread_mutex_unlock(&topic_mutex);
send_message_self("<< see /help for assistance\r\n", cli->connfd);
/* Receive input from client */
while ((rlen = read(cli->connfd, buff_in, sizeof(buff_in) - 1)) > 0) {
buff_in[rlen] = '\0';
buff_out[0] = '\0';
strip_newline(buff_in);
/* Ignore empty buffer */
if (!strlen(buff_in)) {
continue;
}
/* Special options */
if (buff_in[0] == '/') {
char *command, *param;
command = strtok(buff_in," ");
if (!strcmp(command, "/quit")) {
break;
} else if (!strcmp(command, "/ping")) {
send_message_self("<< pong\r\n", cli->connfd);
} else if (!strcmp(command, "/topic")) {
param = strtok(NULL, " ");
if (param) {
pthread_mutex_lock(&topic_mutex);
topic[0] = '\0';
while (param != NULL) {
strcat(topic, param);
strcat(topic, " ");
param = strtok(NULL, " ");
}
pthread_mutex_unlock(&topic_mutex);
sprintf(buff_out, "<< topic changed to: %s \r\n", topic);
send_message_all(buff_out);
} else {
send_message_self("<< message cannot be null\r\n", cli->connfd);
}
} else if (!strcmp(command, "/nick")) {
param = strtok(NULL, " ");
if (param) {
char *old_name = _strdup(cli->name);
if (!old_name) {
perror("Cannot allocate memory");
continue;
}
strncpy(cli->name, param, sizeof(cli->name));
cli->name[sizeof(cli->name)-1] = '\0';
sprintf(buff_out, "<< %s is now known as %s\r\n", old_name, cli->name);
free(old_name);
send_message_all(buff_out);
} else {
send_message_self("<< name cannot be null\r\n", cli->connfd);
}
} else if (!strcmp(command, "/msg")) {
param = strtok(NULL, " ");
if (param) {
int uid = atoi(param);
param = strtok(NULL, " ");
if (param) {
sprintf(buff_out, "[PM][%s]", cli->name);
while (param != NULL) {
strcat(buff_out, " ");
strcat(buff_out, param);
param = strtok(NULL, " ");
}
strcat(buff_out, "\r\n");
send_message_client(buff_out, uid);
} else {
send_message_self("<< message cannot be null\r\n", cli->connfd);
}
} else {
send_message_self("<< reference cannot be null\r\n", cli->connfd);
}
} else if(!strcmp(command, "/list")) {
sprintf(buff_out, "<< clients %d\r\n", cli_count);
send_message_self(buff_out, cli->connfd);
send_active_clients(cli->connfd);
} else if (!strcmp(command, "/help")) {
strcat(buff_out, "<< /quit Quit chatroom\r\n");
strcat(buff_out, "<< /ping Server test\r\n");
strcat(buff_out, "<< /topic <message> Set chat topic\r\n");
strcat(buff_out, "<< /nick <name> Change nickname\r\n");
strcat(buff_out, "<< /msg <reference> <message> Send private message\r\n");
strcat(buff_out, "<< /list Show active clients\r\n");
strcat(buff_out, "<< /help Show help\r\n");
send_message_self(buff_out, cli->connfd);
} else {
send_message_self("<< unknown command\r\n", cli->connfd);
}
} else {
/* Send message */
snprintf(buff_out, sizeof(buff_out), "[%s] %s\r\n", cli->name, buff_in);
send_message(buff_out, cli->uid);
}
}
/* Close connection */
sprintf(buff_out, "<< %s has left\r\n", cli->name);
send_message_all(buff_out);
close(cli->connfd);
/* Delete client from queue and yield thread */
queue_delete(cli->uid);
printf("<< quit ");
print_client_addr(cli->addr);
printf(" referenced by %d\n", cli->uid);
free(cli);
cli_count--;
pthread_detach(pthread_self());
return NULL;
}
int main(int argc, char *argv[]){
int listenfd = 0, connfd = 0;
struct sockaddr_in serv_addr;
struct sockaddr_in cli_addr;
pthread_t tid;
/* Socket settings */
listenfd = socket(AF_INET, SOCK_STREAM, 0);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(5000);
/* Ignore pipe signals */
signal(SIGPIPE, SIG_IGN);
/* Bind */
if (bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
perror("Socket binding failed");
return EXIT_FAILURE;
}
/* Listen */
if (listen(listenfd, 10) < 0) {
perror("Socket listening failed");
return EXIT_FAILURE;
}
printf("<[ SERVER STARTED ]>\n");
/* Accept clients */
while (1) {
socklen_t clilen = sizeof(cli_addr);
connfd = accept(listenfd, (struct sockaddr*)&cli_addr, &clilen);
/* Check if max clients is reached */
if ((cli_count + 1) == MAX_CLIENTS) {
printf("<< max clients reached\n");
printf("<< reject ");
print_client_addr(cli_addr);
printf("\n");
close(connfd);
continue;
}
/* Client settings */
client_t *cli = (client_t *)malloc(sizeof(client_t));
cli->addr = cli_addr;
cli->connfd = connfd;
cli->uid = uid++;
sprintf(cli->name, "%d", cli->uid);
/* Add client to the queue and fork thread */
queue_add(cli);
pthread_create(&tid, NULL, &handle_client, (void*)cli);
/* Reduce CPU usage */
sleep(1);
}
return EXIT_SUCCESS;
}