public DHCPPacketView(DHCPMessageType messageType) { Packet = new DHCPPacket(); DHCPMessageType = messageType; Hops = 0; TimeElapsed = TimeSpan.Zero; BroadcastFlag = false; Packet.sname = string.Empty; Packet.file = string.Empty; Packet.magicNumber = DHCPPacket.DHCPMagicNumber; ClientIP = IPAddress.Any; YourIP = IPAddress.Any; NextServerIP = IPAddress.Any; RelayAgentIP = IPAddress.Any; }
public void SendDHCPMessage(DHCPMessageType messageType, DHCPTransaction transaction) { //mac announced itself, established IP etc.... //send the offer to the mac byte[] subnet; byte[] hostId; byte[] dataToSend; //we shall leave everything as is structure wise //shall CHANGE the type to OFFER //shall set the client's IP-Address try { //change message type to reply transaction.Message.D_op = 2; //subnet subnet = transaction.Data.SubMask.GetAddressBytes(); //create your ip address transaction.Message.D_yiaddr = transaction.Data.IPAddr.GetAddressBytes(); //Host ID hostId = System.Text.Encoding.ASCII.GetBytes(transaction.Data.ServerName); CreateOptionStruct(ref transaction, messageType); //send the data to the unit dataToSend = BuildDataStructure(transaction.Message); udp.SendData(dataToSend); } catch (Exception) { // TODO: handle exception // Console.WriteLine(ex.Message); } finally { subnet = null; //LeaseTime= null; hostId = null; dataToSend = null; } }
public DHCPOptionDHCPMessageType(int optionLength, byte[] buffer, long offset) { MessageType = (DHCPMessageType)Convert.ToInt32(buffer[offset]); }
public DHCPOptionDHCPMessageType(DHCPMessageType messageType) { MessageType = messageType; }
private void AddDHCPMessageType(ref byte[] vend, ref int idx, DHCPMessageType type) { vend[idx++] = 53; vend[idx++] = 0x01; vend[idx++] = (byte)type; }
private void CreateOptionStruct(ref DHCPTransaction transaction, DHCPMessageType optionReplayMessage) { byte[] parameterRequestList; byte[] ipAddress; byte[] leaseTime; byte[] serverIp; try { //we look for the parameter request list parameterRequestList = GetOptionData(DHCPOption.ParameterRequestList, transaction); //erase the options array, and set the message type to ack transaction.Message.D_options = null; CreateOptionElement(DHCPOption.DHCPMessageTYPE, new byte[] { (byte)optionReplayMessage }, ref transaction.Message.D_options); //server identifier, my IP serverIp = transaction.Data.MyIP.GetAddressBytes(); CreateOptionElement(DHCPOption.ServerIdentifier, serverIp, ref transaction.Message.D_options); // parameterRequestList contains the option data in a byte that is requested by the unit foreach (byte i in parameterRequestList) { ipAddress = null; switch ((DHCPOption)i) { case DHCPOption.SubnetMask: ipAddress = transaction.Data.SubMask.GetAddressBytes(); break; case DHCPOption.Router: ipAddress = transaction.Data.RouterIP.GetAddressBytes(); break; case DHCPOption.DomainNameServer: ipAddress = transaction.Data.DomainIP.GetAddressBytes(); break; case DHCPOption.DomainName: ipAddress = System.Text.Encoding.ASCII.GetBytes(transaction.Data.ServerName); break; case DHCPOption.ServerIdentifier: ipAddress = transaction.Data.MyIP.GetAddressBytes(); break; case DHCPOption.LogServer: ipAddress = System.Text.Encoding.ASCII.GetBytes(transaction.Data.LogServerIP); break; case DHCPOption.NetBIOSoverTCPIPNameServer: break; } if (ipAddress != null) { CreateOptionElement((DHCPOption)i, ipAddress, ref transaction.Message.D_options); } } //lease time leaseTime = new byte[4]; leaseTime[3] = (byte)(transaction.Data.LeaseTime); leaseTime[2] = (byte)(transaction.Data.LeaseTime >> 8); leaseTime[1] = (byte)(transaction.Data.LeaseTime >> 16); leaseTime[0] = (byte)(transaction.Data.LeaseTime >> 24); CreateOptionElement(DHCPOption.IPAddressLeaseTime, leaseTime, ref transaction.Message.D_options); CreateOptionElement(DHCPOption.RenewalTimeValue_T1, leaseTime, ref transaction.Message.D_options); CreateOptionElement(DHCPOption.RebindingTimeValue_T2, leaseTime, ref transaction.Message.D_options); //create the end option Array.Resize(ref transaction.Message.D_options, transaction.Message.D_options.Length + 1); Array.Copy(new byte[] { 255 }, 0, transaction.Message.D_options, transaction.Message.D_options.Length - 1, 1); } catch (Exception) { // TODO: handle exception // Console.WriteLine(ex.Message); } finally { leaseTime = null; parameterRequestList = null; ipAddress = null; } }
public DHCPPacket(byte[] data) { DHCPHdr = DecodeBytes(data); if (DHCPHdr.Magic != 0x63538263) { Malformed = true; return; } if (DHCPHdr.MACLength > 16) { Malformed = true; return; } for (int i = 0xF0; i < data.Length;) { if (data[i] == 0xFF) { break; } if (data.Length - i < 2) { Malformed = true; break; } DHCPOption o = new DHCPOption(); o.Type = data[i + 0]; o.Size = data[i + 1]; if (o.Type == 0 || o.Size == 0) { Malformed = true; break; } if (data.Length - (i + o.Size) < 0) { Malformed = true; break; } o.Data = new byte[o.Size]; Buffer.BlockCopy(data, i + 2, o.Data, 0, o.Size); DHCPOptions.Add(o); i += 2 + o.Size; } foreach (DHCPOption o in DHCPOptions) { switch (o.Type) { case 97: if (o.Size != 17) { Malformed = true; continue; } DHCP97ClientUUID = BytesToString(o.Data, 1); break; case 61: //if (o.Size != 17) //{ // Malformed = true; // continue; //} DHCP61ClientGUID = BytesToString(o.Data, 1); break; case 55: DHCP9ReqParameterList = new List <byte>(); foreach (byte b in o.Data) { if (b == 0 || b == 0xff) { Malformed = true; } if (DHCP9ReqParameterList.Contains(b) == false) { DHCP9ReqParameterList.Add(b); } } break; case 53: if (o.Size != 1) { Malformed = true; continue; } DHCP53MessageType = (DHCPMessageType)o.Data[0]; break; case 57: if (o.Size != 2) { Malformed = true; continue; } DHCP57MessageLength = BitConverter.ToUInt16(o.Data.Reverse().ToArray(), 0); break; case 60: if (o.Size != 32) { Malformed = true; continue; } DHCP60ClassIdentifier = Encoding.ASCII.GetString(o.Data); break; case 66: DHCP66BootServer = Encoding.ASCII.GetString(o.Data).NullTrim(); break; case 67: DHCP67BootFilename = Encoding.ASCII.GetString(o.Data).NullTrim(); break; case 93: if (o.Size != 2) { Malformed = true; continue; } DHCP93Architecture = (DHCPArchitecture)BitConverter.ToUInt16(o.Data.Reverse().ToArray(), 0); break; case 94: if (o.Size != 3) { Malformed = true; continue; } if (o.Data[0] == 1) { DHCP94ClientNIC = "UNDI."; } else { DHCP94ClientNIC = "UNKN."; } DHCP94ClientNIC += o.Data[1].ToString("0") + "." + o.Data[2].ToString("0"); break; default: Debug.WriteLine("Unknown code: " + o.Type.ToString() + " (0x" + o.Type.ToString("X2") + ")"); break; } } Flags = DHCPHdr.Flags; OperationCode = DHCPHdr.OperationCode; HardwareType = DHCPHdr.HardwareType; XID = DHCPHdr.XID; IPClient = new IPAddress(DHCPHdr.CIAddr); IPYours = new IPAddress(DHCPHdr.YIAddr); IPServer = new IPAddress(DHCPHdr.SIAddr); IPGateway = new IPAddress(DHCPHdr.GIAddr); MacAddress = new byte[DHCPHdr.MACLength]; Buffer.BlockCopy(DHCPHdr.CHAddr, 0, MacAddress, 0, DHCPHdr.MACLength); Servername = Encoding.ASCII.GetString(DHCPHdr.SName).NullTrim(); BootFile = Encoding.ASCII.GetString(DHCPHdr.File).NullTrim(); }
public DHCPOptionMessageType(DHCPMessageType messageType) : base(DHCPOption.MessageType) { this.MessageType = messageType; }