| 1 | #ifndef WIN32_LEAN_AND_MEAN |
| 2 | #define WIN32_LEAN_AND_MEAN |
| 3 | #endif |
| 4 | #undef __CRT__NO_INLINE |
| 5 | #define __CRT__NO_INLINE |
| 6 | #include <winsock2.h> |
| 7 | #include <wspiapi.h> |
| 8 | |
| 9 | int WINAPI |
| 10 | WspiapiLegacyGetNameInfo (const struct sockaddr *ptSocketAddress, |
| 11 | 			 socklen_t tSocketLength, |
| 12 | 			 char *pszNodeName, size_t tNodeLength, |
| 13 | 			 char *pszServiceName, size_t tServiceLength, |
| 14 | 			 int iFlags) |
| 15 | { |
| 16 | struct servent *svc; |
| 17 | WORD port; |
| 18 | char str[] = "65535"; |
| 19 | char *pstr = str; |
| 20 | struct hostent *phost; |
| 21 | struct in_addr l_inaddr; |
| 22 | char *pnode = NULL, *pc = NULL; |
| 23 | |
| 24 | if (!ptSocketAddress || tSocketLength < (int) sizeof (struct sockaddr)) |
| 25 | return EAI_FAIL; |
| 26 | if (ptSocketAddress->sa_family != AF_INET) |
| 27 | return EAI_FAMILY; |
| 28 | if (tSocketLength < (int) sizeof (struct sockaddr_in)) |
| 29 | return EAI_FAIL; |
| 30 | if (!(pszNodeName && tNodeLength) && !(pszServiceName && tServiceLength)) |
| 31 | return EAI_NONAME; |
| 32 | if ((iFlags & NI_NUMERICHOST) != 0 && (iFlags & NI_NAMEREQD) != 0) |
| 33 | return EAI_BADFLAGS; |
| 34 | if (pszServiceName && tServiceLength) |
| 35 | { |
| 36 | 	port = ((struct sockaddr_in *) ptSocketAddress)->sin_port; |
| 37 | 	if (iFlags & NI_NUMERICSERV) |
| 38 | 	 sprintf (str, "%u", ntohs (port)); |
| 39 | else |
| 40 | 	 { |
| 41 | 	 svc = getservbyport(port, (iFlags & NI_DGRAM) ? "udp" : NULL); |
| 42 | 	 if (svc && svc->s_name) |
| 43 | 	 pstr = svc->s_name; |
| 44 | 	 else |
| 45 | 	 sprintf (str, "%u", ntohs (port)); |
| 46 | 	 } |
| 47 | 	if (tServiceLength > strlen (pstr)) |
| 48 | 	 strcpy (pszServiceName, pstr); |
| 49 | 	else |
| 50 | 	 return EAI_FAIL; |
| 51 | } |
| 52 | if (pszNodeName && tNodeLength) |
| 53 | { |
| 54 | 	l_inaddr = ((struct sockaddr_in *) ptSocketAddress)->sin_addr; |
| 55 | 	if (iFlags & NI_NUMERICHOST) |
| 56 | 	 pnode = inet_ntoa (l_inaddr); |
| 57 | 	else |
| 58 | 	 { |
| 59 | 	 phost = gethostbyaddr ((char *) &l_inaddr, sizeof (struct in_addr), AF_INET); |
| 60 | 	 if (phost && phost->h_name) |
| 61 | 	 { |
| 62 | 		pnode = phost->h_name; |
| 63 | 		if ((iFlags & NI_NOFQDN) != 0 && ((pc = strchr (pnode,'.')) != NULL)) |
| 64 | 		 *pc = 0; |
| 65 | 	 } |
| 66 | 	 else |
| 67 | 	 { |
| 68 | 		if ((iFlags & NI_NAMEREQD) != 0) |
| 69 | 		 { |
| 70 | 		 switch(WSAGetLastError()) |
| 71 | 		 { |
| 72 | 		 case WSAHOST_NOT_FOUND: return EAI_NONAME; |
| 73 | 		 case WSATRY_AGAIN: return EAI_AGAIN; |
| 74 | 		 case WSANO_RECOVERY: return EAI_FAIL; |
| 75 | 		 default: return EAI_NONAME; |
| 76 | 		 } |
| 77 | 		 } |
| 78 | 		else |
| 79 | 		 pnode = inet_ntoa (l_inaddr); |
| 80 | 	 } |
| 81 | 	 } |
| 82 | 	if (tNodeLength > strlen (pnode)) |
| 83 | 	 strcpy (pszNodeName, pnode); |
| 84 | 	else |
| 85 | 	 return EAI_FAIL; |
| 86 | } |
| 87 | return 0; |
| 88 | } |