res_fax.c 122 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2008-2009, Digium, Inc.
  5. *
  6. * Dwayne M. Hubbard <dhubbard@digium.com>
  7. * Kevin P. Fleming <kpfleming@digium.com>
  8. * Matthew Nicholson <mnicholson@digium.com>
  9. *
  10. * Initial T.38-gateway code
  11. * 2008, Daniel Ferenci <daniel.ferenci@nethemba.com>
  12. * Created by Nethemba s.r.o. http://www.nethemba.com
  13. * Sponsored by IPEX a.s. http://www.ipex.cz
  14. *
  15. * T.38-gateway integration into asterisk app_fax and rework
  16. * 2008-2011, Gregory Hinton Nietsky <gregory@distrotech.co.za>
  17. * dns Telecom http://www.dnstelecom.co.za
  18. *
  19. * Modified to make T.38-gateway compatible with Asterisk 1.6.2
  20. * 2010, Anton Verevkin <mymail@verevkin.it>
  21. * ViaNetTV http://www.vianettv.com
  22. *
  23. * Modified to make T.38-gateway work
  24. * 2010, Klaus Darilion, IPCom GmbH, www.ipcom.at
  25. *
  26. * See http://www.asterisk.org for more information about
  27. * the Asterisk project. Please do not directly contact
  28. * any of the maintainers of this project for assistance;
  29. * the project provides a web site, mailing lists and IRC
  30. * channels for your use.
  31. *
  32. * This program is free software, distributed under the terms of
  33. * the GNU General Public License Version 2. See the LICENSE file
  34. * at the top of the source tree.
  35. */
  36. /*** MODULEINFO
  37. <support_level>core</support_level>
  38. <conflict>app_fax</conflict>
  39. ***/
  40. /*! \file
  41. *
  42. * \brief Generic FAX Resource for FAX technology resource modules
  43. *
  44. * \author Dwayne M. Hubbard <dhubbard@digium.com>
  45. * \author Kevin P. Fleming <kpfleming@digium.com>
  46. * \author Matthew Nicholson <mnicholson@digium.com>
  47. * \author Gregory H. Nietsky <gregory@distrotech.co.za>
  48. *
  49. * A generic FAX resource module that provides SendFAX and ReceiveFAX applications.
  50. * This module requires FAX technology modules, like res_fax_spandsp, to register with it
  51. * so it can use the technology modules to perform the actual FAX transmissions.
  52. * \ingroup applications
  53. */
  54. #include "asterisk.h"
  55. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  56. #include "asterisk/io.h"
  57. #include "asterisk/file.h"
  58. #include "asterisk/logger.h"
  59. #include "asterisk/module.h"
  60. #include "asterisk/app.h"
  61. #include "asterisk/lock.h"
  62. #include "asterisk/options.h"
  63. #include "asterisk/strings.h"
  64. #include "asterisk/cli.h"
  65. #include "asterisk/utils.h"
  66. #include "asterisk/config.h"
  67. #include "asterisk/astobj2.h"
  68. #include "asterisk/res_fax.h"
  69. #include "asterisk/file.h"
  70. #include "asterisk/channel.h"
  71. #include "asterisk/pbx.h"
  72. #include "asterisk/manager.h"
  73. #include "asterisk/dsp.h"
  74. #include "asterisk/indications.h"
  75. #include "asterisk/ast_version.h"
  76. #include "asterisk/translate.h"
  77. /*** DOCUMENTATION
  78. <application name="ReceiveFAX" language="en_US" module="res_fax">
  79. <synopsis>
  80. Receive a FAX and save as a TIFF/F file.
  81. </synopsis>
  82. <syntax>
  83. <parameter name="filename" required="true" />
  84. <parameter name="options">
  85. <optionlist>
  86. <option name="d">
  87. <para>Enable FAX debugging.</para>
  88. </option>
  89. <option name="f">
  90. <para>Allow audio fallback FAX transfer on T.38 capable channels.</para>
  91. </option>
  92. <option name="F">
  93. <para>Force usage of audio mode on T.38 capable channels.</para>
  94. </option>
  95. <option name="s">
  96. <para>Send progress Manager events (overrides statusevents setting in res_fax.conf).</para>
  97. </option>
  98. </optionlist>
  99. </parameter>
  100. </syntax>
  101. <description>
  102. <para>This application is provided by res_fax, which is a FAX technology agnostic module
  103. that utilizes FAX technology resource modules to complete a FAX transmission.</para>
  104. <para>Session arguments can be set by the FAXOPT function and to check results of the ReceiveFax() application.</para>
  105. </description>
  106. <see-also>
  107. <ref type="function">FAXOPT</ref>
  108. </see-also>
  109. </application>
  110. <application name="SendFAX" language="en_US" module="res_fax">
  111. <synopsis>
  112. Sends a specified TIFF/F file as a FAX.
  113. </synopsis>
  114. <syntax>
  115. <parameter name="filename" required="true" argsep="&amp;">
  116. <argument name="filename2" multiple="true">
  117. <para>TIFF file to send as a FAX.</para>
  118. </argument>
  119. </parameter>
  120. <parameter name="options">
  121. <optionlist>
  122. <option name="d">
  123. <para>Enable FAX debugging.</para>
  124. </option>
  125. <option name="f">
  126. <para>Allow audio fallback FAX transfer on T.38 capable channels.</para>
  127. </option>
  128. <option name="F">
  129. <para>Force usage of audio mode on T.38 capable channels.</para>
  130. </option>
  131. <option name="s">
  132. <para>Send progress Manager events (overrides statusevents setting in res_fax.conf).</para>
  133. </option>
  134. <option name="z">
  135. <para>Initiate a T.38 reinvite on the channel if the remote end does not.</para>
  136. </option>
  137. </optionlist>
  138. </parameter>
  139. </syntax>
  140. <description>
  141. <para>This application is provided by res_fax, which is a FAX technology agnostic module
  142. that utilizes FAX technology resource modules to complete a FAX transmission.</para>
  143. <para>Session arguments can be set by the FAXOPT function and to check results of the SendFax() application.</para>
  144. </description>
  145. <see-also>
  146. <ref type="function">FAXOPT</ref>
  147. </see-also>
  148. </application>
  149. <function name="FAXOPT" language="en_US" module="res_fax">
  150. <synopsis>
  151. Gets/sets various pieces of information about a fax session.
  152. </synopsis>
  153. <syntax>
  154. <parameter name="item" required="true">
  155. <enumlist>
  156. <enum name="ecm">
  157. <para>R/W Error Correction Mode (ECM) enable with 'yes', disable with 'no'.</para>
  158. </enum>
  159. <enum name="error">
  160. <para>R/O FAX transmission error code upon failure.</para>
  161. </enum>
  162. <enum name="filename">
  163. <para>R/O Filename of the first file of the FAX transmission.</para>
  164. </enum>
  165. <enum name="filenames">
  166. <para>R/O Filenames of all of the files in the FAX transmission (comma separated).</para>
  167. </enum>
  168. <enum name="headerinfo">
  169. <para>R/W FAX header information.</para>
  170. </enum>
  171. <enum name="localstationid">
  172. <para>R/W Local Station Identification.</para>
  173. </enum>
  174. <enum name="minrate">
  175. <para>R/W Minimum transfer rate set before transmission.</para>
  176. </enum>
  177. <enum name="maxrate">
  178. <para>R/W Maximum transfer rate set before transmission.</para>
  179. </enum>
  180. <enum name="modem">
  181. <para>R/W Modem type (v17/v27/v29).</para>
  182. </enum>
  183. <enum name="gateway">
  184. <para>R/W T38 fax gateway, with optional fax activity timeout in seconds (yes[,timeout]/no)</para>
  185. </enum>
  186. <enum name="pages">
  187. <para>R/O Number of pages transferred.</para>
  188. </enum>
  189. <enum name="rate">
  190. <para>R/O Negotiated transmission rate.</para>
  191. </enum>
  192. <enum name="remotestationid">
  193. <para>R/O Remote Station Identification after transmission.</para>
  194. </enum>
  195. <enum name="resolution">
  196. <para>R/O Negotiated image resolution after transmission.</para>
  197. </enum>
  198. <enum name="sessionid">
  199. <para>R/O Session ID of the FAX transmission.</para>
  200. </enum>
  201. <enum name="status">
  202. <para>R/O Result Status of the FAX transmission.</para>
  203. </enum>
  204. <enum name="statusstr">
  205. <para>R/O Verbose Result Status of the FAX transmission.</para>
  206. </enum>
  207. </enumlist>
  208. </parameter>
  209. </syntax>
  210. <description>
  211. <para>FAXOPT can be used to override the settings for a FAX session listed in <filename>res_fax.conf</filename>,
  212. it can also be used to retreive information about a FAX session that has finished eg. pages/status.</para>
  213. </description>
  214. <see-also>
  215. <ref type="application">ReceiveFax</ref>
  216. <ref type="application">SendFax</ref>
  217. </see-also>
  218. </function>
  219. ***/
  220. static const char app_receivefax[] = "ReceiveFAX";
  221. static const char app_sendfax[] = "SendFAX";
  222. struct debug_info_history {
  223. unsigned int consec_frames;
  224. unsigned int consec_ms;
  225. unsigned char silence;
  226. };
  227. struct ast_fax_debug_info {
  228. struct timeval base_tv;
  229. struct debug_info_history c2s, s2c;
  230. struct ast_dsp *dsp;
  231. };
  232. /*! \brief used for gateway framehook */
  233. struct fax_gateway {
  234. /*! \brief FAX Session */
  235. struct ast_fax_session *s;
  236. struct ast_fax_session *peer_v21_session;
  237. struct ast_fax_session *chan_v21_session;
  238. /*! \brief reserved fax session token */
  239. struct ast_fax_tech_token *token;
  240. /*! \brief the start of our timeout counter */
  241. struct timeval timeout_start;
  242. /*! \brief framehook used in gateway mode */
  243. int framehook;
  244. /*! \brief bridged */
  245. int bridged:1;
  246. /*! \brief 1 if a v21 preamble has been detected */
  247. int detected_v21:1;
  248. /*! \brief a flag to track the state of our negotiation */
  249. enum ast_t38_state t38_state;
  250. /*! \brief original audio formats */
  251. struct ast_format chan_read_format;
  252. struct ast_format chan_write_format;
  253. struct ast_format peer_read_format;
  254. struct ast_format peer_write_format;
  255. };
  256. static int fax_logger_level = -1;
  257. /*! \brief maximum buckets for res_fax ao2 containers */
  258. #define FAX_MAXBUCKETS 10
  259. #define RES_FAX_TIMEOUT 10000
  260. #define FAX_GATEWAY_TIMEOUT RES_FAX_TIMEOUT
  261. /*! \brief The faxregistry is used to manage information and statistics for all FAX sessions. */
  262. static struct {
  263. /*! The number of active FAX sessions */
  264. int active_sessions;
  265. /*! The number of reserved FAX sessions */
  266. int reserved_sessions;
  267. /*! active sessions are astobj2 objects */
  268. struct ao2_container *container;
  269. /*! Total number of Tx FAX attempts */
  270. int fax_tx_attempts;
  271. /*! Total number of Rx FAX attempts */
  272. int fax_rx_attempts;
  273. /*! Number of successful FAX transmissions */
  274. int fax_complete;
  275. /*! Number of failed FAX transmissions */
  276. int fax_failures;
  277. /*! the next unique session name */
  278. int nextsessionname;
  279. } faxregistry;
  280. /*! \brief registered FAX technology modules are put into this list */
  281. struct fax_module {
  282. const struct ast_fax_tech *tech;
  283. AST_RWLIST_ENTRY(fax_module) list;
  284. };
  285. static AST_RWLIST_HEAD_STATIC(faxmodules, fax_module);
  286. #define RES_FAX_MINRATE 2400
  287. #define RES_FAX_MAXRATE 14400
  288. #define RES_FAX_STATUSEVENTS 0
  289. #define RES_FAX_MODEM (AST_FAX_MODEM_V17 | AST_FAX_MODEM_V27 | AST_FAX_MODEM_V29)
  290. struct fax_options {
  291. enum ast_fax_modems modems;
  292. uint32_t statusevents:1;
  293. uint32_t ecm:1;
  294. unsigned int minrate;
  295. unsigned int maxrate;
  296. };
  297. static struct fax_options general_options;
  298. static const struct fax_options default_options = {
  299. .minrate = RES_FAX_MINRATE,
  300. .maxrate = RES_FAX_MAXRATE,
  301. .statusevents = RES_FAX_STATUSEVENTS,
  302. .modems = RES_FAX_MODEM,
  303. .ecm = AST_FAX_OPTFLAG_TRUE,
  304. };
  305. AST_RWLOCK_DEFINE_STATIC(options_lock);
  306. static void get_general_options(struct fax_options* options);
  307. static void set_general_options(const struct fax_options* options);
  308. static const char *config = "res_fax.conf";
  309. static int global_fax_debug = 0;
  310. enum {
  311. OPT_CALLEDMODE = (1 << 0),
  312. OPT_CALLERMODE = (1 << 1),
  313. OPT_DEBUG = (1 << 2),
  314. OPT_STATUS = (1 << 3),
  315. OPT_ALLOWAUDIO = (1 << 5),
  316. OPT_REQUEST_T38 = (1 << 6),
  317. OPT_FORCE_AUDIO = (1 << 7),
  318. };
  319. AST_APP_OPTIONS(fax_exec_options, BEGIN_OPTIONS
  320. AST_APP_OPTION('a', OPT_CALLEDMODE),
  321. AST_APP_OPTION('c', OPT_CALLERMODE),
  322. AST_APP_OPTION('d', OPT_DEBUG),
  323. AST_APP_OPTION('f', OPT_ALLOWAUDIO),
  324. AST_APP_OPTION('F', OPT_FORCE_AUDIO),
  325. AST_APP_OPTION('s', OPT_STATUS),
  326. AST_APP_OPTION('z', OPT_REQUEST_T38),
  327. END_OPTIONS);
  328. struct manager_event_info {
  329. char context[AST_MAX_CONTEXT];
  330. char exten[AST_MAX_EXTENSION];
  331. char cid[128];
  332. };
  333. static void debug_check_frame_for_silence(struct ast_fax_session *s, unsigned int c2s, struct ast_frame *frame)
  334. {
  335. struct debug_info_history *history = c2s ? &s->debug_info->c2s : &s->debug_info->s2c;
  336. int dspsilence;
  337. unsigned int last_consec_frames, last_consec_ms;
  338. unsigned char wassil;
  339. struct timeval diff;
  340. diff = ast_tvsub(ast_tvnow(), s->debug_info->base_tv);
  341. ast_dsp_reset(s->debug_info->dsp);
  342. ast_dsp_silence(s->debug_info->dsp, frame, &dspsilence);
  343. wassil = history->silence;
  344. history->silence = (dspsilence != 0) ? 1 : 0;
  345. if (history->silence != wassil) {
  346. last_consec_frames = history->consec_frames;
  347. last_consec_ms = history->consec_ms;
  348. history->consec_frames = 0;
  349. history->consec_ms = 0;
  350. if ((last_consec_frames != 0)) {
  351. ast_verb(6, "Channel '%s' fax session '%d', [ %.3ld.%.6ld ], %s sent %d frames (%d ms) of %s.\n",
  352. s->channame, s->id, (long) diff.tv_sec, (long int) diff.tv_usec,
  353. (c2s) ? "channel" : "stack", last_consec_frames, last_consec_ms,
  354. (wassil) ? "silence" : "energy");
  355. }
  356. }
  357. history->consec_frames++;
  358. history->consec_ms += (frame->samples / 8);
  359. }
  360. static void destroy_callback(void *data)
  361. {
  362. if (data) {
  363. ao2_ref(data, -1);
  364. }
  365. }
  366. static const struct ast_datastore_info fax_datastore = {
  367. .type = "res_fax",
  368. .destroy = destroy_callback,
  369. };
  370. /*! \brief returns a reference counted pointer to a fax datastore, if it exists */
  371. static struct ast_fax_session_details *find_details(struct ast_channel *chan)
  372. {
  373. struct ast_fax_session_details *details;
  374. struct ast_datastore *datastore;
  375. ast_channel_lock(chan);
  376. if (!(datastore = ast_channel_datastore_find(chan, &fax_datastore, NULL))) {
  377. ast_channel_unlock(chan);
  378. return NULL;
  379. }
  380. if (!(details = datastore->data)) {
  381. ast_log(LOG_WARNING, "Huh? channel '%s' has a FAX datastore without data!\n", chan->name);
  382. ast_channel_unlock(chan);
  383. return NULL;
  384. }
  385. ao2_ref(details, 1);
  386. ast_channel_unlock(chan);
  387. return details;
  388. }
  389. /*! \brief destroy a FAX session details structure */
  390. static void destroy_session_details(void *details)
  391. {
  392. struct ast_fax_session_details *d = details;
  393. struct ast_fax_document *doc;
  394. while ((doc = AST_LIST_REMOVE_HEAD(&d->documents, next))) {
  395. ast_free(doc);
  396. }
  397. ast_string_field_free_memory(d);
  398. }
  399. /*! \brief create a FAX session details structure */
  400. static struct ast_fax_session_details *session_details_new(void)
  401. {
  402. struct ast_fax_session_details *d;
  403. struct fax_options options;
  404. if (!(d = ao2_alloc(sizeof(*d), destroy_session_details))) {
  405. return NULL;
  406. }
  407. if (ast_string_field_init(d, 512)) {
  408. ao2_ref(d, -1);
  409. return NULL;
  410. }
  411. get_general_options(&options);
  412. AST_LIST_HEAD_INIT_NOLOCK(&d->documents);
  413. /* These options need to be set to the configured default and may be overridden by
  414. * SendFAX, ReceiveFAX, or FAXOPT */
  415. d->option.request_t38 = AST_FAX_OPTFLAG_FALSE;
  416. d->option.send_cng = AST_FAX_OPTFLAG_FALSE;
  417. d->option.send_ced = AST_FAX_OPTFLAG_FALSE;
  418. d->option.ecm = options.ecm;
  419. d->option.statusevents = options.statusevents;
  420. d->modems = options.modems;
  421. d->minrate = options.minrate;
  422. d->maxrate = options.maxrate;
  423. d->gateway_id = -1;
  424. d->gateway_timeout = 0;
  425. return d;
  426. }
  427. static struct ast_control_t38_parameters our_t38_parameters = {
  428. .version = 0,
  429. .max_ifp = 400,
  430. .rate = AST_T38_RATE_14400,
  431. .rate_management = AST_T38_RATE_MANAGEMENT_TRANSFERRED_TCF,
  432. };
  433. static void t38_parameters_ast_to_fax(struct ast_fax_t38_parameters *dst, const struct ast_control_t38_parameters *src)
  434. {
  435. dst->version = src->version;
  436. dst->max_ifp = src->max_ifp;
  437. dst->rate = src->rate;
  438. dst->rate_management = src->rate_management;
  439. dst->fill_bit_removal = src->fill_bit_removal;
  440. dst->transcoding_mmr = src->transcoding_mmr;
  441. dst->transcoding_jbig = src->transcoding_jbig;
  442. }
  443. static void t38_parameters_fax_to_ast(struct ast_control_t38_parameters *dst, const struct ast_fax_t38_parameters *src)
  444. {
  445. dst->version = src->version;
  446. dst->max_ifp = src->max_ifp;
  447. dst->rate = src->rate;
  448. dst->rate_management = src->rate_management;
  449. dst->fill_bit_removal = src->fill_bit_removal;
  450. dst->transcoding_mmr = src->transcoding_mmr;
  451. dst->transcoding_jbig = src->transcoding_jbig;
  452. }
  453. /*! \brief returns a reference counted details structure from the channel's fax datastore. If the datastore
  454. * does not exist it will be created */
  455. static struct ast_fax_session_details *find_or_create_details(struct ast_channel *chan)
  456. {
  457. struct ast_fax_session_details *details;
  458. struct ast_datastore *datastore;
  459. if ((details = find_details(chan))) {
  460. return details;
  461. }
  462. /* channel does not have one so we must create one */
  463. if (!(details = session_details_new())) {
  464. ast_log(LOG_WARNING, "channel '%s' can't get a FAX details structure for the datastore!\n", chan->name);
  465. return NULL;
  466. }
  467. if (!(datastore = ast_datastore_alloc(&fax_datastore, NULL))) {
  468. ao2_ref(details, -1);
  469. ast_log(LOG_WARNING, "channel '%s' can't get a datastore!\n", chan->name);
  470. return NULL;
  471. }
  472. /* add the datastore to the channel and increment the refcount */
  473. datastore->data = details;
  474. /* initialize default T.38 parameters */
  475. t38_parameters_ast_to_fax(&details->our_t38_parameters, &our_t38_parameters);
  476. t38_parameters_ast_to_fax(&details->their_t38_parameters, &our_t38_parameters);
  477. ao2_ref(details, 1);
  478. ast_channel_lock(chan);
  479. ast_channel_datastore_add(chan, datastore);
  480. ast_channel_unlock(chan);
  481. return details;
  482. }
  483. unsigned int ast_fax_maxrate(void)
  484. {
  485. struct fax_options options;
  486. get_general_options(&options);
  487. return options.maxrate;
  488. }
  489. unsigned int ast_fax_minrate(void)
  490. {
  491. struct fax_options options;
  492. get_general_options(&options);
  493. return options.minrate;
  494. }
  495. static int update_modem_bits(enum ast_fax_modems *bits, const char *value)
  496. {
  497. char *m[5], *tok, *v = (char *)value;
  498. int i = 0, j;
  499. if (!strchr(v, ',')) {
  500. m[i++] = v;
  501. m[i] = NULL;
  502. } else {
  503. tok = strtok(v, ", ");
  504. while (tok && (i < 5)) {
  505. m[i++] = tok;
  506. tok = strtok(NULL, ", ");
  507. }
  508. m[i] = NULL;
  509. }
  510. *bits = 0;
  511. for (j = 0; j < i; j++) {
  512. if (!strcasecmp(m[j], "v17")) {
  513. *bits |= AST_FAX_MODEM_V17;
  514. } else if (!strcasecmp(m[j], "v27")) {
  515. *bits |= AST_FAX_MODEM_V27;
  516. } else if (!strcasecmp(m[j], "v29")) {
  517. *bits |= AST_FAX_MODEM_V29;
  518. } else if (!strcasecmp(m[j], "v34")) {
  519. *bits |= AST_FAX_MODEM_V34;
  520. } else {
  521. ast_log(LOG_WARNING, "ignoring invalid modem setting: '%s', valid options {v17 | v27 | v29 | v34}\n", m[j]);
  522. }
  523. }
  524. return 0;
  525. }
  526. static char *ast_fax_caps_to_str(enum ast_fax_capabilities caps, char *buf, size_t bufsize)
  527. {
  528. char *out = buf;
  529. size_t size = bufsize;
  530. int first = 1;
  531. if (caps & AST_FAX_TECH_SEND) {
  532. if (!first) {
  533. ast_build_string(&buf, &size, ",");
  534. }
  535. ast_build_string(&buf, &size, "SEND");
  536. first = 0;
  537. }
  538. if (caps & AST_FAX_TECH_RECEIVE) {
  539. if (!first) {
  540. ast_build_string(&buf, &size, ",");
  541. }
  542. ast_build_string(&buf, &size, "RECEIVE");
  543. first = 0;
  544. }
  545. if (caps & AST_FAX_TECH_AUDIO) {
  546. if (!first) {
  547. ast_build_string(&buf, &size, ",");
  548. }
  549. ast_build_string(&buf, &size, "AUDIO");
  550. first = 0;
  551. }
  552. if (caps & AST_FAX_TECH_T38) {
  553. if (!first) {
  554. ast_build_string(&buf, &size, ",");
  555. }
  556. ast_build_string(&buf, &size, "T38");
  557. first = 0;
  558. }
  559. if (caps & AST_FAX_TECH_MULTI_DOC) {
  560. if (!first) {
  561. ast_build_string(&buf, &size, ",");
  562. }
  563. ast_build_string(&buf, &size, "MULTI_DOC");
  564. first = 0;
  565. }
  566. if (caps & AST_FAX_TECH_GATEWAY) {
  567. if (!first) {
  568. ast_build_string(&buf, &size, ",");
  569. }
  570. ast_build_string(&buf, &size, "GATEWAY");
  571. first = 0;
  572. }
  573. if (caps & AST_FAX_TECH_V21_DETECT) {
  574. if (!first) {
  575. ast_build_string(&buf, &size, ",");
  576. }
  577. ast_build_string(&buf, &size, "V21");
  578. first = 0;
  579. }
  580. return out;
  581. }
  582. static int ast_fax_modem_to_str(enum ast_fax_modems bits, char *tbuf, size_t bufsize)
  583. {
  584. int count = 0;
  585. if (bits & AST_FAX_MODEM_V17) {
  586. strcat(tbuf, "V17");
  587. count++;
  588. }
  589. if (bits & AST_FAX_MODEM_V27) {
  590. if (count) {
  591. strcat(tbuf, ",");
  592. }
  593. strcat(tbuf, "V27");
  594. count++;
  595. }
  596. if (bits & AST_FAX_MODEM_V29) {
  597. if (count) {
  598. strcat(tbuf, ",");
  599. }
  600. strcat(tbuf, "V29");
  601. count++;
  602. }
  603. if (bits & AST_FAX_MODEM_V34) {
  604. if (count) {
  605. strcat(tbuf, ",");
  606. }
  607. strcat(tbuf, "V34");
  608. count++;
  609. }
  610. return 0;
  611. }
  612. static int check_modem_rate(enum ast_fax_modems modems, unsigned int rate)
  613. {
  614. switch (rate) {
  615. case 2400:
  616. if (!(modems & (AST_FAX_MODEM_V27 | AST_FAX_MODEM_V34))) {
  617. return 1;
  618. }
  619. break;
  620. case 4800:
  621. if (!(modems & (AST_FAX_MODEM_V27 | AST_FAX_MODEM_V34))) {
  622. return 1;
  623. }
  624. break;
  625. case 7200:
  626. case 9600:
  627. if (!(modems & (AST_FAX_MODEM_V17 | AST_FAX_MODEM_V29 | AST_FAX_MODEM_V34))) {
  628. return 1;
  629. }
  630. break;
  631. case 12000:
  632. case 14400:
  633. if (!(modems & (AST_FAX_MODEM_V17 | AST_FAX_MODEM_V34))) {
  634. return 1;
  635. }
  636. break;
  637. case 28800:
  638. case 33600:
  639. if (!(modems & AST_FAX_MODEM_V34)) {
  640. return 1;
  641. }
  642. break;
  643. default:
  644. /* this should never happen */
  645. return 1;
  646. }
  647. return 0;
  648. }
  649. /*! \brief register a FAX technology module */
  650. int ast_fax_tech_register(struct ast_fax_tech *tech)
  651. {
  652. struct fax_module *fax;
  653. if (!(fax = ast_calloc(1, sizeof(*fax)))) {
  654. return -1;
  655. }
  656. fax->tech = tech;
  657. AST_RWLIST_WRLOCK(&faxmodules);
  658. AST_RWLIST_INSERT_TAIL(&faxmodules, fax, list);
  659. AST_RWLIST_UNLOCK(&faxmodules);
  660. ast_module_ref(ast_module_info->self);
  661. ast_verb(3, "Registered handler for '%s' (%s)\n", fax->tech->type, fax->tech->description);
  662. return 0;
  663. }
  664. /*! \brief unregister a FAX technology module */
  665. void ast_fax_tech_unregister(struct ast_fax_tech *tech)
  666. {
  667. struct fax_module *fax;
  668. ast_verb(3, "Unregistering FAX module type '%s'\n", tech->type);
  669. AST_RWLIST_WRLOCK(&faxmodules);
  670. AST_RWLIST_TRAVERSE_SAFE_BEGIN(&faxmodules, fax, list) {
  671. if (fax->tech != tech) {
  672. continue;
  673. }
  674. AST_RWLIST_REMOVE_CURRENT(list);
  675. ast_module_unref(ast_module_info->self);
  676. ast_free(fax);
  677. ast_verb(4, "Unregistered FAX module type '%s'\n", tech->type);
  678. break;
  679. }
  680. AST_RWLIST_TRAVERSE_SAFE_END;
  681. AST_RWLIST_UNLOCK(&faxmodules);
  682. }
  683. /*! \brief convert a ast_fax_state to a string */
  684. const char *ast_fax_state_to_str(enum ast_fax_state state)
  685. {
  686. switch (state) {
  687. case AST_FAX_STATE_UNINITIALIZED:
  688. return "Uninitialized";
  689. case AST_FAX_STATE_INITIALIZED:
  690. return "Initialized";
  691. case AST_FAX_STATE_OPEN:
  692. return "Open";
  693. case AST_FAX_STATE_ACTIVE:
  694. return "Active";
  695. case AST_FAX_STATE_COMPLETE:
  696. return "Complete";
  697. case AST_FAX_STATE_RESERVED:
  698. return "Reserved";
  699. case AST_FAX_STATE_INACTIVE:
  700. return "Inactive";
  701. default:
  702. ast_log(LOG_WARNING, "unhandled FAX state: %d\n", state);
  703. return "Unknown";
  704. }
  705. }
  706. void ast_fax_log(int level, const char *file, const int line, const char *function, const char *msg)
  707. {
  708. if (fax_logger_level != -1) {
  709. ast_log_dynamic_level(fax_logger_level, "%s", msg);
  710. } else {
  711. ast_log(level, file, line, function, "%s", msg);
  712. }
  713. }
  714. /*! \brief convert a rate string to a rate */
  715. static unsigned int fax_rate_str_to_int(const char *ratestr)
  716. {
  717. int rate;
  718. if (sscanf(ratestr, "%d", &rate) != 1) {
  719. ast_log(LOG_ERROR, "failed to sscanf '%s' to rate\n", ratestr);
  720. return 0;
  721. }
  722. switch (rate) {
  723. case 2400:
  724. case 4800:
  725. case 7200:
  726. case 9600:
  727. case 12000:
  728. case 14400:
  729. case 28800:
  730. case 33600:
  731. return rate;
  732. default:
  733. ast_log(LOG_WARNING, "ignoring invalid rate '%s'. Valid options are {2400 | 4800 | 7200 | 9600 | 12000 | 14400 | 28800 | 33600}\n", ratestr);
  734. return 0;
  735. }
  736. }
  737. /*! \brief Release a session token.
  738. * \param s a session returned from fax_session_reserve()
  739. * \param token a token generated from fax_session_reserve()
  740. *
  741. * This function releases the given token and marks the given session as no
  742. * longer reserved. It is safe to call on a session that is not actually
  743. * reserved and with a NULL token. This is so that sessions returned by
  744. * technologies that do not support reserved sessions don't require extra logic
  745. * to handle.
  746. *
  747. * \note This function DOES NOT release the given fax session, only the given
  748. * token.
  749. */
  750. static void fax_session_release(struct ast_fax_session *s, struct ast_fax_tech_token *token)
  751. {
  752. if (token) {
  753. s->tech->release_token(token);
  754. }
  755. if (s->state == AST_FAX_STATE_RESERVED) {
  756. ast_atomic_fetchadd_int(&faxregistry.reserved_sessions, -1);
  757. s->state = AST_FAX_STATE_INACTIVE;
  758. }
  759. }
  760. /*! \brief destroy a FAX session structure */
  761. static void destroy_session(void *session)
  762. {
  763. struct ast_fax_session *s = session;
  764. if (s->tech) {
  765. fax_session_release(s, NULL);
  766. if (s->tech_pvt) {
  767. s->tech->destroy_session(s);
  768. }
  769. ast_module_unref(s->tech->module);
  770. }
  771. if (s->details) {
  772. if (s->details->caps & AST_FAX_TECH_GATEWAY) {
  773. s->details->caps &= ~AST_FAX_TECH_GATEWAY;
  774. }
  775. ao2_ref(s->details, -1);
  776. }
  777. if (s->debug_info) {
  778. ast_dsp_free(s->debug_info->dsp);
  779. ast_free(s->debug_info);
  780. }
  781. if (s->smoother) {
  782. ast_smoother_free(s->smoother);
  783. }
  784. if (s->state != AST_FAX_STATE_INACTIVE) {
  785. ast_atomic_fetchadd_int(&faxregistry.active_sessions, -1);
  786. }
  787. ast_free(s->channame);
  788. ast_free(s->chan_uniqueid);
  789. }
  790. /*! \brief Reserve a fax session.
  791. * \param details the fax session details
  792. * \param token a pointer to a place to store a token to be passed to fax_session_new() later
  793. *
  794. * This function reserves a fax session for use later. If the selected fax
  795. * technology does not support reserving sessions a session will still be
  796. * returned but token will not be set.
  797. *
  798. * \note The reference returned by this function does not get consumed by
  799. * fax_session_new() and must always be dereferenced separately.
  800. *
  801. * \return NULL or an uninitialized and possibly reserved session
  802. */
  803. static struct ast_fax_session *fax_session_reserve(struct ast_fax_session_details *details, struct ast_fax_tech_token **token)
  804. {
  805. struct ast_fax_session *s;
  806. struct fax_module *faxmod;
  807. if (!(s = ao2_alloc(sizeof(*s), destroy_session))) {
  808. return NULL;
  809. }
  810. s->state = AST_FAX_STATE_INACTIVE;
  811. s->details = details;
  812. ao2_ref(s->details, 1);
  813. /* locate a FAX technology module that can handle said requirements
  814. * Note: the requirements have not yet been finalized as T.38
  815. * negotiation has not yet occured. */
  816. AST_RWLIST_RDLOCK(&faxmodules);
  817. AST_RWLIST_TRAVERSE(&faxmodules, faxmod, list) {
  818. if ((faxmod->tech->caps & details->caps) != details->caps) {
  819. continue;
  820. }
  821. ast_debug(4, "Reserving a FAX session from '%s'.\n", faxmod->tech->description);
  822. ast_module_ref(faxmod->tech->module);
  823. s->tech = faxmod->tech;
  824. break;
  825. }
  826. AST_RWLIST_UNLOCK(&faxmodules);
  827. if (!faxmod) {
  828. char caps[128] = "";
  829. ast_log(LOG_ERROR, "Could not locate a FAX technology module with capabilities (%s)\n", ast_fax_caps_to_str(details->caps, caps, sizeof(caps)));
  830. ao2_ref(s, -1);
  831. return NULL;
  832. }
  833. if (!s->tech->reserve_session) {
  834. ast_debug(1, "Selected FAX technology module (%s) does not support reserving sessions.\n", s->tech->description);
  835. return s;
  836. }
  837. if (!(*token = s->tech->reserve_session(s))) {
  838. ao2_ref(s, -1);
  839. return NULL;
  840. }
  841. s->state = AST_FAX_STATE_RESERVED;
  842. ast_atomic_fetchadd_int(&faxregistry.reserved_sessions, 1);
  843. return s;
  844. }
  845. /*! \brief create a FAX session
  846. *
  847. * \param details details for the session
  848. * \param chan the channel the session will run on
  849. * \param reserved a reserved session to base this session on (can be NULL)
  850. * \param token the token for a reserved session (can be NULL)
  851. *
  852. * Create a new fax session based on the given details structure.
  853. *
  854. * \note The given token is always consumed (by tech->new_session() or by
  855. * fax_session_release() in the event of a failure). The given reference to a
  856. * reserved session is never consumed and must be dereferenced separately from
  857. * the reference returned by this function.
  858. *
  859. * \return NULL or a reference to a new fax session
  860. */
  861. static struct ast_fax_session *fax_session_new(struct ast_fax_session_details *details, struct ast_channel *chan, struct ast_fax_session *reserved, struct ast_fax_tech_token *token)
  862. {
  863. struct ast_fax_session *s = NULL;
  864. struct fax_module *faxmod;
  865. if (reserved) {
  866. s = reserved;
  867. ao2_ref(reserved, +1);
  868. /* NOTE: we don't consume the reference to the reserved
  869. * session. The session returned from fax_session_new() is a
  870. * new reference and must be derefed in addition to the
  871. * reserved session.
  872. */
  873. if (s->state == AST_FAX_STATE_RESERVED) {
  874. ast_atomic_fetchadd_int(&faxregistry.reserved_sessions, -1);
  875. s->state = AST_FAX_STATE_UNINITIALIZED;
  876. }
  877. }
  878. if (!s && !(s = ao2_alloc(sizeof(*s), destroy_session))) {
  879. return NULL;
  880. }
  881. ast_atomic_fetchadd_int(&faxregistry.active_sessions, 1);
  882. s->state = AST_FAX_STATE_UNINITIALIZED;
  883. if (details->option.debug && (details->caps & AST_FAX_TECH_AUDIO)) {
  884. if (!(s->debug_info = ast_calloc(1, sizeof(*(s->debug_info))))) {
  885. fax_session_release(s, token);
  886. ao2_ref(s, -1);
  887. return NULL;
  888. }
  889. if (!(s->debug_info->dsp = ast_dsp_new())) {
  890. ast_free(s->debug_info);
  891. s->debug_info = NULL;
  892. fax_session_release(s, token);
  893. ao2_ref(s, -1);
  894. return NULL;
  895. }
  896. ast_dsp_set_threshold(s->debug_info->dsp, 128);
  897. }
  898. if (!(s->channame = ast_strdup(chan->name))) {
  899. fax_session_release(s, token);
  900. ao2_ref(s, -1);
  901. return NULL;
  902. }
  903. if (!(s->chan_uniqueid = ast_strdup(chan->uniqueid))) {
  904. fax_session_release(s, token);
  905. ao2_ref(s, -1);
  906. return NULL;
  907. }
  908. s->chan = chan;
  909. if (!s->details) {
  910. s->details = details;
  911. ao2_ref(s->details, 1);
  912. }
  913. details->id = s->id = ast_atomic_fetchadd_int(&faxregistry.nextsessionname, 1);
  914. if (!token) {
  915. /* locate a FAX technology module that can handle said requirements */
  916. AST_RWLIST_RDLOCK(&faxmodules);
  917. AST_RWLIST_TRAVERSE(&faxmodules, faxmod, list) {
  918. if ((faxmod->tech->caps & details->caps) != details->caps) {
  919. continue;
  920. }
  921. ast_debug(4, "Requesting a new FAX session from '%s'.\n", faxmod->tech->description);
  922. ast_module_ref(faxmod->tech->module);
  923. s->tech = faxmod->tech;
  924. break;
  925. }
  926. AST_RWLIST_UNLOCK(&faxmodules);
  927. if (!faxmod) {
  928. char caps[128] = "";
  929. ast_log(LOG_ERROR, "Could not locate a FAX technology module with capabilities (%s)\n", ast_fax_caps_to_str(details->caps, caps, sizeof(caps)));
  930. ao2_ref(s, -1);
  931. return NULL;
  932. }
  933. }
  934. if (!(s->tech_pvt = s->tech->new_session(s, token))) {
  935. ast_log(LOG_ERROR, "FAX session failed to initialize.\n");
  936. ao2_ref(s, -1);
  937. return NULL;
  938. }
  939. /* link the session to the session container */
  940. if (!(ao2_link(faxregistry.container, s))) {
  941. ast_log(LOG_ERROR, "failed to add FAX session '%d' to container.\n", s->id);
  942. ao2_ref(s, -1);
  943. return NULL;
  944. }
  945. ast_debug(4, "channel '%s' using FAX session '%d'\n", s->channame, s->id);
  946. return s;
  947. }
  948. static void get_manager_event_info(struct ast_channel *chan, struct manager_event_info *info)
  949. {
  950. pbx_substitute_variables_helper(chan, "${CONTEXT}", info->context, sizeof(info->context));
  951. pbx_substitute_variables_helper(chan, "${EXTEN}", info->exten, sizeof(info->exten));
  952. pbx_substitute_variables_helper(chan, "${CALLERID(num)}", info->cid, sizeof(info->cid));
  953. }
  954. /* \brief Generate a string of filenames using the given prefix and separator.
  955. * \param details the fax session details
  956. * \param prefix the prefix to each filename
  957. * \param separator the separator between filenames
  958. *
  959. * This function generates a string of filenames from the given details
  960. * structure and using the given prefix and separator.
  961. *
  962. * \retval NULL there was an error generating the string
  963. * \return the string generated string
  964. */
  965. static char *generate_filenames_string(struct ast_fax_session_details *details, char *prefix, char *separator)
  966. {
  967. char *filenames, *c;
  968. size_t size = 0;
  969. int first = 1;
  970. struct ast_fax_document *doc;
  971. /* don't process empty lists */
  972. if (AST_LIST_EMPTY(&details->documents)) {
  973. return NULL;
  974. }
  975. /* Calculate the total length of all of the file names */
  976. AST_LIST_TRAVERSE(&details->documents, doc, next) {
  977. size += strlen(separator) + strlen(prefix) + strlen(doc->filename);
  978. }
  979. size += 1; /* add space for the terminating null */
  980. if (!(filenames = ast_malloc(size))) {
  981. return NULL;
  982. }
  983. c = filenames;
  984. ast_build_string(&c, &size, "%s%s", prefix, AST_LIST_FIRST(&details->documents)->filename);
  985. AST_LIST_TRAVERSE(&details->documents, doc, next) {
  986. if (first) {
  987. first = 0;
  988. continue;
  989. }
  990. ast_build_string(&c, &size, "%s%s%s", separator, prefix, doc->filename);
  991. }
  992. return filenames;
  993. }
  994. /*! \brief send a FAX status manager event */
  995. static int report_fax_status(struct ast_channel *chan, struct ast_fax_session_details *details, const char *status)
  996. {
  997. char *filenames = generate_filenames_string(details, "FileName: ", "\r\n");
  998. ast_channel_lock(chan);
  999. if (details->option.statusevents) {
  1000. struct manager_event_info info;
  1001. get_manager_event_info(chan, &info);
  1002. manager_event(EVENT_FLAG_CALL,
  1003. "FAXStatus",
  1004. "Operation: %s\r\n"
  1005. "Status: %s\r\n"
  1006. "Channel: %s\r\n"
  1007. "Context: %s\r\n"
  1008. "Exten: %s\r\n"
  1009. "CallerID: %s\r\n"
  1010. "LocalStationID: %s\r\n"
  1011. "%s%s",
  1012. (details->caps & AST_FAX_TECH_GATEWAY) ? "gateway" : (details->caps & AST_FAX_TECH_RECEIVE) ? "receive" : "send",
  1013. status,
  1014. chan->name,
  1015. info.context,
  1016. info.exten,
  1017. info.cid,
  1018. details->localstationid,
  1019. S_OR(filenames, ""),
  1020. filenames ? "\r\n" : "");
  1021. }
  1022. ast_channel_unlock(chan);
  1023. if (filenames) {
  1024. ast_free(filenames);
  1025. }
  1026. return 0;
  1027. }
  1028. /*! \brief Set fax related channel variables. */
  1029. static void set_channel_variables(struct ast_channel *chan, struct ast_fax_session_details *details)
  1030. {
  1031. char buf[10];
  1032. pbx_builtin_setvar_helper(chan, "FAXSTATUS", S_OR(details->result, NULL));
  1033. pbx_builtin_setvar_helper(chan, "FAXERROR", S_OR(details->error, NULL));
  1034. pbx_builtin_setvar_helper(chan, "FAXSTATUSSTRING", S_OR(details->resultstr, NULL));
  1035. pbx_builtin_setvar_helper(chan, "REMOTESTATIONID", S_OR(details->remotestationid, NULL));
  1036. pbx_builtin_setvar_helper(chan, "LOCALSTATIONID", S_OR(details->localstationid, NULL));
  1037. pbx_builtin_setvar_helper(chan, "FAXBITRATE", S_OR(details->transfer_rate, NULL));
  1038. pbx_builtin_setvar_helper(chan, "FAXRESOLUTION", S_OR(details->resolution, NULL));
  1039. snprintf(buf, sizeof(buf), "%d", details->pages_transferred);
  1040. pbx_builtin_setvar_helper(chan, "FAXPAGES", buf);
  1041. }
  1042. #define GENERIC_FAX_EXEC_SET_VARS(fax, chan, errorstr, reason) \
  1043. do { \
  1044. if (ast_strlen_zero(fax->details->result)) \
  1045. ast_string_field_set(fax->details, result, "FAILED"); \
  1046. if (ast_strlen_zero(fax->details->resultstr)) \
  1047. ast_string_field_set(fax->details, resultstr, reason); \
  1048. if (ast_strlen_zero(fax->details->error)) \
  1049. ast_string_field_set(fax->details, error, errorstr); \
  1050. set_channel_variables(chan, fax->details); \
  1051. } while (0)
  1052. #define GENERIC_FAX_EXEC_ERROR_QUIET(fax, chan, errorstr, reason) \
  1053. do { \
  1054. GENERIC_FAX_EXEC_SET_VARS(fax, chan, errorstr, reason); \
  1055. } while (0)
  1056. #define GENERIC_FAX_EXEC_ERROR(fax, chan, errorstr, reason) \
  1057. do { \
  1058. ast_log(LOG_ERROR, "channel '%s' FAX session '%d' failure, reason: '%s' (%s)\n", chan->name, fax->id, reason, errorstr); \
  1059. GENERIC_FAX_EXEC_ERROR_QUIET(fax, chan, errorstr, reason); \
  1060. } while (0)
  1061. static int set_fax_t38_caps(struct ast_channel *chan, struct ast_fax_session_details *details)
  1062. {
  1063. switch (ast_channel_get_t38_state(chan)) {
  1064. case T38_STATE_UNKNOWN:
  1065. details->caps |= AST_FAX_TECH_T38;
  1066. break;
  1067. case T38_STATE_REJECTED:
  1068. case T38_STATE_UNAVAILABLE:
  1069. details->caps |= AST_FAX_TECH_AUDIO;
  1070. break;
  1071. case T38_STATE_NEGOTIATED:
  1072. /* already in T.38 mode? This should not happen. */
  1073. case T38_STATE_NEGOTIATING: {
  1074. /* the other end already sent us a T.38 reinvite, so we need to prod the channel
  1075. * driver into resending their parameters to us if it supports doing so... if
  1076. * not, we can't proceed, because we can't create a proper reply without them.
  1077. * if it does work, the channel driver will send an AST_CONTROL_T38_PARAMETERS
  1078. * with a request of AST_T38_REQUEST_NEGOTIATE, which will be read by the function
  1079. * that gets called after this one completes
  1080. */
  1081. struct ast_control_t38_parameters parameters = { .request_response = AST_T38_REQUEST_PARMS, };
  1082. if (ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &parameters, sizeof(parameters)) != AST_T38_REQUEST_PARMS) {
  1083. ast_log(LOG_ERROR, "channel '%s' is in an unsupported T.38 negotiation state, cannot continue.\n", chan->name);
  1084. return -1;
  1085. }
  1086. details->caps |= AST_FAX_TECH_T38;
  1087. break;
  1088. }
  1089. default:
  1090. ast_log(LOG_ERROR, "channel '%s' is in an unsupported T.38 negotiation state, cannot continue.\n", chan->name);
  1091. return -1;
  1092. }
  1093. return 0;
  1094. }
  1095. static int disable_t38(struct ast_channel *chan)
  1096. {
  1097. int timeout_ms;
  1098. struct ast_frame *frame = NULL;
  1099. struct ast_control_t38_parameters t38_parameters = { .request_response = AST_T38_REQUEST_TERMINATE, };
  1100. struct timeval start;
  1101. int ms;
  1102. ast_debug(1, "Shutting down T.38 on %s\n", chan->name);
  1103. if (ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters)) != 0) {
  1104. ast_debug(1, "error while disabling T.38 on channel '%s'\n", chan->name);
  1105. return -1;
  1106. }
  1107. /* wait up to five seconds for negotiation to complete */
  1108. timeout_ms = 5000;
  1109. start = ast_tvnow();
  1110. while ((ms = ast_remaining_ms(start, timeout_ms))) {
  1111. ms = ast_waitfor(chan, ms);
  1112. if (ms == 0) {
  1113. break;
  1114. }
  1115. if (ms < 0) {
  1116. ast_debug(1, "error while disabling T.38 on channel '%s'\n", chan->name);
  1117. return -1;
  1118. }
  1119. if (!(frame = ast_read(chan))) {
  1120. return -1;
  1121. }
  1122. if ((frame->frametype == AST_FRAME_CONTROL) &&
  1123. (frame->subclass.integer == AST_CONTROL_T38_PARAMETERS) &&
  1124. (frame->datalen == sizeof(t38_parameters))) {
  1125. struct ast_control_t38_parameters *parameters = frame->data.ptr;
  1126. switch (parameters->request_response) {
  1127. case AST_T38_TERMINATED:
  1128. ast_debug(1, "Shut down T.38 on %s\n", chan->name);
  1129. break;
  1130. case AST_T38_REFUSED:
  1131. ast_log(LOG_WARNING, "channel '%s' refused to disable T.38\n", chan->name);
  1132. ast_frfree(frame);
  1133. return -1;
  1134. default:
  1135. ast_log(LOG_ERROR, "channel '%s' failed to disable T.38\n", chan->name);
  1136. ast_frfree(frame);
  1137. return -1;
  1138. }
  1139. ast_frfree(frame);
  1140. break;
  1141. }
  1142. ast_frfree(frame);
  1143. }
  1144. if (ms == 0) { /* all done, nothing happened */
  1145. ast_debug(1, "channel '%s' timed-out during T.38 shutdown\n", chan->name);
  1146. }
  1147. return 0;
  1148. }
  1149. /*! \brief this is the generic FAX session handling function */
  1150. static int generic_fax_exec(struct ast_channel *chan, struct ast_fax_session_details *details, struct ast_fax_session *reserved, struct ast_fax_tech_token *token)
  1151. {
  1152. int ms;
  1153. int timeout = RES_FAX_TIMEOUT;
  1154. int chancount;
  1155. unsigned int expected_frametype = -1;
  1156. union ast_frame_subclass expected_framesubclass = { .integer = -1 };
  1157. unsigned int t38negotiated = (ast_channel_get_t38_state(chan) == T38_STATE_NEGOTIATED);
  1158. struct ast_control_t38_parameters t38_parameters;
  1159. const char *tempvar;
  1160. struct ast_fax_session *fax = NULL;
  1161. struct ast_frame *frame = NULL;
  1162. struct ast_channel *c = chan;
  1163. struct ast_format orig_write_format;
  1164. struct ast_format orig_read_format;
  1165. int remaining_time;
  1166. struct timeval start;
  1167. ast_format_clear(&orig_write_format);
  1168. ast_format_clear(&orig_read_format);
  1169. chancount = 1;
  1170. /* create the FAX session */
  1171. if (!(fax = fax_session_new(details, chan, reserved, token))) {
  1172. ast_log(LOG_ERROR, "Can't create a FAX session, FAX attempt failed.\n");
  1173. report_fax_status(chan, details, "No Available Resource");
  1174. return -1;
  1175. }
  1176. ast_channel_lock(chan);
  1177. /* update session details */
  1178. if (ast_strlen_zero(details->headerinfo) && (tempvar = pbx_builtin_getvar_helper(chan, "LOCALHEADERINFO"))) {
  1179. ast_string_field_set(details, headerinfo, tempvar);
  1180. }
  1181. if (ast_strlen_zero(details->localstationid)) {
  1182. tempvar = pbx_builtin_getvar_helper(chan, "LOCALSTATIONID");
  1183. ast_string_field_set(details, localstationid, tempvar ? tempvar : "unknown");
  1184. }
  1185. ast_channel_unlock(chan);
  1186. report_fax_status(chan, details, "Allocating Resources");
  1187. if (details->caps & AST_FAX_TECH_AUDIO) {
  1188. expected_frametype = AST_FRAME_VOICE;;
  1189. ast_format_set(&expected_framesubclass.format, AST_FORMAT_SLINEAR, 0);
  1190. ast_format_copy(&orig_write_format, &chan->writeformat);
  1191. if (ast_set_write_format_by_id(chan, AST_FORMAT_SLINEAR) < 0) {
  1192. ast_log(LOG_ERROR, "channel '%s' failed to set write format to signed linear'.\n", chan->name);
  1193. ao2_lock(faxregistry.container);
  1194. ao2_unlink(faxregistry.container, fax);
  1195. ao2_unlock(faxregistry.container);
  1196. ao2_ref(fax, -1);
  1197. ast_channel_unlock(chan);
  1198. return -1;
  1199. }
  1200. ast_format_copy(&orig_read_format, &chan->readformat);
  1201. if (ast_set_read_format_by_id(chan, AST_FORMAT_SLINEAR) < 0) {
  1202. ast_log(LOG_ERROR, "channel '%s' failed to set read format to signed linear.\n", chan->name);
  1203. ao2_lock(faxregistry.container);
  1204. ao2_unlink(faxregistry.container, fax);
  1205. ao2_unlock(faxregistry.container);
  1206. ao2_ref(fax, -1);
  1207. ast_channel_unlock(chan);
  1208. return -1;
  1209. }
  1210. if (fax->smoother) {
  1211. ast_smoother_free(fax->smoother);
  1212. fax->smoother = NULL;
  1213. }
  1214. if (!(fax->smoother = ast_smoother_new(320))) {
  1215. ast_log(LOG_WARNING, "Channel '%s' FAX session '%d' failed to obtain a smoother.\n", chan->name, fax->id);
  1216. }
  1217. } else {
  1218. expected_frametype = AST_FRAME_MODEM;
  1219. expected_framesubclass.integer = AST_MODEM_T38;
  1220. }
  1221. if (fax->debug_info) {
  1222. fax->debug_info->base_tv = ast_tvnow();
  1223. }
  1224. /* reset our result fields just in case the fax tech driver wants to
  1225. * set custom error messages */
  1226. ast_string_field_set(details, result, "");
  1227. ast_string_field_set(details, resultstr, "");
  1228. ast_string_field_set(details, error, "");
  1229. set_channel_variables(chan, details);
  1230. if (fax->tech->start_session(fax) < 0) {
  1231. GENERIC_FAX_EXEC_ERROR(fax, chan, "INIT_ERROR", "failed to start FAX session");
  1232. }
  1233. report_fax_status(chan, details, "FAX Transmission In Progress");
  1234. ast_debug(5, "channel %s will wait on FAX fd %d\n", chan->name, fax->fd);
  1235. /* handle frames for the session */
  1236. remaining_time = timeout;
  1237. start = ast_tvnow();
  1238. while (remaining_time > 0) {
  1239. struct ast_channel *ready_chan;
  1240. int ofd, exception;
  1241. ms = 1000;
  1242. errno = 0;
  1243. ready_chan = ast_waitfor_nandfds(&c, chancount, &fax->fd, 1, &exception, &ofd, &ms);
  1244. if (ready_chan) {
  1245. if (!(frame = ast_read(chan))) {
  1246. /* the channel is probably gone, so lets stop polling on it and let the
  1247. * FAX session complete before we exit the application. if needed,
  1248. * send the FAX stack silence so the modems can finish their session without
  1249. * any problems */
  1250. ast_debug(1, "Channel '%s' did not return a frame; probably hung up.\n", chan->name);
  1251. GENERIC_FAX_EXEC_SET_VARS(fax, chan, "HANGUP", "remote channel hungup");
  1252. c = NULL;
  1253. chancount = 0;
  1254. remaining_time = ast_remaining_ms(start, timeout);
  1255. fax->tech->cancel_session(fax);
  1256. if (fax->tech->generate_silence) {
  1257. fax->tech->generate_silence(fax);
  1258. }
  1259. continue;
  1260. }
  1261. if ((frame->frametype == AST_FRAME_CONTROL) &&
  1262. (frame->subclass.integer == AST_CONTROL_T38_PARAMETERS) &&
  1263. (frame->datalen == sizeof(t38_parameters))) {
  1264. unsigned int was_t38 = t38negotiated;
  1265. struct ast_control_t38_parameters *parameters = frame->data.ptr;
  1266. switch (parameters->request_response) {
  1267. case AST_T38_REQUEST_NEGOTIATE:
  1268. /* the other end has requested a switch to T.38, so reply that we are willing, if we can
  1269. * do T.38 as well
  1270. */
  1271. t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
  1272. t38_parameters.request_response = (details->caps & AST_FAX_TECH_T38) ? AST_T38_NEGOTIATED : AST_T38_REFUSED;
  1273. ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters));
  1274. break;
  1275. case AST_T38_NEGOTIATED:
  1276. t38_parameters_ast_to_fax(&details->their_t38_parameters, parameters);
  1277. t38negotiated = 1;
  1278. break;
  1279. default:
  1280. break;
  1281. }
  1282. if (t38negotiated && !was_t38) {
  1283. fax->tech->switch_to_t38(fax);
  1284. details->caps &= ~AST_FAX_TECH_AUDIO;
  1285. expected_frametype = AST_FRAME_MODEM;
  1286. expected_framesubclass.integer = AST_MODEM_T38;
  1287. if (fax->smoother) {
  1288. ast_smoother_free(fax->smoother);
  1289. fax->smoother = NULL;
  1290. }
  1291. report_fax_status(chan, details, "T.38 Negotiated");
  1292. ast_verb(3, "Channel '%s' switched to T.38 FAX session '%d'.\n", chan->name, fax->id);
  1293. }
  1294. } else if ((frame->frametype == expected_frametype) &&
  1295. (!memcmp(&frame->subclass, &expected_framesubclass, sizeof(frame->subclass)))) {
  1296. struct ast_frame *f;
  1297. if (fax->smoother) {
  1298. /* push the frame into a smoother */
  1299. if (ast_smoother_feed(fax->smoother, frame) < 0) {
  1300. GENERIC_FAX_EXEC_ERROR(fax, chan, "UNKNOWN", "Failed to feed the smoother");
  1301. }
  1302. while ((f = ast_smoother_read(fax->smoother)) && (f->data.ptr)) {
  1303. if (fax->debug_info) {
  1304. debug_check_frame_for_silence(fax, 1, f);
  1305. }
  1306. /* write the frame to the FAX stack */
  1307. fax->tech->write(fax, f);
  1308. fax->frames_received++;
  1309. if (f != frame) {
  1310. ast_frfree(f);
  1311. }
  1312. }
  1313. } else {
  1314. /* write the frame to the FAX stack */
  1315. fax->tech->write(fax, frame);
  1316. fax->frames_received++;
  1317. }
  1318. start = ast_tvnow();
  1319. }
  1320. ast_frfree(frame);
  1321. } else if (ofd == fax->fd) {
  1322. /* read a frame from the FAX stack and send it out the channel.
  1323. * the FAX stack will return a NULL if the FAX session has already completed */
  1324. if (!(frame = fax->tech->read(fax))) {
  1325. break;
  1326. }
  1327. if (fax->debug_info && (frame->frametype == AST_FRAME_VOICE)) {
  1328. debug_check_frame_for_silence(fax, 0, frame);
  1329. }
  1330. ast_write(chan, frame);
  1331. fax->frames_sent++;
  1332. ast_frfree(frame);
  1333. start = ast_tvnow();
  1334. } else {
  1335. if (ms && (ofd < 0)) {
  1336. if ((errno == 0) || (errno == EINTR)) {
  1337. remaining_time = ast_remaining_ms(start, timeout);
  1338. if (remaining_time <= 0)
  1339. GENERIC_FAX_EXEC_ERROR(fax, chan, "TIMEOUT", "fax session timed-out");
  1340. continue;
  1341. } else {
  1342. ast_log(LOG_WARNING, "something bad happened while channel '%s' was polling.\n", chan->name);
  1343. GENERIC_FAX_EXEC_ERROR(fax, chan, "UNKNOWN", "error polling data");
  1344. break;
  1345. }
  1346. } else {
  1347. /* nothing happened */
  1348. remaining_time = ast_remaining_ms(start, timeout);
  1349. if (remaining_time <= 0) {
  1350. GENERIC_FAX_EXEC_ERROR(fax, chan, "TIMEOUT", "fax session timed-out");
  1351. break;
  1352. }
  1353. }
  1354. }
  1355. }
  1356. ast_debug(3, "channel '%s' - event loop stopped { timeout: %d, remaining_time: %d }\n", chan->name, timeout, remaining_time);
  1357. set_channel_variables(chan, details);
  1358. ast_atomic_fetchadd_int(&faxregistry.fax_complete, 1);
  1359. if (!strcasecmp(details->result, "FAILED")) {
  1360. ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
  1361. }
  1362. if (fax) {
  1363. ao2_lock(faxregistry.container);
  1364. ao2_unlink(faxregistry.container, fax);
  1365. ao2_unlock(faxregistry.container);
  1366. ao2_ref(fax, -1);
  1367. }
  1368. /* if the channel is still alive, and we changed its read/write formats,
  1369. * restore them now
  1370. */
  1371. if (chancount) {
  1372. if (orig_read_format.id) {
  1373. ast_set_read_format(chan, &orig_read_format);
  1374. }
  1375. if (orig_write_format.id) {
  1376. ast_set_write_format(chan, &orig_write_format);
  1377. }
  1378. }
  1379. /* return the chancount so the calling function can determine if the channel hungup during this FAX session or not */
  1380. return chancount;
  1381. }
  1382. static int receivefax_t38_init(struct ast_channel *chan, struct ast_fax_session_details *details)
  1383. {
  1384. int timeout_ms;
  1385. struct ast_frame *frame = NULL;
  1386. struct ast_control_t38_parameters t38_parameters;
  1387. struct timeval start;
  1388. int ms;
  1389. /* don't send any audio if we've already received a T.38 reinvite */
  1390. if (ast_channel_get_t38_state(chan) != T38_STATE_NEGOTIATING) {
  1391. /* generate 3 seconds of CED */
  1392. if (ast_playtones_start(chan, 1024, "!2100/3000", 1)) {
  1393. ast_log(LOG_ERROR, "error generating CED tone on %s\n", chan->name);
  1394. return -1;
  1395. }
  1396. timeout_ms = 3000;
  1397. start = ast_tvnow();
  1398. while ((ms = ast_remaining_ms(start, timeout_ms))) {
  1399. ms = ast_waitfor(chan, ms);
  1400. if (ms < 0) {
  1401. ast_log(LOG_ERROR, "error while generating CED tone on %s\n", chan->name);
  1402. ast_playtones_stop(chan);
  1403. return -1;
  1404. }
  1405. if (ms == 0) { /* all done, nothing happened */
  1406. break;
  1407. }
  1408. if (!(frame = ast_read(chan))) {
  1409. ast_log(LOG_ERROR, "error reading frame while generating CED tone on %s\n", chan->name);
  1410. ast_playtones_stop(chan);
  1411. return -1;
  1412. }
  1413. if ((frame->frametype == AST_FRAME_CONTROL) &&
  1414. (frame->subclass.integer == AST_CONTROL_T38_PARAMETERS) &&
  1415. (frame->datalen == sizeof(t38_parameters))) {
  1416. struct ast_control_t38_parameters *parameters = frame->data.ptr;
  1417. switch (parameters->request_response) {
  1418. case AST_T38_REQUEST_NEGOTIATE:
  1419. /* the other end has requested a switch to T.38, so reply that we are willing, if we can
  1420. * do T.38 as well
  1421. */
  1422. t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
  1423. t38_parameters.request_response = (details->caps & AST_FAX_TECH_T38) ? AST_T38_NEGOTIATED : AST_T38_REFUSED;
  1424. ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters));
  1425. ast_playtones_stop(chan);
  1426. break;
  1427. case AST_T38_NEGOTIATED:
  1428. ast_debug(1, "Negotiated T.38 for receive on %s\n", chan->name);
  1429. t38_parameters_ast_to_fax(&details->their_t38_parameters, parameters);
  1430. details->caps &= ~AST_FAX_TECH_AUDIO;
  1431. report_fax_status(chan, details, "T.38 Negotiated");
  1432. break;
  1433. default:
  1434. break;
  1435. }
  1436. }
  1437. ast_frfree(frame);
  1438. }
  1439. ast_playtones_stop(chan);
  1440. }
  1441. /* if T.38 was negotiated, we are done initializing */
  1442. if (ast_channel_get_t38_state(chan) == T38_STATE_NEGOTIATED) {
  1443. return 0;
  1444. }
  1445. /* request T.38 */
  1446. ast_debug(1, "Negotiating T.38 for receive on %s\n", chan->name);
  1447. /* wait up to five seconds for negotiation to complete */
  1448. timeout_ms = 5000;
  1449. /* set parameters based on the session's parameters */
  1450. t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
  1451. t38_parameters.request_response = AST_T38_REQUEST_NEGOTIATE;
  1452. if ((ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters)) != 0)) {
  1453. return -1;
  1454. }
  1455. start = ast_tvnow();
  1456. while ((ms = ast_remaining_ms(start, timeout_ms))) {
  1457. int break_loop = 0;
  1458. ms = ast_waitfor(chan, ms);
  1459. if (ms < 0) {
  1460. ast_log(LOG_WARNING, "error on '%s' while waiting for T.38 negotiation.\n", chan->name);
  1461. return -1;
  1462. }
  1463. if (ms == 0) { /* all done, nothing happened */
  1464. ast_log(LOG_WARNING, "channel '%s' timed-out during the T.38 negotiation.\n", chan->name);
  1465. details->caps &= ~AST_FAX_TECH_T38;
  1466. break;
  1467. }
  1468. if (!(frame = ast_read(chan))) {
  1469. ast_log(LOG_WARNING, "error on '%s' while waiting for T.38 negotiation.\n", chan->name);
  1470. return -1;
  1471. }
  1472. if ((frame->frametype == AST_FRAME_CONTROL) &&
  1473. (frame->subclass.integer == AST_CONTROL_T38_PARAMETERS) &&
  1474. (frame->datalen == sizeof(t38_parameters))) {
  1475. struct ast_control_t38_parameters *parameters = frame->data.ptr;
  1476. switch (parameters->request_response) {
  1477. case AST_T38_REQUEST_NEGOTIATE:
  1478. t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
  1479. t38_parameters.request_response = AST_T38_NEGOTIATED;
  1480. ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters));
  1481. break;
  1482. case AST_T38_NEGOTIATED:
  1483. ast_debug(1, "Negotiated T.38 for receive on %s\n", chan->name);
  1484. t38_parameters_ast_to_fax(&details->their_t38_parameters, parameters);
  1485. details->caps &= ~AST_FAX_TECH_AUDIO;
  1486. report_fax_status(chan, details, "T.38 Negotiated");
  1487. break_loop = 1;
  1488. break;
  1489. case AST_T38_REFUSED:
  1490. ast_log(LOG_WARNING, "channel '%s' refused to negotiate T.38\n", chan->name);
  1491. details->caps &= ~AST_FAX_TECH_T38;
  1492. break_loop = 1;
  1493. break;
  1494. default:
  1495. ast_log(LOG_ERROR, "channel '%s' failed to negotiate T.38\n", chan->name);
  1496. details->caps &= ~AST_FAX_TECH_T38;
  1497. break_loop = 1;
  1498. break;
  1499. }
  1500. }
  1501. ast_frfree(frame);
  1502. if (break_loop) {
  1503. break;
  1504. }
  1505. }
  1506. /* if T.38 was negotiated, we are done initializing */
  1507. if (ast_channel_get_t38_state(chan) == T38_STATE_NEGOTIATED) {
  1508. return 0;
  1509. }
  1510. /* if we made it here, then T.38 failed, check the 'f' flag */
  1511. if (details->option.allow_audio != AST_FAX_OPTFLAG_TRUE) {
  1512. ast_log(LOG_WARNING, "Audio FAX not allowed on channel '%s' and T.38 negotiation failed; aborting.\n", chan->name);
  1513. return -1;
  1514. }
  1515. /* ok, audio fallback is allowed */
  1516. details->caps |= AST_FAX_TECH_AUDIO;
  1517. return 0;
  1518. }
  1519. /*! \brief initiate a receive FAX session */
  1520. static int receivefax_exec(struct ast_channel *chan, const char *data)
  1521. {
  1522. char *parse, modems[128] = "";
  1523. int channel_alive;
  1524. struct ast_fax_session_details *details;
  1525. struct ast_fax_session *s;
  1526. struct ast_fax_tech_token *token = NULL;
  1527. struct ast_fax_document *doc;
  1528. AST_DECLARE_APP_ARGS(args,
  1529. AST_APP_ARG(filename);
  1530. AST_APP_ARG(options);
  1531. );
  1532. struct ast_flags opts = { 0, };
  1533. struct manager_event_info info;
  1534. enum ast_t38_state t38state;
  1535. /* initialize output channel variables */
  1536. pbx_builtin_setvar_helper(chan, "FAXSTATUS", "FAILED");
  1537. pbx_builtin_setvar_helper(chan, "REMOTESTATIONID", NULL);
  1538. pbx_builtin_setvar_helper(chan, "FAXPAGES", "0");
  1539. pbx_builtin_setvar_helper(chan, "FAXBITRATE", NULL);
  1540. pbx_builtin_setvar_helper(chan, "FAXRESOLUTION", NULL);
  1541. /* Get a FAX session details structure from the channel's FAX datastore and create one if
  1542. * it does not already exist. */
  1543. if (!(details = find_or_create_details(chan))) {
  1544. pbx_builtin_setvar_helper(chan, "FAXERROR", "MEMORY_ERROR");
  1545. pbx_builtin_setvar_helper(chan, "FAXSTATUSSTRING", "error allocating memory");
  1546. ast_log(LOG_ERROR, "System cannot provide memory for session requirements.\n");
  1547. return -1;
  1548. }
  1549. ast_string_field_set(details, result, "FAILED");
  1550. ast_string_field_set(details, resultstr, "error starting fax session");
  1551. ast_string_field_set(details, error, "INIT_ERROR");
  1552. set_channel_variables(chan, details);
  1553. if (details->gateway_id > 0) {
  1554. ast_string_field_set(details, resultstr, "can't receive a fax on a channel with a T.38 gateway");
  1555. set_channel_variables(chan, details);
  1556. ast_log(LOG_ERROR, "executing ReceiveFAX on a channel with a T.38 Gateway is not supported\n");
  1557. ao2_ref(details, -1);
  1558. return -1;
  1559. }
  1560. if (details->maxrate < details->minrate) {
  1561. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  1562. ast_string_field_set(details, resultstr, "maxrate is less than minrate");
  1563. set_channel_variables(chan, details);
  1564. ast_log(LOG_ERROR, "maxrate %d is less than minrate %d\n", details->maxrate, details->minrate);
  1565. ao2_ref(details, -1);
  1566. return -1;
  1567. }
  1568. if (check_modem_rate(details->modems, details->minrate)) {
  1569. ast_fax_modem_to_str(details->modems, modems, sizeof(modems));
  1570. ast_log(LOG_ERROR, "'modems' setting '%s' is incompatible with 'minrate' setting %d\n", modems, details->minrate);
  1571. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  1572. ast_string_field_set(details, resultstr, "incompatible 'modems' and 'minrate' settings");
  1573. set_channel_variables(chan, details);
  1574. ao2_ref(details, -1);
  1575. return -1;
  1576. }
  1577. if (check_modem_rate(details->modems, details->maxrate)) {
  1578. ast_fax_modem_to_str(details->modems, modems, sizeof(modems));
  1579. ast_log(LOG_ERROR, "'modems' setting '%s' is incompatible with 'maxrate' setting %d\n", modems, details->maxrate);
  1580. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  1581. ast_string_field_set(details, resultstr, "incompatible 'modems' and 'maxrate' settings");
  1582. set_channel_variables(chan, details);
  1583. ao2_ref(details, -1);
  1584. return -1;
  1585. }
  1586. if (ast_strlen_zero(data)) {
  1587. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  1588. ast_string_field_set(details, resultstr, "invalid arguments");
  1589. set_channel_variables(chan, details);
  1590. ast_log(LOG_WARNING, "%s requires an argument (filename[,options])\n", app_receivefax);
  1591. ao2_ref(details, -1);
  1592. return -1;
  1593. }
  1594. parse = ast_strdupa(data);
  1595. AST_STANDARD_APP_ARGS(args, parse);
  1596. if (!ast_strlen_zero(args.options) &&
  1597. ast_app_parse_options(fax_exec_options, &opts, NULL, args.options)) {
  1598. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  1599. ast_string_field_set(details, resultstr, "invalid arguments");
  1600. set_channel_variables(chan, details);
  1601. ao2_ref(details, -1);
  1602. return -1;
  1603. }
  1604. if (ast_strlen_zero(args.filename)) {
  1605. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  1606. ast_string_field_set(details, resultstr, "invalid arguments");
  1607. set_channel_variables(chan, details);
  1608. ast_log(LOG_WARNING, "%s requires an argument (filename[,options])\n", app_receivefax);
  1609. ao2_ref(details, -1);
  1610. return -1;
  1611. }
  1612. /* check for unsupported FAX application options */
  1613. if (ast_test_flag(&opts, OPT_CALLERMODE) || ast_test_flag(&opts, OPT_CALLEDMODE)) {
  1614. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  1615. ast_string_field_set(details, resultstr, "invalid arguments");
  1616. set_channel_variables(chan, details);
  1617. ast_log(LOG_WARNING, "%s does not support polling\n", app_receivefax);
  1618. ao2_ref(details, -1);
  1619. return -1;
  1620. }
  1621. ast_atomic_fetchadd_int(&faxregistry.fax_rx_attempts, 1);
  1622. pbx_builtin_setvar_helper(chan, "FAXERROR", "Channel Problems");
  1623. pbx_builtin_setvar_helper(chan, "FAXSTATUSSTRING", "Error before FAX transmission started.");
  1624. if (!(doc = ast_calloc(1, sizeof(*doc) + strlen(args.filename) + 1))) {
  1625. ast_string_field_set(details, error, "MEMORY_ERROR");
  1626. ast_string_field_set(details, resultstr, "error allocating memory");
  1627. set_channel_variables(chan, details);
  1628. ast_log(LOG_ERROR, "System cannot provide memory for session requirements.\n");
  1629. ao2_ref(details, -1);
  1630. return -1;
  1631. }
  1632. strcpy(doc->filename, args.filename);
  1633. AST_LIST_INSERT_TAIL(&details->documents, doc, next);
  1634. ast_verb(3, "Channel '%s' receiving FAX '%s'\n", chan->name, args.filename);
  1635. details->caps = AST_FAX_TECH_RECEIVE;
  1636. details->option.send_ced = AST_FAX_OPTFLAG_TRUE;
  1637. /* check for debug */
  1638. if (ast_test_flag(&opts, OPT_DEBUG) || global_fax_debug) {
  1639. details->option.debug = AST_FAX_OPTFLAG_TRUE;
  1640. }
  1641. /* check for request for status events */
  1642. if (ast_test_flag(&opts, OPT_STATUS)) {
  1643. details->option.statusevents = AST_FAX_OPTFLAG_TRUE;
  1644. }
  1645. t38state = ast_channel_get_t38_state(chan);
  1646. if ((t38state == T38_STATE_UNAVAILABLE) || (t38state == T38_STATE_REJECTED) ||
  1647. ast_test_flag(&opts, OPT_ALLOWAUDIO) ||
  1648. ast_test_flag(&opts, OPT_FORCE_AUDIO)) {
  1649. details->option.allow_audio = AST_FAX_OPTFLAG_TRUE;
  1650. }
  1651. if (!(s = fax_session_reserve(details, &token))) {
  1652. ast_string_field_set(details, resultstr, "error reserving fax session");
  1653. set_channel_variables(chan, details);
  1654. ast_log(LOG_ERROR, "Unable to reserve FAX session.\n");
  1655. ao2_ref(details, -1);
  1656. return -1;
  1657. }
  1658. /* make sure the channel is up */
  1659. if (chan->_state != AST_STATE_UP) {
  1660. if (ast_answer(chan)) {
  1661. ast_string_field_set(details, resultstr, "error answering channel");
  1662. set_channel_variables(chan, details);
  1663. ast_log(LOG_WARNING, "Channel '%s' failed answer attempt.\n", chan->name);
  1664. fax_session_release(s, token);
  1665. ao2_ref(s, -1);
  1666. ao2_ref(details, -1);
  1667. return -1;
  1668. }
  1669. }
  1670. if (!ast_test_flag(&opts, OPT_FORCE_AUDIO)) {
  1671. if (set_fax_t38_caps(chan, details)) {
  1672. ast_string_field_set(details, error, "T38_NEG_ERROR");
  1673. ast_string_field_set(details, resultstr, "error negotiating T.38");
  1674. set_channel_variables(chan, details);
  1675. fax_session_release(s, token);
  1676. ao2_ref(s, -1);
  1677. ao2_ref(details, -1);
  1678. return -1;
  1679. }
  1680. } else {
  1681. details->caps |= AST_FAX_TECH_AUDIO;
  1682. }
  1683. if (!ast_test_flag(&opts, OPT_FORCE_AUDIO) && (details->caps & AST_FAX_TECH_T38)) {
  1684. if (receivefax_t38_init(chan, details)) {
  1685. ast_string_field_set(details, error, "T38_NEG_ERROR");
  1686. ast_string_field_set(details, resultstr, "error negotiating T.38");
  1687. set_channel_variables(chan, details);
  1688. fax_session_release(s, token);
  1689. ao2_ref(s, -1);
  1690. ao2_ref(details, -1);
  1691. ast_log(LOG_ERROR, "error initializing channel '%s' in T.38 mode\n", chan->name);
  1692. return -1;
  1693. }
  1694. }
  1695. if ((channel_alive = generic_fax_exec(chan, details, s, token)) < 0) {
  1696. ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
  1697. }
  1698. if (ast_channel_get_t38_state(chan) == T38_STATE_NEGOTIATED) {
  1699. if (disable_t38(chan)) {
  1700. ast_debug(1, "error disabling T.38 mode on %s\n", chan->name);
  1701. }
  1702. }
  1703. /* send out the AMI completion event */
  1704. ast_channel_lock(chan);
  1705. get_manager_event_info(chan, &info);
  1706. manager_event(EVENT_FLAG_CALL,
  1707. "ReceiveFAX",
  1708. "Channel: %s\r\n"
  1709. "Context: %s\r\n"
  1710. "Exten: %s\r\n"
  1711. "CallerID: %s\r\n"
  1712. "RemoteStationID: %s\r\n"
  1713. "LocalStationID: %s\r\n"
  1714. "PagesTransferred: %s\r\n"
  1715. "Resolution: %s\r\n"
  1716. "TransferRate: %s\r\n"
  1717. "FileName: %s\r\n",
  1718. chan->name,
  1719. info.context,
  1720. info.exten,
  1721. info.cid,
  1722. S_OR(pbx_builtin_getvar_helper(chan, "REMOTESTATIONID"), ""),
  1723. S_OR(pbx_builtin_getvar_helper(chan, "LOCALSTATIONID"), ""),
  1724. S_OR(pbx_builtin_getvar_helper(chan, "FAXPAGES"), ""),
  1725. S_OR(pbx_builtin_getvar_helper(chan, "FAXRESOLUTION"), ""),
  1726. S_OR(pbx_builtin_getvar_helper(chan, "FAXBITRATE"), ""),
  1727. args.filename);
  1728. ast_channel_unlock(chan);
  1729. ao2_ref(s, -1);
  1730. ao2_ref(details, -1);
  1731. /* If the channel hungup return -1; otherwise, return 0 to continue in the dialplan */
  1732. return (!channel_alive) ? -1 : 0;
  1733. }
  1734. static int sendfax_t38_init(struct ast_channel *chan, struct ast_fax_session_details *details)
  1735. {
  1736. int timeout_ms;
  1737. struct ast_frame *frame = NULL;
  1738. struct ast_control_t38_parameters t38_parameters;
  1739. struct timeval start;
  1740. int ms;
  1741. /* send CNG tone while listening for the receiver to initiate a switch
  1742. * to T.38 mode; if they do, stop sending the CNG tone and proceed with
  1743. * the switch.
  1744. *
  1745. * 10500 is enough time for 3 CNG tones
  1746. */
  1747. timeout_ms = 10500;
  1748. /* don't send any audio if we've already received a T.38 reinvite */
  1749. if (ast_channel_get_t38_state(chan) != T38_STATE_NEGOTIATING) {
  1750. if (ast_playtones_start(chan, 1024, "!1100/500,!0/3000,!1100/500,!0/3000,!1100/500,!0/3000", 1)) {
  1751. ast_log(LOG_ERROR, "error generating CNG tone on %s\n", chan->name);
  1752. return -1;
  1753. }
  1754. }
  1755. start = ast_tvnow();
  1756. while ((ms = ast_remaining_ms(start, timeout_ms))) {
  1757. int break_loop = 0;
  1758. ms = ast_waitfor(chan, ms);
  1759. if (ms < 0) {
  1760. ast_log(LOG_ERROR, "error while generating CNG tone on %s\n", chan->name);
  1761. ast_playtones_stop(chan);
  1762. return -1;
  1763. }
  1764. if (ms == 0) { /* all done, nothing happened */
  1765. break;
  1766. }
  1767. if (!(frame = ast_read(chan))) {
  1768. ast_log(LOG_ERROR, "error reading frame while generating CNG tone on %s\n", chan->name);
  1769. ast_playtones_stop(chan);
  1770. return -1;
  1771. }
  1772. if ((frame->frametype == AST_FRAME_CONTROL) &&
  1773. (frame->subclass.integer == AST_CONTROL_T38_PARAMETERS) &&
  1774. (frame->datalen == sizeof(t38_parameters))) {
  1775. struct ast_control_t38_parameters *parameters = frame->data.ptr;
  1776. switch (parameters->request_response) {
  1777. case AST_T38_REQUEST_NEGOTIATE:
  1778. /* the other end has requested a switch to T.38, so reply that we are willing, if we can
  1779. * do T.38 as well
  1780. */
  1781. t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
  1782. t38_parameters.request_response = (details->caps & AST_FAX_TECH_T38) ? AST_T38_NEGOTIATED : AST_T38_REFUSED;
  1783. ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters));
  1784. ast_playtones_stop(chan);
  1785. break;
  1786. case AST_T38_NEGOTIATED:
  1787. ast_debug(1, "Negotiated T.38 for send on %s\n", chan->name);
  1788. t38_parameters_ast_to_fax(&details->their_t38_parameters, parameters);
  1789. details->caps &= ~AST_FAX_TECH_AUDIO;
  1790. report_fax_status(chan, details, "T.38 Negotiated");
  1791. break_loop = 1;
  1792. break;
  1793. default:
  1794. break;
  1795. }
  1796. }
  1797. ast_frfree(frame);
  1798. if (break_loop) {
  1799. break;
  1800. }
  1801. }
  1802. ast_playtones_stop(chan);
  1803. if (ast_channel_get_t38_state(chan) == T38_STATE_NEGOTIATED) {
  1804. return 0;
  1805. }
  1806. /* T.38 negotiation did not happen, initiate a switch if requested */
  1807. if (details->option.request_t38 == AST_FAX_OPTFLAG_TRUE) {
  1808. ast_debug(1, "Negotiating T.38 for send on %s\n", chan->name);
  1809. /* wait up to five seconds for negotiation to complete */
  1810. timeout_ms = 5000;
  1811. /* set parameters based on the session's parameters */
  1812. t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
  1813. t38_parameters.request_response = AST_T38_REQUEST_NEGOTIATE;
  1814. if ((ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters)) != 0)) {
  1815. return -1;
  1816. }
  1817. start = ast_tvnow();
  1818. while ((ms = ast_remaining_ms(start, timeout_ms))) {
  1819. int break_loop = 0;
  1820. ms = ast_waitfor(chan, ms);
  1821. if (ms < 0) {
  1822. ast_log(LOG_WARNING, "error on '%s' while waiting for T.38 negotiation.\n", chan->name);
  1823. return -1;
  1824. }
  1825. if (ms == 0) { /* all done, nothing happened */
  1826. ast_log(LOG_WARNING, "channel '%s' timed-out during the T.38 negotiation.\n", chan->name);
  1827. details->caps &= ~AST_FAX_TECH_T38;
  1828. break;
  1829. }
  1830. if (!(frame = ast_read(chan))) {
  1831. ast_log(LOG_WARNING, "error on '%s' while waiting for T.38 negotiation.\n", chan->name);
  1832. return -1;
  1833. }
  1834. if ((frame->frametype == AST_FRAME_CONTROL) &&
  1835. (frame->subclass.integer == AST_CONTROL_T38_PARAMETERS) &&
  1836. (frame->datalen == sizeof(t38_parameters))) {
  1837. struct ast_control_t38_parameters *parameters = frame->data.ptr;
  1838. switch (parameters->request_response) {
  1839. case AST_T38_REQUEST_NEGOTIATE:
  1840. t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
  1841. t38_parameters.request_response = AST_T38_NEGOTIATED;
  1842. ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters));
  1843. break;
  1844. case AST_T38_NEGOTIATED:
  1845. ast_debug(1, "Negotiated T.38 for receive on %s\n", chan->name);
  1846. t38_parameters_ast_to_fax(&details->their_t38_parameters, parameters);
  1847. details->caps &= ~AST_FAX_TECH_AUDIO;
  1848. report_fax_status(chan, details, "T.38 Negotiated");
  1849. break_loop = 1;
  1850. break;
  1851. case AST_T38_REFUSED:
  1852. ast_log(LOG_WARNING, "channel '%s' refused to negotiate T.38\n", chan->name);
  1853. details->caps &= ~AST_FAX_TECH_T38;
  1854. break_loop = 1;
  1855. break;
  1856. default:
  1857. ast_log(LOG_ERROR, "channel '%s' failed to negotiate T.38\n", chan->name);
  1858. details->caps &= ~AST_FAX_TECH_T38;
  1859. break_loop = 1;
  1860. break;
  1861. }
  1862. }
  1863. ast_frfree(frame);
  1864. if (break_loop) {
  1865. break;
  1866. }
  1867. }
  1868. /* if T.38 was negotiated, we are done initializing */
  1869. if (ast_channel_get_t38_state(chan) == T38_STATE_NEGOTIATED) {
  1870. return 0;
  1871. }
  1872. /* send one more CNG tone to get audio going again for some
  1873. * carriers if we are going to fall back to audio mode */
  1874. if (details->option.allow_audio == AST_FAX_OPTFLAG_TRUE) {
  1875. if (ast_playtones_start(chan, 1024, "!1100/500,!0/3000", 1)) {
  1876. ast_log(LOG_ERROR, "error generating second CNG tone on %s\n", chan->name);
  1877. return -1;
  1878. }
  1879. timeout_ms = 3500;
  1880. start = ast_tvnow();
  1881. while ((ms = ast_remaining_ms(start, timeout_ms))) {
  1882. int break_loop = 0;
  1883. ms = ast_waitfor(chan, ms);
  1884. if (ms < 0) {
  1885. ast_log(LOG_ERROR, "error while generating second CNG tone on %s\n", chan->name);
  1886. ast_playtones_stop(chan);
  1887. return -1;
  1888. }
  1889. if (ms == 0) { /* all done, nothing happened */
  1890. break;
  1891. }
  1892. if (!(frame = ast_read(chan))) {
  1893. ast_log(LOG_ERROR, "error reading frame while generating second CNG tone on %s\n", chan->name);
  1894. ast_playtones_stop(chan);
  1895. return -1;
  1896. }
  1897. if ((frame->frametype == AST_FRAME_CONTROL) &&
  1898. (frame->subclass.integer == AST_CONTROL_T38_PARAMETERS) &&
  1899. (frame->datalen == sizeof(t38_parameters))) {
  1900. struct ast_control_t38_parameters *parameters = frame->data.ptr;
  1901. switch (parameters->request_response) {
  1902. case AST_T38_REQUEST_NEGOTIATE:
  1903. /* the other end has requested a switch to T.38, so reply that we are willing, if we can
  1904. * do T.38 as well
  1905. */
  1906. t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
  1907. t38_parameters.request_response = (details->caps & AST_FAX_TECH_T38) ? AST_T38_NEGOTIATED : AST_T38_REFUSED;
  1908. ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters));
  1909. ast_playtones_stop(chan);
  1910. break;
  1911. case AST_T38_NEGOTIATED:
  1912. ast_debug(1, "Negotiated T.38 for send on %s\n", chan->name);
  1913. t38_parameters_ast_to_fax(&details->their_t38_parameters, parameters);
  1914. details->caps &= ~AST_FAX_TECH_AUDIO;
  1915. report_fax_status(chan, details, "T.38 Negotiated");
  1916. break_loop = 1;
  1917. break;
  1918. default:
  1919. break;
  1920. }
  1921. }
  1922. ast_frfree(frame);
  1923. if (break_loop) {
  1924. break;
  1925. }
  1926. }
  1927. ast_playtones_stop(chan);
  1928. /* if T.38 was negotiated, we are done initializing */
  1929. if (ast_channel_get_t38_state(chan) == T38_STATE_NEGOTIATED) {
  1930. return 0;
  1931. }
  1932. }
  1933. }
  1934. /* if we made it here, then T.38 failed, check the 'f' flag */
  1935. if (details->option.allow_audio == AST_FAX_OPTFLAG_FALSE) {
  1936. ast_log(LOG_WARNING, "Audio FAX not allowed on channel '%s' and T.38 negotiation failed; aborting.\n", chan->name);
  1937. return -1;
  1938. }
  1939. /* ok, audio fallback is allowed */
  1940. details->caps |= AST_FAX_TECH_AUDIO;
  1941. return 0;
  1942. }
  1943. /*! \brief initiate a send FAX session */
  1944. static int sendfax_exec(struct ast_channel *chan, const char *data)
  1945. {
  1946. char *parse, *filenames, *c, modems[128] = "";
  1947. int channel_alive, file_count;
  1948. struct ast_fax_session_details *details;
  1949. struct ast_fax_session *s;
  1950. struct ast_fax_tech_token *token = NULL;
  1951. struct ast_fax_document *doc;
  1952. AST_DECLARE_APP_ARGS(args,
  1953. AST_APP_ARG(filenames);
  1954. AST_APP_ARG(options);
  1955. );
  1956. struct ast_flags opts = { 0, };
  1957. struct manager_event_info info;
  1958. enum ast_t38_state t38state;
  1959. /* initialize output channel variables */
  1960. pbx_builtin_setvar_helper(chan, "FAXSTATUS", "FAILED");
  1961. pbx_builtin_setvar_helper(chan, "REMOTESTATIONID", NULL);
  1962. pbx_builtin_setvar_helper(chan, "FAXPAGES", "0");
  1963. pbx_builtin_setvar_helper(chan, "FAXBITRATE", NULL);
  1964. pbx_builtin_setvar_helper(chan, "FAXRESOLUTION", NULL);
  1965. /* Get a requirement structure and set it. This structure is used
  1966. * to tell the FAX technology module about the higher level FAX session */
  1967. if (!(details = find_or_create_details(chan))) {
  1968. pbx_builtin_setvar_helper(chan, "FAXERROR", "MEMORY_ERROR");
  1969. pbx_builtin_setvar_helper(chan, "FAXSTATUSSTRING", "error allocating memory");
  1970. ast_log(LOG_ERROR, "System cannot provide memory for session requirements.\n");
  1971. return -1;
  1972. }
  1973. ast_string_field_set(details, result, "FAILED");
  1974. ast_string_field_set(details, resultstr, "error starting fax session");
  1975. ast_string_field_set(details, error, "INIT_ERROR");
  1976. set_channel_variables(chan, details);
  1977. if (details->gateway_id > 0) {
  1978. ast_string_field_set(details, resultstr, "can't send a fax on a channel with a T.38 gateway");
  1979. set_channel_variables(chan, details);
  1980. ast_log(LOG_ERROR, "executing SendFAX on a channel with a T.38 Gateway is not supported\n");
  1981. ao2_ref(details, -1);
  1982. return -1;
  1983. }
  1984. if (details->maxrate < details->minrate) {
  1985. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  1986. ast_string_field_set(details, resultstr, "maxrate is less than minrate");
  1987. set_channel_variables(chan, details);
  1988. ast_log(LOG_ERROR, "maxrate %d is less than minrate %d\n", details->maxrate, details->minrate);
  1989. ao2_ref(details, -1);
  1990. return -1;
  1991. }
  1992. if (check_modem_rate(details->modems, details->minrate)) {
  1993. ast_fax_modem_to_str(details->modems, modems, sizeof(modems));
  1994. ast_log(LOG_ERROR, "'modems' setting '%s' is incompatible with 'minrate' setting %d\n", modems, details->minrate);
  1995. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  1996. ast_string_field_set(details, resultstr, "incompatible 'modems' and 'minrate' settings");
  1997. set_channel_variables(chan, details);
  1998. ao2_ref(details, -1);
  1999. return -1;
  2000. }
  2001. if (check_modem_rate(details->modems, details->maxrate)) {
  2002. ast_fax_modem_to_str(details->modems, modems, sizeof(modems));
  2003. ast_log(LOG_ERROR, "'modems' setting '%s' is incompatible with 'maxrate' setting %d\n", modems, details->maxrate);
  2004. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  2005. ast_string_field_set(details, resultstr, "incompatible 'modems' and 'maxrate' settings");
  2006. set_channel_variables(chan, details);
  2007. ao2_ref(details, -1);
  2008. return -1;
  2009. }
  2010. if (ast_strlen_zero(data)) {
  2011. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  2012. ast_string_field_set(details, resultstr, "invalid arguments");
  2013. set_channel_variables(chan, details);
  2014. ast_log(LOG_WARNING, "%s requires an argument (filename[&filename[&filename]][,options])\n", app_sendfax);
  2015. ao2_ref(details, -1);
  2016. return -1;
  2017. }
  2018. parse = ast_strdupa(data);
  2019. AST_STANDARD_APP_ARGS(args, parse);
  2020. if (!ast_strlen_zero(args.options) &&
  2021. ast_app_parse_options(fax_exec_options, &opts, NULL, args.options)) {
  2022. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  2023. ast_string_field_set(details, resultstr, "invalid arguments");
  2024. set_channel_variables(chan, details);
  2025. ao2_ref(details, -1);
  2026. return -1;
  2027. }
  2028. if (ast_strlen_zero(args.filenames)) {
  2029. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  2030. ast_string_field_set(details, resultstr, "invalid arguments");
  2031. set_channel_variables(chan, details);
  2032. ast_log(LOG_WARNING, "%s requires an argument (filename[&filename[&filename]],options])\n", app_sendfax);
  2033. ao2_ref(details, -1);
  2034. return -1;
  2035. }
  2036. /* check for unsupported FAX application options */
  2037. if (ast_test_flag(&opts, OPT_CALLERMODE) || ast_test_flag(&opts, OPT_CALLEDMODE)) {
  2038. ast_string_field_set(details, error, "INVALID_ARGUMENTS");
  2039. ast_string_field_set(details, resultstr, "invalid arguments");
  2040. set_channel_variables(chan, details);
  2041. ast_log(LOG_WARNING, "%s does not support polling\n", app_sendfax);
  2042. ao2_ref(details, -1);
  2043. return -1;
  2044. }
  2045. ast_atomic_fetchadd_int(&faxregistry.fax_tx_attempts, 1);
  2046. file_count = 0;
  2047. filenames = args.filenames;
  2048. while ((c = strsep(&filenames, "&"))) {
  2049. if (access(c, (F_OK | R_OK)) < 0) {
  2050. ast_string_field_set(details, error, "FILE_ERROR");
  2051. ast_string_field_set(details, resultstr, "error reading file");
  2052. set_channel_variables(chan, details);
  2053. ast_log(LOG_ERROR, "access failure. Verify '%s' exists and check permissions.\n", args.filenames);
  2054. ao2_ref(details, -1);
  2055. return -1;
  2056. }
  2057. if (!(doc = ast_calloc(1, sizeof(*doc) + strlen(c) + 1))) {
  2058. ast_string_field_set(details, error, "MEMORY_ERROR");
  2059. ast_string_field_set(details, resultstr, "error allocating memory");
  2060. set_channel_variables(chan, details);
  2061. ast_log(LOG_ERROR, "System cannot provide memory for session requirements.\n");
  2062. ao2_ref(details, -1);
  2063. return -1;
  2064. }
  2065. strcpy(doc->filename, c);
  2066. AST_LIST_INSERT_TAIL(&details->documents, doc, next);
  2067. file_count++;
  2068. }
  2069. ast_verb(3, "Channel '%s' sending FAX:\n", chan->name);
  2070. AST_LIST_TRAVERSE(&details->documents, doc, next) {
  2071. ast_verb(3, " %s\n", doc->filename);
  2072. }
  2073. details->caps = AST_FAX_TECH_SEND;
  2074. if (file_count > 1) {
  2075. details->caps |= AST_FAX_TECH_MULTI_DOC;
  2076. }
  2077. /* check for debug */
  2078. if (ast_test_flag(&opts, OPT_DEBUG) || global_fax_debug) {
  2079. details->option.debug = AST_FAX_OPTFLAG_TRUE;
  2080. }
  2081. /* check for request for status events */
  2082. if (ast_test_flag(&opts, OPT_STATUS)) {
  2083. details->option.statusevents = AST_FAX_OPTFLAG_TRUE;
  2084. }
  2085. t38state = ast_channel_get_t38_state(chan);
  2086. if ((t38state == T38_STATE_UNAVAILABLE) || (t38state == T38_STATE_REJECTED) ||
  2087. ast_test_flag(&opts, OPT_ALLOWAUDIO) ||
  2088. ast_test_flag(&opts, OPT_FORCE_AUDIO)) {
  2089. details->option.allow_audio = AST_FAX_OPTFLAG_TRUE;
  2090. }
  2091. if (ast_test_flag(&opts, OPT_REQUEST_T38)) {
  2092. details->option.request_t38 = AST_FAX_OPTFLAG_TRUE;
  2093. }
  2094. if (!(s = fax_session_reserve(details, &token))) {
  2095. ast_string_field_set(details, resultstr, "error reserving fax session");
  2096. set_channel_variables(chan, details);
  2097. ast_log(LOG_ERROR, "Unable to reserve FAX session.\n");
  2098. ao2_ref(details, -1);
  2099. return -1;
  2100. }
  2101. /* make sure the channel is up */
  2102. if (chan->_state != AST_STATE_UP) {
  2103. if (ast_answer(chan)) {
  2104. ast_string_field_set(details, resultstr, "error answering channel");
  2105. set_channel_variables(chan, details);
  2106. ast_log(LOG_WARNING, "Channel '%s' failed answer attempt.\n", chan->name);
  2107. fax_session_release(s, token);
  2108. ao2_ref(s, -1);
  2109. ao2_ref(details, -1);
  2110. return -1;
  2111. }
  2112. }
  2113. if (!ast_test_flag(&opts, OPT_FORCE_AUDIO)) {
  2114. if (set_fax_t38_caps(chan, details)) {
  2115. ast_string_field_set(details, error, "T38_NEG_ERROR");
  2116. ast_string_field_set(details, resultstr, "error negotiating T.38");
  2117. set_channel_variables(chan, details);
  2118. fax_session_release(s, token);
  2119. ao2_ref(s, -1);
  2120. ao2_ref(details, -1);
  2121. return -1;
  2122. }
  2123. } else {
  2124. details->caps |= AST_FAX_TECH_AUDIO;
  2125. }
  2126. if (!ast_test_flag(&opts, OPT_FORCE_AUDIO) && (details->caps & AST_FAX_TECH_T38)) {
  2127. if (sendfax_t38_init(chan, details)) {
  2128. ast_string_field_set(details, error, "T38_NEG_ERROR");
  2129. ast_string_field_set(details, resultstr, "error negotiating T.38");
  2130. set_channel_variables(chan, details);
  2131. fax_session_release(s, token);
  2132. ao2_ref(s, -1);
  2133. ao2_ref(details, -1);
  2134. ast_log(LOG_ERROR, "error initializing channel '%s' in T.38 mode\n", chan->name);
  2135. return -1;
  2136. }
  2137. } else {
  2138. details->option.send_cng = 1;
  2139. }
  2140. if ((channel_alive = generic_fax_exec(chan, details, s, token)) < 0) {
  2141. ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
  2142. }
  2143. if (ast_channel_get_t38_state(chan) == T38_STATE_NEGOTIATED) {
  2144. if (disable_t38(chan)) {
  2145. ast_debug(1, "error disabling T.38 mode on %s\n", chan->name);
  2146. }
  2147. }
  2148. if (!(filenames = generate_filenames_string(details, "FileName: ", "\r\n"))) {
  2149. ast_log(LOG_ERROR, "Error generating SendFAX manager event\n");
  2150. ao2_ref(s, -1);
  2151. ao2_ref(details, -1);
  2152. return (!channel_alive) ? -1 : 0;
  2153. }
  2154. /* send out the AMI completion event */
  2155. ast_channel_lock(chan);
  2156. get_manager_event_info(chan, &info);
  2157. manager_event(EVENT_FLAG_CALL,
  2158. "SendFAX",
  2159. "Channel: %s\r\n"
  2160. "Context: %s\r\n"
  2161. "Exten: %s\r\n"
  2162. "CallerID: %s\r\n"
  2163. "RemoteStationID: %s\r\n"
  2164. "LocalStationID: %s\r\n"
  2165. "PagesTransferred: %s\r\n"
  2166. "Resolution: %s\r\n"
  2167. "TransferRate: %s\r\n"
  2168. "%s\r\n",
  2169. chan->name,
  2170. info.context,
  2171. info.exten,
  2172. info.cid,
  2173. S_OR(pbx_builtin_getvar_helper(chan, "REMOTESTATIONID"), ""),
  2174. S_OR(pbx_builtin_getvar_helper(chan, "LOCALSTATIONID"), ""),
  2175. S_OR(pbx_builtin_getvar_helper(chan, "FAXPAGES"), ""),
  2176. S_OR(pbx_builtin_getvar_helper(chan, "FAXRESOLUTION"), ""),
  2177. S_OR(pbx_builtin_getvar_helper(chan, "FAXBITRATE"), ""),
  2178. filenames);
  2179. ast_channel_unlock(chan);
  2180. ast_free(filenames);
  2181. ao2_ref(s, -1);
  2182. ao2_ref(details, -1);
  2183. /* If the channel hungup return -1; otherwise, return 0 to continue in the dialplan */
  2184. return (!channel_alive) ? -1 : 0;
  2185. }
  2186. /*! \brief destroy the v21 detection parts of a fax gateway session */
  2187. static void destroy_v21_sessions(struct fax_gateway *gateway)
  2188. {
  2189. if (gateway->chan_v21_session) {
  2190. ao2_lock(faxregistry.container);
  2191. ao2_unlink(faxregistry.container, gateway->chan_v21_session);
  2192. ao2_unlock(faxregistry.container);
  2193. ao2_ref(gateway->chan_v21_session, -1);
  2194. gateway->chan_v21_session = NULL;
  2195. }
  2196. if (gateway->peer_v21_session) {
  2197. ao2_lock(faxregistry.container);
  2198. ao2_unlink(faxregistry.container, gateway->peer_v21_session);
  2199. ao2_unlock(faxregistry.container);
  2200. ao2_ref(gateway->peer_v21_session, -1);
  2201. gateway->peer_v21_session = NULL;
  2202. }
  2203. }
  2204. /*! \brief destroy a FAX gateway session structure */
  2205. static void destroy_gateway(void *data)
  2206. {
  2207. struct fax_gateway *gateway = data;
  2208. destroy_v21_sessions(gateway);
  2209. if (gateway->s) {
  2210. fax_session_release(gateway->s, gateway->token);
  2211. gateway->token = NULL;
  2212. ao2_lock(faxregistry.container);
  2213. ao2_unlink(faxregistry.container, gateway->s);
  2214. ao2_unlock(faxregistry.container);
  2215. ao2_ref(gateway->s, -1);
  2216. gateway->s = NULL;
  2217. }
  2218. }
  2219. /*! \brief Create a new fax gateway object.
  2220. * \param chan the channel the gateway object will be attached to
  2221. * \param details the fax session details
  2222. * \return NULL or a fax gateway object
  2223. */
  2224. static struct fax_gateway *fax_gateway_new(struct ast_channel *chan, struct ast_fax_session_details *details)
  2225. {
  2226. struct fax_gateway *gateway = ao2_alloc(sizeof(*gateway), destroy_gateway);
  2227. struct ast_fax_session_details *v21_details;
  2228. if (!gateway) {
  2229. return NULL;
  2230. }
  2231. if (!(v21_details = session_details_new())) {
  2232. ao2_ref(gateway, -1);
  2233. return NULL;
  2234. }
  2235. v21_details->caps = AST_FAX_TECH_V21_DETECT;
  2236. if (!(gateway->chan_v21_session = fax_session_new(v21_details, chan, NULL, NULL))) {
  2237. ao2_ref(v21_details, -1);
  2238. ao2_ref(gateway, -1);
  2239. return NULL;
  2240. }
  2241. if (!(gateway->peer_v21_session = fax_session_new(v21_details, chan, NULL, NULL))) {
  2242. ao2_ref(v21_details, -1);
  2243. ao2_ref(gateway, -1);
  2244. return NULL;
  2245. }
  2246. ao2_ref(v21_details, -1);
  2247. gateway->framehook = -1;
  2248. details->caps = AST_FAX_TECH_GATEWAY;
  2249. if (details->gateway_timeout && !(gateway->s = fax_session_reserve(details, &gateway->token))) {
  2250. details->caps &= ~AST_FAX_TECH_GATEWAY;
  2251. ast_log(LOG_ERROR, "Can't reserve a FAX session, gateway attempt failed.\n");
  2252. ao2_ref(gateway, -1);
  2253. return NULL;
  2254. }
  2255. return gateway;
  2256. }
  2257. /*! \brief Create a fax session and start T.30<->T.38 gateway mode
  2258. * \param gateway a fax gateway object
  2259. * \param details fax session details
  2260. * \param chan active channel
  2261. * \return 0 on error 1 on success*/
  2262. static int fax_gateway_start(struct fax_gateway *gateway, struct ast_fax_session_details *details, struct ast_channel *chan)
  2263. {
  2264. struct ast_fax_session *s;
  2265. /* create the FAX session */
  2266. if (!(s = fax_session_new(details, chan, gateway->s, gateway->token))) {
  2267. gateway->token = NULL;
  2268. ast_string_field_set(details, result, "FAILED");
  2269. ast_string_field_set(details, resultstr, "error starting gateway session");
  2270. ast_string_field_set(details, error, "INIT_ERROR");
  2271. set_channel_variables(chan, details);
  2272. report_fax_status(chan, details, "No Available Resource");
  2273. ast_log(LOG_ERROR, "Can't create a FAX session, gateway attempt failed.\n");
  2274. return -1;
  2275. }
  2276. /* release the reference for the reserved session and replace it with
  2277. * the real session */
  2278. if (gateway->s) {
  2279. ao2_ref(gateway->s, -1);
  2280. }
  2281. gateway->s = s;
  2282. gateway->token = NULL;
  2283. if (gateway->s->tech->start_session(gateway->s) < 0) {
  2284. ast_string_field_set(details, result, "FAILED");
  2285. ast_string_field_set(details, resultstr, "error starting gateway session");
  2286. ast_string_field_set(details, error, "INIT_ERROR");
  2287. set_channel_variables(chan, details);
  2288. return -1;
  2289. }
  2290. gateway->timeout_start.tv_sec = 0;
  2291. gateway->timeout_start.tv_usec = 0;
  2292. report_fax_status(chan, details, "FAX Transmission In Progress");
  2293. return 0;
  2294. }
  2295. static struct ast_frame *fax_gateway_request_t38(struct fax_gateway *gateway, struct ast_channel *chan, struct ast_frame *f)
  2296. {
  2297. struct ast_frame *fp;
  2298. struct ast_control_t38_parameters t38_parameters = {
  2299. .request_response = AST_T38_REQUEST_NEGOTIATE,
  2300. };
  2301. struct ast_frame control_frame = {
  2302. .src = "res_fax",
  2303. .frametype = AST_FRAME_CONTROL,
  2304. .datalen = sizeof(t38_parameters),
  2305. .subclass.integer = AST_CONTROL_T38_PARAMETERS,
  2306. .data.ptr = &t38_parameters,
  2307. };
  2308. struct ast_fax_session_details *details = find_details(chan);
  2309. if (!details) {
  2310. ast_log(LOG_ERROR, "no FAX session details found on chan %s for T.38 gateway session, odd\n", chan->name);
  2311. ast_framehook_detach(chan, gateway->framehook);
  2312. return f;
  2313. }
  2314. t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
  2315. ao2_ref(details, -1);
  2316. if (!(fp = ast_frisolate(&control_frame))) {
  2317. ast_log(LOG_ERROR, "error generating T.38 request control frame on chan %s for T.38 gateway session\n", chan->name);
  2318. return f;
  2319. }
  2320. gateway->t38_state = T38_STATE_NEGOTIATING;
  2321. gateway->timeout_start = ast_tvnow();
  2322. details->gateway_timeout = FAX_GATEWAY_TIMEOUT;
  2323. ast_debug(1, "requesting T.38 for gateway session for %s\n", chan->name);
  2324. return fp;
  2325. }
  2326. static struct ast_frame *fax_gateway_detect_v21(struct fax_gateway *gateway, struct ast_channel *chan, struct ast_channel *peer, struct ast_channel *active, struct ast_frame *f)
  2327. {
  2328. struct ast_channel *other = (active == chan) ? peer : chan;
  2329. struct ast_fax_session *active_v21_session = (active == chan) ? gateway->chan_v21_session : gateway->peer_v21_session;
  2330. if (!active_v21_session || gateway->detected_v21) {
  2331. return f;
  2332. }
  2333. if (active_v21_session->tech->write(active_v21_session, f) == 0 &&
  2334. active_v21_session->details->option.v21_detected) {
  2335. gateway->detected_v21 = 1;
  2336. }
  2337. if (gateway->detected_v21) {
  2338. destroy_v21_sessions(gateway);
  2339. if (ast_channel_get_t38_state(other) == T38_STATE_UNKNOWN) {
  2340. ast_debug(1, "detected v21 preamble from %s\n", active->name);
  2341. return fax_gateway_request_t38(gateway, chan, f);
  2342. } else {
  2343. ast_debug(1, "detected v21 preamble on %s, but %s does not support T.38 for T.38 gateway session\n", active->name, other->name);
  2344. }
  2345. }
  2346. return f;
  2347. }
  2348. static int fax_gateway_indicate_t38(struct ast_channel *chan, struct ast_channel *active, struct ast_control_t38_parameters *control_params)
  2349. {
  2350. if (active == chan) {
  2351. return ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, control_params, sizeof(*control_params));
  2352. } else {
  2353. return ast_queue_control_data(chan, AST_CONTROL_T38_PARAMETERS, control_params, sizeof(*control_params));
  2354. }
  2355. }
  2356. /*! \brief T38 Gateway Negotiate t38 parameters
  2357. * \param gateway gateway object
  2358. * \param chan channel running the gateway
  2359. * \param peer channel im bridged too
  2360. * \param active channel the frame originated on
  2361. * \param f the control frame to process
  2362. * \return processed control frame or null frame
  2363. */
  2364. static struct ast_frame *fax_gateway_detect_t38(struct fax_gateway *gateway, struct ast_channel *chan, struct ast_channel *peer, struct ast_channel *active, struct ast_frame *f)
  2365. {
  2366. struct ast_control_t38_parameters *control_params = f->data.ptr;
  2367. struct ast_channel *other = (active == chan) ? peer : chan;
  2368. struct ast_fax_session_details *details;
  2369. if (f->datalen != sizeof(struct ast_control_t38_parameters)) {
  2370. /* invalaid AST_CONTROL_T38_PARAMETERS frame, we can't
  2371. * do anything with it, pass it on */
  2372. return f;
  2373. }
  2374. /* ignore frames from ourselves */
  2375. if ((gateway->t38_state == T38_STATE_NEGOTIATED && control_params->request_response == AST_T38_NEGOTIATED)
  2376. || (gateway->t38_state == T38_STATE_REJECTED && control_params->request_response == AST_T38_REFUSED)
  2377. || (gateway->t38_state == T38_STATE_NEGOTIATING && control_params->request_response == AST_T38_REQUEST_TERMINATE)) {
  2378. return f;
  2379. }
  2380. if (!(details = find_details(chan))) {
  2381. ast_log(LOG_ERROR, "no FAX session details found on chan %s for T.38 gateway session, odd\n", chan->name);
  2382. ast_framehook_detach(chan, gateway->framehook);
  2383. return f;
  2384. }
  2385. if (control_params->request_response == AST_T38_REQUEST_NEGOTIATE) {
  2386. enum ast_t38_state state = ast_channel_get_t38_state(other);
  2387. if (state == T38_STATE_UNKNOWN) {
  2388. /* we detected a request to negotiate T.38 and the
  2389. * other channel appears to support T.38, we'll pass
  2390. * the request through and only step in if the other
  2391. * channel rejects the request */
  2392. ast_debug(1, "%s is attempting to negotiate T.38 with %s, we'll see what happens\n", active->name, other->name);
  2393. t38_parameters_ast_to_fax(&details->their_t38_parameters, control_params);
  2394. gateway->t38_state = T38_STATE_UNKNOWN;
  2395. gateway->timeout_start = ast_tvnow();
  2396. details->gateway_timeout = FAX_GATEWAY_TIMEOUT;
  2397. ao2_ref(details, -1);
  2398. return f;
  2399. } else if (state == T38_STATE_UNAVAILABLE || state == T38_STATE_REJECTED) {
  2400. /* the other channel does not support T.38, we need to
  2401. * step in here */
  2402. ast_debug(1, "%s is attempting to negotiate T.38 but %s does not support it\n", active->name, other->name);
  2403. ast_debug(1, "starting T.38 gateway for T.38 channel %s and G.711 channel %s\n", active->name, other->name);
  2404. t38_parameters_ast_to_fax(&details->their_t38_parameters, control_params);
  2405. t38_parameters_fax_to_ast(control_params, &details->our_t38_parameters);
  2406. if (fax_gateway_start(gateway, details, chan)) {
  2407. ast_log(LOG_ERROR, "error starting T.38 gateway for T.38 channel %s and G.711 channel %s\n", active->name, other->name);
  2408. gateway->t38_state = T38_STATE_REJECTED;
  2409. control_params->request_response = AST_T38_REFUSED;
  2410. ast_framehook_detach(chan, details->gateway_id);
  2411. details->gateway_id = -1;
  2412. } else {
  2413. gateway->t38_state = T38_STATE_NEGOTIATED;
  2414. control_params->request_response = AST_T38_NEGOTIATED;
  2415. report_fax_status(chan, details, "T.38 Negotiated");
  2416. }
  2417. fax_gateway_indicate_t38(chan, active, control_params);
  2418. ao2_ref(details, -1);
  2419. return &ast_null_frame;
  2420. } else if (gateway->t38_state == T38_STATE_NEGOTIATING) {
  2421. /* we got a request to negotiate T.38 after we already
  2422. * sent one to the other party based on v21 preamble
  2423. * detection. We'll just pretend we passed this request
  2424. * through in the first place. */
  2425. t38_parameters_ast_to_fax(&details->their_t38_parameters, control_params);
  2426. gateway->t38_state = T38_STATE_UNKNOWN;
  2427. gateway->timeout_start = ast_tvnow();
  2428. details->gateway_timeout = FAX_GATEWAY_TIMEOUT;
  2429. ast_debug(1, "%s is attempting to negotiate T.38 after we already sent a negotiation request based on v21 preamble detection\n", active->name);
  2430. ao2_ref(details, -1);
  2431. return &ast_null_frame;
  2432. } else if (gateway->t38_state == T38_STATE_NEGOTIATED) {
  2433. /* we got a request to negotiate T.38 after we already
  2434. * sent one to the other party based on v21 preamble
  2435. * detection and received a response. We need to
  2436. * respond to this and shut down the gateway. */
  2437. t38_parameters_fax_to_ast(control_params, &details->their_t38_parameters);
  2438. ast_framehook_detach(chan, details->gateway_id);
  2439. details->gateway_id = -1;
  2440. control_params->request_response = AST_T38_NEGOTIATED;
  2441. fax_gateway_indicate_t38(chan, active, control_params);
  2442. ast_string_field_set(details, result, "SUCCESS");
  2443. ast_string_field_set(details, resultstr, "no gateway necessary");
  2444. ast_string_field_set(details, error, "NATIVE_T38");
  2445. set_channel_variables(chan, details);
  2446. ast_debug(1, "%s is attempting to negotiate T.38 after we already negotiated T.38 with %s, disabling the gateway\n", active->name, other->name);
  2447. ao2_ref(details, -1);
  2448. return &ast_null_frame;
  2449. } else {
  2450. ast_log(LOG_WARNING, "%s is attempting to negotiate T.38 while %s is in an unsupported state\n", active->name, other->name);
  2451. ao2_ref(details, -1);
  2452. return f;
  2453. }
  2454. } else if (gateway->t38_state == T38_STATE_NEGOTIATING
  2455. && control_params->request_response == AST_T38_REFUSED) {
  2456. ast_debug(1, "unable to negotiate T.38 on %s for fax gateway\n", active->name);
  2457. /* our request to negotiate T.38 was refused, if the other
  2458. * channel supports T.38, they might still reinvite and save
  2459. * the day. Otherwise disable the gateway. */
  2460. if (ast_channel_get_t38_state(other) == T38_STATE_UNKNOWN) {
  2461. gateway->t38_state = T38_STATE_UNAVAILABLE;
  2462. } else {
  2463. ast_framehook_detach(chan, details->gateway_id);
  2464. details->gateway_id = -1;
  2465. ast_string_field_set(details, result, "FAILED");
  2466. ast_string_field_set(details, resultstr, "unable to negotiate T.38");
  2467. ast_string_field_set(details, error, "T38_NEG_ERROR");
  2468. set_channel_variables(chan, details);
  2469. }
  2470. ao2_ref(details, -1);
  2471. return &ast_null_frame;
  2472. } else if (gateway->t38_state == T38_STATE_NEGOTIATING
  2473. && control_params->request_response == AST_T38_NEGOTIATED) {
  2474. ast_debug(1, "starting T.38 gateway for T.38 channel %s and G.711 channel %s\n", active->name, other->name);
  2475. t38_parameters_ast_to_fax(&details->their_t38_parameters, control_params);
  2476. if (fax_gateway_start(gateway, details, chan)) {
  2477. ast_log(LOG_ERROR, "error starting T.38 gateway for T.38 channel %s and G.711 channel %s\n", active->name, other->name);
  2478. gateway->t38_state = T38_STATE_NEGOTIATING;
  2479. control_params->request_response = AST_T38_REQUEST_TERMINATE;
  2480. fax_gateway_indicate_t38(chan, active, control_params);
  2481. } else {
  2482. gateway->t38_state = T38_STATE_NEGOTIATED;
  2483. report_fax_status(chan, details, "T.38 Negotiated");
  2484. }
  2485. ao2_ref(details, -1);
  2486. return &ast_null_frame;
  2487. } else if (control_params->request_response == AST_T38_REFUSED) {
  2488. /* the other channel refused the request to negotiate T.38,
  2489. * we'll step in here and pretend the request was accepted */
  2490. ast_debug(1, "%s attempted to negotiate T.38 but %s refused the request\n", other->name, active->name);
  2491. ast_debug(1, "starting T.38 gateway for T.38 channel %s and G.711 channel %s\n", other->name, active->name);
  2492. t38_parameters_fax_to_ast(control_params, &details->our_t38_parameters);
  2493. if (fax_gateway_start(gateway, details, chan)) {
  2494. ast_log(LOG_ERROR, "error starting T.38 gateway for T.38 channel %s and G.711 channel %s\n", active->name, other->name);
  2495. gateway->t38_state = T38_STATE_REJECTED;
  2496. control_params->request_response = AST_T38_REFUSED;
  2497. ast_framehook_detach(chan, details->gateway_id);
  2498. details->gateway_id = -1;
  2499. } else {
  2500. gateway->t38_state = T38_STATE_NEGOTIATED;
  2501. control_params->request_response = AST_T38_NEGOTIATED;
  2502. }
  2503. ao2_ref(details, -1);
  2504. return f;
  2505. } else if (control_params->request_response == AST_T38_REQUEST_TERMINATE) {
  2506. /* the channel wishes to end our short relationship, we shall
  2507. * oblige */
  2508. ast_debug(1, "T.38 channel %s is requesting a shutdown of T.38, disabling the gateway\n", active->name);
  2509. ast_framehook_detach(chan, details->gateway_id);
  2510. details->gateway_id = -1;
  2511. gateway->t38_state = T38_STATE_REJECTED;
  2512. control_params->request_response = AST_T38_TERMINATED;
  2513. fax_gateway_indicate_t38(chan, active, control_params);
  2514. ao2_ref(details, -1);
  2515. return &ast_null_frame;
  2516. } else if (control_params->request_response == AST_T38_NEGOTIATED) {
  2517. ast_debug(1, "T.38 successfully negotiated between %s and %s, no gateway necessary\n", active->name, other->name);
  2518. ast_framehook_detach(chan, details->gateway_id);
  2519. details->gateway_id = -1;
  2520. ast_string_field_set(details, result, "SUCCESS");
  2521. ast_string_field_set(details, resultstr, "no gateway necessary");
  2522. ast_string_field_set(details, error, "NATIVE_T38");
  2523. set_channel_variables(chan, details);
  2524. ao2_ref(details, -1);
  2525. return f;
  2526. } else if (control_params->request_response == AST_T38_TERMINATED) {
  2527. ast_debug(1, "T.38 disabled on channel %s\n", active->name);
  2528. ast_framehook_detach(chan, details->gateway_id);
  2529. details->gateway_id = -1;
  2530. ao2_ref(details, -1);
  2531. return &ast_null_frame;
  2532. }
  2533. ao2_ref(details, -1);
  2534. return f;
  2535. }
  2536. /*! \brief Destroy the gateway data structure when the framehook is detached
  2537. * \param data framehook data (gateway data)*/
  2538. static void fax_gateway_framehook_destroy(void *data) {
  2539. struct fax_gateway *gateway = data;
  2540. if (gateway->s) {
  2541. switch (gateway->s->state) {
  2542. case AST_FAX_STATE_INITIALIZED:
  2543. case AST_FAX_STATE_OPEN:
  2544. case AST_FAX_STATE_ACTIVE:
  2545. case AST_FAX_STATE_COMPLETE:
  2546. if (gateway->s->tech->cancel_session) {
  2547. gateway->s->tech->cancel_session(gateway->s);
  2548. }
  2549. /* fall through */
  2550. default:
  2551. break;
  2552. }
  2553. }
  2554. ao2_ref(gateway, -1);
  2555. }
  2556. /*! \brief T.30<->T.38 gateway framehook.
  2557. *
  2558. * Intercept packets on bridged channels and determine if a T.38 gateway is
  2559. * required. If a gateway is required, start a gateway and handle T.38
  2560. * negotiation if necessary.
  2561. *
  2562. * \param chan channel running the gateway
  2563. * \param f frame to handle may be NULL
  2564. * \param event framehook event
  2565. * \param data framehook data (struct fax_gateway *)
  2566. *
  2567. * \return processed frame or NULL when f is NULL or a null frame
  2568. */
  2569. static struct ast_frame *fax_gateway_framehook(struct ast_channel *chan, struct ast_frame *f, enum ast_framehook_event event, void *data) {
  2570. struct fax_gateway *gateway = data;
  2571. struct ast_channel *peer, *active;
  2572. struct ast_fax_session_details *details;
  2573. if (gateway->s) {
  2574. details = gateway->s->details;
  2575. ao2_ref(details, 1);
  2576. } else {
  2577. if (!(details = find_details(chan))) {
  2578. ast_log(LOG_ERROR, "no FAX session details found on chan %s for T.38 gateway session, odd\n", chan->name);
  2579. ast_framehook_detach(chan, gateway->framehook);
  2580. return f;
  2581. }
  2582. }
  2583. /* restore audio formats when we are detached */
  2584. if (event == AST_FRAMEHOOK_EVENT_DETACHED) {
  2585. set_channel_variables(chan, details);
  2586. if (gateway->bridged) {
  2587. ast_set_read_format(chan, &gateway->chan_read_format);
  2588. ast_set_read_format(chan, &gateway->chan_write_format);
  2589. if ((peer = ast_bridged_channel(chan))) {
  2590. ast_set_read_format(peer, &gateway->peer_read_format);
  2591. ast_set_read_format(peer, &gateway->peer_write_format);
  2592. ast_channel_make_compatible(chan, peer);
  2593. }
  2594. }
  2595. ao2_ref(details, -1);
  2596. return NULL;
  2597. }
  2598. if (!f || (event == AST_FRAMEHOOK_EVENT_ATTACHED)) {
  2599. ao2_ref(details, -1);
  2600. return NULL;
  2601. };
  2602. /* this frame was generated by the fax gateway, pass it on */
  2603. if (ast_test_flag(f, AST_FAX_FRFLAG_GATEWAY)) {
  2604. ao2_ref(details, -1);
  2605. return f;
  2606. }
  2607. if (!(peer = ast_bridged_channel(chan))) {
  2608. /* not bridged, don't do anything */
  2609. ao2_ref(details, -1);
  2610. return f;
  2611. }
  2612. if (!gateway->bridged && peer) {
  2613. /* don't start a gateway if neither channel can handle T.38 */
  2614. if (ast_channel_get_t38_state(chan) == T38_STATE_UNAVAILABLE && ast_channel_get_t38_state(peer) == T38_STATE_UNAVAILABLE) {
  2615. ast_debug(1, "not starting gateway for %s and %s; neither channel supports T.38\n", chan->name, peer->name);
  2616. ast_framehook_detach(chan, gateway->framehook);
  2617. details->gateway_id = -1;
  2618. ast_string_field_set(details, result, "FAILED");
  2619. ast_string_field_set(details, resultstr, "neither channel supports T.38");
  2620. ast_string_field_set(details, error, "T38_NEG_ERROR");
  2621. set_channel_variables(chan, details);
  2622. ao2_ref(details, -1);
  2623. return f;
  2624. }
  2625. if (details->gateway_timeout) {
  2626. gateway->timeout_start = ast_tvnow();
  2627. }
  2628. /* we are bridged, change r/w formats to SLIN for v21 preamble
  2629. * detection and T.30 */
  2630. ast_format_copy(&gateway->chan_read_format, &chan->readformat);
  2631. ast_format_copy(&gateway->chan_write_format, &chan->readformat);
  2632. ast_format_copy(&gateway->peer_read_format, &peer->readformat);
  2633. ast_format_copy(&gateway->peer_write_format, &peer->readformat);
  2634. ast_set_read_format_by_id(chan, AST_FORMAT_SLINEAR);
  2635. ast_set_write_format_by_id(chan, AST_FORMAT_SLINEAR);
  2636. ast_set_read_format_by_id(peer, AST_FORMAT_SLINEAR);
  2637. ast_set_write_format_by_id(peer, AST_FORMAT_SLINEAR);
  2638. ast_channel_make_compatible(chan, peer);
  2639. gateway->bridged = 1;
  2640. }
  2641. if (gateway->bridged && !ast_tvzero(gateway->timeout_start)) {
  2642. if (ast_tvdiff_ms(ast_tvnow(), gateway->timeout_start) > details->gateway_timeout) {
  2643. ast_debug(1, "no fax activity between %s and %s after %d ms, disabling gateway\n", chan->name, peer->name, details->gateway_timeout);
  2644. ast_framehook_detach(chan, gateway->framehook);
  2645. details->gateway_id = -1;
  2646. ast_string_field_set(details, result, "FAILED");
  2647. ast_string_field_build(details, resultstr, "no fax activity after %d ms", details->gateway_timeout);
  2648. ast_string_field_set(details, error, "TIMEOUT");
  2649. set_channel_variables(chan, details);
  2650. ao2_ref(details, -1);
  2651. return f;
  2652. }
  2653. }
  2654. /* only handle VOICE, MODEM, and CONTROL frames*/
  2655. switch (f->frametype) {
  2656. case AST_FRAME_VOICE:
  2657. switch (f->subclass.format.id) {
  2658. case AST_FORMAT_SLINEAR:
  2659. case AST_FORMAT_ALAW:
  2660. case AST_FORMAT_ULAW:
  2661. break;
  2662. default:
  2663. ao2_ref(details, -1);
  2664. return f;
  2665. }
  2666. break;
  2667. case AST_FRAME_MODEM:
  2668. if (f->subclass.integer == AST_MODEM_T38) {
  2669. break;
  2670. }
  2671. ao2_ref(details, -1);
  2672. return f;
  2673. case AST_FRAME_CONTROL:
  2674. if (f->subclass.integer == AST_CONTROL_T38_PARAMETERS) {
  2675. break;
  2676. }
  2677. ao2_ref(details, -1);
  2678. return f;
  2679. default:
  2680. ao2_ref(details, -1);
  2681. return f;
  2682. }
  2683. /* detect the active channel */
  2684. switch (event) {
  2685. case AST_FRAMEHOOK_EVENT_WRITE:
  2686. active = peer;
  2687. break;
  2688. case AST_FRAMEHOOK_EVENT_READ:
  2689. active = chan;
  2690. break;
  2691. default:
  2692. ast_log(LOG_WARNING, "unhandled framehook event %i\n", event);
  2693. ao2_ref(details, -1);
  2694. return f;
  2695. }
  2696. /* handle control frames */
  2697. if (f->frametype == AST_FRAME_CONTROL && f->subclass.integer == AST_CONTROL_T38_PARAMETERS) {
  2698. ao2_ref(details, -1);
  2699. return fax_gateway_detect_t38(gateway, chan, peer, active, f);
  2700. }
  2701. if (!gateway->detected_v21 && gateway->t38_state == T38_STATE_UNAVAILABLE && f->frametype == AST_FRAME_VOICE) {
  2702. /* not in gateway mode and have not detected v21 yet, listen
  2703. * for v21 */
  2704. ao2_ref(details, -1);
  2705. return fax_gateway_detect_v21(gateway, chan, peer, active, f);
  2706. }
  2707. /* in gateway mode, gateway some packets */
  2708. if (gateway->t38_state == T38_STATE_NEGOTIATED) {
  2709. /* framehooks are called in __ast_read() before frame format
  2710. * translation is done, so we need to translate here */
  2711. if ((f->frametype == AST_FRAME_VOICE) && (f->subclass.format.id != AST_FORMAT_SLINEAR)) {
  2712. if (active->readtrans && (f = ast_translate(active->readtrans, f, 1)) == NULL) {
  2713. f = &ast_null_frame;
  2714. ao2_ref(details, -1);
  2715. return f;
  2716. }
  2717. }
  2718. /* XXX we ignore the return value here, perhaps we should
  2719. * disable the gateway if a write fails. I am not sure how a
  2720. * write would fail, or even if a failure would be fatal so for
  2721. * now we'll just ignore the return value. */
  2722. gateway->s->tech->write(gateway->s, f);
  2723. if ((f->frametype == AST_FRAME_VOICE) && (f->subclass.format.id != AST_FORMAT_SLINEAR) && active->readtrans) {
  2724. /* Only free the frame if we translated / duplicated it - otherwise,
  2725. * let whatever is outside the frame hook do it */
  2726. ast_frfree(f);
  2727. }
  2728. f = &ast_null_frame;
  2729. ao2_ref(details, -1);
  2730. return f;
  2731. }
  2732. /* force silence on the line if T.38 negotiation might be taking place */
  2733. if (gateway->t38_state != T38_STATE_UNAVAILABLE && gateway->t38_state != T38_STATE_REJECTED) {
  2734. if (f->frametype == AST_FRAME_VOICE && f->subclass.format.id == AST_FORMAT_SLINEAR) {
  2735. short silence_buf[f->samples];
  2736. struct ast_frame silence_frame = {
  2737. .frametype = AST_FRAME_VOICE,
  2738. .data.ptr = silence_buf,
  2739. .samples = f->samples,
  2740. .datalen = sizeof(silence_buf),
  2741. };
  2742. ast_format_set(&silence_frame.subclass.format, AST_FORMAT_SLINEAR, 0);
  2743. memset(silence_buf, 0, sizeof(silence_buf));
  2744. ao2_ref(details, -1);
  2745. return ast_frisolate(&silence_frame);
  2746. } else {
  2747. ao2_ref(details, -1);
  2748. return &ast_null_frame;
  2749. }
  2750. }
  2751. ao2_ref(details, -1);
  2752. return f;
  2753. }
  2754. /*! \brief Attach a gateway framehook object to a channel.
  2755. * \param chan the channel to attach to
  2756. * \param details fax session details
  2757. * \return the framehook id of the attached framehook or -1 on error
  2758. * \retval -1 error
  2759. */
  2760. static int fax_gateway_attach(struct ast_channel *chan, struct ast_fax_session_details *details)
  2761. {
  2762. struct fax_gateway *gateway;
  2763. struct ast_framehook_interface fr_hook = {
  2764. .version = AST_FRAMEHOOK_INTERFACE_VERSION,
  2765. .event_cb = fax_gateway_framehook,
  2766. .destroy_cb = fax_gateway_framehook_destroy,
  2767. };
  2768. ast_string_field_set(details, result, "SUCCESS");
  2769. ast_string_field_set(details, resultstr, "gateway operation started successfully");
  2770. ast_string_field_set(details, error, "NO_ERROR");
  2771. set_channel_variables(chan, details);
  2772. /* set up the frame hook*/
  2773. gateway = fax_gateway_new(chan, details);
  2774. if (!gateway) {
  2775. ast_string_field_set(details, result, "FAILED");
  2776. ast_string_field_set(details, resultstr, "error initializing gateway session");
  2777. ast_string_field_set(details, error, "INIT_ERROR");
  2778. set_channel_variables(chan, details);
  2779. report_fax_status(chan, details, "No Available Resource");
  2780. return -1;
  2781. }
  2782. fr_hook.data = gateway;
  2783. ast_channel_lock(chan);
  2784. gateway->framehook = ast_framehook_attach(chan, &fr_hook);
  2785. ast_channel_unlock(chan);
  2786. if (gateway->framehook < 0) {
  2787. ao2_ref(gateway, -1);
  2788. ast_string_field_set(details, result, "FAILED");
  2789. ast_string_field_set(details, resultstr, "error attaching gateway to channel");
  2790. ast_string_field_set(details, error, "INIT_ERROR");
  2791. set_channel_variables(chan, details);
  2792. return -1;
  2793. }
  2794. return gateway->framehook;
  2795. }
  2796. /*! \brief hash callback for ao2 */
  2797. static int session_hash_cb(const void *obj, const int flags)
  2798. {
  2799. const struct ast_fax_session *s = obj;
  2800. return s->id;
  2801. }
  2802. /*! \brief compare callback for ao2 */
  2803. static int session_cmp_cb(void *obj, void *arg, int flags)
  2804. {
  2805. struct ast_fax_session *lhs = obj, *rhs = arg;
  2806. return (lhs->id == rhs->id) ? CMP_MATCH | CMP_STOP : 0;
  2807. }
  2808. /*! \brief fax session tab completion */
  2809. static char *fax_session_tab_complete(struct ast_cli_args *a)
  2810. {
  2811. int tklen;
  2812. int wordnum = 0;
  2813. char *name = NULL;
  2814. struct ao2_iterator i;
  2815. struct ast_fax_session *s;
  2816. char tbuf[5];
  2817. if (a->pos != 3) {
  2818. return NULL;
  2819. }
  2820. tklen = strlen(a->word);
  2821. i = ao2_iterator_init(faxregistry.container, 0);
  2822. while ((s = ao2_iterator_next(&i))) {
  2823. snprintf(tbuf, sizeof(tbuf), "%d", s->id);
  2824. if (!strncasecmp(a->word, tbuf, tklen) && ++wordnum > a->n) {
  2825. name = ast_strdup(tbuf);
  2826. ao2_ref(s, -1);
  2827. break;
  2828. }
  2829. ao2_ref(s, -1);
  2830. }
  2831. ao2_iterator_destroy(&i);
  2832. return name;
  2833. }
  2834. static char *cli_fax_show_version(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2835. {
  2836. struct fax_module *fax;
  2837. switch(cmd) {
  2838. case CLI_INIT:
  2839. e->command = "fax show version";
  2840. e->usage =
  2841. "Usage: fax show version\n"
  2842. " Show versions of FAX For Asterisk components.\n";
  2843. return NULL;
  2844. case CLI_GENERATE:
  2845. return NULL;
  2846. }
  2847. if (a->argc != 3) {
  2848. return CLI_SHOWUSAGE;
  2849. }
  2850. ast_cli(a->fd, "FAX For Asterisk Components:\n");
  2851. ast_cli(a->fd, "\tApplications: %s\n", ast_get_version());
  2852. AST_RWLIST_RDLOCK(&faxmodules);
  2853. AST_RWLIST_TRAVERSE(&faxmodules, fax, list) {
  2854. ast_cli(a->fd, "\t%s: %s\n", fax->tech->description, fax->tech->version);
  2855. }
  2856. AST_RWLIST_UNLOCK(&faxmodules);
  2857. ast_cli(a->fd, "\n");
  2858. return CLI_SUCCESS;
  2859. }
  2860. /*! \brief enable FAX debugging */
  2861. static char *cli_fax_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2862. {
  2863. int flag;
  2864. const char *what;
  2865. switch (cmd) {
  2866. case CLI_INIT:
  2867. e->command = "fax set debug {on|off}";
  2868. e->usage =
  2869. "Usage: fax set debug { on | off }\n"
  2870. " Enable/Disable FAX debugging on new FAX sessions. The basic FAX debugging will result in\n"
  2871. " additional events sent to manager sessions with 'call' class permissions. When\n"
  2872. " verbosity is greater than '5' events will be displayed to the console and audio versus\n"
  2873. " energy analysis will be performed and displayed to the console.\n";
  2874. return NULL;
  2875. case CLI_GENERATE:
  2876. return NULL;
  2877. }
  2878. what = a->argv[e->args-1]; /* guaranteed to exist */
  2879. if (!strcasecmp(what, "on")) {
  2880. flag = 1;
  2881. } else if (!strcasecmp(what, "off")) {
  2882. flag = 0;
  2883. } else {
  2884. return CLI_SHOWUSAGE;
  2885. }
  2886. global_fax_debug = flag;
  2887. ast_cli(a->fd, "\n\nFAX Debug %s\n\n", (flag) ? "Enabled" : "Disabled");
  2888. return CLI_SUCCESS;
  2889. }
  2890. /*! \brief display registered FAX capabilities */
  2891. static char *cli_fax_show_capabilities(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2892. {
  2893. struct fax_module *fax;
  2894. unsigned int num_modules = 0;
  2895. switch (cmd) {
  2896. case CLI_INIT:
  2897. e->command = "fax show capabilities";
  2898. e->usage =
  2899. "Usage: fax show capabilities\n"
  2900. " Shows the capabilities of the registered FAX technology modules\n";
  2901. return NULL;
  2902. case CLI_GENERATE:
  2903. return NULL;
  2904. }
  2905. ast_cli(a->fd, "\n\nRegistered FAX Technology Modules:\n\n");
  2906. AST_RWLIST_RDLOCK(&faxmodules);
  2907. AST_RWLIST_TRAVERSE(&faxmodules, fax, list) {
  2908. ast_cli(a->fd, "%-15s : %s\n%-15s : %s\n%-15s : ", "Type", fax->tech->type, "Description", fax->tech->description, "Capabilities");
  2909. fax->tech->cli_show_capabilities(a->fd);
  2910. num_modules++;
  2911. }
  2912. AST_RWLIST_UNLOCK(&faxmodules);
  2913. ast_cli(a->fd, "%d registered modules\n\n", num_modules);
  2914. return CLI_SUCCESS;
  2915. }
  2916. /*! \brief display global defaults and settings */
  2917. static char *cli_fax_show_settings(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2918. {
  2919. struct fax_module *fax;
  2920. char modems[128] = "";
  2921. struct fax_options options;
  2922. switch (cmd) {
  2923. case CLI_INIT:
  2924. e->command = "fax show settings";
  2925. e->usage =
  2926. "Usage: fax show settings\n"
  2927. " Show the global settings and defaults of both the FAX core and technology modules\n";
  2928. return NULL;
  2929. case CLI_GENERATE:
  2930. return NULL;
  2931. }
  2932. get_general_options(&options);
  2933. ast_cli(a->fd, "FAX For Asterisk Settings:\n");
  2934. ast_cli(a->fd, "\tECM: %s\n", options.ecm ? "Enabled" : "Disabled");
  2935. ast_cli(a->fd, "\tStatus Events: %s\n", options.statusevents ? "On" : "Off");
  2936. ast_cli(a->fd, "\tMinimum Bit Rate: %d\n", options.minrate);
  2937. ast_cli(a->fd, "\tMaximum Bit Rate: %d\n", options.maxrate);
  2938. ast_fax_modem_to_str(options.modems, modems, sizeof(modems));
  2939. ast_cli(a->fd, "\tModem Modulations Allowed: %s\n", modems);
  2940. ast_cli(a->fd, "\n\nFAX Technology Modules:\n\n");
  2941. AST_RWLIST_RDLOCK(&faxmodules);
  2942. AST_RWLIST_TRAVERSE(&faxmodules, fax, list) {
  2943. ast_cli(a->fd, "%s (%s) Settings:\n", fax->tech->type, fax->tech->description);
  2944. fax->tech->cli_show_settings(a->fd);
  2945. }
  2946. AST_RWLIST_UNLOCK(&faxmodules);
  2947. return CLI_SUCCESS;
  2948. }
  2949. /*! \brief display details of a specified fax session */
  2950. static char *cli_fax_show_session(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2951. {
  2952. struct ast_fax_session *s, tmp;
  2953. switch (cmd) {
  2954. case CLI_INIT:
  2955. e->command = "fax show session";
  2956. e->usage =
  2957. "Usage: fax show session <session number>\n"
  2958. " Shows status of the named FAX session\n";
  2959. return NULL;
  2960. case CLI_GENERATE:
  2961. return fax_session_tab_complete(a);
  2962. }
  2963. if (a->argc != 4) {
  2964. return CLI_SHOWUSAGE;
  2965. }
  2966. if (sscanf(a->argv[3], "%d", &tmp.id) != 1) {
  2967. ast_log(LOG_ERROR, "invalid session id: '%s'\n", a->argv[3]);
  2968. return RESULT_SUCCESS;
  2969. }
  2970. ast_cli(a->fd, "\nFAX Session Details:\n--------------------\n\n");
  2971. s = ao2_find(faxregistry.container, &tmp, OBJ_POINTER);
  2972. if (s) {
  2973. s->tech->cli_show_session(s, a->fd);
  2974. ao2_ref(s, -1);
  2975. }
  2976. ast_cli(a->fd, "\n\n");
  2977. return CLI_SUCCESS;
  2978. }
  2979. /*! \brief display fax stats */
  2980. static char *cli_fax_show_stats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2981. {
  2982. struct fax_module *fax;
  2983. switch (cmd) {
  2984. case CLI_INIT:
  2985. e->command = "fax show stats";
  2986. e->usage =
  2987. "Usage: fax show stats\n"
  2988. " Shows a statistical summary of FAX transmissions\n";
  2989. return NULL;
  2990. case CLI_GENERATE:
  2991. return NULL;
  2992. }
  2993. ast_cli(a->fd, "\nFAX Statistics:\n---------------\n\n");
  2994. ast_cli(a->fd, "%-20.20s : %d\n", "Current Sessions", faxregistry.active_sessions);
  2995. ast_cli(a->fd, "%-20.20s : %d\n", "Reserved Sessions", faxregistry.reserved_sessions);
  2996. ast_cli(a->fd, "%-20.20s : %d\n", "Transmit Attempts", faxregistry.fax_tx_attempts);
  2997. ast_cli(a->fd, "%-20.20s : %d\n", "Receive Attempts", faxregistry.fax_rx_attempts);
  2998. ast_cli(a->fd, "%-20.20s : %d\n", "Completed FAXes", faxregistry.fax_complete);
  2999. ast_cli(a->fd, "%-20.20s : %d\n", "Failed FAXes", faxregistry.fax_failures);
  3000. AST_RWLIST_RDLOCK(&faxmodules);
  3001. AST_RWLIST_TRAVERSE(&faxmodules, fax, list) {
  3002. fax->tech->cli_show_stats(a->fd);
  3003. }
  3004. AST_RWLIST_UNLOCK(&faxmodules);
  3005. ast_cli(a->fd, "\n\n");
  3006. return CLI_SUCCESS;
  3007. }
  3008. static const char *cli_session_type(struct ast_fax_session *s)
  3009. {
  3010. if (s->details->caps & AST_FAX_TECH_AUDIO) {
  3011. return "G.711";
  3012. }
  3013. if (s->details->caps & AST_FAX_TECH_T38) {
  3014. return "T.38";
  3015. }
  3016. return "none";
  3017. }
  3018. static const char *cli_session_operation(struct ast_fax_session *s)
  3019. {
  3020. if (s->details->caps & AST_FAX_TECH_GATEWAY) {
  3021. return "gateway";
  3022. }
  3023. if (s->details->caps & AST_FAX_TECH_SEND) {
  3024. return "send";
  3025. }
  3026. if (s->details->caps & AST_FAX_TECH_RECEIVE) {
  3027. return "receive";
  3028. }
  3029. if (s->details->caps & AST_FAX_TECH_V21_DETECT) {
  3030. return "V.21";
  3031. }
  3032. return "none";
  3033. }
  3034. /*! \brief display fax sessions */
  3035. static char *cli_fax_show_sessions(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3036. {
  3037. struct ast_fax_session *s;
  3038. struct ao2_iterator i;
  3039. int session_count;
  3040. char *filenames;
  3041. switch (cmd) {
  3042. case CLI_INIT:
  3043. e->command = "fax show sessions";
  3044. e->usage =
  3045. "Usage: fax show sessions\n"
  3046. " Shows the current FAX sessions\n";
  3047. return NULL;
  3048. case CLI_GENERATE:
  3049. return NULL;
  3050. }
  3051. ast_cli(a->fd, "\nCurrent FAX Sessions:\n\n");
  3052. ast_cli(a->fd, "%-20.20s %-10.10s %-10.10s %-5.5s %-10.10s %-15.15s %-30.30s\n",
  3053. "Channel", "Tech", "FAXID", "Type", "Operation", "State", "File(s)");
  3054. i = ao2_iterator_init(faxregistry.container, 0);
  3055. while ((s = ao2_iterator_next(&i))) {
  3056. ao2_lock(s);
  3057. filenames = generate_filenames_string(s->details, "", ", ");
  3058. ast_cli(a->fd, "%-20.20s %-10.10s %-10d %-5.5s %-10.10s %-15.15s %-30s\n",
  3059. s->channame, s->tech->type, s->id,
  3060. cli_session_type(s),
  3061. cli_session_operation(s),
  3062. ast_fax_state_to_str(s->state), S_OR(filenames, ""));
  3063. ast_free(filenames);
  3064. ao2_unlock(s);
  3065. ao2_ref(s, -1);
  3066. }
  3067. ao2_iterator_destroy(&i);
  3068. session_count = ao2_container_count(faxregistry.container);
  3069. ast_cli(a->fd, "\n%d FAX sessions\n\n", session_count);
  3070. return CLI_SUCCESS;
  3071. }
  3072. static struct ast_cli_entry fax_cli[] = {
  3073. AST_CLI_DEFINE(cli_fax_show_version, "Show versions of FAX For Asterisk components"),
  3074. AST_CLI_DEFINE(cli_fax_set_debug, "Enable/Disable FAX debugging on new FAX sessions"),
  3075. AST_CLI_DEFINE(cli_fax_show_capabilities, "Show the capabilities of the registered FAX technology modules"),
  3076. AST_CLI_DEFINE(cli_fax_show_settings, "Show the global settings and defaults of both the FAX core and technology modules"),
  3077. AST_CLI_DEFINE(cli_fax_show_session, "Show the status of the named FAX sessions"),
  3078. AST_CLI_DEFINE(cli_fax_show_sessions, "Show the current FAX sessions"),
  3079. AST_CLI_DEFINE(cli_fax_show_stats, "Summarize FAX session history"),
  3080. };
  3081. static void set_general_options(const struct fax_options *options)
  3082. {
  3083. ast_rwlock_wrlock(&options_lock);
  3084. general_options = *options;
  3085. ast_rwlock_unlock(&options_lock);
  3086. }
  3087. static void get_general_options(struct fax_options *options)
  3088. {
  3089. ast_rwlock_rdlock(&options_lock);
  3090. *options = general_options;
  3091. ast_rwlock_unlock(&options_lock);
  3092. }
  3093. /*! \brief configure res_fax */
  3094. static int set_config(int reload)
  3095. {
  3096. struct ast_config *cfg;
  3097. struct ast_variable *v;
  3098. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  3099. char modems[128] = "";
  3100. struct fax_options options;
  3101. int res = 0;
  3102. options = default_options;
  3103. /* When we're not reloading, we have to be certain to set the general options
  3104. * to the defaults in case config loading goes wrong at some point. On a reload,
  3105. * the general options need to stay the same as what they were prior to the
  3106. * reload rather than being reset to the defaults.
  3107. */
  3108. if (!reload) {
  3109. set_general_options(&options);
  3110. }
  3111. /* read configuration */
  3112. if (!(cfg = ast_config_load2(config, "res_fax", config_flags))) {
  3113. ast_log(LOG_NOTICE, "Configuration file '%s' not found, %s options.\n",
  3114. config, reload ? "not changing" : "using default");
  3115. return 0;
  3116. }
  3117. if (cfg == CONFIG_STATUS_FILEINVALID) {
  3118. ast_log(LOG_NOTICE, "Configuration file '%s' is invalid, %s options.\n",
  3119. config, reload ? "not changing" : "using default");
  3120. return 0;
  3121. }
  3122. if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
  3123. return 0;
  3124. }
  3125. if (reload) {
  3126. options = default_options;
  3127. }
  3128. /* create configuration */
  3129. for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
  3130. int rate;
  3131. if (!strcasecmp(v->name, "minrate")) {
  3132. ast_debug(3, "reading minrate '%s' from configuration file\n", v->value);
  3133. if ((rate = fax_rate_str_to_int(v->value)) == 0) {
  3134. res = -1;
  3135. goto end;
  3136. }
  3137. options.minrate = rate;
  3138. } else if (!strcasecmp(v->name, "maxrate")) {
  3139. ast_debug(3, "reading maxrate '%s' from configuration file\n", v->value);
  3140. if ((rate = fax_rate_str_to_int(v->value)) == 0) {
  3141. res = -1;
  3142. goto end;
  3143. }
  3144. options.maxrate = rate;
  3145. } else if (!strcasecmp(v->name, "statusevents")) {
  3146. ast_debug(3, "reading statusevents '%s' from configuration file\n", v->value);
  3147. options.statusevents = ast_true(v->value);
  3148. } else if (!strcasecmp(v->name, "ecm")) {
  3149. ast_debug(3, "reading ecm '%s' from configuration file\n", v->value);
  3150. options.ecm = ast_true(v->value);
  3151. } else if ((!strcasecmp(v->name, "modem")) || (!strcasecmp(v->name, "modems"))) {
  3152. options.modems = 0;
  3153. update_modem_bits(&options.modems, v->value);
  3154. }
  3155. }
  3156. if (options.maxrate < options.minrate) {
  3157. ast_log(LOG_ERROR, "maxrate %d is less than minrate %d\n", options.maxrate, options.minrate);
  3158. res = -1;
  3159. goto end;
  3160. }
  3161. if (check_modem_rate(options.modems, options.minrate)) {
  3162. ast_fax_modem_to_str(options.modems, modems, sizeof(modems));
  3163. ast_log(LOG_ERROR, "'modems' setting '%s' is incompatible with 'minrate' setting %d\n", modems, options.minrate);
  3164. res = -1;
  3165. goto end;
  3166. }
  3167. if (check_modem_rate(options.modems, options.maxrate)) {
  3168. ast_fax_modem_to_str(options.modems, modems, sizeof(modems));
  3169. ast_log(LOG_ERROR, "'modems' setting '%s' is incompatible with 'maxrate' setting %d\n", modems, options.maxrate);
  3170. res = -1;
  3171. goto end;
  3172. }
  3173. set_general_options(&options);
  3174. end:
  3175. ast_config_destroy(cfg);
  3176. return res;
  3177. }
  3178. /*! \brief FAXOPT read function returns the contents of a FAX option */
  3179. static int acf_faxopt_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  3180. {
  3181. struct ast_fax_session_details *details = find_details(chan);
  3182. int res = 0;
  3183. char *filenames;
  3184. if (!details) {
  3185. ast_log(LOG_ERROR, "channel '%s' can't read FAXOPT(%s) because it has never been written.\n", chan->name, data);
  3186. return -1;
  3187. }
  3188. if (!strcasecmp(data, "ecm")) {
  3189. ast_copy_string(buf, details->option.ecm ? "yes" : "no", len);
  3190. } else if (!strcasecmp(data, "t38gateway") || !strcasecmp(data, "gateway") ||
  3191. !strcasecmp(data, "t38_gateway") || !strcasecmp(data, "faxgateway")) {
  3192. ast_copy_string(buf, details->gateway_id != -1 ? "yes" : "no", len);
  3193. } else if (!strcasecmp(data, "error")) {
  3194. ast_copy_string(buf, details->error, len);
  3195. } else if (!strcasecmp(data, "filename")) {
  3196. if (AST_LIST_EMPTY(&details->documents)) {
  3197. ast_log(LOG_ERROR, "channel '%s' can't read FAXOPT(%s) because it has never been written.\n", chan->name, data);
  3198. res = -1;
  3199. } else {
  3200. ast_copy_string(buf, AST_LIST_FIRST(&details->documents)->filename, len);
  3201. }
  3202. } else if (!strcasecmp(data, "filenames")) {
  3203. if (AST_LIST_EMPTY(&details->documents)) {
  3204. ast_log(LOG_ERROR, "channel '%s' can't read FAXOPT(%s) because it has never been written.\n", chan->name, data);
  3205. res = -1;
  3206. } else if ((filenames = generate_filenames_string(details, "", ","))) {
  3207. ast_copy_string(buf, filenames, len);
  3208. ast_free(filenames);
  3209. } else {
  3210. ast_log(LOG_ERROR, "channel '%s' can't read FAXOPT(%s), there was an error generating the filenames list.\n", chan->name, data);
  3211. res = -1;
  3212. }
  3213. } else if (!strcasecmp(data, "headerinfo")) {
  3214. ast_copy_string(buf, details->headerinfo, len);
  3215. } else if (!strcasecmp(data, "localstationid")) {
  3216. ast_copy_string(buf, details->localstationid, len);
  3217. } else if (!strcasecmp(data, "maxrate")) {
  3218. snprintf(buf, len, "%d", details->maxrate);
  3219. } else if (!strcasecmp(data, "minrate")) {
  3220. snprintf(buf, len, "%d", details->minrate);
  3221. } else if (!strcasecmp(data, "pages")) {
  3222. snprintf(buf, len, "%d", details->pages_transferred);
  3223. } else if (!strcasecmp(data, "rate")) {
  3224. ast_copy_string(buf, details->transfer_rate, len);
  3225. } else if (!strcasecmp(data, "remotestationid")) {
  3226. ast_copy_string(buf, details->remotestationid, len);
  3227. } else if (!strcasecmp(data, "resolution")) {
  3228. ast_copy_string(buf, details->resolution, len);
  3229. } else if (!strcasecmp(data, "sessionid")) {
  3230. snprintf(buf, len, "%d", details->id);
  3231. } else if (!strcasecmp(data, "status")) {
  3232. ast_copy_string(buf, details->result, len);
  3233. } else if (!strcasecmp(data, "statusstr")) {
  3234. ast_copy_string(buf, details->resultstr, len);
  3235. } else if ((!strcasecmp(data, "modem")) || (!strcasecmp(data, "modems"))) {
  3236. ast_fax_modem_to_str(details->modems, buf, len);
  3237. } else {
  3238. ast_log(LOG_WARNING, "channel '%s' can't read FAXOPT(%s) because it is unhandled!\n", chan->name, data);
  3239. res = -1;
  3240. }
  3241. ao2_ref(details, -1);
  3242. return res;
  3243. }
  3244. /*! \brief FAXOPT write function modifies the contents of a FAX option */
  3245. static int acf_faxopt_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
  3246. {
  3247. int res = 0;
  3248. struct ast_fax_session_details *details;
  3249. if (!(details = find_or_create_details(chan))) {
  3250. ast_log(LOG_WARNING, "channel '%s' can't set FAXOPT(%s) to '%s' because it failed to create a datastore.\n", chan->name, data, value);
  3251. return -1;
  3252. }
  3253. ast_debug(3, "channel '%s' setting FAXOPT(%s) to '%s'\n", chan->name, data, value);
  3254. if (!strcasecmp(data, "ecm")) {
  3255. const char *val = ast_skip_blanks(value);
  3256. if (ast_true(val)) {
  3257. details->option.ecm = AST_FAX_OPTFLAG_TRUE;
  3258. } else if (ast_false(val)) {
  3259. details->option.ecm = AST_FAX_OPTFLAG_FALSE;
  3260. } else {
  3261. ast_log(LOG_WARNING, "Unsupported value '%s' passed to FAXOPT(ecm).\n", value);
  3262. }
  3263. } else if (!strcasecmp(data, "t38gateway") || !strcasecmp(data, "gateway") ||
  3264. !strcasecmp(data, "t38_gateway") || !strcasecmp(data, "faxgateway")) {
  3265. const char *val = ast_skip_blanks(value);
  3266. char *timeout = strchr(val, ',');
  3267. if (timeout) {
  3268. *timeout++ = '\0';
  3269. }
  3270. if (ast_true(val)) {
  3271. if (details->gateway_id < 0) {
  3272. details->gateway_timeout = 0;
  3273. if (timeout) {
  3274. unsigned int gwtimeout;
  3275. if (sscanf(timeout, "%u", &gwtimeout) == 1) {
  3276. details->gateway_timeout = gwtimeout * 1000;
  3277. } else {
  3278. ast_log(LOG_WARNING, "Unsupported timeout '%s' passed to FAXOPT(%s).\n", timeout, data);
  3279. }
  3280. }
  3281. details->gateway_id = fax_gateway_attach(chan, details);
  3282. if (details->gateway_id < 0) {
  3283. ast_log(LOG_ERROR, "Error attaching T.38 gateway to channel %s.\n", chan->name);
  3284. res = -1;
  3285. } else {
  3286. ast_debug(1, "Attached T.38 gateway to channel %s.\n", chan->name);
  3287. }
  3288. } else {
  3289. ast_log(LOG_WARNING, "Attempt to attach a T.38 gateway on channel (%s) with gateway already running.\n", chan->name);
  3290. }
  3291. } else if (ast_false(val)) {
  3292. ast_framehook_detach(chan, details->gateway_id);
  3293. details->gateway_id = -1;
  3294. } else {
  3295. ast_log(LOG_WARNING, "Unsupported value '%s' passed to FAXOPT(%s).\n", value, data);
  3296. }
  3297. } else if (!strcasecmp(data, "headerinfo")) {
  3298. ast_string_field_set(details, headerinfo, value);
  3299. } else if (!strcasecmp(data, "localstationid")) {
  3300. ast_string_field_set(details, localstationid, value);
  3301. } else if (!strcasecmp(data, "maxrate")) {
  3302. details->maxrate = fax_rate_str_to_int(value);
  3303. if (!details->maxrate) {
  3304. details->maxrate = ast_fax_maxrate();
  3305. }
  3306. } else if (!strcasecmp(data, "minrate")) {
  3307. details->minrate = fax_rate_str_to_int(value);
  3308. if (!details->minrate) {
  3309. details->minrate = ast_fax_minrate();
  3310. }
  3311. } else if ((!strcasecmp(data, "modem")) || (!strcasecmp(data, "modems"))) {
  3312. update_modem_bits(&details->modems, value);
  3313. } else {
  3314. ast_log(LOG_WARNING, "channel '%s' set FAXOPT(%s) to '%s' is unhandled!\n", chan->name, data, value);
  3315. res = -1;
  3316. }
  3317. ao2_ref(details, -1);
  3318. return res;
  3319. }
  3320. /*! \brief FAXOPT dialplan function */
  3321. struct ast_custom_function acf_faxopt = {
  3322. .name = "FAXOPT",
  3323. .read = acf_faxopt_read,
  3324. .write = acf_faxopt_write,
  3325. };
  3326. /*! \brief unload res_fax */
  3327. static int unload_module(void)
  3328. {
  3329. ast_cli_unregister_multiple(fax_cli, ARRAY_LEN(fax_cli));
  3330. if (ast_custom_function_unregister(&acf_faxopt) < 0) {
  3331. ast_log(LOG_WARNING, "failed to unregister function '%s'\n", acf_faxopt.name);
  3332. }
  3333. if (ast_unregister_application(app_sendfax) < 0) {
  3334. ast_log(LOG_WARNING, "failed to unregister '%s'\n", app_sendfax);
  3335. }
  3336. if (ast_unregister_application(app_receivefax) < 0) {
  3337. ast_log(LOG_WARNING, "failed to unregister '%s'\n", app_receivefax);
  3338. }
  3339. if (fax_logger_level != -1) {
  3340. ast_logger_unregister_level("FAX");
  3341. }
  3342. ao2_ref(faxregistry.container, -1);
  3343. return 0;
  3344. }
  3345. /*! \brief load res_fax */
  3346. static int load_module(void)
  3347. {
  3348. int res;
  3349. /* initialize the registry */
  3350. faxregistry.active_sessions = 0;
  3351. faxregistry.reserved_sessions = 0;
  3352. if (!(faxregistry.container = ao2_container_alloc(FAX_MAXBUCKETS, session_hash_cb, session_cmp_cb))) {
  3353. return AST_MODULE_LOAD_DECLINE;
  3354. }
  3355. if (set_config(0) < 0) {
  3356. ast_log(LOG_ERROR, "failed to load configuration file '%s'\n", config);
  3357. ao2_ref(faxregistry.container, -1);
  3358. return AST_MODULE_LOAD_DECLINE;
  3359. }
  3360. /* register CLI operations and applications */
  3361. if (ast_register_application_xml(app_sendfax, sendfax_exec) < 0) {
  3362. ast_log(LOG_WARNING, "failed to register '%s'.\n", app_sendfax);
  3363. ao2_ref(faxregistry.container, -1);
  3364. return AST_MODULE_LOAD_DECLINE;
  3365. }
  3366. if (ast_register_application_xml(app_receivefax, receivefax_exec) < 0) {
  3367. ast_log(LOG_WARNING, "failed to register '%s'.\n", app_receivefax);
  3368. ast_unregister_application(app_sendfax);
  3369. ao2_ref(faxregistry.container, -1);
  3370. return AST_MODULE_LOAD_DECLINE;
  3371. }
  3372. ast_cli_register_multiple(fax_cli, ARRAY_LEN(fax_cli));
  3373. res = ast_custom_function_register(&acf_faxopt);
  3374. fax_logger_level = ast_logger_register_level("FAX");
  3375. return res;
  3376. }
  3377. static int reload_module(void)
  3378. {
  3379. set_config(1);
  3380. return 0;
  3381. }
  3382. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Generic FAX Applications",
  3383. .load = load_module,
  3384. .unload = unload_module,
  3385. .reload = reload_module,
  3386. .load_pri = AST_MODPRI_APP_DEPEND,
  3387. );