9 #ifndef telnet_telnetConn_hpp    10 #define telnet_telnetConn_hpp    31 #include <mx/timeUtils.hpp>    33 #include "../../libs/libtelnet/libtelnet.h"    42 #ifndef TELNET_BUFFSIZE    43    #define TELNET_BUFFSIZE (1024)    49 static const telnet_telopt_t 
telopts[] = {
    50             { TELNET_TELOPT_ECHO,       TELNET_WONT, TELNET_DO   },
    51             { TELNET_TELOPT_TTYPE,      TELNET_WILL, TELNET_DONT },
    52             { TELNET_TELOPT_COMPRESS2,  TELNET_WONT, TELNET_DO   },
    53             { TELNET_TELOPT_MSSP,       TELNET_WONT, TELNET_DO   },
    56 #define TELNET_WAITING_USER (0)    57 #define TELNET_GOT_USER (1)    58 #define TELNET_WAITING_PASS (2)    59 #define TELNET_GOT_PASS (3)    60 #define TELNET_WAITING_PROMPT (4)    61 #define TELNET_LOGGED_IN (5)   119    int connect( 
const std::string & host, 
   120                 const std::string & port  
   124    int login( 
const std::string & username, 
   125               const std::string & password  
   140    int write( 
const std::string & buffWrite, 
   152    int read( 
const std::string & eot, 
   165    int read( 
int timeoutRead, 
   183    int writeRead( 
const std::string & strWrite, 
   190    static int send( 
int sock,
   217                          const std::string & port
   228    struct sockaddr_in addr;
   230    struct addrinfo hints;
   233    memset(&hints, 0, 
sizeof(hints));
   234    hints.ai_family = AF_UNSPEC;
   235    hints.ai_socktype = SOCK_STREAM;
   236    if ((rs = getaddrinfo(host.c_str(), port.c_str(), &hints, &ai)) != 0)
   238       fprintf(stderr, 
"getaddrinfo() failed for %s: %s\n", host.c_str(),
   244    if ((
m_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
   246       fprintf(stderr, 
"socket() failed: %s\n", strerror(errno));
   251    memset(&addr, 0, 
sizeof(addr));
   252    addr.sin_family = AF_INET;
   253    if (bind(
m_sock, (
struct sockaddr *)&addr, 
sizeof(addr)) == -1)
   255       fprintf(stderr, 
"bind() failed: %s\n", strerror(errno));
   262       fprintf(stderr, 
"connect() failed: %s\n", strerror(errno));
   274       fprintf(stderr, 
"error initializing telnet");
   283                        const std::string & password
   289    struct pollfd pfd[1];
   292    memset(pfd, 0, 
sizeof(pfd));
   294    pfd[0].events = POLLIN;
   297    while (poll(pfd, 1, -1) != -1)
   300       if (pfd[0].revents & POLLIN)
   302          if ((rs = recv(
m_sock, buffer, 
sizeof(buffer), 0)) > 0)
   313             fprintf(stderr, 
"recv(client) failed: %s\n",
   321          int rv = 
write(username + 
"\n", 1000);
   329          int rv = 
write(password + 
"\n", 1000);
   361    pfd.events = POLLOUT;
   363    std::string _buffWrite;
   367    t0 = mx::get_curr_time();
   369    size_t totWritten = 0;
   370    while( totWritten < _buffWrite.size())
   372       int timeoutCurrent = timeoutWrite - (mx::get_curr_time()-t0)*1000;
   375       int rv = poll( &pfd, 1, timeoutCurrent);
   379       telnet_send(
m_telnet, _buffWrite.c_str(), _buffWrite.size());
   380       totWritten = _buffWrite.size();
   383       std::cerr << 
"Wrote " << totWritten << 
" chars of " << buffWrite.size() << 
"\n";
   413    t0 = mx::get_curr_time();
   414    timeoutCurrent = timeoutRead;
   420    rv = poll( &pfd, 1, timeoutCurrent);
   428    telnet_recv(
m_telnet, buffRead, rv);
   433       timeoutCurrent = timeoutRead - (mx::get_curr_time()-t0)*1000;
   436       rv = poll( &pfd, 1, timeoutCurrent);
   444       telnet_recv(
m_telnet, buffRead, rv);
   448       std::cerr << 
"telnetRead: read " << rv << 
" bytes. buffRead=" << buffRead << 
"\n";
   478    rv = 
write( strWrite, timeoutWrite);
   491    t0 = mx::get_curr_time();;
   498       while( 
m_strRead.size() <= strWrite.size() )
   500          timeoutCurrent = timeoutRead - (mx::get_curr_time()-t0)*1000;
   503          rv = poll( &pfd, 1, timeoutCurrent);
   510          telnet_recv(
m_telnet, buffRead, rv);
   519    timeoutCurrent = timeoutRead - (mx::get_curr_time()-t0)*1000;
   523    return read(timeoutCurrent, 
false);
   534       if ((rs = ::
send(sock, buffer, size, 0)) == -1)
   536          fprintf(stderr, 
"send() failed: %s\n", strerror(errno));
   541          fprintf(stderr, 
"send() unexpectedly returned 0\n");
   570          if(ev->data.size == 0) 
break;
   572          char * buf = 
const_cast<char *
>(ev->data.buffer);
   573          buf[ev->data.size] = 0;
   574          for(
size_t i=0; i<ev->data.size; ++i)
   576             if(ev->data.buffer[i] < 20)
   585          std::string sbuf(buf);
   587          if(sbuf.size() == 0) 
break;
   611                if( sbuf.find(cs->
m_prompt) != std::string::npos)
   626          send(sock, ev->data.buffer, ev->data.size);
   652       case TELNET_EV_TTYPE:
   655          if (ev->ttype.cmd == TELNET_TTYPE_SEND)
   657             telnet_ttype_is(telnet, getenv(
"TERM"));
   662       case TELNET_EV_SUBNEGOTIATION:
   667       case TELNET_EV_ERROR:
   669          fprintf(stderr, 
"ERROR: %s\n", ev->error.msg);
   684 #endif //telnet_telnetConn_hpp ~telnetConn()
D'tor, conducts connection cleanup. 
 
int m_loggedin
Flag denoting the login state. 
 
static const telnet_telopt_t telopts[]
libtelnet option table. 
 
#define TTY_E_ERRORONREAD
 
std::string m_strRead
The accumulated string read from the device. 
 
int connect(const std::string &host, const std::string &port)
Connect to the device. 
 
#define TELNET_WAITING_PASS
 
#define TTY_E_ERRORONREADPOLL
 
int read(const std::string &eot, int timeoutRead, bool clear=true)
Read from a telnet connection, until end-of-transmission string is read. 
 
std::string m_usernamePrompt
The device's username entry prompt, used for managing login. 
 
A Telnet connection manager, wrapping libtelnet. 
 
Error numbers for the tty utilities. 
 
#define TTY_E_ERRORONWRITE
 
#define TELNET_WAITING_USER
 
#define TELNET_E_TELNETINIT
 
static int send(int sock, const char *buffer, size_t size)
Internal send for use by event_handler. 
 
int login(const std::string &username, const std::string &password)
Manage the login process on this device. 
 
#define TELNET_WAITING_PROMPT
 
int write(const std::string &buffWrite, int timeoutWrite)
Write to a telnet connection. 
 
#define TTY_E_ERRORONWRITEPOLL
 
int m_sock
The socket file descriptor. 
 
int telnetCRLF(std::string &telnetStr, const std::string &inputStr)
Replace lone \r and \n with \r\n for telnet-ness. 
 
#define TTY_E_TIMEOUTONREADPOLL
 
int noLogin()
Set flags as if we're logged in, used when device doesn't require it. 
 
int writeRead(const std::string &strWrite, bool swallowEcho, int timeoutWrite, int timeoutRead)
Write to a telnet connection, then get the reply. 
 
std::string m_prompt
The device's prompt, used for detecting end of transmission. 
 
int m_EHError
Used to indicate an error occurred in the event handler callback. 
 
std::string m_passwordPrompt
The device's password entry prompt, used for managing login. 
 
Utilities for i/o on a file descriptor pointing to a tty device. 
 
#define TTY_E_TIMEOUTONWRITEPOLL
 
telnet_t * m_telnet
libtelnet telnet_t structure 
 
bool isEndOfTrans(const std::string &strRead, const std::string &eot)
Check if the end of the buffer contains the end-of-transmission string. 
 
static void event_handler(telnet_t *telnet, telnet_event_t *ev, void *user_data)
Event handler callback for libtelnet processing. 
 
#define TTY_E_TIMEOUTONWRITE
 
#define TTY_E_TIMEOUTONREAD