/
dr_cb.c
180 lines (149 loc) · 4.23 KB
/
dr_cb.c
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
/**
*
* drouting module callbacks
*
* Copyright (C) 2014-2020 OpenSIPS Solutions
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdlib.h>
#include "../../dprint.h"
#include "../../mem/mem.h"
#include "dr_cb.h"
struct dr_head_cbl {
struct dr_callback *first;
int types;
};
#define POINTER_CLOSED_MARKER ((void *)(-1))
unsigned char sort_algs[N_MAX_SORT_CBS] = {'N', 'W', 'Q'};
/* the array with all the cb lists (per type) */
static struct dr_callback *dr_cbs[DRCB_MAX];
/* an array of sorting cbs registered by different modules */
static struct dr_callback *dr_sort_cbs[N_MAX_SORT_CBS];
static void destroy_dr_callbacks_list(struct dr_callback *cb)
{
struct dr_callback *cb_t;
while(cb) {
cb_t = cb;
cb = cb->next;
if(cb_t->callback_param_free && cb_t->param) {
cb_t->callback_param_free(cb_t->param);
cb_t->param = NULL;
}
pkg_free(cb_t);
}
}
void destroy_dr_cbs(void)
{
int i;
struct dr_callback *dr_sort_cb_it;
for( i=0 ; i<DRCB_MAX ; i++ ) {
if (dr_cbs[i] && dr_cbs[i]!=POINTER_CLOSED_MARKER)
destroy_dr_callbacks_list(dr_cbs[i]);
dr_cbs[i] = POINTER_CLOSED_MARKER;
}
for(i=0; i <N_MAX_SORT_CBS; i++) {
if ( (dr_sort_cb_it=dr_sort_cbs[i])!=NULL &&
dr_sort_cb_it->callback_param_free && dr_sort_cb_it->param) {
dr_sort_cb_it->callback_param_free(dr_sort_cb_it->param);
dr_sort_cb_it->param = NULL;
}
}
}
/*
* adds a given callback to a given callback list
*/
int insert_drcb(struct dr_head_cbl **dr_cb_list, struct dr_callback *cb,
int types)
{
int i;
struct dr_callback *dr_sort_cb_it;
for( i=0 ; i<DRCB_MAX ; i++ ) {
if (dr_cbs[i] && dr_cbs[i]!=POINTER_CLOSED_MARKER)
destroy_dr_callbacks_list(dr_cbs[i]);
dr_cbs[i] = POINTER_CLOSED_MARKER;
}
for(i=0; i <N_MAX_SORT_CBS; i++) {
if ( (dr_sort_cb_it=dr_sort_cbs[i])!=NULL &&
dr_sort_cb_it->callback_param_free && dr_sort_cb_it->param) {
dr_sort_cb_it->callback_param_free(dr_sort_cb_it->param);
dr_sort_cb_it->param = NULL;
}
}
cb->next = (*dr_cb_list)->first;
(*dr_cb_list)->first = cb;
(*dr_cb_list)->types |= types;
return 0;
}
/* TODO: param will be the index in the array */
int register_dr_cb(enum drcb_types type, dr_cb f, void *param,
dr_param_free_cb ff)
{
sort_cb_type alg = (sort_cb_type)param;
struct dr_callback *cb;
cb = pkg_malloc(sizeof *cb);
if (!cb) {
LM_ERR("oom\n");
return -1;
}
memset(cb, 0, sizeof *cb);
cb->callback = f;
cb->callback_param_free = ff;
if (type != DRCB_SORT_DST) {
cb->param = param; /* because now param holds the type of the
* sorting function */
/* insert callback to the right list (based on type) */
if (dr_cbs[type] == POINTER_CLOSED_MARKER) {
LM_CRIT("DRCB_SORT_DST registered after shut down!\n");
goto error;
}
cb->next = dr_cbs[type];
dr_cbs[type] = cb;
} else {
if (alg >= N_MAX_SORT_CBS) {
LM_ERR("invalid sorting algorithm: %u\n", alg);
goto error;
}
if (dr_sort_cbs[alg])
LM_WARN("sort callback for alg %u will be overwritten\n", alg);
dr_sort_cbs[alg] = cb;
}
return 0;
error:
pkg_free(cb);
return -1;
}
/* runs a callback from an array - sort_cb_type will represent
* the index within the array */
int run_dr_sort_cbs(sort_cb_type type, struct dr_sort_params *param)
{
if (!dr_sort_cbs[type]) {
LM_WARN("callback type '%d' not registered\n", type);
return -1;
}
dr_sort_cbs[type]->callback(param);
return 0;
}
int run_dr_cbs(enum drcb_types type, void *param)
{
struct dr_callback *it = dr_cbs[type];
if (!it)
return -1;
for (; it; it = it->next)
it->callback(param);
return 0;
}