tsip_parser_header_P_Preferred_Identity.rl 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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_Preferred_Identity.c
  23. * @brief SIP P-Preferred-Identity header as per RFC 3325.
  24. * Header field where proxy ACK BYE CAN INV OPT REG
  25. * ------------ ----- ----- --- --- --- --- --- ---
  26. * P-Preferred-Identity adr - o - o o -
  27. *
  28. *
  29. * SUB NOT REF INF UPD PRA
  30. * --- --- --- --- --- ---
  31. * o o o - - -
  32. *
  33. * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
  34. *
  35. */
  36. #include "tinysip/headers/tsip_header_P_Preferred_Identity.h"
  37. #include "tinysip/parsers/tsip_parser_uri.h"
  38. #include "tsk_debug.h"
  39. #include "tsk_memory.h"
  40. /***********************************
  41. * Ragel state machine.
  42. */
  43. %%{
  44. machine tsip_machine_parser_header_P_Preferred_Identity;
  45. # Includes
  46. include tsip_machine_utils "./ragel/tsip_machine_utils.rl";
  47. action tag{
  48. tag_start = p;
  49. }
  50. action parse_uri{
  51. if(!hdr_pi->uri) /* Only one URI */{
  52. int len = (int)(p - tag_start);
  53. if(hdr_pi && !hdr_pi->uri){
  54. if((hdr_pi->uri = tsip_uri_parse(tag_start, (tsk_size_t)len)) && hdr_pi->display_name){
  55. hdr_pi->uri->display_name = tsk_strdup(hdr_pi->display_name);
  56. }
  57. }
  58. }
  59. }
  60. action parse_display_name{
  61. if(!hdr_pi->display_name){
  62. TSK_PARSER_SET_STRING(hdr_pi->display_name);
  63. tsk_strunquote(&hdr_pi->display_name);
  64. }
  65. }
  66. action eob{
  67. }
  68. #/* FIXME: Only one URI is added --> According to the ABNF the header could have more than one URI. */
  69. URI = (scheme HCOLON any+)>tag %parse_uri;
  70. display_name = (( token LWS )+ | quoted_string)>tag %parse_display_name;
  71. my_name_addr = display_name? :>LAQUOT<: URI :>RAQUOT;
  72. PPreferredID_value = (my_name_addr)>0 | (URI)>1;
  73. PPreferredID = "P-Preferred-Identity"i HCOLON PPreferredID_value>1 ( COMMA PPreferredID_value )*>0;
  74. # Entry point
  75. main := PPreferredID :>CRLF @eob;
  76. }%%
  77. tsip_header_P_Preferred_Identity_t* tsip_header_P_Preferred_Identity_create(const tsip_uri_t* uri)
  78. {
  79. return tsk_object_new(TSIP_HEADER_P_PREFERRED_IDENTITY_VA_ARGS(uri));
  80. }
  81. tsip_header_P_Preferred_Identity_t* tsip_header_P_Preferred_Identity_create_null()
  82. {
  83. return tsip_header_P_Preferred_Identity_create(tsk_null);
  84. }
  85. int tsip_header_Preferred_Identity_serialize(const tsip_header_t* header, tsk_buffer_t* output)
  86. {
  87. if(header){
  88. int ret;
  89. const tsip_header_P_Preferred_Identity_t *P_Preferred_Identity = (const tsip_header_P_Preferred_Identity_t *)header;
  90. /* Uri with hacked display-name*/
  91. if((ret = tsip_uri_serialize(P_Preferred_Identity->uri, tsk_true, tsk_true, output))){
  92. return ret;
  93. }
  94. return ret;
  95. }
  96. return -1;
  97. }
  98. tsip_header_P_Preferred_Identity_t *tsip_header_P_Preferred_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_Preferred_Identity_t *hdr_pi = tsip_header_P_Preferred_Identity_create_null();
  105. const char *tag_start = tsk_null;
  106. %%write data;
  107. (void)(eof);
  108. (void)(tsip_machine_parser_header_P_Preferred_Identity_first_final);
  109. (void)(tsip_machine_parser_header_P_Preferred_Identity_error);
  110. (void)(tsip_machine_parser_header_P_Preferred_Identity_en_main);
  111. %%write init;
  112. %%write exec;
  113. if( cs < %%{ write first_final; }%% ){
  114. TSK_DEBUG_ERROR("Failed to parse 'P-Preferred-Identity' header.");
  115. TSK_OBJECT_SAFE_FREE(hdr_pi);
  116. }
  117. return hdr_pi;
  118. }
  119. //========================================================
  120. // P_Preferred_Identity header object definition
  121. //
  122. static tsk_object_t* tsip_header_P_Preferred_Identity_ctor(tsk_object_t *self, va_list * app)
  123. {
  124. tsip_header_P_Preferred_Identity_t *P_Preferred_Identity = self;
  125. if(P_Preferred_Identity){
  126. const tsip_uri_t* uri = va_arg(*app, const tsip_uri_t*);
  127. TSIP_HEADER(P_Preferred_Identity)->type = tsip_htype_P_Preferred_Identity;
  128. TSIP_HEADER(P_Preferred_Identity)->serialize = tsip_header_Preferred_Identity_serialize;
  129. if(uri){
  130. P_Preferred_Identity->uri = tsk_object_ref((void*)uri);
  131. }
  132. }
  133. else{
  134. TSK_DEBUG_ERROR("Failed to create new P_Preferred_Identity header.");
  135. }
  136. return self;
  137. }
  138. static tsk_object_t* tsip_header_P_Preferred_Identity_dtor(tsk_object_t *self)
  139. {
  140. tsip_header_P_Preferred_Identity_t *P_Preferred_Identity = self;
  141. if(P_Preferred_Identity){
  142. TSK_FREE(P_Preferred_Identity->display_name);
  143. TSK_OBJECT_SAFE_FREE(P_Preferred_Identity->uri);
  144. }
  145. else{
  146. TSK_DEBUG_ERROR("Null P_Preferred_Identity header.");
  147. }
  148. return self;
  149. }
  150. static const tsk_object_def_t tsip_header_P_Preferred_Identity_def_s =
  151. {
  152. sizeof(tsip_header_P_Preferred_Identity_t),
  153. tsip_header_P_Preferred_Identity_ctor,
  154. tsip_header_P_Preferred_Identity_dtor,
  155. tsk_null
  156. };
  157. const tsk_object_def_t *tsip_header_P_Preferred_Identity_def_t = &tsip_header_P_Preferred_Identity_def_s;