parse_event.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Event header field body parser.
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * ser is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. * History:
  23. * --------
  24. * 2003-04-26 ZSW (jiri)
  25. */
  26. /*! \file
  27. * \brief Parser :: Event header field body parser.
  28. *
  29. * The parser was written for Presence Agent module only.
  30. * it recognize presence package only, no sub-packages, no parameters
  31. * It should be replaced by a more generic parser if sub-packages or
  32. * parameters should be parsed too.
  33. *
  34. * \ingroup parser
  35. */
  36. #include "parse_event.h"
  37. #include "../mem/mem.h" /* pkg_malloc, pkg_free */
  38. #include "../dprint.h"
  39. #include <string.h> /* memset */
  40. #include "../trim.h" /* trim_leading */
  41. #include <stdio.h> /* printf */
  42. #include "../ut.h"
  43. static struct {
  44. str name;
  45. int type;
  46. } events[] = {
  47. {STR_STATIC_INIT("presence"), EVENT_PRESENCE},
  48. {STR_STATIC_INIT("presence.winfo"), EVENT_PRESENCE_WINFO},
  49. {STR_STATIC_INIT("xcap-change"), EVENT_XCAP_CHANGE},
  50. {STR_STATIC_INIT("sip-profile"), EVENT_SIP_PROFILE},
  51. {STR_STATIC_INIT("message-summary"), EVENT_MESSAGE_SUMMARY},
  52. {STR_STATIC_INIT("dialog"), EVENT_DIALOG},
  53. {STR_STATIC_INIT("ua-profile"), EVENT_UA_PROFILE},
  54. /* The following must be the last element in the array */
  55. {STR_NULL, EVENT_OTHER}
  56. };
  57. static inline char* skip_token(char* _b, int _l)
  58. {
  59. int i = 0;
  60. for(i = 0; i < _l; i++) {
  61. switch(_b[i]) {
  62. case ' ':
  63. case '\r':
  64. case '\n':
  65. case '\t':
  66. case ';':
  67. return _b + i;
  68. }
  69. }
  70. return _b + _l;
  71. }
  72. int event_parser(char* s, int len, event_t* e)
  73. {
  74. int i;
  75. str tmp;
  76. char* end;
  77. param_hooks_t* phooks = NULL;
  78. enum pclass pclass = CLASS_ANY;
  79. if (e == NULL) {
  80. ERR("event_parser: Invalid parameter value\n");
  81. return -1;
  82. }
  83. tmp.s = s;
  84. tmp.len = len;
  85. trim_leading(&tmp);
  86. if (tmp.len == 0) {
  87. LOG(L_ERR, "event_parser: Empty body\n");
  88. return -1;
  89. }
  90. e->name.s = tmp.s;
  91. end = skip_token(tmp.s, tmp.len);
  92. e->name.len = end - tmp.s;
  93. e->type = EVENT_OTHER;
  94. for(i = 0; events[i].name.len; i++) {
  95. if (e->name.len == events[i].name.len &&
  96. !strncasecmp(e->name.s, events[i].name.s, e->name.len)) {
  97. e->type = events[i].type;
  98. break;
  99. }
  100. }
  101. tmp.len -= end - tmp.s;
  102. tmp.s = end;
  103. trim_leading(&tmp);
  104. e->params.list = NULL;
  105. if (tmp.len && (tmp.s[0] == ';')) {
  106. /* Shift the semicolon and skip any leading whitespace, this is needed
  107. * for parse_params to work correctly. */
  108. tmp.s++; tmp.len--;
  109. trim_leading(&tmp);
  110. if (!tmp.len) return 0;
  111. /* We have parameters to parse */
  112. if (e->type == EVENT_DIALOG) {
  113. pclass = CLASS_EVENT_DIALOG;
  114. phooks = (param_hooks_t*)&e->params.hooks;
  115. }
  116. if (parse_params(&tmp, pclass, phooks, &e->params.list) < 0) {
  117. ERR("event_parser: Error while parsing parameters parameters\n");
  118. return -1;
  119. }
  120. }
  121. return 0;
  122. }
  123. /*! \brief
  124. * Parse Event header field body
  125. */
  126. int parse_event(struct hdr_field* _h)
  127. {
  128. event_t* e;
  129. if (_h->parsed != 0) {
  130. return 0;
  131. }
  132. e = (event_t*)pkg_malloc(sizeof(event_t));
  133. if (e == 0) {
  134. LOG(L_ERR, "parse_event(): No memory left\n");
  135. return -1;
  136. }
  137. memset(e, 0, sizeof(event_t));
  138. if (event_parser(_h->body.s, _h->body.len, e) < 0) {
  139. LOG(L_ERR, "parse_event(): Error in event_parser\n");
  140. pkg_free(e);
  141. return -2;
  142. }
  143. _h->parsed = (void*)e;
  144. return 0;
  145. }
  146. /*! \brief
  147. * Free all memory
  148. */
  149. void free_event(event_t** _e)
  150. {
  151. if (*_e) {
  152. if ((*_e)->params.list) free_params((*_e)->params.list);
  153. pkg_free(*_e);
  154. *_e = NULL;
  155. }
  156. }
  157. /*! \brief
  158. * Print structure, for debugging only
  159. */
  160. void print_event(event_t* e)
  161. {
  162. fprintf(stderr, "===Event===\n");
  163. fprintf(stderr, "name : \'%.*s\'\n", STR_FMT(&e->name));
  164. fprintf(stderr, "type: %d\n", e->type);
  165. if (e->params.list) {
  166. print_params(stderr, e->params.list);
  167. }
  168. fprintf(stderr, "===/Event===\n");
  169. }