-
Notifications
You must be signed in to change notification settings - Fork 1
/
group_comm.c
616 lines (534 loc) · 21.3 KB
/
group_comm.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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
#include "basic.h"
#include "hashmap.c"
#define BUFLEN 512
#define PORT 33777
#define BROADCAST_PORT 33333
#define MAX_GROUP_LIMIT 10
#define MAX_FILE_LIMIT 30
#define BROADCAST_TIMEOUT 1
void readLine(char* msg) {
char in = 'a';
int i = 0;
while(in != '\n') {
in = getc(stdin);
if(in != '\n') msg[i++] = in;
}
msg[i] = 0;
}
void genMulticastIP(char* ip) {
ip = (char*)malloc(16);
strcpy(ip, "239.0.0.");
char l[4] = {0};
int a = (rand() % 254) + 1;
sprintf(l, "%d", a);
strcat(ip, l);
}
void sendGrpMsg(char* dest_ip, char msg[BUFLEN]) {
struct sockaddr_in si_dest;
int slen = sizeof(si_dest);
memset((char*) &si_dest, 0, slen);
si_dest.sin_family = AF_INET;
si_dest.sin_port = htons(PORT);
si_dest.sin_addr.s_addr = inet_addr(dest_ip);
}
/*
Message Formats:
B - Broadcast Message
G - Group Message
P - Poll Message
F - Filelist Request Message
R - Filelist Reply Message
C - File Data Request Message
D - File Data Reply Message
S - File Transfer Special Message
*/
/*
Use alarm signal for syncing files between group members
*/
hashmap *grpip, *filelist;
int sockfd_m;
void syncfiles() {
for(int i = 0; i < grpip->capacity; ++i) {
bucket_node* temp = grpip->buckets[i];
while(temp != NULL) {
group* dest_ip = (struct group*)malloc(sizeof(struct group));
dest_ip = get(grpip, temp->key);
char msg[2] = "F:";
struct sockaddr_in si_other;
memset((char*) &si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(PORT);
si_other.sin_addr.s_addr = inet_addr(dest_ip->val);
if(sendto(sockfd_m, msg, strlen(msg), 0, (struct sockaddr*) &si_other, sizeof(si_other)) == -1) {
printf("Error in sending group message.\n");
exit(1);
}
temp = temp->next;
}
}
alarm(60);
}
typedef struct pthread_args{
int sockfd_m;
int sockfd_b;
hashmap* grpip;
hashmap* filelist;
}pthread_args;
// Running on separate thread
static void* handleMessages(void* arg) {
pthread_args* args = (pthread_args*)arg;
int sockfd_m = args->sockfd_m;
int sockfd_b = args->sockfd_b;
hashmap* grpip = args->grpip;
hashmap* filelist = args->filelist;
struct sockaddr_in clAddr;
socklen_t len = sizeof(struct sockaddr_in);
bzero(&clAddr, sizeof(struct sockaddr_in));
while(1){
char recvbuf[BUFLEN] = {0};
int retval;
fd_set rfds;
// struct timeval tv;
// tv.tv_sec = BROADCAST_TIMEOUT;
// tv.tv_usec = 0;
FD_ZERO(&rfds);
FD_SET(sockfd_m, &rfds);
int maxfd = sockfd_m;
retval = select(maxfd + 1, &rfds, NULL, NULL, NULL);
if(retval == -1 && errno == EINTR) continue;
if(FD_ISSET(sockfd_m, &rfds)){
len = sizeof(struct sockaddr_in);
recvfrom(sockfd_m, recvbuf, BUFLEN, 0, (struct sockaddr*) &clAddr, &len);
if(recvbuf[0] == 'B' && recvbuf[1] == ':'){
char sendbuf[50] = {0};
strcpy(sendbuf, recvbuf + 2);
group* g = get(grpip, sendbuf);
char ip[16] = {0};
strcpy(ip, g->val);
strcat(sendbuf, ":");
strcat(sendbuf, ip);
strcat(sendbuf, "\n");
if(sendto(sockfd_b, sendbuf, strlen(sendbuf), 0, (struct sockaddr*) &clAddr, len) == -1) {
printf("Error in sending broadcast message.\n");
exit(1);
}
}
else if(recvbuf[0] == 'G' && recvbuf[1] == ':') { // Display the received group message
char addr[20] = {0};
inet_ntop(AF_INET, &(clAddr.sin_addr.s_addr), addr, 20);
bucket_node *temp;
for(int i = 0; i < MAX_GROUP_LIMIT; i++){
if(grpip->buckets[i]){
temp = grpip->buckets[i];
while(temp != NULL){
if(!strcmp((temp->val).val, addr)){
break;
}
temp = temp->next;
}
}
}
printf("Message recieved for group : %s\n", temp->key);
printf("%s", recvbuf + 2);
} else if(recvbuf[0] == 'F' && recvbuf[1] == ':') { // Handle the file list request message
/*
Message Format:
F:
*/
char msg[500] = "R:";
struct sockaddr_in ip;
socklen_t iplen = sizeof(ip);
char cl_ip[16] = {0};
getsockname(sockfd_m, (struct sockaddr*)&ip, &iplen);
inet_ntop(AF_INET, &(ip.sin_addr), cl_ip, INET_ADDRSTRLEN);
strcat(msg, cl_ip);
strcat(msg, ":");
for(int i = 0; i < filelist->capacity; ++i) {
bucket_node* temp = filelist->buckets[i];
while(temp != NULL) {
strcat(msg, temp->key);
strcat(msg, ":");
temp = temp->next;
}
}
if(sendto(sockfd_m, msg, strlen(msg), 0, (struct sockaddr*) &clAddr, len) == -1) {
printf("Error in sending filelist reply message.\n");
}
} else if(recvbuf[0] == 'R' && recvbuf[1] == ':') { // Add files received in message to filelist
/*
Message Format:
R:client_ip:file1:file2:file3:...
*/
char cl_ip[16] = {0};
strcpy(cl_ip, recvbuf + 2);
char* token = strtok(recvbuf, ":");
strcpy(cl_ip, strtok(NULL, ":"));
token = strtok(NULL, ":");
while(token) {
if(!has_key(filelist, token)) insert2(filelist, token, cl_ip);
token = strtok(NULL, ":");
}
} else if(recvbuf[0] == 'S' && recvbuf[1] == ':') {
/* If client has file requested in special message:
- Send it to the requester
If client doesn't have file requested in special message:
- Forward this special message to all groups this client is a member of
*/
char fname[30];
char req_ip[16];
char* token = strtok(recvbuf, ":");
strcpy(fname, strtok(NULL, ":"));
if(has_key(filelist, fname)) {
strcpy(req_ip, strtok(NULL, ":"));
FILE *fptr;
if((fptr = fopen(fname, "r")) == NULL) {
printf("Error in opening file.\n");
exit(1);
}
/*
Message Format:
D:filename:data
*/
char data[BUFLEN] = "D:";
strcat(data, fname);
strcat(data, ":");
char * line = NULL;
size_t len = 0;
ssize_t read;
while ((read = getline(&line, &len, fptr)) != -1) {
strcat(data, line);
}
fclose(fptr);
struct sockaddr_in si_other;
memset((char*) &si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(PORT);
si_other.sin_addr.s_addr = inet_addr(req_ip);
if(sendto(sockfd_m, data, strlen(data), 0, (struct sockaddr*) &si_other, sizeof(si_other)) == -1) {
printf("Error in sending file data reply message.\n");
exit(1);
}
} else {
for(int i = 0; i < grpip->capacity; ++i) {
bucket_node* temp = grpip->buckets[i];
while(temp) {
group *dest_ip = (struct group*)malloc(sizeof(struct group));
dest_ip = get(grpip, temp->key);
struct sockaddr_in si_other;
memset((char*) &si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(PORT);
si_other.sin_addr.s_addr = inet_addr(dest_ip->val);
if(sendto(sockfd_m, recvbuf, strlen(recvbuf), 0, (struct sockaddr*) &si_other, sizeof(si_other)) == -1) {
printf("Error in forwarding data transfer special message.\n");
}
temp = temp->next;
}
}
}
} else if(recvbuf[0] == 'D' && recvbuf[1] == ':') {
// Save received files data
char fname[30] = {0};
char* token = strtok(recvbuf, ":");
strcpy(fname, strtok(NULL, ":"));
FILE *fptr = fopen(fname, "w");
if(fptr == NULL) {
printf("Error in writing data to file.\n");
exit(1);
}
fprintf(fptr, "%s", strtok(NULL, ":"));
fclose(fptr);
}
}
}
}
int main() {
signal(SIGALRM, syncfiles);
srand(time(0));
char buf[BUFLEN] = {0};
char message[BUFLEN] = {0};
if((sockfd_m = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
printf("Error in creating multicast socket.\n");
exit(1);
}
// Set socket for broadcast
int on = 1;
int sockfd_b;
if((sockfd_b = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
printf("Error in creating broadcast socket.\n");
exit(1);
}
setsockopt(sockfd_b, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
grpip = (hashmap*)malloc(sizeof(hashmap));
filelist = (hashmap*)malloc(sizeof(hashmap));
create_hm(grpip, MAX_GROUP_LIMIT);
create_hm(filelist, MAX_FILE_LIMIT);
// printf("%s\n", genMulticastIP());
pthread_t thread_id;
pthread_args args = {sockfd_m, sockfd_b, grpip, filelist};
pthread_create(&thread_id, NULL, &handleMessages, (void*)&args);
int b;
struct sockaddr_in my_addr;
bzero(&my_addr, sizeof(struct sockaddr_in));
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(PORT);
printf("Here\n");
my_addr.sin_addr.s_addr = inet_addr("localhost");
printf("Here\n");
if((b = bind(sockfd_m, (struct sockaddr*)&my_addr, sizeof(struct sockaddr_in))) < 0){
perror("BIND ERROR");
exit(EXIT_FAILURE);
}
int ch = 0;
while(1) {
printf("------------------------\n");
printf(" MENU \n");
printf("------------------------\n");
printf("1. Create a group\n");
printf("2. Search for a group\n");
printf("3. Join a group\n");
printf("4. Send a group message\n");
printf("5. View file list\n");
printf("6. Download a file\n");
printf("7. Start a group poll\n");
printf("8. Exit\n");
printf("Choose an option: ");
scanf("%d", &ch);
if(ch == 1) {
char in[30];
char* mulip;
printf("Enter name of group to create: ");
readLine(in);
genMulticastIP(mulip);
group g;
strcpy(g.val, mulip);
g.is_member = true;
insert(grpip, in, g);
struct ip_mreq mreq;
mreq.imr_multiaddr.s_addr = inet_addr(mulip);
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
if(setsockopt(sockfd_m, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
printf("Error in creating group..\n");
exit(1);
}
printf("Group created..\n");
}else if(ch == 2) {
struct sockaddr_in si_other;
memset((char*) &si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(PORT);
si_other.sin_addr.s_addr = inet_addr("255.255.255.255");
/*
Message Format:
B:keyword
*/
char keyw[20], msg[22];
strcpy(msg, "B:");
printf("Enter keyword to search for groups: ");
scanf("%s", keyw);
strcat(msg, keyw);
if(sendto(sockfd_m, msg, strlen(msg), 0, (struct sockaddr*) &si_other, sizeof(si_other)) == -1) {
printf("Error in sending broadcast message.\n");
exit(1);
}
char recvbuf[BUFLEN] = {0};
struct sockaddr_in clAddr;
socklen_t len = sizeof(struct sockaddr_in);
bzero(&clAddr, sizeof(struct sockaddr_in));
int retval;
fd_set rfds;
for(;;){
struct timeval tv;
tv.tv_sec = BROADCAST_TIMEOUT;
tv.tv_usec = 0;
FD_ZERO(&rfds);
FD_SET(sockfd_b, &rfds);
int maxfd = sockfd_b;
retval = select(maxfd + 1, &rfds, NULL, NULL, &tv);
if(retval == -1 && errno == EINTR) continue;
if(retval == 0) break;
recvfrom(sockfd_b, recvbuf, BUFLEN, 0, (struct sockaddr*) &clAddr, &len);
char key[20] = {0};
int i;
for(i = 0; recvbuf[i] != ':' && recvbuf[i]; i++){
key[i] = recvbuf[i];
}
group g;
bzero(&g, sizeof(g));
i++;
for(; recvbuf[i]; i++){
g.val[i] = recvbuf[i];
}
g.is_member = false;
if(!has_key(grpip, key)){
insert(grpip, key, g);
}
printf("Group : %s , Group IP : %s\n", key, g.val);
}
}else if(ch == 3){
// Group list hashmap is displayed, user can chose which group to join from this list
// To find more groups, search option can be used
char gname[20] = {0};
printf("Enter group name to join : ");
readLine(gname);
group* g = get(grpip, gname);
struct ip_mreq mreq;
mreq.imr_multiaddr.s_addr = inet_addr(g->val);
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
if(setsockopt(sockfd_m, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
printf("Error in joining group.\n");
exit(1);
}
printf("Successfully joined group.\n");
} else if(ch == 4) {
/*
Message Format:
G:message
*/
char gname[20], txtmsg[30], msg[32] = "G:";
printf("Enter group name to send message to: ");
readLine(gname);
printf("Enter the message: ");
readLine(txtmsg);
strcat(msg, txtmsg);
group* dest_ip = (struct group*)malloc(sizeof(struct group));
dest_ip = get(grpip, gname);
struct sockaddr_in si_other;
memset((char*) &si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(PORT);
si_other.sin_addr.s_addr = inet_addr(dest_ip->val);
if(sendto(sockfd_m, msg, strlen(msg), 0, (struct sockaddr*) &si_other, sizeof(si_other)) == -1) {
printf("Error in sending group message.\n");
exit(1);
}
} else if(ch == 5) {
// Read local files from disk and add to filelist, then display filelist
struct sockaddr_in ip;
socklen_t iplen = sizeof(ip);
char cl_ip[16] = {0};
getsockname(sockfd_m, (struct sockaddr*)&ip, &iplen);
inet_ntop(AF_INET, &(ip.sin_addr), cl_ip, INET_ADDRSTRLEN);
DIR *d = opendir(".");
struct dirent* dir;
if(d) {
while((dir = readdir(d)) != NULL) {
if(!has_key(filelist, dir->d_name)) insert2(filelist, dir->d_name, cl_ip);
}
}
printf("Filelist:\n");
for(int i = 0; i < filelist->capacity; ++i) {
bucket_node* temp = filelist->buckets[i];
while(temp) {
printf("%s\n", temp->key);
temp = temp->next;
}
}
} else if(ch == 6) {
// Search for filename in local list first, if file is not in list, send special message to all group members of groups the user belongs to
char fname[20];
printf("Enter file name to download: ");
readLine(fname);
char msg[32] = "C:";
strcat(msg, fname);
if(has_key(filelist, fname)) {
// File is present in local list
/*
Message Format:
C:filename
*/
char* dest_ip = (char*)malloc(16);
dest_ip = get2(filelist, fname);
struct sockaddr_in si_other;
memset((char*) &si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(PORT);
si_other.sin_addr.s_addr = inet_addr(dest_ip);
if(sendto(sockfd_m, msg, strlen(msg), 0, (struct sockaddr*) &si_other, sizeof(si_other)) == -1) {
printf("Error in sending file request message.\n");
exit(1);
}
} else {
// Send special message
/*
Message Format:
S:filename:local_ip
If this special message is circulated in multiple groups, we need to keep track of the original requester of the file
*/
struct sockaddr_in ip;
socklen_t iplen = sizeof(ip);
char cl_ip[16] = {0};
getsockname(sockfd_m, (struct sockaddr*)&ip, &iplen);
inet_ntop(AF_INET, &(ip.sin_addr), cl_ip, INET_ADDRSTRLEN);
strcat(msg, cl_ip);
for(int i = 0; i < grpip->capacity; ++i) {
bucket_node* temp = grpip->buckets[i];
while(temp) {
group* dest_ip = (struct group*)malloc(sizeof(struct group));
dest_ip = get(grpip, temp->key);
struct sockaddr_in si_other;
memset((char*) &si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(PORT);
si_other.sin_addr.s_addr = inet_addr(dest_ip->val);
if(sendto(sockfd_m, msg, strlen(msg), 0, (struct sockaddr*) &si_other, sizeof(si_other)) == -1) {
printf("Error in sending file transfer special message.\n");
exit(1);
}
temp = temp->next;
}
}
}
} else if(ch == 7) {
char gname[20], pmsg[20];
char** options;
int i = 0;
options[i] = (char*)malloc(20);
printf("Enter group to send poll message to: ");
readLine(gname);
printf("Enter poll question: ");
readLine(pmsg);
char in[20];
while(1) {
printf("Enter poll option(%d): ", i + 1);
readLine(in);
strcpy(options[i], in);
printf("Add more options? (Y/N): ");
readLine(in);
if(!strcmp(in, "Y")) {
options[++i] = (char*)malloc(20);
} else break;
}
/*
Message Format:
P:poll_question:poll_option_1:poll_option_2...
*/
char msg[300] = "P:";
strcat(msg, pmsg);
for(int j = 0; j < i + 1; ++j) {
strcat(msg, options[j]);
}
printf("%s\n", msg);
group* dest_ip = (struct group*)malloc(sizeof(struct group));
dest_ip = get(grpip, gname);
struct sockaddr_in si_other;
memset((char*) &si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(PORT);
si_other.sin_addr.s_addr = inet_addr(dest_ip->val);
if(sendto(sockfd_m, msg, strlen(msg), 0, (struct sockaddr*) &si_other, sizeof(si_other)) == -1) {
printf("Error in sending group poll.\n");
exit(1);
}
// Handle replies
} else if(ch == 8) {
break;
} else {
printf("Invalid option, try again.\n");
}
}
printf("Exiting...\n");
close(sockfd_m);
free_hm(grpip);
return 0;
}