tsip_parser_header_Contact.rl 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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_Contact.c
  23. * @brief SIP Contact/m header.
  24. *
  25. * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
  26. *
  27. */
  28. #include "tinysip/headers/tsip_header_Contact.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_Contact;
  37. # Includes
  38. include tsip_machine_utils "./ragel/tsip_machine_utils.rl";
  39. action tag{
  40. tag_start = p;
  41. }
  42. action create_contact{
  43. if(!curr_contact){
  44. curr_contact = tsip_header_Contact_create();
  45. }
  46. }
  47. action parse_display_name{
  48. if(curr_contact){
  49. TSK_PARSER_SET_STRING(curr_contact->display_name);
  50. tsk_strunquote(&curr_contact->display_name);
  51. }
  52. }
  53. action parse_uri{
  54. if(curr_contact && !curr_contact->uri){
  55. int len = (int)(p - tag_start);
  56. if((curr_contact->uri = tsip_uri_parse(tag_start, (tsk_size_t)len)) && curr_contact->display_name){
  57. curr_contact->uri->display_name = tsk_strdup(curr_contact->display_name);
  58. }
  59. }
  60. }
  61. action parse_expires{
  62. if(curr_contact){
  63. TSK_PARSER_SET_INTEGER(curr_contact->expires);
  64. }
  65. }
  66. action parse_param{
  67. if(curr_contact){
  68. TSK_PARSER_ADD_PARAM(TSIP_HEADER_PARAMS(curr_contact));
  69. }
  70. }
  71. action add_contact{
  72. if(curr_contact){
  73. tsk_list_push_back_data(hdr_contacts, ((void**) &curr_contact));
  74. }
  75. }
  76. action eob{
  77. }
  78. URI = (scheme HCOLON any+)>tag %parse_uri;
  79. display_name = (( token LWS )+ | quoted_string)>tag %parse_display_name;
  80. my_name_addr = display_name? :>LAQUOT<: URI :>RAQUOT;
  81. c_p_expires = "expires"i EQUAL delta_seconds>tag %parse_expires;
  82. contact_extension = (generic_param)>tag %parse_param;
  83. contact_params = c_p_expires@1 | contact_extension@0;
  84. contact_param = ( (my_name_addr | URI) :> (SEMI contact_params)* ) >create_contact %add_contact;
  85. Contact = ( "Contact"i | "m"i ) HCOLON ( STAR | ( contact_param ( COMMA contact_param )* ) );
  86. # Entry point
  87. main := Contact :>CRLF @eob;
  88. }%%
  89. tsip_header_Contact_t* tsip_header_Contact_create()
  90. {
  91. return tsk_object_new(tsip_header_Contact_def_t);
  92. }
  93. int tsip_header_Contact_serialize(const tsip_header_t* header, tsk_buffer_t* output)
  94. {
  95. if(header){
  96. const tsip_header_Contact_t *Contact = (const tsip_header_Contact_t *)header;
  97. int ret = 0;
  98. /* Uri with hacked display-name*/
  99. if((ret = tsip_uri_serialize(Contact->uri, tsk_true, tsk_true, output))){
  100. return ret;
  101. }
  102. /* Expires */
  103. if(Contact->expires >=0){
  104. tsk_buffer_append_2(output, ";expires=%lld", Contact->expires);
  105. }
  106. return ret;
  107. }
  108. return -1;
  109. }
  110. tsip_header_Contacts_L_t *tsip_header_Contact_parse(const char *data, tsk_size_t size)
  111. {
  112. int cs = 0;
  113. const char *p = data;
  114. const char *pe = p + size;
  115. const char *eof = pe;
  116. tsip_header_Contacts_L_t *hdr_contacts = tsk_list_create();
  117. const char *tag_start = tsk_null;
  118. tsip_header_Contact_t *curr_contact = 0;
  119. %%write data;
  120. (void)(eof);
  121. (void)(tsip_machine_parser_header_Contact_first_final);
  122. (void)(tsip_machine_parser_header_Contact_error);
  123. (void)(tsip_machine_parser_header_Contact_en_main);
  124. %%write init;
  125. %%write exec;
  126. if( cs < %%{ write first_final; }%% ){
  127. TSK_DEBUG_ERROR("Failed to parse SIP 'Contact' header.");
  128. TSK_OBJECT_SAFE_FREE(curr_contact);
  129. TSK_OBJECT_SAFE_FREE(hdr_contacts);
  130. }
  131. return hdr_contacts;
  132. }
  133. //========================================================
  134. // Contact header object definition
  135. //
  136. static tsk_object_t* tsip_header_Contact_ctor(tsk_object_t *self, va_list * app)
  137. {
  138. tsip_header_Contact_t *Contact = self;
  139. if(Contact){
  140. TSIP_HEADER(Contact)->type = tsip_htype_Contact;
  141. TSIP_HEADER(Contact)->serialize = tsip_header_Contact_serialize;
  142. Contact->expires = -1;
  143. }
  144. else{
  145. TSK_DEBUG_ERROR("Failed to create new Contact header.");
  146. }
  147. return self;
  148. }
  149. static tsk_object_t* tsip_header_Contact_dtor(tsk_object_t *self)
  150. {
  151. tsip_header_Contact_t *Contact = self;
  152. if(Contact){
  153. TSK_FREE(Contact->display_name);
  154. TSK_OBJECT_SAFE_FREE(Contact->uri);
  155. TSK_OBJECT_SAFE_FREE(TSIP_HEADER_PARAMS(Contact));
  156. }
  157. else{
  158. TSK_DEBUG_ERROR("Null Contact header.");
  159. }
  160. return self;
  161. }
  162. static const tsk_object_def_t tsip_header_Contact_def_s =
  163. {
  164. sizeof(tsip_header_Contact_t),
  165. tsip_header_Contact_ctor,
  166. tsip_header_Contact_dtor,
  167. tsk_null
  168. };
  169. const tsk_object_def_t *tsip_header_Contact_def_t = &tsip_header_Contact_def_s;
  170. ////========================================================
  171. //// Contact object definition
  172. ////
  173. //
  174. ///**@ingroup tsip_header_Contact_group
  175. //*/
  176. //static tsk_object_t* tsip_contact_ctor(tsk_object_t *self, va_list * app)
  177. //{
  178. // tsip_contact_t *contact = self;
  179. // if(contact)
  180. // {
  181. // contact->expires = -1;
  182. // }
  183. // else
  184. // {
  185. // TSK_DEBUG_ERROR("Failed to create new Contact object.");
  186. // }
  187. // return self;
  188. //}
  189. //
  190. //static tsk_object_t* tsip_contact_dtor(tsk_object_t *self)
  191. //{
  192. // tsip_contact_t *contact = self;
  193. // if(contact)
  194. // {
  195. // TSK_FREE(contact->display_name);
  196. // TSK_OBJECT_SAFE_FREE(TSIP_HEADER_PARAMS(contact));
  197. //
  198. // TSK_OBJECT_SAFE_FREE(contact->uri);
  199. // }
  200. // else TSK_DEBUG_ERROR("Null Contact object.");
  201. //
  202. // return self;
  203. //}
  204. //
  205. //static const tsk_object_def_t tsip_contact_def_s =
  206. //{
  207. // sizeof(tsip_contact_t),
  208. // tsip_contact_ctor,
  209. // tsip_contact_dtor,
  210. // 0
  211. //};
  212. //const void *tsip_contact_def_t = &tsip_contact_def_s;