static int ftp_receive_head(FILE *control_stream, int *pnFdData, int *pnFileSize, ftp_host_info_t *server, char *server_path, char caFtpHead[FTP_HEAD_SIZE]) { printf("ftp_receive_head++++++++++++++++++++++\n"); char buf[512]; int rd; /* Connect to the data socket */ if (ftpcmd("PASV", NULL, control_stream, buf) != 227) { ftp_die("PASV", buf); } *pnFdData = xconnect_ftpdata(server, buf); if (*pnFdData < 0) return -1; if (ftpcmd("SIZE", server_path, control_stream, buf) == 213) { *pnFileSize = strtol(buf + 4, NULL, 10); if (*pnFileSize < 0) ftp_die("SIZE", buf); } else { do_continue = 0; } if (ftpcmd("RETR", server_path, control_stream, buf) > 150) { ftp_die("RETR", buf); } rd = safe_read(*pnFdData, caFtpHead, FTP_HEAD_SIZE); printf("ftp_receive_head=============\n"); return rd; }
static int ftp_send(FILE *control_stream, const char *host, const char *server_path, char *local_path) { struct stat sbuf; char buf[512]; int fd_data; int fd_local; int response; /* Connect to the data socket */ if (ftpcmd("PASV", NULL, control_stream, buf) != 227) { bb_error_msg_and_die("PASV error: %s", buf + 4); } fd_data = xconnect_ftpdata(host, buf); if (ftpcmd("CWD ", server_path, control_stream, buf) != 250) { bb_error_msg_and_die("CWD error: %s", buf + 4); } /* get the local file */ fd_local = bb_xopen(local_path, O_RDONLY); fstat(fd_local, &sbuf); sprintf(buf, "ALLO %lu", (unsigned long)sbuf.st_size); response = ftpcmd(buf, NULL, control_stream, buf); switch (response) { case 200: case 202: break; default: close(fd_local); bb_error_msg_and_die("ALLO error: %s", buf + 4); break; } response = ftpcmd("STOR ", local_path, control_stream, buf); switch (response) { case 125: case 150: break; default: close(fd_local); bb_error_msg_and_die("STOR error: %s", buf + 4); } /* transfer the file */ if (bb_copyfd(fd_local, fd_data, 0) == -1) { exit(EXIT_FAILURE); } /* close it all down */ close(fd_data); if (ftpcmd(NULL, NULL, control_stream, buf) != 226) { bb_error_msg_and_die("error: %s", buf + 4); } ftpcmd("QUIT", NULL, control_stream, buf); return(EXIT_SUCCESS); }
static int ftp_send(ftp_host_info_t *server, FILE *control_stream, const char *server_path, char *local_path) { struct stat sbuf; char buf[512]; int fd_data; int fd_local; int response; /* Connect to the data socket */ if (ftpcmd("PASV", NULL, control_stream, buf) != 227) { ftp_die("PASV", buf); } fd_data = xconnect_ftpdata(server, buf); /* get the local file */ fd_local = STDIN_FILENO; if (NOT_LONE_DASH(local_path)) { fd_local = xopen(local_path, O_RDONLY, 0666); fstat(fd_local, &sbuf); sprintf(buf, "ALLO %lu", (long unsigned int)sbuf.st_size); response = ftpcmd(buf, NULL, control_stream, buf); switch (response) { case 200: case 202: break; default: close(fd_local); ftp_die("ALLO", buf); break; } } response = ftpcmd("STOR", server_path, control_stream, buf); switch (response) { case 125: case 150: break; default: close(fd_local); ftp_die("STOR", buf); } /* transfer the file */ if (bb_copyfd_eof(fd_local, fd_data) == -1) { exit(EXIT_FAILURE); } /* close it all down */ close(fd_data); if (ftpcmd(NULL, NULL, control_stream, buf) != 226) { ftp_die("close", buf); } ftpcmd("QUIT", NULL, control_stream, buf); return EXIT_SUCCESS; }
static int ftp_recieve(ftp_host_info_t *server, FILE *control_stream, const char *local_path, char *server_path) { char buf[512]; off_t filesize = 0; int fd_data; int fd_local = -1; off_t beg_range = 0; /* Connect to the data socket */ if (ftpcmd("PASV", NULL, control_stream, buf) != 227) { bb_error_msg_and_die("PASV error: %s", buf + 4); } fd_data = xconnect_ftpdata(server, buf); if (ftpcmd("SIZE ", server_path, control_stream, buf) == 213) { unsigned long value=filesize; if (safe_strtoul(buf + 4, &value)) bb_error_msg_and_die("SIZE error: %s", buf + 4); filesize = value; } if ((local_path[0] == '-') && (local_path[1] == '\0')) { fd_local = STDOUT_FILENO; do_continue = 0; } if (do_continue) { struct stat sbuf; if (lstat(local_path, &sbuf) < 0) { bb_perror_msg_and_die("fstat()"); } if (sbuf.st_size > 0) { beg_range = sbuf.st_size; } else { do_continue = 0; } } if (do_continue) { sprintf(buf, "REST %ld", (long)beg_range); if (ftpcmd(buf, NULL, control_stream, buf) != 350) { do_continue = 0; } else { filesize -= beg_range; } } if (ftpcmd("RETR ", server_path, control_stream, buf) > 150) { bb_error_msg_and_die("RETR error: %s", buf + 4); } /* only make a local file if we know that one exists on the remote server */ if (fd_local == -1) { if (do_continue) { fd_local = bb_xopen(local_path, O_APPEND | O_WRONLY); } else { fd_local = bb_xopen(local_path, O_CREAT | O_TRUNC | O_WRONLY); } } /* Copy the file */ if (bb_copyfd_size(fd_data, fd_local, filesize) == -1) { exit(EXIT_FAILURE); } /* close it all down */ close(fd_data); if (ftpcmd(NULL, NULL, control_stream, buf) != 226) { bb_error_msg_and_die("ftp error: %s", buf + 4); } ftpcmd("QUIT", NULL, control_stream, buf); return(EXIT_SUCCESS); }
static int ftp_receive(ftp_host_info_t *server, FILE *control_stream, const char *local_path, char *server_path) { char buf[512]; /* I think 'filesize' usage here is bogus. Let's see... */ //off_t filesize = -1; #define filesize ((off_t)-1) int fd_data; int fd_local = -1; off_t beg_range = 0; /* Connect to the data socket */ if (ftpcmd("PASV", NULL, control_stream, buf) != 227) { ftp_die("PASV", buf); } fd_data = xconnect_ftpdata(server, buf); if (ftpcmd("SIZE", server_path, control_stream, buf) == 213) { //filesize = BB_STRTOOFF(buf + 4, NULL, 10); //if (errno || filesize < 0) // ftp_die("SIZE", buf); } else { do_continue = 0; } if (LONE_DASH(local_path)) { fd_local = STDOUT_FILENO; do_continue = 0; } if (do_continue) { struct stat sbuf; if (lstat(local_path, &sbuf) < 0) { //bb_error_msg_and_die("lstat"); printf("lstat error."); exit - 1; } if (sbuf.st_size > 0) { beg_range = sbuf.st_size; } else { do_continue = 0; } } if (do_continue) { sprintf(buf, "REST %""1""d", (int)beg_range); if (ftpcmd(buf, NULL, control_stream, buf) != 350) { do_continue = 0; } else { //if (filesize != -1) // filesize -= beg_range; } } if (ftpcmd("RETR", server_path, control_stream, buf) > 150) { ftp_die("RETR", buf); } /* only make a local file if we know that one exists on the remote server */ if (fd_local == -1) { if (do_continue) { fd_local = xopen(local_path, O_APPEND | O_WRONLY, 0666); } else { fd_local = xopen(local_path, O_CREAT | O_TRUNC | O_WRONLY, 0666); } } /* Copy the file */ if (filesize != -1) { if (bb_copyfd_size(fd_data, fd_local, filesize) == -1) return EXIT_FAILURE; } else { if (bb_copyfd_eof(fd_data, fd_local) == -1) return EXIT_FAILURE; } /* close it all down */ close(fd_data); if (ftpcmd(NULL, NULL, control_stream, buf) != 226) { ftp_die(NULL, buf); } ftpcmd("QUIT", NULL, control_stream, buf); return EXIT_SUCCESS; }
/************************************************************************************************** * 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); } } } }
static int ftp_recieve(FILE *control_stream, const char *host, const char *local_path, char *server_path) { char *filename; char *local_file; char buf[512]; off_t filesize = 0; int fd_data; int fd_local; off_t beg_range = 0; filename = bb_get_last_path_component(server_path); local_file = concat_path_file(local_path, filename); /* Connect to the data socket */ if (ftpcmd("PASV", NULL, control_stream, buf) != 227) { bb_error_msg_and_die("PASV error: %s", buf + 4); } fd_data = xconnect_ftpdata(host, buf); if (ftpcmd("SIZE ", server_path, control_stream, buf) == 213) { filesize = atol(buf + 4); } if (do_continue) { struct stat sbuf; if (lstat(local_file, &sbuf) < 0) { bb_perror_msg_and_die("fstat()"); } if (sbuf.st_size > 0) { beg_range = sbuf.st_size; } else { do_continue = 0; } } if (do_continue) { sprintf(buf, "REST %ld", (long)beg_range); if (ftpcmd(buf, NULL, control_stream, buf) != 350) { do_continue = 0; } else { filesize -= beg_range; } } if (ftpcmd("RETR ", server_path, control_stream, buf) > 150) { bb_error_msg_and_die("RETR error: %s", buf + 4); } /* only make a local file if we know that one exists on the remote server */ if (do_continue) { fd_local = bb_xopen(local_file, O_APPEND | O_WRONLY); } else { fd_local = bb_xopen(local_file, O_CREAT | O_TRUNC | O_WRONLY); } /* Copy the file */ if (bb_copyfd(fd_data, fd_local, filesize) == -1) { exit(EXIT_FAILURE); } /* close it all down */ close(fd_data); if (ftpcmd(NULL, NULL, control_stream, buf) != 226) { bb_error_msg_and_die("ftp error: %s", buf + 4); } ftpcmd("QUIT", NULL, control_stream, buf); return(EXIT_SUCCESS); }