Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Collections;
- using PcapDotNet;
- using PcapDotNet.Core;
- using PcapDotNet.Packets;
- using PcapDotNet.Packets.IpV4;
- using PcapDotNet.Packets.Transport;
- using PcapDotNet.Packets.Ethernet;
- using System.Threading;
- namespace DataFus
- {
- public partial class Form1 : Form
- {
- public Hashtable protocolTypeId = new Hashtable();
- public Hashtable protocolMessageId = new Hashtable();
- public Form1()
- {
- InitializeComponent();
- }
- public string FindTextBetween(string text, string left, string right)
- {
- // TODO: Validate input arguments
- int beginIndex = text.IndexOf(left); // find occurence of left delimiter
- if (beginIndex == -1)
- return string.Empty; // or throw exception?
- beginIndex += left.Length;
- int endIndex = text.IndexOf(right, beginIndex); // find occurence of right delimiter
- if (endIndex == -1)
- return string.Empty; // or throw exception?
- return text.Substring(beginIndex, endIndex - beginIndex).Trim();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- int fileCount = 0;
- string folderPath ="types";
- string[] filePathsTypes = Directory.GetFiles(folderPath, "*.as", SearchOption.AllDirectories);
- foreach (var item in filePathsTypes)
- fileCount++;
- folderPath = "messages";
- string[] filePathsMessages = Directory.GetFiles(folderPath, "*.as", SearchOption.AllDirectories);
- foreach (var item in filePathsMessages)
- fileCount++;
- label2.Text = fileCount.ToString();
- fileCount = 0;
- foreach (var item in filePathsTypes)
- {
- string readText = File.ReadAllText(item);
- if (readText.Contains("public static const protocolId:uint ="))
- {
- fileCount++;
- string result = FindTextBetween(readText, "public static const protocolId:uint = ", ";");
- protocolTypeId.Add(result, item);
- }
- }
- foreach (var item in filePathsMessages)
- {
- string readText = File.ReadAllText(item);
- if (readText.Contains("public static const protocolId:uint ="))
- {
- fileCount++;
- string result = FindTextBetween(readText, "public static const protocolId:uint = ", ";");
- protocolMessageId.Add(result, item);
- }
- }
- label4.Text = fileCount.ToString();
- }
- public delegate void UpdateUi(string text);
- public delegate void UpdateUiDone();
- public void UiDoSome(string text)
- {
- label1.Text = text;
- }
- public void UiDoSomeDone()
- {
- label1.Text = "done";
- }
- public void ThreadProc()
- {
- label1.BeginInvoke((MethodInvoker)delegate()
- {
- label1.Text += "ee";
- });
- IList<LivePacketDevice> allDevices = LivePacketDevice.AllLocalMachine;
- if (allDevices.Count == 0)
- MessageBox.Show("No Device Found");
- for (int i = 0; i != allDevices.Count; ++i)
- {
- LivePacketDevice device = allDevices[i];
- //richTextBox1.AppendText((i + 1) + ". " + device.Name);
- if (device.Description != null)
- ;//richTextBox1.AppendText(" (" + device.Description + ")");
- else
- ;//richTextBox1.AppendText(" (No description available)");
- }
- int deviceIndex = 0;
- do
- {
- string deviceIndexString = "6";
- if (!int.TryParse(deviceIndexString, out deviceIndex) ||
- deviceIndex < 1 || deviceIndex > allDevices.Count)
- {
- deviceIndex = 0;
- }
- } while (deviceIndex == 0);
- PacketDevice selectedDevice = allDevices[deviceIndex - 1];
- using (PacketCommunicator communicator =
- selectedDevice.Open(65536, PacketDeviceOpenAttributes.MaximumResponsiveness, 10))
- {
- if (communicator.DataLink.Kind != DataLinkKind.Ethernet)
- {
- //richTextBox1.AppendText("This program works only on Ethernet networks.");
- return;
- }
- using (BerkeleyPacketFilter filter = communicator.CreateFilter("tcp and port " + 5555))
- {
- communicator.SetFilter(filter);
- }
- communicator.ReceivePackets(0, PacketHandler);
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- var t = new Thread(new ThreadStart(ThreadProc));
- t.Start();
- return;
- IList<LivePacketDevice> allDevices = LivePacketDevice.AllLocalMachine;
- if (allDevices.Count == 0)
- MessageBox.Show("No Device Found");
- for (int i = 0; i != allDevices.Count; ++i)
- {
- LivePacketDevice device = allDevices[i];
- richTextBox1.AppendText((i + 1) + ". " + device.Name);
- if (device.Description != null)
- richTextBox1.AppendText(" (" + device.Description + ")");
- else
- richTextBox1.AppendText(" (No description available)");
- }
- int deviceIndex = 0;
- do
- {
- string deviceIndexString = "6";
- if (!int.TryParse(deviceIndexString, out deviceIndex) ||
- deviceIndex < 1 || deviceIndex > allDevices.Count)
- {
- deviceIndex = 0;
- }
- } while (deviceIndex == 0);
- PacketDevice selectedDevice = allDevices[deviceIndex - 1];
- using (PacketCommunicator communicator =
- selectedDevice.Open(65536, PacketDeviceOpenAttributes.MaximumResponsiveness, 10))
- {
- if (communicator.DataLink.Kind != DataLinkKind.Ethernet)
- {
- richTextBox1.AppendText("This program works only on Ethernet networks.");
- return;
- }
- using (BerkeleyPacketFilter filter = communicator.CreateFilter("tcp and port " + 5555))
- {
- communicator.SetFilter(filter);
- }
- communicator.ReceivePackets(0, PacketHandler);
- }
- }
- public static string ByteArrayToString(byte[] ba)
- {
- StringBuilder hex = new StringBuilder(ba.Length * 2);
- foreach (byte b in ba)
- {
- hex.AppendFormat("{0:x2}", b);
- }
- return hex.ToString();
- }
- public void PacketHandler(Packet packet)
- {
- IpV4Datagram datagram = packet.Ethernet.IpV4;
- String ipSource = datagram.Source.ToString();
- MemoryStream stream = datagram.Tcp.Payload.ToMemoryStream();
- byte[] data = stream.ToArray();
- if (data.Length > 0)
- {
- string buff = ByteArrayToString(data);
- string hexid = buff.Substring(0, 4);
- int result = Convert.ToInt32(hexid, 16) >> 2;
- if (protocolMessageId.Contains(result.ToString()))
- {
- richTextBox1.BeginInvoke((MethodInvoker)delegate()
- {
- richTextBox1.AppendText("[" + result.ToString() + "]" + protocolMessageId[result.ToString()].ToString() + "\n");
- });
- }
- else if (protocolTypeId.Contains(result.ToString()))
- {
- richTextBox1.BeginInvoke((MethodInvoker)delegate()
- {
- richTextBox1.AppendText("{" + result.ToString() + "}" + protocolTypeId[result.ToString()].ToString() + "\n");
- });
- }
- }
- if (data.Length == 0)
- return;
- var ethernetLayer = packet.Ethernet.ExtractLayer() as EthernetLayer;
- var ipv4Layer = packet.Ethernet.IpV4.ExtractLayer() as IpV4Layer;
- var tcpLayer = packet.Ethernet.IpV4.Tcp.ExtractLayer() as TcpLayer;
- var payloadLayer = packet.Ethernet.IpV4.Tcp.Payload.ExtractLayer() as PayloadLayer;
- richTextBox1.BeginInvoke((MethodInvoker)delegate()
- {
- richTextBox1.ScrollToCaret();
- });
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement