实现windows下的动态域名解析服务

Home / Article MrLee 2016-4-20 3150

准备着手这个功能的开发,先纪录下来!
此文仅供参考
动态域名解析系统将用户的动态IP地址映射到一个固定的域名解析服务上(DDNS).动态域名解析系统一般由两部分构成。
第一部分是服务器端程序,位于服务商的主机上。另一部分是客户端程序,就运行在广大用户的主机上。在每次连接网络的时候,客户端程序就会通过信息传递,把该主机的动态IP地址传送给位于服务商主机上的服务器程序,服务器程序负责提供DNS服务并实现动态域名解析服务,再收到客户端通知后服务器端程序立即更新数据,将新的IP地址和原有的固定域名绑定,这样就完成了动态域名解析的服务。别人也就可以通过域名访问你的服务器了。
动态域名服务的功能,就是实现固定域名到动态IP地址之间的解析。用户每次上网得到新的IP地址之后,都要向DNS申请更新该域名解析数据库;在用户在线期间,Internet上的其他人要访问这个域名的时候,动态域名解析服务器就会返回正确的IP地址。当用户下线时,DNS要停用该域名的服务,以免因为同一个IP地址重复利用引起混乱。
 
以下详细讲述动态域名解析服务的具体实现细节。
一 dns服务器安装
采用win2000 server操作系统(winxp和win2000专业版本没有dns服务安装选项)。
安装DNS的步骤如下: (1)选“开始→设置→控制面板”,打开“添加/删除程序”。 (2)单击“添加/删除Windows组件”,等待Windows组件向导启动。 (3)单击[下一步]按钮弹出Windows组件清单。 (4)单击[网络服务],然后再单击[详细情况]按钮。 (5)单击“DNS(域名系统)”旁的复选框。 (6)单击[确定]按钮返回“Windows组件”对话框。 (7)单击[下一步]安装相应的服务,然后逐一单击[完成]和[关闭]按钮即可完成。 不需要重新启动计算机,单击“开始→程序→管理工具→DNS”。
创建区域 (1)用鼠标单击主机旁边的“+”号,然后用鼠标右键单击选“新建区域”以进入新建区域向导中。 (2)当向导提示到要让选择“区域类型”时,此处应该选“标准主要区域”,而在“正向或反向搜索区域中”应选“正向搜索区域”,单击“正向搜索区域”旁边的“+”号,用鼠标右键单击“正向搜索区域”,单击[下一步]按钮。 (3)屏幕出现询问“区域名”,则在“名称”后的文字框中输入“sample.net”(以区域sample.net为例子),接着向导进入到“区域文件”提示窗口中,默认的,系统会自动选中“创建新文件,文件名为”一项,并在其后的文字框中自动填有“sample.net.dns”的名字单击[下一步]按钮,然后单击[完成]即可完成区域创建,此时在DNS管理器的左边的“正向搜索区域”里可以看到“sample.net”区域,如下图所示


注意:创建正向搜索区域的目的是为了将主机名翻译为IP地址,你也可以创建反向搜索区域,便于将IP地址翻译成相应的主机名,创建方法和正向搜索区域的创建相似。
创建主机
(1)在上图所示窗口中的“sample.net”区域上单击右键,选“新建主机”,在其后的对话框中的“名称”栏中输入主机名“www”,在“IP地址”栏输入对应的ip地址。(假设为192.168.1.2,你必须根据实际情况填入正确的ip地址)
(2)单击[添加主机]按钮,即成功地创建了主机地址记录www.,在“新建主机”窗口再选“完成”便可回到DNS管理器中。
(3)上述记录建好以后,就可以在DNS管理器中看到相关的DNS映射记录表,这样,就在“www.sample.net”与“192.168.1.2之间建立了映射关系。
测试配置
DNS服务器配置完以后,如何检测它是否配置正确呢?可以用一个称作“Nslookup”的诊断程序来进行检测。下面我们就用这个命令对DNS服务器进行测试。
(1)在命令提示符下输入:Nslookup,启动该程序。此时系统会响应它当前翻译名字所使用的DNS服务器的IP地址。
(2)然后在命令提示符下输入:ls - d sample.net。这个命令的功能是让DNS服务器列出它所知道的有关sample.net的每一条信息。
(3)在命令提示符下输入:Exit,即可退出Nslookup命令。
注意:在安装“DNS服务器”之前,必须用静态的IP地址配置计算机。
好dns服务器安装完毕,就可以进行dns动态域名更新的实现了
 
通过dns api实现动态域名更新,下面详细介绍典型的dns api函数用法,更多的dns api函数用法请参考微软最新的msdn知识库(vc2003,vc2005带有dns api库文件,vc6.0需手动复制windns.h,dnsapi.lib两个文件到vc安装文件夹的include和lib文件夹中,或者安装最新的sdk包,需要这两个文件的朋友可以联系我,我的邮箱:world7th@163.com)
主要介绍如下几个api函数:
DnsQuery,DnsRecordListFree,DnsRecordCopyEx,DnsModifyRecordsInSet
这里首先给出msdn中这些函数的原文说明
1.DnsQuery
The DnsQuery function type is the generic query interface to the DNS namespace, and provides application developers with a DNS query resolution interface. Like many DNS functions, the DnsQuery function type is implemented in multiple forms to facilitate different character encoding.
Based on the character encoding involved, use one of the following functions:
DnsQuery_A (for ANSI encoding) DnsQuery_W (for Unicode encoding) DnsQuery_UTF8 (for UTF-8 encoding) If the DnsQuery function type is called without its suffix (_A, _W, or _UTF8), a compiler error will occur. DNS_STATUS WINAPI DnsQuery( PCSTR lpstrName, WORD wType, DWORD fOptions, PVOID pExtra, PDNS_RECORD* ppQueryResultsSet, PVOID* pReserved );
Parameters lpstrName [in] The name of the owner of the record set that is queried. wType [in] The numeric representation of the type of record set that is queried, such as a value of 1 (0x0001) for an A record (DNS_TYPE_A). For more information and a complete listing of record set types and their numeric representations, see the Windns.h header file. fOptions [in] The query options. Options can be combined and all options override DNS_QUERY_STANDARD. The following table lists the available query options.Query Meaning DNS_QUERY_STANDARD Standard query. DNS_QUERY_ACCEPT_TRUNCATED_RESPONSE Returns truncated results. Does not retry under TCP. DNS_QUERY_USE_TCP_ONLY Uses TCP only for the query. DNS_QUERY_NO_RECURSION Directs the DNS server to perform an iterative query (specifically directs the DNS server not to perform recursive resolution to resolve the query). DNS_QUERY_BYPASS_CACHE Bypasses the resolver cache on the lookup. DNS_QUERY_NO_WIRE_QUERY Directs DNS to perform a query on the local cache only. Windows 2000 Server and Windows 2000 Professional:  This value is not supported. For similar functionality, use DNS_QUERY_CACHE_ONLY. DNS_QUERY_NO_LOCAL_NAME Directs DNS to ignore the local name. Windows 2000 Server and Windows 2000 Professional:  This value is not supported. DNS_QUERY_NO_HOSTS_FILE Prevents the DNS query from consulting the HOSTS file. Windows 2000 Server and Windows 2000 Professional:  This value is not supported. DNS_QUERY_NO_NETBT Prevents the DNS query from using NetBT for resolution. Windows 2000 Server and Windows 2000 Professional:  This value is not supported. DNS_QUERY_TREAT_AS_FQDN Prevents the DNS response from attaching suffixes to the submitted name in a name resolution process. DNS_QUERY_WIRE_ONLY Directs DNS to perform a query using the network only, bypassing local information. Windows 2000 Server and Windows 2000 Professional:  This value is not supported. DNS_QUERY_RETURN_MESSAGE Directs DNS to return the entire DNS response message. Windows 2000 Server and Windows 2000 Professional:  This value is not supported. DNS_QUERY_DONT_RESET_TTL_VALUES If set, and if the response contains multiple records, records are stored with the TTL corresponding to the minimum value TTL from among all records. When this option is set, "Do not change the TTL of individual records" in the returned record set is not modified. DNS_QUERY_RESERVED Reserved. DNS_QUERY_MULTICAST_ONLY Prevents the query from using DNS and uses only Local Link Multicast Name Resolution (LLMNR). Note   This option is available only when using Windows Server "Longhorn" or Windows Vista.
DNS_QUERY_MULTICAST_VERIFY Directs a test using the local machine hostname to verify name uniqueness on the same Local Link. Collects all responses even if normal LLMNR Sender behaviour is not enabled.. Note   This option is available only when using Windows Server "Longhorn" or Windows Vista.
DNS_QUERY_MULTICAST_WAIT Waits for a full timeout to collect all the responses from the Local Link. If not set, the default behaviour is to return with the first response. Note   This option is available only when using Windows Server "Longhorn" or Windows Vista. pExtra [in, out, optional] This parameter is reserved for future use and must be set to NULL. ppQueryResultsSet [in, out] Optional. A pointer to a pointer that points to the list of RRs that comprise the response. For more information, see the Remarks section. pReserved [in, out] This parameter is reserved for future use and must be set to NULL. Return Value Returns success confirmation upon successful completion. Otherwise, returns the appropriate DNS-specific error code as defined in Winerror.h. Remarks Applications that call the DnsQuery function build a query using a fully-qualified DNS name and Resource Record (RR) type, and set query options depending on the type of service desired. When the DNS_QUERY_STANDARD option is set, DNS uses the resolver cache, queries first with UDP, then retries with TCP if the response is truncated, and requests that the server to perform recursive resolution on behalf of the client to resolve the query.
Callers must free returned RR sets with the DnsRecordListFree function.
Note  When calling one of the DnsQuery function types, be aware that a DNS server may return multiple records in response to a query. A computer that is multihomed, for example, will receive multiple A records for the same IP address. The caller must use as many of the returned records as necessary.
Consider the following scenario, in which multiple returned records require additional activity on behalf of the application: A DnsQuery_A function call is made for a multihomed computer and the application finds that the address associated with the first A record is not responding. The application should then attempt to use other IP addresses specified in the (additional) A records returned from the DnsQuery_A function call.
If the ppQueryResultsSet and pReserved parameters are set to NULL, the DnsQuery function fails with the error INVALID_PARAMETER.
Requirements Client Requires Windows Vista, Windows XP, or Windows 2000 Professional. Server Requires Windows Server "Longhorn", Windows Server 2003, or Windows 2000 Server. Header Declared in Windns.h.
Library Use Dnsapi.lib.
DLL Requires Dnsapi.dll.
2.DnsRecordListFree
The DnsRecordListFree function frees memory allocated for DNS records obtained using the DnsQuery function. void WINAPI DnsRecordListFree( PDNS_RECORD pRecordList, DNS_FREE_TYPE FreeType );
Parameters pRecordList [in, out] A pointer to the list of DNS records to be freed. FreeType [in] A specifier of how the record list should be freed. The only type currently supported is a deep freeing of the entire record list. For more information and a list of values, see the DNS_FREE_TYPE enumeration. Return Value This function does not return a value. Remarks The DnsRecordListFree function can be used to free memory allocated from query results obtained using a DnsQuery function call; it cannot free memory allocated for DNS record lists created manually.
Requirements Client Requires Windows Vista, Windows XP, or Windows 2000 Professional. Server Requires Windows Server "Longhorn", Windows Server 2003, or Windows 2000 Server. Header Declared in Windns.h.
Library Use Dnsapi.lib.
DLL Requires Dnsapi.dll.
3.DnsRecordCopyEx
The DnsRecordCopyEx function creates a copy of a specified RR. The DnsRecordCopyEx function is also capable of converting the character encoding during the copy operation. PDNS_RECORD WINAPI DnsRecordCopyEx( PDNS_RECORD pRecord, DNS_CHARSET CharSetIn, DNS_CHARSET CharSetOut );
Parameters pRecord [in] Pointer to the RR to be copied. CharSetIn [in] Character encoding of the source RR. CharSetOut [in] Character encoding required of the destination record. Return Value Successful execution returns a pointer to the (newly created) destination record. Otherwise, returns null.
Remarks The CharSetIn parameter is used only if the character encoding of the source RR is not specified in pRecord.
Requirements Client Requires Windows Vista, Windows XP, or Windows 2000 Professional. Server Requires Windows Server "Longhorn", Windows Server 2003, or Windows 2000 Server. Header Declared in Windns.h.
Library Use Dnsapi.lib.
DLL Requires Dnsapi.dll.
4.DnsModifyRecordsInSet
The DnsModifyRecordsInSetfunction adds, modifies or removes a Resource Record (RR) set that may have been previously registered with DNS servers.
Like many DNS functions, the DnsModifyRecordsInSet function type is implemented in multiple forms to facilitate different character encoding.
Based on the character encoding involved, use one of the following functions:
DnsModifyRecordsInSet_A (_A for ANSI encoding) DnsModifyRecordsInSet_W (_W for Unicode encoding) DnsModifyRecordsInSet_UTF8 (_UTF8 for UTF 8 encoding) If the DnsModifyRecordsInSet function type is called without its suffix (_A, _W, or _UTF8), a compiler error will occur. DNS_STATUS WINAPI DnsModifyRecordsInSet( PDNS_RECORD pAddRecords, PDNS_RECORD pDeleteRecords, DWORD Options, HANDLE hContext, PVOID pExtraList, PVOID pReserved );
Parameters pAddRecords [in] A pointer to the DNS_RECORD structure that contains the RRs to be added to the RR set. pDeleteRecords [in] A pointer to the DNS_RECORD structure that contains the RRs to be deleted from the RR set. Options [in] Options to apply to the operation. Options consist of the following, and may be combined.Option Meaning DNS_UPDATE_SECURITY_USE_DEFAULT Uses the default behavior, which is specified in the registry, for secure dynamic DNS updates. DNS_UPDATE_SECURITY_OFF Does not attempt secure dynamic updates. DNS_UPDATE_SECURITY_ON Attempts nonsecure dynamic update; if refused, attempts secure dynamic update. DNS_UPDATE_SECURITY_ONLY Attempts secure dynamic updates only. DNS_UPDATE_CACHE_SECURITY_CONTEXT Caches the security context for use in future transactions. DNS_UPDATE_TEST_USE_LOCAL_SYS_ACCT Uses credentials of the local computer account. DNS_UPDATE_FORCE_SECURITY_NEGO Does not use cached security context. DNS_UPDATE_RESERVED Reserved for future use.
hContext [in] A handle to the credentials of a specific account. Used when secure dynamic update is required. This parameter is optional. pExtraList [in, out, optional] This parameter is reserved for future use and must be set to NULL. pReserved This parameter is reserved for future use and must be set to NULL. Return Value Returns success confirmation upon successful completion. Otherwise, it returns the appropriate DNS-specific error code as defined in Winerror.h.
Remarks The DnsModifyRecordsInSet function type executes in the following steps. Records specified in pDeleteRecords are deleted. If pDeleteRecords is empty or does not contain records that exist in the current set, the DnsModifyRecordInSet function goes to the next step. Records specified in pAddRecords are added. If pAddRecords is empty, the operation completes without adding any records. To add a new record, provide no records in pDeleteRecords, and provide the record to be added in pAddRecords. To modify a record, specify the record being modified in pDeleteRecords, then add the modified version of that record by placing it in pAddRecords. To delete records, specify only records to be deleted. Multiple records can be added or deleted in a single call to DnsModifyRecordInSet. If a record specified in pAddRecords is already present, no change occurs.
If no server list is specified, the default name server is queried.
Requirements Client Requires Windows Vista, Windows XP, or Windows 2000 Professional. Server Requires Windows Server "Longhorn", Windows Server 2003, or Windows 2000 Server. Header Declared in Windns.h.
Library Use Dnsapi.lib.
DLL Requires Dnsapi.dll.
 

本文链接:https://www.it72.com/9280.htm

推荐阅读
最新回复 (0)
返回