Пример #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());
        }
Пример #2
0
        public void SetMessageType(short msgType)
        {
            DhcpV4MsgTypeOption msgTypeOption = new DhcpV4MsgTypeOption();

            msgTypeOption.SetUnsignedByte(msgType);
            PutDhcpOption(msgTypeOption);
        }
Пример #3
0
        public short GetMessageType()
        {
            DhcpV4MsgTypeOption msgType = (DhcpV4MsgTypeOption)
                                          _dhcpOptions.Get(DhcpConstants.V4OPTION_MESSAGE_TYPE);

            if (msgType != null)
            {
                return(msgType.GetUnsignedByte());
            }
            return(0);
        }
Пример #4
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);
            }
Пример #5
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);
            }
Пример #6
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());
        }
Пример #7
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);
            }
        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);
        }