Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef NVT_H
- #define NVT_H
- #include "socket.h"
- #include "terminal.h"
- #include <string>
- #include <list>
- #include <boost/thread/mutex.hpp>
- using namespace std;
- // TELNET COMMANDS
- #define SE 240
- #define NOP 241 // No Operation
- #define DM 242
- #define BRK 243
- #define IP 244
- #define AO 245
- #define AYT 246 // Are You There?
- #define EC 247
- #define EL 248
- #define GA 249 // Go Ahead
- #define SB 250
- #define WILL 251
- #define WONT 252
- #define DO 253
- #define DONT 254
- #define IAC 255 // Interpret As Command
- // TELNET OPTIONS
- #define BINARY 0
- #define SGA 3 // Suppress Go Ahead
- #define STATUS 5
- #define ECHO 1 // Echo
- #define TMARK 6
- #define TERM_TYPE 24
- #define WIN_SIZE 31
- #define TERM_SPEED 32
- #define FLOW_CTRL 33
- #define LINEMODE 34
- #define ENV_VARS 36
- #define EOR 25 // End Of Record
- #define MSDP 69 // MUD Server Data Protocol
- #define MSSP 70 // MUD Server Status Protocol
- #define MCCP1 85 // MUD Client Compression Protocol (v1)
- #define MCCP2 86 // MUD Client Compression Protocol (v2)
- #define MSP 90 // MUD Sound Protocol
- #define MXP 91 // MUD eXtension Protocol
- #define IN_BUF_SIZE 1024 // Maximum size of local input buffer
- #define OUT_BUF_SIZE 4096 // Maximum size of local buffer for server output
- class Terminal;
- /* This struct represents a Telnet command. It is used by NVT to store telnet commands from server in
- * its internal list and to process them when needed */
- typedef struct
- {
- int command;
- int option;
- } TelnetCommand;
- /* This class represents a Network Virtual Terminal. Basically, it's an abstract device which possess
- * a display and a keyboard. Telnet protocol assumes that at each end of telnet connection there is
- * a NVT, which translates transmitted commands and escape characters into real actions on the
- * physical terminal (or terminal emulator). The only requirement is that NVT's virtual display should
- * be able to handle escape characters and it's virtual keyboard must generate all 128 ASCII
- * characters and escape sequences such as CR, BS, VT etc... */
- class NVT {
- private:
- Socket *sock;
- char in_buf[IN_BUF_SIZE]; // local input buffer (virtual keyboard), contains user and telnet commands
- char out_buf[OUT_BUF_SIZE]; // local output buffer (virtual display), contains raw server output
- int in_bytes_count; // total number of bytes in local input buffer
- int out_bytes_count; // total number of bytes in local output buffer
- boost::mutex sendMutex; // Needed by send() and write*Command methods to make them thread-safe
- boost::mutex recvMutex; // Needed by receive() method to make it thread safe
- public:
- /* Constructor. Creates a Network Virtual Terminal and connects it to given host */
- NVT(string host, int port);
- // Send the local input buffer data to server and clear it. RETURNS: number of sent bytes
- int send(); // THREAD SAFE
- // Receive the response from server and store it into local output buffer. RETURNS: number of received bytes
- int receive(); // THREAD SAFE
- // Write (append) an user command to local input buffer
- void writeUserCommand(string s); // THREAD SAFE
- // Generate a telnet command byte sequence and write it (append) to local input buffer
- void writeTelnetCommand(int command, int option); // THREAD SAFE
- // Extract telnet commands from output buffer and reply to server
- void processCommands();
- // Print the server output to terminal or to stdout (if t is NULL)
- void print(Terminal *t = NULL);
- // Retrieve a const pointer to local output buffer
- const char* getOutBuffer() const;
- // Retrieve a const pointer to input buffer
- const char* getInBuffer() const;
- // Retrieve a size of output buffer
- int getOutBufSize() const;
- // Retrieve a size of input buffer
- int getInBufSize() const;
- // Get a pointer to NVT's socket object
- Socket* getSocketPtr();
- // Destructor. Closes the remote connection and destroys socket.
- ~NVT();
- };
- #endif // NVT_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement