Asterisk - The Open Source Telephony Project  GIT-master-c559667
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"
58 #include "asterisk/indications.h"
59 #include "asterisk/format_cache.h"
60 #include "asterisk/translate.h"
61 #include "asterisk/threadstorage.h"
63 #include "asterisk/pickup.h"
64 #include "asterisk/test.h"
65 #include "asterisk/message.h"
66 
67 #include "asterisk/res_pjsip.h"
69 #include "asterisk/stream.h"
70 
74 
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);
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 */
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",
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",
158  .incoming_response = chan_pjsip_incoming_response_update_cause,
160 };
161 
162 static int chan_pjsip_incoming_ack(struct ast_sip_session *session, struct pjsip_rx_data *rdata);
163 
165  .method = "ACK",
167  .incoming_request = chan_pjsip_incoming_ack,
168 };
169 
170 /*! \brief Function called by RTP engine to get local audio RTP peer */
171 static enum ast_rtp_glue_result chan_pjsip_get_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
172 {
174  struct ast_sip_endpoint *endpoint;
175  struct ast_datastore *datastore;
176  struct ast_sip_session_media *media;
177 
178  if (!channel || !channel->session) {
180  }
181 
182  /* XXX Getting the first RTP instance for direct media related stuff seems just
183  * absolutely wrong. But the native RTP bridge knows no other method than single-stream
184  * for direct media. So this is the best we can do.
185  */
187  if (!media || !media->rtp) {
189  }
190 
191  datastore = ast_sip_session_get_datastore(channel->session, "t38");
192  if (datastore) {
193  ao2_ref(datastore, -1);
195  }
196 
197  endpoint = channel->session->endpoint;
198 
199  *instance = media->rtp;
200  ao2_ref(*instance, +1);
201 
202  ast_assert(endpoint != NULL);
203  if (endpoint->media.rtp.encryption != AST_SIP_MEDIA_ENCRYPT_NONE) {
205  }
206 
207  if (endpoint->media.direct_media.enabled) {
209  }
210 
212 }
213 
214 /*! \brief Function called by RTP engine to get local video RTP peer */
215 static enum ast_rtp_glue_result chan_pjsip_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
216 {
218  struct ast_sip_endpoint *endpoint;
219  struct ast_sip_session_media *media;
220 
221  if (!channel || !channel->session) {
223  }
224 
226  if (!media || !media->rtp) {
228  }
229 
230  endpoint = channel->session->endpoint;
231 
232  *instance = media->rtp;
233  ao2_ref(*instance, +1);
234 
235  ast_assert(endpoint != NULL);
236  if (endpoint->media.rtp.encryption != AST_SIP_MEDIA_ENCRYPT_NONE) {
238  }
239 
241 }
242 
243 /*! \brief Function called by RTP engine to get peer capabilities */
244 static void chan_pjsip_get_codec(struct ast_channel *chan, struct ast_format_cap *result)
245 {
246  SCOPE_ENTER(1, "%s Native formats %s\n", ast_channel_name(chan),
249  SCOPE_EXIT_RTN();
250 }
251 
252 /*! \brief Destructor function for \ref transport_info_data */
253 static void transport_info_destroy(void *obj)
254 {
255  struct transport_info_data *data = obj;
256  ast_free(data);
257 }
258 
259 /*! \brief Datastore used to store local/remote addresses for the
260  * INVITE request that created the PJSIP channel */
262  .type = "chan_pjsip_transport_info",
263  .destroy = transport_info_destroy,
264 };
265 
267 
268 static int direct_media_mitigate_glare(struct ast_sip_session *session)
269 {
270  RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
271 
274  return 0;
275  }
276 
277  datastore = ast_sip_session_get_datastore(session, "direct_media_glare_mitigation");
278  if (!datastore) {
279  return 0;
280  }
281 
282  /* Removing the datastore ensures we won't try to mitigate glare on subsequent reinvites */
283  ast_sip_session_remove_datastore(session, "direct_media_glare_mitigation");
284 
285  if ((session->endpoint->media.direct_media.glare_mitigation ==
287  session->inv_session->role == PJSIP_ROLE_UAC) ||
290  session->inv_session->role == PJSIP_ROLE_UAS)) {
291  return 1;
292  }
293 
294  return 0;
295 }
296 
297 /*! \brief Helper function to find the position for RTCP */
298 static int rtp_find_rtcp_fd_position(struct ast_sip_session *session, struct ast_rtp_instance *rtp)
299 {
300  int index;
301 
302  for (index = 0; index < AST_VECTOR_SIZE(&session->active_media_state->read_callbacks); ++index) {
303  struct ast_sip_session_media_read_callback_state *callback_state =
304  AST_VECTOR_GET_ADDR(&session->active_media_state->read_callbacks, index);
305 
306  if (callback_state->fd != ast_rtp_instance_fd(rtp, 1)) {
307  continue;
308  }
309 
310  return index;
311  }
312 
313  return -1;
314 }
315 
316 /*!
317  * \pre chan is locked
318  */
319 static int check_for_rtp_changes(struct ast_channel *chan, struct ast_rtp_instance *rtp,
320  struct ast_sip_session_media *media, struct ast_sip_session *session)
321 {
322  int changed = 0, position = -1;
323 
324  if (media->rtp) {
325  position = rtp_find_rtcp_fd_position(session, media->rtp);
326  }
327 
328  if (rtp) {
330  if (media->rtp) {
331  if (position != -1) {
332  ast_channel_set_fd(chan, position + AST_EXTENDED_FDS, -1);
333  }
335  }
336  } else if (!ast_sockaddr_isnull(&media->direct_media_addr)){
338  changed = 1;
339  if (media->rtp) {
340  /* Direct media has ended - reset time of last received RTP packet
341  * to avoid premature RTP timeout. Synchronisation between the
342  * modification of direct_mdedia_addr+last_rx here and reading the
343  * values in res_pjsip_sdp_rtp.c:rtp_check_timeout() is provided
344  * by the channel's lock (which is held while this function is
345  * executed).
346  */
347  ast_rtp_instance_set_last_rx(media->rtp, time(NULL));
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;
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 
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 {
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;
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;
415  changed |= check_for_rtp_changes(
416  cdata->chan, cdata->rtp, session->active_media_state->default_session[AST_MEDIA_TYPE_AUDIO], session);
417  }
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) &&
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,
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 */
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 {
456  struct ast_sip_session *session = channel->session;
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) {
475  }
476 
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 
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);
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  }
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 
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);
622  ast_channel_set_readformat(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 
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 
638  if (!ast_strlen_zero(exten)) {
639  /* Set provided DNID on the new channel. */
640  ast_channel_dialed(chan)->number.str = ast_strdup(exten);
641  }
642 
643  ast_channel_priority_set(chan, 1);
644 
647 
650 
651  if (!ast_strlen_zero(session->endpoint->language)) {
652  ast_channel_language_set(chan, session->endpoint->language);
653  }
654 
655  if (!ast_strlen_zero(session->endpoint->zone)) {
656  struct ast_tone_zone *zone = ast_get_indication_zone(session->endpoint->zone);
657  if (!zone) {
658  ast_log(LOG_ERROR, "Unknown country code '%s' for tonezone. Check indications.conf for available country codes.\n", session->endpoint->zone);
659  }
660  ast_channel_zone_set(chan, zone);
661  }
662 
663  for (var = session->endpoint->channel_vars; var; var = var->next) {
664  char buf[512];
666  var->value, buf, sizeof(buf)));
667  }
668 
670  ast_channel_unlock(chan);
671 
673 
674  SCOPE_EXIT_RTN_VALUE(chan);
675 }
676 
677 struct answer_data {
679  unsigned long indent;
680 };
681 
682 static int answer(void *data)
683 {
684  struct answer_data *ans_data = data;
685  pj_status_t status = PJ_SUCCESS;
686  pjsip_tx_data *packet = NULL;
687  struct ast_sip_session *session = ans_data->session;
688  SCOPE_ENTER_TASK(1, ans_data->indent, "%s\n", ast_sip_session_get_name(session));
689 
690  if (session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
691  ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
692  session->inv_session->cause,
693  pjsip_get_status_text(session->inv_session->cause)->ptr);
694  SCOPE_EXIT_RTN_VALUE(0, "Disconnected\n");
695  }
696 
697  pjsip_dlg_inc_lock(session->inv_session->dlg);
698  if (session->inv_session->invite_tsx) {
699  status = pjsip_inv_answer(session->inv_session, 200, NULL, NULL, &packet);
700  } else {
701  ast_log(LOG_ERROR,"Cannot answer '%s' because there is no associated SIP transaction\n",
702  ast_channel_name(session->channel));
703  }
704  pjsip_dlg_dec_lock(session->inv_session->dlg);
705 
706  if (status == PJ_SUCCESS && packet) {
707  ast_sip_session_send_response(session, packet);
708  }
709 
710  if (status != PJ_SUCCESS) {
711  char err[PJ_ERR_MSG_SIZE];
712 
713  pj_strerror(status, err, sizeof(err));
714  ast_log(LOG_WARNING,"Cannot answer '%s': %s\n",
715  ast_channel_name(session->channel), err);
716  /*
717  * Return this value so we can distinguish between this
718  * failure and the threadpool synchronous push failing.
719  */
720  SCOPE_EXIT_RTN_VALUE(-2, "pjproject failure\n");
721  }
723 }
724 
725 /*! \brief Function called by core when we should answer a PJSIP session */
726 static int chan_pjsip_answer(struct ast_channel *ast)
727 {
729  struct ast_sip_session *session;
730  struct answer_data ans_data = { 0, };
731  int res;
732  SCOPE_ENTER(1, "%s\n", ast_channel_name(ast));
733 
734  if (ast_channel_state(ast) == AST_STATE_UP) {
735  SCOPE_EXIT_RTN_VALUE(0, "Already up\n");
736  return 0;
737  }
738 
740  session = ao2_bump(channel->session);
741 
742  /* the answer task needs to be pushed synchronously otherwise a race condition
743  can occur between this thread and bridging (specifically when native bridging
744  attempts to do direct media) */
745  ast_channel_unlock(ast);
746  ans_data.session = session;
747  ans_data.indent = ast_trace_get_indent();
748  res = ast_sip_push_task_wait_serializer(session->serializer, answer, &ans_data);
749  if (res) {
750  if (res == -1) {
751  ast_log(LOG_ERROR,"Cannot answer '%s': Unable to push answer task to the threadpool.\n",
752  ast_channel_name(session->channel));
753  }
754  ao2_ref(session, -1);
755  ast_channel_lock(ast);
756  SCOPE_EXIT_RTN_VALUE(-1, "Couldn't push task\n");
757  }
758  ao2_ref(session, -1);
759  ast_channel_lock(ast);
760 
762 }
763 
764 /*! \brief Internal helper function called when CNG tone is detected */
765 static struct ast_frame *chan_pjsip_cng_tone_detected(struct ast_channel *ast, struct ast_sip_session *session,
766  struct ast_frame *f)
767 {
768  const char *target_context;
769  int exists;
770  int dsp_features;
771 
772  dsp_features = ast_dsp_get_features(session->dsp);
773  dsp_features &= ~DSP_FEATURE_FAX_DETECT;
774  if (dsp_features) {
775  ast_dsp_set_features(session->dsp, dsp_features);
776  } else {
777  ast_dsp_free(session->dsp);
778  session->dsp = NULL;
779  }
780 
781  /* If already executing in the fax extension don't do anything */
782  if (!strcmp(ast_channel_exten(ast), "fax")) {
783  return f;
784  }
785 
786  target_context = S_OR(ast_channel_macrocontext(ast), ast_channel_context(ast));
787 
788  /*
789  * We need to unlock the channel here because ast_exists_extension has the
790  * potential to start and stop an autoservice on the channel. Such action
791  * is prone to deadlock if the channel is locked.
792  *
793  * ast_async_goto() has its own restriction on not holding the channel lock.
794  */
795  ast_channel_unlock(ast);
796  ast_frfree(f);
797  f = &ast_null_frame;
798  exists = ast_exists_extension(ast, target_context, "fax", 1,
799  S_COR(ast_channel_caller(ast)->id.number.valid,
800  ast_channel_caller(ast)->id.number.str, NULL));
801  if (exists) {
802  ast_verb(2, "Redirecting '%s' to fax extension due to CNG detection\n",
803  ast_channel_name(ast));
804  pbx_builtin_setvar_helper(ast, "FAXEXTEN", ast_channel_exten(ast));
805  if (ast_async_goto(ast, target_context, "fax", 1)) {
806  ast_log(LOG_ERROR, "Failed to async goto '%s' into fax extension in '%s'\n",
807  ast_channel_name(ast), target_context);
808  }
809  } else {
810  ast_log(LOG_NOTICE, "FAX CNG detected on '%s' but no fax extension in '%s'\n",
811  ast_channel_name(ast), target_context);
812  }
813 
814  /* It's possible for a masquerade to have occurred when doing the ast_async_goto resulting in
815  * the channel on the session having changed. Since we need to return with the original channel
816  * locked we lock the channel that was passed in and not session->channel.
817  */
818  ast_channel_lock(ast);
819 
820  return f;
821 }
822 
823 /*! \brief Determine if the given frame is in a format we've negotiated */
824 static int is_compatible_format(struct ast_sip_session *session, struct ast_frame *f)
825 {
826  struct ast_stream_topology *topology = session->active_media_state->topology;
827  struct ast_stream *stream = ast_stream_topology_get_stream(topology, f->stream_num);
828  const struct ast_format_cap *cap = ast_stream_get_formats(stream);
829 
831 }
832 
833 /*!
834  * \brief Function called by core to read any waiting frames
835  *
836  * \note The channel is already locked.
837  */
838 static struct ast_frame *chan_pjsip_read_stream(struct ast_channel *ast)
839 {
841  struct ast_sip_session *session = channel->session;
842  struct ast_sip_session_media_read_callback_state *callback_state;
843  struct ast_frame *f;
844  int fdno = ast_channel_fdno(ast) - AST_EXTENDED_FDS;
845  struct ast_frame *cur;
846 
847  if (fdno >= AST_VECTOR_SIZE(&session->active_media_state->read_callbacks)) {
848  return &ast_null_frame;
849  }
850 
851  callback_state = AST_VECTOR_GET_ADDR(&session->active_media_state->read_callbacks, fdno);
852  f = callback_state->read_callback(session, callback_state->session);
853 
854  if (!f) {
855  return f;
856  }
857 
858  for (cur = f; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
859  if (cur->frametype == AST_FRAME_VOICE) {
860  break;
861  }
862  }
863 
864  if (!cur || callback_state->session != session->active_media_state->default_session[callback_state->session->type]) {
865  return f;
866  }
867 
868  session = channel->session;
869 
870  /*
871  * Asymmetric RTP only has one native format set at a time.
872  * Therefore we need to update the native format to the current
873  * raw read format BEFORE the native format check
874  */
875  if (!session->endpoint->asymmetric_rtp_codec &&
877  is_compatible_format(session, cur)) {
878  struct ast_format_cap *caps;
879 
880  /* For maximum compatibility we ensure that the formats match that of the received media */
881  ast_debug(1, "Oooh, got a frame with format of %s on channel '%s' when we're sending '%s', switching to match\n",
884 
886  if (caps) {
889  ast_format_cap_append(caps, cur->subclass.format, 0);
891  ao2_ref(caps, -1);
892  }
893 
896 
897  if (ast_channel_is_bridged(ast)) {
899  }
900  }
901 
904  ast_debug(1, "Oooh, got a frame with format of %s on channel '%s' when it has not been negotiated\n",
906  ast_frfree(f);
907  return &ast_null_frame;
908  }
909 
910  if (session->dsp) {
911  int dsp_features;
912 
913  dsp_features = ast_dsp_get_features(session->dsp);
914  if ((dsp_features & DSP_FEATURE_FAX_DETECT)
915  && session->endpoint->faxdetect_timeout
916  && session->endpoint->faxdetect_timeout <= ast_channel_get_up_time(ast)) {
917  dsp_features &= ~DSP_FEATURE_FAX_DETECT;
918  if (dsp_features) {
919  ast_dsp_set_features(session->dsp, dsp_features);
920  } else {
921  ast_dsp_free(session->dsp);
922  session->dsp = NULL;
923  }
924  ast_debug(3, "Channel driver fax CNG detection timeout on %s\n",
925  ast_channel_name(ast));
926  }
927  }
928  if (session->dsp) {
929  f = ast_dsp_process(ast, session->dsp, f);
930  if (f && (f->frametype == AST_FRAME_DTMF)) {
931  if (f->subclass.integer == 'f') {
932  ast_debug(3, "Channel driver fax CNG detected on %s\n",
933  ast_channel_name(ast));
934  f = chan_pjsip_cng_tone_detected(ast, session, f);
935  /* When chan_pjsip_cng_tone_detected returns it is possible for the
936  * channel pointed to by ast and by session->channel to differ due to a
937  * masquerade. It's best not to touch things after this.
938  */
939  } else {
940  ast_debug(3, "* Detected inband DTMF '%c' on '%s'\n", f->subclass.integer,
941  ast_channel_name(ast));
942  }
943  }
944  }
945 
946  return f;
947 }
948 
949 static int chan_pjsip_write_stream(struct ast_channel *ast, int stream_num, struct ast_frame *frame)
950 {
952  struct ast_sip_session *session = channel->session;
953  struct ast_sip_session_media *media = NULL;
954  int res = 0;
955 
956  /* The core provides a guarantee that the stream will exist when we are called if stream_num is provided */
957  if (stream_num >= 0) {
958  /* What is not guaranteed is that a media session will exist */
959  if (stream_num < AST_VECTOR_SIZE(&channel->session->active_media_state->sessions)) {
960  media = AST_VECTOR_GET(&channel->session->active_media_state->sessions, stream_num);
961  }
962  }
963 
964  switch (frame->frametype) {
965  case AST_FRAME_VOICE:
966  if (!media) {
967  return 0;
968  } else if (media->type != AST_MEDIA_TYPE_AUDIO) {
969  ast_debug(3, "Channel %s stream %d is of type '%s', not audio!\n",
970  ast_channel_name(ast), stream_num, ast_codec_media_type2str(media->type));
971  return 0;
972  } else if (media == channel->session->active_media_state->default_session[AST_MEDIA_TYPE_AUDIO] &&
975  struct ast_str *write_transpath = ast_str_alloca(256);
976  struct ast_str *read_transpath = ast_str_alloca(256);
977 
979  "Channel %s asked to send %s frame when native formats are %s (rd:%s->%s;%s wr:%s->%s;%s)\n",
980  ast_channel_name(ast),
985  ast_translate_path_to_str(ast_channel_readtrans(ast), &read_transpath),
988  ast_translate_path_to_str(ast_channel_writetrans(ast), &write_transpath));
989  return 0;
990  } else if (media->write_callback) {
991  res = media->write_callback(session, media, frame);
992 
993  }
994  break;
995  case AST_FRAME_VIDEO:
996  if (!media) {
997  return 0;
998  } else if (media->type != AST_MEDIA_TYPE_VIDEO) {
999  ast_debug(3, "Channel %s stream %d is of type '%s', not video!\n",
1000  ast_channel_name(ast), stream_num, ast_codec_media_type2str(media->type));
1001  return 0;
1002  } else if (media->write_callback) {
1003  res = media->write_callback(session, media, frame);
1004  }
1005  break;
1006  case AST_FRAME_MODEM:
1007  if (!media) {
1008  return 0;
1009  } else if (media->type != AST_MEDIA_TYPE_IMAGE) {
1010  ast_debug(3, "Channel %s stream %d is of type '%s', not image!\n",
1011  ast_channel_name(ast), stream_num, ast_codec_media_type2str(media->type));
1012  return 0;
1013  } else if (media->write_callback) {
1014  res = media->write_callback(session, media, frame);
1015  }
1016  break;
1017  case AST_FRAME_CNG:
1018  break;
1019  case AST_FRAME_RTCP:
1020  /* We only support writing out feedback */
1021  if (frame->subclass.integer != AST_RTP_RTCP_PSFB || !media) {
1022  return 0;
1023  } else if (media->type != AST_MEDIA_TYPE_VIDEO) {
1024  ast_debug(3, "Channel %s stream %d is of type '%s', not video! Unable to write RTCP feedback.\n",
1025  ast_channel_name(ast), stream_num, ast_codec_media_type2str(media->type));
1026  return 0;
1027  } else if (media->write_callback) {
1028  res = media->write_callback(session, media, frame);
1029  }
1030  break;
1031  default:
1032  ast_log(LOG_WARNING, "Can't send %u type frames with PJSIP\n", frame->frametype);
1033  break;
1034  }
1035 
1036  return res;
1037 }
1038 
1039 static int chan_pjsip_write(struct ast_channel *ast, struct ast_frame *frame)
1040 {
1041  return chan_pjsip_write_stream(ast, -1, frame);
1042 }
1043 
1044 /*! \brief Function called by core to change the underlying owner channel */
1045 static int chan_pjsip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
1046 {
1048 
1049  if (channel->session->channel != oldchan) {
1050  return -1;
1051  }
1052 
1053  /*
1054  * The masquerade has suspended the channel's session
1055  * serializer so we can safely change it outside of
1056  * the serializer thread.
1057  */
1058  channel->session->channel = newchan;
1059 
1061 
1062  return 0;
1063 }
1064 
1065 /*! AO2 hash function for on hold UIDs */
1066 static int uid_hold_hash_fn(const void *obj, const int flags)
1067 {
1068  const char *key = obj;
1069 
1070  switch (flags & OBJ_SEARCH_MASK) {
1071  case OBJ_SEARCH_KEY:
1072  break;
1073  case OBJ_SEARCH_OBJECT:
1074  break;
1075  default:
1076  /* Hash can only work on something with a full key. */
1077  ast_assert(0);
1078  return 0;
1079  }
1080  return ast_str_hash(key);
1081 }
1082 
1083 /*! AO2 sort function for on hold UIDs */
1084 static int uid_hold_sort_fn(const void *obj_left, const void *obj_right, const int flags)
1085 {
1086  const char *left = obj_left;
1087  const char *right = obj_right;
1088  int cmp;
1089 
1090  switch (flags & OBJ_SEARCH_MASK) {
1091  case OBJ_SEARCH_OBJECT:
1092  case OBJ_SEARCH_KEY:
1093  cmp = strcmp(left, right);
1094  break;
1096  cmp = strncmp(left, right, strlen(right));
1097  break;
1098  default:
1099  /* Sort can only work on something with a full or partial key. */
1100  ast_assert(0);
1101  cmp = 0;
1102  break;
1103  }
1104  return cmp;
1105 }
1106 
1108 
1109 /*!
1110  * \brief Add a channel ID to the list of PJSIP channels on hold
1111  *
1112  * \param chan_uid - Unique ID of the channel being put into the hold list
1113  *
1114  * \retval 0 Channel has been added to or was already in the hold list
1115  * \retval -1 Failed to add channel to the hold list
1116  */
1117 static int chan_pjsip_add_hold(const char *chan_uid)
1118 {
1119  RAII_VAR(char *, hold_uid, NULL, ao2_cleanup);
1120 
1121  hold_uid = ao2_find(pjsip_uids_onhold, chan_uid, OBJ_SEARCH_KEY);
1122  if (hold_uid) {
1123  /* Device is already on hold. Nothing to do. */
1124  return 0;
1125  }
1126 
1127  /* Device wasn't in hold list already. Create a new one. */
1128  hold_uid = ao2_alloc_options(strlen(chan_uid) + 1, NULL,
1130  if (!hold_uid) {
1131  return -1;
1132  }
1133 
1134  ast_copy_string(hold_uid, chan_uid, strlen(chan_uid) + 1);
1135 
1136  if (ao2_link(pjsip_uids_onhold, hold_uid) == 0) {
1137  return -1;
1138  }
1139 
1140  return 0;
1141 }
1142 
1143 /*!
1144  * \brief Remove a channel ID from the list of PJSIP channels on hold
1145  *
1146  * \param chan_uid - Unique ID of the channel being taken out of the hold list
1147  */
1148 static void chan_pjsip_remove_hold(const char *chan_uid)
1149 {
1150  ao2_find(pjsip_uids_onhold, chan_uid, OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NODATA);
1151 }
1152 
1153 /*!
1154  * \brief Determine whether a channel ID is in the list of PJSIP channels on hold
1155  *
1156  * \param chan_uid - Channel being checked
1157  *
1158  * \retval 0 The channel is not in the hold list
1159  * \retval 1 The channel is in the hold list
1160  */
1161 static int chan_pjsip_get_hold(const char *chan_uid)
1162 {
1163  RAII_VAR(char *, hold_uid, NULL, ao2_cleanup);
1164 
1165  hold_uid = ao2_find(pjsip_uids_onhold, chan_uid, OBJ_SEARCH_KEY);
1166  if (!hold_uid) {
1167  return 0;
1168  }
1169 
1170  return 1;
1171 }
1172 
1173 /*! \brief Function called to get the device state of an endpoint */
1174 static int chan_pjsip_devicestate(const char *data)
1175 {
1176  RAII_VAR(struct ast_sip_endpoint *, endpoint, ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", data), ao2_cleanup);
1178  RAII_VAR(struct ast_endpoint_snapshot *, endpoint_snapshot, NULL, ao2_cleanup);
1179  struct ast_devstate_aggregate aggregate;
1180  int num, inuse = 0;
1181 
1182  if (!endpoint) {
1183  return AST_DEVICE_INVALID;
1184  }
1185 
1186  endpoint_snapshot = ast_endpoint_latest_snapshot(ast_endpoint_get_tech(endpoint->persistent),
1187  ast_endpoint_get_resource(endpoint->persistent));
1188 
1189  if (!endpoint_snapshot) {
1190  return AST_DEVICE_INVALID;
1191  }
1192 
1193  if (endpoint_snapshot->state == AST_ENDPOINT_OFFLINE) {
1194  state = AST_DEVICE_UNAVAILABLE;
1195  } else if (endpoint_snapshot->state == AST_ENDPOINT_ONLINE) {
1196  state = AST_DEVICE_NOT_INUSE;
1197  }
1198 
1199  if (!endpoint_snapshot->num_channels) {
1200  return state;
1201  }
1202 
1203  ast_devstate_aggregate_init(&aggregate);
1204 
1205  for (num = 0; num < endpoint_snapshot->num_channels; num++) {
1206  struct ast_channel_snapshot *snapshot;
1207 
1208  snapshot = ast_channel_snapshot_get_latest(endpoint_snapshot->channel_ids[num]);
1209  if (!snapshot) {
1210  continue;
1211  }
1212 
1213  if (chan_pjsip_get_hold(snapshot->base->uniqueid)) {
1215  } else {
1216  ast_devstate_aggregate_add(&aggregate, ast_state_chan2dev(snapshot->state));
1217  }
1218 
1219  if ((snapshot->state == AST_STATE_UP) || (snapshot->state == AST_STATE_RING) ||
1220  (snapshot->state == AST_STATE_BUSY)) {
1221  inuse++;
1222  }
1223 
1224  ao2_ref(snapshot, -1);
1225  }
1226 
1227  if (endpoint->devicestate_busy_at && (inuse == endpoint->devicestate_busy_at)) {
1228  state = AST_DEVICE_BUSY;
1229  } else if (ast_devstate_aggregate_result(&aggregate) != AST_DEVICE_INVALID) {
1230  state = ast_devstate_aggregate_result(&aggregate);
1231  }
1232 
1233  return state;
1234 }
1235 
1236 /*! \brief Function called to query options on a channel */
1237 static int chan_pjsip_queryoption(struct ast_channel *ast, int option, void *data, int *datalen)
1238 {
1240  int res = -1;
1242 
1243  if (!channel) {
1244  return -1;
1245  }
1246 
1247  switch (option) {
1248  case AST_OPTION_T38_STATE:
1249  if (channel->session->endpoint->media.t38.enabled) {
1250  switch (channel->session->t38state) {
1251  case T38_LOCAL_REINVITE:
1252  case T38_PEER_REINVITE:
1253  state = T38_STATE_NEGOTIATING;
1254  break;
1255  case T38_ENABLED:
1256  state = T38_STATE_NEGOTIATED;
1257  break;
1258  case T38_REJECTED:
1259  state = T38_STATE_REJECTED;
1260  break;
1261  default:
1262  state = T38_STATE_UNKNOWN;
1263  break;
1264  }
1265  }
1266 
1267  *((enum ast_t38_state *) data) = state;
1268  res = 0;
1269 
1270  break;
1271  default:
1272  break;
1273  }
1274 
1275  return res;
1276 }
1277 
1278 static const char *chan_pjsip_get_uniqueid(struct ast_channel *ast)
1279 {
1282 
1283  if (!uniqueid) {
1284  return "";
1285  }
1286 
1287  ast_copy_pj_str(uniqueid, &channel->session->inv_session->dlg->call_id->id, UNIQUEID_BUFSIZE);
1288 
1289  return uniqueid;
1290 }
1291 
1296  void *frame_data;
1297  size_t datalen;
1298 };
1299 
1300 static void indicate_data_destroy(void *obj)
1301 {
1302  struct indicate_data *ind_data = obj;
1303 
1304  ast_free(ind_data->frame_data);
1305  ao2_ref(ind_data->session, -1);
1306 }
1307 
1308 static struct indicate_data *indicate_data_alloc(struct ast_sip_session *session,
1309  int condition, int response_code, const void *frame_data, size_t datalen)
1310 {
1311  struct indicate_data *ind_data = ao2_alloc(sizeof(*ind_data), indicate_data_destroy);
1312 
1313  if (!ind_data) {
1314  return NULL;
1315  }
1316 
1317  ind_data->frame_data = ast_malloc(datalen);
1318  if (!ind_data->frame_data) {
1319  ao2_ref(ind_data, -1);
1320  return NULL;
1321  }
1322 
1323  memcpy(ind_data->frame_data, frame_data, datalen);
1324  ind_data->datalen = datalen;
1325  ind_data->condition = condition;
1326  ind_data->response_code = response_code;
1327  ao2_ref(session, +1);
1328  ind_data->session = session;
1329 
1330  return ind_data;
1331 }
1332 
1333 static int indicate(void *data)
1334 {
1335  pjsip_tx_data *packet = NULL;
1336  struct indicate_data *ind_data = data;
1337  struct ast_sip_session *session = ind_data->session;
1338  int response_code = ind_data->response_code;
1339 
1340  if ((session->inv_session->state != PJSIP_INV_STATE_DISCONNECTED) &&
1341  (pjsip_inv_answer(session->inv_session, response_code, NULL, NULL, &packet) == PJ_SUCCESS)) {
1342  ast_sip_session_send_response(session, packet);
1343  }
1344 
1345  ao2_ref(ind_data, -1);
1346 
1347  return 0;
1348 }
1349 
1350 /*! \brief Send SIP INFO with video update request */
1351 static int transmit_info_with_vidupdate(void *data)
1352 {
1353  const char * xml =
1354  "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n"
1355  " <media_control>\r\n"
1356  " <vc_primitive>\r\n"
1357  " <to_encoder>\r\n"
1358  " <picture_fast_update/>\r\n"
1359  " </to_encoder>\r\n"
1360  " </vc_primitive>\r\n"
1361  " </media_control>\r\n";
1362 
1363  const struct ast_sip_body body = {
1364  .type = "application",
1365  .subtype = "media_control+xml",
1366  .body_text = xml
1367  };
1368 
1369  RAII_VAR(struct ast_sip_session *, session, data, ao2_cleanup);
1370  struct pjsip_tx_data *tdata;
1371 
1372  if (session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
1373  ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
1374  session->inv_session->cause,
1375  pjsip_get_status_text(session->inv_session->cause)->ptr);
1376  return -1;
1377  }
1378 
1379  if (ast_sip_create_request("INFO", session->inv_session->dlg, session->endpoint, NULL, NULL, &tdata)) {
1380  ast_log(LOG_ERROR, "Could not create text video update INFO request\n");
1381  return -1;
1382  }
1383  if (ast_sip_add_body(tdata, &body)) {
1384  ast_log(LOG_ERROR, "Could not add body to text video update INFO request\n");
1385  return -1;
1386  }
1387  ast_sip_session_send_request(session, tdata);
1388 
1389  return 0;
1390 }
1391 
1392 /*!
1393  * \internal
1394  * \brief TRUE if a COLP update can be sent to the peer.
1395  * \since 13.3.0
1396  *
1397  * \param session The session to see if the COLP update is allowed.
1398  *
1399  * \retval 0 Update is not allowed.
1400  * \retval 1 Update is allowed.
1401  */
1402 static int is_colp_update_allowed(struct ast_sip_session *session)
1403 {
1404  struct ast_party_id connected_id;
1405  int update_allowed = 0;
1406 
1407  if (!session->endpoint->id.send_connected_line
1408  || (!session->endpoint->id.send_pai && !session->endpoint->id.send_rpid)) {
1409  return 0;
1410  }
1411 
1412  /*
1413  * Check if privacy allows the update. Check while the channel
1414  * is locked so we can work with the shallow connected_id copy.
1415  */
1416  ast_channel_lock(session->channel);
1417  connected_id = ast_channel_connected_effective_id(session->channel);
1418  if (connected_id.number.valid
1419  && (session->endpoint->id.trust_outbound
1421  update_allowed = 1;
1422  }
1423  ast_channel_unlock(session->channel);
1424 
1425  return update_allowed;
1426 }
1427 
1428 /*! \brief Update connected line information */
1430 {
1431  struct ast_sip_session *session = data;
1432 
1433  if (session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
1434  ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
1435  session->inv_session->cause,
1436  pjsip_get_status_text(session->inv_session->cause)->ptr);
1437  ao2_ref(session, -1);
1438  return -1;
1439  }
1440 
1441  if (ast_channel_state(session->channel) == AST_STATE_UP
1442  || session->inv_session->role == PJSIP_ROLE_UAC) {
1443  if (is_colp_update_allowed(session)) {
1445  int generate_new_sdp;
1446 
1447  method = session->endpoint->id.refresh_method;
1448  if (session->inv_session->options & PJSIP_INV_SUPPORT_UPDATE) {
1450  }
1451 
1452  /* Only the INVITE method actually needs SDP, UPDATE can do without */
1453  generate_new_sdp = (method == AST_SIP_SESSION_REFRESH_METHOD_INVITE);
1454 
1455  ast_sip_session_refresh(session, NULL, NULL, NULL, method, generate_new_sdp, NULL);
1456  }
1457  } else if (session->endpoint->id.rpid_immediate
1458  && session->inv_session->state != PJSIP_INV_STATE_DISCONNECTED
1459  && is_colp_update_allowed(session)) {
1460  int response_code = 0;
1461 
1462  if (ast_channel_state(session->channel) == AST_STATE_RING) {
1463  response_code = !session->endpoint->inband_progress ? 180 : 183;
1464  } else if (ast_channel_state(session->channel) == AST_STATE_RINGING) {
1465  response_code = 183;
1466  }
1467 
1468  if (response_code) {
1469  struct pjsip_tx_data *packet = NULL;
1470 
1471  if (pjsip_inv_answer(session->inv_session, response_code, NULL, NULL, &packet) == PJ_SUCCESS) {
1472  ast_sip_session_send_response(session, packet);
1473  }
1474  }
1475  }
1476 
1477  ao2_ref(session, -1);
1478  return 0;
1479 }
1480 
1481 /*! \brief Callback which changes the value of locally held on the media stream */
1482 static void local_hold_set_state(struct ast_sip_session_media *session_media, unsigned int held)
1483 {
1484  if (session_media) {
1485  session_media->locally_held = held;
1486  }
1487 }
1488 
1489 /*! \brief Update local hold state and send a re-INVITE with the new SDP */
1490 static int remote_send_hold_refresh(struct ast_sip_session *session, unsigned int held)
1491 {
1494  ao2_ref(session, -1);
1495 
1496  return 0;
1497 }
1498 
1499 /*! \brief Update local hold state to be held */
1500 static int remote_send_hold(void *data)
1501 {
1502  return remote_send_hold_refresh(data, 1);
1503 }
1504 
1505 /*! \brief Update local hold state to be unheld */
1506 static int remote_send_unhold(void *data)
1507 {
1508  return remote_send_hold_refresh(data, 0);
1509 }
1510 
1514 };
1515 
1517 {
1518  ao2_cleanup(refresh_data->session);
1519 
1521  ast_free(refresh_data);
1522 }
1523 
1525  struct ast_sip_session *session, const struct ast_stream_topology *topology)
1526 {
1528 
1529  refresh_data = ast_calloc(1, sizeof(*refresh_data));
1530  if (!refresh_data) {
1531  return NULL;
1532  }
1533 
1534  refresh_data->session = ao2_bump(session);
1536  if (!refresh_data->media_state) {
1537  topology_change_refresh_data_free(refresh_data);
1538  return NULL;
1539  }
1540  refresh_data->media_state->topology = ast_stream_topology_clone(topology);
1541  if (!refresh_data->media_state->topology) {
1542  topology_change_refresh_data_free(refresh_data);
1543  return NULL;
1544  }
1545 
1546  return refresh_data;
1547 }
1548 
1549 static int on_topology_change_response(struct ast_sip_session *session, pjsip_rx_data *rdata)
1550 {
1551  SCOPE_ENTER(3, "%s: Received response code %d. PT: %s AT: %s\n", ast_sip_session_get_name(session),
1552  rdata->msg_info.msg->line.status.code,
1555 
1556 
1557  if (PJSIP_IS_STATUS_IN_CLASS(rdata->msg_info.msg->line.status.code, 200)) {
1558  /* The topology was changed to something new so give notice to what requested
1559  * it so it queries the channel and updates accordingly.
1560  */
1561  if (session->channel) {
1563  SCOPE_EXIT_RTN_VALUE(0, "%s: Queued topology change frame\n", ast_sip_session_get_name(session));
1564  }
1565  SCOPE_EXIT_RTN_VALUE(0, "%s: No channel? Can't queue topology change frame\n", ast_sip_session_get_name(session));
1566  } else if (300 <= rdata->msg_info.msg->line.status.code) {
1567  /* The topology change failed, so drop the current pending media state */
1569  SCOPE_EXIT_RTN_VALUE(0, "%s: response code > 300. Resetting pending media state\n", ast_sip_session_get_name(session));
1570  }
1571 
1572  SCOPE_EXIT_RTN_VALUE(0, "%s: Nothing to do\n", ast_sip_session_get_name(session));
1573 }
1574 
1575 static int send_topology_change_refresh(void *data)
1576 {
1578  struct ast_sip_session *session = refresh_data->session;
1579  int ret;
1580  SCOPE_ENTER(3, "%s: %s\n", ast_sip_session_get_name(session),
1581  ast_str_tmp(256, ast_stream_topology_to_str(refresh_data->media_state->topology, &STR_TMP)));
1582 
1583 
1586  refresh_data->media_state = NULL;
1587  topology_change_refresh_data_free(refresh_data);
1588 
1589  SCOPE_EXIT_RTN_VALUE(ret, "%s\n", ast_sip_session_get_name(session));
1590 }
1591 
1593  const struct ast_stream_topology *proposed)
1594 {
1596  int res;
1597  SCOPE_ENTER(1);
1598 
1599  refresh_data = topology_change_refresh_data_alloc(session, proposed);
1600  if (!refresh_data) {
1601  SCOPE_EXIT_RTN_VALUE(-1, "Couldn't create refresh_data\n");
1602  }
1603 
1604  res = ast_sip_push_task(session->serializer, send_topology_change_refresh, refresh_data);
1605  if (res) {
1606  topology_change_refresh_data_free(refresh_data);
1607  }
1608  SCOPE_EXIT_RTN_VALUE(res, "RC: %d\n", res);
1609 }
1610 
1611 /*! \brief Function called by core to ask the channel to indicate some sort of condition */
1612 static int chan_pjsip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
1613 {
1615  struct ast_sip_session_media *media;
1616  int response_code = 0;
1617  int res = 0;
1618  char *device_buf;
1619  size_t device_buf_size;
1620  int i;
1621  const struct ast_stream_topology *topology;
1622  struct ast_frame f = {
1624  .subclass = {
1625  .integer = condition
1626  }
1627  };
1628  char condition_name[256];
1629  SCOPE_ENTER(3, "%s: Indicated %s\n", ast_channel_name(ast),
1630  ast_frame_subclass2str(&f, condition_name, sizeof(condition_name), NULL, 0));
1631 
1632  switch (condition) {
1633  case AST_CONTROL_RINGING:
1634  if (ast_channel_state(ast) == AST_STATE_RING) {
1635  if (channel->session->endpoint->inband_progress ||
1636  (channel->session->inv_session && channel->session->inv_session->neg &&
1637  pjmedia_sdp_neg_get_state(channel->session->inv_session->neg) == PJMEDIA_SDP_NEG_STATE_DONE)) {
1638  response_code = 183;
1639  res = -1;
1640  } else {
1641  response_code = 180;
1642  }
1643  } else {
1644  res = -1;
1645  }
1647  break;
1648  case AST_CONTROL_BUSY:
1649  if (ast_channel_state(ast) != AST_STATE_UP) {
1650  response_code = 486;
1651  } else {
1652  res = -1;
1653  }
1654  break;
1656  if (ast_channel_state(ast) != AST_STATE_UP) {
1657  response_code = 503;
1658  } else {
1659  res = -1;
1660  }
1661  break;
1663  if (ast_channel_state(ast) != AST_STATE_UP) {
1664  response_code = 484;
1665  } else {
1666  res = -1;
1667  }
1668  break;
1670  if (ast_channel_state(ast) != AST_STATE_UP) {
1671  response_code = 100;
1672  } else {
1673  res = -1;
1674  }
1675  break;
1676  case AST_CONTROL_PROGRESS:
1677  if (ast_channel_state(ast) != AST_STATE_UP) {
1678  response_code = 183;
1679  } else {
1680  res = -1;
1681  }
1683  break;
1684  case AST_CONTROL_VIDUPDATE:
1685  for (i = 0; i < AST_VECTOR_SIZE(&channel->session->active_media_state->sessions); ++i) {
1686  media = AST_VECTOR_GET(&channel->session->active_media_state->sessions, i);
1687  if (!media || media->type != AST_MEDIA_TYPE_VIDEO) {
1688  continue;
1689  }
1690  if (media->rtp) {
1691  /* FIXME: Only use this for VP8. Additional work would have to be done to
1692  * fully support other video codecs */
1693 
1697  (channel->session->endpoint->media.webrtc &&
1699  /* FIXME Fake RTP write, this will be sent as an RTCP packet. Ideally the
1700  * RTP engine would provide a way to externally write/schedule RTCP
1701  * packets */
1702  struct ast_frame fr;
1705  res = ast_rtp_instance_write(media->rtp, &fr);
1706  } else {
1707  ao2_ref(channel->session, +1);
1709  ao2_cleanup(channel->session);
1710  }
1711  }
1712  ast_test_suite_event_notify("AST_CONTROL_VIDUPDATE", "Result: Success");
1713  } else {
1714  ast_test_suite_event_notify("AST_CONTROL_VIDUPDATE", "Result: Failure");
1715  res = -1;
1716  }
1717  }
1718  /* XXX If there were no video streams, then this should set
1719  * res to -1
1720  */
1721  break;
1723  ao2_ref(channel->session, +1);
1725  ao2_cleanup(channel->session);
1726  }
1727  break;
1729  break;
1731  res = -1;
1732  break;
1734  ast_assert(datalen == sizeof(int));
1735  if (*(int *) data) {
1736  /*
1737  * Masquerade is beginning:
1738  * Wait for session serializer to get suspended.
1739  */
1740  ast_channel_unlock(ast);
1741  ast_sip_session_suspend(channel->session);
1742  ast_channel_lock(ast);
1743  } else {
1744  /*
1745  * Masquerade is complete:
1746  * Unsuspend the session serializer.
1747  */
1749  }
1750  break;
1751  case AST_CONTROL_HOLD:
1753  device_buf_size = strlen(ast_channel_name(ast)) + 1;
1754  device_buf = alloca(device_buf_size);
1755  ast_channel_get_device_name(ast, device_buf, device_buf_size);
1757  if (!channel->session->moh_passthrough) {
1758  ast_moh_start(ast, data, NULL);
1759  } else {
1761  ast_log(LOG_WARNING, "Could not queue task to remotely put session '%s' on hold with endpoint '%s'\n",
1763  ao2_ref(channel->session, -1);
1764  }
1765  }
1766  break;
1767  case AST_CONTROL_UNHOLD:
1769  device_buf_size = strlen(ast_channel_name(ast)) + 1;
1770  device_buf = alloca(device_buf_size);
1771  ast_channel_get_device_name(ast, device_buf, device_buf_size);
1773  if (!channel->session->moh_passthrough) {
1774  ast_moh_stop(ast);
1775  } else {
1777  ast_log(LOG_WARNING, "Could not queue task to remotely take session '%s' off hold with endpoint '%s'\n",
1779  ao2_ref(channel->session, -1);
1780  }
1781  }
1782  break;
1783  case AST_CONTROL_SRCUPDATE:
1784  break;
1785  case AST_CONTROL_SRCCHANGE:
1786  break;
1788  if (ast_channel_state(ast) != AST_STATE_UP) {
1789  response_code = 181;
1790  } else {
1791  res = -1;
1792  }
1793  break;
1795  res = 0;
1796 
1797  if (channel->session->t38state == T38_PEER_REINVITE) {
1798  const struct ast_control_t38_parameters *parameters = data;
1799 
1800  if (parameters->request_response == AST_T38_REQUEST_PARMS) {
1801  res = AST_T38_REQUEST_PARMS;
1802  }
1803  }
1804 
1805  break;
1807  topology = data;
1808  ast_trace(-1, "%s: New topology: %s\n", ast_channel_name(ast),
1809  ast_str_tmp(256, ast_stream_topology_to_str(topology, &STR_TMP)));
1810  res = handle_topology_request_change(channel->session, topology);
1811  break;
1813  break;
1815  break;
1816  case -1:
1817  res = -1;
1818  break;
1819  default:
1820  ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", condition);
1821  res = -1;
1822  break;
1823  }
1824 
1825  if (response_code) {
1826  struct indicate_data *ind_data = indicate_data_alloc(channel->session, condition, response_code, data, datalen);
1827 
1828  if (!ind_data) {
1829  SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR, "%s: Couldn't alloc indicate data\n", ast_channel_name(ast));
1830  }
1831 
1832  if (ast_sip_push_task(channel->session->serializer, indicate, ind_data)) {
1833  ast_log(LOG_ERROR, "%s: Cannot send response code %d to endpoint %s. Could not queue task properly\n",
1834  ast_channel_name(ast), response_code, ast_sorcery_object_get_id(channel->session->endpoint));
1835  ao2_cleanup(ind_data);
1836  res = -1;
1837  }
1838  }
1839 
1840  SCOPE_EXIT_RTN_VALUE(res, "%s\n", ast_channel_name(ast));
1841 }
1842 
1845  char *target;
1846 };
1847 
1848 static void transfer_data_destroy(void *obj)
1849 {
1850  struct transfer_data *trnf_data = obj;
1851 
1852  ast_free(trnf_data->target);
1853  ao2_cleanup(trnf_data->session);
1854 }
1855 
1856 static struct transfer_data *transfer_data_alloc(struct ast_sip_session *session, const char *target)
1857 {
1858  struct transfer_data *trnf_data = ao2_alloc(sizeof(*trnf_data), transfer_data_destroy);
1859 
1860  if (!trnf_data) {
1861  return NULL;
1862  }
1863 
1864  if (!(trnf_data->target = ast_strdup(target))) {
1865  ao2_ref(trnf_data, -1);
1866  return NULL;
1867  }
1868 
1869  ao2_ref(session, +1);
1870  trnf_data->session = session;
1871 
1872  return trnf_data;
1873 }
1874 
1875 static void transfer_redirect(struct ast_sip_session *session, const char *target)
1876 {
1877  pjsip_tx_data *packet;
1879  pjsip_contact_hdr *contact;
1880  pj_str_t tmp;
1881 
1882  if (pjsip_inv_end_session(session->inv_session, 302, NULL, &packet) != PJ_SUCCESS
1883  || !packet) {
1884  ast_log(LOG_WARNING, "Failed to redirect PJSIP session for channel %s\n",
1885  ast_channel_name(session->channel));
1886  message = AST_TRANSFER_FAILED;
1887  ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
1888 
1889  return;
1890  }
1891 
1892  if (!(contact = pjsip_msg_find_hdr(packet->msg, PJSIP_H_CONTACT, NULL))) {
1893  contact = pjsip_contact_hdr_create(packet->pool);
1894  }
1895 
1896  pj_strdup2_with_null(packet->pool, &tmp, target);
1897  if (!(contact->uri = pjsip_parse_uri(packet->pool, tmp.ptr, tmp.slen, PJSIP_PARSE_URI_AS_NAMEADDR))) {
1898  ast_log(LOG_WARNING, "Failed to parse destination URI '%s' for channel %s\n",
1899  target, ast_channel_name(session->channel));
1900  message = AST_TRANSFER_FAILED;
1901  ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
1902  pjsip_tx_data_dec_ref(packet);
1903 
1904  return;
1905  }
1906  pjsip_msg_add_hdr(packet->msg, (pjsip_hdr *) contact);
1907 
1908  ast_sip_session_send_response(session, packet);
1909  ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
1910 }
1911 
1912 /*! \brief REFER Callback module, used to attach session data structure to subscription */
1913 static pjsip_module refer_callback_module = {
1914  .name = { "REFER Callback", 14 },
1915  .id = -1,
1916 };
1917 
1918 /*!
1919  * \brief Callback function to report status of implicit REFER-NOTIFY subscription.
1920  *
1921  * This function will be called on any state change in the REFER-NOTIFY subscription.
1922  * Its primary purpose is to report SUCCESS/FAILURE of a transfer initiated via
1923  * \ref transfer_refer as well as to terminate the subscription, if necessary.
1924  */
1925 static void xfer_client_on_evsub_state(pjsip_evsub *sub, pjsip_event *event)
1926 {
1927  struct ast_channel *chan;
1929  int res = 0;
1930 
1931  if (!event) {
1932  return;
1933  }
1934 
1935  chan = pjsip_evsub_get_mod_data(sub, refer_callback_module.id);
1936  if (!chan) {
1937  return;
1938  }
1939 
1940  if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_ACCEPTED) {
1941  /* Check if subscription is suppressed and terminate and send completion code, if so. */
1942  pjsip_rx_data *rdata;
1943  pjsip_generic_string_hdr *refer_sub;
1944  const pj_str_t REFER_SUB = { "Refer-Sub", 9 };
1945 
1946  ast_debug(3, "Transfer accepted on channel %s\n", ast_channel_name(chan));
1947 
1948  /* Check if response message */
1949  if (event->type == PJSIP_EVENT_TSX_STATE && event->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
1950  rdata = event->body.tsx_state.src.rdata;
1951 
1952  /* Find Refer-Sub header */
1953  refer_sub = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &REFER_SUB, NULL);
1954 
1955  /* Check if subscription is suppressed. If it is, the far end will not terminate it,
1956  * and the subscription will remain active until it times out. Terminating it here
1957  * eliminates the unnecessary timeout.
1958  */
1959  if (refer_sub && !pj_stricmp2(&refer_sub->hvalue, "false")) {
1960  /* Since no subscription is desired, assume that call has been transferred successfully. */
1961  /* Channel reference will be released at end of function */
1962  /* Terminate subscription. */
1963  pjsip_evsub_set_mod_data(sub, refer_callback_module.id, NULL);
1964  pjsip_evsub_terminate(sub, PJ_TRUE);
1965  res = -1;
1966  }
1967  }
1968  } else if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_ACTIVE ||
1969  pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) {
1970  /* Check for NOTIFY complete or error. */
1971  pjsip_msg *msg;
1972  pjsip_msg_body *body;
1973  pjsip_status_line status_line = { .code = 0 };
1974  pj_bool_t is_last;
1975  pj_status_t status;
1976 
1977  if (event->type == PJSIP_EVENT_TSX_STATE && event->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
1978  pjsip_rx_data *rdata;
1979 
1980  rdata = event->body.tsx_state.src.rdata;
1981  msg = rdata->msg_info.msg;
1982 
1983  if (!pjsip_method_cmp(&msg->line.req.method, pjsip_get_notify_method())) {
1984  body = msg->body;
1985  if (body && !pj_stricmp2(&body->content_type.type, "message")
1986  && !pj_stricmp2(&body->content_type.subtype, "sipfrag")) {
1987  pjsip_parse_status_line((char *)body->data, body->len, &status_line);
1988  }
1989  }
1990  } else {
1991  status_line.code = 500;
1992  status_line.reason = *pjsip_get_status_text(500);
1993  }
1994 
1995  is_last = (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED);
1996  /* If the status code is >= 200, the subscription is finished. */
1997  if (status_line.code >= 200 || is_last) {
1998  res = -1;
1999 
2000  /* If the subscription has terminated, return AST_TRANSFER_SUCCESS for 2XX.
2001  * Any other status code returns AST_TRANSFER_FAILED.
2002  * The subscription should not terminate for any code < 200,
2003  * but if it does, that constitutes a failure. */
2004  if (status_line.code < 200 || status_line.code >= 300) {
2005  message = AST_TRANSFER_FAILED;
2006  }
2007  /* If subscription not terminated and subscription is finished (status code >= 200)
2008  * terminate it */
2009  if (!is_last) {
2010  pjsip_tx_data *tdata;
2011 
2012  status = pjsip_evsub_initiate(sub, pjsip_get_subscribe_method(), 0, &tdata);
2013  if (status == PJ_SUCCESS) {
2014  pjsip_evsub_send_request(sub, tdata);
2015  }
2016  }
2017  /* Finished. Remove session from subscription */
2018  pjsip_evsub_set_mod_data(sub, refer_callback_module.id, NULL);
2019  ast_debug(3, "Transfer channel %s completed: %d %.*s (%s)\n",
2020  ast_channel_name(chan),
2021  status_line.code,
2022  (int)status_line.reason.slen, status_line.reason.ptr,
2023  (message == AST_TRANSFER_SUCCESS) ? "Success" : "Failure");
2024  }
2025  }
2026 
2027  if (res) {
2028  ast_queue_control_data(chan, AST_CONTROL_TRANSFER, &message, sizeof(message));
2029  ao2_ref(chan, -1);
2030  }
2031 }
2032 
2033 static void transfer_refer(struct ast_sip_session *session, const char *target)
2034 {
2035  pjsip_evsub *sub;
2037  pj_str_t tmp;
2038  pjsip_tx_data *packet;
2039  const char *ref_by_val;
2040  char local_info[pj_strlen(&session->inv_session->dlg->local.info_str) + 1];
2041  struct pjsip_evsub_user xfer_cb;
2042  struct ast_channel *chan = session->channel;
2043 
2044  pj_bzero(&xfer_cb, sizeof(xfer_cb));
2045  xfer_cb.on_evsub_state = &xfer_client_on_evsub_state;
2046 
2047  if (pjsip_xfer_create_uac(session->inv_session->dlg, &xfer_cb, &sub) != PJ_SUCCESS) {
2048  message = AST_TRANSFER_FAILED;
2049  ast_queue_control_data(chan, AST_CONTROL_TRANSFER, &message, sizeof(message));
2050 
2051  return;
2052  }
2053 
2054  /* refer_callback_module requires a reference to chan
2055  * which will be released in xfer_client_on_evsub_state()
2056  * when the implicit REFER subscription terminates */
2057  pjsip_evsub_set_mod_data(sub, refer_callback_module.id, chan);
2058  ao2_ref(chan, +1);
2059 
2060  if (pjsip_xfer_initiate(sub, pj_cstr(&tmp, target), &packet) != PJ_SUCCESS) {
2061  goto failure;
2062  }
2063 
2064  ref_by_val = pbx_builtin_getvar_helper(chan, "SIPREFERREDBYHDR");
2065  if (!ast_strlen_zero(ref_by_val)) {
2066  ast_sip_add_header(packet, "Referred-By", ref_by_val);
2067  } else {
2068  ast_copy_pj_str(local_info, &session->inv_session->dlg->local.info_str, sizeof(local_info));
2069  ast_sip_add_header(packet, "Referred-By", local_info);
2070  }
2071 
2072  if (pjsip_xfer_send_request(sub, packet) == PJ_SUCCESS) {
2073  return;
2074  }
2075 
2076 failure:
2077  message = AST_TRANSFER_FAILED;
2078  ast_queue_control_data(chan, AST_CONTROL_TRANSFER, &message, sizeof(message));
2079  pjsip_evsub_set_mod_data(sub, refer_callback_module.id, NULL);
2080  pjsip_evsub_terminate(sub, PJ_FALSE);
2081 
2082  ao2_ref(chan, -1);
2083 }
2084 
2085 static int transfer(void *data)
2086 {
2087  struct transfer_data *trnf_data = data;
2088  struct ast_sip_endpoint *endpoint = NULL;
2089  struct ast_sip_contact *contact = NULL;
2090  const char *target = trnf_data->target;
2091 
2092  if (trnf_data->session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
2093  ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
2094  trnf_data->session->inv_session->cause,
2095  pjsip_get_status_text(trnf_data->session->inv_session->cause)->ptr);
2096  } else {
2097  /* See if we have an endpoint; if so, use its contact */
2098  endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", target);
2099  if (endpoint) {
2101  if (contact && !ast_strlen_zero(contact->uri)) {
2102  target = contact->uri;
2103  }
2104  }
2105 
2106  if (ast_channel_state(trnf_data->session->channel) == AST_STATE_RING) {
2107  transfer_redirect(trnf_data->session, target);
2108  } else {
2109  transfer_refer(trnf_data->session, target);
2110  }
2111  }
2112 
2113  ao2_ref(trnf_data, -1);
2114  ao2_cleanup(endpoint);
2115  ao2_cleanup(contact);
2116  return 0;
2117 }
2118 
2119 /*! \brief Function called by core for Asterisk initiated transfer */
2120 static int chan_pjsip_transfer(struct ast_channel *chan, const char *target)
2121 {
2123  struct transfer_data *trnf_data = transfer_data_alloc(channel->session, target);
2124 
2125  if (!trnf_data) {
2126  return -1;
2127  }
2128 
2129  if (ast_sip_push_task(channel->session->serializer, transfer, trnf_data)) {
2130  ast_log(LOG_WARNING, "Error requesting transfer\n");
2131  ao2_cleanup(trnf_data);
2132  return -1;
2133  }
2134 
2135  return 0;
2136 }
2137 
2138 /*! \brief Function called by core to start a DTMF digit */
2140 {
2142  struct ast_sip_session_media *media;
2143 
2145 
2146  switch (channel->session->dtmf) {
2147  case AST_SIP_DTMF_RFC_4733:
2148  if (!media || !media->rtp) {
2149  return 0;
2150  }
2151 
2152  ast_rtp_instance_dtmf_begin(media->rtp, digit);
2153  break;
2154  case AST_SIP_DTMF_AUTO:
2155  if (!media || !media->rtp) {
2156  return 0;
2157  }
2158 
2160  return -1;
2161  }
2162 
2163  ast_rtp_instance_dtmf_begin(media->rtp, digit);
2164  break;
2166  if (!media || !media->rtp || (ast_rtp_instance_dtmf_mode_get(media->rtp) == AST_RTP_DTMF_MODE_NONE)) {
2167  return 0;
2168  }
2169  ast_rtp_instance_dtmf_begin(media->rtp, digit);
2170  break;
2171  case AST_SIP_DTMF_NONE:
2172  break;
2173  case AST_SIP_DTMF_INBAND:
2174  return -1;
2175  default:
2176  break;
2177  }
2178 
2179  return 0;
2180 }
2181 
2184  char digit;
2185  unsigned int duration;
2186 };
2187 
2188 static void info_dtmf_data_destroy(void *obj)
2189 {
2190  struct info_dtmf_data *dtmf_data = obj;
2191  ao2_ref(dtmf_data->session, -1);
2192 }
2193 
2194 static struct info_dtmf_data *info_dtmf_data_alloc(struct ast_sip_session *session, char digit, unsigned int duration)
2195 {
2196  struct info_dtmf_data *dtmf_data = ao2_alloc(sizeof(*dtmf_data), info_dtmf_data_destroy);
2197  if (!dtmf_data) {
2198  return NULL;
2199  }
2200  ao2_ref(session, +1);
2201  dtmf_data->session = session;
2202  dtmf_data->digit = digit;
2203  dtmf_data->duration = duration;
2204  return dtmf_data;
2205 }
2206 
2207 static int transmit_info_dtmf(void *data)
2208 {
2209  RAII_VAR(struct info_dtmf_data *, dtmf_data, data, ao2_cleanup);
2210 
2211  struct ast_sip_session *session = dtmf_data->session;
2212  struct pjsip_tx_data *tdata;
2213 
2214  RAII_VAR(struct ast_str *, body_text, NULL, ast_free_ptr);
2215 
2216  struct ast_sip_body body = {
2217  .type = "application",
2218  .subtype = "dtmf-relay",
2219  };
2220 
2221  if (session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
2222  ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
2223  session->inv_session->cause,
2224  pjsip_get_status_text(session->inv_session->cause)->ptr);
2225  return -1;
2226  }
2227 
2228  if (!(body_text = ast_str_create(32))) {
2229  ast_log(LOG_ERROR, "Could not allocate buffer for INFO DTMF.\n");
2230  return -1;
2231  }
2232  ast_str_set(&body_text, 0, "Signal=%c\r\nDuration=%u\r\n", dtmf_data->digit, dtmf_data->duration);
2233 
2235 
2236  if (ast_sip_create_request("INFO", session->inv_session->dlg, session->endpoint, NULL, NULL, &tdata)) {
2237  ast_log(LOG_ERROR, "Could not create DTMF INFO request\n");
2238  return -1;
2239  }
2240  if (ast_sip_add_body(tdata, &body)) {
2241  ast_log(LOG_ERROR, "Could not add body to DTMF INFO request\n");
2242  pjsip_tx_data_dec_ref(tdata);
2243  return -1;
2244  }
2245  ast_sip_session_send_request(session, tdata);
2246 
2247  return 0;
2248 }
2249 
2250 /*! \brief Function called by core to stop a DTMF digit */
2251 static int chan_pjsip_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
2252 {
2254  struct ast_sip_session_media *media;
2255 
2256  if (!channel || !channel->session) {
2257  /* This happens when the channel is hungup while a DTMF digit is playing. See ASTERISK-28086 */
2258  ast_debug(3, "Channel %s disappeared while calling digit_end\n", ast_channel_name(ast));
2259  return -1;
2260  }
2261 
2263 
2264  switch (channel->session->dtmf) {
2266  {
2267  if (!media || !media->rtp) {
2268  return 0;
2269  }
2270 
2272  ast_debug(3, "Told to send end of digit on Auto-Info channel %s RFC4733 negotiated so using it.\n", ast_channel_name(ast));
2273  ast_rtp_instance_dtmf_end_with_duration(media->rtp, digit, duration);
2274  break;
2275  }
2276  /* If RFC_4733 was not negotiated, fail through to the DTMF_INFO processing */
2277  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));
2278  }
2279 
2280  case AST_SIP_DTMF_INFO:
2281  {
2282  struct info_dtmf_data *dtmf_data = info_dtmf_data_alloc(channel->session, digit, duration);
2283 
2284  if (!dtmf_data) {
2285  return -1;
2286  }
2287 
2288  if (ast_sip_push_task(channel->session->serializer, transmit_info_dtmf, dtmf_data)) {
2289  ast_log(LOG_WARNING, "Error sending DTMF via INFO.\n");
2290  ao2_cleanup(dtmf_data);
2291  return -1;
2292  }
2293  break;
2294  }
2295  case AST_SIP_DTMF_RFC_4733:
2296  if (!media || !media->rtp) {
2297  return 0;
2298  }
2299 
2300  ast_rtp_instance_dtmf_end_with_duration(media->rtp, digit, duration);
2301  break;
2302  case AST_SIP_DTMF_AUTO:
2303  if (!media || !media->rtp) {
2304  return 0;
2305  }
2306 
2308  return -1;
2309  }
2310 
2311  ast_rtp_instance_dtmf_end_with_duration(media->rtp, digit, duration);
2312  break;
2313  case AST_SIP_DTMF_NONE:
2314  break;
2315  case AST_SIP_DTMF_INBAND:
2316  return -1;
2317  }
2318 
2319  return 0;
2320 }
2321 
2323 {
2324  struct ast_party_connected_line connected;
2325 
2326  /*
2327  * Use the channel CALLERID() as the initial connected line data.
2328  * The core or a predial handler may have supplied missing values
2329  * from the session->endpoint->id.self about who we are calling.
2330  */
2331  ast_channel_lock(session->channel);
2332  ast_party_id_copy(&session->id, &ast_channel_caller(session->channel)->id);
2333  ast_channel_unlock(session->channel);
2334 
2335  /* Supply initial connected line information if available. */
2336  if (!session->id.number.valid && !session->id.name.valid) {
2337  return;
2338  }
2339 
2340  ast_party_connected_line_init(&connected);
2341  connected.id = session->id;
2343 
2344  ast_channel_queue_connected_line_update(session->channel, &connected, NULL);
2345 }
2346 
2347 static int call(void *data)
2348 {
2349  struct ast_sip_channel_pvt *channel = data;
2350  struct ast_sip_session *session = channel->session;
2351  pjsip_tx_data *tdata;
2352  int res = 0;
2353  SCOPE_ENTER(1, "%s Topology: %s\n",
2354  ast_sip_session_get_name(session),
2356  );
2357 
2358 
2359  res = ast_sip_session_create_invite(session, &tdata);
2360 
2361  if (res) {
2362  ast_set_hangupsource(session->channel, ast_channel_name(session->channel), 0);
2363  ast_queue_hangup(session->channel);
2364  } else {
2367  ast_sip_session_send_request(session, tdata);
2368  }
2369  ao2_ref(channel, -1);
2370  SCOPE_EXIT_RTN_VALUE(res, "RC: %d\n", res);
2371 }
2372 
2373 /*! \brief Function called by core to actually start calling a remote party */
2374 static int chan_pjsip_call(struct ast_channel *ast, const char *dest, int timeout)
2375 {
2377  SCOPE_ENTER(1, "%s Topology: %s\n", ast_sip_session_get_name(channel->session),
2379 
2380  ao2_ref(channel, +1);
2381  if (ast_sip_push_task(channel->session->serializer, call, channel)) {
2382  ast_log(LOG_WARNING, "Error attempting to place outbound call to '%s'\n", dest);
2383  ao2_cleanup(channel);
2384  SCOPE_EXIT_RTN_VALUE(-1, "Couldn't push task\n");
2385  }
2386 
2387  SCOPE_EXIT_RTN_VALUE(0, "'call' task pushed\n");
2388 }
2389 
2390 /*! \brief Internal function which translates from Asterisk cause codes to SIP response codes */
2391 static int hangup_cause2sip(int cause)
2392 {
2393  switch (cause) {
2394  case AST_CAUSE_UNALLOCATED: /* 1 */
2395  case AST_CAUSE_NO_ROUTE_DESTINATION: /* 3 IAX2: Can't find extension in context */
2396  case AST_CAUSE_NO_ROUTE_TRANSIT_NET: /* 2 */
2397  return 404;
2398  case AST_CAUSE_CONGESTION: /* 34 */
2399  case AST_CAUSE_SWITCH_CONGESTION: /* 42 */
2400  return 503;
2401  case AST_CAUSE_NO_USER_RESPONSE: /* 18 */
2402  return 408;
2403  case AST_CAUSE_NO_ANSWER: /* 19 */
2404  case AST_CAUSE_UNREGISTERED: /* 20 */
2405  return 480;
2406  case AST_CAUSE_CALL_REJECTED: /* 21 */
2407  return 403;
2408  case AST_CAUSE_NUMBER_CHANGED: /* 22 */
2409  return 410;
2410  case AST_CAUSE_NORMAL_UNSPECIFIED: /* 31 */
2411  return 480;
2413  return 484;
2414  case AST_CAUSE_USER_BUSY:
2415  return 486;
2416  case AST_CAUSE_FAILURE:
2417  return 500;
2418  case AST_CAUSE_FACILITY_REJECTED: /* 29 */
2419  return 501;
2421  return 503;
2423  return 502;
2424  case AST_CAUSE_BEARERCAPABILITY_NOTAVAIL: /* Can't find codec to connect to host */
2425  return 488;
2426  case AST_CAUSE_INTERWORKING: /* Unspecified Interworking issues */
2427  return 500;
2428  case AST_CAUSE_NOTDEFINED:
2429  default:
2430  ast_debug(1, "AST hangup cause %d (no match found in PJSIP)\n", cause);
2431  return 0;
2432  }
2433 
2434  /* Never reached */
2435  return 0;
2436 }
2437 
2438 struct hangup_data {
2439  int cause;
2441 };
2442 
2443 static void hangup_data_destroy(void *obj)
2444 {
2445  struct hangup_data *h_data = obj;
2446 
2447  h_data->chan = ast_channel_unref(h_data->chan);
2448 }
2449 
2451 {
2452  struct hangup_data *h_data = ao2_alloc(sizeof(*h_data), hangup_data_destroy);
2453 
2454  if (!h_data) {
2455  return NULL;
2456  }
2457 
2458  h_data->cause = cause;
2459  h_data->chan = ast_channel_ref(chan);
2460 
2461  return h_data;
2462 }
2463 
2464 /*! \brief Clear a channel from a session along with its PVT */
2465 static void clear_session_and_channel(struct ast_sip_session *session, struct ast_channel *ast)
2466 {
2467  session->channel = NULL;
2468  set_channel_on_rtp_instance(session, "");
2470 }
2471 
2472 static int hangup(void *data)
2473 {
2474  struct hangup_data *h_data = data;
2475  struct ast_channel *ast = h_data->chan;
2477  SCOPE_ENTER(1, "%s\n", ast_channel_name(ast));
2478 
2479  /*
2480  * Before cleaning we have to ensure that channel or its session is not NULL
2481  * we have seen rare case when taskprocessor calls hangup but channel is NULL
2482  * due to SIP session timeout and answer happening at the same time
2483  */
2484  if (channel) {
2485  struct ast_sip_session *session = channel->session;
2486  if (session) {
2487  int cause = h_data->cause;
2488 
2489  /*
2490  * It's possible that session_terminate might cause the session to be destroyed
2491  * immediately so we need to keep a reference to it so we can NULL session->channel
2492  * afterwards.
2493  */
2494  ast_sip_session_terminate(ao2_bump(session), cause);
2495  clear_session_and_channel(session, ast);
2496  ao2_cleanup(session);
2497  }
2498  ao2_cleanup(channel);
2499  }
2500  ao2_cleanup(h_data);
2502 }
2503 
2504 /*! \brief Function called by core to hang up a PJSIP session */
2505 static int chan_pjsip_hangup(struct ast_channel *ast)
2506 {
2508  int cause;
2509  struct hangup_data *h_data;
2510  SCOPE_ENTER(1, "%s\n", ast_channel_name(ast));
2511 
2512  if (!channel || !channel->session) {
2513  SCOPE_EXIT_RTN_VALUE(-1, "No channel or session\n");
2514  }
2515 
2517  h_data = hangup_data_alloc(cause, ast);
2518 
2519  if (!h_data) {
2520  goto failure;
2521  }
2522 
2523  if (ast_sip_push_task(channel->session->serializer, hangup, h_data)) {
2524  ast_log(LOG_WARNING, "Unable to push hangup task to the threadpool. Expect bad things\n");
2525  goto failure;
2526  }
2527 
2528  SCOPE_EXIT_RTN_VALUE(0, "Cause: %d\n", cause);
2529 
2530 failure:
2531  /* Go ahead and do our cleanup of the session and channel even if we're not going
2532  * to be able to send our SIP request/response
2533  */
2534  clear_session_and_channel(channel->session, ast);
2535  ao2_cleanup(channel);
2536  ao2_cleanup(h_data);
2537 
2538  SCOPE_EXIT_RTN_VALUE(-1, "Cause: %d\n", cause);
2539 }
2540 
2544  const char *dest;
2545  int cause;
2546 };
2547 
2548 static int request(void *obj)
2549 {
2550  struct request_data *req_data = obj;
2551  struct ast_sip_session *session = NULL;
2552  char *tmp = ast_strdupa(req_data->dest), *endpoint_name = NULL, *request_user = NULL;
2553  struct ast_sip_endpoint *endpoint;
2554 
2556  AST_APP_ARG(endpoint);
2557  AST_APP_ARG(aor);
2558  );
2559  SCOPE_ENTER(1, "%s\n",tmp);
2560 
2561  if (ast_strlen_zero(tmp)) {
2562  ast_log(LOG_ERROR, "Unable to create PJSIP channel with empty destination\n");
2564  SCOPE_EXIT_RTN_VALUE(-1, "Empty destination\n");
2565  }
2566 
2567  AST_NONSTANDARD_APP_ARGS(args, tmp, '/');
2568 
2570  /* If a request user has been specified extract it from the endpoint name portion */
2571  if ((endpoint_name = strchr(args.endpoint, '@'))) {
2572  request_user = args.endpoint;
2573  *endpoint_name++ = '\0';
2574  } else {
2575  endpoint_name = args.endpoint;
2576  }
2577 
2578  if (ast_strlen_zero(endpoint_name)) {
2579  if (request_user) {
2580  ast_log(LOG_ERROR, "Unable to create PJSIP channel with empty endpoint name: %s@<endpoint-name>\n",
2581  request_user);
2582  } else {
2583  ast_log(LOG_ERROR, "Unable to create PJSIP channel with empty endpoint name\n");
2584  }
2586  SCOPE_EXIT_RTN_VALUE(-1, "Empty endpoint name\n");
2587  }
2588  endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint",
2589  endpoint_name);
2590  if (!endpoint) {
2591  ast_log(LOG_ERROR, "Unable to create PJSIP channel - endpoint '%s' was not found\n", endpoint_name);
2593  SCOPE_EXIT_RTN_VALUE(-1, "Endpoint not found\n");
2594  }
2595  } else {
2596  /* First try to find an exact endpoint match, for single (user) or multi-domain (user@domain) */
2597  endpoint_name = args.endpoint;
2598  if (ast_strlen_zero(endpoint_name)) {
2599  ast_log(LOG_ERROR, "Unable to create PJSIP channel with empty endpoint name\n");
2601  SCOPE_EXIT_RTN_VALUE(-1, "Empty endpoint name\n");
2602  }
2603  endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint",
2604  endpoint_name);
2605  if (!endpoint) {
2606  /* It seems it's not a multi-domain endpoint or single endpoint exact match,
2607  * it's possible that it's a SIP trunk with a specified user (user@trunkname),
2608  * so extract the user before @ sign.
2609  */
2610  endpoint_name = strchr(args.endpoint, '@');
2611  if (!endpoint_name) {
2612  /*
2613  * Couldn't find an '@' so it had to be an endpoint
2614  * name that doesn't exist.
2615  */
2616  ast_log(LOG_ERROR, "Unable to create PJSIP channel - endpoint '%s' was not found\n",
2617  args.endpoint);
2619  SCOPE_EXIT_RTN_VALUE(-1, "Endpoint not found\n");
2620  }
2621  request_user = args.endpoint;
2622  *endpoint_name++ = '\0';
2623 
2624  if (ast_strlen_zero(endpoint_name)) {
2625  ast_log(LOG_ERROR, "Unable to create PJSIP channel with empty endpoint name: %s@<endpoint-name>\n",
2626  request_user);
2628  SCOPE_EXIT_RTN_VALUE(-1, "Empty endpoint name\n");
2629  }
2630 
2631  endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint",
2632  endpoint_name);
2633  if (!endpoint) {
2634  ast_log(LOG_ERROR, "Unable to create PJSIP channel - endpoint '%s' was not found\n", endpoint_name);
2636  SCOPE_EXIT_RTN_VALUE(-1, "Endpoint not found\n");
2637  }
2638  }
2639  }
2640 
2641  session = ast_sip_session_create_outgoing(endpoint, NULL, args.aor, request_user,
2642  req_data->topology);
2643  ao2_ref(endpoint, -1);
2644  if (!session) {
2645  ast_log(LOG_ERROR, "Failed to create outgoing session to endpoint '%s'\n", endpoint_name);
2647  SCOPE_EXIT_RTN_VALUE(-1, "Couldn't create session\n");
2648  }
2649 
2650  req_data->session = session;
2651 
2653 }
2654 
2655 /*! \brief Function called by core to create a new outgoing PJSIP session */
2656 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)
2657 {
2658  struct request_data req_data;
2659  RAII_VAR(struct ast_sip_session *, session, NULL, ao2_cleanup);
2660  SCOPE_ENTER(1, "%s Topology: %s\n", data,
2661  ast_str_tmp(256, ast_stream_topology_to_str(topology, &STR_TMP)));
2662 
2663  req_data.topology = topology;
2664  req_data.dest = data;
2665  /* Default failure value in case ast_sip_push_task_wait_servant() itself fails. */
2666  req_data.cause = AST_CAUSE_FAILURE;
2667 
2668  if (ast_sip_push_task_wait_servant(NULL, request, &req_data)) {
2669  *cause = req_data.cause;
2670  SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't push task\n");
2671  }
2672 
2673  session = req_data.session;
2674 
2675  if (!(session->channel = chan_pjsip_new(session, AST_STATE_DOWN, NULL, NULL, assignedids, requestor, NULL))) {
2676  /* Session needs to be terminated prematurely */
2677  SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't create channel\n");
2678  }
2679 
2680  SCOPE_EXIT_RTN_VALUE(session->channel, "Channel: %s\n", ast_channel_name(session->channel));
2681 }
2682 
2683 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)
2684 {
2685  struct ast_stream_topology *topology;
2686  struct ast_channel *chan;
2687 
2689  if (!topology) {
2690  return NULL;
2691  }
2692 
2693  chan = chan_pjsip_request_with_stream_topology(type, topology, assignedids, requestor, data, cause);
2694 
2695  ast_stream_topology_free(topology);
2696 
2697  return chan;
2698 }
2699 
2703 };
2704 
2705 static void sendtext_data_destroy(void *obj)
2706 {
2707  struct sendtext_data *data = obj;
2708  ao2_cleanup(data->session);
2709  ast_free(data->msg);
2710 }
2711 
2713  struct ast_msg_data *msg)
2714 {
2716  struct sendtext_data *data = ao2_alloc(sizeof(*data), sendtext_data_destroy);
2717 
2718  if (!data) {
2719  return NULL;
2720  }
2721 
2722  data->msg = ast_msg_data_dup(msg);
2723  if (!data->msg) {
2724  ao2_cleanup(data);
2725  return NULL;
2726  }
2727  data->session = channel->session;
2728  ao2_ref(data->session, +1);
2729 
2730  return data;
2731 }
2732 
2733 static int sendtext(void *obj)
2734 {
2735  struct sendtext_data *data = obj;
2736  pjsip_tx_data *tdata;
2737  const char *body_text = ast_msg_data_get_attribute(data->msg, AST_MSG_DATA_ATTR_BODY);
2738  const char *content_type = ast_msg_data_get_attribute(data->msg, AST_MSG_DATA_ATTR_CONTENT_TYPE);
2739  char *sep;
2740  struct ast_sip_body body = {
2741  .type = "text",
2742  .subtype = "plain",
2743  .body_text = body_text,
2744  };
2745 
2746  if (!ast_strlen_zero(content_type)) {
2747  sep = strchr(content_type, '/');
2748  if (sep) {
2749  *sep = '\0';
2750  body.type = content_type;
2751  body.subtype = ++sep;
2752  }
2753  }
2754 
2755  if (data->session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
2756  ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
2757  data->session->inv_session->cause,
2758  pjsip_get_status_text(data->session->inv_session->cause)->ptr);
2759  } else {
2760  pjsip_from_hdr *hdr;
2761  pjsip_name_addr *name_addr;
2762  const char *from = ast_msg_data_get_attribute(data->msg, AST_MSG_DATA_ATTR_FROM);
2763  const char *to = ast_msg_data_get_attribute(data->msg, AST_MSG_DATA_ATTR_TO);
2764  int invalidate_tdata = 0;
2765 
2766  ast_sip_create_request("MESSAGE", data->session->inv_session->dlg, data->session->endpoint, NULL, NULL, &tdata);
2767  ast_sip_add_body(tdata, &body);
2768 
2769  /*
2770  * If we have a 'from' in the msg, set the display name in the From
2771  * header to it.
2772  */
2773  if (!ast_strlen_zero(from)) {
2774  hdr = PJSIP_MSG_FROM_HDR(tdata->msg);
2775  name_addr = (pjsip_name_addr *) hdr->uri;
2776  pj_strdup2(tdata->pool, &name_addr->display, from);
2777  invalidate_tdata = 1;
2778  }
2779 
2780  /*
2781  * If we have a 'to' in the msg, set the display name in the To
2782  * header to it.
2783  */
2784  if (!ast_strlen_zero(to)) {
2785  hdr = PJSIP_MSG_TO_HDR(tdata->msg);
2786  name_addr = (pjsip_name_addr *) hdr->uri;
2787  pj_strdup2(tdata->pool, &name_addr->display, to);
2788  invalidate_tdata = 1;
2789  }
2790 
2791  if (invalidate_tdata) {
2792  pjsip_tx_data_invalidate_msg(tdata);
2793  }
2794 
2795  ast_sip_send_request(tdata, data->session->inv_session->dlg, data->session->endpoint, NULL, NULL);
2796  }
2797 
2798  ao2_cleanup(data);
2799 
2800  return 0;
2801 }
2802 
2803 /*! \brief Function called by core to send text on PJSIP session */
2804 static int chan_pjsip_sendtext_data(struct ast_channel *ast, struct ast_msg_data *msg)
2805 {
2807  struct sendtext_data *data = sendtext_data_create(ast, msg);
2808 
2809  ast_debug(1, "Sending MESSAGE from '%s' to '%s:%s': %s\n",
2812  ast_channel_name(ast),
2814 
2815  if (!data) {
2816  return -1;
2817  }
2818 
2819  if (ast_sip_push_task(channel->session->serializer, sendtext, data)) {
2820  ao2_ref(data, -1);
2821  return -1;
2822  }
2823  return 0;
2824 }
2825 
2826 static int chan_pjsip_sendtext(struct ast_channel *ast, const char *text)
2827 {
2828  struct ast_msg_data *msg;
2829  int rc;
2830  struct ast_msg_data_attribute attrs[] =
2831  {
2832  {
2834  .value = (char *)text,
2835  }
2836  };
2837 
2839  if (!msg) {
2840  return -1;
2841  }
2842  rc = chan_pjsip_sendtext_data(ast, msg);
2843  ast_free(msg);
2844 
2845  return rc;
2846 }
2847 
2848 /*! \brief Convert SIP hangup causes to Asterisk hangup causes */
2849 static int hangup_sip2cause(int cause)
2850 {
2851  /* Possible values taken from causes.h */
2852 
2853  switch(cause) {
2854  case 401: /* Unauthorized */
2855  return AST_CAUSE_CALL_REJECTED;
2856  case 403: /* Not found */
2857  return AST_CAUSE_CALL_REJECTED;
2858  case 404: /* Not found */
2859  return AST_CAUSE_UNALLOCATED;
2860  case 405: /* Method not allowed */
2861  return AST_CAUSE_INTERWORKING;
2862  case 407: /* Proxy authentication required */
2863  return AST_CAUSE_CALL_REJECTED;
2864  case 408: /* No reaction */
2866  case 409: /* Conflict */
2868  case 410: /* Gone */
2869  return AST_CAUSE_NUMBER_CHANGED;
2870  case 411: /* Length required */
2871  return AST_CAUSE_INTERWORKING;
2872  case 413: /* Request entity too large */
2873  return AST_CAUSE_INTERWORKING;
2874  case 414: /* Request URI too large */
2875  return AST_CAUSE_INTERWORKING;
2876  case 415: /* Unsupported media type */
2877  return AST_CAUSE_INTERWORKING;
2878  case 420: /* Bad extension */
2880  case 480: /* No answer */
2881  return AST_CAUSE_NO_ANSWER;
2882  case 481: /* No answer */
2883  return AST_CAUSE_INTERWORKING;
2884  case 482: /* Loop detected */
2885  return AST_CAUSE_INTERWORKING;
2886  case 483: /* Too many hops */
2887  return AST_CAUSE_NO_ANSWER;
2888  case 484: /* Address incomplete */
2890  case 485: /* Ambiguous */
2891  return AST_CAUSE_UNALLOCATED;
2892  case 486: /* Busy everywhere */
2893  return AST_CAUSE_BUSY;
2894  case 487: /* Request terminated */
2895  return AST_CAUSE_INTERWORKING;
2896  case 488: /* No codecs approved */
2898  case 491: /* Request pending */
2899  return AST_CAUSE_INTERWORKING;
2900  case 493: /* Undecipherable */
2901  return AST_CAUSE_INTERWORKING;
2902  case 500: /* Server internal failure */
2903  return AST_CAUSE_FAILURE;
2904  case 501: /* Call rejected */
2906  case 502:
2908  case 503: /* Service unavailable */
2909  return AST_CAUSE_CONGESTION;
2910  case 504: /* Gateway timeout */
2912  case 505: /* SIP version not supported */
2913  return AST_CAUSE_INTERWORKING;
2914  case 600: /* Busy everywhere */
2915  return AST_CAUSE_USER_BUSY;
2916  case 603: /* Decline */
2917  return AST_CAUSE_CALL_REJECTED;
2918  case 604: /* Does not exist anywhere */
2919  return AST_CAUSE_UNALLOCATED;
2920  case 606: /* Not acceptable */
2922  default:
2923  if (cause < 500 && cause >= 400) {
2924  /* 4xx class error that is unknown - someting wrong with our request */
2925  return AST_CAUSE_INTERWORKING;
2926  } else if (cause < 600 && cause >= 500) {
2927  /* 5xx class error - problem in the remote end */
2928  return AST_CAUSE_CONGESTION;
2929  } else if (cause < 700 && cause >= 600) {
2930  /* 6xx - global errors in the 4xx class */
2931  return AST_CAUSE_INTERWORKING;
2932  }
2933  return AST_CAUSE_NORMAL;
2934  }
2935  /* Never reached */
2936  return 0;
2937 }
2938 
2939 static void chan_pjsip_session_begin(struct ast_sip_session *session)
2940 {
2941  RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
2942  SCOPE_ENTER(1, "%s\n", ast_sip_session_get_name(session));
2943 
2944  if (session->endpoint->media.direct_media.glare_mitigation ==
2946  SCOPE_EXIT_RTN("Direct media no glare mitigation\n");
2947  }
2948 
2949  datastore = ast_sip_session_alloc_datastore(&direct_media_mitigation_info,
2950  "direct_media_glare_mitigation");
2951 
2952  if (!datastore) {
2953  SCOPE_EXIT_RTN("Couldn't create datastore\n");
2954  }
2955 
2956  ast_sip_session_add_datastore(session, datastore);
2957  SCOPE_EXIT_RTN();
2958 }
2959 
2960 /*! \brief Function called when the session ends */
2961 static void chan_pjsip_session_end(struct ast_sip_session *session)
2962 {
2963  SCOPE_ENTER(1, "%s\n", ast_sip_session_get_name(session));
2964 
2965  if (!session->channel) {
2966  SCOPE_EXIT_RTN("No channel\n");
2967  }
2968 
2970 
2971  ast_set_hangupsource(session->channel, ast_channel_name(session->channel), 0);
2972  if (!ast_channel_hangupcause(session->channel) && session->inv_session) {
2973  int cause = hangup_sip2cause(session->inv_session->cause);
2974 
2975  ast_queue_hangup_with_cause(session->channel, cause);
2976  } else {
2977  ast_queue_hangup(session->channel);
2978  }
2979 
2980  SCOPE_EXIT_RTN();
2981 }
2982 
2983 static void set_sipdomain_variable(struct ast_sip_session *session)
2984 {
2985  pjsip_sip_uri *sip_ruri = pjsip_uri_get_uri(session->request_uri);
2986  size_t size = pj_strlen(&sip_ruri->host) + 1;
2987  char *domain = ast_alloca(size);
2988 
2989  ast_copy_pj_str(domain, &sip_ruri->host, size);
2990 
2991  pbx_builtin_setvar_helper(session->channel, "SIPDOMAIN", domain);
2992  return;
2993 }
2994 
2995 /*! \brief Function called when a request is received on the session */
2996 static int chan_pjsip_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
2997 {
2998  RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
2999  struct transport_info_data *transport_data;
3000  pjsip_tx_data *packet = NULL;
3001  SCOPE_ENTER(3, "%s\n", ast_sip_session_get_name(session));
3002 
3003  if (session->channel) {
3004  SCOPE_EXIT_RTN_VALUE(0, "%s: No channel\n", ast_sip_session_get_name(session));
3005  }
3006 
3007  /* Check for a to-tag to determine if this is a reinvite */
3008  if (rdata->msg_info.to->tag.slen) {
3009  /* Weird case. We've received a reinvite but we don't have a channel. The most
3010  * typical case for this happening is that a blind transfer fails, and so the
3011  * transferer attempts to reinvite himself back into the call. We already got
3012  * rid of that channel, and the other side of the call is unrecoverable.
3013  *
3014  * We treat this as a failure, so our best bet is to just hang this call
3015  * up and not create a new channel. Clearing defer_terminate here ensures that
3016  * calling ast_sip_session_terminate() can result in a BYE being sent ASAP.
3017  */
3018  session->defer_terminate = 0;
3019  ast_sip_session_terminate(session, 400);
3020  SCOPE_EXIT_RTN_VALUE(-1, "%s: We have a To tag but no channel. Terminating session\n", ast_sip_session_get_name(session));
3021  }
3022 
3023  datastore = ast_sip_session_alloc_datastore(&transport_info, "transport_info");
3024  if (!datastore) {
3025  SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR, "%s: Couldn't alloc transport_info datastore\n", ast_sip_session_get_name(session));
3026  }
3027 
3028  transport_data = ast_calloc(1, sizeof(*transport_data));
3029  if (!transport_data) {
3030  SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR, "%s: Couldn't alloc transport_info\n", ast_sip_session_get_name(session));
3031  }
3032  pj_sockaddr_cp(&transport_data->local_addr, &rdata->tp_info.transport->local_addr);
3033  pj_sockaddr_cp(&transport_data->remote_addr, &rdata->pkt_info.src_addr);
3034  datastore->data = transport_data;
3035  ast_sip_session_add_datastore(session, datastore);
3036 
3037  if (!(session->channel = chan_pjsip_new(session, AST_STATE_RING, session->exten, NULL, NULL, NULL, NULL))) {
3038  if (pjsip_inv_end_session(session->inv_session, 503, NULL, &packet) == PJ_SUCCESS
3039  && packet) {
3040  ast_sip_session_send_response(session, packet);
3041  }
3042 
3043  SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR, "%s: Failed to allocate new PJSIP channel on incoming SIP INVITE\n",
3044  ast_sip_session_get_name(session));
3045  }
3046 
3047  set_sipdomain_variable(session);
3048 
3049  /* channel gets created on incoming request, but we wait to call start
3050  so other supplements have a chance to run */
3051  SCOPE_EXIT_RTN_VALUE(0, "%s\n", ast_sip_session_get_name(session));
3052 }
3053 
3054 static int call_pickup_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
3055 {
3056  struct ast_features_pickup_config *pickup_cfg;
3057  struct ast_channel *chan;
3058 
3059  /* Check for a to-tag to determine if this is a reinvite */
3060  if (rdata->msg_info.to->tag.slen) {
3061  /* We don't care about reinvites */
3062  return 0;
3063  }
3064 
3065  pickup_cfg = ast_get_chan_features_pickup_config(session->channel);
3066  if (!pickup_cfg) {
3067  ast_log(LOG_ERROR, "Unable to retrieve pickup configuration options. Unable to detect call pickup extension.\n");
3068  return 0;
3069  }
3070 
3071  if (strcmp(session->exten, pickup_cfg->pickupexten)) {
3072  ao2_ref(pickup_cfg, -1);
3073  return 0;
3074  }
3075  ao2_ref(pickup_cfg, -1);
3076 
3077  /* We can't directly use session->channel because the pickup operation will cause a masquerade to occur,
3078  * changing the channel pointer in session to a different channel. To ensure we work on the right channel
3079  * we store a pointer locally before we begin and keep a reference so it remains valid no matter what.
3080  */
3081  chan = ast_channel_ref(session->channel);
3082  if (ast_pickup_call(chan)) {
3084  } else {
3086  }
3087  /* A hangup always occurs because the pickup operation will have either failed resulting in the call
3088  * needing to be hung up OR the pickup operation was a success and the channel we now have is actually
3089  * the channel that was replaced, which should be hung up since it is literally in limbo not connected
3090  * to anything at all.
3091  */
3092  ast_hangup(chan);
3093  ast_channel_unref(chan);
3094 
3095  return 1;
3096 }
3097 
3099  .method = "INVITE",
3100  .priority = AST_SIP_SUPPLEMENT_PRIORITY_LAST - 1,
3101  .incoming_request = call_pickup_incoming_request,
3102 };
3103 
3104 static int pbx_start_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
3105 {
3106  int res;
3107  SCOPE_ENTER(1, "%s\n", ast_sip_session_get_name(session));
3108 
3109  /* Check for a to-tag to determine if this is a reinvite */
3110  if (rdata->msg_info.to->tag.slen) {
3111  /* We don't care about reinvites */
3112  SCOPE_EXIT_RTN_VALUE(0, "Reinvite\n");
3113  }
3114 
3115  res = ast_pbx_start(session->channel);
3116 
3117  switch (res) {
3118  case AST_PBX_FAILED:
3119  ast_log(LOG_WARNING, "Failed to start PBX ;(\n");
3121  ast_hangup(session->channel);
3122  break;
3123  case AST_PBX_CALL_LIMIT:
3124  ast_log(LOG_WARNING, "Failed to start PBX (call limit reached) \n");
3126  ast_hangup(session->channel);
3127  break;
3128  case AST_PBX_SUCCESS:
3129  default:
3130  break;
3131  }
3132 
3133  ast_debug(3, "Started PBX on new PJSIP channel %s\n", ast_channel_name(session->channel));
3134 
3135  SCOPE_EXIT_RTN_VALUE((res == AST_PBX_SUCCESS) ? 0 : -1, "RC: %d\n", res);
3136 }
3137 
3139  .method = "INVITE",
3141  .incoming_request = pbx_start_incoming_request,
3142 };
3143 
3144 /*! \brief Function called when a response is received on the session */
3145 static void chan_pjsip_incoming_response_update_cause(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
3146 {
3147  struct pjsip_status_line status = rdata->msg_info.msg->line.status;
3148  struct ast_control_pvt_cause_code *cause_code;
3149  int data_size = sizeof(*cause_code);
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  /* Build and send the tech-specific cause information */
3157  /* size of the string making up the cause code is "SIP " number + " " + reason length */
3158  data_size += 4 + 4 + pj_strlen(&status.reason);
3159  cause_code = ast_alloca(data_size);
3160  memset(cause_code, 0, data_size);
3161 
3163 
3164  snprintf(cause_code->code, data_size - sizeof(*cause_code) + 1, "SIP %d %.*s", status.code,
3165  (int) pj_strlen(&status.reason), pj_strbuf(&status.reason));
3166 
3167  cause_code->ast_cause = hangup_sip2cause(status.code);
3168  ast_queue_control_data(session->channel, AST_CONTROL_PVT_CAUSE_CODE, cause_code, data_size);
3169  ast_channel_hangupcause_hash_set(session->channel, cause_code, data_size);
3170 
3171  SCOPE_EXIT_RTN("%s\n", ast_sip_session_get_name(session));
3172 }
3173 
3174 /*! \brief Function called when a response is received on the session */
3175 static void chan_pjsip_incoming_response(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
3176 {
3177  struct pjsip_status_line status = rdata->msg_info.msg->line.status;
3178  SCOPE_ENTER(3, "%s: Status: %d\n", ast_sip_session_get_name(session), status.code);
3179 
3180  if (!session->channel) {
3181  SCOPE_EXIT_RTN("%s: No channel\n", ast_sip_session_get_name(session));
3182  }
3183 
3184  switch (status.code) {
3185  case 180:
3186  ast_trace(-1, "%s: Queueing RINGING\n", ast_sip_session_get_name(session));
3188  ast_channel_lock(session->channel);
3189  if (ast_channel_state(session->channel) != AST_STATE_UP) {
3191  }
3192  ast_channel_unlock(session->channel);
3193  break;
3194  case 183:
3195  ast_trace(-1, "%s: Queueing PROGRESS\n", ast_sip_session_get_name(session));
3196  if (session->endpoint->ignore_183_without_sdp) {
3197  pjsip_rdata_sdp_info *sdp = pjsip_rdata_get_sdp_info(rdata);
3198  if (sdp && sdp->body.ptr) {
3199  ast_trace(1, "%s Method: %.*s Status: %d Queueing PROGRESS with SDP\n", ast_sip_session_get_name(session),
3200  (int)rdata->msg_info.cseq->method.name.slen, rdata->msg_info.cseq->method.name.ptr, status.code);
3202  }
3203  } else {
3204  ast_trace(1, "%s Method: %.*s Status: %d Queueing PROGRESS without SDP\n", ast_sip_session_get_name(session),
3205  (int)rdata->msg_info.cseq->method.name.slen, rdata->msg_info.cseq->method.name.ptr, status.code);
3207  }
3208  break;
3209  case 200:
3210  ast_trace(-1, "%s: Queueing ANSWER\n", ast_sip_session_get_name(session));
3212  break;
3213  default:
3214  ast_trace(-1, "%s: Not queueing anything\n", ast_sip_session_get_name(session));
3215  break;
3216  }
3217 
3218  SCOPE_EXIT_RTN("%s\n", ast_sip_session_get_name(session));
3219 }
3220 
3221 static int chan_pjsip_incoming_ack(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
3222 {
3223  SCOPE_ENTER(3, "%s\n", ast_sip_session_get_name(session));
3224 
3225  if (rdata->msg_info.msg->line.req.method.id == PJSIP_ACK_METHOD) {
3226  if (session->endpoint->media.direct_media.enabled && session->channel) {
3227  ast_trace(-1, "%s: Queueing SRCCHANGE\n", ast_sip_session_get_name(session));
3229  }
3230  }
3231  SCOPE_EXIT_RTN_VALUE(0, "%s\n", ast_sip_session_get_name(session));
3232 }
3233 
3234 static int update_devstate(void *obj, void *arg, int flags)
3235 {
3237  "PJSIP/%s", ast_sorcery_object_get_id(obj));
3238  return 0;
3239 }
3240 
3242  .name = "PJSIP_DIAL_CONTACTS",
3244 };
3245 
3247  .name = "PJSIP_PARSE_URI",
3248  .read = pjsip_acf_parse_uri_read,
3249 };
3250 
3252  .name = "PJSIP_MEDIA_OFFER",
3255 };
3256 
3258  .name = "PJSIP_DTMF_MODE",
3259  .read = pjsip_acf_dtmf_mode_read,
3260  .write = pjsip_acf_dtmf_mode_write
3261 };
3262 
3264  .name = "PJSIP_MOH_PASSTHROUGH",
3267 };
3268 
3270  .name = "PJSIP_SEND_SESSION_REFRESH",
3272 };
3273 
3274 /*!
3275  * \brief Load the module
3276  *
3277  * Module loading including tests for configuration or dependencies.
3278  * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
3279  * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
3280  * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
3281  * configuration file or other non-critical problem return
3282  * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
3283  */
3284 static int load_module(void)
3285 {
3286  struct ao2_container *endpoints;
3287 
3289  return AST_MODULE_LOAD_DECLINE;
3290  }
3291 
3293 
3294  ast_rtp_glue_register(&chan_pjsip_rtp_glue);
3295 
3296  if (ast_channel_register(&chan_pjsip_tech)) {
3297  ast_log(LOG_ERROR, "Unable to register channel class %s\n", channel_type);
3298  goto end;
3299  }
3300 
3301  if (ast_custom_function_register(&chan_pjsip_dial_contacts_function)) {
3302  ast_log(LOG_ERROR, "Unable to register PJSIP_DIAL_CONTACTS dialplan function\n");
3303  goto end;
3304  }
3305 
3306  if (ast_custom_function_register(&chan_pjsip_parse_uri_function)) {
3307  ast_log(LOG_ERROR, "Unable to register PJSIP_PARSE_URI dialplan function\n");
3308  goto end;
3309  }
3310 
3311  if (ast_custom_function_register(&media_offer_function)) {
3312  ast_log(LOG_WARNING, "Unable to register PJSIP_MEDIA_OFFER dialplan function\n");
3313  goto end;
3314  }
3315 
3316  if (ast_custom_function_register(&dtmf_mode_function)) {
3317  ast_log(LOG_WARNING, "Unable to register PJSIP_DTMF_MODE dialplan function\n");
3318  goto end;
3319  }
3320 
3321  if (ast_custom_function_register(&moh_passthrough_function)) {
3322  ast_log(LOG_WARNING, "Unable to register PJSIP_MOH_PASSTHROUGH dialplan function\n");
3323  goto end;
3324  }
3325 
3326  if (ast_custom_function_register(&session_refresh_function)) {
3327  ast_log(LOG_WARNING, "Unable to register PJSIP_SEND_SESSION_REFRESH dialplan function\n");
3328  goto end;
3329  }
3330 
3332 
3333  ast_sip_session_register_supplement(&chan_pjsip_supplement);
3334  ast_sip_session_register_supplement(&chan_pjsip_supplement_response);
3335 
3336  if (!(pjsip_uids_onhold = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_RWLOCK,
3338  uid_hold_sort_fn, NULL))) {
3339  ast_log(LOG_ERROR, "Unable to create held channels container\n");
3340  goto end;
3341  }
3342 
3343  ast_sip_session_register_supplement(&call_pickup_supplement);
3344  ast_sip_session_register_supplement(&pbx_start_supplement);
3345  ast_sip_session_register_supplement(&chan_pjsip_ack_supplement);
3346 
3348  ast_log(LOG_ERROR, "Unable to register PJSIP Channel CLI\n");
3349  goto end;
3350  }
3351 
3352  /* since endpoints are loaded before the channel driver their device
3353  states get set to 'invalid', so they need to be updated */
3354  if ((endpoints = ast_sip_get_endpoints())) {
3356  ao2_ref(endpoints, -1);
3357  }
3358 
3359  return 0;
3360 
3361 end:
3362  ao2_cleanup(pjsip_uids_onhold);
3363  pjsip_uids_onhold = NULL;
3364  ast_sip_session_unregister_supplement(&chan_pjsip_ack_supplement);
3365  ast_sip_session_unregister_supplement(&pbx_start_supplement);
3366  ast_sip_session_unregister_supplement(&chan_pjsip_supplement_response);
3367  ast_sip_session_unregister_supplement(&chan_pjsip_supplement);
3368  ast_sip_session_unregister_supplement(&call_pickup_supplement);
3370  ast_custom_function_unregister(&dtmf_mode_function);
3371  ast_custom_function_unregister(&moh_passthrough_function);
3372  ast_custom_function_unregister(&media_offer_function);
3373  ast_custom_function_unregister(&chan_pjsip_dial_contacts_function);
3374  ast_custom_function_unregister(&chan_pjsip_parse_uri_function);
3375  ast_custom_function_unregister(&session_refresh_function);
3376  ast_channel_unregister(&chan_pjsip_tech);
3377  ast_rtp_glue_unregister(&chan_pjsip_rtp_glue);
3378 
3379  return AST_MODULE_LOAD_DECLINE;
3380 }
3381 
3382 /*! \brief Unload the PJSIP channel from Asterisk */
3383 static int unload_module(void)
3384 {
3385  ao2_cleanup(pjsip_uids_onhold);
3386  pjsip_uids_onhold = NULL;
3387 
3389 
3390  ast_sip_session_unregister_supplement(&chan_pjsip_supplement_response);
3391  ast_sip_session_unregister_supplement(&chan_pjsip_supplement);
3392  ast_sip_session_unregister_supplement(&pbx_start_supplement);
3393  ast_sip_session_unregister_supplement(&chan_pjsip_ack_supplement);
3394  ast_sip_session_unregister_supplement(&call_pickup_supplement);
3395 
3397 
3398  ast_custom_function_unregister(&dtmf_mode_function);
3399  ast_custom_function_unregister(&moh_passthrough_function);
3400  ast_custom_function_unregister(&media_offer_function);
3401  ast_custom_function_unregister(&chan_pjsip_dial_contacts_function);
3402  ast_custom_function_unregister(&chan_pjsip_parse_uri_function);
3403  ast_custom_function_unregister(&session_refresh_function);
3404 
3405  ast_channel_unregister(&chan_pjsip_tech);
3406  ao2_ref(chan_pjsip_tech.capabilities, -1);
3407  ast_rtp_glue_unregister(&chan_pjsip_rtp_glue);
3408 
3409  return 0;
3410 }
3411 
3413  .support_level = AST_MODULE_SUPPORT_CORE,
3414  .load = load_module,
3415  .unload = unload_module,
3416  .load_pri = AST_MODPRI_CHANNEL_DRIVER,
3417  .requires = "res_pjsip,res_pjsip_session,res_pjsip_pubsub",
3418 );
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:1500
struct ast_party_caller * ast_channel_caller(struct ast_channel *chan)
const char * type
Definition: datastore.h:32
struct ast_sip_endpoint_pickup_configuration pickup
Definition: res_pjsip.h:851
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:1490
#define AST_THREADSTORAGE(name)
Define a thread storage variable.
Definition: threadstorage.h:84
static int direct_media_mitigate_glare(struct ast_sip_session *session)
Definition: chan_pjsip.c:268
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.
int ast_queue_hangup(struct ast_channel *chan)
Queue a hangup frame.
Definition: channel.c:1146
static struct sendtext_data * sendtext_data_create(struct ast_channel *chan, struct ast_msg_data *msg)
Definition: chan_pjsip.c:2712
#define SCOPE_EXIT_LOG_RTN_VALUE(__value, __log_level,...)
Definition: logger.h:927
struct ast_sip_session * session
Definition: chan_pjsip.c:1844
static struct ast_custom_function session_refresh_function
Definition: chan_pjsip.c:3269
static const char type[]
Definition: chan_ooh323.c:109
void ast_sip_session_send_request(struct ast_sip_session *session, pjsip_tx_data *tdata)
Send a SIP request.
Information needed to identify an endpoint in a call.
Definition: channel.h:339
enum sip_cc_notify_state state
Definition: chan_sip.c:957
Tone Indication Support.
const char * body_text
Definition: res_pjsip.h:2020
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:2996
void ast_party_connected_line_init(struct ast_party_connected_line *init)
Initialize the given connected line structure.
Definition: channel.c:2008
static unsigned int chan_idx
Definition: chan_pjsip.c:80
void ast_channel_pickupgroup_set(struct ast_channel *chan, ast_group_t value)
enum ast_sip_session_t38state t38state
char digit
#define ast_channel_lock(chan)
Definition: channel.h:2890
#define T38_ENABLED
Definition: chan_ooh323.c:102
static int indicate(void *data)
Definition: chan_pjsip.c:1333
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:1484
static char exten[AST_MAX_EXTENSION]
Definition: chan_alsa.c:118
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
struct ast_sip_session_media_state * ast_sip_session_media_state_alloc(void)
Allocate a session media state structure.
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:1237
char * str
Subscriber phone number (Malloced)
Definition: channel.h:292
void ast_sip_session_media_state_free(struct ast_sip_session_media_state *media_state)
Free a session media state structure.
const char *const type
Definition: channel.h:630
struct ast_channel_snapshot_base * base
Asterisk locking-related definitions:
Asterisk main include file. File version handling, generic pbx functions.
Structure which contains media state information (streams, sessions)
static int is_colp_update_allowed(struct ast_sip_session *session)
Definition: chan_pjsip.c:1402
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
struct ast_features_pickup_config * ast_get_chan_features_pickup_config(struct ast_channel *chan)
Get the pickup configuration options for a channel.
char * str
Subscriber phone number (Malloced)
Definition: channel.h:387
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:1227
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&#39;ve negotiated.
Definition: chan_pjsip.c:824
void * ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size)
Retrieve thread storage.
static int transfer(void *data)
Definition: chan_pjsip.c:2085
static void chan_pjsip_pvt_dtor(void *obj)
Definition: chan_pjsip.c:82
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:553
struct ast_sip_session_media_state * pending_media_state
#define ast_channel_alloc_with_endpoint(needqueue, state, cid_num, cid_name, acctcode, exten, context, assignedids, requestor, amaflag, endpoint,...)
Definition: channel.h:1263
struct ast_sockaddr direct_media_addr
Direct media address.
struct ast_rtp_instance * rtp
Definition: chan_pjsip.c:360
#define SCOPE_ENTER_TASK(level, indent,...)
Definition: logger.h:889
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:2925
static struct ast_custom_function media_offer_function
Definition: chan_pjsip.c:3251
enum ast_msg_data_attribute_type type
Definition: message.h:463
static int transmit_info_with_vidupdate(void *data)
Send SIP INFO with video update request.
Definition: chan_pjsip.c:1351
CallerID (and other GR30) management and generation Includes code and algorithms from the Zapata libr...
void ast_channel_set_writeformat(struct ast_channel *chan, struct ast_format *format)
struct ast_msg_data * ast_msg_data_dup(struct ast_msg_data *msg)
Clone an ast_msg_data structure.
Definition: message.c:1458
void ast_sip_session_unsuspend(struct ast_sip_session *session)
Request the session serializer be unsuspended.
static int remote_send_unhold(void *data)
Update local hold state to be unheld.
Definition: chan_pjsip.c:1506
struct ast_party_id id
Connected party ID.
Definition: channel.h:459
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
void * ast_channel_tech_pvt(const struct ast_channel *chan)
#define AST_CAUSE_NORMAL_TEMPORARY_FAILURE
Definition: causes.h:121
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:2926
pjsip_uri * request_uri
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1105
static struct topology_change_refresh_data * topology_change_refresh_data_alloc(struct ast_sip_session *session, const struct ast_stream_topology *topology)
Definition: chan_pjsip.c:1524
int ast_sip_session_create_invite(struct ast_sip_session *session, pjsip_tx_data **tdata)
Creates an INVITE request.
#define AST_CAUSE_SWITCH_CONGESTION
Definition: causes.h:122
static int exists(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_logic.c:124
static void local_hold_set_state(struct ast_sip_session_media *session_media, unsigned int held)
Callback which changes the value of locally held on the media stream.
Definition: chan_pjsip.c:1482
Support for translation of data formats. translate.c.
void ast_sip_session_terminate(struct ast_sip_session *session, int response)
Terminate a session and, if possible, send the provided response code.
static void hangup_data_destroy(void *obj)
Definition: chan_pjsip.c:2443
struct ast_party_name name
Subscriber name.
Definition: channel.h:341
void ast_channel_unregister(const struct ast_channel_tech *tech)
Unregister a channel technology.
Definition: channel.c:566
#define UNIQUEID_BUFSIZE
Definition: chan_pjsip.c:76
void ast_dsp_free(struct ast_dsp *dsp)
Definition: dsp.c:1744
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define AST_CAUSE_UNALLOCATED
Definition: causes.h:97
void ast_channel_hangupcause_set(struct ast_channel *chan, int value)
void ast_sip_session_suspend(struct ast_sip_session *session)
Request and wait for the session serializer to be suspended.
static struct ast_custom_function dtmf_mode_function
Definition: chan_pjsip.c:3257
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:1612
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
void ast_channel_set_rawwriteformat(struct ast_channel *chan, struct ast_format *format)
Call Pickup API.
static int chan_pjsip_write(struct ast_channel *ast, struct ast_frame *f)
Definition: chan_pjsip.c:1039
#define LOG_WARNING
Definition: logger.h:274
enum ast_pbx_result ast_pbx_start(struct ast_channel *c)
Create a new thread and start the PBX.
Definition: pbx.c:4712
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
#define AST_FORMAT_CAP_NAMES_LEN
Definition: format_cap.h:326
struct ast_sip_session * ast_sip_session_create_outgoing(struct ast_sip_endpoint *endpoint, struct ast_sip_contact *contact, const char *location, const char *request_user, struct ast_stream_topology *req_topology)
Create a new outgoing SIP session.
union ast_frame::@257 data
int ast_party_id_presentation(const struct ast_party_id *id)
Determine the overall presentation value for the given party.
Definition: channel.c:1807
#define ao2_callback(c, flags, cb_fn, arg)
Definition: astobj2.h:1716
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:2374
struct ast_sip_session * session
Definition: chan_pjsip.c:2701
static int timeout
Definition: cdr_mysql.c:86
static int tmp()
Definition: bt_open.c:389
static int unload_module(void)
Unload the PJSIP channel from Asterisk.
Definition: chan_pjsip.c:3383
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:1117
#define AST_CAUSE_FACILITY_REJECTED
Definition: causes.h:116
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:347
#define AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE
Definition: causes.h:142
static struct transfer_data * transfer_data_alloc(struct ast_sip_session *session, const char *target)
Definition: chan_pjsip.c:1856
unsigned int ignore_183_without_sdp
Definition: res_pjsip.h:901
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:1751
Structure for variables, used for configurations and for channel variables.
ast_control_transfer
#define var
Definition: ast_expr2f.c:614
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:568
static int hangup_cause2sip(int cause)
Internal function which translates from Asterisk cause codes to SIP response codes.
Definition: chan_pjsip.c:2391
#define ast_rtp_glue_register(glue)
Definition: rtp_engine.h:847
unsigned int asymmetric_rtp_codec
Definition: res_pjsip.h:891
#define AST_LIST_NEXT(elm, field)
Returns the next entry in the list after the given entry.
Definition: linkedlists.h:438
struct ast_format_cap * codecs
Definition: res_pjsip.h:770
Test Framework API.
static struct indicate_data * indicate_data_alloc(struct ast_sip_session *session, int condition, int response_code, const void *frame_data, size_t datalen)
Definition: chan_pjsip.c:1308
static pjsip_module refer_callback_module
REFER Callback module, used to attach session data structure to subscription.
Definition: chan_pjsip.c:1913
const ast_string_field context
Definition: res_pjsip.h:815
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)
Definition: chan_pjsip.c:2683
static int chan_pjsip_write_stream(struct ast_channel *ast, int stream_num, struct ast_frame *f)
Definition: chan_pjsip.c:949
Structure to pass both assignedid values to channel drivers.
Definition: channel.h:605
int pjsip_acf_moh_passthrough_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
PJSIP_MOH_PASSTHROUGH function write callback.
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.
Definition: message.c:1369
#define ast_trace(level,...)
Definition: logger.h:876
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:265
A structure which contains a channel implementation and session.
enum ast_sip_session_refresh_method method
Definition: res_pjsip.h:733
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:261
Definition of a media format.
Definition: format.c:43
ast_t38_state
Possible T38 states on channels.
Definition: channel.h:879
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:2139
const ast_string_field uniqueid
unsigned int duration
Definition: chan_pjsip.c:2185
struct ast_sip_session * session
Definition: chan_pjsip.c:1293
void ast_channel_named_pickupgroups_set(struct ast_channel *chan, struct ast_namedgroups *value)
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:319
static void indicate_data_destroy(void *obj)
Definition: chan_pjsip.c:1300
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:2840
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
#define ao2_alloc_options(data_size, destructor_fn, options)
Definition: astobj2.h:406
#define ast_assert(a)
Definition: utils.h:650
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.
Definition: message.c:1381
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:535
Definition: muted.c:95
char * text
Definition: app_queue.c:1511
#define ast_str_alloca(init_len)
Definition: strings.h:800
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
struct ast_party_dialed::@240 number
Dialed/Called number.
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:243
static int call(void *data)
Definition: chan_pjsip.c:2347
Structure for a data store object.
Definition: datastore.h:68
void ast_copy_pj_str(char *dest, const pj_str_t *src, size_t size)
Copy a pj_str_t into a standard character buffer.
Definition: res_pjsip.c:5125
static int hangup(void *data)
Definition: chan_pjsip.c:2472
Generic File Format Support. Should be included by clients of the file handling routines. File service providers should instead include mod_format.h.
static int pbx_start_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
Definition: chan_pjsip.c:3104
void ast_sip_session_unregister_supplement(struct ast_sip_session_supplement *supplement)
Unregister a an supplement to SIP session processing.
Definition: pjsip_session.c:63
char exten[AST_MAX_EXTENSION]
int pjsip_acf_channel_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
CHANNEL function read callback.
const char * args
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
#define NULL
Definition: resample.c:96
const char * data
Domain data structure.
Definition: sip.h:888
#define AST_CAUSE_NO_USER_RESPONSE
Definition: causes.h:107
Definitions to aid in the use of thread local storage.
char * end
Definition: eagi_proxy.c:73
static struct ast_datastore_info direct_media_mitigation_info
Definition: chan_pjsip.c:266
Out-of-call text message support.
void ast_channel_zone_set(struct ast_channel *chan, struct ast_tone_zone *value)
#define AST_FRAME_DTMF
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:5089
Transport information stored in transport_info datastore.
Definition: chan_pjsip.h:30
static void transfer_data_destroy(void *obj)
Definition: chan_pjsip.c:1848
#define AST_CAUSE_INVALID_NUMBER_FORMAT
Definition: causes.h:115
static struct ast_custom_function chan_pjsip_dial_contacts_function
Definition: chan_pjsip.c:3241
void ast_moh_stop(struct ast_channel *chan)
Turn off music on hold on a given channel.
Definition: channel.c:7739
void ast_free_ptr(void *ptr)
free() wrapper
Definition: astmm.c:1771
static int uid_hold_hash_fn(const void *obj, const int flags)
Definition: chan_pjsip.c:1066
struct ast_sip_direct_media_configuration direct_media
Definition: res_pjsip.h:766
static struct ast_sip_session_supplement chan_pjsip_ack_supplement
Definition: chan_pjsip.c:164
#define ast_verb(level,...)
Definition: logger.h:455
#define ast_trace_get_indent()
Definition: logger.h:879
A structure describing a SIP session.
struct ast_trans_pvt * ast_channel_readtrans(const struct ast_channel *chan)
const char * type
Definition: rtp_engine.h:722
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:2961
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:755
int ast_sip_send_request(pjsip_tx_data *tdata, struct pjsip_dialog *dlg, struct ast_sip_endpoint *endpoint, void *token, void(*callback)(void *token, pjsip_event *e))
General purpose method for sending a SIP request.
Definition: res_pjsip.c:4919
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
int ast_sip_add_header(pjsip_tx_data *tdata, const char *name, const char *value)
Add a header to an outbound SIP message.
Definition: res_pjsip.c:4948
static int chan_pjsip_incoming_ack(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
Definition: chan_pjsip.c:3221
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:863
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:1148
static void ast_sockaddr_setnull(struct ast_sockaddr *addr)
Sets address addr to null.
Definition: netsock2.h:140
int ast_queue_hangup_with_cause(struct ast_channel *chan, int cause)
Queue a hangup frame with hangupcause set.
Definition: channel.c:1162
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:765
static int update_connected_line_information(void *data)
Update connected line information.
Definition: chan_pjsip.c:1429
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:5474
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:1850
static int hangup_sip2cause(int cause)
Convert SIP hangup causes to Asterisk hangup causes.
Definition: chan_pjsip.c:2849
struct ast_format * ast_channel_readformat(struct ast_channel *chan)
static void sendtext_data_destroy(void *obj)
Definition: chan_pjsip.c:2705
static int on_topology_change_response(struct ast_sip_session *session, pjsip_rx_data *rdata)
Definition: chan_pjsip.c:1549
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:583
void ast_channel_tech_set(struct ast_channel *chan, const struct ast_channel_tech *value)
struct ast_sip_endpoint_media_configuration media
Definition: res_pjsip.h:841
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
void ast_sip_session_remove_datastore(struct ast_sip_session *session, const char *name)
Remove a session datastore from the session.
#define ao2_bump(obj)
Definition: astobj2.h:491
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:1065
struct ast_party_id id
Caller party ID.
Definition: channel.h:421
static int chan_pjsip_transfer(struct ast_channel *ast, const char *target)
Function called by core for Asterisk initiated transfer.
Definition: chan_pjsip.c:2120
#define ast_str_tmp(init_len, __expr)
Definition: strings.h:1136
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:444
#define ast_log
Definition: astobj2.c:42
void ast_channel_set_rawreadformat(struct ast_channel *chan, struct ast_format *format)
#define AST_CAUSE_NO_ROUTE_TRANSIT_NET
Definition: causes.h:98
static void set_channel_on_rtp_instance(const struct ast_sip_session *session, const char *channel_id)
Definition: chan_pjsip.c:494
int ast_dsp_get_features(struct ast_dsp *dsp)
Get features.
Definition: dsp.c:1738
The arg parameter is a partial search key similar to OBJ_SEARCH_KEY.
Definition: astobj2.h:1120
unsigned long indent
Definition: chan_pjsip.c:679
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:2804
struct ast_channel * chan
Definition: chan_pjsip.c:359
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:9059
struct ast_msg_data * msg
Definition: chan_pjsip.c:2702
struct ast_namedgroups * named_pickupgroups
Definition: res_pjsip.h:663
General Asterisk PBX channel definitions.
static void transfer_refer(struct ast_sip_session *session, const char *target)
Definition: chan_pjsip.c:2033
#define AST_VECTOR_GET_ADDR(vec, idx)
Get an address of element in a vector.
Definition: vector.h:670
void ast_channel_rings_set(struct ast_channel *chan, int value)
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:3175
static int chan_pjsip_hangup(struct ast_channel *ast)
Function called by core to hang up a PJSIP session.
Definition: chan_pjsip.c:2505
struct ast_datastore * ast_sip_session_alloc_datastore(const struct ast_datastore_info *info, const char *uid)
Alternative for ast_datastore_alloc()
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&#39;s bridge.
Definition: channel.c:2490
struct ast_datastore * ast_sip_session_get_datastore(struct ast_sip_session *session, const char *name)
Retrieve a session datastore.
int ast_sip_add_body(pjsip_tx_data *tdata, const struct ast_sip_body *body)
Add a body to an outbound SIP message.
Definition: res_pjsip.c:4976
#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:851
A set of tones for a given locale.
Definition: indications.h:74
void ast_channel_nativeformats_set(struct ast_channel *chan, struct ast_format_cap *value)
struct ast_trans_pvt * ast_channel_writetrans(const struct ast_channel *chan)
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:2016
static int update_devstate(void *obj, void *arg, int flags)
Definition: chan_pjsip.c:3234
PJSIP dialplan functions header file.
static struct ast_mansession session
const ast_string_field language
Definition: res_pjsip.h:827
Channels have this property if they can create jitter; i.e. most VoIP channels.
Definition: channel.h:966
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:5103
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.
Data structure associated with a custom dialplan function.
Definition: pbx.h:118
Access Control of various sorts.
struct ast_sip_channel_pvt * ast_sip_channel_pvt_alloc(void *pvt, struct ast_sip_session *session)
Allocate a new SIP channel pvt structure.
int ast_sip_session_refresh(struct ast_sip_session *session, ast_sip_session_request_creation_cb on_request_creation, ast_sip_session_sdp_creation_cb on_sdp_creation, ast_sip_session_response_cb on_response, enum ast_sip_session_refresh_method method, int generate_new_sdp, struct ast_sip_session_media_state *media_state)
Send a reinvite or UPDATE on a session.
void * frame_data
Definition: chan_pjsip.c:1296
void pjsip_channel_cli_unregister(void)
Unregisters the channel cli commands.
Definition: cli_commands.c:484
#define AST_CAUSE_NORMAL_CLEARING
Definition: causes.h:105
#define AST_CAUSE_CHAN_NOT_IMPLEMENTED
Definition: causes.h:131
static void chan_pjsip_session_begin(struct ast_sip_session *session)
SIP session interaction functions.
Definition: chan_pjsip.c:2939
size_t datalen
Definition: chan_pjsip.c:1297
struct ast_sip_session * session
Definition: chan_pjsip.c:2542
struct ast_sip_media_rtp_configuration rtp
Definition: res_pjsip.h:764
static struct ao2_container * endpoints
#define ao2_ref(o, delta)
Definition: astobj2.h:464
void ast_channel_set_readformat(struct ast_channel *chan, struct ast_format *format)
#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:85
const ast_string_field accountcode
Definition: res_pjsip.h:835
char * ast_frame_subclass2str(struct ast_frame *f, char *subclass, size_t slen, char *moreinfo, size_t mlen)
Copy the discription of a frame&#39;s subclass into the provided string.
Definition: main/frame.c:406
#define DSP_FEATURE_FAX_DETECT
Definition: dsp.h:29
ast_sip_session_media_read_cb read_callback
The callback to invoke.
struct ast_sip_session_media * session
The media session.
static struct ast_custom_function moh_passthrough_function
Definition: chan_pjsip.c:3263
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2309
struct ast_channel * channel
struct ast_namedgroups * named_callgroups
Definition: res_pjsip.h:661
#define ast_format_cap_append(cap, format, framing)
Definition: format_cap.h:103
const char * method
Definition: res_pjsip.c:4220
static int call_pickup_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
Definition: chan_pjsip.c:3054
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:193
enum ast_device_state ast_state_chan2dev(enum ast_channel_state chanstate)
Convert channel state to devicestate.
Definition: devicestate.c:242
static int transmit_info_dtmf(void *data)
Definition: chan_pjsip.c:2207
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...
#define AST_CAUSE_NO_ANSWER
Definition: causes.h:108
struct ast_format_cap * cap
Definition: chan_pjsip.c:362
static int send_direct_media_request(void *data)
Definition: chan_pjsip.c:396
Channels have this property if they can accept input with jitter; i.e. most VoIP channels.
Definition: channel.h:961
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
unsigned int ast_sip_get_disable_multi_domain(void)
Retrieve the system setting &#39;disable multi domain&#39;.
Structure which contains read callback information.
An entity with which Asterisk communicates.
Definition: res_pjsip.h:812
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:4179
static int answer(void *data)
Definition: chan_pjsip.c:682
ast_rtp_glue_result
Definition: rtp_engine.h:158
enum ast_sip_session_refresh_method refresh_method
Definition: res_pjsip.h:647
void ast_sip_session_send_response(struct ast_sip_session *session, pjsip_tx_data *tdata)
Send a SIP response.
#define ast_format_cap_alloc(flags)
Definition: format_cap.h:52
int ast_sip_register_service(pjsip_module *module)
Register a SIP service in Asterisk.
Definition: res_pjsip.c:3217
static int send_topology_change_refresh(void *data)
Definition: chan_pjsip.c:1575
struct ast_sip_session * session
Definition: chan_pjsip.c:363
#define AST_CAUSE_NOTDEFINED
Definition: causes.h:154
Structure to describe a channel "technology", ie a channel driver See for examples: ...
Definition: channel.h:629
void ast_channel_adsicpe_set(struct ast_channel *chan, enum ast_channel_adsicpe value)
const char * ast_channel_exten(const struct ast_channel *chan)
Core PBX routines and definitions.
static int chan_pjsip_sendtext(struct ast_channel *ast, const char *text)
Definition: chan_pjsip.c:2826
struct ast_sip_contact * ast_sip_location_retrieve_contact_from_aor_list(const char *aor_list)
Retrieve the first bound contact from a list of AORs.
Definition: location.c:304
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:525
A snapshot of an endpoint&#39;s state.
struct ast_taskprocessor * serializer
#define AST_CAUSE_DESTINATION_OUT_OF_ORDER
Definition: causes.h:114
const ast_string_field zone
Definition: res_pjsip.h:825
#define ast_test_suite_event_notify(s, f,...)
Definition: test.h:196
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.
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:290
int ast_channel_fdno(const struct ast_channel *chan)
const char * ast_channel_uniqueid(const struct ast_channel *chan)
struct ast_sip_endpoint_id_configuration id
Definition: res_pjsip.h:847
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:2251
#define AST_CAUSE_NORMAL
Definition: causes.h:150
enum ast_sip_session_media_encryption encryption
Definition: res_pjsip.h:711
#define AST_CAUSE_FAILURE
Definition: causes.h:149
int fd
The file descriptor itself.
void ast_channel_stage_snapshot(struct ast_channel *chan)
Set flag to indicate channel snapshot is being staged.
pj_sockaddr remote_addr
The address that sent the request.
Definition: chan_pjsip.h:32
static void transport_info_destroy(void *obj)
Destructor function for transport_info_data.
Definition: chan_pjsip.c:253
struct ast_format * ast_channel_rawreadformat(struct ast_channel *chan)
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:1161
#define AST_EXTENDED_FDS
Definition: channel.h:196
#define LOG_ERROR
Definition: logger.h:285
#define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn)
Definition: astobj2.h:1310
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:200
const char * ast_sip_session_get_name(const struct ast_sip_session *session)
Get the channel or endpoint name associated with the session.
struct ast_channel * chan
Definition: chan_pjsip.c:2440
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
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:1174
static struct ast_sip_session_supplement chan_pjsip_supplement
SIP session supplement structure.
Definition: chan_pjsip.c:143
struct ast_party_id ast_channel_connected_effective_id(struct ast_channel *chan)
static int chan_pjsip_answer(struct ast_channel *ast)
Function called by core when we should answer a PJSIP session.
Definition: chan_pjsip.c:726
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:5023
#define AST_CAUSE_NORMAL_UNSPECIFIED
Definition: causes.h:118
void ast_channel_named_callgroups_set(struct ast_channel *chan, struct ast_namedgroups *value)
static const char channel_type[]
Definition: chan_pjsip.c:78
#define AST_CAUSE_CHANNEL_UNACCEPTABLE
Definition: causes.h:101
int pjsip_acf_dtmf_mode_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
PJSIP_DTMF_MODE function write callback.
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:2656
#define AST_NONSTANDARD_APP_ARGS(args, parse, sep)
Performs the &#39;nonstandard&#39; argument separation process for an application.
struct ast_tone_zone * ast_get_indication_zone(const char *country)
locate ast_tone_zone
Definition: indications.c:433
static void rtp_direct_media_data_destroy(void *data)
Definition: chan_pjsip.c:366
static void info_dtmf_data_destroy(void *obj)
Definition: chan_pjsip.c:2188
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:1925
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.
Definition: message.c:1496
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:281
const char * subtype
Definition: res_pjsip.h:2018
Connected Line/Party information.
Definition: channel.h:457
int ast_rtp_instance_dtmf_begin(struct ast_rtp_instance *instance, char digit)
Begin sending a DTMF digit.
Definition: rtp_engine.c:2081
struct ast_party_dialed * ast_channel_dialed(struct ast_channel *chan)
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:7729
static struct info_dtmf_data * info_dtmf_data_alloc(struct ast_sip_session *session, char digit, unsigned int duration)
Definition: chan_pjsip.c:2194
ast_sip_session_refresh_method
Definition: res_pjsip.h:484
struct ast_format * ast_format_h264
Built-in cached h264 format.
Definition: format_cache.c:176
static struct hangup_data * hangup_data_alloc(int cause, struct ast_channel *chan)
Definition: chan_pjsip.c:2450
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:411
struct ast_sip_t38_configuration t38
Definition: res_pjsip.h:768
#define LOG_NOTICE
Definition: logger.h:263
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:736
unsigned int faxdetect_timeout
Definition: res_pjsip.h:885
struct ast_format_cap * capabilities
Definition: channel.h:633
#define SCOPE_EXIT_RTN(...)
Definition: logger.h:900
int ast_channel_is_bridged(const struct ast_channel *chan)
Determine if a channel is in a bridge.
Definition: channel.c:10603
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:5510
#define ast_strlen_zero(a)
Definition: muted.c:73
struct ast_stream_topology * topology
Definition: chan_pjsip.c:2543
#define ast_channel_unlock(chan)
Definition: channel.h:2891
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:1045
void ast_dsp_set_features(struct ast_dsp *dsp, int features)
Select feature set.
Definition: dsp.c:1729
#define AST_CAUSE_UNREGISTERED
Definition: causes.h:153
int source
Information about the source of an update.
Definition: channel.h:483
#define ast_free(a)
Definition: astmm.h:182
PJSIP Channel Driver shared data structures.
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
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:172
enum ast_sip_direct_media_glare_mitigation glare_mitigation
Definition: res_pjsip.h:735
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:689
const char * dest
Definition: chan_pjsip.c:2544
void ast_hangup(struct ast_channel *chan)
Hang up a channel.
Definition: channel.c:2534
#define AST_CAUSE_NUMBER_CHANGED
Definition: causes.h:111
#define SCOPE_ENTER(level,...)
Definition: logger.h:885
static struct ao2_container * pjsip_uids_onhold
Definition: chan_pjsip.c:1107
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:2192
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:244
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
enum ast_channel_state state
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1756
An API for managing task processing threads that can be shared across modules.
static int request(void *obj)
Definition: chan_pjsip.c:2548
#define AST_CAUSE_INTERWORKING
Definition: causes.h:145
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:2417
static int cdata(void *userdata, int state, const char *cdata, size_t len)
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:838
static char cid_name[AST_MAX_EXTENSION]
Definition: chan_mgcp.c:165
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
struct ast_format_cap * ast_channel_nativeformats(const struct ast_channel *chan)
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
#define AST_PRES_RESTRICTION
Definition: callerid.h:323
struct ast_frame ast_null_frame
Definition: main/frame.c:79
#define SCOPE_EXIT_RTN_VALUE(__return_value,...)
Definition: logger.h:904
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:448
struct ast_endpoint * persistent
Definition: res_pjsip.h:865
struct ast_sip_session_media_state * media_state
Definition: chan_pjsip.c:1513
The arg parameter is an object of the same type.
Definition: astobj2.h:1091
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
Definition: vector.h:682
struct ast_sip_session * session
Definition: chan_pjsip.c:2183
static struct ast_custom_function chan_pjsip_parse_uri_function
Definition: chan_pjsip.c:3246
enum ast_media_type type
Media type of this session media.
static struct rtp_direct_media_data * rtp_direct_media_data_create(struct ast_channel *chan, struct ast_rtp_instance *rtp, struct ast_rtp_instance *vrtp, const struct ast_format_cap *cap, struct ast_sip_session *session)
Definition: chan_pjsip.c:377
#define AST_CAUSE_NO_ROUTE_DESTINATION
Definition: causes.h:99
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:922
#define ast_channel_ref(c)
Increase channel reference count.
Definition: channel.h:2915
static int load_module(void)
Load the module.
Definition: chan_pjsip.c:3284
struct ast_stream_topology * topology
Definition: res_pjsip.h:772
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...
struct ast_sorcery * ast_sip_get_sorcery(void)
Get a pointer to the SIP sorcery structure.
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
Standard Command Line Interface.
Channels have this property if they implement send_text_data.
Definition: channel.h:976
static struct ast_sip_session_supplement call_pickup_supplement
Definition: chan_pjsip.c:3098
int ast_channel_hangupcause(const struct ast_channel *chan)
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:401
static struct ast_threadstorage uniqueid_threadbuf
Definition: chan_pjsip.c:75
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:79
struct ast_rtp_instance * rtp
RTP instance itself.
const char * ast_channel_name(const struct ast_channel *chan)
#define AST_CAUSE_USER_BUSY
Definition: causes.h:106
void ast_sip_unregister_service(pjsip_module *module)
Definition: res_pjsip.c:3233
int ast_setstate(struct ast_channel *chan, enum ast_channel_state)
Change the state of a channel.
Definition: channel.c:7349
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:7011
#define ast_frfree(fr)
#define AST_PRES_ALLOWED
Definition: callerid.h:324
static PGresult * result
Definition: cel_pgsql.c:88
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:1234
static int handle_topology_request_change(struct ast_sip_session *session, const struct ast_stream_topology *proposed)
Definition: chan_pjsip.c:1592
#define AST_CAUSE_BUSY
Definition: causes.h:148
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:2137
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:10554
struct stasis_forward * sub
Definition: res_corosync.c:240
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:171
#define AST_RTP_RTCP_PSFB
Definition: rtp_engine.h:299
You shouldn&#39;t care about the contents of this struct.
Definition: devicestate.h:230
static int sendtext(void *obj)
Definition: chan_pjsip.c:2733
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:215
const char * ast_channel_context(const struct ast_channel *chan)
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:3145
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:705
struct ao2_container * ast_sip_get_endpoints(void)
Retrieve any endpoints available to sorcery.
Reject objects with duplicate keys in container.
Definition: astobj2.h:1192
enum ast_frame_type frametype
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:746