-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulticast_chat_client.c
390 lines (349 loc) · 9.99 KB
/
multicast_chat_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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <arpa/inet.h>
#include<netinet/in.h>
#define SA struct sockaddr
#define true 1
#define false 0
#define MAX_LEN 2047
const int port = 6000;
const char baseIP[] = "238.101";
int allow_loopback = 0;
int cpid; //variable to store child process id
struct course{
int comcode;
char code[12];
char *cname;
};
typedef struct course course;
course *courseList;
int numCourses;
void ps(char *str){
if(str)
fprintf(stderr,"%s\n",str);
else
fprintf(stderr,"null\n");
}
char *allocString(int size){
char *cstr = (char *)(calloc(size+1,sizeof(char)));
return cstr;
}
/*
* Tests whether strings s1 and s2 are equal
*/
int equals(char *s1, char *s2){
if(s1 == NULL && s2 == NULL)
return true;
else if(s1 == NULL || s2 == NULL)
return false;
else
return (strcmp(s1,s2) == 0);
}
void sighandlr(int signo){
kill(cpid,SIGKILL);
printf("Exiting...\n");
exit(0);
}
int parseCourseInfo(char *buf,course *cptr){
const char delim = ',';
int i=0;
while(buf[i] != '\0' && buf[i] != delim)
i++;
if(!buf[i])
return false;
buf[i] = 0;
sscanf(buf,"%d",&(cptr->comcode));
buf = buf+i+1;
i = 0;
while(buf[i] != '\0' && buf[i] != delim)
i++;
if(!buf[i])
return false;
buf[i] = 0;
strcpy((cptr->code),buf);
buf = buf+i+1;
i = 0;
while(buf[i] != '\n' && buf[i] != '\0')
i++;
buf[i] = '\0';
(cptr->cname) = allocString(i);
strcpy((cptr->cname),buf);
return true;
}
/*
* Format will be
* 2
* 1094,CS F211,DATA STRUCTURES & ALGORITHMS
* 1092,CS F213,OBJECT ORIENTED PROGRAMMING
*/
int readCourseData(char *cpath){
if(cpath == NULL)
return false;
int i;
FILE *fp = fopen(cpath,"r");
if(fp == NULL)
return false;
char buf[201];
if(fgets(buf,10,fp) == NULL)
return false;
sscanf(buf,"%d",&numCourses);
courseList = (course *)(calloc(numCourses,sizeof(course)));
for(i=0;i<numCourses;i++){
if(fgets(buf,200,fp) == NULL)
return false;
if(!parseCourseInfo(buf,courseList+i))
return false;
}
return true;
}
/*
* It gets a courseCode like "CS F211" and it returns index in courseList
*/
int getCourseIdx(char *courseCode){
int i;
//linear search (can be done by binary search if number of courses are binary)
for(i=0;i<numCourses;i++){
course *c = &(courseList[i]);
if(equals(courseCode,c->code)){
return i;
}
}
return -1;
}
char *genIpFromComCode(int comCode){
char *multicastIP = allocString(16);
sprintf(multicastIP,"%s.1%d.1%d",baseIP,comCode/100,comCode%100);
return multicastIP;
}
int joinGroup(int grpIdx, int sockfd){
course *c = &(courseList[grpIdx]);
char *gip = genIpFromComCode(c->comcode);
struct ip_mreq mreq;
struct sockaddr_in gstr;
if((gstr.sin_addr.s_addr = inet_addr(gip)) == -1){
free(gip);
return false;
}
mreq.imr_multiaddr = gstr.sin_addr;
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
if(setsockopt(sockfd,IPPROTO_IP,IP_ADD_MEMBERSHIP,(char *)&mreq,sizeof(mreq)) == -1){
ps("[ERROR]: Unable to join group.");
free(gip);
return false;
}
free(gip);
return true;
}
int leaveGroup(int grpIdx, int sockfd){
course *c = &(courseList[grpIdx]);
char *gip = genIpFromComCode(c->comcode);
struct ip_mreq mreq;
struct sockaddr_in gstr;
if((gstr.sin_addr.s_addr = inet_addr(gip)) == -1) {
free(gip);
return false;
}
mreq.imr_multiaddr = gstr.sin_addr;
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
if(setsockopt(sockfd,IPPROTO_IP,IP_DROP_MEMBERSHIP,(char *)&mreq,sizeof(mreq)) == -1){
perror("setsockopt(): IP_DROP_MEMBERSHIP:");
ps("[ERROR]: Unable to leave group.");
free(gip);
return false;
}
free(gip);
return true;
}
/*
* Create a socket, set various properties and return fd
*/
int createSocket(){
struct sockaddr_in addr;
int sock = socket(AF_INET,SOCK_DGRAM,0);
if(sock < 0)
return -1;
bzero(&addr,sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(port);
int val = 1;
if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0) {
perror("Reusing ADDR failed");
exit(1);
}
if(bind(sock,(struct sockaddr*)&addr,sizeof(addr)) < 0){
ps("[ERROR]: Unable to bind.");
return -1;
}
if(setsockopt(sock, IPPROTO_IP, IP_MULTICAST_LOOP, (char *) &allow_loopback,sizeof(allow_loopback)) == -1){
perror("[ERROR]: Unable to disable loopback");
}
int mc_all = 0;
setsockopt(sock, IPPROTO_IP, IP_MULTICAST_ALL, (void*) &mc_all, sizeof(mc_all));
return sock;
}
void generateMessage(char *buf, char *msg, char *courseCode){
strcpy(buf,"\n\n- - - - - - - - - - - - - - - - - - - -\n");
char headr[100];
sprintf(headr,"MESSAGE FROM %s",courseCode);
strcat(buf,headr);
strcat(buf,"\n- - - - - - - - - - - - - - - - - - - -\n");
int mlen = strlen(msg);
if(mlen > 0 && msg[mlen-1] == '\n')
msg[mlen-1] = '\0';
strcat(buf,msg);
strcat(buf,"\n- - - - - - - - - - - - - - - - - - - -\n");
}
int sendMessage(int grpIdx, int sockfd){
struct sockaddr_in addr;
char buf[MAX_LEN], msg[MAX_LEN];
course *c = &(courseList[grpIdx]);
char *destIp = genIpFromComCode(c->comcode);
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(destIp);
printf("Type the message to be sent to %s\n",c->code);
if(fgets(msg,MAX_LEN - 200,stdin) == NULL){
printf("[ERROR]: Unable to read stdin\n");
free(destIp);
return false;
}
generateMessage(buf,msg,c->code);
int len = strlen(buf);
if(sendto(sockfd,buf,len,0,(SA *) &addr,sizeof(addr)) < 0){
ps("[ERROR]: Unable to send.");
free(destIp);
return false;
}
free(destIp);
return true;
}
void getMessage(int sockfd){
int nr = 0;
char recvBuf[MAX_LEN] = {0};
for(;;){
nr = recv(sockfd,recvBuf,MAX_LEN,0);
if(nr < 0){
ps("[ERROR]: Difficulty in reading from the multicast socket.");
}
write(1,recvBuf,nr);
}
}
void displayMenu(){
printf("1. Send Message\n");
printf("2. Join new course\n");
printf("3. Leave a course\n");
printf("4. Exit\n: ");
}
void displayCourseMenu(int rcl[],int n){
int i;
for(i=0;i<n;i++){
course *c = &(courseList[rcl[i]]);
printf("%d. %s - %s\n",i,c->code,c->cname);
}
printf(": ");
}
void startApp(){
int sockfd =createSocket();
int rcl[10]; //registered course list
int rcli = 0;
memset(rcl,-1,sizeof rcl);
if(sockfd == -1)
exit(-1);
if((cpid=fork()) == 0){
getMessage(sockfd);
}
else{
signal(SIGINT,sighandlr);
for(;;){
displayMenu();
int ch;
scanf("%d",&ch);
if(ch == 1){
//send message
if(rcli == 0){
printf("[ERROR]: You don't have any courses.\n");
}
else{
displayCourseMenu(rcl,rcli);
scanf("%d",&ch);
getchar();
if(ch >= rcli){
printf("[ERROR]: Invalid Choice\n");
}
else{
if(!sendMessage(rcl[ch],sockfd)){
printf("[ERROR]: Failed sending message.\n");
}
printf("[INFO]: Message Sent.\n");
}
}
}
else if(ch == 2){
printf("Enter course code (eg. IS F462): ");
char buf[15] = {0};
getchar();
fgets(buf,15,stdin);
int len = strlen(buf);
if((len -1) >= 0 && buf[len-1] == '\n')
buf[--len] = '\0';
int idx = getCourseIdx(buf);
if(idx == -1){
printf("[ERROR]: No such course exists.\n");
}
else{
rcl[rcli++] = idx;
course *c = &(courseList[idx]);
if(!joinGroup(idx,sockfd)){
printf("[ERROR]: Unable to join multicast group.\n");
rcli--;
}
else{
printf("Successfully added %s - %s\n",c->code,c->cname);
}
}
}
else if(ch == 3){
if(rcli == 0){
printf("[ERROR]: You have no courses.\n");
}
else{
displayCourseMenu(rcl,rcli);
scanf("%d",&ch);
if(ch >= rcli){
printf("[ERROR]: Invalid Choice\n");
}
else{
if(leaveGroup(rcl[ch],sockfd)){
rcli--;
course *c = &(courseList[rcl[ch]]);
rcl[ch] = rcl[rcli];
printf("Successfully left the course %s - %s\n",c->code,c->cname);
}
}
}
}
else{
kill(cpid,SIGKILL);
exit(0);
}
}
}
}
int main(int argc, char *argv[]) {
if(argc < 2){
printf("[ERROR]: This program accepts one argument. Path to the course data file.\n");
return -1;
}
if(argc == 3){
//optional argument to allow/disallow loopback of messages
sscanf(argv[2],"%d",&allow_loopback);
}
if(!readCourseData(argv[1]))
return -1;
startApp();
return 0;
}