2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
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.
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.
21 * \brief Implementation of Inter-Asterisk eXchange Version 2
22 * as specified in RFC 5456
24 * \author Mark Spencer <markster@digium.com>
27 * \arg \ref Config_iax
29 * \ingroup channel_drivers
31 * \todo Implement musicclass settings for IAX2 devices
34 /*! \li \ref chan_iax2.c uses the configuration file \ref iax.conf
35 * \addtogroup configuration_file
38 /*! \page iax.conf iax.conf
39 * \verbinclude iax.conf.sample
43 * \todo XXX The IAX2 channel driver needs its native bridge
44 * code converted to the new bridge technology scheme.
46 * \note The chan_dahdi native bridge code can be used as an
47 * example. It also appears that chan_iax2 also has a native
48 * transfer check like chan_dahdi to eliminate tromboned calls.
50 * \note The existing native bridge code is marked with the
51 * IAX2_NATIVE_BRIDGING conditional.
55 <use type="external">crypto</use>
56 <support_level>core</support_level>
61 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
65 #include <sys/socket.h>
66 #include <netinet/in.h>
67 #include <arpa/inet.h>
68 #include <netinet/in_systm.h>
69 #include <netinet/ip.h>
71 #include <sys/signal.h>
79 #include "asterisk/paths.h"
81 #include "asterisk/lock.h"
82 #include "asterisk/frame.h"
83 #include "asterisk/channel.h"
84 #include "asterisk/module.h"
85 #include "asterisk/pbx.h"
86 #include "asterisk/sched.h"
87 #include "asterisk/io.h"
88 #include "asterisk/config.h"
89 #include "asterisk/cli.h"
90 #include "asterisk/translate.h"
91 #include "asterisk/md5.h"
92 #include "asterisk/crypto.h"
93 #include "asterisk/acl.h"
94 #include "asterisk/manager.h"
95 #include "asterisk/callerid.h"
96 #include "asterisk/app.h"
97 #include "asterisk/astdb.h"
98 #include "asterisk/musiconhold.h"
99 #include "asterisk/features.h"
100 #include "asterisk/utils.h"
101 #include "asterisk/causes.h"
102 #include "asterisk/localtime.h"
103 #include "asterisk/dnsmgr.h"
104 #include "asterisk/devicestate.h"
105 #include "asterisk/netsock.h"
106 #include "asterisk/stringfields.h"
107 #include "asterisk/linkedlists.h"
108 #include "asterisk/astobj2.h"
109 #include "asterisk/timing.h"
110 #include "asterisk/taskprocessor.h"
111 #include "asterisk/test.h"
112 #include "asterisk/data.h"
113 #include "asterisk/security_events.h"
114 #include "asterisk/stasis_endpoints.h"
115 #include "asterisk/bridge.h"
116 #include "asterisk/stasis.h"
117 #include "asterisk/stasis_system.h"
118 #include "asterisk/stasis_channels.h"
119 #include "asterisk/format_cache.h"
120 #include "asterisk/format_compatibility.h"
121 #include "asterisk/format_cap.h"
123 #include "iax2/include/iax2.h"
124 #include "iax2/include/firmware.h"
125 #include "iax2/include/parser.h"
126 #include "iax2/include/provision.h"
127 #include "iax2/include/codec_pref.h"
128 #include "iax2/include/format_compatibility.h"
130 #include "jitterbuf.h"
133 <application name="IAX2Provision" language="en_US">
135 Provision a calling IAXy with a given template.
138 <parameter name="template">
139 <para>If not specified, defaults to <literal>default</literal>.</para>
143 <para>Provisions the calling IAXy (assuming the calling entity is in fact an IAXy) with the
144 given <replaceable>template</replaceable>. Returns <literal>-1</literal> on error
145 or <literal>0</literal> on success.</para>
148 <function name="IAXPEER" language="en_US">
150 Gets IAX peer information.
153 <parameter name="peername" required="true">
155 <enum name="CURRENTCHANNEL">
156 <para>If <replaceable>peername</replaceable> is specified to this value, return the IP address of the
157 endpoint of the current channel</para>
161 <parameter name="item">
162 <para>If <replaceable>peername</replaceable> is specified, valid items are:</para>
165 <para>(default) The IP address.</para>
168 <para>The peer's status (if <literal>qualify=yes</literal>)</para>
170 <enum name="mailbox">
171 <para>The configured mailbox.</para>
173 <enum name="context">
174 <para>The configured context.</para>
177 <para>The epoch time of the next expire.</para>
179 <enum name="dynamic">
180 <para>Is it dynamic? (yes/no).</para>
182 <enum name="callerid_name">
183 <para>The configured Caller ID name.</para>
185 <enum name="callerid_num">
186 <para>The configured Caller ID number.</para>
189 <para>The configured codecs.</para>
191 <enum name="codec[x]">
192 <para>Preferred codec index number <replaceable>x</replaceable> (beginning
193 with <literal>0</literal>)</para>
199 <para>Gets information associated with the specified IAX2 peer.</para>
202 <ref type="function">SIPPEER</ref>
205 <function name="IAXVAR" language="en_US">
207 Sets or retrieves a remote variable.
210 <parameter name="varname" required="true" />
213 <para>Gets or sets a variable that is sent to a remote IAX2 peer during call setup.</para>
216 <manager name="IAXpeers" language="en_US">
221 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
226 <manager name="IAXpeerlist" language="en_US">
231 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
234 <para>List all the IAX peers.</para>
237 <manager name="IAXnetstats" language="en_US">
243 <para>Show IAX channels network statistics.</para>
246 <manager name="IAXregistry" language="en_US">
248 Show IAX registrations.
251 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
254 <para>Show IAX registrations.</para>
259 /* Define SCHED_MULTITHREADED to run the scheduler in a special
260 multithreaded mode. */
261 #define SCHED_MULTITHREADED
263 /* Define DEBUG_SCHED_MULTITHREADED to keep track of where each
264 thread is actually doing. */
265 #define DEBUG_SCHED_MULTITHREAD
269 static int nochecksums = 0;
272 #define PTR_TO_CALLNO(a) ((unsigned short)(unsigned long)(a))
273 #define CALLNO_TO_PTR(a) ((void *)(unsigned long)(a))
275 #define DEFAULT_THREAD_COUNT 10
276 #define DEFAULT_MAX_THREAD_COUNT 100
277 #define DEFAULT_RETRY_TIME 1000
278 #define MEMORY_SIZE 100
279 #define DEFAULT_DROP 3
281 #define DEBUG_SUPPORT
283 #define MIN_REUSE_TIME 60 /* Don't reuse a call number within 60 seconds */
285 /* Sample over last 100 units to determine historic jitter */
288 static struct iax2_codec_pref prefs_global;
290 static const char tdesc[] = "Inter Asterisk eXchange Driver (Ver 2)";
293 /*! \brief Maximum transmission unit for the UDP packet in the trunk not to be
294 fragmented. This is based on 1516 - ethernet - ip - udp - iax minus one g711 frame = 1240 */
295 #define MAX_TRUNK_MTU 1240
297 static int global_max_trunk_mtu; /*!< Maximum MTU, 0 if not used */
298 static int trunk_timed, trunk_untimed, trunk_maxmtu, trunk_nmaxmtu ; /*!< Trunk MTU statistics */
300 #define DEFAULT_CONTEXT "default"
302 static char default_parkinglot[AST_MAX_CONTEXT];
304 static char language[MAX_LANGUAGE] = "";
305 static char regcontext[AST_MAX_CONTEXT] = "";
307 static struct stasis_subscription *network_change_sub; /*!< subscription id for network change events */
308 static struct stasis_subscription *acl_change_sub; /*!< subscription id for ACL change events */
309 static int network_change_sched_id = -1;
311 static int maxauthreq = 3;
312 static int max_retries = 4;
313 static int ping_time = 21;
314 static int lagrq_time = 10;
315 static int maxjitterbuffer=1000;
316 static int resyncthreshold=1000;
317 static int maxjitterinterps=10;
318 static int jittertargetextra = 40; /* number of milliseconds the new jitter buffer adds on to its size */
320 #define MAX_TRUNKDATA 640 * 200 /*!< 40ms, uncompressed linear * 200 channels */
322 static int trunkfreq = 20;
323 static int trunkmaxsize = MAX_TRUNKDATA;
325 static int authdebug = 0;
326 static int autokill = 0;
327 static int iaxcompat = 0;
328 static int last_authmethod = 0;
330 static int iaxdefaultdpcache=10 * 60; /* Cache dialplan entries for 10 minutes by default */
332 static int iaxdefaulttimeout = 5; /* Default to wait no more than 5 seconds for a reply to come back */
339 static int min_reg_expire;
340 static int max_reg_expire;
342 static int srvlookup = 0;
344 static struct ast_timer *timer; /* Timer for trunking */
346 static struct ast_netsock_list *netsock;
347 static struct ast_netsock_list *outsock; /*!< used if sourceaddress specified and bindaddr == INADDR_ANY */
348 static int defaultsockfd = -1;
350 static int (*iax2_regfunk)(const char *username, int onoff) = NULL;
353 #define IAX_CAPABILITY_FULLBANDWIDTH 0xFFFF
355 #define IAX_CAPABILITY_MEDBANDWIDTH (IAX_CAPABILITY_FULLBANDWIDTH & \
357 ~AST_FORMAT_SLIN16 & \
358 ~AST_FORMAT_SIREN7 & \
359 ~AST_FORMAT_SIREN14 & \
365 #define IAX_CAPABILITY_LOWBANDWIDTH (IAX_CAPABILITY_MEDBANDWIDTH & \
367 ~AST_FORMAT_G726_AAL2 & \
370 #define IAX_CAPABILITY_LOWFREE (IAX_CAPABILITY_LOWBANDWIDTH & \
374 #define DEFAULT_MAXMS 2000 /* Must be faster than 2 seconds by default */
375 #define DEFAULT_FREQ_OK 60 * 1000 /* How often to check for the host to be up */
376 #define DEFAULT_FREQ_NOTOK 10 * 1000 /* How often to check, if the host is down... */
378 /* if a pvt has encryption setup done and is running on the call */
379 #define IAX_CALLENCRYPTED(pvt) \
380 (ast_test_flag64(pvt, IAX_ENCRYPTED) && ast_test_flag64(pvt, IAX_KEYPOPULATED))
382 #define IAX_DEBUGDIGEST(msg, key) do { \
384 char digest[33] = ""; \
389 for (idx = 0; idx < 16; idx++) \
390 sprintf(digest + (idx << 1), "%02hhx", (unsigned char) key[idx]); \
392 ast_log(LOG_NOTICE, msg " IAX_COMMAND_RTKEY to rotate key to '%s'\n", digest); \
395 static struct io_context *io;
396 static struct ast_sched_context *sched;
398 #define DONT_RESCHEDULE -2
400 static iax2_format iax2_capability = IAX_CAPABILITY_FULLBANDWIDTH;
402 static int iaxdebug = 0;
404 static int iaxtrunkdebug = 0;
406 static int test_losspct = 0;
408 static int test_late = 0;
409 static int test_resync = 0;
410 static int test_jit = 0;
411 static int test_jitpct = 0;
412 #endif /* IAXTESTS */
414 static char accountcode[AST_MAX_ACCOUNT_CODE];
415 static char mohinterpret[MAX_MUSICCLASS];
416 static char mohsuggest[MAX_MUSICCLASS];
417 static int amaflags = 0;
419 static int delayreject = 0;
420 static int iax2_encryption = 0;
422 static struct ast_flags64 globalflags = { 0 };
424 static pthread_t netthreadid = AST_PTHREADT_NULL;
427 IAX_STATE_STARTED = (1 << 0),
428 IAX_STATE_AUTHENTICATED = (1 << 1),
429 IAX_STATE_TBD = (1 << 2),
432 struct iax2_context {
433 char context[AST_MAX_CONTEXT];
434 struct iax2_context *next;
438 #define IAX_HASCALLERID (uint64_t)(1 << 0) /*!< CallerID has been specified */
439 #define IAX_DELME (uint64_t)(1 << 1) /*!< Needs to be deleted */
440 #define IAX_TEMPONLY (uint64_t)(1 << 2) /*!< Temporary (realtime) */
441 #define IAX_TRUNK (uint64_t)(1 << 3) /*!< Treat as a trunk */
442 #define IAX_NOTRANSFER (uint64_t)(1 << 4) /*!< Don't native bridge */
443 #define IAX_USEJITTERBUF (uint64_t)(1 << 5) /*!< Use jitter buffer */
444 #define IAX_DYNAMIC (uint64_t)(1 << 6) /*!< dynamic peer */
445 #define IAX_SENDANI (uint64_t)(1 << 7) /*!< Send ANI along with CallerID */
446 #define IAX_RTSAVE_SYSNAME (uint64_t)(1 << 8) /*!< Save Systname on Realtime Updates */
447 #define IAX_ALREADYGONE (uint64_t)(1 << 9) /*!< Already disconnected */
448 #define IAX_PROVISION (uint64_t)(1 << 10) /*!< This is a provisioning request */
449 #define IAX_QUELCH (uint64_t)(1 << 11) /*!< Whether or not we quelch audio */
450 #define IAX_ENCRYPTED (uint64_t)(1 << 12) /*!< Whether we should assume encrypted tx/rx */
451 #define IAX_KEYPOPULATED (uint64_t)(1 << 13) /*!< Whether we have a key populated */
452 #define IAX_CODEC_USER_FIRST (uint64_t)(1 << 14) /*!< are we willing to let the other guy choose the codec? */
453 #define IAX_CODEC_NOPREFS (uint64_t)(1 << 15) /*!< Force old behaviour by turning off prefs */
454 #define IAX_CODEC_NOCAP (uint64_t)(1 << 16) /*!< only consider requested format and ignore capabilities*/
455 #define IAX_RTCACHEFRIENDS (uint64_t)(1 << 17) /*!< let realtime stay till your reload */
456 #define IAX_RTUPDATE (uint64_t)(1 << 18) /*!< Send a realtime update */
457 #define IAX_RTAUTOCLEAR (uint64_t)(1 << 19) /*!< erase me on expire */
458 #define IAX_FORCEJITTERBUF (uint64_t)(1 << 20) /*!< Force jitterbuffer, even when bridged to a channel that can take jitter */
459 #define IAX_RTIGNOREREGEXPIRE (uint64_t)(1 << 21) /*!< When using realtime, ignore registration expiration */
460 #define IAX_TRUNKTIMESTAMPS (uint64_t)(1 << 22) /*!< Send trunk timestamps */
461 #define IAX_TRANSFERMEDIA (uint64_t)(1 << 23) /*!< When doing IAX2 transfers, transfer media only */
462 #define IAX_MAXAUTHREQ (uint64_t)(1 << 24) /*!< Maximum outstanding AUTHREQ restriction is in place */
463 #define IAX_DELAYPBXSTART (uint64_t)(1 << 25) /*!< Don't start a PBX on the channel until the peer sends us a response, so that we've achieved a three-way handshake with them before sending voice or anything else */
464 #define IAX_ALLOWFWDOWNLOAD (uint64_t)(1 << 26) /*!< Allow the FWDOWNL command? */
465 #define IAX_IMMEDIATE (uint64_t)(1 << 27) /*!< Allow immediate off-hook to extension s */
466 #define IAX_SENDCONNECTEDLINE (uint64_t)(1 << 28) /*!< Allow sending of connected line updates */
467 #define IAX_RECVCONNECTEDLINE (uint64_t)(1 << 29) /*!< Allow receiving of connected line updates */
468 #define IAX_FORCE_ENCRYPT (uint64_t)(1 << 30) /*!< Forces call encryption, if encryption not possible hangup */
469 #define IAX_SHRINKCALLERID (uint64_t)(1 << 31) /*!< Turn on and off caller id shrinking */
470 static int global_rtautoclear = 120;
472 static int reload_config(int forced_reload);
475 * \brief Call token validation settings.
477 enum calltoken_peer_enum {
478 /*! \brief Default calltoken required unless the ip is in the ignorelist */
479 CALLTOKEN_DEFAULT = 0,
480 /*! \brief Require call token validation. */
482 /*! \brief Require call token validation after a successful registration
483 * using call token validation occurs. */
485 /*! \brief Do not require call token validation. */
490 AST_DECLARE_STRING_FIELDS(
491 AST_STRING_FIELD(name);
492 AST_STRING_FIELD(secret);
493 AST_STRING_FIELD(dbsecret);
494 AST_STRING_FIELD(accountcode);
495 AST_STRING_FIELD(mohinterpret);
496 AST_STRING_FIELD(mohsuggest);
497 AST_STRING_FIELD(inkeys); /*!< Key(s) this user can use to authenticate to us */
498 AST_STRING_FIELD(language);
499 AST_STRING_FIELD(cid_num);
500 AST_STRING_FIELD(cid_name);
501 AST_STRING_FIELD(parkinglot); /*!< Default parkinglot for device */
509 iax2_format capability;
510 int maxauthreq; /*!< Maximum allowed outstanding AUTHREQs */
511 int curauthreq; /*!< Current number of outstanding AUTHREQs */
512 struct iax2_codec_pref prefs;
513 struct ast_acl_list *acl;
514 struct iax2_context *contexts;
515 struct ast_variable *vars;
516 enum calltoken_peer_enum calltoken_required; /*!< Is calltoken validation required or not, can be YES, NO, or AUTO */
520 AST_DECLARE_STRING_FIELDS(
521 AST_STRING_FIELD(name);
522 AST_STRING_FIELD(username);
523 AST_STRING_FIELD(description); /*!< Description of the peer */
524 AST_STRING_FIELD(secret);
525 AST_STRING_FIELD(dbsecret);
526 AST_STRING_FIELD(outkey); /*!< What key we use to talk to this peer */
528 AST_STRING_FIELD(regexten); /*!< Extension to register (if regcontext is used) */
529 AST_STRING_FIELD(context); /*!< For transfers only */
530 AST_STRING_FIELD(peercontext); /*!< Context to pass to peer */
531 AST_STRING_FIELD(mailbox); /*!< Mailbox */
532 AST_STRING_FIELD(mohinterpret);
533 AST_STRING_FIELD(mohsuggest);
534 AST_STRING_FIELD(inkeys); /*!< Key(s) this peer can use to authenticate to us */
535 /* Suggested caller id if registering */
536 AST_STRING_FIELD(cid_num); /*!< Default context (for transfer really) */
537 AST_STRING_FIELD(cid_name); /*!< Default context (for transfer really) */
538 AST_STRING_FIELD(zonetag); /*!< Time Zone */
539 AST_STRING_FIELD(parkinglot); /*!< Default parkinglot for device */
541 struct iax2_codec_pref prefs;
542 struct ast_dnsmgr_entry *dnsmgr; /*!< DNS refresh manager */
543 struct ast_sockaddr addr;
545 int sockfd; /*!< Socket to use for transmission */
546 struct ast_sockaddr mask;
550 /* Dynamic Registration fields */
551 struct ast_sockaddr defaddr; /*!< Default address if there is one */
552 int authmethods; /*!< Authentication methods (IAX_AUTH_*) */
553 int encmethods; /*!< Encryption methods (IAX_ENCRYPT_*) */
555 int expire; /*!< Schedule entry for expiry */
556 int expiry; /*!< How soon to expire */
557 iax2_format capability; /*!< Capability */
560 int callno; /*!< Call number of POKE request */
561 int pokeexpire; /*!< Scheduled qualification-related task (ie iax2_poke_peer_s or iax2_poke_noanswer) */
562 int lastms; /*!< How long last response took (in ms), or -1 for no response */
563 int maxms; /*!< Max ms we will accept for the host to be up, 0 to not monitor */
565 int pokefreqok; /*!< How often to check if the host is up */
566 int pokefreqnotok; /*!< How often to check when the host has been determined to be down */
567 int historicms; /*!< How long recent average responses took */
568 int smoothing; /*!< Sample over how many units to determine historic ms */
569 uint16_t maxcallno; /*!< Max call number limit for this peer. Set on registration */
571 struct stasis_subscription *mwi_event_sub; /*!< This subscription lets pollmailboxes know which mailboxes need to be polled */
573 struct ast_acl_list *acl;
574 enum calltoken_peer_enum calltoken_required; /*!< Is calltoken validation required or not, can be YES, NO, or AUTO */
576 struct ast_endpoint *endpoint; /*!< Endpoint structure for this peer */
579 #define IAX2_TRUNK_PREFACE (sizeof(struct iax_frame) + sizeof(struct ast_iax2_meta_hdr) + sizeof(struct ast_iax2_meta_trunk_hdr))
581 struct iax2_trunk_peer {
584 struct ast_sockaddr addr;
585 struct timeval txtrunktime; /*!< Transmit trunktime */
586 struct timeval rxtrunktime; /*!< Receive trunktime */
587 struct timeval lasttxtime; /*!< Last transmitted trunktime */
588 struct timeval trunkact; /*!< Last trunk activity */
589 unsigned int lastsent; /*!< Last sent time */
590 /* Trunk data and length */
591 unsigned char *trunkdata;
592 unsigned int trunkdatalen;
593 unsigned int trunkdataalloc;
597 AST_LIST_ENTRY(iax2_trunk_peer) list;
600 static AST_LIST_HEAD_STATIC(tpeers, iax2_trunk_peer);
603 REG_STATE_UNREGISTERED = 0,
606 REG_STATE_REGISTERED,
612 enum iax_transfer_state {
617 TRANSFER_PASSTHROUGH,
621 TRANSFER_MPASSTHROUGH,
626 struct iax2_registry {
627 struct ast_sockaddr addr; /*!< Who we connect to for registration purposes */
629 char secret[80]; /*!< Password or key name in []'s */
630 int expire; /*!< Sched ID of expiration */
631 int refresh; /*!< How often to refresh */
632 enum iax_reg_state regstate;
633 int messages; /*!< Message count, low 8 bits = new, high 8 bits = old */
634 int callno; /*!< Associated call number if applicable */
635 struct ast_sockaddr us; /*!< Who the server thinks we are */
636 struct ast_dnsmgr_entry *dnsmgr; /*!< DNS refresh manager */
637 AST_LIST_ENTRY(iax2_registry) entry;
642 static AST_LIST_HEAD_STATIC(registrations, iax2_registry);
644 /* Don't retry more frequently than every 10 ms, or less frequently than every 5 seconds */
645 #define MIN_RETRY_TIME 100
646 #define MAX_RETRY_TIME 10000
648 #define MAX_JITTER_BUFFER 50
649 #define MIN_JITTER_BUFFER 10
651 #define DEFAULT_TRUNKDATA 640 * 10 /*!< 40ms, uncompressed linear * 10 channels */
653 #define MAX_TIMESTAMP_SKEW 160 /*!< maximum difference between actual and predicted ts for sending */
655 /* If consecutive voice frame timestamps jump by more than this many milliseconds, then jitter buffer will resync */
656 #define TS_GAP_FOR_JB_RESYNC 5000
658 /* used for first_iax_message and last_iax_message. If this bit is set it was TX, else RX */
659 #define MARK_IAX_SUBCLASS_TX 0x8000
661 static int iaxthreadcount = DEFAULT_THREAD_COUNT;
662 static int iaxmaxthreadcount = DEFAULT_MAX_THREAD_COUNT;
663 static int iaxdynamicthreadcount = 0;
664 static int iaxdynamicthreadnum = 0;
665 static int iaxactivethreadcount = 0;
679 /* We use the high order bit as the validated flag, and the lower 15 as the
680 * actual call number */
681 typedef uint16_t callno_entry;
683 struct chan_iax2_pvt {
684 /*! Socket to send/receive on for this call */
686 /*! ast_callid bound to dialog */
687 struct ast_callid *callid;
688 /*! Last received voice format */
689 iax2_format voiceformat;
690 /*! Last received video format */
691 iax2_format videoformat;
692 /*! Last sent voice format */
693 iax2_format svoiceformat;
694 /*! Last sent video format */
695 iax2_format svideoformat;
696 /*! What we are capable of sending */
697 iax2_format capability;
698 /*! Last received timestamp */
700 /*! Last sent timestamp - never send the same timestamp twice in a single call */
701 unsigned int lastsent;
702 /*! Timestamp of the last video frame sent */
703 unsigned int lastvsent;
704 /*! Next outgoing timestamp if everything is good */
705 unsigned int nextpred;
706 /*! iax frame subclass that began iax2_pvt entry. 0x8000 bit is set on TX */
707 int first_iax_message;
708 /*! Last iax frame subclass sent or received for a iax2_pvt. 0x8000 bit is set on TX */
709 int last_iax_message;
710 /*! True if the last voice we transmitted was not silence/CNG */
711 unsigned int notsilenttx:1;
713 unsigned int pingtime;
714 /*! Max time for initial response */
717 struct ast_sockaddr addr;
718 /*! Actual used codec preferences */
719 struct iax2_codec_pref prefs;
720 /*! Requested codec preferences */
721 struct iax2_codec_pref rprefs;
722 /*! Our call number */
723 unsigned short callno;
724 /*! Our callno_entry entry */
725 callno_entry callno_entry;
727 unsigned short peercallno;
728 /*! Negotiated format, this is only used to remember what format was
729 chosen for an unauthenticated call so that the channel can get
730 created later using the right format */
731 iax2_format chosenformat;
732 /*! Peer selected format */
733 iax2_format peerformat;
734 /*! Peer capability */
735 iax2_format peercapability;
736 /*! timeval that we base our transmission on */
737 struct timeval offset;
738 /*! timeval that we base our delivery on */
739 struct timeval rxcore;
740 /*! The jitterbuffer */
742 /*! active jb read scheduler id */
746 /*! Error, as discovered by the manager */
748 /*! Owner if we have one */
749 struct ast_channel *owner;
750 /*! What's our state? */
751 struct ast_flags state;
752 /*! Expiry (optional) */
754 /*! Next outgoing sequence number */
755 unsigned char oseqno;
756 /*! Next sequence number they have not yet acknowledged */
757 unsigned char rseqno;
758 /*! Next incoming sequence number */
759 unsigned char iseqno;
760 /*! Last incoming sequence number we have acknowledged */
761 unsigned char aseqno;
763 AST_DECLARE_STRING_FIELDS(
765 AST_STRING_FIELD(peer);
766 /*! Default Context */
767 AST_STRING_FIELD(context);
768 /*! Caller ID if available */
769 AST_STRING_FIELD(cid_num);
770 AST_STRING_FIELD(cid_name);
771 /*! Hidden Caller ID (i.e. ANI) if appropriate */
772 AST_STRING_FIELD(ani);
774 AST_STRING_FIELD(dnid);
776 AST_STRING_FIELD(rdnis);
777 /*! Requested Extension */
778 AST_STRING_FIELD(exten);
779 /*! Expected Username */
780 AST_STRING_FIELD(username);
781 /*! Expected Secret */
782 AST_STRING_FIELD(secret);
784 AST_STRING_FIELD(challenge);
785 /*! Public keys permitted keys for incoming authentication */
786 AST_STRING_FIELD(inkeys);
787 /*! Private key for outgoing authentication */
788 AST_STRING_FIELD(outkey);
789 /*! Preferred language */
790 AST_STRING_FIELD(language);
791 /*! Hostname/peername for naming purposes */
792 AST_STRING_FIELD(host);
794 AST_STRING_FIELD(dproot);
795 AST_STRING_FIELD(accountcode);
796 AST_STRING_FIELD(mohinterpret);
797 AST_STRING_FIELD(mohsuggest);
798 /*! received OSP token */
799 AST_STRING_FIELD(osptoken);
800 /*! Default parkinglot */
801 AST_STRING_FIELD(parkinglot);
803 /*! AUTHREJ all AUTHREP frames */
805 /*! permitted authentication methods */
807 /*! permitted encryption methods */
809 /*! Encryption AES-128 Key */
810 ast_aes_encrypt_key ecx;
811 /*! Decryption AES-128 Key corresponding to ecx */
812 ast_aes_decrypt_key mydcx;
813 /*! Decryption AES-128 Key used to decrypt peer frames */
814 ast_aes_decrypt_key dcx;
815 /*! scheduler id associated with iax_key_rotate
816 * for encrypted calls*/
818 /*! 32 bytes of semi-random data */
819 unsigned char semirand[32];
820 /*! Associated registry */
821 struct iax2_registry *reg;
822 /*! Associated peer for poking */
823 struct iax2_peer *peerpoke;
828 /*! Transferring status */
829 enum iax_transfer_state transferring;
830 /*! Transfer identifier */
832 /*! Who we are IAX transferring to */
833 struct ast_sockaddr transfer;
834 /*! What's the new call number for the transfer */
835 unsigned short transfercallno;
836 /*! Transfer encrypt AES-128 Key */
837 ast_aes_encrypt_key tdcx;
839 /*! Status of knowledge of peer ADSI capability */
842 /*! Callno of native bridge peer. (Valid if nonzero) */
843 unsigned short bridgecallno;
845 int pingid; /*!< Transmit PING request */
846 int lagid; /*!< Retransmit lag request */
847 int autoid; /*!< Auto hangup for Dialplan requestor */
848 int authid; /*!< Authentication rejection ID */
849 int authfail; /*!< Reason to report failure */
850 int initid; /*!< Initial peer auto-congest ID (based on qualified peers) */
855 AST_LIST_HEAD_NOLOCK(, iax2_dpcache) dpentries;
856 /*! variables inherited from the user definition */
857 struct ast_variable *vars;
858 /*! variables transmitted in a NEW packet */
859 struct ast_variable *iaxvars;
860 /*! last received remote rr */
861 struct iax_rr remote_rr;
862 /*! Current base time: (just for stats) */
864 /*! Dropped frame count: (just for stats) */
866 /*! received frame count: (just for stats) */
868 /*! num bytes used for calltoken ie, even an empty ie should contain 2 */
869 unsigned char calltoken_ie_len;
870 /*! hold all signaling frames from the pbx thread until we have a destination callno */
872 /*! frame queue for signaling frames from pbx thread waiting for destination callno */
873 AST_LIST_HEAD_NOLOCK(signaling_queue, signaling_queue_entry) signaling_queue;
876 struct signaling_queue_entry {
878 AST_LIST_ENTRY(signaling_queue_entry) next;
886 #define PTR_TO_CALLNO_ENTRY(a) ((uint16_t)(unsigned long)(a))
887 #define CALLNO_ENTRY_TO_PTR(a) ((void *)(unsigned long)(a))
889 #define CALLNO_ENTRY_SET_VALIDATED(a) ((a) |= 0x8000)
890 #define CALLNO_ENTRY_IS_VALIDATED(a) ((a) & 0x8000)
891 #define CALLNO_ENTRY_GET_CALLNO(a) ((a) & 0x7FFF)
893 struct call_number_pool {
896 callno_entry numbers[IAX_MAX_CALLS / 2 + 1];
899 AST_MUTEX_DEFINE_STATIC(callno_pool_lock);
901 /*! table of available call numbers */
902 static struct call_number_pool callno_pool;
904 /*! table of available trunk call numbers */
905 static struct call_number_pool callno_pool_trunk;
908 * \brief a list of frames that may need to be retransmitted
910 * \note The contents of this list do not need to be explicitly destroyed
911 * on module unload. This is because all active calls are destroyed, and
912 * all frames in this queue will get destroyed as a part of that process.
914 * \note Contents protected by the iaxsl[] locks
916 static AST_LIST_HEAD_NOLOCK(, iax_frame) frame_queue[IAX_MAX_CALLS];
918 static struct ast_taskprocessor *transmit_processor;
920 static int randomcalltokendata;
922 static const time_t MAX_CALLTOKEN_DELAY = 10;
925 * This module will get much higher performance when doing a lot of
926 * user and peer lookups if the number of buckets is increased from 1.
927 * However, to maintain old behavior for Asterisk 1.4, these are set to
928 * 1 by default. When using multiple buckets, search order through these
929 * containers is considered random, so you will not be able to depend on
930 * the order the entires are specified in iax.conf for matching order. */
932 #define MAX_PEER_BUCKETS 17
934 #define MAX_PEER_BUCKETS 563
936 static struct ao2_container *peers;
938 #define MAX_USER_BUCKETS MAX_PEER_BUCKETS
939 static struct ao2_container *users;
941 /*! Table containing peercnt objects for every ip address consuming a callno */
942 static struct ao2_container *peercnts;
944 /*! Table containing custom callno limit rules for a range of ip addresses. */
945 static struct ao2_container *callno_limits;
947 /*! Table containing ip addresses not requiring calltoken validation */
948 static struct ao2_container *calltoken_ignores;
950 static uint16_t DEFAULT_MAXCALLNO_LIMIT = 2048;
952 static uint16_t DEFAULT_MAXCALLNO_LIMIT_NONVAL = 8192;
954 static uint16_t global_maxcallno;
956 /*! Total num of call numbers allowed to be allocated without calltoken validation */
957 static uint16_t global_maxcallno_nonval;
959 static uint16_t total_nonval_callno_used = 0;
961 /*! peer connection private, keeps track of all the call numbers
962 * consumed by a single ip address */
964 /*! ip address consuming call numbers */
965 struct ast_sockaddr addr;
966 /*! Number of call numbers currently used by this ip address */
968 /*! Max call numbers allowed for this ip address */
970 /*! Specifies whether limit is set by a registration or not, if so normal
971 * limit setting rules do not apply to this address. */
975 /*! used by both callno_limits and calltoken_ignores containers */
977 /*! ip address range for custom callno limit rule */
979 /*! callno limit for this ip address range, only used in callno_limits container */
981 /*! delete me marker for reloads */
986 /*! Extension exists */
987 CACHE_FLAG_EXISTS = (1 << 0),
988 /*! Extension is nonexistent */
989 CACHE_FLAG_NONEXISTENT = (1 << 1),
990 /*! Extension can exist */
991 CACHE_FLAG_CANEXIST = (1 << 2),
992 /*! Waiting to hear back response */
993 CACHE_FLAG_PENDING = (1 << 3),
995 CACHE_FLAG_TIMEOUT = (1 << 4),
996 /*! Request transmitted */
997 CACHE_FLAG_TRANSMITTED = (1 << 5),
999 CACHE_FLAG_UNKNOWN = (1 << 6),
1001 CACHE_FLAG_MATCHMORE = (1 << 7),
1004 struct iax2_dpcache {
1005 char peercontext[AST_MAX_CONTEXT];
1006 char exten[AST_MAX_EXTENSION];
1007 struct timeval orig;
1008 struct timeval expiry;
1010 unsigned short callno;
1012 AST_LIST_ENTRY(iax2_dpcache) cache_list;
1013 AST_LIST_ENTRY(iax2_dpcache) peer_list;
1016 static AST_LIST_HEAD_STATIC(dpcache, iax2_dpcache);
1018 static void reg_source_db(struct iax2_peer *p);
1019 static struct iax2_peer *realtime_peer(const char *peername, struct ast_sockaddr *addr);
1020 static struct iax2_user *realtime_user(const char *username, struct ast_sockaddr *addr);
1022 static int ast_cli_netstats(struct mansession *s, int fd, int limit_fmt);
1023 static char *complete_iax2_peers(const char *line, const char *word, int pos, int state, uint64_t flags);
1024 static char *complete_iax2_unregister(const char *line, const char *word, int pos, int state);
1026 enum iax2_thread_iostate {
1029 IAX_IOSTATE_PROCESSING,
1030 IAX_IOSTATE_SCHEDREADY,
1033 enum iax2_thread_type {
1034 IAX_THREAD_TYPE_POOL,
1035 IAX_THREAD_TYPE_DYNAMIC,
1038 struct iax2_pkt_buf {
1039 AST_LIST_ENTRY(iax2_pkt_buf) entry;
1041 unsigned char buf[1];
1044 struct iax2_thread {
1045 AST_LIST_ENTRY(iax2_thread) list;
1046 enum iax2_thread_type type;
1047 enum iax2_thread_iostate iostate;
1048 #ifdef SCHED_MULTITHREADED
1049 void (*schedfunc)(const void *);
1050 const void *scheddata;
1052 #ifdef DEBUG_SCHED_MULTITHREAD
1058 struct ast_sockaddr ioaddr;
1059 unsigned char readbuf[4096];
1067 ast_mutex_t init_lock;
1068 ast_cond_t init_cond;
1069 /*! if this thread is processing a full frame,
1070 some information about that frame will be stored
1071 here, so we can avoid dispatching any more full
1072 frames for that callno to other threads */
1074 unsigned short callno;
1075 struct ast_sockaddr addr;
1079 /*! Queued up full frames for processing. If more full frames arrive for
1080 * a call which this thread is already processing a full frame for, they
1081 * are queued up here. */
1082 AST_LIST_HEAD_NOLOCK(, iax2_pkt_buf) full_frames;
1087 static AST_LIST_HEAD_STATIC(idle_list, iax2_thread);
1088 static AST_LIST_HEAD_STATIC(active_list, iax2_thread);
1089 static AST_LIST_HEAD_STATIC(dynamic_list, iax2_thread);
1091 static void *iax2_process_thread(void *data);
1092 static void iax2_destroy(int callno);
1094 static void signal_condition(ast_mutex_t *lock, ast_cond_t *cond)
1096 ast_mutex_lock(lock);
1097 ast_cond_signal(cond);
1098 ast_mutex_unlock(lock);
1102 * \brief an array of iax2 pvt structures
1104 * The container for active chan_iax2_pvt structures is implemented as an
1105 * array for extremely quick direct access to the correct pvt structure
1106 * based on the local call number. The local call number is used as the
1107 * index into the array where the associated pvt structure is stored.
1109 static struct chan_iax2_pvt *iaxs[IAX_MAX_CALLS];
1111 static struct ast_callid *iax_pvt_callid_get(int callno)
1113 if (iaxs[callno]->callid) {
1114 return ast_callid_ref(iaxs[callno]->callid);
1119 static void iax_pvt_callid_set(int callno, struct ast_callid *callid)
1121 if (iaxs[callno]->callid) {
1122 ast_callid_unref(iaxs[callno]->callid);
1124 ast_callid_ref(callid);
1125 iaxs[callno]->callid = callid;
1128 static void iax_pvt_callid_new(int callno)
1130 struct ast_callid *callid = ast_create_callid();
1131 char buffer[AST_CALLID_BUFFER_LENGTH];
1132 ast_callid_strnprint(buffer, sizeof(buffer), callid);
1133 iax_pvt_callid_set(callno, callid);
1134 ast_callid_unref(callid);
1138 * \brief Another container of iax2_pvt structures
1140 * Active IAX2 pvt structs are also stored in this container, if they are a part
1141 * of an active call where we know the remote side's call number. The reason
1142 * for this is that incoming media frames do not contain our call number. So,
1143 * instead of having to iterate the entire iaxs array, we use this container to
1144 * look up calls where the remote side is using a given call number.
1146 static struct ao2_container *iax_peercallno_pvts;
1149 * \brief chan_iax2_pvt structure locks
1151 * These locks are used when accessing a pvt structure in the iaxs array.
1152 * The index used here is the same as used in the iaxs array. It is the
1153 * local call number for the associated pvt struct.
1155 static ast_mutex_t iaxsl[ARRAY_LEN(iaxs)];
1158 * * \brief Another container of iax2_pvt structures
1160 * Active IAX2 pvt stucts used during transfering a call are stored here.
1162 static struct ao2_container *iax_transfercallno_pvts;
1164 /* Flag to use with trunk calls, keeping these calls high up. It halves our effective use
1165 but keeps the division between trunked and non-trunked better. */
1166 #define TRUNK_CALL_START (IAX_MAX_CALLS / 2)
1168 /* Debug routines... */
1169 static struct ast_sockaddr debugaddr;
1171 static void iax_outputframe(struct iax_frame *f, struct ast_iax2_full_hdr *fhi, int rx, struct ast_sockaddr *addr, int datalen)
1174 (addr && !ast_sockaddr_isnull(&debugaddr) &&
1175 (!ast_sockaddr_port(&debugaddr) ||
1176 ast_sockaddr_port(&debugaddr) == ast_sockaddr_port(addr)) &&
1177 !ast_sockaddr_cmp_addr(&debugaddr, addr))) {
1180 iax_showframe(f, fhi, rx, addr, datalen);
1183 iax_showframe(f, fhi, rx, addr, datalen);
1189 static void iax_debug_output(const char *data)
1192 ast_verbose("%s", data);
1195 static void iax_error_output(const char *data)
1197 ast_log(LOG_WARNING, "%s", data);
1200 static void __attribute__((format(printf, 1, 2))) jb_error_output(const char *fmt, ...)
1205 va_start(args, fmt);
1206 vsnprintf(buf, sizeof(buf), fmt, args);
1209 ast_log(LOG_ERROR, "%s", buf);
1212 static void __attribute__((format(printf, 1, 2))) jb_warning_output(const char *fmt, ...)
1217 va_start(args, fmt);
1218 vsnprintf(buf, sizeof(buf), fmt, args);
1221 ast_log(LOG_WARNING, "%s", buf);
1224 static void __attribute__((format(printf, 1, 2))) jb_debug_output(const char *fmt, ...)
1229 va_start(args, fmt);
1230 vsnprintf(buf, sizeof(buf), fmt, args);
1233 ast_verbose("%s", buf);
1236 static int expire_registry(const void *data);
1237 static int iax2_answer(struct ast_channel *c);
1238 static int iax2_call(struct ast_channel *c, const char *dest, int timeout);
1239 static int iax2_devicestate(const char *data);
1240 static int iax2_digit_begin(struct ast_channel *c, char digit);
1241 static int iax2_digit_end(struct ast_channel *c, char digit, unsigned int duration);
1242 static int iax2_do_register(struct iax2_registry *reg);
1243 static int iax2_fixup(struct ast_channel *oldchannel, struct ast_channel *newchan);
1244 static int iax2_hangup(struct ast_channel *c);
1245 static int iax2_indicate(struct ast_channel *c, int condition, const void *data, size_t datalen);
1246 static int iax2_poke_peer(struct iax2_peer *peer, int heldcall);
1247 static int iax2_provision(struct ast_sockaddr *end, int sockfd, const char *dest, const char *template, int force);
1248 static int iax2_send(struct chan_iax2_pvt *pvt, struct ast_frame *f, unsigned int ts, int seqno, int now, int transfer, int final);
1249 static int iax2_sendhtml(struct ast_channel *c, int subclass, const char *data, int datalen);
1250 static int iax2_sendimage(struct ast_channel *c, struct ast_frame *img);
1251 static int iax2_sendtext(struct ast_channel *c, const char *text);
1252 static int iax2_setoption(struct ast_channel *c, int option, void *data, int datalen);
1253 static int iax2_queryoption(struct ast_channel *c, int option, void *data, int *datalen);
1254 static int iax2_transfer(struct ast_channel *c, const char *dest);
1255 static int iax2_write(struct ast_channel *c, struct ast_frame *f);
1256 static int iax2_sched_add(struct ast_sched_context *sched, int when, ast_sched_cb callback, const void *data);
1258 static int send_trunk(struct iax2_trunk_peer *tpeer, struct timeval *now);
1259 static int send_command(struct chan_iax2_pvt *, char, int, unsigned int, const unsigned char *, int, int);
1260 static int send_command_final(struct chan_iax2_pvt *, char, int, unsigned int, const unsigned char *, int, int);
1261 static int send_command_immediate(struct chan_iax2_pvt *, char, int, unsigned int, const unsigned char *, int, int);
1262 static int send_command_locked(unsigned short callno, char, int, unsigned int, const unsigned char *, int, int);
1263 static int send_command_transfer(struct chan_iax2_pvt *, char, int, unsigned int, const unsigned char *, int);
1264 static struct ast_channel *iax2_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);
1265 static struct ast_frame *iax2_read(struct ast_channel *c);
1266 static struct iax2_peer *build_peer(const char *name, struct ast_variable *v, struct ast_variable *alt, int temponly);
1267 static struct iax2_user *build_user(const char *name, struct ast_variable *v, struct ast_variable *alt, int temponly);
1268 static void realtime_update_peer(const char *peername, struct ast_sockaddr *sockaddr, time_t regtime);
1269 static void *iax2_dup_variable_datastore(void *);
1270 static void prune_peers(void);
1271 static void prune_users(void);
1272 static void iax2_free_variable_datastore(void *);
1274 static int acf_channel_read(struct ast_channel *chan, const char *funcname, char *preparse, char *buf, size_t buflen);
1275 static int decode_frame(ast_aes_decrypt_key *dcx, struct ast_iax2_full_hdr *fh, struct ast_frame *f, int *datalen);
1276 static int encrypt_frame(ast_aes_encrypt_key *ecx, struct ast_iax2_full_hdr *fh, unsigned char *poo, int *datalen);
1277 static void build_ecx_key(const unsigned char *digest, struct chan_iax2_pvt *pvt);
1278 static void build_rand_pad(unsigned char *buf, ssize_t len);
1279 static int get_unused_callno(enum callno_type type, int validated, callno_entry *entry);
1280 static int replace_callno(const void *obj);
1281 static void sched_delay_remove(struct ast_sockaddr *addr, callno_entry entry);
1282 static void network_change_stasis_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message);
1283 static void acl_change_stasis_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message);
1285 static struct ast_channel_tech iax2_tech = {
1287 .description = tdesc,
1288 .properties = AST_CHAN_TP_WANTSJITTER,
1289 .requester = iax2_request,
1290 .devicestate = iax2_devicestate,
1291 .send_digit_begin = iax2_digit_begin,
1292 .send_digit_end = iax2_digit_end,
1293 .send_text = iax2_sendtext,
1294 .send_image = iax2_sendimage,
1295 .send_html = iax2_sendhtml,
1297 .hangup = iax2_hangup,
1298 .answer = iax2_answer,
1300 .write = iax2_write,
1301 .write_video = iax2_write,
1302 .indicate = iax2_indicate,
1303 .setoption = iax2_setoption,
1304 .queryoption = iax2_queryoption,
1305 .transfer = iax2_transfer,
1306 .fixup = iax2_fixup,
1307 .func_channel_read = acf_channel_read,
1312 * \brief Obtain the owner channel lock if the owner exists.
1314 * \param callno IAX2 call id.
1316 * \note Assumes the iaxsl[callno] lock is already obtained.
1319 * IMPORTANT NOTE!!! Any time this function is used, even if
1320 * iaxs[callno] was valid before calling it, it may no longer be
1321 * valid after calling it. This function may unlock and lock
1322 * the mutex associated with this callno, meaning that another
1323 * thread may grab it and destroy the call.
1327 static void iax2_lock_owner(int callno)
1330 if (!iaxs[callno] || !iaxs[callno]->owner) {
1331 /* There is no owner lock to get. */
1334 if (!ast_channel_trylock(iaxs[callno]->owner)) {
1335 /* We got the lock */
1338 /* Avoid deadlock by pausing and trying again */
1339 DEADLOCK_AVOIDANCE(&iaxsl[callno]);
1345 * \brief Check if a control subtype is allowed on the wire.
1347 * \param subtype Control frame subtype to check if allowed to/from the wire.
1349 * \retval non-zero if allowed.
1351 static int iax2_is_control_frame_allowed(int subtype)
1353 enum ast_control_frame_type control = subtype;
1357 * Note: If we compare the enumeration type, which does not have any
1358 * negative constants, the compiler may optimize this code away.
1359 * Therefore, we must perform an integer comparison here.
1361 if (subtype == -1) {
1365 /* Default to not allowing control frames to pass. */
1369 * The switch default is not present in order to take advantage
1370 * of the compiler complaining of a missing enum case.
1374 * These control frames make sense to send/receive across the link.
1376 case AST_CONTROL_HANGUP:
1377 case AST_CONTROL_RING:
1378 case AST_CONTROL_RINGING:
1379 case AST_CONTROL_ANSWER:
1380 case AST_CONTROL_BUSY:
1381 case AST_CONTROL_TAKEOFFHOOK:
1382 case AST_CONTROL_OFFHOOK:
1383 case AST_CONTROL_CONGESTION:
1384 case AST_CONTROL_FLASH:
1385 case AST_CONTROL_WINK:
1386 case AST_CONTROL_OPTION:
1387 case AST_CONTROL_RADIO_KEY:
1388 case AST_CONTROL_RADIO_UNKEY:
1389 case AST_CONTROL_PROGRESS:
1390 case AST_CONTROL_PROCEEDING:
1391 case AST_CONTROL_HOLD:
1392 case AST_CONTROL_UNHOLD:
1393 case AST_CONTROL_VIDUPDATE:
1394 case AST_CONTROL_CONNECTED_LINE:
1395 case AST_CONTROL_REDIRECTING:
1396 case AST_CONTROL_T38_PARAMETERS:
1397 case AST_CONTROL_AOC:
1398 case AST_CONTROL_INCOMPLETE:
1399 case AST_CONTROL_MCID:
1404 * These control frames do not make sense to send/receive across the link.
1406 case _XXX_AST_CONTROL_T38:
1407 /* The control value is deprecated in favor of AST_CONTROL_T38_PARAMETERS. */
1408 case AST_CONTROL_SRCUPDATE:
1409 /* Across an IAX link the source is still the same. */
1410 case AST_CONTROL_TRANSFER:
1411 /* A success/fail status report from calling ast_transfer() on this machine. */
1412 case AST_CONTROL_CC:
1413 /* The payload contains pointers that are valid for the sending machine only. */
1414 case AST_CONTROL_SRCCHANGE:
1415 /* Across an IAX link the source is still the same. */
1416 case AST_CONTROL_READ_ACTION:
1417 /* The action can only be done by the sending machine. */
1418 case AST_CONTROL_END_OF_Q:
1419 /* This frame would cause the call to unexpectedly hangup. */
1420 case AST_CONTROL_UPDATE_RTP_PEER:
1421 /* Only meaningful across a bridge on this machine for direct-media exchange. */
1422 case AST_CONTROL_PVT_CAUSE_CODE:
1423 /* Intended only for the sending machine's local channel structure. */
1424 case AST_CONTROL_MASQUERADE_NOTIFY:
1425 /* Intended only for masquerades when calling ast_indicate_data(). */
1426 case AST_CONTROL_STREAM_STOP:
1427 case AST_CONTROL_STREAM_SUSPEND:
1428 case AST_CONTROL_STREAM_RESTART:
1429 case AST_CONTROL_STREAM_REVERSE:
1430 case AST_CONTROL_STREAM_FORWARD:
1431 /* None of these playback stream control frames should go across the link. */
1432 case AST_CONTROL_RECORD_CANCEL:
1433 case AST_CONTROL_RECORD_STOP:
1434 case AST_CONTROL_RECORD_SUSPEND:
1435 case AST_CONTROL_RECORD_MUTE:
1436 /* None of these media recording control frames should go across the link. */
1442 static void mwi_event_cb(void *userdata, struct stasis_subscription *sub, struct stasis_message *msg)
1444 /* The MWI subscriptions exist just so the core knows we care about those
1445 * mailboxes. However, we just grab the events out of the cache when it
1446 * is time to send MWI, since it is only sent with a REGACK. */
1449 static void network_change_stasis_subscribe(void)
1451 if (!network_change_sub) {
1452 network_change_sub = stasis_subscribe(ast_system_topic(),
1453 network_change_stasis_cb, NULL);
1457 static void network_change_stasis_unsubscribe(void)
1459 network_change_sub = stasis_unsubscribe_and_join(network_change_sub);
1462 static void acl_change_stasis_subscribe(void)
1464 if (!acl_change_sub) {
1465 acl_change_sub = stasis_subscribe(ast_security_topic(),
1466 acl_change_stasis_cb, NULL);
1470 static void acl_change_stasis_unsubscribe(void)
1472 acl_change_sub = stasis_unsubscribe_and_join(acl_change_sub);
1475 static int network_change_sched_cb(const void *data)
1477 struct iax2_registry *reg;
1478 network_change_sched_id = -1;
1479 AST_LIST_LOCK(®istrations);
1480 AST_LIST_TRAVERSE(®istrations, reg, entry) {
1481 iax2_do_register(reg);
1483 AST_LIST_UNLOCK(®istrations);
1488 static void network_change_stasis_cb(void *data, struct stasis_subscription *sub,
1489 struct stasis_message *message)
1491 /* This callback is only concerned with network change messages from the system topic. */
1492 if (stasis_message_type(message) != ast_network_change_type()) {
1496 ast_verb(1, "IAX, got a network change message, renewing all IAX registrations.\n");
1497 if (network_change_sched_id == -1) {
1498 network_change_sched_id = iax2_sched_add(sched, 1000, network_change_sched_cb, NULL);
1502 static void acl_change_stasis_cb(void *data, struct stasis_subscription *sub,
1503 struct stasis_message *message)
1505 if (stasis_message_type(message) != ast_named_acl_change_type()) {
1509 ast_log(LOG_NOTICE, "Reloading chan_iax2 in response to ACL change event.\n");
1513 static const struct ast_datastore_info iax2_variable_datastore_info = {
1514 .type = "IAX2_VARIABLE",
1515 .duplicate = iax2_dup_variable_datastore,
1516 .destroy = iax2_free_variable_datastore,
1519 static void *iax2_dup_variable_datastore(void *old)
1521 AST_LIST_HEAD(, ast_var_t) *oldlist = old, *newlist;
1522 struct ast_var_t *oldvar, *newvar;
1524 newlist = ast_calloc(sizeof(*newlist), 1);
1526 ast_log(LOG_ERROR, "Unable to duplicate iax2 variables\n");
1530 AST_LIST_HEAD_INIT(newlist);
1531 AST_LIST_LOCK(oldlist);
1532 AST_LIST_TRAVERSE(oldlist, oldvar, entries) {
1533 newvar = ast_var_assign(ast_var_name(oldvar), ast_var_value(oldvar));
1535 AST_LIST_INSERT_TAIL(newlist, newvar, entries);
1537 ast_log(LOG_ERROR, "Unable to duplicate iax2 variable '%s'\n", ast_var_name(oldvar));
1539 AST_LIST_UNLOCK(oldlist);
1543 static void iax2_free_variable_datastore(void *old)
1545 AST_LIST_HEAD(, ast_var_t) *oldlist = old;
1546 struct ast_var_t *oldvar;
1548 AST_LIST_LOCK(oldlist);
1549 while ((oldvar = AST_LIST_REMOVE_HEAD(oldlist, entries))) {
1552 AST_LIST_UNLOCK(oldlist);
1553 AST_LIST_HEAD_DESTROY(oldlist);
1558 /* WARNING: insert_idle_thread should only ever be called within the
1559 * context of an iax2_process_thread() thread.
1561 static void insert_idle_thread(struct iax2_thread *thread)
1563 if (thread->type == IAX_THREAD_TYPE_DYNAMIC) {
1564 AST_LIST_LOCK(&dynamic_list);
1565 AST_LIST_INSERT_TAIL(&dynamic_list, thread, list);
1566 AST_LIST_UNLOCK(&dynamic_list);
1568 AST_LIST_LOCK(&idle_list);
1569 AST_LIST_INSERT_TAIL(&idle_list, thread, list);
1570 AST_LIST_UNLOCK(&idle_list);
1576 static struct iax2_thread *find_idle_thread(void)
1578 struct iax2_thread *thread = NULL;
1580 /* Pop the head of the idle list off */
1581 AST_LIST_LOCK(&idle_list);
1582 thread = AST_LIST_REMOVE_HEAD(&idle_list, list);
1583 AST_LIST_UNLOCK(&idle_list);
1585 /* If we popped a thread off the idle list, just return it */
1587 memset(&thread->ffinfo, 0, sizeof(thread->ffinfo));
1591 /* Pop the head of the dynamic list off */
1592 AST_LIST_LOCK(&dynamic_list);
1593 thread = AST_LIST_REMOVE_HEAD(&dynamic_list, list);
1594 AST_LIST_UNLOCK(&dynamic_list);
1596 /* If we popped a thread off the dynamic list, just return it */
1598 memset(&thread->ffinfo, 0, sizeof(thread->ffinfo));
1602 /* If we can't create a new dynamic thread for any reason, return no thread at all */
1603 if (iaxdynamicthreadcount >= iaxmaxthreadcount || !(thread = ast_calloc(1, sizeof(*thread))))
1606 /* Set default values */
1607 ast_atomic_fetchadd_int(&iaxdynamicthreadcount, 1);
1608 thread->threadnum = ast_atomic_fetchadd_int(&iaxdynamicthreadnum, 1);
1609 thread->type = IAX_THREAD_TYPE_DYNAMIC;
1611 /* Initialize lock and condition */
1612 ast_mutex_init(&thread->lock);
1613 ast_cond_init(&thread->cond, NULL);
1614 ast_mutex_init(&thread->init_lock);
1615 ast_cond_init(&thread->init_cond, NULL);
1616 ast_mutex_lock(&thread->init_lock);
1618 /* Create thread and send it on it's way */
1619 if (ast_pthread_create_background(&thread->threadid, NULL, iax2_process_thread, thread)) {
1620 ast_cond_destroy(&thread->cond);
1621 ast_mutex_destroy(&thread->lock);
1622 ast_mutex_unlock(&thread->init_lock);
1623 ast_cond_destroy(&thread->init_cond);
1624 ast_mutex_destroy(&thread->init_lock);
1629 /* this thread is not processing a full frame (since it is idle),
1630 so ensure that the field for the full frame call number is empty */
1631 memset(&thread->ffinfo, 0, sizeof(thread->ffinfo));
1633 /* Wait for the thread to be ready before returning it to the caller */
1634 ast_cond_wait(&thread->init_cond, &thread->init_lock);
1636 /* Done with init_lock */
1637 ast_mutex_unlock(&thread->init_lock);
1642 #ifdef SCHED_MULTITHREADED
1643 static int __schedule_action(void (*func)(const void *data), const void *data, const char *funcname)
1645 struct iax2_thread *thread;
1646 static time_t lasterror;
1649 thread = find_idle_thread();
1650 if (thread != NULL) {
1651 thread->schedfunc = func;
1652 thread->scheddata = data;
1653 thread->iostate = IAX_IOSTATE_SCHEDREADY;
1654 #ifdef DEBUG_SCHED_MULTITHREAD
1655 ast_copy_string(thread->curfunc, funcname, sizeof(thread->curfunc));
1657 signal_condition(&thread->lock, &thread->cond);
1661 if (t != lasterror) {
1663 ast_debug(1, "Out of idle IAX2 threads for scheduling! (%s)\n", funcname);
1668 #define schedule_action(func, data) __schedule_action(func, data, __PRETTY_FUNCTION__)
1671 static int iax2_sched_replace(int id, struct ast_sched_context *con, int when,
1672 ast_sched_cb callback, const void *data)
1674 return ast_sched_replace(id, con, when, callback, data);
1677 static int iax2_sched_add(struct ast_sched_context *con, int when,
1678 ast_sched_cb callback, const void *data)
1680 return ast_sched_add(con, when, callback, data);
1683 static int send_ping(const void *data);
1685 static void __send_ping(const void *data)
1687 int callno = (long) data;
1689 ast_mutex_lock(&iaxsl[callno]);
1692 if (iaxs[callno]->peercallno) {
1693 send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_PING, 0, NULL, 0, -1);
1694 if (iaxs[callno]->pingid != DONT_RESCHEDULE) {
1695 iaxs[callno]->pingid = iax2_sched_add(sched, ping_time * 1000, send_ping, data);
1699 ast_debug(1, "I was supposed to send a PING with callno %d, but no such call exists.\n", callno);
1702 ast_mutex_unlock(&iaxsl[callno]);
1705 static int send_ping(const void *data)
1707 int callno = (long) data;
1708 ast_mutex_lock(&iaxsl[callno]);
1709 if (iaxs[callno] && iaxs[callno]->pingid != DONT_RESCHEDULE) {
1710 iaxs[callno]->pingid = -1;
1712 ast_mutex_unlock(&iaxsl[callno]);
1714 #ifdef SCHED_MULTITHREADED
1715 if (schedule_action(__send_ping, data))
1722 static void encmethods_to_str(int e, struct ast_str **buf)
1724 ast_str_set(buf, 0, "(");
1725 if (e & IAX_ENCRYPT_AES128) {
1726 ast_str_append(buf, 0, "aes128");
1728 if (e & IAX_ENCRYPT_KEYROTATE) {
1729 ast_str_append(buf, 0, ",keyrotate");
1731 if (ast_str_strlen(*buf) > 1) {
1732 ast_str_append(buf, 0, ")");
1734 ast_str_set(buf, 0, "No");
1738 static int get_encrypt_methods(const char *s)
1741 if (!strcasecmp(s, "aes128"))
1742 e = IAX_ENCRYPT_AES128 | IAX_ENCRYPT_KEYROTATE;
1743 else if (ast_true(s))
1744 e = IAX_ENCRYPT_AES128 | IAX_ENCRYPT_KEYROTATE;
1750 static int send_lagrq(const void *data);
1752 static void __send_lagrq(const void *data)
1754 int callno = (long) data;
1756 ast_mutex_lock(&iaxsl[callno]);
1759 if (iaxs[callno]->peercallno) {
1760 send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_LAGRQ, 0, NULL, 0, -1);
1761 if (iaxs[callno]->lagid != DONT_RESCHEDULE) {
1762 iaxs[callno]->lagid = iax2_sched_add(sched, lagrq_time * 1000, send_lagrq, data);
1766 ast_debug(1, "I was supposed to send a LAGRQ with callno %d, but no such call exists.\n", callno);
1769 ast_mutex_unlock(&iaxsl[callno]);
1772 static int send_lagrq(const void *data)
1774 int callno = (long) data;
1775 ast_mutex_lock(&iaxsl[callno]);
1776 if (iaxs[callno] && iaxs[callno]->lagid != DONT_RESCHEDULE) {
1777 iaxs[callno]->lagid = -1;
1779 ast_mutex_unlock(&iaxsl[callno]);
1781 #ifdef SCHED_MULTITHREADED
1782 if (schedule_action(__send_lagrq, data))
1788 static unsigned char compress_subclass(iax2_format subclass)
1792 /* If it's 64 or smaller, just return it */
1793 if (subclass < IAX_FLAG_SC_LOG)
1795 /* Otherwise find its power */
1796 for (x = 0; x < IAX_MAX_SHIFT; x++) {
1797 if (subclass & (1LL << x)) {
1799 ast_log(LOG_WARNING, "Can't compress subclass %lld\n", (long long) subclass);
1805 return power | IAX_FLAG_SC_LOG;
1808 static iax2_format uncompress_subclass(unsigned char csub)
1810 /* If the SC_LOG flag is set, return 2^csub otherwise csub */
1811 if (csub & IAX_FLAG_SC_LOG) {
1812 /* special case for 'compressed' -1 */
1816 return 1LL << (csub & ~IAX_FLAG_SC_LOG & IAX_MAX_SHIFT);
1822 static struct ast_format *codec_choose_from_prefs(struct iax2_codec_pref *pref, struct ast_format_cap *cap)
1825 struct ast_format *found_format = NULL;
1827 for (x = 0; x < ARRAY_LEN(pref->order); ++x) {
1828 struct ast_format *pref_format;
1829 uint64_t pref_bitfield;
1831 pref_bitfield = iax2_codec_pref_order_value_to_format_bitfield(pref->order[x]);
1832 if (!pref_bitfield) {
1836 pref_format = ast_format_compatibility_bitfield2format(pref_bitfield);
1838 /* The bitfield is not associated with any format. */
1841 found_format = ast_format_cap_get_compatible_format(cap, pref_format);
1847 if (found_format && (ast_format_get_type(found_format) == AST_MEDIA_TYPE_AUDIO)) {
1848 return found_format;
1851 ast_debug(4, "Could not find preferred codec - Returning zero codec.\n");
1852 ao2_cleanup(found_format);
1856 static iax2_format iax2_codec_choose(struct iax2_codec_pref *pref, iax2_format formats)
1858 struct ast_format_cap *cap;
1859 struct ast_format *tmpfmt;
1860 iax2_format format = 0;
1862 if ((cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
1863 iax2_format_compatibility_bitfield2cap(formats, cap);
1864 tmpfmt = codec_choose_from_prefs(pref, cap);
1870 format = ast_format_compatibility_format2bitfield(tmpfmt);
1871 ao2_ref(tmpfmt, -1);
1878 const char *iax2_getformatname(iax2_format format)
1880 struct ast_format *tmpfmt;
1882 tmpfmt = ast_format_compatibility_bitfield2format(format);
1887 return ast_format_get_name(tmpfmt);
1890 static const char *iax2_getformatname_multiple(iax2_format format, struct ast_str **codec_buf)
1892 struct ast_format_cap *cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
1897 iax2_format_compatibility_bitfield2cap(format, cap);
1898 ast_format_cap_get_names(cap, codec_buf);
1901 return ast_str_buffer(*codec_buf);
1904 static int iax2_parse_allow_disallow(struct iax2_codec_pref *pref, iax2_format *formats, const char *list, int allowing)
1907 struct ast_format_cap *cap;
1909 /* We want to add the formats to the cap in the preferred order */
1910 cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
1911 if (!cap || iax2_codec_pref_to_cap(pref, cap)) {
1916 res = ast_format_cap_update_by_allow_disallow(cap, list, allowing);
1918 /* Adjust formats bitfield and pref list to match. */
1919 *formats = iax2_format_compatibility_cap2bitfield(cap);
1920 iax2_codec_pref_remove_missing(pref, *formats);
1922 for (i = 0; i < ast_format_cap_count(cap); i++) {
1923 struct ast_format *fmt = ast_format_cap_get_format(cap, i);
1925 iax2_codec_pref_append(pref, fmt, ast_format_cap_get_format_framing(cap, fmt));
1934 static int iax2_data_add_codecs(struct ast_data *root, const char *node_name, iax2_format formats)
1937 struct ast_format_cap *cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
1941 iax2_format_compatibility_bitfield2cap(formats, cap);
1942 res = ast_data_add_codecs(root, node_name, cap);
1948 * \note The only member of the peer passed here guaranteed to be set is the name field
1950 static int peer_hash_cb(const void *obj, const int flags)
1952 const struct iax2_peer *peer = obj;
1953 const char *name = obj;
1955 return ast_str_hash(flags & OBJ_KEY ? name : peer->name);
1959 * \note The only member of the peer passed here guaranteed to be set is the name field
1961 static int peer_cmp_cb(void *obj, void *arg, int flags)
1963 struct iax2_peer *peer = obj, *peer2 = arg;
1964 const char *name = arg;
1966 return !strcmp(peer->name, flags & OBJ_KEY ? name : peer2->name) ?
1967 CMP_MATCH | CMP_STOP : 0;
1971 * \note The only member of the user passed here guaranteed to be set is the name field
1973 static int user_hash_cb(const void *obj, const int flags)
1975 const struct iax2_user *user = obj;
1976 const char *name = obj;
1978 return ast_str_hash(flags & OBJ_KEY ? name : user->name);
1982 * \note The only member of the user passed here guaranteed to be set is the name field
1984 static int user_cmp_cb(void *obj, void *arg, int flags)
1986 struct iax2_user *user = obj, *user2 = arg;
1987 const char *name = arg;
1989 return !strcmp(user->name, flags & OBJ_KEY ? name : user2->name) ?
1990 CMP_MATCH | CMP_STOP : 0;
1994 * \note This funtion calls realtime_peer -> reg_source_db -> iax2_poke_peer -> find_callno,
1995 * so do not call it with a pvt lock held.
1997 static struct iax2_peer *find_peer(const char *name, int realtime)
1999 struct iax2_peer *peer = NULL;
2001 peer = ao2_find(peers, name, OBJ_KEY);
2003 /* Now go for realtime if applicable */
2004 if (!peer && realtime) {
2005 peer = realtime_peer(name, NULL);
2010 static struct iax2_peer *peer_ref(struct iax2_peer *peer)
2016 static inline struct iax2_peer *peer_unref(struct iax2_peer *peer)
2022 static struct iax2_user *find_user(const char *name)
2024 return ao2_find(users, name, OBJ_KEY);
2026 static inline struct iax2_user *user_ref(struct iax2_user *user)
2032 static inline struct iax2_user *user_unref(struct iax2_user *user)
2038 static int iax2_getpeername(struct ast_sockaddr addr, char *host, int len)
2040 struct iax2_peer *peer = NULL;
2042 struct ao2_iterator i;
2044 i = ao2_iterator_init(peers, 0);
2045 while ((peer = ao2_iterator_next(&i))) {
2047 if (!ast_sockaddr_cmp(&peer->addr, &addr)) {
2048 ast_copy_string(host, peer->name, len);
2055 ao2_iterator_destroy(&i);
2058 peer = realtime_peer(NULL, &addr);
2060 ast_copy_string(host, peer->name, len);
2069 /*!\note Assumes the lock on the pvt is already held, when
2070 * iax2_destroy_helper() is called. */
2071 static void iax2_destroy_helper(struct chan_iax2_pvt *pvt)
2073 /* Decrement AUTHREQ count if needed */
2074 if (ast_test_flag64(pvt, IAX_MAXAUTHREQ)) {
2075 struct iax2_user *user;
2077 user = ao2_find(users, pvt->username, OBJ_KEY);
2079 ast_atomic_fetchadd_int(&user->curauthreq, -1);
2083 ast_clear_flag64(pvt, IAX_MAXAUTHREQ);
2085 /* No more pings or lagrq's */
2086 AST_SCHED_DEL_SPINLOCK(sched, pvt->pingid, &iaxsl[pvt->callno]);
2087 pvt->pingid = DONT_RESCHEDULE;
2088 AST_SCHED_DEL_SPINLOCK(sched, pvt->lagid, &iaxsl[pvt->callno]);
2089 pvt->lagid = DONT_RESCHEDULE;
2090 AST_SCHED_DEL(sched, pvt->autoid);
2091 AST_SCHED_DEL(sched, pvt->authid);
2092 AST_SCHED_DEL(sched, pvt->initid);
2093 AST_SCHED_DEL(sched, pvt->jbid);
2094 AST_SCHED_DEL(sched, pvt->keyrotateid);
2097 static void iax2_frame_free(struct iax_frame *fr)
2099 AST_SCHED_DEL(sched, fr->retrans);
2103 static int scheduled_destroy(const void *vid)
2105 unsigned short callno = PTR_TO_CALLNO(vid);
2106 ast_mutex_lock(&iaxsl[callno]);
2108 ast_debug(1, "Really destroying %d now...\n", callno);
2109 iax2_destroy(callno);
2111 ast_mutex_unlock(&iaxsl[callno]);
2115 static void free_signaling_queue_entry(struct signaling_queue_entry *s)
2118 ast_free(s->f.data.ptr);
2123 /*! \brief This function must be called once we are sure the other side has
2124 * given us a call number. All signaling is held here until that point. */
2125 static void send_signaling(struct chan_iax2_pvt *pvt)
2127 struct signaling_queue_entry *s = NULL;
2129 while ((s = AST_LIST_REMOVE_HEAD(&pvt->signaling_queue, next))) {
2130 iax2_send(pvt, &s->f, 0, -1, 0, 0, 0);
2131 free_signaling_queue_entry(s);
2133 pvt->hold_signaling = 0;
2136 /*! \brief All frames other than that of type AST_FRAME_IAX must be held until
2137 * we have received a destination call number. */
2138 static int queue_signalling(struct chan_iax2_pvt *pvt, struct ast_frame *f)
2140 struct signaling_queue_entry *qe;
2142 if (f->frametype == AST_FRAME_IAX || !pvt->hold_signaling) {
2143 return 1; /* do not queue this frame */
2144 } else if (!(qe = ast_calloc(1, sizeof(struct signaling_queue_entry)))) {
2145 return -1; /* out of memory */
2148 /* copy ast_frame into our queue entry */
2150 if (qe->f.datalen) {
2151 /* if there is data in this frame copy it over as well */
2152 if (!(qe->f.data.ptr = ast_malloc(qe->f.datalen))) {
2153 free_signaling_queue_entry(qe);
2156 memcpy(qe->f.data.ptr, f->data.ptr, qe->f.datalen);
2158 AST_LIST_INSERT_TAIL(&pvt->signaling_queue, qe, next);
2163 static void pvt_destructor(void *obj)
2165 struct chan_iax2_pvt *pvt = obj;
2166 struct iax_frame *cur = NULL;
2167 struct signaling_queue_entry *s = NULL;
2169 ast_mutex_lock(&iaxsl[pvt->callno]);
2171 iax2_destroy_helper(pvt);
2173 sched_delay_remove(&pvt->addr, pvt->callno_entry);
2174 pvt->callno_entry = 0;
2177 ast_set_flag64(pvt, IAX_ALREADYGONE);
2179 AST_LIST_TRAVERSE(&frame_queue[pvt->callno], cur, list) {
2180 /* Cancel any pending transmissions */
2184 ast_mutex_unlock(&iaxsl[pvt->callno]);
2186 while ((s = AST_LIST_REMOVE_HEAD(&pvt->signaling_queue, next))) {
2187 free_signaling_queue_entry(s);
2191 pvt->reg->callno = 0;
2197 ast_variables_destroy(pvt->vars);
2201 while (jb_getall(pvt->jb, &frame) == JB_OK) {
2202 iax2_frame_free(frame.data);
2205 jb_destroy(pvt->jb);
2206 ast_string_field_free_memory(pvt);
2210 ast_callid_unref(pvt->callid);
2215 static struct chan_iax2_pvt *new_iax(struct ast_sockaddr *addr, const char *host)
2217 struct chan_iax2_pvt *tmp;
2220 if (!(tmp = ao2_alloc(sizeof(*tmp), pvt_destructor))) {
2224 if (ast_string_field_init(tmp, 32)) {
2230 tmp->prefs = prefs_global;
2236 tmp->keyrotateid = -1;
2238 ast_string_field_set(tmp,exten, "s");
2239 ast_string_field_set(tmp,host, host);
2243 jbconf.max_jitterbuf = maxjitterbuffer;
2244 jbconf.resync_threshold = resyncthreshold;
2245 jbconf.max_contig_interp = maxjitterinterps;
2246 jbconf.target_extra = jittertargetextra;
2247 jb_setconf(tmp->jb,&jbconf);
2249 AST_LIST_HEAD_INIT_NOLOCK(&tmp->dpentries);
2251 tmp->hold_signaling = 1;
2252 AST_LIST_HEAD_INIT_NOLOCK(&tmp->signaling_queue);
2257 static struct iax_frame *iaxfrdup2(struct iax_frame *fr)
2259 struct iax_frame *new = iax_frame_new(DIRECTION_INGRESS, fr->af.datalen, fr->cacheable);
2261 size_t afdatalen = new->afdatalen;
2262 memcpy(new, fr, sizeof(*new));
2263 iax_frame_wrap(new, &fr->af);
2264 new->afdatalen = afdatalen;
2267 new->direction = DIRECTION_INGRESS;
2272 /* keep these defined in this order. They are used in find_callno to
2273 * determine whether or not a new call number should be allowed. */
2275 /* do not allow a new call number, only search ones in use for match */
2277 /* search for match first, then allow a new one to be allocated */
2279 /* do not search for match, force a new call number */
2281 /* do not search for match, force a new call number. Signifies call number
2282 * has been calltoken validated */
2283 NEW_ALLOW_CALLTOKEN_VALIDATED = 3,
2286 static int match(struct ast_sockaddr *addr, unsigned short callno, unsigned short dcallno, const struct chan_iax2_pvt *cur, int check_dcallno)
2288 if (!ast_sockaddr_cmp(&cur->addr, addr)) {
2289 /* This is the main host */
2290 if ( (cur->peercallno == 0 || cur->peercallno == callno) &&
2291 (check_dcallno ? dcallno == cur->callno : 1) ) {
2292 /* That's us. Be sure we keep track of the peer call number */
2296 if (!ast_sockaddr_cmp(&cur->transfer, addr) && cur->transferring) {
2297 /* We're transferring */
2298 if ((dcallno == cur->callno) || (cur->transferring == TRANSFER_MEDIAPASS && cur->transfercallno == callno))
2304 static int make_trunk(unsigned short callno, int locked)
2309 if (iaxs[callno]->oseqno) {
2310 ast_log(LOG_WARNING, "Can't make trunk once a call has started!\n");
2313 if (callno >= TRUNK_CALL_START) {
2314 ast_log(LOG_WARNING, "Call %d is already a trunk\n", callno);
2318 if (get_unused_callno(
2320 CALLNO_ENTRY_IS_VALIDATED(iaxs[callno]->callno_entry),
2322 ast_log(LOG_WARNING, "Unable to trunk call: Insufficient space\n");
2326 x = CALLNO_ENTRY_GET_CALLNO(entry);
2327 ast_mutex_lock(&iaxsl[x]);
2330 * \note We delete these before switching the slot, because if
2331 * they fire in the meantime, they will generate a warning.
2333 AST_SCHED_DEL(sched, iaxs[callno]->pingid);
2334 AST_SCHED_DEL(sched, iaxs[callno]->lagid);
2335 iaxs[callno]->lagid = iaxs[callno]->pingid = -1;
2336 iaxs[x] = iaxs[callno];
2337 iaxs[x]->callno = x;
2339 /* since we copied over the pvt from a different callno, make sure the old entry is replaced
2340 * before assigning the new one */
2341 if (iaxs[x]->callno_entry) {
2344 MIN_REUSE_TIME * 1000,
2346 CALLNO_ENTRY_TO_PTR(iaxs[x]->callno_entry));
2349 iaxs[x]->callno_entry = entry;
2351 iaxs[callno] = NULL;
2352 /* Update the two timers that should have been started */
2353 iaxs[x]->pingid = iax2_sched_add(sched,
2354 ping_time * 1000, send_ping, (void *)(long)x);
2355 iaxs[x]->lagid = iax2_sched_add(sched,
2356 lagrq_time * 1000, send_lagrq, (void *)(long)x);
2359 ast_mutex_unlock(&iaxsl[callno]);
2362 ast_mutex_unlock(&iaxsl[x]);
2364 /* We moved this call from a non-trunked to a trunked call */
2365 ast_debug(1, "Made call %d into trunk call %d\n", callno, x);
2370 static void store_by_transfercallno(struct chan_iax2_pvt *pvt)
2372 if (!pvt->transfercallno) {
2373 ast_log(LOG_ERROR, "This should not be called without a transfer call number.\n");
2377 ao2_link(iax_transfercallno_pvts, pvt);
2380 static void remove_by_transfercallno(struct chan_iax2_pvt *pvt)
2382 if (!pvt->transfercallno) {
2383 ast_log(LOG_ERROR, "This should not be called without a transfer call number.\n");
2387 ao2_unlink(iax_transfercallno_pvts, pvt);
2389 static void store_by_peercallno(struct chan_iax2_pvt *pvt)
2391 if (!pvt->peercallno) {
2392 ast_log(LOG_ERROR, "This should not be called without a peer call number.\n");
2396 ao2_link(iax_peercallno_pvts, pvt);
2399 static void remove_by_peercallno(struct chan_iax2_pvt *pvt)
2401 if (!pvt->peercallno) {
2402 ast_log(LOG_ERROR, "This should not be called without a peer call number.\n");
2406 ao2_unlink(iax_peercallno_pvts, pvt);
2409 static int addr_range_delme_cb(void *obj, void *arg, int flags)
2411 struct addr_range *lim = obj;
2416 static int addr_range_hash_cb(const void *obj, const int flags)
2418 const struct addr_range *lim = obj;
2419 return abs(ast_sockaddr_hash(&lim->ha.addr));
2422 static int addr_range_cmp_cb(void *obj, void *arg, int flags)
2424 struct addr_range *lim1 = obj, *lim2 = arg;
2425 return (!(ast_sockaddr_cmp_addr(&lim1->ha.addr, &lim2->ha.addr)) &&
2426 !(ast_sockaddr_cmp_addr(&lim1->ha.netmask, &lim2->ha.netmask))) ?
2427 CMP_MATCH | CMP_STOP : 0;
2430 static int peercnt_hash_cb(const void *obj, const int flags)
2432 const struct peercnt *peercnt = obj;
2434 if (ast_sockaddr_isnull(&peercnt->addr)) {
2437 return ast_sockaddr_hash(&peercnt->addr);
2440 static int peercnt_cmp_cb(void *obj, void *arg, int flags)
2442 struct peercnt *peercnt1 = obj, *peercnt2 = arg;
2443 return !ast_sockaddr_cmp_addr(&peercnt1->addr, &peercnt2->addr) ? CMP_MATCH | CMP_STOP : 0;
2446 static int addr_range_match_address_cb(void *obj, void *arg, int flags)
2448 struct addr_range *addr_range = obj;
2449 struct ast_sockaddr *addr = arg;
2450 struct ast_sockaddr tmp_addr;
2452 ast_sockaddr_apply_netmask(addr, &addr_range->ha.netmask, &tmp_addr);
2454 if (!ast_sockaddr_cmp_addr(&tmp_addr, &addr_range->ha.addr)) {
2455 return CMP_MATCH | CMP_STOP;
2463 * \brief compares addr to calltoken_ignores table to determine if validation is required.
2465 static int calltoken_required(struct ast_sockaddr *addr, const char *name, int subclass)
2467 struct addr_range *addr_range;
2468 struct iax2_peer *peer = NULL;
2469 struct iax2_user *user = NULL;
2470 /* if no username is given, check for guest accounts */
2471 const char *find = S_OR(name, "guest");
2472 int res = 1; /* required by default */
2474 enum calltoken_peer_enum calltoken_required = CALLTOKEN_DEFAULT;
2475 /* There are only two cases in which calltoken validation is not required.
2476 * Case 1. sin falls within the list of address ranges specified in the calltoken optional table and
2477 * the peer definition has not set the requirecalltoken option.
2478 * Case 2. Username is a valid peer/user, and that peer has requirecalltoken set either auto or no.
2481 /* ----- Case 1 ----- */
2482 if ((addr_range = ao2_callback(calltoken_ignores, 0, addr_range_match_address_cb, addr))) {
2483 ao2_ref(addr_range, -1);
2487 /* ----- Case 2 ----- */
2488 if ((subclass == IAX_COMMAND_NEW) && (user = find_user(find))) {
2489 calltoken_required = user->calltoken_required;
2490 } else if ((subclass == IAX_COMMAND_NEW) && (user = realtime_user(find, addr))) {
2491 calltoken_required = user->calltoken_required;
2492 } else if ((subclass != IAX_COMMAND_NEW) && (peer = find_peer(find, 0))) {
2493 calltoken_required = peer->calltoken_required;
2494 } else if ((subclass != IAX_COMMAND_NEW) && (peer = realtime_peer(find, addr))) {
2495 calltoken_required = peer->calltoken_required;
2505 ast_debug(1, "Determining if address %s with username %s requires calltoken validation. Optional = %d calltoken_required = %u \n", ast_sockaddr_stringify_addr(addr), name, optional, calltoken_required);
2506 if (((calltoken_required == CALLTOKEN_NO) || (calltoken_required == CALLTOKEN_AUTO)) ||
2507 (optional && (calltoken_required == CALLTOKEN_DEFAULT))) {
2517 * \brief set peercnt callno limit.
2520 * First looks in custom definitions. If not found, global limit
2521 * is used. Entries marked as reg already have
2522 * a custom limit set by a registration and are not modified.
2524 static void set_peercnt_limit(struct peercnt *peercnt)
2526 uint16_t limit = global_maxcallno;
2527 struct addr_range *addr_range;
2528 struct ast_sockaddr addr;
2530 ast_sockaddr_copy(&addr, &peercnt->addr);
2532 if (peercnt->reg && peercnt->limit) {
2533 return; /* this peercnt has a custom limit set by a registration */
2536 if ((addr_range = ao2_callback(callno_limits, 0, addr_range_match_address_cb, &addr))) {
2537 limit = addr_range->limit;
2538 ast_debug(1, "custom addr_range %d found for %s\n", limit, ast_sockaddr_stringify(&addr));
2539 ao2_ref(addr_range, -1);
2542 peercnt->limit = limit;
2547 * \brief sets limits for all peercnts in table. done on reload to reflect changes in conf.
2549 static int set_peercnt_limit_all_cb(void *obj, void *arg, int flags)
2551 struct peercnt *peercnt = obj;
2553 set_peercnt_limit(peercnt);
2554 ast_debug(1, "Reset limits for peercnts table\n");
2561 * \brief returns match if delme is set.
2563 static int prune_addr_range_cb(void *obj, void *arg, int flags)
2565 struct addr_range *addr_range = obj;
2567 return addr_range->delme ? CMP_MATCH : 0;
2572 * \brief modifies peercnt entry in peercnts table. Used to set custom limit or mark a registered ip
2574 static void peercnt_modify(unsigned char reg, uint16_t limit, struct ast_sockaddr *sockaddr)
2576 /* this function turns off and on custom callno limits set by peer registration */
2577 struct peercnt *peercnt;
2580 ast_sockaddr_copy(&tmp.addr, sockaddr);
2582 if ((peercnt = ao2_find(peercnts, &tmp, OBJ_POINTER))) {
2585 peercnt->limit = limit;
2587 set_peercnt_limit(peercnt);
2589 ast_debug(1, "peercnt entry %s modified limit:%d registered:%d", ast_sockaddr_stringify_addr(sockaddr), peercnt->limit, peercnt->reg);
2590 ao2_ref(peercnt, -1); /* decrement ref from find */
2596 * \brief adds an ip to the peercnts table, increments connection count if it already exists
2598 * \details First searches for the address in the peercnts table. If found
2599 * the current count is incremented. If not found a new peercnt is allocated
2600 * and linked into the peercnts table with a call number count of 1.
2602 static int peercnt_add(struct ast_sockaddr *addr)
2604 struct peercnt *peercnt;
2608 ast_sockaddr_copy(&tmp.addr, addr);
2610 /* Reasoning for peercnts container lock: Two identical ip addresses
2611 * could be added by different threads at the "same time". Without the container
2612 * lock, both threads could alloc space for the same object and attempt
2613 * to link to table. With the lock, one would create the object and link
2614 * to table while the other would find the already created peercnt object
2615 * rather than creating a new one. */
2617 if ((peercnt = ao2_find(peercnts, &tmp, OBJ_POINTER))) {
2619 } else if ((peercnt = ao2_alloc(sizeof(*peercnt), NULL))) {
2621 /* create and set defaults */
2622 ast_sockaddr_copy(&peercnt->addr, addr);
2623 set_peercnt_limit(peercnt);
2624 /* guarantees it does not go away after unlocking table
2625 * ao2_find automatically adds this */
2626 ao2_link(peercnts, peercnt);
2628 ao2_unlock(peercnts);
2632 /* check to see if the address has hit its callno limit. If not increment cur. */
2633 if (peercnt->limit > peercnt->cur) {
2635 ast_debug(1, "ip callno count incremented to %d for %s\n", peercnt->cur, ast_sockaddr_stringify_addr(addr));
2636 } else { /* max num call numbers for this peer has been reached! */
2637 ast_log(LOG_ERROR, "maxcallnumber limit of %d for %s has been reached!\n", peercnt->limit, ast_sockaddr_stringify_addr(addr));
2641 /* clean up locks and ref count */
2642 ao2_unlock(peercnt);
2643 ao2_unlock(peercnts);
2644 ao2_ref(peercnt, -1); /* decrement ref from find/alloc, only the container ref remains. */
2651 * \brief decrements a peercnts table entry
2653 static void peercnt_remove(struct peercnt *peercnt)
2655 struct ast_sockaddr addr;
2657 ast_sockaddr_copy(&addr, &peercnt->addr);
2660 * Container locked here since peercnt may be unlinked from
2661 * list. If left unlocked, peercnt_add could try and grab this
2662 * entry from the table and modify it at the "same time" this
2663 * thread attemps to unlink it.
2667 ast_debug(1, "ip callno count decremented to %d for %s\n", peercnt->cur, ast_sockaddr_stringify_addr(&addr));
2668 /* if this was the last connection from the peer remove it from table */
2669 if (peercnt->cur == 0) {
2670 ao2_unlink(peercnts, peercnt);/* decrements ref from table, last ref is left to scheduler */
2672 ao2_unlock(peercnts);
2677 * \brief called by scheduler to decrement object
2679 static int peercnt_remove_cb(const void *obj)
2681 struct peercnt *peercnt = (struct peercnt *) obj;
2683 peercnt_remove(peercnt);
2684 ao2_ref(peercnt, -1); /* decrement ref from scheduler */
2691 * \brief decrements peercnts connection count, finds by addr
2693 static int peercnt_remove_by_addr(struct ast_sockaddr *addr)
2695 struct peercnt *peercnt;
2698 ast_sockaddr_copy(&tmp.addr, addr);
2700 if ((peercnt = ao2_find(peercnts, &tmp, OBJ_POINTER))) {
2701 peercnt_remove(peercnt);
2702 ao2_ref(peercnt, -1); /* decrement ref from find */
2709 * \brief Create callno_limit entry based on configuration
2711 static void build_callno_limits(struct ast_variable *v)
2713 struct addr_range *addr_range = NULL;
2714 struct addr_range tmp;
2720 for (; v; v = v->next) {
2724 ha = ast_append_ha("permit", v->name, NULL, &error);
2726 /* check for valid config information */
2728 ast_log(LOG_ERROR, "Call number limit for %s could not be added, Invalid address range\n.", v->name);
2730 } else if ((sscanf(v->value, "%d", &limit) != 1) || (limit < 0)) {
2731 ast_log(LOG_ERROR, "Call number limit for %s could not be added. Invalid limit %s\n.", v->name, v->value);
2736 ast_copy_ha(ha, &tmp.ha);
2737 /* find or create the addr_range */
2738 if ((addr_range = ao2_find(callno_limits, &tmp, OBJ_POINTER))) {
2739 ao2_lock(addr_range);
2741 } else if (!(addr_range = ao2_alloc(sizeof(*addr_range), NULL))) {
2743 return; /* out of memory */
2746 /* copy over config data into addr_range object */
2747 ast_copy_ha(ha, &addr_range->ha); /* this is safe because only one ha is possible for each limit */
2748 ast_free_ha(ha); /* cleanup the tmp ha */
2749 addr_range->limit = limit;
2750 addr_range->delme = 0;
2754 ao2_unlock(addr_range);
2756 ao2_link(callno_limits, addr_range);
2758 ao2_ref(addr_range, -1); /* decrement ref from ao2_find and ao2_alloc, only container ref remains */
2764 * \brief Create calltoken_ignores entry based on configuration
2766 static int add_calltoken_ignore(const char *addr)
2768 struct addr_range tmp;
2769 struct addr_range *addr_range = NULL;
2770 struct ast_ha *ha = NULL;
2773 if (ast_strlen_zero(addr)) {
2774 ast_log(LOG_WARNING, "invalid calltokenoptional %s\n", addr);
2778 ha = ast_append_ha("permit", addr, NULL, &error);
2780 /* check for valid config information */
2782 ast_log(LOG_WARNING, "Error %d creating calltokenoptional entry %s\n", error, addr);
2786 ast_copy_ha(ha, &tmp.ha);
2787 /* find or create the addr_range */
2788 if ((addr_range = ao2_find(calltoken_ignores, &tmp, OBJ_POINTER))) {
2789 ao2_lock(addr_range);
2790 addr_range->delme = 0;
2791 ao2_unlock(addr_range);
2792 } else if ((addr_range = ao2_alloc(sizeof(*addr_range), NULL))) {
2793 /* copy over config data into addr_range object */
2794 ast_copy_ha(ha, &addr_range->ha); /* this is safe because only one ha is possible */
2795 ao2_link(calltoken_ignores, addr_range);
2802 ao2_ref(addr_range, -1); /* decrement ref from ao2_find and ao2_alloc, only container ref remains */
2807 static char *handle_cli_iax2_show_callno_limits(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2809 struct ao2_iterator i;
2810 struct peercnt *peercnt;
2811 struct ast_sockaddr addr;
2816 e->command = "iax2 show callnumber usage";
2818 "Usage: iax2 show callnumber usage [IP address]\n"
2819 " Shows current IP addresses which are consuming iax2 call numbers\n";
2824 if (a->argc < 4 || a->argc > 5)
2825 return CLI_SHOWUSAGE;
2828 ast_cli(a->fd, "%-45s %-12s %-12s\n", "Address", "Callno Usage", "Callno Limit");
2831 i = ao2_iterator_init(peercnts, 0);
2832 while ((peercnt = ao2_iterator_next(&i))) {
2833 ast_sockaddr_copy(&addr, &peercnt->addr);
2836 if (!strcasecmp(a->argv[4], ast_sockaddr_stringify(&addr))) {
2837 ast_cli(a->fd, "%-45s %-12s %-12s\n", "Address", "Callno Usage", "Callno Limit");
2838 ast_cli(a->fd, "%-45s %-12d %-12d\n", ast_sockaddr_stringify(&addr), peercnt->cur, peercnt->limit);
2839 ao2_ref(peercnt, -1);
2844 ast_cli(a->fd, "%-45s %-12d %-12d\n", ast_sockaddr_stringify(&addr), peercnt->cur, peercnt->limit);
2846 ao2_ref(peercnt, -1);
2848 ao2_iterator_destroy(&i);
2851 size_t pool_avail = callno_pool.available;
2852 size_t trunk_pool_avail = callno_pool_trunk.available;
2854 ast_cli(a->fd, "\nNon-CallToken Validation Callno Limit: %d\n"
2855 "Non-CallToken Validated Callno Used: %d\n",
2856 global_maxcallno_nonval,
2857 total_nonval_callno_used);
2859 ast_cli(a->fd, "Total Available Callno: %zu\n"
2860 "Regular Callno Available: %zu\n"
2861 "Trunk Callno Available: %zu\n",
2862 pool_avail + trunk_pool_avail,
2865 } else if (a->argc == 5 && !found) {
2866 ast_cli(a->fd, "No call number table entries for %s found\n", a->argv[4] );
2876 static int get_unused_callno(enum callno_type type, int validated, callno_entry *entry)
2878 struct call_number_pool *pool = NULL;
2883 case CALLNO_TYPE_NORMAL:
2884 pool = &callno_pool;
2886 case CALLNO_TYPE_TRUNK:
2887 pool = &callno_pool_trunk;
2894 /* If we fail, make sure this has a defined value */
2897 /* We lock here primarily to ensure thread safety of the
2898 * total_nonval_callno_used check and increment */
2899 ast_mutex_lock(&callno_pool_lock);
2901 /* Bail out if we don't have any available call numbers */
2902 if (!pool->available) {
2903 ast_log(LOG_WARNING, "Out of call numbers\n");
2904 ast_mutex_unlock(&callno_pool_lock);
2908 /* Only a certain number of non-validated call numbers should be allocated.
2909 * If there ever is an attack, this separates the calltoken validating users
2910 * from the non-calltoken validating users. */
2911 if (!validated && total_nonval_callno_used >= global_maxcallno_nonval) {
2912 ast_log(LOG_WARNING,
2913 "NON-CallToken callnumber limit is reached. Current: %d Max: %d\n",
2914 total_nonval_callno_used,
2915 global_maxcallno_nonval);
2916 ast_mutex_unlock(&callno_pool_lock);
2920 /* We use a modified Fisher-Yates-Durstenfeld Shuffle to maintain a list of
2921 * available call numbers. The array of call numbers begins as an ordered
2922 * list from 1 -> n, and we keep a running tally of how many remain unclaimed
2923 * - let's call that x. When a call number is needed we pick a random index
2924 * into the array between 0 and x and use that as our call number. In a
2925 * typical FYD shuffle, we would swap the value that we are extracting with
2926 * the number at x, but in our case we swap and don't touch the value at x
2927 * because it is effectively invisible. We rely on the rest of the IAX2 core
2928 * to return the number to us at some point. Finally, we decrement x by 1
2929 * which establishes our new unused range.
2931 * When numbers are returned to the pool, we put them just past x and bump x
2932 * by 1 so that this number is now available for re-use. */
2934 choice = ast_random() % pool->available;
2936 *entry = pool->numbers[choice];
2937 swap = pool->numbers[pool->available - 1];
2939 pool->numbers[choice] = swap;
2943 CALLNO_ENTRY_SET_VALIDATED(*entry);
2945 total_nonval_callno_used++;
2948 ast_mutex_unlock(&callno_pool_lock);
2953 static int replace_callno(const void *obj)
2955 callno_entry entry = PTR_TO_CALLNO_ENTRY(obj);
2956 struct call_number_pool *pool;
2958 /* We lock here primarily to ensure thread safety of the
2959 * total_nonval_callno_used check and decrement */
2960 ast_mutex_lock(&callno_pool_lock);
2962 if (!CALLNO_ENTRY_IS_VALIDATED(entry)) {
2963 if (total_nonval_callno_used) {
2964 total_nonval_callno_used--;
2967 "Attempted to decrement total non calltoken validated "
2968 "callnumbers below zero. Callno is: %d\n",
2969 CALLNO_ENTRY_GET_CALLNO(entry));
2973 if (CALLNO_ENTRY_GET_CALLNO(entry) < TRUNK_CALL_START) {
2974 pool = &callno_pool;
2976 pool = &callno_pool_trunk;
2979 ast_assert(pool->capacity > pool->available);
2981 /* This clears the validated flag */
2982 entry = CALLNO_ENTRY_GET_CALLNO(entry);
2984 pool->numbers[pool->available] = entry;
2987 ast_mutex_unlock(&callno_pool_lock);
2992 static int create_callno_pools(void)
2996 callno_pool.available = callno_pool_trunk.available = 0;
2998 /* We start at 2. 0 and 1 are reserved. */
2999 for (i = 2; i < TRUNK_CALL_START; i++) {
3000 callno_pool.numbers[callno_pool.available] = i;
3001 callno_pool.available++;
3004 for (i = TRUNK_CALL_START; i < IAX_MAX_CALLS; i++) {
3005 callno_pool_trunk.numbers[callno_pool_trunk.available] = i;
3006 callno_pool_trunk.available++;
3009 callno_pool.capacity = callno_pool.available;
3010 callno_pool_trunk.capacity = callno_pool_trunk.available;
3012 ast_assert(callno_pool.capacity && callno_pool_trunk.capacity);
3019 * \brief Schedules delayed removal of iax2_pvt call number data
3021 * \note After MIN_REUSE_TIME has passed for a destroyed iax2_pvt, the callno is
3022 * available again, and the address from the previous connection must be decremented
3023 * from the peercnts table. This function schedules these operations to take place.
3025 static void sched_delay_remove(struct ast_sockaddr *addr, callno_entry entry)
3028 struct peercnt *peercnt;
3031 ast_sockaddr_copy(&tmp.addr, addr);
3033 if ((peercnt = ao2_find(peercnts, &tmp, OBJ_POINTER))) {
3034 /* refcount is incremented with ao2_find. keep that ref for the scheduler */
3035 ast_debug(1, "schedule decrement of callno used for %s in %d seconds\n", ast_sockaddr_stringify_addr(addr), MIN_REUSE_TIME);
3036 i = iax2_sched_add(sched, MIN_REUSE_TIME * 1000, peercnt_remove_cb, peercnt);
3038 ao2_ref(peercnt, -1);
3044 MIN_REUSE_TIME * 1000,
3046 CALLNO_ENTRY_TO_PTR(entry));
3051 * \brief returns whether or not a frame is capable of starting a new IAX2 dialog.
3053 * \note For this implementation, inbound pokes should _NOT_ be capable of allocating
3056 static inline int attribute_pure iax2_allow_new(int frametype, int subclass, int inbound)
3058 if (frametype != AST_FRAME_IAX) {
3062 case IAX_COMMAND_NEW:
3063 case IAX_COMMAND_REGREQ:
3064 case IAX_COMMAND_FWDOWNL:
3065 case IAX_COMMAND_REGREL:
3067 case IAX_COMMAND_POKE:
3077 * \note Calling this function while holding another pvt lock can cause a deadlock.
3079 static int __find_callno(unsigned short callno, unsigned short dcallno, struct ast_sockaddr *addr, int new, int sockfd, int return_locked, int check_dcallno)
3083 /* this call is calltoken validated as long as it is either NEW_FORCE
3084 * or NEW_ALLOW_CALLTOKEN_VALIDATED */
3085 int validated = (new > NEW_ALLOW) ? 1 : 0;
3088 if (new <= NEW_ALLOW) {
3090 struct chan_iax2_pvt *pvt;
3091 struct chan_iax2_pvt tmp_pvt = {
3093 .peercallno = callno,
3094 .transfercallno = callno,
3096 .frames_received = check_dcallno,
3099 ast_sockaddr_copy(&tmp_pvt.addr, addr);
3100 /* this works for finding normal call numbers not involving transfering */
3101 if ((pvt = ao2_find(iax_peercallno_pvts, &tmp_pvt, OBJ_POINTER))) {
3102 if (return_locked) {
3103 ast_mutex_lock(&iaxsl[pvt->callno]);
3110 /* this searches for transfer call numbers that might not get caught otherwise */
3111 memset(&tmp_pvt.addr, 0, sizeof(tmp_pvt.addr));
3112 ast_sockaddr_copy(&tmp_pvt.transfer, addr);
3113 if ((pvt = ao2_find(iax_transfercallno_pvts, &tmp_pvt, OBJ_POINTER))) {
3114 if (return_locked) {
3115 ast_mutex_lock(&iaxsl[pvt->callno]);
3123 /* This will occur on the first response to a message that we initiated,
3124 * such as a PING. */
3126 ast_mutex_lock(&iaxsl[dcallno]);
3128 if (callno && dcallno && iaxs[dcallno] && !iaxs[dcallno]->peercallno && match(addr, callno, dcallno, iaxs[dcallno], check_dcallno)) {
3129 iaxs[dcallno]->peercallno = callno;
3131 store_by_peercallno(iaxs[dcallno]);
3132 if (!res || !return_locked) {
3133 ast_mutex_unlock(&iaxsl[dcallno]);
3138 ast_mutex_unlock(&iaxsl[dcallno]);
3141 if (!res && (new >= NEW_ALLOW)) {
3144 /* It may seem odd that we look through the peer list for a name for
3145 * this *incoming* call. Well, it is weird. However, users don't
3146 * have an IP address/port number that we can match against. So,
3147 * this is just checking for a peer that has that IP/port and
3148 * assuming that we have a user of the same name. This isn't always
3149 * correct, but it will be changed if needed after authentication. */
3150 if (!iax2_getpeername(*addr, host, sizeof(host)))
3151 snprintf(host, sizeof(host), "%s", ast_sockaddr_stringify(addr));
3153 if (peercnt_add(addr)) {
3154 /* This address has hit its callnumber limit. When the limit
3155 * is reached, the connection is not added to the peercnts table.*/
3159 if (get_unused_callno(CALLNO_TYPE_NORMAL, validated, &entry)) {
3160 /* since we ran out of space, remove the peercnt
3161 * entry we added earlier */
3162 peercnt_remove_by_addr(addr);
3163 ast_log(LOG_WARNING, "No more space\n");
3166 x = CALLNO_ENTRY_GET_CALLNO(entry);
3167 ast_mutex_lock(&iaxsl[x]);
3169 iaxs[x] = new_iax(addr, host);
3172 ast_debug(1, "Creating new call structure %d\n", x);
3173 iaxs[x]->callno_entry = entry;
3174 iaxs[x]->sockfd = sockfd;
3175 ast_sockaddr_copy(&iaxs[x]->addr, addr);
3176 iaxs[x]->peercallno = callno;
3177 iaxs[x]->callno = x;
3178 iaxs[x]->pingtime = DEFAULT_RETRY_TIME;
3179 iaxs[x]->expiry = min_reg_expire;
3180 iaxs[x]->pingid = iax2_sched_add(sched, ping_time * 1000, send_ping, (void *)(long)x);
3181 iaxs[x]->lagid = iax2_sched_add(sched, lagrq_time * 1000, send_lagrq, (void *)(long)x);
3182 iaxs[x]->amaflags = amaflags;
3183 ast_copy_flags64(iaxs[x], &globalflags, IAX_NOTRANSFER | IAX_TRANSFERMEDIA | IAX_USEJITTERBUF | IAX_FORCEJITTERBUF | IAX_SENDCONNECTEDLINE | IAX_RECVCONNECTEDLINE | IAX_FORCE_ENCRYPT);
3184 ast_string_field_set(iaxs[x], accountcode, accountcode);
3185 ast_string_field_set(iaxs[x], mohinterpret, mohinterpret);
3186 ast_string_field_set(iaxs[x], mohsuggest, mohsuggest);
3187 ast_string_field_set(iaxs[x], parkinglot, default_parkinglot);
3189 if (iaxs[x]->peercallno) {
3190 store_by_peercallno(iaxs[x]);
3193 ast_log(LOG_WARNING, "Out of resources\n");
3194 ast_mutex_unlock(&iaxsl[x]);
3195 replace_callno(CALLNO_ENTRY_TO_PTR(entry));
3199 ast_mutex_unlock(&iaxsl[x]);
3205 static int find_callno(unsigned short callno, unsigned short dcallno, struct ast_sockaddr *addr, int new, int sockfd, int full_frame) {
3206 return __find_callno(callno, dcallno, addr, new, sockfd, 0, full_frame);
3209 static int find_callno_locked(unsigned short callno, unsigned short dcallno, struct ast_sockaddr *addr, int new, int sockfd, int full_frame) {
3211 return __find_callno(callno, dcallno, addr, new, sockfd, 1, full_frame);
3215 * \brief Queue a frame to a call's owning asterisk channel
3217 * \pre This function assumes that iaxsl[callno] is locked when called.
3219 * \note IMPORTANT NOTE!!! Any time this function is used, even if iaxs[callno]
3220 * was valid before calling it, it may no longer be valid after calling it.
3221 * This function may unlock and lock the mutex associated with this callno,
3222 * meaning that another thread may grab it and destroy the call.
3224 static int iax2_queue_frame(int callno, struct ast_frame *f)
3226 iax2_lock_owner(callno);
3227 if (iaxs[callno] && iaxs[callno]->owner) {
3228 ast_queue_frame(iaxs[callno]->owner, f);
3229 ast_channel_unlock(iaxs[callno]->owner);
3235 * \brief Queue a hold frame on the ast_channel owner
3237 * This function queues a hold frame on the owner of the IAX2 pvt struct that
3238 * is active for the given call number.
3240 * \pre Assumes lock for callno is already held.
3242 * \note IMPORTANT NOTE!!! Any time this function is used, even if iaxs[callno]
3243 * was valid before calling it, it may no longer be valid after calling it.
3244 * This function may unlock and lock the mutex associated with this callno,
3245 * meaning that another thread may grab it and destroy the call.
3247 static int iax2_queue_hold(int callno, const char *musicclass)
3249 iax2_lock_owner(callno);
3250 if (iaxs[callno] && iaxs[callno]->owner) {
3251 ast_queue_hold(iaxs[callno]->owner, musicclass);
3252 ast_channel_unlock(iaxs[callno]->owner);
3258 * \brief Queue an unhold frame on the ast_channel owner
3260 * This function queues an unhold frame on the owner of the IAX2 pvt struct that
3261 * is active for the given call number.
3263 * \pre Assumes lock for callno is already held.
3265 * \note IMPORTANT NOTE!!! Any time this function is used, even if iaxs[callno]
3266 * was valid before calling it, it may no longer be valid after calling it.
3267 * This function may unlock and lock the mutex associated with this callno,
3268 * meaning that another thread may grab it and destroy the call.
3270 static int iax2_queue_unhold(int callno)
3272 iax2_lock_owner(callno);
3273 if (iaxs[callno] && iaxs[callno]->owner) {
3274 ast_queue_unhold(iaxs[callno]->owner);
3275 ast_channel_unlock(iaxs[callno]->owner);
3281 * \brief Queue a hangup frame on the ast_channel owner
3283 * This function queues a hangup frame on the owner of the IAX2 pvt struct that
3284 * is active for the given call number.
3286 * \pre Assumes lock for callno is already held.
3288 * \note IMPORTANT NOTE!!! Any time this function is used, even if iaxs[callno]
3289 * was valid before calling it, it may no longer be valid after calling it.
3290 * This function may unlock and lock the mutex associated with this callno,
3291 * meaning that another thread may grab it and destroy the call.
3293 static int iax2_queue_hangup(int callno)
3295 iax2_lock_owner(callno);
3296 if (iaxs[callno] && iaxs[callno]->owner) {
3297 ast_queue_hangup(iaxs[callno]->owner);
3298 ast_channel_unlock(iaxs[callno]->owner);
3304 * \note This function assumes that iaxsl[callno] is locked when called.
3306 * \note IMPORTANT NOTE!!! Any time this function is used, even if iaxs[callno]
3307 * was valid before calling it, it may no longer be valid after calling it.
3308 * This function calls iax2_queue_frame(), which may unlock and lock the mutex
3309 * associated with this callno, meaning that another thread may grab it and destroy the call.
3311 static int __do_deliver(void *data)
3313 /* Just deliver the packet by using queueing. This is called by
3314 the IAX thread with the iaxsl lock held. */
3315 struct iax_frame *fr = data;
3317 ast_clear_flag(&fr->af, AST_FRFLAG_HAS_TIMING_INFO);
3318 if (iaxs[fr->callno] && !ast_test_flag64(iaxs[fr->callno], IAX_ALREADYGONE))
3319 iax2_queue_frame(fr->callno, &fr->af);
3320 /* Free our iax frame */
3321 iax2_frame_free(fr);
3322 /* And don't run again */
3326 static int handle_error(void)
3328 /* XXX Ideally we should figure out why an error occurred and then abort those
3329 rather than continuing to try. Unfortunately, the published interface does
3330 not seem to work XXX */
3332 struct sockaddr_in *sin;
3335 struct sock_extended_err e;
3340 m.msg_controllen = sizeof(e);
3342 res = recvmsg(netsocket, &m, MSG_ERRQUEUE);
3344 ast_log(LOG_WARNING, "Error detected, but unable to read error: %s\n", strerror(errno));
3346 if (m.msg_controllen) {
3347 sin = (struct sockaddr_in *)SO_EE_OFFENDER(&e);
3349 ast_log(LOG_WARNING, "Receive error from %s\n", ast_inet_ntoa(sin->sin_addr));
3351 ast_log(LOG_WARNING, "No address detected??\n");
3353 ast_log(LOG_WARNING, "Local error: %s\n", strerror(e.ee_errno));
3360 static int transmit_trunk(struct iax_frame *f, struct ast_sockaddr *addr, int sockfd)
3363 res = ast_sendto(sockfd, f->data, f->datalen, 0, addr);
3366 ast_debug(1, "Received error: %s\n", strerror(errno));
3373 static int send_packet(struct iax_frame *f)
3376 int callno = f->callno;
3378 /* Don't send if there was an error, but return error instead */
3379 if (!callno || !iaxs[callno] || iaxs[callno]->error)
3382 /* Called with iaxsl held */
3384 ast_debug(3, "Sending %u on %d/%d to %s\n", f->ts, callno, iaxs[callno]->peercallno, ast_sockaddr_stringify(&iaxs[callno]->addr));
3387 iax_outputframe(f, NULL, 0, &iaxs[callno]->transfer, f->datalen - sizeof(struct ast_iax2_full_hdr));
3388 res = ast_sendto(iaxs[callno]->sockfd, f->data, f->datalen, 0, &iaxs[callno]->transfer);
3390 iax_outputframe(f, NULL, 0, &iaxs[callno]->addr, f->datalen - sizeof(struct ast_iax2_full_hdr));
3391 res = ast_sendto(iaxs[callno]->sockfd, f->data, f->datalen, 0, &iaxs[callno]->addr);
3395 ast_debug(1, "Received error: %s\n", strerror(errno));
3404 * \note Since this function calls iax2_queue_hangup(), the pvt struct
3405 * for the given call number may disappear during its execution.
3407 static int iax2_predestroy(int callno)
3409 struct ast_channel *c = NULL;
3410 struct chan_iax2_pvt *pvt = iaxs[callno];
3415 if (!ast_test_flag64(pvt, IAX_ALREADYGONE)) {
3416 iax2_destroy_helper(pvt);
3417 ast_set_flag64(pvt, IAX_ALREADYGONE);
3420 if ((c = pvt->owner)) {
3421 ast_channel_tech_pvt_set(c, NULL);
3422 iax2_queue_hangup(callno);
3424 ast_module_unref(ast_module_info->self);
3430 static void iax2_destroy(int callno)
3432 struct chan_iax2_pvt *pvt = NULL;
3433 struct ast_channel *owner = NULL;
3436 if ((pvt = iaxs[callno])) {
3438 /* iax2_destroy_helper gets called from this function later on. When
3439 * called twice, we get the (previously) familiar FRACK! errors in
3440 * devmode, from the scheduler. An alternative to this approach is to
3441 * reset the scheduler entries to -1 when they're deleted in
3442 * iax2_destroy_helper(). That approach was previously decided to be
3443 * "wrong" because "the memory is going to be deallocated anyway. Why
3444 * should we be resetting those values?" */
3445 iax2_destroy_helper(pvt);
3449 owner = pvt ? pvt->owner : NULL;
3452 if (ast_channel_trylock(owner)) {
3453 ast_debug(3, "Avoiding IAX destroy deadlock\n");
3454 DEADLOCK_AVOIDANCE(&iaxsl[callno]);
3460 iaxs[callno] = NULL;
3467 /* If there's an owner, prod it to give up */
3468 /* It is ok to use ast_queue_hangup() here instead of iax2_queue_hangup()
3469 * because we already hold the owner channel lock. */
3470 ast_queue_hangup(owner);
3473 if (pvt->peercallno) {
3474 remove_by_peercallno(pvt);
3477 if (pvt->transfercallno) {
3478 remove_by_transfercallno(pvt);
3488 ast_channel_unlock(owner);
3492 static int update_packet(struct iax_frame *f)
3494 /* Called with iaxsl lock held, and iaxs[callno] non-NULL */
3495 struct ast_iax2_full_hdr *fh = f->data;
3496 struct ast_frame af;
3498 /* if frame is encrypted. decrypt before updating it. */
3499 if (f->encmethods) {
3500 decode_frame(&f->mydcx, fh, &af, &f->datalen);
3502 /* Mark this as a retransmission */
3503 fh->dcallno = ntohs(IAX_FLAG_RETRANS | f->dcallno);
3505 f->iseqno = iaxs[f->callno]->iseqno;
3506 fh->iseqno = f->iseqno;
3508 /* Now re-encrypt the frame */
3509 if (f->encmethods) {
3510 /* since this is a retransmit frame, create a new random padding
3511 * before re-encrypting. */
3512 build_rand_pad(f->semirand, sizeof(f->semirand));
3513 encrypt_frame(&f->ecx, fh, f->semirand, &f->datalen);
3518 static int attempt_transmit(const void *data);
3519 static void __attempt_transmit(const void *data)
3521 /* Attempt to transmit the frame to the remote peer...
3522 Called without iaxsl held. */
3523 struct iax_frame *f = (struct iax_frame *)data;
3525 int callno = f->callno;
3527 /* Make sure this call is still active */
3529 ast_mutex_lock(&iaxsl[callno]);
3530 if (callno && iaxs[callno]) {
3531 if (f->retries < 0) {
3534 } else if (f->retries >= max_retries) {
3535 /* Too many attempts. Record an error. */
3537 /* Transfer timeout */
3538 send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_TXREJ, 0, NULL, 0, -1);
3539 } else if (f->final) {
3540 iax2_destroy(callno);
3542 if (iaxs[callno]->owner) {
3543 ast_log(LOG_WARNING, "Max retries exceeded to host %s on %s (type = %u, subclass = %d, ts=%u, seqno=%d)\n",
3544 ast_sockaddr_stringify_addr(&iaxs[f->callno]->addr),
3545 ast_channel_name(iaxs[f->callno]->owner),
3547 f->af.subclass.integer,
3551 iaxs[callno]->error = ETIMEDOUT;
3552 if (iaxs[callno]->owner) {
3553 struct ast_frame fr = { AST_FRAME_CONTROL, { AST_CONTROL_HANGUP }, .data.uint32 = AST_CAUSE_DESTINATION_OUT_OF_ORDER };
3555 iax2_queue_frame(callno, &fr); /* XXX */
3556 /* Remember, owner could disappear */
3557 if (iaxs[callno] && iaxs[callno]->owner)
3558 ast_channel_hangupcause_set(iaxs[callno]->owner, AST_CAUSE_DESTINATION_OUT_OF_ORDER);
3560 if (iaxs[callno]->reg) {
3561 memset(&iaxs[callno]->reg->us, 0, sizeof(iaxs[callno]->reg->us));
3562 iaxs[callno]->reg->regstate = REG_STATE_TIMEOUT;
3563 iaxs[callno]->reg->refresh = IAX_DEFAULT_REG_EXPIRE;
3565 iax2_destroy(callno);
3570 /* Update it if it needs it */
3572 /* Attempt transmission */
3575 /* Try again later after 10 times as long */
3577 if (f->retrytime > MAX_RETRY_TIME)
3578 f->retrytime = MAX_RETRY_TIME;
3579 /* Transfer messages max out at one second */
3580 if (f->transfer && (f->retrytime > 1000))
3581 f->retrytime = 1000;
3582 f->retrans = iax2_sched_add(sched, f->retrytime, attempt_transmit, f);
3585 /* Make sure it gets freed */
3591 /* Don't attempt delivery, just remove it from the queue */
3592 AST_LIST_REMOVE(&frame_queue[callno], f, list);
3593 ast_mutex_unlock(&iaxsl[callno]);
3594 f->retrans = -1; /* this is safe because this is the scheduled function */
3595 /* Free the IAX frame */
3597 } else if (callno) {
3598 ast_mutex_unlock(&iaxsl[callno]);
3602 static int attempt_transmit(const void *data)
3604 #ifdef SCHED_MULTITHREADED
3605 if (schedule_action(__attempt_transmit, data))
3607 __attempt_transmit(data);
3611 static char *handle_cli_iax2_prune_realtime(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
3613 struct iax2_peer *peer = NULL;
3614 struct iax2_user *user = NULL;
3615 static const char * const choices[] = { "all", NULL };
3620 e->command = "iax2 prune realtime";
3622 "Usage: iax2 prune realtime [<peername>|all]\n"
3623 " Prunes object(s) from the cache\n";
3627 cmplt = ast_cli_complete(a->word, choices, a->n);
3629 cmplt = complete_iax2_peers(a->line, a->word, a->pos, a->n - sizeof(choices), IAX_RTCACHEFRIENDS);
3635 return CLI_SHOWUSAGE;
3636 if (!strcmp(a->argv[3], "all")) {
3639 ast_cli(a->fd, "Cache flushed successfully.\n");
3642 peer = find_peer(a->argv[3], 0);
3643 user = find_user(a->argv[3]);
3646 if (ast_test_flag64(peer, IAX_RTCACHEFRIENDS)) {
3647 ast_set_flag64(peer, IAX_RTAUTOCLEAR);
3648 expire_registry(peer_ref(peer));
3649 ast_cli(a->fd, "Peer %s was removed from the cache.\n", a->argv[3]);
3651 ast_cli(a->fd, "Peer %s is not eligible for this operation.\n", a->argv[3]);
3656 if (ast_test_flag64(user, IAX_RTCACHEFRIENDS)) {
3657 ast_set_flag64(user, IAX_RTAUTOCLEAR);
3658 ast_cli(a->fd, "User %s was removed from the cache.\n", a->argv[3]);
3660 ast_cli(a->fd, "User %s is not eligible for this operation.\n", a->argv[3]);
3662 ao2_unlink(users,user);
3666 ast_cli(a->fd, "%s was not found in the cache.\n", a->argv[3]);
3672 static char *handle_cli_iax2_test_losspct(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
3676 e->command = "iax2 test losspct";
3678 "Usage: iax2 test losspct <percentage>\n"
3679 " For testing, throws away <percentage> percent of incoming packets\n";
3685 return CLI_SHOWUSAGE;
3687 test_losspct = atoi(a->argv[3]);
3693 static char *handle_cli_iax2_test_late(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
3697 e->command = "iax2 test late";
3699 "Usage: iax2 test late <ms>\n"
3700 " For testing, count the next frame as <ms> ms late\n";
3707 return CLI_SHOWUSAGE;
3709 test_late = atoi(a->argv[3]);
3714 static char *handle_cli_iax2_test_resync(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
3718 e->command = "iax2 test resync";
3720 "Usage: iax2 test resync <ms>\n"
3721 " For testing, adjust all future frames by <ms> ms\n";
3728 return CLI_SHOWUSAGE;
3730 test_resync = atoi(a->argv[3]);
3735 static char *handle_cli_iax2_test_jitter(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
3739 e->command = "iax2 test jitter";
3741 "Usage: iax2 test jitter <ms> <pct>\n"
3742 " For testing, simulate maximum jitter of +/- <ms> on <pct>\n"
3743 " percentage of packets. If <pct> is not specified, adds\n"
3744 " jitter to all packets.\n";
3750 if (a->argc < 4 || a->argc > 5)
3751 return CLI_SHOWUSAGE;
3753 test_jit = atoi(a->argv[3]);
3755 test_jitpct = atoi(a->argv[4]);
3759 #endif /* IAXTESTS */
3761 /*! \brief peer_status: Report Peer status in character string */
3762 /* returns 1 if peer is online, -1 if unmonitored */
3763 static int peer_status(struct iax2_peer *peer, char *status, int statuslen)
3767 if (peer->lastms < 0) {
3768 ast_copy_string(status, "UNREACHABLE", statuslen);
3769 } else if (peer->lastms > peer->maxms) {
3770 snprintf(status, statuslen, "LAGGED (%d ms)", peer->lastms);
3772 } else if (peer->lastms) {
3773 snprintf(status, statuslen, "OK (%d ms)", peer->lastms);
3776 ast_copy_string(status, "UNKNOWN", statuslen);
3779 ast_copy_string(status, "Unmonitored", statuslen);
3785 /*! \brief Show one peer in detail */
3786 static char *handle_cli_iax2_show_peer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)