msg_parser.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. /*
  2. * $Id$
  3. *
  4. * sip msg. header proxy parser
  5. *
  6. */
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include "msg_parser.h"
  10. #include "parser_f.h"
  11. #include "ut.h"
  12. #include "error.h"
  13. #include "dprint.h"
  14. #include "mem.h"
  15. #ifdef DEBUG_DMALLOC
  16. #include <dmalloc.h>
  17. #endif
  18. /* parses the first line, returns pointer to next line & fills fl;
  19. also modifies buffer (to avoid extra copy ops) */
  20. char* parse_first_line(char* buffer, unsigned int len, struct msg_start * fl)
  21. {
  22. char *tmp;
  23. char* second;
  24. char* third;
  25. char* nl;
  26. int offset;
  27. /* int l; */
  28. char* end;
  29. char s1,s2,s3;
  30. /* grammar:
  31. request = method SP uri SP version CRLF
  32. response = version SP status SP reason CRLF
  33. (version = "SIP/2.0")
  34. */
  35. end=buffer+len;
  36. /* see if it's a reply (status) */
  37. /* jku -- parse well-known methods */
  38. /* drop messages which are so short they are for sure useless;
  39. utilize knowledge of minimum size in parsing the first
  40. token
  41. */
  42. if (len <=16 ) {
  43. LOG(L_INFO, "ERROR: parse_first_line: message too short\n");
  44. goto error1;
  45. }
  46. tmp=buffer;
  47. /* is it perhaps a reply, ie does it start with "SIP...." ? */
  48. if ( (*tmp=='S' || *tmp=='s') &&
  49. strncasecmp( tmp+1, SIP_VERSION+1, SIP_VERSION_LEN-1)==0 &&
  50. (*(tmp+SIP_VERSION_LEN)==' ')) {
  51. fl->type=SIP_REPLY;
  52. fl->u.reply.version.len=SIP_VERSION_LEN;
  53. tmp=buffer+SIP_VERSION_LEN;
  54. } else IFISMETHOD( INVITE, 'I' )
  55. else IFISMETHOD( CANCEL, 'C')
  56. else IFISMETHOD( ACK, 'A' )
  57. else IFISMETHOD( BYE, 'B' )
  58. /* if you want to add another method XXX, include METHOD_XXX in
  59. H-file (this is the value which you will take later in
  60. processing and define XXX_LEN as length of method name;
  61. then just call IFISMETHOD( XXX, 'X' ) ... 'X' is the first
  62. latter; everything must be capitals
  63. */
  64. else {
  65. /* neither reply, nor any of known method requests,
  66. let's believe it is an unknown method request
  67. */
  68. tmp=eat_token_end(buffer,buffer+len);
  69. if ((tmp==buffer)||(tmp>=end)){
  70. LOG(L_INFO, "ERROR:parse_first_line: empty or bad first line\n");
  71. goto error1;
  72. }
  73. if (*tmp!=' ') {
  74. LOG(L_INFO, "ERROR:parse_first_line: method not followed by SP\n");
  75. goto error1;
  76. }
  77. fl->type=SIP_REQUEST;
  78. fl->u.request.method_value=METHOD_OTHER;
  79. fl->u.request.method.len=tmp-buffer;
  80. }
  81. /* identifying type of message over now;
  82. tmp points at space after; go ahead */
  83. fl->u.request.method.s=buffer; /* store ptr to first token */
  84. (*tmp)=0; /* mark the 1st token end */
  85. second=tmp+1; /* jump to second token */
  86. offset=second-buffer;
  87. /* EoJku */
  88. /* next element */
  89. tmp=eat_token_end(second, second+len-offset);
  90. if (tmp>=end){
  91. goto error;
  92. }
  93. offset+=tmp-second;
  94. third=eat_space_end(tmp, tmp+len-offset);
  95. offset+=third-tmp;
  96. if ((third==tmp)||(tmp>=end)){
  97. goto error;
  98. }
  99. *tmp=0; /* mark the end of the token */
  100. fl->u.request.uri.s=second;
  101. fl->u.request.uri.len=tmp-second;
  102. /* jku: parse status code */
  103. if (fl->type==SIP_REPLY) {
  104. if (fl->u.request.uri.len!=3) {
  105. LOG(L_INFO, "ERROR:parse_first_line: len(status code)!=3: %s\n",
  106. second );
  107. goto error;
  108. }
  109. s1=*second; s2=*(second+1);s3=*(second+2);
  110. if (s1>='0' && s1<='9' &&
  111. s2>='0' && s2<='9' &&
  112. s3>='0' && s3<='9' ) {
  113. fl->u.reply.statusclass=s1-'0';
  114. fl->u.reply.statuscode=fl->u.reply.statusclass*100+10*(s2-'0')+(s3-'0');
  115. } else {
  116. LOG(L_INFO, "ERROR:parse_first_line: status_code non-numerical: %s\n",
  117. second );
  118. goto error;
  119. }
  120. }
  121. /* EoJku */
  122. /* last part: for a request it must be the version, for a reply
  123. * it can contain almost anything, including spaces, so we don't care
  124. * about it*/
  125. if (fl->type==SIP_REQUEST){
  126. tmp=eat_token_end(third,third+len-offset);
  127. offset+=tmp-third;
  128. if ((tmp==third)||(tmp>=end)){
  129. goto error;
  130. }
  131. if (! is_empty_end(tmp, tmp+len-offset)){
  132. goto error;
  133. }
  134. }else{
  135. tmp=eat_token2_end(third,third+len-offset,'\r'); /* find end of line
  136. ('\n' or '\r') */
  137. if (tmp>=end){ /* no crlf in packet => invalid */
  138. goto error;
  139. }
  140. offset+=tmp-third;
  141. }
  142. nl=eat_line(tmp,len-offset);
  143. if (nl>=end){ /* no crlf in packet or only 1 line > invalid */
  144. goto error;
  145. }
  146. *tmp=0;
  147. fl->u.request.version.s=third;
  148. fl->u.request.version.len=tmp-third;
  149. return nl;
  150. error:
  151. LOG(L_INFO, "ERROR:parse_first_line: bad %s first line\n",
  152. (fl->type==SIP_REPLY)?"reply(status)":"request");
  153. error1:
  154. fl->type=SIP_INVALID;
  155. LOG(L_INFO, "ERROR: at line 0 char %d\n", offset);
  156. /* skip line */
  157. nl=eat_line(buffer,len);
  158. return nl;
  159. }
  160. /* returns pointer to next header line, and fill hdr_f ;
  161. * if at end of header returns pointer to the last crlf (always buf)*/
  162. char* get_hdr_field(char* buf, char* end, struct hdr_field* hdr)
  163. {
  164. char* tmp;
  165. char *match;
  166. struct via_body *vb;
  167. struct cseq_body* cseq_b;
  168. if ((*buf)=='\n' || (*buf)=='\r'){
  169. /* double crlf or lflf or crcr */
  170. DBG("found end of header\n");
  171. hdr->type=HDR_EOH;
  172. return buf;
  173. }
  174. tmp=parse_hname(buf, end, hdr);
  175. if (hdr->type==HDR_ERROR){
  176. LOG(L_ERR, "ERROR: get_hdr_field: bad header\n");
  177. goto error;
  178. }
  179. switch(hdr->type){
  180. case HDR_VIA:
  181. vb=pkg_malloc(sizeof(struct via_body));
  182. if (vb==0){
  183. LOG(L_ERR, "get_hdr_field: out of memory\n");
  184. goto error;
  185. }
  186. memset(vb,0,sizeof(struct via_body));
  187. hdr->body.s=tmp;
  188. tmp=parse_via(tmp, end, vb);
  189. if (vb->error==VIA_PARSE_ERROR){
  190. LOG(L_ERR, "ERROR: get_hdr_field: bad via\n");
  191. pkg_free(vb);
  192. goto error;
  193. }
  194. hdr->parsed=vb;
  195. vb->hdr.s=hdr->name.s;
  196. vb->hdr.len=hdr->name.len;
  197. hdr->body.len=tmp-hdr->body.s;
  198. break;
  199. case HDR_CSEQ:
  200. cseq_b=pkg_malloc(sizeof(struct cseq_body));
  201. if (cseq_b==0){
  202. LOG(L_ERR, "get_hdr_field: out of memory\n");
  203. goto error;
  204. }
  205. memset(cseq_b, 0, sizeof(struct cseq_body));
  206. hdr->body.s=tmp;
  207. tmp=parse_cseq(tmp, end, cseq_b);
  208. if (cseq_b->error==PARSE_ERROR){
  209. LOG(L_ERR, "ERROR: get_hdr_field: bad cseq\n");
  210. pkg_free(cseq_b);
  211. goto error;
  212. }
  213. hdr->parsed=cseq_b;
  214. hdr->body.len=tmp-hdr->body.s;
  215. DBG("get_hdr_field: cseq <%s>: <%s> <%s>\n",
  216. hdr->name.s, cseq_b->number.s, cseq_b->method.s);
  217. break;
  218. case HDR_TO:
  219. case HDR_FROM:
  220. case HDR_CALLID:
  221. case HDR_CONTACT:
  222. case HDR_OTHER:
  223. /* just skip over it */
  224. hdr->body.s=tmp;
  225. /* find end of header */
  226. /* find lf */
  227. do{
  228. match=q_memchr(tmp, '\n', end-tmp);
  229. if (match){
  230. match++;
  231. }else {
  232. tmp=end;
  233. LOG(L_ERR,
  234. "ERROR: get_hdr_field: bad body for <%s>(%d)\n",
  235. hdr->name.s, hdr->type);
  236. goto error;
  237. }
  238. tmp=match;
  239. }while( match<end &&( (*match==' ')||(*match=='\t') ) );
  240. *(match-1)=0; /*null terminate*/
  241. hdr->body.len=match-hdr->body.s;
  242. break;
  243. default:
  244. LOG(L_CRIT, "BUG: get_hdr_field: unknown header type %d\n",
  245. hdr->type);
  246. goto error;
  247. }
  248. return tmp;
  249. error:
  250. DBG("get_hdr_field: error exit\n");
  251. hdr->type=HDR_ERROR;
  252. return tmp;
  253. }
  254. char* parse_hostport(char* buf, str* host, short int* port)
  255. {
  256. char *tmp;
  257. int err;
  258. host->s=buf;
  259. for(tmp=buf;(*tmp)&&(*tmp!=':');tmp++);
  260. host->len=tmp-buf;
  261. if (*tmp==0){
  262. *port=0;
  263. }else{
  264. *tmp=0;
  265. *port=str2s(tmp+1, strlen(tmp+1), &err);
  266. if (err ){
  267. LOG(L_INFO,
  268. "ERROR: hostport: trailing chars in port number: %s\n",
  269. tmp+1);
  270. /* report error? */
  271. }
  272. }
  273. return host->s;
  274. }
  275. /*BUGGY*/
  276. char * parse_cseq(char *buf, char* end, struct cseq_body* cb)
  277. {
  278. char *t, *m, *m_end;
  279. char c;
  280. cb->error=PARSE_ERROR;
  281. t=eat_space_end(buf, end);
  282. if (t>=end) goto error;
  283. cb->number.s=t;
  284. t=eat_token_end(t, end);
  285. if (t>=end) goto error;
  286. m=eat_space_end(t, end);
  287. m_end=eat_token_end(m, end);
  288. *t=0; /*null terminate it*/
  289. cb->number.len=t-cb->number.s;
  290. DBG("parse_cseq: found number %s\n", cb->number.s);
  291. if (m_end>=end) goto error;
  292. if (m_end==m){
  293. /* null method*/
  294. LOG(L_ERR, "ERROR:parse_cseq: no method found\n");
  295. goto error;
  296. }
  297. cb->method.s=m;
  298. t=m_end;
  299. c=*t;
  300. *t=0; /*null terminate it*/
  301. cb->method.len=t-cb->method.s;
  302. DBG("parse_cseq: found method %s\n", cb->method.s);
  303. t++;
  304. /*check if the header ends here*/
  305. if (c=='\n') goto check_continue;
  306. do{
  307. for (;(t<end)&&((*t==' ')||(*t=='\t')||(*t=='\r'));t++);
  308. if (t>=end) goto error;
  309. if (*t!='\n'){
  310. LOG(L_ERR, "ERROR:parse_cseq: unexpected char <%c> at end of"
  311. " cseq\n", *t);
  312. goto error;
  313. }
  314. t++;
  315. check_continue:
  316. }while( (t<end) && ((*t==' ')||(*t=='\t')) );
  317. cb->error=PARSE_OK;
  318. return t;
  319. error:
  320. LOG(L_ERR, "ERROR: parse_cseq: bad cseq\n");
  321. return t;
  322. }
  323. /* buf= pointer to begining of uri (sip:[email protected]:5060;a=b?h=i)
  324. len= len of uri
  325. returns: fills uri & returns <0 on error or 0 if ok */
  326. int parse_uri(char *buf, int len, struct sip_uri* uri)
  327. {
  328. char* next, *end;
  329. char *user, *passwd, *host, *port, *params, *headers;
  330. int host_len, port_len, params_len, headers_len;
  331. int ret;
  332. ret=0;
  333. end=buf+len;
  334. memset(uri, 0, sizeof(struct sip_uri)); /* zero it all, just to be sure */
  335. /* look for "sip:"*/;
  336. next=q_memchr(buf, ':', len);
  337. if ((next==0)||(strncmp(buf,"sip",next-buf)!=0)){
  338. LOG(L_DBG, "ERROR: parse_uri: bad sip uri\n");
  339. ret=E_UNSPEC;
  340. goto error;
  341. }
  342. buf=next+1; /* next char after ':' */
  343. if (buf>end){
  344. LOG(L_DBG, "ERROR: parse_uri: uri too short\n");
  345. ret=E_UNSPEC;
  346. goto error;
  347. }
  348. /*look for '@' */
  349. next=q_memchr(buf,'@', end-buf);
  350. if (next==0){
  351. /* no '@' found, => no userinfo */
  352. uri->user.s=0;
  353. uri->passwd.s=0;
  354. host=buf;
  355. }else{
  356. /* found it */
  357. user=buf;
  358. /* try to find passwd */
  359. passwd=q_memchr(user,':', next-user);
  360. if (passwd==0){
  361. /* no ':' found => no password */
  362. uri->passwd.s=0;
  363. uri->user.s=(char*)pkg_malloc(next-user+1);
  364. if (uri->user.s==0){
  365. LOG(L_ERR,"ERROR:parse_uri: memory allocation failure\n");
  366. ret=E_OUT_OF_MEM;
  367. goto error;
  368. }
  369. memcpy(uri->user.s, user, next-user);
  370. uri->user.len=next-user;
  371. uri->user.s[next-user]=0; /* null terminate it,
  372. usefull for easy printing*/
  373. }else{
  374. uri->user.s=(char*)pkg_malloc(passwd-user+1);
  375. if (uri->user.s==0){
  376. LOG(L_ERR,"ERROR:parse_uri: memory allocation failure\n");
  377. ret=E_OUT_OF_MEM;
  378. goto error;
  379. }
  380. memcpy(uri->user.s, user, passwd-user);
  381. uri->user.len=passwd-user;
  382. uri->user.s[passwd-user]=0;
  383. passwd++; /*skip ':' */
  384. uri->passwd.s=(char*)pkg_malloc(next-passwd+1);
  385. if (uri->passwd.s==0){
  386. LOG(L_ERR,"ERROR:parse_uri: memory allocation failure\n");
  387. ret=E_OUT_OF_MEM;
  388. goto error;
  389. }
  390. memcpy(uri->passwd.s, passwd, next-passwd);
  391. uri->passwd.len=next-passwd;
  392. uri->passwd.s[next-passwd]=0;
  393. }
  394. host=next+1; /* skip '@' */
  395. }
  396. /* try to find the rest */
  397. if(host>=end){
  398. LOG(L_DBG, "ERROR: parse_uri: missing hostport\n");
  399. ret=E_UNSPEC;
  400. goto error;
  401. }
  402. headers=q_memchr(host,'?',end-host);
  403. params=q_memchr(host,';',end-host);
  404. port=q_memchr(host,':',end-host);
  405. host_len=(port)?port-host:(params)?params-host:(headers)?headers-host:
  406. end-host;
  407. /* get host */
  408. uri->host.s=pkg_malloc(host_len+1);
  409. if (uri->host.s==0){
  410. LOG(L_ERR, "ERROR: parse_uri: memory allocation error\n");
  411. ret=E_OUT_OF_MEM;
  412. goto error;
  413. }
  414. memcpy(uri->host.s, host, host_len);
  415. uri->host.len=host_len;
  416. uri->host.s[host_len]=0;
  417. /* get port*/
  418. if ((port)&&(port+1<end)){
  419. port++;
  420. if ( ((params) &&(params<port))||((headers) &&(headers<port)) ){
  421. /* error -> invalid uri we found ';' or '?' before ':' */
  422. LOG(L_DBG, "ERROR: parse_uri: malformed sip uri\n");
  423. ret=E_UNSPEC;
  424. goto error;
  425. }
  426. port_len=(params)?params-port:(headers)?headers-port:end-port;
  427. uri->port.s=pkg_malloc(port_len+1);
  428. if (uri->port.s==0){
  429. LOG(L_ERR, "ERROR: parse_uri: memory allocation error\n");
  430. ret=E_OUT_OF_MEM;
  431. goto error;
  432. }
  433. memcpy(uri->port.s, port, port_len);
  434. uri->port.len=port_len;
  435. uri->port.s[port_len]=0;
  436. }else uri->port.s=0;
  437. /* get params */
  438. if ((params)&&(params+1<end)){
  439. params++;
  440. if ((headers) && (headers<params)){
  441. /* error -> invalid uri we found '?' or '?' before ';' */
  442. LOG(L_DBG, "ERROR: parse_uri: malformed sip uri\n");
  443. ret=E_UNSPEC;
  444. goto error;
  445. }
  446. params_len=(headers)?headers-params:end-params;
  447. uri->params.s=pkg_malloc(params_len+1);
  448. if (uri->params.s==0){
  449. LOG(L_ERR, "ERROR: parse_uri: memory allocation error\n");
  450. ret=E_OUT_OF_MEM;
  451. goto error;
  452. }
  453. memcpy(uri->params.s, params, params_len);
  454. uri->params.len=params_len;
  455. uri->params.s[params_len]=0;
  456. }else uri->params.s=0;
  457. /*get headers */
  458. if ((headers)&&(headers+1<end)){
  459. headers++;
  460. headers_len=end-headers;
  461. uri->headers.s=pkg_malloc(headers_len+1);
  462. if(uri->headers.s==0){
  463. LOG(L_ERR, "ERROR: parse_uri: memory allocation error\n");
  464. ret=E_OUT_OF_MEM;
  465. goto error;
  466. }
  467. memcpy(uri->headers.s, headers, headers_len);
  468. uri->headers.len=headers_len;
  469. uri->headers.s[headers_len]=0;
  470. }else uri->headers.s=0;
  471. return ret;
  472. error:
  473. free_uri(uri);
  474. return ret;
  475. }
  476. /* parse the headers and adds them to msg->headers and msg->to, from etc.
  477. * It stops when all the headers requested in flags were parsed, on error
  478. * (bad header) or end of headers */
  479. /* note: it continues where it previously stopped and goes ahead until
  480. end is encountered or desired HFs are found; if you call it twice
  481. for the same HF which is present only once, it will fail the second
  482. time; if you call it twice and the HF is found on second time too,
  483. it's not replaced in the well-known HF pointer but just added to
  484. header list; if you want to use a dumbie convenience function which will
  485. give you the first occurance of a header you are interested in,
  486. look at check_transaction_quadruple
  487. */
  488. int parse_headers(struct sip_msg* msg, int flags)
  489. {
  490. struct hdr_field* hf;
  491. char* tmp;
  492. char* rest;
  493. char* end;
  494. end=msg->buf+msg->len;
  495. tmp=msg->unparsed;
  496. DBG("parse_headers: flags=%d\n", flags);
  497. while( tmp<end && (flags & msg->parsed_flag) != flags){
  498. hf=pkg_malloc(sizeof(struct hdr_field));
  499. if (hf==0){
  500. LOG(L_ERR, "ERROR:parse_headers: memory allocation error\n");
  501. goto error;
  502. }
  503. memset(hf,0, sizeof(struct hdr_field));
  504. hf->type=HDR_ERROR;
  505. rest=get_hdr_field(tmp, msg->buf+msg->len, hf);
  506. switch (hf->type){
  507. case HDR_ERROR:
  508. LOG(L_INFO,"ERROR: bad header field\n");
  509. goto error;
  510. case HDR_EOH:
  511. msg->eoh=tmp; /* or rest?*/
  512. msg->parsed_flag|=HDR_EOH;
  513. pkg_free(hf);
  514. goto skip;
  515. case HDR_OTHER: /*do nothing*/
  516. break;
  517. case HDR_CALLID:
  518. if (msg->callid==0) msg->callid=hf;
  519. msg->parsed_flag|=HDR_CALLID;
  520. break;
  521. case HDR_TO:
  522. if (msg->to==0) msg->to=hf;
  523. msg->parsed_flag|=HDR_TO;
  524. break;
  525. case HDR_CSEQ:
  526. if (msg->cseq==0) msg->cseq=hf;
  527. msg->parsed_flag|=HDR_CSEQ;
  528. break;
  529. case HDR_FROM:
  530. if (msg->from==0) msg->from=hf;
  531. msg->parsed_flag|=HDR_FROM;
  532. break;
  533. case HDR_CONTACT:
  534. if (msg->contact==0) msg->contact=hf;
  535. msg->parsed_flag|=HDR_CONTACT;
  536. break;
  537. case HDR_VIA:
  538. msg->parsed_flag|=HDR_VIA;
  539. DBG("parse_headers: Via1 found, flags=%d\n", flags);
  540. if (msg->via1==0) {
  541. msg->h_via1=hf;
  542. msg->via1=hf->parsed;
  543. if (msg->via1->next){
  544. msg->via2=msg->via1->next;
  545. msg->parsed_flag|=HDR_VIA2;
  546. }
  547. }else if (msg->via2==0){
  548. msg->h_via2=hf;
  549. msg->via2=hf->parsed;
  550. msg->parsed_flag|=HDR_VIA2;
  551. DBG("parse_headers: Via2 found, flags=%d\n", flags);
  552. }
  553. break;
  554. default:
  555. LOG(L_CRIT, "BUG: parse_headers: unknown header type %d\n",
  556. hf->type);
  557. goto error;
  558. }
  559. /* add the header to the list*/
  560. if (msg->last_header==0){
  561. msg->headers=hf;
  562. msg->last_header=hf;
  563. }else{
  564. msg->last_header->next=hf;
  565. msg->last_header=hf;
  566. }
  567. #ifdef EXTRA_DEBUG
  568. DBG("header field type %d, name=<%s>, body=<%s>\n",
  569. hf->type, hf->name.s, hf->body.s);
  570. #endif
  571. tmp=rest;
  572. }
  573. skip:
  574. msg->unparsed=tmp;
  575. return 0;
  576. error:
  577. if (hf) pkg_free(hf);
  578. return -1;
  579. }
  580. /* returns 0 if ok, -1 for errors */
  581. int parse_msg(char* buf, unsigned int len, struct sip_msg* msg)
  582. {
  583. char *tmp, *bar;
  584. char* rest;
  585. char* first_via;
  586. char* second_via;
  587. struct msg_start *fl;
  588. int offset;
  589. int flags;
  590. /* eat crlf from the beginning */
  591. for (tmp=buf; (*tmp=='\n' || *tmp=='\r')&&
  592. tmp-buf < len ; tmp++);
  593. offset=tmp-buf;
  594. fl=&(msg->first_line);
  595. rest=parse_first_line(tmp, len-offset, fl);
  596. #if 0
  597. rest=parse_fline(tmp, buf+len, fl);
  598. #endif
  599. offset+=rest-tmp;
  600. tmp=rest;
  601. switch(fl->type){
  602. case SIP_INVALID:
  603. DBG("parse_msg: invalid message\n");
  604. goto error;
  605. break;
  606. case SIP_REQUEST:
  607. DBG("SIP Request:\n");
  608. DBG(" method: <%s>\n",fl->u.request.method);
  609. DBG(" uri: <%s>\n",fl->u.request.uri);
  610. DBG(" version: <%s>\n",fl->u.request.version);
  611. flags=HDR_VIA;
  612. break;
  613. case SIP_REPLY:
  614. DBG("SIP Reply (status):\n");
  615. DBG(" version: <%s>\n",fl->u.reply.version);
  616. DBG(" status: <%s>\n",fl->u.reply.status);
  617. DBG(" reason: <%s>\n",fl->u.reply.reason);
  618. flags=HDR_VIA|HDR_VIA2;
  619. break;
  620. default:
  621. DBG("unknown type %d\n",fl->type);
  622. }
  623. msg->unparsed=tmp;
  624. /*find first Via: */
  625. first_via=0;
  626. second_via=0;
  627. if (parse_headers(msg, flags)==-1) goto error;
  628. #ifdef EXTRA_DEBUG
  629. /* dump parsed data */
  630. if (msg->via1){
  631. DBG(" first via: <%s/%s/%s> <%s:%s(%d)>",
  632. msg->via1->name.s, msg->via1->version.s,
  633. msg->via1->transport.s, msg->via1->host.s,
  634. msg->via1->port_str, msg->via1->port);
  635. if (msg->via1->params.s) DBG(";<%s>", msg->via1->params.s);
  636. if (msg->via1->comment.s) DBG(" <%s>", msg->via1->comment.s);
  637. DBG ("\n");
  638. }
  639. if (msg->via2){
  640. DBG(" first via: <%s/%s/%s> <%s:%s(%d)>",
  641. msg->via2->name.s, msg->via2->version.s,
  642. msg->via2->transport.s, msg->via2->host.s,
  643. msg->via2->port_str, msg->via2->port);
  644. if (msg->via2->params.s) DBG(";<%s>", msg->via2->params.s);
  645. if (msg->via2->comment.s) DBG(" <%s>", msg->via2->comment.s);
  646. DBG ("\n");
  647. }
  648. #endif
  649. #ifdef EXTRA_DEBUG
  650. DBG("exiting parse_msg\n");
  651. #endif
  652. return 0;
  653. error:
  654. return -1;
  655. }
  656. void free_uri(struct sip_uri* u)
  657. {
  658. if (u){
  659. if (u->user.s) pkg_free(u->user.s);
  660. if (u->passwd.s) pkg_free(u->passwd.s);
  661. if (u->host.s) pkg_free(u->host.s);
  662. if (u->port.s) pkg_free(u->port.s);
  663. if (u->params.s) pkg_free(u->params.s);
  664. if (u->headers.s) pkg_free(u->headers.s);
  665. }
  666. }
  667. void free_via_param_list(struct via_param* vp)
  668. {
  669. struct via_param* foo;
  670. while(vp){
  671. foo=vp;
  672. vp=vp->next;
  673. pkg_free(foo);
  674. }
  675. }
  676. void free_via_list(struct via_body* vb)
  677. {
  678. struct via_body* foo;
  679. while(vb){
  680. foo=vb;
  681. vb=vb->next;
  682. if (foo->param_lst) free_via_param_list(foo->param_lst);
  683. pkg_free(foo);
  684. }
  685. }
  686. /* frees a hdr_field structure,
  687. * WARNING: it frees only parsed (and not name.s, body.s)*/
  688. void clean_hdr_field(struct hdr_field* hf)
  689. {
  690. if (hf->parsed){
  691. switch(hf->type){
  692. case HDR_VIA:
  693. free_via_list(hf->parsed);
  694. break;
  695. case HDR_CSEQ:
  696. pkg_free(hf->parsed);
  697. break;
  698. default:
  699. LOG(L_CRIT, "BUG: clean_hdr_field: unknown header type %d\n",
  700. hf->type);
  701. }
  702. }
  703. }
  704. /* frees a hdr_field list,
  705. * WARNING: frees only ->parsed and ->next*/
  706. void free_hdr_field_lst(struct hdr_field* hf)
  707. {
  708. struct hdr_field* foo;
  709. while(hf){
  710. foo=hf;
  711. hf=hf->next;
  712. clean_hdr_field(foo);
  713. pkg_free(foo);
  714. }
  715. }
  716. /*only the content*/
  717. void free_sip_msg(struct sip_msg* msg)
  718. {
  719. if (msg->new_uri.s) { pkg_free(msg->new_uri.s); msg->new_uri.len=0; }
  720. if (msg->headers) free_hdr_field_lst(msg->headers);
  721. if (msg->add_rm) free_lump_list(msg->add_rm);
  722. if (msg->repl_add_rm) free_lump_list(msg->repl_add_rm);
  723. pkg_free(msg->orig);
  724. pkg_free(msg->buf);
  725. }
  726. #if 0
  727. /* it's a macro now*/
  728. /* make sure all HFs needed for transaction identification have been
  729. parsed; return 0 if those HFs can't be found
  730. */
  731. int check_transaction_quadruple( struct sip_msg* msg )
  732. {
  733. return
  734. (parse_headers(msg, HDR_FROM|HDR_TO|HDR_CALLID|HDR_CSEQ)!=-1 &&
  735. msg->from && msg->to && msg->callid && msg->cseq);
  736. /* replaced by me ( :) andrei)
  737. ( (msg->from || (parse_headers( msg, HDR_FROM)!=-1 && msg->from)) &&
  738. (msg->to|| (parse_headers( msg, HDR_TO)!=-1 && msg->to)) &&
  739. (msg->callid|| (parse_headers( msg, HDR_CALLID)!=-1 && msg->callid)) &&
  740. (msg->cseq|| (parse_headers( msg, HDR_CSEQ)!=-1 && msg->cseq)) ) ? 1 : 0;
  741. */
  742. }
  743. #endif