File: destination.c

package info (click to toggle)
opensips 2.2.2-3.1
  • links: PTS
  • area: main
  • in suites: bullseye, sid
  • size: 31,160 kB
  • sloc: ansic: 334,348; xml: 91,231; perl: 6,659; sh: 5,148; sql: 4,175; makefile: 3,167; yacc: 2,499; python: 1,197; cpp: 611; php: 573
file content (641 lines) | stat: -rw-r--r-- 17,259 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/*
 * opensips osp module.
 *
 * This module enables opensips to communicate with an Open Settlement
 * Protocol (OSP) server.  The Open Settlement Protocol is an ETSI
 * defined standard for Inter-Domain VoIP pricing, authorization
 * and usage exchange.  The technical specifications for OSP
 * (ETSI TS 101 321 V4.1.1) are available at www.etsi.org.
 *
 * Uli Abend was the original contributor to this module.
 *
 * Copyright (C) 2001-2005 Fhg Fokus
 *
 * This file is part of opensips, a free SIP server.
 *
 * opensips 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
 *
 * opensips 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
 */

#include <string.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "../../str.h"
#include "../../dprint.h"
#include "../../usr_avp.h"
#include "destination.h"
#include "usage.h"

extern int _osp_inbound_avpid;
extern int _osp_origdest_avpid;
extern int _osp_termdest_avpid;
extern int _osp_calling_avpid;
extern int _osp_destmedia_avpid;
extern unsigned short _osp_destmedia_avptype;

/* Name of AVP of OSP */
static str OSP_INBOUND_NAME = {"_osp_inbound_", 13};
static str OSP_ORIGDEST_NAME = {"_osp_orig_dests_", 16};
static str OSP_TERMDEST_NAME = {"_osp_term_dests_", 16};
static str OSP_CALLING_NAME = {"_osp_calling_translated_", 24};

static int ospSaveDestination(osp_dest* dest, int avpid);
static void ospRecordCode(int code, osp_dest* dest);
static int ospIsToReportUsage(int code);

/*
 * Parses avps used by this module
 * return 0 success, -1 failure
 */
int ospParseAvps(void)
{
    if (parse_avp_spec(&OSP_INBOUND_NAME, &_osp_inbound_avpid)) {
        LM_ERR("cannot get INBOUND AVP id\n");
        return -1;
    }

    if (parse_avp_spec(&OSP_ORIGDEST_NAME, &_osp_origdest_avpid)) {
        LM_ERR("cannot get ORIGDEST AVP id\n");
        return -1;
    }

    if (parse_avp_spec(&OSP_TERMDEST_NAME, &_osp_termdest_avpid)) {
        LM_ERR("cannot get TERMDEST AVP id\n");
        return -1;
    }

    if (parse_avp_spec(&OSP_CALLING_NAME, &_osp_calling_avpid)) {
        LM_ERR("cannot get CALLING AVP id\n");
        return -1;
    }

    return 0;
}

/*
 * Initialize inbound info structure
 * param inbound Inbound info data structure
 */
void ospInitInboundInfo(
    osp_inbound* inbound)
{
    memset(inbound, 0, sizeof(osp_inbound));
}

/*
 * Save inbound info as an AVP
 *     avpid - osp_inbound_avpid
 *     value - osp_inbound wrapped in a string
 * param inbound Inbound info structure
 * return 0 success, -1 failure
 */
int ospSaveInboundInfo(
    osp_inbound* inbound)
{
    str wrapper;
    int result = -1;

    wrapper.s = (char*)inbound;
    wrapper.len = sizeof(osp_inbound);

    /*
     * add_avp will make a private copy of both the avpid and value in shared
     * memory which will be released by TM at the end of the transaction
     */
    if (add_avp(AVP_VAL_STR, _osp_inbound_avpid, (int_str)wrapper) == 0) {
        LM_DBG("inbound info saved\n");
        result = 0;
    } else {
        LM_ERR("failed to save inbound info\n");
    }

    return result;
}

/*
 * Retrieved the inbound info from an AVP
 *     avpid - osp_inbound_avpid
 *     value - osp_inbound wrapped in a string
 *  return NULL on failure
 */
osp_inbound* ospGetInboundInfo(void)
{
    int_str inboundval;
    osp_inbound* inbound = NULL;

    if (search_first_avp(AVP_VAL_STR, _osp_inbound_avpid, &inboundval, 0) != NULL) {
        /* OSP inbound info is wrapped in a string */
        inbound = (osp_inbound*)inboundval.s.s;

        LM_DBG("inbound info found\n");
    }

    return inbound;
}

/*
 * Initialize destination structure
 * param dest Destination data structure
 * return initialized destination sturcture
 */
osp_dest* ospInitDestination(
    osp_dest* dest)
{
    memset(dest, 0, sizeof(osp_dest));

    dest->callidsize = sizeof(dest->callid);
    dest->tokensize = sizeof(dest->token);

    LM_DBG("callidsize '%d' tokensize '%d'\n", dest->callidsize, dest->tokensize);

    return dest;
}

/*
 * Save destination as an AVP
 *     avpid - osp_origdest_avpid / osp_termdest_avpid
 *     value - osp_dest wrapped in a string
 * param dest Destination structure
 * param avpid ID of AVP
 * return 0 success, -1 failure
 */
static int ospSaveDestination(
    osp_dest* dest,
    int avpid)
{
    str wrapper;
    int result = -1;

    wrapper.s = (char*)dest;
    wrapper.len = sizeof(osp_dest);

    /*
     * add_avp will make a private copy of both the avpid and value in shared
     * memory which will be released by TM at the end of the transaction
     */
    if (add_avp(AVP_VAL_STR, avpid, (int_str)wrapper) == 0) {
        LM_DBG("destination saved\n");
        result = 0;
    } else {
        LM_ERR("failed to save destination\n");
    }

    return result;
}

/*
 * Save originate destination
 * param dest Originate destination structure
 * return 0 success, -1 failure
 */
int ospSaveOrigDestination(
    osp_dest* dest)
{
    return ospSaveDestination(dest, _osp_origdest_avpid);
}

/*
 * Save terminate destination
 * param dest Terminate destination structure
 * return 0 success, -1 failure
 */
int ospSaveTermDestination(
    osp_dest* dest)
{
    return ospSaveDestination(dest, _osp_termdest_avpid);
}

/*
 * Check if there is an unused and supported originate destination from an AVP
 *     avpid - osp_origdest_avpid
 *     value - osp_dest wrapped in a string
 *     search unused (used==0) & supported (support==1)
 * return 0 success, -1 failure
 */
int ospCheckOrigDestination(void)
{
    struct usr_avp* destavp = NULL;
    int_str destval;
    osp_dest* dest = NULL;
    int result = -1;

    for (destavp = search_first_avp(AVP_VAL_STR, _osp_origdest_avpid, NULL, 0);
        destavp != NULL;
        destavp = search_next_avp(destavp, NULL))
    {
        get_avp_val(destavp, &destval);

        /* OSP destintaion is wrapped in a string */
        dest = (osp_dest*)destval.s.s;

        if (dest->used == 0) {
            if (dest->supported == 1) {
                LM_DBG("orig dest exist\n");
                result = 0;
                break;
            } else {
                /* Make it looks like used */
                dest->used = 1;
                /* 111 means wrong protocol */
                dest->lastcode = 111;
                LM_DBG("destination does not been supported\n");
            }
        } else {
            LM_DBG("destination has already been used\n");
        }
    }

    if (result == -1) {
        LM_DBG("there is not unused destination\n");
        ospReportOrigSetupUsage();
    }

    return result;
}

/*
 * Retrieved an unused and supported originate destination from an AVP
 *     avpid - osp_origdest_avpid
 *     value - osp_dest wrapped in a string
 *     There can be 0, 1 or more originate destinations.
 *     Find the 1st unused destination (used==0) & supported (support==1),
 *     return it, and mark it as used (used==1).
 * return NULL on failure
 */
osp_dest* ospGetNextOrigDestination(void)
{
    struct usr_avp* destavp = NULL;
    int_str destval;
    osp_dest* dest = NULL;
    osp_dest* result = NULL;

    for (destavp = search_first_avp(AVP_VAL_STR, _osp_origdest_avpid, NULL, 0);
        destavp != NULL;
        destavp = search_next_avp(destavp, NULL))
    {
        get_avp_val(destavp, &destval);

        /* OSP destintaion is wrapped in a string */
        dest = (osp_dest*)destval.s.s;

        if (dest->used == 0) {
            if (dest->supported == 1) {
                LM_DBG("orig dest found\n");
                dest->used = 1;
                result = dest;
                break;
            } else {
                /* Make it looks like used */
                dest->used = 1;
                /* 111 means wrong protocol */
                dest->lastcode = 111;
                LM_DBG("destination does not been supported\n");
            }
        } else {
            LM_DBG("destination has already been used\n");
        }
    }

    if (result == NULL) {
        LM_DBG("there is not unused destination\n");
    }

    return result;
}

/*
 * Retrieved the last used originate destination from an AVP
 *    avpid - osp_origdest_avpid
 *    value - osp_dest wrapped in a string
 *    There can be 0, 1 or more destinations.
 *    Find the last used destination (used==1) & supported (support==1),
 *    and return it.
 *    In normal condition, this one is the current destination. But it may
 *    be wrong for loop condition.
 *  return NULL on failure
 */
osp_dest* ospGetLastOrigDestination(void)
{
    struct usr_avp* destavp = NULL;
    int_str destval;
    osp_dest* dest = NULL;
    osp_dest* lastdest = NULL;

    for (destavp = search_first_avp(AVP_VAL_STR, _osp_origdest_avpid, NULL, 0);
        destavp != NULL;
        destavp = search_next_avp(destavp, NULL))
    {
        get_avp_val(destavp, &destval);

        /* OSP destination is wrapped in a string */
        dest = (osp_dest*)destval.s.s;

        if (dest->used == 1) {
            if (dest->supported == 1) {
                lastdest = dest;
                LM_DBG("curent destination '%s'\n", lastdest->host);
            }
        } else {
            break;
        }
    }

    return lastdest;
}

/*
 * Retrieved the terminate destination from an AVP
 *     avpid - osp_termdest_avpid
 *     value - osp_dest wrapped in a string
 *     There can be 0 or 1 term destinations. Find and return it.
 *  return NULL on failure (no terminate destination)
 */
osp_dest* ospGetTermDestination(void)
{
    int_str destval;
    osp_dest* dest = NULL;

    if (search_first_avp(AVP_VAL_STR, _osp_termdest_avpid, &destval, 0) != NULL) {
        /* OSP destination is wrapped in a string */
        dest = (osp_dest*)destval.s.s;

        LM_DBG("term dest found\n");
    }

    return dest;
}

/*
 * Record destination status
 * param code Destination status
 * param dest Destination
 */
static void ospRecordCode(
    int code,
    osp_dest* dest)
{
    struct usr_avp* destmediaavp = NULL;
    int_str destmediaval;

    LM_DBG("code '%d'\n", code);
    dest->lastcode = code;

    switch (code) {
        case 100:
            if (!dest->time100) {
                dest->time100 = time(NULL);
            } else {
                LM_DBG("100 already recorded\n");
            }
            break;
        case 180:
        case 181:
        case 182:
        case 183:
            if (!dest->time180) {
                dest->time180 = time(NULL);

                if (!dest->endtime) {
                    dest->endtime = time(NULL);
                } else {
                    LM_DBG("180, 181, 182 or 183 end already recorded\n");
                }
            } else {
                LM_DBG("180, 181, 182 or 183 already recorded\n");
            }
            break;
        case 200:
        case 202:
            if (!dest->time200) {
                dest->time200 = time(NULL);

                if ((_osp_destmedia_avpid >= 0) &&
                    ((destmediaavp = search_first_avp(_osp_destmedia_avptype, _osp_destmedia_avpid, &destmediaval, 0)) != NULL) &&
                    (destmediaavp->flags & AVP_VAL_STR) && (destmediaval.s.s && destmediaval.s.len))
                {
                    snprintf(dest->destmedia, sizeof(dest->destmedia), "%.*s", destmediaval.s.len, destmediaval.s.s);
                } else {
                    dest->destmedia[0] = '\0';
                }
            } else {
                LM_DBG("200 or 202 already recorded\n");
            }
            break;
        case 408:
        case 487:
            if (!dest->endtime) {
                dest->endtime = time(NULL);
            } else {
                LM_DBG("408 or 487 end already recorded\n");
            }
            break;
        default:
            /* It may overwrite existing end time, it is the expected behavior */
            if ((code >= 400) && (code <= 699)) {
                dest->endtime = time(NULL);
            }
    }
}

/*
 * Check destination status for reporting usage
 * param code Destination status
 * return 1 should report, 0 should not report
 */
static int ospIsToReportUsage(
    int code)
{
    int istime = 0;

    LM_DBG("code '%d'\n", code);
    if (code >= 200) {
        istime = 1;
    }

    return istime;
}

/*
 * Report call setup usage for both client and server side
 * param clientcode Client status
 * param servercode Server status
 */
void ospRecordEvent(
    int clientcode,
    int servercode)
{
    osp_dest* dest;

    LM_DBG("client status '%d'\n", clientcode);
    if ((clientcode != 0) && (dest = ospGetLastOrigDestination())) {
        ospRecordCode(clientcode, dest);

        if (ospIsToReportUsage(servercode) == 1) {
            ospReportOrigSetupUsage();
        }
    }

    LM_DBG("server status '%d'\n", servercode);
    if ((servercode != 0) && (dest = ospGetTermDestination())) {
        ospRecordCode(servercode, dest);

        if (ospIsToReportUsage(servercode) == 1) {
            ospReportTermSetupUsage();
        }
    }
}

/*
 * Dump destination information
 * param dest Destination
 */
void ospDumpDestination(
    osp_dest* dest)
{
    LM_DBG("dest->host..........'%s'\n", dest->host);
    LM_DBG("dest->used..........'%d'\n", dest->used);
    LM_DBG("dest->lastcode......'%d'\n", dest->lastcode);
    LM_DBG("dest->time100.......'%d'\n", (unsigned int)dest->time100);
    LM_DBG("dest->time180.......'%d'\n", (unsigned int)dest->time180);
    LM_DBG("dest->time200.......'%d'\n", (unsigned int)dest->time200);
}

/*
 * Dump all destination information
 */
void ospDumpAllDestination(void)
{
    struct usr_avp* destavp = NULL;
    int_str destval;
    osp_dest* dest = NULL;
    int count = 0;

    for (destavp = search_first_avp(AVP_VAL_STR, _osp_origdest_avpid, NULL, 0);
        destavp != NULL;
        destavp = search_next_avp(destavp, NULL))
    {
        get_avp_val(destavp, &destval);

        /* OSP destination is wrapped in a string */
        dest = (osp_dest*)destval.s.s;

        LM_DBG("....originate '%d'....\n", count++);

        ospDumpDestination(dest);
    }
    if (count == 0) {
        LM_DBG("there is not originate destination AVP\n");
    }

    if (search_first_avp(AVP_VAL_STR, _osp_termdest_avpid, &destval, 0) != NULL) {
        /* OSP destination is wrapped in a string */
        dest = (osp_dest*)destval.s.s;

        LM_DBG("....terminate....\n");

        ospDumpDestination(dest);
    } else {
        LM_DBG("there is not terminate destination AVP\n");
    }
}

/*
 * Convert "address:port" to "[x.x.x.x]:port" or "hostname:port" format
 * param src Source address string
 * param dest Destination address string
 * param bufsize Size of dest buffer
 */
void ospConvertToOutAddress(
    const char* src,
    char* dest,
    int bufsize)
{
    struct in_addr inp;
    char buffer[OSP_STRBUF_SIZE];
    char* port;
    int size;

    if ((src != NULL) && (*src != '\0')) {
        size = sizeof(buffer);
        strncpy(buffer, src, size);
        buffer[size - 1] = '\0';

        if((port = strchr(buffer, ':')) != NULL) {
            *port = '\0';
            port++;
        }

        if (inet_pton(AF_INET, buffer, &inp) == 1) {
            if (port != NULL) {
                snprintf(dest, bufsize, "[%s]:%s", buffer, port);
            } else {
                snprintf(dest, bufsize, "[%s]", buffer);
            }
        } else {
            strncpy(dest, src, bufsize);
            dest[bufsize - 1] = '\0';
        }
    } else {
        *dest = '\0';
    }
}

/*
 * Convert "[x.x.x.x]:port" or "hostname:prot" to "address:port" format
 * param src Source address string
 * param dest Destination address string
 * param bufsize Size of dest buffer
 */
void ospConvertToInAddress(
    const char* src,
    char* dest,
    int bufsize)
{
    char buffer[OSP_STRBUF_SIZE];
    char* end;
    char* port;
    int size;

    if ((src != NULL) && (*src != '\0')) {
        size = sizeof(buffer);
        strncpy(buffer, src, size);
        buffer[size - 1] = '\0';

        if (buffer[0] == '[') {
            if((port = strchr(buffer + 1, ':')) != NULL) {
                *port = '\0';
                port++;
            }

            if ((end = strchr(buffer + 1, ']')) != NULL) {
                *end = '\0';
            }

            if (port != NULL) {
                snprintf(dest, bufsize, "%s:%s", buffer + 1, port);
            } else {
                strncpy(dest, buffer + 1, bufsize);
                dest[bufsize - 1] = '\0';
            }
        } else {
            strncpy(dest, src, bufsize);
            dest[bufsize - 1] = '\0';
        }
    } else {
        *dest = '\0';
    }
}