udptl.c 40 KB

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