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 T38 fax passhtrough :: UDPTL
35 * Asterisk supports T.38 fax passthrough. Asterisk will not be a client, server
36 * or any form of gateway. Currently fax passthrough is only implemented in the
37 * SIP channel for strict SIP to SIP calls. If you are using chan_local or chan_agent
38 * as a proxy channel, T.38 passthrough will not work.
40 * UDPTL is handled very much like RTP. It can be reinvited to go directly between
41 * 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;
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 int udptlfectype;
85 static int udptlfecentries;
86 static int udptlfecspan;
87 static int udptlmaxdatagram;
89 #define LOCAL_FAX_MAX_DATAGRAM 400
90 #define MAX_FEC_ENTRIES 5
91 #define MAX_FEC_SPAN 5
93 #define UDPTL_BUF_MASK 15
97 uint8_t buf[LOCAL_FAX_MAX_DATAGRAM];
98 } udptl_fec_tx_buffer_t;
102 uint8_t buf[LOCAL_FAX_MAX_DATAGRAM];
103 int fec_len[MAX_FEC_ENTRIES];
104 uint8_t fec[MAX_FEC_ENTRIES][LOCAL_FAX_MAX_DATAGRAM];
107 } udptl_fec_rx_buffer_t;
109 /*! \brief Structure for an UDPTL session */
113 struct ast_frame f[16];
114 unsigned char rawdata[8192 + AST_FRIENDLY_OFFSET];
115 unsigned int lasteventseqn;
118 struct sockaddr_in us;
119 struct sockaddr_in them;
121 struct sched_context *sched;
122 struct io_context *io;
124 ast_udptl_callback callback;
125 int udptl_offered_from_local;
127 /*! This option indicates the error correction scheme used in transmitted UDPTL
129 int error_correction_scheme;
131 /*! This option indicates the number of error correction entries transmitted in
133 int error_correction_entries;
135 /*! This option indicates the span of the error correction entries in transmitted
136 UDPTL packets (FEC only). */
137 int error_correction_span;
139 /*! This option indicates the maximum size of a UDPTL packet that can be accepted by
140 the remote device. */
141 int far_max_datagram_size;
143 /*! This option indicates the maximum size of a UDPTL packet that we are prepared to
145 int local_max_datagram_size;
149 struct sockaddr_in far;
153 int rx_expected_seq_no;
155 udptl_fec_tx_buffer_t tx[UDPTL_BUF_MASK + 1];
156 udptl_fec_rx_buffer_t rx[UDPTL_BUF_MASK + 1];
159 static AST_RWLIST_HEAD_STATIC(protos, ast_udptl_protocol);
161 static int udptl_rx_packet(struct ast_udptl *s, uint8_t *buf, int len);
162 static int udptl_build_packet(struct ast_udptl *s, uint8_t *buf, uint8_t *ifp, int ifp_len);
164 static inline int udptl_debug_test_addr(struct sockaddr_in *addr)
168 if (udptldebugaddr.sin_addr.s_addr) {
169 if (((ntohs(udptldebugaddr.sin_port) != 0)
170 && (udptldebugaddr.sin_port != addr->sin_port))
171 || (udptldebugaddr.sin_addr.s_addr != addr->sin_addr.s_addr))
177 static int decode_length(uint8_t *buf, int limit, int *len, int *pvalue)
179 if ((buf[*len] & 0x80) == 0) {
186 if ((buf[*len] & 0x40) == 0) {
187 if (*len >= limit - 1)
189 *pvalue = (buf[*len] & 0x3F) << 8;
191 *pvalue |= buf[*len];
197 *pvalue = (buf[*len] & 0x3F) << 14;
199 /* Indicate we have a fragment */
202 /*- End of function --------------------------------------------------------*/
204 static int decode_open_type(uint8_t *buf, int limit, int *len, const uint8_t **p_object, int *p_num_octets)
210 const uint8_t **pbuf;
212 for (octet_idx = 0, *p_num_octets = 0; ; octet_idx += octet_cnt) {
213 if ((stat = decode_length(buf, limit, len, &octet_cnt)) < 0)
216 *p_num_octets += octet_cnt;
218 pbuf = &p_object[octet_idx];
220 /* Make sure the buffer contains at least the number of bits requested */
221 if ((*len + octet_cnt) > limit)
232 /*- End of function --------------------------------------------------------*/
234 static int encode_length(uint8_t *buf, int *len, int value)
244 if (value < 0x4000) {
246 /* Set the first bit of the first octet */
247 buf[*len] = ((0x8000 | value) >> 8) & 0xFF;
249 buf[*len] = value & 0xFF;
254 multiplier = (value < 0x10000) ? (value >> 14) : 4;
255 /* Set the first 2 bits of the octet */
256 buf[*len] = 0xC0 | multiplier;
258 return multiplier << 14;
260 /*- End of function --------------------------------------------------------*/
262 static int encode_open_type(uint8_t *buf, int *len, const uint8_t *data, int num_octets)
268 /* If open type is of zero length, add a single zero byte (10.1) */
269 if (num_octets == 0) {
274 /* Encode the open type */
275 for (octet_idx = 0; ; num_octets -= enclen, octet_idx += enclen) {
276 if ((enclen = encode_length(buf, len, num_octets)) < 0)
279 memcpy(&buf[*len], &data[octet_idx], enclen);
282 if (enclen >= num_octets)
288 /*- End of function --------------------------------------------------------*/
290 static int udptl_rx_packet(struct ast_udptl *s, uint8_t *buf, int len)
310 const uint8_t *bufs[16];
318 memset(&s->f[0], 0, sizeof(s->f[0]));
320 /* Decode seq_number */
323 seq_no = (buf[0] << 8) | buf[1];
326 /* Break out the primary packet */
327 if ((stat = decode_open_type(buf, len, &ptr, &ifp, &ifp_len)) != 0)
329 /* Decode error_recovery */
332 if ((buf[ptr++] & 0x80) == 0) {
333 /* Secondary packet mode for error recovery */
334 if (seq_no > s->rx_seq_no) {
335 /* We received a later packet than we expected, so we need to check if we can fill in the gap from the
336 secondary packets. */
339 if ((stat2 = decode_length(buf, len, &ptr, &count)) < 0)
341 for (i = 0; i < count; i++) {
342 if ((stat = decode_open_type(buf, len, &ptr, &bufs[total_count + i], &lengths[total_count + i])) != 0)
345 total_count += count;
348 /* Step through in reverse order, so we go oldest to newest */
349 for (i = total_count; i > 0; i--) {
350 if (seq_no - i >= s->rx_seq_no) {
351 /* This one wasn't seen before */
352 /* Decode the secondary IFP packet */
353 //fprintf(stderr, "Secondary %d, len %d\n", seq_no - i, lengths[i - 1]);
354 s->f[ifp_no].frametype = AST_FRAME_MODEM;
355 s->f[ifp_no].subclass = AST_MODEM_T38;
357 s->f[ifp_no].mallocd = 0;
358 s->f[ifp_no].seqno = seq_no - i;
359 s->f[ifp_no].datalen = lengths[i - 1];
360 s->f[ifp_no].data = (uint8_t *) bufs[i - 1];
361 s->f[ifp_no].offset = 0;
362 s->f[ifp_no].src = "UDPTL";
364 AST_LIST_NEXT(&s->f[ifp_no - 1], frame_list) = &s->f[ifp_no];
365 AST_LIST_NEXT(&s->f[ifp_no], frame_list) = NULL;
373 /* FEC mode for error recovery */
374 /* Our buffers cannot tolerate overlength IFP packets in FEC mode */
375 if (ifp_len > LOCAL_FAX_MAX_DATAGRAM)
377 /* Update any missed slots in the buffer */
378 for ( ; seq_no > s->rx_seq_no; s->rx_seq_no++) {
379 x = s->rx_seq_no & UDPTL_BUF_MASK;
380 s->rx[x].buf_len = -1;
381 s->rx[x].fec_len[0] = 0;
382 s->rx[x].fec_span = 0;
383 s->rx[x].fec_entries = 0;
386 x = seq_no & UDPTL_BUF_MASK;
388 memset(repaired, 0, sizeof(repaired));
390 /* Save the new IFP packet */
391 memcpy(s->rx[x].buf, ifp, ifp_len);
392 s->rx[x].buf_len = ifp_len;
395 /* Decode the FEC packets */
396 /* The span is defined as an unconstrained integer, but will never be more
397 than a small value. */
403 s->rx[x].fec_span = span;
405 /* The number of entries is defined as a length, but will only ever be a small
406 value. Treat it as such. */
409 entries = buf[ptr++];
410 s->rx[x].fec_entries = entries;
412 /* Decode the elements */
413 for (i = 0; i < entries; i++) {
414 if ((stat = decode_open_type(buf, len, &ptr, &data, &s->rx[x].fec_len[i])) != 0)
416 if (s->rx[x].fec_len[i] > LOCAL_FAX_MAX_DATAGRAM)
419 /* Save the new FEC data */
420 memcpy(s->rx[x].fec[i], data, s->rx[x].fec_len[i]);
422 fprintf(stderr, "FEC: ");
423 for (j = 0; j < s->rx[x].fec_len[i]; j++)
424 fprintf(stderr, "%02X ", data[j]);
425 fprintf(stderr, "\n");
429 /* See if we can reconstruct anything which is missing */
430 /* TODO: this does not comprehensively hunt back and repair everything that is possible */
431 for (l = x; l != ((x - (16 - span*entries)) & UDPTL_BUF_MASK); l = (l - 1) & UDPTL_BUF_MASK) {
432 if (s->rx[l].fec_len[0] <= 0)
434 for (m = 0; m < s->rx[l].fec_entries; m++) {
435 limit = (l + m) & UDPTL_BUF_MASK;
436 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) {
437 if (s->rx[k].buf_len <= 0)
438 which = (which == -1) ? k : -2;
442 for (j = 0; j < s->rx[l].fec_len[m]; j++) {
443 s->rx[which].buf[j] = s->rx[l].fec[m][j];
444 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)
445 s->rx[which].buf[j] ^= (s->rx[k].buf_len > j) ? s->rx[k].buf[j] : 0;
447 s->rx[which].buf_len = s->rx[l].fec_len[m];
448 repaired[which] = TRUE;
452 /* Now play any new packets forwards in time */
453 for (l = (x + 1) & UDPTL_BUF_MASK, j = seq_no - UDPTL_BUF_MASK; l != x; l = (l + 1) & UDPTL_BUF_MASK, j++) {
455 //fprintf(stderr, "Fixed packet %d, len %d\n", j, l);
456 s->f[ifp_no].frametype = AST_FRAME_MODEM;
457 s->f[ifp_no].subclass = AST_MODEM_T38;
459 s->f[ifp_no].mallocd = 0;
460 s->f[ifp_no].seqno = j;
461 s->f[ifp_no].datalen = s->rx[l].buf_len;
462 s->f[ifp_no].data = s->rx[l].buf;
463 s->f[ifp_no].offset = 0;
464 s->f[ifp_no].src = "UDPTL";
466 AST_LIST_NEXT(&s->f[ifp_no - 1], frame_list) = &s->f[ifp_no];
467 AST_LIST_NEXT(&s->f[ifp_no], frame_list) = NULL;
473 /* If packets are received out of sequence, we may have already processed this packet from the error
474 recovery information in a packet already received. */
475 if (seq_no >= s->rx_seq_no) {
476 /* Decode the primary IFP packet */
477 s->f[ifp_no].frametype = AST_FRAME_MODEM;
478 s->f[ifp_no].subclass = AST_MODEM_T38;
480 s->f[ifp_no].mallocd = 0;
481 s->f[ifp_no].seqno = seq_no;
482 s->f[ifp_no].datalen = ifp_len;
483 s->f[ifp_no].data = (uint8_t *) ifp;
484 s->f[ifp_no].offset = 0;
485 s->f[ifp_no].src = "UDPTL";
487 AST_LIST_NEXT(&s->f[ifp_no - 1], frame_list) = &s->f[ifp_no];
488 AST_LIST_NEXT(&s->f[ifp_no], frame_list) = NULL;
493 s->rx_seq_no = seq_no + 1;
496 /*- End of function --------------------------------------------------------*/
498 static int udptl_build_packet(struct ast_udptl *s, uint8_t *buf, uint8_t *ifp, int ifp_len)
500 uint8_t fec[LOCAL_FAX_MAX_DATAGRAM];
512 seq = s->tx_seq_no & 0xFFFF;
514 /* Map the sequence number to an entry in the circular buffer */
515 entry = seq & UDPTL_BUF_MASK;
517 /* We save the message in a circular buffer, for generating FEC or
518 redundancy sets later on. */
519 s->tx[entry].buf_len = ifp_len;
520 memcpy(s->tx[entry].buf, ifp, ifp_len);
522 /* Build the UDPTLPacket */
525 /* Encode the sequence number */
526 buf[len++] = (seq >> 8) & 0xFF;
527 buf[len++] = seq & 0xFF;
529 /* Encode the primary IFP packet */
530 if (encode_open_type(buf, &len, ifp, ifp_len) < 0)
533 /* Encode the appropriate type of error recovery information */
534 switch (s->error_correction_scheme)
536 case UDPTL_ERROR_CORRECTION_NONE:
537 /* Encode the error recovery type */
539 /* The number of entries will always be zero, so it is pointless allowing
540 for the fragmented case here. */
541 if (encode_length(buf, &len, 0) < 0)
544 case UDPTL_ERROR_CORRECTION_REDUNDANCY:
545 /* Encode the error recovery type */
547 if (s->tx_seq_no > s->error_correction_entries)
548 entries = s->error_correction_entries;
550 entries = s->tx_seq_no;
551 /* The number of entries will always be small, so it is pointless allowing
552 for the fragmented case here. */
553 if (encode_length(buf, &len, entries) < 0)
555 /* Encode the elements */
556 for (i = 0; i < entries; i++) {
557 j = (entry - i - 1) & UDPTL_BUF_MASK;
558 if (encode_open_type(buf, &len, s->tx[j].buf, s->tx[j].buf_len) < 0)
562 case UDPTL_ERROR_CORRECTION_FEC:
563 span = s->error_correction_span;
564 entries = s->error_correction_entries;
565 if (seq < s->error_correction_span*s->error_correction_entries) {
566 /* In the initial stages, wind up the FEC smoothly */
567 entries = seq/s->error_correction_span;
568 if (seq < s->error_correction_span)
571 /* Encode the error recovery type */
573 /* Span is defined as an inconstrained integer, which it dumb. It will only
574 ever be a small value. Treat it as such. */
577 /* The number of entries is defined as a length, but will only ever be a small
578 value. Treat it as such. */
579 buf[len++] = entries;
580 for (m = 0; m < entries; m++) {
581 /* Make an XOR'ed entry the maximum length */
582 limit = (entry + m) & UDPTL_BUF_MASK;
584 for (i = (limit - span*entries) & UDPTL_BUF_MASK; i != limit; i = (i + entries) & UDPTL_BUF_MASK) {
585 if (high_tide < s->tx[i].buf_len) {
586 for (j = 0; j < high_tide; j++)
587 fec[j] ^= s->tx[i].buf[j];
588 for ( ; j < s->tx[i].buf_len; j++)
589 fec[j] = s->tx[i].buf[j];
590 high_tide = s->tx[i].buf_len;
592 for (j = 0; j < s->tx[i].buf_len; j++)
593 fec[j] ^= s->tx[i].buf[j];
596 if (encode_open_type(buf, &len, fec, high_tide) < 0)
603 fprintf(stderr, "\n");
609 int ast_udptl_fd(struct ast_udptl *udptl)
614 void ast_udptl_set_data(struct ast_udptl *udptl, void *data)
619 void ast_udptl_set_callback(struct ast_udptl *udptl, ast_udptl_callback callback)
621 udptl->callback = callback;
624 void ast_udptl_setnat(struct ast_udptl *udptl, int nat)
629 static int udptlread(int *id, int fd, short events, void *cbdata)
631 struct ast_udptl *udptl = cbdata;
634 if ((f = ast_udptl_read(udptl))) {
636 udptl->callback(udptl, f, udptl->data);
641 struct ast_frame *ast_udptl_read(struct ast_udptl *udptl)
644 struct sockaddr_in sin;
647 uint16_t *udptlheader;
651 /* Cache where the header will go */
652 res = recvfrom(udptl->fd,
653 udptl->rawdata + AST_FRIENDLY_OFFSET,
654 sizeof(udptl->rawdata) - AST_FRIENDLY_OFFSET,
656 (struct sockaddr *) &sin,
658 udptlheader = (uint16_t *)(udptl->rawdata + AST_FRIENDLY_OFFSET);
661 ast_log(LOG_WARNING, "UDPTL read error: %s\n", strerror(errno));
664 return &ast_null_frame;
667 /* Ignore if the other side hasn't been given an address yet. */
668 if (!udptl->them.sin_addr.s_addr || !udptl->them.sin_port)
669 return &ast_null_frame;
672 /* Send to whoever sent to us */
673 if ((udptl->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
674 (udptl->them.sin_port != sin.sin_port)) {
675 memcpy(&udptl->them, &sin, sizeof(udptl->them));
676 ast_debug(1, "UDPTL NAT: Using address %s:%d\n", ast_inet_ntoa(udptl->them.sin_addr), ntohs(udptl->them.sin_port));
680 if (udptl_debug_test_addr(&sin)) {
681 ast_verb(1, "Got UDPTL packet from %s:%d (type %d, seq %d, len %d)\n",
682 ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), 0, seqno, res);
685 printf("Got UDPTL packet from %s:%d (seq %d, len = %d)\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), seqno, res);
687 if (udptl_rx_packet(udptl, udptl->rawdata + AST_FRIENDLY_OFFSET, res) < 1)
688 return &ast_null_frame;
693 void ast_udptl_offered_from_local(struct ast_udptl* udptl, int local)
696 udptl->udptl_offered_from_local = local;
698 ast_log(LOG_WARNING, "udptl structure is null\n");
701 int ast_udptl_get_error_correction_scheme(struct ast_udptl* udptl)
704 return udptl->error_correction_scheme;
706 ast_log(LOG_WARNING, "udptl structure is null\n");
711 void ast_udptl_set_error_correction_scheme(struct ast_udptl* udptl, int ec)
715 case UDPTL_ERROR_CORRECTION_FEC:
716 udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_FEC;
718 case UDPTL_ERROR_CORRECTION_REDUNDANCY:
719 udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_REDUNDANCY;
721 case UDPTL_ERROR_CORRECTION_NONE:
722 udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_NONE;
725 ast_log(LOG_WARNING, "error correction parameter invalid\n");
728 ast_log(LOG_WARNING, "udptl structure is null\n");
731 int ast_udptl_get_local_max_datagram(struct ast_udptl* udptl)
734 return udptl->local_max_datagram_size;
736 ast_log(LOG_WARNING, "udptl structure is null\n");
741 int ast_udptl_get_far_max_datagram(struct ast_udptl* udptl)
744 return udptl->far_max_datagram_size;
746 ast_log(LOG_WARNING, "udptl structure is null\n");
751 void ast_udptl_set_local_max_datagram(struct ast_udptl* udptl, int max_datagram)
754 udptl->local_max_datagram_size = max_datagram;
756 ast_log(LOG_WARNING, "udptl structure is null\n");
759 void ast_udptl_set_far_max_datagram(struct ast_udptl* udptl, int max_datagram)
762 udptl->far_max_datagram_size = max_datagram;
764 ast_log(LOG_WARNING, "udptl structure is null\n");
767 struct ast_udptl *ast_udptl_new_with_bindaddr(struct sched_context *sched, struct io_context *io, int callbackmode, struct in_addr addr)
769 struct ast_udptl *udptl;
775 if (!(udptl = ast_calloc(1, sizeof(*udptl))))
778 if (udptlfectype == 2)
779 udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_FEC;
780 else if (udptlfectype == 1)
781 udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_REDUNDANCY;
783 udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_NONE;
784 udptl->error_correction_span = udptlfecspan;
785 udptl->error_correction_entries = udptlfecentries;
787 udptl->far_max_datagram_size = udptlmaxdatagram;
788 udptl->local_max_datagram_size = udptlmaxdatagram;
790 memset(&udptl->rx, 0, sizeof(udptl->rx));
791 memset(&udptl->tx, 0, sizeof(udptl->tx));
792 for (i = 0; i <= UDPTL_BUF_MASK; i++) {
793 udptl->rx[i].buf_len = -1;
794 udptl->tx[i].buf_len = -1;
797 udptl->them.sin_family = AF_INET;
798 udptl->us.sin_family = AF_INET;
800 if ((udptl->fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
802 ast_log(LOG_WARNING, "Unable to allocate socket: %s\n", strerror(errno));
805 flags = fcntl(udptl->fd, F_GETFL);
806 fcntl(udptl->fd, F_SETFL, flags | O_NONBLOCK);
809 setsockopt(udptl->fd, SOL_SOCKET, SO_NO_CHECK, &nochecksums, sizeof(nochecksums));
811 /* Find us a place */
812 x = (ast_random() % (udptlend - udptlstart)) + udptlstart;
815 udptl->us.sin_port = htons(x);
816 udptl->us.sin_addr = addr;
817 if (bind(udptl->fd, (struct sockaddr *) &udptl->us, sizeof(udptl->us)) == 0)
819 if (errno != EADDRINUSE) {
820 ast_log(LOG_WARNING, "Unexpected bind error: %s\n", strerror(errno));
827 if (x == startplace) {
828 ast_log(LOG_WARNING, "No UDPTL ports remaining\n");
834 if (io && sched && callbackmode) {
835 /* Operate this one in a callback mode */
836 udptl->sched = sched;
838 udptl->ioid = ast_io_add(udptl->io, udptl->fd, udptlread, AST_IO_IN, udptl);
843 struct ast_udptl *ast_udptl_new(struct sched_context *sched, struct io_context *io, int callbackmode)
846 memset(&ia, 0, sizeof(ia));
847 return ast_udptl_new_with_bindaddr(sched, io, callbackmode, ia);
850 int ast_udptl_setqos(struct ast_udptl *udptl, int tos, int cos)
852 return ast_netsock_set_qos(udptl->fd, tos, cos);
855 void ast_udptl_set_peer(struct ast_udptl *udptl, struct sockaddr_in *them)
857 udptl->them.sin_port = them->sin_port;
858 udptl->them.sin_addr = them->sin_addr;
861 void ast_udptl_get_peer(struct ast_udptl *udptl, struct sockaddr_in *them)
863 them->sin_family = AF_INET;
864 them->sin_port = udptl->them.sin_port;
865 them->sin_addr = udptl->them.sin_addr;
868 void ast_udptl_get_us(struct ast_udptl *udptl, struct sockaddr_in *us)
870 memcpy(us, &udptl->us, sizeof(udptl->us));
873 void ast_udptl_stop(struct ast_udptl *udptl)
875 memset(&udptl->them.sin_addr, 0, sizeof(udptl->them.sin_addr));
876 memset(&udptl->them.sin_port, 0, sizeof(udptl->them.sin_port));
879 void ast_udptl_destroy(struct ast_udptl *udptl)
882 ast_io_remove(udptl->io, udptl->ioid);
888 int ast_udptl_write(struct ast_udptl *s, struct ast_frame *f)
893 uint8_t buf[LOCAL_FAX_MAX_DATAGRAM];
895 /* If we have no peer, return immediately */
896 if (s->them.sin_addr.s_addr == INADDR_ANY)
899 /* If there is no data length, return immediately */
903 if (f->frametype != AST_FRAME_MODEM) {
904 ast_log(LOG_WARNING, "UDPTL can only send T.38 data\n");
908 /* Save seq_no for debug output because udptl_build_packet increments it */
909 seq = s->tx_seq_no & 0xFFFF;
911 /* Cook up the UDPTL packet, with the relevant EC info. */
912 len = udptl_build_packet(s, buf, f->data, f->datalen);
914 if (len > 0 && s->them.sin_port && s->them.sin_addr.s_addr) {
915 if ((res = sendto(s->fd, buf, len, 0, (struct sockaddr *) &s->them, sizeof(s->them))) < 0)
916 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));
918 printf("Sent %d bytes of UDPTL data to %s:%d\n", res, ast_inet_ntoa(udptl->them.sin_addr), ntohs(udptl->them.sin_port));
920 if (udptl_debug_test_addr(&s->them))
921 ast_verb(1, "Sent UDPTL packet to %s:%d (type %d, seq %d, len %d)\n",
922 ast_inet_ntoa(s->them.sin_addr),
923 ntohs(s->them.sin_port), 0, seq, len);
929 void ast_udptl_proto_unregister(struct ast_udptl_protocol *proto)
931 AST_RWLIST_WRLOCK(&protos);
932 AST_RWLIST_REMOVE(&protos, proto, list);
933 AST_RWLIST_UNLOCK(&protos);
936 int ast_udptl_proto_register(struct ast_udptl_protocol *proto)
938 struct ast_udptl_protocol *cur;
940 AST_RWLIST_WRLOCK(&protos);
941 AST_RWLIST_TRAVERSE(&protos, cur, list) {
942 if (cur->type == proto->type) {
943 ast_log(LOG_WARNING, "Tried to register same protocol '%s' twice\n", cur->type);
944 AST_RWLIST_UNLOCK(&protos);
948 AST_RWLIST_INSERT_TAIL(&protos, proto, list);
949 AST_RWLIST_UNLOCK(&protos);
953 static struct ast_udptl_protocol *get_proto(struct ast_channel *chan)
955 struct ast_udptl_protocol *cur = NULL;
957 AST_RWLIST_RDLOCK(&protos);
958 AST_RWLIST_TRAVERSE(&protos, cur, list) {
959 if (cur->type == chan->tech->type)
962 AST_RWLIST_UNLOCK(&protos);
967 int ast_udptl_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
970 struct ast_channel *who;
971 struct ast_channel *cs[3];
972 struct ast_udptl *p0;
973 struct ast_udptl *p1;
974 struct ast_udptl_protocol *pr0;
975 struct ast_udptl_protocol *pr1;
976 struct sockaddr_in ac0;
977 struct sockaddr_in ac1;
978 struct sockaddr_in t0;
979 struct sockaddr_in t1;
984 ast_channel_lock(c0);
985 while (ast_channel_trylock(c1)) {
986 ast_channel_unlock(c0);
988 ast_channel_lock(c0);
993 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c0->name);
994 ast_channel_unlock(c0);
995 ast_channel_unlock(c1);
999 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c1->name);
1000 ast_channel_unlock(c0);
1001 ast_channel_unlock(c1);
1004 pvt0 = c0->tech_pvt;
1005 pvt1 = c1->tech_pvt;
1006 p0 = pr0->get_udptl_info(c0);
1007 p1 = pr1->get_udptl_info(c1);
1009 /* Somebody doesn't want to play... */
1010 ast_channel_unlock(c0);
1011 ast_channel_unlock(c1);
1014 if (pr0->set_udptl_peer(c0, p1)) {
1015 ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c0->name, c1->name);
1017 /* Store UDPTL peer */
1018 ast_udptl_get_peer(p1, &ac1);
1020 if (pr1->set_udptl_peer(c1, p0))
1021 ast_log(LOG_WARNING, "Channel '%s' failed to talk back to '%s'\n", c1->name, c0->name);
1023 /* Store UDPTL peer */
1024 ast_udptl_get_peer(p0, &ac0);
1026 ast_channel_unlock(c0);
1027 ast_channel_unlock(c1);
1032 if ((c0->tech_pvt != pvt0) ||
1033 (c1->tech_pvt != pvt1) ||
1034 (c0->masq || c0->masqr || c1->masq || c1->masqr)) {
1035 ast_debug(1, "Oooh, something is weird, backing out\n");
1036 /* Tell it to try again later */
1040 ast_udptl_get_peer(p1, &t1);
1041 ast_udptl_get_peer(p0, &t0);
1042 if (inaddrcmp(&t1, &ac1)) {
1043 ast_debug(1, "Oooh, '%s' changed end address to %s:%d\n",
1044 c1->name, ast_inet_ntoa(t1.sin_addr), ntohs(t1.sin_port));
1045 ast_debug(1, "Oooh, '%s' was %s:%d\n",
1046 c1->name, ast_inet_ntoa(ac1.sin_addr), ntohs(ac1.sin_port));
1047 memcpy(&ac1, &t1, sizeof(ac1));
1049 if (inaddrcmp(&t0, &ac0)) {
1050 ast_debug(1, "Oooh, '%s' changed end address to %s:%d\n",
1051 c0->name, ast_inet_ntoa(t0.sin_addr), ntohs(t0.sin_port));
1052 ast_debug(1, "Oooh, '%s' was %s:%d\n",
1053 c0->name, ast_inet_ntoa(ac0.sin_addr), ntohs(ac0.sin_port));
1054 memcpy(&ac0, &t0, sizeof(ac0));
1056 who = ast_waitfor_n(cs, 2, &to);
1058 ast_debug(1, "Ooh, empty read...\n");
1059 /* check for hangup / whentohangup */
1060 if (ast_check_hangup(c0) || ast_check_hangup(c1))
1068 ast_debug(1, "Oooh, got a %s\n", f ? "digit" : "hangup");
1069 /* That's all we needed */
1072 if (f->frametype == AST_FRAME_MODEM) {
1073 /* Forward T.38 frames if they happen upon us */
1076 } else if (who == c1) {
1082 /* Swap priority. Not that it's a big deal at this point */
1090 static char *handle_cli_udptl_debug_ip(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1093 struct ast_hostent ahp;
1100 e->command = "udptl debug ip";
1102 "Usage: udptl debug [ip host[:port]]\n"
1103 " Enable dumping of all UDPTL packets to and from host.\n";
1112 return CLI_SHOWUSAGE;
1114 p = strstr(arg, ":");
1120 hp = ast_gethostbyname(arg, &ahp);
1122 return CLI_SHOWUSAGE;
1123 udptldebugaddr.sin_family = AF_INET;
1124 memcpy(&udptldebugaddr.sin_addr, hp->h_addr, sizeof(udptldebugaddr.sin_addr));
1125 udptldebugaddr.sin_port = htons(port);
1127 ast_cli(a->fd, "UDPTL Debugging Enabled for IP: %s\n", ast_inet_ntoa(udptldebugaddr.sin_addr));
1129 ast_cli(a->fd, "UDPTL Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(udptldebugaddr.sin_addr), port);
1134 static char *handle_cli_udptl_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1138 e->command = "udptl debug";
1140 "Usage: udptl debug\n"
1141 " Enable dumping of all UDPTL packets.\n";
1148 return CLI_SHOWUSAGE;
1151 memset(&udptldebugaddr, 0, sizeof(udptldebugaddr));
1153 ast_cli(a->fd, "UDPTL Debugging Enabled\n");
1157 static char *handle_cli_udptl_debug_off(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1161 e->command = "udptl debug off";
1163 "Usage: udptl debug off\n"
1164 " Disable dumping of all UDPTL packets.\n";
1171 return CLI_SHOWUSAGE;
1175 ast_cli(a->fd, "UDPTL Debugging Disabled\n");
1179 static struct ast_cli_entry cli_udptl[] = {
1180 AST_CLI_DEFINE(handle_cli_udptl_debug, "Enable UDPTL debugging"),
1181 AST_CLI_DEFINE(handle_cli_udptl_debug_ip, "Enable UDPTL debugging on IP"),
1182 AST_CLI_DEFINE(handle_cli_udptl_debug_off, "Disable UDPTL debugging")
1185 static void __ast_udptl_reload(int reload)
1187 struct ast_config *cfg;
1189 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
1191 if ((cfg = ast_config_load("udptl.conf", config_flags)) == CONFIG_STATUS_FILEUNCHANGED)
1197 udptlfecentries = 0;
1199 udptlmaxdatagram = 0;
1202 if ((s = ast_variable_retrieve(cfg, "general", "udptlstart"))) {
1203 udptlstart = atoi(s);
1204 if (udptlstart < 1024)
1206 if (udptlstart > 65535)
1209 if ((s = ast_variable_retrieve(cfg, "general", "udptlend"))) {
1211 if (udptlend < 1024)
1213 if (udptlend > 65535)
1216 if ((s = ast_variable_retrieve(cfg, "general", "udptlchecksums"))) {
1224 ast_log(LOG_WARNING, "Disabling UDPTL checksums is not supported on this operating system!\n");
1227 if ((s = ast_variable_retrieve(cfg, "general", "T38FaxUdpEC"))) {
1228 if (strcmp(s, "t38UDPFEC") == 0)
1230 else if (strcmp(s, "t38UDPRedundancy") == 0)
1233 if ((s = ast_variable_retrieve(cfg, "general", "T38FaxMaxDatagram"))) {
1234 udptlmaxdatagram = atoi(s);
1235 if (udptlmaxdatagram < 0)
1236 udptlmaxdatagram = 0;
1237 if (udptlmaxdatagram > LOCAL_FAX_MAX_DATAGRAM)
1238 udptlmaxdatagram = LOCAL_FAX_MAX_DATAGRAM;
1240 if ((s = ast_variable_retrieve(cfg, "general", "UDPTLFECentries"))) {
1241 udptlfecentries = atoi(s);
1242 if (udptlfecentries < 0)
1243 udptlfecentries = 0;
1244 if (udptlfecentries > MAX_FEC_ENTRIES)
1245 udptlfecentries = MAX_FEC_ENTRIES;
1247 if ((s = ast_variable_retrieve(cfg, "general", "UDPTLFECspan"))) {
1248 udptlfecspan = atoi(s);
1249 if (udptlfecspan < 0)
1251 if (udptlfecspan > MAX_FEC_SPAN)
1252 udptlfecspan = MAX_FEC_SPAN;
1254 ast_config_destroy(cfg);
1256 if (udptlstart >= udptlend) {
1257 ast_log(LOG_WARNING, "Unreasonable values for UDPTL start/end\n");
1261 ast_verb(2, "UDPTL allocating from port range %d -> %d\n", udptlstart, udptlend);
1264 void ast_udptl_reload(void)
1266 __ast_udptl_reload(1);
1269 void ast_udptl_init(void)
1271 ast_cli_register_multiple(cli_udptl, sizeof(cli_udptl) / sizeof(struct ast_cli_entry));
1272 __ast_udptl_reload(0);