📄 ftpclient.c
字号:
}static FILE *ftp_login(ftp_host_info_t *server){ FILE *control_stream; char buf[512]; /* Connect to the command socket */ control_stream = fdopen(xconnect_stream(server->lsa), "r+"); if (control_stream == NULL) { /* fdopen failed - extremely unlikely */ bb_error_msg_and_die("ftp login"); } if (ftpcmd(NULL, NULL, control_stream, buf) != 220) { ftp_die(NULL, buf); } /* Login to the server */ switch (ftpcmd("USER", server->user, control_stream, buf)) { case 230: break; case 331: if (ftpcmd("PASS", server->password, control_stream, buf) != 230) { ftp_die("PASS", buf); } break; default: ftp_die("USER", buf); } ftpcmd("TYPE I", NULL, control_stream, buf); return control_stream;}staticint 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"); } if (sbuf.st_size > 0) { beg_range = sbuf.st_size; } else { do_continue = 0; } } if (do_continue) { sprintf(buf, "REST %""1""d", 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;}staticint ftp_receive_head(FILE *control_stream, int *pnFdData, int *pnFileSize, ftp_host_info_t *server, char *server_path, char caFtpHead[FTP_HEAD_SIZE]){ 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); return rd;}staticint 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 %""1""u", 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;}#define FTPGETPUT_OPT_CONTINUE 1#define FTPGETPUT_OPT_VERBOSE 2#define FTPGETPUT_OPT_USER 4#define FTPGETPUT_OPT_PASSWORD 8#define FTPGETPUT_OPT_PORT 16FILE* ftp_get_head(const char *host, int port, const char *user, const char *password, char *server_path, char caFtpHead[FTP_HEAD_SIZE], int *pnHeadSize, int *pnFdData, FILE *ctrl_stream, int *pnFileSize){ ftp_host_info_t server; FILE *control_stream; char caPort[10]; server.user = user; server.password = password; server.lsa = str2sockaddr(host, port, DIE_ON_ERROR); control_stream = ftp_login(&server); memcpy(ctrl_stream, control_stream, sizeof(FILE)); *pnHeadSize = ftp_receive_head(control_stream, pnFdData, pnFileSize, &server, server_path, caFtpHead); return control_stream;}int ftp_recv_to_file(int nFdData, FILE *control_stream, char caFtpHead[FTP_HEAD_SIZE], int nHeadSize, const char *pcLocalFileName){ int fd_local = -1; char buf[512]; int status = -1; off_t total = 0; char buffer[BUFSIZ]; int size=0; fd_local = xopen(pcLocalFileName, O_CREAT | O_TRUNC | O_WRONLY, 0666); if (fd_local < 0) { goto out; } ssize_t wr = full_write(fd_local, caFtpHead, nHeadSize); if (wr < nHeadSize) { //bb_error_msg(bb_msg_write_error); goto out; } total += nHeadSize; if (!size) { size = BUFSIZ; status = 1; /* copy until eof */ } while (1) { ssize_t rd; rd = safe_read(nFdData, buffer, size > BUFSIZ ? BUFSIZ : size);//printf("-----rd =%d----\n", rd); if (!rd) { /* eof - all done */ status = 0; break; } if (rd < 0) { //bb_error_msg(bb_msg_read_error); break; } /* dst_fd == -1 is a fake, else... */ if (fd_local >= 0) { ssize_t wr = full_write(fd_local, buffer, rd); if (wr < rd) { //bb_error_msg(bb_msg_write_error); break; } } total += rd; if (status < 0) { /* if we aren't copying till EOF... */ size -= rd; if (!size) { /* 'size' bytes copied - all done */ status = 0; break; } } } /* close it all down */ close(fd_local); close(nFdData); if (ftpcmd(NULL, NULL, control_stream, buf) != 226) { ftp_die(NULL, buf); } ftpcmd("QUIT", NULL, control_stream, buf); out: return status ? -1 : total;}int ftp_recv_to_ram(int nFdData, FILE *control_stream, char caFtpHead[FTP_HEAD_SIZE], int nHeadSize, char *pcRam){ int fd_local = -1; char buf[512]; int status = -1; off_t total = 0; char buffer[BUFSIZ]; int size=0; memcpy(pcRam, caFtpHead, nHeadSize); total += nHeadSize; while (1) { ssize_t rd; rd = safe_read(nFdData, buffer, BUFSIZ);//printf("-----rd =%d----\n", rd); if (!rd) { /* eof - all done */ status = 0; break; } if (rd < 0) { //bb_error_msg(bb_msg_read_error); break; } memcpy(pcRam+total, buffer, rd); total += rd; if (status < 0) { /* if we aren't copying till EOF... */ size -= rd; if (!size) { /* 'size' bytes copied - all done */ status = 0; break; } } } /* close it all down */ close(nFdData); if (ftpcmd(NULL, NULL, control_stream, buf) != 226) { ftp_die(NULL, buf); } ftpcmd("QUIT", NULL, control_stream, buf); return status ? -1 : total;}int main(){ /* content-length of the file */ unsigned opt; const char *port = "ftp"; ftp_host_info_t *server; /* socket to ftp server */ FILE *control_stream; static FILE ctrl_stream; /* continue previous transfer (-c) */ char caFtpHead[FTP_HEAD_SIZE]; int nFileSize; int rd = 0; int nFdData; int nSave; char *pcRam; control_stream = ftp_get_head("192.168.0.139", 21, "xyx", "123456", "sip11.cfg", caFtpHead, &rd, &nFdData, &ctrl_stream, &nFileSize); caFtpHead[rd-1] = '\0'; printf("-------rd=%d===nFileSize=%d===\n", rd, nFileSize); printf("-------------------------\n %s \n------------------------\n",caFtpHead); //nSave = ftp_recv_to_file(nFdData, control_stream, // caFtpHead, rd,"test.cfg"); pcRam = (char*)calloc(sizeof(char), nFileSize); if(NULL == pcRam) { perror("calloc error\n"); exit(-1); } nSave = ftp_recv_to_ram(nFdData, control_stream, caFtpHead, rd, pcRam); printf("nSave =%d \n", nSave); pcRam[200] = '\0'; printf("-------------------------\n %s \n------------------------\n",pcRam+130000);return 0; /* Set default values */ server = xmalloc(sizeof(*server)); server->user = "xyx"; server->password = "123456"; /* We want to do exactly _one_ DNS lookup, since some * sites (i.e. ftp.us.debian.org) use round-robin DNS * and we want to connect to only one IP... */ server->lsa = str2sockaddr("192.168.0.139", 21, DIE_ON_ERROR); printf("BUFSIZ=%d off_t = %d\n", BUFSIZ, (off_t)-1); /* Connect/Setup/Configure the FTP session */ control_stream = ftp_login(server); ftp_receive(server, control_stream, "ram.bin", "ram_zimage.bin"); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -