-
-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathQTelnet.h
More file actions
159 lines (136 loc) · 5.49 KB
/
QTelnet.h
File metadata and controls
159 lines (136 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#ifndef QTELNET_H
#define QTELNET_H
#include <QObject>
#include <QTcpSocket>
#include <QWebSocket>
#include <QSize>
#include <QString>
#define IncommingBufferSize (1500)
class QTelnet : public QObject
{
Q_OBJECT
public:
enum SocketStatus
{
Disconnected,
Resolving, // Resolving host
Connecting, // Connecting to host.
Connected // Connected to host.
};
enum SocketType
{
TCP = 0,
WEBSOCKET,
SECUREWEBSOCKET
};
protected:
enum TelnetStateCodes
{
STATE_DATA = (char)0,
STATE_IAC = (char)1,
STATE_IACSB = (char)2,
STATE_IACWILL = (char)3,
STATE_IACDO = (char)4,
STATE_IACWONT = (char)5,
STATE_IACDONT = (char)6,
STATE_IACSBIAC = (char)7,
STATE_IACSBDATA = (char)8,
STATE_IACSBDATAIAC = (char)9,
STATE_DATAR = (char)10,
STATE_DATAN = (char)11
};
enum TelnetCodes
{
// Negociación entrada/salida (client<->servidor)
IAC = (char)255, // Inicia la secuencia para la negociación telnet.
EOR = (char)239, // Estando en la negociación, End Of Record.
WILL = (char)251, // Estando en la negociación, Acepta el protocolo?
WONT = (char)252, // Estando en la negociación, Acepta el protocolo?
DO = (char)253, // Estando en la negociación, Protocolo aceptado.
DONT = (char)254, // Estando en la negociación, Protocolo denegado.
SB = (char)250, // Estando en la negociación, inicia secuencia de sub-negociación.
SE = (char)240, // Estando en la sub-negociación, fin de sub-negociación.
// Negociación de salida (client->servidor)
TELOPT_BINARY = (char)0, // Estando en la negociación, pide modo binario.
TELOPT_ECHO = (char)1, // Estando en la negociación, pide echo local.
TELOPT_SGA = (char)2, // Estando en la negociación, pide Suppress Go Ahead.
TELOPT_EOR = (char)25, // Estando en la negociación, informa End Of Record.
TELOPT_NAWS = (char)31, // Estando en la negociación, Negotiate Abaut Window Size.
TELOPT_TTYPE = (char)24 // Estando en la negociación, Terminal Type.
};
enum TelnetQualifiers
{
TELQUAL_IS = (char)0,
TELQUAL_SEND = (char)1
};
private:
QTcpSocket m_tcpSocket;
QWebSocket m_webSocket;
SocketType m_socketType;
static const char IACWILL[2];
static const char IACWONT[2];
static const char IACDO[2];
static const char IACDONT[2];
static const char IACSB[2];
static const char IACSE[2];
static char _sendCodeArray[2];
static char _arrCRLF[2];
static char _arrCR[2];
QSize m_winSize; // Tamaño de la pantalla en caracteres.
QSize m_oldWinSize; // Tamaño de la pantalla que se envió por última vez al server. Para no enviar el mismo dato.
enum TelnetStateCodes m_negotiationState;
char m_receivedDX[256]; // What IAC DO(NT) request do we have received already ?
char m_receivedWX[256]; // What IAC WILL/WONT request do we have received already ?
char m_sentDX[256]; // What IAC DO/DONT request do we have sent already ?
char m_sentWX[256]; // What IAC WILL/WONT request do we have sent already ?
void resetProtocol();
char m_buffIncoming[IncommingBufferSize];
char m_buffProcessed[IncommingBufferSize];
QByteArray m_buffSB;
int m_actualSB;
void emitEndOfRecord() { emit endOfRecord(); }
void emitEchoLocal(bool bEcho) { emit echoLocal(bEcho); }
void sendTelnetControl(char codigo);
void handleSB(void);
void transpose(const char *buf, int iLen);
void willsReply(char action, char reply);
void wontsReply(char action, char reply);
void doesReply(char action, char reply);
void dontsReply(char action, char reply);
void sendSB(char code, char *arr, int iLen);
qint64 doTelnetInProtocol(qint64 buffSize);
public:
explicit QTelnet(SocketType type = TCP, QObject *parent = 0);
explicit QTelnet(QObject *parent = 0) : QTelnet(TCP, parent) {}
void setType(SocketType type);
void connectToHost(const QString &hostName, quint16 port);
void disconnectFromHost(void);
void sendData(const QByteArray &ba);
void sendData(const char *data, int len);
void setCustomCRLF(char lf = 13, char cr = 10);
void setCustomCR(char cr = 10, char cr2 = 0);
void writeCustomCRLF();
void writeCustomCR();
void write(const char c);
qint64 write(const char *data, qint64 len);
qint64 read(char *data, qint64 maxlen);
bool isConnected() const;
bool testBinaryMode() const;
void setWindSize(QSize s) {m_winSize = s;}
void sendWindowSize();
QString peerInfo()const;
QString peerName()const;
QString errorString();
signals:
void newData(const char *buff, int len);
void endOfRecord();
void echoLocal(bool echo);
void stateChanged(QAbstractSocket::SocketState s);
void error(QAbstractSocket::SocketError err);
private slots:
void socketError(QAbstractSocket::SocketError err);
void onTcpReadyRead();
void binaryMessageReceived(const QByteArray &message);
void onStateChanged(QAbstractSocket::SocketState s);
};
#endif // QTELNET_H