예제 #1
0
        public static byte[] GetTxtRecords(string address, string host, int port)
        {
            byte[]   header       = { 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0 };
            string[] addressParts = address.Split('.');
            byte[]   label        = new byte[address.Length + 2];
            int      pos          = 0;

            foreach (string part in addressParts)
            {
                label[pos++] = System.Convert.ToByte(part.Length);
                foreach (char character in part)
                {
                    label[pos++] = System.Convert.ToByte(character);
                }
                label[pos] = 0;
            }
            byte[] footer = { 0, 16, 0, 1 };

            // Build the query
            byte[] query = new byte[header.Length + label.Length + footer.Length];

            header.CopyTo(query, 0);
            label.CopyTo(query, header.Length);
            footer.CopyTo(query, header.Length + label.Length);

            // Send the query
            //System.Net.IPEndPoint endPoint = new System.Net.IPEndPoint(GetIpAddress(dnsHost), 53);
            System.Net.IPEndPoint endPoint = new System.Net.IPEndPoint(System.Net.IPAddress.Parse(host), 53);
            //System.Net.Sockets.UdpClient udpClient = new System.Net.Sockets.UdpClient();
            ActiveUp.Net.Mail.TimedUdpClient udpClient = new ActiveUp.Net.Mail.TimedUdpClient();
            udpClient.Connect(endPoint);
            udpClient.Send(query, query.Length);
            byte[] response2;
            try
            {
                response2 = udpClient.Receive(ref endPoint);
            }
            catch (Exception ex)
            {
                udpClient.Close();
                throw new System.Exception("Can't connect to DNS server.");
            }

            //System.IO.FileStream fs = new System.IO.FileStream("c:\\Inetpub\\wwwroot\\brol.txt", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
            //fs.Write(response2, 0, response2.Length);
            //fs.Close();

            pos = query.Length;

            //ActiveUp.Net.Mail.MxRecordCollection mxRecords = new ActiveUp.Net.Mail.MxRecordCollection();

            //for (index = 1; index <= answersCount; index++)
            //{
            //ActiveUp.Net.Mail.MxRecord newMxRecord = new ActiveUp.Net.Mail.MxRecord();

            GetLabelsByPos(response2, ref pos);
            pos += 7;
            int length = response2[pos];

            byte[] data = new byte[response2.Length - pos - 4];
            Array.Copy(response2, pos + 4, data, 0, response2.Length - pos - 4);

            return(data);
        }
예제 #2
0
        public static byte[] GetTxtRecords(string address, string host, int port)
        {
            byte[] header = { 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0 };
            string[] addressParts = address.Split('.');
            byte[] label = new byte[address.Length + 2];
            int pos = 0;
            foreach (string part in addressParts)
            {
                label[pos++] = System.Convert.ToByte(part.Length);
                foreach (char character in part)
                    label[pos++] = System.Convert.ToByte(character);
                label[pos] = 0;
            }
            byte[] footer = { 0, 16, 0, 1 };

            // Build the query
            byte[] query = new byte[header.Length + label.Length + footer.Length];

            header.CopyTo(query, 0);
            label.CopyTo(query, header.Length);
            footer.CopyTo(query, header.Length + label.Length);

            // Send the query
            //System.Net.IPEndPoint endPoint = new System.Net.IPEndPoint(GetIpAddress(dnsHost), 53);
            System.Net.IPEndPoint endPoint = new System.Net.IPEndPoint(System.Net.IPAddress.Parse(host), 53);
            //System.Net.Sockets.UdpClient udpClient = new System.Net.Sockets.UdpClient();
            ActiveUp.Net.Mail.TimedUdpClient udpClient = new ActiveUp.Net.Mail.TimedUdpClient();
            udpClient.Connect(endPoint);
            udpClient.Send(query, query.Length);
            byte[] response2;
            try
            {
                response2 = udpClient.Receive(ref endPoint);
            }
            catch(Exception)
            {
                udpClient.Close();
                throw new System.Exception("Can't connect to DNS server.");
            }

            //System.IO.FileStream fs = new System.IO.FileStream("c:\\Inetpub\\wwwroot\\brol.txt", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
            //fs.Write(response2, 0, response2.Length);
            //fs.Close();

            pos = query.Length;

            //ActiveUp.Net.Mail.MxRecordCollection mxRecords = new ActiveUp.Net.Mail.MxRecordCollection();

            //for (index = 1; index <= answersCount; index++)
            //{
                //ActiveUp.Net.Mail.MxRecord newMxRecord = new ActiveUp.Net.Mail.MxRecord();

            GetLabelsByPos(response2, ref pos);
            pos += 7;
            // int length = response2[pos];
            byte[] data = new byte[response2.Length-pos-4];
            Array.Copy(response2, pos + 4, data, 0, response2.Length - pos - 4);
            
            return data;
        }