data_lump.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. LM_ERR("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. LM_ERR("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. LM_ERR("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. LM_ERR("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. LM_ERR("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. LM_ERR("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. LM_ERR("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. LM_ERR("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. LM_ERR("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. LM_CRIT("offset exceeds message size (%d > %d) aborting...\n",
  255. offset, msg->len);
  256. abort();
  257. }
  258. if (offset+len>msg->len){
  259. LM_CRIT("offset + len exceeds message size (%d + %d > %d)\n",
  260. offset, len, msg->len);
  261. abort();
  262. }
  263. if (len==0){
  264. LM_WARN("0 len (offset=%d)\n", offset);
  265. }
  266. tmp=pkg_malloc(sizeof(struct lump));
  267. if (tmp==0){
  268. LM_ERR("out of memory\n");
  269. return 0;
  270. }
  271. memset(tmp,0,sizeof(struct lump));
  272. tmp->op=LUMP_DEL;
  273. tmp->type=type;
  274. tmp->u.offset=offset;
  275. tmp->len=len;
  276. prev=0;
  277. /* check to see whether this might be a body lump */
  278. if ((msg->eoh) && (offset> (int)(msg->eoh-msg->buf)))
  279. list=&msg->body_lumps;
  280. else
  281. list=&msg->add_rm;
  282. for (t=*list;t; prev=t, t=t->next){
  283. /* insert it sorted after offset */
  284. if (((t->op==LUMP_DEL)||(t->op==LUMP_NOP))&&(t->u.offset>offset))
  285. break;
  286. }
  287. tmp->next=t;
  288. if (prev) prev->next=tmp;
  289. else *list=tmp;
  290. return tmp;
  291. }
  292. /* add an anchor
  293. * WARNING: this function adds the lump either to the msg->add_rm or
  294. * msg->body_lumps list, depending on the offset being greater than msg->eoh,
  295. * so msg->eoh must be parsed (parse with HDR_EOH) if you think your lump
  296. * might affect the body!! */
  297. struct lump* anchor_lump(struct sip_msg* msg, int offset, int len, enum _hdr_types_t type)
  298. {
  299. struct lump* tmp;
  300. struct lump* prev, *t;
  301. struct lump** list;
  302. /* extra checks */
  303. if (offset>msg->len){
  304. LM_CRIT("offset exceeds message size (%d > %d) aborting...\n",
  305. offset, msg->len);
  306. abort();
  307. }
  308. if (len){
  309. LM_WARN("len !=0 (%d)\n", len);
  310. if (offset+len>msg->len)
  311. LM_WARN("offset + len exceeds message size (%d + %d > %d)\n",
  312. offset, len, msg->len);
  313. }
  314. tmp=pkg_malloc(sizeof(struct lump));
  315. if (tmp==0){
  316. ser_error=E_OUT_OF_MEM;
  317. LM_ERR("out of memory\n");
  318. return 0;
  319. }
  320. memset(tmp,0,sizeof(struct lump));
  321. tmp->op=LUMP_NOP;
  322. tmp->type=type;
  323. tmp->u.offset=offset;
  324. tmp->len=len;
  325. prev=0;
  326. /* check to see whether this might be a body lump */
  327. if ((msg->eoh) && (offset> (int)(msg->eoh-msg->buf)))
  328. list=&msg->body_lumps;
  329. else
  330. list=&msg->add_rm;
  331. for (t=*list;t; prev=t, t=t->next){
  332. /* insert it sorted after offset */
  333. if (((t->op==LUMP_DEL)||(t->op==LUMP_NOP))&&(t->u.offset>offset))
  334. break;
  335. }
  336. tmp->next=t;
  337. if (prev) prev->next=tmp;
  338. else *list=tmp;
  339. return tmp;
  340. }
  341. /* add an anchor
  342. * Similar to anchor_lump() but this function checks whether or not a lump
  343. * has already been added to the same position. If an existing lump is found
  344. * then it is returned without adding a new one and is_ref is set to 1.
  345. *
  346. * WARNING: this function adds the lump either to the msg->add_rm or
  347. * msg->body_lumps list, depending on the offset being greater than msg->eoh,
  348. * so msg->eoh must be parsed (parse with HDR_EOH) if you think your lump
  349. * might affect the body!! */
  350. struct lump* anchor_lump2(struct sip_msg* msg, int offset, int len, enum _hdr_types_t type,
  351. int *is_ref)
  352. {
  353. struct lump* tmp;
  354. struct lump* prev, *t;
  355. struct lump** list;
  356. /* extra checks */
  357. if (offset>msg->len){
  358. LM_CRIT("offset exceeds message size (%d > %d) aborting...\n",
  359. offset, msg->len);
  360. abort();
  361. }
  362. if (len){
  363. LM_WARN("len !=0 (%d)\n", len);
  364. if (offset+len>msg->len)
  365. LM_WARN("offset + len exceeds message size (%d + %d > %d)\n",
  366. offset, len, msg->len);
  367. }
  368. prev=0;
  369. /* check to see whether this might be a body lump */
  370. if ((msg->eoh) && (offset> (int)(msg->eoh-msg->buf)))
  371. list=&msg->body_lumps;
  372. else
  373. list=&msg->add_rm;
  374. for (t=*list;t; prev=t, t=t->next){
  375. /* insert it sorted after offset */
  376. if (((t->op==LUMP_DEL)||(t->op==LUMP_NOP))&&(t->u.offset>=offset))
  377. break;
  378. }
  379. if (t && (t->u.offset==offset)) {
  380. /* A lump with the same offset is found */
  381. *is_ref=1;
  382. return t;
  383. }
  384. tmp=pkg_malloc(sizeof(struct lump));
  385. if (tmp==0){
  386. ser_error=E_OUT_OF_MEM;
  387. LM_ERR("out of memory\n");
  388. return 0;
  389. }
  390. memset(tmp,0,sizeof(struct lump));
  391. tmp->op=LUMP_NOP;
  392. tmp->type=type;
  393. tmp->u.offset=offset;
  394. tmp->len=len;
  395. tmp->next=t;
  396. if (prev) prev->next=tmp;
  397. else *list=tmp;
  398. *is_ref=0;
  399. return tmp;
  400. }
  401. void free_lump(struct lump* lmp)
  402. {
  403. if (lmp && (lmp->op==LUMP_ADD)){
  404. if (lmp->u.value){
  405. if (lmp->flags &(LUMPFLAG_DUPED|LUMPFLAG_SHMEM)){
  406. LM_CRIT("non free-able lump: %p flags=%x\n", lmp, lmp->flags);
  407. abort();
  408. }else{
  409. pkg_free(lmp->u.value);
  410. lmp->u.value=0;
  411. lmp->len=0;
  412. }
  413. }
  414. }
  415. }
  416. void free_lump_list(struct lump* l)
  417. {
  418. struct lump* t, *r, *foo,*crt;
  419. t=l;
  420. while(t){
  421. crt=t;
  422. t=t->next;
  423. /*
  424. dangerous recursive clean
  425. if (crt->before) free_lump_list(crt->before);
  426. if (crt->after) free_lump_list(crt->after);
  427. */
  428. /* no more recursion, clean after and before and that's it */
  429. r=crt->before;
  430. while(r){
  431. foo=r; r=r->before;
  432. free_lump(foo);
  433. pkg_free(foo);
  434. }
  435. r=crt->after;
  436. while(r){
  437. foo=r; r=r->after;
  438. free_lump(foo);
  439. pkg_free(foo);
  440. }
  441. /*clean current elem*/
  442. free_lump(crt);
  443. pkg_free(crt);
  444. }
  445. }
  446. /* free (shallow-ly) a lump and its after/before lists */
  447. static void free_shallow_lump( struct lump *l )
  448. {
  449. struct lump *r, *foo;
  450. r=l->before;
  451. while(r){
  452. foo=r; r=r->before;
  453. pkg_free(foo);
  454. }
  455. r=l->after;
  456. while(r){
  457. foo=r; r=r->after;
  458. pkg_free(foo);
  459. }
  460. pkg_free(l);
  461. }
  462. /* duplicate (shallow-ly) a lump list into pkg memory */
  463. static struct lump *dup_lump_list_r( struct lump *l,
  464. enum lump_dir dir, int *error)
  465. {
  466. int deep_error;
  467. struct lump *new_lump;
  468. deep_error=0; /* optimist: assume success in recursion */
  469. /* if at list end, terminate recursion successfully */
  470. if (!l) { *error=0; return 0; }
  471. /* otherwise duplicate current element */
  472. new_lump=pkg_malloc(sizeof(struct lump));
  473. if (!new_lump) { *error=1; return 0; }
  474. memcpy(new_lump, l, sizeof(struct lump));
  475. new_lump->flags=LUMPFLAG_DUPED;
  476. new_lump->next=new_lump->before=new_lump->after=0;
  477. switch(dir) {
  478. case LD_NEXT:
  479. new_lump->before=dup_lump_list_r(l->before,
  480. LD_BEFORE, &deep_error);
  481. if (deep_error) goto deeperror;
  482. new_lump->after=dup_lump_list_r(l->after,
  483. LD_AFTER, &deep_error);
  484. if (deep_error) goto deeperror;
  485. new_lump->next=dup_lump_list_r(l->next,
  486. LD_NEXT, &deep_error);
  487. break;
  488. case LD_BEFORE:
  489. new_lump->before=dup_lump_list_r(l->before,
  490. LD_BEFORE, &deep_error);
  491. break;
  492. case LD_AFTER:
  493. new_lump->after=dup_lump_list_r(l->after,
  494. LD_AFTER, &deep_error);
  495. break;
  496. default:
  497. LM_CRIT("unknown dir: %d\n", dir );
  498. deep_error=1;
  499. }
  500. if (deep_error) goto deeperror;
  501. *error=0;
  502. return new_lump;
  503. deeperror:
  504. LM_ERR("out of mem\n");
  505. free_shallow_lump(new_lump);
  506. *error=1;
  507. return 0;
  508. }
  509. /* shallow pkg copy of a lump list
  510. *
  511. * if either original list empty or error occur returns, 0
  512. * is returned, pointer to the copy otherwise
  513. */
  514. struct lump* dup_lump_list( struct lump *l )
  515. {
  516. int deep_error;
  517. deep_error=0;
  518. return dup_lump_list_r(l, LD_NEXT, &deep_error);
  519. }
  520. void free_duped_lump_list(struct lump* l)
  521. {
  522. struct lump *r, *foo,*crt;
  523. while(l){
  524. crt=l;
  525. l=l->next;
  526. r=crt->before;
  527. while(r){
  528. foo=r; r=r->before;
  529. /* (+): if a new item was introduced to the shallow-ly
  530. * duped list, remove it completely, preserve it
  531. * otherwise (it is still referred by original list)
  532. */
  533. if (foo->flags!=LUMPFLAG_DUPED)
  534. free_lump(foo);
  535. pkg_free(foo);
  536. }
  537. r=crt->after;
  538. while(r){
  539. foo=r; r=r->after;
  540. if (foo->flags!=LUMPFLAG_DUPED) /* (+) ... see above */
  541. free_lump(foo);
  542. pkg_free(foo);
  543. }
  544. /*clean current elem*/
  545. if (crt->flags!=LUMPFLAG_DUPED) /* (+) ... see above */
  546. free_lump(crt);
  547. pkg_free(crt);
  548. }
  549. }
  550. void del_nonshm_lump( struct lump** lump_list )
  551. {
  552. struct lump *r, *foo, *crt, **prev, *prev_r;
  553. prev = lump_list;
  554. crt = *lump_list;
  555. while (crt) {
  556. if (!(crt->flags&LUMPFLAG_SHMEM)) {
  557. /* unlink it */
  558. foo = crt;
  559. crt = crt->next;
  560. foo->next = 0;
  561. /* update the 'next' link of the previous lump */
  562. *prev = crt;
  563. /* entire before/after list must be removed */
  564. free_lump_list( foo );
  565. } else {
  566. /* check on before and prev list for non-shmem lumps */
  567. r = crt->after;
  568. prev_r = crt;
  569. while(r){
  570. foo=r; r=r->after;
  571. if (!(foo->flags&LUMPFLAG_SHMEM)) {
  572. prev_r->after = r;
  573. free_lump(foo);
  574. pkg_free(foo);
  575. } else {
  576. prev_r = foo;
  577. }
  578. }
  579. /* before */
  580. r = crt->before;
  581. prev_r = crt;
  582. while(r){
  583. foo=r; r=r->before;
  584. if (!(foo->flags&LUMPFLAG_SHMEM)) {
  585. prev_r->before = r;
  586. free_lump(foo);
  587. pkg_free(foo);
  588. } else {
  589. prev_r = foo;
  590. }
  591. }
  592. /* go to next lump */
  593. prev = &(crt->next);
  594. crt = crt->next;
  595. }
  596. }
  597. }
  598. unsigned int count_applied_lumps(struct lump *ll, int type)
  599. {
  600. unsigned int n = 0;
  601. struct lump *l = 0;
  602. for(l=ll; l; l=l->next) {
  603. if (l->op==LUMP_NOP && l->type==type) {
  604. if (l->after && l->after->op==LUMP_ADD_OPT) {
  605. if (LUMP_IS_COND_TRUE(l->after)) {
  606. n++;
  607. }
  608. } else {
  609. n++;
  610. }
  611. }
  612. }
  613. return n;
  614. }
  615. int remove_lump(sip_msg_t *msg, struct lump *l)
  616. {
  617. struct lump *t = NULL;
  618. struct lump *prev = NULL;
  619. struct lump **list = NULL;
  620. list=&msg->add_rm;
  621. for (t=*list; t; prev=t, t=t->next) {
  622. if(t==l)
  623. break;
  624. }
  625. if(t==NULL) {
  626. list=&msg->body_lumps;
  627. for (t=*list; t; prev=t, t=t->next) {
  628. if(t==l)
  629. break;
  630. }
  631. }
  632. if(t!=NULL) {
  633. if(prev==NULL) {
  634. *list = t->next;
  635. } else {
  636. prev->next = t->next;
  637. }
  638. free_lump(t);
  639. return 1;
  640. }
  641. return 0;
  642. }