| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
#include "asterisk.h" |
| 63 |
|
| 64 |
#include <sys/time.h> |
| 65 |
#include <signal.h> |
| 66 |
#include <fcntl.h> |
| 67 |
|
| 68 |
#include "asterisk/module.h" |
| 69 |
#include "asterisk/udptl.h" |
| 70 |
#include "asterisk/frame.h" |
| 71 |
#include "asterisk/channel.h" |
| 72 |
#include "asterisk/acl.h" |
| 73 |
#include "asterisk/config_options.h" |
| 74 |
#include "asterisk/lock.h" |
| 75 |
#include "asterisk/utils.h" |
| 76 |
#include "asterisk/netsock2.h" |
| 77 |
#include "asterisk/cli.h" |
| 78 |
#include "asterisk/unaligned.h" |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
#define UDPTL_MTU 1200 |
| 142 |
|
| 143 |
#if !defined(FALSE) |
| 144 |
#define FALSE 0 |
| 145 |
#endif |
| 146 |
#if !defined(TRUE) |
| 147 |
#define TRUE (!FALSE) |
| 148 |
#endif |
| 149 |
|
| 150 |
#define LOG_TAG(u) S_OR(u->tag, "no tag") |
| 151 |
|
| 152 |
#define DEFAULT_UDPTLSTART 4000 |
| 153 |
#define DEFAULT_UDPTLEND 4999 |
| 154 |
|
| 155 |
static int udptldebug; |
| 156 |
static struct ast_sockaddr udptldebugaddr; |
| 157 |
|
| 158 |
#define LOCAL_FAX_MAX_DATAGRAM 1400 |
| 159 |
#define DEFAULT_FAX_MAX_DATAGRAM 400 |
| 160 |
#define FAX_MAX_DATAGRAM_LIMIT 1400 |
| 161 |
#define MAX_FEC_ENTRIES 5 |
| 162 |
#define MAX_FEC_SPAN 5 |
| 163 |
|
| 164 |
#define UDPTL_BUF_MASK 15 |
| 165 |
|
| 166 |
typedef struct { |
| 167 |
int buf_len; |
| 168 |
uint8_t buf[LOCAL_FAX_MAX_DATAGRAM]; |
| 169 |
} udptl_fec_tx_buffer_t; |
| 170 |
|
| 171 |
typedef struct { |
| 172 |
int buf_len; |
| 173 |
uint8_t buf[LOCAL_FAX_MAX_DATAGRAM]; |
| 174 |
unsigned int fec_len[MAX_FEC_ENTRIES]; |
| 175 |
uint8_t fec[MAX_FEC_ENTRIES][LOCAL_FAX_MAX_DATAGRAM]; |
| 176 |
unsigned int fec_span; |
| 177 |
unsigned int fec_entries; |
| 178 |
} udptl_fec_rx_buffer_t; |
| 179 |
|
| 180 |
|
| 181 |
struct ast_udptl { |
| 182 |
int fd; |
| 183 |
char resp; |
| 184 |
struct ast_frame f[16]; |
| 185 |
unsigned char rawdata[8192 + AST_FRIENDLY_OFFSET]; |
| 186 |
unsigned int lasteventseqn; |
| 187 |
int nat; |
| 188 |
int flags; |
| 189 |
struct ast_sockaddr us; |
| 190 |
struct ast_sockaddr them; |
| 191 |
int *ioid; |
| 192 |
struct ast_sched_context *sched; |
| 193 |
struct io_context *io; |
| 194 |
void *data; |
| 195 |
char *tag; |
| 196 |
ast_udptl_callback callback; |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
enum ast_t38_ec_modes error_correction_scheme; |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
unsigned int error_correction_entries; |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
unsigned int error_correction_span; |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
int far_max_datagram; |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
int local_max_datagram; |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
int far_max_ifp; |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
int local_max_ifp; |
| 237 |
|
| 238 |
unsigned int tx_seq_no; |
| 239 |
unsigned int rx_seq_no; |
| 240 |
|
| 241 |
udptl_fec_tx_buffer_t tx[UDPTL_BUF_MASK + 1]; |
| 242 |
udptl_fec_rx_buffer_t rx[UDPTL_BUF_MASK + 1]; |
| 243 |
}; |
| 244 |
|
| 245 |
struct udptl_global_options { |
| 246 |
unsigned int start; |
| 247 |
unsigned int end; |
| 248 |
unsigned int fecentries; |
| 249 |
unsigned int fecspan; |
| 250 |
unsigned int nochecksums; |
| 251 |
unsigned int use_even_ports; |
| 252 |
}; |
| 253 |
|
| 254 |
static AO2_GLOBAL_OBJ_STATIC(globals); |
| 255 |
|
| 256 |
struct udptl_config { |
| 257 |
struct udptl_global_options *general; |
| 258 |
}; |
| 259 |
|
| 260 |
static void *udptl_snapshot_alloc(void); |
| 261 |
static int udptl_pre_apply_config(void); |
| 262 |
|
| 263 |
static struct aco_type general_option = { |
| 264 |
.type = ACO_GLOBAL, |
| 265 |
.name = "global", |
| 266 |
.category_match = ACO_WHITELIST_EXACT, |
| 267 |
.item_offset = offsetof(struct udptl_config, general), |
| 268 |
.category = "general", |
| 269 |
}; |
| 270 |
|
| 271 |
static struct aco_type *general_options[] = ACO_TYPES(&general_option); |
| 272 |
|
| 273 |
static struct aco_file udptl_conf = { |
| 274 |
.filename = "udptl.conf", |
| 275 |
.types = ACO_TYPES(&general_option), |
| 276 |
}; |
| 277 |
|
| 278 |
CONFIG_INFO_CORE("udptl", cfg_info, globals, udptl_snapshot_alloc, |
| 279 |
.files = ACO_FILES(&udptl_conf), |
| 280 |
.pre_apply_config = udptl_pre_apply_config, |
| 281 |
); |
| 282 |
|
| 283 |
static inline int udptl_debug_test_addr(const struct ast_sockaddr *addr) |
| 284 |
{ |
| 285 |
if (udptldebug == 0) |
| 286 |
return 0; |
| 287 |
|
| 288 |
if (ast_sockaddr_isnull(&udptldebugaddr)) { |
| 289 |
return 1; |
| 290 |
} |
| 291 |
|
| 292 |
if (ast_sockaddr_port(&udptldebugaddr)) { |
| 293 |
return !ast_sockaddr_cmp(&udptldebugaddr, addr); |
| 294 |
} else { |
| 295 |
return !ast_sockaddr_cmp_addr(&udptldebugaddr, addr); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
static int decode_length(uint8_t *buf, unsigned int limit, unsigned int *len, unsigned int *pvalue) |
| 300 |
{ |
| 301 |
if (*len >= limit) |
| 302 |
return -1; |
| 303 |
if ((buf[*len] & 0x80) == 0) { |
| 304 |
*pvalue = buf[*len]; |
| 305 |
(*len)++; |
| 306 |
return 0; |
| 307 |
} |
| 308 |
if ((buf[*len] & 0x40) == 0) { |
| 309 |
if (*len == limit - 1) |
| 310 |
return -1; |
| 311 |
*pvalue = (buf[*len] & 0x3F) << 8; |
| 312 |
(*len)++; |
| 313 |
*pvalue |= buf[*len]; |
| 314 |
(*len)++; |
| 315 |
return 0; |
| 316 |
} |
| 317 |
*pvalue = (buf[*len] & 0x3F) << 14; |
| 318 |
(*len)++; |
| 319 |
|
| 320 |
ast_debug(1, "UDPTL packet with length greater than 16K received, decoding will fail\n"); |
| 321 |
return 1; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
static int decode_open_type(uint8_t *buf, unsigned int limit, unsigned int *len, const uint8_t **p_object, unsigned int *p_num_octets) |
| 326 |
{ |
| 327 |
unsigned int octet_cnt = 0; |
| 328 |
|
| 329 |
if (decode_length(buf, limit, len, &octet_cnt) != 0) |
| 330 |
return -1; |
| 331 |
|
| 332 |
|
| 333 |
if ((*len + octet_cnt) > limit) { |
| 334 |
return -1; |
| 335 |
} |
| 336 |
|
| 337 |
*p_num_octets = octet_cnt; |
| 338 |
*p_object = &buf[*len]; |
| 339 |
*len += octet_cnt; |
| 340 |
|
| 341 |
return 0; |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
static unsigned int encode_length(uint8_t *buf, unsigned int *len, unsigned int value) |
| 346 |
{ |
| 347 |
unsigned int multiplier; |
| 348 |
|
| 349 |
if (value < 0x80) { |
| 350 |
|
| 351 |
buf[*len] = value; |
| 352 |
(*len)++; |
| 353 |
return value; |
| 354 |
} |
| 355 |
if (value < 0x4000) { |
| 356 |
|
| 357 |
|
| 358 |
buf[*len] = ((0x8000 | value) >> 8) & 0xFF; |
| 359 |
(*len)++; |
| 360 |
buf[*len] = value & 0xFF; |
| 361 |
(*len)++; |
| 362 |
return value; |
| 363 |
} |
| 364 |
|
| 365 |
multiplier = (value < 0x10000) ? (value >> 14) : 4; |
| 366 |
|
| 367 |
buf[*len] = 0xC0 | multiplier; |
| 368 |
(*len)++; |
| 369 |
return multiplier << 14; |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
static int encode_open_type(const struct ast_udptl *udptl, uint8_t *buf, unsigned int buflen, |
| 374 |
unsigned int *len, const uint8_t *data, unsigned int num_octets) |
| 375 |
{ |
| 376 |
unsigned int enclen; |
| 377 |
unsigned int octet_idx; |
| 378 |
uint8_t zero_byte; |
| 379 |
|
| 380 |
|
| 381 |
if (num_octets == 0) { |
| 382 |
zero_byte = 0; |
| 383 |
data = &zero_byte; |
| 384 |
num_octets = 1; |
| 385 |
} |
| 386 |
|
| 387 |
for (octet_idx = 0; ; num_octets -= enclen, octet_idx += enclen) { |
| 388 |
enclen = encode_length(buf, len, num_octets); |
| 389 |
if (enclen + *len > buflen) { |
| 390 |
ast_log(LOG_ERROR, "UDPTL (%s): Buffer overflow detected (%u + %u > %u)\n", |
| 391 |
LOG_TAG(udptl), enclen, *len, buflen); |
| 392 |
return -1; |
| 393 |
} |
| 394 |
if (enclen > 0) { |
| 395 |
memcpy(&buf[*len], &data[octet_idx], enclen); |
| 396 |
*len += enclen; |
| 397 |
} |
| 398 |
if (enclen >= num_octets) |
| 399 |
break; |
| 400 |
} |
| 401 |
|
| 402 |
return 0; |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
static int udptl_rx_packet(struct ast_udptl *s, uint8_t *buf, unsigned int len) |
| 407 |
{ |
| 408 |
int stat1; |
| 409 |
int stat2; |
| 410 |
int i; |
| 411 |
unsigned int ptr; |
| 412 |
int seq_no; |
| 413 |
const uint8_t *ifp = NULL; |
| 414 |
const uint8_t *data = NULL; |
| 415 |
unsigned int ifp_len = 0; |
| 416 |
int repaired[16]; |
| 417 |
const uint8_t *bufs[ARRAY_LEN(s->f) - 1]; |
| 418 |
unsigned int lengths[ARRAY_LEN(s->f) - 1]; |
| 419 |
int span; |
| 420 |
int entries; |
| 421 |
int ifp_no; |
| 422 |
|
| 423 |
ptr = 0; |
| 424 |
ifp_no = 0; |
| 425 |
memset(&s->f[0], 0, sizeof(s->f[0])); |
| 426 |
|
| 427 |
|
| 428 |
if (ptr + 2 > len) |
| 429 |
return -1; |
| 430 |
seq_no = (buf[0] << 8) | buf[1]; |
| 431 |
ptr += 2; |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
if (seq_no < 0x000F && s->rx_seq_no > 0xFFF0) { |
| 444 |
|
| 445 |
seq_no += 0x10000; |
| 446 |
} else { |
| 447 |
|
| 448 |
s->rx_seq_no &= 0xFFFF; |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
if ((stat1 = decode_open_type(buf, len, &ptr, &ifp, &ifp_len)) != 0) |
| 453 |
return -1; |
| 454 |
|
| 455 |
if (ptr + 1 > len) |
| 456 |
return -1; |
| 457 |
if ((buf[ptr++] & 0x80) == 0) { |
| 458 |
|
| 459 |
if (seq_no > s->rx_seq_no) { |
| 460 |
|
| 461 |
|
| 462 |
int total_count = 0; |
| 463 |
do { |
| 464 |
unsigned int count; |
| 465 |
if ((stat2 = decode_length(buf, len, &ptr, &count)) < 0) |
| 466 |
return -1; |
| 467 |
for (i = 0; i < count && total_count + i < ARRAY_LEN(bufs); i++) { |
| 468 |
if ((stat1 = decode_open_type(buf, len, &ptr, &bufs[total_count + i], &lengths[total_count + i])) != 0) { |
| 469 |
return -1; |
| 470 |
} |
| 471 |
|
| 472 |
if (!bufs[total_count + i] || !lengths[total_count + i]) { |
| 473 |
|
| 474 |
i--; |
| 475 |
count--; |
| 476 |
} |
| 477 |
} |
| 478 |
total_count += i; |
| 479 |
} |
| 480 |
while (stat2 > 0 && total_count < ARRAY_LEN(bufs)); |
| 481 |
|
| 482 |
for (i = total_count; i > 0; i--) { |
| 483 |
if (seq_no - i >= s->rx_seq_no) { |
| 484 |
|
| 485 |
|
| 486 |
ast_debug(3, "Recovering lost packet via secondary %d, len %u\n", seq_no - i, lengths[i - 1]); |
| 487 |
s->f[ifp_no].frametype = AST_FRAME_MODEM; |
| 488 |
s->f[ifp_no].subclass.integer = AST_MODEM_T38; |
| 489 |
|
| 490 |
s->f[ifp_no].mallocd = 0; |
| 491 |
s->f[ifp_no].seqno = seq_no - i; |
| 492 |
s->f[ifp_no].datalen = lengths[i - 1]; |
| 493 |
s->f[ifp_no].data.ptr = (uint8_t *) bufs[i - 1]; |
| 494 |
s->f[ifp_no].offset = 0; |
| 495 |
s->f[ifp_no].src = "UDPTL"; |
| 496 |
if (ifp_no > 0) |
| 497 |
AST_LIST_NEXT(&s->f[ifp_no - 1], frame_list) = &s->f[ifp_no]; |
| 498 |
AST_LIST_NEXT(&s->f[ifp_no], frame_list) = NULL; |
| 499 |
ifp_no++; |
| 500 |
} |
| 501 |
} |
| 502 |
} |
| 503 |
} |
| 504 |
else |
| 505 |
{ |
| 506 |
int j; |
| 507 |
int l; |
| 508 |
int x; |
| 509 |
|
| 510 |
|
| 511 |
if (ifp_len > LOCAL_FAX_MAX_DATAGRAM) |
| 512 |
return -1; |
| 513 |
|
| 514 |
for ( ; seq_no > s->rx_seq_no; s->rx_seq_no++) { |
| 515 |
x = s->rx_seq_no & UDPTL_BUF_MASK; |
| 516 |
s->rx[x].buf_len = -1; |
| 517 |
s->rx[x].fec_len[0] = 0; |
| 518 |
s->rx[x].fec_span = 0; |
| 519 |
s->rx[x].fec_entries = 0; |
| 520 |
} |
| 521 |
|
| 522 |
x = seq_no & UDPTL_BUF_MASK; |
| 523 |
|
| 524 |
memset(repaired, 0, sizeof(repaired)); |
| 525 |
|
| 526 |
|
| 527 |
memcpy(s->rx[x].buf, ifp, ifp_len); |
| 528 |
s->rx[x].buf_len = ifp_len; |
| 529 |
repaired[x] = TRUE; |
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
if (ptr + 2 > len) |
| 535 |
return -1; |
| 536 |
if (buf[ptr++] != 1) |
| 537 |
return -1; |
| 538 |
span = buf[ptr++]; |
| 539 |
s->rx[x].fec_span = span; |
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
if (ptr + 1 > len) |
| 544 |
return -1; |
| 545 |
entries = buf[ptr++]; |
| 546 |
if (entries > MAX_FEC_ENTRIES) { |
| 547 |
return -1; |
| 548 |
} |
| 549 |
s->rx[x].fec_entries = entries; |
| 550 |
|
| 551 |
|
| 552 |
for (i = 0; i < entries; i++) { |
| 553 |
if ((stat1 = decode_open_type(buf, len, &ptr, &data, &s->rx[x].fec_len[i])) != 0) |
| 554 |
return -1; |
| 555 |
if (s->rx[x].fec_len[i] > LOCAL_FAX_MAX_DATAGRAM) |
| 556 |
return -1; |
| 557 |
|
| 558 |
|
| 559 |
memcpy(s->rx[x].fec[i], data, s->rx[x].fec_len[i]); |
| 560 |
#if 0 |
| 561 |
fprintf(stderr, "FEC: "); |
| 562 |
for (j = 0; j < s->rx[x].fec_len[i]; j++) |
| 563 |
fprintf(stderr, "%02hhX ", data[j]); |
| 564 |
fprintf(stderr, "\n"); |
| 565 |
#endif |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
for (l = x; l != ((x - (16 - span*entries)) & UDPTL_BUF_MASK); l = (l - 1) & UDPTL_BUF_MASK) { |
| 571 |
int m; |
| 572 |
if (s->rx[l].fec_len[0] <= 0) |
| 573 |
continue; |
| 574 |
for (m = 0; m < s->rx[l].fec_entries; m++) { |
| 575 |
int k; |
| 576 |
int which; |
| 577 |
int limit = (l + m) & UDPTL_BUF_MASK; |
| 578 |
|
| 579 |
|
| 580 |
if (seq_no <= (s->rx[l].fec_span * s->rx[l].fec_entries) - m) { |
| 581 |
continue; |
| 582 |
} |
| 583 |
|
| 584 |
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) { |
| 585 |
if (s->rx[k].buf_len <= 0) |
| 586 |
which = (which == -1) ? k : -2; |
| 587 |
} |
| 588 |
if (which >= 0) { |
| 589 |
|
| 590 |
for (j = 0; j < s->rx[l].fec_len[m]; j++) { |
| 591 |
s->rx[which].buf[j] = s->rx[l].fec[m][j]; |
| 592 |
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) |
| 593 |
s->rx[which].buf[j] ^= (s->rx[k].buf_len > j) ? s->rx[k].buf[j] : 0; |
| 594 |
} |
| 595 |
s->rx[which].buf_len = s->rx[l].fec_len[m]; |
| 596 |
repaired[which] = TRUE; |
| 597 |
} |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
for (l = (x + 1) & UDPTL_BUF_MASK, j = seq_no - UDPTL_BUF_MASK; l != x; l = (l + 1) & UDPTL_BUF_MASK, j++) { |
| 602 |
if (repaired[l]) { |
| 603 |
|
| 604 |
s->f[ifp_no].frametype = AST_FRAME_MODEM; |
| 605 |
s->f[ifp_no].subclass.integer = AST_MODEM_T38; |
| 606 |
|
| 607 |
s->f[ifp_no].mallocd = 0; |
| 608 |
s->f[ifp_no].seqno = j; |
| 609 |
s->f[ifp_no].datalen = s->rx[l].buf_len; |
| 610 |
s->f[ifp_no].data.ptr = s->rx[l].buf; |
| 611 |
s->f[ifp_no].offset = 0; |
| 612 |
s->f[ifp_no].src = "UDPTL"; |
| 613 |
if (ifp_no > 0) |
| 614 |
AST_LIST_NEXT(&s->f[ifp_no - 1], frame_list) = &s->f[ifp_no]; |
| 615 |
AST_LIST_NEXT(&s->f[ifp_no], frame_list) = NULL; |
| 616 |
ifp_no++; |
| 617 |
} |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
if (seq_no >= s->rx_seq_no) { |
| 624 |
|
| 625 |
s->f[ifp_no].frametype = AST_FRAME_MODEM; |
| 626 |
s->f[ifp_no].subclass.integer = AST_MODEM_T38; |
| 627 |
|
| 628 |
s->f[ifp_no].mallocd = 0; |
| 629 |
s->f[ifp_no].seqno = seq_no; |
| 630 |
s->f[ifp_no].datalen = ifp_len; |
| 631 |
s->f[ifp_no].data.ptr = (uint8_t *) ifp; |
| 632 |
s->f[ifp_no].offset = 0; |
| 633 |
s->f[ifp_no].src = "UDPTL"; |
| 634 |
if (ifp_no > 0) |
| 635 |
AST_LIST_NEXT(&s->f[ifp_no - 1], frame_list) = &s->f[ifp_no]; |
| 636 |
AST_LIST_NEXT(&s->f[ifp_no], frame_list) = NULL; |
| 637 |
|
| 638 |
ifp_no++; |
| 639 |
} |
| 640 |
|
| 641 |
s->rx_seq_no = seq_no + 1; |
| 642 |
return ifp_no; |
| 643 |
} |
| 644 |
|
| 645 |
|
| 646 |
static int udptl_build_packet(struct ast_udptl *s, uint8_t *buf, unsigned int buflen, uint8_t *ifp, unsigned int ifp_len) |
| 647 |
{ |
| 648 |
uint8_t fec[LOCAL_FAX_MAX_DATAGRAM * 2] = { 0, }; |
| 649 |
int i; |
| 650 |
int j; |
| 651 |
int seq; |
| 652 |
int entry; |
| 653 |
int entries; |
| 654 |
int span; |
| 655 |
int m; |
| 656 |
unsigned int len; |
| 657 |
int limit; |
| 658 |
int high_tide; |
| 659 |
|
| 660 |
seq = s->tx_seq_no & 0xFFFF; |
| 661 |
|
| 662 |
|
| 663 |
entry = seq & UDPTL_BUF_MASK; |
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
s->tx[entry].buf_len = ifp_len; |
| 668 |
memcpy(s->tx[entry].buf, ifp, ifp_len); |
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
len = 0; |
| 673 |
|
| 674 |
buf[len++] = (seq >> 8) & 0xFF; |
| 675 |
buf[len++] = seq & 0xFF; |
| 676 |
|
| 677 |
|
| 678 |
if (encode_open_type(s, buf, buflen, &len, ifp, ifp_len) < 0) |
| 679 |
return -1; |
| 680 |
|
| 681 |
|
| 682 |
switch (s->error_correction_scheme) |
| 683 |
{ |
| 684 |
case UDPTL_ERROR_CORRECTION_NONE: |
| 685 |
|
| 686 |
buf[len++] = 0x00; |
| 687 |
|
| 688 |
|
| 689 |
encode_length(buf, &len, 0); |
| 690 |
break; |
| 691 |
case UDPTL_ERROR_CORRECTION_REDUNDANCY: |
| 692 |
|
| 693 |
buf[len++] = 0x00; |
| 694 |
if (s->tx_seq_no > s->error_correction_entries) |
| 695 |
entries = s->error_correction_entries; |
| 696 |
else |
| 697 |
entries = s->tx_seq_no; |
| 698 |
|
| 699 |
|
| 700 |
encode_length(buf, &len, entries); |
| 701 |
|
| 702 |
for (i = 0; i < entries; i++) { |
| 703 |
j = (entry - i - 1) & UDPTL_BUF_MASK; |
| 704 |
if (encode_open_type(s, buf, buflen, &len, s->tx[j].buf, s->tx[j].buf_len) < 0) { |
| 705 |
ast_debug(1, "UDPTL (%s): Encoding failed at i=%d, j=%d\n", |
| 706 |
LOG_TAG(s), i, j); |
| 707 |
return -1; |
| 708 |
} |
| 709 |
} |
| 710 |
break; |
| 711 |
case UDPTL_ERROR_CORRECTION_FEC: |
| 712 |
span = s->error_correction_span; |
| 713 |
entries = s->error_correction_entries; |
| 714 |
if (seq < s->error_correction_span*s->error_correction_entries) { |
| 715 |
|
| 716 |
entries = seq/s->error_correction_span; |
| 717 |
if (seq < s->error_correction_span) |
| 718 |
span = 0; |
| 719 |
} |
| 720 |
|
| 721 |
buf[len++] = 0x80; |
| 722 |
|
| 723 |
|
| 724 |
buf[len++] = 1; |
| 725 |
buf[len++] = span; |
| 726 |
|
| 727 |
|
| 728 |
buf[len++] = entries; |
| 729 |
for (m = 0; m < entries; m++) { |
| 730 |
|
| 731 |
limit = (entry + m) & UDPTL_BUF_MASK; |
| 732 |
high_tide = 0; |
| 733 |
for (i = (limit - span*entries) & UDPTL_BUF_MASK; i != limit; i = (i + entries) & UDPTL_BUF_MASK) { |
| 734 |
if (high_tide < s->tx[i].buf_len) { |
| 735 |
for (j = 0; j < high_tide; j++) |
| 736 |
fec[j] ^= s->tx[i].buf[j]; |
| 737 |
for ( ; j < s->tx[i].buf_len; j++) |
| 738 |
fec[j] = s->tx[i].buf[j]; |
| 739 |
high_tide = s->tx[i].buf_len; |
| 740 |
} else { |
| 741 |
for (j = 0; j < s->tx[i].buf_len; j++) |
| 742 |
fec[j] ^= s->tx[i].buf[j]; |
| 743 |
} |
| 744 |
} |
| 745 |
if (encode_open_type(s, buf, buflen, &len, fec, high_tide) < 0) |
| 746 |
return -1; |
| 747 |
} |
| 748 |
break; |
| 749 |
} |
| 750 |
|
| 751 |
s->tx_seq_no++; |
| 752 |
return len; |
| 753 |
} |
| 754 |
|
| 755 |
int ast_udptl_fd(const struct ast_udptl *udptl) |
| 756 |
{ |
| 757 |
return udptl->fd; |
| 758 |
} |
| 759 |
|
| 760 |
void ast_udptl_set_data(struct ast_udptl *udptl, void *data) |
| 761 |
{ |
| 762 |
udptl->data = data; |
| 763 |
} |
| 764 |
|
| 765 |
void ast_udptl_set_callback(struct ast_udptl *udptl, ast_udptl_callback callback) |
| 766 |
{ |
| 767 |
udptl->callback = callback; |
| 768 |
} |
| 769 |
|
| 770 |
void ast_udptl_setnat(struct ast_udptl *udptl, int nat) |
| 771 |
{ |
| 772 |
udptl->nat = nat; |
| 773 |
} |
| 774 |
|
| 775 |
static int udptlread(int *id, int fd, short events, void *cbdata) |
| 776 |
{ |
| 777 |
struct ast_udptl *udptl = cbdata; |
| 778 |
struct ast_frame *f; |
| 779 |
|
| 780 |
if ((f = ast_udptl_read(udptl))) { |
| 781 |
if (udptl->callback) |
| 782 |
udptl->callback(udptl, f, udptl->data); |
| 783 |
} |
| 784 |
return 1; |
| 785 |
} |
| 786 |
|
| 787 |
struct ast_frame *ast_udptl_read(struct ast_udptl *udptl) |
| 788 |
{ |
| 789 |
int res; |
| 790 |
struct ast_sockaddr addr; |
| 791 |
uint8_t *buf; |
| 792 |
|
| 793 |
buf = udptl->rawdata + AST_FRIENDLY_OFFSET; |
| 794 |
|
| 795 |
|
| 796 |
res = ast_recvfrom(udptl->fd, |
| 797 |
buf, |
| 798 |
sizeof(udptl->rawdata) - AST_FRIENDLY_OFFSET, |
| 799 |
0, |
| 800 |
&addr); |
| 801 |
if (res < 0) { |
| 802 |
if (errno != EAGAIN) |
| 803 |
ast_log(LOG_WARNING, "UDPTL (%s): read error: %s\n", |
| 804 |
LOG_TAG(udptl), strerror(errno)); |
| 805 |
ast_assert(errno != EBADF); |
| 806 |
return &ast_null_frame; |
| 807 |
} |
| 808 |
|
| 809 |
|
| 810 |
if (ast_sockaddr_isnull(&udptl->them)) { |
| 811 |
return &ast_null_frame; |
| 812 |
} |
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
if (udptl->tag == NULL) { |
| 823 |
return &ast_null_frame; |
| 824 |
} |
| 825 |
|
| 826 |
if (udptl->nat) { |
| 827 |
|
| 828 |
if (ast_sockaddr_cmp(&udptl->them, &addr)) { |
| 829 |
ast_sockaddr_copy(&udptl->them, &addr); |
| 830 |
ast_debug(1, "UDPTL (%s): NAT, Using address %s\n", |
| 831 |
LOG_TAG(udptl), ast_sockaddr_stringify(&udptl->them)); |
| 832 |
} |
| 833 |
} |
| 834 |
|
| 835 |
if (udptl_debug_test_addr(&addr)) { |
| 836 |
int seq_no; |
| 837 |
|
| 838 |
|
| 839 |
if (res < 2) { |
| 840 |
|
| 841 |
seq_no = -1; |
| 842 |
} else { |
| 843 |
seq_no = (buf[0] << 8) | buf[1]; |
| 844 |
} |
| 845 |
|
| 846 |
ast_verb(1, "UDPTL (%s): packet from %s (seq %d, len %d)\n", |
| 847 |
LOG_TAG(udptl), ast_sockaddr_stringify(&addr), seq_no, res); |
| 848 |
} |
| 849 |
if (udptl_rx_packet(udptl, buf, res) < 1) { |
| 850 |
return &ast_null_frame; |
| 851 |
} |
| 852 |
|
| 853 |
return &udptl->f[0]; |
| 854 |
} |
| 855 |
|
| 856 |
static void calculate_local_max_datagram(struct ast_udptl *udptl) |
| 857 |
{ |
| 858 |
unsigned int new_max = 0; |
| 859 |
|
| 860 |
if (udptl->local_max_ifp == -1) { |
| 861 |
ast_log(LOG_WARNING, "UDPTL (%s): Cannot calculate local_max_datagram before local_max_ifp has been set.\n", |
| 862 |
LOG_TAG(udptl)); |
| 863 |
udptl->local_max_datagram = -1; |
| 864 |
return; |
| 865 |
} |
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
switch (udptl->error_correction_scheme) { |
| 874 |
case UDPTL_ERROR_CORRECTION_NONE: |
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
new_max = 5 + udptl->local_max_ifp; |
| 879 |
break; |
| 880 |
case UDPTL_ERROR_CORRECTION_REDUNDANCY: |
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
new_max = 5 + udptl->local_max_ifp + 2 + (3 * udptl->local_max_ifp); |
| 885 |
break; |
| 886 |
case UDPTL_ERROR_CORRECTION_FEC: |
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
new_max = 5 + udptl->local_max_ifp + 4 + udptl->local_max_ifp; |
| 891 |
break; |
| 892 |
} |
| 893 |
|
| 894 |
udptl->local_max_datagram = MIN(new_max * 1.05, LOCAL_FAX_MAX_DATAGRAM); |
| 895 |
} |
| 896 |
|
| 897 |
static void calculate_far_max_ifp(struct ast_udptl *udptl) |
| 898 |
{ |
| 899 |
unsigned new_max = 0; |
| 900 |
|
| 901 |
if (udptl->far_max_datagram == -1) { |
| 902 |
ast_log(LOG_WARNING, "UDPTL (%s): Cannot calculate far_max_ifp before far_max_datagram has been set.\n", |
| 903 |
LOG_TAG(udptl)); |
| 904 |
udptl->far_max_ifp = -1; |
| 905 |
return; |
| 906 |
} |
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
switch (udptl->error_correction_scheme) { |
| 922 |
case UDPTL_ERROR_CORRECTION_NONE: |
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
new_max = udptl->far_max_datagram - 5; |
| 927 |
break; |
| 928 |
case UDPTL_ERROR_CORRECTION_REDUNDANCY: |
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
for (;;) { |
| 942 |
new_max = (udptl->far_max_datagram - 8) / (udptl->error_correction_entries + 1); |
| 943 |
|
| 944 |
if ((new_max < 80) && (udptl->error_correction_entries > 1)) { |
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
--udptl->error_correction_entries; |
| 949 |
} else { |
| 950 |
break; |
| 951 |
} |
| 952 |
} |
| 953 |
break; |
| 954 |
case UDPTL_ERROR_CORRECTION_FEC: |
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
new_max = (udptl->far_max_datagram - 10) / 2; |
| 959 |
break; |
| 960 |
} |
| 961 |
|
| 962 |
udptl->far_max_ifp = new_max * 0.95; |
| 963 |
} |
| 964 |
|
| 965 |
enum ast_t38_ec_modes ast_udptl_get_error_correction_scheme(const struct ast_udptl *udptl) |
| 966 |
{ |
| 967 |
return udptl->error_correction_scheme; |
| 968 |
} |
| 969 |
|
| 970 |
void ast_udptl_set_error_correction_scheme(struct ast_udptl *udptl, enum ast_t38_ec_modes ec) |
| 971 |
{ |
| 972 |
udptl->error_correction_scheme = ec; |
| 973 |
switch (ec) { |
| 974 |
case UDPTL_ERROR_CORRECTION_FEC: |
| 975 |
udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_FEC; |
| 976 |
if (udptl->error_correction_entries == 0) { |
| 977 |
udptl->error_correction_entries = 3; |
| 978 |
} |
| 979 |
if (udptl->error_correction_span == 0) { |
| 980 |
udptl->error_correction_span = 3; |
| 981 |
} |
| 982 |
break; |
| 983 |
case UDPTL_ERROR_CORRECTION_REDUNDANCY: |
| 984 |
udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_REDUNDANCY; |
| 985 |
if (udptl->error_correction_entries == 0) { |
| 986 |
udptl->error_correction_entries = 3; |
| 987 |
} |
| 988 |
break; |
| 989 |
default: |
| 990 |
|
| 991 |
break; |
| 992 |
}; |
| 993 |
|
| 994 |
udptl->local_max_datagram = -1; |
| 995 |
udptl->far_max_ifp = -1; |
| 996 |
} |
| 997 |
|
| 998 |
void ast_udptl_set_local_max_ifp(struct ast_udptl *udptl, unsigned int max_ifp) |
| 999 |
{ |
| 1000 |
|
| 1001 |
|
| 1002 |
if ((signed int) max_ifp > 0) { |
| 1003 |
udptl->local_max_ifp = max_ifp; |
| 1004 |
|
| 1005 |
udptl->local_max_datagram = -1; |
| 1006 |
} |
| 1007 |
} |
| 1008 |
|
| 1009 |
unsigned int ast_udptl_get_local_max_datagram(struct ast_udptl *udptl) |
| 1010 |
{ |
| 1011 |
if (udptl->local_max_datagram == -1) { |
| 1012 |
calculate_local_max_datagram(udptl); |
| 1013 |
} |
| 1014 |
|
| 1015 |
|
| 1016 |
if (udptl->local_max_datagram < 0) { |
| 1017 |
return 0; |
| 1018 |
} |
| 1019 |
return udptl->local_max_datagram; |
| 1020 |
} |
| 1021 |
|
| 1022 |
void ast_udptl_set_far_max_datagram(struct ast_udptl *udptl, unsigned int max_datagram) |
| 1023 |
{ |
| 1024 |
if (!max_datagram || (max_datagram > FAX_MAX_DATAGRAM_LIMIT)) { |
| 1025 |
udptl->far_max_datagram = DEFAULT_FAX_MAX_DATAGRAM; |
| 1026 |
} else { |
| 1027 |
udptl->far_max_datagram = max_datagram; |
| 1028 |
} |
| 1029 |
|
| 1030 |
udptl->far_max_ifp = -1; |
| 1031 |
} |
| 1032 |
|
| 1033 |
unsigned int ast_udptl_get_far_max_datagram(const struct ast_udptl *udptl) |
| 1034 |
{ |
| 1035 |
if (udptl->far_max_datagram < 0) { |
| 1036 |
return 0; |
| 1037 |
} |
| 1038 |
return udptl->far_max_datagram; |
| 1039 |
} |
| 1040 |
|
| 1041 |
unsigned int ast_udptl_get_far_max_ifp(struct ast_udptl *udptl) |
| 1042 |
{ |
| 1043 |
if (udptl->far_max_ifp == -1) { |
| 1044 |
calculate_far_max_ifp(udptl); |
| 1045 |
} |
| 1046 |
|
| 1047 |
if (udptl->far_max_ifp < 0) { |
| 1048 |
return 0; |
| 1049 |
} |
| 1050 |
return udptl->far_max_ifp; |
| 1051 |
} |
| 1052 |
|
| 1053 |
struct ast_udptl *ast_udptl_new_with_bindaddr(struct ast_sched_context *sched, struct io_context *io, int callbackmode, struct ast_sockaddr *addr) |
| 1054 |
{ |
| 1055 |
struct ast_udptl *udptl; |
| 1056 |
int x; |
| 1057 |
int startplace; |
| 1058 |
int i; |
| 1059 |
RAII_VAR(struct udptl_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup); |
| 1060 |
|
| 1061 |
if (!cfg || !cfg->general) { |
| 1062 |
ast_log(LOG_ERROR, "Could not access global udptl options!\n"); |
| 1063 |
return NULL; |
| 1064 |
} |
| 1065 |
|
| 1066 |
if (!(udptl = ast_calloc(1, sizeof(*udptl)))) { |
| 1067 |
return NULL; |
| 1068 |
} |
| 1069 |
|
| 1070 |
udptl->error_correction_span = cfg->general->fecspan; |
| 1071 |
udptl->error_correction_entries = cfg->general->fecentries; |
| 1072 |
|
| 1073 |
udptl->far_max_datagram = -1; |
| 1074 |
udptl->far_max_ifp = -1; |
| 1075 |
udptl->local_max_ifp = -1; |
| 1076 |
udptl->local_max_datagram = -1; |
| 1077 |
|
| 1078 |
for (i = 0; i <= UDPTL_BUF_MASK; i++) { |
| 1079 |
udptl->rx[i].buf_len = -1; |
| 1080 |
udptl->tx[i].buf_len = -1; |
| 1081 |
} |
| 1082 |
|
| 1083 |
if ((udptl->fd = ast_socket_nonblock(ast_sockaddr_is_ipv6(addr) ? |
| 1084 |
AF_INET6 : AF_INET, SOCK_DGRAM, 0)) < 0) { |
| 1085 |
ast_free(udptl); |
| 1086 |
ast_log(LOG_WARNING, "Unable to allocate socket: %s\n", strerror(errno)); |
| 1087 |
return NULL; |
| 1088 |
} |
| 1089 |
|
| 1090 |
#ifdef SO_NO_CHECK |
| 1091 |
if (cfg->general->nochecksums) |
| 1092 |
setsockopt(udptl->fd, SOL_SOCKET, SO_NO_CHECK, &cfg->general->nochecksums, sizeof(cfg->general->nochecksums)); |
| 1093 |
#endif |
| 1094 |
|
| 1095 |
|
| 1096 |
x = (cfg->general->start == cfg->general->end) ? cfg->general->start : (ast_random() % (cfg->general->end - cfg->general->start)) + cfg->general->start; |
| 1097 |
if (cfg->general->use_even_ports && (x & 1)) { |
| 1098 |
++x; |
| 1099 |
} |
| 1100 |
startplace = x; |
| 1101 |
for (;;) { |
| 1102 |
ast_sockaddr_copy(&udptl->us, addr); |
| 1103 |
ast_sockaddr_set_port(&udptl->us, x); |
| 1104 |
if (ast_bind(udptl->fd, &udptl->us) == 0) { |
| 1105 |
break; |
| 1106 |
} |
| 1107 |
if (errno != EADDRINUSE && errno != EACCES) { |
| 1108 |
ast_log(LOG_WARNING, "Unexpected bind error: %s\n", strerror(errno)); |
| 1109 |
close(udptl->fd); |
| 1110 |
ast_free(udptl); |
| 1111 |
return NULL; |
| 1112 |
} |
| 1113 |
if (cfg->general->use_even_ports) { |
| 1114 |
x += 2; |
| 1115 |
} else { |
| 1116 |
++x; |
| 1117 |
} |
| 1118 |
if (x > cfg->general->end) |
| 1119 |
x = cfg->general->start; |
| 1120 |
if (x == startplace) { |
| 1121 |
ast_log(LOG_WARNING, "No UDPTL ports remaining\n"); |
| 1122 |
close(udptl->fd); |
| 1123 |
ast_free(udptl); |
| 1124 |
return NULL; |
| 1125 |
} |
| 1126 |
} |
| 1127 |
if (io && sched && callbackmode) { |
| 1128 |
|
| 1129 |
udptl->sched = sched; |
| 1130 |
udptl->io = io; |
| 1131 |
udptl->ioid = ast_io_add(udptl->io, udptl->fd, udptlread, AST_IO_IN, udptl); |
| 1132 |
} |
| 1133 |
|
| 1134 |
return udptl; |
| 1135 |
} |
| 1136 |
|
| 1137 |
void ast_udptl_set_tag(struct ast_udptl *udptl, const char *format, ...) |
| 1138 |
{ |
| 1139 |
va_list ap; |
| 1140 |
|
| 1141 |
ast_free(udptl->tag); |
| 1142 |
udptl->tag = NULL; |
| 1143 |
va_start(ap, format); |
| 1144 |
if (ast_vasprintf(&udptl->tag, format, ap) == -1) { |
| 1145 |
udptl->tag = NULL; |
| 1146 |
} |
| 1147 |
va_end(ap); |
| 1148 |
} |
| 1149 |
|
| 1150 |
int ast_udptl_setqos(struct ast_udptl *udptl, unsigned int tos, unsigned int cos) |
| 1151 |
{ |
| 1152 |
return ast_set_qos(udptl->fd, tos, cos, "UDPTL"); |
| 1153 |
} |
| 1154 |
|
| 1155 |
void ast_udptl_set_peer(struct ast_udptl *udptl, const struct ast_sockaddr *them) |
| 1156 |
{ |
| 1157 |
ast_sockaddr_copy(&udptl->them, them); |
| 1158 |
} |
| 1159 |
|
| 1160 |
void ast_udptl_get_peer(const struct ast_udptl *udptl, struct ast_sockaddr *them) |
| 1161 |
{ |
| 1162 |
ast_sockaddr_copy(them, &udptl->them); |
| 1163 |
} |
| 1164 |
|
| 1165 |
void ast_udptl_get_us(const struct ast_udptl *udptl, struct ast_sockaddr *us) |
| 1166 |
{ |
| 1167 |
ast_sockaddr_copy(us, &udptl->us); |
| 1168 |
} |
| 1169 |
|
| 1170 |
void ast_udptl_stop(struct ast_udptl *udptl) |
| 1171 |
{ |
| 1172 |
ast_sockaddr_setnull(&udptl->them); |
| 1173 |
} |
| 1174 |
|
| 1175 |
void ast_udptl_destroy(struct ast_udptl *udptl) |
| 1176 |
{ |
| 1177 |
if (udptl->ioid) |
| 1178 |
ast_io_remove(udptl->io, udptl->ioid); |
| 1179 |
if (udptl->fd > -1) |
| 1180 |
close(udptl->fd); |
| 1181 |
if (udptl->tag) |
| 1182 |
ast_free(udptl->tag); |
| 1183 |
ast_free(udptl); |
| 1184 |
} |
| 1185 |
|
| 1186 |
int ast_udptl_write(struct ast_udptl *s, struct ast_frame *f) |
| 1187 |
{ |
| 1188 |
unsigned int seq; |
| 1189 |
unsigned int len = f->datalen; |
| 1190 |
|
| 1191 |
const int bufsize = (s->far_max_datagram > 0) ? s->far_max_datagram : DEFAULT_FAX_MAX_DATAGRAM; |
| 1192 |
uint8_t buf[bufsize]; |
| 1193 |
|
| 1194 |
memset(buf, 0, sizeof(buf)); |
| 1195 |
|
| 1196 |
|
| 1197 |
if (ast_sockaddr_isnull(&s->them)) { |
| 1198 |
return 0; |
| 1199 |
} |
| 1200 |
|
| 1201 |
|
| 1202 |
if (f->datalen == 0) |
| 1203 |
return 0; |
| 1204 |
|
| 1205 |
if ((f->frametype != AST_FRAME_MODEM) || |
| 1206 |
(f->subclass.integer != AST_MODEM_T38)) { |
| 1207 |
ast_log(LOG_WARNING, "UDPTL (%s): UDPTL can only send T.38 data.\n", |
| 1208 |
LOG_TAG(s)); |
| 1209 |
return -1; |
| 1210 |
} |
| 1211 |
|
| 1212 |
if (len > s->far_max_ifp) { |
| 1213 |
ast_log(LOG_WARNING, |
| 1214 |
"UDPTL (%s): UDPTL asked to send %u bytes of IFP when far end only prepared to accept %d bytes; data loss will occur." |
| 1215 |
"You may need to override the T38FaxMaxDatagram value for this endpoint in the channel driver configuration.\n", |
| 1216 |
LOG_TAG(s), len, s->far_max_ifp); |
| 1217 |
len = s->far_max_ifp; |
| 1218 |
} |
| 1219 |
|
| 1220 |
|
| 1221 |
seq = s->tx_seq_no & 0xFFFF; |
| 1222 |
|
| 1223 |
|
| 1224 |
len = udptl_build_packet(s, buf, sizeof(buf), f->data.ptr, len); |
| 1225 |
|
| 1226 |
if ((signed int) len > 0 && !ast_sockaddr_isnull(&s->them)) { |
| 1227 |
if (ast_sendto(s->fd, buf, len, 0, &s->them) < 0) { |
| 1228 |
ast_log(LOG_NOTICE, "UDPTL (%s): Transmission error to %s: %s\n", |
| 1229 |
LOG_TAG(s), ast_sockaddr_stringify(&s->them), strerror(errno)); |
| 1230 |
} |
| 1231 |
if (udptl_debug_test_addr(&s->them)) { |
| 1232 |
ast_verb(1, "UDPTL (%s): packet to %s (seq %u, len %u)\n", |
| 1233 |
LOG_TAG(s), ast_sockaddr_stringify(&s->them), seq, len); |
| 1234 |
} |
| 1235 |
} |
| 1236 |
|
| 1237 |
return 0; |
| 1238 |
} |
| 1239 |
|
| 1240 |
static char *handle_cli_udptl_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 1241 |
{ |
| 1242 |
switch (cmd) { |
| 1243 |
case CLI_INIT: |
| 1244 |
e->command = "udptl set debug {on|off|ip}"; |
| 1245 |
e->usage = |
| 1246 |
"Usage: udptl set debug {on|off|ip host[:port]}\n" |
| 1247 |
" Enable or disable dumping of UDPTL packets.\n" |
| 1248 |
" If ip is specified, limit the dumped packets to those to and from\n" |
| 1249 |
" the specified 'host' with optional port.\n"; |
| 1250 |
return NULL; |
| 1251 |
case CLI_GENERATE: |
| 1252 |
return NULL; |
| 1253 |
} |
| 1254 |
|
| 1255 |
if (a->argc < 4 || a->argc > 5) |
| 1256 |
return CLI_SHOWUSAGE; |
| 1257 |
|
| 1258 |
if (a->argc == 4) { |
| 1259 |
if (!strncasecmp(a->argv[3], "on", 2)) { |
| 1260 |
udptldebug = 1; |
| 1261 |
memset(&udptldebugaddr, 0, sizeof(udptldebugaddr)); |
| 1262 |
ast_cli(a->fd, "UDPTL Debugging Enabled\n"); |
| 1263 |
} else if (!strncasecmp(a->argv[3], "off", 3)) { |
| 1264 |
udptldebug = 0; |
| 1265 |
ast_cli(a->fd, "UDPTL Debugging Disabled\n"); |
| 1266 |
} else { |
| 1267 |
return CLI_SHOWUSAGE; |
| 1268 |
} |
| 1269 |
} else { |
| 1270 |
struct ast_sockaddr *addrs; |
| 1271 |
if (strncasecmp(a->argv[3], "ip", 2)) |
| 1272 |
return CLI_SHOWUSAGE; |
| 1273 |
if (!ast_sockaddr_resolve(&addrs, a->argv[4], 0, 0)) { |
| 1274 |
return CLI_SHOWUSAGE; |
| 1275 |
} |
| 1276 |
ast_sockaddr_copy(&udptldebugaddr, &addrs[0]); |
| 1277 |
ast_cli(a->fd, "UDPTL Debugging Enabled for IP: %s\n", ast_sockaddr_stringify(&udptldebugaddr)); |
| 1278 |
udptldebug = 1; |
| 1279 |
ast_free(addrs); |
| 1280 |
} |
| 1281 |
|
| 1282 |
return CLI_SUCCESS; |
| 1283 |
} |
| 1284 |
|
| 1285 |
static char *handle_cli_show_config(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 1286 |
{ |
| 1287 |
RAII_VAR(struct udptl_config *, cfg, NULL, ao2_cleanup); |
| 1288 |
|
| 1289 |
switch (cmd) { |
| 1290 |
case CLI_INIT: |
| 1291 |
e->command = "udptl show config"; |
| 1292 |
e->usage = |
| 1293 |
"Usage: udptl show config\n" |
| 1294 |
" Display UDPTL configuration options\n"; |
| 1295 |
return NULL; |
| 1296 |
case CLI_GENERATE: |
| 1297 |
return NULL; |
| 1298 |
} |
| 1299 |
|
| 1300 |
if (!(cfg = ao2_global_obj_ref(globals))) { |
| 1301 |
return CLI_FAILURE; |
| 1302 |
} |
| 1303 |
|
| 1304 |
ast_cli(a->fd, "UDPTL Global options\n"); |
| 1305 |
ast_cli(a->fd, "--------------------\n"); |
| 1306 |
ast_cli(a->fd, "udptlstart: %u\n", cfg->general->start); |
| 1307 |
ast_cli(a->fd, "udptlend: %u\n", cfg->general->end); |
| 1308 |
ast_cli(a->fd, "udptlfecentries: %u\n", cfg->general->fecentries); |
| 1309 |
ast_cli(a->fd, "udptlfecspan: %u\n", cfg->general->fecspan); |
| 1310 |
ast_cli(a->fd, "use_even_ports: %s\n", AST_CLI_YESNO(cfg->general->use_even_ports)); |
| 1311 |
ast_cli(a->fd, "udptlchecksums: %s\n", AST_CLI_YESNO(!cfg->general->nochecksums)); |
| 1312 |
|
| 1313 |
return CLI_SUCCESS; |
| 1314 |
} |
| 1315 |
|
| 1316 |
static struct ast_cli_entry cli_udptl[] = { |
| 1317 |
AST_CLI_DEFINE(handle_cli_udptl_set_debug, "Enable/Disable UDPTL debugging"), |
| 1318 |
AST_CLI_DEFINE(handle_cli_show_config, "Show UDPTL config options"), |
| 1319 |
}; |
| 1320 |
|
| 1321 |
static void udptl_config_destructor(void *obj) |
| 1322 |
{ |
| 1323 |
struct udptl_config *cfg = obj; |
| 1324 |
ao2_cleanup(cfg->general); |
| 1325 |
} |
| 1326 |
|
| 1327 |
static void *udptl_snapshot_alloc(void) |
| 1328 |
{ |
| 1329 |
struct udptl_config *cfg; |
| 1330 |
|
| 1331 |
if (!(cfg = ao2_alloc(sizeof(*cfg), udptl_config_destructor))) { |
| 1332 |
return NULL; |
| 1333 |
} |
| 1334 |
if (!(cfg->general = ao2_alloc(sizeof(*cfg->general), NULL))) { |
| 1335 |
ao2_ref(cfg, -1); |
| 1336 |
return NULL; |
| 1337 |
} |
| 1338 |
|
| 1339 |
return cfg; |
| 1340 |
} |
| 1341 |
|
| 1342 |
static int removed_options_handler(const struct aco_option *opt, struct ast_variable *var, void *obj) |
| 1343 |
{ |
| 1344 |
if (!strcasecmp(var->name, "t38faxudpec")) { |
| 1345 |
ast_log(LOG_WARNING, "t38faxudpec in udptl.conf is no longer supported.\n"); |
| 1346 |
} else if (!strcasecmp(var->name, "t38faxmaxdatagram")) { |
| 1347 |
ast_log(LOG_WARNING, "t38faxmaxdatagram in udptl.conf is no longer supported; value is now supplied by T.38 applications.\n"); |
| 1348 |
} |
| 1349 |
return 0; |
| 1350 |
} |
| 1351 |
|
| 1352 |
static void __ast_udptl_reload(int reload) |
| 1353 |
{ |
| 1354 |
if (aco_process_config(&cfg_info, reload) == ACO_PROCESS_ERROR) { |
| 1355 |
if (!reload) { |
| 1356 |
RAII_VAR(struct udptl_config *, udptl_cfg, udptl_snapshot_alloc(), ao2_cleanup); |
| 1357 |
|
| 1358 |
if (aco_set_defaults(&general_option, "general", udptl_cfg->general)) { |
| 1359 |
ast_log(LOG_ERROR, "Failed to load udptl.conf and failed to initialize defaults.\n"); |
| 1360 |
return; |
| 1361 |
} |
| 1362 |
|
| 1363 |
ast_log(LOG_NOTICE, "Could not load udptl config; using defaults\n"); |
| 1364 |
ao2_global_obj_replace_unref(globals, udptl_cfg); |
| 1365 |
} |
| 1366 |
} |
| 1367 |
} |
| 1368 |
|
| 1369 |
static int udptl_pre_apply_config(void) { |
| 1370 |
struct udptl_config *cfg = aco_pending_config(&cfg_info); |
| 1371 |
|
| 1372 |
if (!cfg->general) { |
| 1373 |
return -1; |
| 1374 |
} |
| 1375 |
|
| 1376 |
#ifndef SO_NO_CHECK |
| 1377 |
if (cfg->general->nochecksums) { |
| 1378 |
ast_log(LOG_WARNING, "Disabling UDPTL checksums is not supported on this operating system!\n"); |
| 1379 |
cfg->general->nochecksums = 0; |
| 1380 |
} |
| 1381 |
#endif |
| 1382 |
|
| 1383 |
|
| 1384 |
if (cfg->general->use_even_ports && (cfg->general->start & 1)) { |
| 1385 |
++cfg->general->start; |
| 1386 |
ast_log(LOG_NOTICE, "Odd numbered udptlstart specified but use_even_ports enabled. udptlstart is now %u\n", cfg->general->start); |
| 1387 |
} |
| 1388 |
if (cfg->general->start > cfg->general->end) { |
| 1389 |
ast_log(LOG_WARNING, "Unreasonable values for UDPTL start/end ports; defaulting to %s-%s.\n", __stringify(DEFAULT_UDPTLSTART), __stringify(DEFAULT_UDPTLEND)); |
| 1390 |
cfg->general->start = DEFAULT_UDPTLSTART; |
| 1391 |
cfg->general->end = DEFAULT_UDPTLEND; |
| 1392 |
} |
| 1393 |
if (cfg->general->use_even_ports && (cfg->general->end & 1)) { |
| 1394 |
--cfg->general->end; |
| 1395 |
ast_log(LOG_NOTICE, "Odd numbered udptlend specified but use_even_ports enabled. udptlend is now %u\n", cfg->general->end); |
| 1396 |
} |
| 1397 |
|
| 1398 |
return 0; |
| 1399 |
} |
| 1400 |
|
| 1401 |
static int reload_module(void) |
| 1402 |
{ |
| 1403 |
__ast_udptl_reload(1); |
| 1404 |
|
| 1405 |
return 0; |
| 1406 |
} |
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
static int unload_module(void) |
| 1413 |
{ |
| 1414 |
ast_cli_unregister_multiple(cli_udptl, ARRAY_LEN(cli_udptl)); |
| 1415 |
ao2_t_global_obj_release(globals, "Unref udptl global container in shutdown"); |
| 1416 |
aco_info_destroy(&cfg_info); |
| 1417 |
|
| 1418 |
return 0; |
| 1419 |
} |
| 1420 |
|
| 1421 |
static int load_module(void) |
| 1422 |
{ |
| 1423 |
if (aco_info_init(&cfg_info)) { |
| 1424 |
return AST_MODULE_LOAD_FAILURE; |
| 1425 |
} |
| 1426 |
|
| 1427 |
aco_option_register(&cfg_info, "udptlstart", ACO_EXACT, general_options, __stringify(DEFAULT_UDPTLSTART), |
| 1428 |
OPT_UINT_T, PARSE_IN_RANGE | PARSE_DEFAULT, |
| 1429 |
FLDSET(struct udptl_global_options, start), DEFAULT_UDPTLSTART, 1024, 65535); |
| 1430 |
|
| 1431 |
aco_option_register(&cfg_info, "udptlend", ACO_EXACT, general_options, __stringify(DEFAULT_UDPTLEND), |
| 1432 |
OPT_UINT_T, PARSE_IN_RANGE | PARSE_DEFAULT, |
| 1433 |
FLDSET(struct udptl_global_options, end), DEFAULT_UDPTLEND, 1024, 65535); |
| 1434 |
|
| 1435 |
aco_option_register(&cfg_info, "udptlfecentries", ACO_EXACT, general_options, NULL, |
| 1436 |
OPT_UINT_T, PARSE_IN_RANGE | PARSE_RANGE_DEFAULTS, |
| 1437 |
FLDSET(struct udptl_global_options, fecentries), 1, MAX_FEC_ENTRIES); |
| 1438 |
|
| 1439 |
aco_option_register(&cfg_info, "udptlfecspan", ACO_EXACT, general_options, NULL, |
| 1440 |
OPT_UINT_T, PARSE_IN_RANGE | PARSE_RANGE_DEFAULTS, |
| 1441 |
FLDSET(struct udptl_global_options, fecspan), 1, MAX_FEC_SPAN); |
| 1442 |
|
| 1443 |
aco_option_register(&cfg_info, "udptlchecksums", ACO_EXACT, general_options, "yes", |
| 1444 |
OPT_BOOL_T, 0, FLDSET(struct udptl_global_options, nochecksums)); |
| 1445 |
|
| 1446 |
aco_option_register(&cfg_info, "use_even_ports", ACO_EXACT, general_options, "no", |
| 1447 |
OPT_BOOL_T, 1, FLDSET(struct udptl_global_options, use_even_ports)); |
| 1448 |
|
| 1449 |
aco_option_register_custom(&cfg_info, "t38faxudpec", ACO_EXACT, general_options, NULL, removed_options_handler, 0); |
| 1450 |
aco_option_register_custom(&cfg_info, "t38faxmaxdatagram", ACO_EXACT, general_options, NULL, removed_options_handler, 0); |
| 1451 |
|
| 1452 |
__ast_udptl_reload(0); |
| 1453 |
|
| 1454 |
ast_cli_register_multiple(cli_udptl, ARRAY_LEN(cli_udptl)); |
| 1455 |
|
| 1456 |
return AST_MODULE_LOAD_SUCCESS; |
| 1457 |
} |
| 1458 |
|
| 1459 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "UDPTL", |
| 1460 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 1461 |
.load = load_module, |
| 1462 |
.unload = unload_module, |
| 1463 |
.reload = reload_module, |
| 1464 |
.load_pri = AST_MODPRI_CORE, |
| 1465 |
.requires = "extconfig", |
| 1466 |
); |