tsip_parser_header_Security_Client.rl 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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_Security_Client.c
  23. * @brief SIP Security-Client header as per RFC 3329.
  24. *
  25. * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
  26. *
  27. */
  28. #include "tinysip/headers/tsip_header_Security_Client.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_Security_Client;
  39. # Includes
  40. include tsip_machine_utils "./ragel/tsip_machine_utils.rl";
  41. action tag{
  42. tag_start = p;
  43. }
  44. action create_securityclient{
  45. if(!curr_securityclient){
  46. curr_securityclient = tsip_header_Security_Client_create_null();
  47. }
  48. }
  49. action add_securityclient{
  50. if(curr_securityclient){
  51. tsk_list_push_back_data(hdr_securityclients, ((void**) &curr_securityclient));
  52. }
  53. }
  54. action parse_mech{
  55. if(curr_securityclient){
  56. TSK_PARSER_SET_STRING(curr_securityclient->mech);
  57. }
  58. }
  59. action parse_port_s{
  60. if(curr_securityclient){
  61. TSK_PARSER_SET_INT(curr_securityclient->port_s);
  62. }
  63. }
  64. action parse_port_c{
  65. if(curr_securityclient){
  66. TSK_PARSER_SET_INT(curr_securityclient->port_c);
  67. }
  68. }
  69. action parse_spi_s{
  70. if(curr_securityclient){
  71. TSK_PARSER_SET_UINT(curr_securityclient->spi_s);
  72. }
  73. }
  74. action parse_spi_c{
  75. if(curr_securityclient){
  76. TSK_PARSER_SET_UINT(curr_securityclient->spi_c);
  77. }
  78. }
  79. action parse_ealg{
  80. if(curr_securityclient){
  81. TSK_PARSER_SET_STRING(curr_securityclient->ealg);
  82. }
  83. }
  84. action parse_alg{
  85. if(curr_securityclient){
  86. TSK_PARSER_SET_STRING(curr_securityclient->alg);
  87. }
  88. }
  89. action parse_prot{
  90. if(curr_securityclient){
  91. TSK_PARSER_SET_STRING(curr_securityclient->prot);
  92. }
  93. }
  94. action parse_preference{
  95. if(curr_securityclient){
  96. TSK_PARSER_SET_DOUBLE(curr_securityclient->q);
  97. }
  98. }
  99. action parse_param{
  100. if(curr_securityclient){
  101. TSK_PARSER_ADD_PARAM(TSIP_HEADER_PARAMS(curr_securityclient));
  102. }
  103. }
  104. action eob{
  105. }
  106. mech_extension = generic_param>tag %parse_param;
  107. port_s = "port-s"i EQUAL DIGIT+>tag %parse_port_s;
  108. port_c = "port-c"i EQUAL DIGIT+>tag %parse_port_c;
  109. spi_s = "spi-s"i EQUAL DIGIT+>tag %parse_spi_s;
  110. spi_c = "spi-c"i EQUAL DIGIT+>tag %parse_spi_c;
  111. ealg = "ealg"i EQUAL token>tag %parse_ealg;
  112. alg = "alg"i EQUAL token>tag %parse_alg;
  113. prot = "prot"i EQUAL token>tag %parse_prot;
  114. preference = "q"i EQUAL qvalue>tag %parse_preference;
  115. mech_parameters = (preference |prot | alg | ealg | spi_c | spi_s | port_c | port_s) @1 | mech_extension @0;
  116. mechanism_name = token>tag %parse_mech;
  117. sec_mechanism = (mechanism_name ( SEMI mech_parameters )*) >create_securityclient %add_securityclient;
  118. Security_Client = "Security-Client"i HCOLON sec_mechanism ( COMMA sec_mechanism )*;
  119. # Entry point
  120. main := Security_Client :>CRLF @eob;
  121. }%%
  122. tsip_header_Security_Client_t* tsip_header_Security_Client_create(const char* mech, const char* alg, const char* prot, const char* mod, const char* ealg, tnet_port_t port_c, tnet_port_t port_s, uint32_t spi_c, uint32_t spi_s)
  123. {
  124. return tsk_object_new(TSIP_HEADER_SECURITY_CLIENT_VA_ARGS(mech, alg, prot, mod, ealg, port_c, port_s, spi_c, spi_s));
  125. }
  126. tsip_header_Security_Client_t* tsip_header_Security_Client_create_null()
  127. {
  128. return tsip_header_Security_Client_create(tsk_null, tsk_null, tsk_null, tsk_null, tsk_null, 0, 0, 0, 0);
  129. }
  130. int tsip_header_Security_Client_serialize(const tsip_header_t* header, tsk_buffer_t* output)
  131. {
  132. if(header){
  133. const tsip_header_Security_Client_t *Security_Client = (const tsip_header_Security_Client_t *)header;
  134. int ret = 0;
  135. // ipsec-3gpp; alg=hmac-md5-96; ealg=des-ede3-cbc; spi-c=1111; spi-s=2222; port-c=5062; port-s=5064
  136. if(tsk_striequals(Security_Client->mech, "ipsec-3gpp")){
  137. ret = tsk_buffer_append_2(output, "%s%s%s%s%s%s%s;spi-c=%u;spi-s=%u;port-c=%u;port-s=%u",
  138. Security_Client->mech,
  139. Security_Client->alg ? ";alg=" : "",
  140. Security_Client->alg ? Security_Client->alg : "",
  141. Security_Client->ealg ? ";ealg=" : "",
  142. Security_Client->ealg ? Security_Client->ealg : "",
  143. Security_Client->prot ? ";prot=" : "",
  144. Security_Client->prot ? Security_Client->prot : "",
  145. Security_Client->spi_c,
  146. Security_Client->spi_s,
  147. Security_Client->port_c,
  148. Security_Client->port_s
  149. );
  150. }
  151. else if(Security_Client->mech){
  152. ret = tsk_buffer_append(output, Security_Client->mech, tsk_strlen(Security_Client->mech));
  153. }
  154. if(Security_Client->q >= 0){
  155. /* qvalue = ("0" [ "." 0*3DIGIT ] ) / ( "1" [ "." 0*3("0") ] ) */
  156. ret = tsk_buffer_append_2(output, ";q=%1.3f", Security_Client->q);
  157. }
  158. return ret;
  159. }
  160. return -1;
  161. }
  162. tsip_header_Security_Clients_L_t *tsip_header_Security_Client_parse(const char *data, tsk_size_t size)
  163. {
  164. int cs = 0;
  165. const char *p = data;
  166. const char *pe = p + size;
  167. const char *eof = pe;
  168. tsip_header_Security_Clients_L_t *hdr_securityclients = tsk_list_create();
  169. const char *tag_start = tsk_null;
  170. tsip_header_Security_Client_t *curr_securityclient = tsk_null;
  171. %%write data;
  172. (void)(eof);
  173. (void)(tsip_machine_parser_header_Security_Client_first_final);
  174. (void)(tsip_machine_parser_header_Security_Client_error);
  175. (void)(tsip_machine_parser_header_Security_Client_en_main);
  176. %%write init;
  177. %%write exec;
  178. if( cs < %%{ write first_final; }%% ){
  179. TSK_DEBUG_ERROR("Failed to parse 'Security-Client' header.");
  180. TSK_OBJECT_SAFE_FREE(curr_securityclient);
  181. TSK_OBJECT_SAFE_FREE(hdr_securityclients);
  182. }
  183. return hdr_securityclients;
  184. }
  185. //========================================================
  186. // Security_Client header object definition
  187. //
  188. static tsk_object_t* tsip_header_Security_Client_ctor(tsk_object_t *self, va_list * app)
  189. {
  190. tsip_header_Security_Client_t *Security_Client = self;
  191. if(Security_Client){
  192. const char* mech = va_arg(*app, const char*);
  193. TSIP_HEADER(Security_Client)->type = tsip_htype_Security_Client;
  194. TSIP_HEADER(Security_Client)->serialize = tsip_header_Security_Client_serialize;
  195. Security_Client->q = -1;
  196. if(mech){
  197. Security_Client->mech = tsk_strdup(mech);
  198. Security_Client->alg = tsk_strdup(va_arg(*app, const char*));
  199. Security_Client->prot = tsk_strdup(va_arg(*app, const char*));
  200. Security_Client->mod = tsk_strdup(va_arg(*app, const char*));
  201. Security_Client->ealg = tsk_strdup(va_arg(*app, const char*));
  202. #if defined(__GNUC__)
  203. Security_Client->port_c = (tnet_port_t)va_arg(*app, unsigned);
  204. Security_Client->port_s = (tnet_port_t)va_arg(*app, unsigned);
  205. #else
  206. Security_Client->port_c = va_arg(*app, tnet_port_t);
  207. Security_Client->port_s = va_arg(*app, tnet_port_t);
  208. #endif
  209. Security_Client->spi_c = va_arg(*app, uint32_t);
  210. Security_Client->spi_s = va_arg(*app, uint32_t);
  211. }
  212. }
  213. else{
  214. TSK_DEBUG_ERROR("Failed to create new Security_Client header.");
  215. }
  216. return self;
  217. }
  218. static tsk_object_t* tsip_header_Security_Client_dtor(tsk_object_t *self)
  219. {
  220. tsip_header_Security_Client_t *Security_Client = self;
  221. if(Security_Client){
  222. TSK_FREE(Security_Client->mech);
  223. TSK_FREE(Security_Client->alg);
  224. TSK_FREE(Security_Client->prot);
  225. TSK_FREE(Security_Client->mod);
  226. TSK_FREE(Security_Client->ealg);
  227. TSK_OBJECT_SAFE_FREE(TSIP_HEADER_PARAMS(Security_Client));
  228. }
  229. else{
  230. TSK_DEBUG_ERROR("Null Security_Client header.");
  231. }
  232. return self;
  233. }
  234. static const tsk_object_def_t tsip_header_Security_Client_def_s =
  235. {
  236. sizeof(tsip_header_Security_Client_t),
  237. tsip_header_Security_Client_ctor,
  238. tsip_header_Security_Client_dtor,
  239. tsk_null
  240. };
  241. const tsk_object_def_t *tsip_header_Security_Client_def_t = &tsip_header_Security_Client_def_s;