|
| 1 | +/* -*- mode: c; c-basic-offset: 4 -*- */ |
| 2 | +#ifndef __SESSION_CACHE_H__ |
| 3 | +#define __SESSION_CACHE_H__ |
| 4 | + |
| 5 | +/* Copyright (C) 2023 Alexander Chernov <[email protected]> */ |
| 6 | + |
| 7 | +/* |
| 8 | + * This program is free software; you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU General Public License as published by |
| 10 | + * the Free Software Foundation; either version 2 of the License, or |
| 11 | + * (at your option) any later version. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * GNU General Public License for more details. |
| 17 | + */ |
| 18 | + |
| 19 | +#include "ejudge/config.h" |
| 20 | +#include "ejudge/ej_types.h" |
| 21 | + |
| 22 | +#include <time.h> |
| 23 | + |
| 24 | +/* session cache to reduce ej-users request ratio */ |
| 25 | +struct new_session_info |
| 26 | +{ |
| 27 | + ej_cookie_t session_id; |
| 28 | + ej_cookie_t client_key; |
| 29 | + |
| 30 | + ej_ip_t origin_ip; // access address |
| 31 | + time_t access_time; // time of the last access |
| 32 | + time_t refresh_time; // time to refresh the values |
| 33 | + time_t expire_time; // cookie expiration time |
| 34 | + |
| 35 | + unsigned char *login; |
| 36 | + unsigned char *name; |
| 37 | + struct userlist_user *user_info; |
| 38 | + |
| 39 | + int cmd; // command (PRIV_GET_COOKIE or TEAM_GET_COOKIE) |
| 40 | + int user_id; |
| 41 | + int contest_id; |
| 42 | + unsigned int reg_flags; |
| 43 | + |
| 44 | + unsigned char ssl_flag; |
| 45 | + signed char locale_id; |
| 46 | + unsigned char priv_level; |
| 47 | + unsigned char role; |
| 48 | + unsigned char is_ws; |
| 49 | + unsigned char is_job; |
| 50 | + unsigned char team_login; |
| 51 | + unsigned char reg_status; |
| 52 | + unsigned char passwd_method; |
| 53 | + |
| 54 | + // these fields are from 'session_info' |
| 55 | + int user_view_all_runs; |
| 56 | + int user_view_all_clars; |
| 57 | + int user_viewed_section; |
| 58 | +}; |
| 59 | + |
| 60 | +struct new_session_cache |
| 61 | +{ |
| 62 | + struct new_session_info *info; |
| 63 | + int reserved; |
| 64 | + int res_index; |
| 65 | + int rehash_threshold; |
| 66 | + int used; |
| 67 | +}; |
| 68 | + |
| 69 | +struct cached_token_info |
| 70 | +{ |
| 71 | + unsigned char token[32]; |
| 72 | + ej_ip_t origin_ip; // access address |
| 73 | + time_t access_time; // time of the last access |
| 74 | + time_t expiry_time; |
| 75 | + time_t refresh_time; |
| 76 | + |
| 77 | + unsigned char *login; |
| 78 | + unsigned char *name; |
| 79 | + |
| 80 | + int cmd; |
| 81 | + int user_id; |
| 82 | + int contest_id; |
| 83 | + unsigned int reg_flags; |
| 84 | + |
| 85 | + unsigned char ssl_flag; |
| 86 | + unsigned char role; |
| 87 | + unsigned char reg_status; |
| 88 | + unsigned char all_contests; |
| 89 | + unsigned char used; // 1 if this entry is used |
| 90 | +}; |
| 91 | + |
| 92 | +struct token_cache |
| 93 | +{ |
| 94 | + struct cached_token_info *info; |
| 95 | + int reserved; |
| 96 | + int res_index; |
| 97 | + int rehash_threshold; |
| 98 | + int used; |
| 99 | +}; |
| 100 | + |
| 101 | +struct id_cache |
| 102 | +{ |
| 103 | + struct new_session_cache s; |
| 104 | + struct token_cache t; |
| 105 | +}; |
| 106 | + |
| 107 | +#ifdef __cplusplus |
| 108 | +extern "C" { |
| 109 | +#endif |
| 110 | + |
| 111 | +void idc_init(struct id_cache *idc); |
| 112 | + |
| 113 | +struct new_session_info * nsc_insert(struct new_session_cache *nsc, ej_cookie_t session_id, ej_cookie_t client_key); |
| 114 | +struct new_session_info * nsc_find(struct new_session_cache *nsc, ej_cookie_t session_id, ej_cookie_t client_key); |
| 115 | +int nsc_remove(struct new_session_cache *nsc, ej_cookie_t session_id, ej_cookie_t client_key, struct new_session_info *out); |
| 116 | + |
| 117 | +struct cached_token_info *tc_insert(struct token_cache *tc, const unsigned char *token); |
| 118 | +struct cached_token_info *tc_find(struct token_cache *tc, const unsigned char *token); |
| 119 | +int tc_remove(struct token_cache *tc, const unsigned char *token, struct cached_token_info *out); |
| 120 | + |
| 121 | +#ifdef __cplusplus |
| 122 | +} |
| 123 | +#endif |
| 124 | + |
| 125 | +#endif /* __SESSION_CACHE_H__ */ |
0 commit comments