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;
122 struct sched_context *sched;
123 struct io_context *io;
125 ast_udptl_callback callback;
126 int udptl_offered_from_local;
128 /*! This option indicates the error correction scheme used in transmitted UDPTL
130 int error_correction_scheme;
132 /*! This option indicates the number of error correction entries transmitted in
134 int error_correction_entries;
136 /*! This option indicates the span of the error correction entries in transmitted
137 UDPTL packets (FEC only). */
138 int error_correction_span;
140 /*! This option indicates the maximum size of a UDPTL packet that can be accepted by
141 the remote device. */
142 int far_max_datagram_size;
144 /*! This option indicates the maximum size of a UDPTL packet that we are prepared to
146 int local_max_datagram_size;
150 struct sockaddr_in far;
154 int rx_expected_seq_no;
156 udptl_fec_tx_buffer_t tx[UDPTL_BUF_MASK + 1];
157 udptl_fec_rx_buffer_t rx[UDPTL_BUF_MASK + 1];
160 static AST_RWLIST_HEAD_STATIC(protos, ast_udptl_protocol);
162 static int udptl_rx_packet(struct ast_udptl *s, uint8_t *buf, int len);
163 static int udptl_build_packet(struct ast_udptl *s, uint8_t *buf, uint8_t *ifp, int ifp_len);
165 static inline int udptl_debug_test_addr(struct sockaddr_in *addr)
169 if (udptldebugaddr.sin_addr.s_addr) {
170 if (((ntohs(udptldebugaddr.sin_port) != 0)
171 && (udptldebugaddr.sin_port != addr->sin_port))
172 || (udptldebugaddr.sin_addr.s_addr != addr->sin_addr.s_addr))
178 static int decode_length(uint8_t *buf, int limit, int *len, int *pvalue)
180 if ((buf[*len] & 0x80) == 0) {
187 if ((buf[*len] & 0x40) == 0) {
188 if (*len >= limit - 1)
190 *pvalue = (buf[*len] & 0x3F) << 8;
192 *pvalue |= buf[*len];
198 *pvalue = (buf[*len] & 0x3F) << 14;
200 /* Indicate we have a fragment */
203 /*- End of function --------------------------------------------------------*/
205 static int decode_open_type(uint8_t *buf, int limit, int *len, const uint8_t **p_object, int *p_num_octets)
211 const uint8_t **pbuf;
213 for (octet_idx = 0, *p_num_octets = 0; ; octet_idx += octet_cnt) {
214 if ((stat = decode_length(buf, limit, len, &octet_cnt)) < 0)
217 *p_num_octets += octet_cnt;
219 pbuf = &p_object[octet_idx];
221 /* Make sure the buffer contains at least the number of bits requested */
222 if ((*len + octet_cnt) > limit)
233 /*- End of function --------------------------------------------------------*/
235 static int encode_length(uint8_t *buf, int *len, int value)
245 if (value < 0x4000) {
247 /* Set the first bit of the first octet */
248 buf[*len] = ((0x8000 | value) >> 8) & 0xFF;
250 buf[*len] = value & 0xFF;
255 multiplier = (value < 0x10000) ? (value >> 14) : 4;
256 /* Set the first 2 bits of the octet */
257 buf[*len] = 0xC0 | multiplier;
259 return multiplier << 14;
261 /*- End of function --------------------------------------------------------*/
263 static int encode_open_type(uint8_t *buf, int *len, const uint8_t *data, int num_octets)
269 /* If open type is of zero length, add a single zero byte (10.1) */
270 if (num_octets == 0) {
275 /* Encode the open type */
276 for (octet_idx = 0; ; num_octets -= enclen, octet_idx += enclen) {
277 if ((enclen = encode_length(buf, len, num_octets)) < 0)
280 memcpy(&buf[*len], &data[octet_idx], enclen);
283 if (enclen >= num_octets)
289 /*- End of function --------------------------------------------------------*/
291 static int udptl_rx_packet(struct ast_udptl *s, uint8_t *buf, int len)
311 const uint8_t *bufs[16];
319 memset(&s->f[0], 0, sizeof(s->f[0]));
321 /* Decode seq_number */
324 seq_no = (buf[0] << 8) | buf[1];
327 /* Break out the primary packet */
328 if ((stat = decode_open_type(buf, len, &ptr, &ifp, &ifp_len)) != 0)
330 /* Decode error_recovery */
333 if ((buf[ptr++] & 0x80) == 0) {
334 /* Secondary packet mode for error recovery */
335 if (seq_no > s->rx_seq_no) {
336 /* We received a later packet than we expected, so we need to check if we can fill in the gap from the
337 secondary packets. */
340 if ((stat2 = decode_length(buf, len, &ptr, &count)) < 0)
342 for (i = 0; i < count; i++) {
343 if ((stat = decode_open_type(buf, len, &ptr, &bufs[total_count + i], &lengths[total_count + i])) != 0)
346 total_count += count;
349 /* Step through in reverse order, so we go oldest to newest */
350 for (i = total_count; i > 0; i--) {
351 if (seq_no - i >= s->rx_seq_no) {
352 /* This one wasn't seen before */
353 /* Decode the secondary IFP packet */
354 //fprintf(stderr, "Secondary %d, len %d\n", seq_no - i, lengths[i - 1]);
355 s->f[ifp_no].frametype = AST_FRAME_MODEM;
356 s->f[ifp_no].subclass = AST_MODEM_T38;
358 s->f[ifp_no].mallocd = 0;
359 //s->f[ifp_no].???seq_no = seq_no - i;
360 s->f[ifp_no].datalen = lengths[i - 1];
361 s->f[ifp_no].data = (uint8_t *) bufs[i - 1];
362 s->f[ifp_no].offset = 0;
363 s->f[ifp_no].src = "UDPTL";
365 AST_LIST_NEXT(&s->f[ifp_no - 1], frame_list) = &s->f[ifp_no];
366 AST_LIST_NEXT(&s->f[ifp_no], frame_list) = NULL;
371 /* If packets are received out of sequence, we may have already processed this packet from the error
372 recovery information in a packet already received. */
373 if (seq_no >= s->rx_seq_no) {
374 /* Decode the primary IFP packet */
375 s->f[ifp_no].frametype = AST_FRAME_MODEM;
376 s->f[ifp_no].subclass = AST_MODEM_T38;
378 s->f[ifp_no].mallocd = 0;
379 //s->f[ifp_no].???seq_no = seq_no;
380 s->f[ifp_no].datalen = ifp_len;
381 s->f[ifp_no].data = (uint8_t *) ifp;
382 s->f[ifp_no].offset = 0;
383 s->f[ifp_no].src = "UDPTL";
385 AST_LIST_NEXT(&s->f[ifp_no - 1], frame_list) = &s->f[ifp_no];
386 AST_LIST_NEXT(&s->f[ifp_no], frame_list) = NULL;
391 /* FEC mode for error recovery */
392 /* Our buffers cannot tolerate overlength IFP packets in FEC mode */
393 if (ifp_len > LOCAL_FAX_MAX_DATAGRAM)
395 /* Update any missed slots in the buffer */
396 for ( ; seq_no > s->rx_seq_no; s->rx_seq_no++) {
397 x = s->rx_seq_no & UDPTL_BUF_MASK;
398 s->rx[x].buf_len = -1;
399 s->rx[x].fec_len[0] = 0;
400 s->rx[x].fec_span = 0;
401 s->rx[x].fec_entries = 0;
404 x = seq_no & UDPTL_BUF_MASK;
406 memset(repaired, 0, sizeof(repaired));
408 /* Save the new IFP packet */
409 memcpy(s->rx[x].buf, ifp, ifp_len);
410 s->rx[x].buf_len = ifp_len;
413 /* Decode the FEC packets */
414 /* The span is defined as an unconstrained integer, but will never be more
415 than a small value. */
421 s->rx[x].fec_span = span;
423 /* The number of entries is defined as a length, but will only ever be a small
424 value. Treat it as such. */
427 entries = buf[ptr++];
428 s->rx[x].fec_entries = entries;
430 /* Decode the elements */
431 for (i = 0; i < entries; i++) {
432 if ((stat = decode_open_type(buf, len, &ptr, &data, &s->rx[x].fec_len[i])) != 0)
434 if (s->rx[x].fec_len[i] > LOCAL_FAX_MAX_DATAGRAM)
437 /* Save the new FEC data */
438 memcpy(s->rx[x].fec[i], data, s->rx[x].fec_len[i]);
440 fprintf(stderr, "FEC: ");
441 for (j = 0; j < s->rx[x].fec_len[i]; j++)
442 fprintf(stderr, "%02X ", data[j]);
443 fprintf(stderr, "\n");
447 /* See if we can reconstruct anything which is missing */
448 /* TODO: this does not comprehensively hunt back and repair everything that is possible */
449 for (l = x; l != ((x - (16 - span*entries)) & UDPTL_BUF_MASK); l = (l - 1) & UDPTL_BUF_MASK) {
450 if (s->rx[l].fec_len[0] <= 0)
452 for (m = 0; m < s->rx[l].fec_entries; m++) {
453 limit = (l + m) & UDPTL_BUF_MASK;
454 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) {
455 if (s->rx[k].buf_len <= 0)
456 which = (which == -1) ? k : -2;
460 for (j = 0; j < s->rx[l].fec_len[m]; j++) {
461 s->rx[which].buf[j] = s->rx[l].fec[m][j];
462 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)
463 s->rx[which].buf[j] ^= (s->rx[k].buf_len > j) ? s->rx[k].buf[j] : 0;
465 s->rx[which].buf_len = s->rx[l].fec_len[m];
466 repaired[which] = TRUE;
470 /* Now play any new packets forwards in time */
471 for (l = (x + 1) & UDPTL_BUF_MASK, j = seq_no - UDPTL_BUF_MASK; l != x; l = (l + 1) & UDPTL_BUF_MASK, j++) {
473 //fprintf(stderr, "Fixed packet %d, len %d\n", j, l);
474 s->f[ifp_no].frametype = AST_FRAME_MODEM;
475 s->f[ifp_no].subclass = AST_MODEM_T38;
477 s->f[ifp_no].mallocd = 0;
478 //s->f[ifp_no].???seq_no = j;
479 s->f[ifp_no].datalen = s->rx[l].buf_len;
480 s->f[ifp_no].data = s->rx[l].buf;
481 s->f[ifp_no].offset = 0;
482 s->f[ifp_no].src = "UDPTL";
484 AST_LIST_NEXT(&s->f[ifp_no - 1], frame_list) = &s->f[ifp_no];
485 AST_LIST_NEXT(&s->f[ifp_no], frame_list) = NULL;
489 /* Decode the primary IFP packet */
490 s->f[ifp_no].frametype = AST_FRAME_MODEM;
491 s->f[ifp_no].subclass = AST_MODEM_T38;
493 s->f[ifp_no].mallocd = 0;
494 //s->f[ifp_no].???seq_no = j;
495 s->f[ifp_no].datalen = ifp_len;
496 s->f[ifp_no].data = (uint8_t *) ifp;
497 s->f[ifp_no].offset = 0;
498 s->f[ifp_no].src = "UDPTL";
500 AST_LIST_NEXT(&s->f[ifp_no - 1], frame_list) = &s->f[ifp_no];
501 AST_LIST_NEXT(&s->f[ifp_no], frame_list) = NULL;
504 s->rx_seq_no = seq_no + 1;
507 /*- End of function --------------------------------------------------------*/
509 static int udptl_build_packet(struct ast_udptl *s, uint8_t *buf, uint8_t *ifp, int ifp_len)
511 uint8_t fec[LOCAL_FAX_MAX_DATAGRAM];
523 seq = s->tx_seq_no & 0xFFFF;
525 /* Map the sequence number to an entry in the circular buffer */
526 entry = seq & UDPTL_BUF_MASK;
528 /* We save the message in a circular buffer, for generating FEC or
529 redundancy sets later on. */
530 s->tx[entry].buf_len = ifp_len;
531 memcpy(s->tx[entry].buf, ifp, ifp_len);
533 /* Build the UDPTLPacket */
536 /* Encode the sequence number */
537 buf[len++] = (seq >> 8) & 0xFF;
538 buf[len++] = seq & 0xFF;
540 /* Encode the primary IFP packet */
541 if (encode_open_type(buf, &len, ifp, ifp_len) < 0)
544 /* Encode the appropriate type of error recovery information */
545 switch (s->error_correction_scheme)
547 case UDPTL_ERROR_CORRECTION_NONE:
548 /* Encode the error recovery type */
550 /* The number of entries will always be zero, so it is pointless allowing
551 for the fragmented case here. */
552 if (encode_length(buf, &len, 0) < 0)
555 case UDPTL_ERROR_CORRECTION_REDUNDANCY:
556 /* Encode the error recovery type */
558 if (s->tx_seq_no > s->error_correction_entries)
559 entries = s->error_correction_entries;
561 entries = s->tx_seq_no;
562 /* The number of entries will always be small, so it is pointless allowing
563 for the fragmented case here. */
564 if (encode_length(buf, &len, entries) < 0)
566 /* Encode the elements */
567 for (i = 0; i < entries; i++) {
568 j = (entry - i - 1) & UDPTL_BUF_MASK;
569 if (encode_open_type(buf, &len, s->tx[j].buf, s->tx[j].buf_len) < 0)
573 case UDPTL_ERROR_CORRECTION_FEC:
574 span = s->error_correction_span;
575 entries = s->error_correction_entries;
576 if (seq < s->error_correction_span*s->error_correction_entries) {
577 /* In the initial stages, wind up the FEC smoothly */
578 entries = seq/s->error_correction_span;
579 if (seq < s->error_correction_span)
582 /* Encode the error recovery type */
584 /* Span is defined as an inconstrained integer, which it dumb. It will only
585 ever be a small value. Treat it as such. */
588 /* The number of entries is defined as a length, but will only ever be a small
589 value. Treat it as such. */
590 buf[len++] = entries;
591 for (m = 0; m < entries; m++) {
592 /* Make an XOR'ed entry the maximum length */
593 limit = (entry + m) & UDPTL_BUF_MASK;
595 for (i = (limit - span*entries) & UDPTL_BUF_MASK; i != limit; i = (i + entries) & UDPTL_BUF_MASK) {
596 if (high_tide < s->tx[i].buf_len) {
597 for (j = 0; j < high_tide; j++)
598 fec[j] ^= s->tx[i].buf[j];
599 for ( ; j < s->tx[i].buf_len; j++)
600 fec[j] = s->tx[i].buf[j];
601 high_tide = s->tx[i].buf_len;
603 for (j = 0; j < s->tx[i].buf_len; j++)
604 fec[j] ^= s->tx[i].buf[j];
607 if (encode_open_type(buf, &len, fec, high_tide) < 0)
614 fprintf(stderr, "\n");
620 int ast_udptl_fd(struct ast_udptl *udptl)
625 void ast_udptl_set_data(struct ast_udptl *udptl, void *data)
630 void ast_udptl_set_callback(struct ast_udptl *udptl, ast_udptl_callback callback)
632 udptl->callback = callback;
635 void ast_udptl_setnat(struct ast_udptl *udptl, int nat)
640 static int udptlread(int *id, int fd, short events, void *cbdata)
642 struct ast_udptl *udptl = cbdata;
645 if ((f = ast_udptl_read(udptl))) {
647 udptl->callback(udptl, f, udptl->data);
652 struct ast_frame *ast_udptl_read(struct ast_udptl *udptl)
655 struct sockaddr_in sin;
658 uint16_t *udptlheader;
662 /* Cache where the header will go */
663 res = recvfrom(udptl->fd,
664 udptl->rawdata + AST_FRIENDLY_OFFSET,
665 sizeof(udptl->rawdata) - AST_FRIENDLY_OFFSET,
667 (struct sockaddr *) &sin,
669 udptlheader = (uint16_t *)(udptl->rawdata + AST_FRIENDLY_OFFSET);
672 ast_log(LOG_WARNING, "UDPTL read error: %s\n", strerror(errno));
675 return &ast_null_frame;
678 /* Ignore if the other side hasn't been given an address yet. */
679 if (!udptl->them.sin_addr.s_addr || !udptl->them.sin_port)
680 return &ast_null_frame;
683 /* Send to whoever sent to us */
684 if ((udptl->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
685 (udptl->them.sin_port != sin.sin_port)) {
686 memcpy(&udptl->them, &sin, sizeof(udptl->them));
687 ast_debug(1, "UDPTL NAT: Using address %s:%d\n", ast_inet_ntoa(udptl->them.sin_addr), ntohs(udptl->them.sin_port));
691 if (udptl_debug_test_addr(&sin)) {
692 ast_verb(1, "Got UDPTL packet from %s:%d (type %d, seq %d, len %d)\n",
693 ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), 0, seqno, res);
696 printf("Got UDPTL packet from %s:%d (seq %d, len = %d)\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), seqno, res);
698 udptl_rx_packet(udptl, udptl->rawdata + AST_FRIENDLY_OFFSET, res);
703 void ast_udptl_offered_from_local(struct ast_udptl* udptl, int local)
706 udptl->udptl_offered_from_local = local;
708 ast_log(LOG_WARNING, "udptl structure is null\n");
711 int ast_udptl_get_error_correction_scheme(struct ast_udptl* udptl)
714 return udptl->error_correction_scheme;
716 ast_log(LOG_WARNING, "udptl structure is null\n");
721 void ast_udptl_set_error_correction_scheme(struct ast_udptl* udptl, int ec)
725 case UDPTL_ERROR_CORRECTION_FEC:
726 udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_FEC;
728 case UDPTL_ERROR_CORRECTION_REDUNDANCY:
729 udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_REDUNDANCY;
731 case UDPTL_ERROR_CORRECTION_NONE:
732 udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_NONE;
735 ast_log(LOG_WARNING, "error correction parameter invalid\n");
738 ast_log(LOG_WARNING, "udptl structure is null\n");
741 int ast_udptl_get_local_max_datagram(struct ast_udptl* udptl)
744 return udptl->local_max_datagram_size;
746 ast_log(LOG_WARNING, "udptl structure is null\n");
751 int ast_udptl_get_far_max_datagram(struct ast_udptl* udptl)
754 return udptl->far_max_datagram_size;
756 ast_log(LOG_WARNING, "udptl structure is null\n");
761 void ast_udptl_set_local_max_datagram(struct ast_udptl* udptl, int max_datagram)
764 udptl->local_max_datagram_size = max_datagram;
766 ast_log(LOG_WARNING, "udptl structure is null\n");
769 void ast_udptl_set_far_max_datagram(struct ast_udptl* udptl, int max_datagram)
772 udptl->far_max_datagram_size = max_datagram;
774 ast_log(LOG_WARNING, "udptl structure is null\n");
777 struct ast_udptl *ast_udptl_new_with_bindaddr(struct sched_context *sched, struct io_context *io, int callbackmode, struct in_addr addr)
779 struct ast_udptl *udptl;
785 if (!(udptl = ast_calloc(1, sizeof(*udptl))))
788 if (udptlfectype == 2)
789 udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_FEC;
790 else if (udptlfectype == 1)
791 udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_REDUNDANCY;
793 udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_NONE;
794 udptl->error_correction_span = udptlfecspan;
795 udptl->error_correction_entries = udptlfecentries;
797 udptl->far_max_datagram_size = udptlmaxdatagram;
798 udptl->local_max_datagram_size = udptlmaxdatagram;
800 memset(&udptl->rx, 0, sizeof(udptl->rx));
801 memset(&udptl->tx, 0, sizeof(udptl->tx));
802 for (i = 0; i <= UDPTL_BUF_MASK; i++) {
803 udptl->rx[i].buf_len = -1;
804 udptl->tx[i].buf_len = -1;
807 udptl->seqno = ast_random() & 0xffff;
808 udptl->them.sin_family = AF_INET;
809 udptl->us.sin_family = AF_INET;
811 if ((udptl->fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
813 ast_log(LOG_WARNING, "Unable to allocate socket: %s\n", strerror(errno));
816 flags = fcntl(udptl->fd, F_GETFL);
817 fcntl(udptl->fd, F_SETFL, flags | O_NONBLOCK);
820 setsockopt(udptl->fd, SOL_SOCKET, SO_NO_CHECK, &nochecksums, sizeof(nochecksums));
822 /* Find us a place */
823 x = (ast_random() % (udptlend - udptlstart)) + udptlstart;
826 udptl->us.sin_port = htons(x);
827 udptl->us.sin_addr = addr;
828 if (bind(udptl->fd, (struct sockaddr *) &udptl->us, sizeof(udptl->us)) == 0)
830 if (errno != EADDRINUSE) {
831 ast_log(LOG_WARNING, "Unexpected bind error: %s\n", strerror(errno));
838 if (x == startplace) {
839 ast_log(LOG_WARNING, "No UDPTL ports remaining\n");
845 if (io && sched && callbackmode) {
846 /* Operate this one in a callback mode */
847 udptl->sched = sched;
849 udptl->ioid = ast_io_add(udptl->io, udptl->fd, udptlread, AST_IO_IN, udptl);
854 struct ast_udptl *ast_udptl_new(struct sched_context *sched, struct io_context *io, int callbackmode)
857 memset(&ia, 0, sizeof(ia));
858 return ast_udptl_new_with_bindaddr(sched, io, callbackmode, ia);
861 int ast_udptl_setqos(struct ast_udptl *udptl, int tos, int cos)
863 return ast_netsock_set_qos(udptl->fd, tos, cos);
866 void ast_udptl_set_peer(struct ast_udptl *udptl, struct sockaddr_in *them)
868 udptl->them.sin_port = them->sin_port;
869 udptl->them.sin_addr = them->sin_addr;
872 void ast_udptl_get_peer(struct ast_udptl *udptl, struct sockaddr_in *them)
874 them->sin_family = AF_INET;
875 them->sin_port = udptl->them.sin_port;
876 them->sin_addr = udptl->them.sin_addr;
879 void ast_udptl_get_us(struct ast_udptl *udptl, struct sockaddr_in *us)
881 memcpy(us, &udptl->us, sizeof(udptl->us));
884 void ast_udptl_stop(struct ast_udptl *udptl)
886 memset(&udptl->them.sin_addr, 0, sizeof(udptl->them.sin_addr));
887 memset(&udptl->them.sin_port, 0, sizeof(udptl->them.sin_port));
890 void ast_udptl_destroy(struct ast_udptl *udptl)
893 ast_io_remove(udptl->io, udptl->ioid);
899 int ast_udptl_write(struct ast_udptl *s, struct ast_frame *f)
903 uint8_t buf[LOCAL_FAX_MAX_DATAGRAM];
905 /* If we have no peer, return immediately */
906 if (s->them.sin_addr.s_addr == INADDR_ANY)
909 /* If there is no data length, return immediately */
913 if (f->frametype != AST_FRAME_MODEM) {
914 ast_log(LOG_WARNING, "UDPTL can only send T.38 data\n");
918 /* Cook up the UDPTL packet, with the relevant EC info. */
919 len = udptl_build_packet(s, buf, f->data, f->datalen);
921 if (len > 0 && s->them.sin_port && s->them.sin_addr.s_addr) {
922 if ((res = sendto(s->fd, buf, len, 0, (struct sockaddr *) &s->them, sizeof(s->them))) < 0)
923 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));
925 printf("Sent %d bytes of UDPTL data to %s:%d\n", res, ast_inet_ntoa(udptl->them.sin_addr), ntohs(udptl->them.sin_port));
927 if (udptl_debug_test_addr(&s->them))
928 ast_verbose("Sent UDPTL packet to %s:%d (type %d, seq %d, len %d)\n",
929 ast_inet_ntoa(s->them.sin_addr),
930 ntohs(s->them.sin_port), 0, s->seqno, len);
936 void ast_udptl_proto_unregister(struct ast_udptl_protocol *proto)
938 AST_RWLIST_WRLOCK(&protos);
939 AST_RWLIST_REMOVE(&protos, proto, list);
940 AST_RWLIST_UNLOCK(&protos);
943 int ast_udptl_proto_register(struct ast_udptl_protocol *proto)
945 struct ast_udptl_protocol *cur;
947 AST_RWLIST_WRLOCK(&protos);
948 AST_RWLIST_TRAVERSE(&protos, cur, list) {
949 if (cur->type == proto->type) {
950 ast_log(LOG_WARNING, "Tried to register same protocol '%s' twice\n", cur->type);
951 AST_RWLIST_UNLOCK(&protos);
955 AST_RWLIST_INSERT_TAIL(&protos, proto, list);
956 AST_RWLIST_UNLOCK(&protos);
960 static struct ast_udptl_protocol *get_proto(struct ast_channel *chan)
962 struct ast_udptl_protocol *cur = NULL;
964 AST_RWLIST_RDLOCK(&protos);
965 AST_RWLIST_TRAVERSE(&protos, cur, list) {
966 if (cur->type == chan->tech->type)
969 AST_RWLIST_UNLOCK(&protos);
974 int ast_udptl_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
977 struct ast_channel *who;
978 struct ast_channel *cs[3];
979 struct ast_udptl *p0;
980 struct ast_udptl *p1;
981 struct ast_udptl_protocol *pr0;
982 struct ast_udptl_protocol *pr1;
983 struct sockaddr_in ac0;
984 struct sockaddr_in ac1;
985 struct sockaddr_in t0;
986 struct sockaddr_in t1;
991 ast_channel_lock(c0);
992 while (ast_channel_trylock(c1)) {
993 ast_channel_unlock(c0);
995 ast_channel_lock(c0);
1000 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c0->name);
1001 ast_channel_unlock(c0);
1002 ast_channel_unlock(c1);
1006 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c1->name);
1007 ast_channel_unlock(c0);
1008 ast_channel_unlock(c1);
1011 pvt0 = c0->tech_pvt;
1012 pvt1 = c1->tech_pvt;
1013 p0 = pr0->get_udptl_info(c0);
1014 p1 = pr1->get_udptl_info(c1);
1016 /* Somebody doesn't want to play... */
1017 ast_channel_unlock(c0);
1018 ast_channel_unlock(c1);
1021 if (pr0->set_udptl_peer(c0, p1)) {
1022 ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c0->name, c1->name);
1024 /* Store UDPTL peer */
1025 ast_udptl_get_peer(p1, &ac1);
1027 if (pr1->set_udptl_peer(c1, p0))
1028 ast_log(LOG_WARNING, "Channel '%s' failed to talk back to '%s'\n", c1->name, c0->name);
1030 /* Store UDPTL peer */
1031 ast_udptl_get_peer(p0, &ac0);
1033 ast_channel_unlock(c0);
1034 ast_channel_unlock(c1);
1039 if ((c0->tech_pvt != pvt0) ||
1040 (c1->tech_pvt != pvt1) ||
1041 (c0->masq || c0->masqr || c1->masq || c1->masqr)) {
1042 ast_debug(1, "Oooh, something is weird, backing out\n");
1043 /* Tell it to try again later */
1047 ast_udptl_get_peer(p1, &t1);
1048 ast_udptl_get_peer(p0, &t0);
1049 if (inaddrcmp(&t1, &ac1)) {
1050 ast_debug(1, "Oooh, '%s' changed end address to %s:%d\n",
1051 c1->name, ast_inet_ntoa(t1.sin_addr), ntohs(t1.sin_port));
1052 ast_debug(1, "Oooh, '%s' was %s:%d\n",
1053 c1->name, ast_inet_ntoa(ac1.sin_addr), ntohs(ac1.sin_port));
1054 memcpy(&ac1, &t1, sizeof(ac1));
1056 if (inaddrcmp(&t0, &ac0)) {
1057 ast_debug(1, "Oooh, '%s' changed end address to %s:%d\n",
1058 c0->name, ast_inet_ntoa(t0.sin_addr), ntohs(t0.sin_port));
1059 ast_debug(1, "Oooh, '%s' was %s:%d\n",
1060 c0->name, ast_inet_ntoa(ac0.sin_addr), ntohs(ac0.sin_port));
1061 memcpy(&ac0, &t0, sizeof(ac0));
1063 who = ast_waitfor_n(cs, 2, &to);
1065 ast_debug(1, "Ooh, empty read...\n");
1066 /* check for hangup / whentohangup */
1067 if (ast_check_hangup(c0) || ast_check_hangup(c1))
1075 ast_debug(1, "Oooh, got a %s\n", f ? "digit" : "hangup");
1076 /* That's all we needed */
1079 if (f->frametype == AST_FRAME_MODEM) {
1080 /* Forward T.38 frames if they happen upon us */
1083 } else if (who == c1) {
1089 /* Swap priority. Not that it's a big deal at this point */
1097 static char *handle_cli_udptl_debug_ip(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1100 struct ast_hostent ahp;
1107 e->command = "udptl debug ip";
1109 "Usage: udptl debug [ip host[:port]]\n"
1110 " Enable dumping of all UDPTL packets to and from host.\n";
1119 return CLI_SHOWUSAGE;
1121 p = strstr(arg, ":");
1127 hp = ast_gethostbyname(arg, &ahp);
1129 return CLI_SHOWUSAGE;
1130 udptldebugaddr.sin_family = AF_INET;
1131 memcpy(&udptldebugaddr.sin_addr, hp->h_addr, sizeof(udptldebugaddr.sin_addr));
1132 udptldebugaddr.sin_port = htons(port);
1134 ast_cli(a->fd, "UDPTL Debugging Enabled for IP: %s\n", ast_inet_ntoa(udptldebugaddr.sin_addr));
1136 ast_cli(a->fd, "UDPTL Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(udptldebugaddr.sin_addr), port);
1141 static char *handle_cli_udptl_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1145 e->command = "udptl debug";
1147 "Usage: udptl debug\n"
1148 " Enable dumping of all UDPTL packets.\n";
1155 return CLI_SHOWUSAGE;
1158 memset(&udptldebugaddr, 0, sizeof(udptldebugaddr));
1160 ast_cli(a->fd, "UDPTL Debugging Enabled\n");
1164 static char *handle_cli_udptl_debug_off(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1168 e->command = "udptl debug off";
1170 "Usage: udptl debug off\n"
1171 " Disable dumping of all UDPTL packets.\n";
1178 return CLI_SHOWUSAGE;
1182 ast_cli(a->fd, "UDPTL Debugging Disabled\n");
1186 static struct ast_cli_entry cli_udptl[] = {
1187 AST_CLI_DEFINE(handle_cli_udptl_debug, "Enable UDPTL debugging"),
1188 AST_CLI_DEFINE(handle_cli_udptl_debug_ip, "Enable UDPTL debugging on IP"),
1189 AST_CLI_DEFINE(handle_cli_udptl_debug_off, "Disable UDPTL debugging")
1192 static void __ast_udptl_reload(int reload)
1194 struct ast_config *cfg;
1196 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
1198 if ((cfg = ast_config_load("udptl.conf", config_flags)) == CONFIG_STATUS_FILEUNCHANGED)
1204 udptlfecentries = 0;
1206 udptlmaxdatagram = 0;
1209 if ((s = ast_variable_retrieve(cfg, "general", "udptlstart"))) {
1210 udptlstart = atoi(s);
1211 if (udptlstart < 1024)
1213 if (udptlstart > 65535)
1216 if ((s = ast_variable_retrieve(cfg, "general", "udptlend"))) {
1218 if (udptlend < 1024)
1220 if (udptlend > 65535)
1223 if ((s = ast_variable_retrieve(cfg, "general", "udptlchecksums"))) {
1231 ast_log(LOG_WARNING, "Disabling UDPTL checksums is not supported on this operating system!\n");
1234 if ((s = ast_variable_retrieve(cfg, "general", "T38FaxUdpEC"))) {
1235 if (strcmp(s, "t38UDPFEC") == 0)
1237 else if (strcmp(s, "t38UDPRedundancy") == 0)
1240 if ((s = ast_variable_retrieve(cfg, "general", "T38FaxMaxDatagram"))) {
1241 udptlmaxdatagram = atoi(s);
1242 if (udptlmaxdatagram < 0)
1243 udptlmaxdatagram = 0;
1244 if (udptlmaxdatagram > LOCAL_FAX_MAX_DATAGRAM)
1245 udptlmaxdatagram = LOCAL_FAX_MAX_DATAGRAM;
1247 if ((s = ast_variable_retrieve(cfg, "general", "UDPTLFECentries"))) {
1248 udptlfecentries = atoi(s);
1249 if (udptlfecentries < 0)
1250 udptlfecentries = 0;
1251 if (udptlfecentries > MAX_FEC_ENTRIES)
1252 udptlfecentries = MAX_FEC_ENTRIES;
1254 if ((s = ast_variable_retrieve(cfg, "general", "UDPTLFECspan"))) {
1255 udptlfecspan = atoi(s);
1256 if (udptlfecspan < 0)
1258 if (udptlfecspan > MAX_FEC_SPAN)
1259 udptlfecspan = MAX_FEC_SPAN;
1261 ast_config_destroy(cfg);
1263 if (udptlstart >= udptlend) {
1264 ast_log(LOG_WARNING, "Unreasonable values for UDPTL start/end\n");
1268 ast_verb(2, "UDPTL allocating from port range %d -> %d\n", udptlstart, udptlend);
1271 void ast_udptl_reload(void)
1273 __ast_udptl_reload(1);
1276 void ast_udptl_init(void)
1278 ast_cli_register_multiple(cli_udptl, sizeof(cli_udptl) / sizeof(struct ast_cli_entry));
1279 __ast_udptl_reload(0);