2 * Asterisk -- A telephony toolkit for Linux.
4 * Implementation of Inter-Asterisk eXchange Version 2
6 * Copyright (C) 2003, Digium
8 * Mark Spencer <markster@digium.com>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
14 #include <asterisk/lock.h>
15 #include <asterisk/frame.h>
16 #include <asterisk/channel.h>
17 #include <asterisk/channel_pvt.h>
18 #include <asterisk/logger.h>
19 #include <asterisk/module.h>
20 #include <asterisk/pbx.h>
21 #include <asterisk/sched.h>
22 #include <asterisk/io.h>
23 #include <asterisk/config.h>
24 #include <asterisk/options.h>
25 #include <asterisk/cli.h>
26 #include <asterisk/translate.h>
27 #include <asterisk/md5.h>
28 #include <asterisk/cdr.h>
29 #include <asterisk/crypto.h>
30 #include <asterisk/acl.h>
31 #include <asterisk/manager.h>
32 #include <asterisk/callerid.h>
33 #include <asterisk/app.h>
34 #include <asterisk/astdb.h>
35 #include <asterisk/musiconhold.h>
36 #include <asterisk/parking.h>
38 #include <arpa/inet.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <netinet/in_systm.h>
43 #include <netinet/ip.h>
45 #include <sys/signal.h>
55 #include <sys/types.h>
58 #include <sys/ioctl.h>
59 #include <linux/zaptel.h>
62 #include <mysql/mysql.h>
65 #include "iax2-parser.h"
66 #include "../astconf.h"
69 #define IPTOS_MINCOST 0x02
73 * Uncomment to try experimental IAX bridge optimization,
74 * designed to reduce latency when IAX calls cannot
78 #define BRIDGE_OPTIMIZATION
80 #define PTR_TO_CALLNO(a) ((unsigned short)(unsigned long)(a))
81 #define CALLNO_TO_PTR(a) ((void *)(unsigned long)(a))
83 #define DEFAULT_RETRY_TIME 1000
84 #define MEMORY_SIZE 100
85 #define DEFAULT_DROP 3
86 /* Flag to use with trunk calls, keeping these calls high up. It halves our effective use
87 but keeps the division between trunked and non-trunked better. */
88 #define TRUNK_CALL_START 0x4000
92 #define MIN_REUSE_TIME 60 /* Don't reuse a call number within 60 seconds */
94 /* Sample over last 100 units to determine historic jitter */
98 static ast_mutex_t mysqllock = AST_MUTEX_INITIALIZER;
100 static char mydbuser[80];
101 static char mydbpass[80];
102 static char mydbhost[80];
103 static char mydbname[80];
105 static char *desc = "Inter Asterisk eXchange (Ver 2)";
106 static char *tdesc = "Inter Asterisk eXchange Driver (Ver 2)";
107 static char *type = "IAX2";
109 static char context[80] = "default";
111 static int max_retries = 4;
112 static int ping_time = 20;
113 static int lagrq_time = 10;
114 static int maxtrunkcall = TRUNK_CALL_START;
115 static int maxnontrunkcall = 1;
116 static int maxjitterbuffer=3000;
117 static int trunkfreq = 20;
118 static int authdebug = 1;
119 static int iaxcompat = 0;
121 static int iaxdefaultdpcache=10 * 60; /* Cache dialplan entries for 10 minutes by default */
123 static int iaxdefaulttimeout = 5; /* Default to wait no more than 5 seconds for a reply to come back */
125 static int netsocket = -1;
129 static int expirey = IAX_DEFAULT_REG_EXPIRE;
131 static int timingfd = -1; /* Timing file descriptor */
134 static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
136 int (*iax2_regfunk)(char *username, int onoff) = NULL;
139 #define IAX_CAPABILITY_FULLBANDWIDTH 0xFFFF
141 #define IAX_CAPABILITY_MEDBANDWIDTH (IAX_CAPABILITY_FULLBANDWIDTH & \
142 ~AST_FORMAT_SLINEAR & \
146 #define IAX_CAPABILITY_LOWBANDWIDTH (IAX_CAPABILITY_MEDBANDWIDTH & \
150 #define IAX_CAPABILITY_LOWFREE (IAX_CAPABILITY_LOWBANDWIDTH & \
154 #define DEFAULT_MAXMS 2000 /* Must be faster than 2 seconds by default */
155 #define DEFAULT_FREQ_OK 60 * 1000 /* How often to check for the host to be up */
156 #define DEFAULT_FREQ_NOTOK 10 * 1000 /* How often to check, if the host is down... */
158 static struct io_context *io;
159 static struct sched_context *sched;
161 static int iax2_capability = IAX_CAPABILITY_FULLBANDWIDTH;
163 static int iax2_dropcount = DEFAULT_DROP;
165 static int use_jitterbuffer = 0;
167 static int iaxdebug = 0;
169 static int iaxtrunkdebug = 0;
171 static char accountcode[20];
172 static int amaflags = 0;
173 static int notransfer = 0;
175 static pthread_t netthreadid = AST_PTHREADT_NULL;
177 #define IAX_STATE_STARTED (1 << 0)
178 #define IAX_STATE_AUTHENTICATED (1 << 1)
179 #define IAX_STATE_TBD (1 << 2)
181 struct iax2_context {
182 char context[AST_MAX_EXTENSION];
183 struct iax2_context *next;
190 char accountcode[20];
191 char inkeys[80]; /* Key(s) this user can use to authenticate to us */
198 char callerid[AST_MAX_EXTENSION];
200 struct iax2_context *contexts;
201 struct iax2_user *next;
209 char outkey[80]; /* What key we use to talk to this peer */
210 char context[AST_MAX_EXTENSION]; /* Default context (for transfer really) */
211 char mailbox[AST_MAX_EXTENSION]; /* Mailbox */
212 struct sockaddr_in addr;
216 /* Dynamic Registration fields */
217 int dynamic; /* If this is a dynamic peer */
218 struct sockaddr_in defaddr; /* Default address if there is one */
219 int authmethods; /* Authentication methods (IAX_AUTH_*) */
220 char inkeys[80]; /* Key(s) this peer can use to authenticate to us */
223 /* Suggested caller id if registering */
224 char callerid[AST_MAX_EXTENSION];
225 /* Whether or not to send ANI */
227 int expire; /* Schedule entry for expirey */
228 int expirey; /* How soon to expire */
229 int capability; /* Capability */
230 int delme; /* I need to be deleted */
231 int temponly; /* I'm only a temp */
232 int trunk; /* Treat as an IAX trunking */
233 struct timeval txtrunktime; /* Transmit trunktime */
234 struct timeval rxtrunktime; /* Receive trunktime */
235 struct timeval lasttxtime; /* Last transmitted trunktime */
236 unsigned int lastsent; /* Last sent time */
239 int callno; /* Call number of POKE request */
240 int pokeexpire; /* When to expire poke */
241 int lastms; /* How long last response took (in ms), or -1 for no response */
242 int maxms; /* Max ms we will accept for the host to be up, 0 to not monitor */
245 struct iax2_peer *next;
249 struct iax_firmware {
250 struct iax_firmware *next;
254 struct ast_iax2_firmware_header *fwh;
258 #define REG_STATE_UNREGISTERED 0
259 #define REG_STATE_REGSENT 1
260 #define REG_STATE_AUTHSENT 2
261 #define REG_STATE_REGISTERED 3
262 #define REG_STATE_REJECTED 4
263 #define REG_STATE_TIMEOUT 5
264 #define REG_STATE_NOAUTH 6
266 #define TRANSFER_NONE 0
267 #define TRANSFER_BEGIN 1
268 #define TRANSFER_READY 2
269 #define TRANSFER_RELEASED 3
270 #define TRANSFER_PASSTHROUGH 4
272 struct iax2_registry {
273 struct sockaddr_in addr; /* Who we connect to for registration purposes */
275 char secret[80]; /* Password or key name in []'s */
277 int expire; /* Sched ID of expiration */
278 int refresh; /* How often to refresh */
280 int messages; /* Message count */
281 int callno; /* Associated call number if applicable */
282 struct sockaddr_in us; /* Who the server thinks we are */
283 struct iax2_registry *next;
286 static struct iax2_registry *registrations;
288 /* Don't retry more frequently than every 10 ms, or less frequently than every 5 seconds */
289 #define MIN_RETRY_TIME 100
290 #define MAX_RETRY_TIME 10000
291 #define MAX_JITTER_BUFFER 50
293 #define MAX_TRUNKDATA 640 /* 40ms, uncompressed linear */
295 /* If we have more than this much excess real jitter buffer, srhink it. */
296 static int max_jitter_buffer = MAX_JITTER_BUFFER;
298 struct chan_iax2_pvt {
299 /* Pipes for communication. pipe[1] belongs to the
300 network thread (write), and pipe[0] belongs to the individual
302 /* Whether or not we Quelch audio */
304 /* Last received voice format */
306 /* Last received voice format */
308 /* Last sent voice format */
310 /* Last sent video format */
312 /* What we are capable of sending */
314 /* Last received timestamp */
316 /* Last sent timestamp - never send the same timestamp twice in a single call */
317 unsigned int lastsent;
319 unsigned int pingtime;
320 /* Max time for initial response */
323 struct sockaddr_in addr;
324 /* Our call number */
325 unsigned short callno;
327 unsigned short peercallno;
328 /* Peer selected format */
330 /* Peer capability */
332 /* timeval that we base our transmission on */
333 struct timeval offset;
334 /* timeval that we base our delivery on */
335 struct timeval rxcore;
336 /* Historical delivery time */
337 int history[MEMORY_SIZE];
338 /* Current base jitterbuffer */
340 /* Current jitter measure */
342 /* Historic jitter value */
346 /* Error, as discovered by the manager */
348 /* Owner if we have one */
349 struct ast_channel *owner;
350 /* What's our state? */
352 /* Expirey (optional) */
354 /* Next outgoing sequence number */
355 unsigned char oseqno;
356 /* Next sequence number they have not yet acknowledged */
357 unsigned char rseqno;
358 /* Next incoming sequence number */
359 unsigned char iseqno;
360 /* Last incoming sequence number we have acknowledged */
361 unsigned char aseqno;
364 /* Default Context */
366 /* Caller ID if available */
368 /* Hidden Caller ID (i.e. ANI) if appropriate */
370 /* Whether or not ani should be transmitted in addition to Caller*ID */
372 /* Whether to request autoanswer */
376 /* Requested Extension */
377 char exten[AST_MAX_EXTENSION];
378 /* Expected Username */
380 /* Expected Secret */
382 /* permitted authentication methods */
386 /* Public keys permitted keys for incoming authentication */
388 /* Private key for outgoing authentication */
390 /* Preferred language */
392 /* Hostname/peername for naming purposes */
394 /* Associated registry */
395 struct iax2_registry *reg;
396 /* Associated peer for poking */
397 struct iax2_peer *peerpoke;
399 /* Transferring status */
401 /* Transfer identifier */
403 /* Already disconnected */
405 /* Who we are IAX transfering to */
406 struct sockaddr_in transfer;
407 /* What's the new call number for the transfer */
408 unsigned short transfercallno;
410 /* Status of knowledge of peer ADSI capability */
413 /* Who we are bridged to */
414 unsigned short bridgecallno;
415 unsigned int bridgesfmt;
416 struct ast_trans_pvt *bridgetrans;
418 int pingid; /* Transmit PING request */
419 int lagid; /* Retransmit lag request */
420 int autoid; /* Auto hangup for Dialplan requestor */
421 int initid; /* Initial peer auto-congest ID (based on qualified peers) */
422 char dproot[AST_MAX_EXTENSION];
423 char accountcode[20];
425 /* This is part of a trunk interface */
427 /* Trunk data and length */
428 unsigned char trunkdata[MAX_TRUNKDATA];
429 unsigned int trunkdatalen;
431 struct iax2_dpcache *dpentries;
432 int notransfer; /* do we want native bridging */
435 static struct ast_iax2_queue {
436 struct iax_frame *head;
437 struct iax_frame *tail;
442 static struct ast_user_list {
443 struct iax2_user *users;
447 static struct ast_peer_list {
448 struct iax2_peer *peers;
452 static struct ast_firmware_list {
453 struct iax_firmware *wares;
457 /* Extension exists */
458 #define CACHE_FLAG_EXISTS (1 << 0)
459 /* Extension is non-existant */
460 #define CACHE_FLAG_NONEXISTANT (1 << 1)
461 /* Extension can exist */
462 #define CACHE_FLAG_CANEXIST (1 << 2)
463 /* Waiting to hear back response */
464 #define CACHE_FLAG_PENDING (1 << 3)
466 #define CACHE_FLAG_TIMEOUT (1 << 4)
467 /* Request transmitted */
468 #define CACHE_FLAG_TRANSMITTED (1 << 5)
470 #define CACHE_FLAG_UNKNOWN (1 << 6)
472 #define CACHE_FLAG_MATCHMORE (1 << 7)
474 static struct iax2_dpcache {
475 char peercontext[AST_MAX_EXTENSION];
476 char exten[AST_MAX_EXTENSION];
478 struct timeval expirey;
480 unsigned short callno;
482 struct iax2_dpcache *next;
483 struct iax2_dpcache *peer; /* For linking in peers */
486 static ast_mutex_t dpcache_lock;
488 static void iax_debug_output(const char *data)
493 static void iax_error_output(const char *data)
495 ast_log(LOG_WARNING, data);
498 /* XXX We probably should use a mutex when working with this XXX */
499 static struct chan_iax2_pvt *iaxs[IAX_MAX_CALLS];
500 static ast_mutex_t iaxsl[IAX_MAX_CALLS];
501 static struct timeval lastused[IAX_MAX_CALLS];
504 static int send_command(struct chan_iax2_pvt *, char, int, unsigned int, char *, int, int);
505 static int send_command_locked(unsigned short callno, char, int, unsigned int, char *, int, int);
506 static int send_command_immediate(struct chan_iax2_pvt *, char, int, unsigned int, char *, int, int);
507 static int send_command_final(struct chan_iax2_pvt *, char, int, unsigned int, char *, int, int);
508 static int send_command_transfer(struct chan_iax2_pvt *, char, int, unsigned int, char *, int);
510 static unsigned int calc_timestamp(struct chan_iax2_pvt *p, unsigned int ts, struct timeval *tv);
512 static int send_ping(void *data)
514 int callno = (long)data;
515 /* Ping only if it's real, not if it's bridged */
517 #ifdef BRIDGE_OPTIMIZATION
518 if (!iaxs[callno]->bridgecallno)
520 send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_PING, 0, NULL, 0, -1);
526 static int send_lagrq(void *data)
528 int callno = (long)data;
529 /* Ping only if it's real not if it's bridged */
531 #ifdef BRIDGE_OPTIMIZATION
532 if (!iaxs[callno]->bridgecallno)
534 send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_LAGRQ, 0, NULL, 0, -1);
540 static unsigned char compress_subclass(int subclass)
544 /* If it's 128 or smaller, just return it */
545 if (subclass < IAX_FLAG_SC_LOG)
547 /* Otherwise find its power */
548 for (x = 0; x < IAX_MAX_SHIFT; x++) {
549 if (subclass & (1 << x)) {
551 ast_log(LOG_WARNING, "Can't compress subclass %d\n", subclass);
557 return power | IAX_FLAG_SC_LOG;
560 static int uncompress_subclass(unsigned char csub)
562 /* If the SC_LOG flag is set, return 2^csub otherwise csub */
563 if (csub & IAX_FLAG_SC_LOG) {
564 /* special case for 'compressed' -1 */
568 return 1 << (csub & ~IAX_FLAG_SC_LOG & IAX_MAX_SHIFT);
574 static int iax2_getpeername(struct sockaddr_in sin, char *host, int len, int lockpeer)
576 struct iax2_peer *peer;
579 ast_mutex_lock(&peerl.lock);
582 if ((peer->addr.sin_addr.s_addr == sin.sin_addr.s_addr) &&
583 (peer->addr.sin_port == sin.sin_port)) {
584 strncpy(host, peer->name, len-1);
591 ast_mutex_unlock(&peerl.lock);
595 static struct chan_iax2_pvt *new_iax(struct sockaddr_in *sin, int lockpeer)
597 struct chan_iax2_pvt *tmp;
598 tmp = malloc(sizeof(struct chan_iax2_pvt));
600 memset(tmp, 0, sizeof(struct chan_iax2_pvt));
603 tmp->transfercallno = 0;
604 tmp->bridgecallno = 0;
609 /* strncpy(tmp->context, context, sizeof(tmp->context)-1); */
610 strncpy(tmp->exten, "s", sizeof(tmp->exten)-1);
611 if (!iax2_getpeername(*sin, tmp->host, sizeof(tmp->host), lockpeer))
612 snprintf(tmp->host, sizeof(tmp->host), "%s:%d", inet_ntoa(sin->sin_addr), ntohs(sin->sin_port));
617 static int get_samples(struct ast_frame *f)
620 switch(f->subclass) {
621 case AST_FORMAT_SPEEX:
622 samples = 160; /* XXX Not necessarily true XXX */
624 case AST_FORMAT_G723_1:
625 samples = 240 /* XXX Not necessarily true XXX */;
627 case AST_FORMAT_ILBC:
628 samples = 240 * (f->datalen / 50);
631 samples = 160 * (f->datalen / 33);
633 case AST_FORMAT_G729A:
634 samples = 160 * (f->datalen / 20);
636 case AST_FORMAT_SLINEAR:
637 samples = f->datalen / 2;
639 case AST_FORMAT_LPC10:
641 samples += (((char *)(f->data))[7] & 0x1) * 8;
643 case AST_FORMAT_ULAW:
644 samples = f->datalen;
646 case AST_FORMAT_ALAW:
647 samples = f->datalen;
649 case AST_FORMAT_ADPCM:
650 case AST_FORMAT_G726:
651 samples = f->datalen *2;
654 ast_log(LOG_WARNING, "Don't know how to calculate samples on %d packets\n", f->subclass);
659 static struct iax_frame *iaxfrdup2(struct iax_frame *fr)
661 /* Malloc() a copy of a frame */
662 struct iax_frame *new = iax_frame_new(DIRECTION_INGRESS, fr->af.datalen);
664 memcpy(new, fr, sizeof(struct iax_frame));
665 iax_frame_wrap(new, &fr->af);
668 new->direction = DIRECTION_INGRESS;
674 #define NEW_PREVENT 0
678 static int match(struct sockaddr_in *sin, unsigned short callno, unsigned short dcallno, struct chan_iax2_pvt *cur)
680 if ((cur->addr.sin_addr.s_addr == sin->sin_addr.s_addr) &&
681 (cur->addr.sin_port == sin->sin_port)) {
682 /* This is the main host */
683 if ((cur->peercallno == callno) ||
684 ((dcallno == cur->callno) && !cur->peercallno)) {
685 /* That's us. Be sure we keep track of the peer call number */
689 if ((cur->transfer.sin_addr.s_addr == sin->sin_addr.s_addr) &&
690 (cur->transfer.sin_port == sin->sin_port) && (cur->transferring)) {
691 /* We're transferring */
692 if (dcallno == cur->callno)
698 static void update_max_trunk(void)
700 int max = TRUNK_CALL_START;
702 /* XXX Prolly don't need locks here XXX */
703 for (x=TRUNK_CALL_START;x<IAX_MAX_CALLS - 1; x++) {
709 ast_log(LOG_DEBUG, "New max trunk callno is %d\n", max);
712 static void update_max_nontrunk(void)
716 /* XXX Prolly don't need locks here XXX */
717 for (x=1;x<TRUNK_CALL_START - 1; x++) {
721 maxnontrunkcall = max;
723 ast_log(LOG_DEBUG, "New max nontrunk callno is %d\n", max);
726 static int make_trunk(unsigned short callno, int locked)
731 if (iaxs[callno]->oseqno) {
732 ast_log(LOG_WARNING, "Can't make trunk once a call has started!\n");
735 if (callno & TRUNK_CALL_START) {
736 ast_log(LOG_WARNING, "Call %d is already a trunk\n", callno);
739 gettimeofday(&now, NULL);
740 for (x=TRUNK_CALL_START;x<IAX_MAX_CALLS - 1; x++) {
741 ast_mutex_lock(&iaxsl[x]);
742 if (!iaxs[x] && ((now.tv_sec - lastused[x].tv_sec) > MIN_REUSE_TIME)) {
743 iaxs[x] = iaxs[callno];
746 /* Update the two timers that should have been started */
747 if (iaxs[x]->pingid > -1)
748 ast_sched_del(sched, iaxs[x]->pingid);
749 if (iaxs[x]->lagid > -1)
750 ast_sched_del(sched, iaxs[x]->lagid);
751 iaxs[x]->pingid = ast_sched_add(sched, ping_time * 1000, send_ping, (void *)x);
752 iaxs[x]->lagid = ast_sched_add(sched, lagrq_time * 1000, send_lagrq, (void *)x);
754 ast_mutex_unlock(&iaxsl[callno]);
757 ast_mutex_unlock(&iaxsl[x]);
760 ast_mutex_unlock(&iaxsl[x]);
762 if (x >= IAX_MAX_CALLS - 1) {
763 ast_log(LOG_WARNING, "Unable to trunk call: Insufficient space\n");
766 ast_log(LOG_DEBUG, "Made call %d into trunk call %d\n", callno, x);
767 /* We move this call from a non-trunked to a trunked call */
769 update_max_nontrunk();
773 static int find_callno(unsigned short callno, unsigned short dcallno, struct sockaddr_in *sin, int new, int lockpeer)
778 if (new <= NEW_ALLOW) {
779 /* Look for an existing connection first */
780 for (x=1;(res < 1) && (x<maxnontrunkcall);x++) {
781 ast_mutex_lock(&iaxsl[x]);
783 /* Look for an exact match */
784 if (match(sin, callno, dcallno, iaxs[x])) {
788 ast_mutex_unlock(&iaxsl[x]);
790 for (x=TRUNK_CALL_START;(res < 1) && (x<maxtrunkcall);x++) {
791 ast_mutex_lock(&iaxsl[x]);
793 /* Look for an exact match */
794 if (match(sin, callno, dcallno, iaxs[x])) {
798 ast_mutex_unlock(&iaxsl[x]);
801 if ((res < 1) && (new >= NEW_ALLOW)) {
802 gettimeofday(&now, NULL);
803 for (x=1;x<TRUNK_CALL_START;x++) {
804 /* Find first unused call number that hasn't been used in a while */
805 ast_mutex_lock(&iaxsl[x]);
806 if (!iaxs[x] && ((now.tv_sec - lastused[x].tv_sec) > MIN_REUSE_TIME)) break;
807 ast_mutex_unlock(&iaxsl[x]);
809 /* We've still got lock held if we found a spot */
810 if (x >= TRUNK_CALL_START) {
811 ast_log(LOG_WARNING, "No more space\n");
814 iaxs[x] = new_iax(sin, lockpeer);
815 ast_mutex_unlock(&iaxsl[x]);
816 update_max_nontrunk();
819 ast_log(LOG_DEBUG, "Creating new call structure %d\n", x);
820 iaxs[x]->addr.sin_port = sin->sin_port;
821 iaxs[x]->addr.sin_family = sin->sin_family;
822 iaxs[x]->addr.sin_addr.s_addr = sin->sin_addr.s_addr;
823 iaxs[x]->peercallno = callno;
825 iaxs[x]->pingtime = DEFAULT_RETRY_TIME;
826 iaxs[x]->expirey = expirey;
827 iaxs[x]->pingid = ast_sched_add(sched, ping_time * 1000, send_ping, (void *)x);
828 iaxs[x]->lagid = ast_sched_add(sched, lagrq_time * 1000, send_lagrq, (void *)x);
829 iaxs[x]->amaflags = amaflags;
830 iaxs[x]->notransfer = notransfer;
831 strncpy(iaxs[x]->accountcode, accountcode, sizeof(iaxs[x]->accountcode)-1);
833 ast_log(LOG_WARNING, "Out of resources\n");
841 static void iax2_frame_free(struct iax_frame *fr)
843 if (fr->retrans > -1)
844 ast_sched_del(sched, fr->retrans);
848 static int iax2_queue_frame(int callno, struct ast_frame *f)
851 /* Assumes lock for callno is already held... */
854 if (!ast_mutex_trylock(&iaxsl[callno])) {
855 ast_log(LOG_WARNING, "Lock is not held on pass %d of iax2_queue_frame\n", pass);
858 if (iaxs[callno] && iaxs[callno]->owner) {
859 if (ast_mutex_trylock(&iaxs[callno]->owner->lock)) {
860 /* Avoid deadlock by pausing and trying again */
861 ast_mutex_unlock(&iaxsl[callno]);
863 ast_mutex_lock(&iaxsl[callno]);
865 ast_queue_frame(iaxs[callno]->owner, f, 0);
866 ast_mutex_unlock(&iaxs[callno]->owner->lock);
875 static void destroy_firmware(struct iax_firmware *cur)
879 munmap(cur->fwh, ntohl(cur->fwh->datalen) + sizeof(*(cur->fwh)));
885 static int try_firmware(char *s)
888 struct iax_firmware *cur;
891 struct ast_iax2_firmware_header *fwh, fwh2;
892 struct MD5Context md5;
893 unsigned char sum[16];
894 res = stat(s, &stbuf);
896 ast_log(LOG_WARNING, "Failed to stat '%s': %s\n", s, strerror(errno));
899 /* Make sure it's not a directory */
900 if (S_ISDIR(stbuf.st_mode))
902 fd = open(s, O_RDONLY);
904 ast_log(LOG_WARNING, "Cannot open '%s': %s\n", s, strerror(errno));
907 if ((res = read(fd, &fwh2, sizeof(fwh2))) != sizeof(fwh2)) {
908 ast_log(LOG_WARNING, "Unable to read firmware header in '%s'\n", s);
912 if (ntohl(fwh2.magic) != IAX_FIRMWARE_MAGIC) {
913 ast_log(LOG_WARNING, "'%s' is not a valid firmware file\n", s);
917 if (ntohl(fwh2.datalen) != (stbuf.st_size - sizeof(fwh2))) {
918 ast_log(LOG_WARNING, "Invalid data length in firmware '%s'\n", s);
922 if (fwh2.devname[sizeof(fwh2.devname) - 1] || !strlen(fwh2.devname)) {
923 ast_log(LOG_WARNING, "No or invalid device type specified for '%s'\n", s);
927 fwh = mmap(NULL, stbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
929 ast_log(LOG_WARNING, "mmap failed: %s\n", strerror(errno));
934 MD5Update(&md5, fwh->data, ntohl(fwh->datalen));
936 if (memcmp(sum, fwh->chksum, sizeof(sum))) {
937 ast_log(LOG_WARNING, "Firmware file '%s' fails checksum\n", s);
938 munmap(fwh, stbuf.st_size);
944 if (!strcmp(cur->fwh->devname, fwh->devname)) {
945 /* Found a candidate */
946 if (cur->dead || (ntohs(cur->fwh->version) < ntohs(fwh->version)))
947 /* The version we have on loaded is older, load this one instead */
949 /* This version is no newer than what we have. Don't worry about it.
950 We'll consider it a proper load anyhow though */
951 munmap(fwh, stbuf.st_size);
958 /* Allocate a new one and link it */
959 cur = malloc(sizeof(struct iax_firmware));
961 memset(cur, 0, sizeof(struct iax_firmware));
963 cur->next = waresl.wares;
969 munmap(cur->fwh, cur->mmaplen);
975 cur->mmaplen = stbuf.st_size;
981 static int iax_check_version(char *dev)
984 struct iax_firmware *cur;
985 if (dev && strlen(dev)) {
986 ast_mutex_lock(&waresl.lock);
989 if (!strcmp(dev, cur->fwh->devname)) {
990 res = ntohs(cur->fwh->version);
995 ast_mutex_unlock(&waresl.lock);
1000 static int iax_firmware_append(struct iax_ie_data *ied, const unsigned char *dev, unsigned int desc)
1003 unsigned int bs = desc & 0xff;
1004 unsigned int start = (desc >> 8) & 0xffffff;
1006 struct iax_firmware *cur;
1007 if (dev && strlen(dev) && bs) {
1009 ast_mutex_lock(&waresl.lock);
1012 if (!strcmp(dev, cur->fwh->devname)) {
1013 iax_ie_append_int(ied, IAX_IE_FWBLOCKDESC, desc);
1014 if (start < ntohl(cur->fwh->datalen)) {
1015 bytes = ntohl(cur->fwh->datalen) - start;
1018 iax_ie_append_raw(ied, IAX_IE_FWBLOCKDATA, cur->fwh->data + start, bytes);
1021 iax_ie_append(ied, IAX_IE_FWBLOCKDATA);
1031 ast_mutex_unlock(&waresl.lock);
1037 static void reload_firmware(void)
1039 struct iax_firmware *cur, *curl, *curp;
1044 /* Mark all as dead */
1045 ast_mutex_lock(&waresl.lock);
1051 /* Now that we've freed them, load the new ones */
1052 snprintf(dir, sizeof(dir), "%s/firmware/iax", (char *)ast_config_AST_VAR_DIR);
1055 while((de = readdir(fwd))) {
1056 if (de->d_name[0] != '.') {
1057 snprintf(fn, sizeof(fn), "%s/%s", dir, de->d_name);
1058 if (!try_firmware(fn)) {
1059 if (option_verbose > 1)
1060 ast_verbose(VERBOSE_PREFIX_2 "Loaded firmware '%s'\n", de->d_name);
1066 ast_log(LOG_WARNING, "Error opening firmware directory '%s': %s\n", dir, strerror(errno));
1068 /* Clean up leftovers */
1080 destroy_firmware(curl);
1085 ast_mutex_unlock(&waresl.lock);
1088 static int iax2_send(struct chan_iax2_pvt *pvt, struct ast_frame *f, unsigned int ts, int seqno, int now, int transfer, int final);
1090 static int __do_deliver(void *data)
1092 /* Just deliver the packet by using queueing. This is called by
1093 the IAX thread with the iaxsl lock held. */
1094 struct iax_frame *fr = data;
1097 if (iaxs[fr->callno] && !iaxs[fr->callno]->alreadygone) {
1098 if (fr->af.frametype == AST_FRAME_IAX) {
1099 /* We have to treat some of these packets specially because
1100 they're LAG measurement packets */
1101 if (fr->af.subclass == IAX_COMMAND_LAGRQ) {
1102 /* If we got a queued request, build a reply and send it */
1103 fr->af.subclass = IAX_COMMAND_LAGRP;
1104 iax2_send(iaxs[fr->callno], &fr->af, fr->ts, -1, 0, 0, 0);
1105 } else if (fr->af.subclass == IAX_COMMAND_LAGRP) {
1106 /* This is a reply we've been given, actually measure the difference */
1107 ts = calc_timestamp(iaxs[fr->callno], 0, NULL);
1108 iaxs[fr->callno]->lag = ts - fr->ts;
1111 iax2_queue_frame(fr->callno, &fr->af);
1114 /* Free our iax frame */
1115 iax2_frame_free(fr);
1116 /* And don't run again */
1120 static int do_deliver(void *data)
1122 /* Locking version of __do_deliver */
1123 struct iax_frame *fr = data;
1124 int callno = fr->callno;
1126 ast_mutex_lock(&iaxsl[callno]);
1127 res = __do_deliver(data);
1128 ast_mutex_unlock(&iaxsl[callno]);
1132 static int handle_error(void)
1134 /* XXX Ideally we should figure out why an error occured and then abort those
1135 rather than continuing to try. Unfortunately, the published interface does
1136 not seem to work XXX */
1138 struct sockaddr_in *sin;
1141 struct sock_extended_err e;
1146 m.msg_controllen = sizeof(e);
1148 res = recvmsg(netsocket, &m, MSG_ERRQUEUE);
1150 ast_log(LOG_WARNING, "Error detected, but unable to read error: %s\n", strerror(errno));
1152 if (m.msg_controllen) {
1153 sin = (struct sockaddr_in *)SO_EE_OFFENDER(&e);
1155 ast_log(LOG_WARNING, "Receive error from %s\n", inet_ntoa(sin->sin_addr));
1157 ast_log(LOG_WARNING, "No address detected??\n");
1159 ast_log(LOG_WARNING, "Local error: %s\n", strerror(e.ee_errno));
1166 static int send_packet(struct iax_frame *f)
1169 /* Called with iaxsl held */
1171 ast_log(LOG_DEBUG, "Sending %d on %d/%d to %s:%d\n", f->ts, f->callno, iaxs[f->callno]->peercallno, inet_ntoa(iaxs[f->callno]->addr.sin_addr), ntohs(iaxs[f->callno]->addr.sin_port));
1172 /* Don't send if there was an error, but return error instead */
1174 ast_log(LOG_WARNING, "Call number = %d\n", f->callno);
1177 if (!iaxs[f->callno])
1179 if (iaxs[f->callno]->error)
1183 iax_showframe(f, NULL, 0, &iaxs[f->callno]->transfer, f->datalen - sizeof(struct ast_iax2_full_hdr));
1184 res = sendto(netsocket, f->data, f->datalen, 0,(struct sockaddr *)&iaxs[f->callno]->transfer,
1185 sizeof(iaxs[f->callno]->transfer));
1188 iax_showframe(f, NULL, 0, &iaxs[f->callno]->addr, f->datalen - sizeof(struct ast_iax2_full_hdr));
1189 res = sendto(netsocket, f->data, f->datalen, 0,(struct sockaddr *)&iaxs[f->callno]->addr,
1190 sizeof(iaxs[f->callno]->addr));
1194 ast_log(LOG_DEBUG, "Received error: %s\n", strerror(errno));
1202 static int iax2_predestroy(int callno)
1204 struct ast_channel *c;
1205 struct chan_iax2_pvt *pvt;
1206 ast_mutex_lock(&iaxsl[callno]);
1209 ast_mutex_unlock(&iaxsl[callno]);
1212 if (!pvt->alreadygone) {
1213 /* No more pings or lagrq's */
1214 if (pvt->pingid > -1)
1215 ast_sched_del(sched, pvt->pingid);
1216 if (pvt->lagid > -1)
1217 ast_sched_del(sched, pvt->lagid);
1218 if (pvt->autoid > -1)
1219 ast_sched_del(sched, pvt->autoid);
1220 if (pvt->initid > -1)
1221 ast_sched_del(sched, pvt->initid);
1226 pvt->alreadygone = 1;
1230 c->_softhangup |= AST_SOFTHANGUP_DEV;
1232 ast_queue_hangup(c, 0);
1234 ast_mutex_lock(&usecnt_lock);
1237 ast_log(LOG_WARNING, "Usecnt < 0???\n");
1238 ast_mutex_unlock(&usecnt_lock);
1240 ast_mutex_unlock(&iaxsl[callno]);
1241 ast_update_use_count();
1245 static int iax2_predestroy_nolock(int callno)
1248 ast_mutex_unlock(&iaxsl[callno]);
1249 res = iax2_predestroy(callno);
1250 ast_mutex_lock(&iaxsl[callno]);
1254 static void iax2_destroy(int callno)
1256 struct chan_iax2_pvt *pvt;
1257 struct iax_frame *cur;
1258 struct ast_channel *owner;
1261 ast_mutex_lock(&iaxsl[callno]);
1263 gettimeofday(&lastused[callno], NULL);
1270 if (ast_mutex_trylock(&owner->lock)) {
1271 ast_log(LOG_NOTICE, "Avoiding IAX destroy deadlock\n");
1272 ast_mutex_unlock(&iaxsl[callno]);
1278 iaxs[callno] = NULL;
1282 /* No more pings or lagrq's */
1283 if (pvt->pingid > -1)
1284 ast_sched_del(sched, pvt->pingid);
1285 if (pvt->lagid > -1)
1286 ast_sched_del(sched, pvt->lagid);
1287 if (pvt->autoid > -1)
1288 ast_sched_del(sched, pvt->autoid);
1289 if (pvt->initid > -1)
1290 ast_sched_del(sched, pvt->initid);
1295 if (pvt->bridgetrans)
1296 ast_translator_free_path(pvt->bridgetrans);
1297 pvt->bridgetrans = NULL;
1300 pvt->alreadygone = 1;
1303 /* If there's an owner, prod it to give up */
1304 owner->_softhangup |= AST_SOFTHANGUP_DEV;
1305 ast_queue_hangup(owner, 0);
1308 for (cur = iaxq.head; cur ; cur = cur->next) {
1309 /* Cancel any pending transmissions */
1310 if (cur->callno == pvt->callno)
1314 pvt->reg->callno = 0;
1320 ast_mutex_unlock(&owner->lock);
1322 ast_mutex_unlock(&iaxsl[callno]);
1323 if (callno & 0x4000)
1326 static void iax2_destroy_nolock(int callno)
1328 /* Actually it's easier to unlock, kill it, and relock */
1329 ast_mutex_unlock(&iaxsl[callno]);
1330 iax2_destroy(callno);
1331 ast_mutex_lock(&iaxsl[callno]);
1334 static int update_packet(struct iax_frame *f)
1336 /* Called with iaxsl lock held, and iaxs[callno] non-NULL */
1337 struct ast_iax2_full_hdr *fh = f->data;
1338 /* Mark this as a retransmission */
1339 fh->dcallno = ntohs(IAX_FLAG_RETRANS | f->dcallno);
1341 f->iseqno = iaxs[f->callno]->iseqno;
1342 fh->iseqno = f->iseqno;
1346 static int attempt_transmit(void *data)
1348 /* Attempt to transmit the frame to the remote peer...
1349 Called without iaxsl held. */
1350 struct iax_frame *f = data;
1352 int callno = f->callno;
1353 /* Make sure this call is still active */
1355 ast_mutex_lock(&iaxsl[callno]);
1356 if ((f->callno) && iaxs[f->callno]) {
1357 if ((f->retries < 0) /* Already ACK'd */ ||
1358 (f->retries >= max_retries) /* Too many attempts */) {
1359 /* Record an error if we've transmitted too many times */
1360 if (f->retries >= max_retries) {
1362 /* Transfer timeout */
1363 send_command(iaxs[f->callno], AST_FRAME_IAX, IAX_COMMAND_TXREJ, 0, NULL, 0, -1);
1364 } else if (f->final) {
1366 iax2_destroy_nolock(f->callno);
1368 if (iaxs[f->callno]->owner)
1369 ast_log(LOG_WARNING, "Max retries exceeded to host %s on %s (type = %d, subclass = %d, ts=%d, seqno=%d)\n", inet_ntoa(iaxs[f->callno]->addr.sin_addr),iaxs[f->callno]->owner->name , f->af.frametype, f->af.subclass, f->ts, f->oseqno);
1370 iaxs[f->callno]->error = ETIMEDOUT;
1371 if (iaxs[f->callno]->owner) {
1372 struct ast_frame fr = { 0, };
1374 fr.frametype = AST_FRAME_CONTROL;
1375 fr.subclass = AST_CONTROL_HANGUP;
1376 iax2_queue_frame(f->callno, &fr);
1378 if (iaxs[f->callno]->reg) {
1379 memset(&iaxs[f->callno]->reg->us, 0, sizeof(iaxs[f->callno]->reg->us));
1380 iaxs[f->callno]->reg->regstate = REG_STATE_TIMEOUT;
1381 iaxs[f->callno]->reg->refresh = IAX_DEFAULT_REG_EXPIRE;
1383 iax2_destroy_nolock(f->callno);
1390 /* Update it if it needs it */
1392 /* Attempt transmission */
1395 /* Try again later after 10 times as long */
1397 if (f->retrytime > MAX_RETRY_TIME)
1398 f->retrytime = MAX_RETRY_TIME;
1399 /* Transfer messages max out at one second */
1400 if (f->transfer && (f->retrytime > 1000))
1401 f->retrytime = 1000;
1402 f->retrans = ast_sched_add(sched, f->retrytime, attempt_transmit, f);
1405 /* Make sure it gets freed */
1410 ast_mutex_unlock(&iaxsl[callno]);
1411 /* Do not try again */
1413 /* Don't attempt delivery, just remove it from the queue */
1414 ast_mutex_lock(&iaxq.lock);
1416 f->prev->next = f->next;
1418 iaxq.head = f->next;
1420 f->next->prev = f->prev;
1422 iaxq.tail = f->prev;
1424 ast_mutex_unlock(&iaxq.lock);
1426 /* Free the IAX frame */
1432 static int iax2_set_jitter(int fd, int argc, char *argv[])
1434 if ((argc != 4) && (argc != 5))
1435 return RESULT_SHOWUSAGE;
1437 max_jitter_buffer = atoi(argv[3]);
1438 if (max_jitter_buffer < 0)
1439 max_jitter_buffer = 0;
1442 if ((atoi(argv[3]) >= 0) && (atoi(argv[3]) < IAX_MAX_CALLS)) {
1443 if (iaxs[atoi(argv[3])]) {
1444 iaxs[atoi(argv[3])]->jitterbuffer = atoi(argv[4]);
1445 if (iaxs[atoi(argv[3])]->jitterbuffer < 0)
1446 iaxs[atoi(argv[3])]->jitterbuffer = 0;
1448 ast_cli(fd, "No such call '%d'\n", atoi(argv[3]));
1450 ast_cli(fd, "%d is not a valid call number\n", atoi(argv[3]));
1453 return RESULT_SUCCESS;
1456 static char jitter_usage[] =
1457 "Usage: iax set jitter [callid] <value>\n"
1458 " If used with a callid, it sets the jitter buffer to the given static\n"
1459 "value (until its next calculation). If used without a callid, the value is used\n"
1460 "to establish the maximum excess jitter buffer that is permitted before the jitter\n"
1461 "buffer size is reduced.";
1463 static int iax2_show_stats(int fd, int argc, char *argv[])
1465 struct iax_frame *cur;
1466 int cnt = 0, dead=0, final=0;
1468 return RESULT_SHOWUSAGE;
1469 for (cur = iaxq.head; cur ; cur = cur->next) {
1470 if (cur->retries < 0)
1476 ast_cli(fd, " IAX Statistics\n");
1477 ast_cli(fd, "---------------------\n");
1478 ast_cli(fd, "Outstanding frames: %d (%d ingress, %d outgress)\n", iax_get_frames(), iax_get_iframes(), iax_get_oframes());
1479 ast_cli(fd, "Packets in transmit queue: %d dead, %d final, %d total\n", dead, final, cnt);
1480 return RESULT_SUCCESS;
1483 static int iax2_show_cache(int fd, int argc, char *argv[])
1485 struct iax2_dpcache *dp;
1486 char tmp[1024], *pc;
1490 gettimeofday(&tv, NULL);
1491 ast_mutex_lock(&dpcache_lock);
1493 ast_cli(fd, "%-20.20s %-12.12s %-9.9s %-8.8s %s\n", "Peer/Context", "Exten", "Exp.", "Wait.", "Flags");
1495 s = dp->expirey.tv_sec - tv.tv_sec;
1497 if (dp->flags & CACHE_FLAG_EXISTS)
1498 strcat(tmp, "EXISTS|");
1499 if (dp->flags & CACHE_FLAG_NONEXISTANT)
1500 strcat(tmp, "NONEXISTANT|");
1501 if (dp->flags & CACHE_FLAG_CANEXIST)
1502 strcat(tmp, "CANEXIST|");
1503 if (dp->flags & CACHE_FLAG_PENDING)
1504 strcat(tmp, "PENDING|");
1505 if (dp->flags & CACHE_FLAG_TIMEOUT)
1506 strcat(tmp, "TIMEOUT|");
1507 if (dp->flags & CACHE_FLAG_TRANSMITTED)
1508 strcat(tmp, "TRANSMITTED|");
1509 if (dp->flags & CACHE_FLAG_MATCHMORE)
1510 strcat(tmp, "MATCHMORE|");
1511 if (dp->flags & CACHE_FLAG_UNKNOWN)
1512 strcat(tmp, "UNKNOWN|");
1513 /* Trim trailing pipe */
1515 tmp[strlen(tmp) - 1] = '\0';
1517 strcpy(tmp, "(none)");
1519 pc = strchr(dp->peercontext, '@');
1521 pc = dp->peercontext;
1524 for (x=0;x<sizeof(dp->waiters) / sizeof(dp->waiters[0]); x++)
1525 if (dp->waiters[x] > -1)
1528 ast_cli(fd, "%-20.20s %-12.12s %-9d %-8d %s\n", pc, dp->exten, s, y, tmp);
1530 ast_cli(fd, "%-20.20s %-12.12s %-9.9s %-8d %s\n", pc, dp->exten, "(expired)", y, tmp);
1533 ast_mutex_unlock(&dpcache_lock);
1534 return RESULT_SUCCESS;
1537 static char show_stats_usage[] =
1538 "Usage: iax show stats\n"
1539 " Display statistics on IAX channel driver.\n";
1542 static char show_cache_usage[] =
1543 "Usage: iax show cache\n"
1544 " Display currently cached IAX Dialplan results.\n";
1546 static struct ast_cli_entry cli_set_jitter =
1547 { { "iax2", "set", "jitter", NULL }, iax2_set_jitter, "Sets IAX jitter buffer", jitter_usage };
1549 static struct ast_cli_entry cli_show_stats =
1550 { { "iax2", "show", "stats", NULL }, iax2_show_stats, "Display IAX statistics", show_stats_usage };
1552 static struct ast_cli_entry cli_show_cache =
1553 { { "iax2", "show", "cache", NULL }, iax2_show_cache, "Display IAX cached dialplan", show_cache_usage };
1555 static unsigned int calc_rxstamp(struct chan_iax2_pvt *p);
1557 #ifdef BRIDGE_OPTIMIZATION
1558 static unsigned int calc_fakestamp(struct chan_iax2_pvt *from, struct chan_iax2_pvt *to, unsigned int ts);
1560 static int forward_delivery(struct iax_frame *fr)
1562 struct chan_iax2_pvt *p1, *p2;
1563 p1 = iaxs[fr->callno];
1564 p2 = iaxs[p1->bridgecallno];
1569 /* Fix relative timestamp */
1570 fr->ts = calc_fakestamp(p1, p2, fr->ts);
1571 /* Now just send it send on the 2nd one
1572 with adjusted timestamp */
1573 return iax2_send(p2, &fr->af, fr->ts, -1, 0, 0, 0);
1577 static int schedule_delivery(struct iax_frame *fr, int reallydeliver, int updatehistory)
1580 int drops[MEMORY_SIZE];
1581 int min, max=0, maxone=0,y,z, match;
1582 /* ms is a measure of the "lateness" of the packet relative to the first
1583 packet we received, which always has a lateness of 1. Called by
1584 IAX thread, with iaxsl lock held. */
1585 ms = calc_rxstamp(iaxs[fr->callno]) - fr->ts;
1588 /* What likely happened here is that our counter has circled but we haven't
1589 gotten the update from the main packet. We'll just pretend that we did, and
1590 update the timestamp appropriately. */
1596 /* We got this packet out of order. Lets add 65536 to it to bring it into our new
1602 fr->af.delivery.tv_sec = iaxs[fr->callno]->rxcore.tv_sec;
1603 fr->af.delivery.tv_usec = iaxs[fr->callno]->rxcore.tv_usec;
1604 fr->af.delivery.tv_sec += fr->ts / 1000;
1605 fr->af.delivery.tv_usec += (fr->ts % 1000) * 1000;
1606 if (fr->af.delivery.tv_usec >= 1000000) {
1607 fr->af.delivery.tv_usec -= 1000000;
1608 fr->af.delivery.tv_sec += 1;
1612 /* Rotate our history queue of "lateness". Don't worry about those initial
1613 zeros because the first entry will always be zero */
1614 if (updatehistory) {
1615 for (x=0;x<MEMORY_SIZE - 1;x++)
1616 iaxs[fr->callno]->history[x] = iaxs[fr->callno]->history[x+1];
1617 /* Add a history entry for this one */
1618 iaxs[fr->callno]->history[x] = ms;
1621 /* Initialize the minimum to reasonable values. It's too much
1622 work to do the same for the maximum, repeatedly */
1623 min=iaxs[fr->callno]->history[0];
1624 for (z=0;z < iax2_dropcount + 1;z++) {
1625 /* Start very optimistic ;-) */
1627 for (x=0;x<MEMORY_SIZE;x++) {
1628 if (max < iaxs[fr->callno]->history[x]) {
1629 /* We have a candidate new maximum value. Make
1630 sure it's not in our drop list */
1632 for (y=0;!match && (y<z);y++)
1633 match |= (drops[y] == x);
1635 /* It's not in our list, use it as the new maximum */
1636 max = iaxs[fr->callno]->history[x];
1642 /* On our first pass, find the minimum too */
1643 if (min > iaxs[fr->callno]->history[x])
1644 min = iaxs[fr->callno]->history[x];
1651 /* Just for reference, keep the "jitter" value, the difference between the
1652 earliest and the latest. */
1653 iaxs[fr->callno]->jitter = max - min;
1655 /* IIR filter for keeping track of historic jitter, but always increase
1656 historic jitter immediately for increase */
1658 if (iaxs[fr->callno]->jitter > iaxs[fr->callno]->historicjitter )
1659 iaxs[fr->callno]->historicjitter = iaxs[fr->callno]->jitter;
1661 iaxs[fr->callno]->historicjitter = GAMMA * (double)iaxs[fr->callno]->jitter + (1-GAMMA) *
1662 iaxs[fr->callno]->historicjitter;
1664 /* If our jitter buffer is too big (by a significant margin), then we slowly
1665 shrink it by about 1 ms each time to avoid letting the change be perceived */
1666 if (max < iaxs[fr->callno]->jitterbuffer - max_jitter_buffer)
1667 iaxs[fr->callno]->jitterbuffer -= 2;
1671 /* Constrain our maximum jitter buffer appropriately */
1672 if (max > min + maxjitterbuffer) {
1674 ast_log(LOG_DEBUG, "Constraining buffer from %d to %d + %d\n", max, min , maxjitterbuffer);
1675 max = min + maxjitterbuffer;
1679 /* If our jitter buffer is smaller than our maximum delay, grow the jitter
1680 buffer immediately to accomodate it (and a little more). */
1681 if (max > iaxs[fr->callno]->jitterbuffer)
1682 iaxs[fr->callno]->jitterbuffer = max
1683 /* + ((float)iaxs[fr->callno]->jitter) * 0.1 */;
1687 ast_log(LOG_DEBUG, "min = %d, max = %d, jb = %d, lateness = %d\n", min, max, iaxs[fr->callno]->jitterbuffer, ms);
1689 /* Subtract the lateness from our jitter buffer to know how long to wait
1690 before sending our packet. */
1691 ms = iaxs[fr->callno]->jitterbuffer - ms;
1693 if (!use_jitterbuffer)
1696 /* If the caller just wanted us to update, return now */
1702 ast_log(LOG_DEBUG, "Calculated ms is %d\n", ms);
1703 /* Don't deliver it more than 4 ms late */
1704 if ((ms > -4) || (fr->af.frametype != AST_FRAME_VOICE)) {
1708 ast_log(LOG_DEBUG, "Dropping voice packet since %d ms is, too old\n", ms);
1709 /* Free our iax frame */
1710 iax2_frame_free(fr);
1714 ast_log(LOG_DEBUG, "Scheduling delivery in %d ms\n", ms);
1715 fr->retrans = ast_sched_add(sched, ms, do_deliver, fr);
1720 static int iax2_transmit(struct iax_frame *fr)
1722 /* Lock the queue and place this packet at the end */
1725 /* By setting this to 0, the network thread will send it for us, and
1726 queue retransmission if necessary */
1728 ast_mutex_lock(&iaxq.lock);
1735 iaxq.tail->next = fr;
1736 fr->prev = iaxq.tail;
1740 ast_mutex_unlock(&iaxq.lock);
1741 /* Wake up the network thread */
1742 pthread_kill(netthreadid, SIGURG);
1748 static int iax2_digit(struct ast_channel *c, char digit)
1750 return send_command_locked(PTR_TO_CALLNO(c->pvt->pvt), AST_FRAME_DTMF, digit, 0, NULL, 0, -1);
1753 static int iax2_sendtext(struct ast_channel *c, char *text)
1756 return send_command_locked(PTR_TO_CALLNO(c->pvt->pvt), AST_FRAME_TEXT,
1757 0, 0, text, strlen(text) + 1, -1);
1760 static int iax2_sendimage(struct ast_channel *c, struct ast_frame *img)
1762 return send_command_locked(PTR_TO_CALLNO(c->pvt->pvt), AST_FRAME_IMAGE, img->subclass, 0, img->data, img->datalen, -1);
1765 static int iax2_sendhtml(struct ast_channel *c, int subclass, char *data, int datalen)
1767 return send_command_locked(PTR_TO_CALLNO(c->pvt->pvt), AST_FRAME_HTML, subclass, 0, data, datalen, -1);
1770 static int iax2_fixup(struct ast_channel *oldchannel, struct ast_channel *newchan, int lock)
1772 unsigned short callno = PTR_TO_CALLNO(newchan->pvt->pvt);
1774 ast_mutex_lock(&iaxsl[callno]);
1776 iaxs[callno]->owner = newchan;
1778 ast_log(LOG_WARNING, "Uh, this isn't a good sign...\n");
1780 ast_mutex_unlock(&iaxsl[callno]);
1784 #ifdef MYSQL_FRIENDS
1786 static void mysql_update_peer(char *peer, struct sockaddr_in *sin)
1788 if (mysql && (strlen(peer) < 128)) {
1792 name = alloca(strlen(peer) * 2 + 1);
1794 mysql_real_escape_string(mysql, name, peer, strlen(peer));
1795 snprintf(query, sizeof(query), "UPDATE iaxfriends SET ipaddr=\"%s\", port=\"%d\", regseconds=\"%ld\" WHERE name=\"%s\"",
1796 inet_ntoa(sin->sin_addr), ntohs(sin->sin_port), nowtime, name);
1797 ast_mutex_lock(&mysqllock);
1798 if (mysql_real_query(mysql, query, strlen(query)))
1799 ast_log(LOG_WARNING, "Unable to update database\n");
1801 ast_mutex_unlock(&mysqllock);
1805 static struct iax2_peer *mysql_peer(char *peer)
1807 struct iax2_peer *p;
1810 p = malloc(sizeof(struct iax2_peer));
1811 memset(p, 0, sizeof(struct iax2_peer));
1812 if (mysql && (strlen(peer) < 128)) {
1817 time_t regseconds, nowtime;
1819 MYSQL_FIELD *fields;
1821 name = alloca(strlen(peer) * 2 + 1);
1822 mysql_real_escape_string(mysql, name, peer, strlen(peer));
1823 snprintf(query, sizeof(query), "SELECT * FROM iaxfriends WHERE name=\"%s\"", name);
1824 ast_mutex_lock(&mysqllock);
1825 mysql_query(mysql, query);
1826 if ((result = mysql_store_result(mysql))) {
1827 if ((rowval = mysql_fetch_row(result))) {
1828 numfields = mysql_num_fields(result);
1829 fields = mysql_fetch_fields(result);
1831 for (x=0;x<numfields;x++) {
1833 if (!strcasecmp(fields[x].name, "secret")) {
1834 strncpy(p->secret, rowval[x], sizeof(p->secret));
1835 } else if (!strcasecmp(fields[x].name, "context")) {
1836 strncpy(p->context, rowval[x], sizeof(p->context) - 1);
1837 } else if (!strcasecmp(fields[x].name, "ipaddr")) {
1838 inet_aton(rowval[x], &p->addr.sin_addr);
1839 } else if (!strcasecmp(fields[x].name, "port")) {
1840 if (sscanf(rowval[x], "%i", &port) != 1)
1842 p->addr.sin_port = htons(port);
1843 } else if (!strcasecmp(fields[x].name, "regseconds")) {
1844 if (sscanf(rowval[x], "%li", ®seconds) != 1)
1850 if ((nowtime - regseconds) > IAX_DEFAULT_REG_EXPIRE)
1851 memset(&p->addr, 0, sizeof(p->addr));
1853 mysql_free_result(result);
1856 ast_mutex_unlock(&mysqllock);
1862 strncpy(p->name, peer, sizeof(p->name) - 1);
1866 p->capability = iax2_capability;
1867 p->authmethods = IAX_AUTH_MD5 | IAX_AUTH_PLAINTEXT;
1871 static struct iax2_user *mysql_user(char *user)
1873 struct iax2_user *p;
1874 struct iax2_context *con;
1877 p = malloc(sizeof(struct iax2_user));
1878 memset(p, 0, sizeof(struct iax2_user));
1879 con = malloc(sizeof(struct iax2_context));
1880 memset(con, 0, sizeof(struct iax2_context));
1881 strcpy(con->context, "default");
1883 if (mysql && (strlen(user) < 128)) {
1888 MYSQL_FIELD *fields;
1890 name = alloca(strlen(user) * 2 + 1);
1891 mysql_real_escape_string(mysql, name, user, strlen(user));
1892 snprintf(query, sizeof(query), "SELECT * FROM iaxfriends WHERE name=\"%s\"", name);
1893 ast_mutex_lock(&mysqllock);
1894 mysql_query(mysql, query);
1895 if ((result = mysql_store_result(mysql))) {
1896 if ((rowval = mysql_fetch_row(result))) {
1897 numfields = mysql_num_fields(result);
1898 fields = mysql_fetch_fields(result);
1900 for (x=0;x<numfields;x++) {
1902 if (!strcasecmp(fields[x].name, "secret")) {
1903 strncpy(p->secret, rowval[x], sizeof(p->secret));
1904 } else if (!strcasecmp(fields[x].name, "context")) {
1905 strncpy(p->contexts->context, rowval[x], sizeof(p->contexts->context) - 1);
1910 mysql_free_result(result);
1913 ast_mutex_unlock(&mysqllock);
1921 strncpy(p->name, user, sizeof(p->name) - 1);
1923 p->capability = iax2_capability;
1924 p->authmethods = IAX_AUTH_MD5 | IAX_AUTH_PLAINTEXT;
1928 #endif /* MYSQL_FRIENDS */
1930 static int create_addr(struct sockaddr_in *sin, int *capability, int *sendani, int *maxtime, char *peer, char *context, int *trunk, int *notransfer, char *secret, int seclen)
1933 struct iax2_peer *p;
1941 sin->sin_family = AF_INET;
1942 ast_mutex_lock(&peerl.lock);
1945 if (!strcasecmp(p->name, peer)) {
1950 ast_mutex_unlock(&peerl.lock);
1951 #ifdef MYSQL_FRIENDS
1953 p = mysql_peer(peer);
1957 if ((p->addr.sin_addr.s_addr || p->defaddr.sin_addr.s_addr) &&
1958 (!p->maxms || ((p->lastms > 0) && (p->lastms <= p->maxms)))) {
1960 *sendani = p->sendani; /* Whether we transmit ANI */
1962 *maxtime = p->maxms; /* Max time they should take */
1964 strncpy(context, p->context, AST_MAX_EXTENSION - 1);
1968 *capability = p->capability;
1970 strncpy(secret, p->secret, seclen);
1971 if (p->addr.sin_addr.s_addr) {
1972 sin->sin_addr = p->addr.sin_addr;
1973 sin->sin_port = p->addr.sin_port;
1975 sin->sin_addr = p->defaddr.sin_addr;
1976 sin->sin_port = p->defaddr.sin_port;
1979 *notransfer=p->notransfer;
1987 hp = gethostbyname(peer);
1989 memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
1990 sin->sin_port = htons(IAX_DEFAULT_PORTNO);
1993 ast_log(LOG_WARNING, "No such host: %s\n", peer);
2003 static int auto_congest(void *nothing)
2005 int callno = PTR_TO_CALLNO(nothing);
2006 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_CONGESTION };
2007 ast_mutex_lock(&iaxsl[callno]);
2009 iaxs[callno]->initid = -1;
2010 iax2_queue_frame(callno, &f);
2011 ast_log(LOG_NOTICE, "Auto-congesting call due to slow response\n");
2013 ast_mutex_unlock(&iaxsl[callno]);
2017 static unsigned int iax2_datetime(void)
2023 localtime_r(&t, &tm);
2024 tmp = (tm.tm_sec >> 1) & 0x1f; /* 5 bits of seconds */
2025 tmp |= (tm.tm_min & 0x3f) << 5; /* 6 bits of minutes */
2026 tmp |= (tm.tm_hour & 0x1f) << 11; /* 5 bits of hours */
2027 tmp |= (tm.tm_mday & 0x1f) << 16; /* 5 bits of day of month */
2028 tmp |= ((tm.tm_mon + 1) & 0xf) << 21; /* 4 bits of month */
2029 tmp |= ((tm.tm_year - 100) & 0x7f) << 25; /* 7 bits of year */
2033 static int iax2_call(struct ast_channel *c, char *dest, int timeout)
2035 struct sockaddr_in sin;
2040 char *secret = NULL;
2043 char *l=NULL, *n=NULL;
2044 struct iax_ie_data ied;
2045 char myrdest [5] = "s";
2046 char context[AST_MAX_EXTENSION] ="";
2047 char *portno = NULL;
2049 unsigned short callno = PTR_TO_CALLNO(c->pvt->pvt);
2051 char storedsecret[80];
2052 if ((c->_state != AST_STATE_DOWN) && (c->_state != AST_STATE_RESERVED)) {
2053 ast_log(LOG_WARNING, "Line is already in use (%s)?\n", c->name);
2056 strncpy(host, dest, sizeof(host)-1);
2058 strsep(&stringp, "/");
2059 /* If no destination extension specified, use 's' */
2060 rdest = strsep(&stringp, "/");
2064 /* Check for trailing options */
2065 opts = strsep(&stringp, "/");
2070 strsep(&stringp, "@");
2071 rcontext = strsep(&stringp, "@");
2073 strsep(&stringp, "@");
2074 username = strsep(&stringp, "@");
2076 /* Really the second argument is the host, not the username */
2084 username = strsep(&stringp, ":");
2085 secret = strsep(&stringp, ":");
2088 if (strsep(&stringp, ":")) {
2090 strsep(&stringp, ":");
2091 portno = strsep(&stringp, ":");
2093 if (create_addr(&sin, NULL, NULL, NULL, hname, context, NULL, NULL, storedsecret, sizeof(storedsecret) - 1)) {
2094 ast_log(LOG_WARNING, "No address associated with '%s'\n", hname);
2097 /* Keep track of the context for outgoing calls too */
2098 strncpy(c->context, context, sizeof(c->context) - 1);
2100 sin.sin_port = htons(atoi(portno));
2103 strncpy(cid, c->callerid, sizeof(cid) - 1);
2104 ast_callerid_parse(cid, &n, &l);
2106 ast_shrink_phone_number(l);
2108 /* Now build request */
2109 memset(&ied, 0, sizeof(ied));
2110 /* On new call, first IE MUST be IAX version of caller */
2111 iax_ie_append_short(&ied, IAX_IE_VERSION, IAX_PROTO_VERSION);
2112 iax_ie_append_str(&ied, IAX_IE_CALLED_NUMBER, rdest);
2113 if (strchr(opts, 'a')) {
2114 /* Request auto answer */
2115 iax_ie_append(&ied, IAX_IE_AUTOANSWER);
2118 iax_ie_append_str(&ied, IAX_IE_CALLING_NUMBER, l);
2120 iax_ie_append_str(&ied, IAX_IE_CALLING_NAME, n);
2121 if (iaxs[callno]->sendani && c->ani) {
2123 strncpy(cid, c->ani, sizeof(cid) - 1);
2124 ast_callerid_parse(cid, &n, &l);
2126 ast_shrink_phone_number(l);
2127 iax_ie_append_str(&ied, IAX_IE_CALLING_ANI, l);
2130 if (c->language && strlen(c->language))
2131 iax_ie_append_str(&ied, IAX_IE_LANGUAGE, c->language);
2132 if (c->dnid && strlen(c->dnid))
2133 iax_ie_append_str(&ied, IAX_IE_DNID, c->dnid);
2135 iax_ie_append_str(&ied, IAX_IE_CALLED_CONTEXT, rcontext);
2137 iax_ie_append_str(&ied, IAX_IE_USERNAME, username);
2138 if (!secret && strlen(storedsecret))
2139 secret = storedsecret;
2140 ast_mutex_lock(&iaxsl[callno]);
2141 if (strlen(c->context))
2142 strncpy(iaxs[callno]->context, c->context, sizeof(iaxs[callno]->context));
2144 if (secret[0] == '[') {
2145 /* This is an RSA key, not a normal secret */
2146 strncpy(iaxs[callno]->outkey, secret + 1, sizeof(iaxs[callno]->secret)-1);
2147 if (strlen(iaxs[callno]->outkey)) {
2148 iaxs[callno]->outkey[strlen(iaxs[callno]->outkey) - 1] = '\0';
2151 strncpy(iaxs[callno]->secret, secret, sizeof(iaxs[callno]->secret)-1);
2153 iax_ie_append_int(&ied, IAX_IE_FORMAT, c->nativeformats);
2154 iax_ie_append_int(&ied, IAX_IE_CAPABILITY, iaxs[callno]->capability);
2155 iax_ie_append_short(&ied, IAX_IE_ADSICPE, c->adsicpe);
2156 iax_ie_append_int(&ied, IAX_IE_DATETIME, iax2_datetime());
2157 /* Transmit the string in a "NEW" request */
2159 /* XXX We have no equivalent XXX */
2160 if (option_verbose > 2)
2161 ast_verbose(VERBOSE_PREFIX_3 "Calling using options '%s'\n", requeststr);
2163 if (iaxs[callno]->maxtime) {
2164 /* Initialize pingtime and auto-congest time */
2165 iaxs[callno]->pingtime = iaxs[callno]->maxtime / 2;
2166 iaxs[callno]->initid = ast_sched_add(sched, iaxs[callno]->maxtime * 2, auto_congest, CALLNO_TO_PTR(callno));
2168 send_command(iaxs[callno], AST_FRAME_IAX,
2169 IAX_COMMAND_NEW, 0, ied.buf, ied.pos, -1);
2170 ast_mutex_unlock(&iaxsl[callno]);
2171 ast_setstate(c, AST_STATE_RINGING);
2175 static int iax2_hangup(struct ast_channel *c)
2177 unsigned short callno = PTR_TO_CALLNO(c->pvt->pvt);
2179 ast_mutex_lock(&iaxsl[callno]);
2180 if (callno && iaxs[callno]) {
2181 ast_log(LOG_DEBUG, "We're hanging up %s now...\n", c->name);
2182 alreadygone = iaxs[callno]->alreadygone;
2183 /* Send the hangup unless we have had a transmission error or are already gone */
2184 if (!iaxs[callno]->error && !alreadygone)
2185 send_command_final(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_HANGUP, 0, NULL, 0, -1);
2186 /* Explicitly predestroy it */
2187 iax2_predestroy_nolock(callno);
2188 /* If we were already gone to begin with, destroy us now */
2190 ast_log(LOG_DEBUG, "Really destroying %s now...\n", c->name);
2191 iax2_destroy_nolock(callno);
2194 ast_mutex_unlock(&iaxsl[callno]);
2195 if (option_verbose > 2)
2196 ast_verbose(VERBOSE_PREFIX_3 "Hungup '%s'\n", c->name);
2200 static int iax2_setoption(struct ast_channel *c, int option, void *data, int datalen)
2202 struct ast_option_header *h;
2204 h = malloc(datalen + sizeof(struct ast_option_header));
2206 h->flag = AST_OPTION_FLAG_REQUEST;
2207 h->option = htons(option);
2208 memcpy(h->data, data, datalen);
2209 res = send_command_locked(PTR_TO_CALLNO(c->pvt->pvt), AST_FRAME_CONTROL,
2210 AST_CONTROL_OPTION, 0, (char *)h, datalen + sizeof(struct ast_option_header), -1);
2214 ast_log(LOG_WARNING, "Out of memory\n");
2218 static struct ast_frame *iax2_read(struct ast_channel *c)
2220 static struct ast_frame f = { AST_FRAME_NULL, };
2221 ast_log(LOG_NOTICE, "I should never be called!\n");
2225 static int iax2_start_transfer(unsigned short callno0, unsigned short callno1)
2228 struct iax_ie_data ied0;
2229 struct iax_ie_data ied1;
2230 unsigned int transferid = rand();
2231 memset(&ied0, 0, sizeof(ied0));
2232 iax_ie_append_addr(&ied0, IAX_IE_APPARENT_ADDR, &iaxs[callno1]->addr);
2233 iax_ie_append_short(&ied0, IAX_IE_CALLNO, iaxs[callno1]->peercallno);
2234 iax_ie_append_int(&ied0, IAX_IE_TRANSFERID, transferid);
2236 memset(&ied1, 0, sizeof(ied1));
2237 iax_ie_append_addr(&ied1, IAX_IE_APPARENT_ADDR, &iaxs[callno0]->addr);
2238 iax_ie_append_short(&ied1, IAX_IE_CALLNO, iaxs[callno0]->peercallno);
2239 iax_ie_append_int(&ied1, IAX_IE_TRANSFERID, transferid);
2241 res = send_command(iaxs[callno0], AST_FRAME_IAX, IAX_COMMAND_TXREQ, 0, ied0.buf, ied0.pos, -1);
2244 res = send_command(iaxs[callno1], AST_FRAME_IAX, IAX_COMMAND_TXREQ, 0, ied1.buf, ied1.pos, -1);
2247 iaxs[callno0]->transferring = TRANSFER_BEGIN;
2248 iaxs[callno1]->transferring = TRANSFER_BEGIN;
2252 static void lock_both(unsigned short callno0, unsigned short callno1)
2254 ast_mutex_lock(&iaxsl[callno0]);
2255 while (ast_mutex_trylock(&iaxsl[callno1])) {
2256 ast_mutex_unlock(&iaxsl[callno0]);
2258 ast_mutex_lock(&iaxsl[callno0]);
2262 static void unlock_both(unsigned short callno0, unsigned short callno1)
2264 ast_mutex_unlock(&iaxsl[callno1]);
2265 ast_mutex_unlock(&iaxsl[callno0]);
2268 static int iax2_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
2270 struct ast_channel *cs[3];
2271 struct ast_channel *who;
2274 int transferstarted=0;
2275 struct ast_frame *f;
2276 unsigned short callno0 = PTR_TO_CALLNO(c0->pvt->pvt);
2277 unsigned short callno1 = PTR_TO_CALLNO(c1->pvt->pvt);
2278 struct timeval waittimer = {0, 0}, tv;
2280 lock_both(callno0, callno1);
2281 /* Put them in native bridge mode */
2282 iaxs[callno0]->bridgecallno = callno1;
2283 iaxs[callno1]->bridgecallno = callno0;
2284 unlock_both(callno0, callno1);
2286 /* If not, try to bridge until we can execute a transfer, if we can */
2289 for (/* ever */;;) {
2290 /* Check in case we got masqueraded into */
2291 if ((c0->type != type) || (c1->type != type)) {
2292 if (option_verbose > 2)
2293 ast_verbose(VERBOSE_PREFIX_3 "Can't masquerade, we're different...\n");
2294 /* Remove from native mode */
2295 if (c0->type == type) {
2296 ast_mutex_lock(&iaxsl[callno0]);
2297 iaxs[callno0]->bridgecallno = 0;
2298 ast_mutex_unlock(&iaxsl[callno0]);
2300 if (c1->type == type) {
2301 ast_mutex_lock(&iaxsl[callno1]);
2302 iaxs[callno1]->bridgecallno = 0;
2303 ast_mutex_unlock(&iaxsl[callno1]);
2307 if (c0->nativeformats != c1->nativeformats) {
2308 ast_verbose(VERBOSE_PREFIX_3 "Operating with different codecs, can't native bridge...\n");
2309 /* Remove from native mode */
2310 lock_both(callno0, callno1);
2311 iaxs[callno0]->bridgecallno = 0;
2312 iaxs[callno1]->bridgecallno = 0;
2313 unlock_both(callno0, callno1);
2316 /* check if transfered and if we really want native bridging */
2317 if (!transferstarted && !iaxs[callno0]->notransfer && !iaxs[callno1]->notransfer) {
2318 /* Try the transfer */
2319 if (iax2_start_transfer(callno0, callno1))
2320 ast_log(LOG_WARNING, "Unable to start the transfer\n");
2321 transferstarted = 1;
2323 if ((iaxs[callno0]->transferring == TRANSFER_RELEASED) && (iaxs[callno1]->transferring == TRANSFER_RELEASED)) {
2324 /* Call has been transferred. We're no longer involved */
2325 gettimeofday(&tv, NULL);
2326 if (!waittimer.tv_sec && !waittimer.tv_usec) {
2327 waittimer.tv_sec = tv.tv_sec;
2328 waittimer.tv_usec = tv.tv_usec;
2329 } else if (tv.tv_sec - waittimer.tv_sec > IAX_LINGER_TIMEOUT) {
2330 c0->_softhangup |= AST_SOFTHANGUP_DEV;
2331 c1->_softhangup |= AST_SOFTHANGUP_DEV;
2339 who = ast_waitfor_n(cs, 2, &to);
2341 if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
2354 if ((f->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
2360 if ((f->frametype == AST_FRAME_VOICE) ||
2361 (f->frametype == AST_FRAME_TEXT) ||
2362 (f->frametype == AST_FRAME_VIDEO) ||
2363 (f->frametype == AST_FRAME_IMAGE) ||
2364 (f->frametype == AST_FRAME_DTMF)) {
2365 if ((f->frametype == AST_FRAME_DTMF) &&
2366 (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))) {
2368 if ((flags & AST_BRIDGE_DTMF_CHANNEL_0)) {
2371 /* Take out of conference mode */
2373 /* Remove from native mode */
2379 if (flags & AST_BRIDGE_DTMF_CHANNEL_1) {
2383 /* Remove from native mode */
2390 ast_log(LOG_DEBUG, "Read from %s\n", who->name);
2392 ast_log(LOG_DEBUG, "Servicing channel %s twice in a row?\n", last->name);
2404 /* Swap who gets priority */
2409 lock_both(callno0, callno1);
2410 iaxs[callno0]->bridgecallno = 0;
2411 iaxs[callno1]->bridgecallno = 0;
2412 unlock_both(callno0, callno1);
2416 static int iax2_answer(struct ast_channel *c)
2418 unsigned short callno = PTR_TO_CALLNO(c->pvt->pvt);
2420 ast_log(LOG_DEBUG, "Answering\n");
2421 return send_command_locked(callno, AST_FRAME_CONTROL, AST_CONTROL_ANSWER, 0, NULL, 0, -1);
2424 static int iax2_indicate(struct ast_channel *c, int condition)
2426 unsigned short callno = PTR_TO_CALLNO(c->pvt->pvt);
2428 ast_log(LOG_DEBUG, "Indicating condition %d\n", condition);
2429 return send_command_locked(callno, AST_FRAME_CONTROL, condition, 0, NULL, 0, -1);
2432 static int iax2_transfer(struct ast_channel *c, char *dest)
2434 unsigned short callno = PTR_TO_CALLNO(c->pvt->pvt);
2435 struct iax_ie_data ied;
2436 char tmp[256] = "", *context;
2437 strncpy(tmp, dest, sizeof(tmp) - 1);
2438 context = strchr(tmp, '@');
2443 memset(&ied, 0, sizeof(ied));
2444 iax_ie_append_str(&ied, IAX_IE_CALLED_NUMBER, tmp);
2446 iax_ie_append_str(&ied, IAX_IE_CALLED_CONTEXT, context);
2448 ast_log(LOG_DEBUG, "Transferring '%s' to '%s'\n", c->name, dest);
2449 return send_command_locked(callno, AST_FRAME_IAX, IAX_COMMAND_TRANSFER, 0, ied.buf, ied.pos, -1);
2453 static int iax2_write(struct ast_channel *c, struct ast_frame *f);
2455 static int iax2_getpeertrunk(struct sockaddr_in sin)
2457 struct iax2_peer *peer;
2459 ast_mutex_lock(&peerl.lock);
2462 if ((peer->addr.sin_addr.s_addr == sin.sin_addr.s_addr) &&
2463 (peer->addr.sin_port == sin.sin_port)) {
2469 ast_mutex_unlock(&peerl.lock);
2473 static struct ast_channel *ast_iax2_new(struct chan_iax2_pvt *i, int state, int capability)
2475 struct ast_channel *tmp;
2476 tmp = ast_channel_alloc(1);
2478 if (strlen(i->username))
2479 snprintf(tmp->name, sizeof(tmp->name), "IAX2[%s@%s]/%d", i->username, i->host, i->callno);
2481 snprintf(tmp->name, sizeof(tmp->name), "IAX2[%s]/%d", i->host, i->callno);
2483 /* We can support any format by default, until we get restricted */
2484 tmp->nativeformats = capability;
2485 tmp->readformat = 0;
2486 tmp->writeformat = 0;
2487 tmp->pvt->pvt = CALLNO_TO_PTR(i->callno);
2488 tmp->pvt->send_digit = iax2_digit;
2489 tmp->pvt->send_text = iax2_sendtext;
2490 tmp->pvt->send_image = iax2_sendimage;
2491 tmp->pvt->send_html = iax2_sendhtml;
2492 tmp->pvt->call = iax2_call;
2493 tmp->pvt->hangup = iax2_hangup;
2494 tmp->pvt->answer = iax2_answer;
2495 tmp->pvt->read = iax2_read;
2496 tmp->pvt->write = iax2_write;
2497 tmp->pvt->write_video = iax2_write;
2498 tmp->pvt->indicate = iax2_indicate;
2499 tmp->pvt->setoption = iax2_setoption;
2500 tmp->pvt->bridge = iax2_bridge;
2501 tmp->pvt->transfer = iax2_transfer;
2502 if (strlen(i->callerid))
2503 tmp->callerid = strdup(i->callerid);
2505 tmp->ani = strdup(i->ani);
2506 if (strlen(i->language))
2507 strncpy(tmp->language, i->language, sizeof(tmp->language)-1);
2508 if (strlen(i->dnid))
2509 tmp->dnid = strdup(i->dnid);
2510 if (strlen(i->accountcode))
2511 strncpy(tmp->accountcode, i->accountcode, sizeof(tmp->accountcode)-1);
2513 tmp->amaflags = i->amaflags;
2514 strncpy(tmp->context, i->context, sizeof(tmp->context)-1);
2515 strncpy(tmp->exten, i->exten, sizeof(tmp->exten)-1);
2516 tmp->adsicpe = i->peeradsicpe;
2517 tmp->pvt->fixup = iax2_fixup;
2519 i->capability = capability;
2520 ast_setstate(tmp, state);
2521 ast_mutex_lock(&usecnt_lock);
2523 ast_mutex_unlock(&usecnt_lock);
2524 ast_update_use_count();
2525 if (state != AST_STATE_DOWN) {
2526 if (ast_pbx_start(tmp)) {
2527 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
2536 static unsigned int calc_txpeerstamp(struct iax2_peer *peer)
2539 unsigned int mssincetx;
2541 gettimeofday(&tv, NULL);
2542 mssincetx = (tv.tv_sec - peer->lasttxtime.tv_sec) * 1000 + (tv.tv_usec - peer->lasttxtime.tv_usec) / 1000;
2543 if (mssincetx > 5000) {
2544 /* If it's been at least 5 seconds since the last time we transmitted on this trunk, reset our timers */
2545 peer->txtrunktime.tv_sec = tv.tv_sec;
2546 peer->txtrunktime.tv_usec = tv.tv_usec;
2548 /* Update last transmit time now */
2549 peer->lasttxtime.tv_sec = tv.tv_sec;
2550 peer->lasttxtime.tv_usec = tv.tv_usec;
2552 /* Calculate ms offset */
2553 ms = (tv.tv_sec - peer->txtrunktime.tv_sec) * 1000 + (tv.tv_usec - peer->txtrunktime.tv_usec) / 1000;
2555 /* We never send the same timestamp twice, so fudge a little if we must */
2556 if (ms == peer->lastsent)
2557 ms = peer->lastsent + 1;
2558 peer->lastsent = ms;
2562 static unsigned int fix_peerts(struct iax2_peer *peer, int callno, unsigned int ts)
2564 long ms; /* NOT unsigned */
2565 if (!iaxs[callno]->rxcore.tv_sec && !iaxs[callno]->rxcore.tv_usec) {
2566 /* Initialize rxcore time if appropriate */
2567 gettimeofday(&iaxs[callno]->rxcore, NULL);
2568 /* Round to nearest 20ms */
2569 iaxs[callno]->rxcore.tv_usec -= iaxs[callno]->rxcore.tv_usec % 20000;
2571 /* Calculate difference between trunk and channel */
2572 ms = (peer->rxtrunktime.tv_sec - iaxs[callno]->rxcore.tv_sec) * 1000 +
2573 (peer->rxtrunktime.tv_usec - iaxs[callno]->rxcore.tv_usec) / 1000;
2574 /* Return as the sum of trunk time and the difference between trunk and real time */
2578 static unsigned int calc_timestamp(struct chan_iax2_pvt *p, unsigned int ts, struct timeval *delivery)
2582 if (!p->offset.tv_sec && !p->offset.tv_usec) {
2583 gettimeofday(&p->offset, NULL);
2584 /* Round to nearest 20ms */
2585 p->offset.tv_usec -= p->offset.tv_usec % 20000;
2587 /* If the timestamp is specified, just send it as is */
2590 if (delivery && (delivery->tv_sec || delivery->tv_usec)) {
2591 ms = (delivery->tv_sec - p->offset.tv_sec) * 1000 + (delivery->tv_usec - p->offset.tv_usec) / 1000;
2593 gettimeofday(&tv, NULL);
2594 ms = (tv.tv_sec - p->offset.tv_sec) * 1000 + (tv.tv_usec - p->offset.tv_usec) / 1000;
2596 /* We never send the same timestamp twice, so fudge a little if we must */
2597 if (ms <= p->lastsent) {
2598 ms = p->lastsent + 1;
2604 #ifdef BRIDGE_OPTIMIZATION
2605 static unsigned int calc_fakestamp(struct chan_iax2_pvt *p1, struct chan_iax2_pvt *p2, unsigned int fakets)
2608 /* Receive from p1, send to p2 */
2610 /* Setup rxcore if necessary on outgoing channel */
2611 if (!p1->rxcore.tv_sec && !p1->rxcore.tv_usec)
2612 gettimeofday(&p1->rxcore, NULL);
2614 /* Setup txcore if necessary on outgoing channel */
2615 if (!p2->offset.tv_sec && !p2->offset.tv_usec)
2616 gettimeofday(&p2->offset, NULL);
2618 /* Now, ts is the timestamp of the original packet in the orignal context.
2619 Adding rxcore to it gives us when we would want the packet to be delivered normally.
2620 Subtracting txcore of the outgoing channel gives us what we'd expect */
2622 ms = (p1->rxcore.tv_sec - p2->offset.tv_sec) * 1000 + (p1->rxcore.tv_usec - p2->offset.tv_usec) / 1000;
2624 if (fakets <= p2->lastsent)
2625 fakets = p2->lastsent + 1;
2626 p2->lastsent = fakets;
2631 static unsigned int calc_rxstamp(struct chan_iax2_pvt *p)
2633 /* Returns where in "receive time" we are */
2636 /* Setup rxcore if necessary */
2637 if (!p->rxcore.tv_sec && !p->rxcore.tv_usec)
2638 gettimeofday(&p->rxcore, NULL);
2640 gettimeofday(&tv, NULL);
2641 ms = (tv.tv_sec - p->rxcore.tv_sec) * 1000 + (tv.tv_usec - p->rxcore.tv_usec) / 1000;
2645 static int iax2_send(struct chan_iax2_pvt *pvt, struct ast_frame *f, unsigned int ts, int seqno, int now, int transfer, int final)
2647 /* Queue a packet for delivery on a given private structure. Use "ts" for
2648 timestamp, or calculate if ts is 0. Send immediately without retransmission
2649 or delayed, with retransmission */
2650 struct ast_iax2_full_hdr *fh;
2651 struct ast_iax2_mini_hdr *mh;
2652 struct ast_iax2_video_hdr *vh;
2654 struct iax_frame fr2;
2655 unsigned char buffer[4096];
2657 struct iax_frame *fr;
2660 unsigned int lastsent;
2664 ast_log(LOG_WARNING, "No private structure for packet?\n");
2668 lastsent = pvt->lastsent;
2670 /* Calculate actual timestamp */
2671 fts = calc_timestamp(pvt, ts, (f->frametype == AST_FRAME_VOICE) ? &f->delivery : NULL);
2673 if ((pvt->trunk || ((fts & 0xFFFF0000L) == (lastsent & 0xFFFF0000L)))
2674 /* High two bits are the same on timestamp, or sending on a trunk */ &&
2675 (f->frametype == AST_FRAME_VOICE)
2676 /* is a voice frame */ &&
2677 (f->subclass == pvt->svoiceformat)
2678 /* is the same type */ ) {
2679 /* Force immediate rather than delayed transmission */
2681 /* Mark that mini-style frame is appropriate */
2684 if (((fts & 0xFFFF8000L) == (lastsent & 0xFFFF8000L)) &&
2685 (f->frametype == AST_FRAME_VIDEO) &&
2686 ((f->subclass & ~0x1) == pvt->svideoformat)) {
2690 /* Allocate an iax_frame */
2694 fr = iax_frame_new(DIRECTION_OUTGRESS, f->datalen);
2696 ast_log(LOG_WARNING, "Out of memory\n");
2699 /* Copy our prospective frame into our immediate or retransmitted wrapper */
2700 iax_frame_wrap(fr, f);
2704 ast_log(LOG_WARNING, "timestamp is 0?\n");
2706 iax2_frame_free(fr);
2709 fr->callno = pvt->callno;
2710 fr->transfer = transfer;
2713 /* We need a full frame */
2717 fr->oseqno = pvt->oseqno++;
2718 fr->iseqno = pvt->iseqno;
2719 fh = (struct ast_iax2_full_hdr *)(fr->af.data - sizeof(struct ast_iax2_full_hdr));
2720 fh->scallno = htons(fr->callno | IAX_FLAG_FULL);
2721 fh->ts = htonl(fr->ts);
2722 fh->oseqno = fr->oseqno;
2726 fh->iseqno = fr->iseqno;
2727 /* Keep track of the last thing we've acknowledged */
2729 pvt->aseqno = fr->iseqno;
2730 fh->type = fr->af.frametype & 0xFF;
2731 if (fr->af.frametype == AST_FRAME_VIDEO)
2732 fh->csub = compress_subclass(fr->af.subclass & ~0x1) | ((fr->af.subclass & 0x1) << 6);
2734 fh->csub = compress_subclass(fr->af.subclass);
2736 fr->dcallno = pvt->transfercallno;
2738 fr->dcallno = pvt->peercallno;
2739 fh->dcallno = htons(fr->dcallno);
2740 fr->datalen = fr->af.datalen + sizeof(struct ast_iax2_full_hdr);
2743 /* Retry after 2x the ping time has passed */
2744 fr->retrytime = pvt->pingtime * 2;
2745 if (fr->retrytime < MIN_RETRY_TIME)
2746 fr->retrytime = MIN_RETRY_TIME;
2747 if (fr->retrytime > MAX_RETRY_TIME)
2748 fr->retrytime = MAX_RETRY_TIME;
2749 /* Acks' don't get retried */
2750 if ((f->frametype == AST_FRAME_IAX) && (f->subclass == IAX_COMMAND_ACK))
2752 if (f->frametype == AST_FRAME_VOICE) {
2753 pvt->svoiceformat = f->subclass;
2755 if (f->frametype == AST_FRAME_VIDEO) {
2756 pvt->svideoformat = f->subclass & ~0x1;
2759 res = send_packet(fr);
2761 res = iax2_transmit(fr);
2764 /* Queue for transmission in a meta frame */
2765 if ((sizeof(pvt->trunkdata) - pvt->trunkdatalen) >= fr->af.datalen) {
2766 memcpy(pvt->trunkdata + pvt->trunkdatalen, fr->af.data, fr->af.datalen);
2767 pvt->trunkdatalen += fr->af.datalen;
2769 pvt->trunkerror = 0;
2771 if (!pvt->trunkerror)
2772 ast_log(LOG_WARNING, "Out of trunk data space on call number %d, dropping\n", pvt->callno);
2773 pvt->trunkerror = 1;
2776 } else if (fr->af.frametype == AST_FRAME_VIDEO) {
2777 /* Video frame have no sequence number */
2780 vh = (struct ast_iax2_video_hdr *)(fr->af.data - sizeof(struct ast_iax2_video_hdr));
2782 vh->callno = htons(0x8000 | fr->callno);
2783 vh->ts = htons((fr->ts & 0x7FFF) | (fr->af.subclass & 0x1 ? 0x8000 : 0));
2784 fr->datalen = fr->af.datalen + sizeof(struct ast_iax2_video_hdr);
2787 res = send_packet(fr);
2789 /* Mini-frames have no sequence number */
2792 /* Mini frame will do */
2793 mh = (struct ast_iax2_mini_hdr *)(fr->af.data - sizeof(struct ast_iax2_mini_hdr));
2794 mh->callno = htons(fr->callno);
2795 mh->ts = htons(fr->ts & 0xFFFF);
2796 fr->datalen = fr->af.datalen + sizeof(struct ast_iax2_mini_hdr);
2799 res = send_packet(fr);
2807 static int iax2_show_users(int fd, int argc, char *argv[])
2809 #define FORMAT "%-15.15s %-15.15s %-15.15s %-15.15s %-5.5s\n"
2810 #define FORMAT2 "%-15.15s %-15.15s %-15.15d %-15.15s %-5.5s\n"
2811 struct iax2_user *user;
2813 return RESULT_SHOWUSAGE;
2814 ast_mutex_lock(&userl.lock);
2815 ast_cli(fd, FORMAT, "Username", "Secret", "Authen", "Def.Context", "A/C");
2816 for(user=userl.users;user;user=user->next) {
2817 ast_cli(fd, FORMAT2, user->name, user->secret, user->authmethods,
2818 user->contexts ? user->contexts->context : context,
2819 user->ha ? "Yes" : "No");
2821 ast_mutex_unlock(&userl.lock);
2822 return RESULT_SUCCESS;
2827 static int iax2_show_peers(int fd, int argc, char *argv[])
2829 #define FORMAT2 "%-15.15s %-15.15s %s %-15.15s %-8s %-10s\n"
2830 #define FORMAT "%-15.15s %-15.15s %s %-15.15s %-5d%s %-10s\n"
2831 struct iax2_peer *peer;
2832 char name[256] = "";
2833 int registeredonly=0;
2834 if ((argc != 3) && (argc != 4))
2835 return RESULT_SHOWUSAGE;
2837 if (!strcasecmp(argv[3], "registered")) {
2840 return RESULT_SHOWUSAGE;
2842 ast_mutex_lock(&peerl.lock);
2843 ast_cli(fd, FORMAT2, "Name/Username", "Host", " ", "Mask", "Port", "Status");
2844 for (peer = peerl.peers;peer;peer = peer->next) {
2847 if (registeredonly && !peer->addr.sin_addr.s_addr)
2849 if (strlen(peer->username))
2850 snprintf(name, sizeof(name), "%s/%s", peer->name, peer->username);
2852 strncpy(name, peer->name, sizeof(name) - 1);
2854 if (peer->lastms < 0)
2855 strcpy(status, "UNREACHABLE");
2856 else if (peer->lastms > peer->maxms)
2857 snprintf(status, sizeof(status), "LAGGED (%d ms)", peer->lastms);
2858 else if (peer->lastms)
2859 snprintf(status, sizeof(status), "OK (%d ms)", peer->lastms);
2861 strcpy(status, "UNKNOWN");
2863 strcpy(status, "Unmonitored");
2864 strncpy(nm, inet_ntoa(peer->mask), sizeof(nm)-1);
2865 ast_cli(fd, FORMAT, name,
2866 peer->addr.sin_addr.s_addr ? inet_ntoa(peer->addr.sin_addr) : "(Unspecified)",
2867 peer->dynamic ? "(D)" : "(S)",
2869 ntohs(peer->addr.sin_port), peer->trunk ? "(T)" : " ", status);
2871 ast_mutex_unlock(&peerl.lock);
2872 return RESULT_SUCCESS;
2877 static int iax2_show_firmware(int fd, int argc, char *argv[])
2879 #define FORMAT2 "%-15.15s %-15.15s %-15.15s\n"
2880 #define FORMAT "%-15.15s %-15d %-15d\n"
2881 struct iax_firmware *cur;
2882 if ((argc != 3) && (argc != 4))
2883 return RESULT_SHOWUSAGE;
2884 ast_mutex_lock(&waresl.lock);
2886 ast_cli(fd, FORMAT2, "Device", "Version", "Size");
2887 for (cur = waresl.wares;cur;cur = cur->next) {
2888 if ((argc == 3) || (!strcasecmp(argv[3], cur->fwh->devname)))
2889 ast_cli(fd, FORMAT, cur->fwh->devname, ntohs(cur->fwh->version),
2890 ntohl(cur->fwh->datalen));
2892 ast_mutex_unlock(&waresl.lock);
2893 return RESULT_SUCCESS;
2898 /* JDG: callback to display iax peers in manager */
2899 static int manager_iax2_show_peers( struct mansession *s, struct message *m )
2901 char *a[] = { "iax2", "show", "users" };
2903 ret = iax2_show_peers( s->fd, 3, a );
2904 ast_cli( s->fd, "\r\n" );
2908 static char *regstate2str(int regstate)
2911 case REG_STATE_UNREGISTERED:
2912 return "Unregistered";
2913 case REG_STATE_REGSENT:
2914 return "Request Sent";
2915 case REG_STATE_AUTHSENT:
2916 return "Auth. Sent";
2917 case REG_STATE_REGISTERED:
2918 return "Registered";
2919 case REG_STATE_REJECTED:
2921 case REG_STATE_TIMEOUT:
2923 case REG_STATE_NOAUTH:
2924 return "No Authentication";
2930 static int iax2_show_registry(int fd, int argc, char *argv[])
2932 #define FORMAT2 "%-20.20s %-10.10s %-20.20s %8.8s %s\n"
2933 #define FORMAT "%-20.20s %-10.10s %-20.20s %8d %s\n"
2934 struct iax2_registry *reg;
2938 return RESULT_SHOWUSAGE;
2939 ast_mutex_lock(&peerl.lock);
2940 ast_cli(fd, FORMAT2, "Host", "Username", "Perceived", "Refresh", "State");
2941 for (reg = registrations;reg;reg = reg->next) {
2942 snprintf(host, sizeof(host), "%s:%d", inet_ntoa(reg->addr.sin_addr), ntohs(reg->addr.sin_port));
2943 if (reg->us.sin_addr.s_addr)
2944 snprintf(perceived, sizeof(perceived), "%s:%d", inet_ntoa(reg->us.sin_addr), ntohs(reg->us.sin_port));
2946 strcpy(perceived, "<Unregistered>");
2947 ast_cli(fd, FORMAT, host,
2948 reg->username, perceived, reg->refresh, regstate2str(reg->regstate));
2950 ast_mutex_unlock(&peerl.lock);
2951 return RESULT_SUCCESS;
2956 static int iax2_show_channels(int fd, int argc, char *argv[])
2958 #define FORMAT2 "%-15.15s %-10.10s %-11.11s %-11.11s %-7.7s %-6.6s %s\n"
2959 #define FORMAT "%-15.15s %-10.10s %5.5d/%5.5d %5.5d/%5.5d %-5.5dms %-4.4dms %-6.6s\n"
2963 return RESULT_SHOWUSAGE;
2964 ast_cli(fd, FORMAT2, "Peer", "Username", "ID (Lo/Rem)", "Seq (Tx/Rx)", "Lag", "Jitter", "Format");
2965 for (x=0;x<IAX_MAX_CALLS;x++) {
2966 ast_mutex_lock(&iaxsl[x]);
2968 ast_cli(fd, FORMAT, inet_ntoa(iaxs[x]->addr.sin_addr),
2969 strlen(iaxs[x]->username) ? iaxs[x]->username : "(None)",
2970 iaxs[x]->callno, iaxs[x]->peercallno,
2971 iaxs[x]->oseqno, iaxs[x]->iseqno,
2974 ast_getformatname(iaxs[x]->voiceformat) );
2977 ast_mutex_unlock(&iaxsl[x]);
2979 ast_cli(fd, "%d active IAX channel(s)\n", numchans);
2980 return RESULT_SUCCESS;
2985 static int iax2_do_trunk_debug(int fd, int argc, char *argv[])
2988 return RESULT_SHOWUSAGE;
2990 ast_cli(fd, "IAX2 Trunk Debug Requested\n");
2991 return RESULT_SUCCESS;
2994 static int iax2_do_debug(int fd, int argc, char *argv[])
2997 return RESULT_SHOWUSAGE;
2999 ast_cli(fd, "IAX2 Debugging Enabled\n");
3000 return RESULT_SUCCESS;
3003 static int iax2_no_debug(int fd, int argc, char *argv[])
3006 return RESULT_SHOWUSAGE;
3008 ast_cli(fd, "IAX2 Debugging Disabled\n");
3009 return RESULT_SUCCESS;
3014 static char show_users_usage[] =
3015 "Usage: iax2 show users\n"
3016 " Lists all users known to the IAX (Inter-Asterisk eXchange rev 2) subsystem.\n";
3018 static char show_channels_usage[] =
3019 "Usage: iax2 show channels\n"
3020 " Lists all currently active IAX channels.\n";
3022 static char show_peers_usage[] =
3023 "Usage: iax2 show peers\n"
3024 " Lists all known IAX peers.\n";
3026 static char show_firmware_usage[] =
3027 "Usage: iax2 show firmware\n"
3028 " Lists all known IAX firmware images.\n";
3030 static char show_reg_usage[] =
3031 "Usage: iax2 show registry\n"
3032 " Lists all registration requests and status.\n";
3034 static char debug_usage[] =
3035 "Usage: iax2 debug\n"
3036 " Enables dumping of IAX packets for debugging purposes\n";
3038 static char no_debug_usage[] =
3039 "Usage: iax2 no debug\n"
3040 " Disables dumping of IAX packets for debugging purposes\n";
3042 static char debug_trunk_usage[] =
3043 "Usage: iax2 trunk debug\n"
3044 " Requests current status of IAX trunking\n";
3046 static struct ast_cli_entry cli_show_users =
3047 { { "iax2", "show", "users", NULL }, iax2_show_users, "Show defined IAX users", show_users_usage };
3048 static struct ast_cli_entry cli_show_firmware =
3049 { { "iax2", "show", "firmware", NULL }, iax2_show_firmware, "Show available IAX firmwares", show_firmware_usage };
3050 static struct ast_cli_entry cli_show_channels =
3051 { { "iax2", "show", "channels", NULL }, iax2_show_channels, "Show active IAX channels", show_channels_usage };
3052 static struct ast_cli_entry cli_show_peers =
3053 { { "iax2", "show", "peers", NULL }, iax2_show_peers, "Show defined IAX peers", show_peers_usage };