Documents & code‎ > ‎

DNS.NET Resolver (VB.NET)

DNS.NET Resolver (VB.NET)   
post id91 
post length9549 
post datetime5/30/2012 8:30:09 PM 
post ip10.10.10.254 

DNS.NET Resolver (VB.NET)

used as basis [!]:   
        urlhttp://www.codeproject.com/Articles/23673/DNS-NET-Resolver-C 
        DNS.NET Resolver (C#) | Heijden 


Working Web`service here: 
        urlhttp://pesap.com/dnsn/ 


more details about structure`s see: 
        urlhttp://www.codeproject.com/Articles/23673/DNS-NET-Resolver-C 

PS: 
        NOT! parse server response in example.. 


Option Strict Off : Option Explicit On 

#Region " Imports 

Imports System.Runtime.InteropServices _ 
      , System.Net.NetworkInformation _ 
      , System.Net.Sockets _ 
      , System.Security _ 
      , System.Text _ 
      , System.Net 

#End Region 

Module Main 

    '-----------{{ 
    '| Header [size: 12 byte`s]   
    '---------------------------- 
    <StructLayout(LayoutKind.Sequential, Pack:=1)> _ 
    Public Structure Header 
        Dim ID, _ 
            Flags, _ 
            QDCOUNT, _ 
            ANCOUNT, _ 
            NSCOUNT, _ 
            ARCOUNT As [Int16] 
    End Structure 

    '-----------{{ 
    '| Question [size: (X^2) + 4 byte`s]   
    '---------------------------- 
    <StructLayout(LayoutKind.Sequential, Pack:=1)> _ 
    Public Structure Question 
        Dim QNAME As [String] _ 
          , QTYPE As QType, _ 
            QCLASS As QClass 
        Sub New(ByVal NAME As [String], ByVal [TYPE] As QType, ByVal [CLASS] As QClass) 
            QNAME = NAME 
            QTYPE = [TYPE] 
            QCLASS = [CLASS] 
            If Not QNAME.EndsWith(".") Then 
                QNAME = QNAME + "." 
            End If 
        End Sub 
        Public ReadOnly Property Data As Byte() 
            Get 
                Try 
                    Dim temp As Byte() _ 
                            = WriteName(QNAME) _ 
                      , [ofs] As Integer _ 
                            = temp.Length _ 
                      , ret(0) As Byte 
                    ReDim Preserve ret([ofs] - 1 + 4) 
                    Buffer.BlockCopy(temp, 0, ret, 0, [ofs]) 
                    Dim T1 As Byte() _ 
                            = BitConverter.GetBytes(UShort.Parse(CType(QTYPE, UShort))) _ 
                      , T2 As Byte() _ 
                            = BitConverter.GetBytes(UShort.Parse(CType(QTYPE, UShort))) 
                    Array.Reverse(T1) _ 
                        : Array.Reverse(T2) 
                    Buffer.BlockCopy(T1, 0, ret, [ofs] + 0, 2) 
                    Buffer.BlockCopy(T2, 0, ret, [ofs] + 2, 2) 
                    Return ret 
                Catch ex As Exception 
                    Return Nothing 
                End Try 
            End Get 
        End Property 
        Private Function WriteName(ByVal src As String) As Byte() 
            If Not src.EndsWith(".") Then 
                src = (src & ".") 
            End If 
            If (src = ".") Then 
                Return New Byte(1 - 1) {} 
            End If 
            Dim sb As New StringBuilder 
            Dim intLen As Integer = src.Length 
            sb.Append(ChrW(0)) 
            Dim intI As Integer = 0 
            Dim intJ As Integer = 0 
            Do While (intI < intLen) 
                sb.Append(src.Chars(intI)) 
                If (src.Chars(intI) = "."c) Then 
                    Dim ch As Char = Microsoft.VisualBasic.ChrW(intJ And &HFF) 
                    sb.Chars((intI - intJ)) = ch 
                    intJ = -1 
                End If 
                intI += 1 
                intJ += 1 
            Loop 
            sb.Chars((sb.Length - 1)) = ChrW(0) 
            Return Encoding.ASCII.GetBytes(sb.ToString) 
        End Function 
    End Structure 

    Public Enum OPCode 
        Query 
        '... 
    End Enum 
    Public Enum QType As UShort 
        A = 1 
        '... 
        ANY = 255 
    End Enum 
    Public Enum QClass As UShort 
        [IN] = 1 
        '... 
        [ANY] = 255 
    End Enum 

    Function SETBIT(ByVal [OBJ] As UShort, ByVal value As Object, ByVal [ofs] As Integer) As UShort 
        Try 
            Select Case value.GetType() 
                Case CType(0, UShort).GetType() 
                    Dim BI2 As BitArray = New BitArray(BitConverter.GetBytes(CType(value, UShort))) 
                    Dim DATA As Byte() = BitConverter.GetBytes([OBJ]) 
                    Dim BI1 As BitArray = New BitArray(DATA) 
                    BI1.Set([ofs] + 0, BI2(0)) 
                    BI1.Set([ofs] + 1, BI2(1)) 
                    BI1.Set([ofs] + 2, BI2(2)) 
                    BI1.Set([ofs] + 3, BI2(3)) 
                    BI1.CopyTo(DATA, 0) 
                    [OBJ] = BitConverter.ToUInt16(DATA, 0) 
                Case CType(0, Boolean).GetType() 
                    Dim DATA As Byte() = BitConverter.GetBytes([OBJ]) 
                    Dim BI1 As BitArray = New BitArray(DATA) 
                    BI1.Set([ofs], CType(value, Boolean)) 
                    BI1.CopyTo(DATA, 0) 
                    [OBJ] = BitConverter.ToUInt16(DATA, 0) 
                    Return [OBJ] 
            End Select 
            Return [OBJ] 
        Catch ex As Exception 
            Console.WriteLine(String.Concat(ex.Message)) 
            Return [OBJ] 
        End Try 
    End Function 

    <STAThread()Sub Main() 
        ' 
        Try 
            ' 
            ' GET DNS SERVER 
            Dim [NDS] As ArrayList _ 
                   = New ArrayList 
            For Each [T1] As Object In NetworkInterface.GetAllNetworkInterfaces() 
                With [T1] 
                    If .OperationalStatus = OperationalStatus.Up Then 
                        For Each [T2] As IPAddress _ 
                            In CType([T1].GetIPProperties(), IPInterfaceProperties).DnsAddresses 
                            [NDS].Add(New IPEndPoint([T2], 53)) 
                        Next 
                    End If 
                End With 
            Next 
            'Create [Header] 
            Try 
                Dim [Header] As Header _ 
                          = New Header 
                Dim [Flags] As UShort = 0 
                [Flags] = SETBIT([Flags], CType(OPCode.Query, UShort), 11) 
                [Flags] = SETBIT([Flags], True, 0) 
                '... 
                '... 
                Dim T1 As Byte() _ 
                    = BitConverter.GetBytes(UShort.Parse(CType(1, UShort))) _ 
                  , T2 As Byte() _ 
                    = BitConverter.GetBytes(UShort.Parse(CType(0, UShort))) _ 
                  , T3 As Byte() _ 
                    = BitConverter.GetBytes(UShort.Parse(CType(0, UShort))) _ 
                  , T4 As Byte() _ 
                    = BitConverter.GetBytes(UShort.Parse(CType(0, UShort))) 
                Array.Reverse(T1) 
                [Header].ID = 1000 
                [Header].Flags = [Flags] 
                [Header].QDCOUNT = BitConverter.ToUInt16(T1, 0) 
                [Header].ANCOUNT = BitConverter.ToUInt16(T2, 0) 
                [Header].NSCOUNT = BitConverter.ToUInt16(T3, 0) 
                [Header].ARCOUNT = BitConverter.ToUInt16(T4, 0) 
                Dim DATA(0) As [Byte] 
                Try 
                    Dim [size] As Integer _ 
                            = Marshal.SizeOf([Header]) _ 
                      , ptr As [IntPtr] _ 
                            = Marshal.AllocHGlobal([size]) 
                    ReDim Preserve DATA([size] - 1) 
                    Marshal.StructureToPtr([Header], ptr, False) 
                    Marshal.Copy(ptr, DATA, 0, [size]) 
                    Marshal.FreeHGlobal(ptr) 
                Catch ex As Exception 
                End Try 
                'Create [Question] 
                Dim [Question] As Question _ 
                            = New Question("test.com", QType.A, QClass.IN) 
                Try 
                    Dim [size] As Integer _ 
                            = [Question].Data.Length _ 
                      , [ofs] As Integer = DATA.Length 
                    ReDim Preserve DATA([ofs] + [size] - 1) 
                    Buffer.BlockCopy([Question].Data, 0, DATA, [ofs], [size]) 
                Catch ex As Exception 
                End Try 
                'SendTo:: 
                Dim socket As Socket = New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp) 
                socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 10000) 
                Try 
                    socket.SendTo(DATA, [NDS](0)) 
                    Dim responseMessage(512) As Byte 
                    Dim ret As Integer _ 
                            = socket.Receive(responseMessage) 
                    Console.Write(String.Concat("receive: ", ret, vbCrLf)) 
                Catch ex As Exception 
                End Try 
                socket.Close() 
                ' 
                '---------------------{ 
                ' Work with Receive... 
                '... 
                '}--------- 
                ' 
            Catch ex As Exception 
                Console.Write(String.Concat(ex.Message, vbCrLf)) 
            End Try 
            ' 
        Catch As Exception 
            Console.WriteLine(String.Concat( _ 
            e.Message _ 
            )) 
        End Try 
        ' 
        Console.WriteLine(String.Concat(ControlChars.CrLf _ 
        , "Press ENTER to continue..." _ 
        )) 
        ' 
        Console.ReadLine() 
        ' 
    End Sub 

End Module 

Comments