Example #1
0
        public DHCPMessage(byte[] msg, delLogLine log)
        {
            _start = DateTime.Now;
            if (log != null)
            {
                log(LogLevels.Trace, "Parsing DHCP Message of length: " + msg.Length);
            }
            BinaryReader br = new BinaryReader(new MemoryStream(msg));

            _opcode        = (DHCPOpCode)br.ReadByte();
            _hardwareType  = br.ReadByte();
            _addressLen    = br.ReadByte();
            _hops          = br.ReadByte();
            _transactionID = br.ReadInt32();
            _seconds       = br.ReadInt16();
            _broadcast     = (br.ReadInt16() ^ 0x8000) == 0x8000;
            _clientIP      = br.ReadInt32();
            _assignedIP    = br.ReadInt32();
            _serverIP      = br.ReadInt32();
            _relayIP       = br.ReadInt32();
            byte[] tmp = br.ReadBytes(16);
            _mac = new byte[_addressLen];
            for (int x = 0; x < _mac.Length; x++)
            {
                _mac[x] = tmp[x];
            }
            tmp = br.ReadBytes(64);
            MemoryStream ms = new MemoryStream();

            for (int x = 0; x < tmp.Length; x++)
            {
                if (tmp[x] == (byte)'\0')
                {
                    break;
                }
                else
                {
                    ms.WriteByte(tmp[x]);
                }
            }
            _serverHostName = System.Text.ASCIIEncoding.ASCII.GetString(ms.ToArray());
            tmp             = br.ReadBytes(128);
            ms = new MemoryStream();
            for (int x = 0; x < tmp.Length; x++)
            {
                if (tmp[x] == (byte)'\0')
                {
                    break;
                }
                else
                {
                    ms.WriteByte(tmp[x]);
                }
            }
            _file    = System.Text.ASCIIEncoding.ASCII.GetString(ms.ToArray());
            _options = new List <DHCPOption>();
            if (br.ReadInt32() == BitConverter.ToInt32(new byte[] { (byte)0x63, (byte)0x82, (byte)0x53, (byte)0x63 }, 0))
            {
                byte b;
                while ((b = br.ReadByte()) != (byte)255)
                {
                    tmp    = new byte[(int)br.ReadByte() + 2];
                    tmp[0] = b;
                    tmp[1] = (byte)(tmp.Length - 2);
                    br.ReadBytes(tmp.Length - 2).CopyTo(tmp, 2);
                    _options.Add(new DHCPOption(tmp));
                }
            }
        }
Example #2
0
 public DHCPMessage(byte[] msg,delLogLine log)
 {
     _start = DateTime.Now;
     if (log!=null)
         log(LogLevels.Trace,"Parsing DHCP Message of length: " + msg.Length);
     BinaryReader br = new BinaryReader(new MemoryStream(msg));
     _opcode = (DHCPOpCode)br.ReadByte();
     _hardwareType = br.ReadByte();
     _addressLen = br.ReadByte();
     _hops = br.ReadByte();
     _transactionID = br.ReadInt32();
     _seconds = br.ReadInt16();
     _broadcast = (br.ReadInt16() ^ 0x8000) == 0x8000;
     _clientIP = br.ReadInt32();
     _assignedIP = br.ReadInt32();
     _serverIP = br.ReadInt32();
     _relayIP = br.ReadInt32();
     byte[] tmp = br.ReadBytes(16);
     _mac = new byte[_addressLen];
     for (int x = 0; x < _mac.Length; x++)
         _mac[x] = tmp[x];
     tmp = br.ReadBytes(64);
     MemoryStream ms = new MemoryStream();
     for (int x = 0; x < tmp.Length; x++)
     {
         if (tmp[x] == (byte)'\0')
             break;
         else
             ms.WriteByte(tmp[x]);
     }
     _serverHostName = System.Text.ASCIIEncoding.ASCII.GetString(ms.ToArray());
     tmp = br.ReadBytes(128);
     ms = new MemoryStream();
     for (int x = 0; x < tmp.Length; x++)
     {
         if (tmp[x] == (byte)'\0')
             break;
         else
             ms.WriteByte(tmp[x]);
     }
     _file = System.Text.ASCIIEncoding.ASCII.GetString(ms.ToArray());
     _options = new List<DHCPOption>();
     if (br.ReadInt32() == BitConverter.ToInt32(new byte[]{(byte)0x63,(byte)0x82,(byte)0x53,(byte)0x63},0))
     {
         byte b;
         while ((b = br.ReadByte()) != (byte)255)
         {
             tmp = new byte[(int)br.ReadByte()+2];
             tmp[0] = b;
             tmp[1] = (byte)(tmp.Length - 2);
             br.ReadBytes(tmp.Length - 2).CopyTo(tmp, 2);
             _options.Add(new DHCPOption(tmp));
         }
     }
 }