acl  3.5.3.0
acl_rfc1035.h
浏览该文件的文档.
1 #ifndef __ACL_RFC1035_INCLUDE_H__
2 #define __ACL_RFC1035_INCLUDE_H__
3 
4 #ifdef __cplusplus
5 extern "C" {
6 #endif
7 
8 #include "../stdlib/acl_define.h"
9 #include "../stdlib/acl_argv.h"
10 
11 #ifdef ACL_UNIX
12 #include <netinet/in.h>
13 #endif
14 
15 /* RFC1035 - DNS */
16 #define ACL_RFC1035_MAXHOSTNAMESZ 256
17 
18 typedef struct ACL_RFC1035_RR {
20  unsigned short type;
21  unsigned short tclass; /* class */
22  unsigned int ttl;
23  unsigned short rdlength;
24  char *rdata;
26 
27 typedef struct ACL_RFC1035_QUERY {
29  unsigned short qtype;
30  unsigned short qclass;
32 
33 typedef struct ACL_RFC1035_MESSAGE {
34  unsigned short id; /* A 16 bit identifier */
35  unsigned int qr:1; /* This message is a query (0), or a response (1) */
36  unsigned int opcode:4; /* Query kind:
37  * 0: A standard query (QUERY)
38  * 1: An inverse query (IQUERY)
39  * 2: A server status request (STATUS)
40  * 3-15: Reserved for future use
41  */
42  unsigned int aa:1; /* Response: Authoritative Answer */
43  unsigned int tc:1; /* Response: Specifies that this message was truncated */
44  unsigned int rd:1; /* Query->Response: Recursion Desired, optional */
45  unsigned int ra:1; /* Response: Recursion Available */
46  unsigned int z:3; /* Reserved for future use */
47  unsigned int rcode:4; /* Response code:
48  * 0: No error condition
49  * 1: Query format error
50  * 2: Server failure
51  * 3: Name Error, domain in the query not exist
52  * 4: Not implement in server side
53  * 5: Refused by the server
54  */
55  unsigned short qdcount; /* the number of entries in the question section */
56  unsigned short ancount; /* the number of resource records in the answer section */
57  unsigned short nscount; /* the number of name server resource records in the authority records */
58  unsigned short arcount; /* the number of resource records in the additional records section */
64 
65 /**
66  * Get error description with the specifed error number
67  * @param errnum {int}
68  * @return {const char*}
69  */
70 ACL_API const char *acl_rfc1035_strerror(int errnum);
71 
72 /**
73  * Builds a message buffer with a QUESTION to lookup A records
74  * for a hostname. Caller must allocate 'buf' which should
75  * probably be at least 512 octets. The 'szp' initially
76  * specifies the size of the buffer, on return it contains
77  * the size of the message (i.e. how much to write).
78  * @param hostname {const char*} the hostname to be resolved
79  * @param buf {char*} the buffer for holding the query content
80  * @param sz {size_t} the buffer's size
81  * @param qid {unsigned short} the unique ID number for thie quering
82  * @param query {ACL_RFC1035_QUERY*} if not null, it's qtype, qclass and
83  * hostname will be set.
84  * @return {size_t} return the size of the query in the buf, 0 return
85  * if some error happened.
86  */
87 ACL_API size_t acl_rfc1035_build_query4a(const char *hostname, char *buf,
88  size_t sz, unsigned short qid, ACL_RFC1035_QUERY *query);
89 
90 /**
91  * Builds a message buffer with a QUESTION to lookup AAAA records
92  * for a hostname.
93  * @param hostname {const char*}
94  * @param buf {char*}
95  * @param sz {size_t}
96  * @param qid {unsigned short}
97  * @param query {ACL_RFC1035_QUERY*}
98  * @return {size_t}
99  */
100 ACL_API size_t acl_rfc1035_build_query4aaaa(const char *hostname, char *buf,
101  size_t sz, unsigned short qid, ACL_RFC1035_QUERY *query);
102 
103 /**
104  * Builds a message buffer with a QUESTION to lookup MX records
105  * for a hostname.
106  * @param hostname {const char*}
107  * @param buf {char*}
108  * @param sz {size_t}
109  * @param qid {unsigned short}
110  * @param query {ACL_RFC1035_QUERY*}
111  * @return {size_t}
112  */
113 ACL_API size_t acl_rfc1035_build_query4mx(const char *hostname, char *buf,
114  size_t sz, unsigned short qid, ACL_RFC1035_QUERY *query);
115 
116 /**
117  * Builds a message buffer with a QUESTION to lookup PTR records
118  * for an address. Caller must allocate 'buf' which should
119  * probably be at least 512 octets. The 'szp' initially
120  * specifies the size of the buffer, on return it contains
121  * the size of the message (i.e. how much to write).
122  * @param addr {const struct in_addr} the addr to resolve for name
123  * @param buf {char*} hold the query package
124  * @param sz {size_t} the buf's size at least 512 octets
125  * @param qid {unsigned short} the query ID
126  * @param query {ACL_RFC1035_QUERY*} if no null, it's some member variable
127  * @return {size_t} Returns the size of the query in the buff
128 */
129 ACL_API size_t acl_rfc1035_build_query4ptr(const struct in_addr addr, char *buf,
130  size_t sz, unsigned short qid, ACL_RFC1035_QUERY *query);
131 
132 /**
133  * We're going to retry a former query, but we
134  * just need a new ID for it. Lucky for us ID
135  * is the first field in the message buffer.
136  * @param buf {char*} hold the result
137  * @param sz {size_t} the buf's size
138  * @param qid {unsigned short} the query ID
139  */
140 ACL_API void acl_rfc1035_set_query_id(char *buf, size_t sz, unsigned short qid);
141 
142 /**
143  * Compares two RFC1035_QUERY entries
144  * @param a {const ACL_RFC1035_QUERY *}
145  * @param b {const ACL_RFC1035_QUERY *}
146  * @return Returns 0 (equal) or !=0 (different)
147  */
148 ACL_API int acl_rfc1035_query_compare(const ACL_RFC1035_QUERY *a,
149  const ACL_RFC1035_QUERY *b);
150 
151 /**
152  * Takes the contents of a DNS reply and fills in an array
153  * of resource record structures. The records array is allocated
154  * here, and should be freed by calling acl_rfc1035_message_destroy().
155  * @param buf {const char*} the data of the DNS reply
156  * @param sz {size_t} the buf's size
157  * @return {ACL_RFC1035_MESSAGE*} return the parsing result
158  */
159 ACL_API ACL_RFC1035_MESSAGE *acl_rfc1035_response_unpack(const char *buf, size_t sz);
160 
161 /**
162  * Takes the contents of a DNS request and fills in an array
163  * of resource record structures. The records array is allocated
164  * here, and should be freed by calling acl_rfc1035_message_destroy().
165  * @param buf {const char*} the data of the DNS reply
166  * @param sz {size_t} the buf's size
167  * @return {ACL_RFC1035_MESSAGE*} return the parsing result
168  */
169 ACL_API ACL_RFC1035_MESSAGE *acl_rfc1035_request_unpack(const char *buf, size_t sz);
170 
171 /**
172  * destroy and free the message created by RFC1035MessageUnpack()
173  * @param message {ACL_RFC1035_MESSAGE*}
174  */
175 ACL_API void acl_rfc1035_message_destroy(ACL_RFC1035_MESSAGE *message);
176 
177 /**
178  * Builds a response message for the query client.
179  * @param hostname {const char*} the name to be resolved
180  * @param ips {const ACL_ARGV*} the ip list of the hostname
181  * @param domain_root {const char*}
182  * @param dnsname {const char*}
183  * @param dnsip {const char*}
184  * @param qid {unsigned short}
185  * @param buf {char*} hold the result
186  * @param sz {size_t} the buf's size
187  * @return {size_t} content size in buf
188  */
189 ACL_API size_t acl_rfc1035_build_reply4a(const char *hostname, const ACL_ARGV *ips,
190  const char *domain_root, const char *dnsname, const char *dnsip,
191  unsigned short qid, char *buf, size_t sz);
192 
193 ACL_API size_t acl_rfc1035_build_reply4aaaa(const char *hostname, const ACL_ARGV *ips,
194  const char *domain_root, const char *dnsname, const char *dnsip,
195  unsigned short qid, char *buf, size_t sz);
196 
197 typedef struct ACL_RFC1035_REPLY {
198  const char *hostname;
199  const char *cname;
200  const ACL_ARGV *ips;
201  const char *domain_root;
202  const char *dns_name;
203  const char *dns_ip;
204  int ip_type; /* RFC1035_TYPE_A, RFC1035_TYPE_AAAA */
205  int ttl;
206  unsigned short qid;
208 
209 ACL_API size_t acl_rfc1035_build_reply(const ACL_RFC1035_REPLY *reply,
210  char *buf, size_t sz);
211 
212 #define ACL_RFC1035_TYPE_A 1 /* a host address */
213 #define ACL_RFC1035_TYPE_NS 2 /* an authoritative name server */
214 #define ACL_RFC1035_TYPE_MD 3 /* a mail destination (Obsolete - use MX) */
215 #define ACL_RFC1035_TYPE_MF 4 /* a mail forwarder (Obsolete - use MX) */
216 #define ACL_RFC1035_TYPE_CNAME 5 /* the canonical name for an alias */
217 #define ACL_RFC1035_TYPE_SOA 6 /* marks the start of a zone of authority */
218 #define ACL_RFC1035_TYPE_MB 7 /* a mailbox domain name (EXPERIMENTAL) */
219 #define ACL_RFC1035_TYPE_MG 8 /* a mail group member (EXPERIMENTAL) */
220 #define ACL_RFC1035_TYPE_MR 9 /* a mail rename domain name (EXPERIMENTAL) */
221 #define ACL_RFC1035_TYPE_NULL 10 /* a null RR (EXPERIMENTAL) */
222 #define ACL_RFC1035_TYPE_WKS 11 /* a well known service description */
223 #define ACL_RFC1035_TYPE_PTR 12 /* a domain name pointer */
224 #define ACL_RFC1035_TYPE_HINFO 13 /* host information */
225 #define ACL_RFC1035_TYPE_MINFO 14 /* mailbox or mail list information */
226 #define ACL_RFC1035_TYPE_MX 15 /* mail exchange */
227 #define ACL_RFC1035_TYPE_TXT 16 /* text strings */
228 #define ACL_RFC1035_TYPE_AAAA 28 /* a IPv6 address of host */
229 #define ACL_RFC1035_TYPE_AXFR 252 /* a request for a transfer of an entire zone */
230 #define ACL_RFC1035_TYPE_MAILB 253 /* a request for mailbox-related records (MB, MG or MR) */
231 #define ACL_RFC1035_TYPE_MAILA 253 /* a request for mail agent RRs (Obsolete - see MX) */
232 #define ACL_RFC1035_TYPE_ALL 255 /* a request for all records */
233 
234 #define ACL_RFC1035_CLASS_IN 1 /* the Internet */
235 #define ACL_RFC1035_CLASS_CS 2 /* the CSNET class (Obsolete - used only for examples in some obsolete RFCs */
236 #define ACL_RFC1035_CLASS_CH 3 /* the CHAOS class */
237 #define ACL_RFC1035_CLASS_HS 4 /* Hesiod [Dyer 87] */
238 
239 #ifdef __cplusplus
240 }
241 #endif
242 
243 #endif
ACL_API size_t acl_rfc1035_build_query4aaaa(const char *hostname, char *buf, size_t sz, unsigned short qid, ACL_RFC1035_QUERY *query)
unsigned short qtype
Definition: acl_rfc1035.h:29
unsigned int tc
Definition: acl_rfc1035.h:43
struct ACL_RFC1035_QUERY ACL_RFC1035_QUERY
const char * hostname
Definition: acl_rfc1035.h:198
ACL_API ACL_RFC1035_MESSAGE * acl_rfc1035_request_unpack(const char *buf, size_t sz)
ACL_API size_t acl_rfc1035_build_query4ptr(const struct in_addr addr, char *buf, size_t sz, unsigned short qid, ACL_RFC1035_QUERY *query)
unsigned short rdlength
Definition: acl_rfc1035.h:23
unsigned int ra
Definition: acl_rfc1035.h:45
ACL_API ACL_RFC1035_MESSAGE * acl_rfc1035_response_unpack(const char *buf, size_t sz)
ACL_API size_t acl_rfc1035_build_query4mx(const char *hostname, char *buf, size_t sz, unsigned short qid, ACL_RFC1035_QUERY *query)
unsigned short qid
Definition: acl_rfc1035.h:206
ACL_API void acl_rfc1035_message_destroy(ACL_RFC1035_MESSAGE *message)
unsigned int ttl
Definition: acl_rfc1035.h:22
unsigned int opcode
Definition: acl_rfc1035.h:36
unsigned short id
Definition: acl_rfc1035.h:34
unsigned int rcode
Definition: acl_rfc1035.h:47
ACL_API size_t acl_rfc1035_build_reply(const ACL_RFC1035_REPLY *reply, char *buf, size_t sz)
unsigned short qclass
Definition: acl_rfc1035.h:30
const ACL_ARGV * ips
Definition: acl_rfc1035.h:200
ACL_API size_t acl_rfc1035_build_reply4a(const char *hostname, const ACL_ARGV *ips, const char *domain_root, const char *dnsname, const char *dnsip, unsigned short qid, char *buf, size_t sz)
ACL_RFC1035_QUERY * query
Definition: acl_rfc1035.h:59
const char * dns_name
Definition: acl_rfc1035.h:202
unsigned int rd
Definition: acl_rfc1035.h:44
ACL_API void acl_rfc1035_set_query_id(char *buf, size_t sz, unsigned short qid)
const char * domain_root
Definition: acl_rfc1035.h:201
ACL_RFC1035_RR * authority
Definition: acl_rfc1035.h:61
struct ACL_RFC1035_REPLY ACL_RFC1035_REPLY
ACL_API size_t acl_rfc1035_build_reply4aaaa(const char *hostname, const ACL_ARGV *ips, const char *domain_root, const char *dnsname, const char *dnsip, unsigned short qid, char *buf, size_t sz)
unsigned int z
Definition: acl_rfc1035.h:46
struct ACL_RFC1035_RR ACL_RFC1035_RR
unsigned short ancount
Definition: acl_rfc1035.h:56
unsigned short qdcount
Definition: acl_rfc1035.h:55
#define ACL_RFC1035_MAXHOSTNAMESZ
Definition: acl_rfc1035.h:16
unsigned short tclass
Definition: acl_rfc1035.h:21
unsigned int qr
Definition: acl_rfc1035.h:35
unsigned short nscount
Definition: acl_rfc1035.h:57
unsigned short arcount
Definition: acl_rfc1035.h:58
ACL_API size_t acl_rfc1035_build_query4a(const char *hostname, char *buf, size_t sz, unsigned short qid, ACL_RFC1035_QUERY *query)
char name[ACL_RFC1035_MAXHOSTNAMESZ]
Definition: acl_rfc1035.h:19
ACL_RFC1035_RR * answer
Definition: acl_rfc1035.h:60
ACL_API const char * acl_rfc1035_strerror(int errnum)
ACL_RFC1035_RR * additional
Definition: acl_rfc1035.h:62
char name[ACL_RFC1035_MAXHOSTNAMESZ]
Definition: acl_rfc1035.h:28
unsigned int aa
Definition: acl_rfc1035.h:42
const char * cname
Definition: acl_rfc1035.h:199
unsigned short type
Definition: acl_rfc1035.h:20
ACL_API int acl_rfc1035_query_compare(const ACL_RFC1035_QUERY *a, const ACL_RFC1035_QUERY *b)
struct ACL_RFC1035_MESSAGE ACL_RFC1035_MESSAGE
const char * dns_ip
Definition: acl_rfc1035.h:203