res_pjsip: Allow configuration of endpoint identifier query order
[asterisk/asterisk.git] / res / res_pjsip_keepalive.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2014, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@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 /*** MODULEINFO
20         <depend>pjproject</depend>
21         <depend>res_pjsip</depend>
22         <support_level>core</support_level>
23  ***/
24
25 #include "asterisk.h"
26
27 #include <pjsip.h>
28 #include <pjsip_ua.h>
29
30 #include "asterisk/res_pjsip.h"
31 #include "asterisk/module.h"
32 #include "asterisk/astobj2.h"
33
34 /*! \brief Number of buckets for keepalive transports */
35 #define KEEPALIVE_TRANSPORTS_BUCKETS 53
36
37 /*! \brief The keep alive packet to send */
38 static const pj_str_t keepalive_packet = { "\r\n\r\n", 4 };
39
40 /*! \brief Global container of active transports */
41 static struct ao2_container *transports;
42
43 /*! \brief Thread keeping things alive */
44 static pthread_t keepalive_thread = AST_PTHREADT_NULL;
45
46 /*! \brief The global interval at which to send keepalives */
47 static unsigned int keepalive_interval;
48
49 /*! \brief Existing transport manager callback that we need to invoke */
50 static pjsip_tp_state_callback tpmgr_state_callback;
51
52 /*! \brief Structure for transport to be kept alive */
53 struct keepalive_transport {
54         /*! \brief The underlying PJSIP transport */
55         pjsip_transport *transport;
56 };
57
58 /*! \brief Callback function to send keepalive */
59 static int keepalive_transport_cb(void *obj, void *arg, int flags)
60 {
61         struct keepalive_transport *keepalive = obj;
62         pjsip_tpselector selector = {
63                 .type = PJSIP_TPSELECTOR_TRANSPORT,
64                 .u.transport = keepalive->transport,
65         };
66
67         pjsip_tpmgr_send_raw(pjsip_endpt_get_tpmgr(ast_sip_get_pjsip_endpoint()),
68                 keepalive->transport->key.type, &selector, NULL, keepalive_packet.ptr, keepalive_packet.slen,
69                 &keepalive->transport->key.rem_addr, pj_sockaddr_get_len(&keepalive->transport->key.rem_addr),
70                 NULL, NULL);
71
72         return 0;
73 }
74
75 /*! \brief Thread which sends keepalives to all active connection-oriented transports */
76 static void *keepalive_transport_thread(void *data)
77 {
78         pj_thread_desc desc;
79         pj_thread_t *thread;
80
81         if (pj_thread_register("Asterisk Keepalive Thread", desc, &thread) != PJ_SUCCESS) {
82                 ast_log(LOG_ERROR, "Could not register keepalive thread with PJLIB, keepalives will not occur.\n");
83                 return NULL;
84         }
85
86         /* Once loaded this module just keeps on going as it is unsafe to stop and change the underlying
87          * callback for the transport manager.
88          */
89         while (1) {
90                 sleep(keepalive_interval);
91                 ao2_callback(transports, OBJ_NODATA, keepalive_transport_cb, NULL);
92         }
93
94         return NULL;
95 }
96
97 /*! \brief Destructor for keepalive transport */
98 static void keepalive_transport_destroy(void *obj)
99 {
100         struct keepalive_transport *keepalive = obj;
101
102         pjsip_transport_dec_ref(keepalive->transport);
103 }
104
105 /*! \brief Callback invoked when transport changes occur */
106 static void keepalive_transport_state_callback(pjsip_transport *transport, pjsip_transport_state state,
107         const pjsip_transport_state_info *info)
108 {
109         /* We only care about connection-oriented transports */
110         if (transport->flag & PJSIP_TRANSPORT_RELIABLE) {
111                 struct keepalive_transport *keepalive;
112
113                 switch (state) {
114                 case PJSIP_TP_STATE_CONNECTED:
115                         keepalive = ao2_alloc(sizeof(*keepalive), keepalive_transport_destroy);
116                         if (keepalive) {
117                                 keepalive->transport = transport;
118                                 pjsip_transport_add_ref(keepalive->transport);
119                                 ao2_link(transports, keepalive);
120                                 ao2_ref(keepalive, -1);
121                         }
122                         break;
123                 case PJSIP_TP_STATE_DISCONNECTED:
124                         ao2_find(transports, transport->obj_name, OBJ_SEARCH_KEY | OBJ_NODATA | OBJ_UNLINK);
125                         break;
126                 default:
127                         break;
128                 }
129         }
130
131         /* Forward to the old state callback if present */
132         if (tpmgr_state_callback) {
133                 tpmgr_state_callback(transport, state, info);
134         }
135 }
136
137 /*! \brief Hashing function for keepalive transport */
138 static int keepalive_transport_hash_fn(const void *obj, int flags)
139 {
140         const struct keepalive_transport *object;
141         const char *key;
142
143         switch (flags & OBJ_SEARCH_MASK) {
144         case OBJ_SEARCH_KEY:
145                 key = obj;
146                 break;
147         case OBJ_SEARCH_OBJECT:
148                 object = obj;
149                 key = object->transport->obj_name;
150                 break;
151         default:
152                 /* Hash can only work on something with a full key. */
153                 ast_assert(0);
154                 return 0;
155         }
156         return ast_str_hash(key);
157 }
158
159 /*! \brief Comparison function for keepalive transport */
160 static int keepalive_transport_cmp_fn(void *obj, void *arg, int flags)
161 {
162         const struct keepalive_transport *object_left = obj;
163         const struct keepalive_transport *object_right = arg;
164         const char *right_key = arg;
165         int cmp;
166
167         switch (flags & OBJ_SEARCH_MASK) {
168         case OBJ_SEARCH_OBJECT:
169                 right_key = object_right->transport->obj_name;
170                 /* Fall through */
171         case OBJ_SEARCH_KEY:
172                 cmp = strcmp(object_left->transport->obj_name, right_key);
173                 break;
174         case OBJ_SEARCH_PARTIAL_KEY:
175                 /*
176                  * We could also use a partial key struct containing a length
177                  * so strlen() does not get called for every comparison instead.
178                  */
179                 cmp = strncmp(object_left->transport->obj_name, right_key, strlen(right_key));
180                 break;
181         default:
182                 /*
183                  * What arg points to is specific to this traversal callback
184                  * and has no special meaning to astobj2.
185                  */
186                 cmp = 0;
187                 break;
188         }
189
190         return !cmp ? CMP_MATCH | CMP_STOP : 0;
191 }
192
193 static void keepalive_global_loaded(const char *object_type)
194 {
195         unsigned int new_interval = ast_sip_get_keep_alive_interval();
196         pjsip_tpmgr *tpmgr;
197
198         if (new_interval) {
199                 keepalive_interval = new_interval;
200         } else if (keepalive_interval) {
201                 ast_log(LOG_NOTICE, "Keepalive support can not be disabled once activated.\n");
202                 return;
203         } else {
204                 /* This will occur if no keepalive interval has been specified at initial start */
205                 return;
206         }
207
208         if (keepalive_thread != AST_PTHREADT_NULL) {
209                 return;
210         }
211
212         transports = ao2_container_alloc(KEEPALIVE_TRANSPORTS_BUCKETS, keepalive_transport_hash_fn,
213                 keepalive_transport_cmp_fn);
214         if (!transports) {
215                 ast_log(LOG_ERROR, "Could not create container for transports to perform keepalive on.\n");
216                 return;
217         }
218
219         tpmgr = pjsip_endpt_get_tpmgr(ast_sip_get_pjsip_endpoint());
220         if (!tpmgr) {
221                 ast_log(LOG_ERROR, "No transport manager to attach keepalive functionality to.\n");
222                 ao2_ref(transports, -1);
223                 return;
224         }
225
226         if (ast_pthread_create(&keepalive_thread, NULL, keepalive_transport_thread, NULL)) {
227                 ast_log(LOG_ERROR, "Could not create thread for sending keepalive messages.\n");
228                 ao2_ref(transports, -1);
229                 return;
230         }
231
232         tpmgr_state_callback = pjsip_tpmgr_get_state_cb(tpmgr);
233         pjsip_tpmgr_set_state_cb(tpmgr, &keepalive_transport_state_callback);
234 }
235
236 /*! \brief Observer which is used to update our interval when the global setting changes */
237 static struct ast_sorcery_observer keepalive_global_observer = {
238         .loaded = keepalive_global_loaded,
239 };
240
241 static int load_module(void)
242 {
243         CHECK_PJSIP_MODULE_LOADED();
244
245         ast_sorcery_observer_add(ast_sip_get_sorcery(), "global", &keepalive_global_observer);
246         ast_sorcery_reload_object(ast_sip_get_sorcery(), "global");
247         ast_module_ref(ast_module_info->self);
248         return AST_MODULE_LOAD_SUCCESS;
249 }
250
251 static int unload_module(void)
252 {
253         /* This will never get called */
254         return 0;
255 }
256
257 static int reload_module(void)
258 {
259         ast_sorcery_reload_object(ast_sip_get_sorcery(), "global");
260         return 0;
261 }
262
263 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Stateful Connection Keepalive Support",
264                 .support_level = AST_MODULE_SUPPORT_CORE,
265                 .load = load_module,
266                 .reload = reload_module,
267                 .unload = unload_module,
268                 .load_pri = AST_MODPRI_CHANNEL_DEPEND - 4,
269                    );