public void TestTuringSocket() { using (TuringSocket server = TuringSocket.Bind(new IPEndPoint(IPAddress.Any, 9787))) { server.OnMessage += Server_OnMessage; using (TuringSocket client = TuringSocket.ConnectTo(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9787))) { client.OnMessage += Client_OnMessage; client.SendMessage(new OpenStreamMessageRequest() { }); for (int x = 0; x < 20 && !isOk; x++) { Thread.Sleep(1000); } } } if (!isOk) { throw (new System.Exception()); } }
public override void Close() { if (_Base != null) { _Base.Close(); _Base.Dispose(); _Base = null; } _Socket.SendMessage(new CloseStreamMessageRequest(_StreamId)); BoolMessageResponse ret = _Socket.ReadMessage <BoolMessageResponse>(); if (!ret.Result) { throw (new Exception("Error clossing stream")); } }
/// <summary> /// Constructor /// </summary> /// <param name="socket">Socket</param> /// <param name="stream">Stream</param> public TuringStream(TuringSocket socket, Stream stream) { _Base = stream; _Socket = socket; socket.EnqueueMessages = true; socket.SendMessage(new OpenStreamMessageRequest()); OpenStreamMessageResponse ret = socket.ReadMessage <OpenStreamMessageResponse>(); _StreamId = ret.Id; _CanRead = ret.CanRead; _CanSeek = ret.CanSeek; _CanTimeout = ret.CanTimeout; _CanWrite = ret.CanWrite; }
void Server_OnMessage(TuringSocket sender, TuringMessage message) { sender.SendMessage(new CloseStreamMessageRequest() { }); }
/// <summary> /// Message logic /// </summary> /// <param name="sender">Socket</param> /// <param name="message">Message</param> void _Socket_OnMessage(TuringSocket sender, TuringMessage message) { if (_Paused) { // Send Paused signal //sender.SendMessage(new WaitMessage(TimeSpan.FromSeconds(1))); // Wait disable pause while (_Paused) { Thread.Sleep(500); } } TuringMessage response = new ExceptionMessage("Bad request"); try { switch (message.Type) { case ETuringMessageType.EndTask: { EndTaskMessage msg = (EndTaskMessage)message; List <FuzzerStat <IFuzzingInput> > sinput = null; List <FuzzerStat <IFuzzingConfig> > sconfig = null; // Log if are data FuzzerLog log = msg.SaveResult(sender, out sinput, out sconfig); if (log != null) { RaiseOnCrashLog(log); } // Send message of the end RaiseOnTestEnd(msg.Result, sinput == null ? null : sinput.ToArray(), sconfig == null ? null : sconfig.ToArray()); response = new BoolMessageResponse(true); break; } case ETuringMessageType.OpenStreamRequest: { Guid id = Guid.NewGuid(); OpenStreamMessageRequest msg = (OpenStreamMessageRequest)message; FuzzingStream stream = GetRandomStream(msg, sender, true, id); if (stream == null) { response = new ExceptionMessage("Not found stream"); break; } sender[id.ToString()] = stream; //if (msg.UseMemoryStream) // response = new OpenStreamMessageResponse(id) // { // CanRead = msg.CanRead, // CanSeek = msg.CanSeek, // CanTimeout = msg.CanTimeout, // CanWrite = msg.CanWrite // }; //else response = new OpenStreamMessageResponse(id) { CanRead = stream.CanRead, CanSeek = stream.CanSeek, CanTimeout = stream.CanTimeout, CanWrite = stream.CanWrite }; break; } case ETuringMessageType.GetStreamLengthRequest: { GetStreamLengthMessageRequest msg = (GetStreamLengthMessageRequest)message; FuzzingStream stream = (FuzzingStream)sender[msg.Id.ToString()]; if (stream == null) { response = new ExceptionMessage("No stream openned with id: " + msg.Id.ToString()); break; } response = new LongMessageResponse(stream.Length); break; } case ETuringMessageType.GetStreamPositionRequest: { GetStreamPositionMessageRequest msg = (GetStreamPositionMessageRequest)message; FuzzingStream stream = (FuzzingStream)sender[msg.Id.ToString()]; if (stream == null) { response = new ExceptionMessage("No stream openned with id: " + msg.Id.ToString()); break; } response = new LongMessageResponse(stream.Position); break; } case ETuringMessageType.SetStreamRequest: { SetStreamMessageRequest msg = (SetStreamMessageRequest)message; FuzzingStream stream = (FuzzingStream)sender[msg.Id.ToString()]; if (stream == null) { response = new ExceptionMessage("No stream openned with id: " + msg.Id.ToString()); break; } switch (msg.ValueType) { case SetStreamMessageRequest.EMode.Position: stream.Position = msg.Value; break; case SetStreamMessageRequest.EMode.Length: stream.SetLength(msg.Value); break; } response = new BoolMessageResponse(true); break; } case ETuringMessageType.FlushStreamRequest: { FlushStreamMessageRequest msg = (FlushStreamMessageRequest)message; FuzzingStream stream = (FuzzingStream)sender[msg.Id.ToString()]; if (stream == null) { response = new ExceptionMessage("No stream openned with id: " + msg.Id.ToString()); break; } stream.Flush(); response = new BoolMessageResponse(true); break; } case ETuringMessageType.CloseStreamRequest: { CloseStreamMessageRequest msg = (CloseStreamMessageRequest)message; FuzzingStream stream = (FuzzingStream)sender[msg.Id.ToString()]; if (stream == null) { response = new ExceptionMessage("No stream openned with id: " + msg.Id.ToString()); break; } // Save patch for dump sender[msg.Id.ToString()] = null; sender["Info=" + msg.Id.ToString()] = new FuzzingLogInfo(stream); try { stream.Close(); } catch { } try { stream.Dispose(); } catch { } response = new BoolMessageResponse(true); break; } case ETuringMessageType.ReadStreamRequest: { StreamReadMessageRequest msg = (StreamReadMessageRequest)message; FuzzingStream stream = (FuzzingStream)sender[msg.Id.ToString()]; if (stream == null) { response = new ExceptionMessage("No stream openned with id: " + msg.Id.ToString()); break; } if (msg.PreAppend != null) { stream.AppendToSource(msg.PreAppend, 0, msg.PreAppend.Length, msg.PreAppendReSeek); } byte[] data = new byte[msg.Length]; int r = stream.Read(data, 0, data.Length); if (r != data.Length) { Array.Resize(ref data, r); } response = new ByteArrayMessageResponse(data); break; } case ETuringMessageType.WriteStreamRequest: { StreamWriteMessageRequest msg = (StreamWriteMessageRequest)message; FuzzingStream stream = (FuzzingStream)sender[msg.Id.ToString()]; if (stream == null) { response = new ExceptionMessage("No stream openned with id: " + msg.Id.ToString()); break; } stream.Write(msg.Data, 0, msg.Data.Length); response = new BoolMessageResponse(true); break; } } } catch (Exception e) { response = new ExceptionMessage(e.ToString()); } sender.SendMessage(response); }