Add missing header to ircd_string.h; add debug logging to test scripts.
[ircu2.10.12-pk.git] / include / res.h
1 /** @file
2  * @brief IRC resolver API.
3  * @version $Id$
4  */
5
6 #ifndef INCLUDED_res_h
7 #define INCLUDED_res_h
8
9 #ifndef INCLUDED_config_h
10 #include "config.h"
11 #endif
12
13 #ifndef INCLUDED_sys_types_h
14 #include <sys/types.h>
15 #define INCLUDED_sys_types_h
16 #endif
17
18 #ifndef INCLUDED_sys_socket_h
19 #include <sys/socket.h>
20 #define INCLUDED_sys_socket_h
21 #endif
22
23 #include <netdb.h>
24
25 #ifndef INCLUDED_netinet_in_h
26 #include <netinet/in.h>
27 #define INCLUDED_netinet_in_h
28 #endif
29
30 #ifdef HAVE_STDINT_H
31 #ifndef INCLUDED_stdint_h
32 #include <stdint.h>
33 #define INCLUDED_stdint_h
34 #endif
35 #endif
36
37 struct Client;
38 struct StatDesc;
39
40 /* Here we define some values lifted from nameser.h */
41 #define NS_INT16SZ 2 /**< Size of a 16-bit value. */
42 #define NS_INT32SZ 4 /**< Size of a 32-bit value. */
43 #define NS_CMPRSFLGS 0xc0 /**< Prefix flags that indicate special types */
44 #define NS_MAXCDNAME 255 /**< Maximum length of a compressed domain name. */
45 #define QUERY 0      /**< Forward (normal) DNS query operation. */
46 #define NO_ERRORS 0  /**< No errors processing a query. */
47 #define SERVFAIL 2   /**< Server error while processing a query. */
48 #define T_A 1        /**< Hostname -> IPv4 query type. */
49 #define T_AAAA 28    /**< Hostname -> IPv6 query type. */
50 #define T_PTR 12     /**< IP(v4 or v6) -> hostname query type. */
51 #define T_CNAME 5    /**< Canonical name resolution query type. */
52 #define C_IN 1       /**< Internet query class. */
53 #define QFIXEDSZ 4   /**< Length of fixed-size part of query. */
54 #define HFIXEDSZ 12  /**< Length of fixed-size DNS header. */
55
56 /** Structure to store an IP address. */
57 struct irc_in_addr
58 {
59   unsigned short in6_16[8]; /**< IPv6 encoded parts, little-endian. */
60 };
61
62 /** Structure to store an IP address and port number. */
63 struct irc_sockaddr
64 {
65   struct irc_in_addr addr; /**< IP address. */
66   unsigned short port;     /**< Port number, host-endian. */
67 };
68
69 /** DNS reply structure. */
70 struct DNSReply
71 {
72   char *h_name;   /**< Hostname. */
73   struct irc_in_addr addr; /**< IP address. */
74 };
75
76 /** DNS callback structure. */
77 struct DNSQuery
78 {
79   void *vptr; /**< pointer used by callback to identify request */
80   void (*callback)(void* vptr, struct DNSReply *reply); /**< callback to call */
81 };
82
83 /** DNS query and response header. */
84 typedef struct
85 {
86         unsigned        id :16;         /**< query identification number */
87 #ifdef WORDS_BIGENDIAN
88                         /* fields in third byte */
89         unsigned        qr: 1;          /**< response flag */
90         unsigned        opcode: 4;      /**< purpose of message */
91         unsigned        aa: 1;          /**< authoritive answer */
92         unsigned        tc: 1;          /**< truncated message */
93         unsigned        rd: 1;          /**< recursion desired */
94                         /* fields in fourth byte */
95         unsigned        ra: 1;          /**< recursion available */
96         unsigned        unused :1;      /**< unused bits (MBZ as of 4.9.3a3) */
97         unsigned        ad: 1;          /**< authentic data from named */
98         unsigned        cd: 1;          /**< checking disabled by resolver */
99         unsigned        rcode :4;       /**< response code */
100 #else
101                         /* fields in third byte */
102         unsigned        rd :1;          /**< recursion desired */
103         unsigned        tc :1;          /**< truncated message */
104         unsigned        aa :1;          /**< authoritive answer */
105         unsigned        opcode :4;      /**< purpose of message */
106         unsigned        qr :1;          /**< response flag */
107                         /* fields in fourth byte */
108         unsigned        rcode :4;       /**< response code */
109         unsigned        cd: 1;          /**< checking disabled by resolver */
110         unsigned        ad: 1;          /**< authentic data from named */
111         unsigned        unused :1;      /**< unused bits (MBZ as of 4.9.3a3) */
112         unsigned        ra :1;          /**< recursion available */
113 #endif
114                         /* remaining bytes */
115         unsigned        qdcount :16;    /**< number of question entries */
116         unsigned        ancount :16;    /**< number of answer entries */
117         unsigned        nscount :16;    /**< number of authority entries */
118         unsigned        arcount :16;    /**< number of resource entries */
119 } HEADER;
120
121 extern void restart_resolver(void);
122 extern void add_local_domain(char *hname, size_t size);
123 extern size_t cres_mem(struct Client* cptr);
124 extern void delete_resolver_queries(const void *vptr);
125 extern void report_dns_servers(struct Client *source_p, const struct StatDesc *sd, char *param);
126 extern void gethost_byname(const char *name, const struct DNSQuery *query);
127 extern void gethost_byaddr(const struct irc_in_addr *addr, const struct DNSQuery *query);
128
129 /** Evaluate to non-zero if \a ADDR is a valid address (not all 0s and not all 1s). */
130 #define irc_in_addr_valid(ADDR) (((ADDR)->in6_16[0] && ~(ADDR)->in6_16[0]) \
131                                  || (ADDR)->in6_16[1] != (ADDR)->in6_16[0] \
132                                  || (ADDR)->in6_16[2] != (ADDR)->in6_16[0] \
133                                  || (ADDR)->in6_16[3] != (ADDR)->in6_16[0] \
134                                  || (ADDR)->in6_16[4] != (ADDR)->in6_16[0] \
135                                  || (ADDR)->in6_16[5] != (ADDR)->in6_16[0] \
136                                  || (ADDR)->in6_16[6] != (ADDR)->in6_16[0] \
137                                  || (ADDR)->in6_16[7] != (ADDR)->in6_16[0])
138 /** Evaluate to non-zero if \a ADDR (of type struct irc_in_addr) is an IPv4 address. */
139 #define irc_in_addr_is_ipv4(ADDR) (!(ADDR)->in6_16[0] && !(ADDR)->in6_16[1] && !(ADDR)->in6_16[2] \
140                                    && !(ADDR)->in6_16[3] && !(ADDR)->in6_16[4] && (ADDR)->in6_16[6] \
141                                    && (!(ADDR)->in6_16[5] || (ADDR)->in6_16[5] == 65535))
142 /** Evaluate to non-zero if \a A is a different IP than \a B. */
143 #define irc_in_addr_cmp(A,B) (irc_in_addr_is_ipv4(A) ? ((A)->in6_16[6] != (B)->in6_16[6] \
144                                   || (A)->in6_16[7] != (B)->in6_16[7] || !irc_in_addr_is_ipv4(B)) \
145                               : memcmp((A), (B), sizeof(struct irc_in_addr)))
146 /** Evaluate to non-zero if \a ADDR is a loopback address. */
147 #define irc_in_addr_is_loopback(ADDR) (!(ADDR)->in6_16[0] && !(ADDR)->in6_16[1] && !(ADDR)->in6_16[2] \
148                                        && !(ADDR)->in6_16[3] && !(ADDR)->in6_16[4] \
149                                        && ((!(ADDR)->in6_16[5] \
150                                             && ((!(ADDR)->in6_16[6] && (ADDR)->in6_16[7] == htons(1)) \
151                                                 || (ntohs((ADDR)->in6_16[6]) & 0xff00) == 0x7f00)) \
152                                            || (((ADDR)->in6_16[5] == 65535) \
153                                                && (ntohs((ADDR)->in6_16[6]) & 0xff00) == 0x7f00)))
154
155 #endif