private void Device_OnPacketArrival(object sender, CaptureEventArgs e) { try { var tcpPacket = TcpPacket.ParsePacket(e.Packet.LinkLayerType, e.Packet.Data); if (tcpPacket != null) { DateTime time = e.Packet.Timeval.Date; int len = e.Packet.Data.Length; var srcIp = tcpPacket.PayloadPacket.ToString(); var data = tcpPacket.PayloadPacket.Bytes; var ipSource = srcIp.Split(new string[] { "SourceAddress=" }, StringSplitOptions.RemoveEmptyEntries)[1].Split(new string[] { ", DestinationAddress=" }, StringSplitOptions.RemoveEmptyEntries)[0]; var ipDest = srcIp.Split(new string[] { ", DestinationAddress=" }, StringSplitOptions.RemoveEmptyEntries)[1].Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries)[0]; Dump.Packages.Add(new Package() { DateTime = time, Size = len, Protocol = Protocol.TCP, From = ipSource, To = ipDest, Message = data, Id = I }); } I++; } catch { } }
private void mTimer_Tick(object sender, EventArgs e) { try { RawCapture packet = null; mTimer.Enabled = false; DateTime now = DateTime.Now; foreach (SessionForm ses in MdiChildren) { if (ses.CloseMe(now)) { closes.Add(ses); } } closes.ForEach((a) => { a.Close(); }); closes.Clear(); while ((packet = mDevice.GetNextPacket()) != null) { if (!started) { continue; } TcpPacket tcpPacket = (TcpPacket)TcpPacket.ParsePacket(packet.LinkLayerType, packet.Data).Extract <TcpPacket>(); SessionForm session = null; try { if (tcpPacket.Synchronize && !tcpPacket.Acknowledgment && tcpPacket.DestinationPort >= Config.Instance.LowPort && tcpPacket.DestinationPort <= Config.Instance.HighPort) { session = NewSession(); var res = session.BufferTCPPacket(tcpPacket, packet.Timeval.Date); if (res == SessionForm.Results.Continue) { int hash = tcpPacket.SourcePort << 16 | tcpPacket.DestinationPort; waiting[hash] = session; } } else { int hash = tcpPacket.DestinationPort << 16 | tcpPacket.SourcePort; session = Array.Find(MdiChildren, f => (f as SessionForm).MatchTCPPacket(tcpPacket)) as SessionForm; if (session != null) { var res = session.BufferTCPPacket(tcpPacket, packet.Timeval.Date); if (res == SessionForm.Results.CloseMe) { waiting.Remove(hash); session.Close(); } continue; } if (waiting.TryGetValue(hash, out session)) { var res = session.BufferTCPPacket(tcpPacket, packet.Timeval.Date); switch (res) { case SessionForm.Results.Show: session.Show(mDockPanel, DockState.Document); break; case SessionForm.Results.Continue: continue; } waiting.Remove(hash); } } } catch (Exception ex) { Console.WriteLine(ex.ToString()); session.Close(); session = null; } } mTimer.Enabled = true; } catch (Exception) { if (!mDevice.Opened) { mDevice.Open(DeviceMode.Promiscuous, 1); } } }
void ParseImportedFile() { RawCapture packet = null; SessionForm session = null; this.Invoke((MethodInvoker) delegate { while ((packet = device.GetNextPacket()) != null) { if (!started) { continue; } TcpPacket tcpPacket = (TcpPacket)TcpPacket.ParsePacket(packet.LinkLayerType, packet.Data).Extract <TcpPacket>(); if (tcpPacket == null) { continue; } if ((tcpPacket.SourcePort < Config.Instance.LowPort || tcpPacket.SourcePort > Config.Instance.HighPort) && (tcpPacket.DestinationPort < Config.Instance.LowPort || tcpPacket.DestinationPort > Config.Instance.HighPort)) { continue; } try { if (tcpPacket.Synchronize && !tcpPacket.Acknowledgment) { if (session != null) { session.Show(mDockPanel, DockState.Document); } session = NewSession(); var res = session.BufferTCPPacket(tcpPacket, packet.Timeval.Date); if (res == SessionForm.Results.Continue) { // mDockPanel.Contents.Add(session); //session.Show(mDockPanel, DockState.Document); } } else if (session != null && session.MatchTCPPacket(tcpPacket)) { var res = session.BufferTCPPacket(tcpPacket, packet.Timeval.Date); if (res == SessionForm.Results.CloseMe) { session.Close(); } } } catch (Exception ex) { Console.WriteLine("Exception while parsing logfile: {0}", ex); session.Close(); session = null; } } if (session != null) { session.Show(mDockPanel, DockState.Document); } if (session != null) { mSearchForm.RefreshOpcodes(false); } }); }