Exemple #1
0
 public static IDhcpOption !Parse(byte option,
                                  byte length,
                                  byte[] !srcBuffer,
                                  int offset)
 {
     if (length != payloadLength)
     {
         throw new InvalidDhcpOptionLength();
     }
     return(new DhcpIPv4Option(option,
                               IPv4.ParseBytes(srcBuffer, offset)));
 }
Exemple #2
0
 public static IDhcpOption !Parse(byte option, byte length,
                                  byte[] !srcBuffer, int offset)
 {
     if ((length & 3) != 0)
     {
         throw new InvalidDhcpOptionLength();
     }
     IPv4 [] addresses = new IPv4 [length / 4];
     for (int i = 0; i < addresses.Length; i++)
     {
         addresses[i] = IPv4.ParseBytes(srcBuffer, offset);
         offset      += IPv4.Length;
     }
     return(new DhcpMultiIPv4Option(option, addresses));
 }
Exemple #3
0
        // creates an arp reply using the received packet
        // the packet must be an arp request
        // the responder should pass its own IP and MAC address
        // we assume that we have the same hw type
        // (return offset_arp size)
        public static int CreateArpReply(ref byte[] !pkt,
                                         IPv4 selfIP,
                                         EthernetAddress selfMACAddr)
        {
            int offset = 14;           // arp message starts here.
            int o      = offset;

            // check we have enough packet
            if (pkt.Length - offset < Size)
            {
                return(offset);
            }

            // check hardware type == 0x0001 (Ethernet)
            if (pkt[o++] != 0x00 || pkt[o++] != 0x01)
            {
                return(offset);
            }

            // check protocol type == 0x0800 (IP)
            if (pkt[o++] != 0x08 || pkt[o++] != 0x00)
            {
                return(offset); // error
            }
            // check addresses len
            if (pkt[o++] != 0x06)
            {
                return(offset);
            }

            if (pkt[o++] != 0x04)
            {
                return(offset);
            }

            // operation code
            Type opcode = (Type)(((int)pkt[o++] << 8) | (int)pkt[o++]);

            // we need a request message.
            if (opcode != Type.ARP_REQUEST)
            {
                return(offset);
            }

            // sender HW & IP
            EthernetAddress senderhw = EthernetAddress.ParseBytes(pkt, o);

            o += EthernetAddress.Length;

            IPv4 senderIP = IPv4.ParseBytes(pkt, o);

            o += IPv4.Length;

            // target HW & IP
            // EthernetAddress targethw = EthernetAddress.ParseBytes(pkt, o);
            o += EthernetAddress.Length;

            // IPv4 targetIP = IPv4.ParseBytes(pkt, o);
            o += IPv4.Length;

            // all is well, do our stuff...
            // 1. set local hw address to arp's source ether address + fix dest addresses
            // 2. set arp type = ARP_REPLY
            // 3. set the destination's ethernet address to self
            //    and target is the source of the request.

            o        = offset + 6; // opcode entry
            pkt[o++] = (byte)(((ushort)Type.ARP_REPLY) >> 8);
            pkt[o++] = (byte)Type.ARP_REPLY;

            // set hw and IP address
            selfMACAddr.CopyOut(pkt, o);
            o += EthernetAddress.Length;

            selfIP.CopyOut(pkt, o);
            o += IPv4.Length;

            // setup dest address to the requesting host address
            senderhw.CopyOut(pkt, o);
            o += EthernetAddress.Length;

            senderIP.CopyOut(pkt, o);
            o += IPv4.Length;

            // set ethernet level addresses
            Array.Copy(pkt, 6, pkt, 0, 6);  // src  -> dest
            selfMACAddr.CopyOut(pkt, 6);    // self -> src

            return(offset + Size);
        }
Exemple #4
0
        /// <summary>
        /// Gets the DNS information for the specified DNS host name.
        /// </summary>
        public StatusCode GetHostByName(string name,
                                        out IPv4HostEntry hostEntry)
        {
            hostEntry = null;

            IPv4 dnsServer = DnsServer;

            if (dnsServer == IPv4.Zero)
            {
                return(StatusCode.NoNameServer);
            }

            Dns.Query  q = new Dns.Query(name, Dns.Type.A, Dns.Class.Internet);
            Dns.Format answer;

            StatusCode askResponse = Ask(dnsServer, q, out answer);

            if (askResponse != StatusCode.Success)
            {
                return(askResponse);
            }
            assert answer != null;

            if (answer.GetRCode() != Dns.RCode.NoError)
            {
                DebugPrint("Dns query failed: RCode = {0}\n", answer.GetRCode());
                return(StatusCode.RequestFailed);
            }

            hostEntry = new IPv4HostEntry(new string [] { name }, new IPv4[] {});
            ArrayList addressList = new ArrayList();

            foreach (Dns.ResourceRecord !rr in answer.AnswerRRs)
            {
                if (rr.Type != Dns.Type.A || rr.Class != Dns.Class.Internet)
                {
                    continue;
                }

                byte [] rdata = rr.RData;
                if (rdata == null)
                {
                    continue;
                }

                if ((rdata.Length % IPv4.Length) != 0)
                {
                    continue;
                }

                int offset = 0;
                while (offset != rdata.Length)
                {
                    addressList.Add(IPv4.ParseBytes(rdata, offset));
                    offset += 4;
                }
            }

            IPv4[] al = hostEntry.AddressList = new IPv4 [addressList.Count];
            for (int i = 0; i < addressList.Count; i++)
            {
                al[i] = (IPv4)(!)addressList[i];
            }

            return(StatusCode.Success);
        }