Ejemplo n.º 1
0
        public static void Main(string[] arg)
        {
            Console.WriteLine("Hi. It is Dns client" +
                              "\nWrite domain to get IP" +
                              "\nParameters after domain can change configuration" +
                              "\n-type <mx>. Type A by default" +
                              "\n-host <1.1.1.1>. Host 8.8.8.8 by default");

            string line = "";

            while (line != "exit")
            {
                packet       = new DnsPacket();
                packet.qtype = Type.A;
                line         = Console.ReadLine();

                var param = line.Split(' ');

                packet.qname = param[0];

                for (int i = 0; i < param.Length; i++)
                {
                    if (param[i] == "-type")
                    {
                        if (param[i + 1].ToLower() == "mx")
                        {
                            packet.qtype = Type.MX;
                        }
                        if (param[i + 1].ToLower() == "txt")
                        {
                            packet.qtype = Type.TXT;
                        }
                    }
                    if (param[i] == "-host")
                    {
                        if (IsValid(param[i + 1]))
                        {
                            serverAddress = param[i + 1];
                        }
                        else
                        {
                            Console.WriteLine("Host invalid");
                            error = true;
                            break;
                        }
                    }
                }
                if (error)
                {
                    error = false;
                    continue;
                }
                packet.qr     = QR.Request;
                packet.opcode = Opcode.Query;
                Client();
            }
        }
Ejemplo n.º 2
0
        public static byte[] Pack(DnsPacket packet)
        {
            var p = packet;


            //byte[] bID = Encoding.UTF8.GetBytes("AAAA");//BitConverter.GetBytes(p.ID);
            short parameters = (short)((byte)p.qr | (byte)p.opcode >> 1 | (byte)p.autorativeAnswer >> 5
                                       | (byte)p.trunCation >> 6 | (byte)p.recursionDesired >> 7
                                       | (byte)p.recursionAvailable >> 8 | (byte)p.reserved >> 9 | (byte)p.rcode >> 12);

            //Console.WriteLine("Params " + parameters);

            byte[] bParam = BitConverter.GetBytes(parameters);
            byte[] bQd    = BitConverter.GetBytes(p.qdCount);
            byte[] bAn    = BitConverter.GetBytes(p.anCount);
            byte[] bSs    = BitConverter.GetBytes(p.ssCount);
            byte[] bAr    = BitConverter.GetBytes(p.arCount);

            string[] parts = p.qname.Split('.');


            byte[] bName   = new byte[p.qname.Length + 2];
            var    counter = 0;

            for (int i = 0; i < parts.Length; i++)
            {
                bName[counter] = BitConverter.GetBytes(parts[i].Length)[0];
                counter++;
                var b = Encoding.Default.GetBytes(parts[i]);
                for (int k = 0; k < b.Length; k++)
                {
                    bName[counter] = b[k];
                    counter++;
                }
            }
            bName[bName.Length - 1] = 0;
            Console.WriteLine("Q TYpe " + (short)p.qtype);
            byte[] bQType  = BitConverter.GetBytes((short)p.qtype);
            byte[] bQClass = BitConverter.GetBytes(p.qclass);

            var nameSize = bName.Length;// % 16 + bName.Length;

            byte[] dnsData = new byte[16 + nameSize];

            dnsData[0] = 0xAA;
            dnsData[1] = 0xAA;
            dnsData[2] = bParam[0];
            dnsData[3] = bParam[1];

            dnsData[4]  = bQd[1];
            dnsData[5]  = bQd[0];
            dnsData[6]  = bAn[0];
            dnsData[7]  = bAn[1];
            dnsData[8]  = bSs[0];
            dnsData[9]  = bSs[1];
            dnsData[10] = bAr[0];
            dnsData[11] = bAr[1];

            dnsData[12 + nameSize] = bQType[1];
            dnsData[13 + nameSize] = bQType[0];
            dnsData[14 + nameSize] = bQClass[1];
            dnsData[15 + nameSize] = bQClass[0];


            for (int i = 0; i < bName.Length; i++)
            {
                dnsData[12 + i] = bName[i];
            }

            dnsData[11 + nameSize] = BitConverter.GetBytes((short)0)[0];

            return(dnsData);
        }