PageRenderTime 52ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/GeniePlugin_MapV2/TestUdt/wspiapi.h

https://github.com/schopenkitto/NetWorkMapPlugin
C Header | 1062 lines | 632 code | 158 blank | 272 comment | 142 complexity | e91489b8dffb3004226dc5745dcb7f4e MD5 | raw file
  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_H_
  12. #pragma once
  13. #if (NTDDI_VERSION >= NTDDI_WIN2K)
  14. #include <stdio.h> // sprintf()
  15. #include <stdlib.h> // calloc(), strtoul()
  16. #include <malloc.h> // calloc()
  17. #include <string.h> // strlen(), strcmp(), strstr()
  18. #if defined(__GOT_SECURE_LIB__) && __GOT_SECURE_LIB__ >= 200402L
  19. #define _WSPIAPI_STRCPY_S strcpy_s
  20. #define _WSPIAPI_STRCAT_S strcat_s
  21. #define _WSPIAPI_STRNCPY_S strncpy_s
  22. #define _WSPIAPI_SPRINTF_S_1 sprintf_s
  23. #else
  24. #define _WSPIAPI_STRCPY_S(_Dst, _Size, _Src) strcpy((_Dst), (_Src))
  25. #define _WSPIAPI_STRCAT_S(_Dst, _Size, _Src) strcat((_Dst), (_Src))
  26. #define _WSPIAPI_STRNCPY_S(_Dst, _Size, _Src, _Count) strncpy((_Dst), (_Src), (_Count)); (_Dst)[(_Size) - 1] = 0
  27. #define _WSPIAPI_SPRINTF_S_1(_Dst, _Size, _Format, _Arg1) sprintf((_Dst), (_Format), (_Arg1))
  28. #endif // defined(__GOT_SECURE_LIB__) && __GOT_SECURE_LIB__ >= 200402L
  29. #if !defined(_WSPIAPI_COUNTOF)
  30. #if !defined(__cplusplus)
  31. #define _WSPIAPI_COUNTOF(_Array) (sizeof(_Array) / sizeof(_Array[0]))
  32. #else
  33. template <typename __CountofType, size_t _N>
  34. char (&__wspiapi_countof_helper(__CountofType (&_Array)[_N]))[_N];
  35. #define _WSPIAPI_COUNTOF(_Array) sizeof(__wspiapi_countof_helper(_Array))
  36. #endif
  37. #endif
  38. #define WspiapiMalloc(tSize) calloc(1, (tSize))
  39. #define WspiapiFree(p) free(p)
  40. #define WspiapiSwap(a, b, c) { (c) = (a); (a) = (b); (b) = (c); }
  41. #define getaddrinfo WspiapiGetAddrInfo
  42. #define getnameinfo WspiapiGetNameInfo
  43. #define freeaddrinfo WspiapiFreeAddrInfo
  44. //
  45. // These function pointers are also within the #if (NTDDI_VERSION >= WIN2K)
  46. // because they are used by the other functions defined in this file available
  47. // only on win2k and above.
  48. //
  49. typedef int (WINAPI *WSPIAPI_PGETADDRINFO) (
  50. IN const char *nodename,
  51. IN const char *servname,
  52. IN const struct addrinfo *hints,
  53. OUT struct addrinfo **res);
  54. typedef int (WINAPI *WSPIAPI_PGETNAMEINFO) (
  55. IN const struct sockaddr *sa,
  56. IN socklen_t salen,
  57. OUT char *host,
  58. IN size_t hostlen,
  59. OUT char *serv,
  60. IN size_t servlen,
  61. IN int flags);
  62. typedef void (WINAPI *WSPIAPI_PFREEADDRINFO) (
  63. IN struct addrinfo *ai);
  64. #ifdef __cplusplus
  65. extern "C" {
  66. #endif
  67. #define _inline inline
  68. ////////////////////////////////////////////////////////////
  69. // v4 only versions of getaddrinfo and friends.
  70. // NOTE: gai_strerror is inlined in ws2tcpip.h
  71. ////////////////////////////////////////////////////////////
  72. _inline
  73. char *
  74. WINAPI
  75. WspiapiStrdup (
  76. IN const char * pszString)
  77. /*++
  78. Routine Description
  79. allocates enough storage via calloc() for a copy of the string,
  80. copies the string into the new memory, and returns a pointer to it.
  81. Arguments
  82. pszString string to copy into new memory
  83. Return Value
  84. a pointer to the newly allocated storage with the string in it.
  85. NULL if enough memory could not be allocated, or string was NULL.
  86. --*/
  87. {
  88. char *pszMemory;
  89. size_t cchMemory;
  90. if (!pszString)
  91. return(NULL);
  92. cchMemory = strlen(pszString) + 1;
  93. pszMemory = (char *) WspiapiMalloc(cchMemory);
  94. if (!pszMemory)
  95. return(NULL);
  96. _WSPIAPI_STRCPY_S(pszMemory, cchMemory, pszString);
  97. return pszMemory;
  98. }
  99. __inline
  100. BOOL
  101. WINAPI
  102. WspiapiParseV4Address (
  103. IN const char * pszAddress,
  104. OUT PDWORD pdwAddress)
  105. /*++
  106. Routine Description
  107. get the IPv4 address (in network byte order) from its string
  108. representation. the syntax should be a.b.c.d.
  109. Arguments
  110. pszArgument string representation of the IPv4 address
  111. ptAddress pointer to the resulting IPv4 address
  112. Return Value
  113. Returns FALSE if there is an error, TRUE for success.
  114. --*/
  115. {
  116. DWORD dwAddress = 0;
  117. const char *pcNext = NULL;
  118. int iCount = 0;
  119. // ensure there are 3 '.' (periods)
  120. for (pcNext = pszAddress; *pcNext != '\0'; pcNext++)
  121. if (*pcNext == '.')
  122. iCount++;
  123. if (iCount != 3)
  124. return FALSE;
  125. // return an error if dwAddress is INADDR_NONE (255.255.255.255)
  126. // since this is never a valid argument to getaddrinfo.
  127. dwAddress = inet_addr(pszAddress);
  128. if (dwAddress == INADDR_NONE)
  129. return FALSE;
  130. *pdwAddress = dwAddress;
  131. return TRUE;
  132. }
  133. __inline
  134. struct addrinfo *
  135. WINAPI
  136. WspiapiNewAddrInfo (
  137. IN int iSocketType,
  138. IN int iProtocol,
  139. IN WORD wPort,
  140. IN DWORD dwAddress)
  141. /*++
  142. Routine Description
  143. allocate an addrinfo structure and populate fields.
  144. IPv4 specific internal function, not exported.
  145. Arguments
  146. iSocketType SOCK_*. can be wildcarded (zero).
  147. iProtocol IPPROTO_*. can be wildcarded (zero).
  148. wPort port number of service (in network order).
  149. dwAddress IPv4 address (in network order).
  150. Return Value
  151. returns an addrinfo struct, or NULL if out of memory.
  152. --*/
  153. {
  154. struct addrinfo *ptNew;
  155. struct sockaddr_in *ptAddress;
  156. // allocate a new addrinfo structure.
  157. ptNew =
  158. (struct addrinfo *) WspiapiMalloc(sizeof(struct addrinfo));
  159. if (!ptNew)
  160. return NULL;
  161. ptAddress =
  162. (struct sockaddr_in *) WspiapiMalloc(sizeof(struct sockaddr_in));
  163. if (!ptAddress)
  164. {
  165. WspiapiFree(ptNew);
  166. return NULL;
  167. }
  168. ptAddress->sin_family = AF_INET;
  169. ptAddress->sin_port = wPort;
  170. ptAddress->sin_addr.s_addr = dwAddress;
  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. Arguments
  195. pszNodeName name of node to resolve.
  196. iSocketType SOCK_*. can be wildcarded (zero).
  197. iProtocol IPPROTO_*. can be wildcarded (zero).
  198. wPort port number of service (in network order).
  199. pszAlias where to return the alias. must be of size NI_MAXHOST.
  200. pptResult where to return the result.
  201. Return Value
  202. Returns 0 on success, an EAI_* style error value otherwise.
  203. --*/
  204. {
  205. struct addrinfo **pptNext = pptResult;
  206. struct hostent *ptHost = NULL;
  207. char **ppAddresses;
  208. *pptNext = NULL;
  209. pszAlias[0] = '\0';
  210. ptHost = gethostbyname(pszNodeName);
  211. if (ptHost)
  212. {
  213. if ((ptHost->h_addrtype == AF_INET) &&
  214. (ptHost->h_length == sizeof(struct in_addr)))
  215. {
  216. for (ppAddresses = ptHost->h_addr_list;
  217. *ppAddresses != NULL;
  218. ppAddresses++)
  219. {
  220. // create an addrinfo structure...
  221. *pptNext = WspiapiNewAddrInfo(
  222. iSocketType,
  223. iProtocol,
  224. wPort,
  225. ((struct in_addr *) *ppAddresses)->s_addr);
  226. if (!*pptNext)
  227. return EAI_MEMORY;
  228. pptNext = &((*pptNext)->ai_next);
  229. }
  230. }
  231. // pick up the canonical name.
  232. _WSPIAPI_STRNCPY_S(pszAlias, NI_MAXHOST, ptHost->h_name, NI_MAXHOST - 1);
  233. return 0;
  234. }
  235. switch (WSAGetLastError())
  236. {
  237. case WSAHOST_NOT_FOUND: return EAI_NONAME;
  238. case WSATRY_AGAIN: return EAI_AGAIN;
  239. case WSANO_RECOVERY: return EAI_FAIL;
  240. case WSANO_DATA: return EAI_NODATA;
  241. default: return EAI_NONAME;
  242. }
  243. }
  244. __inline
  245. int
  246. WINAPI
  247. WspiapiLookupNode(
  248. IN const char *pszNodeName,
  249. IN int iSocketType,
  250. IN int iProtocol,
  251. IN WORD wPort,
  252. IN BOOL bAI_CANONNAME,
  253. OUT struct addrinfo **pptResult)
  254. /*++
  255. Routine Description
  256. resolve a nodename and return a list of addrinfo structures.
  257. IPv4 specific internal function, not exported.
  258. *pptResult would need to be freed if an error is returned.
  259. NOTE: if bAI_CANONNAME is true, the canonical name should be
  260. returned in the first addrinfo structure.
  261. Arguments
  262. pszNodeName name of node to resolve.
  263. iSocketType SOCK_*. can be wildcarded (zero).
  264. iProtocol IPPROTO_*. can be wildcarded (zero).
  265. wPort port number of service (in network order).
  266. bAI_CANONNAME whether the AI_CANONNAME flag is set.
  267. pptResult where to return result.
  268. Return Value
  269. Returns 0 on success, an EAI_* style error value otherwise.
  270. --*/
  271. {
  272. int iError = 0;
  273. int iAliasCount = 0;
  274. char szFQDN1[NI_MAXHOST] = "";
  275. char szFQDN2[NI_MAXHOST] = "";
  276. char *pszName = szFQDN1;
  277. char *pszAlias = szFQDN2;
  278. char *pszScratch = NULL;
  279. _WSPIAPI_STRNCPY_S(pszName, NI_MAXHOST, pszNodeName, NI_MAXHOST - 1);
  280. for (;;)
  281. {
  282. iError = WspiapiQueryDNS(pszNodeName,
  283. iSocketType,
  284. iProtocol,
  285. wPort,
  286. pszAlias,
  287. pptResult);
  288. if (iError)
  289. break;
  290. // if we found addresses, then we are done.
  291. if (*pptResult)
  292. break;
  293. // stop infinite loops due to DNS misconfiguration. there appears
  294. // to be no particular recommended limit in RFCs 1034 and 1035.
  295. if ((!strlen(pszAlias)) ||
  296. (!strcmp(pszName, pszAlias)) ||
  297. (++iAliasCount == 16))
  298. {
  299. iError = EAI_FAIL;
  300. break;
  301. }
  302. // there was a new CNAME, look again.
  303. WspiapiSwap(pszName, pszAlias, pszScratch);
  304. }
  305. if (!iError && bAI_CANONNAME)
  306. {
  307. (*pptResult)->ai_canonname = WspiapiStrdup(pszAlias);
  308. if (!(*pptResult)->ai_canonname)
  309. iError = EAI_MEMORY;
  310. }
  311. return iError;
  312. }
  313. __inline
  314. int
  315. WINAPI
  316. WspiapiClone (
  317. IN WORD wPort,
  318. IN struct addrinfo *ptResult)
  319. /*++
  320. Routine Description
  321. clone every addrinfo structure in ptResult for the UDP service.
  322. ptResult would need to be freed if an error is returned.
  323. Arguments
  324. wPort port number of UDP service.
  325. ptResult list of addrinfo structures, each
  326. of whose node needs to be cloned.
  327. Return Value
  328. Returns 0 on success, an EAI_MEMORY on allocation failure.
  329. --*/
  330. {
  331. struct addrinfo *ptNext = NULL;
  332. struct addrinfo *ptNew = NULL;
  333. for (ptNext = ptResult; ptNext != NULL; )
  334. {
  335. // create an addrinfo structure...
  336. ptNew = WspiapiNewAddrInfo(
  337. SOCK_DGRAM,
  338. ptNext->ai_protocol,
  339. wPort,
  340. ((struct sockaddr_in *) ptNext->ai_addr)->sin_addr.s_addr);
  341. if (!ptNew)
  342. break;
  343. // link the cloned addrinfo
  344. ptNew->ai_next = ptNext->ai_next;
  345. ptNext->ai_next = ptNew;
  346. ptNext = ptNew->ai_next;
  347. }
  348. if (ptNext != NULL)
  349. return EAI_MEMORY;
  350. return 0;
  351. }
  352. __inline
  353. void
  354. WINAPI
  355. WspiapiLegacyFreeAddrInfo (
  356. IN struct addrinfo *ptHead)
  357. /*++
  358. Routine Description
  359. Free an addrinfo structure (or chain of structures).
  360. As specified in RFC 2553, Section 6.4.
  361. Arguments
  362. ptHead structure (chain) to free
  363. --*/
  364. {
  365. struct addrinfo *ptNext; // next strcture to free
  366. for (ptNext = ptHead; ptNext != NULL; ptNext = ptHead)
  367. {
  368. if (ptNext->ai_canonname)
  369. WspiapiFree(ptNext->ai_canonname);
  370. if (ptNext->ai_addr)
  371. WspiapiFree(ptNext->ai_addr);
  372. ptHead = ptNext->ai_next;
  373. WspiapiFree(ptNext);
  374. }
  375. }
  376. __inline
  377. int
  378. WINAPI
  379. WspiapiLegacyGetAddrInfo(
  380. IN const char *pszNodeName,
  381. IN const char *pszServiceName,
  382. IN const struct addrinfo *ptHints,
  383. OUT struct addrinfo **pptResult)
  384. /*++
  385. Routine Description
  386. Protocol-independent name-to-address translation.
  387. As specified in RFC 2553, Section 6.4.
  388. This is the hacked version that only supports IPv4.
  389. Arguments
  390. pszNodeName node name to lookup.
  391. pszServiceName service name to lookup.
  392. ptHints hints about how to process request.
  393. pptResult where to return result.
  394. Return Value
  395. returns zero if successful, an EAI_* error code if not.
  396. --*/
  397. {
  398. int iError = 0;
  399. int iFlags = 0;
  400. int iFamily = PF_UNSPEC;
  401. int iSocketType = 0;
  402. int iProtocol = 0;
  403. WORD wPort = 0;
  404. DWORD dwAddress = 0;
  405. struct servent *ptService = NULL;
  406. char *pc = NULL;
  407. BOOL bClone = FALSE;
  408. WORD wTcpPort = 0;
  409. WORD wUdpPort = 0;
  410. // initialize pptResult with default return value.
  411. *pptResult = NULL;
  412. ////////////////////////////////////////
  413. // validate arguments...
  414. //
  415. // both the node name and the service name can't be NULL.
  416. if ((!pszNodeName) && (!pszServiceName))
  417. return EAI_NONAME;
  418. // validate hints.
  419. if (ptHints)
  420. {
  421. // all members other than ai_flags, ai_family, ai_socktype
  422. // and ai_protocol must be zero or a null pointer.
  423. if ((ptHints->ai_addrlen != 0) ||
  424. (ptHints->ai_canonname != NULL) ||
  425. (ptHints->ai_addr != NULL) ||
  426. (ptHints->ai_next != NULL))
  427. {
  428. return EAI_FAIL;
  429. }
  430. // the spec has the "bad flags" error code, so presumably we
  431. // should check something here. insisting that there aren't
  432. // any unspecified flags set would break forward compatibility,
  433. // however. so we just check for non-sensical combinations.
  434. //
  435. // we cannot come up with a canonical name given a null node name.
  436. iFlags = ptHints->ai_flags;
  437. if ((iFlags & AI_CANONNAME) && !pszNodeName)
  438. return EAI_BADFLAGS;
  439. // we only support a limited number of protocol families.
  440. iFamily = ptHints->ai_family;
  441. if ((iFamily != PF_UNSPEC) && (iFamily != PF_INET))
  442. return EAI_FAMILY;
  443. // we only support only these socket types.
  444. iSocketType = ptHints->ai_socktype;
  445. if ((iSocketType != 0) &&
  446. (iSocketType != SOCK_STREAM) &&
  447. (iSocketType != SOCK_DGRAM) &&
  448. (iSocketType != SOCK_RAW))
  449. return EAI_SOCKTYPE;
  450. // REVIEW: What if ai_socktype and ai_protocol are at odds?
  451. iProtocol = ptHints->ai_protocol;
  452. }
  453. ////////////////////////////////////////
  454. // do service lookup...
  455. if (pszServiceName)
  456. {
  457. wPort = (WORD) strtoul(pszServiceName, &pc, 10);
  458. if (*pc == '\0') // numeric port string
  459. {
  460. wPort = wTcpPort = wUdpPort = htons(wPort);
  461. if (iSocketType == 0)
  462. {
  463. bClone = TRUE;
  464. iSocketType = SOCK_STREAM;
  465. }
  466. }
  467. else // non numeric port string
  468. {
  469. if ((iSocketType == 0) || (iSocketType == SOCK_DGRAM))
  470. {
  471. ptService = getservbyname(pszServiceName, "udp");
  472. if (ptService)
  473. wPort = wUdpPort = ptService->s_port;
  474. }
  475. if ((iSocketType == 0) || (iSocketType == SOCK_STREAM))
  476. {
  477. ptService = getservbyname(pszServiceName, "tcp");
  478. if (ptService)
  479. wPort = wTcpPort = ptService->s_port;
  480. }
  481. // assumes 0 is an invalid service port...
  482. if (wPort == 0) // no service exists
  483. return (iSocketType ? EAI_SERVICE : EAI_NONAME);
  484. if (iSocketType == 0)
  485. {
  486. // if both tcp and udp, process tcp now & clone udp later.
  487. iSocketType = (wTcpPort) ? SOCK_STREAM : SOCK_DGRAM;
  488. bClone = (wTcpPort && wUdpPort);
  489. }
  490. }
  491. }
  492. ////////////////////////////////////////
  493. // do node name lookup...
  494. // if we weren't given a node name,
  495. // return the wildcard or loopback address (depending on AI_PASSIVE).
  496. //
  497. // if we have a numeric host address string,
  498. // return the binary address.
  499. //
  500. if ((!pszNodeName) || (WspiapiParseV4Address(pszNodeName, &dwAddress)))
  501. {
  502. if (!pszNodeName)
  503. {
  504. dwAddress = htonl((iFlags & AI_PASSIVE)
  505. ? INADDR_ANY
  506. : INADDR_LOOPBACK);
  507. }
  508. // create an addrinfo structure...
  509. *pptResult =
  510. WspiapiNewAddrInfo(iSocketType, iProtocol, wPort, dwAddress);
  511. if (!(*pptResult))
  512. iError = EAI_MEMORY;
  513. if (!iError && pszNodeName)
  514. {
  515. // implementation specific behavior: set AI_NUMERICHOST
  516. // to indicate that we got a numeric host address string.
  517. (*pptResult)->ai_flags |= AI_NUMERICHOST;
  518. // return the numeric address string as the canonical name
  519. if (iFlags & AI_CANONNAME)
  520. {
  521. (*pptResult)->ai_canonname =
  522. WspiapiStrdup(inet_ntoa(*((struct in_addr *) &dwAddress)));
  523. if (!(*pptResult)->ai_canonname)
  524. iError = EAI_MEMORY;
  525. }
  526. }
  527. }
  528. // if we do not have a numeric host address string and
  529. // AI_NUMERICHOST flag is set, return an error!
  530. else if (iFlags & AI_NUMERICHOST)
  531. {
  532. iError = EAI_NONAME;
  533. }
  534. // since we have a non-numeric node name,
  535. // we have to do a regular node name lookup.
  536. else
  537. {
  538. iError = WspiapiLookupNode(pszNodeName,
  539. iSocketType,
  540. iProtocol,
  541. wPort,
  542. (iFlags & AI_CANONNAME),
  543. pptResult);
  544. }
  545. if (!iError && bClone)
  546. {
  547. iError = WspiapiClone(wUdpPort, *pptResult);
  548. }
  549. if (iError)
  550. {
  551. WspiapiLegacyFreeAddrInfo(*pptResult);
  552. *pptResult = NULL;
  553. }
  554. return (iError);
  555. }
  556. __inline
  557. int
  558. WINAPI
  559. WspiapiLegacyGetNameInfo(
  560. IN const struct sockaddr *ptSocketAddress,
  561. IN socklen_t tSocketLength,
  562. OUT char *pszNodeName,
  563. IN size_t tNodeLength,
  564. OUT char *pszServiceName,
  565. IN size_t tServiceLength,
  566. IN int iFlags)
  567. /*++
  568. Routine Description
  569. protocol-independent address-to-name translation.
  570. as specified in RFC 2553, Section 6.5.
  571. this is the hacked version that only supports IPv4.
  572. Arguments
  573. ptSocketAddress socket address to translate.
  574. tSocketLength length of above socket address.
  575. pszNodeName where to return the node name.
  576. tNodeLength size of above buffer.
  577. pszServiceName where to return the service name.
  578. tServiceLength size of above buffer.
  579. iFlags flags of type NI_*.
  580. Return Value
  581. returns zero if successful, an EAI_* error code if not.
  582. --*/
  583. {
  584. struct servent *ptService;
  585. WORD wPort;
  586. char szBuffer[] = "65535";
  587. char *pszService = szBuffer;
  588. struct hostent *ptHost;
  589. struct in_addr tAddress;
  590. char *pszNode = NULL;
  591. char *pc = NULL;
  592. // sanity check ptSocketAddress and tSocketLength.
  593. if ((!ptSocketAddress) || (tSocketLength < sizeof(struct sockaddr)))
  594. return EAI_FAIL;
  595. if (ptSocketAddress->sa_family != AF_INET)
  596. return EAI_FAMILY;
  597. if (tSocketLength < sizeof(struct sockaddr_in))
  598. return EAI_FAIL;
  599. if (!(pszNodeName && tNodeLength) &&
  600. !(pszServiceName && tServiceLength))
  601. {
  602. return EAI_NONAME;
  603. }
  604. // the draft has the "bad flags" error code, so presumably we
  605. // should check something here. insisting that there aren't
  606. // any unspecified flags set would break forward compatibility,
  607. // however. so we just check for non-sensical combinations.
  608. if ((iFlags & NI_NUMERICHOST) && (iFlags & NI_NAMEREQD))
  609. {
  610. return EAI_BADFLAGS;
  611. }
  612. // translate the port to a service name (if requested).
  613. if (pszServiceName && tServiceLength)
  614. {
  615. wPort = ((struct sockaddr_in *) ptSocketAddress)->sin_port;
  616. if (iFlags & NI_NUMERICSERV)
  617. {
  618. // return numeric form of the address.
  619. _WSPIAPI_SPRINTF_S_1(szBuffer, _WSPIAPI_COUNTOF(szBuffer), "%u", ntohs(wPort));
  620. }
  621. else
  622. {
  623. // return service name corresponding to port.
  624. ptService = getservbyport(wPort,
  625. (iFlags & NI_DGRAM) ? "udp" : NULL);
  626. if (ptService && ptService->s_name)
  627. {
  628. // lookup successful.
  629. pszService = ptService->s_name;
  630. }
  631. else
  632. {
  633. // DRAFT: return numeric form of the port!
  634. _WSPIAPI_SPRINTF_S_1(szBuffer, _WSPIAPI_COUNTOF(szBuffer), "%u", ntohs(wPort));
  635. }
  636. }
  637. if (tServiceLength > strlen(pszService))
  638. _WSPIAPI_STRCPY_S(pszServiceName, tServiceLength, pszService);
  639. else
  640. return EAI_FAIL;
  641. }
  642. // translate the address to a node name (if requested).
  643. if (pszNodeName && tNodeLength)
  644. {
  645. // this is the IPv4-only version, so we have an IPv4 address.
  646. tAddress = ((struct sockaddr_in *) ptSocketAddress)->sin_addr;
  647. if (iFlags & NI_NUMERICHOST)
  648. {
  649. // return numeric form of the address.
  650. pszNode = inet_ntoa(tAddress);
  651. }
  652. else
  653. {
  654. // return node name corresponding to address.
  655. ptHost = gethostbyaddr((char *) &tAddress,
  656. sizeof(struct in_addr),
  657. AF_INET);
  658. if (ptHost && ptHost->h_name)
  659. {
  660. // DNS lookup successful.
  661. // stop copying at a "." if NI_NOFQDN is specified.
  662. pszNode = ptHost->h_name;
  663. if ((iFlags & NI_NOFQDN) &&
  664. ((pc = strchr(pszNode, '.')) != NULL))
  665. *pc = '\0';
  666. }
  667. else
  668. {
  669. // DNS lookup failed. return numeric form of the address.
  670. if (iFlags & NI_NAMEREQD)
  671. {
  672. switch (WSAGetLastError())
  673. {
  674. case WSAHOST_NOT_FOUND: return EAI_NONAME;
  675. case WSATRY_AGAIN: return EAI_AGAIN;
  676. case WSANO_RECOVERY: return EAI_FAIL;
  677. default: return EAI_NONAME;
  678. }
  679. }
  680. else
  681. pszNode = inet_ntoa(tAddress);
  682. }
  683. }
  684. if (tNodeLength > strlen(pszNode))
  685. _WSPIAPI_STRCPY_S(pszNodeName, tNodeLength, pszNode);
  686. else
  687. return EAI_FAIL;
  688. }
  689. return 0;
  690. }
  691. typedef struct
  692. {
  693. char const *pszName;
  694. FARPROC pfAddress;
  695. } WSPIAPI_FUNCTION;
  696. #define WSPIAPI_FUNCTION_ARRAY \
  697. { \
  698. "getaddrinfo", (FARPROC) WspiapiLegacyGetAddrInfo, \
  699. "getnameinfo", (FARPROC) WspiapiLegacyGetNameInfo, \
  700. "freeaddrinfo", (FARPROC) WspiapiLegacyFreeAddrInfo, \
  701. }
  702. __inline
  703. FARPROC
  704. WINAPI
  705. WspiapiLoad(
  706. IN WORD wFunction)
  707. /*++
  708. Routine Description
  709. try to locate the address family independent name resolution routines
  710. (i.e. getaddrinfo, getnameinfo, freeaddrinfo, gai_strerror).
  711. Locks
  712. this function call is not synchronized. hence the library containing
  713. the routines might be loaded multiple times. another option is to
  714. synchronize through a spin lock using a static local variable and the
  715. InterlockedExchange operation.
  716. Arguments
  717. wFunction ordinal # of the function to get the pointer to
  718. 0 getaddrinfo
  719. 1 getnameinfo
  720. 2 freeaddrinfo
  721. Return Value
  722. address of the library/legacy routine
  723. --*/
  724. {
  725. HMODULE hLibrary = NULL;
  726. // these static variables store state across calls, across threads.
  727. static BOOL bInitialized = FALSE;
  728. static WSPIAPI_FUNCTION rgtGlobal[] = WSPIAPI_FUNCTION_ARRAY;
  729. static const int iNumGlobal = (sizeof(rgtGlobal) /
  730. sizeof(WSPIAPI_FUNCTION));
  731. // we overwrite rgtGlobal only if all routines exist in library.
  732. WSPIAPI_FUNCTION rgtLocal[] = WSPIAPI_FUNCTION_ARRAY;
  733. FARPROC fScratch = NULL;
  734. int i = 0;
  735. if (bInitialized) // WspiapiLoad has already been called once
  736. return (rgtGlobal[wFunction].pfAddress);
  737. for (;;) // breakout loop
  738. {
  739. CHAR SystemDir[MAX_PATH + 1];
  740. CHAR Path[MAX_PATH + 8];
  741. if (GetSystemDirectoryA(SystemDir, MAX_PATH) == 0)
  742. {
  743. break;
  744. }
  745. // in Whistler and beyond...
  746. // the routines are present in the WinSock 2 library (ws2_32.dll).
  747. // printf("Looking in ws2_32 for getaddrinfo...\n");
  748. _WSPIAPI_STRCPY_S(Path, _WSPIAPI_COUNTOF(Path), SystemDir);
  749. _WSPIAPI_STRCAT_S(Path, _WSPIAPI_COUNTOF(Path), "\\ws2_32");
  750. hLibrary = LoadLibraryA(Path);
  751. if (hLibrary != NULL)
  752. {
  753. fScratch = GetProcAddress(hLibrary, "getaddrinfo");
  754. if (fScratch == NULL)
  755. {
  756. FreeLibrary(hLibrary);
  757. hLibrary = NULL;
  758. }
  759. }
  760. if (hLibrary != NULL)
  761. break;
  762. // in the IPv6 Technology Preview...
  763. // the routines are present in the IPv6 WinSock library (wship6.dll).
  764. // printf("Looking in wship6 for getaddrinfo...\n");
  765. _WSPIAPI_STRCPY_S(Path, _WSPIAPI_COUNTOF(Path), SystemDir);
  766. _WSPIAPI_STRCAT_S(Path, _WSPIAPI_COUNTOF(Path), "\\wship6");
  767. hLibrary = LoadLibraryA(Path);
  768. if (hLibrary != NULL)
  769. {
  770. fScratch = GetProcAddress(hLibrary, "getaddrinfo");
  771. if (fScratch == NULL)
  772. {
  773. FreeLibrary(hLibrary);
  774. hLibrary = NULL;
  775. }
  776. }
  777. break;
  778. }
  779. if (hLibrary != NULL)
  780. {
  781. // use routines from this library...
  782. // since getaddrinfo is here, we expect all routines to be here,
  783. // but will fall back to IPv4-only if any of them is missing.
  784. for (i = 0; i < iNumGlobal; i++)
  785. {
  786. rgtLocal[i].pfAddress
  787. = GetProcAddress(hLibrary, rgtLocal[i].pszName);
  788. if (rgtLocal[i].pfAddress == NULL)
  789. {
  790. FreeLibrary(hLibrary);
  791. hLibrary = NULL;
  792. break;
  793. }
  794. }
  795. if (hLibrary != NULL)
  796. {
  797. // printf("found!\n");
  798. for (i = 0; i < iNumGlobal; i++)
  799. rgtGlobal[i].pfAddress = rgtLocal[i].pfAddress;
  800. }
  801. }
  802. bInitialized = TRUE;
  803. return (rgtGlobal[wFunction].pfAddress);
  804. }
  805. __inline
  806. int
  807. WINAPI
  808. WspiapiGetAddrInfo(
  809. IN const char *nodename,
  810. IN const char *servname,
  811. IN const struct addrinfo *hints,
  812. OUT struct addrinfo **res)
  813. {
  814. int iError;
  815. static WSPIAPI_PGETADDRINFO pfGetAddrInfo = NULL;
  816. if (!pfGetAddrInfo)
  817. pfGetAddrInfo = (WSPIAPI_PGETADDRINFO) WspiapiLoad(0);
  818. iError = (*pfGetAddrInfo)(nodename, servname, hints, res);
  819. WSASetLastError(iError);
  820. return iError;
  821. }
  822. __inline
  823. int
  824. WINAPI
  825. WspiapiGetNameInfo (
  826. IN const struct sockaddr *sa,
  827. IN socklen_t salen,
  828. OUT char *host,
  829. IN size_t hostlen,
  830. OUT char *serv,
  831. IN size_t servlen,
  832. IN int flags)
  833. {
  834. int iError;
  835. static WSPIAPI_PGETNAMEINFO pfGetNameInfo = NULL;
  836. if (!pfGetNameInfo)
  837. pfGetNameInfo = (WSPIAPI_PGETNAMEINFO) WspiapiLoad(1);
  838. iError = (*pfGetNameInfo)(sa, salen, host, hostlen, serv, servlen, flags);
  839. WSASetLastError(iError);
  840. return iError;
  841. }
  842. __inline
  843. void
  844. WINAPI
  845. WspiapiFreeAddrInfo (
  846. IN struct addrinfo *ai)
  847. {
  848. static WSPIAPI_PFREEADDRINFO pfFreeAddrInfo = NULL;
  849. if (!pfFreeAddrInfo)
  850. pfFreeAddrInfo = (WSPIAPI_PFREEADDRINFO) WspiapiLoad(2);
  851. (*pfFreeAddrInfo)(ai);
  852. }
  853. #ifdef __cplusplus
  854. }
  855. #endif
  856. #endif // if (NTDDI_VERSION >= WIN2K)
  857. #endif // _WSPIAPI_H_