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; }
bool ChooseAnImageFTP(int sx,int sy, char *ftp_dir, int slot, char **filename, bool *isdir, int *index_file) { /* Parameters: sx, sy - window size, ftp_dir - what FTP directory to use, slot - in what slot should an image go (common: #6 for 5.25' 140Kb floppy disks, and #7 for hard-disks). slot #5 - for 800Kb floppy disks, but we do not use them in Apple][? (They are as a rule with .2mg extension) index_file - from which file we should start cursor (should be static and 0 when changing dir) Out: filename - chosen file name (or dir name) isdir - if chosen name is a directory */ double facx = double(g_ScreenWidth) / double(SCREEN_WIDTH); double facy = double(g_ScreenHeight) / double(SCREEN_HEIGHT); SDL_Surface *my_screen; // for background struct ftpparse FTP_PARSE; // for parsing ftp directories #ifndef _WIN32 struct stat info; #endif if(font_sfc == NULL) if(!fonts_initialization()) return false; //if we don't have a fonts, we just can do none char tmpstr[512]; char ftpdirpath [MAX_PATH]; snprintf(ftpdirpath, MAX_PATH, "%s/%s%s", g_sFTPLocalDir, g_sFTPDirListing, md5str(ftp_dir)); // get path for FTP dir listing // printf("Dir: %s, MD5(dir)=%s\n",ftp_dir,ftpdirpath); List<char> files; // our files List<char> sizes; // and their sizes (or 'dir' for directories) int act_file; // current file int first_file; // from which we output files char ch = 0; // prepare screen SDL_Surface *tempSurface; if(!g_WindowResized) { if(g_nAppMode == MODE_LOGO) tempSurface = g_hLogoBitmap; // use logobitmap else tempSurface = g_hDeviceBitmap; } else tempSurface = g_origscreen; my_screen = SDL_CreateRGBSurface(SDL_SWSURFACE, tempSurface->w, tempSurface->h, tempSurface->format->BitsPerPixel, 0, 0, 0, 0); if(tempSurface->format->palette && my_screen->format->palette) SDL_SetColors(my_screen, tempSurface->format->palette->colors, 0, tempSurface->format->palette->ncolors); surface_fader(my_screen, 0.2F, 0.2F, 0.2F, -1, 0); // fade it out to 20% of normal SDL_BlitSurface(tempSurface, NULL, my_screen, NULL); SDL_BlitSurface(my_screen, NULL, screen, NULL); // show background // ch = 0; #define NORMAL_LENGTH 60 if(strlen(ftp_dir) > NORMAL_LENGTH) { ch = ftp_dir[NORMAL_LENGTH]; ftp_dir[NORMAL_LENGTH] = 0;} //cut-off too long string font_print_centered(sx/2 ,5 * facy , ftp_dir, screen, 1.5 * facx, 1.3 * facy); if(ch) ftp_dir[NORMAL_LENGTH] = ch; //restore cut-off char font_print_centered(sx/2,20 * facy,"Connecting to FTP server... Please wait.", screen, 1 * facx, 1 * facy); SDL_Flip(screen); // show the screen bool OKI; #ifndef _WIN32 if(stat(ftpdirpath,&info) == 0 && info.st_mtime > time(NULL) - RENEW_TIME) { OKI = false; // use this file } else { OKI = ftp_get(ftp_dir,ftpdirpath); // get ftp dir listing } #else // in WIN32 let's use constant caching? -- need to be redone using file.mtime if(GetFileAttributes(ftpdirpath) != DWORD(-1)) OKI = false; else OKI = ftp_get(ftp_dir,ftpdirpath); // get ftp dir listing #endif if(OKI) { // error printf("Failed getting FTP directory %s to %s\n",ftp_dir,ftpdirpath); font_print_centered(sx/2,30 * facy, "Failure. Press any key!",screen, 1.4 * facx, 1.1 * facy); SDL_Flip(screen); // show the screen SDL_Delay(KEY_DELAY); // wait some time to be not too fast ////////////////////////////////// // Wait for keypress ////////////////////////////////// SDL_Event event; // event Uint8 *keyboard; // key state event.type = SDL_QUIT; while(event.type != SDL_KEYDOWN) { // wait for key pressed SDL_Delay(100); SDL_PollEvent(&event); } SDL_FreeSurface(my_screen); return false; } FILE *fdir = fopen(ftpdirpath,"r"); char *tmp; int i,j, B, N; // for cycles, beginning and end of list // build prev dir if(strcmp(ftp_dir, "ftp://")) { tmp = new char[3]; strcpy(tmp, ".."); files.Add(tmp); tmp = new char[5]; strcpy(tmp, "<UP>"); sizes.Add(tmp); // add sign of directory B = 1; } else B = 0; // for sorting dirs while (tmp = fgets(tmpstr,512,fdir)) // first looking for directories { // clear and then try to fill in FTP_PARSE struct memset(&FTP_PARSE,0,sizeof(FTP_PARSE)); ftpparse(&FTP_PARSE, tmp, strlen(tmp)); int what = getstatFTP(&FTP_PARSE, NULL); if (strlen(FTP_PARSE.name) > 0 && what == 1) // is directory! { tmp = new char[strlen(FTP_PARSE.name)+1]; // add entity to list strcpy(tmp, FTP_PARSE.name); files.Add(tmp); tmp = new char[6]; strcpy(tmp, "<DIR>"); sizes.Add(tmp); // add sign of directory } /* if */ } // sort directories. Please, don't laugh at my bubble sorting - it the simplest thing I've ever seen --bb if(files.Length() > 2) { N = files.Length() - 1; // B = 1; - defined above for(i = N; i > B; i--) for(j = B; j < i; j++) if(strcasecmp(files[j], files[j + 1]) > 0) { files.Swap(j,j + 1); sizes.Swap(j,j + 1); } } B = files.Length(); // start for files (void) rewind (fdir); // to the start // now get all regular files while (tmp = fgets(tmpstr,512,fdir)) { int fsize; // clear and then try to fill in FTP_PARSE struct memset(&FTP_PARSE,0,sizeof(FTP_PARSE)); ftpparse(&FTP_PARSE, tmp, strlen(tmp)); if ((getstatFTP(&FTP_PARSE, &fsize) == 2)) // is normal file! { tmp = new char[strlen(FTP_PARSE.name)+1]; // add this entity to list strcpy(tmp, FTP_PARSE.name); files.Add(tmp); tmp = new char[10]; // 1400000KB snprintf(tmp, 9, "%dKB", fsize); sizes.Add(tmp); // add this size to list } /* if */ } (void) fclose (fdir); // do sorting for files if(files.Length() > 2 && B < files.Length()) { N = files.Length() - 1; // B = 1; for(i = N; i > B; i--) for(j = B; j < i; j++) if(strcasecmp(files[j], files[j + 1]) > 0) { files.Swap(j,j + 1); sizes.Swap(j,j + 1); } } // Count out cursor position and file number output act_file = *index_file; if(act_file >= files.Length()) act_file = 0; // cannot be more than files in list first_file = act_file - (FILES_IN_SCREEN / 2); if (first_file < 0) first_file = 0; // cannot be negativ... // Show all directories (first) and files then // char *tmp; char *siz; // int i; while(true) { SDL_BlitSurface(my_screen, NULL, screen, NULL); // show background font_print_centered(sx/2 ,5 * facy , ftp_dir, screen, 1.5 * facx, 1.3 * facy); if (slot == 6) font_print_centered(sx/2,20 * facy,"Choose image for floppy 140KB drive", screen, 1 * facx, 1 * facy); else if (slot == 7) font_print_centered(sx/2,20 * facy,"Choose image for Hard Disk", screen, 1 * facx, 1 * facy); else if (slot == 5) font_print_centered(sx/2,20 * facy,"Choose image for floppy 800KB drive", screen, 1 * facx, 1 * facy); else if (slot == 1) font_print_centered(sx/2,20 * facy,"Select file name for saving snapshot", screen, 1 * facx, 1 * facy); else if (slot == 0) font_print_centered(sx/2,20 * facy,"Select snapshot file name for loading", screen, 1 * facx, 1 * facy); font_print_centered(sx/2,30 * facy, "Press ENTER to choose, or ESC to cancel",screen, 1.4 * facx, 1.1 * facy); files.Rewind(); // from start sizes.Rewind(); i = 0; // printf("We've printed some messages, go to file list!\n"); // show all fetched dirs and files // topX of first fiel visible int TOPX = 45 * facy; while(files.Iterate(tmp)) { sizes.Iterate(siz); // also fetch size string if (i >= first_file && i < first_file + FILES_IN_SCREEN) { // FILES_IN_SCREEN items on screen // char tmp2[80],tmp3[256]; if (i == act_file) { // show item under cursor (in inverse mode) SDL_Rect r; r.x= 2; r.y= TOPX + (i-first_file) * 15 * facy - 1; if(strlen(tmp) > 46) r.w = 46 * 6 * 1.7 * facx + 2; else r.w= strlen(tmp) * 6 * 1.7 * facx + 2; // 6- FONT_SIZE_X r.h= 9 * 1.5 * facy; SDL_FillRect(screen, &r, SDL_MapRGB(screen->format,255,0,0));// in RED } /* if */ // print file name with enlarged font ch = 0; if(strlen(tmp) > 46) { ch = tmp[46]; tmp[46] = 0;} //cut-off too long string font_print(4, TOPX + (i - first_file) * 15 * facy, tmp, screen, 1.7 * facx, 1.5 * facy); // show name font_print(sx - 70*facx, TOPX + (i - first_file) * 15 * facy, siz, screen, 1.7 * facx, 1.5 * facy);// show info (dir or size) if(ch) tmp[46] = ch; //restore cut-off char } /* if */ i++; // next item } /* while */ ///////////////////////////////////////////////////////////////////////////////////////////// // draw rectangles rectangle(screen, 0, TOPX - 5, g_ScreenWidth, 320 * facy, SDL_MapRGB(screen->format, 255, 255, 255)); rectangle(screen, 480 * facx, TOPX - 5, 0, 320 * facy, SDL_MapRGB(screen->format, 255, 255, 255)); SDL_Flip(screen); // show the screen SDL_Delay(KEY_DELAY); // wait some time to be not too fast ////////////////////////////////// // Wait for keypress ////////////////////////////////// SDL_Event event; // event Uint8 *keyboard; // key state event.type = SDL_QUIT; while(event.type != SDL_KEYDOWN) { // wait for key pressed SDL_Delay(10); SDL_PollEvent(&event); } // control cursor keyboard = SDL_GetKeyState(NULL); // get current state of pressed (and not pressed) keys if (keyboard[SDLK_UP] || keyboard[SDLK_LEFT]) { if (act_file>0) act_file--; // up one position if (act_file<first_file) first_file=act_file; } /* if */ if (keyboard[SDLK_DOWN] || keyboard[SDLK_RIGHT]) { if (act_file < (files.Length() - 1)) act_file++; if (act_file >= (first_file + FILES_IN_SCREEN)) first_file=act_file - FILES_IN_SCREEN + 1; } /* if */ if (keyboard[SDLK_PAGEUP]) { act_file-=FILES_IN_SCREEN; if (act_file<0) act_file=0; if (act_file<first_file) first_file=act_file; } /* if */ if (keyboard[SDLK_PAGEDOWN]) { act_file+=FILES_IN_SCREEN; if (act_file>=files.Length()) act_file=(files.Length()-1); if (act_file>=(first_file+FILES_IN_SCREEN)) first_file=act_file-FILES_IN_SCREEN + 1; } /* if */ // choose an item? if (keyboard[SDLK_RETURN]) { // dup string from selected file name *filename = strdup(php_trim(files[act_file],strlen(files[act_file]))); // printf("files[act_file]=%s, *filename=%s\n\n", files[act_file], *filename); if(!strcmp(sizes[act_file], "<DIR>") || !strcmp(sizes[act_file], "<UP>")) *isdir = true; else *isdir = false; // this is directory (catalog in Apple][ terminology) *index_file = act_file; // remember current index files.Delete(); sizes.Delete(); SDL_FreeSurface(my_screen); return true; } /* if */ if (keyboard[SDLK_ESCAPE]) { files.Delete(); sizes.Delete(); SDL_FreeSurface(my_screen); return false; // ESC has been pressed } /* if */ if (keyboard[SDLK_HOME]) { // HOME? act_file=0; first_file=0; } /* if */ if (keyboard[SDLK_END]) { // END? act_file=files.Length() - 1; // go to the last possible file in list first_file=act_file - FILES_IN_SCREEN + 1; if(first_file < 0) first_file = 0; } /* if */ } } /* ChooseAnImageFTP */
/* 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 main(int argc, char *argv[]) { ifstream bcpInfile; // local import file holding records to bcp to x_import table int hndl; // file handle char szXfrFile[128]; // holds name of delimited import file char szTmpFile[128]; // holds name of renamed delimited import file char szXfrLocalFile[128]; // holds name of delimited import file with local dir prepended int intBcpCount; // number of rows bcp'd // Open import log and import error log files if ( !OpenLogFiles() ) return; if ( argc >= 7 ) szSystem = argv[7]; else szSystem = ""; // Check if user wants help or if incorrect args entered if ( !CheckArgs(argc, argv) ) return; // Create transfer filenames wsprintf(szXfrFile, "%s_cags.xfr", argv[7] ); wsprintf(szTmpFile, "%s_cags.yyy", argv[7] ); // Connect to database and init db structures if ( !DBConnect(argv[1], argv[2], argv[3]) ) return; // // Check if local wms_cags.xfr exists to BCP records into import table // wsprintf(szXfrLocalFile, "%s%s", "./transfer/", szXfrFile ); if ( FileSize(szXfrLocalFile) > -1 ) // file exists { // Open local wms_cags.xfr bcpInfile.open(szXfrLocalFile, ios::nocreate, filebuf::sh_none); if ( !bcpInfile.is_open() ) { // Failed open so get out and retry on next run errfile << ERROR_PREFIX << "failed to open bcp input file" << endl; return; } else { // Call bcp routines to move data from import file into x_import table // Note: If migrated to another RDBMS most changes will be to this function if ( (intBcpCount = BCPInRecords(bcpInfile, argv[7])) != -1 ) { // Delete local import file now that its records are in import table bcpInfile.close(); f_deletefile(szXfrLocalFile); logfile << INFO_PREFIX << "successfull BCP of " << intBcpCount << " records into import table" << endl; } else { // Failed bcp so don't delete and get out to retry on next run bcpInfile.close(); errfile << ERROR_PREFIX << "failed BCP of import file to import table" << endl; return; } } } else { if ( FileSize(szXfrLocalFile) == -1 ) logfile << WARNING_PREFIX << "no records to import from " << szXfrLocalFile << " this run" << endl; else errfile << ERROR_PREFIX << "error opening local import file " << szXfrLocalFile << endl; } // // Logon to ftp server to get remote wms_cags.xfr to be bcp'd next import run // hndl = ftp_login(argv[4], argv[5], argv[6], ""); if ( hndl == -1 ) { errfile << ERROR_PREFIX << "failed ftp_login" << endl; return; } // Set current remote transfer directory if ( ftp_cd(hndl, argv[8]) == -1 ) { errfile << ERROR_PREFIX << "failed ftp_cd to " << argv[8] << endl; return; } // Check for no left over records from prior import and new records to import if ( !ftp_exist(hndl, szTmpFile) && ftp_exist(hndl, szXfrFile) ) { // If so, then rename prior to ftp_get to prevent contention with remote site if ( ftp_rename(hndl, szXfrFile, szTmpFile) == -1 ) { // ftp_rename failed so just log message and try again next invocation errfile << ERROR_PREFIX << "failed ftp_remame of " << szXfrFile << " to " << szTmpFile << " will retry next run" << endl; return; } } // Check for either left over records or new records in tmp file to import if ( ftp_exist(hndl, szTmpFile) ) { // If so, then ftp them to local directory if ( ftp_get(hndl, szTmpFile, szXfrLocalFile) == -1 ) { // ftp_get failed so do nothing here and retry next invovation errfile << ERROR_PREFIX << "failed ftp_get of " << szTmpFile << " will retry next run" << endl; } else { if ( ftp_delete(hndl, szTmpFile) == -1 ) { // ftp_delete failed so delete local to prevent re-importing next invocation f_deletefile(szXfrLocalFile); errfile << ERROR_PREFIX << "failed ftp_delete of " << szTmpFile << endl; } else { // successfull FTP logfile << INFO_PREFIX << "successfull FTP from remote site to " << szXfrLocalFile << endl; } } } else logfile << WARNING_PREFIX << "no records to ftp from remote site this run" << endl; // Close opened objects if ( !logfile.is_open() ) logfile.close(); if ( !errfile.is_open() ) errfile.close(); dbexit(); // close database connection ftp_quit(hndl); // close ftp connection }
void HD_FTP_Select(int nDrive) { // Selects HDrive from FTP directoriy static int findex = 0; // file index will be remembered for current dir static int backdx = 0; //reserve static int dirdx = 0; // reserve for dirs char * filename = NULL; // given filename char fullpath[MAX_PATH]; // full path for it char tmppath [MAX_PATH]; bool isdir; // if given filename is a directory? findex = backdx; isdir = true; strcpy(fullpath, g_sFTPServerHDD); // global var for FTP path for HDD while(isdir) { printf("HD_FTP_Select: fullpath=%s\n", fullpath); if(!ChooseAnImageFTP(g_ScreenWidth, g_ScreenHeight, fullpath, 7, &filename, &isdir, &findex)) { DrawFrameWindow(); return; // if ESC was pressed, just leave } // Debug output printf("HD_FTP_Select: we got next:\n"); printf("isdir=%d, findex=%d, filename=%s\n", isdir, findex, filename); // -- if(isdir) { if(!strcmp(filename, "..")) // go to the upper directory { filename = strrchr(fullpath, FTP_SEPARATOR); // look for last '/' if(filename) { *filename = '\0'; // cut it off filename = strrchr(fullpath, FTP_SEPARATOR); // look for last '/' if(filename) *(++filename) = '\0'; // leave it on the place } if(strlen(fullpath) == 0) strcpy(fullpath,"/"); //we don't want fullpath to be empty findex = dirdx; // restore } else { if(strcmp(fullpath, "/")) snprintf(tmppath, MAX_PATH, "%s%s/", fullpath, filename); // next dir else snprintf(tmppath, MAX_PATH, "/%s/", filename); strcpy(fullpath, tmppath); // got ot anew printf("HD_FTP_Select: we build %s\n", tmppath); dirdx = findex; // store it findex = 0; // start with beginning of dir } }/* if isdir */ } /* while isdir */ // we chose some file strcpy(g_sFTPServerHDD, fullpath); RegSaveString(TEXT("Preferences"),REGVALUE_FTP_HDD_DIR, 1, g_sFTPServerHDD);// save it snprintf(tmppath, MAX_PATH, "%s/%s", fullpath, filename); strcpy(fullpath, tmppath); // fullpath - full path to file on FTP server snprintf(tmppath, MAX_PATH, "%s/%s", g_sFTPLocalDir, filename); // local path for file int error = ftp_get(fullpath, tmppath); if(!error) { if (HD_InsertDisk2(nDrive, tmppath)) { // save file names for HDD disk 1 or 2 if(nDrive) RegSaveString(TEXT("Preferences"),REGVALUE_HDD_IMAGE2,1,tmppath); else RegSaveString(TEXT("Preferences"),REGVALUE_HDD_IMAGE1,1,tmppath); } } backdx = findex; //store cursor position DrawFrameWindow(); }
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"); }
int main(int argc, char *argv[]) { CURL *curl; CURLcode res; struct curl_httppost *formpost=NULL; struct curl_httppost *lastptr=NULL; struct curl_slist *headerlist=NULL; static const char buf[] = "Expect:"; ftp_get(); curl_global_init(CURL_GLOBAL_ALL); /* Fill in the file upload field */ curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "sendfile", CURLFORM_FILE, "./00-13.jpg", CURLFORM_END); /* Fill in the filename field */ //curl_formadd(&formpost, // &lastptr, // CURLFORM_COPYNAME, "filename", // CURLFORM_COPYCONTENTS, "00-13.jpg", // CURLFORM_END); // char *memblock = "asdfkasfkjaskdjfkajsdfkjaklsdjffkl;ja"; // long memblock_length = strlen(memblock); //curl_formadd(&formpost, &lastptr, // CURLFORM_COPYNAME, "file", // CURLFORM_BUFFER, "unnamed.jpg", // CURLFORM_BUFFERPTR, memblock, // CURLFORM_BUFFERLENGTH, memblock_length, // CURLFORM_CONTENTTYPE, "image/jpg", // CURLFORM_END); /* Fill in the submit field too, even if this is rarely needed */ curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "submit", CURLFORM_COPYCONTENTS, "Submit", CURLFORM_END); curl = curl_easy_init(); /* initalize custom header list (stating that Expect: 100-continue is not wanted */ headerlist = curl_slist_append(headerlist, buf); if(curl) { /* what URL that receives this POST */ curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:8080/upload"); if ( (argc == 2) && (!strcmp(argv[1], "noexpectheader")) ) /* only disable 100-continue header if explicitly requested */ curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist); curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); /* Perform the request, res will get the return code */ res = curl_easy_perform(curl); /* Check for errors */ if(res != CURLE_OK) fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); /* always cleanup */ curl_easy_cleanup(curl); /* then cleanup the formpost chain */ curl_formfree(formpost); /* free slist */ curl_slist_free_all (headerlist); } return 0; }