rtpengine_funcs.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * Kamailio 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 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * Kamailio 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 this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. * History:
  23. * --------
  24. * 2003-11-06 body len is computed using the message len (it's
  25. * not taken any more from the msg. content-length) (andrei)
  26. * 2008-08-30 body len is taken from Conent-length header as it is more
  27. * reliable (UDP packages may contain garbage at the end)(bogdan)
  28. */
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <sys/types.h>
  32. #include <unistd.h>
  33. #include "rtpengine_funcs.h"
  34. #include "../../dprint.h"
  35. #include "../../config.h"
  36. #include "../../ut.h"
  37. #include "../../forward.h"
  38. #include "../../resolve.h"
  39. #include "../../globals.h"
  40. #include "../../udp_server.h"
  41. #include "../../pt.h"
  42. #include "../../parser/msg_parser.h"
  43. #include "../../trim.h"
  44. #include "../../parser/parse_from.h"
  45. #include "../../parser/contact/parse_contact.h"
  46. #include "../../parser/parse_uri.h"
  47. #include "../../parser/parse_content.h"
  48. #include "../../parser/parser_f.h"
  49. #include "../../parser/sdp/sdp_helpr_funcs.h"
  50. #define READ(val) \
  51. (*(val + 0) + (*(val + 1) << 8) + (*(val + 2) << 16) + (*(val + 3) << 24))
  52. #define advance(_ptr,_n,_str,_error) \
  53. do{\
  54. if ((_ptr)+(_n)>(_str).s+(_str).len)\
  55. goto _error;\
  56. (_ptr) = (_ptr) + (_n);\
  57. }while(0);
  58. #define one_of_16( _x , _t ) \
  59. (_x==_t[0]||_x==_t[15]||_x==_t[8]||_x==_t[2]||_x==_t[3]||_x==_t[4]\
  60. ||_x==_t[5]||_x==_t[6]||_x==_t[7]||_x==_t[1]||_x==_t[9]||_x==_t[10]\
  61. ||_x==_t[11]||_x==_t[12]||_x==_t[13]||_x==_t[14])
  62. #define one_of_8( _x , _t ) \
  63. (_x==_t[0]||_x==_t[7]||_x==_t[1]||_x==_t[2]||_x==_t[3]||_x==_t[4]\
  64. ||_x==_t[5]||_x==_t[6])
  65. /**
  66. * return:
  67. * -1: error
  68. * 1: text or sdp
  69. * 2: multipart
  70. */
  71. int check_content_type(struct sip_msg *msg)
  72. {
  73. static unsigned int appl[16] = {
  74. 0x6c707061/*appl*/,0x6c707041/*Appl*/,0x6c705061/*aPpl*/,
  75. 0x6c705041/*APpl*/,0x6c507061/*apPl*/,0x6c507041/*ApPl*/,
  76. 0x6c505061/*aPPl*/,0x6c505041/*APPl*/,0x4c707061/*appL*/,
  77. 0x4c707041/*AppL*/,0x4c705061/*aPpL*/,0x4c705041/*APpL*/,
  78. 0x4c507061/*apPL*/,0x4c507041/*ApPL*/,0x4c505061/*aPPL*/,
  79. 0x4c505041/*APPL*/};
  80. static unsigned int icat[16] = {
  81. 0x74616369/*icat*/,0x74616349/*Icat*/,0x74614369/*iCat*/,
  82. 0x74614349/*ICat*/,0x74416369/*icAt*/,0x74416349/*IcAt*/,
  83. 0x74414369/*iCAt*/,0x74414349/*ICAt*/,0x54616369/*icaT*/,
  84. 0x54616349/*IcaT*/,0x54614369/*iCaT*/,0x54614349/*ICaT*/,
  85. 0x54416369/*icAT*/,0x54416349/*IcAT*/,0x54414369/*iCAT*/,
  86. 0x54414349/*ICAT*/};
  87. static unsigned int ion_[8] = {
  88. 0x006e6f69/*ion_*/,0x006e6f49/*Ion_*/,0x006e4f69/*iOn_*/,
  89. 0x006e4f49/*IOn_*/,0x004e6f69/*ioN_*/,0x004e6f49/*IoN_*/,
  90. 0x004e4f69/*iON_*/,0x004e4f49/*ION_*/};
  91. static unsigned int sdp_[8] = {
  92. 0x00706473/*sdp_*/,0x00706453/*Sdp_*/,0x00704473/*sDp_*/,
  93. 0x00704453/*SDp_*/,0x00506473/*sdP_*/,0x00506453/*SdP_*/,
  94. 0x00504473/*sDP_*/,0x00504453/*SDP_*/};
  95. str str_type;
  96. unsigned int x;
  97. char *p;
  98. if (!msg->content_type)
  99. {
  100. LM_WARN("the header Content-TYPE is absent!"
  101. "let's assume the content is text/plain ;-)\n");
  102. return 1;
  103. }
  104. trim_len(str_type.len,str_type.s,msg->content_type->body);
  105. if (str_type.len>=15 && (*str_type.s=='m' || *str_type.s=='M')
  106. && strncasecmp(str_type.s, "multipart/mixed", 15) == 0) {
  107. return 2;
  108. }
  109. p = str_type.s;
  110. advance(p,4,str_type,error_1);
  111. x = READ(p-4);
  112. if (!one_of_16(x,appl))
  113. goto other;
  114. advance(p,4,str_type,error_1);
  115. x = READ(p-4);
  116. if (!one_of_16(x,icat))
  117. goto other;
  118. advance(p,3,str_type,error_1);
  119. x = READ(p-3) & 0x00ffffff;
  120. if (!one_of_8(x,ion_))
  121. goto other;
  122. /* skip spaces and tabs if any */
  123. while (*p==' ' || *p=='\t')
  124. advance(p,1,str_type,error_1);
  125. if (*p!='/')
  126. {
  127. LM_ERR("no / found after primary type\n");
  128. goto error;
  129. }
  130. advance(p,1,str_type,error_1);
  131. while ((*p==' ' || *p=='\t') && p+1<str_type.s+str_type.len)
  132. advance(p,1,str_type,error_1);
  133. advance(p,3,str_type,error_1);
  134. x = READ(p-3) & 0x00ffffff;
  135. if (!one_of_8(x,sdp_))
  136. goto other;
  137. if (*p==';'||*p==' '||*p=='\t'||*p=='\n'||*p=='\r'||*p==0) {
  138. LM_DBG("type <%.*s> found valid\n", (int)(p-str_type.s), str_type.s);
  139. return 1;
  140. } else {
  141. LM_ERR("bad end for type!\n");
  142. return -1;
  143. }
  144. error_1:
  145. LM_ERR("body ended :-(!\n");
  146. error:
  147. return -1;
  148. other:
  149. LM_ERR("invalid type for a message\n");
  150. return -1;
  151. }
  152. /*
  153. * Get message body and check Content-Type header field
  154. */
  155. int extract_body(struct sip_msg *msg, str *body )
  156. {
  157. char c;
  158. int ret;
  159. str mpdel;
  160. char *rest, *p1, *p2;
  161. struct hdr_field hf;
  162. unsigned int mime;
  163. body->s = get_body(msg);
  164. if (body->s==0) {
  165. LM_ERR("failed to get the message body\n");
  166. goto error;
  167. }
  168. /*
  169. * Better use the content-len value - no need of any explicit
  170. * parcing as get_body() parsed all headers and Conten-Length
  171. * body header is automaticaly parsed when found.
  172. */
  173. if (msg->content_length==0) {
  174. LM_ERR("failed to get the content length in message\n");
  175. goto error;
  176. }
  177. body->len = get_content_length(msg);
  178. if (body->len==0) {
  179. LM_ERR("message body has length zero\n");
  180. goto error;
  181. }
  182. if (body->len + body->s > msg->buf + msg->len) {
  183. LM_ERR("content-length exceeds packet-length by %d\n",
  184. (int)((body->len + body->s) - (msg->buf + msg->len)));
  185. goto error;
  186. }
  187. /* no need for parse_headers(msg, EOH), get_body will
  188. * parse everything */
  189. /*is the content type correct?*/
  190. if((ret = check_content_type(msg))==-1)
  191. {
  192. LM_ERR("content type mismatching\n");
  193. goto error;
  194. }
  195. if(ret!=2)
  196. goto done;
  197. /* multipart body */
  198. if(get_mixed_part_delimiter(&msg->content_type->body,&mpdel) < 0) {
  199. goto error;
  200. }
  201. p1 = find_sdp_line_delimiter(body->s, body->s+body->len, mpdel);
  202. if (p1 == NULL) {
  203. LM_ERR("empty multipart content\n");
  204. return -1;
  205. }
  206. p2=p1;
  207. c = 0;
  208. for(;;)
  209. {
  210. p1 = p2;
  211. if (p1 == NULL || p1 >= body->s+body->len)
  212. break; /* No parts left */
  213. p2 = find_next_sdp_line_delimiter(p1, body->s+body->len,
  214. mpdel, body->s+body->len);
  215. /* p2 is text limit for application parsing */
  216. rest = eat_line(p1 + mpdel.len + 2, p2 - p1 - mpdel.len - 2);
  217. if ( rest > p2 ) {
  218. LM_ERR("Unparsable <%.*s>\n", (int)(p1-p1), p1);
  219. return -1;
  220. }
  221. while( rest<p2 ) {
  222. memset(&hf,0, sizeof(struct hdr_field));
  223. rest = get_sdp_hdr_field(rest, p2, &hf);
  224. if(hf.type==HDR_EOH_T)
  225. break;
  226. if(hf.type==HDR_ERROR_T)
  227. return -1;
  228. if(hf.type==HDR_CONTENTTYPE_T) {
  229. if(decode_mime_type(hf.body.s, hf.body.s + hf.body.len,
  230. &mime)==NULL)
  231. return -1;
  232. if (((((unsigned int)mime)>>16) == TYPE_APPLICATION)
  233. && ((mime&0x00ff) == SUBTYPE_SDP)) {
  234. c = 1;
  235. }
  236. }
  237. } /* end of while */
  238. if(c==1)
  239. {
  240. if (rest < p2 && *rest == '\r') rest++;
  241. if (rest < p2 && *rest == '\n') rest++;
  242. if (rest < p2 && p2[-1] == '\n') p2--;
  243. if (rest < p2 && p2[-1] == '\r') p2--;
  244. body->s = rest;
  245. body->len = p2-rest;
  246. goto done;
  247. }
  248. }
  249. error:
  250. return -1;
  251. done:
  252. /*LM_DBG("DEBUG:extract_body:=|%.*s|\n",body->len,body->s);*/
  253. return 1;
  254. }
  255. /*
  256. * Some helper functions taken verbatim from tm module.
  257. */
  258. /*
  259. * Extract Call-ID value
  260. * assumes the callid header is already parsed
  261. * (so make sure it is, before calling this function or
  262. * it might fail even if the message _has_ a callid)
  263. */
  264. int
  265. get_callid(struct sip_msg* _m, str* _cid)
  266. {
  267. if ((parse_headers(_m, HDR_CALLID_F, 0) == -1)) {
  268. LM_ERR("failed to parse call-id header\n");
  269. return -1;
  270. }
  271. if (_m->callid == NULL) {
  272. LM_ERR("call-id not found\n");
  273. return -1;
  274. }
  275. _cid->s = _m->callid->body.s;
  276. _cid->len = _m->callid->body.len;
  277. trim(_cid);
  278. return 0;
  279. }
  280. /*
  281. * Extract tag from To header field of a response
  282. */
  283. int
  284. get_to_tag(struct sip_msg* _m, str* _tag)
  285. {
  286. if (parse_to_header(_m) < 0) {
  287. LM_ERR("To header field missing\n");
  288. return -1;
  289. }
  290. if (get_to(_m)->tag_value.len) {
  291. _tag->s = get_to(_m)->tag_value.s;
  292. _tag->len = get_to(_m)->tag_value.len;
  293. } else {
  294. _tag->s = NULL; /* fixes gcc 4.0 warnings */
  295. _tag->len = 0;
  296. }
  297. return 0;
  298. }
  299. /*
  300. * Extract tag from From header field of a request
  301. */
  302. int
  303. get_from_tag(struct sip_msg* _m, str* _tag)
  304. {
  305. if (parse_from_header(_m)<0) {
  306. LM_ERR("failed to parse From header\n");
  307. return -1;
  308. }
  309. if (get_from(_m)->tag_value.len) {
  310. _tag->s = get_from(_m)->tag_value.s;
  311. _tag->len = get_from(_m)->tag_value.len;
  312. } else {
  313. _tag->s = NULL; /* fixes gcc 4.0 warnings */
  314. _tag->len = 0;
  315. }
  316. return 0;
  317. }
  318. /*
  319. * Extract URI from the Contact header field
  320. */
  321. int
  322. get_contact_uri(struct sip_msg* _m, struct sip_uri *uri, contact_t** _c)
  323. {
  324. if ((parse_headers(_m, HDR_CONTACT_F, 0) == -1) || !_m->contact)
  325. return -1;
  326. if (!_m->contact->parsed && parse_contact(_m->contact) < 0) {
  327. LM_ERR("failed to parse Contact body\n");
  328. return -1;
  329. }
  330. *_c = ((contact_body_t*)_m->contact->parsed)->contacts;
  331. if (*_c == NULL)
  332. /* no contacts found */
  333. return -1;
  334. if (parse_uri((*_c)->uri.s, (*_c)->uri.len, uri) < 0 || uri->host.len <= 0) {
  335. LM_ERR("failed to parse Contact URI [%.*s]\n",
  336. (*_c)->uri.len, ((*_c)->uri.s)?(*_c)->uri.s:"");
  337. return -1;
  338. }
  339. return 0;
  340. }
  341. /*
  342. * Extract branch from Via header
  343. */
  344. int
  345. get_via_branch(struct sip_msg* msg, int vianum, str* _branch)
  346. {
  347. struct via_body *via;
  348. struct via_param *p;
  349. if (parse_via_header(msg, vianum, &via) < 0)
  350. return -1;
  351. for (p = via->param_lst; p; p = p->next)
  352. {
  353. if (p->name.len == strlen("branch")
  354. && strncasecmp(p->name.s, "branch", strlen("branch")) == 0) {
  355. _branch->s = p->value.s;
  356. _branch->len = p->value.len;
  357. return 0;
  358. }
  359. }
  360. return -1;
  361. }