Leaked source code of windows server 2003
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.

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