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