ParseDataStream( InputByteArray inputArray, SessionSettings sessionSettings, string DataStreamName, TypeTelnetDevice?TypeDevice = null) { var accumCmdList = new WorkstationCommandList(); var responseItemList = new ResponseItemList(); TelnetCommandList telList = null; ResponseHeader curRespHeader = null; ResponseHeader nextRespHeader = null; DataStreamHeader dsh = null; ControlFunctionList funcList = null; bool didParse = true; while ((didParse == true) && (inputArray.RemainingLength > 0)) { didParse = false; curRespHeader = nextRespHeader; nextRespHeader = null; if ((didParse == false) && (dsh == null)) { var telCmd = inputArray.NextTelnetCommand(); if (telCmd != null) { if (telList == null) { telList = new TelnetCommandList(); } telList.Add(telCmd); var s1 = telCmd.ToString(); didParse = true; } } // parse the data stream header. if ((didParse == false) && (dsh == null)) { var code = inputArray.PeekDataStreamCode(); if (code != null) { var rv = DataStreamHeader.Factory(inputArray); dsh = rv.Item1; didParse = true; // update the type of device depending on data stream header stream // code. if ((TypeDevice == null) && (dsh != null) && (dsh.Errmsg == null)) { TypeDevice = dsh.StreamCode.ToTypeTelnetDevice(); } } } // parse as a sequence of workstation commands. if (didParse == false) { if ((TypeDevice == null) || (TypeDevice.Value != TypeTelnetDevice.Printer)) { var rv = Process5250.ParseWorkstationCommandList( inputArray, sessionSettings); var anotherDsh = rv.Item1; var wrkstnCmdList = rv.Item2; if ((anotherDsh != null) && (anotherDsh.Errmsg == null)) { didParse = true; dsh = anotherDsh; } // draw the fields and literals on the canvas. if (wrkstnCmdList?.Count > 0) { accumCmdList.Append(wrkstnCmdList); didParse = true; } } } // parse as sequence of SCS control functions. ( printer data stream ). if (didParse == false) { if ((TypeDevice == null) || (TypeDevice.Value == TypeTelnetDevice.Printer)) { var rv = ControlFunctionList.ParseDataStream(inputArray); if (rv.Item1?.Count > 0) { didParse = true; funcList = rv.Item1; } } } // parse as response stream header. if ((didParse == false) && (ResponseHeader.IsResponseHeader(inputArray).Item1 == true)) { curRespHeader = new ResponseHeader(inputArray); didParse = true; responseItemList.Add(curRespHeader); var rv = Response5250.ParseResponseStream(inputArray, curRespHeader); responseItemList.Append(rv.Item1); } } return(new Tuple <WorkstationCommandList, ResponseItemList, DataStreamHeader, TelnetCommandList, ControlFunctionList>( accumCmdList, responseItemList, dsh, telList, funcList)); }
/// <summary> /// parse the 5250 data stream that is sent from the client to the server. /// </summary> /// <param name="LogFile"></param> /// <param name="ToServerStream"></param> /// <returns></returns> public static Tuple <ResponseItemList, string> ParseResponseStream( InputByteArray InputArray, ResponseHeader ResponseHeader = null) { var responseItemList = new ResponseItemList(); string errmsg = null; var writeStream = new ByteArrayBuilder(); DataStreamHeader dsHeader = null; ResponseHeader responseHeader = ResponseHeader; // stream starts with data stream header. if (responseHeader == null) { var rv = DataStreamHeader.Factory(InputArray); dsHeader = rv.Item1; responseItemList.Add(dsHeader); errmsg = dsHeader.Errmsg; // next is the response header. if (errmsg == null) { responseHeader = new ResponseHeader(InputArray); responseItemList.Add(responseHeader); errmsg = responseHeader.Errmsg; } } // look for 5250 query reply. if ((errmsg == null) && (responseHeader.AidKey != null) && (responseHeader.AidKey.Value == AidKey.Query5250Reply)) { var queryResp = new Query5250Response(InputArray); if (queryResp.Errmsg == null) { responseItemList.Add(queryResp); } } // repeating instances of sbaOrder, textDataOrder pairs. while (true) { var telCmd = TelnetCommand.Factory(InputArray); if (telCmd != null) { continue; } // check that an SBA order is starting. Leave loop when it is not. if (SetBufferAddressOrder.CheckOrder(InputArray) != null) { break; } var orderPair = new LocatedTextDataOrderPair(InputArray); if (orderPair.Errmsg != null) { break; } responseItemList.Add(orderPair); } return(new Tuple <ResponseItemList, string>(responseItemList, errmsg)); }
/// <summary> /// write buffer contents to log file. /// </summary> /// <param name="Direction"></param> /// <param name="Buffer"></param> /// <param name="Length"></param> public void Write(string Direction, byte[] Buffer, int Length) { lock (this.LockFlag) { this.Counter += 1; if (this.Counter > 7) { int cc = 3; } InputByteArray inputArray = new InputByteArray(Buffer, Length); while (inputArray.IsEof() == false) { TelnetStatement stmt = null; var escapeCmd = inputArray.PeekNextByte().ToTelnetCommand(); var dsHeader = DataStreamHeader.Factory(inputArray); IBM5250DataStreamCommand dataStreamCommand; if (dsHeader != null) { var s1 = dsHeader.ToString(); System.IO.File.AppendAllText( FilePath, Direction + " " + this.Counter.ToString() + " " + s1 + Environment.NewLine); this.CurrentDataStreamHeader = dsHeader; continue; } // an ibm 5250 data stream command. else if ((this.CurrentDataStreamHeader != null) && ((dataStreamCommand = IBM5250DataStreamCommand.Factory(inputArray)) != null)) { var s1 = dataStreamCommand.ToString(); System.IO.File.AppendAllText( FilePath, Direction + " " + this.Counter.ToString() + " " + s1 + Environment.NewLine); continue; } else if ((stmt = TelnetStatement.Factory(inputArray)) != null) { var s1 = stmt.ToString(); System.IO.File.AppendAllText( FilePath, Direction + " " + this.Counter.ToString() + " " + s1 + Environment.NewLine); continue; } else if ((escapeCmd != null) && (escapeCmd.Value == TelnetCommand.ESCAPE)) { var rv = TerminalVt100Statement.Factory(inputArray); var vtStmt = rv.Item1; var otStmt = rv.Item2; System.IO.File.AppendAllText( this.FilePath, Direction + " " + this.Counter.ToString() + " " + vtStmt.ToString() + Environment.NewLine); if (otStmt != null) { System.IO.File.AppendAllText( this.FilePath, Direction + " " + this.Counter.ToString() + " " + otStmt.ToString() + Environment.NewLine); } continue; } else { var remBytes = inputArray.GetBytesToEnd(); System.IO.File.AppendAllText( this.FilePath, Direction + " " + this.Counter.ToString() + " Raw bytes follow:" + Environment.NewLine); LogFile_WriteRawBytes(remBytes, this.FilePath); } } } }
/// <summary> /// write buffer contents to log file. /// </summary> /// <param name="Direction"></param> /// <param name="Buffer"></param> /// <param name="Length"></param> public void Write(string Direction, byte[] Buffer, int Length) { var lines = new List <string>(); this.Counter += 1; var inputArray = new InputByteArray(Buffer, Length); while (inputArray.IsEof() == false) { // peek to see if the current byte is a telnet command. var escapeCmd = inputArray.PeekNextByte().ToTelnetCommandCode(); // peek to see if the current bytes are an IBM5250 datastream header. DataStreamHeader dsh = null; string errmsg = null; { var rv = DataStreamHeader.Factory(inputArray); dsh = rv.Item1; errmsg = rv.Item2; } // current bytes are an ibm5250 datastream header. ( page 2-4 of rfc 1205 ) // store this header as the "current datastream header". When there is a // current data stream header the code will match the current bytes against // ibm5250 data stream commands. if (errmsg == null) { lines.Add( Direction + " " + this.Counter.ToString() + " " + dsh.ToString()); this.CurrentDataStreamHeader = dsh; continue; } // currently in a 5250 data stream header. if (this.CurrentDataStreamHeader != null) { var dataStreamCommand = WorkstationCommandBase.ParseFactory(inputArray); if (dataStreamCommand != null) { lines.Add( Direction + " " + this.Counter.ToString() + " " + dataStreamCommand.ToString()); // all the data stream command ToString methods provide enough info. // But for the WriteToDisplayCommand, list out the ToString contents of // the WtdOrders of that command. if (dataStreamCommand is WriteToDisplayCommand) { var wtdCommand = dataStreamCommand as WriteToDisplayCommand; foreach (var order in wtdCommand.OrderList) { lines.Add(order.ToString()); } } continue; } } { var stmt = TelnetCommand.Factory(inputArray); if (stmt != null) { lines.Add( Direction + " " + this.Counter.ToString() + " " + stmt.ToString()); continue; } } if ((escapeCmd != null) && (escapeCmd.Value == CommandCode.ESCAPE)) { var rv = TerminalVt100Statement.Factory(inputArray); var vtStmt = rv.Item1; var otStmt = rv.Item2; lines.Add( Direction + " " + this.Counter.ToString() + " " + vtStmt.ToString()); if (otStmt != null) { lines.Add( Direction + " " + this.Counter.ToString() + " " + otStmt.ToString()); } continue; } { var remBytes = inputArray.GetBytesToEnd(); lines.Add(Direction + " " + this.Counter.ToString() + " Raw bytes follow:"); LogFile_WriteRawBytes(remBytes, lines); } } WriteTextLines(lines); }
ParseWorkstationCommandList( InputByteArray InputArray, SessionSettings SessionSettings) { var wrkstnCmdList = new WorkstationCommandList(); DataStreamHeader dsh = null; bool gotEOR = false; bool needMoreBytes = false; string errmsg = null; if (InputArray.IsDataStreamHeader()) { var rv = DataStreamHeader.Factory(InputArray); dsh = rv.Item1; errmsg = rv.Item2; } bool lastCmdWasTelnet_EOR = false; bool gotWorkstationCommand = true; gotEOR = false; while ((InputArray.IsEof( ) == false) && (gotEOR == false) && (gotWorkstationCommand == true)) { // no input data to process. lastCmdWasTelnet_EOR = false; gotWorkstationCommand = false; // check for IAC EOR var telCode = InputArray.PeekTelnetCommandCode(CommandCode.EOR); if (telCode != null) { var telCmd = InputArray.NextTelnetCommand(); lastCmdWasTelnet_EOR = true; gotEOR = true; } // process the input as workstation data stream commands. else { var rv = ParseAndProcessWorkstationCommand(InputArray); var workstationCommand = rv.Item1; var howRead = rv.Item2; if (workstationCommand != null) { wrkstnCmdList.Add(workstationCommand); gotWorkstationCommand = true; } } } // read available bytes from input stream. if (InputArray.IsEof() && (lastCmdWasTelnet_EOR == false)) { needMoreBytes = true; } return(new Tuple <DataStreamHeader, WorkstationCommandList, bool, bool>( dsh, wrkstnCmdList, gotEOR, needMoreBytes)); }