分享
 
 
 

MX记录获取组件(vb实现)

王朝vb·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

源码是老外的,俺做了点修改,写成了dll

方法:

Public Function GetDNSinfo() As String

获取dns信息

Public Function MX_Query(DNS_Addr As String, ByVal Domain_Addr As String) As String

获取mx最佳记录,

dns_addr,域名解析服务器,可以用getdnsinfo获取,也可以用nslookup命令

domain_addr,想要获取邮件服务器的域名,如163.com ,hotmail.com

http://www.aspcdrom.com/down/mxquery.rar

VERSION 1.0 CLASS

BEGIN

MultiUse = -1 'True

Persistable = 0 'NotPersistable

DataBindingBehavior = 0 'vbNone

DataSourceBehavior = 0 'vbNone

MTSTransactionMode = 0 'NotAnMTSObject

END

Attribute VB_Name = "mxquery"

Attribute VB_GlobalNameSpace = False

Attribute VB_Creatable = True

Attribute VB_PredeclaredId = False

Attribute VB_Exposed = True

Option Explicit

Private WithEvents objWinSock As MSWinsockLib.Winsock

Attribute objWinSock.VB_VarHelpID = -1

Private Const ERROR_BUFFER_OVERFLOW = 111

Private DNSrecieved As Boolean

Private dnsReply() As Byte

Private Declare Function GetNetworkParams Lib "IPHlpApi" (FixedInfo As Any, pOutBufLen As Long) As Long

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Private Declare Sub MemCopy Lib "kernel32" Alias "RtlMoveMemory" (Dest As Any, Src As Any, ByVal cb&)

Private Declare Function htons Lib "wsock32.dll" (ByVal hostshort As Long) As Integer

Private Declare Function ntohs Lib "wsock32.dll" (ByVal netshort As Long) As Integer

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Const DNS_RECURSION As Byte = 1

Private Const MAX_HOSTNAME_LEN = 132

Private Const MAX_DOMAIN_NAME_LEN = 132

Private Const MAX_SCOPE_ID_LEN = 260

Private Const MAX_ADAPTER_NAME_LENGTH = 260

Private Const MAX_ADAPTER_ADDRESS_LENGTH = 8

Private Const MAX_ADAPTER_DESCRIPTION_LENGTH = 132

Private Type IP_ADDR_STRING

Next As Long

IpAddress As String * 16

IpMask As String * 16

Context As Long

End Type

Private Type FIXED_INFO

HostName As String * MAX_HOSTNAME_LEN

DomainName As String * MAX_DOMAIN_NAME_LEN

CurrentDnsServer As Long

DnsServerList As IP_ADDR_STRING

NodeType As Long

ScopeId As String * MAX_SCOPE_ID_LEN

EnableRouting As Long

EnableProxy As Long

EnableDns As Long

End Type

Private Type DNS_HEADER

qryID As Integer

options As Byte

response As Byte

qdcount As Integer

ancount As Integer

nscount As Integer

arcount As Integer

End Type

Private Type HostEnt

h_name As Long

h_aliases As Long

h_addrtype As Integer

h_length As Integer

h_addr_list As Long

End Type

Private Const hostent_size = 16

Private Type servent

s_name As Long

s_aliases As Long

s_port As Integer

s_proto As Long

End Type

Private Function MakeQName(sDomain As String) As String

Dim iQCount As Integer ' Character count (between dots)

Dim iNdx As Integer ' Index into sDomain string

Dim iCount As Integer ' Total chars in sDomain string

Dim sQName As String ' QNAME string

Dim sDotName As String ' Temp string for chars between dots

Dim sChar As String ' Single char from sDomain string

iNdx = 1

iQCount = 0

iCount = Len(sDomain)

' While we haven't hit end-of-string

While (iNdx <= iCount)

' Read a single char from our domain

sChar = Mid(sDomain, iNdx, 1)

' If the char is a dot, then put our character count and the part of the string

If (sChar = ".") Then

sQName = sQName & Chr(iQCount) & sDotName

iQCount = 0

sDotName = ""

Else

sDotName = sDotName + sChar

iQCount = iQCount + 1

End If

iNdx = iNdx + 1

Wend

sQName = sQName & Chr(iQCount) & sDotName

MakeQName = sQName

End Function

Private Sub ParseName(dnsReply() As Byte, iNdx As Integer, sName As String)

Dim iCompress As Integer ' Compression index (index into original buffer)

Dim iChCount As Integer ' Character count (number of chars to read from buffer)

' While we didn't encounter a null char (end-of-string specifier)

While (dnsReply(iNdx) <> 0)

' Read the next character in the stream (length specifier)

iChCount = dnsReply(iNdx)

' If our length specifier is 192 (0xc0) we have a compressed string

If (iChCount = 192) Then

' Read the location of the rest of the string (offset into buffer)

iCompress = dnsReply(iNdx + 1)

' Call ourself again, this time with the offset of the compressed string

ParseName dnsReply(), iCompress, sName

' Step over the compression indicator and compression index

iNdx = iNdx + 2

' After a compressed string, we are done

Exit Sub

End If

' Move to next char

iNdx = iNdx + 1

' While we should still be reading chars

While (iChCount)

' add the char to our string

sName = sName + Chr(dnsReply(iNdx))

iChCount = iChCount - 1

iNdx = iNdx + 1

Wend

' If the next char isn't null then the string continues, so add the dot

If (dnsReply(iNdx) <> 0) Then sName = sName + "."

Wend

End Sub

Private Function GetMXName(dnsReply() As Byte, iNdx As Integer, iAnCount As Integer) As String

Dim iChCount As Integer ' Character counter

Dim sTemp As String ' Holds original query string

Dim iMXLen As Integer

Dim iBestPref As Integer ' Holds the "best" preference number (lowest)

Dim sBestMX As String ' Holds the "best" MX record (the one with the lowest preference)

iBestPref = -1

ParseName dnsReply(), iNdx, sTemp

' Step over null

iNdx = iNdx + 2

' Step over 6 bytes (not sure what the 6 bytes are, but all other

' documentation shows steping over these 6 bytes)

iNdx = iNdx + 6

On Error Resume Next

While (iAnCount)

' Check to make sure we received an MX record

If (dnsReply(iNdx) = 15) Then

Dim sName As String

Dim iPref As Integer

sName = ""

' Step over the last half of the integer that specifies the record type (1 byte)

' Step over the RR Type, RR Class, TTL (3 integers - 6 bytes)

iNdx = iNdx + 1 + 6

' Read the MX data length specifier

' (not needed, hence why it's commented out)

MemCopy iMXLen, dnsReply(iNdx), 2

iMXLen = ntohs(iMXLen)

' Step over the MX data length specifier (1 integer - 2 bytes)

iNdx = iNdx + 2

MemCopy iPref, dnsReply(iNdx), 2

iPref = ntohs(iPref)

' Step over the MX preference value (1 integer - 2 bytes)

iNdx = iNdx + 2

' Have to step through the byte-stream, looking for 0xc0 or 192 (compression char)

Dim iNdx2 As Integer

iNdx2 = iNdx

ParseName dnsReply(), iNdx2, sName

If (iBestPref = -1 Or iPref < iBestPref) Then

iBestPref = iPref

sBestMX = sName

End If

iNdx = iNdx + iMXLen + 1

' Step over 3 useless bytes

'iNdx = iNdx + 3

Else

GetMXName = sBestMX

Exit Function

End If

iAnCount = iAnCount - 1

Wend

GetMXName = sBestMX

End Function

Public Function GetDNSinfo() As String

Dim error As Long

Dim FixedInfoSize As Long

Dim strDNS As String

Dim FixedInfo As FIXED_INFO

Dim Buffer As IP_ADDR_STRING

Dim FixedInfoBuffer() As Byte

FixedInfoSize = 0

error = GetNetworkParams(ByVal 0&, FixedInfoSize)

If error <> 0 Then

If error <> ERROR_BUFFER_OVERFLOW Then

MsgBox "GetNetworkParams sizing failed with error: " & error

Exit Function

End If

End If

ReDim FixedInfoBuffer(FixedInfoSize - 1)

error = GetNetworkParams(FixedInfoBuffer(0), FixedInfoSize)

If error = 0 Then

CopyMemory FixedInfo, FixedInfoBuffer(0), Len(FixedInfo)

strDNS = FixedInfo.DnsServerList.IpAddress

strDNS = Replace(strDNS, vbCr, "")

strDNS = Replace(strDNS, vbLf, "")

strDNS = Replace(strDNS, vbNullChar, "")

strDNS = Trim(strDNS)

GetDNSinfo = strDNS

End If

End Function

Private Sub Class_Initialize()

Set objWinSock = New MSWinsockLib.Winsock

objWinSock.Protocol = sckUDPProtocol

objWinSock.RemotePort = 53

End Sub

Private Sub Class_Terminate()

Set objWinSock = Nothing '

End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

''class

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Private Sub objWinSock_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)

Debug.Print Description

End Sub

Private Sub objWinSock_DataArrival(ByVal bytesTotal As Long)

DNSrecieved = True

ReDim dnsReply(bytesTotal) As Byte

objWinSock.GetData dnsReply, vbArray + vbByte

End Sub

Public Function MX_Query(DNS_Addr As String, ByVal Domain_Addr As String) As String

Dim IpAddr As Long

Dim iRC As Integer

Dim dnsHead As DNS_HEADER

Dim iSock As Integer

' Set the DNS parameters

dnsHead.qryID = htons(&H11DF)

dnsHead.options = DNS_RECURSION

dnsHead.qdcount = htons(1)

dnsHead.ancount = 0

dnsHead.nscount = 0

dnsHead.arcount = 0

' Query Variables

Dim dnsQuery() As Byte

Dim sQName As String

Dim dnsQueryNdx As Integer

Dim iTemp As Integer

Dim iNdx As Integer

dnsQueryNdx = 0

ReDim dnsQuery(4000)

' Setup the dns structure to send the query in

' First goes the DNS header information

MemCopy dnsQuery(dnsQueryNdx), dnsHead, 12

dnsQueryNdx = dnsQueryNdx + 12

' Then the domain name (as a QNAME)

sQName = MakeQName(Domain_Addr)

iNdx = 0

While (iNdx < Len(sQName))

dnsQuery(dnsQueryNdx + iNdx) = Asc(Mid(sQName, iNdx + 1, 1))

iNdx = iNdx + 1

Wend

dnsQueryNdx = dnsQueryNdx + Len(sQName)

' Null terminate the string

dnsQuery(dnsQueryNdx) = &H0

dnsQueryNdx = dnsQueryNdx + 1

' The type of query (15 means MX query)

iTemp = htons(15)

MemCopy dnsQuery(dnsQueryNdx), iTemp, Len(iTemp)

dnsQueryNdx = dnsQueryNdx + Len(iTemp)

' The class of query (1 means INET)

iTemp = htons(1)

MemCopy dnsQuery(dnsQueryNdx), iTemp, Len(iTemp)

dnsQueryNdx = dnsQueryNdx + Len(iTemp)

On Error Resume Next

ReDim Preserve dnsQuery(dnsQueryNdx - 1)

' Send the query to the DNS server

objWinSock.RemoteHost = DNS_Addr

DNSrecieved = False

objWinSock.SendData dnsQuery

If WaitUntilTrue(DNSrecieved, 60) = False Then

'MX_Query = ""

Exit Function

End If

Dim iAnCount As Integer

' Get the number of answers

MemCopy iAnCount, dnsReply(6), 2

iAnCount = ntohs(iAnCount)

' Parse the answer buffer

MX_Query = Trim(GetMXName(dnsReply(), 12, iAnCount))

End Function

Private Function WaitUntilTrue(ByRef Flag As Boolean, ByVal SecondsToWait As Long) As Boolean

Dim fStart As Single

Dim fTimetoQuit As Single

fStart = Timer

' Deal with timer being reset at Midnight

If fStart + SecondsToWait < 86400 Then

fTimetoQuit = fStart + SecondsToWait

Else

fTimetoQuit = (fStart - 86400) + SecondsToWait

End If

Do Until Flag = True

If Timer >= fTimetoQuit Then

WaitUntilTrue = Flag

Exit Function

End If

DoEvents

Sleep (10)

Loop

WaitUntilTrue = Flag

End Function

极品源码,无限精彩,尽在 http://www.aspcdrom.com

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有