Advertisement
Guest User

Untitled

a guest
Jun 15th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.40 KB | None | 0 0
  1. #include "Function.h"
  2. string receiveMessage(CSocket& sock) {
  3. char buffer;
  4. int byteReceived = 0;
  5. string res;
  6. do {
  7. byteReceived = sock.Receive(&buffer, 1, 0);
  8. if (byteReceived == -1) {
  9. //cout << "Error when receiving server data" << endl;
  10. return "Error";
  11. }
  12. else {
  13. res += buffer;
  14. }
  15. } while (buffer!='\n');
  16. return res;
  17. }
  18.  
  19. int getMessageCode(string message) {
  20. stringstream is(message);
  21. string code;
  22. is >> code;
  23. return stoi(code);
  24. }
  25.  
  26. void logIn(CSocket& sock) {
  27. string username, pass;
  28. int code = 0;
  29. do {
  30. cout << "-->Username: ";
  31. getline(cin, username);
  32. username = "user " + username + "\r\n";
  33. sock.Send((char*)username.c_str(), username.length());
  34. cout << receiveMessage(sock);
  35. cout << "-->Password: ";
  36. getline(cin, pass);
  37. pass = "pass " + pass + "\r\n";
  38. sock.Send((char*)pass.c_str(), pass.length());
  39. string message = receiveMessage(sock);
  40. cout << message;
  41. code = getMessageCode(message);
  42. } while (code != 230);
  43. }
  44.  
  45. bool getFile(CSocket& sock,CSocket& ClientData, string filename,bool passive) {
  46. while (filename == "") {
  47. cout << "Download file: ";
  48. getline(cin, filename);
  49. deleteSpaces(filename);
  50. }
  51. string stringSend = "RETR " + filename + "\r\n";
  52. sock.Send((char*)stringSend.c_str(), stringSend.length());
  53. string command = receiveMessage(sock);
  54. cout << command;
  55. CSocket activeSock;
  56. bool success = false;
  57. int code = getMessageCode(command);
  58. if (code == 150 || code ==125) {
  59. if (!passive) {
  60. ClientData.Accept(activeSock);
  61. }
  62. //Open file
  63. ofstream f;
  64. int pos = filename.find("/");
  65. filename = filename.substr(pos + 1);
  66. f.open(filename, ios::out | ios::binary);
  67. //Data tranfer
  68. char buffer[MAX_BUFFER];
  69. int receivedLen = 0;
  70. do {
  71. if (passive) {
  72. receivedLen = ClientData.Receive(buffer, MAX_BUFFER, 0);
  73. }
  74. else {
  75. receivedLen = activeSock.Receive(buffer, MAX_BUFFER, 0);
  76. }
  77. if (receivedLen == -1) {
  78. cout << "Error when receiving server data" << endl;
  79. break;
  80. }
  81. else {
  82. f.write(buffer, receivedLen);
  83. }
  84. } while (receivedLen > 0);
  85. f.close();
  86. cout << receiveMessage(sock);
  87. success = true;
  88. }
  89. if (!passive)activeSock.Close();
  90. ClientData.Close();
  91. return success;
  92. }
  93.  
  94. void changeServerPath(CSocket& sock, string path) {
  95. while (path == "") {
  96. cout << "Remote directory: ";
  97. getline(cin, path);
  98. deleteSpaces(path);
  99. }
  100. string command = "cwd " + path + "\r\n";
  101. sock.Send((char*)command.c_str(), command.length());
  102. cout << receiveMessage(sock);
  103. }
  104.  
  105. void changeClientPath(string path) {
  106. while (path == "") {
  107. cout << "Local directory: ";
  108. getline(cin, path);
  109. deleteSpaces(path);
  110. }
  111. int res = SetCurrentDirectoryA(path.c_str());
  112. if (res != 0) {
  113. char buf[256];
  114. GetCurrentDirectoryA(256, buf);
  115. cout << "Successfully change directory" << endl;
  116. cout << "Current dir is " + string(buf) << endl;
  117. }
  118. else {
  119. cout << "The system cannot find the path specified" << endl;
  120. }
  121. }
  122.  
  123. string getParameter(stringstream& is, string type) {
  124. string temp = is.str();
  125. string command;
  126. deleteSpaces(temp);
  127. if (temp == type) command = "";
  128. else is >> command;
  129. return command;
  130. }
  131.  
  132. //Connect to data port
  133. bool connectDataPort(CSocket& sock,CSocket& ClientData, bool passive) {
  134. if (passive) {
  135. ClientData.Create();
  136. string stringSend;
  137. string PASV = "PASV\r\n";
  138.  
  139. sock.Send((char*)PASV.c_str(), PASV.length());
  140. string message = receiveMessage(sock);
  141. //String return if succeed: "227 Entering Passive Mode (a1,a2,a3,a4,p1,p2)"
  142. //N = p1 * 256 + p2 IP: a1.a2.a3.a4
  143.  
  144. cout << message;
  145. string IP;
  146. int port;
  147.  
  148. //Xử lý tách chuỗi a1 a2 ....
  149. int pos = message.find('(');
  150. string c = message.substr(pos);
  151. string b(c.begin() + 1, c.end() - 1);
  152. vector<string> tokens;
  153. stringstream k(b);
  154.  
  155. string temp;
  156.  
  157. while (getline(k, temp, ',')) {
  158. tokens.push_back(temp);
  159. }
  160.  
  161. IP = tokens[0] + '.' + tokens[1] + '.' + tokens[2] + '.' + tokens[3];
  162. port = stoi(tokens[4]) * 256 + stoi(tokens[5]);
  163.  
  164.  
  165. const char* msg = (const char*)IP.c_str();
  166. wchar_t *wmsg = new wchar_t[strlen(msg) + 1];
  167. mbstowcs(wmsg, msg, strlen(msg) + 1);
  168.  
  169. if (ClientData.Connect(wmsg, port) == 0) {
  170. cout << "Can't connect to FTP server" << endl;
  171. return false;
  172. }
  173. //else {
  174. // cout << "Successfully connect to FTP server" << endl;
  175. //}
  176. }
  177. else {
  178. ClientData.Create();
  179. ClientData.Listen();
  180. sockaddr_in add;
  181. int addlen = sizeof(add);
  182. sock.GetSockName((sockaddr*)&add, &addlen);
  183.  
  184. string portCommand;
  185. //Create ip address string
  186. int a = add.sin_addr.S_un.S_un_b.s_b1;
  187. portCommand += to_string(a) + ",";
  188. a = add.sin_addr.S_un.S_un_b.s_b2;
  189. portCommand += to_string(a) + ",";
  190. a = add.sin_addr.S_un.S_un_b.s_b3;
  191. portCommand += to_string(a) + ",";
  192. a = add.sin_addr.S_un.S_un_b.s_b4;
  193. portCommand += to_string(a) + ",";
  194. //Append port to command
  195. ClientData.GetSockName((sockaddr*)&add, &addlen);
  196. int port = ntohs(add.sin_port);
  197. portCommand += to_string(port / 256) + ",";
  198. portCommand += to_string(port % 256);
  199. portCommand = "port " + portCommand + "\r\n";
  200. sock.Send(portCommand.c_str(), portCommand.length());
  201. string res= receiveMessage(sock);
  202. cout << res;
  203. }
  204. return true;
  205. }
  206.  
  207. bool uploadFile(CSocket& sock, CSocket &data, string filename,bool passive) {
  208. while (filename == "") {
  209. cout << "Upload file: ";
  210. getline(cin, filename);
  211. deleteSpaces(filename);
  212. }
  213. //Open file
  214. ifstream is;
  215. is.open(filename,ios::binary | ios::in);
  216. bool success = false;
  217. if (is.good()) {
  218. is.seekg(0, is.end);
  219. int lengthFile = is.tellg();
  220. is.seekg(0, is.beg);
  221. string stringSend = "STOR " + filename + "\r\n";
  222. sock.Send((char*)stringSend.c_str(), stringSend.length());
  223. CSocket activeSock;
  224. if (!passive)data.Accept(activeSock);
  225. cout << receiveMessage(sock);
  226.  
  227. //Data tranfer
  228. char* buffer = new char[lengthFile];
  229. int receivedLen = 0;
  230. is.read(buffer, lengthFile);
  231. if (passive) {
  232. receivedLen = data.Send(buffer, lengthFile, 0);
  233. }
  234. else {
  235. receivedLen = activeSock.Send(buffer, lengthFile, 0);
  236. }
  237. if (receivedLen == -1) {
  238. cout << "Error when sending server data" << endl;
  239. }
  240. //close
  241. is.close();
  242. if (!passive)activeSock.Close();
  243. data.Close();
  244. delete[]buffer;
  245. cout << receiveMessage(sock);
  246. success = true;
  247. }
  248. else {
  249. cout<<"There is no file name "<<filename<<endl;
  250. }
  251. return success;
  252. }
  253.  
  254. void deleteFile(CSocket& sock, string filename) {
  255. while (filename == "") {
  256. cout << "Delete file: ";
  257. getline(cin, filename);
  258. deleteSpaces(filename);
  259. }
  260. string stringSend = "DELE " + filename + "\r\n";
  261. sock.Send((char*)stringSend.c_str(), stringSend.length());
  262. cout << receiveMessage(sock);
  263. }
  264.  
  265. void createFolder(CSocket& sock, string folderName) {
  266. while (folderName == "") {
  267. cout << "New folder: ";
  268. getline(cin, folderName);
  269. deleteSpaces(folderName);
  270. }
  271. string stringSend = "MKD " + folderName + "\r\n";
  272. sock.Send((char*)stringSend.c_str(), stringSend.length());
  273. cout << receiveMessage(sock);
  274. }
  275.  
  276. void deleteEmptyFolder(CSocket& sock, string folderName) {
  277. while (folderName == "") {
  278. cout << "Delete empty folder: ";
  279. getline(cin, folderName);
  280. deleteSpaces(folderName);
  281. }
  282. string stringSend = "RMD " + folderName + "\r\n";
  283. sock.Send((char*)stringSend.c_str(), stringSend.length());
  284. cout << receiveMessage(sock);
  285. }
  286.  
  287. string getFileListDetail(CSocket& sock, CSocket& ClientData,string folderName, bool passive) {
  288. string stringSend = "list "+ folderName +"\r\n";
  289. sock.Send((char*)stringSend.c_str(), stringSend.length());
  290. string command = receiveMessage(sock);
  291. cout << command;
  292. int code = getMessageCode(command);
  293. string res = "";
  294. if (code == 150 || code == 125) {
  295. CSocket activeSock;
  296. //Get data from server
  297. if (!passive) {
  298. ClientData.Accept(activeSock);
  299. }
  300.  
  301. //Data tranfer
  302. char buffer[MAX_BUFFER];
  303. int receivedLen = 0;
  304. do {
  305. if (passive) {
  306. receivedLen = ClientData.Receive(buffer, MAX_BUFFER, 0);
  307. }
  308. else {
  309. receivedLen = activeSock.Receive(buffer, MAX_BUFFER, 0);
  310. }
  311. if (receivedLen == -1) {
  312. cout << "Error when receiving server data" << endl;
  313. break;
  314. }
  315. else {
  316. res.append(buffer);
  317. }
  318. } while (receivedLen > 0);
  319. res = res.substr(res.length() / 2);
  320. while (res.length()>0 && res[res.length() - 1] != '\n') res.pop_back();
  321. cout << receiveMessage(sock);
  322. if (!passive)activeSock.Close();
  323. ClientData.Close();
  324. }
  325. if (res == "0\r")res = "";
  326. return res;
  327. }
  328.  
  329. bool getFileList(CSocket& sock, CSocket& ClientData, string folderName, bool passive, vector<string>& result) {
  330. string stringSend = "nlst " + folderName + "\r\n";
  331. sock.Send((char*)stringSend.c_str(), stringSend.length());
  332. string command = receiveMessage(sock);
  333. //cout << command;
  334. int code = getMessageCode(command);
  335. string res = "";
  336. if (code == 150 || code ==125) {
  337. CSocket activeSock;
  338. //Get data from server
  339. if (!passive) {
  340. ClientData.Accept(activeSock);
  341. }
  342.  
  343. //Data tranfer
  344. char buffer[MAX_BUFFER];
  345. int receivedLen = 0;
  346. do {
  347. if (passive) {
  348. receivedLen = ClientData.Receive(buffer, MAX_BUFFER, 0);
  349. }
  350. else {
  351. receivedLen = activeSock.Receive(buffer, MAX_BUFFER, 0);
  352. }
  353. if (receivedLen == -1) {
  354. cout << "Error when receiving server data" << endl;
  355. break;
  356. }
  357. else {
  358. res.append(buffer);
  359. }
  360. } while (receivedLen > 0);
  361. res = res.substr(res.length() / 2);
  362. while (res.length()>0 && res[res.length() - 1] != '\n') res.pop_back();
  363. cout << receiveMessage(sock);
  364. if (!passive)activeSock.Close();
  365. ClientData.Close();
  366. }
  367. if (res == "0")res = "";
  368. string temp = "";
  369. for (int i = 0; i < res.length(); i++) {
  370. if (res[i] == '\r') {
  371. i++;
  372. result.push_back(temp);
  373. temp = "";
  374. }
  375. else {
  376. temp += res[i];
  377. }
  378. }
  379. if (result.size() > 0) {
  380. if (result[0] == folderName) {
  381. return false;
  382. }
  383. }
  384. return true;;
  385. }
  386.  
  387. void deleteMultipleFiles(CSocket& sock, CSocket& ClientData, CSocket& activeSock, stringstream& is, bool passive) {
  388. string fullCommand = is.str();
  389. if (fullCommand == "mdelete") {
  390. string temp = "";
  391. while (temp == "") {
  392. cout << "Files and folders to delete: " << endl;
  393. getline(cin, temp);
  394. is = stringstream(temp);
  395. }
  396. }
  397. string name;
  398. while (!is.eof()) {
  399. is >> name;
  400. cout << "Type y to delete file" << endl;
  401. if (name != "") {
  402. bool isFolder;
  403. vector<string>fileList;
  404. if (passive) {
  405. connectDataPort(sock, ClientData, passive);
  406. isFolder = getFileList(sock, ClientData, name, passive,fileList);
  407. }
  408. else {
  409. connectDataPort(sock, activeSock, passive);
  410. isFolder = getFileList(sock, activeSock, name, passive,fileList);
  411. }
  412. if (isFolder) {
  413. for (int i = 0; i < fileList.size(); i++) {
  414. cout << "Delete " << fileList[i] << " Y/N?";
  415. string answer;
  416. getline(cin, answer);
  417. deleteSpaces(answer);
  418. if (answer != "n" && answer != "N") {
  419. deleteFile(sock, fileList[i]);
  420. }
  421. }
  422. }
  423. else {
  424. cout << "Delete " << name << " Y/N?";
  425. string answer;
  426. getline(cin, answer);
  427. deleteSpaces(answer);
  428. if (answer != "n" && answer != "N") {
  429. deleteFile(sock, name);
  430. }
  431. }
  432. }
  433. }
  434. }
  435.  
  436. void deleteSpaces(string& s) {
  437. while (s[s.length() - 1] == ' ')s.pop_back();
  438. while (s[0] == ' ')s.erase(s.begin(), s.begin() + 1);
  439. }
  440.  
  441. void getMultipleFiles(CSocket& sock, CSocket& ClientData,CSocket& activeSock, stringstream& is, bool passive) {
  442. string fullCommand = is.str();
  443. if (fullCommand == "mget") {
  444. string temp = "";
  445. while (temp == "") {
  446. cout << "Files and folders to download: " << endl;
  447. getline(cin, temp);
  448. is = stringstream(temp);
  449. }
  450. }
  451. string name;
  452. while (!is.eof()) {
  453. is >> name;
  454. cout << "Type y to download file" << endl;
  455. if (name != "") {
  456. bool isFolder;
  457. vector<string>fileList;
  458. if (passive) {
  459. connectDataPort(sock, ClientData, passive);
  460. isFolder = getFileList(sock, ClientData, name, passive,fileList);
  461. }
  462. else {
  463. connectDataPort(sock, activeSock, passive);
  464. isFolder = getFileList(sock, activeSock, name, passive,fileList);
  465. }
  466. if (isFolder) {
  467. for (int i = 0; i < fileList.size(); i++) {
  468. if (fileList[i] != name) {
  469. cout << "Download " << fileList[i] << " Y/N?";
  470. string answer;
  471. getline(cin, answer);
  472. deleteSpaces(answer);
  473. if (answer != "n" && answer != "N") {
  474. if (passive) {
  475. connectDataPort(sock, ClientData, passive);
  476. getFile(sock, ClientData, fileList[i], passive);
  477. }
  478. else {
  479. connectDataPort(sock, activeSock, passive);
  480. getFile(sock, activeSock, fileList[i], passive);
  481. }
  482. }
  483. }
  484. }
  485. }
  486. else{
  487. cout << "Download " << name << " Y/N?";
  488. string answer;
  489. getline(cin, answer);
  490. deleteSpaces(answer);
  491. if (answer != "n" && answer != "N") {
  492. if (passive) {
  493. connectDataPort(sock, ClientData, passive);
  494. getFile(sock, ClientData, name, passive);
  495. }
  496. else {
  497. connectDataPort(sock, activeSock, passive);
  498. getFile(sock, activeSock, name, passive);
  499. }
  500. }
  501. }
  502. }
  503. }
  504. }
  505.  
  506. void uploadMultipleFiles(CSocket& sock, CSocket& ClientData, CSocket& activeSock, stringstream& is, bool passive) {
  507. string fullCommand = is.str();
  508. if (fullCommand == "mput") {
  509. string temp = "";
  510. while (temp == "") {
  511. cout << "Files and folders to upload: " << endl;
  512. getline(cin, temp);
  513. is = stringstream(temp);
  514. }
  515. }
  516. string name;
  517. while (!is.eof()) {
  518. is >> name;
  519. cout << "Type y to upload file" << endl;
  520. if (name != "") {
  521. bool isFolder;
  522. vector<string>fileList;
  523. isFolder = getFileListClient(name, fileList);
  524. if (isFolder) {
  525. char previousPath[MAX_BUFFER];
  526. GetCurrentDirectoryA(MAX_BUFFER, previousPath);
  527. SetCurrentDirectoryA(name.c_str());
  528. for (int i = 0; i < fileList.size(); i++) {
  529. cout << "Upload " <<name<<"\\"<< fileList[i] << " Y/N?";
  530. string answer;
  531. getline(cin, answer);
  532. deleteSpaces(answer);
  533. if (answer != "n" && answer != "N") {
  534. if (passive) {
  535. connectDataPort(sock, ClientData, passive);
  536. uploadFile(sock, ClientData, fileList[i], passive);
  537. }
  538. else {
  539. connectDataPort(sock, activeSock, passive);
  540. uploadFile(sock, activeSock, fileList[i], passive);
  541. }
  542. }
  543. }
  544. SetCurrentDirectoryA(previousPath);
  545. }
  546. else {
  547. cout << "Upload " << name << " Y/N?";
  548. string answer;
  549. getline(cin, answer);
  550. deleteSpaces(answer);
  551. if (answer != "n" && answer != "N") {
  552. if (passive) {
  553. connectDataPort(sock, ClientData, passive);
  554. uploadFile(sock, ClientData, name, passive);
  555. }
  556. else {
  557. connectDataPort(sock, activeSock, passive);
  558. uploadFile(sock, activeSock, name, passive);
  559. }
  560. }
  561. }
  562. }
  563. }
  564. }
  565.  
  566. bool getFileListClient(string folderName,vector<string>& res) {
  567. bool isFolder = true;
  568. DIR *dir;
  569. struct dirent *ent;
  570. if ((dir = opendir(folderName.c_str())) != NULL) {
  571. /* print all the files and directories within directory */
  572. while ((ent = readdir(dir)) != NULL) {
  573. res.push_back(string(ent->d_name));
  574. }
  575. closedir(dir);
  576. if (res.size() > 0) {
  577. res.erase(res.begin());
  578. res.erase(res.begin());
  579. }
  580. }
  581. else {
  582. isFolder = false;
  583. }
  584. return isFolder;
  585. }
  586.  
  587. void showCurrentPath(CSocket& sock) {
  588. string command = "pwd\r\n";
  589. sock.Send((char*)command.c_str(), command.length());
  590. cout << receiveMessage(sock);
  591. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement