msg_parser.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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 <sys/time.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 "../core_stats.h"
  65. #include "../globals.h"
  66. #include "parse_hname2.h"
  67. #include "parse_uri.h"
  68. #include "parse_content.h"
  69. #include "parse_to.h"
  70. #include "../compiler_opt.h"
  71. #ifdef DEBUG_DMALLOC
  72. #include <mem/dmalloc.h>
  73. #endif
  74. #define parse_hname(_b,_e,_h) parse_hname2((_b),(_e),(_h))
  75. /* number of via's encountered */
  76. int via_cnt;
  77. /* global request flags */
  78. unsigned int global_req_flags = 0;
  79. /* returns pointer to next header line, and fill hdr_f ;
  80. * if at end of header returns pointer to the last crlf (always buf)*/
  81. char* get_hdr_field(char* const buf, char* const end, struct hdr_field* const hdr)
  82. {
  83. char* tmp;
  84. char *match;
  85. struct via_body *vb;
  86. struct cseq_body* cseq_b;
  87. struct to_body* to_b;
  88. int integer, err;
  89. unsigned uval;
  90. if ((*buf)=='\n' || (*buf)=='\r'){
  91. /* double crlf or lflf or crcr */
  92. DBG("found end of header\n");
  93. hdr->type=HDR_EOH_T;
  94. return buf;
  95. }
  96. tmp=parse_hname(buf, end, hdr);
  97. if (hdr->type==HDR_ERROR_T){
  98. LOG(L_ERR, "ERROR: get_hdr_field: bad header\n");
  99. goto error;
  100. }
  101. /* eliminate leading whitespace */
  102. tmp=eat_lws_end(tmp, end);
  103. if (tmp>=end) {
  104. LOG(L_ERR, "ERROR: get_hdr_field: HF empty\n");
  105. goto error;
  106. }
  107. /* if header-field well-known, parse it, find its end otherwise ;
  108. * after leaving the hdr->type switch, tmp should be set to the
  109. * next header field
  110. */
  111. switch(hdr->type){
  112. case HDR_VIA_T:
  113. /* keep number of vias parsed -- we want to report it in
  114. replies for diagnostic purposes */
  115. via_cnt++;
  116. vb=pkg_malloc(sizeof(struct via_body));
  117. if (vb==0){
  118. LOG(L_ERR, "get_hdr_field: out of memory\n");
  119. goto error;
  120. }
  121. memset(vb,0,sizeof(struct via_body));
  122. hdr->body.s=tmp;
  123. tmp=parse_via(tmp, end, vb);
  124. if (vb->error==PARSE_ERROR){
  125. LOG(L_ERR, "ERROR: get_hdr_field: bad via\n");
  126. free_via_list(vb);
  127. goto error;
  128. }
  129. hdr->parsed=vb;
  130. vb->hdr.s=hdr->name.s;
  131. vb->hdr.len=hdr->name.len;
  132. hdr->body.len=tmp-hdr->body.s;
  133. break;
  134. case HDR_CSEQ_T:
  135. cseq_b=pkg_malloc(sizeof(struct cseq_body));
  136. if (cseq_b==0){
  137. LOG(L_ERR, "get_hdr_field: out of memory\n");
  138. goto error;
  139. }
  140. memset(cseq_b, 0, sizeof(struct cseq_body));
  141. hdr->body.s=tmp;
  142. tmp=parse_cseq(tmp, end, cseq_b);
  143. if (cseq_b->error==PARSE_ERROR){
  144. LOG(L_ERR, "ERROR: get_hdr_field: bad cseq\n");
  145. free_cseq(cseq_b);
  146. goto error;
  147. }
  148. hdr->parsed=cseq_b;
  149. hdr->body.len=tmp-hdr->body.s;
  150. DBG("get_hdr_field: cseq <%.*s>: <%.*s> <%.*s>\n",
  151. hdr->name.len, ZSW(hdr->name.s),
  152. cseq_b->number.len, ZSW(cseq_b->number.s),
  153. cseq_b->method.len, cseq_b->method.s);
  154. break;
  155. case HDR_TO_T:
  156. to_b=pkg_malloc(sizeof(struct to_body));
  157. if (to_b==0){
  158. LOG(L_ERR, "get_hdr_field: out of memory\n");
  159. goto error;
  160. }
  161. memset(to_b, 0, sizeof(struct to_body));
  162. hdr->body.s=tmp;
  163. tmp=parse_to(tmp, end,to_b);
  164. if (to_b->error==PARSE_ERROR){
  165. LOG(L_ERR, "ERROR: get_hdr_field: bad to header\n");
  166. free_to(to_b);
  167. goto error;
  168. }
  169. hdr->parsed=to_b;
  170. hdr->body.len=tmp-hdr->body.s;
  171. DBG("DEBUG: get_hdr_field: <%.*s> [%d]; uri=[%.*s] \n",
  172. hdr->name.len, ZSW(hdr->name.s),
  173. hdr->body.len, to_b->uri.len,ZSW(to_b->uri.s));
  174. DBG("DEBUG: to body [%.*s]\n",to_b->body.len,
  175. ZSW(to_b->body.s));
  176. break;
  177. case HDR_CONTENTLENGTH_T:
  178. hdr->body.s=tmp;
  179. tmp=parse_content_length(tmp,end, &integer);
  180. if (tmp==0){
  181. LOG(L_ERR, "ERROR:get_hdr_field: bad content_length header\n");
  182. goto error;
  183. }
  184. hdr->parsed=(void*)(long)integer;
  185. hdr->body.len=tmp-hdr->body.s;
  186. DBG("DEBUG: get_hdr_body : content_length=%d\n",
  187. (int)(long)hdr->parsed);
  188. break;
  189. case HDR_RETRY_AFTER_T:
  190. hdr->body.s=tmp;
  191. tmp=parse_retry_after(tmp,end, &uval, &err);
  192. if (err){
  193. LOG(L_ERR, "ERROR:get_hdr_field: bad retry_after header\n");
  194. goto error;
  195. }
  196. hdr->parsed=(void*)(unsigned long)uval;
  197. hdr->body.len=tmp-hdr->body.s;
  198. DBG("DEBUG: get_hdr_body : retry_after=%d\n",
  199. (unsigned)(long)hdr->parsed);
  200. break;
  201. case HDR_IDENTITY_T:
  202. case HDR_DATE_T:
  203. case HDR_IDENTITY_INFO_T:
  204. case HDR_SUPPORTED_T:
  205. case HDR_REQUIRE_T:
  206. case HDR_CONTENTTYPE_T:
  207. case HDR_FROM_T:
  208. case HDR_CALLID_T:
  209. case HDR_CONTACT_T:
  210. case HDR_ROUTE_T:
  211. case HDR_RECORDROUTE_T:
  212. case HDR_MAXFORWARDS_T:
  213. case HDR_AUTHORIZATION_T:
  214. case HDR_EXPIRES_T:
  215. case HDR_PROXYAUTH_T:
  216. case HDR_PROXYREQUIRE_T:
  217. case HDR_UNSUPPORTED_T:
  218. case HDR_ALLOW_T:
  219. case HDR_EVENT_T:
  220. case HDR_ACCEPT_T:
  221. case HDR_ACCEPTLANGUAGE_T:
  222. case HDR_ORGANIZATION_T:
  223. case HDR_PRIORITY_T:
  224. case HDR_SUBJECT_T:
  225. case HDR_USERAGENT_T:
  226. case HDR_SERVER_T:
  227. case HDR_CONTENTDISPOSITION_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* const msg, const hdr_flags_t flags, const 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_DIVERSION_T:
  464. if (msg->diversion==0) msg->diversion = hf;
  465. msg->parsed_flag|=HDR_DIVERSION_F;
  466. break;
  467. case HDR_RPID_T:
  468. if (msg->rpid==0) msg->rpid = hf;
  469. msg->parsed_flag|=HDR_RPID_F;
  470. break;
  471. case HDR_REFER_TO_T:
  472. if (msg->refer_to==0) msg->refer_to = hf;
  473. msg->parsed_flag|=HDR_REFER_TO_F;
  474. break;
  475. case HDR_SESSIONEXPIRES_T:
  476. if (msg->session_expires==0) msg->session_expires = hf;
  477. msg->parsed_flag|=HDR_SESSIONEXPIRES_F;
  478. break;
  479. case HDR_MIN_SE_T:
  480. if (msg->min_se==0) msg->min_se = hf;
  481. msg->parsed_flag|=HDR_MIN_SE_F;
  482. break;
  483. case HDR_SUBSCRIPTION_STATE_T:
  484. if (msg->subscription_state==0) msg->subscription_state = hf;
  485. msg->parsed_flag|=HDR_SUBSCRIPTION_STATE_F;
  486. break;
  487. case HDR_VIA_T:
  488. msg->parsed_flag|=HDR_VIA_F;
  489. DBG("parse_headers: Via found, flags=%llx\n",
  490. (unsigned long long)flags);
  491. if (msg->via1==0) {
  492. DBG("parse_headers: this is the first via\n");
  493. msg->h_via1=hf;
  494. msg->via1=hf->parsed;
  495. if (msg->via1->next){
  496. msg->via2=msg->via1->next;
  497. msg->parsed_flag|=HDR_VIA2_F;
  498. }
  499. }else if (msg->via2==0){
  500. msg->h_via2=hf;
  501. msg->via2=hf->parsed;
  502. msg->parsed_flag|=HDR_VIA2_F;
  503. DBG("parse_headers: this is the second via\n");
  504. }
  505. break;
  506. case HDR_DATE_T:
  507. if (msg->date==0) msg->date=hf;
  508. msg->parsed_flag|=HDR_DATE_F;
  509. break;
  510. case HDR_IDENTITY_T:
  511. if (msg->identity==0) msg->identity=hf;
  512. msg->parsed_flag|=HDR_IDENTITY_F;
  513. break;
  514. case HDR_IDENTITY_INFO_T:
  515. if (msg->identity_info==0) msg->identity_info=hf;
  516. msg->parsed_flag|=HDR_IDENTITY_INFO_F;
  517. break;
  518. case HDR_PATH_T:
  519. if (msg->path==0) msg->path=hf;
  520. msg->parsed_flag|=HDR_PATH_F;
  521. break;
  522. case HDR_PRIVACY_T:
  523. if (msg->privacy==0) msg->privacy=hf;
  524. msg->parsed_flag|=HDR_PRIVACY_F;
  525. break;
  526. case HDR_PAI_T:
  527. if (msg->pai==0) msg->pai=hf;
  528. msg->parsed_flag|=HDR_PAI_F;
  529. break;
  530. case HDR_PPI_T:
  531. if (msg->ppi==0) msg->ppi=hf;
  532. msg->parsed_flag|=HDR_PPI_F;
  533. break;
  534. case HDR_REASON_T:
  535. msg->parsed_flag|=HDR_REASON_F;
  536. break;
  537. default:
  538. LOG(L_CRIT, "BUG: parse_headers: unknown header type %d\n",
  539. hf->type);
  540. goto error;
  541. }
  542. /* add the header to the list*/
  543. if (msg->last_header==0){
  544. msg->headers=hf;
  545. msg->last_header=hf;
  546. }else{
  547. msg->last_header->next=hf;
  548. msg->last_header=hf;
  549. }
  550. #ifdef EXTRA_DEBUG
  551. DBG("header field type %d, name=<%.*s>, body=<%.*s>\n",
  552. hf->type,
  553. hf->name.len, ZSW(hf->name.s),
  554. hf->body.len, ZSW(hf->body.s));
  555. #endif
  556. tmp=rest;
  557. }
  558. skip:
  559. msg->unparsed=tmp;
  560. /* restore original flags */
  561. msg->parsed_flag |= orig_flag;
  562. return 0;
  563. error:
  564. ser_error=E_BAD_REQ;
  565. if (hf) pkg_free(hf);
  566. /* restore original flags */
  567. msg->parsed_flag |= orig_flag;
  568. return -1;
  569. }
  570. /* returns 0 if ok, -1 for errors */
  571. int parse_msg(char* const buf, const unsigned int len, struct sip_msg* const msg)
  572. {
  573. char *tmp;
  574. char* rest;
  575. struct msg_start *fl;
  576. int offset;
  577. hdr_flags_t flags;
  578. /* eat crlf from the beginning */
  579. for (tmp=buf; (*tmp=='\n' || *tmp=='\r')&&
  580. tmp-buf < len ; tmp++);
  581. offset=tmp-buf;
  582. fl=&(msg->first_line);
  583. rest=parse_first_line(tmp, len-offset, fl);
  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. if (parse_headers(msg, flags, 0)==-1) goto error;
  620. #ifdef EXTRA_DEBUG
  621. /* dump parsed data */
  622. if (msg->via1){
  623. DBG("first via: <%.*s/%.*s/%.*s> <%.*s:%.*s(%d)>",
  624. msg->via1->name.len,
  625. ZSW(msg->via1->name.s),
  626. msg->via1->version.len,
  627. ZSW(msg->via1->version.s),
  628. msg->via1->transport.len,
  629. ZSW(msg->via1->transport.s),
  630. msg->via1->host.len,
  631. ZSW(msg->via1->host.s),
  632. msg->via1->port_str.len,
  633. ZSW(msg->via1->port_str.s),
  634. msg->via1->port);
  635. if (msg->via1->params.s) DBG(";<%.*s>",
  636. msg->via1->params.len, ZSW(msg->via1->params.s));
  637. if (msg->via1->comment.s)
  638. DBG(" <%.*s>",
  639. msg->via1->comment.len, ZSW(msg->via1->comment.s));
  640. DBG ("\n");
  641. }
  642. if (msg->via2){
  643. DBG("second via: <%.*s/%.*s/%.*s> <%.*s:%.*s(%d)>",
  644. msg->via2->name.len,
  645. ZSW(msg->via2->name.s),
  646. msg->via2->version.len,
  647. ZSW(msg->via2->version.s),
  648. msg->via2->transport.len,
  649. ZSW(msg->via2->transport.s),
  650. msg->via2->host.len,
  651. ZSW(msg->via2->host.s),
  652. msg->via2->port_str.len,
  653. ZSW(msg->via2->port_str.s),
  654. msg->via2->port);
  655. if (msg->via2->params.s) DBG(";<%.*s>",
  656. msg->via2->params.len, ZSW(msg->via2->params.s));
  657. if (msg->via2->comment.s) DBG(" <%.*s>",
  658. msg->via2->comment.len, ZSW(msg->via2->comment.s));
  659. DBG ("\n");
  660. }
  661. #endif
  662. #ifdef EXTRA_DEBUG
  663. DBG("exiting parse_msg\n");
  664. #endif
  665. return 0;
  666. error:
  667. /* more debugging, msg->orig is/should be null terminated*/
  668. LOG(cfg_get(core, core_cfg, corelog), "ERROR: parse_msg: message=<%.*s>\n",
  669. (int)msg->len, ZSW(msg->buf));
  670. return -1;
  671. }
  672. void free_reply_lump( struct lump_rpl *lump)
  673. {
  674. struct lump_rpl *foo, *bar;
  675. for(foo=lump;foo;)
  676. {
  677. bar=foo->next;
  678. free_lump_rpl(foo);
  679. foo = bar;
  680. }
  681. }
  682. /*only the content*/
  683. void free_sip_msg(struct sip_msg* const msg)
  684. {
  685. if (msg->new_uri.s) { pkg_free(msg->new_uri.s); msg->new_uri.len=0; }
  686. if (msg->dst_uri.s) { pkg_free(msg->dst_uri.s); msg->dst_uri.len=0; }
  687. if (msg->path_vec.s) { pkg_free(msg->path_vec.s); msg->path_vec.len=0; }
  688. if (msg->headers) free_hdr_field_lst(msg->headers);
  689. if (msg->body && msg->body->free) msg->body->free(&msg->body);
  690. if (msg->add_rm) free_lump_list(msg->add_rm);
  691. if (msg->body_lumps) free_lump_list(msg->body_lumps);
  692. if (msg->reply_lump) free_reply_lump(msg->reply_lump);
  693. /* don't free anymore -- now a pointer to a static buffer */
  694. # ifdef DYN_BUF
  695. pkg_free(msg->buf);
  696. # endif
  697. }
  698. /*
  699. * Make a private copy of the string and assign it to dst_uri
  700. */
  701. int set_dst_uri(struct sip_msg* const msg, const str* const uri)
  702. {
  703. char* ptr;
  704. if (unlikely(!msg || !uri)) {
  705. LOG(L_ERR, "set_dst_uri: Invalid parameter value\n");
  706. return -1;
  707. }
  708. if (unlikely(uri->len == 0)) {
  709. reset_dst_uri(msg);
  710. }else 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* const 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 (unlikely(!msg || !path)) {
  738. LM_ERR("invalid parameter value\n");
  739. return -1;
  740. }
  741. if (unlikely(path->len == 0)) {
  742. reset_path_vector(msg);
  743. } else if (msg->path_vec.s && (msg->path_vec.len >= path->len)) {
  744. memcpy(msg->path_vec.s, path->s, path->len);
  745. msg->path_vec.len = path->len;
  746. } else {
  747. ptr = (char*)pkg_malloc(path->len);
  748. if (!ptr) {
  749. LM_ERR("not enough pkg memory\n");
  750. return -1;
  751. }
  752. memcpy(ptr, path->s, path->len);
  753. if (msg->path_vec.s) pkg_free(msg->path_vec.s);
  754. msg->path_vec.s = ptr;
  755. msg->path_vec.len = path->len;
  756. }
  757. return 0;
  758. }
  759. void reset_path_vector(struct sip_msg* msg)
  760. {
  761. if(msg->path_vec.s != 0) {
  762. pkg_free(msg->path_vec.s);
  763. }
  764. msg->path_vec.s = 0;
  765. msg->path_vec.len = 0;
  766. }
  767. hdr_field_t* get_hdr(sip_msg_t *msg, enum _hdr_types_t ht)
  768. {
  769. hdr_field_t *hdr;
  770. if (msg->parsed_flag & HDR_T2F(ht))
  771. for(hdr = msg->headers; hdr; hdr = hdr->next) {
  772. if(hdr->type == ht) return hdr;
  773. }
  774. return NULL;
  775. }
  776. hdr_field_t* next_sibling_hdr(hdr_field_t *hf)
  777. {
  778. hdr_field_t *hdr;
  779. for(hdr = hf->next; hdr; hdr = hdr->next) {
  780. if(hdr->type == hf->type) return hdr;
  781. }
  782. return NULL;
  783. }
  784. hdr_field_t* get_hdr_by_name(sip_msg_t *msg, char *name, int name_len)
  785. {
  786. hdr_field_t *hdr;
  787. for(hdr = msg->headers; hdr; hdr = hdr->next) {
  788. if(hdr->name.len == name_len && *hdr->name.s==*name
  789. && strncmp(hdr->name.s, name, name_len)==0)
  790. return hdr;
  791. }
  792. return NULL;
  793. }
  794. hdr_field_t* next_sibling_hdr_by_name(hdr_field_t *hf)
  795. {
  796. hdr_field_t *hdr;
  797. for(hdr = hf->next; hdr; hdr = hdr->next) {
  798. if(hdr->name.len == hf->name.len && *hdr->name.s==*hf->name.s
  799. && strncmp(hdr->name.s, hf->name.s, hf->name.len)==0)
  800. return hdr;
  801. }
  802. return NULL;
  803. }
  804. /**
  805. * set msg context id
  806. * - return: -1 on error; 0 - on set
  807. */
  808. int msg_ctx_id_set(const sip_msg_t* const msg, msg_ctx_id_t* const mid)
  809. {
  810. if(msg==NULL || mid==NULL)
  811. return -1;
  812. mid->msgid = msg->id;
  813. mid->pid = msg->pid;
  814. return 0;
  815. }
  816. /**
  817. * check msg context id
  818. * - return: -1 on error; 0 - on no match; 1 - on match
  819. */
  820. int msg_ctx_id_match(const sip_msg_t* const msg, const msg_ctx_id_t* const mid)
  821. {
  822. if(msg==NULL || mid==NULL)
  823. return -1;
  824. if(msg->id != mid->msgid || msg->pid!=mid->pid)
  825. return 0;
  826. return 1;
  827. }
  828. /**
  829. * set msg time value
  830. */
  831. int msg_set_time(sip_msg_t* const msg)
  832. {
  833. if(unlikely(msg==NULL))
  834. return -2;
  835. if(msg->tval.tv_sec!=0)
  836. return 0;
  837. return gettimeofday(&msg->tval, NULL);
  838. }