tsip_parser_header_Refer_Sub.rl 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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_Refer_Sub.c
  23. * @brief SIP header 'Refer-Sub' as per 4488.
  24. *
  25. * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
  26. *
  27. */
  28. #include "tinysip/headers/tsip_header_Refer_Sub.h"
  29. #include "tinysip/parsers/tsip_parser_uri.h"
  30. #include "tsk_debug.h"
  31. #include "tsk_memory.h"
  32. #include <string.h>
  33. /***********************************
  34. * Ragel state machine.
  35. */
  36. %%{
  37. machine tsip_machine_parser_header_Refer_Sub;
  38. # Includes
  39. include tsip_machine_utils "./ragel/tsip_machine_utils.rl";
  40. action tag{
  41. tag_start = p;
  42. }
  43. action is_true{
  44. hdr_rsub->sub = tsk_true;
  45. }
  46. action is_false{
  47. hdr_rsub->sub = tsk_false;
  48. }
  49. action parse_param{
  50. TSK_PARSER_ADD_PARAM(TSIP_HEADER_PARAMS(hdr_rsub));
  51. }
  52. action eob{
  53. }
  54. refer_sub_value = "true"i>tag %is_true | "false"i>tag %is_false;
  55. exten = generic_param>tag %parse_param;
  56. Refer_Sub = "Refer-Sub"i HCOLON refer_sub_value ( SEMI exten )*;
  57. # Entry point
  58. main := Refer_Sub :>CRLF @eob;
  59. }%%
  60. tsip_header_Refer_Sub_t* tsip_header_Refer_Sub_create(tsk_bool_t sub)
  61. {
  62. return tsk_object_new(TSIP_HEADER_REFER_SUB_VA_ARGS(sub));
  63. }
  64. tsip_header_Refer_Sub_t* tsip_header_Refer_Sub_create_null()
  65. {
  66. return tsip_header_Refer_Sub_create(tsk_true);
  67. }
  68. int tsip_header_Refer_Sub_serialize(const tsip_header_t* header, tsk_buffer_t* output)
  69. {
  70. if(header){
  71. const tsip_header_Refer_Sub_t *Refer_Sub = (const tsip_header_Refer_Sub_t *)header;
  72. return tsk_buffer_append(output, Refer_Sub->sub ? "true" : "false", Refer_Sub->sub ? 4 : 5);
  73. }
  74. return -1;
  75. }
  76. tsip_header_Refer_Sub_t *tsip_header_Refer_Sub_parse(const char *data, tsk_size_t size)
  77. {
  78. int cs = 0;
  79. const char *p = data;
  80. const char *pe = p + size;
  81. const char *eof = pe;
  82. tsip_header_Refer_Sub_t *hdr_rsub = tsip_header_Refer_Sub_create_null();
  83. const char *tag_start = tsk_null;
  84. %%write data;
  85. (void)(eof);
  86. (void)(tsip_machine_parser_header_Refer_Sub_first_final);
  87. (void)(tsip_machine_parser_header_Refer_Sub_error);
  88. (void)(tsip_machine_parser_header_Refer_Sub_en_main);
  89. %%write init;
  90. %%write exec;
  91. if( cs < %%{ write first_final; }%% ){
  92. TSK_DEBUG_ERROR("Failed to parse 'Refer-Sub' header.");
  93. TSK_OBJECT_SAFE_FREE(hdr_rsub);
  94. }
  95. return hdr_rsub;
  96. }
  97. //========================================================
  98. // Refer_Sub header object definition
  99. //
  100. static tsk_object_t* tsip_header_Refer_Sub_ctor(tsk_object_t *self, va_list * app)
  101. {
  102. tsip_header_Refer_Sub_t *Refer_Sub = self;
  103. if(Refer_Sub){
  104. TSIP_HEADER(Refer_Sub)->type = tsip_htype_Refer_Sub;
  105. TSIP_HEADER(Refer_Sub)->serialize = tsip_header_Refer_Sub_serialize;
  106. Refer_Sub->sub = va_arg(*app, tsk_bool_t);
  107. }
  108. else{
  109. TSK_DEBUG_ERROR("Failed to create new Refer_Sub header.");
  110. }
  111. return self;
  112. }
  113. static tsk_object_t* tsip_header_Refer_Sub_dtor(tsk_object_t *self)
  114. {
  115. tsip_header_Refer_Sub_t *Refer_Sub = self;
  116. if(Refer_Sub){
  117. TSK_OBJECT_SAFE_FREE(TSIP_HEADER_PARAMS(Refer_Sub));
  118. }
  119. else{
  120. TSK_DEBUG_ERROR("Null Refer_Sub header.");
  121. }
  122. return self;
  123. }
  124. static const tsk_object_def_t tsip_header_Refer_Sub_def_s =
  125. {
  126. sizeof(tsip_header_Refer_Sub_t),
  127. tsip_header_Refer_Sub_ctor,
  128. tsip_header_Refer_Sub_dtor,
  129. tsk_null
  130. };
  131. const tsk_object_def_t *tsip_header_Refer_Sub_def_t = &tsip_header_Refer_Sub_def_s;