private static PacketRecord readPacketData(BinaryReader binaryReader, long len, uint tsSec, int curPacket) { // Begin reading headers long packetStartPos = binaryReader.BaseStream.Position; bool isSend = readNetworkHeaders(binaryReader); long headersSize = binaryReader.BaseStream.Position - packetStartPos; // Begin reading non-header packet content StringBuilder packetHeadersStr = new StringBuilder(); StringBuilder packetTypeStr = new StringBuilder(); PacketRecord packet = new PacketRecord(); packet.index = curPacket; packet.isSend = isSend; packet.tsSec = tsSec; packet.extraInfo = ""; packet.data = binaryReader.ReadBytes((int)(len - headersSize)); BinaryReader packetReader = new BinaryReader(new MemoryStream(packet.data)); try { ProtoHeader pHeader = ProtoHeader.read(packetReader); packet.optionalHeadersLen = readOptionalHeaders(pHeader.header_, packetHeadersStr, packetReader); if (packetReader.BaseStream.Position == packetReader.BaseStream.Length) { packetTypeStr.Append("<Header Only>"); } uint HAS_FRAGS_MASK = 0x4; // See SharedNet::SplitPacketData if ((pHeader.header_ & HAS_FRAGS_MASK) != 0) { while (packetReader.BaseStream.Position != packetReader.BaseStream.Length) { if (packetTypeStr.Length != 0) { packetTypeStr.Append(" + "); } BlobFrag newFrag = readFragment(packetReader); packet.frags.Add(newFrag); if (newFrag.memberHeader_.blobNum != 0) { packetTypeStr.Append("FragData["); packetTypeStr.Append(newFrag.memberHeader_.blobNum); packetTypeStr.Append("]"); } else { BinaryReader fragDataReader = new BinaryReader(new MemoryStream(newFrag.dat_)); PacketOpcode opcode = Util.readOpcode(fragDataReader); packet.opcodes.Add(opcode); packetTypeStr.Append(opcode); } } } if (packetReader.BaseStream.Position != packetReader.BaseStream.Length) { packet.extraInfo = "Didnt read entire packet! " + packet.extraInfo; } } catch (OutOfMemoryException e) { //MessageBox.Show("Out of memory (packet " + curPacket + "), stopping read: " + e); return(null); } catch (Exception e) { packet.extraInfo += "EXCEPTION: " + e.Message + " " + e.StackTrace; } packet.packetHeadersStr = packetHeadersStr.ToString(); packet.packetTypeStr = packetTypeStr.ToString(); return(packet); }
private static bool readMessageData(BinaryReader binaryReader, long len, uint tsSec, List <PacketRecord> results, Dictionary <ulong, PacketRecord> incompletePacketMap) { // Begin reading headers long packetStartPos = binaryReader.BaseStream.Position; bool isSend = readNetworkHeaders(binaryReader); long headersSize = binaryReader.BaseStream.Position - packetStartPos; // Begin reading non-header packet content StringBuilder packetHeadersStr = new StringBuilder(); StringBuilder packetTypeStr = new StringBuilder(); PacketRecord packet = null; byte[] packetData = binaryReader.ReadBytes((int)(len - headersSize)); BinaryReader packetReader = new BinaryReader(new MemoryStream(packetData)); try { ProtoHeader pHeader = ProtoHeader.read(packetReader); uint HAS_FRAGS_MASK = 0x4; // See SharedNet::SplitPacketData if ((pHeader.header_ & HAS_FRAGS_MASK) != 0) { readOptionalHeaders(pHeader.header_, packetHeadersStr, packetReader); while (packetReader.BaseStream.Position != packetReader.BaseStream.Length) { BlobFrag newFrag = readFragment(packetReader); ulong blobID = newFrag.memberHeader_.blobID; if (incompletePacketMap.ContainsKey(blobID)) { packet = incompletePacketMap[newFrag.memberHeader_.blobID]; } else { packet = new PacketRecord(); incompletePacketMap.Add(blobID, packet); } if (newFrag.memberHeader_.blobNum == 0) { packet.isSend = isSend; packet.tsSec = tsSec; packet.extraInfo = ""; BinaryReader fragDataReader = new BinaryReader(new MemoryStream(newFrag.dat_)); PacketOpcode opcode = Util.readOpcode(fragDataReader); packet.opcodes.Add(opcode); packet.packetTypeStr = opcode.ToString(); } packet.packetHeadersStr += packetHeadersStr.ToString(); packet.frags.Add(newFrag); if (addPacketIfFinished(results, packet)) { incompletePacketMap.Remove(blobID); } } if (packetReader.BaseStream.Position != packetReader.BaseStream.Length) { packet.extraInfo = "Didnt read entire packet! " + packet.extraInfo; } } } catch (OutOfMemoryException e) { //MessageBox.Show("Out of memory (packet " + curPacket + "), stopping read: " + e); return(false); } catch (Exception e) { packet.extraInfo += "EXCEPTION: " + e.Message + " " + e.StackTrace; } return(true); }
private int readPacketRecordData(BinaryReader binaryReader, long len, uint tsSec, long curPacket, bool dontList) { // Begin reading headers long packetStartPos = binaryReader.BaseStream.Position; EthernetHeader ethernetHeader = EthernetHeader.read(binaryReader); // Skip non-IP packets if (ethernetHeader.proto != 8) { binaryReader.BaseStream.Position += len - (binaryReader.BaseStream.Position - packetStartPos); return(1); } IpHeader ipHeader = IpHeader.read(binaryReader); // Skip non-UDP packets if (ipHeader.proto != 17) { binaryReader.BaseStream.Position += len - (binaryReader.BaseStream.Position - packetStartPos); return(1); } UdpHeader udpHeader = UdpHeader.read(binaryReader); bool isSend = (udpHeader.dPort >= 9000 && udpHeader.dPort <= 9013); bool isRecv = (udpHeader.sPort >= 9000 && udpHeader.sPort <= 9013); // Skip non-AC-port packets if (!isSend && !isRecv) { binaryReader.BaseStream.Position += len - (binaryReader.BaseStream.Position - packetStartPos); return(1); } long headersSize = binaryReader.BaseStream.Position - packetStartPos; // Begin reading non-header packet content StringBuilder packetHeadersStr = new StringBuilder(); StringBuilder packetTypeStr = new StringBuilder(); PacketRecord packet = new PacketRecord(); packet.index = records.Count; packet.isSend = isSend; packet.tsSec = tsSec; packet.netPacket = new NetPacket(); packet.data = binaryReader.ReadBytes((int)(len - headersSize)); packet.extraInfo = ""; BinaryReader packetReader = new BinaryReader(new MemoryStream(packet.data)); try { ProtoHeader pHeader = ProtoHeader.read(packetReader); readOptionalHeaders(packet, pHeader.header_, packetHeadersStr, packetReader); if (packetReader.BaseStream.Position == packetReader.BaseStream.Length) { packetTypeStr.Append("<Header Only>"); } uint HAS_FRAGS_MASK = 0x4; // See SharedNet::SplitPacketData if ((pHeader.header_ & HAS_FRAGS_MASK) != 0) { bool first = true; while (packetReader.BaseStream.Position != packetReader.BaseStream.Length) { if (!first) { packetTypeStr.Append(" + "); } readPacket(packet, packetTypeStr, packetReader); first = false; } } if (packetReader.BaseStream.Position != packetReader.BaseStream.Length) { packet.extraInfo = "Didnt read entire packet! " + packet.extraInfo; } } catch (OutOfMemoryException e) { //MessageBox.Show("Out of memory (packet " + curPacket + "), stopping read: " + e); return(2); } catch (Exception e) { packet.extraInfo += "EXCEPTION: " + e.Message + " " + e.StackTrace; } packet.packetHeadersStr = packetHeadersStr.ToString(); packet.packetTypeStr = packetTypeStr.ToString(); records.Add(packet); if (!dontList) { ListViewItem newItem = new ListViewItem(packet.index.ToString()); newItem.SubItems.Add(packet.isSend ? "Send" : "Recv"); newItem.SubItems.Add(packet.tsSec.ToString()); newItem.SubItems.Add(packet.packetHeadersStr); newItem.SubItems.Add(packet.packetTypeStr); newItem.SubItems.Add(packet.data.Length.ToString()); newItem.SubItems.Add(packet.extraInfo); listItems.Add(newItem); } return(0); }