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