User avatar
tadd
Posts: 14
Joined: Wed Dec 17, 2014 5:15 am
Location: Raleigh, NC
Contact: Website

Need API to interact with telnet server from C

Fri Nov 16, 2018 4:58 am

I am most familiar with C but not in embedded environments. I'm used to working with small cpu systems, not using TCP/IP. I'm trying to add a new utility to a ham radio project which is based on the Raspberry PI. What I want to do is interactively Telnet into an application which is run on the same Raspberry PI, and then have a running dialog with that application's command interface (via telnet) to keep track of, log, and add data to, some real time activities the application is performing over a period of weeks.

My new program would issue commands which read back ASCII. I'd then parse the text, store it away, and command the application to perform actions.

I can perform the actions I want using Telnet (I downloaded and installed Telnet) and have them work. So... first I need to connect into the application and then wait for a log-in prompt, answer the prompt, wait for a password prompt, answer that, and then send a series of commands, capturing the returned text.

I have another program on the same project using a /dev/emulated-tty-port stream into the same application but the Telnet interface is better supported by the application. It's much faster, for one thing.

I sort of got something to work in Python to do some of what I want but I'd really rather work in C.

I was advised to use libcurl but after a bit of reading I don't see that interactive Telnet is one of its strong points. Hit me over the head with some kilobytes if I'm wrong.

What can you suggest for library or API I can use to Telnet from a C program? Thanks!
Tadd
Last edited by tadd on Fri Nov 16, 2018 11:38 pm, edited 1 time in total.

User avatar
DougieLawson
Posts: 34177
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Need API to interact with telnet server from C

Fri Nov 16, 2018 9:05 am

Telnet from C is just a socket client program.

Code: Select all

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

main( argc, argv)
int argc;
char **argv;
{
        int sockint, s; /* client socket */
        unsigned short port ; /* port number client will connect to */
        struct sockaddr_in server; /* server address */
        char buf[32]; /* data buffer for sending and receiving */

        if( argc != 3 ) { /* check command line arguments */
                fprintf( stderr, "Usage: %s hostname port\n", argv[0] );
                exit(1);
        }

        port = (unsigned short) atoi(argv[2]); /* get the port number */

        strcpy( buf, "Hello there!"); /* create the message */

        /* create stream socket using TCP */
        fprintf(stderr, "Creating datagram socket.\n");
        s = socket(AF_INET, SOCK_STREAM, 0);
        if( s == -1 ) {
                fprintf(stderr, "Socket was not created.\n");
                exit(2);
        }
        else
                fprintf(stderr, "Socket created successfully.\n");

        server.sin_family = AF_INET; /* set up the server name */
        server.sin_port = htons(port);
        server.sin_addr.s_addr = inet_addr( argv[1] );

        /* connect to the server */
        if( connect(s, &server, sizeof(server)) < 0) {
                fprintf(stderr, "Failed to connect to socket.\n");
                exit(3);
        }

        printf("Sending the message: %s\n", buf); /* send the message */
        if( send(s, buf, sizeof(buf), 0) < 0 ) {
                fprintf(stderr, "Failed to send data to socket.\n");
                exit(4);
        }

        /* receive the echoed message from the server */
        if( recv(s, buf, sizeof(buf), 0) < 0 ) {
                fprintf(stderr, "Failed to receive data from socket.\n");
                exit(5);
        }
        printf("The message from the server was: %s\n", buf);

        close(s); /* close the socket */
        printf("Client closed successfully\n"); /* successfully exit */
        exit(0);
}

Compile that with gcc -o client client.c
Run that with ./client 127.0.0.1 23

Code: Select all

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

main()
{
        int sockint, s; /* socket for accepting connections */
        int namelen; /* length of client name */
        int ns; /* client socket */
        struct sockaddr_in client; /* client address information */
        struct sockaddr_in server; /* server address information */
        char buf[32]; /* data buffer */

        s = socket(AF_INET, SOCK_STREAM, 0); /* create stream socket using TCP */
        if( s == -1 ) {
                fprintf(stderr, "socket() Socket was not created.\n");
                exit(2);
        }
        else
                fprintf(stderr,"Socket created successfully.\n");

        server.sin_family = AF_INET; /* set up the server name */
        server.sin_port = 0; /* use first available port number */
        server.sin_addr.s_addr = INADDR_ANY;

        if( bind(s, &server, sizeof( server )) < 0 ) { /* bind server address to socket */
                fprintf(stderr,"bind() Error binding server.\n");
                exit(3);
        }

        /* find out what port was assigned */
        namelen = sizeof( server );
        if( getsockname( s, (struct sockaddr *) &server, &namelen) < 0 ) {
                fprintf(stderr, "getsockname() failed to get port number\n");
                exit(4);
        }
        fprintf(stderr,"The assigned port is %d\n", ntohs( server.sin_port));

        if( listen(s, 1) != 0 ) { /* listen for a connection */
                fprintf(stderr, "listen() failed\n");
                exit(5);
        }

        namelen = sizeof(client); /* accept connection request */
        if( (ns = accept(s, &client, &namelen)) == -1) {
                fprintf(stderr, "accept() failed to accept client connection request.\n");
                exit(6);
        }

        if( recv(ns, buf, sizeof(buf), 0) == -1 ) { /* wait for a client message to arrive */
                fprintf(stderr, "recv() did not get client data\n");
                exit(7);
        }

        if( send( ns, buf, sizeof(buf), 0) < 0) { /* echo the client message back to the client */
                fprintf(stderr, "send() failed to send data back to client.\n");
                exit(8);
        }

        close(ns); /* close client socket connection */
        close(s); /* close server socket connection */

        printf("Server finished.\n");
        exit(0);
}
is a sample listener that simply echos what you send to it then terminates you can use with that sample client program.
Note:The use of baseball bats for educational purposes is completely disallowed on this forum.

Any DMs sent on Twitter will be answered next month.

bzt
Posts: 221
Joined: Sat Oct 14, 2017 9:57 pm

Re: Need API to interact with telnet server from C

Fri Nov 16, 2018 11:23 am

Hi,

As @DougieLawson said. If you need more information, check out

this presentation explains how networking works in great detail with example code and figures

this tutorial explains each and every function DougieLawson used in great detail

many examples on Berkley sockets, here the TCPEchoClient.c example can be used directly with a telnet server, and TCPEchoServer.c with a telnet client. Also has examples on accepting connections from multiple clients using different techniques, like fork(), pthread() and select(), as well as how to resolve domain names from C etc.

If you later want to secure your connection with SSL, I suggest to take a look at this code. You'll find many overcomplicated examples on the net, most of them using different layers (like BIO). You won't need those. All you need is SSL_read, SSL_write as in this example, otherwise the logic is exactly the same with a simple TCP client (btw that code has an OpenConnection() function which is exactly what you're after right now).

Cheers,
bzt

User avatar
tadd
Posts: 14
Joined: Wed Dec 17, 2014 5:15 am
Location: Raleigh, NC
Contact: Website

Re: Need API to interact with telnet server from C

Fri Nov 16, 2018 11:39 pm

Perfect. Thanks!

Return to “C/C++”