Exemple #1
0
        //Process the client request.  Find appropriate configuration based on any
        //criteria in the request message that can be matched against the server's
        //configuration, then formulate a response message containing the options
        //to be sent to the client.
        //@return a Reply DhcpMessage
        public DhcpV4Message ProcessMessage(IPAddress localAddress)
        {
            Monitor.Enter(_lock);
            try
            {
                //設定ServerId為管理IP
                _dhcpV4ServerIdOption.SetIpAddress(localAddress.ToString());

                if (!PreProcess())
                {
                    return(null);
                }
                // build a reply message using the local and remote sockets from the request
                _replyMsg = new DhcpV4Message(_requestMsg.GetLocalAddress(), _requestMsg.GetRemoteAddress());

                _replyMsg.SetOp(DhcpConstants.V4_OP_REPLY);
                // copy fields from request to reply
                _replyMsg.SetHtype(_requestMsg.GetHtype());
                _replyMsg.SetHlen(_requestMsg.GetHlen());
                _replyMsg.SetTransactionId(_requestMsg.GetTransactionId());
                _replyMsg.SetFlags(_requestMsg.GetFlags());
                _replyMsg.SetGiAddr(_requestMsg.GetGiAddr());
                _replyMsg.SetChAddr(_requestMsg.GetChAddr());
                // MUST put Server Identifier in REPLY message
                _replyMsg.PutDhcpOption(_dhcpV4ServerIdOption);

                if (!Process())
                {
                    log.Warn("Message dropped by processor");
                    return(null);
                }
            }
            catch (Exception ex)
            {
                log.WarnFormat("BaseDhcpV4Processor ProcessMessage Faile. exMessage:{0} exStackTrace:{1}", ex.Message, ex.StackTrace);
                return(null);
            }
            finally
            {
                if (!PostProcess())
                {
                    log.Warn("Message dropped by postProcess");
                    _replyMsg = null;
                }
                Monitor.Exit(_lock);
            }
            return(_replyMsg);
        }
        public static DhcpV4Message HandleMessage(IPAddress localAddress, DhcpV4Message dhcpMessage)
        {
            DhcpV4Message replyMessage = null;

            if (dhcpMessage.GetOp() == DhcpConstants.V4_OP_REQUEST)
            {
                IPAddress linkAddress = null;
                if (dhcpMessage.GetGiAddr().Equals(DhcpConstants.ZEROADDR_V4))
                {
                    linkAddress = localAddress;
                    //log.Info("Handling client request on local client link address: " +
                    //        linkAddress.ToString());
                }
                else
                {
                    linkAddress = dhcpMessage.GetGiAddr();
                    log.Info("Handling client request on remote client link address: " +
                             linkAddress.ToString());
                }
                DhcpV4MsgTypeOption msgTypeOption = (DhcpV4MsgTypeOption)
                                                    dhcpMessage.GetDhcpOption(DhcpConstants.V4OPTION_MESSAGE_TYPE);
                if (msgTypeOption != null)
                {
                    short msgType = msgTypeOption.GetUnsignedByte();
                    DhcpV4MessageProcessor processor = null;
                    switch (msgType)
                    {
                    case DhcpConstants.V4MESSAGE_TYPE_DISCOVER:
                        processor = new DhcpV4DiscoverProcessor(dhcpMessage, linkAddress);
                        break;

                    case DhcpConstants.V4MESSAGE_TYPE_REQUEST:
                        processor = new DhcpV4RequestProcessor(dhcpMessage, linkAddress);
                        break;

                    case DhcpConstants.V4MESSAGE_TYPE_DECLINE:
                        processor = new DhcpV4DeclineProcessor(dhcpMessage, linkAddress);
                        break;

                    case DhcpConstants.V4MESSAGE_TYPE_RELEASE:
                        processor = new DhcpV4ReleaseProcessor(dhcpMessage, linkAddress);
                        break;

                    case DhcpConstants.V4MESSAGE_TYPE_INFORM:
                        processor = new DhcpV4InformProcessor(dhcpMessage, linkAddress);
                        break;

                    default:
                        log.Error("Unknown message type.");
                        break;
                    }
                    if (processor != null)
                    {
                        return(processor.ProcessMessage(localAddress));
                    }
                    else
                    {
                        log.Error("No processor found for message type: " + msgType);
                    }
                }
                else
                {
                    log.Error("No message type option found in request.");
                }
                return(null);
            }
            else
            {
                log.Error("Unsupported op code: " + dhcpMessage.GetOp());
            }
            return(replyMessage);
        }