Coder Social home page Coder Social logo

Comments (5)

xiaoxiang781216 avatar xiaoxiang781216 commented on July 30, 2024

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

from libtelnet.

alejandro-colomar avatar alejandro-colomar commented on July 30, 2024

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");

from libtelnet.

xiaoxiang781216 avatar xiaoxiang781216 commented on July 30, 2024

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.

from libtelnet.

alejandro-colomar avatar alejandro-colomar commented on July 30, 2024

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;
}

from libtelnet.

xiaoxiang781216 avatar xiaoxiang781216 commented on July 30, 2024

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

from libtelnet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.