Beispiel #1
0
        public static void TraceToFile(Byte[] data, String fileName = null)
        {
#if false // use Wireshark instead
            if (String.IsNullOrEmpty(fileName))
            {
                var directoryName = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);

                var op = data.Length > 0 ? data[0] : 0;

                fileName = String.Format("pkt_{0}_{1}.txt", DateTime.Now.Ticks, 1 == op ? "in" : (2 == op ? "out" : "unknown"));
                fileName = Path.Combine(directoryName, fileName);
            }

            using (var streamWriter = new StreamWriter(fileName, false))
            {
                using (var reader = new DhcpPacketReader(data))
                {
                    streamWriter.WriteLine("--- packet.length={0}", data.Length);

                    streamWriter.WriteLine("op={0}", reader.ReadByte());
                    streamWriter.WriteLine("htype={0}", reader.ReadByte());
                    streamWriter.WriteLine("hlen={0}", reader.ReadByte());
                    streamWriter.WriteLine("hops={0}", reader.ReadByte());
                    streamWriter.WriteLine("xid={0}", reader.ReadUInt32());
                    streamWriter.WriteLine("secs={0}", reader.ReadUInt16());
                    streamWriter.WriteLine("flags={0}", reader.ReadUInt16());
                    streamWriter.WriteLine("ciaddr={0}", reader.ReadIpAddress());
                    streamWriter.WriteLine("yiaddr={0}", reader.ReadIpAddress());
                    streamWriter.WriteLine("siaddr={0}", reader.ReadIpAddress());
                    streamWriter.WriteLine("giaddr={0}", reader.ReadIpAddress());
                    streamWriter.WriteLine("chaddr={0}", TraceBinary(reader.ReadBytes(16)));
                    streamWriter.WriteLine("sname={0}", reader.ReadString(64));
                    streamWriter.WriteLine("file={0}", reader.ReadString(128));
                    streamWriter.WriteLine("mcookie=0x{0:X8}", reader.ReadUInt32());

                    while (true)
                    {
                        var id = reader.ReadByte();

                        if ((Byte)DhcpPacketOptionId.Pad == id)
                        {
                            streamWriter.WriteLine("ID {0}", id);
                            continue;
                        }
                        else if ((Byte)DhcpPacketOptionId.End == id)
                        {
                            streamWriter.WriteLine("ID {0}", id);
                            break;
                        }

                        var length = reader.ReadByte();
                        streamWriter.WriteLine("ID {0} {1}", id, TraceBinary(reader.ReadBytes(length)));
                    }
                }
            }
#endif
        }
Beispiel #2
0
        public void FromArray(Byte[] data, int dataLength = -1)
        {
            if (dataLength < 0)
            {
                dataLength = data.Length;
            }

            if (dataLength < 241)
            {
                Console.WriteLine("Message is too short: {0} bytes", dataLength); // TODO: throw?
            }

            Options = new DhcpPacketOptions();

            using (var reader = new DhcpPacketReader(data))
            {
                op     = reader.ReadByte();
                htype  = reader.ReadByte();
                hlen   = reader.ReadByte();
                hops   = reader.ReadByte();
                xid    = reader.ReadUInt32();
                secs   = reader.ReadUInt16();
                flags  = reader.ReadUInt16();
                ciaddr = reader.ReadIpAddress();
                yiaddr = reader.ReadIpAddress();
                siaddr = reader.ReadIpAddress();
                giaddr = reader.ReadIpAddress();
                chaddr = reader.ReadBytes(16);
                sname  = reader.ReadString(64);
                file   = reader.ReadString(128);

                var magicCookie = reader.ReadUInt32();
                if (magicCookie != DhcpMessageMagicCookie)
                {
                    Console.WriteLine("Wrong magic cookie: 0x{0:X8} instead of 0x{1:X8}", magicCookie, DhcpMessageMagicCookie); // TODO: throw?
                }

                while (true)
                {
                    var id = (DhcpPacketOptionId)reader.ReadByte();

                    if (DhcpPacketOptionId.Pad == id)
                    {
                        continue;
                    }
                    else if (DhcpPacketOptionId.End == id)
                    {
                        break;
                    }

                    var length = reader.ReadByte();

                    Options.SetBytes(id, reader.ReadBytes(length));
                }
            }
        }