ftpclient.c
来自「eCos操作系统源码」· C语言 代码 · 共 711 行 · 第 1/2 页
C
711 行
ftp_printf(1,"bind: %s\n",strerror(errno)); close(s); return FTP_BAD; } len = sizeof(local); if (getsockname(s,&local,&len) < 0) { ftp_printf(1,"getsockname: %s\n",strerror(errno)); close(s); return FTP_BAD; } if (listen(s, 1) < 0) { ftp_printf(1,"listen: %s\n",strerror(errno)); close(s); return FTP_BAD; } getnameinfo(&local,sizeof(local),name,sizeof(name), port, sizeof(port), NI_NUMERICHOST|NI_NUMERICSERV); switch (local.sa_family) { case AF_INET: { snprintf(buf, sizeof(buf), "|1|%s|%s|", name, port); break; }#ifdef CYGPKG_NET_INET6 case AF_INET6: { snprintf(buf, sizeof(buf), "|2|%s|%s|", name, port); break; }#endif default: close (s); return (FTP_BAD); } ret = command("EPRT",buf,ctrl_s,msgbuf,msgbuflen,ftp_printf); if (ret < 0) { close(s); return (ret); } if (ret != 2) { ftp_printf(1,"FTP: PORT failed!\n"); close(s); return (FTP_BAD); } return (s);}/* Receive the file into the buffer and close the data socket afterwards */static int receive_file(int data_s, char *buf, int buf_size,ftp_printf_t ftp_printf){ int remaining = buf_size; int finished = 0; int total_size=0; char *bufp = buf; int len; int s; s = accept(data_s,NULL,0); if (s<0) { ftp_printf(1,"listen: %s\n",strerror(errno)); return FTP_BAD; } do { len = read(s,bufp,remaining); if (len < 0) { ftp_printf(1,"read: %s\n",strerror(errno)); close(s); return FTP_BAD; } if (len == 0) { finished = 1; } else { total_size += len; remaining -= len; bufp += len; if (total_size == buf_size) { ftp_printf(1,"FTP: File too big!\n"); close(s); return FTP_TOOBIG; } } } while (!finished); close(s); return total_size;}/* Receive the file into the buffer and close the socket afterwards*/static int send_file(int data_s, char *buf, int buf_size,ftp_printf_t ftp_printf){ int remaining=buf_size; int finished = 0; char * bufp = buf; int len; int s; s = accept(data_s,NULL,0); if (s<0) { ftp_printf(1,"listen: %s\n",strerror(errno)); return FTP_BAD; } do { len = write(s,bufp,remaining); if (len < 0) { ftp_printf(1,"write: %s\n",strerror(errno)); close(s); return FTP_BAD; } if (len == remaining) { finished = 1; } else { remaining -= len; bufp += len; } } while (!finished); close(s); return 0;}/* All done, say bye, bye */static int quit(int s, char *msgbuf, unsigned msgbuflen, ftp_printf_t ftp_printf) { int ret; ret = command("QUIT",NULL,s,msgbuf,msgbuflen,ftp_printf); if (ret < 0) { return (ret); } if (ret != 2) { ftp_printf(1,"FTP: Quit failed!\n"); return (FTP_BAD); } ftp_printf(0,"FTP: Connection closed\n"); return (0);}/* Get a file from an FTP server. Hostname is the name/IP address of the server. username is the username used to connect to the server with. Passwd is the password used to authentificate the username. filename is the name of the file to receive. It should be the full pathname of the file. buf is a pointer to a buffer the contents of the file should be placed in and buf_size is the size of the buffer. If the file is bigger than the buffer, buf_size bytes will be retrieved and an error code returned. ftp_printf is a function to be called to perform printing. On success the number of bytes received is returned. On error a negative value is returned indicating the type of error. */int ftp_get(char * hostname, char * username, char * passwd, char * filename, char * buf, unsigned buf_size, ftp_printf_t ftp_printf){ struct sockaddr local; char msgbuf[256]; int s,data_s; int bytes; int ret; s = connect_to_server(hostname,&local,ftp_printf); if (s < 0) { return (s); } /* Read the welcome message from the server */ if (get_reply(s,ftp_printf) != 2) { ftp_printf(0,"FTP: Server refused connection\n"); close(s); return FTP_BAD; } ret = login(username,passwd,s,msgbuf,sizeof(msgbuf),ftp_printf); if (ret < 0) { close(s); return (ret); } /* We are now logged in and ready to transfer the file. Open the data socket ready to receive the file. It also build the PORT command ready to send */ data_s = opendatasock(s,&local,msgbuf,sizeof(msgbuf),ftp_printf); if (data_s < 0) { close (s); return (data_s); } /* Ask for the file */ ret = command("RETR",filename,s,msgbuf,sizeof(msgbuf),ftp_printf); if (ret < 0) { close(s); close(data_s); return (ret); } if (ret != 1) { ftp_printf(0,"FTP: RETR failed!\n"); close (data_s); close(s); return (FTP_BADFILENAME); } if ((bytes=receive_file(data_s,buf,buf_size,ftp_printf)) < 0) { ftp_printf(0,"FTP: Receiving file failed\n"); close (data_s); close(s); return (bytes); } if (get_reply(s,ftp_printf) != 2) { ftp_printf(0,"FTP: Transfer failed!\n"); close (data_s); close(s); return (FTP_BAD); } ret = quit(s,msgbuf,sizeof(msgbuf),ftp_printf); if (ret < 0) { close(s); close(data_s); return (ret); } close (data_s); close(s); return bytes;} /* Put a file on an FTP server. Hostname is the name/IP address of the server. username is the username used to connect to the server with. Passwd is the password used to authentificate the username. filename is the name of the file to receive. It should be the full pathname of the file. buf is a pointer to a buffer the contents of the file should be placed in and buf_size is the size of the buffer. ftp_printf is a function to be called to perform printing. On success 0 is returned. On error a negative value is returned indicating the type of error. */int ftp_put(char * hostname, char * username, char * passwd, char * filename, char * buf, unsigned buf_size, ftp_printf_t ftp_printf){ struct sockaddr local; char msgbuf[256]; int s,data_s; int ret; s = connect_to_server(hostname,&local,ftp_printf); if (s < 0) { return (s); } /* Read the welcome message from the server */ if (get_reply(s,ftp_printf) != 2) { ftp_printf(1,"FTP: Server refused connection\n"); close(s); return FTP_BAD; } ret = login(username,passwd,s,msgbuf,sizeof(msgbuf),ftp_printf); if (ret < 0) { close(s); return (ret); } /* We are now logged in and ready to transfer the file. Open the data socket ready to receive the file. It also build the PORT command ready to send */ data_s = opendatasock(s,&local,msgbuf,sizeof(msgbuf),ftp_printf); if (data_s < 0) { close (s); return (data_s); } /* Ask for the file */ ret = command("STOR",filename,s,msgbuf,sizeof(msgbuf),ftp_printf); if (ret < 0) { close(s); close(data_s); return (ret); } if (ret != 1) { ftp_printf(1,"FTP: STOR failed!\n"); close (data_s); close(s); return (FTP_BADFILENAME); } if ((ret = send_file(data_s,buf,buf_size,ftp_printf)) < 0) { ftp_printf(1,"FTP: Sending file failed\n"); close (data_s); close(s); return (ret); } if (get_reply(s,ftp_printf) != 2) { ftp_printf(1,"FTP: Transfer failed!\n"); close (data_s); close(s); return (FTP_BAD); } ret = quit(s,msgbuf,sizeof(msgbuf),ftp_printf); if (ret < 0) { close(s); close(data_s); return (ret); } close (data_s); close(s); return 0;}/* An example ftp_printf function. This uses the standard eCos diagoutput device for outputting error and diagnostic messages. Thefunction take one addition parameter to the normal printf function. Thefirst parameter indicates when the message is an error message whentrue. This can be used to filter errors from diagnostic output. Inthis example the error parameter is ignored and everything printed. */void ftpclient_printf(unsigned error, const char *fmt, ...) { va_list ap; va_start(ap, fmt); diag_vprintf( fmt, ap); va_end(ap);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?