xref: /reactos/sdk/lib/dnslib/hostent.c (revision 34593d93)
1 /*
2  * COPYRIGHT:   See COPYING in the top level directory
3  * PROJECT:     ReactOS DNS Shared Library
4  * FILE:        lib/dnslib/hostent.c
5  * PURPOSE:     Functions for dealing with Host Entry structures
6  */
7 
8 /* INCLUDES ******************************************************************/
9 #include "precomp.h"
10 
11 /* DATA **********************************************************************/
12 
13 /* FUNCTIONS *****************************************************************/
14 
15 PHOSTENT
16 WINAPI
Hostent_Init(IN PVOID * Buffer,IN WORD AddressFamily,IN ULONG AddressSize,IN ULONG AddressCount,IN ULONG AliasCount)17 Hostent_Init(IN PVOID *Buffer,
18              IN WORD AddressFamily,
19              IN ULONG AddressSize,
20              IN ULONG AddressCount,
21              IN ULONG AliasCount)
22 {
23     PHOSTENT Hostent;
24     ULONG_PTR BufferPosition = (ULONG_PTR)*Buffer;
25 
26     /* Align the hostent on the buffer's 4 byte boundary */
27     BufferPosition += 3 & ~3;
28 
29     /* Set up the basic data */
30     Hostent = (PHOSTENT)BufferPosition;
31     Hostent->h_length = (WORD)AddressSize;
32     Hostent->h_addrtype = AddressFamily;
33 
34     /* Put aliases after Hostent */
35     Hostent->h_aliases = (PCHAR*)((ULONG_PTR)(Hostent + 1) & ~3);
36 
37     /* Zero it out */
38     RtlZeroMemory(Hostent->h_aliases, AliasCount * sizeof(PCHAR));
39 
40     /* Put addresses after aliases */
41     Hostent->h_addr_list = (PCHAR*)
42                            ((ULONG_PTR)Hostent->h_aliases +
43                             (AliasCount * sizeof(PCHAR)) + sizeof(PCHAR));
44 
45     /* Update the location */
46     BufferPosition = (ULONG_PTR)Hostent->h_addr_list +
47                      ((AddressCount * sizeof(PCHAR)) + sizeof(PCHAR));
48 
49     /* Send it back */
50     *Buffer = (PVOID)BufferPosition;
51 
52     /* Return the hostent */
53     return Hostent;
54 }
55 
56 VOID
57 WINAPI
Dns_PtrArrayToOffsetArray(PCHAR * List,ULONG_PTR Base)58 Dns_PtrArrayToOffsetArray(PCHAR *List,
59                           ULONG_PTR Base)
60 {
61     /* Loop every pointer in the list */
62     do
63     {
64         /* Update the pointer */
65         *List = (PCHAR)((ULONG_PTR)*List - Base);
66     } while(*List++);
67 }
68 
69 VOID
70 WINAPI
Hostent_ConvertToOffsets(IN PHOSTENT Hostent)71 Hostent_ConvertToOffsets(IN PHOSTENT Hostent)
72 {
73     /* Do we have a name? */
74     if (Hostent->h_name)
75     {
76         /* Update it */
77         Hostent->h_name -= (ULONG_PTR)Hostent;
78     }
79 
80     /* Do we have aliases? */
81     if (Hostent->h_aliases)
82     {
83         /* Update the pointer */
84         Hostent->h_aliases -= (ULONG_PTR)Hostent;
85 
86         /* Fix them up */
87         Dns_PtrArrayToOffsetArray(Hostent->h_aliases, (ULONG_PTR)Hostent);
88     }
89 
90     /* Do we have addresses? */
91     if (Hostent->h_addr_list)
92     {
93         /* Fix them up */
94         Dns_PtrArrayToOffsetArray(Hostent->h_addr_list, (ULONG_PTR)Hostent);
95     }
96 }
97 
98