data_lump.c
6a0f4382
 /*
53c7e0f1
  * Copyright (C) 2001-2003 FhG Fokus
7dd0b342
  *
88693a29
  * This file is part of Kamailio, a free SIP server.
7dd0b342
  *
88693a29
  * Kamailio is free software; you can redistribute it and/or modify
7dd0b342
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version
  *
88693a29
  * Kamailio is distributed in the hope that it will be useful,
7dd0b342
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License 
  * along with this program; if not, write to the Free Software 
9e1ff448
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
4d93f145
  *
831faabf
  */
1d0661db
 /*!
  * \file
88693a29
  * \brief Kamailio core :: Data lumps
1d0661db
  * \ingroup core
  * Module: \ref core
  */
 
831faabf
 
7dd0b342
 
831faabf
 #include "data_lump.h"
 #include "dprint.h"
dda9dab1
 #include "mem/mem.h"
1400b772
 #include "globals.h"
 #include "error.h"
831faabf
 
 #include <stdlib.h>
e22bbdb8
 #include <string.h>
831faabf
 
 #ifdef DEBUG_DMALLOC
 #include <dmalloc.h>
 #endif
 
ab130758
 /* WARNING: all lump add/insert operations expect a pkg_malloc'ed char* 
e61f4ba3
  * pointer the will be DEALLOCATED when the sip_msg is destroyed! */
 
4d93f145
 enum lump_dir { LD_NEXT, LD_BEFORE, LD_AFTER };
831faabf
 
 /* adds a header to the end
  * returns  pointer on success, 0 on error */
 struct lump* append_new_lump(struct lump** list, char* new_hdr,
182fea0e
 							 int len, enum _hdr_types_t type)
831faabf
 {
 	struct lump** t;
 	struct lump* tmp;
 	
 	for (t=list;*t;t=&((*t)->next));
 
22d4aa5d
 	tmp=pkg_malloc(sizeof(struct lump));
831faabf
 	if (tmp==0){
7fb04ae6
 		LM_ERR("out of memory\n");
831faabf
 		return 0;
 	}
 		
 	memset(tmp,0,sizeof(struct lump));
 	tmp->type=type;
 	tmp->op=LUMP_ADD;
 	tmp->u.value=new_hdr;
 	tmp->len=len;
 	*t=tmp;
 	return tmp;
 }
 
b663c49c
 /* adds a header right after an anchor point if exists
  * returns  pointer on success, 0 on error */
 struct lump* add_new_lump(struct lump** list, char* new_hdr,
 							 int len, enum _hdr_types_t type)
 {
 	struct lump** t;
 	struct lump* tmp;
 	
 
 	t = (*list) ? &((*list)->next) : list;
 
 	tmp=pkg_malloc(sizeof(struct lump));
 	if (tmp==0){
7fb04ae6
 		LM_ERR("out of memory\n");
b663c49c
 		return 0;
 	}
 		
 	memset(tmp,0,sizeof(struct lump));
 	tmp->type=type;
 	tmp->op=LUMP_ADD;
 	tmp->u.value=new_hdr;
 	tmp->len=len;
 	tmp->next=*t;
 	*t=tmp;
 	return tmp;
 }
 
831faabf
 
 
 /* inserts a header to the beginning 
  * returns pointer if success, 0 on error */
 struct lump* insert_new_lump(struct lump** list, char* new_hdr,
182fea0e
 								int len, enum _hdr_types_t type)
831faabf
 {
 	struct lump* tmp;
 
22d4aa5d
 	tmp=pkg_malloc(sizeof(struct lump));
831faabf
 	if (tmp==0){
7fb04ae6
 		LM_ERR("out of memory\n");
831faabf
 		return 0;
 	}
 	memset(tmp,0,sizeof(struct lump));
 	tmp->next=*list;
 	tmp->type=type;
 	tmp->op=LUMP_ADD;
 	tmp->u.value=new_hdr;
 	tmp->len=len;
 	*list=tmp;
 	return tmp;
 }
 
 
 
 /* inserts a  header/data lump immediately after hdr 
  * returns pointer on success, 0 on error */
 struct lump* insert_new_lump_after( struct lump* after, char* new_hdr,
182fea0e
 							int len, enum _hdr_types_t type)
831faabf
 {
 	struct lump* tmp;
 
22d4aa5d
 	tmp=pkg_malloc(sizeof(struct lump));
831faabf
 	if (tmp==0){
1400b772
 		ser_error=E_OUT_OF_MEM;
7fb04ae6
 		LM_ERR("out of memory\n");
831faabf
 		return 0;
 	}
 	memset(tmp,0,sizeof(struct lump));
 	tmp->after=after->after;
 	tmp->type=type;
 	tmp->op=LUMP_ADD;
 	tmp->u.value=new_hdr;
 	tmp->len=len;
 	after->after=tmp;
 	return tmp;
 }
 
 
 
 /* inserts a  header/data lump immediately before "before" 
  * returns pointer on success, 0 on error */
 struct lump* insert_new_lump_before( struct lump* before, char* new_hdr,
182fea0e
 							int len, enum _hdr_types_t type)
831faabf
 {
 	struct lump* tmp;
 
22d4aa5d
 	tmp=pkg_malloc(sizeof(struct lump));
831faabf
 	if (tmp==0){
1400b772
 		ser_error=E_OUT_OF_MEM;
7fb04ae6
 		LM_ERR("out of memory\n");
831faabf
 		return 0;
 	}
 	memset(tmp,0,sizeof(struct lump));
 	tmp->before=before->before;
 	tmp->type=type;
 	tmp->op=LUMP_ADD;
 	tmp->u.value=new_hdr;
 	tmp->len=len;
 	before->before=tmp;
 	return tmp;
 }
 
 
 
f15bede1
 /* inserts a  subst lump immediately after hdr 
  * returns pointer on success, 0 on error */
 struct lump* insert_subst_lump_after( struct lump* after, enum lump_subst subst,
182fea0e
 										enum _hdr_types_t type)
f15bede1
 {
 	struct lump* tmp;
73eec4b2
 	
f15bede1
 	tmp=pkg_malloc(sizeof(struct lump));
 	if (tmp==0){
 		ser_error=E_OUT_OF_MEM;
7fb04ae6
 		LM_ERR("out of memory\n");
f15bede1
 		return 0;
 	}
 	memset(tmp,0,sizeof(struct lump));
 	tmp->after=after->after;
 	tmp->type=type;
 	tmp->op=LUMP_ADD_SUBST;
 	tmp->u.subst=subst;
 	tmp->len=0;
 	after->after=tmp;
 	return tmp;
 }
 
 
 
 /* inserts a  subst lump immediately before "before" 
  * returns pointer on success, 0 on error */
 struct lump* insert_subst_lump_before(	struct lump* before, 
 										enum lump_subst subst,
182fea0e
 										enum _hdr_types_t type)
f15bede1
 {
 	struct lump* tmp;
73eec4b2
 	
f15bede1
 	tmp=pkg_malloc(sizeof(struct lump));
 	if (tmp==0){
 		ser_error=E_OUT_OF_MEM;
7fb04ae6
 		LM_ERR("out of memory\n");
f15bede1
 		return 0;
 	}
 	memset(tmp,0,sizeof(struct lump));
 	tmp->before=before->before;
 	tmp->type=type;
 	tmp->op=LUMP_ADD_SUBST;
 	tmp->u.subst=subst;
 	tmp->len=0;
 	before->before=tmp;
 	return tmp;
 }
 
 
 
73eec4b2
 /* inserts a  cond lump immediately after hdr 
  * returns pointer on success, 0 on error */
 struct lump* insert_cond_lump_after( struct lump* after, enum lump_conditions c,
182fea0e
 										enum _hdr_types_t type)
73eec4b2
 {
 	struct lump* tmp;
 	
 	tmp=pkg_malloc(sizeof(struct lump));
 	if (tmp==0){
 		ser_error=E_OUT_OF_MEM;
7fb04ae6
 		LM_ERR("out of memory\n");
73eec4b2
 		return 0;
 	}
 	memset(tmp,0,sizeof(struct lump));
 	tmp->after=after->after;
 	tmp->type=type;
 	tmp->op=LUMP_ADD_OPT;
 	tmp->u.cond=c;
 	tmp->len=0;
 	after->after=tmp;
 	return tmp;
 }
 
 
 
 /* inserts a  conditional lump immediately before "before" 
  * returns pointer on success, 0 on error */
 struct lump* insert_cond_lump_before(	struct lump* before, 
 										enum lump_conditions c,
182fea0e
 										enum _hdr_types_t type)
73eec4b2
 {
 	struct lump* tmp;
 	
 	tmp=pkg_malloc(sizeof(struct lump));
 	if (tmp==0){
 		ser_error=E_OUT_OF_MEM;
7fb04ae6
 		LM_ERR("out of memory\n");
73eec4b2
 		return 0;
 	}
 	memset(tmp,0,sizeof(struct lump));
 	tmp->before=before->before;
 	tmp->type=type;
 	tmp->op=LUMP_ADD_OPT;
 	tmp->u.cond=c;
 	tmp->len=0;
 	before->before=tmp;
 	return tmp;
 }
 
 
 
831faabf
 /* removes an already existing header/data lump */
53c7e0f1
 /* WARNING: this function adds the lump either to the msg->add_rm or
4c37101e
  * msg->body_lumps list, depending on the offset being greater than msg->eoh,
  * so msg->eoh must be parsed (parse with HDR_EOH) if you think your lump
  *  might affect the body!! */
182fea0e
 struct lump* del_lump(struct sip_msg* msg, int offset, int len, enum _hdr_types_t type)
831faabf
 {
 	struct lump* tmp;
 	struct lump* prev, *t;
4c37101e
 	struct lump** list;
831faabf
 
549dba73
 	/* extra checks */
 	if (offset>msg->len){
7fb04ae6
 		LM_CRIT("offset exceeds message size (%d > %d) aborting...\n",
 					offset, msg->len);
549dba73
 		abort();
 	}
 	if (offset+len>msg->len){
7fb04ae6
 		LM_CRIT("offset + len exceeds message size (%d + %d > %d)\n",
 					offset, len,  msg->len);
549dba73
 		abort();
 	}
 	if (len==0){
7fb04ae6
 		LM_WARN("0 len (offset=%d)\n", offset);
549dba73
 	}
 	
22d4aa5d
 	tmp=pkg_malloc(sizeof(struct lump));
831faabf
 	if (tmp==0){
7fb04ae6
 		LM_ERR("out of memory\n");
831faabf
 		return 0;
 	}
 	memset(tmp,0,sizeof(struct lump));
 	tmp->op=LUMP_DEL;
 	tmp->type=type;
 	tmp->u.offset=offset;
 	tmp->len=len;
 	prev=0;
4c37101e
 	/* check to see whether this might be a body lump */
 	if ((msg->eoh) && (offset> (int)(msg->eoh-msg->buf)))
 		list=&msg->body_lumps;
 	else
 		list=&msg->add_rm;
831faabf
 	for (t=*list;t; prev=t, t=t->next){
 		/* insert it sorted after offset */
 		if (((t->op==LUMP_DEL)||(t->op==LUMP_NOP))&&(t->u.offset>offset))
 			break;
 	}
 	tmp->next=t;
 	if (prev) prev->next=tmp;
 	else *list=tmp;
 	return tmp;
 }
 
 
 
4c37101e
 /* add an anchor
53c7e0f1
  * WARNING: this function adds the lump either to the msg->add_rm or
4c37101e
  * msg->body_lumps list, depending on the offset being greater than msg->eoh,
  * so msg->eoh must be parsed (parse with HDR_EOH) if you think your lump
  *  might affect the body!! */
182fea0e
 struct lump* anchor_lump(struct sip_msg* msg, int offset, int len, enum _hdr_types_t type)
831faabf
 {
 	struct lump* tmp;
 	struct lump* prev, *t;
4c37101e
 	struct lump** list;
831faabf
 
4c37101e
 	
549dba73
 	/* extra checks */
 	if (offset>msg->len){
7fb04ae6
 		LM_CRIT("offset exceeds message size (%d > %d) aborting...\n",
 					offset, msg->len);
549dba73
 		abort();
 	}
 	if (len){
7fb04ae6
 		LM_WARN("len !=0 (%d)\n", len);
549dba73
 		if (offset+len>msg->len)
7fb04ae6
 			LM_WARN("offset + len exceeds message size (%d + %d > %d)\n",
 					offset, len,  msg->len);
549dba73
 	}
 	
22d4aa5d
 	tmp=pkg_malloc(sizeof(struct lump));
831faabf
 	if (tmp==0){
1400b772
 		ser_error=E_OUT_OF_MEM;
7fb04ae6
 		LM_ERR("out of memory\n");
831faabf
 		return 0;
 	}
 	memset(tmp,0,sizeof(struct lump));
 	tmp->op=LUMP_NOP;
 	tmp->type=type;
 	tmp->u.offset=offset;
 	tmp->len=len;
 	prev=0;
4c37101e
 	/* check to see whether this might be a body lump */
 	if ((msg->eoh) && (offset> (int)(msg->eoh-msg->buf)))
 		list=&msg->body_lumps;
 	else
 		list=&msg->add_rm;
 		
831faabf
 	for (t=*list;t; prev=t, t=t->next){
 		/* insert it sorted after offset */
 		if (((t->op==LUMP_DEL)||(t->op==LUMP_NOP))&&(t->u.offset>offset))
 			break;
 	}
 	tmp->next=t;
 	
 	if (prev) prev->next=tmp;
 	else *list=tmp;
 	return tmp;
 }
 
b663c49c
 /* add an anchor
  * Similar to anchor_lump() but this function checks whether or not a lump
  * has already been added to the same position. If an existing lump is found
  * then it is returned without adding a new one and is_ref is set to 1.
  *
  * WARNING: this function adds the lump either to the msg->add_rm or
  * msg->body_lumps list, depending on the offset being greater than msg->eoh,
  * so msg->eoh must be parsed (parse with HDR_EOH) if you think your lump
  *  might affect the body!! */
 struct lump* anchor_lump2(struct sip_msg* msg, int offset, int len, enum _hdr_types_t type,
 		int *is_ref)
 {
 	struct lump* tmp;
 	struct lump* prev, *t;
 	struct lump** list;
 
 	
 	/* extra checks */
 	if (offset>msg->len){
7fb04ae6
 		LM_CRIT("offset exceeds message size (%d > %d) aborting...\n",
 					offset, msg->len);
b663c49c
 		abort();
 	}
 	if (len){
7fb04ae6
 		LM_WARN("len !=0 (%d)\n", len);
b663c49c
 		if (offset+len>msg->len)
7fb04ae6
 			LM_WARN("offset + len exceeds message size (%d + %d > %d)\n",
 					offset, len,  msg->len);
b663c49c
 	}
 	
 	prev=0;
 	/* check to see whether this might be a body lump */
 	if ((msg->eoh) && (offset> (int)(msg->eoh-msg->buf)))
 		list=&msg->body_lumps;
 	else
 		list=&msg->add_rm;
 		
 	for (t=*list;t; prev=t, t=t->next){
 		/* insert it sorted after offset */
 		if (((t->op==LUMP_DEL)||(t->op==LUMP_NOP))&&(t->u.offset>=offset))
 			break;
 	}
 	if (t && (t->u.offset==offset)) {
 		/* A lump with the same offset is found */
 		*is_ref=1;
 		return t;
 	}
 
 	tmp=pkg_malloc(sizeof(struct lump));
 	if (tmp==0){
 		ser_error=E_OUT_OF_MEM;
7fb04ae6
 		LM_ERR("out of memory\n");
b663c49c
 		return 0;
 	}
 	memset(tmp,0,sizeof(struct lump));
 	tmp->op=LUMP_NOP;
 	tmp->type=type;
 	tmp->u.offset=offset;
 	tmp->len=len;
 
 	tmp->next=t;
 	
 	if (prev) prev->next=tmp;
 	else *list=tmp;
 
 	*is_ref=0;
 	return tmp;
 }
831faabf
 
 
 void free_lump(struct lump* lmp)
 {
 	if (lmp && (lmp->op==LUMP_ADD)){
3c41d76d
 		if (lmp->u.value){
 			if (lmp->flags &(LUMPFLAG_DUPED|LUMPFLAG_SHMEM)){
7fb04ae6
 				LM_CRIT("non free-able lump: %p flags=%x\n", lmp, lmp->flags);
3c41d76d
 				abort();
 			}else{
 				pkg_free(lmp->u.value);
 				lmp->u.value=0;
 				lmp->len=0;
 			}
 		}
831faabf
 	}
 }
 
 
 
 void free_lump_list(struct lump* l)
 {
5ada8f8a
 	struct lump* t, *r, *foo,*crt;
831faabf
 	t=l;
 	while(t){
 		crt=t;
 		t=t->next;
5ada8f8a
 	/*
 		 dangerous recursive clean
831faabf
 		if (crt->before) free_lump_list(crt->before);
 		if (crt->after)  free_lump_list(crt->after);
5ada8f8a
 	*/
 		/* no more recursion, clean after and before and that's it */
 		r=crt->before;
 		while(r){
 			foo=r; r=r->before;
6fa79282
 			free_lump(foo);
22d4aa5d
 			pkg_free(foo);
5ada8f8a
 		}
 		r=crt->after;
 		while(r){
 			foo=r; r=r->after;
6fa79282
 			free_lump(foo);
22d4aa5d
 			pkg_free(foo);
5ada8f8a
 		}
 		
831faabf
 		/*clean current elem*/
6fa79282
 		free_lump(crt);
22d4aa5d
 		pkg_free(crt);
831faabf
 	}
 }
4d93f145
 
 /* free (shallow-ly) a lump and its after/before lists */
 static void free_shallow_lump( struct lump *l )
 {
 	struct lump *r, *foo;
 
 	r=l->before;
 	while(r){
 		foo=r; r=r->before;
 		pkg_free(foo);
 	}
 	r=l->after;
 	while(r){
 		foo=r; r=r->after;
 		pkg_free(foo);
 	}
 	pkg_free(l);
 }
 
 /* duplicate (shallow-ly) a lump list into pkg memory */
 static struct lump *dup_lump_list_r( struct lump *l, 
 				enum lump_dir dir, int *error)
 {
 	int deep_error;
 	struct lump *new_lump;
 
 	deep_error=0; /* optimist: assume success in recursion */
 	/* if at list end, terminate recursion successfully */
 	if (!l) { *error=0; return 0; }
 	/* otherwise duplicate current element */
 	new_lump=pkg_malloc(sizeof(struct lump));
 	if (!new_lump) { *error=1; return 0; }
 
 	memcpy(new_lump, l, sizeof(struct lump));
 	new_lump->flags=LUMPFLAG_DUPED;
 	new_lump->next=new_lump->before=new_lump->after=0;
 
 	switch(dir) {
 		case LD_NEXT:	
 				new_lump->before=dup_lump_list_r(l->before, 
 								LD_BEFORE, &deep_error);
 				if (deep_error) goto deeperror;
 				new_lump->after=dup_lump_list_r(l->after, 
 								LD_AFTER, &deep_error);
 				if (deep_error) goto deeperror;
 				new_lump->next=dup_lump_list_r(l->next, 
 								LD_NEXT, &deep_error);
 				break;
 		case LD_BEFORE:
 				new_lump->before=dup_lump_list_r(l->before, 
 								LD_BEFORE, &deep_error);
 				break;
 		case LD_AFTER:
 				new_lump->after=dup_lump_list_r(l->after, 
 								LD_AFTER, &deep_error);
 				break;
 		default:
7fb04ae6
 				LM_CRIT("unknown dir: %d\n", dir );
4d93f145
 				deep_error=1;
 	}
 	if (deep_error) goto deeperror;
 
 	*error=0;
 	return new_lump;
 
 deeperror:
7fb04ae6
 	LM_ERR("out of mem\n");
4d93f145
 	free_shallow_lump(new_lump);
 	*error=1;
 	return 0;
 }
 
41f45c80
 
 
4d93f145
 /* shallow pkg copy of a lump list
  *
  * if either original list empty or error occur returns, 0
  * is returned, pointer to the copy otherwise
  */
 struct lump* dup_lump_list( struct lump *l )
 {
 	int deep_error;
 
 	deep_error=0;
 	return dup_lump_list_r(l, LD_NEXT, &deep_error);
 }
 
41f45c80
 
 
4d93f145
 void free_duped_lump_list(struct lump* l)
 {
 	struct lump *r, *foo,*crt;
 	while(l){
 		crt=l;
 		l=l->next;
 
 		r=crt->before;
 		while(r){
 			foo=r; r=r->before;
 			/* (+): if a new item was introduced to the shallow-ly
 			 * duped list, remove it completely, preserve it
53c7e0f1
 			 * otherwise (it is still referred by original list)
4d93f145
 			 */
 			if (foo->flags!=LUMPFLAG_DUPED) 
 					free_lump(foo);
 			pkg_free(foo);
 		}
 		r=crt->after;
 		while(r){
 			foo=r; r=r->after;
 			if (foo->flags!=LUMPFLAG_DUPED) /* (+) ... see above */
 				free_lump(foo);
 			pkg_free(foo);
 		}
 		
 		/*clean current elem*/
 		if (crt->flags!=LUMPFLAG_DUPED) /* (+) ... see above */
 			free_lump(crt);
 		pkg_free(crt);
 	}
 }
 
41f45c80
 
 
 void del_nonshm_lump( struct lump** lump_list )
 {
 	struct lump *r, *foo, *crt, **prev, *prev_r;
 
 	prev = lump_list;
 	crt = *lump_list;
 
 	while (crt) {
8652f5f5
 		if (!(crt->flags&LUMPFLAG_SHMEM)) {
41f45c80
 			/* unlink it */
 			foo = crt;
 			crt = crt->next;
 			foo->next = 0;
 			/* update the 'next' link of the previous lump */
 			*prev = crt;
 			/* entire before/after list must be removed */
 			free_lump_list( foo );
 		} else {
 			/* check on before and prev list for non-shmem lumps */
 			r = crt->after;
 			prev_r = crt;
 			while(r){
 				foo=r; r=r->after;
8652f5f5
 				if (!(foo->flags&LUMPFLAG_SHMEM)) {
41f45c80
 					prev_r->after = r;
 					free_lump(foo);
 					pkg_free(foo);
 				} else {
 					prev_r = foo;
 				}
 			}
 			/* before */
 			r = crt->before;
 			prev_r = crt;
 			while(r){
 				foo=r; r=r->before;
8652f5f5
 				if (!(foo->flags&LUMPFLAG_SHMEM)) {
41f45c80
 					prev_r->before = r;
 					free_lump(foo);
 					pkg_free(foo);
 				} else {
 					prev_r = foo;
 				}
 			}
 			/* go to next lump */
 			prev = &(crt->next);
 			crt = crt->next;
 		}
 	}
 }
 
b05f7d8f
 unsigned int count_applied_lumps(struct lump *ll, int type)
 {
 	unsigned int n = 0;
 	struct lump *l = 0;
 
 	for(l=ll; l; l=l->next) {
 		if (l->op==LUMP_NOP && l->type==type) {
 			if (l->after && l->after->op==LUMP_ADD_OPT) {
 				if (LUMP_IS_COND_TRUE(l->after)) {
 					n++;
 				}
 			} else {
 				n++;
 			}
 		}
 	}
 	return n;
 }
 
1fd0ca99
 /**
  * remove a lump in a root list
  * - it doesn't look for lumps added as before/after rule
  * - destroys the entire lump, with associated before/after rules
  */
7fd56197
 int remove_lump(sip_msg_t *msg, struct lump *l)
 {
 	struct lump *t = NULL;
 	struct lump *prev = NULL;
 	struct lump **list = NULL;
 
 	list=&msg->add_rm;	
 	for (t=*list; t; prev=t, t=t->next) {
 		if(t==l)
 			break;
 	}
 	if(t==NULL) {
 		list=&msg->body_lumps;
 		for (t=*list; t; prev=t, t=t->next) {
 			if(t==l)
 				break;
 		}
 	}
 	if(t!=NULL) {
 		if(prev==NULL) {
 			*list = t->next;
 		} else {
 			prev->next = t->next;
 		}
1fd0ca99
 		/* detach and free all its content */
 		t->next = NULL;
 		free_lump_list(t);
7fd56197
 		return 1;
 	}
 	return 0;
 }