Coverage Report

Created: 2026-12-01 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensips/dset.c
Line
Count
Source
1
/*
2
 * Copyright (C) 2001-2004 FhG FOKUS
3
 *
4
 * This file is part of opensips, a free SIP server.
5
 *
6
 * opensips 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
 * opensips 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 Destination set handling functions
24
 */
25
26
27
#include <string.h>
28
#include "dprint.h"
29
#include "config.h"
30
#include "parser/parser_f.h"
31
#include "parser/msg_parser.h"
32
#include "ut.h"
33
#include "hash_func.h"
34
#include "error.h"
35
#include "context.h"
36
#include "dset.h"
37
#include "mem/mem.h"
38
#include "ip_addr.h"
39
#include "usr_avp.h"
40
41
0
#define CONTACT "Contact: "
42
0
#define CONTACT_LEN (sizeof(CONTACT) - 1)
43
44
0
#define CONTACT_DELIM ", "
45
0
#define CONTACT_DELIM_LEN (sizeof(CONTACT_DELIM) - 1)
46
47
0
#define Q_PARAM ";q="
48
0
#define Q_PARAM_LEN (sizeof(Q_PARAM) - 1)
49
50
0
#define DSET_INCREMENT 4
51
52
53
/* This is an extension of the `msg_branch` struct, bringing the
54
 * buffers to hold the strings
55
 */
56
struct msg_branch_wrap
57
{
58
  struct msg_branch branch;
59
60
  char tag[MAX_URI_SIZE];
61
62
  char uri[MAX_URI_SIZE];
63
64
  char dst_uri[MAX_URI_SIZE];
65
66
  char path[MAX_PATH_SIZE];
67
};
68
69
70
struct dset_ctx
71
{
72
  int enabled;
73
74
  /*! how many of them we currently have */
75
  int nr_branches;
76
77
  /*!
78
   * Where we store URIs of additional transaction branches
79
   * (-1 because of the default branch, #0)
80
   */
81
  struct msg_branch_wrap *branches;
82
83
  /*!
84
   * this is the set of attrs (brnach avps) corresponding to the
85
   * RURI branch (the 0-branch); we do not want to keep them into the
86
   * SIP msg (due to cloning issues), so we store them here (anyhow
87
   * the lifespan of this hook is short, only during request route)
88
   */
89
  struct usr_avp *ruri_attrs;
90
};
91
92
static int dset_ctx_idx = -1;
93
94
#define get_dset_ctx() \
95
135k
  (!current_processing_ctx ? NULL : (struct dset_ctx *) \
96
135k
    context_get_ptr(CONTEXT_GLOBAL, current_processing_ctx, dset_ctx_idx))
97
98
#define store_dset_ctx(value) \
99
0
  (context_put_ptr( \
100
0
    CONTEXT_GLOBAL, current_processing_ctx, dset_ctx_idx, value))
101
102
103
104
int get_dset_size(void)
105
123k
{
106
123k
  struct dset_ctx *dsct = get_dset_ctx();
107
108
123k
  return !dsct ? 0 : dsct->nr_branches;
109
123k
}
110
111
/* empties/frees the content of a dset without free'ing the dset itself */
112
static inline void _empty_branches(struct dset_ctx *dsct)
113
0
{
114
0
  int i;
115
116
0
  for( i = 0 ; i < dsct->nr_branches ; i++ )
117
0
    destroy_avp_list( &dsct->branches[i].branch.attrs );
118
0
  pkg_free( dsct->branches );
119
0
  dsct->branches = NULL;
120
0
}
121
122
/*! Frees a destination set which used to be stored in the global context */
123
static void dset_destroy(void *dsct)
124
0
{
125
  /* emptry all branches */
126
0
  _empty_branches( (struct dset_ctx *)dsct );
127
  /* free attrs/avp for the RURI/0 branch */
128
0
  destroy_avp_list( &((struct dset_ctx *)dsct)->ruri_attrs );
129
0
  pkg_free( dsct );
130
0
}
131
132
133
int init_dset(void)
134
8
{
135
8
  dset_ctx_idx = context_register_ptr(CONTEXT_GLOBAL, dset_destroy);
136
8
  if (dset_ctx_idx < 0)
137
0
    return -1;
138
139
8
  return 0;
140
8
}
141
142
143
/*! \brief Disable/Enables parallel branch usage (read and write)
144
 */
145
void set_dset_state(unsigned char enable)
146
0
{
147
0
  struct dset_ctx *dsct = get_dset_ctx();
148
0
  static unsigned int bk_nr_branches;
149
150
0
  if (!dsct)
151
0
    return;
152
153
0
  if (enable) {
154
    /* enable dset usage */
155
0
    if (dsct->enabled) return; /* already enabled */
156
    /* enable read */
157
0
    dsct->nr_branches = bk_nr_branches;
158
0
    bk_nr_branches = 0;
159
    /* enable write */
160
0
    dsct->enabled = 1;
161
0
  } else {
162
    /* disable dset usage */
163
0
    if (!dsct->enabled) return; /* already disabled */
164
    /* disable read */
165
0
    bk_nr_branches = dsct->nr_branches;
166
0
    dsct->nr_branches = 0;
167
    /* disabel write */
168
0
    dsct->enabled = 0;
169
0
  }
170
0
}
171
172
173
/*! \brief Find the next brand from the destination set
174
 * \return Return the next branch from the dset
175
 * array, 0 is returned if there are no
176
 * more branches
177
 */
178
struct msg_branch* get_msg_branch(unsigned int idx)
179
12.3k
{
180
12.3k
  struct dset_ctx *dsct = get_dset_ctx();
181
182
12.3k
  if (dsct && idx < dsct->nr_branches) {
183
0
    return &dsct->branches[idx].branch;
184
12.3k
  } else {
185
12.3k
    return NULL;
186
12.3k
  }
187
12.3k
}
188
189
190
/*! \brief
191
 * Empty the dset array
192
 */
193
void clear_dset(void)
194
0
{
195
0
  struct dset_ctx *dsct = get_dset_ctx();
196
197
0
  if (dsct) {
198
0
    _empty_branches( dsct );
199
0
    dsct->nr_branches = 0;
200
0
  }
201
0
}
202
203
204
/* copies and fills in a branch_wrap with the values from a msg_branch 
205
 * IMPORTANT: if any attrs are attached the `branch`, they will be moved
206
 * into the dset!!! */
207
static inline int _set_msg_branch(struct msg_branch_wrap *br, 
208
                          struct msg_branch *branch)
209
0
{
210
  /* be sure and clear the attrs from target branch */
211
0
  destroy_avp_list( &br->branch.attrs );
212
213
  /* do full copy, copy the buffers later */
214
0
  br->branch = *branch;
215
216
  /* copy ruri */
217
0
  if (ZSTR(branch->uri) || branch->uri.len > MAX_URI_SIZE - 1) {
218
0
    LM_ERR("too long uri: [%.*s]/%d\n", branch->uri.len, branch->uri.s,
219
0
      branch->uri.len);
220
0
    return -1;
221
0
  }
222
0
  br->branch.uri.s = br->uri;
223
0
  memcpy( br->branch.uri.s, branch->uri.s, branch->uri.len);
224
225
  /* copy the dst_uri */
226
0
  if (!ZSTR(branch->dst_uri)) {
227
0
    if (branch->dst_uri.len > MAX_URI_SIZE - 1) {
228
0
      LM_ERR("too long dst_uri: [%.*s]/%d\n",
229
0
        branch->dst_uri.len, branch->dst_uri.s, branch->dst_uri.len);
230
0
      return -1;
231
0
    }
232
0
    br->branch.dst_uri.s = br->dst_uri;
233
0
    memcpy( br->branch.dst_uri.s, branch->dst_uri.s, branch->dst_uri.len);
234
0
  }
235
236
  /* copy the path string */
237
0
  if (!ZSTR(branch->path)) {
238
0
    if (branch->path.len > MAX_PATH_SIZE - 1) {
239
0
      LM_ERR("too long path: [%.*s]/%d\n",
240
0
        branch->path.len, branch->path.s, branch->path.len);
241
0
      return -1;
242
0
    }
243
0
    br->branch.path.s = br->path;
244
0
    memcpy( br->branch.path.s, branch->path.s, branch->path.len);
245
0
  }
246
0
  return 0;
247
0
}
248
249
250
static inline int _dst_malloc(struct dset_ctx **dsct)
251
0
{
252
0
  *dsct = pkg_malloc(sizeof **dsct);
253
0
  if (*dsct==NULL) {
254
0
    LM_ERR("oom 1\n");
255
0
    return E_OUT_OF_MEM;
256
0
  }
257
0
  memset(*dsct, 0, sizeof **dsct);
258
0
  (*dsct)->enabled = 1;
259
0
  store_dset_ctx(*dsct);
260
0
  return 0;
261
0
}
262
263
#define _post_copy_branch_update(_br)             \
264
0
  do {                                          \
265
0
    (_br)->branch.uri.s = (_br)->uri;         \
266
0
    (_br)->branch.dst_uri.s = (_br)->dst_uri; \
267
0
    (_br)->branch.path.s = (_br)->path;       \
268
0
  } while(0)
269
270
/* ! \brief
271
 * Add a new branch to current transaction
272
 */
273
int append_msg_branch(struct msg_branch *branch)
274
0
{
275
0
  int idx, i;
276
0
  struct msg_branch_wrap *new_br;
277
0
  struct dset_ctx *dsct = get_dset_ctx();
278
279
0
  if (dsct && !dsct->enabled)
280
0
    return -1;
281
282
0
  if (dsct==NULL &&  _dst_malloc(&dsct)<0 )
283
0
    return -1;
284
285
0
  idx = dsct->nr_branches;
286
287
  /* if we have already set up the maximum number
288
   * of branches, don't try new ones
289
   */
290
0
  if (idx == MAX_BRANCHES - 1) {
291
0
    LM_ERR("max nr of branches exceeded\n");
292
0
    ser_error = E_TOO_MANY_BRANCHES;
293
0
    return -1;
294
0
  }
295
296
0
  if (idx % DSET_INCREMENT == 0) {
297
0
    new_br = pkg_realloc(dsct->branches,
298
0
                      (idx + DSET_INCREMENT) * sizeof *dsct->branches);
299
0
    if (!new_br) {
300
0
      LM_ERR("oom 2\n");
301
0
      return E_OUT_OF_MEM;
302
0
    }
303
0
    memset((char *)new_br + idx * sizeof *new_br, 0,
304
0
        DSET_INCREMENT * sizeof *new_br);
305
    /* the array may have moved, update internal links */
306
0
    for ( i=0 ; i<idx ; i++)
307
0
      _post_copy_branch_update( new_br+i );
308
309
0
    dsct->branches = new_br;
310
0
  }
311
312
0
  if (_set_msg_branch( dsct->branches + idx, branch)<0)
313
0
    return -1;
314
315
0
  dsct->nr_branches++;
316
0
  return 1;
317
0
}
318
319
320
/* ! \brief
321
 * Updates URI of an already appended branch
322
 */
323
int update_msg_branch_uri(unsigned int idx, str *val)
324
0
{
325
0
  struct dset_ctx *dsct = get_dset_ctx();
326
0
  struct msg_branch_wrap *br;
327
328
0
  if (!dsct || !dsct->enabled || idx >= dsct->nr_branches || ZSTRP(val))
329
0
    return -1;
330
331
0
  br = dsct->branches + idx;
332
333
0
  if (val->len > MAX_URI_SIZE - 1) {
334
0
    LM_ERR("too long uri: [%.*s]/%d\n", val->len, val->s, val->len);
335
0
    return -1;
336
0
  }
337
0
  br->branch.uri.s = br->uri; /* internal buffer */
338
0
  br->branch.uri.len = val->len;
339
0
  memcpy( br->branch.uri.s, val->s, val->len);
340
341
0
  return 0;
342
0
}
343
344
345
/* ! \brief
346
 * Updates DST_URI of an already appended branch
347
 */
348
int update_msg_branch_dst_uri(unsigned int idx, str *val)
349
0
{
350
0
  struct dset_ctx *dsct = get_dset_ctx();
351
0
  struct msg_branch_wrap *br;
352
353
0
  if (!dsct || !dsct->enabled || idx >= dsct->nr_branches)
354
0
    return -1;
355
356
0
  br = dsct->branches + idx;
357
358
0
  if (ZSTRP(val)) {
359
0
    br->branch.dst_uri.s = NULL;
360
0
    br->branch.dst_uri.len = 0;
361
0
  } else {
362
0
    if (val->len > MAX_URI_SIZE - 1) {
363
0
      LM_ERR("too long dst_uri: [%.*s]/%d\n", val->len, val->s,val->len);
364
0
      return -1;
365
0
    }
366
0
    br->branch.dst_uri.s = br->dst_uri; /* internal buffer */
367
0
    br->branch.dst_uri.len = val->len;
368
0
    memcpy( br->branch.dst_uri.s, val->s, val->len);
369
0
  }
370
0
  return 0;
371
0
}
372
373
374
375
/* ! \brief
376
 * Updates PATH of an already appended branch
377
 */
378
int update_msg_branch_path(unsigned int idx, str *val)
379
0
{
380
0
  struct dset_ctx *dsct = get_dset_ctx();
381
0
  struct msg_branch_wrap *br;
382
383
0
  if (!dsct || !dsct->enabled || idx >= dsct->nr_branches)
384
0
    return -1;
385
386
0
  br = dsct->branches + idx;
387
388
0
  if (ZSTRP(val)) {
389
0
    br->branch.path.s = NULL;
390
0
    br->branch.path.len = 0;
391
0
  } else {
392
0
    if (val->len > MAX_PATH_SIZE - 1) {
393
0
      LM_ERR("too long path: [%.*s]/%d\n", val->len, val->s,val->len);
394
0
      return -1;
395
0
    }
396
0
    br->branch.path.s = br->path; /* internal buffer */
397
0
    br->branch.path.len = val->len;
398
0
    memcpy( br->branch.path.s, val->s, val->len);
399
0
  }
400
0
  return 0;
401
0
}
402
403
404
/* ! \brief
405
 * Updates PATH of an already appended branch
406
 */
407
int update_msg_branch_q(unsigned int idx, int val)
408
0
{
409
0
  struct dset_ctx *dsct = get_dset_ctx();
410
0
  struct msg_branch_wrap *br;
411
412
0
  if (!dsct || !dsct->enabled || idx >= dsct->nr_branches)
413
0
    return -1;
414
415
0
  br = dsct->branches + idx;
416
417
0
  br->branch.q = val;
418
0
  return 0;
419
0
}
420
421
422
/* ! \brief
423
 * Updates SOCKET of an already appended branch
424
 */
425
int update_msg_branch_socket(unsigned int idx, const struct socket_info* val)
426
0
{
427
0
  struct dset_ctx *dsct = get_dset_ctx();
428
0
  struct msg_branch_wrap *br;
429
430
0
  if (!dsct || !dsct->enabled || idx >= dsct->nr_branches)
431
0
    return -1;
432
433
0
  br = dsct->branches + idx;
434
435
0
  br->branch.force_send_socket = val;
436
0
  return 0;
437
0
}
438
439
440
/* ! \brief
441
 * Updates SOCKET of an already appended branch
442
 */
443
int update_msg_branch_bflags(unsigned int idx, unsigned int val)
444
0
{
445
0
  struct dset_ctx *dsct = get_dset_ctx();
446
0
  struct msg_branch_wrap *br;
447
448
0
  if (!dsct || !dsct->enabled || idx >= dsct->nr_branches)
449
0
    return -1;
450
451
0
  br = dsct->branches + idx;
452
453
0
  br->branch.bflags = val;
454
0
  return 0;
455
0
}
456
457
458
/*! \brief
459
 * Removes a msg branch by index. The whole array gets shifted, so the
460
 * indexes inside may change
461
 */
462
int remove_msg_branch(unsigned int idx)
463
0
{
464
0
  struct dset_ctx *dsct = get_dset_ctx();
465
0
  int i;
466
467
0
  if (!dsct || !dsct->enabled || idx >= dsct->nr_branches)
468
0
    return -1;
469
470
  /* destroy any attrs the branch may have */
471
0
  destroy_avp_list( &dsct->branches[idx].branch.attrs );
472
473
  /* not last branch? */
474
0
  if (idx + 1 != dsct->nr_branches) {
475
0
    memmove( dsct->branches + idx, dsct->branches + idx + 1,
476
0
      (dsct->nr_branches - idx - 1) * sizeof *dsct->branches);
477
    /* update internal links */
478
0
    for ( i=idx ; i<dsct->nr_branches-1 ; i++)
479
0
      _post_copy_branch_update( dsct->branches+i );
480
0
  }
481
482
0
  dsct->nr_branches--;
483
  /* cleanup the slot not used anymore */
484
0
  memset(  dsct->branches + dsct->nr_branches, 0, sizeof *dsct->branches );
485
486
0
  return 0;
487
0
}
488
489
490
/*! \brief
491
 * Create a Contact header field from the dset
492
 * array
493
 */
494
char* print_dset(struct sip_msg* msg, int* len)
495
12.3k
{
496
12.3k
  int cnt, i, idx;
497
12.3k
  unsigned int qlen;
498
12.3k
  char* p, *qbuf;
499
12.3k
  static char *dset = NULL;
500
12.3k
  static unsigned int dset_len = 0;
501
12.3k
  struct msg_branch *br;
502
503
12.3k
  if (msg->new_uri.s) {
504
0
    cnt = 1;
505
0
    *len = msg->new_uri.len+2 /*for <>*/;
506
0
    if (get_ruri_q(msg) != Q_UNSPECIFIED) {
507
0
      *len += Q_PARAM_LEN + len_q(get_ruri_q(msg));
508
0
    }
509
12.3k
  } else {
510
12.3k
    cnt = 0;
511
12.3k
    *len = 0;
512
12.3k
  }
513
514
12.3k
  for( idx=0 ; (br=get_msg_branch(idx))!=NULL ; idx++ ) {
515
0
    cnt++;
516
0
    *len += br->uri.len+2 /*for <>*/ ;
517
0
    if (br->q != Q_UNSPECIFIED) {
518
0
      *len += Q_PARAM_LEN + len_q(br->q);
519
0
    }
520
0
  }
521
522
12.3k
  if (cnt == 0) return 0;
523
524
0
  *len += CONTACT_LEN + CRLF_LEN + (cnt - 1) * CONTACT_DELIM_LEN;
525
526
  /* does the current buffer fit the new dset ? */
527
0
  if (*len + 1 > dset_len) {
528
    /* need to resize */
529
0
    dset = pkg_realloc(dset, *len + 1);
530
0
    if (!dset) {
531
0
      dset_len = 0;
532
0
      LM_ERR("failed to allocate redirect buffer for %d bytes\n", *len + 1);
533
0
      return NULL;
534
0
    }
535
0
    dset_len = *len + 1;
536
0
  }
537
538
0
  memcpy(dset, CONTACT, CONTACT_LEN);
539
0
  p = dset + CONTACT_LEN;
540
0
  if (msg->new_uri.s) {
541
0
    *p++ = '<';
542
0
    memcpy(p, msg->new_uri.s, msg->new_uri.len);
543
0
    p += msg->new_uri.len;
544
0
    *p++ = '>';
545
546
0
    if (get_ruri_q(msg) != Q_UNSPECIFIED) {
547
0
      memcpy(p, Q_PARAM, Q_PARAM_LEN);
548
0
      p += Q_PARAM_LEN;
549
550
0
      qbuf = q2str(get_ruri_q(msg), &qlen);
551
0
      memcpy(p, qbuf, qlen);
552
0
      p += qlen;
553
0
    }
554
0
    i = 1;
555
0
  } else {
556
0
    i = 0;
557
0
  }
558
559
0
  for( idx=0 ; (br=get_msg_branch(idx))!=NULL ; idx++ ) {
560
0
    if (i) {
561
0
      memcpy(p, CONTACT_DELIM, CONTACT_DELIM_LEN);
562
0
      p += CONTACT_DELIM_LEN;
563
0
    }
564
565
0
    *p++ = '<';
566
0
    memcpy(p, br->uri.s, br->uri.len);
567
0
    p += br->uri.len;
568
0
    *p++ = '>';
569
570
0
    if (br->q != Q_UNSPECIFIED) {
571
0
      memcpy(p, Q_PARAM, Q_PARAM_LEN);
572
0
      p += Q_PARAM_LEN;
573
574
0
      qbuf = q2str(br->q, &qlen);
575
0
      memcpy(p, qbuf, qlen);
576
0
      p += qlen;
577
0
    }
578
0
    i++;
579
0
  }
580
581
0
  memcpy(p, CRLF " ", CRLF_LEN + 1);
582
0
  return dset;
583
0
}
584
585
586
/*! \brief moves the uri to destination for all branches and
587
 * all uris are set to given uri */
588
int msg_branch_uri2dset( str *new_uri )
589
0
{
590
0
  struct dset_ctx *dsct = get_dset_ctx();
591
0
  struct msg_branch *branch;
592
0
  unsigned int b;
593
594
  /* no branches have been added yet */
595
0
  if (!dsct)
596
0
    return 0;
597
598
0
  if (new_uri->len+1 > MAX_URI_SIZE) {
599
0
    LM_ERR("new uri too long (%d)\n",new_uri->len);
600
0
    return -1;
601
0
  }
602
603
0
  for (b = 0; b < dsct->nr_branches; b++) {
604
0
    branch = &(dsct->branches[b].branch);
605
    /* move uri to dst */
606
0
    memcpy(branch->dst_uri.s, branch->uri.s, branch->uri.len + 1);
607
0
    branch->dst_uri.len = branch->uri.len;
608
    /* set new uri */
609
0
    memcpy(branch->uri.s, new_uri->s, new_uri->len);
610
0
    branch->uri.len =  new_uri->len;
611
0
    branch->uri.s[new_uri->len] = '\0';
612
0
  }
613
614
0
  return 0;
615
0
}
616
617
618
static inline int _branch_2_msg(struct dset_ctx *dsct,
619
              struct msg_branch_wrap *br,struct sip_msg *msg)
620
0
{
621
0
  if (set_ruri( msg, &br->branch.uri))
622
0
    return -1;
623
624
0
  if (set_dst_uri( msg, &br->branch.dst_uri))
625
0
    return -1;
626
627
0
  if (set_path_vector( msg, &br->branch.path))
628
0
    return -1;
629
630
0
  msg->ruri_q = br->branch.q;
631
0
  msg->force_send_socket = br->branch.force_send_socket;
632
0
  msg->ruri_bflags = br->branch.bflags;
633
634
0
  destroy_avp_list( &dsct->ruri_attrs );
635
0
  dsct->ruri_attrs = clone_avp_list(br->branch.attrs);
636
637
0
  return 0;
638
0
}
639
640
641
static inline int _msg_2_branch(struct dset_ctx *dsct,
642
              struct sip_msg *msg, struct msg_branch_wrap *br)
643
0
{
644
0
  struct msg_branch branch;
645
646
  /* run tests first */
647
0
  memset( &branch, 0, sizeof branch);
648
0
  branch.uri = *GET_RURI(msg);
649
0
  branch.dst_uri = msg->dst_uri;
650
0
  branch.path = msg->path_vec;
651
0
  branch.q = msg->ruri_q;
652
0
  branch.force_send_socket = msg->force_send_socket;
653
0
  branch.bflags = msg->ruri_bflags;
654
0
  branch.attrs = clone_avp_list(dsct->ruri_attrs);
655
  /* the cloned list ^^ will remain attached to the branch */
656
657
0
  if (_set_msg_branch( br, &branch)<0)
658
0
    return -1;
659
660
0
  return 0;
661
0
}
662
663
664
static inline int _copy_branch(struct sip_msg *msg, struct dset_ctx *dsct,
665
                          int src_idx, int dst_idx)
666
0
{
667
0
  struct msg_branch_wrap *brs = dsct->branches;
668
0
  int ret = 0;
669
670
0
  if (dst_idx>=0) {
671
    /* we copy into a branch */
672
0
    if (src_idx>=0) {
673
      /* we copy from a branch */
674
      /* destroy the avps of the dst branch before the bulk copy */
675
0
      destroy_avp_list( &brs[dst_idx].branch.attrs );
676
      /* do the copy */
677
0
      brs[dst_idx] = brs[src_idx];
678
0
      _post_copy_branch_update( brs+dst_idx );
679
      /* clone the list of attrs from src to dst now */
680
0
      brs[dst_idx].branch.attrs =
681
0
        clone_avp_list( brs[src_idx].branch.attrs );
682
0
    } else {
683
      /* we copy from msg */
684
0
      ret = _msg_2_branch( dsct, msg, &brs[dst_idx]);
685
0
    }
686
0
  } else {
687
    /* we copy into msg */
688
0
    if (src_idx>=0) {
689
      /* we copy from a branch */
690
0
      ret = _branch_2_msg( dsct, &brs[src_idx], msg);
691
0
    } else {
692
      /* this should not happen, it is a NOP */
693
0
    }
694
0
  }
695
696
0
  return ret;
697
0
}
698
699
700
int move_msg_branch_to_ruri(int idx, struct sip_msg *msg)
701
0
{
702
0
  struct dset_ctx *dsct = get_dset_ctx();
703
704
  /* no branches have been added yet */
705
0
  if (!dsct) {
706
0
    LM_DBG("no branches found\n");
707
0
    return -1;
708
0
  }
709
710
0
  if (idx >= dsct->nr_branches) {
711
0
    LM_DBG("trying to move inexisting branch idx %d, out of %d\n",
712
0
      idx, dsct->nr_branches);
713
0
    return -1;
714
0
  }
715
716
0
  if (_branch_2_msg( dsct, &dsct->branches[idx], msg)!=0) {
717
0
    LM_ERR("failed to move brnach to RURI\n");
718
0
    return -1;
719
0
  }
720
721
0
  return 0;
722
0
}
723
724
725
int swap_msg_branches(struct sip_msg *msg, int src_idx, int dst_idx)
726
0
{
727
0
  struct dset_ctx *dsct = get_dset_ctx();
728
0
  struct msg_branch_wrap *brs;
729
0
  struct msg_branch_wrap bk;
730
0
  struct usr_avp *bk_attrs;
731
732
0
  if (src_idx==dst_idx)
733
    /* this is a NOP */
734
0
    return 0;
735
736
  /* no branches have been added yet */
737
0
  if (!dsct)
738
0
    return -1;
739
740
0
  if ( (src_idx>0 && src_idx>=dsct->nr_branches)
741
0
  || (dst_idx>0 && dst_idx>=dsct->nr_branches) ) {
742
0
    LM_ERR("overflow in src [%d] or dst [%d] indexes (having %d)\n",
743
0
      src_idx, dst_idx, dsct->nr_branches);
744
0
    return -1;
745
0
  }
746
747
0
  brs = dsct->branches;
748
749
  /* to avoid useless cloning of the attr lists, better detach them
750
   * before doing anything and swap them at the end */
751
752
  /* backup the info from dst branch, so we can write into it */
753
0
  if (dst_idx>=0) {
754
    /* detach the attrs */
755
0
    bk_attrs = brs[dst_idx].branch.attrs;
756
0
    brs[dst_idx].branch.attrs = NULL;
757
    /* backup the dst branch */
758
0
    bk = brs[dst_idx];
759
0
    _post_copy_branch_update( &bk );
760
0
  } else {
761
    /* detach the attrs */
762
0
    bk_attrs = dsct->ruri_attrs;
763
0
    dsct->ruri_attrs = NULL;
764
    /* backup the msg branch */
765
0
    if (_msg_2_branch(dsct, msg, &bk)<0)
766
0
      return -1;
767
0
  }
768
769
  /* copy dst over src */
770
0
  if (_copy_branch( msg, dsct, src_idx, dst_idx)<0)
771
0
    return -1;
772
773
  /* now copy the original dst (from bk) into src */
774
0
  if (src_idx>=0) {
775
    /* copy bk into a branch */
776
0
    brs[src_idx] = bk;
777
0
    _post_copy_branch_update( brs+src_idx );
778
    /* attach the dst list of attrs */
779
0
    brs[src_idx].branch.attrs = bk_attrs;
780
0
  } else {
781
    /* copy bk in msg */
782
0
    if (_branch_2_msg( dsct, &bk, msg)<0)
783
0
      return -1; //we may have an inconsistent msg branch :(
784
    /* attach the dst list of attrs */
785
0
    dsct->ruri_attrs = bk_attrs;
786
0
  }
787
788
0
  return 0;
789
0
}
790
791
792
int move_msg_branch(struct sip_msg *msg, int src_idx, int dst_idx,
793
                              int keep_src)
794
0
{
795
0
  struct dset_ctx *dsct = get_dset_ctx();
796
797
0
  if (src_idx==dst_idx)
798
    /* this is a NOP */
799
0
    return 0;
800
801
  /* no branches have been added yet */
802
0
  if (!dsct)
803
0
    return -1;
804
805
0
  if ( (src_idx>0 && src_idx>=dsct->nr_branches)
806
0
  || (dst_idx>0 && dst_idx>=dsct->nr_branches) ) {
807
0
    LM_ERR("overflow in src [%d] or dst [%d] indexes (having %d)\n",
808
0
      src_idx, dst_idx, dsct->nr_branches);
809
0
    return -1;
810
0
  }
811
812
  /* copy dst over src */
813
0
  if (_copy_branch( msg, dsct, src_idx, dst_idx)<0)
814
0
    return -1;
815
816
0
  if (!keep_src && src_idx>0)
817
0
    remove_msg_branch(src_idx);
818
819
0
  return 0;
820
0
}
821
822
823
/**** Functions to work with the members ****/
824
825
static inline unsigned int* get_ptr_bflags(struct sip_msg *msg,
826
                            unsigned int b_idx)
827
0
{
828
0
  struct dset_ctx *dsct = get_dset_ctx();
829
830
0
  if (!dsct && b_idx != 0)
831
0
    return NULL;
832
833
0
  if (b_idx == 0) {
834
0
    return &getb0flags(msg);
835
0
  } else {
836
0
    if (b_idx - 1 < dsct->nr_branches) {
837
0
      return &dsct->branches[b_idx - 1].branch.bflags;
838
0
    } else {
839
0
      return 0;
840
0
    }
841
0
  }
842
0
}
843
844
int setbflag(struct sip_msg *msg, unsigned int b_idx, unsigned int mask)
845
0
{
846
0
  unsigned int *flags;
847
848
0
  flags = get_ptr_bflags( msg, b_idx );
849
#ifdef EXTRA_DEBUG
850
  LM_DBG("bflags for %p : (%u, %u)\n", msg, mask, *flags);
851
#endif
852
0
  if (flags==0)
853
0
    return -1;
854
855
0
  (*flags) |= mask;
856
0
  return 1;
857
0
}
858
859
860
/*! \brief
861
 * Tests the per branch flags
862
 */
863
int isbflagset(struct sip_msg *msg, unsigned int b_idx, unsigned int mask)
864
0
{
865
0
  unsigned int *flags;
866
867
0
  flags = get_ptr_bflags( msg, b_idx );
868
#ifdef EXTRA_DEBUG
869
  LM_DBG("bflags for %p : (%u, %u)\n", msg, mask, *flags);
870
#endif
871
0
  if (flags==0)
872
0
    return -1;
873
874
0
  return ( (*flags) & mask) ? 1 : -1;
875
0
}
876
877
878
/*! \brief
879
 * Resets the per branch flags
880
 */
881
int resetbflag(struct sip_msg *msg, unsigned int b_idx, unsigned int mask)
882
0
{
883
0
  unsigned int *flags;
884
885
0
  flags = get_ptr_bflags( msg, b_idx );
886
#ifdef EXTRA_DEBUG
887
  LM_DBG("bflags for %p : (%u, %u)\n", msg, mask, *flags);
888
#endif
889
0
  if (flags==0)
890
0
    return -1;
891
892
0
  (*flags) &= ~mask;
893
0
  return 1;
894
0
}
895
896
897
int get_msg_branch_attr(unsigned int b_idx, int name_id,
898
                  unsigned short *flags, int_str *val)
899
0
{
900
0
  struct dset_ctx *dsct = get_dset_ctx();
901
0
  struct usr_avp **attrs;
902
0
  struct usr_avp *avp;
903
0
  struct usr_avp** old_list;
904
905
0
  if (!dsct)
906
0
    return -1;
907
908
0
  if (b_idx==0)
909
0
    attrs = &(dsct->ruri_attrs);
910
0
  else if (b_idx-1 < get_dset_size())
911
0
    attrs = &dsct->branches[b_idx - 1].branch.attrs;
912
0
  else {
913
0
    LM_DBG("index %d out of rante (available branches %d)\n",
914
0
      b_idx, get_dset_size() );
915
0
    return -1;
916
0
  }
917
918
0
  LM_DBG("getting attr [%d] on branch %d/ptr=%p\n",name_id, b_idx, attrs);
919
920
  /* operate on the list of ATTRS/AVPS of the branch */
921
0
  old_list = set_avp_list( attrs );
922
923
0
  avp = search_first_avp(0, name_id, val, 0);
924
925
0
  set_avp_list( old_list );
926
927
0
  if (avp)
928
0
    *flags = avp->flags;
929
0
  else
930
0
    *flags = AVP_VAL_NULL;
931
932
0
  return avp ? 1 : -1 ;
933
0
}
934
935
936
int set_msg_branch_attr(unsigned int b_idx, int name_id,
937
                    unsigned short flags, int_str val)
938
0
{
939
0
  struct dset_ctx *dsct = get_dset_ctx();
940
0
  struct usr_avp **attrs;
941
0
  struct usr_avp *avp;
942
0
  struct usr_avp** old_list;
943
944
  /* if we have to set an ATTR for RURI branch, we need to have the dset
945
   * allocated (as the attr holder is there) */
946
0
  if (dsct==NULL && (b_idx!=0 || (b_idx==0 && _dst_malloc(&dsct)<0)) )
947
0
    return -1;
948
949
0
  if (b_idx==0)
950
0
    attrs = &(dsct->ruri_attrs);
951
0
  else if (b_idx-1 < get_dset_size())
952
0
    attrs = &dsct->branches[b_idx - 1].branch.attrs;
953
0
  else {
954
0
    LM_DBG("index %d out of rante (available branches %d)\n",
955
0
      b_idx, get_dset_size() );
956
0
    return -1;
957
0
  }
958
959
0
  LM_DBG("setting attr [%d] on branch %d/ptr=%p\n",name_id, b_idx, attrs);
960
961
  /* operate on the list of ATTRS/AVPS of the branch */
962
0
  old_list = set_avp_list( attrs );
963
964
0
  if ( (avp=search_first_avp( 0, name_id, NULL, 0))!=NULL )
965
0
    destroy_avp(avp);
966
967
0
  if ( !(flags&AVP_VAL_NULL) && add_avp(flags, name_id, val) < 0) {
968
0
    set_avp_list(old_list);
969
0
    return -1;
970
0
  }
971
972
0
  set_avp_list( old_list );
973
974
0
  return 1;
975
0
}
976
977
978
struct usr_avp **ruri_branch_attrs_head(void)
979
0
{
980
0
  struct dset_ctx *dsct = get_dset_ctx();
981
982
0
  if (!dsct)
983
0
    return NULL;
984
985
0
  return &dsct->ruri_attrs;
986
0
}