ReactOS 0.4.15-dev-7924-g5949c20
straddr.c
Go to the documentation of this file.
1/*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS DNS Shared Library
4 * FILE: lib/dnslib/straddr.c
5 * PURPOSE: Functions for address<->string conversion.
6 */
7
8/* INCLUDES ******************************************************************/
9#include "precomp.h"
10
11/* DATA **********************************************************************/
12
13/* FUNCTIONS *****************************************************************/
14
19{
20 /* FIXME */
21 return NULL;
22}
23
28{
29 /* Simply append the ARPA string */
30 return Name + (wsprintfW(Name,
31 L"%u.%u.%u.%u.in-addr.arpa.",
32 Address.S_un.S_addr >> 24,
33 Address.S_un.S_addr >> 10,
34 Address.S_un.S_addr >> 8,
35 Address.S_un.S_addr) * sizeof(WCHAR));
36}
37
42{
43 /* FIXME */
44 return FALSE;
45}
46
51{
52 /* FIXME */
53 return FALSE;
54}
55
60{
61 PCHAR Terminator;
63
64 /* Let RTL Do it for us */
66 if (NT_SUCCESS(Status)) return TRUE;
67
68 /* We failed */
69 return FALSE;
70}
71
76{
77 PCHAR Terminator;
79
80 /* Let RTL Do it for us */
82 if (NT_SUCCESS(Status)) return TRUE;
83
84 /* We failed */
85 return FALSE;
86}
87
92{
93 ULONG Addr;
94
95 /* Use inet_addr to convert it... */
96 Addr = inet_addr(Name);
97 if (Addr == -1)
98 {
99 /* Check if it's the wildcard (which is ok...) */
100 if (strcmp("255.255.255.255", Name)) return FALSE;
101 }
102
103 /* If we got here, then we suceeded... return the address */
104 Address->S_un.S_addr = Addr;
105 return TRUE;
106}
107
109WINAPI
111 IN LPWSTR Name)
112{
113 CHAR AnsiName[16];
114 ULONG Size = sizeof(AnsiName);
116
117 /* Make a copy of the name in ANSI */
118 ErrorCode = Dns_StringCopy(&AnsiName,
119 &Size,
120 Name,
121 0,
123 AnsiString);
124 if (ErrorCode)
125 {
126 /* Copy made sucesfully, now convert it */
128 }
129
130 /* Return either 0 bytes copied (failure == false) or conversion status */
131 return ErrorCode;
132}
133
135WINAPI
137 IN LPWSTR Name)
138{
139 CHAR AnsiName[32];
140 ULONG Size = sizeof(AnsiName);
142
143 /* Make a copy of the name in ANSI */
144 ErrorCode = Dns_StringCopy(&AnsiName,
145 &Size,
146 Name,
147 0,
149 AnsiString);
150 if (ErrorCode)
151 {
152 /* Copy made sucesfully, now convert it */
154 }
155
156 /* Return either 0 bytes copied (failure == false) or conversion status */
157 return ErrorCode;
158}
159
161WINAPI
163 IN OUT PULONG AddressSize,
164 IN PVOID AddressName,
166 IN BOOLEAN Unicode,
167 IN BOOLEAN Reverse)
168{
169 DWORD Af = *AddressFamily;
170 ULONG AddrSize = *AddressSize;
171 IN6_ADDR Addr;
172 BOOLEAN Return;
174 CHAR AnsiName[INET6_ADDRSTRLEN + sizeof("ip6.arpa.")];
175 ULONG Size = sizeof(AnsiName);
176
177 /* First check if this is a reverse address string */
178 if (Reverse)
179 {
180 /* Convert it right now to ANSI as an optimization */
181 Dns_StringCopy(AnsiName,
182 &Size,
183 AddressName,
184 0,
186 AnsiString);
187
188 /* Use the ANSI Name instead */
189 AddressName = AnsiName;
190 }
191
192 /*
193 * If the caller doesn't know what the family is, we'll assume IPv4 and
194 * check if we failed or not. If the caller told us it's IPv4, then just
195 * do IPv4...
196 */
197 if ((Af == AF_UNSPEC) || (Af == AF_INET))
198 {
199 /* Now check if the caller gave us the reverse name or not */
200 if (Reverse)
201 {
202 /* Get the Address */
203 Return = Dns_Ip4ReverseNameToAddress_A((PIN_ADDR)&Addr, AddressName);
204 }
205 else
206 {
207 /* Check if the caller gave us unicode or not */
208 if (Unicode)
209 {
210 /* Get the Address */
211 Return = Dns_Ip4StringToAddress_W((PIN_ADDR)&Addr, AddressName);
212 }
213 else
214 {
215 /* Get the Address */
216 Return = Dns_Ip4StringToAddress_A((PIN_ADDR)&Addr, AddressName);
217 }
218 }
219
220 /* Check if we suceeded */
221 if (Return)
222 {
223 /* Save address family */
224 Af = AF_INET;
225
226 /* Check if the address size matches */
227 if (AddrSize < sizeof(IN_ADDR))
228 {
229 /* Invalid match, set error code */
231 }
232 else
233 {
234 /* It matches, save the address! */
235 *(PIN_ADDR)Address = *(PIN_ADDR)&Addr;
236 }
237 }
238 }
239
240 /* If we are here, either AF_INET6 was specified or IPv4 failed */
241 if ((Af == AF_UNSPEC) || (Af == AF_INET6))
242 {
243 /* Now check if the caller gave us the reverse name or not */
244 if (Reverse)
245 {
246 /* Get the Address */
247 Return = Dns_Ip6ReverseNameToAddress_A(&Addr, AddressName);
248 }
249 else
250 {
251 /* Check if the caller gave us unicode or not */
252 if (Unicode)
253 {
254 /* Get the Address */
255 Return = Dns_Ip6StringToAddress_W(&Addr, AddressName);
256 }
257 else
258 {
259 /* Get the Address */
260 Return = Dns_Ip6StringToAddress_A(&Addr, AddressName);
261 }
262 }
263
264 /* Check if we suceeded */
265 if (Return)
266 {
267 /* Save address family */
268 Af = AF_INET6;
269
270 /* Check if the address size matches */
271 if (AddrSize < sizeof(IN6_ADDR))
272 {
273 /* Invalid match, set error code */
275 }
276 else
277 {
278 /* It matches, save the address! */
279 *(PIN6_ADDR)Address = Addr;
280 }
281 }
282 }
283 else if (Af != AF_INET)
284 {
285 /* You're like.. ATM or something? Get outta here! */
286 Af = AF_UNSPEC;
288 }
289
290 /* Set error if we had one */
292
293 /* Return the address family and size */
294 *AddressFamily = Af;
295 *AddressSize = AddrSize;
296
297 /* Return success or failure */
298 return (ErrorCode == ERROR_SUCCESS);
299}
300
302WINAPI
304 IN OUT PULONG AddressSize,
305 IN LPWSTR AddressName,
307{
308 /* Call the common API */
310 AddressSize,
311 AddressName,
313 TRUE,
314 FALSE);
315}
316
318WINAPI
320 IN PVOID AddressName,
322 IN BOOLEAN Unicode,
323 IN BOOLEAN Reverse)
324{
325 IN6_ADDR Addr;
326 BOOLEAN Return;
328 CHAR AnsiName[INET6_ADDRSTRLEN + sizeof("ip6.arpa.")];
329 ULONG Size = sizeof(AnsiName);
330
331 /* First check if this is a reverse address string */
332 if ((Reverse) && (Unicode))
333 {
334 /* Convert it right now to ANSI as an optimization */
335 Dns_StringCopy(AnsiName,
336 &Size,
337 AddressName,
338 0,
340 AnsiString);
341
342 /* Use the ANSI Name instead */
343 AddressName = AnsiName;
344 }
345
346 /*
347 * If the caller doesn't know what the family is, we'll assume IPv4 and
348 * check if we failed or not. If the caller told us it's IPv4, then just
349 * do IPv4...
350 */
352 {
353 /* Now check if the caller gave us the reverse name or not */
354 if (Reverse)
355 {
356 /* Get the Address */
357 Return = Dns_Ip4ReverseNameToAddress_A((PIN_ADDR)&Addr, AddressName);
358 }
359 else
360 {
361 /* Check if the caller gave us unicode or not */
362 if (Unicode)
363 {
364 /* Get the Address */
365 Return = Dns_Ip4StringToAddress_W((PIN_ADDR)&Addr, AddressName);
366 }
367 else
368 {
369 /* Get the Address */
370 Return = Dns_Ip4StringToAddress_A((PIN_ADDR)&Addr, AddressName);
371 }
372 }
373
374 /* Check if we suceeded */
375 if (Return)
376 {
377 /* Build the IPv4 Address */
378 DnsAddr_BuildFromIp4(DnsAddr, *(PIN_ADDR)&Addr, 0);
379
380 /* So we don't go in the code below... */
382 }
383 }
384
385 /* If we are here, either AF_INET6 was specified or IPv4 failed */
387 {
388 /* Now check if the caller gave us the reverse name or not */
389 if (Reverse)
390 {
391 /* Get the Address */
392 Return = Dns_Ip6ReverseNameToAddress_A(&Addr, AddressName);
393 if (Return)
394 {
395 /* Build the IPv6 Address */
396 DnsAddr_BuildFromIp6(DnsAddr, &Addr, 0, 0);
397 }
398 else
399 {
400 goto Quickie;
401 }
402 }
403 else
404 {
405 /* Check if the caller gave us unicode or not */
406 if (Unicode)
407 {
408 /* Get the Address */
410 &DnsAddr->Ip6Address.sin6_addr,
411 &DnsAddr->Ip6Address.sin6_scope_id,
412 &DnsAddr->Ip6Address.sin6_port)))
413 Return = TRUE;
414 else
415 Return = FALSE;
416 }
417 else
418 {
419 /* Get the Address */
421 &DnsAddr->Ip6Address.sin6_addr,
422 &DnsAddr->Ip6Address.sin6_scope_id,
423 &DnsAddr->Ip6Address.sin6_port)))
424 Return = TRUE;
425 else
426 Return = FALSE;
427 }
428 }
429
430 /* Check if we suceeded */
431 if (Return)
432 {
433 /* Finish setting up the structure */
434 DnsAddr->Ip6Address.sin6_family = AF_INET6;
435 DnsAddr->AddressLength = sizeof(SOCKADDR_IN6);
436 }
437 }
438 else if (AddressFamily != AF_INET)
439 {
440 /* You're like.. ATM or something? Get outta here! */
441 RtlZeroMemory(DnsAddr, sizeof(DNS_ADDRESS));
443 }
444
445Quickie:
446 /* Return success or failure */
447 return (ErrorCode == ERROR_SUCCESS);
448}
449
451WINAPI
453 IN LPWSTR Name)
454{
455 /* Call the common API */
456 return Dns_StringToDnsAddrEx(DnsAddr,
457 Name,
458 AF_UNSPEC,
459 TRUE,
460 TRUE);
461}
462
unsigned char BOOLEAN
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
LONG NTSTATUS
Definition: precomp.h:26
#define ERROR_MORE_DATA
Definition: dderror.h:13
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define SetLastError(x)
Definition: compat.h:752
VOID WINAPI DnsAddr_BuildFromIp4(IN PDNS_ADDRESS DnsAddress, IN IN_ADDR Address, IN WORD Port)
Definition: dnsaddr.c:100
VOID WINAPI DnsAddr_BuildFromIp6(IN PDNS_ADDRESS DnsAddress, IN PIN6_ADDR Address, IN ULONG ScopeId, IN WORD Port)
Definition: dnsaddr.c:116
ULONG WINAPI Dns_StringCopy(OUT PVOID Destination, IN OUT PULONG DestinationSize, IN PVOID String, IN ULONG StringSize OPTIONAL, IN DWORD InputType, IN DWORD OutputType)
Definition: string.c:17
@ AnsiString
Definition: dnslib.h:19
#define AF_INET
Definition: tcpip.h:117
unsigned long DWORD
Definition: ntddk_ex.h:95
Status
Definition: gdiplustypes.h:25
struct in6_addr * PIN6_ADDR
struct in_addr * PIN_ADDR
#define inet_addr(cp)
Definition: inet.h:98
_In_ NDIS_ERROR_CODE ErrorCode
Definition: ndis.h:4436
IN PCO_ADDRESS_FAMILY AddressFamily
Definition: ndis.h:1906
NTSYSAPI NTSTATUS NTAPI RtlIpv6StringToAddressW(_In_ PCWSTR String, _Out_ PCWSTR *Terminator, _Out_ struct in6_addr *Addr)
Definition: network.c:806
NTSYSAPI NTSTATUS NTAPI RtlIpv6StringToAddressExW(_In_ PCWSTR AddressString, _Out_ struct in6_addr *Address, _Out_ PULONG ScopeId, _Out_ PUSHORT Port)
Definition: network.c:954
NTSYSAPI NTSTATUS NTAPI RtlIpv6StringToAddressExA(_In_ PCSTR AddressString, _Out_ struct in6_addr *Address, _Out_ PULONG ScopeId, _Out_ PUSHORT Port)
Definition: network.c:777
NTSYSAPI NTSTATUS NTAPI RtlIpv6StringToAddressA(_In_ PCSTR String, _Out_ PCSTR *Terminator, _Out_ struct in6_addr *Addr)
Definition: network.c:745
#define L(x)
Definition: ntvdm.h:50
DWORD * PDWORD
Definition: pedump.c:68
static WCHAR Address[46]
Definition: ping.c:68
BOOLEAN WINAPI Dns_Ip4ReverseNameToAddress_W(OUT PIN_ADDR Address, IN LPWSTR Name)
Definition: straddr.c:136
BOOLEAN WINAPI Dns_Ip6ReverseNameToAddress_A(OUT PIN6_ADDR Address, IN LPSTR Name)
Definition: straddr.c:49
BOOLEAN WINAPI Dns_ReverseNameToDnsAddr_W(OUT PDNS_ADDRESS DnsAddr, IN LPWSTR Name)
Definition: straddr.c:452
LPWSTR WINAPI Dns_Ip4AddressToReverseName_W(OUT LPWSTR Name, IN IN_ADDR Address)
Definition: straddr.c:26
BOOLEAN WINAPI Dns_Ip6StringToAddress_A(OUT PIN6_ADDR Address, IN LPSTR Name)
Definition: straddr.c:58
BOOLEAN WINAPI Dns_Ip4ReverseNameToAddress_A(OUT PIN_ADDR Address, IN LPSTR Name)
Definition: straddr.c:40
BOOLEAN WINAPI Dns_StringToAddressW(OUT PVOID Address, IN OUT PULONG AddressSize, IN LPWSTR AddressName, IN OUT PDWORD AddressFamily)
Definition: straddr.c:303
BOOLEAN WINAPI Dns_Ip6StringToAddress_W(OUT PIN6_ADDR Address, IN LPWSTR Name)
Definition: straddr.c:74
LPWSTR WINAPI Dns_Ip6AddressToReverseName_W(OUT LPWSTR Name, IN IN6_ADDR Address)
Definition: straddr.c:17
BOOLEAN WINAPI Dns_Ip4StringToAddress_A(OUT PIN_ADDR Address, IN LPSTR Name)
Definition: straddr.c:90
BOOLEAN WINAPI Dns_StringToAddressEx(OUT PVOID Address, IN OUT PULONG AddressSize, IN PVOID AddressName, IN OUT PDWORD AddressFamily, IN BOOLEAN Unicode, IN BOOLEAN Reverse)
Definition: straddr.c:162
BOOLEAN WINAPI Dns_Ip4StringToAddress_W(OUT PIN_ADDR Address, IN LPWSTR Name)
Definition: straddr.c:110
BOOLEAN WINAPI Dns_StringToDnsAddrEx(OUT PDNS_ADDRESS DnsAddr, IN PVOID AddressName, IN DWORD AddressFamily, IN BOOLEAN Unicode, IN BOOLEAN Reverse)
Definition: straddr.c:319
Definition: tcpip.h:126
uint32_t * PULONG
Definition: typedefs.h:59
int32_t INT
Definition: typedefs.h:58
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
#define IN
Definition: typedefs.h:39
uint32_t ULONG
Definition: typedefs.h:59
#define OUT
Definition: typedefs.h:40
char * PCHAR
Definition: typedefs.h:51
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
Definition: wdfdevice.h:4533
#define WINAPI
Definition: msvc.h:6
#define WSA_INVALID_PARAMETER
Definition: winsock2.h:619
#define AF_INET6
Definition: winsock.h:369
#define AF_UNSPEC
Definition: winsock.h:344
int WINAPIV wsprintfW(_Out_ LPWSTR, _In_ _Printf_format_string_ LPCWSTR,...)
SOCKADDR_IN6_LH SOCKADDR_IN6
Definition: ws2ipdef.h:199
#define INET6_ADDRSTRLEN
Definition: ws2ipdef.h:132
char * LPSTR
Definition: xmlstorage.h:182
__wchar_t WCHAR
Definition: xmlstorage.h:180
WCHAR * LPWSTR
Definition: xmlstorage.h:184
char CHAR
Definition: xmlstorage.h:175