Merge "README-SERIOUSLY.bestpractices.md: Speling correetions."
[asterisk/asterisk.git] / res / res_pjsip_session.c
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2013, Digium, Inc.
5 *
6 * Mark Michelson <mmichelson@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 #include <pjlib.h>
30
31 #include "asterisk/res_pjsip.h"
32 #include "asterisk/res_pjsip_session.h"
33 #include "asterisk/callerid.h"
34 #include "asterisk/datastore.h"
35 #include "asterisk/module.h"
36 #include "asterisk/logger.h"
37 #include "asterisk/res_pjsip.h"
38 #include "asterisk/astobj2.h"
39 #include "asterisk/lock.h"
40 #include "asterisk/uuid.h"
41 #include "asterisk/pbx.h"
42 #include "asterisk/taskprocessor.h"
43 #include "asterisk/causes.h"
44 #include "asterisk/sdp_srtp.h"
45 #include "asterisk/dsp.h"
46 #include "asterisk/acl.h"
47 #include "asterisk/features_config.h"
48 #include "asterisk/pickup.h"
49 #include "asterisk/test.h"
50 #include "asterisk/stream.h"
51 #include "asterisk/vector.h"
52
53 #define SDP_HANDLER_BUCKETS 11
54
55 #define MOD_DATA_ON_RESPONSE "on_response"
56 #define MOD_DATA_NAT_HOOK "nat_hook"
57
58 /* Most common case is one audio and one video stream */
59 #define DEFAULT_NUM_SESSION_MEDIA 2
60
61 /* Some forward declarations */
62 static void handle_session_begin(struct ast_sip_session *session);
63 static void handle_session_end(struct ast_sip_session *session);
64 static void handle_session_destroy(struct ast_sip_session *session);
65 static void handle_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata);
66 static void handle_incoming_response(struct ast_sip_session *session, pjsip_rx_data *rdata,
67                 enum ast_sip_session_response_priority response_priority);
68 static int handle_incoming(struct ast_sip_session *session, pjsip_rx_data *rdata,
69                 enum ast_sip_session_response_priority response_priority);
70 static void handle_outgoing_request(struct ast_sip_session *session, pjsip_tx_data *tdata);
71 static void handle_outgoing_response(struct ast_sip_session *session, pjsip_tx_data *tdata);
72
73 /*! \brief NAT hook for modifying outgoing messages with SDP */
74 static struct ast_sip_nat_hook *nat_hook;
75
76 /*!
77  * \brief Registered SDP stream handlers
78  *
79  * This container is keyed on stream types. Each
80  * object in the container is a linked list of
81  * handlers for the stream type.
82  */
83 static struct ao2_container *sdp_handlers;
84
85 /*!
86  * These are the objects in the sdp_handlers container
87  */
88 struct sdp_handler_list {
89         /* The list of handlers to visit */
90         AST_LIST_HEAD_NOLOCK(, ast_sip_session_sdp_handler) list;
91         /* The handlers in this list handle streams of this type */
92         char stream_type[1];
93 };
94
95 static struct pjmedia_sdp_session *create_local_sdp(pjsip_inv_session *inv, struct ast_sip_session *session, const pjmedia_sdp_session *offer);
96
97 static int sdp_handler_list_hash(const void *obj, int flags)
98 {
99         const struct sdp_handler_list *handler_list = obj;
100         const char *stream_type = flags & OBJ_KEY ? obj : handler_list->stream_type;
101
102         return ast_str_hash(stream_type);
103 }
104
105 static int sdp_handler_list_cmp(void *obj, void *arg, int flags)
106 {
107         struct sdp_handler_list *handler_list1 = obj;
108         struct sdp_handler_list *handler_list2 = arg;
109         const char *stream_type2 = flags & OBJ_KEY ? arg : handler_list2->stream_type;
110
111         return strcmp(handler_list1->stream_type, stream_type2) ? 0 : CMP_MATCH | CMP_STOP;
112 }
113
114 int ast_sip_session_register_sdp_handler(struct ast_sip_session_sdp_handler *handler, const char *stream_type)
115 {
116         RAII_VAR(struct sdp_handler_list *, handler_list,
117                         ao2_find(sdp_handlers, stream_type, OBJ_KEY), ao2_cleanup);
118         SCOPED_AO2LOCK(lock, sdp_handlers);
119
120         if (handler_list) {
121                 struct ast_sip_session_sdp_handler *iter;
122                 /* Check if this handler is already registered for this stream type */
123                 AST_LIST_TRAVERSE(&handler_list->list, iter, next) {
124                         if (!strcmp(iter->id, handler->id)) {
125                                 ast_log(LOG_WARNING, "Handler '%s' already registered for stream type '%s'.\n", handler->id, stream_type);
126                                 return -1;
127                         }
128                 }
129                 AST_LIST_INSERT_TAIL(&handler_list->list, handler, next);
130                 ast_debug(1, "Registered SDP stream handler '%s' for stream type '%s'\n", handler->id, stream_type);
131
132                 return 0;
133         }
134
135         /* No stream of this type has been registered yet, so we need to create a new list */
136         handler_list = ao2_alloc(sizeof(*handler_list) + strlen(stream_type), NULL);
137         if (!handler_list) {
138                 return -1;
139         }
140         /* Safe use of strcpy */
141         strcpy(handler_list->stream_type, stream_type);
142         AST_LIST_HEAD_INIT_NOLOCK(&handler_list->list);
143         AST_LIST_INSERT_TAIL(&handler_list->list, handler, next);
144         if (!ao2_link(sdp_handlers, handler_list)) {
145                 return -1;
146         }
147         ast_debug(1, "Registered SDP stream handler '%s' for stream type '%s'\n", handler->id, stream_type);
148
149         return 0;
150 }
151
152 static int remove_handler(void *obj, void *arg, void *data, int flags)
153 {
154         struct sdp_handler_list *handler_list = obj;
155         struct ast_sip_session_sdp_handler *handler = data;
156         struct ast_sip_session_sdp_handler *iter;
157         const char *stream_type = arg;
158
159         AST_LIST_TRAVERSE_SAFE_BEGIN(&handler_list->list, iter, next) {
160                 if (!strcmp(iter->id, handler->id)) {
161                         AST_LIST_REMOVE_CURRENT(next);
162                         ast_debug(1, "Unregistered SDP stream handler '%s' for stream type '%s'\n", handler->id, stream_type);
163                 }
164         }
165         AST_LIST_TRAVERSE_SAFE_END;
166
167         if (AST_LIST_EMPTY(&handler_list->list)) {
168                 ast_debug(3, "No more handlers exist for stream type '%s'\n", stream_type);
169                 return CMP_MATCH;
170         } else {
171                 return CMP_STOP;
172         }
173 }
174
175 void ast_sip_session_unregister_sdp_handler(struct ast_sip_session_sdp_handler *handler, const char *stream_type)
176 {
177         ao2_callback_data(sdp_handlers, OBJ_KEY | OBJ_UNLINK | OBJ_NODATA, remove_handler, (void *)stream_type, handler);
178 }
179
180 static int media_stats_local_ssrc_cmp(
181                 const struct ast_rtp_instance_stats *vec_elem, const struct ast_rtp_instance_stats *srch)
182 {
183         if (vec_elem->local_ssrc == srch->local_ssrc) {
184                 return 1;
185         }
186
187         return 0;
188 }
189
190 static struct ast_sip_session_media_state *internal_sip_session_media_state_alloc(
191         size_t sessions, size_t read_callbacks)
192 {
193         struct ast_sip_session_media_state *media_state;
194
195         media_state = ast_calloc(1, sizeof(*media_state));
196         if (!media_state) {
197                 return NULL;
198         }
199
200         if (AST_VECTOR_INIT(&media_state->sessions, sessions) < 0) {
201                 ast_free(media_state);
202                 return NULL;
203         }
204
205         if (AST_VECTOR_INIT(&media_state->read_callbacks, read_callbacks) < 0) {
206                 AST_VECTOR_FREE(&media_state->sessions);
207                 ast_free(media_state);
208                 return NULL;
209         }
210
211         return media_state;
212 }
213
214 struct ast_sip_session_media_state *ast_sip_session_media_state_alloc(void)
215 {
216         return internal_sip_session_media_state_alloc(
217                 DEFAULT_NUM_SESSION_MEDIA, DEFAULT_NUM_SESSION_MEDIA);
218 }
219
220 void ast_sip_session_media_stats_save(struct ast_sip_session *sip_session, struct ast_sip_session_media_state *media_state)
221 {
222         int i;
223         int ret;
224
225         if (!media_state || !sip_session) {
226                 return;
227         }
228
229         for (i = 0; i < AST_VECTOR_SIZE(&media_state->sessions); i++) {
230                 struct ast_rtp_instance_stats *stats_tmp = NULL;
231                 struct ast_sip_session_media *media = AST_VECTOR_GET(&media_state->sessions, i);
232                 if (!media || !media->rtp) {
233                         continue;
234                 }
235
236                 stats_tmp = ast_calloc(1, sizeof(struct ast_rtp_instance_stats));
237                 if (!stats_tmp) {
238                         return;
239                 }
240
241                 ret = ast_rtp_instance_get_stats(media->rtp, stats_tmp, AST_RTP_INSTANCE_STAT_ALL);
242                 if (ret) {
243                         ast_free(stats_tmp);
244                         continue;
245                 }
246
247                 /* remove all the duplicated stats if exist */
248                 AST_VECTOR_REMOVE_CMP_UNORDERED(&sip_session->media_stats, stats_tmp, media_stats_local_ssrc_cmp, ast_free);
249
250                 AST_VECTOR_APPEND(&sip_session->media_stats, stats_tmp);
251         }
252 }
253
254 void ast_sip_session_media_state_reset(struct ast_sip_session_media_state *media_state)
255 {
256         int index;
257
258         if (!media_state) {
259                 return;
260         }
261
262         AST_VECTOR_RESET(&media_state->sessions, ao2_cleanup);
263         AST_VECTOR_RESET(&media_state->read_callbacks, AST_VECTOR_ELEM_CLEANUP_NOOP);
264
265         for (index = 0; index < AST_MEDIA_TYPE_END; ++index) {
266                 media_state->default_session[index] = NULL;
267         }
268
269         ast_stream_topology_free(media_state->topology);
270         media_state->topology = NULL;
271 }
272
273 struct ast_sip_session_media_state *ast_sip_session_media_state_clone(const struct ast_sip_session_media_state *media_state)
274 {
275         struct ast_sip_session_media_state *cloned;
276         int index;
277
278         if (!media_state) {
279                 return NULL;
280         }
281
282         cloned = internal_sip_session_media_state_alloc(
283                 AST_VECTOR_SIZE(&media_state->sessions),
284                 AST_VECTOR_SIZE(&media_state->read_callbacks));
285         if (!cloned) {
286                 return NULL;
287         }
288
289         if (media_state->topology) {
290                 cloned->topology = ast_stream_topology_clone(media_state->topology);
291                 if (!cloned->topology) {
292                         ast_sip_session_media_state_free(cloned);
293                         return NULL;
294                 }
295         }
296
297         for (index = 0; index < AST_VECTOR_SIZE(&media_state->sessions); ++index) {
298                 struct ast_sip_session_media *session_media = AST_VECTOR_GET(&media_state->sessions, index);
299                 enum ast_media_type type = ast_stream_get_type(ast_stream_topology_get_stream(cloned->topology, index));
300
301                 ao2_bump(session_media);
302                 if (AST_VECTOR_REPLACE(&cloned->sessions, index, session_media)) {
303                         ao2_cleanup(session_media);
304                 }
305                 if (ast_stream_get_state(ast_stream_topology_get_stream(cloned->topology, index)) != AST_STREAM_STATE_REMOVED &&
306                         !cloned->default_session[type]) {
307                         cloned->default_session[type] = session_media;
308                 }
309         }
310
311         for (index = 0; index < AST_VECTOR_SIZE(&media_state->read_callbacks); ++index) {
312                 struct ast_sip_session_media_read_callback_state *read_callback = AST_VECTOR_GET_ADDR(&media_state->read_callbacks, index);
313
314                 AST_VECTOR_REPLACE(&cloned->read_callbacks, index, *read_callback);
315         }
316
317         return cloned;
318 }
319
320 void ast_sip_session_media_state_free(struct ast_sip_session_media_state *media_state)
321 {
322         if (!media_state) {
323                 return;
324         }
325
326         /* This will reset the internal state so we only have to free persistent things */
327         ast_sip_session_media_state_reset(media_state);
328
329         AST_VECTOR_FREE(&media_state->sessions);
330         AST_VECTOR_FREE(&media_state->read_callbacks);
331
332         ast_free(media_state);
333 }
334
335 int ast_sip_session_is_pending_stream_default(const struct ast_sip_session *session, const struct ast_stream *stream)
336 {
337         int index;
338
339         if (!session->pending_media_state->topology) {
340                 ast_log(LOG_WARNING, "Pending topology was NULL for channel '%s'\n",
341                         session->channel ? ast_channel_name(session->channel) : "unknown");
342                 return 0;
343         }
344
345         if (ast_stream_get_state(stream) == AST_STREAM_STATE_REMOVED) {
346                 return 0;
347         }
348
349         for (index = 0; index < ast_stream_topology_get_count(session->pending_media_state->topology); ++index) {
350                 if (ast_stream_get_type(ast_stream_topology_get_stream(session->pending_media_state->topology, index)) !=
351                         ast_stream_get_type(stream)) {
352                         continue;
353                 }
354
355                 return ast_stream_topology_get_stream(session->pending_media_state->topology, index) == stream ? 1 : 0;
356         }
357
358         return 0;
359 }
360
361 int ast_sip_session_media_add_read_callback(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
362         int fd, ast_sip_session_media_read_cb callback)
363 {
364         struct ast_sip_session_media_read_callback_state callback_state = {
365                 .fd = fd,
366                 .read_callback = callback,
367                 .session = session_media,
368         };
369
370         /* The contents of the vector are whole structs and not pointers */
371         return AST_VECTOR_APPEND(&session->pending_media_state->read_callbacks, callback_state);
372 }
373
374 int ast_sip_session_media_set_write_callback(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
375         ast_sip_session_media_write_cb callback)
376 {
377         if (session_media->write_callback) {
378                 if (session_media->write_callback == callback) {
379                         return 0;
380                 }
381
382                 return -1;
383         }
384
385         session_media->write_callback = callback;
386
387         return 0;
388 }
389
390 struct ast_sip_session_media *ast_sip_session_media_get_transport(struct ast_sip_session *session, struct ast_sip_session_media *session_media)
391 {
392         int index;
393
394         if (!session->endpoint->media.bundle || ast_strlen_zero(session_media->mid)) {
395                 return session_media;
396         }
397
398         for (index = 0; index < AST_VECTOR_SIZE(&session->pending_media_state->sessions); ++index) {
399                 struct ast_sip_session_media *bundle_group_session_media;
400
401                 bundle_group_session_media = AST_VECTOR_GET(&session->pending_media_state->sessions, index);
402
403                 /* The first session which is in the bundle group is considered the authoritative session for transport */
404                 if (bundle_group_session_media->bundle_group == session_media->bundle_group) {
405                         return bundle_group_session_media;
406                 }
407         }
408
409         return session_media;
410 }
411
412 /*!
413  * \brief Set an SDP stream handler for a corresponding session media.
414  *
415  * \note Always use this function to set the SDP handler for a session media.
416  *
417  * This function will properly free resources on the SDP handler currently being
418  * used by the session media, then set the session media to use the new SDP
419  * handler.
420  */
421 static void session_media_set_handler(struct ast_sip_session_media *session_media,
422                 struct ast_sip_session_sdp_handler *handler)
423 {
424         ast_assert(session_media->handler != handler);
425
426         if (session_media->handler) {
427                 session_media->handler->stream_destroy(session_media);
428         }
429         session_media->handler = handler;
430 }
431
432 static int stream_destroy(void *obj, void *arg, int flags)
433 {
434         struct sdp_handler_list *handler_list = obj;
435         struct ast_sip_session_media *session_media = arg;
436         struct ast_sip_session_sdp_handler *handler;
437
438         AST_LIST_TRAVERSE(&handler_list->list, handler, next) {
439                 handler->stream_destroy(session_media);
440         }
441
442         return 0;
443 }
444
445 static void session_media_dtor(void *obj)
446 {
447         struct ast_sip_session_media *session_media = obj;
448
449         /* It is possible for multiple handlers to have allocated memory on the
450          * session media (usually through a stream changing types). Therefore, we
451          * traverse all the SDP handlers and let them all call stream_destroy on
452          * the session_media
453          */
454         ao2_callback(sdp_handlers, 0, stream_destroy, session_media);
455
456         if (session_media->srtp) {
457                 ast_sdp_srtp_destroy(session_media->srtp);
458         }
459
460         ast_free(session_media->mid);
461         ast_free(session_media->remote_mslabel);
462 }
463
464 struct ast_sip_session_media *ast_sip_session_media_state_add(struct ast_sip_session *session,
465         struct ast_sip_session_media_state *media_state, enum ast_media_type type, int position)
466 {
467         struct ast_sip_session_media *session_media = NULL;
468
469         /* It is possible for this media state to already contain a session for the stream. If this
470          * is the case we simply return it.
471          */
472         if (position < AST_VECTOR_SIZE(&media_state->sessions)) {
473                 return AST_VECTOR_GET(&media_state->sessions, position);
474         }
475
476         /* Determine if we can reuse the session media from the active media state if present */
477         if (position < AST_VECTOR_SIZE(&session->active_media_state->sessions)) {
478                 session_media = AST_VECTOR_GET(&session->active_media_state->sessions, position);
479                 /* A stream can never exist without an accompanying media session */
480                 if (session_media->type == type) {
481                         ao2_ref(session_media, +1);
482                 } else {
483                         session_media = NULL;
484                 }
485         }
486
487         if (!session_media) {
488                 /* No existing media session we can use so create a new one */
489                 session_media = ao2_alloc_options(sizeof(*session_media), session_media_dtor, AO2_ALLOC_OPT_LOCK_NOLOCK);
490                 if (!session_media) {
491                         return NULL;
492                 }
493
494                 session_media->encryption = session->endpoint->media.rtp.encryption;
495                 session_media->remote_ice = session->endpoint->media.rtp.ice_support;
496                 session_media->remote_rtcp_mux = session->endpoint->media.rtcp_mux;
497                 session_media->keepalive_sched_id = -1;
498                 session_media->timeout_sched_id = -1;
499                 session_media->type = type;
500                 session_media->stream_num = position;
501
502                 if (session->endpoint->media.bundle) {
503                         /* This is a new stream so create a new mid based on media type and position, which makes it unique.
504                          * If this is the result of an offer the mid will just end up getting replaced.
505                          */
506                         if (ast_asprintf(&session_media->mid, "%s-%d", ast_codec_media_type2str(type), position) < 0) {
507                                 ao2_ref(session_media, -1);
508                                 return NULL;
509                         }
510                         session_media->bundle_group = 0;
511
512                         /* Some WebRTC clients can't handle an offer to bundle media streams. Instead they expect them to
513                          * already be bundled. Every client handles this scenario though so if WebRTC is enabled just go
514                          * ahead and treat the streams as having already been bundled.
515                          */
516                         session_media->bundled = session->endpoint->media.webrtc;
517                 } else {
518                         session_media->bundle_group = -1;
519                 }
520         }
521
522         if (AST_VECTOR_REPLACE(&media_state->sessions, position, session_media)) {
523                 ao2_ref(session_media, -1);
524
525                 return NULL;
526         }
527
528         /* If this stream will be active in some way and it is the first of this type then consider this the default media session to match */
529         if (!media_state->default_session[type] && ast_stream_get_state(ast_stream_topology_get_stream(media_state->topology, position)) != AST_STREAM_STATE_REMOVED) {
530                 media_state->default_session[type] = session_media;
531         }
532
533         return session_media;
534 }
535
536 static int is_stream_limitation_reached(enum ast_media_type type, const struct ast_sip_endpoint *endpoint, int *type_streams)
537 {
538         switch (type) {
539         case AST_MEDIA_TYPE_AUDIO:
540                 return !(type_streams[type] < endpoint->media.max_audio_streams);
541         case AST_MEDIA_TYPE_VIDEO:
542                 return !(type_streams[type] < endpoint->media.max_video_streams);
543         case AST_MEDIA_TYPE_IMAGE:
544                 /* We don't have an option for image (T.38) streams so cap it to one. */
545                 return (type_streams[type] > 0);
546         case AST_MEDIA_TYPE_UNKNOWN:
547         case AST_MEDIA_TYPE_TEXT:
548         default:
549                 /* We don't want any unknown or "other" streams on our endpoint,
550                  * so always just say we've reached the limit
551                  */
552                 return 1;
553         }
554 }
555
556 static int get_mid_bundle_group(const pjmedia_sdp_session *sdp, const char *mid)
557 {
558         int bundle_group = 0;
559         int index;
560
561         for (index = 0; index < sdp->attr_count; ++index) {
562                 pjmedia_sdp_attr *attr = sdp->attr[index];
563                 char value[pj_strlen(&attr->value) + 1], *mids = value, *attr_mid;
564
565                 if (pj_strcmp2(&attr->name, "group") || pj_strncmp2(&attr->value, "BUNDLE", 6)) {
566                         continue;
567                 }
568
569                 ast_copy_pj_str(value, &attr->value, sizeof(value));
570
571                 /* Skip the BUNDLE at the front */
572                 mids += 7;
573
574                 while ((attr_mid = strsep(&mids, " "))) {
575                         if (!strcmp(attr_mid, mid)) {
576                                 /* The ordering of attributes determines our internal identification of the bundle group based on number,
577                                  * with -1 being not in a bundle group. Since this is only exposed internally for response purposes it's
578                                  * actually even fine if things move around.
579                                  */
580                                 return bundle_group;
581                         }
582                 }
583
584                 bundle_group++;
585         }
586
587         return -1;
588 }
589
590 static int set_mid_and_bundle_group(struct ast_sip_session *session,
591         struct ast_sip_session_media *session_media,
592         const pjmedia_sdp_session *sdp,
593         const struct pjmedia_sdp_media *stream)
594 {
595         pjmedia_sdp_attr *attr;
596
597         if (!session->endpoint->media.bundle) {
598                 return 0;
599         }
600
601         /* By default on an incoming negotiation we assume no mid and bundle group is present */
602         ast_free(session_media->mid);
603         session_media->mid = NULL;
604         session_media->bundle_group = -1;
605         session_media->bundled = 0;
606
607         /* Grab the media identifier for the stream */
608         attr = pjmedia_sdp_media_find_attr2(stream, "mid", NULL);
609         if (!attr) {
610                 return 0;
611         }
612
613         session_media->mid = ast_calloc(1, attr->value.slen + 1);
614         if (!session_media->mid) {
615                 return 0;
616         }
617         ast_copy_pj_str(session_media->mid, &attr->value, attr->value.slen + 1);
618
619         /* Determine what bundle group this is part of */
620         session_media->bundle_group = get_mid_bundle_group(sdp, session_media->mid);
621
622         /* If this is actually part of a bundle group then the other side requested or accepted the bundle request */
623         session_media->bundled = session_media->bundle_group != -1;
624
625         return 0;
626 }
627
628 static void set_remote_mslabel_and_stream_group(struct ast_sip_session *session,
629         struct ast_sip_session_media *session_media,
630         const pjmedia_sdp_session *sdp,
631         const struct pjmedia_sdp_media *stream,
632         struct ast_stream *asterisk_stream)
633 {
634         int index;
635
636         ast_free(session_media->remote_mslabel);
637         session_media->remote_mslabel = NULL;
638
639         for (index = 0; index < stream->attr_count; ++index) {
640                 pjmedia_sdp_attr *attr = stream->attr[index];
641                 char attr_value[pj_strlen(&attr->value) + 1];
642                 char *ssrc_attribute_name, *ssrc_attribute_value = NULL;
643                 char *msid, *tmp = attr_value;
644                 static const pj_str_t STR_msid = { "msid", 4 };
645                 static const pj_str_t STR_ssrc = { "ssrc", 4 };
646
647                 if (!pj_strcmp(&attr->name, &STR_msid)) {
648                         ast_copy_pj_str(attr_value, &attr->value, sizeof(attr_value));
649                         msid = strsep(&tmp, " ");
650                         session_media->remote_mslabel = ast_strdup(msid);
651                         break;
652                 } else if (!pj_strcmp(&attr->name, &STR_ssrc)) {
653                         ast_copy_pj_str(attr_value, &attr->value, sizeof(attr_value));
654
655                         if ((ssrc_attribute_name = strchr(attr_value, ' '))) {
656                                 /* This has an actual attribute */
657                                 *ssrc_attribute_name++ = '\0';
658                                 ssrc_attribute_value = strchr(ssrc_attribute_name, ':');
659                                 if (ssrc_attribute_value) {
660                                         /* Values are actually optional according to the spec */
661                                         *ssrc_attribute_value++ = '\0';
662                                 }
663
664                                 if (!strcasecmp(ssrc_attribute_name, "mslabel") && !ast_strlen_zero(ssrc_attribute_value)) {
665                                         session_media->remote_mslabel = ast_strdup(ssrc_attribute_value);
666                                         break;
667                                 }
668                         }
669                 }
670         }
671
672         if (ast_strlen_zero(session_media->remote_mslabel)) {
673                 return;
674         }
675
676         /* Iterate through the existing streams looking for a match and if so then group this with it */
677         for (index = 0; index < AST_VECTOR_SIZE(&session->pending_media_state->sessions); ++index) {
678                 struct ast_sip_session_media *group_session_media;
679
680                 group_session_media = AST_VECTOR_GET(&session->pending_media_state->sessions, index);
681
682                 if (ast_strlen_zero(group_session_media->remote_mslabel) ||
683                         strcmp(group_session_media->remote_mslabel, session_media->remote_mslabel)) {
684                         continue;
685                 }
686
687                 ast_stream_set_group(asterisk_stream, index);
688                 break;
689         }
690 }
691
692 static void remove_stream_from_bundle(struct ast_sip_session_media *session_media,
693         struct ast_stream *stream)
694 {
695         ast_stream_set_state(stream, AST_STREAM_STATE_REMOVED);
696         ast_free(session_media->mid);
697         session_media->mid = NULL;
698         session_media->bundle_group = -1;
699         session_media->bundled = 0;
700 }
701
702 static int handle_incoming_sdp(struct ast_sip_session *session, const pjmedia_sdp_session *sdp)
703 {
704         int i;
705         int handled = 0;
706         int type_streams[AST_MEDIA_TYPE_END] = {0};
707
708         if (session->inv_session && session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
709                 ast_log(LOG_ERROR, "Failed to handle incoming SDP. Session has been already disconnected\n");
710                 return -1;
711         }
712
713         /* It is possible for SDP deferral to have already created a pending topology */
714         if (!session->pending_media_state->topology) {
715                 session->pending_media_state->topology = ast_stream_topology_alloc();
716                 if (!session->pending_media_state->topology) {
717                         return -1;
718                 }
719         }
720
721         for (i = 0; i < sdp->media_count; ++i) {
722                 /* See if there are registered handlers for this media stream type */
723                 char media[20];
724                 struct ast_sip_session_sdp_handler *handler;
725                 RAII_VAR(struct sdp_handler_list *, handler_list, NULL, ao2_cleanup);
726                 struct ast_sip_session_media *session_media = NULL;
727                 int res;
728                 enum ast_media_type type;
729                 struct ast_stream *stream = NULL;
730                 pjmedia_sdp_media *remote_stream = sdp->media[i];
731
732                 /* We need a null-terminated version of the media string */
733                 ast_copy_pj_str(media, &sdp->media[i]->desc.media, sizeof(media));
734                 type = ast_media_type_from_str(media);
735
736                 /* See if we have an already existing stream, which can occur from SDP deferral checking */
737                 if (i < ast_stream_topology_get_count(session->pending_media_state->topology)) {
738                         stream = ast_stream_topology_get_stream(session->pending_media_state->topology, i);
739                 }
740                 if (!stream) {
741                         struct ast_stream *existing_stream = NULL;
742
743                         if (session->active_media_state->topology &&
744                                 (i < ast_stream_topology_get_count(session->active_media_state->topology))) {
745                                 existing_stream = ast_stream_topology_get_stream(session->active_media_state->topology, i);
746                         }
747
748                         stream = ast_stream_alloc(existing_stream ? ast_stream_get_name(existing_stream) : ast_codec_media_type2str(type), type);
749                         if (!stream) {
750                                 return -1;
751                         }
752                         if (ast_stream_topology_set_stream(session->pending_media_state->topology, i, stream)) {
753                                 ast_stream_free(stream);
754                                 return -1;
755                         }
756                 }
757
758                 session_media = ast_sip_session_media_state_add(session, session->pending_media_state, ast_media_type_from_str(media), i);
759                 if (!session_media) {
760                         return -1;
761                 }
762
763                 /* If this stream is already declined mark it as such, or mark it as such if we've reached the limit */
764                 if (!remote_stream->desc.port || is_stream_limitation_reached(type, session->endpoint, type_streams)) {
765                         ast_debug(1, "Declining incoming SDP media stream '%s' at position '%d'\n",
766                                 ast_codec_media_type2str(type), i);
767                         remove_stream_from_bundle(session_media, stream);
768                         continue;
769                 }
770
771                 set_mid_and_bundle_group(session, session_media, sdp, remote_stream);
772                 set_remote_mslabel_and_stream_group(session, session_media, sdp, remote_stream, stream);
773
774                 if (session_media->handler) {
775                         handler = session_media->handler;
776                         ast_debug(1, "Negotiating incoming SDP media stream '%s' using %s SDP handler\n",
777                                 ast_codec_media_type2str(session_media->type),
778                                 session_media->handler->id);
779                         res = handler->negotiate_incoming_sdp_stream(session, session_media, sdp, i, stream);
780                         if (res < 0) {
781                                 /* Catastrophic failure. Abort! */
782                                 return -1;
783                         } else if (res == 0) {
784                                 ast_debug(1, "Declining incoming SDP media stream '%s' at position '%d'\n",
785                                         ast_codec_media_type2str(type), i);
786                                 remove_stream_from_bundle(session_media, stream);
787                                 continue;
788                         } else if (res > 0) {
789                                 ast_debug(1, "Media stream '%s' handled by %s\n",
790                                         ast_codec_media_type2str(session_media->type),
791                                         session_media->handler->id);
792                                 /* Handled by this handler. Move to the next stream */
793                                 handled = 1;
794                                 ++type_streams[type];
795                                 continue;
796                         }
797                 }
798
799                 handler_list = ao2_find(sdp_handlers, media, OBJ_KEY);
800                 if (!handler_list) {
801                         ast_debug(1, "No registered SDP handlers for media type '%s'\n", media);
802                         continue;
803                 }
804                 AST_LIST_TRAVERSE(&handler_list->list, handler, next) {
805                         if (handler == session_media->handler) {
806                                 continue;
807                         }
808                         ast_debug(1, "Negotiating incoming SDP media stream '%s' using %s SDP handler\n",
809                                 ast_codec_media_type2str(session_media->type),
810                                 handler->id);
811                         res = handler->negotiate_incoming_sdp_stream(session, session_media, sdp, i, stream);
812                         if (res < 0) {
813                                 /* Catastrophic failure. Abort! */
814                                 return -1;
815                         } else if (res == 0) {
816                                 ast_debug(1, "Declining incoming SDP media stream '%s' at position '%d'\n",
817                                         ast_codec_media_type2str(type), i);
818                                 remove_stream_from_bundle(session_media, stream);
819                                 continue;
820                         } else if (res > 0) {
821                                 ast_debug(1, "Media stream '%s' handled by %s\n",
822                                         ast_codec_media_type2str(session_media->type),
823                                         handler->id);
824                                 /* Handled by this handler. Move to the next stream */
825                                 session_media_set_handler(session_media, handler);
826                                 handled = 1;
827                                 ++type_streams[type];
828                                 break;
829                         }
830                 }
831         }
832         if (!handled) {
833                 return -1;
834         }
835         return 0;
836 }
837
838 static int handle_negotiated_sdp_session_media(struct ast_sip_session_media *session_media,
839                 struct ast_sip_session *session, const pjmedia_sdp_session *local,
840                 const pjmedia_sdp_session *remote, int index, struct ast_stream *asterisk_stream)
841 {
842         /* See if there are registered handlers for this media stream type */
843         struct pjmedia_sdp_media *local_stream = local->media[index];
844         char media[20];
845         struct ast_sip_session_sdp_handler *handler;
846         RAII_VAR(struct sdp_handler_list *, handler_list, NULL, ao2_cleanup);
847         int res;
848
849         /* For backwards compatibility we only reflect the stream state correctly on
850          * the non-default streams. This is because the stream state is also used for
851          * signaling that someone has placed us on hold. This situation is not handled
852          * currently and can result in the remote side being sort of placed on hold too.
853          */
854         if (!ast_sip_session_is_pending_stream_default(session, asterisk_stream)) {
855                 /* Determine the state of the stream based on our local SDP */
856                 if (pjmedia_sdp_media_find_attr2(local_stream, "sendonly", NULL)) {
857                         ast_stream_set_state(asterisk_stream, AST_STREAM_STATE_SENDONLY);
858                 } else if (pjmedia_sdp_media_find_attr2(local_stream, "recvonly", NULL)) {
859                         ast_stream_set_state(asterisk_stream, AST_STREAM_STATE_RECVONLY);
860                 } else if (pjmedia_sdp_media_find_attr2(local_stream, "inactive", NULL)) {
861                         ast_stream_set_state(asterisk_stream, AST_STREAM_STATE_INACTIVE);
862                 } else {
863                         ast_stream_set_state(asterisk_stream, AST_STREAM_STATE_SENDRECV);
864                 }
865         } else {
866                 ast_stream_set_state(asterisk_stream, AST_STREAM_STATE_SENDRECV);
867         }
868
869         /* We need a null-terminated version of the media string */
870         ast_copy_pj_str(media, &local->media[index]->desc.media, sizeof(media));
871
872         set_mid_and_bundle_group(session, session_media, remote, remote->media[index]);
873         set_remote_mslabel_and_stream_group(session, session_media, remote, remote->media[index], asterisk_stream);
874
875         handler = session_media->handler;
876         if (handler) {
877                 ast_debug(1, "Applying negotiated SDP media stream '%s' using %s SDP handler\n",
878                         ast_codec_media_type2str(session_media->type),
879                         handler->id);
880                 res = handler->apply_negotiated_sdp_stream(session, session_media, local, remote, index, asterisk_stream);
881                 if (res >= 0) {
882                         ast_debug(1, "Applied negotiated SDP media stream '%s' using %s SDP handler\n",
883                                 ast_codec_media_type2str(session_media->type),
884                                 handler->id);
885                         return 0;
886                 }
887                 return -1;
888         }
889
890         handler_list = ao2_find(sdp_handlers, media, OBJ_KEY);
891         if (!handler_list) {
892                 ast_debug(1, "No registered SDP handlers for media type '%s'\n", media);
893                 return -1;
894         }
895         AST_LIST_TRAVERSE(&handler_list->list, handler, next) {
896                 if (handler == session_media->handler) {
897                         continue;
898                 }
899                 ast_debug(1, "Applying negotiated SDP media stream '%s' using %s SDP handler\n",
900                         ast_codec_media_type2str(session_media->type),
901                         handler->id);
902                 res = handler->apply_negotiated_sdp_stream(session, session_media, local, remote, index, asterisk_stream);
903                 if (res < 0) {
904                         /* Catastrophic failure. Abort! */
905                         return -1;
906                 }
907                 if (res > 0) {
908                         ast_debug(1, "Applied negotiated SDP media stream '%s' using %s SDP handler\n",
909                                 ast_codec_media_type2str(session_media->type),
910                                 handler->id);
911                         /* Handled by this handler. Move to the next stream */
912                         session_media_set_handler(session_media, handler);
913                         return 0;
914                 }
915         }
916
917         if (session_media->handler && session_media->handler->stream_stop) {
918                 ast_debug(1, "Stopping SDP media stream '%s' as it is not currently negotiated\n",
919                         ast_codec_media_type2str(session_media->type));
920                 session_media->handler->stream_stop(session_media);
921         }
922
923         return 0;
924 }
925
926 static int handle_negotiated_sdp(struct ast_sip_session *session, const pjmedia_sdp_session *local, const pjmedia_sdp_session *remote)
927 {
928         int i;
929         struct ast_stream_topology *topology;
930         unsigned int changed = 0;
931
932         if (!session->pending_media_state->topology) {
933                 if (session->active_media_state->topology) {
934                         /*
935                          * This happens when we have negotiated media after receiving a 183,
936                          * and we're now receiving a 200 with a new SDP.  In this case, there
937                          * is active_media_state, but the pending_media_state has been reset.
938                          */
939                         struct ast_sip_session_media_state *active_media_state_clone;
940
941                         active_media_state_clone =
942                                 ast_sip_session_media_state_clone(session->active_media_state);
943                         if (!active_media_state_clone) {
944                                 ast_log(LOG_WARNING, "Unable to clone active media state for channel '%s'\n",
945                                         session->channel ? ast_channel_name(session->channel) : "unknown");
946                                 return -1;
947                         }
948
949                         ast_sip_session_media_state_free(session->pending_media_state);
950                         session->pending_media_state = active_media_state_clone;
951                 } else {
952                         ast_log(LOG_WARNING, "No pending or active media state for channel '%s'\n",
953                                 session->channel ? ast_channel_name(session->channel) : "unknown");
954                         return -1;
955                 }
956         }
957
958         /* If we're handling negotiated streams, then we should already have set
959          * up session media instances (and Asterisk streams) that correspond to
960          * the local SDP, and there should be the same number of session medias
961          * and streams as there are local SDP streams
962          */
963         if (ast_stream_topology_get_count(session->pending_media_state->topology) != local->media_count
964                 || AST_VECTOR_SIZE(&session->pending_media_state->sessions) != local->media_count) {
965                 ast_log(LOG_WARNING, "Local SDP for channel '%s' contains %d media streams while we expected it to contain %u\n",
966                         session->channel ? ast_channel_name(session->channel) : "unknown",
967                         ast_stream_topology_get_count(session->pending_media_state->topology), local->media_count);
968                 return -1;
969         }
970
971         for (i = 0; i < local->media_count; ++i) {
972                 struct ast_sip_session_media *session_media;
973                 struct ast_stream *stream;
974
975                 if (!remote->media[i]) {
976                         continue;
977                 }
978
979                 session_media = AST_VECTOR_GET(&session->pending_media_state->sessions, i);
980                 stream = ast_stream_topology_get_stream(session->pending_media_state->topology, i);
981
982                 /* Make sure that this stream is in the correct state. If we need to change
983                  * the state to REMOVED, then our work here is done, so go ahead and move on
984                  * to the next stream.
985                  */
986                 if (!remote->media[i]->desc.port) {
987                         ast_stream_set_state(stream, AST_STREAM_STATE_REMOVED);
988                         continue;
989                 }
990
991                 /* If the stream state is REMOVED, nothing needs to be done, so move on to the
992                  * next stream. This can occur if an internal thing has requested it to be
993                  * removed, or if we remove it as a result of the stream limit being reached.
994                  */
995                 if (ast_stream_get_state(stream) == AST_STREAM_STATE_REMOVED) {
996                         /*
997                          * Defer removing the handler until we are ready to activate
998                          * the new topology.  The channel's thread may still be using
999                          * the stream and we could crash before we are ready.
1000                          */
1001                         continue;
1002                 }
1003
1004                 if (handle_negotiated_sdp_session_media(session_media, session, local, remote, i, stream)) {
1005                         return -1;
1006                 }
1007
1008                 changed |= session_media->changed;
1009                 session_media->changed = 0;
1010         }
1011
1012         /* Apply the pending media state to the channel and make it active */
1013         ast_channel_lock(session->channel);
1014
1015         /* Now update the stream handler for any declined/removed streams */
1016         for (i = 0; i < local->media_count; ++i) {
1017                 struct ast_sip_session_media *session_media;
1018                 struct ast_stream *stream;
1019
1020                 if (!remote->media[i]) {
1021                         continue;
1022                 }
1023
1024                 session_media = AST_VECTOR_GET(&session->pending_media_state->sessions, i);
1025                 stream = ast_stream_topology_get_stream(session->pending_media_state->topology, i);
1026
1027                 if (ast_stream_get_state(stream) == AST_STREAM_STATE_REMOVED
1028                         && session_media->handler) {
1029                         /*
1030                          * This stream is no longer being used and the channel's thread
1031                          * is held off because we have the channel lock so release any
1032                          * resources the handler may have on it.
1033                          */
1034                         session_media_set_handler(session_media, NULL);
1035                 }
1036         }
1037
1038         /* Update the topology on the channel to match the accepted one */
1039         topology = ast_stream_topology_clone(session->pending_media_state->topology);
1040         if (topology) {
1041                 ast_channel_set_stream_topology(session->channel, topology);
1042         }
1043
1044         /* Remove all current file descriptors from the channel */
1045         for (i = 0; i < AST_VECTOR_SIZE(&session->active_media_state->read_callbacks); ++i) {
1046                 ast_channel_internal_fd_clear(session->channel, i + AST_EXTENDED_FDS);
1047         }
1048
1049         /* Add all the file descriptors from the pending media state */
1050         for (i = 0; i < AST_VECTOR_SIZE(&session->pending_media_state->read_callbacks); ++i) {
1051                 struct ast_sip_session_media_read_callback_state *callback_state;
1052
1053                 callback_state = AST_VECTOR_GET_ADDR(&session->pending_media_state->read_callbacks, i);
1054                 ast_channel_internal_fd_set(session->channel, i + AST_EXTENDED_FDS, callback_state->fd);
1055         }
1056
1057         /* Active and pending flip flop as needed */
1058         ast_sip_session_media_stats_save(session, session->active_media_state);
1059         SWAP(session->active_media_state, session->pending_media_state);
1060         ast_sip_session_media_state_reset(session->pending_media_state);
1061
1062         ast_channel_unlock(session->channel);
1063
1064         if (changed) {
1065                 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_STREAM_TOPOLOGY_SOURCE_CHANGED };
1066
1067                 ast_queue_frame(session->channel, &f);
1068         } else {
1069                 ast_queue_frame(session->channel, &ast_null_frame);
1070         }
1071
1072         return 0;
1073 }
1074
1075 #define DATASTORE_BUCKETS 53
1076 #define MEDIA_BUCKETS 7
1077
1078 static void session_datastore_destroy(void *obj)
1079 {
1080         struct ast_datastore *datastore = obj;
1081
1082         /* Using the destroy function (if present) destroy the data */
1083         if (datastore->info->destroy != NULL && datastore->data != NULL) {
1084                 datastore->info->destroy(datastore->data);
1085                 datastore->data = NULL;
1086         }
1087
1088         ast_free((void *) datastore->uid);
1089         datastore->uid = NULL;
1090 }
1091
1092 struct ast_datastore *ast_sip_session_alloc_datastore(const struct ast_datastore_info *info, const char *uid)
1093 {
1094         RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
1095         char uuid_buf[AST_UUID_STR_LEN];
1096         const char *uid_ptr = uid;
1097
1098         if (!info) {
1099                 return NULL;
1100         }
1101
1102         datastore = ao2_alloc(sizeof(*datastore), session_datastore_destroy);
1103         if (!datastore) {
1104                 return NULL;
1105         }
1106
1107         datastore->info = info;
1108         if (ast_strlen_zero(uid)) {
1109                 /* They didn't provide an ID so we'll provide one ourself */
1110                 uid_ptr = ast_uuid_generate_str(uuid_buf, sizeof(uuid_buf));
1111         }
1112
1113         datastore->uid = ast_strdup(uid_ptr);
1114         if (!datastore->uid) {
1115                 return NULL;
1116         }
1117
1118         ao2_ref(datastore, +1);
1119         return datastore;
1120 }
1121
1122 int ast_sip_session_add_datastore(struct ast_sip_session *session, struct ast_datastore *datastore)
1123 {
1124         ast_assert(datastore != NULL);
1125         ast_assert(datastore->info != NULL);
1126         ast_assert(ast_strlen_zero(datastore->uid) == 0);
1127
1128         if (!ao2_link(session->datastores, datastore)) {
1129                 return -1;
1130         }
1131         return 0;
1132 }
1133
1134 struct ast_datastore *ast_sip_session_get_datastore(struct ast_sip_session *session, const char *name)
1135 {
1136         return ao2_find(session->datastores, name, OBJ_KEY);
1137 }
1138
1139 void ast_sip_session_remove_datastore(struct ast_sip_session *session, const char *name)
1140 {
1141         ao2_callback(session->datastores, OBJ_KEY | OBJ_UNLINK | OBJ_NODATA, NULL, (void *) name);
1142 }
1143
1144 enum delayed_method {
1145         DELAYED_METHOD_INVITE,
1146         DELAYED_METHOD_UPDATE,
1147         DELAYED_METHOD_BYE,
1148 };
1149
1150 /*!
1151  * \internal
1152  * \brief Convert delayed method enum value to a string.
1153  * \since 13.3.0
1154  *
1155  * \param method Delayed method enum value to convert to a string.
1156  *
1157  * \return String value of delayed method.
1158  */
1159 static const char *delayed_method2str(enum delayed_method method)
1160 {
1161         const char *str = "<unknown>";
1162
1163         switch (method) {
1164         case DELAYED_METHOD_INVITE:
1165                 str = "INVITE";
1166                 break;
1167         case DELAYED_METHOD_UPDATE:
1168                 str = "UPDATE";
1169                 break;
1170         case DELAYED_METHOD_BYE:
1171                 str = "BYE";
1172                 break;
1173         }
1174
1175         return str;
1176 }
1177
1178 /*!
1179  * \brief Structure used for sending delayed requests
1180  *
1181  * Requests are typically delayed because the current transaction
1182  * state of an INVITE. Once the pending INVITE transaction terminates,
1183  * the delayed request will be sent
1184  */
1185 struct ast_sip_session_delayed_request {
1186         /*! Method of the request */
1187         enum delayed_method method;
1188         /*! Callback to call when the delayed request is created. */
1189         ast_sip_session_request_creation_cb on_request_creation;
1190         /*! Callback to call when the delayed request SDP is created */
1191         ast_sip_session_sdp_creation_cb on_sdp_creation;
1192         /*! Callback to call when the delayed request receives a response */
1193         ast_sip_session_response_cb on_response;
1194         /*! Whether to generate new SDP */
1195         int generate_new_sdp;
1196         /*! Requested media state for the SDP */
1197         struct ast_sip_session_media_state *media_state;
1198         AST_LIST_ENTRY(ast_sip_session_delayed_request) next;
1199 };
1200
1201 static struct ast_sip_session_delayed_request *delayed_request_alloc(
1202         enum delayed_method method,
1203         ast_sip_session_request_creation_cb on_request_creation,
1204         ast_sip_session_sdp_creation_cb on_sdp_creation,
1205         ast_sip_session_response_cb on_response,
1206         int generate_new_sdp,
1207         struct ast_sip_session_media_state *media_state)
1208 {
1209         struct ast_sip_session_delayed_request *delay = ast_calloc(1, sizeof(*delay));
1210
1211         if (!delay) {
1212                 return NULL;
1213         }
1214         delay->method = method;
1215         delay->on_request_creation = on_request_creation;
1216         delay->on_sdp_creation = on_sdp_creation;
1217         delay->on_response = on_response;
1218         delay->generate_new_sdp = generate_new_sdp;
1219         delay->media_state = media_state;
1220         return delay;
1221 }
1222
1223 static void delayed_request_free(struct ast_sip_session_delayed_request *delay)
1224 {
1225         ast_sip_session_media_state_free(delay->media_state);
1226         ast_free(delay);
1227 }
1228
1229 static int send_delayed_request(struct ast_sip_session *session, struct ast_sip_session_delayed_request *delay)
1230 {
1231         ast_debug(3, "Endpoint '%s(%s)' sending delayed %s request.\n",
1232                 ast_sorcery_object_get_id(session->endpoint),
1233                 session->channel ? ast_channel_name(session->channel) : "",
1234                 delayed_method2str(delay->method));
1235
1236         switch (delay->method) {
1237         case DELAYED_METHOD_INVITE:
1238                 ast_sip_session_refresh(session, delay->on_request_creation,
1239                         delay->on_sdp_creation, delay->on_response,
1240                         AST_SIP_SESSION_REFRESH_METHOD_INVITE, delay->generate_new_sdp, delay->media_state);
1241                 /* Ownership of media state transitions to ast_sip_session_refresh */
1242                 delay->media_state = NULL;
1243                 return 0;
1244         case DELAYED_METHOD_UPDATE:
1245                 ast_sip_session_refresh(session, delay->on_request_creation,
1246                         delay->on_sdp_creation, delay->on_response,
1247                         AST_SIP_SESSION_REFRESH_METHOD_UPDATE, delay->generate_new_sdp, delay->media_state);
1248                 /* Ownership of media state transitions to ast_sip_session_refresh */
1249                 delay->media_state = NULL;
1250                 return 0;
1251         case DELAYED_METHOD_BYE:
1252                 ast_sip_session_terminate(session, 0);
1253                 return 0;
1254         }
1255         ast_log(LOG_WARNING, "Don't know how to send delayed %s(%d) request.\n",
1256                 delayed_method2str(delay->method), delay->method);
1257         return -1;
1258 }
1259
1260 /*!
1261  * \internal
1262  * \brief The current INVITE transaction is in the PROCEEDING state.
1263  * \since 13.3.0
1264  *
1265  * \param vsession Session object.
1266  *
1267  * \retval 0 on success.
1268  * \retval -1 on error.
1269  */
1270 static int invite_proceeding(void *vsession)
1271 {
1272         struct ast_sip_session *session = vsession;
1273         struct ast_sip_session_delayed_request *delay;
1274         int found = 0;
1275         int res = 0;
1276
1277         AST_LIST_TRAVERSE_SAFE_BEGIN(&session->delayed_requests, delay, next) {
1278                 switch (delay->method) {
1279                 case DELAYED_METHOD_INVITE:
1280                         break;
1281                 case DELAYED_METHOD_UPDATE:
1282                         AST_LIST_REMOVE_CURRENT(next);
1283                         res = send_delayed_request(session, delay);
1284                         delayed_request_free(delay);
1285                         found = 1;
1286                         break;
1287                 case DELAYED_METHOD_BYE:
1288                         /* A BYE is pending so don't bother anymore. */
1289                         found = 1;
1290                         break;
1291                 }
1292                 if (found) {
1293                         break;
1294                 }
1295         }
1296         AST_LIST_TRAVERSE_SAFE_END;
1297
1298         ao2_ref(session, -1);
1299         return res;
1300 }
1301
1302 /*!
1303  * \internal
1304  * \brief The current INVITE transaction is in the TERMINATED state.
1305  * \since 13.3.0
1306  *
1307  * \param vsession Session object.
1308  *
1309  * \retval 0 on success.
1310  * \retval -1 on error.
1311  */
1312 static int invite_terminated(void *vsession)
1313 {
1314         struct ast_sip_session *session = vsession;
1315         struct ast_sip_session_delayed_request *delay;
1316         int found = 0;
1317         int res = 0;
1318         int timer_running;
1319
1320         /* re-INVITE collision timer running? */
1321         timer_running = pj_timer_entry_running(&session->rescheduled_reinvite);
1322
1323         AST_LIST_TRAVERSE_SAFE_BEGIN(&session->delayed_requests, delay, next) {
1324                 switch (delay->method) {
1325                 case DELAYED_METHOD_INVITE:
1326                         if (!timer_running) {
1327                                 found = 1;
1328                         }
1329                         break;
1330                 case DELAYED_METHOD_UPDATE:
1331                 case DELAYED_METHOD_BYE:
1332                         found = 1;
1333                         break;
1334                 }
1335                 if (found) {
1336                         AST_LIST_REMOVE_CURRENT(next);
1337                         res = send_delayed_request(session, delay);
1338                         delayed_request_free(delay);
1339                         break;
1340                 }
1341         }
1342         AST_LIST_TRAVERSE_SAFE_END;
1343
1344         ao2_ref(session, -1);
1345         return res;
1346 }
1347
1348 /*!
1349  * \internal
1350  * \brief INVITE collision timeout.
1351  * \since 13.3.0
1352  *
1353  * \param vsession Session object.
1354  *
1355  * \retval 0 on success.
1356  * \retval -1 on error.
1357  */
1358 static int invite_collision_timeout(void *vsession)
1359 {
1360         struct ast_sip_session *session = vsession;
1361         int res;
1362
1363         if (session->inv_session->invite_tsx) {
1364                 /*
1365                  * INVITE transaction still active.  Let it send
1366                  * the collision re-INVITE when it terminates.
1367                  */
1368                 ao2_ref(session, -1);
1369                 res = 0;
1370         } else {
1371                 res = invite_terminated(session);
1372         }
1373
1374         return res;
1375 }
1376
1377 /*!
1378  * \internal
1379  * \brief The current UPDATE transaction is in the COMPLETED state.
1380  * \since 13.3.0
1381  *
1382  * \param vsession Session object.
1383  *
1384  * \retval 0 on success.
1385  * \retval -1 on error.
1386  */
1387 static int update_completed(void *vsession)
1388 {
1389         struct ast_sip_session *session = vsession;
1390         int res;
1391
1392         if (session->inv_session->invite_tsx) {
1393                 res = invite_proceeding(session);
1394         } else {
1395                 res = invite_terminated(session);
1396         }
1397
1398         return res;
1399 }
1400
1401 static void check_delayed_requests(struct ast_sip_session *session,
1402         int (*cb)(void *vsession))
1403 {
1404         ao2_ref(session, +1);
1405         if (ast_sip_push_task(session->serializer, cb, session)) {
1406                 ao2_ref(session, -1);
1407         }
1408 }
1409
1410 static int delay_request(struct ast_sip_session *session,
1411         ast_sip_session_request_creation_cb on_request,
1412         ast_sip_session_sdp_creation_cb on_sdp_creation,
1413         ast_sip_session_response_cb on_response,
1414         int generate_new_sdp,
1415         enum delayed_method method,
1416         struct ast_sip_session_media_state *media_state)
1417 {
1418         struct ast_sip_session_delayed_request *delay = delayed_request_alloc(method,
1419                         on_request, on_sdp_creation, on_response, generate_new_sdp, media_state);
1420
1421         if (!delay) {
1422                 ast_sip_session_media_state_free(media_state);
1423                 return -1;
1424         }
1425
1426         if (method == DELAYED_METHOD_BYE) {
1427                 /* Send BYE as early as possible */
1428                 AST_LIST_INSERT_HEAD(&session->delayed_requests, delay, next);
1429         } else {
1430                 AST_LIST_INSERT_TAIL(&session->delayed_requests, delay, next);
1431         }
1432         return 0;
1433 }
1434
1435 static pjmedia_sdp_session *generate_session_refresh_sdp(struct ast_sip_session *session)
1436 {
1437         pjsip_inv_session *inv_session = session->inv_session;
1438         const pjmedia_sdp_session *previous_sdp = NULL;
1439
1440         if (inv_session->neg) {
1441                 if (pjmedia_sdp_neg_was_answer_remote(inv_session->neg)) {
1442                         pjmedia_sdp_neg_get_active_remote(inv_session->neg, &previous_sdp);
1443                 } else {
1444                         pjmedia_sdp_neg_get_active_local(inv_session->neg, &previous_sdp);
1445                 }
1446         }
1447         return create_local_sdp(inv_session, session, previous_sdp);
1448 }
1449
1450 static void set_from_header(struct ast_sip_session *session)
1451 {
1452         struct ast_party_id effective_id;
1453         struct ast_party_id connected_id;
1454         pj_pool_t *dlg_pool;
1455         pjsip_fromto_hdr *dlg_info;
1456         pjsip_contact_hdr *dlg_contact;
1457         pjsip_name_addr *dlg_info_name_addr;
1458         pjsip_sip_uri *dlg_info_uri;
1459         pjsip_sip_uri *dlg_contact_uri;
1460         int restricted;
1461         const char *pjsip_from_domain;
1462
1463         if (!session->channel || session->saved_from_hdr) {
1464                 return;
1465         }
1466
1467         /* We need to save off connected_id for RPID/PAI generation */
1468         ast_party_id_init(&connected_id);
1469         ast_channel_lock(session->channel);
1470         effective_id = ast_channel_connected_effective_id(session->channel);
1471         ast_party_id_copy(&connected_id, &effective_id);
1472         ast_channel_unlock(session->channel);
1473
1474         restricted =
1475                 ((ast_party_id_presentation(&connected_id) & AST_PRES_RESTRICTION) != AST_PRES_ALLOWED);
1476
1477         /* Now set up dlg->local.info so pjsip can correctly generate From */
1478
1479         dlg_pool = session->inv_session->dlg->pool;
1480         dlg_info = session->inv_session->dlg->local.info;
1481         dlg_contact = session->inv_session->dlg->local.contact;
1482         dlg_info_name_addr = (pjsip_name_addr *) dlg_info->uri;
1483         dlg_info_uri = pjsip_uri_get_uri(dlg_info_name_addr);
1484         dlg_contact_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(dlg_contact->uri);
1485
1486         if (session->endpoint->id.trust_outbound || !restricted) {
1487                 ast_sip_modify_id_header(dlg_pool, dlg_info, &connected_id);
1488                 if (ast_sip_get_use_callerid_contact() && ast_strlen_zero(session->endpoint->contact_user)) {
1489                         pj_strdup2(dlg_pool, &dlg_contact_uri->user, S_COR(connected_id.number.valid, connected_id.number.str, ""));
1490                 }
1491         }
1492
1493         ast_party_id_free(&connected_id);
1494
1495         if (!ast_strlen_zero(session->endpoint->fromuser)) {
1496                 dlg_info_name_addr->display.ptr = NULL;
1497                 dlg_info_name_addr->display.slen = 0;
1498                 pj_strdup2(dlg_pool, &dlg_info_uri->user, session->endpoint->fromuser);
1499         }
1500
1501         if (!ast_strlen_zero(session->endpoint->fromdomain)) {
1502                 pj_strdup2(dlg_pool, &dlg_info_uri->host, session->endpoint->fromdomain);
1503         }
1504
1505         /*
1506          * Channel variable for compatibility with chan_sip SIPFROMDOMAIN
1507          */
1508         ast_channel_lock(session->channel);
1509         pjsip_from_domain = pbx_builtin_getvar_helper(session->channel, "SIPFROMDOMAIN");
1510         if (!ast_strlen_zero(pjsip_from_domain)) {
1511                 ast_debug(3, "From header domain reset by channel variable SIPFROMDOMAIN (%s)\n", pjsip_from_domain);
1512                 pj_strdup2(dlg_pool, &dlg_info_uri->host, pjsip_from_domain);
1513         }
1514         ast_channel_unlock(session->channel);
1515
1516         /* We need to save off the non-anonymized From for RPID/PAI generation (for domain) */
1517         session->saved_from_hdr = pjsip_hdr_clone(dlg_pool, dlg_info);
1518         ast_sip_add_usereqphone(session->endpoint, dlg_pool, session->saved_from_hdr->uri);
1519
1520         /* In chan_sip, fromuser and fromdomain trump restricted so we only
1521          * anonymize if they're not set.
1522          */
1523         if (restricted) {
1524                 /* fromuser doesn't provide a display name so we always set it */
1525                 pj_strdup2(dlg_pool, &dlg_info_name_addr->display, "Anonymous");
1526
1527                 if (ast_strlen_zero(session->endpoint->fromuser)) {
1528                         pj_strdup2(dlg_pool, &dlg_info_uri->user, "anonymous");
1529                 }
1530
1531                 if (ast_sip_get_use_callerid_contact() && ast_strlen_zero(session->endpoint->contact_user)) {
1532                         pj_strdup2(dlg_pool, &dlg_contact_uri->user, "anonymous");
1533                 }
1534
1535                 if (ast_strlen_zero(session->endpoint->fromdomain)) {
1536                         pj_strdup2(dlg_pool, &dlg_info_uri->host, "anonymous.invalid");
1537                 }
1538         } else {
1539                 ast_sip_add_usereqphone(session->endpoint, dlg_pool, dlg_info->uri);
1540     }
1541 }
1542
1543 int ast_sip_session_refresh(struct ast_sip_session *session,
1544                 ast_sip_session_request_creation_cb on_request_creation,
1545                 ast_sip_session_sdp_creation_cb on_sdp_creation,
1546                 ast_sip_session_response_cb on_response,
1547                 enum ast_sip_session_refresh_method method, int generate_new_sdp,
1548                 struct ast_sip_session_media_state *media_state)
1549 {
1550         pjsip_inv_session *inv_session = session->inv_session;
1551         pjmedia_sdp_session *new_sdp = NULL;
1552         pjsip_tx_data *tdata;
1553
1554         if (media_state && (!media_state->topology || !generate_new_sdp)) {
1555                 ast_sip_session_media_state_free(media_state);
1556                 return -1;
1557         }
1558
1559         if (inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
1560                 /* Don't try to do anything with a hung-up call */
1561                 ast_debug(3, "Not sending reinvite to %s because of disconnected state...\n",
1562                                 ast_sorcery_object_get_id(session->endpoint));
1563                 ast_sip_session_media_state_free(media_state);
1564                 return 0;
1565         }
1566
1567         /* If the dialog has not yet been established we have to defer until it has */
1568         if (inv_session->dlg->state != PJSIP_DIALOG_STATE_ESTABLISHED) {
1569                 ast_debug(3, "Delay sending request to %s because dialog has not been established...\n",
1570                         ast_sorcery_object_get_id(session->endpoint));
1571                 return delay_request(session, on_request_creation, on_sdp_creation, on_response,
1572                         generate_new_sdp,
1573                         method == AST_SIP_SESSION_REFRESH_METHOD_INVITE
1574                                 ? DELAYED_METHOD_INVITE : DELAYED_METHOD_UPDATE,
1575                         media_state);
1576         }
1577
1578         if (method == AST_SIP_SESSION_REFRESH_METHOD_INVITE) {
1579                 if (inv_session->invite_tsx) {
1580                         /* We can't send a reinvite yet, so delay it */
1581                         ast_debug(3, "Delay sending reinvite to %s because of outstanding transaction...\n",
1582                                         ast_sorcery_object_get_id(session->endpoint));
1583                         return delay_request(session, on_request_creation, on_sdp_creation,
1584                                 on_response, generate_new_sdp, DELAYED_METHOD_INVITE, media_state);
1585                 } else if (inv_session->state != PJSIP_INV_STATE_CONFIRMED) {
1586                         /* Initial INVITE transaction failed to progress us to a confirmed state
1587                          * which means re-invites are not possible
1588                          */
1589                         ast_debug(3, "Not sending reinvite to %s because not in confirmed state...\n",
1590                                         ast_sorcery_object_get_id(session->endpoint));
1591                         ast_sip_session_media_state_free(media_state);
1592                         return 0;
1593                 }
1594         }
1595
1596         if (generate_new_sdp) {
1597                 /* SDP can only be generated if current negotiation has already completed */
1598                 if (inv_session->neg
1599                         && pjmedia_sdp_neg_get_state(inv_session->neg)
1600                                 != PJMEDIA_SDP_NEG_STATE_DONE) {
1601                         ast_debug(3, "Delay session refresh with new SDP to %s because SDP negotiation is not yet done...\n",
1602                                 ast_sorcery_object_get_id(session->endpoint));
1603                         return delay_request(session, on_request_creation, on_sdp_creation,
1604                                 on_response, generate_new_sdp,
1605                                 method == AST_SIP_SESSION_REFRESH_METHOD_INVITE
1606                                         ? DELAYED_METHOD_INVITE : DELAYED_METHOD_UPDATE, media_state);
1607                 }
1608
1609                 /* If an explicitly requested media state has been provided use it instead of any pending one */
1610                 if (media_state) {
1611                         int index;
1612                         int type_streams[AST_MEDIA_TYPE_END] = {0};
1613                         struct ast_stream *stream;
1614
1615                         /* Prune the media state so the number of streams fit within the configured limits - we do it here
1616                          * so that the index of the resulting streams in the SDP match. If we simply left the streams out
1617                          * of the SDP when producing it we'd be in trouble. We also enforce formats here for media types that
1618                          * are configurable on the endpoint.
1619                          */
1620                         for (index = 0; index < ast_stream_topology_get_count(media_state->topology); ++index) {
1621                                 struct ast_stream *existing_stream = NULL;
1622
1623                                 stream = ast_stream_topology_get_stream(media_state->topology, index);
1624
1625                                 if (session->active_media_state->topology &&
1626                                         index < ast_stream_topology_get_count(session->active_media_state->topology)) {
1627                                         existing_stream = ast_stream_topology_get_stream(session->active_media_state->topology, index);
1628                                 }
1629
1630                                 if (is_stream_limitation_reached(ast_stream_get_type(stream), session->endpoint, type_streams)) {
1631                                         if (index < AST_VECTOR_SIZE(&media_state->sessions)) {
1632                                                 struct ast_sip_session_media *session_media = AST_VECTOR_GET(&media_state->sessions, index);
1633
1634                                                 ao2_cleanup(session_media);
1635                                                 AST_VECTOR_REMOVE(&media_state->sessions, index, 1);
1636                                         }
1637
1638                                         ast_stream_topology_del_stream(media_state->topology, index);
1639
1640                                         /* A stream has potentially moved into our spot so we need to jump back so we process it */
1641                                         index -= 1;
1642                                         continue;
1643                                 }
1644
1645                                 /* No need to do anything with stream if it's media state is removed */
1646                                 if (ast_stream_get_state(stream) == AST_STREAM_STATE_REMOVED) {
1647                                         /* If there is no existing stream we can just not have this stream in the topology at all. */
1648                                         if (!existing_stream) {
1649                                                 ast_stream_topology_del_stream(media_state->topology, index);
1650                                                 index -= 1;
1651                                         }
1652                                         continue;
1653                                 }
1654
1655                                 /* Enforce the configured allowed codecs on audio and video streams */
1656                                 if (ast_stream_get_type(stream) == AST_MEDIA_TYPE_AUDIO || ast_stream_get_type(stream) == AST_MEDIA_TYPE_VIDEO) {
1657                                         struct ast_format_cap *joint_cap;
1658
1659                                         joint_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
1660                                         if (!joint_cap) {
1661                                                 ast_sip_session_media_state_free(media_state);
1662                                                 return 0;
1663                                         }
1664                                         ast_format_cap_get_compatible(ast_stream_get_formats(stream), session->endpoint->media.codecs, joint_cap);
1665                                         if (!ast_format_cap_count(joint_cap)) {
1666                                                 ao2_ref(joint_cap, -1);
1667
1668                                                 if (!existing_stream) {
1669                                                         /* If there is no existing stream we can just not have this stream in the topology
1670                                                          * at all.
1671                                                          */
1672                                                         ast_stream_topology_del_stream(media_state->topology, index);
1673                                                         index -= 1;
1674                                                         continue;
1675                                                 } else if (ast_stream_get_state(stream) != ast_stream_get_state(existing_stream) ||
1676                                                                 strcmp(ast_stream_get_name(stream), ast_stream_get_name(existing_stream))) {
1677                                                         /* If the underlying stream is a different type or different name then we have to
1678                                                          * mark it as removed, as it is replacing an existing stream. We do this so order
1679                                                          * is preserved.
1680                                                          */
1681                                                         ast_stream_set_state(stream, AST_STREAM_STATE_REMOVED);
1682                                                         continue;
1683                                                 } else {
1684                                                         /* However if the stream is otherwise remaining the same we can keep the formats
1685                                                          * that exist on it already which allows media to continue to flow.
1686                                                          */
1687                                                         joint_cap = ao2_bump(ast_stream_get_formats(existing_stream));
1688                                                 }
1689                                         }
1690                                         ast_stream_set_formats(stream, joint_cap);
1691                                         ao2_cleanup(joint_cap);
1692                                 }
1693
1694                                 ++type_streams[ast_stream_get_type(stream)];
1695                         }
1696
1697                         if (session->active_media_state->topology) {
1698                                 /* SDP is a fun thing. Take for example the fact that streams are never removed. They just become
1699                                  * declined. To better handle this in the case where something requests a topology change for fewer
1700                                  * streams than are currently present we fill in the topology to match the current number of streams
1701                                  * that are active.
1702                                  */
1703                                 for (index = ast_stream_topology_get_count(media_state->topology);
1704                                         index < ast_stream_topology_get_count(session->active_media_state->topology); ++index) {
1705                                         struct ast_stream *cloned;
1706
1707                                         stream = ast_stream_topology_get_stream(session->active_media_state->topology, index);
1708                                         ast_assert(stream != NULL);
1709
1710                                         cloned = ast_stream_clone(stream, NULL);
1711                                         if (!cloned) {
1712                                                 ast_sip_session_media_state_free(media_state);
1713                                                 return -1;
1714                                         }
1715
1716                                         ast_stream_set_state(cloned, AST_STREAM_STATE_REMOVED);
1717                                         if (ast_stream_topology_append_stream(media_state->topology, cloned) < 0) {
1718                                                 ast_stream_free(cloned);
1719                                                 ast_sip_session_media_state_free(media_state);
1720                                                 return -1;
1721                                         }
1722                                 }
1723
1724                                 /* If the resulting media state matches the existing active state don't bother doing a session refresh */
1725                                 if (ast_stream_topology_equal(session->active_media_state->topology, media_state->topology)) {
1726                                         ast_sip_session_media_state_free(media_state);
1727                                         return 0;
1728                                 }
1729                         }
1730
1731                         ast_sip_session_media_state_free(session->pending_media_state);
1732                         session->pending_media_state = media_state;
1733                 }
1734
1735                 new_sdp = generate_session_refresh_sdp(session);
1736                 if (!new_sdp) {
1737                         ast_log(LOG_ERROR, "Failed to generate session refresh SDP. Not sending session refresh\n");
1738                         ast_sip_session_media_state_reset(session->pending_media_state);
1739                         return -1;
1740                 }
1741                 if (on_sdp_creation) {
1742                         if (on_sdp_creation(session, new_sdp)) {
1743                                 ast_sip_session_media_state_reset(session->pending_media_state);
1744                                 return -1;
1745                         }
1746                 }
1747         }
1748
1749         if (method == AST_SIP_SESSION_REFRESH_METHOD_INVITE) {
1750                 if (pjsip_inv_reinvite(inv_session, NULL, new_sdp, &tdata)) {
1751                         ast_log(LOG_WARNING, "Failed to create reinvite properly.\n");
1752                         if (generate_new_sdp) {
1753                                 ast_sip_session_media_state_reset(session->pending_media_state);
1754                         }
1755                         return -1;
1756                 }
1757         } else if (pjsip_inv_update(inv_session, NULL, new_sdp, &tdata)) {
1758                 ast_log(LOG_WARNING, "Failed to create UPDATE properly.\n");
1759                 if (generate_new_sdp) {
1760                         ast_sip_session_media_state_reset(session->pending_media_state);
1761                 }
1762                 return -1;
1763         }
1764         if (on_request_creation) {
1765                 if (on_request_creation(session, tdata)) {
1766                         if (generate_new_sdp) {
1767                                 ast_sip_session_media_state_reset(session->pending_media_state);
1768                         }
1769                         return -1;
1770                 }
1771         }
1772         ast_debug(3, "Sending session refresh SDP via %s to %s\n",
1773                 method == AST_SIP_SESSION_REFRESH_METHOD_INVITE ? "re-INVITE" : "UPDATE",
1774                 ast_sorcery_object_get_id(session->endpoint));
1775         ast_sip_session_send_request_with_cb(session, tdata, on_response);
1776         return 0;
1777 }
1778
1779 int ast_sip_session_regenerate_answer(struct ast_sip_session *session,
1780                 ast_sip_session_sdp_creation_cb on_sdp_creation)
1781 {
1782         pjsip_inv_session *inv_session = session->inv_session;
1783         pjmedia_sdp_session *new_answer = NULL;
1784         const pjmedia_sdp_session *previous_offer = NULL;
1785
1786         /* The SDP answer can only be regenerated if it is still pending to be sent */
1787         if (!inv_session->neg || (pjmedia_sdp_neg_get_state(inv_session->neg) != PJMEDIA_SDP_NEG_STATE_REMOTE_OFFER &&
1788                 pjmedia_sdp_neg_get_state(inv_session->neg) != PJMEDIA_SDP_NEG_STATE_WAIT_NEGO)) {
1789                 ast_log(LOG_WARNING, "Requested to regenerate local SDP answer for channel '%s' but negotiation in state '%s'\n",
1790                         ast_channel_name(session->channel), pjmedia_sdp_neg_state_str(pjmedia_sdp_neg_get_state(inv_session->neg)));
1791                 return -1;
1792         }
1793
1794         pjmedia_sdp_neg_get_neg_remote(inv_session->neg, &previous_offer);
1795         if (pjmedia_sdp_neg_get_state(inv_session->neg) == PJMEDIA_SDP_NEG_STATE_WAIT_NEGO) {
1796                 /* Transition the SDP negotiator back to when it received the remote offer */
1797                 pjmedia_sdp_neg_negotiate(inv_session->pool, inv_session->neg, 0);
1798                 pjmedia_sdp_neg_set_remote_offer(inv_session->pool, inv_session->neg, previous_offer);
1799         }
1800
1801         new_answer = create_local_sdp(inv_session, session, previous_offer);
1802         if (!new_answer) {
1803                 ast_log(LOG_WARNING, "Could not create a new local SDP answer for channel '%s'\n",
1804                         ast_channel_name(session->channel));
1805                 return -1;
1806         }
1807
1808         if (on_sdp_creation) {
1809                 if (on_sdp_creation(session, new_answer)) {
1810                         return -1;
1811                 }
1812         }
1813
1814         pjsip_inv_set_sdp_answer(inv_session, new_answer);
1815
1816         return 0;
1817 }
1818
1819 void ast_sip_session_send_response(struct ast_sip_session *session, pjsip_tx_data *tdata)
1820 {
1821         handle_outgoing_response(session, tdata);
1822         pjsip_inv_send_msg(session->inv_session, tdata);
1823         return;
1824 }
1825
1826 static pj_bool_t session_on_rx_request(pjsip_rx_data *rdata);
1827
1828 static pjsip_module session_module = {
1829         .name = {"Session Module", 14},
1830         .priority = PJSIP_MOD_PRIORITY_APPLICATION,
1831         .on_rx_request = session_on_rx_request,
1832 };
1833
1834 /*! \brief Determine whether the SDP provided requires deferral of negotiating or not
1835  *
1836  * \retval 1 re-invite should be deferred and resumed later
1837  * \retval 0 re-invite should not be deferred
1838  */
1839 static int sdp_requires_deferral(struct ast_sip_session *session, const pjmedia_sdp_session *sdp)
1840 {
1841         int i;
1842
1843         if (!session->pending_media_state->topology) {
1844                 session->pending_media_state->topology = ast_stream_topology_alloc();
1845                 if (!session->pending_media_state->topology) {
1846                         return -1;
1847                 }
1848         }
1849
1850         for (i = 0; i < sdp->media_count; ++i) {
1851                 /* See if there are registered handlers for this media stream type */
1852                 char media[20];
1853                 struct ast_sip_session_sdp_handler *handler;
1854                 RAII_VAR(struct sdp_handler_list *, handler_list, NULL, ao2_cleanup);
1855                 struct ast_stream *existing_stream = NULL;
1856                 struct ast_stream *stream;
1857                 enum ast_media_type type;
1858                 struct ast_sip_session_media *session_media = NULL;
1859                 enum ast_sip_session_sdp_stream_defer res;
1860
1861                 /* We need a null-terminated version of the media string */
1862                 ast_copy_pj_str(media, &sdp->media[i]->desc.media, sizeof(media));
1863
1864                 if (session->active_media_state->topology &&
1865                         (i < ast_stream_topology_get_count(session->active_media_state->topology))) {
1866                         existing_stream = ast_stream_topology_get_stream(session->active_media_state->topology, i);
1867                 }
1868
1869                 type = ast_media_type_from_str(media);
1870                 stream = ast_stream_alloc(existing_stream ? ast_stream_get_name(existing_stream) : ast_codec_media_type2str(type), type);
1871                 if (!stream) {
1872                         return -1;
1873                 }
1874
1875                 /* As this is only called on an incoming SDP offer before processing it is not possible
1876                  * for streams and their media sessions to exist.
1877                  */
1878                 if (ast_stream_topology_set_stream(session->pending_media_state->topology, i, stream)) {
1879                         ast_stream_free(stream);
1880                         return -1;
1881                 }
1882
1883                 session_media = ast_sip_session_media_state_add(session, session->pending_media_state, ast_media_type_from_str(media), i);
1884                 if (!session_media) {
1885                         return -1;
1886                 }
1887
1888                 if (session_media->handler) {
1889                         handler = session_media->handler;
1890                         if (handler->defer_incoming_sdp_stream) {
1891                                 res = handler->defer_incoming_sdp_stream(session, session_media, sdp,
1892                                         sdp->media[i]);
1893                                 switch (res) {
1894                                 case AST_SIP_SESSION_SDP_DEFER_NOT_HANDLED:
1895                                         break;
1896                                 case AST_SIP_SESSION_SDP_DEFER_ERROR:
1897                                         return 0;
1898                                 case AST_SIP_SESSION_SDP_DEFER_NOT_NEEDED:
1899                                         break;
1900                                 case AST_SIP_SESSION_SDP_DEFER_NEEDED:
1901                                         return 1;
1902                                 }
1903                         }
1904                         /* Handled by this handler. Move to the next stream */
1905                         continue;
1906                 }
1907
1908                 handler_list = ao2_find(sdp_handlers, media, OBJ_KEY);
1909                 if (!handler_list) {
1910                         ast_debug(1, "No registered SDP handlers for media type '%s'\n", media);
1911                         continue;
1912                 }
1913                 AST_LIST_TRAVERSE(&handler_list->list, handler, next) {
1914                         if (handler == session_media->handler) {
1915                                 continue;
1916                         }
1917                         if (!handler->defer_incoming_sdp_stream) {
1918                                 continue;
1919                         }
1920                         res = handler->defer_incoming_sdp_stream(session, session_media, sdp,
1921                                 sdp->media[i]);
1922                         switch (res) {
1923                         case AST_SIP_SESSION_SDP_DEFER_NOT_HANDLED:
1924                                 continue;
1925                         case AST_SIP_SESSION_SDP_DEFER_ERROR:
1926                                 session_media_set_handler(session_media, handler);
1927                                 return 0;
1928                         case AST_SIP_SESSION_SDP_DEFER_NOT_NEEDED:
1929                                 /* Handled by this handler. */
1930                                 session_media_set_handler(session_media, handler);
1931                                 break;
1932                         case AST_SIP_SESSION_SDP_DEFER_NEEDED:
1933                                 /* Handled by this handler. */
1934                                 session_media_set_handler(session_media, handler);
1935                                 return 1;
1936                         }
1937                         /* Move to the next stream */
1938                         break;
1939                 }
1940         }
1941         return 0;
1942 }
1943
1944 static pj_bool_t session_reinvite_on_rx_request(pjsip_rx_data *rdata)
1945 {
1946         pjsip_dialog *dlg;
1947         RAII_VAR(struct ast_sip_session *, session, NULL, ao2_cleanup);
1948         pjsip_rdata_sdp_info *sdp_info;
1949
1950         if (rdata->msg_info.msg->line.req.method.id != PJSIP_INVITE_METHOD ||
1951                 !(dlg = pjsip_ua_find_dialog(&rdata->msg_info.cid->id, &rdata->msg_info.to->tag, &rdata->msg_info.from->tag, PJ_FALSE)) ||
1952                 !(session = ast_sip_dialog_get_session(dlg)) ||
1953                 !session->channel) {
1954                 return PJ_FALSE;
1955         }
1956
1957         if (session->deferred_reinvite) {
1958                 pj_str_t key, deferred_key;
1959                 pjsip_tx_data *tdata;
1960
1961                 /* We use memory from the new request on purpose so the deferred reinvite pool does not grow uncontrollably */
1962                 pjsip_tsx_create_key(rdata->tp_info.pool, &key, PJSIP_ROLE_UAS, &rdata->msg_info.cseq->method, rdata);
1963                 pjsip_tsx_create_key(rdata->tp_info.pool, &deferred_key, PJSIP_ROLE_UAS, &session->deferred_reinvite->msg_info.cseq->method,
1964                         session->deferred_reinvite);
1965
1966                 /* If this is a retransmission ignore it */
1967                 if (!pj_strcmp(&key, &deferred_key)) {
1968                         return PJ_TRUE;
1969                 }
1970
1971                 /* Otherwise this is a new re-invite, so reject it */
1972                 if (pjsip_dlg_create_response(dlg, rdata, 491, NULL, &tdata) == PJ_SUCCESS) {
1973                         if (pjsip_endpt_send_response2(ast_sip_get_pjsip_endpoint(), rdata, tdata, NULL, NULL) != PJ_SUCCESS) {
1974                                 pjsip_tx_data_dec_ref(tdata);
1975                         }
1976                 }
1977
1978                 return PJ_TRUE;
1979         }
1980
1981         if (!(sdp_info = pjsip_rdata_get_sdp_info(rdata)) ||
1982                 (sdp_info->sdp_err != PJ_SUCCESS)) {
1983                 return PJ_FALSE;
1984         }
1985
1986         if (!sdp_info->sdp) {
1987                 const pjmedia_sdp_session *local;
1988                 int i;
1989
1990                 ast_queue_unhold(session->channel);
1991
1992                 pjmedia_sdp_neg_get_active_local(session->inv_session->neg, &local);
1993                 if (!local) {
1994                         return PJ_FALSE;
1995                 }
1996
1997                 /*
1998                  * Some devices indicate hold with deferred SDP reinvites (i.e. no SDP in the reinvite).
1999                  * When hold is initially indicated, we
2000                  * - Receive an INVITE with no SDP
2001                  * - Send a 200 OK with SDP, indicating sendrecv in the media streams
2002                  * - Receive an ACK with SDP, indicating sendonly in the media streams
2003                  *
2004                  * At this point, the pjmedia negotiator saves the state of the media direction so that
2005                  * if we are to send any offers, we'll offer recvonly in the media streams. This is
2006                  * problematic if the device is attempting to unhold, though. If the device unholds
2007                  * by sending a reinvite with no SDP, then we will respond with a 200 OK with recvonly.
2008                  * According to RFC 3264, if an offerer offers recvonly, then the answerer MUST respond
2009                  * with sendonly or inactive. The result of this is that the stream is not off hold.
2010                  *
2011                  * Therefore, in this case, when we receive a reinvite while the stream is on hold, we
2012                  * need to be sure to offer sendrecv. This way, the answerer can respond with sendrecv
2013                  * in order to get the stream off hold. If this is actually a different purpose reinvite
2014                  * (like a session timer refresh), then the answerer can respond to our sendrecv with
2015                  * sendonly, keeping the stream on hold.
2016                  */
2017                 for (i = 0; i < local->media_count; ++i) {
2018                         pjmedia_sdp_media *m = local->media[i];
2019                         pjmedia_sdp_attr *recvonly;
2020                         pjmedia_sdp_attr *inactive;
2021
2022                         recvonly = pjmedia_sdp_attr_find2(m->attr_count, m->attr, "recvonly", NULL);
2023                         inactive = pjmedia_sdp_attr_find2(m->attr_count, m->attr, "inactive", NULL);
2024                         if (recvonly || inactive) {
2025                                 pjmedia_sdp_attr *to_remove = recvonly ?: inactive;
2026                                 pjmedia_sdp_attr *sendrecv;
2027
2028                                 pjmedia_sdp_attr_remove(&m->attr_count, m->attr, to_remove);
2029
2030                                 sendrecv = pjmedia_sdp_attr_create(session->inv_session->pool, "sendrecv", NULL);
2031                                 pjmedia_sdp_media_add_attr(m, sendrecv);
2032                         }
2033                 }
2034
2035                 return PJ_FALSE;
2036         }
2037
2038         if (!sdp_requires_deferral(session, sdp_info->sdp)) {
2039                 return PJ_FALSE;
2040         }
2041
2042         pjsip_rx_data_clone(rdata, 0, &session->deferred_reinvite);
2043
2044         return PJ_TRUE;
2045 }
2046
2047 void ast_sip_session_resume_reinvite(struct ast_sip_session *session)
2048 {
2049         if (!session->deferred_reinvite) {
2050                 return;
2051         }
2052
2053         if (session->channel) {
2054                 pjsip_endpt_process_rx_data(ast_sip_get_pjsip_endpoint(),
2055                         session->deferred_reinvite, NULL, NULL);
2056         }
2057         pjsip_rx_data_free_cloned(session->deferred_reinvite);
2058         session->deferred_reinvite = NULL;
2059 }
2060
2061 static pjsip_module session_reinvite_module = {
2062         .name = { "Session Re-Invite Module", 24 },
2063         .priority = PJSIP_MOD_PRIORITY_UA_PROXY_LAYER - 1,
2064         .on_rx_request = session_reinvite_on_rx_request,
2065 };
2066
2067
2068 void ast_sip_session_send_request_with_cb(struct ast_sip_session *session, pjsip_tx_data *tdata,
2069                 ast_sip_session_response_cb on_response)
2070 {
2071         pjsip_inv_session *inv_session = session->inv_session;
2072
2073         /* For every request except BYE we disallow sending of the message when
2074          * the session has been disconnected. A BYE request is special though
2075          * because it can be sent again after the session is disconnected except
2076          * with credentials.
2077          */
2078         if (inv_session->state == PJSIP_INV_STATE_DISCONNECTED &&
2079                 tdata->msg->line.req.method.id != PJSIP_BYE_METHOD) {
2080                 return;
2081         }
2082
2083         ast_sip_mod_data_set(tdata->pool, tdata->mod_data, session_module.id,
2084                              MOD_DATA_ON_RESPONSE, on_response);
2085
2086         handle_outgoing_request(session, tdata);
2087         pjsip_inv_send_msg(session->inv_session, tdata);
2088
2089         return;
2090 }
2091
2092 void ast_sip_session_send_request(struct ast_sip_session *session, pjsip_tx_data *tdata)
2093 {
2094         ast_sip_session_send_request_with_cb(session, tdata, NULL);
2095 }
2096
2097 int ast_sip_session_create_invite(struct ast_sip_session *session, pjsip_tx_data **tdata)
2098 {
2099         pjmedia_sdp_session *offer;
2100
2101         if (!(offer = create_local_sdp(session->inv_session, session, NULL))) {
2102                 pjsip_inv_terminate(session->inv_session, 500, PJ_FALSE);
2103                 return -1;
2104         }
2105
2106         pjsip_inv_set_local_sdp(session->inv_session, offer);
2107         pjmedia_sdp_neg_set_prefer_remote_codec_order(session->inv_session->neg, PJ_FALSE);
2108 #ifdef PJMEDIA_SDP_NEG_ANSWER_MULTIPLE_CODECS
2109         if (!session->endpoint->preferred_codec_only) {
2110                 pjmedia_sdp_neg_set_answer_multiple_codecs(session->inv_session->neg, PJ_TRUE);
2111         }
2112 #endif
2113
2114         /*
2115          * We MUST call set_from_header() before pjsip_inv_invite.  If we don't, the
2116          * From in the initial INVITE will be wrong but the rest of the messages will be OK.
2117          */
2118         set_from_header(session);
2119
2120         if (pjsip_inv_invite(session->inv_session, tdata) != PJ_SUCCESS) {
2121                 return -1;
2122         }
2123
2124         return 0;
2125 }
2126
2127 static int datastore_hash(const void *obj, int flags)
2128 {
2129         const struct ast_datastore *datastore = obj;
2130         const char *uid = flags & OBJ_KEY ? obj : datastore->uid;
2131
2132         ast_assert(uid != NULL);
2133
2134         return ast_str_hash(uid);
2135 }
2136
2137 static int datastore_cmp(void *obj, void *arg, int flags)
2138 {
2139         const struct ast_datastore *datastore1 = obj;
2140         const struct ast_datastore *datastore2 = arg;
2141         const char *uid2 = flags & OBJ_KEY ? arg : datastore2->uid;
2142
2143         ast_assert(datastore1->uid != NULL);
2144         ast_assert(uid2 != NULL);
2145
2146         return strcmp(datastore1->uid, uid2) ? 0 : CMP_MATCH | CMP_STOP;
2147 }
2148
2149 static void session_destructor(void *obj)
2150 {
2151         struct ast_sip_session *session = obj;
2152         struct ast_sip_session_delayed_request *delay;
2153         const char *endpoint_name = session->endpoint ?
2154                 ast_sorcery_object_get_id(session->endpoint) : "<none>";
2155
2156         ast_debug(3, "Destroying SIP session with endpoint %s\n", endpoint_name);
2157
2158         ast_test_suite_event_notify("SESSION_DESTROYING",
2159                 "Endpoint: %s\r\n"
2160                 "AOR: %s\r\n"
2161                 "Contact: %s"
2162                 , endpoint_name
2163                 , session->aor ? ast_sorcery_object_get_id(session->aor) : "<none>"
2164                 , session->contact ? ast_sorcery_object_get_id(session->contact) : "<none>"
2165                 );
2166
2167         /* fire session destroy handler */
2168         handle_session_destroy(session);
2169
2170         /* remove all registered supplements */
2171         ast_sip_session_remove_supplements(session);
2172         AST_LIST_HEAD_DESTROY(&session->supplements);
2173
2174         /* remove all saved media stats */
2175         AST_VECTOR_RESET(&session->media_stats, ast_free);
2176         AST_VECTOR_FREE(&session->media_stats);
2177
2178         ast_taskprocessor_unreference(session->serializer);
2179         ao2_cleanup(session->datastores);
2180         ast_sip_session_media_state_free(session->active_media_state);
2181         ast_sip_session_media_state_free(session->pending_media_state);
2182
2183         while ((delay = AST_LIST_REMOVE_HEAD(&session->delayed_requests, next))) {
2184                 delayed_request_free(delay);
2185         }
2186         ast_party_id_free(&session->id);
2187         ao2_cleanup(session->endpoint);
2188         ao2_cleanup(session->aor);
2189         ao2_cleanup(session->contact);
2190         ao2_cleanup(session->direct_media_cap);
2191
2192         ast_dsp_free(session->dsp);
2193
2194         if (session->inv_session) {
2195                 pjsip_dlg_dec_session(session->inv_session->dlg, &session_module);
2196         }
2197
2198         ast_test_suite_event_notify("SESSION_DESTROYED", "Endpoint: %s", endpoint_name);
2199 }
2200
2201 /*! \brief Destructor for SIP channel */
2202 static void sip_channel_destroy(void *obj)
2203 {
2204         struct ast_sip_channel_pvt *channel = obj;
2205
2206         ao2_cleanup(channel->pvt);
2207         ao2_cleanup(channel->session);
2208 }
2209
2210 struct ast_sip_channel_pvt *ast_sip_channel_pvt_alloc(void *pvt, struct ast_sip_session *session)
2211 {
2212         struct ast_sip_channel_pvt *channel = ao2_alloc(sizeof(*channel), sip_channel_destroy);
2213
2214         if (!channel) {
2215                 return NULL;
2216         }
2217
2218         ao2_ref(pvt, +1);
2219         channel->pvt = pvt;
2220         ao2_ref(session, +1);
2221         channel->session = session;
2222
2223         return channel;
2224 }
2225
2226 struct ast_sip_session *ast_sip_session_alloc(struct ast_sip_endpoint *endpoint,
2227         struct ast_sip_contact *contact, pjsip_inv_session *inv_session, pjsip_rx_data *rdata)
2228 {
2229         RAII_VAR(struct ast_sip_session *, session, NULL, ao2_cleanup);
2230         struct ast_sip_session *ret_session;
2231         int dsp_features = 0;
2232
2233         session = ao2_alloc(sizeof(*session), session_destructor);
2234         if (!session) {
2235                 return NULL;
2236         }
2237
2238         AST_LIST_HEAD_INIT(&session->supplements);
2239         AST_LIST_HEAD_INIT_NOLOCK(&session->delayed_requests);
2240         ast_party_id_init(&session->id);
2241
2242         session->direct_media_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
2243         if (!session->direct_media_cap) {
2244                 return NULL;
2245         }
2246         session->datastores = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
2247                 DATASTORE_BUCKETS, datastore_hash, NULL, datastore_cmp);
2248         if (!session->datastores) {
2249                 return NULL;
2250         }
2251         session->active_media_state = ast_sip_session_media_state_alloc();
2252         if (!session->active_media_state) {
2253                 return NULL;
2254         }
2255         session->pending_media_state = ast_sip_session_media_state_alloc();
2256         if (!session->pending_media_state) {
2257                 return NULL;
2258         }
2259         if (AST_VECTOR_INIT(&session->media_stats, 1) < 0) {
2260                 return NULL;
2261         }
2262
2263         if (endpoint->dtmf == AST_SIP_DTMF_INBAND || endpoint->dtmf == AST_SIP_DTMF_AUTO) {
2264                 dsp_features |= DSP_FEATURE_DIGIT_DETECT;
2265         }
2266         if (endpoint->faxdetect) {
2267                 dsp_features |= DSP_FEATURE_FAX_DETECT;
2268         }
2269         if (dsp_features) {
2270                 session->dsp = ast_dsp_new();
2271                 if (!session->dsp) {
2272                         return NULL;
2273                 }
2274
2275                 ast_dsp_set_features(session->dsp, dsp_features);
2276         }
2277
2278         session->endpoint = ao2_bump(endpoint);
2279
2280         if (rdata) {
2281                 /*
2282                  * We must continue using the serializer that the original
2283                  * INVITE came in on for the dialog.  There may be
2284                  * retransmissions already enqueued in the original
2285                  * serializer that can result in reentrancy and message
2286                  * sequencing problems.
2287                  */
2288                 session->serializer = ast_sip_get_distributor_serializer(rdata);
2289         } else {
2290                 char tps_name[AST_TASKPROCESSOR_MAX_NAME + 1];
2291
2292                 /* Create name with seq number appended. */
2293                 ast_taskprocessor_build_name(tps_name, sizeof(tps_name), "pjsip/outsess/%s",
2294                         ast_sorcery_object_get_id(endpoint));
2295
2296                 session->serializer = ast_sip_create_serializer(tps_name);
2297         }
2298         if (!session->serializer) {
2299                 return NULL;
2300         }
2301         ast_sip_dialog_set_serializer(inv_session->dlg, session->serializer);
2302         ast_sip_dialog_set_endpoint(inv_session->dlg, endpoint);
2303         pjsip_dlg_inc_session(inv_session->dlg, &session_module);
2304         inv_session->mod_data[session_module.id] = ao2_bump(session);
2305         session->contact = ao2_bump(contact);
2306         session->inv_session = inv_session;
2307
2308         session->dtmf = endpoint->dtmf;
2309         session->moh_passthrough = endpoint->moh_passthrough;
2310
2311         if (ast_sip_session_add_supplements(session)) {
2312                 /* Release the ref held by session->inv_session */
2313                 ao2_ref(session, -1);
2314                 return NULL;
2315         }
2316
2317         /* Fire seesion begin handlers */
2318         handle_session_begin(session);
2319
2320         /* Avoid unnecessary ref manipulation to return a session */
2321         ret_session = session;
2322         session = NULL;
2323         return ret_session;
2324 }
2325
2326 /*! \brief struct controlling the suspension of the session's serializer. */
2327 struct ast_sip_session_suspender {
2328         ast_cond_t cond_suspended;
2329         ast_cond_t cond_complete;
2330         int suspended;
2331         int complete;
2332 };
2333
2334 static void sip_session_suspender_dtor(void *vdoomed)
2335 {
2336         struct ast_sip_session_suspender *doomed = vdoomed;
2337
2338         ast_cond_destroy(&doomed->cond_suspended);
2339         ast_cond_destroy(&doomed->cond_complete);
2340 }
2341
2342 /*!
2343  * \internal
2344  * \brief Block the session serializer thread task.
2345  *
2346  * \param data Pushed serializer task data for suspension.
2347  *
2348  * \retval 0
2349  */
2350 static int sip_session_suspend_task(void *data)
2351 {
2352         struct ast_sip_session_suspender *suspender = data;
2353
2354         ao2_lock(suspender);
2355
2356         /* Signal that the serializer task is now suspended. */
2357         suspender->suspended = 1;
2358         ast_cond_signal(&suspender->cond_suspended);
2359
2360         /* Wait for the serializer suspension to be completed. */
2361         while (!suspender->complete) {
2362                 ast_cond_wait(&suspender->cond_complete, ao2_object_get_lockaddr(suspender));
2363         }
2364
2365         ao2_unlock(suspender);
2366         ao2_ref(suspender, -1);
2367
2368         return 0;
2369 }
2370
2371 void ast_sip_session_suspend(struct ast_sip_session *session)
2372 {
2373         struct ast_sip_session_suspender *suspender;
2374         int res;
2375
2376         ast_assert(session->suspended == NULL);
2377
2378         if (ast_taskprocessor_is_task(session->serializer)) {
2379                 /* I am the session's serializer thread so I cannot suspend. */
2380                 return;
2381         }
2382
2383         if (ast_taskprocessor_is_suspended(session->serializer)) {
2384                 /* The serializer already suspended. */
2385                 return;
2386         }
2387
2388         suspender = ao2_alloc(sizeof(*suspender), sip_session_suspender_dtor);
2389         if (!suspender) {
2390                 /* We will just have to hope that the system does not deadlock */
2391                 return;
2392         }
2393         ast_cond_init(&suspender->cond_suspended, NULL);
2394         ast_cond_init(&suspender->cond_complete, NULL);
2395
2396         ao2_ref(suspender, +1);
2397         res = ast_sip_push_task(session->serializer, sip_session_suspend_task, suspender);
2398         if (res) {
2399                 /* We will just have to hope that the system does not deadlock */
2400                 ao2_ref(suspender, -2);
2401                 return;
2402         }
2403
2404         session->suspended = suspender;
2405
2406         /* Wait for the serializer to get suspended. */
2407         ao2_lock(suspender);
2408         while (!suspender->suspended) {
2409                 ast_cond_wait(&suspender->cond_suspended, ao2_object_get_lockaddr(suspender));
2410         }
2411         ao2_unlock(suspender);
2412
2413         ast_taskprocessor_suspend(session->serializer);
2414 }
2415
2416 void ast_sip_session_unsuspend(struct ast_sip_session *session)
2417 {
2418         struct ast_sip_session_suspender *suspender = session->suspended;
2419
2420         if (!suspender) {
2421                 /* Nothing to do */
2422                 return;
2423         }
2424         session->suspended = NULL;
2425
2426         /* Signal that the serializer task suspension is now complete. */
2427         ao2_lock(suspender);
2428         suspender->complete = 1;
2429         ast_cond_signal(&suspender->cond_complete);
2430         ao2_unlock(suspender);
2431
2432         ao2_ref(suspender, -1);
2433
2434         ast_taskprocessor_unsuspend(session->serializer);
2435 }
2436
2437 /*!
2438  * \internal
2439  * \brief Handle initial INVITE challenge response message.
2440  * \since 13.5.0
2441  *
2442  * \param rdata PJSIP receive response message data.
2443  *
2444  * \retval PJ_FALSE Did not handle message.
2445  * \retval PJ_TRUE Handled message.
2446  */
2447 static pj_bool_t outbound_invite_auth(pjsip_rx_data *rdata)
2448 {
2449         pjsip_transaction *tsx;
2450         pjsip_dialog *dlg;
2451         pjsip_inv_session *inv;
2452         pjsip_tx_data *tdata;
2453         struct ast_sip_session *session;
2454
2455         if (rdata->msg_info.msg->line.status.code != 401
2456                 && rdata->msg_info.msg->line.status.code != 407) {
2457                 /* Doesn't pertain to us. Move on */
2458                 return PJ_FALSE;
2459         }
2460
2461         tsx = pjsip_rdata_get_tsx(rdata);
2462         dlg = pjsip_rdata_get_dlg(rdata);
2463         if (!dlg || !tsx) {
2464                 return PJ_FALSE;
2465         }
2466
2467         if (tsx->method.id != PJSIP_INVITE_METHOD) {
2468                 /* Not an INVITE that needs authentication */
2469                 return PJ_FALSE;
2470         }
2471
2472         inv = pjsip_dlg_get_inv_session(dlg);
2473         if (PJSIP_INV_STATE_CONFIRMED <= inv->state) {
2474                 /*
2475                  * We cannot handle reINVITE authentication at this
2476                  * time because the reINVITE transaction is still in
2477                  * progress.
2478                  */
2479                 ast_debug(1, "A reINVITE is being challenged.\n");
2480                 return PJ_FALSE;
2481         }
2482         ast_debug(1, "Initial INVITE is being challenged.\n");
2483
2484         session = inv->mod_data[session_module.id];
2485
2486         if (ast_sip_create_request_with_auth(&session->endpoint->outbound_auths, rdata,
2487                 tsx->last_tx, &tdata)) {
2488                 return PJ_FALSE;
2489         }
2490
2491         /*
2492          * Restart the outgoing initial INVITE transaction to deal
2493          * with authentication.
2494          */
2495         pjsip_inv_uac_restart(inv, PJ_FALSE);
2496
2497         ast_sip_session_send_request(session, tdata);
2498         return PJ_TRUE;
2499 }
2500
2501 static pjsip_module outbound_invite_auth_module = {
2502         .name = {"Outbound INVITE Auth", 20},
2503         .priority = PJSIP_MOD_PRIORITY_DIALOG_USAGE,
2504         .on_rx_response = outbound_invite_auth,
2505 };
2506
2507 /*!
2508  * \internal
2509  * \brief Setup outbound initial INVITE authentication.
2510  * \since 13.5.0
2511  *
2512  * \param dlg PJSIP dialog to attach outbound authentication.
2513  *
2514  * \retval 0 on success.
2515  * \retval -1 on error.
2516  */
2517 static int setup_outbound_invite_auth(pjsip_dialog *dlg)
2518 {
2519         pj_status_t status;
2520
2521         ++dlg->sess_count;
2522         status = pjsip_dlg_add_usage(dlg, &outbound_invite_auth_module, NULL);
2523         --dlg->sess_count;
2524
2525         return status != PJ_SUCCESS ? -1 : 0;
2526 }
2527
2528 struct ast_sip_session *ast_sip_session_create_outgoing(struct ast_sip_endpoint *endpoint,
2529         struct ast_sip_contact *contact, const char *location, const char *request_user,
2530         struct ast_stream_topology *req_topology)
2531 {
2532         const char *uri = NULL;
2533         RAII_VAR(struct ast_sip_aor *, found_aor, NULL, ao2_cleanup);
2534         RAII_VAR(struct ast_sip_contact *, found_contact, NULL, ao2_cleanup);
2535         pjsip_timer_setting timer;
2536         pjsip_dialog *dlg;
2537         struct pjsip_inv_session *inv_session;
2538         RAII_VAR(struct ast_sip_session *, session, NULL, ao2_cleanup);
2539         struct ast_sip_session *ret_session;
2540
2541         /* If no location has been provided use the AOR list from the endpoint itself */
2542         if (location || !contact) {
2543                 location = S_OR(location, endpoint->aors);
2544
2545                 ast_sip_location_retrieve_contact_and_aor_from_list_filtered(location, AST_SIP_CONTACT_FILTER_REACHABLE,
2546                         &found_aor, &found_contact);
2547                 if (!found_contact || ast_strlen_zero(found_contact->uri)) {
2548                         uri = location;
2549                 } else {
2550                         uri = found_contact->uri;
2551                 }
2552         } else {
2553                 uri = contact->uri;
2554         }
2555
2556         /* If we still have no URI to dial fail to create the session */
2557         if (ast_strlen_zero(uri)) {
2558                 ast_log(LOG_ERROR, "Endpoint '%s': No URI available.  Is endpoint registered?\n",
2559                         ast_sorcery_object_get_id(endpoint));
2560                 return NULL;
2561         }
2562
2563         if (!(dlg = ast_sip_create_dialog_uac(endpoint, uri, request_user))) {
2564                 return NULL;
2565         }
2566
2567         if (setup_outbound_invite_auth(dlg)) {
2568                 pjsip_dlg_terminate(dlg);
2569                 return NULL;
2570         }
2571
2572         if (pjsip_inv_create_uac(dlg, NULL, endpoint->extensions.flags, &inv_session) != PJ_SUCCESS) {
2573                 pjsip_dlg_terminate(dlg);
2574                 return NULL;
2575         }
2576 #if defined(HAVE_PJSIP_REPLACE_MEDIA_STREAM) || defined(PJMEDIA_SDP_NEG_ALLOW_MEDIA_CHANGE)
2577         inv_session->sdp_neg_flags = PJMEDIA_SDP_NEG_ALLOW_MEDIA_CHANGE;
2578 #endif
2579
2580         pjsip_timer_setting_default(&timer);
2581         timer.min_se = endpoint->extensions.timer.min_se;
2582         timer.sess_expires = endpoint->extensions.timer.sess_expires;
2583         pjsip_timer_init_session(inv_session, &timer);
2584
2585         session = ast_sip_session_alloc(endpoint, found_contact ? found_contact : contact,
2586                 inv_session, NULL);
2587         if (!session) {
2588                 pjsip_inv_terminate(inv_session, 500, PJ_FALSE);
2589                 return NULL;
2590         }
2591         session->aor = ao2_bump(found_aor);
2592         ast_party_id_copy(&session->id, &endpoint->id.self);
2593
2594         if (ast_stream_topology_get_count(req_topology) > 0) {
2595                 /* get joint caps between req_topology and endpoint topology */
2596                 int i;
2597
2598                 for (i = 0; i < ast_stream_topology_get_count(req_topology); ++i) {
2599                         struct ast_stream *req_stream;
2600                         struct ast_format_cap *req_cap;
2601                         struct ast_format_cap *joint_cap;
2602                         struct ast_stream *clone_stream;
2603
2604                         req_stream = ast_stream_topology_get_stream(req_topology, i);
2605
2606                         if (ast_stream_get_state(req_stream) == AST_STREAM_STATE_REMOVED) {
2607                                 continue;
2608                         }
2609
2610                         req_cap = ast_stream_get_formats(req_stream);
2611
2612                         joint_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
2613                         if (!joint_cap) {
2614                                 continue;
2615                         }
2616
2617                         ast_format_cap_get_compatible(req_cap, endpoint->media.codecs, joint_cap);
2618                         if (!ast_format_cap_count(joint_cap)) {
2619                                 ao2_ref(joint_cap, -1);
2620                                 continue;
2621                         }
2622
2623                         clone_stream = ast_stream_clone(req_stream, NULL);
2624                         if (!clone_stream) {
2625                                 ao2_ref(joint_cap, -1);
2626                                 continue;
2627                         }
2628
2629                         if (ast_stream_get_type(req_stream) == AST_MEDIA_TYPE_AUDIO) {
2630                                 /*
2631                                  * By appending codecs from the endpoint after compatible ones this
2632                                  * guarantees that priority is given to those while also allowing
2633                                  * translation to occur for non-compatible.
2634                                  */
2635                                 ast_format_cap_append_from_cap(joint_cap,
2636                                         endpoint->media.codecs, AST_MEDIA_TYPE_AUDIO);
2637                         }
2638
2639                         ast_stream_set_formats(clone_stream, joint_cap);
2640                         ao2_ref(joint_cap, -1);
2641
2642                         if (!session->pending_media_state->topology) {
2643                                 session->pending_media_state->topology = ast_stream_topology_alloc();
2644                                 if (!session->pending_media_state->topology) {
2645                                         pjsip_inv_terminate(inv_session, 500, PJ_FALSE);
2646                                         ao2_ref(session, -1);
2647                                         return NULL;
2648                                 }
2649                         }
2650
2651                         if (ast_stream_topology_append_stream(session->pending_media_state->topology, clone_stream) < 0) {
2652                                 ast_stream_free(clone_stream);
2653                                 continue;
2654                         }
2655                 }
2656         }
2657
2658         if (!session->pending_media_state->topology) {
2659                 /* Use the configured topology on the endpoint as the pending one */
2660                 session->pending_media_state->topology = ast_stream_topology_clone(endpoint->media.topology);
2661                 if (!session->pending_media_state->topology) {
2662                         pjsip_inv_terminate(inv_session, 500, PJ_FALSE);
2663                         ao2_ref(session, -1);
2664                         return NULL;
2665                 }
2666         }
2667
2668         if (pjsip_dlg_add_usage(dlg, &session_module, NULL) != PJ_SUCCESS) {
2669                 pjsip_inv_terminate(inv_session, 500, PJ_FALSE);
2670                 /* Since we are not notifying ourselves that the INVITE session is being terminated
2671                  * we need to manually drop its reference to session
2672                  */
2673                 ao2_ref(session, -1);
2674                 return NULL;
2675         }
2676
2677         /* Avoid unnecessary ref manipulation to return a session */
2678         ret_session = session;
2679         session = NULL;
2680         return ret_session;
2681 }
2682
2683 static int session_end(void *vsession);
2684 static int session_end_completion(void *vsession);
2685
2686 void ast_sip_session_terminate(struct ast_sip_session *session, int response)
2687 {
2688         pj_status_t status;
2689         pjsip_tx_data *packet = NULL;
2690
2691         if (session->defer_terminate) {
2692                 session->terminate_while_deferred = 1;
2693                 return;
2694         }
2695
2696         if (!response) {
2697                 response = 603;
2698         }
2699
2700         /* The media sessions need to exist for the lifetime of the underlying channel
2701          * to ensure that anything (such as bridge_native_rtp) has access to them as
2702          * appropriate. Since ast_sip_session_terminate is called by chan_pjsip and other
2703          * places when the session is to be terminated we terminate any existing
2704          * media sessions here.
2705          */
2706         ast_sip_session_media_stats_save(session, session->active_media_state);
2707         SWAP(session->active_media_state, session->pending_media_state);
2708         ast_sip_session_media_state_reset(session->pending_media_state);
2709
2710         switch (session->inv_session->state) {
2711         case PJSIP_INV_STATE_NULL:
2712                 if (!session->inv_session->invite_tsx) {
2713                         /*
2714                          * Normally, it's pjproject's transaction cleanup that ultimately causes the
2715                          * final session reference to be released but if both STATE and invite_tsx are NULL,
2716                          * we never created a transaction in the first place.  In this case, we need to
2717                          * do the cleanup ourselves.
2718                          */
2719                         /* Transfer the inv_session session reference to the session_end_task */
2720                         session->inv_session->mod_data[session_module.id] = NULL;
2721                         pjsip_inv_terminate(session->inv_session, response, PJ_TRUE);
2722                         session_end(session);
2723                         /*
2724                          * session_end_completion will cleanup the final session reference unless
2725                          * ast_sip_session_terminate's caller is holding one.
2726                          */
2727                         session_end_completion(session);
2728                 } else {
2729                         pjsip_inv_terminate(session->inv_session, response, PJ_TRUE);
2730                 }
2731                 break;
2732         case PJSIP_INV_STATE_CONFIRMED:
2733                 if (session->inv_session->invite_tsx) {
2734                         ast_debug(3, "Delay sending BYE to %s because of outstanding transaction...\n",
2735                                         ast_sorcery_object_get_id(session->endpoint));
2736                         /* If this is delayed the only thing that will happen is a BYE request so we don't
2737                          * actually need to store the response code for when it happens.
2738                          */
2739                         delay_request(session, NULL, NULL, NULL, 0, DELAYED_METHOD_BYE, NULL);
2740                         break;
2741                 }
2742                 /* Fall through */
2743         default:
2744                 status = pjsip_inv_end_session(session->inv_session, response, NULL, &packet);
2745                 if (status == PJ_SUCCESS && packet) {
2746                         struct ast_sip_session_delayed_request *delay;
2747
2748                         /* Flush any delayed requests so they cannot overlap this transaction. */
2749                         while ((delay = AST_LIST_REMOVE_HEAD(&session->delayed_requests, next))) {
2750                                 delayed_request_free(delay);
2751                         }
2752
2753                         if (packet->msg->type == PJSIP_RESPONSE_MSG) {
2754                                 ast_sip_session_send_response(session, packet);
2755                         } else {
2756                                 ast_sip_session_send_request(session, packet);
2757                         }
2758                 }
2759                 break;
2760         }
2761 }
2762
2763 static int session_termination_task(void *data)
2764 {
2765         struct ast_sip_session *session = data;
2766
2767         if (session->defer_terminate) {
2768                 session->defer_terminate = 0;
2769                 if (session->inv_session) {
2770                         ast_sip_session_terminate(session, 0);
2771                 }
2772         }
2773
2774         ao2_ref(session, -1);
2775         return 0;
2776 }
2777
2778 static void session_termination_cb(pj_timer_heap_t *timer_heap, struct pj_timer_entry *entry)
2779 {
2780         struct ast_sip_session *session = entry->user_data;
2781
2782         if (ast_sip_push_task(session->serializer, session_termination_task, session)) {
2783                 ao2_cleanup(session);
2784         }
2785 }
2786
2787 int ast_sip_session_defer_termination(struct ast_sip_session *session)
2788 {
2789         pj_time_val delay = { .sec = 60, };
2790         int res;
2791
2792         /* The session should not have an active deferred termination request. */
2793         ast_assert(!session->defer_terminate);
2794
2795         session->defer_terminate = 1;
2796
2797         session->defer_end = 1;
2798         session->ended_while_deferred = 0;
2799
2800         ao2_ref(session, +1);
2801         pj_timer_entry_init(&session->scheduled_termination, 0, session, session_termination_cb);
2802
2803         res = (pjsip_endpt_schedule_timer(ast_sip_get_pjsip_endpoint(),
2804                 &session->scheduled_termination, &delay) != PJ_SUCCESS) ? -1 : 0;
2805         if (res) {
2806                 session->defer_terminate = 0;
2807                 ao2_ref(session, -1);
2808         }
2809         return res;
2810 }
2811
2812 /*!
2813  * \internal
2814  * \brief Stop the defer termination timer if it is still running.
2815  * \since 13.5.0
2816  *
2817  * \param session Which session to stop the timer.
2818  *
2819  * \return Nothing
2820  */
2821 static void sip_session_defer_termination_stop_timer(struct ast_sip_session *session)
2822 {
2823         if (pj_timer_heap_cancel_if_active(pjsip_endpt_get_timer_heap(ast_sip_get_pjsip_endpoint()),
2824                 &session->scheduled_termination, session->scheduled_termination.id)) {
2825                 ao2_ref(session, -1);
2826         }
2827 }
2828
2829 void ast_sip_session_defer_termination_cancel(struct ast_sip_session *session)
2830 {
2831         if (!session->defer_terminate) {
2832                 /* Already canceled or timer fired. */
2833                 return;
2834         }
2835
2836         session->defer_terminate = 0;
2837
2838         if (session->terminate_while_deferred) {
2839                 /* Complete the termination started by the upper layer. */
2840                 ast_sip_session_terminate(session, 0);
2841         }
2842
2843         /* Stop the termination timer if it is still running. */
2844         sip_session_defer_termination_stop_timer(session);
2845 }
2846
2847 void ast_sip_session_end_if_deferred(struct ast_sip_session *session)
2848 {
2849         if (!session->defer_end) {
2850                 return;
2851         }
2852
2853         session->defer_end = 0;
2854
2855         if (session->ended_while_deferred) {
2856                 /* Complete the session end started by the remote hangup. */
2857                 ast_debug(3, "Ending session (%p) after being deferred\n", session);
2858                 session->ended_while_deferred = 0;
2859                 session_end(session);
2860         }
2861 }
2862
2863 struct ast_sip_session *ast_sip_dialog_get_session(pjsip_dialog *dlg)
2864 {
2865         pjsip_inv_session *inv_session = pjsip_dlg_get_inv_session(dlg);
2866         struct ast_sip_session *session;
2867
2868         if (!inv_session ||
2869                 !(session = inv_session->mod_data[session_module.id])) {
2870                 return NULL;
2871         }
2872
2873         ao2_ref(session, +1);
2874
2875         return session;
2876 }
2877
2878 enum sip_get_destination_result {
2879         /*! The extension was successfully found */
2880         SIP_GET_DEST_EXTEN_FOUND,
2881         /*! The extension specified in the RURI was not found */
2882         SIP_GET_DEST_EXTEN_NOT_FOUND,
2883         /*! The extension specified in the RURI was a partial match */
2884         SIP_GET_DEST_EXTEN_PARTIAL,
2885         /*! The RURI is of an unsupported scheme */
2886         SIP_GET_DEST_UNSUPPORTED_URI,
2887 };
2888
2889 /*!
2890  * \brief Determine where in the dialplan a call should go
2891  *
2892  * This uses the username in the request URI to try to match
2893  * an extension in the endpoint's configured context in order
2894  * to route the call.
2895  *
2896  * \param session The inbound SIP session
2897  * \param rdata The SIP INVITE
2898  */
2899 static enum sip_get_destination_result get_destination(struct ast_sip_session *session, pjsip_rx_data *rdata)
2900 {
2901         pjsip_uri *ruri = rdata->msg_info.msg->line.req.uri;
2902         pjsip_sip_uri *sip_ruri;
2903         struct ast_features_pickup_config *pickup_cfg;
2904         const char *pickupexten;
2905
2906         if (!PJSIP_URI_SCHEME_IS_SIP(ruri) && !PJSIP_URI_SCHEME_IS_SIPS(ruri)) {
2907                 return SIP_GET_DEST_UNSUPPORTED_URI;
2908         }
2909
2910         sip_ruri = pjsip_uri_get_uri(ruri);
2911         ast_copy_pj_str(session->exten, &sip_ruri->user, sizeof(session->exten));
2912
2913         /*
2914          * We may want to match in the dialplan without any user
2915          * options getting in the way.
2916          */
2917         AST_SIP_USER_OPTIONS_TRUNCATE_CHECK(session->exten);
2918
2919         pickup_cfg = ast_get_chan_features_pickup_config(session->channel);
2920         if (!pickup_cfg) {
2921                 ast_log(LOG_ERROR, "Unable to retrieve pickup configuration options. Unable to detect call pickup extension\n");
2922                 pickupexten = "";
2923         } else {
2924                 pickupexten = ast_strdupa(pickup_cfg->pickupexten);
2925                 ao2_ref(pickup_cfg, -1);
2926         }
2927
2928         if (!strcmp(session->exten, pickupexten) ||
2929                 ast_exists_extension(NULL, session->endpoint->context, session->exten, 1, NULL)) {
2930                 size_t size = pj_strlen(&sip_ruri->host) + 1;
2931                 char *domain = ast_alloca(size);
2932
2933                 ast_copy_pj_str(domain, &sip_ruri->host, size);
2934                 pbx_builtin_setvar_helper(session->channel, "SIPDOMAIN", domain);
2935
2936                 /*
2937                  * Save off the INVITE Request-URI in case it is
2938                  * needed: CHANNEL(pjsip,request_uri)
2939                  */
2940                 session->request_uri = pjsip_uri_clone(session->inv_session->pool, ruri);
2941
2942                 return SIP_GET_DEST_EXTEN_FOUND;
2943         }
2944
2945         /*
2946          * Check for partial match via overlap dialling (if enabled)
2947          */
2948         if (session->endpoint->allow_overlap && (
2949                 !strncmp(session->exten, pickupexten, strlen(session->exten)) ||
2950                 ast_canmatch_extension(NULL, session->endpoint->context, session->exten, 1, NULL))) {
2951                 /* Overlap partial match */
2952                 return SIP_GET_DEST_EXTEN_PARTIAL;
2953         }
2954
2955         return SIP_GET_DEST_EXTEN_NOT_FOUND;
2956 }
2957
2958 static pjsip_inv_session *pre_session_setup(pjsip_rx_data *rdata, const struct ast_sip_endpoint *endpoint)
2959 {
2960         pjsip_tx_data *tdata;
2961         pjsip_dialog *dlg;
2962         pjsip_inv_session *inv_session;
2963         unsigned int options = endpoint->extensions.flags;
2964         pj_status_t dlg_status;
2965
2966         if (pjsip_inv_verify_request(rdata, &options, NULL, NULL, ast_sip_get_pjsip_endpoint(), &tdata) != PJ_SUCCESS) {
2967                 if (tdata) {
2968                         if (pjsip_endpt_send_response2(ast_sip_get_pjsip_endpoint(), rdata, tdata, NULL, NULL) != PJ_SUCCESS) {
2969                                 pjsip_tx_data_dec_ref(tdata);
2970                         }
2971                 } else {
2972                         pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 500, NULL, NULL, NULL);
2973                 }
2974                 return NULL;
2975         }
2976         dlg = ast_sip_create_dialog_uas(endpoint, rdata, &dlg_status);
2977         if (!dlg) {
2978                 if (dlg_status != PJ_EEXISTS) {
2979                         pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 500, NULL, NULL, NULL);
2980                 }
2981                 return NULL;
2982         }
2983         if (pjsip_inv_create_uas(dlg, rdata, NULL, options, &inv_session) != PJ_SUCCESS) {
2984                 pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 500, NULL, NULL, NULL);
2985                 pjsip_dlg_terminate(dlg);
2986                 return NULL;
2987         }
2988
2989 #if defined(HAVE_PJSIP_REPLACE_MEDIA_STREAM) || defined(PJMEDIA_SDP_NEG_ALLOW_MEDIA_CHANGE)
2990         inv_session->sdp_neg_flags = PJMEDIA_SDP_NEG_ALLOW_MEDIA_CHANGE;
2991 #endif
2992         if (pjsip_dlg_add_usage(dlg, &session_module, NULL) != PJ_SUCCESS) {
2993                 if (pjsip_inv_initial_answer(inv_session, rdata, 500, NULL, NULL, &tdata) != PJ_SUCCESS) {
2994                         pjsip_inv_terminate(inv_session, 500, PJ_FALSE);
2995                 }
2996                 pjsip_inv_send_msg(inv_session, tdata);
2997                 return NULL;
2998         }
2999         return inv_session;
3000 }
3001
3002 struct new_invite {
3003         /*! \brief Session created for the new INVITE */
3004         struct ast_sip_session *session;
3005
3006         /*! \brief INVITE request itself */
3007         pjsip_rx_data *rdata;
3008 };
3009
3010 static int new_invite(struct new_invite *invite)
3011 {
3012         pjsip_tx_data *tdata = NULL;
3013         pjsip_timer_setting timer;
3014         pjsip_rdata_sdp_info *sdp_info;
3015         pjmedia_sdp_session *local = NULL;
3016         char buffer[AST_SOCKADDR_BUFLEN];
3017
3018         /* From this point on, any calls to pjsip_inv_terminate have the last argument as PJ_TRUE
3019          * so that we will be notified so we can destroy the session properly
3020          */
3021
3022         if (invite->session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
3023                 ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
3024                         invite->session->inv_session->cause,
3025                         pjsip_get_status_text(invite->session->inv_session->cause)->ptr);
3026 #ifdef HAVE_PJSIP_INV_SESSION_REF
3027                 pjsip_inv_dec_ref(invite->session->inv_session);
3028 #endif
3029                 return -1;
3030         }
3031
3032         switch (get_destination(invite->session, invite->rdata)) {
3033         case SIP_GET_DEST_EXTEN_FOUND:
3034                 /* Things worked. Keep going */
3035                 break;
3036         case SIP_GET_DEST_UNSUPPORTED_URI:
3037                 if (pjsip_inv_initial_answer(invite->session->inv_session, invite->rdata, 416, NULL, NULL, &tdata) == PJ_SUCCESS) {
3038                         ast_sip_session_send_response(invite->session, tdata);
3039                 } else  {
3040                         pjsip_inv_terminate(invite->session->inv_session, 416, PJ_TRUE);
3041                 }
3042                 goto end;
3043         case SIP_GET_DEST_EXTEN_PARTIAL:
3044                 ast_debug(1, "Call from '%s' (%s:%s) to extension '%s' - partial match\n",
3045                         ast_sorcery_object_get_id(invite->session->endpoint),
3046                         invite->rdata->tp_info.transport->type_name,
3047                         pj_sockaddr_print(&invite->rdata->pkt_info.src_addr, buffer, sizeof(buffer), 3),
3048                         invite->session->exten);
3049
3050                 if (pjsip_inv_initial_answer(invite->session->inv_session, invite->rdata, 484, NULL, NULL, &tdata) == PJ_SUCCESS) {
3051                         ast_sip_session_send_response(invite->session, tdata);
3052                 } else  {
3053                         pjsip_inv_terminate(invite->session->inv_session, 484, PJ_TRUE);
3054                 }
3055                 goto end;
3056         case SIP_GET_DEST_EXTEN_NOT_FOUND:
3057         default:
3058                 ast_log(LOG_NOTICE, "Call from '%s' (%s:%s) to extension '%s' rejected because extension not found in context '%s'.\n",
3059                         ast_sorcery_object_get_id(invite->session->endpoint),
3060                         invite->rdata->tp_info.transport->type_name,
3061                         pj_sockaddr_print(&invite->rdata->pkt_info.src_addr, buffer, sizeof(buffer), 3),
3062                         invite->session->exten,
3063                         invite->session->endpoint->context);
3064
3065                 if (pjsip_inv_initial_answer(invite->session->inv_session, invite->rdata, 404, NULL, NULL, &tdata) == PJ_SUCCESS) {
3066                         ast_sip_session_send_response(invite->session, tdata);
3067                 } else  {
3068                         pjsip_inv_terminate(invite->session->inv_session, 404, PJ_TRUE);
3069                 }
3070                 goto end;
3071         };
3072
3073         pjsip_timer_setting_default(&timer);
3074         timer.min_se = invite->session->endpoint->extensions.timer.min_se;
3075         timer.sess_expires = invite->session->endpoint->extensions.timer.sess_expires;
3076         pjsip_timer_init_session(invite->session->inv_session, &timer);
3077
3078         /*
3079          * At this point, we've verified what we can that won't take awhile,
3080          * so let's go ahead and send a 100 Trying out to stop any
3081          * retransmissions.
3082          */
3083         if (pjsip_inv_initial_answer(invite->session->inv_session, invite->rdata, 100, NULL, NULL, &tdata) != PJ_SUCCESS) {
3084                 pjsip_inv_terminate(invite->session->inv_session, 500, PJ_TRUE);
3085                 goto end;
3086         }
3087         ast_sip_session_send_response(invite->session, tdata);
3088
3089         sdp_info = pjsip_rdata_get_sdp_info(invite->rdata);
3090         if (sdp_info && (sdp_info->sdp_err == PJ_SUCCESS) && sdp_info->sdp) {
3091                 if (handle_incoming_sdp(invite->session, sdp_info->sdp)) {
3092                         tdata = NULL;
3093                         if (pjsip_inv_end_session(invite->session->inv_session, 488, NULL, &tdata) == PJ_SUCCESS
3094                                 && tdata) {
3095                                 ast_sip_session_send_response(invite->session, tdata);
3096                         }
3097                         goto end;
3098                 }
3099                 /* We are creating a local SDP which is an answer to their offer */
3100                 local = create_local_sdp(invite->session->inv_session, invite->session, sdp_info->sdp);
3101         } else {
3102                 /* We are creating a local SDP which is an offer */
3103                 local = create_local_sdp(invite->session->inv_session, invite->session, NULL);
3104         }
3105
3106         /* If we were unable to create a local SDP terminate the session early, it won't go anywhere */
3107         if (!local) {
3108                 tdata = NULL;
3109                 if (pjsip_inv_end_session(invite->session->inv_session, 500, NULL, &tdata) == PJ_SUCCESS
3110                         && tdata) {
3111                         ast_sip_session_send_response(invite->session, tdata);
3112                 }
3113                 goto end;
3114         }
3115
3116         pjsip_inv_set_local_sdp(invite->session->inv_session, local);
3117         pjmedia_sdp_neg_set_prefer_remote_codec_order(invite->session->inv_session->neg, PJ_FALSE);
3118 #ifdef PJMEDIA_SDP_NEG_ANSWER_MULTIPLE_CODECS
3119         if (!invite->session->endpoint->preferred_codec_only) {
3120                 pjmedia_sdp_neg_set_answer_multiple_codecs(invite->session->inv_session->neg, PJ_TRUE);
3121         }
3122 #endif
3123
3124         handle_incoming_request(invite->session, invite->rdata);
3125
3126 end:
3127 #ifdef HAVE_PJSIP_INV_SESSION_REF
3128         pjsip_inv_dec_ref(invite->session->inv_session);
3129 #endif
3130         return 0;
3131 }
3132
3133 static void handle_new_invite_request(pjsip_rx_data *rdata)
3134 {
3135         RAII_VAR(struct ast_sip_endpoint *, endpoint,
3136                         ast_pjsip_rdata_get_endpoint(rdata), ao2_cleanup);
3137         pjsip_tx_data *tdata = NULL;
3138         pjsip_inv_session *inv_session = NULL;
3139         struct ast_sip_session *session;
3140         struct new_invite invite;
3141
3142         ast_assert(endpoint != NULL);
3143
3144         inv_session = pre_session_setup(rdata, endpoint);
3145         if (!inv_session) {
3146                 /* pre_session_setup() returns a response on failure */
3147                 return;
3148         }
3149
3150 #ifdef HAVE_PJSIP_INV_SESSION_REF
3151         if (pjsip_inv_add_ref(inv_session) != PJ_SUCCESS) {
3152                 ast_log(LOG_ERROR, "Can't increase the session reference counter\n");
3153                 if (inv_session->state != PJSIP_INV_STATE_DISCONNECTED) {
3154                         if (pjsip_inv_initial_answer(inv_session, rdata, 500, NULL, NULL, &tdata) == PJ_SUCCESS) {
3155                                 pjsip_inv_terminate(inv_session, 500, PJ_FALSE);
3156                         } else {
3157                                 pjsip_inv_send_msg(inv_session, tdata);
3158                         }
3159                 }
3160                 return;
3161         }
3162 #endif
3163
3164         session = ast_sip_session_alloc(endpoint, NULL, inv_session, rdata);
3165         if (!session) {
3166                 if (pjsip_inv_initial_answer(inv_session, rdata, 500, NULL, NULL, &tdata) == PJ_SUCCESS) {
3167                         pjsip_inv_terminate(inv_session, 500, PJ_FALSE);
3168                 } else {
3169                         pjsip_inv_send_msg(inv_session, tdata);
3170                 }
3171 #ifdef HAVE_PJSIP_INV_SESSION_REF
3172                 pjsip_inv_dec_ref(inv_session);
3173 #endif
3174                 return;
3175         }
3176
3177         /*
3178          * The current thread is supposed be the session serializer to prevent
3179          * any initial INVITE retransmissions from trying to setup the same
3180          * call again.
3181          */
3182         ast_assert(ast_taskprocessor_is_task(session->serializer));
3183
3184         invite.session = session;
3185         invite.rdata = rdata;
3186         new_invite(&invite);
3187
3188         ao2_ref(session, -1);
3189 }
3190
3191 static pj_bool_t does_method_match(const pj_str_t *message_method, const char *supplement_method)
3192 {
3193         pj_str_t method;
3194
3195         if (ast_strlen_zero(supplement_method)) {
3196                 return PJ_TRUE;
3197         }
3198
3199         pj_cstr(&method, supplement_method);
3200
3201         return pj_stristr(&method, message_method) ? PJ_TRUE : PJ_FALSE;
3202 }
3203
3204 static pj_bool_t has_supplement(const struct ast_sip_session *session, const pjsip_rx_data *rdata)
3205 {
3206         struct ast_sip_session_supplement *supplement;
3207         struct pjsip_method *method = &rdata->msg_info.msg->line.req.method;
3208
3209         if (!session) {
3210                 return PJ_FALSE;
3211         }
3212
3213         AST_LIST_TRAVERSE(&session->supplements, supplement, next) {
3214                 if (does_method_match(&method->name, supplement->method)) {
3215                         return PJ_TRUE;
3216                 }
3217         }
3218         return PJ_FALSE;
3219 }
3220 /*!
3221  * \brief Called when a new SIP request comes into PJSIP
3222  *
3223  * This function is called under two circumstances
3224  * 1) An out-of-dialog request is received by PJSIP
3225  * 2) An in-dialog request that the inv_session layer does not
3226  *    handle is received (such as an in-dialog INFO)
3227  *
3228  * Except for INVITEs, there is very little we actually do in this function
3229  * 1) For requests we don't handle, we return PJ_FALSE
3230  * 2) For new INVITEs, handle them now to prevent retransmissions from
3231  *    trying to setup the same call again.
3232  * 3) For in-dialog requests we handle, we process them in the
3233  *    .on_state_changed = session_inv_on_state_changed or
3234  *    .on_tsx_state_changed = session_inv_on_tsx_state_changed
3235  *    callbacks instead.
3236  */
3237 static pj_bool_t session_on_rx_request(pjsip_rx_data *rdata)
3238 {
3239         pj_status_t handled = PJ_FALSE;
3240         pjsip_dialog *dlg = pjsip_rdata_get_dlg(rdata);
3241         pjsip_inv_session *inv_session;
3242
3243         switch (rdata->msg_info.msg->line.req.method.id) {
3244         case PJSIP_INVITE_METHOD:
3245                 if (dlg) {
3246                         ast_log(LOG_WARNING, "on_rx_request called for INVITE in mid-dialog?\n");
3247                         break;
3248                 }
3249                 handled = PJ_TRUE;
3250                 handle_new_invite_request(rdata);
3251                 break;
3252         default:
3253                 /* Handle other in-dialog methods if their supplements have been registered */
3254                 handled = dlg && (inv_session = pjsip_dlg_get_inv_session(dlg)) &&
3255                         has_supplement(inv_session->mod_data[session_module.id], rdata);
3256                 break;
3257         }
3258
3259         return handled;
3260 }
3261
3262 static void resend_reinvite(pj_timer_heap_t *timer, pj_timer_entry *entry)
3263 {
3264         struct ast_sip_session *session = entry->user_data;
3265
3266         ast_debug(3, "Endpoint '%s(%s)' re-INVITE collision timer expired.\n",
3267                 ast_sorcery_object_get_id(session->endpoint),
3268                 session->channel ? ast_channel_name(session->channel) : "");
3269
3270         if (AST_LIST_EMPTY(&session->delayed_requests)) {
3271                 /* No delayed request pending, so just return */
3272                 ao2_ref(session, -1);
3273                 return;
3274         }
3275         if (ast_sip_push_task(session->serializer, invite_collision_timeout, session)) {
3276                 /*
3277                  * Uh oh.  We now have nothing in the foreseeable future
3278                  * to trigger sending the delayed requests.
3279                  */
3280                 ao2_ref(session, -1);
3281         }
3282 }
3283
3284 static void reschedule_reinvite(struct ast_sip_session *session, ast_sip_session_response_cb on_response)
3285 {
3286         pjsip_inv_session *inv = session->inv_session;
3287         pj_time_val tv;
3288
3289         ast_debug(3, "Endpoint '%s(%s)' re-INVITE collision.\n",
3290                 ast_sorcery_object_get_id(session->endpoint),
3291                 session->channel ? ast_channel_name(session->channel) : "");
3292         if (delay_request(session, NULL, NULL, on_response, 1, DELAYED_METHOD_INVITE, NULL)) {
3293                 return;
3294         }
3295         if (pj_timer_entry_running(&session->rescheduled_reinvite)) {
3296                 /* Timer already running.  Something weird is going on. */
3297                 ast_debug(1, "Endpoint '%s(%s)' re-INVITE collision while timer running!!!\n",
3298                         ast_sorcery_object_get_id(session->endpoint),
3299                         session->channel ? ast_channel_name(session->channel) : "");
3300                 return;
3301         }
3302
3303         tv.sec = 0;
3304         if (inv->role == PJSIP_ROLE_UAC) {
3305                 tv.msec = 2100 + ast_random() % 2000;
3306         } else {
3307                 tv.msec = ast_random() % 2000;
3308         }
3309         pj_timer_entry_init(&session->rescheduled_reinvite, 0, session, resend_reinvite);
3310
3311         ao2_ref(session, +1);
3312         if (pjsip_endpt_schedule_timer(ast_sip_get_pjsip_endpoint(),
3313                 &session->rescheduled_reinvite, &tv) != PJ_SUCCESS) {
3314                 ao2_ref(session, -1);
3315         }
3316 }
3317
3318 static void __print_debug_details(const char *function, pjsip_inv_session *inv, pjsip_transaction *tsx, pjsip_event *e)
3319 {
3320         int id = session_module.id;
3321         struct ast_sip_session *session = NULL;
3322
3323         if (!DEBUG_ATLEAST(5)) {
3324                 /* Debug not spamy enough */
3325                 return;
3326         }
3327
3328         ast_log(LOG_DEBUG, "Function %s called on event %s\n",
3329                 function, pjsip_event_str(e->type));
3330         if (!inv) {
3331                 ast_log(LOG_DEBUG, "Transaction %p does not belong to an inv_session?\n", tsx);
3332                 ast_log(LOG_DEBUG, "The transaction state is %s\n",
3333                         pjsip_tsx_state_str(tsx->state));
3334                 return;
3335         }
3336         if (id > -1) {
3337                 session = inv->mod_data[session_module.id];
3338         }
3339         if (!session) {
3340                 ast_log(LOG_DEBUG, "inv_session %p has no ast session\n", inv);
3341         } else {
3342                 ast_log(LOG_DEBUG, "The state change pertains to the endpoint '%s(%s)'\n",
3343                         ast_sorcery_object_get_id(session->endpoint),
3344                         session->channel ? ast_channel_name(session->channel) : "");
3345         }
3346         if (inv->invite_tsx) {
3347                 ast_log(LOG_DEBUG, "The inv session still has an invite_tsx (%p)\n",
3348                         inv->invite_tsx);
3349         } else {
3350                 ast_log(LOG_DEBUG, "The inv session does NOT have an invite_tsx\n");
3351         }
3352         if (tsx) {
3353                 ast_log(LOG_DEBUG, "The %s %.*s transaction involved in this state change is %p\n",
3354                         pjsip_role_name(tsx->role),
3355                         (int) pj_strlen(&tsx->method.name), pj_strbuf(&tsx->method.name),
3356                         tsx);
3357                 ast_log(LOG_DEBUG, "The current transaction state is %s\n",
3358                         pjsip_tsx_state_str(tsx->state));
3359                 ast_log(LOG_DEBUG, "The transaction state change event is %s\n",
3360                         pjsip_event_str(e->body.tsx_state.type));
3361         } else {
3362                 ast_log(LOG_DEBUG, "There is no transaction involved in this state change\n");
3363         }
3364         ast_log(LOG_DEBUG, "The current inv state is %s\n", pjsip_inv_state_name(inv->state));
3365 }
3366
3367 #define print_debug_details(inv, tsx, e) __print_debug_details(__PRETTY_FUNCTION__, (inv), (tsx), (e))
3368
3369 static void handle_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
3370 {
3371         struct ast_sip_session_supplement *supplement;
3372         struct pjsip_request_line req = rdata->msg_info.msg->line.req;
3373
3374         ast_debug(3, "Method is %.*s\n", (int) pj_strlen(&req.method.name), pj_strbuf(&req.method.name));
3375         AST_LIST_TRAVERSE(&session->supplements, supplement, next) {
3376                 if (supplement->incoming_request && does_method_match(&req.method.name, supplement->method)) {
3377                         if (supplement->incoming_request(session, rdata)) {
3378                                 break;
3379                         }
3380                 }
3381         }
3382 }
3383
3384 static void handle_session_begin(struct ast_sip_session *session)
3385 {
3386         struct ast_sip_session_supplement *iter;
3387
3388         AST_LIST_TRAVERSE(&session->supplements, iter, next) {
3389                 if (iter->session_begin) {
3390                         iter->session_begin(session);
3391                 }
3392         }
3393 }
3394
3395 static void handle_session_destroy(struct ast_sip_session *session)
3396 {
3397         struct ast_sip_session_supplement *iter;
3398
3399         AST_LIST_TRAVERSE(&session->supplements, iter, next) {
3400                 if (iter->session_destroy) {
3401                         iter->session_destroy(session);
3402                 }
3403         }
3404 }
3405
3406 static void handle_session_end(struct ast_sip_session *session)
3407 {
3408         struct ast_sip_session_supplement *iter;
3409
3410         /* Session is dead.  Notify the supplements. */
3411         AST_LIST_TRAVERSE(&session->supplements, iter, next) {
3412                 if (iter->session_end) {
3413                         iter->session_end(session);
3414                 }
3415         }
3416 }
3417
3418 static void handle_incoming_response(struct ast_sip_session *session, pjsip_rx_data *rdata,
3419                 enum ast_sip_session_response_priority response_priority)
3420 {
3421         struct ast_sip_session_supplement *supplement;
3422         struct pjsip_status_line status = rdata->msg_info.msg->line.status;
3423
3424         ast_debug(3, "Response is %d %.*s\n", status.code, (int) pj_strlen(&status.reason),
3425                         pj_strbuf(&status.reason));
3426
3427         AST_LIST_TRAVERSE(&session->supplements, supplement, next) {
3428                 if (!(supplement->response_priority & response_priority)) {
3429                         continue;
3430                 }
3431                 if (supplement->incoming_response && does_method_match(&rdata->msg_info.cseq->method.name, supplement->method)) {
3432                         supplement->incoming_response(session, rdata);
3433                 }
3434         }
3435 }
3436
3437 static int handle_incoming(struct ast_sip_session *session, pjsip_rx_data *rdata,
3438                 enum ast_sip_session_response_priority response_priority)
3439 {
3440         ast_debug(3, "Received %s\n", rdata->msg_info.msg->type == PJSIP_REQUEST_MSG ?
3441                         "request" : "response");
3442
3443         if (rdata->msg_info.msg->type == PJSIP_REQUEST_MSG) {
3444                 handle_incoming_request(session, rdata);
3445         } else {
3446                 handle_incoming_response(session, rdata, response_priority);
3447         }
3448
3449         return 0;
3450 }
3451
3452 static void handle_outgoing_request(struct ast_sip_session *session, pjsip_tx_data *tdata)
3453 {
3454         struct ast_sip_session_supplement *supplement;
3455         struct pjsip_request_line req = tdata->msg->line.req;
3456
3457         ast_debug(3, "Method is %.*s\n", (int) pj_strlen(&req.method.name), pj_strbuf(&req.method.name));
3458
3459         ast_sip_message_apply_transport(session->endpoint->transport, tdata);
3460
3461         AST_LIST_TRAVERSE(&session->supplements, supplement, next) {
3462                 if (supplement->outgoing_request && does_method_match(&req.method.name, supplement->method)) {
3463                         supplement->outgoing_request(session, tdata);
3464                 }
3465         }
3466 }
3467
3468 static void handle_outgoing_response(struct ast_sip_session *session, pjsip_tx_data *tdata)
3469 {
3470         struct ast_sip_session_supplement *supplement;
3471         struct pjsip_status_line status = tdata->msg->line.status;
3472         pjsip_cseq_hdr *cseq = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CSEQ, NULL);
3473
3474         if (!cseq) {
3475                 ast_log(LOG_ERROR, "Cannot send response due to missing sequence header");
3476                 return;
3477         }
3478
3479         ast_debug(3, "Method is %.*s, Response is %d %.*s\n", (int) pj_strlen(&cseq->method.name),
3480                 pj_strbuf(&cseq->method.name), status.code, (int) pj_strlen(&status.reason),
3481                 pj_strbuf(&status.reason));
3482
3483         ast_sip_message_apply_transport(session->endpoint->transport, tdata);
3484
3485         AST_LIST_TRAVERSE(&session->supplements, supplement, next) {
3486                 if (supplement->outgoing_response && does_method_match(&cseq->method.name, supplement->method)) {
3487                         supplement->outgoing_response(session, tdata);
3488                 }
3489         }
3490 }
3491
3492 static int session_end(void *vsession)
3493 {
3494         struct ast_sip_session *session = vsession;
3495
3496         /* Stop the scheduled termination */
3497         sip_session_defer_termination_stop_timer(session);
3498
3499         /* Session is dead.  Notify the supplements. */
3500         handle_session_end(session);
3501
3502         return 0;
3503 }
3504
3505 /*!
3506  * \internal
3507  * \brief Complete ending session activities.
3508  * \since 13.5.0
3509  *
3510  * \param vsession Which session to complete stopping.
3511  *
3512  * \retval 0 on success.
3513  * \retval -1 on error.
3514  */
3515 static int session_end_completion(void *vsession)
3516 {
3517         struct ast_sip_session *session = vsession;
3518
3519         ast_sip_dialog_set_serializer(session->inv_session->dlg, NULL);
3520         ast_sip_dialog_set_endpoint(session->inv_session->dlg, NULL);
3521
3522         /* Now we can release the ref that was held by session->inv_session */
3523         ao2_cleanup(session);
3524         return 0;
3525 }
3526
3527 static int check_request_status(pjsip_inv_session *inv, pjsip_event *e)
3528 {
3529         struct ast_sip_session *session = inv->mod_data[session_module.id];
3530         pjsip_transaction *tsx = e->body.tsx_state.tsx;
3531
3532         if (tsx->status_code != 503 && tsx->status_code != 408) {
3533                 return 0;
3534         }
3535
3536         if (!ast_sip_failover_request(tsx->last_tx)) {
3537                 return 0;
3538         }
3539
3540         pjsip_inv_uac_restart(inv, PJ_FALSE);
3541         /*
3542          * Bump the ref since it will be on a new transaction and
3543          * we don't want it to go away along with the old transaction.
3544          */
3545         pjsip_tx_data_add_ref(tsx->last_tx);
3546         ast_sip_session_send_request(session, tsx->last_tx);
3547         return 1;
3548 }
3549
3550 static void handle_incoming_before_media(pjsip_inv_session *inv,
3551         struct ast_sip_session *session, pjsip_rx_data *rdata)
3552 {
3553         pjsip_msg *msg;
3554
3555         handle_incoming(session, rdata, AST_SIP_SESSION_BEFORE_MEDIA);
3556         msg = rdata->msg_info.msg;
3557         if (msg->type == PJSIP_REQUEST_MSG
3558                 && msg->line.req.method.id == PJSIP_ACK_METHOD
3559                 && pjmedia_sdp_neg_get_state(inv->neg) != PJMEDIA_SDP_NEG_STATE_DONE) {
3560                 pjsip_tx_data *tdata;
3561
3562                 /*
3563                  * SDP negotiation failed on an incoming call that delayed
3564                  * negotiation and then gave us an invalid SDP answer.  We
3565                  * need to send a BYE to end the call because of the invalid
3566                  * SDP answer.
3567                  */
3568                 ast_debug(1,
3569                         "Endpoint '%s(%s)': Ending session due to incomplete SDP negotiation.  %s\n",
3570                         ast_sorcery_object_get_id(session->endpoint),
3571                         session->channel ? ast_channel_name(session->channel) : "",
3572                         pjsip_rx_data_get_info(rdata));
3573                 if (pjsip_inv_end_session(inv, 400, NULL, &tdata) == PJ_SUCCESS
3574                         && tdata) {
3575                         ast_sip_session_send_request(session, tdata);
3576                 }
3577         }
3578 }
3579
3580 static void session_inv_on_state_changed(pjsip_inv_session *inv, pjsip_event *e)
3581 {
3582         struct ast_sip_session *session;
3583         pjsip_event_id_e type;
3584
3585         if (ast_shutdown_final()) {
3586                 return;
3587         }
3588
3589         if (e) {
3590                 print_debug_details(inv, NULL, e);
3591                 type = e->type;
3592         } else {
3593                 type = PJSIP_EVENT_UNKNOWN;
3594         }
3595
3596         session = inv->mod_data[session_module.id];
3597         if (!session) {
3598                 return;
3599         }
3600
3601         switch(type) {
3602         case PJSIP_EVENT_TX_MSG:
3603                 break;
3604         case PJSIP_EVENT_RX_MSG:
3605                 handle_incoming_before_media(inv, session, e->body.rx_msg.rdata);
3606                 break;
3607         case PJSIP_EVENT_TSX_STATE:
3608                 ast_debug(3, "Source of transaction state change is %s\n", pjsip_event_str(e->body.tsx_state.type));
3609                 /* Transaction state changes are prompted by some other underlying event. */
3610                 switch(e->body.tsx_state.type) {
3611                 case PJSIP_EVENT_TX_MSG:
3612                         break;
3613                 case PJSIP_EVENT_RX_MSG:
3614                         if (!check_request_status(inv, e)) {
3615                                 handle_incoming_before_media(inv, session, e->body.tsx_state.src.rdata);
3616                         }
3617                         break;
3618                 case PJSIP_EVENT_TRANSPORT_ERROR:
3619                 case PJSIP_EVENT_TIMER:
3620                         /*
3621                          * Check the request status on transport error or timeout. A transport
3622                          * error can occur when a TCP socket closes and that can be the result
3623                          * of a 503. Also we may need to failover on a timeout (408).
3624                          */
3625                         check_request_status(inv, e);
3626                         break;
3627                 case PJSIP_EVENT_USER:
3628                 case PJSIP_EVENT_UNKNOWN:
3629                 case PJSIP_EVENT_TSX_STATE:
3630                         /* Inception? */
3631                         break;
3632                 }
3633                 break;
3634         case PJSIP_EVENT_TRANSPORT_ERROR:
3635         case PJSIP_EVENT_TIMER:
3636         case PJSIP_EVENT_UNKNOWN:
3637         case PJSIP_EVENT_USER:
3638         default:
3639                 break;
3640         }
3641
3642         if (inv->state == PJSIP_INV_STATE_DISCONNECTED) {
3643                 if (session->defer_end) {
3644                         ast_debug(3, "Deferring session (%p) end\n", session);
3645                         session->ended_while_deferred = 1;
3646                         return;
3647                 }
3648
3649                 if (ast_sip_push_task(session->serializer, session_end, session)) {
3650                         /* Do it anyway even though this is not the right thread. */
3651                         session_end(session);
3652                 }
3653         }
3654 }
3655
3656 static void session_inv_on_new_session(pjsip_inv_session *inv, pjsip_event *e)
3657 {
3658         /* XXX STUB */
3659 }
3660
3661 static int session_end_if_disconnected(int id, pjsip_inv_session *inv)
3662 {
3663         struct ast_sip_session *session;
3664
3665         if (inv->state != PJSIP_INV_STATE_DISCONNECTED) {
3666                 return 0;
3667         }
3668
3669         /*
3670          * We are locking because ast_sip_dialog_get_session() needs
3671          * the dialog locked to get the session by other threads.
3672          */
3673         pjsip_dlg_inc_lock(inv->dlg);
3674         session = inv->mod_data[id];
3675         inv->mod_data[id] = NULL;
3676         pjsip_dlg_dec_lock(inv->dlg);
3677
3678         /*
3679          * Pass the session ref held by session->inv_session to
3680          * session_end_completion().
3681          */
3682         if (session
3683                 && ast_sip_push_task(session->serializer, session_end_completion, session)) {
3684                 /* Do it anyway even though this is not the right thread. */
3685                 session_end_completion(session);
3686         }
3687
3688         return 1;
3689 }
3690
3691 static void session_inv_on_tsx_state_changed(pjsip_inv_session *inv, pjsip_transaction *tsx, pjsip_event *e)
3692 {
3693         ast_sip_session_response_cb cb;
3694         int id = session_module.id;
3695         struct ast_sip_session *session;
3696         pjsip_tx_data *tdata;
3697
3698         if (ast_shutdown_final()) {
3699                 return;
3700         }
3701
3702         session = inv->mod_data[id];
3703
3704         print_debug_details(inv, tsx, e);
3705         if (!session) {
3706                 /* The session has ended.  Ignore the transaction change. */
3707                 return;
3708         }
3709
3710         /*
3711          * If the session is disconnected really nothing else to do unless currently transacting
3712          * a BYE. If a BYE then hold off destruction until the transaction timeout occurs. This
3713          * has to be done for BYEs because sometimes the dialog can be in a disconnected
3714          * state but the BYE request transaction has not yet completed.
3715          */
3716         if (tsx->method.id != PJSIP_BYE_METHOD && session_end_if_disconnected(id, inv)) {
3717                 return;
3718         }
3719
3720         switch (e->body.tsx_state.type) {
3721         case PJSIP_EVENT_TX_MSG:
3722                 /* When we create an outgoing request, we do not have access to the transaction that
3723                  * is created. Instead, We have to place transaction-specific data in the tdata. Here,
3724                  * we transfer the data into the transaction. This way, when we receive a response, we
3725                  * can dig this data out again
3726                  */
3727                 tsx->mod_data[id] = e->body.tsx_state.src.tdata->mod_data[id];
3728                 break;
3729         case PJSIP_EVENT_RX_MSG:
3730                 cb = ast_sip_mod_data_get(tsx->mod_data, id, MOD_DATA_ON_RESPONSE);
3731                 /* As the PJSIP invite session implementation responds with a 200 OK before we have a
3732                  * chance to be invoked session supplements for BYE requests actually end up executing
3733                  * in the invite session state callback as well. To prevent session supplements from
3734                  * running on the BYE request again we explicitly squash invocation of them here.
3735                  */
3736                 if ((e->body.tsx_state.src.rdata->msg_info.msg->type != PJSIP_REQUEST_MSG) ||
3737                         (tsx->method.id != PJSIP_BYE_METHOD)) {
3738                         handle_incoming(session, e->body.tsx_state.src.rdata,
3739                                 AST_SIP_SESSION_AFTER_MEDIA);
3740                 }
3741                 if (tsx->method.id == PJSIP_INVITE_METHOD) {
3742                         if (tsx->role == PJSIP_ROLE_UAC) {
3743                                 if (tsx->state == PJSIP_TSX_STATE_COMPLETED) {
3744                                         /* This means we got a non 2XX final response to our outgoing INVITE */
3745                                         if (tsx->status_code == PJSIP_SC_REQUEST_PENDING) {
3746                                                 reschedule_reinvite(session, cb);
3747                                                 return;
3748                                         }
3749                                         if (inv->state == PJSIP_INV_STATE_CONFIRMED) {
3750                                                 ast_debug(1, "reINVITE received final response code %d\n",
3751                                                         tsx->status_code);
3752                                                 if ((tsx->status_code == 401 || tsx->status_code == 407)
3753                                                         && !ast_sip_create_request_with_auth(
3754                                                                 &session->endpoint->outbound_auths,
3755                                                                 e->body.tsx_state.src.rdata, tsx->last_tx, &tdata)) {
3756                                                         /* Send authed reINVITE */
3757                                                         ast_sip_session_send_request_with_cb(session, tdata, cb);
3758                                                         return;
3759                                                 }
3760                                                 if (tsx->status_code != 488 && tsx->status_code != 500) {
3761                                                         /* Other reinvite failures (except 488 and 500) result in destroying the session. */
3762                                                         if (pjsip_inv_end_session(inv, 500, NULL, &tdata) == PJ_SUCCESS
3763                                                                 && tdata) {
3764                                                                 ast_sip_session_send_request(session, tdata);
3765                                                         }
3766                                                 }
3767                                         }
3768                                 } else if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
3769                                         if (inv->cancelling && tsx->status_code == PJSIP_SC_OK) {
3770                                                 int sdp_negotiation_done =
3771                                                         pjmedia_sdp_neg_get_state(inv->neg) == PJMEDIA_SDP_NEG_STATE_DONE;
3772
3773                                                 /*
3774                                                  * We can get here for the following reasons.
3775                                                  *
3776                                                  * 1) The race condition detailed in RFC5407 section 3.1.2.
3777                                                  * We sent a CANCEL at the same time that the UAS sent us a
3778                                                  * 200 OK with a valid SDP for the original INVITE.  As a
3779                                                  * result, we have now received a 200 OK for a cancelled
3780                                                  * call and the SDP negotiation is complete.  We need to
3781                                                  * immediately send a BYE to end the dialog.
3782                                                  *
3783                                                  * 2) We sent a CANCEL and hit the race condition but the
3784                                                  * UAS sent us an invalid SDP with the 200 OK.  In this case
3785                                                  * the SDP negotiation is incomplete and PJPROJECT has
3786                                                  * already sent the BYE for us because of the invalid SDP.
3787                                                  *
3788                                                  * 3) We didn't send a CANCEL but the UAS sent us an invalid
3789                                                  * SDP with the 200 OK.  In this case the SDP negotiation is
3790                                                  * incomplete and PJPROJECT has already sent the BYE for us
3791                                                  * because of the invalid SDP.
3792                                                  */
3793                                                 ast_test_suite_event_notify("PJSIP_SESSION_CANCELED",
3794                                                         "Endpoint: %s\r\n"
3795                                                         "Channel: %s\r\n"
3796                                                         "Message: %s\r\n"
3797                                                         "SDP: %s",
3798                                                         ast_sorcery_object_get_id(session->endpoint),
3799                                                         session->channel ? ast_channel_name(session->channel) : "",
3800                                                         pjsip_rx_data_get_info(e->body.tsx_state.src.rdata),
3801                                                         sdp_negotiation_done ? "complete" : "incomplete");
3802                                                 if (!sdp_negotiation_done) {
3803                                                         ast_debug(1, "Endpoint '%s(%s)': Incomplete SDP negotiation cancelled session.  %s\n",
3804                                                                 ast_sorcery_object_get_id(session->endpoint),
3805                                                                 session->channel ? ast_channel_name(session->channel) : "",
3806                                                                 pjsip_rx_data_get_info(e->body.tsx_state.src.rdata));
3807                                                 } else if (pjsip_inv_end_session(inv, 500, NULL, &tdata) == PJ_SUCCESS
3808                                                         && tdata) {
3809                                                         ast_debug(1, "Endpoint '%s(%s)': Ending session due to RFC5407 race condition.  %s\n",
3810                                                                 ast_sorcery_object_get_id(session->endpoint),
3811                                                                 session->channel ? ast_channel_name(session->channel) : "",
3812                                                                 pjsip_rx_data_get_info(e->body.tsx_state.src.rdata));
3813                                                         ast_sip_session_send_request(session, tdata);
3814                                                 }
3815                                         }
3816                                 }
3817                         }
3818                 } else {
3819                         /* All other methods */
3820                         if (tsx->role == PJSIP_ROLE_UAC) {
3821                                 if (tsx->state == PJSIP_TSX_STATE_COMPLETED) {
3822                                         /* This means we got a final response to our outgoing method */
3823                                         ast_debug(1, "%.*s received final response code %d\n",
3824                                                 (int) pj_strlen(&tsx->method.name), pj_strbuf(&tsx->method.name),
3825                                                 tsx->status_code);
3826                                         if ((tsx->status_code == 401 || tsx->status_code == 407)
3827                                                 && !ast_sip_create_request_with_auth(
3828                                                         &session->endpoint->outbound_auths,
3829                                                         e->body.tsx_state.src.rdata, tsx->last_tx, &tdata)) {
3830                                                 /* Send authed version of the method */
3831                                                 ast_sip_session_send_request_with_cb(session, tdata, cb);
3832                                                 return;
3833                                         }
3834                                 }
3835                         }
3836                 }
3837                 if (cb) {
3838                         cb(session, e->body.tsx_state.src.rdata);
3839                 }
3840                 break;
3841         case PJSIP_EVENT_TRANSPORT_ERROR:
3842         case PJSIP_EVENT_TIMER:
3843                 /*
3844                  * The timer event is run by the pjsip monitor thread and not
3845                  * by the session serializer.
3846                  */
3847                 if (session_end_if_disconnected(id, inv)) {
3848                         return;
3849                 }
3850                 break;
3851         case PJSIP_EVENT_USER:
3852         case PJSIP_EVENT_UNKNOWN:
3853         case PJSIP_EVENT_TSX_STATE:
3854                 /* Inception? */
3855                 break;
3856         }
3857
3858         if (AST_LIST_EMPTY(&session->delayed_requests)) {
3859                 /* No delayed request pending, so just return */
3860                 return;
3861         }
3862
3863         if (tsx->method.id == PJSIP_INVITE_METHOD) {
3864                 if (tsx->state == PJSIP_TSX_STATE_PROCEEDING) {
3865                         ast_debug(3, "Endpoint '%s(%s)' INVITE delay check. tsx-state:%s\n",
3866                                 ast_sorcery_object_get_id(session->endpoint),
3867                                 session->channel ? ast_channel_name(session->channel) : "",
3868                                 pjsip_tsx_state_str(tsx->state));
3869                         check_delayed_requests(session, invite_proceeding);
3870                 } else if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
3871                         /*
3872                          * Terminated INVITE transactions always should result in
3873                          * queuing delayed requests, no matter what event caused
3874                          * the transaction to terminate.
3875                          */
3876                         ast_debug(3, "Endpoint '%s(%s)' INVITE delay check. tsx-state:%s\n",
3877                                 ast_sorcery_object_get_id(session->endpoint),
3878                                 session->channel ? ast_channel_name(session->channel) : "",
3879                                 pjsip_tsx_state_str(tsx->state));
3880                         check_delayed_requests(session, invite_terminated);
3881                 }
3882         } else if (tsx->role == PJSIP_ROLE_UAC
3883                 && tsx->state == PJSIP_TSX_STATE_COMPLETED
3884                 && !pj_strcmp2(&tsx->method.name, "UPDATE")) {
3885                 ast_debug(3, "Endpoint '%s(%s)' UPDATE delay check. tsx-state:%s\n",
3886                         ast_sorcery_object_get_id(session->endpoint),
3887                         session->channel ? ast_channel_name(session->channel) : "",
3888                         pjsip_tsx_state_str(tsx->state));
3889                 check_delayed_requests(session, update_completed);
3890         }
3891 }
3892
3893 static int add_sdp_streams(struct ast_sip_session_media *session_media,
3894         struct ast_sip_session *session, pjmedia_sdp_session *answer,
3895         const struct pjmedia_sdp_session *remote,
3896         struct ast_stream *stream)
3897 {
3898         struct ast_sip_session_sdp_handler *handler = session_media->handler;
3899         RAII_VAR(struct sdp_handler_list *, handler_list, NULL, ao2_cleanup);
3900         int res;
3901
3902         if (handler) {
3903                 /* if an already assigned handler reports a catastrophic error, fail */
3904                 res = handler->create_outgoing_sdp_stream(session, session_media, answer, remote, stream);
3905                 if (res < 0) {
3906                         return -1;
3907                 }
3908                 return 0;
3909         }
3910
3911         handler_list = ao2_find(sdp_handlers, ast_codec_media_type2str(session_media->type), OBJ_KEY);
3912         if (!handler_list) {
3913                 return 0;
3914         }
3915
3916         /* no handler for this stream type and we have a list to search */
3917         AST_LIST_TRAVERSE(&handler_list->list, handler, next) {
3918                 if (handler == session_media->handler) {
3919                         continue;
3920                 }
3921                 res = handler->create_outgoing_sdp_stream(session, session_media, answer, remote, stream);
3922                 if (res < 0) {
3923                         /* catastrophic error */
3924                         return -1;
3925                 }
3926                 if (res > 0) {
3927                         /* Handled by this handler. Move to the next stream */
3928                         session_media_set_handler(session_media, handler);
3929                         return 0;
3930                 }
3931         }
3932
3933         /* streams that weren't handled won't be included in generated outbound SDP */
3934         return 0;
3935 }
3936
3937 /*! \brief Bundle group building structure */
3938 struct sip_session_media_bundle_group {
3939         /*! \brief The media identifiers in this bundle group */
3940         char *mids[PJMEDIA_MAX_SDP_MEDIA];
3941         /*! \brief SDP attribute string */
3942         struct ast_str *attr_string;
3943 };
3944
3945 static int add_bundle_groups(struct ast_sip_session *session, pj_pool_t *pool, pjmedia_sdp_session *answer)
3946 {
3947         pj_str_t stmp;
3948         pjmedia_sdp_attr *attr;
3949         struct sip_session_media_bundle_group bundle_groups[PJMEDIA_MAX_SDP_MEDIA];
3950         int index, mid_id;
3951         struct sip_session_media_bundle_group *bundle_group;
3952
3953         if (session->endpoint->media.webrtc) {
3954                 attr = pjmedia_sdp_attr_create(pool, "msid-semantic", pj_cstr(&stmp, "WMS *"));
3955                 pjmedia_sdp_attr_add(&answer->attr_count, answer->attr, attr);
3956         }
3957
3958         if (!session->endpoint->media.bundle) {
3959                 return 0;
3960         }
3961
3962         memset(bundle_groups, 0, sizeof(bundle_groups));
3963
3964         /* Build the bundle group layout so we can then add it to the SDP */
3965         for (index = 0; index < AST_VECTOR_SIZE(&session->pending_media_state->sessions); ++index) {
3966                 struct ast_sip_session_media *session_media = AST_VECTOR_GET(&session->pending_media_state->sessions, index);
3967
3968                 /* If this stream is not part of a bundle group we can't add it */
3969                 if (session_media->bundle_group == -1) {
3970                         continue;
3971                 }
3972
3973                 bundle_group =&n