summaryrefslogtreecommitdiff
path: root/storage/ndb/src/cw/cpcc-win32/csharp/telnetclient/telnetClient.cs
blob: 81b6375cad9d9973cc606aaca3f6196b8e2c1216 (plain)
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/* Copyright (C) 2004 MySQL AB

   This program 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; version 2 of the License.

   This program 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 program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA */

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.IO;
using System.Threading ;

namespace NDB_CPC.telnetclient
{
	/// <summary>
	/// Summary description for telnetClient.
	/// </summary>
	public class telnetClient
	{
		Char IAC				= Convert.ToChar(255);
		Char DO					= Convert.ToChar(253);
		Char DONT				= Convert.ToChar(254);
		Char WILL				= Convert.ToChar(251);
		Char WONT				= Convert.ToChar(252);
		Char SB					= Convert.ToChar(250);
		Char SE					= Convert.ToChar(240);
		const	Char IS			= '0';
		const	Char SEND		= '1';
		const	Char INFO		= '2';
		const	Char VAR		= '0';
		const	Char VALUE		= '1';
		const	Char ESC		= '2';
		const	Char USERVAR	= '3';
		string m_strResp;

		private ArrayList  m_ListOptions = new ArrayList();
		private IPEndPoint iep ;
		private AsyncCallback callbackProc ;
		private string address ;
		private int port ;
		private Socket s ;
		private TextBox textBox1;
		Byte[] m_byBuff = new Byte[32767];


		public telnetClient(string ip, int p, TextBox tb)
		{

			address = ip;
			port = p;
			textBox1=tb;
			IPHostEntry IPHost = Dns.Resolve(address); 
			string []aliases = IPHost.Aliases; 
			IPAddress[] addr = IPHost.AddressList; 
		
			try
			{
				// Create New Socket 
				s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
				// Create New EndPoint
				iep	= new IPEndPoint(addr[0],port);  
				// This is a non blocking IO
				s.Blocking		= false ;	
				// Assign Callback function to read from Asyncronous Socket
				callbackProc	= new AsyncCallback(ConnectCallback);
				// Begin Asyncronous Connection
				s.BeginConnect(iep , callbackProc, s ) ;				
		
			}
			catch(Exception eeeee )
			{
				MessageBox.Show(eeeee.Message , "Application Error!!!" , MessageBoxButtons.OK , MessageBoxIcon.Stop );
				Application.Exit();
			}
		}

		public void ConnectCallback( IAsyncResult ar )
		{
			try
			{
				// Get The connection socket from the callback
				Socket sock1 = (Socket)ar.AsyncState;
				if ( sock1.Connected ) 
				{	
					// Define a new Callback to read the data 
					AsyncCallback recieveData = new AsyncCallback( OnRecievedData );
					// Begin reading data asyncronously
					sock1.BeginReceive( m_byBuff, 0, m_byBuff.Length, SocketFlags.None, recieveData , sock1 );
				}
			}
			catch( Exception ex )
			{
				MessageBox.Show(ex.Message, "Setup Recieve callbackProc failed!" );
			}
		}

		
		public void OnRecievedData( IAsyncResult ar )
		{
			// Get The connection socket from the callback
			Socket sock = (Socket)ar.AsyncState;
				// Get The data , if any
				int nBytesRec = sock.EndReceive( ar );		
				if( nBytesRec > 0 )
				{
					string sRecieved = Encoding.ASCII.GetString( m_byBuff, 0, nBytesRec );
					string m_strLine="";
					for ( int i=0; i < nBytesRec;i++)
					{
						Char ch = Convert.ToChar(m_byBuff[i]);
						switch( ch )
						{
							case '\r': 
								m_strLine += Convert.ToString("\r\n"); 
								break;
							case '\n': 
								break;
							default: 
								m_strLine += Convert.ToString(ch);
								break;
						} 
					}
					try
					{
						int strLinelen = m_strLine.Length ;
						if ( strLinelen == 0 ) 
						{
							m_strLine = Convert.ToString("\r\n");
						}

						Byte[] mToProcess = new Byte[strLinelen];
						for ( int i=0; i <  strLinelen ; i++)
							mToProcess[i] = Convert.ToByte(m_strLine[i]);
						// Process the incoming data
						string mOutText = ProcessOptions(mToProcess);
						if ( mOutText != "" ) 
							textBox1.AppendText(mOutText);
				
						// Respond to any incoming commands
						RespondToOptions();
					}
					catch( Exception ex )
					{
						Object x = this ;
						MessageBox.Show(ex.Message , "Information!" );
					}
				}
				else
				{
					// If no data was recieved then the connection is probably dead
					Console.WriteLine( "Disconnected", sock.RemoteEndPoint );
					sock.Shutdown( SocketShutdown.Both );
					sock.Close();
					Application.Exit();
				}
		}
	
		private string ProcessOptions(byte[] m_strLineToProcess)
		{
			string m_DISPLAYTEXT	="";
			string m_strTemp		="" ;
			string m_strOption		="";
			string m_strNormalText	="";
			bool bScanDone			=false;
			int ndx					=0;
			int ldx					=0;
			char ch	;
			try
			{
				for ( int i=0; i < m_strLineToProcess.Length ; i++)
				{
					Char ss = Convert.ToChar(m_strLineToProcess[i]);
					m_strTemp = m_strTemp + Convert.ToString(ss);
				}

				while(bScanDone != true )
				{
					int lensmk = m_strTemp.Length;
					ndx = m_strTemp.IndexOf(Convert.ToString(IAC));
					if ( ndx > lensmk )
						ndx = m_strTemp.Length;

					if(ndx != -1)
					{
						m_DISPLAYTEXT+= m_strTemp.Substring(0,ndx);
						ch = m_strTemp[ndx + 1];
						if ( ch == DO || ch == DONT || ch == WILL || ch == WONT ) 
						{
							m_strOption		= m_strTemp.Substring(ndx, 3);
							string txt		= m_strTemp.Substring(ndx + 3);
							m_DISPLAYTEXT+= m_strTemp.Substring(0,ndx);
							m_ListOptions.Add(m_strOption);
							m_strTemp		= txt ;
						}
						else
							if ( ch == IAC)
						{
							m_DISPLAYTEXT= m_strTemp.Substring(0,ndx);
							m_strTemp		= m_strTemp.Substring(ndx + 1);
						}
						else
							if ( ch == SB ) 
						{
							m_DISPLAYTEXT= m_strTemp.Substring(0,ndx);
							ldx = m_strTemp.IndexOf(Convert.ToString(SE));
							m_strOption		= m_strTemp.Substring(ndx, ldx);
							m_ListOptions.Add(m_strOption);
							m_strTemp			= m_strTemp.Substring(ldx);
						}
					}
					else
					{
						m_DISPLAYTEXT = m_DISPLAYTEXT + m_strTemp;
						bScanDone = true ;
					}
				} 
				m_strNormalText = m_DISPLAYTEXT; 	
			}
			catch(Exception eP)
			{
				MessageBox.Show(eP.Message , "Application Error!!!" , MessageBoxButtons.OK , MessageBoxIcon.Stop );
				Application.Exit();
			}
			return m_strNormalText ;
		}
	
		void DispatchMessage(string strText)
		{
			try
			{
				Byte[] smk = new Byte[strText.Length];
				for ( int i=0; i < strText.Length ; i++)
				{
					Byte ss = Convert.ToByte(strText[i]);
					smk[i] = ss ;
				}

				IAsyncResult ar2 = s.BeginSend(smk , 0 , smk.Length , SocketFlags.None , callbackProc , s );
				s.EndSend(ar2);
			}
			catch(Exception ers)
			{
				MessageBox.Show("ERROR IN RESPOND OPTIONS");
			}
		}

		void RespondToOptions()
		{
			try
			{
				string strOption;
				for ( int i=0; i < m_ListOptions.Count; i++)
				{
					strOption = (string)m_ListOptions[i];
					ArrangeReply(strOption);
				}
				DispatchMessage(m_strResp);
				m_strResp ="";
				m_ListOptions.Clear();
			}
			catch(Exception ers)
			{
				MessageBox.Show("ERROR IN RESPOND OPTIONS");
			}
		}
		void ArrangeReply(string strOption)
		{
			try
			{

				Char Verb;
				Char Option;
				Char Modifier;
				Char ch;
				bool bDefined = false;

				if(strOption.Length  < 3) return;

				Verb = strOption[1];
				Option = strOption[2];

				if ( Option == 1 || Option == 3 ) 
				{
					//				case 1:	// Echo
					//				case 3: // Suppress Go-Ahead
					bDefined = true;
					//					break;
				}

				m_strResp += IAC;

				if(bDefined == true )
				{
					if ( Verb == DO ) 
					{
						//					case DO:
						ch = WILL;
						m_strResp += ch;
						m_strResp += Option;
						//						break;
					}
					if ( Verb == DONT ) 
					{
						ch = WONT;
						m_strResp += ch;
						m_strResp += Option;
						//		break;
					}
					if ( Verb == WILL ) 
					{
						ch = DO;
						m_strResp += ch;
						m_strResp += Option;
						//break;
					}
					if ( Verb == WONT)
					{
						ch = DONT;
						m_strResp += ch;
						m_strResp += Option;
						//	break;
					}
					if ( Verb == SB)
					{
						Modifier = strOption[3];
						if(Modifier == SEND)
						{
							ch = SB;
							m_strResp += ch;
							m_strResp += Option;
							m_strResp += IS;
							m_strResp += IAC;
							m_strResp += SE;
						}
						//						break;
					}
				}
				else
				{
					//				switch(Verb)
					//				{
					if ( Verb == DO ) 
					{
						ch = WONT;
						m_strResp += ch;
						m_strResp += Option;
						//	break;
					}
					if ( Verb == DONT)
					{
						ch = WONT;
						m_strResp += ch;
						m_strResp += Option;
						//	break;
					}
					if ( Verb == WILL)
					{
						ch = DONT;
						m_strResp += ch;
						m_strResp += Option;
						//	break;
					}
					if ( Verb == WONT)
					{
						ch = DONT;
						m_strResp += ch;
						m_strResp += Option;
						//		break;
					}
				}
			}
			catch(Exception eeeee )
			{
				MessageBox.Show(eeeee.Message , "Application Error!!!" , MessageBoxButtons.OK , MessageBoxIcon.Stop );
				Application.Exit();
			}

		}

		private void textBox1_KeyPress_1(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			if ( e.KeyChar == 13 ) 
			{
				DispatchMessage("\r\n");
			}
			else
			if ( e.KeyChar == 8 )
			{
				try
				{
//					string mtmp = textBox1.Text.Substring(0,textBox1.Text.Length-1);
//					textBox1.Text = "" ;		 
				}
				catch(Exception ebs)
				{
					MessageBox.Show("ERROR IN BACKSPACE");
				}
			}
			else
			{
				string str = e.KeyChar.ToString();
				DispatchMessage(str);
			}
		}


	}
}