Re: How do I get the remote address and port of a SSH client connecting to a SSH server?
- Subject: Re: How do I get the remote address and port of a SSH client connecting to a SSH server?
- From: Stef Bon <stefbon@xxxxxxxxx>
- Reply-to: libssh@xxxxxxxxxx
- Date: Fri, 21 Jul 2017 15:55:20 +0200
- To: libssh@xxxxxxxxxx
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);
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);
Archive administrator: firstname.lastname@example.org