2
0

parse_event.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #include "parse_event.h"
  34. #include "../mem/mem.h" /* pkg_malloc, pkg_free */
  35. #include "../dprint.h"
  36. #include <string.h> /* memset */
  37. #include "../trim.h" /* trim_leading */
  38. #include <stdio.h> /* printf */
  39. #define PRES_STR "presence"
  40. #define PRES_STR_LEN 8
  41. static inline char* skip_token_nodot(char* _b, int _l)
  42. {
  43. int i = 0;
  44. for(i = 0; i < _l; i++) {
  45. switch(_b[i]) {
  46. case ' ':
  47. case '\r':
  48. case '\n':
  49. case '\t':
  50. case ';':
  51. case '.':
  52. return _b + i;
  53. }
  54. }
  55. return _b + _l;
  56. }
  57. static inline int event_parser(char* _s, int _l, event_t* _e)
  58. {
  59. str tmp;
  60. char* end;
  61. tmp.s = _s;
  62. tmp.len = _l;
  63. trim_leading(&tmp);
  64. if (tmp.len == 0) {
  65. LOG(L_ERR, "event_parser(): Empty body\n");
  66. return -1;
  67. }
  68. _e->text.s = tmp.s;
  69. end = skip_token_nodot(tmp.s, tmp.len);
  70. _e->text.len = end - tmp.s;
  71. if ((_e->text.len == PRES_STR_LEN) &&
  72. !strncasecmp(PRES_STR, tmp.s, _e->text.len)) {
  73. _e->parsed = EVENT_PRESENCE;
  74. } else {
  75. _e->parsed = EVENT_OTHER;
  76. }
  77. return 0;
  78. }
  79. /*
  80. * Parse Event header field body
  81. */
  82. int parse_event(struct hdr_field* _h)
  83. {
  84. event_t* e;
  85. if (_h->parsed != 0) {
  86. return 0;
  87. }
  88. e = (event_t*)pkg_malloc(sizeof(event_t));
  89. if (e == 0) {
  90. LOG(L_ERR, "parse_event(): No memory left\n");
  91. return -1;
  92. }
  93. memset(e, 0, sizeof(event_t));
  94. if (event_parser(_h->body.s, _h->body.len, e) < 0) {
  95. LOG(L_ERR, "parse_event(): Error in event_parser\n");
  96. pkg_free(e);
  97. return -2;
  98. }
  99. _h->parsed = (void*)e;
  100. return 0;
  101. }
  102. /*
  103. * Free all memory
  104. */
  105. void free_event(event_t** _e)
  106. {
  107. if (*_e) pkg_free(*_e);
  108. *_e = 0;
  109. }
  110. /*
  111. * Print structure, for debugging only
  112. */
  113. void print_event(event_t* _e)
  114. {
  115. printf("===Event===\n");
  116. printf("text : \'%.*s\'\n", _e->text.len, _e->text.s);
  117. printf("parsed: %s\n",
  118. (_e->parsed == EVENT_PRESENCE) ? ("EVENT_PRESENCE") : ("EVENT_OTHER"));
  119. printf("===/Event===\n");
  120. }