rtpproxy_funcs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 "rtpproxy_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 skip;
  159. int ret;
  160. str mpdel;
  161. char *rest, *p1, *p2;
  162. struct hdr_field hf;
  163. unsigned int mime;
  164. body->s = get_body(msg);
  165. if (body->s==0) {
  166. LM_ERR("failed to get the message body\n");
  167. goto error;
  168. }
  169. /*
  170. * Better use the content-len value - no need of any explicit
  171. * parcing as get_body() parsed all headers and Conten-Length
  172. * body header is automaticaly parsed when found.
  173. */
  174. if (msg->content_length==0) {
  175. LM_ERR("failed to get the content length in message\n");
  176. goto error;
  177. }
  178. body->len = get_content_length(msg);
  179. if (body->len==0) {
  180. LM_ERR("message body has length zero\n");
  181. goto error;
  182. }
  183. if (body->len + body->s > msg->buf + msg->len) {
  184. LM_ERR("content-length exceeds packet-length by %d\n",
  185. (int)((body->len + body->s) - (msg->buf + msg->len)));
  186. goto error;
  187. }
  188. /* no need for parse_headers(msg, EOH), get_body will
  189. * parse everything */
  190. /*is the content type correct?*/
  191. if((ret = check_content_type(msg))==-1)
  192. {
  193. LM_ERR("content type mismatching\n");
  194. goto error;
  195. }
  196. if(ret!=2)
  197. goto done;
  198. /* multipart body */
  199. if(get_mixed_part_delimiter(&msg->content_type->body,&mpdel) < 0) {
  200. goto error;
  201. }
  202. p1 = find_sdp_line_delimiter(body->s, body->s+body->len, mpdel);
  203. if (p1 == NULL) {
  204. LM_ERR("empty multipart content\n");
  205. return -1;
  206. }
  207. p2=p1;
  208. c = 0;
  209. for(;;)
  210. {
  211. p1 = p2;
  212. if (p1 == NULL || p1 >= body->s+body->len)
  213. break; /* No parts left */
  214. p2 = find_next_sdp_line_delimiter(p1, body->s+body->len,
  215. mpdel, body->s+body->len);
  216. /* p2 is text limit for application parsing */
  217. rest = eat_line(p1 + mpdel.len + 2, p2 - p1 - mpdel.len - 2);
  218. if ( rest > p2 ) {
  219. LM_ERR("Unparsable <%.*s>\n", (int)(p1-p1), p1);
  220. return -1;
  221. }
  222. while( rest<p2 ) {
  223. memset(&hf,0, sizeof(struct hdr_field));
  224. rest = get_sdp_hdr_field(rest, p2, &hf);
  225. if(hf.type==HDR_EOH_T)
  226. break;
  227. if(hf.type==HDR_ERROR_T)
  228. return -1;
  229. if(hf.type==HDR_CONTENTTYPE_T) {
  230. if(decode_mime_type(hf.body.s, hf.body.s + hf.body.len,
  231. &mime)==NULL)
  232. return -1;
  233. if (((((unsigned int)mime)>>16) == TYPE_APPLICATION)
  234. && ((mime&0x00ff) == SUBTYPE_SDP)) {
  235. c = 1;
  236. }
  237. }
  238. } /* end of while */
  239. if(c==1)
  240. {
  241. body->s = rest;
  242. body->len = p2-rest;
  243. goto done;
  244. }
  245. }
  246. error:
  247. return -1;
  248. done:
  249. for (skip = 0; skip < body->len; skip++) {
  250. c = body->s[body->len - skip - 1];
  251. if (c != '\r' && c != '\n')
  252. break;
  253. }
  254. if (skip == body->len) {
  255. LM_ERR("empty body");
  256. goto error;
  257. }
  258. body->len -= skip;
  259. /*LM_DBG("DEBUG:extract_body:=|%.*s|\n",body->len,body->s);*/
  260. return 1;
  261. }
  262. /*
  263. * ser_memmem() returns the location of the first occurrence of data
  264. * pattern b2 of size len2 in memory block b1 of size len1 or
  265. * NULL if none is found. Obtained from NetBSD.
  266. */
  267. void *
  268. ser_memmem(const void *b1, const void *b2, size_t len1, size_t len2)
  269. {
  270. /* Initialize search pointer */
  271. char *sp = (char *) b1;
  272. /* Initialize pattern pointer */
  273. char *pp = (char *) b2;
  274. /* Initialize end of search address space pointer */
  275. char *eos = sp + len1 - len2;
  276. /* Sanity check */
  277. if(!(b1 && b2 && len1 && len2))
  278. return NULL;
  279. while (sp <= eos) {
  280. if (*sp == *pp)
  281. if (memcmp(sp, pp, len2) == 0)
  282. return sp;
  283. sp++;
  284. }
  285. return NULL;
  286. }
  287. /*
  288. * Some helper functions taken verbatim from tm module.
  289. */
  290. /*
  291. * Extract Call-ID value
  292. * assumes the callid header is already parsed
  293. * (so make sure it is, before calling this function or
  294. * it might fail even if the message _has_ a callid)
  295. */
  296. int
  297. get_callid(struct sip_msg* _m, str* _cid)
  298. {
  299. if ((parse_headers(_m, HDR_CALLID_F, 0) == -1)) {
  300. LM_ERR("failed to parse call-id header\n");
  301. return -1;
  302. }
  303. if (_m->callid == NULL) {
  304. LM_ERR("call-id not found\n");
  305. return -1;
  306. }
  307. _cid->s = _m->callid->body.s;
  308. _cid->len = _m->callid->body.len;
  309. trim(_cid);
  310. return 0;
  311. }
  312. /*
  313. * Extract tag from To header field of a response
  314. * assumes the to header is already parsed, so
  315. * make sure it really is before calling this function
  316. */
  317. int
  318. get_to_tag(struct sip_msg* _m, str* _tag)
  319. {
  320. if (!_m->to) {
  321. LM_ERR("To header field missing\n");
  322. return -1;
  323. }
  324. if (get_to(_m)->tag_value.len) {
  325. _tag->s = get_to(_m)->tag_value.s;
  326. _tag->len = get_to(_m)->tag_value.len;
  327. } else {
  328. _tag->s = NULL; /* fixes gcc 4.0 warnings */
  329. _tag->len = 0;
  330. }
  331. return 0;
  332. }
  333. /*
  334. * Extract tag from From header field of a request
  335. */
  336. int
  337. get_from_tag(struct sip_msg* _m, str* _tag)
  338. {
  339. if (parse_from_header(_m)<0) {
  340. LM_ERR("failed to parse From header\n");
  341. return -1;
  342. }
  343. if (get_from(_m)->tag_value.len) {
  344. _tag->s = get_from(_m)->tag_value.s;
  345. _tag->len = get_from(_m)->tag_value.len;
  346. } else {
  347. _tag->s = NULL; /* fixes gcc 4.0 warnings */
  348. _tag->len = 0;
  349. }
  350. return 0;
  351. }
  352. /*
  353. * Extract URI from the Contact header field
  354. */
  355. int
  356. get_contact_uri(struct sip_msg* _m, struct sip_uri *uri, contact_t** _c)
  357. {
  358. if ((parse_headers(_m, HDR_CONTACT_F, 0) == -1) || !_m->contact)
  359. return -1;
  360. if (!_m->contact->parsed && parse_contact(_m->contact) < 0) {
  361. LM_ERR("failed to parse Contact body\n");
  362. return -1;
  363. }
  364. *_c = ((contact_body_t*)_m->contact->parsed)->contacts;
  365. if (*_c == NULL)
  366. /* no contacts found */
  367. return -1;
  368. if (parse_uri((*_c)->uri.s, (*_c)->uri.len, uri) < 0 || uri->host.len <= 0) {
  369. LM_ERR("failed to parse Contact URI [%.*s]\n",
  370. (*_c)->uri.len, ((*_c)->uri.s)?(*_c)->uri.s:"");
  371. return -1;
  372. }
  373. return 0;
  374. }
  375. /*
  376. * Extract branch from Via header
  377. */
  378. int
  379. get_via_branch(struct sip_msg* msg, int vianum, str* _branch)
  380. {
  381. struct via_body *via;
  382. struct via_param *p;
  383. if (parse_via_header(msg, vianum, &via) < 0)
  384. return -1;
  385. for (p = via->param_lst; p; p = p->next)
  386. {
  387. if (p->name.len == strlen("branch")
  388. && strncasecmp(p->name.s, "branch", strlen("branch")) == 0) {
  389. _branch->s = p->value.s;
  390. _branch->len = p->value.len;
  391. return 0;
  392. }
  393. }
  394. return -1;
  395. }