2020-10-19 13:34:18 +08:00
|
|
|
#ifndef __ACL_RFC1035_INCLUDE_H__
|
|
|
|
#define __ACL_RFC1035_INCLUDE_H__
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2020-10-19 13:34:18 +08:00
|
|
|
#include "../stdlib/acl_define.h"
|
|
|
|
#include "../stdlib/acl_argv.h"
|
|
|
|
|
2014-11-19 00:25:21 +08:00
|
|
|
#ifdef ACL_UNIX
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#endif
|
|
|
|
|
2020-10-18 11:42:55 +08:00
|
|
|
/* RFC1035 - DNS */
|
2020-10-19 13:34:18 +08:00
|
|
|
#define ACL_RFC1035_MAXHOSTNAMESZ 256
|
2014-11-19 00:25:21 +08:00
|
|
|
|
2020-10-19 13:34:18 +08:00
|
|
|
typedef struct ACL_RFC1035_RR {
|
|
|
|
char name[ACL_RFC1035_MAXHOSTNAMESZ];
|
2014-11-19 00:25:21 +08:00
|
|
|
unsigned short type;
|
|
|
|
unsigned short tclass; /* class */
|
|
|
|
unsigned int ttl;
|
|
|
|
unsigned short rdlength;
|
|
|
|
char *rdata;
|
2020-10-19 13:34:18 +08:00
|
|
|
} ACL_RFC1035_RR;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
2020-10-19 13:34:18 +08:00
|
|
|
typedef struct ACL_RFC1035_QUERY {
|
|
|
|
char name[ACL_RFC1035_MAXHOSTNAMESZ];
|
2014-11-19 00:25:21 +08:00
|
|
|
unsigned short qtype;
|
|
|
|
unsigned short qclass;
|
2020-10-19 13:34:18 +08:00
|
|
|
} ACL_RFC1035_QUERY;
|
2020-10-18 11:42:55 +08:00
|
|
|
|
2020-10-19 13:34:18 +08:00
|
|
|
typedef struct ACL_RFC1035_MESSAGE {
|
2020-10-18 11:42:55 +08:00
|
|
|
unsigned short id; /* A 16 bit identifier */
|
|
|
|
unsigned int qr:1; /* This message is a query (0), or a response (1) */
|
|
|
|
unsigned int opcode:4; /* Query kind:
|
|
|
|
* 0: A standard query (QUERY)
|
|
|
|
* 1: An inverse query (IQUERY)
|
|
|
|
* 2: A server status request (STATUS)
|
|
|
|
* 3-15: Reserved for future use
|
|
|
|
*/
|
|
|
|
unsigned int aa:1; /* Response: Authoritative Answer */
|
|
|
|
unsigned int tc:1; /* Response: Specifies that this message was truncated */
|
|
|
|
unsigned int rd:1; /* Query->Response: Recursion Desired, optional */
|
|
|
|
unsigned int ra:1; /* Response: Recursion Available */
|
|
|
|
unsigned int z:4; /* Reserved for future use */
|
|
|
|
unsigned int rcode:4; /* Response code:
|
|
|
|
* 0: No error condition
|
|
|
|
* 1: Query format error
|
|
|
|
* 2: Server failure
|
|
|
|
* 3: Name Error, domain in the query not exist
|
|
|
|
* 4: Not implement in server side
|
|
|
|
* 5: Refused by the server
|
|
|
|
*/
|
|
|
|
unsigned short qdcount; /* the number of entries in the question section */
|
|
|
|
unsigned short ancount; /* the number of resource records in the answer section */
|
|
|
|
unsigned short nscount; /* the number of name server resource records in the authority records */
|
|
|
|
unsigned short arcount; /* the number of resource records in the additional records section */
|
2020-10-19 13:34:18 +08:00
|
|
|
ACL_RFC1035_QUERY *query;
|
|
|
|
ACL_RFC1035_RR *answer;
|
|
|
|
ACL_RFC1035_RR *authority;
|
|
|
|
ACL_RFC1035_RR *additional;
|
|
|
|
} ACL_RFC1035_MESSAGE;
|
2020-10-18 11:42:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get error description with the specifed error number
|
|
|
|
* @param errnum {int}
|
|
|
|
* @return {const char*}
|
|
|
|
*/
|
2020-10-19 13:34:18 +08:00
|
|
|
ACL_API const char *acl_rfc1035_strerror(int errnum);
|
2020-10-18 11:42:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds a message buffer with a QUESTION to lookup A records
|
|
|
|
* for a hostname. Caller must allocate 'buf' which should
|
|
|
|
* probably be at least 512 octets. The 'szp' initially
|
|
|
|
* specifies the size of the buffer, on return it contains
|
|
|
|
* the size of the message (i.e. how much to write).
|
|
|
|
* @param hostname {const char*} the hostname to be resolved
|
|
|
|
* @param buf {char*} the buffer for holding the query content
|
|
|
|
* @param sz {size_t} the buffer's size
|
|
|
|
* @param qid {unsigned short} the unique ID number for thie quering
|
2020-10-19 13:34:18 +08:00
|
|
|
* @param query {ACL_RFC1035_QUERY*} if not null, it's qtype, qclass and
|
2020-10-18 11:42:55 +08:00
|
|
|
* hostname will be set.
|
|
|
|
* @return {size_t} return the size of the query in the buf, 0 return
|
|
|
|
* if some error happened.
|
|
|
|
*/
|
2020-10-19 13:34:18 +08:00
|
|
|
ACL_API size_t acl_rfc1035_build_query4a(const char *hostname, char *buf,
|
|
|
|
size_t sz, unsigned short qid, ACL_RFC1035_QUERY *query);
|
2020-10-18 11:42:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds a message buffer with a QUESTION to lookup AAAA records
|
|
|
|
* for a hostname.
|
|
|
|
* @param hostname {const char*}
|
|
|
|
* @param buf {char*}
|
|
|
|
* @param sz {size_t}
|
|
|
|
* @param qid {unsigned short}
|
2020-10-19 13:34:18 +08:00
|
|
|
* @param query {ACL_RFC1035_QUERY*}
|
2020-10-18 11:42:55 +08:00
|
|
|
* @return {size_t}
|
|
|
|
*/
|
2020-10-19 13:34:18 +08:00
|
|
|
ACL_API size_t acl_rfc1035_build_query4aaaa(const char *hostname, char *buf,
|
|
|
|
size_t sz, unsigned short qid, ACL_RFC1035_QUERY *query);
|
2020-10-18 11:42:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds a message buffer with a QUESTION to lookup MX records
|
|
|
|
* for a hostname.
|
|
|
|
* @param hostname {const char*}
|
|
|
|
* @param buf {char*}
|
|
|
|
* @param sz {size_t}
|
|
|
|
* @param qid {unsigned short}
|
2020-10-19 13:34:18 +08:00
|
|
|
* @param query {ACL_RFC1035_QUERY*}
|
2020-10-18 11:42:55 +08:00
|
|
|
* @return {size_t}
|
|
|
|
*/
|
2020-10-19 13:34:18 +08:00
|
|
|
ACL_API size_t acl_rfc1035_build_query4mx(const char *hostname, char *buf,
|
|
|
|
size_t sz, unsigned short qid, ACL_RFC1035_QUERY *query);
|
2020-10-18 11:42:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds a message buffer with a QUESTION to lookup PTR records
|
|
|
|
* for an address. Caller must allocate 'buf' which should
|
|
|
|
* probably be at least 512 octets. The 'szp' initially
|
|
|
|
* specifies the size of the buffer, on return it contains
|
|
|
|
* the size of the message (i.e. how much to write).
|
|
|
|
* @param addr {const struct in_addr} the addr to resolve for name
|
|
|
|
* @param buf {char*} hold the query package
|
|
|
|
* @param sz {size_t} the buf's size at least 512 octets
|
|
|
|
* @param qid {unsigned short} the query ID
|
2020-10-19 13:34:18 +08:00
|
|
|
* @param query {ACL_RFC1035_QUERY*} if no null, it's some member variable
|
2020-10-18 11:42:55 +08:00
|
|
|
* @return {size_t} Returns the size of the query in the buff
|
|
|
|
*/
|
2020-10-19 13:34:18 +08:00
|
|
|
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);
|
2020-10-18 11:42:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* We're going to retry a former query, but we
|
|
|
|
* just need a new ID for it. Lucky for us ID
|
|
|
|
* is the first field in the message buffer.
|
|
|
|
* @param buf {char*} hold the result
|
|
|
|
* @param sz {size_t} the buf's size
|
|
|
|
* @param qid {unsigned short} the query ID
|
|
|
|
*/
|
2020-10-19 13:34:18 +08:00
|
|
|
ACL_API void acl_rfc1035_set_query_id(char *buf, size_t sz, unsigned short qid);
|
2020-10-18 11:42:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Compares two RFC1035_QUERY entries
|
2020-10-19 13:34:18 +08:00
|
|
|
* @param a {const ACL_RFC1035_QUERY *}
|
|
|
|
* @param b {const ACL_RFC1035_QUERY *}
|
2020-10-18 11:42:55 +08:00
|
|
|
* @return Returns 0 (equal) or !=0 (different)
|
|
|
|
*/
|
2020-10-19 13:34:18 +08:00
|
|
|
ACL_API int acl_rfc1035_query_compare(const ACL_RFC1035_QUERY *a,
|
|
|
|
const ACL_RFC1035_QUERY *b);
|
2020-10-18 11:42:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes the contents of a DNS reply and fills in an array
|
|
|
|
* of resource record structures. The records array is allocated
|
|
|
|
* here, and should be freed by calling RFC1035RRDestroy().
|
|
|
|
* @param buf {const char*} the data of the DNS reply
|
|
|
|
* @param sz {size_t} the buf's size
|
2020-10-23 15:26:11 +08:00
|
|
|
* @return {ACL_RFC1035_MESSAGE*} return the parsing result
|
2020-10-18 11:42:55 +08:00
|
|
|
*/
|
2020-10-23 15:26:11 +08:00
|
|
|
ACL_API ACL_RFC1035_MESSAGE *acl_rfc1035_response_unpack(const char *buf, size_t sz);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes the contents of a DNS request and fills in an array
|
|
|
|
* of resource record structures. The records array is allocated
|
|
|
|
* here, and should be freed by calling RFC1035RRDestroy().
|
|
|
|
* @param buf {const char*} the data of the DNS reply
|
|
|
|
* @param sz {size_t} the buf's size
|
|
|
|
* @return {ACL_RFC1035_MESSAGE*} return the parsing result
|
|
|
|
*/
|
|
|
|
ACL_API ACL_RFC1035_MESSAGE *acl_rfc1035_request_unpack(const char *buf, size_t sz);
|
2020-10-18 11:42:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* destroy and free the message created by RFC1035MessageUnpack()
|
2020-10-19 13:34:18 +08:00
|
|
|
* @param message {ACL_RFC1035_MESSAGE*}
|
2020-10-18 11:42:55 +08:00
|
|
|
*/
|
2020-10-19 13:34:18 +08:00
|
|
|
ACL_API void acl_rfc1035_message_destroy(ACL_RFC1035_MESSAGE *message);
|
2020-10-18 11:42:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds a response message for the query client.
|
|
|
|
* @param hostname {const char*} the name to be resolved
|
|
|
|
* @param ips {const ACL_ARGV*} the ip list of the hostname
|
|
|
|
* @param domain_root {const char*}
|
|
|
|
* @param dnsname {const char*}
|
|
|
|
* @param dnsip {const char*}
|
|
|
|
* @param qid {unsigned short}
|
|
|
|
* @param buf {char*} hold the result
|
|
|
|
* @param sz {size_t} the buf's size
|
|
|
|
* @return {size_t} content size in buf
|
|
|
|
*/
|
2020-10-19 13:34:18 +08:00
|
|
|
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);
|
2020-10-18 11:42:55 +08:00
|
|
|
|
2020-10-19 13:34:18 +08:00
|
|
|
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);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
2020-10-19 13:34:18 +08:00
|
|
|
typedef struct ACL_RFC1035_REPLY {
|
2020-10-18 11:42:55 +08:00
|
|
|
const char *hostname;
|
|
|
|
const ACL_ARGV *ips;
|
|
|
|
const char *domain_root;
|
|
|
|
const char *dns_name;
|
|
|
|
const char *dns_ip;
|
2020-10-19 13:34:18 +08:00
|
|
|
int ip_type; /* RFC1035_TYPE_A, RFC1035_TYPE_AAAA */
|
2020-10-18 11:42:55 +08:00
|
|
|
int ttl;
|
|
|
|
unsigned short qid;
|
2020-10-19 13:34:18 +08:00
|
|
|
} ACL_RFC1035_REPLY;
|
|
|
|
|
|
|
|
ACL_API size_t acl_rfc1035_build_reply(const ACL_RFC1035_REPLY *reply,
|
|
|
|
char *buf, size_t sz);
|
|
|
|
|
|
|
|
#define ACL_RFC1035_TYPE_A 1 /* a host address */
|
|
|
|
#define ACL_RFC1035_TYPE_NS 2 /* an authoritative name server */
|
|
|
|
#define ACL_RFC1035_TYPE_MD 3 /* a mail destination (Obsolete - use MX) */
|
|
|
|
#define ACL_RFC1035_TYPE_MF 4 /* a mail forwarder (Obsolete - use MX) */
|
|
|
|
#define ACL_RFC1035_TYPE_CNAME 5 /* the canonical name for an alias */
|
|
|
|
#define ACL_RFC1035_TYPE_SOA 6 /* marks the start of a zone of authority */
|
|
|
|
#define ACL_RFC1035_TYPE_MB 7 /* a mailbox domain name (EXPERIMENTAL) */
|
|
|
|
#define ACL_RFC1035_TYPE_MG 8 /* a mail group member (EXPERIMENTAL) */
|
|
|
|
#define ACL_RFC1035_TYPE_MR 9 /* a mail rename domain name (EXPERIMENTAL) */
|
|
|
|
#define ACL_RFC1035_TYPE_NULL 10 /* a null RR (EXPERIMENTAL) */
|
|
|
|
#define ACL_RFC1035_TYPE_WKS 11 /* a well known service description */
|
|
|
|
#define ACL_RFC1035_TYPE_PTR 12 /* a domain name pointer */
|
|
|
|
#define ACL_RFC1035_TYPE_HINFO 13 /* host information */
|
|
|
|
#define ACL_RFC1035_TYPE_MINFO 14 /* mailbox or mail list information */
|
|
|
|
#define ACL_RFC1035_TYPE_MX 15 /* mail exchange */
|
|
|
|
#define ACL_RFC1035_TYPE_TXT 16 /* text strings */
|
|
|
|
#define ACL_RFC1035_TYPE_AAAA 28 /* a IPv6 address of host */
|
|
|
|
#define ACL_RFC1035_TYPE_AXFR 252 /* a request for a transfer of an entire zone */
|
|
|
|
#define ACL_RFC1035_TYPE_MAILB 253 /* a request for mailbox-related records (MB, MG or MR) */
|
|
|
|
#define ACL_RFC1035_TYPE_MAILA 253 /* a request for mail agent RRs (Obsolete - see MX) */
|
|
|
|
#define ACL_RFC1035_TYPE_ALL 255 /* a request for all records */
|
|
|
|
|
|
|
|
#define ACL_RFC1035_CLASS_IN 1 /* the Internet */
|
|
|
|
#define ACL_RFC1035_CLASS_CS 2 /* the CSNET class (Obsolete - used only for examples in some obsolete RFCs */
|
|
|
|
#define ACL_RFC1035_CLASS_CH 3 /* the CHAOS class */
|
|
|
|
#define ACL_RFC1035_CLASS_HS 4 /* Hesiod [Dyer 87] */
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|