static void Main(string[] args) { Templates _templates = new Templates(); Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9991); sock.Bind(iep); EndPoint ep = (EndPoint)iep; byte[] data = new byte[2048]; while (true) { int recv = sock.ReceiveFrom(data, ref ep); //Console.ReadKey(); Console.Clear(); byte[] bytes = new byte[recv]; for (int i = 0; i < recv; i++) bytes[i] = data[i]; Packet packet = new Packet(bytes, _templates); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine(packet.ToString()); } sock.Close(); Console.ReadKey(); }
public Packet(Byte[] bytes, Templates templates) { this._bytes = bytes; this.Parse(templates); }
private void Parse(Templates templates) { this._flowset = new List<FlowSet>(); Int32 length = _bytes.Length - 20; Byte[] header = new Byte[20]; Byte[] flowset = new Byte[length]; Array.Copy(_bytes, 0, header, 0, 20); Array.Copy(_bytes, 20, flowset, 0, length); this._header = new Header(header); byte[] reverse = flowset.Reverse().ToArray(); int templengh = 0; while ((templengh + 2) < flowset.Length) { UInt16 lengths = BitConverter.ToUInt16(reverse, flowset.Length - sizeof(Int16) - (templengh+2)); Byte[] bflowsets = new Byte[lengths]; Array.Copy(flowset, templengh, bflowsets, 0, lengths); FlowSet flowsets = new FlowSet(bflowsets, templates); this._flowset.Add(flowsets); templengh += lengths; } }
private void Parse(Templates templates) { byte[] reverse = this._bytes.Reverse().ToArray(); this._template = new List<Template>(); this._valuebyte = new List<byte>(); this._id = BitConverter.ToUInt16(reverse, this._bytes.Length - sizeof(Int16) - 0); this._length = BitConverter.ToUInt16(reverse, this._bytes.Length - sizeof(Int16) - 2); if ((this._id == 0) || (this._id == 1)) { if (this._id == 0) { int cout, pastaddress, address = 6; while (address < this._bytes.Length) { cout = BitConverter.ToUInt16(reverse, this._bytes.Length - sizeof(Int16) - address); int length = cout * 4 + 4; Byte[] btemplate = new Byte[length]; Array.Copy(this._bytes, address - 2, btemplate, 0, length); Template template = new Template(btemplate); this._template.Add(template); Boolean flag = false; Template[] templs = templates.Templats.ToArray(); for (int i = 0; i < templs.Length; i++ ) { if (template.ID == templs[i].ID) { flag = true; templs[i] = template; } } if (flag) { templates.Templats = templs.ToList(); } else { templates.Templats.Add(template); } pastaddress = address; address = cout * 4 + pastaddress + 4; } } } else if (this._id > 255) { Boolean flag = false; Template templs = null; foreach(Template templ in templates.Templats) { if (templ.ID == this._id) { templs = DeepClone(templ) as Template; flag = true; } } int j = 4, z; if (flag) { z = (this._length - 4) / templs.FieldLength; for (int y = 0; y < z; y++) { foreach (Field fields in templs.Field) { for (int i = 0; i < fields.Length; i++, j++) { fields.Value.Add(this._bytes[j]); } } this._template.Add(DeepClone(templs) as Template); foreach (Field filds in templs.Field) { filds.Value.Clear(); } } } if (!flag) { for (int i = 4; i < this._bytes.Length; i++) { this._valuebyte.Add(this._bytes[i]); } } } }