tsip_parser_header_P_Asserted_Identity.rl 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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_P_Asserted_Identity.c
  23. * @brief SIP P-Asserted-Identity header as per RFC 3325.
  24. *
  25. * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
  26. *
  27. */
  28. #include "tinysip/headers/tsip_header_P_Asserted_Identity.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_P_Asserted_Identity;
  39. # Includes
  40. include tsip_machine_utils "./ragel/tsip_machine_utils.rl";
  41. action tag{
  42. tag_start = p;
  43. }
  44. action create_p_asserted_identity{
  45. if(!curr_p_asserted_identity){
  46. curr_p_asserted_identity = tsip_header_P_Asserted_Identity_create();
  47. }
  48. }
  49. action parse_display_name{
  50. if(curr_p_asserted_identity){
  51. TSK_PARSER_SET_STRING(curr_p_asserted_identity->display_name);
  52. tsk_strunquote(&curr_p_asserted_identity->display_name);
  53. }
  54. }
  55. action parse_uri{
  56. if(curr_p_asserted_identity && !curr_p_asserted_identity->uri){
  57. int len = (int)(p - tag_start);
  58. if(curr_p_asserted_identity && !curr_p_asserted_identity->uri){
  59. if((curr_p_asserted_identity->uri = tsip_uri_parse(tag_start, (tsk_size_t)len)) && curr_p_asserted_identity->display_name){
  60. curr_p_asserted_identity->uri->display_name = tsk_strdup(curr_p_asserted_identity->display_name);
  61. }
  62. }
  63. }
  64. }
  65. action add_p_asserted_identity{
  66. if(curr_p_asserted_identity){
  67. tsk_list_push_back_data(hdr_p_asserted_identities, ((void**) &curr_p_asserted_identity));
  68. }
  69. }
  70. action eob{
  71. }
  72. URI = (scheme HCOLON any+)>tag %parse_uri;
  73. display_name = (( token LWS )+ | quoted_string)>tag %parse_display_name;
  74. my_name_addr = display_name? :>LAQUOT<: URI :>RAQUOT;
  75. PAssertedID_value = (my_name_addr | URI) >create_p_asserted_identity %add_p_asserted_identity;
  76. PAssertedID = "P-Asserted-Identity"i HCOLON PAssertedID_value ( COMMA PAssertedID_value )*;
  77. P_Asserted_Identity = PAssertedID;
  78. # Entry point
  79. main := P_Asserted_Identity :>CRLF @eob;
  80. }%%
  81. tsip_header_P_Asserted_Identity_t* tsip_header_P_Asserted_Identity_create()
  82. {
  83. return tsk_object_new(tsip_header_P_Asserted_Identity_def_t);
  84. }
  85. int tsip_header_P_Asserted_Identity_serialize(const tsip_header_t* header, tsk_buffer_t* output)
  86. {
  87. if(header){
  88. const tsip_header_P_Asserted_Identity_t *P_Asserted_Identity = (const tsip_header_P_Asserted_Identity_t *)header;
  89. int ret = 0;
  90. /* Uri with hacked display-name*/
  91. if((ret = tsip_uri_serialize(P_Asserted_Identity->uri, tsk_true, tsk_true, output))){
  92. return ret;
  93. }
  94. return ret;
  95. }
  96. return -1;
  97. }
  98. tsip_header_P_Asserted_Identities_L_t *tsip_header_P_Asserted_Identity_parse(const char *data, tsk_size_t size)
  99. {
  100. int cs = 0;
  101. const char *p = data;
  102. const char *pe = p + size;
  103. const char *eof = pe;
  104. tsip_header_P_Asserted_Identities_L_t *hdr_p_asserted_identities = tsk_list_create();
  105. const char *tag_start = tsk_null;
  106. tsip_header_P_Asserted_Identity_t *curr_p_asserted_identity = 0;
  107. %%write data;
  108. (void)(eof);
  109. (void)(tsip_machine_parser_header_P_Asserted_Identity_first_final);
  110. (void)(tsip_machine_parser_header_P_Asserted_Identity_error);
  111. (void)(tsip_machine_parser_header_P_Asserted_Identity_en_main);
  112. %%write init;
  113. %%write exec;
  114. if( cs < %%{ write first_final; }%% ){
  115. TSK_DEBUG_ERROR("Failed to parse 'P-Asserted-Identity' header.");
  116. TSK_OBJECT_SAFE_FREE(curr_p_asserted_identity);
  117. TSK_OBJECT_SAFE_FREE(hdr_p_asserted_identities);
  118. }
  119. return hdr_p_asserted_identities;
  120. }
  121. //========================================================
  122. // P_Asserted_Identity header object definition
  123. //
  124. /**@ingroup tsip_header_P_Asserted_Identity_group
  125. */
  126. static tsk_object_t* tsip_header_P_Asserted_Identity_ctor(tsk_object_t *self, va_list * app)
  127. {
  128. tsip_header_P_Asserted_Identity_t *P_Asserted_Identity = self;
  129. if(P_Asserted_Identity){
  130. TSIP_HEADER(P_Asserted_Identity)->type = tsip_htype_P_Asserted_Identity;
  131. TSIP_HEADER(P_Asserted_Identity)->serialize = tsip_header_P_Asserted_Identity_serialize;
  132. }
  133. else{
  134. TSK_DEBUG_ERROR("Failed to create new P_Asserted_Identity header.");
  135. }
  136. return self;
  137. }
  138. /**@ingroup tsip_header_P_Asserted_Identity_group
  139. */
  140. static tsk_object_t* tsip_header_P_Asserted_Identity_dtor(tsk_object_t *self)
  141. {
  142. tsip_header_P_Asserted_Identity_t *P_Asserted_Identity = self;
  143. if(P_Asserted_Identity){
  144. TSK_FREE(P_Asserted_Identity->display_name);
  145. TSK_OBJECT_SAFE_FREE(P_Asserted_Identity->uri);
  146. TSK_OBJECT_SAFE_FREE(TSIP_HEADER_PARAMS(P_Asserted_Identity));
  147. }
  148. else{
  149. TSK_DEBUG_ERROR("Null P_Asserted_Identity header.");
  150. }
  151. return self;
  152. }
  153. static const tsk_object_def_t tsip_header_P_Asserted_Identity_def_s =
  154. {
  155. sizeof(tsip_header_P_Asserted_Identity_t),
  156. tsip_header_P_Asserted_Identity_ctor,
  157. tsip_header_P_Asserted_Identity_dtor,
  158. tsk_null
  159. };
  160. const tsk_object_def_t *tsip_header_P_Asserted_Identity_def_t = &tsip_header_P_Asserted_Identity_def_s;