private bool SendToWireshark(byte[] buffer, int offset, int lenght, uint seconds, uint microseconds) { if (!isConnected) { return(false); } var header = new PcapPacketHeader((uint)lenght, seconds, microseconds); var headerBytes = header.ToByteArray(); try { // Wireshark Header wiresharkPipe.Write(headerBytes, 0, headerBytes.Length); // Bacnet packet wiresharkPipe.Write(buffer, offset, lenght); } catch (Exception) { // We should probably handle IOException to somehow restart the pipe. // It is difficult to test, though, and Wireshark may not survive it. return(false); } return(true); }
private bool SendToWireshark(byte[] buffer, int offset, int lenght, UInt32 date_sec, UInt32 date_usec) { if (!isConnected) { return(false); } var pHdr = new PcapPacketHeader((UInt32)lenght, date_sec, date_usec); var b = pHdr.ToByteArray(); try { // Wireshark Header wiresharkPipe.Write(b, 0, b.Length); // Bacnet packet wiresharkPipe.Write(buffer, offset, lenght); } catch (IOException) { return(false); } catch (Exception) { // Unknow error, not due to the pipe // No need to restart it return(false); } return(true); }
private void OnPacketCaptured(IntPtr iptrParams, PcapPacketHeader pHeader, IntPtr pPacketData) { if (BytesCaptured != null) { DateTime dtCaptureDate = DateTime.Now; byte[] bBuffer = new byte[pHeader.PacketLength]; Marshal.Copy(pPacketData, bBuffer, 0, (int)pHeader.PacketLength); WinPcapCaptureHeader wpcHeader = new WinPcapCaptureHeader(dtCaptureDate, (int)pHeader.CaptureLength, (int)pHeader.PacketLength); if (BytesCaptured != null) { foreach (Delegate dDelgate in BytesCaptured.GetInvocationList()) { if (dDelgate.Target != null && dDelgate.Target is EthernetInterface) { ((EthernetInterface)dDelgate.Target).OnBytesCaptured(wpcHeader, bBuffer, this); } else if (dDelgate.Target != null && dDelgate.Target is System.ComponentModel.ISynchronizeInvoke && ((System.ComponentModel.ISynchronizeInvoke)(dDelgate.Target)).InvokeRequired) { ((System.ComponentModel.ISynchronizeInvoke)(dDelgate.Target)).BeginInvoke(dDelgate, new object[] { wpcHeader, bBuffer, this }); } else { ((ByteCapturedHandler)dDelgate)(wpcHeader, bBuffer, this); } } } } }
public void Get() { var pcapPacketHeader = new PcapPacketHeader { IsLittleEndian = true, Bytes = new Byte[] { 0xBE, 0x8B, 0x55, 0x5C, 0xD7, 0x50, 0x0C, 0x00, 0x4A, 0x00, 0x00, 0x00, 0x4A, 0x00, 0x00, 0x00, } }; pcapPacketHeader.IsLittleEndian.Should().Be(true); pcapPacketHeader.TimestampSecondPart.Should().Be(1549110206); pcapPacketHeader.TimestampMicrosecondPart.Should().Be(807127); pcapPacketHeader.CapturedLength.Should().Be(74); pcapPacketHeader.OriginalLength.Should().Be(74); }