msg_parser.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. /*
  2. * sip msg. header proxy parser
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. *
  27. * History:
  28. * ---------
  29. * 2003-02-28 scratchpad compatibility abandoned (jiri)
  30. * 2003-01-29 scrathcpad removed (jiri)
  31. * 2003-01-27 next baby-step to removing ZT - PRESERVE_ZT (jiri)
  32. * 2003-03-31 removed msg->repl_add_rm (andrei)
  33. * 2003-04-26 ZSW (jiri)
  34. * 2003-05-01 parser extended to support Accept header field (janakj)
  35. * 2005-02-23 parse_headers uses hdr_flags_t now (andrei)
  36. * 2005-03-02 free_via_list(vb) on via parse error (andrei)
  37. * 2007-01-26 parser extended to support Identity, Identity-info and Date
  38. * header fields (gergo)
  39. */
  40. /** Parser :: SIP Message header proxy parser.
  41. * @file
  42. * @ingroup parser
  43. */
  44. /*! \defgroup parser SIP-router SIP message parser
  45. *
  46. * The SIP message parser
  47. *
  48. */
  49. #include <string.h>
  50. #include <stdlib.h>
  51. #include <sys/time.h>
  52. #include "../comp_defs.h"
  53. #include "msg_parser.h"
  54. #include "parser_f.h"
  55. #include "../ut.h"
  56. #include "../error.h"
  57. #include "../dprint.h"
  58. #include "../data_lump_rpl.h"
  59. #include "../mem/mem.h"
  60. #include "../error.h"
  61. #include "../core_stats.h"
  62. #include "../globals.h"
  63. #include "parse_hname2.h"
  64. #include "parse_uri.h"
  65. #include "parse_content.h"
  66. #include "parse_to.h"
  67. #include "../compiler_opt.h"
  68. #ifdef DEBUG_DMALLOC
  69. #include <mem/dmalloc.h>
  70. #endif
  71. #define parse_hname(_b,_e,_h) parse_hname2((_b),(_e),(_h))
  72. /* number of via's encountered */
  73. int via_cnt;
  74. /* global request flags */
  75. unsigned int global_req_flags = 0;
  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* const buf, char* const end, struct hdr_field* const hdr)
  79. {
  80. char *tmp = 0;
  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) {
  88. DBG("null buffer pointer\n");
  89. goto error;
  90. }
  91. if ((*buf)=='\n' || (*buf)=='\r'){
  92. /* double crlf or lflf or crcr */
  93. DBG("found end of header\n");
  94. hdr->type=HDR_EOH_T;
  95. return buf;
  96. }
  97. tmp=parse_hname(buf, end, hdr);
  98. if (hdr->type==HDR_ERROR_T){
  99. LOG(L_ERR, "ERROR: get_hdr_field: bad header\n");
  100. goto error;
  101. }
  102. /* eliminate leading whitespace */
  103. tmp=eat_lws_end(tmp, end);
  104. if (tmp>=end) {
  105. LOG(L_ERR, "ERROR: get_hdr_field: HF empty\n");
  106. goto error;
  107. }
  108. /* if header-field well-known, parse it, find its end otherwise ;
  109. * after leaving the hdr->type switch, tmp should be set to the
  110. * next header field
  111. */
  112. switch(hdr->type){
  113. case HDR_VIA_T:
  114. /* keep number of vias parsed -- we want to report it in
  115. replies for diagnostic purposes */
  116. via_cnt++;
  117. vb=pkg_malloc(sizeof(struct via_body));
  118. if (vb==0){
  119. LOG(L_ERR, "get_hdr_field: out of memory\n");
  120. goto error;
  121. }
  122. memset(vb,0,sizeof(struct via_body));
  123. hdr->body.s=tmp;
  124. tmp=parse_via(tmp, end, vb);
  125. if (vb->error==PARSE_ERROR){
  126. LOG(L_ERR, "ERROR: get_hdr_field: bad via\n");
  127. free_via_list(vb);
  128. goto error;
  129. }
  130. hdr->parsed=vb;
  131. vb->hdr.s=hdr->name.s;
  132. vb->hdr.len=hdr->name.len;
  133. hdr->body.len=tmp-hdr->body.s;
  134. break;
  135. case HDR_CSEQ_T:
  136. cseq_b=pkg_malloc(sizeof(struct cseq_body));
  137. if (cseq_b==0){
  138. LOG(L_ERR, "get_hdr_field: out of memory\n");
  139. goto error;
  140. }
  141. memset(cseq_b, 0, sizeof(struct cseq_body));
  142. hdr->body.s=tmp;
  143. tmp=parse_cseq(tmp, end, cseq_b);
  144. if (cseq_b->error==PARSE_ERROR){
  145. LOG(L_ERR, "ERROR: get_hdr_field: bad cseq\n");
  146. free_cseq(cseq_b);
  147. goto error;
  148. }
  149. hdr->parsed=cseq_b;
  150. hdr->body.len=tmp-hdr->body.s;
  151. DBG("get_hdr_field: cseq <%.*s>: <%.*s> <%.*s>\n",
  152. hdr->name.len, ZSW(hdr->name.s),
  153. cseq_b->number.len, ZSW(cseq_b->number.s),
  154. cseq_b->method.len, cseq_b->method.s);
  155. break;
  156. case HDR_TO_T:
  157. to_b=pkg_malloc(sizeof(struct to_body));
  158. if (to_b==0){
  159. LOG(L_ERR, "get_hdr_field: out of memory\n");
  160. goto error;
  161. }
  162. memset(to_b, 0, sizeof(struct to_body));
  163. hdr->body.s=tmp;
  164. tmp=parse_to(tmp, end,to_b);
  165. if (to_b->error==PARSE_ERROR){
  166. LOG(L_ERR, "ERROR: get_hdr_field: bad to header\n");
  167. free_to(to_b);
  168. goto error;
  169. }
  170. hdr->parsed=to_b;
  171. hdr->body.len=tmp-hdr->body.s;
  172. DBG("DEBUG: get_hdr_field: <%.*s> [%d]; uri=[%.*s] \n",
  173. hdr->name.len, ZSW(hdr->name.s),
  174. hdr->body.len, to_b->uri.len,ZSW(to_b->uri.s));
  175. DBG("DEBUG: to body [%.*s]\n",to_b->body.len,
  176. ZSW(to_b->body.s));
  177. break;
  178. case HDR_CONTENTLENGTH_T:
  179. hdr->body.s=tmp;
  180. tmp=parse_content_length(tmp,end, &integer);
  181. if (tmp==0){
  182. LOG(L_ERR, "ERROR:get_hdr_field: bad content_length header\n");
  183. goto error;
  184. }
  185. hdr->parsed=(void*)(long)integer;
  186. hdr->body.len=tmp-hdr->body.s;
  187. DBG("DEBUG: get_hdr_body : content_length=%d\n",
  188. (int)(long)hdr->parsed);
  189. break;
  190. case HDR_RETRY_AFTER_T:
  191. hdr->body.s=tmp;
  192. tmp=parse_retry_after(tmp,end, &uval, &err);
  193. if (err){
  194. LOG(L_ERR, "ERROR:get_hdr_field: bad retry_after header\n");
  195. goto error;
  196. }
  197. hdr->parsed=(void*)(unsigned long)uval;
  198. hdr->body.len=tmp-hdr->body.s;
  199. DBG("DEBUG: get_hdr_body : retry_after=%d\n",
  200. (unsigned)(long)hdr->parsed);
  201. break;
  202. case HDR_IDENTITY_T:
  203. case HDR_DATE_T:
  204. case HDR_IDENTITY_INFO_T:
  205. case HDR_SUPPORTED_T:
  206. case HDR_REQUIRE_T:
  207. case HDR_CONTENTTYPE_T:
  208. case HDR_FROM_T:
  209. case HDR_CALLID_T:
  210. case HDR_CONTACT_T:
  211. case HDR_ROUTE_T:
  212. case HDR_RECORDROUTE_T:
  213. case HDR_MAXFORWARDS_T:
  214. case HDR_AUTHORIZATION_T:
  215. case HDR_EXPIRES_T:
  216. case HDR_PROXYAUTH_T:
  217. case HDR_PROXYREQUIRE_T:
  218. case HDR_UNSUPPORTED_T:
  219. case HDR_ALLOW_T:
  220. case HDR_EVENT_T:
  221. case HDR_ACCEPT_T:
  222. case HDR_ACCEPTLANGUAGE_T:
  223. case HDR_ORGANIZATION_T:
  224. case HDR_PRIORITY_T:
  225. case HDR_SUBJECT_T:
  226. case HDR_USERAGENT_T:
  227. case HDR_SERVER_T:
  228. case HDR_CONTENTDISPOSITION_T:
  229. case HDR_DIVERSION_T:
  230. case HDR_RPID_T:
  231. case HDR_SIPIFMATCH_T:
  232. case HDR_REFER_TO_T:
  233. case HDR_SESSIONEXPIRES_T:
  234. case HDR_MIN_SE_T:
  235. case HDR_SUBSCRIPTION_STATE_T:
  236. case HDR_ACCEPTCONTACT_T:
  237. case HDR_ALLOWEVENTS_T:
  238. case HDR_CONTENTENCODING_T:
  239. case HDR_REFERREDBY_T:
  240. case HDR_REJECTCONTACT_T:
  241. case HDR_REQUESTDISPOSITION_T:
  242. case HDR_WWW_AUTHENTICATE_T:
  243. case HDR_PROXY_AUTHENTICATE_T:
  244. case HDR_PATH_T:
  245. case HDR_PRIVACY_T:
  246. case HDR_PAI_T:
  247. case HDR_PPI_T:
  248. case HDR_REASON_T:
  249. case HDR_OTHER_T:
  250. /* just skip over it */
  251. hdr->body.s=tmp;
  252. /* find end of header */
  253. /* find lf */
  254. do{
  255. match=q_memchr(tmp, '\n', end-tmp);
  256. if (match){
  257. match++;
  258. }else {
  259. LOG(L_ERR,
  260. "ERROR: get_hdr_field: bad body for <%s>(%d)\n",
  261. hdr->name.s, hdr->type);
  262. /* abort(); */
  263. tmp=end;
  264. goto error;
  265. }
  266. tmp=match;
  267. }while( match<end &&( (*match==' ')||(*match=='\t') ) );
  268. tmp=match;
  269. hdr->body.len=match-hdr->body.s;
  270. break;
  271. default:
  272. LOG(L_CRIT, "BUG: get_hdr_field: unknown header type %d\n",
  273. hdr->type);
  274. goto error;
  275. }
  276. /* jku: if \r covered by current length, shrink it */
  277. trim_r( hdr->body );
  278. hdr->len=tmp-hdr->name.s;
  279. return tmp;
  280. error:
  281. DBG("get_hdr_field: error exit\n");
  282. STATS_BAD_MSG_HDR();
  283. hdr->type=HDR_ERROR_T;
  284. hdr->len=tmp-hdr->name.s;
  285. return tmp;
  286. }
  287. /* parse the headers and adds them to msg->headers and msg->to, from etc.
  288. * It stops when all the headers requested in flags were parsed, on error
  289. * (bad header) or end of headers
  290. * WARNING: parse_headers was changed to use hdr_flags_t (the flags are now
  291. * different from the header types). Don't call it with a header type
  292. * (HDR_xxx_T), only with header flags (HDR_xxx_F)!*/
  293. /* note: it continues where it previously stopped and goes ahead until
  294. end is encountered or desired HFs are found; if you call it twice
  295. for the same HF which is present only once, it will fail the second
  296. time; if you call it twice and the HF is found on second time too,
  297. it's not replaced in the well-known HF pointer but just added to
  298. header list; if you want to use a dumb convenience function which will
  299. give you the first occurrence of a header you are interested in,
  300. look at check_transaction_quadruple
  301. */
  302. int parse_headers(struct sip_msg* const msg, const hdr_flags_t flags, const int next)
  303. {
  304. struct hdr_field* hf;
  305. char* tmp;
  306. char* rest;
  307. char* end;
  308. hdr_flags_t orig_flag;
  309. end=msg->buf+msg->len;
  310. tmp=msg->unparsed;
  311. if (unlikely(next)) {
  312. orig_flag = msg->parsed_flag;
  313. msg->parsed_flag &= ~flags;
  314. }else
  315. orig_flag=0;
  316. #ifdef EXTRA_DEBUG
  317. DBG("parse_headers: flags=%llx\n", (unsigned long long)flags);
  318. #endif
  319. while( tmp<end && (flags & msg->parsed_flag) != flags){
  320. prefetch_loc_r(tmp+64, 1);
  321. hf=pkg_malloc(sizeof(struct hdr_field));
  322. if (unlikely(hf==0)){
  323. ser_error=E_OUT_OF_MEM;
  324. LOG(L_ERR, "ERROR:parse_headers: memory allocation error\n");
  325. goto error;
  326. }
  327. memset(hf,0, sizeof(struct hdr_field));
  328. hf->type=HDR_ERROR_T;
  329. rest=get_hdr_field(tmp, end, hf);
  330. switch (hf->type){
  331. case HDR_ERROR_T:
  332. LOG(L_INFO,"ERROR: bad header field [%.*s]\n",
  333. (end-tmp>20)?20:(int)(end-tmp), tmp);
  334. goto error;
  335. case HDR_EOH_T:
  336. msg->eoh=tmp; /* or rest?*/
  337. msg->parsed_flag|=HDR_EOH_F;
  338. pkg_free(hf);
  339. goto skip;
  340. case HDR_ACCEPTCONTACT_T:
  341. case HDR_ALLOWEVENTS_T:
  342. case HDR_CONTENTENCODING_T:
  343. case HDR_REFERREDBY_T:
  344. case HDR_REJECTCONTACT_T:
  345. case HDR_REQUESTDISPOSITION_T:
  346. case HDR_WWW_AUTHENTICATE_T:
  347. case HDR_PROXY_AUTHENTICATE_T:
  348. case HDR_RETRY_AFTER_T:
  349. case HDR_OTHER_T: /* mark the type as found/parsed*/
  350. msg->parsed_flag|=HDR_T2F(hf->type);
  351. break;
  352. case HDR_CALLID_T:
  353. if (msg->callid==0) msg->callid=hf;
  354. msg->parsed_flag|=HDR_CALLID_F;
  355. break;
  356. case HDR_SIPIFMATCH_T:
  357. if (msg->sipifmatch==0) msg->sipifmatch=hf;
  358. msg->parsed_flag|=HDR_SIPIFMATCH_F;
  359. break;
  360. case HDR_TO_T:
  361. if (msg->to==0) msg->to=hf;
  362. msg->parsed_flag|=HDR_TO_F;
  363. break;
  364. case HDR_CSEQ_T:
  365. if (msg->cseq==0) msg->cseq=hf;
  366. msg->parsed_flag|=HDR_CSEQ_F;
  367. break;
  368. case HDR_FROM_T:
  369. if (msg->from==0) msg->from=hf;
  370. msg->parsed_flag|=HDR_FROM_F;
  371. break;
  372. case HDR_CONTACT_T:
  373. if (msg->contact==0) msg->contact=hf;
  374. msg->parsed_flag|=HDR_CONTACT_F;
  375. break;
  376. case HDR_MAXFORWARDS_T:
  377. if(msg->maxforwards==0) msg->maxforwards=hf;
  378. msg->parsed_flag|=HDR_MAXFORWARDS_F;
  379. break;
  380. case HDR_ROUTE_T:
  381. if (msg->route==0) msg->route=hf;
  382. msg->parsed_flag|=HDR_ROUTE_F;
  383. break;
  384. case HDR_RECORDROUTE_T:
  385. if (msg->record_route==0) msg->record_route = hf;
  386. msg->parsed_flag|=HDR_RECORDROUTE_F;
  387. break;
  388. case HDR_CONTENTTYPE_T:
  389. if (msg->content_type==0) msg->content_type = hf;
  390. msg->parsed_flag|=HDR_CONTENTTYPE_F;
  391. break;
  392. case HDR_CONTENTLENGTH_T:
  393. if (msg->content_length==0) msg->content_length = hf;
  394. msg->parsed_flag|=HDR_CONTENTLENGTH_F;
  395. break;
  396. case HDR_AUTHORIZATION_T:
  397. if (msg->authorization==0) msg->authorization = hf;
  398. msg->parsed_flag|=HDR_AUTHORIZATION_F;
  399. break;
  400. case HDR_EXPIRES_T:
  401. if (msg->expires==0) msg->expires = hf;
  402. msg->parsed_flag|=HDR_EXPIRES_F;
  403. break;
  404. case HDR_PROXYAUTH_T:
  405. if (msg->proxy_auth==0) msg->proxy_auth = hf;
  406. msg->parsed_flag|=HDR_PROXYAUTH_F;
  407. break;
  408. case HDR_PROXYREQUIRE_T:
  409. if (msg->proxy_require==0) msg->proxy_require = hf;
  410. msg->parsed_flag|=HDR_PROXYREQUIRE_F;
  411. break;
  412. case HDR_SUPPORTED_T:
  413. if (msg->supported==0) msg->supported=hf;
  414. msg->parsed_flag|=HDR_SUPPORTED_F;
  415. break;
  416. case HDR_REQUIRE_T:
  417. if (msg->require==0) msg->require=hf;
  418. msg->parsed_flag|=HDR_REQUIRE_F;
  419. break;
  420. case HDR_UNSUPPORTED_T:
  421. if (msg->unsupported==0) msg->unsupported=hf;
  422. msg->parsed_flag|=HDR_UNSUPPORTED_F;
  423. break;
  424. case HDR_ALLOW_T:
  425. if (msg->allow==0) msg->allow = hf;
  426. msg->parsed_flag|=HDR_ALLOW_F;
  427. break;
  428. case HDR_EVENT_T:
  429. if (msg->event==0) msg->event = hf;
  430. msg->parsed_flag|=HDR_EVENT_F;
  431. break;
  432. case HDR_ACCEPT_T:
  433. if (msg->accept==0) msg->accept = hf;
  434. msg->parsed_flag|=HDR_ACCEPT_F;
  435. break;
  436. case HDR_ACCEPTLANGUAGE_T:
  437. if (msg->accept_language==0) msg->accept_language = hf;
  438. msg->parsed_flag|=HDR_ACCEPTLANGUAGE_F;
  439. break;
  440. case HDR_ORGANIZATION_T:
  441. if (msg->organization==0) msg->organization = hf;
  442. msg->parsed_flag|=HDR_ORGANIZATION_F;
  443. break;
  444. case HDR_PRIORITY_T:
  445. if (msg->priority==0) msg->priority = hf;
  446. msg->parsed_flag|=HDR_PRIORITY_F;
  447. break;
  448. case HDR_SUBJECT_T:
  449. if (msg->subject==0) msg->subject = hf;
  450. msg->parsed_flag|=HDR_SUBJECT_F;
  451. break;
  452. case HDR_USERAGENT_T:
  453. if (msg->user_agent==0) msg->user_agent = hf;
  454. msg->parsed_flag|=HDR_USERAGENT_F;
  455. break;
  456. case HDR_SERVER_T:
  457. if (msg->server==0) msg->server = hf;
  458. msg->parsed_flag|=HDR_SERVER_F;
  459. break;
  460. case HDR_CONTENTDISPOSITION_T:
  461. if (msg->content_disposition==0) msg->content_disposition = hf;
  462. msg->parsed_flag|=HDR_CONTENTDISPOSITION_F;
  463. break;
  464. case HDR_DIVERSION_T:
  465. if (msg->diversion==0) msg->diversion = hf;
  466. msg->parsed_flag|=HDR_DIVERSION_F;
  467. break;
  468. case HDR_RPID_T:
  469. if (msg->rpid==0) msg->rpid = hf;
  470. msg->parsed_flag|=HDR_RPID_F;
  471. break;
  472. case HDR_REFER_TO_T:
  473. if (msg->refer_to==0) msg->refer_to = hf;
  474. msg->parsed_flag|=HDR_REFER_TO_F;
  475. break;
  476. case HDR_SESSIONEXPIRES_T:
  477. if (msg->session_expires==0) msg->session_expires = hf;
  478. msg->parsed_flag|=HDR_SESSIONEXPIRES_F;
  479. break;
  480. case HDR_MIN_SE_T:
  481. if (msg->min_se==0) msg->min_se = hf;
  482. msg->parsed_flag|=HDR_MIN_SE_F;
  483. break;
  484. case HDR_SUBSCRIPTION_STATE_T:
  485. if (msg->subscription_state==0) msg->subscription_state = hf;
  486. msg->parsed_flag|=HDR_SUBSCRIPTION_STATE_F;
  487. break;
  488. case HDR_VIA_T:
  489. msg->parsed_flag|=HDR_VIA_F;
  490. DBG("parse_headers: Via found, flags=%llx\n",
  491. (unsigned long long)flags);
  492. if (msg->via1==0) {
  493. DBG("parse_headers: this is the first via\n");
  494. msg->h_via1=hf;
  495. msg->via1=hf->parsed;
  496. if (msg->via1->next){
  497. msg->via2=msg->via1->next;
  498. msg->parsed_flag|=HDR_VIA2_F;
  499. }
  500. }else if (msg->via2==0){
  501. msg->h_via2=hf;
  502. msg->via2=hf->parsed;
  503. msg->parsed_flag|=HDR_VIA2_F;
  504. DBG("parse_headers: this is the second via\n");
  505. }
  506. break;
  507. case HDR_DATE_T:
  508. if (msg->date==0) msg->date=hf;
  509. msg->parsed_flag|=HDR_DATE_F;
  510. break;
  511. case HDR_IDENTITY_T:
  512. if (msg->identity==0) msg->identity=hf;
  513. msg->parsed_flag|=HDR_IDENTITY_F;
  514. break;
  515. case HDR_IDENTITY_INFO_T:
  516. if (msg->identity_info==0) msg->identity_info=hf;
  517. msg->parsed_flag|=HDR_IDENTITY_INFO_F;
  518. break;
  519. case HDR_PATH_T:
  520. if (msg->path==0) msg->path=hf;
  521. msg->parsed_flag|=HDR_PATH_F;
  522. break;
  523. case HDR_PRIVACY_T:
  524. if (msg->privacy==0) msg->privacy=hf;
  525. msg->parsed_flag|=HDR_PRIVACY_F;
  526. break;
  527. case HDR_PAI_T:
  528. if (msg->pai==0) msg->pai=hf;
  529. msg->parsed_flag|=HDR_PAI_F;
  530. break;
  531. case HDR_PPI_T:
  532. if (msg->ppi==0) msg->ppi=hf;
  533. msg->parsed_flag|=HDR_PPI_F;
  534. break;
  535. case HDR_REASON_T:
  536. msg->parsed_flag|=HDR_REASON_F;
  537. break;
  538. default:
  539. LOG(L_CRIT, "BUG: parse_headers: unknown header type %d\n",
  540. hf->type);
  541. goto error;
  542. }
  543. /* add the header to the list*/
  544. if (msg->last_header==0){
  545. msg->headers=hf;
  546. msg->last_header=hf;
  547. }else{
  548. msg->last_header->next=hf;
  549. msg->last_header=hf;
  550. }
  551. #ifdef EXTRA_DEBUG
  552. DBG("header field type %d, name=<%.*s>, body=<%.*s>\n",
  553. hf->type,
  554. hf->name.len, ZSW(hf->name.s),
  555. hf->body.len, ZSW(hf->body.s));
  556. #endif
  557. tmp=rest;
  558. }
  559. skip:
  560. msg->unparsed=tmp;
  561. /* restore original flags */
  562. msg->parsed_flag |= orig_flag;
  563. return 0;
  564. error:
  565. ser_error=E_BAD_REQ;
  566. if (hf) pkg_free(hf);
  567. /* restore original flags */
  568. msg->parsed_flag |= orig_flag;
  569. return -1;
  570. }
  571. /* returns 0 if ok, -1 for errors */
  572. int parse_msg(char* const buf, const unsigned int len, struct sip_msg* const msg)
  573. {
  574. char *tmp;
  575. char* rest;
  576. struct msg_start *fl;
  577. int offset;
  578. hdr_flags_t flags;
  579. /* eat crlf from the beginning */
  580. for (tmp=buf; (*tmp=='\n' || *tmp=='\r')&&
  581. tmp-buf < len ; tmp++);
  582. offset=tmp-buf;
  583. fl=&(msg->first_line);
  584. rest=parse_first_line(tmp, len-offset, fl);
  585. offset+=rest-tmp;
  586. tmp=rest;
  587. switch(fl->type){
  588. case SIP_INVALID:
  589. DBG("parse_msg: invalid message\n");
  590. goto error;
  591. break;
  592. case SIP_REQUEST:
  593. DBG("SIP Request:\n");
  594. DBG(" method: <%.*s>\n",fl->u.request.method.len,
  595. ZSW(fl->u.request.method.s));
  596. DBG(" uri: <%.*s>\n",fl->u.request.uri.len,
  597. ZSW(fl->u.request.uri.s));
  598. DBG(" version: <%.*s>\n",fl->u.request.version.len,
  599. ZSW(fl->u.request.version.s));
  600. flags=HDR_VIA_F;
  601. break;
  602. case SIP_REPLY:
  603. DBG("SIP Reply (status):\n");
  604. DBG(" version: <%.*s>\n",fl->u.reply.version.len,
  605. ZSW(fl->u.reply.version.s));
  606. DBG(" status: <%.*s>\n", fl->u.reply.status.len,
  607. ZSW(fl->u.reply.status.s));
  608. DBG(" reason: <%.*s>\n", fl->u.reply.reason.len,
  609. ZSW(fl->u.reply.reason.s));
  610. /* flags=HDR_VIA | HDR_VIA2; */
  611. /* we don't try to parse VIA2 for local messages; -Jiri */
  612. flags=HDR_VIA_F;
  613. break;
  614. default:
  615. DBG("unknown type %d\n",fl->type);
  616. goto error;
  617. }
  618. msg->unparsed=tmp;
  619. /*find first Via: */
  620. if (parse_headers(msg, flags, 0)==-1) goto error;
  621. #ifdef EXTRA_DEBUG
  622. /* dump parsed data */
  623. if (msg->via1){
  624. DBG("first via: <%.*s/%.*s/%.*s> <%.*s:%.*s(%d)>",
  625. msg->via1->name.len,
  626. ZSW(msg->via1->name.s),
  627. msg->via1->version.len,
  628. ZSW(msg->via1->version.s),
  629. msg->via1->transport.len,
  630. ZSW(msg->via1->transport.s),
  631. msg->via1->host.len,
  632. ZSW(msg->via1->host.s),
  633. msg->via1->port_str.len,
  634. ZSW(msg->via1->port_str.s),
  635. msg->via1->port);
  636. if (msg->via1->params.s) DBG(";<%.*s>",
  637. msg->via1->params.len, ZSW(msg->via1->params.s));
  638. if (msg->via1->comment.s)
  639. DBG(" <%.*s>",
  640. msg->via1->comment.len, ZSW(msg->via1->comment.s));
  641. DBG ("\n");
  642. }
  643. if (msg->via2){
  644. DBG("second via: <%.*s/%.*s/%.*s> <%.*s:%.*s(%d)>",
  645. msg->via2->name.len,
  646. ZSW(msg->via2->name.s),
  647. msg->via2->version.len,
  648. ZSW(msg->via2->version.s),
  649. msg->via2->transport.len,
  650. ZSW(msg->via2->transport.s),
  651. msg->via2->host.len,
  652. ZSW(msg->via2->host.s),
  653. msg->via2->port_str.len,
  654. ZSW(msg->via2->port_str.s),
  655. msg->via2->port);
  656. if (msg->via2->params.s) DBG(";<%.*s>",
  657. msg->via2->params.len, ZSW(msg->via2->params.s));
  658. if (msg->via2->comment.s) DBG(" <%.*s>",
  659. msg->via2->comment.len, ZSW(msg->via2->comment.s));
  660. DBG ("\n");
  661. }
  662. #endif
  663. #ifdef EXTRA_DEBUG
  664. DBG("exiting parse_msg\n");
  665. #endif
  666. return 0;
  667. error:
  668. /* more debugging, msg->orig is/should be null terminated*/
  669. LOG(cfg_get(core, core_cfg, corelog), "ERROR: parse_msg: message=<%.*s>\n",
  670. (int)msg->len, ZSW(msg->buf));
  671. return -1;
  672. }
  673. void free_reply_lump( struct lump_rpl *lump)
  674. {
  675. struct lump_rpl *foo, *bar;
  676. for(foo=lump;foo;)
  677. {
  678. bar=foo->next;
  679. free_lump_rpl(foo);
  680. foo = bar;
  681. }
  682. }
  683. /*only the content*/
  684. void free_sip_msg(struct sip_msg* const msg)
  685. {
  686. reset_new_uri(msg);
  687. reset_dst_uri(msg);
  688. reset_path_vector(msg);
  689. reset_instance(msg);
  690. reset_ruid(msg);
  691. reset_ua(msg);
  692. if (msg->headers) free_hdr_field_lst(msg->headers);
  693. if (msg->body && msg->body->free) msg->body->free(&msg->body);
  694. if (msg->add_rm) free_lump_list(msg->add_rm);
  695. if (msg->body_lumps) free_lump_list(msg->body_lumps);
  696. if (msg->reply_lump) free_reply_lump(msg->reply_lump);
  697. msg_ldata_reset(msg);
  698. /* don't free anymore -- now a pointer to a static buffer */
  699. # ifdef DYN_BUF
  700. pkg_free(msg->buf);
  701. # endif
  702. }
  703. /**
  704. * reset new uri value
  705. */
  706. void reset_new_uri(struct sip_msg* const msg)
  707. {
  708. if(msg->new_uri.s != 0) {
  709. pkg_free(msg->new_uri.s);
  710. }
  711. msg->new_uri.s = 0;
  712. msg->new_uri.len = 0;
  713. msg->parsed_uri_ok = 0;
  714. }
  715. /*
  716. * Make a private copy of the string and assign it to dst_uri
  717. */
  718. int set_dst_uri(struct sip_msg* const msg, const str* const uri)
  719. {
  720. char* ptr;
  721. if (unlikely(!msg || !uri)) {
  722. LOG(L_ERR, "set_dst_uri: Invalid parameter value\n");
  723. return -1;
  724. }
  725. if (unlikely(uri->len == 0)) {
  726. reset_dst_uri(msg);
  727. }else if (msg->dst_uri.s && (msg->dst_uri.len >= uri->len)) {
  728. memcpy(msg->dst_uri.s, uri->s, uri->len);
  729. msg->dst_uri.len = uri->len;
  730. } else {
  731. ptr = (char*)pkg_malloc(uri->len);
  732. if (!ptr) {
  733. LOG(L_ERR, "set_dst_uri: Not enough memory\n");
  734. return -1;
  735. }
  736. memcpy(ptr, uri->s, uri->len);
  737. if (msg->dst_uri.s) pkg_free(msg->dst_uri.s);
  738. msg->dst_uri.s = ptr;
  739. msg->dst_uri.len = uri->len;
  740. }
  741. return 0;
  742. }
  743. void reset_dst_uri(struct sip_msg* const msg)
  744. {
  745. if(msg->dst_uri.s != 0) {
  746. pkg_free(msg->dst_uri.s);
  747. }
  748. msg->dst_uri.s = 0;
  749. msg->dst_uri.len = 0;
  750. }
  751. int set_path_vector(struct sip_msg* msg, str* path)
  752. {
  753. char* ptr;
  754. if (unlikely(!msg || !path)) {
  755. LM_ERR("invalid parameter value\n");
  756. return -1;
  757. }
  758. if (unlikely(path->len == 0)) {
  759. reset_path_vector(msg);
  760. } else if (msg->path_vec.s && (msg->path_vec.len >= path->len)) {
  761. memcpy(msg->path_vec.s, path->s, path->len);
  762. msg->path_vec.len = path->len;
  763. } else {
  764. ptr = (char*)pkg_malloc(path->len);
  765. if (!ptr) {
  766. LM_ERR("not enough pkg memory\n");
  767. return -1;
  768. }
  769. memcpy(ptr, path->s, path->len);
  770. if (msg->path_vec.s) pkg_free(msg->path_vec.s);
  771. msg->path_vec.s = ptr;
  772. msg->path_vec.len = path->len;
  773. }
  774. return 0;
  775. }
  776. void reset_path_vector(struct sip_msg* const msg)
  777. {
  778. if(msg->path_vec.s != 0) {
  779. pkg_free(msg->path_vec.s);
  780. }
  781. msg->path_vec.s = 0;
  782. msg->path_vec.len = 0;
  783. }
  784. int set_instance(struct sip_msg* msg, str* instance)
  785. {
  786. char* ptr;
  787. if (unlikely(!msg || !instance)) {
  788. LM_ERR("invalid instance parameter value\n");
  789. return -1;
  790. }
  791. if (unlikely(instance->len == 0)) {
  792. reset_instance(msg);
  793. } else if (msg->instance.s && (msg->instance.len >= instance->len)) {
  794. memcpy(msg->instance.s, instance->s, instance->len);
  795. msg->instance.len = instance->len;
  796. } else {
  797. ptr = (char*)pkg_malloc(instance->len);
  798. if (!ptr) {
  799. LM_ERR("not enough pkg memory for instance\n");
  800. return -1;
  801. }
  802. memcpy(ptr, instance->s, instance->len);
  803. if (msg->instance.s) pkg_free(msg->instance.s);
  804. msg->instance.s = ptr;
  805. msg->instance.len = instance->len;
  806. }
  807. return 0;
  808. }
  809. void reset_instance(struct sip_msg* const msg)
  810. {
  811. if(msg->instance.s != 0) {
  812. pkg_free(msg->instance.s);
  813. }
  814. msg->instance.s = 0;
  815. msg->instance.len = 0;
  816. }
  817. int set_ruid(struct sip_msg* msg, str* ruid)
  818. {
  819. char* ptr;
  820. if (unlikely(!msg || !ruid)) {
  821. LM_ERR("invalid ruid parameter value\n");
  822. return -1;
  823. }
  824. if (unlikely(ruid->len == 0)) {
  825. reset_ruid(msg);
  826. } else if (msg->ruid.s && (msg->ruid.len >= ruid->len)) {
  827. memcpy(msg->ruid.s, ruid->s, ruid->len);
  828. msg->ruid.len = ruid->len;
  829. } else {
  830. ptr = (char*)pkg_malloc(ruid->len);
  831. if (!ptr) {
  832. LM_ERR("not enough pkg memory for ruid\n");
  833. return -1;
  834. }
  835. memcpy(ptr, ruid->s, ruid->len);
  836. if (msg->ruid.s) pkg_free(msg->ruid.s);
  837. msg->ruid.s = ptr;
  838. msg->ruid.len = ruid->len;
  839. }
  840. return 0;
  841. }
  842. void reset_ruid(struct sip_msg* const msg)
  843. {
  844. if(msg->ruid.s != 0) {
  845. pkg_free(msg->ruid.s);
  846. }
  847. msg->ruid.s = 0;
  848. msg->ruid.len = 0;
  849. }
  850. int set_ua(struct sip_msg* msg, str* location_ua)
  851. {
  852. char* ptr;
  853. if (unlikely(!msg || !location_ua)) {
  854. LM_ERR("invalid location_ua parameter value\n");
  855. return -1;
  856. }
  857. if (unlikely(location_ua->len == 0)) {
  858. reset_ua(msg);
  859. } else if (msg->location_ua.s && (msg->location_ua.len >= location_ua->len)) {
  860. memcpy(msg->location_ua.s, location_ua->s, location_ua->len);
  861. msg->location_ua.len = location_ua->len;
  862. } else {
  863. ptr = (char*)pkg_malloc(location_ua->len);
  864. if (!ptr) {
  865. LM_ERR("not enough pkg memory for location_ua\n");
  866. return -1;
  867. }
  868. memcpy(ptr, location_ua->s, location_ua->len);
  869. if (msg->location_ua.s) pkg_free(msg->location_ua.s);
  870. msg->location_ua.s = ptr;
  871. msg->location_ua.len = location_ua->len;
  872. }
  873. return 0;
  874. }
  875. void reset_ua(struct sip_msg* const msg)
  876. {
  877. if(msg->location_ua.s != 0) {
  878. pkg_free(msg->location_ua.s);
  879. }
  880. msg->location_ua.s = 0;
  881. msg->location_ua.len = 0;
  882. }
  883. /**
  884. * reset content of msg->ldv (msg_ldata_t structure)
  885. */
  886. void msg_ldata_reset(sip_msg_t *msg)
  887. {
  888. if(msg==NULL)
  889. return;
  890. memset(&msg->ldv, 0, sizeof(msg_ldata_t));
  891. }
  892. hdr_field_t* get_hdr(const sip_msg_t* const msg, const enum _hdr_types_t ht)
  893. {
  894. hdr_field_t *hdr;
  895. if (msg->parsed_flag & HDR_T2F(ht))
  896. for(hdr = msg->headers; hdr; hdr = hdr->next) {
  897. if(hdr->type == ht) return hdr;
  898. }
  899. return NULL;
  900. }
  901. hdr_field_t* next_sibling_hdr(const hdr_field_t* const hf)
  902. {
  903. hdr_field_t *hdr;
  904. for(hdr = hf->next; hdr; hdr = hdr->next) {
  905. if(hdr->type == hf->type) return hdr;
  906. }
  907. return NULL;
  908. }
  909. hdr_field_t* get_hdr_by_name(const sip_msg_t* const msg, const char* const name, const int name_len)
  910. {
  911. hdr_field_t *hdr;
  912. for(hdr = msg->headers; hdr; hdr = hdr->next) {
  913. if(hdr->name.len == name_len && *hdr->name.s==*name
  914. && strncmp(hdr->name.s, name, name_len)==0)
  915. return hdr;
  916. }
  917. return NULL;
  918. }
  919. /** not used yet */
  920. hdr_field_t* next_sibling_hdr_by_name(const hdr_field_t* const hf)
  921. {
  922. hdr_field_t *hdr;
  923. for(hdr = hf->next; hdr; hdr = hdr->next) {
  924. if(hdr->name.len == hf->name.len && *hdr->name.s==*hf->name.s
  925. && strncmp(hdr->name.s, hf->name.s, hf->name.len)==0)
  926. return hdr;
  927. }
  928. return NULL;
  929. }
  930. /**
  931. * set msg context id
  932. * - return: -1 on error; 0 - on set
  933. */
  934. int msg_ctx_id_set(const sip_msg_t* const msg, msg_ctx_id_t* const mid)
  935. {
  936. if(msg==NULL || mid==NULL)
  937. return -1;
  938. mid->msgid = msg->id;
  939. mid->pid = msg->pid;
  940. return 0;
  941. }
  942. /**
  943. * check msg context id
  944. * - return: -1 on error; 0 - on no match; 1 - on match
  945. */
  946. int msg_ctx_id_match(const sip_msg_t* const msg, const msg_ctx_id_t* const mid)
  947. {
  948. if(msg==NULL || mid==NULL)
  949. return -1;
  950. if(msg->id != mid->msgid || msg->pid!=mid->pid)
  951. return 0;
  952. return 1;
  953. }
  954. /**
  955. * set msg time value
  956. */
  957. int msg_set_time(sip_msg_t* const msg)
  958. {
  959. if(unlikely(msg==NULL))
  960. return -2;
  961. if(msg->tval.tv_sec!=0)
  962. return 0;
  963. return gettimeofday(&msg->tval, NULL);
  964. }