parse_param.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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. }
  160. }
  161. /*! \brief
  162. * Try to find out parameter name, recognized parameters
  163. * are transport, lr, r2, maddr
  164. */
  165. static inline void parse_uri_class(param_hooks_t* _h, param_t* _p)
  166. {
  167. if (!_p->name.s) {
  168. LOG(L_ERR, "ERROR: parse_uri_class: empty value\n");
  169. return;
  170. }
  171. if (!_h) {
  172. LOG(L_CRIT, "BUG: parse_uri_class: NULL param hook pointer\n");
  173. return;
  174. }
  175. switch(_p->name.s[0]) {
  176. case 't':
  177. case 'T':
  178. if ((_p->name.len == 9) &&
  179. (!strncasecmp(_p->name.s + 1, "ransport", 8))) {
  180. _p->type = P_TRANSPORT;
  181. _h->uri.transport = _p;
  182. } else if (_p->name.len == 2) {
  183. if (((_p->name.s[1] == 't') || (_p->name.s[1] == 'T')) &&
  184. ((_p->name.s[2] == 'l') || (_p->name.s[2] == 'L'))) {
  185. _p->type = P_TTL;
  186. _h->uri.ttl = _p;
  187. }
  188. }
  189. break;
  190. case 'l':
  191. case 'L':
  192. if ((_p->name.len == 2) && ((_p->name.s[1] == 'r') || (_p->name.s[1] == 'R'))) {
  193. _p->type = P_LR;
  194. _h->uri.lr = _p;
  195. }
  196. break;
  197. case 'r':
  198. case 'R':
  199. if ((_p->name.len == 2) && (_p->name.s[1] == '2')) {
  200. _p->type = P_R2;
  201. _h->uri.r2 = _p;
  202. }
  203. break;
  204. case 'm':
  205. case 'M':
  206. if ((_p->name.len == 5) &&
  207. (!strncasecmp(_p->name.s + 1, "addr", 4))) {
  208. _p->type = P_MADDR;
  209. _h->uri.maddr = _p;
  210. }
  211. break;
  212. case 'd':
  213. case 'D':
  214. if ((_p->name.len == 5) &&
  215. (!strncasecmp(_p->name.s + 1, "stip", 4))) {
  216. _p->type = P_DSTIP;
  217. _h->uri.dstip = _p;
  218. } else if ((_p->name.len == 7) &&
  219. (!strncasecmp(_p->name.s + 1, "stport", 6))) {
  220. _p->type = P_DSTPORT;
  221. _h->uri.dstport = _p;
  222. }
  223. break;
  224. case 'f':
  225. case 'F':
  226. if ((_p->name.len == 4) &&
  227. (!strncasecmp(_p->name.s + 1, "tag", 3))) {
  228. _p->type = P_FTAG;
  229. _h->uri.ftag = _p;
  230. }
  231. break;
  232. }
  233. }
  234. /*! \brief
  235. * Parse quoted string in a parameter body
  236. * return the string without quotes in _r
  237. * parameter and update _s to point behind the
  238. * closing quote
  239. */
  240. static inline int parse_quoted_param(str* _s, str* _r)
  241. {
  242. char* end_quote;
  243. char quote;
  244. /* The string must have at least
  245. * surrounding quotes
  246. */
  247. if (_s->len < 2) {
  248. return -1;
  249. }
  250. /* Store the kind of quoting (single or double)
  251. * which we're handling with
  252. */
  253. quote = (_s->s)[0];
  254. /* Skip opening quote */
  255. _s->s++;
  256. _s->len--;
  257. /* Find closing quote */
  258. end_quote = q_memchr(_s->s, quote, _s->len);
  259. /* Not found, return error */
  260. if (!end_quote) {
  261. return -2;
  262. }
  263. /* Let _r point to the string without
  264. * surrounding quotes
  265. */
  266. _r->s = _s->s;
  267. _r->len = end_quote - _s->s;
  268. /* Update _s parameter to point
  269. * behind the closing quote
  270. */
  271. _s->len -= (end_quote - _s->s + 1);
  272. _s->s = end_quote + 1;
  273. /* Everything went OK */
  274. return 0;
  275. }
  276. /*! \brief
  277. * Parse unquoted token in a parameter body
  278. * let _r point to the token and update _s
  279. * to point right behind the token
  280. */
  281. static inline int parse_token_param(str* _s, str* _r)
  282. {
  283. int i;
  284. /* There is nothing to parse,
  285. * return error
  286. */
  287. if (_s->len == 0) {
  288. return -1;
  289. }
  290. /* Save the begining of the
  291. * token in _r->s
  292. */
  293. _r->s = _s->s;
  294. /* Iterate through the
  295. * token body
  296. */
  297. for(i = 0; i < _s->len; i++) {
  298. /* All these characters
  299. * mark end of the token
  300. */
  301. switch(_s->s[i]) {
  302. case ' ':
  303. case '\t':
  304. case '\r':
  305. case '\n':
  306. case ',':
  307. case ';':
  308. /* So if you find
  309. * any of them
  310. * stop iterating
  311. */
  312. goto out;
  313. }
  314. }
  315. out:
  316. if (i == 0) {
  317. return -1;
  318. }
  319. /* Save length of the token */
  320. _r->len = i;
  321. /* Update _s parameter so it points
  322. * right behind the end of the token
  323. */
  324. _s->s = _s->s + i;
  325. _s->len -= i;
  326. /* Everything went OK */
  327. return 0;
  328. }
  329. /*! \brief
  330. * Parse a parameter name
  331. */
  332. static inline void parse_param_name(str* _s, pclass_t _c, param_hooks_t* _h, param_t* _p)
  333. {
  334. if (!_s->s) {
  335. DBG("DEBUG: parse_param_name: empty parameter\n");
  336. return;
  337. }
  338. _p->name.s = _s->s;
  339. while(_s->len) {
  340. switch(_s->s[0]) {
  341. case ' ':
  342. case '\t':
  343. case '\r':
  344. case '\n':
  345. case ';':
  346. case ',':
  347. case '=':
  348. goto out;
  349. }
  350. _s->s++;
  351. _s->len--;
  352. }
  353. out:
  354. _p->name.len = _s->s - _p->name.s;
  355. switch(_c) {
  356. case CLASS_CONTACT: parse_contact_class(_h, _p); break;
  357. case CLASS_URI: parse_uri_class(_h, _p); break;
  358. case CLASS_EVENT_DIALOG: parse_event_dialog_class(_h, _p); break;
  359. default: break;
  360. }
  361. }
  362. /*! \brief
  363. * Parse body of a parameter. It can be quoted string or
  364. * a single token.
  365. */
  366. static inline int parse_param_body(str* _s, param_t* _c)
  367. {
  368. if (_s->s[0] == '\"' || _s->s[0] == '\'') {
  369. if (parse_quoted_param(_s, &(_c->body)) < 0) {
  370. LOG(L_ERR, "parse_param_body(): Error while parsing quoted string\n");
  371. return -2;
  372. }
  373. } else {
  374. if (parse_token_param(_s, &(_c->body)) < 0) {
  375. LOG(L_ERR, "parse_param_body(): Error while parsing token\n");
  376. return -3;
  377. }
  378. }
  379. return 0;
  380. }
  381. /*! \brief
  382. * Only parse one parameter
  383. * Returns:
  384. * t: out parameter
  385. * -1: on error
  386. * 0: success, but expect a next paramter
  387. * 1: success and exepect no more parameters
  388. */
  389. inline int parse_param(str *_s, pclass_t _c, param_hooks_t *_h, param_t *t)
  390. {
  391. memset(t, 0, sizeof(param_t));
  392. parse_param_name(_s, _c, _h, t);
  393. trim_leading(_s);
  394. if (_s->len == 0) { /* The last parameter without body */
  395. t->len = t->name.len;
  396. goto ok;
  397. }
  398. if (_s->s[0] == '=') {
  399. _s->s++;
  400. _s->len--;
  401. trim_leading(_s);
  402. if (_s->len == 0) {
  403. /* Be forgiving and accept parameters with missing value,
  404. * we just set the length of parameter body to 0. */
  405. t->body.s = _s->s;
  406. t->body.len = 0;
  407. } else if (parse_param_body(_s, t) < 0) {
  408. LOG(L_ERR, "parse_params(): Error while parsing param body\n");
  409. goto error;
  410. }
  411. t->len = _s->s - t->name.s;
  412. trim_leading(_s);
  413. if (_s->len == 0) {
  414. goto ok;
  415. }
  416. } else {
  417. t->len = t->name.len;
  418. }
  419. if (_s->s[0] == ',') goto ok; /* To be able to parse header parameters */
  420. if (_s->s[0] == '>') goto ok; /* To be able to parse URI parameters */
  421. if (_s->s[0] != ';') {
  422. LOG(L_ERR, "parse_params(): Invalid character, ; expected\n");
  423. goto error;
  424. }
  425. _s->s++;
  426. _s->len--;
  427. trim_leading(_s);
  428. if (_s->len == 0) {
  429. LOG(L_ERR, "parse_params(): Param name missing after ;\n");
  430. goto error;
  431. }
  432. return 0; /* expect more params */
  433. ok:
  434. return 1; /* done with parsing for params */
  435. error:
  436. return -1;
  437. }
  438. /*! \brief
  439. * Parse parameters
  440. * \param _s is string containing parameters, it will be updated to point behind the parameters
  441. * \param _c is class of parameters
  442. * \param _h is pointer to structure that will be filled with pointer to well known parameters
  443. * \param _p pointing to linked list where parsed parameters will be stored
  444. * \return 0 on success and negative number on an error
  445. */
  446. int parse_params(str* _s, pclass_t _c, param_hooks_t* _h, param_t** _p)
  447. {
  448. param_t* t;
  449. if (!_s || !_p) {
  450. LOG(L_ERR, "parse_params(): Invalid parameter value\n");
  451. return -1;
  452. }
  453. if (_h)
  454. memset(_h, 0, sizeof(param_hooks_t));
  455. *_p = 0;
  456. if (!_s->s) { /* no parameters at all -- we're done */
  457. DBG("DEBUG: parse_params: empty uri params, skipping\n");
  458. return 0;
  459. }
  460. while(1) {
  461. t = (param_t*)pkg_malloc(sizeof(param_t));
  462. if (t == 0) {
  463. LOG(L_ERR, "parse_params(): No memory left\n");
  464. goto error;
  465. }
  466. switch(parse_param(_s, _c, _h, t)) {
  467. case 0: break;
  468. case 1: goto ok;
  469. default: goto error;
  470. }
  471. t->next = *_p;
  472. *_p = t;
  473. }
  474. error:
  475. if (t) pkg_free(t);
  476. free_params(*_p);
  477. *_p = 0;
  478. return -2;
  479. ok:
  480. t->next = *_p;
  481. *_p = t;
  482. return 0;
  483. }
  484. /*! \brief
  485. * Free linked list of parameters
  486. */
  487. static inline void do_free_params(param_t* _p, int _shm)
  488. {
  489. param_t* ptr;
  490. while(_p) {
  491. ptr = _p;
  492. _p = _p->next;
  493. if (_shm) shm_free(ptr);
  494. else pkg_free(ptr);
  495. }
  496. }
  497. /*! \brief
  498. * Free linked list of parameters
  499. */
  500. void free_params(param_t* _p)
  501. {
  502. do_free_params(_p, 0);
  503. }
  504. /*! \brief
  505. * Free linked list of parameters
  506. */
  507. void shm_free_params(param_t* _p)
  508. {
  509. do_free_params(_p, 1);
  510. }
  511. /*! \brief
  512. * Print a parameter structure, just for debugging
  513. */
  514. static inline void print_param(FILE* _o, param_t* _p)
  515. {
  516. char* type;
  517. fprintf(_o, "---param(%p)---\n", _p);
  518. switch(_p->type) {
  519. case P_OTHER: type = "P_OTHER"; break;
  520. case P_Q: type = "P_Q"; break;
  521. case P_EXPIRES: type = "P_EXPIRES"; break;
  522. case P_METHODS: type = "P_METHODS"; break;
  523. case P_TRANSPORT: type = "P_TRANSPORT"; break;
  524. case P_LR: type = "P_LR"; break;
  525. case P_R2: type = "P_R2"; break;
  526. case P_MADDR: type = "P_MADDR"; break;
  527. case P_TTL: type = "P_TTL"; break;
  528. case P_RECEIVED: type = "P_RECEIVED"; break;
  529. case P_DSTIP: type = "P_DSTIP"; break;
  530. case P_DSTPORT: type = "P_DSTPORT"; break;
  531. case P_INSTANCE: type = "P_INSTANCE"; break;
  532. case P_FTAG: type = "P_FTAG"; break;
  533. case P_CALL_ID: type = "P_CALL_ID"; break;
  534. case P_FROM_TAG: type = "P_FROM_TAG"; break;
  535. case P_TO_TAG: type = "P_TO_TAG"; break;
  536. case P_ISD: type = "P_ISD"; break;
  537. case P_SLA: type = "P_SLA"; break;
  538. default: type = "UNKNOWN"; break;
  539. }
  540. fprintf(_o, "type: %s\n", type);
  541. fprintf(_o, "name: \'%.*s\'\n", _p->name.len, _p->name.s);
  542. fprintf(_o, "body: \'%.*s\'\n", _p->body.len, _p->body.s);
  543. fprintf(_o, "len : %d\n", _p->len);
  544. fprintf(_o, "---/param---\n");
  545. }
  546. /*! \brief
  547. * Print linked list of parameters, just for debugging
  548. */
  549. void print_params(FILE* _o, param_t* _p)
  550. {
  551. param_t* ptr;
  552. ptr = _p;
  553. while(ptr) {
  554. print_param(_o, ptr);
  555. ptr = ptr->next;
  556. }
  557. }
  558. /*! \brief
  559. * Duplicate linked list of parameters
  560. */
  561. static inline int do_duplicate_params(param_t** _n, param_t* _p, int _shm)
  562. {
  563. param_t* last, *ptr, *t;
  564. if (!_n) {
  565. LOG(L_ERR, "duplicate_params(): Invalid parameter value\n");
  566. return -1;
  567. }
  568. last = 0;
  569. *_n = 0;
  570. ptr = _p;
  571. while(ptr) {
  572. if (_shm) {
  573. t = (param_t*)shm_malloc(sizeof(param_t));
  574. } else {
  575. t = (param_t*)pkg_malloc(sizeof(param_t));
  576. }
  577. if (!t) {
  578. LOG(L_ERR, "duplicate_params(): Invalid parameter value\n");
  579. goto err;
  580. }
  581. memcpy(t, ptr, sizeof(param_t));
  582. t->next = 0;
  583. if (!*_n) *_n = t;
  584. if (last) last->next = t;
  585. last = t;
  586. ptr = ptr->next;
  587. }
  588. return 0;
  589. err:
  590. do_free_params(*_n, _shm);
  591. return -2;
  592. }
  593. /*! \brief
  594. * Duplicate linked list of parameters
  595. */
  596. int duplicate_params(param_t** _n, param_t* _p)
  597. {
  598. return do_duplicate_params(_n, _p, 0);
  599. }
  600. /*! \brief
  601. * Duplicate linked list of parameters
  602. */
  603. int shm_duplicate_params(param_t** _n, param_t* _p)
  604. {
  605. return do_duplicate_params(_n, _p, 1);
  606. }