tsip_parser_header_Privacy.rl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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_Privacy.c
  23. * @brief SIP Privacy header.
  24. *
  25. * Header field where proxy ACK BYE CAN INV OPT REG
  26. * ___________________________________________________________
  27. * Privacy amrd o o o o o o
  28. *
  29. * Header field SUB NOT PRK IFO UPD MSG
  30. * ___________________________________________________________
  31. * Privacy o o o o o o
  32. *
  33. * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
  34. *
  35. */
  36. #include "tinysip/headers/tsip_header_Privacy.h"
  37. #include "tinysip/parsers/tsip_parser_uri.h"
  38. #include "tsk_debug.h"
  39. #include "tsk_memory.h"
  40. #include <string.h>
  41. /***********************************
  42. * Ragel state machine.
  43. */
  44. %%{
  45. machine tsip_machine_parser_header_Privacy;
  46. # Includes
  47. include tsip_machine_utils "./ragel/tsip_machine_utils.rl";
  48. action tag{
  49. tag_start = p;
  50. }
  51. action parse_priv_value{
  52. TSK_PARSER_ADD_STRING(hdr_privacy->values);
  53. }
  54. action eob{
  55. }
  56. priv_value = ("header"i | "session"i | "user"i | "none"i | "critical"i | "id"i | "history"i)@1 | token@0;
  57. Privacy_hdr = "Privacy"i HCOLON priv_value>tag %parse_priv_value ( ";" priv_value>tag %parse_priv_value )*;
  58. # Entry point
  59. main := Privacy_hdr :>CRLF @eob;
  60. }%%
  61. tsip_header_Privacy_t* tsip_header_Privacy_create()
  62. {
  63. return tsk_object_new(tsip_header_Privacy_def_t);
  64. }
  65. int tsip_header_Privacy_serialize(const tsip_header_t* header, tsk_buffer_t* output)
  66. {
  67. if(header){
  68. const tsip_header_Privacy_t *Privacy = (const tsip_header_Privacy_t *)header;
  69. tsk_list_item_t *item;
  70. tsk_string_t *str;
  71. int ret = 0;
  72. tsk_list_foreach(item, Privacy->values){
  73. str = item->data;
  74. if(item == Privacy->values->head){
  75. ret = tsk_buffer_append(output, str->value, tsk_strlen(str->value));
  76. }
  77. else{
  78. ret = tsk_buffer_append_2(output, ";%s", str->value);
  79. }
  80. }
  81. return ret;
  82. }
  83. return -1;
  84. }
  85. tsip_header_Privacy_t *tsip_header_Privacy_parse(const char *data, tsk_size_t size)
  86. {
  87. int cs = 0;
  88. const char *p = data;
  89. const char *pe = p + size;
  90. const char *eof = pe;
  91. tsip_header_Privacy_t *hdr_privacy = tsip_header_Privacy_create();
  92. const char *tag_start = tsk_null;
  93. %%write data;
  94. (void)(eof);
  95. (void)(tsip_machine_parser_header_Privacy_first_final);
  96. (void)(tsip_machine_parser_header_Privacy_error);
  97. (void)(tsip_machine_parser_header_Privacy_en_main);
  98. %%write init;
  99. %%write exec;
  100. if( cs < %%{ write first_final; }%% ){
  101. TSK_DEBUG_ERROR("Failed to parse 'Privacy' header.");
  102. TSK_OBJECT_SAFE_FREE(hdr_privacy);
  103. }
  104. return hdr_privacy;
  105. }
  106. //========================================================
  107. // Privacy header object definition
  108. //
  109. static tsk_object_t* tsip_header_Privacy_ctor(tsk_object_t *self, va_list * app)
  110. {
  111. tsip_header_Privacy_t *Privacy = self;
  112. if(Privacy){
  113. TSIP_HEADER(Privacy)->type = tsip_htype_Privacy;
  114. TSIP_HEADER(Privacy)->serialize = tsip_header_Privacy_serialize;
  115. }
  116. else{
  117. TSK_DEBUG_ERROR("Failed to create new Privacy header.");
  118. }
  119. return self;
  120. }
  121. static tsk_object_t* tsip_header_Privacy_dtor(tsk_object_t *self)
  122. {
  123. tsip_header_Privacy_t *Privacy = self;
  124. if(Privacy){
  125. TSK_OBJECT_SAFE_FREE(Privacy->values);
  126. }
  127. else{
  128. TSK_DEBUG_ERROR("Null Privacy header.");
  129. }
  130. return self;
  131. }
  132. static const tsk_object_def_t tsip_header_Privacy_def_s =
  133. {
  134. sizeof(tsip_header_Privacy_t),
  135. tsip_header_Privacy_ctor,
  136. tsip_header_Privacy_dtor,
  137. tsk_null
  138. };
  139. const tsk_object_def_t *tsip_header_Privacy_def_t = &tsip_header_Privacy_def_s;