3 #include <sys/socket.h>
7 #include "Exceptions.hpp"
9 static const char CRLF
[] = { 13, 10, 0 };
10 static const char IAC
[] = { 255, 0 };
12 Telnet::Telnet( int sock
)
21 TelnetPtr
Telnet::Create( int sock
)
23 TelnetPtr
ret( new Telnet( sock
) );
25 ret
->m_cmd
.reset( new TelnetCommand( ret
) );
34 // Read all that's waiting on the socket
38 int size
= recv( m_sock
, tmpBuf
, BufSize
, 0 );
44 // No error, just nothing to be read from socket
48 throw strerror( errno
);
52 throw ConnectionTerminatedException
;
55 buf
.append( tmpBuf
, size
);
58 // Parse telnet commands
59 for( unsigned int i
=0; i
<buf
.size(); i
++ )
61 if( m_cmd
->ParsingCommand() || (unsigned char)buf
[i
] == 255 )
63 if( !m_cmd
->Parse( buf
[i
] ) )
65 throw "Telnet parse error";
74 return m_readBuf
.find( CRLF
) != std::string::npos
;
77 void Telnet::Write( const std::string
& msg
)
80 for( unsigned int i
=0; i
<msg
.size(); i
++ )
84 if( (unsigned char)msg
[i
] == 255 )
92 char *ptr
= (char*)buf
.c_str();
94 while( pos
!= buf
.size() )
96 int size
= send( m_sock
, ptr
, buf
.size() - pos
, 0 );
100 throw strerror( errno
);
104 throw ConnectionTerminatedException
;
112 std::string
Telnet::GetBuf()
116 unsigned int pos
= m_readBuf
.find( CRLF
);
117 if( pos
== std::string::npos
)
119 throw "Trying to get telnet buffer when buffer not ready";
122 ret
= m_readBuf
.substr( 0, pos
);
124 m_readBuf
.erase( 0, pos
+ 2 );
129 void Telnet::EraseCharacter()
131 if( m_readBuf
.size() > 0 )
133 m_readBuf
.erase( m_readBuf
.end() - 1 );
137 void Telnet::EraseLine()
139 throw "Unimplemented";