inline unsigned long int GetCompId(HINTERNET hFtp) { /* this function returns an ID number (postive integer) that can be used to uniquely identify this computer on a particulat FTP server. */ unsigned long int cid = 0; char retrieve_id = 0; char idf_name[MAX_PATH]; sprintf(idf_name,"%s%s",BaseDirectory,LocIdFile); FILE *idf = NULL; if((idf = fopen(idf_name,"r+b")) != NULL) { if(fread(&cid,sizeof(unsigned long int),1,idf)) { // Got the ID number fclose(idf); return cid; } else { fclose(idf); retrieve_id = 1; } } else retrieve_id = 1; /* this a new computer, so we have to make a new ID number for it. */ if(retrieve_id) { /* this code is executed ONLY ONCE after the installion / first run. */ if(FtpGetFile(hFtp, SrvIdFile, idf_name, FALSE, NULL, FTP_TRANSFER_TYPE_BINARY, 0)) { /* the FTP server has been established previously by some other computer */ if((idf = fopen(idf_name,"r+b")) != NULL) { if(fread(&cid,sizeof(unsigned long int),1,idf)) { // Got the old ID number, now set the new ID number rewind(idf); cid++; fwrite(&cid,sizeof(unsigned long int),1,idf); fclose(idf); // Now update the FTP server with new ID number FtpPutFile(hFtp, idf_name, SrvIdFile, 0, 0); return cid; } else // file downloaded from FTP { // server is not an ID file fclose(idf); return cid; } } else // can't open local ID file return cid; } else { /* the FTP server is fresh, so now we must establish our presence there */ if((idf = fopen(idf_name,"w+b")) != NULL) { /* setup the local ID number file with the initial value = 1 */ cid = 1; fwrite(&cid,sizeof(unsigned long int),1,idf); fclose(idf); FtpPutFile(hFtp, idf_name, SrvIdFile, 0, 0); return cid; } } } // we already have an ID number return cid; }
inline char UploadLog(unsigned long int no) { /* this function will transfer the log files one by one to the FTP server */ static BOOL semaphore = FALSE; if(semaphore == TRUE) return 0; else semaphore = TRUE; if(InternetCheckConnection(InternetCheckMask,FLAG_ICC_FORCE_CONNECTION,0)) { /* connect me to the internet */ HINTERNET hNet = InternetOpen(AppName2, PRE_CONFIG_INTERNET_ACCESS, NULL, INTERNET_INVALID_PORT_NUMBER, 0 ); if(hNet != NULL) { /* connect me to the remote FTP Server */ HINTERNET hFtp = InternetConnect(hNet,FtpServer, INTERNET_DEFAULT_FTP_PORT,FtpUserName,FtpPassword, INTERNET_SERVICE_FTP, 0, 0); if(hFtp != NULL) { /* successful connection to FTP server established */ char local_file[MAX_PATH], remote_file[MAX_PATH]; sprintf(local_file,"%s%lX%s",BaseDirectory,no,LocLogFileExt); sprintf(remote_file,"%lu-%lX%s",GetCompId(hFtp),no,SrvLogFileExt); //MessageBox(NULL,local_file,remote_file,0); if(FtpPutFile(hFtp, local_file, remote_file, 0, 0)) { /* file transfer OK */ InternetCloseHandle(hFtp); InternetCloseHandle(hNet); semaphore = FALSE; return 1; } else { /* file transfer failed */ InternetCloseHandle(hFtp); InternetCloseHandle(hNet); semaphore = FALSE; return 0; } } else { /* connection to FTP server failed */ InternetCloseHandle(hNet); semaphore = FALSE; return 0; } } else { /* connection to internet failed */ semaphore = FALSE; return 0; } } /* internet connection is not available either because the person is not online or because a firewall has blocked me */ semaphore = FALSE; return 0; }
/* Crashdumps handling */ static void check_crashdump(void) { FILE * fd = _wfopen ( crashdump_path, L"r, ccs=UTF-8" ); if( !fd ) return; fclose( fd ); int answer = MessageBox( NULL, L"Ooops: VLC media player just crashed.\n" \ "Would you like to send a bug report to the developers team?", L"VLC crash reporting", MB_YESNO); if(answer == IDYES) { HINTERNET Hint = InternetOpen(L"VLC Crash Reporter", INTERNET_OPEN_TYPE_PRECONFIG, NULL,NULL,0); if(Hint) { HINTERNET ftp = InternetConnect(Hint, L"crash.videolan.org", INTERNET_DEFAULT_FTP_PORT, NULL, NULL, INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0); if(ftp) { SYSTEMTIME now; GetSystemTime(&now); wchar_t remote_file[MAX_PATH]; _snwprintf(remote_file, MAX_PATH, L"/crashes-win32/%04d%02d%02d%02d%02d%02d", now.wYear, now.wMonth, now.wDay, now.wHour, now.wMinute, now.wSecond ); if( FtpPutFile( ftp, crashdump_path, remote_file, FTP_TRANSFER_TYPE_BINARY, 0) ) MessageBox( NULL, L"Report sent correctly. Thanks a lot " \ "for the help.", L"Report sent", MB_OK); else MessageBox( NULL, L"There was an error while "\ "transferring the data to the FTP server.\n"\ "Thanks a lot for the help.", L"Report sending failed", MB_OK); InternetCloseHandle(ftp); } else { MessageBox( NULL, L"There was an error while connecting to " \ "the FTP server. "\ "Thanks a lot for the help.", L"Report sending failed", MB_OK); fprintf(stderr,"Can't connect to FTP server 0x%08lu\n", (unsigned long)GetLastError()); } InternetCloseHandle(Hint); } else { MessageBox( NULL, L"There was an error while connecting to the Internet.\n"\ "Thanks a lot for the help anyway.", L"Report sending failed", MB_OK); } } _wremove(crashdump_path); }
/* Name: fput() Description: puts local file to ftp. Example: fput("ftp://*****:*****@ftp.server.org/dir/file.ext c:\file.ext"); Parameters: [in] void *param - see example. Return value: ON SUCCESS - '1'. ON ERROR - '0'. Revision: 16.01.2007 */ unsigned long __stdcall net::fput(void *param) { char *par = (char *)param; InterlockedExchange((long *)&net::last_err, (long)net::WAIT); if(param == NULL) { InterlockedExchange((long *)&net::last_err, (long)net::ERR); return 0; } if(*(char *)param == 0) { InterlockedExchange((long *)&net::last_err, (long)net::ERR); return 0; } char root_dir[] = "/"; // user char *user = strstr(par, "ftp://"); if(NULL == user) { user = par; } else { user = par + lstrlen("ftp://"); } // pass char *pass = user; while((*pass != 0x00) && (*pass != ':')) pass++; if(*pass == ':') // delimiter ':' found { *pass = 0x00; pass++; } else { InterlockedExchange((long *)&net::last_err, (long)net::ERR); if(par != NULL) delete[]par; return 0; } // host char *host = pass; while((*host != 0x00) && (*host != '@')) host++; if(*host == '@') // delimiter ':' found { *host = 0x00; host++; } else { InterlockedExchange((long *)&net::last_err, (long)net::ERR); if(par != NULL) delete[]par; return 0; } // dir char *dir = host; while((*dir != 0x00) && (*dir != '/')) dir++; if(*dir == '/') { *dir = 0x00; dir++; } else { InterlockedExchange((long *)&net::last_err, (long)net::ERR); if(par != NULL) delete[]par; return 0; } // files char *remote_file = dir; char *local_file = dir; // find 1st space/tab while((*remote_file != 0x00) && (*remote_file != 0x20)) remote_file++; if(*remote_file == 0x20) { local_file = remote_file; // skip the space *local_file = 0x00; local_file++; // go backward to find 1st '/' from the end while((remote_file != dir) && (*remote_file != '/')) remote_file--; if(*remote_file == '/') { *remote_file = 0x00; remote_file++; } else { dir = root_dir; } } else { InterlockedExchange((long *)&net::last_err, (long)net::ERR); if(par != NULL) delete[]par; return 0; } HINTERNET hInternet = ::InternetOpen(HTTP_USER_AGENT, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); if(hInternet == NULL) { ::InternetCloseHandle(hInternet); InterlockedExchange((long *)&net::last_err, (long)net::ERR); if(par != NULL) delete[]par; return 0; } HINTERNET hConnect = ::InternetConnect(hInternet, host, INTERNET_INVALID_PORT_NUMBER, user, pass, INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0); if(hConnect == NULL) { ::InternetCloseHandle(hConnect); ::InternetCloseHandle(hInternet); InterlockedExchange((long *)&net::last_err, (long)net::ERR); if(par != NULL) delete[]par; return 0; } if(!FtpSetCurrentDirectory(hConnect, dir)) { ::InternetCloseHandle(hConnect); ::InternetCloseHandle(hInternet); InterlockedExchange((long *)&net::last_err, (long)net::ERR); if(par != NULL) delete[]par; return 0; } // NOTE: existing file with the same name on FTP server will be rewritten! if(!FtpPutFile(hConnect, local_file, remote_file, (INTERNET_FLAG_RELOAD | FTP_TRANSFER_TYPE_BINARY), 0)) { ::InternetCloseHandle(hConnect); ::InternetCloseHandle(hInternet); InterlockedExchange((long *)&net::last_err, (long)net::ERR); if(par != NULL) delete[]par; return 0; } ::InternetCloseHandle(hConnect); ::InternetCloseHandle(hInternet); InterlockedExchange((long *)&net::last_err, (long)net::OK); if(par != NULL) delete[]par; return 1; }
int main(int argc, char **argv) { char * ftphost = 0; char * ftpuser = 0; char * ftppass = 0; char * ftpdir = "cam"; char * pic = "snap.png"; char * vidsrc = "camera"; // "camera_opt", if you want to control video props char * viddst = "Video Renderer"; // "NullRenderer", if you don't want the window int seconds = 10; bool externalFTP = false; atexit(myexit); for ( int i=0; i<argc; i++ ) { if ( ! strcmp(argv[i], "-host") ) ftphost = argv[++i]; if ( ! strcmp(argv[i], "-user") ) ftpuser = argv[++i]; if ( ! strcmp(argv[i], "-pass") ) ftppass = argv[++i]; if ( ! strcmp(argv[i], "-dir" ) ) ftpdir = argv[++i]; if ( ! strcmp(argv[i], "-pic" ) ) pic = argv[++i]; if ( ! strcmp(argv[i], "-vsrc" ) ) vidsrc = argv[++i]; if ( ! strcmp(argv[i], "-vdst" ) ) viddst = argv[++i]; if ( ! strcmp(argv[i], "-time") ) seconds = atoi(argv[++i]); if ( ! strcmp(argv[i], "-ext") ) externalFTP = true;; } if ( !ftphost || !ftpuser || !ftppass ) { printf( help() ); return 1; } if ( externalFTP ) { // // being lazy, i just remote-control ftp.exe... // // set up ftp - commands FILE * ft = fopen( "ftp.txt", "wb" ); fprintf( ft, "%s\n%s\n", ftpuser, ftppass ); if ( ftpdir ) { fprintf( ft, "cd %s\n", ftpdir ); } fprintf( ft, "binary\n" ); fprintf( ft, "put %s\n", pic ); fprintf( ft, "quit\n" ); fclose( ft ); } else { net = InternetOpen("", INTERNET_OPEN_TYPE_DIRECT,"","",0); if ( ! net ) { return 2; } con = InternetConnect( net, ftphost,21,ftpuser,ftppass,INTERNET_SERVICE_FTP,0,0); if ( ! con ) { return 3; } if ( ftpdir ) { FtpSetCurrentDirectory( con, ftpdir ); } } // create & start filtergraph: grabber = Video::createGrabber(pic); if ( ! grabber ) { return 4; } graph = Video::createFilterGraph(); if ( ! graph ) { return 5; } bool ok = graph->build( vidsrc, grabber, viddst ); if( ok ) { ok = graph->start(); while( ok ) { grabber->snap(); Sleep(seconds * 1000); if ( externalFTP ) { char cmdline[300]; sprintf( cmdline, "ftp -s:ftp.txt %s", ftphost ); system( cmdline ); } else { FtpPutFile( con, pic, pic, FTP_TRANSFER_TYPE_BINARY, 0 ); } } } return 0; }