int main(int argc,char *argv[]) { int ret; st_ftp s; memset(&s,0,sizeof(s)); s.getCoverFalgs = TRUE; /* 如果下载的文件本地已经存在,是否需要覆盖,TRUE覆盖 ,FALSE不覆盖 */ s.port = 21; strcpy(s.ip,"192.168.0.28"); strcpy(s.user,"rong"); strcpy(s.password,"111111"); strcpy(s.putFileName,"u.txt uuuu.txt"); /* 上传文件u.txt ,上传后修改名字为 uuuu.txt ( 为空 则不修改名字 ) */ strcpy(s.getFileName,"h.txt hhhh.txt"); /* 下载文件h.txt ,下载后修改名字为 hhhh.txt ( 为空 则不修改名字 ) */ strcpy(s.ftpDir,"hello"); /* ftp 服务器上的文件存放目录 */ strcpy(s.localDir,"/home/rong/ftp"); /* 本地的文件存放目录 */ /* 不修改名字: strcpy(s.putFileName,"u.txt"); strcpy(s.getFileName,"h.txt"); */ if((ret = ftp_put(&s)) != 0) { printf("ftp put error!\n"); } if((ret = ftp_get(&s)) != 0) { printf("ftp get error!\n"); } return 0; }
int ftp_send_response(SOCKET soc, char *buf, int buf_len){ buf[buf_len] = 0; printf("The Server received: '%s' cmd from client \n", buf); //get filename char file_name[FTP_FILENAME_LEN]; strcpy(file_name, buf+4); //文件下载 if (strncmp(buf,"get",3)==0) ftp_get(soc, file_name); //文件上传 else if (strncmp(buf,"put",3)==0) ftp_put(soc, file_name); else printf("the commod is not found\n"); return 0; }
/************************************************************************************************** * function : upload product files to product&service center ; * para : {void} * * return : {void} * * history : {2013.7.25 wujun} frist create; **************************************************************************************************/ void upload() { int i,j; char *filename,tempFilename; char *analysisCenterPath,*tempAnalysisCenterPath; char *productCenterPath, *tempProductCenterPath; int uploadState = 0; int ftperror = 0;//ftp relative error code int sockfd = 0; int len = 0; //downlaod starting time and end time char startTime[100] = {0}; char endTime[100] = {0}; time_t timer; while(1) { sleep(1); UploadNode * p,*p1; p = uploadList->next;//temporary variable always points to uploadList head pointer. p1 = uploadList; printf("upload..12.\n"); display(); //to traverse the upload task list, check whether there are some upload task or not. while( p!= NULL ) { printf("upload...\n"); sem_p(giSemUpload);//lock len = strlen(p->filename) + 1; filename = malloc(sizeof(char)*len); memset(filename,0,len); strcpy(filename, p->filename); len += strlen(".inmp"); tempFilename = malloc(sizeof(char)*len); memset(filename,0,len); sprintf(tempFilename,"%s%s",p->filename, ".inmp"); len = strlen(filename) + strlen(p->analysisCenterPath) + 1; analysisCenterPath = malloc(sizeof(char)*len); memset(analysisCenterPath,0,len); sprintf(analysisCenterPath,"%s%s",p->analysisCenterPath,filename); len = strlen(tempFilename) + strlen(analysisCenterPath) + 1; tempAnalysisCenterPath = malloc(sizeof(char)*len); memset(tempAnalysisCenterPath,0,len); sprintf(tempAnalysisCenterPath,"%s%s",p->analysisCenterPath,tempFilename); len = strlen(p->filename) + strlen(p->productCenterPath) + 1; productCenterPath = malloc(sizeof(char)*len); memset(productCenterPath,0,len); sprintf(productCenterPath,"%s%s",p->productCenterPath,p->filename); len = strlen(tempFilename) + strlen(p->productCenterPath) + 1; tempProductCenterPath = malloc(sizeof(char)*len); memset(tempProductCenterPath,0,len); sprintf(tempProductCenterPath,"%s%s",p->productCenterPath,tempFilename); uploadState = p->state; sem_v(giSemUpload);//unlock if( (uploadState == UPLOAD_FILE_EXIST) || (uploadState == UPLOAD_FILE_UNKNOWN) ) { //pthread_mutex_unlock(&uploadMutex);//unlock //we have three times to try to connect to ftp server ,if failed. Otherwise send network error. int conncectTimes = 0; while((sockfd = connectFtpServer(p->server->ip, p->server->port, p->server->username, p->server->passwd)) <= FTP_CONNECT_FAILED_FLAG) { if( MAX_CONNECT_TIMES <= ++conncectTimes ) { //to recod failed connnection time timer =time(NULL); memset(startTime, 0, 100); strftime( startTime, sizeof(startTime), "%Y-%m-%d %T",localtime(&timer) ); //add event log; sem_p(giSemLog);//lock addEventLog(UPLOAD_CONNNET_FAILED, filename, startTime,""); sem_v(giSemLog);//unlock #ifdef DEBUG printf("connect error.\n"); #endif break; } //delay some time and connect ftp again. //delay(); } if( sockfd > FTP_CONNECT_FAILED_FLAG ) { sem_p(giSemUpload);//lock p->state = UPLOAD_FILE_UPLOADING;//uploading state sem_v(&giSemUpload);//unlock #ifdef DEBUG printf("filename = %s,\nprefix%s\n",filename,analysisCenterPath); #endif // record starting-upload time timer =time(NULL); memset(startTime, 0, 100); strftime( startTime, sizeof(startTime), "%Y-%m-%d %T",localtime(&timer) ); /* * 1.check out whether the file's father dir do exist or not. * 2.if exist, then upload the specifed file; * 3.else, make dir and then upload the file; * 4.rename the file if upload it completely. */ ftp_mkdir(tempAnalysisCenterPath);//make sure the dir exists. ftperror = ftp_put(tempAnalysisCenterPath, tempProductCenterPath, sockfd); // record end-upload time timer =time(NULL); memset(endTime, 0, 100); strftime( endTime, sizeof(endTime), "%Y-%m-%d %T",localtime(&timer) ); //add log switch(ftperror) { case UPLOAD_CONNNET_FAILED: { sem_p(giSemLog);//lock addEventLog(L_UPLOAD_CONNNET_FAILED, filename, startTime,endTime); sem_v(giSemLog);//unlock sem_p(giSemUpload);//lock p->state = UPLOAD_FILE_UPLOAD_FAILED; sem_v(giSemUpload);//unlock break; } case UPLOAD_LOCAL_FILENAME_NULL: case UPLOAD_LOCAL_OPEN_ERROR: case UPLOAD_DATA_SOCKET_ERROR: case UPLOAD_PORT_MODE_ERROR: { sem_p(giSemLog);//lock addEventLog(L_UPLOAD_FAILED, filename, startTime,endTime); sem_v(giSemLog);//unlock sem_p(giSemUpload);//lock p->state = UPLOAD_FILE_UPLOAD_FAILED; sem_v(giSemUpload);//unlock break; } case UPLOAD_SUCCESS: { ftp_rename(tempProductCenterPath,productCenterPath);//if upload succsessfull, then rename the temporary file sem_p(giSemLog);//lock addEventLog(L_UPLOAD_SUCCESS, filename, startTime,endTime); sem_v(giSemLog);//unlock sem_p(giSemUpload);//lock p->state = UPLOAD_FILE_UPLOAD_SUCCESS; sem_v(giSemUpload);//unlock break; } default: break; } close(sockfd); } } else if((uploadState == UPLOAD_FILE_NONEXIST) || (uploadState == UPLOAD_FILE_UPLOAD_INTIME) || (uploadState == UPLOAD_FILE_UPLOAD_LATE)) { timer =time(NULL); memset(endTime, 0, 100); strftime( endTime, sizeof(endTime), "%Y-%m-%d %T",localtime(&timer) ); switch(uploadState) { case UPLOAD_FILE_NONEXIST: { sem_p(giSemLog);//lock addEventLog(L_UPLOAD_FILE_NOEXIST, filename, endTime,endTime); sem_v(giSemLog);//unlock break; } case UPLOAD_FILE_UPLOAD_INTIME: { sem_p(giSemLog);//lock addEventLog(L_UPLOAD_INTIME, filename, endTime,endTime); sem_v(giSemLog);//unlock break; } case UPLOAD_FILE_UPLOAD_LATE: { sem_p(giSemLog);//lock addEventLog(L_UPLOAD_LATE, filename, endTime,endTime); sem_v(giSemLog);//unlock break; } default: break; } sem_p(giSemUpload);//lock p1->next=p->next; ////freeUploadNode(p);//free(p); p=p1; sem_v(giSemUpload);//unlock } sem_p(giSemUpload);//lock p1=p; p=p->next; sem_v(giSemUpload);//lock } } }
/* TODO: IPv6 */ int sbbs_t::exec_net(csi_t* csi) { char str[512],rsp[512],buf[1025],ch,*p,**pp,**pp1,**pp2; ushort w; uint i; BOOL rd; int32_t *lp,*lp1,*lp2; time_t start; switch(*(csi->ip++)) { /* sub-op-code stored as next byte */ case CS_SOCKET_OPEN: lp=getintvar(csi,*(int32_t *)csi->ip); csi->ip+=4; csi->logic=LOGIC_FALSE; csi->socket_error=0; if(csi->sockets>=MAX_SOCKETS) return(0); if(lp!=NULL) { SOCKET sock=open_socket(SOCK_STREAM, NULL); if(sock!=INVALID_SOCKET) { SOCKADDR_IN addr; memset(&addr,0,sizeof(addr)); addr.sin_addr.s_addr = htonl(startup->outgoing4.s_addr); addr.sin_family = AF_INET; if((i=bind(sock, (struct sockaddr *) &addr, sizeof (addr)))!=0) { csi->socket_error=ERROR_VALUE; close_socket(sock); return(0); } *lp=sock; for(i=0;i<csi->sockets;i++) if(!csi->socket[i]) break; csi->socket[i]=*lp; if(i==csi->sockets) csi->sockets++; csi->logic=LOGIC_TRUE; } } return(0); case CS_SOCKET_CLOSE: lp=getintvar(csi,*(int32_t *)csi->ip); csi->ip+=4; csi->logic=LOGIC_FALSE; csi->socket_error=0; if(lp && *lp) { csi->logic=close_socket((SOCKET)*lp); csi->socket_error=ERROR_VALUE; for(i=0;i<csi->sockets;i++) if(csi->socket[i]==(SOCKET)*lp) csi->socket[i]=0; *lp=0; } return(0); case CS_SOCKET_CHECK: lp=getintvar(csi,*(int32_t *)csi->ip); csi->ip+=4; csi->logic=LOGIC_FALSE; csi->socket_error=0; if(lp==NULL || *lp==INVALID_SOCKET) return(0); if(socket_check(*lp,NULL,NULL,0)==TRUE) csi->logic=LOGIC_TRUE; else csi->socket_error=ERROR_VALUE; return(0); case CS_SOCKET_CONNECT: lp=getintvar(csi,*(int32_t *)csi->ip); /* socket */ csi->ip+=4; pp=getstrvar(csi,*(int32_t *)csi->ip); /* address */ csi->ip+=4; w=*(ushort *)csi->ip; /* port */ csi->ip+=2; csi->logic=LOGIC_FALSE; csi->socket_error=0; if(!lp || !*lp || !pp || !*pp || !w) return(0); ulong ip_addr; if((ip_addr=resolve_ip(*pp))==INADDR_NONE) return(0); SOCKADDR_IN addr; memset(&addr,0,sizeof(addr)); addr.sin_addr.s_addr = ip_addr; addr.sin_family = AF_INET; addr.sin_port = htons(w); if((i=connect(*lp, (struct sockaddr *)&addr, sizeof(addr)))!=0) { csi->socket_error=ERROR_VALUE; return(0); } csi->logic=LOGIC_TRUE; return(0); case CS_SOCKET_ACCEPT: lp1=getintvar(csi,*(int32_t *)csi->ip); /* socket */ csi->ip+=4; csi->socket_error=0; /* TODO */ return(0); case CS_SOCKET_NREAD: lp1=getintvar(csi,*(int32_t *)csi->ip); /* socket */ csi->ip+=4; lp2=getintvar(csi,*(int32_t *)csi->ip); /* var */ csi->ip+=4; csi->logic=LOGIC_FALSE; csi->socket_error=0; if(!lp1 || !lp2) return(0); if(ioctlsocket(*lp1, FIONREAD, (ulong*)lp2)==0) csi->logic=LOGIC_TRUE; else csi->socket_error=ERROR_VALUE; return(0); case CS_SOCKET_PEEK: case CS_SOCKET_READ: lp=getintvar(csi,*(int32_t *)csi->ip); /* socket */ csi->ip+=4; pp=getstrvar(csi,*(int32_t *)csi->ip); /* buffer */ csi->ip+=4; w=*(ushort *)csi->ip; /* length */ csi->ip+=2; csi->logic=LOGIC_FALSE; csi->socket_error=0; if(!lp || !pp) return(0); if(w<1 || w>sizeof(buf)-1) w=sizeof(buf)-1; if((i=recv(*lp,buf,w ,*(csi->ip-13)==CS_SOCKET_PEEK ? MSG_PEEK : 0))>0) { csi->logic=LOGIC_TRUE; buf[i]=0; if(csi->etx) { p=strchr(buf,csi->etx); if(p) *p=0; } *pp=copystrvar(csi,*pp,buf); } else csi->socket_error=ERROR_VALUE; return(0); case CS_SOCKET_READLINE: lp=getintvar(csi,*(int32_t *)csi->ip); /* socket */ csi->ip+=4; pp=getstrvar(csi,*(int32_t *)csi->ip); /* buffer */ csi->ip+=4; w=*(ushort *)csi->ip; /* length */ csi->ip+=2; csi->logic=LOGIC_FALSE; csi->socket_error=0; if(!lp || !pp) return(0); if(w<1 || w>sizeof(buf)-1) w=sizeof(buf)-1; start=time(NULL); for(i=0;i<w;) { if(!online) return(1); if(!socket_check(*lp,&rd,NULL,1000)) return(0); if(!rd) { if(time(NULL)-start>TIMEOUT_SOCK_READLINE) { lprintf(LOG_WARNING,"!socket_readline: timeout (%d) exceeded" ,TIMEOUT_SOCK_READLINE); return(0); } continue; } if(recv(*lp, &ch, 1, 0)!=1) { csi->socket_error=ERROR_VALUE; return(0); } if(ch=='\n' && i>=1) break; buf[i++]=ch; } if(i>0 && buf[i-1]=='\r') buf[i-1]=0; else buf[i]=0; if(csi->etx) { p=strchr(buf,csi->etx); if(p) *p=0; } *pp=copystrvar(csi,*pp,buf); csi->logic=LOGIC_TRUE; return(0); case CS_SOCKET_WRITE: lp=getintvar(csi,*(int32_t *)csi->ip); /* socket */ csi->ip+=4; pp=getstrvar(csi,*(int32_t *)csi->ip); /* buffer */ csi->ip+=4; csi->logic=LOGIC_FALSE; csi->socket_error=0; if(!lp || !pp || !(*pp)) return(0); if(sendsocket(*lp,*pp,strlen(*pp))>0) csi->logic=LOGIC_TRUE; else csi->socket_error=ERROR_VALUE; return(0); /* FTP Functions */ case CS_FTP_LOGIN: lp=getintvar(csi,*(int32_t *)csi->ip); /* socket */ csi->ip+=4; pp1=getstrvar(csi,*(int32_t *)csi->ip); /* username */ csi->ip+=4; pp2=getstrvar(csi,*(int32_t *)csi->ip); /* password */ csi->ip+=4; csi->logic=LOGIC_FALSE; csi->socket_error=0; if(!lp || !pp1 || !pp2) return(0); if(!ftp_cmd(csi,*lp,NULL,rsp)) return(0); if(atoi(rsp)!=220) return(0); sprintf(str,"USER %s",*pp1); if(!ftp_cmd(csi,*lp,str,rsp)) return(0); if(atoi(rsp)==331) { /* Password needed */ sprintf(str,"PASS %s",*pp2); if(!ftp_cmd(csi,*lp,str,rsp)) return(0); } if(atoi(rsp)==230) /* Login successful */ csi->logic=LOGIC_TRUE; return(0); case CS_FTP_LOGOUT: lp=getintvar(csi,*(int32_t *)csi->ip); /* socket */ csi->ip+=4; csi->logic=LOGIC_FALSE; csi->socket_error=0; if(!lp) return(0); if(!ftp_cmd(csi,*lp,"QUIT",rsp)) return(0); if(atoi(rsp)==221) /* Logout successful */ csi->logic=LOGIC_TRUE; return(0); case CS_FTP_PWD: lp=getintvar(csi,*(int32_t *)csi->ip); /* socket */ csi->ip+=4; csi->logic=LOGIC_FALSE; csi->socket_error=0; if(!lp) return(0); if(!ftp_cmd(csi,*lp,"PWD",rsp)) return(0); if(atoi(rsp)==257) /* pathname */ csi->logic=LOGIC_TRUE; return(0); case CS_FTP_CWD: lp=getintvar(csi,*(int32_t *)csi->ip); /* socket */ csi->ip+=4; pp=getstrvar(csi,*(int32_t *)csi->ip); /* path */ csi->ip+=4; csi->logic=LOGIC_FALSE; csi->socket_error=0; if(!lp || !pp) return(0); sprintf(str,"CWD %s",*pp); if(!ftp_cmd(csi,*lp,str,rsp)) return(0); if(atoi(rsp)==250) csi->logic=LOGIC_TRUE; return(0); case CS_FTP_DIR: lp=getintvar(csi,*(int32_t *)csi->ip); /* socket */ csi->ip+=4; pp=getstrvar(csi,*(int32_t *)csi->ip); /* path */ csi->ip+=4; csi->logic=LOGIC_FALSE; csi->socket_error=0; if(!lp || !pp) return(0); if(ftp_get(csi,*lp,*pp,NULL /* unused */, true /* DIR */)==true) csi->logic=LOGIC_TRUE; return(0); case CS_FTP_DELETE: lp=getintvar(csi,*(int32_t *)csi->ip); /* socket */ csi->ip+=4; pp=getstrvar(csi,*(int32_t *)csi->ip); /* path */ csi->ip+=4; csi->logic=LOGIC_FALSE; csi->socket_error=0; if(!lp || !pp) return(0); sprintf(str,"DELE %s",*pp); if(!ftp_cmd(csi,*lp,str,rsp)) return(0); if(atoi(rsp)==250) csi->logic=LOGIC_TRUE; return(0); case CS_FTP_GET: lp=getintvar(csi,*(int32_t *)csi->ip); /* socket */ csi->ip+=4; pp1=getstrvar(csi,*(int32_t *)csi->ip); /* src path */ csi->ip+=4; pp2=getstrvar(csi,*(int32_t *)csi->ip); /* dest path */ csi->ip+=4; csi->logic=LOGIC_FALSE; csi->socket_error=0; if(!lp || !pp1 || !pp2) return(0); if(ftp_get(csi,*lp,*pp1,*pp2)==true) csi->logic=LOGIC_TRUE; return(0); case CS_FTP_PUT: lp=getintvar(csi,*(int32_t *)csi->ip); /* socket */ csi->ip+=4; pp1=getstrvar(csi,*(int32_t *)csi->ip); /* src path */ csi->ip+=4; pp2=getstrvar(csi,*(int32_t *)csi->ip); /* dest path */ csi->ip+=4; csi->logic=LOGIC_FALSE; csi->socket_error=0; if(!lp || !pp1 || !pp2) return(0); if(ftp_put(csi,*lp,*pp1,*pp2)==true) csi->logic=LOGIC_TRUE; return(0); default: errormsg(WHERE,ERR_CHK,"net sub-instruction",*(csi->ip-1)); return(0); } }
void ftp_test(cyg_addrword_t p) { int ret; CYG_TEST_INIT(); init_all_network_interfaces(); CYG_TEST_INFO("Getting /etc/passwd from " _FTP_SRV); ret = ftp_get(_FTP_SRV,"anonymous","ftpclient1", "/etc/passwd",ftpbuf,FTPBUFSIZE, ftpclient_printf); if (ret > 0) { diag_printf("PASS:< %d bytes received>\n",ret); } else { diag_printf("FAIL:< ftp_get returned %d>\n",ret); } CYG_TEST_INFO("Putting passwd file back in /incoming/passwd\n"); ret = ftp_put(_FTP_SRV,"anonymous","ftpclient1", "/incoming/passwd",ftpbuf,ret, ftpclient_printf); if (ret > 0) { diag_printf("PASS:\n"); } else { diag_printf("FAIL:< ftp_get returned %d>\n",ret); } CYG_TEST_INFO("Reading back /incoming/passwd\n"); ret = ftp_get(_FTP_SRV,"anonymous","ftpclient1", "/incoming/passwd",ftpbuf1,FTPBUFSIZE, ftpclient_printf); if (ret > 0) { diag_printf("PASS:< %d bytes received>\n",ret); } else { diag_printf("FAIL:< ftp_get returned %d>\n",ret); } CYG_TEST_PASS_FAIL(!memcmp(ftpbuf,ftpbuf1,ret),"Transfer integrity"); #ifdef CYGPKG_NET_INET6 CYG_TEST_INFO("Getting /etc/passwd from " _FTP_SRV_V6); ret = ftp_get(_FTP_SRV_V6,"anonymous","ftpclient1", "/etc/passwd",ftpbuf,FTPBUFSIZE, ftpclient_printf); if (ret > 0) { diag_printf("PASS:< %d bytes received>\n",ret); } else { diag_printf("FAIL:< ftp_get returned %d>\n",ret); } CYG_TEST_INFO("Putting passwd file back in /incoming/passwd\n"); ret = ftp_put(_FTP_SRV_V6,"anonymous","ftpclient1", "/incoming/passwd",ftpbuf,ret, ftpclient_printf); if (ret > 0) { diag_printf("PASS:\n"); } else { diag_printf("FAIL:< ftp_get returned %d>\n",ret); } CYG_TEST_INFO("Reading back /incoming/passwd\n"); ret = ftp_get(_FTP_SRV_V6,"anonymous","ftpclient1", "/incoming/passwd",ftpbuf1,FTPBUFSIZE, ftpclient_printf); if (ret > 0) { diag_printf("PASS:< %d bytes received>\n",ret); } else { diag_printf("FAIL:< ftp_get returned %d>\n",ret); } CYG_TEST_PASS_FAIL(!memcmp(ftpbuf,ftpbuf1,ret),"Transfer integrity"); #endif CYG_TEST_INFO("ftp_Get'ing with a bad username\n"); ret = ftp_get(_FTP_SRV,"nosuchuser","ftpclient1", "/incoming/passwd",ftpbuf1,FTPBUFSIZE, ftpclient_printf); CYG_TEST_PASS_FAIL(ret==FTP_BADUSER,"Bad Username"); CYG_TEST_INFO("ftp_get'ting with a bad passwd\n"); ret = ftp_get(_FTP_SRV,"nobody","ftpclient1", "/incoming/passwd",ftpbuf1,FTPBUFSIZE, ftpclient_printf); CYG_TEST_PASS_FAIL(ret==FTP_BADUSER,"Bad passwd"); CYG_TEST_INFO("ftp_get'ing from a with a bad passwd\n"); ret = ftp_get(_FTP_SRV,"nobody","ftpclient1", "/incoming/passwd",ftpbuf1,FTPBUFSIZE, ftpclient_printf); CYG_TEST_PASS_FAIL(ret==FTP_BADUSER,"Bad passwd"); CYG_TEST_INFO("ftp_get'ing from a bad server\n"); ret = ftp_get("127.0.0.1","nobody","ftpclient1", "/incoming/passwd",ftpbuf1,FTPBUFSIZE, ftpclient_printf); CYG_TEST_PASS_FAIL(ret==FTP_NOSUCHHOST,"Bad server"); CYG_TEST_INFO("ftp_get'ing a file which is too big"); ret = ftp_get(_FTP_SRV,"anonymous","ftpclient1", "/incoming/passwd",ftpbuf,2, ftpclient_printf); CYG_TEST_PASS_FAIL(ret==FTP_TOOBIG,"File too big"); }