wspiapi.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /**
  2. * This file has no copyright assigned and is placed in the Public Domain.
  3. * This file is part of the mingw-w64 runtime package.
  4. * No warranty is given; refer to the file DISCLAIMER.PD within this package.
  5. */
  6. #ifndef _WSPIAPI_H_
  7. #define _WSPIAPI_H_
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <malloc.h>
  11. #include <string.h>
  12. #include <ws2tcpip.h>
  13. #define _WSPIAPI_STRCPY_S(_Dst,_Size,_Src) strcpy((_Dst),(_Src))
  14. #define _WSPIAPI_STRCAT_S(_Dst,_Size,_Src) strcat((_Dst),(_Src))
  15. #define _WSPIAPI_STRNCPY_S(_Dst,_Size,_Src,_Count) strncpy((_Dst),(_Src),(_Count)); (_Dst)[(_Size) - 1] = 0
  16. #define _WSPIAPI_SPRINTF_S_1(_Dst,_Size,_Format,_Arg1) sprintf((_Dst),(_Format),(_Arg1))
  17. #ifndef _WSPIAPI_COUNTOF
  18. #ifndef __cplusplus
  19. #define _WSPIAPI_COUNTOF(_Array) (sizeof(_Array) / sizeof(_Array[0]))
  20. #else
  21. template <typename __CountofType,size_t __wspiapi_countof_helper_N> char (&__wspiapi_countof_helper(__CountofType (&_Array)[__wspiapi_countof_helper_N]))[__wspiapi_countof_helper_N];
  22. #define _WSPIAPI_COUNTOF(_Array) sizeof(__wspiapi_countof_helper(_Array))
  23. #endif
  24. #endif
  25. #define WspiapiMalloc(tSize) calloc(1,(tSize))
  26. #define WspiapiFree(p) free(p)
  27. #define WspiapiSwap(a,b,c) { (c) = (a); (a) = (b); (b) = (c); }
  28. #define getaddrinfo WspiapiGetAddrInfo
  29. #define getnameinfo WspiapiGetNameInfo
  30. #define freeaddrinfo WspiapiFreeAddrInfo
  31. typedef int (WINAPI *WSPIAPI_PGETADDRINFO)(const char *nodename,const char *servname,const struct addrinfo *hints,struct addrinfo **res);
  32. typedef int (WINAPI *WSPIAPI_PGETNAMEINFO)(const struct sockaddr *sa,socklen_t salen,char *host,size_t hostlen,char *serv,size_t servlen,int flags);
  33. typedef void (WINAPI *WSPIAPI_PFREEADDRINFO)(struct addrinfo *ai);
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. typedef struct {
  38. char const *pszName;
  39. FARPROC pfAddress;
  40. } WSPIAPI_FUNCTION;
  41. #define WSPIAPI_FUNCTION_ARRAY { { "getaddrinfo",(FARPROC) WspiapiLegacyGetAddrInfo }, \
  42. { "getnameinfo",(FARPROC) WspiapiLegacyGetNameInfo }, \
  43. { "freeaddrinfo",(FARPROC) WspiapiLegacyFreeAddrInfo } }
  44. char *WINAPI WspiapiStrdup (const char *pszString);
  45. WINBOOL WINAPI WspiapiParseV4Address (const char *pszAddress,PDWORD pdwAddress);
  46. struct addrinfo * WINAPI WspiapiNewAddrInfo (int iSocketType,int iProtocol,WORD wPort,DWORD dwAddress);
  47. int WINAPI WspiapiQueryDNS (const char *pszNodeName,int iSocketType,int iProtocol,WORD wPort,char pszAlias[NI_MAXHOST],struct addrinfo **pptResult);
  48. int WINAPI WspiapiLookupNode (const char *pszNodeName,int iSocketType,int iProtocol,WORD wPort,WINBOOL bAI_CANONNAME,struct addrinfo **pptResult);
  49. int WINAPI WspiapiClone (WORD wPort,struct addrinfo *ptResult);
  50. void WINAPI WspiapiLegacyFreeAddrInfo (struct addrinfo *ptHead);
  51. int WINAPI WspiapiLegacyGetAddrInfo(const char *pszNodeName,const char *pszServiceName,const struct addrinfo *ptHints,struct addrinfo **pptResult);
  52. int WINAPI WspiapiLegacyGetNameInfo(const struct sockaddr *ptSocketAddress,socklen_t tSocketLength,char *pszNodeName,size_t tNodeLength,char *pszServiceName,size_t tServiceLength,int iFlags);
  53. FARPROC WINAPI WspiapiLoad(WORD wFunction);
  54. int WINAPI WspiapiGetAddrInfo(const char *nodename,const char *servname,const struct addrinfo *hints,struct addrinfo **res);
  55. int WINAPI WspiapiGetNameInfo (const struct sockaddr *sa,socklen_t salen,char *host,size_t hostlen,char *serv,size_t servlen,int flags);
  56. void WINAPI WspiapiFreeAddrInfo (struct addrinfo *ai);
  57. #ifndef __CRT__NO_INLINE
  58. __CRT_INLINE char * WINAPI
  59. WspiapiStrdup (const char *pszString)
  60. {
  61. char *rstr;
  62. size_t szlen;
  63. if(!pszString)
  64. return NULL;
  65. szlen = strlen(pszString) + 1;
  66. rstr = (char *) WspiapiMalloc (szlen);
  67. if (!rstr)
  68. return NULL;
  69. strcpy (rstr, pszString);
  70. return rstr;
  71. }
  72. __CRT_INLINE WINBOOL WINAPI
  73. WspiapiParseV4Address (const char *pszAddress, PDWORD pdwAddress)
  74. {
  75. DWORD dwAddress = 0;
  76. const char *h = NULL;
  77. int cnt;
  78. for (cnt = 0,h = pszAddress; *h != 0; h++)
  79. if (h[0] == '.')
  80. cnt++;
  81. if (cnt != 3)
  82. return FALSE;
  83. dwAddress = inet_addr (pszAddress);
  84. if (dwAddress == INADDR_NONE)
  85. return FALSE;
  86. *pdwAddress = dwAddress;
  87. return TRUE;
  88. }
  89. __CRT_INLINE struct addrinfo * WINAPI
  90. WspiapiNewAddrInfo (int iSocketType,int iProtocol, WORD wPort,DWORD dwAddress)
  91. {
  92. struct addrinfo *n;
  93. struct sockaddr_in *pa;
  94. if ((n = (struct addrinfo *) WspiapiMalloc (sizeof (struct addrinfo))) == NULL)
  95. return NULL;
  96. if ((pa = (struct sockaddr_in *) WspiapiMalloc (sizeof(struct sockaddr_in))) == NULL)
  97. {
  98. WspiapiFree(n);
  99. return NULL;
  100. }
  101. pa->sin_family = AF_INET;
  102. pa->sin_port = wPort;
  103. pa->sin_addr.s_addr = dwAddress;
  104. n->ai_family = PF_INET;
  105. n->ai_socktype = iSocketType;
  106. n->ai_protocol = iProtocol;
  107. n->ai_addrlen = sizeof (struct sockaddr_in);
  108. n->ai_addr = (struct sockaddr *) pa;
  109. return n;
  110. }
  111. __CRT_INLINE int WINAPI
  112. WspiapiLookupNode (const char *pszNodeName, int iSocketType, int iProtocol, WORD wPort,
  113. WINBOOL bAI_CANONNAME, struct addrinfo **pptResult)
  114. {
  115. int err = 0, cntAlias = 0;
  116. char name[NI_MAXHOST] = "";
  117. char alias[NI_MAXHOST] = "";
  118. char *pname = name, *palias = alias, *tmp = NULL;
  119. strncpy (pname, pszNodeName, NI_MAXHOST - 1);
  120. pname[NI_MAXHOST - 1] = 0;
  121. for (;;)
  122. {
  123. err = WspiapiQueryDNS (pszNodeName, iSocketType, iProtocol, wPort, palias, pptResult);
  124. if (err)
  125. break;
  126. if (*pptResult)
  127. break;
  128. ++cntAlias;
  129. if (strlen (palias) == 0 || !strcmp (pname, palias) || cntAlias == 16)
  130. {
  131. err = EAI_FAIL;
  132. break;
  133. }
  134. WspiapiSwap(pname, palias, tmp);
  135. }
  136. if (!err && bAI_CANONNAME)
  137. {
  138. (*pptResult)->ai_canonname = WspiapiStrdup (palias);
  139. if (!(*pptResult)->ai_canonname)
  140. err = EAI_MEMORY;
  141. }
  142. return err;
  143. }
  144. __CRT_INLINE int WINAPI
  145. WspiapiClone (WORD wPort,struct addrinfo *ptResult)
  146. {
  147. struct addrinfo *p = NULL;
  148. struct addrinfo *n = NULL;
  149. for (p = ptResult; p != NULL;)
  150. {
  151. n = WspiapiNewAddrInfo (SOCK_DGRAM, p->ai_protocol, wPort,
  152. ((struct sockaddr_in *) p->ai_addr)->sin_addr.s_addr);
  153. if (!n)
  154. break;
  155. n->ai_next = p->ai_next;
  156. p->ai_next = n;
  157. p = n->ai_next;
  158. }
  159. if (p != NULL)
  160. return EAI_MEMORY;
  161. return 0;
  162. }
  163. __CRT_INLINE void WINAPI
  164. WspiapiLegacyFreeAddrInfo (struct addrinfo *ptHead)
  165. {
  166. struct addrinfo *p;
  167. for (p = ptHead; p != NULL; p = ptHead)
  168. {
  169. if (p->ai_canonname)
  170. WspiapiFree (p->ai_canonname);
  171. if (p->ai_addr)
  172. WspiapiFree (p->ai_addr);
  173. ptHead = p->ai_next;
  174. WspiapiFree (p);
  175. }
  176. }
  177. #endif /* !__CRT__NO_INLINE */
  178. #ifdef __cplusplus
  179. }
  180. #endif
  181. #endif