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