parse_to.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. /*
  2. * $Id$
  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. * ser is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. * History:
  23. * ---------
  24. * 2003-04-26 ZSW (jiri)
  25. * 2010-03-03 fix multi-token no-quotes display name (andrei)
  26. */
  27. /** Parser :: Parse To: header.
  28. * @file
  29. * @ingroup parser
  30. */
  31. #include "parse_to.h"
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include "../dprint.h"
  35. #include "msg_parser.h"
  36. #include "parse_uri.h"
  37. #include "../ut.h"
  38. #include "../mem/mem.h"
  39. enum {
  40. START_TO, DISPLAY_QUOTED, E_DISPLAY_QUOTED, DISPLAY_TOKEN,
  41. DISPLAY_TOKEN_SP, S_URI_ENCLOSED, URI_ENCLOSED, E_URI_ENCLOSED,
  42. URI_OR_TOKEN, MAYBE_URI_END, END, F_CR, F_LF, F_CRLF
  43. };
  44. enum {
  45. S_PARA_NAME=20, PARA_NAME, S_EQUAL, S_PARA_VALUE, TAG1, TAG2,
  46. TAG3, PARA_VALUE_TOKEN , PARA_VALUE_QUOTED, E_PARA_VALUE
  47. };
  48. #define add_param( _param , _body , _newparam ) \
  49. do{\
  50. DBG("DEBUG: add_param: %.*s=%.*s\n",param->name.len,ZSW(param->name.s),\
  51. param->value.len,ZSW(param->value.s));\
  52. if (!(_body)->param_lst) (_body)->param_lst=(_param);\
  53. else (_body)->last_param->next=(_param);\
  54. (_body)->last_param =(_param);\
  55. if ((_param)->type==TAG_PARAM)\
  56. memcpy(&((_body)->tag_value),&((_param)->value),sizeof(str));\
  57. _newparam = 0;\
  58. }while(0);
  59. static /*inline*/ char* parse_to_param(char *buffer, char *end,
  60. struct to_body *to_b,
  61. int *returned_status)
  62. {
  63. struct to_param *param;
  64. struct to_param *newparam;
  65. int status;
  66. int saved_status;
  67. char *tmp;
  68. param=0;
  69. newparam=0;
  70. status=E_PARA_VALUE;
  71. saved_status=E_PARA_VALUE;
  72. for( tmp=buffer; tmp<end; tmp++)
  73. {
  74. switch(*tmp)
  75. {
  76. case ' ':
  77. case '\t':
  78. switch (status)
  79. {
  80. case TAG3:
  81. param->type=TAG_PARAM;
  82. case PARA_NAME:
  83. case TAG1:
  84. case TAG2:
  85. param->name.len = tmp-param->name.s;
  86. status = S_EQUAL;
  87. break;
  88. case PARA_VALUE_TOKEN:
  89. param->value.len = tmp-param->value.s;
  90. status = E_PARA_VALUE;
  91. add_param(param, to_b, newparam);
  92. break;
  93. case F_CRLF:
  94. case F_LF:
  95. case F_CR:
  96. /*previous=crlf and now =' '*/
  97. status=saved_status;
  98. break;
  99. }
  100. break;
  101. case '\n':
  102. switch (status)
  103. {
  104. case S_PARA_NAME:
  105. case S_EQUAL:
  106. case S_PARA_VALUE:
  107. case E_PARA_VALUE:
  108. saved_status=status;
  109. status=F_LF;
  110. break;
  111. case TAG3:
  112. param->type=TAG_PARAM;
  113. case PARA_NAME:
  114. case TAG1:
  115. case TAG2:
  116. param->name.len = tmp-param->name.s;
  117. saved_status = S_EQUAL;
  118. status = F_LF;
  119. break;
  120. case PARA_VALUE_TOKEN:
  121. param->value.len = tmp-param->value.s;
  122. saved_status = E_PARA_VALUE;
  123. status = F_LF;
  124. add_param(param, to_b, newparam);
  125. break;
  126. case F_CR:
  127. status=F_CRLF;
  128. break;
  129. case F_CRLF:
  130. case F_LF:
  131. status=saved_status;
  132. goto endofheader;
  133. default:
  134. LOG( L_ERR , "ERROR: parse_to_param : "
  135. "unexpected char [%c] in status %d: <<%.*s>> .\n",
  136. *tmp,status, (int)(tmp-buffer), ZSW(buffer));
  137. goto error;
  138. }
  139. break;
  140. case '\r':
  141. switch (status)
  142. {
  143. case S_PARA_NAME:
  144. case S_EQUAL:
  145. case S_PARA_VALUE:
  146. case E_PARA_VALUE:
  147. saved_status=status;
  148. status=F_CR;
  149. break;
  150. case TAG3:
  151. param->type=TAG_PARAM;
  152. case PARA_NAME:
  153. case TAG1:
  154. case TAG2:
  155. param->name.len = tmp-param->name.s;
  156. saved_status = S_EQUAL;
  157. status = F_CR;
  158. break;
  159. case PARA_VALUE_TOKEN:
  160. param->value.len = tmp-param->value.s;
  161. saved_status = E_PARA_VALUE;
  162. status = F_CR;
  163. add_param(param, to_b, newparam);
  164. break;
  165. case F_CRLF:
  166. case F_CR:
  167. case F_LF:
  168. status=saved_status;
  169. goto endofheader;
  170. default:
  171. LOG( L_ERR , "ERROR: parse_to_param : "
  172. "unexpected char [%c] in status %d: <<%.*s>> .\n",
  173. *tmp,status, (int)(tmp-buffer), ZSW(buffer));
  174. goto error;
  175. }
  176. break;
  177. case 0:
  178. switch (status)
  179. {
  180. case TAG3:
  181. param->type = TAG_PARAM;
  182. case PARA_NAME:
  183. case TAG1:
  184. case TAG2:
  185. param->name.len = tmp-param->name.s;
  186. status = S_EQUAL;
  187. case S_EQUAL:
  188. case S_PARA_VALUE:
  189. saved_status=status;
  190. goto endofheader;
  191. case PARA_VALUE_TOKEN:
  192. status = E_PARA_VALUE;
  193. param->value.len = tmp-param->value.s;
  194. add_param(param , to_b, newparam);
  195. case E_PARA_VALUE:
  196. saved_status = status;
  197. goto endofheader;
  198. break;
  199. default:
  200. LOG( L_ERR , "ERROR: parse_to_param : "
  201. "unexpected char [%c] in status %d: <<%.*s>> .\n",
  202. *tmp,status, (int)(tmp-buffer), ZSW(buffer));
  203. goto error;
  204. }
  205. break;
  206. case '\\':
  207. switch (status)
  208. {
  209. case PARA_VALUE_QUOTED:
  210. switch (*(tmp+1))
  211. {
  212. case '\r':
  213. case '\n':
  214. break;
  215. default:
  216. tmp++;
  217. }
  218. default:
  219. LOG( L_ERR , "ERROR: parse_to_param : "
  220. "unexpected char [%c] in status %d: <<%.*s>> .\n",
  221. *tmp,status, (int)(tmp-buffer), ZSW(buffer));
  222. goto error;
  223. }
  224. break;
  225. case '"':
  226. switch (status)
  227. {
  228. case S_PARA_VALUE:
  229. param->value.s = tmp+1;
  230. status = PARA_VALUE_QUOTED;
  231. break;
  232. case PARA_VALUE_QUOTED:
  233. param->value.len=tmp-param->value.s;
  234. add_param(param, to_b, newparam);
  235. status = E_PARA_VALUE;
  236. break;
  237. case F_CRLF:
  238. case F_LF:
  239. case F_CR:
  240. /*previous=crlf and now !=' '*/
  241. goto endofheader;
  242. default:
  243. LOG( L_ERR , "ERROR: parse_to_param :"
  244. "unexpected char [%c] in status %d: <<%.*s>> .\n",
  245. *tmp,status,(int)(tmp-buffer), ZSW(buffer));
  246. goto error;
  247. }
  248. break;
  249. case ';' :
  250. switch (status)
  251. {
  252. case PARA_VALUE_QUOTED:
  253. break;
  254. case TAG3:
  255. param->type = TAG_PARAM;
  256. case PARA_NAME:
  257. case TAG1:
  258. case TAG2:
  259. param->name.len = tmp-param->name.s;
  260. case S_EQUAL:
  261. param->value.s = 0;
  262. param->value.len = 0;
  263. goto semicolon_add_param;
  264. case S_PARA_VALUE:
  265. param->value.s = tmp;
  266. case PARA_VALUE_TOKEN:
  267. param->value.len=tmp-param->value.s;
  268. semicolon_add_param:
  269. add_param(param, to_b, newparam);
  270. case E_PARA_VALUE:
  271. param = (struct to_param*)
  272. pkg_malloc(sizeof(struct to_param));
  273. if (!param){
  274. LOG( L_ERR , "ERROR: parse_to_param"
  275. " - out of memory\n" );
  276. goto error;
  277. }
  278. memset(param,0,sizeof(struct to_param));
  279. param->type=GENERAL_PARAM;
  280. status = S_PARA_NAME;
  281. /* link to free mem if not added in to_body list */
  282. newparam = param;
  283. break;
  284. case F_CRLF:
  285. case F_LF:
  286. case F_CR:
  287. /*previous=crlf and now !=' '*/
  288. goto endofheader;
  289. default:
  290. LOG( L_ERR , "ERROR: parse_to_param :"
  291. "unexpected char [%c] in status %d: <<%.*s>> .\n",
  292. *tmp,status, (int)(tmp-buffer), ZSW(buffer));
  293. goto error;
  294. }
  295. break;
  296. case 'T':
  297. case 't' :
  298. switch (status)
  299. {
  300. case PARA_VALUE_QUOTED:
  301. case PARA_VALUE_TOKEN:
  302. case PARA_NAME:
  303. break;
  304. case S_PARA_NAME:
  305. param->name.s = tmp;
  306. status = TAG1;
  307. break;
  308. case S_PARA_VALUE:
  309. param->value.s = tmp;
  310. status = PARA_VALUE_TOKEN;
  311. break;
  312. case TAG1:
  313. case TAG2:
  314. case TAG3:
  315. status = PARA_NAME;
  316. break;
  317. case F_CRLF:
  318. case F_LF:
  319. case F_CR:
  320. /*previous=crlf and now !=' '*/
  321. goto endofheader;
  322. default:
  323. LOG( L_ERR , "ERROR: parse_to_param :"
  324. "unexpected char [%c] in status %d: <<%.*s>> .\n",
  325. *tmp,status, (int)(tmp-buffer), ZSW(buffer));
  326. goto error;
  327. }
  328. break;
  329. case 'A':
  330. case 'a' :
  331. switch (status)
  332. {
  333. case PARA_VALUE_QUOTED:
  334. case PARA_VALUE_TOKEN:
  335. case PARA_NAME:
  336. break;
  337. case S_PARA_NAME:
  338. param->name.s = tmp;
  339. status = PARA_NAME;
  340. break;
  341. case S_PARA_VALUE:
  342. param->value.s = tmp;
  343. status = PARA_VALUE_TOKEN;
  344. break;
  345. case TAG1:
  346. status = TAG2;
  347. break;
  348. case TAG2:
  349. case TAG3:
  350. status = PARA_NAME;
  351. break;
  352. case F_CRLF:
  353. case F_LF:
  354. case F_CR:
  355. /*previous=crlf and now !=' '*/
  356. goto endofheader;
  357. default:
  358. LOG( L_ERR , "ERROR: parse_to_param : "
  359. "unexpected char [%c] in status %d: <<%.*s>> .\n",
  360. *tmp,status, (int)(tmp-buffer), ZSW(buffer));
  361. goto error;
  362. }
  363. break;
  364. case 'G':
  365. case 'g' :
  366. switch (status)
  367. {
  368. case PARA_VALUE_QUOTED:
  369. case PARA_VALUE_TOKEN:
  370. case PARA_NAME:
  371. break;
  372. case S_PARA_NAME:
  373. param->name.s = tmp;
  374. status = PARA_NAME;
  375. break;
  376. case S_PARA_VALUE:
  377. param->value.s = tmp;
  378. status = PARA_VALUE_TOKEN;
  379. break;
  380. case TAG1:
  381. case TAG3:
  382. status = PARA_NAME;
  383. break;
  384. case TAG2:
  385. status = TAG3;
  386. break;
  387. case F_CRLF:
  388. case F_LF:
  389. case F_CR:
  390. /*previous=crlf and now !=' '*/
  391. goto endofheader;
  392. default:
  393. LOG( L_ERR , "ERROR: parse_to_param : "
  394. "unexpected char [%c] in status %d: <<%.*s>> .\n",
  395. *tmp,status, (int)(tmp-buffer), ZSW(buffer));
  396. goto error;
  397. }
  398. break;
  399. case '=':
  400. switch (status)
  401. {
  402. case PARA_VALUE_QUOTED:
  403. break;
  404. case TAG3:
  405. param->type=TAG_PARAM;
  406. case PARA_NAME:
  407. case TAG1:
  408. case TAG2:
  409. param->name.len = tmp-param->name.s;
  410. status = S_PARA_VALUE;
  411. break;
  412. case S_EQUAL:
  413. status = S_PARA_VALUE;
  414. break;
  415. case F_CRLF:
  416. case F_LF:
  417. case F_CR:
  418. /*previous=crlf and now !=' '*/
  419. goto endofheader;
  420. default:
  421. LOG( L_ERR , "ERROR: parse_to_param : "
  422. "unexpected char [%c] in status %d: <<%.*s>> .\n",
  423. *tmp,status, (int)(tmp-buffer), ZSW(buffer));
  424. goto error;
  425. }
  426. break;
  427. default:
  428. switch (status)
  429. {
  430. case TAG1:
  431. case TAG2:
  432. case TAG3:
  433. status = PARA_NAME;
  434. break;
  435. case PARA_VALUE_TOKEN:
  436. case PARA_NAME:
  437. case PARA_VALUE_QUOTED:
  438. break;
  439. case S_PARA_NAME:
  440. param->name.s = tmp;
  441. status = PARA_NAME;
  442. break;
  443. case S_PARA_VALUE:
  444. param->value.s = tmp;
  445. status = PARA_VALUE_TOKEN;
  446. break;
  447. case F_CRLF:
  448. case F_LF:
  449. case F_CR:
  450. /*previous=crlf and now !=' '*/
  451. goto endofheader;
  452. default:
  453. LOG(L_ERR, "ERROR: parse_to_param: "
  454. "spitting out [%c] in status %d\n",*tmp,status );
  455. goto error;
  456. }
  457. }/*switch*/
  458. }/*for*/
  459. if (!(status==F_CR || status==F_LF || status==F_CRLF))
  460. saved_status=status;
  461. endofheader:
  462. switch(saved_status){
  463. case TAG3:
  464. param->type = TAG_PARAM; /* tag at the end */
  465. /* no break */
  466. case PARA_NAME:
  467. case TAG1:
  468. case TAG2:
  469. param->name.len = tmp-param->name.s;
  470. /* no break */
  471. case S_EQUAL:
  472. /* parameter without '=', e.g. foo */
  473. param->value.s=0;
  474. param->value.len=0;
  475. add_param(param, to_b, newparam);
  476. saved_status=E_PARA_VALUE;
  477. break;
  478. case S_PARA_VALUE:
  479. /* parameter with null value, e.g. foo= */
  480. param->value.s=tmp;
  481. param->value.len=0;
  482. add_param(param, to_b, newparam);
  483. saved_status=E_PARA_VALUE;
  484. break;
  485. case PARA_VALUE_TOKEN:
  486. param->value.len=tmp-param->value.s;
  487. add_param(param, to_b, newparam);
  488. saved_status=E_PARA_VALUE;
  489. break;
  490. case E_PARA_VALUE:
  491. break;
  492. default:
  493. LOG( L_ERR , "ERROR: parse_to_param : unexpected end of header,"
  494. " status %d: <<%.*s>> .\n",
  495. saved_status, (int)(tmp-buffer), ZSW(buffer));
  496. goto error;
  497. }
  498. *returned_status=saved_status;
  499. return tmp;
  500. error:
  501. if (newparam) pkg_free(newparam);
  502. to_b->error=PARSE_ERROR;
  503. *returned_status = status;
  504. return tmp;
  505. }
  506. char* parse_to(char* buffer, char *end, struct to_body *to_b)
  507. {
  508. int status;
  509. int saved_status;
  510. char *tmp,*foo;
  511. saved_status=START_TO; /* fixes gcc 4.x warning */
  512. status=START_TO;
  513. memset(to_b, 0, sizeof(struct to_body));
  514. to_b->error=PARSE_OK;
  515. foo=0;
  516. for( tmp=buffer; tmp<end; tmp++)
  517. {
  518. switch(*tmp)
  519. {
  520. case ' ':
  521. case '\t':
  522. switch (status)
  523. {
  524. case F_CRLF:
  525. case F_LF:
  526. case F_CR:
  527. /*previous=crlf and now =' '*/
  528. status=saved_status;
  529. break;
  530. case URI_ENCLOSED:
  531. to_b->uri.len = tmp - to_b->uri.s;
  532. status = E_URI_ENCLOSED;
  533. break;
  534. case URI_OR_TOKEN:
  535. foo = tmp;
  536. status = MAYBE_URI_END;
  537. break;
  538. case DISPLAY_TOKEN:
  539. foo = tmp;
  540. status = DISPLAY_TOKEN_SP;
  541. break;
  542. }
  543. break;
  544. case '\n':
  545. switch (status)
  546. {
  547. case URI_OR_TOKEN:
  548. foo = tmp;
  549. status = MAYBE_URI_END;
  550. case MAYBE_URI_END:
  551. case DISPLAY_TOKEN_SP:
  552. case E_DISPLAY_QUOTED:
  553. case END:
  554. saved_status=status;
  555. status=F_LF;
  556. break;
  557. case DISPLAY_TOKEN:
  558. foo=tmp;
  559. saved_status=DISPLAY_TOKEN_SP;
  560. status=F_LF;
  561. break;
  562. case F_CR:
  563. status=F_CRLF;
  564. break;
  565. case F_CRLF:
  566. case F_LF:
  567. status=saved_status;
  568. goto endofheader;
  569. default:
  570. LOG( L_ERR , "ERROR: parse_to : unexpected char [%c] "
  571. "in status %d: <<%.*s>> .\n",
  572. *tmp,status, (int)(tmp-buffer), ZSW(buffer));
  573. goto error;
  574. }
  575. break;
  576. case '\r':
  577. switch (status)
  578. {
  579. case URI_OR_TOKEN:
  580. foo = tmp;
  581. status = MAYBE_URI_END;
  582. case MAYBE_URI_END:
  583. case DISPLAY_TOKEN_SP:
  584. case E_DISPLAY_QUOTED:
  585. case END:
  586. saved_status=status;
  587. status=F_CR;
  588. break;
  589. case DISPLAY_TOKEN:
  590. foo=tmp;
  591. saved_status=DISPLAY_TOKEN_SP;
  592. status=F_CR;
  593. break;
  594. case F_CRLF:
  595. case F_CR:
  596. case F_LF:
  597. status=saved_status;
  598. goto endofheader;
  599. default:
  600. LOG( L_ERR , "ERROR: parse_to : unexpected char [%c] "
  601. "in status %d: <<%.*s>> .\n",
  602. *tmp,status, (int)(tmp-buffer), ZSW(buffer));
  603. goto error;
  604. }
  605. break;
  606. case 0:
  607. switch (status)
  608. {
  609. case URI_OR_TOKEN:
  610. case MAYBE_URI_END:
  611. to_b->uri.len = tmp - to_b->uri.s;
  612. case END:
  613. saved_status = status = END;
  614. goto endofheader;
  615. default:
  616. LOG( L_ERR , "ERROR: parse_to : unexpected char [%c] "
  617. "in status %d: <<%.*s>> .\n",
  618. *tmp,status, (int)(tmp-buffer), ZSW(buffer));
  619. goto error;
  620. }
  621. break;
  622. case '\\':
  623. switch (status)
  624. {
  625. case DISPLAY_QUOTED:
  626. tmp++; /* jump over next char */
  627. break;
  628. default:
  629. LOG( L_ERR , "ERROR: parse_to : unexpected char [%c] "
  630. "in status %d: <<%.*s>> .\n",
  631. *tmp,status, (int)(tmp-buffer), ZSW(buffer));
  632. goto error;
  633. }
  634. break;
  635. case '<':
  636. switch (status)
  637. {
  638. case START_TO:
  639. to_b->body.s=tmp;
  640. status = S_URI_ENCLOSED;
  641. break;
  642. case DISPLAY_QUOTED:
  643. break;
  644. case E_DISPLAY_QUOTED:
  645. status = S_URI_ENCLOSED;
  646. break;
  647. case URI_OR_TOKEN:
  648. case DISPLAY_TOKEN:
  649. to_b->display.len=tmp-to_b->display.s;
  650. status = S_URI_ENCLOSED;
  651. break;
  652. case DISPLAY_TOKEN_SP:
  653. case MAYBE_URI_END:
  654. to_b->display.len=foo-to_b->display.s;
  655. status = S_URI_ENCLOSED;
  656. break;
  657. case F_CRLF:
  658. case F_LF:
  659. case F_CR:
  660. /*previous=crlf and now !=' '*/
  661. goto endofheader;
  662. default:
  663. LOG( L_ERR , "ERROR: parse_to : unexpected char [%c] "
  664. "in status %d: <<%.*s>> .\n",
  665. *tmp,status, (int)(tmp-buffer), ZSW(buffer));
  666. goto error;
  667. }
  668. break;
  669. case '>':
  670. switch (status)
  671. {
  672. case DISPLAY_QUOTED:
  673. break;
  674. case URI_ENCLOSED:
  675. to_b->uri.len = tmp - to_b->uri.s;
  676. case E_URI_ENCLOSED:
  677. status = END;
  678. foo = 0;
  679. break;
  680. case F_CRLF:
  681. case F_LF:
  682. case F_CR:
  683. /*previous=crlf and now !=' '*/
  684. goto endofheader;
  685. default:
  686. LOG( L_ERR , "ERROR: parse_to : unexpected char [%c] "
  687. "in status %d: <<%.*s>> .\n",
  688. *tmp,status, (int)(tmp-buffer), ZSW(buffer));
  689. goto error;
  690. }
  691. break;
  692. case '"':
  693. switch (status)
  694. {
  695. case START_TO:
  696. to_b->body.s = tmp;
  697. to_b->display.s = tmp;
  698. status = DISPLAY_QUOTED;
  699. break;
  700. case DISPLAY_QUOTED:
  701. status = E_DISPLAY_QUOTED;
  702. to_b->display.len = tmp-to_b->display.s+1;
  703. break;
  704. case F_CRLF:
  705. case F_LF:
  706. case F_CR:
  707. /*previous=crlf and now !=' '*/
  708. goto endofheader;
  709. default:
  710. LOG( L_ERR , "ERROR: parse_to : unexpected char [%c] "
  711. "in status %d: <<%.*s>> .\n",
  712. *tmp,status, (int)(tmp-buffer), buffer);
  713. goto error;
  714. }
  715. break;
  716. case ';' :
  717. switch (status)
  718. {
  719. case DISPLAY_QUOTED:
  720. case URI_ENCLOSED:
  721. break;
  722. case URI_OR_TOKEN:
  723. foo = tmp;
  724. case MAYBE_URI_END:
  725. to_b->uri.len = foo - to_b->uri.s;
  726. case END:
  727. to_b->body.len = tmp-to_b->body.s;
  728. tmp = parse_to_param(tmp,end,to_b,&saved_status);
  729. goto endofheader;
  730. case F_CRLF:
  731. case F_LF:
  732. case F_CR:
  733. /*previous=crlf and now !=' '*/
  734. goto endofheader;
  735. default:
  736. LOG( L_ERR , "ERROR: parse_to : unexpected char [%c] "
  737. "in status %d: <<%.*s>> .\n",
  738. *tmp,status, (int)(tmp-buffer), buffer);
  739. goto error;
  740. }
  741. break;
  742. default:
  743. switch (status)
  744. {
  745. case START_TO:
  746. to_b->uri.s = to_b->body.s = tmp;
  747. status = URI_OR_TOKEN;
  748. to_b->display.s=tmp;
  749. break;
  750. case S_URI_ENCLOSED:
  751. to_b->uri.s=tmp;
  752. status=URI_ENCLOSED;
  753. break;
  754. case MAYBE_URI_END:
  755. case DISPLAY_TOKEN_SP:
  756. status = DISPLAY_TOKEN;
  757. case DISPLAY_QUOTED:
  758. case DISPLAY_TOKEN:
  759. case URI_ENCLOSED:
  760. case URI_OR_TOKEN:
  761. break;
  762. case F_CRLF:
  763. case F_LF:
  764. case F_CR:
  765. /*previous=crlf and now !=' '*/
  766. goto endofheader;
  767. default:
  768. DBG("DEBUG:parse_to: spitting out [%c] in status %d\n",
  769. *tmp,status );
  770. goto error;
  771. }
  772. }/*char switch*/
  773. }/*for*/
  774. endofheader:
  775. if (to_b->display.len==0) to_b->display.s=0;
  776. status=saved_status;
  777. DBG("end of header reached, state=%d\n", status);
  778. /* check if error*/
  779. switch(status){
  780. case MAYBE_URI_END:
  781. to_b->uri.len = foo - to_b->uri.s;
  782. case END:
  783. to_b->body.len = tmp - to_b->body.s;
  784. case E_PARA_VALUE:
  785. break;
  786. default:
  787. LOG(L_ERR, "ERROR: parse_to: invalid To - unexpected "
  788. "end of header in state %d\n", status);
  789. goto error;
  790. }
  791. return tmp;
  792. error:
  793. to_b->error=PARSE_ERROR;
  794. return tmp;
  795. }
  796. void free_to_params(struct to_body* tb)
  797. {
  798. struct to_param *tp=tb->param_lst;
  799. struct to_param *foo;
  800. while (tp){
  801. foo = tp->next;
  802. pkg_free(tp);
  803. tp=foo;
  804. }
  805. }
  806. void free_to(struct to_body* tb)
  807. {
  808. free_to_params(tb);
  809. pkg_free(tb);
  810. }
  811. int parse_to_header(struct sip_msg *msg)
  812. {
  813. if ( !msg->to && ( parse_headers(msg,HDR_TO_F,0)==-1 || !msg->to)) {
  814. ERR("bad msg or missing TO header\n");
  815. return -1;
  816. }
  817. // HDR_TO_T is automatically parsed (get_hdr_field in parser/msg_parser.c)
  818. // so check only ptr validity
  819. if (msg->to->parsed)
  820. return 0;
  821. else
  822. return -1;
  823. }
  824. sip_uri_t *parse_to_uri(sip_msg_t *msg)
  825. {
  826. to_body_t *tb = NULL;
  827. if(msg==NULL)
  828. return NULL;
  829. if(parse_to_header(msg)<0)
  830. {
  831. LM_ERR("cannot parse TO header\n");
  832. return NULL;
  833. }
  834. if(msg->to==NULL || get_to(msg)==NULL)
  835. return NULL;
  836. tb = get_to(msg);
  837. if(tb->parsed_uri.user.s!=NULL || tb->parsed_uri.host.s!=NULL)
  838. return &tb->parsed_uri;
  839. if (parse_uri(tb->uri.s, tb->uri.len , &tb->parsed_uri)<0)
  840. {
  841. LM_ERR("failed to parse To uri\n");
  842. memset(&tb->parsed_uri, 0, sizeof(struct sip_uri));
  843. return NULL;
  844. }
  845. return &tb->parsed_uri;
  846. }