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>
37 #include <asterisk/utils.h>
39 #include <arpa/inet.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <netinet/in_systm.h>
44 #include <netinet/ip.h>
46 #include <sys/signal.h>
56 #include <sys/types.h>
59 #include <sys/ioctl.h>
60 #include <linux/zaptel.h>
63 #include <mysql/mysql.h>
66 #include "iax2-parser.h"
67 #include "../astconf.h"
70 #define IPTOS_MINCOST 0x02
74 * Uncomment to try experimental IAX bridge optimization,
75 * designed to reduce latency when IAX calls cannot
79 #define BRIDGE_OPTIMIZATION
81 #define PTR_TO_CALLNO(a) ((unsigned short)(unsigned long)(a))
82 #define CALLNO_TO_PTR(a) ((void *)(unsigned long)(a))
84 #define DEFAULT_RETRY_TIME 1000
85 #define MEMORY_SIZE 100
86 #define DEFAULT_DROP 3
87 /* Flag to use with trunk calls, keeping these calls high up. It halves our effective use
88 but keeps the division between trunked and non-trunked better. */
89 #define TRUNK_CALL_START 0x4000
93 #define MIN_REUSE_TIME 60 /* Don't reuse a call number within 60 seconds */
95 /* Sample over last 100 units to determine historic jitter */
99 static ast_mutex_t mysqllock = AST_MUTEX_INITIALIZER;
101 static char mydbuser[80];
102 static char mydbpass[80];
103 static char mydbhost[80];
104 static char mydbname[80];
106 static char *desc = "Inter Asterisk eXchange (Ver 2)";
107 static char *tdesc = "Inter Asterisk eXchange Driver (Ver 2)";
108 static char *type = "IAX2";
110 static char context[80] = "default";
112 static char language[MAX_LANGUAGE] = "";
114 static int max_retries = 4;
115 static int ping_time = 20;
116 static int lagrq_time = 10;
117 static int maxtrunkcall = TRUNK_CALL_START;
118 static int maxnontrunkcall = 1;
119 static int maxjitterbuffer=3000;
120 static int trunkfreq = 20;
121 static int authdebug = 1;
122 static int iaxcompat = 0;
124 static int iaxdefaultdpcache=10 * 60; /* Cache dialplan entries for 10 minutes by default */
126 static int iaxdefaulttimeout = 5; /* Default to wait no more than 5 seconds for a reply to come back */
128 static int netsocket = -1;
132 static int expirey = IAX_DEFAULT_REG_EXPIRE;
134 static int timingfd = -1; /* Timing file descriptor */
137 static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
139 int (*iax2_regfunk)(char *username, int onoff) = NULL;
142 #define IAX_CAPABILITY_FULLBANDWIDTH 0xFFFF
144 #define IAX_CAPABILITY_MEDBANDWIDTH (IAX_CAPABILITY_FULLBANDWIDTH & \
145 ~AST_FORMAT_SLINEAR & \
149 #define IAX_CAPABILITY_LOWBANDWIDTH (IAX_CAPABILITY_MEDBANDWIDTH & \
153 #define IAX_CAPABILITY_LOWFREE (IAX_CAPABILITY_LOWBANDWIDTH & \
157 #define DEFAULT_MAXMS 2000 /* Must be faster than 2 seconds by default */
158 #define DEFAULT_FREQ_OK 60 * 1000 /* How often to check for the host to be up */
159 #define DEFAULT_FREQ_NOTOK 10 * 1000 /* How often to check, if the host is down... */
161 static struct io_context *io;
162 static struct sched_context *sched;
164 static int iax2_capability = IAX_CAPABILITY_FULLBANDWIDTH;
166 static int iax2_dropcount = DEFAULT_DROP;
168 static int use_jitterbuffer = 0;
170 static int iaxdebug = 0;
172 static int iaxtrunkdebug = 0;
174 static char accountcode[20];
175 static int amaflags = 0;
176 static int globalnotransfer = 0;
178 static pthread_t netthreadid = AST_PTHREADT_NULL;
180 #define IAX_STATE_STARTED (1 << 0)
181 #define IAX_STATE_AUTHENTICATED (1 << 1)
182 #define IAX_STATE_TBD (1 << 2)
184 struct iax2_context {
185 char context[AST_MAX_EXTENSION];
186 struct iax2_context *next;
193 char accountcode[20];
194 char inkeys[80]; /* Key(s) this user can use to authenticate to us */
195 char language[MAX_LANGUAGE];
202 char callerid[AST_MAX_EXTENSION];
204 struct iax2_context *contexts;
205 struct iax2_user *next;
213 char outkey[80]; /* What key we use to talk to this peer */
214 char context[AST_MAX_EXTENSION]; /* Default context (for transfer really) */
215 char mailbox[AST_MAX_EXTENSION]; /* Mailbox */
216 struct sockaddr_in addr;
220 /* Dynamic Registration fields */
221 int dynamic; /* If this is a dynamic peer */
222 struct sockaddr_in defaddr; /* Default address if there is one */
223 int authmethods; /* Authentication methods (IAX_AUTH_*) */
224 char inkeys[80]; /* Key(s) this peer can use to authenticate to us */
227 /* Suggested caller id if registering */
228 char callerid[AST_MAX_EXTENSION];
229 /* Whether or not to send ANI */
231 int expire; /* Schedule entry for expirey */
232 int expirey; /* How soon to expire */
233 int capability; /* Capability */
234 int delme; /* I need to be deleted */
235 int temponly; /* I'm only a temp */
236 int trunk; /* Treat as an IAX trunking */
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 #define IAX2_TRUNK_PREFACE (sizeof(struct iax_frame) + sizeof(struct ast_iax2_meta_hdr) + sizeof(struct ast_iax2_meta_trunk_hdr))
251 static struct iax2_trunk_peer {
253 struct sockaddr_in addr;
254 struct timeval txtrunktime; /* Transmit trunktime */
255 struct timeval rxtrunktime; /* Receive trunktime */
256 struct timeval lasttxtime; /* Last transmitted trunktime */
257 struct timeval trunkact; /* Last trunk activity */
258 unsigned int lastsent; /* Last sent time */
259 /* Trunk data and length */
260 unsigned char *trunkdata;
261 unsigned int trunkdatalen;
262 unsigned int trunkdataalloc;
263 struct iax2_trunk_peer *next;
269 static ast_mutex_t tpeerlock = AST_MUTEX_INITIALIZER;
271 struct iax_firmware {
272 struct iax_firmware *next;
276 struct ast_iax2_firmware_header *fwh;
280 #define REG_STATE_UNREGISTERED 0
281 #define REG_STATE_REGSENT 1
282 #define REG_STATE_AUTHSENT 2
283 #define REG_STATE_REGISTERED 3
284 #define REG_STATE_REJECTED 4
285 #define REG_STATE_TIMEOUT 5
286 #define REG_STATE_NOAUTH 6
288 #define TRANSFER_NONE 0
289 #define TRANSFER_BEGIN 1
290 #define TRANSFER_READY 2
291 #define TRANSFER_RELEASED 3
292 #define TRANSFER_PASSTHROUGH 4
294 struct iax2_registry {
295 struct sockaddr_in addr; /* Who we connect to for registration purposes */
297 char secret[80]; /* Password or key name in []'s */
299 int expire; /* Sched ID of expiration */
300 int refresh; /* How often to refresh */
302 int messages; /* Message count */
303 int callno; /* Associated call number if applicable */
304 struct sockaddr_in us; /* Who the server thinks we are */
305 struct iax2_registry *next;
308 static struct iax2_registry *registrations;
310 /* Don't retry more frequently than every 10 ms, or less frequently than every 5 seconds */
311 #define MIN_RETRY_TIME 100
312 #define MAX_RETRY_TIME 10000
313 #define MAX_JITTER_BUFFER 50
315 #define DEFAULT_TRUNKDATA 640 * 10 /* 40ms, uncompressed linear * 10 channels */
316 #define MAX_TRUNKDATA 640 * 200 /* 40ms, uncompressed linear * 200 channels */
318 /* If we have more than this much excess real jitter buffer, srhink it. */
319 static int max_jitter_buffer = MAX_JITTER_BUFFER;
321 struct chan_iax2_pvt {
322 /* Pipes for communication. pipe[1] belongs to the
323 network thread (write), and pipe[0] belongs to the individual
325 /* Whether or not we Quelch audio */
327 /* Last received voice format */
329 /* Last received voice format */
331 /* Last sent voice format */
333 /* Last sent video format */
335 /* What we are capable of sending */
337 /* Last received timestamp */
339 /* Last sent timestamp - never send the same timestamp twice in a single call */
340 unsigned int lastsent;
341 /* Next outgoing timestamp if everything is good */
342 unsigned int nextpred;
344 unsigned int pingtime;
345 /* Max time for initial response */
348 struct sockaddr_in addr;
349 /* Our call number */
350 unsigned short callno;
352 unsigned short peercallno;
353 /* Peer selected format */
355 /* Peer capability */
357 /* timeval that we base our transmission on */
358 struct timeval offset;
359 /* timeval that we base our delivery on */
360 struct timeval rxcore;
361 /* Historical delivery time */
362 int history[MEMORY_SIZE];
363 /* Current base jitterbuffer */
365 /* Current jitter measure */
367 /* Historic jitter value */
371 /* Error, as discovered by the manager */
373 /* Owner if we have one */
374 struct ast_channel *owner;
375 /* What's our state? */
377 /* Expirey (optional) */
379 /* Next outgoing sequence number */
380 unsigned char oseqno;
381 /* Next sequence number they have not yet acknowledged */
382 unsigned char rseqno;
383 /* Next incoming sequence number */
384 unsigned char iseqno;
385 /* Last incoming sequence number we have acknowledged */
386 unsigned char aseqno;
389 /* Default Context */
391 /* Caller ID if available */
393 /* Hidden Caller ID (i.e. ANI) if appropriate */
395 /* Whether or not ani should be transmitted in addition to Caller*ID */
397 /* Whether to request autoanswer */
401 /* Requested Extension */
402 char exten[AST_MAX_EXTENSION];
403 /* Expected Username */
405 /* Expected Secret */
407 /* permitted authentication methods */
411 /* Public keys permitted keys for incoming authentication */
413 /* Private key for outgoing authentication */
415 /* Preferred language */
416 char language[MAX_LANGUAGE];
417 /* Hostname/peername for naming purposes */
419 /* Associated registry */
420 struct iax2_registry *reg;
421 /* Associated peer for poking */
422 struct iax2_peer *peerpoke;
424 /* Transferring status */
426 /* Transfer identifier */
428 /* Already disconnected */
430 /* Who we are IAX transfering to */
431 struct sockaddr_in transfer;
432 /* What's the new call number for the transfer */
433 unsigned short transfercallno;
435 /* Status of knowledge of peer ADSI capability */
438 /* Who we are bridged to */
439 unsigned short bridgecallno;
440 unsigned int bridgesfmt;
441 struct ast_trans_pvt *bridgetrans;
443 int pingid; /* Transmit PING request */
444 int lagid; /* Retransmit lag request */
445 int autoid; /* Auto hangup for Dialplan requestor */
446 int initid; /* Initial peer auto-congest ID (based on qualified peers) */
447 char dproot[AST_MAX_EXTENSION];
448 char accountcode[20];
450 /* This is part of a trunk interface */
452 struct iax2_dpcache *dpentries;
453 int notransfer; /* do we want native bridging */
456 static struct ast_iax2_queue {
457 struct iax_frame *head;
458 struct iax_frame *tail;
463 static struct ast_user_list {
464 struct iax2_user *users;
468 static struct ast_peer_list {
469 struct iax2_peer *peers;
473 static struct ast_firmware_list {
474 struct iax_firmware *wares;
478 /* Extension exists */
479 #define CACHE_FLAG_EXISTS (1 << 0)
480 /* Extension is non-existant */
481 #define CACHE_FLAG_NONEXISTANT (1 << 1)
482 /* Extension can exist */
483 #define CACHE_FLAG_CANEXIST (1 << 2)
484 /* Waiting to hear back response */
485 #define CACHE_FLAG_PENDING (1 << 3)
487 #define CACHE_FLAG_TIMEOUT (1 << 4)
488 /* Request transmitted */
489 #define CACHE_FLAG_TRANSMITTED (1 << 5)
491 #define CACHE_FLAG_UNKNOWN (1 << 6)
493 #define CACHE_FLAG_MATCHMORE (1 << 7)
495 static struct iax2_dpcache {
496 char peercontext[AST_MAX_EXTENSION];
497 char exten[AST_MAX_EXTENSION];
499 struct timeval expirey;
501 unsigned short callno;
503 struct iax2_dpcache *next;
504 struct iax2_dpcache *peer; /* For linking in peers */
507 static ast_mutex_t dpcache_lock;
509 static void iax_debug_output(const char *data)
515 static void iax_error_output(const char *data)
517 ast_log(LOG_WARNING, data);
520 /* XXX We probably should use a mutex when working with this XXX */
521 static struct chan_iax2_pvt *iaxs[IAX_MAX_CALLS];
522 static ast_mutex_t iaxsl[IAX_MAX_CALLS];
523 static struct timeval lastused[IAX_MAX_CALLS];
526 static int send_command(struct chan_iax2_pvt *, char, int, unsigned int, char *, int, int);
527 static int send_command_locked(unsigned short callno, char, int, unsigned int, char *, int, int);
528 static int send_command_immediate(struct chan_iax2_pvt *, char, int, unsigned int, char *, int, int);
529 static int send_command_final(struct chan_iax2_pvt *, char, int, unsigned int, char *, int, int);
530 static int send_command_transfer(struct chan_iax2_pvt *, char, int, unsigned int, char *, int);
532 static unsigned int calc_timestamp(struct chan_iax2_pvt *p, unsigned int ts, struct ast_frame *f);
534 static int send_ping(void *data)
536 int callno = (long)data;
537 /* Ping only if it's real, not if it's bridged */
539 #ifdef BRIDGE_OPTIMIZATION
540 if (!iaxs[callno]->bridgecallno)
542 send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_PING, 0, NULL, 0, -1);
548 static int send_lagrq(void *data)
550 int callno = (long)data;
551 /* Ping only if it's real not if it's bridged */
553 #ifdef BRIDGE_OPTIMIZATION
554 if (!iaxs[callno]->bridgecallno)
556 send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_LAGRQ, 0, NULL, 0, -1);
562 static unsigned char compress_subclass(int subclass)
566 /* If it's 128 or smaller, just return it */
567 if (subclass < IAX_FLAG_SC_LOG)
569 /* Otherwise find its power */
570 for (x = 0; x < IAX_MAX_SHIFT; x++) {
571 if (subclass & (1 << x)) {
573 ast_log(LOG_WARNING, "Can't compress subclass %d\n", subclass);
579 return power | IAX_FLAG_SC_LOG;
582 static int uncompress_subclass(unsigned char csub)
584 /* If the SC_LOG flag is set, return 2^csub otherwise csub */
585 if (csub & IAX_FLAG_SC_LOG) {
586 /* special case for 'compressed' -1 */
590 return 1 << (csub & ~IAX_FLAG_SC_LOG & IAX_MAX_SHIFT);
596 static int iax2_getpeername(struct sockaddr_in sin, char *host, int len, int lockpeer)
598 struct iax2_peer *peer;
601 ast_mutex_lock(&peerl.lock);
604 if ((peer->addr.sin_addr.s_addr == sin.sin_addr.s_addr) &&
605 (peer->addr.sin_port == sin.sin_port)) {
606 strncpy(host, peer->name, len-1);
613 ast_mutex_unlock(&peerl.lock);
617 static struct chan_iax2_pvt *new_iax(struct sockaddr_in *sin, int lockpeer)
619 struct chan_iax2_pvt *tmp;
620 tmp = malloc(sizeof(struct chan_iax2_pvt));
622 memset(tmp, 0, sizeof(struct chan_iax2_pvt));
625 tmp->transfercallno = 0;
626 tmp->bridgecallno = 0;
631 /* strncpy(tmp->context, context, sizeof(tmp->context)-1); */
632 strncpy(tmp->exten, "s", sizeof(tmp->exten)-1);
633 if (!iax2_getpeername(*sin, tmp->host, sizeof(tmp->host), lockpeer))
634 snprintf(tmp->host, sizeof(tmp->host), "%s:%d", inet_ntoa(sin->sin_addr), ntohs(sin->sin_port));
639 static int get_samples(struct ast_frame *f)
642 switch(f->subclass) {
643 case AST_FORMAT_SPEEX:
644 samples = 160; /* XXX Not necessarily true XXX */
646 case AST_FORMAT_G723_1:
647 samples = 240 /* XXX Not necessarily true XXX */;
649 case AST_FORMAT_ILBC:
650 samples = 240 * (f->datalen / 50);
653 samples = 160 * (f->datalen / 33);
655 case AST_FORMAT_G729A:
656 samples = 160 * (f->datalen / 20);
658 case AST_FORMAT_SLINEAR:
659 samples = f->datalen / 2;
661 case AST_FORMAT_LPC10:
663 samples += (((char *)(f->data))[7] & 0x1) * 8;
665 case AST_FORMAT_ULAW:
666 samples = f->datalen;
668 case AST_FORMAT_ALAW:
669 samples = f->datalen;
671 case AST_FORMAT_ADPCM:
672 case AST_FORMAT_G726:
673 samples = f->datalen *2;
676 ast_log(LOG_WARNING, "Don't know how to calculate samples on %d packets\n", f->subclass);
681 static struct iax_frame *iaxfrdup2(struct iax_frame *fr)
683 /* Malloc() a copy of a frame */
684 struct iax_frame *new = iax_frame_new(DIRECTION_INGRESS, fr->af.datalen);
686 memcpy(new, fr, sizeof(struct iax_frame));
687 iax_frame_wrap(new, &fr->af);
690 new->direction = DIRECTION_INGRESS;
696 #define NEW_PREVENT 0
700 static int match(struct sockaddr_in *sin, unsigned short callno, unsigned short dcallno, struct chan_iax2_pvt *cur)
702 if ((cur->addr.sin_addr.s_addr == sin->sin_addr.s_addr) &&
703 (cur->addr.sin_port == sin->sin_port)) {
704 /* This is the main host */
705 if ((cur->peercallno == callno) ||
706 ((dcallno == cur->callno) && !cur->peercallno)) {
707 /* That's us. Be sure we keep track of the peer call number */
711 if ((cur->transfer.sin_addr.s_addr == sin->sin_addr.s_addr) &&
712 (cur->transfer.sin_port == sin->sin_port) && (cur->transferring)) {
713 /* We're transferring */
714 if (dcallno == cur->callno)
720 static void update_max_trunk(void)
722 int max = TRUNK_CALL_START;
724 /* XXX Prolly don't need locks here XXX */
725 for (x=TRUNK_CALL_START;x<IAX_MAX_CALLS - 1; x++) {
731 ast_log(LOG_DEBUG, "New max trunk callno is %d\n", max);
734 static void update_max_nontrunk(void)
738 /* XXX Prolly don't need locks here XXX */
739 for (x=1;x<TRUNK_CALL_START - 1; x++) {
743 maxnontrunkcall = max;
745 ast_log(LOG_DEBUG, "New max nontrunk callno is %d\n", max);
748 static int make_trunk(unsigned short callno, int locked)
753 if (iaxs[callno]->oseqno) {
754 ast_log(LOG_WARNING, "Can't make trunk once a call has started!\n");
757 if (callno & TRUNK_CALL_START) {
758 ast_log(LOG_WARNING, "Call %d is already a trunk\n", callno);
761 gettimeofday(&now, NULL);
762 for (x=TRUNK_CALL_START;x<IAX_MAX_CALLS - 1; x++) {
763 ast_mutex_lock(&iaxsl[x]);
764 if (!iaxs[x] && ((now.tv_sec - lastused[x].tv_sec) > MIN_REUSE_TIME)) {
765 iaxs[x] = iaxs[callno];
768 /* Update the two timers that should have been started */
769 if (iaxs[x]->pingid > -1)
770 ast_sched_del(sched, iaxs[x]->pingid);
771 if (iaxs[x]->lagid > -1)
772 ast_sched_del(sched, iaxs[x]->lagid);
773 iaxs[x]->pingid = ast_sched_add(sched, ping_time * 1000, send_ping, (void *)x);
774 iaxs[x]->lagid = ast_sched_add(sched, lagrq_time * 1000, send_lagrq, (void *)x);
776 ast_mutex_unlock(&iaxsl[callno]);
779 ast_mutex_unlock(&iaxsl[x]);
782 ast_mutex_unlock(&iaxsl[x]);
784 if (x >= IAX_MAX_CALLS - 1) {
785 ast_log(LOG_WARNING, "Unable to trunk call: Insufficient space\n");
788 ast_log(LOG_DEBUG, "Made call %d into trunk call %d\n", callno, x);
789 /* We move this call from a non-trunked to a trunked call */
791 update_max_nontrunk();
795 static int find_callno(unsigned short callno, unsigned short dcallno, struct sockaddr_in *sin, int new, int lockpeer)
800 if (new <= NEW_ALLOW) {
801 /* Look for an existing connection first */
802 for (x=1;(res < 1) && (x<maxnontrunkcall);x++) {
803 ast_mutex_lock(&iaxsl[x]);
805 /* Look for an exact match */
806 if (match(sin, callno, dcallno, iaxs[x])) {
810 ast_mutex_unlock(&iaxsl[x]);
812 for (x=TRUNK_CALL_START;(res < 1) && (x<maxtrunkcall);x++) {
813 ast_mutex_lock(&iaxsl[x]);
815 /* Look for an exact match */
816 if (match(sin, callno, dcallno, iaxs[x])) {
820 ast_mutex_unlock(&iaxsl[x]);
823 if ((res < 1) && (new >= NEW_ALLOW)) {
824 gettimeofday(&now, NULL);
825 for (x=1;x<TRUNK_CALL_START;x++) {
826 /* Find first unused call number that hasn't been used in a while */
827 ast_mutex_lock(&iaxsl[x]);
828 if (!iaxs[x] && ((now.tv_sec - lastused[x].tv_sec) > MIN_REUSE_TIME)) break;
829 ast_mutex_unlock(&iaxsl[x]);
831 /* We've still got lock held if we found a spot */
832 if (x >= TRUNK_CALL_START) {
833 ast_log(LOG_WARNING, "No more space\n");
836 iaxs[x] = new_iax(sin, lockpeer);
837 update_max_nontrunk();
840 ast_log(LOG_DEBUG, "Creating new call structure %d\n", x);
841 iaxs[x]->addr.sin_port = sin->sin_port;
842 iaxs[x]->addr.sin_family = sin->sin_family;
843 iaxs[x]->addr.sin_addr.s_addr = sin->sin_addr.s_addr;
844 iaxs[x]->peercallno = callno;
846 iaxs[x]->pingtime = DEFAULT_RETRY_TIME;
847 iaxs[x]->expirey = expirey;
848 iaxs[x]->pingid = ast_sched_add(sched, ping_time * 1000, send_ping, (void *)x);
849 iaxs[x]->lagid = ast_sched_add(sched, lagrq_time * 1000, send_lagrq, (void *)x);
850 iaxs[x]->amaflags = amaflags;
851 iaxs[x]->notransfer = globalnotransfer;
852 strncpy(iaxs[x]->accountcode, accountcode, sizeof(iaxs[x]->accountcode)-1);
854 ast_log(LOG_WARNING, "Out of resources\n");
855 ast_mutex_unlock(&iaxsl[x]);
858 ast_mutex_unlock(&iaxsl[x]);
864 static void iax2_frame_free(struct iax_frame *fr)
866 if (fr->retrans > -1)
867 ast_sched_del(sched, fr->retrans);
871 static int iax2_queue_frame(int callno, struct ast_frame *f)
873 /* Assumes lock for callno is already held... */
875 if (iaxs[callno] && iaxs[callno]->owner) {
876 if (ast_mutex_trylock(&iaxs[callno]->owner->lock)) {
877 /* Avoid deadlock by pausing and trying again */
878 ast_mutex_unlock(&iaxsl[callno]);
880 ast_mutex_lock(&iaxsl[callno]);
882 ast_queue_frame(iaxs[callno]->owner, f);
883 ast_mutex_unlock(&iaxs[callno]->owner->lock);
892 static void destroy_firmware(struct iax_firmware *cur)
896 munmap(cur->fwh, ntohl(cur->fwh->datalen) + sizeof(*(cur->fwh)));
902 static int try_firmware(char *s)
905 struct iax_firmware *cur;
908 struct ast_iax2_firmware_header *fwh, fwh2;
909 struct MD5Context md5;
910 unsigned char sum[16];
911 res = stat(s, &stbuf);
913 ast_log(LOG_WARNING, "Failed to stat '%s': %s\n", s, strerror(errno));
916 /* Make sure it's not a directory */
917 if (S_ISDIR(stbuf.st_mode))
919 fd = open(s, O_RDONLY);
921 ast_log(LOG_WARNING, "Cannot open '%s': %s\n", s, strerror(errno));
924 if ((res = read(fd, &fwh2, sizeof(fwh2))) != sizeof(fwh2)) {
925 ast_log(LOG_WARNING, "Unable to read firmware header in '%s'\n", s);
929 if (ntohl(fwh2.magic) != IAX_FIRMWARE_MAGIC) {
930 ast_log(LOG_WARNING, "'%s' is not a valid firmware file\n", s);
934 if (ntohl(fwh2.datalen) != (stbuf.st_size - sizeof(fwh2))) {
935 ast_log(LOG_WARNING, "Invalid data length in firmware '%s'\n", s);
939 if (fwh2.devname[sizeof(fwh2.devname) - 1] || ast_strlen_zero(fwh2.devname)) {
940 ast_log(LOG_WARNING, "No or invalid device type specified for '%s'\n", s);
944 fwh = mmap(NULL, stbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
946 ast_log(LOG_WARNING, "mmap failed: %s\n", strerror(errno));
951 MD5Update(&md5, fwh->data, ntohl(fwh->datalen));
953 if (memcmp(sum, fwh->chksum, sizeof(sum))) {
954 ast_log(LOG_WARNING, "Firmware file '%s' fails checksum\n", s);
955 munmap(fwh, stbuf.st_size);
961 if (!strcmp(cur->fwh->devname, fwh->devname)) {
962 /* Found a candidate */
963 if (cur->dead || (ntohs(cur->fwh->version) < ntohs(fwh->version)))
964 /* The version we have on loaded is older, load this one instead */
966 /* This version is no newer than what we have. Don't worry about it.
967 We'll consider it a proper load anyhow though */
968 munmap(fwh, stbuf.st_size);
975 /* Allocate a new one and link it */
976 cur = malloc(sizeof(struct iax_firmware));
978 memset(cur, 0, sizeof(struct iax_firmware));
980 cur->next = waresl.wares;
986 munmap(cur->fwh, cur->mmaplen);
992 cur->mmaplen = stbuf.st_size;
998 static int iax_check_version(char *dev)
1001 struct iax_firmware *cur;
1002 if (dev && !ast_strlen_zero(dev)) {
1003 ast_mutex_lock(&waresl.lock);
1006 if (!strcmp(dev, cur->fwh->devname)) {
1007 res = ntohs(cur->fwh->version);
1012 ast_mutex_unlock(&waresl.lock);
1017 static int iax_firmware_append(struct iax_ie_data *ied, const unsigned char *dev, unsigned int desc)
1020 unsigned int bs = desc & 0xff;
1021 unsigned int start = (desc >> 8) & 0xffffff;
1023 struct iax_firmware *cur;
1024 if (dev && !ast_strlen_zero(dev) && bs) {
1026 ast_mutex_lock(&waresl.lock);
1029 if (!strcmp(dev, cur->fwh->devname)) {
1030 iax_ie_append_int(ied, IAX_IE_FWBLOCKDESC, desc);
1031 if (start < ntohl(cur->fwh->datalen)) {
1032 bytes = ntohl(cur->fwh->datalen) - start;
1035 iax_ie_append_raw(ied, IAX_IE_FWBLOCKDATA, cur->fwh->data + start, bytes);
1038 iax_ie_append(ied, IAX_IE_FWBLOCKDATA);
1048 ast_mutex_unlock(&waresl.lock);
1054 static void reload_firmware(void)
1056 struct iax_firmware *cur, *curl, *curp;
1061 /* Mark all as dead */
1062 ast_mutex_lock(&waresl.lock);
1068 /* Now that we've freed them, load the new ones */
1069 snprintf(dir, sizeof(dir), "%s/firmware/iax", (char *)ast_config_AST_VAR_DIR);
1072 while((de = readdir(fwd))) {
1073 if (de->d_name[0] != '.') {
1074 snprintf(fn, sizeof(fn), "%s/%s", dir, de->d_name);
1075 if (!try_firmware(fn)) {
1076 if (option_verbose > 1)
1077 ast_verbose(VERBOSE_PREFIX_2 "Loaded firmware '%s'\n", de->d_name);
1083 ast_log(LOG_WARNING, "Error opening firmware directory '%s': %s\n", dir, strerror(errno));
1085 /* Clean up leftovers */
1097 destroy_firmware(curl);
1102 ast_mutex_unlock(&waresl.lock);
1105 static int iax2_send(struct chan_iax2_pvt *pvt, struct ast_frame *f, unsigned int ts, int seqno, int now, int transfer, int final);
1107 static int __do_deliver(void *data)
1109 /* Just deliver the packet by using queueing. This is called by
1110 the IAX thread with the iaxsl lock held. */
1111 struct iax_frame *fr = data;
1113 if (iaxs[fr->callno] && !iaxs[fr->callno]->alreadygone) {
1114 if (fr->af.frametype == AST_FRAME_IAX &&
1115 fr->af.subclass == IAX_COMMAND_LAGRQ) {
1116 /* send a lag response to a lag request that has
1117 * gone through our jitter buffer */
1118 fr->af.subclass = IAX_COMMAND_LAGRP;
1119 iax2_send(iaxs[fr->callno], &fr->af, fr->ts, -1, 0, 0, 0);
1121 iax2_queue_frame(fr->callno, &fr->af);
1124 /* Free our iax frame */
1125 iax2_frame_free(fr);
1126 /* And don't run again */
1130 static int do_deliver(void *data)
1132 /* Locking version of __do_deliver */
1133 struct iax_frame *fr = data;
1134 int callno = fr->callno;
1136 ast_mutex_lock(&iaxsl[callno]);
1137 res = __do_deliver(data);
1138 ast_mutex_unlock(&iaxsl[callno]);
1142 static int handle_error(void)
1144 /* XXX Ideally we should figure out why an error occured and then abort those
1145 rather than continuing to try. Unfortunately, the published interface does
1146 not seem to work XXX */
1148 struct sockaddr_in *sin;
1151 struct sock_extended_err e;
1156 m.msg_controllen = sizeof(e);
1158 res = recvmsg(netsocket, &m, MSG_ERRQUEUE);
1160 ast_log(LOG_WARNING, "Error detected, but unable to read error: %s\n", strerror(errno));
1162 if (m.msg_controllen) {
1163 sin = (struct sockaddr_in *)SO_EE_OFFENDER(&e);
1165 ast_log(LOG_WARNING, "Receive error from %s\n", inet_ntoa(sin->sin_addr));
1167 ast_log(LOG_WARNING, "No address detected??\n");
1169 ast_log(LOG_WARNING, "Local error: %s\n", strerror(e.ee_errno));
1176 static int send_packet(struct iax_frame *f)
1179 /* Called with iaxsl held */
1181 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));
1182 /* Don't send if there was an error, but return error instead */
1184 ast_log(LOG_WARNING, "Call number = %d\n", f->callno);
1187 if (!iaxs[f->callno])
1189 if (iaxs[f->callno]->error)
1193 iax_showframe(f, NULL, 0, &iaxs[f->callno]->transfer, f->datalen - sizeof(struct ast_iax2_full_hdr));
1194 res = sendto(netsocket, f->data, f->datalen, 0,(struct sockaddr *)&iaxs[f->callno]->transfer,
1195 sizeof(iaxs[f->callno]->transfer));
1198 iax_showframe(f, NULL, 0, &iaxs[f->callno]->addr, f->datalen - sizeof(struct ast_iax2_full_hdr));
1199 res = sendto(netsocket, f->data, f->datalen, 0,(struct sockaddr *)&iaxs[f->callno]->addr,
1200 sizeof(iaxs[f->callno]->addr));
1204 ast_log(LOG_DEBUG, "Received error: %s\n", strerror(errno));
1212 static int iax2_predestroy(int callno)
1214 struct ast_channel *c;
1215 struct chan_iax2_pvt *pvt;
1216 ast_mutex_lock(&iaxsl[callno]);
1219 ast_mutex_unlock(&iaxsl[callno]);
1222 if (!pvt->alreadygone) {
1223 /* No more pings or lagrq's */
1224 if (pvt->pingid > -1)
1225 ast_sched_del(sched, pvt->pingid);
1226 if (pvt->lagid > -1)
1227 ast_sched_del(sched, pvt->lagid);
1228 if (pvt->autoid > -1)
1229 ast_sched_del(sched, pvt->autoid);
1230 if (pvt->initid > -1)
1231 ast_sched_del(sched, pvt->initid);
1236 pvt->alreadygone = 1;
1240 c->_softhangup |= AST_SOFTHANGUP_DEV;
1242 ast_queue_hangup(c);
1244 ast_mutex_lock(&usecnt_lock);
1247 ast_log(LOG_WARNING, "Usecnt < 0???\n");
1248 ast_mutex_unlock(&usecnt_lock);
1250 ast_mutex_unlock(&iaxsl[callno]);
1251 ast_update_use_count();
1255 static int iax2_predestroy_nolock(int callno)
1258 ast_mutex_unlock(&iaxsl[callno]);
1259 res = iax2_predestroy(callno);
1260 ast_mutex_lock(&iaxsl[callno]);
1264 static void iax2_destroy(int callno)
1266 struct chan_iax2_pvt *pvt;
1267 struct iax_frame *cur;
1268 struct ast_channel *owner;
1271 ast_mutex_lock(&iaxsl[callno]);
1273 gettimeofday(&lastused[callno], NULL);
1280 if (ast_mutex_trylock(&owner->lock)) {
1281 ast_log(LOG_NOTICE, "Avoiding IAX destroy deadlock\n");
1282 ast_mutex_unlock(&iaxsl[callno]);
1288 iaxs[callno] = NULL;
1292 /* No more pings or lagrq's */
1293 if (pvt->pingid > -1)
1294 ast_sched_del(sched, pvt->pingid);
1295 if (pvt->lagid > -1)
1296 ast_sched_del(sched, pvt->lagid);
1297 if (pvt->autoid > -1)
1298 ast_sched_del(sched, pvt->autoid);
1299 if (pvt->initid > -1)
1300 ast_sched_del(sched, pvt->initid);
1305 if (pvt->bridgetrans)
1306 ast_translator_free_path(pvt->bridgetrans);
1307 pvt->bridgetrans = NULL;
1310 pvt->alreadygone = 1;
1313 /* If there's an owner, prod it to give up */
1314 owner->_softhangup |= AST_SOFTHANGUP_DEV;
1315 ast_queue_hangup(owner);
1318 for (cur = iaxq.head; cur ; cur = cur->next) {
1319 /* Cancel any pending transmissions */
1320 if (cur->callno == pvt->callno)
1324 pvt->reg->callno = 0;
1330 ast_mutex_unlock(&owner->lock);
1332 ast_mutex_unlock(&iaxsl[callno]);
1333 if (callno & 0x4000)
1336 static void iax2_destroy_nolock(int callno)
1338 /* Actually it's easier to unlock, kill it, and relock */
1339 ast_mutex_unlock(&iaxsl[callno]);
1340 iax2_destroy(callno);
1341 ast_mutex_lock(&iaxsl[callno]);
1344 static int update_packet(struct iax_frame *f)
1346 /* Called with iaxsl lock held, and iaxs[callno] non-NULL */
1347 struct ast_iax2_full_hdr *fh = f->data;
1348 /* Mark this as a retransmission */
1349 fh->dcallno = ntohs(IAX_FLAG_RETRANS | f->dcallno);
1351 f->iseqno = iaxs[f->callno]->iseqno;
1352 fh->iseqno = f->iseqno;
1356 static int attempt_transmit(void *data)
1358 /* Attempt to transmit the frame to the remote peer...
1359 Called without iaxsl held. */
1360 struct iax_frame *f = data;
1362 int callno = f->callno;
1363 /* Make sure this call is still active */
1365 ast_mutex_lock(&iaxsl[callno]);
1366 if ((f->callno) && iaxs[f->callno]) {
1367 if ((f->retries < 0) /* Already ACK'd */ ||
1368 (f->retries >= max_retries) /* Too many attempts */) {
1369 /* Record an error if we've transmitted too many times */
1370 if (f->retries >= max_retries) {
1372 /* Transfer timeout */
1373 send_command(iaxs[f->callno], AST_FRAME_IAX, IAX_COMMAND_TXREJ, 0, NULL, 0, -1);
1374 } else if (f->final) {
1376 iax2_destroy_nolock(f->callno);
1378 if (iaxs[f->callno]->owner)
1379 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);
1380 iaxs[f->callno]->error = ETIMEDOUT;
1381 if (iaxs[f->callno]->owner) {
1382 struct ast_frame fr = { 0, };
1384 fr.frametype = AST_FRAME_CONTROL;
1385 fr.subclass = AST_CONTROL_HANGUP;
1386 iax2_queue_frame(f->callno, &fr);
1388 if (iaxs[f->callno]->reg) {
1389 memset(&iaxs[f->callno]->reg->us, 0, sizeof(iaxs[f->callno]->reg->us));
1390 iaxs[f->callno]->reg->regstate = REG_STATE_TIMEOUT;
1391 iaxs[f->callno]->reg->refresh = IAX_DEFAULT_REG_EXPIRE;
1393 iax2_destroy_nolock(f->callno);
1400 /* Update it if it needs it */
1402 /* Attempt transmission */
1405 /* Try again later after 10 times as long */
1407 if (f->retrytime > MAX_RETRY_TIME)
1408 f->retrytime = MAX_RETRY_TIME;
1409 /* Transfer messages max out at one second */
1410 if (f->transfer && (f->retrytime > 1000))
1411 f->retrytime = 1000;
1412 f->retrans = ast_sched_add(sched, f->retrytime, attempt_transmit, f);
1415 /* Make sure it gets freed */
1420 ast_mutex_unlock(&iaxsl[callno]);
1421 /* Do not try again */
1423 /* Don't attempt delivery, just remove it from the queue */
1424 ast_mutex_lock(&iaxq.lock);
1426 f->prev->next = f->next;
1428 iaxq.head = f->next;
1430 f->next->prev = f->prev;
1432 iaxq.tail = f->prev;
1434 ast_mutex_unlock(&iaxq.lock);
1436 /* Free the IAX frame */
1442 static int iax2_set_jitter(int fd, int argc, char *argv[])
1444 if ((argc != 4) && (argc != 5))
1445 return RESULT_SHOWUSAGE;
1447 max_jitter_buffer = atoi(argv[3]);
1448 if (max_jitter_buffer < 0)
1449 max_jitter_buffer = 0;
1452 if ((atoi(argv[3]) >= 0) && (atoi(argv[3]) < IAX_MAX_CALLS)) {
1453 if (iaxs[atoi(argv[3])]) {
1454 iaxs[atoi(argv[3])]->jitterbuffer = atoi(argv[4]);
1455 if (iaxs[atoi(argv[3])]->jitterbuffer < 0)
1456 iaxs[atoi(argv[3])]->jitterbuffer = 0;
1458 ast_cli(fd, "No such call '%d'\n", atoi(argv[3]));
1460 ast_cli(fd, "%d is not a valid call number\n", atoi(argv[3]));
1463 return RESULT_SUCCESS;
1466 static char jitter_usage[] =
1467 "Usage: iax set jitter [callid] <value>\n"
1468 " If used with a callid, it sets the jitter buffer to the given static\n"
1469 "value (until its next calculation). If used without a callid, the value is used\n"
1470 "to establish the maximum excess jitter buffer that is permitted before the jitter\n"
1471 "buffer size is reduced.";
1473 static int iax2_show_stats(int fd, int argc, char *argv[])
1475 struct iax_frame *cur;
1476 int cnt = 0, dead=0, final=0;
1478 return RESULT_SHOWUSAGE;
1479 for (cur = iaxq.head; cur ; cur = cur->next) {
1480 if (cur->retries < 0)
1486 ast_cli(fd, " IAX Statistics\n");
1487 ast_cli(fd, "---------------------\n");
1488 ast_cli(fd, "Outstanding frames: %d (%d ingress, %d outgress)\n", iax_get_frames(), iax_get_iframes(), iax_get_oframes());
1489 ast_cli(fd, "Packets in transmit queue: %d dead, %d final, %d total\n", dead, final, cnt);
1490 return RESULT_SUCCESS;
1493 static int iax2_show_cache(int fd, int argc, char *argv[])
1495 struct iax2_dpcache *dp;
1496 char tmp[1024], *pc;
1500 gettimeofday(&tv, NULL);
1501 ast_mutex_lock(&dpcache_lock);
1503 ast_cli(fd, "%-20.20s %-12.12s %-9.9s %-8.8s %s\n", "Peer/Context", "Exten", "Exp.", "Wait.", "Flags");
1505 s = dp->expirey.tv_sec - tv.tv_sec;
1507 if (dp->flags & CACHE_FLAG_EXISTS)
1508 strcat(tmp, "EXISTS|");
1509 if (dp->flags & CACHE_FLAG_NONEXISTANT)
1510 strcat(tmp, "NONEXISTANT|");
1511 if (dp->flags & CACHE_FLAG_CANEXIST)
1512 strcat(tmp, "CANEXIST|");
1513 if (dp->flags & CACHE_FLAG_PENDING)
1514 strcat(tmp, "PENDING|");
1515 if (dp->flags & CACHE_FLAG_TIMEOUT)
1516 strcat(tmp, "TIMEOUT|");
1517 if (dp->flags & CACHE_FLAG_TRANSMITTED)
1518 strcat(tmp, "TRANSMITTED|");
1519 if (dp->flags & CACHE_FLAG_MATCHMORE)
1520 strcat(tmp, "MATCHMORE|");
1521 if (dp->flags & CACHE_FLAG_UNKNOWN)
1522 strcat(tmp, "UNKNOWN|");
1523 /* Trim trailing pipe */
1524 if (!ast_strlen_zero(tmp))
1525 tmp[strlen(tmp) - 1] = '\0';
1527 strcpy(tmp, "(none)");
1529 pc = strchr(dp->peercontext, '@');
1531 pc = dp->peercontext;
1534 for (x=0;x<sizeof(dp->waiters) / sizeof(dp->waiters[0]); x++)
1535 if (dp->waiters[x] > -1)
1538 ast_cli(fd, "%-20.20s %-12.12s %-9d %-8d %s\n", pc, dp->exten, s, y, tmp);
1540 ast_cli(fd, "%-20.20s %-12.12s %-9.9s %-8d %s\n", pc, dp->exten, "(expired)", y, tmp);
1543 ast_mutex_unlock(&dpcache_lock);
1544 return RESULT_SUCCESS;
1547 static char show_stats_usage[] =
1548 "Usage: iax show stats\n"
1549 " Display statistics on IAX channel driver.\n";
1552 static char show_cache_usage[] =
1553 "Usage: iax show cache\n"
1554 " Display currently cached IAX Dialplan results.\n";
1556 static struct ast_cli_entry cli_set_jitter =
1557 { { "iax2", "set", "jitter", NULL }, iax2_set_jitter, "Sets IAX jitter buffer", jitter_usage };
1559 static struct ast_cli_entry cli_show_stats =
1560 { { "iax2", "show", "stats", NULL }, iax2_show_stats, "Display IAX statistics", show_stats_usage };
1562 static struct ast_cli_entry cli_show_cache =
1563 { { "iax2", "show", "cache", NULL }, iax2_show_cache, "Display IAX cached dialplan", show_cache_usage };
1565 static unsigned int calc_rxstamp(struct chan_iax2_pvt *p);
1567 #ifdef BRIDGE_OPTIMIZATION
1568 static unsigned int calc_fakestamp(struct chan_iax2_pvt *from, struct chan_iax2_pvt *to, unsigned int ts);
1570 static int forward_delivery(struct iax_frame *fr)
1572 struct chan_iax2_pvt *p1, *p2;
1573 p1 = iaxs[fr->callno];
1574 p2 = iaxs[p1->bridgecallno];
1579 /* Fix relative timestamp */
1580 fr->ts = calc_fakestamp(p1, p2, fr->ts);
1581 /* Now just send it send on the 2nd one
1582 with adjusted timestamp */
1583 return iax2_send(p2, &fr->af, fr->ts, -1, 0, 0, 0);
1587 static int schedule_delivery(struct iax_frame *fr, int reallydeliver, int updatehistory)
1590 int drops[MEMORY_SIZE];
1591 int min, max=0, maxone=0,y,z, match;
1592 /* ms is a measure of the "lateness" of the packet relative to the first
1593 packet we received, which always has a lateness of 1. Called by
1594 IAX thread, with iaxsl lock held. */
1595 ms = calc_rxstamp(iaxs[fr->callno]) - fr->ts;
1598 /* What likely happened here is that our counter has circled but we haven't
1599 gotten the update from the main packet. We'll just pretend that we did, and
1600 update the timestamp appropriately. */
1606 /* We got this packet out of order. Lets add 65536 to it to bring it into our new
1612 fr->af.delivery.tv_sec = iaxs[fr->callno]->rxcore.tv_sec;
1613 fr->af.delivery.tv_usec = iaxs[fr->callno]->rxcore.tv_usec;
1614 fr->af.delivery.tv_sec += fr->ts / 1000;
1615 fr->af.delivery.tv_usec += (fr->ts % 1000) * 1000;
1616 if (fr->af.delivery.tv_usec >= 1000000) {
1617 fr->af.delivery.tv_usec -= 1000000;
1618 fr->af.delivery.tv_sec += 1;
1622 /* Rotate our history queue of "lateness". Don't worry about those initial
1623 zeros because the first entry will always be zero */
1624 if (updatehistory) {
1625 for (x=0;x<MEMORY_SIZE - 1;x++)
1626 iaxs[fr->callno]->history[x] = iaxs[fr->callno]->history[x+1];
1627 /* Add a history entry for this one */
1628 iaxs[fr->callno]->history[x] = ms;
1631 /* Initialize the minimum to reasonable values. It's too much
1632 work to do the same for the maximum, repeatedly */
1633 min=iaxs[fr->callno]->history[0];
1634 for (z=0;z < iax2_dropcount + 1;z++) {
1635 /* Start very optimistic ;-) */
1637 for (x=0;x<MEMORY_SIZE;x++) {
1638 if (max < iaxs[fr->callno]->history[x]) {
1639 /* We have a candidate new maximum value. Make
1640 sure it's not in our drop list */
1642 for (y=0;!match && (y<z);y++)
1643 match |= (drops[y] == x);
1645 /* It's not in our list, use it as the new maximum */
1646 max = iaxs[fr->callno]->history[x];
1652 /* On our first pass, find the minimum too */
1653 if (min > iaxs[fr->callno]->history[x])
1654 min = iaxs[fr->callno]->history[x];
1661 /* Just for reference, keep the "jitter" value, the difference between the
1662 earliest and the latest. */
1663 iaxs[fr->callno]->jitter = max - min;
1665 /* IIR filter for keeping track of historic jitter, but always increase
1666 historic jitter immediately for increase */
1668 if (iaxs[fr->callno]->jitter > iaxs[fr->callno]->historicjitter )
1669 iaxs[fr->callno]->historicjitter = iaxs[fr->callno]->jitter;
1671 iaxs[fr->callno]->historicjitter = GAMMA * (double)iaxs[fr->callno]->jitter + (1-GAMMA) *
1672 iaxs[fr->callno]->historicjitter;
1674 /* If our jitter buffer is too big (by a significant margin), then we slowly
1675 shrink it by about 1 ms each time to avoid letting the change be perceived */
1676 if (max < iaxs[fr->callno]->jitterbuffer - max_jitter_buffer)
1677 iaxs[fr->callno]->jitterbuffer -= 2;
1681 /* Constrain our maximum jitter buffer appropriately */
1682 if (max > min + maxjitterbuffer) {
1684 ast_log(LOG_DEBUG, "Constraining buffer from %d to %d + %d\n", max, min , maxjitterbuffer);
1685 max = min + maxjitterbuffer;
1689 /* If our jitter buffer is smaller than our maximum delay, grow the jitter
1690 buffer immediately to accomodate it (and a little more). */
1691 if (max > iaxs[fr->callno]->jitterbuffer)
1692 iaxs[fr->callno]->jitterbuffer = max
1693 /* + ((float)iaxs[fr->callno]->jitter) * 0.1 */;
1697 ast_log(LOG_DEBUG, "min = %d, max = %d, jb = %d, lateness = %d\n", min, max, iaxs[fr->callno]->jitterbuffer, ms);
1699 /* Subtract the lateness from our jitter buffer to know how long to wait
1700 before sending our packet. */
1701 ms = iaxs[fr->callno]->jitterbuffer - ms;
1703 if (!use_jitterbuffer)
1706 /* If the caller just wanted us to update, return now */
1712 ast_log(LOG_DEBUG, "Calculated ms is %d\n", ms);
1713 /* Don't deliver it more than 4 ms late */
1714 if ((ms > -4) || (fr->af.frametype != AST_FRAME_VOICE)) {
1718 ast_log(LOG_DEBUG, "Dropping voice packet since %d ms is, too old\n", ms);
1719 /* Free our iax frame */
1720 iax2_frame_free(fr);
1724 ast_log(LOG_DEBUG, "Scheduling delivery in %d ms\n", ms);
1725 fr->retrans = ast_sched_add(sched, ms, do_deliver, fr);
1730 static int iax2_transmit(struct iax_frame *fr)
1732 /* Lock the queue and place this packet at the end */
1735 /* By setting this to 0, the network thread will send it for us, and
1736 queue retransmission if necessary */
1738 ast_mutex_lock(&iaxq.lock);
1745 iaxq.tail->next = fr;
1746 fr->prev = iaxq.tail;
1750 ast_mutex_unlock(&iaxq.lock);
1751 /* Wake up the network thread */
1752 pthread_kill(netthreadid, SIGURG);
1758 static int iax2_digit(struct ast_channel *c, char digit)
1760 return send_command_locked(PTR_TO_CALLNO(c->pvt->pvt), AST_FRAME_DTMF, digit, 0, NULL, 0, -1);
1763 static int iax2_sendtext(struct ast_channel *c, char *text)
1766 return send_command_locked(PTR_TO_CALLNO(c->pvt->pvt), AST_FRAME_TEXT,
1767 0, 0, text, strlen(text) + 1, -1);
1770 static int iax2_sendimage(struct ast_channel *c, struct ast_frame *img)
1772 return send_command_locked(PTR_TO_CALLNO(c->pvt->pvt), AST_FRAME_IMAGE, img->subclass, 0, img->data, img->datalen, -1);
1775 static int iax2_sendhtml(struct ast_channel *c, int subclass, char *data, int datalen)
1777 return send_command_locked(PTR_TO_CALLNO(c->pvt->pvt), AST_FRAME_HTML, subclass, 0, data, datalen, -1);
1780 static int iax2_fixup(struct ast_channel *oldchannel, struct ast_channel *newchan)
1782 unsigned short callno = PTR_TO_CALLNO(newchan->pvt->pvt);
1783 ast_mutex_lock(&iaxsl[callno]);
1785 iaxs[callno]->owner = newchan;
1787 ast_log(LOG_WARNING, "Uh, this isn't a good sign...\n");
1788 ast_mutex_unlock(&iaxsl[callno]);
1792 #ifdef MYSQL_FRIENDS
1794 static void mysql_update_peer(char *peer, struct sockaddr_in *sin)
1796 if (mysql && (strlen(peer) < 128)) {
1800 name = alloca(strlen(peer) * 2 + 1);
1802 mysql_real_escape_string(mysql, name, peer, strlen(peer));
1803 snprintf(query, sizeof(query), "UPDATE iaxfriends SET ipaddr=\"%s\", port=\"%d\", regseconds=\"%ld\" WHERE name=\"%s\"",
1804 inet_ntoa(sin->sin_addr), ntohs(sin->sin_port), nowtime, name);
1805 ast_mutex_lock(&mysqllock);
1806 if (mysql_real_query(mysql, query, strlen(query)))
1807 ast_log(LOG_WARNING, "Unable to update database\n");
1809 ast_mutex_unlock(&mysqllock);
1813 static struct iax2_peer *mysql_peer(char *peer)
1815 struct iax2_peer *p;
1818 p = malloc(sizeof(struct iax2_peer));
1819 memset(p, 0, sizeof(struct iax2_peer));
1820 if (mysql && (strlen(peer) < 128)) {
1825 time_t regseconds, nowtime;
1827 MYSQL_FIELD *fields;
1829 name = alloca(strlen(peer) * 2 + 1);
1830 mysql_real_escape_string(mysql, name, peer, strlen(peer));
1831 snprintf(query, sizeof(query), "SELECT name, secret, context, ipaddr, port, regseconds FROM iaxfriends WHERE name=\"%s\"", name);
1832 ast_mutex_lock(&mysqllock);
1833 mysql_query(mysql, query);
1834 if ((result = mysql_store_result(mysql))) {
1835 if ((rowval = mysql_fetch_row(result))) {
1836 numfields = mysql_num_fields(result);
1837 fields = mysql_fetch_fields(result);
1839 for (x=0;x<numfields;x++) {
1841 if (!strcasecmp(fields[x].name, "secret")) {
1842 strncpy(p->secret, rowval[x], sizeof(p->secret));
1843 } else if (!strcasecmp(fields[x].name, "context")) {
1844 strncpy(p->context, rowval[x], sizeof(p->context) - 1);
1845 } else if (!strcasecmp(fields[x].name, "ipaddr")) {
1846 inet_aton(rowval[x], &p->addr.sin_addr);
1847 } else if (!strcasecmp(fields[x].name, "port")) {
1848 if (sscanf(rowval[x], "%i", &port) != 1)
1850 p->addr.sin_port = htons(port);
1851 } else if (!strcasecmp(fields[x].name, "regseconds")) {
1852 if (sscanf(rowval[x], "%li", ®seconds) != 1)
1858 if ((nowtime - regseconds) > IAX_DEFAULT_REG_EXPIRE)
1859 memset(&p->addr, 0, sizeof(p->addr));
1861 mysql_free_result(result);
1864 ast_mutex_unlock(&mysqllock);
1870 strncpy(p->name, peer, sizeof(p->name) - 1);
1874 p->capability = iax2_capability;
1875 p->authmethods = IAX_AUTH_MD5 | IAX_AUTH_PLAINTEXT;
1879 static struct iax2_user *mysql_user(char *user)
1881 struct iax2_user *p;
1882 struct iax2_context *con;
1885 p = malloc(sizeof(struct iax2_user));
1886 memset(p, 0, sizeof(struct iax2_user));
1887 con = malloc(sizeof(struct iax2_context));
1888 memset(con, 0, sizeof(struct iax2_context));
1889 strcpy(con->context, "default");
1891 if (mysql && (strlen(user) < 128)) {
1896 MYSQL_FIELD *fields;
1898 name = alloca(strlen(user) * 2 + 1);
1899 mysql_real_escape_string(mysql, name, user, strlen(user));
1900 snprintf(query, sizeof(query), "SELECT name, secret, context, ipaddr, port, regseconds, accountcode FROM iaxfriends WHERE name=\"%s\"", name);
1901 ast_mutex_lock(&mysqllock);
1902 mysql_query(mysql, query);
1903 if ((result = mysql_store_result(mysql))) {
1904 if ((rowval = mysql_fetch_row(result))) {
1905 numfields = mysql_num_fields(result);
1906 fields = mysql_fetch_fields(result);
1908 for (x=0;x<numfields;x++) {
1910 if (!strcasecmp(fields[x].name, "secret")) {
1911 strncpy(p->secret, rowval[x], sizeof(p->secret));
1912 } else if (!strcasecmp(fields[x].name, "context")) {
1913 strncpy(p->contexts->context, rowval[x], sizeof(p->contexts->context) - 1);
1914 } else if (!strcasecmp(fields[x].name, "accountcode")) {
1915 strncpy(p->accountcode, rowval[x], sizeof(p->accountcode));
1920 mysql_free_result(result);
1923 ast_mutex_unlock(&mysqllock);
1931 strncpy(p->name, user, sizeof(p->name) - 1);
1933 p->capability = iax2_capability;
1934 p->authmethods = IAX_AUTH_MD5 | IAX_AUTH_PLAINTEXT;
1938 #endif /* MYSQL_FRIENDS */
1940 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)
1942 struct ast_hostent ahp; struct hostent *hp;
1943 struct iax2_peer *p;
1951 sin->sin_family = AF_INET;
1952 ast_mutex_lock(&peerl.lock);
1955 if (!strcasecmp(p->name, peer)) {
1960 ast_mutex_unlock(&peerl.lock);
1961 #ifdef MYSQL_FRIENDS
1963 p = mysql_peer(peer);
1967 if ((p->addr.sin_addr.s_addr || p->defaddr.sin_addr.s_addr) &&
1968 (!p->maxms || ((p->lastms > 0) && (p->lastms <= p->maxms)))) {
1970 *sendani = p->sendani; /* Whether we transmit ANI */
1972 *maxtime = p->maxms; /* Max time they should take */
1974 strncpy(context, p->context, AST_MAX_EXTENSION - 1);
1978 *capability = p->capability;
1980 strncpy(secret, p->secret, seclen);
1981 if (p->addr.sin_addr.s_addr) {
1982 sin->sin_addr = p->addr.sin_addr;
1983 sin->sin_port = p->addr.sin_port;
1985 sin->sin_addr = p->defaddr.sin_addr;
1986 sin->sin_port = p->defaddr.sin_port;
1989 *notransfer=p->notransfer;
1997 hp = ast_gethostbyname(peer, &ahp);
1999 memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
2000 sin->sin_port = htons(IAX_DEFAULT_PORTNO);
2003 ast_log(LOG_WARNING, "No such host: %s\n", peer);
2013 static int auto_congest(void *nothing)
2015 int callno = PTR_TO_CALLNO(nothing);
2016 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_CONGESTION };
2017 ast_mutex_lock(&iaxsl[callno]);
2019 iaxs[callno]->initid = -1;
2020 iax2_queue_frame(callno, &f);
2021 ast_log(LOG_NOTICE, "Auto-congesting call due to slow response\n");
2023 ast_mutex_unlock(&iaxsl[callno]);
2027 static unsigned int iax2_datetime(void)
2033 localtime_r(&t, &tm);
2034 tmp = (tm.tm_sec >> 1) & 0x1f; /* 5 bits of seconds */
2035 tmp |= (tm.tm_min & 0x3f) << 5; /* 6 bits of minutes */
2036 tmp |= (tm.tm_hour & 0x1f) << 11; /* 5 bits of hours */
2037 tmp |= (tm.tm_mday & 0x1f) << 16; /* 5 bits of day of month */
2038 tmp |= ((tm.tm_mon + 1) & 0xf) << 21; /* 4 bits of month */
2039 tmp |= ((tm.tm_year - 100) & 0x7f) << 25; /* 7 bits of year */
2043 static int iax2_call(struct ast_channel *c, char *dest, int timeout)
2045 struct sockaddr_in sin;
2050 char *secret = NULL;
2053 char *l=NULL, *n=NULL;
2054 struct iax_ie_data ied;
2055 char myrdest [5] = "s";
2056 char context[AST_MAX_EXTENSION] ="";
2057 char *portno = NULL;
2059 unsigned short callno = PTR_TO_CALLNO(c->pvt->pvt);
2061 char storedsecret[80];
2062 if ((c->_state != AST_STATE_DOWN) && (c->_state != AST_STATE_RESERVED)) {
2063 ast_log(LOG_WARNING, "Line is already in use (%s)?\n", c->name);
2066 strncpy(host, dest, sizeof(host)-1);
2068 strsep(&stringp, "/");
2069 /* If no destination extension specified, use 's' */
2070 rdest = strsep(&stringp, "/");
2074 /* Check for trailing options */
2075 opts = strsep(&stringp, "/");
2080 strsep(&stringp, "@");
2081 rcontext = strsep(&stringp, "@");
2083 strsep(&stringp, "@");
2084 username = strsep(&stringp, "@");
2086 /* Really the second argument is the host, not the username */
2094 username = strsep(&stringp, ":");
2095 secret = strsep(&stringp, ":");
2098 if (strsep(&stringp, ":")) {
2100 strsep(&stringp, ":");
2101 portno = strsep(&stringp, ":");
2103 if (create_addr(&sin, NULL, NULL, NULL, hname, context, NULL, NULL, storedsecret, sizeof(storedsecret) - 1)) {
2104 ast_log(LOG_WARNING, "No address associated with '%s'\n", hname);
2107 /* Keep track of the context for outgoing calls too */
2108 strncpy(c->context, context, sizeof(c->context) - 1);
2110 sin.sin_port = htons(atoi(portno));
2113 strncpy(cid, c->callerid, sizeof(cid) - 1);
2114 ast_callerid_parse(cid, &n, &l);
2116 ast_shrink_phone_number(l);
2118 /* Now build request */
2119 memset(&ied, 0, sizeof(ied));
2120 /* On new call, first IE MUST be IAX version of caller */
2121 iax_ie_append_short(&ied, IAX_IE_VERSION, IAX_PROTO_VERSION);
2122 iax_ie_append_str(&ied, IAX_IE_CALLED_NUMBER, rdest);
2123 if (strchr(opts, 'a')) {
2124 /* Request auto answer */
2125 iax_ie_append(&ied, IAX_IE_AUTOANSWER);
2128 iax_ie_append_str(&ied, IAX_IE_CALLING_NUMBER, l);
2130 iax_ie_append_str(&ied, IAX_IE_CALLING_NAME, n);
2131 if (iaxs[callno]->sendani && c->ani) {
2133 strncpy(cid, c->ani, sizeof(cid) - 1);
2134 ast_callerid_parse(cid, &n, &l);
2136 ast_shrink_phone_number(l);
2137 iax_ie_append_str(&ied, IAX_IE_CALLING_ANI, l);
2140 if (c->language && !ast_strlen_zero(c->language))
2141 iax_ie_append_str(&ied, IAX_IE_LANGUAGE, c->language);
2142 if (c->dnid && !ast_strlen_zero(c->dnid))
2143 iax_ie_append_str(&ied, IAX_IE_DNID, c->dnid);
2145 iax_ie_append_str(&ied, IAX_IE_CALLED_CONTEXT, rcontext);
2147 iax_ie_append_str(&ied, IAX_IE_USERNAME, username);
2148 if (!secret && !ast_strlen_zero(storedsecret))
2149 secret = storedsecret;
2150 ast_mutex_lock(&iaxsl[callno]);
2151 if (!ast_strlen_zero(c->context))
2152 strncpy(iaxs[callno]->context, c->context, sizeof(iaxs[callno]->context));
2154 if (secret[0] == '[') {
2155 /* This is an RSA key, not a normal secret */
2156 strncpy(iaxs[callno]->outkey, secret + 1, sizeof(iaxs[callno]->secret)-1);
2157 if (!ast_strlen_zero(iaxs[callno]->outkey)) {
2158 iaxs[callno]->outkey[strlen(iaxs[callno]->outkey) - 1] = '\0';
2161 strncpy(iaxs[callno]->secret, secret, sizeof(iaxs[callno]->secret)-1);
2163 iax_ie_append_int(&ied, IAX_IE_FORMAT, c->nativeformats);
2164 iax_ie_append_int(&ied, IAX_IE_CAPABILITY, iaxs[callno]->capability);
2165 iax_ie_append_short(&ied, IAX_IE_ADSICPE, c->adsicpe);
2166 iax_ie_append_int(&ied, IAX_IE_DATETIME, iax2_datetime());
2167 /* Transmit the string in a "NEW" request */
2169 /* XXX We have no equivalent XXX */
2170 if (option_verbose > 2)
2171 ast_verbose(VERBOSE_PREFIX_3 "Calling using options '%s'\n", requeststr);
2173 if (iaxs[callno]->maxtime) {
2174 /* Initialize pingtime and auto-congest time */
2175 iaxs[callno]->pingtime = iaxs[callno]->maxtime / 2;
2176 iaxs[callno]->initid = ast_sched_add(sched, iaxs[callno]->maxtime * 2, auto_congest, CALLNO_TO_PTR(callno));
2178 send_command(iaxs[callno], AST_FRAME_IAX,
2179 IAX_COMMAND_NEW, 0, ied.buf, ied.pos, -1);
2180 ast_mutex_unlock(&iaxsl[callno]);
2181 ast_setstate(c, AST_STATE_RINGING);
2185 static int iax2_hangup(struct ast_channel *c)
2187 unsigned short callno = PTR_TO_CALLNO(c->pvt->pvt);
2189 ast_mutex_lock(&iaxsl[callno]);
2190 if (callno && iaxs[callno]) {
2191 ast_log(LOG_DEBUG, "We're hanging up %s now...\n", c->name);
2192 alreadygone = iaxs[callno]->alreadygone;
2193 /* Send the hangup unless we have had a transmission error or are already gone */
2194 if (!iaxs[callno]->error && !alreadygone)
2195 send_command_final(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_HANGUP, 0, NULL, 0, -1);
2196 /* Explicitly predestroy it */
2197 iax2_predestroy_nolock(callno);
2198 /* If we were already gone to begin with, destroy us now */
2200 ast_log(LOG_DEBUG, "Really destroying %s now...\n", c->name);
2201 iax2_destroy_nolock(callno);
2204 ast_mutex_unlock(&iaxsl[callno]);
2205 if (option_verbose > 2)
2206 ast_verbose(VERBOSE_PREFIX_3 "Hungup '%s'\n", c->name);
2210 static int iax2_setoption(struct ast_channel *c, int option, void *data, int datalen)
2212 struct ast_option_header *h;
2214 h = malloc(datalen + sizeof(struct ast_option_header));
2216 h->flag = AST_OPTION_FLAG_REQUEST;
2217 h->option = htons(option);
2218 memcpy(h->data, data, datalen);
2219 res = send_command_locked(PTR_TO_CALLNO(c->pvt->pvt), AST_FRAME_CONTROL,
2220 AST_CONTROL_OPTION, 0, (char *)h, datalen + sizeof(struct ast_option_header), -1);
2224 ast_log(LOG_WARNING, "Out of memory\n");
2228 static struct ast_frame *iax2_read(struct ast_channel *c)
2230 static struct ast_frame f = { AST_FRAME_NULL, };
2231 ast_log(LOG_NOTICE, "I should never be called!\n");
2235 static int iax2_start_transfer(unsigned short callno0, unsigned short callno1)
2238 struct iax_ie_data ied0;
2239 struct iax_ie_data ied1;
2240 unsigned int transferid = rand();
2241 memset(&ied0, 0, sizeof(ied0));
2242 iax_ie_append_addr(&ied0, IAX_IE_APPARENT_ADDR, &iaxs[callno1]->addr);
2243 iax_ie_append_short(&ied0, IAX_IE_CALLNO, iaxs[callno1]->peercallno);
2244 iax_ie_append_int(&ied0, IAX_IE_TRANSFERID, transferid);
2246 memset(&ied1, 0, sizeof(ied1));
2247 iax_ie_append_addr(&ied1, IAX_IE_APPARENT_ADDR, &iaxs[callno0]->addr);
2248 iax_ie_append_short(&ied1, IAX_IE_CALLNO, iaxs[callno0]->peercallno);
2249 iax_ie_append_int(&ied1, IAX_IE_TRANSFERID, transferid);
2251 res = send_command(iaxs[callno0], AST_FRAME_IAX, IAX_COMMAND_TXREQ, 0, ied0.buf, ied0.pos, -1);
2254 res = send_command(iaxs[callno1], AST_FRAME_IAX, IAX_COMMAND_TXREQ, 0, ied1.buf, ied1.pos, -1);
2257 iaxs[callno0]->transferring = TRANSFER_BEGIN;
2258 iaxs[callno1]->transferring = TRANSFER_BEGIN;
2262 static void lock_both(unsigned short callno0, unsigned short callno1)
2264 ast_mutex_lock(&iaxsl[callno0]);
2265 while (ast_mutex_trylock(&iaxsl[callno1])) {
2266 ast_mutex_unlock(&iaxsl[callno0]);
2268 ast_mutex_lock(&iaxsl[callno0]);
2272 static void unlock_both(unsigned short callno0, unsigned short callno1)
2274 ast_mutex_unlock(&iaxsl[callno1]);
2275 ast_mutex_unlock(&iaxsl[callno0]);
2278 static int iax2_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
2280 struct ast_channel *cs[3];
2281 struct ast_channel *who;
2284 int transferstarted=0;
2285 struct ast_frame *f;
2286 unsigned short callno0 = PTR_TO_CALLNO(c0->pvt->pvt);
2287 unsigned short callno1 = PTR_TO_CALLNO(c1->pvt->pvt);
2288 struct timeval waittimer = {0, 0}, tv;
2290 lock_both(callno0, callno1);
2291 /* Put them in native bridge mode */
2292 iaxs[callno0]->bridgecallno = callno1;
2293 iaxs[callno1]->bridgecallno = callno0;
2294 unlock_both(callno0, callno1);
2296 /* If not, try to bridge until we can execute a transfer, if we can */
2299 for (/* ever */;;) {
2300 /* Check in case we got masqueraded into */
2301 if ((c0->type != type) || (c1->type != type)) {
2302 if (option_verbose > 2)
2303 ast_verbose(VERBOSE_PREFIX_3 "Can't masquerade, we're different...\n");
2304 /* Remove from native mode */
2305 if (c0->type == type) {
2306 ast_mutex_lock(&iaxsl[callno0]);
2307 iaxs[callno0]->bridgecallno = 0;
2308 ast_mutex_unlock(&iaxsl[callno0]);
2310 if (c1->type == type) {
2311 ast_mutex_lock(&iaxsl[callno1]);
2312 iaxs[callno1]->bridgecallno = 0;
2313 ast_mutex_unlock(&iaxsl[callno1]);
2317 if (c0->nativeformats != c1->nativeformats) {
2318 ast_verbose(VERBOSE_PREFIX_3 "Operating with different codecs, can't native bridge...\n");
2319 /* Remove from native mode */
2320 lock_both(callno0, callno1);
2321 iaxs[callno0]->bridgecallno = 0;
2322 iaxs[callno1]->bridgecallno = 0;
2323 unlock_both(callno0, callno1);
2326 /* check if transfered and if we really want native bridging */
2327 if (!transferstarted && !iaxs[callno0]->notransfer && !iaxs[callno1]->notransfer) {
2328 /* Try the transfer */
2329 if (iax2_start_transfer(callno0, callno1))
2330 ast_log(LOG_WARNING, "Unable to start the transfer\n");
2331 transferstarted = 1;
2333 if ((iaxs[callno0]->transferring == TRANSFER_RELEASED) && (iaxs[callno1]->transferring == TRANSFER_RELEASED)) {
2334 /* Call has been transferred. We're no longer involved */
2335 gettimeofday(&tv, NULL);
2336 if (!waittimer.tv_sec && !waittimer.tv_usec) {
2337 waittimer.tv_sec = tv.tv_sec;
2338 waittimer.tv_usec = tv.tv_usec;
2339 } else if (tv.tv_sec - waittimer.tv_sec > IAX_LINGER_TIMEOUT) {
2340 c0->_softhangup |= AST_SOFTHANGUP_DEV;
2341 c1->_softhangup |= AST_SOFTHANGUP_DEV;
2349 who = ast_waitfor_n(cs, 2, &to);
2351 if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
2364 if ((f->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
2370 if ((f->frametype == AST_FRAME_VOICE) ||
2371 (f->frametype == AST_FRAME_TEXT) ||
2372 (f->frametype == AST_FRAME_VIDEO) ||
2373 (f->frametype == AST_FRAME_IMAGE) ||
2374 (f->frametype == AST_FRAME_DTMF)) {
2375 if ((f->frametype == AST_FRAME_DTMF) &&
2376 (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))) {
2378 if ((flags & AST_BRIDGE_DTMF_CHANNEL_0)) {
2381 /* Take out of conference mode */
2383 /* Remove from native mode */
2389 if (flags & AST_BRIDGE_DTMF_CHANNEL_1) {
2393 /* Remove from native mode */
2400 ast_log(LOG_DEBUG, "Read from %s\n", who->name);
2402 ast_log(LOG_DEBUG, "Servicing channel %s twice in a row?\n", last->name);
2414 /* Swap who gets priority */
2419 lock_both(callno0, callno1);
2420 iaxs[callno0]->bridgecallno = 0;
2421 iaxs[callno1]->bridgecallno = 0;
2422 unlock_both(callno0, callno1);
2426 static int iax2_answer(struct ast_channel *c)
2428 unsigned short callno = PTR_TO_CALLNO(c->pvt->pvt);
2430 ast_log(LOG_DEBUG, "Answering\n");
2431 return send_command_locked(callno, AST_FRAME_CONTROL, AST_CONTROL_ANSWER, 0, NULL, 0, -1);
2434 static int iax2_indicate(struct ast_channel *c, int condition)
2436 unsigned short callno = PTR_TO_CALLNO(c->pvt->pvt);
2438 ast_log(LOG_DEBUG, "Indicating condition %d\n", condition);
2439 return send_command_locked(callno, AST_FRAME_CONTROL, condition, 0, NULL, 0, -1);
2442 static int iax2_transfer(struct ast_channel *c, char *dest)
2444 unsigned short callno = PTR_TO_CALLNO(c->pvt->pvt);
2445 struct iax_ie_data ied;
2446 char tmp[256] = "", *context;
2447 strncpy(tmp, dest, sizeof(tmp) - 1);
2448 context = strchr(tmp, '@');
2453 memset(&ied, 0, sizeof(ied));
2454 iax_ie_append_str(&ied, IAX_IE_CALLED_NUMBER, tmp);
2456 iax_ie_append_str(&ied, IAX_IE_CALLED_CONTEXT, context);
2458 ast_log(LOG_DEBUG, "Transferring '%s' to '%s'\n", c->name, dest);
2459 return send_command_locked(callno, AST_FRAME_IAX, IAX_COMMAND_TRANSFER, 0, ied.buf, ied.pos, -1);
2463 static int iax2_write(struct ast_channel *c, struct ast_frame *f);
2465 static int iax2_getpeertrunk(struct sockaddr_in sin)
2467 struct iax2_peer *peer;
2469 ast_mutex_lock(&peerl.lock);
2472 if ((peer->addr.sin_addr.s_addr == sin.sin_addr.s_addr) &&
2473 (peer->addr.sin_port == sin.sin_port)) {
2479 ast_mutex_unlock(&peerl.lock);
2483 static struct ast_channel *ast_iax2_new(struct chan_iax2_pvt *i, int state, int capability)
2485 struct ast_channel *tmp;
2486 tmp = ast_channel_alloc(1);
2488 if (!ast_strlen_zero(i->username))
2489 snprintf(tmp->name, sizeof(tmp->name), "IAX2[%s@%s]/%d", i->username, i->host, i->callno);
2491 snprintf(tmp->name, sizeof(tmp->name), "IAX2[%s]/%d", i->host, i->callno);
2493 /* We can support any format by default, until we get restricted */
2494 tmp->nativeformats = capability;
2495 tmp->readformat = 0;
2496 tmp->writeformat = 0;
2497 tmp->pvt->pvt = CALLNO_TO_PTR(i->callno);
2498 tmp->pvt->send_digit = iax2_digit;
2499 tmp->pvt->send_text = iax2_sendtext;
2500 tmp->pvt->send_image = iax2_sendimage;
2501 tmp->pvt->send_html = iax2_sendhtml;
2502 tmp->pvt->call = iax2_call;
2503 tmp->pvt->hangup = iax2_hangup;
2504 tmp->pvt->answer = iax2_answer;
2505 tmp->pvt->read = iax2_read;
2506 tmp->pvt->write = iax2_write;
2507 tmp->pvt->write_video = iax2_write;
2508 tmp->pvt->indicate = iax2_indicate;
2509 tmp->pvt->setoption = iax2_setoption;
2510 tmp->pvt->bridge = iax2_bridge;
2511 tmp->pvt->transfer = iax2_transfer;
2512 if (!ast_strlen_zero(i->callerid))
2513 tmp->callerid = strdup(i->callerid);
2514 if (!ast_strlen_zero(i->ani))
2515 tmp->ani = strdup(i->ani);
2516 if (!ast_strlen_zero(i->language))
2517 strncpy(tmp->language, i->language, sizeof(tmp->language)-1);
2518 if (!ast_strlen_zero(i->dnid))
2519 tmp->dnid = strdup(i->dnid);
2520 if (!ast_strlen_zero(i->accountcode))
2521 strncpy(tmp->accountcode, i->accountcode, sizeof(tmp->accountcode)-1);
2523 tmp->amaflags = i->amaflags;
2524 strncpy(tmp->context, i->context, sizeof(tmp->context)-1);
2525 strncpy(tmp->exten, i->exten, sizeof(tmp->exten)-1);
2526 tmp->adsicpe = i->peeradsicpe;
2527 tmp->pvt->fixup = iax2_fixup;
2529 i->capability = capability;
2530 ast_setstate(tmp, state);
2531 ast_mutex_lock(&usecnt_lock);
2533 ast_mutex_unlock(&usecnt_lock);
2534 ast_update_use_count();
2535 if (state != AST_STATE_DOWN) {
2536 if (ast_pbx_start(tmp)) {
2537 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
2546 static unsigned int calc_txpeerstamp(struct iax2_trunk_peer *tpeer, int sampms, struct timeval *tv)
2548 unsigned long int mssincetx; /* unsigned to handle overflows */
2551 tpeer->trunkact = *tv;
2552 mssincetx = (tv->tv_sec - tpeer->lasttxtime.tv_sec) * 1000 + (tv->tv_usec - tpeer->lasttxtime.tv_usec) / 1000;
2553 if (mssincetx > 5000 || (!tpeer->txtrunktime.tv_sec && !tpeer->txtrunktime.tv_usec)) {
2554 /* If it's been at least 5 seconds since the last time we transmitted on this trunk, reset our timers */
2555 tpeer->txtrunktime.tv_sec = tv->tv_sec;
2556 tpeer->txtrunktime.tv_usec = tv->tv_usec;
2557 tpeer->lastsent = 999999;
2559 /* Update last transmit time now */
2560 tpeer->lasttxtime.tv_sec = tv->tv_sec;
2561 tpeer->lasttxtime.tv_usec = tv->tv_usec;
2563 /* Calculate ms offset */
2564 ms = (tv->tv_sec - tpeer->txtrunktime.tv_sec) * 1000 + (tv->tv_usec - tpeer->txtrunktime.tv_usec) / 1000;
2565 /* Predict from last value */
2566 pred = tpeer->lastsent + sampms;
2567 if (abs(ms - pred) < 640)
2570 /* We never send the same timestamp twice, so fudge a little if we must */
2571 if (ms == tpeer->lastsent)
2572 ms = tpeer->lastsent + 1;
2573 tpeer->lastsent = ms;
2577 static unsigned int fix_peerts(struct timeval *tv, int callno, unsigned int ts)
2579 long ms; /* NOT unsigned */
2580 if (!iaxs[callno]->rxcore.tv_sec && !iaxs[callno]->rxcore.tv_usec) {
2581 /* Initialize rxcore time if appropriate */
2582 gettimeofday(&iaxs[callno]->rxcore, NULL);
2583 /* Round to nearest 20ms */
2584 iaxs[callno]->rxcore.tv_usec -= iaxs[callno]->rxcore.tv_usec % 20000;
2586 /* Calculate difference between trunk and channel */
2587 ms = (tv->tv_sec - iaxs[callno]->rxcore.tv_sec) * 1000 +
2588 (tv->tv_usec - iaxs[callno]->rxcore.tv_usec) / 1000;
2589 /* Return as the sum of trunk time and the difference between trunk and real time */
2593 static unsigned int calc_timestamp(struct chan_iax2_pvt *p, unsigned int ts, struct ast_frame *f)
2599 struct timeval *delivery = NULL;
2601 if (f->frametype == AST_FRAME_VOICE) {
2603 delivery = &f->delivery;
2604 } else if (f->frametype == AST_FRAME_IAX) {
2608 if (!p->offset.tv_sec && !p->offset.tv_usec) {
2609 gettimeofday(&p->offset, NULL);
2610 /* Round to nearest 20ms */
2611 p->offset.tv_usec -= p->offset.tv_usec % 20000;
2613 /* If the timestamp is specified, just send it as is */
2616 if (delivery && (delivery->tv_sec || delivery->tv_usec)) {
2617 ms = (delivery->tv_sec - p->offset.tv_sec) * 1000 + (delivery->tv_usec - p->offset.tv_usec) / 1000;
2619 gettimeofday(&tv, NULL);
2620 ms = (tv.tv_sec - p->offset.tv_sec) * 1000 + (tv.tv_usec - p->offset.tv_usec) / 1000;
2624 /* On a voice frame, use predicted values if appropriate */
2625 if (abs(ms - p->nextpred) <= 640) {
2627 p->nextpred = f->samples / 8;
2632 /* On a dataframe, use last value + 3 (to accomodate jitter buffer shrinkign) if appropriate unless
2633 it's a genuine frame */
2635 if (ms <= p->lastsent)
2636 ms = p->lastsent + 3;
2637 } else if (abs(ms - p->lastsent) <= 640) {
2638 ms = p->lastsent + 3;
2644 p->nextpred = p->nextpred + f->samples / 8;
2646 printf("TS: %s - %dms\n", voice ? "Audio" : "Control", ms);
2651 #ifdef BRIDGE_OPTIMIZATION
2652 static unsigned int calc_fakestamp(struct chan_iax2_pvt *p1, struct chan_iax2_pvt *p2, unsigned int fakets)
2655 /* Receive from p1, send to p2 */
2657 /* Setup rxcore if necessary on outgoing channel */
2658 if (!p1->rxcore.tv_sec && !p1->rxcore.tv_usec)
2659 gettimeofday(&p1->rxcore, NULL);
2661 /* Setup txcore if necessary on outgoing channel */
2662 if (!p2->offset.tv_sec && !p2->offset.tv_usec)
2663 gettimeofday(&p2->offset, NULL);
2665 /* Now, ts is the timestamp of the original packet in the orignal context.
2666 Adding rxcore to it gives us when we would want the packet to be delivered normally.
2667 Subtracting txcore of the outgoing channel gives us what we'd expect */
2669 ms = (p1->rxcore.tv_sec - p2->offset.tv_sec) * 1000 + (p1->rxcore.tv_usec - p2->offset.tv_usec) / 1000;
2671 if (fakets <= p2->lastsent)
2672 fakets = p2->lastsent + 1;
2673 p2->lastsent = fakets;
2678 static unsigned int calc_rxstamp(struct chan_iax2_pvt *p)
2680 /* Returns where in "receive time" we are */
2683 /* Setup rxcore if necessary */
2684 if (!p->rxcore.tv_sec && !p->rxcore.tv_usec)
2685 gettimeofday(&p->rxcore, NULL);
2687 gettimeofday(&tv, NULL);
2688 ms = (tv.tv_sec - p->rxcore.tv_sec) * 1000 + (tv.tv_usec - p->rxcore.tv_usec) / 1000;
2692 struct iax2_trunk_peer *find_tpeer(struct sockaddr_in *sin)
2694 struct iax2_trunk_peer *tpeer;
2695 /* Finds and locks trunk peer */
2696 ast_mutex_lock(&tpeerlock);
2699 /* We don't lock here because tpeer->addr *never* changes */
2700 if (!inaddrcmp(&tpeer->addr, sin)) {
2701 ast_mutex_lock(&tpeer->lock);
2704 tpeer = tpeer->next;
2707 tpeer = malloc(sizeof(struct iax2_trunk_peer));
2709 memset(tpeer, 0, sizeof(struct iax2_trunk_peer));
2710 ast_mutex_init(&tpeer->lock);
2711 tpeer->lastsent = 9999;
2712 memcpy(&tpeer->addr, sin, sizeof(tpeer->addr));
2713 gettimeofday(&tpeer->trunkact, NULL);
2714 ast_mutex_lock(&tpeer->lock);
2715 tpeer->next = tpeers;
2717 ast_log(LOG_DEBUG, "Created trunk peer for '%s:%d'\n", inet_ntoa(tpeer->addr.sin_addr), ntohs(tpeer->addr.sin_port));
2720 ast_mutex_unlock(&tpeerlock);
2724 static int iax2_trunk_queue(struct chan_iax2_pvt *pvt, struct ast_frame *f)
2726 struct iax2_trunk_peer *tpeer;
2728 struct ast_iax2_meta_trunk_entry *met;
2729 tpeer = find_tpeer(&pvt->addr);
2731 if (tpeer->trunkdatalen + f->datalen + 4 >= tpeer->trunkdataalloc) {
2732 /* Need to reallocate space */
2733 if (tpeer->trunkdataalloc < MAX_TRUNKDATA) {
2734 tmp = realloc(tpeer->trunkdata, tpeer->trunkdataalloc + DEFAULT_TRUNKDATA + IAX2_TRUNK_PREFACE);
2736 tpeer->trunkdataalloc += DEFAULT_TRUNKDATA;
2737 tpeer->trunkdata = tmp;
2738 ast_log(LOG_DEBUG, "Expanded trunk '%s:%d' to %d bytes\n", inet_ntoa(tpeer->addr.sin_addr), ntohs(tpeer->addr.sin_port), tpeer->trunkdataalloc);
2740 ast_log(LOG_WARNING, "Insufficient memory to expand trunk data to %s:%d\n", inet_ntoa(tpeer->addr.sin_addr), ntohs(tpeer->addr.sin_port));
2741 ast_mutex_unlock(&tpeer->lock);
2745 ast_log(LOG_WARNING, "Maximum trunk data space exceeded to %s:%d\n", inet_ntoa(tpeer->addr.sin_addr), ntohs(tpeer->addr.sin_port));
2746 ast_mutex_unlock(&tpeer->lock);
2751 /* Append to meta frame */
2752 ptr = tpeer->trunkdata + IAX2_TRUNK_PREFACE + tpeer->trunkdatalen;
2753 met = (struct ast_iax2_meta_trunk_entry *)ptr;
2754 /* Store call number and length in meta header */
2755 met->callno = htons(pvt->callno);
2756 met->len = htons(f->datalen);
2757 /* Advance pointers/decrease length past trunk entry header */
2758 ptr += sizeof(struct ast_iax2_meta_trunk_entry);
2759 tpeer->trunkdatalen += sizeof(struct ast_iax2_meta_trunk_entry);
2760 /* Copy actual trunk data */
2761 memcpy(ptr, f->data, f->datalen);
2762 tpeer->trunkdatalen += f->datalen;
2763 if (!tpeer->firstcallno)
2764 tpeer->firstcallno = pvt->callno;
2766 ast_mutex_unlock(&tpeer->lock);
2771 static int iax2_send(struct chan_iax2_pvt *pvt, struct ast_frame *f, unsigned int ts, int seqno, int now, int transfer, int final)
2773 /* Queue a packet for delivery on a given private structure. Use "ts" for
2774 timestamp, or calculate if ts is 0. Send immediately without retransmission
2775 or delayed, with retransmission */
2776 struct ast_iax2_full_hdr *fh;
2777 struct ast_iax2_mini_hdr *mh;
2778 struct ast_iax2_video_hdr *vh;
2780 struct iax_frame fr2;
2781 unsigned char buffer[4096];
2783 struct iax_frame *fr;
2786 unsigned int lastsent;
2790 ast_log(LOG_WARNING, "No private structure for packet?\n");
2794 lastsent = pvt->lastsent;
2796 /* Calculate actual timestamp */
2797 fts = calc_timestamp(pvt, ts, f);
2799 if ((pvt->trunk || ((fts & 0xFFFF0000L) == (lastsent & 0xFFFF0000L)))
2800 /* High two bits are the same on timestamp, or sending on a trunk */ &&
2801 (f->frametype == AST_FRAME_VOICE)
2802 /* is a voice frame */ &&
2803 (f->subclass == pvt->svoiceformat)
2804 /* is the same type */ ) {
2805 /* Force immediate rather than delayed transmission */
2807 /* Mark that mini-style frame is appropriate */
2810 if (((fts & 0xFFFF8000L) == (lastsent & 0xFFFF8000L)) &&
2811 (f->frametype == AST_FRAME_VIDEO) &&
2812 ((f->subclass & ~0x1) == pvt->svideoformat)) {
2816 /* Allocate an iax_frame */
2820 fr = iax_frame_new(DIRECTION_OUTGRESS, f->datalen);
2822 ast_log(LOG_WARNING, "Out of memory\n");
2825 /* Copy our prospective frame into our immediate or retransmitted wrapper */
2826 iax_frame_wrap(fr, f);
2830 ast_log(LOG_WARNING, "timestamp is 0?\n");
2832 iax2_frame_free(fr);
2835 fr->callno = pvt->callno;
2836 fr->transfer = transfer;
2839 /* We need a full frame */
2843 fr->oseqno = pvt->oseqno++;
2844 fr->iseqno = pvt->iseqno;
2845 fh = (struct ast_iax2_full_hdr *)(fr->af.data - sizeof(struct ast_iax2_full_hdr));
2846 fh->scallno = htons(fr->callno | IAX_FLAG_FULL);
2847 fh->ts = htonl(fr->ts);
2848 fh->oseqno = fr->oseqno;
2852 fh->iseqno = fr->iseqno;
2853 /* Keep track of the last thing we've acknowledged */
2855 pvt->aseqno = fr->iseqno;
2856 fh->type = fr->af.frametype & 0xFF;
2857 if (fr->af.frametype == AST_FRAME_VIDEO)
2858 fh->csub = compress_subclass(fr->af.subclass & ~0x1) | ((fr->af.subclass & 0x1) << 6);
2860 fh->csub = compress_subclass(fr->af.subclass);
2862 fr->dcallno = pvt->transfercallno;
2864 fr->dcallno = pvt->peercallno;
2865 fh->dcallno = htons(fr->dcallno);
2866 fr->datalen = fr->af.datalen + sizeof(struct ast_iax2_full_hdr);
2869 /* Retry after 2x the ping time has passed */
2870 fr->retrytime = pvt->pingtime * 2;
2871 if (fr->retrytime < MIN_RETRY_TIME)
2872 fr->retrytime = MIN_RETRY_TIME;
2873 if (fr->retrytime > MAX_RETRY_TIME)
2874 fr->retrytime = MAX_RETRY_TIME;
2875 /* Acks' don't get retried */
2876 if ((f->frametype == AST_FRAME_IAX) && (f->subclass == IAX_COMMAND_ACK))
2878 else if (f->frametype == AST_FRAME_VOICE)
2879 pvt->svoiceformat = f->subclass;
2880 else if (f->frametype == AST_FRAME_VIDEO)
2881 pvt->svideoformat = f->subclass & ~0x1;
2883 res = send_packet(fr);
2885 res = iax2_transmit(fr);
2888 iax2_trunk_queue(pvt, &fr->af);
2890 } else if (fr->af.frametype == AST_FRAME_VIDEO) {
2891 /* Video frame have no sequence number */
2894 vh = (struct ast_iax2_video_hdr *)(fr->af.data - sizeof(struct ast_iax2_video_hdr));
2896 vh->callno = htons(0x8000 | fr->callno);
2897 vh->ts = htons((fr->ts & 0x7FFF) | (fr->af.subclass & 0x1 ? 0x8000 : 0));
2898 fr->datalen = fr->af.datalen + sizeof(struct ast_iax2_video_hdr);
2901 res = send_packet(fr);
2903 /* Mini-frames have no sequence number */
2906 /* Mini frame will do */
2907 mh = (struct ast_iax2_mini_hdr *)(fr->af.data - sizeof(struct ast_iax2_mini_hdr));
2908 mh->callno = htons(fr->callno);
2909 mh->ts = htons(fr->ts & 0xFFFF);
2910 fr->datalen = fr->af.datalen + sizeof(struct ast_iax2_mini_hdr);
2913 res = send_packet(fr);
2921 static int iax2_show_users(int fd, int argc, char *argv[])
2923 #define FORMAT "%-15.15s %-20.20s %-15.15s %-15.15s %-5.5s\n"
2924 #define FORMAT2 "%-15.15s %-20.20s %-15.15d %-15.15s %-5.5s\n"
2925 struct iax2_user *user;
2928 return RESULT_SHOWUSAGE;
2929 ast_mutex_lock(&userl.lock);
2930 ast_cli(fd, FORMAT, "Username", "Secret", "Authen", "Def.Context", "A/C");
2931 for(user=userl.users;user;user=user->next) {
2932 if (!ast_strlen_zero(user->secret)) {
2933 strncpy(auth,user->secret,sizeof(auth)-1);
2934 } else if (!ast_strlen_zero(user->inkeys)) {
2935 sprintf(auth,"Key: %-15.15s ",user->inkeys);
2937 strcpy(auth,"-no secret-");
2938 ast_cli(fd, FORMAT2, user->name, auth, user->authmethods,
2939 user->contexts ? user->contexts->context : context,
2940 user->ha ? "Yes" : "No");
2942 ast_mutex_unlock(&userl.lock);
2943 return RESULT_SUCCESS;
2948 static int iax2_show_peers(int fd, int argc, char *argv[])
2950 #define FORMAT2 "%-15.15s %-15.15s %s %-15.15s %-8s %-10s\n"
2951 #define FORMAT "%-15.15s %-15.15s %s %-15.15s %-5d%s %-10s\n"
2952 struct iax2_peer *peer;
2953 char name[256] = "";
2954 int registeredonly=0;
2955 if ((argc != 3) && (argc != 4) && (argc != 5))
2956 return RESULT_SHOWUSAGE;
2958 if (!strcasecmp(argv[3], "registered")) {
2961 return RESULT_SHOWUSAGE;
2963 ast_mutex_lock(&peerl.lock);
2964 ast_cli(fd, FORMAT2, "Name/Username", "Host", " ", "Mask", "Port", "Status");
2965 for (peer = peerl.peers;peer;peer = peer->next) {
2968 int print_line = -1;
2970 if (registeredonly && !peer->addr.sin_addr.s_addr)
2972 if (!ast_strlen_zero(peer->username))
2973 snprintf(name, sizeof(name), "%s/%s", peer->name, peer->username);
2975 strncpy(name, peer->name, sizeof(name) - 1);
2977 if (peer->lastms < 0)
2978 strcpy(status, "UNREACHABLE");
2979 else if (peer->lastms > peer->maxms)
2980 snprintf(status, sizeof(status), "LAGGED (%d ms)", peer->lastms);
2981 else if (peer->lastms)
2982 snprintf(status, sizeof(status), "OK (%d ms)", peer->lastms);
2984 strcpy(status, "UNKNOWN");
2986 strcpy(status, "Unmonitored");
2987 strncpy(nm, inet_ntoa(peer->mask), sizeof(nm)-1);
2989 sprintf(srch, FORMAT, name,
2990 peer->addr.sin_addr.s_addr ? inet_ntoa(peer->addr.sin_addr) : "(Unspecified)",
2991 peer->dynamic ? "(D)" : "(S)",
2993 ntohs(peer->addr.sin_port), peer->trunk ? "(T)" : " ", status);
2996 if (!strcasecmp(argv[3],"include") && strstr(srch,argv[4])) {
2998 } else if (!strcasecmp(argv[3],"exclude") && !strstr(srch,argv[4])) {
3000 } else if (!strcasecmp(argv[3],"begin") && !strncasecmp(srch,argv[4],strlen(argv[4]))) {
3008 ast_cli(fd, FORMAT, name,
3009 peer->addr.sin_addr.s_addr ? inet_ntoa(peer->addr.sin_addr) : "(Unspecified)",
3010 peer->dynamic ? "(D)" : "(S)",
3012 ntohs(peer->addr.sin_port), peer->trunk ? "(T)" : " ", status);
3015 ast_mutex_unlock(&peerl.lock);
3016 return RESULT_SUCCESS;
3021 static int iax2_show_firmware(int fd, int argc, char *argv[])
3023 #define FORMAT2 "%-15.15s %-15.15s %-15.15s\n"
3024 #define FORMAT "%-15.15s %-15d %-15d\n"
3025 struct iax_firmware *cur;
3026 if ((argc != 3) && (argc != 4))
3027 return RESULT_SHOWUSAGE;
3028 ast_mutex_lock(&waresl.lock);
3030 ast_cli(fd, FORMAT2, "Device", "Version", "Size");