2 * Asterisk -- A telephony toolkit for Linux.
4 * UDPTL support for T.38
6 * Copyright (C) 2005, Steve Underwood, partly based on RTP code which is
7 * Copyright (C) 1999-2006, Digium, Inc.
9 * Steve Underwood <steveu@coppice.org>
11 * See http://www.asterisk.org for more information about
12 * the Asterisk project. Please do not directly contact
13 * any of the maintainers of this project for assistance;
14 * the project provides a web site, mailing lists and IRC
15 * channels for your use.
17 * This program is free software, distributed under the terms of
18 * the GNU General Public License Version 2. See the LICENSE file
19 * at the top of the source tree.
21 * A license has been granted to Digium (via disclaimer) for the use of
28 * \brief UDPTL support for T.38 faxing
31 * \author Mark Spencer <markster@digium.com>, Steve Underwood <steveu@coppice.org>
33 * \page T38fax_udptl T.38 support :: UDPTL
35 * Asterisk supports T.38 fax passthrough, origination and termination. It does
36 * not support gateway operation. The only channel driver that supports T.38 at
37 * this time is chan_sip.
39 * UDPTL is handled very much like RTP. It can be reinvited to go directly between
40 * the endpoints, without involving Asterisk in the media stream.
51 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
57 #include "asterisk/udptl.h"
58 #include "asterisk/frame.h"
59 #include "asterisk/channel.h"
60 #include "asterisk/acl.h"
61 #include "asterisk/config.h"
62 #include "asterisk/lock.h"
63 #include "asterisk/utils.h"
64 #include "asterisk/netsock.h"
65 #include "asterisk/cli.h"
66 #include "asterisk/unaligned.h"
68 #define UDPTL_MTU 1200
77 static int udptlstart = 4500;
78 static int udptlend = 4599;
79 static int udptldebug; /*!< Are we debugging? */
80 static struct sockaddr_in udptldebugaddr; /*!< Debug packets to/from this host */
82 static int nochecksums;
84 static enum ast_t38_ec_modes udptlfectype;
85 static int udptlfecentries;
86 static int udptlfecspan;
87 static int udptlmaxdatagram;
88 static int use_even_ports;
90 #define LOCAL_FAX_MAX_DATAGRAM 1400
91 #define MAX_FEC_ENTRIES 5
92 #define MAX_FEC_SPAN 5
94 #define UDPTL_BUF_MASK 15
98 uint8_t buf[LOCAL_FAX_MAX_DATAGRAM];
99 } udptl_fec_tx_buffer_t;
103 uint8_t buf[LOCAL_FAX_MAX_DATAGRAM];
104 unsigned int fec_len[MAX_FEC_ENTRIES];
105 uint8_t fec[MAX_FEC_ENTRIES][LOCAL_FAX_MAX_DATAGRAM];
106 unsigned int fec_span;
107 unsigned int fec_entries;
108 } udptl_fec_rx_buffer_t;
110 /*! \brief Structure for an UDPTL session */
114 struct ast_frame f[16];
115 unsigned char rawdata[8192 + AST_FRIENDLY_OFFSET];
116 unsigned int lasteventseqn;
119 struct sockaddr_in us;
120 struct sockaddr_in them;
122 struct sched_context *sched;
123 struct io_context *io;
125 ast_udptl_callback callback;
127 /*! This option indicates the error correction scheme used in transmitted UDPTL
128 * packets and expected in received UDPTL packets.
130 enum ast_t38_ec_modes error_correction_scheme;
132 /*! This option indicates the number of error correction entries transmitted in
133 * UDPTL packets and expected in received UDPTL packets.
135 unsigned int error_correction_entries;
137 /*! This option indicates the span of the error correction entries in transmitted
138 * UDPTL packets (FEC only).
140 unsigned int error_correction_span;
142 /*! The maximum size UDPTL packet that can be accepted by
145 unsigned int far_max_datagram;
147 /*! The maximum size UDPTL packet that we are prepared to
150 unsigned int local_max_datagram;
152 /*! The maximum IFP that can be submitted for sending
153 * to the remote device. Calculated from far_max_datagram,
154 * error_correction_scheme and error_correction_entries.
156 unsigned int far_max_ifp;
158 /*! The maximum IFP that the local endpoint is prepared
159 * to accept. Along with error_correction_scheme and
160 * error_correction_entries, used to calculate local_max_datagram.
162 unsigned int local_max_ifp;
166 struct sockaddr_in far;
168 unsigned int tx_seq_no;
169 unsigned int rx_seq_no;
170 unsigned int rx_expected_seq_no;
172 udptl_fec_tx_buffer_t tx[UDPTL_BUF_MASK + 1];
173 udptl_fec_rx_buffer_t rx[UDPTL_BUF_MASK + 1];
176 static AST_RWLIST_HEAD_STATIC(protos, ast_udptl_protocol);
178 static inline int udptl_debug_test_addr(const struct sockaddr_in *addr)
182 if (udptldebugaddr.sin_addr.s_addr) {
183 if (((ntohs(udptldebugaddr.sin_port) != 0) &&
184 (udptldebugaddr.sin_port != addr->sin_port)) ||
185 (udptldebugaddr.sin_addr.s_addr != addr->sin_addr.s_addr))
191 static int decode_length(uint8_t *buf, unsigned int limit, unsigned int *len, unsigned int *pvalue)
195 if ((buf[*len] & 0x80) == 0) {
200 if ((buf[*len] & 0x40) == 0) {
201 if (*len == limit - 1)
203 *pvalue = (buf[*len] & 0x3F) << 8;
205 *pvalue |= buf[*len];
209 *pvalue = (buf[*len] & 0x3F) << 14;
211 /* Indicate we have a fragment */
214 /*- End of function --------------------------------------------------------*/
216 static int decode_open_type(uint8_t *buf, unsigned int limit, unsigned int *len, const uint8_t **p_object, unsigned int *p_num_octets)
218 unsigned int octet_cnt;
219 unsigned int octet_idx;
222 const uint8_t **pbuf;
224 for (octet_idx = 0, *p_num_octets = 0; ; octet_idx += octet_cnt) {
226 if ((length = decode_length(buf, limit, len, &octet_cnt)) < 0)
229 *p_num_octets += octet_cnt;
231 pbuf = &p_object[octet_idx];
233 /* Make sure the buffer contains at least the number of bits requested */
234 if ((*len + octet_cnt) > limit)
245 /*- End of function --------------------------------------------------------*/
247 static unsigned int encode_length(uint8_t *buf, unsigned int *len, unsigned int value)
249 unsigned int multiplier;
257 if (value < 0x4000) {
259 /* Set the first bit of the first octet */
260 buf[*len] = ((0x8000 | value) >> 8) & 0xFF;
262 buf[*len] = value & 0xFF;
267 multiplier = (value < 0x10000) ? (value >> 14) : 4;
268 /* Set the first 2 bits of the octet */
269 buf[*len] = 0xC0 | multiplier;
271 return multiplier << 14;
273 /*- End of function --------------------------------------------------------*/
275 static int encode_open_type(uint8_t *buf, unsigned int buflen, unsigned int *len, const uint8_t *data, unsigned int num_octets)
278 unsigned int octet_idx;
281 /* If open type is of zero length, add a single zero byte (10.1) */
282 if (num_octets == 0) {
287 /* Encode the open type */
288 for (octet_idx = 0; ; num_octets -= enclen, octet_idx += enclen) {
289 if ((enclen = encode_length(buf, len, num_octets)) < 0)
291 if (enclen + *len > buflen) {
292 ast_log(LOG_ERROR, "Buffer overflow detected (%d + %d > %d)\n", enclen, *len, buflen);
296 memcpy(&buf[*len], &data[octet_idx], enclen);
299 if (enclen >= num_octets)
305 /*- End of function --------------------------------------------------------*/
307 static int udptl_rx_packet(struct ast_udptl *s, uint8_t *buf, unsigned int len)
325 unsigned int ifp_len;
327 const uint8_t *bufs[16];
328 unsigned int lengths[16];
335 memset(&s->f[0], 0, sizeof(s->f[0]));
337 /* Decode seq_number */
340 seq_no = (buf[0] << 8) | buf[1];
343 /* Break out the primary packet */
344 if ((stat1 = decode_open_type(buf, len, &ptr, &ifp, &ifp_len)) != 0)
346 /* Decode error_recovery */
349 if ((buf[ptr++] & 0x80) == 0) {
350 /* Secondary packet mode for error recovery */
351 if (seq_no > s->rx_seq_no) {
352 /* We received a later packet than we expected, so we need to check if we can fill in the gap from the
353 secondary packets. */
356 if ((stat2 = decode_length(buf, len, &ptr, &count)) < 0)
358 for (i = 0; i < count; i++) {
359 if ((stat1 = decode_open_type(buf, len, &ptr, &bufs[total_count + i], &lengths[total_count + i])) != 0)
362 total_count += count;
365 /* Step through in reverse order, so we go oldest to newest */
366 for (i = total_count; i > 0; i--) {
367 if (seq_no - i >= s->rx_seq_no) {
368 /* This one wasn't seen before */
369 /* Decode the secondary IFP packet */
370 //fprintf(stderr, "Secondary %d, len %d\n", seq_no - i, lengths[i - 1]);
371 s->f[ifp_no].frametype = AST_FRAME_MODEM;
372 s->f[ifp_no].subclass = AST_MODEM_T38;
374 s->f[ifp_no].mallocd = 0;
375 s->f[ifp_no].seqno = seq_no - i;
376 s->f[ifp_no].datalen = lengths[i - 1];
377 s->f[ifp_no].data.ptr = (uint8_t *) bufs[i - 1];
378 s->f[ifp_no].offset = 0;
379 s->f[ifp_no].src = "UDPTL";
381 AST_LIST_NEXT(&s->f[ifp_no - 1], frame_list) = &s->f[ifp_no];
382 AST_LIST_NEXT(&s->f[ifp_no], frame_list) = NULL;
390 /* FEC mode for error recovery */
391 /* Our buffers cannot tolerate overlength IFP packets in FEC mode */
392 if (ifp_len > LOCAL_FAX_MAX_DATAGRAM)
394 /* Update any missed slots in the buffer */
395 for ( ; seq_no > s->rx_seq_no; s->rx_seq_no++) {
396 x = s->rx_seq_no & UDPTL_BUF_MASK;
397 s->rx[x].buf_len = -1;
398 s->rx[x].fec_len[0] = 0;
399 s->rx[x].fec_span = 0;
400 s->rx[x].fec_entries = 0;
403 x = seq_no & UDPTL_BUF_MASK;
405 memset(repaired, 0, sizeof(repaired));
407 /* Save the new IFP packet */
408 memcpy(s->rx[x].buf, ifp, ifp_len);
409 s->rx[x].buf_len = ifp_len;
412 /* Decode the FEC packets */
413 /* The span is defined as an unconstrained integer, but will never be more
414 than a small value. */
420 s->rx[x].fec_span = span;
422 /* The number of entries is defined as a length, but will only ever be a small
423 value. Treat it as such. */
426 entries = buf[ptr++];
427 s->rx[x].fec_entries = entries;
429 /* Decode the elements */
430 for (i = 0; i < entries; i++) {
431 if ((stat1 = decode_open_type(buf, len, &ptr, &data, &s->rx[x].fec_len[i])) != 0)
433 if (s->rx[x].fec_len[i] > LOCAL_FAX_MAX_DATAGRAM)
436 /* Save the new FEC data */
437 memcpy(s->rx[x].fec[i], data, s->rx[x].fec_len[i]);
439 fprintf(stderr, "FEC: ");
440 for (j = 0; j < s->rx[x].fec_len[i]; j++)
441 fprintf(stderr, "%02X ", data[j]);
442 fprintf(stderr, "\n");
446 /* See if we can reconstruct anything which is missing */
447 /* TODO: this does not comprehensively hunt back and repair everything that is possible */
448 for (l = x; l != ((x - (16 - span*entries)) & UDPTL_BUF_MASK); l = (l - 1) & UDPTL_BUF_MASK) {
449 if (s->rx[l].fec_len[0] <= 0)
451 for (m = 0; m < s->rx[l].fec_entries; m++) {
452 limit = (l + m) & UDPTL_BUF_MASK;
453 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) {
454 if (s->rx[k].buf_len <= 0)
455 which = (which == -1) ? k : -2;
459 for (j = 0; j < s->rx[l].fec_len[m]; j++) {
460 s->rx[which].buf[j] = s->rx[l].fec[m][j];
461 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)
462 s->rx[which].buf[j] ^= (s->rx[k].buf_len > j) ? s->rx[k].buf[j] : 0;
464 s->rx[which].buf_len = s->rx[l].fec_len[m];
465 repaired[which] = TRUE;
469 /* Now play any new packets forwards in time */
470 for (l = (x + 1) & UDPTL_BUF_MASK, j = seq_no - UDPTL_BUF_MASK; l != x; l = (l + 1) & UDPTL_BUF_MASK, j++) {
472 //fprintf(stderr, "Fixed packet %d, len %d\n", j, l);
473 s->f[ifp_no].frametype = AST_FRAME_MODEM;
474 s->f[ifp_no].subclass = AST_MODEM_T38;
476 s->f[ifp_no].mallocd = 0;
477 s->f[ifp_no].seqno = j;
478 s->f[ifp_no].datalen = s->rx[l].buf_len;
479 s->f[ifp_no].data.ptr = s->rx[l].buf;
480 s->f[ifp_no].offset = 0;
481 s->f[ifp_no].src = "UDPTL";
483 AST_LIST_NEXT(&s->f[ifp_no - 1], frame_list) = &s->f[ifp_no];
484 AST_LIST_NEXT(&s->f[ifp_no], frame_list) = NULL;
490 /* If packets are received out of sequence, we may have already processed this packet from the error
491 recovery information in a packet already received. */
492 if (seq_no >= s->rx_seq_no) {
493 /* Decode the primary IFP packet */
494 s->f[ifp_no].frametype = AST_FRAME_MODEM;
495 s->f[ifp_no].subclass = AST_MODEM_T38;
497 s->f[ifp_no].mallocd = 0;
498 s->f[ifp_no].seqno = seq_no;
499 s->f[ifp_no].datalen = ifp_len;
500 s->f[ifp_no].data.ptr = (uint8_t *) ifp;
501 s->f[ifp_no].offset = 0;
502 s->f[ifp_no].src = "UDPTL";
504 AST_LIST_NEXT(&s->f[ifp_no - 1], frame_list) = &s->f[ifp_no];
505 AST_LIST_NEXT(&s->f[ifp_no], frame_list) = NULL;
510 s->rx_seq_no = seq_no + 1;
513 /*- End of function --------------------------------------------------------*/
515 static int udptl_build_packet(struct ast_udptl *s, uint8_t *buf, unsigned int buflen, uint8_t *ifp, unsigned int ifp_len)
517 uint8_t fec[LOCAL_FAX_MAX_DATAGRAM * 2];
529 seq = s->tx_seq_no & 0xFFFF;
531 /* Map the sequence number to an entry in the circular buffer */
532 entry = seq & UDPTL_BUF_MASK;
534 /* We save the message in a circular buffer, for generating FEC or
535 redundancy sets later on. */
536 s->tx[entry].buf_len = ifp_len;
537 memcpy(s->tx[entry].buf, ifp, ifp_len);
539 /* Build the UDPTLPacket */
542 /* Encode the sequence number */
543 buf[len++] = (seq >> 8) & 0xFF;
544 buf[len++] = seq & 0xFF;
546 /* Encode the primary IFP packet */
547 if (encode_open_type(buf, buflen, &len, ifp, ifp_len) < 0)
550 /* Encode the appropriate type of error recovery information */
551 switch (s->error_correction_scheme)
553 case UDPTL_ERROR_CORRECTION_NONE:
554 /* Encode the error recovery type */
556 /* The number of entries will always be zero, so it is pointless allowing
557 for the fragmented case here. */
558 if (encode_length(buf, &len, 0) < 0)
561 case UDPTL_ERROR_CORRECTION_REDUNDANCY:
562 /* Encode the error recovery type */
564 if (s->tx_seq_no > s->error_correction_entries)
565 entries = s->error_correction_entries;
567 entries = s->tx_seq_no;
568 /* The number of entries will always be small, so it is pointless allowing
569 for the fragmented case here. */
570 if (encode_length(buf, &len, entries) < 0)
572 /* Encode the elements */
573 for (i = 0; i < entries; i++) {
574 j = (entry - i - 1) & UDPTL_BUF_MASK;
575 if (encode_open_type(buf, buflen, &len, s->tx[j].buf, s->tx[j].buf_len) < 0) {
577 ast_log(LOG_DEBUG, "Encoding failed at i=%d, j=%d\n", i, j);
583 case UDPTL_ERROR_CORRECTION_FEC:
584 span = s->error_correction_span;
585 entries = s->error_correction_entries;
586 if (seq < s->error_correction_span*s->error_correction_entries) {
587 /* In the initial stages, wind up the FEC smoothly */
588 entries = seq/s->error_correction_span;
589 if (seq < s->error_correction_span)
592 /* Encode the error recovery type */
594 /* Span is defined as an inconstrained integer, which it dumb. It will only
595 ever be a small value. Treat it as such. */
598 /* The number of entries is defined as a length, but will only ever be a small
599 value. Treat it as such. */
600 buf[len++] = entries;
601 for (m = 0; m < entries; m++) {
602 /* Make an XOR'ed entry the maximum length */
603 limit = (entry + m) & UDPTL_BUF_MASK;
605 for (i = (limit - span*entries) & UDPTL_BUF_MASK; i != limit; i = (i + entries) & UDPTL_BUF_MASK) {
606 if (high_tide < s->tx[i].buf_len) {
607 for (j = 0; j < high_tide; j++)
608 fec[j] ^= s->tx[i].buf[j];
609 for ( ; j < s->tx[i].buf_len; j++)
610 fec[j] = s->tx[i].buf[j];
611 high_tide = s->tx[i].buf_len;
613 for (j = 0; j < s->tx[i].buf_len; j++)
614 fec[j] ^= s->tx[i].buf[j];
617 if (encode_open_type(buf, buflen, &len, fec, high_tide) < 0)
624 fprintf(stderr, "\n");
630 int ast_udptl_fd(const struct ast_udptl *udptl)
635 void ast_udptl_set_data(struct ast_udptl *udptl, void *data)
640 void ast_udptl_set_callback(struct ast_udptl *udptl, ast_udptl_callback callback)
642 udptl->callback = callback;
645 void ast_udptl_setnat(struct ast_udptl *udptl, int nat)
650 static int udptlread(int *id, int fd, short events, void *cbdata)
652 struct ast_udptl *udptl = cbdata;
655 if ((f = ast_udptl_read(udptl))) {
657 udptl->callback(udptl, f, udptl->data);
662 struct ast_frame *ast_udptl_read(struct ast_udptl *udptl)
665 struct sockaddr_in sin;
668 uint16_t *udptlheader;
672 /* Cache where the header will go */
673 res = recvfrom(udptl->fd,
674 udptl->rawdata + AST_FRIENDLY_OFFSET,
675 sizeof(udptl->rawdata) - AST_FRIENDLY_OFFSET,
677 (struct sockaddr *) &sin,
679 udptlheader = (uint16_t *)(udptl->rawdata + AST_FRIENDLY_OFFSET);
682 ast_log(LOG_WARNING, "UDPTL read error: %s\n", strerror(errno));
683 ast_assert(errno != EBADF);
684 return &ast_null_frame;
687 /* Ignore if the other side hasn't been given an address yet. */
688 if (!udptl->them.sin_addr.s_addr || !udptl->them.sin_port)
689 return &ast_null_frame;
692 /* Send to whoever sent to us */
693 if ((udptl->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
694 (udptl->them.sin_port != sin.sin_port)) {
695 memcpy(&udptl->them, &sin, sizeof(udptl->them));
696 ast_debug(1, "UDPTL NAT: Using address %s:%d\n", ast_inet_ntoa(udptl->them.sin_addr), ntohs(udptl->them.sin_port));
700 if (udptl_debug_test_addr(&sin)) {
701 ast_verb(1, "Got UDPTL packet from %s:%d (type %d, seq %d, len %d)\n",
702 ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), 0, seqno, res);
705 printf("Got UDPTL packet from %s:%d (seq %d, len = %d)\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), seqno, res);
707 if (udptl_rx_packet(udptl, udptl->rawdata + AST_FRIENDLY_OFFSET, res) < 1)
708 return &ast_null_frame;
713 static void calculate_local_max_datagram(struct ast_udptl *udptl)
715 unsigned int new_max = 200;
717 /* calculate the amount of space required to receive an IFP
718 * using the current error correction mode, and ensure that our
719 * local max datagram size is at least that big
721 switch (udptl->error_correction_scheme) {
722 case UDPTL_ERROR_CORRECTION_NONE:
723 /* only need room for sequence number and length indicators */
724 new_max = 6 + udptl->local_max_ifp;
726 case UDPTL_ERROR_CORRECTION_REDUNDANCY:
727 /* need room for sequence number, length indicators and the
728 * configured number of redundant packets
730 new_max = 6 + udptl->local_max_ifp + 2 + (udptl->error_correction_entries * udptl->local_max_ifp);
732 case UDPTL_ERROR_CORRECTION_FEC:
733 /* need room for sequence number, length indicators and a
734 * a single IFP of the maximum size expected
736 new_max = 6 + udptl->local_max_ifp + 4 + udptl->local_max_ifp;
739 /* add 25% of extra space for insurance, but no larger than LOCAL_FAX_MAX_DATAGRAM */
740 udptl->local_max_datagram = MIN(new_max * 1.25, LOCAL_FAX_MAX_DATAGRAM);
743 static void calculate_far_max_ifp(struct ast_udptl *udptl)
745 unsigned new_max = 60;
747 /* calculate the maximum IFP the local endpoint should
748 * generate based on the far end's maximum datagram size
749 * and the current error correction mode. some endpoints
750 * bogus 'max datagram' values that would result in unusable
751 * (too small) maximum IFP values, so we have a a reasonable
752 * minimum value to ensure that we can actually construct
755 switch (udptl->error_correction_scheme) {
756 case UDPTL_ERROR_CORRECTION_NONE:
757 /* only need room for sequence number and length indicators */
758 new_max = MAX(new_max, udptl->far_max_datagram - 6);
760 case UDPTL_ERROR_CORRECTION_REDUNDANCY:
761 /* need room for sequence number, length indicators and the
762 * configured number of redundant packets
764 new_max = MAX(new_max, (udptl->far_max_datagram - 8) / (udptl->error_correction_entries + 1));
766 case UDPTL_ERROR_CORRECTION_FEC:
767 /* need room for sequence number, length indicators and a
768 * a single IFP of the maximum size expected
770 new_max = MAX(new_max, (udptl->far_max_datagram - 10) / 2);
773 /* subtract 25% of space for insurance */
774 udptl->far_max_ifp = new_max * 0.75;
777 enum ast_t38_ec_modes ast_udptl_get_error_correction_scheme(const struct ast_udptl *udptl)
780 return udptl->error_correction_scheme;
782 ast_log(LOG_WARNING, "udptl structure is null\n");
787 void ast_udptl_set_error_correction_scheme(struct ast_udptl *udptl, enum ast_t38_ec_modes ec)
790 udptl->error_correction_scheme = ec;
792 case UDPTL_ERROR_CORRECTION_FEC:
793 udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_FEC;
794 if (udptl->error_correction_entries == 0) {
795 udptl->error_correction_entries = 3;
797 if (udptl->error_correction_span == 0) {
798 udptl->error_correction_span = 3;
801 case UDPTL_ERROR_CORRECTION_REDUNDANCY:
802 udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_REDUNDANCY;
803 if (udptl->error_correction_entries == 0) {
804 udptl->error_correction_entries = 3;
811 calculate_local_max_datagram(udptl);
812 calculate_far_max_ifp(udptl);
814 ast_log(LOG_WARNING, "udptl structure is null\n");
817 unsigned int ast_udptl_get_local_max_datagram(const struct ast_udptl *udptl)
820 return udptl->local_max_datagram;
822 ast_log(LOG_WARNING, "udptl structure is null\n");
827 unsigned int ast_udptl_get_far_max_datagram(const struct ast_udptl *udptl)
830 return udptl->far_max_datagram;
832 ast_log(LOG_WARNING, "udptl structure is null\n");
837 void ast_udptl_set_far_max_datagram(struct ast_udptl *udptl, unsigned int max_datagram)
840 udptl->far_max_datagram = max_datagram;
841 calculate_far_max_ifp(udptl);
843 ast_log(LOG_WARNING, "udptl structure is null\n");
847 void ast_udptl_set_local_max_ifp(struct ast_udptl *udptl, unsigned int max_ifp)
849 udptl->local_max_ifp = max_ifp;
850 calculate_local_max_datagram(udptl);
853 unsigned int ast_udptl_get_far_max_ifp(const struct ast_udptl *udptl)
855 return udptl->far_max_ifp;
858 struct ast_udptl *ast_udptl_new_with_bindaddr(struct sched_context *sched, struct io_context *io, int callbackmode, struct in_addr addr)
860 struct ast_udptl *udptl;
866 if (!(udptl = ast_calloc(1, sizeof(*udptl))))
869 udptl->error_correction_scheme = udptlfectype;
870 udptl->error_correction_span = udptlfecspan;
871 udptl->error_correction_entries = udptlfecentries;
873 udptl->far_max_datagram = udptlmaxdatagram;
874 udptl->local_max_datagram = udptlmaxdatagram;
876 for (i = 0; i <= UDPTL_BUF_MASK; i++) {
877 udptl->rx[i].buf_len = -1;
878 udptl->tx[i].buf_len = -1;
881 udptl->them.sin_family = AF_INET;
882 udptl->us.sin_family = AF_INET;
884 if ((udptl->fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
886 ast_log(LOG_WARNING, "Unable to allocate socket: %s\n", strerror(errno));
889 flags = fcntl(udptl->fd, F_GETFL);
890 fcntl(udptl->fd, F_SETFL, flags | O_NONBLOCK);
893 setsockopt(udptl->fd, SOL_SOCKET, SO_NO_CHECK, &nochecksums, sizeof(nochecksums));
895 /* Find us a place */
896 x = (udptlstart == udptlend) ? udptlstart : (ast_random() % (udptlend - udptlstart)) + udptlstart;
897 if (use_even_ports && (x & 1)) {
902 udptl->us.sin_port = htons(x);
903 udptl->us.sin_addr = addr;
904 if (bind(udptl->fd, (struct sockaddr *) &udptl->us, sizeof(udptl->us)) == 0)
906 if (errno != EADDRINUSE) {
907 ast_log(LOG_WARNING, "Unexpected bind error: %s\n", strerror(errno));
912 if (use_even_ports) {
919 if (x == startplace) {
920 ast_log(LOG_WARNING, "No UDPTL ports remaining\n");
926 if (io && sched && callbackmode) {
927 /* Operate this one in a callback mode */
928 udptl->sched = sched;
930 udptl->ioid = ast_io_add(udptl->io, udptl->fd, udptlread, AST_IO_IN, udptl);
935 struct ast_udptl *ast_udptl_new(struct sched_context *sched, struct io_context *io, int callbackmode)
938 memset(&ia, 0, sizeof(ia));
939 return ast_udptl_new_with_bindaddr(sched, io, callbackmode, ia);
942 int ast_udptl_setqos(struct ast_udptl *udptl, unsigned int tos, unsigned int cos)
944 return ast_netsock_set_qos(udptl->fd, tos, cos, "UDPTL");
947 void ast_udptl_set_peer(struct ast_udptl *udptl, const struct sockaddr_in *them)
949 udptl->them.sin_port = them->sin_port;
950 udptl->them.sin_addr = them->sin_addr;
953 void ast_udptl_get_peer(const struct ast_udptl *udptl, struct sockaddr_in *them)
955 memset(them, 0, sizeof(*them));
956 them->sin_family = AF_INET;
957 them->sin_port = udptl->them.sin_port;
958 them->sin_addr = udptl->them.sin_addr;
961 void ast_udptl_get_us(const struct ast_udptl *udptl, struct sockaddr_in *us)
963 memcpy(us, &udptl->us, sizeof(udptl->us));
966 void ast_udptl_stop(struct ast_udptl *udptl)
968 memset(&udptl->them.sin_addr, 0, sizeof(udptl->them.sin_addr));
969 memset(&udptl->them.sin_port, 0, sizeof(udptl->them.sin_port));
972 void ast_udptl_destroy(struct ast_udptl *udptl)
975 ast_io_remove(udptl->io, udptl->ioid);
981 int ast_udptl_write(struct ast_udptl *s, struct ast_frame *f)
986 uint8_t buf[s->far_max_datagram];
988 /* If we have no peer, return immediately */
989 if (s->them.sin_addr.s_addr == INADDR_ANY)
992 /* If there is no data length, return immediately */
996 if ((f->frametype != AST_FRAME_MODEM) ||
997 (f->subclass != AST_MODEM_T38)) {
998 ast_log(LOG_WARNING, "UDPTL can only send T.38 data.\n");
1002 if (f->datalen > s->far_max_ifp) {
1003 ast_log(LOG_WARNING, "UDPTL asked to send %d bytes of IFP when far end only prepared to accept %d bytes; data loss may occur.\n", f->datalen, s->far_max_ifp);
1006 /* Save seq_no for debug output because udptl_build_packet increments it */
1007 seq = s->tx_seq_no & 0xFFFF;
1009 /* Cook up the UDPTL packet, with the relevant EC info. */
1010 len = udptl_build_packet(s, buf, sizeof(buf), f->data.ptr, f->datalen);
1012 if (len > 0 && s->them.sin_port && s->them.sin_addr.s_addr) {
1013 if ((res = sendto(s->fd, buf, len, 0, (struct sockaddr *) &s->them, sizeof(s->them))) < 0)
1014 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));
1016 printf("Sent %d bytes of UDPTL data to %s:%d\n", res, ast_inet_ntoa(udptl->them.sin_addr), ntohs(udptl->them.sin_port));
1018 if (udptl_debug_test_addr(&s->them))
1019 ast_verb(1, "Sent UDPTL packet to %s:%d (type %d, seq %d, len %d)\n",
1020 ast_inet_ntoa(s->them.sin_addr),
1021 ntohs(s->them.sin_port), 0, seq, len);
1027 void ast_udptl_proto_unregister(struct ast_udptl_protocol *proto)
1029 AST_RWLIST_WRLOCK(&protos);
1030 AST_RWLIST_REMOVE(&protos, proto, list);
1031 AST_RWLIST_UNLOCK(&protos);
1034 int ast_udptl_proto_register(struct ast_udptl_protocol *proto)
1036 struct ast_udptl_protocol *cur;
1038 AST_RWLIST_WRLOCK(&protos);
1039 AST_RWLIST_TRAVERSE(&protos, cur, list) {
1040 if (cur->type == proto->type) {
1041 ast_log(LOG_WARNING, "Tried to register same protocol '%s' twice\n", cur->type);
1042 AST_RWLIST_UNLOCK(&protos);
1046 AST_RWLIST_INSERT_TAIL(&protos, proto, list);
1047 AST_RWLIST_UNLOCK(&protos);
1051 static struct ast_udptl_protocol *get_proto(struct ast_channel *chan)
1053 struct ast_udptl_protocol *cur = NULL;
1055 AST_RWLIST_RDLOCK(&protos);
1056 AST_RWLIST_TRAVERSE(&protos, cur, list) {
1057 if (cur->type == chan->tech->type)
1060 AST_RWLIST_UNLOCK(&protos);
1065 int ast_udptl_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
1067 struct ast_frame *f;
1068 struct ast_channel *who;
1069 struct ast_channel *cs[3];
1070 struct ast_udptl *p0;
1071 struct ast_udptl *p1;
1072 struct ast_udptl_protocol *pr0;
1073 struct ast_udptl_protocol *pr1;
1074 struct sockaddr_in ac0;
1075 struct sockaddr_in ac1;
1076 struct sockaddr_in t0;
1077 struct sockaddr_in t1;
1082 ast_channel_lock(c0);
1083 while (ast_channel_trylock(c1)) {
1084 ast_channel_unlock(c0);
1086 ast_channel_lock(c0);
1088 pr0 = get_proto(c0);
1089 pr1 = get_proto(c1);
1091 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c0->name);
1092 ast_channel_unlock(c0);
1093 ast_channel_unlock(c1);
1097 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c1->name);
1098 ast_channel_unlock(c0);
1099 ast_channel_unlock(c1);
1102 pvt0 = c0->tech_pvt;
1103 pvt1 = c1->tech_pvt;
1104 p0 = pr0->get_udptl_info(c0);
1105 p1 = pr1->get_udptl_info(c1);
1107 /* Somebody doesn't want to play... */
1108 ast_channel_unlock(c0);
1109 ast_channel_unlock(c1);
1112 if (pr0->set_udptl_peer(c0, p1)) {
1113 ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c0->name, c1->name);
1114 memset(&ac1, 0, sizeof(ac1));
1116 /* Store UDPTL peer */
1117 ast_udptl_get_peer(p1, &ac1);
1119 if (pr1->set_udptl_peer(c1, p0)) {
1120 ast_log(LOG_WARNING, "Channel '%s' failed to talk back to '%s'\n", c1->name, c0->name);
1121 memset(&ac0, 0, sizeof(ac0));
1123 /* Store UDPTL peer */
1124 ast_udptl_get_peer(p0, &ac0);
1126 ast_channel_unlock(c0);
1127 ast_channel_unlock(c1);
1132 if ((c0->tech_pvt != pvt0) ||
1133 (c1->tech_pvt != pvt1) ||
1134 (c0->masq || c0->masqr || c1->masq || c1->masqr)) {
1135 ast_debug(1, "Oooh, something is weird, backing out\n");
1136 /* Tell it to try again later */
1140 ast_udptl_get_peer(p1, &t1);
1141 ast_udptl_get_peer(p0, &t0);
1142 if (inaddrcmp(&t1, &ac1)) {
1143 ast_debug(1, "Oooh, '%s' changed end address to %s:%d\n",
1144 c1->name, ast_inet_ntoa(t1.sin_addr), ntohs(t1.sin_port));
1145 ast_debug(1, "Oooh, '%s' was %s:%d\n",
1146 c1->name, ast_inet_ntoa(ac1.sin_addr), ntohs(ac1.sin_port));
1147 memcpy(&ac1, &t1, sizeof(ac1));
1149 if (inaddrcmp(&t0, &ac0)) {
1150 ast_debug(1, "Oooh, '%s' changed end address to %s:%d\n",
1151 c0->name, ast_inet_ntoa(t0.sin_addr), ntohs(t0.sin_port));
1152 ast_debug(1, "Oooh, '%s' was %s:%d\n",
1153 c0->name, ast_inet_ntoa(ac0.sin_addr), ntohs(ac0.sin_port));
1154 memcpy(&ac0, &t0, sizeof(ac0));
1156 who = ast_waitfor_n(cs, 2, &to);
1158 ast_debug(1, "Ooh, empty read...\n");
1159 /* check for hangup / whentohangup */
1160 if (ast_check_hangup(c0) || ast_check_hangup(c1))
1168 ast_debug(1, "Oooh, got a %s\n", f ? "digit" : "hangup");
1169 /* That's all we needed */
1172 if (f->frametype == AST_FRAME_MODEM) {
1173 /* Forward T.38 frames if they happen upon us */
1176 } else if (who == c1) {
1182 /* Swap priority. Not that it's a big deal at this point */
1190 static char *handle_cli_udptl_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1193 struct ast_hostent ahp;
1200 e->command = "udptl set debug {on|off|ip}";
1202 "Usage: udptl set debug {on|off|ip host[:port]}\n"
1203 " Enable or disable dumping of UDPTL packets.\n"
1204 " If ip is specified, limit the dumped packets to those to and from\n"
1205 " the specified 'host' with optional port.\n";
1211 if (a->argc < 4 || a->argc > 5)
1212 return CLI_SHOWUSAGE;
1215 if (!strncasecmp(a->argv[3], "on", 2)) {
1217 memset(&udptldebugaddr, 0, sizeof(udptldebugaddr));
1218 ast_cli(a->fd, "UDPTL Debugging Enabled\n");
1219 } else if (!strncasecmp(a->argv[3], "off", 3)) {
1221 ast_cli(a->fd, "UDPTL Debugging Disabled\n");
1223 return CLI_SHOWUSAGE;
1226 if (strncasecmp(a->argv[3], "ip", 2))
1227 return CLI_SHOWUSAGE;
1229 arg = ast_strdupa(a->argv[4]);
1230 p = strstr(arg, ":");
1236 hp = ast_gethostbyname(arg, &ahp);
1238 return CLI_SHOWUSAGE;
1239 udptldebugaddr.sin_family = AF_INET;
1240 memcpy(&udptldebugaddr.sin_addr, hp->h_addr, sizeof(udptldebugaddr.sin_addr));
1241 udptldebugaddr.sin_port = htons(port);
1243 ast_cli(a->fd, "UDPTL Debugging Enabled for IP: %s\n", ast_inet_ntoa(udptldebugaddr.sin_addr));
1245 ast_cli(a->fd, "UDPTL Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(udptldebugaddr.sin_addr), port);
1253 static struct ast_cli_entry cli_udptl[] = {
1254 AST_CLI_DEFINE(handle_cli_udptl_set_debug, "Enable/Disable UDPTL debugging")
1257 static void __ast_udptl_reload(int reload)
1259 struct ast_config *cfg;
1261 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
1263 cfg = ast_config_load2("udptl.conf", "udptl", config_flags);
1264 if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID) {
1270 udptlfectype = UDPTL_ERROR_CORRECTION_NONE;
1271 udptlfecentries = 0;
1273 udptlmaxdatagram = 0;
1277 if ((s = ast_variable_retrieve(cfg, "general", "udptlstart"))) {
1278 udptlstart = atoi(s);
1279 if (udptlstart < 1024) {
1280 ast_log(LOG_WARNING, "Ports under 1024 are not allowed for T.38.\n");
1283 if (udptlstart > 65535) {
1284 ast_log(LOG_WARNING, "Ports over 65535 are invalid.\n");
1288 if ((s = ast_variable_retrieve(cfg, "general", "udptlend"))) {
1290 if (udptlend < 1024) {
1291 ast_log(LOG_WARNING, "Ports under 1024 are not allowed for T.38.\n");
1294 if (udptlend > 65535) {
1295 ast_log(LOG_WARNING, "Ports over 65535 are invalid.\n");
1299 if ((s = ast_variable_retrieve(cfg, "general", "udptlchecksums"))) {
1307 ast_log(LOG_WARNING, "Disabling UDPTL checksums is not supported on this operating system!\n");
1310 if ((s = ast_variable_retrieve(cfg, "general", "T38FaxUdpEC"))) {
1311 if (strcmp(s, "t38UDPFEC") == 0)
1312 udptlfectype = UDPTL_ERROR_CORRECTION_FEC;
1313 else if (strcmp(s, "t38UDPRedundancy") == 0)
1314 udptlfectype = UDPTL_ERROR_CORRECTION_REDUNDANCY;
1316 if ((s = ast_variable_retrieve(cfg, "general", "T38FaxMaxDatagram"))) {
1317 udptlmaxdatagram = atoi(s);
1318 if (udptlmaxdatagram < 100) {
1319 ast_log(LOG_WARNING, "Too small T38FaxMaxDatagram size. Defaulting to 100.\n");
1320 udptlmaxdatagram = 100;
1322 if (udptlmaxdatagram > LOCAL_FAX_MAX_DATAGRAM) {
1323 ast_log(LOG_WARNING, "Too large T38FaxMaxDatagram size. Defaulting to %d.\n", LOCAL_FAX_MAX_DATAGRAM);
1324 udptlmaxdatagram = LOCAL_FAX_MAX_DATAGRAM;
1327 if ((s = ast_variable_retrieve(cfg, "general", "UDPTLFECentries"))) {
1328 udptlfecentries = atoi(s);
1329 if (udptlfecentries < 1) {
1330 ast_log(LOG_WARNING, "Too small UDPTLFECentries value. Defaulting to 1.\n");
1331 udptlfecentries = 1;
1333 if (udptlfecentries > MAX_FEC_ENTRIES) {
1334 ast_log(LOG_WARNING, "Too large UDPTLFECentries value. Defaulting to %d.\n", MAX_FEC_ENTRIES);
1335 udptlfecentries = MAX_FEC_ENTRIES;
1338 if ((s = ast_variable_retrieve(cfg, "general", "UDPTLFECspan"))) {
1339 udptlfecspan = atoi(s);
1340 if (udptlfecspan < 1) {
1341 ast_log(LOG_WARNING, "Too small UDPTLFECspan value. Defaulting to 1.\n");
1344 if (udptlfecspan > MAX_FEC_SPAN) {
1345 ast_log(LOG_WARNING, "Too large UDPTLFECspan value. Defaulting to %d.\n", MAX_FEC_SPAN);
1346 udptlfecspan = MAX_FEC_SPAN;
1349 if ((s = ast_variable_retrieve(cfg, "general", "use_even_ports"))) {
1350 use_even_ports = ast_true(s);
1352 ast_config_destroy(cfg);
1354 if (udptlstart >= udptlend) {
1355 ast_log(LOG_WARNING, "Unreasonable values for UDPTL start/end\n");
1359 if (use_even_ports && (udptlstart & 1)) {
1361 ast_log(LOG_NOTICE, "Odd numbered udptlstart specified but use_even_ports enabled. udptlstart is now %d\n", udptlstart);
1363 if (use_even_ports && (udptlend & 1)) {
1365 ast_log(LOG_NOTICE, "Odd numbered udptlend specified but use_event_ports enabled. udptlend is now %d\n", udptlend);
1367 ast_verb(2, "UDPTL allocating from port range %d -> %d\n", udptlstart, udptlend);
1370 int ast_udptl_reload(void)
1372 __ast_udptl_reload(1);
1376 void ast_udptl_init(void)
1378 ast_cli_register_multiple(cli_udptl, ARRAY_LEN(cli_udptl));
1379 __ast_udptl_reload(0);