msg_parser.c 22 KB

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