Browse code

core, lib, modules: restructured source code tree

- new folder src/ to hold the source code for main project applications
- main.c is in src/
- all core files are subfolder are in src/core/
- modules are in src/modules/
- libs are in src/lib/
- application Makefiles are in src/
- application binary is built in src/ (src/kamailio)

Daniel-Constantin Mierla authored on 07/12/2016 11:03:51
Showing 1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,856 +0,0 @@
1
-/*
2
- * Copyright (C) 2001-2003 FhG Fokus
3
- *
4
- * This file is part of Kamailio, a free SIP server.
5
- *
6
- * Kamailio is free software; you can redistribute it and/or modify
7
- * it under the terms of the GNU General Public License as published by
8
- * the Free Software Foundation; either version 2 of the License, or
9
- * (at your option) any later version
10
- *
11
- * Kamailio is distributed in the hope that it will be useful,
12
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
- * GNU General Public License for more details.
15
- *
16
- * You should have received a copy of the GNU General Public License 
17
- * along with this program; if not, write to the Free Software 
18
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19
- *
20
- */
21
-/*!
22
- * \file
23
- * \brief Kamailio core :: Data lumps
24
- * \ingroup core
25
- * Module: \ref core
26
- */
27
-
28
-
29
-
30
-#include "data_lump.h"
31
-#include "dprint.h"
32
-#include "mem/mem.h"
33
-#include "globals.h"
34
-#include "error.h"
35
-
36
-#include <stdlib.h>
37
-#include <string.h>
38
-
39
-#ifdef DEBUG_DMALLOC
40
-#include <dmalloc.h>
41
-#endif
42
-
43
-/* WARNING: all lump add/insert operations expect a pkg_malloc'ed char* 
44
- * pointer the will be DEALLOCATED when the sip_msg is destroyed! */
45
-
46
-enum lump_dir { LD_NEXT, LD_BEFORE, LD_AFTER };
47
-
48
-/* adds a header to the end
49
- * returns  pointer on success, 0 on error */
50
-struct lump* append_new_lump(struct lump** list, char* new_hdr,
51
-							 int len, enum _hdr_types_t type)
52
-{
53
-	struct lump** t;
54
-	struct lump* tmp;
55
-	
56
-	for (t=list;*t;t=&((*t)->next));
57
-
58
-	tmp=pkg_malloc(sizeof(struct lump));
59
-	if (tmp==0){
60
-		LM_ERR("out of memory\n");
61
-		return 0;
62
-	}
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
-
73
-/* adds a header right after an anchor point if exists
74
- * returns  pointer on success, 0 on error */
75
-struct lump* add_new_lump(struct lump** list, char* new_hdr,
76
-							 int len, enum _hdr_types_t type)
77
-{
78
-	struct lump** t;
79
-	struct lump* tmp;
80
-	
81
-
82
-	t = (*list) ? &((*list)->next) : list;
83
-
84
-	tmp=pkg_malloc(sizeof(struct lump));
85
-	if (tmp==0){
86
-		LM_ERR("out of memory\n");
87
-		return 0;
88
-	}
89
-		
90
-	memset(tmp,0,sizeof(struct lump));
91
-	tmp->type=type;
92
-	tmp->op=LUMP_ADD;
93
-	tmp->u.value=new_hdr;
94
-	tmp->len=len;
95
-	tmp->next=*t;
96
-	*t=tmp;
97
-	return tmp;
98
-}
99
-
100
-
101
-
102
-/* inserts a header to the beginning 
103
- * returns pointer if success, 0 on error */
104
-struct lump* insert_new_lump(struct lump** list, char* new_hdr,
105
-								int len, enum _hdr_types_t type)
106
-{
107
-	struct lump* tmp;
108
-
109
-	tmp=pkg_malloc(sizeof(struct lump));
110
-	if (tmp==0){
111
-		LM_ERR("out of memory\n");
112
-		return 0;
113
-	}
114
-	memset(tmp,0,sizeof(struct lump));
115
-	tmp->next=*list;
116
-	tmp->type=type;
117
-	tmp->op=LUMP_ADD;
118
-	tmp->u.value=new_hdr;
119
-	tmp->len=len;
120
-	*list=tmp;
121
-	return tmp;
122
-}
123
-
124
-
125
-
126
-/* inserts a  header/data lump immediately after hdr 
127
- * returns pointer on success, 0 on error */
128
-struct lump* insert_new_lump_after( struct lump* after, char* new_hdr,
129
-							int len, enum _hdr_types_t type)
130
-{
131
-	struct lump* tmp;
132
-
133
-	tmp=pkg_malloc(sizeof(struct lump));
134
-	if (tmp==0){
135
-		ser_error=E_OUT_OF_MEM;
136
-		LM_ERR("out of memory\n");
137
-		return 0;
138
-	}
139
-	memset(tmp,0,sizeof(struct lump));
140
-	tmp->after=after->after;
141
-	tmp->type=type;
142
-	tmp->op=LUMP_ADD;
143
-	tmp->u.value=new_hdr;
144
-	tmp->len=len;
145
-	after->after=tmp;
146
-	return tmp;
147
-}
148
-
149
-
150
-
151
-/* inserts a  header/data lump immediately before "before" 
152
- * returns pointer on success, 0 on error */
153
-struct lump* insert_new_lump_before( struct lump* before, char* new_hdr,
154
-							int len, enum _hdr_types_t type)
155
-{
156
-	struct lump* tmp;
157
-
158
-	tmp=pkg_malloc(sizeof(struct lump));
159
-	if (tmp==0){
160
-		ser_error=E_OUT_OF_MEM;
161
-		LM_ERR("out of memory\n");
162
-		return 0;
163
-	}
164
-	memset(tmp,0,sizeof(struct lump));
165
-	tmp->before=before->before;
166
-	tmp->type=type;
167
-	tmp->op=LUMP_ADD;
168
-	tmp->u.value=new_hdr;
169
-	tmp->len=len;
170
-	before->before=tmp;
171
-	return tmp;
172
-}
173
-
174
-
175
-
176
-/* inserts a  subst lump immediately after hdr 
177
- * returns pointer on success, 0 on error */
178
-struct lump* insert_subst_lump_after( struct lump* after, enum lump_subst subst,
179
-										enum _hdr_types_t type)
180
-{
181
-	struct lump* tmp;
182
-	
183
-	tmp=pkg_malloc(sizeof(struct lump));
184
-	if (tmp==0){
185
-		ser_error=E_OUT_OF_MEM;
186
-		LM_ERR("out of memory\n");
187
-		return 0;
188
-	}
189
-	memset(tmp,0,sizeof(struct lump));
190
-	tmp->after=after->after;
191
-	tmp->type=type;
192
-	tmp->op=LUMP_ADD_SUBST;
193
-	tmp->u.subst=subst;
194
-	tmp->len=0;
195
-	after->after=tmp;
196
-	return tmp;
197
-}
198
-
199
-
200
-
201
-/* inserts a  subst lump immediately before "before" 
202
- * returns pointer on success, 0 on error */
203
-struct lump* insert_subst_lump_before(	struct lump* before, 
204
-										enum lump_subst subst,
205
-										enum _hdr_types_t type)
206
-{
207
-	struct lump* tmp;
208
-	
209
-	tmp=pkg_malloc(sizeof(struct lump));
210
-	if (tmp==0){
211
-		ser_error=E_OUT_OF_MEM;
212
-		LM_ERR("out of memory\n");
213
-		return 0;
214
-	}
215
-	memset(tmp,0,sizeof(struct lump));
216
-	tmp->before=before->before;
217
-	tmp->type=type;
218
-	tmp->op=LUMP_ADD_SUBST;
219
-	tmp->u.subst=subst;
220
-	tmp->len=0;
221
-	before->before=tmp;
222
-	return tmp;
223
-}
224
-
225
-
226
-
227
-/* inserts a  cond lump immediately after hdr 
228
- * returns pointer on success, 0 on error */
229
-struct lump* insert_cond_lump_after( struct lump* after, enum lump_conditions c,
230
-										enum _hdr_types_t type)
231
-{
232
-	struct lump* tmp;
233
-	
234
-	tmp=pkg_malloc(sizeof(struct lump));
235
-	if (tmp==0){
236
-		ser_error=E_OUT_OF_MEM;
237
-		LM_ERR("out of memory\n");
238
-		return 0;
239
-	}
240
-	memset(tmp,0,sizeof(struct lump));
241
-	tmp->after=after->after;
242
-	tmp->type=type;
243
-	tmp->op=LUMP_ADD_OPT;
244
-	tmp->u.cond=c;
245
-	tmp->len=0;
246
-	after->after=tmp;
247
-	return tmp;
248
-}
249
-
250
-
251
-
252
-/* inserts a  conditional lump immediately before "before" 
253
- * returns pointer on success, 0 on error */
254
-struct lump* insert_cond_lump_before(	struct lump* before, 
255
-										enum lump_conditions c,
256
-										enum _hdr_types_t type)
257
-{
258
-	struct lump* tmp;
259
-	
260
-	tmp=pkg_malloc(sizeof(struct lump));
261
-	if (tmp==0){
262
-		ser_error=E_OUT_OF_MEM;
263
-		LM_ERR("out of memory\n");
264
-		return 0;
265
-	}
266
-	memset(tmp,0,sizeof(struct lump));
267
-	tmp->before=before->before;
268
-	tmp->type=type;
269
-	tmp->op=LUMP_ADD_OPT;
270
-	tmp->u.cond=c;
271
-	tmp->len=0;
272
-	before->before=tmp;
273
-	return tmp;
274
-}
275
-
276
-
277
-
278
-/* removes an already existing header/data lump */
279
-/* WARNING: this function adds the lump either to the msg->add_rm or
280
- * msg->body_lumps list, depending on the offset being greater than msg->eoh,
281
- * so msg->eoh must be parsed (parse with HDR_EOH) if you think your lump
282
- *  might affect the body!! */
283
-struct lump* del_lump(struct sip_msg* msg, int offset, int len, enum _hdr_types_t type)
284
-{
285
-	struct lump* tmp;
286
-	struct lump* prev, *t;
287
-	struct lump** list;
288
-
289
-	/* extra checks */
290
-	if (offset>msg->len){
291
-		LM_CRIT("offset exceeds message size (%d > %d) aborting...\n",
292
-					offset, msg->len);
293
-		abort();
294
-	}
295
-	if (offset+len>msg->len){
296
-		LM_CRIT("offset + len exceeds message size (%d + %d > %d)\n",
297
-					offset, len,  msg->len);
298
-		abort();
299
-	}
300
-	if (len==0){
301
-		LM_WARN("0 len (offset=%d)\n", offset);
302
-	}
303
-	
304
-	tmp=pkg_malloc(sizeof(struct lump));
305
-	if (tmp==0){
306
-		LM_ERR("out of memory\n");
307
-		return 0;
308
-	}
309
-	memset(tmp,0,sizeof(struct lump));
310
-	tmp->op=LUMP_DEL;
311
-	tmp->type=type;
312
-	tmp->u.offset=offset;
313
-	tmp->len=len;
314
-	prev=0;
315
-	/* check to see whether this might be a body lump */
316
-	if ((msg->eoh) && (offset> (int)(msg->eoh-msg->buf)))
317
-		list=&msg->body_lumps;
318
-	else
319
-		list=&msg->add_rm;
320
-	for (t=*list;t; prev=t, t=t->next){
321
-		/* insert it sorted after offset */
322
-		if (((t->op==LUMP_DEL)||(t->op==LUMP_NOP))&&(t->u.offset>offset))
323
-			break;
324
-	}
325
-	tmp->next=t;
326
-	if (prev) prev->next=tmp;
327
-	else *list=tmp;
328
-	return tmp;
329
-}
330
-
331
-
332
-
333
-/* add an anchor
334
- * WARNING: this function adds the lump either to the msg->add_rm or
335
- * msg->body_lumps list, depending on the offset being greater than msg->eoh,
336
- * so msg->eoh must be parsed (parse with HDR_EOH) if you think your lump
337
- *  might affect the body!! */
338
-struct lump* anchor_lump(struct sip_msg* msg, int offset, int len, enum _hdr_types_t type)
339
-{
340
-	struct lump* tmp;
341
-	struct lump* prev, *t;
342
-	struct lump** list;
343
-
344
-	
345
-	/* extra checks */
346
-	if (offset>msg->len){
347
-		LM_CRIT("offset exceeds message size (%d > %d) aborting...\n",
348
-					offset, msg->len);
349
-		abort();
350
-	}
351
-	if (len){
352
-		LM_WARN("len !=0 (%d)\n", len);
353
-		if (offset+len>msg->len)
354
-			LM_WARN("offset + len exceeds message size (%d + %d > %d)\n",
355
-					offset, len,  msg->len);
356
-	}
357
-	
358
-	tmp=pkg_malloc(sizeof(struct lump));
359
-	if (tmp==0){
360
-		ser_error=E_OUT_OF_MEM;
361
-		LM_ERR("out of memory\n");
362
-		return 0;
363
-	}
364
-	memset(tmp,0,sizeof(struct lump));
365
-	tmp->op=LUMP_NOP;
366
-	tmp->type=type;
367
-	tmp->u.offset=offset;
368
-	tmp->len=len;
369
-	prev=0;
370
-	/* check to see whether this might be a body lump */
371
-	if ((msg->eoh) && (offset> (int)(msg->eoh-msg->buf)))
372
-		list=&msg->body_lumps;
373
-	else
374
-		list=&msg->add_rm;
375
-		
376
-	for (t=*list;t; prev=t, t=t->next){
377
-		/* insert it sorted after offset */
378
-		if (((t->op==LUMP_DEL)||(t->op==LUMP_NOP))&&(t->u.offset>offset))
379
-			break;
380
-	}
381
-	tmp->next=t;
382
-	
383
-	if (prev) prev->next=tmp;
384
-	else *list=tmp;
385
-	return tmp;
386
-}
387
-
388
-/* add an anchor
389
- * Similar to anchor_lump() but this function checks whether or not a lump
390
- * has already been added to the same position. If an existing lump is found
391
- * then it is returned without adding a new one and is_ref is set to 1.
392
- *
393
- * WARNING: this function adds the lump either to the msg->add_rm or
394
- * msg->body_lumps list, depending on the offset being greater than msg->eoh,
395
- * so msg->eoh must be parsed (parse with HDR_EOH) if you think your lump
396
- *  might affect the body!! */
397
-struct lump* anchor_lump2(struct sip_msg* msg, int offset, int len, enum _hdr_types_t type,
398
-		int *is_ref)
399
-{
400
-	struct lump* tmp;
401
-	struct lump* prev, *t;
402
-	struct lump** list;
403
-
404
-	
405
-	/* extra checks */
406
-	if (offset>msg->len){
407
-		LM_CRIT("offset exceeds message size (%d > %d) aborting...\n",
408
-					offset, msg->len);
409
-		abort();
410
-	}
411
-	if (len){
412
-		LM_WARN("len !=0 (%d)\n", len);
413
-		if (offset+len>msg->len)
414
-			LM_WARN("offset + len exceeds message size (%d + %d > %d)\n",
415
-					offset, len,  msg->len);
416
-	}
417
-	
418
-	prev=0;
419
-	/* check to see whether this might be a body lump */
420
-	if ((msg->eoh) && (offset> (int)(msg->eoh-msg->buf)))
421
-		list=&msg->body_lumps;
422
-	else
423
-		list=&msg->add_rm;
424
-		
425
-	for (t=*list;t; prev=t, t=t->next){
426
-		/* insert it sorted after offset */
427
-		if (((t->op==LUMP_DEL)||(t->op==LUMP_NOP))&&(t->u.offset>=offset))
428
-			break;
429
-	}
430
-	if (t && (t->u.offset==offset)) {
431
-		/* A lump with the same offset is found */
432
-		*is_ref=1;
433
-		return t;
434
-	}
435
-
436
-	tmp=pkg_malloc(sizeof(struct lump));
437
-	if (tmp==0){
438
-		ser_error=E_OUT_OF_MEM;
439
-		LM_ERR("out of memory\n");
440
-		return 0;
441
-	}
442
-	memset(tmp,0,sizeof(struct lump));
443
-	tmp->op=LUMP_NOP;
444
-	tmp->type=type;
445
-	tmp->u.offset=offset;
446
-	tmp->len=len;
447
-
448
-	tmp->next=t;
449
-	
450
-	if (prev) prev->next=tmp;
451
-	else *list=tmp;
452
-
453
-	*is_ref=0;
454
-	return tmp;
455
-}
456
-
457
-
458
-void free_lump(struct lump* lmp)
459
-{
460
-	if (lmp && (lmp->op==LUMP_ADD)){
461
-		if (lmp->u.value){
462
-			if (lmp->flags &(LUMPFLAG_DUPED|LUMPFLAG_SHMEM)){
463
-				LM_CRIT("non free-able lump: %p flags=%x\n", lmp, lmp->flags);
464
-				abort();
465
-			}else{
466
-				pkg_free(lmp->u.value);
467
-				lmp->u.value=0;
468
-				lmp->len=0;
469
-			}
470
-		}
471
-	}
472
-}
473
-
474
-
475
-
476
-void free_lump_list(struct lump* l)
477
-{
478
-	struct lump* t, *r, *foo,*crt;
479
-	t=l;
480
-	while(t){
481
-		crt=t;
482
-		t=t->next;
483
-	/*
484
-		 dangerous recursive clean
485
-		if (crt->before) free_lump_list(crt->before);
486
-		if (crt->after)  free_lump_list(crt->after);
487
-	*/
488
-		/* no more recursion, clean after and before and that's it */
489
-		r=crt->before;
490
-		while(r){
491
-			foo=r; r=r->before;
492
-			free_lump(foo);
493
-			pkg_free(foo);
494
-		}
495
-		r=crt->after;
496
-		while(r){
497
-			foo=r; r=r->after;
498
-			free_lump(foo);
499
-			pkg_free(foo);
500
-		}
501
-		
502
-		/*clean current elem*/
503
-		free_lump(crt);
504
-		pkg_free(crt);
505
-	}
506
-}
507
-
508
-/* free (shallow-ly) a lump and its after/before lists */
509
-static void free_shallow_lump( struct lump *l )
510
-{
511
-	struct lump *r, *foo;
512
-
513
-	r=l->before;
514
-	while(r){
515
-		foo=r; r=r->before;
516
-		pkg_free(foo);
517
-	}
518
-	r=l->after;
519
-	while(r){
520
-		foo=r; r=r->after;
521
-		pkg_free(foo);
522
-	}
523
-	pkg_free(l);
524
-}
525
-
526
-/* duplicate (shallow-ly) a lump list into pkg memory */
527
-static struct lump *dup_lump_list_r( struct lump *l, 
528
-				enum lump_dir dir, int *error)
529
-{
530
-	int deep_error;
531
-	struct lump *new_lump;
532
-
533
-	deep_error=0; /* optimist: assume success in recursion */
534
-	/* if at list end, terminate recursion successfully */
535
-	if (!l) { *error=0; return 0; }
536
-	/* otherwise duplicate current element */
537
-	new_lump=pkg_malloc(sizeof(struct lump));
538
-	if (!new_lump) { *error=1; return 0; }
539
-
540
-	memcpy(new_lump, l, sizeof(struct lump));
541
-	new_lump->flags=LUMPFLAG_DUPED;
542
-	new_lump->next=new_lump->before=new_lump->after=0;
543
-
544
-	switch(dir) {
545
-		case LD_NEXT:	
546
-				new_lump->before=dup_lump_list_r(l->before, 
547
-								LD_BEFORE, &deep_error);
548
-				if (deep_error) goto deeperror;
549
-				new_lump->after=dup_lump_list_r(l->after, 
550
-								LD_AFTER, &deep_error);
551
-				if (deep_error) goto deeperror;
552
-				new_lump->next=dup_lump_list_r(l->next, 
553
-								LD_NEXT, &deep_error);
554
-				break;
555
-		case LD_BEFORE:
556
-				new_lump->before=dup_lump_list_r(l->before, 
557
-								LD_BEFORE, &deep_error);
558
-				break;
559
-		case LD_AFTER:
560
-				new_lump->after=dup_lump_list_r(l->after, 
561
-								LD_AFTER, &deep_error);
562
-				break;
563
-		default:
564
-				LM_CRIT("unknown dir: %d\n", dir );
565
-				deep_error=1;
566
-	}
567
-	if (deep_error) goto deeperror;
568
-
569
-	*error=0;
570
-	return new_lump;
571
-
572
-deeperror:
573
-	LM_ERR("out of mem\n");
574
-	free_shallow_lump(new_lump);
575
-	*error=1;
576
-	return 0;
577
-}
578
-
579
-
580
-
581
-/* shallow pkg copy of a lump list
582
- *
583
- * if either original list empty or error occur returns, 0
584
- * is returned, pointer to the copy otherwise
585
- */
586
-struct lump* dup_lump_list( struct lump *l )
587
-{
588
-	int deep_error;
589
-
590
-	deep_error=0;
591
-	return dup_lump_list_r(l, LD_NEXT, &deep_error);
592
-}
593
-
594
-
595
-
596
-void free_duped_lump_list(struct lump* l)
597
-{
598
-	struct lump *r, *foo,*crt;
599
-	while(l){
600
-		crt=l;
601
-		l=l->next;
602
-
603
-		r=crt->before;
604
-		while(r){
605
-			foo=r; r=r->before;
606
-			/* (+): if a new item was introduced to the shallow-ly
607
-			 * duped list, remove it completely, preserve it
608
-			 * otherwise (it is still referred by original list)
609
-			 */
610
-			if (foo->flags!=LUMPFLAG_DUPED) 
611
-					free_lump(foo);
612
-			pkg_free(foo);
613
-		}
614
-		r=crt->after;
615
-		while(r){
616
-			foo=r; r=r->after;
617
-			if (foo->flags!=LUMPFLAG_DUPED) /* (+) ... see above */
618
-				free_lump(foo);
619
-			pkg_free(foo);
620
-		}
621
-		
622
-		/*clean current elem*/
623
-		if (crt->flags!=LUMPFLAG_DUPED) /* (+) ... see above */
624
-			free_lump(crt);
625
-		pkg_free(crt);
626
-	}
627
-}
628
-
629
-
630
-
631
-void del_nonshm_lump( struct lump** lump_list )
632
-{
633
-	struct lump *r, *foo, *crt, **prev, *prev_r;
634
-
635
-	prev = lump_list;
636
-	crt = *lump_list;
637
-
638
-	while (crt) {
639
-		if (!(crt->flags&LUMPFLAG_SHMEM)) {
640
-			/* unlink it */
641
-			foo = crt;
642
-			crt = crt->next;
643
-			foo->next = 0;
644
-			/* update the 'next' link of the previous lump */
645
-			*prev = crt;
646
-			/* entire before/after list must be removed */
647
-			free_lump_list( foo );
648
-		} else {
649
-			/* check on before and prev list for non-shmem lumps */
650
-			r = crt->after;
651
-			prev_r = crt;
652
-			while(r){
653
-				foo=r; r=r->after;
654
-				if (!(foo->flags&LUMPFLAG_SHMEM)) {
655
-					prev_r->after = r;
656
-					free_lump(foo);
657
-					pkg_free(foo);
658
-				} else {
659
-					prev_r = foo;
660
-				}
661
-			}
662
-			/* before */
663
-			r = crt->before;
664
-			prev_r = crt;
665
-			while(r){
666
-				foo=r; r=r->before;
667
-				if (!(foo->flags&LUMPFLAG_SHMEM)) {
668
-					prev_r->before = r;
669
-					free_lump(foo);
670
-					pkg_free(foo);
671
-				} else {
672
-					prev_r = foo;
673
-				}
674
-			}
675
-			/* go to next lump */
676
-			prev = &(crt->next);
677
-			crt = crt->next;
678
-		}
679
-	}
680
-}
681
-
682
-unsigned int count_applied_lumps(struct lump *ll, int type)
683
-{
684
-	unsigned int n = 0;
685
-	struct lump *l = 0;
686
-
687
-	for(l=ll; l; l=l->next) {
688
-		if (l->op==LUMP_NOP && l->type==type) {
689
-			if (l->after && l->after->op==LUMP_ADD_OPT) {
690
-				if (LUMP_IS_COND_TRUE(l->after)) {
691
-					n++;
692
-				}
693
-			} else {
694
-				n++;
695
-			}
696
-		}
697
-	}
698
-	return n;
699
-}
700
-
701
-/**
702
- * remove a lump in a root list
703
- * - it doesn't look for lumps added as before/after rule
704
- * - destroys the entire lump, with associated before/after rules
705
- */
706
-int remove_lump(sip_msg_t *msg, struct lump *l)
707
-{
708
-	struct lump *t = NULL;
709
-	struct lump *prev = NULL;
710
-	struct lump **list = NULL;
711
-
712
-	list=&msg->add_rm;	
713
-	for (t=*list; t; prev=t, t=t->next) {
714
-		if(t==l)
715
-			break;
716
-	}
717
-	if(t==NULL) {
718
-		list=&msg->body_lumps;
719
-		for (t=*list; t; prev=t, t=t->next) {
720
-			if(t==l)
721
-				break;
722
-		}
723
-	}
724
-	if(t!=NULL) {
725
-		if(prev==NULL) {
726
-			*list = t->next;
727
-		} else {
728
-			prev->next = t->next;
729
-		}
730
-		/* detach and free all its content */
731
-		t->next = NULL;
732
-		free_lump_list(t);
733
-		return 1;
734
-	}
735
-	return 0;
736
-}
737
-
738
-/**
739
- *
740
- */
741
-int sr_hdr_add(sip_msg_t *msg, str *sname, str *sbody)
742
-{
743
-	struct lump* anchor;
744
-	str h;
745
-
746
-	parse_headers(msg, HDR_EOH_F, 0);
747
-	if(msg->last_header == 0) {
748
-		LM_ERR("failed to parse headers\n");
749
-		return -1;
750
-	}
751
-	h.len = sname->len + 2 + sbody->len + CRLF_LEN;
752
-	h.s = (char*)pkg_malloc(h.len+1);
753
-	if(h.s == 0) {
754
-		LM_ERR("no more pkg\n");
755
-		return -1;
756
-	}
757
-	anchor = anchor_lump(msg, msg->last_header->name.s + msg->last_header->len
758
-					- msg->buf, 0, 0);
759
-	if(anchor == 0)
760
-	{
761
-		LM_ERR("cannot get the anchor\n");
762
-		pkg_free(h.s);
763
-		return -1;
764
-	}
765
-	memcpy(h.s, sname->s, sname->len);
766
-	memcpy(h.s+sname->len, ": ", 2);
767
-	memcpy(h.s+sname->len+2, sbody->s, sbody->len);
768
-	memcpy(h.s+sname->len+2+sbody->len, CRLF, CRLF_LEN);
769
-	h.s[h.len] = '\0';
770
-	if (insert_new_lump_after(anchor, h.s, h.len, 0) == 0)
771
-	{
772
-		LM_ERR("cannot insert lump\n");
773
-		pkg_free(h.s);
774
-		return -1;
775
-	}
776
-	LM_DBG("added new header (%d) [%s]\n", h.len, h.s);
777
-	return 0;
778
-}
779
-
780
-/**
781
- *
782
- */
783
-int sr_hdr_add_zz(sip_msg_t *msg, char *hname, char *hbody)
784
-{
785
-	str sname;
786
-	str sbody;
787
-
788
-	sname.s = hname;
789
-	sname.len = strlen(sname.s);
790
-	sbody.s = hbody;
791
-	sbody.len = strlen(sbody.s);
792
-
793
-	return sr_hdr_add(msg, &sname, &sbody);
794
-}
795
-
796
-/**
797
- *
798
- */
799
-int sr_hdr_add_zs(sip_msg_t *msg, char *hname, str *sbody)
800
-{
801
-	str sname;
802
-
803
-	sname.s = hname;
804
-	sname.len = strlen(sname.s);
805
-
806
-	return sr_hdr_add(msg, &sname, sbody);
807
-}
808
-
809
-/**
810
- *
811
- */
812
-hdr_field_t *sr_hdr_get_z(sip_msg_t *msg, char *hname)
813
-{
814
-	hdr_field_t *hf;
815
-	str sname;
816
-
817
-	sname.s = hname;
818
-	sname.len = strlen(sname.s);
819
-
820
-	for (hf=msg->headers; hf; hf=hf->next) {
821
-		if (hf->name.len==sname.len
822
-				&& strncasecmp(hf->name.s, sname.s,
823
-					sname.len)==0) {
824
-			return hf;
825
-
826
-		}
827
-	}
828
-	return NULL;
829
-}
830
-
831
-/**
832
- *
833
- */
834
-int sr_hdr_del_z(sip_msg_t *msg, char *hname)
835
-{
836
-	hdr_field_t *hf;
837
-	struct lump* l;
838
-	str sname;
839
-
840
-	sname.s = hname;
841
-	sname.len = strlen(sname.s);
842
-
843
-	for (hf=msg->headers; hf; hf=hf->next) {
844
-		if (hf->name.len==sname.len
845
-				&& strncasecmp(hf->name.s, sname.s,
846
-					sname.len)==0) {
847
-			l=del_lump(msg, hf->name.s-msg->buf, hf->len, 0);
848
-			if (l==0) {
849
-				LM_ERR("unable to delete cookie header\n");
850
-				return -1;
851
-			}
852
-			return 0;
853
-		}
854
-	}
855
-	return 0;
856
-}
Browse code

core: print the len for added header in debug message

Daniel-Constantin Mierla authored on 13/10/2016 12:27:44
Showing 1 changed files
... ...
@@ -773,7 +773,7 @@ int sr_hdr_add(sip_msg_t *msg, str *sname, str *sbody)
773 773
 		pkg_free(h.s);
774 774
 		return -1;
775 775
 	}
776
-	LM_DBG("added new header [%s]\n", h.s);
776
+	LM_DBG("added new header (%d) [%s]\n", h.len, h.s);
777 777
 	return 0;
778 778
 }
779 779
 
Browse code

core: rely on msg->last_header to get the anchor inside sr_hdr_add()

- might fix GH #789

Daniel-Constantin Mierla authored on 20/09/2016 09:40:07
Showing 1 changed files
... ...
@@ -743,13 +743,19 @@ int sr_hdr_add(sip_msg_t *msg, str *sname, str *sbody)
743 743
 	struct lump* anchor;
744 744
 	str h;
745 745
 
746
+	parse_headers(msg, HDR_EOH_F, 0);
747
+	if(msg->last_header == 0) {
748
+		LM_ERR("failed to parse headers\n");
749
+		return -1;
750
+	}
746 751
 	h.len = sname->len + 2 + sbody->len + CRLF_LEN;
747 752
 	h.s = (char*)pkg_malloc(h.len+1);
748 753
 	if(h.s == 0) {
749 754
 		LM_ERR("no more pkg\n");
750 755
 		return -1;
751 756
 	}
752
-	anchor = anchor_lump(msg, msg->unparsed - msg->buf, 0, 0);
757
+	anchor = anchor_lump(msg, msg->last_header->name.s + msg->last_header->len
758
+					- msg->buf, 0, 0);
753 759
 	if(anchor == 0)
754 760
 	{
755 761
 		LM_ERR("cannot get the anchor\n");
Browse code

core: add header helper uses after lump anchor insert

Daniel-Constantin Mierla authored on 13/07/2016 08:17:04
Showing 1 changed files
... ...
@@ -743,7 +743,7 @@ int sr_hdr_add(sip_msg_t *msg, str *sname, str *sbody)
743 743
 	struct lump* anchor;
744 744
 	str h;
745 745
 
746
-	h.len = sname->len + 2 + sbody->len + 1 + CRLF_LEN;
746
+	h.len = sname->len + 2 + sbody->len + CRLF_LEN;
747 747
 	h.s = (char*)pkg_malloc(h.len+1);
748 748
 	if(h.s == 0) {
749 749
 		LM_ERR("no more pkg\n");
... ...
@@ -761,7 +761,7 @@ int sr_hdr_add(sip_msg_t *msg, str *sname, str *sbody)
761 761
 	memcpy(h.s+sname->len+2, sbody->s, sbody->len);
762 762
 	memcpy(h.s+sname->len+2+sbody->len, CRLF, CRLF_LEN);
763 763
 	h.s[h.len] = '\0';
764
-	if (insert_new_lump_before(anchor, h.s, h.len, 0) == 0)
764
+	if (insert_new_lump_after(anchor, h.s, h.len, 0) == 0)
765 765
 	{
766 766
 		LM_ERR("cannot insert lump\n");
767 767
 		pkg_free(h.s);
Browse code

core: helper functions to add headers with str parameters

Daniel-Constantin Mierla authored on 13/07/2016 06:40:56
Showing 1 changed files