msg_parser.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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. * History:
  31. * ---------
  32. * 2003-02-28 scratchpad compatibility abandoned (jiri)
  33. * 2003-01-29 scrathcpad removed (jiri)
  34. * 2003-01-27 next baby-step to removing ZT - PRESERVE_ZT (jiri)
  35. * 2003-03-31 removed msg->repl_add_rm (andrei)
  36. * 2003-04-26 ZSW (jiri)
  37. * 2003-05-01 parser extended to support Accept header field (janakj)
  38. * 2005-02-23 parse_headers uses hdr_flags_t now (andrei)
  39. * 2005-03-02 free_via_list(vb) on via parse error (andrei)
  40. * 2007-01-26 parser extended to support Identity, Identity-info and Date
  41. * header fields (gergo)
  42. */
  43. /*! \file
  44. * \brief Parser :: SIP Message header proxy parser
  45. *
  46. * \ingroup parser
  47. */
  48. /*! \defgroup parser SIP-router SIP message parser
  49. *
  50. * The SIP message parser
  51. *
  52. */
  53. #include <string.h>
  54. #include <stdlib.h>
  55. #include "../comp_defs.h"
  56. #include "msg_parser.h"
  57. #include "parser_f.h"
  58. #include "../ut.h"
  59. #include "../error.h"
  60. #include "../dprint.h"
  61. #include "../data_lump_rpl.h"
  62. #include "../mem/mem.h"
  63. #include "../error.h"
  64. #include "../globals.h"
  65. #include "parse_hname2.h"
  66. #include "parse_uri.h"
  67. #include "parse_content.h"
  68. #include "parse_to.h"
  69. #include "../compiler_opt.h"
  70. #ifdef DEBUG_DMALLOC
  71. #include <mem/dmalloc.h>
  72. #endif
  73. #define parse_hname(_b,_e,_h) parse_hname2((_b),(_e),(_h))
  74. /* number of via's encountered */
  75. int via_cnt;
  76. /* returns pointer to next header line, and fill hdr_f ;
  77. * if at end of header returns pointer to the last crlf (always buf)*/
  78. char* get_hdr_field(char* buf, char* end, struct hdr_field* hdr)
  79. {
  80. char* tmp;
  81. char *match;
  82. struct via_body *vb;
  83. struct cseq_body* cseq_b;
  84. struct to_body* to_b;
  85. int integer, err;
  86. unsigned uval;
  87. if ((*buf)=='\n' || (*buf)=='\r'){
  88. /* double crlf or lflf or crcr */
  89. DBG("found end of header\n");
  90. hdr->type=HDR_EOH_T;
  91. return buf;
  92. }
  93. tmp=parse_hname(buf, end, hdr);
  94. if (hdr->type==HDR_ERROR_T){
  95. LOG(L_ERR, "ERROR: get_hdr_field: bad header\n");
  96. goto error;
  97. }
  98. /* eliminate leading whitespace */
  99. tmp=eat_lws_end(tmp, end);
  100. if (tmp>=end) {
  101. LOG(L_ERR, "ERROR: get_hdr_field: HF empty\n");
  102. goto error;
  103. }
  104. /* if header-field well-known, parse it, find its end otherwise ;
  105. * after leaving the hdr->type switch, tmp should be set to the
  106. * next header field
  107. */
  108. switch(hdr->type){
  109. case HDR_VIA_T:
  110. /* keep number of vias parsed -- we want to report it in
  111. replies for diagnostic purposes */
  112. via_cnt++;
  113. vb=pkg_malloc(sizeof(struct via_body));
  114. if (vb==0){
  115. LOG(L_ERR, "get_hdr_field: out of memory\n");
  116. goto error;
  117. }
  118. memset(vb,0,sizeof(struct via_body));
  119. hdr->body.s=tmp;
  120. tmp=parse_via(tmp, end, vb);
  121. if (vb->error==PARSE_ERROR){
  122. LOG(L_ERR, "ERROR: get_hdr_field: bad via\n");
  123. free_via_list(vb);
  124. goto error;
  125. }
  126. hdr->parsed=vb;
  127. vb->hdr.s=hdr->name.s;
  128. vb->hdr.len=hdr->name.len;
  129. hdr->body.len=tmp-hdr->body.s;
  130. break;
  131. case HDR_CSEQ_T:
  132. cseq_b=pkg_malloc(sizeof(struct cseq_body));
  133. if (cseq_b==0){
  134. LOG(L_ERR, "get_hdr_field: out of memory\n");
  135. goto error;
  136. }
  137. memset(cseq_b, 0, sizeof(struct cseq_body));
  138. hdr->body.s=tmp;
  139. tmp=parse_cseq(tmp, end, cseq_b);
  140. if (cseq_b->error==PARSE_ERROR){
  141. LOG(L_ERR, "ERROR: get_hdr_field: bad cseq\n");
  142. pkg_free(cseq_b);
  143. goto error;
  144. }
  145. hdr->parsed=cseq_b;
  146. hdr->body.len=tmp-hdr->body.s;
  147. DBG("get_hdr_field: cseq <%.*s>: <%.*s> <%.*s>\n",
  148. hdr->name.len, ZSW(hdr->name.s),
  149. cseq_b->number.len, ZSW(cseq_b->number.s),
  150. cseq_b->method.len, cseq_b->method.s);
  151. break;
  152. case HDR_TO_T:
  153. to_b=pkg_malloc(sizeof(struct to_body));
  154. if (to_b==0){
  155. LOG(L_ERR, "get_hdr_field: out of memory\n");
  156. goto error;
  157. }
  158. memset(to_b, 0, sizeof(struct to_body));
  159. hdr->body.s=tmp;
  160. tmp=parse_to(tmp, end,to_b);
  161. if (to_b->error==PARSE_ERROR){
  162. LOG(L_ERR, "ERROR: get_hdr_field: bad to header\n");
  163. pkg_free(to_b);
  164. goto error;
  165. }
  166. hdr->parsed=to_b;
  167. hdr->body.len=tmp-hdr->body.s;
  168. DBG("DEBUG: get_hdr_field: <%.*s> [%d]; uri=[%.*s] \n",
  169. hdr->name.len, ZSW(hdr->name.s),
  170. hdr->body.len, to_b->uri.len,ZSW(to_b->uri.s));
  171. DBG("DEBUG: to body [%.*s]\n",to_b->body.len,
  172. ZSW(to_b->body.s));
  173. break;
  174. case HDR_CONTENTLENGTH_T:
  175. hdr->body.s=tmp;
  176. tmp=parse_content_length(tmp,end, &integer);
  177. if (tmp==0){
  178. LOG(L_ERR, "ERROR:get_hdr_field: bad content_length header\n");
  179. goto error;
  180. }
  181. hdr->parsed=(void*)(long)integer;
  182. hdr->body.len=tmp-hdr->body.s;
  183. DBG("DEBUG: get_hdr_body : content_length=%d\n",
  184. (int)(long)hdr->parsed);
  185. break;
  186. case HDR_RETRY_AFTER_T:
  187. hdr->body.s=tmp;
  188. tmp=parse_retry_after(tmp,end, &uval, &err);
  189. if (err){
  190. LOG(L_ERR, "ERROR:get_hdr_field: bad retry_after header\n");
  191. goto error;
  192. }
  193. hdr->parsed=(void*)(unsigned long)uval;
  194. hdr->body.len=tmp-hdr->body.s;
  195. DBG("DEBUG: get_hdr_body : retry_after=%d\n",
  196. (unsigned)(long)hdr->parsed);
  197. break;
  198. case HDR_IDENTITY_T:
  199. case HDR_DATE_T:
  200. case HDR_IDENTITY_INFO_T:
  201. case HDR_SUPPORTED_T:
  202. case HDR_REQUIRE_T:
  203. case HDR_CONTENTTYPE_T:
  204. case HDR_FROM_T:
  205. case HDR_CALLID_T:
  206. case HDR_CONTACT_T:
  207. case HDR_ROUTE_T:
  208. case HDR_RECORDROUTE_T:
  209. case HDR_MAXFORWARDS_T:
  210. case HDR_AUTHORIZATION_T:
  211. case HDR_EXPIRES_T:
  212. case HDR_PROXYAUTH_T:
  213. case HDR_PROXYREQUIRE_T:
  214. case HDR_UNSUPPORTED_T:
  215. case HDR_ALLOW_T:
  216. case HDR_EVENT_T:
  217. case HDR_ACCEPT_T:
  218. case HDR_ACCEPTLANGUAGE_T:
  219. case HDR_ORGANIZATION_T:
  220. case HDR_PRIORITY_T:
  221. case HDR_SUBJECT_T:
  222. case HDR_USERAGENT_T:
  223. case HDR_SERVER_T:
  224. case HDR_CONTENTDISPOSITION_T:
  225. case HDR_ACCEPTDISPOSITION_T:
  226. case HDR_DIVERSION_T:
  227. case HDR_RPID_T:
  228. case HDR_SIPIFMATCH_T:
  229. case HDR_REFER_TO_T:
  230. case HDR_SESSIONEXPIRES_T:
  231. case HDR_MIN_SE_T:
  232. case HDR_SUBSCRIPTION_STATE_T:
  233. case HDR_ACCEPTCONTACT_T:
  234. case HDR_ALLOWEVENTS_T:
  235. case HDR_CONTENTENCODING_T:
  236. case HDR_REFERREDBY_T:
  237. case HDR_REJECTCONTACT_T:
  238. case HDR_REQUESTDISPOSITION_T:
  239. case HDR_WWW_AUTHENTICATE_T:
  240. case HDR_PROXY_AUTHENTICATE_T:
  241. case HDR_PATH_T:
  242. case HDR_PRIVACY_T:
  243. case HDR_PAI_T:
  244. case HDR_PPI_T:
  245. case HDR_OTHER_T:
  246. /* just skip over it */
  247. hdr->body.s=tmp;
  248. /* find end of header */
  249. /* find lf */
  250. do{
  251. match=q_memchr(tmp, '\n', end-tmp);
  252. if (match){
  253. match++;
  254. }else {
  255. LOG(L_ERR,
  256. "ERROR: get_hdr_field: bad body for <%s>(%d)\n",
  257. hdr->name.s, hdr->type);
  258. /* abort(); */
  259. tmp=end;
  260. goto error;
  261. }
  262. tmp=match;
  263. }while( match<end &&( (*match==' ')||(*match=='\t') ) );
  264. tmp=match;
  265. hdr->body.len=match-hdr->body.s;
  266. break;
  267. default:
  268. LOG(L_CRIT, "BUG: get_hdr_field: unknown header type %d\n",
  269. hdr->type);
  270. goto error;
  271. }
  272. /* jku: if \r covered by current length, shrink it */
  273. trim_r( hdr->body );
  274. hdr->len=tmp-hdr->name.s;
  275. return tmp;
  276. error:
  277. DBG("get_hdr_field: error exit\n");
  278. hdr->type=HDR_ERROR_T;
  279. hdr->len=tmp-hdr->name.s;
  280. return tmp;
  281. }
  282. /* parse the headers and adds them to msg->headers and msg->to, from etc.
  283. * It stops when all the headers requested in flags were parsed, on error
  284. * (bad header) or end of headers
  285. * WARNING: parse_headers was changed to use hdr_flags_t (the flags are now
  286. * different from the header types). Don't call it with a header type
  287. * (HDR_xxx_T), only with header flags (HDR_xxx_F)!*/
  288. /* note: it continues where it previously stopped and goes ahead until
  289. end is encountered or desired HFs are found; if you call it twice
  290. for the same HF which is present only once, it will fail the second
  291. time; if you call it twice and the HF is found on second time too,
  292. it's not replaced in the well-known HF pointer but just added to
  293. header list; if you want to use a dumb convenience function which will
  294. give you the first occurrence of a header you are interested in,
  295. look at check_transaction_quadruple
  296. */
  297. int parse_headers(struct sip_msg* msg, hdr_flags_t flags, int next)
  298. {
  299. struct hdr_field* hf;
  300. char* tmp;
  301. char* rest;
  302. char* end;
  303. hdr_flags_t orig_flag;
  304. end=msg->buf+msg->len;
  305. tmp=msg->unparsed;
  306. if (unlikely(next)) {
  307. orig_flag = msg->parsed_flag;
  308. msg->parsed_flag &= ~flags;
  309. }else
  310. orig_flag=0;
  311. #ifdef EXTRA_DEBUG
  312. DBG("parse_headers: flags=%llx\n", (unsigned long long)flags);
  313. #endif
  314. while( tmp<end && (flags & msg->parsed_flag) != flags){
  315. prefetch_loc_r(tmp+64, 1);
  316. hf=pkg_malloc(sizeof(struct hdr_field));
  317. if (unlikely(hf==0)){
  318. ser_error=E_OUT_OF_MEM;
  319. LOG(L_ERR, "ERROR:parse_headers: memory allocation error\n");
  320. goto error;
  321. }
  322. memset(hf,0, sizeof(struct hdr_field));
  323. hf->type=HDR_ERROR_T;
  324. rest=get_hdr_field(tmp, end, hf);
  325. switch (hf->type){
  326. case HDR_ERROR_T:
  327. LOG(L_INFO,"ERROR: bad header field\n");
  328. goto error;
  329. case HDR_EOH_T:
  330. msg->eoh=tmp; /* or rest?*/
  331. msg->parsed_flag|=HDR_EOH_F;
  332. pkg_free(hf);
  333. goto skip;
  334. case HDR_ACCEPTCONTACT_T:
  335. case HDR_ALLOWEVENTS_T:
  336. case HDR_CONTENTENCODING_T:
  337. case HDR_REFERREDBY_T:
  338. case HDR_REJECTCONTACT_T:
  339. case HDR_REQUESTDISPOSITION_T:
  340. case HDR_WWW_AUTHENTICATE_T:
  341. case HDR_PROXY_AUTHENTICATE_T:
  342. case HDR_RETRY_AFTER_T:
  343. case HDR_OTHER_T: /*do nothing*/
  344. break;
  345. case HDR_CALLID_T:
  346. if (msg->callid==0) msg->callid=hf;
  347. msg->parsed_flag|=HDR_CALLID_F;
  348. break;
  349. case HDR_SIPIFMATCH_T:
  350. if (msg->sipifmatch==0) msg->sipifmatch=hf;
  351. msg->parsed_flag|=HDR_SIPIFMATCH_F;
  352. break;
  353. case HDR_TO_T:
  354. if (msg->to==0) msg->to=hf;
  355. msg->parsed_flag|=HDR_TO_F;
  356. break;
  357. case HDR_CSEQ_T:
  358. if (msg->cseq==0) msg->cseq=hf;
  359. msg->parsed_flag|=HDR_CSEQ_F;
  360. break;
  361. case HDR_FROM_T:
  362. if (msg->from==0) msg->from=hf;
  363. msg->parsed_flag|=HDR_FROM_F;
  364. break;
  365. case HDR_CONTACT_T:
  366. if (msg->contact==0) msg->contact=hf;
  367. msg->parsed_flag|=HDR_CONTACT_F;
  368. break;
  369. case HDR_MAXFORWARDS_T:
  370. if(msg->maxforwards==0) msg->maxforwards=hf;
  371. msg->parsed_flag|=HDR_MAXFORWARDS_F;
  372. break;
  373. case HDR_ROUTE_T:
  374. if (msg->route==0) msg->route=hf;
  375. msg->parsed_flag|=HDR_ROUTE_F;
  376. break;
  377. case HDR_RECORDROUTE_T:
  378. if (msg->record_route==0) msg->record_route = hf;
  379. msg->parsed_flag|=HDR_RECORDROUTE_F;
  380. break;
  381. case HDR_CONTENTTYPE_T:
  382. if (msg->content_type==0) msg->content_type = hf;
  383. msg->parsed_flag|=HDR_CONTENTTYPE_F;
  384. break;
  385. case HDR_CONTENTLENGTH_T:
  386. if (msg->content_length==0) msg->content_length = hf;
  387. msg->parsed_flag|=HDR_CONTENTLENGTH_F;
  388. break;
  389. case HDR_AUTHORIZATION_T:
  390. if (msg->authorization==0) msg->authorization = hf;
  391. msg->parsed_flag|=HDR_AUTHORIZATION_F;
  392. break;
  393. case HDR_EXPIRES_T:
  394. if (msg->expires==0) msg->expires = hf;
  395. msg->parsed_flag|=HDR_EXPIRES_F;
  396. break;
  397. case HDR_PROXYAUTH_T:
  398. if (msg->proxy_auth==0) msg->proxy_auth = hf;
  399. msg->parsed_flag|=HDR_PROXYAUTH_F;
  400. break;
  401. case HDR_PROXYREQUIRE_T:
  402. if (msg->proxy_require==0) msg->proxy_require = hf;
  403. msg->parsed_flag|=HDR_PROXYREQUIRE_F;
  404. break;
  405. case HDR_SUPPORTED_T:
  406. if (msg->supported==0) msg->supported=hf;
  407. msg->parsed_flag|=HDR_SUPPORTED_F;
  408. break;
  409. case HDR_REQUIRE_T:
  410. if (msg->require==0) msg->require=hf;
  411. msg->parsed_flag|=HDR_REQUIRE_F;
  412. break;
  413. case HDR_UNSUPPORTED_T:
  414. if (msg->unsupported==0) msg->unsupported=hf;
  415. msg->parsed_flag|=HDR_UNSUPPORTED_F;
  416. break;
  417. case HDR_ALLOW_T:
  418. if (msg->allow==0) msg->allow = hf;
  419. msg->parsed_flag|=HDR_ALLOW_F;
  420. break;
  421. case HDR_EVENT_T:
  422. if (msg->event==0) msg->event = hf;
  423. msg->parsed_flag|=HDR_EVENT_F;
  424. break;
  425. case HDR_ACCEPT_T:
  426. if (msg->accept==0) msg->accept = hf;
  427. msg->parsed_flag|=HDR_ACCEPT_F;
  428. break;
  429. case HDR_ACCEPTLANGUAGE_T:
  430. if (msg->accept_language==0) msg->accept_language = hf;
  431. msg->parsed_flag|=HDR_ACCEPTLANGUAGE_F;
  432. break;
  433. case HDR_ORGANIZATION_T:
  434. if (msg->organization==0) msg->organization = hf;
  435. msg->parsed_flag|=HDR_ORGANIZATION_F;
  436. break;
  437. case HDR_PRIORITY_T:
  438. if (msg->priority==0) msg->priority = hf;
  439. msg->parsed_flag|=HDR_PRIORITY_F;
  440. break;
  441. case HDR_SUBJECT_T:
  442. if (msg->subject==0) msg->subject = hf;
  443. msg->parsed_flag|=HDR_SUBJECT_F;
  444. break;
  445. case HDR_USERAGENT_T:
  446. if (msg->user_agent==0) msg->user_agent = hf;
  447. msg->parsed_flag|=HDR_USERAGENT_F;
  448. break;
  449. case HDR_SERVER_T:
  450. if (msg->server==0) msg->server = hf;
  451. msg->parsed_flag|=HDR_SERVER_F;
  452. break;
  453. case HDR_CONTENTDISPOSITION_T:
  454. if (msg->content_disposition==0) msg->content_disposition = hf;
  455. msg->parsed_flag|=HDR_CONTENTDISPOSITION_F;
  456. break;
  457. case HDR_ACCEPTDISPOSITION_T:
  458. if (msg->accept_disposition==0) msg->accept_disposition = hf;
  459. msg->parsed_flag|=HDR_ACCEPTDISPOSITION_F;
  460. break;
  461. case HDR_DIVERSION_T:
  462. if (msg->diversion==0) msg->diversion = hf;
  463. msg->parsed_flag|=HDR_DIVERSION_F;
  464. break;
  465. case HDR_RPID_T:
  466. if (msg->rpid==0) msg->rpid = hf;
  467. msg->parsed_flag|=HDR_RPID_F;
  468. break;
  469. case HDR_REFER_TO_T:
  470. if (msg->refer_to==0) msg->refer_to = hf;
  471. msg->parsed_flag|=HDR_REFER_TO_F;
  472. break;
  473. case HDR_SESSIONEXPIRES_T:
  474. if (msg->session_expires==0) msg->session_expires = hf;
  475. msg->parsed_flag|=HDR_SESSIONEXPIRES_F;
  476. break;
  477. case HDR_MIN_SE_T:
  478. if (msg->min_se==0) msg->min_se = hf;
  479. msg->parsed_flag|=HDR_MIN_SE_F;
  480. break;
  481. case HDR_SUBSCRIPTION_STATE_T:
  482. if (msg->subscription_state==0) msg->subscription_state = hf;
  483. msg->parsed_flag|=HDR_SUBSCRIPTION_STATE_F;
  484. break;
  485. case HDR_VIA_T:
  486. msg->parsed_flag|=HDR_VIA_F;
  487. DBG("parse_headers: Via found, flags=%llx\n",
  488. (unsigned long long)flags);
  489. if (msg->via1==0) {
  490. DBG("parse_headers: this is the first via\n");
  491. msg->h_via1=hf;
  492. msg->via1=hf->parsed;
  493. if (msg->via1->next){
  494. msg->via2=msg->via1->next;
  495. msg->parsed_flag|=HDR_VIA2_F;
  496. }
  497. }else if (msg->via2==0){
  498. msg->h_via2=hf;
  499. msg->via2=hf->parsed;
  500. msg->parsed_flag|=HDR_VIA2_F;
  501. DBG("parse_headers: this is the second via\n");
  502. }
  503. break;
  504. case HDR_DATE_T:
  505. if (msg->date==0) msg->date=hf;
  506. msg->parsed_flag|=HDR_DATE_F;
  507. break;
  508. case HDR_IDENTITY_T:
  509. if (msg->identity==0) msg->identity=hf;
  510. msg->parsed_flag|=HDR_IDENTITY_F;
  511. break;
  512. case HDR_IDENTITY_INFO_T:
  513. if (msg->identity_info==0) msg->identity_info=hf;
  514. msg->parsed_flag|=HDR_IDENTITY_INFO_F;
  515. break;
  516. case HDR_PATH_T:
  517. if (msg->path==0) msg->path=hf;
  518. msg->parsed_flag|=HDR_PATH_F;
  519. break;
  520. case HDR_PRIVACY_T:
  521. if (msg->privacy==0) msg->privacy=hf;
  522. msg->parsed_flag|=HDR_PRIVACY_F;
  523. break;
  524. case HDR_PAI_T:
  525. if (msg->pai==0) msg->pai=hf;
  526. msg->parsed_flag|=HDR_PAI_F;
  527. break;
  528. case HDR_PPI_T:
  529. if (msg->ppi==0) msg->ppi=hf;
  530. msg->parsed_flag|=HDR_PPI_F;
  531. break;
  532. default:
  533. LOG(L_CRIT, "BUG: parse_headers: unknown header type %d\n",
  534. hf->type);
  535. goto error;
  536. }
  537. /* add the header to the list*/
  538. if (msg->last_header==0){
  539. msg->headers=hf;
  540. msg->last_header=hf;
  541. }else{
  542. msg->last_header->next=hf;
  543. msg->last_header=hf;
  544. }
  545. #ifdef EXTRA_DEBUG
  546. DBG("header field type %d, name=<%.*s>, body=<%.*s>\n",
  547. hf->type,
  548. hf->name.len, ZSW(hf->name.s),
  549. hf->body.len, ZSW(hf->body.s));
  550. #endif
  551. tmp=rest;
  552. }
  553. skip:
  554. msg->unparsed=tmp;
  555. /* restore original flags */
  556. msg->parsed_flag |= orig_flag;
  557. return 0;
  558. error:
  559. ser_error=E_BAD_REQ;
  560. if (hf) pkg_free(hf);
  561. /* restore original flags */
  562. msg->parsed_flag |= orig_flag;
  563. return -1;
  564. }
  565. /* returns 0 if ok, -1 for errors */
  566. int parse_msg(char* buf, unsigned int len, struct sip_msg* msg)
  567. {
  568. char *tmp;
  569. char* rest;
  570. char* first_via;
  571. char* second_via;
  572. struct msg_start *fl;
  573. int offset;
  574. hdr_flags_t flags;
  575. /* eat crlf from the beginning */
  576. for (tmp=buf; (*tmp=='\n' || *tmp=='\r')&&
  577. tmp-buf < len ; tmp++);
  578. offset=tmp-buf;
  579. fl=&(msg->first_line);
  580. rest=parse_first_line(tmp, len-offset, fl);
  581. #if 0
  582. rest=parse_fline(tmp, buf+len, fl);
  583. #endif
  584. offset+=rest-tmp;
  585. tmp=rest;
  586. switch(fl->type){
  587. case SIP_INVALID:
  588. DBG("parse_msg: invalid message\n");
  589. goto error;
  590. break;
  591. case SIP_REQUEST:
  592. DBG("SIP Request:\n");
  593. DBG(" method: <%.*s>\n",fl->u.request.method.len,
  594. ZSW(fl->u.request.method.s));
  595. DBG(" uri: <%.*s>\n",fl->u.request.uri.len,
  596. ZSW(fl->u.request.uri.s));
  597. DBG(" version: <%.*s>\n",fl->u.request.version.len,
  598. ZSW(fl->u.request.version.s));
  599. flags=HDR_VIA_F;
  600. break;
  601. case SIP_REPLY:
  602. DBG("SIP Reply (status):\n");
  603. DBG(" version: <%.*s>\n",fl->u.reply.version.len,
  604. ZSW(fl->u.reply.version.s));
  605. DBG(" status: <%.*s>\n", fl->u.reply.status.len,
  606. ZSW(fl->u.reply.status.s));
  607. DBG(" reason: <%.*s>\n", fl->u.reply.reason.len,
  608. ZSW(fl->u.reply.reason.s));
  609. /* flags=HDR_VIA | HDR_VIA2; */
  610. /* we don't try to parse VIA2 for local messages; -Jiri */
  611. flags=HDR_VIA_F;
  612. break;
  613. default:
  614. DBG("unknown type %d\n",fl->type);
  615. goto error;
  616. }
  617. msg->unparsed=tmp;
  618. /*find first Via: */
  619. first_via=0;
  620. second_via=0;
  621. if (parse_headers(msg, flags, 0)==-1) goto error;
  622. #ifdef EXTRA_DEBUG
  623. /* dump parsed data */
  624. if (msg->via1){
  625. DBG("first via: <%.*s/%.*s/%.*s> <%.*s:%.*s(%d)>",
  626. msg->via1->name.len,
  627. ZSW(msg->via1->name.s),
  628. msg->via1->version.len,
  629. ZSW(msg->via1->version.s),
  630. msg->via1->transport.len,
  631. ZSW(msg->via1->transport.s),
  632. msg->via1->host.len,
  633. ZSW(msg->via1->host.s),
  634. msg->via1->port_str.len,
  635. ZSW(msg->via1->port_str.s),
  636. msg->via1->port);
  637. if (msg->via1->params.s) DBG(";<%.*s>",
  638. msg->via1->params.len, ZSW(msg->via1->params.s));
  639. if (msg->via1->comment.s)
  640. DBG(" <%.*s>",
  641. msg->via1->comment.len, ZSW(msg->via1->comment.s));
  642. DBG ("\n");
  643. }
  644. if (msg->via2){
  645. DBG("second via: <%.*s/%.*s/%.*s> <%.*s:%.*s(%d)>",
  646. msg->via2->name.len,
  647. ZSW(msg->via2->name.s),
  648. msg->via2->version.len,
  649. ZSW(msg->via2->version.s),
  650. msg->via2->transport.len,
  651. ZSW(msg->via2->transport.s),
  652. msg->via2->host.len,
  653. ZSW(msg->via2->host.s),
  654. msg->via2->port_str.len,
  655. ZSW(msg->via2->port_str.s),
  656. msg->via2->port);
  657. if (msg->via2->params.s) DBG(";<%.*s>",
  658. msg->via2->params.len, ZSW(msg->via2->params.s));
  659. if (msg->via2->comment.s) DBG(" <%.*s>",
  660. msg->via2->comment.len, ZSW(msg->via2->comment.s));
  661. DBG ("\n");
  662. }
  663. #endif
  664. #ifdef EXTRA_DEBUG
  665. DBG("exiting parse_msg\n");
  666. #endif
  667. return 0;
  668. error:
  669. /* more debugging, msg->orig is/should be null terminated*/
  670. LOG(L_ERR, "ERROR: parse_msg: message=<%.*s>\n",
  671. (int)msg->len, ZSW(msg->buf));
  672. return -1;
  673. }
  674. void free_reply_lump( struct lump_rpl *lump)
  675. {
  676. struct lump_rpl *foo, *bar;
  677. for(foo=lump;foo;)
  678. {
  679. bar=foo->next;
  680. free_lump_rpl(foo);
  681. foo = bar;
  682. }
  683. }
  684. /*only the content*/
  685. void free_sip_msg(struct sip_msg* msg)
  686. {
  687. if (msg->new_uri.s) { pkg_free(msg->new_uri.s); msg->new_uri.len=0; }
  688. if (msg->dst_uri.s) { pkg_free(msg->dst_uri.s); msg->dst_uri.len=0; }
  689. if (msg->path_vec.s) { pkg_free(msg->path_vec.s); msg->path_vec.len=0; }
  690. if (msg->headers) free_hdr_field_lst(msg->headers);
  691. if (msg->body && msg->body->free) msg->body->free(&msg->body);
  692. if (msg->add_rm) free_lump_list(msg->add_rm);
  693. if (msg->body_lumps) free_lump_list(msg->body_lumps);
  694. if (msg->reply_lump) free_reply_lump(msg->reply_lump);
  695. /* don't free anymore -- now a pointer to a static buffer */
  696. # ifdef DYN_BUF
  697. pkg_free(msg->buf);
  698. # endif
  699. }
  700. /*
  701. * Make a private copy of the string and assign it to dst_uri
  702. */
  703. int set_dst_uri(struct sip_msg* msg, str* uri)
  704. {
  705. char* ptr;
  706. if (!msg || !uri) {
  707. LOG(L_ERR, "set_dst_uri: Invalid parameter value\n");
  708. return -1;
  709. }
  710. if (msg->dst_uri.s && (msg->dst_uri.len >= uri->len)) {
  711. memcpy(msg->dst_uri.s, uri->s, uri->len);
  712. msg->dst_uri.len = uri->len;
  713. } else {
  714. ptr = (char*)pkg_malloc(uri->len);
  715. if (!ptr) {
  716. LOG(L_ERR, "set_dst_uri: Not enough memory\n");
  717. return -1;
  718. }
  719. memcpy(ptr, uri->s, uri->len);
  720. if (msg->dst_uri.s) pkg_free(msg->dst_uri.s);
  721. msg->dst_uri.s = ptr;
  722. msg->dst_uri.len = uri->len;
  723. }
  724. return 0;
  725. }
  726. void reset_dst_uri(struct sip_msg* msg)
  727. {
  728. if(msg->dst_uri.s != 0) {
  729. pkg_free(msg->dst_uri.s);
  730. }
  731. msg->dst_uri.s = 0;
  732. msg->dst_uri.len = 0;
  733. }
  734. int set_path_vector(struct sip_msg* msg, str* path)
  735. {
  736. char* ptr;
  737. if (!msg || !path) {
  738. LM_ERR("invalid parameter value\n");
  739. return -1;
  740. }
  741. if (msg->path_vec.s && (msg->path_vec.len >= path->len)) {
  742. memcpy(msg->path_vec.s, path->s, path->len);
  743. msg->path_vec.len = path->len;
  744. } else {
  745. ptr = (char*)pkg_malloc(path->len);
  746. if (!ptr) {
  747. LM_ERR("not enough pkg memory\n");
  748. return -1;
  749. }
  750. memcpy(ptr, path->s, path->len);
  751. if (msg->path_vec.s) pkg_free(msg->path_vec.s);
  752. msg->path_vec.s = ptr;
  753. msg->path_vec.len = path->len;
  754. }
  755. return 0;
  756. }
  757. void reset_path_vector(struct sip_msg* msg)
  758. {
  759. if(msg->path_vec.s != 0) {
  760. pkg_free(msg->path_vec.s);
  761. }
  762. msg->path_vec.s = 0;
  763. msg->path_vec.len = 0;
  764. }
  765. struct hdr_field* get_hdr(struct sip_msg *msg, enum _hdr_types_t ht)
  766. {
  767. struct hdr_field *hdr;
  768. for(hdr = msg->headers; hdr; hdr = hdr->next) {
  769. if(hdr->type == ht) return hdr;
  770. }
  771. return NULL;
  772. }
  773. struct hdr_field* next_sibling_hdr(struct hdr_field *hf)
  774. {
  775. struct hdr_field *hdr;
  776. for(hdr = hf->next; hdr; hdr = hdr->next) {
  777. if(hdr->type == hf->type) return hdr;
  778. }
  779. return NULL;
  780. }