00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "asterisk.h"
00019
00020 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 308721 $")
00021
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <string.h>
00025 #include <sys/time.h>
00026 #include <signal.h>
00027 #include <errno.h>
00028 #include <unistd.h>
00029 #include <netinet/in.h>
00030 #include <sys/time.h>
00031 #include <sys/socket.h>
00032 #include <arpa/inet.h>
00033 #include <fcntl.h>
00034
00035 #include "asterisk/udptl.h"
00036 #include "asterisk/frame.h"
00037 #include "asterisk/logger.h"
00038 #include "asterisk/options.h"
00039 #include "asterisk/channel.h"
00040 #include "asterisk/acl.h"
00041 #include "asterisk/channel.h"
00042 #include "asterisk/config.h"
00043 #include "asterisk/lock.h"
00044 #include "asterisk/utils.h"
00045 #include "asterisk/cli.h"
00046 #include "asterisk/unaligned.h"
00047 #include "asterisk/utils.h"
00048
00049 #define UDPTL_MTU 1200
00050
00051 #if !defined(FALSE)
00052 #define FALSE 0
00053 #endif
00054 #if !defined(TRUE)
00055 #define TRUE (!FALSE)
00056 #endif
00057
00058 static int udptlstart;
00059 static int udptlend;
00060 static int udptldebug;
00061 static struct sockaddr_in udptldebugaddr;
00062 #ifdef SO_NO_CHECK
00063 static int nochecksums;
00064 #endif
00065 static int udptlfectype;
00066 static int udptlfecentries;
00067 static int udptlfecspan;
00068 static int udptlmaxdatagram;
00069 static int use_even_ports;
00070
00071 #define LOCAL_FAX_MAX_DATAGRAM 1400
00072 #define MAX_FEC_ENTRIES 5
00073 #define MAX_FEC_SPAN 5
00074
00075 #define UDPTL_BUF_MASK 15
00076
00077 typedef struct {
00078 int buf_len;
00079 uint8_t buf[LOCAL_FAX_MAX_DATAGRAM];
00080 } udptl_fec_tx_buffer_t;
00081
00082 typedef struct {
00083 int buf_len;
00084 uint8_t buf[LOCAL_FAX_MAX_DATAGRAM];
00085 int fec_len[MAX_FEC_ENTRIES];
00086 uint8_t fec[MAX_FEC_ENTRIES][LOCAL_FAX_MAX_DATAGRAM];
00087 int fec_span;
00088 int fec_entries;
00089 } udptl_fec_rx_buffer_t;
00090
00091 struct ast_udptl {
00092 int fd;
00093 char resp;
00094 struct ast_frame f[16];
00095 unsigned char rawdata[8192 + AST_FRIENDLY_OFFSET];
00096 unsigned int lasteventseqn;
00097 int nat;
00098 int flags;
00099 struct sockaddr_in us;
00100 struct sockaddr_in them;
00101 int *ioid;
00102 struct sched_context *sched;
00103 struct io_context *io;
00104 void *data;
00105 ast_udptl_callback callback;
00106 int udptl_offered_from_local;
00107
00108
00109
00110 int error_correction_scheme;
00111
00112
00113
00114 int error_correction_entries;
00115
00116
00117
00118 int error_correction_span;
00119
00120
00121
00122 int far_max_datagram_size;
00123
00124
00125
00126 int local_max_datagram_size;
00127
00128 struct sockaddr_in far;
00129
00130 int tx_seq_no;
00131 int rx_seq_no;
00132 int rx_expected_seq_no;
00133
00134 udptl_fec_tx_buffer_t tx[UDPTL_BUF_MASK + 1];
00135 udptl_fec_rx_buffer_t rx[UDPTL_BUF_MASK + 1];
00136 };
00137
00138 static struct ast_udptl_protocol *protos;
00139
00140 static int udptl_rx_packet(struct ast_udptl *s, uint8_t *buf, int len);
00141 static int udptl_build_packet(struct ast_udptl *s, uint8_t *buf, int buflen, uint8_t *ifp, int ifp_len);
00142
00143 static inline int udptl_debug_test_addr(struct sockaddr_in *addr)
00144 {
00145 if (udptldebug == 0)
00146 return 0;
00147 if (udptldebugaddr.sin_addr.s_addr) {
00148 if (((ntohs(udptldebugaddr.sin_port) != 0)
00149 && (udptldebugaddr.sin_port != addr->sin_port))
00150 || (udptldebugaddr.sin_addr.s_addr != addr->sin_addr.s_addr))
00151 return 0;
00152 }
00153 return 1;
00154 }
00155
00156 static int decode_length(uint8_t *buf, int limit, int *len, int *pvalue)
00157 {
00158 if (*len >= limit)
00159 return -1;
00160 if ((buf[*len] & 0x80) == 0) {
00161 *pvalue = buf[*len];
00162 (*len)++;
00163 return 0;
00164 }
00165 if ((buf[*len] & 0x40) == 0) {
00166 if (*len == limit - 1)
00167 return -1;
00168 *pvalue = (buf[*len] & 0x3F) << 8;
00169 (*len)++;
00170 *pvalue |= buf[*len];
00171 (*len)++;
00172 return 0;
00173 }
00174 *pvalue = (buf[*len] & 0x3F) << 14;
00175 (*len)++;
00176
00177 if (option_debug) {
00178 ast_log(LOG_DEBUG, "UDPTL packet with length greater than 16K received, decoding will fail\n");
00179 }
00180 return 1;
00181 }
00182
00183
00184 static int decode_open_type(uint8_t *buf, int limit, int *len, const uint8_t **p_object, int *p_num_octets)
00185 {
00186 int octet_cnt = 0;
00187
00188 if (decode_length(buf, limit, len, &octet_cnt) != 0)
00189 return -1;
00190
00191 if (octet_cnt > 0) {
00192
00193 if ((*len + octet_cnt) > limit)
00194 return -1;
00195
00196 *p_num_octets = octet_cnt;
00197 *p_object = &buf[*len];
00198 *len += octet_cnt;
00199 }
00200
00201 return 0;
00202 }
00203
00204
00205 static int encode_length(uint8_t *buf, int *len, int value)
00206 {
00207 int multiplier;
00208
00209 if (value < 0x80) {
00210
00211 buf[*len] = value;
00212 (*len)++;
00213 return value;
00214 }
00215 if (value < 0x4000) {
00216
00217
00218 buf[*len] = ((0x8000 | value) >> 8) & 0xFF;
00219 (*len)++;
00220 buf[*len] = value & 0xFF;
00221 (*len)++;
00222 return value;
00223 }
00224
00225 multiplier = (value < 0x10000) ? (value >> 14) : 4;
00226
00227 buf[*len] = 0xC0 | multiplier;
00228 (*len)++;
00229 return multiplier << 14;
00230 }
00231
00232
00233 static int encode_open_type(uint8_t *buf, int buflen, int *len, const uint8_t *data, int num_octets)
00234 {
00235 int enclen;
00236 int octet_idx;
00237 uint8_t zero_byte;
00238
00239
00240 if (num_octets == 0) {
00241 zero_byte = 0;
00242 data = &zero_byte;
00243 num_octets = 1;
00244 }
00245
00246 for (octet_idx = 0; ; num_octets -= enclen, octet_idx += enclen) {
00247 if ((enclen = encode_length(buf, len, num_octets)) < 0)
00248 return -1;
00249 if (enclen + *len > buflen) {
00250 ast_log(LOG_ERROR, "Buffer overflow detected (%d + %d > %d)\n", enclen, *len, buflen);
00251 return -1;
00252 }
00253 if (enclen > 0) {
00254 memcpy(&buf[*len], &data[octet_idx], enclen);
00255 *len += enclen;
00256 }
00257 if (enclen >= num_octets)
00258 break;
00259 }
00260
00261 return 0;
00262 }
00263
00264
00265 static int udptl_rx_packet(struct ast_udptl *s, uint8_t *buf, int len)
00266 {
00267 int stat;
00268 int stat2;
00269 int i;
00270 int j;
00271 int k;
00272 int l;
00273 int m;
00274 int x;
00275 int limit;
00276 int which;
00277 int ptr;
00278 int count;
00279 int total_count;
00280 int seq_no;
00281 const uint8_t *ifp = NULL;
00282 const uint8_t *data = NULL;
00283 int ifp_len = 0;
00284 int repaired[16];
00285 const uint8_t *bufs[ARRAY_LEN(s->f) - 1];
00286 int lengths[ARRAY_LEN(s->f) - 1];
00287 int span;
00288 int entries;
00289 int ifp_no;
00290
00291 ptr = 0;
00292 ifp_no = 0;
00293 memset(&s->f[0], 0, sizeof(s->f[0]));
00294
00295
00296 if (ptr + 2 > len)
00297 return -1;
00298 seq_no = (buf[0] << 8) | buf[1];
00299 ptr += 2;
00300
00301
00302 if ((stat = decode_open_type(buf, len, &ptr, &ifp, &ifp_len)) != 0)
00303 return -1;
00304
00305 if (ptr + 1 > len)
00306 return -1;
00307 if ((buf[ptr++] & 0x80) == 0) {
00308
00309 if (seq_no > s->rx_seq_no) {
00310
00311
00312 total_count = 0;
00313 do {
00314 if ((stat2 = decode_length(buf, len, &ptr, &count)) < 0)
00315 return -1;
00316 for (i = 0; i < count && total_count + i < ARRAY_LEN(bufs); i++) {
00317 if ((stat = decode_open_type(buf, len, &ptr, &bufs[total_count + i], &lengths[total_count + i])) != 0)
00318 return -1;
00319 }
00320 total_count += i;
00321 }
00322 while (stat2 > 0 && total_count < ARRAY_LEN(bufs));
00323
00324 for (i = total_count; i > 0; i--) {
00325 if (seq_no - i >= s->rx_seq_no) {
00326
00327
00328
00329 s->f[ifp_no].frametype = AST_FRAME_MODEM;
00330 s->f[ifp_no].subclass = AST_MODEM_T38;
00331
00332 s->f[ifp_no].mallocd = 0;
00333 s->f[ifp_no].seqno = seq_no - i;
00334 s->f[ifp_no].datalen = lengths[i - 1];
00335 s->f[ifp_no].data = (uint8_t *) bufs[i - 1];
00336 s->f[ifp_no].offset = 0;
00337 s->f[ifp_no].src = "UDPTL";
00338 if (ifp_no > 0)
00339 AST_LIST_NEXT(&s->f[ifp_no - 1], frame_list) = &s->f[ifp_no];
00340 AST_LIST_NEXT(&s->f[ifp_no], frame_list) = NULL;
00341 ifp_no++;
00342 }
00343 }
00344 }
00345 }
00346 else
00347 {
00348
00349
00350 if (ifp_len > LOCAL_FAX_MAX_DATAGRAM)
00351 return -1;
00352
00353 for ( ; seq_no > s->rx_seq_no; s->rx_seq_no++) {
00354 x = s->rx_seq_no & UDPTL_BUF_MASK;
00355 s->rx[x].buf_len = -1;
00356 s->rx[x].fec_len[0] = 0;
00357 s->rx[x].fec_span = 0;
00358 s->rx[x].fec_entries = 0;
00359 }
00360
00361 x = seq_no & UDPTL_BUF_MASK;
00362
00363 memset(repaired, 0, sizeof(repaired));
00364
00365
00366 memcpy(s->rx[x].buf, ifp, ifp_len);
00367 s->rx[x].buf_len = ifp_len;
00368 repaired[x] = TRUE;
00369
00370
00371
00372
00373 if (ptr + 2 > len)
00374 return -1;
00375 if (buf[ptr++] != 1)
00376 return -1;
00377 span = buf[ptr++];
00378 s->rx[x].fec_span = span;
00379
00380
00381
00382 if (ptr + 1 > len)
00383 return -1;
00384 entries = buf[ptr++];
00385 if (entries > MAX_FEC_ENTRIES) {
00386 return -1;
00387 }
00388 s->rx[x].fec_entries = entries;
00389
00390
00391 for (i = 0; i < entries; i++) {
00392 if ((stat = decode_open_type(buf, len, &ptr, &data, &s->rx[x].fec_len[i])) != 0)
00393 return -1;
00394 if (s->rx[x].fec_len[i] > LOCAL_FAX_MAX_DATAGRAM)
00395 return -1;
00396
00397
00398 memcpy(s->rx[x].fec[i], data, s->rx[x].fec_len[i]);
00399 #if 0
00400 fprintf(stderr, "FEC: ");
00401 for (j = 0; j < s->rx[x].fec_len[i]; j++)
00402 fprintf(stderr, "%02X ", data[j]);
00403 fprintf(stderr, "\n");
00404 #endif
00405 }
00406
00407
00408
00409 for (l = x; l != ((x - (16 - span*entries)) & UDPTL_BUF_MASK); l = (l - 1) & UDPTL_BUF_MASK) {
00410 if (s->rx[l].fec_len[0] <= 0)
00411 continue;
00412 for (m = 0; m < s->rx[l].fec_entries; m++) {
00413 limit = (l + m) & UDPTL_BUF_MASK;
00414 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) {
00415 if (s->rx[k].buf_len <= 0)
00416 which = (which == -1) ? k : -2;
00417 }
00418 if (which >= 0) {
00419
00420 for (j = 0; j < s->rx[l].fec_len[m]; j++) {
00421 s->rx[which].buf[j] = s->rx[l].fec[m][j];
00422 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)
00423 s->rx[which].buf[j] ^= (s->rx[k].buf_len > j) ? s->rx[k].buf[j] : 0;
00424 }
00425 s->rx[which].buf_len = s->rx[l].fec_len[m];
00426 repaired[which] = TRUE;
00427 }
00428 }
00429 }
00430
00431 for (l = (x + 1) & UDPTL_BUF_MASK, j = seq_no - UDPTL_BUF_MASK; l != x; l = (l + 1) & UDPTL_BUF_MASK, j++) {
00432 if (repaired[l]) {
00433
00434 s->f[ifp_no].frametype = AST_FRAME_MODEM;
00435 s->f[ifp_no].subclass = AST_MODEM_T38;
00436
00437 s->f[ifp_no].mallocd = 0;
00438 s->f[ifp_no].seqno = j;
00439 s->f[ifp_no].datalen = s->rx[l].buf_len;
00440 s->f[ifp_no].data = s->rx[l].buf;
00441 s->f[ifp_no].offset = 0;
00442 s->f[ifp_no].src = "UDPTL";
00443 if (ifp_no > 0)
00444 AST_LIST_NEXT(&s->f[ifp_no - 1], frame_list) = &s->f[ifp_no];
00445 AST_LIST_NEXT(&s->f[ifp_no], frame_list) = NULL;
00446 ifp_no++;
00447 }
00448 }
00449 }
00450
00451
00452
00453 if (seq_no >= s->rx_seq_no) {
00454
00455 s->f[ifp_no].frametype = AST_FRAME_MODEM;
00456 s->f[ifp_no].subclass = AST_MODEM_T38;
00457
00458 s->f[ifp_no].mallocd = 0;
00459 s->f[ifp_no].seqno = seq_no;
00460 s->f[ifp_no].datalen = ifp_len;
00461 s->f[ifp_no].data = (uint8_t *) ifp;
00462 s->f[ifp_no].offset = 0;
00463 s->f[ifp_no].src = "UDPTL";
00464 if (ifp_no > 0)
00465 AST_LIST_NEXT(&s->f[ifp_no - 1], frame_list) = &s->f[ifp_no];
00466 AST_LIST_NEXT(&s->f[ifp_no], frame_list) = NULL;
00467
00468 ifp_no++;
00469 }
00470
00471 s->rx_seq_no = seq_no + 1;
00472 return ifp_no;
00473 }
00474
00475
00476 static int udptl_build_packet(struct ast_udptl *s, uint8_t *buf, int buflen, uint8_t *ifp, int ifp_len)
00477 {
00478 uint8_t fec[LOCAL_FAX_MAX_DATAGRAM * 2];
00479 int i;
00480 int j;
00481 int seq;
00482 int entry;
00483 int entries;
00484 int span;
00485 int m;
00486 int len;
00487 int limit;
00488 int high_tide;
00489
00490 seq = s->tx_seq_no & 0xFFFF;
00491
00492
00493 entry = seq & UDPTL_BUF_MASK;
00494
00495
00496
00497 s->tx[entry].buf_len = ifp_len;
00498 memcpy(s->tx[entry].buf, ifp, ifp_len);
00499
00500
00501
00502 len = 0;
00503
00504 buf[len++] = (seq >> 8) & 0xFF;
00505 buf[len++] = seq & 0xFF;
00506
00507
00508 if (encode_open_type(buf, buflen, &len, ifp, ifp_len) < 0)
00509 return -1;
00510
00511
00512 switch (s->error_correction_scheme)
00513 {
00514 case UDPTL_ERROR_CORRECTION_NONE:
00515
00516 buf[len++] = 0x00;
00517
00518
00519 if (encode_length(buf, &len, 0) < 0)
00520 return -1;
00521 break;
00522 case UDPTL_ERROR_CORRECTION_REDUNDANCY:
00523
00524 buf[len++] = 0x00;
00525 if (s->tx_seq_no > s->error_correction_entries)
00526 entries = s->error_correction_entries;
00527 else
00528 entries = s->tx_seq_no;
00529
00530
00531 if (encode_length(buf, &len, entries) < 0)
00532 return -1;
00533
00534 for (i = 0; i < entries; i++) {
00535 j = (entry - i - 1) & UDPTL_BUF_MASK;
00536 if (encode_open_type(buf, buflen, &len, s->tx[j].buf, s->tx[j].buf_len) < 0) {
00537 if (option_debug) {
00538 ast_log(LOG_DEBUG, "Encoding failed at i=%d, j=%d\n", i, j);
00539 }
00540 return -1;
00541 }
00542 }
00543 break;
00544 case UDPTL_ERROR_CORRECTION_FEC:
00545 span = s->error_correction_span;
00546 entries = s->error_correction_entries;
00547 if (seq < s->error_correction_span*s->error_correction_entries) {
00548
00549 entries = seq/s->error_correction_span;
00550 if (seq < s->error_correction_span)
00551 span = 0;
00552 }
00553
00554 buf[len++] = 0x80;
00555
00556
00557 buf[len++] = 1;
00558 buf[len++] = span;
00559
00560
00561 buf[len++] = entries;
00562 for (m = 0; m < entries; m++) {
00563
00564 limit = (entry + m) & UDPTL_BUF_MASK;
00565 high_tide = 0;
00566 for (i = (limit - span*entries) & UDPTL_BUF_MASK; i != limit; i = (i + entries) & UDPTL_BUF_MASK) {
00567 if (high_tide < s->tx[i].buf_len) {
00568 for (j = 0; j < high_tide; j++)
00569 fec[j] ^= s->tx[i].buf[j];
00570 for ( ; j < s->tx[i].buf_len; j++)
00571 fec[j] = s->tx[i].buf[j];
00572 high_tide = s->tx[i].buf_len;
00573 } else {
00574 for (j = 0; j < s->tx[i].buf_len; j++)
00575 fec[j] ^= s->tx[i].buf[j];
00576 }
00577 }
00578 if (encode_open_type(buf, buflen, &len, fec, high_tide) < 0)
00579 return -1;
00580 }
00581 break;
00582 }
00583
00584 s->tx_seq_no++;
00585 return len;
00586 }
00587
00588 int ast_udptl_fd(struct ast_udptl *udptl)
00589 {
00590 return udptl->fd;
00591 }
00592
00593 void ast_udptl_set_data(struct ast_udptl *udptl, void *data)
00594 {
00595 udptl->data = data;
00596 }
00597
00598 void ast_udptl_set_callback(struct ast_udptl *udptl, ast_udptl_callback callback)
00599 {
00600 udptl->callback = callback;
00601 }
00602
00603 void ast_udptl_setnat(struct ast_udptl *udptl, int nat)
00604 {
00605 udptl->nat = nat;
00606 }
00607
00608 static int udptlread(int *id, int fd, short events, void *cbdata)
00609 {
00610 struct ast_udptl *udptl = cbdata;
00611 struct ast_frame *f;
00612
00613 if ((f = ast_udptl_read(udptl))) {
00614 if (udptl->callback)
00615 udptl->callback(udptl, f, udptl->data);
00616 }
00617 return 1;
00618 }
00619
00620 struct ast_frame *ast_udptl_read(struct ast_udptl *udptl)
00621 {
00622 int res;
00623 struct sockaddr_in sin;
00624 socklen_t len;
00625 uint16_t seqno = 0;
00626 uint16_t *udptlheader;
00627
00628 len = sizeof(sin);
00629
00630
00631 res = recvfrom(udptl->fd,
00632 udptl->rawdata + AST_FRIENDLY_OFFSET,
00633 sizeof(udptl->rawdata) - AST_FRIENDLY_OFFSET,
00634 0,
00635 (struct sockaddr *) &sin,
00636 &len);
00637 udptlheader = (uint16_t *)(udptl->rawdata + AST_FRIENDLY_OFFSET);
00638 if (res < 0) {
00639 if (errno != EAGAIN)
00640 ast_log(LOG_WARNING, "UDPTL read error: %s\n", strerror(errno));
00641 ast_assert(errno != EBADF);
00642 return &ast_null_frame;
00643 }
00644
00645
00646 if (!udptl->them.sin_addr.s_addr || !udptl->them.sin_port)
00647 return &ast_null_frame;
00648
00649 if (udptl->nat) {
00650
00651 if ((udptl->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
00652 (udptl->them.sin_port != sin.sin_port)) {
00653 memcpy(&udptl->them, &sin, sizeof(udptl->them));
00654 ast_log(LOG_DEBUG, "UDPTL NAT: Using address %s:%d\n", ast_inet_ntoa(udptl->them.sin_addr), ntohs(udptl->them.sin_port));
00655 }
00656 }
00657
00658 if (udptl_debug_test_addr(&sin)) {
00659 ast_verbose("Got UDPTL packet from %s:%d (type %d, seq %d, len %d)\n",
00660 ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), 0, seqno, res);
00661 }
00662 #if 0
00663 printf("Got UDPTL packet from %s:%d (seq %d, len = %d)\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), seqno, res);
00664 #endif
00665 if (udptl_rx_packet(udptl, udptl->rawdata + AST_FRIENDLY_OFFSET, res) < 1)
00666 return &ast_null_frame;
00667
00668 return &udptl->f[0];
00669 }
00670
00671 void ast_udptl_offered_from_local(struct ast_udptl* udptl, int local)
00672 {
00673 if (udptl)
00674 udptl->udptl_offered_from_local = local;
00675 else
00676 ast_log(LOG_WARNING, "udptl structure is null\n");
00677 }
00678
00679 int ast_udptl_get_error_correction_scheme(struct ast_udptl* udptl)
00680 {
00681 if (udptl)
00682 return udptl->error_correction_scheme;
00683 else {
00684 ast_log(LOG_WARNING, "udptl structure is null\n");
00685 return -1;
00686 }
00687 }
00688
00689 void ast_udptl_set_error_correction_scheme(struct ast_udptl* udptl, int ec)
00690 {
00691 if (udptl) {
00692 switch (ec) {
00693 case UDPTL_ERROR_CORRECTION_FEC:
00694 udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_FEC;
00695 break;
00696 case UDPTL_ERROR_CORRECTION_REDUNDANCY:
00697 udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_REDUNDANCY;
00698 break;
00699 case UDPTL_ERROR_CORRECTION_NONE:
00700 udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_NONE;
00701 break;
00702 default:
00703 ast_log(LOG_WARNING, "error correction parameter invalid\n");
00704 };
00705 } else
00706 ast_log(LOG_WARNING, "udptl structure is null\n");
00707 }
00708
00709 int ast_udptl_get_local_max_datagram(struct ast_udptl* udptl)
00710 {
00711 if (udptl)
00712 return udptl->local_max_datagram_size;
00713 else {
00714 ast_log(LOG_WARNING, "udptl structure is null\n");
00715 return -1;
00716 }
00717 }
00718
00719 int ast_udptl_get_far_max_datagram(struct ast_udptl* udptl)
00720 {
00721 if (udptl)
00722 return udptl->far_max_datagram_size;
00723 else {
00724 ast_log(LOG_WARNING, "udptl structure is null\n");
00725 return -1;
00726 }
00727 }
00728
00729 void ast_udptl_set_local_max_datagram(struct ast_udptl* udptl, int max_datagram)
00730 {
00731 if (udptl)
00732 udptl->local_max_datagram_size = max_datagram;
00733 else
00734 ast_log(LOG_WARNING, "udptl structure is null\n");
00735 }
00736
00737 void ast_udptl_set_far_max_datagram(struct ast_udptl* udptl, int max_datagram)
00738 {
00739 if (udptl)
00740 udptl->far_max_datagram_size = max_datagram;
00741 else
00742 ast_log(LOG_WARNING, "udptl structure is null\n");
00743 }
00744
00745 struct ast_udptl *ast_udptl_new_with_bindaddr(struct sched_context *sched, struct io_context *io, int callbackmode, struct in_addr addr)
00746 {
00747 struct ast_udptl *udptl;
00748 int x;
00749 int startplace;
00750 int i;
00751 long int flags;
00752
00753 if (!(udptl = ast_calloc(1, sizeof(*udptl))))
00754 return NULL;
00755
00756 if (udptlfectype == 2)
00757 udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_FEC;
00758 else if (udptlfectype == 1)
00759 udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_REDUNDANCY;
00760 else
00761 udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_NONE;
00762 udptl->error_correction_span = udptlfecspan;
00763 udptl->error_correction_entries = udptlfecentries;
00764
00765 udptl->far_max_datagram_size = udptlmaxdatagram;
00766 udptl->local_max_datagram_size = udptlmaxdatagram;
00767
00768 memset(&udptl->rx, 0, sizeof(udptl->rx));
00769 memset(&udptl->tx, 0, sizeof(udptl->tx));
00770 for (i = 0; i <= UDPTL_BUF_MASK; i++) {
00771 udptl->rx[i].buf_len = -1;
00772 udptl->tx[i].buf_len = -1;
00773 }
00774
00775 udptl->them.sin_family = AF_INET;
00776 udptl->us.sin_family = AF_INET;
00777
00778 if ((udptl->fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
00779 free(udptl);
00780 ast_log(LOG_WARNING, "Unable to allocate socket: %s\n", strerror(errno));
00781 return NULL;
00782 }
00783 flags = fcntl(udptl->fd, F_GETFL);
00784 fcntl(udptl->fd, F_SETFL, flags | O_NONBLOCK);
00785 #ifdef SO_NO_CHECK
00786 if (nochecksums)
00787 setsockopt(udptl->fd, SOL_SOCKET, SO_NO_CHECK, &nochecksums, sizeof(nochecksums));
00788 #endif
00789
00790 x = (ast_random() % (udptlend - udptlstart)) + udptlstart;
00791 if (use_even_ports && (x & 1)) {
00792 ++x;
00793 }
00794 startplace = x;
00795 for (;;) {
00796 udptl->us.sin_port = htons(x);
00797 udptl->us.sin_addr = addr;
00798 if (bind(udptl->fd, (struct sockaddr *) &udptl->us, sizeof(udptl->us)) == 0)
00799 break;
00800 if (errno != EADDRINUSE) {
00801 ast_log(LOG_WARNING, "Unexpected bind error: %s\n", strerror(errno));
00802 close(udptl->fd);
00803 free(udptl);
00804 return NULL;
00805 }
00806 if (use_even_ports) {
00807 x += 2;
00808 } else {
00809 ++x;
00810 }
00811 if (x > udptlend)
00812 x = udptlstart;
00813 if (x == startplace) {
00814 ast_log(LOG_WARNING, "No UDPTL ports remaining\n");
00815 close(udptl->fd);
00816 free(udptl);
00817 return NULL;
00818 }
00819 }
00820 if (io && sched && callbackmode) {
00821
00822 udptl->sched = sched;
00823 udptl->io = io;
00824 udptl->ioid = ast_io_add(udptl->io, udptl->fd, udptlread, AST_IO_IN, udptl);
00825 }
00826 return udptl;
00827 }
00828
00829 struct ast_udptl *ast_udptl_new(struct sched_context *sched, struct io_context *io, int callbackmode)
00830 {
00831 struct in_addr ia;
00832 memset(&ia, 0, sizeof(ia));
00833 return ast_udptl_new_with_bindaddr(sched, io, callbackmode, ia);
00834 }
00835
00836 int ast_udptl_settos(struct ast_udptl *udptl, int tos)
00837 {
00838 int res;
00839
00840 if ((res = setsockopt(udptl->fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos))))
00841 ast_log(LOG_WARNING, "UDPTL unable to set TOS to %d\n", tos);
00842 return res;
00843 }
00844
00845 void ast_udptl_set_peer(struct ast_udptl *udptl, struct sockaddr_in *them)
00846 {
00847 udptl->them.sin_port = them->sin_port;
00848 udptl->them.sin_addr = them->sin_addr;
00849 }
00850
00851 void ast_udptl_get_peer(struct ast_udptl *udptl, struct sockaddr_in *them)
00852 {
00853 memset(them, 0, sizeof(*them));
00854 them->sin_family = AF_INET;
00855 them->sin_port = udptl->them.sin_port;
00856 them->sin_addr = udptl->them.sin_addr;
00857 }
00858
00859 void ast_udptl_get_us(struct ast_udptl *udptl, struct sockaddr_in *us)
00860 {
00861 memcpy(us, &udptl->us, sizeof(udptl->us));
00862 }
00863
00864 void ast_udptl_stop(struct ast_udptl *udptl)
00865 {
00866 memset(&udptl->them.sin_addr, 0, sizeof(udptl->them.sin_addr));
00867 memset(&udptl->them.sin_port, 0, sizeof(udptl->them.sin_port));
00868 }
00869
00870 void ast_udptl_destroy(struct ast_udptl *udptl)
00871 {
00872 if (udptl->ioid)
00873 ast_io_remove(udptl->io, udptl->ioid);
00874 if (udptl->fd > -1)
00875 close(udptl->fd);
00876 free(udptl);
00877 }
00878
00879 int ast_udptl_write(struct ast_udptl *s, struct ast_frame *f)
00880 {
00881 int seq;
00882 int len;
00883 int res;
00884 uint8_t buf[LOCAL_FAX_MAX_DATAGRAM * 2];
00885
00886
00887 if (s->them.sin_addr.s_addr == INADDR_ANY)
00888 return 0;
00889
00890
00891 if (f->datalen == 0)
00892 return 0;
00893
00894 if (f->frametype != AST_FRAME_MODEM) {
00895 ast_log(LOG_WARNING, "UDPTL can only send T.38 data\n");
00896 return -1;
00897 }
00898
00899
00900 seq = s->tx_seq_no & 0xFFFF;
00901
00902
00903 len = udptl_build_packet(s, buf, sizeof(buf), f->data, f->datalen);
00904
00905 if (len > 0 && s->them.sin_port && s->them.sin_addr.s_addr) {
00906 if ((res = sendto(s->fd, buf, len, 0, (struct sockaddr *) &s->them, sizeof(s->them))) < 0)
00907 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));
00908 #if 0
00909 printf("Sent %d bytes of UDPTL data to %s:%d\n", res, ast_inet_ntoa(udptl->them.sin_addr), ntohs(udptl->them.sin_port));
00910 #endif
00911 if (udptl_debug_test_addr(&s->them))
00912 ast_verbose("Sent UDPTL packet to %s:%d (type %d, seq %d, len %d)\n",
00913 ast_inet_ntoa(s->them.sin_addr),
00914 ntohs(s->them.sin_port), 0, seq, len);
00915 }
00916
00917 return 0;
00918 }
00919
00920 void ast_udptl_proto_unregister(struct ast_udptl_protocol *proto)
00921 {
00922 struct ast_udptl_protocol *cur;
00923 struct ast_udptl_protocol *prev;
00924
00925 cur = protos;
00926 prev = NULL;
00927 while (cur) {
00928 if (cur == proto) {
00929 if (prev)
00930 prev->next = proto->next;
00931 else
00932 protos = proto->next;
00933 return;
00934 }
00935 prev = cur;
00936 cur = cur->next;
00937 }
00938 }
00939
00940 int ast_udptl_proto_register(struct ast_udptl_protocol *proto)
00941 {
00942 struct ast_udptl_protocol *cur;
00943
00944 cur = protos;
00945 while (cur) {
00946 if (cur->type == proto->type) {
00947 ast_log(LOG_WARNING, "Tried to register same protocol '%s' twice\n", cur->type);
00948 return -1;
00949 }
00950 cur = cur->next;
00951 }
00952 proto->next = protos;
00953 protos = proto;
00954 return 0;
00955 }
00956
00957 static struct ast_udptl_protocol *get_proto(struct ast_channel *chan)
00958 {
00959 struct ast_udptl_protocol *cur;
00960
00961 cur = protos;
00962 while (cur) {
00963 if (cur->type == chan->tech->type)
00964 return cur;
00965 cur = cur->next;
00966 }
00967 return NULL;
00968 }
00969
00970 int ast_udptl_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
00971 {
00972 struct ast_frame *f;
00973 struct ast_channel *who;
00974 struct ast_channel *cs[3];
00975 struct ast_udptl *p0;
00976 struct ast_udptl *p1;
00977 struct ast_udptl_protocol *pr0;
00978 struct ast_udptl_protocol *pr1;
00979 struct sockaddr_in ac0;
00980 struct sockaddr_in ac1;
00981 struct sockaddr_in t0;
00982 struct sockaddr_in t1;
00983 void *pvt0;
00984 void *pvt1;
00985 int to;
00986
00987 ast_channel_lock(c0);
00988 while (ast_channel_trylock(c1)) {
00989 ast_channel_unlock(c0);
00990 usleep(1);
00991 ast_channel_lock(c0);
00992 }
00993 pr0 = get_proto(c0);
00994 pr1 = get_proto(c1);
00995 if (!pr0) {
00996 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c0->name);
00997 ast_channel_unlock(c0);
00998 ast_channel_unlock(c1);
00999 return -1;
01000 }
01001 if (!pr1) {
01002 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c1->name);
01003 ast_channel_unlock(c0);
01004 ast_channel_unlock(c1);
01005 return -1;
01006 }
01007 pvt0 = c0->tech_pvt;
01008 pvt1 = c1->tech_pvt;
01009 p0 = pr0->get_udptl_info(c0);
01010 p1 = pr1->get_udptl_info(c1);
01011 if (!p0 || !p1) {
01012
01013 ast_channel_unlock(c0);
01014 ast_channel_unlock(c1);
01015 return -2;
01016 }
01017 if (pr0->set_udptl_peer(c0, p1)) {
01018 ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c0->name, c1->name);
01019 memset(&ac1, 0, sizeof(ac1));
01020 } else {
01021
01022 ast_udptl_get_peer(p1, &ac1);
01023 }
01024 if (pr1->set_udptl_peer(c1, p0)) {
01025 ast_log(LOG_WARNING, "Channel '%s' failed to talk back to '%s'\n", c1->name, c0->name);
01026 memset(&ac0, 0, sizeof(ac0));
01027 } else {
01028
01029 ast_udptl_get_peer(p0, &ac0);
01030 }
01031 ast_channel_unlock(c0);
01032 ast_channel_unlock(c1);
01033 cs[0] = c0;
01034 cs[1] = c1;
01035 cs[2] = NULL;
01036 for (;;) {
01037 if ((c0->tech_pvt != pvt0) ||
01038 (c1->tech_pvt != pvt1) ||
01039 (c0->masq || c0->masqr || c1->masq || c1->masqr)) {
01040 ast_log(LOG_DEBUG, "Oooh, something is weird, backing out\n");
01041
01042 return -3;
01043 }
01044 to = -1;
01045 ast_udptl_get_peer(p1, &t1);
01046 ast_udptl_get_peer(p0, &t0);
01047 if (inaddrcmp(&t1, &ac1)) {
01048 ast_log(LOG_DEBUG, "Oooh, '%s' changed end address to %s:%d\n",
01049 c1->name, ast_inet_ntoa(t1.sin_addr), ntohs(t1.sin_port));
01050 ast_log(LOG_DEBUG, "Oooh, '%s' was %s:%d\n",
01051 c1->name, ast_inet_ntoa(ac1.sin_addr), ntohs(ac1.sin_port));
01052 memcpy(&ac1, &t1, sizeof(ac1));
01053 }
01054 if (inaddrcmp(&t0, &ac0)) {
01055 ast_log(LOG_DEBUG, "Oooh, '%s' changed end address to %s:%d\n",
01056 c0->name, ast_inet_ntoa(t0.sin_addr), ntohs(t0.sin_port));
01057 ast_log(LOG_DEBUG, "Oooh, '%s' was %s:%d\n",
01058 c0->name, ast_inet_ntoa(ac0.sin_addr), ntohs(ac0.sin_port));
01059 memcpy(&ac0, &t0, sizeof(ac0));
01060 }
01061 who = ast_waitfor_n(cs, 2, &to);
01062 if (!who) {
01063 ast_log(LOG_DEBUG, "Ooh, empty read...\n");
01064
01065 if (ast_check_hangup(c0) || ast_check_hangup(c1))
01066 break;
01067 continue;
01068 }
01069 f = ast_read(who);
01070 if (!f) {
01071 *fo = f;
01072 *rc = who;
01073 ast_log(LOG_DEBUG, "Oooh, got a %s\n", f ? "digit" : "hangup");
01074
01075 return 0;
01076 } else {
01077 if (f->frametype == AST_FRAME_MODEM) {
01078
01079 if (who == c0) {
01080 ast_write(c1, f);
01081 } else if (who == c1) {
01082 ast_write(c0, f);
01083 }
01084 }
01085 ast_frfree(f);
01086 }
01087
01088 cs[2] = cs[0];
01089 cs[0] = cs[1];
01090 cs[1] = cs[2];
01091 }
01092 return -1;
01093 }
01094
01095 static int udptl_do_debug_ip(int fd, int argc, char *argv[])
01096 {
01097 struct hostent *hp;
01098 struct ast_hostent ahp;
01099 int port;
01100 char *p;
01101 char *arg;
01102
01103 port = 0;
01104 if (argc != 4)
01105 return RESULT_SHOWUSAGE;
01106 arg = argv[3];
01107 p = strstr(arg, ":");
01108 if (p) {
01109 *p = '\0';
01110 p++;
01111 port = atoi(p);
01112 }
01113 hp = ast_gethostbyname(arg, &ahp);
01114 if (hp == NULL)
01115 return RESULT_SHOWUSAGE;
01116 udptldebugaddr.sin_family = AF_INET;
01117 memcpy(&udptldebugaddr.sin_addr, hp->h_addr, sizeof(udptldebugaddr.sin_addr));
01118 udptldebugaddr.sin_port = htons(port);
01119 if (port == 0)
01120 ast_cli(fd, "UDPTL Debugging Enabled for IP: %s\n", ast_inet_ntoa(udptldebugaddr.sin_addr));
01121 else
01122 ast_cli(fd, "UDPTL Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(udptldebugaddr.sin_addr), port);
01123 udptldebug = 1;
01124 return RESULT_SUCCESS;
01125 }
01126
01127 static int udptl_do_debug(int fd, int argc, char *argv[])
01128 {
01129 if (argc != 2) {
01130 if (argc != 4)
01131 return RESULT_SHOWUSAGE;
01132 return udptl_do_debug_ip(fd, argc, argv);
01133 }
01134 udptldebug = 1;
01135 memset(&udptldebugaddr,0,sizeof(udptldebugaddr));
01136 ast_cli(fd, "UDPTL Debugging Enabled\n");
01137 return RESULT_SUCCESS;
01138 }
01139
01140 static int udptl_nodebug(int fd, int argc, char *argv[])
01141 {
01142 if (argc != 3)
01143 return RESULT_SHOWUSAGE;
01144 udptldebug = 0;
01145 ast_cli(fd,"UDPTL Debugging Disabled\n");
01146 return RESULT_SUCCESS;
01147 }
01148
01149 static char debug_usage[] =
01150 "Usage: udptl debug [ip host[:port]]\n"
01151 " Enable dumping of all UDPTL packets to and from host.\n";
01152
01153 static char nodebug_usage[] =
01154 "Usage: udptl debug off\n"
01155 " Disable all UDPTL debugging\n";
01156
01157 static struct ast_cli_entry cli_udptl_no_debug = {
01158 { "udptl", "no", "debug", NULL },
01159 udptl_nodebug, NULL,
01160 NULL };
01161
01162 static struct ast_cli_entry cli_udptl[] = {
01163 { { "udptl", "debug", NULL },
01164 udptl_do_debug, "Enable UDPTL debugging",
01165 debug_usage },
01166
01167 { { "udptl", "debug", "ip", NULL },
01168 udptl_do_debug, "Enable UDPTL debugging on IP",
01169 debug_usage },
01170
01171 { { "udptl", "debug", "off", NULL },
01172 udptl_nodebug, "Disable UDPTL debugging",
01173 nodebug_usage, NULL, &cli_udptl_no_debug },
01174 };
01175
01176 void ast_udptl_reload(void)
01177 {
01178 struct ast_config *cfg;
01179 const char *s;
01180
01181 udptlstart = 4500;
01182 udptlend = 4999;
01183 udptlfectype = 0;
01184 udptlfecentries = 0;
01185 udptlfecspan = 0;
01186 udptlmaxdatagram = 0;
01187 use_even_ports = 0;
01188
01189 if ((cfg = ast_config_load("udptl.conf"))) {
01190 if ((s = ast_variable_retrieve(cfg, "general", "udptlstart"))) {
01191 udptlstart = atoi(s);
01192 if (udptlstart < 1024) {
01193 ast_log(LOG_WARNING, "Ports under 1024 are not allowed for T.38.\n");
01194 udptlstart = 1024;
01195 }
01196 if (udptlstart > 65535) {
01197 ast_log(LOG_WARNING, "Ports over 65535 are invalid.\n");
01198 udptlstart = 65535;
01199 }
01200 }
01201 if ((s = ast_variable_retrieve(cfg, "general", "udptlend"))) {
01202 udptlend = atoi(s);
01203 if (udptlend < 1024) {
01204 ast_log(LOG_WARNING, "Ports under 1024 are not allowed for T.38.\n");
01205 udptlend = 1024;
01206 }
01207 if (udptlend > 65535) {
01208 ast_log(LOG_WARNING, "Ports over 65535 are invalid.\n");
01209 udptlend = 65535;
01210 }
01211 }
01212 if ((s = ast_variable_retrieve(cfg, "general", "udptlchecksums"))) {
01213 #ifdef SO_NO_CHECK
01214 if (ast_false(s))
01215 nochecksums = 1;
01216 else
01217 nochecksums = 0;
01218 #else
01219 if (ast_false(s))
01220 ast_log(LOG_WARNING, "Disabling UDPTL checksums is not supported on this operating system!\n");
01221 #endif
01222 }
01223 if ((s = ast_variable_retrieve(cfg, "general", "T38FaxUdpEC"))) {
01224 if (strcmp(s, "t38UDPFEC") == 0)
01225 udptlfectype = 2;
01226 else if (strcmp(s, "t38UDPRedundancy") == 0)
01227 udptlfectype = 1;
01228 }
01229 if ((s = ast_variable_retrieve(cfg, "general", "T38FaxMaxDatagram"))) {
01230 udptlmaxdatagram = atoi(s);
01231 if (udptlmaxdatagram < 100) {
01232 ast_log(LOG_WARNING, "Too small T38FaxMaxDatagram size. Defaulting to 100.\n");
01233 udptlmaxdatagram = 100;
01234 }
01235 if (udptlmaxdatagram > LOCAL_FAX_MAX_DATAGRAM) {
01236 ast_log(LOG_WARNING, "Too large T38FaxMaxDatagram size. Defaulting to %d.\n", LOCAL_FAX_MAX_DATAGRAM);
01237 udptlmaxdatagram = LOCAL_FAX_MAX_DATAGRAM;
01238 }
01239 }
01240 if ((s = ast_variable_retrieve(cfg, "general", "UDPTLFECentries"))) {
01241 udptlfecentries = atoi(s);
01242 if (udptlfecentries < 1) {
01243 ast_log(LOG_WARNING, "Too small UDPTLFECentries value. Defaulting to 1.\n");
01244 udptlfecentries = 1;
01245 }
01246 if (udptlfecentries > MAX_FEC_ENTRIES) {
01247 ast_log(LOG_WARNING, "Too large UDPTLFECentries value. Defaulting to %d.\n", MAX_FEC_ENTRIES);
01248 udptlfecentries = MAX_FEC_ENTRIES;
01249 }
01250 }
01251 if ((s = ast_variable_retrieve(cfg, "general", "UDPTLFECspan"))) {
01252 udptlfecspan = atoi(s);
01253 if (udptlfecspan < 1) {
01254 ast_log(LOG_WARNING, "Too small UDPTLFECspan value. Defaulting to 1.\n");
01255 udptlfecspan = 1;
01256 }
01257 if (udptlfecspan > MAX_FEC_SPAN) {
01258 ast_log(LOG_WARNING, "Too large UDPTLFECspan value. Defaulting to %d.\n", MAX_FEC_SPAN);
01259 udptlfecspan = MAX_FEC_SPAN;
01260 }
01261 }
01262 if ((s = ast_variable_retrieve(cfg, "general", "use_even_ports"))) {
01263 use_even_ports = ast_true(s);
01264 }
01265 ast_config_destroy(cfg);
01266 }
01267 if (udptlstart >= udptlend) {
01268 ast_log(LOG_WARNING, "Unreasonable values for UDPTL start/end\n");
01269 udptlstart = 4500;
01270 udptlend = 4999;
01271 }
01272 if (use_even_ports && (udptlstart & 1)) {
01273 ++udptlstart;
01274 ast_log(LOG_NOTICE, "Odd numbered udptlstart specified but use_even_ports enabled. udptlstart is now %d\n", udptlstart);
01275 }
01276 if (use_even_ports && (udptlend & 1)) {
01277 --udptlend;
01278 ast_log(LOG_NOTICE, "Odd numbered udptlend specified but use_event_ports enabled. udptlend is now %d\n", udptlend);
01279 }
01280 if (option_verbose > 1)
01281 ast_verbose(VERBOSE_PREFIX_2 "UDPTL allocating from port range %d -> %d\n", udptlstart, udptlend);
01282 }
01283
01284 void ast_udptl_init(void)
01285 {
01286 ast_cli_register_multiple(cli_udptl, sizeof(cli_udptl) / sizeof(struct ast_cli_entry));
01287 ast_udptl_reload();
01288 }