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.
... | ... |
@@ -81,6 +81,33 @@ struct lump* append_new_lump(struct lump** list, char* new_hdr, |
81 | 81 |
return tmp; |
82 | 82 |
} |
83 | 83 |
|
84 |
+/* adds a header right after an anchor point if exists |
|
85 |
+ * returns pointer on success, 0 on error */ |
|
86 |
+struct lump* add_new_lump(struct lump** list, char* new_hdr, |
|
87 |
+ int len, enum _hdr_types_t type) |
|
88 |
+{ |
|
89 |
+ struct lump** t; |
|
90 |
+ struct lump* tmp; |
|
91 |
+ |
|
92 |
+ |
|
93 |
+ t = (*list) ? &((*list)->next) : list; |
|
94 |
+ |
|
95 |
+ tmp=pkg_malloc(sizeof(struct lump)); |
|
96 |
+ if (tmp==0){ |
|
97 |
+ LOG(L_ERR, "ERROR: add_new_lump: out of memory\n"); |
|
98 |
+ return 0; |
|
99 |
+ } |
|
100 |
+ |
|
101 |
+ memset(tmp,0,sizeof(struct lump)); |
|
102 |
+ tmp->type=type; |
|
103 |
+ tmp->op=LUMP_ADD; |
|
104 |
+ tmp->u.value=new_hdr; |
|
105 |
+ tmp->len=len; |
|
106 |
+ tmp->next=*t; |
|
107 |
+ *t=tmp; |
|
108 |
+ return tmp; |
|
109 |
+} |
|
110 |
+ |
|
84 | 111 |
|
85 | 112 |
|
86 | 113 |
/* 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 |
343 | 370 |
tmp=pkg_malloc(sizeof(struct lump)); |
344 | 371 |
if (tmp==0){ |
345 | 372 |
ser_error=E_OUT_OF_MEM; |
346 |
- LOG(L_ERR, "ERROR: insert_new_lump_before: out of memory\n"); |
|
373 |
+ LOG(L_ERR, "ERROR: anchor_lump: out of memory\n"); |
|
347 | 374 |
return 0; |
348 | 375 |
} |
349 | 376 |
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 |
370 | 397 |
return tmp; |
371 | 398 |
} |
372 | 399 |
|
400 |
+/* add an anchor |
|
401 |
+ * Similar to anchor_lump() but this function checks whether or not a lump |
|
402 |
+ * has already been added to the same position. If an existing lump is found |
|
403 |
+ * then it is returned without adding a new one and is_ref is set to 1. |
|
404 |
+ * |
|
405 |
+ * WARNING: this function adds the lump either to the msg->add_rm or |
|
406 |
+ * msg->body_lumps list, depending on the offset being greater than msg->eoh, |
|
407 |
+ * so msg->eoh must be parsed (parse with HDR_EOH) if you think your lump |
|
408 |
+ * might affect the body!! */ |
|
409 |
+struct lump* anchor_lump2(struct sip_msg* msg, int offset, int len, enum _hdr_types_t type, |
|
410 |
+ int *is_ref) |
|
411 |
+{ |
|
412 |
+ struct lump* tmp; |
|
413 |
+ struct lump* prev, *t; |
|
414 |
+ struct lump** list; |
|
415 |
+ |
|
416 |
+ |
|
417 |
+ /* extra checks */ |
|
418 |
+ if (offset>msg->len){ |
|
419 |
+ LOG(L_CRIT, "BUG: anchor_lump2: offset exceeds message size (%d > %d)" |
|
420 |
+ " aborting...\n", offset, msg->len); |
|
421 |
+ abort(); |
|
422 |
+ } |
|
423 |
+ if (len){ |
|
424 |
+ LOG(L_WARN, "WARNING: anchor_lump2: called with len !=0 (%d)\n", len); |
|
425 |
+ if (offset+len>msg->len) |
|
426 |
+ LOG(L_WARN, "WARNING: anchor_lump2: offset + len exceeds message" |
|
427 |
+ " size (%d + %d > %d)\n", offset, len, msg->len); |
|
428 |
+ } |
|
429 |
+ |
|
430 |
+ prev=0; |
|
431 |
+ /* check to see whether this might be a body lump */ |
|
432 |
+ if ((msg->eoh) && (offset> (int)(msg->eoh-msg->buf))) |
|
433 |
+ list=&msg->body_lumps; |
|
434 |
+ else |
|
435 |
+ list=&msg->add_rm; |
|
436 |
+ |
|
437 |
+ for (t=*list;t; prev=t, t=t->next){ |
|
438 |
+ /* insert it sorted after offset */ |
|
439 |
+ if (((t->op==LUMP_DEL)||(t->op==LUMP_NOP))&&(t->u.offset>=offset)) |
|
440 |
+ break; |
|
441 |
+ } |
|
442 |
+ if (t && (t->u.offset==offset)) { |
|
443 |
+ /* A lump with the same offset is found */ |
|
444 |
+ *is_ref=1; |
|
445 |
+ return t; |
|
446 |
+ } |
|
447 |
+ |
|
448 |
+ tmp=pkg_malloc(sizeof(struct lump)); |
|
449 |
+ if (tmp==0){ |
|
450 |
+ ser_error=E_OUT_OF_MEM; |
|
451 |
+ LOG(L_ERR, "ERROR: anchor_lump2: out of memory\n"); |
|
452 |
+ return 0; |
|
453 |
+ } |
|
454 |
+ memset(tmp,0,sizeof(struct lump)); |
|
455 |
+ tmp->op=LUMP_NOP; |
|
456 |
+ tmp->type=type; |
|
457 |
+ tmp->u.offset=offset; |
|
458 |
+ tmp->len=len; |
|
459 |
+ |
|
460 |
+ tmp->next=t; |
|
461 |
+ |
|
462 |
+ if (prev) prev->next=tmp; |
|
463 |
+ else *list=tmp; |
|
464 |
+ |
|
465 |
+ *is_ref=0; |
|
466 |
+ return tmp; |
|
467 |
+} |
|
373 | 468 |
|
374 | 469 |
|
375 | 470 |
void free_lump(struct lump* lmp) |
... | ... |
@@ -48,6 +48,11 @@ |
48 | 48 |
#include "parser/msg_parser.h" |
49 | 49 |
#include "parser/hf.h" |
50 | 50 |
|
51 |
+ |
|
52 |
+/* adds a header right after an anchor point if exists */ |
|
53 |
+struct lump* add_new_lump(struct lump** list, char* new_hdr, |
|
54 |
+ int len, enum _hdr_types_t type); |
|
55 |
+ |
|
51 | 56 |
/*! \brief adds a header to the end */ |
52 | 57 |
struct lump* append_new_lump(struct lump** list, char* new_hdr, |
53 | 58 |
int len, enum _hdr_types_t type); |
... | ... |
@@ -71,6 +76,10 @@ struct lump* insert_cond_lump_before(struct lump* after, enum lump_conditions c, |
71 | 76 |
enum _hdr_types_t type); |
72 | 77 |
|
73 | 78 |
/*! \brief removes an already existing header */ |
79 |
+/* set an anchor if there is no existing one at the given offset, |
|
80 |
+ * otherwise return the existing anchor */ |
|
81 |
+struct lump* anchor_lump2(struct sip_msg* msg, int offset, int len, enum _hdr_types_t type, |
|
82 |
+ int *is_ref); |
|
74 | 83 |
struct lump* del_lump(struct sip_msg* msg, int offset, int len, enum _hdr_types_t type); |
75 | 84 |
/*! \brief set an anchor */ |
76 | 85 |
struct lump* anchor_lump(struct sip_msg* msg, int offset, int len, enum _hdr_types_t type); |