parse_rr.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * Route & Record-Route header field 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. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27. /*! \file
  28. * \brief Parser :: Route & Record-Route header field parser
  29. *
  30. * \ingroup parser
  31. */
  32. /**
  33. * History:
  34. * --------
  35. * 2003-10-07 parse_rr() split and added parse_rr_body()
  36. * 2003-10-21 duplicate_rr() duplicate the whole linked list of RR
  37. */
  38. #include <string.h>
  39. #include "parse_rr.h"
  40. #include "../mem/mem.h"
  41. #include "../mem/shm_mem.h"
  42. #include "../dprint.h"
  43. #include "../trim.h"
  44. #include "../ut.h"
  45. /*! \brief
  46. * Parse Route or Record-Route body
  47. */
  48. static inline int do_parse_rr_body(char *buf, int len, rr_t **head)
  49. {
  50. rr_t* r, *last;
  51. str s;
  52. param_hooks_t hooks;
  53. /* Make a temporary copy of the string pointer */
  54. if(buf==0 || len<=0)
  55. {
  56. DBG("parse_rr_body(): No body for record-route\n");
  57. *head = 0;
  58. return -2;
  59. }
  60. s.s = buf;
  61. s.len = len;
  62. trim_leading(&s);
  63. last = 0;
  64. while(1) {
  65. /* Allocate and clear rr structure */
  66. r = (rr_t*)pkg_malloc(sizeof(rr_t));
  67. if (!r) {
  68. LOG(L_ERR, "parse_rr(): No memory left\n");
  69. goto error;
  70. }
  71. memset(r, 0, sizeof(rr_t));
  72. /* Parse name-addr part of the header */
  73. if (parse_nameaddr(&s, &r->nameaddr) < 0) {
  74. LOG(L_ERR, "parse_rr(): Error while parsing name-addr (%.*s)\n",
  75. s.len, ZSW(s.s));
  76. goto error;
  77. }
  78. r->len = r->nameaddr.len;
  79. /* Shift just behind the closing > */
  80. s.s = r->nameaddr.name.s + r->nameaddr.len; /* Point just behind > */
  81. s.len -= r->nameaddr.len;
  82. trim_leading(&s); /* Skip any white-chars */
  83. if (s.len == 0) goto ok; /* Nothing left, finish */
  84. if (s.s[0] == ';') { /* Route parameter found */
  85. s.s++;
  86. s.len--;
  87. trim_leading(&s);
  88. if (s.len == 0) {
  89. LOG(L_ERR, "parse_rr(): Error while parsing params\n");
  90. goto error;
  91. }
  92. /* Parse all parameters */
  93. if (parse_params(&s, CLASS_ANY, &hooks, &r->params) < 0) {
  94. LOG(L_ERR, "parse_rr(): Error while parsing params\n");
  95. goto error;
  96. }
  97. r->len = r->params->name.s + r->params->len - r->nameaddr.name.s;
  98. /* Copy hooks */
  99. /*r->r2 = hooks.rr.r2; */
  100. trim_leading(&s);
  101. if (s.len == 0) goto ok;
  102. }
  103. if (s.s[0] != ',') {
  104. LOG(L_ERR, "parse_rr(): Invalid character '%c', comma expected\n", s.s[0]);
  105. goto error;
  106. }
  107. /* Next character is comma or end of header*/
  108. s.s++;
  109. s.len--;
  110. trim_leading(&s);
  111. if (s.len == 0) {
  112. LOG(L_ERR, "parse_rr(): Text after comma missing\n");
  113. goto error;
  114. }
  115. /* Append the structure as last parameter of the linked list */
  116. if (!*head) *head = r;
  117. if (last) last->next = r;
  118. last = r;
  119. }
  120. error:
  121. if (r) free_rr(&r);
  122. free_rr(head); /* Free any contacts created so far */
  123. return -1;
  124. ok:
  125. if (!*head) *head = r;
  126. if (last) last->next = r;
  127. return 0;
  128. }
  129. /*! \brief
  130. * Wrapper to do_parse_rr_body() for external calls
  131. */
  132. int parse_rr_body(char *buf, int len, rr_t **head)
  133. {
  134. return do_parse_rr_body(buf, len, head);
  135. }
  136. /*! \brief
  137. * Parse Route and Record-Route header fields
  138. */
  139. int parse_rr(struct hdr_field* _h)
  140. {
  141. rr_t* r = NULL;
  142. if (!_h) {
  143. LOG(L_ERR, "parse_rr(): Invalid parameter value\n");
  144. return -1;
  145. }
  146. if (_h->parsed) {
  147. /* Already parsed, return */
  148. return 0;
  149. }
  150. if(do_parse_rr_body(_h->body.s, _h->body.len, &r) < 0)
  151. return -1;
  152. _h->parsed = (void*)r;
  153. return 0;
  154. }
  155. /*! \brief
  156. * Free list of rrs
  157. * _r is head of the list
  158. */
  159. static inline void do_free_rr(rr_t** _r, int _shm)
  160. {
  161. rr_t* ptr;
  162. while(*_r) {
  163. ptr = *_r;
  164. *_r = (*_r)->next;
  165. if (ptr->params) {
  166. if (_shm) shm_free_params(ptr->params);
  167. else free_params(ptr->params);
  168. }
  169. if (_shm) shm_free(ptr);
  170. else pkg_free(ptr);
  171. }
  172. }
  173. /*! \brief
  174. * Free list of rrs
  175. * _r is head of the list
  176. */
  177. void free_rr(rr_t** _r)
  178. {
  179. do_free_rr(_r, 0);
  180. }
  181. /*! \brief
  182. * Free list of rrs
  183. * _r is head of the list
  184. */
  185. void shm_free_rr(rr_t** _r)
  186. {
  187. do_free_rr(_r, 1);
  188. }
  189. /*! \brief
  190. * Print list of RRs, just for debugging
  191. */
  192. void print_rr(FILE* _o, rr_t* _r)
  193. {
  194. rr_t* ptr;
  195. ptr = _r;
  196. while(ptr) {
  197. fprintf(_o, "---RR---\n");
  198. print_nameaddr(_o, &ptr->nameaddr);
  199. fprintf(_o, "r2 : %p\n", ptr->r2);
  200. if (ptr->params) {
  201. print_params(_o, ptr->params);
  202. }
  203. fprintf(_o, "len: %d\n", ptr->len);
  204. fprintf(_o, "---/RR---\n");
  205. ptr = ptr->next;
  206. }
  207. }
  208. /*! \brief
  209. * Translate all pointers in the structure and also
  210. * in all parameters in the list
  211. */
  212. static inline void xlate_pointers(rr_t* _orig, rr_t* _r)
  213. {
  214. param_t* ptr;
  215. _r->nameaddr.uri.s = translate_pointer(_r->nameaddr.name.s, _orig->nameaddr.name.s, _r->nameaddr.uri.s);
  216. ptr = _r->params;
  217. while(ptr) {
  218. /* if (ptr->type == P_R2) _r->r2 = ptr; */
  219. ptr->name.s = translate_pointer(_r->nameaddr.name.s, _orig->nameaddr.name.s, ptr->name.s);
  220. ptr->body.s = translate_pointer(_r->nameaddr.name.s, _orig->nameaddr.name.s, ptr->body.s);
  221. ptr = ptr->next;
  222. }
  223. }
  224. /*! \brief
  225. * Duplicate a single rr_t structure using pkg_malloc or shm_malloc
  226. */
  227. static inline int do_duplicate_rr(rr_t** _new, rr_t* _r, int _shm)
  228. {
  229. int len, ret;
  230. rr_t* res, *prev, *it;
  231. if (!_new || !_r) {
  232. LOG(L_ERR, "duplicate_rr(): Invalid parameter value\n");
  233. return -1;
  234. }
  235. prev = NULL;
  236. *_new = NULL;
  237. it = _r;
  238. while(it)
  239. {
  240. if (it->params) {
  241. len = it->params->name.s + it->params->len - it->nameaddr.name.s;
  242. } else {
  243. len = it->nameaddr.len;
  244. }
  245. if (_shm) res = shm_malloc(sizeof(rr_t) + len);
  246. else res = pkg_malloc(sizeof(rr_t) + len);
  247. if (!res) {
  248. LOG(L_ERR, "duplicate_rr(): No memory left\n");
  249. return -2;
  250. }
  251. memcpy(res, it, sizeof(rr_t));
  252. res->nameaddr.name.s = (char*)res + sizeof(rr_t);
  253. memcpy(res->nameaddr.name.s, it->nameaddr.name.s, len);
  254. if (_shm) {
  255. ret = shm_duplicate_params(&res->params, it->params);
  256. } else {
  257. ret = duplicate_params(&res->params, it->params);
  258. }
  259. if (ret < 0) {
  260. LOG(L_ERR, "duplicate_rr(): Error while duplicating parameters\n");
  261. if (_shm) shm_free(res);
  262. else pkg_free(res);
  263. return -3;
  264. }
  265. xlate_pointers(it, res);
  266. res->next=NULL;
  267. if(*_new==NULL)
  268. *_new = res;
  269. if(prev)
  270. prev->next = res;
  271. prev = res;
  272. it = it->next;
  273. }
  274. return 0;
  275. }
  276. /*! \brief
  277. * Duplicate a single rr_t structure using pkg_malloc
  278. */
  279. int duplicate_rr(rr_t** _new, rr_t* _r)
  280. {
  281. return do_duplicate_rr(_new, _r, 0);
  282. }
  283. /*! \brief
  284. * Duplicate a single rr_t structure using pkg_malloc
  285. */
  286. int shm_duplicate_rr(rr_t** _new, rr_t* _r)
  287. {
  288. return do_duplicate_rr(_new, _r, 1);
  289. }
  290. /*!
  291. * get first RR header and print comma separated bodies in oroute
  292. * - order = 0 normal; order = 1 reverse
  293. * - nb_recs - input=skip number of rr; output=number of printed rrs
  294. */
  295. int print_rr_body(struct hdr_field *iroute, str *oroute, int order,
  296. unsigned int * nb_recs)
  297. {
  298. rr_t *p;
  299. int n = 0, nr=0;
  300. int i = 0;
  301. int route_len;
  302. #define MAX_RR_HDRS 64
  303. static str route[MAX_RR_HDRS];
  304. char *cp, *start;
  305. if(iroute==NULL)
  306. return 0;
  307. route_len= 0;
  308. memset(route, 0, MAX_RR_HDRS*sizeof(str));
  309. while (iroute!=NULL)
  310. {
  311. if (parse_rr(iroute) < 0)
  312. {
  313. LM_ERR("failed to parse RR\n");
  314. goto error;
  315. }
  316. p =(rr_t*)iroute->parsed;
  317. while (p)
  318. {
  319. route[n].s = p->nameaddr.name.s;
  320. route[n].len = p->len;
  321. LM_DBG("current rr is %.*s\n", route[n].len, route[n].s);
  322. n++;
  323. if(n==MAX_RR_HDRS)
  324. {
  325. LM_ERR("too many RR\n");
  326. goto error;
  327. }
  328. p = p->next;
  329. }
  330. iroute = next_sibling_hdr(iroute);
  331. }
  332. for(i=0;i<n;i++){
  333. if(!nb_recs || (nb_recs &&
  334. ( (!order&& (i>=*nb_recs)) || (order && (i<=(n-*nb_recs)) )) ) )
  335. {
  336. route_len+= route[i].len;
  337. nr++;
  338. }
  339. }
  340. if(nb_recs)
  341. LM_DBG("skipping %i route records\n", *nb_recs);
  342. route_len += --nr; /* for commas */
  343. oroute->s=(char*)pkg_malloc(route_len);
  344. if(oroute->s==0)
  345. {
  346. LM_ERR("no more pkg mem\n");
  347. goto error;
  348. }
  349. cp = start = oroute->s;
  350. if(order==0)
  351. {
  352. i= (nb_recs == NULL) ? 0:*nb_recs;
  353. while (i<n)
  354. {
  355. memcpy(cp, route[i].s, route[i].len);
  356. cp += route[i].len;
  357. if (++i<n)
  358. *(cp++) = ',';
  359. }
  360. } else {
  361. i = (nb_recs == NULL) ? n-1 : (n-*nb_recs-1);
  362. while (i>=0)
  363. {
  364. memcpy(cp, route[i].s, route[i].len);
  365. cp += route[i].len;
  366. if (i-->0)
  367. *(cp++) = ',';
  368. }
  369. }
  370. oroute->len=cp - start;
  371. LM_DBG("out rr [%.*s]\n", oroute->len, oroute->s);
  372. LM_DBG("we have %i records\n", (nb_recs == NULL) ? n : n-*nb_recs);
  373. if(nb_recs != NULL)
  374. *nb_recs = (unsigned int)n-*nb_recs;
  375. return 0;
  376. error:
  377. return -1;
  378. }
  379. /*!
  380. * Path must be available. Function returns the first uri
  381. * from Path without any duplication.
  382. */
  383. int get_path_dst_uri(str *_p, str *_dst)
  384. {
  385. rr_t *route = 0;
  386. LM_DBG("path for branch: '%.*s'\n", _p->len, _p->s);
  387. if(parse_rr_body(_p->s, _p->len, &route) < 0) {
  388. LM_ERR("failed to parse Path body\n");
  389. return -1;
  390. }
  391. if(!route) {
  392. LM_ERR("failed to parse Path body no head found\n");
  393. return -1;
  394. }
  395. *_dst = route->nameaddr.uri;
  396. free_rr(&route);
  397. return 0;
  398. }