tsip_parser_header_Warning.rl 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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_header_Warning.c
  23. * @brief SIP Warning header.
  24. *
  25. * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
  26. *
  27. */
  28. #include "tinysip/headers/tsip_header_Warning.h"
  29. #include "tinysip/parsers/tsip_parser_uri.h"
  30. #include "tsk_debug.h"
  31. #include "tsk_memory.h"
  32. #include "tsk_time.h"
  33. #include <string.h>
  34. /***********************************
  35. * Ragel state machine.
  36. */
  37. %%{
  38. machine tsip_machine_parser_header_Warning;
  39. # Includes
  40. include tsip_machine_utils "./ragel/tsip_machine_utils.rl";
  41. action tag{
  42. tag_start = p;
  43. }
  44. action create_warning{
  45. if(!curr_warning){
  46. curr_warning = tsip_header_Warning_create();
  47. }
  48. }
  49. action parse_agent{
  50. if(curr_warning){
  51. TSK_PARSER_SET_STRING(curr_warning->agent);
  52. }
  53. }
  54. action parse_text{
  55. if(curr_warning){
  56. TSK_PARSER_SET_STRING(curr_warning->text);
  57. }
  58. }
  59. action parse_code{
  60. if(curr_warning){
  61. TSK_PARSER_SET_INTEGER(curr_warning->code);
  62. }
  63. }
  64. action add_warning{
  65. if(curr_warning){
  66. tsk_list_push_back_data(hdr_warnings, ((void**) &curr_warning));
  67. }
  68. }
  69. action eob{
  70. }
  71. warn_code = DIGIT{3};
  72. pseudonym = token;
  73. warn_agent = hostport | pseudonym;
  74. warn_text = quoted_string;
  75. warning_value = (warn_code>tag %parse_code :>SP warn_agent>tag %parse_agent :>SP warn_text>tag %parse_text) >create_warning %add_warning;
  76. Warning = "Warning"i HCOLON warning_value ( COMMA warning_value )*;
  77. # Entry point
  78. main := Warning :>CRLF @eob;
  79. }%%
  80. tsip_header_Warning_t* tsip_header_Warning_create()
  81. {
  82. return tsk_object_new(tsip_header_Warning_def_t);
  83. }
  84. int tsip_header_Warning_serialize(const tsip_header_t* header, tsk_buffer_t* output)
  85. {
  86. if(header){
  87. const tsip_header_Warning_t *Warning = (const tsip_header_Warning_t *)header;
  88. return tsk_buffer_append_2(output, "%d %s %s",
  89. Warning->code, Warning->agent, Warning->text); /* warn-code SP warn-agent SP warn-text */
  90. }
  91. return -1;
  92. }
  93. tsip_header_Warnings_L_t *tsip_header_Warning_parse(const char *data, tsk_size_t size)
  94. {
  95. int cs = 0;
  96. const char *p = data;
  97. const char *pe = p + size;
  98. const char *eof = pe;
  99. tsip_header_Warnings_L_t *hdr_warnings = tsk_list_create();
  100. const char *tag_start = tsk_null;
  101. tsip_header_Warning_t *curr_warning = 0;
  102. %%write data;
  103. (void)(eof);
  104. (void)(tsip_machine_parser_header_Warning_first_final);
  105. (void)(tsip_machine_parser_header_Warning_error);
  106. (void)(tsip_machine_parser_header_Warning_en_main);
  107. %%write init;
  108. %%write exec;
  109. if( cs < %%{ write first_final; }%% ){
  110. TSK_DEBUG_ERROR("Failed to parse 'Warning' header.");
  111. TSK_OBJECT_SAFE_FREE(curr_warning);
  112. TSK_OBJECT_SAFE_FREE(hdr_warnings);
  113. }
  114. return hdr_warnings;
  115. }
  116. //========================================================
  117. // Warning header object definition
  118. //
  119. static tsk_object_t* tsip_header_Warning_ctor(tsk_object_t *self, va_list * app)
  120. {
  121. tsip_header_Warning_t *Warning = self;
  122. if(Warning){
  123. TSIP_HEADER(Warning)->type = tsip_htype_Warning;
  124. TSIP_HEADER(Warning)->serialize = tsip_header_Warning_serialize;
  125. Warning->code = -1;
  126. }
  127. else{
  128. TSK_DEBUG_ERROR("Failed to create new Warning header.");
  129. }
  130. return self;
  131. }
  132. static tsk_object_t* tsip_header_Warning_dtor(tsk_object_t *self)
  133. {
  134. tsip_header_Warning_t *Warning = self;
  135. if(Warning){
  136. TSK_FREE(Warning->agent);
  137. TSK_FREE(Warning->text);
  138. TSK_OBJECT_SAFE_FREE(TSIP_HEADER_PARAMS(Warning));
  139. }
  140. else{
  141. TSK_DEBUG_ERROR("Null Warning header.");
  142. }
  143. return self;
  144. }
  145. static const tsk_object_def_t tsip_header_Warning_def_s =
  146. {
  147. sizeof(tsip_header_Warning_t),
  148. tsip_header_Warning_ctor,
  149. tsip_header_Warning_dtor,
  150. tsk_null
  151. };
  152. const tsk_object_def_t *tsip_header_Warning_def_t = &tsip_header_Warning_def_s;