Esempio n. 1
0
 // Network Layer
 public bool SendPacket(MAC adapter, IPv4Header data)
 {
     if (NetworkInterfaces.ContainsKey(adapter))
     {
         NetworkInterfaces[adapter].SendPacket(data.ToBytes());
         return(true);
     }
     else
     {
         return(false);
     }
 }
Esempio n. 2
0
        public void ToBytesAndBack1()
        {
            Optionlist ol = new Optionlist();

            ol.Add(new DHCPOption(Tag.dhcpServerID, IP.Broadcast.ToBytes()));
            ol.Add(new DHCPOption(Tag.router, IP.Broadcast.ToBytes()));
            ol.Add(new DHCPOption(Tag.subnetmask, IP.Zero.ToBytes()));
            ol.Add(new DHCPOption(Tag.domainserver, IP.Broadcast.ToBytes()));



            DHCPDatagram DHCP1 = new DHCPDatagram(1, 78974564, 1, 10, 0, 0, true, IP.Broadcast, null, null, null, MAC.Random(), ol);
            UDPHeader    UDP1  = new UDPHeader(100, 101, DHCP1.ToBytes());
            IPv4Header   ipv41 = new IPv4Header(1, false, false, 30, IPv4Header.ProtocolType.UDP, IP.Zero, IP.Broadcast, new byte[0], UDP1.ToBytes());

            byte[]       Bytes = ipv41.ToBytes();
            IPv4Header   ipv42 = new IPv4Header(Bytes);
            UDPHeader    UDP2  = new UDPHeader(ipv42.Datagram);
            DHCPDatagram DHCP2 = new DHCPDatagram(UDP2.Datagram);

            Assert.Equal(ipv41.ToBytes(), ipv42.ToBytes());
            Assert.Equal(UDP1.ToBytes(), UDP2.ToBytes());
            Assert.Equal(DHCP1.ToBytes(), DHCP2.ToBytes());
        }
Esempio n. 3
0
        public void ToBytes()
        {
            byte[] expected, actual, tmp;

            expected = new byte[20]
            {
                0x45, 0x00, 0x00, 0x28, 0x6e, 0x9c, 0x40, 0x00, 0x80, 0x06, 0x77, 0xd7, 0x0a, 0x90, 0xe3, 0x64, 0x28, 0x43, 0xfe, 0x24
            };

            IPv4Header ipv4 = new IPv4Header(0x6E9C, true, false, 128, IPv4Header.ProtocolType.TCP, IP.Parse("10.144.227.100"), IP.Parse("40.67.254.36"), new byte[0], new byte[20]);

            tmp = ipv4.ToBytes();

            actual = new byte[20];
            Array.Copy(tmp, actual, 20);

            Assert.Equal(expected, actual);
        }
Esempio n. 4
0
        protected virtual void handleICMPPacket(IPv4Header datagram, NetworkInterface a)
        {
            switch (datagram.Datagram[0])
            {
            case 0:     // Echo Replys
            {
                ICMPEchoRequestReply header = new ICMPEchoRequestReply(datagram.Datagram);
                OnICMPPacket?.Invoke(datagram, header, header.Code, a);
            }
            break;

            case 8:     // Echo Request
                if (respondToEcho)
                {
                    ICMPEchoRequestReply header = new ICMPEchoRequestReply(datagram.Datagram);
                    ICMPEchoRequestReply icmp   = new ICMPEchoRequestReply(0, header.Identifier, (UInt16)(header.SequenceNumber + 1));
                    IPv4Header           ipv4   = new IPv4Header(datagram.Identification, false, false, 255, IPv4Header.ProtocolType.ICMP, a.LocalIP, datagram.SourceAddress, null, icmp.ToBytes());

                    a.SendPacket(ipv4.ToBytes());
                }
                break;
            }
        }
Esempio n. 5
0
            public void OnTick(uint tick)
            {
                switch (state)
                {
                case State.INIT:
                    if (failures <= 10)
                    {
                        offeredIP       = null;
                        DHCPServerIP    = null;
                        Offer           = null;
                        RequestResponce = null;


                        // Create DHCP Options
                        Optionlist ol = new Optionlist();
                        ol.Add(new DHCPOption(Tag.dhcpMsgType, new byte[1] {
                            (byte)DHCPOption.MsgType.DHCPDISCOVER
                        }));
                        // Requesting Spesific IP
                        if (a.LocalIP != null)
                        {
                            ol.Add(new DHCPOption(Tag.addressRequest, a.LocalIP.ToBytes()));
                        }
                        // Asking for Spesific Params back
                        ol.Add(new DHCPOption(Tag.paramaterList, paramlist.ToArray()));

                        // create DHCP Datagram
                        DHCPDatagram DhcpDatagram = new DHCPDatagram(0, xid, ClientMAC: a.MyMACAddress, Broadcast: true, options: ol);

                        // UDP and IPv4 Headers
                        UDPHeader  UDP  = new UDPHeader(68, 67, DhcpDatagram.ToBytes());
                        IPv4Header IPv4 = IPv4Header.DefaultUDPWrapper(IP.Zero, IP.Broadcast, UDP.ToBytes(), 32);
                        state = State.SELECTING;

                        Client.Log("Sending Discover Packet");

                        // Send IPv4 Packet
                        a.SendPacket(IPv4.ToBytes());

                        T3 = 100;
                    }
                    break;

                case State.SELECTING:
                    if (Offer != null)
                    {
                        if (Offer.yiaddr != null)
                        {
                            offeredIP    = Offer.yiaddr;
                            DHCPServerIP = null;

                            foreach (DHCPOption o in Offer.options)
                            {
                                if (o.tag == Tag.dhcpServerID)
                                {
                                    DHCPServerIP = new IP(o.data);
                                }
                            }

                            Optionlist ol = new Optionlist()
                            {
                                new DHCPOption(Tag.dhcpMsgType, new byte[1] {
                                    (byte)DHCPOption.MsgType.DHCPREQUEST
                                }),
                                new DHCPOption(Tag.addressRequest, offeredIP.ToBytes())
                            };

                            if (DHCPServerIP != null)
                            {
                                ol.Add(new DHCPOption(Tag.dhcpServerID, DHCPServerIP.ToBytes()));
                            }

                            ol.Add(new DHCPOption(Tag.paramaterList, paramlist.ToArray()));



                            DHCPDatagram DhcpDatagram = new DHCPDatagram(0, xid, ClientMAC: a.MyMACAddress, options: ol);
                            UDPHeader    UDP          = new UDPHeader(68, 67, DhcpDatagram.ToBytes());
                            IP           server       = IP.Broadcast;
                            if (Offer.siaddr != null)
                            {
                                server = Offer.siaddr;
                            }
                            IPv4Header IPv4 = IPv4Header.DefaultUDPWrapper(IP.Zero, server, UDP.ToBytes(), 32);

                            a.SendPacket(IPv4.ToBytes());
                            Client.Log("Sending Request Packet");

                            state = State.REQUESTING;
                        }
                    }
                    else
                    if (T3-- <= 0)
                    {
                        failures++;
                        state = State.INIT;
                    }
                    break;

                case State.REQUESTING:
                    if (RequestResponce != null)
                    {
                        foreach (DHCPOption o in RequestResponce.options)
                        {
                            if (o.tag == Tag.dhcpMsgType)
                            {
                                if (o.data[0] == (byte)DHCPOption.MsgType.DHCPNAK)
                                {
                                    Offer           = null;
                                    RequestResponce = null;
                                    state           = State.INIT;
                                    break;
                                }
                                else
                                if (o.data[0] == (byte)DHCPOption.MsgType.DHCPACK)
                                {
                                    foreach (DHCPOption op in RequestResponce.options)
                                    {
                                        switch (op.tag)
                                        {
                                        case Tag.addressTime:
                                            T1 = BitConverter.ToUInt32(op.data, 0);
                                            T2 = T1 - 10;
                                            break;

                                        case Tag.subnetmask:
                                            a.SubnetMask = new IP(op.data, 0);
                                            break;

                                        case Tag.router:
                                            a.DefaultGateway = new IP(op.data, 0);
                                            break;

                                        case Tag.domainserver:
                                            a.DNS = new IP(op.data, 0);
                                            break;
                                        }
                                    }
                                    a.LocalIP = RequestResponce.yiaddr;
                                    state     = State.BOUND;
                                    break;
                                }
                            }
                        }
                    }
                    break;

                case State.BOUND:
                    if (T2-- <= 0)
                    {
                        T1--;
                        Optionlist ol = new Optionlist()
                        {
                            new DHCPOption(Tag.dhcpMsgType, new byte[1] {
                                (byte)DHCPOption.MsgType.DHCPREQUEST
                            }),
                            new DHCPOption(Tag.addressRequest, a.LocalIP.ToBytes())
                        };

                        if (DHCPServerIP != null)
                        {
                            ol.Add(new DHCPOption(Tag.dhcpServerID, DHCPServerIP.ToBytes()));
                        }

                        ol.Add(new DHCPOption(Tag.paramaterList, paramlist.ToArray()));



                        DHCPDatagram DhcpDatagram = new DHCPDatagram(0, xid, ClientMAC: a.MyMACAddress, options: ol);
                        UDPHeader    UDP          = new UDPHeader(68, 67, DhcpDatagram.ToBytes());
                        IP           server       = IP.Broadcast;
                        if (Offer.siaddr != null)
                        {
                            server = Offer.siaddr;
                        }
                        IPv4Header IPv4 = IPv4Header.DefaultUDPWrapper(a.LocalIP, server, UDP.ToBytes(), 32);

                        RequestResponce = null;

                        a.SendPacket(IPv4.ToBytes());

                        state = State.RENEWING;
                    }
                    break;

                case State.RENEWING:
                    if (RequestResponce != null)
                    {
                        foreach (DHCPOption o in RequestResponce.options)
                        {
                            if (o.tag == Tag.dhcpMsgType)
                            {
                                if (o.data[0] == (byte)DHCPOption.MsgType.DHCPNAK)
                                {
                                    Offer = null;
                                    state = State.INIT;
                                    break;
                                }
                                else
                                if (o.data[0] == (byte)DHCPOption.MsgType.DHCPACK)
                                {
                                    foreach (DHCPOption op in RequestResponce.options)
                                    {
                                        switch (op.tag)
                                        {
                                        case Tag.addressTime:
                                            T1 = BitConverter.ToUInt32(op.data, 0);
                                            T2 = T1 - 200;
                                            break;

                                        case Tag.subnetmask:
                                            a.SubnetMask = new IP(op.data, 0);
                                            break;

                                        case Tag.router:
                                            a.DefaultGateway = new IP(op.data, 0);
                                            break;

                                        case Tag.domainserver:
                                            a.DNS = new IP(op.data, 0);
                                            break;
                                        }
                                    }
                                    a.LocalIP = RequestResponce.yiaddr;
                                    state     = State.BOUND;
                                    break;
                                }
                            }
                        }
                    }
                    else
                    if (T1-- <= 0)
                    {
                        Optionlist ol = new Optionlist()
                        {
                            new DHCPOption(Tag.dhcpMsgType, new byte[1] {
                                (byte)DHCPOption.MsgType.DHCPREQUEST
                            }),
                            new DHCPOption(Tag.addressRequest, a.LocalIP.ToBytes())
                        };

                        if (DHCPServerIP != null)
                        {
                            ol.Add(new DHCPOption(Tag.dhcpServerID, DHCPServerIP.ToBytes()));
                        }

                        ol.Add(new DHCPOption(Tag.paramaterList, paramlist.ToArray()));

                        DHCPDatagram DhcpDatagram = new DHCPDatagram(0, xid, ClientMAC: a.MyMACAddress, options: ol);
                        UDPHeader    UDP          = new UDPHeader(68, 67, DhcpDatagram.ToBytes());
                        IP           server       = IP.Broadcast;
                        if (Offer.siaddr != null)
                        {
                            server = Offer.siaddr;
                        }
                        IPv4Header IPv4 = IPv4Header.DefaultUDPWrapper(a.LocalIP, server, UDP.ToBytes(), 32);

                        state = State.REBINDING;
                        T3    = 200;

                        a.SendPacket(IPv4.ToBytes());
                    }
                    break;

                case State.REBINDING:
                    if (RequestResponce != null)
                    {
                        foreach (DHCPOption o in RequestResponce.options)
                        {
                            if (o.tag == Tag.dhcpMsgType)
                            {
                                if (o.data[0] == (byte)DHCPOption.MsgType.DHCPNAK)
                                {
                                    Offer = null;
                                    state = State.INIT;
                                    break;
                                }
                                else
                                if (o.data[0] == (byte)DHCPOption.MsgType.DHCPACK)
                                {
                                    foreach (DHCPOption op in RequestResponce.options)
                                    {
                                        switch (op.tag)
                                        {
                                        case Tag.addressTime:
                                            T1 = BitConverter.ToUInt32(op.data, 0);
                                            T2 = T1 - 200;
                                            break;

                                        case Tag.subnetmask:
                                            a.SubnetMask = new IP(op.data, 0);
                                            break;

                                        case Tag.router:
                                            a.DefaultGateway = new IP(op.data, 0);
                                            break;

                                        case Tag.domainserver:
                                            a.DNS = new IP(op.data, 0);
                                            break;
                                        }
                                    }
                                    a.LocalIP = RequestResponce.yiaddr;
                                    state     = State.BOUND;
                                    break;
                                }
                            }
                        }
                    }
                    else
                    if (T3-- <= 0)
                    {
                        state = State.INIT;
                    }
                    break;

                case State.INITREBOOT:
                    state = State.INIT;
                    break;

                case State.REBOOTING:
                    state = State.INIT;
                    break;
                }
            }
Esempio n. 6
0
        public void packet(IPv4Header ipv4, UDPHeader udp, UInt16 dest, NetworkInterface a)
        {
            if (dest == 67)
            {
                try
                {
                    DHCPDatagram dHCP    = new DHCPDatagram(udp.Datagram);
                    DHCPDatagram newdHCP = new DHCPDatagram(2, dHCP.xid, ClientMAC: dHCP.chaddr);
                    Optionlist   ol      = new Optionlist();

                    foreach (DHCPOption o in dHCP.options)
                    {
                        switch (o.tag)
                        {
                        case Tag.paramaterList:
                            foreach (byte b in o.data)
                            {
                                switch ((Tag)b)
                                {
                                case Tag.dhcpServerID:
                                    ol.Add(new DHCPOption(Tag.dhcpServerID, a.LocalIP.ToBytes()));
                                    break;

                                case Tag.router:
                                    ol.Add(new DHCPOption(Tag.router, GatewayIP.ToBytes()));
                                    break;

                                case Tag.subnetmask:
                                    ol.Add(new DHCPOption(Tag.subnetmask, SubnetMask.ToBytes()));
                                    break;

                                case Tag.domainserver:
                                    ol.Add(new DHCPOption(Tag.domainserver, DNS.ToBytes()));
                                    break;
                                }
                            }
                            break;

                        case Tag.dhcpMsgType:
                            switch ((DHCPOption.MsgType)o.data[0])
                            {
                            case DHCPOption.MsgType.DHCPDISCOVER:
                                Log("DHCPDescover recieved from " + dHCP.chaddr.ToString());
                                if (newdHCP == null)
                                {
                                    break;
                                }
                                newdHCP.yiaddr = NewAddress(dHCP.chaddr);
                                Reserved.Add(newdHCP.yiaddr, new Lease(newdHCP.yiaddr, dHCP.chaddr, currentTick, 100));
                                ol.Add(new DHCPOption(Tag.dhcpMsgType, new byte[] { (byte)DHCPOption.MsgType.DHCPOFFER }));

                                Log("Offered IP " + newdHCP.yiaddr + " to " + newdHCP.chaddr);
                                break;


                            case DHCPOption.MsgType.DHCPREQUEST:
                                IP Request = new IP(dHCP.options.Find(match => match.tag == Tag.addressRequest).data, 0);

                                Log("DHCPRequest recieved from " + dHCP.chaddr.ToString());

                                if (isAvailable(Request, dHCP.chaddr))
                                {
                                    lock (Leaseslock)
                                    {
                                        if (Reserved.ContainsKey(Request))
                                        {
                                            Reserved.Remove(Request);
                                        }
                                        if (Leases.ContainsKey(Request))
                                        {
                                            Leases.Remove(Request);
                                        }
                                        Lease l = new Lease(Request, dHCP.chaddr, currentTick, (uint)r.Next(40, 60));
                                        Leases.Add(l.ciaddr, l);
                                        ol.Add(new DHCPOption(Tag.dhcpMsgType, new byte[] { (byte)DHCPOption.MsgType.DHCPACK }));
                                        newdHCP.yiaddr = Request;
                                        Log("Leased IP " + l.ciaddr + " to " + l.chaddr + " for " + l.LeaseLength + " ticks");
                                    }
                                }
                                else
                                {
                                    ol.Add(new DHCPOption(Tag.dhcpMsgType, new byte[] { (byte)DHCPOption.MsgType.DHCPNAK }));
                                    Log("denied lease of " + Request + " to " + dHCP.chaddr);
                                }
                                break;
                            }
                            break;
                        }
                    }
                    newdHCP.options = ol;
                    UDPHeader  newupd  = new UDPHeader(67, 68, newdHCP.ToBytes());
                    IPv4Header newipv4 = IPv4Header.DefaultUDPWrapper(a.LocalIP, IP.Broadcast, newupd.ToBytes(), 32);
                    a.SendPacket(newipv4.ToBytes());
                    Log("Packet Sent");
                }
                catch (Exception e)
                {
                    Log(e.ToString());
                }
            }
        }
Esempio n. 7
0
        static void Main(string[] args)
        {
            {
                Console.WriteLine("NSHG.IP");
                IP ip0 = new IP(new Byte[] { 0, 0, 0, 0 });
                Output(ip0.Equals(ip0), "ip.Equals (Identical)");

                Console.WriteLine("NSHG.IP");
                IP ip1234  = new IP(new Byte[] { 1, 2, 3, 4, 5 }, 0);
                IP ip12342 = new IP(new Byte[] { 1, 2, 3, 4 });
                Output(ip12342.Equals(ip1234), "ip.from array size > 4");


                IP ip255  = new IP(new Byte[] { 255, 255, 255, 255 });
                IP ip2552 = new IP(new Byte[] { 255, 255, 255, 255 });
                Output(ip255.Equals(ip2552), "ip.Equals (Non Identical)");

                IP ip3 = new IP(new Byte[] { 0, 0, 0, 0 });
                Output(ip0.Equals(ip3), "ip.Equals (Non Identical)");

                IP ip4 = new IP(new Byte[] { 0, 0, 0, 1 });
                Output(!ip0.Equals(ip4), "ip.Equals (Non Identical, Not Equal)");

                // Parse
                // Normal Parse's
                IP ip5   = IP.Parse("192.168.1.1");
                IP ip192 = new IP(new Byte[] { 192, 168, 1, 1 });
                Output(ip5.Equals(ip192), "ip.Parse(\"192.168.1.1\")");

                ip5 = IP.Parse("0.0.0.0");
                Output(ip5.Equals(ip0), "ip.Parse(\"0.0.0.0\")");

                ip5 = IP.Parse("255.255.255.255");
                Output(ip5.Equals(ip255), "ip.Parse(\"255.255.255.255\")");


                // Address Segments > 255
                bool result = false;
                try
                {
                    IP ip = IP.Parse("256.256.256.256");
                }
                catch (OverflowException)
                {
                    result = true;
                }
                finally
                {
                    Output(result, "IP.Parse (Overflow)");
                }

                // Null Input
                result = false;
                try
                {
                    IP ip = IP.Parse("");
                }
                catch (ArgumentNullException)
                {
                    result = true;
                }
                finally
                {
                    Output(result, "IP.Parse (Null)");
                }

                // Not Enough Segments
                result = false;
                try
                {
                    IP ip = IP.Parse("1");
                }
                catch (ArgumentOutOfRangeException)
                {
                    result = true;
                }
                finally
                {
                    Output(result, "IP.Parse (1)");
                }
                result = false;
                try
                {
                    IP ip = IP.Parse("1.1");
                }
                catch (ArgumentOutOfRangeException)
                {
                    result = true;
                }
                finally
                {
                    Output(result, "IP.Parse (1.1)");
                }
                result = false;
                try
                {
                    IP ip = IP.Parse("1.1.1");
                }
                catch (ArgumentOutOfRangeException)
                {
                    result = true;
                }
                finally
                {
                    Output(result, "IP.Parse (1.1.1)");
                }
            }// IP

            {
                MAC m = new MAC(new Byte[] { 255, 255, 255, 255, 255, 255 });

                m = MAC.Parse("FF:FF:FF:FF:FF:00");
                Console.WriteLine(m.ToString());
                foreach (Byte b in m.ToBytes())
                {
                    Console.WriteLine(b);
                }
            }// MAC

            {
                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine();

                byte[] expected, actual;

                expected = new byte[]
                {
                    0x45, 0x00, 0x00, 0x28, 0x6e, 0x9c, 0x40, 0x00, 0x80, 0x06, 0x77, 0xd7, 0x0a, 0x90, 0xe3, 0x64, 0x28, 0x43, 0xfe, 0x24
                };

                IPv4Header ipv4 = new IPv4Header(0x6E9C, true, false, 128, (IPv4Header.ProtocolType) 6, IP.Parse("10.144.227.100"), IP.Parse("40.67.254.36"), new byte[0], new byte[20]);



                actual = ipv4.ToBytes();

                foreach (byte b in expected)
                {
                    Console.Write(b.ToString("X"));
                    Console.Write(" ");
                }
                Console.WriteLine();


                foreach (byte b in actual)
                {
                    Console.Write(b.ToString("X"));
                    Console.Write(" ");
                }
                Console.WriteLine();
            }// IP  Header

            {
            }// TCP Header


            Console.ReadLine();
        }