Exemplo n.º 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);
        }
Exemplo n.º 2
0
            private DhcpV4Message BuildReleaseMessage(DhcpV4Message ack)
            {
                DhcpV4Message msg = new DhcpV4Message(null, new IPEndPoint(serverAddr, serverPort));

                msg.SetOp((short)DhcpConstants.V4_OP_REQUEST);
                msg.SetTransactionId(ack.GetTransactionId());
                msg.SetHtype((short)1); // ethernet
                msg.SetHlen((byte)6);
                msg.SetChAddr(ack.GetChAddr());
                msg.SetGiAddr(clientAddr);  // look like a relay to the DHCP server
                msg.SetCiAddr(ack.GetYiAddr());

                DhcpV4MsgTypeOption msgTypeOption = new DhcpV4MsgTypeOption();

                msgTypeOption.SetUnsignedByte((short)DhcpConstants.V4MESSAGE_TYPE_RELEASE);

                msg.PutDhcpOption(msgTypeOption);

                return(msg);
            }
Exemplo n.º 3
0
            private DhcpV4Message BuildRequestMessage(DhcpV4Message offer)
            {
                DhcpV4Message msg = new DhcpV4Message(IPAddress.Parse("0.0.0.0"), new IPEndPoint(serverAddr, serverPort));

                msg.SetOp((short)DhcpConstants.V4_OP_REQUEST);
                msg.SetTransactionId(offer.GetTransactionId());
                msg.SetHtype((short)1); // ethernet
                msg.SetHlen((byte)6);
                msg.SetChAddr(offer.GetChAddr());
                msg.SetGiAddr(clientAddr);  // look like a relay to the DHCP server

                DhcpV4MsgTypeOption msgTypeOption = new DhcpV4MsgTypeOption();

                msgTypeOption.SetUnsignedByte((short)DhcpConstants.V4MESSAGE_TYPE_REQUEST);

                msg.PutDhcpOption(msgTypeOption);

                DhcpV4RequestedIpAddressOption reqIpOption = new DhcpV4RequestedIpAddressOption();

                reqIpOption.SetIpAddress(offer.GetYiAddr().ToString());
                msg.PutDhcpOption(reqIpOption);

                return(msg);
            }