tsip_parser_header_Call_ID.rl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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_Call_ID.c
  23. * @brief SIP Call-ID/i header.
  24. *
  25. * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
  26. *
  27. */
  28. #include "tinysip/headers/tsip_header_Call_ID.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_Call_ID;
  39. # Includes
  40. include tsip_machine_utils "./ragel/tsip_machine_utils.rl";
  41. action tag{
  42. tag_start = p;
  43. }
  44. action parse_value{
  45. TSK_PARSER_SET_STRING(hdr_call_id->value);
  46. }
  47. action eob{
  48. }
  49. callid = word ( "@" word )?;
  50. Call_ID = ( "Call-ID"i | "i"i ) HCOLON callid>tag %parse_value;
  51. # Entry point
  52. main := Call_ID :>CRLF @eob;
  53. }%%
  54. tsip_header_Call_ID_t* tsip_header_Call_ID_create(const char* call_id)
  55. {
  56. return tsk_object_new(TSIP_HEADER_CALL_ID_VA_ARGS(call_id));
  57. }
  58. int tsip_header_Call_ID_serialize(const tsip_header_t* header, tsk_buffer_t* output)
  59. {
  60. if(header){
  61. const tsip_header_Call_ID_t *Call_ID = (const tsip_header_Call_ID_t *)header;
  62. if(Call_ID->value){
  63. return tsk_buffer_append(output, Call_ID->value, tsk_strlen(Call_ID->value));
  64. }
  65. }
  66. return -1;
  67. }
  68. int tsip_header_Call_ID_random(tsk_uuidstring_t *result)
  69. {
  70. return tsk_uuidgenerate(result);
  71. }
  72. tsip_header_Call_ID_t *tsip_header_Call_ID_parse(const char *data, tsk_size_t size)
  73. {
  74. int cs = 0;
  75. const char *p = data;
  76. const char *pe = p + size;
  77. const char *eof = pe;
  78. tsip_header_Call_ID_t *hdr_call_id = tsip_header_Call_ID_create(0);
  79. const char *tag_start = tsk_null;
  80. %%write data;
  81. (void)(eof);
  82. (void)(tsip_machine_parser_header_Call_ID_first_final);
  83. (void)(tsip_machine_parser_header_Call_ID_error);
  84. (void)(tsip_machine_parser_header_Call_ID_en_main);
  85. %%write init;
  86. %%write exec;
  87. if( cs < %%{ write first_final; }%% ){
  88. TSK_DEBUG_ERROR("Failed to parse SIP 'Call-ID' header.");
  89. TSK_OBJECT_SAFE_FREE(hdr_call_id);
  90. }
  91. return hdr_call_id;
  92. }
  93. //========================================================
  94. // Call_ID header object definition
  95. //
  96. static tsk_object_t* tsip_header_Call_ID_ctor(tsk_object_t *self, va_list * app)
  97. {
  98. tsip_header_Call_ID_t *Call_ID = self;
  99. if(Call_ID){
  100. Call_ID->value = tsk_strdup(va_arg(*app, const char *));
  101. TSIP_HEADER(Call_ID)->type = tsip_htype_Call_ID;
  102. TSIP_HEADER(Call_ID)->serialize = tsip_header_Call_ID_serialize;
  103. }
  104. else{
  105. TSK_DEBUG_ERROR("Failed to create new Call-ID header.");
  106. }
  107. return self;
  108. }
  109. static tsk_object_t* tsip_header_Call_ID_dtor(tsk_object_t *self)
  110. {
  111. tsip_header_Call_ID_t *Call_ID = self;
  112. if(Call_ID){
  113. TSK_FREE(Call_ID->value);
  114. TSK_OBJECT_SAFE_FREE(TSIP_HEADER_PARAMS(Call_ID));
  115. }
  116. else{
  117. TSK_DEBUG_ERROR("Null Call-ID header.");
  118. }
  119. return self;
  120. }
  121. static const tsk_object_def_t tsip_header_Call_ID_def_s =
  122. {
  123. sizeof(tsip_header_Call_ID_t),
  124. tsip_header_Call_ID_ctor,
  125. tsip_header_Call_ID_dtor,
  126. tsk_null
  127. };
  128. const tsk_object_def_t *tsip_header_Call_ID_def_t = &tsip_header_Call_ID_def_s;