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 * This program is free software, distributed under the terms of
12 * the GNU General Public License
14 * A license has been granted to Digium (via disclaimer) for the use of
20 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
29 #include <netinet/in.h>
31 #include <sys/socket.h>
32 #include <arpa/inet.h>
35 #include "asterisk/udptl.h"
36 #include "asterisk/frame.h"
37 #include "asterisk/logger.h"
38 #include "asterisk/options.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/acl.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/config.h"
43 #include "asterisk/lock.h"
44 #include "asterisk/utils.h"
45 #include "asterisk/cli.h"
46 #include "asterisk/unaligned.h"
47 #include "asterisk/utils.h"
49 #define UDPTL_MTU 1200
58 static int udptlstart
;
60 static int udptldebug
; /* Are we debugging? */
61 static struct sockaddr_in udptldebugaddr
; /* Debug packets to/from this host */
63 static int nochecksums
;
65 static int udptlfectype
;
66 static int udptlfecentries
;
67 static int udptlfecspan
;
68 static int udptlmaxdatagram
;
70 #define LOCAL_FAX_MAX_DATAGRAM 400
71 #define MAX_FEC_ENTRIES 5
72 #define MAX_FEC_SPAN 5
74 #define UDPTL_BUF_MASK 15
78 uint8_t buf
[LOCAL_FAX_MAX_DATAGRAM
];
79 } udptl_fec_tx_buffer_t
;
83 uint8_t buf
[LOCAL_FAX_MAX_DATAGRAM
];
84 int fec_len
[MAX_FEC_ENTRIES
];
85 uint8_t fec
[MAX_FEC_ENTRIES
][LOCAL_FAX_MAX_DATAGRAM
];
88 } udptl_fec_rx_buffer_t
;
93 struct ast_frame f
[16];
94 unsigned char rawdata
[8192 + AST_FRIENDLY_OFFSET
];
95 unsigned int lasteventseqn
;
98 struct sockaddr_in us
;
99 struct sockaddr_in them
;
101 struct sched_context
*sched
;
102 struct io_context
*io
;
104 ast_udptl_callback callback
;
105 int udptl_offered_from_local
;
107 /*! This option indicates the error correction scheme used in transmitted UDPTL
109 int error_correction_scheme
;
111 /*! This option indicates the number of error correction entries transmitted in
113 int error_correction_entries
;
115 /*! This option indicates the span of the error correction entries in transmitted
116 UDPTL packets (FEC only). */
117 int error_correction_span
;
119 /*! This option indicates the maximum size of a UDPTL packet that can be accepted by
120 the remote device. */
121 int far_max_datagram_size
;
123 /*! This option indicates the maximum size of a UDPTL packet that we are prepared to
125 int local_max_datagram_size
;
129 struct sockaddr_in far
;
133 int rx_expected_seq_no
;
135 udptl_fec_tx_buffer_t tx
[UDPTL_BUF_MASK
+ 1];
136 udptl_fec_rx_buffer_t rx
[UDPTL_BUF_MASK
+ 1];
139 static struct ast_udptl_protocol
*protos
;
141 static int udptl_rx_packet(struct ast_udptl
*s
, uint8_t *buf
, int len
);
142 static int udptl_build_packet(struct ast_udptl
*s
, uint8_t *buf
, uint8_t *ifp
, int ifp_len
);
144 static inline int udptl_debug_test_addr(struct sockaddr_in
*addr
)
148 if (udptldebugaddr
.sin_addr
.s_addr
) {
149 if (((ntohs(udptldebugaddr
.sin_port
) != 0)
150 && (udptldebugaddr
.sin_port
!= addr
->sin_port
))
151 || (udptldebugaddr
.sin_addr
.s_addr
!= addr
->sin_addr
.s_addr
))
157 static int decode_length(uint8_t *buf
, int limit
, int *len
, int *pvalue
)
159 if ((buf
[*len
] & 0x80) == 0) {
166 if ((buf
[*len
] & 0x40) == 0) {
167 if (*len
>= limit
- 1)
169 *pvalue
= (buf
[*len
] & 0x3F) << 8;
171 *pvalue
|= buf
[*len
];
177 *pvalue
= (buf
[*len
] & 0x3F) << 14;
179 /* Indicate we have a fragment */
182 /*- End of function --------------------------------------------------------*/
184 static int decode_open_type(uint8_t *buf
, int limit
, int *len
, const uint8_t **p_object
, int *p_num_octets
)
190 const uint8_t **pbuf
;
192 for (octet_idx
= 0, *p_num_octets
= 0; ; octet_idx
+= octet_cnt
) {
193 if ((stat
= decode_length(buf
, limit
, len
, &octet_cnt
)) < 0)
196 *p_num_octets
+= octet_cnt
;
198 pbuf
= &p_object
[octet_idx
];
200 /* Make sure the buffer contains at least the number of bits requested */
201 if ((*len
+ octet_cnt
) > limit
)
212 /*- End of function --------------------------------------------------------*/
214 static int encode_length(uint8_t *buf
, int *len
, int value
)
224 if (value
< 0x4000) {
226 /* Set the first bit of the first octet */
227 buf
[*len
] = ((0x8000 | value
) >> 8) & 0xFF;
229 buf
[*len
] = value
& 0xFF;
234 multiplier
= (value
< 0x10000) ? (value
>> 14) : 4;
235 /* Set the first 2 bits of the octet */
236 buf
[*len
] = 0xC0 | multiplier
;
238 return multiplier
<< 14;
240 /*- End of function --------------------------------------------------------*/
242 static int encode_open_type(uint8_t *buf
, int *len
, const uint8_t *data
, int num_octets
)
248 /* If open type is of zero length, add a single zero byte (10.1) */
249 if (num_octets
== 0) {
254 /* Encode the open type */
255 for (octet_idx
= 0; ; num_octets
-= enclen
, octet_idx
+= enclen
) {
256 if ((enclen
= encode_length(buf
, len
, num_octets
)) < 0)
259 memcpy(&buf
[*len
], &data
[octet_idx
], enclen
);
262 if (enclen
>= num_octets
)
268 /*- End of function --------------------------------------------------------*/
270 static int udptl_rx_packet(struct ast_udptl
*s
, uint8_t *buf
, int len
)
290 const uint8_t *bufs
[16];
298 memset(&s
->f
[0], 0, sizeof(s
->f
[0]));
300 /* Decode seq_number */
303 seq_no
= (buf
[0] << 8) | buf
[1];
306 /* Break out the primary packet */
307 if ((stat
= decode_open_type(buf
, len
, &ptr
, &ifp
, &ifp_len
)) != 0)
309 /* Decode error_recovery */
312 if ((buf
[ptr
++] & 0x80) == 0) {
313 /* Secondary packet mode for error recovery */
314 if (seq_no
> s
->rx_seq_no
) {
315 /* We received a later packet than we expected, so we need to check if we can fill in the gap from the
316 secondary packets. */
319 if ((stat2
= decode_length(buf
, len
, &ptr
, &count
)) < 0)
321 for (i
= 0; i
< count
; i
++) {
322 if ((stat
= decode_open_type(buf
, len
, &ptr
, &bufs
[total_count
+ i
], &lengths
[total_count
+ i
])) != 0)
325 total_count
+= count
;
328 /* Step through in reverse order, so we go oldest to newest */
329 for (i
= total_count
; i
> 0; i
--) {
330 if (seq_no
- i
>= s
->rx_seq_no
) {
331 /* This one wasn't seen before */
332 /* Decode the secondary IFP packet */
333 //fprintf(stderr, "Secondary %d, len %d\n", seq_no - i, lengths[i - 1]);
334 s
->f
[ifp_no
].frametype
= AST_FRAME_MODEM
;
335 s
->f
[ifp_no
].subclass
= AST_MODEM_T38
;
337 s
->f
[ifp_no
].mallocd
= 0;
338 s
->f
[ifp_no
].seqno
= seq_no
- i
;
339 s
->f
[ifp_no
].datalen
= lengths
[i
- 1];
340 s
->f
[ifp_no
].data
= (uint8_t *) bufs
[i
- 1];
341 s
->f
[ifp_no
].offset
= 0;
342 s
->f
[ifp_no
].src
= "UDPTL";
344 AST_LIST_NEXT(&s
->f
[ifp_no
- 1], frame_list
) = &s
->f
[ifp_no
];
345 AST_LIST_NEXT(&s
->f
[ifp_no
], frame_list
) = NULL
;
353 /* FEC mode for error recovery */
354 /* Our buffers cannot tolerate overlength IFP packets in FEC mode */
355 if (ifp_len
> LOCAL_FAX_MAX_DATAGRAM
)
357 /* Update any missed slots in the buffer */
358 for ( ; seq_no
> s
->rx_seq_no
; s
->rx_seq_no
++) {
359 x
= s
->rx_seq_no
& UDPTL_BUF_MASK
;
360 s
->rx
[x
].buf_len
= -1;
361 s
->rx
[x
].fec_len
[0] = 0;
362 s
->rx
[x
].fec_span
= 0;
363 s
->rx
[x
].fec_entries
= 0;
366 x
= seq_no
& UDPTL_BUF_MASK
;
368 memset(repaired
, 0, sizeof(repaired
));
370 /* Save the new IFP packet */
371 memcpy(s
->rx
[x
].buf
, ifp
, ifp_len
);
372 s
->rx
[x
].buf_len
= ifp_len
;
375 /* Decode the FEC packets */
376 /* The span is defined as an unconstrained integer, but will never be more
377 than a small value. */
383 s
->rx
[x
].fec_span
= span
;
385 /* The number of entries is defined as a length, but will only ever be a small
386 value. Treat it as such. */
389 entries
= buf
[ptr
++];
390 s
->rx
[x
].fec_entries
= entries
;
392 /* Decode the elements */
393 for (i
= 0; i
< entries
; i
++) {
394 if ((stat
= decode_open_type(buf
, len
, &ptr
, &data
, &s
->rx
[x
].fec_len
[i
])) != 0)
396 if (s
->rx
[x
].fec_len
[i
] > LOCAL_FAX_MAX_DATAGRAM
)
399 /* Save the new FEC data */
400 memcpy(s
->rx
[x
].fec
[i
], data
, s
->rx
[x
].fec_len
[i
]);
402 fprintf(stderr
, "FEC: ");
403 for (j
= 0; j
< s
->rx
[x
].fec_len
[i
]; j
++)
404 fprintf(stderr
, "%02X ", data
[j
]);
405 fprintf(stderr
, "\n");
409 /* See if we can reconstruct anything which is missing */
410 /* TODO: this does not comprehensively hunt back and repair everything that is possible */
411 for (l
= x
; l
!= ((x
- (16 - span
*entries
)) & UDPTL_BUF_MASK
); l
= (l
- 1) & UDPTL_BUF_MASK
) {
412 if (s
->rx
[l
].fec_len
[0] <= 0)
414 for (m
= 0; m
< s
->rx
[l
].fec_entries
; m
++) {
415 limit
= (l
+ m
) & UDPTL_BUF_MASK
;
416 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
) {
417 if (s
->rx
[k
].buf_len
<= 0)
418 which
= (which
== -1) ? k
: -2;
422 for (j
= 0; j
< s
->rx
[l
].fec_len
[m
]; j
++) {
423 s
->rx
[which
].buf
[j
] = s
->rx
[l
].fec
[m
][j
];
424 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
)
425 s
->rx
[which
].buf
[j
] ^= (s
->rx
[k
].buf_len
> j
) ? s
->rx
[k
].buf
[j
] : 0;
427 s
->rx
[which
].buf_len
= s
->rx
[l
].fec_len
[m
];
428 repaired
[which
] = TRUE
;
432 /* Now play any new packets forwards in time */
433 for (l
= (x
+ 1) & UDPTL_BUF_MASK
, j
= seq_no
- UDPTL_BUF_MASK
; l
!= x
; l
= (l
+ 1) & UDPTL_BUF_MASK
, j
++) {
435 //fprintf(stderr, "Fixed packet %d, len %d\n", j, l);
436 s
->f
[ifp_no
].frametype
= AST_FRAME_MODEM
;
437 s
->f
[ifp_no
].subclass
= AST_MODEM_T38
;
439 s
->f
[ifp_no
].mallocd
= 0;
440 s
->f
[ifp_no
].seqno
= j
;
441 s
->f
[ifp_no
].datalen
= s
->rx
[l
].buf_len
;
442 s
->f
[ifp_no
].data
= s
->rx
[l
].buf
;
443 s
->f
[ifp_no
].offset
= 0;
444 s
->f
[ifp_no
].src
= "UDPTL";
446 AST_LIST_NEXT(&s
->f
[ifp_no
- 1], frame_list
) = &s
->f
[ifp_no
];
447 AST_LIST_NEXT(&s
->f
[ifp_no
], frame_list
) = NULL
;
453 /* If packets are received out of sequence, we may have already processed this packet from the error
454 recovery information in a packet already received. */
455 if (seq_no
>= s
->rx_seq_no
) {
456 /* Decode the primary IFP packet */
457 s
->f
[ifp_no
].frametype
= AST_FRAME_MODEM
;
458 s
->f
[ifp_no
].subclass
= AST_MODEM_T38
;
460 s
->f
[ifp_no
].mallocd
= 0;
461 s
->f
[ifp_no
].seqno
= seq_no
;
462 s
->f
[ifp_no
].datalen
= ifp_len
;
463 s
->f
[ifp_no
].data
= (uint8_t *) ifp
;
464 s
->f
[ifp_no
].offset
= 0;
465 s
->f
[ifp_no
].src
= "UDPTL";
467 AST_LIST_NEXT(&s
->f
[ifp_no
- 1], frame_list
) = &s
->f
[ifp_no
];
468 AST_LIST_NEXT(&s
->f
[ifp_no
], frame_list
) = NULL
;
473 s
->rx_seq_no
= seq_no
+ 1;
476 /*- End of function --------------------------------------------------------*/
478 static int udptl_build_packet(struct ast_udptl
*s
, uint8_t *buf
, uint8_t *ifp
, int ifp_len
)
480 uint8_t fec
[LOCAL_FAX_MAX_DATAGRAM
];
492 seq
= s
->tx_seq_no
& 0xFFFF;
494 /* Map the sequence number to an entry in the circular buffer */
495 entry
= seq
& UDPTL_BUF_MASK
;
497 /* We save the message in a circular buffer, for generating FEC or
498 redundancy sets later on. */
499 s
->tx
[entry
].buf_len
= ifp_len
;
500 memcpy(s
->tx
[entry
].buf
, ifp
, ifp_len
);
502 /* Build the UDPTLPacket */
505 /* Encode the sequence number */
506 buf
[len
++] = (seq
>> 8) & 0xFF;
507 buf
[len
++] = seq
& 0xFF;
509 /* Encode the primary IFP packet */
510 if (encode_open_type(buf
, &len
, ifp
, ifp_len
) < 0)
513 /* Encode the appropriate type of error recovery information */
514 switch (s
->error_correction_scheme
)
516 case UDPTL_ERROR_CORRECTION_NONE
:
517 /* Encode the error recovery type */
519 /* The number of entries will always be zero, so it is pointless allowing
520 for the fragmented case here. */
521 if (encode_length(buf
, &len
, 0) < 0)
524 case UDPTL_ERROR_CORRECTION_REDUNDANCY
:
525 /* Encode the error recovery type */
527 if (s
->tx_seq_no
> s
->error_correction_entries
)
528 entries
= s
->error_correction_entries
;
530 entries
= s
->tx_seq_no
;
531 /* The number of entries will always be small, so it is pointless allowing
532 for the fragmented case here. */
533 if (encode_length(buf
, &len
, entries
) < 0)
535 /* Encode the elements */
536 for (i
= 0; i
< entries
; i
++) {
537 j
= (entry
- i
- 1) & UDPTL_BUF_MASK
;
538 if (encode_open_type(buf
, &len
, s
->tx
[j
].buf
, s
->tx
[j
].buf_len
) < 0)
542 case UDPTL_ERROR_CORRECTION_FEC
:
543 span
= s
->error_correction_span
;
544 entries
= s
->error_correction_entries
;
545 if (seq
< s
->error_correction_span
*s
->error_correction_entries
) {
546 /* In the initial stages, wind up the FEC smoothly */
547 entries
= seq
/s
->error_correction_span
;
548 if (seq
< s
->error_correction_span
)
551 /* Encode the error recovery type */
553 /* Span is defined as an inconstrained integer, which it dumb. It will only
554 ever be a small value. Treat it as such. */
557 /* The number of entries is defined as a length, but will only ever be a small
558 value. Treat it as such. */
559 buf
[len
++] = entries
;
560 for (m
= 0; m
< entries
; m
++) {
561 /* Make an XOR'ed entry the maximum length */
562 limit
= (entry
+ m
) & UDPTL_BUF_MASK
;
564 for (i
= (limit
- span
*entries
) & UDPTL_BUF_MASK
; i
!= limit
; i
= (i
+ entries
) & UDPTL_BUF_MASK
) {
565 if (high_tide
< s
->tx
[i
].buf_len
) {
566 for (j
= 0; j
< high_tide
; j
++)
567 fec
[j
] ^= s
->tx
[i
].buf
[j
];
568 for ( ; j
< s
->tx
[i
].buf_len
; j
++)
569 fec
[j
] = s
->tx
[i
].buf
[j
];
570 high_tide
= s
->tx
[i
].buf_len
;
572 for (j
= 0; j
< s
->tx
[i
].buf_len
; j
++)
573 fec
[j
] ^= s
->tx
[i
].buf
[j
];
576 if (encode_open_type(buf
, &len
, fec
, high_tide
) < 0)
583 fprintf(stderr
, "\n");
589 int ast_udptl_fd(struct ast_udptl
*udptl
)
594 void ast_udptl_set_data(struct ast_udptl
*udptl
, void *data
)
599 void ast_udptl_set_callback(struct ast_udptl
*udptl
, ast_udptl_callback callback
)
601 udptl
->callback
= callback
;
604 void ast_udptl_setnat(struct ast_udptl
*udptl
, int nat
)
609 static int udptlread(int *id
, int fd
, short events
, void *cbdata
)
611 struct ast_udptl
*udptl
= cbdata
;
614 if ((f
= ast_udptl_read(udptl
))) {
616 udptl
->callback(udptl
, f
, udptl
->data
);
621 struct ast_frame
*ast_udptl_read(struct ast_udptl
*udptl
)
624 struct sockaddr_in sin
;
627 uint16_t *udptlheader
;
631 /* Cache where the header will go */
632 res
= recvfrom(udptl
->fd
,
633 udptl
->rawdata
+ AST_FRIENDLY_OFFSET
,
634 sizeof(udptl
->rawdata
) - AST_FRIENDLY_OFFSET
,
636 (struct sockaddr
*) &sin
,
638 udptlheader
= (uint16_t *)(udptl
->rawdata
+ AST_FRIENDLY_OFFSET
);
641 ast_log(LOG_WARNING
, "UDPTL read error: %s\n", strerror(errno
));
644 return &ast_null_frame
;
647 /* Ignore if the other side hasn't been given an address yet. */
648 if (!udptl
->them
.sin_addr
.s_addr
|| !udptl
->them
.sin_port
)
649 return &ast_null_frame
;
652 /* Send to whoever sent to us */
653 if ((udptl
->them
.sin_addr
.s_addr
!= sin
.sin_addr
.s_addr
) ||
654 (udptl
->them
.sin_port
!= sin
.sin_port
)) {
655 memcpy(&udptl
->them
, &sin
, sizeof(udptl
->them
));
656 ast_log(LOG_DEBUG
, "UDPTL NAT: Using address %s:%d\n", ast_inet_ntoa(udptl
->them
.sin_addr
), ntohs(udptl
->them
.sin_port
));
660 if (udptl_debug_test_addr(&sin
)) {
661 ast_verbose("Got UDPTL packet from %s:%d (type %d, seq %d, len %d)\n",
662 ast_inet_ntoa(sin
.sin_addr
), ntohs(sin
.sin_port
), 0, seqno
, res
);
665 printf("Got UDPTL packet from %s:%d (seq %d, len = %d)\n", ast_inet_ntoa(sin
.sin_addr
), ntohs(sin
.sin_port
), seqno
, res
);
667 if (udptl_rx_packet(udptl
, udptl
->rawdata
+ AST_FRIENDLY_OFFSET
, res
) < 1)
668 return &ast_null_frame
;
673 void ast_udptl_offered_from_local(struct ast_udptl
* udptl
, int local
)
676 udptl
->udptl_offered_from_local
= local
;
678 ast_log(LOG_WARNING
, "udptl structure is null\n");
681 int ast_udptl_get_error_correction_scheme(struct ast_udptl
* udptl
)
684 return udptl
->error_correction_scheme
;
686 ast_log(LOG_WARNING
, "udptl structure is null\n");
691 void ast_udptl_set_error_correction_scheme(struct ast_udptl
* udptl
, int ec
)
695 case UDPTL_ERROR_CORRECTION_FEC
:
696 udptl
->error_correction_scheme
= UDPTL_ERROR_CORRECTION_FEC
;
698 case UDPTL_ERROR_CORRECTION_REDUNDANCY
:
699 udptl
->error_correction_scheme
= UDPTL_ERROR_CORRECTION_REDUNDANCY
;
701 case UDPTL_ERROR_CORRECTION_NONE
:
702 udptl
->error_correction_scheme
= UDPTL_ERROR_CORRECTION_NONE
;
705 ast_log(LOG_WARNING
, "error correction parameter invalid\n");
708 ast_log(LOG_WARNING
, "udptl structure is null\n");
711 int ast_udptl_get_local_max_datagram(struct ast_udptl
* udptl
)
714 return udptl
->local_max_datagram_size
;
716 ast_log(LOG_WARNING
, "udptl structure is null\n");
721 int ast_udptl_get_far_max_datagram(struct ast_udptl
* udptl
)
724 return udptl
->far_max_datagram_size
;
726 ast_log(LOG_WARNING
, "udptl structure is null\n");
731 void ast_udptl_set_local_max_datagram(struct ast_udptl
* udptl
, int max_datagram
)
734 udptl
->local_max_datagram_size
= max_datagram
;
736 ast_log(LOG_WARNING
, "udptl structure is null\n");
739 void ast_udptl_set_far_max_datagram(struct ast_udptl
* udptl
, int max_datagram
)
742 udptl
->far_max_datagram_size
= max_datagram
;
744 ast_log(LOG_WARNING
, "udptl structure is null\n");
747 struct ast_udptl
*ast_udptl_new_with_bindaddr(struct sched_context
*sched
, struct io_context
*io
, int callbackmode
, struct in_addr addr
)
749 struct ast_udptl
*udptl
;
755 if (!(udptl
= ast_calloc(1, sizeof(*udptl
))))
758 if (udptlfectype
== 2)
759 udptl
->error_correction_scheme
= UDPTL_ERROR_CORRECTION_FEC
;
760 else if (udptlfectype
== 1)
761 udptl
->error_correction_scheme
= UDPTL_ERROR_CORRECTION_REDUNDANCY
;
763 udptl
->error_correction_scheme
= UDPTL_ERROR_CORRECTION_NONE
;
764 udptl
->error_correction_span
= udptlfecspan
;
765 udptl
->error_correction_entries
= udptlfecentries
;
767 udptl
->far_max_datagram_size
= udptlmaxdatagram
;
768 udptl
->local_max_datagram_size
= udptlmaxdatagram
;
770 memset(&udptl
->rx
, 0, sizeof(udptl
->rx
));
771 memset(&udptl
->tx
, 0, sizeof(udptl
->tx
));
772 for (i
= 0; i
<= UDPTL_BUF_MASK
; i
++) {
773 udptl
->rx
[i
].buf_len
= -1;
774 udptl
->tx
[i
].buf_len
= -1;
777 udptl
->them
.sin_family
= AF_INET
;
778 udptl
->us
.sin_family
= AF_INET
;
780 if ((udptl
->fd
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0) {
782 ast_log(LOG_WARNING
, "Unable to allocate socket: %s\n", strerror(errno
));
785 flags
= fcntl(udptl
->fd
, F_GETFL
);
786 fcntl(udptl
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
789 setsockopt(udptl
->fd
, SOL_SOCKET
, SO_NO_CHECK
, &nochecksums
, sizeof(nochecksums
));
791 /* Find us a place */
792 x
= (ast_random() % (udptlend
- udptlstart
)) + udptlstart
;
795 udptl
->us
.sin_port
= htons(x
);
796 udptl
->us
.sin_addr
= addr
;
797 if (bind(udptl
->fd
, (struct sockaddr
*) &udptl
->us
, sizeof(udptl
->us
)) == 0)
799 if (errno
!= EADDRINUSE
) {
800 ast_log(LOG_WARNING
, "Unexpected bind error: %s\n", strerror(errno
));
807 if (x
== startplace
) {
808 ast_log(LOG_WARNING
, "No UDPTL ports remaining\n");
814 if (io
&& sched
&& callbackmode
) {
815 /* Operate this one in a callback mode */
816 udptl
->sched
= sched
;
818 udptl
->ioid
= ast_io_add(udptl
->io
, udptl
->fd
, udptlread
, AST_IO_IN
, udptl
);
823 struct ast_udptl
*ast_udptl_new(struct sched_context
*sched
, struct io_context
*io
, int callbackmode
)
826 memset(&ia
, 0, sizeof(ia
));
827 return ast_udptl_new_with_bindaddr(sched
, io
, callbackmode
, ia
);
830 int ast_udptl_settos(struct ast_udptl
*udptl
, int tos
)
834 if ((res
= setsockopt(udptl
->fd
, IPPROTO_IP
, IP_TOS
, &tos
, sizeof(tos
))))
835 ast_log(LOG_WARNING
, "UDPTL unable to set TOS to %d\n", tos
);
839 void ast_udptl_set_peer(struct ast_udptl
*udptl
, struct sockaddr_in
*them
)
841 udptl
->them
.sin_port
= them
->sin_port
;
842 udptl
->them
.sin_addr
= them
->sin_addr
;
845 void ast_udptl_get_peer(struct ast_udptl
*udptl
, struct sockaddr_in
*them
)
847 them
->sin_family
= AF_INET
;
848 them
->sin_port
= udptl
->them
.sin_port
;
849 them
->sin_addr
= udptl
->them
.sin_addr
;
852 void ast_udptl_get_us(struct ast_udptl
*udptl
, struct sockaddr_in
*us
)
854 memcpy(us
, &udptl
->us
, sizeof(udptl
->us
));
857 void ast_udptl_stop(struct ast_udptl
*udptl
)
859 memset(&udptl
->them
.sin_addr
, 0, sizeof(udptl
->them
.sin_addr
));
860 memset(&udptl
->them
.sin_port
, 0, sizeof(udptl
->them
.sin_port
));
863 void ast_udptl_destroy(struct ast_udptl
*udptl
)
866 ast_io_remove(udptl
->io
, udptl
->ioid
);
872 int ast_udptl_write(struct ast_udptl
*s
, struct ast_frame
*f
)
877 uint8_t buf
[LOCAL_FAX_MAX_DATAGRAM
];
879 /* If we have no peer, return immediately */
880 if (s
->them
.sin_addr
.s_addr
== INADDR_ANY
)
883 /* If there is no data length, return immediately */
887 if (f
->frametype
!= AST_FRAME_MODEM
) {
888 ast_log(LOG_WARNING
, "UDPTL can only send T.38 data\n");
892 /* Save seq_no for debug output because udptl_build_packet increments it */
893 seq
= s
->tx_seq_no
& 0xFFFF;
895 /* Cook up the UDPTL packet, with the relevant EC info. */
896 len
= udptl_build_packet(s
, buf
, f
->data
, f
->datalen
);
898 if (len
> 0 && s
->them
.sin_port
&& s
->them
.sin_addr
.s_addr
) {
899 if ((res
= sendto(s
->fd
, buf
, len
, 0, (struct sockaddr
*) &s
->them
, sizeof(s
->them
))) < 0)
900 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
));
902 printf("Sent %d bytes of UDPTL data to %s:%d\n", res
, ast_inet_ntoa(udptl
->them
.sin_addr
), ntohs(udptl
->them
.sin_port
));
904 if (udptl_debug_test_addr(&s
->them
))
905 ast_verbose("Sent UDPTL packet to %s:%d (type %d, seq %d, len %d)\n",
906 ast_inet_ntoa(s
->them
.sin_addr
),
907 ntohs(s
->them
.sin_port
), 0, seq
, len
);
913 void ast_udptl_proto_unregister(struct ast_udptl_protocol
*proto
)
915 struct ast_udptl_protocol
*cur
;
916 struct ast_udptl_protocol
*prev
;
923 prev
->next
= proto
->next
;
925 protos
= proto
->next
;
933 int ast_udptl_proto_register(struct ast_udptl_protocol
*proto
)
935 struct ast_udptl_protocol
*cur
;
939 if (cur
->type
== proto
->type
) {
940 ast_log(LOG_WARNING
, "Tried to register same protocol '%s' twice\n", cur
->type
);
945 proto
->next
= protos
;
950 static struct ast_udptl_protocol
*get_proto(struct ast_channel
*chan
)
952 struct ast_udptl_protocol
*cur
;
956 if (cur
->type
== chan
->tech
->type
)
963 int ast_udptl_bridge(struct ast_channel
*c0
, struct ast_channel
*c1
, int flags
, struct ast_frame
**fo
, struct ast_channel
**rc
)
966 struct ast_channel
*who
;
967 struct ast_channel
*cs
[3];
968 struct ast_udptl
*p0
;
969 struct ast_udptl
*p1
;
970 struct ast_udptl_protocol
*pr0
;
971 struct ast_udptl_protocol
*pr1
;
972 struct sockaddr_in ac0
;
973 struct sockaddr_in ac1
;
974 struct sockaddr_in t0
;
975 struct sockaddr_in t1
;
980 ast_channel_lock(c0
);
981 while (ast_channel_trylock(c1
)) {
982 ast_channel_unlock(c0
);
984 ast_channel_lock(c0
);
989 ast_log(LOG_WARNING
, "Can't find native functions for channel '%s'\n", c0
->name
);
990 ast_channel_unlock(c0
);
991 ast_channel_unlock(c1
);
995 ast_log(LOG_WARNING
, "Can't find native functions for channel '%s'\n", c1
->name
);
996 ast_channel_unlock(c0
);
997 ast_channel_unlock(c1
);
1000 pvt0
= c0
->tech_pvt
;
1001 pvt1
= c1
->tech_pvt
;
1002 p0
= pr0
->get_udptl_info(c0
);
1003 p1
= pr1
->get_udptl_info(c1
);
1005 /* Somebody doesn't want to play... */
1006 ast_channel_unlock(c0
);
1007 ast_channel_unlock(c1
);
1010 if (pr0
->set_udptl_peer(c0
, p1
)) {
1011 ast_log(LOG_WARNING
, "Channel '%s' failed to talk to '%s'\n", c0
->name
, c1
->name
);
1013 /* Store UDPTL peer */
1014 ast_udptl_get_peer(p1
, &ac1
);
1016 if (pr1
->set_udptl_peer(c1
, p0
))
1017 ast_log(LOG_WARNING
, "Channel '%s' failed to talk back to '%s'\n", c1
->name
, c0
->name
);
1019 /* Store UDPTL peer */
1020 ast_udptl_get_peer(p0
, &ac0
);
1022 ast_channel_unlock(c0
);
1023 ast_channel_unlock(c1
);
1028 if ((c0
->tech_pvt
!= pvt0
) ||
1029 (c1
->tech_pvt
!= pvt1
) ||
1030 (c0
->masq
|| c0
->masqr
|| c1
->masq
|| c1
->masqr
)) {
1031 ast_log(LOG_DEBUG
, "Oooh, something is weird, backing out\n");
1032 /* Tell it to try again later */
1036 ast_udptl_get_peer(p1
, &t1
);
1037 ast_udptl_get_peer(p0
, &t0
);
1038 if (inaddrcmp(&t1
, &ac1
)) {
1039 ast_log(LOG_DEBUG
, "Oooh, '%s' changed end address to %s:%d\n",
1040 c1
->name
, ast_inet_ntoa(t1
.sin_addr
), ntohs(t1
.sin_port
));
1041 ast_log(LOG_DEBUG
, "Oooh, '%s' was %s:%d\n",
1042 c1
->name
, ast_inet_ntoa(ac1
.sin_addr
), ntohs(ac1
.sin_port
));
1043 memcpy(&ac1
, &t1
, sizeof(ac1
));
1045 if (inaddrcmp(&t0
, &ac0
)) {
1046 ast_log(LOG_DEBUG
, "Oooh, '%s' changed end address to %s:%d\n",
1047 c0
->name
, ast_inet_ntoa(t0
.sin_addr
), ntohs(t0
.sin_port
));
1048 ast_log(LOG_DEBUG
, "Oooh, '%s' was %s:%d\n",
1049 c0
->name
, ast_inet_ntoa(ac0
.sin_addr
), ntohs(ac0
.sin_port
));
1050 memcpy(&ac0
, &t0
, sizeof(ac0
));
1052 who
= ast_waitfor_n(cs
, 2, &to
);
1054 ast_log(LOG_DEBUG
, "Ooh, empty read...\n");
1055 /* check for hangup / whentohangup */
1056 if (ast_check_hangup(c0
) || ast_check_hangup(c1
))
1064 ast_log(LOG_DEBUG
, "Oooh, got a %s\n", f
? "digit" : "hangup");
1065 /* That's all we needed */
1068 if (f
->frametype
== AST_FRAME_MODEM
) {
1069 /* Forward T.38 frames if they happen upon us */
1072 } else if (who
== c1
) {
1078 /* Swap priority. Not that it's a big deal at this point */
1086 static int udptl_do_debug_ip(int fd
, int argc
, char *argv
[])
1089 struct ast_hostent ahp
;
1096 return RESULT_SHOWUSAGE
;
1098 p
= strstr(arg
, ":");
1104 hp
= ast_gethostbyname(arg
, &ahp
);
1106 return RESULT_SHOWUSAGE
;
1107 udptldebugaddr
.sin_family
= AF_INET
;
1108 memcpy(&udptldebugaddr
.sin_addr
, hp
->h_addr
, sizeof(udptldebugaddr
.sin_addr
));
1109 udptldebugaddr
.sin_port
= htons(port
);
1111 ast_cli(fd
, "UDPTL Debugging Enabled for IP: %s\n", ast_inet_ntoa(udptldebugaddr
.sin_addr
));
1113 ast_cli(fd
, "UDPTL Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(udptldebugaddr
.sin_addr
), port
);
1115 return RESULT_SUCCESS
;
1118 static int udptl_do_debug(int fd
, int argc
, char *argv
[])
1122 return RESULT_SHOWUSAGE
;
1123 return udptl_do_debug_ip(fd
, argc
, argv
);
1126 memset(&udptldebugaddr
,0,sizeof(udptldebugaddr
));
1127 ast_cli(fd
, "UDPTL Debugging Enabled\n");
1128 return RESULT_SUCCESS
;
1131 static int udptl_nodebug(int fd
, int argc
, char *argv
[])
1134 return RESULT_SHOWUSAGE
;
1136 ast_cli(fd
,"UDPTL Debugging Disabled\n");
1137 return RESULT_SUCCESS
;
1140 static char debug_usage
[] =
1141 "Usage: udptl debug [ip host[:port]]\n"
1142 " Enable dumping of all UDPTL packets to and from host.\n";
1144 static char nodebug_usage
[] =
1145 "Usage: udptl debug off\n"
1146 " Disable all UDPTL debugging\n";
1148 static struct ast_cli_entry cli_udptl_no_debug
= {
1149 { "udptl", "no", "debug", NULL
},
1150 udptl_nodebug
, NULL
,
1153 static struct ast_cli_entry cli_udptl
[] = {
1154 { { "udptl", "debug", NULL
},
1155 udptl_do_debug
, "Enable UDPTL debugging",
1158 { { "udptl", "debug", "ip", NULL
},
1159 udptl_do_debug
, "Enable UDPTL debugging on IP",
1162 { { "udptl", "debug", "off", NULL
},
1163 udptl_nodebug
, "Disable UDPTL debugging",
1164 nodebug_usage
, NULL
, &cli_udptl_no_debug
},
1167 void ast_udptl_reload(void)
1169 struct ast_config
*cfg
;
1175 udptlfecentries
= 0;
1177 udptlmaxdatagram
= 0;
1179 if ((cfg
= ast_config_load("udptl.conf"))) {
1180 if ((s
= ast_variable_retrieve(cfg
, "general", "udptlstart"))) {
1181 udptlstart
= atoi(s
);
1182 if (udptlstart
< 1024)
1184 if (udptlstart
> 65535)
1187 if ((s
= ast_variable_retrieve(cfg
, "general", "udptlend"))) {
1189 if (udptlend
< 1024)
1191 if (udptlend
> 65535)
1194 if ((s
= ast_variable_retrieve(cfg
, "general", "udptlchecksums"))) {
1202 ast_log(LOG_WARNING
, "Disabling UDPTL checksums is not supported on this operating system!\n");
1205 if ((s
= ast_variable_retrieve(cfg
, "general", "T38FaxUdpEC"))) {
1206 if (strcmp(s
, "t38UDPFEC") == 0)
1208 else if (strcmp(s
, "t38UDPRedundancy") == 0)
1211 if ((s
= ast_variable_retrieve(cfg
, "general", "T38FaxMaxDatagram"))) {
1212 udptlmaxdatagram
= atoi(s
);
1213 if (udptlmaxdatagram
< 0)
1214 udptlmaxdatagram
= 0;
1215 if (udptlmaxdatagram
> LOCAL_FAX_MAX_DATAGRAM
)
1216 udptlmaxdatagram
= LOCAL_FAX_MAX_DATAGRAM
;
1218 if ((s
= ast_variable_retrieve(cfg
, "general", "UDPTLFECentries"))) {
1219 udptlfecentries
= atoi(s
);
1220 if (udptlfecentries
< 0)
1221 udptlfecentries
= 0;
1222 if (udptlfecentries
> MAX_FEC_ENTRIES
)
1223 udptlfecentries
= MAX_FEC_ENTRIES
;
1225 if ((s
= ast_variable_retrieve(cfg
, "general", "UDPTLFECspan"))) {
1226 udptlfecspan
= atoi(s
);
1227 if (udptlfecspan
< 0)
1229 if (udptlfecspan
> MAX_FEC_SPAN
)
1230 udptlfecspan
= MAX_FEC_SPAN
;
1232 ast_config_destroy(cfg
);
1234 if (udptlstart
>= udptlend
) {
1235 ast_log(LOG_WARNING
, "Unreasonable values for UDPTL start/end\n");
1239 if (option_verbose
> 1)
1240 ast_verbose(VERBOSE_PREFIX_2
"UDPTL allocating from port range %d -> %d\n", udptlstart
, udptlend
);
1243 void ast_udptl_init(void)
1245 ast_cli_register_multiple(cli_udptl
, sizeof(cli_udptl
) / sizeof(struct ast_cli_entry
));