1158115Sume/*- 2158115Sume * Copyright (c) 2005 Michael Bushkov <bushman@rsu.ru> 3158115Sume * All rights reserved. 4158115Sume * 5158115Sume * Redistribution and use in source and binary forms, with or without 6158115Sume * modification, are permitted provided that the following conditions 7158115Sume * are met: 8158115Sume * 1. Redistributions of source code must retain the above copyright 9158115Sume * notice, this list of conditions and the following disclaimer. 10158115Sume * 2. Redistributions in binary form must reproduce the above copyright 11158115Sume * notice, this list of conditions and the following disclaimer in the 12158115Sume * documentation and/or other materials provided with the distribution. 13158115Sume * 14158115Sume * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15158115Sume * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16158115Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17158115Sume * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18158115Sume * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19158115Sume * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20158115Sume * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21158115Sume * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22158115Sume * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23158115Sume * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24158115Sume * SUCH DAMAGE. 25158115Sume * 26158115Sume * $FreeBSD$ 27158115Sume */ 28158115Sume 29158115Sume#ifndef __NS_CACHE_H__ 30158115Sume#define __NS_CACHE_H__ 31158115Sume 32158115Sume#include "nscachedcli.h" 33158115Sume 34158115Sumetypedef int (*nss_cache_id_func_t)(char *, size_t *, va_list, void *); 35158115Sumetypedef int (*nss_cache_marshal_func_t)(char *, size_t *, void *, va_list, 36158115Sume void *); 37158115Sumetypedef int (*nss_cache_unmarshal_func_t)(char *, size_t, void *, va_list, 38158115Sume void *); 39158115Sume 40158115Sumetypedef void (*nss_set_mp_ws_func_t)(cached_mp_write_session); 41158115Sumetypedef cached_mp_write_session (*nss_get_mp_ws_func_t)(void); 42158115Sume 43158115Sumetypedef void (*nss_set_mp_rs_func_t)(cached_mp_read_session); 44158115Sumetypedef cached_mp_read_session (*nss_get_mp_rs_func_t)(void); 45158115Sume 46158115Sumetypedef struct _nss_cache_info { 47158115Sume char *entry_name; 48158115Sume void *mdata; 49158115Sume 50158115Sume /* 51158115Sume * These 3 functions should be implemented specifically for each 52158115Sume * nsswitch database. 53158115Sume */ 54158115Sume nss_cache_id_func_t id_func; /* marshals the request parameters */ 55158115Sume nss_cache_marshal_func_t marshal_func; /* marshals response */ 56158115Sume nss_cache_unmarshal_func_t unmarshal_func; /* unmarshals response */ 57158115Sume 58158115Sume /* 59158115Sume * These 4 functions should be generated with NSS_MP_CACHE_HANDLING 60158115Sume * macro. 61158115Sume */ 62158115Sume nss_set_mp_ws_func_t set_mp_ws_func; /* sets current write session */ 63158115Sume nss_get_mp_ws_func_t get_mp_ws_func; /* gets current write session */ 64158115Sume 65158115Sume nss_set_mp_rs_func_t set_mp_rs_func; /* sets current read session */ 66158115Sume nss_get_mp_rs_func_t get_mp_rs_func; /* gets current read session */ 67158115Sume} nss_cache_info; 68158115Sume 69158115Sume/* 70158115Sume * NSS_MP_CACHE_HANDLING implements the set_mp_ws, get_mp_ws, set_mp_rs, 71158115Sume * get_mp_rs functions, that are used in _nss_cache_info. It uses 72158115Sume * NSS_TLS_HANDLING macro to organize thread local storage. 73158115Sume */ 74158115Sume#define NSS_MP_CACHE_HANDLING(name) \ 75158115Sumestruct name##_mp_state { \ 76158115Sume cached_mp_write_session mp_write_session; \ 77158115Sume cached_mp_read_session mp_read_session; \ 78158115Sume}; \ 79158115Sume \ 80158115Sumestatic void \ 81158115Sumename##_mp_endstate(void *s) { \ 82158115Sume struct name##_mp_state *mp_state; \ 83158115Sume \ 84158115Sume mp_state = (struct name##_mp_state *)s; \ 85158115Sume if (mp_state->mp_write_session != INVALID_CACHED_MP_WRITE_SESSION)\ 86158115Sume __abandon_cached_mp_write_session(mp_state->mp_write_session);\ 87158115Sume \ 88158115Sume if (mp_state->mp_read_session != INVALID_CACHED_MP_READ_SESSION)\ 89158115Sume __close_cached_mp_read_session(mp_state->mp_read_session);\ 90158115Sume} \ 91158115SumeNSS_TLS_HANDLING(name##_mp); \ 92158115Sume \ 93158115Sumestatic void \ 94158115Sumename##_set_mp_ws(cached_mp_write_session ws) \ 95158115Sume{ \ 96158115Sume struct name##_mp_state *mp_state; \ 97158115Sume int res; \ 98158115Sume \ 99158115Sume res = name##_mp_getstate(&mp_state); \ 100158115Sume if (res != 0) \ 101158115Sume return; \ 102158115Sume \ 103158115Sume mp_state->mp_write_session = ws; \ 104158115Sume} \ 105158115Sume \ 106158115Sumestatic cached_mp_write_session \ 107158115Sumename##_get_mp_ws(void) \ 108158115Sume{ \ 109158115Sume struct name##_mp_state *mp_state; \ 110158115Sume int res; \ 111158115Sume \ 112158115Sume res = name##_mp_getstate(&mp_state); \ 113158115Sume if (res != 0) \ 114158115Sume return (INVALID_CACHED_MP_WRITE_SESSION); \ 115158115Sume \ 116158115Sume return (mp_state->mp_write_session); \ 117158115Sume} \ 118158115Sume \ 119158115Sumestatic void \ 120158115Sumename##_set_mp_rs(cached_mp_read_session rs) \ 121158115Sume{ \ 122158115Sume struct name##_mp_state *mp_state; \ 123158115Sume int res; \ 124158115Sume \ 125158115Sume res = name##_mp_getstate(&mp_state); \ 126158115Sume if (res != 0) \ 127158115Sume return; \ 128158115Sume \ 129158115Sume mp_state->mp_read_session = rs; \ 130158115Sume} \ 131158115Sume \ 132158115Sumestatic cached_mp_read_session \ 133158115Sumename##_get_mp_rs(void) \ 134158115Sume{ \ 135158115Sume struct name##_mp_state *mp_state; \ 136158115Sume int res; \ 137158115Sume \ 138158115Sume res = name##_mp_getstate(&mp_state); \ 139158115Sume if (res != 0) \ 140158115Sume return (INVALID_CACHED_MP_READ_SESSION); \ 141158115Sume \ 142158115Sume return (mp_state->mp_read_session); \ 143158115Sume} 144158115Sume 145158115Sume/* 146158115Sume * These macros should be used to initialize _nss_cache_info structure. For 147158115Sume * multipart queries in setXXXent and getXXXent functions mf and uf 148158115Sume * (marshal function and unmarshal function) should be both NULL. 149158115Sume */ 150158115Sume#define NS_COMMON_CACHE_INFO_INITIALIZER(name, mdata, if, mf, uf) \ 151158115Sume {#name, mdata, if, mf, uf, NULL, NULL, NULL, NULL} 152158115Sume#define NS_MP_CACHE_INFO_INITIALIZER(name, mdata, mf, uf) \ 153158115Sume {#name, mdata, NULL, mf, uf, name##_set_mp_ws, name##_get_mp_ws,\ 154158115Sume name##_set_mp_rs, name##_get_mp_rs } 155158115Sume 156158115Sume/* 157158115Sume * Analog of other XXX_CB macros. Has the pointer to _nss_cache_info 158158115Sume * structure as the only argument. 159158115Sume */ 160158115Sume#define NS_CACHE_CB(cinfo) {NSSRC_CACHE, __nss_cache_handler, (void *)(cinfo) }, 161158115Sume 162158115Sume/* args are: current pointer, current buffer, initial buffer, pointer type */ 163158115Sume#define NS_APPLY_OFFSET(cp, cb, ib, p_type) \ 164158115Sume if ((cp) != NULL) \ 165158115Sume (cp) = (p_type)((char *)(cb) + (size_t)(cp) - (size_t)(ib)) 166158115Sume/* 167158115Sume * Gets new pointer from the marshalled buffer by uisng initial address 168158115Sume * and initial buffer address 169158115Sume */ 170158115Sume#define NS_GET_NEWP(cp, cb, ib) \ 171158115Sume ((char *)(cb) + (size_t)(cp) - (size_t)(ib)) 172158115Sume 173158115Sumetypedef struct _nss_cache_data { 174158115Sume char *key; 175158115Sume size_t key_size; 176158115Sume 177158115Sume nss_cache_info const *info; 178158115Sume} nss_cache_data; 179158115Sume 180158115Sume__BEGIN_DECLS 181158115Sume/* dummy function, which is needed to make nss_method_lookup happy */ 182158115Sumeextern int __nss_cache_handler(void *, void *, va_list); 183158115Sume 184158115Sume#ifdef _NS_PRIVATE 185158115Sumeextern int __nss_common_cache_read(void *, void *, va_list); 186158115Sumeextern int __nss_common_cache_write(void *, void *, va_list); 187158115Sumeextern int __nss_common_cache_write_negative(void *); 188158115Sume 189158115Sumeextern int __nss_mp_cache_read(void *, void *, va_list); 190158115Sumeextern int __nss_mp_cache_write(void *, void *, va_list); 191158115Sumeextern int __nss_mp_cache_write_submit(void *, void *, va_list); 192158115Sumeextern int __nss_mp_cache_end(void *, void *, va_list); 193158115Sume#endif /* _NS_PRIVATE */ 194158115Sume 195158115Sume__END_DECLS 196158115Sume 197158115Sume#endif 198