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