parse_rr.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. #include <string.h>
  30. #include "parse_rr.h"
  31. #include "../mem/mem.h"
  32. #include "../mem/shm_mem.h"
  33. #include "../dprint.h"
  34. #include "../trim.h"
  35. #include "../ut.h"
  36. /*
  37. * Parse Route and Record-Route header fields
  38. */
  39. int parse_rr(struct hdr_field* _h)
  40. {
  41. rr_t* r, *last;
  42. str s;
  43. param_hooks_t hooks;
  44. if (!_h) {
  45. LOG(L_ERR, "parse_rr(): Invalid parameter value\n");
  46. return -1;
  47. }
  48. if (_h->parsed) {
  49. /* Already parsed, return */
  50. return 0;
  51. }
  52. /* Make a temporary copy of the string pointer */
  53. s.s = _h->body.s;
  54. s.len = _h->body.len;
  55. trim_leading(&s);
  56. last = 0;
  57. while(1) {
  58. /* Allocate and clear rr stucture */
  59. r = (rr_t*)pkg_malloc(sizeof(rr_t));
  60. if (!r) {
  61. LOG(L_ERR, "parse_rr(): No memory left\n");
  62. goto error;
  63. }
  64. memset(r, 0, sizeof(rr_t));
  65. /* Parse name-addr part of the header */
  66. if (parse_nameaddr(&s, &r->nameaddr) < 0) {
  67. LOG(L_ERR, "parse_rr(): Error while parsing name-addr\n");
  68. goto error;
  69. }
  70. r->len = r->nameaddr.len;
  71. /* Shift just behind the closing > */
  72. s.s = r->nameaddr.name.s + r->nameaddr.len; /* Point just behind > */
  73. s.len -= r->nameaddr.len;
  74. trim_leading(&s); /* Skip any whitechars */
  75. /* Nothing left, finish */
  76. if (s.len == 0) goto ok;
  77. if (s.s[0] == ';') { /* Contact parameter found */
  78. s.s++;
  79. s.len--;
  80. trim_leading(&s);
  81. if (s.len == 0) {
  82. LOG(L_ERR, "parse_rr(): Error while parsing params\n");
  83. goto error;
  84. }
  85. /* Parse all parameters */
  86. if (parse_params(&s, CLASS_ANY, &hooks, &r->params) < 0) {
  87. LOG(L_ERR, "parse_rr(): Error while parsing params\n");
  88. goto error;
  89. }
  90. r->len = r->params->name.s + r->params->len - r->nameaddr.name.s;
  91. /* Copy hooks */
  92. /*r->r2 = hooks.rr.r2; */
  93. trim_leading(&s);
  94. if (s.len == 0) goto ok;
  95. }
  96. if (s.s[0] != ',') {
  97. LOG(L_ERR, "parse_rr(): Invalid character '%c', comma expected\n", s.s[0]);
  98. goto error;
  99. }
  100. /* Next character is comma or end of header*/
  101. s.s++;
  102. s.len--;
  103. trim_leading(&s);
  104. if (s.len == 0) {
  105. LOG(L_ERR, "parse_rr(): Text after comma missing\n");
  106. goto error;
  107. }
  108. /* Append the structure as last parameter of the linked list */
  109. if (!_h->parsed) _h->parsed = (void*)r;
  110. if (last) last->next = r;
  111. last = r;
  112. }
  113. error:
  114. if (r) pkg_free(r);
  115. free_rr((rr_t**)&_h->parsed); /* Free any contacts created so far */
  116. return -1;
  117. ok:
  118. if (!_h->parsed) _h->parsed = (void*)r;
  119. if (last) last->next = r;
  120. return 0;
  121. }
  122. /*
  123. * Free list of rrs
  124. * _r is head of the list
  125. */
  126. static inline void do_free_rr(rr_t** _r, int _shm)
  127. {
  128. rr_t* ptr;
  129. while(*_r) {
  130. ptr = *_r;
  131. *_r = (*_r)->next;
  132. if (ptr->params) {
  133. if (_shm) shm_free_params(ptr->params);
  134. else free_params(ptr->params);
  135. }
  136. if (_shm) shm_free(ptr);
  137. else pkg_free(ptr);
  138. }
  139. }
  140. /*
  141. * Free list of rrs
  142. * _r is head of the list
  143. */
  144. void free_rr(rr_t** _r)
  145. {
  146. do_free_rr(_r, 0);
  147. }
  148. /*
  149. * Free list of rrs
  150. * _r is head of the list
  151. */
  152. void shm_free_rr(rr_t** _r)
  153. {
  154. do_free_rr(_r, 1);
  155. }
  156. /*
  157. * Print list of RRs, just for debugging
  158. */
  159. void print_rr(FILE* _o, rr_t* _r)
  160. {
  161. rr_t* ptr;
  162. ptr = _r;
  163. while(ptr) {
  164. fprintf(_o, "---RR---\n");
  165. print_nameaddr(_o, &ptr->nameaddr);
  166. fprintf(_o, "r2 : %p\n", ptr->r2);
  167. if (ptr->params) {
  168. print_params(_o, ptr->params);
  169. }
  170. fprintf(_o, "len: %d\n", ptr->len);
  171. fprintf(_o, "---/RR---\n");
  172. ptr = ptr->next;
  173. }
  174. }
  175. /*
  176. * Translate all pointers in the structure and also
  177. * in all parameters in the list
  178. */
  179. static inline void xlate_pointers(rr_t* _orig, rr_t* _r)
  180. {
  181. param_t* ptr;
  182. _r->nameaddr.uri.s = translate_pointer(_r->nameaddr.name.s, _orig->nameaddr.name.s, _r->nameaddr.uri.s);
  183. ptr = _r->params;
  184. while(ptr) {
  185. /* if (ptr->type == P_R2) _r->r2 = ptr; */
  186. ptr->name.s = translate_pointer(_r->nameaddr.name.s, _orig->nameaddr.name.s, ptr->name.s);
  187. ptr->body.s = translate_pointer(_r->nameaddr.name.s, _orig->nameaddr.name.s, ptr->body.s);
  188. ptr = ptr->next;
  189. }
  190. }
  191. /*
  192. * Duplicate a single rr_t structure using pkg_malloc or shm_malloc
  193. */
  194. static inline int do_duplicate_rr(rr_t** _new, rr_t* _r, int _shm)
  195. {
  196. int len, ret;
  197. rr_t* res;
  198. if (!_new || !_r) {
  199. LOG(L_ERR, "duplicate_rr(): Invalid parameter value\n");
  200. return -1;
  201. }
  202. if (_r->params) {
  203. len = _r->params->name.s + _r->params->len - _r->nameaddr.name.s;
  204. } else {
  205. len = _r->nameaddr.len;
  206. }
  207. if (_shm) res = shm_malloc(sizeof(rr_t) + len);
  208. else res = pkg_malloc(sizeof(rr_t) + len);
  209. if (!res) {
  210. LOG(L_ERR, "duplicate_rr(): No memory left\n");
  211. return -2;
  212. }
  213. memcpy(res, _r, sizeof(rr_t));
  214. res->nameaddr.name.s = (char*)res + sizeof(rr_t);
  215. memcpy(res->nameaddr.name.s, _r->nameaddr.name.s, len);
  216. if (_shm) {
  217. ret = shm_duplicate_params(&res->params, _r->params);
  218. } else {
  219. ret = duplicate_params(&res->params, _r->params);
  220. }
  221. if (ret < 0) {
  222. LOG(L_ERR, "Error while duplicating parameters\n");
  223. if (_shm) shm_free(res);
  224. else pkg_free(res);
  225. return -3;
  226. }
  227. xlate_pointers(_r, res);
  228. *_new = res;
  229. return 0;
  230. }
  231. /*
  232. * Duplicate a single rr_t structure using pkg_malloc
  233. */
  234. int duplicate_rr(rr_t** _new, rr_t* _r)
  235. {
  236. return do_duplicate_rr(_new, _r, 0);
  237. }
  238. /*
  239. * Duplicate a single rr_t structure using pkg_malloc
  240. */
  241. int shm_duplicate_rr(rr_t** _new, rr_t* _r)
  242. {
  243. return do_duplicate_rr(_new, _r, 1);
  244. }