00001
00055 #include <time.h>
00056 #include <stdio.h>
00057 #include <stdlib.h>
00058
00059 #include "session.h"
00060 #include "diameter.h"
00061 #include "config.h"
00062
00063 extern dp_config *config;
00065 gen_lock_t *session_lock;
00066 unsigned int *session_id1;
00067 unsigned int *session_id2;
00072 int session_init()
00073 {
00074 session_lock = lock_alloc();
00075 if (!session_lock){
00076 LOG_NO_MEM("lock",sizeof(gen_lock_t));
00077 goto error;
00078 }
00079 session_lock = lock_init(session_lock);
00080 session_id1 = shm_malloc(sizeof(unsigned int));
00081 if (!session_id1){
00082 LOG_NO_MEM("shm",sizeof(unsigned int));
00083 goto error;
00084 }
00085 session_id2 = shm_malloc(sizeof(unsigned int));
00086 if (!session_id2){
00087 LOG_NO_MEM("shm",sizeof(unsigned int));
00088 goto error;
00089 }
00090 srand((unsigned int)time(0));
00091 *session_id1 = rand();
00092 *session_id1 <<= 16;
00093 *session_id1 += time(0)&0xFFFF;
00094 *session_id2 = 0;
00095 return 1;
00096 error:
00097 return 0;
00098 }
00099
00103 int session_destroy()
00104 {
00105 lock_get(session_lock);
00106 lock_destroy(session_lock);
00107 lock_dealloc((void*)session_lock);
00108 shm_free(session_id1);
00109 shm_free(session_id2);
00110 return 1;
00111 }
00112
00113
00114
00115
00116
00117
00118
00119 static int generate_sessionID( str *sID, unsigned int end_pad_len)
00120 {
00121 unsigned int s2;
00122
00123
00124 if (!sID)
00125 goto error;
00126
00127
00128 sID->len = config->identity.len +
00129 1 + 10 +
00130 1 + 10 +
00131
00132 1 +
00133 end_pad_len;
00134
00135
00136 sID->s = (char*)shm_malloc( sID->len );
00137 if (sID->s==0) {
00138 LOG(L_ERR,"ERROR:generate_sessionID: no more free memory!\n");
00139 goto error;
00140 }
00141
00142 lock_get(session_lock);
00143 s2 = *session_id2 +1;
00144 *session_id2 = s2;
00145 lock_release(session_lock);
00146
00147
00148 sprintf(sID->s,"%.*s;%u;%u",config->identity.len,config->identity.s,*session_id1,s2);
00149 sID->len = strlen(sID->s);
00150 return 1;
00151 error:
00152 return -1;
00153 }
00154
00155
00156
00157
00158
00159
00160
00161
00166 AAASessionId AAACreateSession()
00167 {
00168 AAASessionId sID={0,0};
00169
00170
00171
00172 if (generate_sessionID( &(sID), 0 )!=1)
00173 goto error;
00174
00175 return sID;
00176 error:
00177 LOG(L_ERR,"ERROR:AAACreateSession(): Error on new session generation\n");
00178 return sID;
00179 }
00180
00185 int AAADropSession(AAASessionId *s)
00186 {
00187 if (s->s) shm_free(s->s);
00188 s->s =0;
00189 s->len=0;
00190 return AAA_ERR_SUCCESS;
00191 }