-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoteFileClient.cpp
548 lines (504 loc) · 16.5 KB
/
RemoteFileClient.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
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
/*
* Name: Johnson, Caleb
* Project: PA-2(Programming)
* Instructor: Feng Chen
* Class: cs7103-au20
* LogonID: cs710310
*/
#include <iostream>
#include <string>
#include <streambuf>
#include <fstream>
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#define PORT 8080
using namespace std;
/*
* Common Functions
*/
string IP = ""; //IP Address, set at beginning of main from IPAddress.txt
//Make socket connection, send message, get response
string SendAndGetResponse(string message)
{
int sock = 0, valread;
struct sockaddr_in serv_addr;
char buffer[1024] = {0};
sock = socket(AF_INET, SOCK_STREAM, 0);
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
inet_pton(AF_INET, IP.c_str(), &serv_addr.sin_addr);
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
cout << "Connection to server failed, program aborting." << endl;
abort();
}
char* charmsg = (char*)malloc(message.length());
for(int i = 0; i < message.length(); i++)
{
charmsg[i] = message[i];
}
send(sock, charmsg, message.length(), 0);
valread = read(sock, buffer, 1024);
string response(buffer);
return response;
}
//In each function, construct message
//First 32 chars of message is the function name
//Every argument after that each has its own 32 chars
//Therefore args must be padded out to 32 chars, also means usernames and filenames can't exceed 32 chars
bool connect(string username)
{
string function = "SignIn";
function.append(32 - function.length(), '\0');
string arg1 = username;
arg1.append(32 - arg1.length(), '\0');
string message = function + arg1;
return SendAndGetResponse(message) == "true";
}
string GetUserAndFiles(string username)
{
string function = "UserFiles";
function.append(32 - function.length(), '\0');
string arg1 = username;
arg1.append(32 - arg1.length(), '\0');
string message = function + arg1;
return SendAndGetResponse(message);
}
/*
* Admin Functions
*/
string ListAllUsers()
{
string function = "GetUsers";
function.append(32 - function.length(), '\0');
string message = function;
return SendAndGetResponse(message);
}
bool CreateUser(string username)
{
string function = "NewUser";
function.append(32 - function.length(), '\0');
string arg1 = username;
arg1.append(32 - arg1.length(), '\0');
string message = function + arg1;
return SendAndGetResponse(message) == "true";
}
bool DeleteUser(string username)
{
string function = "RemoveUser";
function.append(32 - function.length(), '\0');
string arg1 = username;
arg1.append(32 - arg1.length(), '\0');
string message = function + arg1;
return SendAndGetResponse(message) == "true";
}
string ListAllFiles()
{
string function = "ListFiles";
function.append(32 - function.length(), '\0');
string message = function;
string response = SendAndGetResponse(message);
return response;
}
string PrintFile(string filename)
{
string function = "ListFile";
function.append(32 - function.length(), '\0');
string arg1 = filename;
arg1.append(32 - arg1.length(), '\0');
string message = function + arg1;
return SendAndGetResponse(message);
}
bool DeleteFile(string filename)
{
string function = "RemoveFile";
function.append(32 - function.length(), '\0');
string arg1 = filename;
arg1.append(32 - arg1.length(), '\0');
string message = function + arg1;
return SendAndGetResponse(message) == "true";
}
bool ChangeOwner(string filename, string newowner)
{
string function = "ChangeFileOwner";
function.append(32 - function.length(), '\0');
string arg1 = filename;
arg1.append(32 - arg1.length(), '\0');
string arg2 = newowner;
arg2.append(32 - arg2.length(), '\0');
string message = function + arg1 + arg2;
return SendAndGetResponse(message) == "true";
}
/*
* User Functions
*/
bool UploadFile(string filecontents, string filename, string username)
{
string function = "SendFile";
function.append(32 - function.length(), '\0');
//For this one, the first argument will be the length of the file (arg4)
string arg1 = to_string(filecontents.length());
arg1.append(32 - arg1.length(), '\0');
string arg2 = filename;
arg2.append(32 - arg2.length(), '\0');
string arg3 = username;
arg3.append(32 - arg3.length(), '\0');
string arg4 = filecontents;
string message = function + arg1 + arg2 + arg3 + arg4;
return SendAndGetResponse(message) == "true";
}
string DownloadFile(string filename, string username)
{
string function = "GetFile";
function.append(32 - function.length(), '\0');
string arg1 = filename;
arg1.append(32 - arg1.length(), '\0');
string arg2 = username;
arg2.append(32 - arg2.length(), '\0');
string message = function + arg1 + arg2;
return SendAndGetResponse(message);
}
bool DeleteOwnedFile(string filename, string username)
{
string function = "RemoveOwned";
function.append(32 - function.length(), '\0');
string arg1 = filename;
arg1.append(32 - arg1.length(), '\0');
string arg2 = username;
arg2.append(32 - arg2.length(), '\0');
string message = function + arg1 + arg2;
return SendAndGetResponse(message) == "true";
}
bool TransferOwner(string filename, string originalowner, string newowner)
{
string function = "TransferOwnership";
function.append(32 - function.length(), '\0');
string arg1 = filename;
arg1.append(32 - arg1.length(), '\0');
string arg2 = originalowner;
arg2.append(32 - arg2.length(), '\0');
string arg3 = newowner;
arg3.append(32 - arg3.length(), '\0');
string message = function + arg1 + arg2 + arg3;
return SendAndGetResponse(message) == "true";
}
/*
* Frontend Functions
*/
int AdminMenu()
{
int choice;
while (true) //Repeat until a choice is returned
{
cout << endl;
cout << "1: List All Users" << endl;
cout << "2: List Files Owned by Specified User" << endl;
cout << "3: Create User" << endl;
cout << "4: Delete User" << endl;
cout << "5: List All Files" << endl;
cout << "6: Show Specified File and Owner " << endl;
cout << "7: Delete Specified File " << endl;
cout << "8: Transfer File Ownership " << endl;
cout << "9: Exit" << endl;
cout << "Enter choice: ";
cin >> choice;
if (choice >= 1 && choice <= 9)
return choice;
cout << "\"" << choice << "\" is not a valid option" << endl;
}
}
int UserMenu()
{
int choice;
while (true) //Repeat until a choice is returned
{
cout << endl;
cout << "1: List Owned Files" << endl;
cout << "2: Upload File" << endl;
cout << "3: Download Owned File" << endl;
cout << "4: Delete Owned File" << endl;
cout << "5: Transfer File Ownership " << endl;
cout << "6: Exit" << endl;
cout << "Enter choice: ";
cin >> choice;
if (choice >= 1 && choice <= 6)
return choice;
cout << "\"" << choice << "\" is not a valid option" << endl;
}
}
string EnterUser()
{
string user;
while (user.length() == 0 && user.length() < 33)
{
cout << "Enter specified user name (32 character max): " << endl;
cin >> user;
}
return user;
}
string EnterUserTransfer()
{
string user;
while (user.length() == 0 && user.length() < 33)
{
cout << "Enter specified user name for new owner (32 character max): " << endl;
cin >> user;
}
return user;
}
string EnterFile()
{
string filename;
while (filename.length() == 0 && filename.length() < 33)
{
cout << "Enter specified file name (including extension) (32 characters max): " << endl;
cin >> filename;
}
return filename;
}
int main()
{
//Get IP address from file
ifstream IPFile;
IPFile.open("IPAddress.txt");
IPFile >> IP;
IPFile.close();
//Sign in
cout << "Please enter your username: ";
string username;
cin >> username;
while (!connect(username))
{
cout << "User does not exist, please try again." << endl;
cin >> username;
}
cout << "Log in successful. Please choose to run in admin or user mode (type a for admin or u for user)." << endl;
string choice;
cin >> choice;
while(choice != "a" && choice != "u")
{
cout << "Please type a for admin mode or u for user mode." << endl;
cin >> choice;
}
if (choice == "a")
{
//Admin CMD
int choice = AdminMenu();
while (choice != 9)
{
if (choice == 1) //List all users
{
string users = ListAllUsers();
if(users == "none")
{
cout << "No users found on server." << endl;
}
else
{
cout << users << endl;
}
}
else if (choice == 2) //List all owned files for user
{
string user = EnterUser();
cout << "Owned files for user " + user + ": " << endl;
string ownedfiles = GetUserAndFiles(user);
if (ownedfiles == "none") //Nothing returned
{
cout << "No files found, either user does not exist or user owns no files." << endl;
}
else
{
cout << ownedfiles;
}
}
else if (choice == 3) //Create new user
{
string user = EnterUser();
bool created = CreateUser(user);
if (created)
{
cout << "User " + user + " successfully created." << endl;
}
else
{
cout << "User " + user + " already exists." << endl;
}
}
else if (choice == 4) //Delete user
{
string user = EnterUser();
bool deleted = DeleteUser(user);
if (deleted)
{
cout << "User " + user + " successfully deleted." << endl;
}
else
{
cout << "User " + user + " does not exist so can't be deleted." << endl;
}
}
else if (choice == 5) //List all files
{
string files = ListAllFiles();
if(files == "none")
{
cout << "No files found on server." << endl;
}
else
{
cout << files << endl;
}
}
else if (choice == 6) //List file and its owner
{
string filename = EnterFile();
string fileandowner = PrintFile(filename);
if (fileandowner == "none") //Nothing returned
{
cout << "File name " + filename + " not found." << endl;
}
else
{
cout << fileandowner << endl;
}
}
else if (choice == 7) //Delete a file
{
string filename = EnterFile();
bool deleted = DeleteFile(filename);
if (deleted)
{
cout << "File " + filename + " successfully deleted." << endl;
}
else
{
cout << "File " + filename + " does not exist so can't be deleted." << endl;
}
}
else if (choice == 8) //Change ownership of a file
{
string filename = EnterFile();
string user = EnterUserTransfer();
bool changed = ChangeOwner(filename, user);
if (changed)
{
cout << "File " + filename + " successfully transferred ownership to " + user + "." << endl;
}
else
{
cout << "Either file " + filename + " or user " + user + " does not exist so can't transfer ownership." << endl;
}
}
//Show menu and get next choice
choice = AdminMenu();
}
}
else
{
//User CMD
int choice = UserMenu();
while (choice != 6)
{
if (choice == 1) //List all owned files for user
{
cout << "Owned files for user " + username + ": " << endl;
string ownedfiles = GetUserAndFiles(username);
if (ownedfiles == "none") //Nothing returned
{
cout << "No owned files found." << endl;
}
else
{
cout << ownedfiles << endl;
}
}
else if (choice == 2) //Upload File
{
string filename = EnterFile();
//Get file from pc as char array
streampos size;
char* memblock;
string filecontents;
ifstream file(filename, ios::in | ios::binary | ios::ate);
if (file.is_open())
{
size = file.tellg();
memblock = new char[size];
file.seekg(0, ios::beg);
file.read(memblock, size);
file.close();
string tempcontents(memblock, size);
delete[] memblock;
filecontents = tempcontents;
}
//Upload to server
if (filecontents.length() == 0)
{
cout << "File " + filename + " either does not exist or is empty." << endl;
}
else
{
bool uploaded = UploadFile(filecontents, filename, username);
if (uploaded)
{
cout << "File " + filename + " successfully uploaded." << endl;
}
else
{
cout << "File " + filename + " already exists on server." << endl;
}
}
}
else if (choice == 3) //Download File
{
string filename = EnterFile();
string filecontents = DownloadFile(filename, username);
if (filecontents == "none") //Nothing returned
{
cout << "Either file does not exist or it is not owned by user " + username + "." << endl;
}
else
{
//Save file contents down as file name
ofstream out(filename);
out.write(filecontents.c_str(), filecontents.size());
out.close();
cout << "File " << filename << " downloaded successfully." << endl;
}
}
else if (choice == 4) //Delete a file
{
string filename = EnterFile();
bool deleted = DeleteOwnedFile(filename, username);
if (deleted)
{
cout << "File " + filename + " successfully deleted." << endl;
}
else
{
cout << "File " + filename + " either does not exist or is not owned by " + username + " so can't be deleted." << endl;
}
}
else if (choice == 5) //Change ownership of a file
{
string filename = EnterFile();
string user = EnterUserTransfer();
bool changed = TransferOwner(filename, username, user);
if (changed)
{
cout << "File " + filename + " successfully transferred ownership to " + user + "." << endl;
}
else
{
cout << "File " + filename + " either does not exist or is not owned by " + username + " or new owner " + user + " does not exist, so can't transfer ownership." << endl;
}
}
//Show menu and get next choice
choice = UserMenu();
}
}
}