| 1 | /** | 
 
 
 
 
 | 2 | * This file has no copyright assigned and is placed in the Public Domain. | 
 
 
 
 
 | 3 | * This file is part of the mingw-w64 runtime package. | 
 
 
 
 
 | 4 | * No warranty is given; refer to the file DISCLAIMER.PD within this package. | 
 
 
 
 
 | 5 | */ | 
 
 
 
 
 | 6 | #ifndef _WSPIAPI_H_ | 
 
 
 
 
 | 7 | #define _WSPIAPI_H_ | 
 
 
 
 
 | 8 |  | 
 
 
 
 
 | 9 | #include <stdio.h> | 
 
 
 
 
 | 10 | #include <stdlib.h> | 
 
 
 
 
 | 11 | #include <malloc.h> | 
 
 
 
 
 | 12 | #include <string.h> | 
 
 
 
 
 | 13 | #include <ws2tcpip.h> | 
 
 
 
 
 | 14 |  | 
 
 
 
 
 | 15 | #define _WSPIAPI_STRCPY_S(_Dst,_Size,_Src) strcpy((_Dst),(_Src)) | 
 
 
 
 
 | 16 | #define _WSPIAPI_STRCAT_S(_Dst,_Size,_Src) strcat((_Dst),(_Src)) | 
 
 
 
 
 | 17 | #define _WSPIAPI_STRNCPY_S(_Dst,_Size,_Src,_Count) strncpy((_Dst),(_Src),(_Count)); (_Dst)[(_Size) - 1] = 0 | 
 
 
 
 
 | 18 | #define _WSPIAPI_SPRINTF_S_1(_Dst,_Size,_Format,_Arg1) sprintf((_Dst),(_Format),(_Arg1)) | 
 
 
 
 
 | 19 |  | 
 
 
 
 
 | 20 | #ifndef _WSPIAPI_COUNTOF | 
 
 
 
 
 | 21 | #ifndef __cplusplus | 
 
 
 
 
 | 22 | #define _WSPIAPI_COUNTOF(_Array) (sizeof(_Array) / sizeof(_Array[0])) | 
 
 
 
 
 | 23 | #else | 
 
 
 
 
 | 24 | template <typename __CountofType,size_t __wspiapi_countof_helper_N> char (&__wspiapi_countof_helper(__CountofType (&_Array)[__wspiapi_countof_helper_N]))[__wspiapi_countof_helper_N]; | 
 
 
 
 
 | 25 | #define _WSPIAPI_COUNTOF(_Array) sizeof(__wspiapi_countof_helper(_Array)) | 
 
 
 
 
 | 26 | #endif | 
 
 
 
 
 | 27 | #endif | 
 
 
 
 
 | 28 |  | 
 
 
 
 
 | 29 | #define WspiapiMalloc(tSize) calloc(1,(tSize)) | 
 
 
 
 
 | 30 | #define WspiapiFree(p) free(p) | 
 
 
 
 
 | 31 | #define WspiapiSwap(a,b,c) { (c) = (a); (a) = (b); (b) = (c); } | 
 
 
 
 
 | 32 | #define getaddrinfo WspiapiGetAddrInfo | 
 
 
 
 
 | 33 | #define getnameinfo WspiapiGetNameInfo | 
 
 
 
 
 | 34 | #define freeaddrinfo WspiapiFreeAddrInfo | 
 
 
 
 
 | 35 |  | 
 
 
 
 
 | 36 | typedef int (WINAPI *WSPIAPI_PGETADDRINFO)(const char *nodename,const char *servname,const struct addrinfo *hints,struct addrinfo **res); | 
 
 
 
 
 | 37 | typedef int (WINAPI *WSPIAPI_PGETNAMEINFO)(const struct sockaddr *sa,socklen_t salen,char *host,size_t hostlen,char *serv,size_t servlen,int flags); | 
 
 
 
 
 | 38 | typedef void (WINAPI *WSPIAPI_PFREEADDRINFO)(struct addrinfo *ai); | 
 
 
 
 
 | 39 |  | 
 
 
 
 
 | 40 | #ifdef __cplusplus | 
 
 
 
 
 | 41 | extern "C" { | 
 
 
 
 
 | 42 | #endif | 
 
 
 
 
 | 43 | typedef struct { | 
 
 
 
 
 | 44 | char const *pszName; | 
 
 
 
 
 | 45 | FARPROC pfAddress; | 
 
 
 
 
 | 46 | } WSPIAPI_FUNCTION; | 
 
 
 
 
 | 47 |  | 
 
 
 
 
 | 48 | #define WSPIAPI_FUNCTION_ARRAY { { "getaddrinfo",(FARPROC) WspiapiLegacyGetAddrInfo }, \ | 
 
 
 
 
 | 49 | { "getnameinfo",(FARPROC) WspiapiLegacyGetNameInfo }, \ | 
 
 
 
 
 | 50 | { "freeaddrinfo",(FARPROC) WspiapiLegacyFreeAddrInfo } } | 
 
 
 
 
 | 51 |  | 
 
 
 
 
 | 52 | char *WINAPI WspiapiStrdup (const char *pszString); | 
 
 
 
 
 | 53 | WINBOOL WINAPI WspiapiParseV4Address (const char *pszAddress,PDWORD pdwAddress); | 
 
 
 
 
 | 54 | struct addrinfo * WINAPI WspiapiNewAddrInfo (int iSocketType,int iProtocol,WORD wPort,DWORD dwAddress); | 
 
 
 
 
 | 55 | int WINAPI WspiapiQueryDNS (const char *pszNodeName,int iSocketType,int iProtocol,WORD wPort,char pszAlias[NI_MAXHOST],struct addrinfo **pptResult); | 
 
 
 
 
 | 56 | int WINAPI WspiapiLookupNode (const char *pszNodeName,int iSocketType,int iProtocol,WORD wPort,WINBOOL bAI_CANONNAME,struct addrinfo **pptResult); | 
 
 
 
 
 | 57 | int WINAPI WspiapiClone (WORD wPort,struct addrinfo *ptResult); | 
 
 
 
 
 | 58 | void WINAPI WspiapiLegacyFreeAddrInfo (struct addrinfo *ptHead); | 
 
 
 
 
 | 59 | int WINAPI WspiapiLegacyGetAddrInfo(const char *pszNodeName,const char *pszServiceName,const struct addrinfo *ptHints,struct addrinfo **pptResult); | 
 
 
 
 
 | 60 | int WINAPI WspiapiLegacyGetNameInfo(const struct sockaddr *ptSocketAddress,socklen_t tSocketLength,char *pszNodeName,size_t tNodeLength,char *pszServiceName,size_t tServiceLength,int iFlags); | 
 
 
 
 
 | 61 | FARPROC WINAPI WspiapiLoad(WORD wFunction); | 
 
 
 
 
 | 62 | int WINAPI WspiapiGetAddrInfo(const char *nodename,const char *servname,const struct addrinfo *hints,struct addrinfo **res); | 
 
 
 
 
 | 63 | int WINAPI WspiapiGetNameInfo (const struct sockaddr *sa,socklen_t salen,char *host,size_t hostlen,char *serv,size_t servlen,int flags); | 
 
 
 
 
 | 64 | void WINAPI WspiapiFreeAddrInfo (struct addrinfo *ai); | 
 
 
 
 
 | 65 |  | 
 
 
 
 
 | 66 | #ifndef __CRT__NO_INLINE | 
 
 
 
 
 | 67 | __CRT_INLINE char * WINAPI | 
 
 
 
 
 | 68 | WspiapiStrdup (const char *pszString) | 
 
 
 
 
 | 69 | { | 
 
 
 
 
 | 70 | char *rstr; | 
 
 
 
 
 | 71 | size_t szlen; | 
 
 
 
 
 | 72 |  | 
 
 
 
 
 | 73 | if(!pszString) | 
 
 
 
 
 | 74 | return NULL; | 
 
 
 
 
 | 75 | szlen = strlen(pszString) + 1; | 
 
 
 
 
 | 76 | rstr = (char *) WspiapiMalloc (szlen); | 
 
 
 
 
 | 77 | if (!rstr) | 
 
 
 
 
 | 78 | return NULL; | 
 
 
 
 
 | 79 | strcpy (rstr, pszString); | 
 
 
 
 
 | 80 | return rstr; | 
 
 
 
 
 | 81 | } | 
 
 
 
 
 | 82 |  | 
 
 
 
 
 | 83 | __CRT_INLINE WINBOOL WINAPI | 
 
 
 
 
 | 84 | WspiapiParseV4Address (const char *pszAddress, PDWORD pdwAddress) | 
 
 
 
 
 | 85 | { | 
 
 
 
 
 | 86 | DWORD dwAddress = 0; | 
 
 
 
 
 | 87 | const char *h = NULL; | 
 
 
 
 
 | 88 | int cnt; | 
 
 
 
 
 | 89 |  | 
 
 
 
 
 | 90 | for (cnt = 0,h = pszAddress; *h != 0; h++) | 
 
 
 
 
 | 91 | if (h[0] == '.') | 
 
 
 
 
 | 92 | cnt++; | 
 
 
 
 
 | 93 | if (cnt != 3) | 
 
 
 
 
 | 94 | return FALSE; | 
 
 
 
 
 | 95 | dwAddress = inet_addr (pszAddress); | 
 
 
 
 
 | 96 | if (dwAddress == INADDR_NONE) | 
 
 
 
 
 | 97 | return FALSE; | 
 
 
 
 
 | 98 | *pdwAddress = dwAddress; | 
 
 
 
 
 | 99 | return TRUE; | 
 
 
 
 
 | 100 | } | 
 
 
 
 
 | 101 |  | 
 
 
 
 
 | 102 | __CRT_INLINE struct addrinfo * WINAPI | 
 
 
 
 
 | 103 | WspiapiNewAddrInfo (int iSocketType,int iProtocol, WORD wPort,DWORD dwAddress) | 
 
 
 
 
 | 104 | { | 
 
 
 
 
 | 105 | struct addrinfo *n; | 
 
 
 
 
 | 106 | struct sockaddr_in *pa; | 
 
 
 
 
 | 107 |  | 
 
 
 
 
 | 108 | if ((n = (struct addrinfo *) WspiapiMalloc (sizeof (struct addrinfo))) == NULL) | 
 
 
 
 
 | 109 | return NULL; | 
 
 
 
 
 | 110 | if ((pa = (struct sockaddr_in *) WspiapiMalloc (sizeof(struct sockaddr_in))) == NULL) | 
 
 
 
 
 | 111 | { | 
 
 
 
 
 | 112 | WspiapiFree(n); | 
 
 
 
 
 | 113 | return NULL; | 
 
 
 
 
 | 114 | } | 
 
 
 
 
 | 115 | pa->sin_family = AF_INET; | 
 
 
 
 
 | 116 | pa->sin_port = wPort; | 
 
 
 
 
 | 117 | pa->sin_addr.s_addr = dwAddress; | 
 
 
 
 
 | 118 | n->ai_family = PF_INET; | 
 
 
 
 
 | 119 | n->ai_socktype = iSocketType; | 
 
 
 
 
 | 120 | n->ai_protocol = iProtocol; | 
 
 
 
 
 | 121 | n->ai_addrlen = sizeof (struct sockaddr_in); | 
 
 
 
 
 | 122 | n->ai_addr = (struct sockaddr *) pa; | 
 
 
 
 
 | 123 | return n; | 
 
 
 
 
 | 124 | } | 
 
 
 
 
 | 125 |  | 
 
 
 
 
 | 126 | __CRT_INLINE int WINAPI | 
 
 
 
 
 | 127 | WspiapiLookupNode (const char *pszNodeName, int iSocketType, int iProtocol, WORD wPort, | 
 
 
 
 
 | 128 | WINBOOL bAI_CANONNAME, struct addrinfo **pptResult) | 
 
 
 
 
 | 129 | { | 
 
 
 
 
 | 130 | int err = 0, cntAlias = 0; | 
 
 
 
 
 | 131 | char name[NI_MAXHOST] = ""; | 
 
 
 
 
 | 132 | char alias[NI_MAXHOST] = ""; | 
 
 
 
 
 | 133 | char *pname = name, *palias = alias, *tmp = NULL; | 
 
 
 
 
 | 134 |  | 
 
 
 
 
 | 135 | strncpy (pname, pszNodeName, NI_MAXHOST - 1); | 
 
 
 
 
 | 136 | pname[NI_MAXHOST - 1] = 0; | 
 
 
 
 
 | 137 | for (;;) | 
 
 
 
 
 | 138 | { | 
 
 
 
 
 | 139 | err = WspiapiQueryDNS (pszNodeName, iSocketType, iProtocol, wPort, palias, pptResult); | 
 
 
 
 
 | 140 | if (err) | 
 
 
 
 
 | 141 | break; | 
 
 
 
 
 | 142 | if (*pptResult) | 
 
 
 
 
 | 143 | break; | 
 
 
 
 
 | 144 | ++cntAlias; | 
 
 
 
 
 | 145 | if (strlen (palias) == 0 || !strcmp (pname, palias) || cntAlias == 16) | 
 
 
 
 
 | 146 | { | 
 
 
 
 
 | 147 | err = EAI_FAIL; | 
 
 
 
 
 | 148 | break; | 
 
 
 
 
 | 149 | } | 
 
 
 
 
 | 150 | WspiapiSwap(pname, palias, tmp); | 
 
 
 
 
 | 151 | } | 
 
 
 
 
 | 152 | if (!err && bAI_CANONNAME) | 
 
 
 
 
 | 153 | { | 
 
 
 
 
 | 154 | (*pptResult)->ai_canonname = WspiapiStrdup (palias); | 
 
 
 
 
 | 155 | if (!(*pptResult)->ai_canonname) | 
 
 
 
 
 | 156 | err = EAI_MEMORY; | 
 
 
 
 
 | 157 | } | 
 
 
 
 
 | 158 | return err; | 
 
 
 
 
 | 159 | } | 
 
 
 
 
 | 160 |  | 
 
 
 
 
 | 161 | __CRT_INLINE int WINAPI | 
 
 
 
 
 | 162 | WspiapiClone (WORD wPort,struct addrinfo *ptResult) | 
 
 
 
 
 | 163 | { | 
 
 
 
 
 | 164 | struct addrinfo *p = NULL; | 
 
 
 
 
 | 165 | struct addrinfo *n = NULL; | 
 
 
 
 
 | 166 |  | 
 
 
 
 
 | 167 | for (p = ptResult; p != NULL;) | 
 
 
 
 
 | 168 | { | 
 
 
 
 
 | 169 | n = WspiapiNewAddrInfo (SOCK_DGRAM, p->ai_protocol, wPort, | 
 
 
 
 
 | 170 | ((struct sockaddr_in *) p->ai_addr)->sin_addr.s_addr); | 
 
 
 
 
 | 171 | if (!n) | 
 
 
 
 
 | 172 | break; | 
 
 
 
 
 | 173 | n->ai_next = p->ai_next; | 
 
 
 
 
 | 174 | p->ai_next = n; | 
 
 
 
 
 | 175 | p = n->ai_next; | 
 
 
 
 
 | 176 | } | 
 
 
 
 
 | 177 | if (p != NULL) | 
 
 
 
 
 | 178 | return EAI_MEMORY; | 
 
 
 
 
 | 179 | return 0; | 
 
 
 
 
 | 180 | } | 
 
 
 
 
 | 181 |  | 
 
 
 
 
 | 182 | __CRT_INLINE void WINAPI | 
 
 
 
 
 | 183 | WspiapiLegacyFreeAddrInfo (struct addrinfo *ptHead) | 
 
 
 
 
 | 184 | { | 
 
 
 
 
 | 185 | struct addrinfo *p; | 
 
 
 
 
 | 186 |  | 
 
 
 
 
 | 187 | for (p = ptHead; p != NULL; p = ptHead) | 
 
 
 
 
 | 188 | { | 
 
 
 
 
 | 189 | if (p->ai_canonname) | 
 
 
 
 
 | 190 | WspiapiFree (p->ai_canonname); | 
 
 
 
 
 | 191 | if (p->ai_addr) | 
 
 
 
 
 | 192 | WspiapiFree (p->ai_addr); | 
 
 
 
 
 | 193 | ptHead = p->ai_next; | 
 
 
 
 
 | 194 | WspiapiFree (p); | 
 
 
 
 
 | 195 | } | 
 
 
 
 
 | 196 | } | 
 
 
 
 
 | 197 | #endif /* !__CRT__NO_INLINE */ | 
 
 
 
 
 | 198 |  | 
 
 
 
 
 | 199 | #ifdef __cplusplus | 
 
 
 
 
 | 200 | } | 
 
 
 
 
 | 201 | #endif | 
 
 
 
 
 | 202 |  | 
 
 
 
 
 | 203 | #endif |