1 #ifndef _DNS_GENERATOR_H_
2 #define _DNS_GENERATOR_H_
3 
4 #include <string.h>
5 #include "dnsparser.h"
6 #include "array.h"
7 #include "stringlist.h"
8 #include "common.h"
9 
10 #define SET_16_BIT_U_INT(here, val)	(*(uint16_t *)(here) = htons((uint16_t)(val)))
11 #define SET_32_BIT_U_INT(here, val)	(*(uint32_t *)(here) = htonl((uint32_t)(val)))
12 
13 /* Handle DNS header*/
14 #define DNSSetQueryIdentifier(dns_start, QId)	SET_16_BIT_U_INT((char *)(dns_start), QId)
15 
16 #define DNSSetFlags(dns_start, Flags)			SET_16_BIT_U_INT((char *)(dns_start) + 2, Flags)
17 
18 #define DNSSetQuestionCount(dns_start, QC)		SET_16_BIT_U_INT((char *)(dns_start) + 4, QC)
19 
20 #define DNSSetAnswerCount(dns_start, AnC)		SET_16_BIT_U_INT((char *)(dns_start) + 6, AnC)
21 
22 #define DNSSetNameServerCount(dns_start, ASC)	SET_16_BIT_U_INT((char *)(dns_start) + 8, ASC)
23 
24 #define DNSSetAdditionalCount(dns_start, AdC)	SET_16_BIT_U_INT((char *)(dns_start) + 10, AdC)
25 
26 #define DNSLabelMakePointer(pointer_ptr, location)	(((unsigned char *)(pointer_ptr))[0] = (192 + (location) / 256), ((unsigned char *)(pointer_ptr))[1] = (location) % 256)
27 
28 char *DNSLabelizedName(__inout char *Origin, __in size_t OriginSpaceLength);
29 
30 int DNSCompress(__inout char *DNSBody, __in int DNSBodyLength);
31 
32 /**
33   New Implementation
34 */
35 
36 typedef struct _DnsGenerator DnsGenerator;
37 
38 struct _DnsGenerator {
39     /* private */
40     char *Buffer;
41     int BufferLength;
42     char *Itr;
43 
44     void *NumberOfRecords;
45 
46     /* public */
47     DNSHeader *Header;
48 
49     int (*Length)(DnsGenerator *g);
50 
51     DnsRecordPurpose (*NextPurpose)(DnsGenerator *g);
52     void (*CopyHeader)(DnsGenerator *g,
53                        const char *Source,
54                        BOOL IncludeRecordCounts
55                        );
56     void (*CopyIdentifier)(DnsGenerator *g, uint16_t Value);
57     int (*CopyCName)(DnsGenerator *g, DnsSimpleParserIterator *i);
58     int (*CopyA)(DnsGenerator *g, DnsSimpleParserIterator *i);
59     int (*CopyAAAA)(DnsGenerator *g, DnsSimpleParserIterator *i);
60 
61     int (*Question)(DnsGenerator *g,
62                     const char *Name,
63                     DNSRecordType Type,
64                     DNSRecordClass Klass
65                     );
66 
67     int (*CName)(DnsGenerator *g,
68                  const char *Name,
69                  const char *CName,
70                  int Ttl
71                  );
72 
73     int (*A)(DnsGenerator *g,
74              const char *Name,
75              const char *ip,
76              int Ttl
77              );
78 
79     int (*AAAA)(DnsGenerator *g,
80                 const char *Name,
81                 const char *ip,
82                 int Ttl
83                 );
84 
85     int (*EDns)(DnsGenerator *g, int UdpPayloadSize);
86 
87     int (*RawData)(DnsGenerator *g,
88                    const char *Name,
89                    DNSRecordType Type,
90                    DNSRecordClass Klass,
91                    const char *Data,
92                    int DataLength,
93                    int Ttl
94                    );
95 };
96 
97 int DnsGenerator_Init(DnsGenerator *g,
98                       char *Buffer,
99                       int BufferLength,
100                       const char *CopyFrom,
101                       int SourceLength,
102 
103                       /* Whether to remove every record except question and
104                          answer records. Used when `CopyFrom' is not `NULL'.
105                       */
106                       BOOL Strip
107                       );
108 
109 #endif /* _DNS_GENERATOR_H_ */
110