client.h 5.9 KB
Newer Older
Bob Halley's avatar
add  
Bob Halley committed
1
/*
Bob Halley's avatar
Bob Halley committed
2
 * Copyright (C) 1999, 2000  Internet Software Consortium.
Bob Halley's avatar
add  
Bob Halley committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */

#ifndef NS_CLIENT_H
#define NS_CLIENT_H 1

21 22 23 24 25 26 27 28 29
/*****
 ***** Module Info
 *****/

/*
 * Client
 *
 * This module defines two objects, ns_client_t and ns_clientmgr_t.
 *
Andreas Gustafsson's avatar
Andreas Gustafsson committed
30 31
 * An ns_client_t object handles incoming DNS requests from clients
 * on a given network interface.
32 33 34
 *
 * Each ns_client_t object can handle only one TCP connection or UDP
 * request at a time.  Therefore, several ns_client_t objects are
Andreas Gustafsson's avatar
Andreas Gustafsson committed
35 36 37
 * typically created to serve each network interface, e.g., one
 * for handling TCP requests and a few (one per CPU) for handling 
 * UDP requests.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
 *
 * Incoming requests are classified as queries, zone transfer
 * requests, update requests, notify requests, etc, and handed off 
 * to the appropriate request handler.  When the request has been
 * fully handled (which can be much later), the ns_client_t must be 
 * notified of this by calling one of the following functions 
 * exactly once in the context of its task:
 *
 *   ns_client_send()	(sending a non-error response)
 *   ns_client_error()	(sending an error response)
 *   ns_client_next()	(sending no response)
 *
 * This will release any resources used by the request and 
 * and allow the ns_client_t to listen for the next request.
 *
 * A ns_clientmgr_t manages a number of ns_client_t objects.
 * New ns_client_t objects are created by calling
55 56
 * ns_clientmgr_createclients(). They are destroyed by
 * destroying their manager.
57 58 59 60 61 62
 */

/***
 *** Imports
 ***/

Bob Halley's avatar
add  
Bob Halley committed
63
#include <isc/types.h>
64
#include <isc/stdtime.h>
Bob Halley's avatar
add  
Bob Halley committed
65
#include <isc/buffer.h>
66
#include <isc/quota.h>
Bob Halley's avatar
add  
Bob Halley committed
67

68
#include <dns/name.h>
Bob Halley's avatar
add  
Bob Halley committed
69
#include <dns/types.h>
Bob Halley's avatar
Bob Halley committed
70
#include <dns/tcpmsg.h>
Bob Halley's avatar
add  
Bob Halley committed
71 72

#include <named/types.h>
Bob Halley's avatar
Bob Halley committed
73
#include <named/query.h>
Bob Halley's avatar
add  
Bob Halley committed
74

75 76 77
/***
 *** Types
 ***/
Bob Halley's avatar
add  
Bob Halley committed
78

79 80
typedef ISC_LIST(ns_client_t) client_list_t;
     
Bob Halley's avatar
add  
Bob Halley committed
81 82
struct ns_client {
	unsigned int			magic;
Bob Halley's avatar
Bob Halley committed
83
	isc_mem_t *			mctx;
Bob Halley's avatar
add  
Bob Halley committed
84
	ns_clientmgr_t *		manager;
85 86 87 88 89
	isc_boolean_t			shuttingdown;
	int				naccepts;
	int				nreads;
	int				nsends;
	int				nwaiting;
Bob Halley's avatar
add  
Bob Halley committed
90 91
	unsigned int			attributes;
	isc_task_t *			task;
92
	dns_view_t *			view;
93
	dns_view_t *			lockview;
Bob Halley's avatar
add  
Bob Halley committed
94 95 96
	dns_dispatch_t *		dispatch;
	dns_dispentry_t *		dispentry;
	dns_dispatchevent_t *		dispevent;
Bob Halley's avatar
Bob Halley committed
97 98 99
	isc_socket_t *			tcplistener;
	isc_socket_t *			tcpsocket;
	dns_tcpmsg_t			tcpmsg;
100
	isc_boolean_t			tcpmsg_valid;
Bob Halley's avatar
add  
Bob Halley committed
101 102
	isc_timer_t *			timer;
	dns_message_t *			message;
103
	unsigned char *			sendbuf;
Bob Halley's avatar
EDNS0  
Bob Halley committed
104
	dns_rdataset_t *		opt;
105
	isc_uint16_t			udpsize;
106
	void				(*next)(ns_client_t *, isc_result_t);
107 108
	void				(*shutdown)(void *arg);
	void 				*shutdown_arg;
Bob Halley's avatar
Bob Halley committed
109
	ns_query_t			query;
110
	isc_stdtime_t			requesttime;
Bob Halley's avatar
Bob Halley committed
111
	isc_stdtime_t			now;
112 113
	dns_name_t			signername; /* [T]SIG key name */
	dns_name_t *			signer; /* NULL if not valid sig */
114
	isc_boolean_t			mortal; /* Die after handling request. */
115 116
	isc_quota_t			*tcpquota;
	isc_quota_t			*recursionquota;
117
	ns_interface_t			*interface;
118 119 120
	ISC_LINK(ns_client_t)		link;
	client_list_t			*list;	/* The list 'link' is part of,
					   or NULL if not on any list. */
Bob Halley's avatar
add  
Bob Halley committed
121 122 123 124 125 126
};

#define NS_CLIENT_MAGIC			0x4E534363U	/* NSCc */
#define NS_CLIENT_VALID(c)		((c) != NULL && \
					 (c)->magic == NS_CLIENT_MAGIC)

Bob Halley's avatar
Bob Halley committed
127
#define NS_CLIENTATTR_TCP		0x01
128
#define NS_CLIENTATTR_RA		0x02 /* Client gets recusive service */
Bob Halley's avatar
Bob Halley committed
129

130 131 132 133
/***
 *** Functions
 ***/

Bob Halley's avatar
add  
Bob Halley committed
134 135 136 137
/*
 * Note!  These ns_client_ routines MUST be called ONLY from the client's
 * task in order to ensure synchronization.
 */
Andreas Gustafsson's avatar
Andreas Gustafsson committed
138

Bob Halley's avatar
add  
Bob Halley committed
139
void
Andreas Gustafsson's avatar
Andreas Gustafsson committed
140 141 142 143 144
ns_client_send(ns_client_t *client);
/*
 * Finish processing the current client request and
 * send client->message as a response.
 */
Bob Halley's avatar
add  
Bob Halley committed
145 146

void
Andreas Gustafsson's avatar
Andreas Gustafsson committed
147 148 149 150 151 152
ns_client_error(ns_client_t *client, isc_result_t result);
/*
 * Finish processing the current client request and return
 * an error response to the client.  The error response
 * will have an RCODE determined by 'result'.
 */
Bob Halley's avatar
add  
Bob Halley committed
153

Bob Halley's avatar
Bob Halley committed
154
void
Andreas Gustafsson's avatar
Andreas Gustafsson committed
155 156 157 158 159
ns_client_next(ns_client_t *client, isc_result_t result);
/*
 * Finish processing the current client request, 
 * return no response to the client.
 */
Bob Halley's avatar
Bob Halley committed
160

161 162 163 164 165 166
isc_boolean_t
ns_client_shuttingdown(ns_client_t *client);
/*
 * Return ISC_TRUE iff the client is currently shutting down.
 */

167 168
void
ns_client_wait(ns_client_t *client);
169
/*
Andreas Gustafsson's avatar
Andreas Gustafsson committed
170 171
 * Increment reference count.  The client object will
 * not be destroyed while the reference count is nonzero.
172
 */
173

174
void
175
ns_client_unwait(ns_client_t *client);
176 177 178 179 180
/*
 * Decrement reference count.
 */

isc_result_t
181
ns_client_replace(ns_client_t *client);
182 183 184 185 186
/*
 * Try to replace the current client with a new one, so that the
 * current one can go off and do some lengthy work without
 * leaving the dispatch/socket without service.
 */
187

Bob Halley's avatar
add  
Bob Halley committed
188 189 190
isc_result_t
ns_clientmgr_create(isc_mem_t *mctx, isc_taskmgr_t *taskmgr,
		    isc_timermgr_t *timermgr, ns_clientmgr_t **managerp);
Andreas Gustafsson's avatar
Andreas Gustafsson committed
191 192 193
/*
 * Create a client manager.
 */
Bob Halley's avatar
add  
Bob Halley committed
194 195 196

void
ns_clientmgr_destroy(ns_clientmgr_t **managerp);
Andreas Gustafsson's avatar
Andreas Gustafsson committed
197 198 199 200
/*
 * Destroy a client manager and all ns_client_t objects
 * managed by it.
 */
Bob Halley's avatar
add  
Bob Halley committed
201 202

isc_result_t
203 204
ns_clientmgr_createclients(ns_clientmgr_t *manager, unsigned int n,
			   ns_interface_t *ifp, isc_boolean_t tcp);
205
/*
206 207 208
 * Create up to 'n' clients listening on interface 'ifp'.
 * If 'tcp' is ISC_TRUE, the clients will listen for TCP connections,
 * otherwise for UDP requests.
209
 */
Bob Halley's avatar
Bob Halley committed
210

211 212
isc_sockaddr_t *
ns_client_getsockaddr(ns_client_t *client);
Andreas Gustafsson's avatar
Andreas Gustafsson committed
213 214 215 216
/*
 * Get the socket address of the client whose request is
 * currently being processed.
 */
217

Bob Halley's avatar
add  
Bob Halley committed
218
#endif /* NS_CLIENT_H */