udptl.c 40 KB

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