tsip_parser_message.rl 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Copyright (C) 2010-2011 Mamadou Diop.
  3. *
  4. * Contact: Mamadou Diop <diopmamadou(at)doubango[dot]org>
  5. *
  6. * This file is part of Open Source Doubango Framework.
  7. *
  8. * DOUBANGO 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 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * DOUBANGO 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 DOUBANGO.
  20. *
  21. */
  22. /**@file tsip_parser_message.c
  23. * @brief SIP parser.
  24. *
  25. * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
  26. *
  27. */
  28. #include "tinysip/parsers/tsip_parser_message.h"
  29. #include "tinysip/parsers/tsip_parser_header.h"
  30. #include "tinysip/parsers/tsip_parser_uri.h"
  31. #include "tsk_debug.h"
  32. #include "tsk_memory.h"
  33. static void tsip_message_parser_execute(tsk_ragel_state_t *state, tsip_message_t *message, tsk_bool_t extract_content);
  34. static void tsip_message_parser_init(tsk_ragel_state_t *state);
  35. static void tsip_message_parser_eoh(tsk_ragel_state_t *state, tsip_message_t *message, tsk_bool_t extract_content);
  36. // Check if we have ",CRLF" ==> See WWW-Authenticate header
  37. // As :>CRLF is preceded by any+ ==> p will be at least (start + 1)
  38. // p point to CR
  39. #define prev_not_comma(p) !(p && p[-1] == ',')
  40. /***********************************
  41. * Ragel state machine.
  42. */
  43. %%{
  44. machine tsip_machine_parser_message;
  45. #/* Tag the buffer (start point). */
  46. action tag
  47. {
  48. state->tag_start = p;
  49. }
  50. #/* SIP method */
  51. action parse_method
  52. {
  53. int len;
  54. state->tag_end = p;
  55. len = (int)(state->tag_end - state->tag_start);
  56. if(message->type == tsip_unknown)
  57. {
  58. message->type = tsip_request;
  59. if(!message->line.request.method)
  60. {
  61. message->line.request.method = tsk_calloc(1, len+1);
  62. memcpy(message->line.request.method, state->tag_start, len);
  63. message->line.request.request_type = tsip_request_get_type(message->line.request.method);
  64. }
  65. }
  66. else
  67. {
  68. state->cs = tsip_machine_parser_message_error;
  69. }
  70. }
  71. #/* Request URI parsing */
  72. action parse_requesturi
  73. {
  74. int len;
  75. state->tag_end = p;
  76. len = (int)(state->tag_end - state->tag_start);
  77. if(!message->line.request.uri)
  78. {
  79. message->line.request.uri = tsip_uri_parse(state->tag_start, (tsk_size_t)len);
  80. }
  81. }
  82. #/* Sip Version */
  83. action parse_sipversion
  84. {
  85. int len;
  86. state->tag_end = p;
  87. len = (int)(state->tag_end - state->tag_start);
  88. if(!message->sip_version)
  89. {
  90. message->sip_version = tsk_calloc(1, len+1);
  91. memcpy(message->sip_version, state->tag_start, len);
  92. }
  93. }
  94. #/* Status Code */
  95. action parse_status_code
  96. {
  97. int len;
  98. state->tag_end = p;
  99. len = (int)(state->tag_end - state->tag_start);
  100. if(message->type == tsip_unknown)
  101. {
  102. message->type = tsip_response;
  103. message->line.response.status_code = atoi(state->tag_start);
  104. }
  105. else
  106. {
  107. state->cs = tsip_machine_parser_message_error;
  108. }
  109. }
  110. #/* Reason Phrase */
  111. action parse_reason_phrase
  112. {
  113. int len;
  114. state->tag_end = p;
  115. len = (int)(state->tag_end - state->tag_start);
  116. if(!message->line.response.reason_phrase)
  117. {
  118. message->line.response.reason_phrase = tsk_calloc(1, len+1);
  119. memcpy(message->line.response.reason_phrase, state->tag_start, len);
  120. }
  121. }
  122. #/* Parse sip header */
  123. action parse_header
  124. {
  125. int len;
  126. state->tag_end = p;
  127. len = (int)(state->tag_end - state->tag_start);
  128. if(tsip_header_parse(state, message)){
  129. //TSK_DEBUG_INFO("TSIP_MESSAGE_PARSER::PARSE_HEADER len=%d state=%d", len, state->cs);
  130. }
  131. else{
  132. TSK_DEBUG_ERROR("Failed to parse header - %s", state->tag_start);
  133. }
  134. }
  135. #/* Parse sip content/body. */
  136. #action parse_body
  137. #{
  138. # int len;
  139. # state->tag_end = p;
  140. # len = (int)(state->tag_end - state->tag_start);
  141. # TSK_DEBUG_ERROR("==TSIP_MESSAGE_PARSER::PARSE_BODY==");
  142. #}
  143. #/* End-Of-Headers */
  144. action eoh
  145. {
  146. state->cs = cs;
  147. state->p = p;
  148. state->pe = pe;
  149. state->eof = eof;
  150. tsip_message_parser_eoh(state, message, extract_content);
  151. cs = state->cs;
  152. p = state->p;
  153. pe = state->pe;
  154. eof = state->eof;
  155. }
  156. # Includes
  157. include tsip_machine_utils "./ragel/tsip_machine_utils.rl";
  158. include tsip_machine_message "./ragel/tsip_machine_message.rl";
  159. # Entry point
  160. main := SIP_message;
  161. }%%
  162. /* Regel data */
  163. %%write data;
  164. tsk_bool_t tsip_message_parse(tsk_ragel_state_t *state, tsip_message_t **result, tsk_bool_t extract_content)
  165. {
  166. if(!state || state->pe <= state->p){
  167. return tsk_false;
  168. }
  169. if(!*result){
  170. *result = tsip_message_create();
  171. }
  172. /* Ragel init */
  173. tsip_message_parser_init(state);
  174. /*
  175. * State mechine execution.
  176. */
  177. tsip_message_parser_execute(state, *result, extract_content);
  178. /* Check result */
  179. if( state->cs < %%{ write first_final; }%% )
  180. {
  181. TSK_DEBUG_ERROR("Failed to parse SIP message: %s", state->p);
  182. TSK_OBJECT_SAFE_FREE(*result);
  183. return tsk_false;
  184. }
  185. return tsk_true;
  186. }
  187. static void tsip_message_parser_init(tsk_ragel_state_t *state)
  188. {
  189. int cs = 0;
  190. /* Regel machine initialization. */
  191. %% write init;
  192. state->cs = cs;
  193. }
  194. static void tsip_message_parser_execute(tsk_ragel_state_t *state, tsip_message_t *message, tsk_bool_t extract_content)
  195. {
  196. int cs = state->cs;
  197. const char *p = state->p;
  198. const char *pe = state->pe;
  199. const char *eof = state->eof;
  200. %% write exec;
  201. state->cs = cs;
  202. state->p = p;
  203. state->pe = pe;
  204. state->eof = eof;
  205. }
  206. static void tsip_message_parser_eoh(tsk_ragel_state_t *state, tsip_message_t *message, tsk_bool_t extract_content)
  207. {
  208. int cs = state->cs;
  209. const char *p = state->p;
  210. const char *pe = state->pe;
  211. const char *eof = state->eof;
  212. const char *eoh = (state->p + 1);
  213. if(extract_content && message)
  214. {
  215. uint32_t clen = TSIP_MESSAGE_CONTENT_LENGTH(message);
  216. if((p+clen) <pe && !message->Content){
  217. message->Content = tsk_buffer_create((p+1), clen);
  218. p = (p+clen);
  219. }
  220. else{
  221. p = (pe-1);
  222. }
  223. }
  224. //%%write eof;
  225. state->cs = cs;
  226. state->p = p;
  227. state->pe = pe;
  228. state->eof = eof;
  229. state->eoh = eoh;
  230. }