Asterisk - The Open Source Telephony Project  21.4.1
chan_pjsip.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18 
19 /*! \file
20  *
21  * \author Joshua Colp <jcolp@digium.com>
22  *
23  * \brief PSJIP SIP Channel Driver
24  *
25  * \ingroup channel_drivers
26  */
27 
28 /*** MODULEINFO
29  <depend>pjproject</depend>
30  <depend>res_pjsip</depend>
31  <depend>res_pjsip_pubsub</depend>
32  <depend>res_pjsip_session</depend>
33  <support_level>core</support_level>
34  ***/
35 
36 #include "asterisk.h"
37 
38 #include <pjsip.h>
39 #include <pjsip_ua.h>
40 #include <pjlib.h>
41 
42 #include "asterisk/lock.h"
43 #include "asterisk/channel.h"
44 #include "asterisk/module.h"
45 #include "asterisk/pbx.h"
46 #include "asterisk/rtp_engine.h"
47 #include "asterisk/acl.h"
48 #include "asterisk/callerid.h"
49 #include "asterisk/file.h"
50 #include "asterisk/cli.h"
51 #include "asterisk/app.h"
52 #include "asterisk/musiconhold.h"
53 #include "asterisk/causes.h"
54 #include "asterisk/taskprocessor.h"
55 #include "asterisk/dsp.h"
57 #include "asterisk/stasis_channels.h"
58 #include "asterisk/indications.h"
59 #include "asterisk/format_cache.h"
60 #include "asterisk/translate.h"
61 #include "asterisk/threadstorage.h"
62 #include "asterisk/features_config.h"
63 #include "asterisk/pickup.h"
64 #include "asterisk/test.h"
65 #include "asterisk/message.h"
66 
67 #include "asterisk/res_pjsip.h"
68 #include "asterisk/res_pjsip_session.h"
69 #include "asterisk/stream.h"
70 
74 
75 AST_THREADSTORAGE(uniqueid_threadbuf);
76 #define UNIQUEID_BUFSIZE 256
77 
78 static const char channel_type[] = "PJSIP";
79 
80 static unsigned int chan_idx;
81 
82 static void chan_pjsip_pvt_dtor(void *obj)
83 {
84 }
85 
86 /*! \brief Asterisk core interaction functions */
87 static struct ast_channel *chan_pjsip_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause);
88 static struct ast_channel *chan_pjsip_request_with_stream_topology(const char *type,
89  struct ast_stream_topology *topology, const struct ast_assigned_ids *assignedids,
90  const struct ast_channel *requestor, const char *data, int *cause);
91 static int chan_pjsip_sendtext_data(struct ast_channel *ast, struct ast_msg_data *msg);
92 static int chan_pjsip_sendtext(struct ast_channel *ast, const char *text);
93 static int chan_pjsip_digit_begin(struct ast_channel *ast, char digit);
94 static int chan_pjsip_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
95 static int chan_pjsip_call(struct ast_channel *ast, const char *dest, int timeout);
96 static int chan_pjsip_hangup(struct ast_channel *ast);
97 static int chan_pjsip_answer(struct ast_channel *ast);
98 static struct ast_frame *chan_pjsip_read_stream(struct ast_channel *ast);
99 static int chan_pjsip_write(struct ast_channel *ast, struct ast_frame *f);
100 static int chan_pjsip_write_stream(struct ast_channel *ast, int stream_num, struct ast_frame *f);
101 static int chan_pjsip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
102 static int chan_pjsip_transfer(struct ast_channel *ast, const char *target);
103 static int chan_pjsip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
104 static int chan_pjsip_devicestate(const char *data);
105 static int chan_pjsip_queryoption(struct ast_channel *ast, int option, void *data, int *datalen);
106 static const char *chan_pjsip_get_uniqueid(struct ast_channel *ast);
107 
108 /*! \brief PBX interface structure for channel registration */
110  .type = channel_type,
111  .description = "PJSIP Channel Driver",
112  .requester = chan_pjsip_request,
113  .requester_with_stream_topology = chan_pjsip_request_with_stream_topology,
114  .send_text = chan_pjsip_sendtext,
115  .send_text_data = chan_pjsip_sendtext_data,
116  .send_digit_begin = chan_pjsip_digit_begin,
117  .send_digit_end = chan_pjsip_digit_end,
118  .call = chan_pjsip_call,
119  .hangup = chan_pjsip_hangup,
120  .answer = chan_pjsip_answer,
121  .read_stream = chan_pjsip_read_stream,
122  .write = chan_pjsip_write,
123  .write_stream = chan_pjsip_write_stream,
124  .exception = chan_pjsip_read_stream,
125  .indicate = chan_pjsip_indicate,
126  .transfer = chan_pjsip_transfer,
127  .fixup = chan_pjsip_fixup,
128  .devicestate = chan_pjsip_devicestate,
129  .queryoption = chan_pjsip_queryoption,
130  .func_channel_read = pjsip_acf_channel_read,
131  .get_pvt_uniqueid = chan_pjsip_get_uniqueid,
133 };
134 
135 /*! \brief SIP session interaction functions */
136 static void chan_pjsip_session_begin(struct ast_sip_session *session);
137 static void chan_pjsip_session_end(struct ast_sip_session *session);
138 static int chan_pjsip_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata);
139 static void chan_pjsip_incoming_response(struct ast_sip_session *session, struct pjsip_rx_data *rdata);
140 static void chan_pjsip_incoming_response_update_cause(struct ast_sip_session *session, struct pjsip_rx_data *rdata);
141 
142 /*! \brief SIP session supplement structure */
144  .method = "INVITE",
145  .priority = AST_SIP_SUPPLEMENT_PRIORITY_CHANNEL,
146  .session_begin = chan_pjsip_session_begin,
147  .session_end = chan_pjsip_session_end,
148  .incoming_request = chan_pjsip_incoming_request,
149  .incoming_response = chan_pjsip_incoming_response,
150  /* It is important that this supplement runs after media has been negotiated */
151  .response_priority = AST_SIP_SESSION_AFTER_MEDIA,
152 };
153 
154 /*! \brief SIP session supplement structure just for responses */
156  .method = "INVITE",
157  .priority = AST_SIP_SUPPLEMENT_PRIORITY_CHANNEL,
158  .incoming_response = chan_pjsip_incoming_response_update_cause,
159  .response_priority = AST_SIP_SESSION_BEFORE_MEDIA | AST_SIP_SESSION_AFTER_MEDIA,
160 };
161 
162 static int chan_pjsip_incoming_ack(struct ast_sip_session *session, struct pjsip_rx_data *rdata);
163 
164 static struct ast_sip_session_supplement chan_pjsip_ack_supplement = {
165  .method = "ACK",
166  .priority = AST_SIP_SUPPLEMENT_PRIORITY_CHANNEL,
167  .incoming_request = chan_pjsip_incoming_ack,
168 };
169 
170 static int chan_pjsip_incoming_prack(struct ast_sip_session *session, struct pjsip_rx_data *rdata);
171 
172 static struct ast_sip_session_supplement chan_pjsip_prack_supplement = {
173  .method = "PRACK",
174  .priority = AST_SIP_SUPPLEMENT_PRIORITY_CHANNEL,
175  .incoming_request = chan_pjsip_incoming_prack,
176 };
177 
178 /*! \brief Function called by RTP engine to get local audio RTP peer */
179 static enum ast_rtp_glue_result chan_pjsip_get_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
180 {
181  struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
182  struct ast_sip_endpoint *endpoint;
183  struct ast_datastore *datastore;
184  struct ast_sip_session_media *media;
185 
186  if (!channel || !channel->session) {
188  }
189 
190  /* XXX Getting the first RTP instance for direct media related stuff seems just
191  * absolutely wrong. But the native RTP bridge knows no other method than single-stream
192  * for direct media. So this is the best we can do.
193  */
194  media = channel->session->active_media_state->default_session[AST_MEDIA_TYPE_AUDIO];
195  if (!media || !media->rtp) {
197  }
198 
199  datastore = ast_sip_session_get_datastore(channel->session, "t38");
200  if (datastore) {
201  ao2_ref(datastore, -1);
203  }
204 
205  endpoint = channel->session->endpoint;
206 
207  *instance = media->rtp;
208  ao2_ref(*instance, +1);
209 
210  ast_assert(endpoint != NULL);
211  if (endpoint->media.rtp.encryption != AST_SIP_MEDIA_ENCRYPT_NONE) {
213  }
214 
215  if (endpoint->media.direct_media.enabled) {
217  }
218 
220 }
221 
222 /*! \brief Function called by RTP engine to get local video RTP peer */
223 static enum ast_rtp_glue_result chan_pjsip_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
224 {
225  struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
226  struct ast_sip_endpoint *endpoint;
227  struct ast_sip_session_media *media;
228 
229  if (!channel || !channel->session) {
231  }
232 
233  media = channel->session->active_media_state->default_session[AST_MEDIA_TYPE_VIDEO];
234  if (!media || !media->rtp) {
236  }
237 
238  endpoint = channel->session->endpoint;
239 
240  *instance = media->rtp;
241  ao2_ref(*instance, +1);
242 
243  ast_assert(endpoint != NULL);
244  if (endpoint->media.rtp.encryption != AST_SIP_MEDIA_ENCRYPT_NONE) {
246  }
247 
249 }
250 
251 /*! \brief Function called by RTP engine to get peer capabilities */
252 static void chan_pjsip_get_codec(struct ast_channel *chan, struct ast_format_cap *result)
253 {
254  SCOPE_ENTER(1, "%s Native formats %s\n", ast_channel_name(chan),
255  ast_str_tmp(AST_FORMAT_CAP_NAMES_LEN, ast_format_cap_get_names(ast_channel_nativeformats(chan), &STR_TMP)));
256  ast_format_cap_append_from_cap(result, ast_channel_nativeformats(chan), AST_MEDIA_TYPE_UNKNOWN);
257  SCOPE_EXIT_RTN();
258 }
259 
260 /*! \brief Destructor function for \ref transport_info_data */
261 static void transport_info_destroy(void *obj)
262 {
263  struct transport_info_data *data = obj;
264  ast_free(data);
265 }
266 
267 /*! \brief Datastore used to store local/remote addresses for the
268  * INVITE request that created the PJSIP channel */
270  .type = "chan_pjsip_transport_info",
271  .destroy = transport_info_destroy,
272 };
273 
274 static struct ast_datastore_info direct_media_mitigation_info = { };
275 
276 static int direct_media_mitigate_glare(struct ast_sip_session *session)
277 {
278  RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
279 
281  AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_NONE) {
282  return 0;
283  }
284 
285  datastore = ast_sip_session_get_datastore(session, "direct_media_glare_mitigation");
286  if (!datastore) {
287  return 0;
288  }
289 
290  /* Removing the datastore ensures we won't try to mitigate glare on subsequent reinvites */
291  ast_sip_session_remove_datastore(session, "direct_media_glare_mitigation");
292 
293  if ((session->endpoint->media.direct_media.glare_mitigation ==
294  AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_OUTGOING &&
295  session->inv_session->role == PJSIP_ROLE_UAC) ||
297  AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_INCOMING &&
298  session->inv_session->role == PJSIP_ROLE_UAS)) {
299  return 1;
300  }
301 
302  return 0;
303 }
304 
305 /*! \brief Helper function to find the position for RTCP */
306 static int rtp_find_rtcp_fd_position(struct ast_sip_session *session, struct ast_rtp_instance *rtp)
307 {
308  int index;
309 
310  for (index = 0; index < AST_VECTOR_SIZE(&session->active_media_state->read_callbacks); ++index) {
311  struct ast_sip_session_media_read_callback_state *callback_state =
313 
314  if (callback_state->fd != ast_rtp_instance_fd(rtp, 1)) {
315  continue;
316  }
317 
318  return index;
319  }
320 
321  return -1;
322 }
323 
324 /*!
325  * \pre chan is locked
326  */
327 static int check_for_rtp_changes(struct ast_channel *chan, struct ast_rtp_instance *rtp,
328  struct ast_sip_session_media *media, struct ast_sip_session *session)
329 {
330  int changed = 0, position = -1;
331 
332  if (media->rtp) {
333  position = rtp_find_rtcp_fd_position(session, media->rtp);
334  }
335 
336  if (rtp) {
338  if (media->rtp) {
339  if (position != -1) {
340  ast_channel_set_fd(chan, position + AST_EXTENDED_FDS, -1);
341  }
343  }
344  } else if (!ast_sockaddr_isnull(&media->direct_media_addr)){
346  changed = 1;
347  if (media->rtp) {
349  if (position != -1) {
350  ast_channel_set_fd(chan, position + AST_EXTENDED_FDS, ast_rtp_instance_fd(media->rtp, 1));
351  }
352  }
353  }
354 
355  return changed;
356 }
357 
359  struct ast_channel *chan;
360  struct ast_rtp_instance *rtp;
361  struct ast_rtp_instance *vrtp;
362  struct ast_format_cap *cap;
363  struct ast_sip_session *session;
364 };
365 
366 static void rtp_direct_media_data_destroy(void *data)
367 {
368  struct rtp_direct_media_data *cdata = data;
369 
370  ao2_cleanup(cdata->session);
371  ao2_cleanup(cdata->cap);
372  ao2_cleanup(cdata->vrtp);
373  ao2_cleanup(cdata->rtp);
374  ao2_cleanup(cdata->chan);
375 }
376 
377 static struct rtp_direct_media_data *rtp_direct_media_data_create(
378  struct ast_channel *chan, struct ast_rtp_instance *rtp, struct ast_rtp_instance *vrtp,
379  const struct ast_format_cap *cap, struct ast_sip_session *session)
380 {
381  struct rtp_direct_media_data *cdata = ao2_alloc(sizeof(*cdata), rtp_direct_media_data_destroy);
382 
383  if (!cdata) {
384  return NULL;
385  }
386 
387  cdata->chan = ao2_bump(chan);
388  cdata->rtp = ao2_bump(rtp);
389  cdata->vrtp = ao2_bump(vrtp);
390  cdata->cap = ao2_bump((struct ast_format_cap *)cap);
391  cdata->session = ao2_bump(session);
392 
393  return cdata;
394 }
395 
396 static int send_direct_media_request(void *data)
397 {
398  struct rtp_direct_media_data *cdata = data;
399  struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(cdata->chan);
400  struct ast_sip_session *session;
401  int changed = 0;
402  int res = 0;
403 
404  /* XXX In an ideal world each media stream would be direct, but for now preserve behavior
405  * and connect only the default media sessions for audio and video.
406  */
407 
408  /* The channel needs to be locked when checking for RTP changes.
409  * Otherwise, we could end up destroying an underlying RTCP structure
410  * at the same time that the channel thread is attempting to read RTCP
411  */
412  ast_channel_lock(cdata->chan);
413  session = channel->session;
414  if (session->active_media_state->default_session[AST_MEDIA_TYPE_AUDIO]) {
415  changed |= check_for_rtp_changes(
416  cdata->chan, cdata->rtp, session->active_media_state->default_session[AST_MEDIA_TYPE_AUDIO], session);
417  }
418  if (session->active_media_state->default_session[AST_MEDIA_TYPE_VIDEO]) {
419  changed |= check_for_rtp_changes(
420  cdata->chan, cdata->vrtp, session->active_media_state->default_session[AST_MEDIA_TYPE_VIDEO], session);
421  }
422  ast_channel_unlock(cdata->chan);
423 
424  if (direct_media_mitigate_glare(cdata->session)) {
425  ast_debug(4, "Disregarding setting RTP on %s: mitigating re-INVITE glare\n", ast_channel_name(cdata->chan));
426  ao2_ref(cdata, -1);
427  return 0;
428  }
429 
430  if (cdata->cap && ast_format_cap_count(cdata->cap) &&
431  !ast_format_cap_identical(cdata->session->direct_media_cap, cdata->cap)) {
432  ast_format_cap_remove_by_type(cdata->session->direct_media_cap, AST_MEDIA_TYPE_UNKNOWN);
433  ast_format_cap_append_from_cap(cdata->session->direct_media_cap, cdata->cap, AST_MEDIA_TYPE_UNKNOWN);
434  changed = 1;
435  }
436 
437  if (changed) {
438  ast_debug(4, "RTP changed on %s; initiating direct media update\n", ast_channel_name(cdata->chan));
439  res = ast_sip_session_refresh(cdata->session, NULL, NULL, NULL,
440  cdata->session->endpoint->media.direct_media.method, 1, NULL);
441  }
442 
443  ao2_ref(cdata, -1);
444  return res;
445 }
446 
447 /*! \brief Function called by RTP engine to change where the remote party should send media */
448 static int chan_pjsip_set_rtp_peer(struct ast_channel *chan,
449  struct ast_rtp_instance *rtp,
450  struct ast_rtp_instance *vrtp,
451  struct ast_rtp_instance *tpeer,
452  const struct ast_format_cap *cap,
453  int nat_active)
454 {
455  struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
456  struct ast_sip_session *session = channel->session;
457  struct rtp_direct_media_data *cdata;
458  SCOPE_ENTER(1, "%s %s\n", ast_channel_name(chan),
460 
461  /* Don't try to do any direct media shenanigans on early bridges */
462  if ((rtp || vrtp || tpeer) && !ast_channel_is_bridged(chan)) {
463  ast_debug(4, "Disregarding setting RTP on %s: channel is not bridged\n", ast_channel_name(chan));
464  SCOPE_EXIT_RTN_VALUE(0, "Channel not bridged\n");
465  }
466 
467  if (nat_active && session->endpoint->media.direct_media.disable_on_nat) {
468  ast_debug(4, "Disregarding setting RTP on %s: NAT is active\n", ast_channel_name(chan));
469  SCOPE_EXIT_RTN_VALUE(0, "NAT is active\n");
470  }
471 
472  cdata = rtp_direct_media_data_create(chan, rtp, vrtp, cap, session);
473  if (!cdata) {
474  SCOPE_EXIT_RTN_VALUE(0);
475  }
476 
477  if (ast_sip_push_task(session->serializer, send_direct_media_request, cdata)) {
478  ast_log(LOG_ERROR, "Unable to send direct media request for channel %s\n", ast_channel_name(chan));
479  ao2_ref(cdata, -1);
480  }
481 
482  SCOPE_EXIT_RTN_VALUE(0);
483 }
484 
485 /*! \brief Local glue for interacting with the RTP engine core */
487  .type = "PJSIP",
488  .get_rtp_info = chan_pjsip_get_rtp_peer,
489  .get_vrtp_info = chan_pjsip_get_vrtp_peer,
490  .get_codec = chan_pjsip_get_codec,
491  .update_peer = chan_pjsip_set_rtp_peer,
492 };
493 
494 static void set_channel_on_rtp_instance(const struct ast_sip_session *session,
495  const char *channel_id)
496 {
497  int i;
498 
499  for (i = 0; i < AST_VECTOR_SIZE(&session->active_media_state->sessions); ++i) {
500  struct ast_sip_session_media *session_media;
501 
502  session_media = AST_VECTOR_GET(&session->active_media_state->sessions, i);
503  if (!session_media || !session_media->rtp) {
504  continue;
505  }
506 
507  ast_rtp_instance_set_channel_id(session_media->rtp, channel_id);
508  }
509 }
510 
511 /*!
512  * \brief Determine if a topology is compatible with format capabilities
513  *
514  * This will return true if ANY formats in the topology are compatible with the format
515  * capabilities.
516  *
517  * XXX When supporting true multistream, we will need to be sure to mark which streams from
518  * top1 are compatible with which streams from top2. Then the ones that are not compatible
519  * will need to be marked as "removed" so that they are negotiated as expected.
520  *
521  * \param top Topology
522  * \param cap Format capabilities
523  * \retval 1 The topology has at least one compatible format
524  * \retval 0 The topology has no compatible formats or an error occurred.
525  */
526 static int compatible_formats_exist(struct ast_stream_topology *top, struct ast_format_cap *cap)
527 {
528  struct ast_format_cap *cap_from_top;
529  int res;
530  SCOPE_ENTER(1, "Topology: %s Formats: %s\n",
533 
534  cap_from_top = ast_stream_topology_get_formats(top);
535 
536  if (!cap_from_top) {
537  SCOPE_EXIT_RTN_VALUE(0, "Topology had no formats\n");
538  }
539 
540  res = ast_format_cap_iscompatible(cap_from_top, cap);
541  ao2_ref(cap_from_top, -1);
542 
543  SCOPE_EXIT_RTN_VALUE(res, "Compatible? %s\n", res ? "yes" : "no");
544 }
545 
546 /*! \brief Function called to create a new PJSIP Asterisk channel */
547 static struct ast_channel *chan_pjsip_new(struct ast_sip_session *session, int state, const char *exten, const char *title, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *cid_name)
548 {
549  struct ast_channel *chan;
550  struct ast_format_cap *caps;
551  RAII_VAR(struct chan_pjsip_pvt *, pvt, NULL, ao2_cleanup);
552  struct ast_sip_channel_pvt *channel;
553  struct ast_variable *var;
554  struct ast_stream_topology *topology;
555  SCOPE_ENTER(1, "%s\n", ast_sip_session_get_name(session));
556 
557  if (!(pvt = ao2_alloc_options(sizeof(*pvt), chan_pjsip_pvt_dtor, AO2_ALLOC_OPT_LOCK_NOLOCK))) {
558  SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't create pvt\n");
559  }
560 
561  chan = ast_channel_alloc_with_endpoint(1, state,
562  S_COR(session->id.number.valid, session->id.number.str, ""),
563  S_COR(session->id.name.valid, session->id.name.str, ""),
564  session->endpoint->accountcode,
565  exten, session->endpoint->context,
566  assignedids, requestor, 0,
567  session->endpoint->persistent, "PJSIP/%s-%08x",
569  (unsigned) ast_atomic_fetchadd_int((int *) &chan_idx, +1));
570  if (!chan) {
571  SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't create channel\n");
572  }
573 
574  ast_channel_tech_set(chan, &chan_pjsip_tech);
575 
576  if (!(channel = ast_sip_channel_pvt_alloc(pvt, session))) {
577  ast_channel_unlock(chan);
578  ast_hangup(chan);
579  SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't create pvt channel\n");
580  }
581 
582  ast_channel_tech_pvt_set(chan, channel);
583 
587  if (!caps) {
588  ast_channel_unlock(chan);
589  ast_hangup(chan);
590  SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't create caps\n");
591  }
592  ast_format_cap_append_from_cap(caps, session->endpoint->media.codecs, AST_MEDIA_TYPE_UNKNOWN);
593  topology = ast_stream_topology_clone(session->endpoint->media.topology);
594  } else {
597  }
598 
599  if (!topology || !caps) {
600  ao2_cleanup(caps);
601  ast_stream_topology_free(topology);
602  ast_channel_unlock(chan);
603  ast_hangup(chan);
604  SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't get caps or clone topology\n");
605  }
606 
608 
609  ast_channel_nativeformats_set(chan, caps);
610  ast_channel_set_stream_topology(chan, topology);
611 
612  if (!ast_format_cap_empty(caps)) {
613  struct ast_format *fmt;
614 
615  fmt = ast_format_cap_get_best_by_type(caps, AST_MEDIA_TYPE_AUDIO);
616  if (!fmt) {
617  /* Since our capabilities aren't empty, this will succeed */
618  fmt = ast_format_cap_get_format(caps, 0);
619  }
620  ast_channel_set_writeformat(chan, fmt);
621  ast_channel_set_rawwriteformat(chan, fmt);
622  ast_channel_set_readformat(chan, fmt);
623  ast_channel_set_rawreadformat(chan, fmt);
624  ao2_ref(fmt, -1);
625  }
626 
627  ao2_ref(caps, -1);
628 
629  if (state == AST_STATE_RING) {
630  ast_channel_rings_set(chan, 1);
631  }
632 
633  ast_channel_adsicpe_set(chan, AST_ADSI_UNAVAILABLE);
634 
635  ast_party_id_copy(&ast_channel_caller(chan)->id, &session->id);
636  ast_party_id_copy(&ast_channel_caller(chan)->ani, &session->id);
637  ast_channel_caller(chan)->ani2 = session->ani2;
638 
639  if (!ast_strlen_zero(exten)) {
640  /* Set provided DNID on the new channel. */
641  ast_channel_dialed(chan)->number.str = ast_strdup(exten);
642  }
643 
644  ast_channel_priority_set(chan, 1);
645 
648 
649  ast_channel_named_callgroups_set(chan, session->endpoint->pickup.named_callgroups);
650  ast_channel_named_pickupgroups_set(chan, session->endpoint->pickup.named_pickupgroups);
651 
652  if (!ast_strlen_zero(session->endpoint->language)) {
653  ast_channel_language_set(chan, session->endpoint->language);
654  }
655 
656  if (!ast_strlen_zero(session->endpoint->zone)) {
657  struct ast_tone_zone *zone = ast_get_indication_zone(session->endpoint->zone);
658  if (!zone) {
659  ast_log(LOG_ERROR, "Unknown country code '%s' for tonezone. Check indications.conf for available country codes.\n", session->endpoint->zone);
660  }
661  ast_channel_zone_set(chan, zone);
662  }
663 
664  for (var = session->endpoint->channel_vars; var; var = var->next) {
665  char buf[512];
667  var->value, buf, sizeof(buf)));
668  }
669 
671  ast_channel_unlock(chan);
672 
673  set_channel_on_rtp_instance(session, ast_channel_uniqueid(chan));
674 
675  SCOPE_EXIT_RTN_VALUE(chan);
676 }
677 
678 struct answer_data {
679  struct ast_sip_session *session;
680  unsigned long indent;
681 };
682 
683 static int answer(void *data)
684 {
685  struct answer_data *ans_data = data;
686  pj_status_t status = PJ_SUCCESS;
687  pjsip_tx_data *packet = NULL;
688  struct ast_sip_session *session = ans_data->session;
689  SCOPE_ENTER_TASK(1, ans_data->indent, "%s\n", ast_sip_session_get_name(session));
690 
691  if (session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
692  ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
693  session->inv_session->cause,
694  pjsip_get_status_text(session->inv_session->cause)->ptr);
695  SCOPE_EXIT_RTN_VALUE(0, "Disconnected\n");
696  }
697 
698  pjsip_dlg_inc_lock(session->inv_session->dlg);
699  if (session->inv_session->invite_tsx) {
700  status = pjsip_inv_answer(session->inv_session, 200, NULL, NULL, &packet);
701  } else {
702  ast_log(LOG_ERROR,"Cannot answer '%s' because there is no associated SIP transaction\n",
703  ast_channel_name(session->channel));
704  }
705  pjsip_dlg_dec_lock(session->inv_session->dlg);
706 
707  if (status == PJ_SUCCESS && packet) {
708  ast_sip_session_send_response(session, packet);
709  }
710 
711  if (status != PJ_SUCCESS) {
712  char err[PJ_ERR_MSG_SIZE];
713 
714  pj_strerror(status, err, sizeof(err));
715  ast_log(LOG_WARNING,"Cannot answer '%s': %s\n",
716  ast_channel_name(session->channel), err);
717  /*
718  * Return this value so we can distinguish between this
719  * failure and the threadpool synchronous push failing.
720  */
721  SCOPE_EXIT_RTN_VALUE(-2, "pjproject failure\n");
722  }
723  SCOPE_EXIT_RTN_VALUE(0);
724 }
725 
726 /*! \brief Function called by core when we should answer a PJSIP session */
727 static int chan_pjsip_answer(struct ast_channel *ast)
728 {
729  struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
730  struct ast_sip_session *session;
731  struct answer_data ans_data = { 0, };
732  int res;
733  SCOPE_ENTER(1, "%s\n", ast_channel_name(ast));
734 
735  if (ast_channel_state(ast) == AST_STATE_UP) {
736  SCOPE_EXIT_RTN_VALUE(0, "Already up\n");
737  return 0;
738  }
739 
741  session = ao2_bump(channel->session);
742 
743  /* the answer task needs to be pushed synchronously otherwise a race condition
744  can occur between this thread and bridging (specifically when native bridging
745  attempts to do direct media) */
746  ast_channel_unlock(ast);
747  ans_data.session = session;
748  ans_data.indent = ast_trace_get_indent();
749  res = ast_sip_push_task_wait_serializer(session->serializer, answer, &ans_data);
750  if (res) {
751  if (res == -1) {
752  ast_log(LOG_ERROR,"Cannot answer '%s': Unable to push answer task to the threadpool.\n",
753  ast_channel_name(session->channel));
754  }
755  ao2_ref(session, -1);
756  ast_channel_lock(ast);
757  SCOPE_EXIT_RTN_VALUE(-1, "Couldn't push task\n");
758  }
759  ao2_ref(session, -1);
760  ast_channel_lock(ast);
761 
762  SCOPE_EXIT_RTN_VALUE(0);
763 }
764 
765 /*! \brief Internal helper function called when CNG tone is detected */
766 static struct ast_frame *chan_pjsip_cng_tone_detected(struct ast_channel *ast, struct ast_sip_session *session,
767  struct ast_frame *f)
768 {
769  const char *target_context;
770  int exists;
771  int dsp_features;
772 
773  dsp_features = ast_dsp_get_features(session->dsp);
774  dsp_features &= ~DSP_FEATURE_FAX_DETECT;
775  if (dsp_features) {
776  ast_dsp_set_features(session->dsp, dsp_features);
777  } else {
778  ast_dsp_free(session->dsp);
779  session->dsp = NULL;
780  }
781 
782  /* If already executing in the fax extension don't do anything */
783  if (!strcmp(ast_channel_exten(ast), "fax")) {
784  return f;
785  }
786 
787  target_context = ast_channel_context(ast);
788 
789  /*
790  * We need to unlock the channel here because ast_exists_extension has the
791  * potential to start and stop an autoservice on the channel. Such action
792  * is prone to deadlock if the channel is locked.
793  *
794  * ast_async_goto() has its own restriction on not holding the channel lock.
795  */
796  ast_channel_unlock(ast);
797  ast_frfree(f);
798  f = &ast_null_frame;
799  exists = ast_exists_extension(ast, target_context, "fax", 1,
800  S_COR(ast_channel_caller(ast)->id.number.valid,
801  ast_channel_caller(ast)->id.number.str, NULL));
802  if (exists) {
803  ast_verb(2, "Redirecting '%s' to fax extension due to CNG detection\n",
804  ast_channel_name(ast));
805  pbx_builtin_setvar_helper(ast, "FAXEXTEN", ast_channel_exten(ast));
806  if (ast_async_goto(ast, target_context, "fax", 1)) {
807  ast_log(LOG_ERROR, "Failed to async goto '%s' into fax extension in '%s'\n",
808  ast_channel_name(ast), target_context);
809  }
810  } else {
811  ast_log(LOG_NOTICE, "FAX CNG detected on '%s' but no fax extension in '%s'\n",
812  ast_channel_name(ast), target_context);
813  }
814 
815  /* It's possible for a masquerade to have occurred when doing the ast_async_goto resulting in
816  * the channel on the session having changed. Since we need to return with the original channel
817  * locked we lock the channel that was passed in and not session->channel.
818  */
819  ast_channel_lock(ast);
820 
821  return f;
822 }
823 
824 /*! \brief Determine if the given frame is in a format we've negotiated */
825 static int is_compatible_format(struct ast_sip_session *session, struct ast_frame *f)
826 {
827  struct ast_stream_topology *topology = session->active_media_state->topology;
828  struct ast_stream *stream = ast_stream_topology_get_stream(topology, f->stream_num);
829  const struct ast_format_cap *cap = ast_stream_get_formats(stream);
830 
832 }
833 
834 /*!
835  * \brief Function called by core to read any waiting frames
836  *
837  * \note The channel is already locked.
838  */
839 static struct ast_frame *chan_pjsip_read_stream(struct ast_channel *ast)
840 {
841  struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
842  struct ast_sip_session *session = channel->session;
843  struct ast_sip_session_media_read_callback_state *callback_state;
844  struct ast_frame *f;
845  int fdno = ast_channel_fdno(ast) - AST_EXTENDED_FDS;
846  struct ast_frame *cur;
847 
848  if (fdno >= AST_VECTOR_SIZE(&session->active_media_state->read_callbacks)) {
849  return &ast_null_frame;
850  }
851 
852  callback_state = AST_VECTOR_GET_ADDR(&session->active_media_state->read_callbacks, fdno);
853  f = callback_state->read_callback(session, callback_state->session);
854 
855  if (!f) {
856  return f;
857  }
858 
859  for (cur = f; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
860  if (cur->frametype == AST_FRAME_VOICE) {
861  break;
862  }
863  }
864 
865  if (!cur || callback_state->session != session->active_media_state->default_session[callback_state->session->type]) {
866  return f;
867  }
868 
869  session = channel->session;
870 
871  /*
872  * Asymmetric RTP only has one native format set at a time.
873  * Therefore we need to update the native format to the current
874  * raw read format BEFORE the native format check
875  */
876  if (!session->endpoint->asymmetric_rtp_codec &&
877  ast_format_cmp(ast_channel_rawwriteformat(ast), cur->subclass.format) == AST_FORMAT_CMP_NOT_EQUAL &&
878  is_compatible_format(session, cur)) {
879  struct ast_format_cap *caps;
880 
881  /* For maximum compatibility we ensure that the formats match that of the received media */
882  ast_debug(1, "Oooh, got a frame with format of %s on channel '%s' when we're sending '%s', switching to match\n",
883  ast_format_get_name(cur->subclass.format), ast_channel_name(ast),
884  ast_format_get_name(ast_channel_rawwriteformat(ast)));
885 
887  if (caps) {
888  ast_format_cap_append_from_cap(caps, ast_channel_nativeformats(ast), AST_MEDIA_TYPE_UNKNOWN);
889  ast_format_cap_remove_by_type(caps, AST_MEDIA_TYPE_AUDIO);
890  ast_format_cap_append(caps, cur->subclass.format, 0);
891  ast_channel_nativeformats_set(ast, caps);
892  ao2_ref(caps, -1);
893  }
894 
895  ast_set_write_format_path(ast, ast_channel_writeformat(ast), cur->subclass.format);
896  ast_set_read_format_path(ast, ast_channel_readformat(ast), cur->subclass.format);
897 
898  if (ast_channel_is_bridged(ast)) {
900  }
901  }
902 
903  if (ast_format_cap_iscompatible_format(ast_channel_nativeformats(ast), cur->subclass.format)
905  ast_debug(1, "Oooh, got a frame with format of %s on channel '%s' when it has not been negotiated\n",
906  ast_format_get_name(cur->subclass.format), ast_channel_name(ast));
907  ast_frfree(f);
908  return &ast_null_frame;
909  }
910 
911  if (session->dsp) {
912  int dsp_features;
913 
914  dsp_features = ast_dsp_get_features(session->dsp);
915  if ((dsp_features & DSP_FEATURE_FAX_DETECT)
916  && session->endpoint->faxdetect_timeout
917  && session->endpoint->faxdetect_timeout <= ast_channel_get_up_time(ast)) {
918  dsp_features &= ~DSP_FEATURE_FAX_DETECT;
919  if (dsp_features) {
920  ast_dsp_set_features(session->dsp, dsp_features);
921  } else {
922  ast_dsp_free(session->dsp);
923  session->dsp = NULL;
924  }
925  ast_debug(3, "Channel driver fax CNG detection timeout on %s\n",
926  ast_channel_name(ast));
927  }
928  }
929  if (session->dsp) {
930  f = ast_dsp_process(ast, session->dsp, f);
931  if (f && (f->frametype == AST_FRAME_DTMF)) {
932  if (f->subclass.integer == 'f') {
933  ast_debug(3, "Channel driver fax CNG detected on %s\n",
934  ast_channel_name(ast));
935  f = chan_pjsip_cng_tone_detected(ast, session, f);
936  /* When chan_pjsip_cng_tone_detected returns it is possible for the
937  * channel pointed to by ast and by session->channel to differ due to a
938  * masquerade. It's best not to touch things after this.
939  */
940  } else {
941  ast_debug(3, "* Detected inband DTMF '%c' on '%s'\n", f->subclass.integer,
942  ast_channel_name(ast));
943  }
944  }
945  }
946 
947  return f;
948 }
949 
950 static int chan_pjsip_write_stream(struct ast_channel *ast, int stream_num, struct ast_frame *frame)
951 {
952  struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
953  struct ast_sip_session *session = channel->session;
954  struct ast_sip_session_media *media = NULL;
955  int res = 0;
956 
957  /* The core provides a guarantee that the stream will exist when we are called if stream_num is provided */
958  if (stream_num >= 0) {
959  /* What is not guaranteed is that a media session will exist */
960  if (stream_num < AST_VECTOR_SIZE(&channel->session->active_media_state->sessions)) {
961  media = AST_VECTOR_GET(&channel->session->active_media_state->sessions, stream_num);
962  }
963  }
964 
965  switch (frame->frametype) {
966  case AST_FRAME_VOICE:
967  if (!media) {
968  return 0;
969  } else if (media->type != AST_MEDIA_TYPE_AUDIO) {
970  ast_debug(3, "Channel %s stream %d is of type '%s', not audio!\n",
971  ast_channel_name(ast), stream_num, ast_codec_media_type2str(media->type));
972  return 0;
973  } else if (media == channel->session->active_media_state->default_session[AST_MEDIA_TYPE_AUDIO] &&
974  ast_format_cap_iscompatible_format(ast_channel_nativeformats(ast), frame->subclass.format) == AST_FORMAT_CMP_NOT_EQUAL) {
975  struct ast_str *cap_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
976  struct ast_str *write_transpath = ast_str_alloca(256);
977  struct ast_str *read_transpath = ast_str_alloca(256);
978 
979  ast_log(LOG_WARNING,
980  "Channel %s asked to send %s frame when native formats are %s (rd:%s->%s;%s wr:%s->%s;%s)\n",
981  ast_channel_name(ast),
983  ast_format_cap_get_names(ast_channel_nativeformats(ast), &cap_buf),
984  ast_format_get_name(ast_channel_rawreadformat(ast)),
985  ast_format_get_name(ast_channel_readformat(ast)),
986  ast_translate_path_to_str(ast_channel_readtrans(ast), &read_transpath),
987  ast_format_get_name(ast_channel_writeformat(ast)),
988  ast_format_get_name(ast_channel_rawwriteformat(ast)),
989  ast_translate_path_to_str(ast_channel_writetrans(ast), &write_transpath));
990  return 0;
991  } else if (media->write_callback) {
992  res = media->write_callback(session, media, frame);
993 
994  }
995  break;
996  case AST_FRAME_VIDEO:
997  if (!media) {
998  return 0;
999  } else if (media->type != AST_MEDIA_TYPE_VIDEO) {
1000  ast_debug(3, "Channel %s stream %d is of type '%s', not video!\n",
1001  ast_channel_name(ast), stream_num, ast_codec_media_type2str(media->type));
1002  return 0;
1003  } else if (media->write_callback) {
1004  res = media->write_callback(session, media, frame);
1005  }
1006  break;
1007  case AST_FRAME_MODEM:
1008  if (!media) {
1009  return 0;
1010  } else if (media->type != AST_MEDIA_TYPE_IMAGE) {
1011  ast_debug(3, "Channel %s stream %d is of type '%s', not image!\n",
1012  ast_channel_name(ast), stream_num, ast_codec_media_type2str(media->type));
1013  return 0;
1014  } else if (media->write_callback) {
1015  res = media->write_callback(session, media, frame);
1016  }
1017  break;
1018  case AST_FRAME_CNG:
1019  break;
1020  case AST_FRAME_RTCP:
1021  /* We only support writing out feedback */
1022  if (frame->subclass.integer != AST_RTP_RTCP_PSFB || !media) {
1023  return 0;
1024  } else if (media->type != AST_MEDIA_TYPE_VIDEO) {
1025  ast_debug(3, "Channel %s stream %d is of type '%s', not video! Unable to write RTCP feedback.\n",
1026  ast_channel_name(ast), stream_num, ast_codec_media_type2str(media->type));
1027  return 0;
1028  } else if (media->write_callback) {
1029  res = media->write_callback(session, media, frame);
1030  }
1031  break;
1032  default:
1033  ast_log(LOG_WARNING, "Can't send %u type frames with PJSIP\n", frame->frametype);
1034  break;
1035  }
1036 
1037  return res;
1038 }
1039 
1040 static int chan_pjsip_write(struct ast_channel *ast, struct ast_frame *frame)
1041 {
1042  return chan_pjsip_write_stream(ast, -1, frame);
1043 }
1044 
1045 /*! \brief Function called by core to change the underlying owner channel */
1046 static int chan_pjsip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
1047 {
1048  struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(newchan);
1049 
1050  if (channel->session->channel != oldchan) {
1051  return -1;
1052  }
1053 
1054  /*
1055  * The masquerade has suspended the channel's session
1056  * serializer so we can safely change it outside of
1057  * the serializer thread.
1058  */
1059  channel->session->channel = newchan;
1060 
1061  set_channel_on_rtp_instance(channel->session, ast_channel_uniqueid(newchan));
1062 
1063  return 0;
1064 }
1065 
1066 /*! AO2 hash function for on hold UIDs */
1067 static int uid_hold_hash_fn(const void *obj, const int flags)
1068 {
1069  const char *key = obj;
1070 
1071  switch (flags & OBJ_SEARCH_MASK) {
1072  case OBJ_SEARCH_KEY:
1073  break;
1074  case OBJ_SEARCH_OBJECT:
1075  break;
1076  default:
1077  /* Hash can only work on something with a full key. */
1078  ast_assert(0);
1079  return 0;
1080  }
1081  return ast_str_hash(key);
1082 }
1083 
1084 /*! AO2 sort function for on hold UIDs */
1085 static int uid_hold_sort_fn(const void *obj_left, const void *obj_right, const int flags)
1086 {
1087  const char *left = obj_left;
1088  const char *right = obj_right;
1089  int cmp;
1090 
1091  switch (flags & OBJ_SEARCH_MASK) {
1092  case OBJ_SEARCH_OBJECT:
1093  case OBJ_SEARCH_KEY:
1094  cmp = strcmp(left, right);
1095  break;
1097  cmp = strncmp(left, right, strlen(right));
1098  break;
1099  default:
1100  /* Sort can only work on something with a full or partial key. */
1101  ast_assert(0);
1102  cmp = 0;
1103  break;
1104  }
1105  return cmp;
1106 }
1107 
1108 static struct ao2_container *pjsip_uids_onhold;
1109 
1110 /*!
1111  * \brief Add a channel ID to the list of PJSIP channels on hold
1112  *
1113  * \param chan_uid - Unique ID of the channel being put into the hold list
1114  *
1115  * \retval 0 Channel has been added to or was already in the hold list
1116  * \retval -1 Failed to add channel to the hold list
1117  */
1118 static int chan_pjsip_add_hold(const char *chan_uid)
1119 {
1120  RAII_VAR(char *, hold_uid, NULL, ao2_cleanup);
1121 
1122  hold_uid = ao2_find(pjsip_uids_onhold, chan_uid, OBJ_SEARCH_KEY);
1123  if (hold_uid) {
1124  /* Device is already on hold. Nothing to do. */
1125  return 0;
1126  }
1127 
1128  /* Device wasn't in hold list already. Create a new one. */
1129  hold_uid = ao2_alloc_options(strlen(chan_uid) + 1, NULL,
1131  if (!hold_uid) {
1132  return -1;
1133  }
1134 
1135  ast_copy_string(hold_uid, chan_uid, strlen(chan_uid) + 1);
1136 
1137  if (ao2_link(pjsip_uids_onhold, hold_uid) == 0) {
1138  return -1;
1139  }
1140 
1141  return 0;
1142 }
1143 
1144 /*!
1145  * \brief Remove a channel ID from the list of PJSIP channels on hold
1146  *
1147  * \param chan_uid - Unique ID of the channel being taken out of the hold list
1148  */
1149 static void chan_pjsip_remove_hold(const char *chan_uid)
1150 {
1151  ao2_find(pjsip_uids_onhold, chan_uid, OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NODATA);
1152 }
1153 
1154 /*!
1155  * \brief Determine whether a channel ID is in the list of PJSIP channels on hold
1156  *
1157  * \param chan_uid - Channel being checked
1158  *
1159  * \retval 0 The channel is not in the hold list
1160  * \retval 1 The channel is in the hold list
1161  */
1162 static int chan_pjsip_get_hold(const char *chan_uid)
1163 {
1164  RAII_VAR(char *, hold_uid, NULL, ao2_cleanup);
1165 
1166  hold_uid = ao2_find(pjsip_uids_onhold, chan_uid, OBJ_SEARCH_KEY);
1167  if (!hold_uid) {
1168  return 0;
1169  }
1170 
1171  return 1;
1172 }
1173 
1174 /*! \brief Function called to get the device state of an endpoint */
1175 static int chan_pjsip_devicestate(const char *data)
1176 {
1177  RAII_VAR(struct ast_sip_endpoint *, endpoint, ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", data), ao2_cleanup);
1179  RAII_VAR(struct ast_endpoint_snapshot *, endpoint_snapshot, NULL, ao2_cleanup);
1180  struct ast_devstate_aggregate aggregate;
1181  int num, inuse = 0;
1182 
1183  if (!endpoint) {
1184  return AST_DEVICE_INVALID;
1185  }
1186 
1187  endpoint_snapshot = ast_endpoint_latest_snapshot(ast_endpoint_get_tech(endpoint->persistent),
1188  ast_endpoint_get_resource(endpoint->persistent));
1189 
1190  if (!endpoint_snapshot) {
1191  return AST_DEVICE_INVALID;
1192  }
1193 
1194  if (endpoint_snapshot->state == AST_ENDPOINT_OFFLINE) {
1195  state = AST_DEVICE_UNAVAILABLE;
1196  } else if (endpoint_snapshot->state == AST_ENDPOINT_ONLINE) {
1197  state = AST_DEVICE_NOT_INUSE;
1198  }
1199 
1200  if (!endpoint_snapshot->num_channels) {
1201  return state;
1202  }
1203 
1204  ast_devstate_aggregate_init(&aggregate);
1205 
1206  for (num = 0; num < endpoint_snapshot->num_channels; num++) {
1207  struct ast_channel_snapshot *snapshot;
1208 
1209  snapshot = ast_channel_snapshot_get_latest(endpoint_snapshot->channel_ids[num]);
1210  if (!snapshot) {
1211  continue;
1212  }
1213 
1214  if (chan_pjsip_get_hold(snapshot->base->uniqueid)) {
1216  } else {
1217  ast_devstate_aggregate_add(&aggregate, ast_state_chan2dev(snapshot->state));
1218  }
1219 
1220  if ((snapshot->state == AST_STATE_UP) || (snapshot->state == AST_STATE_RING) ||
1221  (snapshot->state == AST_STATE_BUSY)) {
1222  inuse++;
1223  }
1224 
1225  ao2_ref(snapshot, -1);
1226  }
1227 
1228  if (endpoint->devicestate_busy_at && (inuse == endpoint->devicestate_busy_at)) {
1229  state = AST_DEVICE_BUSY;
1230  } else if (ast_devstate_aggregate_result(&aggregate) != AST_DEVICE_INVALID) {
1231  state = ast_devstate_aggregate_result(&aggregate);
1232  }
1233 
1234  return state;
1235 }
1236 
1237 /*! \brief Function called to query options on a channel */
1238 static int chan_pjsip_queryoption(struct ast_channel *ast, int option, void *data, int *datalen)
1239 {
1240  struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
1241  int res = -1;
1243 
1244  if (!channel) {
1245  return -1;
1246  }
1247 
1248  switch (option) {
1249  case AST_OPTION_T38_STATE:
1250  if (channel->session->endpoint->media.t38.enabled) {
1251  switch (channel->session->t38state) {
1252  case T38_LOCAL_REINVITE:
1253  case T38_PEER_REINVITE:
1254  state = T38_STATE_NEGOTIATING;
1255  break;
1256  case T38_ENABLED:
1257  state = T38_STATE_NEGOTIATED;
1258  break;
1259  case T38_REJECTED:
1260  state = T38_STATE_REJECTED;
1261  break;
1262  default:
1263  state = T38_STATE_UNKNOWN;
1264  break;
1265  }
1266  }
1267 
1268  *((enum ast_t38_state *) data) = state;
1269  res = 0;
1270 
1271  break;
1272  default:
1273  break;
1274  }
1275 
1276  return res;
1277 }
1278 
1279 static const char *chan_pjsip_get_uniqueid(struct ast_channel *ast)
1280 {
1281  struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
1282  char *uniqueid = ast_threadstorage_get(&uniqueid_threadbuf, UNIQUEID_BUFSIZE);
1283 
1284  if (!channel || !uniqueid) {
1285  return "";
1286  }
1287 
1288  ast_copy_pj_str(uniqueid, &channel->session->inv_session->dlg->call_id->id, UNIQUEID_BUFSIZE);
1289 
1290  return uniqueid;
1291 }
1292 
1294  struct ast_sip_session *session;
1295  int condition;
1296  int response_code;
1297  void *frame_data;
1298  size_t datalen;
1299 };
1300 
1301 static void indicate_data_destroy(void *obj)
1302 {
1303  struct indicate_data *ind_data = obj;
1304 
1305  ast_free(ind_data->frame_data);
1306  ao2_ref(ind_data->session, -1);
1307 }
1308 
1309 static struct indicate_data *indicate_data_alloc(struct ast_sip_session *session,
1310  int condition, int response_code, const void *frame_data, size_t datalen)
1311 {
1312  struct indicate_data *ind_data = ao2_alloc(sizeof(*ind_data), indicate_data_destroy);
1313 
1314  if (!ind_data) {
1315  return NULL;
1316  }
1317 
1318  ind_data->frame_data = ast_malloc(datalen);
1319  if (!ind_data->frame_data) {
1320  ao2_ref(ind_data, -1);
1321  return NULL;
1322  }
1323 
1324  memcpy(ind_data->frame_data, frame_data, datalen);
1325  ind_data->datalen = datalen;
1326  ind_data->condition = condition;
1327  ind_data->response_code = response_code;
1328  ao2_ref(session, +1);
1329  ind_data->session = session;
1330 
1331  return ind_data;
1332 }
1333 
1334 static int indicate(void *data)
1335 {
1336  pjsip_tx_data *packet = NULL;
1337  struct indicate_data *ind_data = data;
1338  struct ast_sip_session *session = ind_data->session;
1339  int response_code = ind_data->response_code;
1340 
1341  if ((session->inv_session->state != PJSIP_INV_STATE_DISCONNECTED) &&
1342  (pjsip_inv_answer(session->inv_session, response_code, NULL, NULL, &packet) == PJ_SUCCESS)) {
1343  ast_sip_session_send_response(session, packet);
1344  }
1345 
1346  ao2_ref(ind_data, -1);
1347 
1348  return 0;
1349 }
1350 
1351 /*! \brief Send SIP INFO with video update request */
1352 static int transmit_info_with_vidupdate(void *data)
1353 {
1354  const char * xml =
1355  "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n"
1356  " <media_control>\r\n"
1357  " <vc_primitive>\r\n"
1358  " <to_encoder>\r\n"
1359  " <picture_fast_update/>\r\n"
1360  " </to_encoder>\r\n"
1361  " </vc_primitive>\r\n"
1362  " </media_control>\r\n";
1363 
1364  const struct ast_sip_body body = {
1365  .type = "application",
1366  .subtype = "media_control+xml",
1367  .body_text = xml
1368  };
1369 
1370  RAII_VAR(struct ast_sip_session *, session, data, ao2_cleanup);
1371  struct pjsip_tx_data *tdata;
1372 
1373  if (session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
1374  ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
1375  session->inv_session->cause,
1376  pjsip_get_status_text(session->inv_session->cause)->ptr);
1377  return -1;
1378  }
1379 
1380  if (ast_sip_create_request("INFO", session->inv_session->dlg, session->endpoint, NULL, NULL, &tdata)) {
1381  ast_log(LOG_ERROR, "Could not create text video update INFO request\n");
1382  return -1;
1383  }
1384  if (ast_sip_add_body(tdata, &body)) {
1385  ast_log(LOG_ERROR, "Could not add body to text video update INFO request\n");
1386  return -1;
1387  }
1388  ast_sip_session_send_request(session, tdata);
1389 
1390  return 0;
1391 }
1392 
1393 /*!
1394  * \internal
1395  * \brief TRUE if a COLP update can be sent to the peer.
1396  * \since 13.3.0
1397  *
1398  * \param session The session to see if the COLP update is allowed.
1399  *
1400  * \retval 0 Update is not allowed.
1401  * \retval 1 Update is allowed.
1402  */
1403 static int is_colp_update_allowed(struct ast_sip_session *session)
1404 {
1405  struct ast_party_id connected_id;
1406  int update_allowed = 0;
1407 
1408  if (!session->endpoint->id.send_connected_line
1409  || (!session->endpoint->id.send_pai && !session->endpoint->id.send_rpid)) {
1410  return 0;
1411  }
1412 
1413  /*
1414  * Check if privacy allows the update. Check while the channel
1415  * is locked so we can work with the shallow connected_id copy.
1416  */
1417  ast_channel_lock(session->channel);
1418  connected_id = ast_channel_connected_effective_id(session->channel);
1419  if (connected_id.number.valid
1420  && (session->endpoint->id.trust_outbound
1421  || (ast_party_id_presentation(&connected_id) & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED)) {
1422  update_allowed = 1;
1423  }
1424  ast_channel_unlock(session->channel);
1425 
1426  return update_allowed;
1427 }
1428 
1429 /*! \brief Update connected line information */
1431 {
1432  struct ast_sip_session *session = data;
1433 
1434  if (session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
1435  ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
1436  session->inv_session->cause,
1437  pjsip_get_status_text(session->inv_session->cause)->ptr);
1438  ao2_ref(session, -1);
1439  return -1;
1440  }
1441 
1442  if (ast_channel_state(session->channel) == AST_STATE_UP
1443  || session->inv_session->role == PJSIP_ROLE_UAC) {
1444  if (is_colp_update_allowed(session)) {
1445  enum ast_sip_session_refresh_method method;
1446  int generate_new_sdp;
1447 
1448  method = session->endpoint->id.refresh_method;
1449  if (session->inv_session->options & PJSIP_INV_SUPPORT_UPDATE) {
1450  method = AST_SIP_SESSION_REFRESH_METHOD_UPDATE;
1451  }
1452 
1453  /* Only the INVITE method actually needs SDP, UPDATE can do without */
1454  generate_new_sdp = (method == AST_SIP_SESSION_REFRESH_METHOD_INVITE);
1455 
1456  ast_sip_session_refresh(session, NULL, NULL, NULL, method, generate_new_sdp, NULL);
1457  }
1458  } else if (session->endpoint->id.rpid_immediate
1459  && session->inv_session->state != PJSIP_INV_STATE_DISCONNECTED
1460  && is_colp_update_allowed(session)) {
1461  int response_code = 0;
1462 
1463  if (ast_channel_state(session->channel) == AST_STATE_RING) {
1464  response_code = !session->endpoint->inband_progress ? 180 : 183;
1465  } else if (ast_channel_state(session->channel) == AST_STATE_RINGING) {
1466  response_code = 183;
1467  }
1468 
1469  if (response_code) {
1470  struct pjsip_tx_data *packet = NULL;
1471 
1472  if (pjsip_inv_answer(session->inv_session, response_code, NULL, NULL, &packet) == PJ_SUCCESS) {
1473  ast_sip_session_send_response(session, packet);
1474  }
1475  }
1476  }
1477 
1478  ao2_ref(session, -1);
1479  return 0;
1480 }
1481 
1482 /*! \brief Update local hold state and send a re-INVITE with the new SDP */
1483 static int remote_send_hold_refresh(struct ast_sip_session *session, unsigned int held)
1484 {
1485  struct ast_sip_session_media *session_media = session->active_media_state->default_session[AST_MEDIA_TYPE_AUDIO];
1486  if (session_media) {
1487  session_media->locally_held = held;
1488  }
1489  ast_sip_session_refresh(session, NULL, NULL, NULL, AST_SIP_SESSION_REFRESH_METHOD_INVITE, 1, NULL);
1490  ao2_ref(session, -1);
1491 
1492  return 0;
1493 }
1494 
1495 /*! \brief Update local hold state to be held */
1496 static int remote_send_hold(void *data)
1497 {
1498  return remote_send_hold_refresh(data, 1);
1499 }
1500 
1501 /*! \brief Update local hold state to be unheld */
1502 static int remote_send_unhold(void *data)
1503 {
1504  return remote_send_hold_refresh(data, 0);
1505 }
1506 
1508  struct ast_sip_session *session;
1509  struct ast_sip_session_media_state *media_state;
1510 };
1511 
1512 static void topology_change_refresh_data_free(struct topology_change_refresh_data *refresh_data)
1513 {
1514  ao2_cleanup(refresh_data->session);
1515 
1516  ast_sip_session_media_state_free(refresh_data->media_state);
1517  ast_free(refresh_data);
1518 }
1519 
1520 static struct topology_change_refresh_data *topology_change_refresh_data_alloc(
1521  struct ast_sip_session *session, const struct ast_stream_topology *topology)
1522 {
1524 
1525  refresh_data = ast_calloc(1, sizeof(*refresh_data));
1526  if (!refresh_data) {
1527  return NULL;
1528  }
1529 
1530  refresh_data->session = ao2_bump(session);
1531  refresh_data->media_state = ast_sip_session_media_state_alloc();
1532  if (!refresh_data->media_state) {
1533  topology_change_refresh_data_free(refresh_data);
1534  return NULL;
1535  }
1536  refresh_data->media_state->topology = ast_stream_topology_clone(topology);
1537  if (!refresh_data->media_state->topology) {
1538  topology_change_refresh_data_free(refresh_data);
1539  return NULL;
1540  }
1541 
1542  return refresh_data;
1543 }
1544 
1545 static int on_topology_change_response(struct ast_sip_session *session, pjsip_rx_data *rdata)
1546 {
1547  SCOPE_ENTER(3, "%s: Received response code %d. PT: %s AT: %s\n", ast_sip_session_get_name(session),
1548  rdata->msg_info.msg->line.status.code,
1551 
1552 
1553  if (PJSIP_IS_STATUS_IN_CLASS(rdata->msg_info.msg->line.status.code, 200)) {
1554  /* The topology was changed to something new so give notice to what requested
1555  * it so it queries the channel and updates accordingly.
1556  */
1557  if (session->channel) {
1559  SCOPE_EXIT_RTN_VALUE(0, "%s: Queued topology change frame\n", ast_sip_session_get_name(session));
1560  }
1561  SCOPE_EXIT_RTN_VALUE(0, "%s: No channel? Can't queue topology change frame\n", ast_sip_session_get_name(session));
1562  } else if (300 <= rdata->msg_info.msg->line.status.code) {
1563  /* The topology change failed, so drop the current pending media state */
1564  ast_sip_session_media_state_reset(session->pending_media_state);
1565  SCOPE_EXIT_RTN_VALUE(0, "%s: response code > 300. Resetting pending media state\n", ast_sip_session_get_name(session));
1566  }
1567 
1568  SCOPE_EXIT_RTN_VALUE(0, "%s: Nothing to do\n", ast_sip_session_get_name(session));
1569 }
1570 
1571 static int send_topology_change_refresh(void *data)
1572 {
1573  struct topology_change_refresh_data *refresh_data = data;
1574  struct ast_sip_session *session = refresh_data->session;
1576  enum ast_sip_session_refresh_method method = AST_SIP_SESSION_REFRESH_METHOD_INVITE;
1577  int ret;
1578  SCOPE_ENTER(3, "%s: %s\n", ast_sip_session_get_name(session),
1579  ast_str_tmp(256, ast_stream_topology_to_str(refresh_data->media_state->topology, &STR_TMP)));
1580 
1581  /* See RFC 6337, especially section 3.2: If the early media SDP was sent reliably, we are allowed
1582  * to send UPDATEs. Only relevant for AST_STATE_RINGING and AST_STATE_RING - if the channel is UP,
1583  * re-INVITES can be sent.
1584  */
1585  if (session->early_confirmed && (state == AST_STATE_RINGING || state == AST_STATE_RING)) {
1586  method = AST_SIP_SESSION_REFRESH_METHOD_UPDATE;
1587  }
1588 
1589  ret = ast_sip_session_refresh(session, NULL, NULL, on_topology_change_response,
1590  method, 1, refresh_data->media_state);
1591  refresh_data->media_state = NULL;
1592  topology_change_refresh_data_free(refresh_data);
1593 
1594  SCOPE_EXIT_RTN_VALUE(ret, "%s\n", ast_sip_session_get_name(session));
1595 }
1596 
1597 static int handle_topology_request_change(struct ast_sip_session *session,
1598  const struct ast_stream_topology *proposed)
1599 {
1600  struct topology_change_refresh_data *refresh_data;
1601  int res;
1602  SCOPE_ENTER(1);
1603 
1604  refresh_data = topology_change_refresh_data_alloc(session, proposed);
1605  if (!refresh_data) {
1606  SCOPE_EXIT_RTN_VALUE(-1, "Couldn't create refresh_data\n");
1607  }
1608 
1609  res = ast_sip_push_task(session->serializer, send_topology_change_refresh, refresh_data);
1610  if (res) {
1611  topology_change_refresh_data_free(refresh_data);
1612  }
1613  SCOPE_EXIT_RTN_VALUE(res, "RC: %d\n", res);
1614 }
1615 
1616 /* Forward declarations */
1617 static int transmit_info_dtmf(void *data);
1618 static struct info_dtmf_data *info_dtmf_data_alloc(struct ast_sip_session *session, char digit, unsigned int duration);
1619 
1620 /*! \brief Function called by core to ask the channel to indicate some sort of condition */
1621 static int chan_pjsip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
1622 {
1623  struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
1624  struct ast_sip_session_media *media;
1625  int response_code = 0;
1626  int res = 0;
1627  char *device_buf;
1628  size_t device_buf_size;
1629  int i;
1630  const struct ast_stream_topology *topology;
1631  struct ast_frame f = {
1633  .subclass = {
1634  .integer = condition
1635  },
1636  .datalen = datalen,
1637  .data.ptr = (void *)data,
1638  };
1639  char condition_name[256];
1640  unsigned int duration;
1641  char digit;
1642  struct info_dtmf_data *dtmf_data;
1643 
1644  SCOPE_ENTER(3, "%s: Indicated %s\n", ast_channel_name(ast),
1645  ast_frame_subclass2str(&f, condition_name, sizeof(condition_name), NULL, 0));
1646 
1647  switch (condition) {
1648  case AST_CONTROL_RINGING:
1649  if (ast_channel_state(ast) == AST_STATE_RING) {
1650  if (channel->session->endpoint->inband_progress ||
1651  (channel->session->inv_session && channel->session->inv_session->neg &&
1652  pjmedia_sdp_neg_get_state(channel->session->inv_session->neg) == PJMEDIA_SDP_NEG_STATE_DONE)) {
1653  res = -1;
1654  if (ast_sip_get_allow_sending_180_after_183()) {
1655  response_code = 180;
1656  } else {
1657  response_code = 183;
1658  }
1659  } else {
1660  response_code = 180;
1661  }
1662  } else {
1663  res = -1;
1664  }
1666  break;
1667  case AST_CONTROL_BUSY:
1668  if (ast_channel_state(ast) != AST_STATE_UP) {
1669  response_code = 486;
1670  } else {
1671  res = -1;
1672  }
1673  break;
1675  if (ast_channel_state(ast) != AST_STATE_UP) {
1676  response_code = 503;
1677  } else {
1678  res = -1;
1679  }
1680  break;
1682  if (ast_channel_state(ast) != AST_STATE_UP) {
1683  response_code = 484;
1684  } else {
1685  res = -1;
1686  }
1687  break;
1689  if (ast_channel_state(ast) != AST_STATE_UP) {
1690  response_code = 100;
1691  } else {
1692  res = -1;
1693  }
1694  break;
1695  case AST_CONTROL_PROGRESS:
1696  if (ast_channel_state(ast) != AST_STATE_UP) {
1697  response_code = 183;
1698  } else {
1699  res = -1;
1700  }
1702  break;
1703  case AST_CONTROL_FLASH:
1704  duration = 300;
1705  digit = '!';
1706  dtmf_data = info_dtmf_data_alloc(channel->session, digit, duration);
1707 
1708  if (!dtmf_data) {
1709  res = -1;
1710  break;
1711  }
1712 
1713  if (ast_sip_push_task(channel->session->serializer, transmit_info_dtmf, dtmf_data)) {
1714  ast_log(LOG_WARNING, "Error sending FLASH via INFO on channel %s\n", ast_channel_name(ast));
1715  ao2_ref(dtmf_data, -1); /* dtmf_data can't be null here */
1716  res = -1;
1717  }
1718  break;
1719  case AST_CONTROL_VIDUPDATE:
1720  for (i = 0; i < AST_VECTOR_SIZE(&channel->session->active_media_state->sessions); ++i) {
1721  media = AST_VECTOR_GET(&channel->session->active_media_state->sessions, i);
1722  if (!media || media->type != AST_MEDIA_TYPE_VIDEO) {
1723  continue;
1724  }
1725  if (media->rtp) {
1726  /* FIXME: Only use this for VP8. Additional work would have to be done to
1727  * fully support other video codecs */
1728 
1729  if (ast_format_cap_iscompatible_format(ast_channel_nativeformats(ast), ast_format_vp8) != AST_FORMAT_CMP_NOT_EQUAL ||
1730  ast_format_cap_iscompatible_format(ast_channel_nativeformats(ast), ast_format_vp9) != AST_FORMAT_CMP_NOT_EQUAL ||
1731  ast_format_cap_iscompatible_format(ast_channel_nativeformats(ast), ast_format_h265) != AST_FORMAT_CMP_NOT_EQUAL ||
1732  (channel->session->endpoint->media.webrtc &&
1733  ast_format_cap_iscompatible_format(ast_channel_nativeformats(ast), ast_format_h264) != AST_FORMAT_CMP_NOT_EQUAL)) {
1734  /* FIXME Fake RTP write, this will be sent as an RTCP packet. Ideally the
1735  * RTP engine would provide a way to externally write/schedule RTCP
1736  * packets */
1737  struct ast_frame fr;
1740  res = ast_rtp_instance_write(media->rtp, &fr);
1741  } else {
1742  ao2_ref(channel->session, +1);
1744  ao2_cleanup(channel->session);
1745  }
1746  }
1747  ast_test_suite_event_notify("AST_CONTROL_VIDUPDATE", "Result: Success");
1748  } else {
1749  ast_test_suite_event_notify("AST_CONTROL_VIDUPDATE", "Result: Failure");
1750  res = -1;
1751  }
1752  }
1753  /* XXX If there were no video streams, then this should set
1754  * res to -1
1755  */
1756  break;
1758  ao2_ref(channel->session, +1);
1760  ao2_cleanup(channel->session);
1761  }
1762  break;
1764  break;
1766  res = -1;
1767  break;
1769  ast_assert(datalen == sizeof(int));
1770  if (*(int *) data) {
1771  /*
1772  * Masquerade is beginning:
1773  * Wait for session serializer to get suspended.
1774  */
1775  ast_channel_unlock(ast);
1776  ast_sip_session_suspend(channel->session);
1777  ast_channel_lock(ast);
1778  } else {
1779  /*
1780  * Masquerade is complete:
1781  * Unsuspend the session serializer.
1782  */
1783  ast_sip_session_unsuspend(channel->session);
1784  }
1785  break;
1786  case AST_CONTROL_HOLD:
1787  chan_pjsip_add_hold(ast_channel_uniqueid(ast));
1788  device_buf_size = strlen(ast_channel_name(ast)) + 1;
1789  device_buf = alloca(device_buf_size);
1790  ast_channel_get_device_name(ast, device_buf, device_buf_size);
1792  if (!channel->session->moh_passthrough) {
1793  ast_moh_start(ast, data, NULL);
1794  } else {
1796  ast_log(LOG_WARNING, "Could not queue task to remotely put session '%s' on hold with endpoint '%s'\n",
1798  ao2_ref(channel->session, -1);
1799  }
1800  }
1801  break;
1802  case AST_CONTROL_UNHOLD:
1803  chan_pjsip_remove_hold(ast_channel_uniqueid(ast));
1804  device_buf_size = strlen(ast_channel_name(ast)) + 1;
1805  device_buf = alloca(device_buf_size);
1806  ast_channel_get_device_name(ast, device_buf, device_buf_size);
1808  if (!channel->session->moh_passthrough) {
1809  ast_moh_stop(ast);
1810  } else {
1812  ast_log(LOG_WARNING, "Could not queue task to remotely take session '%s' off hold with endpoint '%s'\n",
1814  ao2_ref(channel->session, -1);
1815  }
1816  }
1817  break;
1818  case AST_CONTROL_SRCUPDATE:
1819  break;
1820  case AST_CONTROL_SRCCHANGE:
1821  break;
1823  if (ast_channel_state(ast) != AST_STATE_UP) {
1824  response_code = 181;
1825  } else {
1826  res = -1;
1827  }
1828  break;
1830  res = 0;
1831 
1832  if (channel->session->t38state == T38_PEER_REINVITE) {
1833  const struct ast_control_t38_parameters *parameters = data;
1834 
1835  if (parameters->request_response == AST_T38_REQUEST_PARMS) {
1836  res = AST_T38_REQUEST_PARMS;
1837  }
1838  }
1839 
1840  break;
1842  topology = data;
1843  ast_trace(-1, "%s: New topology: %s\n", ast_channel_name(ast),
1844  ast_str_tmp(256, ast_stream_topology_to_str(topology, &STR_TMP)));
1845  res = handle_topology_request_change(channel->session, topology);
1846  break;
1848  break;
1850  break;
1851  case -1:
1852  res = -1;
1853  break;
1854  default:
1855  ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", condition);
1856  res = -1;
1857  break;
1858  }
1859 
1860  if (response_code) {
1861  struct indicate_data *ind_data = indicate_data_alloc(channel->session, condition, response_code, data, datalen);
1862 
1863  if (!ind_data) {
1864  SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR, "%s: Couldn't alloc indicate data\n", ast_channel_name(ast));
1865  }
1866 
1867  if (ast_sip_push_task(channel->session->serializer, indicate, ind_data)) {
1868  ast_log(LOG_ERROR, "%s: Cannot send response code %d to endpoint %s. Could not queue task properly\n",
1869  ast_channel_name(ast), response_code, ast_sorcery_object_get_id(channel->session->endpoint));
1870  ao2_cleanup(ind_data);
1871  res = -1;
1872  }
1873  }
1874 
1875  SCOPE_EXIT_RTN_VALUE(res, "%s\n", ast_channel_name(ast));
1876 }
1877 
1879  struct ast_sip_session *session;
1880  char *target;
1881 };
1882 
1883 static void transfer_data_destroy(void *obj)
1884 {
1885  struct transfer_data *trnf_data = obj;
1886 
1887  ast_free(trnf_data->target);
1888  ao2_cleanup(trnf_data->session);
1889 }
1890 
1891 static struct transfer_data *transfer_data_alloc(struct ast_sip_session *session, const char *target)
1892 {
1893  struct transfer_data *trnf_data = ao2_alloc(sizeof(*trnf_data), transfer_data_destroy);
1894 
1895  if (!trnf_data) {
1896  return NULL;
1897  }
1898 
1899  if (!(trnf_data->target = ast_strdup(target))) {
1900  ao2_ref(trnf_data, -1);
1901  return NULL;
1902  }
1903 
1904  ao2_ref(session, +1);
1905  trnf_data->session = session;
1906 
1907  return trnf_data;
1908 }
1909 
1910 static void transfer_redirect(struct ast_sip_session *session, const char *target)
1911 {
1912  pjsip_tx_data *packet;
1914  pjsip_contact_hdr *contact;
1915  pj_str_t tmp;
1916 
1917  if (pjsip_inv_end_session(session->inv_session, 302, NULL, &packet) != PJ_SUCCESS
1918  || !packet) {
1919  ast_log(LOG_WARNING, "Failed to redirect PJSIP session for channel %s\n",
1920  ast_channel_name(session->channel));
1921  message = AST_TRANSFER_FAILED;
1922  ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
1923 
1924  return;
1925  }
1926 
1927  if (!(contact = pjsip_msg_find_hdr(packet->msg, PJSIP_H_CONTACT, NULL))) {
1928  contact = pjsip_contact_hdr_create(packet->pool);
1929  }
1930 
1931  pj_strdup2_with_null(packet->pool, &tmp, target);
1932  if (!(contact->uri = pjsip_parse_uri(packet->pool, tmp.ptr, tmp.slen, PJSIP_PARSE_URI_AS_NAMEADDR))) {
1933  ast_log(LOG_WARNING, "Failed to parse destination URI '%s' for channel %s\n",
1934  target, ast_channel_name(session->channel));
1935  message = AST_TRANSFER_FAILED;
1936  ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
1937  pjsip_tx_data_dec_ref(packet);
1938 
1939  return;
1940  }
1941  pjsip_msg_add_hdr(packet->msg, (pjsip_hdr *) contact);
1942 
1943  ast_sip_session_send_response(session, packet);
1944  ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
1945 }
1946 
1947 /*! \brief REFER Callback module, used to attach session data structure to subscription */
1948 static pjsip_module refer_callback_module = {
1949  .name = { "REFER Callback", 14 },
1950  .id = -1,
1951 };
1952 
1953 /*!
1954  * \brief Callback function to report status of implicit REFER-NOTIFY subscription.
1955  *
1956  * This function will be called on any state change in the REFER-NOTIFY subscription.
1957  * Its primary purpose is to report SUCCESS/FAILURE of a transfer initiated via
1958  * \ref transfer_refer as well as to terminate the subscription, if necessary.
1959  */
1960 static void xfer_client_on_evsub_state(pjsip_evsub *sub, pjsip_event *event)
1961 {
1962  struct ast_channel *chan;
1964  int res = 0;
1965 
1966  if (!event) {
1967  return;
1968  }
1969 
1970  chan = pjsip_evsub_get_mod_data(sub, refer_callback_module.id);
1971  if (!chan) {
1972  return;
1973  }
1974 
1975  if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_ACCEPTED) {
1976  /* Check if subscription is suppressed and terminate and send completion code, if so. */
1977  pjsip_rx_data *rdata;
1978  pjsip_generic_string_hdr *refer_sub;
1979  const pj_str_t REFER_SUB = { "Refer-Sub", 9 };
1980 
1981  ast_debug(3, "Transfer accepted on channel %s\n", ast_channel_name(chan));
1982 
1983  /* Check if response message */
1984  if (event->type == PJSIP_EVENT_TSX_STATE && event->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
1985  rdata = event->body.tsx_state.src.rdata;
1986 
1987  /* Find Refer-Sub header */
1988  refer_sub = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &REFER_SUB, NULL);
1989 
1990  /* Check if subscription is suppressed. If it is, the far end will not terminate it,
1991  * and the subscription will remain active until it times out. Terminating it here
1992  * eliminates the unnecessary timeout.
1993  */
1994  if (refer_sub && !pj_stricmp2(&refer_sub->hvalue, "false")) {
1995  /* Since no subscription is desired, assume that call has been transferred successfully. */
1996  /* Channel reference will be released at end of function */
1997  /* Terminate subscription. */
1998  pjsip_evsub_set_mod_data(sub, refer_callback_module.id, NULL);
1999  pjsip_evsub_terminate(sub, PJ_TRUE);
2000  res = -1;
2001  }
2002  }
2003  } else if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_ACTIVE ||
2004  pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) {
2005  /* Check for NOTIFY complete or error. */
2006  pjsip_msg *msg;
2007  pjsip_msg_body *body;
2008  pjsip_status_line status_line = { .code = 0 };
2009  pj_bool_t is_last;
2010  pj_status_t status;
2011 
2012  if (event->type == PJSIP_EVENT_TSX_STATE && event->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
2013  pjsip_rx_data *rdata;
2014 
2015  rdata = event->body.tsx_state.src.rdata;
2016  msg = rdata->msg_info.msg;
2017 
2018  if (msg->type == PJSIP_REQUEST_MSG) {
2019  if (!pjsip_method_cmp(&msg->line.req.method, pjsip_get_notify_method())) {
2020  body = msg->body;
2021  if (body && !pj_stricmp2(&body->content_type.type, "message")
2022  && !pj_stricmp2(&body->content_type.subtype, "sipfrag")) {
2023  pjsip_parse_status_line((char *)body->data, body->len, &status_line);
2024  }
2025  }
2026  } else {
2027  status_line.code = msg->line.status.code;
2028  status_line.reason = msg->line.status.reason;
2029  }
2030  } else {
2031  status_line.code = 500;
2032  status_line.reason = *pjsip_get_status_text(500);
2033  }
2034 
2035  is_last = (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED);
2036  /* If the status code is >= 200, the subscription is finished. */
2037  if (status_line.code >= 200 || is_last) {
2038  res = -1;
2039 
2040  /* If the subscription has terminated, return AST_TRANSFER_SUCCESS for 2XX.
2041  * Return AST_TRANSFER_FAILED for any code < 200.
2042  * Otherwise, return the status code.
2043  * The subscription should not terminate for any code < 200,
2044  * but if it does, that constitutes a failure. */
2045  if (status_line.code < 200) {
2046  message = AST_TRANSFER_FAILED;
2047  } else if (status_line.code >= 300) {
2048  message = status_line.code;
2049  }
2050 
2051  /* If subscription not terminated and subscription is finished (status code >= 200)
2052  * terminate it */
2053  if (!is_last) {
2054  pjsip_tx_data *tdata;
2055 
2056  status = pjsip_evsub_initiate(sub, pjsip_get_subscribe_method(), 0, &tdata);
2057  if (status == PJ_SUCCESS) {
2058  pjsip_evsub_send_request(sub, tdata);
2059  }
2060  }
2061  /* Finished. Remove session from subscription */
2062  pjsip_evsub_set_mod_data(sub, refer_callback_module.id, NULL);
2063  ast_debug(3, "Transfer channel %s completed: %d %.*s (%s)\n",
2064  ast_channel_name(chan),
2065  status_line.code,
2066  (int)status_line.reason.slen, status_line.reason.ptr,
2067  (message == AST_TRANSFER_SUCCESS) ? "Success" : "Failure");
2068  }
2069  }
2070 
2071  if (res) {
2072  ast_queue_control_data(chan, AST_CONTROL_TRANSFER, &message, sizeof(message));
2073  ao2_ref(chan, -1);
2074  }
2075 }
2076 
2077 static void transfer_refer(struct ast_sip_session *session, const char *target)
2078 {
2079  pjsip_evsub *sub;
2081  pj_str_t tmp;
2082  pjsip_tx_data *packet;
2083  const char *ref_by_val;
2084  char local_info[pj_strlen(&session->inv_session->dlg->local.info_str) + 1];
2085  struct pjsip_evsub_user xfer_cb;
2086  struct ast_channel *chan = session->channel;
2087 
2088  pj_bzero(&xfer_cb, sizeof(xfer_cb));
2089  xfer_cb.on_evsub_state = &xfer_client_on_evsub_state;
2090 
2091  if (pjsip_xfer_create_uac(session->inv_session->dlg, &xfer_cb, &sub) != PJ_SUCCESS) {
2092  message = AST_TRANSFER_FAILED;
2093  ast_queue_control_data(chan, AST_CONTROL_TRANSFER, &message, sizeof(message));
2094 
2095  return;
2096  }
2097 
2098  /* refer_callback_module requires a reference to chan
2099  * which will be released in xfer_client_on_evsub_state()
2100  * when the implicit REFER subscription terminates */
2101  pjsip_evsub_set_mod_data(sub, refer_callback_module.id, chan);
2102  ao2_ref(chan, +1);
2103 
2104  if (pjsip_xfer_initiate(sub, pj_cstr(&tmp, target), &packet) != PJ_SUCCESS) {
2105  goto failure;
2106  }
2107 
2108  ref_by_val = pbx_builtin_getvar_helper(chan, "SIPREFERREDBYHDR");
2109  if (!ast_strlen_zero(ref_by_val)) {
2110  ast_sip_add_header(packet, "Referred-By", ref_by_val);
2111  } else {
2112  ast_copy_pj_str(local_info, &session->inv_session->dlg->local.info_str, sizeof(local_info));
2113  ast_sip_add_header(packet, "Referred-By", local_info);
2114  }
2115 
2116  if (pjsip_xfer_send_request(sub, packet) == PJ_SUCCESS) {
2117  return;
2118  }
2119 
2120 failure:
2121  message = AST_TRANSFER_FAILED;
2122  ast_queue_control_data(chan, AST_CONTROL_TRANSFER, &message, sizeof(message));
2123  pjsip_evsub_set_mod_data(sub, refer_callback_module.id, NULL);
2124  pjsip_evsub_terminate(sub, PJ_FALSE);
2125 
2126  ao2_ref(chan, -1);
2127 }
2128 
2129 static int transfer(void *data)
2130 {
2131  struct transfer_data *trnf_data = data;
2132  struct ast_sip_endpoint *endpoint = NULL;
2133  struct ast_sip_contact *contact = NULL;
2134  const char *target = trnf_data->target;
2135 
2136  if (trnf_data->session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
2137  ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
2138  trnf_data->session->inv_session->cause,
2139  pjsip_get_status_text(trnf_data->session->inv_session->cause)->ptr);
2140  } else {
2141  /* See if we have an endpoint; if so, use its contact */
2142  endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", target);
2143  if (endpoint) {
2144  contact = ast_sip_location_retrieve_contact_from_aor_list(endpoint->aors);
2145  if (contact && !ast_strlen_zero(contact->uri)) {
2146  target = contact->uri;
2147  }
2148  }
2149 
2150  if (ast_channel_state(trnf_data->session->channel) == AST_STATE_RING) {
2151  transfer_redirect(trnf_data->session, target);
2152  } else {
2153  transfer_refer(trnf_data->session, target);
2154  }
2155  }
2156 
2157  ao2_ref(trnf_data, -1);
2158  ao2_cleanup(endpoint);
2159  ao2_cleanup(contact);
2160  return 0;
2161 }
2162 
2163 /*! \brief Function called by core for Asterisk initiated transfer */
2164 static int chan_pjsip_transfer(struct ast_channel *chan, const char *target)
2165 {
2166  struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
2167  struct transfer_data *trnf_data = transfer_data_alloc(channel->session, target);
2168 
2169  if (!trnf_data) {
2170  return -1;
2171  }
2172 
2173  if (ast_sip_push_task(channel->session->serializer, transfer, trnf_data)) {
2174  ast_log(LOG_WARNING, "Error requesting transfer\n");
2175  ao2_cleanup(trnf_data);
2176  return -1;
2177  }
2178 
2179  return 0;
2180 }
2181 
2182 /*! \brief Function called by core to start a DTMF digit */
2183 static int chan_pjsip_digit_begin(struct ast_channel *chan, char digit)
2184 {
2185  struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
2186  struct ast_sip_session_media *media;
2187 
2188  media = channel->session->active_media_state->default_session[AST_MEDIA_TYPE_AUDIO];
2189 
2190  switch (channel->session->dtmf) {
2191  case AST_SIP_DTMF_RFC_4733:
2192  if (!media || !media->rtp) {
2193  return 0;
2194  }
2195 
2196  ast_rtp_instance_dtmf_begin(media->rtp, digit);
2197  break;
2198  case AST_SIP_DTMF_AUTO:
2199  if (!media || !media->rtp) {
2200  return 0;
2201  }
2202 
2204  return -1;
2205  }
2206 
2207  ast_rtp_instance_dtmf_begin(media->rtp, digit);
2208  break;
2209  case AST_SIP_DTMF_AUTO_INFO:
2210  if (!media || !media->rtp || (ast_rtp_instance_dtmf_mode_get(media->rtp) == AST_RTP_DTMF_MODE_NONE)) {
2211  return 0;
2212  }
2213  ast_rtp_instance_dtmf_begin(media->rtp, digit);
2214  break;
2215  case AST_SIP_DTMF_NONE:
2216  break;
2217  case AST_SIP_DTMF_INBAND:
2218  return -1;
2219  default:
2220  break;
2221  }
2222 
2223  return 0;
2224 }
2225 
2227  struct ast_sip_session *session;
2228  char digit;
2229  unsigned int duration;
2230 };
2231 
2232 static void info_dtmf_data_destroy(void *obj)
2233 {
2234  struct info_dtmf_data *dtmf_data = obj;
2235  ao2_ref(dtmf_data->session, -1);
2236 }
2237 
2238 static struct info_dtmf_data *info_dtmf_data_alloc(struct ast_sip_session *session, char digit, unsigned int duration)
2239 {
2240  struct info_dtmf_data *dtmf_data = ao2_alloc(sizeof(*dtmf_data), info_dtmf_data_destroy);
2241  if (!dtmf_data) {
2242  return NULL;
2243  }
2244  ao2_ref(session, +1);
2245  dtmf_data->session = session;
2246  dtmf_data->digit = digit;
2247  dtmf_data->duration = duration;
2248  return dtmf_data;
2249 }
2250 
2251 static int transmit_info_dtmf(void *data)
2252 {
2253  RAII_VAR(struct info_dtmf_data *, dtmf_data, data, ao2_cleanup);
2254 
2255  struct ast_sip_session *session = dtmf_data->session;
2256  struct pjsip_tx_data *tdata;
2257 
2258  RAII_VAR(struct ast_str *, body_text, NULL, ast_free_ptr);
2259 
2260  struct ast_sip_body body = {
2261  .type = "application",
2262  .subtype = "dtmf-relay",
2263  };
2264 
2265  if (session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
2266  ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
2267  session->inv_session->cause,
2268  pjsip_get_status_text(session->inv_session->cause)->ptr);
2269  return -1;
2270  }
2271 
2272  if (!(body_text = ast_str_create(32))) {
2273  ast_log(LOG_ERROR, "Could not allocate buffer for INFO DTMF.\n");
2274  return -1;
2275  }
2276  ast_str_set(&body_text, 0, "Signal=%c\r\nDuration=%u\r\n", dtmf_data->digit, dtmf_data->duration);
2277 
2279 
2280  if (ast_sip_create_request("INFO", session->inv_session->dlg, session->endpoint, NULL, NULL, &tdata)) {
2281  ast_log(LOG_ERROR, "Could not create DTMF INFO request\n");
2282  return -1;
2283  }
2284  if (ast_sip_add_body(tdata, &body)) {
2285  ast_log(LOG_ERROR, "Could not add body to DTMF INFO request\n");
2286  pjsip_tx_data_dec_ref(tdata);
2287  return -1;
2288  }
2289  ast_sip_session_send_request(session, tdata);
2290 
2291  return 0;
2292 }
2293 
2294 /*! \brief Function called by core to stop a DTMF digit */
2295 static int chan_pjsip_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
2296 {
2297  struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
2298  struct ast_sip_session_media *media;
2299 
2300  if (!channel || !channel->session) {
2301  /* This happens when the channel is hungup while a DTMF digit is playing. See ASTERISK-28086 */
2302  ast_debug(3, "Channel %s disappeared while calling digit_end\n", ast_channel_name(ast));
2303  return -1;
2304  }
2305 
2306  media = channel->session->active_media_state->default_session[AST_MEDIA_TYPE_AUDIO];
2307 
2308  switch (channel->session->dtmf) {
2309  case AST_SIP_DTMF_AUTO_INFO:
2310  {
2311  if (!media || !media->rtp) {
2312  return 0;
2313  }
2314 
2316  ast_debug(3, "Told to send end of digit on Auto-Info channel %s RFC4733 negotiated so using it.\n", ast_channel_name(ast));
2317  ast_rtp_instance_dtmf_end_with_duration(media->rtp, digit, duration);
2318  break;
2319  }
2320  /* If RFC_4733 was not negotiated, fail through to the DTMF_INFO processing */
2321  ast_debug(3, "Told to send end of digit on Auto-Info channel %s RFC4733 NOT negotiated using INFO instead.\n", ast_channel_name(ast));
2322  }
2323 
2324  case AST_SIP_DTMF_INFO:
2325  {
2326  struct info_dtmf_data *dtmf_data = info_dtmf_data_alloc(channel->session, digit, duration);
2327 
2328  if (!dtmf_data) {
2329  return -1;
2330  }
2331 
2332  if (ast_sip_push_task(channel->session->serializer, transmit_info_dtmf, dtmf_data)) {
2333  ast_log(LOG_WARNING, "Error sending DTMF via INFO.\n");
2334  ao2_cleanup(dtmf_data);
2335  return -1;
2336  }
2337  break;
2338  }
2339  case AST_SIP_DTMF_RFC_4733:
2340  if (!media || !media->rtp) {
2341  return 0;
2342  }
2343 
2344  ast_rtp_instance_dtmf_end_with_duration(media->rtp, digit, duration);
2345  break;
2346  case AST_SIP_DTMF_AUTO:
2347  if (!media || !media->rtp) {
2348  return 0;
2349  }
2350 
2352  return -1;
2353  }
2354 
2355  ast_rtp_instance_dtmf_end_with_duration(media->rtp, digit, duration);
2356  break;
2357  case AST_SIP_DTMF_NONE:
2358  break;
2359  case AST_SIP_DTMF_INBAND:
2360  return -1;
2361  }
2362 
2363  return 0;
2364 }
2365 
2366 static void update_initial_connected_line(struct ast_sip_session *session)
2367 {
2368  struct ast_party_connected_line connected;
2369 
2370  /*
2371  * Use the channel CALLERID() as the initial connected line data.
2372  * The core or a predial handler may have supplied missing values
2373  * from the session->endpoint->id.self about who we are calling.
2374  */
2375  ast_channel_lock(session->channel);
2376  ast_party_id_copy(&session->id, &ast_channel_caller(session->channel)->id);
2377  ast_channel_unlock(session->channel);
2378 
2379  /* Supply initial connected line information if available. */
2380  if (!session->id.number.valid && !session->id.name.valid) {
2381  return;
2382  }
2383 
2384  ast_party_connected_line_init(&connected);
2385  connected.id = session->id;
2386  connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
2387 
2388  ast_channel_queue_connected_line_update(session->channel, &connected, NULL);
2389 }
2390 
2391 static int call(void *data)
2392 {
2393  struct ast_sip_channel_pvt *channel = data;
2394  struct ast_sip_session *session = channel->session;
2395  pjsip_tx_data *tdata;
2396  int res = 0;
2397  SCOPE_ENTER(1, "%s Topology: %s\n",
2398  ast_sip_session_get_name(session),
2400  );
2401 
2402 
2403  res = ast_sip_session_create_invite(session, &tdata);
2404 
2405  if (res) {
2406  ast_set_hangupsource(session->channel, ast_channel_name(session->channel), 0);
2407  ast_queue_hangup(session->channel);
2408  } else {
2409  set_channel_on_rtp_instance(session, ast_channel_uniqueid(session->channel));
2410  update_initial_connected_line(session);
2411  ast_sip_session_send_request(session, tdata);
2412  }
2413  ao2_ref(channel, -1);
2414  SCOPE_EXIT_RTN_VALUE(res, "RC: %d\n", res);
2415 }
2416 
2417 /*! \brief Function called by core to actually start calling a remote party */
2418 static int chan_pjsip_call(struct ast_channel *ast, const char *dest, int timeout)
2419 {
2420  struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
2421  SCOPE_ENTER(1, "%s Topology: %s\n", ast_sip_session_get_name(channel->session),
2423 
2424  ao2_ref(channel, +1);
2425  if (ast_sip_push_task(channel->session->serializer, call, channel)) {
2426  ast_log(LOG_WARNING, "Error attempting to place outbound call to '%s'\n", dest);
2427  ao2_cleanup(channel);
2428  SCOPE_EXIT_RTN_VALUE(-1, "Couldn't push task\n");
2429  }
2430 
2431  SCOPE_EXIT_RTN_VALUE(0, "'call' task pushed\n");
2432 }
2433 
2434 /*! \brief Internal function which translates from Asterisk cause codes to SIP response codes */
2435 static int hangup_cause2sip(int cause)
2436 {
2437  switch (cause) {
2438  case AST_CAUSE_UNALLOCATED: /* 1 */
2439  case AST_CAUSE_NO_ROUTE_DESTINATION: /* 3 IAX2: Can't find extension in context */
2440  case AST_CAUSE_NO_ROUTE_TRANSIT_NET: /* 2 */
2441  return 404;
2442  case AST_CAUSE_CONGESTION: /* 34 */
2443  case AST_CAUSE_SWITCH_CONGESTION: /* 42 */
2444  return 503;
2445  case AST_CAUSE_NO_USER_RESPONSE: /* 18 */
2446  return 408;
2447  case AST_CAUSE_NO_ANSWER: /* 19 */
2448  case AST_CAUSE_UNREGISTERED: /* 20 */
2449  return 480;
2450  case AST_CAUSE_CALL_REJECTED: /* 21 */
2451  return 403;
2452  case AST_CAUSE_NUMBER_CHANGED: /* 22 */
2453  return 410;
2454  case AST_CAUSE_NORMAL_UNSPECIFIED: /* 31 */
2455  return 480;
2456  case AST_CAUSE_INVALID_NUMBER_FORMAT:
2457  return 484;
2458  case AST_CAUSE_USER_BUSY:
2459  return 486;
2460  case AST_CAUSE_FAILURE:
2461  return 500;
2462  case AST_CAUSE_FACILITY_REJECTED: /* 29 */
2463  return 501;
2464  case AST_CAUSE_CHAN_NOT_IMPLEMENTED:
2465  return 503;
2466  case AST_CAUSE_DESTINATION_OUT_OF_ORDER:
2467  return 502;
2468  case AST_CAUSE_BEARERCAPABILITY_NOTAVAIL: /* Can't find codec to connect to host */
2469  return 488;
2470  case AST_CAUSE_INTERWORKING: /* Unspecified Interworking issues */
2471  return 500;
2472  case AST_CAUSE_NOTDEFINED:
2473  default:
2474  ast_debug(1, "AST hangup cause %d (no match found in PJSIP)\n", cause);
2475  return 0;
2476  }
2477 
2478  /* Never reached */
2479  return 0;
2480 }
2481 
2482 struct hangup_data {
2483  int cause;
2484  struct ast_channel *chan;
2485 };
2486 
2487 static void hangup_data_destroy(void *obj)
2488 {
2489  struct hangup_data *h_data = obj;
2490 
2491  h_data->chan = ast_channel_unref(h_data->chan);
2492 }
2493 
2494 static struct hangup_data *hangup_data_alloc(int cause, struct ast_channel *chan)
2495 {
2496  struct hangup_data *h_data = ao2_alloc(sizeof(*h_data), hangup_data_destroy);
2497 
2498  if (!h_data) {
2499  return NULL;
2500  }
2501 
2502  h_data->cause = cause;
2503  h_data->chan = ast_channel_ref(chan);
2504 
2505  return h_data;
2506 }
2507 
2508 /*! \brief Clear a channel from a session along with its PVT */
2509 static void clear_session_and_channel(struct ast_sip_session *session, struct ast_channel *ast)
2510 {
2511  session->channel = NULL;
2512  set_channel_on_rtp_instance(session, "");
2513  ast_channel_tech_pvt_set(ast, NULL);
2514 }
2515 
2516 static int hangup(void *data)
2517 {
2518  struct hangup_data *h_data = data;
2519  struct ast_channel *ast = h_data->chan;
2520  struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
2521  SCOPE_ENTER(1, "%s\n", ast_channel_name(ast));
2522 
2523  /*
2524  * Before cleaning we have to ensure that channel or its session is not NULL
2525  * we have seen rare case when taskprocessor calls hangup but channel is NULL
2526  * due to SIP session timeout and answer happening at the same time
2527  */
2528  if (channel) {
2529  struct ast_sip_session *session = channel->session;
2530  if (session) {
2531  int cause = h_data->cause;
2532 
2533  if (channel->session->active_media_state &&
2534  channel->session->active_media_state->default_session[AST_MEDIA_TYPE_AUDIO]) {
2535  struct ast_sip_session_media *media =
2536  channel->session->active_media_state->default_session[AST_MEDIA_TYPE_AUDIO];
2537  if (media->rtp) {
2538  ast_rtp_instance_set_stats_vars(ast, media->rtp);
2539  }
2540  }
2541 
2542  /*
2543  * It's possible that session_terminate might cause the session to be destroyed
2544  * immediately so we need to keep a reference to it so we can NULL session->channel
2545  * afterwards.
2546  */
2547  ast_sip_session_terminate(ao2_bump(session), cause);
2548  clear_session_and_channel(session, ast);
2549  ao2_cleanup(session);
2550  }
2551  ao2_cleanup(channel);
2552  }
2553  ao2_cleanup(h_data);
2554  SCOPE_EXIT_RTN_VALUE(0);
2555 }
2556 
2557 /*! \brief Function called by core to hang up a PJSIP session */
2558 static int chan_pjsip_hangup(struct ast_channel *ast)
2559 {
2560  struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
2561  int cause;
2562  struct hangup_data *h_data;
2563  SCOPE_ENTER(1, "%s\n", ast_channel_name(ast));
2564 
2565  if (!channel || !channel->session) {
2566  SCOPE_EXIT_RTN_VALUE(-1, "No channel or session\n");
2567  }
2568 
2569  cause = hangup_cause2sip(ast_channel_hangupcause(channel->session->channel));
2570  h_data = hangup_data_alloc(cause, ast);
2571 
2572  if (!h_data) {
2573  goto failure;
2574  }
2575 
2576  if (ast_sip_push_task(channel->session->serializer, hangup, h_data)) {
2577  ast_log(LOG_WARNING, "Unable to push hangup task to the threadpool. Expect bad things\n");
2578  goto failure;
2579  }
2580 
2581  SCOPE_EXIT_RTN_VALUE(0, "Cause: %d\n", cause);
2582 
2583 failure:
2584  /* Go ahead and do our cleanup of the session and channel even if we're not going
2585  * to be able to send our SIP request/response
2586  */
2587  clear_session_and_channel(channel->session, ast);
2588  ao2_cleanup(channel);
2589  ao2_cleanup(h_data);
2590 
2591  SCOPE_EXIT_RTN_VALUE(-1, "Cause: %d\n", cause);
2592 }
2593 
2595  struct ast_sip_session *session;
2596  struct ast_stream_topology *topology;
2597  const char *dest;
2598  int cause;
2599 };
2600 
2601 static int request(void *obj)
2602 {
2603  struct request_data *req_data = obj;
2604  struct ast_sip_session *session = NULL;
2605  char *tmp = ast_strdupa(req_data->dest), *endpoint_name = NULL, *request_user = NULL;
2606  struct ast_sip_endpoint *endpoint;
2607 
2608  AST_DECLARE_APP_ARGS(args,
2609  AST_APP_ARG(endpoint);
2610  AST_APP_ARG(aor);
2611  );
2612  SCOPE_ENTER(1, "%s\n",tmp);
2613 
2614  if (ast_strlen_zero(tmp)) {
2615  ast_log(LOG_ERROR, "Unable to create PJSIP channel with empty destination\n");
2616  req_data->cause = AST_CAUSE_CHANNEL_UNACCEPTABLE;
2617  SCOPE_EXIT_RTN_VALUE(-1, "Empty destination\n");
2618  }
2619 
2620  AST_NONSTANDARD_APP_ARGS(args, tmp, '/');
2621 
2622  if (ast_sip_get_disable_multi_domain()) {
2623  /* If a request user has been specified extract it from the endpoint name portion */
2624  if ((endpoint_name = strchr(args.endpoint, '@'))) {
2625  request_user = args.endpoint;
2626  *endpoint_name++ = '\0';
2627  } else {
2628  endpoint_name = args.endpoint;
2629  }
2630 
2631  if (ast_strlen_zero(endpoint_name)) {
2632  if (request_user) {
2633  ast_log(LOG_ERROR, "Unable to create PJSIP channel with empty endpoint name: %s@<endpoint-name>\n",
2634  request_user);
2635  } else {
2636  ast_log(LOG_ERROR, "Unable to create PJSIP channel with empty endpoint name\n");
2637  }
2638  req_data->cause = AST_CAUSE_CHANNEL_UNACCEPTABLE;
2639  SCOPE_EXIT_RTN_VALUE(-1, "Empty endpoint name\n");
2640  }
2641  endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint",
2642  endpoint_name);
2643  if (!endpoint) {
2644  ast_log(LOG_ERROR, "Unable to create PJSIP channel - endpoint '%s' was not found\n", endpoint_name);
2645  req_data->cause = AST_CAUSE_NO_ROUTE_DESTINATION;
2646  SCOPE_EXIT_RTN_VALUE(-1, "Endpoint not found\n");
2647  }
2648  } else {
2649  /* First try to find an exact endpoint match, for single (user) or multi-domain (user@domain) */
2650  endpoint_name = args.endpoint;
2651  if (ast_strlen_zero(endpoint_name)) {
2652  ast_log(LOG_ERROR, "Unable to create PJSIP channel with empty endpoint name\n");
2653  req_data->cause = AST_CAUSE_CHANNEL_UNACCEPTABLE;
2654  SCOPE_EXIT_RTN_VALUE(-1, "Empty endpoint name\n");
2655  }
2656  endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint",
2657  endpoint_name);
2658  if (!endpoint) {
2659  /* It seems it's not a multi-domain endpoint or single endpoint exact match,
2660  * it's possible that it's a SIP trunk with a specified user (user@trunkname),
2661  * so extract the user before @ sign.
2662  */
2663  endpoint_name = strchr(args.endpoint, '@');
2664  if (!endpoint_name) {
2665  /*
2666  * Couldn't find an '@' so it had to be an endpoint
2667  * name that doesn't exist.
2668  */
2669  ast_log(LOG_ERROR, "Unable to create PJSIP channel - endpoint '%s' was not found\n",
2670  args.endpoint);
2671  req_data->cause = AST_CAUSE_NO_ROUTE_DESTINATION;
2672  SCOPE_EXIT_RTN_VALUE(-1, "Endpoint not found\n");
2673  }
2674  request_user = args.endpoint;
2675  *endpoint_name++ = '\0';
2676 
2677  if (ast_strlen_zero(endpoint_name)) {
2678  ast_log(LOG_ERROR, "Unable to create PJSIP channel with empty endpoint name: %s@<endpoint-name>\n",
2679  request_user);
2680  req_data->cause = AST_CAUSE_CHANNEL_UNACCEPTABLE;
2681  SCOPE_EXIT_RTN_VALUE(-1, "Empty endpoint name\n");
2682  }
2683 
2684  endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint",
2685  endpoint_name);
2686  if (!endpoint) {
2687  ast_log(LOG_ERROR, "Unable to create PJSIP channel - endpoint '%s' was not found\n", endpoint_name);
2688  req_data->cause = AST_CAUSE_NO_ROUTE_DESTINATION;
2689  SCOPE_EXIT_RTN_VALUE(-1, "Endpoint not found\n");
2690  }
2691  }
2692  }
2693 
2694  session = ast_sip_session_create_outgoing(endpoint, NULL, args.aor, request_user,
2695  req_data->topology);
2696  ao2_ref(endpoint, -1);
2697  if (!session) {
2698  ast_log(LOG_ERROR, "Failed to create outgoing session to endpoint '%s'\n", endpoint_name);
2699  req_data->cause = AST_CAUSE_NO_ROUTE_DESTINATION;
2700  SCOPE_EXIT_RTN_VALUE(-1, "Couldn't create session\n");
2701  }
2702 
2703  req_data->session = session;
2704 
2705  SCOPE_EXIT_RTN_VALUE(0);
2706 }
2707 
2708 /*! \brief Function called by core to create a new outgoing PJSIP session */
2709 static struct ast_channel *chan_pjsip_request_with_stream_topology(const char *type, struct ast_stream_topology *topology, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause)
2710 {
2711  struct request_data req_data;
2712  RAII_VAR(struct ast_sip_session *, session, NULL, ao2_cleanup);
2713  SCOPE_ENTER(1, "%s Topology: %s\n", data,
2714  ast_str_tmp(256, ast_stream_topology_to_str(topology, &STR_TMP)));
2715 
2716  req_data.topology = topology;
2717  req_data.dest = data;
2718  /* Default failure value in case ast_sip_push_task_wait_servant() itself fails. */
2719  req_data.cause = AST_CAUSE_FAILURE;
2720 
2721  if (ast_sip_push_task_wait_servant(NULL, request, &req_data)) {
2722  *cause = req_data.cause;
2723  SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't push task\n");
2724  }
2725 
2726  session = req_data.session;
2727 
2728  if (!(session->channel = chan_pjsip_new(session, AST_STATE_DOWN, NULL, NULL, assignedids, requestor, NULL))) {
2729  /* Session needs to be terminated prematurely */
2730  SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't create channel\n");
2731  }
2732 
2733  SCOPE_EXIT_RTN_VALUE(session->channel, "Channel: %s\n", ast_channel_name(session->channel));
2734 }
2735 
2736 static struct ast_channel *chan_pjsip_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause)
2737 {
2738  struct ast_stream_topology *topology;
2739  struct ast_channel *chan;
2740 
2742  if (!topology) {
2743  return NULL;
2744  }
2745 
2746  chan = chan_pjsip_request_with_stream_topology(type, topology, assignedids, requestor, data, cause);
2747 
2748  ast_stream_topology_free(topology);
2749 
2750  return chan;
2751 }
2752 
2754  struct ast_sip_session *session;
2755  struct ast_msg_data *msg;
2756 };
2757 
2758 static void sendtext_data_destroy(void *obj)
2759 {
2760  struct sendtext_data *data = obj;
2761  ao2_cleanup(data->session);
2762  ast_free(data->msg);
2763 }
2764 
2765 static struct sendtext_data* sendtext_data_create(struct ast_channel *chan,
2766  struct ast_msg_data *msg)
2767 {
2768  struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
2769  struct sendtext_data *data = ao2_alloc(sizeof(*data), sendtext_data_destroy);
2770 
2771  if (!data) {
2772  return NULL;
2773  }
2774 
2775  data->msg = ast_msg_data_dup(msg);
2776  if (!data->msg) {
2777  ao2_cleanup(data);
2778  return NULL;
2779  }
2780  data->session = channel->session;
2781  ao2_ref(data->session, +1);
2782 
2783  return data;
2784 }
2785 
2786 static int sendtext(void *obj)
2787 {
2788  struct sendtext_data *data = obj;
2789  pjsip_tx_data *tdata;
2790  const char *body_text = ast_msg_data_get_attribute(data->msg, AST_MSG_DATA_ATTR_BODY);
2791  const char *content_type = ast_msg_data_get_attribute(data->msg, AST_MSG_DATA_ATTR_CONTENT_TYPE);
2792  char *sep;
2793  struct ast_sip_body body = {
2794  .type = "text",
2795  .subtype = "plain",
2796  .body_text = body_text,
2797  };
2798 
2799  if (!ast_strlen_zero(content_type)) {
2800  sep = strchr(content_type, '/');
2801  if (sep) {
2802  *sep = '\0';
2803  body.type = content_type;
2804  body.subtype = ++sep;
2805  }
2806  }
2807 
2808  if (data->session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
2809  ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
2810  data->session->inv_session->cause,
2811  pjsip_get_status_text(data->session->inv_session->cause)->ptr);
2812  } else {
2813  pjsip_from_hdr *hdr;
2814  pjsip_name_addr *name_addr;
2815  const char *from = ast_msg_data_get_attribute(data->msg, AST_MSG_DATA_ATTR_FROM);
2816  const char *to = ast_msg_data_get_attribute(data->msg, AST_MSG_DATA_ATTR_TO);
2817  int invalidate_tdata = 0;
2818 
2819  ast_sip_create_request("MESSAGE", data->session->inv_session->dlg, data->session->endpoint, NULL, NULL, &tdata);
2820  ast_sip_add_body(tdata, &body);
2821 
2822  /*
2823  * If we have a 'from' in the msg, set the display name in the From
2824  * header to it.
2825  */
2826  if (!ast_strlen_zero(from)) {
2827  hdr = PJSIP_MSG_FROM_HDR(tdata->msg);
2828  name_addr = (pjsip_name_addr *) hdr->uri;
2829  pj_strdup2(tdata->pool, &name_addr->display, from);
2830  invalidate_tdata = 1;
2831  }
2832 
2833  /*
2834  * If we have a 'to' in the msg, set the display name in the To
2835  * header to it.
2836  */
2837  if (!ast_strlen_zero(to)) {
2838  hdr = PJSIP_MSG_TO_HDR(tdata->msg);
2839  name_addr = (pjsip_name_addr *) hdr->uri;
2840  pj_strdup2(tdata->pool, &name_addr->display, to);
2841  invalidate_tdata = 1;
2842  }
2843 
2844  if (invalidate_tdata) {
2845  pjsip_tx_data_invalidate_msg(tdata);
2846  }
2847 
2848  ast_sip_send_request(tdata, data->session->inv_session->dlg, data->session->endpoint, NULL, NULL);
2849  }
2850 
2851  ao2_cleanup(data);
2852 
2853  return 0;
2854 }
2855 
2856 /*! \brief Function called by core to send text on PJSIP session */
2857 static int chan_pjsip_sendtext_data(struct ast_channel *ast, struct ast_msg_data *msg)
2858 {
2859  struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
2860  struct sendtext_data *data = sendtext_data_create(ast, msg);
2861 
2862  ast_debug(1, "Sending MESSAGE from '%s' to '%s:%s': %s\n",
2863  ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_FROM),
2864  ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_TO),
2865  ast_channel_name(ast),
2866  ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_BODY));
2867 
2868  if (!data) {
2869  return -1;
2870  }
2871 
2872  if (ast_sip_push_task(channel->session->serializer, sendtext, data)) {
2873  ao2_ref(data, -1);
2874  return -1;
2875  }
2876  return 0;
2877 }
2878 
2879 static int chan_pjsip_sendtext(struct ast_channel *ast, const char *text)
2880 {
2881  struct ast_msg_data *msg;
2882  int rc;
2883  struct ast_msg_data_attribute attrs[] =
2884  {
2885  {
2886  .type = AST_MSG_DATA_ATTR_BODY,
2887  .value = (char *)text,
2888  }
2889  };
2890 
2891  msg = ast_msg_data_alloc(AST_MSG_DATA_SOURCE_TYPE_UNKNOWN, attrs, ARRAY_LEN(attrs));
2892  if (!msg) {
2893  return -1;
2894  }
2895  rc = chan_pjsip_sendtext_data(ast, msg);
2896  ast_free(msg);
2897 
2898  return rc;
2899 }
2900 
2901 static void chan_pjsip_session_begin(struct ast_sip_session *session)
2902 {
2903  RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
2904  SCOPE_ENTER(1, "%s\n", ast_sip_session_get_name(session));
2905 
2906  if (session->endpoint->media.direct_media.glare_mitigation ==
2907  AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_NONE) {
2908  SCOPE_EXIT_RTN("Direct media no glare mitigation\n");
2909  }
2910 
2911  datastore = ast_sip_session_alloc_datastore(&direct_media_mitigation_info,
2912  "direct_media_glare_mitigation");
2913 
2914  if (!datastore) {
2915  SCOPE_EXIT_RTN("Couldn't create datastore\n");
2916  }
2917 
2918  ast_sip_session_add_datastore(session, datastore);
2919  SCOPE_EXIT_RTN();
2920 }
2921 
2922 /*! \brief Function called when the session ends */
2923 static void chan_pjsip_session_end(struct ast_sip_session *session)
2924 {
2925  SCOPE_ENTER(1, "%s\n", ast_sip_session_get_name(session));
2926 
2927  if (!session->channel) {
2928  SCOPE_EXIT_RTN("No channel\n");
2929  }
2930 
2931 
2932  if (session->active_media_state &&
2933  session->active_media_state->default_session[AST_MEDIA_TYPE_AUDIO]) {
2934  struct ast_sip_session_media *media =
2935  session->active_media_state->default_session[AST_MEDIA_TYPE_AUDIO];
2936  if (media->rtp) {
2937  ast_rtp_instance_set_stats_vars(session->channel, media->rtp);
2938  }
2939  }
2940 
2941  chan_pjsip_remove_hold(ast_channel_uniqueid(session->channel));
2942 
2943  ast_set_hangupsource(session->channel, ast_channel_name(session->channel), 0);
2944  if (!ast_channel_hangupcause(session->channel) && session->inv_session) {
2945  int cause = ast_sip_hangup_sip2cause(session->inv_session->cause);
2946 
2947  ast_queue_hangup_with_cause(session->channel, cause);
2948  } else {
2949  ast_queue_hangup(session->channel);
2950  }
2951 
2952  SCOPE_EXIT_RTN();
2953 }
2954 
2955 static void set_sipdomain_variable(struct ast_sip_session *session)
2956 {
2957  const pj_str_t *host = ast_sip_pjsip_uri_get_hostname(session->request_uri);
2958  size_t size = pj_strlen(host) + 1;
2959  char *domain = ast_alloca(size);
2960 
2961  ast_copy_pj_str(domain, host, size);
2962 
2963  pbx_builtin_setvar_helper(session->channel, "SIPDOMAIN", domain);
2964  return;
2965 }
2966 
2967 /*! \brief Function called when a request is received on the session */
2968 static int chan_pjsip_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
2969 {
2970  RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
2971  struct transport_info_data *transport_data;
2972  pjsip_tx_data *packet = NULL;
2973  SCOPE_ENTER(3, "%s\n", ast_sip_session_get_name(session));
2974 
2975  if (session->channel) {
2976  SCOPE_EXIT_RTN_VALUE(0, "%s: No channel\n", ast_sip_session_get_name(session));
2977  }
2978 
2979  /* Check for a to-tag to determine if this is a reinvite */
2980  if (rdata->msg_info.to->tag.slen) {
2981  /* Weird case. We've received a reinvite but we don't have a channel. The most
2982  * typical case for this happening is that a blind transfer fails, and so the
2983  * transferer attempts to reinvite himself back into the call. We already got
2984  * rid of that channel, and the other side of the call is unrecoverable.
2985  *
2986  * We treat this as a failure, so our best bet is to just hang this call
2987  * up and not create a new channel. Clearing defer_terminate here ensures that
2988  * calling ast_sip_session_terminate() can result in a BYE being sent ASAP.
2989  */
2990  session->defer_terminate = 0;
2991  ast_sip_session_terminate(session, 400);
2992  SCOPE_EXIT_RTN_VALUE(-1, "%s: We have a To tag but no channel. Terminating session\n", ast_sip_session_get_name(session));
2993  }
2994 
2995  datastore = ast_sip_session_alloc_datastore(&transport_info, "transport_info");
2996  if (!datastore) {
2997  SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR, "%s: Couldn't alloc transport_info datastore\n", ast_sip_session_get_name(session));
2998  }
2999 
3000  transport_data = ast_calloc(1, sizeof(*transport_data));
3001  if (!transport_data) {
3002  SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR, "%s: Couldn't alloc transport_info\n", ast_sip_session_get_name(session));
3003  }
3004  pj_sockaddr_cp(&transport_data->local_addr, &rdata->tp_info.transport->local_addr);
3005  pj_sockaddr_cp(&transport_data->remote_addr, &rdata->pkt_info.src_addr);
3006  datastore->data = transport_data;
3007  ast_sip_session_add_datastore(session, datastore);
3008 
3009  if (!(session->channel = chan_pjsip_new(session, AST_STATE_RING, session->exten, NULL, NULL, NULL, NULL))) {
3010  if (pjsip_inv_end_session(session->inv_session, 503, NULL, &packet) == PJ_SUCCESS
3011  && packet) {
3012  ast_sip_session_send_response(session, packet);
3013  }
3014 
3015  SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR, "%s: Failed to allocate new PJSIP channel on incoming SIP INVITE\n",
3016  ast_sip_session_get_name(session));
3017  }
3018 
3019  set_sipdomain_variable(session);
3020 
3021  /* channel gets created on incoming request, but we wait to call start
3022  so other supplements have a chance to run */
3023  SCOPE_EXIT_RTN_VALUE(0, "%s\n", ast_sip_session_get_name(session));
3024 }
3025 
3026 static int call_pickup_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
3027 {
3028  struct ast_features_pickup_config *pickup_cfg;
3029  struct ast_channel *chan;
3030 
3031  /* Check for a to-tag to determine if this is a reinvite */
3032  if (rdata->msg_info.to->tag.slen) {
3033  /* We don't care about reinvites */
3034  return 0;
3035  }
3036 
3037  pickup_cfg = ast_get_chan_features_pickup_config(session->channel);
3038  if (!pickup_cfg) {
3039  ast_log(LOG_ERROR, "Unable to retrieve pickup configuration options. Unable to detect call pickup extension.\n");
3040  return 0;
3041  }
3042 
3043  if (strcmp(session->exten, pickup_cfg->pickupexten)) {
3044  ao2_ref(pickup_cfg, -1);
3045  return 0;
3046  }
3047  ao2_ref(pickup_cfg, -1);
3048 
3049  /* We can't directly use session->channel because the pickup operation will cause a masquerade to occur,
3050  * changing the channel pointer in session to a different channel. To ensure we work on the right channel
3051  * we store a pointer locally before we begin and keep a reference so it remains valid no matter what.
3052  */
3053  chan = ast_channel_ref(session->channel);
3054  if (ast_pickup_call(chan)) {
3055  ast_channel_hangupcause_set(chan, AST_CAUSE_CALL_REJECTED);
3056  } else {
3057  ast_channel_hangupcause_set(chan, AST_CAUSE_NORMAL_CLEARING);
3058  }
3059  /* A hangup always occurs because the pickup operation will have either failed resulting in the call
3060  * needing to be hung up OR the pickup operation was a success and the channel we now have is actually
3061  * the channel that was replaced, which should be hung up since it is literally in limbo not connected
3062  * to anything at all.
3063  */
3064  ast_hangup(chan);
3065  ast_channel_unref(chan);
3066 
3067  return 1;
3068 }
3069 
3070 static struct ast_sip_session_supplement call_pickup_supplement = {
3071  .method = "INVITE",
3072  .priority = AST_SIP_SUPPLEMENT_PRIORITY_LAST - 1,
3073  .incoming_request = call_pickup_incoming_request,
3074 };
3075 
3076 static int pbx_start_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
3077 {
3078  int res;
3079  SCOPE_ENTER(1, "%s\n", ast_sip_session_get_name(session));
3080 
3081  /* Check for a to-tag to determine if this is a reinvite */
3082  if (rdata->msg_info.to->tag.slen) {
3083  /* We don't care about reinvites */
3084  SCOPE_EXIT_RTN_VALUE(0, "Reinvite\n");
3085  }
3086 
3087  res = ast_pbx_start(session->channel);
3088 
3089  switch (res) {
3090  case AST_PBX_FAILED:
3091  ast_log(LOG_WARNING, "Failed to start PBX ;(\n");
3092  ast_channel_hangupcause_set(session->channel, AST_CAUSE_SWITCH_CONGESTION);
3093  ast_hangup(session->channel);
3094  break;
3095  case AST_PBX_CALL_LIMIT:
3096  ast_log(LOG_WARNING, "Failed to start PBX (call limit reached) \n");
3097  ast_channel_hangupcause_set(session->channel, AST_CAUSE_SWITCH_CONGESTION);
3098  ast_hangup(session->channel);
3099  break;
3100  case AST_PBX_SUCCESS:
3101  default:
3102  break;
3103  }
3104 
3105  ast_debug(3, "Started PBX on new PJSIP channel %s\n", ast_channel_name(session->channel));
3106 
3107  SCOPE_EXIT_RTN_VALUE((res == AST_PBX_SUCCESS) ? 0 : -1, "RC: %d\n", res);
3108 }
3109 
3110 static struct ast_sip_session_supplement pbx_start_supplement = {
3111  .method = "INVITE",
3112  .priority = AST_SIP_SUPPLEMENT_PRIORITY_LAST,
3113  .incoming_request = pbx_start_incoming_request,
3114 };
3115 
3116 /*! \brief Function called when a response is received on the session */
3117 static void chan_pjsip_incoming_response_update_cause(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
3118 {
3119  struct pjsip_status_line status = rdata->msg_info.msg->line.status;
3120  struct ast_control_pvt_cause_code *cause_code;
3121  int data_size = sizeof(*cause_code);
3122  SCOPE_ENTER(3, "%s: Status: %d\n", ast_sip_session_get_name(session), status.code);
3123 
3124  if (!session->channel) {
3125  SCOPE_EXIT_RTN("%s: No channel\n", ast_sip_session_get_name(session));
3126  }
3127 
3128  /* Build and send the tech-specific cause information */
3129  /* size of the string making up the cause code is "SIP " number + " " + reason length */
3130  data_size += 4 + 4 + pj_strlen(&status.reason);
3131  cause_code = ast_alloca(data_size);
3132  memset(cause_code, 0, data_size);
3133 
3134  ast_copy_string(cause_code->chan_name, ast_channel_name(session->channel), AST_CHANNEL_NAME);
3135 
3136  snprintf(cause_code->code, data_size - sizeof(*cause_code) + 1, "SIP %d %.*s", status.code,
3137  (int) pj_strlen(&status.reason), pj_strbuf(&status.reason));
3138 
3139  cause_code->ast_cause = ast_sip_hangup_sip2cause(status.code);
3140  ast_queue_control_data(session->channel, AST_CONTROL_PVT_CAUSE_CODE, cause_code, data_size);
3141  ast_channel_hangupcause_hash_set(session->channel, cause_code, data_size);
3142 
3143  SCOPE_EXIT_RTN("%s\n", ast_sip_session_get_name(session));
3144 }
3145 
3146 /*! \brief Function called when a response is received on the session */
3147 static void chan_pjsip_incoming_response(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
3148 {
3149  struct pjsip_status_line status = rdata->msg_info.msg->line.status;
3150  SCOPE_ENTER(3, "%s: Status: %d\n", ast_sip_session_get_name(session), status.code);
3151 
3152  if (!session->channel) {
3153  SCOPE_EXIT_RTN("%s: No channel\n", ast_sip_session_get_name(session));
3154  }
3155 
3156  switch (status.code) {
3157  case 180: {
3158  pjsip_rdata_sdp_info *sdp = pjsip_rdata_get_sdp_info(rdata);
3159  if (sdp && sdp->body.ptr) {
3160  ast_trace(-1, "%s: Queueing PROGRESS\n", ast_sip_session_get_name(session));
3161  session->early_confirmed = pjsip_100rel_is_reliable(rdata) == PJ_TRUE;
3163  } else {
3164  ast_trace(-1, "%s: Queueing RINGING\n", ast_sip_session_get_name(session));
3166  }
3167 
3168  ast_channel_lock(session->channel);
3169  if (ast_channel_state(session->channel) != AST_STATE_UP) {
3171  }
3172  ast_channel_unlock(session->channel);
3173  break;
3174  }
3175  case 183:
3176  if (session->endpoint->ignore_183_without_sdp) {
3177  pjsip_rdata_sdp_info *sdp = pjsip_rdata_get_sdp_info(rdata);
3178  if (sdp && sdp->body.ptr) {
3179  ast_trace(-1, "%s: Queueing PROGRESS\n", ast_sip_session_get_name(session));
3180  ast_trace(1, "%s Method: %.*s Status: %d Queueing PROGRESS with SDP\n", ast_sip_session_get_name(session),
3181  (int)rdata->msg_info.cseq->method.name.slen, rdata->msg_info.cseq->method.name.ptr, status.code);
3182  session->early_confirmed = pjsip_100rel_is_reliable(rdata) == PJ_TRUE;
3184  }
3185  } else {
3186  ast_trace(-1, "%s: Queueing PROGRESS\n", ast_sip_session_get_name(session));
3187  ast_trace(1, "%s Method: %.*s Status: %d Queueing PROGRESS without SDP\n", ast_sip_session_get_name(session),
3188  (int)rdata->msg_info.cseq->method.name.slen, rdata->msg_info.cseq->method.name.ptr, status.code);
3190  }
3191  break;
3192  case 200:
3193  ast_trace(-1, "%s: Queueing ANSWER\n", ast_sip_session_get_name(session));
3195  break;
3196  default:
3197  ast_trace(-1, "%s: Not queueing anything\n", ast_sip_session_get_name(session));
3198  break;
3199  }
3200 
3201  SCOPE_EXIT_RTN("%s\n", ast_sip_session_get_name(session));
3202 }
3203 
3204 static int chan_pjsip_incoming_ack(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
3205 {
3206  SCOPE_ENTER(3, "%s\n", ast_sip_session_get_name(session));
3207 
3208  if (rdata->msg_info.msg->line.req.method.id == PJSIP_ACK_METHOD) {
3209  if (session->endpoint->media.direct_media.enabled && session->channel) {
3210  ast_trace(-1, "%s: Queueing SRCCHANGE\n", ast_sip_session_get_name(session));
3212  }
3213  }
3214  SCOPE_EXIT_RTN_VALUE(0, "%s\n", ast_sip_session_get_name(session));
3215 }
3216 
3217 static int chan_pjsip_incoming_prack(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
3218 {
3219  SCOPE_ENTER(3, "%s\n", ast_sip_session_get_name(session));
3220 
3221  if (pj_strcmp2(&rdata->msg_info.msg->line.req.method.name, "PRACK") == 0 &&
3222  pjmedia_sdp_neg_get_state(session->inv_session->neg) == PJMEDIA_SDP_NEG_STATE_DONE) {
3223 
3224  session->early_confirmed = 1;
3225  }
3226  SCOPE_EXIT_RTN_VALUE(0, "%s\n", ast_sip_session_get_name(session));
3227 }
3228 
3229 static int update_devstate(void *obj, void *arg, int flags)
3230 {
3232  "PJSIP/%s", ast_sorcery_object_get_id(obj));
3233  return 0;
3234 }
3235 
3236 static struct ast_custom_function chan_pjsip_dial_contacts_function = {
3237  .name = "PJSIP_DIAL_CONTACTS",
3239 };
3240 
3241 static struct ast_custom_function chan_pjsip_parse_uri_function = {
3242  .name = "PJSIP_PARSE_URI",
3243  .read = pjsip_acf_parse_uri_read,
3244 };
3245 
3246 static struct ast_custom_function chan_pjsip_parse_uri_from_function = {
3247  .name = "PJSIP_PARSE_URI_FROM",
3248  .read = pjsip_acf_parse_uri_read,
3249 };
3250 
3251 static struct ast_custom_function media_offer_function = {
3252  .name = "PJSIP_MEDIA_OFFER",
3255 };
3256 
3257 static struct ast_custom_function dtmf_mode_function = {
3258  .name = "PJSIP_DTMF_MODE",
3259  .read = pjsip_acf_dtmf_mode_read,
3260  .write = pjsip_acf_dtmf_mode_write
3261 };
3262 
3263 static struct ast_custom_function moh_passthrough_function = {
3264  .name = "PJSIP_MOH_PASSTHROUGH",
3267 };
3268 
3269 static struct ast_custom_function session_refresh_function = {
3270  .name = "PJSIP_SEND_SESSION_REFRESH",
3272 };
3273 
3274 static char *app_pjsip_hangup = "PJSIPHangup";
3275 
3276 /*!
3277  * \brief Load the module
3278  *
3279  * Module loading including tests for configuration or dependencies.
3280  * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
3281  * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
3282  * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
3283  * configuration file or other non-critical problem return
3284  * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
3285  */
3286 static int load_module(void)
3287 {
3288  struct ao2_container *endpoints;
3289 
3291  return AST_MODULE_LOAD_DECLINE;
3292  }
3293 
3294  ast_format_cap_append_by_type(chan_pjsip_tech.capabilities, AST_MEDIA_TYPE_AUDIO);
3295 
3296  ast_rtp_glue_register(&chan_pjsip_rtp_glue);
3297 
3298  if (ast_channel_register(&chan_pjsip_tech)) {
3299  ast_log(LOG_ERROR, "Unable to register channel class %s\n", channel_type);
3300  goto end;
3301  }
3302 
3303  if (ast_custom_function_register(&chan_pjsip_dial_contacts_function)) {
3304  ast_log(LOG_ERROR, "Unable to register PJSIP_DIAL_CONTACTS dialplan function\n");
3305  goto end;
3306  }
3307 
3308  if (ast_custom_function_register(&chan_pjsip_parse_uri_function)) {
3309  ast_log(LOG_ERROR, "Unable to register PJSIP_PARSE_URI dialplan function\n");
3310  goto end;
3311  }
3312 
3313  if (ast_custom_function_register(&chan_pjsip_parse_uri_from_function)) {
3314  ast_log(LOG_ERROR, "Unable to register PJSIP_PARSE_URI_FROM dialplan function\n");
3315  goto end;
3316  }
3317 
3318  if (ast_custom_function_register(&media_offer_function)) {
3319  ast_log(LOG_WARNING, "Unable to register PJSIP_MEDIA_OFFER dialplan function\n");
3320  goto end;
3321  }
3322 
3323  if (ast_custom_function_register(&dtmf_mode_function)) {
3324  ast_log(LOG_WARNING, "Unable to register PJSIP_DTMF_MODE dialplan function\n");
3325  goto end;
3326  }
3327 
3328  if (ast_custom_function_register(&moh_passthrough_function)) {
3329  ast_log(LOG_WARNING, "Unable to register PJSIP_MOH_PASSTHROUGH dialplan function\n");
3330  goto end;
3331  }
3332 
3333  if (ast_custom_function_register(&session_refresh_function)) {
3334  ast_log(LOG_WARNING, "Unable to register PJSIP_SEND_SESSION_REFRESH dialplan function\n");
3335  goto end;
3336  }
3337 
3338  if (ast_register_application_xml(app_pjsip_hangup, pjsip_app_hangup)) {
3339  ast_log(LOG_WARNING, "Unable to register PJSIPHangup dialplan application\n");
3340  goto end;
3341  }
3342  ast_manager_register_xml(app_pjsip_hangup, EVENT_FLAG_SYSTEM | EVENT_FLAG_CALL, pjsip_action_hangup);
3343 
3344 
3345  ast_sip_register_service(&refer_callback_module);
3346 
3347  ast_sip_session_register_supplement(&chan_pjsip_supplement);
3348  ast_sip_session_register_supplement(&chan_pjsip_supplement_response);
3349 
3350  if (!(pjsip_uids_onhold = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_RWLOCK,
3352  uid_hold_sort_fn, NULL))) {
3353  ast_log(LOG_ERROR, "Unable to create held channels container\n");
3354  goto end;
3355  }
3356 
3357  ast_sip_session_register_supplement(&call_pickup_supplement);
3358  ast_sip_session_register_supplement(&pbx_start_supplement);
3359  ast_sip_session_register_supplement(&chan_pjsip_ack_supplement);
3360  ast_sip_session_register_supplement(&chan_pjsip_prack_supplement);
3361 
3363  ast_log(LOG_ERROR, "Unable to register PJSIP Channel CLI\n");
3364  goto end;
3365  }
3366 
3367  /* since endpoints are loaded before the channel driver their device
3368  states get set to 'invalid', so they need to be updated */
3369  if ((endpoints = ast_sip_get_endpoints())) {
3370  ao2_callback(endpoints, OBJ_NODATA, update_devstate, NULL);
3371  ao2_ref(endpoints, -1);
3372  }
3373 
3374  return 0;
3375 
3376 end:
3377  ao2_cleanup(pjsip_uids_onhold);
3378  pjsip_uids_onhold = NULL;
3379  ast_sip_session_unregister_supplement(&chan_pjsip_ack_supplement);
3380  ast_sip_session_unregister_supplement(&chan_pjsip_prack_supplement);
3381  ast_sip_session_unregister_supplement(&pbx_start_supplement);
3382  ast_sip_session_unregister_supplement(&chan_pjsip_supplement_response);
3383  ast_sip_session_unregister_supplement(&chan_pjsip_supplement);
3384  ast_sip_session_unregister_supplement(&call_pickup_supplement);
3385  ast_sip_unregister_service(&refer_callback_module);
3386  ast_custom_function_unregister(&dtmf_mode_function);
3387  ast_custom_function_unregister(&moh_passthrough_function);
3388  ast_custom_function_unregister(&media_offer_function);
3389  ast_custom_function_unregister(&chan_pjsip_dial_contacts_function);
3390  ast_custom_function_unregister(&chan_pjsip_parse_uri_function);
3391  ast_custom_function_unregister(&chan_pjsip_parse_uri_from_function);
3392  ast_custom_function_unregister(&session_refresh_function);
3393  ast_unregister_application(app_pjsip_hangup);
3394  ast_manager_unregister(app_pjsip_hangup);
3395 
3396  ast_channel_unregister(&chan_pjsip_tech);
3397  ast_rtp_glue_unregister(&chan_pjsip_rtp_glue);
3398 
3399  return AST_MODULE_LOAD_DECLINE;
3400 }
3401 
3402 /*! \brief Unload the PJSIP channel from Asterisk */
3403 static int unload_module(void)
3404 {
3405  ao2_cleanup(pjsip_uids_onhold);
3406  pjsip_uids_onhold = NULL;
3407 
3409 
3410  ast_sip_session_unregister_supplement(&chan_pjsip_supplement_response);
3411  ast_sip_session_unregister_supplement(&chan_pjsip_supplement);
3412  ast_sip_session_unregister_supplement(&pbx_start_supplement);
3413  ast_sip_session_unregister_supplement(&chan_pjsip_ack_supplement);
3414  ast_sip_session_unregister_supplement(&chan_pjsip_prack_supplement);
3415  ast_sip_session_unregister_supplement(&call_pickup_supplement);
3416 
3417  ast_sip_unregister_service(&refer_callback_module);
3418 
3419  ast_custom_function_unregister(&dtmf_mode_function);
3420  ast_custom_function_unregister(&moh_passthrough_function);
3421  ast_custom_function_unregister(&media_offer_function);
3422  ast_custom_function_unregister(&chan_pjsip_dial_contacts_function);
3423  ast_custom_function_unregister(&chan_pjsip_parse_uri_function);
3424  ast_custom_function_unregister(&chan_pjsip_parse_uri_from_function);
3425  ast_custom_function_unregister(&session_refresh_function);
3426  ast_unregister_application(app_pjsip_hangup);
3427  ast_manager_unregister(app_pjsip_hangup);
3428 
3429  ast_channel_unregister(&chan_pjsip_tech);
3430  ao2_ref(chan_pjsip_tech.capabilities, -1);
3431  ast_rtp_glue_unregister(&chan_pjsip_rtp_glue);
3432 
3433  return 0;
3434 }
3435 
3436 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Channel Driver",
3437  .support_level = AST_MODULE_SUPPORT_CORE,
3438  .load = load_module,
3439  .unload = unload_module,
3440  .load_pri = AST_MODPRI_CHANNEL_DRIVER,
3441  .requires = "res_pjsip,res_pjsip_session,res_pjsip_pubsub",
3442 );
const char * name
Definition: pbx.h:119
static int remote_send_hold(void *data)
Update local hold state to be held.
Definition: chan_pjsip.c:1496
const char * type
Definition: datastore.h:32
struct ast_sip_endpoint_pickup_configuration pickup
Definition: res_pjsip.h:990
struct ast_variable * next
struct ast_format * ast_format_vp8
Built-in cached vp8 format.
Definition: format_cache.c:191
static int remote_send_hold_refresh(struct ast_sip_session *session, unsigned int held)
Update local hold state and send a re-INVITE with the new SDP.
Definition: chan_pjsip.c:1483
#define AST_THREADSTORAGE(name)
Define a thread storage variable.
Definition: threadstorage.h:86
int ast_queue_hangup(struct ast_channel *chan)
Queue a hangup frame.
Definition: channel.c:1150
Information needed to identify an endpoint in a call.
Definition: channel.h:338
Tone Indication Support.
const char * body_text
Definition: res_pjsip.h:2315
static int chan_pjsip_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
Function called when a request is received on the session.
Definition: chan_pjsip.c:2968
void ast_party_connected_line_init(struct ast_party_connected_line *init)
Initialize the given connected line structure.
Definition: channel.c:2022
void ast_channel_pickupgroup_set(struct ast_channel *chan, ast_group_t value)
enum ast_sip_session_t38state t38state
struct ast_frame * ast_dsp_process(struct ast_channel *chan, struct ast_dsp *dsp, struct ast_frame *inf)
Return AST_FRAME_NULL frames when there is silence, AST_FRAME_BUSY on busies, and call progress...
Definition: dsp.c:1499
Main Channel structure associated with a channel.
struct ast_sip_endpoint * endpoint
Music on hold handling.
pj_sockaddr local_addr
Our address that received the request.
Definition: chan_pjsip.h:34
ast_device_state
Device States.
Definition: devicestate.h:52
static int chan_pjsip_queryoption(struct ast_channel *ast, int option, void *data, int *datalen)
Function called to query options on a channel.
Definition: chan_pjsip.c:1238
char * str
Subscriber phone number (Malloced)
Definition: channel.h:291
struct ast_channel_snapshot_base * base
Asterisk locking-related definitions:
Channels have this property if they implement send_text_data.
Definition: channel.h:975
Asterisk main include file. File version handling, generic pbx functions.
Structure which contains media state information (streams, sessions)
PJSIP dialplan functions header file.
char * str
Subscriber phone number (Malloced)
Definition: channel.h:386
char chan_name[AST_CHANNEL_NAME]
int ast_queue_control(struct ast_channel *chan, enum ast_control_frame_type control)
Queue a control frame without payload.
Definition: channel.c:1231
unsigned int locally_held
Stream is on hold by local side.
static int is_compatible_format(struct ast_sip_session *session, struct ast_frame *f)
Determine if the given frame is in a format we've negotiated.
Definition: chan_pjsip.c:825
void * ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size)
Retrieve thread storage.
void ast_rtp_instance_set_channel_id(struct ast_rtp_instance *instance, const char *uniqueid)
Set the channel that owns this RTP instance.
Definition: rtp_engine.c:575
struct ast_sip_session_media_state * pending_media_state
struct ast_sockaddr direct_media_addr
Direct media address.
char * ast_get_encoded_str(const char *stream, char *result, size_t result_len)
Decode a stream of encoded control or extended ASCII characters.
Definition: main/app.c:3162
static int transmit_info_with_vidupdate(void *data)
Send SIP INFO with video update request.
Definition: chan_pjsip.c:1352
CallerID (and other GR30) management and generation Includes code and algorithms from the Zapata libr...
struct ast_msg_data * ast_msg_data_dup(struct ast_msg_data *msg)
Clone an ast_msg_data structure.
static int remote_send_unhold(void *data)
Update local hold state to be unheld.
Definition: chan_pjsip.c:1502
int pjsip_acf_moh_passthrough_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
PJSIP_MOH_PASSTHROUGH function read callback.
struct ast_format_cap * ast_stream_topology_get_formats(struct ast_stream_topology *topology)
Create a format capabilities structure representing the topology.
Definition: stream.c:930
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:2958
pjsip_uri * request_uri
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1101
Support for translation of data formats. translate.c.
struct ast_party_name name
Subscriber name.
Definition: channel.h:340
void ast_channel_unregister(const struct ast_channel_tech *tech)
Unregister a channel technology.
Definition: channel.c:570
unsigned int defer_terminate
static struct ast_sip_session_supplement chan_pjsip_supplement_response
SIP session supplement structure just for responses.
Definition: chan_pjsip.c:155
Convenient Signal Processing routines.
static int chan_pjsip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
Function called by core to ask the channel to indicate some sort of condition.
Definition: chan_pjsip.c:1621
const char * ast_endpoint_get_tech(const struct ast_endpoint *endpoint)
Gets the technology of the given endpoint.
static struct ast_channel * chan_pjsip_new(struct ast_sip_session *session, int state, const char *exten, const char *title, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *cid_name)
Function called to create a new PJSIP Asterisk channel.
Definition: chan_pjsip.c:547
Call Pickup API.
enum ast_pbx_result ast_pbx_start(struct ast_channel *c)
Create a new thread and start the PBX.
Definition: pbx.c:4708
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
#define AST_FORMAT_CAP_NAMES_LEN
Definition: format_cap.h:324
int ast_party_id_presentation(const struct ast_party_id *id)
Determine the overall presentation value for the given party.
Definition: channel.c:1821
#define ao2_callback(c, flags, cb_fn, arg)
ao2_callback() is a generic function that applies cb_fn() to all objects in a container, as described below.
Definition: astobj2.h:1693
struct ast_endpoint_snapshot * ast_endpoint_latest_snapshot(const char *tech, const char *resource)
Retrieve the most recent snapshot for the endpoint with the given name.
static int chan_pjsip_call(struct ast_channel *ast, const char *dest, int timeout)
Function called by core to actually start calling a remote party.
Definition: chan_pjsip.c:2418
static int unload_module(void)
Unload the PJSIP channel from Asterisk.
Definition: chan_pjsip.c:3403
static int chan_pjsip_add_hold(const char *chan_uid)
Add a channel ID to the list of PJSIP channels on hold.
Definition: chan_pjsip.c:1118
const char * ast_codec_media_type2str(enum ast_media_type type)
Conversion function to take a media type and turn it into a string.
Definition: codec.c:348
unsigned int ignore_183_without_sdp
Definition: res_pjsip.h:1040
void ast_party_id_copy(struct ast_party_id *dest, const struct ast_party_id *src)
Copy the source party id information to the destination party id.
Definition: channel.c:1765
Structure for variables, used for configurations and for channel variables.
ast_control_transfer
Structure representing a snapshot of channel state.
int ast_rtp_instance_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
Send a frame out over RTP.
Definition: rtp_engine.c:590
static int hangup_cause2sip(int cause)
Internal function which translates from Asterisk cause codes to SIP response codes.
Definition: chan_pjsip.c:2435
unsigned int asymmetric_rtp_codec
Definition: res_pjsip.h:1030
#define AST_LIST_NEXT(elm, field)
Returns the next entry in the list after the given entry.
Definition: linkedlists.h:439
struct ast_format_cap * codecs
Definition: res_pjsip.h:907
Test Framework API.
static pjsip_module refer_callback_module
REFER Callback module, used to attach session data structure to subscription.
Definition: chan_pjsip.c:1948
const ast_string_field context
Definition: res_pjsip.h:952
struct ast_sip_session_media * default_session[AST_MEDIA_TYPE_END]
Default media sessions for each type.
enum ast_control_t38 request_response
static struct ast_channel * chan_pjsip_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause)
Asterisk core interaction functions.
Definition: chan_pjsip.c:2736
Structure to pass both assignedid values to channel drivers.
Definition: channel.h:604
Structure for a data store type.
Definition: datastore.h:31
void ast_channel_callgroup_set(struct ast_channel *chan, ast_group_t value)
Structure used to transport a message through the frame core.
ast_channel_state
ast_channel states
Definition: channelstate.h:35
ast_sip_session_media_write_cb write_callback
The write callback when writing frames.
char * str
Subscriber name (Malloced)
Definition: channel.h:264
A structure which contains a channel implementation and session.
enum ast_sip_session_refresh_method method
Definition: res_pjsip.h:868
Definition: astman.c:222
struct ast_sip_session * session
Pointer to session.
static struct ast_datastore_info transport_info
Datastore used to store local/remote addresses for the INVITE request that created the PJSIP channel...
Definition: chan_pjsip.c:269
Definition of a media format.
Definition: format.c:43
ast_t38_state
Possible T38 states on channels.
Definition: channel.h:878
static int chan_pjsip_digit_begin(struct ast_channel *ast, char digit)
Function called by core to start a DTMF digit.
Definition: chan_pjsip.c:2183
const ast_string_field uniqueid
int ast_format_cap_append_by_type(struct ast_format_cap *cap, enum ast_media_type type)
Add all codecs Asterisk knows about for a specific type to the capabilities structure.
Definition: format_cap.c:216
static int check_for_rtp_changes(struct ast_channel *chan, struct ast_rtp_instance *rtp, struct ast_sip_session_media *media, struct ast_sip_session *session)
Definition: chan_pjsip.c:327
size_t ast_format_cap_count(const struct ast_format_cap *cap)
Get the number of formats present within the capabilities structure.
Definition: format_cap.c:395
struct ast_stream_topology * ast_stream_topology_create_from_format_cap(struct ast_format_cap *cap)
A helper function that, given a format capabilities structure, creates a topology and separates the m...
Definition: stream.c:848
int ast_channel_get_up_time(struct ast_channel *chan)
Obtain how long it has been since the channel was answered.
Definition: channel.c:2845
struct ast_format * ast_format_cap_get_best_by_type(const struct ast_format_cap *cap, enum ast_media_type type)
Get the most preferred format for a particular media type.
Definition: format_cap.c:417
struct ast_msg_data * ast_msg_data_alloc(enum ast_msg_data_source_type source, struct ast_msg_data_attribute attributes[], size_t count)
Allocates an ast_msg_data structure.
int ast_channel_register(const struct ast_channel_tech *tech)
Register a channel technology (a new channel driver) Called by a channel module to register the kind ...
Definition: channel.c:539
unsigned int early_confirmed
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:241
Structure for a data store object.
Definition: datastore.h:64
Generic File Format Support. Should be included by clients of the file handling routines. File service providers should instead include mod_format.h.
char exten[AST_MAX_EXTENSION]
struct ast_stream * ast_stream_topology_get_stream(const struct ast_stream_topology *topology, unsigned int position)
Get a specific stream from the topology.
Definition: stream.c:788
const char * data
Definitions to aid in the use of thread local storage.
Out-of-call text message support.
struct pjsip_inv_session * inv_session
int ast_sip_push_task_wait_servant(struct ast_taskprocessor *serializer, int(*sip_task)(void *), void *task_data)
Push a task to SIP servants and wait for it to complete.
Definition: res_pjsip.c:2165
Transport information stored in transport_info datastore.
Definition: chan_pjsip.h:30
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
void ast_moh_stop(struct ast_channel *chan)
Turn off music on hold on a given channel.
Definition: channel.c:7776
void ast_free_ptr(void *ptr)
free() wrapper
Definition: main/astmm.c:1739
static int uid_hold_hash_fn(const void *obj, const int flags)
Definition: chan_pjsip.c:1067
struct ast_sip_direct_media_configuration direct_media
Definition: res_pjsip.h:903
A structure describing a SIP session.
const char * type
Definition: rtp_engine.h:775
const char * ast_endpoint_get_resource(const struct ast_endpoint *endpoint)
Gets the resource name of the given endpoint.
static void chan_pjsip_session_end(struct ast_sip_session *session)
Function called when the session ends.
Definition: chan_pjsip.c:2923
int ast_atomic_fetchadd_int(volatile int *p, int v)
Atomically add v to *p and return the previous value of *p.
Definition: lock.h:757
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
int pjsip_action_hangup(struct mansession *s, const struct message *m)
PJSIPHangup Manager Action.
const char * pbx_builtin_getvar_helper(struct ast_channel *chan, const char *name)
Return a pointer to the value of the corresponding channel variable.
struct ast_format_cap * direct_media_cap
struct ast_frame_subclass subclass
enum ast_device_state ast_devstate_aggregate_result(struct ast_devstate_aggregate *agg)
Get the aggregate device state result.
Definition: devicestate.c:663
Media Stream API.
unsigned int inband_progress
Definition: res_pjsip.h:1002
static void chan_pjsip_remove_hold(const char *chan_uid)
Remove a channel ID from the list of PJSIP channels on hold.
Definition: chan_pjsip.c:1149
static void ast_sockaddr_setnull(struct ast_sockaddr *addr)
Sets address addr to null.
Definition: netsock2.h:138
int ast_queue_hangup_with_cause(struct ast_channel *chan, int cause)
Queue a hangup frame with hangupcause set.
Definition: channel.c:1166
static struct ast_frame * chan_pjsip_cng_tone_detected(struct ast_channel *ast, struct ast_sip_session *session, struct ast_frame *f)
Internal helper function called when CNG tone is detected.
Definition: chan_pjsip.c:766
static int update_connected_line_information(void *data)
Update connected line information.
Definition: chan_pjsip.c:1430
int ast_set_read_format_path(struct ast_channel *chan, struct ast_format *raw_format, struct ast_format *core_format)
Set specific read path on channel.
Definition: channel.c:5488
struct ast_sip_session_media_state * active_media_state
void * ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id)
Retrieve an object using its unique identifier.
Definition: sorcery.c:1853
enum ast_format_cmp_res ast_format_cap_iscompatible_format(const struct ast_format_cap *cap, const struct ast_format *format)
Find if ast_format is within the capabilities of the ast_format_cap object.
Definition: format_cap.c:581
int pjsip_acf_moh_passthrough_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
PJSIP_MOH_PASSTHROUGH function write callback.
struct ast_sip_endpoint_media_configuration media
Definition: res_pjsip.h:980
Number structure.
Definition: app_followme.c:154
static int ast_sockaddr_isnull(const struct ast_sockaddr *addr)
Checks if the ast_sockaddr is null. "null" in this sense essentially means uninitialized, or having a 0 length.
Definition: netsock2.h:127
#define ao2_bump(obj)
Bump refcount on an AO2 object by one, returning the object.
Definition: astobj2.h:480
struct ast_dsp * dsp
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:1113
struct ast_party_id id
Caller party ID.
Definition: channel.h:420
static int chan_pjsip_transfer(struct ast_channel *ast, const char *target)
Function called by core for Asterisk initiated transfer.
Definition: chan_pjsip.c:2164
#define ast_str_tmp(init_len, __expr)
Provides a temporary ast_str and returns a copy of its buffer.
Definition: strings.h:1189
int pjsip_acf_dtmf_mode_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
PJSIP_DTMF_MODE function write callback.
int pjsip_acf_channel_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
CHANNEL function read callback.
int ast_dsp_get_features(struct ast_dsp *dsp)
Get features.
Definition: dsp.c:1777
The arg parameter is a partial search key similar to OBJ_SEARCH_KEY.
Definition: astobj2.h:1116
const char * ast_stream_topology_to_str(const struct ast_stream_topology *topology, struct ast_str **buf)
Get a string representing the topology for debugging/display purposes.
Definition: stream.c:936
enum ast_format_cmp_res ast_format_cmp(const struct ast_format *format1, const struct ast_format *format2)
Compare two formats.
Definition: format.c:201
int ast_devstate_changed(enum ast_device_state state, enum ast_devstate_cache cachable, const char *fmt,...)
Tells Asterisk the State for Device is changed.
Definition: devicestate.c:510
static int chan_pjsip_sendtext_data(struct ast_channel *ast, struct ast_msg_data *msg)
Function called by core to send text on PJSIP session.
Definition: chan_pjsip.c:2857
void ast_channel_queue_connected_line_update(struct ast_channel *chan, const struct ast_party_connected_line *connected, const struct ast_set_party_connected_line *update)
Queue a connected line update frame on a channel.
Definition: channel.c:9106
struct ast_namedgroups * named_pickupgroups
Definition: res_pjsip.h:798
General Asterisk PBX channel definitions.
#define AST_VECTOR_GET_ADDR(vec, idx)
Get an address of element in a vector.
Definition: vector.h:668
static void chan_pjsip_incoming_response(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
Function called when a response is received on the session.
Definition: chan_pjsip.c:3147
static int chan_pjsip_hangup(struct ast_channel *ast)
Function called by core to hang up a PJSIP session.
Definition: chan_pjsip.c:2558
static int compatible_formats_exist(struct ast_stream_topology *top, struct ast_format_cap *cap)
Determine if a topology is compatible with format capabilities.
Definition: chan_pjsip.c:526
void ast_set_hangupsource(struct ast_channel *chan, const char *source, int force)
Set the source of the hangup in this channel and it's bridge.
Definition: channel.c:2499
A set of tones for a given locale.
Definition: indications.h:74
void ast_channel_stage_snapshot_done(struct ast_channel *chan)
Clear flag to indicate channel snapshot is being staged, and publish snapshot.
const char * type
Definition: res_pjsip.h:2311
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
int pjsip_acf_dial_contacts_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
PJSIP_DIAL_CONTACTS function read callback.
const ast_string_field language
Definition: res_pjsip.h:964
int ast_sip_push_task_wait_serializer(struct ast_taskprocessor *serializer, int(*sip_task)(void *), void *task_data)
Push a task to the serializer and wait for it to complete.
Definition: res_pjsip.c:2179
Data structure associated with a custom dialplan function.
Definition: pbx.h:118
Access Control of various sorts.
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
void pjsip_channel_cli_unregister(void)
Unregisters the channel cli commands.
Definition: cli_commands.c:498
static void chan_pjsip_session_begin(struct ast_sip_session *session)
SIP session interaction functions.
Definition: chan_pjsip.c:2901
struct ast_sip_media_rtp_configuration rtp
Definition: res_pjsip.h:901
#define S_COR(a, b, c)
returns the equivalent of logic or for strings, with an additional boolean check: second one if not e...
Definition: strings.h:87
const ast_string_field accountcode
Definition: res_pjsip.h:972
char * ast_frame_subclass2str(struct ast_frame *f, char *subclass, size_t slen, char *moreinfo, size_t mlen)
Copy the discription of a frame's subclass into the provided string.
Definition: main/frame.c:406
ast_sip_session_media_read_cb read_callback
The callback to invoke.
struct ast_sip_session_media * session
The media session.
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2317
struct ast_channel * channel
struct ast_namedgroups * named_callgroups
Definition: res_pjsip.h:796
#define ast_format_cap_append(cap, format, framing)
Add format capability to capabilities structure.
Definition: format_cap.h:99
struct ast_sip_session_media_state::@268 read_callbacks
Added read callbacks - these are whole structs and not pointers.
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:191
enum ast_device_state ast_state_chan2dev(enum ast_channel_state chanstate)
Convert channel state to devicestate.
Definition: devicestate.c:242
#define ast_debug(level,...)
Log a DEBUG message.
struct ast_sip_session_media_state::@267 sessions
Mapping of stream to media sessions.
void ast_channel_set_unbridged_nolock(struct ast_channel *chan, int value)
Variant of ast_channel_set_unbridged. Use this if the channel is already locked prior to calling...
const struct ast_format_cap * ast_stream_get_formats(const struct ast_stream *stream)
Get the current negotiated formats of a stream.
Definition: stream.c:330
Structure which contains read callback information.
An entity with which Asterisk communicates.
Definition: res_pjsip.h:949
int pjsip_acf_media_offer_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
PJSIP_MEDIA_OFFER function write callback.
int ast_exists_extension(struct ast_channel *c, const char *context, const char *exten, int priority, const char *callerid)
Determine whether an extension exists.
Definition: pbx.c:4175
ast_rtp_glue_result
Definition: rtp_engine.h:161
enum ast_sip_session_refresh_method refresh_method
Definition: res_pjsip.h:782
#define ast_format_cap_alloc(flags)
Allocate a new ast_format_cap structure.
Definition: format_cap.h:49
Structure to describe a channel "technology", ie a channel driver See for examples: ...
Definition: channel.h:628
Core PBX routines and definitions.
void ast_format_cap_remove_by_type(struct ast_format_cap *cap, enum ast_media_type type)
Remove all formats matching a specific format type.
Definition: format_cap.c:523
A snapshot of an endpoint's state.
struct ast_taskprocessor * serializer
const ast_string_field zone
Definition: res_pjsip.h:962
#define ast_test_suite_event_notify(s, f,...)
Definition: test.h:189
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:288
struct ast_sip_endpoint_id_configuration id
Definition: res_pjsip.h:986
void ast_devstate_aggregate_add(struct ast_devstate_aggregate *agg, enum ast_device_state state)
Add a device state to the aggregate device state.
Definition: devicestate.c:636
static int chan_pjsip_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
Function called by core to stop a DTMF digit.
Definition: chan_pjsip.c:2295
enum ast_sip_session_media_encryption encryption
Definition: res_pjsip.h:846
int ast_manager_unregister(const char *action)
Unregister a registered manager command.
Definition: manager.c:8057
int fd
The file descriptor itself.
void ast_channel_stage_snapshot(struct ast_channel *chan)
Set flag to indicate channel snapshot is being staged.
int pjsip_acf_parse_uri_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
PJSIP_PARSE_URI function read callback.
pj_sockaddr remote_addr
The address that sent the request.
Definition: chan_pjsip.h:32
int pjsip_acf_session_refresh_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
PJSIP_SEND_SESSION_REFRESH function write callback.
static void transport_info_destroy(void *obj)
Destructor function for transport_info_data.
Definition: chan_pjsip.c:261
static int chan_pjsip_get_hold(const char *chan_uid)
Determine whether a channel ID is in the list of PJSIP channels on hold.
Definition: chan_pjsip.c:1162
#define AST_EXTENDED_FDS
Definition: channel.h:195
#define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn)
Allocate and initialize a hash container with the desired number of buckets.
Definition: astobj2.h:1303
struct ast_stream_topology * ast_channel_set_stream_topology(struct ast_channel *chan, struct ast_stream_topology *topology)
Set the topology of streams on a channel.
int ast_pickup_call(struct ast_channel *chan)
Pickup a call.
Definition: pickup.c:199
Support for dynamic strings.
Definition: strings.h:623
Format capabilities structure, holds formats + preference order + etc.
Definition: format_cap.c:54
static int chan_pjsip_devicestate(const char *data)
Function called to get the device state of an endpoint.
Definition: chan_pjsip.c:1175
static struct ast_sip_session_supplement chan_pjsip_supplement
SIP session supplement structure.
Definition: chan_pjsip.c:143
static int chan_pjsip_answer(struct ast_channel *ast)
Function called by core when we should answer a PJSIP session.
Definition: chan_pjsip.c:727
int ast_sip_push_task(struct ast_taskprocessor *serializer, int(*sip_task)(void *), void *task_data)
Pushes a task to SIP servants.
Definition: res_pjsip.c:2099
static struct ast_channel * chan_pjsip_request_with_stream_topology(const char *type, struct ast_stream_topology *topology, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause)
Function called by core to create a new outgoing PJSIP session.
Definition: chan_pjsip.c:2709
#define AST_NONSTANDARD_APP_ARGS(args, parse, sep)
Performs the 'nonstandard' argument separation process for an application.
struct ast_tone_zone * ast_get_indication_zone(const char *country)
locate ast_tone_zone
Definition: indications.c:439
static void xfer_client_on_evsub_state(pjsip_evsub *sub, pjsip_event *event)
Callback function to report status of implicit REFER-NOTIFY subscription.
Definition: chan_pjsip.c:1960
int ani2
Automatic Number Identification 2 (Info Digits)
Definition: channel.h:433
enum ast_sip_dtmf_mode dtmf
const char * ast_msg_data_get_attribute(struct ast_msg_data *msg, enum ast_msg_data_attribute_type attribute_type)
Get attribute from ast_msg_data.
The PJSIP channel driver pvt, stored in the ast_sip_channel_pvt data structure.
Definition: chan_pjsip.h:42
Contact associated with an address of record.
Definition: res_pjsip.h:392
const char * subtype
Definition: res_pjsip.h:2313
Connected Line/Party information.
Definition: channel.h:456
int ast_rtp_instance_dtmf_begin(struct ast_rtp_instance *instance, char digit)
Begin sending a DTMF digit.
Definition: rtp_engine.c:2205
struct ast_format * ast_format_vp9
Built-in cached vp9 format.
Definition: format_cache.c:196
int ast_moh_start(struct ast_channel *chan, const char *mclass, const char *interpclass)
Turn on music on hold on a given channel.
Definition: channel.c:7766
struct ast_format * ast_format_h264
Built-in cached h264 format.
Definition: format_cache.c:176
struct ast_sip_t38_configuration t38
Definition: res_pjsip.h:905
const char * ast_format_cap_get_names(const struct ast_format_cap *cap, struct ast_str **buf)
Get the names of codecs of a set of formats.
Definition: format_cap.c:734
unsigned int faxdetect_timeout
Definition: res_pjsip.h:1024
struct ast_format_cap * capabilities
Definition: channel.h:632
int ast_channel_is_bridged(const struct ast_channel *chan)
Determine if a channel is in a bridge.
Definition: channel.c:10545
int ast_set_write_format_path(struct ast_channel *chan, struct ast_format *core_format, struct ast_format *raw_format)
Set specific write path on channel.
Definition: channel.c:5524
static struct stasis_rest_handlers endpoints
REST handler for /api-docs/endpoints.json.
static int chan_pjsip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
Function called by core to change the underlying owner channel.
Definition: chan_pjsip.c:1046
void ast_dsp_set_features(struct ast_dsp *dsp, int features)
Select feature set.
Definition: dsp.c:1768
union ast_frame::@224 data
PJSIP Channel Driver shared data structures.
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
void ast_devstate_aggregate_init(struct ast_devstate_aggregate *agg)
Initialize aggregate device state.
Definition: devicestate.c:630
#define AST_CHANNEL_NAME
Definition: channel.h:171
enum ast_sip_direct_media_glare_mitigation glare_mitigation
Definition: res_pjsip.h:870
int ast_format_cap_identical(const struct ast_format_cap *cap1, const struct ast_format_cap *cap2)
Determine if two capabilities structures are identical.
Definition: format_cap.c:687
void ast_hangup(struct ast_channel *chan)
Hang up a channel.
Definition: channel.c:2541
int ast_rtp_instance_fd(struct ast_rtp_instance *instance, int rtcp)
Get the file descriptor for an RTP session (or RTCP)
Definition: rtp_engine.c:2316
static void chan_pjsip_get_codec(struct ast_channel *chan, struct ast_format_cap *result)
Function called by RTP engine to get peer capabilities.
Definition: chan_pjsip.c:252
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
enum ast_channel_state state
An API for managing task processing threads that can be shared across modules.
int ast_stream_topology_get_count(const struct ast_stream_topology *topology)
Get the number of streams in a topology.
Definition: stream.c:765
A structure containing SIP session media information.
void ast_channel_set_fd(struct ast_channel *chan, int which, int fd)
Definition: channel.c:2426
static struct ast_frame * chan_pjsip_read_stream(struct ast_channel *ast)
Function called by core to read any waiting frames.
Definition: chan_pjsip.c:839
A supplement to SIP message processing.
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name...
struct ast_stream_topology * ast_stream_topology_clone(const struct ast_stream_topology *topology)
Create a deep clone of an existing stream topology.
Definition: stream.c:667
int pjsip_acf_dtmf_mode_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
PJSIP_DTMF_MODE function read callback.
struct ast_frame ast_null_frame
Definition: main/frame.c:79
struct ast_format * ast_format_h265
Built-in cached h265 format.
Definition: format_cache.c:181
int pjsip_channel_cli_register(void)
Registers the channel cli commands.
Definition: cli_commands.c:462
struct ast_endpoint * persistent
Definition: res_pjsip.h:1004
The arg parameter is an object of the same type.
Definition: astobj2.h:1087
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
Definition: vector.h:680
enum ast_media_type type
Media type of this session media.
const char * ast_translate_path_to_str(struct ast_trans_pvt *t, struct ast_str **str)
Puts a string representation of the translation path into outbuf.
Definition: translate.c:930
#define ast_channel_ref(c)
Increase channel reference count.
Definition: channel.h:2947
static int load_module(void)
Load the module.
Definition: chan_pjsip.c:3286
struct ast_stream_topology * topology
Definition: res_pjsip.h:909
struct ast_channel_snapshot * ast_channel_snapshot_get_latest(const char *uniqueid)
Obtain the latest ast_channel_snapshot from the Stasis Message Bus API cache. This is an ao2 object...
Standard Command Line Interface.
struct ast_stream_topology * topology
The media stream topology.
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
struct ast_rtp_instance * rtp
RTP instance itself.
Channels have this property if they can accept input with jitter; i.e. most VoIP channels.
Definition: channel.h:960
int ast_setstate(struct ast_channel *chan, enum ast_channel_state)
Change the state of a channel.
Definition: channel.c:7386
int ast_rtp_glue_unregister(struct ast_rtp_glue *glue)
Unregister RTP glue.
Definition: rtp_engine.c:408
int ast_async_goto(struct ast_channel *chan, const char *context, const char *exten, int priority)
Set the channel to next execute the specified dialplan location.
Definition: pbx.c:6969
struct ast_party_dialed::@206 number
Dialed/Called number.
int ast_queue_control_data(struct ast_channel *chan, enum ast_control_frame_type control, const void *data, size_t datalen)
Queue a control frame with payload.
Definition: channel.c:1238
enum ast_rtp_dtmf_mode ast_rtp_instance_dtmf_mode_get(struct ast_rtp_instance *instance)
Get the DTMF mode of an RTP instance.
Definition: rtp_engine.c:2261
void ast_rtp_instance_set_stats_vars(struct ast_channel *chan, struct ast_rtp_instance *instance)
Set standard statistics from an RTP instance on a channel.
Definition: rtp_engine.c:2639
int ast_channel_get_device_name(struct ast_channel *chan, char *device_name, size_t name_buffer_length)
Get a device name given its channel structure.
Definition: channel.c:10496
Data structure associated with a single frame of data.
Internal Asterisk hangup causes.
static enum ast_rtp_glue_result chan_pjsip_get_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
Function called by RTP engine to get local audio RTP peer.
Definition: chan_pjsip.c:179
#define AST_RTP_RTCP_PSFB
Definition: rtp_engine.h:329
int pjsip_app_hangup(struct ast_channel *chan, const char *data)
PJSIPHangup Dialplan App.
You shouldn't care about the contents of this struct.
Definition: devicestate.h:228
static enum ast_rtp_glue_result chan_pjsip_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
Function called by RTP engine to get local video RTP peer.
Definition: chan_pjsip.c:223
static void chan_pjsip_incoming_response_update_cause(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
Function called when a response is received on the session.
Definition: chan_pjsip.c:3117
void ast_rtp_instance_set_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value)
Set the value of an RTP instance property.
Definition: rtp_engine.c:727
Reject objects with duplicate keys in container.
Definition: astobj2.h:1188
enum ast_frame_type frametype
Channels have this property if they can create jitter; i.e. most VoIP channels.
Definition: channel.h:965
int ast_format_cap_empty(const struct ast_format_cap *cap)
Determine if a format cap has no formats in it.
Definition: format_cap.c:744
Generic container type.
int ast_format_cap_append_from_cap(struct ast_format_cap *dst, const struct ast_format_cap *src, enum ast_media_type type)
Append the formats of provided type in src to dst.
Definition: format_cap.c:269
unsigned char valid
TRUE if the name information is valid/present.
Definition: channel.h:279
Search option field mask.
Definition: astobj2.h:1072
struct ast_variable * channel_vars
Definition: res_pjsip.h:1014
struct ast_format * ast_format_cap_get_format(const struct ast_format_cap *cap, int position)
Get the format at a specific index.
Definition: format_cap.c:400
struct ast_format * format
int ast_devstate_changed_literal(enum ast_device_state state, enum ast_devstate_cache cachable, const char *device)
Tells Asterisk the State for Device is changed.
Definition: devicestate.c:471
void ast_stream_topology_free(struct ast_stream_topology *topology)
Unreference and destroy a stream topology.
Definition: stream.c:743
const ast_string_field aors
Definition: res_pjsip.h:958
#define ast_manager_register_xml(action, authority, func)
Register a manager callback using XML documentation to describe the manager.
Definition: manager.h:191
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
static int uid_hold_sort_fn(const void *obj_left, const void *obj_right, const int flags)
Definition: chan_pjsip.c:1085
const ast_string_field uri
Definition: res_pjsip.h:414
Pluggable RTP Architecture.
Asterisk module definitions.
int pjsip_acf_media_offer_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
PJSIP_MEDIA_OFFER function read callback.
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941
static int rtp_find_rtcp_fd_position(struct ast_sip_session *session, struct ast_rtp_instance *rtp)
Helper function to find the position for RTCP.
Definition: chan_pjsip.c:306
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
struct ast_party_id id
static int chan_pjsip_set_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance *rtp, struct ast_rtp_instance *vrtp, struct ast_rtp_instance *tpeer, const struct ast_format_cap *cap, int nat_active)
Function called by RTP engine to change where the remote party should send media. ...
Definition: chan_pjsip.c:448
unsigned char valid
TRUE if the number information is valid/present.
Definition: channel.h:297
#define ast_rtp_instance_get_and_cmp_remote_address(instance, address)
Get the address of the remote endpoint that we are sending RTP to, comparing its address to another...
Definition: rtp_engine.h:1281
Endpoint abstractions.
struct ast_channel_tech chan_pjsip_tech
PBX interface structure for channel registration.
Definition: chan_pjsip.c:109
void ast_channel_hangupcause_hash_set(struct ast_channel *chan, const struct ast_control_pvt_cause_code *cause_code, int datalen)
Sets the HANGUPCAUSE hash and optionally the SIP_CAUSE hash on the given channel. ...
Definition: channel.c:4346
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1558
SIP body description.
Definition: res_pjsip.h:2309
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
Definition: vector.h:609
char exten[AST_MAX_EXTENSION]
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:640
PJSIP CLI functions header file.
static void clear_session_and_channel(struct ast_sip_session *session, struct ast_channel *ast)
Clear a channel from a session along with its PVT.
Definition: chan_pjsip.c:2509
Media Format Cache API.
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
Definition: strings.h:659
unsigned int moh_passthrough
Configuration relating to call pickup.
#define AST_APP_ARG(name)
Define an application argument.
static force_inline int attribute_pure ast_str_hash(const char *str)
Compute a hash value on a string.
Definition: strings.h:1259
static struct ast_rtp_glue chan_pjsip_rtp_glue
Local glue for interacting with the RTP engine core.
Definition: chan_pjsip.c:486
struct ast_party_number number
Subscriber phone number.
Definition: channel.h:342
#define ao2_link(container, obj)
Add an object to a container.
Definition: astobj2.h:1532
int ast_format_cap_iscompatible(const struct ast_format_cap *cap1, const struct ast_format_cap *cap2)
Determine if any joint capabilities exist between two capabilities structures.
Definition: format_cap.c:653