123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740 |
- /* $Id$
- *
- *
- * Copyright (C) 2001-2003 FhG Fokus
- *
- * This file is part of SIP-router, a free SIP server.
- *
- * SIP-router is free software; you can redistribute it and/or modify
- * 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
- *
- * SIP-router is distributed in the hope that it will be useful,
- * 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
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
- * History:
- * --------
- * 2003-01-19 support for duplication lump lists added (jiri)
- * 2003-03-31 added subst lumps --they expand in ip addr, port a.s.o (andrei)
- * 2003-04-01 added conditional lump support functions (andrei)
- * 2003-10-20 anchor_lump & del_lump will automatically choose the lump list
- * based on msg->eoh comparisons (andrei)
- * 2003-10-28 added extra checks (paranoia) for {anchor,del}_lump (andrei)
- * 2005-03-24 the type of type attribute changed to enum _hdr_types_t (janakj)
- */
- /*!
- * \file
- * \brief SIP-router core ::
- * \ingroup core
- * Module: \ref core
- */
- #include "data_lump.h"
- #include "dprint.h"
- #include "mem/mem.h"
- #include "globals.h"
- #include "error.h"
- #include <stdlib.h>
- #include <string.h>
- #ifdef DEBUG_DMALLOC
- #include <dmalloc.h>
- #endif
- /* WARNING: all lump add/insert operations expect a pkg_malloc'ed char*
- * pointer the will be DEALLOCATED when the sip_msg is destroyed! */
- enum lump_dir { LD_NEXT, LD_BEFORE, LD_AFTER };
- /* adds a header to the end
- * returns pointer on success, 0 on error */
- struct lump* append_new_lump(struct lump** list, char* new_hdr,
- int len, enum _hdr_types_t type)
- {
- struct lump** t;
- struct lump* tmp;
-
- for (t=list;*t;t=&((*t)->next));
- tmp=pkg_malloc(sizeof(struct lump));
- if (tmp==0){
- LM_ERR("out of memory\n");
- 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;
- }
- /* 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){
- LM_ERR("out of memory\n");
- 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;
- }
- /* inserts a header to the beginning
- * returns pointer if success, 0 on error */
- struct lump* insert_new_lump(struct lump** list, char* new_hdr,
- int len, enum _hdr_types_t type)
- {
- struct lump* tmp;
- tmp=pkg_malloc(sizeof(struct lump));
- if (tmp==0){
- LM_ERR("out of memory\n");
- 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,
- int len, enum _hdr_types_t type)
- {
- struct lump* tmp;
- tmp=pkg_malloc(sizeof(struct lump));
- if (tmp==0){
- ser_error=E_OUT_OF_MEM;
- LM_ERR("out of memory\n");
- 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,
- int len, enum _hdr_types_t type)
- {
- struct lump* tmp;
- tmp=pkg_malloc(sizeof(struct lump));
- if (tmp==0){
- ser_error=E_OUT_OF_MEM;
- LM_ERR("out of memory\n");
- 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;
- }
- /* 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,
- enum _hdr_types_t type)
- {
- struct lump* tmp;
-
- tmp=pkg_malloc(sizeof(struct lump));
- if (tmp==0){
- ser_error=E_OUT_OF_MEM;
- LM_ERR("out of memory\n");
- 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,
- enum _hdr_types_t type)
- {
- struct lump* tmp;
-
- tmp=pkg_malloc(sizeof(struct lump));
- if (tmp==0){
- ser_error=E_OUT_OF_MEM;
- LM_ERR("out of memory\n");
- 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;
- }
- /* 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,
- enum _hdr_types_t type)
- {
- struct lump* tmp;
-
- tmp=pkg_malloc(sizeof(struct lump));
- if (tmp==0){
- ser_error=E_OUT_OF_MEM;
- LM_ERR("out of memory\n");
- 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,
- enum _hdr_types_t type)
- {
- struct lump* tmp;
-
- tmp=pkg_malloc(sizeof(struct lump));
- if (tmp==0){
- ser_error=E_OUT_OF_MEM;
- LM_ERR("out of memory\n");
- 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;
- }
- /* removes an already existing header/data lump */
- /* 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* del_lump(struct sip_msg* msg, int offset, int len, enum _hdr_types_t type)
- {
- struct lump* tmp;
- struct lump* prev, *t;
- struct lump** list;
- /* extra checks */
- if (offset>msg->len){
- LM_CRIT("offset exceeds message size (%d > %d) aborting...\n",
- offset, msg->len);
- abort();
- }
- if (offset+len>msg->len){
- LM_CRIT("offset + len exceeds message size (%d + %d > %d)\n",
- offset, len, msg->len);
- abort();
- }
- if (len==0){
- LM_WARN("0 len (offset=%d)\n", offset);
- }
-
- tmp=pkg_malloc(sizeof(struct lump));
- if (tmp==0){
- LM_ERR("out of memory\n");
- return 0;
- }
- memset(tmp,0,sizeof(struct lump));
- tmp->op=LUMP_DEL;
- tmp->type=type;
- tmp->u.offset=offset;
- tmp->len=len;
- 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;
- }
- tmp->next=t;
- if (prev) prev->next=tmp;
- else *list=tmp;
- return tmp;
- }
- /* add an anchor
- * 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_lump(struct sip_msg* msg, int offset, int len, enum _hdr_types_t type)
- {
- struct lump* tmp;
- struct lump* prev, *t;
- struct lump** list;
-
- /* extra checks */
- if (offset>msg->len){
- LM_CRIT("offset exceeds message size (%d > %d) aborting...\n",
- offset, msg->len);
- abort();
- }
- if (len){
- LM_WARN("len !=0 (%d)\n", len);
- if (offset+len>msg->len)
- LM_WARN("offset + len exceeds message size (%d + %d > %d)\n",
- offset, len, msg->len);
- }
-
- tmp=pkg_malloc(sizeof(struct lump));
- if (tmp==0){
- ser_error=E_OUT_OF_MEM;
- LM_ERR("out of memory\n");
- return 0;
- }
- memset(tmp,0,sizeof(struct lump));
- tmp->op=LUMP_NOP;
- tmp->type=type;
- tmp->u.offset=offset;
- tmp->len=len;
- 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;
- }
- tmp->next=t;
-
- if (prev) prev->next=tmp;
- else *list=tmp;
- return tmp;
- }
- /* 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){
- LM_CRIT("offset exceeds message size (%d > %d) aborting...\n",
- offset, msg->len);
- abort();
- }
- if (len){
- LM_WARN("len !=0 (%d)\n", len);
- if (offset+len>msg->len)
- LM_WARN("offset + len exceeds message size (%d + %d > %d)\n",
- offset, len, msg->len);
- }
-
- 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;
- LM_ERR("out of memory\n");
- 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;
- }
- void free_lump(struct lump* lmp)
- {
- if (lmp && (lmp->op==LUMP_ADD)){
- if (lmp->u.value){
- if (lmp->flags &(LUMPFLAG_DUPED|LUMPFLAG_SHMEM)){
- LM_CRIT("non free-able lump: %p flags=%x\n", lmp, lmp->flags);
- abort();
- }else{
- pkg_free(lmp->u.value);
- lmp->u.value=0;
- lmp->len=0;
- }
- }
- }
- }
- void free_lump_list(struct lump* l)
- {
- struct lump* t, *r, *foo,*crt;
- t=l;
- while(t){
- crt=t;
- t=t->next;
- /*
- dangerous recursive clean
- if (crt->before) free_lump_list(crt->before);
- if (crt->after) free_lump_list(crt->after);
- */
- /* no more recursion, clean after and before and that's it */
- r=crt->before;
- while(r){
- foo=r; r=r->before;
- free_lump(foo);
- pkg_free(foo);
- }
- r=crt->after;
- while(r){
- foo=r; r=r->after;
- free_lump(foo);
- pkg_free(foo);
- }
-
- /*clean current elem*/
- free_lump(crt);
- pkg_free(crt);
- }
- }
- /* 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:
- LM_CRIT("unknown dir: %d\n", dir );
- deep_error=1;
- }
- if (deep_error) goto deeperror;
- *error=0;
- return new_lump;
- deeperror:
- LM_ERR("out of mem\n");
- free_shallow_lump(new_lump);
- *error=1;
- return 0;
- }
- /* 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);
- }
- 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
- * otherwise (it is still referred by original list)
- */
- 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);
- }
- }
- void del_nonshm_lump( struct lump** lump_list )
- {
- struct lump *r, *foo, *crt, **prev, *prev_r;
- prev = lump_list;
- crt = *lump_list;
- while (crt) {
- if (!(crt->flags&LUMPFLAG_SHMEM)) {
- /* 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;
- if (!(foo->flags&LUMPFLAG_SHMEM)) {
- 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;
- if (!(foo->flags&LUMPFLAG_SHMEM)) {
- prev_r->before = r;
- free_lump(foo);
- pkg_free(foo);
- } else {
- prev_r = foo;
- }
- }
- /* go to next lump */
- prev = &(crt->next);
- crt = crt->next;
- }
- }
- }
- 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;
- }
- 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;
- }
- free_lump(t);
- return 1;
- }
- return 0;
- }
|