index.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include <stdio.h>
00035 #include "../../dprint.h"
00036 #include "../../timer.h"
00037 #include "index.h"
00038 #include "auth_mod.h"
00039
00040 #define set_buf_bit(index) \
00041 do{\
00042 nonce_buf[index>>3] |= (1<<(index%8));\
00043 }while(0)
00044
00045 #define unset_buf_bit(index) \
00046 do{\
00047 nonce_buf[index>>3] &= ~(1<<(index%8));\
00048 }while(0)
00049
00050 #define check_buf_bit(index) ( nonce_buf[index>>3] & (1<<(index%8)) )
00051
00052
00053
00054
00055
00056
00057 int reserve_nonce_index(void)
00058 {
00059 unsigned int curr_sec;
00060 int index, i;
00061
00062 curr_sec = get_ticks()%(nonce_expire+1);
00063 lock_get(nonce_lock);
00064
00065
00066 if(*next_index== -1)
00067 *next_index= 0;
00068 else
00069 {
00070
00071 if(*second!= curr_sec)
00072 {
00073
00074 index= (*next_index==MAX_NONCE_INDEX)?MAX_NONCE_INDEX-1:*next_index -1;
00075
00076
00077 if(curr_sec> *second)
00078 {
00079 for (i= *second; i< curr_sec; i++)
00080 sec_monit[i]= index;
00081 }
00082 else
00083 {
00084 for (i= *second; i<= nonce_expire; i++)
00085 sec_monit[i]= index;
00086
00087 for (i= 0; i< curr_sec; i++)
00088 sec_monit[i]= index;
00089 }
00090 }
00091 }
00092 *second= curr_sec;
00093
00094 if(sec_monit[curr_sec]== -1)
00095 {
00096 if(*next_index == MAX_NONCE_INDEX)
00097 {
00098 lock_release(nonce_lock);
00099 return -1;
00100 }
00101
00102 goto done;
00103 }
00104
00105 if(*next_index> sec_monit[curr_sec])
00106 {
00107
00108 if(*next_index == MAX_NONCE_INDEX)
00109 {
00110 *next_index = 0;
00111 goto index_smaller;
00112 }
00113 goto done;
00114 }
00115
00116 index_smaller:
00117 if(*next_index== sec_monit[curr_sec])
00118 {
00119 lock_release(nonce_lock);
00120 LM_INFO("no more indexes available\n");
00121 return -1;
00122 }
00123
00124 done:
00125 unset_buf_bit(*next_index);
00126 index= *next_index;
00127 *next_index = *next_index + 1;
00128 LM_DBG("second= %d, sec_monit= %d, index= %d\n", *second, sec_monit[curr_sec], index);
00129 lock_release(nonce_lock);
00130 return index;
00131 }
00132
00133
00134
00135
00136
00137
00138
00139 int is_nonce_index_valid(int index)
00140 {
00141
00142 if(index>= MAX_NONCE_INDEX ) {
00143 LM_ERR("index greater than buffer length\n");
00144 return 0;
00145 }
00146
00147 lock_get(nonce_lock);
00148
00149
00150 if(sec_monit[*second]== -1)
00151 {
00152 if(index>= *next_index)
00153 {
00154 LM_DBG("index out of range\n");
00155 lock_release(nonce_lock);
00156 return 0;
00157 }
00158 else
00159 {
00160 set_buf_bit(index);
00161 lock_release(nonce_lock);
00162 return 1;
00163 }
00164 }
00165
00166
00167 if(*next_index < sec_monit[*second])
00168 {
00169 if(!(index>= sec_monit[*second] || index<= *next_index))
00170 {
00171 LM_DBG("index out of the permitted interval\n");
00172 goto error;
00173 }
00174 }
00175 else
00176 {
00177 if(!(index >= sec_monit[*second] && index<=*next_index))
00178 {
00179 LM_DBG("index out of the permitted interval\n");
00180 goto error;
00181 }
00182 }
00183
00184
00185 if(check_buf_bit(index))
00186 {
00187 LM_DBG("nonce already used\n");
00188 goto error;
00189 }
00190
00191 set_buf_bit(index);
00192 lock_release(nonce_lock);
00193 return 1;
00194
00195 error:
00196 lock_release(nonce_lock);
00197 return 0;
00198
00199 }