parse_subscription_state.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (C) 2006 Vaclav Kubart, vaclav dot kubart at iptel dot org
  3. *
  4. * This file is part of SIP-router, a free SIP server.
  5. *
  6. * SIP-router is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * SIP-router is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /*! \file
  21. * \brief Parser :: Parse subscription-state in NOTIFY
  22. *
  23. * \ingroup parser
  24. */
  25. #include "parse_subscription_state.h"
  26. #include "../dprint.h"
  27. #include "../trim.h"
  28. #include "../mem/mem.h"
  29. #include "../ut.h"
  30. #include "parser_f.h"
  31. #include "parse_param.h"
  32. #include <string.h>
  33. void free_subscription_state(subscription_state_t**ss)
  34. {
  35. if (ss) {
  36. if (*ss) pkg_free(*ss);
  37. *ss = 0;
  38. }
  39. }
  40. static inline int str_cmp(const str *a, const str *b)
  41. {
  42. int i;
  43. if (a->len != b->len) return 1;
  44. for (i = 0; i < a->len; i++)
  45. if (a->s[i] != b->s[i]) return 1;
  46. return 0;
  47. }
  48. static int ss_parse(str *src, subscription_state_t *ss)
  49. {
  50. static str active = STR_STATIC_INIT("active");
  51. static str pending = STR_STATIC_INIT("pending");
  52. static str terminated = STR_STATIC_INIT("terminated");
  53. int res = 0;
  54. param_hooks_t ph;
  55. param_t *params;
  56. str s = *src;
  57. str state;
  58. char *c;
  59. /* initialization */
  60. ss->expires_set = 0;
  61. ss->expires = 0;
  62. trim_leading(&s);
  63. state = s;
  64. c = find_not_quoted(&s, ';');
  65. if (c) {
  66. /* first parameter starts after c */
  67. state.len = c - state.s;
  68. s.len = s.len - (c - s.s) - 1;
  69. s.s = c + 1;
  70. }
  71. else {
  72. s.len = 0;
  73. }
  74. /* set state value */
  75. trim(&state);
  76. if (str_cmp(&state, &active) == 0) {
  77. ss->value = ss_active;
  78. }
  79. else if (str_cmp(&state, &pending) == 0) {
  80. ss->value = ss_pending;
  81. }
  82. else if (str_cmp(&state, &terminated) == 0) {
  83. ss->value = ss_terminated;
  84. }
  85. else {
  86. /* INFO("unknown subscription-State value :%.*s\n",
  87. state.len, state.s); */
  88. ss->value = ss_extension;
  89. }
  90. /* explore parameters */
  91. trim_leading(&s);
  92. if (s.len > 0) {
  93. params = NULL;
  94. if (parse_params(&s, CLASS_CONTACT, &ph, &params) < 0) {
  95. ERR("can't parse params\n");
  96. res = -1;
  97. }
  98. else {
  99. if (ph.contact.expires) {
  100. ss->expires_set = 1;
  101. res = str2int(&ph.contact.expires->body, &ss->expires);
  102. if (res != 0)
  103. ERR("invalid expires value: \'%.*s\'\n",
  104. ph.contact.expires->body.len,
  105. ph.contact.expires->body.s);
  106. }
  107. if (params) free_params(params);
  108. }
  109. }
  110. /*
  111. ss->value = ss_active;
  112. ss->expires = 0;*/
  113. return res;
  114. }
  115. int parse_subscription_state(struct hdr_field *h)
  116. {
  117. subscription_state_t *ss;
  118. if (h->parsed) return 0;
  119. ss = (subscription_state_t*)pkg_malloc(sizeof(*ss));
  120. if (!ss) {
  121. ERR("No memory left\n");
  122. return -1;
  123. }
  124. memset(ss, 0, sizeof(*ss));
  125. if (ss_parse(&h->body, ss) < 0) {
  126. ERR("Can't parse Subscription-State\n");
  127. pkg_free(ss);
  128. return -2;
  129. }
  130. h->parsed = (void*)ss;
  131. return 0;
  132. }