udptl.c 41 KB

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