tsip_parser_header_Referred_By.rl 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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_Referred_By.c
  23. * @brief SIP Referred-By header as per RFC 3892.
  24. *
  25. * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
  26. *
  27. */
  28. #include "tinysip/headers/tsip_header_Referred_By.h"
  29. #include "tinysip/parsers/tsip_parser_uri.h"
  30. #include "tsk_debug.h"
  31. #include "tsk_memory.h"
  32. /***********************************
  33. * Ragel state machine.
  34. */
  35. %%{
  36. machine tsip_machine_parser_header_Referred_By;
  37. # Includes
  38. include tsip_machine_utils "./ragel/tsip_machine_utils.rl";
  39. action tag{
  40. tag_start = p;
  41. }
  42. action parse_uri{
  43. if(!r_by->uri) /* Only one URI */{
  44. int len = (int)(p - tag_start);
  45. if(r_by && !r_by->uri){
  46. if((r_by->uri = tsip_uri_parse(tag_start, (tsk_size_t)len)) && r_by->display_name){
  47. r_by->uri->display_name = tsk_strdup(r_by->display_name);
  48. }
  49. }
  50. }
  51. }
  52. action parse_display_name{
  53. if(!r_by->display_name){
  54. TSK_PARSER_SET_STRING(r_by->display_name);
  55. tsk_strunquote(&r_by->display_name);
  56. }
  57. }
  58. action parse_cid{
  59. TSK_PARSER_SET_STRING(r_by->cid);
  60. }
  61. action parse_param{
  62. TSK_PARSER_ADD_PARAM(TSIP_HEADER_PARAMS(r_by));
  63. }
  64. action eob{
  65. }
  66. URI = (scheme HCOLON any+)>tag %parse_uri;
  67. display_name = (( token LWS )+ | quoted_string)>tag %parse_display_name;
  68. my_name_addr = display_name? :>LAQUOT<: URI :>RAQUOT;
  69. referrer_uri = ( my_name_addr>0 | URI>1 );
  70. atom = ( alphanum | "-" | "!" | "%" | "*" | "_" | "+" | "'" | "`" | "~" )+;
  71. dot_atom = atom ( "." atom )*;
  72. sip_clean_msg_id = LDQUOT dot_atom "@" ( dot_atom | host ) RDQUOT;
  73. referredby_id_param = "cid"i EQUAL sip_clean_msg_id>tag %parse_cid;
  74. Referred_By = ( "Referred-By"i | "b"i ) HCOLON referrer_uri ( SEMI ( referredby_id_param | generic_param>tag %parse_param ) )*;
  75. # Entry point
  76. main := Referred_By :>CRLF @eob;
  77. }%%
  78. tsip_header_Referred_By_t* tsip_header_Referred_By_create(const tsip_uri_t* uri, const char* cid)
  79. {
  80. return tsk_object_new(TSIP_HEADER_REFERRED_BY_VA_ARGS(uri, cid));
  81. }
  82. tsip_header_Referred_By_t* tsip_header_Referred_By_create_null()
  83. {
  84. return tsip_header_Referred_By_create(tsk_null, tsk_null);
  85. }
  86. int tsip_header_Referred_By_serialize(const tsip_header_t* header, tsk_buffer_t* output)
  87. {
  88. if(header){
  89. int ret;
  90. const tsip_header_Referred_By_t *Referred_By = (const tsip_header_Referred_By_t *)header;
  91. /* Uri with hacked display-name*/
  92. if((ret = tsip_uri_serialize(Referred_By->uri, tsk_true, tsk_true, output))){
  93. return ret;
  94. }
  95. /* cid */
  96. if(Referred_By->cid && (ret = tsk_buffer_append_2(output, ";cid=%s", Referred_By->cid))){
  97. return ret;
  98. }
  99. return ret;
  100. }
  101. return -1;
  102. }
  103. tsip_header_Referred_By_t *tsip_header_Referred_By_parse(const char *data, tsk_size_t size)
  104. {
  105. int cs = 0;
  106. const char *p = data;
  107. const char *pe = p + size;
  108. const char *eof = pe;
  109. tsip_header_Referred_By_t *r_by = tsip_header_Referred_By_create_null();
  110. const char *tag_start = tsk_null;
  111. %%write data;
  112. (void)(eof);
  113. (void)(tsip_machine_parser_header_Referred_By_first_final);
  114. (void)(tsip_machine_parser_header_Referred_By_error);
  115. (void)(tsip_machine_parser_header_Referred_By_en_main);
  116. %%write init;
  117. %%write exec;
  118. if( cs < %%{ write first_final; }%% ){
  119. TSK_DEBUG_ERROR("Failed to parse 'Referred-By' header.");
  120. TSK_OBJECT_SAFE_FREE(r_by);
  121. }
  122. return r_by;
  123. }
  124. //========================================================
  125. // Referred_By header object definition
  126. //
  127. static tsk_object_t* tsip_header_Referred_By_ctor(tsk_object_t *self, va_list * app)
  128. {
  129. tsip_header_Referred_By_t *Referred_By = self;
  130. if(Referred_By){
  131. const tsip_uri_t* uri = va_arg(*app, const tsip_uri_t*);
  132. const char* cid = va_arg(*app, const char*);
  133. TSIP_HEADER(Referred_By)->type = tsip_htype_Referred_By;
  134. TSIP_HEADER(Referred_By)->serialize = tsip_header_Referred_By_serialize;
  135. Referred_By->uri = tsk_object_ref((void*)uri);
  136. Referred_By->cid = tsk_strdup(cid);
  137. }
  138. else{
  139. TSK_DEBUG_ERROR("Failed to create new Referred_By header.");
  140. }
  141. return self;
  142. }
  143. static tsk_object_t* tsip_header_Referred_By_dtor(tsk_object_t *self)
  144. {
  145. tsip_header_Referred_By_t *Referred_By = self;
  146. if(Referred_By){
  147. TSK_FREE(Referred_By->display_name);
  148. TSK_OBJECT_SAFE_FREE(Referred_By->uri);
  149. TSK_FREE(Referred_By->cid);
  150. TSK_OBJECT_SAFE_FREE(TSIP_HEADER_PARAMS(Referred_By));
  151. }
  152. else{
  153. TSK_DEBUG_ERROR("Null Referred_By header.");
  154. }
  155. return self;
  156. }
  157. static const tsk_object_def_t tsip_header_Referred_By_def_s =
  158. {
  159. sizeof(tsip_header_Referred_By_t),
  160. tsip_header_Referred_By_ctor,
  161. tsip_header_Referred_By_dtor,
  162. tsk_null
  163. };
  164. const tsk_object_def_t *tsip_header_Referred_By_def_t = &tsip_header_Referred_By_def_s;