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$")
60 #include <netinet/in.h>
62 #include <sys/socket.h>
63 #include <arpa/inet.h>
66 #include "asterisk/udptl.h"
67 #include "asterisk/frame.h"
68 #include "asterisk/logger.h"
69 #include "asterisk/options.h"
70 #include "asterisk/channel.h"
71 #include "asterisk/acl.h"
72 #include "asterisk/channel.h"
73 #include "asterisk/config.h"
74 #include "asterisk/lock.h"
75 #include "asterisk/utils.h"
76 #include "asterisk/netsock.h"
77 #include "asterisk/cli.h"
78 #include "asterisk/unaligned.h"
79 #include "asterisk/utils.h"
81 #define UDPTL_MTU 1200
90 static int udptlstart;
92 static int udptldebug; /*!< Are we debugging? */
93 static struct sockaddr_in udptldebugaddr; /*!< Debug packets to/from this host */
95 static int nochecksums;
97 static int udptlfectype;
98 static int udptlfecentries;
99 static int udptlfecspan;
100 static int udptlmaxdatagram;
102 #define LOCAL_FAX_MAX_DATAGRAM 400
103 #define MAX_FEC_ENTRIES 5
104 #define MAX_FEC_SPAN 5
106 #define UDPTL_BUF_MASK 15
110 uint8_t buf[LOCAL_FAX_MAX_DATAGRAM];
111 } udptl_fec_tx_buffer_t;
115 uint8_t buf[LOCAL_FAX_MAX_DATAGRAM];
116 int fec_len[MAX_FEC_ENTRIES];
117 uint8_t fec[MAX_FEC_ENTRIES][LOCAL_FAX_MAX_DATAGRAM];
120 } udptl_fec_rx_buffer_t;
122 /*! \brief Structure for an UDPTL session */
126 struct ast_frame f[16];
127 unsigned char rawdata[8192 + AST_FRIENDLY_OFFSET];
128 unsigned int lasteventseqn;
131 struct sockaddr_in us;
132 struct sockaddr_in them;
135 struct sched_context *sched;
136 struct io_context *io;
138 ast_udptl_callback callback;
139 int udptl_offered_from_local;
141 /*! This option indicates the error correction scheme used in transmitted UDPTL
143 int error_correction_scheme;
145 /*! This option indicates the number of error correction entries transmitted in
147 int error_correction_entries;
149 /*! This option indicates the span of the error correction entries in transmitted
150 UDPTL packets (FEC only). */
151 int error_correction_span;
153 /*! This option indicates the maximum size of a UDPTL packet that can be accepted by
154 the remote device. */
155 int far_max_datagram_size;
157 /*! This option indicates the maximum size of a UDPTL packet that we are prepared to
159 int local_max_datagram_size;
163 struct sockaddr_in far;
167 int rx_expected_seq_no;
169 udptl_fec_tx_buffer_t tx[UDPTL_BUF_MASK + 1];
170 udptl_fec_rx_buffer_t rx[UDPTL_BUF_MASK + 1];
173 static struct ast_udptl_protocol *protos;
175 static int udptl_rx_packet(struct ast_udptl *s, uint8_t *buf, int len);
176 static int udptl_build_packet(struct ast_udptl *s, uint8_t *buf, uint8_t *ifp, int ifp_len);
178 static inline int udptl_debug_test_addr(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, int limit, int *len, int *pvalue)
193 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];
211 *pvalue = (buf[*len] & 0x3F) << 14;
213 /* Indicate we have a fragment */
216 /*- End of function --------------------------------------------------------*/
218 static int decode_open_type(uint8_t *buf, int limit, int *len, const uint8_t **p_object, int *p_num_octets)
224 const uint8_t **pbuf;
226 for (octet_idx = 0, *p_num_octets = 0; ; octet_idx += octet_cnt) {
227 if ((stat = decode_length(buf, limit, len, &octet_cnt)) < 0)
230 *p_num_octets += octet_cnt;
232 pbuf = &p_object[octet_idx];
234 /* Make sure the buffer contains at least the number of bits requested */
235 if ((*len + octet_cnt) > limit)
246 /*- End of function --------------------------------------------------------*/
248 static int encode_length(uint8_t *buf, int *len, int value)
258 if (value < 0x4000) {
260 /* Set the first bit of the first octet */
261 buf[*len] = ((0x8000 | value) >> 8) & 0xFF;
263 buf[*len] = value & 0xFF;
268 multiplier = (value < 0x10000) ? (value >> 14) : 4;
269 /* Set the first 2 bits of the octet */
270 buf[*len] = 0xC0 | multiplier;
272 return multiplier << 14;
274 /*- End of function --------------------------------------------------------*/
276 static int encode_open_type(uint8_t *buf, int *len, const uint8_t *data, int num_octets)
282 /* If open type is of zero length, add a single zero byte (10.1) */
283 if (num_octets == 0) {
288 /* Encode the open type */
289 for (octet_idx = 0; ; num_octets -= enclen, octet_idx += enclen) {
290 if ((enclen = encode_length(buf, len, num_octets)) < 0)
293 memcpy(&buf[*len], &data[octet_idx], enclen);
296 if (enclen >= num_octets)
302 /*- End of function --------------------------------------------------------*/
304 static int udptl_rx_packet(struct ast_udptl *s, uint8_t *buf, int len)
324 const uint8_t *bufs[16];
332 memset(&s->f[0], 0, sizeof(s->f[0]));
334 /* Decode seq_number */
337 seq_no = (buf[0] << 8) | buf[1];
340 /* Break out the primary packet */
341 if ((stat = decode_open_type(buf, len, &ptr, &ifp, &ifp_len)) != 0)
343 /* Decode error_recovery */
346 if ((buf[ptr++] & 0x80) == 0) {
347 /* Secondary packet mode for error recovery */
348 if (seq_no > s->rx_seq_no) {
349 /* We received a later packet than we expected, so we need to check if we can fill in the gap from the
350 secondary packets. */
353 if ((stat2 = decode_length(buf, len, &ptr, &count)) < 0)
355 for (i = 0; i < count; i++) {
356 if ((stat = decode_open_type(buf, len, &ptr, &bufs[total_count + i], &lengths[total_count + i])) != 0)
359 total_count += count;
362 /* Step through in reverse order, so we go oldest to newest */
363 for (i = total_count; i > 0; i--) {
364 if (seq_no - i >= s->rx_seq_no) {
365 /* This one wasn't seen before */
366 /* Decode the secondary IFP packet */
367 //fprintf(stderr, "Secondary %d, len %d\n", seq_no - i, lengths[i - 1]);
368 s->f[ifp_no].frametype = AST_FRAME_MODEM;
369 s->f[ifp_no].subclass = AST_MODEM_T38;
371 s->f[ifp_no].mallocd = 0;
372 //s->f[ifp_no].???seq_no = seq_no - i;
373 s->f[ifp_no].datalen = lengths[i - 1];
374 s->f[ifp_no].data = (uint8_t *) bufs[i - 1];
375 s->f[ifp_no].offset = 0;
376 s->f[ifp_no].src = "UDPTL";
378 AST_LIST_NEXT(&s->f[ifp_no - 1], frame_list) = &s->f[ifp_no];
379 AST_LIST_NEXT(&s->f[ifp_no], frame_list) = NULL;
384 /* If packets are received out of sequence, we may have already processed this packet from the error
385 recovery information in a packet already received. */
386 if (seq_no >= s->rx_seq_no) {
387 /* Decode the primary IFP packet */
388 s->f[ifp_no].frametype = AST_FRAME_MODEM;
389 s->f[ifp_no].subclass = AST_MODEM_T38;
391 s->f[ifp_no].mallocd = 0;
392 //s->f[ifp_no].???seq_no = seq_no;
393 s->f[ifp_no].datalen = ifp_len;
394 s->f[ifp_no].data = (uint8_t *) ifp;
395 s->f[ifp_no].offset = 0;
396 s->f[ifp_no].src = "UDPTL";
398 AST_LIST_NEXT(&s->f[ifp_no - 1], frame_list) = &s->f[ifp_no];
399 AST_LIST_NEXT(&s->f[ifp_no], frame_list) = NULL;
404 /* FEC mode for error recovery */
405 /* Our buffers cannot tolerate overlength IFP packets in FEC mode */
406 if (ifp_len > LOCAL_FAX_MAX_DATAGRAM)
408 /* Update any missed slots in the buffer */
409 for ( ; seq_no > s->rx_seq_no; s->rx_seq_no++) {
410 x = s->rx_seq_no & UDPTL_BUF_MASK;
411 s->rx[x].buf_len = -1;
412 s->rx[x].fec_len[0] = 0;
413 s->rx[x].fec_span = 0;
414 s->rx[x].fec_entries = 0;
417 x = seq_no & UDPTL_BUF_MASK;
419 memset(repaired, 0, sizeof(repaired));
421 /* Save the new IFP packet */
422 memcpy(s->rx[x].buf, ifp, ifp_len);
423 s->rx[x].buf_len = ifp_len;
426 /* Decode the FEC packets */
427 /* The span is defined as an unconstrained integer, but will never be more
428 than a small value. */
434 s->rx[x].fec_span = span;
436 /* The number of entries is defined as a length, but will only ever be a small
437 value. Treat it as such. */
440 entries = buf[ptr++];
441 s->rx[x].fec_entries = entries;
443 /* Decode the elements */
444 for (i = 0; i < entries; i++) {
445 if ((stat = decode_open_type(buf, len, &ptr, &data, &s->rx[x].fec_len[i])) != 0)
447 if (s->rx[x].fec_len[i] > LOCAL_FAX_MAX_DATAGRAM)
450 /* Save the new FEC data */
451 memcpy(s->rx[x].fec[i], data, s->rx[x].fec_len[i]);
453 fprintf(stderr, "FEC: ");
454 for (j = 0; j < s->rx[x].fec_len[i]; j++)
455 fprintf(stderr, "%02X ", data[j]);
456 fprintf(stderr, "\n");
460 /* See if we can reconstruct anything which is missing */
461 /* TODO: this does not comprehensively hunt back and repair everything that is possible */
462 for (l = x; l != ((x - (16 - span*entries)) & UDPTL_BUF_MASK); l = (l - 1) & UDPTL_BUF_MASK) {
463 if (s->rx[l].fec_len[0] <= 0)
465 for (m = 0; m < s->rx[l].fec_entries; m++) {
466 limit = (l + m) & UDPTL_BUF_MASK;
467 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) {
468 if (s->rx[k].buf_len <= 0)
469 which = (which == -1) ? k : -2;
473 for (j = 0; j < s->rx[l].fec_len[m]; j++) {
474 s->rx[which].buf[j] = s->rx[l].fec[m][j];
475 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)
476 s->rx[which].buf[j] ^= (s->rx[k].buf_len > j) ? s->rx[k].buf[j] : 0;
478 s->rx[which].buf_len = s->rx[l].fec_len[m];
479 repaired[which] = TRUE;
483 /* Now play any new packets forwards in time */
484 for (l = (x + 1) & UDPTL_BUF_MASK, j = seq_no - UDPTL_BUF_MASK; l != x; l = (l + 1) & UDPTL_BUF_MASK, j++) {
486 //fprintf(stderr, "Fixed packet %d, len %d\n", j, l);
487 s->f[ifp_no].frametype = AST_FRAME_MODEM;
488 s->f[ifp_no].subclass = AST_MODEM_T38;
490 s->f[ifp_no].mallocd = 0;
491 //s->f[ifp_no].???seq_no = j;
492 s->f[ifp_no].datalen = s->rx[l].buf_len;
493 s->f[ifp_no].data = s->rx[l].buf;
494 s->f[ifp_no].offset = 0;
495 s->f[ifp_no].src = "UDPTL";
497 AST_LIST_NEXT(&s->f[ifp_no - 1], frame_list) = &s->f[ifp_no];
498 AST_LIST_NEXT(&s->f[ifp_no], frame_list) = NULL;
502 /* Decode the primary IFP packet */
503 s->f[ifp_no].frametype = AST_FRAME_MODEM;
504 s->f[ifp_no].subclass = AST_MODEM_T38;
506 s->f[ifp_no].mallocd = 0;
507 //s->f[ifp_no].???seq_no = j;
508 s->f[ifp_no].datalen = ifp_len;
509 s->f[ifp_no].data = (uint8_t *) ifp;
510 s->f[ifp_no].offset = 0;
511 s->f[ifp_no].src = "UDPTL";
513 AST_LIST_NEXT(&s->f[ifp_no - 1], frame_list) = &s->f[ifp_no];
514 AST_LIST_NEXT(&s->f[ifp_no], frame_list) = NULL;
517 s->rx_seq_no = seq_no + 1;
520 /*- End of function --------------------------------------------------------*/
522 static int udptl_build_packet(struct ast_udptl *s, uint8_t *buf, uint8_t *ifp, int ifp_len)
524 uint8_t fec[LOCAL_FAX_MAX_DATAGRAM];
536 seq = s->tx_seq_no & 0xFFFF;
538 /* Map the sequence number to an entry in the circular buffer */
539 entry = seq & UDPTL_BUF_MASK;
541 /* We save the message in a circular buffer, for generating FEC or
542 redundancy sets later on. */
543 s->tx[entry].buf_len = ifp_len;
544 memcpy(s->tx[entry].buf, ifp, ifp_len);
546 /* Build the UDPTLPacket */
549 /* Encode the sequence number */
550 buf[len++] = (seq >> 8) & 0xFF;
551 buf[len++] = seq & 0xFF;
553 /* Encode the primary IFP packet */
554 if (encode_open_type(buf, &len, ifp, ifp_len) < 0)
557 /* Encode the appropriate type of error recovery information */
558 switch (s->error_correction_scheme)
560 case UDPTL_ERROR_CORRECTION_NONE:
561 /* Encode the error recovery type */
563 /* The number of entries will always be zero, so it is pointless allowing
564 for the fragmented case here. */
565 if (encode_length(buf, &len, 0) < 0)
568 case UDPTL_ERROR_CORRECTION_REDUNDANCY:
569 /* Encode the error recovery type */
571 if (s->tx_seq_no > s->error_correction_entries)
572 entries = s->error_correction_entries;
574 entries = s->tx_seq_no;
575 /* The number of entries will always be small, so it is pointless allowing
576 for the fragmented case here. */
577 if (encode_length(buf, &len, entries) < 0)
579 /* Encode the elements */
580 for (i = 0; i < entries; i++) {
581 j = (entry - i - 1) & UDPTL_BUF_MASK;
582 if (encode_open_type(buf, &len, s->tx[j].buf, s->tx[j].buf_len) < 0)
586 case UDPTL_ERROR_CORRECTION_FEC:
587 span = s->error_correction_span;
588 entries = s->error_correction_entries;
589 if (seq < s->error_correction_span*s->error_correction_entries) {
590 /* In the initial stages, wind up the FEC smoothly */
591 entries = seq/s->error_correction_span;
592 if (seq < s->error_correction_span)
595 /* Encode the error recovery type */
597 /* Span is defined as an inconstrained integer, which it dumb. It will only
598 ever be a small value. Treat it as such. */
601 /* The number of entries is defined as a length, but will only ever be a small
602 value. Treat it as such. */
603 buf[len++] = entries;
604 for (m = 0; m < entries; m++) {
605 /* Make an XOR'ed entry the maximum length */
606 limit = (entry + m) & UDPTL_BUF_MASK;
608 for (i = (limit - span*entries) & UDPTL_BUF_MASK; i != limit; i = (i + entries) & UDPTL_BUF_MASK) {
609 if (high_tide < s->tx[i].buf_len) {
610 for (j = 0; j < high_tide; j++)
611 fec[j] ^= s->tx[i].buf[j];
612 for ( ; j < s->tx[i].buf_len; j++)
613 fec[j] = s->tx[i].buf[j];
614 high_tide = s->tx[i].buf_len;
616 for (j = 0; j < s->tx[i].buf_len; j++)
617 fec[j] ^= s->tx[i].buf[j];
620 if (encode_open_type(buf, &len, fec, high_tide) < 0)
627 fprintf(stderr, "\n");
633 int ast_udptl_fd(struct ast_udptl *udptl)
638 void ast_udptl_set_data(struct ast_udptl *udptl, void *data)
643 void ast_udptl_set_callback(struct ast_udptl *udptl, ast_udptl_callback callback)
645 udptl->callback = callback;
648 void ast_udptl_setnat(struct ast_udptl *udptl, int nat)
653 static int udptlread(int *id, int fd, short events, void *cbdata)
655 struct ast_udptl *udptl = cbdata;
658 if ((f = ast_udptl_read(udptl))) {
660 udptl->callback(udptl, f, udptl->data);
665 struct ast_frame *ast_udptl_read(struct ast_udptl *udptl)
668 struct sockaddr_in sin;
671 uint16_t *udptlheader;
675 /* Cache where the header will go */
676 res = recvfrom(udptl->fd,
677 udptl->rawdata + AST_FRIENDLY_OFFSET,
678 sizeof(udptl->rawdata) - AST_FRIENDLY_OFFSET,
680 (struct sockaddr *) &sin,
682 udptlheader = (uint16_t *)(udptl->rawdata + AST_FRIENDLY_OFFSET);
685 ast_log(LOG_WARNING, "UDPTL read error: %s\n", strerror(errno));
688 return &ast_null_frame;
691 /* Ignore if the other side hasn't been given an address yet. */
692 if (!udptl->them.sin_addr.s_addr || !udptl->them.sin_port)
693 return &ast_null_frame;
696 /* Send to whoever sent to us */
697 if ((udptl->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
698 (udptl->them.sin_port != sin.sin_port)) {
699 memcpy(&udptl->them, &sin, sizeof(udptl->them));
700 ast_debug(1, "UDPTL NAT: Using address %s:%d\n", ast_inet_ntoa(udptl->them.sin_addr), ntohs(udptl->them.sin_port));
704 if (udptl_debug_test_addr(&sin)) {
706 ast_verbose("Got UDPTL packet from %s:%d (type %d, seq %d, len %d)\n",
707 ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), 0, seqno, res);
710 printf("Got UDPTL packet from %s:%d (seq %d, len = %d)\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), seqno, res);
712 udptl_rx_packet(udptl, udptl->rawdata + AST_FRIENDLY_OFFSET, res);
717 void ast_udptl_offered_from_local(struct ast_udptl* udptl, int local)
720 udptl->udptl_offered_from_local = local;
722 ast_log(LOG_WARNING, "udptl structure is null\n");
725 int ast_udptl_get_error_correction_scheme(struct ast_udptl* udptl)
728 return udptl->error_correction_scheme;
730 ast_log(LOG_WARNING, "udptl structure is null\n");
735 void ast_udptl_set_error_correction_scheme(struct ast_udptl* udptl, int ec)
739 case UDPTL_ERROR_CORRECTION_FEC:
740 udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_FEC;
742 case UDPTL_ERROR_CORRECTION_REDUNDANCY:
743 udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_REDUNDANCY;
745 case UDPTL_ERROR_CORRECTION_NONE:
746 udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_NONE;
749 ast_log(LOG_WARNING, "error correction parameter invalid\n");
752 ast_log(LOG_WARNING, "udptl structure is null\n");
755 int ast_udptl_get_local_max_datagram(struct ast_udptl* udptl)
758 return udptl->local_max_datagram_size;
760 ast_log(LOG_WARNING, "udptl structure is null\n");
765 int ast_udptl_get_far_max_datagram(struct ast_udptl* udptl)
768 return udptl->far_max_datagram_size;
770 ast_log(LOG_WARNING, "udptl structure is null\n");
775 void ast_udptl_set_local_max_datagram(struct ast_udptl* udptl, int max_datagram)
778 udptl->local_max_datagram_size = max_datagram;
780 ast_log(LOG_WARNING, "udptl structure is null\n");
783 void ast_udptl_set_far_max_datagram(struct ast_udptl* udptl, int max_datagram)
786 udptl->far_max_datagram_size = max_datagram;
788 ast_log(LOG_WARNING, "udptl structure is null\n");
791 struct ast_udptl *ast_udptl_new_with_bindaddr(struct sched_context *sched, struct io_context *io, int callbackmode, struct in_addr addr)
793 struct ast_udptl *udptl;
799 if (!(udptl = ast_calloc(1, sizeof(*udptl))))
802 if (udptlfectype == 2)
803 udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_FEC;
804 else if (udptlfectype == 1)
805 udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_REDUNDANCY;
807 udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_NONE;
808 udptl->error_correction_span = udptlfecspan;
809 udptl->error_correction_entries = udptlfecentries;
811 udptl->far_max_datagram_size = udptlmaxdatagram;
812 udptl->local_max_datagram_size = udptlmaxdatagram;
814 memset(&udptl->rx, 0, sizeof(udptl->rx));
815 memset(&udptl->tx, 0, sizeof(udptl->tx));
816 for (i = 0; i <= UDPTL_BUF_MASK; i++) {
817 udptl->rx[i].buf_len = -1;
818 udptl->tx[i].buf_len = -1;
821 udptl->seqno = ast_random() & 0xffff;
822 udptl->them.sin_family = AF_INET;
823 udptl->us.sin_family = AF_INET;
825 if ((udptl->fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
827 ast_log(LOG_WARNING, "Unable to allocate socket: %s\n", strerror(errno));
830 flags = fcntl(udptl->fd, F_GETFL);
831 fcntl(udptl->fd, F_SETFL, flags | O_NONBLOCK);
834 setsockopt(udptl->fd, SOL_SOCKET, SO_NO_CHECK, &nochecksums, sizeof(nochecksums));
836 /* Find us a place */
837 x = (ast_random() % (udptlend - udptlstart)) + udptlstart;
840 udptl->us.sin_port = htons(x);
841 udptl->us.sin_addr = addr;
842 if (bind(udptl->fd, (struct sockaddr *) &udptl->us, sizeof(udptl->us)) == 0)
844 if (errno != EADDRINUSE) {
845 ast_log(LOG_WARNING, "Unexpected bind error: %s\n", strerror(errno));
852 if (x == startplace) {
853 ast_log(LOG_WARNING, "No UDPTL ports remaining\n");
859 if (io && sched && callbackmode) {
860 /* Operate this one in a callback mode */
861 udptl->sched = sched;
863 udptl->ioid = ast_io_add(udptl->io, udptl->fd, udptlread, AST_IO_IN, udptl);
868 struct ast_udptl *ast_udptl_new(struct sched_context *sched, struct io_context *io, int callbackmode)
871 memset(&ia, 0, sizeof(ia));
872 return ast_udptl_new_with_bindaddr(sched, io, callbackmode, ia);
875 int ast_udptl_setqos(struct ast_udptl *udptl, int tos, int cos)
877 return ast_netsock_set_qos(udptl->fd, tos, cos);
880 void ast_udptl_set_peer(struct ast_udptl *udptl, struct sockaddr_in *them)
882 udptl->them.sin_port = them->sin_port;
883 udptl->them.sin_addr = them->sin_addr;
886 void ast_udptl_get_peer(struct ast_udptl *udptl, struct sockaddr_in *them)
888 them->sin_family = AF_INET;
889 them->sin_port = udptl->them.sin_port;
890 them->sin_addr = udptl->them.sin_addr;
893 void ast_udptl_get_us(struct ast_udptl *udptl, struct sockaddr_in *us)
895 memcpy(us, &udptl->us, sizeof(udptl->us));
898 void ast_udptl_stop(struct ast_udptl *udptl)
900 memset(&udptl->them.sin_addr, 0, sizeof(udptl->them.sin_addr));
901 memset(&udptl->them.sin_port, 0, sizeof(udptl->them.sin_port));
904 void ast_udptl_destroy(struct ast_udptl *udptl)
907 ast_io_remove(udptl->io, udptl->ioid);
913 int ast_udptl_write(struct ast_udptl *s, struct ast_frame *f)
917 uint8_t buf[LOCAL_FAX_MAX_DATAGRAM];
919 /* If we have no peer, return immediately */
920 if (s->them.sin_addr.s_addr == INADDR_ANY)
923 /* If there is no data length, return immediately */
927 if (f->frametype != AST_FRAME_MODEM) {
928 ast_log(LOG_WARNING, "UDPTL can only send T.38 data\n");
932 /* Cook up the UDPTL packet, with the relevant EC info. */
933 len = udptl_build_packet(s, buf, f->data, f->datalen);
935 if (len > 0 && s->them.sin_port && s->them.sin_addr.s_addr) {
936 if ((res = sendto(s->fd, buf, len, 0, (struct sockaddr *) &s->them, sizeof(s->them))) < 0)
937 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));
939 printf("Sent %d bytes of UDPTL data to %s:%d\n", res, ast_inet_ntoa(udptl->them.sin_addr), ntohs(udptl->them.sin_port));
941 if (udptl_debug_test_addr(&s->them))
942 ast_verbose("Sent UDPTL packet to %s:%d (type %d, seq %d, len %d)\n",
943 ast_inet_ntoa(s->them.sin_addr),
944 ntohs(s->them.sin_port), 0, s->seqno, len);
950 void ast_udptl_proto_unregister(struct ast_udptl_protocol *proto)
952 struct ast_udptl_protocol *cur;
953 struct ast_udptl_protocol *prev;
960 prev->next = proto->next;
962 protos = proto->next;
970 int ast_udptl_proto_register(struct ast_udptl_protocol *proto)
972 struct ast_udptl_protocol *cur;
976 if (cur->type == proto->type) {
977 ast_log(LOG_WARNING, "Tried to register same protocol '%s' twice\n", cur->type);
982 proto->next = protos;
987 static struct ast_udptl_protocol *get_proto(struct ast_channel *chan)
989 struct ast_udptl_protocol *cur;
993 if (cur->type == chan->tech->type)
1000 int ast_udptl_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
1002 struct ast_frame *f;
1003 struct ast_channel *who;
1004 struct ast_channel *cs[3];
1005 struct ast_udptl *p0;
1006 struct ast_udptl *p1;
1007 struct ast_udptl_protocol *pr0;
1008 struct ast_udptl_protocol *pr1;
1009 struct sockaddr_in ac0;
1010 struct sockaddr_in ac1;
1011 struct sockaddr_in t0;
1012 struct sockaddr_in t1;
1017 ast_channel_lock(c0);
1018 while (ast_channel_trylock(c1)) {
1019 ast_channel_unlock(c0);
1021 ast_channel_lock(c0);
1023 pr0 = get_proto(c0);
1024 pr1 = get_proto(c1);
1026 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c0->name);
1027 ast_channel_unlock(c0);
1028 ast_channel_unlock(c1);
1032 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c1->name);
1033 ast_channel_unlock(c0);
1034 ast_channel_unlock(c1);
1037 pvt0 = c0->tech_pvt;
1038 pvt1 = c1->tech_pvt;
1039 p0 = pr0->get_udptl_info(c0);
1040 p1 = pr1->get_udptl_info(c1);
1042 /* Somebody doesn't want to play... */
1043 ast_channel_unlock(c0);
1044 ast_channel_unlock(c1);
1047 if (pr0->set_udptl_peer(c0, p1)) {
1048 ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c0->name, c1->name);
1050 /* Store UDPTL peer */
1051 ast_udptl_get_peer(p1, &ac1);
1053 if (pr1->set_udptl_peer(c1, p0))
1054 ast_log(LOG_WARNING, "Channel '%s' failed to talk back to '%s'\n", c1->name, c0->name);
1056 /* Store UDPTL peer */
1057 ast_udptl_get_peer(p0, &ac0);
1059 ast_channel_unlock(c0);
1060 ast_channel_unlock(c1);
1065 if ((c0->tech_pvt != pvt0) ||
1066 (c1->tech_pvt != pvt1) ||
1067 (c0->masq || c0->masqr || c1->masq || c1->masqr)) {
1068 ast_debug(1, "Oooh, something is weird, backing out\n");
1069 /* Tell it to try again later */
1073 ast_udptl_get_peer(p1, &t1);
1074 ast_udptl_get_peer(p0, &t0);
1075 if (inaddrcmp(&t1, &ac1)) {
1076 ast_debug(1, "Oooh, '%s' changed end address to %s:%d\n",
1077 c1->name, ast_inet_ntoa(t1.sin_addr), ntohs(t1.sin_port));
1078 ast_debug(1, "Oooh, '%s' was %s:%d\n",
1079 c1->name, ast_inet_ntoa(ac1.sin_addr), ntohs(ac1.sin_port));
1080 memcpy(&ac1, &t1, sizeof(ac1));
1082 if (inaddrcmp(&t0, &ac0)) {
1083 ast_debug(1, "Oooh, '%s' changed end address to %s:%d\n",
1084 c0->name, ast_inet_ntoa(t0.sin_addr), ntohs(t0.sin_port));
1085 ast_debug(1, "Oooh, '%s' was %s:%d\n",
1086 c0->name, ast_inet_ntoa(ac0.sin_addr), ntohs(ac0.sin_port));
1087 memcpy(&ac0, &t0, sizeof(ac0));
1089 who = ast_waitfor_n(cs, 2, &to);
1091 ast_debug(1, "Ooh, empty read...\n");
1092 /* check for hangup / whentohangup */
1093 if (ast_check_hangup(c0) || ast_check_hangup(c1))
1101 ast_debug(1, "Oooh, got a %s\n", f ? "digit" : "hangup");
1102 /* That's all we needed */
1105 if (f->frametype == AST_FRAME_MODEM) {
1106 /* Forward T.38 frames if they happen upon us */
1109 } else if (who == c1) {
1115 /* Swap priority. Not that it's a big deal at this point */
1123 static int udptl_do_debug_ip(int fd, int argc, char *argv[])
1126 struct ast_hostent ahp;
1133 return RESULT_SHOWUSAGE;
1135 p = strstr(arg, ":");
1141 hp = ast_gethostbyname(arg, &ahp);
1143 return RESULT_SHOWUSAGE;
1144 udptldebugaddr.sin_family = AF_INET;
1145 memcpy(&udptldebugaddr.sin_addr, hp->h_addr, sizeof(udptldebugaddr.sin_addr));
1146 udptldebugaddr.sin_port = htons(port);
1148 ast_cli(fd, "UDPTL Debugging Enabled for IP: %s\n", ast_inet_ntoa(udptldebugaddr.sin_addr));
1150 ast_cli(fd, "UDPTL Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(udptldebugaddr.sin_addr), port);
1152 return RESULT_SUCCESS;
1155 static int udptl_do_debug(int fd, int argc, char *argv[])
1159 return RESULT_SHOWUSAGE;
1160 return udptl_do_debug_ip(fd, argc, argv);
1163 memset(&udptldebugaddr,0,sizeof(udptldebugaddr));
1164 ast_cli(fd, "UDPTL Debugging Enabled\n");
1165 return RESULT_SUCCESS;
1168 static int udptl_nodebug(int fd, int argc, char *argv[])
1171 return RESULT_SHOWUSAGE;
1173 ast_cli(fd,"UDPTL Debugging Disabled\n");
1174 return RESULT_SUCCESS;
1177 static const char debug_usage[] =
1178 "Usage: udptl debug [ip host[:port]]\n"
1179 " Enable dumping of all UDPTL packets to and from host.\n";
1181 static const char nodebug_usage[] =
1182 "Usage: udptl debug off\n"
1183 " Disable all UDPTL debugging\n";
1185 static struct ast_cli_entry cli_udptl[] = {
1186 { { "udptl", "debug", NULL },
1187 udptl_do_debug, "Enable UDPTL debugging",
1190 { { "udptl", "debug", "ip", NULL },
1191 udptl_do_debug, "Enable UDPTL debugging on IP",
1194 { { "udptl", "debug", "off", NULL },
1195 udptl_nodebug, "Disable UDPTL debugging",
1199 void ast_udptl_reload(void)
1201 struct ast_config *cfg;
1207 udptlfecentries = 0;
1209 udptlmaxdatagram = 0;
1211 if ((cfg = ast_config_load("udptl.conf"))) {
1212 if ((s = ast_variable_retrieve(cfg, "general", "udptlstart"))) {
1213 udptlstart = atoi(s);
1214 if (udptlstart < 1024)
1216 if (udptlstart > 65535)
1219 if ((s = ast_variable_retrieve(cfg, "general", "udptlend"))) {
1221 if (udptlend < 1024)
1223 if (udptlend > 65535)
1226 if ((s = ast_variable_retrieve(cfg, "general", "udptlchecksums"))) {
1234 ast_log(LOG_WARNING, "Disabling UDPTL checksums is not supported on this operating system!\n");
1237 if ((s = ast_variable_retrieve(cfg, "general", "T38FaxUdpEC"))) {
1238 if (strcmp(s, "t38UDPFEC") == 0)
1240 else if (strcmp(s, "t38UDPRedundancy") == 0)
1243 if ((s = ast_variable_retrieve(cfg, "general", "T38FaxMaxDatagram"))) {
1244 udptlmaxdatagram = atoi(s);
1245 if (udptlmaxdatagram < 0)
1246 udptlmaxdatagram = 0;
1247 if (udptlmaxdatagram > LOCAL_FAX_MAX_DATAGRAM)
1248 udptlmaxdatagram = LOCAL_FAX_MAX_DATAGRAM;
1250 if ((s = ast_variable_retrieve(cfg, "general", "UDPTLFECentries"))) {
1251 udptlfecentries = atoi(s);
1252 if (udptlfecentries < 0)
1253 udptlfecentries = 0;
1254 if (udptlfecentries > MAX_FEC_ENTRIES)
1255 udptlfecentries = MAX_FEC_ENTRIES;
1257 if ((s = ast_variable_retrieve(cfg, "general", "UDPTLFECspan"))) {
1258 udptlfecspan = atoi(s);
1259 if (udptlfecspan < 0)
1261 if (udptlfecspan > MAX_FEC_SPAN)
1262 udptlfecspan = MAX_FEC_SPAN;
1264 ast_config_destroy(cfg);
1266 if (udptlstart >= udptlend) {
1267 ast_log(LOG_WARNING, "Unreasonable values for UDPTL start/end\n");
1271 if (option_verbose > 1)
1272 ast_verbose(VERBOSE_PREFIX_2 "UDPTL allocating from port range %d -> %d\n", udptlstart, udptlend);
1275 void ast_udptl_init(void)
1277 ast_cli_register_multiple(cli_udptl, sizeof(cli_udptl) / sizeof(struct ast_cli_entry));