KLone APIs | Modules | Data Structures | File List | Data Fields | Globals

ses_prv.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005, 2006 by KoanLogic s.r.l. <http://www.koanlogic.com>
00003  * All rights reserved.
00004  *
00005  * This file is part of KLone, and as such it is subject to the license stated
00006  * in the LICENSE file which you have received as part of this distribution.
00007  *
00008  * $Id: ses_prv.h,v 1.19 2009/05/31 18:50:27 tho Exp $
00009  */
00010 
00011 #ifndef _KLONE_SESPRV_H_
00012 #define _KLONE_SESPRV_H_
00013 
00014 #include "klone_conf.h"
00015 #ifdef HAVE_LIBOPENSSL
00016 #include <openssl/hmac.h>
00017 #include <openssl/evp.h>
00018 #include <openssl/rand.h>
00019 #endif /* HAVE_LIBOPENSSL */
00020 #include <u/libu.h>
00021 #include <klone/session.h>
00022 #include <klone/request.h>
00023 #include <klone/response.h>
00024 #include <klone/vars.h>
00025 #include <klone/http.h>
00026 #include <klone/atom.h>
00027 #include <klone/md5.h>
00028 
00029 #ifdef __cplusplus
00030 extern "C" {
00031 #endif
00032 
00033 typedef int (*session_load_t)(session_t*);
00034 typedef int (*session_save_t)(session_t*);
00035 typedef int (*session_remove_t)(session_t*);
00036 typedef int (*session_term_t)(session_t*);
00037 
00038 /* session type */
00039 enum { 
00040     SESSION_TYPE_UNKNOWN, 
00041     SESSION_TYPE_FILE, 
00042     SESSION_TYPE_MEMORY, 
00043     SESSION_TYPE_CLIENT
00044 };
00045 
00046 enum { 
00047     SESSION_ID_LENGTH = MD5_DIGEST_LEN,         /* sid length       */
00048     SESSION_ID_BUFSZ = 1 + SESSION_ID_LENGTH    /* sid buffer size  */
00049 };
00050 
00051 /* hmac and cipher key size */
00052 enum { 
00053     HMAC_KEY_SIZE = 64, 
00054     #ifdef HAVE_LIBOPENSSL
00055     CIPHER_KEY_SIZE = EVP_MAX_KEY_LENGTH, 
00056     CIPHER_IV_SIZE = EVP_MAX_IV_LENGTH
00057     #else
00058     CIPHER_KEY_SIZE = 64, CIPHER_IV_SIZE = 64
00059     #endif
00060  };
00061 
00062 /* session runtime parameters */
00063 typedef struct session_opt_s
00064 {
00065     /* common session options */
00066     int type;       /* type of sessions (file, memory, client-side)  */
00067     int max_age;    /* max allowed age of sessions                   */
00068     int encrypt;    /* >0 when client-side session encryption is on  */
00069     int compress;   /* >0 when client-side session compression is on */
00070     char name[128]; /* cookie name                                   */
00071     #ifdef HAVE_LIBOPENSSL
00072     const EVP_CIPHER *cipher; /* encryption cipher algorithm         */
00073     unsigned char cipher_key[CIPHER_KEY_SIZE]; /* cipher secret key  */
00074     unsigned char cipher_iv[CIPHER_IV_SIZE];   /* cipher Init Vector */
00075     #endif
00076 
00077     /* file session options/struct                                   */
00078     char path[U_FILENAME_MAX]; /* session save path                  */
00079     unsigned char session_key[CIPHER_KEY_SIZE]; /* session secret key*/
00080     unsigned char session_iv[CIPHER_IV_SIZE];   /* session init vect */
00081 
00082     /* in-memory session options/struct                              */
00083     atoms_t *atoms; /* atom list used to store in-memory sessions    */
00084     size_t max_count;   /* max # of in-memory sessions               */
00085     size_t mem_limit;   /* max (total) size of in-memory sessions    */
00086 
00087     /* client-side options/structs                                   */
00088     #ifdef HAVE_LIBOPENSSL
00089     HMAC_CTX hmac_ctx;  /* openssl HMAC context                      */
00090     const EVP_MD *hash; /* client-side session HMAC hash algorithm   */
00091     char hmac_key[HMAC_KEY_SIZE]; /* session HMAC secret key         */
00092     #endif
00093 } session_opt_t;
00094 
00095 struct session_s
00096 {
00097     vars_t *vars;               /* variable list                              */
00098     request_t *rq;              /* request bound to this session              */
00099     response_t *rs;             /* response bound to this session             */
00100     char filename[U_FILENAME_MAX];/* session filename                         */
00101     char id[SESSION_ID_BUFSZ];  /* session ID                                 */
00102     int removed;                /* >0 if the calling session has been deleted */
00103     int mtime;                  /* last modified time                         */
00104     session_load_t load;        /* ptr to the driver load function            */
00105     session_save_t save;        /* ptr to the driver save function            */
00106     session_remove_t remove;    /* ptr to the driver remove function          */
00107     session_term_t term;        /* ptr to the driver term function            */
00108     session_opt_t *so;          /* runtime option                             */
00109 };
00110 
00111 /* main c'tor */
00112 int session_create(session_opt_t*, request_t*, response_t*, session_t**);
00113 
00114 /* driver c'tor */
00115 int session_client_create(session_opt_t*, request_t*, response_t*, session_t**);
00116 int session_file_create(session_opt_t*, request_t*, response_t*, session_t**);
00117 int session_mem_create(session_opt_t*, request_t*, response_t*, session_t**);
00118 
00119 /* private functions */
00120 int session_prv_init(session_t *, request_t *, response_t *);
00121 int session_prv_load_from_io(session_t *, io_t *);
00122 int session_prv_save_to_io(session_t*, io_t *);
00123 int session_prv_save_var(var_t *, void*);
00124 int session_prv_calc_maxsize(var_t *v, void *p);
00125 int session_prv_save_to_buf(session_t *ss, char **pbuf, size_t *psz);
00126 int session_prv_load_from_buf(session_t *ss, char *buf, size_t size);
00127 int session_prv_set_id(session_t *ss, const char *sid);
00128 
00129 /* init/term funcs */
00130 int session_module_init(u_config_t *config, session_opt_t **pso);
00131 int session_file_module_init(u_config_t *config, session_opt_t *pso);
00132 int session_mem_module_init(u_config_t *config, session_opt_t *pso);
00133 int session_client_module_init(u_config_t *config, session_opt_t *pso);
00134 int session_module_term(session_opt_t *so);
00135 int session_module_term(session_opt_t *so);
00136 
00137 #ifdef __cplusplus
00138 }
00139 #endif 
00140 
00141 #endif