cURL / Mailing Lists / curl-library / Single Mail

curl-library

425 Can't open data connection

From: Buchibabu Chennupati <cbuchibabu_at_gmail.com>
Date: Tue, 11 Nov 2008 20:27:58 +0530

Hi

I am new to curl and just playing around with sample examples given in the
site.
Using the following code I am trying to get a file magnum.xml at location
/tmp/magnumfw using ftp.
I am not sure what the problem is but I couldn't get the file. I am getting
< 425 Can't open data connection.

Please clarify me where I have missed.

===========================
<code start>

#include <stdio.h>

#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>

/*
 * This is an example showing how to get a single file from an FTP server.
 * It delays the actual destination file creation until the first write
 * callback so that it won't create an empty file in case the remote file
 * doesn't exist or something else fails.
 */

struct FtpFile {
  const char *filename;
  FILE *stream;
};

static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void
*stream)
{
  struct FtpFile *out=(struct FtpFile *)stream;
  if(out && !out->stream) {
    /* open file for writing */
    out->stream=fopen(out->filename, "wb");
    if(!out->stream)
      return -1; /* failure, can't open file to write */
  }
  return fwrite(buffer, size, nmemb, out->stream);
}

int main(int argc,char *argv[])
{
  CURL *curl;
  CURLcode res;
  struct FtpFile ftpfile={
    "magnum.xml", /* name to store the file as if succesful */
    NULL
  };

  struct curl_slist *headerlist=NULL;
  static const char cmd[] = "CWD /tmp/magnumfw";
  static const char cmd1[] = "PWD";
  static const char cmd2[] = "PASV";
  static const char cmd3[] = "TYPE A";
  static const char cmd4[] = "RETR magnum.xml";
  curl_global_init(CURL_GLOBAL_DEFAULT);

  curl = curl_easy_init();
  if(curl) {
        headerlist = curl_slist_append(headerlist, cmd);
        headerlist = curl_slist_append(headerlist, cmd1);
        headerlist = curl_slist_append(headerlist, cmd2);
        headerlist = curl_slist_append(headerlist, cmd3);
        headerlist = curl_slist_append(headerlist, cmd4);
    /*
     * Get curl 7.9.2 from sunet.se's FTP site. curl 7.9.2 is most likely
not
     * present there by the time you read this, so you'd better replace the
     * URL with one that works!
     */
    curl_easy_setopt(curl, CURLOPT_URL,argv[1]);
    curl_easy_setopt(curl, CURLOPT_USERPWD,argv[2]);
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
        printf("\nBuchi:1\n");
    res = curl_easy_perform(curl);

    curl_easy_setopt(curl, CURLOPT_QUOTE, headerlist);

        printf("\nBuchi:2\n");
    res = curl_easy_perform(curl);

    /* Define our callback to get called when there's data to be written */
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);

    /* Set a pointer to our struct to pass to the callback */
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);

    /* Switch on full protocol/debug output */

        printf("\nBuchi:3\n");
    res = curl_easy_perform(curl);

        curl_slist_free_all (headerlist);

    /* always cleanup */
    curl_easy_cleanup(curl);

    if(CURLE_OK != res) {
      /* we failed */
      fprintf(stderr, "curl told us %d\n", res);
    }
  }

  if(ftpfile.stream)
    fclose(ftpfile.stream); /* close the local file */

  curl_global_cleanup();

  return 0;
}

<code end>
==============================
<output start>
-bash # ./a.out "ftp://129.159.15.16" 'root:'

Buchi:1
* About to connect() to 129.159.15.16 port 21
* Trying 129.159.14.16... * connected
* Connected to 129.159.15.16 (129.159.15.16) port 21
< 220 shmm1500 FTP server (Version wu-2.6.2(1) Thu Apr 27 19:11:06 GMT 2006)
ready.
> USER root
< 331 Password required for root.
> PASS
< 230 User root logged in.
* We have successfully logged in
> PWD
< 257 "/home/root" is current directory.
* Entry path is '/home/root'
> EPSV
< 500 'EPSV': command not understood.
> PASV
< 227 Entering Passive Mode (129,159,15,16,84,71)
* Trying 129.159.15.16... * connected
* Connecting to 129.159.15.16 (129.159.15.16) port 21575
* Connected the data stream with PASV!
> TYPE A
< 200 Type set to A.
> LIST
< 150 Opening ASCII mode data connection for /bin/ls.
* Getting file with size: -1
-rw------- 1 root root 7 Sep 22 2008 .rhosts
drwx------ 2 root root 0 Sep 22 2008 .ssh
-rwxrwxrwx 1 root root 1397 Jul 20 21:26 sandiasetup.sh
< 226 Transfer complete.
* Connection #0 to host 129.159.145.166 left intact

Buchi:2
* Re-using existing connection! (#0) with host 129.159.15.16
* Connected to 129.159.15.16 (129.159.15.16) port 21
> CWD /tmp/magnumfw
< 250 CWD command successful.
> PWD
< 257 "/tmp/magnumfw" is current directory.
> PASV
< 227 Entering Passive Mode (129,159,15,16,116,2)
> TYPE A
< 200 Type set to A.
> RETR magnum.xml
< 425 Can't open data connection.
* Connection #0 to host 129.159.15.16 left intact

Buchi:3
* Re-using existing connection! (#0) with host 129.159.15.16
* Connected to 129.159.15.16 (`"u>�) port 21
> CWD /tmp/magnumfw
< 250 CWD command successful.
> PWD
< 257 "/tmp/magnumfw" is current directory.
> PASV
< 227 Entering Passive Mode (129,159,15,16,211,50)
> TYPE A
< 200 Type set to A.
> RETR magnum.xml
< 425 Can't open data connection.
* Connection #0 to host 129.159.15.16 left intact
* Closing connection #0
curl told us 21

<output end>

-- 
Regards,
Buchibabu.
Received on 2008-11-11