Coverage Report

Created: 2025-10-10 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/rtpproxy/src/rtpp_modman.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2020 Sippy Software, Inc., http://www.sippysoft.com
3
 *
4
 * Redistribution and use in source and binary forms, with or without
5
 * modification, are permitted provided that the following conditions
6
 * are met:
7
 * 1. Redistributions of source code must retain the above copyright
8
 *    notice, this list of conditions and the following disclaimer.
9
 * 2. Redistributions in binary form must reproduce the above copyright
10
 *    notice, this list of conditions and the following disclaimer in the
11
 *    documentation and/or other materials provided with the distribution.
12
 *
13
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23
 * SUCH DAMAGE.
24
 */
25
26
#include <sys/socket.h>
27
#include <stddef.h>
28
#include <stdint.h>
29
#include <stdlib.h>
30
31
#include "rtpp_types.h"
32
#include "rtpp_list.h"
33
#include "rtpp_mallocs.h"
34
#include "rtpp_codeptr.h"
35
#include "rtpp_refcnt.h"
36
#include "rtpp_modman.h"
37
#include "rtpp_module.h"
38
#include "rtpp_module_if.h"
39
#include "rtpp_modman_fin.h"
40
#include "rtpp_command.h"
41
#include "rtpp_command_args.h"
42
#include "rtpp_command_sub.h"
43
#include "rtpp_command_private.h"
44
45
struct rtpp_modman_priv {
46
    struct rtpp_modman pub;
47
    struct rtpp_list all;
48
};
49
50
static void rtpp_modman_dtor(struct rtpp_modman_priv *);
51
static void rtpp_modman_insert(struct rtpp_modman *, struct rtpp_module_if *);
52
static int rtpp_modman_startall(struct rtpp_modman *, const struct rtpp_cfg *,
53
  const char **);
54
static unsigned int rtpp_modman_get_next_id(struct rtpp_modman *, unsigned int);
55
static void rtpp_modman_do_acct(struct rtpp_modman *, struct rtpp_acct *);
56
static int rtpp_modman_get_ul_subc_h(struct rtpp_modman *, unsigned int,
57
  unsigned int, struct after_success_h *);
58
59
struct rtpp_modman *
60
rtpp_modman_ctor(void)
61
0
{
62
0
    struct rtpp_modman_priv *pvt;
63
64
0
    pvt = rtpp_rzmalloc(sizeof(struct rtpp_modman_priv), PVT_RCOFFS(pvt));
65
0
    if (pvt == NULL) {
66
0
        goto e0;
67
0
    }
68
0
    pvt->pub.insert = rtpp_modman_insert;
69
0
    pvt->pub.startall = rtpp_modman_startall;
70
0
    pvt->pub.get_next_id = rtpp_modman_get_next_id;
71
0
    pvt->pub.do_acct = rtpp_modman_do_acct;
72
0
    pvt->pub.get_ul_subc_h = rtpp_modman_get_ul_subc_h;
73
0
    CALL_SMETHOD(pvt->pub.rcnt, attach, (rtpp_refcnt_dtor_t)&rtpp_modman_dtor,
74
0
      pvt);
75
0
    return ((&pvt->pub));
76
77
0
e0:
78
0
    return (NULL);
79
0
}
80
81
static void
82
rtpp_modman_dtor(struct rtpp_modman_priv *pvt)
83
0
{
84
0
    struct rtpp_module_if *mif, *tmp;
85
86
0
    rtpp_modman_fin(&(pvt->pub));
87
0
    for (mif = RTPP_LIST_HEAD(&pvt->all); mif != NULL; mif = tmp) {
88
0
        tmp = RTPP_ITER_NEXT(mif);
89
0
        CALL_METHOD(mif, kaput);
90
0
        RTPP_OBJ_DECREF(mif);
91
0
    }
92
0
}
93
94
static void
95
rtpp_modman_insert(struct rtpp_modman *self, struct rtpp_module_if *mif)
96
0
{
97
0
    struct rtpp_modman_priv *pvt;
98
99
0
    PUB2PVT(self, pvt);
100
0
    mif->ids->module_idx = self->count.total;
101
0
    rtpp_list_append(&pvt->all, mif);
102
0
    self->count.total++;
103
0
    if (mif->has.do_acct)
104
0
        self->count.sess_acct++;
105
0
}
106
107
static int
108
rtpp_modman_startall(struct rtpp_modman *self, const struct rtpp_cfg *cfp,
109
  const char **failedmod)
110
0
{
111
0
    struct rtpp_module_if *mif;
112
0
    struct rtpp_modman_priv *pvt;
113
114
0
    PUB2PVT(self, pvt);
115
0
    for (mif = RTPP_LIST_HEAD(&pvt->all); mif != NULL; mif = RTPP_ITER_NEXT(mif)) {
116
0
        if (CALL_METHOD(mif, construct, cfp) != 0) {
117
0
            goto failed;
118
0
        }
119
0
        if (CALL_METHOD(mif, start, cfp) != 0) {
120
0
            goto failed;
121
0
        }
122
0
    }
123
0
    return (0);
124
0
failed:
125
0
    *failedmod = mif->descr->name;
126
0
    return (-1);
127
0
}
128
129
static unsigned int
130
rtpp_modman_get_next_id(struct rtpp_modman *self, unsigned int module_id)
131
0
{
132
0
    unsigned int ri;
133
0
    struct rtpp_modman_priv *pvt;
134
135
0
    ri = 1;
136
0
    PUB2PVT(self, pvt);
137
0
    for (struct rtpp_module_if *tmp = RTPP_LIST_HEAD(&pvt->all);
138
0
      tmp != NULL; tmp = RTPP_ITER_NEXT(tmp)) {
139
0
        if (tmp->descr->module_id != module_id)
140
0
            continue;
141
0
        ri += 1;
142
0
    }
143
0
    return (ri);
144
0
}
145
146
static void
147
rtpp_modman_do_acct(struct rtpp_modman *self, struct rtpp_acct *ap)
148
0
{
149
0
    struct rtpp_modman_priv *pvt;
150
0
    PUB2PVT(self, pvt);
151
0
    for (struct rtpp_module_if *tmp = RTPP_LIST_HEAD(&pvt->all);
152
0
      tmp != NULL; tmp = RTPP_ITER_NEXT(tmp)) {
153
0
        if (tmp->has.do_acct == 0)
154
0
            continue;
155
0
        CALL_METHOD(tmp, do_acct, ap);
156
0
    }
157
0
}
158
159
static int
160
rtpp_modman_get_ul_subc_h(struct rtpp_modman *self, unsigned int mod_id,
161
  unsigned int inst_id, struct after_success_h *ashp)
162
0
{
163
0
    struct rtpp_modman_priv *pvt;
164
0
    PUB2PVT(self, pvt);
165
0
    for (struct rtpp_module_if *tmp = RTPP_LIST_HEAD(&pvt->all);
166
0
      tmp != NULL; tmp = RTPP_ITER_NEXT(tmp)) {
167
0
        if (tmp->descr->module_id != mod_id || tmp->ids->instance_id != inst_id)
168
0
            continue;
169
0
        if (tmp->has.ul_subc_h == 0)
170
0
            break;
171
0
        ashp->handler = (after_success_t)tmp->ul_subc_handle;
172
0
        ashp->args.stat = tmp;
173
0
        return (0);
174
0
    }
175
0
    return (-1);
176
0
}