[sr-dev] git:master: data lumps: two new add lump functions

Miklos Tirpak miklos at iptel.org
Mon Apr 19 12:50:51 CEST 2010


Module: sip-router
Branch: master
Commit: b663c49cf5c2a0b91453920afb14f9234f186e1e
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=b663c49cf5c2a0b91453920afb14f9234f186e1e

Author: Miklos Tirpak <miklos at iptel.org>
Committer: Miklos Tirpak <miklos at iptel.org>
Date:   Mon Apr 19 12:44:05 2010 +0200

data lumps: two new add lump functions

The following new functions are introduces that can be used
to add new lumps:

- add_new_lump(): Add a data lump right after the anchor point
	into the main lump list. Every "before" lump of the same
	anchor point will preceed, every "after" lump will
	follow this lump.

- anchor_lump2(): Return the anchor point at the given offset
	if exists, otherwise create a new anchor.

---

 data_lump.c |   97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 data_lump.h |    9 +++++
 2 files changed, 105 insertions(+), 1 deletions(-)

diff --git a/data_lump.c b/data_lump.c
index 024f602..433ce2f 100644
--- a/data_lump.c
+++ b/data_lump.c
@@ -81,6 +81,33 @@ struct lump* append_new_lump(struct lump** list, char* new_hdr,
 	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){
+		LOG(L_ERR, "ERROR: add_new_lump: 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 
@@ -343,7 +370,7 @@ struct lump* anchor_lump(struct sip_msg* msg, int offset, int len, enum _hdr_typ
 	tmp=pkg_malloc(sizeof(struct lump));
 	if (tmp==0){
 		ser_error=E_OUT_OF_MEM;
-		LOG(L_ERR, "ERROR: insert_new_lump_before: out of memory\n");
+		LOG(L_ERR, "ERROR: anchor_lump: out of memory\n");
 		return 0;
 	}
 	memset(tmp,0,sizeof(struct lump));
@@ -370,6 +397,74 @@ struct lump* anchor_lump(struct sip_msg* msg, int offset, int len, enum _hdr_typ
 	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){
+		LOG(L_CRIT, "BUG: anchor_lump2: offset exceeds message size (%d > %d)"
+					" aborting...\n", offset, msg->len);
+		abort();
+	}
+	if (len){
+		LOG(L_WARN, "WARNING: anchor_lump2: called with len !=0 (%d)\n", len);
+		if (offset+len>msg->len)
+			LOG(L_WARN, "WARNING: anchor_lump2: 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;
+		LOG(L_ERR, "ERROR: anchor_lump2: 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)
diff --git a/data_lump.h b/data_lump.h
index 9fe82f4..5438c97 100644
--- a/data_lump.h
+++ b/data_lump.h
@@ -48,6 +48,11 @@
 #include "parser/msg_parser.h"
 #include "parser/hf.h"
 
+
+/* adds a header right after an anchor point if exists */
+struct lump* add_new_lump(struct lump** list, char* new_hdr,
+							 int len, enum _hdr_types_t type);
+
 /*! \brief adds a header to the end */
 struct lump* append_new_lump(struct lump** list, char* new_hdr,
 							 int len, enum _hdr_types_t type);
@@ -71,6 +76,10 @@ struct lump* insert_cond_lump_before(struct lump* after, enum lump_conditions c,
 									enum _hdr_types_t type);
 
 /*! \brief removes an already existing header */
+/* set an anchor if there is no existing one at the given offset,
+ * otherwise return the existing anchor */
+struct lump* anchor_lump2(struct sip_msg* msg, int offset, int len, enum _hdr_types_t type,
+								int *is_ref);
 struct lump* del_lump(struct sip_msg* msg, int offset, int len, enum _hdr_types_t type);
 /*! \brief set an anchor */
 struct lump* anchor_lump(struct sip_msg* msg, int offset, int len, enum _hdr_types_t type);




More information about the sr-dev mailing list