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