2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005 Michael Bushkov <bushman@rsu.ru>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #ifndef __NS_CACHE_H__
32 #define __NS_CACHE_H__
34 #include "nscachedcli.h"
36 typedef int (*nss_cache_id_func_t)(char *, size_t *, va_list, void *);
37 typedef int (*nss_cache_marshal_func_t)(char *, size_t *, void *, va_list,
39 typedef int (*nss_cache_unmarshal_func_t)(char *, size_t, void *, va_list,
42 typedef void (*nss_set_mp_ws_func_t)(cached_mp_write_session);
43 typedef cached_mp_write_session (*nss_get_mp_ws_func_t)(void);
45 typedef void (*nss_set_mp_rs_func_t)(cached_mp_read_session);
46 typedef cached_mp_read_session (*nss_get_mp_rs_func_t)(void);
48 typedef struct _nss_cache_info {
53 * These 3 functions should be implemented specifically for each
56 nss_cache_id_func_t id_func; /* marshals the request parameters */
57 nss_cache_marshal_func_t marshal_func; /* marshals response */
58 nss_cache_unmarshal_func_t unmarshal_func; /* unmarshals response */
61 * These 4 functions should be generated with NSS_MP_CACHE_HANDLING
64 nss_set_mp_ws_func_t set_mp_ws_func; /* sets current write session */
65 nss_get_mp_ws_func_t get_mp_ws_func; /* gets current write session */
67 nss_set_mp_rs_func_t set_mp_rs_func; /* sets current read session */
68 nss_get_mp_rs_func_t get_mp_rs_func; /* gets current read session */
72 * NSS_MP_CACHE_HANDLING implements the set_mp_ws, get_mp_ws, set_mp_rs,
73 * get_mp_rs functions, that are used in _nss_cache_info. It uses
74 * NSS_TLS_HANDLING macro to organize thread local storage.
76 #define NSS_MP_CACHE_HANDLING(name) \
77 struct name##_mp_state { \
78 cached_mp_write_session mp_write_session; \
79 cached_mp_read_session mp_read_session; \
83 name##_mp_endstate(void *s) { \
84 struct name##_mp_state *mp_state; \
86 mp_state = (struct name##_mp_state *)s; \
87 if (mp_state->mp_write_session != INVALID_CACHED_MP_WRITE_SESSION)\
88 __abandon_cached_mp_write_session(mp_state->mp_write_session);\
90 if (mp_state->mp_read_session != INVALID_CACHED_MP_READ_SESSION)\
91 __close_cached_mp_read_session(mp_state->mp_read_session);\
93 NSS_TLS_HANDLING(name##_mp); \
96 name##_set_mp_ws(cached_mp_write_session ws) \
98 struct name##_mp_state *mp_state; \
101 res = name##_mp_getstate(&mp_state); \
105 mp_state->mp_write_session = ws; \
108 static cached_mp_write_session \
109 name##_get_mp_ws(void) \
111 struct name##_mp_state *mp_state; \
114 res = name##_mp_getstate(&mp_state); \
116 return (INVALID_CACHED_MP_WRITE_SESSION); \
118 return (mp_state->mp_write_session); \
122 name##_set_mp_rs(cached_mp_read_session rs) \
124 struct name##_mp_state *mp_state; \
127 res = name##_mp_getstate(&mp_state); \
131 mp_state->mp_read_session = rs; \
134 static cached_mp_read_session \
135 name##_get_mp_rs(void) \
137 struct name##_mp_state *mp_state; \
140 res = name##_mp_getstate(&mp_state); \
142 return (INVALID_CACHED_MP_READ_SESSION); \
144 return (mp_state->mp_read_session); \
148 * These macros should be used to initialize _nss_cache_info structure. For
149 * multipart queries in setXXXent and getXXXent functions mf and uf
150 * (marshal function and unmarshal function) should be both NULL.
152 #define NS_COMMON_CACHE_INFO_INITIALIZER(name, mdata, if, mf, uf) \
153 {#name, mdata, if, mf, uf, NULL, NULL, NULL, NULL}
154 #define NS_MP_CACHE_INFO_INITIALIZER(name, mdata, mf, uf) \
155 {#name, mdata, NULL, mf, uf, name##_set_mp_ws, name##_get_mp_ws,\
156 name##_set_mp_rs, name##_get_mp_rs }
159 * Analog of other XXX_CB macros. Has the pointer to _nss_cache_info
160 * structure as the only argument.
162 #define NS_CACHE_CB(cinfo) {NSSRC_CACHE, __nss_cache_handler, (void *)(cinfo) },
164 /* args are: current pointer, current buffer, initial buffer, pointer type */
165 #define NS_APPLY_OFFSET(cp, cb, ib, p_type) \
167 (cp) = (p_type)((char *)(cb) + (size_t)(cp) - (size_t)(ib))
169 * Gets new pointer from the marshalled buffer by uisng initial address
170 * and initial buffer address
172 #define NS_GET_NEWP(cp, cb, ib) \
173 ((char *)(cb) + (size_t)(cp) - (size_t)(ib))
175 typedef struct _nss_cache_data {
179 nss_cache_info const *info;
183 /* dummy function, which is needed to make nss_method_lookup happy */
184 extern int __nss_cache_handler(void *, void *, va_list);
187 extern int __nss_common_cache_read(void *, void *, va_list);
188 extern int __nss_common_cache_write(void *, void *, va_list);
189 extern int __nss_common_cache_write_negative(void *);
191 extern int __nss_mp_cache_read(void *, void *, va_list);
192 extern int __nss_mp_cache_write(void *, void *, va_list);
193 extern int __nss_mp_cache_write_submit(void *, void *, va_list);
194 extern int __nss_mp_cache_end(void *, void *, va_list);
195 #endif /* _NS_PRIVATE */