index.c

Go to the documentation of this file.
00001 /*
00002  * $Id: index.c 5367 2008-12-16 10:19:53Z henningw $
00003  *
00004  * Copyright (C)2008  Voice System S.R.L
00005  *
00006  * This file is part of Kamailio, a free SIP server.
00007  *
00008  * Kamailio is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version
00012  *
00013  * Kamailio is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License 
00019  * along with this program; if not, write to the Free Software 
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  * History:
00023  * --------
00024  *  2008-05-29  initial version (anca)
00025  */
00026 
00027 /*!
00028  * \file
00029  * \brief Nonce index related functions
00030  * \ingroup auth
00031  * - Module: \ref auth
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  * \brief Get valid index for nonce
00055  * \return index on success, -1 on failure
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    /* update last index for the previous seconds */
00066    if(*next_index== -1) /* for the first request */
00067       *next_index= 0;
00068    else
00069    {
00070       /* if the portion with still alive nonces is not yet reached */
00071       if(*second!= curr_sec)
00072       {
00073          /* get the index for the next nonce */
00074          index= (*next_index==MAX_NONCE_INDEX)?MAX_NONCE_INDEX-1:*next_index -1;
00075 
00076          /* set the interval in sec_monit vector */
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) /* if in the first second*/
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]) /* if at the end of the buffer */
00106    {
00107       /* if at the end of the buffer */
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])  /* no more space -> return error */
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  * \brief Check if the nonce has been used before
00136  * \param index index
00137  * \return 1 if nonce is valid, 0 if not valid or on errors
00138  */
00139 int is_nonce_index_valid(int index)
00140 {
00141    /* if greater than MAX_NONCE_INDEX ->error */
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     /* if in the first 30 seconds */
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     /* check if right interval */
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     /* check if the first time used */
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 }

Generated on Wed May 23 08:00:57 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6