sip_utils.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #ifdef SER
  2. #include <cds/sip_utils.h>
  3. #include <cds/sstr.h>
  4. #include <parser/parse_expires.h>
  5. #include <parser/parse_subscription_state.h>
  6. #include <parser/parser_f.h>
  7. #include <parser/parse_to.h>
  8. #include <trim.h>
  9. int get_expiration_value(struct sip_msg *m, int *value)
  10. {
  11. exp_body_t *expires = NULL;
  12. int res = 1;
  13. if (parse_headers(m, HDR_EXPIRES_F, 0) == -1) {
  14. /* can't parse expires header */
  15. return -1;
  16. }
  17. if (m->expires) {
  18. if (parse_expires(m->expires) < 0) {
  19. return -1;
  20. }
  21. res = 0;
  22. expires = (exp_body_t *)m->expires->parsed;
  23. if (expires) if (expires->valid && value) *value = expires->val;
  24. }
  25. /* ERR("subscription will expire in %d secs\n", e); */
  26. return res;
  27. }
  28. int is_terminating_notify(struct sip_msg *m)
  29. {
  30. subscription_state_t *ss;
  31. if (parse_headers(m, HDR_SUBSCRIPTION_STATE_F, 0) == -1) {
  32. ERR("Error while parsing headers\n");
  33. return 0; /* ignore */
  34. }
  35. if (!m->subscription_state) {
  36. ERR("Invalid NOTIFY request (without Subscription-State)\n");
  37. return 0; /* ignore */
  38. }
  39. if (parse_subscription_state(m->subscription_state) < 0) {
  40. ERR("can't parse Subscription-State\n");
  41. return 0; /* ignore */
  42. }
  43. ss = (subscription_state_t*)m->subscription_state->parsed;
  44. if (!ss) {
  45. ERR("invalid Subscription-State\n");
  46. return 0; /* ignore */
  47. }
  48. if (ss->value == ss_terminated) return 1;
  49. return 0;
  50. }
  51. static inline int contains_extension_support(struct hdr_field *h,
  52. str *extension)
  53. {
  54. /* "parses" Supported header and looks for extension */
  55. str s, val;
  56. char *c;
  57. if (!h) return 0;
  58. s = h->body;
  59. while (s.len > 0) {
  60. c = find_not_quoted(&s, ',');
  61. if (c) {
  62. val.s = s.s;
  63. val.len = c - val.s;
  64. trim(&val);
  65. if (str_case_equals(&val, extension) == 0) return 1;
  66. s.len = s.len - (c - s.s) - 1;
  67. s.s = c + 1;
  68. }
  69. else {
  70. trim(&s);
  71. if (str_case_equals(&s, extension) == 0) return 1;
  72. break;
  73. }
  74. }
  75. return 0;
  76. }
  77. int supports_extension(struct sip_msg *m, str *extension)
  78. {
  79. /* walk through all Supported headers */
  80. struct hdr_field *h;
  81. int res;
  82. /* we need all Supported headers */
  83. res = parse_headers(m, HDR_EOH_F, 0);
  84. if (res == -1) {
  85. ERR("Error while parsing headers (%d)\n", res);
  86. return 0; /* what to return here ? */
  87. }
  88. h = m->supported;
  89. while (h) {
  90. if (h->type == HDR_SUPPORTED_T) {
  91. if (contains_extension_support(h, extension)) return 1;
  92. }
  93. h = h->next;
  94. }
  95. return 0;
  96. }
  97. int requires_extension(struct sip_msg *m, str *extension)
  98. {
  99. /* walk through all Require headers */
  100. struct hdr_field *h;
  101. int res;
  102. /* we need all Require headers */
  103. res = parse_headers(m, HDR_EOH_F, 0);
  104. if (res == -1) {
  105. ERR("Error while parsing headers (%d)\n", res);
  106. return 0; /* what to return here ? */
  107. }
  108. h = m->require;
  109. while (h) {
  110. if (h->type == HDR_REQUIRE_T) {
  111. if (contains_extension_support(h, extension)) return 1;
  112. }
  113. h = h->next;
  114. }
  115. return 0;
  116. }
  117. /**
  118. * Verifies presence of the To-tag in message. Returns 1 if
  119. * the tag is present, 0 if not, -1 on error.
  120. */
  121. int has_to_tag(struct sip_msg *_m)
  122. {
  123. struct to_body *to = (struct to_body*)_m->to->parsed;
  124. if (!to) return -1;
  125. if (to->tag_value.len > 0) return 1;
  126. return 0;
  127. }
  128. #endif