小七技术论坛,计算机安全和杀毒软件,编程交流于软件安全技术交流,给新手一个绿色安全的论坛

标题: Ghost的编译问题 [打印本页]

作者: asd526818    时间: 2013-4-4 11:32
标题: Ghost的编译问题
为什么我用C++编译svchost files会出错,我的SDK和XTP库都安装上了
--------------------Configuration: svchost - Win32 Release--------------------
Compiling resources...
Compiling...
Audio.cpp
AudioManager.cpp
Buffer.cpp
ClientSocket.cpp
Dialupass.cpp
DTDY.cpp
FileManager.cpp
install.cpp
KernelManager.cpp
D:\我的文档\黑客软件\远控源码\Ghost源码\Server\svchost\common\Proxy.h(20) : note C6311: D:\我的文档\黑客软件\远控源码\Ghost源码\Server\svchost\common\portmapping.cpp(44) : see previous definition of 'MAXBUFSIZE'
D:\VC++\Microsoft Visual Studio\VC98\INCLUDE\wspiapi.h(45) : error C2265: '<Unknown>' : reference to a zero-sized array is illegal
D:\VC++\Microsoft Visual Studio\VC98\INCLUDE\wspiapi.h(45) : error C2265: '<Unknown>' : reference to a zero-sized array is illegal
KeyboardManager.cpp
Manager.cpp
RegEditEx.cpp
RegeditOpt.cpp
Registry.cpp
RegManager.cpp
ScreenManager.cpp
ScreenSpy.cpp
ServersManagement.cpp
ServersManager.cpp
ShellManager.cpp
Generating Code...
Compiling...
svchost.cpp
SystemManager.cpp
until.cpp
VideoCap.cpp
VideoManager.cpp
XvidDec.cpp
XvidEnc.cpp
Generating Code...
执行 cl.exe 时出错.

main.exe - 1 error(s), 0 warning(s)
作者: 丝腰    时间: 2013-4-5 00:38
这是神马东西额 操蛋 了
作者: hkyaxu    时间: 2013-4-5 08:02
111111111111
作者: haoran    时间: 2013-6-20 12:53
用这个wspiapi.h
  1. /*++

  2. Copyright (c) Microsoft Corporation. All rights reserved.

  3. Module Name:
  4.     wspiapi.h

  5. Abstract:
  6.     The file contains protocol independent API functions.

  7. Revision History:
  8.     Wed Jul 12 10:50:31 2000, Created

  9. --*/

  10. #ifndef _WSPIAPI_H_
  11. #define _WSPIAPI_COUNTOF

  12. #include <stdio.h>              // sprintf()
  13. #include <stdlib.h>             // calloc(), strtoul()
  14. #include <malloc.h>             // calloc()
  15. #include <string.h>             // strlen(), strcmp(), strstr()

  16. #if defined(__GOT_SECURE_LIB__) && __GOT_SECURE_LIB__ >= 200402L

  17. #define _WSPIAPI_STRCPY_S strcpy_s
  18. #define _WSPIAPI_STRCAT_S strcat_s
  19. #define _WSPIAPI_STRNCPY_S strncpy_s
  20. #define _WSPIAPI_SPRINTF_S_1 sprintf_s

  21. #else

  22. #define _WSPIAPI_STRCPY_S(_Dst, _Size, _Src) strcpy((_Dst), (_Src))
  23. #define _WSPIAPI_STRCAT_S(_Dst, _Size, _Src) strcat((_Dst), (_Src))
  24. #define _WSPIAPI_STRNCPY_S(_Dst, _Size, _Src, _Count) strncpy((_Dst), (_Src), (_Count)); (_Dst)[(_Size) - 1] = 0
  25. #define _WSPIAPI_SPRINTF_S_1(_Dst, _Size, _Format, _Arg1) sprintf((_Dst), (_Format), (_Arg1))

  26. #endif // defined(__GOT_SECURE_LIB__) && __GOT_SECURE_LIB__ >= 200402L

  27. #if !defined(_WSPIAPI_COUNTOF)
  28. #if !defined(__cplusplus)
  29. #define _WSPIAPI_COUNTOF(_Array) (sizeof(_Array) / sizeof(_Array[0]))
  30. #else
  31. template <typename __CountofType, size_t _N>
  32. char (&__wspiapi_countof_helper(__CountofType (&_Array)[_N]))[_N];
  33. #define _WSPIAPI_COUNTOF(_Array) sizeof(__wspiapi_countof_helper(_Array))
  34. #endif
  35. #endif

  36. #define WspiapiMalloc(tSize)    calloc(1, (tSize))
  37. #define WspiapiFree(p)          free(p)
  38. #define WspiapiSwap(a, b, c)    { (c) = (a); (a) = (b); (b) = (c); }
  39. #define getaddrinfo             WspiapiGetAddrInfo
  40. #define getnameinfo             WspiapiGetNameInfo
  41. #define freeaddrinfo            WspiapiFreeAddrInfo

  42. typedef int (WINAPI *WSPIAPI_PGETADDRINFO) (
  43.     IN  const char                      *nodename,
  44.     IN  const char                      *servname,
  45.     IN  const struct addrinfo           *hints,
  46.     OUT struct addrinfo                 **res);

  47. typedef int (WINAPI *WSPIAPI_PGETNAMEINFO) (
  48.     IN  const struct sockaddr           *sa,
  49.     IN  socklen_t                       salen,
  50.     OUT char                            *host,
  51.     IN  size_t                          hostlen,
  52.     OUT char                            *serv,
  53.     IN  size_t                          servlen,
  54.     IN  int                             flags);

  55. typedef void (WINAPI *WSPIAPI_PFREEADDRINFO) (
  56.     IN  struct addrinfo                 *ai);



  57. #ifdef __cplusplus
  58. extern "C" {
  59. #endif
  60.    
  61. ////////////////////////////////////////////////////////////
  62. // v4 only versions of getaddrinfo and friends.
  63. // NOTE: gai_strerror is inlined in ws2tcpip.h
  64. ////////////////////////////////////////////////////////////

  65. _inline   
  66. char *
  67. WINAPI
  68. WspiapiStrdup (
  69.         IN  const char *                    pszString)
  70. /*++

  71. Routine Description
  72.     allocates enough storage via calloc() for a copy of the string,
  73.     copies the string into the new memory, and returns a pointer to it.

  74. Arguments
  75.     pszString       string to copy into new memory

  76. Return Value
  77.     a pointer to the newly allocated storage with the string in it.
  78.     NULL if enough memory could not be allocated, or string was NULL.

  79. --*/   
  80. {
  81.     char    *pszMemory;
  82.     size_t  cchMemory;

  83.     if (!pszString)
  84.         return(NULL);

  85.     cchMemory = strlen(pszString) + 1;
  86.     pszMemory = (char *) WspiapiMalloc(cchMemory);
  87.     if (!pszMemory)
  88.         return(NULL);

  89.     _WSPIAPI_STRCPY_S(pszMemory, cchMemory, pszString);
  90.     return pszMemory;
  91. }

  92.    
  93.    
  94. __inline
  95. BOOL
  96. WINAPI
  97. WspiapiParseV4Address (
  98.     IN  const char *                    pszAddress,
  99.     OUT PDWORD                          pdwAddress)
  100. /*++

  101. Routine Description
  102.     get the IPv4 address (in network byte order) from its string
  103.     representation.  the syntax should be a.b.c.d.
  104.    
  105. Arguments
  106.     pszArgument         string representation of the IPv4 address
  107.     ptAddress           pointer to the resulting IPv4 address

  108. Return Value
  109.     Returns FALSE if there is an error, TRUE for success.
  110.    
  111. --*/
  112. {
  113.     DWORD       dwAddress   = 0;
  114.     const char  *pcNext     = NULL;
  115.     int         iCount      = 0;

  116.     // ensure there are 3 '.' (periods)
  117.     for (pcNext = pszAddress; *pcNext != '\0'; pcNext++)
  118.         if (*pcNext == '.')
  119.             iCount++;
  120.     if (iCount != 3)
  121.         return FALSE;

  122.     // return an error if dwAddress is INADDR_NONE (255.255.255.255)
  123.     // since this is never a valid argument to getaddrinfo.
  124.     dwAddress = inet_addr(pszAddress);
  125.     if (dwAddress == INADDR_NONE)
  126.         return FALSE;

  127.     *pdwAddress = dwAddress;
  128.     return TRUE;
  129. }



  130. __inline
  131. struct addrinfo *
  132. WINAPI
  133. WspiapiNewAddrInfo (
  134.     IN  int                             iSocketType,
  135.     IN  int                             iProtocol,
  136.     IN  WORD                            wPort,
  137.     IN  DWORD                           dwAddress)
  138. /*++

  139. Routine Description
  140.     allocate an addrinfo structure and populate fields.
  141.     IPv4 specific internal function, not exported.
  142.    
  143. Arguments
  144.     iSocketType         SOCK_*.  can be wildcarded (zero).
  145.     iProtocol           IPPROTO_*.  can be wildcarded (zero).
  146.     wPort               port number of service (in network order).
  147.     dwAddress           IPv4 address (in network order).
  148.    
  149. Return Value
  150.     returns an addrinfo struct, or NULL if out of memory.

  151. --*/   
  152. {
  153.     struct addrinfo     *ptNew;
  154.     struct sockaddr_in  *ptAddress;

  155.     // allocate a new addrinfo structure.
  156.     ptNew       =
  157.         (struct addrinfo *) WspiapiMalloc(sizeof(struct addrinfo));
  158.     if (!ptNew)
  159.         return NULL;

  160.     ptAddress   =
  161.         (struct sockaddr_in *) WspiapiMalloc(sizeof(struct sockaddr_in));
  162.     if (!ptAddress)
  163.     {
  164.         WspiapiFree(ptNew);
  165.         return NULL;
  166.     }
  167.     ptAddress->sin_family       = AF_INET;
  168.     ptAddress->sin_port         = wPort;
  169.     ptAddress->sin_addr.s_addr  = dwAddress;
  170.    
  171.     // fill in the fields...
  172.     ptNew->ai_family            = PF_INET;
  173.     ptNew->ai_socktype          = iSocketType;
  174.     ptNew->ai_protocol          = iProtocol;
  175.     ptNew->ai_addrlen           = sizeof(struct sockaddr_in);
  176.     ptNew->ai_addr              = (struct sockaddr *) ptAddress;

  177.     return ptNew;
  178. }



  179. __inline
  180. int
  181. WINAPI
  182. WspiapiQueryDNS(
  183.     IN  const char                      *pszNodeName,
  184.     IN  int                             iSocketType,
  185.     IN  int                             iProtocol,  
  186.     IN  WORD                            wPort,      
  187.     OUT char                            pszAlias[NI_MAXHOST],
  188.     OUT struct addrinfo                 **pptResult)
  189. /*++

  190. Routine Description
  191.     helper routine for WspiapiLookupNode.
  192.     performs name resolution by querying the DNS for A records.
  193.     *pptResult would need to be freed if an error is returned.
  194.    
  195. Arguments
  196.     pszNodeName         name of node to resolve.
  197.     iSocketType         SOCK_*.  can be wildcarded (zero).
  198.     iProtocol           IPPROTO_*.  can be wildcarded (zero).
  199.     wPort               port number of service (in network order).
  200.     pszAlias            where to return the alias.  must be of size NI_MAXHOST.
  201.     pptResult           where to return the result.
  202.    
  203. Return Value
  204.     Returns 0 on success, an EAI_* style error value otherwise.

  205. --*/   
  206. {
  207.     struct addrinfo **pptNext   = pptResult;
  208.     struct hostent  *ptHost     = NULL;
  209.     char            **ppAddresses;

  210.     *pptNext    = NULL;
  211.     pszAlias[0] = '\0';

  212.     ptHost = gethostbyname(pszNodeName);
  213.     if (ptHost)
  214.     {
  215.         if ((ptHost->h_addrtype == AF_INET)     &&
  216.             (ptHost->h_length   == sizeof(struct in_addr)))
  217.         {
  218.             for (ppAddresses    = ptHost->h_addr_list;
  219.                  *ppAddresses   != NULL;
  220.                  ppAddresses++)
  221.             {
  222.                 // create an addrinfo structure...
  223.                 *pptNext = WspiapiNewAddrInfo(
  224.                     iSocketType,
  225.                     iProtocol,
  226.                     wPort,
  227.                     ((struct in_addr *) *ppAddresses)->s_addr);
  228.                 if (!*pptNext)
  229.                     return EAI_MEMORY;

  230.                 pptNext = &((*pptNext)->ai_next);
  231.             }
  232.         }

  233.         // pick up the canonical name.
  234.         _WSPIAPI_STRNCPY_S(pszAlias, NI_MAXHOST, ptHost->h_name, NI_MAXHOST - 1);
  235.         
  236.         return 0;
  237.     }
  238.    
  239.     switch (WSAGetLastError())
  240.     {
  241.         case WSAHOST_NOT_FOUND: return EAI_NONAME;
  242.         case WSATRY_AGAIN:      return EAI_AGAIN;
  243.         case WSANO_RECOVERY:    return EAI_FAIL;
  244.         case WSANO_DATA:        return EAI_NODATA;
  245.         default:                return EAI_NONAME;
  246.     }
  247. }



  248. __inline
  249. int
  250. WINAPI
  251. WspiapiLookupNode(
  252.     IN  const char                      *pszNodeName,
  253.     IN  int                             iSocketType,
  254.     IN  int                             iProtocol,  
  255.     IN  WORD                            wPort,      
  256.     IN  BOOL                            bAI_CANONNAME,
  257.     OUT struct addrinfo                 **pptResult)
  258. /*++

  259. Routine Description
  260.     resolve a nodename and return a list of addrinfo structures.
  261.     IPv4 specific internal function, not exported.
  262.     *pptResult would need to be freed if an error is returned.
  263.    
  264.     NOTE: if bAI_CANONNAME is true, the canonical name should be
  265.           returned in the first addrinfo structure.
  266.    
  267. Arguments
  268.     pszNodeName         name of node to resolve.
  269.     iSocketType         SOCK_*.  can be wildcarded (zero).
  270.     iProtocol           IPPROTO_*.  can be wildcarded (zero).
  271.     wPort               port number of service (in network order).
  272.     bAI_CANONNAME       whether the AI_CANONNAME flag is set.
  273.     pptResult           where to return result.
  274.    
  275. Return Value
  276.     Returns 0 on success, an EAI_* style error value otherwise.

  277. --*/
  278. {
  279.     int     iError              = 0;
  280.     int     iAliasCount         = 0;

  281.     char    szFQDN1[NI_MAXHOST] = "";
  282.     char    szFQDN2[NI_MAXHOST] = "";
  283.     char    *pszName            = szFQDN1;
  284.     char    *pszAlias           = szFQDN2;
  285.     char    *pszScratch         = NULL;
  286.     _WSPIAPI_STRNCPY_S(pszName, NI_MAXHOST, pszNodeName, NI_MAXHOST - 1);
  287.    
  288.     for (;;)
  289.     {
  290.         iError = WspiapiQueryDNS(pszNodeName,
  291.                                  iSocketType,
  292.                                  iProtocol,
  293.                                  wPort,
  294.                                  pszAlias,
  295.                                  pptResult);
  296.         if (iError)
  297.             break;

  298.         // if we found addresses, then we are done.
  299.         if (*pptResult)
  300.             break;

  301.         // stop infinite loops due to DNS misconfiguration.  there appears
  302.         // to be no particular recommended limit in RFCs 1034 and 1035.
  303.         if ((!strlen(pszAlias))             ||
  304.             (!strcmp(pszName, pszAlias))    ||
  305.             (++iAliasCount == 16))
  306.         {
  307.             iError = EAI_FAIL;
  308.             break;
  309.         }

  310.         // there was a new CNAME, look again.
  311.         WspiapiSwap(pszName, pszAlias, pszScratch);
  312.     }

  313.     if (!iError && bAI_CANONNAME)
  314.     {
  315.         (*pptResult)->ai_canonname = WspiapiStrdup(pszAlias);
  316.         if (!(*pptResult)->ai_canonname)
  317.             iError = EAI_MEMORY;
  318.     }

  319.     return iError;
  320. }



  321. __inline
  322. int
  323. WINAPI
  324. WspiapiClone (
  325.     IN  WORD                            wPort,      
  326.     IN  struct addrinfo                 *ptResult)
  327. /*++

  328. Routine Description
  329.     clone every addrinfo structure in ptResult for the UDP service.
  330.     ptResult would need to be freed if an error is returned.
  331.    
  332. Arguments
  333.     wPort               port number of UDP service.
  334.     ptResult            list of addrinfo structures, each
  335.                         of whose node needs to be cloned.

  336. Return Value
  337.     Returns 0 on success, an EAI_MEMORY on allocation failure.

  338. --*/
  339. {
  340.     struct addrinfo *ptNext = NULL;
  341.     struct addrinfo *ptNew  = NULL;

  342.     for (ptNext = ptResult; ptNext != NULL; )
  343.     {
  344.         // create an addrinfo structure...
  345.         ptNew = WspiapiNewAddrInfo(
  346.             SOCK_DGRAM,
  347.             ptNext->ai_protocol,
  348.             wPort,
  349.             ((struct sockaddr_in *) ptNext->ai_addr)->sin_addr.s_addr);
  350.         if (!ptNew)
  351.             break;

  352.         // link the cloned addrinfo
  353.         ptNew->ai_next  = ptNext->ai_next;
  354.         ptNext->ai_next = ptNew;
  355.         ptNext          = ptNew->ai_next;
  356.     }

  357.     if (ptNext != NULL)
  358.         return EAI_MEMORY;
  359.    
  360.     return 0;
  361. }



  362. __inline
  363. void
  364. WINAPI
  365. WspiapiLegacyFreeAddrInfo (
  366.     IN  struct addrinfo                 *ptHead)
  367. /*++

  368. Routine Description
  369.     Free an addrinfo structure (or chain of structures).
  370.     As specified in RFC 2553, Section 6.4.
  371.    
  372. Arguments
  373.     ptHead              structure (chain) to free
  374.    
  375. --*/   
  376. {
  377.     struct addrinfo *ptNext;    // next strcture to free

  378.     for (ptNext = ptHead; ptNext != NULL; ptNext = ptHead)
  379.     {
  380.         if (ptNext->ai_canonname)
  381.             WspiapiFree(ptNext->ai_canonname);
  382.         
  383.         if (ptNext->ai_addr)
  384.             WspiapiFree(ptNext->ai_addr);

  385.         ptHead = ptNext->ai_next;
  386.         WspiapiFree(ptNext);
  387.     }
  388. }



  389. __inline
  390. int
  391. WINAPI
  392. WspiapiLegacyGetAddrInfo(
  393.     IN const char                       *pszNodeName,
  394.     IN const char                       *pszServiceName,
  395.     IN const struct addrinfo            *ptHints,
  396.     OUT struct addrinfo                 **pptResult)
  397. /*++

  398. Routine Description
  399.     Protocol-independent name-to-address translation.
  400.     As specified in RFC 2553, Section 6.4.
  401.     This is the hacked version that only supports IPv4.
  402.    
  403. Arguments
  404.     pszNodeName         node name to lookup.
  405.     pszServiceName      service name to lookup.
  406.     ptHints             hints about how to process request.
  407.     pptResult           where to return result.
  408.    
  409. Return Value
  410.     returns zero if successful, an EAI_* error code if not.

  411. --*/   
  412. {
  413.     int                 iError      = 0;
  414.     int                 iFlags      = 0;
  415.     int                 iFamily     = PF_UNSPEC;
  416.     int                 iSocketType = 0;
  417.     int                 iProtocol   = 0;
  418.     WORD                wPort       = 0;
  419.     DWORD               dwAddress   = 0;

  420.     struct servent      *ptService  = NULL;
  421.     char                *pc         = NULL;
  422.     BOOL                bClone      = FALSE;
  423.     WORD                wTcpPort    = 0;
  424.     WORD                wUdpPort    = 0;
  425.    
  426.    
  427.     // initialize pptResult with default return value.
  428.     *pptResult  = NULL;


  429.     ////////////////////////////////////////
  430.     // validate arguments...
  431.     //
  432.    
  433.     // both the node name and the service name can't be NULL.
  434.     if ((!pszNodeName) && (!pszServiceName))
  435.         return EAI_NONAME;

  436.     // validate hints.
  437.     if (ptHints)
  438.     {
  439.         // all members other than ai_flags, ai_family, ai_socktype
  440.         // and ai_protocol must be zero or a null pointer.
  441.         if ((ptHints->ai_addrlen    != 0)       ||
  442.             (ptHints->ai_canonname  != NULL)    ||
  443.             (ptHints->ai_addr       != NULL)    ||
  444.             (ptHints->ai_next       != NULL))
  445.         {
  446.             return EAI_FAIL;
  447.         }
  448.         
  449.         // the spec has the "bad flags" error code, so presumably we
  450.         // should check something here.  insisting that there aren't
  451.         // any unspecified flags set would break forward compatibility,
  452.         // however.  so we just check for non-sensical combinations.
  453.         //
  454.         // we cannot come up with a canonical name given a null node name.
  455.         iFlags      = ptHints->ai_flags;
  456.         if ((iFlags & AI_CANONNAME) && !pszNodeName)
  457.             return EAI_BADFLAGS;

  458.         // we only support a limited number of protocol families.
  459.         iFamily     = ptHints->ai_family;
  460.         if ((iFamily != PF_UNSPEC) && (iFamily != PF_INET))
  461.             return EAI_FAMILY;

  462.         // we only support only these socket types.
  463.         iSocketType = ptHints->ai_socktype;
  464.         if ((iSocketType != 0)                  &&
  465.             (iSocketType != SOCK_STREAM)        &&
  466.             (iSocketType != SOCK_DGRAM)         &&
  467.             (iSocketType != SOCK_RAW))
  468.             return EAI_SOCKTYPE;

  469.         // REVIEW: What if ai_socktype and ai_protocol are at odds?
  470.         iProtocol   = ptHints->ai_protocol;
  471.     }


  472.     ////////////////////////////////////////
  473.     // do service lookup...

  474.     if (pszServiceName)
  475.     {
  476.         wPort = (WORD) strtoul(pszServiceName, &pc, 10);
  477.         if (*pc == '\0')        // numeric port string
  478.         {
  479.             wPort = wTcpPort = wUdpPort = htons(wPort);
  480.             if (iSocketType == 0)
  481.             {
  482.                 bClone      = TRUE;
  483.                 iSocketType = SOCK_STREAM;
  484.             }
  485.         }
  486.         else                    // non numeric port string
  487.         {
  488.             if ((iSocketType == 0) || (iSocketType == SOCK_DGRAM))
  489.             {
  490.                 ptService = getservbyname(pszServiceName, "udp");
  491.                 if (ptService)
  492.                     wPort = wUdpPort = ptService->s_port;
  493.             }

  494.             if ((iSocketType == 0) || (iSocketType == SOCK_STREAM))
  495.             {
  496.                 ptService = getservbyname(pszServiceName, "tcp");
  497.                 if (ptService)
  498.                     wPort = wTcpPort = ptService->s_port;
  499.             }
  500.             
  501.             // assumes 0 is an invalid service port...
  502.             if (wPort == 0)     // no service exists
  503.                 return (iSocketType ? EAI_SERVICE : EAI_NONAME);

  504.             if (iSocketType == 0)
  505.             {
  506.                 // if both tcp and udp, process tcp now & clone udp later.
  507.                 iSocketType = (wTcpPort) ? SOCK_STREAM : SOCK_DGRAM;
  508.                 bClone      = (wTcpPort && wUdpPort);
  509.             }
  510.         }
  511.     }
  512.    


  513.     ////////////////////////////////////////
  514.     // do node name lookup...

  515.     // if we weren't given a node name,
  516.     // return the wildcard or loopback address (depending on AI_PASSIVE).
  517.     //
  518.     // if we have a numeric host address string,
  519.     // return the binary address.
  520.     //
  521.     if ((!pszNodeName) || (WspiapiParseV4Address(pszNodeName, &dwAddress)))
  522.     {
  523.         if (!pszNodeName)
  524.         {
  525.             dwAddress = htonl((iFlags & AI_PASSIVE)
  526.                               ? INADDR_ANY
  527.                               : INADDR_LOOPBACK);
  528.         }
  529.         
  530.         // create an addrinfo structure...
  531.         *pptResult =
  532.             WspiapiNewAddrInfo(iSocketType, iProtocol, wPort, dwAddress);
  533.         if (!(*pptResult))
  534.             iError = EAI_MEMORY;
  535.         
  536.         if (!iError && pszNodeName)
  537.         {
  538.             // implementation specific behavior: set AI_NUMERICHOST
  539.             // to indicate that we got a numeric host address string.
  540.             (*pptResult)->ai_flags |= AI_NUMERICHOST;
  541.             
  542.             // return the numeric address string as the canonical name
  543.             if (iFlags & AI_CANONNAME)
  544.             {
  545.                 (*pptResult)->ai_canonname =
  546.                     WspiapiStrdup(inet_ntoa(*((struct in_addr *) &dwAddress)));
  547.                 if (!(*pptResult)->ai_canonname)        
  548.                     iError = EAI_MEMORY;
  549.             }
  550.         }
  551.     }


  552.     // if we do not have a numeric host address string and
  553.     // AI_NUMERICHOST flag is set, return an error!
  554.     else if (iFlags & AI_NUMERICHOST)
  555.     {
  556.         iError = EAI_NONAME;
  557.     }
  558.    

  559.     // since we have a non-numeric node name,
  560.     // we have to do a regular node name lookup.
  561.     else
  562.     {
  563.         iError = WspiapiLookupNode(pszNodeName,
  564.                                    iSocketType,
  565.                                    iProtocol,
  566.                                    wPort,
  567.                                    (iFlags & AI_CANONNAME),
  568.                                    pptResult);
  569.     }

  570.     if (!iError && bClone)
  571.     {
  572.         iError = WspiapiClone(wUdpPort, *pptResult);
  573.     }

  574.     if (iError)
  575.     {
  576.         WspiapiLegacyFreeAddrInfo(*pptResult);
  577.         *pptResult  = NULL;        
  578.     }

  579.     return (iError);
  580. }



  581. __inline
  582. int
  583. WINAPI
  584. WspiapiLegacyGetNameInfo(
  585.     IN  const struct sockaddr           *ptSocketAddress,
  586.     IN  socklen_t                       tSocketLength,
  587.     OUT char                            *pszNodeName,
  588.     IN  size_t                          tNodeLength,
  589.     OUT char                            *pszServiceName,
  590.     IN  size_t                          tServiceLength,
  591.     IN  int                             iFlags)
  592. /*++

  593. Routine Description
  594.     protocol-independent address-to-name translation.
  595.     as specified in RFC 2553, Section 6.5.
  596.     this is the hacked version that only supports IPv4.
  597.    
  598. Arguments
  599.     ptSocketAddress     socket address to translate.
  600.     tSocketLength       length of above socket address.
  601.     pszNodeName         where to return the node name.
  602.     tNodeLength         size of above buffer.
  603.     pszServiceName      where to return the service name.
  604.     tServiceLength      size of above buffer.
  605.     iFlags              flags of type NI_*.
  606.    
  607. Return Value
  608.     returns zero if successful, an EAI_* error code if not.

  609. --*/   
  610. {
  611.     struct servent  *ptService;
  612.     WORD            wPort;   
  613.     char            szBuffer[]  = "65535";
  614.     char            *pszService = szBuffer;

  615.     struct hostent  *ptHost;
  616.     struct in_addr  tAddress;
  617.     char            *pszNode    = NULL;
  618.     char            *pc         = NULL;
  619.    

  620.     // sanity check ptSocketAddress and tSocketLength.
  621.     if ((!ptSocketAddress) || (tSocketLength < sizeof(struct sockaddr)))
  622.         return EAI_FAIL;
  623.    
  624.     if (ptSocketAddress->sa_family != AF_INET)
  625.         return EAI_FAMILY;

  626.     if (tSocketLength < sizeof(struct sockaddr_in))
  627.         return EAI_FAIL;
  628.    
  629.     if (!(pszNodeName && tNodeLength) &&
  630.         !(pszServiceName && tServiceLength))
  631.     {
  632.         return EAI_NONAME;   
  633.     }

  634.     // the draft has the "bad flags" error code, so presumably we
  635.     // should check something here.  insisting that there aren't
  636.     // any unspecified flags set would break forward compatibility,
  637.     // however.  so we just check for non-sensical combinations.
  638.     if ((iFlags & NI_NUMERICHOST) && (iFlags & NI_NAMEREQD))
  639.     {                                                                       
  640.         return EAI_BADFLAGS;
  641.     }
  642.         
  643.     // translate the port to a service name (if requested).
  644.     if (pszServiceName && tServiceLength)
  645.     {
  646.         wPort = ((struct sockaddr_in *) ptSocketAddress)->sin_port;
  647.         
  648.         if (iFlags & NI_NUMERICSERV)
  649.         {
  650.             // return numeric form of the address.
  651.             _WSPIAPI_SPRINTF_S_1(szBuffer, _WSPIAPI_COUNTOF(szBuffer), "%u", ntohs(wPort));
  652.         }
  653.         else
  654.         {
  655.             // return service name corresponding to port.
  656.             ptService = getservbyport(wPort,
  657.                                       (iFlags & NI_DGRAM) ? "udp" : NULL);
  658.             if (ptService && ptService->s_name)
  659.             {
  660.                 // lookup successful.
  661.                 pszService = ptService->s_name;
  662.             }
  663.             else
  664.             {
  665.                 // DRAFT: return numeric form of the port!
  666.                 _WSPIAPI_SPRINTF_S_1(szBuffer, _WSPIAPI_COUNTOF(szBuffer), "%u", ntohs(wPort));
  667.             }
  668.         }
  669.         
  670.         
  671.         if (tServiceLength > strlen(pszService))
  672.             _WSPIAPI_STRCPY_S(pszServiceName, tServiceLength, pszService);
  673.         else
  674.             return EAI_FAIL;
  675.     }

  676.    
  677.     // translate the address to a node name (if requested).
  678.     if (pszNodeName && tNodeLength)
  679.     {   
  680.         // this is the IPv4-only version, so we have an IPv4 address.
  681.         tAddress = ((struct sockaddr_in *) ptSocketAddress)->sin_addr;

  682.         if (iFlags & NI_NUMERICHOST)
  683.         {
  684.             // return numeric form of the address.
  685.             pszNode  = inet_ntoa(tAddress);
  686.         }
  687.         else
  688.         {
  689.             // return node name corresponding to address.
  690.             ptHost = gethostbyaddr((char *) &tAddress,
  691.                                    sizeof(struct in_addr),
  692.                                    AF_INET);
  693.             if (ptHost && ptHost->h_name)
  694.             {
  695.                 // DNS lookup successful.
  696.                 // stop copying at a "." if NI_NOFQDN is specified.
  697.                 pszNode = ptHost->h_name;
  698.                 if ((iFlags & NI_NOFQDN) &&
  699.                     ((pc = strchr(pszNode, '.')) != NULL))
  700.                     *pc = '\0';
  701.             }
  702.             else
  703.             {
  704.                 // DNS lookup failed.  return numeric form of the address.
  705.                 if (iFlags & NI_NAMEREQD)
  706.                 {
  707.                     switch (WSAGetLastError())
  708.                     {
  709.                         case WSAHOST_NOT_FOUND: return EAI_NONAME;
  710.                         case WSATRY_AGAIN:      return EAI_AGAIN;
  711.                         case WSANO_RECOVERY:    return EAI_FAIL;
  712.                         default:                return EAI_NONAME;
  713.                     }
  714.                 }
  715.                 else
  716.                     pszNode  = inet_ntoa(tAddress);
  717.             }
  718.         }

  719.         if (tNodeLength > strlen(pszNode))
  720.             _WSPIAPI_STRCPY_S(pszNodeName, tNodeLength, pszNode);
  721.         else
  722.             return EAI_FAIL;
  723.     }

  724.     return 0;
  725. }



  726. typedef struct
  727. {
  728.     char const          *pszName;
  729.     FARPROC             pfAddress;
  730. } WSPIAPI_FUNCTION;

  731. #define WSPIAPI_FUNCTION_ARRAY                                  \
  732. {                                                               \
  733.     "getaddrinfo",      (FARPROC) WspiapiLegacyGetAddrInfo,     \
  734.     "getnameinfo",      (FARPROC) WspiapiLegacyGetNameInfo,     \
  735.     "freeaddrinfo",     (FARPROC) WspiapiLegacyFreeAddrInfo,    \
  736. }



  737. __inline
  738. FARPROC
  739. WINAPI
  740. WspiapiLoad(
  741.     IN  WORD                            wFunction)
  742. /*++

  743. Routine Description
  744.     try to locate the address family independent name resolution routines
  745.     (i.e. getaddrinfo, getnameinfo, freeaddrinfo, gai_strerror).
  746.    
  747. Locks
  748.     this function call is not synchronized.  hence the library containing
  749.     the routines might be loaded multiple times.  another option is to
  750.     synchronize through a spin lock using a static local variable and the
  751.     InterlockedExchange operation.  

  752.    
  753. Arguments
  754.     wFunction           ordinal # of the function to get the pointer to
  755.                         0   getaddrinfo
  756.                         1   getnameinfo
  757.                         2   freeaddrinfo
  758.    
  759. Return Value
  760.     address of the library/legacy routine

  761. --*/
  762. {
  763.     HMODULE                 hLibrary        = NULL;

  764.     // these static variables store state across calls, across threads.
  765.     static BOOL             bInitialized    = FALSE;
  766.     static WSPIAPI_FUNCTION rgtGlobal[]     = WSPIAPI_FUNCTION_ARRAY;
  767.     static const int        iNumGlobal      = (sizeof(rgtGlobal) /
  768.                                                sizeof(WSPIAPI_FUNCTION));
  769.    
  770.     // we overwrite rgtGlobal only if all routines exist in library.
  771.     WSPIAPI_FUNCTION        rgtLocal[]      = WSPIAPI_FUNCTION_ARRAY;
  772.     FARPROC                 fScratch        = NULL;
  773.     int                     i               = 0;
  774.    
  775.    
  776.     if (bInitialized)           // WspiapiLoad has already been called once
  777.         return (rgtGlobal[wFunction].pfAddress);

  778.     for (;;)                    // breakout loop
  779.     {
  780.         CHAR SystemDir[MAX_PATH + 1];
  781.         CHAR Path[MAX_PATH + 8];

  782.         if (GetSystemDirectoryA(SystemDir, MAX_PATH) == 0)
  783.         {
  784.             break;
  785.         }

  786.         // in Whistler and beyond...
  787.         // the routines are present in the WinSock 2 library (ws2_32.dll).
  788.         // printf("Looking in ws2_32 for getaddrinfo...\n");
  789.         _WSPIAPI_STRCPY_S(Path, _WSPIAPI_COUNTOF(Path), SystemDir);
  790.         _WSPIAPI_STRCAT_S(Path, _WSPIAPI_COUNTOF(Path), "\\ws2_32");
  791.         hLibrary = LoadLibraryA(Path);
  792.         if (hLibrary != NULL)
  793.         {
  794.             fScratch = GetProcAddress(hLibrary, "getaddrinfo");
  795.             if (fScratch == NULL)
  796.             {
  797.                 FreeLibrary(hLibrary);
  798.                 hLibrary = NULL;
  799.             }
  800.         }
  801.         if (hLibrary != NULL)
  802.             break;
  803.         

  804.         // in the IPv6 Technology Preview...        
  805.         // the routines are present in the IPv6 WinSock library (wship6.dll).
  806.         // printf("Looking in wship6 for getaddrinfo...\n");
  807.         _WSPIAPI_STRCPY_S(Path, _WSPIAPI_COUNTOF(Path), SystemDir);
  808.         _WSPIAPI_STRCAT_S(Path, _WSPIAPI_COUNTOF(Path), "\\wship6");
  809.         hLibrary = LoadLibraryA(Path);
  810.         if (hLibrary != NULL)
  811.         {
  812.             fScratch = GetProcAddress(hLibrary, "getaddrinfo");
  813.             if (fScratch == NULL)
  814.             {
  815.                 FreeLibrary(hLibrary);
  816.                 hLibrary = NULL;
  817.             }
  818.         }

  819.         break;
  820.     }


  821.     if (hLibrary != NULL)
  822.     {
  823.         // use routines from this library...
  824.         // since getaddrinfo is here, we expect all routines to be here,
  825.         // but will fall back to IPv4-only if any of them is missing.
  826.         for (i = 0; i < iNumGlobal; i++)
  827.         {
  828.             rgtLocal[i].pfAddress
  829.                 = GetProcAddress(hLibrary, rgtLocal[i].pszName);
  830.             if (rgtLocal[i].pfAddress == NULL)
  831.             {
  832.                 FreeLibrary(hLibrary);
  833.                 hLibrary = NULL;
  834.                 break;
  835.             }
  836.         }

  837.         if (hLibrary != NULL)
  838.         {
  839.             // printf("found!\n");
  840.             for (i = 0; i < iNumGlobal; i++)
  841.                 rgtGlobal[i].pfAddress = rgtLocal[i].pfAddress;
  842.         }
  843.     }
  844.    
  845.     bInitialized = TRUE;
  846.     return (rgtGlobal[wFunction].pfAddress);
  847. }



  848. __inline
  849. int
  850. WINAPI
  851. WspiapiGetAddrInfo(
  852.     IN const char                       *nodename,
  853.     IN const char                       *servname,
  854.     IN const struct addrinfo            *hints,
  855.     OUT struct addrinfo                 **res)
  856. {
  857.     int                             iError;
  858.     static WSPIAPI_PGETADDRINFO     pfGetAddrInfo   = NULL;
  859.    
  860.     if (!pfGetAddrInfo)
  861.         pfGetAddrInfo   = (WSPIAPI_PGETADDRINFO) WspiapiLoad(0);

  862.     iError = (*pfGetAddrInfo)(nodename, servname, hints, res);
  863.     WSASetLastError(iError);
  864.     return iError;
  865. }



  866. __inline
  867. int
  868. WINAPI
  869. WspiapiGetNameInfo (
  870.     IN  const struct sockaddr           *sa,
  871.     IN  socklen_t                       salen,
  872.     OUT char                            *host,
  873.     IN  size_t                          hostlen,
  874.     OUT char                            *serv,
  875.     IN  size_t                          servlen,
  876.     IN  int                             flags)
  877. {
  878.     int                             iError;
  879.     static WSPIAPI_PGETNAMEINFO     pfGetNameInfo   = NULL;
  880.    
  881.     if (!pfGetNameInfo)
  882.         pfGetNameInfo   = (WSPIAPI_PGETNAMEINFO) WspiapiLoad(1);

  883.     iError = (*pfGetNameInfo)(sa, salen, host, hostlen, serv, servlen, flags);
  884.     WSASetLastError(iError);
  885.     return iError;
  886. }



  887. __inline
  888. void
  889. WINAPI
  890. WspiapiFreeAddrInfo (
  891.     IN  struct addrinfo                 *ai)
  892. {
  893.     static WSPIAPI_PFREEADDRINFO    pfFreeAddrInfo   = NULL;

  894.     if (!pfFreeAddrInfo)
  895.         pfFreeAddrInfo  = (WSPIAPI_PFREEADDRINFO) WspiapiLoad(2);
  896.     (*pfFreeAddrInfo)(ai);
  897. }

  898. #ifdef  __cplusplus
  899. }
  900. #endif

  901. #endif // _WSPIAPI_H_
复制代码





欢迎光临 小七技术论坛,计算机安全和杀毒软件,编程交流于软件安全技术交流,给新手一个绿色安全的论坛 (https://xqmsyk.com/) Powered by Discuz! X3.3