xref: /freebsd/usr.sbin/nscd/protocol.c (revision ea906c4152774dff300bb26fbfc1e4188351c89a)
1 /*-
2  * Copyright (c) 2005 Michael Bushkov <bushman@rsu.ru>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <assert.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include "debug.h"
35 #include "log.h"
36 #include "protocol.h"
37 
38 /*
39  * Initializes the comm_element with any given type of data
40  */
41 void
42 init_comm_element(struct comm_element *element, enum comm_element_t type)
43 {
44 
45 	TRACE_IN(init_comm_element);
46 	memset(element, 0, sizeof(struct comm_element));
47 
48 	switch (type) {
49 	case CET_WRITE_REQUEST:
50 		init_cache_write_request(&element->c_write_request);
51 		break;
52 	case CET_WRITE_RESPONSE:
53 		init_cache_write_response(&element->c_write_response);
54 		break;
55 	case CET_READ_REQUEST:
56 		init_cache_read_request(&element->c_read_request);
57 		break;
58 	case CET_READ_RESPONSE:
59 		init_cache_read_response(&element->c_read_response);
60 		break;
61 	case CET_TRANSFORM_REQUEST:
62 		init_cache_transform_request(&element->c_transform_request);
63 		break;
64 	case CET_TRANSFORM_RESPONSE:
65 		init_cache_transform_response(&element->c_transform_response);
66 		break;
67 	case CET_MP_WRITE_SESSION_REQUEST:
68 		init_cache_mp_write_session_request(&element->c_mp_ws_request);
69 		break;
70 	case CET_MP_WRITE_SESSION_RESPONSE:
71 		init_cache_mp_write_session_response(&element->c_mp_ws_response);
72 		break;
73 	case CET_MP_WRITE_SESSION_WRITE_REQUEST:
74 		init_cache_mp_write_session_write_request(
75 			&element->c_mp_ws_write_request);
76 		break;
77 	case CET_MP_WRITE_SESSION_WRITE_RESPONSE:
78 		init_cache_mp_write_session_write_response(
79 			&element->c_mp_ws_write_response);
80 		break;
81 	case CET_MP_READ_SESSION_REQUEST:
82 		init_cache_mp_read_session_request(&element->c_mp_rs_request);
83 		break;
84 	case CET_MP_READ_SESSION_RESPONSE:
85 		init_cache_mp_read_session_response(&element->c_mp_rs_response);
86 		break;
87 	case CET_MP_READ_SESSION_READ_RESPONSE:
88 		init_cache_mp_read_session_read_response(
89 			&element->c_mp_rs_read_response);
90 		break;
91 	case CET_UNDEFINED:
92 		break;
93 	default:
94 		LOG_ERR_2("init_comm_element", "invalid communication element");
95 		TRACE_OUT(init_comm_element);
96 	return;
97 	}
98 
99 	element->type = type;
100 	TRACE_OUT(init_comm_element);
101 }
102 
103 void
104 finalize_comm_element(struct comm_element *element)
105 {
106 
107 	TRACE_IN(finalize_comm_element);
108 	switch (element->type) {
109 	case CET_WRITE_REQUEST:
110 		finalize_cache_write_request(&element->c_write_request);
111 		break;
112 	case CET_WRITE_RESPONSE:
113 		finalize_cache_write_response(&element->c_write_response);
114 		break;
115 	case CET_READ_REQUEST:
116 		finalize_cache_read_request(&element->c_read_request);
117 		break;
118 	case CET_READ_RESPONSE:
119 		finalize_cache_read_response(&element->c_read_response);
120 		break;
121 	case CET_TRANSFORM_REQUEST:
122 		finalize_cache_transform_request(&element->c_transform_request);
123 		break;
124 	case CET_TRANSFORM_RESPONSE:
125 		finalize_cache_transform_response(
126 			&element->c_transform_response);
127 		break;
128 	case CET_MP_WRITE_SESSION_REQUEST:
129 		finalize_cache_mp_write_session_request(
130 			&element->c_mp_ws_request);
131 		break;
132 	case CET_MP_WRITE_SESSION_RESPONSE:
133 		finalize_cache_mp_write_session_response(
134 			&element->c_mp_ws_response);
135 		break;
136 	case CET_MP_WRITE_SESSION_WRITE_REQUEST:
137 		finalize_cache_mp_write_session_write_request(
138 			&element->c_mp_ws_write_request);
139 		break;
140 	case CET_MP_WRITE_SESSION_WRITE_RESPONSE:
141 		finalize_cache_mp_write_session_write_response(
142 			&element->c_mp_ws_write_response);
143 		break;
144 	case CET_MP_READ_SESSION_REQUEST:
145 		finalize_cache_mp_read_session_request(
146 			&element->c_mp_rs_request);
147 		break;
148 	case CET_MP_READ_SESSION_RESPONSE:
149 		finalize_cache_mp_read_session_response(
150 			&element->c_mp_rs_response);
151 		break;
152 	case CET_MP_READ_SESSION_READ_RESPONSE:
153 		finalize_cache_mp_read_session_read_response(
154 			&element->c_mp_rs_read_response);
155 		break;
156 	case CET_UNDEFINED:
157 		break;
158 	default:
159 	break;
160 	}
161 
162 	element->type = CET_UNDEFINED;
163 	TRACE_OUT(finalize_comm_element);
164 }
165 
166 void
167 init_cache_write_request(struct cache_write_request *write_request)
168 {
169 
170 	TRACE_IN(init_cache_write_request);
171 	memset(write_request, 0, sizeof(struct cache_write_request));
172 	TRACE_OUT(init_cache_write_request);
173 }
174 
175 void
176 finalize_cache_write_request(struct cache_write_request *write_request)
177 {
178 
179 	TRACE_IN(finalize_cache_write_request);
180 	free(write_request->entry);
181 	free(write_request->cache_key);
182 	free(write_request->data);
183 	TRACE_OUT(finalize_cache_write_request);
184 }
185 
186 struct cache_write_request *
187 get_cache_write_request(struct comm_element *element)
188 {
189 
190 	TRACE_IN(get_cache_write_request);
191 	assert(element->type == CET_WRITE_REQUEST);
192 	TRACE_OUT(get_cache_write_request);
193 	return (&element->c_write_request);
194 }
195 
196 void
197 init_cache_write_response(struct cache_write_response *write_response)
198 {
199 
200 	TRACE_IN(init_cache_write_response);
201 	memset(write_response, 0, sizeof(struct cache_write_response));
202 	TRACE_OUT(init_cache_write_response);
203 }
204 
205 void
206 finalize_cache_write_response(struct cache_write_response *write_response)
207 {
208 
209 	TRACE_IN(finalize_cache_write_response);
210 	TRACE_OUT(finalize_cache_write_response);
211 }
212 
213 struct cache_write_response *
214 get_cache_write_response(struct comm_element *element)
215 {
216 
217 	TRACE_IN(get_cache_write_response);
218 	assert(element->type == CET_WRITE_RESPONSE);
219 	TRACE_OUT(get_cache_write_response);
220 	return (&element->c_write_response);
221 }
222 
223 void
224 init_cache_read_request(struct cache_read_request *read_request)
225 {
226 
227 	TRACE_IN(init_cache_read_request);
228 	memset(read_request, 0, sizeof(struct cache_read_request));
229 	TRACE_OUT(init_cache_read_request);
230 }
231 
232 void
233 finalize_cache_read_request(struct cache_read_request *read_request)
234 {
235 
236 	TRACE_IN(finalize_cache_read_request);
237 	free(read_request->entry);
238 	free(read_request->cache_key);
239 	TRACE_OUT(finalize_cache_read_request);
240 }
241 
242 struct cache_read_request *
243 get_cache_read_request(struct comm_element *element)
244 {
245 
246 	TRACE_IN(get_cache_read_request);
247 	assert(element->type == CET_READ_REQUEST);
248 	TRACE_OUT(get_cache_read_request);
249 	return (&element->c_read_request);
250 }
251 
252 void
253 init_cache_read_response(struct cache_read_response *read_response)
254 {
255 
256 	TRACE_IN(init_cache_read_response);
257 	memset(read_response, 0, sizeof(struct cache_read_response));
258 	TRACE_OUT(init_cache_read_response);
259 }
260 
261 void
262 finalize_cache_read_response(struct cache_read_response *read_response)
263 {
264 
265 	TRACE_IN(finalize_cache_read_response);
266 	free(read_response->data);
267 	TRACE_OUT(finalize_cache_read_response);
268 }
269 
270 struct cache_read_response *
271 get_cache_read_response(struct comm_element *element)
272 {
273 
274 	TRACE_IN(get_cache_read_response);
275 	assert(element->type == CET_READ_RESPONSE);
276 	TRACE_OUT(get_cache_read_response);
277 	return (&element->c_read_response);
278 }
279 
280 void
281 init_cache_transform_request(struct cache_transform_request *transform_request)
282 {
283 
284 	TRACE_IN(init_cache_transform_request);
285 	memset(transform_request, 0, sizeof(struct cache_transform_request));
286 	TRACE_OUT(init_cache_transform_request);
287 }
288 
289 void
290 finalize_cache_transform_request(
291 	struct cache_transform_request *transform_request)
292 {
293 
294 	TRACE_IN(finalize_cache_transform_request);
295 	free(transform_request->entry);
296 	TRACE_OUT(finalize_cache_transform_request);
297 }
298 
299 struct cache_transform_request *
300 get_cache_transform_request(struct comm_element *element)
301 {
302 
303 	TRACE_IN(get_cache_transform_request);
304 	assert(element->type == CET_TRANSFORM_REQUEST);
305 	TRACE_OUT(get_cache_transform_request);
306 	return (&element->c_transform_request);
307 }
308 
309 void
310 init_cache_transform_response(
311 	struct cache_transform_response *transform_response)
312 {
313 
314 	TRACE_IN(init_cache_transform_request);
315 	memset(transform_response, 0, sizeof(struct cache_transform_response));
316 	TRACE_OUT(init_cache_transform_request);
317 }
318 
319 void
320 finalize_cache_transform_response(
321 	struct cache_transform_response *transform_response)
322 {
323 
324 	TRACE_IN(finalize_cache_transform_response);
325 	TRACE_OUT(finalize_cache_transform_response);
326 }
327 
328 struct cache_transform_response *
329 get_cache_transform_response(struct comm_element *element)
330 {
331 
332 	TRACE_IN(get_cache_transform_response);
333 	assert(element->type == CET_TRANSFORM_RESPONSE);
334 	TRACE_OUT(get_cache_transform_response);
335 	return (&element->c_transform_response);
336 }
337 
338 
339 void
340 init_cache_mp_write_session_request(
341 	struct cache_mp_write_session_request *mp_ws_request)
342 {
343 
344 	TRACE_IN(init_cache_mp_write_session_request);
345 	memset(mp_ws_request, 0,
346     		sizeof(struct cache_mp_write_session_request));
347 	TRACE_OUT(init_cache_mp_write_session_request);
348 }
349 
350 void
351 finalize_cache_mp_write_session_request(
352 	struct cache_mp_write_session_request *mp_ws_request)
353 {
354 
355 	TRACE_IN(finalize_cache_mp_write_session_request);
356 	free(mp_ws_request->entry);
357 	TRACE_OUT(finalize_cache_mp_write_session_request);
358 }
359 
360 struct cache_mp_write_session_request *
361 get_cache_mp_write_session_request(struct comm_element *element)
362 {
363 
364 	TRACE_IN(get_cache_mp_write_session_request);
365 	assert(element->type == CET_MP_WRITE_SESSION_REQUEST);
366 	TRACE_OUT(get_cache_mp_write_session_request);
367 	return (&element->c_mp_ws_request);
368 }
369 
370 void
371 init_cache_mp_write_session_response(
372 	struct cache_mp_write_session_response *mp_ws_response)
373 {
374 
375 	TRACE_IN(init_cache_mp_write_session_response);
376 	memset(mp_ws_response, 0,
377     		sizeof(struct cache_mp_write_session_response));
378 	TRACE_OUT(init_cache_mp_write_session_response);
379 }
380 
381 void
382 finalize_cache_mp_write_session_response(
383 	struct cache_mp_write_session_response *mp_ws_response)
384 {
385 
386 	TRACE_IN(finalize_cache_mp_write_session_response);
387 	TRACE_OUT(finalize_cache_mp_write_session_response);
388 }
389 
390 struct cache_mp_write_session_response *
391 get_cache_mp_write_session_response(struct comm_element *element)
392 {
393 
394 	TRACE_IN(get_cache_mp_write_session_response);
395 	assert(element->type == CET_MP_WRITE_SESSION_RESPONSE);
396 	TRACE_OUT(get_cache_mp_write_session_response);
397 	return (&element->c_mp_ws_response);
398 }
399 
400 void
401 init_cache_mp_write_session_write_request(
402 	struct cache_mp_write_session_write_request *mp_ws_write_request)
403 {
404 
405 	TRACE_IN(init_cache_mp_write_session_write_request);
406 	memset(mp_ws_write_request, 0,
407 		sizeof(struct cache_mp_write_session_write_request));
408 	TRACE_OUT(init_cache_mp_write_session_write_response);
409 }
410 
411 void
412 finalize_cache_mp_write_session_write_request(
413 	struct cache_mp_write_session_write_request *mp_ws_write_request)
414 {
415 
416 	TRACE_IN(finalize_cache_mp_write_session_write_request);
417 	free(mp_ws_write_request->data);
418 	TRACE_OUT(finalize_cache_mp_write_session_write_request);
419 }
420 
421 struct cache_mp_write_session_write_request *
422 get_cache_mp_write_session_write_request(struct comm_element *element)
423 {
424 
425 	TRACE_IN(get_cache_mp_write_session_write_request);
426 	assert(element->type == CET_MP_WRITE_SESSION_WRITE_REQUEST);
427 	TRACE_OUT(get_cache_mp_write_session_write_request);
428 	return (&element->c_mp_ws_write_request);
429 }
430 
431 void
432 init_cache_mp_write_session_write_response(
433 	struct cache_mp_write_session_write_response *mp_ws_write_response)
434 {
435 
436 	TRACE_IN(init_cache_mp_write_session_write_response);
437 	memset(mp_ws_write_response, 0,
438 		sizeof(struct cache_mp_write_session_write_response));
439 	TRACE_OUT(init_cache_mp_write_session_write_response);
440 }
441 
442 void
443 finalize_cache_mp_write_session_write_response(
444 	struct cache_mp_write_session_write_response *mp_ws_write_response)
445 {
446 
447 	TRACE_IN(finalize_cache_mp_write_session_write_response);
448 	TRACE_OUT(finalize_cache_mp_write_session_write_response);
449 }
450 
451 struct cache_mp_write_session_write_response *
452 get_cache_mp_write_session_write_response(struct comm_element *element)
453 {
454 
455 	TRACE_IN(get_cache_mp_write_session_write_response);
456 	assert(element->type == CET_MP_WRITE_SESSION_WRITE_RESPONSE);
457 	TRACE_OUT(get_cache_mp_write_session_write_response);
458 	return (&element->c_mp_ws_write_response);
459 }
460 
461 void
462 init_cache_mp_read_session_request(
463 	struct cache_mp_read_session_request *mp_rs_request)
464 {
465 
466 	TRACE_IN(init_cache_mp_read_session_request);
467 	memset(mp_rs_request, 0, sizeof(struct cache_mp_read_session_request));
468 	TRACE_OUT(init_cache_mp_read_session_request);
469 }
470 
471 void
472 finalize_cache_mp_read_session_request(
473 	struct cache_mp_read_session_request *mp_rs_request)
474 {
475 
476 	TRACE_IN(finalize_cache_mp_read_session_request);
477 	free(mp_rs_request->entry);
478 	TRACE_OUT(finalize_cache_mp_read_session_request);
479 }
480 
481 struct cache_mp_read_session_request *
482 get_cache_mp_read_session_request(struct comm_element *element)
483 {
484 
485 	TRACE_IN(get_cache_mp_read_session_request);
486 	assert(element->type == CET_MP_READ_SESSION_REQUEST);
487 	TRACE_OUT(get_cache_mp_read_session_request);
488 	return (&element->c_mp_rs_request);
489 }
490 
491 void
492 init_cache_mp_read_session_response(
493 	struct cache_mp_read_session_response *mp_rs_response)
494 {
495 
496 	TRACE_IN(init_cache_mp_read_session_response);
497 	memset(mp_rs_response, 0,
498     		sizeof(struct cache_mp_read_session_response));
499 	TRACE_OUT(init_cache_mp_read_session_response);
500 }
501 
502 void
503 finalize_cache_mp_read_session_response(
504 	struct cache_mp_read_session_response *mp_rs_response)
505 {
506 
507 	TRACE_IN(finalize_cache_mp_read_session_response);
508 	TRACE_OUT(finalize_cache_mp_read_session_response);
509 }
510 
511 struct cache_mp_read_session_response *
512 get_cache_mp_read_session_response(struct comm_element *element)
513 {
514 
515 	TRACE_IN(get_cache_mp_read_session_response);
516 	assert(element->type == CET_MP_READ_SESSION_RESPONSE);
517 	TRACE_OUT(get_cache_mp_read_session_response);
518 	return (&element->c_mp_rs_response);
519 }
520 
521 void
522 init_cache_mp_read_session_read_response(
523 	struct cache_mp_read_session_read_response *mp_ws_read_response)
524 {
525 
526 	TRACE_IN(init_cache_mp_read_session_read_response);
527 	memset(mp_ws_read_response, 0,
528 		sizeof(struct cache_mp_read_session_read_response));
529 	TRACE_OUT(init_cache_mp_read_session_read_response);
530 }
531 
532 void
533 finalize_cache_mp_read_session_read_response(
534 	struct cache_mp_read_session_read_response *mp_rs_read_response)
535 {
536 
537 	TRACE_IN(finalize_cache_mp_read_session_read_response);
538 	free(mp_rs_read_response->data);
539 	TRACE_OUT(finalize_cache_mp_read_session_read_response);
540 }
541 
542 struct cache_mp_read_session_read_response *
543 get_cache_mp_read_session_read_response(struct comm_element *element)
544 {
545 
546 	TRACE_IN(get_cache_mp_read_session_read_response);
547 	assert(element->type == CET_MP_READ_SESSION_READ_RESPONSE);
548 	TRACE_OUT(get_cache_mp_read_session_read_response);
549 	return (&element->c_mp_rs_read_response);
550 }
551