EldoS | Feel safer!

Software components for data protection, secure storage and transfer

TElSocket.SendTo

Also by EldoS: CallbackFilter
A component to monitor and control disk activity, track file and directory operations (create, read, write, rename etc.), alter file data, encrypt files, create virtual files.
#6639
Posted: 06/16/2008 14:00:26
by Dean Reid (Basic support level)
Joined: 06/16/2008
Posts: 1

I am using the SSHClient and am able to connect with no problem (at least it appears that I am connected). However, when I try to send a file (using the TElSocket object SendTo method) I receive the following error message "Wrong socket state 0 in Send (error code is 96258)". I am evaluating this product to see if I can use the Eldos solution for secure connections and file encrytion. I have the file encryption and decryption working the last piece needed is sending a file from the client to the server.

Here is the code:
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using SBSimpleSSH;
using SBSSHConstants;
using SBSSHCommon;
using SBSSHKeyStorage;
using SBUtils;
using SBSocket;
using System.Net;
using System.Net.Sockets;

namespace SSHDemo {
   public  class SSHConnect  {

        private SBSimpleSSH.TElSimpleSSHClient client;
        private TElSSHMemoryKeyStorage  KeyStorage;
        private System.ComponentModel.Component cp = new System.ComponentModel.Component();

        public SSHConnect()
        {
            Initialize();
            client = new TElSimpleSSHClient();
            KeyStorage = new TElSSHMemoryKeyStorage(cp);
            client.KeyStorage = KeyStorage;
        }

        private void Initialize()   {
            SBUtils.Unit.SetLicenseKey(SBUtils.Unit.BytesOfString("0645...589D"));

          
          
        }

        public bool Connect()    {

            bool isOpen = false;
            if (client.Active) {
               Log("Already connected",false);
               isOpen = true;
                return isOpen;
            }

            
                client.Address = "ultasogeti";
                client.Port = 22;
                client.Username = "Administrator";
                client.Password = "p@ssw0rd";
                client.Versions = 0;
                client.AutoAdjustCiphers = true;

              
                client.Versions += SBSSHCommon.Unit.sbSSH1;
                client.Versions += SBSSHCommon.Unit.sbSSH2;

                client.ForceCompression = false;
                
                client.OnAuthenticationSuccess +=new TNotifyEvent(client_OnAuthenticationSuccess);
                client.OnKeyValidate +=new TSSHKeyValidateEvent(client_OnKeyValidate);
                
              
                KeyStorage.Clear();
                TElSSHKey Key = new TElSSHKey();
                //if ((dlg.txtPrivateKey.Text != "") && (Key.LoadPrivateKey(dlg.txtPrivateKey.Text, "") == 0))
               // {
                    
                    KeyStorage.Add(Key);
               //     client.AuthenticationTypes = client.AuthenticationTypes | SBSSHConstants.Unit.SSH_AUTH_TYPE_PUBLICKEY;
               // }
               // else
                   client.AuthenticationTypes = SBSSHConstants.Unit.SSH_AUTH_TYPE_PASSWORD;
                  // client.AuthenticationTypes = client.AuthenticationTypes & (~SBSSHConstants.Unit.SSH_AUTH_TYPE_PUBLICKEY);
                    

                Log("Connecting to " + client.Address + "...", false);
                
                try  {
                    client.Open();
                    //timer.Enabled = true;
                    isOpen = true;
                  
                }
                catch (Exception ex) {
                    Log("SSH connection failed", true);

                    return false;
                }

                Log("SSH connection established", false);
                return isOpen;
            
        }

        void client_OnAuthenticationStart(object Sender, int SupportedAuths)
        {
            throw new NotImplementedException();
        }
        private void Disconnect()
        {
            if (client.Active)
            {
                Log("Disconnecting", false);
                client.Close();
            }
        }
      
        public void Send(string Data) {
            byte[] encoded = System.Text.Encoding.UTF8.GetBytes(Data + "\x0d\x0a");
            client.SendData(encoded, encoded.Length);
            client.SendText(Data);
            
             Log("Sent encrypted file", false);
            Disconnect();
            Console.ReadLine();
        }
        public void SendEncryptedData (String data){
            TElSocket tSocket = new TElSocket();

            IPAddress ip = IPAddress.Parse("10.2.2.184");
            IPEndPoint endPoint = new IPEndPoint(ip,22);
            
            
            byte[] dataBytes = Encoding.Default.GetBytes(Settings.fileLocation +  data);
            int b = 0;
            int i =0;
            try {
                i = tSocket.SendTo(dataBytes, 1, dataBytes.Length, ref b, endPoint.Address.ToString(), Convert.ToUInt16(endPoint.Port));
               // tSocket.Send(dataBytes, 1, dataBytes.Length, ref b);
            }
            catch (Exception ex){
                Log(String.Format("Send to error {0}",ex.Message),false);
            }
            if  (i !=-1){
                Log("Sent encrypted file", false);
            }else{
                Log("Encrypted file NOT sent", false);
            }
            
            Disconnect();
            Console.ReadLine();
        }

        private void client_OnAuthenticationFailed(object Sender, int AuthenticationType) {
            //timer.Enabled = false;
            Log("Authentication type (" + AuthenticationType.ToString() + ") failed", true);
        }

        private void client_OnAuthenticationSuccess(object Sender) {
            Log("Authentication succeeded", false);
            TElSSHKey key;
            key = ((SBSimpleSSH.TElSimpleSSHClient)Sender).ServerKey;
            client.KeyStorage.Add(key);
        }

        private void client_OnCloseConnection(object Sender) {
           // timer.Enabled = false;
            Log("SSH connection closed", false);
        }

        private void client_OnError(object Sender, int ErrorCode) {
            Log("SSH error " + ErrorCode.ToString(), true);
        }

        private void client_OnKeyValidate(object Sender, SBSSHKeyStorage.TElSSHKey ServerKey, ref bool Validate) {
            Log("Server key received, fingerprint " + SBUtils.Unit.DigestToStr128(ServerKey.FingerprintMD5, true), false);
            if (ServerKey.IsKeyValid() ){
                Validate = true; // NEVER do this. You MUST check the key validity somehow
            }
            
        }

      

        private void Log(string S, bool error)  {
          
            string[] items = new string[2];
            items[0] = DateTime.Now.ToShortTimeString();
            items[1] = S;
            Console.WriteLine(items[0] + " " + items[1]);
            
        }
    }
}
#6640
Posted: 06/16/2008 14:23:23
by Eugene Mayevski (EldoS Corp.)

I am sorry, but the route you've taken is far from the right one. To send the data to the remote host over SSH you need to use ElSimpleSSHClient.SendData method. To receive the data you need to use ElSimpleSSHClient.ReceiveData method. Please see the sample application (SimpleSSHClient will be a good one) to see how it transfers the data.


Sincerely yours
Eugene Mayevski

Reply

Statistics

Topic viewed 1966 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!