static void Main(string[] args) { var pipename = "pipey"; var pipeClient = new NamedPipeClientStream(pipename); Console.WriteLine("Connecting to server pipe '{0}'", pipename); pipeClient.Connect(); var hdr = new PipeHeader(); var hdrSize = Marshal.SizeOf(hdr); hdr.command = 1; hdr.sockid = 1912; hdr.datasize = 32; var buf = Serialize(hdr); Console.WriteLine("Writing to server pipe"); pipeClient.Write(buf, 0, hdrSize); pipeClient.Read(buf, 0, hdrSize); hdr = (PipeHeader)Deserialize(buf, hdr.GetType()); Console.WriteLine("Pipe read {{ command: {0}, sockid: {1}, datasize: {2} }}", hdr.command, hdr.sockid, hdr.datasize); hdr.command = 0; // tell server to disconnect buf = Serialize(hdr); Console.WriteLine("Sending disconnect"); pipeClient.Write(buf, 0, hdrSize); pipeClient.Close(); Console.WriteLine("Pipe closed"); }
public void Dismiss() { if (this.pipeOut != null && this.pipeOut.IsConnected == true) { var header = new PipeHeader(); header.command = Constants.CMD_UNLOAD_DLL; try { WriteHeader(header); } catch { } if (readingThread != null && readingThread.IsAlive) { readingThread.Abort(); } this.pipeOut.Close(); } if (this.pipeIn != null && this.pipeIn.IsConnected == true) { this.pipeIn.Close(); } this.processId = 0; }
public void WriteMessage(string socket, byte[] payload) { PipeHeader message = new PipeHeader { command = Constants.CMD_INJECT, function = Constants.FUNC_SEND, datasize = payload.Length, sockid = int.Parse(socket, System.Globalization.NumberStyles.HexNumber) }; pipeOut.Write(Utilities.RawSerializeEx(message), 0, Marshal.SizeOf(message)); pipeOut.Write(payload, 0, message.datasize); }
/// <summary> /// Helper for sending messages /// </summary> private void SendMessage(IPipeMessage message) { PipeHeader h = PipeHeader.Next(); h.TypeID = message.TypeID; //Send Header #if DEBUG //Console.WriteLine("PipeRepo: Header(" + h.TypeID + ", " + h.DebugNumber + ")"); #endif ProtocolParser.WriteBytes(output, PipeHeader.SerializeToBytes(h)); //Send Message #if DEBUG //Console.WriteLine("PipeRepo: Message(" + message + ")"); #endif byte[] messageBytes = GetMessageBytes(message); ProtocolParser.WriteBytes(output, messageBytes); }
private void WriteHeader(PipeHeader packet) { pipeOut.Write(Utilities.RawSerializeEx(packet), 0, Marshal.SizeOf(packet)); }
private void PipeRead() { byte[] packetHeaderBytes = new byte[14]; while (pipeIn.IsConnected) { while (pipeIn.Read(packetHeaderBytes, 0, 14) != 0) { var packetHeaderObject = (PipeHeader)Utilities.RawDeserializeEx(packetHeaderBytes, typeof(PipeHeader)); if (packetHeaderObject.datasize != 0) { this.socket = packetHeaderObject.sockid.ToString("X4"); var packetData = new byte[packetHeaderObject.datasize]; pipeIn.Read(packetData, 0, packetData.Length); switch (packetHeaderObject.function) { case Constants.FUNC_SEND: Console.WriteLine("Sent: " + packetData.Length); if (this.packetFilterActions.ContainsKey(packetData[3])) { packetFilterActions[packetData[3]](new RecordedPacket { Data = packetData, Header = packetHeaderObject }); } break; case Constants.FUNC_RECV: Console.WriteLine("Received: " + packetData.Length); if (this.packetFilterActions.ContainsKey(packetData[3])) { packetFilterActions[packetData[3]](new RecordedPacket { Data = packetData, Header = packetHeaderObject }); } break; default: Console.WriteLine("Other type of message"); break; } } else { if (packetHeaderObject.command == Constants.CMD_INIT) { if (packetHeaderObject.function == Constants.INIT_DECRYPT) { if (packetHeaderObject.extra == 0) { Console.WriteLine("Failed."); continue; } else { PipeHeader message = new PipeHeader { datasize = 0, command = Constants.CMD_ENABLE_MONITOR }; WriteHeader(message); } } } } } } }
public void ReceiveMessage() { #if DEBUG_SERVER_PROFILING if (nextReport < DateTime.Now) { Console.WriteLine("Waiting: {0}", waiting.TotalSeconds / processing.TotalSeconds); nextReport = DateTime.Now.AddSeconds(1); } DateTime start = DateTime.Now; #endif PipeHeader head; try { head = PipeHeader.Deserialize(ProtocolParser.ReadBytes(input)); } catch (IOException ioe) { throw new EndOfStreamException("At next header", ioe); } #if DEBUG_SERVER_PROFILING if (waiting.Ticks == 0) { waiting = new TimeSpan(1); } else { waiting = waiting + (DateTime.Now - start); } start = DateTime.Now; #endif if (head == null) { Console.Error.WriteLine("Null header"); return; } #if DEBUG Console.WriteLine("Header(" + head.TypeID + ", " + head.DebugNumber + ")"); #endif switch (head.TypeID) { case 1: ProcessGetCustomHash(); break; case 2: ProcessReadChunk(); break; case 3: ProcessWriteChunk(); break; case 4: ProcessMessageList(); break; case 5: ProcessStoreMessage(); break; default: throw new InvalidDataException("Unknown message type"); } output.Flush(); #if DEBUG_SERVER_PROFILING processing = processing + (DateTime.Now - start); #endif }