Fri Sep 11 13:45:03 2009

Asterisk developer's documentation


udptl.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- A telephony toolkit for Linux.
00003  *
00004  * UDPTL support for T.38
00005  * 
00006  * Copyright (C) 2005, Steve Underwood, partly based on RTP code which is
00007  * Copyright (C) 1999-2006, Digium, Inc.
00008  *
00009  * Steve Underwood <steveu@coppice.org>
00010  *
00011  * This program is free software, distributed under the terms of
00012  * the GNU General Public License
00013  *
00014  * A license has been granted to Digium (via disclaimer) for the use of
00015  * this code.
00016  */
00017 
00018 #include "asterisk.h"
00019 
00020 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 175311 $")
00021 
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <string.h>
00025 #include <sys/time.h>
00026 #include <signal.h>
00027 #include <errno.h>
00028 #include <unistd.h>
00029 #include <netinet/in.h>
00030 #include <sys/time.h>
00031 #include <sys/socket.h>
00032 #include <arpa/inet.h>
00033 #include <fcntl.h>
00034 
00035 #include "asterisk/udptl.h"
00036 #include "asterisk/frame.h"
00037 #include "asterisk/logger.h"
00038 #include "asterisk/options.h"
00039 #include "asterisk/channel.h"
00040 #include "asterisk/acl.h"
00041 #include "asterisk/channel.h"
00042 #include "asterisk/config.h"
00043 #include "asterisk/lock.h"
00044 #include "asterisk/utils.h"
00045 #include "asterisk/cli.h"
00046 #include "asterisk/unaligned.h"
00047 #include "asterisk/utils.h"
00048 
00049 #define UDPTL_MTU    1200
00050 
00051 #if !defined(FALSE)
00052 #define FALSE 0
00053 #endif
00054 #if !defined(TRUE)
00055 #define TRUE (!FALSE)
00056 #endif
00057 
00058 static int udptlstart;
00059 static int udptlend;
00060 static int udptldebug;                    /* Are we debugging? */
00061 static struct sockaddr_in udptldebugaddr;   /* Debug packets to/from this host */
00062 #ifdef SO_NO_CHECK
00063 static int nochecksums;
00064 #endif
00065 static int udptlfectype;
00066 static int udptlfecentries;
00067 static int udptlfecspan;
00068 static int udptlmaxdatagram;
00069 
00070 #define LOCAL_FAX_MAX_DATAGRAM      1400
00071 #define MAX_FEC_ENTRIES             5
00072 #define MAX_FEC_SPAN                5
00073 
00074 #define UDPTL_BUF_MASK              15
00075 
00076 typedef struct {
00077    int buf_len;
00078    uint8_t buf[LOCAL_FAX_MAX_DATAGRAM];
00079 } udptl_fec_tx_buffer_t;
00080 
00081 typedef struct {
00082    int buf_len;
00083    uint8_t buf[LOCAL_FAX_MAX_DATAGRAM];
00084    int fec_len[MAX_FEC_ENTRIES];
00085    uint8_t fec[MAX_FEC_ENTRIES][LOCAL_FAX_MAX_DATAGRAM];
00086    int fec_span;
00087    int fec_entries;
00088 } udptl_fec_rx_buffer_t;
00089 
00090 struct ast_udptl {
00091    int fd;
00092    char resp;
00093    struct ast_frame f[16];
00094    unsigned char rawdata[8192 + AST_FRIENDLY_OFFSET];
00095    unsigned int lasteventseqn;
00096    int nat;
00097    int flags;
00098    struct sockaddr_in us;
00099    struct sockaddr_in them;
00100    int *ioid;
00101    struct sched_context *sched;
00102    struct io_context *io;
00103    void *data;
00104    ast_udptl_callback callback;
00105    int udptl_offered_from_local;
00106 
00107    /*! This option indicates the error correction scheme used in transmitted UDPTL
00108        packets. */
00109    int error_correction_scheme;
00110 
00111    /*! This option indicates the number of error correction entries transmitted in
00112        UDPTL packets. */
00113    int error_correction_entries;
00114 
00115    /*! This option indicates the span of the error correction entries in transmitted
00116        UDPTL packets (FEC only). */
00117    int error_correction_span;
00118 
00119    /*! This option indicates the maximum size of a UDPTL packet that can be accepted by
00120        the remote device. */
00121    int far_max_datagram_size;
00122 
00123    /*! This option indicates the maximum size of a UDPTL packet that we are prepared to
00124        accept. */
00125    int local_max_datagram_size;
00126 
00127    int verbose;
00128 
00129    struct sockaddr_in far;
00130 
00131    int tx_seq_no;
00132    int rx_seq_no;
00133    int rx_expected_seq_no;
00134 
00135    udptl_fec_tx_buffer_t tx[UDPTL_BUF_MASK + 1];
00136    udptl_fec_rx_buffer_t rx[UDPTL_BUF_MASK + 1];
00137 };
00138 
00139 static struct ast_udptl_protocol *protos;
00140 
00141 static int udptl_rx_packet(struct ast_udptl *s, uint8_t *buf, int len);
00142 static int udptl_build_packet(struct ast_udptl *s, uint8_t *buf, int buflen, uint8_t *ifp, int ifp_len);
00143 
00144 static inline int udptl_debug_test_addr(struct sockaddr_in *addr)
00145 {
00146    if (udptldebug == 0)
00147       return 0;
00148    if (udptldebugaddr.sin_addr.s_addr) {
00149       if (((ntohs(udptldebugaddr.sin_port) != 0)
00150          && (udptldebugaddr.sin_port != addr->sin_port))
00151          || (udptldebugaddr.sin_addr.s_addr != addr->sin_addr.s_addr))
00152          return 0;
00153    }
00154    return 1;
00155 }
00156 
00157 static int decode_length(uint8_t *buf, int limit, int *len, int *pvalue)
00158 {
00159    if (*len >= limit)
00160       return -1;
00161    if ((buf[*len] & 0x80) == 0) {
00162       *pvalue = buf[*len];
00163       (*len)++;
00164       return 0;
00165    }
00166    if ((buf[*len] & 0x40) == 0) {
00167       if (*len == limit - 1)
00168          return -1;
00169       *pvalue = (buf[*len] & 0x3F) << 8;
00170       (*len)++;
00171       *pvalue |= buf[*len];
00172       (*len)++;
00173       return 0;
00174    }
00175    *pvalue = (buf[*len] & 0x3F) << 14;
00176    (*len)++;
00177    /* Indicate we have a fragment */
00178    return 1;
00179 }
00180 /*- End of function --------------------------------------------------------*/
00181 
00182 static int decode_open_type(uint8_t *buf, int limit, int *len, const uint8_t **p_object, int *p_num_octets)
00183 {
00184    int octet_cnt;
00185    int octet_idx;
00186    int stat;
00187    int i;
00188    const uint8_t **pbuf;
00189 
00190    for (octet_idx = 0, *p_num_octets = 0; ; octet_idx += octet_cnt) {
00191       if ((stat = decode_length(buf, limit, len, &octet_cnt)) < 0)
00192          return -1;
00193       if (octet_cnt > 0) {
00194          *p_num_octets += octet_cnt;
00195 
00196          pbuf = &p_object[octet_idx];
00197          i = 0;
00198          /* Make sure the buffer contains at least the number of bits requested */
00199          if ((*len + octet_cnt) > limit)
00200             return -1;
00201 
00202          *pbuf = &buf[*len];
00203          *len += octet_cnt;
00204       }
00205       if (stat == 0)
00206          break;
00207    }
00208    return 0;
00209 }
00210 /*- End of function --------------------------------------------------------*/
00211 
00212 static int encode_length(uint8_t *buf, int *len, int value)
00213 {
00214    int multiplier;
00215 
00216    if (value < 0x80) {
00217       /* 1 octet */
00218       buf[*len] = value;
00219       (*len)++;
00220       return value;
00221    }
00222    if (value < 0x4000) {
00223       /* 2 octets */
00224       /* Set the first bit of the first octet */
00225       buf[*len] = ((0x8000 | value) >> 8) & 0xFF;
00226       (*len)++;
00227       buf[*len] = value & 0xFF;
00228       (*len)++;
00229       return value;
00230    }
00231    /* Fragmentation */
00232    multiplier = (value < 0x10000) ? (value >> 14) : 4;
00233    /* Set the first 2 bits of the octet */
00234    buf[*len] = 0xC0 | multiplier;
00235    (*len)++;
00236    return multiplier << 14;
00237 }
00238 /*- End of function --------------------------------------------------------*/
00239 
00240 static int encode_open_type(uint8_t *buf, int buflen, int *len, const uint8_t *data, int num_octets)
00241 {
00242    int enclen;
00243    int octet_idx;
00244    uint8_t zero_byte;
00245 
00246    /* If open type is of zero length, add a single zero byte (10.1) */
00247    if (num_octets == 0) {
00248       zero_byte = 0;
00249       data = &zero_byte;
00250       num_octets = 1;
00251    }
00252    /* Encode the open type */
00253    for (octet_idx = 0; ; num_octets -= enclen, octet_idx += enclen) {
00254       if ((enclen = encode_length(buf, len, num_octets)) < 0)
00255          return -1;
00256       if (enclen + *len > buflen) {
00257          ast_log(LOG_ERROR, "Buffer overflow detected (%d + %d > %d)\n", enclen, *len, buflen);
00258          return -1;
00259       }
00260       if (enclen > 0) {
00261          memcpy(&buf[*len], &data[octet_idx], enclen);
00262          *len += enclen;
00263       }
00264       if (enclen >= num_octets)
00265          break;
00266    }
00267 
00268    return 0;
00269 }
00270 /*- End of function --------------------------------------------------------*/
00271 
00272 static int udptl_rx_packet(struct ast_udptl *s, uint8_t *buf, int len)
00273 {
00274    int stat;
00275    int stat2;
00276    int i;
00277    int j;
00278    int k;
00279    int l;
00280    int m;
00281    int x;
00282    int limit;
00283    int which;
00284    int ptr;
00285    int count;
00286    int total_count;
00287    int seq_no;
00288    const uint8_t *ifp;
00289    const uint8_t *data;
00290    int ifp_len;
00291    int repaired[16];
00292    const uint8_t *bufs[16];
00293    int lengths[16];
00294    int span;
00295    int entries;
00296    int ifp_no;
00297 
00298    ptr = 0;
00299    ifp_no = 0;
00300    memset(&s->f[0], 0, sizeof(s->f[0]));
00301 
00302    /* Decode seq_number */
00303    if (ptr + 2 > len)
00304       return -1;
00305    seq_no = (buf[0] << 8) | buf[1];
00306    ptr += 2;
00307 
00308    /* Break out the primary packet */
00309    if ((stat = decode_open_type(buf, len, &ptr, &ifp, &ifp_len)) != 0)
00310       return -1;
00311    /* Decode error_recovery */
00312    if (ptr + 1 > len)
00313       return -1;
00314    if ((buf[ptr++] & 0x80) == 0) {
00315       /* Secondary packet mode for error recovery */
00316       if (seq_no > s->rx_seq_no) {
00317          /* We received a later packet than we expected, so we need to check if we can fill in the gap from the
00318             secondary packets. */
00319          total_count = 0;
00320          do {
00321             if ((stat2 = decode_length(buf, len, &ptr, &count)) < 0)
00322                return -1;
00323             for (i = 0; i < count; i++) {
00324                if ((stat = decode_open_type(buf, len, &ptr, &bufs[total_count + i], &lengths[total_count + i])) != 0)
00325                   return -1;
00326             }
00327             total_count += count;
00328          }
00329          while (stat2 > 0);
00330          /* Step through in reverse order, so we go oldest to newest */
00331          for (i = total_count; i > 0; i--) {
00332             if (seq_no - i >= s->rx_seq_no) {
00333                /* This one wasn't seen before */
00334                /* Decode the secondary IFP packet */
00335                //fprintf(stderr, "Secondary %d, len %d\n", seq_no - i, lengths[i - 1]);
00336                s->f[ifp_no].frametype = AST_FRAME_MODEM;
00337                s->f[ifp_no].subclass = AST_MODEM_T38;
00338 
00339                s->f[ifp_no].mallocd = 0;
00340                s->f[ifp_no].seqno = seq_no - i;
00341                s->f[ifp_no].datalen = lengths[i - 1];
00342                s->f[ifp_no].data = (uint8_t *) bufs[i - 1];
00343                s->f[ifp_no].offset = 0;
00344                s->f[ifp_no].src = "UDPTL";
00345                if (ifp_no > 0)
00346                   AST_LIST_NEXT(&s->f[ifp_no - 1], frame_list) = &s->f[ifp_no];
00347                AST_LIST_NEXT(&s->f[ifp_no], frame_list) = NULL;
00348                ifp_no++;
00349             }
00350          }
00351       }
00352    }
00353    else
00354    {
00355       /* FEC mode for error recovery */
00356       /* Our buffers cannot tolerate overlength IFP packets in FEC mode */
00357       if (ifp_len > LOCAL_FAX_MAX_DATAGRAM)
00358          return -1;
00359       /* Update any missed slots in the buffer */
00360       for ( ; seq_no > s->rx_seq_no; s->rx_seq_no++) {
00361          x = s->rx_seq_no & UDPTL_BUF_MASK;
00362          s->rx[x].buf_len = -1;
00363          s->rx[x].fec_len[0] = 0;
00364          s->rx[x].fec_span = 0;
00365          s->rx[x].fec_entries = 0;
00366       }
00367 
00368       x = seq_no & UDPTL_BUF_MASK;
00369 
00370       memset(repaired, 0, sizeof(repaired));
00371 
00372       /* Save the new IFP packet */
00373       memcpy(s->rx[x].buf, ifp, ifp_len);
00374       s->rx[x].buf_len = ifp_len;
00375       repaired[x] = TRUE;
00376 
00377       /* Decode the FEC packets */
00378       /* The span is defined as an unconstrained integer, but will never be more
00379          than a small value. */
00380       if (ptr + 2 > len)
00381          return -1;
00382       if (buf[ptr++] != 1)
00383          return -1;
00384       span = buf[ptr++];
00385       s->rx[x].fec_span = span;
00386 
00387       /* The number of entries is defined as a length, but will only ever be a small
00388          value. Treat it as such. */
00389       if (ptr + 1 > len)
00390          return -1;
00391       entries = buf[ptr++];
00392       s->rx[x].fec_entries = entries;
00393 
00394       /* Decode the elements */
00395       for (i = 0; i < entries; i++) {
00396          if ((stat = decode_open_type(buf, len, &ptr, &data, &s->rx[x].fec_len[i])) != 0)
00397             return -1;
00398          if (s->rx[x].fec_len[i] > LOCAL_FAX_MAX_DATAGRAM)
00399             return -1;
00400 
00401          /* Save the new FEC data */
00402          memcpy(s->rx[x].fec[i], data, s->rx[x].fec_len[i]);
00403 #if 0
00404          fprintf(stderr, "FEC: ");
00405          for (j = 0; j < s->rx[x].fec_len[i]; j++)
00406             fprintf(stderr, "%02X ", data[j]);
00407          fprintf(stderr, "\n");
00408 #endif
00409       }
00410 
00411       /* See if we can reconstruct anything which is missing */
00412       /* TODO: this does not comprehensively hunt back and repair everything that is possible */
00413       for (l = x; l != ((x - (16 - span*entries)) & UDPTL_BUF_MASK); l = (l - 1) & UDPTL_BUF_MASK) {
00414          if (s->rx[l].fec_len[0] <= 0)
00415             continue;
00416          for (m = 0; m < s->rx[l].fec_entries; m++) {
00417             limit = (l + m) & UDPTL_BUF_MASK;
00418             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) {
00419                if (s->rx[k].buf_len <= 0)
00420                   which = (which == -1) ? k : -2;
00421             }
00422             if (which >= 0) {
00423                /* Repairable */
00424                for (j = 0; j < s->rx[l].fec_len[m]; j++) {
00425                   s->rx[which].buf[j] = s->rx[l].fec[m][j];
00426                   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)
00427                      s->rx[which].buf[j] ^= (s->rx[k].buf_len > j) ? s->rx[k].buf[j] : 0;
00428                }
00429                s->rx[which].buf_len = s->rx[l].fec_len[m];
00430                repaired[which] = TRUE;
00431             }
00432          }
00433       }
00434       /* Now play any new packets forwards in time */
00435       for (l = (x + 1) & UDPTL_BUF_MASK, j = seq_no - UDPTL_BUF_MASK; l != x; l = (l + 1) & UDPTL_BUF_MASK, j++) {
00436          if (repaired[l]) {
00437             //fprintf(stderr, "Fixed packet %d, len %d\n", j, l);
00438             s->f[ifp_no].frametype = AST_FRAME_MODEM;
00439             s->f[ifp_no].subclass = AST_MODEM_T38;
00440          
00441             s->f[ifp_no].mallocd = 0;
00442             s->f[ifp_no].seqno = j;
00443             s->f[ifp_no].datalen = s->rx[l].buf_len;
00444             s->f[ifp_no].data = s->rx[l].buf;
00445             s->f[ifp_no].offset = 0;
00446             s->f[ifp_no].src = "UDPTL";
00447             if (ifp_no > 0)
00448                AST_LIST_NEXT(&s->f[ifp_no - 1], frame_list) = &s->f[ifp_no];
00449             AST_LIST_NEXT(&s->f[ifp_no], frame_list) = NULL;
00450             ifp_no++;
00451          }
00452       }
00453    }
00454 
00455    /* If packets are received out of sequence, we may have already processed this packet from the error
00456       recovery information in a packet already received. */
00457    if (seq_no >= s->rx_seq_no) {
00458       /* Decode the primary IFP packet */
00459       s->f[ifp_no].frametype = AST_FRAME_MODEM;
00460       s->f[ifp_no].subclass = AST_MODEM_T38;
00461       
00462       s->f[ifp_no].mallocd = 0;
00463       s->f[ifp_no].seqno = seq_no;
00464       s->f[ifp_no].datalen = ifp_len;
00465       s->f[ifp_no].data = (uint8_t *) ifp;
00466       s->f[ifp_no].offset = 0;
00467       s->f[ifp_no].src = "UDPTL";
00468       if (ifp_no > 0)
00469          AST_LIST_NEXT(&s->f[ifp_no - 1], frame_list) = &s->f[ifp_no];
00470       AST_LIST_NEXT(&s->f[ifp_no], frame_list) = NULL;
00471 
00472       ifp_no++;
00473    }
00474 
00475    s->rx_seq_no = seq_no + 1;
00476    return ifp_no;
00477 }
00478 /*- End of function --------------------------------------------------------*/
00479 
00480 static int udptl_build_packet(struct ast_udptl *s, uint8_t *buf, int buflen, uint8_t *ifp, int ifp_len)
00481 {
00482    uint8_t fec[LOCAL_FAX_MAX_DATAGRAM * 2];
00483    int i;
00484    int j;
00485    int seq;
00486    int entry;
00487    int entries;
00488    int span;
00489    int m;
00490    int len;
00491    int limit;
00492    int high_tide;
00493 
00494    seq = s->tx_seq_no & 0xFFFF;
00495 
00496    /* Map the sequence number to an entry in the circular buffer */
00497    entry = seq & UDPTL_BUF_MASK;
00498 
00499    /* We save the message in a circular buffer, for generating FEC or
00500       redundancy sets later on. */
00501    s->tx[entry].buf_len = ifp_len;
00502    memcpy(s->tx[entry].buf, ifp, ifp_len);
00503    
00504    /* Build the UDPTLPacket */
00505 
00506    len = 0;
00507    /* Encode the sequence number */
00508    buf[len++] = (seq >> 8) & 0xFF;
00509    buf[len++] = seq & 0xFF;
00510 
00511    /* Encode the primary IFP packet */
00512    if (encode_open_type(buf, buflen, &len, ifp, ifp_len) < 0)
00513       return -1;
00514 
00515    /* Encode the appropriate type of error recovery information */
00516    switch (s->error_correction_scheme)
00517    {
00518    case UDPTL_ERROR_CORRECTION_NONE:
00519       /* Encode the error recovery type */
00520       buf[len++] = 0x00;
00521       /* The number of entries will always be zero, so it is pointless allowing
00522          for the fragmented case here. */
00523       if (encode_length(buf, &len, 0) < 0)
00524          return -1;
00525       break;
00526    case UDPTL_ERROR_CORRECTION_REDUNDANCY:
00527       /* Encode the error recovery type */
00528       buf[len++] = 0x00;
00529       if (s->tx_seq_no > s->error_correction_entries)
00530          entries = s->error_correction_entries;
00531       else
00532          entries = s->tx_seq_no;
00533       /* The number of entries will always be small, so it is pointless allowing
00534          for the fragmented case here. */
00535       if (encode_length(buf, &len, entries) < 0)
00536          return -1;
00537       /* Encode the elements */
00538       for (i = 0; i < entries; i++) {
00539          j = (entry - i - 1) & UDPTL_BUF_MASK;
00540          if (encode_open_type(buf, buflen, &len, s->tx[j].buf, s->tx[j].buf_len) < 0) {
00541             if (option_debug) {
00542                ast_log(LOG_DEBUG, "Encoding failed at i=%d, j=%d\n", i, j);
00543             }
00544             return -1;
00545          }
00546       }
00547       break;
00548    case UDPTL_ERROR_CORRECTION_FEC:
00549       span = s->error_correction_span;
00550       entries = s->error_correction_entries;
00551       if (seq < s->error_correction_span*s->error_correction_entries) {
00552          /* In the initial stages, wind up the FEC smoothly */
00553          entries = seq/s->error_correction_span;
00554          if (seq < s->error_correction_span)
00555             span = 0;
00556       }
00557       /* Encode the error recovery type */
00558       buf[len++] = 0x80;
00559       /* Span is defined as an inconstrained integer, which it dumb. It will only
00560          ever be a small value. Treat it as such. */
00561       buf[len++] = 1;
00562       buf[len++] = span;
00563       /* The number of entries is defined as a length, but will only ever be a small
00564          value. Treat it as such. */
00565       buf[len++] = entries;
00566       for (m = 0; m < entries; m++) {
00567          /* Make an XOR'ed entry the maximum length */
00568          limit = (entry + m) & UDPTL_BUF_MASK;
00569          high_tide = 0;
00570          for (i = (limit - span*entries) & UDPTL_BUF_MASK; i != limit; i = (i + entries) & UDPTL_BUF_MASK) {
00571             if (high_tide < s->tx[i].buf_len) {
00572                for (j = 0; j < high_tide; j++)
00573                   fec[j] ^= s->tx[i].buf[j];
00574                for ( ; j < s->tx[i].buf_len; j++)
00575                   fec[j] = s->tx[i].buf[j];
00576                high_tide = s->tx[i].buf_len;
00577             } else {
00578                for (j = 0; j < s->tx[i].buf_len; j++)
00579                   fec[j] ^= s->tx[i].buf[j];
00580             }
00581          }
00582          if (encode_open_type(buf, buflen, &len, fec, high_tide) < 0)
00583             return -1;
00584       }
00585       break;
00586    }
00587 
00588    if (s->verbose)
00589       fprintf(stderr, "\n");
00590 
00591    s->tx_seq_no++;
00592    return len;
00593 }
00594 
00595 int ast_udptl_fd(struct ast_udptl *udptl)
00596 {
00597    return udptl->fd;
00598 }
00599 
00600 void ast_udptl_set_data(struct ast_udptl *udptl, void *data)
00601 {
00602    udptl->data = data;
00603 }
00604 
00605 void ast_udptl_set_callback(struct ast_udptl *udptl, ast_udptl_callback callback)
00606 {
00607    udptl->callback = callback;
00608 }
00609 
00610 void ast_udptl_setnat(struct ast_udptl *udptl, int nat)
00611 {
00612    udptl->nat = nat;
00613 }
00614 
00615 static int udptlread(int *id, int fd, short events, void *cbdata)
00616 {
00617    struct ast_udptl *udptl = cbdata;
00618    struct ast_frame *f;
00619 
00620    if ((f = ast_udptl_read(udptl))) {
00621       if (udptl->callback)
00622          udptl->callback(udptl, f, udptl->data);
00623    }
00624    return 1;
00625 }
00626 
00627 struct ast_frame *ast_udptl_read(struct ast_udptl *udptl)
00628 {
00629    int res;
00630    struct sockaddr_in sin;
00631    socklen_t len;
00632    uint16_t seqno = 0;
00633    uint16_t *udptlheader;
00634 
00635    len = sizeof(sin);
00636    
00637    /* Cache where the header will go */
00638    res = recvfrom(udptl->fd,
00639          udptl->rawdata + AST_FRIENDLY_OFFSET,
00640          sizeof(udptl->rawdata) - AST_FRIENDLY_OFFSET,
00641          0,
00642          (struct sockaddr *) &sin,
00643          &len);
00644    udptlheader = (uint16_t *)(udptl->rawdata + AST_FRIENDLY_OFFSET);
00645    if (res < 0) {
00646       if (errno != EAGAIN)
00647          ast_log(LOG_WARNING, "UDPTL read error: %s\n", strerror(errno));
00648       ast_assert(errno != EBADF);
00649       return &ast_null_frame;
00650    }
00651 
00652    /* Ignore if the other side hasn't been given an address yet. */
00653    if (!udptl->them.sin_addr.s_addr || !udptl->them.sin_port)
00654       return &ast_null_frame;
00655 
00656    if (udptl->nat) {
00657       /* Send to whoever sent to us */
00658       if ((udptl->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
00659          (udptl->them.sin_port != sin.sin_port)) {
00660          memcpy(&udptl->them, &sin, sizeof(udptl->them));
00661          ast_log(LOG_DEBUG, "UDPTL NAT: Using address %s:%d\n", ast_inet_ntoa(udptl->them.sin_addr), ntohs(udptl->them.sin_port));
00662       }
00663    }
00664 
00665    if (udptl_debug_test_addr(&sin)) {
00666       ast_verbose("Got UDPTL packet from %s:%d (type %d, seq %d, len %d)\n",
00667          ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), 0, seqno, res);
00668    }
00669 #if 0
00670    printf("Got UDPTL packet from %s:%d (seq %d, len = %d)\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), seqno, res);
00671 #endif
00672    if (udptl_rx_packet(udptl, udptl->rawdata + AST_FRIENDLY_OFFSET, res) < 1)
00673       return &ast_null_frame;
00674 
00675    return &udptl->f[0];
00676 }
00677 
00678 void ast_udptl_offered_from_local(struct ast_udptl* udptl, int local)
00679 {
00680    if (udptl)
00681       udptl->udptl_offered_from_local = local;
00682    else
00683       ast_log(LOG_WARNING, "udptl structure is null\n");
00684 }
00685 
00686 int ast_udptl_get_error_correction_scheme(struct ast_udptl* udptl)
00687 {
00688    if (udptl)
00689       return udptl->error_correction_scheme;
00690    else {
00691       ast_log(LOG_WARNING, "udptl structure is null\n");
00692       return -1;
00693    }
00694 }
00695 
00696 void ast_udptl_set_error_correction_scheme(struct ast_udptl* udptl, int ec)
00697 {
00698    if (udptl) {
00699       switch (ec) {
00700       case UDPTL_ERROR_CORRECTION_FEC:
00701          udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_FEC;
00702          break;
00703       case UDPTL_ERROR_CORRECTION_REDUNDANCY:
00704          udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_REDUNDANCY;
00705          break;
00706       case UDPTL_ERROR_CORRECTION_NONE:
00707          udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_NONE;
00708          break;
00709       default:
00710          ast_log(LOG_WARNING, "error correction parameter invalid\n");
00711       };
00712    } else
00713       ast_log(LOG_WARNING, "udptl structure is null\n");
00714 }
00715 
00716 int ast_udptl_get_local_max_datagram(struct ast_udptl* udptl)
00717 {
00718    if (udptl)
00719       return udptl->local_max_datagram_size;
00720    else {
00721       ast_log(LOG_WARNING, "udptl structure is null\n");
00722       return -1;
00723    }
00724 }
00725 
00726 int ast_udptl_get_far_max_datagram(struct ast_udptl* udptl)
00727 {
00728    if (udptl)
00729       return udptl->far_max_datagram_size;
00730    else {
00731       ast_log(LOG_WARNING, "udptl structure is null\n");
00732       return -1;
00733    }
00734 }
00735 
00736 void ast_udptl_set_local_max_datagram(struct ast_udptl* udptl, int max_datagram)
00737 {
00738    if (udptl)
00739       udptl->local_max_datagram_size = max_datagram;
00740    else
00741       ast_log(LOG_WARNING, "udptl structure is null\n");
00742 }
00743 
00744 void ast_udptl_set_far_max_datagram(struct ast_udptl* udptl, int max_datagram)
00745 {
00746    if (udptl)
00747       udptl->far_max_datagram_size = max_datagram;
00748    else
00749       ast_log(LOG_WARNING, "udptl structure is null\n");
00750 }
00751 
00752 struct ast_udptl *ast_udptl_new_with_bindaddr(struct sched_context *sched, struct io_context *io, int callbackmode, struct in_addr addr)
00753 {
00754    struct ast_udptl *udptl;
00755    int x;
00756    int startplace;
00757    int i;
00758    long int flags;
00759 
00760    if (!(udptl = ast_calloc(1, sizeof(*udptl))))
00761       return NULL;
00762 
00763    if (udptlfectype == 2)
00764       udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_FEC;
00765    else if (udptlfectype == 1)
00766       udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_REDUNDANCY;
00767    else
00768       udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_NONE;
00769    udptl->error_correction_span = udptlfecspan;
00770    udptl->error_correction_entries = udptlfecentries;
00771    
00772    udptl->far_max_datagram_size = udptlmaxdatagram;
00773    udptl->local_max_datagram_size = udptlmaxdatagram;
00774 
00775    memset(&udptl->rx, 0, sizeof(udptl->rx));
00776    memset(&udptl->tx, 0, sizeof(udptl->tx));
00777    for (i = 0; i <= UDPTL_BUF_MASK; i++) {
00778       udptl->rx[i].buf_len = -1;
00779       udptl->tx[i].buf_len = -1;
00780    }
00781 
00782    udptl->them.sin_family = AF_INET;
00783    udptl->us.sin_family = AF_INET;
00784 
00785    if ((udptl->fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
00786       free(udptl);
00787       ast_log(LOG_WARNING, "Unable to allocate socket: %s\n", strerror(errno));
00788       return NULL;
00789    }
00790    flags = fcntl(udptl->fd, F_GETFL);
00791    fcntl(udptl->fd, F_SETFL, flags | O_NONBLOCK);
00792 #ifdef SO_NO_CHECK
00793    if (nochecksums)
00794       setsockopt(udptl->fd, SOL_SOCKET, SO_NO_CHECK, &nochecksums, sizeof(nochecksums));
00795 #endif
00796    /* Find us a place */
00797    x = (ast_random() % (udptlend - udptlstart)) + udptlstart;
00798    startplace = x;
00799    for (;;) {
00800       udptl->us.sin_port = htons(x);
00801       udptl->us.sin_addr = addr;
00802       if (bind(udptl->fd, (struct sockaddr *) &udptl->us, sizeof(udptl->us)) == 0)
00803          break;
00804       if (errno != EADDRINUSE) {
00805          ast_log(LOG_WARNING, "Unexpected bind error: %s\n", strerror(errno));
00806          close(udptl->fd);
00807          free(udptl);
00808          return NULL;
00809       }
00810       if (++x > udptlend)
00811          x = udptlstart;
00812       if (x == startplace) {
00813          ast_log(LOG_WARNING, "No UDPTL ports remaining\n");
00814          close(udptl->fd);
00815          free(udptl);
00816          return NULL;
00817       }
00818    }
00819    if (io && sched && callbackmode) {
00820       /* Operate this one in a callback mode */
00821       udptl->sched = sched;
00822       udptl->io = io;
00823       udptl->ioid = ast_io_add(udptl->io, udptl->fd, udptlread, AST_IO_IN, udptl);
00824    }
00825    return udptl;
00826 }
00827 
00828 struct ast_udptl *ast_udptl_new(struct sched_context *sched, struct io_context *io, int callbackmode)
00829 {
00830    struct in_addr ia;
00831    memset(&ia, 0, sizeof(ia));
00832    return ast_udptl_new_with_bindaddr(sched, io, callbackmode, ia);
00833 }
00834 
00835 int ast_udptl_settos(struct ast_udptl *udptl, int tos)
00836 {
00837    int res;
00838 
00839    if ((res = setsockopt(udptl->fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)))) 
00840       ast_log(LOG_WARNING, "UDPTL unable to set TOS to %d\n", tos);
00841    return res;
00842 }
00843 
00844 void ast_udptl_set_peer(struct ast_udptl *udptl, struct sockaddr_in *them)
00845 {
00846    udptl->them.sin_port = them->sin_port;
00847    udptl->them.sin_addr = them->sin_addr;
00848 }
00849 
00850 void ast_udptl_get_peer(struct ast_udptl *udptl, struct sockaddr_in *them)
00851 {
00852    memset(them, 0, sizeof(*them));
00853    them->sin_family = AF_INET;
00854    them->sin_port = udptl->them.sin_port;
00855    them->sin_addr = udptl->them.sin_addr;
00856 }
00857 
00858 void ast_udptl_get_us(struct ast_udptl *udptl, struct sockaddr_in *us)
00859 {
00860    memcpy(us, &udptl->us, sizeof(udptl->us));
00861 }
00862 
00863 void ast_udptl_stop(struct ast_udptl *udptl)
00864 {
00865    memset(&udptl->them.sin_addr, 0, sizeof(udptl->them.sin_addr));
00866    memset(&udptl->them.sin_port, 0, sizeof(udptl->them.sin_port));
00867 }
00868 
00869 void ast_udptl_destroy(struct ast_udptl *udptl)
00870 {
00871    if (udptl->ioid)
00872       ast_io_remove(udptl->io, udptl->ioid);
00873    if (udptl->fd > -1)
00874       close(udptl->fd);
00875    free(udptl);
00876 }
00877 
00878 int ast_udptl_write(struct ast_udptl *s, struct ast_frame *f)
00879 {
00880    int seq;
00881    int len;
00882    int res;
00883    uint8_t buf[LOCAL_FAX_MAX_DATAGRAM * 2];
00884 
00885    /* If we have no peer, return immediately */ 
00886    if (s->them.sin_addr.s_addr == INADDR_ANY)
00887       return 0;
00888 
00889    /* If there is no data length, return immediately */
00890    if (f->datalen == 0)
00891       return 0;
00892    
00893    if (f->frametype != AST_FRAME_MODEM) {
00894       ast_log(LOG_WARNING, "UDPTL can only send T.38 data\n");
00895       return -1;
00896    }
00897 
00898    /* Save seq_no for debug output because udptl_build_packet increments it */
00899    seq = s->tx_seq_no & 0xFFFF;
00900 
00901    /* Cook up the UDPTL packet, with the relevant EC info. */
00902    len = udptl_build_packet(s, buf, sizeof(buf), f->data, f->datalen);
00903 
00904    if (len > 0 && s->them.sin_port && s->them.sin_addr.s_addr) {
00905       if ((res = sendto(s->fd, buf, len, 0, (struct sockaddr *) &s->them, sizeof(s->them))) < 0)
00906          ast_log(LOG_NOTICE, "UDPTL Transmission error to %s:%d: %s\n", ast_inet_ntoa(s->them.sin_addr), ntohs(s->them.sin_port), strerror(errno));
00907 #if 0
00908       printf("Sent %d bytes of UDPTL data to %s:%d\n", res, ast_inet_ntoa(udptl->them.sin_addr), ntohs(udptl->them.sin_port));
00909 #endif
00910       if (udptl_debug_test_addr(&s->them))
00911          ast_verbose("Sent UDPTL packet to %s:%d (type %d, seq %d, len %d)\n",
00912                ast_inet_ntoa(s->them.sin_addr),
00913                ntohs(s->them.sin_port), 0, seq, len);
00914    }
00915       
00916    return 0;
00917 }
00918 
00919 void ast_udptl_proto_unregister(struct ast_udptl_protocol *proto)
00920 {
00921    struct ast_udptl_protocol *cur;
00922    struct ast_udptl_protocol *prev;
00923 
00924    cur = protos;
00925    prev = NULL;
00926    while (cur) {
00927       if (cur == proto) {
00928          if (prev)
00929             prev->next = proto->next;
00930          else
00931             protos = proto->next;
00932          return;
00933       }
00934       prev = cur;
00935       cur = cur->next;
00936    }
00937 }
00938 
00939 int ast_udptl_proto_register(struct ast_udptl_protocol *proto)
00940 {
00941    struct ast_udptl_protocol *cur;
00942 
00943    cur = protos;
00944    while (cur) {
00945       if (cur->type == proto->type) {
00946          ast_log(LOG_WARNING, "Tried to register same protocol '%s' twice\n", cur->type);
00947          return -1;
00948       }
00949       cur = cur->next;
00950    }
00951    proto->next = protos;
00952    protos = proto;
00953    return 0;
00954 }
00955 
00956 static struct ast_udptl_protocol *get_proto(struct ast_channel *chan)
00957 {
00958    struct ast_udptl_protocol *cur;
00959 
00960    cur = protos;
00961    while (cur) {
00962       if (cur->type == chan->tech->type)
00963          return cur;
00964       cur = cur->next;
00965    }
00966    return NULL;
00967 }
00968 
00969 int ast_udptl_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
00970 {
00971    struct ast_frame *f;
00972    struct ast_channel *who;
00973    struct ast_channel *cs[3];
00974    struct ast_udptl *p0;
00975    struct ast_udptl *p1;
00976    struct ast_udptl_protocol *pr0;
00977    struct ast_udptl_protocol *pr1;
00978    struct sockaddr_in ac0;
00979    struct sockaddr_in ac1;
00980    struct sockaddr_in t0;
00981    struct sockaddr_in t1;
00982    void *pvt0;
00983    void *pvt1;
00984    int to;
00985    
00986    ast_channel_lock(c0);
00987    while (ast_channel_trylock(c1)) {
00988       ast_channel_unlock(c0);
00989       usleep(1);
00990       ast_channel_lock(c0);
00991    }
00992    pr0 = get_proto(c0);
00993    pr1 = get_proto(c1);
00994    if (!pr0) {
00995       ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c0->name);
00996       ast_channel_unlock(c0);
00997       ast_channel_unlock(c1);
00998       return -1;
00999    }
01000    if (!pr1) {
01001       ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c1->name);
01002       ast_channel_unlock(c0);
01003       ast_channel_unlock(c1);
01004       return -1;
01005    }
01006    pvt0 = c0->tech_pvt;
01007    pvt1 = c1->tech_pvt;
01008    p0 = pr0->get_udptl_info(c0);
01009    p1 = pr1->get_udptl_info(c1);
01010    if (!p0 || !p1) {
01011       /* Somebody doesn't want to play... */
01012       ast_channel_unlock(c0);
01013       ast_channel_unlock(c1);
01014       return -2;
01015    }
01016    if (pr0->set_udptl_peer(c0, p1)) {
01017       ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c0->name, c1->name);
01018       memset(&ac1, 0, sizeof(ac1));
01019    } else {
01020       /* Store UDPTL peer */
01021       ast_udptl_get_peer(p1, &ac1);
01022    }
01023    if (pr1->set_udptl_peer(c1, p0)) {
01024       ast_log(LOG_WARNING, "Channel '%s' failed to talk back to '%s'\n", c1->name, c0->name);
01025       memset(&ac0, 0, sizeof(ac0));
01026    } else {
01027       /* Store UDPTL peer */
01028       ast_udptl_get_peer(p0, &ac0);
01029    }
01030    ast_channel_unlock(c0);
01031    ast_channel_unlock(c1);
01032    cs[0] = c0;
01033    cs[1] = c1;
01034    cs[2] = NULL;
01035    for (;;) {
01036       if ((c0->tech_pvt != pvt0) ||
01037          (c1->tech_pvt != pvt1) ||
01038          (c0->masq || c0->masqr || c1->masq || c1->masqr)) {
01039             ast_log(LOG_DEBUG, "Oooh, something is weird, backing out\n");
01040             /* Tell it to try again later */
01041             return -3;
01042       }
01043       to = -1;
01044       ast_udptl_get_peer(p1, &t1);
01045       ast_udptl_get_peer(p0, &t0);
01046       if (inaddrcmp(&t1, &ac1)) {
01047          ast_log(LOG_DEBUG, "Oooh, '%s' changed end address to %s:%d\n", 
01048             c1->name, ast_inet_ntoa(t1.sin_addr), ntohs(t1.sin_port));
01049          ast_log(LOG_DEBUG, "Oooh, '%s' was %s:%d\n", 
01050             c1->name, ast_inet_ntoa(ac1.sin_addr), ntohs(ac1.sin_port));
01051          memcpy(&ac1, &t1, sizeof(ac1));
01052       }
01053       if (inaddrcmp(&t0, &ac0)) {
01054          ast_log(LOG_DEBUG, "Oooh, '%s' changed end address to %s:%d\n", 
01055             c0->name, ast_inet_ntoa(t0.sin_addr), ntohs(t0.sin_port));
01056          ast_log(LOG_DEBUG, "Oooh, '%s' was %s:%d\n", 
01057             c0->name, ast_inet_ntoa(ac0.sin_addr), ntohs(ac0.sin_port));
01058          memcpy(&ac0, &t0, sizeof(ac0));
01059       }
01060       who = ast_waitfor_n(cs, 2, &to);
01061       if (!who) {
01062          ast_log(LOG_DEBUG, "Ooh, empty read...\n");
01063          /* check for hangup / whentohangup */
01064          if (ast_check_hangup(c0) || ast_check_hangup(c1))
01065             break;
01066          continue;
01067       }
01068       f = ast_read(who);
01069       if (!f) {
01070          *fo = f;
01071          *rc = who;
01072          ast_log(LOG_DEBUG, "Oooh, got a %s\n", f ? "digit" : "hangup");
01073          /* That's all we needed */
01074          return 0;
01075       } else {
01076          if (f->frametype == AST_FRAME_MODEM) {
01077             /* Forward T.38 frames if they happen upon us */
01078             if (who == c0) {
01079                ast_write(c1, f);
01080             } else if (who == c1) {
01081                ast_write(c0, f);
01082             }
01083          }
01084          ast_frfree(f);
01085       }
01086       /* Swap priority. Not that it's a big deal at this point */
01087       cs[2] = cs[0];
01088       cs[0] = cs[1];
01089       cs[1] = cs[2];
01090    }
01091    return -1;
01092 }
01093 
01094 static int udptl_do_debug_ip(int fd, int argc, char *argv[])
01095 {
01096    struct hostent *hp;
01097    struct ast_hostent ahp;
01098    int port;
01099    char *p;
01100    char *arg;
01101 
01102    port = 0;
01103    if (argc != 4)
01104       return RESULT_SHOWUSAGE;
01105    arg = argv[3];
01106    p = strstr(arg, ":");
01107    if (p) {
01108       *p = '\0';
01109       p++;
01110       port = atoi(p);
01111    }
01112    hp = ast_gethostbyname(arg, &ahp);
01113    if (hp == NULL)
01114       return RESULT_SHOWUSAGE;
01115    udptldebugaddr.sin_family = AF_INET;
01116    memcpy(&udptldebugaddr.sin_addr, hp->h_addr, sizeof(udptldebugaddr.sin_addr));
01117    udptldebugaddr.sin_port = htons(port);
01118    if (port == 0)
01119       ast_cli(fd, "UDPTL Debugging Enabled for IP: %s\n", ast_inet_ntoa(udptldebugaddr.sin_addr));
01120    else
01121       ast_cli(fd, "UDPTL Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(udptldebugaddr.sin_addr), port);
01122    udptldebug = 1;
01123    return RESULT_SUCCESS;
01124 }
01125 
01126 static int udptl_do_debug(int fd, int argc, char *argv[])
01127 {
01128    if (argc != 2) {
01129       if (argc != 4)
01130          return RESULT_SHOWUSAGE;
01131       return udptl_do_debug_ip(fd, argc, argv);
01132    }
01133    udptldebug = 1;
01134    memset(&udptldebugaddr,0,sizeof(udptldebugaddr));
01135    ast_cli(fd, "UDPTL Debugging Enabled\n");
01136    return RESULT_SUCCESS;
01137 }
01138 
01139 static int udptl_nodebug(int fd, int argc, char *argv[])
01140 {
01141    if (argc != 3)
01142       return RESULT_SHOWUSAGE;
01143    udptldebug = 0;
01144    ast_cli(fd,"UDPTL Debugging Disabled\n");
01145    return RESULT_SUCCESS;
01146 }
01147 
01148 static char debug_usage[] =
01149   "Usage: udptl debug [ip host[:port]]\n"
01150   "       Enable dumping of all UDPTL packets to and from host.\n";
01151 
01152 static char nodebug_usage[] =
01153   "Usage: udptl debug off\n"
01154   "       Disable all UDPTL debugging\n";
01155 
01156 static struct ast_cli_entry cli_udptl_no_debug = {
01157    { "udptl", "no", "debug", NULL },
01158    udptl_nodebug, NULL,
01159    NULL };
01160 
01161 static struct ast_cli_entry cli_udptl[] = {
01162    { { "udptl", "debug", NULL },
01163    udptl_do_debug, "Enable UDPTL debugging",
01164    debug_usage },
01165 
01166    { { "udptl", "debug", "ip", NULL },
01167    udptl_do_debug, "Enable UDPTL debugging on IP",
01168    debug_usage },
01169 
01170    { { "udptl", "debug", "off", NULL },
01171    udptl_nodebug, "Disable UDPTL debugging",
01172    nodebug_usage, NULL, &cli_udptl_no_debug },
01173 };
01174 
01175 void ast_udptl_reload(void)
01176 {
01177    struct ast_config *cfg;
01178    const char *s;
01179 
01180    udptlstart = 4500;
01181    udptlend = 4999;
01182    udptlfectype = 0;
01183    udptlfecentries = 0;
01184    udptlfecspan = 0;
01185    udptlmaxdatagram = 0;
01186 
01187    if ((cfg = ast_config_load("udptl.conf"))) {
01188       if ((s = ast_variable_retrieve(cfg, "general", "udptlstart"))) {
01189          udptlstart = atoi(s);
01190          if (udptlstart < 1024) {
01191             ast_log(LOG_WARNING, "Ports under 1024 are not allowed for T.38.\n");
01192             udptlstart = 1024;
01193          }
01194          if (udptlstart > 65535) {
01195             ast_log(LOG_WARNING, "Ports over 65535 are invalid.\n");
01196             udptlstart = 65535;
01197          }
01198       }
01199       if ((s = ast_variable_retrieve(cfg, "general", "udptlend"))) {
01200          udptlend = atoi(s);
01201          if (udptlend < 1024) {
01202             ast_log(LOG_WARNING, "Ports under 1024 are not allowed for T.38.\n");
01203             udptlend = 1024;
01204          }
01205          if (udptlend > 65535) {
01206             ast_log(LOG_WARNING, "Ports over 65535 are invalid.\n");
01207             udptlend = 65535;
01208          }
01209       }
01210       if ((s = ast_variable_retrieve(cfg, "general", "udptlchecksums"))) {
01211 #ifdef SO_NO_CHECK
01212          if (ast_false(s))
01213             nochecksums = 1;
01214          else
01215             nochecksums = 0;
01216 #else
01217          if (ast_false(s))
01218             ast_log(LOG_WARNING, "Disabling UDPTL checksums is not supported on this operating system!\n");
01219 #endif
01220       }
01221       if ((s = ast_variable_retrieve(cfg, "general", "T38FaxUdpEC"))) {
01222          if (strcmp(s, "t38UDPFEC") == 0)
01223             udptlfectype = 2;
01224          else if (strcmp(s, "t38UDPRedundancy") == 0)
01225             udptlfectype = 1;
01226       }
01227       if ((s = ast_variable_retrieve(cfg, "general", "T38FaxMaxDatagram"))) {
01228          udptlmaxdatagram = atoi(s);
01229          if (udptlmaxdatagram < 100) {
01230             ast_log(LOG_WARNING, "Too small T38FaxMaxDatagram size.  Defaulting to 100.\n");
01231             udptlmaxdatagram = 100;
01232          }
01233          if (udptlmaxdatagram > LOCAL_FAX_MAX_DATAGRAM) {
01234             ast_log(LOG_WARNING, "Too large T38FaxMaxDatagram size.  Defaulting to %d.\n", LOCAL_FAX_MAX_DATAGRAM);
01235             udptlmaxdatagram = LOCAL_FAX_MAX_DATAGRAM;
01236          }
01237       }
01238       if ((s = ast_variable_retrieve(cfg, "general", "UDPTLFECentries"))) {
01239          udptlfecentries = atoi(s);
01240          if (udptlfecentries < 1) {
01241             ast_log(LOG_WARNING, "Too small UDPTLFECentries value.  Defaulting to 1.\n");
01242             udptlfecentries = 1;
01243          }
01244          if (udptlfecentries > MAX_FEC_ENTRIES) {
01245             ast_log(LOG_WARNING, "Too large UDPTLFECentries value.  Defaulting to %d.\n", MAX_FEC_ENTRIES);
01246             udptlfecentries = MAX_FEC_ENTRIES;
01247          }
01248       }
01249       if ((s = ast_variable_retrieve(cfg, "general", "UDPTLFECspan"))) {
01250          udptlfecspan = atoi(s);
01251          if (udptlfecspan < 1) {
01252             ast_log(LOG_WARNING, "Too small UDPTLFECspan value.  Defaulting to 1.\n");
01253             udptlfecspan = 1;
01254          }
01255          if (udptlfecspan > MAX_FEC_SPAN) {
01256             ast_log(LOG_WARNING, "Too large UDPTLFECspan value.  Defaulting to %d.\n", MAX_FEC_SPAN);
01257             udptlfecspan = MAX_FEC_SPAN;
01258          }
01259       }
01260       ast_config_destroy(cfg);
01261    }
01262    if (udptlstart >= udptlend) {
01263       ast_log(LOG_WARNING, "Unreasonable values for UDPTL start/end\n");
01264       udptlstart = 4500;
01265       udptlend = 4999;
01266    }
01267    if (option_verbose > 1)
01268       ast_verbose(VERBOSE_PREFIX_2 "UDPTL allocating from port range %d -> %d\n", udptlstart, udptlend);
01269 }
01270 
01271 void ast_udptl_init(void)
01272 {
01273    ast_cli_register_multiple(cli_udptl, sizeof(cli_udptl) / sizeof(struct ast_cli_entry));
01274    ast_udptl_reload();
01275 }

Generated on Fri Sep 11 13:45:03 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7