📄 ftplib.c
字号:
// perror("setsockopt"); net_close(sControl); return 0; } if (connect(sControl, (struct sockaddr *)&sin, sizeof(sin)) == -1) {/* This original code is commented out due to the conversion changes */// perror("connect"); net_close(sControl); return 0; } ctrl = calloc(1,sizeof(netbuf)); if (ctrl == NULL) {/* This original code is commented out due to the conversion changes */// perror("calloc"); net_close(sControl); return 0; } ctrl->buf = malloc(FTPLIB_BUFSIZ); if (ctrl->buf == NULL) {/* This original code is commented out due to the conversion changes */// perror("calloc"); net_close(sControl); free(ctrl); return 0; } ctrl->handle = sControl; ctrl->dir = FTPLIB_CONTROL; ctrl->ctrl = NULL; ctrl->cmode = FTPLIB_DEFMODE; ctrl->idlecb = NULL; ctrl->idletime.tv_sec = ctrl->idletime.tv_usec = 0; ctrl->idlearg = NULL; ctrl->xfered = 0; ctrl->xfered1 = 0; ctrl->cbbytes = 0; if (readresp('2', ctrl) == 0) { net_close(sControl); free(ctrl->buf); free(ctrl); return 0; } *nControl = ctrl; return 1;}/* * FtpOptions - change connection options * * returns 1 if successful, 0 on error *//* This original code is commented out due to the conversion changes *///GLOBALDEF int FtpOptions(int opt, long val, netbuf *nControl)int FtpOptions(int opt, long val, netbuf *nControl){ int v,rv=0; switch (opt) { case FTPLIB_CONNMODE: v = (int) val; if ((v == FTPLIB_PASSIVE) || (v == FTPLIB_PORT)) { nControl->cmode = v; rv = 1; } break; case FTPLIB_CALLBACK: nControl->idlecb = (FtpCallback) val; rv = 1; break; case FTPLIB_IDLETIME: v = (int) val; rv = 1; nControl->idletime.tv_sec = v / 1000; nControl->idletime.tv_usec = (v % 1000) * 1000; break; case FTPLIB_CALLBACKARG: rv = 1; nControl->idlearg = (void *) val; break; case FTPLIB_CALLBACKBYTES: rv = 1; nControl->cbbytes = (int) val; break; } return rv;}/* * FtpSendCmd - send a command and wait for expected response * * return 1 if proper response received, 0 otherwise */static int FtpSendCmd(const char *cmd, char expresp, netbuf *nControl){ char buf[256]; if (nControl->dir != FTPLIB_CONTROL) return 0; if (ftplib_debug > 2) fprintf(stderr,"%s\n",cmd); if ((strlen(cmd) + 3) > sizeof(buf)) return 0; sprintf(buf,"%s\r\n",cmd); if (net_write(nControl->handle,buf,strlen(buf)) <= 0) {/* This original code is commented out due to the conversion changes */// perror("write"); return 0; } return readresp(expresp, nControl);}/* * FtpLogin - log in to remote server * * return 1 if logged in, 0 otherwise *//* This original code is commented out due to the conversion changes *///GLOBALDEF int FtpLogin(const char *user, const char *pass, netbuf *nControl)int FtpLogin(const char *user, const char *pass, netbuf *nControl){ char tempbuf[64]; if (((strlen(user) + 7) > sizeof(tempbuf)) || ((strlen(pass) + 7) > sizeof(tempbuf))) return 0; sprintf(tempbuf,"USER %s",user); if (!FtpSendCmd(tempbuf,'3',nControl)) { if (nControl->response[0] == '2') return 1; return 0; } sprintf(tempbuf,"PASS %s",pass); return FtpSendCmd(tempbuf,'2',nControl);}/* * FtpOpenPort - set up data connection * * return 1 if successful, 0 otherwise *//* This original code is commented out due to the conversion changes *///static int FtpOpenPort(const char *host, netbuf *nControl, netbuf **nData, int mode, int dir)static int FtpOpenPort(netbuf *nControl, netbuf **nData, int mode, int dir){ int sData; struct sockaddr_in in; char ip_addr[30]; char *ip_tmp; int i; unsigned int port = 0, portOctet1 = 0, portOctet2 = 0;/* This original code is commented out due to the conversion changes *//* union { struct sockaddr sa; struct sockaddr_in in; } sin; struct linger lng = { 0, 0 };*/ int on=1; netbuf *ctrl; char *cp;/* This original code is commented out due to the conversion changes */// unsigned int v[6];// char buf[256]; if (nControl->dir != FTPLIB_CONTROL) return -1; if ((dir != FTPLIB_READ) && (dir != FTPLIB_WRITE)) { sprintf(nControl->response, "Invalid direction %d\n", dir); return -1; } if ((mode != FTPLIB_ASCII) && (mode != FTPLIB_IMAGE)) { sprintf(nControl->response, "Invalid mode %c\n", mode); return -1; }/* This original code is commented out due to the conversion changes *//* l = sizeof(sa); l = sizeof(sin); if (nControl->cmode == FTPLIB_PASSIVE) { memset(&sa, 0, l); memset(&sin, 0, l); in.sin_family = AF_INET; sin.in.sin_family = AF_INET;*/ if (!FtpSendCmd("PASV",'2',nControl)) return -1; cp = strchr(nControl->response,'('); cp++; if (cp == NULL) return -1;// Process address and port// First, get the address ip_tmp = strtok(cp, ","); strcpy(ip_addr,ip_tmp); for(i=0;i<3;i++) { strcat(ip_addr, "."); ip_tmp = strtok(NULL, ","); strcat(ip_addr,ip_tmp); }// Now get the port number portOctet1 = atoi(strtok(NULL, ",")); portOctet2 = atoi(strtok(NULL, ")")); port = portOctet1 * 256 + portOctet2;/* This original code is commented out due to the conversion changes */ /* sscanf(cp,"%u,%u,%u,%u,%u,%u",&v[2],&v[3],&v[4],&v[5],&v[0],&v[1]); sa.sa_data[2] = v[2]; sa.sa_data[3] = v[3]; sa.sa_data[4] = v[4]; sa.sa_data[5] = v[5]; sa.sa_data[0] = v[0]; sa.sa_data[1] = v[1]; } else { if (getsockname(nControl->handle, &sin.sa, &l) < 0) { perror("getsockname"); return 0; } }*/ sData = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP); if (sData == -1) {/* This original code is commented out due to the conversion changes */// perror("socket"); return -1; } if (setsockopt(sData,SOL_SOCKET,SO_REUSEADDR, SETSOCKOPT_OPTVAL_TYPE &on,sizeof(on)) == -1) {/* This original code is commented out due to the conversion changes */// perror("setsockopt"); net_close(sData); return -1; }/* This original code is commented out due to the conversion changes */// SO_LINGER cannot be used, not supported/* if (setsockopt(sData,SOL_SOCKET,SO_LINGER, SETSOCKOPT_OPTVAL_TYPE &lng,sizeof(lng)) == -1) { perror("setsockopt"); net_close(sData); getchar(); return -1; }*/ in.sin_family = AF_INET; in.sin_addr.s_addr = inet_addr(ip_addr); in.sin_port = htons(port); bzero(&(in.sin_zero), 8);/* This original code is commented out due to the conversion changes *//* if (nControl->cmode == FTPLIB_PASSIVE) { if (connect(sData, &sa, sizeof(sa)) == -1)*/ if (connect(sData, (struct sockaddr*)&in, sizeof(struct sockaddr)) == -1) {/* This original code is commented out due to the conversion changes */// perror("connect"); net_close(sData); return -1; }/* This original code is commented out due to the conversion changes *//* } else { if (bind(sData, &sin.sa, sizeof(sin)) == -1) { perror("bind"); net_close(sData); return 0; } if (listen(sData, 1) < 0) { perror("listen"); net_close(sData); return 0; } if (getsockname(sData, &sin.sa, &l) < 0) return 0; sprintf(buf, "PORT %d,%d,%d,%d,%d,%d", (unsigned char) sin.sa.sa_data[2], (unsigned char) sin.sa.sa_data[3], (unsigned char) sin.sa.sa_data[4], (unsigned char) sin.sa.sa_data[5], (unsigned char) sin.sa.sa_data[0], (unsigned char) sin.sa.sa_data[1]); if (!FtpSendCmd(buf,'2',nControl)) { net_close(sData); return 0; } }*/ ctrl = calloc(1,sizeof(netbuf)); if (ctrl == NULL) {/* This original code is commented out due to the conversion changes */// perror("calloc"); net_close(sData); return -1; } if ((mode == 'A') && ((ctrl->buf = malloc(FTPLIB_BUFSIZ)) == NULL)) {/* This original code is commented out due to the conversion changes */// perror("calloc"); net_close(sData); free(ctrl); return -1; } ctrl->handle = sData; ctrl->dir = dir; ctrl->idletime = nControl->idletime; ctrl->idlearg = nControl->idlearg; ctrl->xfered = 0; ctrl->xfered1 = 0; ctrl->cbbytes = nControl->cbbytes; if (ctrl->idletime.tv_sec || ctrl->idletime.tv_usec || ctrl->cbbytes) ctrl->idlecb = nControl->idlecb; else ctrl->idlecb = NULL; *nData = ctrl; return 1;}/* * FtpAcceptConnection - accept connection from server * * return 1 if successful, 0 otherwise */static int FtpAcceptConnection(netbuf *nData, netbuf *nControl){ int sData; struct sockaddr addr; unsigned int l; int i; struct timeval tv; fd_set mask; int rv = 0; FD_ZERO(&mask); FD_SET(nControl->handle, &mask); FD_SET(nData->handle, &mask); tv.tv_usec = 0; tv.tv_sec = ACCEPT_TIMEOUT; i = nControl->handle; if (i < nData->handle) i = nData->handle; i = select(i+1, &mask, NULL, NULL, &tv); if (i == -1) { strncpy(nControl->response, strerror(errno), sizeof(nControl->response)); net_close(nData->handle); nData->handle = 0; rv = 0; } else if (i == 0) { strcpy(nControl->response, "timed out waiting for connection"); net_close(nData->handle); nData->handle = 0; rv = 0; } else { if (FD_ISSET(nData->handle, &mask)) { l = sizeof(addr); sData = accept(nData->handle, &addr, &l); i = errno; net_close(nData->handle); if (sData > 0) { rv = 1; nData->handle = sData; } else { strncpy(nControl->response, strerror(i), sizeof(nControl->response)); nData->handle = 0; rv = 0; } } else if (FD_ISSET(nControl->handle, &mask)) { net_close(nData->handle); nData->handle = 0; readresp('2', nControl); rv = 0; } } return rv; }/* * FtpAccess - return a handle for a data stream * * return 1 if successful, 0 otherwise *//* This original code is commented out due to the conversion changes *///GLOBALDEF int FtpAccess(const char *path, int typ, int mode, netbuf *nControl,int FtpAccess(const char *path, int typ, int mode, netbuf *nControl, netbuf **nData){ char buf[256]; int dir; if ((path == NULL) && ((typ == FTPLIB_FILE_WRITE) || (typ == FTPLIB_FILE_READ))) { sprintf(nControl->response, "Missing path argument for file transfer\n"); return 0; } sprintf(buf, "TYPE %c", mode); if (!FtpSendCmd(buf, '2', nControl)) return 0; switch (typ) { case FTPLIB_DIR: strcpy(buf,"NLST"); dir = FTPLIB_READ; // Change path to NULL path = NULL; break; case FTPLIB_DIR_VERBOSE: strcpy(buf,"LIST"); dir = FTPLIB_READ; // Change path to NULL path = NULL; break; case FTPLIB_FILE_READ: strcpy(buf,"RETR"); dir = FTPLIB_READ; break; case FTPLIB_FILE_WRITE: strcpy(buf,"STOR"); dir = FTPLIB_WRITE; break; default: sprintf(nControl->response, "Invalid open type %d\n", typ); return 0; } if (path != NULL) { int i = strlen(buf); buf[i++] = ' '; if ((strlen(path) + i) >= sizeof(buf)) return 0; strcpy(&buf[i],path); } if (FtpOpenPort(nControl, nData, mode, dir) == -1) return 0; if (!FtpSendCmd(buf, '1', nControl)) { FtpClose(*nData); *nData = NULL; return 0; } (*nData)->ctrl = nControl; nControl->data = *nData; if (nControl->cmode == FTPLIB_PORT) { if (!FtpAcceptConnection(*nData,nControl)) { FtpClose(*nData); *nData = NULL; nControl->data = NULL; return 0; } } return 1;}/* * FtpRead - read from a data connection *//* This original code is commented out due to the conversion changes *///GLOBALDEF int FtpRead(void *buf, int max, netbuf *nData)int FtpRead(void *buf, int max, netbuf *nData){ int i; if (nData->dir != FTPLIB_READ)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -