-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracker.cpp
344 lines (328 loc) · 8.54 KB
/
tracker.cpp
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
#include <iostream>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <vector>
#include "readfiles.h"
#include "user_handler.h"
#include "group_handler.h"
#define CMD_SIZE 512
#define MAX_INC_CONN 5
typedef struct{
int sockfd;
struct sockaddr_in sock_addr;
socklen_t addr_len;
}connection_t;
void * process_req(void* arg){
connection_t* connection;
if(!arg) pthread_exit(0);
string g_id;
string u_id;
string passwd;
string ipv4;
string port;
string cmd_s;
string return_msg="";
int status;
group_t group;
char* ipv4_port = new char[CMD_SIZE];
char* cmd = new char[CMD_SIZE];
user_t* user=nullptr;
connection = (connection_t*)arg;
//read ip and port sent from client
if(read(connection->sockfd, ipv4_port, CMD_SIZE) <= 0){
cerr<<"Unresponsive peer\n";
}
else{
auto v = split(string(ipv4_port),':');//To Do error handling
ipv4 = v[0];
port = v[1];
while(read(connection->sockfd, cmd, CMD_SIZE)>0){
// fprintf(stdout, "%s\n",(char*)cmd);
return_msg = "";
cmd_s = (char*)cmd;
auto argv = split(cmd_s, ' ');
if(argv[0].compare("create_user")==0){
if(argv.size() != 3){
return_msg = "Incorrect command\n";
}
else{
u_id = argv[1];
passwd = argv[2];
if(user != nullptr){
return_msg = "Not permitted in Login state\nLogout first or start process from different shell\n";
}
else{
user= new user_t(u_id, passwd, ipv4, port);
if((status = user->create_user()) == SUCC){
return_msg = "New u_id:" + u_id + " created\n";
}
else if(status == DIR_NULL){
return_msg = "Not able to create user_data dir\n";
}
else{
return_msg = "User with u_id: " + u_id + " already exists!!\n";
}
delete user;
user = nullptr;
}
}
}//end of command
else if(argv[0].compare("login")==0){
if(argv.size() != 3){
return_msg = "Incorrect command\n";
}
else{
u_id = argv[1];
passwd = argv[2];
if(user != nullptr){
return_msg = "User logged in from different account\n";
return_msg += "Logout first or start process from different shell\n";
}
else{
user = new user_t(u_id, passwd, ipv4, port);
status = user->login_user();
switch(status){
case SUCC:
return_msg = "User: "+u_id+" logged in\n";
break;
case PRV_LOG:
return_msg = "User: " + u_id + " already logged in. You spy process >:#\n";
delete user;
user = nullptr;
break;
case INC_PSSWD:
return_msg = "Invalid Password\n";
delete user;
user = nullptr;
break;//To Do
//promt for passwd again
case LOGON_FAILED:
return_msg = "User: " + u_id + " No such user exists!!\n";
delete user;
user = nullptr;
break;
}
}
}
}//end of command
else if(argv[0].compare("create_group")==0){
if(argv.size() != 2){
return_msg = "Incorrect command\n";
}
else{
g_id = argv[1];
if(user == nullptr){
return_msg = "Login required\n";
}
else{
if((status = group.create_group(g_id, user->get_uid())) == SUCC){
return_msg = "Group: " + g_id + " created\n";
}
else if(status == PRV_GRP){
return_msg = "Group: " + g_id + " already exists!!\n";
}
else if(status == DIR_NULL){
return_msg = "Group: " + g_id + " metadata still exists in group_data\n";
}
}
}
}//end of command
else if(argv[0].compare("join_group")==0){
if(argv.size() != 2){
return_msg = "Incorrect command\n";
}
else{
g_id = argv[1];
if(user == nullptr){
return_msg = "Login required\n";
}
else{
if((status=group.join_group(g_id, user->get_uid())) == SUCC){
return_msg = "Group join request sent to admin\n";
}
else if(status == INV_GRP){
return_msg = "Invalid group request\n";
}
else if(status == DUP_REQ){
return_msg = "Duplicate group request\n";
}
else if(status == USR_EXISTS){
return_msg = "User: " + u_id + " already member\n";
}
}
}
}//end of command
else if(argv[0].compare("leave_group")==0){
}
else if(argv[0].compare("list_requests")==0){
if(argv.size() != 2){
return_msg = "Incorrect command\n";
}
else{
g_id = argv[1];
if(user == nullptr){
return_msg = "Login required\n";
}
else{
std::pair<int, std::vector<std::string>> it = group.list_requests(g_id, user->get_uid());
status = it.first;
if(status == SUCC){
if(!(it.second).empty()){
for(auto u : it.second){
return_msg += u;
}
}
else{
return_msg = "No pending Requests\n";
}
}
else if(status == PER_DEN){
return_msg = "User "+user->get_uid()+": insufficient privelleges\n";
}
else if(status == INV_GRP){
return_msg = "Group "+g_id+": doesn't exist!!\n";
}
}
}
}//end of command
else if(argv[0].compare("accept_request")==0){
if(argv.size() != 3){
return_msg = "Incorrect command\n";
}
else{
g_id = argv[1];
u_id = argv[2];
if(user == nullptr){
return_msg = "Login required\n";
}
else{
if((status = group.accept_request(g_id, user->get_uid(), u_id)) == SUCC){
return_msg = "Group "+g_id+": "+"added "+u_id+"\n";
}
else if(status == INV_GRP){
return_msg = "Group "+g_id+": doesn't exist!!\n";
}
else if(status == PER_DEN){
return_msg = "User "+user->get_uid()+": insufficient privelleges\n";
}
else if(status == INV_USR){
return_msg = "No request from"+u_id+"\n";
}
}
}
}
else if(argv[0].compare("list_groups")==0){
if(argv.size() != 1){
return_msg = "Incorrect command\n";
}
else{
std::vector<std::string> v;
if(user == nullptr){
return_msg = "Login required\n";
}
else{
v = group.list_groups();
if(v.empty()){
return_msg = "No group Exists!!\n";
}
else{
for(auto a: v){
return_msg += a + "\n";
}
}
}
}
}
else if(argv[0].compare("list_files")==0){
}
else if(argv[0].compare("upload_file")==0){
}
else if(argv[0].compare("download_file")==0){
}
else if(argv[0].compare("logout")==0){
if(argv.size() != 1){
return_msg = "Incorrect command\n";
}
else{
if(user != nullptr and (status = user->logout() != LOGON_FAILED)){
return_msg = "Logged out \n";
}
else{
return_msg = "Login required\n";
}
delete user;
user = nullptr;
}
}
else if(argv[0].compare("stop_share")==0){
}
else{
return_msg = "Incorrect command\n";
}
write(connection->sockfd, return_msg.c_str(), return_msg.size());
memset(cmd, '\0', CMD_SIZE);
}//end of while
}
close(connection->sockfd);
free(connection);
pthread_exit(0);
}
int main(int argc, char const *argv[]){
int welcomeSocket;
string filename;
int tracker_no;
struct sockaddr_in serverAddr;
connection_t* connection;
string port;
string ipv4;
pthread_t thread;
if(argc != 3){
cerr<<"usage: "<<argv[0]<<" tracker_info.txt tracker_no\n";
exit(1);
}
//reads ip and port of given tracker into port and ipv4
filename = argv[1];
tracker_no = atoi(argv[2]);
if(read_ipv4_port(filename, tracker_no, ipv4, port)<0){
cerr<<filename<<" Valid filename: tracker_info.txt\n";
exit(1);
}
welcomeSocket = socket(AF_INET, SOCK_STREAM, 0);
if(welcomeSocket <=0){
cerr<<argv[0]<<": error: cannot create socket\n";
exit(1);
}
bzero(&serverAddr, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(stoi(port));
inet_pton(AF_INET, ipv4.c_str(), &(serverAddr.sin_addr));
bind(welcomeSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
if(listen(welcomeSocket, MAX_INC_CONN)==0){
printf("Listening\n");
}
else{
perror("Listen error\n");
}
while(1){
connection = (connection_t*)malloc(sizeof(connection_t));
connection->sockfd = accept(welcomeSocket, (struct sockaddr* )&connection->sock_addr, &connection->addr_len);
if(connection->sockfd <= 0){
free(connection);
}
else{
pthread_create(&thread, NULL, process_req, (void *)connection);
// process_req((void *)&welcomeSocket);
// pthread_join(thread1, NULL);
pthread_detach(thread);
}
}
}