cURL / Mailing Lists / curl-library / Single Mail

curl-library

SFTP problems

From: Daniel Karunairatnam <daniel.karunairatnam_at_gmail.com>
Date: Sun, 19 Oct 2014 19:43:27 +1100

Hi guys,

I'm trying to use libcurl in Visual Studio Express C++ 2010 (Windows 8) to
upload and append files to an sftp server.
I am using the following code (taken from
http://curl.haxx.se/libcurl/c/sftpget.html, with a few changes).

/**************************************************************************
 * _ _ ____ _
 * Project ___| | | | _ \| |
 * / __| | | | |_) | |
 * | (__| |_| | _ <| |___
 * \___|\___/|_| \_\_____|
 *
 * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel_at_haxx.se>, et al.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution. The terms
 * are also available at http://curl.haxx.se/docs/copyright.html.
 *
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
 * copies of the Software, and permit persons to whom the Software is
 * furnished to do so, under the terms of the COPYING file.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ***************************************************************************/

#include <stdio.h>
#include "stdafx.h"

#include <curl/curl.h>

/* define this to switch off the use of ssh-agent in this program */
#undef DISABLE_SSH_AGENT

/*
 * This is an example showing how to get a single file from an SFTP 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(void)
{
  CURL *curl;
  CURLcode res;
  struct FtpFile ftpfile={
    "yourfile.bin", /* name to store the file as if succesful */
    NULL
  };

  curl_global_init(CURL_GLOBAL_DEFAULT);

  curl = curl_easy_init();
  if(curl) {
    /*
     * You better replace the URL with one that works!
     */
    curl_easy_setopt(curl, CURLOPT_URL,
                     "10.1.1.4");
curl_easy_setopt(curl, CURLOPT_PORT, 22);
curl_easy_setopt(curl, CURLOPT_DIRLISTONLY, 1);
    /* 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);

#ifndef DISABLE_SSH_AGENT
    /* We activate ssh agent. For this to work you need
       to have ssh-agent running (type set | grep SSH_AGENT to check) or
       pageant on Windows (there is an icon in systray if so) */
    curl_easy_setopt(curl, CURLOPT_SSH_AUTH_TYPES, CURLSSH_AUTH_AGENT);
#endif

    /* Switch on full protocol/debug output */
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

    res = curl_easy_perform(curl);

    /* 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;
}

I have similar experiences using both the private server I need to connect
to, and a server I set up using freeSSHd.
If I use sftp:// before the ip address/hostname, i get a stack error. it
says the stack around variable hash is corrupt.

If I remove the sftp:// then the freesshd server says connection is made,
and says Accept: */* then SSH-2.0-WeOnlyDo 2.1.3.
followed by connection #0 to host 10.1.1.4 left intact.but nothing is
displayed after that, and the directory list should be.

When I use the private server I need to connect to (I can't mention for
privacy reasons) After saying it connected using Get/HTTP 1.1/ and the
Accept: */* it says SSH-2.0-OpenSSH_5.9p1 Debian-5ubuntu1.4
then recv failure: connection was reset
Closing connection 0
curl told us 56.

I have no idea why it isn't working. Is the sftp:// required in both cases?
If so, does anyone know why I keep getting a stack error?

On a slightly relevant note. I tried using libssh2 to achieve this task
however I kept getting a stack error also.

Any help would be greatly appreciated.

Thanks,
Daniel

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2014-10-19