udptl.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * UDPTL support for T.38
  5. *
  6. * Copyright (C) 2005, Steve Underwood, partly based on RTP code which is
  7. * Copyright (C) 1999-2009, Digium, Inc.
  8. *
  9. * Steve Underwood <steveu@coppice.org>
  10. * Kevin P. Fleming <kpfleming@digium.com>
  11. *
  12. * See http://www.asterisk.org for more information about
  13. * the Asterisk project. Please do not directly contact
  14. * any of the maintainers of this project for assistance;
  15. * the project provides a web site, mailing lists and IRC
  16. * channels for your use.
  17. *
  18. * This program is free software, distributed under the terms of
  19. * the GNU General Public License Version 2. See the LICENSE file
  20. * at the top of the source tree.
  21. *
  22. * A license has been granted to Digium (via disclaimer) for the use of
  23. * this code.
  24. */
  25. /*!
  26. * \file
  27. *
  28. * \brief UDPTL support for T.38 faxing
  29. *
  30. *
  31. * \author Mark Spencer <markster@digium.com>
  32. * \author Steve Underwood <steveu@coppice.org>
  33. * \author Kevin P. Fleming <kpfleming@digium.com>
  34. *
  35. * \page T38fax_udptl T.38 support :: UDPTL
  36. *
  37. * Asterisk supports T.38 fax passthrough, origination and termination. It does
  38. * not support gateway operation. The only channel driver that supports T.38 at
  39. * this time is chan_sip.
  40. *
  41. * UDPTL is handled very much like RTP. It can be reinvited to go directly between
  42. * the endpoints, without involving Asterisk in the media stream.
  43. *
  44. * \b References:
  45. * - chan_sip.c
  46. * - udptl.c
  47. * - app_fax.c
  48. */
  49. /*! \li \ref udptl.c uses the configuration file \ref udptl.conf
  50. * \addtogroup configuration_file Configuration Files
  51. */
  52. /*!
  53. * \page udptl.conf udptl.conf
  54. * \verbinclude udptl.conf.sample
  55. */
  56. /*** MODULEINFO
  57. <support_level>core</support_level>
  58. ***/
  59. #include "asterisk.h"
  60. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  61. #include <sys/time.h>
  62. #include <signal.h>
  63. #include <fcntl.h>
  64. #include "asterisk/udptl.h"
  65. #include "asterisk/frame.h"
  66. #include "asterisk/channel.h"
  67. #include "asterisk/acl.h"
  68. #include "asterisk/config_options.h"
  69. #include "asterisk/lock.h"
  70. #include "asterisk/utils.h"
  71. #include "asterisk/netsock2.h"
  72. #include "asterisk/cli.h"
  73. #include "asterisk/unaligned.h"
  74. /*** DOCUMENTATION
  75. <configInfo name="udptl" language="en_US">
  76. <configFile name="udptl.conf">
  77. <configObject name="global">
  78. <synopsis>Global options for configuring UDPTL</synopsis>
  79. <configOption name="udptlstart">
  80. <synopsis>The start of the UDPTL port range</synopsis>
  81. </configOption>
  82. <configOption name="udptlend">
  83. <synopsis>The end of the UDPTL port range</synopsis>
  84. </configOption>
  85. <configOption name="udptlchecksums">
  86. <synopsis>Whether to enable or disable UDP checksums on UDPTL traffic</synopsis>
  87. </configOption>
  88. <configOption name="udptlfecentries">
  89. <synopsis>The number of error correction entries in a UDPTL packet</synopsis>
  90. </configOption>
  91. <configOption name="udptlfecspan">
  92. <synopsis>The span over which parity is calculated for FEC in a UDPTL packet</synopsis>
  93. </configOption>
  94. <configOption name="use_even_ports">
  95. <synopsis>Whether to only use even-numbered UDPTL ports</synopsis>
  96. </configOption>
  97. <configOption name="t38faxudpec">
  98. <synopsis>Removed</synopsis>
  99. </configOption>
  100. <configOption name="t38faxmaxdatagram">
  101. <synopsis>Removed</synopsis>
  102. </configOption>
  103. </configObject>
  104. </configFile>
  105. </configInfo>
  106. ***/
  107. #define UDPTL_MTU 1200
  108. #if !defined(FALSE)
  109. #define FALSE 0
  110. #endif
  111. #if !defined(TRUE)
  112. #define TRUE (!FALSE)
  113. #endif
  114. #define LOG_TAG(u) S_OR(u->tag, "no tag")
  115. #define DEFAULT_UDPTLSTART 4000
  116. #define DEFAULT_UDPTLEND 4999
  117. static int udptldebug; /*!< Are we debugging? */
  118. static struct ast_sockaddr udptldebugaddr; /*!< Debug packets to/from this host */
  119. #define LOCAL_FAX_MAX_DATAGRAM 1400
  120. #define DEFAULT_FAX_MAX_DATAGRAM 400
  121. #define FAX_MAX_DATAGRAM_LIMIT 1400
  122. #define MAX_FEC_ENTRIES 5
  123. #define MAX_FEC_SPAN 5
  124. #define UDPTL_BUF_MASK 15
  125. typedef struct {
  126. int buf_len;
  127. uint8_t buf[LOCAL_FAX_MAX_DATAGRAM];
  128. } udptl_fec_tx_buffer_t;
  129. typedef struct {
  130. int buf_len;
  131. uint8_t buf[LOCAL_FAX_MAX_DATAGRAM];
  132. unsigned int fec_len[MAX_FEC_ENTRIES];
  133. uint8_t fec[MAX_FEC_ENTRIES][LOCAL_FAX_MAX_DATAGRAM];
  134. unsigned int fec_span;
  135. unsigned int fec_entries;
  136. } udptl_fec_rx_buffer_t;
  137. /*! \brief Structure for an UDPTL session */
  138. struct ast_udptl {
  139. int fd;
  140. char resp;
  141. struct ast_frame f[16];
  142. unsigned char rawdata[8192 + AST_FRIENDLY_OFFSET];
  143. unsigned int lasteventseqn;
  144. int nat;
  145. int flags;
  146. struct ast_sockaddr us;
  147. struct ast_sockaddr them;
  148. int *ioid;
  149. struct ast_sched_context *sched;
  150. struct io_context *io;
  151. void *data;
  152. char *tag;
  153. ast_udptl_callback callback;
  154. /*! This option indicates the error correction scheme used in transmitted UDPTL
  155. * packets and expected in received UDPTL packets.
  156. */
  157. enum ast_t38_ec_modes error_correction_scheme;
  158. /*! This option indicates the number of error correction entries transmitted in
  159. * UDPTL packets and expected in received UDPTL packets.
  160. */
  161. unsigned int error_correction_entries;
  162. /*! This option indicates the span of the error correction entries in transmitted
  163. * UDPTL packets (FEC only).
  164. */
  165. unsigned int error_correction_span;
  166. /*! The maximum size UDPTL packet that can be accepted by
  167. * the remote device.
  168. */
  169. int far_max_datagram;
  170. /*! The maximum size UDPTL packet that we are prepared to
  171. * accept, or -1 if it hasn't been calculated since the last
  172. * changes were applied to the UDPTL structure.
  173. */
  174. int local_max_datagram;
  175. /*! The maximum IFP that can be submitted for sending
  176. * to the remote device. Calculated from far_max_datagram,
  177. * error_correction_scheme and error_correction_entries,
  178. * or -1 if it hasn't been calculated since the last
  179. * changes were applied to the UDPTL structure.
  180. */
  181. int far_max_ifp;
  182. /*! The maximum IFP that the local endpoint is prepared
  183. * to accept. Along with error_correction_scheme and
  184. * error_correction_entries, used to calculate local_max_datagram.
  185. */
  186. int local_max_ifp;
  187. unsigned int tx_seq_no;
  188. unsigned int rx_seq_no;
  189. udptl_fec_tx_buffer_t tx[UDPTL_BUF_MASK + 1];
  190. udptl_fec_rx_buffer_t rx[UDPTL_BUF_MASK + 1];
  191. };
  192. struct udptl_global_options {
  193. unsigned int start; /*< The UDPTL start port */
  194. unsigned int end; /*< The UDPTL end port */
  195. unsigned int fecentries;
  196. unsigned int fecspan;
  197. unsigned int nochecksums;
  198. unsigned int use_even_ports;
  199. };
  200. static AO2_GLOBAL_OBJ_STATIC(globals);
  201. struct udptl_config {
  202. struct udptl_global_options *general;
  203. };
  204. static void *udptl_snapshot_alloc(void);
  205. static int udptl_pre_apply_config(void);
  206. static struct aco_type general_option = {
  207. .type = ACO_GLOBAL,
  208. .name = "global",
  209. .category_match = ACO_WHITELIST,
  210. .item_offset = offsetof(struct udptl_config, general),
  211. .category = "^general$",
  212. };
  213. static struct aco_type *general_options[] = ACO_TYPES(&general_option);
  214. static struct aco_file udptl_conf = {
  215. .filename = "udptl.conf",
  216. .types = ACO_TYPES(&general_option),
  217. };
  218. CONFIG_INFO_CORE("udptl", cfg_info, globals, udptl_snapshot_alloc,
  219. .files = ACO_FILES(&udptl_conf),
  220. .pre_apply_config = udptl_pre_apply_config,
  221. );
  222. static inline int udptl_debug_test_addr(const struct ast_sockaddr *addr)
  223. {
  224. if (udptldebug == 0)
  225. return 0;
  226. if (ast_sockaddr_isnull(&udptldebugaddr)) {
  227. return 1;
  228. }
  229. if (ast_sockaddr_port(&udptldebugaddr)) {
  230. return !ast_sockaddr_cmp(&udptldebugaddr, addr);
  231. } else {
  232. return !ast_sockaddr_cmp_addr(&udptldebugaddr, addr);
  233. }
  234. }
  235. static int decode_length(uint8_t *buf, unsigned int limit, unsigned int *len, unsigned int *pvalue)
  236. {
  237. if (*len >= limit)
  238. return -1;
  239. if ((buf[*len] & 0x80) == 0) {
  240. *pvalue = buf[*len];
  241. (*len)++;
  242. return 0;
  243. }
  244. if ((buf[*len] & 0x40) == 0) {
  245. if (*len == limit - 1)
  246. return -1;
  247. *pvalue = (buf[*len] & 0x3F) << 8;
  248. (*len)++;
  249. *pvalue |= buf[*len];
  250. (*len)++;
  251. return 0;
  252. }
  253. *pvalue = (buf[*len] & 0x3F) << 14;
  254. (*len)++;
  255. /* We have a fragment. Currently we don't process fragments. */
  256. ast_debug(1, "UDPTL packet with length greater than 16K received, decoding will fail\n");
  257. return 1;
  258. }
  259. /*- End of function --------------------------------------------------------*/
  260. static int decode_open_type(uint8_t *buf, unsigned int limit, unsigned int *len, const uint8_t **p_object, unsigned int *p_num_octets)
  261. {
  262. unsigned int octet_cnt = 0;
  263. if (decode_length(buf, limit, len, &octet_cnt) != 0)
  264. return -1;
  265. if (octet_cnt > 0) {
  266. /* Make sure the buffer contains at least the number of bits requested */
  267. if ((*len + octet_cnt) > limit)
  268. return -1;
  269. *p_num_octets = octet_cnt;
  270. *p_object = &buf[*len];
  271. *len += octet_cnt;
  272. }
  273. return 0;
  274. }
  275. /*- End of function --------------------------------------------------------*/
  276. static unsigned int encode_length(uint8_t *buf, unsigned int *len, unsigned int value)
  277. {
  278. unsigned int multiplier;
  279. if (value < 0x80) {
  280. /* 1 octet */
  281. buf[*len] = value;
  282. (*len)++;
  283. return value;
  284. }
  285. if (value < 0x4000) {
  286. /* 2 octets */
  287. /* Set the first bit of the first octet */
  288. buf[*len] = ((0x8000 | value) >> 8) & 0xFF;
  289. (*len)++;
  290. buf[*len] = value & 0xFF;
  291. (*len)++;
  292. return value;
  293. }
  294. /* Fragmentation */
  295. multiplier = (value < 0x10000) ? (value >> 14) : 4;
  296. /* Set the first 2 bits of the octet */
  297. buf[*len] = 0xC0 | multiplier;
  298. (*len)++;
  299. return multiplier << 14;
  300. }
  301. /*- End of function --------------------------------------------------------*/
  302. static int encode_open_type(const struct ast_udptl *udptl, uint8_t *buf, unsigned int buflen,
  303. unsigned int *len, const uint8_t *data, unsigned int num_octets)
  304. {
  305. unsigned int enclen;
  306. unsigned int octet_idx;
  307. uint8_t zero_byte;
  308. /* If open type is of zero length, add a single zero byte (10.1) */
  309. if (num_octets == 0) {
  310. zero_byte = 0;
  311. data = &zero_byte;
  312. num_octets = 1;
  313. }
  314. /* Encode the open type */
  315. for (octet_idx = 0; ; num_octets -= enclen, octet_idx += enclen) {
  316. if ((enclen = encode_length(buf, len, num_octets)) < 0)
  317. return -1;
  318. if (enclen + *len > buflen) {
  319. ast_log(LOG_ERROR, "UDPTL (%s): Buffer overflow detected (%d + %d > %d)\n",
  320. LOG_TAG(udptl), enclen, *len, buflen);
  321. return -1;
  322. }
  323. if (enclen > 0) {
  324. memcpy(&buf[*len], &data[octet_idx], enclen);
  325. *len += enclen;
  326. }
  327. if (enclen >= num_octets)
  328. break;
  329. }
  330. return 0;
  331. }
  332. /*- End of function --------------------------------------------------------*/
  333. static int udptl_rx_packet(struct ast_udptl *s, uint8_t *buf, unsigned int len)
  334. {
  335. int stat1;
  336. int stat2;
  337. int i;
  338. unsigned int ptr; /* an index that keeps track of how much of the UDPTL packet has been processed */
  339. int seq_no;
  340. const uint8_t *ifp = NULL;
  341. const uint8_t *data = NULL;
  342. unsigned int ifp_len = 0;
  343. int repaired[16];
  344. const uint8_t *bufs[ARRAY_LEN(s->f) - 1];
  345. unsigned int lengths[ARRAY_LEN(s->f) - 1];
  346. int span;
  347. int entries;
  348. int ifp_no;
  349. ptr = 0;
  350. ifp_no = 0;
  351. memset(&s->f[0], 0, sizeof(s->f[0]));
  352. /* Decode seq_number */
  353. if (ptr + 2 > len)
  354. return -1;
  355. seq_no = (buf[0] << 8) | buf[1];
  356. ptr += 2;
  357. /* Break out the primary packet */
  358. if ((stat1 = decode_open_type(buf, len, &ptr, &ifp, &ifp_len)) != 0)
  359. return -1;
  360. /* Decode error_recovery */
  361. if (ptr + 1 > len)
  362. return -1;
  363. if ((buf[ptr++] & 0x80) == 0) {
  364. /* Secondary packet mode for error recovery */
  365. if (seq_no > s->rx_seq_no) {
  366. /* We received a later packet than we expected, so we need to check if we can fill in the gap from the
  367. secondary packets. */
  368. int total_count = 0;
  369. do {
  370. unsigned int count;
  371. if ((stat2 = decode_length(buf, len, &ptr, &count)) < 0)
  372. return -1;
  373. for (i = 0; i < count && total_count + i < ARRAY_LEN(bufs); i++) {
  374. if ((stat1 = decode_open_type(buf, len, &ptr, &bufs[total_count + i], &lengths[total_count + i])) != 0) {
  375. return -1;
  376. }
  377. /* valid secondaries can contain zero-length packets that should be ignored */
  378. if (!bufs[total_count + i] || !lengths[total_count + i]) {
  379. /* drop the count of items to process and reuse the buffers that were just set */
  380. i--;
  381. count--;
  382. }
  383. }
  384. total_count += i;
  385. }
  386. while (stat2 > 0 && total_count < ARRAY_LEN(bufs));
  387. /* Step through in reverse order, so we go oldest to newest */
  388. for (i = total_count; i > 0; i--) {
  389. if (seq_no - i >= s->rx_seq_no) {
  390. /* This one wasn't seen before */
  391. /* Decode the secondary IFP packet */
  392. ast_debug(3, "Recovering lost packet via secondary %d, len %d\n", seq_no - i, lengths[i - 1]);
  393. s->f[ifp_no].frametype = AST_FRAME_MODEM;
  394. s->f[ifp_no].subclass.integer = AST_MODEM_T38;
  395. s->f[ifp_no].mallocd = 0;
  396. s->f[ifp_no].seqno = seq_no - i;
  397. s->f[ifp_no].datalen = lengths[i - 1];
  398. s->f[ifp_no].data.ptr = (uint8_t *) bufs[i - 1];
  399. s->f[ifp_no].offset = 0;
  400. s->f[ifp_no].src = "UDPTL";
  401. if (ifp_no > 0)
  402. AST_LIST_NEXT(&s->f[ifp_no - 1], frame_list) = &s->f[ifp_no];
  403. AST_LIST_NEXT(&s->f[ifp_no], frame_list) = NULL;
  404. ifp_no++;
  405. }
  406. }
  407. }
  408. }
  409. else
  410. {
  411. int j;
  412. int l;
  413. int x;
  414. /* FEC mode for error recovery */
  415. /* Our buffers cannot tolerate overlength IFP packets in FEC mode */
  416. if (ifp_len > LOCAL_FAX_MAX_DATAGRAM)
  417. return -1;
  418. /* Update any missed slots in the buffer */
  419. for ( ; seq_no > s->rx_seq_no; s->rx_seq_no++) {
  420. x = s->rx_seq_no & UDPTL_BUF_MASK;
  421. s->rx[x].buf_len = -1;
  422. s->rx[x].fec_len[0] = 0;
  423. s->rx[x].fec_span = 0;
  424. s->rx[x].fec_entries = 0;
  425. }
  426. x = seq_no & UDPTL_BUF_MASK;
  427. memset(repaired, 0, sizeof(repaired));
  428. /* Save the new IFP packet */
  429. memcpy(s->rx[x].buf, ifp, ifp_len);
  430. s->rx[x].buf_len = ifp_len;
  431. repaired[x] = TRUE;
  432. /* Decode the FEC packets */
  433. /* The span is defined as an unconstrained integer, but will never be more
  434. than a small value. */
  435. if (ptr + 2 > len)
  436. return -1;
  437. if (buf[ptr++] != 1)
  438. return -1;
  439. span = buf[ptr++];
  440. s->rx[x].fec_span = span;
  441. /* The number of entries is defined as a length, but will only ever be a small
  442. value. Treat it as such. */
  443. if (ptr + 1 > len)
  444. return -1;
  445. entries = buf[ptr++];
  446. if (entries > MAX_FEC_ENTRIES) {
  447. return -1;
  448. }
  449. s->rx[x].fec_entries = entries;
  450. /* Decode the elements */
  451. for (i = 0; i < entries; i++) {
  452. if ((stat1 = decode_open_type(buf, len, &ptr, &data, &s->rx[x].fec_len[i])) != 0)
  453. return -1;
  454. if (s->rx[x].fec_len[i] > LOCAL_FAX_MAX_DATAGRAM)
  455. return -1;
  456. /* Save the new FEC data */
  457. memcpy(s->rx[x].fec[i], data, s->rx[x].fec_len[i]);
  458. #if 0
  459. fprintf(stderr, "FEC: ");
  460. for (j = 0; j < s->rx[x].fec_len[i]; j++)
  461. fprintf(stderr, "%02X ", data[j]);
  462. fprintf(stderr, "\n");
  463. #endif
  464. }
  465. /* See if we can reconstruct anything which is missing */
  466. /* TODO: this does not comprehensively hunt back and repair everything that is possible */
  467. for (l = x; l != ((x - (16 - span*entries)) & UDPTL_BUF_MASK); l = (l - 1) & UDPTL_BUF_MASK) {
  468. int m;
  469. if (s->rx[l].fec_len[0] <= 0)
  470. continue;
  471. for (m = 0; m < s->rx[l].fec_entries; m++) {
  472. int k;
  473. int which;
  474. int limit = (l + m) & UDPTL_BUF_MASK;
  475. 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) {
  476. if (s->rx[k].buf_len <= 0)
  477. which = (which == -1) ? k : -2;
  478. }
  479. if (which >= 0) {
  480. /* Repairable */
  481. for (j = 0; j < s->rx[l].fec_len[m]; j++) {
  482. s->rx[which].buf[j] = s->rx[l].fec[m][j];
  483. 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)
  484. s->rx[which].buf[j] ^= (s->rx[k].buf_len > j) ? s->rx[k].buf[j] : 0;
  485. }
  486. s->rx[which].buf_len = s->rx[l].fec_len[m];
  487. repaired[which] = TRUE;
  488. }
  489. }
  490. }
  491. /* Now play any new packets forwards in time */
  492. for (l = (x + 1) & UDPTL_BUF_MASK, j = seq_no - UDPTL_BUF_MASK; l != x; l = (l + 1) & UDPTL_BUF_MASK, j++) {
  493. if (repaired[l]) {
  494. //fprintf(stderr, "Fixed packet %d, len %d\n", j, l);
  495. s->f[ifp_no].frametype = AST_FRAME_MODEM;
  496. s->f[ifp_no].subclass.integer = AST_MODEM_T38;
  497. s->f[ifp_no].mallocd = 0;
  498. s->f[ifp_no].seqno = j;
  499. s->f[ifp_no].datalen = s->rx[l].buf_len;
  500. s->f[ifp_no].data.ptr = s->rx[l].buf;
  501. s->f[ifp_no].offset = 0;
  502. s->f[ifp_no].src = "UDPTL";
  503. if (ifp_no > 0)
  504. AST_LIST_NEXT(&s->f[ifp_no - 1], frame_list) = &s->f[ifp_no];
  505. AST_LIST_NEXT(&s->f[ifp_no], frame_list) = NULL;
  506. ifp_no++;
  507. }
  508. }
  509. }
  510. /* If packets are received out of sequence, we may have already processed this packet from the error
  511. recovery information in a packet already received. */
  512. if (seq_no >= s->rx_seq_no) {
  513. /* Decode the primary IFP packet */
  514. s->f[ifp_no].frametype = AST_FRAME_MODEM;
  515. s->f[ifp_no].subclass.integer = AST_MODEM_T38;
  516. s->f[ifp_no].mallocd = 0;
  517. s->f[ifp_no].seqno = seq_no;
  518. s->f[ifp_no].datalen = ifp_len;
  519. s->f[ifp_no].data.ptr = (uint8_t *) ifp;
  520. s->f[ifp_no].offset = 0;
  521. s->f[ifp_no].src = "UDPTL";
  522. if (ifp_no > 0)
  523. AST_LIST_NEXT(&s->f[ifp_no - 1], frame_list) = &s->f[ifp_no];
  524. AST_LIST_NEXT(&s->f[ifp_no], frame_list) = NULL;
  525. ifp_no++;
  526. }
  527. s->rx_seq_no = seq_no + 1;
  528. return ifp_no;
  529. }
  530. /*- End of function --------------------------------------------------------*/
  531. static int udptl_build_packet(struct ast_udptl *s, uint8_t *buf, unsigned int buflen, uint8_t *ifp, unsigned int ifp_len)
  532. {
  533. uint8_t fec[LOCAL_FAX_MAX_DATAGRAM * 2] = { 0, };
  534. int i;
  535. int j;
  536. int seq;
  537. int entry;
  538. int entries;
  539. int span;
  540. int m;
  541. unsigned int len;
  542. int limit;
  543. int high_tide;
  544. seq = s->tx_seq_no & 0xFFFF;
  545. /* Map the sequence number to an entry in the circular buffer */
  546. entry = seq & UDPTL_BUF_MASK;
  547. /* We save the message in a circular buffer, for generating FEC or
  548. redundancy sets later on. */
  549. s->tx[entry].buf_len = ifp_len;
  550. memcpy(s->tx[entry].buf, ifp, ifp_len);
  551. /* Build the UDPTLPacket */
  552. len = 0;
  553. /* Encode the sequence number */
  554. buf[len++] = (seq >> 8) & 0xFF;
  555. buf[len++] = seq & 0xFF;
  556. /* Encode the primary IFP packet */
  557. if (encode_open_type(s, buf, buflen, &len, ifp, ifp_len) < 0)
  558. return -1;
  559. /* Encode the appropriate type of error recovery information */
  560. switch (s->error_correction_scheme)
  561. {
  562. case UDPTL_ERROR_CORRECTION_NONE:
  563. /* Encode the error recovery type */
  564. buf[len++] = 0x00;
  565. /* The number of entries will always be zero, so it is pointless allowing
  566. for the fragmented case here. */
  567. if (encode_length(buf, &len, 0) < 0)
  568. return -1;
  569. break;
  570. case UDPTL_ERROR_CORRECTION_REDUNDANCY:
  571. /* Encode the error recovery type */
  572. buf[len++] = 0x00;
  573. if (s->tx_seq_no > s->error_correction_entries)
  574. entries = s->error_correction_entries;
  575. else
  576. entries = s->tx_seq_no;
  577. /* The number of entries will always be small, so it is pointless allowing
  578. for the fragmented case here. */
  579. if (encode_length(buf, &len, entries) < 0)
  580. return -1;
  581. /* Encode the elements */
  582. for (i = 0; i < entries; i++) {
  583. j = (entry - i - 1) & UDPTL_BUF_MASK;
  584. if (encode_open_type(s, buf, buflen, &len, s->tx[j].buf, s->tx[j].buf_len) < 0) {
  585. ast_debug(1, "UDPTL (%s): Encoding failed at i=%d, j=%d\n",
  586. LOG_TAG(s), i, j);
  587. return -1;
  588. }
  589. }
  590. break;
  591. case UDPTL_ERROR_CORRECTION_FEC:
  592. span = s->error_correction_span;
  593. entries = s->error_correction_entries;
  594. if (seq < s->error_correction_span*s->error_correction_entries) {
  595. /* In the initial stages, wind up the FEC smoothly */
  596. entries = seq/s->error_correction_span;
  597. if (seq < s->error_correction_span)
  598. span = 0;
  599. }
  600. /* Encode the error recovery type */
  601. buf[len++] = 0x80;
  602. /* Span is defined as an inconstrained integer, which it dumb. It will only
  603. ever be a small value. Treat it as such. */
  604. buf[len++] = 1;
  605. buf[len++] = span;
  606. /* The number of entries is defined as a length, but will only ever be a small
  607. value. Treat it as such. */
  608. buf[len++] = entries;
  609. for (m = 0; m < entries; m++) {
  610. /* Make an XOR'ed entry the maximum length */
  611. limit = (entry + m) & UDPTL_BUF_MASK;
  612. high_tide = 0;
  613. for (i = (limit - span*entries) & UDPTL_BUF_MASK; i != limit; i = (i + entries) & UDPTL_BUF_MASK) {
  614. if (high_tide < s->tx[i].buf_len) {
  615. for (j = 0; j < high_tide; j++)
  616. fec[j] ^= s->tx[i].buf[j];
  617. for ( ; j < s->tx[i].buf_len; j++)
  618. fec[j] = s->tx[i].buf[j];
  619. high_tide = s->tx[i].buf_len;
  620. } else {
  621. for (j = 0; j < s->tx[i].buf_len; j++)
  622. fec[j] ^= s->tx[i].buf[j];
  623. }
  624. }
  625. if (encode_open_type(s, buf, buflen, &len, fec, high_tide) < 0)
  626. return -1;
  627. }
  628. break;
  629. }
  630. s->tx_seq_no++;
  631. return len;
  632. }
  633. int ast_udptl_fd(const struct ast_udptl *udptl)
  634. {
  635. return udptl->fd;
  636. }
  637. void ast_udptl_set_data(struct ast_udptl *udptl, void *data)
  638. {
  639. udptl->data = data;
  640. }
  641. void ast_udptl_set_callback(struct ast_udptl *udptl, ast_udptl_callback callback)
  642. {
  643. udptl->callback = callback;
  644. }
  645. void ast_udptl_setnat(struct ast_udptl *udptl, int nat)
  646. {
  647. udptl->nat = nat;
  648. }
  649. static int udptlread(int *id, int fd, short events, void *cbdata)
  650. {
  651. struct ast_udptl *udptl = cbdata;
  652. struct ast_frame *f;
  653. if ((f = ast_udptl_read(udptl))) {
  654. if (udptl->callback)
  655. udptl->callback(udptl, f, udptl->data);
  656. }
  657. return 1;
  658. }
  659. struct ast_frame *ast_udptl_read(struct ast_udptl *udptl)
  660. {
  661. int res;
  662. struct ast_sockaddr addr;
  663. uint8_t *buf;
  664. buf = udptl->rawdata + AST_FRIENDLY_OFFSET;
  665. /* Cache where the header will go */
  666. res = ast_recvfrom(udptl->fd,
  667. buf,
  668. sizeof(udptl->rawdata) - AST_FRIENDLY_OFFSET,
  669. 0,
  670. &addr);
  671. if (res < 0) {
  672. if (errno != EAGAIN)
  673. ast_log(LOG_WARNING, "UDPTL (%s): read error: %s\n",
  674. LOG_TAG(udptl), strerror(errno));
  675. ast_assert(errno != EBADF);
  676. return &ast_null_frame;
  677. }
  678. /* Ignore if the other side hasn't been given an address yet. */
  679. if (ast_sockaddr_isnull(&udptl->them)) {
  680. return &ast_null_frame;
  681. }
  682. if (udptl->nat) {
  683. /* Send to whoever sent to us */
  684. if (ast_sockaddr_cmp(&udptl->them, &addr)) {
  685. ast_sockaddr_copy(&udptl->them, &addr);
  686. ast_debug(1, "UDPTL (%s): NAT, Using address %s\n",
  687. LOG_TAG(udptl), ast_sockaddr_stringify(&udptl->them));
  688. }
  689. }
  690. if (udptl_debug_test_addr(&addr)) {
  691. int seq_no;
  692. /* Decode sequence number just for verbose message. */
  693. if (res < 2) {
  694. /* Short packet. */
  695. seq_no = -1;
  696. } else {
  697. seq_no = (buf[0] << 8) | buf[1];
  698. }
  699. ast_verb(1, "UDPTL (%s): packet from %s (seq %d, len %d)\n",
  700. LOG_TAG(udptl), ast_sockaddr_stringify(&addr), seq_no, res);
  701. }
  702. if (udptl_rx_packet(udptl, buf, res) < 1) {
  703. return &ast_null_frame;
  704. }
  705. return &udptl->f[0];
  706. }
  707. static void calculate_local_max_datagram(struct ast_udptl *udptl)
  708. {
  709. unsigned int new_max = 0;
  710. if (udptl->local_max_ifp == -1) {
  711. ast_log(LOG_WARNING, "UDPTL (%s): Cannot calculate local_max_datagram before local_max_ifp has been set.\n",
  712. LOG_TAG(udptl));
  713. udptl->local_max_datagram = -1;
  714. return;
  715. }
  716. /* calculate the amount of space required to receive an IFP
  717. * of the maximum size supported by the application/endpoint
  718. * that we are delivering them to (local endpoint), and add
  719. * the amount of space required to support the selected
  720. * error correction mode
  721. */
  722. switch (udptl->error_correction_scheme) {
  723. case UDPTL_ERROR_CORRECTION_NONE:
  724. /* need room for sequence number, length indicator, redundancy
  725. * indicator and following length indicator
  726. */
  727. new_max = 5 + udptl->local_max_ifp;
  728. break;
  729. case UDPTL_ERROR_CORRECTION_REDUNDANCY:
  730. /* need room for sequence number, length indicators, plus
  731. * room for up to 3 redundancy packets
  732. */
  733. new_max = 5 + udptl->local_max_ifp + 2 + (3 * udptl->local_max_ifp);
  734. break;
  735. case UDPTL_ERROR_CORRECTION_FEC:
  736. /* need room for sequence number, length indicators and a
  737. * a single IFP of the maximum size expected
  738. */
  739. new_max = 5 + udptl->local_max_ifp + 4 + udptl->local_max_ifp;
  740. break;
  741. }
  742. /* add 5% extra space for insurance, but no larger than LOCAL_FAX_MAX_DATAGRAM */
  743. udptl->local_max_datagram = MIN(new_max * 1.05, LOCAL_FAX_MAX_DATAGRAM);
  744. }
  745. static void calculate_far_max_ifp(struct ast_udptl *udptl)
  746. {
  747. unsigned new_max = 0;
  748. if (udptl->far_max_datagram == -1) {
  749. ast_log(LOG_WARNING, "UDPTL (%s): Cannot calculate far_max_ifp before far_max_datagram has been set.\n",
  750. LOG_TAG(udptl));
  751. udptl->far_max_ifp = -1;
  752. return;
  753. }
  754. /* the goal here is to supply the local endpoint (application
  755. * or bridged channel) a maximum IFP value that will allow it
  756. * to effectively and efficiently transfer image data at its
  757. * selected bit rate, taking into account the selected error
  758. * correction mode, but without overrunning the far endpoint's
  759. * datagram buffer. this is complicated by the fact that some
  760. * far endpoints send us bogus (small) max datagram values,
  761. * which would result in either buffer overrun or no error
  762. * correction. we try to accomodate those, but if the supplied
  763. * value is too small to do so, we'll emit warning messages and
  764. * the user will have to use configuration options to override
  765. * the max datagram value supplied by the far endpoint.
  766. */
  767. switch (udptl->error_correction_scheme) {
  768. case UDPTL_ERROR_CORRECTION_NONE:
  769. /* need room for sequence number, length indicator, redundancy
  770. * indicator and following length indicator
  771. */
  772. new_max = udptl->far_max_datagram - 5;
  773. break;
  774. case UDPTL_ERROR_CORRECTION_REDUNDANCY:
  775. /* for this case, we'd like to send as many error correction entries
  776. * as possible (up to the number we're configured for), but we'll settle
  777. * for sending fewer if the configured number would cause the
  778. * calculated max IFP to be too small for effective operation
  779. *
  780. * need room for sequence number, length indicators and the
  781. * configured number of redundant packets
  782. *
  783. * note: we purposely don't allow error_correction_entries to drop to
  784. * zero in this loop; we'd rather send smaller IFPs (and thus reduce
  785. * the image data transfer rate) than sacrifice redundancy completely
  786. */
  787. for (;;) {
  788. new_max = (udptl->far_max_datagram - 8) / (udptl->error_correction_entries + 1);
  789. if ((new_max < 80) && (udptl->error_correction_entries > 1)) {
  790. /* the max ifp is not large enough, subtract an
  791. * error correction entry and calculate again
  792. * */
  793. --udptl->error_correction_entries;
  794. } else {
  795. break;
  796. }
  797. }
  798. break;
  799. case UDPTL_ERROR_CORRECTION_FEC:
  800. /* need room for sequence number, length indicators and a
  801. * a single IFP of the maximum size expected
  802. */
  803. new_max = (udptl->far_max_datagram - 10) / 2;
  804. break;
  805. }
  806. /* subtract 5% of space for insurance */
  807. udptl->far_max_ifp = new_max * 0.95;
  808. }
  809. enum ast_t38_ec_modes ast_udptl_get_error_correction_scheme(const struct ast_udptl *udptl)
  810. {
  811. return udptl->error_correction_scheme;
  812. }
  813. void ast_udptl_set_error_correction_scheme(struct ast_udptl *udptl, enum ast_t38_ec_modes ec)
  814. {
  815. udptl->error_correction_scheme = ec;
  816. switch (ec) {
  817. case UDPTL_ERROR_CORRECTION_FEC:
  818. udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_FEC;
  819. if (udptl->error_correction_entries == 0) {
  820. udptl->error_correction_entries = 3;
  821. }
  822. if (udptl->error_correction_span == 0) {
  823. udptl->error_correction_span = 3;
  824. }
  825. break;
  826. case UDPTL_ERROR_CORRECTION_REDUNDANCY:
  827. udptl->error_correction_scheme = UDPTL_ERROR_CORRECTION_REDUNDANCY;
  828. if (udptl->error_correction_entries == 0) {
  829. udptl->error_correction_entries = 3;
  830. }
  831. break;
  832. default:
  833. /* nothing to do */
  834. break;
  835. };
  836. /* reset calculated values so they'll be computed again */
  837. udptl->local_max_datagram = -1;
  838. udptl->far_max_ifp = -1;
  839. }
  840. void ast_udptl_set_local_max_ifp(struct ast_udptl *udptl, unsigned int max_ifp)
  841. {
  842. /* make sure max_ifp is a positive value since a cast will take place when
  843. * when setting local_max_ifp */
  844. if ((signed int) max_ifp > 0) {
  845. udptl->local_max_ifp = max_ifp;
  846. /* reset calculated values so they'll be computed again */
  847. udptl->local_max_datagram = -1;
  848. }
  849. }
  850. unsigned int ast_udptl_get_local_max_datagram(struct ast_udptl *udptl)
  851. {
  852. if (udptl->local_max_datagram == -1) {
  853. calculate_local_max_datagram(udptl);
  854. }
  855. /* this function expects a unsigned value in return. */
  856. if (udptl->local_max_datagram < 0) {
  857. return 0;
  858. }
  859. return udptl->local_max_datagram;
  860. }
  861. void ast_udptl_set_far_max_datagram(struct ast_udptl *udptl, unsigned int max_datagram)
  862. {
  863. if (!max_datagram || (max_datagram > FAX_MAX_DATAGRAM_LIMIT)) {
  864. udptl->far_max_datagram = DEFAULT_FAX_MAX_DATAGRAM;
  865. } else {
  866. udptl->far_max_datagram = max_datagram;
  867. }
  868. /* reset calculated values so they'll be computed again */
  869. udptl->far_max_ifp = -1;
  870. }
  871. unsigned int ast_udptl_get_far_max_datagram(const struct ast_udptl *udptl)
  872. {
  873. if (udptl->far_max_datagram < 0) {
  874. return 0;
  875. }
  876. return udptl->far_max_datagram;
  877. }
  878. unsigned int ast_udptl_get_far_max_ifp(struct ast_udptl *udptl)
  879. {
  880. if (udptl->far_max_ifp == -1) {
  881. calculate_far_max_ifp(udptl);
  882. }
  883. if (udptl->far_max_ifp < 0) {
  884. return 0;
  885. }
  886. return udptl->far_max_ifp;
  887. }
  888. struct ast_udptl *ast_udptl_new_with_bindaddr(struct ast_sched_context *sched, struct io_context *io, int callbackmode, struct ast_sockaddr *addr)
  889. {
  890. struct ast_udptl *udptl;
  891. int x;
  892. int startplace;
  893. int i;
  894. long int flags;
  895. RAII_VAR(struct udptl_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  896. if (!cfg || !cfg->general) {
  897. ast_log(LOG_ERROR, "Could not access global udptl options!\n");
  898. return NULL;
  899. }
  900. if (!(udptl = ast_calloc(1, sizeof(*udptl)))) {
  901. return NULL;
  902. }
  903. udptl->error_correction_span = cfg->general->fecspan;
  904. udptl->error_correction_entries = cfg->general->fecentries;
  905. udptl->far_max_datagram = -1;
  906. udptl->far_max_ifp = -1;
  907. udptl->local_max_ifp = -1;
  908. udptl->local_max_datagram = -1;
  909. for (i = 0; i <= UDPTL_BUF_MASK; i++) {
  910. udptl->rx[i].buf_len = -1;
  911. udptl->tx[i].buf_len = -1;
  912. }
  913. if ((udptl->fd = socket(ast_sockaddr_is_ipv6(addr) ?
  914. AF_INET6 : AF_INET, SOCK_DGRAM, 0)) < 0) {
  915. ast_free(udptl);
  916. ast_log(LOG_WARNING, "Unable to allocate socket: %s\n", strerror(errno));
  917. return NULL;
  918. }
  919. flags = fcntl(udptl->fd, F_GETFL);
  920. fcntl(udptl->fd, F_SETFL, flags | O_NONBLOCK);
  921. #ifdef SO_NO_CHECK
  922. if (cfg->general->nochecksums)
  923. setsockopt(udptl->fd, SOL_SOCKET, SO_NO_CHECK, &cfg->general->nochecksums, sizeof(cfg->general->nochecksums));
  924. #endif
  925. /* Find us a place */
  926. x = (cfg->general->start == cfg->general->end) ? cfg->general->start : (ast_random() % (cfg->general->end - cfg->general->start)) + cfg->general->start;
  927. if (cfg->general->use_even_ports && (x & 1)) {
  928. ++x;
  929. }
  930. startplace = x;
  931. for (;;) {
  932. ast_sockaddr_copy(&udptl->us, addr);
  933. ast_sockaddr_set_port(&udptl->us, x);
  934. if (ast_bind(udptl->fd, &udptl->us) == 0) {
  935. break;
  936. }
  937. if (errno != EADDRINUSE && errno != EACCES) {
  938. ast_log(LOG_WARNING, "Unexpected bind error: %s\n", strerror(errno));
  939. close(udptl->fd);
  940. ast_free(udptl);
  941. return NULL;
  942. }
  943. if (cfg->general->use_even_ports) {
  944. x += 2;
  945. } else {
  946. ++x;
  947. }
  948. if (x > cfg->general->end)
  949. x = cfg->general->start;
  950. if (x == startplace) {
  951. ast_log(LOG_WARNING, "No UDPTL ports remaining\n");
  952. close(udptl->fd);
  953. ast_free(udptl);
  954. return NULL;
  955. }
  956. }
  957. if (io && sched && callbackmode) {
  958. /* Operate this one in a callback mode */
  959. udptl->sched = sched;
  960. udptl->io = io;
  961. udptl->ioid = ast_io_add(udptl->io, udptl->fd, udptlread, AST_IO_IN, udptl);
  962. }
  963. return udptl;
  964. }
  965. void ast_udptl_set_tag(struct ast_udptl *udptl, const char *format, ...)
  966. {
  967. va_list ap;
  968. ast_free(udptl->tag);
  969. udptl->tag = NULL;
  970. va_start(ap, format);
  971. if (ast_vasprintf(&udptl->tag, format, ap) == -1) {
  972. udptl->tag = NULL;
  973. }
  974. va_end(ap);
  975. }
  976. int ast_udptl_setqos(struct ast_udptl *udptl, unsigned int tos, unsigned int cos)
  977. {
  978. return ast_set_qos(udptl->fd, tos, cos, "UDPTL");
  979. }
  980. void ast_udptl_set_peer(struct ast_udptl *udptl, const struct ast_sockaddr *them)
  981. {
  982. ast_sockaddr_copy(&udptl->them, them);
  983. }
  984. void ast_udptl_get_peer(const struct ast_udptl *udptl, struct ast_sockaddr *them)
  985. {
  986. ast_sockaddr_copy(them, &udptl->them);
  987. }
  988. void ast_udptl_get_us(const struct ast_udptl *udptl, struct ast_sockaddr *us)
  989. {
  990. ast_sockaddr_copy(us, &udptl->us);
  991. }
  992. void ast_udptl_stop(struct ast_udptl *udptl)
  993. {
  994. ast_sockaddr_setnull(&udptl->them);
  995. }
  996. void ast_udptl_destroy(struct ast_udptl *udptl)
  997. {
  998. if (udptl->ioid)
  999. ast_io_remove(udptl->io, udptl->ioid);
  1000. if (udptl->fd > -1)
  1001. close(udptl->fd);
  1002. if (udptl->tag)
  1003. ast_free(udptl->tag);
  1004. ast_free(udptl);
  1005. }
  1006. int ast_udptl_write(struct ast_udptl *s, struct ast_frame *f)
  1007. {
  1008. unsigned int seq;
  1009. unsigned int len = f->datalen;
  1010. /* if no max datagram size is provided, use default value */
  1011. const int bufsize = (s->far_max_datagram > 0) ? s->far_max_datagram : DEFAULT_FAX_MAX_DATAGRAM;
  1012. uint8_t buf[bufsize];
  1013. memset(buf, 0, sizeof(buf));
  1014. /* If we have no peer, return immediately */
  1015. if (ast_sockaddr_isnull(&s->them)) {
  1016. return 0;
  1017. }
  1018. /* If there is no data length, return immediately */
  1019. if (f->datalen == 0)
  1020. return 0;
  1021. if ((f->frametype != AST_FRAME_MODEM) ||
  1022. (f->subclass.integer != AST_MODEM_T38)) {
  1023. ast_log(LOG_WARNING, "UDPTL (%s): UDPTL can only send T.38 data.\n",
  1024. LOG_TAG(s));
  1025. return -1;
  1026. }
  1027. if (len > s->far_max_ifp) {
  1028. ast_log(LOG_WARNING,
  1029. "UDPTL (%s): UDPTL asked to send %d bytes of IFP when far end only prepared to accept %d bytes; data loss will occur."
  1030. "You may need to override the T38FaxMaxDatagram value for this endpoint in the channel driver configuration.\n",
  1031. LOG_TAG(s), len, s->far_max_ifp);
  1032. len = s->far_max_ifp;
  1033. }
  1034. /* Save seq_no for debug output because udptl_build_packet increments it */
  1035. seq = s->tx_seq_no & 0xFFFF;
  1036. /* Cook up the UDPTL packet, with the relevant EC info. */
  1037. len = udptl_build_packet(s, buf, sizeof(buf), f->data.ptr, len);
  1038. if ((signed int) len > 0 && !ast_sockaddr_isnull(&s->them)) {
  1039. if (ast_sendto(s->fd, buf, len, 0, &s->them) < 0) {
  1040. ast_log(LOG_NOTICE, "UDPTL (%s): Transmission error to %s: %s\n",
  1041. LOG_TAG(s), ast_sockaddr_stringify(&s->them), strerror(errno));
  1042. }
  1043. if (udptl_debug_test_addr(&s->them)) {
  1044. ast_verb(1, "UDPTL (%s): packet to %s (seq %d, len %d)\n",
  1045. LOG_TAG(s), ast_sockaddr_stringify(&s->them), seq, len);
  1046. }
  1047. }
  1048. return 0;
  1049. }
  1050. static char *handle_cli_udptl_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  1051. {
  1052. switch (cmd) {
  1053. case CLI_INIT:
  1054. e->command = "udptl set debug {on|off|ip}";
  1055. e->usage =
  1056. "Usage: udptl set debug {on|off|ip host[:port]}\n"
  1057. " Enable or disable dumping of UDPTL packets.\n"
  1058. " If ip is specified, limit the dumped packets to those to and from\n"
  1059. " the specified 'host' with optional port.\n";
  1060. return NULL;
  1061. case CLI_GENERATE:
  1062. return NULL;
  1063. }
  1064. if (a->argc < 4 || a->argc > 5)
  1065. return CLI_SHOWUSAGE;
  1066. if (a->argc == 4) {
  1067. if (!strncasecmp(a->argv[3], "on", 2)) {
  1068. udptldebug = 1;
  1069. memset(&udptldebugaddr, 0, sizeof(udptldebugaddr));
  1070. ast_cli(a->fd, "UDPTL Debugging Enabled\n");
  1071. } else if (!strncasecmp(a->argv[3], "off", 3)) {
  1072. udptldebug = 0;
  1073. ast_cli(a->fd, "UDPTL Debugging Disabled\n");
  1074. } else {
  1075. return CLI_SHOWUSAGE;
  1076. }
  1077. } else {
  1078. struct ast_sockaddr *addrs;
  1079. if (strncasecmp(a->argv[3], "ip", 2))
  1080. return CLI_SHOWUSAGE;
  1081. if (!ast_sockaddr_resolve(&addrs, a->argv[4], 0, 0)) {
  1082. return CLI_SHOWUSAGE;
  1083. }
  1084. ast_sockaddr_copy(&udptldebugaddr, &addrs[0]);
  1085. ast_cli(a->fd, "UDPTL Debugging Enabled for IP: %s\n", ast_sockaddr_stringify(&udptldebugaddr));
  1086. udptldebug = 1;
  1087. ast_free(addrs);
  1088. }
  1089. return CLI_SUCCESS;
  1090. }
  1091. static char *handle_cli_show_config(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  1092. {
  1093. RAII_VAR(struct udptl_config *, cfg, NULL, ao2_cleanup);
  1094. switch (cmd) {
  1095. case CLI_INIT:
  1096. e->command = "udptl show config";
  1097. e->usage =
  1098. "Usage: udptl show config\n"
  1099. " Display UDPTL configuration options\n";
  1100. return NULL;
  1101. case CLI_GENERATE:
  1102. return NULL;
  1103. }
  1104. if (!(cfg = ao2_global_obj_ref(globals))) {
  1105. return CLI_FAILURE;
  1106. }
  1107. ast_cli(a->fd, "UDPTL Global options\n");
  1108. ast_cli(a->fd, "--------------------\n");
  1109. ast_cli(a->fd, "udptlstart: %u\n", cfg->general->start);
  1110. ast_cli(a->fd, "udptlend: %u\n", cfg->general->end);
  1111. ast_cli(a->fd, "udptlfecentries: %u\n", cfg->general->fecentries);
  1112. ast_cli(a->fd, "udptlfecspan: %u\n", cfg->general->fecspan);
  1113. ast_cli(a->fd, "use_even_ports: %s\n", AST_CLI_YESNO(cfg->general->use_even_ports));
  1114. ast_cli(a->fd, "udptlchecksums: %s\n", AST_CLI_YESNO(!cfg->general->nochecksums));
  1115. return CLI_SUCCESS;
  1116. }
  1117. static struct ast_cli_entry cli_udptl[] = {
  1118. AST_CLI_DEFINE(handle_cli_udptl_set_debug, "Enable/Disable UDPTL debugging"),
  1119. AST_CLI_DEFINE(handle_cli_show_config, "Show UDPTL config options"),
  1120. };
  1121. static void udptl_config_destructor(void *obj)
  1122. {
  1123. struct udptl_config *cfg = obj;
  1124. ao2_cleanup(cfg->general);
  1125. }
  1126. static void *udptl_snapshot_alloc(void)
  1127. {
  1128. struct udptl_config *cfg;
  1129. if (!(cfg = ao2_alloc(sizeof(*cfg), udptl_config_destructor))) {
  1130. return NULL;
  1131. }
  1132. if (!(cfg->general = ao2_alloc(sizeof(*cfg->general), NULL))) {
  1133. ao2_ref(cfg, -1);
  1134. return NULL;
  1135. }
  1136. return cfg;
  1137. }
  1138. static int removed_options_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  1139. {
  1140. if (!strcasecmp(var->name, "t38faxudpec")) {
  1141. ast_log(LOG_WARNING, "t38faxudpec in udptl.conf is no longer supported; use the t38pt_udptl configuration option in sip.conf instead.\n");
  1142. } else if (!strcasecmp(var->name, "t38faxmaxdatagram")) {
  1143. ast_log(LOG_WARNING, "t38faxmaxdatagram in udptl.conf is no longer supported; value is now supplied by T.38 applications.\n");
  1144. }
  1145. return 0;
  1146. }
  1147. static void __ast_udptl_reload(int reload)
  1148. {
  1149. if (aco_process_config(&cfg_info, reload) == ACO_PROCESS_ERROR) {
  1150. if (!reload) {
  1151. RAII_VAR(struct udptl_config *, udptl_cfg, udptl_snapshot_alloc(), ao2_cleanup);
  1152. if (aco_set_defaults(&general_option, "general", udptl_cfg->general)) {
  1153. ast_log(LOG_ERROR, "Failed to load udptl.conf and failed to initialize defaults.\n");
  1154. return;
  1155. }
  1156. ast_log(LOG_NOTICE, "Could not load udptl config; using defaults\n");
  1157. ao2_global_obj_replace_unref(globals, udptl_cfg);
  1158. }
  1159. }
  1160. }
  1161. static int udptl_pre_apply_config(void) {
  1162. struct udptl_config *cfg = aco_pending_config(&cfg_info);
  1163. if (!cfg->general) {
  1164. return -1;
  1165. }
  1166. #ifndef SO_NO_CHECK
  1167. if (cfg->general->nochecksums) {
  1168. ast_log(LOG_WARNING, "Disabling UDPTL checksums is not supported on this operating system!\n");
  1169. cfg->general->nochecksums = 0;
  1170. }
  1171. #endif
  1172. /* Fix up any global config values that we can handle before replacing the config */
  1173. if (cfg->general->use_even_ports && (cfg->general->start & 1)) {
  1174. ++cfg->general->start;
  1175. ast_log(LOG_NOTICE, "Odd numbered udptlstart specified but use_even_ports enabled. udptlstart is now %d\n", cfg->general->start);
  1176. }
  1177. if (cfg->general->start > cfg->general->end) {
  1178. ast_log(LOG_WARNING, "Unreasonable values for UDPTL start/end ports; defaulting to %s-%s.\n", __stringify(DEFAULT_UDPTLSTART), __stringify(DEFAULT_UDPTLEND));
  1179. cfg->general->start = DEFAULT_UDPTLSTART;
  1180. cfg->general->end = DEFAULT_UDPTLEND;
  1181. }
  1182. if (cfg->general->use_even_ports && (cfg->general->end & 1)) {
  1183. --cfg->general->end;
  1184. ast_log(LOG_NOTICE, "Odd numbered udptlend specified but use_even_ports enabled. udptlend is now %d\n", cfg->general->end);
  1185. }
  1186. return 0;
  1187. }
  1188. int ast_udptl_reload(void)
  1189. {
  1190. __ast_udptl_reload(1);
  1191. return 0;
  1192. }
  1193. /*!
  1194. * \internal
  1195. * \brief Clean up resources on Asterisk shutdown
  1196. */
  1197. static void udptl_shutdown(void)
  1198. {
  1199. ast_cli_unregister_multiple(cli_udptl, ARRAY_LEN(cli_udptl));
  1200. ao2_t_global_obj_release(globals, "Unref udptl global container in shutdown");
  1201. aco_info_destroy(&cfg_info);
  1202. }
  1203. void ast_udptl_init(void)
  1204. {
  1205. if (aco_info_init(&cfg_info)) {
  1206. return;
  1207. }
  1208. aco_option_register(&cfg_info, "udptlstart", ACO_EXACT, general_options, __stringify(DEFAULT_UDPTLSTART),
  1209. OPT_UINT_T, PARSE_IN_RANGE | PARSE_DEFAULT,
  1210. FLDSET(struct udptl_global_options, start), DEFAULT_UDPTLSTART, 1024, 65535);
  1211. aco_option_register(&cfg_info, "udptlend", ACO_EXACT, general_options, __stringify(DEFAULT_UDPTLEND),
  1212. OPT_UINT_T, PARSE_IN_RANGE | PARSE_DEFAULT,
  1213. FLDSET(struct udptl_global_options, end), DEFAULT_UDPTLEND, 1024, 65535);
  1214. aco_option_register(&cfg_info, "udptlfecentries", ACO_EXACT, general_options, NULL,
  1215. OPT_UINT_T, PARSE_IN_RANGE | PARSE_RANGE_DEFAULTS,
  1216. FLDSET(struct udptl_global_options, fecentries), 1, MAX_FEC_ENTRIES);
  1217. aco_option_register(&cfg_info, "udptlfecspan", ACO_EXACT, general_options, NULL,
  1218. OPT_UINT_T, PARSE_IN_RANGE | PARSE_RANGE_DEFAULTS,
  1219. FLDSET(struct udptl_global_options, fecspan), 1, MAX_FEC_SPAN);
  1220. aco_option_register(&cfg_info, "udptlchecksums", ACO_EXACT, general_options, "yes",
  1221. OPT_BOOL_T, 0, FLDSET(struct udptl_global_options, nochecksums));
  1222. aco_option_register(&cfg_info, "use_even_ports", ACO_EXACT, general_options, "no",
  1223. OPT_BOOL_T, 1, FLDSET(struct udptl_global_options, use_even_ports));
  1224. aco_option_register_custom(&cfg_info, "t38faxudpec", ACO_EXACT, general_options, NULL, removed_options_handler, 0);
  1225. aco_option_register_custom(&cfg_info, "t38faxmaxdatagram", ACO_EXACT, general_options, NULL, removed_options_handler, 0);
  1226. __ast_udptl_reload(0);
  1227. ast_cli_register_multiple(cli_udptl, ARRAY_LEN(cli_udptl));
  1228. ast_register_atexit(udptl_shutdown);
  1229. }