-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
281 lines (227 loc) · 8.03 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
// MIT License
// Copyright (c) 2022 Lucas Araújo
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <time.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdbool.h>
#include <pthread.h>
#define LENGTH 2048
#define MAX_CLIENTS 3
#define RESET "\e[0m"
#define RED "\e[1;31m"
#define GREEN "\e[1;32m"
#define YELLOW "\e[1;33m"
#define BLUE "\e[1;34m"
#define PINK "\e[1;35m"
#define CYAN "\e[1;36m"
#define GRAY "\e[1;90m"
#define DATE "\e[7;36m"
#define OPEN "\e[0;92m"
#define EXIT "\e[0;91m"
int user_id = 0;
unsigned int clients_n = 0;
struct tm *p;
time_t seconds;
typedef struct {
struct sockaddr_in address;
int user_id;
char name[30];
int client_socket;
} client_t;
client_t *clients[MAX_CLIENTS];
pthread_mutex_t clients_mutex = PTHREAD_MUTEX_INITIALIZER;
void clear(char *text, size_t length) {
for (size_t i = 0; i < length; i++) {
if (text[i] == '\n') {
text[i] = '\0';
return;
}
}
}
void date_and_time() {
time(&seconds);
p = localtime(&seconds);
char buffer[20];
memset(buffer, '\0', 20);
snprintf(buffer, 20, "%02d/%02d/%04d %02d:%02d:%02d",
p->tm_mday,
p->tm_mon + 1,
p->tm_year + 1900,
p->tm_hour,
p->tm_min,
p->tm_sec
);
printf(DATE "%s" RESET " ", buffer);
}
void client_address(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
);
}
void adds_client_to_queue(client_t *client) {
pthread_mutex_lock(&clients_mutex);
for (size_t i = 0; i < MAX_CLIENTS; i++) {
if (!clients[i]) {
clients[i] = client;
break;
}
}
pthread_mutex_unlock(&clients_mutex);
}
void removes_client_from_queue(int user_id) {
pthread_mutex_lock(&clients_mutex);
for (size_t i = 0; i < MAX_CLIENTS; i++) {
if (clients[i]) {
if (clients[i]->user_id == user_id) {
clients[i] = NULL;
break;
}
}
}
pthread_mutex_unlock(&clients_mutex);
}
void send_message(char *text, int user_id) {
pthread_mutex_lock(&clients_mutex);
for (size_t i = 0; i < MAX_CLIENTS; i++) {
if (clients[i]) {
if (clients[i]->user_id != user_id) {
if (write(clients[i]->client_socket, text, strlen(text)) == -1) {
fprintf(stderr, "[" RED "!" RESET "] An error occurred while sending the message to the users.\n");
break;
}
}
}
}
pthread_mutex_unlock(&clients_mutex);
}
void *processes_client_communication(void *client_object) {
bool flag = false;
char name[30], buffer[LENGTH];
clients_n++;
client_t *client = (client_t *) client_object;
if (recv(client->client_socket, name, 30, 0) <= 0 || strlen(name) < 3 || strlen(name) >= 28) {
printf("Didn't enter the name.\n");
flag = true;
}
else {
strncpy(client->name, name, 30);
sprintf(buffer, OPEN "%s has joined\n" RESET, client->name);
date_and_time();
printf("%s", buffer);
send_message(buffer, client->user_id);
}
memset(buffer, '\0', LENGTH);
while (true) {
if (flag) {
break;
}
int receive = recv(client->client_socket, buffer, LENGTH, 0);
if (receive > 0) {
if (strlen(buffer) > 0) {
send_message(buffer, client->user_id);
clear(buffer, strlen(buffer));
date_and_time();
printf("%s\n", buffer);
}
}
else if (!receive || !strcmp(buffer, "exit")) {
sprintf(buffer, EXIT "%s has left\n" RESET, client->name);
date_and_time();
printf("%s", buffer);
send_message(buffer, client->user_id);
flag = true;
}
else {
fprintf(stderr, "An error occurred while processing the communication.\n");
flag = true;
}
memset(buffer, '\0', LENGTH);
}
close(client->client_socket);
removes_client_from_queue(client->user_id);
free(client);
clients_n--;
pthread_detach(pthread_self());
return NULL;
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "[" RED "!" RESET "] Usage: %s <port>\n", argv[0]);
return EXIT_FAILURE;
}
char ip_address[] = "127.0.0.1";
int port = atoi(argv[1]);
int server_socket = 0;
int connection = 0;
int option = 1;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
server_socket = socket(AF_INET, SOCK_STREAM, 0);
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(ip_address);
server_addr.sin_port = htons(port);
signal(SIGPIPE, SIG_IGN);
if (setsockopt(server_socket, SOL_SOCKET, (SO_REUSEPORT | SO_REUSEADDR), (char *) &option, sizeof(option)) == -1) {
fprintf(stderr, "[" RED "!" RESET "] An error occurred while setting the socket options.\n");
return EXIT_FAILURE;
}
if (bind(server_socket, (struct sockaddr *) &server_addr, sizeof(server_addr)) == -1) {
fprintf(stderr, "[" RED "!" RESET "] An error occurred while linking a name to the socket.\n");
return EXIT_FAILURE;
}
if (listen(server_socket, 10) == -1) {
fprintf(stderr, "[" RED "!" RESET "] An error occurred while accepting connection requests from clients.\n");
return EXIT_FAILURE;
}
pthread_t thread_id;
printf(CYAN "┌─┐┬ ┬┌─┐┌┬┐ ┬─┐┌─┐┌─┐┌┬┐ \n"
"│ ├─┤├─┤ │ ├┬┘│ ││ ││││ \n"
"└─┘┴ ┴┴ ┴ ┴ ┴└─└─┘└─┘┴ ┴ " RESET "(server)\n"
"https://github.com/lucapwn \n");
printf("\nWaiting for clients...\n\n");
while (true) {
socklen_t client_l = sizeof(client_addr);
connection = accept(server_socket, (struct sockaddr *) &client_addr, &client_l);
if (clients_n >= MAX_CLIENTS) {
printf("The maximum customer quantity has been reached.\nClient rejected: ");
client_address(client_addr);
printf(":%d\n", client_addr.sin_port);
close(connection);
continue;
}
client_t *client = (client_t *) malloc(sizeof(client_t));
client->address = client_addr;
client->client_socket = connection;
client->user_id = user_id++;
adds_client_to_queue(client);
pthread_create(&thread_id, NULL, &processes_client_communication, (void *) client);
sleep(1);
}
return EXIT_SUCCESS;
}