nid.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * $Id$
  3. *
  4. * nonce id and pool management (stuff common to nonce-count and one
  5. * time nonces)
  6. *
  7. * Copyright (C) 2008 iptelorg GmbH
  8. *
  9. * This file is part of ser, a free SIP server.
  10. *
  11. * ser is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version
  15. *
  16. * ser is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. /*
  26. * Defines:
  27. * USE_NC, USE_OT_NONCE - if neither of them defined no code will be
  28. * compiled
  29. */
  30. /*
  31. * History:
  32. * --------
  33. * 2008-07-08 initial version (andrei)
  34. */
  35. unsigned int nid_pool_no; /* number of index pools, 2^k */
  36. #if defined USE_NC || defined USE_OT_NONCE
  37. #include "nid.h"
  38. #include "../../dprint.h"
  39. #include "../../bit_scan.h"
  40. struct pool_index* nid_crt=0;
  41. /* instead of storing only the 2^k size we store also k
  42. * for faster operations */
  43. unsigned int nid_pool_k; /* pools no in bits (k in 2^k) */
  44. unsigned int nid_pool_mask; /* mask for computing the current pool*/
  45. /* returns -1 on error, 0 on success */
  46. int init_nonce_id()
  47. {
  48. unsigned pool_no, r;
  49. if (nid_crt!=0)
  50. return 0; /* already init */
  51. if (nid_pool_no==0){
  52. nid_pool_no=DEFAULT_NID_POOL_SIZE;
  53. }
  54. if (nid_pool_no>MAX_NID_POOL_SIZE){
  55. WARN("auth: nid_pool_no too big, truncating to %d\n",
  56. MAX_NID_POOL_SIZE);
  57. nid_pool_no=MAX_NID_POOL_SIZE;
  58. }
  59. nid_pool_k=bit_scan_reverse32(nid_pool_no);
  60. nid_pool_mask=(1<<nid_pool_k)-1;
  61. pool_no=1UL<<nid_pool_k; /* ROUNDDOWN to 2^k */
  62. if (pool_no!=nid_pool_no){
  63. INFO("auth: nid_pool_no rounded down to %d\n", pool_no);
  64. }
  65. nid_pool_no=pool_no;
  66. nid_crt=shm_malloc(sizeof(*nid_crt)*nid_pool_no);
  67. if (nid_crt==0){
  68. ERR("auth: init_nonce_id: memory allocation failure\n");
  69. return -1;
  70. }
  71. /* init nc_crt_id with random values */
  72. for (r=0; r<nid_pool_no; r++)
  73. atomic_set(&nid_crt[r].id, random());
  74. return 0;
  75. /*
  76. error:
  77. destroy_nonce_id();
  78. return -1;
  79. */
  80. }
  81. void destroy_nonce_id()
  82. {
  83. if (nid_crt){
  84. shm_free(nid_crt);
  85. nid_crt=0;
  86. }
  87. }
  88. #endif /*if defined USE_NC || defined USE_OT_NONCE */