Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1011 lines
29 KiB

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