/************************************************************************************************** * function : rename upload file in ftp server * para : {char * oldName} old file name; * {char * newName} new file name; * {int socket_control} socket file description * * return : {int} -1: error; 0: OK * history : {2013.7.18 wujun} fristly be created {2013.7.29 wujun} modify return data type from void to int **************************************************************************************************/ int ftp_rename(char *oldName, char *newName, int socket_control) { int error; //ftp_get_reply(socket_control); if( ftp_send_cmd("RNFR ", oldName, socket_control) < 0 ) { plog("%s%s", oldName, " :send 'RNFR' command error.\n"); return -1; } if( (error = ftp_get_reply(socket_control, NULL)) <= 0 ) { plog("%s","Fun(ftp_rename):recv no message 1.\n"); return -1; } if( error == 350 )//350 Ready for RNTO. { if( (ftp_send_cmd("RNTO ", newName, socket_control)) <= 0 ) { plog("%s", "Fun(ftp_rename): send command 'RNTO' error.\n"); return -1; } if( ( error = ftp_get_reply(socket_control, NULL) ) <= 0 ) { plog("%s","Fun(ftp_rename):recv no message 2.\n"); return -1; } if(error == 250)//250 Rename successful. { plog("%s%s%s", "Fun(ftp_rename): rename ", oldName, " successfullu.\n"); return 0; } else { plog("%s","RNTO command excuted failed.\n"); return -1; } }else { plog("%s","RNFR command excuted failed.\n"); return -1; } }
/************************************************************************************************** * function : get ftp port, when mode=1, port =21 * para : {void} . * return : {int } port * history : {2013.7.18 wujun} fristly be created **************************************************************************************************/ int get_port(int socket_control) { char port_respond[2048]; char *temp; int count,port_num; int error = ftp_send_cmd("PASV",NULL,socket_control); if( error < 0) { plog("%s","Fun(get_port) : Send 'PASV' command error.\n"); return 0;//è¿å ¥è¢«å¨æ¨¡å¼ } memset(port_respond, 0, 2048); //count = recv(socket_control,port_respond,510, MSG_WAITALL); count = ftp_get_reply(socket_control, port_respond); if( count <= 0 ) { plog("%s", "Fun(get_port): recv no message or other error.\n"); return 0; } plog("Fun(get_port): %s.", port_respond); if( atoi( port_respond ) == 227 )//ç¡®å®è¿å ¥è¢«å¨æ¨¡å¼ { temp = strrchr( port_respond, ',' );//å¨ä¸²ä¸æå®å符çæåä¸ä¸ªåºç°ä½ç½®ä»¥æ¾åºn6 port_num = atoi( temp+1 ); *temp = '\0'; //æªæn6æ¥ån5; temp = strrchr( port_respond, ','); port_num += atoi( temp+1 ) * 256; return port_num; } return 0; }
/************************************************************************************************** * function : send command 'user' and 'pass' to ftp server; * para : {char * user} ftp user name; * {char * password} ftp password; * {int socket_control} socket file description * return : {int} error code, like 0: ok, -1:error ; * history : {2013.7.18 wujun} fristly be created **************************************************************************************************/ int ftp_login(int socket_control, char * user, char * password) { int err; if(ftp_send_cmd("USER ", user, socket_control) < 0) { plog("%s", "Fun(ftp_login): send command 'USER' error.\n"); return -1; } err = ftp_get_reply(socket_control, NULL); if(err == 331) { if(ftp_send_cmd("PASS ", password, socket_control) < 0) { plog("%s", "Fun(ftp_login):send command 'PASS' error.\n"); return -1; } err = ftp_get_reply(socket_control, NULL); if(err != 230) { plog("%s", "Fun(ftp_login): Password error!\n"); return -1; } else { plog("%s", "Fun(ftp_login): Password correct!\n"); return 0; } } else if ( err == 530) { plog("%s", "Fun(ftp_login): Already connected.\n"); return -1; } else { plog("%s", "Fun(ftp_login): other error.\n"); return -1; } }
void ftp_quit() { int error; ftp_send_cmd("QUIT",NULL,sock_control); error = ftp_get_reply(sock_control); printf("ftp reply number:<%d>, fun<%s> line<%d>\n",error,__FUNCTION__,__LINE__); close(sock_control); }
/***************************************************************************** * function : make dir in ftp server * para : {char * dirName} dir name; * {int socket_control} socket file description * * return : {int} -1: error; 0: OK * history : {2013.7.18 wujun} fristly be created {2013.7.29 wujun} modify return data type from void to int *******************************************************************************/ int ftp_mkdir(char *dirName, int socket_control) { if( dirName == NULL ) { plog("%s", "Fun(ftp_mkdir): dirName is null.\n" ); return -1; } int len = strlen(dirName); char *path; if( len == 0) { plog("%s", "Fun(ftp_mkdir): dirName content is blank.\n" ); return -1; } path = (char *)malloc(len+1); if( path == NULL ) { plog("%s", "Fun(ftp_mkdir): malloc 'path' error.\n" ); return -1; } memset(path,0,len+1); strcpy(path, dirName); int p; for( p = 1; p <= len; p++ ) { if( path[p] == '/') { path[p] = 0; if( ( ftp_send_cmd("MKD ", path, socket_control) ) <= 0 ) { plog("%s", "Fun(ftp_mkdir) : send command MKD error.\n"); } if( ftp_get_reply(socket_control, NULL) <= 0 ) { plog("%s","Fun(ftp_mkdir) :recv no message 3 or network error.\n"); return -1; } path[p] = '/'; } } plog("%s%s", path, ": create successfully\n."); return 0; }
int ftp_cd(char *dir) { int error; if(dir==NULL) { printf("command error!\n"); return -1; } char ftpDir[1024]; memset(ftpDir,0,sizeof(ftpDir)); strncpy(ftpDir,dir,strlen(dir)); ftpDir[strlen(dir)]='\0'; ftp_send_cmd("CWD ",ftpDir,sock_control); /* 改变服务器上的工作目录 */ error = ftp_get_reply(sock_control); printf("ftp reply number:<%d>, fun<%s> line<%d>\n",error,__FUNCTION__,__LINE__); if(error == 550) { printf("ftp file error !\n"); return -1; } printf("ftp cd succ! ftpDir=%s\n",ftpDir); return 0; }
int ftp_open(char * arg) { char * host; int port; char * dir; char * file; int tcp_sock; int data_sock; char ftp_request[1024]; struct sockaddr_in stLclAddr; socklen_t namelen; int i; /* Check for URL syntax */ if(strncmp(arg, "ftp://", strlen("ftp://"))) return(0); /* Parse URL */ port = 21; host = arg + strlen("ftp://"); if((dir = strchr(host, '/')) == NULL) return(0); *dir++ = 0; if((file = strrchr(dir, '/')) == NULL) { file = dir; dir = NULL; } else *file++ = 0; if(strchr(host, ':') != NULL) /* port is specified */ { port = atoi(strchr(host, ':') + 1); *strchr(host, ':') = 0; } /* Open a TCP socket */ if(!(tcp_sock = tcp_open(host, port))) { perror("ftp_open"); return(0); } /* Send FTP USER and PASS request */ ftp_get_reply(tcp_sock); sprintf(ftp_request, "USER anonymous\r\n"); send(tcp_sock, ftp_request, strlen(ftp_request), 0); if(ftp_get_reply(tcp_sock) != 331) return(0); sprintf(ftp_request, "PASS smpeguser@\r\n"); send(tcp_sock, ftp_request, strlen(ftp_request), 0); if(ftp_get_reply(tcp_sock) != 230) return(0); sprintf(ftp_request, "TYPE I\r\n"); send(tcp_sock, ftp_request, strlen(ftp_request), 0); if(ftp_get_reply(tcp_sock) != 200) return(0); if(dir != NULL) { sprintf(ftp_request, "CWD %s\r\n", dir); send(tcp_sock, ftp_request, strlen(ftp_request), 0); if(ftp_get_reply(tcp_sock) != 250) return(0); } /* Get interface address */ namelen = sizeof(stLclAddr); if(getsockname(tcp_sock, (struct sockaddr *) &stLclAddr, &namelen) < 0) return(0); /* Open data socket */ if ((data_sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) return(0); stLclAddr.sin_family = AF_INET; /* Get the first free port */ for(i = 0; i < 0xC000; i++) { stLclAddr.sin_port = htons(0x4000 + i); if(bind(data_sock, (struct sockaddr *) &stLclAddr, sizeof(stLclAddr)) >= 0) break; } port = 0x4000 + i; if(listen(data_sock, 1) < 0) return(0); i = ntohl(stLclAddr.sin_addr.s_addr); sprintf(ftp_request, "PORT %d,%d,%d,%d,%d,%d\r\n", (i >> 24) & 0xFF, (i >> 16) & 0xFF, (i >> 8) & 0xFF, i & 0xFF, (port >> 8) & 0xFF, port & 0xFF); send(tcp_sock, ftp_request, strlen(ftp_request), 0); if(ftp_get_reply(tcp_sock) != 200) return(0); sprintf(ftp_request, "RETR %s\r\n", file); send(tcp_sock, ftp_request, strlen(ftp_request), 0); if(ftp_get_reply(tcp_sock) != 150) return(0); return(accept(data_sock, NULL, NULL)); }
/************************************************************************************************** * function : upload filr from ftp server; * para : {char* src_file} file path in local host; * {char * dst_file} file path in ftp server; * {int socket_control} socket file description * * return : {int} -1: local file does not exist; -2: can not open local file; -3: command socket file description error; -4: data socket file description error * history : {2013.7.18 wujun} fristly be created {2013.7.29 wujun} modify return data type from void to int **************************************************************************************************/ int ftp_put(char* src_file, char * dst_file, int socket_control) { char send_buff[512]; struct stat file_info; int local_file; int file_put_sock,new_sock,count=0,i=0; int set=sizeof(local_host); char log[500]; if( stat( src_file, &file_info ) < 0 ) { memset( log, 0, 500 ); sprintf( log, "Fun(ftp_put):upload:local file %s doesn't exist!\n\n", src_file ); plog( "%s", log ); return UPLOAD_LOCAL_FILENAME_NULL; } local_file = fopen( src_file, "r" ); if( local_file < 0 ) { plog("%s","Fun(ftp_put): Open file error.\n\n"); return UPLOAD_LOCAL_OPEN_ERROR; } file_put_sock = xconnect_ftpdata( socket_control); if( file_put_sock < 0 ) { ftp_get_reply( socket_control, NULL ); plog("%s", "Fun(ftp_put):Creat data socket error.\n\n"); return UPLOAD_DATA_SOCKET_ERROR; } if( ftp_send_cmd("STOR " , dst_file,socket_control ) < 0 ) { plog("%s", "Fun(ftp_put):send 'STOR' command error.\n\n"); close(file_put_sock); return -1; } if( ftp_get_reply(socket_control, NULL) == 0) { plog("%s", "Fun(ftp_put):recv no message after sending 'STOR' command.\n\n"); close(file_put_sock); return -1; } if( ftp_send_cmd("TYPE I",NULL,socket_control) < 0) { plog("%s", "Fun(ftp_put):send 'TYPE I' command error.\n\n"); close(file_put_sock); return -1; } if( ftp_get_reply(socket_control, NULL) == 0) { plog("%s", "Fun(ftp_put):recv no message after sending 'TYPE I' command.\n\n"); close(file_put_sock); return -1; } if(mode == 0) { while(i<3) { new_sock=accept(file_put_sock,(struct sockaddr *)&local_host,(socklen_t*)&set); if(new_sock==-1) { plog("%s","Fun(ftp_put):error create new_sock in put port.\n\n"); i++; continue ; } else break; } if(new_sock==-1) { plog("%s","Fun(ftp_put):The PORT mode won't work.\n\n"); close(file_put_sock); return UPLOAD_PORT_MODE_ERROR; } while(1) { count=recv(local_file,send_buff,sizeof(send_buff), MSG_WAITALL); if(count<=0) { fclose(local_file); close(file_put_sock); close(new_sock); plog("%s%s", src_file, "Fun(ftp_put):upload over.\n\n"); ftp_delay(); return UPLOAD_SUCCESS; } else { write(new_sock,send_buff,sizeof(send_buff)); } } } else if( mode == 1 ) { while( 1 ) { count = recv( local_file, send_buff, sizeof( send_buff ), MSG_WAITALL); if( count <= 0 ) { fclose( local_file ); close( file_put_sock ); ftp_delay( ); plog("%s%s", src_file, "Fun(ftp_put):upload over.\n\n"); return UPLOAD_SUCCESS; } else { write( file_put_sock, send_buff, count ); } } } }
/************************************************************************************************** * function : download filr from ftp server; * para : {char* src_file} file path in ftp server; * {char * dst_file} file path in local host; * {int socket_control} socket file description * * return : {int} 0:ok; -1:create local file error; -2:command socket file description error; -3:transfer mode set error; * history : {2013.7.18 wujun} fristly be created {2013.7.29 wujun} modify return data type from void to int **************************************************************************************************/ int ftp_get(char* src_file, char * dst_file, int socket_control) { int get_sock, set, new_sock, i = 0; char rcv_buf[512]; char cover_flag[3]; struct stat file_info; FILE * local_file; int count = 0; int replayId = 0; if( src_file == NULL || dst_file == NULL) { plog("%s","Fun(ftp_get) : file path is invalid.\n\n"); return DOWNLOAD_LOCAL_FILENAME_NULL; } get_sock = xconnect_ftpdata(socket_control); if(get_sock < 0) { plog("%s","Fun(ftp_get) :socket error!\n\n"); return DOWNLOAD_CONNECT_SOCKET_ERROR; } set = sizeof(local_host); if( ftp_send_cmd("TYPE I", NULL, socket_control) < 0 ) { plog("%s", "Fun(ftp_get) :send 'TYPE I' command error.\n\n"); close(get_sock); return -1; } if( ftp_get_reply(socket_control, NULL) <= 0 ) { plog("%s", "Fun(ftp_get) :'TYPE I' recv no message.\n"); close(get_sock); return -1; } if( ftp_send_cmd("RETR ", src_file, socket_control) < 0) { plog("%s", "Fun(ftp_get) :send 'RETR' command error.\n\n"); close(get_sock); return -1; } if(!mode) { while(i < 3) { new_sock = accept(get_sock, (struct sockaddr *)&local_host, (socklen_t *)&set); if(new_sock == -1) { plog("%s","Fun(ftp_get) :accept errno.\n"); i++; continue; } else { break; } } if( new_sock == -1 ) { plog("%s","Fun(ftp_get) :Sorry, you can't use PORT mode. \n\n"); close(get_sock); return DOWNLOAD_PORT_MODE_ERROR; } if( ( replayId = ftp_get_reply(socket_control, NULL ) ) <= 0 ) { plog("%s", "Fun(ftp_get) :recv no message or network error.\n\n"); close(get_sock); return -1; } if(replayId == 550)//remote file does not exist. { plog("%s%s", src_file, " does not exsit.\n"); close(get_sock); return DOWNLOAD_REMOTE_FILE_NOEXIST; } local_file = fopen(dst_file, "w+"); if( local_file == NULL ) { plog("%s","Fun(ftp_get) :creat local file error!\n\n"); close(get_sock); return DOWNLOAD_CREAET_LOCALFILE_ERROR; } while(1) { count = recv(new_sock, rcv_buf, sizeof(rcv_buf),MSG_WAITALL); if(count <= 0) { fclose(local_file); close(get_sock); close(new_sock); //char commmand[1024]; //sprintf(commmand, "chmod 0777 %s", dst_file); //system(commmand); ftp_delay(); plog("%s\t%s",src_file," download successfully.\n\n\n"); return FTP_DOWNLOAD_SUCCESS; } else { fprintf(local_file, "%s",rcv_buf); } } } else { if( ( replayId=ftp_get_reply(socket_control, NULL) ) <= 0 ) { plog("%s", "Fun(ftp_get) :'RETR' recv no message.\n\n"); close(get_sock); return -1; } if(replayId == 550)//remote file does not exist. { plog("%s%s", src_file, " does not exsit.\n"); close(get_sock); return DOWNLOAD_REMOTE_FILE_NOEXIST; } local_file = fopen(dst_file, "w+"); if( local_file == NULL ) { plog("%s","Fun(ftp_get) :creat local file error!\n\n"); close(get_sock); return DOWNLOAD_CREAET_LOCALFILE_ERROR; } while(1) { memset(rcv_buf,0,512); count = recv(get_sock, rcv_buf, 512, MSG_WAITALL); if(count <= 0) { fclose(local_file); close(get_sock); char commmand[1024]; sprintf(commmand, "chmod 0777 %s", dst_file); system(commmand); ftp_delay(); plog("%s\t%s",src_file," download successfully.\n\n"); return FTP_DOWNLOAD_SUCCESS; } else { fwrite (rcv_buf , sizeof(char), sizeof(rcv_buf), local_file); } } } }
/************************************************************************************************** * function : according to mode, set ftp data transfer method * para : * return : {int} error code; * history : {2013.7.18 wujun} fristly be created **************************************************************************************************/ int xconnect_ftpdata(int socket_control) { if(mode) { int data_port = get_port(socket_control); if( data_port != 0 ) { ftp_server.sin_port=htons(data_port); } return(xconnect(&ftp_server, 0)); } else { int client_port, get_sock, opt, set; char cmd_buf[32]; char log[500] = {0}; struct timeval outtime; struct sockaddr_in local; char local_ip[24]; char *ip_1, *ip_2, *ip_3, *ip_4; int addr_len = sizeof(struct sockaddr); client_port = rand_local_port(); get_sock = socket(AF_INET, SOCK_STREAM, 0); if(get_sock < 0) { plog("%s", "Fun(xconnect_ftpdata):set work mode PORT and get socket error.\n"); return -1; } //set outtime for the data socket outtime.tv_sec = 1; outtime.tv_usec = 0; opt = SO_REUSEADDR; //set = setsockopt(get_sock, SOL_SOCKET,SO_RCVTIMEO, &outtime,sizeof(outtime)); if(set !=0) { memset(log, 0, 500); sprintf(log,"Fun(xconnect_ftpdata):set socket %s errno:%d\n",strerror(errno),errno); plog("%s", log); close(get_sock); return -1; } set = setsockopt(get_sock, SOL_SOCKET,SO_REUSEADDR, &opt,sizeof(opt)); if(set !=0) { memset(log, 0, 500); sprintf(log,"Fun(xconnect_ftpdata):set socket %s errno:%d\n",strerror(errno),errno); plog("%s",log); close(get_sock); return -1; } bzero(&local_host,sizeof(local_host)); local_host.sin_family = AF_INET; local_host.sin_port = htons(client_port); local_host.sin_addr.s_addr = htonl(INADDR_ANY); bzero(&local, sizeof(struct sockaddr)); while(1) { set = bind(get_sock, (struct sockaddr *)&local_host, sizeof(local_host)); if(set != 0 && errno == 11) { client_port = rand_local_port(); continue; } set = listen(get_sock, 1); if(set != 0 && errno == 11) { plog("%s","Fun(xconnect_ftpdata):listen().\n"); close(get_sock); return -1; } //get local host's ip if(getsockname(socket_control,(struct sockaddr*)&local,(socklen_t *)&addr_len) < 0) { plog("%s", "Fun(xconnect_ftpdata): get sock name error.\n"); close(get_sock); return -1; } snprintf(local_ip, sizeof(local_ip), inet_ntoa(local.sin_addr)); //change the format to the PORT command needs. local_ip[strlen(local_ip)]='\0'; ip_1 = local_ip; ip_2 = strchr(local_ip, '.'); *ip_2 = '\0'; ip_2++; ip_3 = strchr(ip_2, '.'); *ip_3 = '\0'; ip_3++; ip_4 = strchr(ip_3, '.'); *ip_4 = '\0'; ip_4++; snprintf(cmd_buf, sizeof(cmd_buf), "PORT %s,%s,%s,%s,%d,%d", ip_1, ip_2, ip_3, ip_4, client_port >> 8, client_port&0xff); if( ftp_send_cmd( cmd_buf, NULL, socket_control ) < 0 ) { plog("%s", "Fun(xconnect_ftpdata):send 'PORT' command error.\n"); close(get_sock); return -1; } if(ftp_get_reply(socket_control, NULL) != 200) { plog("%s","Fun(xconnect_ftpdata):Can not use PORT mode!Please use \"mode\" change to PASV mode.\n"); close(get_sock); return -1; } else { return get_sock; } } } }
int connectFtpServer(char * server_ip, int port, char * user, char * password) { int socket_control; giLogLine++; if( server_ip == NULL || user == NULL) { plog( "%s\n\n", "Fun(connectFtpServer): server_ip or user is NULL." ); return -1; } int error; char log[1000]={0}; error = fill_host_addr( server_ip, &ftp_server, port ); if( error == 254 ) { plog("%s\n\n", "Fun(connectFtpServer): Invalid port!"); return -1; } if( error == 253 ) { plog("%s\n\n", "Fun(connectFtpServer): Invalid address!"); return -1; } struct timeval outtime; int type = 1; socket_control = socket( AF_INET,SOCK_STREAM, 0 ); if( socket_control < 0 ) { plog("%s\n\n", "Fun(connectFtpServer): Creat socket error."); return -1; } if( type == 1 ) { outtime.tv_sec = 10; outtime.tv_usec = 0;// 300000; } else { outtime.tv_sec = 5; outtime.tv_usec = 0; } //fcntl(socket_control ,F_SETFL, ~O_NONBLOCK); //set = setsockopt( socket_control, SOL_SOCKET, SO_RCVTIMEO, &outtime, sizeof(outtime) ); //connect to the server if(connect( socket_control, (struct sockaddr *) &ftp_server, sizeof(struct sockaddr_in) ) <0 ) { memset( log, 0, 1000 ); sprintf(log,"Fun(connectFtpServer): Can't connet to the server:%s,port:%d\n\n",inet_ntoa(ftp_server.sin_addr),ntohs(ftp_server.sin_port)); plog( "%s", log ); close(socket_control); return -1; } else { memset( log, 0, 1000 ); sprintf( log, "Fun(connectFtpServer): Successfully connect to server:%s,port:%d\n",inet_ntoa(ftp_server.sin_addr),ntohs(ftp_server.sin_port)); plog( "%s", log ); } error = ftp_get_reply( socket_control, NULL ); if( error == 421 )//There are too many connections from your internet address. { plog("%s\n\n","Fun(connectFtpServer): 421 too many connections."); close(socket_control); return -1; } else if( error == 220)//Successfully connect { if( (error = ftp_login(socket_control, user, password)) == -1 ) { plog("%s\n\n", "Fun(connectFtpServer): user or password error."); close(socket_control); return -1; } plog("Fun(connectFtpServer): socket_control = %d\n",socket_control); return socket_control; } else { plog("%s\n\n", "Fun(connectFtpServer): Connect error!"); close(socket_control); return -1; } }
int ftp_put(st_ftp *s) { int ret,error; char src_file[512],dst_file[512],send_buff[512],put_cmd[512]; struct stat file_info; int local_file; int file_put_sock,new_sock,count=0,i=0; int set=sizeof(local_host); memset(src_file,0,sizeof(src_file)); memset(dst_file,0,sizeof(dst_file)); memset(send_buff,0,sizeof(send_buff)); memset(put_cmd,0,sizeof(put_cmd)); sprintf(put_cmd,"put %s",s->putFileName); if((ret = ftp_login(s->ip, s->port, s->user, s->password)) == -1) // 登录ftp { printf("Password error!\n"); return -1; } else if(ret == -2) { printf("User error!\n"); return -1; } if(local_cd(s->localDir) != 0) // 切换到本地目录 { printf("local cd error !\n"); return -1; } if(ftp_cd(s->ftpDir) != 0)// 改变ftp服务器的目录 { printf("ftp cd error !\n"); return -1; } ftp_cmd_filename(put_cmd,src_file,dst_file);// ftp操作命令 if((stat(src_file,&file_info))<0) // 获取文件信息 { printf("local file %s doesn't exist!\n",src_file); close(sock_control); return -1; } local_file=open(src_file,O_RDONLY); if(local_file<0) { printf("Open file error\n"); close(sock_control); return -2; } file_put_sock=ftp_connect_data(); if(file_put_sock<0) { ftp_get_reply(sock_control); printf("Creat data socket error!\n"); close(local_file); close(sock_control); return -3; } ftp_send_cmd("TYPE I",NULL,sock_control);/* 数据类型(A=ASCII,E=EBCDIC,I=binary) */ error = ftp_get_reply(sock_control); printf("ftp reply number:<%d>, fun<%s> line<%d>\n",error,__FUNCTION__,__LINE__); if(error != 200) { printf("commond :<TYPE I> error!,line<%d>\n",__LINE__); return -1; } ftp_send_cmd("STOR ",dst_file,sock_control);/* 储存(复制)文件到服务器上 */ error = ftp_get_reply(sock_control); printf("ftp reply number:<%d>, fun<%s> line<%d>\n",error,__FUNCTION__,__LINE__); if(error != 150) { printf("commond :<STOR> error!,line<%d>\n",__LINE__); return -1; } printf("put file begin,please wait ...\n"); while(1) { count=read(local_file,send_buff,sizeof(send_buff)); if(count<=0) { break; } else { if((write(file_put_sock,send_buff,count)) == -1) { printf("write err!\n"); break; } } } printf("put file succ!\n"); close(local_file); close(file_put_sock); ftp_get_reply(sock_control); ftp_quit(); return 0; }
int ftp_get(st_ftp *s) { int ret,error; int get_sock, set, new_sock, i = 0; char src_file[512],dst_file[512],rcv_buf[512],get_cmd[512]; int local_file; int count = 0; struct stat file_info; memset(src_file,0,sizeof(src_file)); memset(dst_file,0,sizeof(dst_file)); memset(rcv_buf,0,sizeof(rcv_buf)); memset(get_cmd,0,sizeof(get_cmd)); sprintf(get_cmd,"get %s",s->getFileName); if((ret = ftp_login(s->ip, s->port, s->user, s->password)) == -1) { printf("Password error!\n"); return -1; } else if(ret == -2) { printf("User error!\n"); return -1; } if(local_cd(s->localDir) != 0) { printf("local cd error !\n"); return -1; } if(ftp_cd(s->ftpDir) != 0) { printf("ftp cd error !\n"); return -1; } ftp_cmd_filename(get_cmd, src_file, dst_file); if(!stat(dst_file, &file_info)) { printf("local file %s exists: %d bytes\n", dst_file, (int)file_info.st_size); if(!s->getCoverFalgs) { close(sock_control); return -1; } } local_file = open(dst_file, O_CREAT|O_TRUNC|O_WRONLY); if(local_file < 0) { printf("creat local file %s error!\n", dst_file); close(sock_control); return -2; } get_sock = ftp_connect_data(); if(get_sock < 0) { printf("socket error!\n"); close(sock_control); return -3; } set = sizeof(local_host); ftp_send_cmd("TYPE I", NULL, sock_control); error = ftp_get_reply(sock_control); printf("ftp reply number:<%d>, fun<%s> line<%d>\n",error,__FUNCTION__,__LINE__); if(error != 200) { printf("commond :<TYPE I> error!,line<%d>\n",__LINE__); return -1; } ftp_send_cmd("RETR ", src_file, sock_control);/* 从服务器上找回(复制)文件 */ error = ftp_get_reply(sock_control); printf("ftp reply number:<%d>, fun<%s> line<%d>\n",error,__FUNCTION__,__LINE__); if(error != 150) { printf("commond :<RETR> error!,line<%d>\n",__LINE__); return -1; } printf("get file begin,please wait ...\n"); while(1) { count = read(get_sock, rcv_buf, sizeof(rcv_buf)); if(count <= 0) { break; } else { if((write(local_file, rcv_buf, count)) == -1) { printf("write err!\n"); break; } } } printf("get file succ!\n"); close(local_file); close(get_sock); ftp_get_reply(sock_control); ftp_quit(); return 0; }
int ftp_login(char * server_ip, int port, char * user, char * password) { int error; if(port<=0||port>=(1<<16)) { cmd_err_exit("Invalid port!",254); } bzero(&ftp_server,sizeof(struct sockaddr_in)); ftp_server.sin_family=AF_INET; ftp_server.sin_port=htons(port); if(inet_addr(server_ip)!=-1) { ftp_server.sin_addr.s_addr=inet_addr(server_ip); } else { if((server_hostent=gethostbyname(server_ip))!=0) { memcpy(&ftp_server.sin_addr,server_hostent->h_addr,sizeof(ftp_server.sin_addr)); } else { cmd_err_exit("Invalid address!",253); } } sock_control=ftp_connect(&ftp_server,1); /* 建立控制通道 */ if((error=ftp_get_reply(sock_control))!=220)/* 对新用户准备好 */ { cmd_err_exit("Connect error!",220); } if(ftp_send_cmd("USER ", user, sock_control) < 0) { cmd_err_exit("Can not send message",1); } error = ftp_get_reply(sock_control); printf("ftp reply number:<%d>, fun<%s> line<%d>\n",error,__FUNCTION__,__LINE__); if(error == 331) { if(ftp_send_cmd("PASS ", password, sock_control) <= 0) { cmd_err_exit("Can not send message",1); } else { error = ftp_get_reply(sock_control); } if(error != 230) { printf("Password error!\n"); close(sock_control); return -1; } printf("login succ!\n"); return 0; } else { printf("User error!\n"); close(sock_control); return -2; } }