SSH Client/Server addr/port info

Discussion of open issues, suggestions and bugs regarding network security and data protection solution - SecureBridge
Post Reply
cboling
Posts: 24
Joined: Fri 12 Apr 2013 01:00

SSH Client/Server addr/port info

Post by cboling » Tue 06 Mar 2018 17:58

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.)

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: SSH Client/Server addr/port info

Post by ViktorV » Fri 09 Mar 2018 09:01

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

cboling
Posts: 24
Joined: Fri 12 Apr 2013 01:00

Re: SSH Client/Server addr/port info

Post by cboling » Fri 09 Mar 2018 23:55

Good 'nuff. Thanks!

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: SSH Client/Server addr/port info

Post by ViktorV » Mon 12 Mar 2018 13:05

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.

cboling
Posts: 24
Joined: Fri 12 Apr 2013 01:00

Re: SSH Client/Server addr/port info

Post by cboling » Tue 05 May 2020 04:58

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.

cboling
Posts: 24
Joined: Fri 12 Apr 2013 01:00

Re: SSH Client/Server addr/port info

Post by cboling » Tue 05 May 2020 05:17

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?

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: SSH Client/Server addr/port info

Post by ViktorV » Fri 08 May 2020 16:36

Thank you for the information. The issue and investigation is in progress. We will inform you when we have any results.

Post Reply