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; } }
private void DHCPRequest(DHCPTransaction transaction, string macId) { //announced so then send the offer transaction.Data.IPAddr = GetIpForDevice(macId); transaction.Data.SubMask = IPAddress.Parse("255.255.255.0"); transaction.Data.LeaseTime = 2000; transaction.Data.ServerName = "HASystem"; transaction.Data.MyIP = adapterIp; transaction.Data.RouterIP = IPAddress.None; transaction.Data.LogServerIP = "0.0.0.0"; transaction.Data.DomainIP = IPAddress.None; dhcpServer.SendDHCPMessage(DHCPMessageType.DHCPACK, transaction); }
private void DHCPAnnounced(DHCPTransaction transaction, string macId) { //options should be filled with valid data transaction.Data.IPAddr = GetIpForDevice(macId); transaction.Data.SubMask = IPAddress.Parse("255.255.255.0"); transaction.Data.LeaseTime = 2000; transaction.Data.ServerName = "HASystem"; transaction.Data.MyIP = adapterIp; transaction.Data.RouterIP = IPAddress.None; transaction.Data.LogServerIP = "0.0.0.0"; transaction.Data.DomainIP = IPAddress.None; dhcpServer.SendDHCPMessage(DHCPMessageType.DHCPOFFER, transaction); }
private void UDPDataReceived(byte[] data, IPEndPoint endPoint) { DHCPTransaction transaction; DHCPMessageType messageType; string macId; try { transaction = new DHCPTransaction(data); //data is now in the structure, get the msg type messageType = GetMessageType(transaction); macId = ByteToString(transaction.Message.D_chaddr, transaction.Message.D_hlen); switch (messageType) { case DHCPMessageType.DHCPDISCOVER: // a Mac has requested an IP // discover Msg Has been sent Announced(transaction, macId); break; case DHCPMessageType.DHCPREQUEST: Request(transaction, macId); break; } } catch (Exception) { // TODO: handle exception // Console.WriteLine(ex.Message); } }
private byte[] GetOptionData(DHCPOption type, DHCPTransaction transaction) { // Pass the option type that you require // Parse the option data // Return the data in a byte of what we need int id = 0; byte dataId = 0; byte dataLength = 0; byte[] dataDump; try { id = (int)type; //loop through look for the bit that states that the identifier is there for (int i = 0; i < transaction.Message.D_options.Length; i++) { //at the start we have the code + length //i has the code, i+1 = length of data, i+1+n = data skip dataId = transaction.Message.D_options[i]; if (dataId == id) { dataLength = transaction.Message.D_options[i + 1]; dataDump = new byte[dataLength]; Array.Copy(transaction.Message.D_options, i + 2, dataDump, 0, dataLength); return dataDump; } else { // Length of code dataLength = transaction.Message.D_options[i + 1]; i += 1 + dataLength; } } } catch (Exception) { // TODO: handle exception // Console.WriteLine(ex.Message); } finally { dataDump = null; } return null; }
private DHCPMessageType GetMessageType(DHCPTransaction transaction) { // Get the Message type, located in the options stream byte[] data; try { data = GetOptionData(DHCPOption.DHCPMessageTYPE, transaction); if (data != null) { return (DHCPMessageType)data[0]; } } catch (Exception) { // TODO: handle exception // Console.WriteLine(ex.Message); } return 0; }
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; } }