Ejemplo n.º 1
0
        private static IPv4Packet BuildDhcpDiscoverMessage(NetworkInterface inter, ushort packetId, uint dhcpTransactionId)
        {
            var hardwareAddress = new HardwareAddress(HardwareAddressType.Ethernet,
                                                      inter.GetPhysicalAddress().GetAddressBytes());

            var dhcpPacket = new DhcpPacket();

            dhcpPacket.Operation       = DhcpOperation.Request;
            dhcpPacket.MessageType     = DhcpMessageType.Discover;
            dhcpPacket.TransactionId   = dhcpTransactionId;
            dhcpPacket.HardwareAddress = hardwareAddress;

            var dhcpUdpPacket = new UdpPacket();

            dhcpUdpPacket.SourcePort      = 68;
            dhcpUdpPacket.DestinationPort = 67;
            dhcpUdpPacket.Payload         = dhcpPacket;

            var dhcpIpPacket = new IPv4Packet();

            dhcpIpPacket.Source        = IPAddress.Any;
            dhcpIpPacket.Destination   = IPAddress.Broadcast;
            dhcpIpPacket.Identifiation = packetId;
            dhcpIpPacket.Payload       = dhcpUdpPacket;
            return(dhcpIpPacket);
        }
Ejemplo n.º 2
0
        private static IPv4Packet BuildDhcpRebindMessage(DhcpOffer offer, NetworkInterface inter, ushort packetId, uint dhcpTransactionId)
        {
            var hardwareAddress = new HardwareAddress(HardwareAddressType.Ethernet,
                                                      inter.GetPhysicalAddress().GetAddressBytes());

            var dhcpPacket = new DhcpPacket();

            dhcpPacket.Operation       = DhcpOperation.Request;
            dhcpPacket.MessageType     = DhcpMessageType.Request;
            dhcpPacket.TransactionId   = dhcpTransactionId;
            dhcpPacket.HardwareAddress = hardwareAddress;
            dhcpPacket.ClientAddress   = offer.ClientAddress;

            dhcpPacket.Options.Add(new DhcpClientIdentifier(hardwareAddress));

            var dhcpUdpPacket = new UdpPacket();

            dhcpUdpPacket.SourcePort      = 68;
            dhcpUdpPacket.DestinationPort = 67;
            dhcpUdpPacket.Payload         = dhcpPacket;

            var dhcpIpPacket = new IPv4Packet();

            dhcpIpPacket.Source        = offer.ClientAddress;
            dhcpIpPacket.Destination   = offer.DhcpServer;
            dhcpIpPacket.Identifiation = packetId;
            dhcpIpPacket.Payload       = dhcpUdpPacket;

            return(dhcpIpPacket);
        }
Ejemplo n.º 3
0
        public static DhcpPacket CreateFromRaw(byte[] rawPacket)
        {
            DhcpPacket packet = new DhcpPacket();

            using (var reader = new BinaryReader(new MemoryStream(rawPacket)))
            {
                packet.MessageType = (DhcpMessageType)reader.ReadByte();
                HardwareAddressType hardwareAddressType = (HardwareAddressType)reader.ReadByte();
                byte hardwareAddressLength = reader.ReadByte();
                packet.Hops              = reader.ReadByte();
                packet.TransactionId     = (uint)IPAddress.NetworkToHostOrder(reader.ReadInt32());
                packet.SecondsElapsed    = (ushort)IPAddress.NetworkToHostOrder(reader.ReadInt16());
                packet.Flags             = (ushort)IPAddress.NetworkToHostOrder(reader.ReadInt16());
                packet.ClientAddress     = new IPAddress(reader.ReadBytes(4));
                packet.YourClientAddress = new IPAddress(reader.ReadBytes(4));
                packet.NextServerAddress = new IPAddress(reader.ReadBytes(4));
                packet.RelayAgentAddress = new IPAddress(reader.ReadBytes(4));
                packet.HardwareAddress   = new HardwareAddress(hardwareAddressType,
                                                               reader.ReadBytes(hardwareAddressLength));
                reader.ReadBytes(16 - hardwareAddressLength); // padding
                packet.ServerName   = Encoding.ASCII.GetString(reader.ReadBytes(64));
                packet.BootFileName = Encoding.ASCII.GetString(reader.ReadBytes(128));

                int packetMagicCookie = IPAddress.NetworkToHostOrder(reader.ReadInt32());
                if (packetMagicCookie != MagicCookie)
                {
                    throw new InvalidDataException("Magic cookie not found!");
                }

                byte messageType = reader.ReadByte();
                if (messageType != 53)
                {
                    throw new InvalidDataException("Options should start with Message type!");
                }
                reader.ReadByte(); // length
                packet.MessageType = (DhcpMessageType)reader.ReadByte();

                var  optionFactory = new DhcpOptionFactory();
                byte optionType    = reader.ReadByte();
                while (optionType != 255)
                {
                    DhcpOptionType type       = (DhcpOptionType)optionType;
                    byte           length     = reader.ReadByte();
                    byte[]         optionData = reader.ReadBytes(length);

                    DhcpOption option = optionFactory.CreateFromRaw(type, optionData);
                    if (option != null)
                    {
                        packet.Options.Add(option);
                    }
                    else
                    {
                        Console.WriteLine($"WARN: Unsupported option type: {type}");
                    }
                    optionType = reader.ReadByte();
                }
            }
            return(packet);
        }
Ejemplo n.º 4
0
        public IApplicationPacket CreateFromRaw(ushort port, byte[] rawPacket)
        {
            switch (port)
            {
            case 68:
                return(DhcpPacket.CreateFromRaw(rawPacket));

            default:
                return(new UnknownPayload(rawPacket));
            }
        }
Ejemplo n.º 5
0
        public static DhcpOffer FromDhcpPacket(DhcpPacket packet)
        {
            if (packet.MessageType != DhcpMessageType.Offer)
            {
                return(null);
            }

            return(new DhcpOffer
            {
                DhcpServer = packet.GetOption <DhcpServerIdentifierOption>().ServerAddress,
                ClientAddress = packet.YourClientAddress,
                Gateway = packet.GetOption <DhcpRouterOption>()?.Routers[0],
                DnsServer = packet.GetOption <DhcpDomainNameServerOption>()?.DomainNameServers[0],
                Domain = packet.GetOption <DhcpDomainNameOption>()?.DomainName,
                SubnetMask = packet.GetOption <DhcpSubnetMaskOption>()?.Mask,
                RelayAgnetAddress = packet.RelayAgentAddress
            });
        }