Fri Sep 25 19:28:45 2009

Asterisk developer's documentation


udptl.c File Reference

#include "asterisk.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include "asterisk/udptl.h"
#include "asterisk/frame.h"
#include "asterisk/logger.h"
#include "asterisk/options.h"
#include "asterisk/channel.h"
#include "asterisk/acl.h"
#include "asterisk/config.h"
#include "asterisk/lock.h"
#include "asterisk/utils.h"
#include "asterisk/cli.h"
#include "asterisk/unaligned.h"

Include dependency graph for udptl.c:

Go to the source code of this file.

Data Structures

struct  ast_udptl
struct  udptl_fec_rx_buffer_t
struct  udptl_fec_tx_buffer_t

Defines

#define FALSE   0
#define LOCAL_FAX_MAX_DATAGRAM   400
#define MAX_FEC_ENTRIES   5
#define MAX_FEC_SPAN   5
#define TRUE   (!FALSE)
#define UDPTL_BUF_MASK   15
#define UDPTL_MTU   1200

Functions

int ast_udptl_bridge (struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
void ast_udptl_destroy (struct ast_udptl *udptl)
int ast_udptl_fd (struct ast_udptl *udptl)
int ast_udptl_get_error_correction_scheme (struct ast_udptl *udptl)
int ast_udptl_get_far_max_datagram (struct ast_udptl *udptl)
int ast_udptl_get_local_max_datagram (struct ast_udptl *udptl)
void ast_udptl_get_peer (struct ast_udptl *udptl, struct sockaddr_in *them)
void ast_udptl_get_us (struct ast_udptl *udptl, struct sockaddr_in *us)
void ast_udptl_init (void)
struct ast_udptlast_udptl_new (struct sched_context *sched, struct io_context *io, int callbackmode)
struct ast_udptlast_udptl_new_with_bindaddr (struct sched_context *sched, struct io_context *io, int callbackmode, struct in_addr addr)
void ast_udptl_offered_from_local (struct ast_udptl *udptl, int local)
int ast_udptl_proto_register (struct ast_udptl_protocol *proto)
void ast_udptl_proto_unregister (struct ast_udptl_protocol *proto)
struct ast_frameast_udptl_read (struct ast_udptl *udptl)
void ast_udptl_reload (void)
void ast_udptl_set_callback (struct ast_udptl *udptl, ast_udptl_callback callback)
void ast_udptl_set_data (struct ast_udptl *udptl, void *data)
void ast_udptl_set_error_correction_scheme (struct ast_udptl *udptl, int ec)
void ast_udptl_set_far_max_datagram (struct ast_udptl *udptl, int max_datagram)
void ast_udptl_set_local_max_datagram (struct ast_udptl *udptl, int max_datagram)
void ast_udptl_set_peer (struct ast_udptl *udptl, struct sockaddr_in *them)
void ast_udptl_setnat (struct ast_udptl *udptl, int nat)
int ast_udptl_settos (struct ast_udptl *udptl, int tos)
void ast_udptl_stop (struct ast_udptl *udptl)
int ast_udptl_write (struct ast_udptl *s, struct ast_frame *f)
static int decode_length (uint8_t *buf, int limit, int *len, int *pvalue)
static int decode_open_type (uint8_t *buf, int limit, int *len, const uint8_t **p_object, int *p_num_octets)
static int encode_length (uint8_t *buf, int *len, int value)
static int encode_open_type (uint8_t *buf, int *len, const uint8_t *data, int num_octets)
static struct ast_udptl_protocolget_proto (struct ast_channel *chan)
static int udptl_build_packet (struct ast_udptl *s, uint8_t *buf, uint8_t *ifp, int ifp_len)
static int udptl_debug_test_addr (struct sockaddr_in *addr)
static int udptl_do_debug (int fd, int argc, char *argv[])
static int udptl_do_debug_ip (int fd, int argc, char *argv[])
static int udptl_nodebug (int fd, int argc, char *argv[])
static int udptl_rx_packet (struct ast_udptl *s, uint8_t *buf, int len)
static int udptlread (int *id, int fd, short events, void *cbdata)

Variables

static struct ast_cli_entry cli_udptl []
static struct ast_cli_entry cli_udptl_no_debug
static char debug_usage []
static char nodebug_usage []
static struct ast_udptl_protocolprotos
static int udptldebug
static struct sockaddr_in udptldebugaddr
static int udptlend
static int udptlfecentries
static int udptlfecspan
static int udptlfectype
static int udptlmaxdatagram
static int udptlstart


Define Documentation

#define FALSE   0

Definition at line 52 of file udptl.c.

#define LOCAL_FAX_MAX_DATAGRAM   400

Definition at line 70 of file udptl.c.

Referenced by ast_udptl_reload(), ast_udptl_write(), udptl_build_packet(), and udptl_rx_packet().

#define MAX_FEC_ENTRIES   5

Definition at line 71 of file udptl.c.

Referenced by ast_udptl_reload().

#define MAX_FEC_SPAN   5

Definition at line 72 of file udptl.c.

Referenced by ast_udptl_reload().

#define TRUE   (!FALSE)

Definition at line 55 of file udptl.c.

#define UDPTL_BUF_MASK   15

Definition at line 74 of file udptl.c.

Referenced by ast_udptl_new_with_bindaddr(), udptl_build_packet(), and udptl_rx_packet().

#define UDPTL_MTU   1200

Definition at line 49 of file udptl.c.


Function Documentation

int ast_udptl_bridge ( struct ast_channel c0,
struct ast_channel c1,
int  flags,
struct ast_frame **  fo,
struct ast_channel **  rc 
)

Definition at line 963 of file udptl.c.

References ast_channel_lock, ast_channel_trylock, ast_channel_unlock, ast_check_hangup(), AST_FRAME_MODEM, ast_frfree, ast_inet_ntoa(), ast_log(), ast_read(), ast_udptl_get_peer(), ast_waitfor_n(), ast_write(), f, ast_frame::frametype, get_proto(), ast_udptl_protocol::get_udptl_info, inaddrcmp(), LOG_DEBUG, LOG_WARNING, ast_channel::masq, ast_channel::masqr, ast_udptl_protocol::set_udptl_peer, and ast_channel::tech_pvt.

00964 {
00965    struct ast_frame *f;
00966    struct ast_channel *who;
00967    struct ast_channel *cs[3];
00968    struct ast_udptl *p0;
00969    struct ast_udptl *p1;
00970    struct ast_udptl_protocol *pr0;
00971    struct ast_udptl_protocol *pr1;
00972    struct sockaddr_in ac0;
00973    struct sockaddr_in ac1;
00974    struct sockaddr_in t0;
00975    struct sockaddr_in t1;
00976    void *pvt0;
00977    void *pvt1;
00978    int to;
00979    
00980    ast_channel_lock(c0);
00981    while (ast_channel_trylock(c1)) {
00982       ast_channel_unlock(c0);
00983       usleep(1);
00984       ast_channel_lock(c0);
00985    }
00986    pr0 = get_proto(c0);
00987    pr1 = get_proto(c1);
00988    if (!pr0) {
00989       ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c0->name);
00990       ast_channel_unlock(c0);
00991       ast_channel_unlock(c1);
00992       return -1;
00993    }
00994    if (!pr1) {
00995       ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c1->name);
00996       ast_channel_unlock(c0);
00997       ast_channel_unlock(c1);
00998       return -1;
00999    }
01000    pvt0 = c0->tech_pvt;
01001    pvt1 = c1->tech_pvt;
01002    p0 = pr0->get_udptl_info(c0);
01003    p1 = pr1->get_udptl_info(c1);
01004    if (!p0 || !p1) {
01005       /* Somebody doesn't want to play... */
01006       ast_channel_unlock(c0);
01007       ast_channel_unlock(c1);
01008       return -2;
01009    }
01010    if (pr0->set_udptl_peer(c0, p1)) {
01011       ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c0->name, c1->name);
01012    } else {
01013       /* Store UDPTL peer */
01014       ast_udptl_get_peer(p1, &ac1);
01015    }
01016    if (pr1->set_udptl_peer(c1, p0))
01017       ast_log(LOG_WARNING, "Channel '%s' failed to talk back to '%s'\n", c1->name, c0->name);
01018    else {
01019       /* Store UDPTL peer */
01020       ast_udptl_get_peer(p0, &ac0);
01021    }
01022    ast_channel_unlock(c0);
01023    ast_channel_unlock(c1);
01024    cs[0] = c0;
01025    cs[1] = c1;
01026    cs[2] = NULL;
01027    for (;;) {
01028       if ((c0->tech_pvt != pvt0) ||
01029          (c1->tech_pvt != pvt1) ||
01030          (c0->masq || c0->masqr || c1->masq || c1->masqr)) {
01031             ast_log(LOG_DEBUG, "Oooh, something is weird, backing out\n");
01032             /* Tell it to try again later */
01033             return -3;
01034       }
01035       to = -1;
01036       ast_udptl_get_peer(p1, &t1);
01037       ast_udptl_get_peer(p0, &t0);
01038       if (inaddrcmp(&t1, &ac1)) {
01039          ast_log(LOG_DEBUG, "Oooh, '%s' changed end address to %s:%d\n", 
01040             c1->name, ast_inet_ntoa(t1.sin_addr), ntohs(t1.sin_port));
01041          ast_log(LOG_DEBUG, "Oooh, '%s' was %s:%d\n", 
01042             c1->name, ast_inet_ntoa(ac1.sin_addr), ntohs(ac1.sin_port));
01043          memcpy(&ac1, &t1, sizeof(ac1));
01044       }
01045       if (inaddrcmp(&t0, &ac0)) {
01046          ast_log(LOG_DEBUG, "Oooh, '%s' changed end address to %s:%d\n", 
01047             c0->name, ast_inet_ntoa(t0.sin_addr), ntohs(t0.sin_port));
01048          ast_log(LOG_DEBUG, "Oooh, '%s' was %s:%d\n", 
01049             c0->name, ast_inet_ntoa(ac0.sin_addr), ntohs(ac0.sin_port));
01050          memcpy(&ac0, &t0, sizeof(ac0));
01051       }
01052       who = ast_waitfor_n(cs, 2, &to);
01053       if (!who) {
01054          ast_log(LOG_DEBUG, "Ooh, empty read...\n");
01055          /* check for hangup / whentohangup */
01056          if (ast_check_hangup(c0) || ast_check_hangup(c1))
01057             break;
01058          continue;
01059       }
01060       f = ast_read(who);
01061       if (!f) {
01062          *fo = f;
01063          *rc = who;
01064          ast_log(LOG_DEBUG, "Oooh, got a %s\n", f ? "digit" : "hangup");
01065          /* That's all we needed */
01066          return 0;
01067       } else {
01068          if (f->frametype == AST_FRAME_MODEM) {
01069             /* Forward T.38 frames if they happen upon us */
01070             if (who == c0) {
01071                ast_write(c1, f);
01072             } else if (who == c1) {
01073                ast_write(c0, f);
01074             }
01075          }
01076          ast_frfree(f);
01077       }
01078       /* Swap priority. Not that it's a big deal at this point */
01079       cs[2] = cs[0];
01080       cs[0] = cs[1];
01081       cs[1] = cs[2];
01082    }
01083    return -1;
01084 }

void ast_udptl_destroy ( struct ast_udptl udptl  ) 

Definition at line 863 of file udptl.c.

References ast_io_remove(), ast_udptl::fd, free, ast_udptl::io, and ast_udptl::ioid.

Referenced by __sip_destroy(), and create_addr_from_peer().

00864 {
00865    if (udptl->ioid)
00866       ast_io_remove(udptl->io, udptl->ioid);
00867    if (udptl->fd > -1)
00868       close(udptl->fd);
00869    free(udptl);
00870 }

int ast_udptl_fd ( struct ast_udptl udptl  ) 

Definition at line 589 of file udptl.c.

References ast_udptl::fd.

Referenced by __oh323_new(), and sip_new().

00590 {
00591    return udptl->fd;
00592 }

int ast_udptl_get_error_correction_scheme ( struct ast_udptl udptl  ) 

Definition at line 681 of file udptl.c.

References ast_log(), ast_udptl::error_correction_scheme, and LOG_WARNING.

Referenced by create_addr_from_peer(), sip_alloc(), and sip_handle_t38_reinvite().

00682 {
00683    if (udptl)
00684       return udptl->error_correction_scheme;
00685    else {
00686       ast_log(LOG_WARNING, "udptl structure is null\n");
00687       return -1;
00688    }
00689 }

int ast_udptl_get_far_max_datagram ( struct ast_udptl udptl  ) 

Definition at line 721 of file udptl.c.

References ast_log(), ast_udptl::far_max_datagram_size, and LOG_WARNING.

00722 {
00723    if (udptl)
00724       return udptl->far_max_datagram_size;
00725    else {
00726       ast_log(LOG_WARNING, "udptl structure is null\n");
00727       return -1;
00728    }
00729 }

int ast_udptl_get_local_max_datagram ( struct ast_udptl udptl  ) 

Definition at line 711 of file udptl.c.

References ast_log(), ast_udptl::local_max_datagram_size, and LOG_WARNING.

Referenced by add_t38_sdp(), and sip_handle_t38_reinvite().

00712 {
00713    if (udptl)
00714       return udptl->local_max_datagram_size;
00715    else {
00716       ast_log(LOG_WARNING, "udptl structure is null\n");
00717       return -1;
00718    }
00719 }

void ast_udptl_get_peer ( struct ast_udptl udptl,
struct sockaddr_in *  them 
)

Definition at line 845 of file udptl.c.

References ast_udptl::them.

Referenced by ast_udptl_bridge(), sip_handle_t38_reinvite(), and sip_set_udptl_peer().

00846 {
00847    them->sin_family = AF_INET;
00848    them->sin_port = udptl->them.sin_port;
00849    them->sin_addr = udptl->them.sin_addr;
00850 }

void ast_udptl_get_us ( struct ast_udptl udptl,
struct sockaddr_in *  us 
)

Definition at line 852 of file udptl.c.

References ast_udptl::us.

Referenced by add_t38_sdp().

00853 {
00854    memcpy(us, &udptl->us, sizeof(udptl->us));
00855 }

void ast_udptl_init ( void   ) 

Definition at line 1243 of file udptl.c.

References ast_cli_register_multiple(), and ast_udptl_reload().

Referenced by main().

01244 {
01245    ast_cli_register_multiple(cli_udptl, sizeof(cli_udptl) / sizeof(struct ast_cli_entry));
01246    ast_udptl_reload();
01247 }

struct ast_udptl* ast_udptl_new ( struct sched_context sched,
struct io_context io,
int  callbackmode 
) [read]

Definition at line 823 of file udptl.c.

References ast_udptl_new_with_bindaddr().

00824 {
00825    struct in_addr ia;
00826    memset(&ia, 0, sizeof(ia));
00827    return ast_udptl_new_with_bindaddr(sched, io, callbackmode, ia);
00828 }

struct ast_udptl* ast_udptl_new_with_bindaddr ( struct sched_context sched,
struct io_context io,
int  callbackmode,
struct in_addr  addr 
) [read]

Definition at line 747 of file udptl.c.

References ast_calloc, ast_io_add(), AST_IO_IN, ast_log(), ast_random(), udptl_fec_tx_buffer_t::buf_len, udptl_fec_rx_buffer_t::buf_len, errno, ast_udptl::error_correction_entries, ast_udptl::error_correction_scheme, ast_udptl::error_correction_span, ast_udptl::far_max_datagram_size, ast_udptl::fd, ast_udptl::flags, free, ast_udptl::io, ast_udptl::ioid, ast_udptl::local_max_datagram_size, LOG_WARNING, ast_udptl::rx, ast_udptl::sched, ast_udptl::them, ast_udptl::tx, UDPTL_BUF_MASK, UDPTL_ERROR_CORRECTION_FEC, UDPTL_ERROR_CORRECTION_NONE, UDPTL_ERROR_CORRECTION_REDUNDANCY, udptlread(), and ast_udptl::us.

Referenced by ast_udptl_new(), and sip_alloc().

00748 {
00749    struct ast_udptl *udptl;
00750    int x;
00751    int startplace;
00752    int i;
00753    long int flags;
00754 
00755    if (!(udptl = ast_calloc(1, sizeof(*udptl))))
00756       return NULL;
00757 
00758    if (udptlfectype == 2)
00759       udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_FEC;
00760    else if (udptlfectype == 1)
00761       udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_REDUNDANCY;
00762    else
00763       udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_NONE;
00764    udptl->error_correction_span = udptlfecspan;
00765    udptl->error_correction_entries = udptlfecentries;
00766    
00767    udptl->far_max_datagram_size = udptlmaxdatagram;
00768    udptl->local_max_datagram_size = udptlmaxdatagram;
00769 
00770    memset(&udptl->rx, 0, sizeof(udptl->rx));
00771    memset(&udptl->tx, 0, sizeof(udptl->tx));
00772    for (i = 0; i <= UDPTL_BUF_MASK; i++) {
00773       udptl->rx[i].buf_len = -1;
00774       udptl->tx[i].buf_len = -1;
00775    }
00776 
00777    udptl->them.sin_family = AF_INET;
00778    udptl->us.sin_family = AF_INET;
00779 
00780    if ((udptl->fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
00781       free(udptl);
00782       ast_log(LOG_WARNING, "Unable to allocate socket: %s\n", strerror(errno));
00783       return NULL;
00784    }
00785    flags = fcntl(udptl->fd, F_GETFL);
00786    fcntl(udptl->fd, F_SETFL, flags | O_NONBLOCK);
00787 #ifdef SO_NO_CHECK
00788    if (nochecksums)
00789       setsockopt(udptl->fd, SOL_SOCKET, SO_NO_CHECK, &nochecksums, sizeof(nochecksums));
00790 #endif
00791    /* Find us a place */
00792    x = (ast_random() % (udptlend - udptlstart)) + udptlstart;
00793    startplace = x;
00794    for (;;) {
00795       udptl->us.sin_port = htons(x);
00796       udptl->us.sin_addr = addr;
00797       if (bind(udptl->fd, (struct sockaddr *) &udptl->us, sizeof(udptl->us)) == 0)
00798          break;
00799       if (errno != EADDRINUSE) {
00800          ast_log(LOG_WARNING, "Unexpected bind error: %s\n", strerror(errno));
00801          close(udptl->fd);
00802          free(udptl);
00803          return NULL;
00804       }
00805       if (++x > udptlend)
00806          x = udptlstart;
00807       if (x == startplace) {
00808          ast_log(LOG_WARNING, "No UDPTL ports remaining\n");
00809          close(udptl->fd);
00810          free(udptl);
00811          return NULL;
00812       }
00813    }
00814    if (io && sched && callbackmode) {
00815       /* Operate this one in a callback mode */
00816       udptl->sched = sched;
00817       udptl->io = io;
00818       udptl->ioid = ast_io_add(udptl->io, udptl->fd, udptlread, AST_IO_IN, udptl);
00819    }
00820    return udptl;
00821 }

void ast_udptl_offered_from_local ( struct ast_udptl udptl,
int  local 
)

Definition at line 673 of file udptl.c.

References ast_log(), LOG_WARNING, and ast_udptl::udptl_offered_from_local.

Referenced by transmit_invite(), transmit_reinvite_with_t38_sdp(), and transmit_response_with_t38_sdp().

00674 {
00675    if (udptl)
00676       udptl->udptl_offered_from_local = local;
00677    else
00678       ast_log(LOG_WARNING, "udptl structure is null\n");
00679 }

int ast_udptl_proto_register ( struct ast_udptl_protocol proto  ) 

Definition at line 933 of file udptl.c.

References ast_log(), LOG_WARNING, ast_udptl_protocol::next, and ast_udptl_protocol::type.

Referenced by load_module().

00934 {
00935    struct ast_udptl_protocol *cur;
00936 
00937    cur = protos;
00938    while (cur) {
00939       if (cur->type == proto->type) {
00940          ast_log(LOG_WARNING, "Tried to register same protocol '%s' twice\n", cur->type);
00941          return -1;
00942       }
00943       cur = cur->next;
00944    }
00945    proto->next = protos;
00946    protos = proto;
00947    return 0;
00948 }

void ast_udptl_proto_unregister ( struct ast_udptl_protocol proto  ) 

Definition at line 913 of file udptl.c.

References ast_udptl_protocol::next.

Referenced by unload_module().

00914 {
00915    struct ast_udptl_protocol *cur;
00916    struct ast_udptl_protocol *prev;
00917 
00918    cur = protos;
00919    prev = NULL;
00920    while (cur) {
00921       if (cur == proto) {
00922          if (prev)
00923             prev->next = proto->next;
00924          else
00925             protos = proto->next;
00926          return;
00927       }
00928       prev = cur;
00929       cur = cur->next;
00930    }
00931 }

struct ast_frame* ast_udptl_read ( struct ast_udptl udptl  )  [read]

Definition at line 621 of file udptl.c.

References AST_FRIENDLY_OFFSET, ast_inet_ntoa(), ast_log(), ast_null_frame, ast_verbose(), CRASH, errno, ast_udptl::f, ast_udptl::fd, len, LOG_DEBUG, LOG_WARNING, ast_udptl::nat, ast_udptl::rawdata, ast_udptl::them, udptl_debug_test_addr(), and udptl_rx_packet().

Referenced by sip_rtp_read(), skinny_rtp_read(), and udptlread().

00622 {
00623    int res;
00624    struct sockaddr_in sin;
00625    socklen_t len;
00626    uint16_t seqno = 0;
00627    uint16_t *udptlheader;
00628 
00629    len = sizeof(sin);
00630    
00631    /* Cache where the header will go */
00632    res = recvfrom(udptl->fd,
00633          udptl->rawdata + AST_FRIENDLY_OFFSET,
00634          sizeof(udptl->rawdata) - AST_FRIENDLY_OFFSET,
00635          0,
00636          (struct sockaddr *) &sin,
00637          &len);
00638    udptlheader = (uint16_t *)(udptl->rawdata + AST_FRIENDLY_OFFSET);
00639    if (res < 0) {
00640       if (errno != EAGAIN)
00641          ast_log(LOG_WARNING, "UDPTL read error: %s\n", strerror(errno));
00642       if (errno == EBADF)
00643          CRASH;
00644       return &ast_null_frame;
00645    }
00646 
00647    /* Ignore if the other side hasn't been given an address yet. */
00648    if (!udptl->them.sin_addr.s_addr || !udptl->them.sin_port)
00649       return &ast_null_frame;
00650 
00651    if (udptl->nat) {
00652       /* Send to whoever sent to us */
00653       if ((udptl->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
00654          (udptl->them.sin_port != sin.sin_port)) {
00655          memcpy(&udptl->them, &sin, sizeof(udptl->them));
00656          ast_log(LOG_DEBUG, "UDPTL NAT: Using address %s:%d\n", ast_inet_ntoa(udptl->them.sin_addr), ntohs(udptl->them.sin_port));
00657       }
00658    }
00659 
00660    if (udptl_debug_test_addr(&sin)) {
00661       ast_verbose("Got UDPTL packet from %s:%d (type %d, seq %d, len %d)\n",
00662          ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), 0, seqno, res);
00663    }
00664 #if 0
00665    printf("Got UDPTL packet from %s:%d (seq %d, len = %d)\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), seqno, res);
00666 #endif
00667    if (udptl_rx_packet(udptl, udptl->rawdata + AST_FRIENDLY_OFFSET, res) < 1)
00668       return &ast_null_frame;
00669 
00670    return &udptl->f[0];
00671 }

void ast_udptl_reload ( void   ) 

Definition at line 1167 of file udptl.c.

References ast_config_destroy(), ast_config_load(), ast_false(), ast_log(), ast_variable_retrieve(), ast_verbose(), LOCAL_FAX_MAX_DATAGRAM, LOG_WARNING, MAX_FEC_ENTRIES, MAX_FEC_SPAN, option_verbose, s, and VERBOSE_PREFIX_2.

Referenced by ast_udptl_init().

01168 {
01169    struct ast_config *cfg;
01170    const char *s;
01171 
01172    udptlstart = 4500;
01173    udptlend = 4999;
01174    udptlfectype = 0;
01175    udptlfecentries = 0;
01176    udptlfecspan = 0;
01177    udptlmaxdatagram = 0;
01178 
01179    if ((cfg = ast_config_load("udptl.conf"))) {
01180       if ((s = ast_variable_retrieve(cfg, "general", "udptlstart"))) {
01181          udptlstart = atoi(s);
01182          if (udptlstart < 1024)
01183             udptlstart = 1024;
01184          if (udptlstart > 65535)
01185             udptlstart = 65535;
01186       }
01187       if ((s = ast_variable_retrieve(cfg, "general", "udptlend"))) {
01188          udptlend = atoi(s);
01189          if (udptlend < 1024)
01190             udptlend = 1024;
01191          if (udptlend > 65535)
01192             udptlend = 65535;
01193       }
01194       if ((s = ast_variable_retrieve(cfg, "general", "udptlchecksums"))) {
01195 #ifdef SO_NO_CHECK
01196          if (ast_false(s))
01197             nochecksums = 1;
01198          else
01199             nochecksums = 0;
01200 #else
01201          if (ast_false(s))
01202             ast_log(LOG_WARNING, "Disabling UDPTL checksums is not supported on this operating system!\n");
01203 #endif
01204       }
01205       if ((s = ast_variable_retrieve(cfg, "general", "T38FaxUdpEC"))) {
01206          if (strcmp(s, "t38UDPFEC") == 0)
01207             udptlfectype = 2;
01208          else if (strcmp(s, "t38UDPRedundancy") == 0)
01209             udptlfectype = 1;
01210       }
01211       if ((s = ast_variable_retrieve(cfg, "general", "T38FaxMaxDatagram"))) {
01212          udptlmaxdatagram = atoi(s);
01213          if (udptlmaxdatagram < 0)
01214             udptlmaxdatagram = 0;
01215          if (udptlmaxdatagram > LOCAL_FAX_MAX_DATAGRAM)
01216             udptlmaxdatagram = LOCAL_FAX_MAX_DATAGRAM;
01217       }
01218       if ((s = ast_variable_retrieve(cfg, "general", "UDPTLFECentries"))) {
01219          udptlfecentries = atoi(s);
01220          if (udptlfecentries < 0)
01221             udptlfecentries = 0;
01222          if (udptlfecentries > MAX_FEC_ENTRIES)
01223             udptlfecentries = MAX_FEC_ENTRIES;
01224       }
01225       if ((s = ast_variable_retrieve(cfg, "general", "UDPTLFECspan"))) {
01226          udptlfecspan = atoi(s);
01227          if (udptlfecspan < 0)
01228             udptlfecspan = 0;
01229          if (udptlfecspan > MAX_FEC_SPAN)
01230             udptlfecspan = MAX_FEC_SPAN;
01231       }
01232       ast_config_destroy(cfg);
01233    }
01234    if (udptlstart >= udptlend) {
01235       ast_log(LOG_WARNING, "Unreasonable values for UDPTL start/end\n");
01236       udptlstart = 4500;
01237       udptlend = 4999;
01238    }
01239    if (option_verbose > 1)
01240       ast_verbose(VERBOSE_PREFIX_2 "UDPTL allocating from port range %d -> %d\n", udptlstart, udptlend);
01241 }

void ast_udptl_set_callback ( struct ast_udptl udptl,
ast_udptl_callback  callback 
)

Definition at line 599 of file udptl.c.

References ast_udptl::callback.

00600 {
00601    udptl->callback = callback;
00602 }

void ast_udptl_set_data ( struct ast_udptl udptl,
void *  data 
)

Definition at line 594 of file udptl.c.

References ast_udptl::data.

00595 {
00596    udptl->data = data;
00597 }

void ast_udptl_set_error_correction_scheme ( struct ast_udptl udptl,
int  ec 
)

Definition at line 691 of file udptl.c.

References ast_log(), ast_udptl::error_correction_scheme, LOG_WARNING, UDPTL_ERROR_CORRECTION_FEC, UDPTL_ERROR_CORRECTION_NONE, and UDPTL_ERROR_CORRECTION_REDUNDANCY.

Referenced by process_sdp(), and sip_handle_t38_reinvite().

00692 {
00693    if (udptl) {
00694       switch (ec) {
00695       case UDPTL_ERROR_CORRECTION_FEC:
00696          udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_FEC;
00697          break;
00698       case UDPTL_ERROR_CORRECTION_REDUNDANCY:
00699          udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_REDUNDANCY;
00700          break;
00701       case UDPTL_ERROR_CORRECTION_NONE:
00702          udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_NONE;
00703          break;
00704       default:
00705          ast_log(LOG_WARNING, "error correction parameter invalid\n");
00706       };
00707    } else
00708       ast_log(LOG_WARNING, "udptl structure is null\n");
00709 }

void ast_udptl_set_far_max_datagram ( struct ast_udptl udptl,
int  max_datagram 
)

Definition at line 739 of file udptl.c.

References ast_log(), ast_udptl::far_max_datagram_size, and LOG_WARNING.

Referenced by process_sdp(), and sip_handle_t38_reinvite().

00740 {
00741    if (udptl)
00742       udptl->far_max_datagram_size = max_datagram;
00743    else
00744       ast_log(LOG_WARNING, "udptl structure is null\n");
00745 }

void ast_udptl_set_local_max_datagram ( struct ast_udptl udptl,
int  max_datagram 
)

Definition at line 731 of file udptl.c.

References ast_log(), ast_udptl::local_max_datagram_size, and LOG_WARNING.

Referenced by process_sdp(), and sip_handle_t38_reinvite().

00732 {
00733    if (udptl)
00734       udptl->local_max_datagram_size = max_datagram;
00735    else
00736       ast_log(LOG_WARNING, "udptl structure is null\n");
00737 }

void ast_udptl_set_peer ( struct ast_udptl udptl,
struct sockaddr_in *  them 
)

Definition at line 839 of file udptl.c.

References ast_udptl::them.

Referenced by process_sdp().

00840 {
00841    udptl->them.sin_port = them->sin_port;
00842    udptl->them.sin_addr = them->sin_addr;
00843 }

void ast_udptl_setnat ( struct ast_udptl udptl,
int  nat 
)

Definition at line 604 of file udptl.c.

References ast_udptl::nat.

Referenced by do_setnat().

00605 {
00606    udptl->nat = nat;
00607 }

int ast_udptl_settos ( struct ast_udptl udptl,
int  tos 
)

Definition at line 830 of file udptl.c.

References ast_log(), ast_udptl::fd, and LOG_WARNING.

Referenced by sip_alloc().

00831 {
00832    int res;
00833 
00834    if ((res = setsockopt(udptl->fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)))) 
00835       ast_log(LOG_WARNING, "UDPTL unable to set TOS to %d\n", tos);
00836    return res;
00837 }

void ast_udptl_stop ( struct ast_udptl udptl  ) 

Definition at line 857 of file udptl.c.

References ast_udptl::them.

Referenced by process_sdp(), and stop_media_flows().

00858 {
00859    memset(&udptl->them.sin_addr, 0, sizeof(udptl->them.sin_addr));
00860    memset(&udptl->them.sin_port, 0, sizeof(udptl->them.sin_port));
00861 }

int ast_udptl_write ( struct ast_udptl s,
struct ast_frame f 
)

Definition at line 872 of file udptl.c.

References AST_FRAME_MODEM, ast_inet_ntoa(), ast_log(), ast_verbose(), ast_frame::data, ast_frame::datalen, errno, ast_udptl::fd, ast_frame::frametype, len, LOCAL_FAX_MAX_DATAGRAM, LOG_NOTICE, LOG_WARNING, seq, ast_udptl::them, ast_udptl::tx_seq_no, udptl_build_packet(), and udptl_debug_test_addr().

Referenced by sip_write().

00873 {
00874    int seq;
00875    int len;
00876    int res;
00877    uint8_t buf[LOCAL_FAX_MAX_DATAGRAM];
00878 
00879    /* If we have no peer, return immediately */ 
00880    if (s->them.sin_addr.s_addr == INADDR_ANY)
00881       return 0;
00882 
00883    /* If there is no data length, return immediately */
00884    if (f->datalen == 0)
00885       return 0;
00886    
00887    if (f->frametype != AST_FRAME_MODEM) {
00888       ast_log(LOG_WARNING, "UDPTL can only send T.38 data\n");
00889       return -1;
00890    }
00891 
00892    /* Save seq_no for debug output because udptl_build_packet increments it */
00893    seq = s->tx_seq_no & 0xFFFF;
00894 
00895    /* Cook up the UDPTL packet, with the relevant EC info. */
00896    len = udptl_build_packet(s, buf, f->data, f->datalen);
00897 
00898    if (len > 0 && s->them.sin_port && s->them.sin_addr.s_addr) {
00899       if ((res = sendto(s->fd, buf, len, 0, (struct sockaddr *) &s->them, sizeof(s->them))) < 0)
00900          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));
00901 #if 0
00902       printf("Sent %d bytes of UDPTL data to %s:%d\n", res, ast_inet_ntoa(udptl->them.sin_addr), ntohs(udptl->them.sin_port));
00903 #endif
00904       if (udptl_debug_test_addr(&s->them))
00905          ast_verbose("Sent UDPTL packet to %s:%d (type %d, seq %d, len %d)\n",
00906                ast_inet_ntoa(s->them.sin_addr),
00907                ntohs(s->them.sin_port), 0, seq, len);
00908    }
00909       
00910    return 0;
00911 }

static int decode_length ( uint8_t *  buf,
int  limit,
int *  len,
int *  pvalue 
) [static]

Definition at line 157 of file udptl.c.

Referenced by decode_open_type(), and udptl_rx_packet().

00158 {
00159    if ((buf[*len] & 0x80) == 0) {
00160       if (*len >= limit)
00161          return -1;
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    if (*len >= limit)
00176       return -1;
00177    *pvalue = (buf[*len] & 0x3F) << 14;
00178    (*len)++;
00179    /* Indicate we have a fragment */
00180    return 1;
00181 }

static int decode_open_type ( uint8_t *  buf,
int  limit,
int *  len,
const uint8_t **  p_object,
int *  p_num_octets 
) [static]

Definition at line 184 of file udptl.c.

References decode_length().

Referenced by udptl_rx_packet().

00185 {
00186    int octet_cnt;
00187    int octet_idx;
00188    int stat;
00189    int i;
00190    const uint8_t **pbuf;
00191 
00192    for (octet_idx = 0, *p_num_octets = 0; ; octet_idx += octet_cnt) {
00193       if ((stat = decode_length(buf, limit, len, &octet_cnt)) < 0)
00194          return -1;
00195       if (octet_cnt > 0) {
00196          *p_num_octets += octet_cnt;
00197 
00198          pbuf = &p_object[octet_idx];
00199          i = 0;
00200          /* Make sure the buffer contains at least the number of bits requested */
00201          if ((*len + octet_cnt) > limit)
00202             return -1;
00203 
00204          *pbuf = &buf[*len];
00205          *len += octet_cnt;
00206       }
00207       if (stat == 0)
00208          break;
00209    }
00210    return 0;
00211 }

static int encode_length ( uint8_t *  buf,
int *  len,
int  value 
) [static]

Definition at line 214 of file udptl.c.

Referenced by encode_open_type(), and udptl_build_packet().

00215 {
00216    int multiplier;
00217 
00218    if (value < 0x80) {
00219       /* 1 octet */
00220       buf[*len] = value;
00221       (*len)++;
00222       return value;
00223    }
00224    if (value < 0x4000) {
00225       /* 2 octets */
00226       /* Set the first bit of the first octet */
00227       buf[*len] = ((0x8000 | value) >> 8) & 0xFF;
00228       (*len)++;
00229       buf[*len] = value & 0xFF;
00230       (*len)++;
00231       return value;
00232    }
00233    /* Fragmentation */
00234    multiplier = (value < 0x10000) ? (value >> 14) : 4;
00235    /* Set the first 2 bits of the octet */
00236    buf[*len] = 0xC0 | multiplier;
00237    (*len)++;
00238    return multiplier << 14;
00239 }

static int encode_open_type ( uint8_t *  buf,
int *  len,
const uint8_t *  data,
int  num_octets 
) [static]

Definition at line 242 of file udptl.c.

References encode_length().

Referenced by udptl_build_packet().

00243 {
00244    int enclen;
00245    int octet_idx;
00246    uint8_t zero_byte;
00247 
00248    /* If open type is of zero length, add a single zero byte (10.1) */
00249    if (num_octets == 0) {
00250       zero_byte = 0;
00251       data = &zero_byte;
00252       num_octets = 1;
00253    }
00254    /* Encode the open type */
00255    for (octet_idx = 0; ; num_octets -= enclen, octet_idx += enclen) {
00256       if ((enclen = encode_length(buf, len, num_octets)) < 0)
00257          return -1;
00258       if (enclen > 0) {
00259          memcpy(&buf[*len], &data[octet_idx], enclen);
00260          *len += enclen;
00261       }
00262       if (enclen >= num_octets)
00263          break;
00264    }
00265 
00266    return 0;
00267 }

static struct ast_udptl_protocol* get_proto ( struct ast_channel chan  )  [static, read]

Definition at line 950 of file udptl.c.

References ast_udptl_protocol::next, ast_channel::tech, ast_channel_tech::type, and ast_udptl_protocol::type.

00951 {
00952    struct ast_udptl_protocol *cur;
00953 
00954    cur = protos;
00955    while (cur) {
00956       if (cur->type == chan->tech->type)
00957          return cur;
00958       cur = cur->next;
00959    }
00960    return NULL;
00961 }

static int udptl_build_packet ( struct ast_udptl s,
uint8_t *  buf,
uint8_t *  ifp,
int  ifp_len 
) [static]

Definition at line 478 of file udptl.c.

References udptl_fec_tx_buffer_t::buf, udptl_fec_tx_buffer_t::buf_len, encode_length(), encode_open_type(), ast_udptl::error_correction_entries, ast_udptl::error_correction_scheme, ast_udptl::error_correction_span, len, LOCAL_FAX_MAX_DATAGRAM, seq, ast_udptl::tx, ast_udptl::tx_seq_no, UDPTL_BUF_MASK, UDPTL_ERROR_CORRECTION_FEC, UDPTL_ERROR_CORRECTION_NONE, UDPTL_ERROR_CORRECTION_REDUNDANCY, and ast_udptl::verbose.

Referenced by ast_udptl_write().

00479 {
00480    uint8_t fec[LOCAL_FAX_MAX_DATAGRAM];
00481    int i;
00482    int j;
00483    int seq;
00484    int entry;
00485    int entries;
00486    int span;
00487    int m;
00488    int len;
00489    int limit;
00490    int high_tide;
00491 
00492    seq = s->tx_seq_no & 0xFFFF;
00493 
00494    /* Map the sequence number to an entry in the circular buffer */
00495    entry = seq & UDPTL_BUF_MASK;
00496 
00497    /* We save the message in a circular buffer, for generating FEC or
00498       redundancy sets later on. */
00499    s->tx[entry].buf_len = ifp_len;
00500    memcpy(s->tx[entry].buf, ifp, ifp_len);
00501    
00502    /* Build the UDPTLPacket */
00503 
00504    len = 0;
00505    /* Encode the sequence number */
00506    buf[len++] = (seq >> 8) & 0xFF;
00507    buf[len++] = seq & 0xFF;
00508 
00509    /* Encode the primary IFP packet */
00510    if (encode_open_type(buf, &len, ifp, ifp_len) < 0)
00511       return -1;
00512 
00513    /* Encode the appropriate type of error recovery information */
00514    switch (s->error_correction_scheme)
00515    {
00516    case UDPTL_ERROR_CORRECTION_NONE:
00517       /* Encode the error recovery type */
00518       buf[len++] = 0x00;
00519       /* The number of entries will always be zero, so it is pointless allowing
00520          for the fragmented case here. */
00521       if (encode_length(buf, &len, 0) < 0)
00522          return -1;
00523       break;
00524    case UDPTL_ERROR_CORRECTION_REDUNDANCY:
00525       /* Encode the error recovery type */
00526       buf[len++] = 0x00;
00527       if (s->tx_seq_no > s->error_correction_entries)
00528          entries = s->error_correction_entries;
00529       else
00530          entries = s->tx_seq_no;
00531       /* The number of entries will always be small, so it is pointless allowing
00532          for the fragmented case here. */
00533       if (encode_length(buf, &len, entries) < 0)
00534          return -1;
00535       /* Encode the elements */
00536       for (i = 0; i < entries; i++) {
00537          j = (entry - i - 1) & UDPTL_BUF_MASK;
00538          if (encode_open_type(buf, &len, s->tx[j].buf, s->tx[j].buf_len) < 0)
00539             return -1;
00540       }
00541       break;
00542    case UDPTL_ERROR_CORRECTION_FEC:
00543       span = s->error_correction_span;
00544       entries = s->error_correction_entries;
00545       if (seq < s->error_correction_span*s->error_correction_entries) {
00546          /* In the initial stages, wind up the FEC smoothly */
00547          entries = seq/s->error_correction_span;
00548          if (seq < s->error_correction_span)
00549             span = 0;
00550       }
00551       /* Encode the error recovery type */
00552       buf[len++] = 0x80;
00553       /* Span is defined as an inconstrained integer, which it dumb. It will only
00554          ever be a small value. Treat it as such. */
00555       buf[len++] = 1;
00556       buf[len++] = span;
00557       /* The number of entries is defined as a length, but will only ever be a small
00558          value. Treat it as such. */
00559       buf[len++] = entries;
00560       for (m = 0; m < entries; m++) {
00561          /* Make an XOR'ed entry the maximum length */
00562          limit = (entry + m) & UDPTL_BUF_MASK;
00563          high_tide = 0;
00564          for (i = (limit - span*entries) & UDPTL_BUF_MASK; i != limit; i = (i + entries) & UDPTL_BUF_MASK) {
00565             if (high_tide < s->tx[i].buf_len) {
00566                for (j = 0; j < high_tide; j++)
00567                   fec[j] ^= s->tx[i].buf[j];
00568                for ( ; j < s->tx[i].buf_len; j++)
00569                   fec[j] = s->tx[i].buf[j];
00570                high_tide = s->tx[i].buf_len;
00571             } else {
00572                for (j = 0; j < s->tx[i].buf_len; j++)
00573                   fec[j] ^= s->tx[i].buf[j];
00574             }
00575          }
00576          if (encode_open_type(buf, &len, fec, high_tide) < 0)
00577             return -1;
00578       }
00579       break;
00580    }
00581 
00582    if (s->verbose)
00583       fprintf(stderr, "\n");
00584 
00585    s->tx_seq_no++;
00586    return len;
00587 }

static int udptl_debug_test_addr ( struct sockaddr_in *  addr  )  [inline, static]

Definition at line 144 of file udptl.c.

References udptldebugaddr.

Referenced by ast_udptl_read(), and ast_udptl_write().

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 }

static int udptl_do_debug ( int  fd,
int  argc,
char *  argv[] 
) [static]

Definition at line 1118 of file udptl.c.

References ast_cli(), RESULT_SHOWUSAGE, RESULT_SUCCESS, udptl_do_debug_ip(), and udptldebugaddr.

01119 {
01120    if (argc != 2) {
01121       if (argc != 4)
01122          return RESULT_SHOWUSAGE;
01123       return udptl_do_debug_ip(fd, argc, argv);
01124    }
01125    udptldebug = 1;
01126    memset(&udptldebugaddr,0,sizeof(udptldebugaddr));
01127    ast_cli(fd, "UDPTL Debugging Enabled\n");
01128    return RESULT_SUCCESS;
01129 }

static int udptl_do_debug_ip ( int  fd,
int  argc,
char *  argv[] 
) [static]

Definition at line 1086 of file udptl.c.

References ast_cli(), ast_gethostbyname(), ast_inet_ntoa(), hp, RESULT_SHOWUSAGE, RESULT_SUCCESS, and udptldebugaddr.

Referenced by udptl_do_debug().

01087 {
01088    struct hostent *hp;
01089    struct ast_hostent ahp;
01090    int port;
01091    char *p;
01092    char *arg;
01093 
01094    port = 0;
01095    if (argc != 4)
01096       return RESULT_SHOWUSAGE;
01097    arg = argv[3];
01098    p = strstr(arg, ":");
01099    if (p) {
01100       *p = '\0';
01101       p++;
01102       port = atoi(p);
01103    }
01104    hp = ast_gethostbyname(arg, &ahp);
01105    if (hp == NULL)
01106       return RESULT_SHOWUSAGE;
01107    udptldebugaddr.sin_family = AF_INET;
01108    memcpy(&udptldebugaddr.sin_addr, hp->h_addr, sizeof(udptldebugaddr.sin_addr));
01109    udptldebugaddr.sin_port = htons(port);
01110    if (port == 0)
01111       ast_cli(fd, "UDPTL Debugging Enabled for IP: %s\n", ast_inet_ntoa(udptldebugaddr.sin_addr));
01112    else
01113       ast_cli(fd, "UDPTL Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(udptldebugaddr.sin_addr), port);
01114    udptldebug = 1;
01115    return RESULT_SUCCESS;
01116 }

static int udptl_nodebug ( int  fd,
int  argc,
char *  argv[] 
) [static]

Definition at line 1131 of file udptl.c.

References ast_cli(), RESULT_SHOWUSAGE, and RESULT_SUCCESS.

01132 {
01133    if (argc != 3)
01134       return RESULT_SHOWUSAGE;
01135    udptldebug = 0;
01136    ast_cli(fd,"UDPTL Debugging Disabled\n");
01137    return RESULT_SUCCESS;
01138 }

static int udptl_rx_packet ( struct ast_udptl s,
uint8_t *  buf,
int  len 
) [static]

Definition at line 270 of file udptl.c.

References AST_FRAME_MODEM, AST_LIST_NEXT, AST_MODEM_T38, udptl_fec_rx_buffer_t::buf, udptl_fec_rx_buffer_t::buf_len, ast_frame::data, ast_frame::datalen, decode_length(), decode_open_type(), ast_udptl::f, udptl_fec_rx_buffer_t::fec, udptl_fec_rx_buffer_t::fec_entries, udptl_fec_rx_buffer_t::fec_len, udptl_fec_rx_buffer_t::fec_span, ast_frame::frametype, LOCAL_FAX_MAX_DATAGRAM, ast_frame::mallocd, ast_frame::offset, ast_udptl::rx, ast_udptl::rx_seq_no, ast_frame::seqno, ast_frame::src, ast_frame::subclass, TRUE, and UDPTL_BUF_MASK.

Referenced by ast_udptl_read().

00271 {
00272    int stat;
00273    int stat2;
00274    int i;
00275    int j;
00276    int k;
00277    int l;
00278    int m;
00279    int x;
00280    int limit;
00281    int which;
00282    int ptr;
00283    int count;
00284    int total_count;
00285    int seq_no;
00286    const uint8_t *ifp;
00287    const uint8_t *data;
00288    int ifp_len;
00289    int repaired[16];
00290    const uint8_t *bufs[16];
00291    int lengths[16];
00292    int span;
00293    int entries;
00294    int ifp_no;
00295 
00296    ptr = 0;
00297    ifp_no = 0;
00298    memset(&s->f[0], 0, sizeof(s->f[0]));
00299 
00300    /* Decode seq_number */
00301    if (ptr + 2 > len)
00302       return -1;
00303    seq_no = (buf[0] << 8) | buf[1];
00304    ptr += 2;
00305 
00306    /* Break out the primary packet */
00307    if ((stat = decode_open_type(buf, len, &ptr, &ifp, &ifp_len)) != 0)
00308       return -1;
00309    /* Decode error_recovery */
00310    if (ptr + 1 > len)
00311       return -1;
00312    if ((buf[ptr++] & 0x80) == 0) {
00313       /* Secondary packet mode for error recovery */
00314       if (seq_no > s->rx_seq_no) {
00315          /* We received a later packet than we expected, so we need to check if we can fill in the gap from the
00316             secondary packets. */
00317          total_count = 0;
00318          do {
00319             if ((stat2 = decode_length(buf, len, &ptr, &count)) < 0)
00320                return -1;
00321             for (i = 0; i < count; i++) {
00322                if ((stat = decode_open_type(buf, len, &ptr, &bufs[total_count + i], &lengths[total_count + i])) != 0)
00323                   return -1;
00324             }
00325             total_count += count;
00326          }
00327          while (stat2 > 0);
00328          /* Step through in reverse order, so we go oldest to newest */
00329          for (i = total_count; i > 0; i--) {
00330             if (seq_no - i >= s->rx_seq_no) {
00331                /* This one wasn't seen before */
00332                /* Decode the secondary IFP packet */
00333                //fprintf(stderr, "Secondary %d, len %d\n", seq_no - i, lengths[i - 1]);
00334                s->f[ifp_no].frametype = AST_FRAME_MODEM;
00335                s->f[ifp_no].subclass = AST_MODEM_T38;
00336 
00337                s->f[ifp_no].mallocd = 0;
00338                s->f[ifp_no].seqno = seq_no - i;
00339                s->f[ifp_no].datalen = lengths[i - 1];
00340                s->f[ifp_no].data = (uint8_t *) bufs[i - 1];
00341                s->f[ifp_no].offset = 0;
00342                s->f[ifp_no].src = "UDPTL";
00343                if (ifp_no > 0)
00344                   AST_LIST_NEXT(&s->f[ifp_no - 1], frame_list) = &s->f[ifp_no];
00345                AST_LIST_NEXT(&s->f[ifp_no], frame_list) = NULL;
00346                ifp_no++;
00347             }
00348          }
00349       }
00350    }
00351    else
00352    {
00353       /* FEC mode for error recovery */
00354       /* Our buffers cannot tolerate overlength IFP packets in FEC mode */
00355       if (ifp_len > LOCAL_FAX_MAX_DATAGRAM)
00356          return -1;
00357       /* Update any missed slots in the buffer */
00358       for ( ; seq_no > s->rx_seq_no; s->rx_seq_no++) {
00359          x = s->rx_seq_no & UDPTL_BUF_MASK;
00360          s->rx[x].buf_len = -1;
00361          s->rx[x].fec_len[0] = 0;
00362          s->rx[x].fec_span = 0;
00363          s->rx[x].fec_entries = 0;
00364       }
00365 
00366       x = seq_no & UDPTL_BUF_MASK;
00367 
00368       memset(repaired, 0, sizeof(repaired));
00369 
00370       /* Save the new IFP packet */
00371       memcpy(s->rx[x].buf, ifp, ifp_len);
00372       s->rx[x].buf_len = ifp_len;
00373       repaired[x] = TRUE;
00374 
00375       /* Decode the FEC packets */
00376       /* The span is defined as an unconstrained integer, but will never be more
00377          than a small value. */
00378       if (ptr + 2 > len)
00379          return -1;
00380       if (buf[ptr++] != 1)
00381          return -1;
00382       span = buf[ptr++];
00383       s->rx[x].fec_span = span;
00384 
00385       /* The number of entries is defined as a length, but will only ever be a small
00386          value. Treat it as such. */
00387       if (ptr + 1 > len)
00388          return -1;
00389       entries = buf[ptr++];
00390       s->rx[x].fec_entries = entries;
00391 
00392       /* Decode the elements */
00393       for (i = 0; i < entries; i++) {
00394          if ((stat = decode_open_type(buf, len, &ptr, &data, &s->rx[x].fec_len[i])) != 0)
00395             return -1;
00396          if (s->rx[x].fec_len[i] > LOCAL_FAX_MAX_DATAGRAM)
00397             return -1;
00398 
00399          /* Save the new FEC data */
00400          memcpy(s->rx[x].fec[i], data, s->rx[x].fec_len[i]);
00401 #if 0
00402          fprintf(stderr, "FEC: ");
00403          for (j = 0; j < s->rx[x].fec_len[i]; j++)
00404             fprintf(stderr, "%02X ", data[j]);
00405          fprintf(stderr, "\n");
00406 #endif
00407       }
00408 
00409       /* See if we can reconstruct anything which is missing */
00410       /* TODO: this does not comprehensively hunt back and repair everything that is possible */
00411       for (l = x; l != ((x - (16 - span*entries)) & UDPTL_BUF_MASK); l = (l - 1) & UDPTL_BUF_MASK) {
00412          if (s->rx[l].fec_len[0] <= 0)
00413             continue;
00414          for (m = 0; m < s->rx[l].fec_entries; m++) {
00415             limit = (l + m) & UDPTL_BUF_MASK;
00416             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) {
00417                if (s->rx[k].buf_len <= 0)
00418                   which = (which == -1) ? k : -2;
00419             }
00420             if (which >= 0) {
00421                /* Repairable */
00422                for (j = 0; j < s->rx[l].fec_len[m]; j++) {
00423                   s->rx[which].buf[j] = s->rx[l].fec[m][j];
00424                   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)
00425                      s->rx[which].buf[j] ^= (s->rx[k].buf_len > j) ? s->rx[k].buf[j] : 0;
00426                }
00427                s->rx[which].buf_len = s->rx[l].fec_len[m];
00428                repaired[which] = TRUE;
00429             }
00430          }
00431       }
00432       /* Now play any new packets forwards in time */
00433       for (l = (x + 1) & UDPTL_BUF_MASK, j = seq_no - UDPTL_BUF_MASK; l != x; l = (l + 1) & UDPTL_BUF_MASK, j++) {
00434          if (repaired[l]) {
00435             //fprintf(stderr, "Fixed packet %d, len %d\n", j, l);
00436             s->f[ifp_no].frametype = AST_FRAME_MODEM;
00437             s->f[ifp_no].subclass = AST_MODEM_T38;
00438          
00439             s->f[ifp_no].mallocd = 0;
00440             s->f[ifp_no].seqno = j;
00441             s->f[ifp_no].datalen = s->rx[l].buf_len;
00442             s->f[ifp_no].data = s->rx[l].buf;
00443             s->f[ifp_no].offset = 0;
00444             s->f[ifp_no].src = "UDPTL";
00445             if (ifp_no > 0)
00446                AST_LIST_NEXT(&s->f[ifp_no - 1], frame_list) = &s->f[ifp_no];
00447             AST_LIST_NEXT(&s->f[ifp_no], frame_list) = NULL;
00448             ifp_no++;
00449          }
00450       }
00451    }
00452 
00453    /* If packets are received out of sequence, we may have already processed this packet from the error
00454       recovery information in a packet already received. */
00455    if (seq_no >= s->rx_seq_no) {
00456       /* Decode the primary IFP packet */
00457       s->f[ifp_no].frametype = AST_FRAME_MODEM;
00458       s->f[ifp_no].subclass = AST_MODEM_T38;
00459       
00460       s->f[ifp_no].mallocd = 0;
00461       s->f[ifp_no].seqno = seq_no;
00462       s->f[ifp_no].datalen = ifp_len;
00463       s->f[ifp_no].data = (uint8_t *) ifp;
00464       s->f[ifp_no].offset = 0;
00465       s->f[ifp_no].src = "UDPTL";
00466       if (ifp_no > 0)
00467          AST_LIST_NEXT(&s->f[ifp_no - 1], frame_list) = &s->f[ifp_no];
00468       AST_LIST_NEXT(&s->f[ifp_no], frame_list) = NULL;
00469 
00470       ifp_no++;
00471    }
00472 
00473    s->rx_seq_no = seq_no + 1;
00474    return ifp_no;
00475 }

static int udptlread ( int *  id,
int  fd,
short  events,
void *  cbdata 
) [static]

Definition at line 609 of file udptl.c.

References ast_udptl_read(), ast_udptl::callback, ast_udptl::data, and f.

Referenced by ast_udptl_new_with_bindaddr().

00610 {
00611    struct ast_udptl *udptl = cbdata;
00612    struct ast_frame *f;
00613 
00614    if ((f = ast_udptl_read(udptl))) {
00615       if (udptl->callback)
00616          udptl->callback(udptl, f, udptl->data);
00617    }
00618    return 1;
00619 }


Variable Documentation

struct ast_cli_entry cli_udptl[] [static]

Definition at line 1153 of file udptl.c.

Initial value:

 {
   { "udptl", "no", "debug", NULL },
   udptl_nodebug, NULL,
   NULL }

Definition at line 1148 of file udptl.c.

char debug_usage[] [static]

Initial value:

  "Usage: udptl debug [ip host[:port]]\n"
  "       Enable dumping of all UDPTL packets to and from host.\n"

Definition at line 1140 of file udptl.c.

char nodebug_usage[] [static]

Initial value:

  "Usage: udptl debug off\n"
  "       Disable all UDPTL debugging\n"

Definition at line 1144 of file udptl.c.

struct ast_udptl_protocol* protos [static]

Definition at line 139 of file udptl.c.

Referenced by ast_rtp_proto_register(), ast_rtp_proto_unregister(), and get_proto().

int udptldebug [static]

Definition at line 60 of file udptl.c.

struct sockaddr_in udptldebugaddr [static]

Definition at line 61 of file udptl.c.

Referenced by udptl_debug_test_addr(), udptl_do_debug(), and udptl_do_debug_ip().

int udptlend [static]

Definition at line 59 of file udptl.c.

int udptlfecentries [static]

Definition at line 66 of file udptl.c.

int udptlfecspan [static]

Definition at line 67 of file udptl.c.

int udptlfectype [static]

Definition at line 65 of file udptl.c.

int udptlmaxdatagram [static]

Definition at line 68 of file udptl.c.

int udptlstart [static]

Definition at line 58 of file udptl.c.


Generated on Fri Sep 25 19:28:45 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.5