parse_param.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. /*
  2. * Generic Parameter 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. * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. * History:
  23. * -------
  24. * 2003-03-24 Created by janakj
  25. * 2003-04-07 shm duplication added (janakj)
  26. * 2003-04-07 URI class added (janakj)
  27. */
  28. /*! \file
  29. * \brief Parser :: Generic Parameter Parser
  30. *
  31. * \ingroup parser
  32. */
  33. #include <string.h>
  34. #include "../str.h"
  35. #include "../ut.h"
  36. #include "../dprint.h"
  37. #include "../trim.h"
  38. #include "../mem/mem.h"
  39. #include "../mem/shm_mem.h"
  40. #include "parse_param.h"
  41. static inline void parse_event_dialog_class(param_hooks_t* h, param_t* p)
  42. {
  43. if (!p->name.s) {
  44. LOG(L_ERR, "ERROR: parse_event_dialog_class: empty value\n");
  45. return;
  46. }
  47. if (!h) {
  48. LOG(L_CRIT, "BUG: parse_event_dialog_class: NULL param hook pointer\n");
  49. return;
  50. }
  51. switch(p->name.s[0]) {
  52. case 'c':
  53. case 'C':
  54. if ((p->name.len == 7) &&
  55. (!strncasecmp(p->name.s + 1, "all-id", 6))) {
  56. p->type = P_CALL_ID;
  57. h->event_dialog.call_id = p;
  58. }
  59. break;
  60. case 'f':
  61. case 'F':
  62. if ((p->name.len == 8) &&
  63. (!strncasecmp(p->name.s + 1, "rom-tag", 7))) {
  64. p->type = P_FROM_TAG;
  65. h->event_dialog.from_tag = p;
  66. }
  67. break;
  68. case 't':
  69. case 'T':
  70. if ((p->name.len == 6) &&
  71. (!strncasecmp(p->name.s + 1, "o-tag", 5))) {
  72. p->type = P_TO_TAG;
  73. h->event_dialog.to_tag = p;
  74. }
  75. break;
  76. case 'i':
  77. case 'I':
  78. if ((p->name.len == 27) &&
  79. (!strncasecmp(p->name.s + 1, "nclude-session-description", 26))) {
  80. p->type = P_ISD;
  81. h->event_dialog.include_session_description = p;
  82. }
  83. break;
  84. case 's':
  85. case 'S':
  86. if ((p->name.len == 3) &&
  87. (!strncasecmp(p->name.s + 1, "la", 2))) {
  88. p->type = P_SLA;
  89. h->event_dialog.sla = p;
  90. }
  91. break;
  92. case 'm':
  93. case 'M':
  94. if ((p->name.len == 2) &&
  95. (!strncasecmp(p->name.s + 1, "a", 1))) {
  96. p->type = P_MA;
  97. h->event_dialog.ma = p;
  98. }
  99. break;
  100. }
  101. }
  102. /*! \brief
  103. * Try to find out parameter name, recognized parameters
  104. * are q, expires and methods
  105. */
  106. static inline void parse_contact_class(param_hooks_t* _h, param_t* _p)
  107. {
  108. if (!_p->name.s) {
  109. LOG(L_ERR, "ERROR: parse_contact_class: empty value\n");
  110. return;
  111. }
  112. if (!_h) {
  113. LOG(L_CRIT, "BUG: parse_contact_class: NULL param hook pointer\n");
  114. return;
  115. }
  116. switch(_p->name.s[0]) {
  117. case 'q':
  118. case 'Q':
  119. if (_p->name.len == 1) {
  120. _p->type = P_Q;
  121. _h->contact.q = _p;
  122. }
  123. break;
  124. case 'e':
  125. case 'E':
  126. if ((_p->name.len == 7) &&
  127. (!strncasecmp(_p->name.s + 1, "xpires", 6))) {
  128. _p->type = P_EXPIRES;
  129. _h->contact.expires = _p;
  130. }
  131. break;
  132. case 'm':
  133. case 'M':
  134. if ((_p->name.len == 7) &&
  135. (!strncasecmp(_p->name.s + 1, "ethods", 6))) {
  136. _p->type = P_METHODS;
  137. _h->contact.methods = _p;
  138. }
  139. break;
  140. case 'r':
  141. case 'R':
  142. if ((_p->name.len == 8) &&
  143. (!strncasecmp(_p->name.s + 1, "eceived", 7))) {
  144. _p->type = P_RECEIVED;
  145. _h->contact.received = _p;
  146. } else if((_p->name.len == 6) &&
  147. (!strncasecmp(_p->name.s + 1, "eg-id", 5))) {
  148. _p->type = P_REG_ID;
  149. _h->contact.reg_id = _p;
  150. }
  151. break;
  152. case '+':
  153. if ((_p->name.len == 13) &&
  154. (!strncasecmp(_p->name.s + 1, "sip.instance", 12))) {
  155. _p->type = P_INSTANCE;
  156. _h->contact.instance = _p;
  157. }
  158. break;
  159. case 'o':
  160. case 'O':
  161. if ((_p->name.len == 2) &&
  162. (!strncasecmp(_p->name.s + 1, "b", 1))) {
  163. _p->type = P_OB;
  164. _h->contact.ob = _p;
  165. }
  166. break;
  167. }
  168. }
  169. /*! \brief
  170. * Try to find out parameter name, recognized parameters
  171. * are transport, lr, r2, maddr
  172. */
  173. static inline void parse_uri_class(param_hooks_t* _h, param_t* _p)
  174. {
  175. if (!_p->name.s) {
  176. LOG(L_ERR, "ERROR: parse_uri_class: empty value\n");
  177. return;
  178. }
  179. if (!_h) {
  180. LOG(L_CRIT, "BUG: parse_uri_class: NULL param hook pointer\n");
  181. return;
  182. }
  183. switch(_p->name.s[0]) {
  184. case 't':
  185. case 'T':
  186. if ((_p->name.len == 9) &&
  187. (!strncasecmp(_p->name.s + 1, "ransport", 8))) {
  188. _p->type = P_TRANSPORT;
  189. _h->uri.transport = _p;
  190. } else if (_p->name.len == 2) {
  191. if (((_p->name.s[1] == 't') || (_p->name.s[1] == 'T')) &&
  192. ((_p->name.s[2] == 'l') || (_p->name.s[2] == 'L'))) {
  193. _p->type = P_TTL;
  194. _h->uri.ttl = _p;
  195. }
  196. }
  197. break;
  198. case 'l':
  199. case 'L':
  200. if ((_p->name.len == 2) && ((_p->name.s[1] == 'r') || (_p->name.s[1] == 'R'))) {
  201. _p->type = P_LR;
  202. _h->uri.lr = _p;
  203. }
  204. break;
  205. case 'r':
  206. case 'R':
  207. if ((_p->name.len == 2) && (_p->name.s[1] == '2')) {
  208. _p->type = P_R2;
  209. _h->uri.r2 = _p;
  210. }
  211. break;
  212. case 'm':
  213. case 'M':
  214. if ((_p->name.len == 5) &&
  215. (!strncasecmp(_p->name.s + 1, "addr", 4))) {
  216. _p->type = P_MADDR;
  217. _h->uri.maddr = _p;
  218. }
  219. break;
  220. case 'd':
  221. case 'D':
  222. if ((_p->name.len == 5) &&
  223. (!strncasecmp(_p->name.s + 1, "stip", 4))) {
  224. _p->type = P_DSTIP;
  225. _h->uri.dstip = _p;
  226. } else if ((_p->name.len == 7) &&
  227. (!strncasecmp(_p->name.s + 1, "stport", 6))) {
  228. _p->type = P_DSTPORT;
  229. _h->uri.dstport = _p;
  230. }
  231. break;
  232. case 'f':
  233. case 'F':
  234. if ((_p->name.len == 4) &&
  235. (!strncasecmp(_p->name.s + 1, "tag", 3))) {
  236. _p->type = P_FTAG;
  237. _h->uri.ftag = _p;
  238. }
  239. break;
  240. case 'o':
  241. case 'O':
  242. if ((_p->name.len == 2) &&
  243. (!strncasecmp(_p->name.s + 1, "b", 1))) {
  244. _p->type = P_OB;
  245. _h->uri.ob = _p;
  246. }
  247. break;
  248. }
  249. }
  250. /*! \brief
  251. * Parse quoted string in a parameter body
  252. * return the string without quotes in _r
  253. * parameter and update _s to point behind the
  254. * closing quote
  255. */
  256. static inline int parse_quoted_param(str* _s, str* _r)
  257. {
  258. char* end_quote;
  259. char quote;
  260. /* The string must have at least
  261. * surrounding quotes
  262. */
  263. if (_s->len < 2) {
  264. return -1;
  265. }
  266. /* Store the kind of quoting (single or double)
  267. * which we're handling with
  268. */
  269. quote = (_s->s)[0];
  270. /* Skip opening quote */
  271. _s->s++;
  272. _s->len--;
  273. /* Find closing quote */
  274. end_quote = q_memchr(_s->s, quote, _s->len);
  275. /* Not found, return error */
  276. if (!end_quote) {
  277. return -2;
  278. }
  279. /* Let _r point to the string without
  280. * surrounding quotes
  281. */
  282. _r->s = _s->s;
  283. _r->len = end_quote - _s->s;
  284. /* Update _s parameter to point
  285. * behind the closing quote
  286. */
  287. _s->len -= (end_quote - _s->s + 1);
  288. _s->s = end_quote + 1;
  289. /* Everything went OK */
  290. return 0;
  291. }
  292. /*! \brief
  293. * Parse unquoted token in a parameter body
  294. * let _r point to the token and update _s
  295. * to point right behind the token
  296. */
  297. static inline int parse_token_param(str* _s, str* _r, char separator)
  298. {
  299. int i;
  300. /* There is nothing to parse,
  301. * return error
  302. */
  303. if (_s->len == 0) {
  304. return -1;
  305. }
  306. /* Save the begining of the
  307. * token in _r->s
  308. */
  309. _r->s = _s->s;
  310. /* Iterate through the
  311. * token body
  312. */
  313. for(i = 0; i < _s->len; i++) {
  314. /* All these characters
  315. * mark end of the token
  316. */
  317. switch(_s->s[i]) {
  318. case ' ':
  319. case '\t':
  320. case '\r':
  321. case '\n':
  322. case ',':
  323. /* So if you find
  324. * any of them
  325. * stop iterating
  326. */
  327. goto out;
  328. default:
  329. if(_s->s[i] == separator)
  330. goto out;
  331. }
  332. }
  333. out:
  334. if (i == 0) {
  335. return -1;
  336. }
  337. /* Save length of the token */
  338. _r->len = i;
  339. /* Update _s parameter so it points
  340. * right behind the end of the token
  341. */
  342. _s->s = _s->s + i;
  343. _s->len -= i;
  344. /* Everything went OK */
  345. return 0;
  346. }
  347. /*! \brief
  348. * Parse a parameter name
  349. */
  350. static inline void parse_param_name(str* _s, pclass_t _c, param_hooks_t* _h, param_t* _p, char separator)
  351. {
  352. if (!_s->s) {
  353. DBG("DEBUG: parse_param_name: empty parameter\n");
  354. return;
  355. }
  356. _p->name.s = _s->s;
  357. while(_s->len) {
  358. switch(_s->s[0]) {
  359. case ' ':
  360. case '\t':
  361. case '\r':
  362. case '\n':
  363. case ',':
  364. case '=':
  365. goto out;
  366. default:
  367. if (_s->s[0] == separator)
  368. goto out;
  369. }
  370. _s->s++;
  371. _s->len--;
  372. }
  373. out:
  374. _p->name.len = _s->s - _p->name.s;
  375. switch(_c) {
  376. case CLASS_CONTACT: parse_contact_class(_h, _p); break;
  377. case CLASS_URI: parse_uri_class(_h, _p); break;
  378. case CLASS_EVENT_DIALOG: parse_event_dialog_class(_h, _p); break;
  379. default: break;
  380. }
  381. }
  382. /*! \brief
  383. * Parse body of a parameter. It can be quoted string or
  384. * a single token.
  385. */
  386. static inline int parse_param_body(str* _s, param_t* _c, char separator)
  387. {
  388. if (_s->s[0] == '\"' || _s->s[0] == '\'') {
  389. if (parse_quoted_param(_s, &(_c->body)) < 0) {
  390. LOG(L_ERR, "parse_param_body(): Error while parsing quoted string\n");
  391. return -2;
  392. }
  393. } else {
  394. if (parse_token_param(_s, &(_c->body), separator) < 0) {
  395. LOG(L_ERR, "parse_param_body(): Error while parsing token\n");
  396. return -3;
  397. }
  398. }
  399. return 0;
  400. }
  401. /*! \brief
  402. * Only parse one parameter
  403. * Returns:
  404. * t: out parameter
  405. * -1: on error
  406. * 0: success, but expect a next paramter
  407. * 1: success and exepect no more parameters
  408. */
  409. static inline int parse_param2(str *_s, pclass_t _c, param_hooks_t *_h, param_t *t, char separator)
  410. {
  411. memset(t, 0, sizeof(param_t));
  412. parse_param_name(_s, _c, _h, t, separator);
  413. trim_leading(_s);
  414. if (_s->len == 0) { /* The last parameter without body */
  415. t->len = t->name.len;
  416. goto ok;
  417. }
  418. if (_s->s[0] == '=') {
  419. _s->s++;
  420. _s->len--;
  421. trim_leading(_s);
  422. if (_s->len == 0) {
  423. /* Be forgiving and accept parameters with missing value,
  424. * we just set the length of parameter body to 0. */
  425. t->body.s = _s->s;
  426. t->body.len = 0;
  427. } else if (parse_param_body(_s, t, separator) < 0) {
  428. LOG(L_ERR, "parse_params(): Error while parsing param body\n");
  429. goto error;
  430. }
  431. t->len = _s->s - t->name.s;
  432. trim_leading(_s);
  433. if (_s->len == 0) {
  434. goto ok;
  435. }
  436. } else {
  437. t->len = t->name.len;
  438. }
  439. if (_s->s[0] == ',') goto ok; /* To be able to parse header parameters */
  440. if (_s->s[0] == '>') goto ok; /* To be able to parse URI parameters */
  441. if (_s->s[0] != separator) {
  442. LOG(L_ERR, "parse_params(): Invalid character, %c expected\n",
  443. separator);
  444. goto error;
  445. }
  446. _s->s++;
  447. _s->len--;
  448. trim_leading(_s);
  449. if (_s->len == 0) {
  450. LOG(L_ERR, "parse_params(): Param name missing after %c\n",
  451. separator);
  452. goto error;
  453. }
  454. return 0; /* expect more params */
  455. ok:
  456. return 1; /* done with parsing for params */
  457. error:
  458. return -1;
  459. }
  460. /*! \brief
  461. * Only parse one parameter
  462. * Returns:
  463. * t: out parameter
  464. * -1: on error
  465. * 0: success, but expect a next paramter
  466. * 1: success and exepect no more parameters
  467. */
  468. inline int parse_param(str *_s, pclass_t _c, param_hooks_t *_h, param_t *t)
  469. {
  470. return parse_param2(_s, _c, _h, t, ';');
  471. }
  472. /*! \brief
  473. * Parse parameters
  474. * \param _s is string containing parameters, it will be updated to point behind the parameters
  475. * \param _c is class of parameters
  476. * \param _h is pointer to structure that will be filled with pointer to well known parameters
  477. * \param _p pointing to linked list where parsed parameters will be stored
  478. * \return 0 on success and negative number on an error
  479. */
  480. int parse_params(str* _s, pclass_t _c, param_hooks_t* _h, param_t** _p)
  481. {
  482. return parse_params2(_s, _c, _h, _p, ';');
  483. }
  484. /*! \brief
  485. * Parse parameters with configurable separator
  486. * \param _s is string containing parameters, it will be updated to point behind the parameters
  487. * \param _c is class of parameters
  488. * \param _h is pointer to structure that will be filled with pointer to well known parameters
  489. * \param _p pointing to linked list where parsed parameters will be stored
  490. * \param separator single character separator
  491. * \return 0 on success and negative number on an error
  492. */
  493. int parse_params2(str* _s, pclass_t _c, param_hooks_t* _h, param_t** _p,
  494. char separator)
  495. {
  496. param_t* t;
  497. if (!_s || !_p) {
  498. LOG(L_ERR, "parse_params(): Invalid parameter value\n");
  499. return -1;
  500. }
  501. if (_h)
  502. memset(_h, 0, sizeof(param_hooks_t));
  503. *_p = 0;
  504. if (!_s->s) { /* no parameters at all -- we're done */
  505. DBG("DEBUG: parse_params: empty uri params, skipping\n");
  506. return 0;
  507. }
  508. while(1) {
  509. t = (param_t*)pkg_malloc(sizeof(param_t));
  510. if (t == 0) {
  511. LOG(L_ERR, "parse_params(): No memory left\n");
  512. goto error;
  513. }
  514. switch(parse_param2(_s, _c, _h, t, separator)) {
  515. case 0: break;
  516. case 1: goto ok;
  517. default: goto error;
  518. }
  519. t->next = *_p;
  520. *_p = t;
  521. }
  522. error:
  523. if (t) pkg_free(t);
  524. free_params(*_p);
  525. *_p = 0;
  526. return -2;
  527. ok:
  528. t->next = *_p;
  529. *_p = t;
  530. return 0;
  531. }
  532. /*! \brief
  533. * Free linked list of parameters
  534. */
  535. static inline void do_free_params(param_t* _p, int _shm)
  536. {
  537. param_t* ptr;
  538. while(_p) {
  539. ptr = _p;
  540. _p = _p->next;
  541. if (_shm) shm_free(ptr);
  542. else pkg_free(ptr);
  543. }
  544. }
  545. /*! \brief
  546. * Free linked list of parameters
  547. */
  548. void free_params(param_t* _p)
  549. {
  550. do_free_params(_p, 0);
  551. }
  552. /*! \brief
  553. * Free linked list of parameters
  554. */
  555. void shm_free_params(param_t* _p)
  556. {
  557. do_free_params(_p, 1);
  558. }
  559. /*! \brief
  560. * Print a parameter structure, just for debugging
  561. */
  562. static inline void print_param(FILE* _o, param_t* _p)
  563. {
  564. char* type;
  565. fprintf(_o, "---param(%p)---\n", _p);
  566. switch(_p->type) {
  567. case P_OTHER: type = "P_OTHER"; break;
  568. case P_Q: type = "P_Q"; break;
  569. case P_EXPIRES: type = "P_EXPIRES"; break;
  570. case P_METHODS: type = "P_METHODS"; break;
  571. case P_TRANSPORT: type = "P_TRANSPORT"; break;
  572. case P_LR: type = "P_LR"; break;
  573. case P_R2: type = "P_R2"; break;
  574. case P_MADDR: type = "P_MADDR"; break;
  575. case P_TTL: type = "P_TTL"; break;
  576. case P_RECEIVED: type = "P_RECEIVED"; break;
  577. case P_DSTIP: type = "P_DSTIP"; break;
  578. case P_DSTPORT: type = "P_DSTPORT"; break;
  579. case P_INSTANCE: type = "P_INSTANCE"; break;
  580. case P_FTAG: type = "P_FTAG"; break;
  581. case P_CALL_ID: type = "P_CALL_ID"; break;
  582. case P_FROM_TAG: type = "P_FROM_TAG"; break;
  583. case P_TO_TAG: type = "P_TO_TAG"; break;
  584. case P_ISD: type = "P_ISD"; break;
  585. case P_SLA: type = "P_SLA"; break;
  586. default: type = "UNKNOWN"; break;
  587. }
  588. fprintf(_o, "type: %s\n", type);
  589. fprintf(_o, "name: \'%.*s\'\n", _p->name.len, _p->name.s);
  590. fprintf(_o, "body: \'%.*s\'\n", _p->body.len, _p->body.s);
  591. fprintf(_o, "len : %d\n", _p->len);
  592. fprintf(_o, "---/param---\n");
  593. }
  594. /*! \brief
  595. * Print linked list of parameters, just for debugging
  596. */
  597. void print_params(FILE* _o, param_t* _p)
  598. {
  599. param_t* ptr;
  600. ptr = _p;
  601. while(ptr) {
  602. print_param(_o, ptr);
  603. ptr = ptr->next;
  604. }
  605. }
  606. /*! \brief
  607. * Duplicate linked list of parameters
  608. */
  609. static inline int do_duplicate_params(param_t** _n, param_t* _p, int _shm)
  610. {
  611. param_t* last, *ptr, *t;
  612. if (!_n) {
  613. LOG(L_ERR, "duplicate_params(): Invalid parameter value\n");
  614. return -1;
  615. }
  616. last = 0;
  617. *_n = 0;
  618. ptr = _p;
  619. while(ptr) {
  620. if (_shm) {
  621. t = (param_t*)shm_malloc(sizeof(param_t));
  622. } else {
  623. t = (param_t*)pkg_malloc(sizeof(param_t));
  624. }
  625. if (!t) {
  626. LOG(L_ERR, "duplicate_params(): Invalid parameter value\n");
  627. goto err;
  628. }
  629. memcpy(t, ptr, sizeof(param_t));
  630. t->next = 0;
  631. if (!*_n) *_n = t;
  632. if (last) last->next = t;
  633. last = t;
  634. ptr = ptr->next;
  635. }
  636. return 0;
  637. err:
  638. do_free_params(*_n, _shm);
  639. return -2;
  640. }
  641. /*! \brief
  642. * Duplicate linked list of parameters
  643. */
  644. int duplicate_params(param_t** _n, param_t* _p)
  645. {
  646. return do_duplicate_params(_n, _p, 0);
  647. }
  648. /*! \brief
  649. * Duplicate linked list of parameters
  650. */
  651. int shm_duplicate_params(param_t** _n, param_t* _p)
  652. {
  653. return do_duplicate_params(_n, _p, 1);
  654. }