0 votes
by (160 points)

Applies to: File Server

1 Answer

+1 vote
by (150k points)
selected by
 
Best answer

var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(new DnsEndPoint("test.rebex.net", 22));
Console.WriteLine(socket.RemoteEndPoint);
socket.Close();

var ep = (IPEndPoint)args.ClientEndPoint;
IPHostEntry entry = Dns.GetHostEntry(ep.Address);
string hostName = (entry != null) ? entry.HostName : null;

by (160 points)
Thanks for the explanation! However, it does not really address the problem that I'm trying to solve. The service that I'm implementing has multiple DNS addresses associated to the same IP address. Each DNS address represents a different "virtual host" and that information provides context for authentication and the file system that needs to be presented to the client. So the information that I am trying to gather is the actual host address that the client used to establish this connection (reverse DNS would return all the address associated with the IP, but would not tell me which one was used by the client...). The HTTP protocol solves this problem through the "Host" header, and the FTP protocol has a "HOST" command to solve this problem (see https://tools.ietf.org/html/rfc7151). I don't know if this information is transmitted in the connection in a way that would be available to the SFTP server.
by (150k points)
Thanks for the clarification, I understand now. This would indeed be a very useful, but unfortunately, there is no such feature in the SSH protocol - the host name is not transmitted to the server. It might be possible to add a custom extension for it, but that would only work if both the SSH client and SSH server supported it, which is a showstopper for almost all scenarios.
There was a discussion about adding such feature to OpenSSH few years ago (see https://lists.gt.net/openssh/dev/55943), but it got nowhere (and the suggested workarounds are not really suitable, I'm afraid).
by (160 points)
Ok, thanks for the link! This answers my question.
...