Jump to content

Telnet implementation

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
elegos

elegos

    Newbie

  • Members
  • PipPip
  • 13 posts
This is an example of telnet session to my server (bold are commands)

Quote

telnet ADDRESS PORT
Trying ADDRESS...
Connected to ADDRESS.
Escape character is '^]'.
Benvenuto in Dawn of Elegy!@Ricordati di frequentare il sito (e forum)!@http://www.dawnofelegy.org
USER myuser
PASS mypass
+Logged in.
TC>help
Syntax: .help [$command]

Display usage instructions for the given $command. If no $command provided show list available commands.
Commands available to you:

account ...
gm ...
tele ...
reload ...
list ...
lookup ...
pdump ...
guild ...
instance ...
server ...
announce
gmannounce
notify
gmnotify
commands
help
saveall
kick
ban ...
unban ...
banlist ...
plimit
pinfo
senditems
sendmail
sendmoney
rename
loadscripts
mute
unmute
ahbotoptions
chardelete
sendmessage
TC>

This is what I've done so far:

telnet.h
/*

 *  telnet.h

 *  Trinity Remote Administrator

 *

 *  Created by Giacomo Furlan on 29/01/09.

 *  Copyright 2009 EleGoSoft. All rights reserved.

 *

 */

 

#define MAXBUFFER 1024 // max buffer allowed by server

 

#define ERR_GETHOSTBYNAME 1

#define ERR_CONNECTION 2

#define ERR_LOGIN 3

 

#define TELNET_SEND 0

#define TELNET_REC 1

 

int sendCommandToRA(const char*, int, const char*, const char*, const char*, int, char*);

telnet.cpp
/*

 *  telnet.cpp

 *  Trinity Remote Administrator

 *

 *  Created by Giacomo Furlan on 29/01/09.

 *  Copyright 2009 EleGoSoft. All rights reserved.

 *

 */

 

#include "telnet.h"

 

#include <stdio.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <unistd.h>

#include <netdb.h>

#include <string.h>

 

int sendCommandToRA(const char *address, int port, const char *user, const char *pass, const char *command, int sendorrec = TELNET_SEND, char *reply = NULL)

{

    // variables declaration

    int server, err;

    char MSG[MAXBUFFER];

    struct sockaddr_in server_addr;

    struct hostent *host_address;

    char buffer[MAXBUFFER];

 

    // socket creation

    server = socket(AF_INET, SOCK_STREAM, 0);

 

    server_addr.sin_family  =   AF_INET;

    server_addr.sin_port    =   htons(port);

 

    // get IP from hostname

    host_address=gethostbyname(address);

    if(host_address == 0)

    {

        printf("Gethostbyname faliled!\n");

        if (h_errno==HOST_NOT_FOUND) printf("host not found\n");

        if (h_errno==NO_ADDRESS) printf("name is valid but no has IP address\n");

        if (h_errno==NO_RECOVERY) printf("non recoverable name server error occurred\n");

        return ERR_GETHOSTBYNAME;

    }

    bcopy(host_address->h_addr,&server_addr.sin_addr,host_address->h_length);

   

    // connecting

    err = connect(server,(struct sockaddr*) &server_addr, sizeof(server_addr));

   

    if(!err)

    {

        // wait for server response

        recv(server,buffer,MAXBUFFER,0);

       

        // log in

        sprintf(MSG,"USER %s\r",user);

            send(server,MSG,MAXBUFFER,0);

        sprintf(MSG,"PASS %s\r",pass);

            send(server,MSG,MAXBUFFER,0);

       

        // wait for server response

        recv(server, buffer, MAXBUFFER, 0); 

        printf(buffer);

       

        // login check (expected value)

        strncpy(buffer, buffer, 11);

        buffer[11] = '\0';

        if(strcmp(buffer,"+Logged in."))

            return ERR_LOGIN;

        else

        {

            sprintf(MSG,"%s\r",command);

            send(server,MSG,MAXBUFFER,0);

            if(sendorrec == TELNET_REC)

            {

                recv(server,buffer,MAXBUFFER,0);

                strcpy(reply,buffer);

            }

        }

    }

    else

        return ERR_CONNECTION;

   

    close(server);

    return 0;

}

Problems:
1. it doesn't work. the "// wait for server response" printf(buffer) allways report me the MOTD, instead of "+Logged in." or something else ("-Access denied" for example). --> FIXED: now I allways return this: "\220\324" (without quotes)... what does it mean?

2. I think this is pretty a scratch of what I can implement telnet, this is just what (I think) I've understood from socket online manuals and examples...

3. not sure if actually the buffer takes only a single line of reply or all till buffer is 100% loaded

Can you please help me?

Thanks :)

Edited by elegos, 31 January 2009 - 12:59 AM.


#2
Lance

Lance

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
Would you be interested to look into an actual implementation of a telnet client?

Quote

net-misc/telnet-bsd
Latest version available: 1.2-r1
Latest version installed: [ Not Installed ]
Size of files: 189 kB
Homepage: ftp://ftp.suse.com/pub/people/kukuk/ipv6/
Description: Telnet and telnetd ported from OpenBSD with IPv6 support
License: BSD