tsip_parser_header_Record_Route.rl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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_Record_Route.c
  23. * @brief SIP Record-Route header.
  24. *
  25. * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
  26. *
  27. */
  28. #include "tinysip/headers/tsip_header_Record_Route.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_Record_Route;
  39. # Includes
  40. include tsip_machine_utils "./ragel/tsip_machine_utils.rl";
  41. action tag{
  42. tag_start = p;
  43. }
  44. action create_route{
  45. if(!curr_route){
  46. curr_route = tsip_header_Record_Route_create_null();
  47. }
  48. }
  49. action parse_display_name{
  50. if(curr_route){
  51. TSK_PARSER_SET_STRING(curr_route->display_name);
  52. tsk_strunquote(&curr_route->display_name);
  53. }
  54. }
  55. action parse_uri{
  56. if(curr_route && !curr_route->uri){
  57. int len = (int)(p - tag_start);
  58. if(curr_route && !curr_route->uri){
  59. if((curr_route->uri = tsip_uri_parse(tag_start, (tsk_size_t)len)) && curr_route->display_name){
  60. curr_route->uri->display_name = tsk_strdup(curr_route->display_name);
  61. }
  62. }
  63. }
  64. }
  65. action parse_param{
  66. if(curr_route){
  67. TSK_PARSER_ADD_PARAM(TSIP_HEADER_PARAMS(curr_route));
  68. }
  69. }
  70. action add_route{
  71. if(curr_route){
  72. tsk_list_push_back_data(hdr_record_routes, ((void**) &curr_route));
  73. }
  74. }
  75. action eob{
  76. }
  77. URI = (scheme HCOLON any+)>tag %parse_uri;
  78. display_name = (( token LWS )+ | quoted_string)>tag %parse_display_name;
  79. my_name_addr = display_name? :>LAQUOT<: URI :>RAQUOT;
  80. rr_param = (generic_param)>tag %parse_param;
  81. rec_route = ( my_name_addr (SEMI rr_param)* ) >create_route %add_route;
  82. Record_Route = "Record-Route"i HCOLON rec_route (COMMA rec_route)*;
  83. # Entry point
  84. main := Record_Route :>CRLF @eob;
  85. }%%
  86. tsip_header_Record_Route_t* tsip_header_Record_Route_create(const tsip_uri_t* uri)
  87. {
  88. return tsk_object_new(TSIP_HEADER_RECORD_ROUTE_VA_ARGS(uri));
  89. }
  90. tsip_header_Record_Route_t* tsip_header_Record_Route_create_null()
  91. {
  92. return tsip_header_Record_Route_create(tsk_null);
  93. }
  94. int tsip_header_Record_Route_serialize(const tsip_header_t* header, tsk_buffer_t* output)
  95. {
  96. if(header){
  97. int ret;
  98. const tsip_header_Record_Route_t *Record_Route = (const tsip_header_Record_Route_t *)header;
  99. /* Uri with hacked display-name*/
  100. if((ret = tsip_uri_serialize(Record_Route->uri, tsk_true, tsk_true, output))){
  101. return ret;
  102. }
  103. return ret;
  104. }
  105. return -1;
  106. }
  107. tsip_header_Record_Routes_L_t* tsip_header_Record_Route_parse(const char *data, tsk_size_t size)
  108. {
  109. int cs = 0;
  110. const char *p = data;
  111. const char *pe = p + size;
  112. const char *eof = pe;
  113. tsip_header_Record_Routes_L_t *hdr_record_routes = tsk_list_create();
  114. tsip_header_Record_Route_t *curr_route = tsk_null;
  115. const char *tag_start = tsk_null;
  116. %%write data;
  117. (void)(eof);
  118. (void)(tsip_machine_parser_header_Record_Route_first_final);
  119. (void)(tsip_machine_parser_header_Record_Route_error);
  120. (void)(tsip_machine_parser_header_Record_Route_en_main);
  121. %%write init;
  122. %%write exec;
  123. if( cs < %%{ write first_final; }%% ){
  124. TSK_DEBUG_ERROR("Failed to parse 'Record-Route' header.");
  125. TSK_OBJECT_SAFE_FREE(curr_route);
  126. TSK_OBJECT_SAFE_FREE(hdr_record_routes);
  127. }
  128. return hdr_record_routes;
  129. }
  130. //========================================================
  131. // Record_Route header object definition
  132. //
  133. static tsk_object_t* tsip_header_Record_Route_ctor(tsk_object_t *self, va_list * app)
  134. {
  135. tsip_header_Record_Route_t *Record_Route = self;
  136. if(Record_Route){
  137. const tsip_uri_t* uri = va_arg(*app, const tsip_uri_t *);
  138. if(uri){
  139. Record_Route->uri = tsk_object_ref((void*)uri);
  140. }
  141. TSIP_HEADER(Record_Route)->type = tsip_htype_Record_Route;
  142. TSIP_HEADER(Record_Route)->serialize = tsip_header_Record_Route_serialize;
  143. }
  144. else{
  145. TSK_DEBUG_ERROR("Failed to create new Record_Route header.");
  146. }
  147. return self;
  148. }
  149. static tsk_object_t* tsip_header_Record_Route_dtor(tsk_object_t *self)
  150. {
  151. tsip_header_Record_Route_t *Record_Route = self;
  152. if(Record_Route){
  153. TSK_FREE(Record_Route->display_name);
  154. TSK_OBJECT_SAFE_FREE(Record_Route->uri);
  155. TSK_OBJECT_SAFE_FREE(TSIP_HEADER_PARAMS(Record_Route));
  156. }
  157. else{
  158. TSK_DEBUG_ERROR("Null Record_Route header.");
  159. }
  160. return self;
  161. }
  162. static const tsk_object_def_t tsip_header_Record_Route_def_s =
  163. {
  164. sizeof(tsip_header_Record_Route_t),
  165. tsip_header_Record_Route_ctor,
  166. tsip_header_Record_Route_dtor,
  167. tsk_null
  168. };
  169. const tsk_object_def_t *tsip_header_Record_Route_def_t = &tsip_header_Record_Route_def_s;