private static uint HandlePktSendHook(ref IntPtr Packet, ref uint PacketSize, uint ControlCode, uint IpAddress, IntPtr Reserved, IntPtr PktContext)
        {
            var stopPropagation = false;

            var packet        = new DhcpServerPacketWritable(Packet, (int)PacketSize);
            var serverAddress = DhcpServerIpAddress.FromNative(IpAddress);

            // ControlCode is ignored in this wrapper as:
            // https://msdn.microsoft.com/en-us/library/windows/desktop/aa363294.aspx
            // "The only acceptable value in this version of the DHCP Server API is DHCP_SEND_PACKET."

            foreach (var consumer in Consumers)
            {
                if (consumer.SupportFlags.HasFlag(CalloutConsumerSupportFlags.PacketSend))
                {
                    try
                    {
                        consumer.Proxy.PacketSend(packet, serverAddress, ref stopPropagation);
                    }
                    catch (Win32Exception ex)
                    {
                        return((uint)ex.NativeErrorCode);
                    }
                    catch (Exception)
                    {
                        return(0x1U); //ERROR_INVALID_FUNCTION
                    }

                    // stop propagation if instructed
                    if (stopPropagation)
                    {
                        break;
                    }
                }
            }

            // write packet
            if (packet.BufferModified)
            {
                packet.WriteBuffer(Packet, ref PacketSize);
            }

            // chain
            if (!stopPropagation && ChainTable.DhcpPktSendHook != null)
            {
                return(ChainTable.DhcpPktSendHook(ref Packet, ref PacketSize, ControlCode, IpAddress, Reserved, PktContext));
            }

            return(ERROR_SUCCESS);
        }
        private static uint HandleNewPktHook(ref IntPtr Packet, ref uint PacketSize, uint IpAddress, IntPtr Reserved, IntPtr PktContext, out bool ProcessIt)
        {
            ProcessIt = true;
            var stopPropagation = false;

            var packet        = new DhcpServerPacketWritable(Packet, (int)PacketSize);
            var serverAddress = DhcpServerIpAddress.FromNative(IpAddress);

            foreach (var consumer in Consumers)
            {
                if (consumer.SupportFlags.HasFlag(CalloutConsumerSupportFlags.NewPacket))
                {
                    try
                    {
                        consumer.Proxy.NewPacket(packet, serverAddress, ref ProcessIt, ref stopPropagation);
                    }
                    catch (Win32Exception ex)
                    {
                        return((uint)ex.NativeErrorCode);
                    }
                    catch (Exception)
                    {
                        return(0x1U); //ERROR_INVALID_FUNCTION
                    }

                    // stop propagation if instructed or the packet is not to be processed
                    if (stopPropagation || !ProcessIt)
                    {
                        break;
                    }
                }
            }

            // write packet
            if (ProcessIt && packet.BufferModified)
            {
                packet.WriteBuffer(Packet, ref PacketSize);
            }

            // chain
            if (ProcessIt && !stopPropagation && ChainTable.DhcpNewPktHook != null)
            {
                return(ChainTable.DhcpNewPktHook(ref Packet, ref PacketSize, IpAddress, Reserved, PktContext, out ProcessIt));
            }

            return(ERROR_SUCCESS);
        }
        public void NewPacket(IDhcpServerPacketWritable Packet, DhcpServerIpAddress ServerAddress, ref bool ProcessIt, ref bool StopPropagation)
        {
            try
            {
                var sb = new StringBuilder();
                sb.AppendLine("NEW PACKET:");
                sb.AppendLine(Packet.ToString());
                sb.AppendLine("---------------------");
                WriteLog(sb.ToString());
            }
            catch (Exception ex)
            {
                WriteLog($"NEW PACKET ERROR: {ex.Message} [{ex.GetType().Name}]");
                WriteLog(ex.StackTrace);

                throw;
            }
        }
        private static uint HandleDeleteClientHook(uint IpAddress, IntPtr HwAddress, uint HwAddressLength, uint Reserved, uint ClientType)
        {
            var stopPropagation = false;

            var leaseAddress = DhcpServerIpAddress.FromNative(IpAddress);

            if (HwAddressLength <= DhcpServerHardwareAddress.MaximumLength)
            {
                var leaseHardwareAddress = DhcpServerHardwareAddress.FromNative(DhcpServerHardwareType.Ethernet, HwAddress, (int)HwAddressLength);

                foreach (var consumer in Consumers)
                {
                    if (consumer.SupportFlags.HasFlag(CalloutConsumerSupportFlags.DeleteClient))
                    {
                        try
                        {
                            consumer.Proxy.DeleteClient(leaseAddress, leaseHardwareAddress, ref stopPropagation);
                        }
                        catch (Win32Exception ex)
                        {
                            return((uint)ex.NativeErrorCode);
                        }
                        catch (Exception)
                        {
                            return(0x1U); //ERROR_INVALID_FUNCTION
                        }

                        // stop propagation if instructed
                        if (stopPropagation)
                        {
                            return(ERROR_SUCCESS);
                        }
                    }
                }
            }

            // chain
            if (!stopPropagation && ChainTable.DhcpDeleteClientHook != null)
            {
                return(ChainTable.DhcpDeleteClientHook(IpAddress, HwAddress, HwAddressLength, Reserved, ClientType));
            }

            return(ERROR_SUCCESS);
        }
        private static uint HandleAddressOfferHook(IntPtr Packet, uint PacketSize, uint ControlCode, uint IpAddress, uint AltAddress, uint AddrType, uint LeaseTime, IntPtr Reserved, IntPtr PktContext)
        {
            var stopPropagation = false;

            var packet        = new DhcpServerPacket(Packet, (int)PacketSize);
            var controlCode   = (OfferAddressControlCodes)ControlCode;
            var serverAddress = DhcpServerIpAddress.FromNative(IpAddress);
            var leaseAddress  = DhcpServerIpAddress.FromNative(AltAddress);
            var addressType   = (OfferAddressTypes)AddrType;
            var leaseTime     = new TimeSpan(LeaseTime * 10_000_000L);

            foreach (var consumer in Consumers)
            {
                if (consumer.SupportFlags.HasFlag(CalloutConsumerSupportFlags.AddressOffer))
                {
                    try
                    {
                        consumer.Proxy.AddressOffer(packet, controlCode, serverAddress, leaseAddress, addressType, leaseTime, ref stopPropagation);
                    }
                    catch (Win32Exception ex)
                    {
                        return((uint)ex.NativeErrorCode);
                    }
                    catch (Exception)
                    {
                        return(0x1U); //ERROR_INVALID_FUNCTION
                    }

                    if (stopPropagation)
                    {
                        return(ERROR_SUCCESS);
                    }
                }
            }

            // chain
            if (!stopPropagation && ChainTable.DhcpAddressOfferHook != null)
            {
                return(ChainTable.DhcpAddressOfferHook(Packet, PacketSize, ControlCode, IpAddress, AltAddress, AddrType, LeaseTime, Reserved, PktContext));
            }

            return(ERROR_SUCCESS);
        }
        private static uint HandleAddressDelHook(IntPtr Packet, uint PacketSize, uint ControlCode, uint IpAddress, uint AltAddress, IntPtr Reserved, IntPtr PktContext)
        {
            var stopPropagation = false;

            var packet        = new DhcpServerPacket(Packet, (int)PacketSize);
            var controlCode   = (AddressDeleteControlCodes)ControlCode;
            var serverAddress = DhcpServerIpAddress.FromNative(IpAddress);
            var leaseAddress  = DhcpServerIpAddress.FromNative(AltAddress);

            foreach (var consumer in Consumers)
            {
                if (consumer.SupportFlags.HasFlag(CalloutConsumerSupportFlags.AddressDelete))
                {
                    try
                    {
                        consumer.Proxy.AddressDelete(packet, controlCode, serverAddress, leaseAddress, ref stopPropagation);
                    }
                    catch (Win32Exception ex)
                    {
                        return((uint)ex.NativeErrorCode);
                    }
                    catch (Exception)
                    {
                        return(0x1U); //ERROR_INVALID_FUNCTION
                    }

                    // stop propagation if instructed
                    if (stopPropagation)
                    {
                        return(ERROR_SUCCESS);
                    }
                }
            }

            // chain
            if (!stopPropagation && ChainTable.DhcpAddressDelHook != null)
            {
                return(ChainTable.DhcpAddressDelHook(Packet, PacketSize, ControlCode, IpAddress, AltAddress, Reserved, PktContext));
            }

            return(ERROR_SUCCESS);
        }
        public void DeleteClient(DhcpServerIpAddress LeaseAddress, DhcpServerHardwareAddress LeaseHardwareAddress, ref bool StopPropagation)
        {
            try
            {
                var sb = new StringBuilder();
                sb.AppendLine("DELETE CLIENT:");
                sb.AppendLine($"  Lease Address: {LeaseAddress}");
                sb.AppendLine($"  Hardware Address: {LeaseHardwareAddress}");
                sb.AppendLine("---------------------");

                WriteLog(sb.ToString());
            }
            catch (Exception ex)
            {
                WriteLog($"DELETE CLIENT ERROR: {ex.Message} [{ex.GetType().Name}]");
                WriteLog(ex.StackTrace);

                throw;
            }
        }
 public void AddressDelete(IDhcpServerPacket Packet, AddressDeleteControlCodes ControlCode, DhcpServerIpAddress ServerAddress, DhcpServerIpAddress LeaseAddress, ref bool StopPropagation)
 {
     addressDeleteHandler.AddressDelete(Packet, ControlCode, ServerAddress, LeaseAddress, ref StopPropagation);
 }
 public void PacketSend(IDhcpServerPacketWritable Packet, DhcpServerIpAddress ServerAddress, ref bool StopPropagation)
 {
     packetSendHandler.PacketSend(Packet, ServerAddress, ref StopPropagation);
 }
 public void PacketDrop(IDhcpServerPacket Packet, PacketDropControlCodes ControlCode, DhcpServerIpAddress ServerAddress, ref bool StopPropagation)
 {
     packetDropHandler.PacketDrop(Packet, ControlCode, ServerAddress, ref StopPropagation);
 }
 public void NewPacket(IDhcpServerPacketWritable Packet, DhcpServerIpAddress ServerAddress, ref bool ProcessIt, ref bool StopPropagation)
 {
     newPacketHandler.NewPacket(Packet, ServerAddress, ref ProcessIt, ref StopPropagation);
 }
        public void PacketDrop(IDhcpServerPacket Packet, PacketDropControlCodes ControlCode, DhcpServerIpAddress ServerAddress, ref bool StopPropagation)
        {
            try
            {
                var sb = new StringBuilder();
                sb.AppendLine("PACKET DROP:");
                sb.AppendLine($" Transaction Id: {Packet.TransactionId}");
                sb.AppendLine($" Parameters:");
                sb.AppendLine($"  Control Code: {ControlCode}");
                sb.AppendLine("---------------------");

                WriteLog(sb.ToString());
            }
            catch (Exception ex)
            {
                WriteLog($"PACKET DROP ERROR: {ex.Message} [{ex.GetType().Name}]");
                WriteLog(ex.StackTrace);

                throw;
            }
        }
        public void AddressOffer(IDhcpServerPacket Packet, OfferAddressControlCodes ControlCode, DhcpServerIpAddress ServerAddress, DhcpServerIpAddress LeaseAddress, OfferAddressTypes AddressType, TimeSpan LeaseTime, ref bool StopPropagation)
        {
            try
            {
                var sb = new StringBuilder();
                sb.AppendLine("ADDRESS OFFER:");
                sb.AppendLine($" Transaction Id: {Packet.TransactionId}");
                sb.AppendLine($" Parameters:");
                sb.AppendLine($"  Dhcp Message Type: {Packet.DhcpMessageType}");
                sb.AppendLine($"  Control Code: {ControlCode}");
                sb.AppendLine($"  Offering: {LeaseAddress}");
                sb.AppendLine($"  Type: {AddressType}");
                sb.AppendLine($"  Time: {LeaseTime}");
                sb.AppendLine("---------------------");

                WriteLog(sb.ToString());
            }
            catch (Exception ex)
            {
                WriteLog($"ADDRESS OFFER ERROR: {ex.Message} [{ex.GetType().Name}]");
                WriteLog(ex.StackTrace);

                throw;
            }
        }
 public DHCP_IP_RESERVATION_V4_Managed(DhcpServerIpAddress address, DhcpServerHardwareAddress reservedForClient, DhcpServerClientTypes allowedClientTypes)
     : this(reservedIpAddress : address.ToNativeAsNetwork(),
            reservedForClient : reservedForClient.ToNativeClientUid(),
            allowedClientTypes : (DHCP_CLIENT_TYPE)allowedClientTypes)
 {
 }
 public DHCP_IP_RESERVATION_Managed(DhcpServerIpAddress address, DhcpServerHardwareAddress reservedForClient)
     : this(reservedIpAddress : address.ToNativeAsNetwork(),
            reservedForClient : reservedForClient.ToNativeClientUid())
 {
 }
 public void AddressOffer(IDhcpServerPacket Packet, OfferAddressControlCodes ControlCode, DhcpServerIpAddress ServerAddress, DhcpServerIpAddress LeaseAddress, OfferAddressTypes AddressType, TimeSpan LeaseTime, ref bool StopPropagation)
 {
     addressOfferHandler.AddressOffer(Packet, ControlCode, ServerAddress, LeaseAddress, AddressType, LeaseTime, ref StopPropagation);
 }
 public void DeleteClient(DhcpServerIpAddress LeaseAddress, DhcpServerHardwareAddress LeaseHardwareAddress, ref bool StopPropagation)
 {
     deleteClientHandler.DeleteClient(LeaseAddress, LeaseHardwareAddress, ref StopPropagation);
 }
        public void AddressDelete(IDhcpServerPacket Packet, AddressDeleteControlCodes ControlCode, DhcpServerIpAddress ServerAddress, DhcpServerIpAddress LeaseAddress, ref bool StopPropagation)
        {
            try
            {
                var sb = new StringBuilder();
                sb.AppendLine("ADDRESS DELETE:");
                sb.AppendLine($" Transaction Id: {Packet.TransactionId}");
                sb.AppendLine($" Parameters:");
                sb.AppendLine($"  Control Code: {ControlCode}");
                sb.AppendLine($"  Lease Address: {LeaseAddress}");
                sb.AppendLine("---------------------");

                WriteLog(sb.ToString());
            }
            catch (Exception ex)
            {
                WriteLog($"ADDRESS DELETE ERROR: {ex.Message} [{ex.GetType().Name}]");
                WriteLog(ex.StackTrace);

                throw;
            }
        }