SHARE
TWEET

Untitled

a guest Aug 13th, 2017 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ifndef NVT_H
  2. #define NVT_H
  3.  
  4. #include "socket.h"
  5. #include "terminal.h"
  6. #include <string>
  7. #include <list>
  8. #include <boost/thread/mutex.hpp>
  9. using namespace std;
  10.  
  11. // TELNET COMMANDS
  12. #define SE      240
  13. #define NOP     241     // No Operation
  14. #define DM      242
  15. #define BRK     243
  16. #define IP      244
  17. #define AO      245
  18. #define AYT     246     // Are You There?
  19. #define EC      247
  20. #define EL      248
  21. #define GA      249     // Go Ahead
  22. #define SB      250
  23. #define WILL    251
  24. #define WONT    252
  25. #define DO      253
  26. #define DONT    254
  27. #define IAC     255     // Interpret As Command
  28.  
  29. // TELNET OPTIONS
  30. #define BINARY      0
  31. #define SGA         3       // Suppress Go Ahead
  32. #define STATUS      5
  33. #define ECHO        1       // Echo
  34. #define TMARK       6
  35. #define TERM_TYPE   24
  36. #define WIN_SIZE    31
  37. #define TERM_SPEED  32
  38. #define FLOW_CTRL   33
  39. #define LINEMODE    34
  40. #define ENV_VARS    36
  41. #define EOR         25      // End Of Record
  42. #define MSDP        69      // MUD Server Data Protocol
  43. #define MSSP        70      // MUD Server Status Protocol
  44. #define MCCP1       85      // MUD Client Compression Protocol (v1)
  45. #define MCCP2       86      // MUD Client Compression Protocol (v2)
  46. #define MSP         90      // MUD Sound Protocol
  47. #define MXP         91      // MUD eXtension Protocol
  48.  
  49. #define IN_BUF_SIZE 1024    // Maximum size of local input buffer
  50. #define OUT_BUF_SIZE 4096   // Maximum size of local buffer for server output
  51.  
  52. class Terminal;
  53.  
  54. /* This struct represents a Telnet command. It is used by NVT to store telnet commands from server in
  55.  * its internal list and to process them when needed */
  56. typedef struct
  57. {
  58.     int command;
  59.     int option;
  60.    
  61. } TelnetCommand;
  62.  
  63. /* This class represents a Network Virtual Terminal. Basically, it's an abstract device which possess
  64.  * a display and a keyboard. Telnet protocol assumes that at each end of telnet connection there is
  65.  * a NVT, which translates transmitted commands and escape characters into real actions on the
  66.  * physical terminal (or terminal emulator). The only requirement is that NVT's virtual display should
  67.  * be able to handle escape characters and it's virtual keyboard must generate all 128 ASCII
  68.  * characters and escape sequences such as CR, BS, VT etc... */
  69. class NVT {
  70. private:
  71.     Socket  *sock;
  72.     char    in_buf[IN_BUF_SIZE];    // local input buffer (virtual keyboard), contains user and telnet commands
  73.     char    out_buf[OUT_BUF_SIZE];  // local output buffer (virtual display), contains raw server output
  74.     int     in_bytes_count;         // total number of bytes in local input buffer
  75.     int     out_bytes_count;        // total number of bytes in local output buffer
  76.    
  77.     boost::mutex    sendMutex;      // Needed by send() and write*Command methods to make them thread-safe
  78.     boost::mutex    recvMutex;      // Needed by receive() method to make it thread safe
  79.    
  80.  
  81. public:
  82.     /* Constructor. Creates a Network Virtual Terminal and connects it to given host */
  83.     NVT(string host, int port);
  84.    
  85.     // Send the local input buffer data to server and clear it. RETURNS: number of sent bytes
  86.     int     send(); // THREAD SAFE
  87.    
  88.     // Receive the response from server and store it into local output buffer. RETURNS: number of received bytes
  89.     int     receive(); // THREAD SAFE
  90.    
  91.     // Write (append) an user command to local input buffer
  92.     void    writeUserCommand(string s); // THREAD SAFE
  93.    
  94.     // Generate a telnet command byte sequence and write it (append) to local input buffer
  95.     void    writeTelnetCommand(int command, int option); // THREAD SAFE
  96.    
  97.     // Extract telnet commands from output buffer and reply to server
  98.     void    processCommands();
  99.    
  100.     // Print the server output to terminal or to stdout (if t is NULL)
  101.     void    print(Terminal *t = NULL);
  102.    
  103.     // Retrieve a const pointer to local output buffer
  104.     const char* getOutBuffer() const;
  105.    
  106.     // Retrieve a const pointer to input buffer
  107.     const char* getInBuffer() const;
  108.    
  109.     // Retrieve a size of output buffer
  110.     int         getOutBufSize() const;
  111.    
  112.     // Retrieve a size of input buffer
  113.     int         getInBufSize() const;
  114.    
  115.     // Get a pointer to NVT's socket object
  116.     Socket*     getSocketPtr();
  117.    
  118.     // Destructor. Closes the remote connection and destroys socket.
  119.     ~NVT();
  120.  
  121. };
  122.  
  123. #endif // NVT_H
RAW Paste Data
Top