ftp.cpp
资源名称:myftp.zip [点击查看]
上传用户:kkaqas
上传日期:2007-01-07
资源大小:38k
文件大小:7k
源码类别:
Ftp客户端
开发平台:
Visual C++
- #include <windows.h>
- #include <winsock.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include "tcp.h"
- #include "ftp.h"
- int g_code =0;
- char g_reply[2000];
- int sd_login =-1;
- void log_msg(char *);
- int ftp_cmd(int sd, char *cmd, int success_code)
- {
- int code;
- if(tcp_send(sd, cmd, strlen(cmd), 5) !=(int)strlen(cmd))
- {
- log_msg("ftp_cmd: tcp_send failed");
- return -1;
- }
- if((code =get_reply(sd)) !=success_code)
- {
- log_msg(cmd);
- log_msg(g_reply);
- return -2;
- }
- return 0;
- }
- int ftp_login(char *hostname, char *user_name, char *passwd)
- {
- int sd, ret;
- char cmds[100];
- if((sd =tcp_connect(hostname, 21, 10)) <0)
- return -1;
- if((ret =get_reply(sd)) !=220)
- {
- closesocket(sd);
- return -1;
- }
- sprintf(cmds, "USER %srn", user_name);
- if(ftp_cmd(sd, cmds, 331) <0)
- {
- closesocket(sd);
- return -1;
- }
- sprintf(cmds, "PASS %srn", passwd);
- if(ftp_cmd(sd, cmds, 230) <0)
- {
- closesocket(sd);
- return -1;
- }
- sd_login =sd;
- return sd;
- }
- int ftp_chdir(int sd, char *path)
- {
- char cmds[100];
- sprintf(cmds, "CWD %srn", path);
- if(ftp_cmd(sd, cmds, 250) <0)
- return -1;
- return 0;
- }
- int ftp_get_size(int sd, char *file_remote)
- {
- int size =0;
- char cmds[100];
- sprintf(cmds, "SIZE %srn", file_remote);
- if(ftp_cmd(sd, cmds, 213) <0)
- return 0;
- if(sscanf(g_reply, "%*d %d", &size) !=1)
- return 0;
- return size;
- }
- int ftp_get_time(int sd, char *file_remote, char *pstime)
- {
- char cmds[100];
- sprintf(cmds, "MDTM %srn", file_remote);
- if(ftp_cmd(sd, cmds, 213) <0)
- return -1;
- strncpy(pstime, &g_reply[4], 14);
- return 0;
- }
- int ftp_get_file(int sd, char *file_remote, char *file_local, int file_len, int max_wait_time)
- {
- char temp[200], cmds[300];
- int len, len_recved =0, ret =0, code;
- char buf[4096];
- FILE *fp =NULL;
- int sd_data =-1;
- temp[0] =0;
- if(ftp_cmd(sd, "TYPE Irn", 200) <0)
- {
- sprintf(temp, "ftp_get_file:TYPE I failed! sd=%d, file_remote:%s", sd, file_remote);
- ret =-2;
- goto f_exit;
- }
- if(file_len <=0)
- if((file_len =ftp_get_size(sd, file_remote)) <=0)
- {
- sprintf(temp, "ftp_get_file:ftp_get_size failed! sd =%d, file_remote=%s, ret_len=%d", sd, file_remote, file_len);
- ret =-3;
- goto f_exit;
- }
- if((sd_data =initconn(sd)) <0)
- {
- sprintf(temp, "ftp_get_file:initconn failed! sd =%d, file_remote:%s", sd, file_remote);
- ret =-4;
- goto f_exit;
- }
- sprintf(cmds, "RETR %srn", file_remote);
- if(ftp_cmd(sd, cmds, 150) <0)
- {
- sprintf(temp, "ftp_get_file:ftp_cmd failed! cmd:%s", cmds);
- ret =-5;
- goto f_exit;
- }
- if((sd_accept =tcp_accept(sd_data, 20)) <0)
- {
- sprintf(temp, "ftp_get_file:ftp_accept failed! sd=%d, sd_data=%d, file_remote=%s", sd, sd_data, file_remote);
- ret =-6;
- goto f_exit;
- }
- len_recved =0;
- if((fp =fopen(file_local, "wb")) ==NULL)
- {
- sprintf(temp, "ftp_get_file:open file for write failed! local_file:%s", file_local);
- ret =-1;
- goto f_exit;
- }
- while(len_recved < file_len)
- {
- if(file_len-len_recved <sizeof(buf)) len =file_len-len_recved;
- else len =sizeof(buf);
- if(tcp_recv(sd_accept, buf, len, max_wait_time) !=len)
- {
- sprintf(temp, "ftp_get_file:tcp_recv failed! len_recved:%d, file_len:%d, file_remote:%s", len_recved, file_len, file_remote);
- ret =-7;
- get_reply(sd);
- goto f_exit;
- }
- if(fwrite(buf, len, 1, fp) !=1)
- {
- sprintf(temp, "ftp_get_file: fwrite failed! len_recved:%d, file_local:%s", len_recved, file_local);
- get_reply(sd);
- ret =-8;
- goto f_exit;
- }
- len_recved +=len;
- }
- tcp_close(sd_accept); sd_accept =-1;
- ret =len_recved;
- if((code =get_reply(sd)) !=226)
- {
- sprintf(temp, "ftp_get_file:get_reply failed after recv success! ret_code:%d, reply string:%s", code, g_reply);
- goto f_exit;
- }
- f_exit:
- if(sd_accept >=0) tcp_close(sd_accept);
- if(sd_data >=0) tcp_close(sd_data);
- sd_accept =-1;
- sd_data =-1;
- if(fp) fclose(fp);
- if(ret <0)
- {
- sprintf(temp, "ftp_get_file failed. ret=%d", ret);
- log_msg(temp);
- remove(file_local);
- }
- return ret;
- }
- int ftp_put_file(int sd, char *file_local, int pos, char *file_remote, int max_wait_time)
- {
- char cmds[300];
- int len, file_len =0, len_sent =0, ret =0, code;
- char buf[4096];
- FILE *fp =NULL;
- int sd_data;
- if((fp =fopen(file_local, "r")) ==NULL)
- {
- ret =-1;
- goto f_exit;
- }
- fseek(fp, 0, SEEK_END);
- file_len =ftell(fp)-pos;
- if(file_len <0) return -1;
- if(file_len ==0) return 0;
- fseek(fp, pos, SEEK_SET);
- if(ftp_cmd(sd, "TYPE Irn", 200) <0)
- {
- ret =-1;
- goto f_exit;
- }
- if((sd_data =initconn(sd)) <0)
- {
- ret =-1;
- goto f_exit;
- }
- sprintf(cmds, "STOR %srn", file_remote);
- if(ftp_cmd(sd, cmds, 150) <0)
- {
- ret =-1;
- goto f_exit;
- }
- if((sd_accept =tcp_accept(sd_data, 20)) <0)
- {
- ret =-1;
- goto f_exit;
- }
- len_sent =0;
- while(len_sent < file_len)
- {
- if(file_len-len_sent <sizeof(buf)) len =file_len-len_sent;
- else len =sizeof(buf);
- if(fread(buf, len, 1, fp) !=1)
- {
- get_reply(sd);
- ret =-1;
- goto f_exit;
- }
- if(tcp_send(sd_accept, buf, len, max_wait_time) !=len)
- {
- ret =-1;
- get_reply(sd);
- goto f_exit;
- }
- len_sent +=len;
- }
- closesocket(sd_accept); sd_accept =-1;
- if((code =get_reply(sd)) !=226)
- {
- goto f_exit;
- }
- ret =len_sent;
- f_exit:
- if(sd_accept >=0) closesocket(sd_accept);
- if(sd_data >=0) closesocket(sd_data);
- sd_accept =-1;
- sd_data =-1;
- if(fp) fclose(fp);
- if(buf) free(buf);
- return ret;
- }
- int initconn(int sd)
- {
- char *p1, *p2;
- struct sockaddr_in addr1, addr2;
- char temp[256];
- int len;
- int code, sd_data=-1;
- if((sd_data =tcp_bind(NULL, 0)) <0)
- return -1;
- len =sizeof(addr1);
- if(getsockname(sd_login, (struct sockaddr *)&addr1, &len) <0)
- return -1;
- if(getsockname(sd_data, (struct sockaddr *)&addr2, &len) <0)
- return -1;
- p1 =(char *)&addr1.sin_addr;
- p2 =(char *)&addr2.sin_port;
- sprintf(temp, "PORT %d,%d,%d,%d,%d,%drn", ((int)p1[0]) &0xff, ((int)p1[1]) &0xff, (int)p1[2] &0xff, (int)p1[3]&0xff, (int)p2[0]&0xff, (int)p2[1]&0xff);
- if(tcp_send(sd, temp, strlen(temp), 10) !=(int)strlen(temp))
- {
- closesocket(sd_data);
- return -1;
- }
- if((code =get_reply(sd)) !=200)
- {
- closesocket(sd_data);
- return -1;
- }
- return sd_data;
- }
- int get_reply(int sd)
- {
- int i, code =0;
- again:
- i =0;
- memset(g_reply, 0, sizeof(g_reply));
- while(1)
- {
- if(tcp_recv(sd, &g_reply[i], 1, 10) <=0)
- break;
- if(g_reply[i] =='r') g_reply[i] =' ';
- if(g_reply[i] =='n')
- {
- g_reply[i] =' ';
- if(sscanf(g_reply, "%d", &code) !=1)
- {
- return -1;
- }
- else break;
- }
- i++;
- }
- if(g_reply[3] =='-')
- {
- i =0;
- goto again;
- }
- g_code =code;
- return code;
- }
- void ftp_quit(int sd)
- {
- ftp_cmd(sd, "QUITrn", 221);
- closesocket(sd);
- }