hash_mode.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "../../sr_module.h"
  2. #include "../../crc.h"
  3. #include <ctype.h>
  4. #include "sipcapture.h"
  5. #include "hash_mode.h"
  6. static int get_source(struct _sipcapture_object *sco, enum hash_source source,
  7. str *source_string);
  8. static int get_call_id (struct _sipcapture_object *sco, str *source_string);
  9. static int get_from_user (struct _sipcapture_object *sco, str *source_string);
  10. static int get_to_user (struct _sipcapture_object *sco, str *source_string);
  11. static int first_token (str *source_string);
  12. int hash_func (struct _sipcapture_object * sco,
  13. enum hash_source source, int denominator) {
  14. int ret;
  15. unsigned int hash;
  16. str source_string;
  17. if(get_source (sco, source, &source_string) == -1) {
  18. return -1;
  19. }
  20. LM_DBG("source string: [%.*s]\n", source_string.len, source_string.s);
  21. crc32_uint(&source_string, &hash);
  22. ret = hash % denominator;
  23. return ret;
  24. }
  25. static int get_source (struct _sipcapture_object *sco, enum hash_source source,
  26. str *source_string) {
  27. source_string->s = NULL;
  28. source_string->len = 0;
  29. switch (source) {
  30. case hs_call_id:
  31. return get_call_id (sco, source_string);
  32. case hs_from_user:
  33. return get_from_user(sco, source_string);
  34. case hs_to_user:
  35. return get_to_user(sco, source_string);
  36. default:
  37. LM_ERR("unknown hash source %i.\n",
  38. (int) source);
  39. return -1;
  40. }
  41. }
  42. static int get_call_id (struct _sipcapture_object * sco, str *source_string) {
  43. if (!sco->callid.s || !sco->callid.len)
  44. {
  45. return -1;
  46. }
  47. source_string->s = sco->callid.s;
  48. source_string->len = sco->callid.len;
  49. first_token (source_string);
  50. return 0;
  51. }
  52. static int get_from_user (struct _sipcapture_object * sco, str *source_string) {
  53. if (!sco->from_user.s || !sco->from_user.len)
  54. {
  55. return -1;
  56. }
  57. source_string->s = sco->from_user.s;
  58. source_string->len = sco->from_user.len;
  59. return 0;
  60. }
  61. static int get_to_user (struct _sipcapture_object * sco, str *source_string) {
  62. if (!sco->to_user.s || !sco->to_user.len)
  63. {
  64. return -1;
  65. }
  66. source_string->s = sco->to_user.s;
  67. source_string->len = sco->to_user.len;
  68. return 0;
  69. }
  70. static int first_token (str *source_string) {
  71. size_t len;
  72. if (source_string->s == NULL || source_string->len == 0) {
  73. return 0;
  74. }
  75. while (source_string->len > 0 && isspace (*source_string->s)) {
  76. ++source_string->s;
  77. --source_string->len;
  78. }
  79. for (len = 0; len < source_string->len; ++len) {
  80. if (isspace (source_string->s[len])) {
  81. source_string->len = len;
  82. break;
  83. }
  84. }
  85. return 0;
  86. }
  87. enum hash_source get_hash_source (const char *hash_source){
  88. if (strcasecmp ("call_id", hash_source) == 0){
  89. return hs_call_id;
  90. }
  91. else if (strcasecmp("from_user", hash_source) == 0)
  92. {
  93. return hs_from_user;
  94. }
  95. else if (strcasecmp("to_user", hash_source) == 0)
  96. {
  97. return hs_to_user;
  98. }
  99. else {
  100. return hs_error;
  101. }
  102. }