parse_param.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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)
  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. case ';':
  324. /* So if you find
  325. * any of them
  326. * stop iterating
  327. */
  328. goto out;
  329. }
  330. }
  331. out:
  332. if (i == 0) {
  333. return -1;
  334. }
  335. /* Save length of the token */
  336. _r->len = i;
  337. /* Update _s parameter so it points
  338. * right behind the end of the token
  339. */
  340. _s->s = _s->s + i;
  341. _s->len -= i;
  342. /* Everything went OK */
  343. return 0;
  344. }
  345. /*! \brief
  346. * Parse a parameter name
  347. */
  348. static inline void parse_param_name(str* _s, pclass_t _c, param_hooks_t* _h, param_t* _p)
  349. {
  350. if (!_s->s) {
  351. DBG("DEBUG: parse_param_name: empty parameter\n");
  352. return;
  353. }
  354. _p->name.s = _s->s;
  355. while(_s->len) {
  356. switch(_s->s[0]) {
  357. case ' ':
  358. case '\t':
  359. case '\r':
  360. case '\n':
  361. case ';':
  362. case ',':
  363. case '=':
  364. goto out;
  365. }
  366. _s->s++;
  367. _s->len--;
  368. }
  369. out:
  370. _p->name.len = _s->s - _p->name.s;
  371. switch(_c) {
  372. case CLASS_CONTACT: parse_contact_class(_h, _p); break;
  373. case CLASS_URI: parse_uri_class(_h, _p); break;
  374. case CLASS_EVENT_DIALOG: parse_event_dialog_class(_h, _p); break;
  375. default: break;
  376. }
  377. }
  378. /*! \brief
  379. * Parse body of a parameter. It can be quoted string or
  380. * a single token.
  381. */
  382. static inline int parse_param_body(str* _s, param_t* _c)
  383. {
  384. if (_s->s[0] == '\"' || _s->s[0] == '\'') {
  385. if (parse_quoted_param(_s, &(_c->body)) < 0) {
  386. LOG(L_ERR, "parse_param_body(): Error while parsing quoted string\n");
  387. return -2;
  388. }
  389. } else {
  390. if (parse_token_param(_s, &(_c->body)) < 0) {
  391. LOG(L_ERR, "parse_param_body(): Error while parsing token\n");
  392. return -3;
  393. }
  394. }
  395. return 0;
  396. }
  397. /*! \brief
  398. * Only parse one parameter
  399. * Returns:
  400. * t: out parameter
  401. * -1: on error
  402. * 0: success, but expect a next paramter
  403. * 1: success and exepect no more parameters
  404. */
  405. inline int parse_param(str *_s, pclass_t _c, param_hooks_t *_h, param_t *t)
  406. {
  407. memset(t, 0, sizeof(param_t));
  408. parse_param_name(_s, _c, _h, t);
  409. trim_leading(_s);
  410. if (_s->len == 0) { /* The last parameter without body */
  411. t->len = t->name.len;
  412. goto ok;
  413. }
  414. if (_s->s[0] == '=') {
  415. _s->s++;
  416. _s->len--;
  417. trim_leading(_s);
  418. if (_s->len == 0) {
  419. /* Be forgiving and accept parameters with missing value,
  420. * we just set the length of parameter body to 0. */
  421. t->body.s = _s->s;
  422. t->body.len = 0;
  423. } else if (parse_param_body(_s, t) < 0) {
  424. LOG(L_ERR, "parse_params(): Error while parsing param body\n");
  425. goto error;
  426. }
  427. t->len = _s->s - t->name.s;
  428. trim_leading(_s);
  429. if (_s->len == 0) {
  430. goto ok;
  431. }
  432. } else {
  433. t->len = t->name.len;
  434. }
  435. if (_s->s[0] == ',') goto ok; /* To be able to parse header parameters */
  436. if (_s->s[0] == '>') goto ok; /* To be able to parse URI parameters */
  437. if (_s->s[0] != ';') {
  438. LOG(L_ERR, "parse_params(): Invalid character, ; expected\n");
  439. goto error;
  440. }
  441. _s->s++;
  442. _s->len--;
  443. trim_leading(_s);
  444. if (_s->len == 0) {
  445. LOG(L_ERR, "parse_params(): Param name missing after ;\n");
  446. goto error;
  447. }
  448. return 0; /* expect more params */
  449. ok:
  450. return 1; /* done with parsing for params */
  451. error:
  452. return -1;
  453. }
  454. /*! \brief
  455. * Parse parameters
  456. * \param _s is string containing parameters, it will be updated to point behind the parameters
  457. * \param _c is class of parameters
  458. * \param _h is pointer to structure that will be filled with pointer to well known parameters
  459. * \param _p pointing to linked list where parsed parameters will be stored
  460. * \return 0 on success and negative number on an error
  461. */
  462. int parse_params(str* _s, pclass_t _c, param_hooks_t* _h, param_t** _p)
  463. {
  464. param_t* t;
  465. if (!_s || !_p) {
  466. LOG(L_ERR, "parse_params(): Invalid parameter value\n");
  467. return -1;
  468. }
  469. if (_h)
  470. memset(_h, 0, sizeof(param_hooks_t));
  471. *_p = 0;
  472. if (!_s->s) { /* no parameters at all -- we're done */
  473. DBG("DEBUG: parse_params: empty uri params, skipping\n");
  474. return 0;
  475. }
  476. while(1) {
  477. t = (param_t*)pkg_malloc(sizeof(param_t));
  478. if (t == 0) {
  479. LOG(L_ERR, "parse_params(): No memory left\n");
  480. goto error;
  481. }
  482. switch(parse_param(_s, _c, _h, t)) {
  483. case 0: break;
  484. case 1: goto ok;
  485. default: goto error;
  486. }
  487. t->next = *_p;
  488. *_p = t;
  489. }
  490. error:
  491. if (t) pkg_free(t);
  492. free_params(*_p);
  493. *_p = 0;
  494. return -2;
  495. ok:
  496. t->next = *_p;
  497. *_p = t;
  498. return 0;
  499. }
  500. /*! \brief
  501. * Free linked list of parameters
  502. */
  503. static inline void do_free_params(param_t* _p, int _shm)
  504. {
  505. param_t* ptr;
  506. while(_p) {
  507. ptr = _p;
  508. _p = _p->next;
  509. if (_shm) shm_free(ptr);
  510. else pkg_free(ptr);
  511. }
  512. }
  513. /*! \brief
  514. * Free linked list of parameters
  515. */
  516. void free_params(param_t* _p)
  517. {
  518. do_free_params(_p, 0);
  519. }
  520. /*! \brief
  521. * Free linked list of parameters
  522. */
  523. void shm_free_params(param_t* _p)
  524. {
  525. do_free_params(_p, 1);
  526. }
  527. /*! \brief
  528. * Print a parameter structure, just for debugging
  529. */
  530. static inline void print_param(FILE* _o, param_t* _p)
  531. {
  532. char* type;
  533. fprintf(_o, "---param(%p)---\n", _p);
  534. switch(_p->type) {
  535. case P_OTHER: type = "P_OTHER"; break;
  536. case P_Q: type = "P_Q"; break;
  537. case P_EXPIRES: type = "P_EXPIRES"; break;
  538. case P_METHODS: type = "P_METHODS"; break;
  539. case P_TRANSPORT: type = "P_TRANSPORT"; break;
  540. case P_LR: type = "P_LR"; break;
  541. case P_R2: type = "P_R2"; break;
  542. case P_MADDR: type = "P_MADDR"; break;
  543. case P_TTL: type = "P_TTL"; break;
  544. case P_RECEIVED: type = "P_RECEIVED"; break;
  545. case P_DSTIP: type = "P_DSTIP"; break;
  546. case P_DSTPORT: type = "P_DSTPORT"; break;
  547. case P_INSTANCE: type = "P_INSTANCE"; break;
  548. case P_FTAG: type = "P_FTAG"; break;
  549. case P_CALL_ID: type = "P_CALL_ID"; break;
  550. case P_FROM_TAG: type = "P_FROM_TAG"; break;
  551. case P_TO_TAG: type = "P_TO_TAG"; break;
  552. case P_ISD: type = "P_ISD"; break;
  553. case P_SLA: type = "P_SLA"; break;
  554. default: type = "UNKNOWN"; break;
  555. }
  556. fprintf(_o, "type: %s\n", type);
  557. fprintf(_o, "name: \'%.*s\'\n", _p->name.len, _p->name.s);
  558. fprintf(_o, "body: \'%.*s\'\n", _p->body.len, _p->body.s);
  559. fprintf(_o, "len : %d\n", _p->len);
  560. fprintf(_o, "---/param---\n");
  561. }
  562. /*! \brief
  563. * Print linked list of parameters, just for debugging
  564. */
  565. void print_params(FILE* _o, param_t* _p)
  566. {
  567. param_t* ptr;
  568. ptr = _p;
  569. while(ptr) {
  570. print_param(_o, ptr);
  571. ptr = ptr->next;
  572. }
  573. }
  574. /*! \brief
  575. * Duplicate linked list of parameters
  576. */
  577. static inline int do_duplicate_params(param_t** _n, param_t* _p, int _shm)
  578. {
  579. param_t* last, *ptr, *t;
  580. if (!_n) {
  581. LOG(L_ERR, "duplicate_params(): Invalid parameter value\n");
  582. return -1;
  583. }
  584. last = 0;
  585. *_n = 0;
  586. ptr = _p;
  587. while(ptr) {
  588. if (_shm) {
  589. t = (param_t*)shm_malloc(sizeof(param_t));
  590. } else {
  591. t = (param_t*)pkg_malloc(sizeof(param_t));
  592. }
  593. if (!t) {
  594. LOG(L_ERR, "duplicate_params(): Invalid parameter value\n");
  595. goto err;
  596. }
  597. memcpy(t, ptr, sizeof(param_t));
  598. t->next = 0;
  599. if (!*_n) *_n = t;
  600. if (last) last->next = t;
  601. last = t;
  602. ptr = ptr->next;
  603. }
  604. return 0;
  605. err:
  606. do_free_params(*_n, _shm);
  607. return -2;
  608. }
  609. /*! \brief
  610. * Duplicate linked list of parameters
  611. */
  612. int duplicate_params(param_t** _n, param_t* _p)
  613. {
  614. return do_duplicate_params(_n, _p, 0);
  615. }
  616. /*! \brief
  617. * Duplicate linked list of parameters
  618. */
  619. int shm_duplicate_params(param_t** _n, param_t* _p)
  620. {
  621. return do_duplicate_params(_n, _p, 1);
  622. }