[svn-commits] blanchet: branch blanchet/v6 r60453 -
/team/blanchet/v6/main/udptl.c
svn-commits at lists.digium.com
svn-commits at lists.digium.com
Fri Apr 6 09:15:13 MST 2007
Author: blanchet
Date: Fri Apr 6 11:15:12 2007
New Revision: 60453
URL: http://svn.digium.com/view/asterisk?view=rev&rev=60453
Log:
- new definitions of ast_viudptl routines and definitions for IP version independent routines. vi = version independent.
- see README-IPV6.txt to see the reasoning on why duplicating code and using another namespace: it enables incremental conversion of channels to IPv6. But has drawbacks.
- note well: while the RTP code was tested over both Ipv4 and Ipv6. However, this converted T.38 code was not tested even over IPv4.
Modified:
team/blanchet/v6/main/udptl.c
Modified: team/blanchet/v6/main/udptl.c
URL: http://svn.digium.com/view/asterisk/team/blanchet/v6/main/udptl.c?view=diff&rev=60453&r1=60452&r2=60453
==============================================================================
--- team/blanchet/v6/main/udptl.c (original)
+++ team/blanchet/v6/main/udptl.c Fri Apr 6 11:15:12 2007
@@ -1157,6 +1157,10 @@
udptl_nodebug, NULL,
NULL };
+/* forward declaration of version independent versions of debug functions */
+static int viudptl_do_debug_ip(int fd, int argc, char *argv[]);
+static int viudptl_do_debug(int fd, int argc, char *argv[]);
+
static struct ast_cli_entry cli_udptl[] = {
{ { "udptl", "debug", NULL },
udptl_do_debug, "Enable UDPTL debugging",
@@ -1169,6 +1173,15 @@
{ { "udptl", "debug", "off", NULL },
udptl_nodebug, "Disable UDPTL debugging",
nodebug_usage, NULL, &cli_udptl_no_debug },
+
+/* temporary new cmds to debug version independent mode */
+ { { "udptl", "videbug", NULL },
+ viudptl_do_debug, "Enable UDPTL debugging",
+ debug_usage },
+
+ { { "udptl", "videbug", "ip", NULL },
+ viudptl_do_debug, "Enable UDPTL debugging on IP",
+ debug_usage },
};
void ast_udptl_reload(void)
@@ -1252,3 +1265,976 @@
ast_cli_register_multiple(cli_udptl, sizeof(cli_udptl) / sizeof(struct ast_cli_entry));
ast_udptl_reload();
}
+
+/*!
+ *
+ * New versions of udptl routines for IP version independence(IPv4 and IPv6)
+ * new routines start with ast_viudptl
+ * using a new namespace to avoid conflict with current code
+ * so any channel and module can transition to IPv6 one at a time.
+ * duplicate code however is not automatically synched with changes
+ *
+ * \author Marc Blanchet <marc.blanchet at viagenie.ca>
+ * Copyright (C) 2006, Viagenie, Inc.
+ */
+
+#include "asterisk/netsock.h"
+
+static struct sockaddr_storage viudptldebugaddr; /* Debug packets to/from this host */
+static socklen_t viudptldebugaddr_len = 0;
+
+struct ast_viudptl {
+ int fd;
+ char resp;
+ struct ast_frame f[16];
+ unsigned char rawdata[8192 + AST_FRIENDLY_OFFSET];
+ unsigned int lasteventseqn;
+ int nat;
+ int flags;
+ struct sockaddr_storage us;
+ socklen_t us_len;
+ struct sockaddr_storage them;
+ socklen_t them_len;
+ int *ioid;
+ uint16_t seqno;
+ struct sched_context *sched;
+ struct io_context *io;
+ void *data;
+ ast_viudptl_callback callback;
+ int udptl_offered_from_local;
+
+ /*! This option indicates the error correction scheme used in transmitted UDPTL
+ packets. */
+ int error_correction_scheme;
+
+ /*! This option indicates the number of error correction entries transmitted in
+ UDPTL packets. */
+ int error_correction_entries;
+
+ /*! This option indicates the span of the error correction entries in transmitted
+ UDPTL packets (FEC only). */
+ int error_correction_span;
+
+ /*! This option indicates the maximum size of a UDPTL packet that can be accepted by
+ the remote device. */
+ int far_max_datagram_size;
+
+ /*! This option indicates the maximum size of a UDPTL packet that we are prepared to
+ accept. */
+ int local_max_datagram_size;
+
+ int verbose;
+
+ struct sockaddr_storage far;
+ socklen_t farlen;
+
+ int tx_seq_no;
+ int rx_seq_no;
+ int rx_expected_seq_no;
+
+ udptl_fec_tx_buffer_t tx[UDPTL_BUF_MASK + 1];
+ udptl_fec_rx_buffer_t rx[UDPTL_BUF_MASK + 1];
+};
+
+static struct ast_viudptl_protocol *viprotos;
+
+static int viudptl_rx_packet(struct ast_viudptl *s, uint8_t *buf, int len);
+static int viudptl_build_packet(struct ast_viudptl *s, uint8_t *buf, uint8_t *ifp, int ifp_len);
+
+static inline int viudptl_debug_test_addr(struct sockaddr *addr, socklen_t addr_len)
+{
+ if (udptldebug == 0)
+ return 0;
+
+ if (viudptldebugaddr_len) {
+ if (((ast_vinetsock_sa_getport((struct sockaddr*)&viudptldebugaddr, viudptldebugaddr_len) != 0)
+ && ast_vinetsock_sacmp_port((struct sockaddr*)&viudptldebugaddr, viudptldebugaddr_len, addr, addr_len))
+ || ast_vinetsock_sacmp((struct sockaddr*)&viudptldebugaddr, viudptldebugaddr_len, addr, addr_len, 1))
+ return 0;
+ }
+ return 1;
+}
+
+static int viudptl_rx_packet(struct ast_viudptl *s, uint8_t *buf, int len)
+{
+ int stat;
+ int stat2;
+ int i;
+ int j;
+ int k;
+ int l;
+ int m;
+ int x;
+ int limit;
+ int which;
+ int ptr;
+ int count;
+ int total_count;
+ int seq_no;
+ const uint8_t *ifp;
+ const uint8_t *data;
+ int ifp_len;
+ int repaired[16];
+ const uint8_t *bufs[16];
+ int lengths[16];
+ 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 ((stat = 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; i++) {
+ if ((stat = decode_open_type(buf, len, &ptr, &bufs[total_count + i], &lengths[total_count + i])) != 0)
+ return -1;
+ }
+ total_count += count;
+ }
+ while (stat2 > 0);
+ /* 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].???seq_no = seq_no - i;
+ s->f[ifp_no].datalen = lengths[i - 1];
+ s->f[ifp_no].data = (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++;
+ }
+ }
+ }
+ /* 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].???seq_no = seq_no;
+ s->f[ifp_no].datalen = ifp_len;
+ s->f[ifp_no].data = (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;
+ }
+ }
+ 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++];
+ s->rx[x].fec_entries = entries;
+
+ /* Decode the elements */
+ for (i = 0; i < entries; i++) {
+ if ((stat = 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].???seq_no = j;
+ s->f[ifp_no].datalen = s->rx[l].buf_len;
+ s->f[ifp_no].data = 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++;
+ }
+ }
+ /* 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].???seq_no = j;
+ s->f[ifp_no].datalen = ifp_len;
+ s->f[ifp_no].data = (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;
+ }
+
+ s->rx_seq_no = seq_no + 1;
+ return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int viudptl_build_packet(struct ast_viudptl *s, uint8_t *buf, uint8_t *ifp, int ifp_len)
+{
+ uint8_t fec[LOCAL_FAX_MAX_DATAGRAM];
+ int i;
+ int j;
+ int seq;
+ int entry;
+ int entries;
+ int span;
+ int m;
+ 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(buf, &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(buf, &len, s->tx[j].buf, s->tx[j].buf_len) < 0)
+ 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(buf, &len, fec, high_tide) < 0)
+ return -1;
+ }
+ break;
+ }
+
+ if (s->verbose)
+ fprintf(stderr, "\n");
+
+ s->tx_seq_no++;
+ return len;
+}
+
+int ast_viudptl_fd(struct ast_viudptl *udptl)
+{
+ return udptl->fd;
+}
+
+void ast_viudptl_set_data(struct ast_viudptl *udptl, void *data)
+{
+ udptl->data = data;
+}
+
+void ast_viudptl_set_callback(struct ast_viudptl *udptl, ast_viudptl_callback callback)
+{
+ udptl->callback = callback;
+}
+
+void ast_viudptl_setnat(struct ast_viudptl *udptl, int nat)
+{
+ udptl->nat = nat;
+}
+
+static int viudptlread(int *id, int fd, short events, void *cbdata)
+{
+ struct ast_viudptl *udptl = cbdata;
+ struct ast_frame *f;
+
+ if ((f = ast_viudptl_read(udptl))) {
+ if (udptl->callback)
+ udptl->callback(udptl, f, udptl->data);
+ }
+ return 1;
+}
+
+struct ast_frame *ast_viudptl_read(struct ast_viudptl *udptl)
+{
+ int res;
+ struct sockaddr_storage sa;
+ socklen_t sa_len=sizeof(sa);
+ uint16_t seqno = 0;
+ uint16_t *udptlheader;
+ char hostport[NI_MAXHOST];
+
+
+ /* Cache where the header will go */
+ res = recvfrom(udptl->fd,
+ udptl->rawdata + AST_FRIENDLY_OFFSET,
+ sizeof(udptl->rawdata) - AST_FRIENDLY_OFFSET,
+ 0,
+ (struct sockaddr *) &sa,
+ &sa_len);
+ udptlheader = (uint16_t *)(udptl->rawdata + AST_FRIENDLY_OFFSET);
+ if (res < 0) {
+ if (errno != EAGAIN)
+ ast_log(LOG_WARNING, "UDPTL read error: %s\n", strerror(errno));
+ if (errno == EBADF)
+ CRASH;
+ return &ast_null_frame;
+ }
+
+ /* Ignore if the other side hasn't been given an address yet. */
+ if (!udptl->them_len || !ast_vinetsock_sa_getport((struct sockaddr*)&udptl->them,udptl->them_len))
+ return &ast_null_frame;
+
+ ast_vinetsock_sa_get_hostport((struct sockaddr*)&udptl->them, udptl->them_len, hostport, sizeof(hostport));
+
+ if (udptl->nat) {
+ /* Send to whoever sent to us */
+ if ((ast_vinetsock_sacmp((struct sockaddr *)&udptl->them, udptl->them_len,(struct sockaddr *)&sa, sa_len, 1)) ||
+ (ast_vinetsock_sacmp_port((struct sockaddr *)&udptl->them, udptl->them_len,(struct sockaddr *)&sa, sa_len))) {
+ memcpy(&udptl->them, &sa, sizeof(udptl->them));
+ ast_log(LOG_DEBUG, "UDPTL NAT: Using address %s\n", hostport);
+ }
+ }
+
+ ast_vinetsock_sa_get_hostport((struct sockaddr*)&sa, sa_len, hostport, sizeof(hostport));
+
+ if (viudptl_debug_test_addr((struct sockaddr*)&sa, sa_len)) {
+ ast_verbose("Got UDPTL packet from %s (type %d, seq %d, len %d)\n",
+ hostport, 0, seqno, res);
+ }
+#if 0
+ printf("Got UDPTL packet from %s (seq %d, len = %d)\n", hostport, seqno, res);
+#endif
+ viudptl_rx_packet(udptl, udptl->rawdata + AST_FRIENDLY_OFFSET, res);
+
+ return &udptl->f[0];
+}
+
+void ast_viudptl_offered_from_local(struct ast_viudptl* udptl, int local)
+{
+ if (udptl)
+ udptl->udptl_offered_from_local = local;
+ else
+ ast_log(LOG_WARNING, "udptl structure is null\n");
+}
+
+int ast_viudptl_get_error_correction_scheme(struct ast_viudptl* udptl)
+{
+ if (udptl)
+ return udptl->error_correction_scheme;
+ else {
+ ast_log(LOG_WARNING, "udptl structure is null\n");
+ return -1;
+ }
+}
+
+void ast_viudptl_set_error_correction_scheme(struct ast_viudptl* udptl, int ec)
+{
+ if (udptl) {
+ switch (ec) {
+ case UDPTL_ERROR_CORRECTION_FEC:
+ udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_FEC;
+ break;
+ case UDPTL_ERROR_CORRECTION_REDUNDANCY:
+ udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_REDUNDANCY;
+ break;
+ case UDPTL_ERROR_CORRECTION_NONE:
+ udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_NONE;
+ break;
+ default:
+ ast_log(LOG_WARNING, "error correction parameter invalid\n");
+ };
+ } else
+ ast_log(LOG_WARNING, "udptl structure is null\n");
+}
+
+int ast_viudptl_get_local_max_datagram(struct ast_viudptl* udptl)
+{
+ if (udptl)
+ return udptl->local_max_datagram_size;
+ else {
+ ast_log(LOG_WARNING, "udptl structure is null\n");
+ return -1;
+ }
+}
+
+int ast_viudptl_get_far_max_datagram(struct ast_viudptl* udptl)
+{
+ if (udptl)
+ return udptl->far_max_datagram_size;
+ else {
+ ast_log(LOG_WARNING, "udptl structure is null\n");
+ return -1;
+ }
+}
+
+void ast_viudptl_set_local_max_datagram(struct ast_viudptl* udptl, int max_datagram)
+{
+ if (udptl)
+ udptl->local_max_datagram_size = max_datagram;
+ else
+ ast_log(LOG_WARNING, "udptl structure is null\n");
+}
+
+void ast_viudptl_set_far_max_datagram(struct ast_viudptl* udptl, int max_datagram)
+{
+ if (udptl)
+ udptl->far_max_datagram_size = max_datagram;
+ else
+ ast_log(LOG_WARNING, "udptl structure is null\n");
+}
+
+struct ast_viudptl *ast_viudptl_new_with_bindaddr(struct sched_context *sched, struct io_context *io, int callbackmode, struct sockaddr *addr, socklen_t addr_len)
+{
+ struct ast_viudptl *udptl;
+ int x;
+ int startplace;
+ int i;
+ long int flags;
+
+ if (!(udptl = ast_calloc(1, sizeof(*udptl))))
+ return NULL;
+ memset(&udptl->us, 0, sizeof(udptl->us));
+ memset(&udptl->them, 0, sizeof(udptl->them));
+ udptl->us_len = 0;
+ udptl->them_len = 0;
+
+ if (udptlfectype == 2)
+ udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_FEC;
+ else if (udptlfectype == 1)
+ udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_REDUNDANCY;
+ else
+ udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_NONE;
+ udptl->error_correction_span = udptlfecspan;
+ udptl->error_correction_entries = udptlfecentries;
+
+ udptl->far_max_datagram_size = udptlmaxdatagram;
+ udptl->local_max_datagram_size = udptlmaxdatagram;
+
+ memset(&udptl->rx, 0, sizeof(udptl->rx));
+ memset(&udptl->tx, 0, sizeof(udptl->tx));
+ for (i = 0; i <= UDPTL_BUF_MASK; i++) {
+ udptl->rx[i].buf_len = -1;
+ udptl->tx[i].buf_len = -1;
+ }
+
+ udptl->seqno = ast_random() & 0xffff;
+ udptl->them.ss_family = addr->sa_family;
+ udptl->us.ss_family = addr->sa_family;
+
+ if ((udptl->fd = socket(addr->sa_family, SOCK_DGRAM, 0)) < 0) {
+ 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 = (ast_random() % (udptlend - udptlstart)) + udptlstart;
+ startplace = x;
+ for (;;) {
+ ast_vinetsock_sa_setport((struct sockaddr*)&udptl->us, x);
+ memcpy(&udptl->us, addr, sizeof(udptl->us));
+ 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);
+ free(udptl);
+ return NULL;
+ }
+ if (++x > udptlend)
+ x = udptlstart;
+ if (x == startplace) {
+ ast_log(LOG_WARNING, "No UDPTL ports remaining\n");
+ close(udptl->fd);
+ 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, viudptlread, AST_IO_IN, udptl);
+ }
+ return udptl;
+}
+
+struct ast_viudptl *ast_viudptl_new(struct sched_context *sched, struct io_context *io, int callbackmode)
+{
+ struct sockaddr_storage ia;
+ socklen_t ialen;
+ memset(&ia, 0, sizeof(ia));
+ ialen = sizeof(ia);
+ return ast_viudptl_new_with_bindaddr(sched, io, callbackmode, (struct sockaddr *)&ia, ialen);
+}
+
+int ast_viudptl_settos(struct ast_viudptl *udptl, int tos)
+{
+ int res;
+
+ if ((res = setsockopt(udptl->fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos))))
+ ast_log(LOG_WARNING, "UDPTL unable to set TOS to %d\n", tos);
+ return res;
+}
+
+void ast_viudptl_set_peer(struct ast_viudptl *udptl, struct sockaddr *them, socklen_t them_len)
+{
+ ast_vinetsock_sa_setport((struct sockaddr *)&udptl->them,
+ ast_vinetsock_sa_getport(them, them_len));
+ memcpy(&udptl->them, them, sizeof(udptl->them));
+}
+
+void ast_viudptl_get_peer(struct ast_viudptl *udptl, struct sockaddr *them, socklen_t them_len)
+{
+ ast_vinetsock_sa_setport(them,
+ ast_vinetsock_sa_getport((struct sockaddr *)&udptl->them, udptl->them_len));
+ memcpy(them, &udptl->them, sizeof(*them));
+
+}
+
+void ast_viudptl_get_us(struct ast_viudptl *udptl, struct sockaddr *us, socklen_t us_len)
+{
+ memcpy(us, &udptl->us, sizeof(udptl->us));
+}
+
+void ast_viudptl_stop(struct ast_viudptl *udptl)
+{
+ memset(&udptl->them, 0, sizeof(udptl->them));
+ ast_vinetsock_sa_setport((struct sockaddr *)&udptl->them,0);
+}
+
+void ast_viudptl_destroy(struct ast_viudptl *udptl)
+{
+ if (udptl->ioid)
+ ast_io_remove(udptl->io, udptl->ioid);
+ if (udptl->fd > -1)
+ close(udptl->fd);
+ free(udptl);
+}
+
+int ast_viudptl_write(struct ast_viudptl *s, struct ast_frame *f)
+{
+ int len;
+ int res;
+ uint8_t buf[LOCAL_FAX_MAX_DATAGRAM];
+ char hostport[NI_MAXHOST];
+
+ /* If we have no peer, return immediately */
+ if (s->them_len)
+ return 0;
+
+ /* If there is no data length, return immediately */
+ if (f->datalen == 0)
+ return 0;
+
+ if (f->frametype != AST_FRAME_MODEM) {
+ ast_log(LOG_WARNING, "UDPTL can only send T.38 data\n");
+ return -1;
+ }
+
+ /* Cook up the UDPTL packet, with the relevant EC info. */
+ len = viudptl_build_packet(s, buf, f->data, f->datalen);
+
+ ast_vinetsock_sa_get_hostport((struct sockaddr*)&s->them, s->them_len, hostport, sizeof(hostport));
+ if (len > 0 && ast_vinetsock_sa_getport( (struct sockaddr*)&s->them,s->them_len)
+ && s->them_len) {
+ if ((res = sendto(s->fd, buf, len, 0, (struct sockaddr *) &s->them, sizeof(s->them))) < 0)
+ ast_log(LOG_NOTICE, "UDPTL Transmission error to %s: %s\n", hostport, strerror(errno));
+#if 0
+ printf("Sent %d bytes of UDPTL data to %s\n", res, hostport);
+#endif
+ if (viudptl_debug_test_addr((struct sockaddr *)&s->them, s->them_len))
+ ast_verbose("Sent UDPTL packet to %s (type %d, seq %d, len %d)\n",
+ hostport, 0, s->seqno, len);
+ }
+
+ return 0;
+}
+
+void ast_viudptl_proto_unregister(struct ast_viudptl_protocol *proto)
+{
+ struct ast_viudptl_protocol *cur;
+ struct ast_viudptl_protocol *prev;
+
+ cur = viprotos;
+ prev = NULL;
+ while (cur) {
+ if (cur == proto) {
+ if (prev)
+ prev->next = proto->next;
+ else
+ viprotos = proto->next;
+ return;
+ }
+ prev = cur;
+ cur = cur->next;
+ }
+}
+
+int ast_viudptl_proto_register(struct ast_viudptl_protocol *proto)
+{
+ struct ast_viudptl_protocol *cur;
+
+ cur = viprotos;
+ while (cur) {
+ if (cur->type == proto->type) {
+ ast_log(LOG_WARNING, "Tried to register same protocol '%s' twice\n", cur->type);
+ return -1;
+ }
+ cur = cur->next;
+ }
+ proto->next = viprotos;
+ viprotos = proto;
+ return 0;
+}
+static struct ast_viudptl_protocol *get_viproto(struct ast_channel *chan)
+{
+ struct ast_viudptl_protocol *cur;
+
+ cur = viprotos;
+ while (cur) {
+ if (cur->type == chan->tech->type)
+ return cur;
+ cur = cur->next;
+ }
+ return NULL;
+}
+
+int ast_viudptl_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
+{
+ struct ast_frame *f;
+ struct ast_channel *who;
+ struct ast_channel *cs[3];
+ struct ast_viudptl *p0;
+ struct ast_viudptl *p1;
+ struct ast_viudptl_protocol *pr0;
+ struct ast_viudptl_protocol *pr1;
+ struct sockaddr_storage ac0;
+ socklen_t ac0_len = sizeof(ac0);
+ struct sockaddr_storage ac1;
+ socklen_t ac1_len = sizeof(ac1);
+ struct sockaddr_storage t0;
+ socklen_t t0_len = sizeof(t0);
+ struct sockaddr_storage t1;
+ socklen_t t1_len = sizeof(t1);
+ void *pvt0;
+ void *pvt1;
+ int to;
+ char hostport[NI_MAXHOST];
+
+ ast_channel_lock(c0);
+ while (ast_channel_trylock(c1)) {
+ ast_channel_unlock(c0);
+ usleep(1);
+ ast_channel_lock(c0);
+ }
+ pr0 = get_viproto(c0);
+ pr1 = get_viproto(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);
+ } else {
+ /* Store UDPTL peer */
+ ast_viudptl_get_peer(p1, (struct sockaddr*)&ac1, ac1_len);
+ }
+ if (pr1->set_udptl_peer(c1, p0))
+ ast_log(LOG_WARNING, "Channel '%s' failed to talk back to '%s'\n", c1->name, c0->name);
+ else {
+ /* Store UDPTL peer */
+ ast_viudptl_get_peer(p0, (struct sockaddr*)&ac0, ac0_len);
+ }
+ 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_log(LOG_DEBUG, "Oooh, something is weird, backing out\n");
+ /* Tell it to try again later */
+ return -3;
+ }
+ to = -1;
+ ast_viudptl_get_peer(p1, (struct sockaddr *)&t1, t1_len);
+ ast_viudptl_get_peer(p0, (struct sockaddr *)&t0, t0_len);
+ if (ast_vinetsock_sacmp((struct sockaddr*)&t1, t1_len, (struct sockaddr*)&ac1, ac1_len, 1)) {
+ ast_vinetsock_sa_get_hostport((struct sockaddr *)&t1, t1_len, hostport, sizeof(hostport));
+
+ ast_log(LOG_DEBUG, "Oooh, '%s' changed end address to %s\n",
+ c1->name, hostport);
+ ast_vinetsock_sa_get_hostport((struct sockaddr *)&ac1, ac1_len, hostport, sizeof(hostport));
+ ast_log(LOG_DEBUG, "Oooh, '%s' was %s\n",
+ c1->name, hostport);
+ memcpy(&ac1, &t1, sizeof(ac1));
+ ac1_len = t1_len;
+ }
+ if (ast_vinetsock_sacmp((struct sockaddr*)&t0, t0_len, (struct sockaddr*)&ac0, ac0_len, 1)) {
+ ast_vinetsock_sa_get_hostport((struct sockaddr *)&t0, t0_len, hostport, sizeof(hostport));
+ ast_log(LOG_DEBUG, "Oooh, '%s' changed end address to %s\n",
+ c0->name, hostport);
+ ast_vinetsock_sa_get_hostport((struct sockaddr *)&ac0, ac0_len, hostport, sizeof(hostport));
+ ast_log(LOG_DEBUG, "Oooh, '%s' was %s\n",
+ c0->name, hostport);
+ memcpy(&ac0, &t0, sizeof(ac0));
+ ac0_len = t0_len;
+ }
+ who = ast_waitfor_n(cs, 2, &to);
+ if (!who) {
+ ast_log(LOG_DEBUG, "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_log(LOG_DEBUG, "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;
+}
+
+static int viudptl_do_debug_ip(int fd, int argc, char *argv[])
+{
+ char host[NI_MAXHOST];
+ char port[NI_MAXSERV];
+
+ if (argc != 4)
+ return RESULT_SHOWUSAGE;
+
+ if (ast_vinetsock_str2hp(host, sizeof(host), port, sizeof(port), argv[3], 1, NULL)) {
+ return RESULT_SHOWUSAGE;
+ }
+
+ if (ast_vinetsock_sa_fromstr(host, port, (struct sockaddr*)&viudptldebugaddr,
+ &viudptldebugaddr_len, AF_UNSPEC, 1)) { return RESULT_SHOWUSAGE;
+ }
+ memset(host, 0, sizeof(host));
+ ast_vinetsock_sa_get_hostport((struct sockaddr*)&viudptldebugaddr, viudptldebugaddr_len,
+ host, sizeof(host));
+
+ ast_cli(fd, "UDPTL Debugging Enabled for IP: %s\n", host);
+ udptldebug = 1;
+ return RESULT_SUCCESS;
+}
+
+static int viudptl_do_debug(int fd, int argc, char *argv[])
+{
+ if (argc != 2) {
+ if (argc != 4)
+ return RESULT_SHOWUSAGE;
+ return viudptl_do_debug_ip(fd, argc, argv);
+ }
+ udptldebug = 1;
+ memset(&udptldebugaddr,0,sizeof(udptldebugaddr));
+ ast_cli(fd, "UDPTL Debugging Enabled\n");
+ return RESULT_SUCCESS;
+}
More information about the svn-commits
mailing list