xref: /reactos/sdk/lib/dnslib/dnsaddr.c (revision 1734f297)
1 /*
2  * COPYRIGHT:   See COPYING in the top level directory
3  * PROJECT:     ReactOS DNS Shared Library
4  * FILE:        lib/dnslib/dnsaddr.c
5  * PURPOSE:     Functions dealing with DNS_ADDRESS and DNS_ARRAY addresses.
6  */
7 
8 /* INCLUDES ******************************************************************/
9 #include "precomp.h"
10 
11 /* DATA **********************************************************************/
12 
13 /* FUNCTIONS *****************************************************************/
14 
15 PDNS_ARRAY
16 WINAPI
17 DnsAddrArray_Create(ULONG Count)
18 {
19     PDNS_ARRAY DnsAddrArray;
20 
21     /* Allocate space for the array and the addresses within it */
22     DnsAddrArray = Dns_AllocZero(sizeof(DNS_ARRAY) +
23                                  (Count * sizeof(DNS_ADDRESS)));
24 
25     /* Write the allocated address count */
26     if (DnsAddrArray) DnsAddrArray->AllocatedAddresses = Count;
27 
28     /* Return it */
29     return DnsAddrArray;
30 }
31 
32 VOID
33 WINAPI
34 DnsAddrArray_Free(IN PDNS_ARRAY DnsAddrArray)
35 {
36     /* Just free the entire array */
37     Dns_Free(DnsAddrArray);
38 }
39 
40 BOOL
41 WINAPI
42 DnsAddrArray_AddIp4(IN PDNS_ARRAY DnsAddrArray,
43                     IN IN_ADDR Address,
44                     IN DWORD AddressType)
45 {
46     DNS_ADDRESS DnsAddress;
47 
48     /* Build the DNS Address */
49     DnsAddr_BuildFromIp4(&DnsAddress, Address, 0);
50 
51     /* Add it to the array */
52     return DnsAddrArray_AddAddr(DnsAddrArray, &DnsAddress, 0, AddressType);
53 }
54 
55 BOOL
56 WINAPI
57 DnsAddrArray_AddAddr(IN PDNS_ARRAY DnsAddrArray,
58                      IN PDNS_ADDRESS DnsAddress,
59                      IN WORD AddressFamily OPTIONAL,
60                      IN DWORD AddressType OPTIONAL)
61 {
62     /* Make sure we have an array */
63     if (!DnsAddrArray) return FALSE;
64 
65     /* Check if we should validate the Address Family */
66     if (AddressFamily)
67     {
68         /* Validate it */
69         if (AddressFamily != DnsAddress->AddressFamily) return TRUE;
70     }
71 
72     /* Check if we should validate the Address Type */
73     if (AddressType)
74     {
75         /* Make sure that this array contains this type of addresses */
76         if (!DnsAddrArray_ContainsAddr(DnsAddrArray, DnsAddress, AddressType))
77         {
78             /* Won't be adding it */
79             return TRUE;
80         }
81     }
82 
83     /* Make sure we have space in the array */
84     if (DnsAddrArray->AllocatedAddresses < DnsAddrArray->UsedAddresses)
85     {
86         return FALSE;
87     }
88 
89     /* Now add the address */
90     RtlCopyMemory(&DnsAddrArray->Addresses[DnsAddrArray->UsedAddresses],
91                   DnsAddress,
92                   sizeof(DNS_ADDRESS));
93 
94     /* Return success */
95     return TRUE;
96 }
97 
98 VOID
99 WINAPI
100 DnsAddr_BuildFromIp4(IN PDNS_ADDRESS DnsAddress,
101                      IN IN_ADDR Address,
102                      IN WORD Port)
103 {
104     /* Clear the address */
105     RtlZeroMemory(DnsAddress, sizeof(DNS_ADDRESS));
106 
107     /* Write data */
108     DnsAddress->Ip4Address.sin_family = AF_INET;
109     DnsAddress->Ip4Address.sin_port  = Port;
110     DnsAddress->Ip4Address.sin_addr = Address;
111     DnsAddress->AddressLength = sizeof(SOCKADDR_IN);
112 }
113 
114 VOID
115 WINAPI
116 DnsAddr_BuildFromIp6(IN PDNS_ADDRESS DnsAddress,
117                      IN PIN6_ADDR Address,
118                      IN ULONG ScopeId,
119                      IN WORD Port)
120 {
121     /* Clear the address */
122     RtlZeroMemory(DnsAddress, sizeof(DNS_ADDRESS));
123 
124     /* Write data */
125     DnsAddress->Ip6Address.sin6_family = AF_INET6;
126     DnsAddress->Ip6Address.sin6_port  = Port;
127     DnsAddress->Ip6Address.sin6_addr = *Address;
128     DnsAddress->Ip6Address.sin6_scope_id = ScopeId;
129     DnsAddress->AddressLength = sizeof(SOCKADDR_IN6);
130 }
131 
132 VOID
133 WINAPI
134 DnsAddr_BuildFromAtm(IN PDNS_ADDRESS DnsAddress,
135                      IN DWORD AddressType,
136                      IN PVOID AddressData)
137 {
138     ATM_ADDRESS Address;
139 
140     /* Clear the address */
141     RtlZeroMemory(DnsAddress, sizeof(DNS_ADDRESS));
142 
143     /* Build an ATM Address */
144     Address.AddressType = AddressType;
145     Address.NumofDigits = DNS_ATMA_MAX_ADDR_LENGTH;
146     RtlCopyMemory(&Address.Addr, AddressData, DNS_ATMA_MAX_ADDR_LENGTH);
147 
148     /* Write data */
149     DnsAddress->AtmAddress = Address;
150     DnsAddress->AddressLength = sizeof(ATM_ADDRESS);
151 }
152 
153 BOOLEAN
154 WINAPI
155 DnsAddr_BuildFromDnsRecord(IN PDNS_RECORD DnsRecord,
156                            OUT PDNS_ADDRESS DnsAddr)
157 {
158     /* Check what kind of record this is */
159     switch(DnsRecord->wType)
160     {
161         /* IPv4 */
162         case DNS_TYPE_A:
163             /* Create the DNS Address */
164             DnsAddr_BuildFromIp4(DnsAddr,
165                                  *(PIN_ADDR)&DnsRecord->Data.A.IpAddress,
166                                  0);
167             break;
168 
169         /* IPv6 */
170         case DNS_TYPE_AAAA:
171             /* Create the DNS Address */
172             DnsAddr_BuildFromIp6(DnsAddr,
173                                  (PIN6_ADDR)&DnsRecord->Data.AAAA.Ip6Address,
174                                  DnsRecord->dwReserved,
175                                  0);
176             break;
177 
178         /* ATM */
179         case DNS_TYPE_ATMA:
180             /* Create the DNS Address */
181             DnsAddr_BuildFromAtm(DnsAddr,
182                                  DnsRecord->Data.Atma.AddressType,
183                                  &DnsRecord->Data.Atma.Address);
184             break;
185     }
186 
187     /* Done! */
188     return TRUE;
189 }
190 
191 BOOL
192 WINAPI
193 DnsAddrArray_ContainsAddr(IN PDNS_ARRAY DnsAddrArray,
194                           IN PDNS_ADDRESS DnsAddress,
195                           IN DWORD AddressType)
196 {
197     /* FIXME */
198     return TRUE;
199 }
200 
201