res_pjsip_t38.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \author Joshua Colp <jcolp@digium.com>
  21. *
  22. * \brief SIP T.38 handling
  23. */
  24. /*** MODULEINFO
  25. <depend>pjproject</depend>
  26. <depend>res_pjsip</depend>
  27. <depend>res_pjsip_session</depend>
  28. <support_level>core</support_level>
  29. ***/
  30. #include "asterisk.h"
  31. #include <pjsip.h>
  32. #include <pjsip_ua.h>
  33. #include <pjmedia.h>
  34. #include <pjlib.h>
  35. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  36. #include "asterisk/module.h"
  37. #include "asterisk/udptl.h"
  38. #include "asterisk/netsock2.h"
  39. #include "asterisk/channel.h"
  40. #include "asterisk/acl.h"
  41. #include "asterisk/res_pjsip.h"
  42. #include "asterisk/res_pjsip_session.h"
  43. /*! \brief The number of seconds after receiving a T.38 re-invite before automatically rejecting it */
  44. #define T38_AUTOMATIC_REJECTION_SECONDS 5
  45. /*! \brief Address for IPv4 UDPTL */
  46. static struct ast_sockaddr address_ipv4;
  47. /*! \brief Address for IPv6 UDPTL */
  48. static struct ast_sockaddr address_ipv6;
  49. /*! \brief T.38 state information */
  50. struct t38_state {
  51. /*! \brief Current state */
  52. enum ast_sip_session_t38state state;
  53. /*! \brief Our T.38 parameters */
  54. struct ast_control_t38_parameters our_parms;
  55. /*! \brief Their T.38 parameters */
  56. struct ast_control_t38_parameters their_parms;
  57. /*! \brief Timer entry for automatically rejecting an inbound re-invite */
  58. pj_timer_entry timer;
  59. };
  60. /*! \brief Destructor for T.38 state information */
  61. static void t38_state_destroy(void *obj)
  62. {
  63. ast_free(obj);
  64. }
  65. /*! \brief Datastore for attaching T.38 state information */
  66. static const struct ast_datastore_info t38_datastore = {
  67. .type = "t38",
  68. .destroy = t38_state_destroy,
  69. };
  70. /*! \brief Structure for T.38 parameters task data */
  71. struct t38_parameters_task_data {
  72. /*! \brief Session itself */
  73. struct ast_sip_session *session;
  74. /*! \brief T.38 control frame */
  75. struct ast_frame *frame;
  76. };
  77. /*! \brief Destructor for T.38 data */
  78. static void t38_parameters_task_data_destroy(void *obj)
  79. {
  80. struct t38_parameters_task_data *data = obj;
  81. ao2_cleanup(data->session);
  82. if (data->frame) {
  83. ast_frfree(data->frame);
  84. }
  85. }
  86. /*! \brief Allocator for T.38 data */
  87. static struct t38_parameters_task_data *t38_parameters_task_data_alloc(struct ast_sip_session *session,
  88. struct ast_frame *frame)
  89. {
  90. struct t38_parameters_task_data *data = ao2_alloc(sizeof(*data), t38_parameters_task_data_destroy);
  91. if (!data) {
  92. return NULL;
  93. }
  94. data->session = session;
  95. ao2_ref(session, +1);
  96. data->frame = frame;
  97. return data;
  98. }
  99. /*! \brief Helper function for changing the T.38 state */
  100. static void t38_change_state(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
  101. struct t38_state *state, enum ast_sip_session_t38state new_state)
  102. {
  103. enum ast_sip_session_t38state old_state = session->t38state;
  104. struct ast_control_t38_parameters parameters = { .request_response = 0, };
  105. pj_time_val delay = { .sec = T38_AUTOMATIC_REJECTION_SECONDS };
  106. if (old_state == new_state) {
  107. return;
  108. }
  109. session->t38state = new_state;
  110. ast_debug(2, "T.38 state changed to '%u' from '%u' on channel '%s'\n", new_state, old_state, ast_channel_name(session->channel));
  111. if (pj_timer_heap_cancel(pjsip_endpt_get_timer_heap(ast_sip_get_pjsip_endpoint()), &state->timer)) {
  112. ast_debug(2, "Automatic T.38 rejection on channel '%s' terminated\n", ast_channel_name(session->channel));
  113. ao2_ref(session, -1);
  114. }
  115. if (!session->channel) {
  116. return;
  117. }
  118. switch (new_state) {
  119. case T38_PEER_REINVITE:
  120. ao2_ref(session, +1);
  121. if (pjsip_endpt_schedule_timer(ast_sip_get_pjsip_endpoint(), &state->timer, &delay) != PJ_SUCCESS) {
  122. ast_log(LOG_WARNING, "Scheduling of automatic T.38 rejection for channel '%s' failed\n",
  123. ast_channel_name(session->channel));
  124. ao2_ref(session, -1);
  125. }
  126. parameters = state->their_parms;
  127. parameters.max_ifp = ast_udptl_get_far_max_ifp(session_media->udptl);
  128. parameters.request_response = AST_T38_REQUEST_NEGOTIATE;
  129. ast_udptl_set_tag(session_media->udptl, "%s", ast_channel_name(session->channel));
  130. break;
  131. case T38_ENABLED:
  132. parameters = state->their_parms;
  133. parameters.max_ifp = ast_udptl_get_far_max_ifp(session_media->udptl);
  134. parameters.request_response = AST_T38_NEGOTIATED;
  135. ast_udptl_set_tag(session_media->udptl, "%s", ast_channel_name(session->channel));
  136. break;
  137. case T38_REJECTED:
  138. case T38_DISABLED:
  139. if (old_state == T38_ENABLED) {
  140. parameters.request_response = AST_T38_TERMINATED;
  141. } else if (old_state == T38_LOCAL_REINVITE) {
  142. parameters.request_response = AST_T38_REFUSED;
  143. }
  144. break;
  145. case T38_LOCAL_REINVITE:
  146. /* wait until we get a peer response before responding to local reinvite */
  147. break;
  148. case T38_MAX_ENUM:
  149. /* Well, that shouldn't happen */
  150. ast_assert(0);
  151. break;
  152. }
  153. if (parameters.request_response) {
  154. ast_queue_control_data(session->channel, AST_CONTROL_T38_PARAMETERS, &parameters, sizeof(parameters));
  155. }
  156. }
  157. /*! \brief Task function which rejects a T.38 re-invite and resumes handling it */
  158. static int t38_automatic_reject(void *obj)
  159. {
  160. RAII_VAR(struct ast_sip_session *, session, obj, ao2_cleanup);
  161. RAII_VAR(struct ast_datastore *, datastore, ast_sip_session_get_datastore(session, "t38"), ao2_cleanup);
  162. RAII_VAR(struct ast_sip_session_media *, session_media, ao2_find(session->media, "image", OBJ_KEY), ao2_cleanup);
  163. if (!datastore) {
  164. return 0;
  165. }
  166. ast_debug(2, "Automatically rejecting T.38 request on channel '%s'\n", ast_channel_name(session->channel));
  167. t38_change_state(session, session_media, datastore->data, T38_REJECTED);
  168. ast_sip_session_resume_reinvite(session);
  169. return 0;
  170. }
  171. /*! \brief Timer entry callback which queues a task to reject a T.38 re-invite and resume handling it */
  172. static void t38_automatic_reject_timer_cb(pj_timer_heap_t *timer_heap, struct pj_timer_entry *entry)
  173. {
  174. struct ast_sip_session *session = entry->user_data;
  175. if (ast_sip_push_task(session->serializer, t38_automatic_reject, session)) {
  176. ao2_ref(session, -1);
  177. }
  178. }
  179. /*! \brief Helper function which retrieves or allocates a T.38 state information datastore */
  180. static struct t38_state *t38_state_get_or_alloc(struct ast_sip_session *session)
  181. {
  182. RAII_VAR(struct ast_datastore *, datastore, ast_sip_session_get_datastore(session, "t38"), ao2_cleanup);
  183. struct t38_state *state;
  184. /* While the datastore refcount is decremented this is operating in the serializer so it will remain valid regardless */
  185. if (datastore) {
  186. return datastore->data;
  187. }
  188. if (!(datastore = ast_sip_session_alloc_datastore(&t38_datastore, "t38")) ||
  189. !(datastore->data = ast_calloc(1, sizeof(struct t38_state))) ||
  190. ast_sip_session_add_datastore(session, datastore)) {
  191. return NULL;
  192. }
  193. state = datastore->data;
  194. /* This will get bumped up before scheduling */
  195. state->timer.user_data = session;
  196. state->timer.cb = t38_automatic_reject_timer_cb;
  197. datastore->data = state;
  198. return state;
  199. }
  200. /*! \brief Initializes UDPTL support on a session, only done when actually needed */
  201. static int t38_initialize_session(struct ast_sip_session *session, struct ast_sip_session_media *session_media)
  202. {
  203. if (session_media->udptl) {
  204. return 0;
  205. }
  206. if (!(session_media->udptl = ast_udptl_new_with_bindaddr(NULL, NULL, 0,
  207. session->endpoint->media.t38.ipv6 ? &address_ipv6 : &address_ipv4))) {
  208. return -1;
  209. }
  210. ast_channel_set_fd(session->channel, 5, ast_udptl_fd(session_media->udptl));
  211. ast_udptl_set_error_correction_scheme(session_media->udptl, session->endpoint->media.t38.error_correction);
  212. ast_udptl_setnat(session_media->udptl, session->endpoint->media.t38.nat);
  213. return 0;
  214. }
  215. /*! \brief Callback for when T.38 reinvite SDP is created */
  216. static int t38_reinvite_sdp_cb(struct ast_sip_session *session, pjmedia_sdp_session *sdp)
  217. {
  218. int stream;
  219. /* Move the image media stream to the front and have it as the only stream, pjmedia will fill in
  220. * dummy streams for the rest
  221. */
  222. for (stream = 0; stream < sdp->media_count++; ++stream) {
  223. if (!pj_strcmp2(&sdp->media[stream]->desc.media, "image")) {
  224. sdp->media[0] = sdp->media[stream];
  225. sdp->media_count = 1;
  226. break;
  227. }
  228. }
  229. return 0;
  230. }
  231. /*! \brief Callback for when a response is received for a T.38 re-invite */
  232. static int t38_reinvite_response_cb(struct ast_sip_session *session, pjsip_rx_data *rdata)
  233. {
  234. struct pjsip_status_line status = rdata->msg_info.msg->line.status;
  235. struct t38_state *state;
  236. RAII_VAR(struct ast_sip_session_media *, session_media, NULL, ao2_cleanup);
  237. if (status.code == 100) {
  238. return 0;
  239. }
  240. if (!(state = t38_state_get_or_alloc(session)) ||
  241. !(session_media = ao2_find(session->media, "image", OBJ_KEY))) {
  242. ast_log(LOG_WARNING, "Received response to T.38 re-invite on '%s' but state unavailable\n",
  243. ast_channel_name(session->channel));
  244. return 0;
  245. }
  246. t38_change_state(session, session_media, state, (status.code == 200) ? T38_ENABLED : T38_REJECTED);
  247. return 0;
  248. }
  249. /*! \brief Task for reacting to T.38 control frame */
  250. static int t38_interpret_parameters(void *obj)
  251. {
  252. RAII_VAR(struct t38_parameters_task_data *, data, obj, ao2_cleanup);
  253. const struct ast_control_t38_parameters *parameters = data->frame->data.ptr;
  254. struct t38_state *state = t38_state_get_or_alloc(data->session);
  255. RAII_VAR(struct ast_sip_session_media *, session_media, ao2_find(data->session->media, "image", OBJ_KEY), ao2_cleanup);
  256. /* Without session media or state we can't interpret parameters */
  257. if (!session_media || !state) {
  258. return 0;
  259. }
  260. switch (parameters->request_response) {
  261. case AST_T38_NEGOTIATED:
  262. case AST_T38_REQUEST_NEGOTIATE: /* Request T38 */
  263. /* Negotiation can not take place without a valid max_ifp value. */
  264. if (!parameters->max_ifp) {
  265. t38_change_state(data->session, session_media, state, T38_REJECTED);
  266. if (data->session->t38state == T38_PEER_REINVITE) {
  267. ast_sip_session_resume_reinvite(data->session);
  268. }
  269. break;
  270. } else if (data->session->t38state == T38_PEER_REINVITE) {
  271. state->our_parms = *parameters;
  272. /* modify our parameters to conform to the peer's parameters,
  273. * based on the rules in the ITU T.38 recommendation
  274. */
  275. if (!state->their_parms.fill_bit_removal) {
  276. state->our_parms.fill_bit_removal = 0;
  277. }
  278. if (!state->their_parms.transcoding_mmr) {
  279. state->our_parms.transcoding_mmr = 0;
  280. }
  281. if (!state->their_parms.transcoding_jbig) {
  282. state->our_parms.transcoding_jbig = 0;
  283. }
  284. state->our_parms.version = MIN(state->our_parms.version, state->their_parms.version);
  285. state->our_parms.rate_management = state->their_parms.rate_management;
  286. ast_udptl_set_local_max_ifp(session_media->udptl, state->our_parms.max_ifp);
  287. t38_change_state(data->session, session_media, state, T38_ENABLED);
  288. ast_sip_session_resume_reinvite(data->session);
  289. } else if (data->session->t38state != T38_ENABLED) {
  290. if (t38_initialize_session(data->session, session_media)) {
  291. break;
  292. }
  293. state->our_parms = *parameters;
  294. ast_udptl_set_local_max_ifp(session_media->udptl, state->our_parms.max_ifp);
  295. t38_change_state(data->session, session_media, state, T38_LOCAL_REINVITE);
  296. ast_sip_session_refresh(data->session, NULL, t38_reinvite_sdp_cb, t38_reinvite_response_cb,
  297. AST_SIP_SESSION_REFRESH_METHOD_INVITE, 1);
  298. }
  299. break;
  300. case AST_T38_TERMINATED:
  301. case AST_T38_REFUSED:
  302. case AST_T38_REQUEST_TERMINATE: /* Shutdown T38 */
  303. if (data->session->t38state == T38_PEER_REINVITE) {
  304. t38_change_state(data->session, session_media, state, T38_REJECTED);
  305. ast_sip_session_resume_reinvite(data->session);
  306. } else if (data->session->t38state == T38_ENABLED) {
  307. t38_change_state(data->session, session_media, state, T38_DISABLED);
  308. ast_sip_session_refresh(data->session, NULL, NULL, NULL, AST_SIP_SESSION_REFRESH_METHOD_INVITE, 1);
  309. }
  310. break;
  311. case AST_T38_REQUEST_PARMS: { /* Application wants remote's parameters re-sent */
  312. struct ast_control_t38_parameters parameters = state->their_parms;
  313. if (data->session->t38state == T38_PEER_REINVITE) {
  314. parameters.max_ifp = ast_udptl_get_far_max_ifp(session_media->udptl);
  315. parameters.request_response = AST_T38_REQUEST_NEGOTIATE;
  316. ast_queue_control_data(data->session->channel, AST_CONTROL_T38_PARAMETERS, &parameters, sizeof(parameters));
  317. }
  318. break;
  319. }
  320. default:
  321. break;
  322. }
  323. return 0;
  324. }
  325. /*! \brief Frame hook callback for writing */
  326. static struct ast_frame *t38_framehook_write(struct ast_sip_session *session, struct ast_frame *f)
  327. {
  328. if (f->frametype == AST_FRAME_CONTROL && f->subclass.integer == AST_CONTROL_T38_PARAMETERS &&
  329. session->endpoint->media.t38.enabled) {
  330. struct t38_parameters_task_data *data = t38_parameters_task_data_alloc(session, f);
  331. if (!data) {
  332. return f;
  333. }
  334. if (ast_sip_push_task(session->serializer, t38_interpret_parameters, data)) {
  335. ao2_ref(data, -1);
  336. }
  337. f = &ast_null_frame;
  338. } else if (f->frametype == AST_FRAME_MODEM) {
  339. RAII_VAR(struct ast_sip_session_media *, session_media, NULL, ao2_cleanup);
  340. if ((session_media = ao2_find(session->media, "image", OBJ_KEY)) &&
  341. session_media->udptl) {
  342. ast_udptl_write(session_media->udptl, f);
  343. }
  344. }
  345. return f;
  346. }
  347. /*! \brief Frame hook callback for reading */
  348. static struct ast_frame *t38_framehook_read(struct ast_sip_session *session, struct ast_frame *f)
  349. {
  350. if (ast_channel_fdno(session->channel) == 5) {
  351. RAII_VAR(struct ast_sip_session_media *, session_media, NULL, ao2_cleanup);
  352. if ((session_media = ao2_find(session->media, "image", OBJ_KEY)) &&
  353. session_media->udptl) {
  354. f = ast_udptl_read(session_media->udptl);
  355. }
  356. }
  357. return f;
  358. }
  359. /*! \brief Frame hook callback for T.38 related stuff */
  360. static struct ast_frame *t38_framehook(struct ast_channel *chan, struct ast_frame *f,
  361. enum ast_framehook_event event, void *data)
  362. {
  363. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
  364. if (event == AST_FRAMEHOOK_EVENT_READ) {
  365. f = t38_framehook_read(channel->session, f);
  366. } else if (event == AST_FRAMEHOOK_EVENT_WRITE) {
  367. f = t38_framehook_write(channel->session, f);
  368. }
  369. return f;
  370. }
  371. static void t38_masq(void *data, int framehook_id,
  372. struct ast_channel *old_chan, struct ast_channel *new_chan)
  373. {
  374. if (ast_channel_tech(old_chan) == ast_channel_tech(new_chan)) {
  375. return;
  376. }
  377. /* This framehook is only applicable to PJSIP channels */
  378. ast_framehook_detach(new_chan, framehook_id);
  379. }
  380. /*! \brief Function called to attach T.38 framehook to channel when appropriate */
  381. static void t38_attach_framehook(struct ast_sip_session *session)
  382. {
  383. static struct ast_framehook_interface hook = {
  384. .version = AST_FRAMEHOOK_INTERFACE_VERSION,
  385. .event_cb = t38_framehook,
  386. .chan_fixup_cb = t38_masq,
  387. .chan_breakdown_cb = t38_masq,
  388. };
  389. /* Only attach the framehook on the first outgoing INVITE or the first incoming INVITE */
  390. if ((session->inv_session->state != PJSIP_INV_STATE_NULL &&
  391. session->inv_session->state != PJSIP_INV_STATE_INCOMING) ||
  392. !session->endpoint->media.t38.enabled) {
  393. return;
  394. }
  395. if (ast_framehook_attach(session->channel, &hook) < 0) {
  396. ast_log(LOG_WARNING, "Could not attach T.38 Frame hook to channel, T.38 will be unavailable on '%s'\n",
  397. ast_channel_name(session->channel));
  398. }
  399. }
  400. /*! \brief Function called when an INVITE goes out */
  401. static int t38_incoming_invite_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
  402. {
  403. t38_attach_framehook(session);
  404. return 0;
  405. }
  406. /*! \brief Function called when an INVITE comes in */
  407. static void t38_outgoing_invite_request(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
  408. {
  409. t38_attach_framehook(session);
  410. }
  411. /*! \brief Get Max T.38 Transmission rate from T38 capabilities */
  412. static unsigned int t38_get_rate(enum ast_control_t38_rate rate)
  413. {
  414. switch (rate) {
  415. case AST_T38_RATE_2400:
  416. return 2400;
  417. case AST_T38_RATE_4800:
  418. return 4800;
  419. case AST_T38_RATE_7200:
  420. return 7200;
  421. case AST_T38_RATE_9600:
  422. return 9600;
  423. case AST_T38_RATE_12000:
  424. return 12000;
  425. case AST_T38_RATE_14400:
  426. return 14400;
  427. default:
  428. return 0;
  429. }
  430. }
  431. /*! \brief Supplement for adding framehook to session channel */
  432. static struct ast_sip_session_supplement t38_supplement = {
  433. .method = "INVITE",
  434. .priority = AST_SIP_SUPPLEMENT_PRIORITY_CHANNEL + 1,
  435. .incoming_request = t38_incoming_invite_request,
  436. .outgoing_request = t38_outgoing_invite_request,
  437. };
  438. /*! \brief Parse a T.38 image stream and store the attribute information */
  439. static void t38_interpret_sdp(struct t38_state *state, struct ast_sip_session *session, struct ast_sip_session_media *session_media,
  440. const struct pjmedia_sdp_media *stream)
  441. {
  442. unsigned int attr_i;
  443. for (attr_i = 0; attr_i < stream->attr_count; attr_i++) {
  444. pjmedia_sdp_attr *attr = stream->attr[attr_i];
  445. if (!pj_stricmp2(&attr->name, "t38faxmaxbuffer")) {
  446. /* This is purposely left empty, it is unused */
  447. } else if (!pj_stricmp2(&attr->name, "t38maxbitrate") || !pj_stricmp2(&attr->name, "t38faxmaxrate")) {
  448. switch (pj_strtoul(&attr->value)) {
  449. case 14400:
  450. state->their_parms.rate = AST_T38_RATE_14400;
  451. break;
  452. case 12000:
  453. state->their_parms.rate = AST_T38_RATE_12000;
  454. break;
  455. case 9600:
  456. state->their_parms.rate = AST_T38_RATE_9600;
  457. break;
  458. case 7200:
  459. state->their_parms.rate = AST_T38_RATE_7200;
  460. break;
  461. case 4800:
  462. state->their_parms.rate = AST_T38_RATE_4800;
  463. break;
  464. case 2400:
  465. state->their_parms.rate = AST_T38_RATE_2400;
  466. break;
  467. }
  468. } else if (!pj_stricmp2(&attr->name, "t38faxversion")) {
  469. state->their_parms.version = pj_strtoul(&attr->value);
  470. } else if (!pj_stricmp2(&attr->name, "t38faxmaxdatagram") || !pj_stricmp2(&attr->name, "t38maxdatagram")) {
  471. if (session->endpoint->media.t38.maxdatagram) {
  472. ast_udptl_set_far_max_datagram(session_media->udptl, session->endpoint->media.t38.maxdatagram);
  473. } else {
  474. ast_udptl_set_far_max_datagram(session_media->udptl, pj_strtoul(&attr->value));
  475. }
  476. } else if (!pj_stricmp2(&attr->name, "t38faxfillbitremoval")) {
  477. state->their_parms.fill_bit_removal = 1;
  478. } else if (!pj_stricmp2(&attr->name, "t38faxtranscodingmmr")) {
  479. state->their_parms.transcoding_mmr = 1;
  480. } else if (!pj_stricmp2(&attr->name, "t38faxtranscodingjbig")) {
  481. state->their_parms.transcoding_jbig = 1;
  482. } else if (!pj_stricmp2(&attr->name, "t38faxratemanagement")) {
  483. if (!pj_stricmp2(&attr->value, "localTCF")) {
  484. state->their_parms.rate_management = AST_T38_RATE_MANAGEMENT_LOCAL_TCF;
  485. } else if (!pj_stricmp2(&attr->value, "transferredTCF")) {
  486. state->their_parms.rate_management = AST_T38_RATE_MANAGEMENT_TRANSFERRED_TCF;
  487. }
  488. } else if (!pj_stricmp2(&attr->name, "t38faxudpec")) {
  489. if (!pj_stricmp2(&attr->value, "t38UDPRedundancy")) {
  490. ast_udptl_set_error_correction_scheme(session_media->udptl, UDPTL_ERROR_CORRECTION_REDUNDANCY);
  491. } else if (!pj_stricmp2(&attr->value, "t38UDPFEC")) {
  492. ast_udptl_set_error_correction_scheme(session_media->udptl, UDPTL_ERROR_CORRECTION_FEC);
  493. } else {
  494. ast_udptl_set_error_correction_scheme(session_media->udptl, UDPTL_ERROR_CORRECTION_NONE);
  495. }
  496. }
  497. }
  498. }
  499. /*! \brief Function which defers an incoming media stream */
  500. static enum ast_sip_session_sdp_stream_defer defer_incoming_sdp_stream(
  501. struct ast_sip_session *session, struct ast_sip_session_media *session_media,
  502. const struct pjmedia_sdp_session *sdp, const struct pjmedia_sdp_media *stream)
  503. {
  504. struct t38_state *state;
  505. if (!session->endpoint->media.t38.enabled) {
  506. return AST_SIP_SESSION_SDP_DEFER_NOT_HANDLED;
  507. }
  508. if (t38_initialize_session(session, session_media)) {
  509. return AST_SIP_SESSION_SDP_DEFER_ERROR;
  510. }
  511. if (!(state = t38_state_get_or_alloc(session))) {
  512. return AST_SIP_SESSION_SDP_DEFER_ERROR;
  513. }
  514. t38_interpret_sdp(state, session, session_media, stream);
  515. /* If they are initiating the re-invite we need to defer responding until later */
  516. if (session->t38state == T38_DISABLED) {
  517. t38_change_state(session, session_media, state, T38_PEER_REINVITE);
  518. return AST_SIP_SESSION_SDP_DEFER_NEEDED;
  519. }
  520. return AST_SIP_SESSION_SDP_DEFER_NOT_NEEDED;
  521. }
  522. /*! \brief Function which negotiates an incoming media stream */
  523. static int negotiate_incoming_sdp_stream(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
  524. const struct pjmedia_sdp_session *sdp, const struct pjmedia_sdp_media *stream)
  525. {
  526. struct t38_state *state;
  527. char host[NI_MAXHOST];
  528. RAII_VAR(struct ast_sockaddr *, addrs, NULL, ast_free_ptr);
  529. if (!session->endpoint->media.t38.enabled) {
  530. return -1;
  531. }
  532. if (!(state = t38_state_get_or_alloc(session))) {
  533. return -1;
  534. }
  535. if ((session->t38state == T38_REJECTED) || (session->t38state == T38_DISABLED)) {
  536. t38_change_state(session, session_media, state, T38_DISABLED);
  537. return -1;
  538. }
  539. ast_copy_pj_str(host, stream->conn ? &stream->conn->addr : &sdp->conn->addr, sizeof(host));
  540. /* Ensure that the address provided is valid */
  541. if (ast_sockaddr_resolve(&addrs, host, PARSE_PORT_FORBID, AST_AF_INET) <= 0) {
  542. /* The provided host was actually invalid so we error out this negotiation */
  543. return -1;
  544. }
  545. /* Check the address family to make sure it matches configured */
  546. if ((ast_sockaddr_is_ipv6(addrs) && !session->endpoint->media.t38.ipv6) ||
  547. (ast_sockaddr_is_ipv4(addrs) && session->endpoint->media.t38.ipv6)) {
  548. /* The address does not match configured */
  549. return -1;
  550. }
  551. return 1;
  552. }
  553. /*! \brief Function which creates an outgoing stream */
  554. static int create_outgoing_sdp_stream(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
  555. struct pjmedia_sdp_session *sdp)
  556. {
  557. pj_pool_t *pool = session->inv_session->pool_prov;
  558. static const pj_str_t STR_IN = { "IN", 2 };
  559. static const pj_str_t STR_IP4 = { "IP4", 3};
  560. static const pj_str_t STR_IP6 = { "IP6", 3};
  561. static const pj_str_t STR_UDPTL = { "udptl", 5 };
  562. static const pj_str_t STR_T38 = { "t38", 3 };
  563. static const pj_str_t STR_TRANSFERREDTCF = { "transferredTCF", 14 };
  564. static const pj_str_t STR_LOCALTCF = { "localTCF", 8 };
  565. static const pj_str_t STR_T38UDPFEC = { "t38UDPFEC", 9 };
  566. static const pj_str_t STR_T38UDPREDUNDANCY = { "t38UDPRedundancy", 16 };
  567. struct t38_state *state;
  568. pjmedia_sdp_media *media;
  569. char hostip[PJ_INET6_ADDRSTRLEN+2];
  570. struct ast_sockaddr addr;
  571. char tmp[512];
  572. pj_str_t stmp;
  573. if (!session->endpoint->media.t38.enabled) {
  574. return 1;
  575. } else if ((session->t38state != T38_LOCAL_REINVITE) && (session->t38state != T38_PEER_REINVITE) &&
  576. (session->t38state != T38_ENABLED)) {
  577. return 1;
  578. } else if (!(state = t38_state_get_or_alloc(session))) {
  579. return -1;
  580. } else if (t38_initialize_session(session, session_media)) {
  581. return -1;
  582. }
  583. if (!(media = pj_pool_zalloc(pool, sizeof(struct pjmedia_sdp_media))) ||
  584. !(media->conn = pj_pool_zalloc(pool, sizeof(struct pjmedia_sdp_conn)))) {
  585. return -1;
  586. }
  587. media->desc.media = pj_str(session_media->stream_type);
  588. media->desc.transport = STR_UDPTL;
  589. if (ast_strlen_zero(session->endpoint->media.address)) {
  590. pj_sockaddr localaddr;
  591. if (pj_gethostip(session->endpoint->media.t38.ipv6 ? pj_AF_INET6() : pj_AF_INET(), &localaddr)) {
  592. return -1;
  593. }
  594. pj_sockaddr_print(&localaddr, hostip, sizeof(hostip), 2);
  595. } else {
  596. ast_copy_string(hostip, session->endpoint->media.address, sizeof(hostip));
  597. }
  598. media->conn->net_type = STR_IN;
  599. media->conn->addr_type = session->endpoint->media.t38.ipv6 ? STR_IP6 : STR_IP4;
  600. pj_strdup2(pool, &media->conn->addr, hostip);
  601. ast_udptl_get_us(session_media->udptl, &addr);
  602. media->desc.port = (pj_uint16_t) ast_sockaddr_port(&addr);
  603. media->desc.port_count = 1;
  604. media->desc.fmt[media->desc.fmt_count++] = STR_T38;
  605. snprintf(tmp, sizeof(tmp), "%u", state->our_parms.version);
  606. media->attr[media->attr_count++] = pjmedia_sdp_attr_create(pool, "T38FaxVersion", pj_cstr(&stmp, tmp));
  607. snprintf(tmp, sizeof(tmp), "%u", t38_get_rate(state->our_parms.rate));
  608. media->attr[media->attr_count++] = pjmedia_sdp_attr_create(pool, "T38MaxBitRate", pj_cstr(&stmp, tmp));
  609. if (state->our_parms.fill_bit_removal) {
  610. media->attr[media->attr_count++] = pjmedia_sdp_attr_create(pool, "T38FaxFillBitRemoval", NULL);
  611. }
  612. if (state->our_parms.transcoding_mmr) {
  613. media->attr[media->attr_count++] = pjmedia_sdp_attr_create(pool, "T38FaxTranscodingMMR", NULL);
  614. }
  615. if (state->our_parms.transcoding_jbig) {
  616. media->attr[media->attr_count++] = pjmedia_sdp_attr_create(pool, "T38FaxTranscodingJBIG", NULL);
  617. }
  618. switch (state->our_parms.rate_management) {
  619. case AST_T38_RATE_MANAGEMENT_TRANSFERRED_TCF:
  620. media->attr[media->attr_count++] = pjmedia_sdp_attr_create(pool, "T38FaxRateManagement", &STR_TRANSFERREDTCF);
  621. break;
  622. case AST_T38_RATE_MANAGEMENT_LOCAL_TCF:
  623. media->attr[media->attr_count++] = pjmedia_sdp_attr_create(pool, "T38FaxRateManagement", &STR_LOCALTCF);
  624. break;
  625. }
  626. snprintf(tmp, sizeof(tmp), "%u", ast_udptl_get_local_max_datagram(session_media->udptl));
  627. media->attr[media->attr_count++] = pjmedia_sdp_attr_create(pool, "T38FaxMaxDatagram", pj_cstr(&stmp, tmp));
  628. switch (ast_udptl_get_error_correction_scheme(session_media->udptl)) {
  629. case UDPTL_ERROR_CORRECTION_NONE:
  630. break;
  631. case UDPTL_ERROR_CORRECTION_FEC:
  632. media->attr[media->attr_count++] = pjmedia_sdp_attr_create(pool, "T38FaxUdpEC", &STR_T38UDPFEC);
  633. break;
  634. case UDPTL_ERROR_CORRECTION_REDUNDANCY:
  635. media->attr[media->attr_count++] = pjmedia_sdp_attr_create(pool, "T38FaxUdpEC", &STR_T38UDPREDUNDANCY);
  636. break;
  637. }
  638. sdp->media[sdp->media_count++] = media;
  639. return 1;
  640. }
  641. /*! \brief Function which applies a negotiated stream */
  642. static int apply_negotiated_sdp_stream(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
  643. const struct pjmedia_sdp_session *local, const struct pjmedia_sdp_media *local_stream,
  644. const struct pjmedia_sdp_session *remote, const struct pjmedia_sdp_media *remote_stream)
  645. {
  646. RAII_VAR(struct ast_sockaddr *, addrs, NULL, ast_free_ptr);
  647. char host[NI_MAXHOST];
  648. struct t38_state *state;
  649. if (!session_media->udptl) {
  650. return 0;
  651. }
  652. if (!(state = t38_state_get_or_alloc(session))) {
  653. return -1;
  654. }
  655. ast_copy_pj_str(host, remote_stream->conn ? &remote_stream->conn->addr : &remote->conn->addr, sizeof(host));
  656. /* Ensure that the address provided is valid */
  657. if (ast_sockaddr_resolve(&addrs, host, PARSE_PORT_FORBID, AST_AF_UNSPEC) <= 0) {
  658. /* The provided host was actually invalid so we error out this negotiation */
  659. return -1;
  660. }
  661. ast_sockaddr_set_port(addrs, remote_stream->desc.port);
  662. ast_udptl_set_peer(session_media->udptl, addrs);
  663. t38_interpret_sdp(state, session, session_media, remote_stream);
  664. return 0;
  665. }
  666. /*! \brief Function which updates the media stream with external media address, if applicable */
  667. static void change_outgoing_sdp_stream_media_address(pjsip_tx_data *tdata, struct pjmedia_sdp_media *stream, struct ast_sip_transport *transport)
  668. {
  669. char host[NI_MAXHOST];
  670. struct ast_sockaddr addr = { { 0, } };
  671. /* If the stream has been rejected there will be no connection line */
  672. if (!stream->conn) {
  673. return;
  674. }
  675. ast_copy_pj_str(host, &stream->conn->addr, sizeof(host));
  676. ast_sockaddr_parse(&addr, host, PARSE_PORT_FORBID);
  677. /* Is the address within the SDP inside the same network? */
  678. if (ast_apply_ha(transport->localnet, &addr) == AST_SENSE_ALLOW) {
  679. return;
  680. }
  681. pj_strdup2(tdata->pool, &stream->conn->addr, transport->external_media_address);
  682. }
  683. /*! \brief Function which destroys the UDPTL instance when session ends */
  684. static void stream_destroy(struct ast_sip_session_media *session_media)
  685. {
  686. if (session_media->udptl) {
  687. ast_udptl_destroy(session_media->udptl);
  688. }
  689. }
  690. /*! \brief SDP handler for 'image' media stream */
  691. static struct ast_sip_session_sdp_handler image_sdp_handler = {
  692. .id = "image",
  693. .defer_incoming_sdp_stream = defer_incoming_sdp_stream,
  694. .negotiate_incoming_sdp_stream = negotiate_incoming_sdp_stream,
  695. .create_outgoing_sdp_stream = create_outgoing_sdp_stream,
  696. .apply_negotiated_sdp_stream = apply_negotiated_sdp_stream,
  697. .change_outgoing_sdp_stream_media_address = change_outgoing_sdp_stream_media_address,
  698. .stream_destroy = stream_destroy,
  699. };
  700. /*! \brief Unloads the SIP T.38 module from Asterisk */
  701. static int unload_module(void)
  702. {
  703. ast_sip_session_unregister_sdp_handler(&image_sdp_handler, "image");
  704. ast_sip_session_unregister_supplement(&t38_supplement);
  705. return 0;
  706. }
  707. /*!
  708. * \brief Load the module
  709. *
  710. * Module loading including tests for configuration or dependencies.
  711. * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
  712. * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
  713. * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
  714. * configuration file or other non-critical problem return
  715. * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
  716. */
  717. static int load_module(void)
  718. {
  719. CHECK_PJSIP_SESSION_MODULE_LOADED();
  720. ast_sockaddr_parse(&address_ipv4, "0.0.0.0", 0);
  721. ast_sockaddr_parse(&address_ipv6, "::", 0);
  722. if (ast_sip_session_register_supplement(&t38_supplement)) {
  723. ast_log(LOG_ERROR, "Unable to register T.38 session supplement\n");
  724. goto end;
  725. }
  726. if (ast_sip_session_register_sdp_handler(&image_sdp_handler, "image")) {
  727. ast_log(LOG_ERROR, "Unable to register SDP handler for image stream type\n");
  728. goto end;
  729. }
  730. return AST_MODULE_LOAD_SUCCESS;
  731. end:
  732. unload_module();
  733. return AST_MODULE_LOAD_FAILURE;
  734. }
  735. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP T.38 UDPTL Support",
  736. .support_level = AST_MODULE_SUPPORT_CORE,
  737. .load = load_module,
  738. .unload = unload_module,
  739. .load_pri = AST_MODPRI_CHANNEL_DRIVER,
  740. );