Exemplo n.º 1
0
        private void SendRTPPacket(string sourceSocket, string destinationSocket)
        {
            try
            {
                //logger.Debug("Attempting to send RTP packet from " + sourceSocket + " to " + destinationSocket + ".");
                Log("Attempting to send RTP packet from " + sourceSocket + " to " + destinationSocket + ".");

                IPEndPoint sourceEP = IPSocket.GetIPEndPoint(sourceSocket);
                IPEndPoint destEP   = IPSocket.GetIPEndPoint(destinationSocket);

                RTPPacket rtpPacket = new RTPPacket(80);
                rtpPacket.Header.SequenceNumber = (UInt16)6500;
                rtpPacket.Header.Timestamp      = 100000;

                UDPPacket  udpPacket = new UDPPacket(sourceEP.Port, destEP.Port, rtpPacket.GetBytes());
                IPv4Header ipHeader  = new IPv4Header(ProtocolType.Udp, Crypto.GetRandomInt(6), sourceEP.Address, destEP.Address);
                IPv4Packet ipPacket  = new IPv4Packet(ipHeader, udpPacket.GetBytes());

                byte[] data = ipPacket.GetBytes();

                Socket rawSocket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
                rawSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, 1);

                rawSocket.SendTo(data, destEP);
            }
            catch (Exception excp)
            {
                logger.Error("Exception SendRTPPacket. " + excp.Message);
            }
        }
Exemplo n.º 2
0
        public void IPEmptyPacketSendUnitTest()
        {
            Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().Name);

            UDPPacket  udpPacket = new UDPPacket(4001, 4001, new byte[] { 0x1, 0x2, 0x3, 0x4 });
            IPv4Header header    = new IPv4Header(ProtocolType.Udp, 7890, IPAddress.Parse("194.213.29.54"), IPAddress.Parse("194.213.29.54"));

            byte[] headerData = header.GetBytes();

            foreach (byte headerByte in headerData)
            {
                Console.Write("0x{0:x} ", headerByte);
            }

            Console.WriteLine();

            Socket rawSocket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);

            //rawSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, 1);
            rawSocket.SendTo(headerData, new IPEndPoint(IPAddress.Parse("194.213.29.54"), 5060));

            rawSocket.Shutdown(SocketShutdown.Both);
            rawSocket.Close();

            Assert.True(true, "True was false.");
        }
Exemplo n.º 3
0
        public void IPHeaderConstructionUnitTest()
        {
            logger.LogDebug("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name);
            logger.BeginScope(System.Reflection.MethodBase.GetCurrentMethod().Name);

            IPv4Header header = new IPv4Header(ProtocolType.Udp, 4567, IPAddress.Parse("127.0.0.1"), IPAddress.Parse("127.0.0.1"));

            byte[] headerData = header.GetBytes();

            int count = 0;

            foreach (byte headerByte in headerData)
            {
                logger.LogDebug("0x{0,-2:x} ", headerByte);
                count++;
                if (count % 4 == 0)
                {
                    logger.LogDebug("\n");
                }
            }

            logger.LogDebug("\n");

            Assert.True(true, "True was false.");
        }
Exemplo n.º 4
0
Arquivo: Device.cs Projeto: sgf/SCTP
 public unsafe void Send(Span <byte> data)
 {
     if (Running)
     {
         IPv4Header iphdr = new IPv4Header();
         iphdr.SrcAddr   = IPAddress.Parse("127.0.0.1");
         iphdr.DstAddr   = IPAddress.Parse("127.0.0.1");
         iphdr.HdrLength = (byte)sizeof(IPv4Header);
         iphdr.Version   = 4;
         iphdr.Protocol  = 132;//SCTP协议
Exemplo n.º 5
0
        private unsafe byte[] ConstructIP4Packet(
            byte ipversion,
            ushort id,
            byte flags,
            ushort fragmentoffset,
            IPProtocol protocol,
            uint source_address,
            uint destination_address,
            byte[] data,
            bool bHardware = false
            )
        {
            int size = sizeof(IPv4Header) + (data?.Length ?? 0);

            byte[] ret = new byte[size];

            IPv4Header header = new IPv4Header();

            header.version_ihl = (byte)((ipversion << 4) + (sizeof(IPv4Header) / 4));
            header.tos_ecn     = 0;
            if (!bHardware)
            {
                header.packet_length = Utility.htons((ushort)ret.Length);
            }
            header.identification = Utility.htons(id);
            //header.flags_fragmentoffset = (ushort)(flags + Utility.htons((ushort)(fragmentoffset << 3)));
            header.flags_fragmentoffset = (ushort)((flags << 4) + (Utility.htons(fragmentoffset) >> 3));
            header.ttl         = 0;
            header.protocol    = protocol;
            header.checksum    = 0;
            header.ip_srcaddr  = source_address;      // not sure why, these are not coming across the wire as net order?
            header.ip_destaddr = destination_address; // not sure why, these are not coming across the wire as net order?

            IntPtr ptr = IntPtr.Zero;

            try
            {
                ptr = Marshal.AllocHGlobal(sizeof(IPv4Header));
                Marshal.StructureToPtr(header, ptr, true);
                Marshal.Copy(ptr, ret, 0, sizeof(IPv4Header));
            }
            finally
            {
                if (ptr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(ptr);
                }
            }
            if ((data?.Length ?? 0) > 0)
            {
                Array.Copy(data, 0, ret, sizeof(IPv4Header), data.Length);
            }

            return(ret);
        }
Exemplo n.º 6
0
 // Network Layer
 public bool SendPacket(MAC adapter, IPv4Header data)
 {
     if (NetworkInterfaces.ContainsKey(adapter))
     {
         NetworkInterfaces[adapter].SendPacket(data.ToBytes());
         return(true);
     }
     else
     {
         return(false);
     }
 }
Exemplo n.º 7
0
        public void Checksum()
        {
            UInt16 expected, actual;

            expected = 0b0111_0111_1101_0111;

            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]);

            actual = ipv4.HeaderChecksum;

            Assert.Equal(expected, actual);
        }
Exemplo n.º 8
0
        static bool PacketFilter(IPv4Header ipv4Header, UDPFrame udpFrame)
        {
            if (ipv4Header.ChecksumOK == false ||
                !ipv4Header.SourceAddress.Equals(FilterSourceAddr) ||
                !ipv4Header.DestinationAddress.Equals(FilterDestAddr) ||
                udpFrame.Header.DestinationPort != FilterDestPort)
            {
                return(false);
            }

            return(UDPFns.UDPChecksum(ipv4Header.SourceAddress, ipv4Header.DestinationAddress, udpFrame) == udpFrame.Header.Checksum);
        }
Exemplo n.º 9
0
 public void packet(IPv4Header ipv4, UDPHeader udp, UInt16 dest, NetworkInterface n)
 {
     if (dest == 68)
     {
         try
         {
             DHCPDatagram d = new DHCPDatagram(udp.Datagram);
             if (sessions.ContainsKey(d.xid))
             {
                 sessions[d.xid].Packet(d);
             }
         }
         catch { }
     }
     return;
 }
Exemplo n.º 10
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);
        }
Exemplo n.º 11
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;
            }
        }
Exemplo n.º 12
0
        public void IPHeaderConstructionUnitTest()
        {
            Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().Name);

            IPv4Header header = new IPv4Header(ProtocolType.Udp, 4567, IPAddress.Parse("194.213.29.54"), IPAddress.Parse("194.213.29.54"));

            byte[] headerData = header.GetBytes();

            int count = 0;

            foreach (byte headerByte in headerData)
            {
                Console.Write("0x{0,-2:x} ", headerByte);
                count++;
                if (count % 4 == 0)
                {
                    Console.Write("\n");
                }
            }

            Console.WriteLine();

            Assert.True(true, "True was false.");
        }
Exemplo n.º 13
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());
        }
Exemplo n.º 14
0
        public void SendRTPPacket(string sourceSocket, string destinationSocket)
        {
            try
            {
                //logger.Debug("Attempting to send RTP packet from " + sourceSocket + " to " + destinationSocket + ".");
                FireLogEvent("Attempting to send RTP packet from " + sourceSocket + " to " + destinationSocket + ".");
                
                IPEndPoint sourceEP = IPSocket.GetIPEndPoint(sourceSocket);
                IPEndPoint destEP = IPSocket.GetIPEndPoint(destinationSocket);

                RTPPacket rtpPacket = new RTPPacket(80);
                rtpPacket.Header.SequenceNumber = (UInt16)6500;
                rtpPacket.Header.Timestamp = 100000;

                UDPPacket udpPacket = new UDPPacket(sourceEP.Port, destEP.Port, rtpPacket.GetBytes());
                IPv4Header ipHeader = new IPv4Header(ProtocolType.Udp, Crypto.GetRandomInt(6), sourceEP.Address, destEP.Address);
                IPv4Packet ipPacket = new IPv4Packet(ipHeader, udpPacket.GetBytes());

                byte[] data = ipPacket.GetBytes();

                Socket rawSocket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
                rawSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, 1);

                rawSocket.SendTo(data, destEP);
            }
            catch (Exception excp)
            {
                logger.Error("Exception SendRTPPacket. " + excp.Message);
            }
        }
Exemplo n.º 15
0
        public void Packet(byte[] datagram, NetworkInterface a)
        {
            try
            {
                OnRecievedPacket.Invoke(datagram, a);
            }
            catch
            {
            }
            IPv4Header Data;

            try
            {
                Data = new IPv4Header(datagram);
            }
            catch (Exception e)
            {
                OnCorruptPacket.BeginInvoke(datagram, a, null, null);
                return;
            }

            if (a.LocalIP != null)
            {
                if (Data.DestinationAddress == a.LocalIP)
                {
                }
                else if (a.SubnetMask != null)
                {
                    if ((Data.DestinationAddress & ~a.SubnetMask) == (a.LocalIP & ~a.SubnetMask))
                    {
                        OnNotForMe?.Invoke(Data, a);
                        return;
                    }
                }
            }

            switch (Data.Protocol)
            {
            case IPv4Header.ProtocolType.ICMP:
                handleICMPPacket(Data, a);
                break;

            case IPv4Header.ProtocolType.TCP:
                TCPHeader TCP;
                try
                {
                    TCP = new TCPHeader(Data.Datagram);
                }
                catch (Exception e)
                {
                    OnCorruptPacket?.Invoke(datagram, a);
                    break;
                }

                OnTCPPacket?.Invoke(Data, TCP, TCP.DestinationPort, a);
                break;

            case IPv4Header.ProtocolType.UDP:
                UDPHeader UDP;
                try
                {
                    UDP = new UDPHeader(Data.Datagram);
                }
                catch (Exception e)
                {
                    OnCorruptPacket?.Invoke(datagram, a);
                    break;
                }
                OnUDPPacket?.Invoke(Data, UDP, UDP.DestinationPort, a);
                break;

            default:
                break;
            }
        }
Exemplo n.º 16
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());
                }
            }
        }
Exemplo n.º 17
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;
                }
            }
Exemplo n.º 18
0
        public void onPacket(byte[] datagram)
        {
            if (on)
            {
                IPv4Header iP = new IPv4Header(datagram);
                if (ip)
                {
                    Log("----------------IP----------------");
                    Log("Version:             " + iP.Version);
                    Log("HeaderLength:        " + iP.IHL);
                    Log("Type Of Service      " + iP.TOS);
                    Log("Total Length         " + iP.Length);
                    Log("Identification       " + iP.Identification);
                    Log("Reserved             " + iP.RES);
                    Log("Don't Fragment       " + iP.DF);
                    Log("More Fragments       " + iP.MF);
                    Log("Fragment Offset      " + iP.FragmentOffset);
                    Log("Time To Live         " + iP.TTL);
                    Log("Protocol             " + iP.Protocol);
                    Log("Checksum             " + iP.HeaderChecksum);
                    Log("Source Address       " + iP.SourceAddress);
                    Log("Destination Address  " + iP.DestinationAddress);
                    Log("----------------------------------------");
                }

                switch (iP.Protocol)
                {
                case IPv4Header.ProtocolType.ICMP:
                    ICMPEchoRequestReply icmp = new ICMPEchoRequestReply(iP.Datagram);
                    break;

                case IPv4Header.ProtocolType.UDP:
                    UDPHeader uDP = new UDPHeader(iP.Datagram);
                    if (udp)
                    {
                        Log("----------------UDP----------------");
                        Log("Source Port       " + uDP.SourcePort);
                        Log("Destination Port  " + uDP.DestinationPort);
                        Log("Length            " + uDP.Length);
                        Log("Checksum          " + uDP.Checksum);
                        Log("----------------------------------------");
                    }

                    DHCPDatagram dHCP = new DHCPDatagram(uDP.Datagram);
                    if (dhcp)
                    {
                        Log("----------------DHCP----------------");
                        Log("Op                ");
                        Log("htype             ");
                        Log("hlen              ");
                        Log("hops              ");
                        Log("transaction ID    ");
                        Log("Secconds Elapsed  ");
                        Log("Flags");
                        Log("    Boradcast     ");
                        Log("Client IP         ");
                        Log("Your IP           ");
                        Log("Server IP         ");
                        Log("Router IP         ");
                        Log("Client MAC        ");
                        Log("Server Host Name  ");
                        Log("Boot File Name    ");
                        Log("Options");
                        foreach (DHCPOption option in dHCP.options)
                        {
                            switch (option.tag)
                            {
                            case Tag.subnetmask:
                                Log("    SubnetMask         " + new IP(option.data).ToString());
                                break;

                            case Tag.defaultIPTTL:
                                Log("    Default TTL        " + option.data[0]);
                                break;

                            case Tag.addressRequest:
                                Log("    Requested IP       " + new IP(option.data).ToString());
                                break;

                            case Tag.addressTime:
                                Log("    Lease Time         " + BitConverter.ToUInt16(option.data, 0));
                                break;

                            case Tag.router:
                                Log("    DHCP Message Type  " + option.data[0]);
                                break;

                            case Tag.paramaterList:
                                Log("    Paramater List  ");
                                foreach (byte b in option.data)
                                {
                                    Log("        " + b);
                                }
                                break;
                            }
                        }
                        Log("----------------------------------------");
                    }
                    break;
                }
            }
        }
Exemplo n.º 19
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();
        }