[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How do I get the remote address and port of a SSH client connecting to a SSH server?


2017-07-21 14:11 GMT+02:00 Nicolas Lykke Iversen <nlykkei@xxxxxxxxx>:
> I've a working SSH client and server written using libssh.
>
> On the server side, I would like to know the address and port of the client
> once it has connected.
>
> Looking through ssh_bind and ssh_session, I can get the address and port on
> which the server listens. However, I cannot find any fields to get the
> address and port of the connected client?

I just know a general method. Recently I had the same issue.
If you have a socket (=fd) for the connection between server and client,
get the sockaddr addr using:

getpeername(fd, &addr, &len)

this addr is the input for getnameinfo:

getnameinfo(&addr, len, buffer, NI_MAXHOST, NULL, 0, NI_NAMEREQD);

where
char buffer[NI_MAXHOST]
see man getnameinfo.

in buffer is the hostname of the client as the server knows it. This
may not be the exact dns name,
if the hostnames are not available through dns.

O and the address in case of ipv4 is simular:

struct sockaddr_in addr
socklen_t len=sizeof(struct sockaddr_in);
getpeername(fd, &addr, &len);
address=inet_ntoa(addr.sin_addr);

Stef

Archive administrator: postmaster@lists.cynapses.org