public DhcpPacket(MemBlock Packet) {
if(Packet.Length < 240) {
throw new Exception("Invalid Dhcp Packet: Length < 240.");
}
_packet = Packet;
op = Packet[0];
int hlen = Packet[2];
xid = Packet.Slice(4, 4);
ciaddr = Packet.Slice(12, 4);
yiaddr = Packet.Slice(16, 4);
siaddr = Packet.Slice(20, 4);
chaddr = Packet.Slice(28, hlen);
MemBlock key = Packet.Slice(236, 4);
if(!key.Equals(magic_key)) {
throw new Exception("Invalid Dhcp Packet: Invalid magic key.");
}
int idx = 240;
/* Parse the options */
Options = new Dictionary<OptionTypes, MemBlock>();
/* 255 is end of options */
while(Packet[idx] != 255) {
/* 0 is padding */
if(Packet[idx] != 0) {
OptionTypes type = (OptionTypes) Packet[idx++];
byte length = Packet[idx++];
Options[type] = Packet.Slice(idx, length);
idx += length;
}
else {
idx++;
}
}
}