[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?


On 22/07/17 10:25, Nicolas Lykke Iversen wrote:
Thank you Stef,

What socket do you use to get the address and port of the client? In what structure is it located?

you can use "ssh_get_fd" to get it from your "ssh_session":

socket_t ssh_get_fd(ssh_session session)

jcs

-Nicolas

2017-07-21 15:55 GMT+02:00 Stef Bon <stefbon@xxxxxxxxx <mailto:stefbon@xxxxxxxxx>>:

    2017-07-21 14:11 GMT+02:00 Nicolas Lykke Iversen
    <nlykkei@xxxxxxxxx <mailto: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