data_lump.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /* $Id$
  2. *
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. *
  27. * History:
  28. * --------
  29. * 2003-01-19 support for duplication lump lists added (jiri)
  30. */
  31. #include "data_lump.h"
  32. #include "dprint.h"
  33. #include "mem/mem.h"
  34. #include "globals.h"
  35. #include "error.h"
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #ifdef DEBUG_DMALLOC
  39. #include <dmalloc.h>
  40. #endif
  41. /* WARNING: all lump add/insert operations excpect a pkg_malloc'ed char*
  42. * pointer the will be DEALLOCATED when the sip_msg is destroyed! */
  43. enum lump_dir { LD_NEXT, LD_BEFORE, LD_AFTER };
  44. /* adds a header to the end
  45. * returns pointer on success, 0 on error */
  46. struct lump* append_new_lump(struct lump** list, char* new_hdr,
  47. int len, int type)
  48. {
  49. struct lump** t;
  50. struct lump* tmp;
  51. for (t=list;*t;t=&((*t)->next));
  52. tmp=pkg_malloc(sizeof(struct lump));
  53. if (tmp==0){
  54. LOG(L_ERR, "ERROR: append_new_lump: out of memory\n");
  55. return 0;
  56. }
  57. memset(tmp,0,sizeof(struct lump));
  58. tmp->type=type;
  59. tmp->op=LUMP_ADD;
  60. tmp->u.value=new_hdr;
  61. tmp->len=len;
  62. *t=tmp;
  63. return tmp;
  64. }
  65. /* inserts a header to the beginning
  66. * returns pointer if success, 0 on error */
  67. struct lump* insert_new_lump(struct lump** list, char* new_hdr,
  68. int len, int type)
  69. {
  70. struct lump* tmp;
  71. tmp=pkg_malloc(sizeof(struct lump));
  72. if (tmp==0){
  73. LOG(L_ERR, "ERROR: insert_new_lump: out of memory\n");
  74. return 0;
  75. }
  76. memset(tmp,0,sizeof(struct lump));
  77. tmp->next=*list;
  78. tmp->type=type;
  79. tmp->op=LUMP_ADD;
  80. tmp->u.value=new_hdr;
  81. tmp->len=len;
  82. *list=tmp;
  83. return tmp;
  84. }
  85. /* inserts a header/data lump immediately after hdr
  86. * returns pointer on success, 0 on error */
  87. struct lump* insert_new_lump_after( struct lump* after, char* new_hdr,
  88. int len, int type)
  89. {
  90. struct lump* tmp;
  91. tmp=pkg_malloc(sizeof(struct lump));
  92. if (tmp==0){
  93. ser_error=E_OUT_OF_MEM;
  94. LOG(L_ERR, "ERROR: insert_new_lump_after: out of memory\n");
  95. return 0;
  96. }
  97. memset(tmp,0,sizeof(struct lump));
  98. tmp->after=after->after;
  99. tmp->type=type;
  100. tmp->op=LUMP_ADD;
  101. tmp->u.value=new_hdr;
  102. tmp->len=len;
  103. after->after=tmp;
  104. return tmp;
  105. }
  106. /* inserts a header/data lump immediately before "before"
  107. * returns pointer on success, 0 on error */
  108. struct lump* insert_new_lump_before( struct lump* before, char* new_hdr,
  109. int len, int type)
  110. {
  111. struct lump* tmp;
  112. tmp=pkg_malloc(sizeof(struct lump));
  113. if (tmp==0){
  114. ser_error=E_OUT_OF_MEM;
  115. LOG(L_ERR,"ERROR: insert_new_lump_before: out of memory\n");
  116. return 0;
  117. }
  118. memset(tmp,0,sizeof(struct lump));
  119. tmp->before=before->before;
  120. tmp->type=type;
  121. tmp->op=LUMP_ADD;
  122. tmp->u.value=new_hdr;
  123. tmp->len=len;
  124. before->before=tmp;
  125. return tmp;
  126. }
  127. /* removes an already existing header/data lump */
  128. struct lump* del_lump(struct lump** list, int offset, int len, int type)
  129. {
  130. struct lump* tmp;
  131. struct lump* prev, *t;
  132. tmp=pkg_malloc(sizeof(struct lump));
  133. if (tmp==0){
  134. LOG(L_ERR, "ERROR: insert_new_lump_before: out of memory\n");
  135. return 0;
  136. }
  137. memset(tmp,0,sizeof(struct lump));
  138. tmp->op=LUMP_DEL;
  139. tmp->type=type;
  140. tmp->u.offset=offset;
  141. tmp->len=len;
  142. prev=0;
  143. for (t=*list;t; prev=t, t=t->next){
  144. /* insert it sorted after offset */
  145. if (((t->op==LUMP_DEL)||(t->op==LUMP_NOP))&&(t->u.offset>offset))
  146. break;
  147. }
  148. tmp->next=t;
  149. if (prev) prev->next=tmp;
  150. else *list=tmp;
  151. return tmp;
  152. }
  153. /* add an anhor */
  154. struct lump* anchor_lump(struct lump** list, int offset, int len, int type)
  155. {
  156. struct lump* tmp;
  157. struct lump* prev, *t;
  158. tmp=pkg_malloc(sizeof(struct lump));
  159. if (tmp==0){
  160. ser_error=E_OUT_OF_MEM;
  161. LOG(L_ERR, "ERROR: insert_new_lump_before: out of memory\n");
  162. return 0;
  163. }
  164. memset(tmp,0,sizeof(struct lump));
  165. tmp->op=LUMP_NOP;
  166. tmp->type=type;
  167. tmp->u.offset=offset;
  168. tmp->len=len;
  169. prev=0;
  170. for (t=*list;t; prev=t, t=t->next){
  171. /* insert it sorted after offset */
  172. if (((t->op==LUMP_DEL)||(t->op==LUMP_NOP))&&(t->u.offset>offset))
  173. break;
  174. }
  175. tmp->next=t;
  176. if (prev) prev->next=tmp;
  177. else *list=tmp;
  178. return tmp;
  179. }
  180. void free_lump(struct lump* lmp)
  181. {
  182. if (lmp && (lmp->op==LUMP_ADD)){
  183. if (lmp->u.value) pkg_free(lmp->u.value);
  184. lmp->u.value=0;
  185. lmp->len=0;
  186. }
  187. }
  188. void free_lump_list(struct lump* l)
  189. {
  190. struct lump* t, *r, *foo,*crt;
  191. t=l;
  192. while(t){
  193. crt=t;
  194. t=t->next;
  195. /*
  196. dangerous recursive clean
  197. if (crt->before) free_lump_list(crt->before);
  198. if (crt->after) free_lump_list(crt->after);
  199. */
  200. /* no more recursion, clean after and before and that's it */
  201. r=crt->before;
  202. while(r){
  203. foo=r; r=r->before;
  204. free_lump(foo);
  205. pkg_free(foo);
  206. }
  207. r=crt->after;
  208. while(r){
  209. foo=r; r=r->after;
  210. free_lump(foo);
  211. pkg_free(foo);
  212. }
  213. /*clean current elem*/
  214. free_lump(crt);
  215. pkg_free(crt);
  216. }
  217. }
  218. /* free (shallow-ly) a lump and its after/before lists */
  219. static void free_shallow_lump( struct lump *l )
  220. {
  221. struct lump *r, *foo;
  222. r=l->before;
  223. while(r){
  224. foo=r; r=r->before;
  225. pkg_free(foo);
  226. }
  227. r=l->after;
  228. while(r){
  229. foo=r; r=r->after;
  230. pkg_free(foo);
  231. }
  232. pkg_free(l);
  233. }
  234. /* duplicate (shallow-ly) a lump list into pkg memory */
  235. static struct lump *dup_lump_list_r( struct lump *l,
  236. enum lump_dir dir, int *error)
  237. {
  238. int deep_error;
  239. struct lump *new_lump;
  240. deep_error=0; /* optimist: assume success in recursion */
  241. /* if at list end, terminate recursion successfully */
  242. if (!l) { *error=0; return 0; }
  243. /* otherwise duplicate current element */
  244. new_lump=pkg_malloc(sizeof(struct lump));
  245. if (!new_lump) { *error=1; return 0; }
  246. memcpy(new_lump, l, sizeof(struct lump));
  247. new_lump->flags=LUMPFLAG_DUPED;
  248. new_lump->next=new_lump->before=new_lump->after=0;
  249. switch(dir) {
  250. case LD_NEXT:
  251. new_lump->before=dup_lump_list_r(l->before,
  252. LD_BEFORE, &deep_error);
  253. if (deep_error) goto deeperror;
  254. new_lump->after=dup_lump_list_r(l->after,
  255. LD_AFTER, &deep_error);
  256. if (deep_error) goto deeperror;
  257. new_lump->next=dup_lump_list_r(l->next,
  258. LD_NEXT, &deep_error);
  259. break;
  260. case LD_BEFORE:
  261. new_lump->before=dup_lump_list_r(l->before,
  262. LD_BEFORE, &deep_error);
  263. break;
  264. case LD_AFTER:
  265. new_lump->after=dup_lump_list_r(l->after,
  266. LD_AFTER, &deep_error);
  267. break;
  268. default:
  269. LOG(L_CRIT, "BUG: dup_limp_list_r: unknown dir: "
  270. "%d\n", dir );
  271. deep_error=1;
  272. }
  273. if (deep_error) goto deeperror;
  274. *error=0;
  275. return new_lump;
  276. deeperror:
  277. LOG(L_ERR, "ERROR: dup_lump_list_r: out of mem\n");
  278. free_shallow_lump(new_lump);
  279. *error=1;
  280. return 0;
  281. }
  282. /* shallow pkg copy of a lump list
  283. *
  284. * if either original list empty or error occur returns, 0
  285. * is returned, pointer to the copy otherwise
  286. */
  287. struct lump* dup_lump_list( struct lump *l )
  288. {
  289. int deep_error;
  290. deep_error=0;
  291. return dup_lump_list_r(l, LD_NEXT, &deep_error);
  292. }
  293. void free_duped_lump_list(struct lump* l)
  294. {
  295. struct lump *r, *foo,*crt;
  296. while(l){
  297. crt=l;
  298. l=l->next;
  299. r=crt->before;
  300. while(r){
  301. foo=r; r=r->before;
  302. /* (+): if a new item was introduced to the shallow-ly
  303. * duped list, remove it completely, preserve it
  304. * othewise (it is still refered by original list)
  305. */
  306. if (foo->flags!=LUMPFLAG_DUPED)
  307. free_lump(foo);
  308. pkg_free(foo);
  309. }
  310. r=crt->after;
  311. while(r){
  312. foo=r; r=r->after;
  313. if (foo->flags!=LUMPFLAG_DUPED) /* (+) ... see above */
  314. free_lump(foo);
  315. pkg_free(foo);
  316. }
  317. /*clean current elem*/
  318. if (crt->flags!=LUMPFLAG_DUPED) /* (+) ... see above */
  319. free_lump(crt);
  320. pkg_free(crt);
  321. }
  322. }