parse_rr.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * $Id$
  3. *
  4. * Route & Record-Route header field parser
  5. *
  6. * Copyright (C) 2001-2003 FhG Fokus
  7. *
  8. * This file is part of ser, a free SIP server.
  9. *
  10. * ser is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * For a license to use the ser software under conditions
  16. * other than those described here, or to purchase support for this
  17. * software, please contact iptel.org by e-mail at the following addresses:
  18. * [email protected]
  19. *
  20. * ser is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. */
  29. /*! \file
  30. * \brief Parser :: Route & Record-Route header field parser
  31. *
  32. * \ingroup parser
  33. */
  34. /**
  35. * History:
  36. * --------
  37. * 2003-10-07 parse_rr() split and added parse_rr_body()
  38. * 2003-10-21 duplicate_rr() duplicate the whole linked list of RR
  39. */
  40. #include <string.h>
  41. #include "parse_rr.h"
  42. #include "../mem/mem.h"
  43. #include "../mem/shm_mem.h"
  44. #include "../dprint.h"
  45. #include "../trim.h"
  46. #include "../ut.h"
  47. /*! \brief
  48. * Parse Route or Record-Route body
  49. */
  50. static inline int do_parse_rr_body(char *buf, int len, rr_t **head)
  51. {
  52. rr_t* r, *last;
  53. str s;
  54. param_hooks_t hooks;
  55. /* Make a temporary copy of the string pointer */
  56. if(buf==0 || len<=0)
  57. {
  58. DBG("parse_rr_body(): No body for record-route\n");
  59. *head = 0;
  60. return -2;
  61. }
  62. s.s = buf;
  63. s.len = len;
  64. trim_leading(&s);
  65. last = 0;
  66. while(1) {
  67. /* Allocate and clear rr structure */
  68. r = (rr_t*)pkg_malloc(sizeof(rr_t));
  69. if (!r) {
  70. LOG(L_ERR, "parse_rr(): No memory left\n");
  71. goto error;
  72. }
  73. memset(r, 0, sizeof(rr_t));
  74. /* Parse name-addr part of the header */
  75. if (parse_nameaddr(&s, &r->nameaddr) < 0) {
  76. LOG(L_ERR, "parse_rr(): Error while parsing name-addr (%.*s)\n",
  77. s.len, ZSW(s.s));
  78. goto error;
  79. }
  80. r->len = r->nameaddr.len;
  81. /* Shift just behind the closing > */
  82. s.s = r->nameaddr.name.s + r->nameaddr.len; /* Point just behind > */
  83. s.len -= r->nameaddr.len;
  84. trim_leading(&s); /* Skip any white-chars */
  85. if (s.len == 0) goto ok; /* Nothing left, finish */
  86. if (s.s[0] == ';') { /* Route parameter found */
  87. s.s++;
  88. s.len--;
  89. trim_leading(&s);
  90. if (s.len == 0) {
  91. LOG(L_ERR, "parse_rr(): Error while parsing params\n");
  92. goto error;
  93. }
  94. /* Parse all parameters */
  95. if (parse_params(&s, CLASS_ANY, &hooks, &r->params) < 0) {
  96. LOG(L_ERR, "parse_rr(): Error while parsing params\n");
  97. goto error;
  98. }
  99. r->len = r->params->name.s + r->params->len - r->nameaddr.name.s;
  100. /* Copy hooks */
  101. /*r->r2 = hooks.rr.r2; */
  102. trim_leading(&s);
  103. if (s.len == 0) goto ok;
  104. }
  105. if (s.s[0] != ',') {
  106. LOG(L_ERR, "parse_rr(): Invalid character '%c', comma expected\n", s.s[0]);
  107. goto error;
  108. }
  109. /* Next character is comma or end of header*/
  110. s.s++;
  111. s.len--;
  112. trim_leading(&s);
  113. if (s.len == 0) {
  114. LOG(L_ERR, "parse_rr(): Text after comma missing\n");
  115. goto error;
  116. }
  117. /* Append the structure as last parameter of the linked list */
  118. if (!*head) *head = r;
  119. if (last) last->next = r;
  120. last = r;
  121. }
  122. error:
  123. if (r) pkg_free(r);
  124. free_rr(head); /* Free any contacts created so far */
  125. return -1;
  126. ok:
  127. if (!*head) *head = r;
  128. if (last) last->next = r;
  129. return 0;
  130. }
  131. /*! \brief
  132. * Wrapper to do_parse_rr_body() for external calls
  133. */
  134. int parse_rr_body(char *buf, int len, rr_t **head)
  135. {
  136. return do_parse_rr_body(buf, len, head);
  137. }
  138. /*! \brief
  139. * Parse Route and Record-Route header fields
  140. */
  141. int parse_rr(struct hdr_field* _h)
  142. {
  143. rr_t* r = NULL;
  144. if (!_h) {
  145. LOG(L_ERR, "parse_rr(): Invalid parameter value\n");
  146. return -1;
  147. }
  148. if (_h->parsed) {
  149. /* Already parsed, return */
  150. return 0;
  151. }
  152. if(do_parse_rr_body(_h->body.s, _h->body.len, &r) < 0)
  153. return -1;
  154. _h->parsed = (void*)r;
  155. return 0;
  156. }
  157. /*! \brief
  158. * Free list of rrs
  159. * _r is head of the list
  160. */
  161. static inline void do_free_rr(rr_t** _r, int _shm)
  162. {
  163. rr_t* ptr;
  164. while(*_r) {
  165. ptr = *_r;
  166. *_r = (*_r)->next;
  167. if (ptr->params) {
  168. if (_shm) shm_free_params(ptr->params);
  169. else free_params(ptr->params);
  170. }
  171. if (_shm) shm_free(ptr);
  172. else pkg_free(ptr);
  173. }
  174. }
  175. /*! \brief
  176. * Free list of rrs
  177. * _r is head of the list
  178. */
  179. void free_rr(rr_t** _r)
  180. {
  181. do_free_rr(_r, 0);
  182. }
  183. /*! \brief
  184. * Free list of rrs
  185. * _r is head of the list
  186. */
  187. void shm_free_rr(rr_t** _r)
  188. {
  189. do_free_rr(_r, 1);
  190. }
  191. /*! \brief
  192. * Print list of RRs, just for debugging
  193. */
  194. void print_rr(FILE* _o, rr_t* _r)
  195. {
  196. rr_t* ptr;
  197. ptr = _r;
  198. while(ptr) {
  199. fprintf(_o, "---RR---\n");
  200. print_nameaddr(_o, &ptr->nameaddr);
  201. fprintf(_o, "r2 : %p\n", ptr->r2);
  202. if (ptr->params) {
  203. print_params(_o, ptr->params);
  204. }
  205. fprintf(_o, "len: %d\n", ptr->len);
  206. fprintf(_o, "---/RR---\n");
  207. ptr = ptr->next;
  208. }
  209. }
  210. /*! \brief
  211. * Translate all pointers in the structure and also
  212. * in all parameters in the list
  213. */
  214. static inline void xlate_pointers(rr_t* _orig, rr_t* _r)
  215. {
  216. param_t* ptr;
  217. _r->nameaddr.uri.s = translate_pointer(_r->nameaddr.name.s, _orig->nameaddr.name.s, _r->nameaddr.uri.s);
  218. ptr = _r->params;
  219. while(ptr) {
  220. /* if (ptr->type == P_R2) _r->r2 = ptr; */
  221. ptr->name.s = translate_pointer(_r->nameaddr.name.s, _orig->nameaddr.name.s, ptr->name.s);
  222. ptr->body.s = translate_pointer(_r->nameaddr.name.s, _orig->nameaddr.name.s, ptr->body.s);
  223. ptr = ptr->next;
  224. }
  225. }
  226. /*! \brief
  227. * Duplicate a single rr_t structure using pkg_malloc or shm_malloc
  228. */
  229. static inline int do_duplicate_rr(rr_t** _new, rr_t* _r, int _shm)
  230. {
  231. int len, ret;
  232. rr_t* res, *prev, *it;
  233. if (!_new || !_r) {
  234. LOG(L_ERR, "duplicate_rr(): Invalid parameter value\n");
  235. return -1;
  236. }
  237. prev = NULL;
  238. *_new = NULL;
  239. it = _r;
  240. while(it)
  241. {
  242. if (it->params) {
  243. len = it->params->name.s + it->params->len - it->nameaddr.name.s;
  244. } else {
  245. len = it->nameaddr.len;
  246. }
  247. if (_shm) res = shm_malloc(sizeof(rr_t) + len);
  248. else res = pkg_malloc(sizeof(rr_t) + len);
  249. if (!res) {
  250. LOG(L_ERR, "duplicate_rr(): No memory left\n");
  251. return -2;
  252. }
  253. memcpy(res, it, sizeof(rr_t));
  254. res->nameaddr.name.s = (char*)res + sizeof(rr_t);
  255. memcpy(res->nameaddr.name.s, it->nameaddr.name.s, len);
  256. if (_shm) {
  257. ret = shm_duplicate_params(&res->params, it->params);
  258. } else {
  259. ret = duplicate_params(&res->params, it->params);
  260. }
  261. if (ret < 0) {
  262. LOG(L_ERR, "duplicate_rr(): Error while duplicating parameters\n");
  263. if (_shm) shm_free(res);
  264. else pkg_free(res);
  265. return -3;
  266. }
  267. xlate_pointers(it, res);
  268. res->next=NULL;
  269. if(*_new==NULL)
  270. *_new = res;
  271. if(prev)
  272. prev->next = res;
  273. prev = res;
  274. it = it->next;
  275. }
  276. return 0;
  277. }
  278. /*! \brief
  279. * Duplicate a single rr_t structure using pkg_malloc
  280. */
  281. int duplicate_rr(rr_t** _new, rr_t* _r)
  282. {
  283. return do_duplicate_rr(_new, _r, 0);
  284. }
  285. /*! \brief
  286. * Duplicate a single rr_t structure using pkg_malloc
  287. */
  288. int shm_duplicate_rr(rr_t** _new, rr_t* _r)
  289. {
  290. return do_duplicate_rr(_new, _r, 1);
  291. }