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

https://bitbucket.org/freebsd/freebsd-head/ · C Header · 234 lines · 113 code · 34 blank · 87 comment · 5 complexity · 852c195140ca5b845a7e8d975767dba1 MD5 · raw file

  1. /*
  2. * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 2000, 2001 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: lwdclient.h,v 1.20 2009/01/17 23:47:42 tbox Exp $ */
  18. #ifndef NAMED_LWDCLIENT_H
  19. #define NAMED_LWDCLIENT_H 1
  20. /*! \file */
  21. #include <isc/event.h>
  22. #include <isc/eventclass.h>
  23. #include <isc/netaddr.h>
  24. #include <isc/sockaddr.h>
  25. #include <isc/types.h>
  26. #include <dns/fixedname.h>
  27. #include <dns/types.h>
  28. #include <lwres/lwres.h>
  29. #include <named/lwsearch.h>
  30. #define LWRD_EVENTCLASS ISC_EVENTCLASS(4242)
  31. #define LWRD_SHUTDOWN (LWRD_EVENTCLASS + 0x0001)
  32. /*% Lightweight Resolver Daemon Client */
  33. struct ns_lwdclient {
  34. isc_sockaddr_t address; /*%< where to reply */
  35. struct in6_pktinfo pktinfo;
  36. isc_boolean_t pktinfo_valid;
  37. ns_lwdclientmgr_t *clientmgr; /*%< our parent */
  38. ISC_LINK(ns_lwdclient_t) link;
  39. unsigned int state;
  40. void *arg; /*%< packet processing state */
  41. /*
  42. * Received data info.
  43. */
  44. unsigned char buffer[LWRES_RECVLENGTH]; /*%< receive buffer */
  45. isc_uint32_t recvlength; /*%< length recv'd */
  46. lwres_lwpacket_t pkt;
  47. /*%
  48. * Send data state. If sendbuf != buffer (that is, the send buffer
  49. * isn't our receive buffer) it will be freed to the lwres_context_t.
  50. */
  51. unsigned char *sendbuf;
  52. isc_uint32_t sendlength;
  53. isc_buffer_t recv_buffer;
  54. /*%
  55. * gabn (get address by name) state info.
  56. */
  57. dns_adbfind_t *find;
  58. dns_adbfind_t *v4find;
  59. dns_adbfind_t *v6find;
  60. unsigned int find_wanted; /*%< Addresses we want */
  61. dns_fixedname_t query_name;
  62. dns_fixedname_t target_name;
  63. ns_lwsearchctx_t searchctx;
  64. lwres_gabnresponse_t gabn;
  65. /*%
  66. * gnba (get name by address) state info.
  67. */
  68. lwres_gnbaresponse_t gnba;
  69. dns_byaddr_t *byaddr;
  70. unsigned int options;
  71. isc_netaddr_t na;
  72. /*%
  73. * grbn (get rrset by name) state info.
  74. *
  75. * Note: this also uses target_name and searchctx.
  76. */
  77. lwres_grbnresponse_t grbn;
  78. dns_lookup_t *lookup;
  79. dns_rdatatype_t rdtype;
  80. /*%
  81. * Alias and address info. This is copied up to the gabn/gnba
  82. * structures eventually.
  83. *
  84. * XXXMLG We can keep all of this in a client since we only service
  85. * three packet types right now. If we started handling more,
  86. * we'd need to use "arg" above and allocate/destroy things.
  87. */
  88. char *aliases[LWRES_MAX_ALIASES];
  89. isc_uint16_t aliaslen[LWRES_MAX_ALIASES];
  90. lwres_addr_t addrs[LWRES_MAX_ADDRS];
  91. };
  92. /*%
  93. * Client states.
  94. *
  95. * _IDLE The client is not doing anything at all.
  96. *
  97. * _RECV The client is waiting for data after issuing a socket recv().
  98. *
  99. * _RECVDONE Data has been received, and is being processed.
  100. *
  101. * _FINDWAIT An adb (or other) request was made that cannot be satisfied
  102. * immediately. An event will wake the client up.
  103. *
  104. * _SEND All data for a response has completed, and a reply was
  105. * sent via a socket send() call.
  106. *
  107. * Badly formatted state table:
  108. *
  109. * IDLE -> RECV when client has a recv() queued.
  110. *
  111. * RECV -> RECVDONE when recvdone event received.
  112. *
  113. * RECVDONE -> SEND if the data for a reply is at hand.
  114. * RECVDONE -> FINDWAIT if more searching is needed, and events will
  115. * eventually wake us up again.
  116. *
  117. * FINDWAIT -> SEND when enough data was received to reply.
  118. *
  119. * SEND -> IDLE when a senddone event was received.
  120. *
  121. * At any time -> IDLE on error. Sometimes this will be -> SEND
  122. * instead, if enough data is on hand to reply with a meaningful
  123. * error.
  124. *
  125. * Packets which are badly formatted may or may not get error returns.
  126. */
  127. #define NS_LWDCLIENT_STATEIDLE 1
  128. #define NS_LWDCLIENT_STATERECV 2
  129. #define NS_LWDCLIENT_STATERECVDONE 3
  130. #define NS_LWDCLIENT_STATEFINDWAIT 4
  131. #define NS_LWDCLIENT_STATESEND 5
  132. #define NS_LWDCLIENT_STATESENDDONE 6
  133. #define NS_LWDCLIENT_ISIDLE(c) \
  134. ((c)->state == NS_LWDCLIENT_STATEIDLE)
  135. #define NS_LWDCLIENT_ISRECV(c) \
  136. ((c)->state == NS_LWDCLIENT_STATERECV)
  137. #define NS_LWDCLIENT_ISRECVDONE(c) \
  138. ((c)->state == NS_LWDCLIENT_STATERECVDONE)
  139. #define NS_LWDCLIENT_ISFINDWAIT(c) \
  140. ((c)->state == NS_LWDCLIENT_STATEFINDWAIT)
  141. #define NS_LWDCLIENT_ISSEND(c) \
  142. ((c)->state == NS_LWDCLIENT_STATESEND)
  143. /*%
  144. * Overall magic test that means we're not idle.
  145. */
  146. #define NS_LWDCLIENT_ISRUNNING(c) (!NS_LWDCLIENT_ISIDLE(c))
  147. #define NS_LWDCLIENT_SETIDLE(c) \
  148. ((c)->state = NS_LWDCLIENT_STATEIDLE)
  149. #define NS_LWDCLIENT_SETRECV(c) \
  150. ((c)->state = NS_LWDCLIENT_STATERECV)
  151. #define NS_LWDCLIENT_SETRECVDONE(c) \
  152. ((c)->state = NS_LWDCLIENT_STATERECVDONE)
  153. #define NS_LWDCLIENT_SETFINDWAIT(c) \
  154. ((c)->state = NS_LWDCLIENT_STATEFINDWAIT)
  155. #define NS_LWDCLIENT_SETSEND(c) \
  156. ((c)->state = NS_LWDCLIENT_STATESEND)
  157. #define NS_LWDCLIENT_SETSENDDONE(c) \
  158. ((c)->state = NS_LWDCLIENT_STATESENDDONE)
  159. /*% lightweight daemon client manager */
  160. struct ns_lwdclientmgr {
  161. ns_lwreslistener_t *listener;
  162. isc_mem_t *mctx;
  163. isc_socket_t *sock; /*%< socket to use */
  164. dns_view_t *view;
  165. lwres_context_t *lwctx; /*%< lightweight proto context */
  166. isc_task_t *task; /*%< owning task */
  167. unsigned int flags;
  168. ISC_LINK(ns_lwdclientmgr_t) link;
  169. ISC_LIST(ns_lwdclient_t) idle; /*%< idle client slots */
  170. ISC_LIST(ns_lwdclient_t) running; /*%< running clients */
  171. };
  172. #define NS_LWDCLIENTMGR_FLAGRECVPENDING 0x00000001
  173. #define NS_LWDCLIENTMGR_FLAGSHUTTINGDOWN 0x00000002
  174. isc_result_t
  175. ns_lwdclientmgr_create(ns_lwreslistener_t *, unsigned int, isc_taskmgr_t *);
  176. void
  177. ns_lwdclient_initialize(ns_lwdclient_t *, ns_lwdclientmgr_t *);
  178. isc_result_t
  179. ns_lwdclient_startrecv(ns_lwdclientmgr_t *);
  180. void
  181. ns_lwdclient_stateidle(ns_lwdclient_t *);
  182. void
  183. ns_lwdclient_recv(isc_task_t *, isc_event_t *);
  184. void
  185. ns_lwdclient_shutdown(isc_task_t *, isc_event_t *);
  186. void
  187. ns_lwdclient_send(isc_task_t *, isc_event_t *);
  188. isc_result_t
  189. ns_lwdclient_sendreply(ns_lwdclient_t *client, isc_region_t *r);
  190. /*
  191. * Processing functions of various types.
  192. */
  193. void ns_lwdclient_processgabn(ns_lwdclient_t *, lwres_buffer_t *);
  194. void ns_lwdclient_processgnba(ns_lwdclient_t *, lwres_buffer_t *);
  195. void ns_lwdclient_processgrbn(ns_lwdclient_t *, lwres_buffer_t *);
  196. void ns_lwdclient_processnoop(ns_lwdclient_t *, lwres_buffer_t *);
  197. void ns_lwdclient_errorpktsend(ns_lwdclient_t *, isc_uint32_t);
  198. void ns_lwdclient_log(int level, const char *format, ...)
  199. ISC_FORMAT_PRINTF(2, 3);
  200. #endif /* NAMED_LWDCLIENT_H */