-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
367 lines (280 loc) · 7.97 KB
/
client.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
#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 <netdb.h>
#include <stdbool.h>
#include <stdint.h>
#include <time.h>
#include <pthread.h>
#define BUFFER_SIZE 200
#define MODULO 128
#define NAME_SIZE 20
//error
void error(const char *msg)
{
perror(msg);
printf("closing connection with the server\n");
exit(0);
}
// remove from string
void string_trimln(char* message)
{
char* first_newline = strchr(message, '\n');
if (first_newline)
*first_newline = '\0';
}
// generate public key
uint8_t generate_public_key()
{
srand(time(0));
uint8_t public_key = rand()%MODULO;
return public_key;
}
// generate private key
uint8_t generate_private_key(uint8_t public_key)
{
uint8_t key = MODULO - public_key;
return key;
}
// Encrypt function for messages
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;
}
// Decrypt funtion for messsages
void decrypt(uint8_t private_key,char* message)
{
//printf("message to de deccrypted=%s\n",message);
int n = strlen(message);
for (int i=0;i<n;i++){
char c = (message[i] + private_key) % MODULO;
message[i]=c;
}
//printf("message after decryption=%s\n",message);
}
// global variables
char name[NAME_SIZE];
int sockfd = 0;
pthread_t tmp_thread;
uint8_t my_public_key;
uint8_t my_private_key;
uint8_t user_public_key;
char user_name[NAME_SIZE];
// function for sending message
void str_overwrite_stdout()
{
printf("%s", ">");
fflush(stdout);
}
void *send_message()
{
char message[BUFFER_SIZE];
char buffer[BUFFER_SIZE + NAME_SIZE + 4];
char *encrypted_message;
while(1)
{
bzero(message,BUFFER_SIZE);
bzero(buffer,BUFFER_SIZE + NAME_SIZE + 4 );
str_overwrite_stdout();
fgets(message,BUFFER_SIZE, stdin);
string_trimln(message);
int i = strncmp("exit",message,4);
if(i==0)
{
write(sockfd,message,strlen(message));
fflush(stdout);
pthread_cancel(tmp_thread);
pthread_exit(NULL);
break;
}
sprintf(buffer, "%s : %s", name, message);
//printf("\nEntered message = %s",buffer);
//encrypt message
encrypted_message = encrypt(user_public_key,buffer);
int n = write(sockfd,encrypted_message,strlen(encrypted_message));
if (n < 0)
error("ERROR writing to socket");
}
}
// function for receiving message
void *recieve_message()
{
char message[BUFFER_SIZE];
tmp_thread = pthread_self();
while (1)
{
bzero(message, BUFFER_SIZE);
fflush(stdout);
// read message
int n = read( sockfd, message, BUFFER_SIZE );
if (n < 0)
error("ERROR reading from socket");
// decrypt
decrypt(my_private_key,message);
//printf("Message received");
printf("%s\n", message);
str_overwrite_stdout();
}
}
//get public, private keys and usernaame
void presteps_chat(int sockfd)
{
int n;
// enter your user name
printf("Enter your username : ");
fgets(name,NAME_SIZE,stdin);
string_trimln(name);
//send name
n = write(sockfd,name,strlen(name));
if (n < 0)
error("ERROR writing to socket");
my_public_key = generate_public_key();
my_private_key = generate_private_key(my_public_key);
// send public key
n = write(sockfd,&my_public_key,sizeof(uint8_t));
if (n < 0)
error("ERROR reading from socket");
}
// get active users and return choice
int get_users(int sockfd)
{
int n;
// read and print all users available
printf("\n*********Users available*********\n");
int i=0;
while (1)
{
char buffer[100];
int temp;
n = read(sockfd,&temp,sizeof(int));
if (n < 0)
error("ERROR reading from socket");
if(temp==-1)
break;
n=read(sockfd,buffer,100);
if (n < 0)
error("ERROR reading from socket");
string_trimln(buffer);
printf("[%d] Chat with %s\n",temp,buffer);
i++;
}
printf("\nUsers available for chat: %d\n",i);
printf("\n[0] Exit program\n");
printf("[1] Refresh\n");
int choice;
do
{
printf("\nEnter your choice(Ex. 1,2,0) : ");
scanf("%d",&choice);
} while (choice<0);
return choice;
}
// main funtion
int main(int argc, char *argv[])
{
int portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[100];
if (argc < 3)
{
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
// create stream socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL)
{
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
//build server address structure
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
//connect to server
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
else
printf("\n***********Chat App started successfully***********\n");
presteps_chat(sockfd);
int choice;
while(1)
{
choice=get_users(sockfd);
if(choice==0)
{
// send choice to server
n = write(sockfd,&choice,sizeof(int));
if (n < 0)
error("ERROR writing to socket");
break;
}
else if(choice!=1)
{
n = write(sockfd,&choice,sizeof(int));
if (n < 0)
error("ERROR writing to socket");
//read status from server
printf("\nWaiting for response from server.please wait\n");
int response;
n = read(sockfd,&response,sizeof(int));
if (n < 0)
error("ERROR reading from socket");
if(response==1)
{
// get user name of other user
n = read(sockfd,user_name,NAME_SIZE);
if (n < 0)
error("ERROR reading from socket");
string_trimln(user_name);
// get public key of other user
n = read(sockfd,&user_public_key,sizeof(uint8_t));
if (n < 0)
error("ERROR reading from socket");
printf("***********Chat with %s started***********",user_name);
pthread_t send_message_thread;
pthread_t recieve_message_thread;
pthread_create(&send_message_thread, NULL, &send_message, NULL);
pthread_create(&recieve_message_thread, NULL, &recieve_message, NULL);
pthread_join(send_message_thread,NULL);
pthread_join(recieve_message_thread,NULL);
}
else
{
if(response==2)
printf("\nTimeout.No response from user!!! Connection failed. please try again\n");
else if(response==3)
printf("\nUser busy or not available!!! Connection failed. please try again\n");
}
}
else
{
n = write(sockfd,&choice,sizeof(int));
if (n < 0)
error("ERROR writing to socket");
}
}
close(sockfd);
return 0;
}