Showing posts with label DNS. Show all posts
Showing posts with label DNS. Show all posts

Lookup SPF record in C#

We have retrieved already MX and SRV records. Now let's take SPF record from DNS server. SPF string is TXT record(s) and if you are looking for google.com record proceed with
nslookup -type=TXT google.com

Csharp class looks like

public class nDnsQuery
    {
        public nDnsQuery()
        {
        }

        [DllImport("dnsapi", EntryPoint = "DnsQuery_W", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
        private static extern int DnsQuery([MarshalAs(UnmanagedType.VBByRefStr)]ref string pszName, QueryTypes wType, QueryOptions options, int aipServers, ref IntPtr ppQueryResults, int pReserved);

        [DllImport("dnsapi", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern void DnsRecordListFree(IntPtr pRecordList, int FreeType);

  

        public static string[] GetSPFRecords(string domain)
        {

            IntPtr ptr1 = IntPtr.Zero;
            IntPtr ptr2 = IntPtr.Zero;
            SPFRecord recSPF;
            if (Environment.OSVersion.Platform != PlatformID.Win32NT)
            {
                throw new NotSupportedException();
            }
            ArrayList list1 = new ArrayList();
            try
            {

                int num1 = nDnsQuery.DnsQuery(ref domain, QueryTypes.DNS_TYPE_TEXT, QueryOptions.DNS_QUERY_BYPASS_CACHE, 0, ref ptr1, 0);
                if (num1 != 0)
                {
                    if (num1 == 9003)
                    {
                        list1.Add("DNS record does not exist");
                    }
                    else
                    {
                        throw new Win32Exception(num1);
                    }
                }
                for (ptr2 = ptr1; !ptr2.Equals(IntPtr.Zero); ptr2 = recSPF.pNext)
                {
                    recSPF = (SPFRecord)Marshal.PtrToStructure(ptr2, typeof(SPFRecord));
                    if (recSPF.wType == (ushort)QueryTypes.DNS_TYPE_TEXT)
                    {
                        for (int i = 0; i < recSPF.dwStringCount; i++)
                        {
                            IntPtr pString = recSPF.pStringArray + i;
                            string text1 = Marshal.PtrToStringAuto(pString);
                            if (text1.Contains("v=spf1") || text1.Contains("spf2.0"))
                            {
                                list1.Add(text1);
                            }
                        }
                    }
                }
            }
            finally
            {
                nDnsQuery.DnsRecordListFree(ptr1, 0);
            }
            return (string[])list1.ToArray(typeof(string));
        }

        private enum QueryOptions
        {
            DNS_QUERY_ACCEPT_TRUNCATED_RESPONSE = 1,
            DNS_QUERY_BYPASS_CACHE = 8,
            DNS_QUERY_DONT_RESET_TTL_VALUES = 0x100000,
            DNS_QUERY_NO_HOSTS_FILE = 0x40,
            DNS_QUERY_NO_LOCAL_NAME = 0x20,
            DNS_QUERY_NO_NETBT = 0x80,
            DNS_QUERY_NO_RECURSION = 4,
            DNS_QUERY_NO_WIRE_QUERY = 0x10,
            DNS_QUERY_RESERVED = -16777216,
            DNS_QUERY_RETURN_MESSAGE = 0x200,
            DNS_QUERY_STANDARD = 0,
            DNS_QUERY_TREAT_AS_FQDN = 0x1000,
            DNS_QUERY_USE_TCP_ONLY = 2,
            DNS_QUERY_WIRE_ONLY = 0x100
        }

        private enum QueryTypes
        {
            DNS_TYPE_A = 0x0001,
            DNS_TYPE_MX = 0x000f,
            DNS_TYPE_TEXT = 0x0010,
            DNS_TYPE_SRV = 0x0021
        }

  

        [StructLayout(LayoutKind.Sequential)]
        private struct SPFRecord
        {
            public IntPtr pNext;
            public string pName;
            public ushort wType;
            public ushort wDataLength;
            public int flags;
            public int dwTtl;
            public int dwReserved;
            public Int32 dwStringCount;
            public IntPtr pStringArray;
        }

    }

You can take full sources here.

Enjoy!

Lookup SRV record in C#

Similar to MX lookup, we can use WINAPI DnsQuery from csharp to complete DNS SRV request.
You can take sources here.

public class nDnsQuery
    {
        public nDnsQuery()
        {
        }

        [DllImport("dnsapi", EntryPoint = "DnsQuery_W", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
        private static extern int DnsQuery([MarshalAs(UnmanagedType.VBByRefStr)]ref string pszName, QueryTypes wType, QueryOptions options, int aipServers, ref IntPtr ppQueryResults, int pReserved);

        [DllImport("dnsapi", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern void DnsRecordListFree(IntPtr pRecordList, int FreeType);

  
        public static string[] GetSRVRecords(string needle)
        {

            IntPtr ptr1 = IntPtr.Zero;
            IntPtr ptr2 = IntPtr.Zero;
            SRVRecord recSRV;
            if (Environment.OSVersion.Platform != PlatformID.Win32NT)
            {
                throw new NotSupportedException();
            }
            ArrayList list1 = new ArrayList();
            try
            {

                int num1 = nDnsQuery.DnsQuery(ref needle, QueryTypes.DNS_TYPE_SRV, QueryOptions.DNS_QUERY_BYPASS_CACHE, 0, ref ptr1, 0);
                if (num1 != 0)
                {
                    if (num1 == 9003)
                    {
                        list1.Add("DNS record does not exist");
                    }
                    else
                    {
                        throw new Win32Exception(num1);
                    }
                }
                for (ptr2 = ptr1; !ptr2.Equals(IntPtr.Zero); ptr2 = recSRV.pNext)
                {
                    recSRV = (SRVRecord)Marshal.PtrToStructure(ptr2, typeof(SRVRecord));
                    if (recSRV.wType == (ushort)QueryTypes.DNS_TYPE_SRV)
                    {
                        string text1 = Marshal.PtrToStringAuto(recSRV.pNameTarget);
                        text1 += ":" + recSRV.wPort;
                        list1.Add(text1);
                    }
                }
            }
            finally
            {
                nDnsQuery.DnsRecordListFree(ptr1, 0);
            }
            return (string[])list1.ToArray(typeof(string));
        }

        private enum QueryOptions
        {
            DNS_QUERY_ACCEPT_TRUNCATED_RESPONSE = 1,
            DNS_QUERY_BYPASS_CACHE = 8,
            DNS_QUERY_DONT_RESET_TTL_VALUES = 0x100000,
            DNS_QUERY_NO_HOSTS_FILE = 0x40,
            DNS_QUERY_NO_LOCAL_NAME = 0x20,
            DNS_QUERY_NO_NETBT = 0x80,
            DNS_QUERY_NO_RECURSION = 4,
            DNS_QUERY_NO_WIRE_QUERY = 0x10,
            DNS_QUERY_RESERVED = -16777216,
            DNS_QUERY_RETURN_MESSAGE = 0x200,
            DNS_QUERY_STANDARD = 0,
            DNS_QUERY_TREAT_AS_FQDN = 0x1000,
            DNS_QUERY_USE_TCP_ONLY = 2,
            DNS_QUERY_WIRE_ONLY = 0x100
        }

        private enum QueryTypes
        {
            DNS_TYPE_A = 0x0001,
            DNS_TYPE_MX = 0x000f,
            DNS_TYPE_SRV = 0x0021
        }

  
        [StructLayout(LayoutKind.Sequential)]
        private struct SRVRecord
        {
            public IntPtr pNext;
            public string pName;
            public ushort wType;
            public ushort wDataLength;
            public int flags;
            public int dwTtl;
            public int dwReserved;
            public IntPtr pNameTarget;
            public ushort wPriority;
            public ushort wWeight;
            public ushort wPort;
            public ushort Pad;
        }
    }
Enjoy!

Lookup MX record in C#

Thank you Peter A. Bromberg for great article "Build a C# DNS MX (Mail Exchange) Record Query Class
Original
MSDN


 
using System;
using System.Collections;
using System.ComponentModel;
using System.Runtime.InteropServices;

namespace DNS_MX
{
    public class DnsMx
    {
        public DnsMx()
        {
        }

        [DllImport("dnsapi"EntryPoint "DnsQuery_W"CharSet CharSet.UnicodeSetLastError trueExactSpelling =true)]
        private static extern int DnsQuery([MarshalAs(UnmanagedType.VBByRefStr)]ref string pszNameQueryTypes wType,QueryOptions optionsint aipServersref IntPtr ppQueryResultsint pReserved);

        [DllImport("dnsapi"CharSet CharSet.AutoSetLastError true)]
        private static extern void DnsRecordListFree(IntPtr pRecordListint FreeType);

        public static string[GetMXRecords(string domain)
        {

            IntPtr ptr1 IntPtr.Zero;
            IntPtr ptr2 IntPtr.Zero;
            MXRecord recMx;
            if (Environment.OSVersion.Platform != PlatformID.Win32NT)
            {
                throw new NotSupportedException();
            }
            ArrayList list1 new ArrayList();
            try
            {

                int num1 DnsMx.DnsQuery(ref domainQueryTypes.DNS_TYPE_MXQueryOptions.DNS_QUERY_BYPASS_CACHE0refptr10);
                if (num1 != 0)
                {
                    if (num1 == 9003)
                    {
                        list1.Add("DNS record does not exist");
                    }
                    else
                    {
                        throw new Win32Exception(num1);
                    }
                }
                for (ptr2 ptr1!ptr2.Equals(IntPtr.Zero)ptr2 recMx.pNext)
                {
                    recMx (MXRecord)Marshal.PtrToStructure(ptr2typeof(MXRecord));
                    if (recMx.wType == 15)
                    {
                        string text1 Marshal.PtrToStringAuto(recMx.pNameExchange);
                        list1.Add(text1);
                    }
                }
            }
            finally
            {
                DnsMx.DnsRecordListFree(ptr10);
            }
            return (string[])list1.ToArray(typeof(string));
        }

        private enum QueryOptions
        {
            DNS_QUERY_ACCEPT_TRUNCATED_RESPONSE 1,
            DNS_QUERY_BYPASS_CACHE 8,
            DNS_QUERY_DONT_RESET_TTL_VALUES 0x100000,
            DNS_QUERY_NO_HOSTS_FILE 0x40,
            DNS_QUERY_NO_LOCAL_NAME 0x20,
            DNS_QUERY_NO_NETBT 0x80,
            DNS_QUERY_NO_RECURSION 4,
            DNS_QUERY_NO_WIRE_QUERY 0x10,
            DNS_QUERY_RESERVED -16777216,
            DNS_QUERY_RETURN_MESSAGE 0x200,
            DNS_QUERY_STANDARD 0,
            DNS_QUERY_TREAT_AS_FQDN 0x1000,
            DNS_QUERY_USE_TCP_ONLY 2,
            DNS_QUERY_WIRE_ONLY 0x100
        }

        private enum QueryTypes
        {
            DNS_TYPE_MX 15
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct MXRecord
        {
            public IntPtr pNext;
            public string pName;
            public ushort wType;
            public ushort wDataLength;
            public int flags;
            public int dwTtl;
            public int dwReserved;
            public IntPtr pNameExchange;
            public ushort wPreference;
            public ushort Pad;
        }
    }
}

HOWTO: Repair Logitech M325 Mouse

FixIt says that you will find single screw under CE label. It isn't always true.