Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.74 KB | None | 0 0
  1. // Jeremiah Schultz
  2. // CS 176B Winter 2016
  3. // HW2: mftp.c - FTP Client
  4.  
  5. #include "mftp.h"
  6.  
  7. // print error message to stderr & exit with proper error code
  8. void exitCode(int errCode){
  9.  
  10. switch(errCode){
  11. case 0 :
  12. fprintf(stderr, "exit-code %i: Operation successfully completed\n", errCode);
  13. break;
  14. case 1 :
  15. fprintf(stderr, "exit-code %i: Can't connect to server\n", errCode);
  16. break;
  17. case 2 :
  18. fprintf(stderr, "exit-code %i: Authentication failed\n",errCode);
  19. break;
  20. case 3 :
  21. fprintf(stderr, "exit-code %i: File not found\n", errCode);
  22. break;
  23. case 4 :
  24. fprintf(stderr, "exit-code %i: Syntax error in client request\n", errCode);
  25. break;
  26. case 5 :
  27. fprintf(stderr, "exit-code %i: Command not implemented by server\n", errCode);
  28. break;
  29. case 6 :
  30. fprintf(stderr, "exit-code %i: Operation not allowed by server\n", errCode);
  31. break;
  32. case 7 :
  33. fprintf(stderr, "exit-code %i: Generic error\n", errCode);
  34. break;
  35. }
  36.  
  37. exit(errCode);
  38. }
  39.  
  40. // display working version of client
  41. void printVersion(){
  42. printf("App: mftp client\nVersion: 0.1\nAuthor: Jeremiah");
  43.  
  44. }
  45.  
  46. // display proper usage ofr mftp client
  47. void printHelp(){
  48. printf("Usage: ./mftp [FLAGS]\n");
  49.  
  50. //print all possible flag options
  51. printf("FLAGS:\n");
  52. printf(" -a, --active Forces active behavior (default: passive)\n");
  53. printf(" -f, --file [file] Specifies the file to download\n");
  54. printf(" -h, --help Displays this help information and exits\n");
  55. printf(" -l, --log [logfile] Logs all the FTP commands exchanged with server to file ('-' will log to stdout)\n");
  56. printf(" -m, --mode [mode] Specifies the mode to be used for the transfer (ASCII or binary) (default: binary)\n");
  57. printf(" -n, --username [user] Specifies the username to use when logging into the FTP server (default: anonymous)\n");
  58. printf(" -p, --port [port] Specifies the port to be used when contacting the server (default: 21)\n");
  59. printf(" -P, --password [password] Specifies the password to use when logging into the FTP server (default: user@localhost.localnet)\n");
  60. printf(" -s, --server [hostname] Specifies the server to download the file from\n");
  61. printf(" -v, --version Prints version information and exits\n");
  62. printf(" -w, --swarm [swarm-file] Enables swarming mode\n(use with optional -b [num-bytes] to force download in segments of size num_bytes)\n");
  63.  
  64. }
  65.  
  66.  
  67. // MAIN | MAIN | MAIN | MAIN //
  68. int main(int argc, char* argv[]){
  69.  
  70. // myData initialization
  71. myData.active = false;
  72. myData.filename = NULL;
  73. myData.logfile = NULL;
  74. myData.logSet = false;
  75. myData.mode = 0;
  76. myData.user = "anonymous";
  77. myData.portNum = 21;
  78. myData.pass = "user@localhost.localnet";
  79. myData.server = NULL;
  80. myData.swarmfile = NULL;
  81.  
  82. // check usage and display help if needed
  83. if (argc == 1){
  84. printHelp();
  85. exitCode(0);
  86. }
  87.  
  88. // parse command line options
  89. int optIndex = 0;
  90. int opt = 0;
  91.  
  92. opt = getopt_long(argc, argv, flags, longOptions, &optIndex);
  93.  
  94. while (opt != -1){
  95.  
  96. switch(opt){
  97. case 'a' :
  98. // active mode
  99. fprintf(stdout, "active mode\n");
  100. break;
  101. case 'f' :
  102. // store filename
  103. myData.filename = optarg;
  104. break;
  105. case 'h' :
  106. // display help info
  107. printHelp();
  108. break;
  109. case 'l' :
  110. // update logfile path
  111. if (!strcmp(optarg, "-")){
  112. myData.logfile = stdout;
  113. myData.logSet = true;
  114. printf("Printing to stdout\n");
  115. }
  116. else{
  117. if((myData.logfile = fopen(optarg, "w")) == NULL){
  118. fprintf(stderr, "Logfile could not be created\n");
  119. exitCode(7);
  120. }
  121. myData.logSet = true;
  122. printf("Logfile %s created successfully\n", optarg);
  123. }
  124. break;
  125. case 'm' :
  126. // set transfer mode to binary (0) or ASCII (1)
  127. if (!strcasecmp(optarg, "binary")){
  128. myData.mode = 0;
  129. printf("Binary transfer mode enabled\n");
  130. }
  131. else if (!strcasecmp(optarg, "ASCII")){
  132. myData.mode = 1;
  133. printf("ASCII transfer mode enabled\n");
  134. }
  135. else{
  136. // invalid mode provided
  137. printf("Invalid mode\n");
  138. exitCode(4);
  139. }
  140. break;
  141. case 'n' :
  142. // store username
  143. myData.user = optarg;
  144. break;
  145. case 'p' :
  146. // store port number
  147. myData.portNum = atoi(optarg);
  148. break;
  149. case 'P' :
  150. // store password
  151. myData.pass = optarg;
  152. break;
  153. case 's' :
  154. // store servername
  155. myData.server = optarg;
  156. break;
  157. case 'v' :
  158. // display version info
  159. printVersion();
  160. break;
  161. }
  162. opt = getopt_long(argc, argv, flags, longOptions, &optIndex);
  163. }
  164.  
  165.  
  166. // setup control connection
  167. int sockfd = 0;
  168. int bytesBack = 0;
  169. char recvBuf[SIZE];
  170. char sendBuf[SIZE];
  171. char echo[SIZE];
  172. struct sockaddr_in serv_addr;
  173. struct hostent* server;
  174.  
  175. memset(recvBuf, '0', SIZE);
  176. memset(sendBuf, '0', SIZE);
  177. memset(&serv_addr, '0', sizeof(serv_addr));
  178.  
  179.  
  180. if ((sockfd = socket(AF_INET, SOCK_STREAM, 0) < 0)){
  181. fprintf(stderr, "Error opening socket\n");
  182. exitCode(7);
  183. }
  184.  
  185.  
  186.  
  187.  
  188.  
  189. if ((server = gethostbyname(myData.server)) == NULL){
  190. // can't connect to server
  191. exitCode(1);
  192. }
  193.  
  194.  
  195.  
  196. //bzero((char *) &serv_addr, sizeof(serv_addr));
  197. memcpy(&server->h_addr, server->h_addr_list[0], server->h_length);
  198. serv_addr.sin_family = AF_INET;
  199. serv_addr.sin_port = htons(myData.portNum);
  200.  
  201. if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0){
  202. exitCode(1);
  203. }
  204.  
  205. if ((bytesBack = recv(sockfd, recvBuf, sizeof(recvBuf), 0)) < 0){
  206. fprintf(stderr, "No greeting received from server\n");
  207. exitCode(7);
  208. }
  209.  
  210. printf("Received from server: %s", recvBuf);
  211.  
  212.  
  213.  
  214. exitCode(0);
  215. return 0;
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement