/contrib/bind9/bin/named/include/named/client.h

https://bitbucket.org/freebsd/freebsd-head/ · C Header · 381 lines · 153 code · 42 blank · 186 comment · 0 complexity · 0ac61250e1b1ee8c95a9e51fc88a17e9 MD5 · raw file

  1. /*
  2. * Copyright (C) 2004-2009, 2012 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 1999-2003 Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  10. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  11. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  13. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  14. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. * PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* $Id: client.h,v 1.91.278.2 2012/01/31 23:46:39 tbox Exp $ */
  18. #ifndef NAMED_CLIENT_H
  19. #define NAMED_CLIENT_H 1
  20. /*****
  21. ***** Module Info
  22. *****/
  23. /*! \file
  24. * \brief
  25. * This module defines two objects, ns_client_t and ns_clientmgr_t.
  26. *
  27. * An ns_client_t object handles incoming DNS requests from clients
  28. * on a given network interface.
  29. *
  30. * Each ns_client_t object can handle only one TCP connection or UDP
  31. * request at a time. Therefore, several ns_client_t objects are
  32. * typically created to serve each network interface, e.g., one
  33. * for handling TCP requests and a few (one per CPU) for handling
  34. * UDP requests.
  35. *
  36. * Incoming requests are classified as queries, zone transfer
  37. * requests, update requests, notify requests, etc, and handed off
  38. * to the appropriate request handler. When the request has been
  39. * fully handled (which can be much later), the ns_client_t must be
  40. * notified of this by calling one of the following functions
  41. * exactly once in the context of its task:
  42. * \code
  43. * ns_client_send() (sending a non-error response)
  44. * ns_client_sendraw() (sending a raw response)
  45. * ns_client_error() (sending an error response)
  46. * ns_client_next() (sending no response)
  47. *\endcode
  48. * This will release any resources used by the request and
  49. * and allow the ns_client_t to listen for the next request.
  50. *
  51. * A ns_clientmgr_t manages a number of ns_client_t objects.
  52. * New ns_client_t objects are created by calling
  53. * ns_clientmgr_createclients(). They are destroyed by
  54. * destroying their manager.
  55. */
  56. /***
  57. *** Imports
  58. ***/
  59. #include <isc/buffer.h>
  60. #include <isc/magic.h>
  61. #include <isc/stdtime.h>
  62. #include <isc/quota.h>
  63. #include <dns/fixedname.h>
  64. #include <dns/name.h>
  65. #include <dns/rdataclass.h>
  66. #include <dns/rdatatype.h>
  67. #include <dns/tcpmsg.h>
  68. #include <dns/types.h>
  69. #include <named/types.h>
  70. #include <named/query.h>
  71. /***
  72. *** Types
  73. ***/
  74. typedef ISC_LIST(ns_client_t) client_list_t;
  75. /*% nameserver client structure */
  76. struct ns_client {
  77. unsigned int magic;
  78. isc_mem_t * mctx;
  79. ns_clientmgr_t * manager;
  80. int state;
  81. int newstate;
  82. int naccepts;
  83. int nreads;
  84. int nsends;
  85. int nrecvs;
  86. int nupdates;
  87. int nctls;
  88. int references;
  89. isc_boolean_t needshutdown; /*
  90. * Used by clienttest to get
  91. * the client to go from
  92. * inactive to free state
  93. * by shutting down the
  94. * client's task.
  95. */
  96. unsigned int attributes;
  97. isc_task_t * task;
  98. dns_view_t * view;
  99. dns_dispatch_t * dispatch;
  100. isc_socket_t * udpsocket;
  101. isc_socket_t * tcplistener;
  102. isc_socket_t * tcpsocket;
  103. unsigned char * tcpbuf;
  104. dns_tcpmsg_t tcpmsg;
  105. isc_boolean_t tcpmsg_valid;
  106. isc_timer_t * timer;
  107. isc_boolean_t timerset;
  108. dns_message_t * message;
  109. isc_socketevent_t * sendevent;
  110. isc_socketevent_t * recvevent;
  111. unsigned char * recvbuf;
  112. dns_rdataset_t * opt;
  113. isc_uint16_t udpsize;
  114. isc_uint16_t extflags;
  115. isc_int16_t ednsversion; /* -1 noedns */
  116. void (*next)(ns_client_t *);
  117. void (*shutdown)(void *arg, isc_result_t result);
  118. void *shutdown_arg;
  119. ns_query_t query;
  120. isc_stdtime_t requesttime;
  121. isc_stdtime_t now;
  122. dns_name_t signername; /*%< [T]SIG key name */
  123. dns_name_t * signer; /*%< NULL if not valid sig */
  124. isc_boolean_t mortal; /*%< Die after handling request */
  125. isc_quota_t *tcpquota;
  126. isc_quota_t *recursionquota;
  127. ns_interface_t *interface;
  128. isc_sockaddr_t peeraddr;
  129. isc_boolean_t peeraddr_valid;
  130. isc_netaddr_t destaddr;
  131. struct in6_pktinfo pktinfo;
  132. isc_event_t ctlevent;
  133. #ifdef ALLOW_FILTER_AAAA_ON_V4
  134. dns_v4_aaaa_t filter_aaaa;
  135. #endif
  136. /*%
  137. * Information about recent FORMERR response(s), for
  138. * FORMERR loop avoidance. This is separate for each
  139. * client object rather than global only to avoid
  140. * the need for locking.
  141. */
  142. struct {
  143. isc_sockaddr_t addr;
  144. isc_stdtime_t time;
  145. dns_messageid_t id;
  146. } formerrcache;
  147. ISC_LINK(ns_client_t) link;
  148. /*%
  149. * The list 'link' is part of, or NULL if not on any list.
  150. */
  151. client_list_t *list;
  152. };
  153. #define NS_CLIENT_MAGIC ISC_MAGIC('N','S','C','c')
  154. #define NS_CLIENT_VALID(c) ISC_MAGIC_VALID(c, NS_CLIENT_MAGIC)
  155. #define NS_CLIENTATTR_TCP 0x01
  156. #define NS_CLIENTATTR_RA 0x02 /*%< Client gets recursive service */
  157. #define NS_CLIENTATTR_PKTINFO 0x04 /*%< pktinfo is valid */
  158. #define NS_CLIENTATTR_MULTICAST 0x08 /*%< recv'd from multicast */
  159. #define NS_CLIENTATTR_WANTDNSSEC 0x10 /*%< include dnssec records */
  160. #define NS_CLIENTATTR_WANTNSID 0x20 /*%< include nameserver ID */
  161. #ifdef ALLOW_FILTER_AAAA_ON_V4
  162. #define NS_CLIENTATTR_FILTER_AAAA 0x40 /*%< suppress AAAAs */
  163. #define NS_CLIENTATTR_FILTER_AAAA_RC 0x80 /*%< recursing for A against AAAA */
  164. #endif
  165. extern unsigned int ns_client_requests;
  166. /***
  167. *** Functions
  168. ***/
  169. /*%
  170. * Note! These ns_client_ routines MUST be called ONLY from the client's
  171. * task in order to ensure synchronization.
  172. */
  173. void
  174. ns_client_send(ns_client_t *client);
  175. /*%
  176. * Finish processing the current client request and
  177. * send client->message as a response.
  178. * \brief
  179. * Note! These ns_client_ routines MUST be called ONLY from the client's
  180. * task in order to ensure synchronization.
  181. */
  182. void
  183. ns_client_sendraw(ns_client_t *client, dns_message_t *msg);
  184. /*%
  185. * Finish processing the current client request and
  186. * send msg as a response using client->message->id for the id.
  187. */
  188. void
  189. ns_client_error(ns_client_t *client, isc_result_t result);
  190. /*%
  191. * Finish processing the current client request and return
  192. * an error response to the client. The error response
  193. * will have an RCODE determined by 'result'.
  194. */
  195. void
  196. ns_client_next(ns_client_t *client, isc_result_t result);
  197. /*%
  198. * Finish processing the current client request,
  199. * return no response to the client.
  200. */
  201. isc_boolean_t
  202. ns_client_shuttingdown(ns_client_t *client);
  203. /*%
  204. * Return ISC_TRUE iff the client is currently shutting down.
  205. */
  206. void
  207. ns_client_attach(ns_client_t *source, ns_client_t **target);
  208. /*%
  209. * Attach '*targetp' to 'source'.
  210. */
  211. void
  212. ns_client_detach(ns_client_t **clientp);
  213. /*%
  214. * Detach '*clientp' from its client.
  215. */
  216. isc_result_t
  217. ns_client_replace(ns_client_t *client);
  218. /*%
  219. * Try to replace the current client with a new one, so that the
  220. * current one can go off and do some lengthy work without
  221. * leaving the dispatch/socket without service.
  222. */
  223. void
  224. ns_client_settimeout(ns_client_t *client, unsigned int seconds);
  225. /*%
  226. * Set a timer in the client to go off in the specified amount of time.
  227. */
  228. isc_result_t
  229. ns_clientmgr_create(isc_mem_t *mctx, isc_taskmgr_t *taskmgr,
  230. isc_timermgr_t *timermgr, ns_clientmgr_t **managerp);
  231. /*%
  232. * Create a client manager.
  233. */
  234. void
  235. ns_clientmgr_destroy(ns_clientmgr_t **managerp);
  236. /*%
  237. * Destroy a client manager and all ns_client_t objects
  238. * managed by it.
  239. */
  240. isc_result_t
  241. ns_clientmgr_createclients(ns_clientmgr_t *manager, unsigned int n,
  242. ns_interface_t *ifp, isc_boolean_t tcp);
  243. /*%
  244. * Create up to 'n' clients listening on interface 'ifp'.
  245. * If 'tcp' is ISC_TRUE, the clients will listen for TCP connections,
  246. * otherwise for UDP requests.
  247. */
  248. isc_sockaddr_t *
  249. ns_client_getsockaddr(ns_client_t *client);
  250. /*%
  251. * Get the socket address of the client whose request is
  252. * currently being processed.
  253. */
  254. isc_result_t
  255. ns_client_checkaclsilent(ns_client_t *client, isc_netaddr_t *netaddr,
  256. dns_acl_t *acl, isc_boolean_t default_allow);
  257. /*%
  258. * Convenience function for client request ACL checking.
  259. *
  260. * Check the current client request against 'acl'. If 'acl'
  261. * is NULL, allow the request iff 'default_allow' is ISC_TRUE.
  262. * If netaddr is NULL, check the ACL against client->peeraddr;
  263. * otherwise check it against netaddr.
  264. *
  265. * Notes:
  266. *\li This is appropriate for checking allow-update,
  267. * allow-query, allow-transfer, etc. It is not appropriate
  268. * for checking the blackhole list because we treat positive
  269. * matches as "allow" and negative matches as "deny"; in
  270. * the case of the blackhole list this would be backwards.
  271. *
  272. * Requires:
  273. *\li 'client' points to a valid client.
  274. *\li 'netaddr' points to a valid address, or is NULL.
  275. *\li 'acl' points to a valid ACL, or is NULL.
  276. *
  277. * Returns:
  278. *\li ISC_R_SUCCESS if the request should be allowed
  279. * \li DNS_R_REFUSED if the request should be denied
  280. *\li No other return values are possible.
  281. */
  282. isc_result_t
  283. ns_client_checkacl(ns_client_t *client,
  284. isc_sockaddr_t *sockaddr,
  285. const char *opname, dns_acl_t *acl,
  286. isc_boolean_t default_allow,
  287. int log_level);
  288. /*%
  289. * Like ns_client_checkaclsilent, except the outcome of the check is
  290. * logged at log level 'log_level' if denied, and at debug 3 if approved.
  291. * Log messages will refer to the request as an 'opname' request.
  292. *
  293. * Requires:
  294. *\li 'client' points to a valid client.
  295. *\li 'sockaddr' points to a valid address, or is NULL.
  296. *\li 'acl' points to a valid ACL, or is NULL.
  297. *\li 'opname' points to a null-terminated string.
  298. */
  299. void
  300. ns_client_log(ns_client_t *client, isc_logcategory_t *category,
  301. isc_logmodule_t *module, int level,
  302. const char *fmt, ...) ISC_FORMAT_PRINTF(5, 6);
  303. void
  304. ns_client_logv(ns_client_t *client, isc_logcategory_t *category,
  305. isc_logmodule_t *module, int level, const char *fmt, va_list ap) ISC_FORMAT_PRINTF(5, 0);
  306. void
  307. ns_client_aclmsg(const char *msg, dns_name_t *name, dns_rdatatype_t type,
  308. dns_rdataclass_t rdclass, char *buf, size_t len);
  309. #define NS_CLIENT_ACLMSGSIZE(x) \
  310. (DNS_NAME_FORMATSIZE + DNS_RDATATYPE_FORMATSIZE + \
  311. DNS_RDATACLASS_FORMATSIZE + sizeof(x) + sizeof("'/'"))
  312. void
  313. ns_client_recursing(ns_client_t *client);
  314. /*%
  315. * Add client to end of th recursing list.
  316. */
  317. void
  318. ns_client_killoldestquery(ns_client_t *client);
  319. /*%
  320. * Kill the oldest recursive query (recursing list head).
  321. */
  322. void
  323. ns_client_dumprecursing(FILE *f, ns_clientmgr_t *manager);
  324. /*%
  325. * Dump the outstanding recursive queries to 'f'.
  326. */
  327. void
  328. ns_client_qnamereplace(ns_client_t *client, dns_name_t *name);
  329. /*%
  330. * Replace the qname.
  331. */
  332. isc_boolean_t
  333. ns_client_isself(dns_view_t *myview, dns_tsigkey_t *mykey,
  334. isc_sockaddr_t *srcaddr, isc_sockaddr_t *destaddr,
  335. dns_rdataclass_t rdclass, void *arg);
  336. /*%
  337. * Isself callback.
  338. */
  339. #endif /* NAMED_CLIENT_H */