ReactOS  0.4.14-dev-77-gd9e7c48
query.c File Reference
#include "precomp.h"
#include <winreg.h>
#include <iphlpapi.h>
#include <strsafe.h>
#include <debug.h>
Include dependency graph for query.c:

Go to the source code of this file.

Macros

#define NDEBUG
 
#define CNAME_LOOP_MAX   16
 

Functions

static PCHAR DnsWToC (const WCHAR *WideString)
 
static PWCHAR DnsCToW (const CHAR *NarrowString)
 
static PCHAR DnsWToUTF8 (const WCHAR *WideString)
 
static PWCHAR DnsUTF8ToW (const CHAR *NarrowString)
 
DNS_STATUS WINAPI DnsQuery_CodePage (UINT CodePage, LPCSTR Name, WORD Type, DWORD Options, PIP4_ARRAY Servers, PDNS_RECORD *QueryResultSet, PVOID *Reserved)
 
DNS_STATUS WINAPI DnsQuery_A (LPCSTR Name, WORD Type, DWORD Options, PIP4_ARRAY Servers, PDNS_RECORD *QueryResultSet, PVOID *Reserved)
 
DNS_STATUS WINAPI DnsQuery_UTF8 (LPCSTR Name, WORD Type, DWORD Options, PIP4_ARRAY Servers, PDNS_RECORD *QueryResultSet, PVOID *Reserved)
 
WCHARxstrsave (const WCHAR *str)
 
CHARxstrsaveA (const CHAR *str)
 
HANDLE OpenNetworkDatabase (LPCWSTR Name)
 
IP4_ADDRESS CheckForCurrentHostname (CONST CHAR *Name, PFIXED_INFO network_info)
 
BOOL ParseV4Address (LPCSTR AddressString, OUT PDWORD pAddress)
 
IP4_ADDRESS FindEntryInHosts (CONST CHAR *name)
 
DNS_STATUS WINAPI DnsQuery_W (LPCWSTR Name, WORD Type, DWORD Options, PIP4_ARRAY Servers, PDNS_RECORD *QueryResultSet, PVOID *Reserved)
 
void DnsIntFreeRecordList (PDNS_RECORD ToDelete)
 

Macro Definition Documentation

◆ CNAME_LOOP_MAX

#define CNAME_LOOP_MAX   16

◆ NDEBUG

#define NDEBUG

Definition at line 16 of file query.c.

Function Documentation

◆ CheckForCurrentHostname()

IP4_ADDRESS CheckForCurrentHostname ( CONST CHAR Name,
PFIXED_INFO  network_info 
)

Definition at line 473 of file query.c.

474 {
475  PCHAR TempName;
476  DWORD AdapterAddressesSize, Status;
477  IP4_ADDRESS ret = 0, Address;
478  PIP_ADAPTER_ADDRESSES Addresses = NULL, pip;
479  BOOL Found = FALSE;
480 
481  if (network_info->DomainName[0])
482  {
483  size_t StringLength;
484  size_t TempSize = 2;
485  StringCchLengthA(network_info->HostName, sizeof(network_info->HostName), &StringLength);
486  TempSize += StringLength;
487  StringCchLengthA(network_info->DomainName, sizeof(network_info->DomainName), &StringLength);
488  TempSize += StringLength;
489  TempName = RtlAllocateHeap(RtlGetProcessHeap(), 0, TempSize);
490  StringCchCopyA(TempName, TempSize, network_info->HostName);
491  StringCchCatA(TempName, TempSize, ".");
492  StringCchCatA(TempName, TempSize, network_info->DomainName);
493  }
494  else
495  {
496  TempName = RtlAllocateHeap(RtlGetProcessHeap(), 0, 1);
497  TempName[0] = 0;
498  }
499  Found = !stricmp(Name, network_info->HostName) || !stricmp(Name, TempName);
500  RtlFreeHeap(RtlGetProcessHeap(), 0, TempName);
501  if (!Found)
502  {
503  return 0;
504  }
505  /* get adapter info */
506  AdapterAddressesSize = 0;
507  GetAdaptersAddresses(AF_INET,
508  GAA_FLAG_SKIP_FRIENDLY_NAME | GAA_FLAG_SKIP_DNS_SERVER |
509  GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST,
510  NULL,
511  Addresses,
512  &AdapterAddressesSize);
513  if (!AdapterAddressesSize)
514  {
515  return 0;
516  }
517  Addresses = RtlAllocateHeap(RtlGetProcessHeap(), 0, AdapterAddressesSize);
518  Status = GetAdaptersAddresses(AF_INET,
519  GAA_FLAG_SKIP_FRIENDLY_NAME | GAA_FLAG_SKIP_DNS_SERVER |
520  GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST,
521  NULL,
522  Addresses,
523  &AdapterAddressesSize);
524  if (Status)
525  {
526  RtlFreeHeap(RtlGetProcessHeap(), 0, Addresses);
527  return 0;
528  }
529  for (pip = Addresses; pip != NULL; pip = pip->Next) {
530  Address = ((LPSOCKADDR_IN)pip->FirstUnicastAddress->Address.lpSockaddr)->sin_addr.S_un.S_addr;
531  if (Address != ntohl(INADDR_LOOPBACK))
532  break;
533  }
535  {
536  ret = Address;
537  }
538  RtlFreeHeap(RtlGetProcessHeap(), 0, Addresses);
539  return ret;
540 }
struct sockaddr_in * LPSOCKADDR_IN
Definition: winsock.h:489
signed char * PCHAR
Definition: retypes.h:7
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:606
#define GAA_FLAG_SKIP_FRIENDLY_NAME
unsigned int BOOL
Definition: ntddk_ex.h:94
STRSAFEAPI StringCchCatA(STRSAFE_LPSTR pszDest, size_t cchDest, STRSAFE_LPCSTR pszSrc)
Definition: strsafe.h:320
smooth NULL
Definition: ftsmooth.c:416
static WCHAR Address[46]
Definition: ping.c:68
char DomainName[MAX_DOMAIN_NAME_LEN+4]
Definition: iptypes.h:82
return Found
Definition: dirsup.c:1270
_Check_return_ _CRTIMP int __cdecl stricmp(_In_z_ const char *_Str1, _In_z_ const char *_Str2)
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:588
unsigned long DWORD
Definition: ntddk_ex.h:95
int ret
DWORD IP4_ADDRESS
Definition: windns.h:36
char HostName[MAX_HOSTNAME_LEN+4]
Definition: iptypes.h:81
Status
Definition: gdiplustypes.h:24
STRSAFEAPI StringCchCopyA(STRSAFE_LPSTR pszDest, size_t cchDest, STRSAFE_LPCSTR pszSrc)
Definition: strsafe.h:145
STRSAFEAPI StringCchLengthA(STRSAFE_LPCSTR psz, size_t cchMax, size_t *pcchLength)
Definition: strsafe.h:832
#define AF_INET
Definition: tcpip.h:117
#define INADDR_LOOPBACK
Definition: winsock.h:312
static PIP_ADAPTER_ADDRESSES
Definition: iphlpapi.c:76
#define ntohl(x)
Definition: module.h:203

Referenced by DnsQuery_W().

◆ DnsCToW()

static PWCHAR DnsCToW ( const CHAR NarrowString)
static

Definition at line 69 of file query.c.

70 {
71  PWCHAR WideString;
72  int WideLen = MultiByteToWideChar(CP_ACP,
73  0,
74  NarrowString,
75  -1,
76  NULL,
77  0);
78  if (WideLen == 0)
79  return NULL;
80  WideString = RtlAllocateHeap(RtlGetProcessHeap(), 0, WideLen * sizeof(WCHAR));
81  if (WideString == NULL)
82  {
83  return NULL;
84  }
86  0,
87  NarrowString,
88  -1,
89  WideString,
90  WideLen);
91 
92  return WideString;
93 }
#define CP_ACP
Definition: compat.h:99
uint16_t * PWCHAR
Definition: typedefs.h:54
smooth NULL
Definition: ftsmooth.c:416
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:588
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define MultiByteToWideChar
Definition: compat.h:100

Referenced by DnsQuery_CodePage(), and DnsQuery_W().

◆ DnsIntFreeRecordList()

void DnsIntFreeRecordList ( PDNS_RECORD  ToDelete)

Definition at line 1034 of file query.c.

1035 {
1036  UINT i;
1037  PDNS_RECORD next = 0;
1038 
1039  while(ToDelete)
1040  {
1041  if(ToDelete->pName)
1042  RtlFreeHeap(RtlGetProcessHeap(), 0, ToDelete->pName);
1043 
1044  switch(ToDelete->wType)
1045  {
1046  case DNS_TYPE_CNAME:
1047  case DNS_TYPE_PTR:
1048  case DNS_TYPE_NS:
1049  case DNS_TYPE_MB:
1050  case DNS_TYPE_MD:
1051  case DNS_TYPE_MF:
1052  case DNS_TYPE_MG:
1053  case DNS_TYPE_MR:
1054  RtlFreeHeap(RtlGetProcessHeap(), 0, ToDelete->Data.PTR.pNameHost);
1055  break;
1056 
1057  case DNS_TYPE_MINFO:
1058  case DNS_TYPE_MX:
1059  RtlFreeHeap(RtlGetProcessHeap(), 0, ToDelete->Data.MX.pNameExchange);
1060  break;
1061 
1062  case DNS_TYPE_HINFO:
1063  for(i = 0; i < ToDelete->Data.TXT.dwStringCount; i++)
1064  RtlFreeHeap(RtlGetProcessHeap(), 0, ToDelete->Data.TXT.pStringArray[i]);
1065 
1066  RtlFreeHeap(RtlGetProcessHeap(), 0, ToDelete->Data.TXT.pStringArray);
1067  break;
1068  }
1069 
1070  next = ToDelete->pNext;
1071  RtlFreeHeap(RtlGetProcessHeap(), 0, ToDelete);
1072  ToDelete = next;
1073  }
1074 }
#define DNS_TYPE_MD
Definition: windns.h:43
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:606
#define DNS_TYPE_MG
Definition: windns.h:48
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define DNS_TYPE_PTR
Definition: windns.h:52
#define DNS_TYPE_CNAME
Definition: windns.h:45
#define PDNS_RECORD
Definition: windns.h:636
#define DNS_TYPE_MB
Definition: windns.h:47
#define DNS_TYPE_MX
Definition: windns.h:55
#define DNS_TYPE_MR
Definition: windns.h:49
static unsigned __int64 next
Definition: rand_nt.c:6
#define DNS_TYPE_MINFO
Definition: windns.h:54
unsigned int UINT
Definition: ndis.h:50
#define DNS_TYPE_NS
Definition: windns.h:42
#define DNS_TYPE_MF
Definition: windns.h:44
#define DNS_TYPE_HINFO
Definition: windns.h:53

Referenced by DnsFree(), and DnsQuery_CodePage().

◆ DnsQuery_A()

DNS_STATUS WINAPI DnsQuery_A ( LPCSTR  Name,
WORD  Type,
DWORD  Options,
PIP4_ARRAY  Servers,
PDNS_RECORD QueryResultSet,
PVOID Reserved 
)

Definition at line 322 of file query.c.

328 {
329  return DnsQuery_CodePage(CP_ACP, Name, Type, Options, Servers, QueryResultSet, Reserved);
330 }
Type
Definition: Type.h:6
#define CP_ACP
Definition: compat.h:99
enum OPTION_FLAGS Options
Definition: stats.c:44
DNS_STATUS WINAPI DnsQuery_CodePage(UINT CodePage, LPCSTR Name, WORD Type, DWORD Options, PIP4_ARRAY Servers, PDNS_RECORD *QueryResultSet, PVOID *Reserved)
Definition: query.c:154
_Reserved_ PVOID Reserved
Definition: winddi.h:3974

Referenced by TestHostName().

◆ DnsQuery_CodePage()

DNS_STATUS WINAPI DnsQuery_CodePage ( UINT  CodePage,
LPCSTR  Name,
WORD  Type,
DWORD  Options,
PIP4_ARRAY  Servers,
PDNS_RECORD QueryResultSet,
PVOID Reserved 
)

Definition at line 154 of file query.c.

161 {
162  UINT i;
163  PWCHAR Buffer;
164  DNS_STATUS Status;
165  PDNS_RECORD QueryResultWide;
166  PDNS_RECORD ConvertedRecord = 0, LastRecord = 0;
167 
168  if (Name == NULL)
170  if (QueryResultSet == NULL)
172 
173  switch (CodePage)
174  {
175  case CP_ACP:
176  Buffer = DnsCToW(Name);
177  break;
178 
179  case CP_UTF8:
181  break;
182 
183  default:
185  }
186 
187  Status = DnsQuery_W(Buffer, Type, Options, Servers, &QueryResultWide, Reserved);
188 
189  while (Status == ERROR_SUCCESS && QueryResultWide)
190  {
191  switch (QueryResultWide->wType)
192  {
193  case DNS_TYPE_A:
194  case DNS_TYPE_WKS:
195  case DNS_TYPE_CNAME:
196  case DNS_TYPE_PTR:
197  case DNS_TYPE_NS:
198  case DNS_TYPE_MB:
199  case DNS_TYPE_MD:
200  case DNS_TYPE_MF:
201  case DNS_TYPE_MG:
202  case DNS_TYPE_MR:
203  ConvertedRecord = RtlAllocateHeap(RtlGetProcessHeap(), 0, sizeof(DNS_RECORD));
204  break;
205 
206  case DNS_TYPE_MINFO:
207  ConvertedRecord = RtlAllocateHeap(RtlGetProcessHeap(), 0, sizeof(DNS_TXT_DATA) + QueryResultWide->Data.TXT.dwStringCount);
208  break;
209 
210  case DNS_TYPE_NULL:
211  ConvertedRecord = RtlAllocateHeap(RtlGetProcessHeap(), 0, sizeof(DNS_NULL_DATA) + QueryResultWide->Data.Null.dwByteCount);
212  break;
213  }
214  if (ConvertedRecord == NULL)
215  {
216  /* The name */
217  RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer);
218  /* The result*/
219  DnsIntFreeRecordList(QueryResultWide);
220  QueryResultSet = NULL;
221  return ERROR_OUTOFMEMORY;
222  }
223 
224  if (CodePage == CP_ACP)
225  ConvertedRecord->pName = DnsWToC((PWCHAR)QueryResultWide->pName);
226  else
227  ConvertedRecord->pName = DnsWToUTF8((PWCHAR)QueryResultWide->pName);
228 
229  ConvertedRecord->wType = QueryResultWide->wType;
230 
231  switch (QueryResultWide->wType)
232  {
233  case DNS_TYPE_A:
234  case DNS_TYPE_WKS:
235  ConvertedRecord->wDataLength = QueryResultWide->wDataLength;
236  memcpy(&ConvertedRecord->Data, &QueryResultWide->Data, QueryResultWide->wDataLength);
237  break;
238 
239  case DNS_TYPE_CNAME:
240  case DNS_TYPE_PTR:
241  case DNS_TYPE_NS:
242  case DNS_TYPE_MB:
243  case DNS_TYPE_MD:
244  case DNS_TYPE_MF:
245  case DNS_TYPE_MG:
246  case DNS_TYPE_MR:
247  ConvertedRecord->wDataLength = sizeof(DNS_PTR_DATA);
248  if (CodePage == CP_ACP)
249  ConvertedRecord->Data.PTR.pNameHost = DnsWToC((PWCHAR)QueryResultWide->Data.PTR.pNameHost);
250  else
251  ConvertedRecord->Data.PTR.pNameHost = DnsWToUTF8((PWCHAR)QueryResultWide->Data.PTR.pNameHost);
252  break;
253 
254  case DNS_TYPE_MINFO:
255  ConvertedRecord->wDataLength = sizeof(DNS_MINFO_DATA);
256  if (CodePage == CP_ACP)
257  {
258  ConvertedRecord->Data.MINFO.pNameMailbox = DnsWToC((PWCHAR)QueryResultWide->Data.MINFO.pNameMailbox);
259  ConvertedRecord->Data.MINFO.pNameErrorsMailbox = DnsWToC((PWCHAR)QueryResultWide->Data.MINFO.pNameErrorsMailbox);
260  }
261  else
262  {
263  ConvertedRecord->Data.MINFO.pNameMailbox = DnsWToUTF8((PWCHAR)QueryResultWide->Data.MINFO.pNameMailbox);
264  ConvertedRecord->Data.MINFO.pNameErrorsMailbox = DnsWToUTF8((PWCHAR)QueryResultWide->Data.MINFO.pNameErrorsMailbox);
265  }
266  break;
267 
268  case DNS_TYPE_MX:
269  ConvertedRecord->wDataLength = sizeof(DNS_MX_DATA);
270  if (CodePage == CP_ACP)
271  ConvertedRecord->Data.MX.pNameExchange = DnsWToC((PWCHAR)QueryResultWide->Data.MX.pNameExchange);
272  else
273  ConvertedRecord->Data.MX.pNameExchange = DnsWToUTF8((PWCHAR)QueryResultWide->Data.MX.pNameExchange);
274  ConvertedRecord->Data.MX.wPreference = QueryResultWide->Data.MX.wPreference;
275  break;
276 
277  case DNS_TYPE_HINFO:
278  ConvertedRecord->wDataLength = sizeof(DNS_TXT_DATA) + (sizeof(PCHAR) * QueryResultWide->Data.TXT.dwStringCount);
279  ConvertedRecord->Data.TXT.dwStringCount = QueryResultWide->Data.TXT.dwStringCount;
280 
281  if (CodePage == CP_ACP)
282  for (i = 0; i < ConvertedRecord->Data.TXT.dwStringCount; i++)
283  ConvertedRecord->Data.TXT.pStringArray[i] = DnsWToC((PWCHAR)QueryResultWide->Data.TXT.pStringArray[i]);
284  else
285  for (i = 0; i < ConvertedRecord->Data.TXT.dwStringCount; i++)
286  ConvertedRecord->Data.TXT.pStringArray[i] = DnsWToUTF8((PWCHAR)QueryResultWide->Data.TXT.pStringArray[i]);
287 
288  break;
289 
290  case DNS_TYPE_NULL:
291  ConvertedRecord->wDataLength = sizeof(DNS_NULL_DATA) + QueryResultWide->Data.Null.dwByteCount;
292  ConvertedRecord->Data.Null.dwByteCount = QueryResultWide->Data.Null.dwByteCount;
293  memcpy(&ConvertedRecord->Data.Null.Data, &QueryResultWide->Data.Null.Data, QueryResultWide->Data.Null.dwByteCount);
294  break;
295  }
296 
297  if (LastRecord)
298  {
299  LastRecord->pNext = ConvertedRecord;
300  LastRecord = LastRecord->pNext;
301  }
302  else
303  {
304  LastRecord = *QueryResultSet = ConvertedRecord;
305  }
306 
307  QueryResultWide = QueryResultWide->pNext;
308  }
309 
310  if (LastRecord)
311  LastRecord->pNext = 0;
312 
313  /* The name */
314  RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer);
315  /* The result*/
316  if (QueryResultWide) DnsIntFreeRecordList(QueryResultWide);
317 
318  return Status;
319 }
#define DNS_TXT_DATA
Definition: windns.h:929
signed char * PCHAR
Definition: retypes.h:7
#define ERROR_INVALID_PARAMETER
Definition: compat.h:91
#define DNS_TYPE_MD
Definition: windns.h:43
#define DNS_TYPE_A
Definition: windns.h:41
#define DNS_MINFO_DATA
Definition: windns.h:917
#define DNS_MX_DATA
Definition: windns.h:919
static PCHAR DnsWToC(const WCHAR *WideString)
Definition: query.c:38
Type
Definition: Type.h:6
#define ERROR_SUCCESS
Definition: deptool.c:10
#define CP_ACP
Definition: compat.h:99
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:606
void DnsIntFreeRecordList(PDNS_RECORD ToDelete)
Definition: query.c:1034
#define DNS_TYPE_WKS
Definition: windns.h:51
uint16_t * PWCHAR
Definition: typedefs.h:54
#define DNS_TYPE_MG
Definition: windns.h:48
enum OPTION_FLAGS Options
Definition: stats.c:44
#define DNS_PTR_DATA
Definition: windns.h:923
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define CP_UTF8
Definition: nls.h:20
#define DNS_TYPE_PTR
Definition: windns.h:52
smooth NULL
Definition: ftsmooth.c:416
#define DNS_TYPE_CNAME
Definition: windns.h:45
_Reserved_ PVOID Reserved
Definition: winddi.h:3974
Definition: bufpool.h:45
#define PDNS_RECORD
Definition: windns.h:636
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:588
#define DNS_TYPE_MB
Definition: windns.h:47
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
#define DNS_TYPE_MX
Definition: windns.h:55
#define DNS_TYPE_MR
Definition: windns.h:49
#define DNS_RECORD
Definition: windns.h:635
static PCHAR DnsWToUTF8(const WCHAR *WideString)
Definition: query.c:96
static PWCHAR DnsCToW(const CHAR *NarrowString)
Definition: query.c:69
Status
Definition: gdiplustypes.h:24
#define DNS_TYPE_NULL
Definition: windns.h:50
#define DNS_TYPE_MINFO
Definition: windns.h:54
unsigned int UINT
Definition: ndis.h:50
#define DNS_TYPE_NS
Definition: windns.h:42
static PWCHAR DnsUTF8ToW(const CHAR *NarrowString)
Definition: query.c:127
DNS_STATUS WINAPI DnsQuery_W(LPCWSTR Name, WORD Type, DWORD Options, PIP4_ARRAY Servers, PDNS_RECORD *QueryResultSet, PVOID *Reserved)
Definition: query.c:725
#define DNS_TYPE_MF
Definition: windns.h:44
IN BOOLEAN OUT PSTR Buffer
Definition: progress.h:34
#define DNS_TYPE_HINFO
Definition: windns.h:53
#define ERROR_OUTOFMEMORY
Definition: deptool.c:13

Referenced by DnsQuery_A(), and DnsQuery_UTF8().

◆ DnsQuery_UTF8()

DNS_STATUS WINAPI DnsQuery_UTF8 ( LPCSTR  Name,
WORD  Type,
DWORD  Options,
PIP4_ARRAY  Servers,
PDNS_RECORD QueryResultSet,
PVOID Reserved 
)

Definition at line 333 of file query.c.

339 {
340  return DnsQuery_CodePage(CP_UTF8, Name, Type, Options, Servers, QueryResultSet, Reserved);
341 }
Type
Definition: Type.h:6
enum OPTION_FLAGS Options
Definition: stats.c:44
DNS_STATUS WINAPI DnsQuery_CodePage(UINT CodePage, LPCSTR Name, WORD Type, DWORD Options, PIP4_ARRAY Servers, PDNS_RECORD *QueryResultSet, PVOID *Reserved)
Definition: query.c:154
#define CP_UTF8
Definition: nls.h:20
_Reserved_ PVOID Reserved
Definition: winddi.h:3974

Referenced by TestHostName().

◆ DnsQuery_W()

DNS_STATUS WINAPI DnsQuery_W ( LPCWSTR  Name,
WORD  Type,
DWORD  Options,
PIP4_ARRAY  Servers,
PDNS_RECORD QueryResultSet,
PVOID Reserved 
)

Definition at line 725 of file query.c.

731 {
732  adns_state astate;
733  int quflags = (Options & DNS_QUERY_NO_RECURSION) == 0 ? adns_qf_search : 0;
734  int adns_error;
735  adns_answer *answer;
736  LPSTR CurrentName;
737  unsigned i, CNameLoop;
738  PFIXED_INFO network_info;
739  ULONG network_info_blen = 0;
740  DWORD network_info_result;
741  PIP_ADDR_STRING pip;
743  struct in_addr addr;
744  PCHAR HostWithDomainName;
745  PCHAR AnsiName;
746  size_t NameLen = 0;
747 
748  if (Name == NULL)
750  if (QueryResultSet == NULL)
754 
755  *QueryResultSet = 0;
756 
757  switch (Type)
758  {
759  case DNS_TYPE_A:
760  /* FIXME: how much instead of MAX_PATH? */
761  NameLen = WideCharToMultiByte(CP_ACP,
762  0,
763  Name,
764  -1,
765  NULL,
766  0,
767  NULL,
768  0);
769  AnsiName = RtlAllocateHeap(RtlGetProcessHeap(), 0, NameLen);
770  if (NULL == AnsiName)
771  {
772  return ERROR_OUTOFMEMORY;
773  }
775  0,
776  Name,
777  -1,
778  AnsiName,
779  NameLen,
780  NULL,
781  0);
782  NameLen--;
783  /* Is it an IPv4 address? */
784  if (ParseV4Address(AnsiName, &Address))
785  {
786  RtlFreeHeap(RtlGetProcessHeap(), 0, AnsiName);
787  *QueryResultSet = (PDNS_RECORD)RtlAllocateHeap(RtlGetProcessHeap(), 0, sizeof(DNS_RECORD));
788 
789  if (NULL == *QueryResultSet)
790  {
791  return ERROR_OUTOFMEMORY;
792  }
793 
794  (*QueryResultSet)->pNext = NULL;
795  (*QueryResultSet)->wType = Type;
796  (*QueryResultSet)->wDataLength = sizeof(DNS_A_DATA);
797  (*QueryResultSet)->Data.A.IpAddress = Address;
798 
799  (*QueryResultSet)->pName = (LPSTR)xstrsave(Name);
800 
801  return (*QueryResultSet)->pName ? ERROR_SUCCESS : ERROR_OUTOFMEMORY;
802  }
803 
804  /* Check allowed characters
805  * According to RFC a-z,A-Z,0-9,-,_, but can't start or end with - or _
806  */
807  if (AnsiName[0] == '-' || AnsiName[0] == '_' || AnsiName[NameLen - 1] == '-' ||
808  AnsiName[NameLen - 1] == '_' || strstr(AnsiName, "..") != NULL)
809  {
810  RtlFreeHeap(RtlGetProcessHeap(), 0, AnsiName);
811  return ERROR_INVALID_NAME;
812  }
813  i = 0;
814  while (i < NameLen)
815  {
816  if (!((AnsiName[i] >= 'a' && AnsiName[i] <= 'z') ||
817  (AnsiName[i] >= 'A' && AnsiName[i] <= 'Z') ||
818  (AnsiName[i] >= '0' && AnsiName[i] <= '9') ||
819  AnsiName[i] == '-' || AnsiName[i] == '_' || AnsiName[i] == '.'))
820  {
821  RtlFreeHeap(RtlGetProcessHeap(), 0, AnsiName);
823  }
824  i++;
825  }
826 
827  if ((Options & DNS_QUERY_NO_HOSTS_FILE) == 0)
828  {
829  if ((Address = FindEntryInHosts(AnsiName)) != 0)
830  {
831  RtlFreeHeap(RtlGetProcessHeap(), 0, AnsiName);
832  *QueryResultSet = (PDNS_RECORD)RtlAllocateHeap(RtlGetProcessHeap(), 0, sizeof(DNS_RECORD));
833 
834  if (NULL == *QueryResultSet)
835  {
836  return ERROR_OUTOFMEMORY;
837  }
838 
839  (*QueryResultSet)->pNext = NULL;
840  (*QueryResultSet)->wType = Type;
841  (*QueryResultSet)->wDataLength = sizeof(DNS_A_DATA);
842  (*QueryResultSet)->Data.A.IpAddress = Address;
843 
844  (*QueryResultSet)->pName = (LPSTR)xstrsave(Name);
845 
846  return (*QueryResultSet)->pName ? ERROR_SUCCESS : ERROR_OUTOFMEMORY;
847  }
848  }
849 
850  network_info_result = GetNetworkParams(NULL, &network_info_blen);
851  network_info = (PFIXED_INFO)RtlAllocateHeap(RtlGetProcessHeap(), 0, (size_t)network_info_blen);
852  if (NULL == network_info)
853  {
854  return ERROR_OUTOFMEMORY;
855  }
856 
857  network_info_result = GetNetworkParams(network_info, &network_info_blen);
858  if (network_info_result != ERROR_SUCCESS)
859  {
860  RtlFreeHeap(RtlGetProcessHeap(), 0, network_info);
861  return network_info_result;
862  }
863 
864  if ((Address = CheckForCurrentHostname(NameLen != 0 ? AnsiName : network_info->HostName, network_info)) != 0)
865  {
866  size_t TempLen = 2, StringLength = 0;
867  RtlFreeHeap(RtlGetProcessHeap(), 0, AnsiName);
868  StringCchLengthA(network_info->HostName, sizeof(network_info->HostName), &StringLength);
869  TempLen += StringLength;
870  StringCchLengthA(network_info->DomainName, sizeof(network_info->DomainName), &StringLength);
871  TempLen += StringLength;
872  HostWithDomainName = (PCHAR)RtlAllocateHeap(RtlGetProcessHeap(), 0, TempLen);
873  StringCchCopyA(HostWithDomainName, TempLen, network_info->HostName);
874  if (network_info->DomainName[0])
875  {
876  StringCchCatA(HostWithDomainName, TempLen, ".");
877  StringCchCatA(HostWithDomainName, TempLen, network_info->DomainName);
878  }
879  RtlFreeHeap(RtlGetProcessHeap(), 0, network_info);
880  *QueryResultSet = (PDNS_RECORD)RtlAllocateHeap(RtlGetProcessHeap(), 0, sizeof(DNS_RECORD));
881 
882  if (NULL == *QueryResultSet)
883  {
884  RtlFreeHeap(RtlGetProcessHeap(), 0, HostWithDomainName);
885  return ERROR_OUTOFMEMORY;
886  }
887 
888  (*QueryResultSet)->pNext = NULL;
889  (*QueryResultSet)->wType = Type;
890  (*QueryResultSet)->wDataLength = sizeof(DNS_A_DATA);
891  (*QueryResultSet)->Data.A.IpAddress = Address;
892 
893  (*QueryResultSet)->pName = (LPSTR)DnsCToW(HostWithDomainName);
894 
895  RtlFreeHeap(RtlGetProcessHeap(), 0, HostWithDomainName);
896  return (*QueryResultSet)->pName ? ERROR_SUCCESS : ERROR_OUTOFMEMORY;
897  }
898 
899  if ((Options & DNS_QUERY_NO_WIRE_QUERY) != 0)
900  {
901  RtlFreeHeap(RtlGetProcessHeap(), 0, AnsiName);
902  RtlFreeHeap(RtlGetProcessHeap(), 0, network_info);
903  return ERROR_FILE_NOT_FOUND;
904  }
905 
906  adns_error = adns_init(&astate, adns_if_noenv | adns_if_noerrprint | adns_if_noserverwarn, 0);
907  if (adns_error != adns_s_ok)
908  {
909  RtlFreeHeap(RtlGetProcessHeap(), 0, AnsiName);
910  RtlFreeHeap(RtlGetProcessHeap(), 0, network_info);
911  return DnsIntTranslateAdnsToDNS_STATUS(adns_error);
912  }
913  for (pip = &(network_info->DnsServerList); pip; pip = pip->Next)
914  {
915  addr.s_addr = inet_addr(pip->IpAddress.String);
916  if ((addr.s_addr != INADDR_ANY) && (addr.s_addr != INADDR_NONE))
917  adns_addserver(astate, addr);
918  }
919  if (network_info->DomainName[0])
920  {
921  adns_ccf_search(astate, "LOCALDOMAIN", -1, network_info->DomainName);
922  }
923  RtlFreeHeap(RtlGetProcessHeap(), 0, network_info);
924 
925  if (Servers)
926  {
927  for (i = 0; i < Servers->AddrCount; i++)
928  {
929  adns_addserver(astate, *((struct in_addr *)&Servers->AddrArray[i]));
930  }
931  }
932 
933  if (!adns_numservers(astate))
934  {
935  /* There are no servers to query so bail out */
936  adns_finish(astate);
937  RtlFreeHeap(RtlGetProcessHeap(), 0, AnsiName);
938  return ERROR_FILE_NOT_FOUND;
939  }
940 
941  /*
942  * adns doesn't resolve chained CNAME records (a CNAME which points to
943  * another CNAME pointing to another... pointing to an A record), according
944  * to a mailing list thread the authors believe that chained CNAME records
945  * are invalid and the DNS entries should be fixed. That's a nice academic
946  * standpoint, but there certainly are chained CNAME records out there,
947  * even some fairly major ones (at the time of this writing
948  * download.mozilla.org is a chained CNAME). Everyone else seems to resolve
949  * these fine, so we should too. So we loop here to try to resolve CNAME
950  * chains ourselves. Of course, there must be a limit to protect against
951  * CNAME loops.
952  */
953 
954 #define CNAME_LOOP_MAX 16
955 
956  CurrentName = AnsiName;
957 
958  for (CNameLoop = 0; CNameLoop < CNAME_LOOP_MAX; CNameLoop++)
959  {
960  adns_error = adns_synchronous(astate, CurrentName, adns_r_addr, quflags, &answer);
961 
962  if (adns_error != adns_s_ok)
963  {
964  adns_finish(astate);
965 
966  if (CurrentName != AnsiName)
967  RtlFreeHeap(RtlGetProcessHeap(), 0, CurrentName);
968 
969  RtlFreeHeap(RtlGetProcessHeap(), 0, AnsiName);
970  return DnsIntTranslateAdnsToDNS_STATUS(adns_error);
971  }
972 
973  if (answer && answer->rrs.addr)
974  {
975  if (CurrentName != AnsiName)
976  RtlFreeHeap(RtlGetProcessHeap(), 0, CurrentName);
977 
978  RtlFreeHeap(RtlGetProcessHeap(), 0, AnsiName);
979  *QueryResultSet = (PDNS_RECORD)RtlAllocateHeap(RtlGetProcessHeap(), 0, sizeof(DNS_RECORD));
980 
981  if (NULL == *QueryResultSet)
982  {
983  adns_finish(astate);
984  return ERROR_OUTOFMEMORY;
985  }
986 
987  (*QueryResultSet)->pNext = NULL;
988  (*QueryResultSet)->wType = Type;
989  (*QueryResultSet)->wDataLength = sizeof(DNS_A_DATA);
990  (*QueryResultSet)->Data.A.IpAddress = answer->rrs.addr->addr.inet.sin_addr.s_addr;
991 
992  adns_finish(astate);
993 
994  (*QueryResultSet)->pName = (LPSTR)xstrsave(Name);
995 
996  return (*QueryResultSet)->pName ? ERROR_SUCCESS : ERROR_OUTOFMEMORY;
997  }
998 
999  if (NULL == answer || adns_s_prohibitedcname != answer->status || NULL == answer->cname)
1000  {
1001  adns_finish(astate);
1002 
1003  if (CurrentName != AnsiName)
1004  RtlFreeHeap(RtlGetProcessHeap(), 0, CurrentName);
1005 
1006  RtlFreeHeap(RtlGetProcessHeap(), 0, AnsiName);
1007  return ERROR_FILE_NOT_FOUND;
1008  }
1009 
1010  if (CurrentName != AnsiName)
1011  RtlFreeHeap(RtlGetProcessHeap(), 0, CurrentName);
1012 
1013  CurrentName = (LPSTR)xstrsaveA(answer->cname);
1014 
1015  if (!CurrentName)
1016  {
1017  RtlFreeHeap(RtlGetProcessHeap(), 0, AnsiName);
1018  adns_finish(astate);
1019  return ERROR_OUTOFMEMORY;
1020  }
1021  }
1022 
1023  adns_finish(astate);
1024  RtlFreeHeap(RtlGetProcessHeap(), 0, AnsiName);
1025  RtlFreeHeap(RtlGetProcessHeap(), 0, CurrentName);
1026  return ERROR_FILE_NOT_FOUND;
1027 
1028  default:
1029  return ERROR_OUTOFMEMORY; /* XXX arty: find a better error code. */
1030  }
1031 }
signed char * PCHAR
Definition: retypes.h:7
#define ERROR_INVALID_PARAMETER
Definition: compat.h:91
#define DNS_TYPE_A
Definition: windns.h:41
#define inet_addr(cp)
Definition: inet.h:98
Type
Definition: Type.h:6
#define ERROR_SUCCESS
Definition: deptool.c:10
#define WideCharToMultiByte
Definition: compat.h:101
ADNS_API void adns_finish(adns_state ads)
Definition: setup.c:650
char * strstr(char *String1, char *String2)
Definition: utclib.c:653
#define CP_ACP
Definition: compat.h:99
IP_ADDRESS_STRING IpAddress
Definition: iptypes.h:47
struct _IP_ADDR_STRING * Next
Definition: iptypes.h:46
#define INADDR_NONE
Definition: tcp.c:42
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:606
#define INADDR_ANY
Definition: StrAddr.c:4
char String[4 *4]
Definition: iptypes.h:42
#define DNS_QUERY_WIRE_ONLY
Definition: windns.h:18
union adns_rr_addr::@3830 addr
IP4_ADDRESS AddrArray[1]
Definition: windns.h:161
adns_rr_addr * addr
Definition: adns.h:322
char * LPSTR
Definition: xmlstorage.h:182
enum OPTION_FLAGS Options
Definition: stats.c:44
ADNS_API void adns_ccf_search(adns_state state, const char *fn, int lno, const char *buf)
Definition: setup.c:715
#define DNS_QUERY_NO_RECURSION
Definition: windns.h:11
WCHAR * xstrsave(const WCHAR *str)
Definition: query.c:344
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
STRSAFEAPI StringCchCatA(STRSAFE_LPSTR pszDest, size_t cchDest, STRSAFE_LPCSTR pszSrc)
Definition: strsafe.h:320
DWORD WINAPI GetNetworkParams(PFIXED_INFO pFixedInfo, PULONG pOutBufLen)
smooth NULL
Definition: ftsmooth.c:416
static WCHAR Address[46]
Definition: ping.c:68
char DomainName[MAX_DOMAIN_NAME_LEN+4]
Definition: iptypes.h:82
#define PDNS_RECORD
Definition: windns.h:636
#define ERROR_FILE_NOT_FOUND
Definition: disk.h:79
#define PCHAR
Definition: match.c:90
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:588
union adns_answer::@3831 rrs
__kernel_size_t size_t
Definition: linux.h:237
IP4_ADDRESS CheckForCurrentHostname(CONST CHAR *Name, PFIXED_INFO network_info)
Definition: query.c:473
ADNS_API int adns_numservers(adns_state state)
Definition: setup.c:718
Definition: tcpip.h:125
DNS_STATUS DnsIntTranslateAdnsToDNS_STATUS(int Status)
Definition: adns.c:17
unsigned long DWORD
Definition: ntddk_ex.h:95
ADNS_API void adns_addserver(adns_state state, struct in_addr server)
Definition: setup.c:712
adns_status status
Definition: adns.h:311
DWORD IP4_ADDRESS
Definition: windns.h:36
GLenum const GLvoid * addr
Definition: glext.h:9621
char * cname
Definition: adns.h:312
#define DNS_QUERY_NO_WIRE_QUERY
Definition: windns.h:13
char HostName[MAX_HOSTNAME_LEN+4]
Definition: iptypes.h:81
struct FIXED_INFO * PFIXED_INFO
#define DNS_RECORD
Definition: windns.h:635
CHAR * xstrsaveA(const CHAR *str)
Definition: query.c:362
static PWCHAR DnsCToW(const CHAR *NarrowString)
Definition: query.c:69
#define CNAME_LOOP_MAX
STRSAFEAPI StringCchCopyA(STRSAFE_LPSTR pszDest, size_t cchDest, STRSAFE_LPCSTR pszSrc)
Definition: strsafe.h:145
BOOL ParseV4Address(LPCSTR AddressString, OUT PDWORD pAddress)
Definition: query.c:543
STRSAFEAPI StringCchLengthA(STRSAFE_LPCSTR psz, size_t cchMax, size_t *pcchLength)
Definition: strsafe.h:832
ADNS_API int adns_init(adns_state *newstate_r, adns_initflags flags, FILE *diagfile)
Definition: setup.c:568
struct sockaddr_in inet
Definition: adns.h:271
#define DNS_ERROR_INVALID_NAME_CHAR
Definition: winerror.h:1877
int adns_synchronous(adns_state ads, const char *owner, adns_rrtype type, adns_queryflags flags, adns_answer **answer_r)
Definition: query.c:314
unsigned int ULONG
Definition: retypes.h:1
#define ERROR_INVALID_NAME
Definition: compat.h:93
IP_ADDR_STRING DnsServerList
Definition: iptypes.h:84
IP4_ADDRESS FindEntryInHosts(CONST CHAR *name)
Definition: query.c:613
DWORD AddrCount
Definition: windns.h:160
#define ERROR_OUTOFMEMORY
Definition: deptool.c:13
#define DNS_QUERY_NO_HOSTS_FILE
Definition: windns.h:15

Referenced by DnsQuery_CodePage(), NSP_GetHostByNameHeapAllocW(), SaBlob_Query(), and TestHostName().

◆ DnsUTF8ToW()

static PWCHAR DnsUTF8ToW ( const CHAR NarrowString)
static

Definition at line 127 of file query.c.

128 {
129  PWCHAR WideString;
130  int WideLen = MultiByteToWideChar(CP_UTF8,
131  0,
132  NarrowString,
133  -1,
134  NULL,
135  0);
136  if (WideLen == 0)
137  return NULL;
138  WideString = RtlAllocateHeap(RtlGetProcessHeap(), 0, WideLen * sizeof(WCHAR));
139  if (WideString == NULL)
140  {
141  return NULL;
142  }
144  0,
145  NarrowString,
146  -1,
147  WideString,
148  WideLen);
149 
150  return WideString;
151 }
uint16_t * PWCHAR
Definition: typedefs.h:54
#define CP_UTF8
Definition: nls.h:20
smooth NULL
Definition: ftsmooth.c:416
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:588
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define MultiByteToWideChar
Definition: compat.h:100

Referenced by DnsQuery_CodePage().

◆ DnsWToC()

static PCHAR DnsWToC ( const WCHAR WideString)
static

Definition at line 38 of file query.c.

39 {
41  int AnsiLen = WideCharToMultiByte(CP_ACP,
42  0,
43  WideString,
44  -1,
45  NULL,
46  0,
47  NULL,
48  0);
49  if (AnsiLen == 0)
50  return NULL;
51  AnsiString = RtlAllocateHeap(RtlGetProcessHeap(), 0, AnsiLen);
52  if (AnsiString == NULL)
53  {
54  return NULL;
55  }
57  0,
58  WideString,
59  -1,
60  AnsiString,
61  AnsiLen,
62  NULL,
63  0);
64 
65  return AnsiString;
66 }
signed char * PCHAR
Definition: retypes.h:7
#define WideCharToMultiByte
Definition: compat.h:101
#define CP_ACP
Definition: compat.h:99
smooth NULL
Definition: ftsmooth.c:416
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:588

Referenced by DnsQuery_CodePage().

◆ DnsWToUTF8()

static PCHAR DnsWToUTF8 ( const WCHAR WideString)
static

Definition at line 96 of file query.c.

97 {
99  int AnsiLen = WideCharToMultiByte(CP_UTF8,
100  0,
101  WideString,
102  -1,
103  NULL,
104  0,
105  NULL,
106  0);
107  if (AnsiLen == 0)
108  return NULL;
109  AnsiString = RtlAllocateHeap(RtlGetProcessHeap(), 0, AnsiLen);
110  if (AnsiString == NULL)
111  {
112  return NULL;
113  }
115  0,
116  WideString,
117  -1,
118  AnsiString,
119  AnsiLen,
120  NULL,
121  0);
122 
123  return AnsiString;
124 }
signed char * PCHAR
Definition: retypes.h:7
#define WideCharToMultiByte
Definition: compat.h:101
#define CP_UTF8
Definition: nls.h:20
smooth NULL
Definition: ftsmooth.c:416
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:588

Referenced by DnsQuery_CodePage().

◆ FindEntryInHosts()

IP4_ADDRESS FindEntryInHosts ( CONST CHAR name)

Definition at line 613 of file query.c.

614 {
615  BOOL Found = FALSE;
616  HANDLE HostsFile;
617  CHAR HostsDBData[BUFSIZ] = { 0 };
618  PCHAR AddressStr, DnsName = NULL, AddrTerm, NameSt, NextLine, ThisLine, Comment;
619  UINT ValidData = 0;
620  DWORD ReadSize;
621  DWORD Address;
622 
623  /* Open the network database */
624  HostsFile = OpenNetworkDatabase(L"hosts");
625  if (HostsFile == INVALID_HANDLE_VALUE)
626  {
628  return 0;
629  }
630 
631  while (!Found && ReadFile(HostsFile,
632  HostsDBData + ValidData,
633  sizeof(HostsDBData) - ValidData,
634  &ReadSize,
635  NULL))
636  {
637  ValidData += ReadSize;
638  ReadSize = 0;
639  NextLine = ThisLine = HostsDBData;
640 
641  /* Find the beginning of the next line */
642  while ((NextLine < HostsDBData + ValidData) &&
643  (*NextLine != '\r') &&
644  (*NextLine != '\n'))
645  {
646  NextLine++;
647  }
648 
649  /* Zero and skip, so we can treat what we have as a string */
650  if (NextLine > HostsDBData + ValidData)
651  break;
652 
653  *NextLine = 0;
654  NextLine++;
655 
656  Comment = strchr(ThisLine, '#');
657  if (Comment)
658  *Comment = 0; /* Terminate at comment start */
659 
660  AddressStr = ThisLine;
661  /* Find the first space separating the IP address from the DNS name */
662  AddrTerm = strchr(ThisLine, ' ');
663  if (AddrTerm)
664  {
665  /* Terminate the address string */
666  *AddrTerm = 0;
667 
668  /* Find the last space before the DNS name */
669  NameSt = strrchr(ThisLine, ' ');
670 
671  /* If there is only one space (the one we removed above), then just use the address terminator */
672  if (!NameSt)
673  NameSt = AddrTerm;
674 
675  /* Move from the space to the first character of the DNS name */
676  NameSt++;
677 
678  DnsName = NameSt;
679 
680  if (!stricmp(name, DnsName) || !stricmp(name, AddressStr))
681  {
682  Found = TRUE;
683  break;
684  }
685  }
686 
687  /* Get rid of everything we read so far */
688  while (NextLine <= HostsDBData + ValidData &&
689  isspace(*NextLine))
690  {
691  NextLine++;
692  }
693 
694  if (HostsDBData + ValidData - NextLine <= 0)
695  break;
696 
697  memmove(HostsDBData, NextLine, HostsDBData + ValidData - NextLine);
698  ValidData -= NextLine - HostsDBData;
699  }
700 
701  CloseHandle(HostsFile);
702 
703  if (!Found)
704  {
706  return 0;
707  }
708 
709  if (strstr(AddressStr, ":"))
710  {
712  return 0;
713  }
714 
715  if (!ParseV4Address(AddressStr, &Address))
716  {
718  return 0;
719  }
720 
721  return Address;
722 }
signed char * PCHAR
Definition: retypes.h:7
#define isspace(c)
Definition: acclib.h:69
#define memmove(s1, s2, n)
Definition: mkisofs.h:881
#define TRUE
Definition: types.h:120
#define CloseHandle
Definition: compat.h:398
char * strstr(char *String1, char *String2)
Definition: utclib.c:653
#define WSAEINVAL
Definition: winerror.h:1946
char CHAR
Definition: xmlstorage.h:175
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
#define BUFSIZ
Definition: nsplookup.c:26
_Check_return_ _CRTIMP _CONST_RETURN char *__cdecl strrchr(_In_z_ const char *_Str, _In_ int _Ch)
unsigned int BOOL
Definition: ntddk_ex.h:94
smooth NULL
Definition: ftsmooth.c:416
static WCHAR Address[46]
Definition: ping.c:68
VOID WSAAPI WSASetLastError(IN INT iError)
Definition: dllmain.c:123
return Found
Definition: dirsup.c:1270
_Check_return_ _CRTIMP int __cdecl stricmp(_In_z_ const char *_Str1, _In_z_ const char *_Str2)
#define WSANO_RECOVERY
Definition: winerror.h:2002
#define WSANO_DATA
Definition: winerror.h:2003
unsigned long DWORD
Definition: ntddk_ex.h:95
static const WCHAR L[]
Definition: oid.c:1250
HANDLE OpenNetworkDatabase(LPCWSTR Name)
Definition: query.c:380
BOOL ParseV4Address(LPCSTR AddressString, OUT PDWORD pAddress)
Definition: query.c:543
unsigned int UINT
Definition: ndis.h:50
char * strchr(const char *String, int ch)
Definition: utclib.c:501
Definition: name.c:36
BOOL WINAPI ReadFile(IN HANDLE hFile, IN LPVOID lpBuffer, IN DWORD nNumberOfBytesToRead, OUT LPDWORD lpNumberOfBytesRead OPTIONAL, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:123
BOOLEAN NextLine
Definition: bootvid.c:28

Referenced by DnsQuery_W().

◆ OpenNetworkDatabase()

HANDLE OpenNetworkDatabase ( LPCWSTR  Name)

Definition at line 380 of file query.c.

381 {
382  PWSTR ExpandedPath;
384  INT ErrorCode;
385  HKEY DatabaseKey;
386  DWORD RegType;
387  DWORD RegSize = 0;
388  size_t StringLength;
389  HANDLE ret;
390 
391  ExpandedPath = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
392  if (!ExpandedPath)
393  return INVALID_HANDLE_VALUE;
394 
395  /* Open the database path key */
397  L"System\\CurrentControlSet\\Services\\Tcpip\\Parameters",
398  0,
399  KEY_READ,
400  &DatabaseKey);
401  if (ErrorCode == NO_ERROR)
402  {
403  /* Read the actual path */
404  ErrorCode = RegQueryValueExW(DatabaseKey,
405  L"DatabasePath",
406  NULL,
407  &RegType,
408  NULL,
409  &RegSize);
410 
411  DatabasePath = HeapAlloc(GetProcessHeap(), 0, RegSize);
412  if (!DatabasePath)
413  {
414  HeapFree(GetProcessHeap(), 0, ExpandedPath);
415  return INVALID_HANDLE_VALUE;
416  }
417 
418  /* Read the actual path */
419  ErrorCode = RegQueryValueExW(DatabaseKey,
420  L"DatabasePath",
421  NULL,
422  &RegType,
424  &RegSize);
425 
426  /* Close the key */
427  RegCloseKey(DatabaseKey);
428 
429  /* Expand the name */
431 
433  }
434  else
435  {
436  /* Use defalt path */
437  GetSystemDirectoryW(ExpandedPath, MAX_PATH);
438  StringCchLengthW(ExpandedPath, MAX_PATH, &StringLength);
439  if (ExpandedPath[StringLength - 1] != L'\\')
440  {
441  /* It isn't, so add it ourselves */
442  StringCchCatW(ExpandedPath, MAX_PATH, L"\\");
443  }
444  StringCchCatW(ExpandedPath, MAX_PATH, L"DRIVERS\\ETC\\");
445  }
446 
447  /* Make sure that the path is backslash-terminated */
448  StringCchLengthW(ExpandedPath, MAX_PATH, &StringLength);
449  if (ExpandedPath[StringLength - 1] != L'\\')
450  {
451  /* It isn't, so add it ourselves */
452  StringCchCatW(ExpandedPath, MAX_PATH, L"\\");
453  }
454 
455  /* Add the database name */
456  StringCchCatW(ExpandedPath, MAX_PATH, Name);
457 
458  /* Return a handle to the file */
459  ret = CreateFileW(ExpandedPath,
462  NULL,
465  NULL);
466 
467  HeapFree(GetProcessHeap(), 0, ExpandedPath);
468  return ret;
469 }
_In_ NDIS_ERROR_CODE ErrorCode
Definition: ndis.h:4436
#define KEY_READ
Definition: nt_native.h:1023
uint16_t * PWSTR
Definition: typedefs.h:54
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
#define NO_ERROR
Definition: dderror.h:5
int32_t INT
Definition: typedefs.h:56
#define FILE_SHARE_READ
Definition: compat.h:125
STRSAFEAPI StringCchCatW(STRSAFE_LPWSTR pszDest, size_t cchDest, STRSAFE_LPCWSTR pszSrc)
Definition: strsafe.h:325
LONG WINAPI RegCloseKey(HKEY hKey)
Definition: reg.c:423
unsigned char * LPBYTE
Definition: typedefs.h:52
#define FILE_READ_DATA
Definition: nt_native.h:628
smooth NULL
Definition: ftsmooth.c:416
#define OPEN_EXISTING
Definition: compat.h:426
#define GetProcessHeap()
Definition: compat.h:395
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
LONG WINAPI RegQueryValueExW(_In_ HKEY hkeyorg, _In_ LPCWSTR name, _In_ LPDWORD reserved, _In_ LPDWORD type, _In_ LPBYTE data, _In_ LPDWORD count)
Definition: reg.c:4116
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define MAX_PATH
Definition: compat.h:26
unsigned long DWORD
Definition: ntddk_ex.h:95
int ret
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:126
static const WCHAR L[]
Definition: oid.c:1250
PWSTR DatabasePath
Definition: database.c:31
UINT WINAPI GetSystemDirectoryW(OUT LPWSTR lpBuffer, IN UINT uSize)
Definition: path.c:2312
DWORD WINAPI ExpandEnvironmentStringsW(IN LPCWSTR lpSrc, IN LPWSTR lpDst, IN DWORD nSize)
Definition: environ.c:519
STRSAFEAPI StringCchLengthW(STRSAFE_LPCWSTR psz, size_t cchMax, size_t *pcchLength)
Definition: strsafe.h:842
#define CreateFileW
Definition: compat.h:400
LONG WINAPI RegOpenKeyExW(HKEY hKey, LPCWSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)
Definition: reg.c:3366
#define HeapFree(x, y, z)
Definition: compat.h:394
#define HKEY_LOCAL_MACHINE
Definition: winreg.h:12

Referenced by FindEntryInHosts().

◆ ParseV4Address()

BOOL ParseV4Address ( LPCSTR  AddressString,
OUT PDWORD  pAddress 
)

Definition at line 543 of file query.c.

545 {
546  CHAR * cp = (CHAR *)AddressString;
547  DWORD val, base;
548  unsigned char c;
549  DWORD parts[4], *pp = parts;
550  if (!AddressString)
551  return FALSE;
552  if (!isdigit(*cp)) return FALSE;
553 
554 again:
555  /*
556  * Collect number up to ``.''.
557  * Values are specified as for C:
558  * 0x=hex, 0=octal, other=decimal.
559  */
560  val = 0; base = 10;
561  if (*cp == '0') {
562  if (*++cp == 'x' || *cp == 'X')
563  base = 16, cp++;
564  else
565  base = 8;
566  }
567  while ((c = *cp)) {
568  if (isdigit(c)) {
569  val = (val * base) + (c - '0');
570  cp++;
571  continue;
572  }
573  if (base == 16 && isxdigit(c)) {
574  val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A'));
575  cp++;
576  continue;
577  }
578  break;
579  }
580  if (*cp == '.') {
581  /*
582  * Internet format:
583  * a.b.c.d
584  */
585  if (pp >= parts + 4) return FALSE;
586  *pp++ = val;
587  cp++;
588  goto again;
589  }
590  /*
591  * Check for trailing characters.
592  */
593  if (*cp && *cp > ' ') return FALSE;
594 
595  if (pp >= parts + 4) return FALSE;
596  *pp++ = val;
597  /*
598  * Concoct the address according to
599  * the number of parts specified.
600  */
601  if ((DWORD)(pp - parts) != 4) return FALSE;
602  if (parts[0] > 0xff || parts[1] > 0xff || parts[2] > 0xff || parts[3] > 0xff) return FALSE;
603  val = (parts[3] << 24) | (parts[2] << 16) | (parts[1] << 8) | parts[0];
604 
605  if (pAddress)
606  *pAddress = val;
607 
608  return TRUE;
609 }
#define TRUE
Definition: types.h:120
char CHAR
Definition: xmlstorage.h:175
GLuint base
Definition: 3dtext.c:35
#define pp
Definition: hlsl.yy.c:978
#define isdigit(c)
Definition: acclib.h:68
GLuint GLfloat * val
Definition: glext.h:7180
const GLubyte * c
Definition: glext.h:8905
unsigned long DWORD
Definition: ntddk_ex.h:95
#define islower(c)
Definition: acclib.h:72
#define isxdigit(c)
Definition: acclib.h:70
POINT cp
Definition: magnifier.c:59
#define c
Definition: ke_i.h:80

Referenced by DnsQuery_W(), and FindEntryInHosts().

◆ xstrsave()

WCHAR* xstrsave ( const WCHAR str)

Definition at line 344 of file query.c.

345 {
346  WCHAR *p;
347  size_t len = 0;
348 
349  /* FIXME: how much instead of MAX_PATH? */
351  len+=sizeof(WCHAR);
352 
353  p = RtlAllocateHeap(RtlGetProcessHeap(), 0, len);
354 
355  if (p)
356  StringCbCopyW(p, len, str);
357 
358  return p;
359 }
STRSAFEAPI StringCbLengthW(STRSAFE_LPCWSTR psz, size_t cbMax, size_t *pcbLength)
Definition: strsafe.h:876
const WCHAR * str
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:588
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define MAX_PATH
Definition: compat.h:26
GLenum GLsizei len
Definition: glext.h:6722
GLfloat GLfloat p
Definition: glext.h:8902
STRSAFEAPI StringCbCopyW(STRSAFE_LPWSTR pszDest, size_t cbDest, STRSAFE_LPCWSTR pszSrc)
Definition: strsafe.h:166

Referenced by DnsQuery_W(), of_asynch_id(), of_ptr(), prep_query(), and query_do().

◆ xstrsaveA()

CHAR* xstrsaveA ( const CHAR str)

Definition at line 362 of file query.c.

363 {
364  CHAR *p;
365  size_t len = 0;
366 
367  /* FIXME: how much instead of MAX_PATH? */
369  len++;
370 
371  p = RtlAllocateHeap(RtlGetProcessHeap(), 0, len);
372 
373  if (p)
374  StringCbCopyA(p, len, str);
375 
376  return p;
377 }
char CHAR
Definition: xmlstorage.h:175
const WCHAR * str
STRSAFEAPI StringCbCopyA(STRSAFE_LPSTR pszDest, size_t cbDest, STRSAFE_LPCSTR pszSrc)
Definition: strsafe.h:161
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:588
#define MAX_PATH
Definition: compat.h:26
GLenum GLsizei len
Definition: glext.h:6722
STRSAFEAPI StringCbLengthA(STRSAFE_LPCSTR psz, size_t cbMax, size_t *pcbLength)
Definition: strsafe.h:859
GLfloat GLfloat p
Definition: glext.h:8902

Referenced by DnsQuery_W().