2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000, 2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and 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.
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.
18 /* $Id: lwdclient.h,v 1.13.208.1 2004/03/06 10:21:24 marka Exp $ */
20 #ifndef NAMED_LWDCLIENT_H
21 #define NAMED_LWDCLIENT_H 1
23 #include <isc/event.h>
24 #include <isc/eventclass.h>
25 #include <isc/netaddr.h>
26 #include <isc/sockaddr.h>
27 #include <isc/types.h>
29 #include <dns/fixedname.h>
30 #include <dns/types.h>
32 #include <lwres/lwres.h>
34 #include <named/lwsearch.h>
36 #define LWRD_EVENTCLASS ISC_EVENTCLASS(4242)
38 #define LWRD_SHUTDOWN (LWRD_EVENTCLASS + 0x0001)
41 isc_sockaddr_t address; /* where to reply */
42 struct in6_pktinfo pktinfo;
43 isc_boolean_t pktinfo_valid;
44 ns_lwdclientmgr_t *clientmgr; /* our parent */
45 ISC_LINK(ns_lwdclient_t) link;
47 void *arg; /* packet processing state */
52 unsigned char buffer[LWRES_RECVLENGTH]; /* receive buffer */
53 isc_uint32_t recvlength; /* length recv'd */
57 * Send data state. If sendbuf != buffer (that is, the send buffer
58 * isn't our receive buffer) it will be freed to the lwres_context_t.
60 unsigned char *sendbuf;
61 isc_uint32_t sendlength;
62 isc_buffer_t recv_buffer;
65 * gabn (get address by name) state info.
68 dns_adbfind_t *v4find;
69 dns_adbfind_t *v6find;
70 unsigned int find_wanted; /* Addresses we want */
71 dns_fixedname_t query_name;
72 dns_fixedname_t target_name;
73 ns_lwsearchctx_t searchctx;
74 lwres_gabnresponse_t gabn;
77 * gnba (get name by address) state info.
79 lwres_gnbaresponse_t gnba;
85 * grbn (get rrset by name) state info.
87 * Note: this also uses target_name and searchctx.
89 lwres_grbnresponse_t grbn;
91 dns_rdatatype_t rdtype;
94 * Alias and address info. This is copied up to the gabn/gnba
95 * structures eventually.
97 * XXXMLG We can keep all of this in a client since we only service
98 * three packet types right now. If we started handling more,
99 * we'd need to use "arg" above and allocate/destroy things.
101 char *aliases[LWRES_MAX_ALIASES];
102 isc_uint16_t aliaslen[LWRES_MAX_ALIASES];
103 lwres_addr_t addrs[LWRES_MAX_ADDRS];
109 * _IDLE The client is not doing anything at all.
111 * _RECV The client is waiting for data after issuing a socket recv().
113 * _RECVDONE Data has been received, and is being processed.
115 * _FINDWAIT An adb (or other) request was made that cannot be satisfied
116 * immediately. An event will wake the client up.
118 * _SEND All data for a response has completed, and a reply was
119 * sent via a socket send() call.
121 * Badly formatted state table:
123 * IDLE -> RECV when client has a recv() queued.
125 * RECV -> RECVDONE when recvdone event received.
127 * RECVDONE -> SEND if the data for a reply is at hand.
128 * RECVDONE -> FINDWAIT if more searching is needed, and events will
129 * eventually wake us up again.
131 * FINDWAIT -> SEND when enough data was received to reply.
133 * SEND -> IDLE when a senddone event was received.
135 * At any time -> IDLE on error. Sometimes this will be -> SEND
136 * instead, if enough data is on hand to reply with a meaningful
139 * Packets which are badly formatted may or may not get error returns.
141 #define NS_LWDCLIENT_STATEIDLE 1
142 #define NS_LWDCLIENT_STATERECV 2
143 #define NS_LWDCLIENT_STATERECVDONE 3
144 #define NS_LWDCLIENT_STATEFINDWAIT 4
145 #define NS_LWDCLIENT_STATESEND 5
146 #define NS_LWDCLIENT_STATESENDDONE 6
148 #define NS_LWDCLIENT_ISIDLE(c) \
149 ((c)->state == NS_LWDCLIENT_STATEIDLE)
150 #define NS_LWDCLIENT_ISRECV(c) \
151 ((c)->state == NS_LWDCLIENT_STATERECV)
152 #define NS_LWDCLIENT_ISRECVDONE(c) \
153 ((c)->state == NS_LWDCLIENT_STATERECVDONE)
154 #define NS_LWDCLIENT_ISFINDWAIT(c) \
155 ((c)->state == NS_LWDCLIENT_STATEFINDWAIT)
156 #define NS_LWDCLIENT_ISSEND(c) \
157 ((c)->state == NS_LWDCLIENT_STATESEND)
160 * Overall magic test that means we're not idle.
162 #define NS_LWDCLIENT_ISRUNNING(c) (!NS_LWDCLIENT_ISIDLE(c))
164 #define NS_LWDCLIENT_SETIDLE(c) \
165 ((c)->state = NS_LWDCLIENT_STATEIDLE)
166 #define NS_LWDCLIENT_SETRECV(c) \
167 ((c)->state = NS_LWDCLIENT_STATERECV)
168 #define NS_LWDCLIENT_SETRECVDONE(c) \
169 ((c)->state = NS_LWDCLIENT_STATERECVDONE)
170 #define NS_LWDCLIENT_SETFINDWAIT(c) \
171 ((c)->state = NS_LWDCLIENT_STATEFINDWAIT)
172 #define NS_LWDCLIENT_SETSEND(c) \
173 ((c)->state = NS_LWDCLIENT_STATESEND)
174 #define NS_LWDCLIENT_SETSENDDONE(c) \
175 ((c)->state = NS_LWDCLIENT_STATESENDDONE)
177 struct ns_lwdclientmgr {
178 ns_lwreslistener_t *listener;
180 isc_socket_t *sock; /* socket to use */
182 lwres_context_t *lwctx; /* lightweight proto context */
183 isc_task_t *task; /* owning task */
185 ISC_LINK(ns_lwdclientmgr_t) link;
186 ISC_LIST(ns_lwdclient_t) idle; /* idle client slots */
187 ISC_LIST(ns_lwdclient_t) running; /* running clients */
190 #define NS_LWDCLIENTMGR_FLAGRECVPENDING 0x00000001
191 #define NS_LWDCLIENTMGR_FLAGSHUTTINGDOWN 0x00000002
194 ns_lwdclientmgr_create(ns_lwreslistener_t *, unsigned int, isc_taskmgr_t *);
197 ns_lwdclient_initialize(ns_lwdclient_t *, ns_lwdclientmgr_t *);
200 ns_lwdclient_startrecv(ns_lwdclientmgr_t *);
203 ns_lwdclient_stateidle(ns_lwdclient_t *);
206 ns_lwdclient_recv(isc_task_t *, isc_event_t *);
209 ns_lwdclient_shutdown(isc_task_t *, isc_event_t *);
212 ns_lwdclient_send(isc_task_t *, isc_event_t *);
215 ns_lwdclient_sendreply(ns_lwdclient_t *client, isc_region_t *r);
218 * Processing functions of various types.
220 void ns_lwdclient_processgabn(ns_lwdclient_t *, lwres_buffer_t *);
221 void ns_lwdclient_processgnba(ns_lwdclient_t *, lwres_buffer_t *);
222 void ns_lwdclient_processgrbn(ns_lwdclient_t *, lwres_buffer_t *);
223 void ns_lwdclient_processnoop(ns_lwdclient_t *, lwres_buffer_t *);
225 void ns_lwdclient_errorpktsend(ns_lwdclient_t *, isc_uint32_t);
227 void ns_lwdclient_log(int level, const char *format, ...)
228 ISC_FORMAT_PRINTF(2, 3);
230 #endif /* NAMED_LWDCLIENT_H */