-
Notifications
You must be signed in to change notification settings - Fork 1
/
CTelnetClient.h
241 lines (206 loc) · 6.75 KB
/
CTelnetClient.h
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/***********************************************************
WinTN3270
Copyright © 2007 Bob Carroll ([email protected])
This software is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation;
either version 2, or (at your option) any later version.
This software is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public
License along with this software; if not, write to the
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Boston, MA 02110-1301 USA
***********************************************************/
#pragma once
#include "TelnetProtocol.h"
namespace WinTN3270
{
/***********************************************************
Client for communicating with a remote TELNET server.
***********************************************************/
public ref class CTelnetClient
{
public: /* Public Delegates */
delegate bool OnCertPolicyError(
System::Security::Cryptography::X509Certificates::X509Certificate^ mpCertificate,
System::Security::Cryptography::X509Certificates::X509Chain^ mpChain,
System::Net::Security::SslPolicyErrors ePolicyErrors);
delegate void OnReceiveASCII(System::String^ cchData);
delegate void OnReceiveBinary(cli::array<System::Byte>^ mpData);
delegate void OnSend();
private: /* Private Member Attributes */
System::String^ m_cchRemoteHost;
bool m_fGoAhead;
OnCertPolicyError^ m_mpCertErrorCbk;
System::Collections::Hashtable^ m_mpClientOpts;
System::Threading::EventWaitHandle^ m_mpSendEvent;
cli::array<System::Byte>^ m_mpRcvCommandBuffer;
cli::array<System::Byte>^ m_mpRcvDataBuffer;
OnReceiveASCII^ m_mpReceiveAscCbk;
OnReceiveBinary^ m_mpReceiveBinCbk;
System::Net::IPEndPoint^ m_mpRemoteIP;
OnSend^ m_mpSendCbk;
System::Collections::Hashtable^ m_mpServerOpts;
System::Net::Sockets::Socket^ m_mpSocket;
System::IO::Stream^ m_mpSocketStream;
TNSUBOPTION^ m_mpSubOpt;
cli::array<System::Byte>^ m_mpTransmQueue;
public: /* Public Properties */
property bool BinaryTransmission
{
bool get()
{
return m_mpClientOpts->ContainsKey(safe_cast<int^>(TELNET_OPTION_BINARY));
}
void set(bool fValue)
{
if (!fValue &&
m_mpClientOpts->ContainsKey(safe_cast<int^>(TELNET_OPTION_BINARY)))
{
m_mpClientOpts->Remove(safe_cast<int^>(TELNET_OPTION_BINARY));
}
m_mpClientOpts[safe_cast<int^>(TELNET_OPTION_BINARY)] = true;
}
}
property OnCertPolicyError^ CertificateErrorCallback
{
OnCertPolicyError^ get()
{
System::Threading::Monitor::Enter(this);
OnCertPolicyError^ mpCertErrorCbk = m_mpCertErrorCbk;
System::Threading::Monitor::Exit(this);
return mpCertErrorCbk;
}
void set(OnCertPolicyError^ mpValue)
{
System::Threading::Monitor::Enter(this);
m_mpCertErrorCbk = mpValue;
System::Threading::Monitor::Exit(this);
}
}
property bool Connected
{
bool get() { return m_mpSocket->Connected; }
}
property bool EndOfRecord
{
bool get()
{
return m_mpClientOpts->ContainsKey(TELNET_OPTION_END_OF_RECORD);
}
void set(bool fValue)
{
if (!fValue &&
m_mpClientOpts->ContainsKey(TELNET_OPTION_END_OF_RECORD))
{
m_mpClientOpts->Remove(TELNET_OPTION_END_OF_RECORD);
return;
}
m_mpClientOpts[TELNET_OPTION_END_OF_RECORD] = true;
}
}
property bool FullDuplex
{
bool get()
{
return m_mpClientOpts->ContainsKey(TELNET_OPTION_SUPPRESS_GA);
}
void set(bool fValue)
{
if (!fValue &&
m_mpClientOpts->ContainsKey(TELNET_OPTION_SUPPRESS_GA))
{
m_mpClientOpts->Remove(TELNET_OPTION_SUPPRESS_GA);
return;
}
m_mpClientOpts[TELNET_OPTION_SUPPRESS_GA] = true;
}
}
property OnReceiveASCII^ ReceiveCallbackASCII
{
OnReceiveASCII^ get() { return m_mpReceiveAscCbk; }
void set(OnReceiveASCII^ mpValue)
{
m_mpReceiveAscCbk = mpValue;
}
}
property OnReceiveBinary^ ReceiveCallbackBinary
{
OnReceiveBinary^ get() { return m_mpReceiveBinCbk; }
void set(OnReceiveBinary^ mpValue)
{
m_mpReceiveBinCbk = mpValue;
}
}
property OnSend^ SendCallback
{
OnSend^ get() { return m_mpSendCbk; }
void set(OnSend^ mpValue)
{
m_mpSendCbk = mpValue;
}
}
property cli::array<System::Byte>^ TerminalType
{
cli::array<System::Byte>^ get()
{
if (!m_mpClientOpts->ContainsKey(TELNET_OPTION_TERM_TYPE))
return nullptr;
return (cli::array<System::Byte>^)m_mpClientOpts[TELNET_OPTION_TERM_TYPE];
}
void set(cli::array<System::Byte>^ mpValue)
{
if (mpValue == nullptr &&
m_mpClientOpts->ContainsKey(TELNET_OPTION_TERM_TYPE))
{
m_mpClientOpts->Remove(TELNET_OPTION_TERM_TYPE);
return;
}
m_mpClientOpts[TELNET_OPTION_TERM_TYPE] = mpValue;
}
}
private: /* Private Member Functions */
bool CertValidationCallback( System::Object^ mpSender,
System::Security::Cryptography::X509Certificates::X509Certificate^ mpCertificate,
System::Security::Cryptography::X509Certificates::X509Chain^ mpChain,
System::Net::Security::SslPolicyErrors ePolicyErrors);
void FlushTransmitQueue();
cli::array<System::Byte>^ GetClientOption(int nId);
cli::array<System::Byte>^ InterpretCommands(cli::array<System::Byte>^ mpData);
static void ReceiveProc(System::IAsyncResult^ mpResult);
static void SendProc(System::IAsyncResult^ mpResult);
bool SetServerOption(int nId, bool fValue);
bool SetServerOption(int nId, cli::array<System::Byte>^ mpValue);
void Transmit(cli::array<System::Byte>^ mpData);
public: /* Public Member Functions */
CTelnetClient(System::String^ cchIPAddr, int nPort);
~CTelnetClient();
void Connect();
void Connect(bool fSecure);
void Disconnect();
cli::array<System::Byte>^ GetServerOption(int nId);
void Send(System::String^ cchData);
void Send(cli::array<System::Byte>^ mpData);
bool TestServerOption(int nId);
};
/* Socket Receive State */
ref struct SOCKETRCVSTATE
{
cli::array<System::Byte>^ mpBuffer;
CTelnetClient::OnReceiveASCII^ mpAscCallback;
CTelnetClient::OnReceiveBinary^ mpBinCallback;
CTelnetClient^ mpClient;
System::IO::Stream^ mpStream;
};
/* Socket Send State */
ref struct SOCKETSNDSTATE
{
CTelnetClient::OnSend^ mpCallback;
CTelnetClient^ mpClient;
System::IO::Stream^ mpStream;
};
}