ReactOS  0.4.13-dev-259-g5ca9c9c
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)
 
DNS_STATUS WINAPI DnsQuery_A (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)
 
DNS_STATUS WINAPI DnsQuery_UTF8 (LPCSTR 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 341 of file query.c.

342 {
343  PCHAR TempName;
344  DWORD AdapterAddressesSize, Status;
345  IP4_ADDRESS ret = 0, Address;
346  PIP_ADAPTER_ADDRESSES Addresses = NULL, pip;
347  BOOL Found = FALSE;
348 
349  if (network_info->DomainName[0])
350  {
351  size_t StringLength;
352  size_t TempSize = 2;
353  StringCchLengthA(network_info->HostName, sizeof(network_info->HostName), &StringLength);
354  TempSize += StringLength;
355  StringCchLengthA(network_info->DomainName, sizeof(network_info->DomainName), &StringLength);
356  TempSize += StringLength;
357  TempName = RtlAllocateHeap(RtlGetProcessHeap(), 0, TempSize);
358  StringCchCopyA(TempName, TempSize, network_info->HostName);
359  StringCchCatA(TempName, TempSize, ".");
360  StringCchCatA(TempName, TempSize, network_info->DomainName);
361  }
362  else
363  {
364  TempName = RtlAllocateHeap(RtlGetProcessHeap(), 0, 1);
365  TempName[0] = 0;
366  }
367  Found = !stricmp(Name, network_info->HostName) || !stricmp(Name, TempName);
368  RtlFreeHeap(RtlGetProcessHeap(), 0, TempName);
369  if (!Found)
370  {
371  return 0;
372  }
373  /* get adapter info */
374  AdapterAddressesSize = 0;
375  GetAdaptersAddresses(AF_INET,
376  GAA_FLAG_SKIP_FRIENDLY_NAME | GAA_FLAG_SKIP_DNS_SERVER |
377  GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST,
378  NULL,
379  Addresses,
380  &AdapterAddressesSize);
381  if (!AdapterAddressesSize)
382  {
383  return 0;
384  }
385  Addresses = RtlAllocateHeap(RtlGetProcessHeap(), 0, AdapterAddressesSize);
386  Status = GetAdaptersAddresses(AF_INET,
387  GAA_FLAG_SKIP_FRIENDLY_NAME | GAA_FLAG_SKIP_DNS_SERVER |
388  GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST,
389  NULL,
390  Addresses,
391  &AdapterAddressesSize);
392  if (Status)
393  {
394  RtlFreeHeap(RtlGetProcessHeap(), 0, Addresses);
395  return 0;
396  }
397  for (pip = Addresses; pip != NULL; pip = pip->Next) {
398  Address = ((LPSOCKADDR_IN)pip->FirstUnicastAddress->Address.lpSockaddr)->sin_addr.S_un.S_addr;
399  if (Address != ntohl(INADDR_LOOPBACK))
400  break;
401  }
403  {
404  ret = Address;
405  }
406  RtlFreeHeap(RtlGetProcessHeap(), 0, Addresses);
407  return ret;
408 }
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:603
#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:585
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  WideLen *= sizeof(WCHAR);
81  WideString = RtlAllocateHeap(RtlGetProcessHeap(), 0, WideLen);
82  if (WideString == NULL)
83  {
84  return NULL;
85  }
87  0,
88  NarrowString,
89  -1,
90  WideString,
91  WideLen);
92 
93  return WideString;
94 }
#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:585
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define MultiByteToWideChar
Definition: compat.h:100

Referenced by DnsQuery_A(), and DnsQuery_W().

◆ DnsIntFreeRecordList()

void DnsIntFreeRecordList ( PDNS_RECORD  ToDelete)

Definition at line 914 of file query.c.

915 {
916  UINT i;
917  PDNS_RECORD next = 0;
918 
919  while(ToDelete)
920  {
921  if(ToDelete->pName)
922  RtlFreeHeap(RtlGetProcessHeap(), 0, ToDelete->pName);
923 
924  switch(ToDelete->wType)
925  {
926  case DNS_TYPE_CNAME:
927  case DNS_TYPE_PTR:
928  case DNS_TYPE_NS:
929  case DNS_TYPE_MB:
930  case DNS_TYPE_MD:
931  case DNS_TYPE_MF:
932  case DNS_TYPE_MG:
933  case DNS_TYPE_MR:
934  RtlFreeHeap(RtlGetProcessHeap(), 0, ToDelete->Data.PTR.pNameHost);
935  break;
936 
937  case DNS_TYPE_MINFO:
938  case DNS_TYPE_MX:
939  RtlFreeHeap(RtlGetProcessHeap(), 0, ToDelete->Data.MX.pNameExchange);
940  break;
941 
942  case DNS_TYPE_HINFO:
943  for(i = 0; i < ToDelete->Data.TXT.dwStringCount; i++)
944  RtlFreeHeap(RtlGetProcessHeap(), 0, ToDelete->Data.TXT.pStringArray[i]);
945 
946  RtlFreeHeap(RtlGetProcessHeap(), 0, ToDelete->Data.TXT.pStringArray);
947  break;
948  }
949 
950  next = ToDelete->pNext;
951  RtlFreeHeap(RtlGetProcessHeap(), 0, ToDelete);
952  ToDelete = next;
953  }
954 }
#define DNS_TYPE_MD
Definition: windns.h:43
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:603
#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_A().

◆ DnsQuery_A()

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

Definition at line 97 of file query.c.

103 {
104  UINT i;
105  PWCHAR Buffer;
106  DNS_STATUS Status;
107  PDNS_RECORD QueryResultWide;
108  PDNS_RECORD ConvertedRecord = 0, LastRecord = 0;
109 
110  if (Name == NULL)
112  if (QueryResultSet == NULL)
114 
115  Buffer = DnsCToW(Name);
116 
117  Status = DnsQuery_W(Buffer, Type, Options, Servers, &QueryResultWide, Reserved);
118 
119  while (Status == ERROR_SUCCESS && QueryResultWide)
120  {
121  switch (QueryResultWide->wType)
122  {
123  case DNS_TYPE_A:
124  case DNS_TYPE_WKS:
125  ConvertedRecord = RtlAllocateHeap(RtlGetProcessHeap(), 0, sizeof(DNS_RECORD));
126  ConvertedRecord->pName = DnsWToC((PWCHAR)QueryResultWide->pName);
127  ConvertedRecord->wType = QueryResultWide->wType;
128  ConvertedRecord->wDataLength = QueryResultWide->wDataLength;
129  memcpy(&ConvertedRecord->Data, &QueryResultWide->Data, QueryResultWide->wDataLength);
130  break;
131 
132  case DNS_TYPE_CNAME:
133  case DNS_TYPE_PTR:
134  case DNS_TYPE_NS:
135  case DNS_TYPE_MB:
136  case DNS_TYPE_MD:
137  case DNS_TYPE_MF:
138  case DNS_TYPE_MG:
139  case DNS_TYPE_MR:
140  ConvertedRecord = RtlAllocateHeap(RtlGetProcessHeap(), 0, sizeof(DNS_RECORD));
141  ConvertedRecord->pName = DnsWToC((PWCHAR)QueryResultWide->pName);
142  ConvertedRecord->wType = QueryResultWide->wType;
143  ConvertedRecord->wDataLength = sizeof(DNS_PTR_DATA);
144  ConvertedRecord->Data.PTR.pNameHost = DnsWToC((PWCHAR)QueryResultWide->Data.PTR.pNameHost);
145  break;
146 
147  case DNS_TYPE_MINFO:
148  ConvertedRecord = RtlAllocateHeap(RtlGetProcessHeap(), 0, sizeof(DNS_RECORD));
149  ConvertedRecord->pName = DnsWToC((PWCHAR)QueryResultWide->pName);
150  ConvertedRecord->wType = QueryResultWide->wType;
151  ConvertedRecord->wDataLength = sizeof(DNS_MINFO_DATA);
152  ConvertedRecord->Data.MINFO.pNameMailbox = DnsWToC((PWCHAR)QueryResultWide->Data.MINFO.pNameMailbox);
153  ConvertedRecord->Data.MINFO.pNameErrorsMailbox = DnsWToC((PWCHAR)QueryResultWide->Data.MINFO.pNameErrorsMailbox);
154  break;
155 
156  case DNS_TYPE_MX:
157  ConvertedRecord = RtlAllocateHeap(RtlGetProcessHeap(), 0, sizeof(DNS_RECORD));
158  ConvertedRecord->pName = DnsWToC((PWCHAR)QueryResultWide->pName);
159  ConvertedRecord->wType = QueryResultWide->wType;
160  ConvertedRecord->wDataLength = sizeof(DNS_MX_DATA);
161  ConvertedRecord->Data.MX.pNameExchange = DnsWToC((PWCHAR)QueryResultWide->Data.MX.pNameExchange);
162  ConvertedRecord->Data.MX.wPreference = QueryResultWide->Data.MX.wPreference;
163  break;
164 
165  case DNS_TYPE_HINFO:
166  ConvertedRecord = RtlAllocateHeap(RtlGetProcessHeap(), 0, sizeof(DNS_TXT_DATA) + QueryResultWide->Data.TXT.dwStringCount);
167  ConvertedRecord->pName = DnsWToC((PWCHAR)QueryResultWide->pName);
168  ConvertedRecord->wType = QueryResultWide->wType;
169  ConvertedRecord->wDataLength = sizeof(DNS_TXT_DATA) + (sizeof(PCHAR) * QueryResultWide->Data.TXT.dwStringCount);
170  ConvertedRecord->Data.TXT.dwStringCount = QueryResultWide->Data.TXT.dwStringCount;
171 
172  for (i = 0; i < ConvertedRecord->Data.TXT.dwStringCount; i++)
173  ConvertedRecord->Data.TXT.pStringArray[i] = DnsWToC((PWCHAR)QueryResultWide->Data.TXT.pStringArray[i]);
174 
175  break;
176 
177  case DNS_TYPE_NULL:
178  ConvertedRecord = RtlAllocateHeap(RtlGetProcessHeap(), 0, sizeof(DNS_NULL_DATA) + QueryResultWide->Data.Null.dwByteCount);
179  ConvertedRecord->pName = DnsWToC((PWCHAR)QueryResultWide->pName);
180  ConvertedRecord->wType = QueryResultWide->wType;
181  ConvertedRecord->wDataLength = sizeof(DNS_NULL_DATA) + QueryResultWide->Data.Null.dwByteCount;
182  ConvertedRecord->Data.Null.dwByteCount = QueryResultWide->Data.Null.dwByteCount;
183  memcpy(&ConvertedRecord->Data.Null.Data, &QueryResultWide->Data.Null.Data, QueryResultWide->Data.Null.dwByteCount);
184  break;
185  }
186 
187  if (LastRecord)
188  {
189  LastRecord->pNext = ConvertedRecord;
190  LastRecord = LastRecord->pNext;
191  }
192  else
193  {
194  LastRecord = *QueryResultSet = ConvertedRecord;
195  }
196 
197  QueryResultWide = QueryResultWide->pNext;
198  }
199 
200  if (LastRecord)
201  LastRecord->pNext = 0;
202 
203  /* The name */
204  RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer);
205  /* The result*/
206  if (QueryResultWide) DnsIntFreeRecordList(QueryResultWide);
207 
208  return Status;
209 }
#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
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:603
void DnsIntFreeRecordList(PDNS_RECORD ToDelete)
Definition: query.c:914
#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 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:585
#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 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
DNS_STATUS WINAPI DnsQuery_W(LPCWSTR Name, WORD Type, DWORD Options, PIP4_ARRAY Servers, PDNS_RECORD *QueryResultSet, PVOID *Reserved)
Definition: query.c:593
#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

Referenced by TestHostName().

◆ DnsQuery_UTF8()

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

Definition at line 902 of file query.c.

908 {
910  return ERROR_OUTOFMEMORY;
911 }
#define UNIMPLEMENTED
Definition: debug.h:114
#define ERROR_OUTOFMEMORY
Definition: deptool.c:13

◆ DnsQuery_W()

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

Definition at line 593 of file query.c.

599 {
600  adns_state astate;
601  int quflags = (Options & DNS_QUERY_NO_RECURSION) == 0 ? adns_qf_search : 0;
602  int adns_error;
603  adns_answer *answer;
604  LPSTR CurrentName;
605  unsigned i, CNameLoop;
606  PFIXED_INFO network_info;
607  ULONG network_info_blen = 0;
608  DWORD network_info_result;
609  PIP_ADDR_STRING pip;
611  struct in_addr addr;
612  PCHAR HostWithDomainName;
613  PCHAR AnsiName;
614  size_t NameLen = 0;
615 
616  if (Name == NULL)
618  if (QueryResultSet == NULL)
622 
623  *QueryResultSet = 0;
624 
625  switch (Type)
626  {
627  case DNS_TYPE_A:
628  /* FIXME: how much instead of MAX_PATH? */
629  NameLen = WideCharToMultiByte(CP_ACP,
630  0,
631  Name,
632  -1,
633  NULL,
634  0,
635  NULL,
636  0);
637  AnsiName = RtlAllocateHeap(RtlGetProcessHeap(), 0, NameLen);
638  if (NULL == AnsiName)
639  {
640  return ERROR_OUTOFMEMORY;
641  }
643  0,
644  Name,
645  -1,
646  AnsiName,
647  NameLen,
648  NULL,
649  0);
650  NameLen--;
651  /* Is it an IPv4 address? */
652  if (ParseV4Address(AnsiName, &Address))
653  {
654  RtlFreeHeap(RtlGetProcessHeap(), 0, AnsiName);
655  *QueryResultSet = (PDNS_RECORD)RtlAllocateHeap(RtlGetProcessHeap(), 0, sizeof(DNS_RECORD));
656 
657  if (NULL == *QueryResultSet)
658  {
659  return ERROR_OUTOFMEMORY;
660  }
661 
662  (*QueryResultSet)->pNext = NULL;
663  (*QueryResultSet)->wType = Type;
664  (*QueryResultSet)->wDataLength = sizeof(DNS_A_DATA);
665  (*QueryResultSet)->Data.A.IpAddress = Address;
666 
667  (*QueryResultSet)->pName = (LPSTR)xstrsave(Name);
668 
669  return (*QueryResultSet)->pName ? ERROR_SUCCESS : ERROR_OUTOFMEMORY;
670  }
671 
672  /* Check allowed characters
673  * According to RFC a-z,A-Z,0-9,-,_, but can't start or end with - or _
674  */
675  if (AnsiName[0] == '-' || AnsiName[0] == '_' || AnsiName[NameLen - 1] == '-' ||
676  AnsiName[NameLen - 1] == '_' || strstr(AnsiName, "..") != NULL)
677  {
678  RtlFreeHeap(RtlGetProcessHeap(), 0, AnsiName);
679  return ERROR_INVALID_NAME;
680  }
681  i = 0;
682  while (i < NameLen)
683  {
684  if (!((AnsiName[i] >= 'a' && AnsiName[i] <= 'z') ||
685  (AnsiName[i] >= 'A' && AnsiName[i] <= 'Z') ||
686  (AnsiName[i] >= '0' && AnsiName[i] <= '9') ||
687  AnsiName[i] == '-' || AnsiName[i] == '_' || AnsiName[i] == '.'))
688  {
689  RtlFreeHeap(RtlGetProcessHeap(), 0, AnsiName);
690  return ERROR_INVALID_NAME;
691  }
692  i++;
693  }
694 
695  if ((Options & DNS_QUERY_NO_HOSTS_FILE) == 0)
696  {
697  if ((Address = FindEntryInHosts(AnsiName)) != 0)
698  {
699  RtlFreeHeap(RtlGetProcessHeap(), 0, AnsiName);
700  *QueryResultSet = (PDNS_RECORD)RtlAllocateHeap(RtlGetProcessHeap(), 0, sizeof(DNS_RECORD));
701 
702  if (NULL == *QueryResultSet)
703  {
704  return ERROR_OUTOFMEMORY;
705  }
706 
707  (*QueryResultSet)->pNext = NULL;
708  (*QueryResultSet)->wType = Type;
709  (*QueryResultSet)->wDataLength = sizeof(DNS_A_DATA);
710  (*QueryResultSet)->Data.A.IpAddress = Address;
711 
712  (*QueryResultSet)->pName = (LPSTR)xstrsave(Name);
713 
714  return (*QueryResultSet)->pName ? ERROR_SUCCESS : ERROR_OUTOFMEMORY;
715  }
716  }
717 
718  network_info_result = GetNetworkParams(NULL, &network_info_blen);
719  network_info = (PFIXED_INFO)RtlAllocateHeap(RtlGetProcessHeap(), 0, (size_t)network_info_blen);
720  if (NULL == network_info)
721  {
722  return ERROR_OUTOFMEMORY;
723  }
724 
725  network_info_result = GetNetworkParams(network_info, &network_info_blen);
726  if (network_info_result != ERROR_SUCCESS)
727  {
728  RtlFreeHeap(RtlGetProcessHeap(), 0, network_info);
729  return network_info_result;
730  }
731 
732  if ((Address = CheckForCurrentHostname(NameLen != 0 ? AnsiName : network_info->HostName, network_info)) != 0)
733  {
734  size_t TempLen = 2, StringLength = 0;
735  RtlFreeHeap(RtlGetProcessHeap(), 0, AnsiName);
736  StringCchLengthA(network_info->HostName, sizeof(network_info->HostName), &StringLength);
737  TempLen += StringLength;
738  StringCchLengthA(network_info->DomainName, sizeof(network_info->DomainName), &StringLength);
739  TempLen += StringLength;
740  HostWithDomainName = (PCHAR)RtlAllocateHeap(RtlGetProcessHeap(), 0, TempLen);
741  StringCchCopyA(HostWithDomainName, TempLen, network_info->HostName);
742  if (network_info->DomainName[0])
743  {
744  StringCchCatA(HostWithDomainName, TempLen, ".");
745  StringCchCatA(HostWithDomainName, TempLen, network_info->DomainName);
746  }
747  RtlFreeHeap(RtlGetProcessHeap(), 0, network_info);
748  *QueryResultSet = (PDNS_RECORD)RtlAllocateHeap(RtlGetProcessHeap(), 0, sizeof(DNS_RECORD));
749 
750  if (NULL == *QueryResultSet)
751  {
752  RtlFreeHeap(RtlGetProcessHeap(), 0, HostWithDomainName);
753  return ERROR_OUTOFMEMORY;
754  }
755 
756  (*QueryResultSet)->pNext = NULL;
757  (*QueryResultSet)->wType = Type;
758  (*QueryResultSet)->wDataLength = sizeof(DNS_A_DATA);
759  (*QueryResultSet)->Data.A.IpAddress = Address;
760 
761  (*QueryResultSet)->pName = (LPSTR)DnsCToW(HostWithDomainName);
762 
763  RtlFreeHeap(RtlGetProcessHeap(), 0, HostWithDomainName);
764  return (*QueryResultSet)->pName ? ERROR_SUCCESS : ERROR_OUTOFMEMORY;
765  }
766 
767  if ((Options & DNS_QUERY_NO_WIRE_QUERY) != 0)
768  {
769  RtlFreeHeap(RtlGetProcessHeap(), 0, AnsiName);
770  RtlFreeHeap(RtlGetProcessHeap(), 0, network_info);
771  return ERROR_FILE_NOT_FOUND;
772  }
773 
774  adns_error = adns_init(&astate, adns_if_noenv | adns_if_noerrprint | adns_if_noserverwarn, 0);
775  if (adns_error != adns_s_ok)
776  {
777  RtlFreeHeap(RtlGetProcessHeap(), 0, AnsiName);
778  RtlFreeHeap(RtlGetProcessHeap(), 0, network_info);
779  return DnsIntTranslateAdnsToDNS_STATUS(adns_error);
780  }
781  for (pip = &(network_info->DnsServerList); pip; pip = pip->Next)
782  {
783  addr.s_addr = inet_addr(pip->IpAddress.String);
784  if ((addr.s_addr != INADDR_ANY) && (addr.s_addr != INADDR_NONE))
785  adns_addserver(astate, addr);
786  }
787  if (network_info->DomainName[0])
788  {
789  adns_ccf_search(astate, "LOCALDOMAIN", -1, network_info->DomainName);
790  }
791  RtlFreeHeap(RtlGetProcessHeap(), 0, network_info);
792 
793  if (Servers)
794  {
795  for (i = 0; i < Servers->AddrCount; i++)
796  {
797  adns_addserver(astate, *((struct in_addr *)&Servers->AddrArray[i]));
798  }
799  }
800 
801  if (!adns_numservers(astate))
802  {
803  /* There are no servers to query so bail out */
804  adns_finish(astate);
805  RtlFreeHeap(RtlGetProcessHeap(), 0, AnsiName);
806  return ERROR_FILE_NOT_FOUND;
807  }
808 
809  /*
810  * adns doesn't resolve chained CNAME records (a CNAME which points to
811  * another CNAME pointing to another... pointing to an A record), according
812  * to a mailing list thread the authors believe that chained CNAME records
813  * are invalid and the DNS entries should be fixed. That's a nice academic
814  * standpoint, but there certainly are chained CNAME records out there,
815  * even some fairly major ones (at the time of this writing
816  * download.mozilla.org is a chained CNAME). Everyone else seems to resolve
817  * these fine, so we should too. So we loop here to try to resolve CNAME
818  * chains ourselves. Of course, there must be a limit to protect against
819  * CNAME loops.
820  */
821 
822 #define CNAME_LOOP_MAX 16
823 
824  CurrentName = AnsiName;
825 
826  for (CNameLoop = 0; CNameLoop < CNAME_LOOP_MAX; CNameLoop++)
827  {
828  adns_error = adns_synchronous(astate, CurrentName, adns_r_addr, quflags, &answer);
829 
830  if (adns_error != adns_s_ok)
831  {
832  adns_finish(astate);
833 
834  if (CurrentName != AnsiName)
835  RtlFreeHeap(RtlGetProcessHeap(), 0, CurrentName);
836 
837  RtlFreeHeap(RtlGetProcessHeap(), 0, AnsiName);
838  return DnsIntTranslateAdnsToDNS_STATUS(adns_error);
839  }
840 
841  if (answer && answer->rrs.addr)
842  {
843  if (CurrentName != AnsiName)
844  RtlFreeHeap(RtlGetProcessHeap(), 0, CurrentName);
845 
846  RtlFreeHeap(RtlGetProcessHeap(), 0, AnsiName);
847  *QueryResultSet = (PDNS_RECORD)RtlAllocateHeap(RtlGetProcessHeap(), 0, sizeof(DNS_RECORD));
848 
849  if (NULL == *QueryResultSet)
850  {
851  adns_finish(astate);
852  return ERROR_OUTOFMEMORY;
853  }
854 
855  (*QueryResultSet)->pNext = NULL;
856  (*QueryResultSet)->wType = Type;
857  (*QueryResultSet)->wDataLength = sizeof(DNS_A_DATA);
858  (*QueryResultSet)->Data.A.IpAddress = answer->rrs.addr->addr.inet.sin_addr.s_addr;
859 
860  adns_finish(astate);
861 
862  (*QueryResultSet)->pName = (LPSTR)xstrsave(Name);
863 
864  return (*QueryResultSet)->pName ? ERROR_SUCCESS : ERROR_OUTOFMEMORY;
865  }
866 
867  if (NULL == answer || adns_s_prohibitedcname != answer->status || NULL == answer->cname)
868  {
869  adns_finish(astate);
870 
871  if (CurrentName != AnsiName)
872  RtlFreeHeap(RtlGetProcessHeap(), 0, CurrentName);
873 
874  RtlFreeHeap(RtlGetProcessHeap(), 0, AnsiName);
875  return ERROR_FILE_NOT_FOUND;
876  }
877 
878  if (CurrentName != AnsiName)
879  RtlFreeHeap(RtlGetProcessHeap(), 0, CurrentName);
880 
881  CurrentName = (LPSTR)xstrsaveA(answer->cname);
882 
883  if (!CurrentName)
884  {
885  RtlFreeHeap(RtlGetProcessHeap(), 0, AnsiName);
886  adns_finish(astate);
887  return ERROR_OUTOFMEMORY;
888  }
889  }
890 
891  adns_finish(astate);
892  RtlFreeHeap(RtlGetProcessHeap(), 0, AnsiName);
893  RtlFreeHeap(RtlGetProcessHeap(), 0, CurrentName);
894  return ERROR_FILE_NOT_FOUND;
895 
896  default:
897  return ERROR_OUTOFMEMORY; /* XXX arty: find a better error code. */
898  }
899 }
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:603
#define INADDR_ANY
Definition: StrAddr.c:4
char String[4 *4]
Definition: iptypes.h:42
#define DNS_QUERY_WIRE_ONLY
Definition: windns.h:18
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:212
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:585
__kernel_size_t size_t
Definition: linux.h:237
union adns_answer::@3822 rrs
IP4_ADDRESS CheckForCurrentHostname(CONST CHAR *Name, PFIXED_INFO network_info)
Definition: query.c:341
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:230
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:411
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
union adns_rr_addr::@3821 addr
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:481
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_A(), NSP_GetHostByNameHeapAllocW(), SaBlob_Query(), and TestHostName().

◆ 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:585

Referenced by DnsQuery_A().

◆ FindEntryInHosts()

IP4_ADDRESS FindEntryInHosts ( CONST CHAR name)

Definition at line 481 of file query.c.

482 {
483  BOOL Found = FALSE;
484  HANDLE HostsFile;
485  CHAR HostsDBData[BUFSIZ] = { 0 };
486  PCHAR AddressStr, DnsName = NULL, AddrTerm, NameSt, NextLine, ThisLine, Comment;
487  UINT ValidData = 0;
488  DWORD ReadSize;
489  DWORD Address;
490 
491  /* Open the network database */
492  HostsFile = OpenNetworkDatabase(L"hosts");
493  if (HostsFile == INVALID_HANDLE_VALUE)
494  {
496  return 0;
497  }
498 
499  while (!Found && ReadFile(HostsFile,
500  HostsDBData + ValidData,
501  sizeof(HostsDBData) - ValidData,
502  &ReadSize,
503  NULL))
504  {
505  ValidData += ReadSize;
506  ReadSize = 0;
507  NextLine = ThisLine = HostsDBData;
508 
509  /* Find the beginning of the next line */
510  while ((NextLine < HostsDBData + ValidData) &&
511  (*NextLine != '\r') &&
512  (*NextLine != '\n'))
513  {
514  NextLine++;
515  }
516 
517  /* Zero and skip, so we can treat what we have as a string */
518  if (NextLine > HostsDBData + ValidData)
519  break;
520 
521  *NextLine = 0;
522  NextLine++;
523 
524  Comment = strchr(ThisLine, '#');
525  if (Comment)
526  *Comment = 0; /* Terminate at comment start */
527 
528  AddressStr = ThisLine;
529  /* Find the first space separating the IP address from the DNS name */
530  AddrTerm = strchr(ThisLine, ' ');
531  if (AddrTerm)
532  {
533  /* Terminate the address string */
534  *AddrTerm = 0;
535 
536  /* Find the last space before the DNS name */
537  NameSt = strrchr(ThisLine, ' ');
538 
539  /* If there is only one space (the one we removed above), then just use the address terminator */
540  if (!NameSt)
541  NameSt = AddrTerm;
542 
543  /* Move from the space to the first character of the DNS name */
544  NameSt++;
545 
546  DnsName = NameSt;
547 
548  if (!stricmp(name, DnsName) || !stricmp(name, AddressStr))
549  {
550  Found = TRUE;
551  break;
552  }
553  }
554 
555  /* Get rid of everything we read so far */
556  while (NextLine <= HostsDBData + ValidData &&
557  isspace(*NextLine))
558  {
559  NextLine++;
560  }
561 
562  if (HostsDBData + ValidData - NextLine <= 0)
563  break;
564 
565  memmove(HostsDBData, NextLine, HostsDBData + ValidData - NextLine);
566  ValidData -= NextLine - HostsDBData;
567  }
568 
569  CloseHandle(HostsFile);
570 
571  if (!Found)
572  {
574  return 0;
575  }
576 
577  if (strstr(AddressStr, ":"))
578  {
580  return 0;
581  }
582 
583  if (!ParseV4Address(AddressStr, &Address))
584  {
586  return 0;
587  }
588 
589  return Address;
590 }
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:248
BOOL ParseV4Address(LPCSTR AddressString, OUT PDWORD pAddress)
Definition: query.c:411
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 248 of file query.c.

249 {
250  PWSTR ExpandedPath;
252  INT ErrorCode;
253  HKEY DatabaseKey;
254  DWORD RegType;
255  DWORD RegSize = 0;
256  size_t StringLength;
257  HANDLE ret;
258 
259  ExpandedPath = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
260  if (!ExpandedPath)
261  return INVALID_HANDLE_VALUE;
262 
263  /* Open the database path key */
265  L"System\\CurrentControlSet\\Services\\Tcpip\\Parameters",
266  0,
267  KEY_READ,
268  &DatabaseKey);
269  if (ErrorCode == NO_ERROR)
270  {
271  /* Read the actual path */
272  ErrorCode = RegQueryValueExW(DatabaseKey,
273  L"DatabasePath",
274  NULL,
275  &RegType,
276  NULL,
277  &RegSize);
278 
279  DatabasePath = HeapAlloc(GetProcessHeap(), 0, RegSize);
280  if (!DatabasePath)
281  {
282  HeapFree(GetProcessHeap(), 0, ExpandedPath);
283  return INVALID_HANDLE_VALUE;
284  }
285 
286  /* Read the actual path */
287  ErrorCode = RegQueryValueExW(DatabaseKey,
288  L"DatabasePath",
289  NULL,
290  &RegType,
292  &RegSize);
293 
294  /* Close the key */
295  RegCloseKey(DatabaseKey);
296 
297  /* Expand the name */
299 
301  }
302  else
303  {
304  /* Use defalt path */
305  GetSystemDirectoryW(ExpandedPath, MAX_PATH);
306  StringCchLengthW(ExpandedPath, MAX_PATH, &StringLength);
307  if (ExpandedPath[StringLength - 1] != L'\\')
308  {
309  /* It isn't, so add it ourselves */
310  StringCchCatW(ExpandedPath, MAX_PATH, L"\\");
311  }
312  StringCchCatW(ExpandedPath, MAX_PATH, L"DRIVERS\\ETC\\");
313  }
314 
315  /* Make sure that the path is backslash-terminated */
316  StringCchLengthW(ExpandedPath, MAX_PATH, &StringLength);
317  if (ExpandedPath[StringLength - 1] != L'\\')
318  {
319  /* It isn't, so add it ourselves */
320  StringCchCatW(ExpandedPath, MAX_PATH, L"\\");
321  }
322 
323  /* Add the database name */
324  StringCchCatW(ExpandedPath, MAX_PATH, Name);
325 
326  /* Return a handle to the file */
327  ret = CreateFileW(ExpandedPath,
330  NULL,
333  NULL);
334 
335  HeapFree(GetProcessHeap(), 0, ExpandedPath);
336  return ret;
337 }
_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:4134
__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:3381
#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 411 of file query.c.

413 {
414  CHAR * cp = (CHAR *)AddressString;
415  DWORD val, base;
416  unsigned char c;
417  DWORD parts[4], *pp = parts;
418  if (!AddressString)
419  return FALSE;
420  if (!isdigit(*cp)) return FALSE;
421 
422 again:
423  /*
424  * Collect number up to ``.''.
425  * Values are specified as for C:
426  * 0x=hex, 0=octal, other=decimal.
427  */
428  val = 0; base = 10;
429  if (*cp == '0') {
430  if (*++cp == 'x' || *cp == 'X')
431  base = 16, cp++;
432  else
433  base = 8;
434  }
435  while ((c = *cp)) {
436  if (isdigit(c)) {
437  val = (val * base) + (c - '0');
438  cp++;
439  continue;
440  }
441  if (base == 16 && isxdigit(c)) {
442  val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A'));
443  cp++;
444  continue;
445  }
446  break;
447  }
448  if (*cp == '.') {
449  /*
450  * Internet format:
451  * a.b.c.d
452  */
453  if (pp >= parts + 4) return FALSE;
454  *pp++ = val;
455  cp++;
456  goto again;
457  }
458  /*
459  * Check for trailing characters.
460  */
461  if (*cp && *cp > ' ') return FALSE;
462 
463  if (pp >= parts + 4) return FALSE;
464  *pp++ = val;
465  /*
466  * Concoct the address according to
467  * the number of parts specified.
468  */
469  if ((DWORD)(pp - parts) != 4) return FALSE;
470  if (parts[0] > 0xff || parts[1] > 0xff || parts[2] > 0xff || parts[3] > 0xff) return FALSE;
471  val = (parts[3] << 24) | (parts[2] << 16) | (parts[1] << 8) | parts[0];
472 
473  if (pAddress)
474  *pAddress = val;
475 
476  return TRUE;
477 }
#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:61
#define c
Definition: ke_i.h:80

Referenced by DnsQuery_W(), and FindEntryInHosts().

◆ xstrsave()

WCHAR* xstrsave ( const WCHAR str)

Definition at line 212 of file query.c.

213 {
214  WCHAR *p;
215  size_t len = 0;
216 
217  /* FIXME: how much instead of MAX_PATH? */
219  len+=sizeof(WCHAR);
220 
221  p = RtlAllocateHeap(RtlGetProcessHeap(), 0, len);
222 
223  if (p)
224  StringCbCopyW(p, len, str);
225 
226  return p;
227 }
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:585
__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 230 of file query.c.

231 {
232  CHAR *p;
233  size_t len = 0;
234 
235  /* FIXME: how much instead of MAX_PATH? */
237  len++;
238 
239  p = RtlAllocateHeap(RtlGetProcessHeap(), 0, len);
240 
241  if (p)
242  StringCbCopyA(p, len, str);
243 
244  return p;
245 }
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:585
#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().