2
0

msg_parser.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /*
  2. * $Id$
  3. *
  4. * sip msg. header proxy parser
  5. *
  6. *
  7. * Copyright (C) 2001-2003 Fhg Fokus
  8. *
  9. * This file is part of ser, a free SIP server.
  10. *
  11. * ser is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version
  15. *
  16. * For a license to use the ser software under conditions
  17. * other than those described here, or to purchase support for this
  18. * software, please contact iptel.org by e-mail at the following addresses:
  19. * [email protected]
  20. *
  21. * ser is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  29. */
  30. #include <string.h>
  31. #include <stdlib.h>
  32. #include "msg_parser.h"
  33. #include "parser_f.h"
  34. #include "../ut.h"
  35. #include "../error.h"
  36. #include "../dprint.h"
  37. #include "../data_lump_rpl.h"
  38. #include "../mem/mem.h"
  39. #include "../error.h"
  40. #include "../globals.h"
  41. #include "parse_hname2.h"
  42. #include "parse_uri.h"
  43. #include "parse_content.h"
  44. #ifdef DEBUG_DMALLOC
  45. #include <mem/dmalloc.h>
  46. #endif
  47. #define parse_hname(_b,_e,_h) parse_hname2((_b),(_e),(_h))
  48. /* number of via's encounteded */
  49. int via_cnt;
  50. /* returns pointer to next header line, and fill hdr_f ;
  51. * if at end of header returns pointer to the last crlf (always buf)*/
  52. char* get_hdr_field(char* buf, char* end, struct hdr_field* hdr)
  53. {
  54. char* tmp;
  55. char *match;
  56. struct via_body *vb;
  57. struct cseq_body* cseq_b;
  58. struct to_body* to_b;
  59. int integer;
  60. if ((*buf)=='\n' || (*buf)=='\r'){
  61. /* double crlf or lflf or crcr */
  62. DBG("found end of header\n");
  63. hdr->type=HDR_EOH;
  64. return buf;
  65. }
  66. tmp=parse_hname(buf, end, hdr);
  67. if (hdr->type==HDR_ERROR){
  68. LOG(L_ERR, "ERROR: get_hdr_field: bad header\n");
  69. goto error;
  70. }
  71. switch(hdr->type){
  72. case HDR_VIA:
  73. /* keep number of vias parsed -- we want to report it in
  74. replies for diagnostic purposes */
  75. via_cnt++;
  76. vb=pkg_malloc(sizeof(struct via_body));
  77. if (vb==0){
  78. LOG(L_ERR, "get_hdr_field: out of memory\n");
  79. goto error;
  80. }
  81. memset(vb,0,sizeof(struct via_body));
  82. hdr->body.s=tmp;
  83. tmp=parse_via(tmp, end, vb);
  84. if (vb->error==PARSE_ERROR){
  85. LOG(L_ERR, "ERROR: get_hdr_field: bad via\n");
  86. pkg_free(vb);
  87. goto error;
  88. }
  89. hdr->parsed=vb;
  90. vb->hdr.s=hdr->name.s;
  91. vb->hdr.len=hdr->name.len;
  92. hdr->body.len=tmp-hdr->body.s;
  93. break;
  94. case HDR_CSEQ:
  95. cseq_b=pkg_malloc(sizeof(struct cseq_body));
  96. if (cseq_b==0){
  97. LOG(L_ERR, "get_hdr_field: out of memory\n");
  98. goto error;
  99. }
  100. memset(cseq_b, 0, sizeof(struct cseq_body));
  101. hdr->body.s=tmp;
  102. tmp=parse_cseq(tmp, end, cseq_b);
  103. if (cseq_b->error==PARSE_ERROR){
  104. LOG(L_ERR, "ERROR: get_hdr_field: bad cseq\n");
  105. pkg_free(cseq_b);
  106. goto error;
  107. }
  108. hdr->parsed=cseq_b;
  109. hdr->body.len=tmp-hdr->body.s;
  110. DBG("get_hdr_field: cseq <%s>: <%s> <%s>\n",
  111. hdr->name.s, cseq_b->number.s, cseq_b->method.s);
  112. break;
  113. case HDR_TO:
  114. to_b=pkg_malloc(sizeof(struct to_body));
  115. if (to_b==0){
  116. LOG(L_ERR, "get_hdr_field: out of memory\n");
  117. goto error;
  118. }
  119. memset(to_b, 0, sizeof(struct to_body));
  120. hdr->body.s=tmp;
  121. tmp=parse_to(tmp, end,to_b);
  122. if (to_b->error==PARSE_ERROR){
  123. LOG(L_ERR, "ERROR: get_hdr_field: bad to header\n");
  124. pkg_free(to_b);
  125. goto error;
  126. }
  127. hdr->parsed=to_b;
  128. hdr->body.len=tmp-hdr->body.s;
  129. DBG("DEBUG: get_hdr_field: <%s> [%d]; uri=[%.*s] \n",
  130. hdr->name.s, hdr->body.len, to_b->uri.len,to_b->uri.s);
  131. DBG("DEBUG: to body [%.*s]\n",to_b->body.len,to_b->body.s);
  132. break;
  133. case HDR_CONTENTLENGTH:
  134. hdr->body.s=tmp;
  135. tmp=parse_content_length(tmp,end, &integer);
  136. if (tmp==0){
  137. LOG(L_ERR, "ERROR:get_hdr_field: bad content_length header\n");
  138. goto error;
  139. }
  140. hdr->parsed=(void*)integer;
  141. hdr->body.len=tmp-hdr->body.s;
  142. DBG("DEBUG: get_hdr_body : content_length=%d\n",(int)hdr->parsed);
  143. break;
  144. case HDR_CONTENTTYPE:
  145. hdr->body.s=tmp;
  146. tmp=parse_content_type(tmp,end, &integer);
  147. if (tmp==0){
  148. LOG(L_ERR, "ERROR:get_hdr_field: bad content_type header\n");
  149. goto error;
  150. }
  151. hdr->parsed=(void*)integer;
  152. hdr->body.len=tmp-hdr->body.s;
  153. DBG("DEBUG: get_hdr_body : content_type=%d\n",(int)hdr->parsed);
  154. break;
  155. case HDR_FROM:
  156. case HDR_CALLID:
  157. case HDR_CONTACT:
  158. case HDR_ROUTE:
  159. case HDR_RECORDROUTE:
  160. case HDR_MAXFORWARDS:
  161. case HDR_AUTHORIZATION:
  162. case HDR_EXPIRES:
  163. case HDR_PROXYAUTH:
  164. case HDR_WWWAUTH:
  165. case HDR_SUPPORTED:
  166. case HDR_REQUIRE:
  167. case HDR_PROXYREQUIRE:
  168. case HDR_UNSUPPORTED:
  169. case HDR_ALLOW:
  170. case HDR_EVENT:
  171. case HDR_OTHER:
  172. /* just skip over it */
  173. hdr->body.s=tmp;
  174. /* find end of header */
  175. /* find lf */
  176. do{
  177. match=q_memchr(tmp, '\n', end-tmp);
  178. if (match){
  179. match++;
  180. }else {
  181. LOG(L_ERR,
  182. "ERROR: get_hdr_field: bad body for <%s>(%d)\n",
  183. hdr->name.s, hdr->type);
  184. /* abort(); */
  185. tmp=end;
  186. goto error;
  187. }
  188. tmp=match;
  189. }while( match<end &&( (*match==' ')||(*match=='\t') ) );
  190. *(match-1)=0; /*null terminate*/
  191. hdr->body.len=match-hdr->body.s;
  192. break;
  193. default:
  194. LOG(L_CRIT, "BUG: get_hdr_field: unknown header type %d\n",
  195. hdr->type);
  196. goto error;
  197. }
  198. /* jku: if \r covered by current length, shrink it */
  199. trim_r( hdr->body );
  200. return tmp;
  201. error:
  202. DBG("get_hdr_field: error exit\n");
  203. hdr->type=HDR_ERROR;
  204. return tmp;
  205. }
  206. /* parse the headers and adds them to msg->headers and msg->to, from etc.
  207. * It stops when all the headers requested in flags were parsed, on error
  208. * (bad header) or end of headers */
  209. /* note: it continues where it previously stopped and goes ahead until
  210. end is encountered or desired HFs are found; if you call it twice
  211. for the same HF which is present only once, it will fail the second
  212. time; if you call it twice and the HF is found on second time too,
  213. it's not replaced in the well-known HF pointer but just added to
  214. header list; if you want to use a dumbie convenience function which will
  215. give you the first occurance of a header you are interested in,
  216. look at check_transaction_quadruple
  217. */
  218. int parse_headers(struct sip_msg* msg, int flags, int next)
  219. {
  220. struct hdr_field* hf;
  221. char* tmp;
  222. char* rest;
  223. char* end;
  224. int orig_flag;
  225. end=msg->buf+msg->len;
  226. tmp=msg->unparsed;
  227. if (next) {
  228. orig_flag = msg->parsed_flag;
  229. msg->parsed_flag &= ~flags;
  230. }else
  231. orig_flag=0;
  232. DBG("parse_headers: flags=%d\n", flags);
  233. while( tmp<end && (flags & msg->parsed_flag) != flags){
  234. hf=pkg_malloc(sizeof(struct hdr_field));
  235. if (hf==0){
  236. ser_error=E_OUT_OF_MEM;
  237. LOG(L_ERR, "ERROR:parse_headers: memory allocation error\n");
  238. goto error;
  239. }
  240. memset(hf,0, sizeof(struct hdr_field));
  241. hf->type=HDR_ERROR;
  242. rest=get_hdr_field(tmp, msg->buf+msg->len, hf);
  243. switch (hf->type){
  244. case HDR_ERROR:
  245. LOG(L_INFO,"ERROR: bad header field\n");
  246. goto error;
  247. case HDR_EOH:
  248. msg->eoh=tmp; /* or rest?*/
  249. msg->parsed_flag|=HDR_EOH;
  250. pkg_free(hf);
  251. goto skip;
  252. case HDR_OTHER: /*do nothing*/
  253. break;
  254. case HDR_CALLID:
  255. if (msg->callid==0) msg->callid=hf;
  256. msg->parsed_flag|=HDR_CALLID;
  257. break;
  258. case HDR_TO:
  259. if (msg->to==0) msg->to=hf;
  260. msg->parsed_flag|=HDR_TO;
  261. break;
  262. case HDR_CSEQ:
  263. if (msg->cseq==0) msg->cseq=hf;
  264. msg->parsed_flag|=HDR_CSEQ;
  265. break;
  266. case HDR_FROM:
  267. if (msg->from==0) msg->from=hf;
  268. msg->parsed_flag|=HDR_FROM;
  269. break;
  270. case HDR_CONTACT:
  271. if (msg->contact==0) msg->contact=hf;
  272. msg->parsed_flag|=HDR_CONTACT;
  273. break;
  274. case HDR_MAXFORWARDS:
  275. if(msg->maxforwards==0) msg->maxforwards=hf;
  276. msg->parsed_flag|=HDR_MAXFORWARDS;
  277. break;
  278. case HDR_ROUTE:
  279. if (msg->route==0) msg->route=hf;
  280. msg->parsed_flag|=HDR_ROUTE;
  281. break;
  282. case HDR_RECORDROUTE:
  283. if (msg->record_route==0) msg->record_route = hf;
  284. msg->parsed_flag|=HDR_RECORDROUTE;
  285. break;
  286. case HDR_CONTENTTYPE:
  287. if (msg->content_type==0) msg->content_type = hf;
  288. msg->parsed_flag|=HDR_CONTENTTYPE;
  289. break;
  290. case HDR_CONTENTLENGTH:
  291. if (msg->content_length==0) msg->content_length = hf;
  292. msg->parsed_flag|=HDR_CONTENTLENGTH;
  293. break;
  294. case HDR_AUTHORIZATION:
  295. if (msg->authorization==0) msg->authorization = hf;
  296. msg->parsed_flag|=HDR_AUTHORIZATION;
  297. break;
  298. case HDR_EXPIRES:
  299. if (msg->expires==0) msg->expires = hf;
  300. msg->parsed_flag|=HDR_EXPIRES;
  301. break;
  302. case HDR_PROXYAUTH:
  303. if (msg->proxy_auth==0) msg->proxy_auth = hf;
  304. msg->parsed_flag|=HDR_PROXYAUTH;
  305. break;
  306. case HDR_WWWAUTH:
  307. if (msg->www_auth==0) msg->www_auth = hf;
  308. msg->parsed_flag|=HDR_WWWAUTH;
  309. break;
  310. case HDR_SUPPORTED:
  311. if (msg->supported==0) msg->supported = hf;
  312. msg->parsed_flag|=HDR_SUPPORTED;
  313. break;
  314. case HDR_REQUIRE:
  315. if (msg->require==0) msg->require = hf;
  316. msg->parsed_flag|=HDR_REQUIRE;
  317. break;
  318. case HDR_PROXYREQUIRE:
  319. if (msg->proxy_require==0) msg->proxy_require = hf;
  320. msg->parsed_flag|=HDR_PROXYREQUIRE;
  321. break;
  322. case HDR_UNSUPPORTED:
  323. if (msg->unsupported==0) msg->unsupported=hf;
  324. msg->parsed_flag|=HDR_UNSUPPORTED;
  325. break;
  326. case HDR_ALLOW:
  327. if (msg->allow==0) msg->allow = hf;
  328. msg->parsed_flag|=HDR_ALLOW;
  329. break;
  330. case HDR_EVENT:
  331. if (msg->allow==0) msg->event = hf;
  332. msg->parsed_flag|=HDR_EVENT;
  333. break;
  334. case HDR_VIA:
  335. msg->parsed_flag|=HDR_VIA;
  336. DBG("parse_headers: Via found, flags=%d\n", flags);
  337. if (msg->via1==0) {
  338. DBG("parse_headers: this is the first via\n");
  339. msg->h_via1=hf;
  340. msg->via1=hf->parsed;
  341. if (msg->via1->next){
  342. msg->via2=msg->via1->next;
  343. msg->parsed_flag|=HDR_VIA2;
  344. }
  345. }else if (msg->via2==0){
  346. msg->h_via2=hf;
  347. msg->via2=hf->parsed;
  348. msg->parsed_flag|=HDR_VIA2;
  349. DBG("parse_headers: this is the second via\n");
  350. }
  351. break;
  352. default:
  353. LOG(L_CRIT, "BUG: parse_headers: unknown header type %d\n",
  354. hf->type);
  355. goto error;
  356. }
  357. /* add the header to the list*/
  358. if (msg->last_header==0){
  359. msg->headers=hf;
  360. msg->last_header=hf;
  361. }else{
  362. msg->last_header->next=hf;
  363. msg->last_header=hf;
  364. }
  365. #ifdef EXTRA_DEBUG
  366. DBG("header field type %d, name=<%s>, body=<%s>\n",
  367. hf->type, hf->name.s, hf->body.s);
  368. #endif
  369. tmp=rest;
  370. }
  371. skip:
  372. msg->unparsed=tmp;
  373. return 0;
  374. error:
  375. ser_error=E_BAD_REQ;
  376. if (hf) pkg_free(hf);
  377. if (next) msg->parsed_flag |= orig_flag;
  378. return -1;
  379. }
  380. /* returns 0 if ok, -1 for errors */
  381. int parse_msg(char* buf, unsigned int len, struct sip_msg* msg)
  382. {
  383. char *tmp;
  384. char* rest;
  385. char* first_via;
  386. char* second_via;
  387. struct msg_start *fl;
  388. int offset;
  389. int flags;
  390. /* eat crlf from the beginning */
  391. for (tmp=buf; (*tmp=='\n' || *tmp=='\r')&&
  392. tmp-buf < len ; tmp++);
  393. offset=tmp-buf;
  394. fl=&(msg->first_line);
  395. rest=parse_first_line(tmp, len-offset, fl);
  396. #if 0
  397. rest=parse_fline(tmp, buf+len, fl);
  398. #endif
  399. offset+=rest-tmp;
  400. tmp=rest;
  401. switch(fl->type){
  402. case SIP_INVALID:
  403. DBG("parse_msg: invalid message\n");
  404. goto error;
  405. break;
  406. case SIP_REQUEST:
  407. DBG("SIP Request:\n");
  408. DBG(" method: <%s>\n",fl->u.request.method.s);
  409. DBG(" uri: <%s>\n",fl->u.request.uri.s);
  410. DBG(" version: <%s>\n",fl->u.request.version.s);
  411. flags=HDR_VIA;
  412. break;
  413. case SIP_REPLY:
  414. DBG("SIP Reply (status):\n");
  415. DBG(" version: <%s>\n",fl->u.reply.version.s);
  416. DBG(" status: <%s>\n",fl->u.reply.status.s);
  417. DBG(" reason: <%s>\n",fl->u.reply.reason.s);
  418. /* flags=HDR_VIA | HDR_VIA2; */
  419. /* we don't try to parse VIA2 for local messages; -Jiri */
  420. flags=HDR_VIA;
  421. break;
  422. default:
  423. DBG("unknown type %d\n",fl->type);
  424. goto error;
  425. }
  426. msg->unparsed=tmp;
  427. /*find first Via: */
  428. first_via=0;
  429. second_via=0;
  430. if (parse_headers(msg, flags, 0)==-1) goto error;
  431. #ifdef EXTRA_DEBUG
  432. /* dump parsed data */
  433. if (msg->via1){
  434. DBG(" first via: <%s/%s/%s> <%s:%s(%d)>",
  435. msg->via1->name.s, msg->via1->version.s,
  436. msg->via1->transport.s, msg->via1->host.s,
  437. msg->via1->port_str.s, msg->via1->port);
  438. if (msg->via1->params.s) DBG(";<%s>", msg->via1->params.s);
  439. if (msg->via1->comment.s) DBG(" <%s>", msg->via1->comment.s);
  440. DBG ("\n");
  441. }
  442. if (msg->via2){
  443. DBG(" first via: <%s/%s/%s> <%s:%s(%d)>",
  444. msg->via2->name.s, msg->via2->version.s,
  445. msg->via2->transport.s, msg->via2->host.s,
  446. msg->via2->port_str.s, msg->via2->port);
  447. if (msg->via2->params.s) DBG(";<%s>", msg->via2->params.s);
  448. if (msg->via2->comment.s) DBG(" <%s>", msg->via2->comment.s);
  449. DBG ("\n");
  450. }
  451. #endif
  452. #ifdef EXTRA_DEBUG
  453. DBG("exiting parse_msg\n");
  454. #endif
  455. return 0;
  456. error:
  457. /* more debugging, msg->orig is/should be null terminated*/
  458. LOG(L_ERR, "ERROR: parse_msg: message=<%.*s>\n", (int)msg->len, msg->orig);
  459. return -1;
  460. }
  461. void free_reply_lump( struct lump_rpl *lump)
  462. {
  463. struct lump_rpl *foo, *bar;
  464. for(foo=lump;foo;)
  465. {
  466. bar=foo->next;
  467. free_lump_rpl(foo);
  468. foo = bar;
  469. }
  470. }
  471. /*only the content*/
  472. void free_sip_msg(struct sip_msg* msg)
  473. {
  474. if (msg->new_uri.s) { pkg_free(msg->new_uri.s); msg->new_uri.len=0; }
  475. if (msg->headers) free_hdr_field_lst(msg->headers);
  476. if (msg->add_rm) free_lump_list(msg->add_rm);
  477. if (msg->repl_add_rm) free_lump_list(msg->repl_add_rm);
  478. if (msg->reply_lump) free_reply_lump(msg->reply_lump);
  479. pkg_free(msg->orig);
  480. /* don't free anymore -- now a pointer to a static buffer */
  481. # ifdef DYN_BUF
  482. pkg_free(msg->buf);
  483. # endif
  484. }
  485. /* make sure all HFs needed for transaction identification have been
  486. parsed; return 0 if those HFs can't be found
  487. */
  488. int check_transaction_quadruple( struct sip_msg* msg )
  489. {
  490. if ( parse_headers(msg, HDR_FROM|HDR_TO|HDR_CALLID|HDR_CSEQ,0)!=-1
  491. && msg->from && msg->to && msg->callid && msg->cseq ) {
  492. return 1;
  493. } else {
  494. ser_error=E_BAD_TUPEL;
  495. return 0;
  496. }
  497. }