parse_event.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * $Id$
  3. *
  4. * Event header field body parser.
  5. * The parser was written for Presence Agent module only.
  6. * it recognize presence package only, no subpackages, no parameters
  7. * It should be replaced by a more generic parser if subpackages or
  8. * parameters should be parsed too.
  9. *
  10. * Copyright (C) 2001-2003 Fhg Fokus
  11. *
  12. * This file is part of ser, a free SIP server.
  13. *
  14. * ser is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version
  18. *
  19. * For a license to use the ser software under conditions
  20. * other than those described here, or to purchase support for this
  21. * software, please contact iptel.org by e-mail at the following addresses:
  22. * [email protected]
  23. *
  24. * ser is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with this program; if not, write to the Free Software
  31. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  32. *
  33. * History:
  34. * --------
  35. * 2003-04-26 ZSW (jiri)
  36. */
  37. #include "parse_event.h"
  38. #include "../mem/mem.h" /* pkg_malloc, pkg_free */
  39. #include "../dprint.h"
  40. #include <string.h> /* memset */
  41. #include "../trim.h" /* trim_leading */
  42. #include <stdio.h> /* printf */
  43. #include "../ut.h"
  44. #define PRES_STR "presence"
  45. #define PRES_STR_LEN 8
  46. static inline char* skip_token_nodot(char* _b, int _l)
  47. {
  48. int i = 0;
  49. for(i = 0; i < _l; i++) {
  50. switch(_b[i]) {
  51. case ' ':
  52. case '\r':
  53. case '\n':
  54. case '\t':
  55. case ';':
  56. case '.':
  57. return _b + i;
  58. }
  59. }
  60. return _b + _l;
  61. }
  62. static inline int event_parser(char* _s, int _l, event_t* _e)
  63. {
  64. str tmp;
  65. char* end;
  66. tmp.s = _s;
  67. tmp.len = _l;
  68. trim_leading(&tmp);
  69. if (tmp.len == 0) {
  70. LOG(L_ERR, "event_parser(): Empty body\n");
  71. return -1;
  72. }
  73. _e->text.s = tmp.s;
  74. end = skip_token_nodot(tmp.s, tmp.len);
  75. _e->text.len = end - tmp.s;
  76. if ((_e->text.len == PRES_STR_LEN) &&
  77. !strncasecmp(PRES_STR, tmp.s, _e->text.len)) {
  78. _e->parsed = EVENT_PRESENCE;
  79. } else {
  80. _e->parsed = EVENT_OTHER;
  81. }
  82. return 0;
  83. }
  84. /*
  85. * Parse Event header field body
  86. */
  87. int parse_event(struct hdr_field* _h)
  88. {
  89. event_t* e;
  90. if (_h->parsed != 0) {
  91. return 0;
  92. }
  93. e = (event_t*)pkg_malloc(sizeof(event_t));
  94. if (e == 0) {
  95. LOG(L_ERR, "parse_event(): No memory left\n");
  96. return -1;
  97. }
  98. memset(e, 0, sizeof(event_t));
  99. if (event_parser(_h->body.s, _h->body.len, e) < 0) {
  100. LOG(L_ERR, "parse_event(): Error in event_parser\n");
  101. pkg_free(e);
  102. return -2;
  103. }
  104. _h->parsed = (void*)e;
  105. return 0;
  106. }
  107. /*
  108. * Free all memory
  109. */
  110. void free_event(event_t** _e)
  111. {
  112. if (*_e) pkg_free(*_e);
  113. *_e = 0;
  114. }
  115. /*
  116. * Print structure, for debugging only
  117. */
  118. void print_event(event_t* _e)
  119. {
  120. printf("===Event===\n");
  121. printf("text : \'%.*s\'\n", _e->text.len, ZSW(_e->text.s));
  122. printf("parsed: %s\n",
  123. (_e->parsed == EVENT_PRESENCE) ? ("EVENT_PRESENCE") : ("EVENT_OTHER"));
  124. printf("===/Event===\n");
  125. }