Changes to add udptl to asterisk (preliminary merging of the t.38 patch)
[asterisk/asterisk.git] / udptl.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * UDPTL support for T.38
5  * 
6  * Copyright (C) 2005, Steve Underwood, partly based on RTP code which is
7  * Copyright (C) 1999-2004, Digium, Inc.
8  *
9  * Steve Underwood <steveu@coppice.org>
10  *
11  * This program is free software, distributed under the terms of
12  * the GNU General Public License
13  *
14  * This version is disclaimed to DIGIUM for inclusion in the Asterisk project.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <sys/time.h>
21 #include <signal.h>
22 #include <errno.h>
23 #include <unistd.h>
24 #include <stdint.h>
25 #include <netinet/in.h>
26 #include <sys/time.h>
27 #include <sys/socket.h>
28 #include <arpa/inet.h>
29 #include <fcntl.h>
30
31 #include "asterisk.h"
32
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34
35 #include "asterisk/udptl.h"
36 #include "asterisk/frame.h"
37 #include "asterisk/logger.h"
38 #include "asterisk/options.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/acl.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/config.h"
43 #include "asterisk/lock.h"
44 #include "asterisk/utils.h"
45 #include "asterisk/cli.h"
46 #include "asterisk/unaligned.h"
47 #include "asterisk/utils.h"
48
49 #define UDPTL_MTU               1200
50
51 #if !defined(FALSE)
52 #define FALSE 0
53 #endif
54 #if !defined(TRUE)
55 #define TRUE (!FALSE)
56 #endif
57
58 static int udptlstart = 0;
59 static int udptlend = 0;
60 static int udptldebug = 0;                      /* Are we debugging? */
61 static struct sockaddr_in udptldebugaddr;       /* Debug packets to/from this host */
62 #ifdef SO_NO_CHECK
63 static int nochecksums = 0;
64 #endif
65 static int udptlfectype = 0;
66 static int udptlfecentries = 0;
67 static int udptlfecspan = 0;
68 static int udptlmaxdatagram = 0;
69
70 #define LOCAL_FAX_MAX_DATAGRAM      400
71 #define MAX_FEC_ENTRIES             5
72 #define MAX_FEC_SPAN                5
73
74 #define UDPTL_BUF_MASK              15
75
76 typedef struct {
77         int buf_len;
78         uint8_t buf[LOCAL_FAX_MAX_DATAGRAM];
79 } udptl_fec_tx_buffer_t;
80
81 typedef struct {
82         int buf_len;
83         uint8_t buf[LOCAL_FAX_MAX_DATAGRAM];
84         int fec_len[MAX_FEC_ENTRIES];
85         uint8_t fec[MAX_FEC_ENTRIES][LOCAL_FAX_MAX_DATAGRAM];
86         int fec_span;
87         int fec_entries;
88 } udptl_fec_rx_buffer_t;
89
90 struct ast_udptl {
91         int fd;
92         char resp;
93         struct ast_frame f[16];
94         unsigned char rawdata[8192 + AST_FRIENDLY_OFFSET];
95         unsigned int lasteventseqn;
96         int nat;
97         int flags;
98         struct sockaddr_in us;
99         struct sockaddr_in them;
100         int *ioid;
101         uint16_t seqno;
102         struct sched_context *sched;
103         struct io_context *io;
104         void *data;
105         ast_udptl_callback callback;
106         int udptl_offered_from_local;
107
108         /*! This option indicates the error correction scheme used in transmitted UDPTL
109             packets. */
110         int error_correction_scheme;
111
112         /*! This option indicates the number of error correction entries transmitted in
113             UDPTL packets. */
114         int error_correction_entries;
115
116         /*! This option indicates the span of the error correction entries in transmitted
117             UDPTL packets (FEC only). */
118         int error_correction_span;
119
120         /*! This option indicates the maximum size of a UDPTL packet that can be accepted by
121             the remote device. */
122         int far_max_datagram_size;
123
124         /*! This option indicates the maximum size of a UDPTL packet that we are prepared to
125             accept. */
126         int local_max_datagram_size;
127
128         int verbose;
129
130         struct sockaddr_in far;
131
132         int tx_seq_no;
133         int rx_seq_no;
134         int rx_expected_seq_no;
135
136         udptl_fec_tx_buffer_t tx[UDPTL_BUF_MASK + 1];
137         udptl_fec_rx_buffer_t rx[UDPTL_BUF_MASK + 1];
138 };
139
140 static struct ast_udptl_protocol *protos = NULL;
141
142 static int udptl_rx_packet(struct ast_udptl *s, uint8_t *buf, int len);
143 static int udptl_build_packet(struct ast_udptl *s, uint8_t *buf, uint8_t *ifp, int ifp_len);
144
145 static inline int udptl_debug_test_addr(struct sockaddr_in *addr)
146 {
147         if (udptldebug == 0)
148                 return 0;
149         if (udptldebugaddr.sin_addr.s_addr) {
150                 if (((ntohs(udptldebugaddr.sin_port) != 0)
151                         && (udptldebugaddr.sin_port != addr->sin_port))
152                         || (udptldebugaddr.sin_addr.s_addr != addr->sin_addr.s_addr))
153                 return 0;
154         }
155         return 1;
156 }
157
158 static int decode_length(uint8_t *buf, int limit, int *len, int *pvalue)
159 {
160         if ((buf[*len] & 0x80) == 0) {
161                 if (*len >= limit)
162                         return -1;
163                 *pvalue = buf[*len];
164                 (*len)++;
165                 return 0;
166         }
167         if ((buf[*len] & 0x40) == 0) {
168                 if (*len >= limit - 1)
169                         return -1;
170                 *pvalue = (buf[*len] & 0x3F) << 8;
171                 (*len)++;
172                 *pvalue |= buf[*len];
173                 (*len)++;
174                 return 0;
175         }
176         if (*len >= limit)
177                 return -1;
178         *pvalue = (buf[*len] & 0x3F) << 14;
179         (*len)++;
180         /* Indicate we have a fragment */
181         return 1;
182 }
183 /*- End of function --------------------------------------------------------*/
184
185 static int decode_open_type(uint8_t *buf, int limit, int *len, const uint8_t **p_object, int *p_num_octets)
186 {
187         int octet_cnt;
188         int octet_idx;
189         int stat;
190         int i;
191         const uint8_t **pbuf;
192
193         for (octet_idx = 0, *p_num_octets = 0;  ;  octet_idx += octet_cnt) {
194                 if ((stat = decode_length(buf, limit, len, &octet_cnt)) < 0)
195                         return -1;
196                 if (octet_cnt > 0) {
197                         *p_num_octets += octet_cnt;
198
199                         pbuf = &p_object[octet_idx];
200                         i = 0;
201                         /* Make sure the buffer contains at least the number of bits requested */
202                         if ((*len + octet_cnt) > limit)
203                                 return -1;
204
205                         *pbuf = &buf[*len];
206                         *len += octet_cnt;
207                 }
208                 if (stat == 0)
209                         break;
210         }
211         return 0;
212 }
213 /*- End of function --------------------------------------------------------*/
214
215 static int encode_length(uint8_t *buf, int *len, int value)
216 {
217         int multiplier;
218
219         if (value < 0x80) {
220                 /* 1 octet */
221                 buf[*len] = value;
222                 (*len)++;
223                 return value;
224         }
225         if (value < 0x4000) {
226                 /* 2 octets */
227                 /* Set the first bit of the first octet */
228                 buf[*len] = ((0x8000 | value) >> 8) & 0xFF;
229                 (*len)++;
230                 buf[*len] = value & 0xFF;
231                 (*len)++;
232                 return value;
233         }
234         /* Fragmentation */
235         multiplier = (value < 0x10000)  ?  (value >> 14)  :  4;
236         /* Set the first 2 bits of the octet */
237         buf[*len] = 0xC0 | multiplier;
238         (*len)++;
239         return multiplier << 14;
240 }
241 /*- End of function --------------------------------------------------------*/
242
243 static int encode_open_type(uint8_t *buf, int *len, const uint8_t *data, int num_octets)
244 {
245         int enclen;
246         int octet_idx;
247         uint8_t zero_byte;
248
249         /* If open type is of zero length, add a single zero byte (10.1) */
250         if (num_octets == 0) {
251                 zero_byte = 0;
252                 data = &zero_byte;
253                 num_octets = 1;
254         }
255         /* Encode the open type */
256         for (octet_idx = 0;  ;  num_octets -= enclen, octet_idx += enclen) {
257                 if ((enclen = encode_length(buf, len, num_octets)) < 0)
258                         return -1;
259                 if (enclen > 0) {
260                         memcpy(&buf[*len], &data[octet_idx], enclen);
261                         *len += enclen;
262                 }
263                 if (enclen >= num_octets)
264                         break;
265         }
266
267         return 0;
268 }
269 /*- End of function --------------------------------------------------------*/
270
271 static int udptl_rx_packet(struct ast_udptl *s, uint8_t *buf, int len)
272 {
273         int stat;
274         int stat2;
275         int i;
276         int j;
277         int k;
278         int l;
279         int m;
280         int x;
281         int limit;
282         int which;
283         int ptr;
284         int count;
285         int total_count;
286         int seq_no;
287         const uint8_t *ifp;
288         const uint8_t *data;
289         int ifp_len;
290         int repaired[16];
291         const uint8_t *bufs[16];
292         int lengths[16];
293         int span;
294         int entries;
295         int ifp_no;
296
297         ptr = 0;
298         ifp_no = 0;
299         s->f[0].prev = NULL;
300         s->f[0].next = NULL;
301
302         /* Decode seq_number */
303         if (ptr + 2 > len)
304                 return -1;
305         seq_no = (buf[0] << 8) | buf[1];
306         ptr += 2;
307
308         /* Break out the primary packet */
309         if ((stat = decode_open_type(buf, len, &ptr, &ifp, &ifp_len)) != 0)
310                 return -1;
311         /* Decode error_recovery */
312         if (ptr + 1 > len)
313                 return -1;
314         if ((buf[ptr++] & 0x80) == 0) {
315                 /* Secondary packet mode for error recovery */
316                 if (seq_no > s->rx_seq_no) {
317                         /* We received a later packet than we expected, so we need to check if we can fill in the gap from the
318                            secondary packets. */
319                         total_count = 0;
320                         do {
321                                 if ((stat2 = decode_length(buf, len, &ptr, &count)) < 0)
322                                         return -1;
323                                 for (i = 0;  i < count;  i++) {
324                                         if ((stat = decode_open_type(buf, len, &ptr, &bufs[total_count + i], &lengths[total_count + i])) != 0)
325                                                 return -1;
326                                 }
327                                 total_count += count;
328                         }
329                         while (stat2 > 0);
330                         /* Step through in reverse order, so we go oldest to newest */
331                         for (i = total_count;  i > 0;  i--) {
332                                 if (seq_no - i >= s->rx_seq_no) {
333                                         /* This one wasn't seen before */
334                                         /* Decode the secondary IFP packet */
335                                         //fprintf(stderr, "Secondary %d, len %d\n", seq_no - i, lengths[i - 1]);
336                                         s->f[ifp_no].frametype = AST_FRAME_MODEM;
337                                         s->f[ifp_no].subclass = AST_MODEM_T38;
338
339                                         s->f[ifp_no].mallocd = 0;
340                                         //s->f[ifp_no].???seq_no = seq_no - i;
341                                         s->f[ifp_no].datalen = lengths[i - 1];
342                                         s->f[ifp_no].data = (uint8_t *) bufs[i - 1];
343                                         s->f[ifp_no].offset = 0;
344                                         s->f[ifp_no].src = "UDPTL";
345                                         if (ifp_no > 0) {
346                                                 s->f[ifp_no].prev = &s->f[ifp_no - 1];
347                                                 s->f[ifp_no - 1].next = &s->f[ifp_no];
348                                         }
349                                         s->f[ifp_no].next = NULL;
350                                         ifp_no++;
351                                 }
352                         }
353                 }
354                 /* If packets are received out of sequence, we may have already processed this packet from the error
355                    recovery information in a packet already received. */
356                 if (seq_no >= s->rx_seq_no) {
357                         /* Decode the primary IFP packet */
358                         s->f[ifp_no].frametype = AST_FRAME_MODEM;
359                         s->f[ifp_no].subclass = AST_MODEM_T38;
360                         
361                         s->f[ifp_no].mallocd = 0;
362                         //s->f[ifp_no].???seq_no = seq_no;
363                         s->f[ifp_no].datalen = ifp_len;
364                         s->f[ifp_no].data = (uint8_t *) ifp;
365                         s->f[ifp_no].offset = 0;
366                         s->f[ifp_no].src = "UDPTL";
367                         if (ifp_no > 0) {
368                                 s->f[ifp_no].prev = &s->f[ifp_no - 1];
369                                 s->f[ifp_no - 1].next = &s->f[ifp_no];
370                         }
371                         s->f[ifp_no].next = NULL;
372                 }
373         }
374         else
375         {
376                 /* FEC mode for error recovery */
377                 /* Our buffers cannot tolerate overlength IFP packets in FEC mode */
378                 if (ifp_len > LOCAL_FAX_MAX_DATAGRAM)
379                         return -1;
380                 /* Update any missed slots in the buffer */
381                 for (  ;  seq_no > s->rx_seq_no;  s->rx_seq_no++) {
382                         x = s->rx_seq_no & UDPTL_BUF_MASK;
383                         s->rx[x].buf_len = -1;
384                         s->rx[x].fec_len[0] = 0;
385                         s->rx[x].fec_span = 0;
386                         s->rx[x].fec_entries = 0;
387                 }
388
389                 x = seq_no & UDPTL_BUF_MASK;
390
391                 memset(repaired, 0, sizeof(repaired));
392
393                 /* Save the new IFP packet */
394                 memcpy(s->rx[x].buf, ifp, ifp_len);
395                 s->rx[x].buf_len = ifp_len;
396                 repaired[x] = TRUE;
397
398                 /* Decode the FEC packets */
399                 /* The span is defined as an unconstrained integer, but will never be more
400                    than a small value. */
401                 if (ptr + 2 > len)
402                         return -1;
403                 if (buf[ptr++] != 1)
404                         return -1;
405                 span = buf[ptr++];
406                 s->rx[x].fec_span = span;
407
408                 /* The number of entries is defined as a length, but will only ever be a small
409                    value. Treat it as such. */
410                 if (ptr + 1 > len)
411                         return -1;
412                 entries = buf[ptr++];
413                 s->rx[x].fec_entries = entries;
414
415                 /* Decode the elements */
416                 for (i = 0;  i < entries;  i++) {
417                         if ((stat = decode_open_type(buf, len, &ptr, &data, &s->rx[x].fec_len[i])) != 0)
418                                 return -1;
419                         if (s->rx[x].fec_len[i] > LOCAL_FAX_MAX_DATAGRAM)
420                                 return -1;
421
422                         /* Save the new FEC data */
423                         memcpy(s->rx[x].fec[i], data, s->rx[x].fec_len[i]);
424 #if 0
425                         fprintf(stderr, "FEC: ");
426                         for (j = 0;  j < s->rx[x].fec_len[i];  j++)
427                                 fprintf(stderr, "%02X ", data[j]);
428                         fprintf(stderr, "\n");
429 #endif
430            }
431
432                 /* See if we can reconstruct anything which is missing */
433                 /* TODO: this does not comprehensively hunt back and repair everything that is possible */
434                 for (l = x;  l != ((x - (16 - span*entries)) & UDPTL_BUF_MASK);  l = (l - 1) & UDPTL_BUF_MASK) {
435                         if (s->rx[l].fec_len[0] <= 0)
436                                 continue;
437                         for (m = 0;  m < s->rx[l].fec_entries;  m++) {
438                                 limit = (l + m) & UDPTL_BUF_MASK;
439                                 for (which = -1, k = (limit - s->rx[l].fec_span*s->rx[l].fec_entries) & UDPTL_BUF_MASK;  k != limit;  k = (k + s->rx[l].fec_entries) & UDPTL_BUF_MASK) {
440                                         if (s->rx[k].buf_len <= 0)
441                                                 which = (which == -1)  ?  k  :  -2;
442                                 }
443                                 if (which >= 0) {
444                                         /* Repairable */
445                                         for (j = 0;  j < s->rx[l].fec_len[m];  j++) {
446                                                 s->rx[which].buf[j] = s->rx[l].fec[m][j];
447                                                 for (k = (limit - s->rx[l].fec_span*s->rx[l].fec_entries) & UDPTL_BUF_MASK;  k != limit;  k = (k + s->rx[l].fec_entries) & UDPTL_BUF_MASK)
448                                                         s->rx[which].buf[j] ^= (s->rx[k].buf_len > j)  ?  s->rx[k].buf[j]  :  0;
449                                         }
450                                         s->rx[which].buf_len = s->rx[l].fec_len[m];
451                                         repaired[which] = TRUE;
452                                 }
453                         }
454                 }
455                 /* Now play any new packets forwards in time */
456                 for (l = (x + 1) & UDPTL_BUF_MASK, j = seq_no - UDPTL_BUF_MASK;  l != x;  l = (l + 1) & UDPTL_BUF_MASK, j++) {
457                         if (repaired[l]) {
458                                 //fprintf(stderr, "Fixed packet %d, len %d\n", j, l);
459                                 s->f[ifp_no].frametype = AST_FRAME_MODEM;
460                                 s->f[ifp_no].subclass = AST_MODEM_T38;
461                         
462                                 s->f[ifp_no].mallocd = 0;
463                                 //s->f[ifp_no].???seq_no = j;
464                                 s->f[ifp_no].datalen = s->rx[l].buf_len;
465                                 s->f[ifp_no].data = s->rx[l].buf;
466                                 s->f[ifp_no].offset = 0;
467                                 s->f[ifp_no].src = "UDPTL";
468                                 if (ifp_no > 0) {
469                                         s->f[ifp_no].prev = &s->f[ifp_no - 1];
470                                         s->f[ifp_no - 1].next = &s->f[ifp_no];
471                                 }
472                                 s->f[ifp_no].next = NULL;
473                                 ifp_no++;
474                         }
475                 }
476                 /* Decode the primary IFP packet */
477                 s->f[ifp_no].frametype = AST_FRAME_MODEM;
478                 s->f[ifp_no].subclass = AST_MODEM_T38;
479                         
480                 s->f[ifp_no].mallocd = 0;
481                 //s->f[ifp_no].???seq_no = j;
482                 s->f[ifp_no].datalen = ifp_len;
483                 s->f[ifp_no].data = (uint8_t *) ifp;
484                 s->f[ifp_no].offset = 0;
485                 s->f[ifp_no].src = "UDPTL";
486                 if (ifp_no > 0) {
487                         s->f[ifp_no].prev = &s->f[ifp_no - 1];
488                         s->f[ifp_no - 1].next = &s->f[ifp_no];
489                 }
490                 s->f[ifp_no].next = NULL;
491         }
492
493         s->rx_seq_no = seq_no + 1;
494         return 0;
495 }
496 /*- End of function --------------------------------------------------------*/
497
498 static int udptl_build_packet(struct ast_udptl *s, uint8_t *buf, uint8_t *ifp, int ifp_len)
499 {
500         uint8_t fec[LOCAL_FAX_MAX_DATAGRAM];
501         int i;
502         int j;
503         int seq;
504         int entry;
505         int entries;
506         int span;
507         int m;
508         int len;
509         int limit;
510         int high_tide;
511
512         seq = s->tx_seq_no & 0xFFFF;
513
514         /* Map the sequence number to an entry in the circular buffer */
515         entry = seq & UDPTL_BUF_MASK;
516
517         /* We save the message in a circular buffer, for generating FEC or
518            redundancy sets later on. */
519         s->tx[entry].buf_len = ifp_len;
520         memcpy(s->tx[entry].buf, ifp, ifp_len);
521         
522         /* Build the UDPTLPacket */
523
524         len = 0;
525         /* Encode the sequence number */
526         buf[len++] = (seq >> 8) & 0xFF;
527         buf[len++] = seq & 0xFF;
528
529         /* Encode the primary IFP packet */
530         if (encode_open_type(buf, &len, ifp, ifp_len) < 0)
531                 return -1;
532
533         /* Encode the appropriate type of error recovery information */
534         switch (s->error_correction_scheme)
535         {
536         case UDPTL_ERROR_CORRECTION_NONE:
537                 /* Encode the error recovery type */
538                 buf[len++] = 0x00;
539                 /* The number of entries will always be zero, so it is pointless allowing
540                    for the fragmented case here. */
541                 if (encode_length(buf, &len, 0) < 0)
542                         return -1;
543                 break;
544         case UDPTL_ERROR_CORRECTION_REDUNDANCY:
545                 /* Encode the error recovery type */
546                 buf[len++] = 0x00;
547                 if (s->tx_seq_no > s->error_correction_entries)
548                         entries = s->error_correction_entries;
549                 else
550                         entries = s->tx_seq_no;
551                 /* The number of entries will always be small, so it is pointless allowing
552                    for the fragmented case here. */
553                 if (encode_length(buf, &len, entries) < 0)
554                         return -1;
555                 /* Encode the elements */
556                 for (i = 0;  i < entries;  i++) {
557                         j = (entry - i - 1) & UDPTL_BUF_MASK;
558                         if (encode_open_type(buf, &len, s->tx[j].buf, s->tx[j].buf_len) < 0)
559                                 return -1;
560                 }
561                 break;
562         case UDPTL_ERROR_CORRECTION_FEC:
563                 span = s->error_correction_span;
564                 entries = s->error_correction_entries;
565                 if (seq < s->error_correction_span*s->error_correction_entries) {
566                         /* In the initial stages, wind up the FEC smoothly */
567                         entries = seq/s->error_correction_span;
568                         if (seq < s->error_correction_span)
569                                 span = 0;
570                 }
571                 /* Encode the error recovery type */
572                 buf[len++] = 0x80;
573                 /* Span is defined as an inconstrained integer, which it dumb. It will only
574                    ever be a small value. Treat it as such. */
575                 buf[len++] = 1;
576                 buf[len++] = span;
577                 /* The number of entries is defined as a length, but will only ever be a small
578                    value. Treat it as such. */
579                 buf[len++] = entries;
580                 for (m = 0;  m < entries;  m++) {
581                         /* Make an XOR'ed entry the maximum length */
582                         limit = (entry + m) & UDPTL_BUF_MASK;
583                         high_tide = 0;
584                         for (i = (limit - span*entries) & UDPTL_BUF_MASK;  i != limit;  i = (i + entries) & UDPTL_BUF_MASK) {
585                                 if (high_tide < s->tx[i].buf_len) {
586                                         for (j = 0;  j < high_tide;  j++)
587                                                 fec[j] ^= s->tx[i].buf[j];
588                                         for (  ;  j < s->tx[i].buf_len;  j++)
589                                                 fec[j] = s->tx[i].buf[j];
590                                         high_tide = s->tx[i].buf_len;
591                                 } else {
592                                         for (j = 0;  j < s->tx[i].buf_len;  j++)
593                                                 fec[j] ^= s->tx[i].buf[j];
594                                 }
595                         }
596                         if (encode_open_type(buf, &len, fec, high_tide) < 0)
597                                 return -1;
598                 }
599                 break;
600         }
601
602         if (s->verbose)
603                 fprintf(stderr, "\n");
604
605         s->tx_seq_no++;
606         return len;
607 }
608
609 int ast_udptl_fd(struct ast_udptl *udptl)
610 {
611         return udptl->fd;
612 }
613
614 void ast_udptl_set_data(struct ast_udptl *udptl, void *data)
615 {
616         udptl->data = data;
617 }
618
619 void ast_udptl_set_callback(struct ast_udptl *udptl, ast_udptl_callback callback)
620 {
621         udptl->callback = callback;
622 }
623
624 void ast_udptl_setnat(struct ast_udptl *udptl, int nat)
625 {
626         udptl->nat = nat;
627 }
628
629 static int udptlread(int *id, int fd, short events, void *cbdata)
630 {
631         struct ast_udptl *udptl = cbdata;
632         struct ast_frame *f;
633
634         if ((f = ast_udptl_read(udptl))) {
635                 if (udptl->callback)
636                         udptl->callback(udptl, f, udptl->data);
637         }
638         return 1;
639 }
640
641 struct ast_frame *ast_udptl_read(struct ast_udptl *udptl)
642 {
643         int res;
644         struct sockaddr_in sin;
645         socklen_t len;
646         uint16_t seqno = 0;
647         char iabuf[INET_ADDRSTRLEN];
648         uint16_t *udptlheader;
649         static struct ast_frame null_frame = { AST_FRAME_NULL, };
650
651         len = sizeof(sin);
652         
653         /* Cache where the header will go */
654         res = recvfrom(udptl->fd,
655                         udptl->rawdata + AST_FRIENDLY_OFFSET,
656                         sizeof(udptl->rawdata) - AST_FRIENDLY_OFFSET,
657                         0,
658                         (struct sockaddr *) &sin,
659                         &len);
660         udptlheader = (uint16_t *)(udptl->rawdata + AST_FRIENDLY_OFFSET);
661         if (res < 0) {
662                 if (errno != EAGAIN)
663                         ast_log(LOG_WARNING, "UDPTL read error: %s\n", strerror(errno));
664                 if (errno == EBADF)
665                         CRASH;
666                 return &null_frame;
667         }
668
669         /* Ignore if the other side hasn't been given an address yet. */
670         if (!udptl->them.sin_addr.s_addr || !udptl->them.sin_port)
671                 return &null_frame;
672
673         if (udptl->nat) {
674                 /* Send to whoever sent to us */
675                 if ((udptl->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
676                         (udptl->them.sin_port != sin.sin_port)) {
677                         memcpy(&udptl->them, &sin, sizeof(udptl->them));
678                         ast_log(LOG_DEBUG, "UDPTL NAT: Using address %s:%d\n", ast_inet_ntoa(iabuf, sizeof(iabuf), udptl->them.sin_addr), ntohs(udptl->them.sin_port));
679                 }
680         }
681
682         if (udptl_debug_test_addr(&sin)) {
683                 ast_verbose("Got UDPTL packet from %s:%d (type %d, seq %d, len %d)\n",
684                         ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr), ntohs(sin.sin_port), 0, seqno, res);
685         }
686 #if 0
687         printf("Got UDPTL packet from %s:%d (seq %d, len = %d)\n", ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr), ntohs(sin.sin_port), seqno, res);
688 #endif
689         udptl_rx_packet(udptl, udptl->rawdata + AST_FRIENDLY_OFFSET, res);
690
691         return &udptl->f[0];
692 }
693
694 void ast_udptl_offered_from_local(struct ast_udptl* udptl, int local)
695 {
696         if (udptl)
697                 udptl->udptl_offered_from_local = local;
698         else
699                 ast_log(LOG_WARNING, "udptl structure is null\n");
700 }
701
702 int ast_udptl_get_error_correction_scheme(struct ast_udptl* udptl)
703 {
704     if (udptl)
705             return udptl->error_correction_scheme;
706     else {
707             ast_log(LOG_WARNING, "udptl structure is null\n");
708             return -1;
709     }
710 }
711
712 void ast_udptl_set_error_correction_scheme(struct ast_udptl* udptl, int ec)
713 {
714     if (udptl) {
715         switch (ec) {
716             case UDPTL_ERROR_CORRECTION_FEC:
717                 udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_FEC;
718                 break;
719             case UDPTL_ERROR_CORRECTION_REDUNDANCY:
720                 udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_REDUNDANCY;
721                 break;
722             case UDPTL_ERROR_CORRECTION_NONE:
723                 udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_NONE;
724                 break;
725             default:
726                 ast_log(LOG_WARNING, "error correction parameter invalid");
727         };
728     } else
729             ast_log(LOG_WARNING, "udptl structure is null\n");
730 }
731
732 int ast_udptl_get_local_max_datagram(struct ast_udptl* udptl)
733 {
734     if (udptl)
735             return udptl->local_max_datagram_size;
736     else {
737             ast_log(LOG_WARNING, "udptl structure is null\n");
738             return -1;
739     }
740 }
741
742 int ast_udptl_get_far_max_datagram(struct ast_udptl* udptl)
743 {
744     if (udptl)
745             return udptl->far_max_datagram_size;
746     else {
747             ast_log(LOG_WARNING, "udptl structure is null\n");
748             return -1;
749     }
750 }
751
752 void ast_udptl_set_local_max_datagram(struct ast_udptl* udptl, int max_datagram)
753 {
754     if (udptl)
755             udptl->local_max_datagram_size = max_datagram;
756     else
757             ast_log(LOG_WARNING, "udptl structure is null\n");
758 }
759
760 void ast_udptl_set_far_max_datagram(struct ast_udptl* udptl, int max_datagram)
761 {
762     if (udptl)
763             udptl->far_max_datagram_size = max_datagram;
764     else
765             ast_log(LOG_WARNING, "udptl structure is null\n");
766 }
767
768 struct ast_udptl *ast_udptl_new_with_bindaddr(struct sched_context *sched, struct io_context *io, int callbackmode, struct in_addr addr)
769 {
770         struct ast_udptl *udptl;
771         int x;
772         int startplace;
773         int i;
774         long int flags;
775
776         if ((udptl = malloc(sizeof(struct ast_udptl))) == NULL)
777                 return NULL;
778         memset(udptl, 0, sizeof(struct ast_udptl));
779
780         if (udptlfectype == 2)
781                 udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_FEC;
782         else if (udptlfectype == 1)
783                 udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_REDUNDANCY;
784         else
785                 udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_NONE;
786         udptl->error_correction_span = udptlfecspan;
787         udptl->error_correction_entries = udptlfecentries;
788         
789         udptl->far_max_datagram_size = udptlmaxdatagram;
790         udptl->local_max_datagram_size = udptlmaxdatagram;
791
792         memset(&udptl->rx, 0, sizeof(udptl->rx));
793         memset(&udptl->tx, 0, sizeof(udptl->tx));
794         for (i = 0;  i <= UDPTL_BUF_MASK;  i++) {
795                 udptl->rx[i].buf_len = -1;
796                 udptl->tx[i].buf_len = -1;
797         }
798
799         udptl->seqno = rand() & 0xffff;
800         udptl->them.sin_family = AF_INET;
801         udptl->us.sin_family = AF_INET;
802
803         if ((udptl->fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
804                 free(udptl);
805                 ast_log(LOG_WARNING, "Unable to allocate socket: %s\n", strerror(errno));
806                 return NULL;
807         }
808         flags = fcntl(udptl->fd, F_GETFL);
809         fcntl(udptl->fd, F_SETFL, flags | O_NONBLOCK);
810 #ifdef SO_NO_CHECK
811         if (nochecksums)
812                 setsockopt(udptl->fd, SOL_SOCKET, SO_NO_CHECK, &nochecksums, sizeof(nochecksums));
813 #endif
814         /* Find us a place */
815         x = (rand()%(udptlend - udptlstart)) + udptlstart;
816         startplace = x;
817         for (;;) {
818                 udptl->us.sin_port = htons(x);
819                 udptl->us.sin_addr = addr;
820                 if (bind(udptl->fd, (struct sockaddr *) &udptl->us, sizeof(udptl->us)) == 0)
821                         break;
822                 if (errno != EADDRINUSE) {
823                         ast_log(LOG_WARNING, "Unexpected bind error: %s\n", strerror(errno));
824                         close(udptl->fd);
825                         free(udptl);
826                         return NULL;
827                 }
828                 if (++x > udptlend)
829                         x = udptlstart;
830                 if (x == startplace) {
831                         ast_log(LOG_WARNING, "No UDPTL ports remaining\n");
832                         close(udptl->fd);
833                         free(udptl);
834                         return NULL;
835                 }
836         }
837         if (io && sched && callbackmode) {
838                 /* Operate this one in a callback mode */
839                 udptl->sched = sched;
840                 udptl->io = io;
841                 udptl->ioid = ast_io_add(udptl->io, udptl->fd, udptlread, AST_IO_IN, udptl);
842         }
843         return udptl;
844 }
845
846 struct ast_udptl *ast_udptl_new(struct sched_context *sched, struct io_context *io, int callbackmode)
847 {
848         struct in_addr ia;
849         memset(&ia, 0, sizeof(ia));
850         return ast_udptl_new_with_bindaddr(sched, io, callbackmode, ia);
851 }
852
853 int ast_udptl_settos(struct ast_udptl *udptl, int tos)
854 {
855         int res;
856
857         if ((res = setsockopt(udptl->fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)))) 
858                 ast_log(LOG_WARNING, "UDPTL unable to set TOS to %d\n", tos);
859         return res;
860 }
861
862 void ast_udptl_set_peer(struct ast_udptl *udptl, struct sockaddr_in *them)
863 {
864         udptl->them.sin_port = them->sin_port;
865         udptl->them.sin_addr = them->sin_addr;
866 }
867
868 void ast_udptl_get_peer(struct ast_udptl *udptl, struct sockaddr_in *them)
869 {
870         them->sin_family = AF_INET;
871         them->sin_port = udptl->them.sin_port;
872         them->sin_addr = udptl->them.sin_addr;
873 }
874
875 void ast_udptl_get_us(struct ast_udptl *udptl, struct sockaddr_in *us)
876 {
877         memcpy(us, &udptl->us, sizeof(udptl->us));
878 }
879
880 void ast_udptl_stop(struct ast_udptl *udptl)
881 {
882         memset(&udptl->them.sin_addr, 0, sizeof(udptl->them.sin_addr));
883         memset(&udptl->them.sin_port, 0, sizeof(udptl->them.sin_port));
884 }
885
886 void ast_udptl_destroy(struct ast_udptl *udptl)
887 {
888         if (udptl->ioid)
889                 ast_io_remove(udptl->io, udptl->ioid);
890         if (udptl->fd > -1)
891                 close(udptl->fd);
892         free(udptl);
893 }
894
895 int ast_udptl_write(struct ast_udptl *s, struct ast_frame *f)
896 {
897         int len;
898         int res;
899         uint8_t buf[LOCAL_FAX_MAX_DATAGRAM];
900         char iabuf[INET_ADDRSTRLEN];
901
902         /* If we have no peer, return immediately */    
903         if (s->them.sin_addr.s_addr == INADDR_ANY)
904                 return 0;
905
906         /* If there is no data length, return immediately */
907         if (f->datalen == 0)
908                 return 0;
909         
910         if (f->frametype != AST_FRAME_MODEM) {
911                 ast_log(LOG_WARNING, "UDPTL can only send T.38 data\n");
912                 return -1;
913         }
914
915         /* Cook up the UDPTL packet, with the relevant EC info. */
916         len = udptl_build_packet(s, buf, f->data, f->datalen);
917
918         if (len > 0  &&  s->them.sin_port && s->them.sin_addr.s_addr) {
919                 if ((res = sendto(s->fd, buf, len, 0, (struct sockaddr *) &s->them, sizeof(s->them))) < 0)
920                         ast_log(LOG_NOTICE, "UDPTL Transmission error to %s:%d: %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), s->them.sin_addr), ntohs(s->them.sin_port), strerror(errno));
921 #if 0
922                 printf("Sent %d bytes of UDPTL data to %s:%d\n", res, ast_inet_ntoa(iabuf, sizeof(iabuf), udptl->them.sin_addr), ntohs(udptl->them.sin_port));
923 #endif
924                 if (udptl_debug_test_addr(&s->them))
925                         ast_verbose("Sent UDPTL packet to %s:%d (type %d, seq %d, len %d)\n",
926                                         ast_inet_ntoa(iabuf, sizeof(iabuf), s->them.sin_addr),
927                                         ntohs(s->them.sin_port), 0, s->seqno, len);
928         }
929                 
930         return 0;
931 }
932
933 void ast_udptl_proto_unregister(struct ast_udptl_protocol *proto)
934 {
935         struct ast_udptl_protocol *cur;
936         struct ast_udptl_protocol *prev;
937
938         cur = protos;
939         prev = NULL;
940         while(cur) {
941                 if (cur == proto) {
942                         if (prev)
943                                 prev->next = proto->next;
944                         else
945                                 protos = proto->next;
946                         return;
947                 }
948                 prev = cur;
949                 cur = cur->next;
950         }
951 }
952
953 int ast_udptl_proto_register(struct ast_udptl_protocol *proto)
954 {
955         struct ast_udptl_protocol *cur;
956
957         cur = protos;
958         while(cur) {
959                 if (cur->type == proto->type) {
960                         ast_log(LOG_WARNING, "Tried to register same protocol '%s' twice\n", cur->type);
961                         return -1;
962                 }
963                 cur = cur->next;
964         }
965         proto->next = protos;
966         protos = proto;
967         return 0;
968 }
969
970 static struct ast_udptl_protocol *get_proto(struct ast_channel *chan)
971 {
972         struct ast_udptl_protocol *cur;
973
974         cur = protos;
975         while (cur) {
976                 if (cur->type == chan->type)
977                         return cur;
978                 cur = cur->next;
979         }
980         return NULL;
981 }
982
983 int ast_udptl_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
984 {
985         struct ast_frame *f;
986         struct ast_channel *who;
987         struct ast_channel *cs[3];
988         struct ast_udptl *p0;
989         struct ast_udptl *p1;
990         struct ast_udptl_protocol *pr0;
991         struct ast_udptl_protocol *pr1;
992         struct sockaddr_in ac0;
993         struct sockaddr_in ac1;
994         struct sockaddr_in t0;
995         struct sockaddr_in t1;
996         char iabuf[INET_ADDRSTRLEN];
997         void *pvt0;
998         void *pvt1;
999         int to;
1000         
1001         ast_mutex_lock(&c0->lock);
1002         while (ast_mutex_trylock(&c1->lock)) {
1003                 ast_mutex_unlock(&c0->lock);
1004                 usleep(1);
1005                 ast_mutex_lock(&c0->lock);
1006         }
1007         pr0 = get_proto(c0);
1008         pr1 = get_proto(c1);
1009         if (!pr0) {
1010                 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c0->name);
1011                 ast_mutex_unlock(&c0->lock);
1012                 ast_mutex_unlock(&c1->lock);
1013                 return -1;
1014         }
1015         if (!pr1) {
1016                 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c1->name);
1017                 ast_mutex_unlock(&c0->lock);
1018                 ast_mutex_unlock(&c1->lock);
1019                 return -1;
1020         }
1021         pvt0 = c0->tech_pvt;
1022         pvt1 = c1->tech_pvt;
1023         p0 = pr0->get_udptl_info(c0);
1024         p1 = pr1->get_udptl_info(c1);
1025         if (!p0 || !p1) {
1026                 /* Somebody doesn't want to play... */
1027                 ast_mutex_unlock(&c0->lock);
1028                 ast_mutex_unlock(&c1->lock);
1029                 return -2;
1030         }
1031         if (pr0->set_udptl_peer(c0, p1)) {
1032                 ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c0->name, c1->name);
1033         } else {
1034                 /* Store UDPTL peer */
1035                 ast_udptl_get_peer(p1, &ac1);
1036         }
1037         if (pr1->set_udptl_peer(c1, p0))
1038                 ast_log(LOG_WARNING, "Channel '%s' failed to talk back to '%s'\n", c1->name, c0->name);
1039         else {
1040                 /* Store UDPTL peer */
1041                 ast_udptl_get_peer(p0, &ac0);
1042         }
1043         ast_mutex_unlock(&c0->lock);
1044         ast_mutex_unlock(&c1->lock);
1045         cs[0] = c0;
1046         cs[1] = c1;
1047         cs[2] = NULL;
1048         for (;;) {
1049                 if ((c0->tech_pvt != pvt0)  ||
1050                         (c1->tech_pvt != pvt1) ||
1051                         (c0->masq || c0->masqr || c1->masq || c1->masqr)) {
1052                                 ast_log(LOG_DEBUG, "Oooh, something is weird, backing out\n");
1053                                 /* Tell it to try again later */
1054                                 return -3;
1055                 }
1056                 to = -1;
1057                 ast_udptl_get_peer(p1, &t1);
1058                 ast_udptl_get_peer(p0, &t0);
1059                 if (inaddrcmp(&t1, &ac1)) {
1060                         ast_log(LOG_DEBUG, "Oooh, '%s' changed end address to %s:%d\n", 
1061                                 c1->name, ast_inet_ntoa(iabuf, sizeof(iabuf), t1.sin_addr), ntohs(t1.sin_port));
1062                         ast_log(LOG_DEBUG, "Oooh, '%s' was %s:%d\n", 
1063                                 c1->name, ast_inet_ntoa(iabuf, sizeof(iabuf), ac1.sin_addr), ntohs(ac1.sin_port));
1064                         memcpy(&ac1, &t1, sizeof(ac1));
1065                 }
1066                 if (inaddrcmp(&t0, &ac0)) {
1067                         ast_log(LOG_DEBUG, "Oooh, '%s' changed end address to %s:%d\n", 
1068                                 c0->name, ast_inet_ntoa(iabuf, sizeof(iabuf), t0.sin_addr), ntohs(t0.sin_port));
1069                         ast_log(LOG_DEBUG, "Oooh, '%s' was %s:%d\n", 
1070                                 c0->name, ast_inet_ntoa(iabuf, sizeof(iabuf), ac0.sin_addr), ntohs(ac0.sin_port));
1071                         memcpy(&ac0, &t0, sizeof(ac0));
1072                 }
1073                 who = ast_waitfor_n(cs, 2, &to);
1074                 if (!who) {
1075                         ast_log(LOG_DEBUG, "Ooh, empty read...\n");
1076                         /* check for hangup / whentohangup */
1077                         if (ast_check_hangup(c0) || ast_check_hangup(c1))
1078                                 break;
1079                         continue;
1080                 }
1081                 f = ast_read(who);
1082                 if (!f) {
1083                         *fo = f;
1084                         *rc = who;
1085                         ast_log(LOG_DEBUG, "Oooh, got a %s\n", f ? "digit" : "hangup");
1086                         /* That's all we needed */
1087                         return 0;
1088                 } else {
1089                         if (f->frametype == AST_FRAME_MODEM) {
1090                                 /* Forward T.38 frames if they happen upon us */
1091                                 if (who == c0) {
1092                                         ast_write(c1, f);
1093                                 } else if (who == c1) {
1094                                         ast_write(c0, f);
1095                                 }
1096                         }
1097                         ast_frfree(f);
1098                 }
1099                 /* Swap priority. Not that it's a big deal at this point */
1100                 cs[2] = cs[0];
1101                 cs[0] = cs[1];
1102                 cs[1] = cs[2];
1103         }
1104         return -1;
1105 }
1106
1107 static int udptl_do_debug_ip(int fd, int argc, char *argv[])
1108 {
1109         struct hostent *hp;
1110         struct ast_hostent ahp;
1111         char iabuf[INET_ADDRSTRLEN];
1112         int port;
1113         char *p;
1114         char *arg;
1115
1116         port = 0;
1117         if (argc != 4)
1118                 return RESULT_SHOWUSAGE;
1119         arg = argv[3];
1120         p = strstr(arg, ":");
1121         if (p) {
1122                 *p = '\0';
1123                 p++;
1124                 port = atoi(p);
1125         }
1126         hp = ast_gethostbyname(arg, &ahp);
1127         if (hp == NULL)
1128                 return RESULT_SHOWUSAGE;
1129         udptldebugaddr.sin_family = AF_INET;
1130         memcpy(&udptldebugaddr.sin_addr, hp->h_addr, sizeof(udptldebugaddr.sin_addr));
1131         udptldebugaddr.sin_port = htons(port);
1132         if (port == 0)
1133                 ast_cli(fd, "UDPTL Debugging Enabled for IP: %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), udptldebugaddr.sin_addr));
1134         else
1135                 ast_cli(fd, "UDPTL Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(iabuf, sizeof(iabuf), udptldebugaddr.sin_addr), port);
1136         udptldebug = 1;
1137         return RESULT_SUCCESS;
1138 }
1139
1140 static int udptl_do_debug(int fd, int argc, char *argv[])
1141 {
1142         if (argc != 2) {
1143                 if (argc != 4)
1144                         return RESULT_SHOWUSAGE;
1145                 return udptl_do_debug_ip(fd, argc, argv);
1146         }
1147         udptldebug = 1;
1148         memset(&udptldebugaddr,0,sizeof(udptldebugaddr));
1149         ast_cli(fd, "UDPTL Debugging Enabled\n");
1150         return RESULT_SUCCESS;
1151 }
1152    
1153 static int udptl_no_debug(int fd, int argc, char *argv[])
1154 {
1155         if (argc !=3)
1156                 return RESULT_SHOWUSAGE;
1157         udptldebug = 0;
1158         ast_cli(fd,"UDPTL Debugging Disabled\n");
1159         return RESULT_SUCCESS;
1160 }
1161
1162 static char debug_usage[] =
1163   "Usage: udptl debug [ip host[:port]]\n"
1164   "       Enable dumping of all UDPTL packets to and from host.\n";
1165
1166 static char no_debug_usage[] =
1167   "Usage: udptl no debug\n"
1168   "       Disable all UDPTL debugging\n";
1169
1170 static struct ast_cli_entry  cli_debug_ip =
1171 {{ "udptl", "debug", "ip", NULL } , udptl_do_debug, "Enable UDPTL debugging on IP", debug_usage };
1172
1173 static struct ast_cli_entry  cli_debug =
1174 {{ "udptl", "debug", NULL } , udptl_do_debug, "Enable UDPTL debugging", debug_usage };
1175
1176 static struct ast_cli_entry  cli_no_debug =
1177 {{ "udptl", "no", "debug", NULL } , udptl_no_debug, "Disable UDPTL debugging", no_debug_usage };
1178
1179 void ast_udptl_reload(void)
1180 {
1181         struct ast_config *cfg;
1182         char *s;
1183
1184         udptlstart = 4500;
1185         udptlend = 4999;
1186         udptlfectype = 0;
1187         udptlfecentries = 0;
1188         udptlfecspan = 0;
1189         udptlmaxdatagram = 0;
1190
1191         if ((cfg = ast_config_load("udptl.conf"))) {
1192                 if ((s = ast_variable_retrieve(cfg, "general", "udptlstart"))) {
1193                         udptlstart = atoi(s);
1194                         if (udptlstart < 1024)
1195                                 udptlstart = 1024;
1196                         if (udptlstart > 65535)
1197                                 udptlstart = 65535;
1198                 }
1199                 if ((s = ast_variable_retrieve(cfg, "general", "udptlend"))) {
1200                         udptlend = atoi(s);
1201                         if (udptlend < 1024)
1202                                 udptlend = 1024;
1203                         if (udptlend > 65535)
1204                                 udptlend = 65535;
1205                 }
1206                 if ((s = ast_variable_retrieve(cfg, "general", "udptlchecksums"))) {
1207 #ifdef SO_NO_CHECK
1208                         if (ast_false(s))
1209                                 nochecksums = 1;
1210                         else
1211                                 nochecksums = 0;
1212 #else
1213                         if (ast_false(s))
1214                                 ast_log(LOG_WARNING, "Disabling UDPTL checksums is not supported on this operating system!\n");
1215 #endif
1216                 }
1217                 if ((s = ast_variable_retrieve(cfg, "general", "T38FaxUdpEC"))) {
1218                         if (strcmp(s, "t38UDPFEC") == 0)
1219                                 udptlfectype = 2;
1220                         else if (strcmp(s, "t38UDPRedundancy") == 0)
1221                                 udptlfectype = 1;
1222                 }
1223                 if ((s = ast_variable_retrieve(cfg, "general", "T38FaxMaxDatagram"))) {
1224                         udptlmaxdatagram = atoi(s);
1225                         if (udptlmaxdatagram < 0)
1226                                 udptlmaxdatagram = 0;
1227                         if (udptlmaxdatagram > LOCAL_FAX_MAX_DATAGRAM)
1228                                 udptlmaxdatagram = LOCAL_FAX_MAX_DATAGRAM;
1229                 }
1230                 if ((s = ast_variable_retrieve(cfg, "general", "UDPTLFECentries"))) {
1231                         udptlfecentries = atoi(s);
1232                         if (udptlfecentries < 0)
1233                                 udptlfecentries = 0;
1234                         if (udptlfecentries > MAX_FEC_ENTRIES)
1235                                 udptlfecentries = MAX_FEC_ENTRIES;
1236                 }
1237                 if ((s = ast_variable_retrieve(cfg, "general", "UDPTLFECspan"))) {
1238                         udptlfecspan = atoi(s);
1239                         if (udptlfecspan < 0)
1240                                 udptlfecspan = 0;
1241                         if (udptlfecspan > MAX_FEC_SPAN)
1242                                 udptlfecspan = MAX_FEC_SPAN;
1243                 }
1244                 ast_config_destroy(cfg);
1245         }
1246         if (udptlstart >= udptlend) {
1247                 ast_log(LOG_WARNING, "Unreasonable values for UDPTL start/end\n");
1248                 udptlstart = 4500;
1249                 udptlend = 4999;
1250         }
1251         if (option_verbose > 1)
1252                 ast_verbose(VERBOSE_PREFIX_2 "UDPTL allocating from port range %d -> %d\n", udptlstart, udptlend);
1253 }
1254
1255 void ast_udptl_init(void)
1256 {
1257         ast_cli_register(&cli_debug);
1258         ast_cli_register(&cli_debug_ip);
1259         ast_cli_register(&cli_no_debug);
1260         ast_udptl_reload();
1261 }