實現windows下的動態網域名稱解析服務(三)

來源:互聯網
上載者:User

  通過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. 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.