Exemplo n.º 1
0
        private static void V4Request()
        {
            var V4Message = new DhcpV4Message(IPAddress.Any,
                                              new IPEndPoint(IPAddress.Parse("192.168.61.48"), DhcpConstants.V4_SERVER_PORT));

            V4Message.SetOp((short)DhcpConstants.V4_OP_REQUEST); //V4_OP_REQUEST
            V4Message.SetGiAddr(IPAddress.Any);
            V4Message.SetChAddr(PhysicalAddress.Parse("9C-EB-E8-28-92-D4").GetAddressBytes());

            DhcpV4MsgTypeOption msgTypeOption = new DhcpV4MsgTypeOption();

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

            V4Message.PutDhcpOption(new DhcpV4RequestedIpAddressOption(
                                        new v4RequestedIpAddressOption()
            {
                code      = DhcpConstants.V4OPTION_REQUESTED_IP,
                ipAddress = "192.168.61.140"
            }));
            V4Message.SetDhcpV4ServerIdOption(
                new DhcpV4ServerIdOption(
                    new v4ServerIdOption()
            {
                code      = DhcpConstants.V4OPTION_SERVERID,
                ipAddress = "192.168.61.48"
            }));
            var message = DhcpV4MessageHandler.HandleMessage(
                IPAddress.Parse("192.168.61.48"), V4Message);

            Console.WriteLine(message.ToString());
        }
Exemplo n.º 2
0
        //Populate v4 options.
        //@param link the link
        //@param configObj the config object or null if none
        protected void PopulateV4Reply(DhcpLink dhcpLink, DhcpV4OptionConfigObject configObj)
        {
            string sname = DhcpServerPolicies.EffectivePolicy(_requestMsg, configObj,
                                                              dhcpLink.GetLink(), Property.V4_HEADER_SNAME);

            if (!String.IsNullOrEmpty(sname))
            {
                _replyMsg.SetsName(sname);
            }

            string filename = DhcpServerPolicies.EffectivePolicy(_requestMsg, configObj,
                                                                 dhcpLink.GetLink(), Property.V4_HEADER_FILENAME);

            if (!String.IsNullOrEmpty(filename))
            {
                _replyMsg.SetFile(filename);
            }

            Dictionary <int, DhcpOption> optionMap =
                _dhcpServerConfig.EffectiveV4AddrOptions(_requestMsg, dhcpLink, configObj);

            if (DhcpServerPolicies.EffectivePolicyAsBoolean(configObj,
                                                            dhcpLink.GetLink(), Property.SEND_REQUESTED_OPTIONS_ONLY))
            {
                optionMap = RequestedOptions(optionMap, _requestMsg);
            }
            _replyMsg.PutAllDhcpOptions(optionMap);

            // copy the relay agent info option from request to reply
            // in order to echo option back to router as required
            if (_requestMsg.HasOption(DhcpConstants.V4OPTION_RELAY_INFO))
            {
                _requestMsg.PutDhcpOption(_requestMsg.GetDhcpOption(DhcpConstants.V4OPTION_RELAY_INFO));
            }
        }
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);
            }
Exemplo n.º 4
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.º 5
0
            /**
             * Builds the discover message.
             *
             * @return the  dhcp message
             */
            private DhcpV4Message BuildDiscoverMessage(byte[] chAddr)
            {
                DhcpV4Message msg = new DhcpV4Message(IPAddress.Parse("0.0.0.0"), new IPEndPoint(serverAddr, serverPort));

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

                DhcpV4MsgTypeOption msgTypeOption = new DhcpV4MsgTypeOption();

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

                msg.PutDhcpOption(msgTypeOption);

                return(msg);
            }
Exemplo n.º 6
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.º 7
0
        /**
         * Adds the v4 binding to reply.
         *
         * @param clientLink the client link
         * @param binding the binding
         */
        protected void AddBindingToReply(DhcpLink clientLink, Binding binding)
        {
            HashSet <BindingObject> bindingObjs = binding.GetBindingObjects();

            if ((bindingObjs != null) && bindingObjs.Count > 0)
            {
                if (bindingObjs.Count == 1)
                {
                    BindingObject bindingObj = bindingObjs.First();
                    IPAddress     inetAddr   = bindingObj.GetIpAddress();
                    if (inetAddr != null)
                    {
                        _replyMsg.SetYiAddr(inetAddr);
                        // must be an DhcpV4OptionConfigObject for v4 binding
                        DhcpV4OptionConfigObject configObj =
                            (DhcpV4OptionConfigObject)bindingObj.GetConfigObj();
                        if (configObj != null)
                        {
                            long preferred = configObj.GetPreferredLifetime();
                            DhcpV4LeaseTimeOption dhcpV4LeaseTimeOption = new DhcpV4LeaseTimeOption();
                            dhcpV4LeaseTimeOption.SetUnsignedInt(preferred);
                            _replyMsg.PutDhcpOption(dhcpV4LeaseTimeOption);
                            PopulateV4Reply(clientLink, configObj);
                            //TODO when do actually start the timer?  currently, two get
                            //     created - one during advertise, one during reply
                            //     policy to allow real-time expiration?
                            //					bp.startExpireTimerTask(bindingAddr, iaAddrOption.getValidLifetime());
                        }
                        else
                        {
                            log.Error("Null binding pool in binding: " + binding.ToString());
                        }
                    }
                    else
                    {
                        log.Error("Null address in binding: " + binding.ToString());
                    }
                }
                else
                {
                    log.Error("Expected only one bindingObject in v4 Binding, but found " +
                              bindingObjs.Count + "bindingObjects");
                }
            }
            else
            {
                log.Error("No V4 bindings in binding object!");
            }
        }
Exemplo n.º 8
0
        private static void V4Discover()
        {
            var V4Message = new DhcpV4Message(IPAddress.Any,
                                              new IPEndPoint(IPAddress.Parse("192.168.61.48"), DhcpConstants.V4_SERVER_PORT));

            V4Message.SetOp((short)DhcpConstants.V4_OP_REQUEST); //V4_OP_REQUEST
            V4Message.SetGiAddr(IPAddress.Any);
            V4Message.SetChAddr(PhysicalAddress.Parse("9C-EB-E8-28-92-D4").GetAddressBytes());
            V4Message.SetHlen(6);
            V4Message.SetHtype((byte)6);
            V4Message.SetTransactionId(-1729018559);

            DhcpV4MsgTypeOption msgTypeOption = new DhcpV4MsgTypeOption();

            msgTypeOption.SetUnsignedByte((short)DhcpConstants.V4MESSAGE_TYPE_DISCOVER);
            V4Message.PutDhcpOption(msgTypeOption);

            var message = DhcpV4MessageHandler.HandleMessage(
                IPAddress.Parse("192.168.61.48"), V4Message);

            Console.WriteLine(message.ToString());
        }