-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
302 lines (234 loc) · 6.26 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdbool.h>
#include <stdint.h>
#include <time.h>
#include <pthread.h>
#define MAX_CLIENTS 20
#define BUFFER_SIZE 200
#define MODULO 128
#define NAME_SIZE 20
// error
void error(const char *msg)
{
perror(msg);
exit(1);
}
// generate public key
uint8_t generate_public_key()
{
srand(time(0));
uint8_t public_key = rand()%MODULO;
return public_key;
}
// Encrypt function
char* encrypt(uint8_t public_key,char* message)
{
//printf("original message=%s\n",message);
int n = strlen(message);
char* temp = (char *)malloc(n+1);
strcpy(temp, message);
for (int i=0;i<n;i++){
char c = (temp[i] + public_key) % MODULO;
temp[i]=c;
}
//printf("Encrypted message=%s\n",temp);
return (char *)temp;
}
// Client structure
typedef struct
{
struct sockaddr_in address;
int sockfd;
int uid;
uint8_t public_key;
char name[NAME_SIZE];
} client_t;
// global variables
client_t *clients[MAX_CLIENTS];
static _Atomic unsigned int cli_count = 0;
static int uid = 10;
pthread_mutex_t clients_mutex = PTHREAD_MUTEX_INITIALIZER;
uint8_t public_key;
// 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);
}
// remove \n from string
void string_trimln(char* message)
{
char* first_newline = strchr(message, '\n');
if (first_newline)
*first_newline = '\0';
}
// add client to queue
void queue_add(client_t *client_structure)
{
pthread_mutex_lock(&clients_mutex);
for(int i=0; i < MAX_CLIENTS; ++i)
{
if(!clients[i])
{
clients[i] = client_structure;
break;
}
}
pthread_mutex_unlock(&clients_mutex);
}
// Remove clients from queue
void queue_remove(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 except sender
void send_message(char *message, int uid)
{
int n;
pthread_mutex_lock(&clients_mutex);
for(int i=0; i<MAX_CLIENTS; ++i)
{
if(clients[i])
{
if(clients[i]->uid != uid)
{
n = write(clients[i]->sockfd,message,strlen(message));
if (n < 0)
printf("ERROR writing to socket to user id=%d",uid);
}
}
}
pthread_mutex_unlock(&clients_mutex);
}
// Handle all communication with the client
void *handle_client(void *arg)
{
char buffer[BUFFER_SIZE];
char name[NAME_SIZE];
int n;
char *encrypted_message;
cli_count++;
client_t *client_structure = (client_t *)arg;
// send public_key to client
n = write( client_structure->sockfd ,&public_key,sizeof(uint8_t));
if (n < 0)
error("ERROR writing to socket");
// Read name of client
n = read( client_structure->sockfd, name , NAME_SIZE);
if (n < 0)
error("ERROR reading from socket");
string_trimln(name);
strcpy(client_structure->name, name);
sprintf(buffer, "%s has joined", client_structure->name);
printf("\n%s", buffer );
encrypted_message = encrypt(public_key,buffer);
send_message(encrypted_message, client_structure->uid);
while(1)
{
bzero(buffer,BUFFER_SIZE);
n = read(client_structure->sockfd , buffer, BUFFER_SIZE );
if (n < 0)
error("ERROR writing to socket");
int e = strncmp( "exit",buffer,4);
if( e!=0 && strlen(buffer) > 0 )
{
// printf("\n%s", buffer);
send_message(buffer, client_structure->uid);
}
else if ( e == 0 )
{
sprintf(buffer, "%s has left", client_structure->name);
printf("\n%s", buffer );
encrypted_message = encrypt(public_key,buffer);
send_message( encrypted_message, client_structure->uid);
break;
}
}
// Delete client from queue and yield thread
close(client_structure->sockfd);
queue_remove(client_structure->uid);
free(client_structure);
cli_count--;
pthread_detach(pthread_self());
return NULL;
}
// main funtion
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno,temp;
socklen_t clilen;
char buffer[100],server_name[20],client_name[20];
struct sockaddr_in serv_addr, cli_addr;
int n;
pthread_t tid;
if (argc < 2)
{
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
//build server address structure
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(portno);
//bind local port number
if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR on binding");
//specify number of concurrent
//clients to listen for
listen(sockfd,10);
public_key = generate_public_key();
printf("\n*********Chat room started*********\n");
while(1)
{
//Wait for client
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr,&clilen);
if (newsockfd < 0)
error("ERROR on accept");
else
printf("\nReceived connection from host [IP %s TCP port %d] \n",inet_ntoa(cli_addr.sin_addr),ntohs(cli_addr.sin_port));
if((cli_count + 1) == MAX_CLIENTS){
printf("Max clients reached. Rejected: ");
print_client_addr(cli_addr);
close(newsockfd);
continue;
}
client_t *client_structure = (client_t *)malloc(sizeof(client_t));
client_structure->address = cli_addr;
client_structure->sockfd = newsockfd;
client_structure->uid = uid++;
// Add client to the queue and fork thread
queue_add(client_structure);
pthread_create(&tid, NULL, &handle_client, (void*)client_structure);
/* Reduce CPU usage */
sleep(1);
}
return 0;
}