-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.c
289 lines (256 loc) · 7.84 KB
/
app.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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdbool.h>
#include <string.h>
#include <arpa/inet.h>
#include <netdb.h>
#define SIZE 1024
#define TIMEOUT 0
#define STDIN 0
// Struct to define every user
typedef struct detail {
char name[SIZE];
char ip[SIZE];
int port;
} User;
// Gets all user data into User type array
void readUserData(FILE* fp, User users[], int num_users) {
char line[SIZE];
for (int i = 0; i < num_users; i++) {
fgets(line, SIZE, fp);
sscanf(line, "%s %s %d", users[i].name, users[i].ip, &users[i].port);
}
return;
}
// Gets number of users from the first line of file
int getTotalUsers(FILE* fp) {
char line[SIZE];
int res;
fgets(line, SIZE, fp);
sscanf(line, "%d", &res);
return res;
}
// Checks if port entered by user is in user list
char* welcomeAndIdentity(int svrport, User users[], int num_users) {
printf("Welcome to Peer Chat!\n\nThese are the people available to chat:\n");
int x = -1;
for (int i = 0; i < num_users; i++) {
if (svrport != users[i].port) printf("%s\n", users[i].name);
else x = i;
}
if (x == -1) {
fprintf(stderr, "[-] Entered port not found in users list.\n");
exit(1);
}
printf("You are %s. Start chatting!\n", users[x].name);
return users[x].name;
}
char** parseMessage(char* buff) {
char** res = malloc( sizeof(char*) * 2 );
res[0] = malloc(SIZE);
res[1] = malloc(SIZE);
int len = strlen(buff), x = 0;
for (int i = 0; i < len; i++) {
if (buff[i] == '/') {
res[0][i] = '\0';
x = i + 1;
}
else if (x == 0) res[0][i] = buff[i];
else res[1][i - x] = buff[i];
}
res[1][len - x] = '\0';
return res;
}
// Free memory from memory parsing
void freeMemory(char** detail) {
free(detail[0]);
free(detail[1]);
free(detail);
return;
}
// Send message to person mentioned
void sendMessageToPerson(char* buff, char* identity, User users[], int num_users) {
int sockfd, peer_port;
struct sockaddr_in peeraddr;
struct hostent* peer;
char hostname[20];
bool personExists = false;
char** detail = parseMessage(buff);
for (int i = 0; i < num_users; i++) {
if (strcmp(detail[0], users[i].name) == 0) {
peer_port = users[i].port;
strcpy(hostname, users[i].ip);
personExists = true;
break;
}
}
if (!personExists) {
printf("[-] Such a person does not exist.\n[-] Invalid format.\nTry again!\n\n");
return;
}
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
fprintf(stderr, "[-] Error in opeing socket.\n");
exit(1);
}
if ( (peer = gethostbyname(hostname)) == NULL ) {
fprintf(stderr, "[-] Error: No host found for given peer.\n");
return;
}
bzero(&peeraddr, sizeof(peeraddr));
peeraddr.sin_family = AF_INET;
bcopy((char*) peer -> h_addr, (char*) &peeraddr.sin_addr.s_addr, peer -> h_length);
peeraddr.sin_port = htons(peer_port);
if ( connect(sockfd, (struct sockaddr*) &peeraddr, sizeof(peeraddr)) < 0 ) {
fprintf(stderr, "[-] Peer is offline.\n\n");
return;
}
char message[2 * SIZE];
strcpy(message, identity);
strcat(message, "/");
strcat(message, detail[1]);
if ( write(sockfd, message, strlen(message)) < 0 ) {
fprintf(stderr, "[-] Error writing to socket.\n");
return;
}
freeMemory(detail);
close(sockfd);
return;
}
// Function to control chat
void startChat(int svrsock, User users[], int num_users, struct sockaddr_in serveraddr, socklen_t len, char* identity) {
fd_set masterfds, readfds;
struct timeval timeout;
int maxfd, peersock;
size_t size = SIZE;
bool exit_flag = false;
char *buff = malloc(SIZE);
char** detail;
FD_ZERO(&masterfds);
FD_SET(svrsock, &masterfds);
FD_SET(STDIN, &masterfds);
maxfd = svrsock;
while (true) {
timeout.tv_sec = TIMEOUT;
timeout.tv_usec = 0;
memcpy(&readfds, &masterfds, sizeof(readfds));
int result = select(maxfd + 1, &readfds, NULL, NULL, &timeout);
if (result == 0 && exit_flag) {
/* After TIMEOUT amount of time after entering
quit/exit as input, the program enters here */
printf("Do you want to exit application? [Y/N]: ");
getline(&buff, &size, stdin);
if (!strcmp(buff, "Y\n") || !strcmp(buff, "y\n")) break;
if (strlen(buff) != 2 || (strcmp(buff, "N\n") && strcmp(buff, "n\n")))
printf("[-] Invalid Character Input. Try quitting again.\n");
exit_flag = false;
printf("\n");
} else if (result < 0 && errno != EINTR) {
/* select() error */
fprintf(stderr, "[-] Error in select() function\n");
exit(1);
} else if (result > 0) {
/* If Server Socket is ready, accept connection */
if ( FD_ISSET(svrsock, &readfds) ) {
peersock = accept(svrsock, (struct sockaddr*) &serveraddr, &len);
if ( peersock < 0 ) {
fprintf(stderr, "[-] Error in accept().\n");
} else {
FD_SET(peersock, &masterfds);
maxfd = (maxfd < peersock)? peersock: maxfd;
}
// Remove Server Socket from readfds
FD_CLR(svrsock, &readfds);
}
/* If stdin is ready, send message to person, if person exists, else continue */
if ( FD_ISSET(STDIN, &readfds) ) {
getline(&buff, &size, stdin);
printf("\n");
// If user wants to exit, allow and continue
if ( !strcmp(buff, "exit\n") || !strcmp(buff, "quit\n") ) {
exit_flag = true;
continue;
}
sendMessageToPerson(buff, identity, users, num_users);
continue;
}
/* Check for all the other sockets which might be ready to get read from */
for (int i = 1; i <= maxfd; i++) {
if ( FD_ISSET(i, &readfds) ) {
int res = recv(i, buff, SIZE, 0);
if (res > 0) {
buff[res] = '\0';
detail = parseMessage(buff);
printf("%s: %s\n", detail[0], detail[1]);
} else if (res == 0) { // EOF
// Close and clear socket
close(i);
FD_CLR(i, &masterfds);
} else {
fprintf(stderr, "[-] Error in recv().\n");
exit(1);
}
}
}
}
}
freeMemory(detail);
return;
}
int main(int argc, char** argv) {
/* CHECK NUMBER OF ARGUMENTS */
if (argc != 2) {
fprintf(stderr, "USAGE: %s <YOUR PORT>\n", argv[0]);
exit(1);
}
/* OPEN USER INFORMATION FILE */
FILE* fp;
fp = fopen("USERS.txt", "r");
if (fp == NULL) {
fprintf(stderr, "[-] Error opening USERS.txt file.\n");
exit(1);
}
/* READ USER DATA */
int num_users = getTotalUsers(fp);
User users[num_users];
readUserData(fp, users, num_users);
fclose(fp);
/* START APPLICATION SERVER */
int svrport = atoi(argv[1]);
int svrsock = socket(AF_INET, SOCK_STREAM, 0);
if (svrsock < 0) {
fprintf(stderr, "[-] Error opening server socket.\n");
exit(1);
}
struct sockaddr_in serveraddr;
bzero(&serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET; // IPv4 Address Family
serveraddr.sin_port = htons((unsigned short) svrport); // Set port to bind
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY); // Accept connections from all IPs of machine
socklen_t len = sizeof(serveraddr);
// Bind socket to port
if ( bind(svrsock, (struct sockaddr*) &serveraddr, sizeof(serveraddr)) < 0 ) {
fprintf(stderr, "[-] Unsuccessful binding!\n");
exit(1);
}
// Listen for connection requests (MAX = 5)
if ( listen(svrsock, 5) < 0 ) {
fprintf(stderr, "[-] Error in listening!\n");
exit(1);
}
printf("\n[+] Server Running! Start Conversation!\n");
// Check if the port mentioned in arguments exists in user list
char* identity = welcomeAndIdentity(svrport, users, num_users);
printf("\nEnter 'quit' or 'exit' for exiting application.\n");
printf("\nUse the following format for messaging:\nreceiver_name/message\n\n");
/* START CHAT */
startChat(svrsock, users, num_users, serveraddr, len, identity);
return 0;
}