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>
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.


Sign In
Create Account


Back to top









