⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 winsockdnsclient.cpp

📁 This zip file contain tow TDI client samples.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//****************************************************************************//
//*                                                                           //
//* Copyright (C) 2003, James Antognini, antognini@mindspring.com.            //
//*                                                                           //
//****************************************************************************//

/**************************************************************************************************/      
/*                                                                                                */      
/* Adapted from                                                                                   */
/* ms-help://MS.MSDNQTR.2002JUL.1033/wcewinsk/htm/_wcecomm_TCP_Stream_Socket_Client.htm.          */
/*                                                                                                */      
/**************************************************************************************************/      

#define JAProgNm      "WinsockDNSClient"
#define JAProgVersion "1.00"  

#include <winsock2.h>
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <process.h>
#include <commctrl.h>
#include <winioctl.h>

#include "TDIClient.h"

#define charDfltDNSServerAddr "207.69.188.186"
#define charDfltDNSPort       "53"

PUCHAR
CopyNextStanza(IN PUCHAR pInData, OUT PUCHAR pStr)
  {
   PUCHAR pNextAvail;

   do
     {
      UCHAR tmpName[63+1+1],
            ulChar = *pInData;                        // Pick up length field. 

      if (0==ulChar)                                  // Zero?
        {
         pNextAvail = NULL;
         break;
        }

      memcpy(tmpName, pInData+1, ulChar);             // Copy partial name to temp.

      tmpName[ulChar] = '.';                          // Append a period
      tmpName[ulChar+1] = 0;                          //   and a null terminator.

      strcat((char *)pStr, (char *)tmpName);          // Copy from temp to permanent.

      pNextAvail = pInData +                          // Point to next available byte in permanent.
                   strlen((char *)tmpName);
     }
      while(0);

   return pNextAvail;
  }

int main(
         IN int    nbrArgs,
         IN char * pArgv[]
        )
{
  #define rcOK         0
  #define rcError      8

  #define CmdUnknown   0
  #define CmdStart     1
  #define CmdStop      2
  #define CmdTest      3
  #define CmdTest2     4

  static struct
    {
     DWORD CmdCode;
     char * pInStr;
    }
     InStrArr[] =
       {                                              // Except that CmdUnknown has to be first, the following can be in any order.
        {CmdUnknown, ""},
        {CmdTest,   "DNSServer"},
        {CmdTest2,  "."},
       };

  char             static dfltIPAddr[] =              // Default IP address.
                            charDfltDNSServerAddr,
                          dfltPort[] =                // Default port.
                            charDfltDNSPort,
                          dfltName[] = "www.ibm.com",
                          DateCompiledBase[] = __DATE__,
                          TimeCompiledBase[] = " "__TIME__,
                          JAProgInfo[] = JAProgNm " v" JAProgVersion " (compiled ";

  #define CompDateTimeStr "dd mmm yyyy hh:mm:ss"      

  char                    DateCompiled[12] =          // Build date in preferred (dd mmm yyyy) format.
                            {DateCompiledBase[4], DateCompiledBase[5], DateCompiledBase[6],
                             DateCompiledBase[0], DateCompiledBase[1], DateCompiledBase[2], DateCompiledBase[3],
                             DateCompiledBase[7], DateCompiledBase[8], DateCompiledBase[9], DateCompiledBase[10],
                             0x0
                            },
                          szClientA[TDIClientRecvBfrLen],
                          PgmCompiledInfo[sizeof(CompDateTimeStr)+1];
  char                  * pIPAddr,     
                        * pPort;       
  int rcRecv,                         // Return value of recv function
      port,                           // Port to use.
      i,
      rc = rcOK,
      lnInStrArr =                    // Get number of instances.          
        sizeof InStrArr / sizeof InStrArr[0],                           
      lclStatus,
      sendLn,
      CmdNbr = CmdUnknown;
  BOOL bFirst = TRUE;

  SOCKET ServerSock = INVALID_SOCKET; // Socket bound to the server
  SOCKADDR_IN destination_sin;        // Server socket address
  PHOSTENT pHostEnt = NULL;           // Points to the HOSTENT structure of the server
  WSADATA WSAData;                    // Contains details of the Winsocket implementation
  PUSHORT pShort;

  char * pBuffer = NULL,
       * pQuestData;

  typedef struct _myDNSQuery
    {
     USHORT            QueryId;
     union
       {
        USHORT         QueryFields;
        struct
          {
           USHORT      RecursDesired : 1;             // 1 ==> recursion desired.
           USHORT      Truncd        : 1;             // 1 ==> message truncated.  Meaningful only in responses.
           USHORT      AuthAnsw      : 1;             // 1 ==> authoritative answer.  Meaningful only in responses.
           USHORT      OpcodeFd      : 4;             // 0 ==> standard query.
           USHORT      QryReqFd      : 1;             // 0 ==> query; 1 ==> response.
           USHORT      RespCode      : 4;             // Response code:  0 ==> success;
                                                      //                 1 ==> format error; 
                                                      //                 2 ==> server error; 
                                                      //                 3 ==> name error; 
                                                      //                 4 ==> not implemented;
                                                      //                 5 ==> refused.
           USHORT      ZReserved     : 3;             // Reserved.
           USHORT      RecursAvail   : 1;             // 1 ==> recursion available in server.
          };
       };
     USHORT            QuestionCount;                 // Number of entries in question section.
     USHORT            AnswerCount;                   // Number of resource records (RRs) in answer section.
     USHORT            NSRRCount;                     // Number of name-server RRs in authority-records section.
     USHORT            AddRRCount;                    // Number of RRs in addition records section.
     char              Question[1];                   // Beginning of question section.
    }
     myDNSQuery, * pMyDNSQuery;

  typedef struct _myDNSRR                             // Map response AFTER name field, which may be a name or an offset to a name.
    {
     USHORT            RRType;                        // RR type code.
     USHORT            RRClass;                       // Class of data in RRData.
     ULONG             RRTTL;                         // Time to live, in seconds.
     USHORT            RRRDataLn;                     // Size of RRData.
     char              RRData[1];                     // Response record data.  Interpretation depends on RespType and RespClass.
    }
     myDNSRR, * pMyDNSRR;

  pMyDNSQuery pQryData,
              pRspData;

  pMyDNSRR    pRRData;

  if (' '==DateCompiled[0])
   strcpy(PgmCompiledInfo, DateCompiled+1);
  else
   strcpy(PgmCompiledInfo, DateCompiled+0);

  strcat(PgmCompiledInfo, TimeCompiledBase);

  printf("%s%s)\n", JAProgInfo, PgmCompiledInfo);

  if (nbrArgs>=2)
    {
     for (i = 1; i < lnInStrArr; i ++)                // Go through expected parameters.
       if (0==_stricmp(pArgv[1], InStrArr[i].pInStr)) // Does the second parm match?
         break;                                       // If yes, break.
    }
  else
    i = lnInStrArr;                                   // Ensure next conditional yields supported parameter information.

  if (i<lnInStrArr)                                   // Found a supported command?
    {
     CmdNbr = InStrArr[i].CmdCode;
     if (CmdTest2==CmdNbr)                            // Dot given?
       CmdNbr = CmdTest;                              // Use full command.
    }
  else
    {
     printf("\nUnsupported command\n\n");
     printf("Usage: " JAProgNm " <operand>\n\n");
     printf("       where <operand> is one of these:\n\n");
     for (i = 1; i < lnInStrArr; i ++)
       printf("         %s\n", InStrArr[i].pInStr);
     printf("\n       Eg,\n\n         <DNSServer  | . <IP address | IP name | .> <port | .>>\n");
     printf("\n       (Capitalization is ignored.)\n");
     printf("\n       (Defaults:  IP addr = %s, port = %s.)\n", dfltIPAddr, dfltPort);

     rc = 1;
     goto ExitPoint;
    }

  if (nbrArgs>=3)
    {
     pIPAddr = pArgv[2];                              // Get IP address.
     if (0==strcmp(pIPAddr, "."))                     // Dot given?
       pIPAddr = dfltIPAddr;                          // Point to default IP address.
    }
  else
    pIPAddr = dfltIPAddr;                             // Get IP address.

  if (4==nbrArgs)
    {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -