Page 1 of 1

SSH Client/Server addr/port info

Posted: Tue 06 Mar 2018 17:58
by cboling
I'd like to be able to log connections (both client & server components) w/ address/port info for both ends, like this:
10.0.0.1:1234 --> 10.0.0.2:22
Using the SimpleSSHServer demo, I learned how to get some of the info using IPEndpoint from WinSock on a TClientInfo object. (Similar code can also be found elsewhere in this forum.) Here's a simple function that returns a formatted address/port pair :

Code: Select all

function GetScSSHClientAddrPort(ClientInfo: TScSSHClientInfo): string;
var
  Sock: TIPEndPoint;
begin
  Sock := nil;
  try
    Sock := TIPEndPoint.Create(PSockAddrIn(ClientInfo.SockAddr));
    result := Sock.ToString+':'+IntToStr(Sock.Port);
  finally
    Sock.Free;
  end;
end;
TScSSHServer - 10.0.0.1:1234 --> ????:22
Feeding ClientInfo from the AfterClientConnect event to the above routine gives me the IP adddress & port of the client. I can cheat and say I already know my own port#, since I'm only listening on one port. What I need to know is how to determine the IP address of my own interface that the client connected to.

TScSSHClient - ????:1234 --> 10.0.0.2:22
After connecting, if I feed the SSHClient's .ClientInfo property to the same function, I get the address of the server that I connected to (handy, since I could've specified a name and thus not known the address) and my own port# used to establish the connection. As before, I do already know the port# that I'm connecting to. The missing piece of info is again my own IP address

How can I determine my own IP address being used for the connections? (Sorry, the Winsock API and I don't know each other well.)

Re: SSH Client/Server addr/port info

Posted: Fri 09 Mar 2018 09:01
by ViktorV
Currently, SecureBridge does not allow getting server IP in the AfterClientConnect and AfterClientDisconnect event handlers.
We will add this functionality in the next release of SecureBridge. You can follow the news about SecureBridge releases at https://devart.com/news

Re: SSH Client/Server addr/port info

Posted: Fri 09 Mar 2018 23:55
by cboling
Good 'nuff. Thanks!

Re: SSH Client/Server addr/port info

Posted: Mon 12 Mar 2018 13:05
by ViktorV
Thank you for the interest in our products.
If you have any questions during using our products, please don't hesitate to contact us - and we will try to help you solve them.

Re: SSH Client/Server addr/port info

Posted: Tue 05 May 2020 04:58
by cboling
A note to anyone reading this thread as a tutorial: The new property that was added is called LocalSockAddr; use it the same way as you use SockAddr, and it will return the IP address of the local interface used to establish the connection.

Re: SSH Client/Server addr/port info

Posted: Tue 05 May 2020 05:17
by cboling
The IP address part of the above works fine; however, I spotted a problem w/ the ports that I'd never noticed before: they're bogus; I can't figure out where the port#'s are coming from!
Example:
I open an SSH connection from my machine (192.168.214.11) to another machine (192.168.214.4:10022).
"netstat -n" shows a Local address of 192.168.214.11:2314 and a Foreign Address of 192.168.214.4:10022
(netstat on the foreign machine agrees, showing the same ports w/ local & foreign swapped)
However, the above Delphi code, using TIPEndPoint, shows a port# of 2569 for ClientInfo.LocalSockAddr and port 9767 if I feed it ClientInfo.SockAddr.

I ran it a 2nd time; this time, netstat shows port 2316 and my Delphi code reports 3081 for the local port. (The port when called w/ SockAddr is still 9767. I don't know what that's even supposed to be; 2 years ago, I just assumed that it was the local port, since it was an unfamiliar number.)

Any idea what I'm doing wrong -- how to get a valid number for the local port?

Re: SSH Client/Server addr/port info

Posted: Fri 08 May 2020 16:36
by ViktorV
Thank you for the information. The issue and investigation is in progress. We will inform you when we have any results.