seanmiddleditch / libtelnet

Simple RFC-complient TELNET implementation as a C library.
http://github.com/seanmiddleditch/libtelnet
Other
329 stars 133 forks source link

Simple client that only talks #59

Closed alejandro-colomar closed 4 years ago

alejandro-colomar commented 4 years ago

I would like to create a simple client that only logs to the server (as@192.168.0.1:23 - no password) and sends messages such as const char *msg = "exe\n";.

I don't have any experience using telnet (some experience but not much using tcp sockets).

I tried to read the header libtelnet.h to see how I can do that, but I didn't know which options I had to use. I also don't know how to tell the library the info of the server (user: as; ip: 192.168.0.1; port: 23).

What functions and options should I use for this?

xiaoxiang781216 commented 4 years ago

You can modify this simple client to fit your need: https://github.com/seanmiddleditch/libtelnet/blob/develop/util/telnet-client.c

alejandro-colomar commented 4 years ago

I'm sorry, but I've already seen that code, and that doesn't fit into my concept of simple.

I think it should use some abstraction between sockets, tcp, and telnet. I tried to do it, but I can't understand from that code how is libtelnet supposed to be used.

Do I still need to open the tcp socket myself? I would expect a libtelnet library to handle all that stuff. Is it a client or a server? Because I see some code trying to open a socket as a server.

I think it will be easier for me to open a pipe with something like this: FILE *telnet = popen("telnet -l as 192.168.0.1 23", "w");

xiaoxiang781216 commented 4 years ago

No, libtelnet doesn't design this way, you need manage the socket by yourself. Of course, if you prefer file API, you can create something like telnet_popen on top of libtelnet. This isn't a very hard work.

alejandro-colomar commented 4 years ago

I would like to do it, but I'm having a lot of trouble understanding the code. I'm talking, for example, about lines like this one: /* bind server socket */ ¿server?

In fact, I don't need a file API, but a socket API where libtelnet already gives me a working socket would be nice.

I don't see any benefit in writing the code to get the socket outside of libtelnet. It's always going to be the same. Repeating the same code for every program that uses the library isn't good IMHO.

Couldn't this function be used inside libtelnet to provide a socket? :

int alx_tcp_client_open (const char *restrict server_addr,
                 const char *restrict server_port)
{
    struct protoent *tcp;
    int     sd;
    struct addrinfo hint = {0};
    struct addrinfo *addrs;
    int     status;

    tcp = getprotobyname("tcp");
    if (!tcp)
        return  -EINVAL;
    hint.ai_family      = AF_UNSPEC;
    hint.ai_socktype    = SOCK_STREAM;
    hint.ai_protocol    = tcp->p_proto;
    status  = getaddrinfo(server_addr, server_port, &hint, &addrs);
    if (status)
        return  -labs(status);

    for (struct addrinfo *ad = addrs; ad; ad = ad->ai_next) {
        sd = socket(ad->ai_family, ad->ai_socktype, ad->ai_protocol);
        if (sd < 0)
            continue;
        if (connect(sd, ad->ai_addr, ad->ai_addrlen))
            goto try_next;
        break;
try_next:
        close(sd);
        sd = -1;
    }
    freeaddrinfo(addrs);

    if (sd < 0)
        return  -errno;
    return  sd;
}
xiaoxiang781216 commented 4 years ago

I amn't the libtelnet owner, you can open an issue to discuss this type of API.