Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Jeremiah Schultz
- // CS 176B Winter 2016
- // HW2: mftp.c - FTP Client
- #include "mftp.h"
- // print error message to stderr & exit with proper error code
- void exitCode(int errCode){
- switch(errCode){
- case 0 :
- fprintf(stderr, "exit-code %i: Operation successfully completed\n", errCode);
- break;
- case 1 :
- fprintf(stderr, "exit-code %i: Can't connect to server\n", errCode);
- break;
- case 2 :
- fprintf(stderr, "exit-code %i: Authentication failed\n",errCode);
- break;
- case 3 :
- fprintf(stderr, "exit-code %i: File not found\n", errCode);
- break;
- case 4 :
- fprintf(stderr, "exit-code %i: Syntax error in client request\n", errCode);
- break;
- case 5 :
- fprintf(stderr, "exit-code %i: Command not implemented by server\n", errCode);
- break;
- case 6 :
- fprintf(stderr, "exit-code %i: Operation not allowed by server\n", errCode);
- break;
- case 7 :
- fprintf(stderr, "exit-code %i: Generic error\n", errCode);
- break;
- }
- exit(errCode);
- }
- // display working version of client
- void printVersion(){
- printf("App: mftp client\nVersion: 0.1\nAuthor: Jeremiah");
- }
- // display proper usage ofr mftp client
- void printHelp(){
- printf("Usage: ./mftp [FLAGS]\n");
- //print all possible flag options
- printf("FLAGS:\n");
- printf(" -a, --active Forces active behavior (default: passive)\n");
- printf(" -f, --file [file] Specifies the file to download\n");
- printf(" -h, --help Displays this help information and exits\n");
- printf(" -l, --log [logfile] Logs all the FTP commands exchanged with server to file ('-' will log to stdout)\n");
- printf(" -m, --mode [mode] Specifies the mode to be used for the transfer (ASCII or binary) (default: binary)\n");
- printf(" -n, --username [user] Specifies the username to use when logging into the FTP server (default: anonymous)\n");
- printf(" -p, --port [port] Specifies the port to be used when contacting the server (default: 21)\n");
- printf(" -P, --password [password] Specifies the password to use when logging into the FTP server (default: user@localhost.localnet)\n");
- printf(" -s, --server [hostname] Specifies the server to download the file from\n");
- printf(" -v, --version Prints version information and exits\n");
- 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");
- }
- // MAIN | MAIN | MAIN | MAIN //
- int main(int argc, char* argv[]){
- // myData initialization
- myData.active = false;
- myData.filename = NULL;
- myData.logfile = NULL;
- myData.logSet = false;
- myData.mode = 0;
- myData.user = "anonymous";
- myData.portNum = 21;
- myData.pass = "user@localhost.localnet";
- myData.server = NULL;
- myData.swarmfile = NULL;
- // check usage and display help if needed
- if (argc == 1){
- printHelp();
- exitCode(0);
- }
- // parse command line options
- int optIndex = 0;
- int opt = 0;
- opt = getopt_long(argc, argv, flags, longOptions, &optIndex);
- while (opt != -1){
- switch(opt){
- case 'a' :
- // active mode
- fprintf(stdout, "active mode\n");
- break;
- case 'f' :
- // store filename
- myData.filename = optarg;
- break;
- case 'h' :
- // display help info
- printHelp();
- break;
- case 'l' :
- // update logfile path
- if (!strcmp(optarg, "-")){
- myData.logfile = stdout;
- myData.logSet = true;
- printf("Printing to stdout\n");
- }
- else{
- if((myData.logfile = fopen(optarg, "w")) == NULL){
- fprintf(stderr, "Logfile could not be created\n");
- exitCode(7);
- }
- myData.logSet = true;
- printf("Logfile %s created successfully\n", optarg);
- }
- break;
- case 'm' :
- // set transfer mode to binary (0) or ASCII (1)
- if (!strcasecmp(optarg, "binary")){
- myData.mode = 0;
- printf("Binary transfer mode enabled\n");
- }
- else if (!strcasecmp(optarg, "ASCII")){
- myData.mode = 1;
- printf("ASCII transfer mode enabled\n");
- }
- else{
- // invalid mode provided
- printf("Invalid mode\n");
- exitCode(4);
- }
- break;
- case 'n' :
- // store username
- myData.user = optarg;
- break;
- case 'p' :
- // store port number
- myData.portNum = atoi(optarg);
- break;
- case 'P' :
- // store password
- myData.pass = optarg;
- break;
- case 's' :
- // store servername
- myData.server = optarg;
- break;
- case 'v' :
- // display version info
- printVersion();
- break;
- }
- opt = getopt_long(argc, argv, flags, longOptions, &optIndex);
- }
- // setup control connection
- int sockfd = 0;
- int bytesBack = 0;
- char recvBuf[SIZE];
- char sendBuf[SIZE];
- char echo[SIZE];
- struct sockaddr_in serv_addr;
- struct hostent* server;
- memset(recvBuf, '0', SIZE);
- memset(sendBuf, '0', SIZE);
- memset(&serv_addr, '0', sizeof(serv_addr));
- if ((sockfd = socket(AF_INET, SOCK_STREAM, 0) < 0)){
- fprintf(stderr, "Error opening socket\n");
- exitCode(7);
- }
- if ((server = gethostbyname(myData.server)) == NULL){
- // can't connect to server
- exitCode(1);
- }
- //bzero((char *) &serv_addr, sizeof(serv_addr));
- memcpy(&server->h_addr, server->h_addr_list[0], server->h_length);
- serv_addr.sin_family = AF_INET;
- serv_addr.sin_port = htons(myData.portNum);
- if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0){
- exitCode(1);
- }
- if ((bytesBack = recv(sockfd, recvBuf, sizeof(recvBuf), 0)) < 0){
- fprintf(stderr, "No greeting received from server\n");
- exitCode(7);
- }
- printf("Received from server: %s", recvBuf);
- exitCode(0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement