11 #include <sys/types.h>
12 #include <sys/socket.h>
14 #include <netinet/in.h>
32 const std::string & port
43 struct sockaddr_in addr;
45 struct addrinfo hints;
48 memset(&hints, 0,
sizeof(hints));
49 hints.ai_family = AF_UNSPEC;
50 hints.ai_socktype = SOCK_STREAM;
51 if( getaddrinfo(host.c_str(), port.c_str(), &hints, &ai) != 0)
57 if ((
m_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
63 memset(&addr, 0,
sizeof(addr));
64 addr.sin_family = AF_INET;
65 if (bind(
m_sock, (
struct sockaddr *)&addr,
sizeof(addr)) == -1)
91 const std::string & password
100 memset(pfd, 0,
sizeof(pfd));
102 pfd[0].events = POLLIN;
109 while ( (pollrv = poll(pfd, 1, 30000)) > 0)
115 if (pfd[0].revents & POLLIN)
120 if ((rs = recv(
m_sock, buffer,
sizeof(buffer), 0)) > 0)
123 std::cerr <<
"read: " << rs <<
"bytes\n";
134 fprintf(stderr,
"recv(client) failed: %s\n",
142 int rv =
write(username +
"\n", 1000);
150 int rv =
write(password +
"\n", 1000);
191 pfd.events = POLLOUT;
193 std::string _buffWrite;
197 t0 = mx::sys::get_curr_time();
199 size_t totWritten = 0;
200 while( totWritten < _buffWrite.size())
202 int timeoutCurrent = timeoutWrite - (mx::sys::get_curr_time()-t0)*1000;
205 int rv = poll( &pfd, 1, timeoutCurrent);
209 telnet_send(
m_telnet, _buffWrite.c_str(), _buffWrite.size());
210 totWritten = _buffWrite.size();
213 std::cerr <<
"Wrote " << totWritten <<
" chars of " << buffWrite.size() <<
"\n";
242 t0 = mx::sys::get_curr_time();
243 timeoutCurrent = timeoutRead;
249 rv = poll( &pfd, 1, timeoutCurrent);
257 telnet_recv(
m_telnet, buffRead, rv);
262 timeoutCurrent = timeoutRead - (mx::sys::get_curr_time()-t0)*1000;
265 rv = poll( &pfd, 1, timeoutCurrent);
273 telnet_recv(
m_telnet, buffRead, rv);
277 std::cerr <<
"telnetRead: read " << rv <<
" bytes. buffRead=" << buffRead <<
"\n";
305 rv =
write( strWrite, timeoutWrite);
318 t0 = mx::sys::get_curr_time();;
325 while(
m_strRead.size() <= strWrite.size() )
327 timeoutCurrent = timeoutRead - (mx::sys::get_curr_time()-t0)*1000;
330 rv = poll( &pfd, 1, timeoutCurrent);
337 telnet_recv(
m_telnet, buffRead, rv);
346 timeoutCurrent = timeoutRead - (mx::sys::get_curr_time()-t0)*1000;
350 return read(timeoutCurrent,
false);
360 if ((rs = ::
send(sock, buffer, size, 0)) == -1)
362 fprintf(stderr,
"send() failed: %s\n", strerror(errno));
367 fprintf(stderr,
"send() unexpectedly returned 0\n");
395 std::cerr <<
"Got ev->data.size: " << ev->data.size <<
"\n";
399 if(ev->data.size == 0)
break;
401 char * buf =
const_cast<char *
>(ev->data.buffer);
402 buf[ev->data.size] = 0;
405 for(
size_t i=0; i<ev->data.size; ++i)
407 if(ev->data.buffer[i] < 32)
423 for(
size_t i=nn; i < ev->data.size;++i)
425 if( ev->data.buffer[i] == 0)
437 std::string sbuf(buf);
439 if(sbuf.size() == 0)
break;
442 std::cerr <<
"ev->data: " << sbuf <<
"\n";
467 if( sbuf.find(cs->
m_prompt) != std::string::npos)
482 send(sock, ev->data.buffer, ev->data.size);
508 case TELNET_EV_TTYPE:
511 if (ev->ttype.cmd == TELNET_TTYPE_SEND)
513 telnet_ttype_is(telnet, getenv(
"TERM"));
518 case TELNET_EV_SUBNEGOTIATION:
523 case TELNET_EV_ERROR:
525 fprintf(stderr,
"ERROR: %s\n", ev->error.msg);
static const telnet_telopt_t telopts[]
libtelnet option table.
bool isEndOfTrans(const std::string &strRead, const std::string &eot)
Check if the end of the buffer contains the end-of-transmission string.
int telnetCRLF(std::string &telnetStr, const std::string &inputStr)
Replace lone \r and \n with \r\n for telnet-ness.
A Telnet connection manager, wrapping libtelnet.
int m_EHError
Used to indicate an error occurred in the event handler callback.
int login(const std::string &username, const std::string &password)
Manage the login process on this device.
std::string m_usernamePrompt
The device's username entry prompt, used for managing login.
int write(const std::string &buffWrite, int timeoutWrite)
Write to a telnet connection.
telnet_t * m_telnet
libtelnet telnet_t structure
~telnetConn()
D'tor, conducts connection cleanup.
static int send(int sock, const char *buffer, size_t size)
Internal send for use by event_handler.
std::string m_strRead
The accumulated string read from the device.
static void event_handler(telnet_t *telnet, telnet_event_t *ev, void *user_data)
Event handler callback for libtelnet processing.
int m_loggedin
Flag denoting the login state.
int m_sock
The socket file descriptor.
int noLogin()
Set flags as if we're logged in, used when device doesn't require it.
std::string m_passwordPrompt
The device's password entry prompt, used for managing login.
std::string m_prompt
The device's prompt, used for detecting end of transmission.
int writeRead(const std::string &strWrite, bool swallowEcho, int timeoutWrite, int timeoutRead)
Write to a telnet connection, then get the reply.
int read(const std::string &eot, int timeoutRead, bool clear=true)
Read from a telnet connection, until end-of-transmission string is read.
int connect(const std::string &host, const std::string &port)
Connect to the device.
Managing a connection to a telnet device.
#define TELNET_WAITING_USER
#define TELNET_WAITING_PROMPT
#define TELNET_WAITING_PASS
#define TTY_E_TIMEOUTONREAD
#define TTY_E_TIMEOUTONREADPOLL
#define TTY_E_ERRORONWRITE
#define TTY_E_ERRORONWRITEPOLL
#define TTY_E_ERRORONREADPOLL
#define TTY_E_ERRORONREAD
#define TELNET_E_TELNETINIT
#define TTY_E_TIMEOUTONWRITEPOLL
#define TELNET_E_LOGINTIMEOUT
#define TTY_E_TIMEOUTONWRITE