EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Connection lost (error code is 0)

Also by EldoS: MsgConnect
Cross-platform protocol-independent communication framework for building peer-to-peer and client-server applications and middleware components.
#20731
Posted: 07/06/2012 14:13:52
by Tim Molloy (Standard support level)
Joined: 11/13/2009
Posts: 21

I'm trying to use SecureBlackBox to connect to a client's IBM FTP setup. It looks like the Open() is hanging until the client times out.

Code
private static void PerformUpload(SftpFileTransferArgs args)
        {
            _transferOperationType = SftpTransferOperationType.Upload;

            ProcessLogger.Log.InfoFormat("Attempting to upload file {0} to {1} over SFTP.", args.LocalFileName, args.FtpUrl);

            SBUtils.__Global.SetLicenseKey(SBUtils.__Global.BytesOfString(LH1ConfigurationManager.AppSettings["SecureBlackBox.LicenseKey"]));
            var sftpClient = new TElSimpleSFTPClient {Versions = SBSftpCommon.__Global.sbSFTP3};
            sftpClient.OnError += SftpClientOnError;
            sftpClient.OnKeyValidate += SftpClientOnKeyValidate;
            sftpClient.OnProgress += SftpClientOnProgress;
            sftpClient.OnAuthenticationFailed += SftpClientOnAuthenticationFailed;

            var sftpUrl = new Uri(args.FtpUrl);

            sftpClient.Address = sftpUrl.Host;
            sftpClient.Port = sftpUrl.Port;
            sftpClient.Username = args.FtpUsername;
            sftpClient.Password = args.FtpPassword;
            sftpClient.ASCIIMode = args.AsciiMode;

            sftpClient.AutoAdjustCiphers = true;
            sftpClient.set_KexAlgorithms(SBSSHConstants.__Global.SSH_KEX_FIRST, false);
            sftpClient.set_KexAlgorithms(SBSSHConstants.__Global.SSH_KEX_DH_GROUP_14, false);
            sftpClient.set_KexAlgorithms(SBSSHConstants.__Global.SSH_KEX_DH_GROUP_EXCHANGE256, false);
            sftpClient.set_KexAlgorithms(SBSSHConstants.__Global.SSH_KEX_LAST, false);
            sftpClient.set_KexAlgorithms(SBSSHConstants.__Global.SSH_KEX_DH_GROUP, true);
            sftpClient.set_KexAlgorithms(SBSSHConstants.__Global.SSH_KEX_DH_GROUP_EXCHANGE, true);

            try
            {
                ProcessLogger.Log.Info("Making the Connection...");
                sftpClient.Open();
                ProcessLogger.Log.Info("Finished making the Connection!");

                string remoteDir;
                try
                {
                    remoteDir = sftpClient.RequestAbsolutePath(args.RemoteDirectory);
                }
                catch
                {
                    remoteDir = ".";
                }

                var remoteFilePath = Path.Combine(remoteDir, args.RemoteFileName);
                var localFilePath = Path.Combine(args.LocalDirectory, args.LocalFileName);
                sftpClient.UploadFile(localFilePath, remoteFilePath, TSBSFTPFileTransferMode.ftmOverwrite);
                try
                {
                    // now adjust the attributes of the uploaded file
                    var attribs = new TElSftpFileAttributes {CTime = UtcTimeStamp()};
                    attribs.MTime = attribs.CTime;
                    attribs.ATime = attribs.CTime;
                    attribs.CATime = attribs.CTime;

                    attribs.IncludedAttributes = SBSftpCommon.__Global.saATime | SBSftpCommon.__Global.saMTime | SBSftpCommon.__Global.saCTime | SBSftpCommon.__Global.saCATime;
                    sftpClient.SetAttributes(remoteFilePath, attribs);
                }
                catch (Exception ex)
                {
                    ProcessLogger.Log.ErrorFormat("Error when trying to change the attributes of the uploaded file: {0}", ex.Message);
                }

                ProcessLogger.Log.InfoFormat("File {0} successfully uploaded to {1}.", args.LocalFileName, args.RemoteDirectory);
                _transferOperationState = SftpTransferOperationState.Success;
            }
            catch (Exception ex)
            {
                var errMessage = string.Format("Error encountered connecting to remote FTP server at {0}.", args.FtpUrl);
                ProcessLogger.Log.Error(errMessage, ex);

                ProcessLogger.Log.ErrorFormat("Error message {0}", ex.Message);
                _transferOperationState = SftpTransferOperationState.Failure;
            }

            if (sftpClient.Active)
            {
                sftpClient.Close(false);
            }
            sftpClient.Dispose();
        }

        #region Private Methods
        private static void SftpClientOnError(object sender, int errorCode)
        {
            var errMessage = string.Format("Error encountered during STFP upload.  Error Code: {0} (see SecureBlackbox documentation for error description", errorCode);
            ProcessLogger.Log.Error(errMessage);
            throw new Exception(errMessage);
        }

        private static void SftpClientOnAuthenticationFailed(object sender, int errorCode)
        {
            var errMessage = string.Format("Error encountered during STFP authorization attempt.  Error Code: {0} (see SecureBlackbox documentation for error description", errorCode);
            ProcessLogger.Log.Error(errMessage);
            throw new Exception(errMessage);
        }

        private static void SftpClientOnKeyValidate(object sender, SBSSHKeyStorage.TElSSHKey serverKey, ref bool validate)
        {
            ProcessLogger.Log.InfoFormat("Server key [{0}] received", SBUtils.__Global.DigestToStr128(serverKey.FingerprintMD5, true));
            validate = true;
        }

        private static void SftpClientOnProgress(object sender, long total, long current, ref bool cancel)
        {
            if (_transferOperationType != SftpTransferOperationType.Download) return;

            if (total >= current)
                _downloadComplete = true;
        }

        private static DateTime UtcTimeStamp()
        {
            var localZone = TimeZone.CurrentTimeZone;
            var currentDate = DateTime.Now;
            var dst = localZone.IsDaylightSavingTime(currentDate);

            return !dst ? localZone.ToUniversalTime(currentDate).AddHours(1) : localZone.ToUniversalTime(currentDate);
        }
        #endregion


I tried using logging from Filezilla to get information about the client server, but I don't get much information.

Code
2012-07-06 13:38:18 982124 3 Status: Connecting to **.**.***.**:21...
2012-07-06 13:38:21 982124 3 Status: Connection established, waiting for welcome message...
2012-07-06 13:38:21 982124 3 Response: 220-FTPD1 IBM FTP CS V1R12 at HMSYSNT5, 18:38:21 on 2012-07-06.
2012-07-06 13:38:21 982124 3 Response: 220 Connection will close if idle for more than 5 minutes.
2012-07-06 13:38:21 982124 3 Command: USER USER01
2012-07-06 13:38:22 982124 3 Response: 331 Send password please.
2012-07-06 13:38:22 982124 3 Command: PASS ********
2012-07-06 13:38:22 982124 3 Response: 230 USER01 is logged on.  Working directory is "FOLDER01.".
2012-07-06 13:38:22 982124 3 Command: SYST
2012-07-06 13:38:22 982124 3 Response: 215 MVS is the operating system of this server. FTP Server is running on z/OS.
2012-07-06 13:38:22 982124 3 Command: FEAT
2012-07-06 13:38:22 982124 3 Response: 211 no Extensions supported
2012-07-06 13:38:22 982124 3 Status: Connected
2012-07-06 13:38:22 982124 3 Status: Retrieving directory listing...
2012-07-06 13:38:22 982124 3 Command: PWD
2012-07-06 13:38:22 982124 3 Response: 257 "'FOLDER01.'" is working directory.
2012-07-06 13:38:22 982124 3 Command: TYPE I
2012-07-06 13:38:22 982124 3 Response: 200 Representation type is Image
2012-07-06 13:38:22 982124 3 Command: PASV
2012-07-06 13:38:22 982124 3 Response: 227 Entering Passive Mode (64,57,148,10,246,207)
2012-07-06 13:38:22 982124 3 Command: LIST
2012-07-06 13:38:22 982124 3 Response: 550 No data sets found.
2012-07-06 13:38:22 982124 3 Status: Directory listing successful


Based on my app's logging...the error is raised after 5 min.

Code
2012-07-06 13:59:52,113 [] INFO  SimpleSftpClient [(null)] - Making the Connection...
2012-07-06 14:04:52,229 [] ERROR SimpleSftpClient [(null)] - Error encountered connecting to remote FTP server at ftp://**.**.***.**.
2012-07-06 14:04:52,260 [] ERROR SimpleSftpClient [(null)] - Error message Connection lost (error code is 0)


Any ideas on what I can try?
#20732
Posted: 07/07/2012 02:14:31
by Eugene Mayevski (EldoS Corp.)

You are trying to use SFTP client for FTP connection. SFTP is not FTP (read the article).

Please use TElSimpleFTPSClient component for FTP/FTPS connections.


Sincerely yours
Eugene Mayevski
#20735
Posted: 07/09/2012 13:27:29
by Tim Molloy (Standard support level)
Joined: 11/13/2009
Posts: 21

Thanks. The reason I was trying to use SFTP, because I use the same type of solution for another customer over FTP and it works for that one...

So with TElSimpleFTPSClient how do I configure to transfer in ASCII mode?
#20736
Posted: 07/09/2012 13:31:39
by Vsevolod Ievgiienko (EldoS Corp.)

Hello.

It can be configured using TElSimpleFTPSClient.TransferType property.

Reply

Statistics

Topic viewed 1776 times

Number of guests: 1, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!