Thu Apr 28 2011 17:14:03

Asterisk developer's documentation


udptl.c File Reference

UDPTL support for T.38 faxing. More...

#include "asterisk.h"
#include <sys/time.h>
#include <signal.h>
#include <fcntl.h>
#include "asterisk/udptl.h"
#include "asterisk/frame.h"
#include "asterisk/channel.h"
#include "asterisk/acl.h"
#include "asterisk/config.h"
#include "asterisk/lock.h"
#include "asterisk/utils.h"
#include "asterisk/netsock.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
 Structure for an UDPTL session. More...
struct  protos
 List of current sessions. More...
struct  udptl_fec_rx_buffer_t
struct  udptl_fec_tx_buffer_t

Defines

#define DEFAULT_FAX_MAX_DATAGRAM   400
#define FALSE   0
#define FAX_MAX_DATAGRAM_LIMIT   1400
#define LOCAL_FAX_MAX_DATAGRAM   1400
#define LOG_TAG(u)   S_OR(u->tag, "no tag")
#define MAX_FEC_ENTRIES   5
#define MAX_FEC_SPAN   5
#define TRUE   (!FALSE)
#define UDPTL_BUF_MASK   15
#define UDPTL_MTU   1200

Functions

static void __ast_udptl_reload (int reload)
static void __fini_protos (void)
static void __init_protos (void)
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 (const struct ast_udptl *udptl)
enum ast_t38_ec_modes ast_udptl_get_error_correction_scheme (const struct ast_udptl *udptl)
unsigned int ast_udptl_get_far_max_datagram (const struct ast_udptl *udptl)
unsigned int ast_udptl_get_far_max_ifp (struct ast_udptl *udptl)
 retrieves far max ifp
unsigned int ast_udptl_get_local_max_datagram (struct ast_udptl *udptl)
 retrieves local_max_datagram.
void ast_udptl_get_peer (const struct ast_udptl *udptl, struct sockaddr_in *them)
void ast_udptl_get_us (const 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)
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)
int 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, enum ast_t38_ec_modes ec)
void ast_udptl_set_far_max_datagram (struct ast_udptl *udptl, unsigned int max_datagram)
 sets far max datagram size. If max_datagram is = 0, the far max datagram size is set to a default value.
void ast_udptl_set_local_max_ifp (struct ast_udptl *udptl, unsigned int max_ifp)
void ast_udptl_set_peer (struct ast_udptl *udptl, const struct sockaddr_in *them)
void ast_udptl_set_tag (struct ast_udptl *udptl, const char *format,...)
 Associates a character string 'tag' with a UDPTL session.
void ast_udptl_setnat (struct ast_udptl *udptl, int nat)
int ast_udptl_setqos (struct ast_udptl *udptl, unsigned int tos, unsigned int cos)
void ast_udptl_stop (struct ast_udptl *udptl)
int ast_udptl_write (struct ast_udptl *s, struct ast_frame *f)
static void calculate_far_max_ifp (struct ast_udptl *udptl)
static void calculate_local_max_datagram (struct ast_udptl *udptl)
static int decode_length (uint8_t *buf, unsigned int limit, unsigned int *len, unsigned int *pvalue)
static int decode_open_type (uint8_t *buf, unsigned int limit, unsigned int *len, const uint8_t **p_object, unsigned int *p_num_octets)
static unsigned int encode_length (uint8_t *buf, unsigned int *len, unsigned int value)
static int encode_open_type (const struct ast_udptl *udptl, uint8_t *buf, unsigned int buflen, unsigned int *len, const uint8_t *data, unsigned int num_octets)
static struct ast_udptl_protocolget_proto (struct ast_channel *chan)
static char * handle_cli_udptl_set_debug (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static int udptl_build_packet (struct ast_udptl *s, uint8_t *buf, unsigned int buflen, uint8_t *ifp, unsigned int ifp_len)
static int udptl_debug_test_addr (const struct sockaddr_in *addr)
static int udptl_rx_packet (struct ast_udptl *s, uint8_t *buf, unsigned int len)
static int udptlread (int *id, int fd, short events, void *cbdata)

Variables

static struct ast_cli_entry cli_udptl []
static struct protos protos
static int udptldebug
static struct sockaddr_in udptldebugaddr
static int udptlend = 4599
static int udptlfecentries
static int udptlfecspan
static int udptlstart = 4500
static int use_even_ports

Detailed Description

UDPTL support for T.38 faxing.

Author:
Mark Spencer <markster@digium.com>
Steve Underwood <steveu@coppice.org>
Kevin P. Fleming <kpfleming@digium.com>

Definition in file udptl.c.


Define Documentation

#define DEFAULT_FAX_MAX_DATAGRAM   400

Definition at line 94 of file udptl.c.

Referenced by ast_udptl_set_far_max_datagram(), and ast_udptl_write().

#define FALSE   0

Definition at line 74 of file udptl.c.

#define FAX_MAX_DATAGRAM_LIMIT   1400

Definition at line 95 of file udptl.c.

Referenced by ast_udptl_set_far_max_datagram().

#define LOCAL_FAX_MAX_DATAGRAM   1400

Definition at line 93 of file udptl.c.

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

#define LOG_TAG (   u)    S_OR(u->tag, "no tag")
#define MAX_FEC_ENTRIES   5

Definition at line 96 of file udptl.c.

Referenced by __ast_udptl_reload(), and udptl_rx_packet().

#define MAX_FEC_SPAN   5

Definition at line 97 of file udptl.c.

Referenced by __ast_udptl_reload().

#define TRUE   (!FALSE)

Definition at line 77 of file udptl.c.

Referenced by udptl_rx_packet().

#define UDPTL_BUF_MASK   15

Definition at line 99 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 71 of file udptl.c.


Function Documentation

static void __ast_udptl_reload ( int  reload) [static]

Definition at line 1331 of file udptl.c.

References ast_config_destroy(), ast_config_load2(), ast_false(), ast_log(), ast_true(), ast_variable_retrieve(), ast_verb, CONFIG_FLAG_FILEUNCHANGED, CONFIG_STATUS_FILEINVALID, CONFIG_STATUS_FILEMISSING, CONFIG_STATUS_FILEUNCHANGED, LOG_NOTICE, LOG_WARNING, MAX_FEC_ENTRIES, MAX_FEC_SPAN, s, udptlend, and udptlstart.

Referenced by ast_udptl_init(), and ast_udptl_reload().

{
   struct ast_config *cfg;
   const char *s;
   struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };

   cfg = ast_config_load2("udptl.conf", "udptl", config_flags);
   if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID) {
      return;
   }

   udptlstart = 4500;
   udptlend = 4999;
   udptlfecentries = 0;
   udptlfecspan = 0;
   use_even_ports = 0;

   if (cfg) {
      if ((s = ast_variable_retrieve(cfg, "general", "udptlstart"))) {
         udptlstart = atoi(s);
         if (udptlstart < 1024) {
            ast_log(LOG_WARNING, "Ports under 1024 are not allowed for T.38.\n");
            udptlstart = 1024;
         }
         if (udptlstart > 65535) {
            ast_log(LOG_WARNING, "Ports over 65535 are invalid.\n");
            udptlstart = 65535;
         }
      }
      if ((s = ast_variable_retrieve(cfg, "general", "udptlend"))) {
         udptlend = atoi(s);
         if (udptlend < 1024) {
            ast_log(LOG_WARNING, "Ports under 1024 are not allowed for T.38.\n");
            udptlend = 1024;
         }
         if (udptlend > 65535) {
            ast_log(LOG_WARNING, "Ports over 65535 are invalid.\n");
            udptlend = 65535;
         }
      }
      if ((s = ast_variable_retrieve(cfg, "general", "udptlchecksums"))) {
#ifdef SO_NO_CHECK
         if (ast_false(s))
            nochecksums = 1;
         else
            nochecksums = 0;
#else
         if (ast_false(s))
            ast_log(LOG_WARNING, "Disabling UDPTL checksums is not supported on this operating system!\n");
#endif
      }
      if ((s = ast_variable_retrieve(cfg, "general", "T38FaxUdpEC"))) {
         ast_log(LOG_WARNING, "T38FaxUdpEC in udptl.conf is no longer supported; use the t38pt_udptl configuration option in sip.conf instead.\n");
      }
      if ((s = ast_variable_retrieve(cfg, "general", "T38FaxMaxDatagram"))) {
         ast_log(LOG_WARNING, "T38FaxMaxDatagram in udptl.conf is no longer supported; value is now supplied by T.38 applications.\n");
      }
      if ((s = ast_variable_retrieve(cfg, "general", "UDPTLFECEntries"))) {
         udptlfecentries = atoi(s);
         if (udptlfecentries < 1) {
            ast_log(LOG_WARNING, "Too small UDPTLFECEntries value.  Defaulting to 1.\n");
            udptlfecentries = 1;
         }
         if (udptlfecentries > MAX_FEC_ENTRIES) {
            ast_log(LOG_WARNING, "Too large UDPTLFECEntries value.  Defaulting to %d.\n", MAX_FEC_ENTRIES);
            udptlfecentries = MAX_FEC_ENTRIES;
         }
      }
      if ((s = ast_variable_retrieve(cfg, "general", "UDPTLFECSpan"))) {
         udptlfecspan = atoi(s);
         if (udptlfecspan < 1) {
            ast_log(LOG_WARNING, "Too small UDPTLFECSpan value.  Defaulting to 1.\n");
            udptlfecspan = 1;
         }
         if (udptlfecspan > MAX_FEC_SPAN) {
            ast_log(LOG_WARNING, "Too large UDPTLFECSpan value.  Defaulting to %d.\n", MAX_FEC_SPAN);
            udptlfecspan = MAX_FEC_SPAN;
         }
      }
      if ((s = ast_variable_retrieve(cfg, "general", "use_even_ports"))) {
         use_even_ports = ast_true(s);
      }
      ast_config_destroy(cfg);
   }
   if (udptlstart >= udptlend) {
      ast_log(LOG_WARNING, "Unreasonable values for UDPTL start/end ports; defaulting to 4500-4999.\n");
      udptlstart = 4500;
      udptlend = 4999;
   }
   if (use_even_ports && (udptlstart & 1)) {
      ++udptlstart;
      ast_log(LOG_NOTICE, "Odd numbered udptlstart specified but use_even_ports enabled. udptlstart is now %d\n", udptlstart);
   }
   if (use_even_ports && (udptlend & 1)) {
      --udptlend;
      ast_log(LOG_NOTICE, "Odd numbered udptlend specified but use_event_ports enabled. udptlend is now %d\n", udptlend);
   }
   ast_verb(2, "UDPTL allocating from port range %d -> %d\n", udptlstart, udptlend);
}
static void __fini_protos ( void  ) [static]

Definition at line 183 of file udptl.c.

{
static void __init_protos ( void  ) [static]

Definition at line 183 of file udptl.c.

{
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 1139 of file udptl.c.

References ast_channel_lock, ast_channel_trylock, ast_channel_unlock, ast_check_hangup(), ast_debug, 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_WARNING, ast_channel::masq, ast_channel::masqr, ast_channel::name, ast_udptl_protocol::set_udptl_peer, and ast_channel::tech_pvt.

{
   struct ast_frame *f;
   struct ast_channel *who;
   struct ast_channel *cs[3];
   struct ast_udptl *p0;
   struct ast_udptl *p1;
   struct ast_udptl_protocol *pr0;
   struct ast_udptl_protocol *pr1;
   struct sockaddr_in ac0;
   struct sockaddr_in ac1;
   struct sockaddr_in t0;
   struct sockaddr_in t1;
   void *pvt0;
   void *pvt1;
   int to;
   
   ast_channel_lock(c0);
   while (ast_channel_trylock(c1)) {
      ast_channel_unlock(c0);
      usleep(1);
      ast_channel_lock(c0);
   }
   pr0 = get_proto(c0);
   pr1 = get_proto(c1);
   if (!pr0) {
      ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c0->name);
      ast_channel_unlock(c0);
      ast_channel_unlock(c1);
      return -1;
   }
   if (!pr1) {
      ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c1->name);
      ast_channel_unlock(c0);
      ast_channel_unlock(c1);
      return -1;
   }
   pvt0 = c0->tech_pvt;
   pvt1 = c1->tech_pvt;
   p0 = pr0->get_udptl_info(c0);
   p1 = pr1->get_udptl_info(c1);
   if (!p0 || !p1) {
      /* Somebody doesn't want to play... */
      ast_channel_unlock(c0);
      ast_channel_unlock(c1);
      return -2;
   }
   if (pr0->set_udptl_peer(c0, p1)) {
      ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c0->name, c1->name);
      memset(&ac1, 0, sizeof(ac1));
   } else {
      /* Store UDPTL peer */
      ast_udptl_get_peer(p1, &ac1);
   }
   if (pr1->set_udptl_peer(c1, p0)) {
      ast_log(LOG_WARNING, "Channel '%s' failed to talk back to '%s'\n", c1->name, c0->name);
      memset(&ac0, 0, sizeof(ac0));
   } else {
      /* Store UDPTL peer */
      ast_udptl_get_peer(p0, &ac0);
   }
   ast_channel_unlock(c0);
   ast_channel_unlock(c1);
   cs[0] = c0;
   cs[1] = c1;
   cs[2] = NULL;
   for (;;) {
      if ((c0->tech_pvt != pvt0) ||
         (c1->tech_pvt != pvt1) ||
         (c0->masq || c0->masqr || c1->masq || c1->masqr)) {
            ast_debug(1, "Oooh, something is weird, backing out\n");
            /* Tell it to try again later */
            return -3;
      }
      to = -1;
      ast_udptl_get_peer(p1, &t1);
      ast_udptl_get_peer(p0, &t0);
      if (inaddrcmp(&t1, &ac1)) {
         ast_debug(1, "Oooh, '%s' changed end address to %s:%d\n", 
            c1->name, ast_inet_ntoa(t1.sin_addr), ntohs(t1.sin_port));
         ast_debug(1, "Oooh, '%s' was %s:%d\n", 
            c1->name, ast_inet_ntoa(ac1.sin_addr), ntohs(ac1.sin_port));
         memcpy(&ac1, &t1, sizeof(ac1));
      }
      if (inaddrcmp(&t0, &ac0)) {
         ast_debug(1, "Oooh, '%s' changed end address to %s:%d\n", 
            c0->name, ast_inet_ntoa(t0.sin_addr), ntohs(t0.sin_port));
         ast_debug(1, "Oooh, '%s' was %s:%d\n", 
            c0->name, ast_inet_ntoa(ac0.sin_addr), ntohs(ac0.sin_port));
         memcpy(&ac0, &t0, sizeof(ac0));
      }
      who = ast_waitfor_n(cs, 2, &to);
      if (!who) {
         ast_debug(1, "Ooh, empty read...\n");
         /* check for hangup / whentohangup */
         if (ast_check_hangup(c0) || ast_check_hangup(c1))
            break;
         continue;
      }
      f = ast_read(who);
      if (!f) {
         *fo = f;
         *rc = who;
         ast_debug(1, "Oooh, got a %s\n", f ? "digit" : "hangup");
         /* That's all we needed */
         return 0;
      } else {
         if (f->frametype == AST_FRAME_MODEM) {
            /* Forward T.38 frames if they happen upon us */
            if (who == c0) {
               ast_write(c1, f);
            } else if (who == c1) {
               ast_write(c0, f);
            }
         }
         ast_frfree(f);
      }
      /* Swap priority. Not that it's a big deal at this point */
      cs[2] = cs[0];
      cs[0] = cs[1];
      cs[1] = cs[2];
   }
   return -1;
}
void ast_udptl_destroy ( struct ast_udptl udptl)

Definition at line 1038 of file udptl.c.

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

Referenced by __sip_destroy(), and create_addr_from_peer().

{
   if (udptl->ioid)
      ast_io_remove(udptl->io, udptl->ioid);
   if (udptl->fd > -1)
      close(udptl->fd);
   if (udptl->tag)
      ast_free(udptl->tag);
   ast_free(udptl);
}
int ast_udptl_fd ( const struct ast_udptl udptl)

Definition at line 629 of file udptl.c.

References ast_udptl::fd.

Referenced by __oh323_new(), and sip_new().

{
   return udptl->fd;
}
enum ast_t38_ec_modes ast_udptl_get_error_correction_scheme ( const struct ast_udptl udptl)

Definition at line 820 of file udptl.c.

References ast_udptl::error_correction_scheme.

Referenced by add_sdp().

{
   return udptl->error_correction_scheme;
}
unsigned int ast_udptl_get_far_max_datagram ( const struct ast_udptl udptl)

Definition at line 888 of file udptl.c.

References ast_udptl::far_max_datagram.

Referenced by process_sdp().

{
   if (udptl->far_max_datagram < 0) {
      return 0;
   }
   return udptl->far_max_datagram;
}
unsigned int ast_udptl_get_far_max_ifp ( struct ast_udptl udptl)

retrieves far max ifp

Return values:
positivevalue representing max ifp size
0if no value is present

Definition at line 896 of file udptl.c.

References calculate_far_max_ifp(), and ast_udptl::far_max_ifp.

Referenced by change_t38_state(), and interpret_t38_parameters().

{
   if (udptl->far_max_ifp == -1) {
      calculate_far_max_ifp(udptl);
   }

   if (udptl->far_max_ifp < 0) {
      return 0;
   }
   return udptl->far_max_ifp;
}
unsigned int ast_udptl_get_local_max_datagram ( struct ast_udptl udptl)

retrieves local_max_datagram.

Return values:
positivevalue representing max datagram size.
0if no value is present

Definition at line 864 of file udptl.c.

References calculate_local_max_datagram(), and ast_udptl::local_max_datagram.

Referenced by add_sdp().

{
   if (udptl->local_max_datagram == -1) {
      calculate_local_max_datagram(udptl);
   }

   /* this function expects a unsigned value in return. */
   if (udptl->local_max_datagram < 0) {
      return 0;
   }
   return udptl->local_max_datagram;
}
void ast_udptl_get_peer ( const struct ast_udptl udptl,
struct sockaddr_in *  them 
)

Definition at line 1019 of file udptl.c.

References ast_udptl::them.

Referenced by ast_udptl_bridge(), and sip_set_udptl_peer().

{
   memset(them, 0, sizeof(*them));
   them->sin_family = AF_INET;
   them->sin_port = udptl->them.sin_port;
   them->sin_addr = udptl->them.sin_addr;
}
void ast_udptl_get_us ( const struct ast_udptl udptl,
struct sockaddr_in *  us 
)

Definition at line 1027 of file udptl.c.

References ast_udptl::us.

Referenced by add_sdp().

{
   memcpy(us, &udptl->us, sizeof(udptl->us));
}
void ast_udptl_init ( void  )
struct ast_udptl* ast_udptl_new ( struct sched_context sched,
struct io_context io,
int  callbackmode 
) [read]

Definition at line 986 of file udptl.c.

References ast_udptl_new_with_bindaddr().

{
   struct in_addr ia;
   memset(&ia, 0, sizeof(ia));
   return ast_udptl_new_with_bindaddr(sched, io, callbackmode, ia);
}
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 908 of file udptl.c.

References ast_calloc, ast_free, 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_span, ast_udptl::far_max_datagram, ast_udptl::far_max_ifp, ast_udptl::fd, ast_udptl::flags, io, ast_udptl::io, ast_udptl::ioid, ast_udptl::local_max_datagram, ast_udptl::local_max_ifp, LOG_WARNING, ast_udptl::rx, sched, ast_udptl::sched, ast_udptl::them, ast_udptl::tx, UDPTL_BUF_MASK, udptlend, udptlfecentries, udptlfecspan, udptlread(), udptlstart, and ast_udptl::us.

Referenced by ast_udptl_new(), create_addr_from_peer(), handle_request_invite(), and sip_alloc().

{
   struct ast_udptl *udptl;
   int x;
   int startplace;
   int i;
   long int flags;

   if (!(udptl = ast_calloc(1, sizeof(*udptl))))
      return NULL;

   udptl->error_correction_span = udptlfecspan;
   udptl->error_correction_entries = udptlfecentries;
   
   udptl->far_max_datagram = -1;
   udptl->far_max_ifp = -1;
   udptl->local_max_ifp = -1;
   udptl->local_max_datagram = -1;

   for (i = 0; i <= UDPTL_BUF_MASK; i++) {
      udptl->rx[i].buf_len = -1;
      udptl->tx[i].buf_len = -1;
   }

   udptl->them.sin_family = AF_INET;
   udptl->us.sin_family = AF_INET;

   if ((udptl->fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
      ast_free(udptl);
      ast_log(LOG_WARNING, "Unable to allocate socket: %s\n", strerror(errno));
      return NULL;
   }
   flags = fcntl(udptl->fd, F_GETFL);
   fcntl(udptl->fd, F_SETFL, flags | O_NONBLOCK);
#ifdef SO_NO_CHECK
   if (nochecksums)
      setsockopt(udptl->fd, SOL_SOCKET, SO_NO_CHECK, &nochecksums, sizeof(nochecksums));
#endif
   /* Find us a place */
   x = (udptlstart == udptlend) ? udptlstart : (ast_random() % (udptlend - udptlstart)) + udptlstart;
   if (use_even_ports && (x & 1)) {
      ++x;
   }
   startplace = x;
   for (;;) {
      udptl->us.sin_port = htons(x);
      udptl->us.sin_addr = addr;
      if (bind(udptl->fd, (struct sockaddr *) &udptl->us, sizeof(udptl->us)) == 0)
         break;
      if (errno != EADDRINUSE) {
         ast_log(LOG_WARNING, "Unexpected bind error: %s\n", strerror(errno));
         close(udptl->fd);
         ast_free(udptl);
         return NULL;
      }
      if (use_even_ports) {
         x += 2;
      } else {
         ++x;
      }
      if (x > udptlend)
         x = udptlstart;
      if (x == startplace) {
         ast_log(LOG_WARNING, "No UDPTL ports remaining\n");
         close(udptl->fd);
         ast_free(udptl);
         return NULL;
      }
   }
   if (io && sched && callbackmode) {
      /* Operate this one in a callback mode */
      udptl->sched = sched;
      udptl->io = io;
      udptl->ioid = ast_io_add(udptl->io, udptl->fd, udptlread, AST_IO_IN, udptl);
   }
   return udptl;
}
int ast_udptl_proto_register ( struct ast_udptl_protocol proto)

Definition at line 1108 of file udptl.c.

References ast_log(), AST_RWLIST_INSERT_TAIL, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, LOG_WARNING, and ast_udptl_protocol::type.

Referenced by load_module().

{
   struct ast_udptl_protocol *cur;

   AST_RWLIST_WRLOCK(&protos);
   AST_RWLIST_TRAVERSE(&protos, cur, list) {
      if (cur->type == proto->type) {
         ast_log(LOG_WARNING, "Tried to register same protocol '%s' twice\n", cur->type);
         AST_RWLIST_UNLOCK(&protos);
         return -1;
      }
   }
   AST_RWLIST_INSERT_TAIL(&protos, proto, list);
   AST_RWLIST_UNLOCK(&protos);
   return 0;
}
void ast_udptl_proto_unregister ( struct ast_udptl_protocol proto)

Definition at line 1101 of file udptl.c.

References AST_RWLIST_REMOVE, AST_RWLIST_UNLOCK, and AST_RWLIST_WRLOCK.

Referenced by unload_module().

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

Definition at line 661 of file udptl.c.

References ast_assert, ast_debug, AST_FRIENDLY_OFFSET, ast_inet_ntoa(), ast_log(), ast_null_frame, ast_verb, errno, ast_udptl::f, ast_udptl::fd, len(), LOG_TAG, 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().

{
   int res;
   struct sockaddr_in sin;
   socklen_t len;
   uint16_t seqno = 0;
   uint16_t *udptlheader;

   len = sizeof(sin);
   
   /* Cache where the header will go */
   res = recvfrom(udptl->fd,
         udptl->rawdata + AST_FRIENDLY_OFFSET,
         sizeof(udptl->rawdata) - AST_FRIENDLY_OFFSET,
         0,
         (struct sockaddr *) &sin,
         &len);
   udptlheader = (uint16_t *)(udptl->rawdata + AST_FRIENDLY_OFFSET);
   if (res < 0) {
      if (errno != EAGAIN)
         ast_log(LOG_WARNING, "(%s): UDPTL read error: %s\n",
            LOG_TAG(udptl), strerror(errno));
      ast_assert(errno != EBADF);
      return &ast_null_frame;
   }

   /* Ignore if the other side hasn't been given an address yet. */
   if (!udptl->them.sin_addr.s_addr || !udptl->them.sin_port)
      return &ast_null_frame;

   if (udptl->nat) {
      /* Send to whoever sent to us */
      if ((udptl->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
         (udptl->them.sin_port != sin.sin_port)) {
         memcpy(&udptl->them, &sin, sizeof(udptl->them));
         ast_debug(1, "UDPTL NAT (%s): Using address %s:%d\n",
              LOG_TAG(udptl), ast_inet_ntoa(udptl->them.sin_addr), ntohs(udptl->them.sin_port));
      }
   }

   if (udptl_debug_test_addr(&sin)) {
      ast_verb(1, "UDPTL (%s): packet from %s:%d (type %d, seq %d, len %d)\n",
          LOG_TAG(udptl), ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), 0, seqno, res);
   }
   if (udptl_rx_packet(udptl, udptl->rawdata + AST_FRIENDLY_OFFSET, res) < 1)
      return &ast_null_frame;

   return &udptl->f[0];
}
int ast_udptl_reload ( void  )
Version:
1.6.1 return changed to int

Definition at line 1431 of file udptl.c.

References __ast_udptl_reload().

{
   __ast_udptl_reload(1);
   return 0;
}
void ast_udptl_set_callback ( struct ast_udptl udptl,
ast_udptl_callback  callback 
)

Definition at line 639 of file udptl.c.

References ast_udptl::callback.

{
   udptl->callback = callback;
}
void ast_udptl_set_data ( struct ast_udptl udptl,
void *  data 
)

Definition at line 634 of file udptl.c.

References ast_udptl::data.

{
   udptl->data = data;
}
void ast_udptl_set_error_correction_scheme ( struct ast_udptl udptl,
enum ast_t38_ec_modes  ec 
)
void ast_udptl_set_far_max_datagram ( struct ast_udptl udptl,
unsigned int  max_datagram 
)

sets far max datagram size. If max_datagram is = 0, the far max datagram size is set to a default value.

Definition at line 877 of file udptl.c.

References DEFAULT_FAX_MAX_DATAGRAM, ast_udptl::far_max_datagram, ast_udptl::far_max_ifp, and FAX_MAX_DATAGRAM_LIMIT.

Referenced by process_sdp(), and process_sdp_a_image().

{
   if (!max_datagram || (max_datagram > FAX_MAX_DATAGRAM_LIMIT)) {
      udptl->far_max_datagram = DEFAULT_FAX_MAX_DATAGRAM;
   } else {
      udptl->far_max_datagram = max_datagram;
   }
   /* reset calculated values so they'll be computed again */
   udptl->far_max_ifp = -1;
}
void ast_udptl_set_local_max_ifp ( struct ast_udptl udptl,
unsigned int  max_ifp 
)

Definition at line 853 of file udptl.c.

References ast_udptl::local_max_datagram, and ast_udptl::local_max_ifp.

Referenced by interpret_t38_parameters().

{
   /* make sure max_ifp is a positive value since a cast will take place when
    * when setting local_max_ifp */
   if ((signed int) max_ifp > 0) {
      udptl->local_max_ifp = max_ifp;
      /* reset calculated values so they'll be computed again */
      udptl->local_max_datagram = -1;
   }
}
void ast_udptl_set_peer ( struct ast_udptl udptl,
const struct sockaddr_in *  them 
)

Definition at line 1013 of file udptl.c.

References ast_udptl::them.

Referenced by process_sdp().

{
   udptl->them.sin_port = them->sin_port;
   udptl->them.sin_addr = them->sin_addr;
}
void ast_udptl_set_tag ( struct ast_udptl udptl,
const char *  format,
  ... 
)

Associates a character string 'tag' with a UDPTL session.

Parameters:
udptlThe UDPTL session.
formatprintf-style format string used to construct the tag

This function formats a tag for the specified UDPTL session, so that any log messages generated by the UDPTL stack related to that session will include the tag and the reader of the messages will be able to identify which endpoint caused them to be generated.

Return values:
none

Definition at line 993 of file udptl.c.

References ast_free, ast_vasprintf, and ast_udptl::tag.

Referenced by change_t38_state().

{
   va_list ap;

   if (udptl->tag) {
      ast_free(udptl->tag);
      udptl->tag = NULL;
   }
   va_start(ap, format);
   if (ast_vasprintf(&udptl->tag, format, ap) == -1) {
      udptl->tag = NULL;
   }
   va_end(ap);
}
void ast_udptl_setnat ( struct ast_udptl udptl,
int  nat 
)

Definition at line 644 of file udptl.c.

References nat, and ast_udptl::nat.

Referenced by do_setnat().

{
   udptl->nat = nat;
}
int ast_udptl_setqos ( struct ast_udptl udptl,
unsigned int  tos,
unsigned int  cos 
)

Definition at line 1008 of file udptl.c.

References ast_netsock_set_qos(), and ast_udptl::fd.

Referenced by sip_alloc().

{
   return ast_netsock_set_qos(udptl->fd, tos, cos, "UDPTL");
}
void ast_udptl_stop ( struct ast_udptl udptl)

Definition at line 1032 of file udptl.c.

References ast_udptl::them.

Referenced by process_sdp(), and stop_media_flows().

{
   memset(&udptl->them.sin_addr, 0, sizeof(udptl->them.sin_addr));
   memset(&udptl->them.sin_port, 0, sizeof(udptl->them.sin_port));
}
int ast_udptl_write ( struct ast_udptl s,
struct ast_frame f 
)

Definition at line 1049 of file udptl.c.

References AST_FRAME_MODEM, ast_inet_ntoa(), ast_log(), AST_MODEM_T38, ast_verb, buf, ast_frame::data, ast_frame::datalen, DEFAULT_FAX_MAX_DATAGRAM, errno, ast_udptl::far_max_datagram, ast_udptl::far_max_ifp, ast_udptl::fd, ast_frame::frametype, len(), LOG_NOTICE, LOG_TAG, LOG_WARNING, ast_frame::ptr, seq, ast_frame::subclass, ast_udptl::them, ast_udptl::tx_seq_no, udptl_build_packet(), and udptl_debug_test_addr().

Referenced by sip_write().

{
   unsigned int seq;
   unsigned int len = f->datalen;
   int res;
   /* if no max datagram size is provided, use default value */
   const int bufsize = (s->far_max_datagram > 0) ? s->far_max_datagram : DEFAULT_FAX_MAX_DATAGRAM;
   uint8_t buf[bufsize];

   memset(buf, 0, sizeof(buf));

   /* If we have no peer, return immediately */ 
   if (s->them.sin_addr.s_addr == INADDR_ANY)
      return 0;

   /* If there is no data length, return immediately */
   if (f->datalen == 0)
      return 0;
   
   if ((f->frametype != AST_FRAME_MODEM) ||
       (f->subclass != AST_MODEM_T38)) {
      ast_log(LOG_WARNING, "(%s): UDPTL can only send T.38 data.\n",
         LOG_TAG(s));
      return -1;
   }

   if (len > s->far_max_ifp) {
      ast_log(LOG_WARNING,
         "(%s): UDPTL asked to send %d bytes of IFP when far end only prepared to accept %d bytes; data loss will occur."
         "You may need to override the T38FaxMaxDatagram value for this endpoint in the channel driver configuration.\n",
         LOG_TAG(s), len, s->far_max_ifp);
      len = s->far_max_ifp;
   }

   /* Save seq_no for debug output because udptl_build_packet increments it */
   seq = s->tx_seq_no & 0xFFFF;

   /* Cook up the UDPTL packet, with the relevant EC info. */
   len = udptl_build_packet(s, buf, sizeof(buf), f->data.ptr, len);

   if ((signed int) len > 0 && s->them.sin_port && s->them.sin_addr.s_addr) {
      if ((res = sendto(s->fd, buf, len, 0, (struct sockaddr *) &s->them, sizeof(s->them))) < 0)
         ast_log(LOG_NOTICE, "(%s): UDPTL Transmission error to %s:%d: %s\n",
            LOG_TAG(s), ast_inet_ntoa(s->them.sin_addr), ntohs(s->them.sin_port), strerror(errno));
      if (udptl_debug_test_addr(&s->them))
         ast_verb(1, "UDPTL (%s): packet to %s:%d (type %d, seq %d, len %d)\n",
             LOG_TAG(s), ast_inet_ntoa(s->them.sin_addr), ntohs(s->them.sin_port), 0, seq, len);
   }
      
   return 0;
}
static void calculate_far_max_ifp ( struct ast_udptl udptl) [static]

Definition at line 752 of file udptl.c.

References ast_log(), ast_udptl::error_correction_entries, ast_udptl::error_correction_scheme, ast_udptl::far_max_datagram, ast_udptl::far_max_ifp, LOG_TAG, LOG_WARNING, UDPTL_ERROR_CORRECTION_FEC, UDPTL_ERROR_CORRECTION_NONE, and UDPTL_ERROR_CORRECTION_REDUNDANCY.

Referenced by ast_udptl_get_far_max_ifp().

{
   unsigned new_max = 0;

   if (udptl->far_max_datagram == -1) {
      ast_log(LOG_WARNING, "(%s): Cannot calculate far_max_ifp before far_max_datagram has been set.\n",
         LOG_TAG(udptl));
      udptl->far_max_ifp = -1;
      return;
   }

   /* the goal here is to supply the local endpoint (application
    * or bridged channel) a maximum IFP value that will allow it
    * to effectively and efficiently transfer image data at its
    * selected bit rate, taking into account the selected error
    * correction mode, but without overrunning the far endpoint's
    * datagram buffer. this is complicated by the fact that some
    * far endpoints send us bogus (small) max datagram values,
    * which would result in either buffer overrun or no error
    * correction. we try to accomodate those, but if the supplied
    * value is too small to do so, we'll emit warning messages and
    * the user will have to use configuration options to override
    * the max datagram value supplied by the far endpoint.
    */
   switch (udptl->error_correction_scheme) {
   case UDPTL_ERROR_CORRECTION_NONE:
      /* need room for sequence number, length indicator, redundancy
       * indicator and following length indicator
       */
      new_max = udptl->far_max_datagram - 5;
      break;
   case UDPTL_ERROR_CORRECTION_REDUNDANCY:
      /* for this case, we'd like to send as many error correction entries
       * as possible (up to the number we're configured for), but we'll settle
       * for sending fewer if the configured number would cause the
       * calculated max IFP to be too small for effective operation
       *
       * need room for sequence number, length indicators and the
       * configured number of redundant packets
       *
       * note: we purposely don't allow error_correction_entries to drop to
       * zero in this loop; we'd rather send smaller IFPs (and thus reduce
       * the image data transfer rate) than sacrifice redundancy completely
       */
      for (;;) {
         new_max = (udptl->far_max_datagram - 8) / (udptl->error_correction_entries + 1);

         if ((new_max < 80) && (udptl->error_correction_entries > 1)) {
            /* the max ifp is not large enough, subtract an
             * error correction entry and calculate again
             * */
            --udptl->error_correction_entries;
         } else {
            break;
         }
      }
      break;
   case UDPTL_ERROR_CORRECTION_FEC:
      /* need room for sequence number, length indicators and a
       * a single IFP of the maximum size expected
       */
      new_max = (udptl->far_max_datagram - 10) / 2;
      break;
   }
   /* subtract 5% of space for insurance */
   udptl->far_max_ifp = new_max * 0.95;
}
static void calculate_local_max_datagram ( struct ast_udptl udptl) [static]

Definition at line 711 of file udptl.c.

References ast_log(), ast_udptl::error_correction_scheme, LOCAL_FAX_MAX_DATAGRAM, ast_udptl::local_max_datagram, ast_udptl::local_max_ifp, LOG_TAG, LOG_WARNING, MIN, UDPTL_ERROR_CORRECTION_FEC, UDPTL_ERROR_CORRECTION_NONE, and UDPTL_ERROR_CORRECTION_REDUNDANCY.

Referenced by ast_udptl_get_local_max_datagram().

{
   unsigned int new_max = 0;

   if (udptl->local_max_ifp == -1) {
      ast_log(LOG_WARNING, "(%s): Cannot calculate local_max_datagram before local_max_ifp has been set.\n",
         LOG_TAG(udptl));
      udptl->local_max_datagram = -1;
      return;
   }

   /* calculate the amount of space required to receive an IFP
    * of the maximum size supported by the application/endpoint
    * that we are delivering them to (local endpoint), and add
    * the amount of space required to support the selected
    * error correction mode
    */
   switch (udptl->error_correction_scheme) {
   case UDPTL_ERROR_CORRECTION_NONE:
      /* need room for sequence number, length indicator, redundancy
       * indicator and following length indicator
       */
      new_max = 5 + udptl->local_max_ifp;
      break;
   case UDPTL_ERROR_CORRECTION_REDUNDANCY:
      /* need room for sequence number, length indicators, plus
       * room for up to 3 redundancy packets
       */
      new_max = 5 + udptl->local_max_ifp + 2 + (3 * udptl->local_max_ifp);
      break;
   case UDPTL_ERROR_CORRECTION_FEC:
      /* need room for sequence number, length indicators and a
       * a single IFP of the maximum size expected
       */
      new_max = 5 + udptl->local_max_ifp + 4 + udptl->local_max_ifp;
      break;
   }
   /* add 5% extra space for insurance, but no larger than LOCAL_FAX_MAX_DATAGRAM */
   udptl->local_max_datagram = MIN(new_max * 1.05, LOCAL_FAX_MAX_DATAGRAM);
}
static int decode_length ( uint8_t *  buf,
unsigned int  limit,
unsigned int *  len,
unsigned int *  pvalue 
) [static]

Definition at line 198 of file udptl.c.

References ast_debug, and len().

Referenced by decode_open_type(), and udptl_rx_packet().

{
   if (*len >= limit)
      return -1;
   if ((buf[*len] & 0x80) == 0) {
      *pvalue = buf[*len];
      (*len)++;
      return 0;
   }
   if ((buf[*len] & 0x40) == 0) {
      if (*len == limit - 1)
         return -1;
      *pvalue = (buf[*len] & 0x3F) << 8;
      (*len)++;
      *pvalue |= buf[*len];
      (*len)++;
      return 0;
   }
   *pvalue = (buf[*len] & 0x3F) << 14;
   (*len)++;
   /* We have a fragment.  Currently we don't process fragments. */
   ast_debug(1, "UDPTL packet with length greater than 16K received, decoding will fail\n");
   return 1;
}
static int decode_open_type ( uint8_t *  buf,
unsigned int  limit,
unsigned int *  len,
const uint8_t **  p_object,
unsigned int *  p_num_octets 
) [static]

Definition at line 224 of file udptl.c.

References decode_length(), and len().

Referenced by udptl_rx_packet().

{
   unsigned int octet_cnt = 0;

   if (decode_length(buf, limit, len, &octet_cnt) != 0)
      return -1;

   if (octet_cnt > 0) {
      /* Make sure the buffer contains at least the number of bits requested */
      if ((*len + octet_cnt) > limit)
         return -1;

      *p_num_octets = octet_cnt;
      *p_object = &buf[*len];
      *len += octet_cnt;
   }

   return 0;
}
static unsigned int encode_length ( uint8_t *  buf,
unsigned int *  len,
unsigned int  value 
) [static]

Definition at line 245 of file udptl.c.

References len().

Referenced by encode_open_type(), and udptl_build_packet().

{
   unsigned int multiplier;

   if (value < 0x80) {
      /* 1 octet */
      buf[*len] = value;
      (*len)++;
      return value;
   }
   if (value < 0x4000) {
      /* 2 octets */
      /* Set the first bit of the first octet */
      buf[*len] = ((0x8000 | value) >> 8) & 0xFF;
      (*len)++;
      buf[*len] = value & 0xFF;
      (*len)++;
      return value;
   }
   /* Fragmentation */
   multiplier = (value < 0x10000) ? (value >> 14) : 4;
   /* Set the first 2 bits of the octet */
   buf[*len] = 0xC0 | multiplier;
   (*len)++;
   return multiplier << 14;
}
static int encode_open_type ( const struct ast_udptl udptl,
uint8_t *  buf,
unsigned int  buflen,
unsigned int *  len,
const uint8_t *  data,
unsigned int  num_octets 
) [static]

Definition at line 273 of file udptl.c.

References ast_log(), encode_length(), LOG_ERROR, and LOG_TAG.

Referenced by udptl_build_packet().

{
   unsigned int enclen;
   unsigned int octet_idx;
   uint8_t zero_byte;

   /* If open type is of zero length, add a single zero byte (10.1) */
   if (num_octets == 0) {
      zero_byte = 0;
      data = &zero_byte;
      num_octets = 1;
   }
   /* Encode the open type */
   for (octet_idx = 0; ; num_octets -= enclen, octet_idx += enclen) {
      if ((enclen = encode_length(buf, len, num_octets)) < 0)
         return -1;
      if (enclen + *len > buflen) {
         ast_log(LOG_ERROR, "(%s): Buffer overflow detected (%d + %d > %d)\n",
            LOG_TAG(udptl), enclen, *len, buflen);
         return -1;
      }
      if (enclen > 0) {
         memcpy(&buf[*len], &data[octet_idx], enclen);
         *len += enclen;
      }
      if (enclen >= num_octets)
         break;
   }

   return 0;
}
static struct ast_udptl_protocol* get_proto ( struct ast_channel chan) [static, read]

Definition at line 1125 of file udptl.c.

References AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, ast_channel::tech, ast_channel_tech::type, and ast_udptl_protocol::type.

Referenced by ast_udptl_bridge().

{
   struct ast_udptl_protocol *cur = NULL;

   AST_RWLIST_RDLOCK(&protos);
   AST_RWLIST_TRAVERSE(&protos, cur, list) {
      if (cur->type == chan->tech->type)
         break;
   }
   AST_RWLIST_UNLOCK(&protos);

   return cur;
}
static char* handle_cli_udptl_set_debug ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
) [static]

Definition at line 1264 of file udptl.c.

References ast_cli_args::argc, ast_cli_args::argv, ast_cli(), ast_gethostbyname(), ast_inet_ntoa(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, hp, udptldebugaddr, and ast_cli_entry::usage.

{
   struct hostent *hp;
   struct ast_hostent ahp;
   int port;
   char *p;
   char *arg;

   switch (cmd) {
   case CLI_INIT:
      e->command = "udptl set debug {on|off|ip}";
      e->usage = 
         "Usage: udptl set debug {on|off|ip host[:port]}\n"
         "       Enable or disable dumping of UDPTL packets.\n"
         "       If ip is specified, limit the dumped packets to those to and from\n"
         "       the specified 'host' with optional port.\n";
      return NULL;
   case CLI_GENERATE:
      return NULL;
   }

   if (a->argc < 4 || a->argc > 5)
      return CLI_SHOWUSAGE;

   if (a->argc == 4) {
      if (!strncasecmp(a->argv[3], "on", 2)) {
         udptldebug = 1;
         memset(&udptldebugaddr, 0, sizeof(udptldebugaddr));
         ast_cli(a->fd, "UDPTL Debugging Enabled\n");
      } else if (!strncasecmp(a->argv[3], "off", 3)) {
         udptldebug = 0;
         ast_cli(a->fd, "UDPTL Debugging Disabled\n");
      } else {
         return CLI_SHOWUSAGE;
      }
   } else {
      if (strncasecmp(a->argv[3], "ip", 2))
         return CLI_SHOWUSAGE;
      port = 0;
      arg = a->argv[4];
      p = strstr(arg, ":");
      if (p) {
         *p = '\0';
         p++;
         port = atoi(p);
      }
      hp = ast_gethostbyname(arg, &ahp);
      if (hp == NULL)
         return CLI_SHOWUSAGE;
      udptldebugaddr.sin_family = AF_INET;
      memcpy(&udptldebugaddr.sin_addr, hp->h_addr, sizeof(udptldebugaddr.sin_addr));
      udptldebugaddr.sin_port = htons(port);
      if (port == 0)
         ast_cli(a->fd, "UDPTL Debugging Enabled for IP: %s\n", ast_inet_ntoa(udptldebugaddr.sin_addr));
      else
         ast_cli(a->fd, "UDPTL Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(udptldebugaddr.sin_addr), port);
      udptldebug = 1;
   }

   return CLI_SUCCESS;
}
static int udptl_build_packet ( struct ast_udptl s,
uint8_t *  buf,
unsigned int  buflen,
uint8_t *  ifp,
unsigned int  ifp_len 
) [static]

Definition at line 518 of file udptl.c.

References ast_debug, 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, LOG_TAG, seq, ast_udptl::tx, ast_udptl::tx_seq_no, UDPTL_BUF_MASK, UDPTL_ERROR_CORRECTION_FEC, UDPTL_ERROR_CORRECTION_NONE, and UDPTL_ERROR_CORRECTION_REDUNDANCY.

Referenced by ast_udptl_write().

{
   uint8_t fec[LOCAL_FAX_MAX_DATAGRAM * 2];
   int i;
   int j;
   int seq;
   int entry;
   int entries;
   int span;
   int m;
   unsigned int len;
   int limit;
   int high_tide;

   seq = s->tx_seq_no & 0xFFFF;

   /* Map the sequence number to an entry in the circular buffer */
   entry = seq & UDPTL_BUF_MASK;

   /* We save the message in a circular buffer, for generating FEC or
      redundancy sets later on. */
   s->tx[entry].buf_len = ifp_len;
   memcpy(s->tx[entry].buf, ifp, ifp_len);
   
   /* Build the UDPTLPacket */

   len = 0;
   /* Encode the sequence number */
   buf[len++] = (seq >> 8) & 0xFF;
   buf[len++] = seq & 0xFF;

   /* Encode the primary IFP packet */
   if (encode_open_type(s, buf, buflen, &len, ifp, ifp_len) < 0)
      return -1;

   /* Encode the appropriate type of error recovery information */
   switch (s->error_correction_scheme)
   {
   case UDPTL_ERROR_CORRECTION_NONE:
      /* Encode the error recovery type */
      buf[len++] = 0x00;
      /* The number of entries will always be zero, so it is pointless allowing
         for the fragmented case here. */
      if (encode_length(buf, &len, 0) < 0)
         return -1;
      break;
   case UDPTL_ERROR_CORRECTION_REDUNDANCY:
      /* Encode the error recovery type */
      buf[len++] = 0x00;
      if (s->tx_seq_no > s->error_correction_entries)
         entries = s->error_correction_entries;
      else
         entries = s->tx_seq_no;
      /* The number of entries will always be small, so it is pointless allowing
         for the fragmented case here. */
      if (encode_length(buf, &len, entries) < 0)
         return -1;
      /* Encode the elements */
      for (i = 0; i < entries; i++) {
         j = (entry - i - 1) & UDPTL_BUF_MASK;
         if (encode_open_type(s, buf, buflen, &len, s->tx[j].buf, s->tx[j].buf_len) < 0) {
            ast_debug(1, "(%s): Encoding failed at i=%d, j=%d\n",
                 LOG_TAG(s), i, j);
            return -1;
         }
      }
      break;
   case UDPTL_ERROR_CORRECTION_FEC:
      span = s->error_correction_span;
      entries = s->error_correction_entries;
      if (seq < s->error_correction_span*s->error_correction_entries) {
         /* In the initial stages, wind up the FEC smoothly */
         entries = seq/s->error_correction_span;
         if (seq < s->error_correction_span)
            span = 0;
      }
      /* Encode the error recovery type */
      buf[len++] = 0x80;
      /* Span is defined as an inconstrained integer, which it dumb. It will only
         ever be a small value. Treat it as such. */
      buf[len++] = 1;
      buf[len++] = span;
      /* The number of entries is defined as a length, but will only ever be a small
         value. Treat it as such. */
      buf[len++] = entries;
      for (m = 0; m < entries; m++) {
         /* Make an XOR'ed entry the maximum length */
         limit = (entry + m) & UDPTL_BUF_MASK;
         high_tide = 0;
         for (i = (limit - span*entries) & UDPTL_BUF_MASK; i != limit; i = (i + entries) & UDPTL_BUF_MASK) {
            if (high_tide < s->tx[i].buf_len) {
               for (j = 0; j < high_tide; j++)
                  fec[j] ^= s->tx[i].buf[j];
               for ( ; j < s->tx[i].buf_len; j++)
                  fec[j] = s->tx[i].buf[j];
               high_tide = s->tx[i].buf_len;
            } else {
               for (j = 0; j < s->tx[i].buf_len; j++)
                  fec[j] ^= s->tx[i].buf[j];
            }
         }
         if (encode_open_type(s, buf, buflen, &len, fec, high_tide) < 0)
            return -1;
      }
      break;
   }

   s->tx_seq_no++;
   return len;
}
static int udptl_debug_test_addr ( const struct sockaddr_in *  addr) [inline, static]

Definition at line 185 of file udptl.c.

References udptldebugaddr.

Referenced by ast_udptl_read(), and ast_udptl_write().

{
   if (udptldebug == 0)
      return 0;
   if (udptldebugaddr.sin_addr.s_addr) {
      if (((ntohs(udptldebugaddr.sin_port) != 0) &&
           (udptldebugaddr.sin_port != addr->sin_port)) ||
          (udptldebugaddr.sin_addr.s_addr != addr->sin_addr.s_addr))
         return 0;
   }
   return 1;
}
static int udptl_rx_packet ( struct ast_udptl s,
uint8_t *  buf,
unsigned int  len 
) [static]

Definition at line 307 of file udptl.c.

References ARRAY_LEN, 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, MAX_FEC_ENTRIES, ast_frame::offset, ast_frame::ptr, 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().

{
   int stat1;
   int stat2;
   int i;
   int j;
   int k;
   int l;
   int m;
   int x;
   int limit;
   int which;
   unsigned int ptr;
   unsigned int count;
   int total_count;
   int seq_no;
   const uint8_t *ifp;
   const uint8_t *data;
   unsigned int ifp_len;
   int repaired[16];
   const uint8_t *bufs[ARRAY_LEN(s->f) - 1];
   unsigned int lengths[ARRAY_LEN(s->f) - 1];
   int span;
   int entries;
   int ifp_no;

   ptr = 0;
   ifp_no = 0;
   memset(&s->f[0], 0, sizeof(s->f[0]));

   /* Decode seq_number */
   if (ptr + 2 > len)
      return -1;
   seq_no = (buf[0] << 8) | buf[1];
   ptr += 2;

   /* Break out the primary packet */
   if ((stat1 = decode_open_type(buf, len, &ptr, &ifp, &ifp_len)) != 0)
      return -1;
   /* Decode error_recovery */
   if (ptr + 1 > len)
      return -1;
   if ((buf[ptr++] & 0x80) == 0) {
      /* Secondary packet mode for error recovery */
      if (seq_no > s->rx_seq_no) {
         /* We received a later packet than we expected, so we need to check if we can fill in the gap from the
            secondary packets. */
         total_count = 0;
         do {
            if ((stat2 = decode_length(buf, len, &ptr, &count)) < 0)
               return -1;
            for (i = 0; i < count && total_count + i < ARRAY_LEN(bufs); i++) {
               if ((stat1 = decode_open_type(buf, len, &ptr, &bufs[total_count + i], &lengths[total_count + i])) != 0)
                  return -1;
            }
            total_count += i;
         }
         while (stat2 > 0 && total_count < ARRAY_LEN(bufs));
         /* Step through in reverse order, so we go oldest to newest */
         for (i = total_count; i > 0; i--) {
            if (seq_no - i >= s->rx_seq_no) {
               /* This one wasn't seen before */
               /* Decode the secondary IFP packet */
               //fprintf(stderr, "Secondary %d, len %d\n", seq_no - i, lengths[i - 1]);
               s->f[ifp_no].frametype = AST_FRAME_MODEM;
               s->f[ifp_no].subclass = AST_MODEM_T38;

               s->f[ifp_no].mallocd = 0;
               s->f[ifp_no].seqno = seq_no - i;
               s->f[ifp_no].datalen = lengths[i - 1];
               s->f[ifp_no].data.ptr = (uint8_t *) bufs[i - 1];
               s->f[ifp_no].offset = 0;
               s->f[ifp_no].src = "UDPTL";
               if (ifp_no > 0)
                  AST_LIST_NEXT(&s->f[ifp_no - 1], frame_list) = &s->f[ifp_no];
               AST_LIST_NEXT(&s->f[ifp_no], frame_list) = NULL;
               ifp_no++;
            }
         }
      }
   }
   else
   {
      /* FEC mode for error recovery */
      /* Our buffers cannot tolerate overlength IFP packets in FEC mode */
      if (ifp_len > LOCAL_FAX_MAX_DATAGRAM)
         return -1;
      /* Update any missed slots in the buffer */
      for ( ; seq_no > s->rx_seq_no; s->rx_seq_no++) {
         x = s->rx_seq_no & UDPTL_BUF_MASK;
         s->rx[x].buf_len = -1;
         s->rx[x].fec_len[0] = 0;
         s->rx[x].fec_span = 0;
         s->rx[x].fec_entries = 0;
      }

      x = seq_no & UDPTL_BUF_MASK;

      memset(repaired, 0, sizeof(repaired));

      /* Save the new IFP packet */
      memcpy(s->rx[x].buf, ifp, ifp_len);
      s->rx[x].buf_len = ifp_len;
      repaired[x] = TRUE;

      /* Decode the FEC packets */
      /* The span is defined as an unconstrained integer, but will never be more
         than a small value. */
      if (ptr + 2 > len)
         return -1;
      if (buf[ptr++] != 1)
         return -1;
      span = buf[ptr++];
      s->rx[x].fec_span = span;

      /* The number of entries is defined as a length, but will only ever be a small
         value. Treat it as such. */
      if (ptr + 1 > len)
         return -1;
      entries = buf[ptr++];
      if (entries > MAX_FEC_ENTRIES) {
         return -1;
      }
      s->rx[x].fec_entries = entries;

      /* Decode the elements */
      for (i = 0; i < entries; i++) {
         if ((stat1 = decode_open_type(buf, len, &ptr, &data, &s->rx[x].fec_len[i])) != 0)
            return -1;
         if (s->rx[x].fec_len[i] > LOCAL_FAX_MAX_DATAGRAM)
            return -1;

         /* Save the new FEC data */
         memcpy(s->rx[x].fec[i], data, s->rx[x].fec_len[i]);
#if 0
         fprintf(stderr, "FEC: ");
         for (j = 0; j < s->rx[x].fec_len[i]; j++)
            fprintf(stderr, "%02X ", data[j]);
         fprintf(stderr, "\n");
#endif
      }

      /* See if we can reconstruct anything which is missing */
      /* TODO: this does not comprehensively hunt back and repair everything that is possible */
      for (l = x; l != ((x - (16 - span*entries)) & UDPTL_BUF_MASK); l = (l - 1) & UDPTL_BUF_MASK) {
         if (s->rx[l].fec_len[0] <= 0)
            continue;
         for (m = 0; m < s->rx[l].fec_entries; m++) {
            limit = (l + m) & UDPTL_BUF_MASK;
            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) {
               if (s->rx[k].buf_len <= 0)
                  which = (which == -1) ? k : -2;
            }
            if (which >= 0) {
               /* Repairable */
               for (j = 0; j < s->rx[l].fec_len[m]; j++) {
                  s->rx[which].buf[j] = s->rx[l].fec[m][j];
                  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)
                     s->rx[which].buf[j] ^= (s->rx[k].buf_len > j) ? s->rx[k].buf[j] : 0;
               }
               s->rx[which].buf_len = s->rx[l].fec_len[m];
               repaired[which] = TRUE;
            }
         }
      }
      /* Now play any new packets forwards in time */
      for (l = (x + 1) & UDPTL_BUF_MASK, j = seq_no - UDPTL_BUF_MASK; l != x; l = (l + 1) & UDPTL_BUF_MASK, j++) {
         if (repaired[l]) {
            //fprintf(stderr, "Fixed packet %d, len %d\n", j, l);
            s->f[ifp_no].frametype = AST_FRAME_MODEM;
            s->f[ifp_no].subclass = AST_MODEM_T38;
         
            s->f[ifp_no].mallocd = 0;
            s->f[ifp_no].seqno = j;
            s->f[ifp_no].datalen = s->rx[l].buf_len;
            s->f[ifp_no].data.ptr = s->rx[l].buf;
            s->f[ifp_no].offset = 0;
            s->f[ifp_no].src = "UDPTL";
            if (ifp_no > 0)
               AST_LIST_NEXT(&s->f[ifp_no - 1], frame_list) = &s->f[ifp_no];
            AST_LIST_NEXT(&s->f[ifp_no], frame_list) = NULL;
            ifp_no++;
         }
      }
   }

   /* If packets are received out of sequence, we may have already processed this packet from the error
      recovery information in a packet already received. */
   if (seq_no >= s->rx_seq_no) {
      /* Decode the primary IFP packet */
      s->f[ifp_no].frametype = AST_FRAME_MODEM;
      s->f[ifp_no].subclass = AST_MODEM_T38;
      
      s->f[ifp_no].mallocd = 0;
      s->f[ifp_no].seqno = seq_no;
      s->f[ifp_no].datalen = ifp_len;
      s->f[ifp_no].data.ptr = (uint8_t *) ifp;
      s->f[ifp_no].offset = 0;
      s->f[ifp_no].src = "UDPTL";
      if (ifp_no > 0)
         AST_LIST_NEXT(&s->f[ifp_no - 1], frame_list) = &s->f[ifp_no];
      AST_LIST_NEXT(&s->f[ifp_no], frame_list) = NULL;

      ifp_no++;
   }

   s->rx_seq_no = seq_no + 1;
   return ifp_no;
}
static int udptlread ( int *  id,
int  fd,
short  events,
void *  cbdata 
) [static]

Definition at line 649 of file udptl.c.

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

Referenced by ast_udptl_new_with_bindaddr().

{
   struct ast_udptl *udptl = cbdata;
   struct ast_frame *f;

   if ((f = ast_udptl_read(udptl))) {
      if (udptl->callback)
         udptl->callback(udptl, f, udptl->data);
   }
   return 1;
}

Variable Documentation

struct ast_cli_entry cli_udptl[] [static]
Initial value:
 {
   AST_CLI_DEFINE(handle_cli_udptl_set_debug, "Enable/Disable UDPTL debugging")
}

Definition at line 1327 of file udptl.c.

struct protos protos [static]
int udptldebug [static]

Are we debugging?

Definition at line 84 of file udptl.c.

struct sockaddr_in udptldebugaddr [static]

Debug packets to/from this host

Definition at line 85 of file udptl.c.

Referenced by handle_cli_udptl_set_debug(), and udptl_debug_test_addr().

int udptlend = 4599 [static]

Definition at line 83 of file udptl.c.

Referenced by __ast_udptl_reload(), and ast_udptl_new_with_bindaddr().

int udptlfecentries [static]

Definition at line 89 of file udptl.c.

Referenced by ast_udptl_new_with_bindaddr().

int udptlfecspan [static]

Definition at line 90 of file udptl.c.

Referenced by ast_udptl_new_with_bindaddr().

int udptlstart = 4500 [static]

Definition at line 82 of file udptl.c.

Referenced by __ast_udptl_reload(), and ast_udptl_new_with_bindaddr().

int use_even_ports [static]

Definition at line 91 of file udptl.c.