AST-2018-005: res_pjsip_transport_management: Move to core
[asterisk/asterisk.git] / res / res_pjsip / pjsip_transport_events.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2017, Digium Inc.
5  *
6  * Richard Mudgett <rmudgett@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*!
20  * \file
21  * \brief Manages the global transport event notification callbacks.
22  *
23  * \author Richard Mudgett <rmudgett@digium.com>
24  *      See Also:
25  *
26  * \arg \ref AstCREDITS
27  */
28
29
30 #include "asterisk.h"
31
32 #include "asterisk/res_pjsip.h"
33 #include "include/res_pjsip_private.h"
34 #include "asterisk/linkedlists.h"
35 #include "asterisk/vector.h"
36
37 /* ------------------------------------------------------------------- */
38
39 /*! \brief Number of buckets for monitored active transports */
40 #define ACTIVE_TRANSPORTS_BUCKETS 127
41
42 /*! Who to notify when transport shuts down. */
43 struct transport_monitor_notifier {
44         /*! Who to call when transport shuts down. */
45         ast_transport_monitor_shutdown_cb cb;
46         /*! ao2 data object to pass to callback. */
47         void *data;
48 };
49
50 /*! \brief Structure for transport to be monitored */
51 struct transport_monitor {
52         /*! \brief The underlying PJSIP transport */
53         pjsip_transport *transport;
54         /*! Who is interested in when this transport shuts down. */
55         AST_VECTOR(, struct transport_monitor_notifier) monitors;
56 };
57
58 /*! \brief Global container of active reliable transports */
59 static AO2_GLOBAL_OBJ_STATIC(active_transports);
60
61 /*! \brief Existing transport events callback that we need to invoke */
62 static pjsip_tp_state_callback tpmgr_state_callback;
63
64 /*! List of registered transport state callbacks. */
65 static AST_RWLIST_HEAD(, ast_sip_tpmgr_state_callback) transport_state_list;
66
67
68 /*! \brief Hashing function for struct transport_monitor */
69 AO2_STRING_FIELD_HASH_FN(transport_monitor, transport->obj_name);
70
71 /*! \brief Comparison function for struct transport_monitor */
72 AO2_STRING_FIELD_CMP_FN(transport_monitor, transport->obj_name);
73
74 static const char *transport_state2str(pjsip_transport_state state)
75 {
76         const char *name;
77
78         switch (state) {
79         case PJSIP_TP_STATE_CONNECTED:
80                 name = "CONNECTED";
81                 break;
82         case PJSIP_TP_STATE_DISCONNECTED:
83                 name = "DISCONNECTED";
84                 break;
85         case PJSIP_TP_STATE_SHUTDOWN:
86                 name = "SHUTDOWN";
87                 break;
88         case PJSIP_TP_STATE_DESTROY:
89                 name = "DESTROY";
90                 break;
91         default:
92                 /*
93                  * We have to have a default case because the enum is
94                  * defined by a third-party library.
95                  */
96                 ast_assert(0);
97                 name = "<unknown>";
98                 break;
99         }
100         return name;
101 }
102
103 static void transport_monitor_dtor(void *vdoomed)
104 {
105         struct transport_monitor *monitored = vdoomed;
106         int idx;
107
108         for (idx = AST_VECTOR_SIZE(&monitored->monitors); idx--;) {
109                 struct transport_monitor_notifier *notifier;
110
111                 notifier = AST_VECTOR_GET_ADDR(&monitored->monitors, idx);
112                 ao2_cleanup(notifier->data);
113         }
114         AST_VECTOR_FREE(&monitored->monitors);
115 }
116
117 /*! \brief Callback invoked when transport state changes occur */
118 static void transport_state_callback(pjsip_transport *transport,
119         pjsip_transport_state state, const pjsip_transport_state_info *info)
120 {
121         struct ao2_container *transports;
122
123         /* We only care about monitoring reliable transports */
124         if (PJSIP_TRANSPORT_IS_RELIABLE(transport)
125                 && (transports = ao2_global_obj_ref(active_transports))) {
126                 struct transport_monitor *monitored;
127
128                 ast_debug(3, "Reliable transport '%s' state:%s\n",
129                         transport->obj_name, transport_state2str(state));
130                 switch (state) {
131                 case PJSIP_TP_STATE_CONNECTED:
132                         monitored = ao2_alloc_options(sizeof(*monitored),
133                                 transport_monitor_dtor, AO2_ALLOC_OPT_LOCK_NOLOCK);
134                         if (!monitored) {
135                                 break;
136                         }
137                         monitored->transport = transport;
138                         if (AST_VECTOR_INIT(&monitored->monitors, 5)) {
139                                 ao2_ref(monitored, -1);
140                                 break;
141                         }
142
143                         ao2_link(transports, monitored);
144                         ao2_ref(monitored, -1);
145                         break;
146                 case PJSIP_TP_STATE_DISCONNECTED:
147                         if (!transport->is_shutdown) {
148                                 pjsip_transport_shutdown(transport);
149                         }
150                         break;
151                 case PJSIP_TP_STATE_SHUTDOWN:
152                         /*
153                          * Set shutdown flag early so we can force a new transport to be
154                          * created if a monitor callback needs to reestablish a link.
155                          * PJPROJECT sets the flag after this routine returns even though
156                          * it has already called the transport's shutdown routine.
157                          */
158                         transport->is_shutdown = PJ_TRUE;
159
160                         monitored = ao2_find(transports, transport->obj_name,
161                                 OBJ_SEARCH_KEY | OBJ_UNLINK);
162                         if (monitored) {
163                                 int idx;
164
165                                 for (idx = AST_VECTOR_SIZE(&monitored->monitors); idx--;) {
166                                         struct transport_monitor_notifier *notifier;
167
168                                         notifier = AST_VECTOR_GET_ADDR(&monitored->monitors, idx);
169                                         ast_debug(3, "running callback %p(%p) for transport %s\n",
170                                                 notifier->cb, notifier->data, transport->obj_name);
171                                         notifier->cb(notifier->data);
172                                 }
173                                 ao2_ref(monitored, -1);
174                         }
175                         break;
176                 default:
177                         break;
178                 }
179
180                 ao2_ref(transports, -1);
181         }
182
183         /* Loop over other transport state callbacks registered with us. */
184         if (!AST_LIST_EMPTY(&transport_state_list)) {
185                 struct ast_sip_tpmgr_state_callback *tpmgr_notifier;
186
187                 AST_RWLIST_RDLOCK(&transport_state_list);
188                 AST_LIST_TRAVERSE(&transport_state_list, tpmgr_notifier, node) {
189                         tpmgr_notifier->cb(transport, state, info);
190                 }
191                 AST_RWLIST_UNLOCK(&transport_state_list);
192         }
193
194         /* Forward to the old state callback if present */
195         if (tpmgr_state_callback) {
196                 tpmgr_state_callback(transport, state, info);
197         }
198 }
199
200 struct callback_data {
201         ast_transport_monitor_shutdown_cb cb;
202         void *data;
203         ast_transport_monitor_data_matcher matches;
204 };
205
206 static int transport_monitor_unregister_cb(void *obj, void *arg, int flags)
207 {
208         struct transport_monitor *monitored = obj;
209         struct callback_data *cb_data = arg;
210         int idx;
211
212         for (idx = AST_VECTOR_SIZE(&monitored->monitors); idx--;) {
213                 struct transport_monitor_notifier *notifier;
214
215                 notifier = AST_VECTOR_GET_ADDR(&monitored->monitors, idx);
216                 if (notifier->cb == cb_data->cb && (!cb_data->data
217                         || cb_data->matches(cb_data->data, notifier->data))) {
218                         ao2_cleanup(notifier->data);
219                         AST_VECTOR_REMOVE_UNORDERED(&monitored->monitors, idx);
220                         ast_debug(3, "Unregistered monitor %p(%p) from transport %s\n",
221                                 notifier->cb, notifier->data, monitored->transport->obj_name);
222                 }
223         }
224         return 0;
225 }
226
227 static int ptr_matcher(void *a, void *b)
228 {
229         return a == b;
230 }
231
232 void ast_sip_transport_monitor_unregister_all(ast_transport_monitor_shutdown_cb cb,
233         void *data, ast_transport_monitor_data_matcher matches)
234 {
235         struct ao2_container *transports;
236         struct callback_data cb_data = {
237                 .cb = cb,
238                 .data = data,
239                 .matches = matches ?: ptr_matcher,
240         };
241
242         ast_assert(cb != NULL);
243
244         transports = ao2_global_obj_ref(active_transports);
245         if (!transports) {
246                 return;
247         }
248         ao2_callback(transports, OBJ_MULTIPLE | OBJ_NODATA, transport_monitor_unregister_cb, &cb_data);
249         ao2_ref(transports, -1);
250 }
251
252 void ast_sip_transport_monitor_unregister(pjsip_transport *transport,
253         ast_transport_monitor_shutdown_cb cb, void *data, ast_transport_monitor_data_matcher matches)
254 {
255         struct ao2_container *transports;
256         struct transport_monitor *monitored;
257
258         ast_assert(transport != NULL && cb != NULL);
259
260         transports = ao2_global_obj_ref(active_transports);
261         if (!transports) {
262                 return;
263         }
264
265         ao2_lock(transports);
266         monitored = ao2_find(transports, transport->obj_name, OBJ_SEARCH_KEY | OBJ_NOLOCK);
267         if (monitored) {
268                 struct callback_data cb_data = {
269                         .cb = cb,
270                         .data = data,
271                         .matches = matches ?: ptr_matcher,
272                 };
273
274                 transport_monitor_unregister_cb(monitored, &cb_data, 0);
275                 ao2_ref(monitored, -1);
276         }
277         ao2_unlock(transports);
278         ao2_ref(transports, -1);
279 }
280
281 enum ast_transport_monitor_reg ast_sip_transport_monitor_register(pjsip_transport *transport,
282         ast_transport_monitor_shutdown_cb cb, void *ao2_data)
283 {
284         struct ao2_container *transports;
285         struct transport_monitor *monitored;
286         enum ast_transport_monitor_reg res = AST_TRANSPORT_MONITOR_REG_NOT_FOUND;
287
288         ast_assert(transport != NULL && cb != NULL);
289
290         transports = ao2_global_obj_ref(active_transports);
291         if (!transports) {
292                 return res;
293         }
294
295         ao2_lock(transports);
296         monitored = ao2_find(transports, transport->obj_name, OBJ_SEARCH_KEY | OBJ_NOLOCK);
297         if (monitored) {
298                 struct transport_monitor_notifier new_monitor;
299
300                 /* Add new monitor to vector */
301                 new_monitor.cb = cb;
302                 new_monitor.data = ao2_bump(ao2_data);
303                 if (AST_VECTOR_APPEND(&monitored->monitors, new_monitor)) {
304                         ao2_cleanup(ao2_data);
305                         res = AST_TRANSPORT_MONITOR_REG_FAILED;
306                         ast_debug(3, "Register monitor %p(%p) to transport %s FAILED\n",
307                                 cb, ao2_data, transport->obj_name);
308                 } else {
309                         res = AST_TRANSPORT_MONITOR_REG_SUCCESS;
310                         ast_debug(3, "Registered monitor %p(%p) to transport %s\n",
311                                 cb, ao2_data, transport->obj_name);
312                 }
313
314                 ao2_ref(monitored, -1);
315         }
316         ao2_unlock(transports);
317         ao2_ref(transports, -1);
318         return res;
319 }
320
321 void ast_sip_transport_state_unregister(struct ast_sip_tpmgr_state_callback *element)
322 {
323         AST_RWLIST_WRLOCK(&transport_state_list);
324         AST_LIST_REMOVE(&transport_state_list, element, node);
325         AST_RWLIST_UNLOCK(&transport_state_list);
326 }
327
328 void ast_sip_transport_state_register(struct ast_sip_tpmgr_state_callback *element)
329 {
330         struct ast_sip_tpmgr_state_callback *tpmgr_notifier;
331
332         AST_RWLIST_WRLOCK(&transport_state_list);
333         AST_LIST_TRAVERSE(&transport_state_list, tpmgr_notifier, node) {
334                 if (element == tpmgr_notifier) {
335                         /* Already registered. */
336                         AST_RWLIST_UNLOCK(&transport_state_list);
337                         return;
338                 }
339         }
340         AST_LIST_INSERT_HEAD(&transport_state_list, element, node);
341         AST_RWLIST_UNLOCK(&transport_state_list);
342 }
343
344 void ast_sip_destroy_transport_events(void)
345 {
346         pjsip_tpmgr *tpmgr;
347
348         tpmgr = pjsip_endpt_get_tpmgr(ast_sip_get_pjsip_endpoint());
349         if (tpmgr) {
350                 pjsip_tpmgr_set_state_cb(tpmgr, tpmgr_state_callback);
351         }
352
353         ao2_global_obj_release(active_transports);
354 }
355
356 int ast_sip_initialize_transport_events(void)
357 {
358         pjsip_tpmgr *tpmgr;
359         struct ao2_container *transports;
360
361         tpmgr = pjsip_endpt_get_tpmgr(ast_sip_get_pjsip_endpoint());
362         if (!tpmgr) {
363                 return -1;
364         }
365
366         transports = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
367                 ACTIVE_TRANSPORTS_BUCKETS, transport_monitor_hash_fn, NULL,
368                 transport_monitor_cmp_fn);
369         if (!transports) {
370                 return -1;
371         }
372         ao2_global_obj_replace_unref(active_transports, transports);
373         ao2_ref(transports, -1);
374
375         tpmgr_state_callback = pjsip_tpmgr_get_state_cb(tpmgr);
376         pjsip_tpmgr_set_state_cb(tpmgr, &transport_state_callback);
377
378         return 0;
379 }