Chinaunix首页 | 论坛 | 博客
  • 博客访问: 10079
  • 博文数量: 2
  • 博客积分: 65
  • 博客等级: 民兵
  • 技术积分: 34
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-06 15:43
文章分类
文章存档

2012年(2)

我的朋友
最近访客

分类: C/C++

2012-08-03 13:32:26


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <strings.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <netdb.h>
  8. #include <netinet/in.h>
  9. #include <arpa/inet.h>
  10. #include <ctype.h>
  11. #include <unistd.h>
  12. #include <errno.h>
  13. #include <dirent.h>
  14. #include <curses.h>
  15. #include <termios.h>
  16. #include <sys/stat.h>
  17. #include <fcntl.h>

  18. #define DEFAULT_FTP_PORT 21

  19. extern int h_errno;

  20. char user[64]; //ftp usr
  21. char passwd[64]; //ftp passwd

  22. //ftp server address
  23. struct sockaddr_in ftp_server, local_host;
  24. struct hostent * server_hostent;

  25. int sock_control;
  26. int mode = 1; //ftp mode, 0 is PORT, 1 is PASV;

  27. //echo_off and echo_on for get usr password from stdin
  28. static struct termios stored_settings;
  29. void echo_off(void)
  30. {
  31.     struct termios new_settings;
  32.     tcgetattr(0,&stored_settings);
  33.     new_settings = stored_settings;
  34.     new_settings.c_lflag &= (~ECHO);
  35.     tcsetattr(0,TCSANOW,&new_settings);
  36.     return;
  37. }
  38. void echo_on(void)
  39. {
  40.     tcsetattr(0,TCSANOW,&stored_settings);
  41.     return;
  42. }

  43. void cmd_err_exit(char * err_msg, int err_code)
  44. {
  45.     printf("%s\n", err_msg);
  46.     exit(err_code);
  47. }

  48. int fill_host_addr(char * host_ip_addr, struct sockaddr_in * host, int port)
  49. {
  50.     if(port <= 0 || port > 65535)
  51.         return 254;
  52.     bzero(host, sizeof(struct sockaddr_in));
  53.     host->sin_family = AF_INET;
  54.         if(inet_addr(host_ip_addr) != -1)
  55.     {
  56.                 host->sin_addr.s_addr = inet_addr(host_ip_addr);
  57.     }
  58.         else
  59.     {
  60.         if((server_hostent = gethostbyname(host_ip_addr)) != 0)
  61.         {
  62.             memcpy(&host->sin_addr, server_hostent->h_addr,\
  63.              sizeof(host->sin_addr));
  64.         }
  65.      else return 253;
  66.     }
  67.         host->sin_port = htons(port);
  68.     return 1;
  69. }

  70. int xconnect(struct sockaddr_in *s_addr, int type)//
  71. {
  72.     struct timeval outtime;
  73.     int set;
  74.     int s = socket(AF_INET, SOCK_STREAM, 0);
  75.     if(s < 0)
  76.         cmd_err_exit("creat socket error!", 249);

  77.     //set outtime for the control socket
  78.     if(type == 1)
  79.     {
  80.         outtime.tv_sec = 0;
  81.         outtime.tv_usec = 300000;
  82.     }
  83.     else
  84.     {
  85.         outtime.tv_sec = 5;
  86.         outtime.tv_usec = 0;
  87.     }
  88.     set = setsockopt(s, SOL_SOCKET,SO_RCVTIMEO, &outtime,sizeof(outtime));
  89.     if(set !=0)
  90.     {
  91.         printf("set socket %s errno:%d\n",strerror(errno),errno);
  92.         cmd_err_exit("set socket", 1);
  93.     }

  94.     //connect to the server
  95.     if (connect(s,(struct sockaddr *)s_addr,sizeof(struct sockaddr_in)) < 0)
  96.     {
  97.         printf("Can't connect to server %s, port %d\n",\
  98.             inet_ntoa(s_addr->sin_addr),ntohs(ftp_server.sin_port));
  99.         exit(252);
  100.     }
  101.     return s;
  102. }

  103. //send command to server with sock_fd
  104. int ftp_send_cmd(const char *s1, const char *s2, int sock_fd)
  105. {
  106.     char send_buf[256];
  107.     int send_err, len;
  108.     if(s1)
  109.     {
  110.         strcpy(send_buf,s1);
  111.         if(s2)
  112.         {    
  113.             strcat(send_buf, s2);
  114.             strcat(send_buf,"\r\n");
  115.             len = strlen(send_buf);
  116.             send_err = send(sock_fd, send_buf, len, 0);
  117.         }
  118.         else
  119.         {
  120.             strcat(send_buf,"\r\n");
  121.             len = strlen(send_buf);
  122.             send_err = send(sock_fd, send_buf, len, 0);
  123.         }
  124.         }
  125.     if(send_err < 0)
  126.         printf("send() error!\n");
  127.     return send_err;
  128. }

  129. //get the server's reply message from sock_fd
  130. int ftp_get_reply(int sock_fd)
  131. {
  132.     static int reply_code = 0,count=0;
  133.     char rcv_buf[512];
  134.     count=read(sock_fd, rcv_buf, 510);
  135.     if(count > 0)
  136.         reply_code = atoi(rcv_buf);
  137.     else
  138.         return 0;
  139.     while(1)
  140.     {
  141.         if(count <= 0)
  142.             break;
  143.         rcv_buf[count]='\0';
  144.         printf("%s",rcv_buf);
  145.         count=read(sock_fd, rcv_buf, 510);
  146.     }
  147.     return reply_code;
  148. }

  149. int get_port()
  150. {
  151.     char port_respond[512];
  152.     char *buf_ptr;
  153.     int count,port_num;
  154.     ftp_send_cmd("PASV",NULL,sock_control);
  155.     count = read(sock_control, port_respond, 510);
  156.     if(count <= 0)
  157.         return 0;
  158.     port_respond[count]='\0';
  159.     if(atoi(port_respond) == 227)
  160.     {
  161.         //get low byte of the port
  162.         buf_ptr = strrchr(port_respond, ',');
  163.         port_num = atoi(buf_ptr + 1);
  164.         *buf_ptr = '\0';
  165.         //get high byte of the port
  166.         buf_ptr = strrchr(port_respond, ',');
  167.         port_num += atoi(buf_ptr + 1) * 256;
  168.         return port_num;
  169.     }
  170.     return 0;
  171. }

  172. int rand_local_port()
  173. {
  174.     int local_port;
  175.     srand((unsigned)time(NULL));
  176.     local_port = rand() % 40000 + 1025;
  177.     return local_port;
  178. }

  179. //connect data stream
  180. int xconnect_ftpdata()
  181. {
  182.     if(mode)
  183.     {
  184.         int data_port = get_port();
  185.         if(data_port != 0)
  186.             ftp_server.sin_port=htons(data_port);
  187.         return(xconnect(&ftp_server, 0));
  188.     }
  189.     else
  190.     {
  191.         int client_port, get_sock, opt, set;
  192.         char cmd_buf[32];
  193.         struct timeval outtime;
  194.         struct sockaddr_in local;
  195.         char local_ip[24];
  196.         char *ip_1, *ip_2, *ip_3, *ip_4;
  197.         int addr_len = sizeof(struct sockaddr);
  198.         client_port = rand_local_port();
  199.         get_sock = socket(AF_INET, SOCK_STREAM, 0);
  200.         if(get_sock < 0)
  201.         {
  202.             cmd_err_exit("socket()", 1);
  203.         }

  204.         //set outtime for the data socket
  205.         outtime.tv_sec = 7;
  206.         outtime.tv_usec = 0;
  207.         opt = SO_REUSEADDR;
  208.         set = setsockopt(get_sock, SOL_SOCKET,SO_RCVTIMEO, \
  209.                 &outtime,sizeof(outtime));
  210.         if(set !=0)
  211.         {
  212.             printf("set socket %s errno:%d\n",strerror(errno),errno);
  213.             cmd_err_exit("set socket", 1);
  214.         }
  215.         set = setsockopt(get_sock, SOL_SOCKET,SO_REUSEADDR, \
  216.                 &opt,sizeof(opt));
  217.         if(set !=0)
  218.         {
  219.             printf("set socket %s errno:%d\n",strerror(errno),errno);
  220.             cmd_err_exit("set socket", 1);
  221.         }

  222.         bzero(&local_host,sizeof(local_host));
  223.         local_host.sin_family = AF_INET;
  224.         local_host.sin_port = htons(client_port);
  225.         local_host.sin_addr.s_addr = htonl(INADDR_ANY);
  226.         bzero(&local, sizeof(struct sockaddr));
  227.         while(1)
  228.         {
  229.             set = bind(get_sock, (struct sockaddr *)&local_host, \
  230.                     sizeof(local_host));
  231.             if(set != 0 && errno == 11)
  232.             {
  233.                 client_port = rand_local_port();
  234.                 continue;
  235.             }
  236.             set = listen(get_sock, 1);
  237.             if(set != 0 && errno == 11)
  238.             {
  239.                 cmd_err_exit("listen()", 1);
  240.             }
  241.             //get local host's ip
  242.             if(getsockname(sock_control,(struct sockaddr*)&local,\
  243.                                (socklen_t *)&addr_len) < 0)
  244.                 return -1;
  245.             snprintf(local_ip, sizeof(local_ip), inet_ntoa(local.sin_addr));
  246.             //change the format to the PORT command needs.
  247.             local_ip[strlen(local_ip)]='\0';
  248.             ip_1 = local_ip;
  249.             ip_2 = strchr(local_ip, '.');
  250.             *ip_2 = '\0';
  251.             ip_2++;
  252.             ip_3 = strchr(ip_2, '.');
  253.             *ip_3 = '\0';
  254.             ip_3++;
  255.             ip_4 = strchr(ip_3, '.');
  256.             *ip_4 = '\0';
  257.             ip_4++;
  258.             snprintf(cmd_buf, sizeof(cmd_buf), "PORT %s,%s,%s,%s,%d,%d", \
  259.             ip_1, ip_2, ip_3, ip_4,    client_port >> 8, client_port&0xff);
  260.             ftp_send_cmd(cmd_buf, NULL, sock_control);
  261.             if(ftp_get_reply(sock_control) != 200)
  262.             {
  263.                 printf("Can not use PORT mode!Please use \"mode\" change to PASV mode.\n");
  264.                 return -1;
  265.             }
  266.             else
  267.                 return get_sock;
  268.         }
  269.     }
  270. }


  271. //deal with the "list" command
  272. void ftp_list()
  273. {    
  274.     int i = 0,new_sock;
  275.     int set = sizeof(local_host);
  276.     int list_sock_data = xconnect_ftpdata();
  277.     if(list_sock_data < 0)
  278.     {
  279.         ftp_get_reply(sock_control);
  280.         printf("creat data sock error!\n");
  281.         return;
  282.     }
  283.     ftp_get_reply(sock_control);
  284.     ftp_send_cmd("LIST", NULL, sock_control);
  285.     ftp_get_reply(sock_control);
  286.     if(mode)
  287.         ftp_get_reply(list_sock_data);
  288.     else
  289.     {
  290.         while(i < 3)
  291.         {
  292.             new_sock = accept(list_sock_data, (struct sockaddr *)&local_host, \
  293.                 (socklen_t *)&set);
  294.             if(new_sock == -1)
  295.             {
  296.                 printf("accept return:%s errno: %d\n", strerror(errno),errno);
  297.                 i++;
  298.                 continue;
  299.             }
  300.             else break;
  301.         }
  302.         if(new_sock == -1)
  303.         {
  304.             printf("Sorry, you can't use PORT mode. There is something wrong when the server connect to you.\n");
  305.             return;
  306.         }
  307.         ftp_get_reply(new_sock);
  308.         close(new_sock);
  309.     }

  310.     close(list_sock_data);
  311.     ftp_get_reply(sock_control);
  312. }

  313. //get filename(s) from user's command
  314. void ftp_cmd_filename(char * usr_cmd, char * src_file, char * dst_file)
  315. {    
  316.     int length, flag = 0;
  317.     int i = 0, j = 0;
  318.     char * cmd_src;
  319.     cmd_src = strchr(usr_cmd, ' ');
  320.     if(cmd_src == NULL)
  321.     {
  322.         printf("command error!\n");
  323.         return;
  324.     }
  325.     else
  326.     {
  327.         while(*cmd_src == ' ')
  328.             cmd_src ++;
  329.     }
  330.     if(cmd_src == NULL || cmd_src == '\0')
  331.     {
  332.         printf("command error!\n");
  333.     }
  334.     else
  335.     {    
  336.         length = strlen(cmd_src);
  337.         while(i <= length)//be careful with space in the filename
  338.         {    
  339.             if((*(cmd_src+i)) !=' ' && (*(cmd_src+i)) != '\\')
  340.             {
  341.                 if(flag == 0)
  342.                     src_file[j] = *(cmd_src +i);
  343.                 else
  344.                     dst_file[j] = *(cmd_src +i);
  345.                 j++;
  346.             }
  347.             if((*(cmd_src+i)) == '\\' && (*(cmd_src+i+1)) !=' ')
  348.             {
  349.                 if(flag == 0)
  350.                     src_file[j] = *(cmd_src +i);
  351.                 else
  352.                     dst_file[j] = *(cmd_src +i);
  353.                 j++;
  354.             }
  355.             if((*(cmd_src+i)) == ' ' && (*(cmd_src+i-1)) != '\\')
  356.             {
  357.                 src_file[j] = '\0';
  358.                 flag = 1;
  359.                 j = 0;
  360.             }
  361.             if((*(cmd_src+i)) == '\\' && (*(cmd_src+i+1)) == ' ')
  362.             {
  363.                 if(flag == 0)
  364.                     src_file[j] = ' ';
  365.                 else
  366.                     dst_file[j] = ' ';
  367.                 j++;
  368.             }
  369.             i++;
  370.         };
  371.     }
  372.     if(flag == 0)
  373.         strcpy(dst_file, src_file);
  374.     else
  375.         dst_file[j] = '\0';
  376. }

  377. //deal with the "get" command
  378. void ftp_get(char * usr_cmd)
  379. {
  380.     int get_sock, set, new_sock, i = 0;
  381.     char src_file[512];
  382.     char dst_file[512];
  383.     char rcv_buf[512];
  384.     char cover_flag[3];
  385.     struct stat file_info;
  386.     int local_file;
  387.     int count = 0;
  388.     ftp_cmd_filename(usr_cmd, src_file, dst_file);
  389.     ftp_send_cmd("SIZE ", src_file, sock_control);
  390.     if(ftp_get_reply(sock_control) != 213)
  391.     {
  392.         printf("SIZE error!\n");
  393.         return;
  394.     }
  395.     if(!stat(dst_file, &file_info))
  396.     {
  397.         printf("local file %s exists: %d bytes\n", dst_file, (int)file_info.st_size);
  398.         printf("Do you want to cover it? [y/n]");
  399.         fgets(cover_flag, sizeof(cover_flag), stdin);
  400.         fflush(stdin);
  401.         if(cover_flag[0] != 'y')
  402.         {
  403.             printf("get file %s aborted.\n", src_file);
  404.             return;
  405.         }
  406.     }
  407.     local_file = open(dst_file, O_CREAT|O_TRUNC|O_WRONLY);
  408.     if(local_file < 0)
  409.     {
  410.         printf("creat local file %s error!\n", dst_file);
  411.         return;
  412.     }
  413.     get_sock = xconnect_ftpdata();
  414.     if(get_sock < 0)
  415.     {
  416.         printf("socket error!\n");
  417.         return;
  418.     }
  419.     set = sizeof(local_host);
  420.     
  421.     ftp_send_cmd("TYPE I", NULL, sock_control);
  422.     ftp_get_reply(sock_control);
  423.     ftp_send_cmd("RETR ", src_file, sock_control);
  424.     if(!mode)
  425.     {
  426.         while(i < 3)
  427.         {
  428.             new_sock = accept(get_sock, (struct sockaddr *)&local_host, \
  429.                 (socklen_t *)&set);
  430.             if(new_sock == -1)
  431.             {
  432.                 printf("accept return:%s errno: %d\n", strerror(errno),errno);
  433.                 i++;
  434.                 continue;
  435.             }
  436.                 else break;
  437.         }
  438.         if(new_sock == -1)
  439.         {
  440.             printf("Sorry, you can't use PORT mode. There is something wrong when the server connect to you.\n");
  441.             return;
  442.         }
  443.         ftp_get_reply(sock_control);
  444.         while(1)
  445.         {
  446.             printf("loop \n");
  447.             count = read(new_sock, rcv_buf, sizeof(rcv_buf));
  448.             if(count <= 0)
  449.                 break;
  450.             else
  451.             {
  452.                 write(local_file, rcv_buf, count);
  453.             }
  454.         }
  455.         close(local_file);
  456.         close(get_sock);
  457.         close(new_sock);
  458.         ftp_get_reply(sock_control);
  459.     }
  460.     else
  461.     {
  462.         ftp_get_reply(sock_control);
  463.         while(1)
  464.         {
  465.             count = read(get_sock, rcv_buf, sizeof(rcv_buf));
  466.             if(count <= 0)
  467.                 break;
  468.             else
  469.             {
  470.                 write(local_file, rcv_buf, count);
  471.             }
  472.         }
  473.     close(local_file);
  474.     close(get_sock);
  475.     ftp_get_reply(sock_control);
  476.     }
  477.     if(!chmod(src_file, 0644))
  478.     {
  479.         printf("chmod %s to 0644\n", dst_file);
  480.         return;
  481.     }
  482.     else
  483.         printf("chmod %s to 0644 error!\n", dst_file);
  484.     ftp_get_reply(sock_control);
  485. }

  486. //deal with "put" command
  487. void ftp_put(char * usr_cmd)
  488. {    
  489.     char src_file[512];
  490.     char dst_file[512];
  491.     char send_buf[512];
  492.     struct stat file_info;
  493.     int local_file;
  494.     int file_put_sock, new_sock, count = 0, i = 0;
  495.     int set = sizeof(local_host);
  496.     ftp_cmd_filename(usr_cmd, src_file, dst_file);
  497.     if(stat(src_file, &file_info) < 0)
  498.     {
  499.         printf("local file %s doesn't exist!\n", src_file);
  500.         return;
  501.     }
  502.     local_file = open(src_file, O_RDONLY);
  503.     if(local_file < 0)
  504.     {
  505.         printf("open local file %s error!\n", dst_file);
  506.         return;
  507.     }
  508.     file_put_sock = xconnect_ftpdata();
  509.     if(file_put_sock < 0)
  510.     {    
  511.         ftp_get_reply(sock_control);
  512.         printf("creat data sock errro!\n");
  513.         return;
  514.     }
  515.     ftp_send_cmd("STOR ", dst_file, sock_control);
  516.     ftp_get_reply(sock_control);
  517.     ftp_send_cmd("TYPE I", NULL, sock_control);
  518.     ftp_get_reply(sock_control);
  519.     if(!mode)
  520.     {
  521.         while(i < 3)
  522.         {
  523.             new_sock = accept(file_put_sock, (struct sockaddr *)&local_host, \
  524.                 (socklen_t *)&set);
  525.             if(new_sock == -1)
  526.             {
  527.                 printf("accept return:%s errno: %d\n", strerror(errno),errno);
  528.                 i++;
  529.                 continue;
  530.             }
  531.                 else break;
  532.         }
  533.         if(new_sock == -1)
  534.         {
  535.             printf("Sorry, you can't use PORT mode. There is something wrong when the server connect to you.\n");
  536.             return;
  537.         }
  538.         while(1)
  539.         {
  540.             count = read(local_file, send_buf, sizeof(send_buf));
  541.             if(count <= 0)
  542.                 break;
  543.             else
  544.             {
  545.                 write(new_sock, send_buf,sizeof(send_buf));
  546.             }
  547.         }
  548.         close(local_file);
  549.         close(file_put_sock);
  550.         close(new_sock);
  551.     }
  552.     else
  553.     {
  554.         while(1)
  555.         {
  556.             count = read(local_file, send_buf, sizeof(send_buf));
  557.             if(count <= 0)
  558.                 break;
  559.             else
  560.             {
  561.                 write(file_put_sock, send_buf,count);
  562.             }
  563.         }
  564.         close(local_file);
  565.         close(file_put_sock);
  566.     }
  567.     ftp_get_reply(sock_control);
  568. }

  569. //call this function to quit
  570. void ftp_quit()
  571. {
  572.     ftp_send_cmd("QUIT",NULL,sock_control);
  573.     ftp_get_reply(sock_control);
  574.     close(sock_control);
  575. }

  576. //tell the user what current directory is in the server
  577. void ftp_pwd()
  578. {
  579.     ftp_send_cmd("PWD", NULL, sock_control);
  580.     ftp_get_reply(sock_control);
  581. }

  582. //change the directory in the server
  583. void ftp_cd(char * usr_cmd)
  584. {
  585.     char *cmd = strchr(usr_cmd, ' ');
  586.     char path[1024];
  587.     if(cmd == NULL)
  588.     {
  589.         printf("command error!\n");
  590.         return;
  591.     }
  592.     else
  593.     {
  594.         while(*cmd == ' ')
  595.             cmd ++;
  596.     }
  597.     if(cmd == NULL || cmd == '\0')
  598.     {
  599.         printf("command error!\n");
  600.         return;
  601.     }
  602.     else
  603.     {
  604.         strncpy(path, cmd, strlen(cmd));
  605.         path[strlen(cmd)]='\0';
  606.         ftp_send_cmd("CWD ", path, sock_control);
  607.         ftp_get_reply(sock_control);
  608.     }
  609. }

  610. //list files and directories in local host
  611. void local_list()
  612. {
  613.     DIR * dp;
  614.     struct dirent *dirp;
  615.     if((dp = opendir("./")) == NULL)
  616.     {
  617.         printf("opendir() error!\n");
  618.         return;
  619.     }
  620.     printf("Local file list:\n");
  621.     while((dirp = readdir(dp)) != NULL)
  622.     {
  623.         if(strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0)
  624.             continue;
  625.         printf("%s\n", dirp->d_name);
  626.     }
  627. }

  628. //print local current directory
  629. void local_pwd()
  630. {
  631.     char curr_dir[512];
  632.     int size = sizeof(curr_dir);
  633.     if(getcwd(curr_dir, size) == NULL)
  634.         printf("getcwd failed\n");
  635.     else
  636.         printf("Current local directory: %s\n", curr_dir);
  637. }

  638. //change local directory
  639. void local_cd(char * usr_cmd)
  640. {
  641.     char *cmd = strchr(usr_cmd, ' ');
  642.     char path[1024];
  643.     if(cmd == NULL)
  644.     {
  645.         printf("command error!\n");
  646.         return;
  647.     }
  648.     else
  649.     {
  650.         while(*cmd == ' ')
  651.             cmd ++;
  652.     }
  653.     if(cmd == NULL || cmd == '\0')
  654.     {
  655.         printf("command error!\n");
  656.         return;
  657.     }
  658.     else
  659.     {
  660.         strncpy(path, cmd, strlen(cmd));
  661.         path[strlen(cmd)]='\0';
  662.         if(chdir(path) < 0)
  663.             printf("Local: chdir to %s error!\n", path);
  664.         else
  665.             printf("Local: chdir to %s\n", path);
  666.     }
  667. }

  668. void show_help()
  669. {    
  670.     printf("\033[32mhelp\033[5m\t--print this command list\n");
  671.     printf("\033[32mpwd\033[0m\t--print the current directory of server\n");
  672.     printf("\033[32mlist\033[0m\t--list the files and directoris in current directory of server\n");
  673.     printf("\033[32mcd [directory]\033[0m\n\t--enter of server\n");
  674.     printf("\033[32mmode\033[0m\n\t--change current mode, PORT or PASV\n");
  675.     printf("\033[32mput [local_file] \033[0m\n\t--send [local_file] to server as \n");
  676.     printf("\tif isn't given, it will be the same with [local_file] \n");
  677.     printf("\tif there is any \' \' in , write like this \'\\ \'\n");
  678.     printf("\033[32mget [remote file] \033[0m\n\t--get [remote file] to local host as\n");
  679.     printf("\tif isn't given, it will be the same with [remote_file] \n");
  680.     printf("\tif there is any \' \' in , write like this \'\\ \'\n");
  681.     printf("\033[32mlpwd\033[0m\t--print the current directory of local host\n");
  682.     printf("\033[32mllist\033[0m\t--list the files and directoris in current directory of local host\n");
  683.     printf("\033[32mlcd [directory]\033[0m\n\t--enter of localhost\n");
  684.     printf("\033[32mquit\033[0m\t--quit this ftp client program\n");
  685. }

  686. //get user and password for login
  687. void get_user()
  688. {
  689.     char read_buf[64];
  690.     printf("User(Press for anonymous): ");
  691.     fgets(read_buf, sizeof(read_buf), stdin);
  692.     if(read_buf[0]=='\n')
  693.         strncpy(user, "anonymous", 9);
  694.     else
  695.         strncpy(user, read_buf, strlen(read_buf)-1);
  696. }
  697. void get_pass()
  698. {
  699.     char read_buf[64];
  700.     printf("Password(Press for anonymous): ");
  701.     echo_off();
  702.     fgets(read_buf, sizeof(read_buf), stdin);
  703.     if(read_buf[0]=='\n')
  704.         strncpy(passwd, "anonymous", 9);
  705.     else
  706.         strncpy(passwd, read_buf, strlen(read_buf)-1);
  707.     echo_on();
  708.     printf("\n");
  709. }

  710. //login to the server
  711. int ftp_login()
  712. {
  713.     int err;
  714.     get_user();
  715.     if(ftp_send_cmd("USER ", user, sock_control) < 0)
  716.         cmd_err_exit("Can not send message",1);;
  717.     err = ftp_get_reply(sock_control);
  718.     if(err == 331)
  719.     {
  720.         get_pass();
  721.         if(ftp_send_cmd("PASS ", passwd, sock_control) <= 0)
  722.             cmd_err_exit("Can not send message",1);
  723.         else
  724.             err = ftp_get_reply(sock_control);
  725.         if(err != 230)
  726.         {
  727.             printf("Password error!\n");
  728.             return 0;
  729.         }
  730.         return 1;
  731.     }
  732.     else
  733.     {
  734.         printf("User error!\n");
  735.         return 0;
  736.     }
  737. }

  738. //deal with user's command
  739. int ftp_usr_cmd(char * usr_cmd)
  740. {
  741.     if(!strncmp(usr_cmd,"list",4))
  742.         return 1;
  743.     if(!strncmp(usr_cmd,"pwd",3))
  744.         return 2;
  745.     if(!strncmp(usr_cmd,"cd ",3))
  746.         return 3;
  747.     if(!strncmp(usr_cmd,"put ",4))
  748.         return 4;
  749.     if(!strncmp(usr_cmd,"get ",4))
  750.         return 5;
  751.     if(!strncmp(usr_cmd,"quit",4))
  752.         return 6;
  753.     if(!strncmp(usr_cmd,"mode",4))
  754.         return 7;
  755.     if(!strncmp(usr_cmd,"llist",5))
  756.         return 11;
  757.     if(!strncmp(usr_cmd,"lpwd",4))
  758.         return 12;
  759.     if(!strncmp(usr_cmd,"lcd ",4))
  760.         return 13;
  761.     return -1;
  762. }

  763. int start_ftp_cmd(char * host_ip_addr, int port)
  764. {
  765.     int err;
  766.     int cmd_flag;
  767.     char usr_cmd[1024];
  768.     err = fill_host_addr(host_ip_addr, &ftp_server, port);
  769.     if(err == 254)
  770.         cmd_err_exit("Invalid port!",254);
  771.     if(err == 253)
  772.         cmd_err_exit("Invalid server address!",253);

  773.     sock_control = xconnect(&ftp_server,1);//涓庢湇鍔″櫒鍙戝嚭杩炴帴璇锋眰
  774.     if((err = ftp_get_reply(sock_control)) != 220)
  775.         cmd_err_exit("Connect error!",220);
  776.     do
  777.     {
  778.         err = ftp_login();
  779.     }while(err != 1);

  780.     while(1)
  781.     {
  782.         printf("ftp_client>");
  783.         fgets(usr_cmd, 510, stdin);
  784.         fflush(stdin);
  785.         if(usr_cmd[0] == '\n')
  786.             continue;
  787.         usr_cmd[strlen(usr_cmd)-1] = '\0';
  788.         cmd_flag = ftp_usr_cmd(usr_cmd);
  789.         switch(cmd_flag)
  790.         {
  791.             case 1:
  792.                 ftp_list();
  793.                 memset(usr_cmd, '\0',sizeof(usr_cmd));
  794.                 break;
  795.             case 2:
  796.                 ftp_pwd();
  797.                 memset(usr_cmd, '\0',sizeof(usr_cmd));
  798.                 break;
  799.             case 3:
  800.                 ftp_cd(usr_cmd);
  801.                 memset(usr_cmd, '\0',sizeof(usr_cmd));
  802.                 break;
  803.             case 4:
  804.                 ftp_put(usr_cmd);
  805.                 memset(usr_cmd, '\0',sizeof(usr_cmd));
  806.                 break;
  807.             case 5:
  808.                 ftp_get(usr_cmd);
  809.                 memset(usr_cmd, '\0',sizeof(usr_cmd));
  810.                 break;
  811.             case 6:
  812.                 ftp_quit();
  813.                 exit(0);
  814.             case 7:
  815.                 mode = (mode + 1)%2;
  816.                 if(mode)
  817.                     printf("change mode to PASV\n");
  818.                 else
  819.                     printf("change mode to PORT\n");
  820.                 break;
  821.             case 11:
  822.                 local_list();
  823.                 memset(usr_cmd, '\0',sizeof(usr_cmd));
  824.                 break;
  825.             case 12:
  826.                 local_pwd();
  827.                 memset(usr_cmd, '\0',sizeof(usr_cmd));
  828.                 break;
  829.             case 13:
  830.                 local_cd(usr_cmd);
  831.                 memset(usr_cmd, '\0',sizeof(usr_cmd));
  832.                 break;
  833.             default:
  834.                 show_help();
  835.                 memset(usr_cmd, '\0',sizeof(usr_cmd));
  836.                 break;
  837.         }
  838.     }
  839.     return 1;
  840. }

  841. //main process
  842. int main(int argc, char * argv[])
  843. {
  844.     if(argc != 2 && argc != 3)
  845.     {
  846.         printf("Usage: %s [port]\n",argv[0]);
  847.         exit(1);
  848.     }
  849.     else
  850.     {
  851.         if(argv[2]==NULL)
  852.             start_ftp_cmd(argv[1], DEFAULT_FTP_PORT);
  853.         else
  854.             start_ftp_cmd(argv[1], atol(argv[2]));
  855.     }
  856.     return 1;
  857. }

阅读(1181) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:没有了

给主人留下些什么吧!~~