data_lump.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. /* $Id$
  2. *
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of SIP-router, a free SIP server.
  7. *
  8. * SIP-router 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. * SIP-router 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-01-19 support for duplication lump lists added (jiri)
  25. * 2003-03-31 added subst lumps --they expand in ip addr, port a.s.o (andrei)
  26. * 2003-04-01 added conditional lump support functions (andrei)
  27. * 2003-10-20 anchor_lump & del_lump will automatically choose the lump list
  28. * based on msg->eoh comparisons (andrei)
  29. * 2003-10-28 added extra checks (paranoia) for {anchor,del}_lump (andrei)
  30. * 2005-03-24 the type of type attribute changed to enum _hdr_types_t (janakj)
  31. */
  32. /*!
  33. * \file
  34. * \brief SIP-router core ::
  35. * \ingroup core
  36. * Module: \ref core
  37. */
  38. #include "data_lump.h"
  39. #include "dprint.h"
  40. #include "mem/mem.h"
  41. #include "globals.h"
  42. #include "error.h"
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #ifdef DEBUG_DMALLOC
  46. #include <dmalloc.h>
  47. #endif
  48. /* WARNING: all lump add/insert operations expect a pkg_malloc'ed char*
  49. * pointer the will be DEALLOCATED when the sip_msg is destroyed! */
  50. enum lump_dir { LD_NEXT, LD_BEFORE, LD_AFTER };
  51. /* adds a header to the end
  52. * returns pointer on success, 0 on error */
  53. struct lump* append_new_lump(struct lump** list, char* new_hdr,
  54. int len, enum _hdr_types_t type)
  55. {
  56. struct lump** t;
  57. struct lump* tmp;
  58. for (t=list;*t;t=&((*t)->next));
  59. tmp=pkg_malloc(sizeof(struct lump));
  60. if (tmp==0){
  61. LOG(L_ERR, "ERROR: append_new_lump: out of memory\n");
  62. return 0;
  63. }
  64. memset(tmp,0,sizeof(struct lump));
  65. tmp->type=type;
  66. tmp->op=LUMP_ADD;
  67. tmp->u.value=new_hdr;
  68. tmp->len=len;
  69. *t=tmp;
  70. return tmp;
  71. }
  72. /* adds a header right after an anchor point if exists
  73. * returns pointer on success, 0 on error */
  74. struct lump* add_new_lump(struct lump** list, char* new_hdr,
  75. int len, enum _hdr_types_t type)
  76. {
  77. struct lump** t;
  78. struct lump* tmp;
  79. t = (*list) ? &((*list)->next) : list;
  80. tmp=pkg_malloc(sizeof(struct lump));
  81. if (tmp==0){
  82. LOG(L_ERR, "ERROR: add_new_lump: out of memory\n");
  83. return 0;
  84. }
  85. memset(tmp,0,sizeof(struct lump));
  86. tmp->type=type;
  87. tmp->op=LUMP_ADD;
  88. tmp->u.value=new_hdr;
  89. tmp->len=len;
  90. tmp->next=*t;
  91. *t=tmp;
  92. return tmp;
  93. }
  94. /* inserts a header to the beginning
  95. * returns pointer if success, 0 on error */
  96. struct lump* insert_new_lump(struct lump** list, char* new_hdr,
  97. int len, enum _hdr_types_t type)
  98. {
  99. struct lump* tmp;
  100. tmp=pkg_malloc(sizeof(struct lump));
  101. if (tmp==0){
  102. LOG(L_ERR, "ERROR: insert_new_lump: out of memory\n");
  103. return 0;
  104. }
  105. memset(tmp,0,sizeof(struct lump));
  106. tmp->next=*list;
  107. tmp->type=type;
  108. tmp->op=LUMP_ADD;
  109. tmp->u.value=new_hdr;
  110. tmp->len=len;
  111. *list=tmp;
  112. return tmp;
  113. }
  114. /* inserts a header/data lump immediately after hdr
  115. * returns pointer on success, 0 on error */
  116. struct lump* insert_new_lump_after( struct lump* after, char* new_hdr,
  117. int len, enum _hdr_types_t type)
  118. {
  119. struct lump* tmp;
  120. tmp=pkg_malloc(sizeof(struct lump));
  121. if (tmp==0){
  122. ser_error=E_OUT_OF_MEM;
  123. LOG(L_ERR, "ERROR: insert_new_lump_after: out of memory\n");
  124. return 0;
  125. }
  126. memset(tmp,0,sizeof(struct lump));
  127. tmp->after=after->after;
  128. tmp->type=type;
  129. tmp->op=LUMP_ADD;
  130. tmp->u.value=new_hdr;
  131. tmp->len=len;
  132. after->after=tmp;
  133. return tmp;
  134. }
  135. /* inserts a header/data lump immediately before "before"
  136. * returns pointer on success, 0 on error */
  137. struct lump* insert_new_lump_before( struct lump* before, char* new_hdr,
  138. int len, enum _hdr_types_t type)
  139. {
  140. struct lump* tmp;
  141. tmp=pkg_malloc(sizeof(struct lump));
  142. if (tmp==0){
  143. ser_error=E_OUT_OF_MEM;
  144. LOG(L_ERR,"ERROR: insert_new_lump_before: out of memory\n");
  145. return 0;
  146. }
  147. memset(tmp,0,sizeof(struct lump));
  148. tmp->before=before->before;
  149. tmp->type=type;
  150. tmp->op=LUMP_ADD;
  151. tmp->u.value=new_hdr;
  152. tmp->len=len;
  153. before->before=tmp;
  154. return tmp;
  155. }
  156. /* inserts a subst lump immediately after hdr
  157. * returns pointer on success, 0 on error */
  158. struct lump* insert_subst_lump_after( struct lump* after, enum lump_subst subst,
  159. enum _hdr_types_t type)
  160. {
  161. struct lump* tmp;
  162. tmp=pkg_malloc(sizeof(struct lump));
  163. if (tmp==0){
  164. ser_error=E_OUT_OF_MEM;
  165. LOG(L_ERR, "ERROR: insert_new_lump_after: out of memory\n");
  166. return 0;
  167. }
  168. memset(tmp,0,sizeof(struct lump));
  169. tmp->after=after->after;
  170. tmp->type=type;
  171. tmp->op=LUMP_ADD_SUBST;
  172. tmp->u.subst=subst;
  173. tmp->len=0;
  174. after->after=tmp;
  175. return tmp;
  176. }
  177. /* inserts a subst lump immediately before "before"
  178. * returns pointer on success, 0 on error */
  179. struct lump* insert_subst_lump_before( struct lump* before,
  180. enum lump_subst subst,
  181. enum _hdr_types_t type)
  182. {
  183. struct lump* tmp;
  184. tmp=pkg_malloc(sizeof(struct lump));
  185. if (tmp==0){
  186. ser_error=E_OUT_OF_MEM;
  187. LOG(L_ERR,"ERROR: insert_new_lump_before: out of memory\n");
  188. return 0;
  189. }
  190. memset(tmp,0,sizeof(struct lump));
  191. tmp->before=before->before;
  192. tmp->type=type;
  193. tmp->op=LUMP_ADD_SUBST;
  194. tmp->u.subst=subst;
  195. tmp->len=0;
  196. before->before=tmp;
  197. return tmp;
  198. }
  199. /* inserts a cond lump immediately after hdr
  200. * returns pointer on success, 0 on error */
  201. struct lump* insert_cond_lump_after( struct lump* after, enum lump_conditions c,
  202. enum _hdr_types_t type)
  203. {
  204. struct lump* tmp;
  205. tmp=pkg_malloc(sizeof(struct lump));
  206. if (tmp==0){
  207. ser_error=E_OUT_OF_MEM;
  208. LOG(L_ERR, "ERROR: insert_new_lump_after: out of memory\n");
  209. return 0;
  210. }
  211. memset(tmp,0,sizeof(struct lump));
  212. tmp->after=after->after;
  213. tmp->type=type;
  214. tmp->op=LUMP_ADD_OPT;
  215. tmp->u.cond=c;
  216. tmp->len=0;
  217. after->after=tmp;
  218. return tmp;
  219. }
  220. /* inserts a conditional lump immediately before "before"
  221. * returns pointer on success, 0 on error */
  222. struct lump* insert_cond_lump_before( struct lump* before,
  223. enum lump_conditions c,
  224. enum _hdr_types_t type)
  225. {
  226. struct lump* tmp;
  227. tmp=pkg_malloc(sizeof(struct lump));
  228. if (tmp==0){
  229. ser_error=E_OUT_OF_MEM;
  230. LOG(L_ERR,"ERROR: insert_new_lump_before: out of memory\n");
  231. return 0;
  232. }
  233. memset(tmp,0,sizeof(struct lump));
  234. tmp->before=before->before;
  235. tmp->type=type;
  236. tmp->op=LUMP_ADD_OPT;
  237. tmp->u.cond=c;
  238. tmp->len=0;
  239. before->before=tmp;
  240. return tmp;
  241. }
  242. /* removes an already existing header/data lump */
  243. /* WARNING: this function adds the lump either to the msg->add_rm or
  244. * msg->body_lumps list, depending on the offset being greater than msg->eoh,
  245. * so msg->eoh must be parsed (parse with HDR_EOH) if you think your lump
  246. * might affect the body!! */
  247. struct lump* del_lump(struct sip_msg* msg, int offset, int len, enum _hdr_types_t type)
  248. {
  249. struct lump* tmp;
  250. struct lump* prev, *t;
  251. struct lump** list;
  252. /* extra checks */
  253. if (offset>msg->len){
  254. LOG(L_CRIT, "BUG: del_lump: offset exceeds message size (%d > %d)"
  255. " aborting...\n", offset, msg->len);
  256. abort();
  257. }
  258. if (offset+len>msg->len){
  259. LOG(L_CRIT, " BUG: del_lump: offset + len exceeds message"
  260. " size (%d + %d > %d)\n", offset, len, msg->len);
  261. abort();
  262. }
  263. if (len==0){
  264. LOG(L_WARN, "WARNING: del_lump: called with 0 len (offset =%d)\n",
  265. offset);
  266. }
  267. tmp=pkg_malloc(sizeof(struct lump));
  268. if (tmp==0){
  269. LOG(L_ERR, "ERROR: del_lump: out of memory\n");
  270. return 0;
  271. }
  272. memset(tmp,0,sizeof(struct lump));
  273. tmp->op=LUMP_DEL;
  274. tmp->type=type;
  275. tmp->u.offset=offset;
  276. tmp->len=len;
  277. prev=0;
  278. /* check to see whether this might be a body lump */
  279. if ((msg->eoh) && (offset> (int)(msg->eoh-msg->buf)))
  280. list=&msg->body_lumps;
  281. else
  282. list=&msg->add_rm;
  283. for (t=*list;t; prev=t, t=t->next){
  284. /* insert it sorted after offset */
  285. if (((t->op==LUMP_DEL)||(t->op==LUMP_NOP))&&(t->u.offset>offset))
  286. break;
  287. }
  288. tmp->next=t;
  289. if (prev) prev->next=tmp;
  290. else *list=tmp;
  291. return tmp;
  292. }
  293. /* add an anchor
  294. * WARNING: this function adds the lump either to the msg->add_rm or
  295. * msg->body_lumps list, depending on the offset being greater than msg->eoh,
  296. * so msg->eoh must be parsed (parse with HDR_EOH) if you think your lump
  297. * might affect the body!! */
  298. struct lump* anchor_lump(struct sip_msg* msg, int offset, int len, enum _hdr_types_t type)
  299. {
  300. struct lump* tmp;
  301. struct lump* prev, *t;
  302. struct lump** list;
  303. /* extra checks */
  304. if (offset>msg->len){
  305. LOG(L_CRIT, "BUG: anchor_lump: offset exceeds message size (%d > %d)"
  306. " aborting...\n", offset, msg->len);
  307. abort();
  308. }
  309. if (len){
  310. LOG(L_WARN, "WARNING: anchor_lump: called with len !=0 (%d)\n", len);
  311. if (offset+len>msg->len)
  312. LOG(L_WARN, "WARNING: anchor_lump: offset + len exceeds message"
  313. " size (%d + %d > %d)\n", offset, len, msg->len);
  314. }
  315. tmp=pkg_malloc(sizeof(struct lump));
  316. if (tmp==0){
  317. ser_error=E_OUT_OF_MEM;
  318. LOG(L_ERR, "ERROR: anchor_lump: out of memory\n");
  319. return 0;
  320. }
  321. memset(tmp,0,sizeof(struct lump));
  322. tmp->op=LUMP_NOP;
  323. tmp->type=type;
  324. tmp->u.offset=offset;
  325. tmp->len=len;
  326. prev=0;
  327. /* check to see whether this might be a body lump */
  328. if ((msg->eoh) && (offset> (int)(msg->eoh-msg->buf)))
  329. list=&msg->body_lumps;
  330. else
  331. list=&msg->add_rm;
  332. for (t=*list;t; prev=t, t=t->next){
  333. /* insert it sorted after offset */
  334. if (((t->op==LUMP_DEL)||(t->op==LUMP_NOP))&&(t->u.offset>offset))
  335. break;
  336. }
  337. tmp->next=t;
  338. if (prev) prev->next=tmp;
  339. else *list=tmp;
  340. return tmp;
  341. }
  342. /* add an anchor
  343. * Similar to anchor_lump() but this function checks whether or not a lump
  344. * has already been added to the same position. If an existing lump is found
  345. * then it is returned without adding a new one and is_ref is set to 1.
  346. *
  347. * WARNING: this function adds the lump either to the msg->add_rm or
  348. * msg->body_lumps list, depending on the offset being greater than msg->eoh,
  349. * so msg->eoh must be parsed (parse with HDR_EOH) if you think your lump
  350. * might affect the body!! */
  351. struct lump* anchor_lump2(struct sip_msg* msg, int offset, int len, enum _hdr_types_t type,
  352. int *is_ref)
  353. {
  354. struct lump* tmp;
  355. struct lump* prev, *t;
  356. struct lump** list;
  357. /* extra checks */
  358. if (offset>msg->len){
  359. LOG(L_CRIT, "BUG: anchor_lump2: offset exceeds message size (%d > %d)"
  360. " aborting...\n", offset, msg->len);
  361. abort();
  362. }
  363. if (len){
  364. LOG(L_WARN, "WARNING: anchor_lump2: called with len !=0 (%d)\n", len);
  365. if (offset+len>msg->len)
  366. LOG(L_WARN, "WARNING: anchor_lump2: offset + len exceeds message"
  367. " size (%d + %d > %d)\n", offset, len, msg->len);
  368. }
  369. prev=0;
  370. /* check to see whether this might be a body lump */
  371. if ((msg->eoh) && (offset> (int)(msg->eoh-msg->buf)))
  372. list=&msg->body_lumps;
  373. else
  374. list=&msg->add_rm;
  375. for (t=*list;t; prev=t, t=t->next){
  376. /* insert it sorted after offset */
  377. if (((t->op==LUMP_DEL)||(t->op==LUMP_NOP))&&(t->u.offset>=offset))
  378. break;
  379. }
  380. if (t && (t->u.offset==offset)) {
  381. /* A lump with the same offset is found */
  382. *is_ref=1;
  383. return t;
  384. }
  385. tmp=pkg_malloc(sizeof(struct lump));
  386. if (tmp==0){
  387. ser_error=E_OUT_OF_MEM;
  388. LOG(L_ERR, "ERROR: anchor_lump2: out of memory\n");
  389. return 0;
  390. }
  391. memset(tmp,0,sizeof(struct lump));
  392. tmp->op=LUMP_NOP;
  393. tmp->type=type;
  394. tmp->u.offset=offset;
  395. tmp->len=len;
  396. tmp->next=t;
  397. if (prev) prev->next=tmp;
  398. else *list=tmp;
  399. *is_ref=0;
  400. return tmp;
  401. }
  402. void free_lump(struct lump* lmp)
  403. {
  404. if (lmp && (lmp->op==LUMP_ADD)){
  405. if (lmp->u.value){
  406. if (lmp->flags &(LUMPFLAG_DUPED|LUMPFLAG_SHMEM)){
  407. LOG(L_CRIT, "BUG: free_lump: called on a not free-able lump:"
  408. "%p flags=%x\n", lmp, lmp->flags);
  409. abort();
  410. }else{
  411. pkg_free(lmp->u.value);
  412. lmp->u.value=0;
  413. lmp->len=0;
  414. }
  415. }
  416. }
  417. }
  418. void free_lump_list(struct lump* l)
  419. {
  420. struct lump* t, *r, *foo,*crt;
  421. t=l;
  422. while(t){
  423. crt=t;
  424. t=t->next;
  425. /*
  426. dangerous recursive clean
  427. if (crt->before) free_lump_list(crt->before);
  428. if (crt->after) free_lump_list(crt->after);
  429. */
  430. /* no more recursion, clean after and before and that's it */
  431. r=crt->before;
  432. while(r){
  433. foo=r; r=r->before;
  434. free_lump(foo);
  435. pkg_free(foo);
  436. }
  437. r=crt->after;
  438. while(r){
  439. foo=r; r=r->after;
  440. free_lump(foo);
  441. pkg_free(foo);
  442. }
  443. /*clean current elem*/
  444. free_lump(crt);
  445. pkg_free(crt);
  446. }
  447. }
  448. /* free (shallow-ly) a lump and its after/before lists */
  449. static void free_shallow_lump( struct lump *l )
  450. {
  451. struct lump *r, *foo;
  452. r=l->before;
  453. while(r){
  454. foo=r; r=r->before;
  455. pkg_free(foo);
  456. }
  457. r=l->after;
  458. while(r){
  459. foo=r; r=r->after;
  460. pkg_free(foo);
  461. }
  462. pkg_free(l);
  463. }
  464. /* duplicate (shallow-ly) a lump list into pkg memory */
  465. static struct lump *dup_lump_list_r( struct lump *l,
  466. enum lump_dir dir, int *error)
  467. {
  468. int deep_error;
  469. struct lump *new_lump;
  470. deep_error=0; /* optimist: assume success in recursion */
  471. /* if at list end, terminate recursion successfully */
  472. if (!l) { *error=0; return 0; }
  473. /* otherwise duplicate current element */
  474. new_lump=pkg_malloc(sizeof(struct lump));
  475. if (!new_lump) { *error=1; return 0; }
  476. memcpy(new_lump, l, sizeof(struct lump));
  477. new_lump->flags=LUMPFLAG_DUPED;
  478. new_lump->next=new_lump->before=new_lump->after=0;
  479. switch(dir) {
  480. case LD_NEXT:
  481. new_lump->before=dup_lump_list_r(l->before,
  482. LD_BEFORE, &deep_error);
  483. if (deep_error) goto deeperror;
  484. new_lump->after=dup_lump_list_r(l->after,
  485. LD_AFTER, &deep_error);
  486. if (deep_error) goto deeperror;
  487. new_lump->next=dup_lump_list_r(l->next,
  488. LD_NEXT, &deep_error);
  489. break;
  490. case LD_BEFORE:
  491. new_lump->before=dup_lump_list_r(l->before,
  492. LD_BEFORE, &deep_error);
  493. break;
  494. case LD_AFTER:
  495. new_lump->after=dup_lump_list_r(l->after,
  496. LD_AFTER, &deep_error);
  497. break;
  498. default:
  499. LOG(L_CRIT, "BUG: dup_limp_list_r: unknown dir: "
  500. "%d\n", dir );
  501. deep_error=1;
  502. }
  503. if (deep_error) goto deeperror;
  504. *error=0;
  505. return new_lump;
  506. deeperror:
  507. LOG(L_ERR, "ERROR: dup_lump_list_r: out of mem\n");
  508. free_shallow_lump(new_lump);
  509. *error=1;
  510. return 0;
  511. }
  512. /* shallow pkg copy of a lump list
  513. *
  514. * if either original list empty or error occur returns, 0
  515. * is returned, pointer to the copy otherwise
  516. */
  517. struct lump* dup_lump_list( struct lump *l )
  518. {
  519. int deep_error;
  520. deep_error=0;
  521. return dup_lump_list_r(l, LD_NEXT, &deep_error);
  522. }
  523. void free_duped_lump_list(struct lump* l)
  524. {
  525. struct lump *r, *foo,*crt;
  526. while(l){
  527. crt=l;
  528. l=l->next;
  529. r=crt->before;
  530. while(r){
  531. foo=r; r=r->before;
  532. /* (+): if a new item was introduced to the shallow-ly
  533. * duped list, remove it completely, preserve it
  534. * otherwise (it is still referred by original list)
  535. */
  536. if (foo->flags!=LUMPFLAG_DUPED)
  537. free_lump(foo);
  538. pkg_free(foo);
  539. }
  540. r=crt->after;
  541. while(r){
  542. foo=r; r=r->after;
  543. if (foo->flags!=LUMPFLAG_DUPED) /* (+) ... see above */
  544. free_lump(foo);
  545. pkg_free(foo);
  546. }
  547. /*clean current elem*/
  548. if (crt->flags!=LUMPFLAG_DUPED) /* (+) ... see above */
  549. free_lump(crt);
  550. pkg_free(crt);
  551. }
  552. }
  553. void del_nonshm_lump( struct lump** lump_list )
  554. {
  555. struct lump *r, *foo, *crt, **prev, *prev_r;
  556. prev = lump_list;
  557. crt = *lump_list;
  558. while (crt) {
  559. if (!(crt->flags&LUMPFLAG_SHMEM)) {
  560. /* unlink it */
  561. foo = crt;
  562. crt = crt->next;
  563. foo->next = 0;
  564. /* update the 'next' link of the previous lump */
  565. *prev = crt;
  566. /* entire before/after list must be removed */
  567. free_lump_list( foo );
  568. } else {
  569. /* check on before and prev list for non-shmem lumps */
  570. r = crt->after;
  571. prev_r = crt;
  572. while(r){
  573. foo=r; r=r->after;
  574. if (!(foo->flags&LUMPFLAG_SHMEM)) {
  575. prev_r->after = r;
  576. free_lump(foo);
  577. pkg_free(foo);
  578. } else {
  579. prev_r = foo;
  580. }
  581. }
  582. /* before */
  583. r = crt->before;
  584. prev_r = crt;
  585. while(r){
  586. foo=r; r=r->before;
  587. if (!(foo->flags&LUMPFLAG_SHMEM)) {
  588. prev_r->before = r;
  589. free_lump(foo);
  590. pkg_free(foo);
  591. } else {
  592. prev_r = foo;
  593. }
  594. }
  595. /* go to next lump */
  596. prev = &(crt->next);
  597. crt = crt->next;
  598. }
  599. }
  600. }
  601. unsigned int count_applied_lumps(struct lump *ll, int type)
  602. {
  603. unsigned int n = 0;
  604. struct lump *l = 0;
  605. for(l=ll; l; l=l->next) {
  606. if (l->op==LUMP_NOP && l->type==type) {
  607. if (l->after && l->after->op==LUMP_ADD_OPT) {
  608. if (LUMP_IS_COND_TRUE(l->after)) {
  609. n++;
  610. }
  611. } else {
  612. n++;
  613. }
  614. }
  615. }
  616. return n;
  617. }
  618. int remove_lump(sip_msg_t *msg, struct lump *l)
  619. {
  620. struct lump *t = NULL;
  621. struct lump *prev = NULL;
  622. struct lump **list = NULL;
  623. list=&msg->add_rm;
  624. for (t=*list; t; prev=t, t=t->next) {
  625. if(t==l)
  626. break;
  627. }
  628. if(t==NULL) {
  629. list=&msg->body_lumps;
  630. for (t=*list; t; prev=t, t=t->next) {
  631. if(t==l)
  632. break;
  633. }
  634. }
  635. if(t!=NULL) {
  636. if(prev==NULL) {
  637. *list = t->next;
  638. } else {
  639. prev->next = t->next;
  640. }
  641. free_lump(t);
  642. return 1;
  643. }
  644. return 0;
  645. }