Version 0.1.6 from FTP
[asterisk/asterisk.git] / channels / chan_iax.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Implementation of Inter-Asterisk eXchange
5  * 
6  * Copyright (C) 1999, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <asterisk/frame.h> 
15 #include <asterisk/channel.h>
16 #include <asterisk/channel_pvt.h>
17 #include <asterisk/logger.h>
18 #include <asterisk/module.h>
19 #include <asterisk/pbx.h>
20 #include <asterisk/sched.h>
21 #include <asterisk/io.h>
22 #include <asterisk/config.h>
23 #include <asterisk/options.h>
24 #include <asterisk/cli.h>
25 #include <asterisk/translate.h>
26 #include <asterisk/md5.h>
27 #include <arpa/inet.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <sys/time.h>
31 #include <sys/signal.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <unistd.h>
37 #include <netdb.h>
38
39 #include "iax.h"
40
41 #define DEFAULT_RETRY_TIME 1000
42 #define MEMORY_SIZE 100
43 #define DEFAULT_DROP 3
44
45 /* If you want to use the simulator, then define IAX_SIMULATOR.  */
46
47 /*
48 #define IAX_SIMULATOR
49 */
50 static char *desc = "Inter Asterisk eXchange";
51 static char *tdesc = "Inter Asterisk eXchange Drver";
52 static char *type = "IAX";
53 static char *config = "iax.conf";
54
55 static char context[80] = "default";
56
57 static int max_retries = 4;
58 static int ping_time = 20;
59 static int lagrq_time = 10;
60 static int nextcallno = 0;
61 static int maxjitterbuffer=4000;
62
63 static int netsocket = -1;
64
65 static int usecnt;
66 static pthread_mutex_t usecnt_lock = PTHREAD_MUTEX_INITIALIZER;
67 static pthread_mutex_t iaxs_lock = PTHREAD_MUTEX_INITIALIZER;
68
69 /* Ethernet, etc */
70 #define IAX_CAPABILITY_FULLBANDWIDTH    0x7FFFFFFF
71 /* T1, maybe ISDN */
72 #define IAX_CAPABILITY_MEDBANDWIDTH     (IAX_CAPABILITY_FULLBANDWIDTH & \
73                                                                         ~AST_FORMAT_SLINEAR & \
74                                                                         ~AST_FORMAT_ULAW & \
75                                                                         ~AST_FORMAT_ALAW) 
76 /* A modem */
77 #define IAX_CAPABILITY_LOWBANDWIDTH             (IAX_CAPABILITY_MEDBANDWIDTH & \
78                                                                         ~AST_FORMAT_MP3 & \
79                                                                         ~AST_FORMAT_ADPCM)
80
81 #define IAX_CAPABILITY_LOWFREE          (IAX_CAPABILITY_LOWBANDWIDTH & \
82                                                                          ~AST_FORMAT_G723_1)
83
84 static  struct io_context *io;
85 static  struct sched_context *sched;
86
87 static int iax_capability = IAX_CAPABILITY_FULLBANDWIDTH;
88
89 static int iax_dropcount = DEFAULT_DROP;
90
91 static int use_jitterbuffer = 1;
92
93 static pthread_t netthreadid;
94
95 #define IAX_STATE_STARTED               (1 << 0)
96 #define IAX_STATE_AUTHENTICATED (1 << 1)
97
98 #define IAX_SENSE_DENY                  0
99 #define IAX_SENSE_ALLOW                 1
100
101 struct iax_ha {
102         /* Host access rule */
103         struct in_addr netaddr;
104         struct in_addr netmask;
105         int sense;
106         struct iax_ha *next;
107 };
108
109 struct iax_context {
110         char context[AST_MAX_EXTENSION];
111         struct iax_context *next;
112 };
113
114 struct iax_user {
115         char name[80];
116         char secret[80];
117         char methods[80];
118         struct iax_ha *ha;
119         struct iax_context *contexts;
120         struct iax_user *next;
121 };
122
123 struct iax_peer {
124         char name[80];
125         char username[80];
126         char secret[80];
127         struct sockaddr_in addr;
128         int formats;
129         struct in_addr mask;
130
131         /* Dynamic Registration fields */
132         int dynamic;
133         struct sockaddr_in defaddr;
134         char regsecret[80];
135         char methods[80];
136         struct timeval nexpire;
137         int expire;
138         struct iax_ha *ha;
139         struct iax_peer *next;
140 };
141         
142 /* Don't retry more frequently than every 10 ms, or less frequently than every 5 seconds */
143 #define MIN_RETRY_TIME  10
144 #define MAX_RETRY_TIME  10000
145 #define MAX_JITTER_BUFFER 50
146
147 /* If we have more than this much excess real jitter buffer, srhink it. */
148 static int max_jitter_buffer = MAX_JITTER_BUFFER;
149
150 struct chan_iax_pvt {
151         /* Pipes for communication.  pipe[1] belongs to the
152            network thread (write), and pipe[0] belongs to the individual 
153            channel (read) */
154         int pipe[2];
155         /* Last received voice format */
156         int voiceformat;
157         /* Last sent voice format */
158         int svoiceformat;
159         /* Last received timestamp */
160         unsigned int last;
161         /* Last sent timestamp - never send the same timestamp twice in a single call */
162         unsigned int lastsent;
163         /* Ping time */
164         unsigned int pingtime;
165         /* Peer Address */
166         struct sockaddr_in addr;
167         /* Our call number */
168         int callno;
169         /* Peer callno */
170         int peercallno;
171         /* Peer supported formats */
172         int peerformats;
173         /* timeval that we base our transmission on */
174         struct timeval offset;
175         /* timeval that we base our delivery on */
176         struct timeval rxcore;
177         /* Historical delivery time */
178         int history[MEMORY_SIZE];
179         /* Current base jitterbuffer */
180         int jitterbuffer;
181         /* Current jitter measure */
182         int jitter;
183         /* LAG */
184         int lag;
185         /* Error, as discovered by the manager */
186         int error;
187         /* Onwer if we have one */
188         struct ast_channel *owner;
189         /* What's our state? */
190         int state;
191         /* Next outgoing sequence number */
192         unsigned short oseqno;
193         /* Next incoming sequence number */
194         unsigned short iseqno;
195         /* Peer name */
196         char peer[80];
197         /* Default Context */
198         char context[80];
199         /* Caller ID if available */
200         char callerid[80];
201         /* DNID */
202         char dnid[80];
203         /* Requested Extension */
204         char exten[AST_MAX_EXTENSION];
205         /* Expected Username */
206         char username[80];
207         /* Expected Secret */
208         char secret[80];
209         /* permitted authentication methods */
210         char methods[80];
211         /* MD5 challenge */
212         char challenge[10];
213 };
214
215 struct ast_iax_frame {
216         /* Actual, isolated frame */
217         struct ast_frame *f;
218         /* /Our/ call number */
219         short callno;
220         /* Start of raw frame (outgoing only) */
221         void *data;
222         /* Length of frame (outgoing only) */
223         int datalen;
224         /* How many retries so far? */
225         int retries;
226         /* Outgoing relative timestamp (ms) */
227         unsigned int ts;
228         /* How long to wait before retrying */
229         int retrytime;
230         /* Are we received out of order?  */
231         int outoforder;
232         /* Have we been sent at all yet? */
233         int sentyet;
234         /* Packet sequence number */
235         int seqno;
236         /* Easy linking */
237         struct ast_iax_frame *next;
238         struct ast_iax_frame *prev;
239 };
240
241 static struct ast_iax_queue {
242         struct ast_iax_frame *head;
243         struct ast_iax_frame *tail;
244         int count;
245         pthread_mutex_t lock;
246 } iaxq;
247
248 static struct ast_user_list {
249         struct iax_user *users;
250         pthread_mutex_t lock;
251 } userl;
252
253 static struct ast_peer_list {
254         struct iax_peer *peers;
255         pthread_mutex_t lock;
256 } peerl;
257
258 /* XXX We probably should use a mutex when working with this XXX */
259 static struct chan_iax_pvt *iaxs[AST_IAX_MAX_CALLS];
260
261 static int send_command(struct chan_iax_pvt *, char, int, unsigned int, char *, int, int);
262
263 static unsigned int calc_timestamp(struct chan_iax_pvt *p, unsigned int ts);
264
265 static int send_ping(void *data)
266 {
267         int callno = (long)data;
268         if (iaxs[callno]) {
269                 send_command(iaxs[callno], AST_FRAME_IAX, AST_IAX_COMMAND_PING, 0, NULL, 0, -1);
270                 return 1;
271         } else
272                 return 0;
273 }
274
275 static int send_lagrq(void *data)
276 {
277         int callno = (long)data;
278         if (iaxs[callno]) {
279                 send_command(iaxs[callno], AST_FRAME_IAX, AST_IAX_COMMAND_LAGRQ, 0, NULL, 0, -1);
280                 return 1;
281         } else
282                 return 0;
283 }
284
285 static unsigned char compress_subclass(int subclass)
286 {
287         int x;
288         int power=-1;
289         /* If it's 128 or smaller, just return it */
290         if (subclass < AST_FLAG_SC_LOG)
291                 return subclass;
292         /* Otherwise find its power */
293         for (x = 0; x < AST_MAX_SHIFT; x++) {
294                 if (subclass & (1 << x)) {
295                         if (power > -1) {
296                                 ast_log(LOG_WARNING, "Can't compress subclass %d\n", subclass);
297                                 return 0;
298                         } else
299                                 power = x;
300                 }
301         }
302         return power | AST_FLAG_SC_LOG;
303 }
304
305 static int uncompress_subclass(unsigned char csub)
306 {
307         /* If the SC_LOG flag is set, return 2^csub otherwise csub */
308         if (csub & AST_FLAG_SC_LOG)
309                 return 1 << (csub & ~AST_FLAG_SC_LOG & AST_MAX_SHIFT);
310         else
311                 return csub;
312 }
313
314 static struct chan_iax_pvt *new_iax(void)
315 {
316         struct chan_iax_pvt *tmp;
317         tmp = malloc(sizeof(struct chan_iax_pvt));
318         if (tmp) {
319                 memset(tmp, 0, sizeof(struct chan_iax_pvt));
320                 /* On my linux system, pipe's are more than 2x as fast as socketpairs */
321                 if (pipe(tmp->pipe)) {
322                         ast_log(LOG_WARNING, "Unable to create pipe: %s\n", strerror(errno));
323                         free(tmp);
324                         return NULL;
325                 }
326                 tmp->callno = -1;
327                 tmp->peercallno = -1;
328                 /* strncpy(tmp->context, context, sizeof(tmp->context)); */
329                 strncpy(tmp->exten, "s", sizeof(tmp->exten));
330         }
331         return tmp;
332 }
333
334 static int get_timelen(struct ast_frame *f)
335 {
336         int timelen=0;
337         switch(f->subclass) {
338         case AST_FORMAT_G723_1:
339                 timelen = 30;
340                 break;
341         case AST_FORMAT_GSM:
342                 timelen = 20;
343                 break;
344         case AST_FORMAT_SLINEAR:
345                 timelen = f->datalen / 8;
346                 break;
347         case AST_FORMAT_LPC10:
348                 timelen = 22;
349                 timelen += ((char *)(f->data))[7] & 0x1;
350                 break;
351         default:
352                 ast_log(LOG_WARNING, "Don't know how to calculate timelen on %d packets\n", f->subclass);
353         }
354         return timelen;
355 }
356
357 #if 0
358 static struct ast_iax_frame *iaxfrdup(struct ast_iax_frame *fr)
359 {
360         /* Malloc() a copy of a frame */
361         struct ast_iax_frame *new = malloc(sizeof(struct ast_iax_frame));
362         if (new) 
363                 memcpy(new, fr, sizeof(struct ast_iax_frame));  
364         return new;
365 }
366 #endif
367
368 static struct ast_iax_frame *iaxfrdup2(struct ast_iax_frame *fr, int ch)
369 {
370         /* Malloc() a copy of a frame */
371         struct ast_iax_frame *new = malloc(sizeof(struct ast_iax_frame));
372         if (new) {
373                 memcpy(new, fr, sizeof(struct ast_iax_frame));  
374                 new->f = ast_frdup(fr->f);
375                 /* Copy full header */
376                 if (ch) {
377                         memcpy(new->f->data - sizeof(struct ast_iax_full_hdr),
378                                         fr->f->data - sizeof(struct ast_iax_full_hdr), 
379                                                 sizeof(struct ast_iax_full_hdr));
380                         /* Grab new data pointer */
381                         new->data = new->f->data - (fr->f->data - fr->data);
382                 } else {
383                         new->data = NULL;
384                         new->datalen = 0;
385                 }
386         }
387         return new;
388 }
389
390 #define NEW_PREVENT 0
391 #define NEW_ALLOW       1
392 #define NEW_FORCE       2
393
394 static int find_callno(short callno, short dcallno ,struct sockaddr_in *sin, int new)
395 {
396         int res = -1;
397         int x;
398         int start;
399         if (new <= NEW_ALLOW) {
400                 /* Look for an existing connection first */
401                 for (x=0;x<AST_IAX_MAX_CALLS;x++) {
402                         if (iaxs[x]) {
403                                 /* Look for an exact match */
404                                 if ((sin->sin_port == iaxs[x]->addr.sin_port) &&
405                                     (sin->sin_addr.s_addr == iaxs[x]->addr.sin_addr.s_addr) &&
406                                         ((callno == iaxs[x]->peercallno) || /* Our expected source call number is the same */
407                                          ((dcallno == x) && (iaxs[x]->peercallno = -1)) 
408                                          /* We have no expected source number, and the destination is right */
409                                          )) {
410                                         res = x;
411                                         break;
412                                 }
413                         }
414                 }
415         }
416         if ((res < 0) && (new >= NEW_ALLOW)) {
417                 /* Create a new one */
418                 start = nextcallno;
419                 for (x = nextcallno + 1; iaxs[x] && (x != start); x = (x + 1) % AST_IAX_MAX_CALLS) 
420                 if (x == start) {
421                         ast_log(LOG_WARNING, "Unable to accept more calls\n");
422                         return -1;
423                 }
424                 iaxs[x] = new_iax();
425                 if (iaxs[x]) {
426                         if (option_debug)
427                                 ast_log(LOG_DEBUG, "Creating new call structure %d\n", x);
428                         iaxs[x]->addr.sin_port = sin->sin_port;
429                         iaxs[x]->addr.sin_family = sin->sin_family;
430                         iaxs[x]->addr.sin_addr.s_addr = sin->sin_addr.s_addr;
431                         iaxs[x]->peercallno = callno;
432                         iaxs[x]->callno = x;
433                         iaxs[x]->pingtime = DEFAULT_RETRY_TIME;
434                         ast_sched_add(sched, ping_time * 1000, send_ping, (void *)x);
435                         ast_sched_add(sched, lagrq_time * 1000, send_lagrq, (void *)x);
436                 } else
437                         ast_log(LOG_DEBUG, "Out of memory\n");
438                 res = x;
439                 nextcallno = x;
440         }
441         return res;
442 }
443
444 static int iax_send(struct chan_iax_pvt *pvt, struct ast_frame *f, unsigned int ts, int seqno);
445
446 static int do_deliver(void *data)
447 {
448         /* Just deliver the packet by writing it to half of the pipe. */
449         struct ast_iax_frame *fr = data;
450         unsigned int ts;
451         if (iaxs[fr->callno]) {
452                 if (fr->f->frametype == AST_FRAME_IAX) {
453                         /* We have to treat some of these packets specially because
454                            they're LAG measurement packets */
455                         if (fr->f->subclass == AST_IAX_COMMAND_LAGRQ) {
456                                 /* If we got a queued request, build a reply and send it */
457                                 fr->f->subclass = AST_IAX_COMMAND_LAGRP;
458                                 iax_send(iaxs[fr->callno], fr->f, fr->ts, -1);
459                         } else if (fr->f->subclass == AST_IAX_COMMAND_LAGRP) {
460                                 /* This is a reply we've been given, actually measure the difference */
461                                 ts = calc_timestamp(iaxs[fr->callno], 0);
462                                 iaxs[fr->callno]->lag = ts - fr->ts;
463                         }
464                 } else {
465                         ast_fr_fdwrite(iaxs[fr->callno]->pipe[1], fr->f);
466                 }
467         }
468         /* Free the packet */
469         ast_frfree(fr->f);
470         /* And our iax frame */
471         free(fr);
472         /* And don't run again */
473         return 0;
474 }
475
476 static int handle_error()
477 {
478         /* XXX Ideally we should figure out why an error occured and then abort those
479            rather than continuing to try.  Unfortunately, the published interface does
480            not seem to work XXX */
481 #if 0
482         struct sockaddr_in *sin;
483         int res;
484         struct msghdr m;
485         struct sock_extended_err e;
486         m.msg_name = NULL;
487         m.msg_namelen = 0;
488         m.msg_iov = NULL;
489         m.msg_control = &e;
490         m.msg_controllen = sizeof(e);
491         m.msg_flags = 0;
492         res = recvmsg(netsocket, &m, MSG_ERRQUEUE);
493         if (res < 0)
494                 ast_log(LOG_WARNING, "Error detected, but unable to read error: %s\n", strerror(errno));
495         else {
496                 if (m.msg_controllen) {
497                         sin = (struct sockaddr_in *)SO_EE_OFFENDER(&e);
498                         if (sin) 
499                                 ast_log(LOG_WARNING, "Receive error from %s\n", inet_ntoa(sin->sin_addr));
500                         else
501                                 ast_log(LOG_WARNING, "No address detected??\n");
502                 } else {
503                         ast_log(LOG_WARNING, "Local error: %s\n", strerror(e.ee_errno));
504                 }
505         }
506 #endif
507         return 0;
508 }
509
510 #ifdef IAX_SIMULATOR
511 static int __send_packet(struct ast_iax_frame *f)
512 #else
513 static int send_packet(struct ast_iax_frame *f)
514 #endif
515 {
516         int res;
517         if (option_debug)
518                 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));
519         /* Don't send if there was an error, but return error instead */
520         if (f->callno < 0) {
521                 ast_log(LOG_WARNING, "Call number = %d\n", f->callno);
522                 return -1;
523         }
524         if (iaxs[f->callno]->error)
525                 return -1;
526         res = sendto(netsocket, f->data, f->datalen, 0, &iaxs[f->callno]->addr, 
527                                         sizeof(iaxs[f->callno]->addr));
528         if (res < 0) { 
529                 ast_log(LOG_WARNING, "Received error: %s\n", strerror(errno));
530                 handle_error();
531         } else 
532                 res = 0;
533         return res;
534 }
535
536 #ifdef IAX_SIMULATOR
537
538 /* Average amount of delay in the connection */
539 static int average_delay = 0;
540 /* Permitted deviation either side of the average delay */
541 static int delay_deviation = 0;
542 /* Percent chance that a packet arrives O.K. */
543 static int reliability = 100;
544
545 static int iax_sim_calc_delay()
546 {
547         int ms;
548         ms = average_delay - delay_deviation;
549         ms += ((float)(delay_deviation * 2)) * rand() / (RAND_MAX + 1.0);
550         if (ms < 0)
551                 ms = 0;
552         if ((float)rand()/(RAND_MAX + 1.0) < ((float)reliability)/100)
553                 return ms;
554         else
555                 return -1;
556 }
557
558 static int d_send_packet(void *v)
559 {
560         struct ast_iax_frame *f = (struct ast_iax_frame *)v;
561         if (iaxs[f->callno])
562                 __send_packet(f);
563         ast_frfree(f->f);
564         free(f);
565         return 0;
566 }
567
568 static int send_packet(struct ast_iax_frame *f)
569 {
570         struct ast_iax_frame *fn;
571         int ms;
572         ms = iax_sim_calc_delay();
573         if (ms == 0)
574                 return __send_packet(f);
575         else if (ms > 0) {
576                 /* Make a completely independent frame, in case the other
577                    is destroyed -- still doesn't make things like hangups
578                    arrive if the main channel is destroyed, but close enough */
579                 fn = iaxfrdup2(f, 1);
580                 ast_sched_add(sched, ms, d_send_packet, fn);
581         } /* else we drop the packet */
582         return 0;
583 }
584
585 static int iax_sim_set(int fd, int argc, char *argv[])
586 {
587         if (argc != 4)
588                 return RESULT_SHOWUSAGE;
589         if (!strcasecmp(argv[2], "delay")) 
590                 average_delay = atoi(argv[3]);
591         else if (!strcasecmp(argv[2], "deviation"))
592                 delay_deviation = atoi(argv[3]);
593         else if (!strcasecmp(argv[2], "reliability"))
594                 reliability = atoi(argv[3]);
595         else 
596                 return RESULT_SHOWUSAGE;
597         if (reliability > 100)
598                 reliability = 100;
599         if (reliability < 0)
600                 reliability = 0;
601         if (delay_deviation > average_delay)
602                 delay_deviation = average_delay;
603         return RESULT_SUCCESS;
604 }
605
606 static char delay_usage[] = 
607 "Usage: sim set delay <value>\n"
608 "       Configure the IAX network simulator to generate average\n"
609 "       delays equal to the specified value (in milliseconds).\n";
610
611 static char deviation_usage[] = 
612 "Usage: sim set deviation <value>\n"
613 "       Configures the IAX network simulator's deviation value.\n"
614 "       The delays generated by the simulator will always be within\n"
615 "       this value of milliseconds (postive or negative) of the \n"
616 "       average delay.\n";
617
618 static char reliability_usage[] = 
619 "Usage: sim set reliability <value>\n"
620 "       Configure the probability that a packet will be delivered.\n"
621 "       The value specified is a percentage from 0 to 100\n";
622
623 static int iax_sim_show(int fd, int argc, char *argv[])
624 {
625         if (argc != 2)
626                 return RESULT_SHOWUSAGE;
627         ast_cli(fd, "Average Delay:   %d ms\n", average_delay);
628         ast_cli(fd, "Delay Deviation: %d ms\n", delay_deviation);
629         ast_cli(fd, "Reliability:     %d %\n", reliability);
630         return RESULT_SUCCESS;
631 }
632
633 static char sim_show_usage[] = 
634 "Usage: sim show\n"
635 "       Displays average delay, deviation, and reliability\n"
636 "       used by the network simulator.\n";
637
638 static struct ast_cli_entry delay_cli = 
639 { { "sim", "set", "delay", NULL }, iax_sim_set, "Sets simulated average delay", delay_usage };
640 static struct ast_cli_entry deviation_cli = 
641 { { "sim", "set", "deviation", NULL }, iax_sim_set, "Sets simulated delay deviation", deviation_usage };
642 static struct ast_cli_entry reliability_cli = 
643 { { "sim", "set", "reliability", NULL }, iax_sim_set, "Sets simulated reliability", reliability_usage };
644 static struct ast_cli_entry sim_show_cli = 
645 { { "sim", "show", NULL }, iax_sim_show, "Displays simulation parameters", sim_show_usage };
646
647 #endif
648
649 static int attempt_transmit(void *data)
650 {
651         /* Attempt to transmit the frame to the remote peer */
652         char zero = 0;
653         struct ast_iax_frame *f = data;
654         int res = 0;
655         /* Make sure this call is still active */
656         if (iaxs[f->callno]) {
657                 if ((f->retries == -1) /* Already ACK'd */ ||
658                     (f->retries >= max_retries) /* Too many attempts */) {
659                                 /* Record an error if we've transmitted too many times */
660                                 if (f->retries >= max_retries) {
661                                         ast_log(LOG_WARNING, "Max retries exceeded to host %s (type = %d, subclass = %d, ts=%d, seqno=%d)\n", inet_ntoa(iaxs[f->callno]->addr.sin_addr), f->f->frametype, f->f->subclass, f->ts, f->seqno);
662                                         iaxs[f->callno]->error = ETIMEDOUT;
663                                         /* Send a bogus frame to wake up the waiting process */
664                                         write(iaxs[f->callno]->pipe[1], &zero, 1);
665                                 }
666                                 /* Don't attempt delivery, just remove it from the queue */
667                                 pthread_mutex_lock(&iaxq.lock);
668                                 if (f->prev) 
669                                         f->prev->next = f->next;
670                                 else
671                                         iaxq.head = f->next;
672                                 if (f->next)
673                                         f->next->prev = f->prev;
674                                 else
675                                         iaxq.tail = f->prev;
676                                 iaxq.count--;
677                                 pthread_mutex_unlock(&iaxq.lock);
678                 } else {
679                         /* Attempt transmission */
680                         send_packet(f);
681                         f->retries++;
682                         /* Try again later after 10 times as long */
683                         f->retrytime *= 10;
684                         if (f->retrytime > MAX_RETRY_TIME)
685                                 f->retrytime = MAX_RETRY_TIME;
686                         ast_sched_add(sched, f->retrytime, attempt_transmit, f);
687                         res=0;
688                 }
689         }
690         /* Do not try again */
691         return res;
692 }
693
694 static int iax_set_jitter(int fd, int argc, char *argv[])
695 {
696         if ((argc != 4) && (argc != 5))
697                 return RESULT_SHOWUSAGE;
698         if (argc == 4) {
699                 max_jitter_buffer = atoi(argv[3]);
700                 if (max_jitter_buffer < 0)
701                         max_jitter_buffer = 0;
702         } else {
703                 if (argc == 5) {
704                         pthread_mutex_lock(&iaxs_lock);
705                         if ((atoi(argv[3]) >= 0) && (atoi(argv[3]) < AST_IAX_MAX_CALLS)) {
706                                 if (iaxs[atoi(argv[3])]) {
707                                         iaxs[atoi(argv[3])]->jitterbuffer = atoi(argv[4]);
708                                         if (iaxs[atoi(argv[3])]->jitterbuffer < 0)
709                                                 iaxs[atoi(argv[3])]->jitterbuffer = 0;
710                                 } else
711                                         ast_cli(fd, "No such call '%d'\n", atoi(argv[3]));
712                         } else
713                                 ast_cli(fd, "%d is not a valid call number\n", atoi(argv[3]));
714                         pthread_mutex_unlock(&iaxs_lock);
715                 }
716         }
717         return RESULT_SUCCESS;
718 }
719
720 static char jitter_usage[] = 
721 "Usage: iax set jitter [callid] <value>\n"
722 "       If used with a callid, it sets the jitter buffer to the given static\n"
723 "value (until its next calculation).  If used without a callid, the value is used\n"
724 "to establish the maximum excess jitter buffer that is permitted before the jitter\n"
725 "buffer size is reduced.";
726
727 static struct ast_cli_entry cli_set_jitter = 
728 { { "iax", "set", "jitter", NULL }, iax_set_jitter, "Sets IAX jitter buffer", jitter_usage };
729
730 static unsigned int calc_rxstamp(struct chan_iax_pvt *p);
731
732 static int schedule_delivery(struct ast_iax_frame *fr)
733 {
734         /* XXX FIXME: I should delay delivery with a sliding jitter buffer XXX */
735         int ms,x;
736         int drops[MEMORY_SIZE];
737         int min, max=0, maxone=0,y,z, match;
738         /* ms is a measure of the "lateness" of the packet relative to the first
739            packet we received, which always has a lateness of 1.  */
740         ms = calc_rxstamp(iaxs[fr->callno]) - fr->ts;
741         
742         /* Rotate our history queue of "lateness".  Don't worry about those initial
743            zeros because the first entry will always be zero */
744         for (x=0;x<MEMORY_SIZE - 1;x++) 
745                 iaxs[fr->callno]->history[x] = iaxs[fr->callno]->history[x+1];
746         /* Add a history entry for this one */
747         iaxs[fr->callno]->history[x] = ms;
748
749         /* Initialize the minimum to reasonable values.  It's too much
750            work to do the same for the maximum, repeatedly */
751         min=iaxs[fr->callno]->history[0];
752         for (z=0;z < iax_dropcount + 1;z++) {
753                 /* Start very pessimistic ;-) */
754                 max=-999999999;
755                 for (x=0;x<MEMORY_SIZE;x++) {
756                         if (max < iaxs[fr->callno]->history[x]) {
757                                 /* We have a candidate new maximum value.  Make
758                                    sure it's not in our drop list */
759                                 match = 0;
760                                 for (y=0;!match && (y<z);y++)
761                                         match |= (drops[y] == x);
762                                 if (!match) {
763                                         /* It's not in our list, use it as the new maximum */
764                                         max = iaxs[fr->callno]->history[x];
765                                         maxone = x;
766                                 }
767                                 
768                         }
769                         if (!z) {
770                                 /* On our first pass, find the minimum too */
771                                 if (min > iaxs[fr->callno]->history[x])
772                                         min = iaxs[fr->callno]->history[x];
773                         }
774                 }
775 #if 1
776                 drops[z] = maxone;
777 #endif
778         }
779         /* Just for reference, keep the "jitter" value, the difference between the
780            earliest and the latest. */
781         iaxs[fr->callno]->jitter = max - min;   
782         
783         /* If our jitter buffer is too big (by a significant margin), then we slowly
784            shrink it by about 1 ms each time to avoid letting the change be perceived */
785         if (max < iaxs[fr->callno]->jitterbuffer - max_jitter_buffer)
786                 iaxs[fr->callno]->jitterbuffer -= 2;
787
788 #if 1
789         /* Constrain our maximum jitter buffer appropriately */
790         if (max > min + maxjitterbuffer) {
791                 if (option_debug)
792                         ast_log(LOG_DEBUG, "Constraining buffer from %d to %d + %d\n", max, min , maxjitterbuffer);
793                 max = min + maxjitterbuffer;
794         }
795 #endif
796
797         /* If our jitter buffer is smaller than our maximum delay, grow the jitter
798            buffer immediately to accomodate it (and a little more).  */
799         if (max > iaxs[fr->callno]->jitterbuffer)
800                 iaxs[fr->callno]->jitterbuffer = max 
801                         /* + ((float)iaxs[fr->callno]->jitter) * 0.1 */;
802                 
803
804         if (option_debug)
805                 ast_log(LOG_DEBUG, "min = %d, max = %d, jb = %d, lateness = %d\n", min, max, iaxs[fr->callno]->jitterbuffer, ms);
806         
807         /* Subtract the lateness from our jitter buffer to know how long to wait
808            before sending our packet.  */
809         ms = iaxs[fr->callno]->jitterbuffer - ms;
810         
811         if (!use_jitterbuffer)
812                 ms = 0;
813
814         if (ms < 1) {
815                 if (option_debug)
816                         ast_log(LOG_DEBUG, "Calculated ms is %d\n", ms);
817                 /* Don't deliver it more than 4 ms late */
818                 if ((ms > -4) || (fr->f->frametype != AST_FRAME_VOICE)) {
819                         do_deliver(fr);
820                 }
821                 else {
822                         /* Free the packet */
823                         ast_frfree(fr->f);
824                         /* And our iax frame */
825                         free(fr);
826                 }
827         } else {
828                 if (option_debug)
829                         ast_log(LOG_DEBUG, "Scheduling delivery in %d ms\n", ms);
830                 ast_sched_add(sched, ms, do_deliver, fr);
831         }
832         return 0;
833 }
834
835 static int iax_transmit(struct ast_iax_frame *fr)
836 {
837         /* Lock the queue and place this packet at the end */
838         fr->next = NULL;
839         fr->prev = NULL;
840         /* By setting this to 0, the network thread will send it for us, and
841            queue retransmission if necessary */
842         fr->sentyet = 0;
843         pthread_mutex_lock(&iaxq.lock);
844         if (!iaxq.head) {
845                 /* Empty queue */
846                 iaxq.head = fr;
847                 iaxq.tail = fr;
848         } else {
849                 /* Double link */
850                 iaxq.tail->next = fr;
851                 fr->prev = iaxq.tail;
852                 iaxq.tail = fr;
853         }
854         iaxq.count++;
855         pthread_mutex_unlock(&iaxq.lock);
856         /* Wake up the network thread */
857         pthread_kill(netthreadid, SIGURG);
858         return 0;
859 }
860
861
862
863 static int iax_digit(struct ast_channel *c, char digit)
864 {
865         return send_command(c->pvt->pvt, AST_FRAME_DTMF, digit, 0, NULL, 0, -1);
866 }
867
868 static int iax_sendtext(struct ast_channel *c, char *text)
869 {
870         
871         return send_command(c->pvt->pvt, AST_FRAME_TEXT,
872                 0, 0, text, strlen(text) + 1, -1);
873 }
874
875 static int create_addr(struct sockaddr_in *sin, char *peer)
876 {
877         struct hostent *hp;
878         struct iax_peer *p;
879         sin->sin_family = AF_INET;
880         pthread_mutex_lock(&peerl.lock);
881         p = peerl.peers;
882         while(p) {
883                 if (!strcasecmp(p->name, peer)) {
884                         sin->sin_addr = p->addr.sin_addr;
885                         sin->sin_port = p->addr.sin_port;
886                         break;
887                 }
888                 p = p->next;
889         }
890         pthread_mutex_unlock(&peerl.lock);
891         if (!p) {
892                 hp = gethostbyname(peer);
893                 if (hp) {
894                         memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
895                         sin->sin_port = htons(AST_DEFAULT_IAX_PORTNO);
896                         return 0;
897                 } else
898                         return -1;
899         } else
900                 return 0;
901 }
902 static int iax_call(struct ast_channel *c, char *dest, int timeout)
903 {
904         struct sockaddr_in sin;
905         char host[256];
906         char *rdest;
907         char *rcontext;
908         char *username;
909         char *hname;
910         char requeststr[256] = "";
911         char myrdest [5] = "s";
912         char *portno = NULL;
913         if ((c->state != AST_STATE_DOWN) && (c->state != AST_STATE_RESERVED)) {
914                 ast_log(LOG_WARNING, "Line is already in use (%s)?\n", c->name);
915                 return -1;
916         }
917         strncpy(host, dest, sizeof(host));
918         strtok(host, "/");
919         /* If no destination extension specified, use 's' */
920         rdest = strtok(NULL, "/");
921         if (!rdest) 
922                 rdest = myrdest;
923         strtok(rdest, "@");
924         rcontext = strtok(NULL, "@");
925         strtok(host, "@");
926         username = strtok(NULL, "@");
927         if (username) {
928                 /* Really the second argument is the host, not the username */
929                 hname = username;
930                 username = host;
931         } else {
932                 hname = host;
933         }
934         if (strtok(hname, ":")) {
935                 strtok(hname, ":");
936                 portno = strtok(hname, ":");
937         }
938         if (create_addr(&sin, hname)) {
939                 ast_log(LOG_WARNING, "No address associated with '%s'\n", hname);
940                 return -1;
941         }
942         if (portno) {
943                 sin.sin_port = htons(atoi(portno));
944         }
945         /* Now we build our request string */
946 #define MYSNPRINTF snprintf(requeststr + strlen(requeststr), sizeof(requeststr) - strlen(requeststr), 
947         MYSNPRINTF "exten=%s;", rdest);
948         if (c->callerid)
949                 MYSNPRINTF "callerid=%s;", c->callerid);
950         if (c->dnid)
951                 MYSNPRINTF "dnid=%s;", c->dnid);
952         if (rcontext)
953                 MYSNPRINTF "context=%s;", rcontext);
954         if (username)
955                 MYSNPRINTF "username=%s;", username);
956         MYSNPRINTF "formats=%d;", c->format);
957         MYSNPRINTF "version=%d;", AST_IAX_PROTO_VERSION);
958         /* Trim the trailing ";" */
959         if (strlen(requeststr))
960                 requeststr[strlen(requeststr) - 1] = '\0';
961         /* Transmit the string in a "NEW" request */
962         if (option_verbose > 2)
963                 ast_verbose(VERBOSE_PREFIX_3 "Calling using options '%s'\n", requeststr);
964         send_command((struct chan_iax_pvt *)c->pvt->pvt, AST_FRAME_IAX,
965                 AST_IAX_COMMAND_NEW, 0, requeststr, strlen(requeststr) + 1, -1);
966         c->state = AST_STATE_RINGING;
967         return 0;
968 }
969
970 static void iax_destroy(int callno)
971 {
972         char zero=0;
973         struct chan_iax_pvt *pvt = iaxs[callno];
974         if (pvt) {
975                 if (pvt->owner) {
976                         /* If there's an owner, prod it to give up */
977                         write(pvt->pipe[1], &zero, 1);
978                         return;
979                 }
980                 iaxs[callno] = NULL;
981                 close(pvt->pipe[0]);
982                 close(pvt->pipe[1]);
983                 free(pvt);
984         }
985 }
986
987 static int iax_hangup(struct ast_channel *c) {
988         struct chan_iax_pvt *pvt = c->pvt->pvt;
989         /* Send the hangup unless we have had a transmission error */
990         if (!pvt->error) {
991                 send_command(pvt, AST_FRAME_IAX, AST_IAX_COMMAND_HANGUP, 0, NULL, 0, -1);
992                 /* Wait for the network thread to transmit our command -- of course, if
993                    it doesn't, that's okay too -- the other end will find out
994                    soon enough, but it's a nicity if it can know now.  */
995                 sleep(1);
996         }
997         pthread_mutex_lock(&iaxs_lock);
998         c->pvt->pvt = NULL;
999         pvt->owner = NULL;
1000         pthread_mutex_lock(&usecnt_lock);
1001         usecnt--;
1002         if (usecnt < 0) 
1003                 ast_log(LOG_WARNING, "Usecnt < 0???\n");
1004         pthread_mutex_unlock(&usecnt_lock);
1005         ast_update_use_count();
1006         if (option_verbose > 2) 
1007                 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", c->name);
1008         iax_destroy(pvt->callno);
1009         pthread_mutex_unlock(&iaxs_lock);
1010         return 0;
1011 }
1012
1013 static struct ast_frame *iax_read(struct ast_channel *c) 
1014 {
1015         struct chan_iax_pvt *pvt = c->pvt->pvt;
1016         struct ast_frame *f;
1017         if (pvt->error) {
1018                 ast_log(LOG_DEBUG, "Connection closed, error: %s\n", strerror(pvt->error));
1019                 return NULL;
1020         }
1021         f = ast_fr_fdread(pvt->pipe[0]);
1022         if (f) {
1023                 if ((f->frametype == AST_FRAME_CONTROL) &&
1024                     (f->subclass == AST_CONTROL_ANSWER))
1025                                 c->state = AST_STATE_UP;
1026         }
1027         return f;
1028 }
1029
1030 static int iax_answer(struct ast_channel *c)
1031 {
1032         struct chan_iax_pvt *pvt = c->pvt->pvt;
1033         if (option_debug)
1034                 ast_log(LOG_DEBUG, "Answering\n");
1035         return send_command(pvt, AST_FRAME_CONTROL, AST_CONTROL_ANSWER, 0, NULL, 0, -1);
1036 }
1037
1038 static int iax_write(struct ast_channel *c, struct ast_frame *f);
1039
1040 static struct ast_channel *ast_iax_new(struct chan_iax_pvt *i, int state)
1041 {
1042         struct ast_channel *tmp;
1043         tmp = ast_channel_alloc();
1044         if (tmp) {
1045                 snprintf(tmp->name, sizeof(tmp->name), "IAX[%s:%d]/%d", inet_ntoa(i->addr.sin_addr), ntohs(i->addr.sin_port), i->callno);
1046                 tmp->type = type;
1047                 tmp->fd = i->pipe[0];
1048                 /* We can support any format by default, until we get restricted */
1049                 tmp->format = iax_capability;
1050                 tmp->pvt->pvt = i;
1051                 tmp->pvt->send_digit = iax_digit;
1052                 tmp->pvt->send_text = iax_sendtext;
1053                 tmp->pvt->call = iax_call;
1054                 tmp->pvt->hangup = iax_hangup;
1055                 tmp->pvt->answer = iax_answer;
1056                 tmp->pvt->read = iax_read;
1057                 tmp->pvt->write = iax_write;
1058                 if (strlen(i->callerid))
1059                         tmp->callerid = strdup(i->callerid);
1060                 if (strlen(i->dnid))
1061                         tmp->dnid = strdup(i->dnid);
1062                 strncpy(tmp->context, i->context, sizeof(tmp->context));
1063                 strncpy(tmp->exten, i->exten, sizeof(tmp->exten));
1064                 i->owner = tmp;
1065                 tmp->state = state;
1066                 pthread_mutex_lock(&usecnt_lock);
1067                 usecnt++;
1068                 pthread_mutex_unlock(&usecnt_lock);
1069                 ast_update_use_count();
1070                 if (state != AST_STATE_DOWN) {
1071                         if (ast_pbx_start(tmp)) {
1072                                 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
1073                                 ast_hangup(tmp);
1074                                 tmp = NULL;
1075                         }
1076                 }
1077         }
1078         return tmp;
1079 }
1080
1081 static unsigned int calc_timestamp(struct chan_iax_pvt *p, unsigned int ts)
1082 {
1083         struct timeval tv;
1084         unsigned int ms;
1085         if (!p->offset.tv_sec && !p->offset.tv_usec)
1086                 gettimeofday(&p->offset, NULL);
1087         /* If the timestamp is specified, just send it as is */
1088         if (ts)
1089                 return ts;
1090         gettimeofday(&tv, NULL);
1091         ms = (tv.tv_sec - p->offset.tv_sec) * 1000 + (tv.tv_usec - p->offset.tv_usec) / 1000;
1092         /* We never send the same timestamp twice, so fudge a little if we must */
1093         if (ms <= p->lastsent)
1094                 ms = p->lastsent + 1;
1095         p->lastsent = ms;
1096         return ms;
1097 }
1098
1099 static unsigned int calc_rxstamp(struct chan_iax_pvt *p)
1100 {
1101         /* Returns where in "receive time" we are */
1102         struct timeval tv;
1103         unsigned int ms;
1104         if (!p->rxcore.tv_sec && !p->rxcore.tv_usec)
1105                 gettimeofday(&p->rxcore, NULL);
1106         gettimeofday(&tv, NULL);
1107         ms = (tv.tv_sec - p->offset.tv_sec) * 1000 + (tv.tv_usec - p->offset.tv_usec) / 1000;
1108         return ms;
1109 }
1110 static int iax_send(struct chan_iax_pvt *pvt, struct ast_frame *f, unsigned int ts, int seqno)
1111 {
1112         /* Queue a packet for delivery on a given private structure.  Use "ts" for
1113            timestamp, or calculate if ts is 0 */
1114         struct ast_iax_full_hdr *fh;
1115         struct ast_iax_mini_hdr *mh;
1116         struct ast_iax_frame *fr;
1117         int res;
1118         unsigned int lastsent;
1119         /* Allocate an ast_iax_frame */
1120         fr = malloc(sizeof(struct ast_iax_frame));
1121         if (!fr) {
1122                 ast_log(LOG_WARNING, "Out of memory\n");
1123                 return -1;
1124         }
1125         if (!pvt) {
1126                 ast_log(LOG_WARNING, "No private structure for packet (%d)?\n", fr->callno);
1127                 free(fr);
1128                 return -1;
1129         }
1130         /* Isolate our frame for transmission */
1131         fr->f = ast_frdup(f);
1132         if (!fr->f) {
1133                 ast_log(LOG_WARNING, "Out of memory\n");
1134                 free(fr);
1135                 return -1;
1136         }
1137         if (fr->f->offset < sizeof(struct ast_iax_full_hdr)) {
1138                 ast_log(LOG_WARNING, "Packet from '%s' not friendly\n", fr->f->src);
1139                 free(fr);
1140                 return -1;
1141         }
1142         lastsent = pvt->lastsent;
1143         fr->ts = calc_timestamp(pvt, ts);
1144         if (!fr->ts) {
1145                 ast_log(LOG_WARNING, "timestamp is 0?\n");
1146                 return -1;
1147         }
1148         fr->callno = pvt->callno;
1149         if (((fr->ts & 0xFFFF0000L) != (lastsent & 0xFFFF0000L))
1150                 /* High two bits of timestamp differ */ ||
1151             (fr->f->frametype != AST_FRAME_VOICE) 
1152                 /* or not a voice frame */ || 
1153                 (fr->f->subclass != pvt->svoiceformat) 
1154                 /* or new voice format */ ) {
1155                 /* We need a full frame */
1156                 if (seqno > -1)
1157                         fr->seqno = seqno;
1158                 else
1159                         fr->seqno = pvt->oseqno++;
1160                 fh = (struct ast_iax_full_hdr *)(fr->f->data - sizeof(struct ast_iax_full_hdr));
1161                 fh->callno = htons(fr->callno | AST_FLAG_FULL);
1162                 fh->ts = htonl(fr->ts);
1163                 fh->seqno = htons(fr->seqno);
1164                 fh->type = fr->f->frametype & 0xFF;
1165                 fh->csub = compress_subclass(fr->f->subclass);
1166 #if 0
1167                 fh->subclasshigh = (fr->f->subclass & 0xFF0000) >> 16;
1168                 fh->subclasslow = htons(fr->f->subclass & 0xFFFF);
1169 #endif
1170                 fh->dcallno = htons(pvt->peercallno);
1171                 fr->datalen = fr->f->datalen + sizeof(struct ast_iax_full_hdr);
1172                 fr->data = fh;
1173                 fr->retries = 0;
1174                 /* Retry after 2x the ping time has passed */
1175                 fr->retrytime = pvt->pingtime * 2;
1176                 if (fr->retrytime < MIN_RETRY_TIME)
1177                         fr->retrytime = MIN_RETRY_TIME;
1178                 if (fr->retrytime > MAX_RETRY_TIME)
1179                         fr->retrytime = MAX_RETRY_TIME;
1180                 /* Acks' don't get retried */
1181                 if ((f->frametype == AST_FRAME_IAX) && (f->subclass == AST_IAX_COMMAND_ACK))
1182                         fr->retries = -1;
1183                 if (f->frametype == AST_FRAME_VOICE) {
1184                         pvt->svoiceformat = f->subclass;
1185                 }
1186                 res = iax_transmit(fr);
1187         } else {
1188                 /* Mini-frames have no sequence number */
1189                 fr->seqno = -1;
1190                 /* Mini frame will do */
1191                 mh = (struct ast_iax_mini_hdr *)(fr->f->data - sizeof(struct ast_iax_mini_hdr));
1192                 mh->callno = htons(fr->callno);
1193                 mh->ts = htons(fr->ts & 0xFFFF);
1194                 fr->datalen = fr->f->datalen + sizeof(struct ast_iax_mini_hdr);
1195                 fr->data = mh;
1196                 fr->retries = -1;
1197                 res = iax_transmit(fr);
1198         }
1199         return res;
1200 }
1201
1202
1203
1204 static int iax_show_users(int fd, int argc, char *argv[])
1205 {
1206 #define FORMAT "%-15.15s  %-15.15s  %-15.15s  %-15.15s  %-5.5s\n"
1207         struct iax_user *user;
1208         if (argc != 3) 
1209                 return RESULT_SHOWUSAGE;
1210         pthread_mutex_lock(&userl.lock);
1211         ast_cli(fd, FORMAT, "Username", "Secret", "Authen", "Def.Context", "A/C");
1212         for(user=userl.users;user;user=user->next) {
1213                 ast_cli(fd, FORMAT, user->name, user->secret, user->methods, 
1214                                 user->contexts ? user->contexts->context : context,
1215                                 user->ha ? "Yes" : "No");
1216         }
1217         pthread_mutex_unlock(&userl.lock);
1218         return RESULT_SUCCESS;
1219 #undef FORMAT
1220 }
1221
1222 static int iax_show_peers(int fd, int argc, char *argv[])
1223 {
1224 #define FORMAT2 "%-15.15s  %-15.15s  %-15.15s  %-15.15s  %s\n"
1225 #define FORMAT "%-15.15s  %-15.15s  %-15.15s  %-15.15s  %d\n"
1226         struct iax_peer *peer;
1227         if (argc != 3)
1228                 return RESULT_SHOWUSAGE;
1229         pthread_mutex_lock(&peerl.lock);
1230         ast_cli(fd, FORMAT2, "Name", "Username", "Host", "Mask", "Port");
1231         for (peer = peerl.peers;peer;peer = peer->next) {
1232                 char nm[20];
1233                 strncpy(nm, inet_ntoa(peer->mask), sizeof(nm));
1234                 ast_cli(fd, FORMAT, peer->name, 
1235                                         peer->username ? peer->username : "(Any)",
1236                                         peer->addr.sin_addr.s_addr ? inet_ntoa(peer->addr.sin_addr) : "(Any)",
1237                                         nm,
1238                                         ntohs(peer->addr.sin_port));
1239         }
1240         pthread_mutex_unlock(&peerl.lock);
1241         return RESULT_SUCCESS;
1242 #undef FORMAT
1243 #undef FORMAT2
1244 }
1245
1246 static int iax_show_channels(int fd, int argc, char *argv[])
1247 {
1248 #define FORMAT2 "%-15.15s  %-10.10s  %-11.11s  %-11.11s  %-7.7s  %-6.6s  %s\n"
1249 #define FORMAT  "%-15.15s  %-10.10s  %5.5d/%5.5d  %5.5d/%5.5d  %-5.5dms  %-4.4dms  %d\n"
1250         int x;
1251         if (argc != 3)
1252                 return RESULT_SHOWUSAGE;
1253         pthread_mutex_lock(&iaxs_lock);
1254         ast_cli(fd, FORMAT2, "Peer", "Username", "ID (Lo/Rem)", "Seq (Tx/Rx)", "Lag", "Jitter", "Format");
1255         for (x=0;x<AST_IAX_MAX_CALLS;x++)
1256                 if (iaxs[x]) 
1257                         ast_cli(fd, FORMAT, inet_ntoa(iaxs[x]->addr.sin_addr), 
1258                                                 strlen(iaxs[x]->username) ? iaxs[x]->username : "(None)", 
1259                                                 iaxs[x]->callno, iaxs[x]->peercallno, 
1260                                                 iaxs[x]->oseqno, iaxs[x]->iseqno, 
1261                                                 iaxs[x]->lag,
1262                                                 iaxs[x]->jitter,
1263                                                 iaxs[x]->voiceformat);
1264         pthread_mutex_unlock(&iaxs_lock);
1265         return RESULT_SUCCESS;
1266 #undef FORMAT
1267 #undef FORMAT2
1268 }
1269
1270 static char show_users_usage[] = 
1271 "Usage: iax show users\n"
1272 "       Lists all users known to the IAX (Inter-Asterisk eXchange) subsystem.\n";
1273
1274 static char show_channels_usage[] = 
1275 "Usage: iax show channels\n"
1276 "       Lists all currently active IAX channels.\n";
1277
1278 static char show_peers_usage[] = 
1279 "Usage: iax show peers\n"
1280 "       Lists all known IAX peers.\n";
1281
1282 static struct ast_cli_entry  cli_show_users = 
1283         { { "iax", "show", "users", NULL }, iax_show_users, "Show defined IAX users", show_users_usage };
1284 static struct ast_cli_entry  cli_show_channels =
1285         { { "iax", "show", "channels", NULL }, iax_show_channels, "Show active IAX channels", show_channels_usage };
1286 static struct ast_cli_entry  cli_show_peers =
1287         { { "iax", "show", "peers", NULL }, iax_show_peers, "Show defined IAX peers", show_peers_usage };
1288
1289 static int iax_write(struct ast_channel *c, struct ast_frame *f)
1290 {
1291         struct chan_iax_pvt *i = c->pvt->pvt;
1292         /* If there's an outstanding error, return failure now */
1293         if (i->error) {
1294                 ast_log(LOG_DEBUG, "Write error: %s\n", strerror(errno));
1295                 return -1;
1296         }
1297         /* Don't waste bandwidth sending null frames */
1298         if (f->frametype == AST_FRAME_NULL)
1299                 return 0;
1300         /* Simple, just queue for transmission */
1301         return iax_send(i, f, 0, -1);
1302 }
1303
1304 static int send_command(struct chan_iax_pvt *i, char type, int command, unsigned int ts, char *data, int datalen, int seqno)
1305 {
1306         struct ast_frame f;
1307         f.frametype = type;
1308         f.subclass = command;
1309         f.datalen = datalen;
1310         f.timelen = 0;
1311         f.mallocd = 0;
1312         f.offset = 0;
1313         f.src = __FUNCTION__;
1314         f.data = data;
1315         return iax_send(i, &f, ts, seqno);
1316 }
1317
1318 static int apply_context(struct iax_context *con, char *context)
1319 {
1320         while(con) {
1321                 if (!strcmp(con->context, context))
1322                         return -1;
1323                 con = con->next;
1324         }
1325         return 0;
1326 }
1327
1328 static int apply_ha(struct iax_ha *ha, struct sockaddr_in *sin)
1329 {
1330         /* Start optimistic */
1331         int res = IAX_SENSE_ALLOW;
1332         while(ha) {
1333                 /* For each rule, if this address and the netmask = the net address
1334                    apply the current rule */
1335                 if ((sin->sin_addr.s_addr & ha->netmask.s_addr) == (ha->netaddr.s_addr))
1336                         res = ha->sense;
1337                 ha = ha->next;
1338         }
1339         return res;
1340 }
1341
1342 static int iax_getformats(int callno, char *orequest)
1343 {
1344         char *var, *value;
1345         char request[256];
1346         strncpy(request, orequest, sizeof(request));
1347         var = strtok(request, ";");
1348         while(var) {
1349                 value = strchr(var, '=');
1350                 if (value) {
1351                         *value='\0';
1352                         value++;
1353                         if (!strcmp(var, "formats")) {
1354                                 iaxs[callno]->peerformats = atoi(value);
1355                         } else 
1356                                 ast_log(LOG_WARNING, "Unknown variable '%s' with value '%s'\n", var, value);
1357                 }
1358                 var = strtok(NULL, ";");
1359         }
1360         return 0;
1361 }
1362
1363 static int check_access(int callno, struct sockaddr_in *sin, char *orequest, int requestl)
1364 {
1365         /* Start pessimistic */
1366         int res = -1;
1367         int version = 1;
1368         char *var, *value;
1369         struct iax_user *user;
1370         char request[256];
1371         strncpy(request, orequest, sizeof(request));
1372         if (!iaxs[callno])
1373                 return res;
1374         var = strtok(request, ";");
1375         while(var) {
1376                 value = strchr(var, '=');
1377                 if (value) { 
1378                         *value='\0';
1379                         value++;
1380                         if (!strcmp(var, "exten")) 
1381                                 strncpy(iaxs[callno]->exten, value, sizeof(iaxs[callno]->exten));
1382                         else if (!strcmp(var, "callerid"))
1383                                 strncpy(iaxs[callno]->callerid, value, sizeof(iaxs[callno]->callerid));
1384                         else if (!strcmp(var, "dnid"))
1385                                 strncpy(iaxs[callno]->dnid, value, sizeof(iaxs[callno]->dnid));
1386                         else if (!strcmp(var, "context"))
1387                                 strncpy(iaxs[callno]->context, value, sizeof(iaxs[callno]->context));
1388                         else if (!strcmp(var, "username"))
1389                                 strncpy(iaxs[callno]->username, value, sizeof(iaxs[callno]->username));
1390                         else if (!strcmp(var, "formats"))
1391                                 iaxs[callno]->peerformats = atoi(value);
1392                         else if (!strcmp(var, "version"))
1393                                 version = atoi(value);
1394                         else 
1395                                 ast_log(LOG_WARNING, "Unknown variable '%s' with value '%s'\n", var, value);
1396                 }
1397                 var = strtok(NULL, ";");
1398         }
1399         if (version > AST_IAX_PROTO_VERSION) {
1400                 ast_log(LOG_WARNING, "Peer '%s' has too new a protocol version (%d) for me\n", 
1401                         inet_ntoa(sin->sin_addr), version);
1402                 return res;
1403         }
1404         pthread_mutex_lock(&userl.lock);
1405         /* Search the userlist for a compatible entry, and fill in the rest */
1406         user = userl.users;
1407         while(user) {
1408                 if ((!strlen(iaxs[callno]->username) ||                         /* No username specified */
1409                         !strcmp(iaxs[callno]->username, user->name))    /* Or this username specified */
1410                         && (apply_ha(user->ha, sin) == IAX_SENSE_ALLOW) /* Access is permitted from this IP */
1411                         && (!strlen(iaxs[callno]->context) ||                   /* No context specified */
1412                              apply_context(user->contexts, iaxs[callno]->context))) {                   /* Context is permitted */
1413                         /* We found our match (use the first) */
1414                         
1415                         /* Store the requested username if not specified */
1416                         if (!strlen(iaxs[callno]->username))
1417                                 strncpy(iaxs[callno]->username, user->name, sizeof(iaxs[callno]->username));
1418                         /* And use the default context */
1419                         if (!strlen(iaxs[callno]->context)) {
1420                                 if (user->contexts)
1421                                         strncpy(iaxs[callno]->context, user->contexts->context, sizeof(iaxs[callno]->context));
1422                                 else
1423                                         strncpy(iaxs[callno]->context, context, sizeof(iaxs[callno]->context));
1424                         }
1425                         /* Copy the secret */
1426                         strncpy(iaxs[callno]->secret, user->secret, sizeof(iaxs[callno]->secret));
1427                         /* And the permitted authentication methods */
1428                         strncpy(iaxs[callno]->methods, user->methods, sizeof(iaxs[callno]->methods));
1429                         res = 0;
1430                         break;
1431                 }
1432                 user = user->next;      
1433         }
1434         pthread_mutex_unlock(&userl.lock);
1435         return res;
1436 }
1437
1438 static int raw_hangup(struct sockaddr_in *sin, short src, short dst)
1439 {
1440         struct ast_iax_full_hdr fh;
1441         fh.callno = htons(src | AST_FLAG_FULL);
1442         fh.dcallno = htons(dst);
1443         fh.ts = 0;
1444         fh.seqno = 0;
1445         fh.type = AST_FRAME_IAX;
1446         fh.csub = compress_subclass(AST_IAX_COMMAND_INVAL);
1447         if (option_debug)
1448                 ast_log(LOG_DEBUG, "Raw Hangup\n");
1449         return sendto(netsocket, &fh, sizeof(fh), 0, sin, sizeof(*sin));
1450 }
1451
1452 static int authenticate_request(struct chan_iax_pvt *p)
1453 {
1454         char requeststr[256] = "";
1455         MYSNPRINTF "methods=%s;", p->methods);
1456         if (strstr(p->methods, "md5")) {
1457                 /* Build the challenge */
1458                 srand(time(NULL));
1459                 snprintf(p->challenge, sizeof(p->challenge), "%d", rand());
1460                 MYSNPRINTF "challenge=%s;", p->challenge);
1461         }
1462         MYSNPRINTF "username=%s;", p->username);
1463         if (strlen(requeststr))
1464                 requeststr[strlen(requeststr) - 1] = '\0';
1465         return send_command(p, AST_FRAME_IAX, AST_IAX_COMMAND_AUTHREQ, 0, requeststr, strlen(requeststr) + 1, -1);
1466 }
1467
1468 static int authenticate_verify(struct chan_iax_pvt *p, char *orequest)
1469 {
1470         char requeststr[256] = "";
1471         char *var, *value, request[256];
1472         char md5secret[256] = "";
1473         char secret[256] = "";
1474         int res = -1; 
1475         int x;
1476         
1477         if (!(p->state & IAX_STATE_AUTHENTICATED))
1478                 return res;
1479         strncpy(request, orequest, sizeof(request));
1480         var = strtok(request, ";");
1481         while(var) {
1482                 value = strchr(var, '=');
1483                 if (value) { 
1484                         *value='\0';
1485                         value++;
1486                         if (!strcmp(var, "secret")) 
1487                                 strncpy(secret, value, sizeof(secret));
1488                         else if (!strcmp(var, "md5secret"))
1489                                 strncpy(md5secret, value, sizeof(md5secret));
1490                         else 
1491                                 ast_log(LOG_WARNING, "Unknown variable '%s' with value '%s'\n", var, value);
1492                 }
1493                 var = strtok(NULL, ";");
1494         }
1495         if (strstr(p->methods, "md5")) {
1496                 struct MD5Context md5;
1497                 unsigned char digest[16];
1498                 MD5Init(&md5);
1499                 MD5Update(&md5, p->challenge, strlen(p->challenge));
1500                 MD5Update(&md5, p->secret, strlen(p->secret));
1501                 MD5Final(digest, &md5);
1502                 /* If they support md5, authenticate with it.  */
1503                 for (x=0;x<16;x++)
1504                         MYSNPRINTF "%2.2x", digest[x]);
1505                 if (!strcasecmp(requeststr, md5secret))
1506                         res = 0;
1507         } else if (strstr(p->methods, "plaintext")) {
1508                 if (!strcmp(secret, p->secret))
1509                         res = 0;
1510         }
1511         return res;
1512 }
1513
1514 static int authenticate_reply(struct chan_iax_pvt *p, struct sockaddr_in *sin, char *orequest)
1515 {
1516         struct iax_peer *peer;
1517         /* Start pessimistic */
1518         int res = -1;
1519         char request[256];
1520         char methods[80] = "";
1521         char requeststr[256] = "";
1522         char *var, *value;
1523         int x;
1524         strncpy(request, orequest, sizeof(request));
1525         var = strtok(request, ";");
1526         while(var) {
1527                 value = strchr(var, '=');
1528                 if (value) { 
1529                         *value='\0';
1530                         value++;
1531                         if (!strcmp(var, "username")) 
1532                                 strncpy(p->username, value, sizeof(p->username));
1533                         else if (!strcmp(var, "challenge"))
1534                                 strncpy(p->challenge, value, sizeof(p->challenge));
1535                         else if (!strcmp(var, "methods"))
1536                                 strncpy(methods, value, sizeof(methods));
1537                         else 
1538                                 ast_log(LOG_WARNING, "Unknown variable '%s' with value '%s'\n", var, value);
1539                 }
1540                 var = strtok(NULL, ";");
1541         }
1542         pthread_mutex_lock(&peerl.lock);
1543         peer = peerl.peers;
1544         while(peer) {
1545                 if ((!strlen(p->peer) || !strcmp(p->peer, peer->name)) 
1546                                                                 /* No peer specified at our end, or this is the peer */
1547                          && (!strlen(peer->username) || (!strcmp(peer->username, p->username)))
1548                                                                 /* No username specified in peer rule, or this is the right username */
1549                          && (!peer->addr.sin_addr.s_addr || ((sin->sin_addr.s_addr & peer->mask.s_addr) == (peer->addr.sin_addr.s_addr & peer->mask.s_addr)))
1550                                                                 /* No specified host, or this is our host */
1551                         ) {
1552                         /* We have a match, authenticate it. */
1553                         res = 0;
1554                         if (strstr(methods, "md5")) {
1555                                 struct MD5Context md5;
1556                                 unsigned char digest[16];
1557                                 MD5Init(&md5);
1558                                 MD5Update(&md5, p->challenge, strlen(p->challenge));
1559                                 MD5Update(&md5, peer->secret, strlen(peer->secret));
1560                                 MD5Final(digest, &md5);
1561                                 /* If they support md5, authenticate with it.  */
1562                                 MYSNPRINTF "md5secret=");
1563                                 for (x=0;x<16;x++)
1564                                         MYSNPRINTF "%2.2x", digest[x]);
1565                                 MYSNPRINTF ";");
1566                         } else if (strstr(methods, "plaintext")) {
1567                                 MYSNPRINTF "secret=%s;", peer->secret);
1568                         } else 
1569                                 res = -1;
1570                         if (strlen(requeststr))
1571                                 requeststr[strlen(requeststr)-1] = '\0';
1572                         if (!res)
1573                                 res = send_command(p, AST_FRAME_IAX, AST_IAX_COMMAND_AUTHREP, 0, requeststr, strlen(requeststr) + 1, -1);
1574                         break;  
1575                 }
1576                 peer = peer->next;
1577         }
1578         pthread_mutex_unlock(&peerl.lock);
1579         return res;
1580 }
1581
1582 static int socket_read(int *id, int fd, short events, void *cbdata)
1583 {
1584         struct sockaddr_in sin;
1585         int res;
1586         int new = NEW_PREVENT;
1587         char buf[4096];
1588         char src[80];
1589         int len = sizeof(sin);
1590         int dcallno = -1;
1591         struct ast_iax_full_hdr *fh = (struct ast_iax_full_hdr *)buf;
1592         struct ast_iax_mini_hdr *mh = (struct ast_iax_mini_hdr *)buf;
1593         struct ast_iax_frame fr, *cur;
1594         struct ast_frame f;
1595         struct ast_channel *c;
1596         res = recvfrom(netsocket, buf, sizeof(buf), 0, &sin, &len);
1597         if (res < 0) {
1598                 ast_log(LOG_WARNING, "Error: %s\n", strerror(errno));
1599                 handle_error();
1600                 return 1;
1601         }
1602         if (res < sizeof(struct ast_iax_mini_hdr)) {
1603                 ast_log(LOG_WARNING, "midget packet received (%d of %d min)\n", res, sizeof(struct ast_iax_mini_hdr));
1604                 return 1;
1605         }
1606         if (ntohs(mh->callno) & AST_FLAG_FULL) {
1607                 /* Get the destination call number */
1608                 dcallno = ntohs(fh->dcallno);
1609                 /* Retrieve the type and subclass */
1610                 f.frametype = fh->type;
1611                 f.subclass = uncompress_subclass(fh->csub);
1612 #if 0
1613                 f.subclass = fh->subclasshigh << 16;
1614                 f.subclass += ntohs(fh->subclasslow);
1615 #endif
1616                 if ((f.frametype == AST_FRAME_IAX) && (f.subclass == AST_IAX_COMMAND_NEW))
1617                         new = NEW_ALLOW;
1618         }
1619         pthread_mutex_lock(&iaxs_lock);
1620         fr.callno = find_callno(ntohs(mh->callno) & ~AST_FLAG_FULL, dcallno, &sin, new);
1621         if ((fr.callno < 0) || !iaxs[fr.callno]) {
1622                 /* A call arrived for a non-existant destination.  Unless it's an "inval"
1623                    frame, reply with an inval */
1624                 if (ntohs(mh->callno) & AST_FLAG_FULL) {
1625                         /* We can only raw hangup control frames */
1626                         if ((f.subclass != AST_IAX_COMMAND_INVAL) || (f.frametype != AST_FRAME_IAX))
1627                                 raw_hangup(&sin, ntohs(fh->dcallno), ntohs(mh->callno));
1628                 }
1629                 pthread_mutex_unlock(&iaxs_lock);
1630                 return 1;
1631         }
1632         iaxs[fr.callno]->peercallno = ntohs(mh->callno) & ~AST_FLAG_FULL;
1633         if (ntohs(mh->callno) & AST_FLAG_FULL) {
1634                 if (option_debug)
1635                         ast_log(LOG_DEBUG, "Received packet %d, (%d, %d)\n", ntohs(fh->seqno), f.frametype, f.subclass);
1636                 /* Check if it's out of order (and not an ACK or INVAL) */
1637                 fr.seqno = ntohs(fh->seqno);
1638                 if (iaxs[fr.callno]->iseqno != fr.seqno) {
1639                         if (
1640                          ((f.subclass != AST_IAX_COMMAND_ACK) && (f.subclass != AST_IAX_COMMAND_INVAL)) ||
1641                          (f.frametype != AST_FRAME_IAX)) {
1642                                 /* If it's not an ACK packet, it's out of order. */
1643                                 if (option_debug)
1644                                         ast_log(LOG_DEBUG, "Packet arrived out of order (expecting %d, got %d) (frametype = %d, subclass = %d)\n", 
1645                                         iaxs[fr.callno]->iseqno, fr.seqno, f.frametype, f.subclass);
1646                                 if (iaxs[fr.callno]->iseqno > fr.seqno) {
1647                                         /* If we've already seen it, ack it XXX There's a border condition here XXX */
1648                                         if ((f.frametype != AST_FRAME_IAX) || 
1649                                                         ((f.subclass != AST_IAX_COMMAND_ACK) && (f.subclass != AST_IAX_COMMAND_INVAL))) {
1650                                                 if (option_debug)
1651                                                         ast_log(LOG_DEBUG, "Acking anyway\n");
1652                                                 send_command(iaxs[fr.callno], AST_FRAME_IAX, AST_IAX_COMMAND_ACK, fr.ts, NULL, 0,fr.seqno);
1653                                         }
1654                                 }
1655                                 pthread_mutex_unlock(&iaxs_lock);
1656                                 return 1;
1657                         }
1658                 } else {
1659                         /* Increment unless it's an ACK */
1660                         if ((f.subclass != AST_IAX_COMMAND_ACK) ||
1661                             (f.frametype != AST_FRAME_IAX))
1662                                 iaxs[fr.callno]->iseqno++;
1663                 }
1664                 /* A full frame */
1665                 if (res < sizeof(struct ast_iax_full_hdr)) {
1666                         ast_log(LOG_WARNING, "midget packet received (%d of %d min)\n", res, sizeof(struct ast_iax_full_hdr));
1667                         pthread_mutex_unlock(&iaxs_lock);
1668                         return 1;
1669                 }
1670                 f.datalen = res - sizeof(struct ast_iax_full_hdr);
1671                 if (f.datalen)
1672                         f.data = buf + sizeof(struct ast_iax_full_hdr);
1673                 else
1674                         f.data = NULL;
1675                 fr.ts = ntohl(fh->ts);
1676                 /* Unless this is an ACK or INVAL frame, ack it */
1677                 if ((f.frametype != AST_FRAME_IAX) || 
1678                          ((f.subclass != AST_IAX_COMMAND_ACK) && (f.subclass != AST_IAX_COMMAND_INVAL))) 
1679                         send_command(iaxs[fr.callno], AST_FRAME_IAX, AST_IAX_COMMAND_ACK, fr.ts, NULL, 0,fr.seqno);
1680                 if (f.frametype == AST_FRAME_VOICE)
1681                         iaxs[fr.callno]->voiceformat = f.subclass;
1682                 if (f.frametype == AST_FRAME_IAX) {
1683                         /* Handle the IAX pseudo frame itself */
1684                         if (option_debug)
1685                                 ast_log(LOG_DEBUG, "IAX subclass %d received\n", f.subclass);
1686                         switch(f.subclass) {
1687                         case AST_IAX_COMMAND_ACK:
1688                                 /* Ack the packet with the given timestamp */
1689                                 pthread_mutex_lock(&iaxq.lock);
1690                                 for (cur = iaxq.head; cur ; cur = cur->next) {
1691                                         /* If it's our call, and our timestamp, mark -1 retries */
1692                                         if ((fr.callno == cur->callno) && (fr.seqno == cur->seqno))
1693                                                 cur->retries = -1;
1694                                 }
1695                                 pthread_mutex_unlock(&iaxq.lock);
1696                                 break;
1697                         case AST_IAX_COMMAND_NEW:
1698                                 ((char *)f.data)[f.datalen] = '\0';
1699                                 if (check_access(fr.callno, &sin, f.data, f.datalen)) {
1700                                         /* They're not allowed on */
1701                                         send_command(iaxs[fr.callno], AST_FRAME_IAX, AST_IAX_COMMAND_REJECT, 0, "No authority found", strlen("No authority found"), -1);
1702                                         ast_log(LOG_NOTICE, "Rejected connect attempt from %s, request '%s'\n", inet_ntoa(sin.sin_addr), f.data);
1703                                         /* XXX Not guaranteed to work, but probably does XXX */
1704                                         pthread_mutex_lock(&iaxq.lock);
1705                                         send_packet(iaxq.tail);
1706                                         pthread_mutex_unlock(&iaxq.lock);
1707                                         iax_destroy(fr.callno);
1708                                         break;
1709                                 }
1710                                 if (!strlen(iaxs[fr.callno]->secret)) {
1711                                         /* No authentication required, let them in */
1712                                         send_command(iaxs[fr.callno], AST_FRAME_IAX, AST_IAX_COMMAND_ACCEPT, 0, NULL, 0, -1);
1713                                         iaxs[fr.callno]->state |= IAX_STATE_STARTED;
1714                                         if(!(c = ast_iax_new(iaxs[fr.callno], AST_STATE_RING)))
1715                                                 iax_destroy(fr.callno);
1716                                         else
1717                                                 c->format = iaxs[fr.callno]->peerformats;
1718                                         break;
1719                                 }
1720                                 authenticate_request(iaxs[fr.callno]);
1721                                 iaxs[fr.callno]->state |= IAX_STATE_AUTHENTICATED;
1722                                 break;
1723                         case AST_IAX_COMMAND_HANGUP:
1724 #if 0
1725                                 iaxs[fr.callno]->error = ENOTCONN;
1726 #endif
1727                                 iax_destroy(fr.callno);
1728                                 break;
1729                         case AST_IAX_COMMAND_REJECT:
1730                                 if (f.data)
1731                                         ((char *)f.data)[f.datalen] = '\0';
1732                                 ast_log(LOG_WARNING, "Call rejected by %s: %s\n", inet_ntoa(iaxs[fr.callno]->addr.sin_addr), f.data);
1733                                 iaxs[fr.callno]->error = EPERM;
1734                                 iax_destroy(fr.callno);
1735                                 break;
1736                         case AST_IAX_COMMAND_ACCEPT:
1737                                 if (f.data) {
1738                                         ((char *)f.data)[f.datalen]='\0';
1739                                         iax_getformats(fr.callno, (char *)f.data);
1740                                 } else {
1741                                         iaxs[fr.callno]->peerformats = iax_capability;
1742                                 }
1743                                 if (option_verbose > 2)
1744                                         ast_verbose(VERBOSE_PREFIX_3 "Call accepted by %s\n", inet_ntoa(iaxs[fr.callno]->addr.sin_addr));
1745                                 iaxs[fr.callno]->state |= IAX_STATE_STARTED;
1746                                 if (iaxs[fr.callno]->owner) {
1747                                         /* Switch us to use a compatible format */
1748                                         iaxs[fr.callno]->owner->format &= iaxs[fr.callno]->peerformats;
1749
1750                                         if (!iaxs[fr.callno]->owner->format) 
1751                                                 iaxs[fr.callno]->owner->format = iaxs[fr.callno]->peerformats & iax_capability;
1752                                         if (!iaxs[fr.callno]->owner->format) {
1753                                                 ast_log(LOG_WARNING, "Unable to negotiate a common format with the peer.");
1754                                                 iaxs[fr.callno]->error = EBADE;
1755                                                 iax_destroy(fr.callno);
1756                                         } else {
1757                                                 if (option_verbose > 2)
1758                                                         ast_verbose(VERBOSE_PREFIX_3 "Format for call is %d\n", iaxs[fr.callno]->owner->format);
1759                                         }
1760                                                 
1761                                 }
1762                                 break;
1763                         case AST_IAX_COMMAND_PING:
1764                                 /* Send back a pong packet with the original timestamp */
1765                                 send_command(iaxs[fr.callno], AST_FRAME_IAX, AST_IAX_COMMAND_PONG, fr.ts, NULL, 0, -1);
1766                                 break;
1767                         case AST_IAX_COMMAND_PONG:
1768                                 iaxs[fr.callno]->pingtime =  calc_timestamp(iaxs[fr.callno], 0) - fr.ts;
1769                                 break;
1770                         case AST_IAX_COMMAND_LAGRQ:
1771                         case AST_IAX_COMMAND_LAGRP:
1772                                 /* A little strange -- We have to actually go through the motions of
1773                                    delivering the packet.  In the very last step, it will be properly
1774                                    handled by do_deliver */
1775                                 snprintf(src, sizeof(src), "LAGRQ-IAX/%s/%d", inet_ntoa(sin.sin_addr),fr.callno);
1776                                 f.src = src;
1777                                 f.mallocd = 0;
1778                                 f.offset = 0;
1779                                 fr.f = &f;
1780                                 f.timelen = 0;
1781                                 schedule_delivery(iaxfrdup2(&fr, 0));
1782                                 break;
1783                         case AST_IAX_COMMAND_AUTHREQ:
1784                                 ((char *)f.data)[f.datalen] = '\0';
1785                                 if (authenticate_reply(iaxs[fr.callno], &iaxs[fr.callno]->addr, (char *)f.data)) {
1786                                         ast_log(LOG_WARNING, 
1787                                                 "I don't know how to authenticate %s to %s\n", 
1788                                                 f.data, inet_ntoa(iaxs[fr.callno]->addr.sin_addr));
1789                                         iax_destroy(fr.callno);
1790                                 }
1791                                 break;
1792                         case AST_IAX_COMMAND_AUTHREP:
1793                                 ((char *)f.data)[f.datalen] = '\0';
1794                                 if (authenticate_verify(iaxs[fr.callno], (char *)f.data)) {
1795                                         ast_log(LOG_NOTICE, "Host %s failed to authenticate as %s\n", inet_ntoa(iaxs[fr.callno]->addr.sin_addr), iaxs[fr.callno]->username);
1796                                         send_command(iaxs[fr.callno], AST_FRAME_IAX, AST_IAX_COMMAND_REJECT, 0, "No authority found", strlen("No authority found"), -1);
1797                                         /* XXX Not guaranteed to work, but probably does XXX */
1798                                         pthread_mutex_lock(&iaxq.lock);
1799                                         send_packet(iaxq.tail);
1800                                         pthread_mutex_unlock(&iaxq.lock);
1801                                         iax_destroy(fr.callno);
1802                                         break;
1803                                 }
1804                                 /* Authentication is fine, go ahead */
1805                                 send_command(iaxs[fr.callno], AST_FRAME_IAX, AST_IAX_COMMAND_ACCEPT, 0, NULL, 0, -1);
1806                                 iaxs[fr.callno]->state |= IAX_STATE_STARTED;
1807                                 if(!(c = ast_iax_new(iaxs[fr.callno], AST_STATE_RING)))
1808                                         iax_destroy(fr.callno);
1809                                 else
1810                                         c->format = iaxs[fr.callno]->peerformats;
1811                                 break;
1812                         case AST_IAX_COMMAND_INVAL:
1813                                 iaxs[fr.callno]->error = ENOTCONN;
1814                                 iax_destroy(fr.callno);
1815                                 if (option_debug)
1816                                         ast_log(LOG_DEBUG, "Destroying call %d\n", fr.callno);
1817                                 break;
1818                         default:
1819                                 ast_log(LOG_DEBUG, "Unknown IAX command %d on %d/%d\n", f.subclass, fr.callno, iaxs[fr.callno]->peercallno);
1820                         }
1821                         /* Don't actually pass these frames along */
1822                         pthread_mutex_unlock(&iaxs_lock);
1823                         return 1;
1824                 }
1825         } else {
1826                 /* A mini frame */
1827                 f.frametype = AST_FRAME_VOICE;
1828                 if (iaxs[fr.callno]->voiceformat > 0)
1829                         f.subclass = iaxs[fr.callno]->voiceformat;
1830                 else {
1831                         ast_log(LOG_WARNING, "Received mini frame before first full voice frame\n ");
1832                         pthread_mutex_unlock(&iaxs_lock);
1833                         return 1;
1834                 }
1835                 f.datalen = res - sizeof(struct ast_iax_mini_hdr);
1836                 if (f.datalen < 0) {
1837                         ast_log(LOG_WARNING, "Datalen < 0?\n");
1838                         pthread_mutex_unlock(&iaxs_lock);
1839                         return 1;
1840                 }
1841                 if (f.datalen)
1842                         f.data = buf + sizeof(struct ast_iax_mini_hdr);
1843                 else
1844                         f.data = NULL;
1845                 fr.ts = (iaxs[fr.callno]->last & 0xFFFF0000L) | ntohs(mh->ts);
1846         }
1847         /* Don't pass any packets until we're started */
1848         if (!(iaxs[fr.callno]->state & IAX_STATE_STARTED)) {
1849                 pthread_mutex_unlock(&iaxs_lock);
1850                 return 1;
1851         }
1852         /* Common things */
1853         snprintf(src, sizeof(src), "IAX/%s/%d", inet_ntoa(sin.sin_addr),fr.callno);
1854         f.src = src;
1855         f.mallocd = 0;
1856         f.offset = 0;
1857         fr.f = &f;
1858         if (f.datalen && (f.frametype == AST_FRAME_VOICE)) 
1859                 f.timelen = get_timelen(&f);
1860         else
1861                 f.timelen = 0;
1862
1863         /* If this is our most recent packet, use it as our basis for timestamping */
1864         if (iaxs[fr.callno]->last < fr.ts) {
1865                 iaxs[fr.callno]->last = fr.ts;
1866                 fr.outoforder = 0;
1867         } else {
1868                 ast_log(LOG_DEBUG, "Received out of order packet... (type=%d, subclass %d, ts = %d, last = %d)\n", f.frametype, f.subclass, fr.ts, iaxs[fr.callno]->last);
1869                 fr.outoforder = -1;
1870         }
1871         schedule_delivery(iaxfrdup2(&fr, 0));
1872         /* Always run again */
1873         pthread_mutex_unlock(&iaxs_lock);
1874         return 1;
1875 }
1876
1877 static void free_ha(struct iax_ha *ha)
1878 {
1879         struct iax_ha *hal;
1880         while(ha) {
1881                 hal = ha;
1882                 ha = ha->next;
1883                 free(hal);
1884         }
1885 }
1886
1887 static void free_context(struct iax_context *con)
1888 {
1889         struct iax_context *conl;
1890         while(con) {
1891                 conl = con;
1892                 con = con->next;
1893                 free(conl);
1894         }
1895 }
1896
1897 static struct ast_channel *iax_request(char *type, int format, void *data)
1898 {
1899         int callno;
1900         struct sockaddr_in sin;
1901         char s[256];
1902         char *st;
1903         struct ast_channel *c;
1904         strncpy(s, (char *)data, sizeof(s));
1905         strtok(s, "/");
1906         strtok(s, "@");
1907         st = strtok(NULL, "@");
1908         if (!st)
1909                 st = s;
1910         /* Populate our address from the given */
1911         if (create_addr(&sin, st)) {
1912                 ast_log(LOG_WARNING, "Unable to assign address\n");
1913                 return NULL;
1914         }
1915         pthread_mutex_lock(&iaxs_lock);
1916         callno = find_callno(-1, -1, &sin, NEW_FORCE);
1917         if (callno < 0) {
1918                 ast_log(LOG_WARNING, "Unable to create call\n");
1919                 return NULL;
1920         }
1921         c = ast_iax_new(iaxs[callno], AST_STATE_DOWN);
1922         if (c) {
1923                 /* Choose a format we can live with */
1924                 if (c->format & format)
1925                         c->format &= format;
1926                 else 
1927                         c->format = ast_translator_best_choice(format, c->format);
1928         }
1929         pthread_mutex_unlock(&iaxs_lock);
1930         return c;
1931 }
1932
1933 static void *network_thread(void *ignore)
1934 {
1935         /* Our job is simple: Send queued messages, retrying if necessary.  Read frames 
1936            from the network, and queue them for delivery to the channels */
1937         int res;
1938         struct ast_iax_frame *f, *freeme;
1939         /* Establish I/O callback for socket read */
1940         ast_io_add(io, netsocket, socket_read, AST_IO_IN, NULL);
1941         pthread_mutex_lock(&iaxs_lock);
1942         for(;;) {
1943                 /* Go through the queue, sending messages which have not yet been
1944                    sent, and scheduling retransmissions if appropriate */
1945                 pthread_mutex_lock(&iaxq.lock);
1946                 f = iaxq.head;
1947                 while(f) {
1948                         freeme = NULL;
1949                         if (!f->sentyet) {
1950                                 f->sentyet++;
1951                                 /* Send a copy immediately */
1952                                 if (iaxs[f->callno]) {
1953                                         send_packet(f);
1954                                 } 
1955                                 if (f->retries < 0) {
1956                                         /* This is not supposed to be retransmitted */
1957                                         if (f->prev) 
1958                                                 f->prev->next = f->next;
1959                                         else
1960                                                 iaxq.head = f->next;
1961                                         if (f->next)
1962                                                 f->next->prev = f->prev;
1963                                         else
1964                                                 iaxq.tail = f->prev;
1965                                         iaxq.count--;
1966                                         /* Free the frame */
1967                                         ast_frfree(f->f);
1968                                         /* Free the iax frame */
1969                                         freeme = f;
1970                                 } else {
1971                                         /* We need reliable delivery.  Schedule a retransmission */
1972                                         f->retries++;
1973                                         ast_sched_add(sched, f->retrytime, attempt_transmit, f);
1974                                 }
1975                         }
1976                         f = f->next;
1977                         if (freeme)
1978                                 free(freeme);
1979                 }
1980                 pthread_mutex_unlock(&iaxq.lock);
1981                 pthread_mutex_unlock(&iaxs_lock);
1982                 res = ast_sched_wait(sched);
1983                 res = ast_io_wait(io, res);
1984                 pthread_mutex_lock(&iaxs_lock);
1985                 if (res >= 0) {
1986                         ast_sched_runq(sched);
1987                 }
1988         }
1989 }
1990
1991 static int start_network_thread()
1992 {
1993         return pthread_create(&netthreadid, NULL, network_thread, NULL);
1994 }
1995
1996 static struct iax_context *build_context(char *context)
1997 {
1998         struct iax_context *con = malloc(sizeof(struct iax_context));
1999         if (con) {
2000                 strncpy(con->context, context, sizeof(con->context));
2001                 con->next = NULL;
2002         }
2003         return con;
2004 }
2005
2006 static struct iax_ha *build_ha(char *sense, char *stuff)
2007 {
2008         struct iax_ha *ha = malloc(sizeof(struct iax_ha));
2009         char *nm;
2010         if (ha) {
2011                 strtok(stuff, "/");
2012                 nm = strtok(NULL, "/");
2013                 if (!nm)
2014                         nm = "255.255.255.255";
2015                 if (!inet_aton(stuff, &ha->netaddr)) {
2016                         ast_log(LOG_WARNING, "%s not a valid IP\n", stuff);
2017                         free(ha);
2018                         return NULL;
2019                 }
2020                 if (!inet_aton(nm, &ha->netmask)) {
2021                         ast_log(LOG_WARNING, "%s not a valid netmask\n", nm);
2022                         free(ha);
2023                         return NULL;
2024                 }
2025                 ha->netaddr.s_addr &= ha->netmask.s_addr;
2026                 if (!strcasecmp(sense, "a")) {
2027                         ha->sense = IAX_SENSE_ALLOW;
2028                 } else {
2029                         ha->sense = IAX_SENSE_DENY;
2030                 }
2031                 ha->next = NULL;
2032         }
2033         return ha;
2034 }
2035
2036 static struct iax_peer *build_peer(char *name, struct ast_variable *v)
2037 {
2038         struct iax_peer *peer;
2039         int maskfound=0;
2040         struct hostent *hp;
2041         peer = malloc(sizeof(struct iax_peer));
2042         if (peer) {
2043                 memset(peer, 0, sizeof(struct iax_peer));
2044                 strncpy(peer->name, name, sizeof(peer->name));
2045                 peer->addr.sin_port = htons(AST_DEFAULT_IAX_PORTNO);
2046                 while(v) {
2047                         if (!strcasecmp(v->name, "secret")) 
2048                                 strncpy(peer->secret, v->value, sizeof(peer->secret));
2049                         else if (!strcasecmp(v->name, "host")) {
2050                                 if (!strcasecmp(v->value, "dynamic")) {
2051                                         /* They'll register with us */
2052                                         peer->dynamic = 1;
2053                                         memset(&peer->addr.sin_addr, 0, 4);
2054                                 } else {
2055                                         peer->dynamic = 0;
2056                                         hp = gethostbyname(v->value);
2057                                         if (hp) {
2058                                                 memcpy(&peer->addr.sin_addr, hp->h_addr, sizeof(peer->addr.sin_addr));
2059                                         } else {
2060                                                 ast_log(LOG_WARNING, "Unable to lookup '%s'\n", v->value);
2061                                                 free(peer);
2062                                                 return NULL;
2063                                         }
2064                                         if (!maskfound)
2065                                                 inet_aton("255.255.255.255", &peer->mask);
2066                                 }
2067                         }
2068                         else if (!strcasecmp(v->name, "mask")) {
2069                                 maskfound++;
2070                                 inet_aton(v->value, &peer->mask);
2071                         } else if (!strcasecmp(v->name, "port"))
2072                                 peer->addr.sin_port = htons(atoi(v->value));
2073                         else if (!strcasecmp(v->name, "username"))
2074                                 strncpy(peer->username, v->value, sizeof(peer->username));
2075                         v=v->next;
2076                 }
2077         }
2078         return peer;
2079 }
2080
2081 static struct iax_user *build_user(char *name, struct ast_variable *v)
2082 {
2083         struct iax_user *user;
2084         struct iax_context *con, *conl = NULL;
2085         struct iax_ha *ha, *hal = NULL;
2086         user = (struct iax_user *)malloc(sizeof(struct iax_user));
2087         if (user) {
2088                 memset(user, 0, sizeof(struct iax_user));
2089                 strncpy(user->name, name, sizeof(user->name));
2090                 while(v) {
2091                         if (!strcasecmp(v->name, "context")) {
2092                                 con = build_context(v->value);
2093                                 if (con) {
2094                                         if (conl)
2095                                                 conl->next = con;
2096                                         else
2097                                                 user->contexts = con;
2098                                         conl = con;
2099                                 }
2100                         } else if (!strcasecmp(v->name, "allow") ||
2101                                            !strcasecmp(v->name, "deny")) {
2102                                 ha = build_ha(v->name, v->value);
2103                                 if (ha) {
2104                                         if (hal)
2105                                                 hal->next = ha;
2106                                         else
2107                                                 user->ha = ha;
2108                                         hal = ha;
2109                                 }
2110                         } else if (!strcasecmp(v->name, "auth")) {
2111                                 strncpy(user->methods, v->value, sizeof(user->methods));
2112                         } else if (!strcasecmp(v->name, "secret")) {
2113                                 strncpy(user->secret, v->value, sizeof(user->secret));
2114                         }
2115                         v = v->next;
2116                 }
2117         }
2118         return user;
2119 }
2120
2121 int load_module()
2122 {
2123         int res = 0;
2124         struct ast_config *cfg;
2125         struct ast_variable *v;
2126         struct iax_user *user;
2127         struct iax_peer *peer;
2128         char *cat;
2129         char *utype;
2130         int format;
2131         
2132         struct sockaddr_in sin;
2133         
2134         sin.sin_family = AF_INET;
2135         sin.sin_port = ntohs(AST_DEFAULT_IAX_PORTNO);
2136         sin.sin_addr.s_addr = INADDR_ANY;
2137         
2138         io = io_context_create();
2139         sched = sched_context_create();
2140         
2141         if (!io || !sched) {
2142                 ast_log(LOG_ERROR, "Out of memory\n");
2143                 return -1;
2144         }
2145
2146         pthread_mutex_init(&iaxq.lock, NULL);
2147         pthread_mutex_init(&userl.lock, NULL);
2148
2149         ast_cli_register(&cli_show_users);
2150         ast_cli_register(&cli_show_channels);
2151         ast_cli_register(&cli_show_peers);
2152         ast_cli_register(&cli_set_jitter);
2153 #ifdef IAX_SIMULATOR
2154         ast_cli_register(&delay_cli);
2155         ast_cli_register(&deviation_cli);
2156         ast_cli_register(&reliability_cli);
2157         ast_cli_register(&sim_show_cli);
2158 #endif
2159         cfg = ast_load(config);
2160         
2161         if (!cfg) {
2162                 ast_log(LOG_ERROR, "Unable to load config %s\n", config);
2163                 return -1;
2164         }
2165         v = ast_variable_browse(cfg, "general");
2166         while(v) {
2167                 if (!strcasecmp(v->name, "port")) 
2168                         sin.sin_port = ntohs(atoi(v->value));
2169                 else if (!strcasecmp(v->name, "pingtime")) 
2170                         ping_time = atoi(v->value);
2171                 else if (!strcasecmp(v->name, "maxjitterbuffer")) 
2172                         maxjitterbuffer = atoi(v->value);
2173                 else if (!strcasecmp(v->name, "maxexcessbuffer")) 
2174                         max_jitter_buffer = atoi(v->value);
2175                 else if (!strcasecmp(v->name, "lagrqtime")) 
2176                         lagrq_time = atoi(v->value);
2177                 else if (!strcasecmp(v->name, "dropcount")) 
2178                         iax_dropcount = atoi(v->value);
2179                 else if (!strcasecmp(v->name, "bindaddr"))
2180                         inet_aton(v->value, &sin.sin_addr);
2181                 else if (!strcasecmp(v->name, "jitterbuffer"))
2182                         use_jitterbuffer = ast_true(v->value);
2183                 else if (!strcasecmp(v->name, "bandwidth")) {
2184                         if (!strcasecmp(v->value, "low")) {
2185                                 iax_capability = IAX_CAPABILITY_LOWBANDWIDTH;
2186                         } else if (!strcasecmp(v->value, "medium")) {
2187                                 iax_capability = IAX_CAPABILITY_MEDBANDWIDTH;
2188                         } else if (!strcasecmp(v->value, "high")) {
2189                                 iax_capability = IAX_CAPABILITY_FULLBANDWIDTH;
2190                         } else
2191                                 ast_log(LOG_WARNING, "bandwidth must be either low, medium, or high\n");
2192                 } else if (!strcasecmp(v->name, "allow")) {
2193                         format = ast_getformatbyname(v->value);
2194                         if (format < 1) 
2195                                 ast_log(LOG_WARNING, "Cannot allow unknown format '%s'\n", v->value);
2196                         else
2197                                 iax_capability |= format;
2198                 } else if (!strcasecmp(v->name, "disallow")) {
2199                         format = ast_getformatbyname(v->value);
2200                         if (format < 1) 
2201                                 ast_log(LOG_WARNING, "Cannot disallow unknown format '%s'\n", v->value);
2202                         else
2203                                 iax_capability &= ~format;
2204                 }
2205                 v = v->next;
2206         }
2207         cat = ast_category_browse(cfg, NULL);
2208         while(cat) {
2209                 if (strcasecmp(cat, "general")) {
2210                         utype = ast_variable_retrieve(cfg, cat, "type");
2211                         if (utype) {
2212                                 if (!strcasecmp(utype, "user")) {
2213                                         user = build_user(cat, ast_variable_browse(cfg, cat));
2214                                         if (user) {
2215                                                 pthread_mutex_lock(&userl.lock);
2216                                                 user->next = userl.users;
2217                                                 userl.users = user;
2218                                                 pthread_mutex_unlock(&userl.lock);
2219                                         }
2220                                 } else if (!strcasecmp(utype, "peer")) {
2221                                         peer = build_peer(cat, ast_variable_browse(cfg, cat));
2222                                         if (peer) {
2223                                                 pthread_mutex_lock(&peerl.lock);
2224                                                 peer->next = peerl.peers;
2225                                                 peerl.peers = peer;
2226                                                 pthread_mutex_unlock(&peerl.lock);
2227                                         }
2228                                 } else {
2229                                         ast_log(LOG_WARNING, "Unknown type '%s' for '%s' in %s\n", utype, cat, config);
2230                                 }
2231                         } else
2232                                 ast_log(LOG_WARNING, "Section '%s' lacks type\n", cat);
2233                 }
2234                 cat = ast_category_browse(cfg, cat);
2235         }
2236         ast_destroy(cfg);
2237         if (ast_channel_register(type, tdesc, iax_capability, iax_request)) {
2238                 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
2239                 unload_module();
2240                 return -1;
2241         }
2242         
2243         /* Make a UDP socket */
2244         netsocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
2245                 
2246         if (netsocket < 0) {
2247                 ast_log(LOG_ERROR, "Unable to create network socket: %s\n", strerror(errno));
2248                 return -1;
2249         }
2250         if (bind(netsocket, &sin, sizeof(sin))) {
2251                 ast_log(LOG_ERROR, "Unable to bind to %s port %d\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
2252                 return -1;
2253         }
2254
2255         if (!res) {
2256                 res = start_network_thread();
2257                 if (option_verbose > 1) 
2258                         ast_verbose(VERBOSE_PREFIX_2 "IAX Ready and Listening on %s port %d\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
2259         } else {
2260                 ast_log(LOG_ERROR, "Unable to start network thread\n");
2261                 close(netsocket);
2262         }
2263         return res;
2264 }
2265
2266 char *description()
2267 {
2268         return desc;
2269 }
2270
2271 int unload_module()
2272 {
2273         struct iax_user *user, *userlast;
2274         struct iax_peer *peer, *peerlast;
2275         int x;
2276         /* Cancel the network thread, close the net socket */
2277         pthread_cancel(netthreadid);
2278         pthread_join(netthreadid, NULL);
2279         close(netsocket);
2280         for (x=0;x<AST_IAX_MAX_CALLS;x++)
2281                 if (iaxs[x])
2282                         iax_destroy(x);
2283         ast_cli_unregister(&cli_show_users);
2284         ast_cli_unregister(&cli_show_channels);
2285         ast_cli_unregister(&cli_show_peers);
2286         ast_cli_unregister(&cli_set_jitter);
2287 #ifdef IAX_SIMULATOR
2288         ast_cli_unregister(&delay_cli);
2289         ast_cli_unregister(&deviation_cli);
2290         ast_cli_unregister(&reliability_cli);
2291         ast_cli_unregister(&sim_show_cli);
2292 #endif
2293         for (user=userl.users;user;) {
2294                 free_ha(user->ha);
2295                 free_context(user->contexts);
2296                 userlast = user;
2297                 user=user->next;
2298                 free(userlast);
2299         }
2300         for (peer=peerl.peers;peer;) {
2301                 peerlast = peer;
2302                 peer=peer->next;
2303                 free(peerlast);
2304         }
2305         return 0;
2306 }
2307
2308 int usecount()
2309 {
2310         int res;
2311         pthread_mutex_lock(&usecnt_lock);
2312         res = usecnt;
2313         pthread_mutex_unlock(&usecnt_lock);
2314         return res;
2315 }
2316