那位高手有做过的。。
能否详细的说明一下。。
OICQ:41233280
e-mail:[email protected]

解决方案 »

  1.   

    /************************************************************\
        THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
        ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
        THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
        PARTICULAR PURPOSE.  Copyright ?2000  Microsoft Corporation.  All Rights Reserved./***************************************************************/
    /*
       FILE:          Dnsquery.cpp
       DESCRIPTION:   This sample illustrates the use of DnsQuery() function to send query to 
                      a DNS server to resolve the host name to an IP address and vice-versa. 
                      
       PLATFORM:      Windows 2000
       WRITTEN BY:    Rashmi Anoop 
       DATE:          3/22/2000
       *//* 
      includes
    */
    #include <windows.h>  //windows
    #include <windns.h>   //DNS api's
    #include <stdio.h>    //standard i/o
    #include <winsock.h>  //winsock#define BUFFER_LEN 255
    //Usage of the program
    void Usage(char *progname) {
        fprintf(stderr,"Usage\n%s -n [OwnerName] -t [Type]  -s [DnsServerIp]\n",
                progname);
        fprintf(stderr,"Where:\n\t\"OwnerName\" is name of the owner of the record set being queried\n");
        fprintf(stderr,"\t\"Type\" is the type of record set to be queried A or PTR\n");
        fprintf(stderr,"\t\"DnsServerIp\"is the IP address of DNS server (in dotted decimal notation)");
        fprintf(stderr,"to which the query should be sent\n");
        exit(1);
    }//  the main function 
    void __cdecl main(int argc, char *argv[]){    DNS_STATUS status;               // return value of  DnsQuery_A() function.
        PDNS_RECORD pDnsRecord;          //pointer to DNS_RECORD structure
        PIP4_ARRAY pSrvList = NULL;      //pinter to IP4_ARRAY structure
        LPTSTR pOwnerName = NULL;        //owner name to be queried 
        WORD wType;                      //Type of the record to be queried
        char DnsServIp[BUFFER_LEN];      //DNS server ip address
        DNS_FREE_TYPE freetype ;
        freetype =  DnsFreeRecordListDeep;
        IN_ADDR ipaddr;
        if (argc > 4) {        for (int i = 1; i < argc ; i++) {            if ( (argv[i][0] == '-') || (argv[i][0] == '/') ) {                switch (tolower(argv[i][1])) {
                    
                    case 'n':
                        pOwnerName = argv[++i];
                        break;                case 't':
                        if (!_stricmp(argv[i+1], "A") )
                            wType = DNS_TYPE_A; //Query host records to resolve a name
                        else if (!_stricmp(argv[i+1], "PTR") )
                            wType = DNS_TYPE_PTR; //Query PTR records to resovle an IP address
                        else
                            Usage(argv[0]);
                        i++;
                        break;                case 's':
                        // Allocate memory for IP4_ARRAY structure
                        pSrvList = (PIP4_ARRAY) LocalAlloc(LPTR,sizeof(IP4_ARRAY));
                        if (!pSrvList) {
                            printf("Memory allocation failed \n");
                            exit(1);
                        }
                        if (argv[++i]) {
                            strncpy(DnsServIp, argv[i], sizeof(DnsServIp)-1 );
                            DnsServIp[sizeof(DnsServIp)-1] = '\0';
                            pSrvList->AddrCount = 1;
                            pSrvList->AddrArray[0] = inet_addr(DnsServIp); //DNS server IP address
                            if ( pSrvList->AddrArray[0] == INADDR_NONE ) {
                                printf("Invalid DNS server IP address \n");
                                Usage( argv[0] );
                            }
                            break; 
                        }                default:
                        Usage(argv[0]);
                        break;
                    }
                }
                else
                    Usage(argv[0]);     
            }       }
        else
            Usage(argv[0]);
        // Calling function DnsQuery_A() to query Host or PTR records        status = DnsQuery_A(pOwnerName,                 //pointer to OwnerName 
                            wType,                      //Type of the record to be queried
                            DNS_QUERY_BYPASS_CACHE,     // Bypasses the resolver cache on the lookup. 
                            pSrvList,                   //contains DNS server IP address
                            &pDnsRecord,                //Resource record comprising the response
                            NULL);                     //reserved for future use    if (status) {
            if (wType == DNS_TYPE_A)
                printf("Failed to query the host record for %s and the error is %d \n", pOwnerName, status);
            else
                printf("Failed to query the PTR record and the error is %d \n", status);
        }
        else {
            if (wType == DNS_TYPE_A) {            //convert the Internet network address into a string
                //in Internet standard dotted format.
                ipaddr.S_un.S_addr = (pDnsRecord->Data.A.IpAddress);
                printf("The IP address of the host %s is %s \n", pOwnerName,inet_ntoa(ipaddr));            // Free memory allocated for DNS records 
                DnsRecordListFree(pDnsRecord, freetype);
            }
            else {
                printf("The host name is %s  \n",(pDnsRecord->Data.PTR.pNameHost));            // Free memory allocated for DNS records 
                DnsRecordListFree(pDnsRecord, freetype);
            }
        }
        LocalFree(pSrvList);}
      

  2.   

    // GetDNS.h: interface for the GetDNS class.
    //
    //////////////////////////////////////////////////////////////////////#if !defined(AFX_GETDNS_H__66D46E32_01C5_4D19_B1BE_459C57F7DC14__INCLUDED_)
    #define AFX_GETDNS_H__66D46E32_01C5_4D19_B1BE_459C57F7DC14__INCLUDED_#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000class CGetDNS  
    {
    public:
    CGetDNS();
    virtual ~CGetDNS();// Functions
    void GetBestPreference(CString* szMailExchange);
    void Query(HWND hwnd, char* host, char* dns);protected:// Results Structure
    struct dns_results
    {
    char szMailDomain[256];
    char szMailPreference[256];
    char szMailExchange[256];
    } DNS_RESULT[20];// Variables
    int iStructCounter;
    int already_read;
    unsigned char *query_rep;// Functions
    unsigned long int GetLongInt(SOCKET sock);
    unsigned short int GetShortInt(SOCKET sock);
    unsigned short int GetTypeAndClass(SOCKET sock);
    unsigned char GetByte(SOCKET sock);
    unsigned char GetNewByte(SOCKET sock, int partenza);
    unsigned char GetSegment(SOCKET sock, char nomehost[], int partenza);
    void GetLabel(SOCKET sock, char nomehost[], int partenza);
    void GetAnswer(SOCKET sock, unsigned short int type, char nomehost[]);
    void mx_query(char* host, SOCKADDR_IN dns_addr);
    int process_errors(int errors,char* message, char* host);
    int res_mkquery(int query_type, char* domain_name, int dns_class, int type,char* data, int data_length, int inutile, char* buffer,int buf_length);
    };#endif // !defined(AFX_GETDNS_H__66D46E32_01C5_4D19_B1BE_459C57F7DC14__INCLUDED_)
      

  3.   

    // GetDNS.cpp: implementation of the GetDNS class.
    //
    //////////////////////////////////////////////////////////////////////#include "stdafx.h"
    #include "GetDNS.h"#ifdef _DEBUG
    #undef THIS_FILE
    static char THIS_FILE[]=__FILE__;
    #define new DEBUG_NEW
    #endif//////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////CGetDNS::CGetDNS()
    {
    WSADATA dati;
    if(WSAStartup(MAKEWORD(2,0),&dati)){
    WSACleanup();
    return;
    }
    iStructCounter = 0;
    }CGetDNS::~CGetDNS()
    {
    WSACleanup();
    }//////////////////////////////////////////////////////////////////////
    // Functions
    //////////////////////////////////////////////////////////////////////unsigned short int CGetDNS::GetShortInt(SOCKET sock){
    unsigned short int lunghezza;
    int bytes=recv(sock,((char*)(&lunghezza))+1,1,0);
    bytes=recv(sock,((char*)(&lunghezza))+0,1,0);
    already_read+=2;
    return lunghezza;
    }unsigned char CGetDNS::GetByte(SOCKET sock){
    char byte;
    int bytes=recv(sock,&byte,1,0);
    already_read++;
    return (unsigned char)byte;
    }unsigned long int CGetDNS::GetLongInt(SOCKET sock){
    unsigned long int lunghezza;
    int bytes;
    bytes=recv(sock,((char*)(&lunghezza))+3,1,0);
    bytes=recv(sock,((char*)(&lunghezza))+2,1,0);
    bytes=recv(sock,((char*)(&lunghezza))+1,1,0);
    bytes=recv(sock,((char*)(&lunghezza))+0,1,0);
    already_read+=4;
    return lunghezza;
    }unsigned char CGetDNS::GetNewByte(SOCKET sock, int partenza){
    if (partenza==already_read) {
    query_rep[partenza]=GetByte(sock);
    }
    return query_rep[partenza];
    }unsigned char CGetDNS::GetSegment(SOCKET sock, char nomehost[], int partenza){
    unsigned char byte=GetNewByte(sock, partenza);
    int i;
    if ((byte&192)==192){
    unsigned char byte2=GetNewByte(sock,partenza+1);
    int npartenza=(byte&63)*256+byte2;
    GetLabel(sock, nomehost, npartenza);
    return 192;
    }
    else{
    for(i=0;i<byte;i++){
    unsigned char byte2=GetNewByte(sock,partenza+1+i);
    nomehost=byte2;
    }
    return byte;
    }
    }void CGetDNS::GetLabel(SOCKET sock, char nomehost[], int partenza){
    unsigned char length;
    int read_now=0;
    for(;;){
    length=GetSegment(sock,nomehost+read_now,partenza+read_now);
    if (length==192) break;
    if (length==0){
    nomehost[strlen(nomehost)-1]=0;
    break;
    }
    read_now+=length;
    nomehost[read_now++]='.';
    nomehost[read_now]=0;
    }
    }unsigned short int CGetDNS::GetTypeAndClass(SOCKET sock){
    unsigned short int byte=GetShortInt(sock);//type
    unsigned short int byte2;//class
    byte2=GetShortInt(sock);
    return byte;
    }int CGetDNS::process_errors(int errors,char* message, char* host){
    switch(errors&15){
    case 0:
    break;
    case 1:
    strcpy(message,"Format error");
    break;
    case 2:
    strcpy(message,"Server failure");
    break;
    case 3:
    sprintf(message,"The host or domain %s does not exist",host);
    break;
    case 4:
    strcpy(message,"Not implemented");
    break;
    case 5:
    strcpy(message,"Refused");
    break;
    default:
    strcpy(message,"Unknown error");
    }
    return (errors&15);
    }int CGetDNS::res_mkquery(int query_type, char* domain_name, int dns_class, int type,
    char* data, int data_length, int inutile, char* buffer,
    int buf_length){
    int lunghezza,i,l;
    unsigned char count;
    struct {
    char id[2];
    unsigned char control[2];
    unsigned short int qdcount;
    unsigned short int ancount;
    unsigned short int nscount;
    unsigned short int arcount;
    }query;
    unsigned short qtype[2];
    lunghezza=sizeof(query)+sizeof(qtype)+strlen(domain_name)+2;
    if (buf_length<lunghezza) return -1;
    query.id[0]=query.id[1]='F';
    query.control[0]=1+((query_type&15)<<3);//recursion desired
    query.control[1]=0;//no error
    query.qdcount=htons(1);
    query.ancount=0;
    query.nscount=0;
    query.arcount=0;
    qtype[0]=htons((unsigned short)type);
    qtype[1]=htons((unsigned short)dns_class);
    memcpy(buffer,&query,sizeof(query));
    l=strlen(domain_name);
    count=0;
    for(i=0;i<=l;i++){
    if ((domain_name!='.')&&(domain_name!=0))
    count++;
    else{
    buffer[sizeof(query)+i-count]=count;
    memcpy(buffer+sizeof(query)+i-count+1,domain_name+i-count,count);
    count=0;
    }
    }
    buffer[sizeof(query)+l+1]=0;
    memcpy(buffer+sizeof(query)+l+2,(char*)&qtype,sizeof(qtype));
    return lunghezza;
    }void CGetDNS::GetAnswer(SOCKET sock, unsigned short int type, char nomehost[]){
    unsigned short int anslength;
    char soa_label_1[256],soa_label_2[256];
    int i;
    unsigned char len;
    unsigned long int lungo[5];
    anslength=GetShortInt(sock);
    anslength=GetShortInt(sock);
    anslength=GetShortInt(sock);
    switch(type){
    case 1:
    {unsigned char byte1=GetByte(sock);
    unsigned char byte2=GetByte(sock);
    unsigned char byte3=GetByte(sock);
    unsigned char byte4=GetByte(sock);
    sprintf(nomehost,"%u.%u.%u.%u",byte1,byte2,byte3,byte4);
    break;}
    case 2:
    GetLabel(sock, nomehost, already_read);
    break;
    case 5:
    GetLabel(sock, nomehost, already_read);
    break;
    case 6:
    GetLabel(sock, soa_label_1, already_read);
    GetLabel(sock, soa_label_2, already_read);
    for(i=0; i<5; i++) {
    lungo=GetLongInt(sock);
    }
    sprintf(nomehost,"Domain name %s, mailbox %s, serial %u, refresh %u, retry %u, expire %u, minimum %u",
    soa_label_1,soa_label_2,lungo[0],lungo[1],lungo[2],lungo[3],lungo[4]);
    break;
    case 12:
    GetLabel(sock, nomehost, already_read);
    break;
    case 13:
    nomehost[0]=GetByte(sock);
    len=1;
    for(i=0;i<nomehost[0];i++)
    nomehost[len++]=GetByte(sock);
    nomehost[len++]=GetByte(sock);
    for(i=0;i<nomehost[nomehost[0]+1];i++)
    nomehost[len++]=GetByte(sock);
    break;
    case 15:
    nomehost[0]=GetByte(sock);
    nomehost[1]=GetByte(sock);
    GetLabel(sock, nomehost+2, already_read);
    break;
    default:
    for(i=0; i<anslength; i++) GetByte(sock);
    }
    }
      

  4.   

    masterz
    oicq聊好吗。。
    41233280
      

  5.   

    void CGetDNS::mx_query(char* host, SOCKADDR_IN dns_addr){
    int length=0,result,i,timeout=20,err;
    char *query,message[256],*name,*answer;
    unsigned char errors;
    unsigned short int qdcount,ancount,nscount,arcount,anstype;
    SOCKET sock;
    BOOL alias; while(1){
    length++;
    query=(char*)malloc(length);
    result=res_mkquery(0,host,1,15,0,0,0,query,length);
    if (result!=-1) break;
    free(query);
    }
    sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    setsockopt(sock,SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout));
    err=connect(sock,(SOCKADDR*)&dns_addr,16);
    if (err){
    //sprintf(message,"Socket error, connection failed");
    //AfxMessageBox(message);
    return;
    }
    send(sock,((char*)(&result))+1,1,0);
    send(sock,((char*)(&result))+0,1,0);
    send(sock,query,result,0);
    already_read=-2;
    length=GetShortInt(sock);
    query_rep=(unsigned char*)malloc(length);
    GetShortInt(sock);//id
    GetByte(sock);//type
    errors=GetByte(sock);
    if (process_errors(errors,message,host)){
    //AfxMessageBox(message);
    return;
    }
    qdcount=GetShortInt(sock);
    ancount=GetShortInt(sock);
    nscount=GetShortInt(sock);
    arcount=GetShortInt(sock);
    if (ancount==0){
    //sprintf(message,"%s is a valid domain name, but not a valid mail address",host);
    //AfxMessageBox(message);
    closesocket(sock);
    return;
    }
    name=(char*)malloc(256);
    answer=(char*)malloc(256);
    for(i=0;i<qdcount;i++){
    GetLabel(sock,name,already_read);
    GetTypeAndClass(sock);
    }
    for(i=0;i<ancount;i++){
    char pref[6];
    GetLabel(sock,name,already_read);
    anstype=GetTypeAndClass(sock);
    GetAnswer(sock,anstype,answer);
    sprintf(pref,"%u",answer[0]*256+answer[1]);
    //-- Ensure we dont go over our structure array
    if( iStructCounter <= 20 ) {
    strcpy( DNS_RESULT[iStructCounter].szMailDomain , name);
    strcpy( DNS_RESULT[iStructCounter].szMailExchange , answer+2);
    strcpy( DNS_RESULT[iStructCounter].szMailPreference , pref);
    iStructCounter++;
    }
    }
    alias=TRUE;
    closesocket(sock);
    free(query_rep);
    }void CGetDNS::Query(HWND hwnd, char* host, char* dns){
    //char message[256];
    SOCKADDR_IN dns_addr, host_addr; dns_addr.sin_port=htons(53);
    dns_addr.sin_family=AF_INET;
    dns_addr.sin_addr.S_un.S_addr=inet_addr(dns);
    if (dns_addr.sin_addr.S_un.S_addr==INADDR_NONE){
    struct hostent* hostgot=gethostbyname(dns);
    if (hostgot==NULL){
    /*
    sprintf(message,"Could not resolve %s",dns);
    MessageBox(hwnd,message,"Error",MB_OK|MB_ICONERROR);
    */
    return;
    }
    dns_addr.sin_addr.S_un.S_un_b.s_b1=hostgot->h_addr_list[0][0];
    dns_addr.sin_addr.S_un.S_un_b.s_b2=hostgot->h_addr_list[0][1];
    dns_addr.sin_addr.S_un.S_un_b.s_b3=hostgot->h_addr_list[0][2];
    dns_addr.sin_addr.S_un.S_un_b.s_b4=hostgot->h_addr_list[0][3];
    }
    memset(dns_addr.sin_zero,0,8);
    host_addr.sin_addr.S_un.S_addr=inet_addr(host);

    //-- Perform MX_DNS
    mx_query(host,dns_addr); //-- Loop thru structure of returned results
    /*
    for( int n = 0; n <= iStructCounter -1; n++ ) {
    CString szOutput;
    szOutput.Format("[%s] %s", DNS_RESULT[n].szMailPreference, DNS_RESULT[n].szMailExchange);
    AfxMessageBox(szOutput);
    }
    */
    }//-- Pass reference to lowest MX preference
    void CGetDNS::GetBestPreference(CString *szMailExchange)
    {
    int iPos = 0;
    int iPreference = 100;
    if( iStructCounter > 0 ) {
    for( int n = 0; n <= iStructCounter -1; n++ ) {
    if( iPreference >= atoi(DNS_RESULT[n].szMailPreference) ) {
    iPreference = atoi(DNS_RESULT[n].szMailPreference);
    iPos = n;
    }
    }
    *szMailExchange = DNS_RESULT[iPos].szMailExchange;
    }else{
    szMailExchange->Empty();
    }
    }//http://www.codeguru.com/cgi-bin/bbs/wt/showpost.pl?Board=vc&Number=329921&page=&view=&sb=
    //An example of usage is:
    CGetDNS oDNS;
    CString szDNSResult, szDomain;
    oDNS.Query( m_hWnd, szDomain.GetBuffer(szDomain.GetLength()+1), 
       m_szDNSServer.GetBuffer(m_szDNSServer.GetLength()+1) );
    oDNS.GetBestPreference( &szDNSResult );
    szDomain.ReleaseBuffer();
    m_szDNSServer.ReleaseBuffer();
    if( !szDNSResult.IsEmpty() ) 
    {   
    AfxMessageBox(szDNSResult)
    }
      

  6.   

    非常谢谢你的回答 masterz() 
    现在我想知道怎么DNS向报文多点。。
    不用DnsQuery
    不知有那个高手做过
      

  7.   

    还有我的vs6sp5 没有DnsQuery