cURL / Mailing Lists / curl-library / Single Mail

curl-library

uploaded file is of 0 bytes using FTP-SSL with libcurl

From: azad amm <ammazad_at_rediffmail.com>
Date: 18 Jan 2006 15:34:23 -0000

   Hi I tried uploading a file using libcurl to a FTP-SSL server running on windows. My libcurl client program runs on Solaris 2.9 The client program was basically the same example program ftpupload.c but with some additions done to include userid:passwd, SSL options and a custom CURLOPT_READFUNCTION The problem that i face is that the client program runs completely. However on the server's user home directory, the file uploaded has the name while-uploading.txt with 0 bytes in it. The curl version i am running on the client solaris machine is bash-2.05# curl --version curl 7.14.1 (sparc-sun-solaris2.9) libcurl/7.14.1 OpenSSL/0.9.8 zlib/1.1.3 Protocols: ftp gopher telnet dict ldap http file https ftps Features: IPv6 Largefile NTLM SSL libz bash-2.05# The sample curl client program is bash-2.05# cat ftpupload.c /***************************************************************************** * _ _ ____ _ * Project ___| | | | _ \| | * / __| | | | |_) | | * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * * $Id: ftpupload.c,v 1.7 2005/01/20 14:24:56 bagder Exp $ */ #include <stdio.h> #include <curl/curl.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #define TRUE 1 /* * This example shows an FTP upload, with a rename of the file just after * a successful upload. * * Example based on source code provided by Erick Nuwendam. Thanks! */ #define LOCAL_FILE "/tmp/test" #define UPLOAD_FILE_AS "while-uploading.txt" #define REMOTE_URL "ftp://bridge/" UPLOAD_FILE_AS #define RENAME_FILE_TO "renamed-and-fine.txt" struct FtpFile { char *filename; FILE *stream; }; int my_fread(void *buffer, size_t size, size_t nmemb, void *stream) { struct FtpFile *out=(struct FtpFile *)stream; if(out && !out->stream) { /* open file for reading */ out->stream=fopen(out->filename, "rb"); if(!out->stream) return -1; /* failure, can't open file to read */ } return fread(buffer, size, nmemb, out->stream); } int main(int argc, char **argv) { CURL *curl; CURLcode res; FILE * hd_src ; int hd ; struct stat file_info; char passwd[] = "hemant:sample"; struct FtpFile ftpfile={ "a.txt", /* name to store the file as if succesful */ NULL }; struct curl_slist *headerlist=NULL; char buf_1 [] = "RNFR " UPLOAD_FILE_AS; char buf_2 [] = "RNTO " RENAME_FILE_TO; /* get the file size of the local file */ hd = open(LOCAL_FILE, O_RDONLY) ; fstat(hd, &file_info); close(hd) ; /* get a FILE * of the same file, could also be made with fdopen() from the previous descriptor, but hey this is just an example! */ hd_src = fopen(LOCAL_FILE, "rb"); /* In windows, this will init the winsock stuff */ curl_global_init(CURL_GLOBAL_ALL); /* get a curl handle */ curl = curl_easy_init(); if(curl) { /* build a list of commands to pass to libcurl */ headerlist = curl_slist_append(headerlist, buf_1); headerlist = curl_slist_append(headerlist, buf_2); /* enable uploading */ curl_easy_setopt(curl, CURLOPT_UPLOAD, TRUE) ; /* specify target */ curl_easy_setopt(curl,CURLOPT_URL, REMOTE_URL); curl_easy_setopt(curl, CURLOPT_USERPWD, passwd); curl_easy_setopt(curl, CURLOPT_FTP_SSL, CURLFTPSSL_ALL); /* pass in that last of FTP commands to run after the transfer */ curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist); /* Define our callback to get called when there's data to be read */ curl_easy_setopt(curl, CURLOPT_READFUNCTION, my_fread); /* now specify which file to upload */ curl_easy_setopt(curl, CURLOPT_READDATA, &ftpfile); /* NOTE: if you want this example to work on Windows with libcurl as a DLL, you MUST also provide a read callback with CURLOPT_READFUNCTION. Failing to do so will give you a crash since a DLL may not use the variable's memory when passed in to it from an app like this. */ /* Set the size of the file to upload (optional). If you give a *_LARGE option you MUST make sure that the type of the passed-in argument is a curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must make sure that to pass in a type 'long' argument. */ curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size); curl_easy_setopt(curl, CURLOPT_VERBOSE, TRUE); /* Now run off and do what you've been told! */ res = curl_easy_perform(curl); /* clean up the FTP commands list */ curl_slist_free_all (headerlist); /* always cleanup */ curl_easy_cleanup(curl); } fclose(hd_src); /* close the local file */ curl_global_cleanup(); return 0; } bash-2.05# The debug output of the program run is bash-2.05# prog * About to connect() to bridge port 21 * Trying 10.212.10.33... * connected * Connected to bridge (10.212.10.33) port 21 < 220 Titan FTP Server 5.05.323 Ready. > AUTH SSL < 234 Security data exchange complete. * successfully set certificate verify locations: * CAfile: /usr/local/share/curl/curl-ca-bundle.crt CApath: none * SSL connection using DHE-RSA-AES256-SHA * Server certificate: * subject: /CN=bridge/C=IN/L=pune/ST=mah/O=veritas/OU=nbu/emailAddress=hemant_mishra@symantec.com * start date: 2006-01-18 07:30:14 GMT * expire date: 2007-01-17 18:30:00 GMT * common name: bridge (matched) * issuer: /CN=bridge/C=IN/L=pune/ST=mah/O=veritas/OU=nbu/emailAddress=hemant_mishra@symantec.com * SSL certificate verify ok. > USER hemant < 331 User name okay, need password. > PASS sample < 230-Welcome hemant from 10.212.10.122. You are now logged in to the server. < 230 User logged in, proceed. > PBSZ 0 < 502 SSL support is not currently enabled, or is not available, for this server. > PROT P < 200 Encrypting Data Channel. > PWD < 257 "/" is current directory. * Entry path is '/' > EPSV * Connect data stream passively < 229 Entering Extended Passive Mode (|||1220|) * Trying 10.212.10.33... * connected * Connecting to 10.212.10.33 (10.212.10.33) port 1220 > TYPE I < 200 Type set to I. > STOR while-uploading.txt < 150 File status okay; about to open data connection. * Doing the SSL/TLS handshake on the datastream * successfully set certificate verify locations: * CAfile: /usr/local/share/curl/curl-ca-bundle.crt CApath: none * SSL re-using session ID * SSL connection using DHE-RSA-AES256-SHA * Server certificate: * subject: /CN=bridge/C=IN/L=pune/ST=mah/O=veritas/OU=nbu/emailAddress=hemant_mishra@symantec.com * start date: 2006-01-18 07:30:14 GMT * expire date: 2007-01-17 18:30:00 GMT * common name: bridge (matched) * issuer: /CN=bridge/C=IN/L=pune/ST=mah/O=veritas/OU=nbu/emailAddress=hemant_mishra@symantec.com * SSL certificate verify ok. * Uploaded unaligned file size (1 out of 21474908464 bytes) * Closing connection #0 bash-2.05# Is there anything wrong, that i am doing?? Thanks Azad
Received on 2006-01-18