msg_parser.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. /* only free path vector from pkg IFF it is still in pkg... - ie. if msg is shm we don't free... */
  779. if (!(msg->msg_flags&FL_SHM_CLONE)) {
  780. if (msg->path_vec.s)
  781. pkg_free(msg->path_vec.s);
  782. msg->path_vec.s = 0;
  783. msg->path_vec.len = 0;
  784. }
  785. }
  786. int set_instance(struct sip_msg* msg, str* instance)
  787. {
  788. char* ptr;
  789. if (unlikely(!msg || !instance)) {
  790. LM_ERR("invalid instance parameter value\n");
  791. return -1;
  792. }
  793. if (unlikely(instance->len == 0)) {
  794. reset_instance(msg);
  795. } else if (msg->instance.s && (msg->instance.len >= instance->len)) {
  796. memcpy(msg->instance.s, instance->s, instance->len);
  797. msg->instance.len = instance->len;
  798. } else {
  799. ptr = (char*)pkg_malloc(instance->len);
  800. if (!ptr) {
  801. LM_ERR("not enough pkg memory for instance\n");
  802. return -1;
  803. }
  804. memcpy(ptr, instance->s, instance->len);
  805. if (msg->instance.s) pkg_free(msg->instance.s);
  806. msg->instance.s = ptr;
  807. msg->instance.len = instance->len;
  808. }
  809. return 0;
  810. }
  811. void reset_instance(struct sip_msg* const msg)
  812. {
  813. if(msg->instance.s != 0) {
  814. pkg_free(msg->instance.s);
  815. }
  816. msg->instance.s = 0;
  817. msg->instance.len = 0;
  818. }
  819. int set_ruid(struct sip_msg* msg, str* ruid)
  820. {
  821. char* ptr;
  822. if (unlikely(!msg || !ruid)) {
  823. LM_ERR("invalid ruid parameter value\n");
  824. return -1;
  825. }
  826. if (unlikely(ruid->len == 0)) {
  827. reset_ruid(msg);
  828. } else if (msg->ruid.s && (msg->ruid.len >= ruid->len)) {
  829. memcpy(msg->ruid.s, ruid->s, ruid->len);
  830. msg->ruid.len = ruid->len;
  831. } else {
  832. ptr = (char*)pkg_malloc(ruid->len);
  833. if (!ptr) {
  834. LM_ERR("not enough pkg memory for ruid\n");
  835. return -1;
  836. }
  837. memcpy(ptr, ruid->s, ruid->len);
  838. if (msg->ruid.s) pkg_free(msg->ruid.s);
  839. msg->ruid.s = ptr;
  840. msg->ruid.len = ruid->len;
  841. }
  842. return 0;
  843. }
  844. void reset_ruid(struct sip_msg* const msg)
  845. {
  846. if(msg->ruid.s != 0) {
  847. pkg_free(msg->ruid.s);
  848. }
  849. msg->ruid.s = 0;
  850. msg->ruid.len = 0;
  851. }
  852. int set_ua(struct sip_msg* msg, str* location_ua)
  853. {
  854. char* ptr;
  855. if (unlikely(!msg || !location_ua)) {
  856. LM_ERR("invalid location_ua parameter value\n");
  857. return -1;
  858. }
  859. if (unlikely(location_ua->len == 0)) {
  860. reset_ua(msg);
  861. } else if (msg->location_ua.s && (msg->location_ua.len >= location_ua->len)) {
  862. memcpy(msg->location_ua.s, location_ua->s, location_ua->len);
  863. msg->location_ua.len = location_ua->len;
  864. } else {
  865. ptr = (char*)pkg_malloc(location_ua->len);
  866. if (!ptr) {
  867. LM_ERR("not enough pkg memory for location_ua\n");
  868. return -1;
  869. }
  870. memcpy(ptr, location_ua->s, location_ua->len);
  871. if (msg->location_ua.s) pkg_free(msg->location_ua.s);
  872. msg->location_ua.s = ptr;
  873. msg->location_ua.len = location_ua->len;
  874. }
  875. return 0;
  876. }
  877. void reset_ua(struct sip_msg* const msg)
  878. {
  879. if(msg->location_ua.s != 0) {
  880. pkg_free(msg->location_ua.s);
  881. }
  882. msg->location_ua.s = 0;
  883. msg->location_ua.len = 0;
  884. }
  885. /**
  886. * reset content of msg->ldv (msg_ldata_t structure)
  887. */
  888. void msg_ldata_reset(sip_msg_t *msg)
  889. {
  890. if(msg==NULL)
  891. return;
  892. memset(&msg->ldv, 0, sizeof(msg_ldata_t));
  893. }
  894. hdr_field_t* get_hdr(const sip_msg_t* const msg, const enum _hdr_types_t ht)
  895. {
  896. hdr_field_t *hdr;
  897. if (msg->parsed_flag & HDR_T2F(ht))
  898. for(hdr = msg->headers; hdr; hdr = hdr->next) {
  899. if(hdr->type == ht) return hdr;
  900. }
  901. return NULL;
  902. }
  903. hdr_field_t* next_sibling_hdr(const hdr_field_t* const hf)
  904. {
  905. hdr_field_t *hdr;
  906. for(hdr = hf->next; hdr; hdr = hdr->next) {
  907. if(hdr->type == hf->type) return hdr;
  908. }
  909. return NULL;
  910. }
  911. hdr_field_t* get_hdr_by_name(const sip_msg_t* const msg, const char* const name, const int name_len)
  912. {
  913. hdr_field_t *hdr;
  914. for(hdr = msg->headers; hdr; hdr = hdr->next) {
  915. if(hdr->name.len == name_len && *hdr->name.s==*name
  916. && strncmp(hdr->name.s, name, name_len)==0)
  917. return hdr;
  918. }
  919. return NULL;
  920. }
  921. /** not used yet */
  922. hdr_field_t* next_sibling_hdr_by_name(const hdr_field_t* const hf)
  923. {
  924. hdr_field_t *hdr;
  925. for(hdr = hf->next; hdr; hdr = hdr->next) {
  926. if(hdr->name.len == hf->name.len && *hdr->name.s==*hf->name.s
  927. && strncmp(hdr->name.s, hf->name.s, hf->name.len)==0)
  928. return hdr;
  929. }
  930. return NULL;
  931. }
  932. /**
  933. * set msg context id
  934. * - return: -1 on error; 0 - on set
  935. */
  936. int msg_ctx_id_set(const sip_msg_t* const msg, msg_ctx_id_t* const mid)
  937. {
  938. if(msg==NULL || mid==NULL)
  939. return -1;
  940. mid->msgid = msg->id;
  941. mid->pid = msg->pid;
  942. return 0;
  943. }
  944. /**
  945. * check msg context id
  946. * - return: -1 on error; 0 - on no match; 1 - on match
  947. */
  948. int msg_ctx_id_match(const sip_msg_t* const msg, const msg_ctx_id_t* const mid)
  949. {
  950. if(msg==NULL || mid==NULL)
  951. return -1;
  952. if(msg->id != mid->msgid || msg->pid!=mid->pid)
  953. return 0;
  954. return 1;
  955. }
  956. /**
  957. * set msg time value
  958. */
  959. int msg_set_time(sip_msg_t* const msg)
  960. {
  961. if(unlikely(msg==NULL))
  962. return -2;
  963. if(msg->tval.tv_sec!=0)
  964. return 0;
  965. return gettimeofday(&msg->tval, NULL);
  966. }