Ejemplo n.º 1
0
    private void receiveCallback(Packet packet)
    {
        try
        {
            IpV4Datagram ipPacket    = packet.Ethernet.IpV4;
            UdpDatagram  udpDatagram = ipPacket.Udp;
            Datagram     datagram    = udpDatagram.Payload;

            if (udpDatagram.SourcePort.Equals(68) & udpDatagram.DestinationPort.Equals(67))
            {
                DHCPv4Packet dhcpv4Packet = new DHCPv4Packet();
                if (dhcpv4Packet.parsePacket(datagram.ToArray()))
                {
                    List <DHCPv4Option> list = new DHCPv4Option().parseDhcpOptions(dhcpv4Packet.dhcpOptions);
                    foreach (DHCPv4Option dhcpMessageTypeOption in list)
                    {
                        if (dhcpMessageTypeOption.optionIdBytes.Equals(0x35))
                        {
                            switch (dhcpMessageTypeOption.optionValue[0])
                            {
                            //--Packet is a Discover
                            case 0x01:
                                Console.WriteLine("Service received:\t" + packet.Ethernet.Destination + "\tDISCOVER\txid: " + BitConverter.ToString(dhcpv4Packet.xid));
                                sendDhcpOffer(new MacAddress(inter.getMacAddress()), packet.Ethernet.Source, dhcpv4Packet.xid, dhcpv4Packet.secs);

                                break;

                            //--Packet is an Request
                            case 0x03:
                                foreach (DHCPv4Option dhcpServerIdentifierOption in list)
                                {
                                    //--DHCP contains Server-Identifier-Option.
                                    if (dhcpServerIdentifierOption.optionIdBytes.Equals(0x36))
                                    {
                                        //--DHCP-Server-Identifier equals IP-Address of DHCP-Server.
                                        if (BitConverter.ToInt32(dhcpServerIdentifierOption.optionValue, 0).Equals(BitConverter.ToInt32(inter.getIPAddress().GetAddressBytes(), 0)))
                                        {
                                            Console.WriteLine("Service received:\t" + packet.Ethernet.Destination + "\tREQUEST\t\txid: " + BitConverter.ToString(dhcpv4Packet.xid) + "\tSID: " + BitConverter.ToString(dhcpServerIdentifierOption.optionValue, 0));
                                            sendDhcpAck(new MacAddress(inter.getMacAddress()), packet.Ethernet.Source, dhcpv4Packet.xid, dhcpv4Packet.secs);
                                        }
                                        else
                                        {
                                            Console.WriteLine("Client preferes other DHCP-Server!");
                                        }
                                    }
                                }
                                break;
                            }
                        }
                    }
                }
                else
                {
                    Console.WriteLine("The DHCP-Message could not be parsed...");
                }
            }
        }
        catch (Exception) { }
    }
Ejemplo n.º 2
0
    public Packet buildDhcpOffer(MacAddress pSourceMacAddress, MacAddress pDestinationMacAddress, byte[] pTransactionId, byte[] pSecs)
    {
        IPAddress newClientIPAddress = IPAddress.Parse("0.0.0.0");
        bool      clientFound        = false;

        if (clientList.Count != 0)
        {
            foreach (Client item in clientList)
            {
                if (StructuralComparisons.StructuralEqualityComparer.Equals(item.macAddress, pDestinationMacAddress))
                {
                    newClientIPAddress = item.ipaddress;
                    clientFound        = true;

                    Debug.WriteLine("Listed Client has been found with IP-Binding: " + newClientIPAddress.ToString());
                }
            }
        }


        if (!clientFound)
        {
            newClientIPAddress = addressPool.getFreeIPAddress();
            Client client = new Client(newClientIPAddress.ToString(), newClientIPAddress, pTransactionId, pDestinationMacAddress);
            clientList.Add(client);

            Debug.WriteLine("Listed Client has been generated with IP-Binding: " + newClientIPAddress.ToString() + " and xid: " + BitConverter.ToString(pTransactionId) + " and MacAddress: " + pDestinationMacAddress.ToString());
        }

        IPAddress ipaddress = inter.getIPAddress();

        DHCPv4Option dhcpMessageTypeOption = new DHCPv4Option
        {
            optionId     = DHCPv4OptionIds.DhcpMessageType,
            optionLength = 0x01,
            optionValue  = new byte[] { 0x02 },
        };

        DHCPv4Option dhcpServerIdentifierOption = new DHCPv4Option
        {
            optionId     = DHCPv4OptionIds.ServerIdentifier,
            optionLength = 0x04,
            optionValue  = ipaddress.GetAddressBytes(),
        };

        DHCPv4Packet dhcpPacket = new DHCPv4Packet
        {
            op          = 0x02,
            htype       = 0x01,
            hlen        = 0x06,
            xid         = pTransactionId,
            secs        = pSecs,
            ciaddr      = new byte[] { 0x00, 0x00, 0x00, 0x00 },
            yiaddr      = newClientIPAddress.GetAddressBytes(),
            siaddr      = ipaddress.GetAddressBytes(),
            chaddr      = PhysicalAddress.Parse(pDestinationMacAddress.ToString().Replace(":", "-")).GetAddressBytes(),
            dhcpOptions = dhcpMessageTypeOption.buildDhcpOption().Concat(dhcpServerIdentifierOption.buildDhcpOption()).ToArray(),
        };

        EthernetLayer ethernetLayer = new EthernetLayer
        {
            Source      = pSourceMacAddress,
            Destination = pDestinationMacAddress
        };

        IpV4Layer ipV4Layer = new IpV4Layer
        {
            Source             = new IpV4Address(ipaddress.ToString()),
            CurrentDestination = new IpV4Address(newClientIPAddress.ToString()),
            Ttl = 128,
        };

        UdpLayer udpLayer = new UdpLayer
        {
            SourcePort      = (ushort)67,
            DestinationPort = (ushort)68,
        };

        PayloadLayer payloadLayer = new PayloadLayer
        {
            Data = new Datagram(dhcpPacket.buildPacket()),
        };

        PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, udpLayer, payloadLayer);

        return(builder.Build(DateTime.Now));
    }
Ejemplo n.º 3
0
    public Packet buildDhcpAck(MacAddress pSourceMacAddress, MacAddress pDestinationMacAddress, byte[] pTransactionId, byte[] pSecs)
    {
        IPAddress newClientIPAddress = IPAddress.Parse("0.0.0.0");
        bool      clientFound        = false;

        if (clientList.Count != 0)
        {
            foreach (Client item in clientList)
            {
                if (StructuralComparisons.StructuralEqualityComparer.Equals(item.macAddress, pDestinationMacAddress))
                {
                    newClientIPAddress = item.ipaddress;
                    clientFound        = true;

                    Debug.WriteLine("Listed Client has been found with IP-Binding: " + newClientIPAddress.ToString());
                }
            }
        }

        if (!clientFound)
        {
            Debug.WriteLine("Server sends a NAK. There is no TransactionId paired to a leased IP-Address! The Server did not received a DISCOVER from the Client");
        }

        IPAddress ipaddress  = inter.getIPAddress();
        IPAddress subnetmask = inter.getNetmask();

        DHCPv4Option dhcpMessageTypeOption = new DHCPv4Option
        {
            optionId     = DHCPv4OptionIds.DhcpMessageType,
            optionLength = 0x01,
            optionValue  = new byte[] { 0x05 },
        };

        DHCPv4Option dhcpServerIdentifierOption = new DHCPv4Option
        {
            optionId     = DHCPv4OptionIds.ServerIdentifier,
            optionLength = 0x04,
            optionValue  = ipaddress.GetAddressBytes(),
        };

        DHCPv4Option ipAddressLeaseTimeOption = new DHCPv4Option
        {
            optionId     = DHCPv4OptionIds.IpAddressLeaseTime,
            optionLength = 0x04,
            optionValue  = new byte[] { 0x00, 0x0d, 0x2f, 0x00 },
        };

        DHCPv4Option renewalTimeValueOption = new DHCPv4Option
        {
            optionId     = DHCPv4OptionIds.RenewalTimeValue,
            optionLength = 0x04,
            optionValue  = new byte[] { 0x00, 0x06, 0x97, 0x80 },
        };

        DHCPv4Option rebindTimeValueOption = new DHCPv4Option
        {
            optionId     = DHCPv4OptionIds.RebindingTimeValue,
            optionLength = 0x04,
            optionValue  = new byte[] { 0x00, 0x0b, 0x89, 0x20 },
        };


        DHCPv4Option subnetMaskOption = new DHCPv4Option
        {
            optionId     = DHCPv4OptionIds.Subnetmask,
            optionLength = 0x04,
            optionValue  = subnetmask.GetAddressBytes(),
        };

        DHCPv4Option routerOption = new DHCPv4Option
        {
            optionId     = DHCPv4OptionIds.Router,
            optionLength = 0x04,
            optionValue  = addressPool.getGatewayIpAddress().GetAddressBytes(),
        };

        DHCPv4Option domainNameServerOption = new DHCPv4Option
        {
            optionId     = DHCPv4OptionIds.DomainNameServer,
            optionLength = 0x04,
            optionValue  = ipaddress.GetAddressBytes(),
        };

        DHCPv4Option domainNameOption = new DHCPv4Option
        {
            optionId     = DHCPv4OptionIds.DomainName,
            optionLength = (byte)addressPool.getDomainName().Length,
            optionValue  = Encoding.ASCII.GetBytes(addressPool.getDomainName()),
        };

        DHCPv4Packet dhcpPacket = new DHCPv4Packet
        {
            op          = 0x02,
            htype       = 0x01,
            hlen        = 0x06,
            xid         = pTransactionId,
            secs        = pSecs,
            ciaddr      = new byte[] { 0x00, 0x00, 0x00, 0x00 },
            yiaddr      = newClientIPAddress.GetAddressBytes(),
            siaddr      = ipaddress.GetAddressBytes(),
            chaddr      = PhysicalAddress.Parse(pDestinationMacAddress.ToString().Replace(":", "-")).GetAddressBytes(),
            dhcpOptions = dhcpMessageTypeOption.buildDhcpOption().Concat(dhcpServerIdentifierOption.buildDhcpOption()).Concat(ipAddressLeaseTimeOption.buildDhcpOption()).Concat(renewalTimeValueOption.buildDhcpOption()).Concat(rebindTimeValueOption.buildDhcpOption()).Concat(subnetMaskOption.buildDhcpOption()).Concat(routerOption.buildDhcpOption()).Concat(domainNameServerOption.buildDhcpOption()).Concat(domainNameOption.buildDhcpOption()).ToArray(),
        };

        EthernetLayer ethernetLayer = new EthernetLayer
        {
            Source      = pSourceMacAddress,
            Destination = pDestinationMacAddress
        };

        IpV4Layer ipV4Layer = new IpV4Layer
        {
            Source             = new IpV4Address(ipaddress.ToString()),
            CurrentDestination = new IpV4Address(newClientIPAddress.ToString()),
            Ttl = 128,
        };

        UdpLayer udpLayer = new UdpLayer
        {
            SourcePort      = (ushort)67,
            DestinationPort = (ushort)68,
        };

        PayloadLayer payloadLayer = new PayloadLayer
        {
            Data = new Datagram(dhcpPacket.buildPacket()),
        };

        PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, udpLayer, payloadLayer);

        return(builder.Build(DateTime.Now));
    }