tsip_parser_header_Via.rl 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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_Via.c
  23. * @brief SIP Via/v header as per RFC 3261 subclause 20.42.
  24. *
  25. * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
  26. *
  27. */
  28. #include "tinysip/headers/tsip_header_Via.h"
  29. #include "tsk_debug.h"
  30. #include "tsk_memory.h"
  31. /***********************************
  32. * Ragel state machine.
  33. */
  34. %%{
  35. machine tsip_machine_parser_header_Via;
  36. # Includes
  37. include tsip_machine_utils "./ragel/tsip_machine_utils.rl";
  38. action tag{
  39. tag_start = p;
  40. }
  41. action create_via{
  42. if(!curr_via){
  43. curr_via = tsip_header_Via_create_null();
  44. }
  45. }
  46. action parse_protocol_name{
  47. TSK_PARSER_SET_STRING(curr_via->proto_name);
  48. }
  49. action parse_protocol_version{
  50. TSK_PARSER_SET_STRING(curr_via->proto_version);
  51. }
  52. action parse_host{
  53. TSK_PARSER_SET_STRING(curr_via->host);
  54. if(curr_via->host && *curr_via->host == '['){
  55. tsk_strunquote_2(&curr_via->host, '[', ']');
  56. }
  57. }
  58. action parse_port{
  59. TSK_PARSER_SET_INTEGER(curr_via->port);
  60. }
  61. action parse_transport{
  62. TSK_PARSER_SET_STRING(curr_via->transport);
  63. }
  64. action parse_ttl{
  65. TSK_PARSER_SET_INTEGER(curr_via->ttl);
  66. }
  67. action parse_maddr{
  68. TSK_PARSER_SET_STRING(curr_via->maddr);
  69. }
  70. action parse_received{
  71. TSK_PARSER_SET_STRING(curr_via->received);
  72. }
  73. action parse_branch{
  74. TSK_PARSER_SET_STRING(curr_via->branch);
  75. }
  76. action parse_comp{
  77. TSK_PARSER_SET_STRING(curr_via->comp);
  78. }
  79. action parse_rport{
  80. TSK_PARSER_SET_INTEGER(curr_via->rport);
  81. }
  82. action has_rport{
  83. if(curr_via->rport <0){
  84. curr_via->rport = 0;
  85. }
  86. }
  87. action parse_param{
  88. TSK_PARSER_ADD_PARAM(TSIP_HEADER_PARAMS(curr_via));
  89. }
  90. action add_via{
  91. if(curr_via){
  92. tsk_list_push_back_data(hdr_vias, ((void**) &curr_via));
  93. }
  94. }
  95. action eob{
  96. }
  97. protocol_name = "SIP"i | token >tag %parse_protocol_name;
  98. protocol_version = token >tag %parse_protocol_version;
  99. transport = ("UDP"i | "TCP"i | "TLS"i | "SCTP"i | "TLS-SCTP"i | other_transport) >tag %parse_transport;
  100. sent_protocol = protocol_name SLASH protocol_version SLASH transport;
  101. sent_by = host>tag %parse_host ( COLON port >tag %parse_port )?;
  102. via_ttl = "ttl"i EQUAL ttl >tag %parse_ttl;
  103. via_maddr = "maddr"i EQUAL host >tag %parse_maddr;
  104. via_received = "received"i EQUAL ( IPv4address | IPv6address )>tag %parse_received;
  105. via_branch = "branch"i EQUAL token >tag %parse_branch;
  106. via_compression = "comp"i EQUAL ( "sigcomp"i | other_compression )>tag %parse_comp;
  107. response_port = "rport"i ( EQUAL DIGIT+ >tag %parse_rport )? %has_rport;
  108. via_extension = (generic_param) >tag %parse_param;
  109. via_params = (via_ttl | via_maddr | via_received | via_branch | via_compression | response_port)@1 | (via_extension)@0;
  110. via_parm = (sent_protocol LWS sent_by ( SEMI via_params )*) >create_via %add_via;
  111. Via = ( "Via"i | "v"i ) HCOLON via_parm ( COMMA via_parm )*;
  112. # Entry point
  113. main := Via :>CRLF @eob;
  114. }%%
  115. tsip_header_Via_t* tsip_header_Via_create(const char* proto_name, const char* proto_version, const char* transport, const char* host, uint16_t port)
  116. {
  117. return tsk_object_new(TSIP_HEADER_VIA_VA_ARGS(proto_name, proto_version, transport, host, port));
  118. }
  119. tsip_header_Via_t* tsip_header_Via_create_null()
  120. {
  121. return tsip_header_Via_create(tsk_null, tsk_null, tsk_null, tsk_null, 0);
  122. }
  123. int tsip_header_Via_serialize(const tsip_header_t* header, tsk_buffer_t* output)
  124. {
  125. if(header){
  126. const tsip_header_Via_t *Via = (const tsip_header_Via_t *)header;
  127. tsk_istr_t port, rport, ttl;
  128. int ipv6 = (Via->host && tsk_strcontains(Via->host, tsk_strlen(Via->host), ":"));
  129. if(Via->port){
  130. tsk_itoa(Via->port, &port);
  131. }
  132. if(Via->rport){
  133. tsk_itoa(Via->rport, &rport);
  134. }
  135. if(Via->ttl){
  136. tsk_itoa(Via->ttl, &ttl);
  137. }
  138. /* SIP/2.0/UDP [::]:1988;test=1234;comp=sigcomp;rport=254;ttl=457;received=192.0.2.101;branch=z9hG4bK1245420841406\r\n" */
  139. return tsk_buffer_append_2(output, "%s/%s/%s %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
  140. Via->proto_name ? Via->proto_name : "SIP",
  141. Via->proto_version ? Via->proto_version : "2.0",
  142. Via->transport ? Via->transport : "UDP",
  143. ipv6 ? "[" : "",
  144. Via->host ? Via->host : "127.0.0.1",
  145. ipv6 ? "]" : "",
  146. Via->port ? ":" : "",
  147. Via->port ? port : "",
  148. Via->maddr ? ";maddr=" : "",
  149. Via->maddr ? Via->maddr : "",
  150. Via->sigcomp_id ? ";sigcomp-id=" : "",
  151. Via->sigcomp_id ? Via->sigcomp_id : "",
  152. Via->comp ? ";comp=" : "",
  153. Via->comp ? Via->comp : "",
  154. Via->rport>=0 ? (Via->rport>0?";rport=":";rport") : "",
  155. Via->rport>0 ? rport : "",
  156. Via->ttl>=0 ? (Via->ttl>0?";ttl=":";ttl") : "",
  157. Via->ttl>0 ? ttl : "",
  158. Via->received ? ";received=" : "",
  159. Via->received ? Via->received : "",
  160. Via->branch ? ";branch=" : "",
  161. Via->branch ? Via->branch : ""
  162. );
  163. }
  164. return -1;
  165. }
  166. char* tsip_header_Via_get_special_param_value(const tsip_header_t* header, const char* pname)
  167. {
  168. if(header){
  169. const tsip_header_Via_t *Via = (const tsip_header_Via_t *)header;
  170. if(tsk_striequals(pname, "maddr")){
  171. return tsk_strdup(Via->maddr);
  172. }
  173. else if(tsk_striequals(pname, "sigcomp-id")){
  174. return tsk_strdup(Via->sigcomp_id);
  175. }
  176. else if(tsk_striequals(pname, "comp")){
  177. return tsk_strdup(Via->comp);
  178. }
  179. else if(tsk_striequals(pname, "rport")){
  180. tsk_istr_t rport;
  181. tsk_itoa(Via->rport, &rport);
  182. return tsk_strdup(rport);
  183. }
  184. else if(tsk_striequals(pname, "received")){
  185. return tsk_strdup(Via->received);
  186. }
  187. else if(tsk_striequals(pname, "branch")){
  188. return tsk_strdup(Via->branch);
  189. }
  190. }
  191. return tsk_null;
  192. }
  193. tsip_header_Vias_L_t *tsip_header_Via_parse(const char *data, tsk_size_t size)
  194. {
  195. int cs = 0;
  196. const char *p = data;
  197. const char *pe = p + size;
  198. const char *eof = pe;
  199. tsip_header_Vias_L_t *hdr_vias = tsk_list_create();
  200. tsip_header_Via_t *curr_via = tsk_null;
  201. const char *tag_start = tsk_null;
  202. %%write data;
  203. (void)(eof);
  204. (void)(tsip_machine_parser_header_Via_first_final);
  205. (void)(tsip_machine_parser_header_Via_error);
  206. (void)(tsip_machine_parser_header_Via_en_main);
  207. %%write init;
  208. %%write exec;
  209. if( cs < %%{ write first_final; }%% ){
  210. TSK_DEBUG_ERROR("Failed to parse 'Via' header.");
  211. TSK_OBJECT_SAFE_FREE(curr_via);
  212. TSK_OBJECT_SAFE_FREE(hdr_vias);
  213. }
  214. return hdr_vias;
  215. }
  216. //========================================================
  217. // Via header object definition
  218. //
  219. static tsk_object_t* tsip_header_Via_ctor(tsk_object_t *self, va_list * app)
  220. {
  221. tsip_header_Via_t *via = self;
  222. if(via){
  223. const char* proto_name = va_arg(*app, const char *);
  224. const char* proto_version = va_arg(*app, const char *);
  225. const char* transport = va_arg(*app, const char *);
  226. const char* host = va_arg(*app, const char *);
  227. #if defined(__GNUC__)
  228. uint16_t port = (uint16_t)va_arg(*app, unsigned);
  229. #else
  230. uint16_t port = va_arg(*app, uint16_t);
  231. #endif
  232. if(proto_name) via->proto_name = tsk_strdup(proto_name);
  233. if(proto_version) via->proto_version = tsk_strdup(proto_version);
  234. if(transport) via->transport = tsk_strdup(transport);
  235. if(host) via->host = tsk_strdup(host);
  236. via->port = port;
  237. via->rport = -1;
  238. via->ttl = -1;
  239. TSIP_HEADER(via)->type = tsip_htype_Via;
  240. TSIP_HEADER(via)->serialize = tsip_header_Via_serialize;
  241. TSIP_HEADER(via)->get_special_param_value = tsip_header_Via_get_special_param_value;
  242. }
  243. else{
  244. TSK_DEBUG_ERROR("Failed to create new Via header.");
  245. }
  246. return self;
  247. }
  248. static tsk_object_t* tsip_header_Via_dtor(tsk_object_t *self)
  249. {
  250. tsip_header_Via_t *via = self;
  251. if(via){
  252. TSK_FREE(via->branch);
  253. TSK_FREE(via->comp);
  254. TSK_FREE(via->host);
  255. TSK_FREE(via->maddr);
  256. TSK_FREE(via->proto_name);
  257. TSK_FREE(via->proto_version);
  258. TSK_FREE(via->received);
  259. TSK_FREE(via->sigcomp_id);
  260. TSK_FREE(via->transport);
  261. TSK_OBJECT_SAFE_FREE(TSIP_HEADER_PARAMS(via));
  262. }
  263. else{
  264. TSK_DEBUG_ERROR("Null Via header.");
  265. }
  266. return self;
  267. }
  268. static const tsk_object_def_t tsip_header_Via_def_s =
  269. {
  270. sizeof(tsip_header_Via_t),
  271. tsip_header_Via_ctor,
  272. tsip_header_Via_dtor,
  273. tsk_null
  274. };
  275. const tsk_object_def_t *tsip_header_Via_def_t = &tsip_header_Via_def_s;