aboutsummaryrefslogtreecommitdiff
path: root/lib/libc/mingw/libsrc/wspiapi/WspiapiQueryDNS.c
blob: 6e4bb08d6c611af17babc46a1030dd1698067a54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#undef  __CRT__NO_INLINE
#define __CRT__NO_INLINE
#include <winsock2.h>
#include <wspiapi.h>

int WINAPI
WspiapiQueryDNS(const char *pszNodeName,
		int iSocketType, int iProtocol,
		WORD wPort, char pszAlias[NI_MAXHOST],
		struct addrinfo **pptResult)
{
  struct addrinfo **paddrinfo = pptResult;
  struct hostent *phost = NULL;
  char **h;

  *paddrinfo = NULL;
  pszAlias[0] = 0;
  phost = gethostbyname (pszNodeName);
  if (phost)
    {
      if (phost->h_addrtype == AF_INET && phost->h_length == sizeof(struct in_addr))
	  {
	    for (h = phost->h_addr_list; *h != NULL; h++)
	      {
		*paddrinfo = WspiapiNewAddrInfo (iSocketType, iProtocol, wPort,
						 ((struct in_addr *) *h)->s_addr);
		if (!*paddrinfo)
		  return EAI_MEMORY;
		paddrinfo = &((*paddrinfo)->ai_next);
	      }
	  }
	strncpy (pszAlias, phost->h_name, NI_MAXHOST - 1);
	pszAlias[NI_MAXHOST - 1] = 0;
	return 0;
    }
  switch(WSAGetLastError())
    {
    case WSAHOST_NOT_FOUND: break;
    case WSATRY_AGAIN: return EAI_AGAIN;
    case WSANO_RECOVERY: return EAI_FAIL;
    case WSANO_DATA: return EAI_NODATA;
    default: break;
    }
  return EAI_NONAME;
}