Example #1
0
File: handles.c Project: yuyuvn/ftp
/**
 * Generates response message for client
 * @param cmd Current command
 * @param state Current connection state
 */
void response(Command *cmd, State *state)
{
  switch(lookup_cmd(cmd->command)){
    case USER: ftp_user(cmd,state); break;
    case PASS: ftp_pass(cmd,state); break;
    case PASV: ftp_pasv(cmd,state); break;
    case LIST: ftp_list(cmd,state); break;
    case CWD:  ftp_cwd(cmd,state); break;
    case PWD:  ftp_pwd(cmd,state); break;
    case MKD:  ftp_mkd(cmd,state); break;
    case RMD:  ftp_rmd(cmd,state); break;
    case RETR: ftp_retr(cmd,state); break;
    case STOR: ftp_stor(cmd,state); break;
    case DELE: ftp_dele(cmd,state); break;
    case SIZE: ftp_size(cmd,state); break;
    case ABOR: ftp_abor(state); break;
    case QUIT: ftp_quit(state); break;
    case TYPE: ftp_type(cmd,state); break;
    case CDUP: ftp_cdup(state); break;
    case HELP: ftp_help(cmd, state); break;
    case NLST: ftp_nlst(cmd, state); break;
    case RNFR: ftp_rnfr(cmd, state); break;
    case RNTO: ftp_rnto(cmd, state); break;
    case APPE: ftp_appe(cmd, state); break;
    case NOOP:
      if(state->logged_in){
        state->message = "200 Zzz...\n";
      }else{
        state->message = "530 Please login with USER and PASS\n";
      }
      write_state(state);
      break;
    default: 
      state->message = "500 Unknown command\n";
      write_state(state);
      break;
  }
}
int main(int argc, char* argv[])
{
    if (argc != 2)
    {
        print_usage(argv[0]);
        return EXIT_FAILURE;
    }

    URL url;
    url_init(&url);
    int error = url_from_string(&url, argv[1]);
    if (error)
    {
        fprintf(stderr, "Could not sucessfully parse FTP url (error code: %d)\n", error);
        url_destroy(&url);
        return EXIT_FAILURE;
    }

    error = url_host_to_ip(&url);
    if (error)
    {
        fprintf(stderr, "Could not get an IPv4 IP address from hostname %s (error code: %d)\n", url.host, error);
        return EXIT_FAILURE;
    }

    FTP ftp;
    error = ftp_connect(&ftp, url.ip, url.port ? url.port : 21);
    if (error)
    {
        fprintf(stderr, "Could not connect to ftp at IP %s, port %d\n", url.ip, url.port ? url.port : 21);
        return EXIT_FAILURE;
    }

    const char* user = strlen(url.user) ? url.user : "******";
    const char* pass = strlen(url.password) ? url.password : "******";

    error = ftp_login(&ftp, user, pass);
    if (error)
    {
        fprintf(stderr, "Could not login with user %s and pass %s\n", user, pass);
        return EXIT_FAILURE;
    }

    char path[1024] = "";
    for (int i = 0; i < url.num_parts - 1; ++i) {
        strcat(path, url.parts[i]);
        strcat(path, "/");
    }

    if (path[0] != 0) {
        error = ftp_cwd(&ftp, path);
        if (error) {
            perror("ftp_cwd");
            return error;
        }
    }

    error = ftp_pasv(&ftp);
    if (error) {
        perror("ftp_pasv");
        return error;
    }

    const char* file_name = url.num_parts ? url.parts[url.num_parts - 1] : "";

    error = ftp_retr(&ftp, file_name);
    if (error) {
        perror("ftp_retr");
        return error;
    }

    error = ftp_download(&ftp, file_name);
    if (error) {
        perror("ftp_download");
        return error;
    }

    error = ftp_disconnect(&ftp);
    if (error) {
        perror("ftp_disconnect");
        return error;
    }

    url_destroy(&url);

    return EXIT_SUCCESS;
}