aaa_avps.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * $Id$
  3. *
  4. * Common functions for Digest Authentication and Accounting Modules
  5. *
  6. * Copyright (C) 2001-2004 FhG Fokus
  7. *
  8. * This file is part of ser, a free SIP server.
  9. *
  10. * ser is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * For a license to use the ser software under conditions
  16. * other than those described here, or to purchase support for this
  17. * software, please contact iptel.org by e-mail at the following addresses:
  18. * [email protected]
  19. *
  20. * ser is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  28. *
  29. */
  30. #ifndef _SER_AAA_AVPS_H_
  31. #define _SER_AAA_AVPS_H_
  32. #include "../../mem/mem.h"
  33. #include "../../parser/parser_f.h"
  34. #include "../../dprint.h"
  35. #include <string.h>
  36. /*
  37. * Parse list of tokens separated by some char and put each token
  38. * into result array. Caller frees result array!
  39. */
  40. static inline int
  41. parse_token_list(char *p, char *pend, char separator, str **result)
  42. {
  43. int i;
  44. i = 0;
  45. *result = NULL;
  46. while ((pend - p) > 0) {
  47. *result = pkg_realloc(*result, sizeof(**result) * (i + 1));
  48. if (*result == NULL)
  49. return -1;
  50. (*result)[i].s = p;
  51. p = eat_token2_end(p, pend, separator) + 1;
  52. (*result)[i].len = p - (*result)[i].s - 1;
  53. i++;
  54. }
  55. return i;
  56. }
  57. /*
  58. * Parse the list of AVP names separated by '|' into an array
  59. * of names, each element of the array is str string
  60. */
  61. static int
  62. aaa_avps_init(str *avp_list, str **parsed_avps, int *avps_n)
  63. {
  64. int errcode, i;
  65. char *cp;
  66. if (!avp_list->s || !avp_list->len) {
  67. /* AVPs disabled, nothing to do */
  68. *avps_n = 0;
  69. return 1;
  70. }
  71. cp = pkg_malloc(avp_list->len + 1);
  72. if (cp == NULL) {
  73. LOG(L_ERR, "aaa_avps::aaa_avps_init(): can't allocate memory\n");
  74. errcode = -1;
  75. goto bad;
  76. }
  77. memcpy(cp, avp_list->s, avp_list->len);
  78. *avps_n = parse_token_list(cp, cp + avp_list->len, '|', parsed_avps);
  79. if (*avps_n == -1) {
  80. LOG(L_ERR, "aaa_avps::aaa_avps_init(): can't parse avps_column_int "
  81. "parameter\n");
  82. errcode = -2;
  83. pkg_free(cp);
  84. goto bad;
  85. }
  86. for (i = 0; i < *avps_n; i++) {
  87. (*parsed_avps)[i].s[(*parsed_avps)[i].len] = '\0';
  88. }
  89. return 0;
  90. bad:
  91. if (*parsed_avps != NULL) {
  92. pkg_free((*parsed_avps)[0].s);
  93. pkg_free(*parsed_avps);
  94. }
  95. return errcode;
  96. }
  97. #endif