ns_turn_server.c 138 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874
  1. /*
  2. * Copyright (C) 2011, 2012, 2013 Citrix Systems
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the project nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #include "ns_turn_server.h"
  31. #include "ns_turn_utils.h"
  32. #include "ns_turn_allocation.h"
  33. #include "ns_turn_msg_addr.h"
  34. #include "ns_turn_ioalib.h"
  35. ///////////////////////////////////////////
  36. #define FUNCSTART if(server && eve(server->verbose)) TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO,"%s:%d:start\n",__FUNCTION__,__LINE__)
  37. #define FUNCEND if(server && eve(server->verbose)) TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO,"%s:%d:end\n",__FUNCTION__,__LINE__)
  38. ////////////////////////////////////////////////
  39. static inline int get_family(int stun_family) {
  40. switch(stun_family) {
  41. case STUN_ATTRIBUTE_REQUESTED_ADDRESS_FAMILY_VALUE_IPV4:
  42. return AF_INET;
  43. case STUN_ATTRIBUTE_REQUESTED_ADDRESS_FAMILY_VALUE_IPV6:
  44. return AF_INET6;
  45. case STUN_ATTRIBUTE_REQUESTED_ADDRESS_FAMILY_VALUE_DEFAULT:
  46. return AF_INET;
  47. default:
  48. return AF_INET;
  49. };
  50. }
  51. ////////////////////////////////////////////////
  52. #define MAX_NUMBER_OF_UNKNOWN_ATTRS (128)
  53. int TURN_MAX_ALLOCATE_TIMEOUT = 60;
  54. int TURN_MAX_ALLOCATE_TIMEOUT_STUN_ONLY = 3;
  55. static inline void log_method(ts_ur_super_session* ss, const char *method, int err_code, const u08bits *reason)
  56. {
  57. if(ss) {
  58. if(!method) method = "unknown";
  59. if(!err_code) {
  60. if(ss->origin[0]) {
  61. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO,
  62. "session %018llu: origin <%s> realm <%s> user <%s>: incoming packet %s processed, success\n",
  63. (unsigned long long)(ss->id), (const char*)(ss->origin),(const char*)(ss->realm_options.name),(const char*)(ss->username),method);
  64. } else {
  65. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO,
  66. "session %018llu: realm <%s> user <%s>: incoming packet %s processed, success\n",
  67. (unsigned long long)(ss->id), (const char*)(ss->realm_options.name),(const char*)(ss->username),method);
  68. }
  69. } else {
  70. if(!reason) reason=(const u08bits*)"Unknown error";
  71. if(ss->origin[0]) {
  72. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO,
  73. "session %018llu: origin <%s> realm <%s> user <%s>: incoming packet %s processed, error %d: %s\n",
  74. (unsigned long long)(ss->id), (const char*)(ss->origin),(const char*)(ss->realm_options.name),(const char*)(ss->username), method, err_code, reason);
  75. } else {
  76. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO,
  77. "session %018llu: realm <%s> user <%s>: incoming packet %s processed, error %d: %s\n",
  78. (unsigned long long)(ss->id), (const char*)(ss->realm_options.name),(const char*)(ss->username), method, err_code, reason);
  79. }
  80. }
  81. }
  82. }
  83. ///////////////////////////////////////////
  84. static int attach_socket_to_session(turn_turnserver* server, ioa_socket_handle s, ts_ur_super_session* ss);
  85. static int check_stun_auth(turn_turnserver *server,
  86. ts_ur_super_session *ss, stun_tid *tid, int *resp_constructed,
  87. int *err_code, const u08bits **reason,
  88. ioa_net_data *in_buffer, ioa_network_buffer_handle nbh,
  89. u16bits method, int *message_integrity,
  90. int *postpone_reply,
  91. int can_resume);
  92. static int create_relay_connection(turn_turnserver* server,
  93. ts_ur_super_session *ss, u32bits lifetime,
  94. int address_family, u08bits transport,
  95. int even_port, u64bits in_reservation_token, u64bits *out_reservation_token,
  96. int *err_code, const u08bits **reason, accept_cb acb);
  97. static int refresh_relay_connection(turn_turnserver* server,
  98. ts_ur_super_session *ss, u32bits lifetime, int even_port,
  99. u64bits in_reservation_token, u64bits *out_reservation_token,
  100. int *err_code, int family);
  101. static int write_client_connection(turn_turnserver *server, ts_ur_super_session* ss, ioa_network_buffer_handle nbh, int ttl, int tos);
  102. static void tcp_peer_accept_connection(ioa_socket_handle s, void *arg);
  103. static int read_client_connection(turn_turnserver *server,
  104. ts_ur_super_session *ss, ioa_net_data *in_buffer,
  105. int can_resume, int count_usage);
  106. static int need_stun_authentication(turn_turnserver *server, ts_ur_super_session *ss);
  107. /////////////////// timer //////////////////////////
  108. static void timer_timeout_handler(ioa_engine_handle e, void *arg)
  109. {
  110. UNUSED_ARG(e);
  111. if(arg) {
  112. turn_turnserver *server=(turn_turnserver*)arg;
  113. server->ctime = turn_time();
  114. }
  115. }
  116. turn_time_t get_turn_server_time(turn_turnserver *server)
  117. {
  118. if(server) {
  119. return server->ctime;
  120. }
  121. return turn_time();
  122. }
  123. /////////////////// quota //////////////////////
  124. static int inc_quota(ts_ur_super_session* ss, u08bits *username)
  125. {
  126. if(ss && !(ss->quota_used) && ss->server && ((turn_turnserver*)ss->server)->chquotacb && username) {
  127. if(((turn_turnserver*)ss->server)->ct == TURN_CREDENTIALS_LONG_TERM) {
  128. if(!(ss->origin_set)) {
  129. return -1;
  130. }
  131. }
  132. if((((turn_turnserver*)ss->server)->chquotacb)(username, ss->oauth, (u08bits*)ss->realm_options.name)<0) {
  133. return -1;
  134. } else {
  135. STRCPY(ss->username,username);
  136. ss->quota_used = 1;
  137. }
  138. }
  139. return 0;
  140. }
  141. static void dec_quota(ts_ur_super_session* ss)
  142. {
  143. if(ss && ss->quota_used && ss->server && ((turn_turnserver*)ss->server)->raqcb) {
  144. ss->quota_used = 0;
  145. (((turn_turnserver*)ss->server)->raqcb)(ss->username, ss->oauth, (u08bits*)ss->realm_options.name);
  146. }
  147. }
  148. static void dec_bps(ts_ur_super_session* ss)
  149. {
  150. if(ss && ss->server) {
  151. if(ss->bps) {
  152. if(((turn_turnserver*)ss->server)->allocate_bps_func) {
  153. ((turn_turnserver*)ss->server)->allocate_bps_func(ss->bps,0);
  154. }
  155. ss->bps = 0;
  156. }
  157. }
  158. }
  159. /////////////////// server lists ///////////////////
  160. void init_turn_server_addrs_list(turn_server_addrs_list_t *l)
  161. {
  162. if(l) {
  163. l->addrs = NULL;
  164. l->size = 0;
  165. turn_mutex_init(&(l->m));
  166. }
  167. }
  168. /////////////////// RFC 5780 ///////////////////////
  169. void set_rfc5780(turn_turnserver *server, get_alt_addr_cb cb, send_message_cb smcb)
  170. {
  171. if(server) {
  172. if(!cb || !smcb) {
  173. server->rfc5780 = 0;
  174. server->alt_addr_cb = NULL;
  175. server->sm_cb = NULL;
  176. } else {
  177. server->rfc5780 = 1;
  178. server->alt_addr_cb = cb;
  179. server->sm_cb = smcb;
  180. }
  181. }
  182. }
  183. static int is_rfc5780(turn_turnserver *server)
  184. {
  185. if(!server)
  186. return 0;
  187. return ((server->rfc5780) && (server->alt_addr_cb));
  188. }
  189. static int get_other_address(turn_turnserver *server, ts_ur_super_session *ss, ioa_addr *alt_addr)
  190. {
  191. if(is_rfc5780(server) && ss && ss->client_socket) {
  192. int ret = server->alt_addr_cb(get_local_addr_from_ioa_socket(ss->client_socket), alt_addr);
  193. return ret;
  194. }
  195. return -1;
  196. }
  197. static int send_turn_message_to(turn_turnserver *server, ioa_network_buffer_handle nbh, ioa_addr *response_origin, ioa_addr *response_destination)
  198. {
  199. if(is_rfc5780(server) && nbh && response_origin && response_destination) {
  200. return server->sm_cb(server->e, nbh, response_origin, response_destination);
  201. }
  202. return -1;
  203. }
  204. /////////////////// Peer addr check /////////////////////////////
  205. static int good_peer_addr(turn_turnserver *server, const char* realm, ioa_addr *peer_addr)
  206. {
  207. #define CHECK_REALM(r) if((r)[0] && realm && realm[0] && strcmp((r),realm)) continue
  208. if(server && peer_addr) {
  209. if(*(server->no_multicast_peers) && ioa_addr_is_multicast(peer_addr))
  210. return 0;
  211. if(*(server->no_loopback_peers) && ioa_addr_is_loopback(peer_addr))
  212. return 0;
  213. {
  214. int i;
  215. if(server->ip_whitelist) {
  216. // White listing of addr ranges
  217. for (i = server->ip_whitelist->ranges_number - 1; i >= 0; --i) {
  218. CHECK_REALM(server->ip_whitelist->rs[i].realm);
  219. if (ioa_addr_in_range(&(server->ip_whitelist->rs[i].enc), peer_addr))
  220. return 1;
  221. }
  222. }
  223. {
  224. ioa_lock_whitelist(server->e);
  225. const ip_range_list_t* wl = ioa_get_whitelist(server->e);
  226. if(wl) {
  227. // White listing of addr ranges
  228. for (i = wl->ranges_number - 1; i >= 0; --i) {
  229. CHECK_REALM(wl->rs[i].realm);
  230. if (ioa_addr_in_range(&(wl->rs[i].enc), peer_addr)) {
  231. ioa_unlock_whitelist(server->e);
  232. return 1;
  233. }
  234. }
  235. }
  236. ioa_unlock_whitelist(server->e);
  237. }
  238. if(server->ip_blacklist) {
  239. // Black listing of addr ranges
  240. for (i = server->ip_blacklist->ranges_number - 1; i >= 0; --i) {
  241. CHECK_REALM(server->ip_blacklist->rs[i].realm);
  242. if (ioa_addr_in_range(&(server->ip_blacklist->rs[i].enc), peer_addr)) {
  243. char saddr[129];
  244. addr_to_string_no_port(peer_addr,(u08bits*)saddr);
  245. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "A peer IP %s denied in the range: %s\n",saddr,server->ip_blacklist->rs[i].str);
  246. return 0;
  247. }
  248. }
  249. }
  250. {
  251. ioa_lock_blacklist(server->e);
  252. const ip_range_list_t* bl = ioa_get_blacklist(server->e);
  253. if(bl) {
  254. // Black listing of addr ranges
  255. for (i = bl->ranges_number - 1; i >= 0; --i) {
  256. CHECK_REALM(bl->rs[i].realm);
  257. if (ioa_addr_in_range(&(bl->rs[i].enc), peer_addr)) {
  258. ioa_unlock_blacklist(server->e);
  259. char saddr[129];
  260. addr_to_string_no_port(peer_addr,(u08bits*)saddr);
  261. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "A peer IP %s denied in the range: %s\n",saddr,bl->rs[i].str);
  262. return 0;
  263. }
  264. }
  265. }
  266. ioa_unlock_blacklist(server->e);
  267. }
  268. }
  269. }
  270. #undef CHECK_REALM
  271. return 1;
  272. }
  273. /////////////////// Allocation //////////////////////////////////
  274. allocation* get_allocation_ss(ts_ur_super_session *ss) {
  275. return &(ss->alloc);
  276. }
  277. static inline relay_endpoint_session *get_relay_session_ss(ts_ur_super_session *ss, int family)
  278. {
  279. return get_relay_session(&(ss->alloc),family);
  280. }
  281. static inline ioa_socket_handle get_relay_socket_ss(ts_ur_super_session *ss, int family)
  282. {
  283. return get_relay_socket(&(ss->alloc),family);
  284. }
  285. /////////// Session info ///////
  286. void turn_session_info_init(struct turn_session_info* tsi) {
  287. if(tsi) {
  288. ns_bzero(tsi,sizeof(struct turn_session_info));
  289. }
  290. }
  291. void turn_session_info_clean(struct turn_session_info* tsi) {
  292. if(tsi) {
  293. if(tsi->extra_peers_data) {
  294. turn_free(tsi->extra_peers_data, sizeof(addr_data)*(tsi->extra_peers_size));
  295. }
  296. turn_session_info_init(tsi);
  297. }
  298. }
  299. void turn_session_info_add_peer(struct turn_session_info* tsi, ioa_addr *peer)
  300. {
  301. if(tsi && peer) {
  302. {
  303. size_t i;
  304. for(i=0;i<tsi->main_peers_size;++i) {
  305. if(addr_eq(peer, &(tsi->main_peers_data[i].addr))) {
  306. return;
  307. }
  308. }
  309. if(tsi->main_peers_size < TURN_MAIN_PEERS_ARRAY_SIZE) {
  310. addr_cpy(&(tsi->main_peers_data[tsi->main_peers_size].addr),peer);
  311. addr_to_string(&(tsi->main_peers_data[tsi->main_peers_size].addr),
  312. (u08bits*)tsi->main_peers_data[tsi->main_peers_size].saddr);
  313. tsi->main_peers_size += 1;
  314. return;
  315. }
  316. }
  317. if(tsi->extra_peers_data) {
  318. size_t sz;
  319. for(sz=0;sz<tsi->extra_peers_size;++sz) {
  320. if(addr_eq(peer, &(tsi->extra_peers_data[sz].addr))) {
  321. return;
  322. }
  323. }
  324. }
  325. tsi->extra_peers_data = (addr_data*)turn_realloc(tsi->extra_peers_data,tsi->extra_peers_size*sizeof(addr_data),(tsi->extra_peers_size+1)*sizeof(addr_data));
  326. addr_cpy(&(tsi->extra_peers_data[tsi->extra_peers_size].addr),peer);
  327. addr_to_string(&(tsi->extra_peers_data[tsi->extra_peers_size].addr),
  328. (u08bits*)tsi->extra_peers_data[tsi->extra_peers_size].saddr);
  329. tsi->extra_peers_size += 1;
  330. }
  331. }
  332. struct tsi_arg {
  333. struct turn_session_info* tsi;
  334. ioa_addr *addr;
  335. };
  336. static int turn_session_info_foreachcb(ur_map_key_type key, ur_map_value_type value, void *arg)
  337. {
  338. UNUSED_ARG(value);
  339. int port = (int)key;
  340. struct tsi_arg *ta = (struct tsi_arg *)arg;
  341. if(port && ta && ta->tsi && ta->addr) {
  342. ioa_addr a;
  343. addr_cpy(&a,ta->addr);
  344. addr_set_port(&a,port);
  345. turn_session_info_add_peer(ta->tsi,&a);
  346. }
  347. return 0;
  348. }
  349. int turn_session_info_copy_from(struct turn_session_info* tsi, ts_ur_super_session *ss)
  350. {
  351. int ret = -1;
  352. if(tsi && ss) {
  353. tsi->id = ss->id;
  354. tsi->bps = ss->bps;
  355. tsi->start_time = ss->start_time;
  356. tsi->valid = is_allocation_valid(&(ss->alloc)) && !(ss->to_be_closed) && (ss->quota_used);
  357. if(tsi->valid) {
  358. if(ss->alloc.relay_sessions[ALLOC_IPV4_INDEX].s) {
  359. tsi->expiration_time = ss->alloc.relay_sessions[ALLOC_IPV4_INDEX].expiration_time;
  360. if(ss->alloc.relay_sessions[ALLOC_IPV6_INDEX].s) {
  361. if(turn_time_before(tsi->expiration_time,ss->alloc.relay_sessions[ALLOC_IPV6_INDEX].expiration_time)) {
  362. tsi->expiration_time = ss->alloc.relay_sessions[ALLOC_IPV6_INDEX].expiration_time;
  363. }
  364. }
  365. } else if(ss->alloc.relay_sessions[ALLOC_IPV6_INDEX].s) {
  366. tsi->expiration_time = ss->alloc.relay_sessions[ALLOC_IPV6_INDEX].expiration_time;
  367. }
  368. if(ss->client_socket) {
  369. tsi->client_protocol = get_ioa_socket_type(ss->client_socket);
  370. addr_cpy(&(tsi->local_addr_data.addr),get_local_addr_from_ioa_socket(ss->client_socket));
  371. addr_to_string(&(tsi->local_addr_data.addr),(u08bits*)tsi->local_addr_data.saddr);
  372. addr_cpy(&(tsi->remote_addr_data.addr),get_remote_addr_from_ioa_socket(ss->client_socket));
  373. addr_to_string(&(tsi->remote_addr_data.addr),(u08bits*)tsi->remote_addr_data.saddr);
  374. }
  375. {
  376. if(ss->alloc.relay_sessions[ALLOC_IPV4_INDEX].s) {
  377. tsi->peer_protocol = get_ioa_socket_type(ss->alloc.relay_sessions[ALLOC_IPV4_INDEX].s);
  378. if(ss->alloc.is_valid) {
  379. addr_cpy(&(tsi->relay_addr_data_ipv4.addr),get_local_addr_from_ioa_socket(ss->alloc.relay_sessions[ALLOC_IPV4_INDEX].s));
  380. addr_to_string(&(tsi->relay_addr_data_ipv4.addr),(u08bits*)tsi->relay_addr_data_ipv4.saddr);
  381. }
  382. }
  383. if(ss->alloc.relay_sessions[ALLOC_IPV6_INDEX].s) {
  384. tsi->peer_protocol = get_ioa_socket_type(ss->alloc.relay_sessions[ALLOC_IPV6_INDEX].s);
  385. if(ss->alloc.is_valid) {
  386. addr_cpy(&(tsi->relay_addr_data_ipv6.addr),get_local_addr_from_ioa_socket(ss->alloc.relay_sessions[ALLOC_IPV6_INDEX].s));
  387. addr_to_string(&(tsi->relay_addr_data_ipv6.addr),(u08bits*)tsi->relay_addr_data_ipv6.saddr);
  388. }
  389. }
  390. }
  391. STRCPY(tsi->username,ss->username);
  392. tsi->enforce_fingerprints = ss->enforce_fingerprints;
  393. STRCPY(tsi->tls_method, get_ioa_socket_tls_method(ss->client_socket));
  394. STRCPY(tsi->tls_cipher, get_ioa_socket_tls_cipher(ss->client_socket));
  395. STRCPY(tsi->realm, ss->realm_options.name);
  396. STRCPY(tsi->origin, ss->origin);
  397. if(ss->t_received_packets > ss->received_packets)
  398. tsi->received_packets = ss->t_received_packets;
  399. else
  400. tsi->received_packets = ss->received_packets;
  401. if(ss->t_sent_packets > ss->sent_packets)
  402. tsi->sent_packets = ss->t_sent_packets;
  403. else
  404. tsi->sent_packets = ss->sent_packets;
  405. if(ss->t_received_bytes > ss->received_bytes)
  406. tsi->received_bytes = ss->t_received_bytes;
  407. else
  408. tsi->received_bytes = ss->received_bytes;
  409. if(ss->t_sent_bytes > ss->sent_bytes)
  410. tsi->sent_bytes = ss->t_sent_bytes;
  411. else
  412. tsi->sent_bytes = ss->sent_bytes;
  413. {
  414. tsi->received_rate = ss->received_rate;
  415. tsi->sent_rate = ss->sent_rate;
  416. tsi->total_rate = tsi->received_rate + tsi->sent_rate;
  417. }
  418. tsi->is_mobile = ss->is_mobile;
  419. {
  420. size_t i;
  421. for(i=0;i<TURN_PERMISSION_HASHTABLE_SIZE;++i) {
  422. turn_permission_array *parray = &(ss->alloc.addr_to_perm.table[i]);
  423. {
  424. size_t j;
  425. for(j=0;j<TURN_PERMISSION_ARRAY_SIZE;++j) {
  426. turn_permission_slot* slot = &(parray->main_slots[j]);
  427. if(slot->info.allocated) {
  428. turn_session_info_add_peer(tsi,&(slot->info.addr));
  429. struct tsi_arg arg = {
  430. tsi,
  431. &(slot->info.addr)
  432. };
  433. lm_map_foreach_arg(&(slot->info.chns), turn_session_info_foreachcb, &arg);
  434. }
  435. }
  436. }
  437. {
  438. turn_permission_slot **slots = parray->extra_slots;
  439. if(slots) {
  440. size_t sz = parray->extra_sz;
  441. size_t j;
  442. for(j=0;j<sz;++j) {
  443. turn_permission_slot* slot = slots[j];
  444. if(slot && slot->info.allocated) {
  445. turn_session_info_add_peer(tsi,&(slot->info.addr));
  446. struct tsi_arg arg = {
  447. tsi,
  448. &(slot->info.addr)
  449. };
  450. lm_map_foreach_arg(&(slot->info.chns), turn_session_info_foreachcb, &arg);
  451. }
  452. }
  453. }
  454. }
  455. }
  456. }
  457. {
  458. tcp_connection_list *tcl = &(ss->alloc.tcs);
  459. if(tcl->elems) {
  460. size_t i;
  461. size_t sz = tcl->sz;
  462. for(i=0;i<sz;++i) {
  463. if(tcl->elems[i]) {
  464. tcp_connection *tc = tcl->elems[i];
  465. if(tc) {
  466. turn_session_info_add_peer(tsi,&(tc->peer_addr));
  467. }
  468. }
  469. }
  470. }
  471. }
  472. }
  473. ret = 0;
  474. }
  475. return ret;
  476. }
  477. int report_turn_session_info(turn_turnserver *server, ts_ur_super_session *ss, int force_invalid)
  478. {
  479. if(server && ss && server->send_turn_session_info) {
  480. struct turn_session_info tsi;
  481. turn_session_info_init(&tsi);
  482. if(turn_session_info_copy_from(&tsi,ss)<0) {
  483. turn_session_info_clean(&tsi);
  484. } else {
  485. if(force_invalid)
  486. tsi.valid = 0;
  487. if(server->send_turn_session_info(&tsi)<0) {
  488. turn_session_info_clean(&tsi);
  489. } else {
  490. return 0;
  491. }
  492. }
  493. }
  494. return -1;
  495. }
  496. /////////// SS /////////////////
  497. static int mobile_id_to_string(mobile_id_t mid, char *dst, size_t dst_sz)
  498. {
  499. size_t output_length = 0;
  500. if(!dst)
  501. return -1;
  502. char *s = base64_encode((const unsigned char *)&mid,
  503. sizeof(mid),
  504. &output_length);
  505. if(!s)
  506. return -1;
  507. if(!output_length || (output_length+1 > dst_sz)) {
  508. turn_free(s, output_length);
  509. return -1;
  510. }
  511. ns_bcopy(s, dst, output_length);
  512. turn_free(s, output_length);
  513. dst[output_length] = 0;
  514. return (int)output_length;
  515. }
  516. static mobile_id_t string_to_mobile_id(char* src)
  517. {
  518. mobile_id_t mid = 0;
  519. if(src) {
  520. size_t output_length = 0;
  521. unsigned char *out = base64_decode(src, strlen(src), &output_length);
  522. if(out) {
  523. if(output_length == sizeof(mid)) {
  524. mid = *((mobile_id_t*)out);
  525. }
  526. turn_free(out, output_length);
  527. }
  528. }
  529. return mid;
  530. }
  531. static mobile_id_t get_new_mobile_id(turn_turnserver* server)
  532. {
  533. mobile_id_t newid = 0;
  534. if(server && server->mobile_connections_map) {
  535. ur_map *map = server->mobile_connections_map;
  536. u64bits sid = server->id;
  537. sid = sid<<56;
  538. do {
  539. while (!newid) {
  540. if(TURN_RANDOM_SIZE == sizeof(mobile_id_t))
  541. newid = (mobile_id_t)turn_random();
  542. else {
  543. newid = (mobile_id_t)turn_random();
  544. newid = (newid<<32) + (mobile_id_t)turn_random();
  545. }
  546. if(!newid) {
  547. continue;
  548. }
  549. newid = newid & 0x00FFFFFFFFFFFFFFLL;
  550. if(!newid) {
  551. continue;
  552. }
  553. newid = newid | sid;
  554. }
  555. } while(ur_map_get(map, (ur_map_key_type)newid, NULL));
  556. }
  557. return newid;
  558. }
  559. static void put_session_into_mobile_map(ts_ur_super_session *ss)
  560. {
  561. if(ss && ss->server) {
  562. turn_turnserver* server = (turn_turnserver*)(ss->server);
  563. if(*(server->mobility) && server->mobile_connections_map) {
  564. if(!(ss->mobile_id)) {
  565. ss->mobile_id = get_new_mobile_id(server);
  566. mobile_id_to_string(ss->mobile_id, ss->s_mobile_id, sizeof(ss->s_mobile_id));
  567. }
  568. ur_map_put(server->mobile_connections_map, (ur_map_key_type)(ss->mobile_id), (ur_map_value_type)ss);
  569. }
  570. }
  571. }
  572. static void put_session_into_map(ts_ur_super_session *ss)
  573. {
  574. if(ss && ss->server) {
  575. turn_turnserver* server = (turn_turnserver*)(ss->server);
  576. if(!(ss->id)) {
  577. ss->id = (turnsession_id)((turnsession_id)server->id * TURN_SESSION_ID_FACTOR);
  578. ss->id += ++(server->session_id_counter);
  579. ss->start_time = server->ctime;
  580. }
  581. ur_map_put(server->sessions_map, (ur_map_key_type)(ss->id), (ur_map_value_type)ss);
  582. put_session_into_mobile_map(ss);
  583. }
  584. }
  585. static void delete_session_from_mobile_map(ts_ur_super_session *ss)
  586. {
  587. if(ss && ss->server && ss->mobile_id) {
  588. turn_turnserver* server = (turn_turnserver*)(ss->server);
  589. if(server->mobile_connections_map) {
  590. ur_map_del(server->mobile_connections_map, (ur_map_key_type)(ss->mobile_id), NULL);
  591. }
  592. ss->mobile_id = 0;
  593. ss->s_mobile_id[0] = 0;
  594. }
  595. }
  596. static void delete_session_from_map(ts_ur_super_session *ss)
  597. {
  598. if(ss && ss->server) {
  599. turn_turnserver* server = (turn_turnserver*)(ss->server);
  600. ur_map_del(server->sessions_map, (ur_map_key_type)(ss->id), NULL);
  601. delete_session_from_mobile_map(ss);
  602. }
  603. }
  604. static ts_ur_super_session* get_session_from_map(turn_turnserver* server, turnsession_id sid)
  605. {
  606. ts_ur_super_session *ss = NULL;
  607. if(server) {
  608. ur_map_value_type value = 0;
  609. if(ur_map_get(server->sessions_map, (ur_map_key_type)sid, &value) && value) {
  610. ss = (ts_ur_super_session*)value;
  611. }
  612. }
  613. return ss;
  614. }
  615. void turn_cancel_session(turn_turnserver *server, turnsession_id sid)
  616. {
  617. if(server) {
  618. ts_ur_super_session* ts = get_session_from_map(server, sid);
  619. if(ts) {
  620. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Session %018llu to be forcefully canceled\n",(unsigned long long)sid);
  621. shutdown_client_connection(server, ts, 0, "Forceful shutdown");
  622. }
  623. }
  624. }
  625. static ts_ur_super_session* get_session_from_mobile_map(turn_turnserver* server, mobile_id_t mid)
  626. {
  627. ts_ur_super_session *ss = NULL;
  628. if(server && *(server->mobility) && server->mobile_connections_map && mid) {
  629. ur_map_value_type value = 0;
  630. if(ur_map_get(server->mobile_connections_map, (ur_map_key_type)mid, &value) && value) {
  631. ss = (ts_ur_super_session*)value;
  632. }
  633. }
  634. return ss;
  635. }
  636. static ts_ur_super_session* create_new_ss(turn_turnserver* server) {
  637. //
  638. //printf("%s: 111.111: session size=%lu\n",__FUNCTION__,(unsigned long)sizeof(ts_ur_super_session));
  639. //
  640. ts_ur_super_session *ss = (ts_ur_super_session*)turn_malloc(sizeof(ts_ur_super_session));
  641. ns_bzero(ss,sizeof(ts_ur_super_session));
  642. ss->server = server;
  643. get_default_realm_options(&(ss->realm_options));
  644. put_session_into_map(ss);
  645. init_allocation(ss,&(ss->alloc), server->tcp_relay_connections);
  646. return ss;
  647. }
  648. static void delete_ur_map_ss(void *p) {
  649. if (p) {
  650. ts_ur_super_session* ss = (ts_ur_super_session*) p;
  651. delete_session_from_map(ss);
  652. IOA_CLOSE_SOCKET(ss->client_socket);
  653. clear_allocation(get_allocation_ss(ss));
  654. IOA_EVENT_DEL(ss->to_be_allocated_timeout_ev);
  655. turn_free(p,sizeof(ts_ur_super_session));
  656. }
  657. }
  658. /////////// clean all /////////////////////
  659. static int turn_server_remove_all_from_ur_map_ss(ts_ur_super_session* ss) {
  660. if (!ss)
  661. return 0;
  662. else {
  663. int ret = 0;
  664. if (ss->client_socket) {
  665. clear_ioa_socket_session_if(ss->client_socket, ss);
  666. }
  667. if (get_relay_socket_ss(ss,AF_INET)) {
  668. clear_ioa_socket_session_if(get_relay_socket_ss(ss,AF_INET), ss);
  669. }
  670. if (get_relay_socket_ss(ss,AF_INET6)) {
  671. clear_ioa_socket_session_if(get_relay_socket_ss(ss,AF_INET6), ss);
  672. }
  673. delete_ur_map_ss(ss);
  674. return ret;
  675. }
  676. }
  677. /////////////////////////////////////////////////////////////////
  678. static void client_ss_channel_timeout_handler(ioa_engine_handle e, void* arg) {
  679. UNUSED_ARG(e);
  680. if (!arg)
  681. return;
  682. ch_info* chn = (ch_info*) arg;
  683. turn_channel_delete(chn);
  684. }
  685. static void client_ss_perm_timeout_handler(ioa_engine_handle e, void* arg) {
  686. UNUSED_ARG(e);
  687. if (!arg) {
  688. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "!!! %s: empty permission to be cleaned\n",__FUNCTION__);
  689. return;
  690. }
  691. turn_permission_info* tinfo = (turn_permission_info*) arg;
  692. if(!(tinfo->allocated)) {
  693. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "!!! %s: unallocated permission to be cleaned\n",__FUNCTION__);
  694. return;
  695. }
  696. if(!(tinfo->lifetime_ev)) {
  697. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "!!! %s: strange (1) permission to be cleaned\n",__FUNCTION__);
  698. }
  699. if(!(tinfo->owner)) {
  700. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "!!! %s: strange (2) permission to be cleaned\n",__FUNCTION__);
  701. }
  702. turn_permission_clean(tinfo);
  703. }
  704. ///////////////////////////////////////////////////////////////////
  705. static int update_turn_permission_lifetime(ts_ur_super_session *ss, turn_permission_info *tinfo, turn_time_t time_delta) {
  706. if (ss && tinfo && tinfo->owner) {
  707. turn_turnserver *server = (turn_turnserver *) (ss->server);
  708. if (server) {
  709. if(!time_delta) time_delta = STUN_PERMISSION_LIFETIME;
  710. tinfo->expiration_time = server->ctime + time_delta;
  711. IOA_EVENT_DEL(tinfo->lifetime_ev);
  712. tinfo->lifetime_ev = set_ioa_timer(server->e, time_delta, 0,
  713. client_ss_perm_timeout_handler, tinfo, 0,
  714. "client_ss_channel_timeout_handler");
  715. if(server->verbose) {
  716. tinfo->verbose = 1;
  717. tinfo->session_id = ss->id;
  718. char s[257]="\0";
  719. addr_to_string(&(tinfo->addr),(u08bits*)s);
  720. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "session %018llu: peer %s lifetime updated: %lu\n",(unsigned long long)ss->id,s,(unsigned long)time_delta);
  721. }
  722. return 0;
  723. }
  724. }
  725. return -1;
  726. }
  727. static int update_channel_lifetime(ts_ur_super_session *ss, ch_info* chn)
  728. {
  729. if (chn) {
  730. turn_permission_info* tinfo = (turn_permission_info*) (chn->owner);
  731. if (tinfo && tinfo->owner) {
  732. turn_turnserver *server = (turn_turnserver *) (ss->server);
  733. if (server) {
  734. if (update_turn_permission_lifetime(ss, tinfo, STUN_CHANNEL_LIFETIME) < 0)
  735. return -1;
  736. chn->expiration_time = server->ctime + STUN_CHANNEL_LIFETIME;
  737. IOA_EVENT_DEL(chn->lifetime_ev);
  738. chn->lifetime_ev = set_ioa_timer(server->e, STUN_CHANNEL_LIFETIME, 0,
  739. client_ss_channel_timeout_handler,
  740. chn, 0,
  741. "client_ss_channel_timeout_handler");
  742. return 0;
  743. }
  744. }
  745. }
  746. return -1;
  747. }
  748. /////////////// TURN ///////////////////////////
  749. #define SKIP_ATTRIBUTES case STUN_ATTRIBUTE_OAUTH_ACCESS_TOKEN: case STUN_ATTRIBUTE_PRIORITY: case STUN_ATTRIBUTE_FINGERPRINT: case STUN_ATTRIBUTE_MESSAGE_INTEGRITY: break; \
  750. case STUN_ATTRIBUTE_USERNAME: case STUN_ATTRIBUTE_REALM: case STUN_ATTRIBUTE_NONCE: case STUN_ATTRIBUTE_ORIGIN: \
  751. sar = stun_attr_get_next_str(ioa_network_buffer_data(in_buffer->nbh),\
  752. ioa_network_buffer_get_size(in_buffer->nbh), sar); \
  753. continue
  754. static u08bits get_transport_value(const u08bits *value) {
  755. if((value[0] == STUN_ATTRIBUTE_TRANSPORT_UDP_VALUE)||
  756. (value[0] == STUN_ATTRIBUTE_TRANSPORT_TCP_VALUE)) {
  757. return value[0];
  758. }
  759. return 0;
  760. }
  761. static int handle_turn_allocate(turn_turnserver *server,
  762. ts_ur_super_session *ss, stun_tid *tid, int *resp_constructed,
  763. int *err_code, const u08bits **reason, u16bits *unknown_attrs, u16bits *ua_num,
  764. ioa_net_data *in_buffer, ioa_network_buffer_handle nbh) {
  765. int err_code4 = 0;
  766. int err_code6 = 0;
  767. allocation* alloc = get_allocation_ss(ss);
  768. if (is_allocation_valid(alloc)) {
  769. if (!stun_tid_equals(tid, &(alloc->tid))) {
  770. *err_code = 437;
  771. *reason = (const u08bits *)"Wrong TID";
  772. } else {
  773. size_t len = ioa_network_buffer_get_size(nbh);
  774. ioa_addr xor_relayed_addr1, *pxor_relayed_addr1=NULL;
  775. ioa_addr xor_relayed_addr2, *pxor_relayed_addr2=NULL;
  776. ioa_addr *relayed_addr1 = get_local_addr_from_ioa_socket(get_relay_socket_ss(ss,AF_INET));
  777. ioa_addr *relayed_addr2 = get_local_addr_from_ioa_socket(get_relay_socket_ss(ss,AF_INET6));
  778. if(get_relay_session_failure(alloc,AF_INET)) {
  779. addr_set_any(&xor_relayed_addr1);
  780. pxor_relayed_addr1 = &xor_relayed_addr1;
  781. } else if(relayed_addr1) {
  782. if(server->external_ip_set) {
  783. addr_cpy(&xor_relayed_addr1, &(server->external_ip));
  784. addr_set_port(&xor_relayed_addr1,addr_get_port(relayed_addr1));
  785. } else {
  786. addr_cpy(&xor_relayed_addr1, relayed_addr1);
  787. }
  788. pxor_relayed_addr1 = &xor_relayed_addr1;
  789. }
  790. if(get_relay_session_failure(alloc,AF_INET6)) {
  791. addr_set_any(&xor_relayed_addr2);
  792. pxor_relayed_addr2 = &xor_relayed_addr2;
  793. } else if(relayed_addr2) {
  794. if(server->external_ip_set) {
  795. addr_cpy(&xor_relayed_addr2, &(server->external_ip));
  796. addr_set_port(&xor_relayed_addr2,addr_get_port(relayed_addr2));
  797. } else {
  798. addr_cpy(&xor_relayed_addr2, relayed_addr2);
  799. }
  800. pxor_relayed_addr2 = &xor_relayed_addr2;
  801. }
  802. if(pxor_relayed_addr1 || pxor_relayed_addr2) {
  803. u32bits lifetime = 0;
  804. if(pxor_relayed_addr1) {
  805. lifetime = (get_relay_session(alloc,pxor_relayed_addr1->ss.sa_family)->expiration_time - server->ctime);
  806. } else if(pxor_relayed_addr2) {
  807. lifetime = (get_relay_session(alloc,pxor_relayed_addr2->ss.sa_family)->expiration_time - server->ctime);
  808. }
  809. stun_set_allocate_response_str(ioa_network_buffer_data(nbh), &len,
  810. tid,
  811. pxor_relayed_addr1, pxor_relayed_addr2,
  812. get_remote_addr_from_ioa_socket(ss->client_socket),
  813. lifetime, 0, NULL, 0,
  814. ss->s_mobile_id);
  815. ioa_network_buffer_set_size(nbh,len);
  816. *resp_constructed = 1;
  817. }
  818. }
  819. } else {
  820. u08bits transport = 0;
  821. turn_time_t lifetime = 0;
  822. int even_port = -1;
  823. int dont_fragment = 0;
  824. u64bits in_reservation_token = 0;
  825. int af4 = 0;
  826. int af6 = 0;
  827. u08bits username[STUN_MAX_USERNAME_SIZE+1]="\0";
  828. size_t ulen = 0;
  829. band_limit_t bps = 0;
  830. band_limit_t max_bps = 0;
  831. stun_attr_ref sar = stun_attr_get_first_str(ioa_network_buffer_data(in_buffer->nbh),
  832. ioa_network_buffer_get_size(in_buffer->nbh));
  833. while (sar && (!(*err_code)) && (*ua_num < MAX_NUMBER_OF_UNKNOWN_ATTRS)) {
  834. int attr_type = stun_attr_get_type(sar);
  835. if(attr_type == STUN_ATTRIBUTE_USERNAME) {
  836. const u08bits* value = stun_attr_get_value(sar);
  837. if (value) {
  838. ulen = stun_attr_get_len(sar);
  839. if(ulen>=sizeof(username)) {
  840. *err_code = 400;
  841. *reason = (const u08bits *)"User name is too long";
  842. break;
  843. }
  844. ns_bcopy(value,username,ulen);
  845. username[ulen]=0;
  846. }
  847. }
  848. switch (attr_type) {
  849. SKIP_ATTRIBUTES;
  850. case STUN_ATTRIBUTE_NEW_BANDWIDTH:
  851. bps = stun_attr_get_bandwidth(sar);
  852. break;
  853. case STUN_ATTRIBUTE_MOBILITY_TICKET:
  854. if(!(*(server->mobility))) {
  855. *err_code = 405;
  856. *reason = (const u08bits *)"Mobility Forbidden";
  857. } else if (stun_attr_get_len(sar) != 0) {
  858. *err_code = 400;
  859. *reason = (const u08bits *)"Wrong Mobility Field";
  860. } else {
  861. ss->is_mobile = 1;
  862. }
  863. break;
  864. case STUN_ATTRIBUTE_REQUESTED_TRANSPORT: {
  865. if (stun_attr_get_len(sar) != 4) {
  866. *err_code = 400;
  867. *reason = (const u08bits *)"Wrong Transport Field";
  868. } else if(transport) {
  869. *err_code = 400;
  870. *reason = (const u08bits *)"Duplicate Transport Fields";
  871. } else {
  872. const u08bits* value = stun_attr_get_value(sar);
  873. if (value) {
  874. transport = get_transport_value(value);
  875. if (!transport) {
  876. *err_code = 442;
  877. }
  878. if((transport == STUN_ATTRIBUTE_TRANSPORT_TCP_VALUE) && *(server->no_tcp_relay)) {
  879. *err_code = 442;
  880. *reason = (const u08bits *)"TCP Transport is not allowed by the TURN Server configuration";
  881. } else if((transport == STUN_ATTRIBUTE_TRANSPORT_UDP_VALUE) && *(server->no_udp_relay)) {
  882. *err_code = 442;
  883. *reason = (const u08bits *)"UDP Transport is not allowed by the TURN Server configuration";
  884. } else if(ss->client_socket) {
  885. SOCKET_TYPE cst = get_ioa_socket_type(ss->client_socket);
  886. if((transport == STUN_ATTRIBUTE_TRANSPORT_TCP_VALUE) && !is_stream_socket(cst)) {
  887. *err_code = 400;
  888. *reason = (const u08bits *)"Wrong Transport Data";
  889. } else {
  890. ss->is_tcp_relay = (transport == STUN_ATTRIBUTE_TRANSPORT_TCP_VALUE);
  891. }
  892. }
  893. } else {
  894. *err_code = 400;
  895. *reason = (const u08bits *)"Wrong Transport Data";
  896. }
  897. }
  898. }
  899. break;
  900. case STUN_ATTRIBUTE_DONT_FRAGMENT:
  901. dont_fragment = 1;
  902. if(!(server->dont_fragment))
  903. unknown_attrs[(*ua_num)++] = nswap16(attr_type);
  904. break;
  905. case STUN_ATTRIBUTE_LIFETIME: {
  906. if (stun_attr_get_len(sar) != 4) {
  907. *err_code = 400;
  908. *reason = (const u08bits *)"Wrong Lifetime Field";
  909. } else {
  910. const u08bits* value = stun_attr_get_value(sar);
  911. if (!value) {
  912. *err_code = 400;
  913. *reason = (const u08bits *)"Wrong Lifetime Data";
  914. } else {
  915. lifetime = nswap32(*((const u32bits*)value));
  916. }
  917. }
  918. }
  919. break;
  920. case STUN_ATTRIBUTE_EVEN_PORT: {
  921. if (in_reservation_token) {
  922. *err_code = 400;
  923. *reason = (const u08bits *)"Even Port and Reservation Token cannot be used together";
  924. } else {
  925. even_port = stun_attr_get_even_port(sar);
  926. if(even_port) {
  927. if (af4 && af6) {
  928. *err_code = 400;
  929. *reason = (const u08bits *)"Even Port cannot be used with Dual Allocation";
  930. }
  931. }
  932. }
  933. }
  934. break;
  935. case STUN_ATTRIBUTE_RESERVATION_TOKEN: {
  936. int len = stun_attr_get_len(sar);
  937. if (len != 8) {
  938. *err_code = 400;
  939. *reason = (const u08bits *)"Wrong Format of Reservation Token";
  940. } else if(af4 || af6) {
  941. *err_code = 400;
  942. *reason = (const u08bits *)"Address family attribute can not be used with reservation token request";
  943. } else {
  944. if (even_port >= 0) {
  945. *err_code = 400;
  946. *reason = (const u08bits *)"Reservation Token cannot be used in this request with even port";
  947. } else if (in_reservation_token) {
  948. *err_code = 400;
  949. *reason = (const u08bits *)"Reservation Token cannot be used in this request";
  950. } else {
  951. in_reservation_token = stun_attr_get_reservation_token_value(sar);
  952. }
  953. }
  954. }
  955. break;
  956. case STUN_ATTRIBUTE_ADDITIONAL_ADDRESS_FAMILY:
  957. if(even_port>0) {
  958. *err_code = 400;
  959. *reason = (const u08bits *)"Even Port cannot be used with Dual Allocation";
  960. break;
  961. }
  962. case STUN_ATTRIBUTE_REQUESTED_ADDRESS_FAMILY: {
  963. if(in_reservation_token) {
  964. *err_code = 400;
  965. *reason = (const u08bits *)"Address family attribute can not be used with reservation token request";
  966. } else if(af4 || af6) {
  967. *err_code = 400;
  968. *reason = (const u08bits *)"Extra address family attribute can not be used in the request";
  969. } else {
  970. int af_req = stun_get_requested_address_family(sar);
  971. switch (af_req) {
  972. case STUN_ATTRIBUTE_REQUESTED_ADDRESS_FAMILY_VALUE_IPV4:
  973. if(attr_type == STUN_ATTRIBUTE_ADDITIONAL_ADDRESS_FAMILY) {
  974. *err_code = 400;
  975. *reason = (const u08bits *)"Invalid value of the additional address family attribute";
  976. } else {
  977. af4 = af_req;
  978. }
  979. break;
  980. case STUN_ATTRIBUTE_REQUESTED_ADDRESS_FAMILY_VALUE_IPV6:
  981. if(attr_type == STUN_ATTRIBUTE_ADDITIONAL_ADDRESS_FAMILY) {
  982. af4 = STUN_ATTRIBUTE_REQUESTED_ADDRESS_FAMILY_VALUE_IPV4;
  983. }
  984. af6 = af_req;
  985. break;
  986. default:
  987. *err_code = 440;
  988. }
  989. }
  990. }
  991. break;
  992. default:
  993. if(attr_type>=0x0000 && attr_type<=0x7FFF)
  994. unknown_attrs[(*ua_num)++] = nswap16(attr_type);
  995. };
  996. sar = stun_attr_get_next_str(ioa_network_buffer_data(in_buffer->nbh),
  997. ioa_network_buffer_get_size(in_buffer->nbh),
  998. sar);
  999. }
  1000. if (!transport) {
  1001. *err_code = 400;
  1002. if(!(*reason))
  1003. *reason = (const u08bits *)"Transport field missed or wrong";
  1004. } else if (*ua_num > 0) {
  1005. *err_code = 420;
  1006. } else if (*err_code) {
  1007. ;
  1008. } else if((transport == STUN_ATTRIBUTE_TRANSPORT_TCP_VALUE) && (dont_fragment || in_reservation_token || (even_port!=-1))) {
  1009. *err_code = 400;
  1010. if(!(*reason))
  1011. *reason = (const u08bits *)"Request parameters are incompatible with TCP transport";
  1012. } else {
  1013. if(*(server->mobility)) {
  1014. if(!(ss->is_mobile)) {
  1015. delete_session_from_mobile_map(ss);
  1016. }
  1017. }
  1018. lifetime = stun_adjust_allocate_lifetime(lifetime, ss->max_session_time_auth);
  1019. u64bits out_reservation_token = 0;
  1020. if(inc_quota(ss, username)<0) {
  1021. *err_code = 486;
  1022. } else {
  1023. if(server->allocate_bps_func) {
  1024. max_bps = ss->realm_options.perf_options.max_bps;
  1025. if(max_bps && (!bps || (bps && (bps>max_bps)))) {
  1026. bps = max_bps;
  1027. }
  1028. if(bps && (ss->bps == 0)) {
  1029. ss->bps = server->allocate_bps_func(bps,1);
  1030. if(!(ss->bps)) {
  1031. *err_code = 486;
  1032. *reason = (const u08bits *)"Allocation Bandwidth Quota Reached";
  1033. }
  1034. }
  1035. }
  1036. if(af4) af4 = STUN_ATTRIBUTE_REQUESTED_ADDRESS_FAMILY_VALUE_IPV4;
  1037. if(af6) af6 = STUN_ATTRIBUTE_REQUESTED_ADDRESS_FAMILY_VALUE_IPV6;
  1038. if(af4 && af6) {
  1039. if(server->external_ip_set) {
  1040. *err_code = 440;
  1041. *reason = (const u08bits *)"Dual allocation cannot be supported in the current server configuration";
  1042. }
  1043. if(even_port > 0) {
  1044. *err_code = 440;
  1045. *reason = (const u08bits *)"Dual allocation cannot be supported with even-port functionality";
  1046. }
  1047. }
  1048. if(!(*err_code)) {
  1049. if(!af4 && !af6) {
  1050. int af4res = create_relay_connection(server, ss, lifetime,
  1051. STUN_ATTRIBUTE_REQUESTED_ADDRESS_FAMILY_VALUE_DEFAULT, transport,
  1052. even_port, in_reservation_token, &out_reservation_token,
  1053. err_code, reason,
  1054. tcp_peer_accept_connection);
  1055. if(af4res<0) {
  1056. set_relay_session_failure(alloc,AF_INET);
  1057. if(!(*err_code)) {
  1058. *err_code = 437;
  1059. }
  1060. }
  1061. } else if(!af4 && af6) {
  1062. int af6res = create_relay_connection(server, ss, lifetime,
  1063. af6, transport,
  1064. even_port, in_reservation_token, &out_reservation_token,
  1065. err_code, reason,
  1066. tcp_peer_accept_connection);
  1067. if(af6res<0) {
  1068. set_relay_session_failure(alloc,AF_INET6);
  1069. if(!(*err_code)) {
  1070. *err_code = 437;
  1071. }
  1072. }
  1073. } else if(af4 && !af6) {
  1074. int af4res = create_relay_connection(server, ss, lifetime,
  1075. af4, transport,
  1076. even_port, in_reservation_token, &out_reservation_token,
  1077. err_code, reason,
  1078. tcp_peer_accept_connection);
  1079. if(af4res<0) {
  1080. set_relay_session_failure(alloc,AF_INET);
  1081. if(!(*err_code)) {
  1082. *err_code = 437;
  1083. }
  1084. }
  1085. } else {
  1086. const u08bits *reason4 = NULL;
  1087. const u08bits *reason6 = NULL;
  1088. {
  1089. int af4res = create_relay_connection(server, ss, lifetime,
  1090. af4, transport,
  1091. even_port, in_reservation_token, &out_reservation_token,
  1092. &err_code4, &reason4,
  1093. tcp_peer_accept_connection);
  1094. if(af4res<0) {
  1095. set_relay_session_failure(alloc,AF_INET);
  1096. if(!err_code4) {
  1097. err_code4 = 440;
  1098. }
  1099. }
  1100. }
  1101. {
  1102. int af6res = create_relay_connection(server, ss, lifetime,
  1103. af6, transport,
  1104. even_port, in_reservation_token, &out_reservation_token,
  1105. &err_code6, &reason6,
  1106. tcp_peer_accept_connection);
  1107. if(af6res<0) {
  1108. set_relay_session_failure(alloc,AF_INET6);
  1109. if(!err_code6) {
  1110. err_code6 = 440;
  1111. }
  1112. }
  1113. }
  1114. if(err_code4 && err_code6) {
  1115. if(reason4) {
  1116. *err_code = err_code4;
  1117. *reason = reason4;
  1118. } else if(reason6) {
  1119. *err_code = err_code6;
  1120. *reason = reason6;
  1121. } else {
  1122. *err_code = err_code4;
  1123. }
  1124. }
  1125. }
  1126. }
  1127. if (*err_code) {
  1128. if(!(*reason)) {
  1129. *reason = (const u08bits *)"Cannot create relay endpoint(s)";
  1130. }
  1131. } else {
  1132. set_allocation_valid(alloc,1);
  1133. stun_tid_cpy(&(alloc->tid), tid);
  1134. size_t len = ioa_network_buffer_get_size(nbh);
  1135. ioa_addr xor_relayed_addr1, *pxor_relayed_addr1=NULL;
  1136. ioa_addr xor_relayed_addr2, *pxor_relayed_addr2=NULL;
  1137. ioa_addr *relayed_addr1 = get_local_addr_from_ioa_socket(get_relay_socket_ss(ss,AF_INET));
  1138. ioa_addr *relayed_addr2 = get_local_addr_from_ioa_socket(get_relay_socket_ss(ss,AF_INET6));
  1139. if(get_relay_session_failure(alloc,AF_INET)) {
  1140. addr_set_any(&xor_relayed_addr1);
  1141. pxor_relayed_addr1 = &xor_relayed_addr1;
  1142. } else if(relayed_addr1) {
  1143. if(server->external_ip_set) {
  1144. addr_cpy(&xor_relayed_addr1, &(server->external_ip));
  1145. addr_set_port(&xor_relayed_addr1,addr_get_port(relayed_addr1));
  1146. } else {
  1147. addr_cpy(&xor_relayed_addr1, relayed_addr1);
  1148. }
  1149. pxor_relayed_addr1 = &xor_relayed_addr1;
  1150. }
  1151. if(get_relay_session_failure(alloc,AF_INET6)) {
  1152. addr_set_any(&xor_relayed_addr2);
  1153. pxor_relayed_addr2 = &xor_relayed_addr2;
  1154. } else if(relayed_addr2) {
  1155. if(server->external_ip_set) {
  1156. addr_cpy(&xor_relayed_addr2, &(server->external_ip));
  1157. addr_set_port(&xor_relayed_addr2,addr_get_port(relayed_addr2));
  1158. } else {
  1159. addr_cpy(&xor_relayed_addr2, relayed_addr2);
  1160. }
  1161. pxor_relayed_addr2 = &xor_relayed_addr2;
  1162. }
  1163. if(pxor_relayed_addr1 || pxor_relayed_addr2) {
  1164. stun_set_allocate_response_str(ioa_network_buffer_data(nbh), &len, tid,
  1165. pxor_relayed_addr1, pxor_relayed_addr2,
  1166. get_remote_addr_from_ioa_socket(ss->client_socket), lifetime,
  1167. 0,NULL,
  1168. out_reservation_token,
  1169. ss->s_mobile_id);
  1170. if(ss->bps) {
  1171. stun_attr_add_bandwidth_str(ioa_network_buffer_data(nbh), &len, ss->bps);
  1172. }
  1173. ioa_network_buffer_set_size(nbh,len);
  1174. *resp_constructed = 1;
  1175. turn_report_allocation_set(&(ss->alloc), lifetime, 0);
  1176. }
  1177. }
  1178. }
  1179. }
  1180. }
  1181. if (!(*resp_constructed)) {
  1182. if (!(*err_code)) {
  1183. *err_code = 437;
  1184. }
  1185. size_t len = ioa_network_buffer_get_size(nbh);
  1186. stun_set_allocate_response_str(ioa_network_buffer_data(nbh), &len, tid, NULL, NULL, NULL, 0, *err_code, *reason, 0, ss->s_mobile_id);
  1187. ioa_network_buffer_set_size(nbh,len);
  1188. *resp_constructed = 1;
  1189. }
  1190. if(*resp_constructed && !(*err_code)) {
  1191. if(err_code4) {
  1192. size_t len = ioa_network_buffer_get_size(nbh);
  1193. stun_attr_add_address_error_code(ioa_network_buffer_data(nbh), &len, STUN_ATTRIBUTE_REQUESTED_ADDRESS_FAMILY_VALUE_IPV4, err_code4);
  1194. ioa_network_buffer_set_size(nbh,len);
  1195. }
  1196. if(err_code6) {
  1197. size_t len = ioa_network_buffer_get_size(nbh);
  1198. stun_attr_add_address_error_code(ioa_network_buffer_data(nbh), &len, STUN_ATTRIBUTE_REQUESTED_ADDRESS_FAMILY_VALUE_IPV6, err_code6);
  1199. ioa_network_buffer_set_size(nbh,len);
  1200. }
  1201. }
  1202. return 0;
  1203. }
  1204. static void copy_auth_parameters(ts_ur_super_session *orig_ss, ts_ur_super_session *ss) {
  1205. if(orig_ss && ss) {
  1206. dec_quota(ss);
  1207. ns_bcopy(orig_ss->nonce,ss->nonce,sizeof(ss->nonce));
  1208. ss->nonce_expiration_time = orig_ss->nonce_expiration_time;
  1209. ns_bcopy(&(orig_ss->realm_options),&(ss->realm_options),sizeof(ss->realm_options));
  1210. ns_bcopy(orig_ss->username,ss->username,sizeof(ss->username));
  1211. ss->hmackey_set = orig_ss->hmackey_set;
  1212. ns_bcopy(orig_ss->hmackey,ss->hmackey,sizeof(ss->hmackey));
  1213. ss->oauth = orig_ss->oauth;
  1214. ns_bcopy(orig_ss->origin,ss->origin,sizeof(ss->origin));
  1215. ss->origin_set = orig_ss->origin_set;
  1216. ns_bcopy(orig_ss->pwd,ss->pwd,sizeof(ss->pwd));
  1217. ss->max_session_time_auth = orig_ss->max_session_time_auth;
  1218. inc_quota(ss,ss->username);
  1219. }
  1220. }
  1221. static int handle_turn_refresh(turn_turnserver *server,
  1222. ts_ur_super_session *ss, stun_tid *tid, int *resp_constructed,
  1223. int *err_code, const u08bits **reason, u16bits *unknown_attrs, u16bits *ua_num,
  1224. ioa_net_data *in_buffer, ioa_network_buffer_handle nbh,
  1225. int message_integrity, int *no_response, int can_resume) {
  1226. allocation* a = get_allocation_ss(ss);
  1227. int af4c = 0;
  1228. int af6c = 0;
  1229. int af4 = 0;
  1230. int af6 = 0;
  1231. {
  1232. int i;
  1233. for(i = 0;i<ALLOC_PROTOCOLS_NUMBER; ++i) {
  1234. if(a->relay_sessions[i].s && !ioa_socket_tobeclosed(a->relay_sessions[i].s)) {
  1235. int family = get_ioa_socket_address_family(a->relay_sessions[i].s);
  1236. if(AF_INET == family) {
  1237. af4c = 1;
  1238. } else if(AF_INET6 == family) {
  1239. af6c = 1;
  1240. }
  1241. }
  1242. }
  1243. }
  1244. if (!is_allocation_valid(a) && !(*(server->mobility))) {
  1245. *err_code = 437;
  1246. *reason = (const u08bits *)"Invalid allocation";
  1247. } else {
  1248. turn_time_t lifetime = 0;
  1249. int to_delete = 0;
  1250. mobile_id_t mid = 0;
  1251. char smid[sizeof(ss->s_mobile_id)] = "\0";
  1252. stun_attr_ref sar = stun_attr_get_first_str(ioa_network_buffer_data(in_buffer->nbh),
  1253. ioa_network_buffer_get_size(in_buffer->nbh));
  1254. while (sar && (!(*err_code)) && (*ua_num < MAX_NUMBER_OF_UNKNOWN_ATTRS)) {
  1255. int attr_type = stun_attr_get_type(sar);
  1256. switch (attr_type) {
  1257. SKIP_ATTRIBUTES;
  1258. case STUN_ATTRIBUTE_MOBILITY_TICKET: {
  1259. if(!(*(server->mobility))) {
  1260. *err_code = 405;
  1261. *reason = (const u08bits *)"Mobility forbidden";
  1262. } else {
  1263. int smid_len = stun_attr_get_len(sar);
  1264. if(smid_len>0 && (((size_t)smid_len)<sizeof(smid))) {
  1265. const u08bits* smid_val = stun_attr_get_value(sar);
  1266. if(smid_val) {
  1267. ns_bcopy(smid_val, smid, (size_t)smid_len);
  1268. mid = string_to_mobile_id(smid);
  1269. if(is_allocation_valid(a) && (mid != ss->old_mobile_id)) {
  1270. *err_code = 400;
  1271. *reason = (const u08bits *)"Mobility ticket cannot be used for a stable, already established allocation";
  1272. }
  1273. }
  1274. } else {
  1275. *err_code = 400;
  1276. *reason = (const u08bits *)"Mobility ticket has wrong length";
  1277. }
  1278. }
  1279. }
  1280. break;
  1281. case STUN_ATTRIBUTE_LIFETIME: {
  1282. if (stun_attr_get_len(sar) != 4) {
  1283. *err_code = 400;
  1284. *reason = (const u08bits *)"Wrong Lifetime field format";
  1285. } else {
  1286. const u08bits* value = stun_attr_get_value(sar);
  1287. if (!value) {
  1288. *err_code = 400;
  1289. *reason = (const u08bits *)"Wrong lifetime field data";
  1290. } else {
  1291. lifetime = nswap32(*((const u32bits*)value));
  1292. if (!lifetime)
  1293. to_delete = 1;
  1294. }
  1295. }
  1296. }
  1297. break;
  1298. case STUN_ATTRIBUTE_ADDITIONAL_ADDRESS_FAMILY: /* deprecated, for backward compatibility with older versions of TURN-bis */
  1299. case STUN_ATTRIBUTE_REQUESTED_ADDRESS_FAMILY: {
  1300. int af_req = stun_get_requested_address_family(sar);
  1301. {
  1302. int is_err = 0;
  1303. switch (af_req) {
  1304. case STUN_ATTRIBUTE_REQUESTED_ADDRESS_FAMILY_VALUE_IPV4:
  1305. if(!af4c) {
  1306. is_err = 1;
  1307. } else {
  1308. af4 = 1;
  1309. }
  1310. break;
  1311. case STUN_ATTRIBUTE_REQUESTED_ADDRESS_FAMILY_VALUE_IPV6:
  1312. if(!af6c) {
  1313. is_err = 1;
  1314. } else {
  1315. af6 = 1;
  1316. }
  1317. break;
  1318. default:
  1319. is_err = 1;
  1320. }
  1321. if(is_err) {
  1322. *err_code = 443;
  1323. *reason = (const u08bits *)"Peer Address Family Mismatch (1)";
  1324. }
  1325. }
  1326. }
  1327. break;
  1328. default:
  1329. if(attr_type>=0x0000 && attr_type<=0x7FFF)
  1330. unknown_attrs[(*ua_num)++] = nswap16(attr_type);
  1331. };
  1332. sar = stun_attr_get_next_str(ioa_network_buffer_data(in_buffer->nbh),
  1333. ioa_network_buffer_get_size(in_buffer->nbh), sar);
  1334. }
  1335. if (*ua_num > 0) {
  1336. *err_code = 420;
  1337. } else if (*err_code) {
  1338. ;
  1339. } else if(!is_allocation_valid(a)) {
  1340. if(mid && smid[0]) {
  1341. turnserver_id tsid = ((0xFF00000000000000LL) & mid)>>56;
  1342. if(tsid != server->id) {
  1343. if(server->send_socket_to_relay) {
  1344. ioa_socket_handle new_s = detach_ioa_socket(ss->client_socket);
  1345. if(new_s) {
  1346. if(server->send_socket_to_relay(tsid, mid, tid, new_s, message_integrity,
  1347. RMT_MOBILE_SOCKET, in_buffer, can_resume)<0) {
  1348. *err_code = 400;
  1349. *reason = (const u08bits *)"Wrong mobile ticket";
  1350. } else {
  1351. *no_response = 1;
  1352. }
  1353. } else {
  1354. *err_code = 500;
  1355. *reason = (const u08bits *)"Cannot create new socket";
  1356. return -1;
  1357. }
  1358. } else {
  1359. *err_code = 500;
  1360. *reason = (const u08bits *)"Server send socket procedure is not set";
  1361. }
  1362. ss->to_be_closed = 1;
  1363. } else {
  1364. ts_ur_super_session *orig_ss = get_session_from_mobile_map(server, mid);
  1365. if(!orig_ss || orig_ss->to_be_closed || ioa_socket_tobeclosed(orig_ss->client_socket)) {
  1366. *err_code = 404;
  1367. *reason = (const u08bits *)"Allocation not found";
  1368. } else if(orig_ss == ss) {
  1369. *err_code = 437;
  1370. *reason = (const u08bits *)"Invalid allocation";
  1371. } else if(!(orig_ss->is_mobile)) {
  1372. *err_code = 500;
  1373. *reason = (const u08bits *)"Software error: invalid mobile allocation";
  1374. } else if(orig_ss->client_socket == ss->client_socket) {
  1375. *err_code = 500;
  1376. *reason = (const u08bits *)"Software error: invalid mobile client socket (orig)";
  1377. } else if(!(ss->client_socket)) {
  1378. *err_code = 500;
  1379. *reason = (const u08bits *)"Software error: invalid mobile client socket (new)";
  1380. } else {
  1381. get_realm_options_by_name(orig_ss->realm_options.name, &(ss->realm_options));
  1382. //Check security:
  1383. int postpone_reply = 0;
  1384. if(!(ss->hmackey_set)) {
  1385. copy_auth_parameters(orig_ss,ss);
  1386. }
  1387. if(check_stun_auth(server, ss, tid, resp_constructed, err_code, reason, in_buffer, nbh,
  1388. STUN_METHOD_REFRESH, &message_integrity, &postpone_reply, can_resume)<0) {
  1389. if(!(*err_code)) {
  1390. *err_code = 401;
  1391. }
  1392. }
  1393. if(postpone_reply) {
  1394. *no_response = 1;
  1395. } else if(!(*err_code)) {
  1396. //Session transfer:
  1397. if (to_delete)
  1398. lifetime = 0;
  1399. else {
  1400. lifetime = stun_adjust_allocate_lifetime(lifetime, ss->max_session_time_auth);
  1401. }
  1402. if (af4c && refresh_relay_connection(server, orig_ss, lifetime, 0, 0, 0,
  1403. err_code, AF_INET) < 0) {
  1404. if (!(*err_code)) {
  1405. *err_code = 437;
  1406. *reason = (const u08bits *)"Cannot refresh relay connection (internal error)";
  1407. }
  1408. } else if (af6c && refresh_relay_connection(server, orig_ss, lifetime, 0, 0, 0,
  1409. err_code, AF_INET6) < 0) {
  1410. if (!(*err_code)) {
  1411. *err_code = 437;
  1412. *reason = (const u08bits *)"Cannot refresh relay connection (internal error)";
  1413. }
  1414. } else {
  1415. //Transfer socket:
  1416. ioa_socket_handle s = detach_ioa_socket(ss->client_socket);
  1417. ss->to_be_closed = 1;
  1418. if(!s) {
  1419. *err_code = 500;
  1420. } else {
  1421. if(attach_socket_to_session(server, s, orig_ss) < 0) {
  1422. if(orig_ss->client_socket != s) {
  1423. IOA_CLOSE_SOCKET(s);
  1424. }
  1425. *err_code = 500;
  1426. } else {
  1427. if(ss->hmackey_set) {
  1428. copy_auth_parameters(ss,orig_ss);
  1429. }
  1430. delete_session_from_mobile_map(ss);
  1431. delete_session_from_mobile_map(orig_ss);
  1432. put_session_into_mobile_map(orig_ss);
  1433. //Use new buffer and redefine ss:
  1434. nbh = ioa_network_buffer_allocate(server->e);
  1435. dec_quota(ss);
  1436. ss = orig_ss;
  1437. inc_quota(ss,ss->username);
  1438. ss->old_mobile_id = mid;
  1439. size_t len = ioa_network_buffer_get_size(nbh);
  1440. turn_report_allocation_set(&(ss->alloc), lifetime, 1);
  1441. stun_init_success_response_str(STUN_METHOD_REFRESH, ioa_network_buffer_data(nbh), &len, tid);
  1442. u32bits lt = nswap32(lifetime);
  1443. stun_attr_add_str(ioa_network_buffer_data(nbh), &len, STUN_ATTRIBUTE_LIFETIME,
  1444. (const u08bits*) &lt, 4);
  1445. ioa_network_buffer_set_size(nbh,len);
  1446. stun_attr_add_str(ioa_network_buffer_data(nbh), &len,
  1447. STUN_ATTRIBUTE_MOBILITY_TICKET,
  1448. (u08bits*)ss->s_mobile_id,strlen(ss->s_mobile_id));
  1449. ioa_network_buffer_set_size(nbh,len);
  1450. {
  1451. static const u08bits *field = (const u08bits *) TURN_SOFTWARE;
  1452. static const size_t fsz = sizeof(TURN_SOFTWARE)-1;
  1453. size_t len = ioa_network_buffer_get_size(nbh);
  1454. stun_attr_add_str(ioa_network_buffer_data(nbh), &len, STUN_ATTRIBUTE_SOFTWARE, field, fsz);
  1455. ioa_network_buffer_set_size(nbh, len);
  1456. }
  1457. if(message_integrity) {
  1458. stun_attr_add_integrity_str(server->ct,ioa_network_buffer_data(nbh),&len,ss->hmackey,ss->pwd,SHATYPE_DEFAULT);
  1459. ioa_network_buffer_set_size(nbh,len);
  1460. }
  1461. if ((server->fingerprint) || ss->enforce_fingerprints) {
  1462. if (stun_attr_add_fingerprint_str(ioa_network_buffer_data(nbh), &len) < 0) {
  1463. *err_code = 500;
  1464. ioa_network_buffer_delete(server->e, nbh);
  1465. return -1;
  1466. }
  1467. ioa_network_buffer_set_size(nbh, len);
  1468. }
  1469. *no_response = 1;
  1470. return write_client_connection(server, ss, nbh, TTL_IGNORE, TOS_IGNORE);
  1471. }
  1472. }
  1473. }
  1474. }
  1475. report_turn_session_info(server,orig_ss,0);
  1476. }
  1477. }
  1478. } else {
  1479. *err_code = 437;
  1480. *reason = (const u08bits *)"Invalid allocation";
  1481. }
  1482. } else {
  1483. if (to_delete)
  1484. lifetime = 0;
  1485. else {
  1486. lifetime = stun_adjust_allocate_lifetime(lifetime, ss->max_session_time_auth);
  1487. }
  1488. if(!af4 && !af6) {
  1489. af4 = af4c;
  1490. af6 = af6c;
  1491. }
  1492. if (af4 && refresh_relay_connection(server, ss, lifetime, 0, 0, 0,
  1493. err_code, AF_INET) < 0) {
  1494. if (!(*err_code)) {
  1495. *err_code = 437;
  1496. *reason = (const u08bits *)"Cannot refresh relay connection (internal error)";
  1497. }
  1498. } else if (af6 && refresh_relay_connection(server, ss, lifetime, 0, 0, 0,
  1499. err_code, AF_INET6) < 0) {
  1500. if (!(*err_code)) {
  1501. *err_code = 437;
  1502. *reason = (const u08bits *)"Cannot refresh relay connection (internal error)";
  1503. }
  1504. } else {
  1505. turn_report_allocation_set(&(ss->alloc), lifetime, 1);
  1506. size_t len = ioa_network_buffer_get_size(nbh);
  1507. stun_init_success_response_str(STUN_METHOD_REFRESH, ioa_network_buffer_data(nbh), &len, tid);
  1508. if(ss->s_mobile_id[0]) {
  1509. stun_attr_add_str(ioa_network_buffer_data(nbh), &len,
  1510. STUN_ATTRIBUTE_MOBILITY_TICKET,
  1511. (u08bits*)ss->s_mobile_id,strlen(ss->s_mobile_id));
  1512. ioa_network_buffer_set_size(nbh,len);
  1513. }
  1514. u32bits lt = nswap32(lifetime);
  1515. stun_attr_add_str(ioa_network_buffer_data(nbh), &len, STUN_ATTRIBUTE_LIFETIME,
  1516. (const u08bits*) &lt, 4);
  1517. ioa_network_buffer_set_size(nbh,len);
  1518. *resp_constructed = 1;
  1519. }
  1520. }
  1521. }
  1522. if(!no_response) {
  1523. if (!(*resp_constructed)) {
  1524. if (!(*err_code)) {
  1525. *err_code = 437;
  1526. }
  1527. size_t len = ioa_network_buffer_get_size(nbh);
  1528. stun_init_error_response_str(STUN_METHOD_REFRESH, ioa_network_buffer_data(nbh), &len, *err_code, *reason, tid);
  1529. ioa_network_buffer_set_size(nbh,len);
  1530. *resp_constructed = 1;
  1531. }
  1532. }
  1533. return 0;
  1534. }
  1535. /* RFC 6062 ==>> */
  1536. static void tcp_deliver_delayed_buffer(unsent_buffer *ub, ioa_socket_handle s, ts_ur_super_session *ss)
  1537. {
  1538. if(ub && s && ub->bufs && ub->sz && ss) {
  1539. size_t i = 0;
  1540. do {
  1541. ioa_network_buffer_handle nbh = top_unsent_buffer(ub);
  1542. if(!nbh)
  1543. break;
  1544. u32bits bytes = (u32bits)ioa_network_buffer_get_size(nbh);
  1545. int ret = send_data_from_ioa_socket_nbh(s, NULL, nbh, TTL_IGNORE, TOS_IGNORE, NULL);
  1546. if (ret < 0) {
  1547. set_ioa_socket_tobeclosed(s);
  1548. } else {
  1549. ++(ss->sent_packets);
  1550. ss->sent_bytes += bytes;
  1551. turn_report_session_usage(ss);
  1552. }
  1553. pop_unsent_buffer(ub);
  1554. } while(!ioa_socket_tobeclosed(s) && ((i++)<MAX_UNSENT_BUFFER_SIZE));
  1555. }
  1556. }
  1557. static void tcp_peer_input_handler(ioa_socket_handle s, int event_type, ioa_net_data *in_buffer, void *arg, int can_resume)
  1558. {
  1559. if (!(event_type & IOA_EV_READ) || !arg)
  1560. return;
  1561. UNUSED_ARG(s);
  1562. UNUSED_ARG(can_resume);
  1563. tcp_connection *tc = (tcp_connection*)arg;
  1564. ts_ur_super_session *ss=NULL;
  1565. allocation *a=(allocation*)tc->owner;
  1566. if(a) {
  1567. ss=(ts_ur_super_session*)a->owner;
  1568. }
  1569. if((tc->state != TC_STATE_READY) || !(tc->client_s)) {
  1570. add_unsent_buffer(&(tc->ub_to_client), in_buffer->nbh);
  1571. in_buffer->nbh = NULL;
  1572. return;
  1573. }
  1574. ioa_network_buffer_handle nbh = in_buffer->nbh;
  1575. in_buffer->nbh = NULL;
  1576. u32bits bytes = (u32bits)ioa_network_buffer_get_size(nbh);
  1577. int ret = send_data_from_ioa_socket_nbh(tc->client_s, NULL, nbh, TTL_IGNORE, TOS_IGNORE, NULL);
  1578. if (ret < 0) {
  1579. set_ioa_socket_tobeclosed(s);
  1580. } else if(ss) {
  1581. ++(ss->sent_packets);
  1582. ss->sent_bytes += bytes;
  1583. turn_report_session_usage(ss);
  1584. }
  1585. }
  1586. static void tcp_client_input_handler_rfc6062data(ioa_socket_handle s, int event_type, ioa_net_data *in_buffer, void *arg, int can_resume)
  1587. {
  1588. if (!(event_type & IOA_EV_READ) || !arg)
  1589. return;
  1590. UNUSED_ARG(s);
  1591. UNUSED_ARG(can_resume);
  1592. tcp_connection *tc = (tcp_connection*)arg;
  1593. ts_ur_super_session *ss=NULL;
  1594. allocation *a=(allocation*)tc->owner;
  1595. if(a) {
  1596. ss=(ts_ur_super_session*)a->owner;
  1597. }
  1598. if(tc->state != TC_STATE_READY)
  1599. return;
  1600. if(!(tc->peer_s))
  1601. return;
  1602. ioa_network_buffer_handle nbh = in_buffer->nbh;
  1603. in_buffer->nbh = NULL;
  1604. if(ss) {
  1605. u32bits bytes = (u32bits)ioa_network_buffer_get_size(nbh);
  1606. ++(ss->received_packets);
  1607. ss->received_bytes += bytes;
  1608. }
  1609. int ret = send_data_from_ioa_socket_nbh(tc->peer_s, NULL, nbh, TTL_IGNORE, TOS_IGNORE, NULL);
  1610. if (ret < 0) {
  1611. set_ioa_socket_tobeclosed(s);
  1612. }
  1613. turn_report_session_usage(ss);
  1614. }
  1615. static void tcp_conn_bind_timeout_handler(ioa_engine_handle e, void *arg)
  1616. {
  1617. UNUSED_ARG(e);
  1618. if(arg) {
  1619. tcp_connection *tc = (tcp_connection *)arg;
  1620. delete_tcp_connection(tc);
  1621. }
  1622. }
  1623. static void tcp_peer_connection_completed_callback(int success, void *arg)
  1624. {
  1625. if(arg) {
  1626. tcp_connection *tc = (tcp_connection *)arg;
  1627. allocation *a = (allocation*)(tc->owner);
  1628. ts_ur_super_session *ss = (ts_ur_super_session*)(a->owner);
  1629. turn_turnserver *server=(turn_turnserver*)(ss->server);
  1630. int err_code = 0;
  1631. IOA_EVENT_DEL(tc->peer_conn_timeout);
  1632. ioa_network_buffer_handle nbh = ioa_network_buffer_allocate(server->e);
  1633. size_t len = ioa_network_buffer_get_size(nbh);
  1634. if(success) {
  1635. if(register_callback_on_ioa_socket(server->e, tc->peer_s, IOA_EV_READ, tcp_peer_input_handler, tc, 1)<0) {
  1636. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "%s: cannot set TCP peer data input callback\n", __FUNCTION__);
  1637. success=0;
  1638. err_code = 500;
  1639. }
  1640. }
  1641. if(success) {
  1642. tc->state = TC_STATE_PEER_CONNECTED;
  1643. stun_init_success_response_str(STUN_METHOD_CONNECT, ioa_network_buffer_data(nbh), &len, &(tc->tid));
  1644. stun_attr_add_str(ioa_network_buffer_data(nbh), &len, STUN_ATTRIBUTE_CONNECTION_ID,
  1645. (const u08bits*)&(tc->id), 4);
  1646. IOA_EVENT_DEL(tc->conn_bind_timeout);
  1647. tc->conn_bind_timeout = set_ioa_timer(server->e, TCP_CONN_BIND_TIMEOUT, 0,
  1648. tcp_conn_bind_timeout_handler, tc, 0,
  1649. "tcp_conn_bind_timeout_handler");
  1650. } else {
  1651. tc->state = TC_STATE_FAILED;
  1652. if(!err_code) {
  1653. err_code = 447;
  1654. }
  1655. {
  1656. char ls[257]="\0";
  1657. char rs[257]="\0";
  1658. ioa_addr *laddr = get_local_addr_from_ioa_socket(ss->client_socket);
  1659. if(laddr)
  1660. addr_to_string(laddr,(u08bits*)ls);
  1661. addr_to_string(&(tc->peer_addr),(u08bits*)rs);
  1662. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "%s: failure to connect from %s to %s\n", __FUNCTION__, ls,rs);
  1663. }
  1664. stun_init_error_response_str(STUN_METHOD_CONNECT, ioa_network_buffer_data(nbh), &len, err_code, NULL, &(tc->tid));
  1665. }
  1666. ioa_network_buffer_set_size(nbh,len);
  1667. if(need_stun_authentication(server, ss)) {
  1668. stun_attr_add_integrity_str(server->ct,ioa_network_buffer_data(nbh),&len,ss->hmackey,ss->pwd,SHATYPE_DEFAULT);
  1669. ioa_network_buffer_set_size(nbh,len);
  1670. }
  1671. write_client_connection(server, ss, nbh, TTL_IGNORE, TOS_IGNORE);
  1672. if(!success) {
  1673. delete_tcp_connection(tc);
  1674. }
  1675. /* test */
  1676. else if(0)
  1677. {
  1678. int i = 0;
  1679. for(i=0;i<22;i++) {
  1680. ioa_network_buffer_handle nbh_test = ioa_network_buffer_allocate(server->e);
  1681. size_t len_test = ioa_network_buffer_get_size(nbh_test);
  1682. u08bits *data = ioa_network_buffer_data(nbh_test);
  1683. const char* data_test="111.111.111.111.111";
  1684. len_test = strlen(data_test);
  1685. ns_bcopy(data_test,data,len_test);
  1686. ioa_network_buffer_set_size(nbh_test,len_test);
  1687. send_data_from_ioa_socket_nbh(tc->peer_s, NULL, nbh_test, TTL_IGNORE, TOS_IGNORE, NULL);
  1688. }
  1689. }
  1690. }
  1691. }
  1692. static void tcp_peer_conn_timeout_handler(ioa_engine_handle e, void *arg)
  1693. {
  1694. UNUSED_ARG(e);
  1695. tcp_peer_connection_completed_callback(0,arg);
  1696. }
  1697. static int tcp_start_connection_to_peer(turn_turnserver *server, ts_ur_super_session *ss, stun_tid *tid,
  1698. allocation *a, ioa_addr *peer_addr,
  1699. int *err_code, const u08bits **reason)
  1700. {
  1701. FUNCSTART;
  1702. if(!ss) {
  1703. *err_code = 500;
  1704. *reason = (const u08bits *)"Server error: empty session";
  1705. FUNCEND;
  1706. return -1;
  1707. }
  1708. if(!peer_addr) {
  1709. *err_code = 500;
  1710. *reason = (const u08bits *)"Server error: empty peer addr";
  1711. FUNCEND;
  1712. return -1;
  1713. }
  1714. if(!get_relay_socket(a,peer_addr->ss.sa_family)) {
  1715. *err_code = 500;
  1716. *reason = (const u08bits *)"Server error: no relay connection created";
  1717. FUNCEND;
  1718. return -1;
  1719. }
  1720. tcp_connection *tc = get_tcp_connection_by_peer(a, peer_addr);
  1721. if(tc) {
  1722. *err_code = 446;
  1723. FUNCEND;
  1724. return -1;
  1725. }
  1726. tc = create_tcp_connection(server->id, a, tid, peer_addr, err_code);
  1727. if(!tc) {
  1728. if(!(*err_code)) {
  1729. *err_code = 500;
  1730. *reason = (const u08bits *)"Server error: TCP connection object creation failed";
  1731. }
  1732. FUNCEND;
  1733. return -1;
  1734. } else if(*err_code) {
  1735. delete_tcp_connection(tc);
  1736. FUNCEND;
  1737. return -1;
  1738. }
  1739. IOA_EVENT_DEL(tc->peer_conn_timeout);
  1740. tc->peer_conn_timeout = set_ioa_timer(server->e, TCP_PEER_CONN_TIMEOUT, 0,
  1741. tcp_peer_conn_timeout_handler, tc, 0,
  1742. "tcp_peer_conn_timeout_handler");
  1743. ioa_socket_handle tcs = ioa_create_connecting_tcp_relay_socket(get_relay_socket(a,peer_addr->ss.sa_family),
  1744. peer_addr, tcp_peer_connection_completed_callback, tc);
  1745. if(!tcs) {
  1746. delete_tcp_connection(tc);
  1747. *err_code = 500;
  1748. *reason = (const u08bits *)"Server error: TCP relay socket for connection cannot be created";
  1749. FUNCEND;
  1750. return -1;
  1751. }
  1752. tc->state = TC_STATE_CLIENT_TO_PEER_CONNECTING;
  1753. if(tc->peer_s != tcs) {
  1754. IOA_CLOSE_SOCKET(tc->peer_s);
  1755. tc->peer_s = tcs;
  1756. }
  1757. set_ioa_socket_sub_session(tc->peer_s,tc);
  1758. FUNCEND;
  1759. return 0;
  1760. }
  1761. static void tcp_peer_accept_connection(ioa_socket_handle s, void *arg)
  1762. {
  1763. if(s) {
  1764. if(!arg) {
  1765. close_ioa_socket(s);
  1766. return;
  1767. }
  1768. ts_ur_super_session *ss = (ts_ur_super_session*)arg;
  1769. turn_turnserver *server=(turn_turnserver*)(ss->server);
  1770. FUNCSTART;
  1771. allocation *a = &(ss->alloc);
  1772. ioa_addr *peer_addr = get_remote_addr_from_ioa_socket(s);
  1773. if(!peer_addr) {
  1774. close_ioa_socket(s);
  1775. FUNCEND;
  1776. return;
  1777. }
  1778. tcp_connection *tc = get_tcp_connection_by_peer(a, peer_addr);
  1779. if(tc) {
  1780. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "%s: peer data socket with this address already exist\n", __FUNCTION__);
  1781. if(tc->peer_s != s)
  1782. close_ioa_socket(s);
  1783. FUNCEND;
  1784. return;
  1785. }
  1786. if(!good_peer_addr(server, ss->realm_options.name, peer_addr)) {
  1787. u08bits saddr[256];
  1788. addr_to_string(peer_addr, saddr);
  1789. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "%s: an attempt to connect from a peer with forbidden address: %s\n", __FUNCTION__,saddr);
  1790. close_ioa_socket(s);
  1791. FUNCEND;
  1792. return;
  1793. }
  1794. if(!can_accept_tcp_connection_from_peer(a,peer_addr,server->server_relay)) {
  1795. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "%s: peer has no permission to connect\n", __FUNCTION__);
  1796. close_ioa_socket(s);
  1797. FUNCEND;
  1798. return;
  1799. }
  1800. stun_tid tid;
  1801. ns_bzero(&tid,sizeof(stun_tid));
  1802. int err_code=0;
  1803. tc = create_tcp_connection(server->id, a, &tid, peer_addr, &err_code);
  1804. if(!tc) {
  1805. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "%s: cannot create TCP connection\n", __FUNCTION__);
  1806. close_ioa_socket(s);
  1807. FUNCEND;
  1808. return;
  1809. }
  1810. tc->state = TC_STATE_PEER_CONNECTED;
  1811. tc->peer_s = s;
  1812. set_ioa_socket_session(s,ss);
  1813. set_ioa_socket_sub_session(s,tc);
  1814. if(register_callback_on_ioa_socket(server->e, s, IOA_EV_READ, tcp_peer_input_handler, tc, 1)<0) {
  1815. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "%s: cannot set TCP peer data input callback\n", __FUNCTION__);
  1816. IOA_CLOSE_SOCKET(tc->peer_s);
  1817. tc->state = TC_STATE_UNKNOWN;
  1818. FUNCEND;
  1819. return;
  1820. }
  1821. IOA_EVENT_DEL(tc->conn_bind_timeout);
  1822. tc->conn_bind_timeout = set_ioa_timer(server->e, TCP_CONN_BIND_TIMEOUT, 0,
  1823. tcp_conn_bind_timeout_handler, tc, 0,
  1824. "tcp_conn_bind_timeout_handler");
  1825. ioa_network_buffer_handle nbh = ioa_network_buffer_allocate(server->e);
  1826. size_t len = ioa_network_buffer_get_size(nbh);
  1827. stun_init_indication_str(STUN_METHOD_CONNECTION_ATTEMPT, ioa_network_buffer_data(nbh), &len);
  1828. stun_attr_add_str(ioa_network_buffer_data(nbh), &len, STUN_ATTRIBUTE_CONNECTION_ID,
  1829. (const u08bits*)&(tc->id), 4);
  1830. stun_attr_add_addr_str(ioa_network_buffer_data(nbh), &len, STUN_ATTRIBUTE_XOR_PEER_ADDRESS, peer_addr);
  1831. ioa_network_buffer_set_size(nbh,len);
  1832. {
  1833. static const u08bits *field = (const u08bits *) TURN_SOFTWARE;
  1834. static const size_t fsz = sizeof(TURN_SOFTWARE)-1;
  1835. size_t len = ioa_network_buffer_get_size(nbh);
  1836. stun_attr_add_str(ioa_network_buffer_data(nbh), &len, STUN_ATTRIBUTE_SOFTWARE, field, fsz);
  1837. ioa_network_buffer_set_size(nbh, len);
  1838. }
  1839. if ((server->fingerprint) || ss->enforce_fingerprints) {
  1840. size_t len = ioa_network_buffer_get_size(nbh);
  1841. stun_attr_add_fingerprint_str(ioa_network_buffer_data(nbh), &len);
  1842. ioa_network_buffer_set_size(nbh, len);
  1843. }
  1844. write_client_connection(server, ss, nbh, TTL_IGNORE, TOS_IGNORE);
  1845. FUNCEND;
  1846. }
  1847. }
  1848. static int handle_turn_connect(turn_turnserver *server,
  1849. ts_ur_super_session *ss, stun_tid *tid,
  1850. int *err_code, const u08bits **reason, u16bits *unknown_attrs, u16bits *ua_num,
  1851. ioa_net_data *in_buffer) {
  1852. FUNCSTART;
  1853. ioa_addr peer_addr;
  1854. int peer_found = 0;
  1855. addr_set_any(&peer_addr);
  1856. allocation* a = get_allocation_ss(ss);
  1857. if(!(ss->is_tcp_relay)) {
  1858. *err_code = 403;
  1859. *reason = (const u08bits *)"Connect cannot be used with UDP relay";
  1860. } else if (!is_allocation_valid(a)) {
  1861. *err_code = 437;
  1862. } else {
  1863. stun_attr_ref sar = stun_attr_get_first_str(ioa_network_buffer_data(in_buffer->nbh),
  1864. ioa_network_buffer_get_size(in_buffer->nbh));
  1865. while (sar && (!(*err_code)) && (*ua_num < MAX_NUMBER_OF_UNKNOWN_ATTRS)) {
  1866. int attr_type = stun_attr_get_type(sar);
  1867. switch (attr_type) {
  1868. SKIP_ATTRIBUTES;
  1869. case STUN_ATTRIBUTE_XOR_PEER_ADDRESS:
  1870. {
  1871. if(stun_attr_get_addr_str(ioa_network_buffer_data(in_buffer->nbh),
  1872. ioa_network_buffer_get_size(in_buffer->nbh),
  1873. sar, &peer_addr,
  1874. NULL) == -1) {
  1875. *err_code = 400;
  1876. *reason = (const u08bits *)"Bad Peer Address";
  1877. } else {
  1878. if(!get_relay_socket(a,peer_addr.ss.sa_family)) {
  1879. *err_code = 443;
  1880. *reason = (const u08bits *)"Peer Address Family Mismatch (2)";
  1881. }
  1882. peer_found = 1;
  1883. }
  1884. break;
  1885. }
  1886. default:
  1887. if(attr_type>=0x0000 && attr_type<=0x7FFF)
  1888. unknown_attrs[(*ua_num)++] = nswap16(attr_type);
  1889. };
  1890. sar = stun_attr_get_next_str(ioa_network_buffer_data(in_buffer->nbh),
  1891. ioa_network_buffer_get_size(in_buffer->nbh),
  1892. sar);
  1893. }
  1894. if (*ua_num > 0) {
  1895. *err_code = 420;
  1896. } else if (*err_code) {
  1897. ;
  1898. } else if (!peer_found) {
  1899. *err_code = 400;
  1900. *reason = (const u08bits *)"Where is Peer Address ?";
  1901. } else {
  1902. if(!good_peer_addr(server,ss->realm_options.name,&peer_addr)) {
  1903. *err_code = 403;
  1904. *reason = (const u08bits *) "Forbidden IP";
  1905. } else {
  1906. tcp_start_connection_to_peer(server, ss, tid, a, &peer_addr, err_code, reason);
  1907. }
  1908. }
  1909. }
  1910. FUNCEND;
  1911. return 0;
  1912. }
  1913. static int handle_turn_connection_bind(turn_turnserver *server,
  1914. ts_ur_super_session *ss, stun_tid *tid, int *resp_constructed,
  1915. int *err_code, const u08bits **reason, u16bits *unknown_attrs, u16bits *ua_num,
  1916. ioa_net_data *in_buffer, ioa_network_buffer_handle nbh, int message_integrity,
  1917. int can_resume) {
  1918. allocation* a = get_allocation_ss(ss);
  1919. u16bits method = STUN_METHOD_CONNECTION_BIND;
  1920. if(ss->to_be_closed) {
  1921. *err_code = 400;
  1922. } else if (is_allocation_valid(a)) {
  1923. *err_code = 400;
  1924. *reason = (const u08bits *)"Bad request: CONNECTION_BIND cannot be issued after allocation";
  1925. } else if(!is_stream_socket(get_ioa_socket_type(ss->client_socket))) {
  1926. *err_code = 400;
  1927. *reason = (const u08bits *)"Bad request: CONNECTION_BIND only possible with TCP/TLS";
  1928. } else {
  1929. tcp_connection_id id = 0;
  1930. stun_attr_ref sar = stun_attr_get_first_str(ioa_network_buffer_data(in_buffer->nbh),
  1931. ioa_network_buffer_get_size(in_buffer->nbh));
  1932. while (sar && (!(*err_code)) && (*ua_num < MAX_NUMBER_OF_UNKNOWN_ATTRS)) {
  1933. int attr_type = stun_attr_get_type(sar);
  1934. switch (attr_type) {
  1935. SKIP_ATTRIBUTES;
  1936. case STUN_ATTRIBUTE_CONNECTION_ID: {
  1937. if (stun_attr_get_len(sar) != 4) {
  1938. *err_code = 400;
  1939. *reason = (const u08bits *)"Wrong Connection ID field format";
  1940. } else {
  1941. const u08bits* value = stun_attr_get_value(sar);
  1942. if (!value) {
  1943. *err_code = 400;
  1944. *reason = (const u08bits *)"Wrong Connection ID field data";
  1945. } else {
  1946. id = *((const u32bits*)value); //AS-IS encoding, no conversion to/from network byte order
  1947. }
  1948. }
  1949. }
  1950. break;
  1951. default:
  1952. if(attr_type>=0x0000 && attr_type<=0x7FFF)
  1953. unknown_attrs[(*ua_num)++] = nswap16(attr_type);
  1954. };
  1955. sar = stun_attr_get_next_str(ioa_network_buffer_data(in_buffer->nbh),
  1956. ioa_network_buffer_get_size(in_buffer->nbh), sar);
  1957. }
  1958. if (*ua_num > 0) {
  1959. *err_code = 420;
  1960. } else if (*err_code) {
  1961. ;
  1962. } else {
  1963. if(server->send_socket_to_relay) {
  1964. turnserver_id sid = (id & 0xFF000000)>>24;
  1965. ioa_socket_handle s = ss->client_socket;
  1966. if(s && !ioa_socket_tobeclosed(s)) {
  1967. ioa_socket_handle new_s = detach_ioa_socket(s);
  1968. if(new_s) {
  1969. if(server->send_socket_to_relay(sid, id, tid, new_s, message_integrity, RMT_CB_SOCKET, in_buffer, can_resume)<0) {
  1970. *err_code = 400;
  1971. *reason = (const u08bits *)"Wrong connection id";
  1972. }
  1973. } else {
  1974. *err_code = 500;
  1975. }
  1976. } else {
  1977. *err_code = 500;
  1978. }
  1979. } else {
  1980. *err_code = 500;
  1981. }
  1982. ss->to_be_closed = 1;
  1983. }
  1984. }
  1985. if (!(*resp_constructed) && ss->client_socket && !ioa_socket_tobeclosed(ss->client_socket)) {
  1986. if (!(*err_code)) {
  1987. *err_code = 437;
  1988. }
  1989. size_t len = ioa_network_buffer_get_size(nbh);
  1990. stun_init_error_response_str(method, ioa_network_buffer_data(nbh), &len, *err_code, *reason, tid);
  1991. ioa_network_buffer_set_size(nbh,len);
  1992. *resp_constructed = 1;
  1993. }
  1994. return 0;
  1995. }
  1996. int turnserver_accept_tcp_client_data_connection(turn_turnserver *server, tcp_connection_id tcid, stun_tid *tid, ioa_socket_handle s, int message_integrity, ioa_net_data *in_buffer, int can_resume)
  1997. {
  1998. if(!server)
  1999. return -1;
  2000. FUNCSTART;
  2001. tcp_connection *tc = NULL;
  2002. ts_ur_super_session *ss = NULL;
  2003. int err_code = 0;
  2004. const u08bits *reason = NULL;
  2005. ioa_socket_handle s_to_delete = s;
  2006. if(tcid && tid && s) {
  2007. tc = get_tcp_connection_by_id(server->tcp_relay_connections, tcid);
  2008. ioa_network_buffer_handle nbh = ioa_network_buffer_allocate(server->e);
  2009. int resp_constructed = 0;
  2010. if(!tc || (tc->state == TC_STATE_READY) || (tc->client_s)) {
  2011. err_code = 400;
  2012. } else {
  2013. allocation *a = (allocation*)(tc->owner);
  2014. if(!a || !(a->owner)) {
  2015. err_code = 500;
  2016. } else {
  2017. ss = (ts_ur_super_session*)(a->owner);
  2018. if(ss->to_be_closed || ioa_socket_tobeclosed(ss->client_socket)) {
  2019. err_code = 404;
  2020. } else {
  2021. //Check security:
  2022. int postpone_reply = 0;
  2023. check_stun_auth(server, ss, tid, &resp_constructed, &err_code, &reason, in_buffer, nbh,
  2024. STUN_METHOD_CONNECTION_BIND, &message_integrity, &postpone_reply, can_resume);
  2025. if(postpone_reply) {
  2026. ioa_network_buffer_delete(server->e, nbh);
  2027. return 0;
  2028. } else if(!err_code) {
  2029. tc->state = TC_STATE_READY;
  2030. tc->client_s = s;
  2031. s_to_delete = NULL;
  2032. set_ioa_socket_session(s,ss);
  2033. set_ioa_socket_sub_session(s,tc);
  2034. set_ioa_socket_app_type(s,TCP_CLIENT_DATA_SOCKET);
  2035. if(register_callback_on_ioa_socket(server->e, s, IOA_EV_READ, tcp_client_input_handler_rfc6062data, tc, 1)<0) {
  2036. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "%s: cannot set TCP client data input callback\n", __FUNCTION__);
  2037. err_code = 500;
  2038. } else {
  2039. IOA_EVENT_DEL(tc->conn_bind_timeout);
  2040. }
  2041. }
  2042. }
  2043. }
  2044. }
  2045. if(tc)
  2046. get_and_clean_tcp_connection_by_id(server->tcp_relay_connections, tcid);
  2047. if(!resp_constructed) {
  2048. if(!err_code) {
  2049. size_t len = ioa_network_buffer_get_size(nbh);
  2050. stun_init_success_response_str(STUN_METHOD_CONNECTION_BIND, ioa_network_buffer_data(nbh), &len, tid);
  2051. ioa_network_buffer_set_size(nbh,len);
  2052. } else {
  2053. size_t len = ioa_network_buffer_get_size(nbh);
  2054. stun_init_error_response_str(STUN_METHOD_CONNECTION_BIND, ioa_network_buffer_data(nbh), &len, err_code, NULL, tid);
  2055. ioa_network_buffer_set_size(nbh,len);
  2056. }
  2057. }
  2058. {
  2059. static const u08bits *field = (const u08bits *) TURN_SOFTWARE;
  2060. static const size_t fsz = sizeof(TURN_SOFTWARE)-1;
  2061. size_t len = ioa_network_buffer_get_size(nbh);
  2062. stun_attr_add_str(ioa_network_buffer_data(nbh), &len, STUN_ATTRIBUTE_SOFTWARE, field, fsz);
  2063. ioa_network_buffer_set_size(nbh, len);
  2064. }
  2065. if(message_integrity && ss) {
  2066. size_t len = ioa_network_buffer_get_size(nbh);
  2067. stun_attr_add_integrity_str(server->ct,ioa_network_buffer_data(nbh),&len,ss->hmackey,ss->pwd,SHATYPE_DEFAULT);
  2068. ioa_network_buffer_set_size(nbh,len);
  2069. }
  2070. if ((server->fingerprint) || (ss &&(ss->enforce_fingerprints))) {
  2071. size_t len = ioa_network_buffer_get_size(nbh);
  2072. stun_attr_add_fingerprint_str(ioa_network_buffer_data(nbh), &len);
  2073. ioa_network_buffer_set_size(nbh, len);
  2074. }
  2075. if(server->verbose) {
  2076. log_method(ss, "CONNECTION_BIND", err_code, reason);
  2077. }
  2078. if(ss && !err_code) {
  2079. send_data_from_ioa_socket_nbh(s, NULL, nbh, TTL_IGNORE, TOS_IGNORE, NULL);
  2080. tcp_deliver_delayed_buffer(&(tc->ub_to_client),s,ss);
  2081. IOA_CLOSE_SOCKET(s_to_delete);
  2082. FUNCEND;
  2083. return 0;
  2084. } else {
  2085. /* Just to set the necessary structures for the packet sending: */
  2086. if(register_callback_on_ioa_socket(server->e, s, IOA_EV_READ, NULL, NULL, 1)<0) {
  2087. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "%s: cannot set TCP tmp client data input callback\n", __FUNCTION__);
  2088. ioa_network_buffer_delete(server->e, nbh);
  2089. } else {
  2090. send_data_from_ioa_socket_nbh(s, NULL, nbh, TTL_IGNORE, TOS_IGNORE, NULL);
  2091. }
  2092. }
  2093. }
  2094. IOA_CLOSE_SOCKET(s_to_delete);
  2095. FUNCEND;
  2096. return -1;
  2097. }
  2098. /* <<== RFC 6062 */
  2099. static int handle_turn_channel_bind(turn_turnserver *server,
  2100. ts_ur_super_session *ss, stun_tid *tid, int *resp_constructed,
  2101. int *err_code, const u08bits **reason, u16bits *unknown_attrs, u16bits *ua_num,
  2102. ioa_net_data *in_buffer, ioa_network_buffer_handle nbh) {
  2103. FUNCSTART;
  2104. u16bits chnum = 0;
  2105. ioa_addr peer_addr;
  2106. addr_set_any(&peer_addr);
  2107. allocation* a = get_allocation_ss(ss);
  2108. int addr_found = 0;
  2109. if(ss->is_tcp_relay) {
  2110. *err_code = 403;
  2111. *reason = (const u08bits *)"Channel bind cannot be used with TCP relay";
  2112. } else if (is_allocation_valid(a)) {
  2113. stun_attr_ref sar = stun_attr_get_first_str(ioa_network_buffer_data(in_buffer->nbh),
  2114. ioa_network_buffer_get_size(in_buffer->nbh));
  2115. while (sar && (!(*err_code)) && (*ua_num < MAX_NUMBER_OF_UNKNOWN_ATTRS)) {
  2116. int attr_type = stun_attr_get_type(sar);
  2117. switch (attr_type) {
  2118. SKIP_ATTRIBUTES;
  2119. case STUN_ATTRIBUTE_CHANNEL_NUMBER: {
  2120. if (chnum) {
  2121. chnum = 0;
  2122. *err_code = 400;
  2123. *reason = (const u08bits *)"Channel number cannot be duplicated in this request";
  2124. break;
  2125. }
  2126. chnum = stun_attr_get_channel_number(sar);
  2127. if (!chnum) {
  2128. *err_code = 400;
  2129. *reason = (const u08bits *)"Channel number cannot be zero in this request";
  2130. break;
  2131. }
  2132. }
  2133. break;
  2134. case STUN_ATTRIBUTE_XOR_PEER_ADDRESS:
  2135. {
  2136. stun_attr_get_addr_str(ioa_network_buffer_data(in_buffer->nbh),
  2137. ioa_network_buffer_get_size(in_buffer->nbh),
  2138. sar, &peer_addr,
  2139. NULL);
  2140. if(!get_relay_socket(a,peer_addr.ss.sa_family)) {
  2141. *err_code = 443;
  2142. *reason = (const u08bits *)"Peer Address Family Mismatch (3)";
  2143. }
  2144. if(addr_get_port(&peer_addr) < 1) {
  2145. *err_code = 400;
  2146. *reason = (const u08bits *)"Empty port number in channel bind request";
  2147. } else {
  2148. addr_found = 1;
  2149. }
  2150. break;
  2151. }
  2152. default:
  2153. if(attr_type>=0x0000 && attr_type<=0x7FFF)
  2154. unknown_attrs[(*ua_num)++] = nswap16(attr_type);
  2155. };
  2156. sar = stun_attr_get_next_str(ioa_network_buffer_data(in_buffer->nbh),
  2157. ioa_network_buffer_get_size(in_buffer->nbh),
  2158. sar);
  2159. }
  2160. if (*ua_num > 0) {
  2161. *err_code = 420;
  2162. } else if (*err_code) {
  2163. ;
  2164. } else if (!chnum || addr_any(&peer_addr) || !addr_found) {
  2165. *err_code = 400;
  2166. *reason = (const u08bits *)"Bad channel bind request";
  2167. } else if(!STUN_VALID_CHANNEL(chnum)) {
  2168. *err_code = 400;
  2169. *reason = (const u08bits *)"Bad channel number";
  2170. } else {
  2171. ch_info* chn = allocation_get_ch_info(a, chnum);
  2172. turn_permission_info* tinfo = NULL;
  2173. if (chn) {
  2174. if (!addr_eq(&peer_addr, &(chn->peer_addr))) {
  2175. *err_code = 400;
  2176. *reason = (const u08bits *)"You cannot use the same channel number with different peer";
  2177. } else {
  2178. tinfo = (turn_permission_info*) (chn->owner);
  2179. if (!tinfo) {
  2180. *err_code = 500;
  2181. *reason = (const u08bits *)"Wrong permission info";
  2182. } else {
  2183. if (!addr_eq_no_port(&peer_addr, &(tinfo->addr))) {
  2184. *err_code = 500;
  2185. *reason = (const u08bits *)"Wrong permission info and peer addr conbination";
  2186. } else if (chn->port != addr_get_port(&peer_addr)) {
  2187. *err_code = 500;
  2188. *reason = (const u08bits *)"Wrong port number";
  2189. }
  2190. }
  2191. }
  2192. } else {
  2193. chn = allocation_get_ch_info_by_peer_addr(a, &peer_addr);
  2194. if(chn) {
  2195. *err_code = 400;
  2196. *reason = (const u08bits *)"You cannot use the same peer with different channel number";
  2197. } else {
  2198. if(!good_peer_addr(server,ss->realm_options.name,&peer_addr)) {
  2199. *err_code = 403;
  2200. *reason = (const u08bits *) "Forbidden IP";
  2201. } else {
  2202. chn = allocation_get_new_ch_info(a, chnum, &peer_addr);
  2203. if (!chn) {
  2204. *err_code = 500;
  2205. *reason = (const u08bits *) "Cannot find channel data";
  2206. } else {
  2207. tinfo = (turn_permission_info*) (chn->owner);
  2208. if (!tinfo) {
  2209. *err_code = 500;
  2210. *reason
  2211. = (const u08bits *) "Wrong turn permission info";
  2212. }
  2213. }
  2214. }
  2215. }
  2216. }
  2217. if (!(*err_code) && chn && tinfo) {
  2218. if (update_channel_lifetime(ss,chn) < 0) {
  2219. *err_code = 500;
  2220. *reason = (const u08bits *)"Cannot update channel lifetime (internal error)";
  2221. } else {
  2222. size_t len = ioa_network_buffer_get_size(nbh);
  2223. stun_set_channel_bind_response_str(ioa_network_buffer_data(nbh), &len, tid, 0, NULL);
  2224. ioa_network_buffer_set_size(nbh,len);
  2225. *resp_constructed = 1;
  2226. if(!(ss->is_mobile)) {
  2227. if(get_ioa_socket_type(ss->client_socket) == UDP_SOCKET ||
  2228. get_ioa_socket_type(ss->client_socket) == TCP_SOCKET ||
  2229. get_ioa_socket_type(ss->client_socket) == SCTP_SOCKET) {
  2230. if(get_ioa_socket_type(get_relay_socket(&(ss->alloc),peer_addr.ss.sa_family)) == UDP_SOCKET) {
  2231. chn->kernel_channel = CREATE_TURN_CHANNEL_KERNEL(chn->chnum,
  2232. get_ioa_socket_address_family(ss->client_socket),
  2233. peer_addr.ss.sa_family,
  2234. (get_ioa_socket_type(ss->client_socket)==UDP_SOCKET ? IPPROTO_UDP : IPPROTO_TCP),
  2235. &(get_remote_addr_from_ioa_socket(ss->client_socket)->ss),
  2236. &(get_local_addr_from_ioa_socket(ss->client_socket)->ss),
  2237. &(get_local_addr_from_ioa_socket(get_relay_socket(&(ss->alloc),peer_addr.ss.sa_family))),
  2238. &(get_remote_addr_from_ioa_socket(get_relay_socket(&(ss->alloc),peer_addr.ss.sa_family)))
  2239. );
  2240. }
  2241. }
  2242. }
  2243. }
  2244. }
  2245. }
  2246. }
  2247. FUNCEND;
  2248. return 0;
  2249. }
  2250. static int handle_turn_binding(turn_turnserver *server,
  2251. ts_ur_super_session *ss, stun_tid *tid, int *resp_constructed,
  2252. int *err_code, const u08bits **reason, u16bits *unknown_attrs, u16bits *ua_num,
  2253. ioa_net_data *in_buffer, ioa_network_buffer_handle nbh,
  2254. int *origin_changed, ioa_addr *response_origin,
  2255. int *dest_changed, ioa_addr *response_destination,
  2256. u32bits cookie, int old_stun) {
  2257. FUNCSTART;
  2258. int change_ip = 0;
  2259. int change_port = 0;
  2260. int padding = 0;
  2261. int response_port_present = 0;
  2262. u16bits response_port = 0;
  2263. SOCKET_TYPE st = get_ioa_socket_type(ss->client_socket);
  2264. int use_reflected_from = 0;
  2265. if(!(ss->client_socket))
  2266. return -1;
  2267. *origin_changed = 0;
  2268. *dest_changed = 0;
  2269. stun_attr_ref sar = stun_attr_get_first_str(ioa_network_buffer_data(in_buffer->nbh),
  2270. ioa_network_buffer_get_size(in_buffer->nbh));
  2271. while (sar && (!(*err_code)) && (*ua_num < MAX_NUMBER_OF_UNKNOWN_ATTRS)) {
  2272. int attr_type = stun_attr_get_type(sar);
  2273. switch (attr_type) {
  2274. case OLD_STUN_ATTRIBUTE_PASSWORD:
  2275. SKIP_ATTRIBUTES;
  2276. case STUN_ATTRIBUTE_CHANGE_REQUEST:
  2277. /*
  2278. * This fix allows the client program from the Stuntman source to make STUN binding requests
  2279. * to this server.
  2280. *
  2281. * It was provided by John Selbie, from STUNTMAN project:
  2282. *
  2283. * "Here's the gist of the change. Stuntman comes with a STUN client library
  2284. * and client program. The client program displays the mapped IP address and
  2285. * port if it gets back a successful binding response.
  2286. * It also interops with JSTUN, a Java implementation of STUN.
  2287. * However, the JSTUN server refuses to respond to any binding request that
  2288. * doesn't have a CHANGE-REQUEST attribute in it.
  2289. * ... workaround is for the client to make a request with an empty CHANGE-REQUEST
  2290. * attribute (neither the ip or port bit are set)."
  2291. *
  2292. */
  2293. stun_attr_get_change_request_str(sar, &change_ip, &change_port);
  2294. if( (!is_rfc5780(server)) && (change_ip || change_port)) {
  2295. *err_code = 420;
  2296. *reason = (const u08bits *)"Unknown attribute: TURN server was configured without RFC 5780 support";
  2297. break;
  2298. }
  2299. if(change_ip || change_port) {
  2300. if(st != UDP_SOCKET) {
  2301. *err_code = 400;
  2302. *reason = (const u08bits *)"Wrong request: applicable only to UDP protocol";
  2303. }
  2304. }
  2305. break;
  2306. case STUN_ATTRIBUTE_PADDING:
  2307. if(response_port_present) {
  2308. *err_code = 400;
  2309. *reason = (const u08bits *)"Wrong request format: you cannot use PADDING and RESPONSE_PORT together";
  2310. } else if((st != UDP_SOCKET) && (st != DTLS_SOCKET)) {
  2311. *err_code = 400;
  2312. *reason = (const u08bits *)"Wrong request: padding applicable only to UDP and DTLS protocols";
  2313. } else {
  2314. padding = 1;
  2315. }
  2316. break;
  2317. case STUN_ATTRIBUTE_RESPONSE_PORT:
  2318. if(padding) {
  2319. *err_code = 400;
  2320. *reason = (const u08bits *)"Wrong request format: you cannot use PADDING and RESPONSE_PORT together";
  2321. } else if(st != UDP_SOCKET) {
  2322. *err_code = 400;
  2323. *reason = (const u08bits *)"Wrong request: applicable only to UDP protocol";
  2324. } else {
  2325. int rp = stun_attr_get_response_port_str(sar);
  2326. if(rp>=0) {
  2327. response_port_present = 1;
  2328. response_port = (u16bits)rp;
  2329. } else {
  2330. *err_code = 400;
  2331. *reason = (const u08bits *)"Wrong response port format";
  2332. }
  2333. }
  2334. break;
  2335. case OLD_STUN_ATTRIBUTE_RESPONSE_ADDRESS:
  2336. if(old_stun) {
  2337. use_reflected_from = 1;
  2338. stun_attr_get_addr_str(ioa_network_buffer_data(in_buffer->nbh),
  2339. ioa_network_buffer_get_size(in_buffer->nbh),
  2340. sar, response_destination, response_destination);
  2341. }
  2342. break;
  2343. default:
  2344. if(attr_type>=0x0000 && attr_type<=0x7FFF)
  2345. unknown_attrs[(*ua_num)++] = nswap16(attr_type);
  2346. };
  2347. sar = stun_attr_get_next_str(ioa_network_buffer_data(in_buffer->nbh),
  2348. ioa_network_buffer_get_size(in_buffer->nbh),
  2349. sar);
  2350. }
  2351. if (*ua_num > 0) {
  2352. *err_code = 420;
  2353. } else if (*err_code) {
  2354. ;
  2355. } else if(ss->client_socket && get_remote_addr_from_ioa_socket(ss->client_socket)) {
  2356. size_t len = ioa_network_buffer_get_size(nbh);
  2357. if (stun_set_binding_response_str(ioa_network_buffer_data(nbh), &len, tid,
  2358. get_remote_addr_from_ioa_socket(ss->client_socket), 0, NULL, cookie, old_stun) >= 0) {
  2359. addr_cpy(response_origin, get_local_addr_from_ioa_socket(ss->client_socket));
  2360. *resp_constructed = 1;
  2361. if(old_stun && use_reflected_from) {
  2362. stun_attr_add_addr_str(ioa_network_buffer_data(nbh), &len,
  2363. OLD_STUN_ATTRIBUTE_REFLECTED_FROM,
  2364. get_remote_addr_from_ioa_socket(ss->client_socket));
  2365. }
  2366. if(!is_rfc5780(server)) {
  2367. if(old_stun) {
  2368. stun_attr_add_addr_str(ioa_network_buffer_data(nbh), &len,
  2369. OLD_STUN_ATTRIBUTE_SOURCE_ADDRESS, response_origin);
  2370. stun_attr_add_addr_str(ioa_network_buffer_data(nbh), &len,
  2371. OLD_STUN_ATTRIBUTE_CHANGED_ADDRESS, response_origin);
  2372. } else {
  2373. stun_attr_add_addr_str(ioa_network_buffer_data(nbh), &len,
  2374. STUN_ATTRIBUTE_RESPONSE_ORIGIN, response_origin);
  2375. }
  2376. } else if(ss->client_socket) {
  2377. ioa_addr other_address;
  2378. if(get_other_address(server,ss,&other_address) == 0) {
  2379. addr_cpy(response_destination, get_remote_addr_from_ioa_socket(ss->client_socket));
  2380. if(change_ip) {
  2381. *origin_changed = 1;
  2382. if(change_port) {
  2383. addr_cpy(response_origin,&other_address);
  2384. } else {
  2385. int old_port = addr_get_port(response_origin);
  2386. addr_cpy(response_origin,&other_address);
  2387. addr_set_port(response_origin,old_port);
  2388. }
  2389. } else if(change_port) {
  2390. *origin_changed = 1;
  2391. addr_set_port(response_origin,addr_get_port(&other_address));
  2392. }
  2393. if(old_stun) {
  2394. stun_attr_add_addr_str(ioa_network_buffer_data(nbh), &len,
  2395. OLD_STUN_ATTRIBUTE_SOURCE_ADDRESS, response_origin);
  2396. stun_attr_add_addr_str(ioa_network_buffer_data(nbh), &len,
  2397. OLD_STUN_ATTRIBUTE_CHANGED_ADDRESS, &other_address);
  2398. } else {
  2399. stun_attr_add_addr_str(ioa_network_buffer_data(nbh), &len,
  2400. STUN_ATTRIBUTE_RESPONSE_ORIGIN, response_origin);
  2401. stun_attr_add_addr_str(ioa_network_buffer_data(nbh), &len,
  2402. STUN_ATTRIBUTE_OTHER_ADDRESS, &other_address);
  2403. }
  2404. if(response_port_present) {
  2405. *dest_changed = 1;
  2406. addr_set_port(response_destination, (int)response_port);
  2407. }
  2408. if(padding) {
  2409. int mtu = get_local_mtu_ioa_socket(ss->client_socket);
  2410. if(mtu<68)
  2411. mtu=1500;
  2412. mtu = (mtu >> 2) << 2;
  2413. stun_attr_add_padding_str(ioa_network_buffer_data(nbh), &len, (u16bits)mtu);
  2414. }
  2415. }
  2416. }
  2417. }
  2418. ioa_network_buffer_set_size(nbh, len);
  2419. }
  2420. FUNCEND;
  2421. return 0;
  2422. }
  2423. static int handle_turn_send(turn_turnserver *server, ts_ur_super_session *ss,
  2424. int *err_code, const u08bits **reason, u16bits *unknown_attrs, u16bits *ua_num,
  2425. ioa_net_data *in_buffer) {
  2426. FUNCSTART;
  2427. ioa_addr peer_addr;
  2428. const u08bits* value = NULL;
  2429. int len = -1;
  2430. int addr_found = 0;
  2431. int set_df = 0;
  2432. addr_set_any(&peer_addr);
  2433. allocation* a = get_allocation_ss(ss);
  2434. if(ss->is_tcp_relay) {
  2435. *err_code = 403;
  2436. *reason = (const u08bits *)"Send cannot be used with TCP relay";
  2437. } else if (is_allocation_valid(a) && (in_buffer->recv_ttl != 0)) {
  2438. stun_attr_ref sar = stun_attr_get_first_str(ioa_network_buffer_data(in_buffer->nbh),
  2439. ioa_network_buffer_get_size(in_buffer->nbh));
  2440. while (sar && (!(*err_code)) && (*ua_num < MAX_NUMBER_OF_UNKNOWN_ATTRS)) {
  2441. int attr_type = stun_attr_get_type(sar);
  2442. switch (attr_type) {
  2443. SKIP_ATTRIBUTES;
  2444. case STUN_ATTRIBUTE_DONT_FRAGMENT:
  2445. if(!(server->dont_fragment))
  2446. unknown_attrs[(*ua_num)++] = nswap16(attr_type);
  2447. else
  2448. set_df = 1;
  2449. break;
  2450. case STUN_ATTRIBUTE_XOR_PEER_ADDRESS: {
  2451. if (addr_found) {
  2452. *err_code = 400;
  2453. *reason = (const u08bits *)"Address duplication";
  2454. } else {
  2455. stun_attr_get_addr_str(ioa_network_buffer_data(in_buffer->nbh),
  2456. ioa_network_buffer_get_size(in_buffer->nbh),
  2457. sar, &peer_addr,
  2458. NULL);
  2459. }
  2460. }
  2461. break;
  2462. case STUN_ATTRIBUTE_DATA: {
  2463. if (len >= 0) {
  2464. *err_code = 400;
  2465. *reason = (const u08bits *)"Data duplication";
  2466. } else {
  2467. len = stun_attr_get_len(sar);
  2468. value = stun_attr_get_value(sar);
  2469. }
  2470. }
  2471. break;
  2472. default:
  2473. if(attr_type>=0x0000 && attr_type<=0x7FFF)
  2474. unknown_attrs[(*ua_num)++] = nswap16(attr_type);
  2475. };
  2476. sar = stun_attr_get_next_str(ioa_network_buffer_data(in_buffer->nbh),
  2477. ioa_network_buffer_get_size(in_buffer->nbh),
  2478. sar);
  2479. }
  2480. if (*err_code) {
  2481. ;
  2482. } else if (*ua_num > 0) {
  2483. *err_code = 420;
  2484. } else if (!addr_any(&peer_addr) && len >= 0) {
  2485. turn_permission_info* tinfo = NULL;
  2486. if(!(server->server_relay))
  2487. tinfo = allocation_get_permission(a, &peer_addr);
  2488. if (tinfo || (server->server_relay)) {
  2489. set_df_on_ioa_socket(get_relay_socket_ss(ss,peer_addr.ss.sa_family), set_df);
  2490. ioa_network_buffer_handle nbh = in_buffer->nbh;
  2491. if(value && len>0) {
  2492. u16bits offset = (u16bits)(value - ioa_network_buffer_data(nbh));
  2493. ioa_network_buffer_add_offset_size(nbh,offset,0,len);
  2494. } else {
  2495. len = 0;
  2496. ioa_network_buffer_set_size(nbh,len);
  2497. }
  2498. ioa_network_buffer_header_init(nbh);
  2499. send_data_from_ioa_socket_nbh(get_relay_socket_ss(ss,peer_addr.ss.sa_family), &peer_addr, nbh, in_buffer->recv_ttl-1, in_buffer->recv_tos, NULL);
  2500. in_buffer->nbh = NULL;
  2501. }
  2502. } else {
  2503. *err_code = 400;
  2504. *reason = (const u08bits *)"No address found";
  2505. }
  2506. }
  2507. FUNCEND;
  2508. return 0;
  2509. }
  2510. static int update_permission(ts_ur_super_session *ss, ioa_addr *peer_addr) {
  2511. if (!ss || !peer_addr)
  2512. return -1;
  2513. allocation* a = get_allocation_ss(ss);
  2514. turn_permission_info* tinfo = allocation_get_permission(a, peer_addr);
  2515. if (!tinfo) {
  2516. tinfo = allocation_add_permission(a, peer_addr);
  2517. }
  2518. if (!tinfo)
  2519. return -1;
  2520. if (update_turn_permission_lifetime(ss, tinfo, 0) < 0)
  2521. return -1;
  2522. ch_info *chn = get_turn_channel(tinfo, peer_addr);
  2523. if(chn) {
  2524. if (update_channel_lifetime(ss, chn) < 0)
  2525. return -1;
  2526. }
  2527. return 0;
  2528. }
  2529. static int handle_turn_create_permission(turn_turnserver *server,
  2530. ts_ur_super_session *ss, stun_tid *tid, int *resp_constructed,
  2531. int *err_code, const u08bits **reason, u16bits *unknown_attrs, u16bits *ua_num,
  2532. ioa_net_data *in_buffer, ioa_network_buffer_handle nbh) {
  2533. int ret = -1;
  2534. int addr_found = 0;
  2535. UNUSED_ARG(server);
  2536. allocation* a = get_allocation_ss(ss);
  2537. if (is_allocation_valid(a)) {
  2538. {
  2539. stun_attr_ref sar = stun_attr_get_first_str(ioa_network_buffer_data(in_buffer->nbh),
  2540. ioa_network_buffer_get_size(in_buffer->nbh));
  2541. while (sar && (!(*err_code)) && (*ua_num < MAX_NUMBER_OF_UNKNOWN_ATTRS)) {
  2542. int attr_type = stun_attr_get_type(sar);
  2543. switch (attr_type) {
  2544. SKIP_ATTRIBUTES;
  2545. case STUN_ATTRIBUTE_XOR_PEER_ADDRESS: {
  2546. ioa_addr peer_addr;
  2547. addr_set_any(&peer_addr);
  2548. stun_attr_get_addr_str(ioa_network_buffer_data(in_buffer->nbh),
  2549. ioa_network_buffer_get_size(in_buffer->nbh),
  2550. sar, &peer_addr,
  2551. NULL);
  2552. if(!get_relay_socket(a,peer_addr.ss.sa_family)) {
  2553. *err_code = 443;
  2554. *reason = (const u08bits *)"Peer Address Family Mismatch (4)";
  2555. } else if(!good_peer_addr(server, ss->realm_options.name, &peer_addr)) {
  2556. *err_code = 403;
  2557. *reason = (const u08bits *) "Forbidden IP";
  2558. } else {
  2559. addr_found++;
  2560. }
  2561. }
  2562. break;
  2563. default:
  2564. if(attr_type>=0x0000 && attr_type<=0x7FFF)
  2565. unknown_attrs[(*ua_num)++] = nswap16(attr_type);
  2566. };
  2567. sar = stun_attr_get_next_str(ioa_network_buffer_data(in_buffer->nbh),
  2568. ioa_network_buffer_get_size(in_buffer->nbh),
  2569. sar);
  2570. }
  2571. }
  2572. if (*ua_num > 0) {
  2573. *err_code = 420;
  2574. } else if (*err_code) {
  2575. ;
  2576. } else if (!addr_found) {
  2577. *err_code = 400;
  2578. *reason = (const u08bits *)"No address found";
  2579. } else {
  2580. stun_attr_ref sar = stun_attr_get_first_str(ioa_network_buffer_data(in_buffer->nbh),
  2581. ioa_network_buffer_get_size(in_buffer->nbh));
  2582. while (sar) {
  2583. int attr_type = stun_attr_get_type(sar);
  2584. switch (attr_type) {
  2585. SKIP_ATTRIBUTES;
  2586. case STUN_ATTRIBUTE_XOR_PEER_ADDRESS: {
  2587. ioa_addr peer_addr;
  2588. addr_set_any(&peer_addr);
  2589. stun_attr_get_addr_str(ioa_network_buffer_data(in_buffer->nbh),
  2590. ioa_network_buffer_get_size(in_buffer->nbh),
  2591. sar, &peer_addr,
  2592. NULL);
  2593. addr_set_port(&peer_addr, 0);
  2594. if (update_permission(ss, &peer_addr) < 0) {
  2595. *err_code = 500;
  2596. *reason = (const u08bits *)"Cannot update some permissions (critical server software error)";
  2597. }
  2598. }
  2599. break;
  2600. default:
  2601. ;
  2602. }
  2603. sar = stun_attr_get_next_str(ioa_network_buffer_data(in_buffer->nbh),
  2604. ioa_network_buffer_get_size(in_buffer->nbh),
  2605. sar);
  2606. }
  2607. if(*err_code == 0) {
  2608. size_t len = ioa_network_buffer_get_size(nbh);
  2609. stun_init_success_response_str(STUN_METHOD_CREATE_PERMISSION,
  2610. ioa_network_buffer_data(nbh), &len, tid);
  2611. ioa_network_buffer_set_size(nbh,len);
  2612. ret = 0;
  2613. *resp_constructed = 1;
  2614. }
  2615. }
  2616. }
  2617. return ret;
  2618. }
  2619. // AUTH ==>>
  2620. static int need_stun_authentication(turn_turnserver *server, ts_ur_super_session *ss)
  2621. {
  2622. UNUSED_ARG(ss);
  2623. if(server) {
  2624. switch(server->ct) {
  2625. case TURN_CREDENTIALS_LONG_TERM:
  2626. return 1;
  2627. default:
  2628. ;
  2629. };
  2630. }
  2631. return 0;
  2632. }
  2633. static int create_challenge_response(ts_ur_super_session *ss, stun_tid *tid, int *resp_constructed,
  2634. int *err_code, const u08bits **reason,
  2635. ioa_network_buffer_handle nbh,
  2636. u16bits method)
  2637. {
  2638. size_t len = ioa_network_buffer_get_size(nbh);
  2639. stun_init_error_response_str(method, ioa_network_buffer_data(nbh), &len, *err_code, *reason, tid);
  2640. *resp_constructed = 1;
  2641. stun_attr_add_str(ioa_network_buffer_data(nbh), &len, STUN_ATTRIBUTE_NONCE,
  2642. ss->nonce, (int)(NONCE_MAX_SIZE-1));
  2643. char *realm = ss->realm_options.name;
  2644. stun_attr_add_str(ioa_network_buffer_data(nbh), &len, STUN_ATTRIBUTE_REALM,
  2645. (u08bits*)realm, (int)(strlen((s08bits*)(realm))));
  2646. if(ss->server) {
  2647. turn_turnserver* server = (turn_turnserver*)ss->server;
  2648. if(server->oauth) {
  2649. const char *server_name = server->oauth_server_name;
  2650. if(!(server_name && server_name[0])) {
  2651. server_name = realm;
  2652. }
  2653. stun_attr_add_str(ioa_network_buffer_data(nbh), &len,
  2654. STUN_ATTRIBUTE_THIRD_PARTY_AUTHORIZATION,
  2655. (const u08bits*)(server_name),
  2656. strlen(server_name));
  2657. }
  2658. }
  2659. ioa_network_buffer_set_size(nbh,len);
  2660. return 0;
  2661. }
  2662. #if !defined(min)
  2663. #define min(a,b) ((a)<=(b) ? (a) : (b))
  2664. #endif
  2665. static void resume_processing_after_username_check(int success, int oauth, int max_session_time, hmackey_t hmackey, password_t pwd, turn_turnserver *server, u64bits ctxkey, ioa_net_data *in_buffer)
  2666. {
  2667. if(server && in_buffer && in_buffer->nbh) {
  2668. ts_ur_super_session *ss = get_session_from_map(server,(turnsession_id)ctxkey);
  2669. if(ss && ss->client_socket) {
  2670. turn_turnserver *server = (turn_turnserver *)ss->server;
  2671. if(success) {
  2672. ns_bcopy(hmackey,ss->hmackey,sizeof(hmackey_t));
  2673. ss->hmackey_set = 1;
  2674. ss->oauth = oauth;
  2675. ss->max_session_time_auth = (turn_time_t)max_session_time;
  2676. ns_bcopy(pwd,ss->pwd,sizeof(password_t));
  2677. }
  2678. read_client_connection(server,ss,in_buffer,0,0);
  2679. close_ioa_socket_after_processing_if_necessary(ss->client_socket);
  2680. ioa_network_buffer_delete(server->e, in_buffer->nbh);
  2681. in_buffer->nbh=NULL;
  2682. }
  2683. }
  2684. }
  2685. static int check_stun_auth(turn_turnserver *server,
  2686. ts_ur_super_session *ss, stun_tid *tid, int *resp_constructed,
  2687. int *err_code, const u08bits **reason,
  2688. ioa_net_data *in_buffer, ioa_network_buffer_handle nbh,
  2689. u16bits method, int *message_integrity,
  2690. int *postpone_reply,
  2691. int can_resume)
  2692. {
  2693. u08bits usname[STUN_MAX_USERNAME_SIZE+1];
  2694. u08bits nonce[STUN_MAX_NONCE_SIZE+1];
  2695. u08bits realm[STUN_MAX_REALM_SIZE+1];
  2696. size_t alen = 0;
  2697. if(!need_stun_authentication(server, ss))
  2698. return 0;
  2699. int new_nonce = 0;
  2700. {
  2701. int generate_new_nonce = 0;
  2702. if(ss->nonce[0]==0) {
  2703. generate_new_nonce = 1;
  2704. new_nonce = 1;
  2705. }
  2706. if(*(server->stale_nonce)) {
  2707. if(turn_time_before(ss->nonce_expiration_time,server->ctime)) {
  2708. generate_new_nonce = 1;
  2709. }
  2710. }
  2711. if(generate_new_nonce) {
  2712. int i = 0;
  2713. if(TURN_RANDOM_SIZE == 8) {
  2714. for(i=0;i<(NONCE_LENGTH_32BITS>>1);i++) {
  2715. u08bits *s = ss->nonce + 8*i;
  2716. u64bits rand=(u64bits)turn_random();
  2717. snprintf((s08bits*)s, NONCE_MAX_SIZE-8*i, "%08lx",(unsigned long)rand);
  2718. }
  2719. } else {
  2720. for(i=0;i<NONCE_LENGTH_32BITS;i++) {
  2721. u08bits *s = ss->nonce + 4*i;
  2722. u32bits rand=(u32bits)turn_random();
  2723. snprintf((s08bits*)s, NONCE_MAX_SIZE-4*i, "%04x",(unsigned int)rand);
  2724. }
  2725. }
  2726. ss->nonce_expiration_time = server->ctime + STUN_NONCE_EXPIRATION_TIME;
  2727. }
  2728. }
  2729. /* MESSAGE_INTEGRITY ATTR: */
  2730. stun_attr_ref sar = stun_attr_get_first_by_type_str(ioa_network_buffer_data(in_buffer->nbh),
  2731. ioa_network_buffer_get_size(in_buffer->nbh),
  2732. STUN_ATTRIBUTE_MESSAGE_INTEGRITY);
  2733. if(!sar) {
  2734. *err_code = 401;
  2735. return create_challenge_response(ss,tid,resp_constructed,err_code,reason,nbh,method);
  2736. }
  2737. {
  2738. int sarlen = stun_attr_get_len(sar);
  2739. switch(sarlen) {
  2740. case SHA1SIZEBYTES:
  2741. break;
  2742. case SHA256SIZEBYTES:
  2743. case SHA384SIZEBYTES:
  2744. case SHA512SIZEBYTES:
  2745. default:
  2746. *err_code = 401;
  2747. return create_challenge_response(ss,tid,resp_constructed,err_code,reason,nbh,method);
  2748. };
  2749. }
  2750. {
  2751. /* REALM ATTR: */
  2752. sar = stun_attr_get_first_by_type_str(ioa_network_buffer_data(in_buffer->nbh),
  2753. ioa_network_buffer_get_size(in_buffer->nbh),
  2754. STUN_ATTRIBUTE_REALM);
  2755. if(!sar) {
  2756. *err_code = 400;
  2757. return -1;
  2758. }
  2759. alen = min((size_t)stun_attr_get_len(sar),sizeof(realm)-1);
  2760. ns_bcopy(stun_attr_get_value(sar),realm,alen);
  2761. realm[alen]=0;
  2762. if(method == STUN_METHOD_CONNECTION_BIND) {
  2763. get_realm_options_by_name((char *)realm, &(ss->realm_options));
  2764. } else if(strcmp((char*)realm, (char*)(ss->realm_options.name))) {
  2765. if(method == STUN_METHOD_ALLOCATE) {
  2766. *err_code = 437;
  2767. *reason = (const u08bits*)"Allocation mismatch: wrong credentials: the realm value is incorrect";
  2768. } else {
  2769. *err_code = 441;
  2770. *reason = (const u08bits*)"Wrong credentials: the realm value is incorrect";
  2771. }
  2772. return -1;
  2773. }
  2774. }
  2775. /* USERNAME ATTR: */
  2776. sar = stun_attr_get_first_by_type_str(ioa_network_buffer_data(in_buffer->nbh),
  2777. ioa_network_buffer_get_size(in_buffer->nbh),
  2778. STUN_ATTRIBUTE_USERNAME);
  2779. if(!sar) {
  2780. *err_code = 400;
  2781. return -1;
  2782. }
  2783. alen = min((size_t)stun_attr_get_len(sar),sizeof(usname)-1);
  2784. ns_bcopy(stun_attr_get_value(sar),usname,alen);
  2785. usname[alen]=0;
  2786. if(ss->username[0]) {
  2787. if(strcmp((char*)ss->username,(char*)usname)) {
  2788. if(ss->oauth) {
  2789. ss->hmackey_set = 0;
  2790. STRCPY(ss->username,usname);
  2791. } else {
  2792. if(method == STUN_METHOD_ALLOCATE) {
  2793. *err_code = 437;
  2794. *reason = (const u08bits*)"Allocation mismatch: wrong credentials";
  2795. } else {
  2796. *err_code = 441;
  2797. }
  2798. return -1;
  2799. }
  2800. }
  2801. } else {
  2802. STRCPY(ss->username,usname);
  2803. set_realm_hash(ss->client_socket,(u08bits*)ss->realm_options.name);
  2804. }
  2805. {
  2806. /* NONCE ATTR: */
  2807. sar = stun_attr_get_first_by_type_str(ioa_network_buffer_data(in_buffer->nbh),
  2808. ioa_network_buffer_get_size(in_buffer->nbh),
  2809. STUN_ATTRIBUTE_NONCE);
  2810. if(!sar) {
  2811. *err_code = 400;
  2812. return -1;
  2813. }
  2814. alen = min((size_t)stun_attr_get_len(sar),sizeof(nonce)-1);
  2815. ns_bcopy(stun_attr_get_value(sar),nonce,alen);
  2816. nonce[alen]=0;
  2817. /* Stale Nonce check: */
  2818. if(new_nonce) {
  2819. *err_code = 438;
  2820. *reason = (const u08bits*)"Wrong nonce";
  2821. return create_challenge_response(ss,tid,resp_constructed,err_code,reason,nbh,method);
  2822. }
  2823. if(strcmp((s08bits*)ss->nonce,(s08bits*)nonce)) {
  2824. *err_code = 438;
  2825. *reason = (const u08bits*)"Stale nonce";
  2826. return create_challenge_response(ss,tid,resp_constructed,err_code,reason,nbh,method);
  2827. }
  2828. }
  2829. /* Password */
  2830. if(!(ss->hmackey_set) && (ss->pwd[0] == 0)) {
  2831. if(can_resume) {
  2832. (server->userkeycb)(server->id, server->ct, server->oauth, &(ss->oauth), usname, realm, resume_processing_after_username_check, in_buffer, ss->id, postpone_reply);
  2833. if(*postpone_reply) {
  2834. return 0;
  2835. }
  2836. }
  2837. /* direct user pattern is supported only for long-term credentials */
  2838. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR,
  2839. "%s: Cannot find credentials of user <%s>\n",
  2840. __FUNCTION__, (char*)usname);
  2841. *err_code = 401;
  2842. return create_challenge_response(ss,tid,resp_constructed,err_code,reason,nbh,method);
  2843. }
  2844. /* Check integrity */
  2845. if(stun_check_message_integrity_by_key_str(server->ct,ioa_network_buffer_data(in_buffer->nbh),
  2846. ioa_network_buffer_get_size(in_buffer->nbh),
  2847. ss->hmackey,
  2848. ss->pwd,
  2849. SHATYPE_DEFAULT)<1) {
  2850. if(can_resume) {
  2851. (server->userkeycb)(server->id, server->ct, server->oauth, &(ss->oauth), usname, realm, resume_processing_after_username_check, in_buffer, ss->id, postpone_reply);
  2852. if(*postpone_reply) {
  2853. return 0;
  2854. }
  2855. }
  2856. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR,
  2857. "%s: user %s credentials are incorrect\n",
  2858. __FUNCTION__, (char*)usname);
  2859. *err_code = 401;
  2860. return create_challenge_response(ss,tid,resp_constructed,err_code,reason,nbh,method);
  2861. }
  2862. *message_integrity = 1;
  2863. return 0;
  2864. }
  2865. //<<== AUTH
  2866. static void set_alternate_server(turn_server_addrs_list_t *asl, const ioa_addr *local_addr, size_t *counter, u16bits method, stun_tid *tid, int *resp_constructed, int *err_code, const u08bits **reason, ioa_network_buffer_handle nbh)
  2867. {
  2868. if(asl && asl->size && local_addr) {
  2869. size_t i;
  2870. /* to prevent indefinite cycle: */
  2871. for(i=0;i<asl->size;++i) {
  2872. ioa_addr *addr = &(asl->addrs[i]);
  2873. if(addr_eq(addr,local_addr))
  2874. return;
  2875. }
  2876. for(i=0;i<asl->size;++i) {
  2877. if(*counter>=asl->size)
  2878. *counter = 0;
  2879. ioa_addr *addr = &(asl->addrs[*counter]);
  2880. *counter +=1;
  2881. if(addr->ss.sa_family == local_addr->ss.sa_family) {
  2882. *err_code = 300;
  2883. size_t len = ioa_network_buffer_get_size(nbh);
  2884. stun_init_error_response_str(method, ioa_network_buffer_data(nbh), &len, *err_code, *reason, tid);
  2885. *resp_constructed = 1;
  2886. stun_attr_add_addr_str(ioa_network_buffer_data(nbh), &len, STUN_ATTRIBUTE_ALTERNATE_SERVER, addr);
  2887. ioa_network_buffer_set_size(nbh,len);
  2888. return;
  2889. }
  2890. }
  2891. }
  2892. }
  2893. static int handle_turn_command(turn_turnserver *server, ts_ur_super_session *ss, ioa_net_data *in_buffer, ioa_network_buffer_handle nbh, int *resp_constructed, int can_resume)
  2894. {
  2895. stun_tid tid;
  2896. int err_code = 0;
  2897. const u08bits *reason = NULL;
  2898. int no_response = 0;
  2899. int message_integrity = 0;
  2900. if(!(ss->client_socket))
  2901. return -1;
  2902. u16bits unknown_attrs[MAX_NUMBER_OF_UNKNOWN_ATTRS];
  2903. u16bits ua_num = 0;
  2904. u16bits method = stun_get_method_str(ioa_network_buffer_data(in_buffer->nbh),
  2905. ioa_network_buffer_get_size(in_buffer->nbh));
  2906. *resp_constructed = 0;
  2907. stun_tid_from_message_str(ioa_network_buffer_data(in_buffer->nbh),
  2908. ioa_network_buffer_get_size(in_buffer->nbh),
  2909. &tid);
  2910. if (stun_is_request_str(ioa_network_buffer_data(in_buffer->nbh),
  2911. ioa_network_buffer_get_size(in_buffer->nbh))) {
  2912. if((method == STUN_METHOD_BINDING) && (*(server->no_stun))) {
  2913. no_response = 1;
  2914. if(server->verbose) {
  2915. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO,
  2916. "%s: STUN method 0x%x ignored\n",
  2917. __FUNCTION__, (unsigned int)method);
  2918. }
  2919. } else if((method != STUN_METHOD_BINDING) && (*(server->stun_only))) {
  2920. no_response = 1;
  2921. if(server->verbose) {
  2922. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO,
  2923. "%s: STUN method 0x%x ignored\n",
  2924. __FUNCTION__, (unsigned int)method);
  2925. }
  2926. } else if((method != STUN_METHOD_BINDING) || (*(server->secure_stun))) {
  2927. if(method == STUN_METHOD_ALLOCATE) {
  2928. allocation *a = get_allocation_ss(ss);
  2929. if(is_allocation_valid(a)) {
  2930. if(!stun_tid_equals(&(a->tid), &tid)) {
  2931. err_code = 437;
  2932. reason = (const u08bits *)"Mismatched allocation: wrong transaction ID";
  2933. }
  2934. }
  2935. if(!err_code) {
  2936. SOCKET_TYPE cst = get_ioa_socket_type(ss->client_socket);
  2937. turn_server_addrs_list_t *asl = server->alternate_servers_list;
  2938. if(((cst == UDP_SOCKET)||(cst == DTLS_SOCKET)) && server->self_udp_balance &&
  2939. server->aux_servers_list && server->aux_servers_list->size) {
  2940. asl = server->aux_servers_list;
  2941. } else if(((cst == TLS_SOCKET) || (cst == DTLS_SOCKET) ||(cst == TLS_SCTP_SOCKET)) &&
  2942. server->tls_alternate_servers_list && server->tls_alternate_servers_list->size) {
  2943. asl = server->tls_alternate_servers_list;
  2944. }
  2945. if(asl && asl->size) {
  2946. turn_mutex_lock(&(asl->m));
  2947. set_alternate_server(asl,get_local_addr_from_ioa_socket(ss->client_socket),&(server->as_counter),method,&tid,resp_constructed,&err_code,&reason,nbh);
  2948. turn_mutex_unlock(&(asl->m));
  2949. }
  2950. }
  2951. }
  2952. /* check that the realm is the same as in the original request */
  2953. if(ss->origin_set) {
  2954. stun_attr_ref sar = stun_attr_get_first_str(ioa_network_buffer_data(in_buffer->nbh),
  2955. ioa_network_buffer_get_size(in_buffer->nbh));
  2956. int origin_found = 0;
  2957. int norigins = 0;
  2958. while(sar && !origin_found) {
  2959. if(stun_attr_get_type(sar) == STUN_ATTRIBUTE_ORIGIN) {
  2960. int sarlen = stun_attr_get_len(sar);
  2961. if(sarlen>0) {
  2962. ++norigins;
  2963. char *o = (char*)turn_malloc(sarlen+1);
  2964. ns_bcopy(stun_attr_get_value(sar),o,sarlen);
  2965. o[sarlen]=0;
  2966. char *corigin = (char*)turn_malloc(STUN_MAX_ORIGIN_SIZE+1);
  2967. corigin[0]=0;
  2968. if(get_canonic_origin(o,corigin,STUN_MAX_ORIGIN_SIZE)<0) {
  2969. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR,
  2970. "%s: Wrong origin format: %s\n",
  2971. __FUNCTION__, o);
  2972. }
  2973. if(!strncmp(ss->origin,corigin,STUN_MAX_ORIGIN_SIZE)) {
  2974. origin_found = 1;
  2975. }
  2976. turn_free(corigin,sarlen+1);
  2977. turn_free(o,sarlen+1);
  2978. }
  2979. }
  2980. sar = stun_attr_get_next_str(ioa_network_buffer_data(in_buffer->nbh),
  2981. ioa_network_buffer_get_size(in_buffer->nbh), sar);
  2982. }
  2983. if(server->check_origin && *(server->check_origin)) {
  2984. if(ss->origin[0]) {
  2985. if(!origin_found) {
  2986. err_code = 441;
  2987. reason = (const u08bits *)"The origin attribute does not match the initial session origin value";
  2988. if(server->verbose) {
  2989. char smethod[129];
  2990. stun_method_str(method,smethod);
  2991. log_method(ss, smethod, err_code, reason);
  2992. }
  2993. }
  2994. } else if(norigins > 0){
  2995. err_code = 441;
  2996. reason = (const u08bits *)"The origin attribute is empty, does not match the initial session origin value";
  2997. if(server->verbose) {
  2998. char smethod[129];
  2999. stun_method_str(method,smethod);
  3000. log_method(ss, smethod, err_code, reason);
  3001. }
  3002. }
  3003. }
  3004. }
  3005. /* get the initial origin value */
  3006. if(!err_code && !(ss->origin_set) && (method == STUN_METHOD_ALLOCATE)) {
  3007. stun_attr_ref sar = stun_attr_get_first_str(ioa_network_buffer_data(in_buffer->nbh),
  3008. ioa_network_buffer_get_size(in_buffer->nbh));
  3009. int origin_found = 0;
  3010. while(sar && !origin_found) {
  3011. if(stun_attr_get_type(sar) == STUN_ATTRIBUTE_ORIGIN) {
  3012. int sarlen = stun_attr_get_len(sar);
  3013. if(sarlen>0) {
  3014. char *o = (char*)turn_malloc(sarlen+1);
  3015. ns_bcopy(stun_attr_get_value(sar),o,sarlen);
  3016. o[sarlen]=0;
  3017. char *corigin = (char*)turn_malloc(STUN_MAX_ORIGIN_SIZE+1);
  3018. corigin[0]=0;
  3019. if(get_canonic_origin(o,corigin,STUN_MAX_ORIGIN_SIZE)<0) {
  3020. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR,
  3021. "%s: Wrong origin format: %s\n",
  3022. __FUNCTION__, o);
  3023. }
  3024. strncpy(ss->origin,corigin,STUN_MAX_ORIGIN_SIZE);
  3025. turn_free(corigin,sarlen+1);
  3026. turn_free(o,sarlen+1);
  3027. origin_found = get_realm_options_by_origin(ss->origin,&(ss->realm_options));
  3028. }
  3029. }
  3030. sar = stun_attr_get_next_str(ioa_network_buffer_data(in_buffer->nbh),
  3031. ioa_network_buffer_get_size(in_buffer->nbh), sar);
  3032. }
  3033. ss->origin_set = 1;
  3034. }
  3035. if(!err_code && !(*resp_constructed) && !no_response) {
  3036. if(method == STUN_METHOD_CONNECTION_BIND) {
  3037. ;
  3038. } else if(!(*(server->mobility)) || (method != STUN_METHOD_REFRESH) || is_allocation_valid(get_allocation_ss(ss))) {
  3039. int postpone_reply = 0;
  3040. check_stun_auth(server, ss, &tid, resp_constructed, &err_code, &reason, in_buffer, nbh, method, &message_integrity, &postpone_reply, can_resume);
  3041. if(postpone_reply)
  3042. no_response = 1;
  3043. }
  3044. }
  3045. }
  3046. if (!err_code && !(*resp_constructed) && !no_response) {
  3047. switch (method){
  3048. case STUN_METHOD_ALLOCATE:
  3049. {
  3050. handle_turn_allocate(server, ss, &tid, resp_constructed, &err_code, &reason,
  3051. unknown_attrs, &ua_num, in_buffer, nbh);
  3052. if(server->verbose) {
  3053. log_method(ss, "ALLOCATE", err_code, reason);
  3054. }
  3055. break;
  3056. }
  3057. case STUN_METHOD_CONNECT:
  3058. handle_turn_connect(server, ss, &tid, &err_code, &reason,
  3059. unknown_attrs, &ua_num, in_buffer);
  3060. if(server->verbose) {
  3061. log_method(ss, "CONNECT", err_code, reason);
  3062. }
  3063. if(!err_code)
  3064. no_response = 1;
  3065. break;
  3066. case STUN_METHOD_CONNECTION_BIND:
  3067. handle_turn_connection_bind(server, ss, &tid, resp_constructed, &err_code, &reason,
  3068. unknown_attrs, &ua_num, in_buffer, nbh, message_integrity, can_resume);
  3069. if(server->verbose && err_code) {
  3070. log_method(ss, "CONNECTION_BIND", err_code, reason);
  3071. }
  3072. break;
  3073. case STUN_METHOD_REFRESH:
  3074. handle_turn_refresh(server, ss, &tid, resp_constructed, &err_code, &reason,
  3075. unknown_attrs, &ua_num, in_buffer, nbh, message_integrity,
  3076. &no_response, can_resume);
  3077. if(server->verbose) {
  3078. log_method(ss, "REFRESH", err_code, reason);
  3079. }
  3080. break;
  3081. case STUN_METHOD_CHANNEL_BIND:
  3082. handle_turn_channel_bind(server, ss, &tid, resp_constructed, &err_code, &reason,
  3083. unknown_attrs, &ua_num, in_buffer, nbh);
  3084. if(server->verbose) {
  3085. log_method(ss, "CHANNEL_BIND", err_code, reason);
  3086. }
  3087. break;
  3088. case STUN_METHOD_CREATE_PERMISSION:
  3089. handle_turn_create_permission(server, ss, &tid, resp_constructed, &err_code, &reason,
  3090. unknown_attrs, &ua_num, in_buffer, nbh);
  3091. if(server->verbose) {
  3092. log_method(ss, "CREATE_PERMISSION", err_code, reason);
  3093. }
  3094. break;
  3095. case STUN_METHOD_BINDING:
  3096. {
  3097. int origin_changed=0;
  3098. ioa_addr response_origin;
  3099. int dest_changed=0;
  3100. ioa_addr response_destination;
  3101. handle_turn_binding(server, ss, &tid, resp_constructed, &err_code, &reason,
  3102. unknown_attrs, &ua_num, in_buffer, nbh,
  3103. &origin_changed, &response_origin,
  3104. &dest_changed, &response_destination,
  3105. 0, 0);
  3106. if(server->verbose) {
  3107. log_method(ss, "BINDING", err_code, reason);
  3108. }
  3109. if(*resp_constructed && !err_code && (origin_changed || dest_changed)) {
  3110. if (server->verbose) {
  3111. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "RFC 5780 request successfully processed\n");
  3112. }
  3113. {
  3114. static const u08bits *field = (const u08bits *) TURN_SOFTWARE;
  3115. static const size_t fsz = sizeof(TURN_SOFTWARE)-1;
  3116. size_t len = ioa_network_buffer_get_size(nbh);
  3117. stun_attr_add_str(ioa_network_buffer_data(nbh), &len, STUN_ATTRIBUTE_SOFTWARE, field, fsz);
  3118. ioa_network_buffer_set_size(nbh, len);
  3119. }
  3120. send_turn_message_to(server, nbh, &response_origin, &response_destination);
  3121. no_response = 1;
  3122. }
  3123. break;
  3124. }
  3125. default:
  3126. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unsupported STUN request received, method 0x%x\n",(unsigned int)method);
  3127. };
  3128. }
  3129. } else if (stun_is_indication_str(ioa_network_buffer_data(in_buffer->nbh),
  3130. ioa_network_buffer_get_size(in_buffer->nbh))) {
  3131. no_response = 1;
  3132. int postpone = 0;
  3133. if (!postpone && !err_code) {
  3134. switch (method){
  3135. case STUN_METHOD_BINDING:
  3136. //ICE ?
  3137. break;
  3138. case STUN_METHOD_SEND:
  3139. handle_turn_send(server, ss, &err_code, &reason, unknown_attrs, &ua_num, in_buffer);
  3140. if(eve(server->verbose)) {
  3141. log_method(ss, "SEND", err_code, reason);
  3142. }
  3143. break;
  3144. case STUN_METHOD_DATA:
  3145. err_code = 403;
  3146. if(eve(server->verbose)) {
  3147. log_method(ss, "DATA", err_code, reason);
  3148. }
  3149. break;
  3150. default:
  3151. if (server->verbose) {
  3152. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Unsupported STUN indication received: method 0x%x\n",(unsigned int)method);
  3153. }
  3154. }
  3155. };
  3156. } else {
  3157. no_response = 1;
  3158. if (server->verbose) {
  3159. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Wrong STUN message received\n");
  3160. }
  3161. }
  3162. if(ss->to_be_closed || !(ss->client_socket) || ioa_socket_tobeclosed(ss->client_socket))
  3163. return 0;
  3164. if (ua_num > 0) {
  3165. err_code = 420;
  3166. size_t len = ioa_network_buffer_get_size(nbh);
  3167. stun_init_error_response_str(method, ioa_network_buffer_data(nbh), &len, err_code, NULL, &tid);
  3168. stun_attr_add_str(ioa_network_buffer_data(nbh), &len, STUN_ATTRIBUTE_UNKNOWN_ATTRIBUTES, (const u08bits*) unknown_attrs, (ua_num
  3169. * 2));
  3170. ioa_network_buffer_set_size(nbh,len);
  3171. *resp_constructed = 1;
  3172. }
  3173. if (!no_response) {
  3174. if (!(*resp_constructed)) {
  3175. if (!err_code)
  3176. err_code = 400;
  3177. size_t len = ioa_network_buffer_get_size(nbh);
  3178. stun_init_error_response_str(method, ioa_network_buffer_data(nbh), &len, err_code, reason, &tid);
  3179. ioa_network_buffer_set_size(nbh,len);
  3180. *resp_constructed = 1;
  3181. }
  3182. {
  3183. static const u08bits *field = (const u08bits *) TURN_SOFTWARE;
  3184. static const size_t fsz = sizeof(TURN_SOFTWARE)-1;
  3185. size_t len = ioa_network_buffer_get_size(nbh);
  3186. stun_attr_add_str(ioa_network_buffer_data(nbh), &len, STUN_ATTRIBUTE_SOFTWARE, field, fsz);
  3187. ioa_network_buffer_set_size(nbh, len);
  3188. }
  3189. if(message_integrity) {
  3190. size_t len = ioa_network_buffer_get_size(nbh);
  3191. stun_attr_add_integrity_str(server->ct,ioa_network_buffer_data(nbh),&len,ss->hmackey,ss->pwd,SHATYPE_DEFAULT);
  3192. ioa_network_buffer_set_size(nbh,len);
  3193. }
  3194. if(err_code) {
  3195. if(server->verbose) {
  3196. log_method(ss, "message", err_code, reason);
  3197. }
  3198. }
  3199. } else {
  3200. *resp_constructed = 0;
  3201. }
  3202. return 0;
  3203. }
  3204. static int handle_old_stun_command(turn_turnserver *server, ts_ur_super_session *ss, ioa_net_data *in_buffer, ioa_network_buffer_handle nbh, int *resp_constructed, u32bits cookie)
  3205. {
  3206. stun_tid tid;
  3207. int err_code = 0;
  3208. const u08bits *reason = NULL;
  3209. int no_response = 0;
  3210. u16bits unknown_attrs[MAX_NUMBER_OF_UNKNOWN_ATTRS];
  3211. u16bits ua_num = 0;
  3212. u16bits method = stun_get_method_str(ioa_network_buffer_data(in_buffer->nbh),
  3213. ioa_network_buffer_get_size(in_buffer->nbh));
  3214. *resp_constructed = 0;
  3215. stun_tid_from_message_str(ioa_network_buffer_data(in_buffer->nbh),
  3216. ioa_network_buffer_get_size(in_buffer->nbh),
  3217. &tid);
  3218. if (stun_is_request_str(ioa_network_buffer_data(in_buffer->nbh),
  3219. ioa_network_buffer_get_size(in_buffer->nbh))) {
  3220. if(method != STUN_METHOD_BINDING) {
  3221. no_response = 1;
  3222. if(server->verbose) {
  3223. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO,
  3224. "%s: OLD STUN method 0x%x ignored\n",
  3225. __FUNCTION__, (unsigned int)method);
  3226. }
  3227. }
  3228. if (!err_code && !(*resp_constructed) && !no_response) {
  3229. int origin_changed=0;
  3230. ioa_addr response_origin;
  3231. int dest_changed=0;
  3232. ioa_addr response_destination;
  3233. handle_turn_binding(server, ss, &tid, resp_constructed, &err_code, &reason,
  3234. unknown_attrs, &ua_num, in_buffer, nbh,
  3235. &origin_changed, &response_origin,
  3236. &dest_changed, &response_destination,
  3237. cookie,1);
  3238. if(server->verbose) {
  3239. log_method(ss, "OLD BINDING", err_code, reason);
  3240. }
  3241. if(*resp_constructed && !err_code && (origin_changed || dest_changed)) {
  3242. if (server->verbose) {
  3243. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "RFC3489 CHANGE request successfully processed\n");
  3244. }
  3245. {
  3246. size_t newsz = (((sizeof(TURN_SOFTWARE))>>2) + 1)<<2;
  3247. u08bits software[120];
  3248. if(newsz>sizeof(software))
  3249. newsz = sizeof(software);
  3250. ns_bcopy(TURN_SOFTWARE,software,newsz);
  3251. size_t len = ioa_network_buffer_get_size(nbh);
  3252. stun_attr_add_str(ioa_network_buffer_data(nbh), &len, OLD_STUN_ATTRIBUTE_SERVER, software, newsz);
  3253. ioa_network_buffer_set_size(nbh, len);
  3254. }
  3255. send_turn_message_to(server, nbh, &response_origin, &response_destination);
  3256. no_response = 1;
  3257. }
  3258. }
  3259. } else {
  3260. no_response = 1;
  3261. if (server->verbose) {
  3262. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Wrong OLD STUN message received\n");
  3263. }
  3264. }
  3265. if (ua_num > 0) {
  3266. err_code = 420;
  3267. size_t len = ioa_network_buffer_get_size(nbh);
  3268. old_stun_init_error_response_str(method, ioa_network_buffer_data(nbh), &len, err_code, NULL, &tid, cookie);
  3269. stun_attr_add_str(ioa_network_buffer_data(nbh), &len, STUN_ATTRIBUTE_UNKNOWN_ATTRIBUTES, (const u08bits*) unknown_attrs, (ua_num * 2));
  3270. ioa_network_buffer_set_size(nbh,len);
  3271. *resp_constructed = 1;
  3272. }
  3273. if (!no_response) {
  3274. if (!(*resp_constructed)) {
  3275. if (!err_code)
  3276. err_code = 400;
  3277. size_t len = ioa_network_buffer_get_size(nbh);
  3278. old_stun_init_error_response_str(method, ioa_network_buffer_data(nbh), &len, err_code, reason, &tid, cookie);
  3279. ioa_network_buffer_set_size(nbh,len);
  3280. *resp_constructed = 1;
  3281. }
  3282. {
  3283. size_t newsz = (((sizeof(TURN_SOFTWARE))>>2) + 1)<<2;
  3284. u08bits software[120];
  3285. if(newsz>sizeof(software))
  3286. newsz = sizeof(software);
  3287. ns_bcopy(TURN_SOFTWARE,software,newsz);
  3288. size_t len = ioa_network_buffer_get_size(nbh);
  3289. stun_attr_add_str(ioa_network_buffer_data(nbh), &len, OLD_STUN_ATTRIBUTE_SERVER, software, newsz);
  3290. ioa_network_buffer_set_size(nbh, len);
  3291. }
  3292. if(err_code) {
  3293. if(server->verbose) {
  3294. log_method(ss, "OLD STUN message", err_code, reason);
  3295. }
  3296. }
  3297. } else {
  3298. *resp_constructed = 0;
  3299. }
  3300. return 0;
  3301. }
  3302. //////////////////////////////////////////////////////////////////
  3303. static int write_to_peerchannel(ts_ur_super_session* ss, u16bits chnum, ioa_net_data *in_buffer) {
  3304. int rc = 0;
  3305. if (ss && (in_buffer->recv_ttl!=0)) {
  3306. allocation* a = get_allocation_ss(ss);
  3307. if (is_allocation_valid(a)) {
  3308. ch_info* chn = allocation_get_ch_info(a, chnum);
  3309. if (!chn)
  3310. return -1;
  3311. /* Channel packets are always sent with DF=0: */
  3312. set_df_on_ioa_socket(get_relay_socket_ss(ss, chn->peer_addr.ss.sa_family), 0);
  3313. ioa_network_buffer_handle nbh = in_buffer->nbh;
  3314. ioa_network_buffer_add_offset_size(in_buffer->nbh, STUN_CHANNEL_HEADER_LENGTH, 0, ioa_network_buffer_get_size(in_buffer->nbh)-STUN_CHANNEL_HEADER_LENGTH);
  3315. ioa_network_buffer_header_init(nbh);
  3316. rc = send_data_from_ioa_socket_nbh(get_relay_socket_ss(ss, chn->peer_addr.ss.sa_family), &(chn->peer_addr), nbh, in_buffer->recv_ttl-1, in_buffer->recv_tos, NULL);
  3317. in_buffer->nbh = NULL;
  3318. }
  3319. }
  3320. return rc;
  3321. }
  3322. static void client_input_handler(ioa_socket_handle s, int event_type,
  3323. ioa_net_data *data, void *arg, int can_resume);
  3324. static void peer_input_handler(ioa_socket_handle s, int event_type,
  3325. ioa_net_data *data, void *arg, int can_resume);
  3326. /////////////// Client actions /////////////////
  3327. int shutdown_client_connection(turn_turnserver *server, ts_ur_super_session *ss, int force, const char* reason) {
  3328. FUNCSTART;
  3329. if (!ss)
  3330. return -1;
  3331. report_turn_session_info(server,ss,1);
  3332. dec_quota(ss);
  3333. dec_bps(ss);
  3334. allocation* alloc = get_allocation_ss(ss);
  3335. if (!is_allocation_valid(alloc)) {
  3336. force = 1;
  3337. }
  3338. if(!force && ss->is_mobile) {
  3339. if (ss->client_socket && server->verbose) {
  3340. char sraddr[129]="\0";
  3341. char sladdr[129]="\0";
  3342. addr_to_string(get_remote_addr_from_ioa_socket(ss->client_socket),(u08bits*)sraddr);
  3343. addr_to_string(get_local_addr_from_ioa_socket(ss->client_socket),(u08bits*)sladdr);
  3344. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "session %018llu: closed (1st stage), user <%s> realm <%s> origin <%s>, local %s, remote %s, reason: %s\n",(unsigned long long)(ss->id),(char*)ss->username,(char*)ss->realm_options.name,(char*)ss->origin, sladdr,sraddr,reason);
  3345. }
  3346. IOA_CLOSE_SOCKET(ss->client_socket);
  3347. FUNCEND;
  3348. return 0;
  3349. }
  3350. if (eve(server->verbose)) {
  3351. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO,
  3352. "closing session 0x%lx, client socket 0x%lx (socket session=0x%lx)\n",
  3353. (long) ss,
  3354. (long) ss->client_socket,
  3355. (long)get_ioa_socket_session(ss->client_socket));
  3356. }
  3357. if (server->disconnect)
  3358. server->disconnect(ss);
  3359. if (server->verbose) {
  3360. char sraddr[129]="\0";
  3361. char sladdr[129]="\0";
  3362. addr_to_string(get_remote_addr_from_ioa_socket(ss->client_socket),(u08bits*)sraddr);
  3363. addr_to_string(get_local_addr_from_ioa_socket(ss->client_socket),(u08bits*)sladdr);
  3364. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "session %018llu: closed (2nd stage), user <%s> realm <%s> origin <%s>, local %s, remote %s, reason: %s\n",
  3365. (unsigned long long)(ss->id), (char*)ss->username,(char*)ss->realm_options.name,(char*)ss->origin, sladdr,sraddr, reason);
  3366. }
  3367. IOA_CLOSE_SOCKET(ss->client_socket);
  3368. {
  3369. int i;
  3370. for(i=0;i<ALLOC_PROTOCOLS_NUMBER;++i) {
  3371. IOA_CLOSE_SOCKET(ss->alloc.relay_sessions[i].s);
  3372. }
  3373. }
  3374. turn_server_remove_all_from_ur_map_ss(ss);
  3375. FUNCEND;
  3376. return 0;
  3377. }
  3378. static void client_to_be_allocated_timeout_handler(ioa_engine_handle e,
  3379. void *arg) {
  3380. if (!arg)
  3381. return;
  3382. UNUSED_ARG(e);
  3383. ts_ur_super_session* ss = (ts_ur_super_session*) arg;
  3384. turn_turnserver* server = (turn_turnserver*) (ss->server);
  3385. if (!server)
  3386. return;
  3387. FUNCSTART;
  3388. int to_close = 0;
  3389. ioa_socket_handle s = ss->client_socket;
  3390. if(!s || ioa_socket_tobeclosed(s)) {
  3391. to_close = 1;
  3392. } else if(get_ioa_socket_app_type(s) == HTTPS_CLIENT_SOCKET) {
  3393. ;
  3394. } else {
  3395. ioa_socket_handle rs4 = ss->alloc.relay_sessions[ALLOC_IPV4_INDEX].s;
  3396. ioa_socket_handle rs6 = ss->alloc.relay_sessions[ALLOC_IPV6_INDEX].s;
  3397. if((!rs4 || ioa_socket_tobeclosed(rs4)) && (!rs6 || ioa_socket_tobeclosed(rs6))) {
  3398. to_close = 1;
  3399. } else if(ss->client_socket == NULL) {
  3400. to_close = 1;
  3401. } else if(!(ss->alloc.relay_sessions[ALLOC_IPV4_INDEX].lifetime_ev) &&
  3402. !(ss->alloc.relay_sessions[ALLOC_IPV6_INDEX].lifetime_ev)) {
  3403. to_close = 1;
  3404. } else if(!(ss->to_be_allocated_timeout_ev)) {
  3405. to_close = 1;
  3406. }
  3407. }
  3408. if(to_close) {
  3409. IOA_EVENT_DEL(ss->to_be_allocated_timeout_ev);
  3410. shutdown_client_connection(server, ss, 1, "allocation watchdog determined stale session state");
  3411. }
  3412. FUNCEND;
  3413. }
  3414. static int write_client_connection(turn_turnserver *server, ts_ur_super_session* ss, ioa_network_buffer_handle nbh, int ttl, int tos) {
  3415. FUNCSTART;
  3416. if (!(ss->client_socket)) {
  3417. ioa_network_buffer_delete(server->e, nbh);
  3418. FUNCEND;
  3419. return -1;
  3420. } else {
  3421. if (eve(server->verbose)) {
  3422. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO,
  3423. "%s: prepare to write to s 0x%lx\n", __FUNCTION__,
  3424. (long) (ss->client_socket));
  3425. }
  3426. int skip = 0;
  3427. int ret = send_data_from_ioa_socket_nbh(ss->client_socket, NULL, nbh, ttl, tos, &skip);
  3428. if(!skip) {
  3429. ++(ss->sent_packets);
  3430. ss->sent_bytes += (u32bits)ioa_network_buffer_get_size(nbh);
  3431. turn_report_session_usage(ss);
  3432. }
  3433. FUNCEND;
  3434. return ret;
  3435. }
  3436. }
  3437. static void client_ss_allocation_timeout_handler(ioa_engine_handle e, void *arg) {
  3438. UNUSED_ARG(e);
  3439. if (!arg)
  3440. return;
  3441. relay_endpoint_session *rsession = (relay_endpoint_session*)arg;
  3442. if(!(rsession->s))
  3443. return;
  3444. ts_ur_super_session* ss = get_ioa_socket_session(rsession->s);
  3445. if (!ss)
  3446. return;
  3447. allocation* a = get_allocation_ss(ss);
  3448. turn_turnserver* server = (turn_turnserver*) (ss->server);
  3449. if (!server) {
  3450. clear_allocation(a);
  3451. return;
  3452. }
  3453. FUNCSTART;
  3454. int family = get_ioa_socket_address_family(rsession->s);
  3455. set_allocation_family_invalid(a,family);
  3456. if(!get_relay_socket(a, AF_INET) && !get_relay_socket(a, AF_INET6)) {
  3457. shutdown_client_connection(server, ss, 0, "allocation timeout");
  3458. }
  3459. FUNCEND;
  3460. }
  3461. static int create_relay_connection(turn_turnserver* server,
  3462. ts_ur_super_session *ss, u32bits lifetime,
  3463. int address_family, u08bits transport,
  3464. int even_port, u64bits in_reservation_token, u64bits *out_reservation_token,
  3465. int *err_code, const u08bits **reason,
  3466. accept_cb acb) {
  3467. if (server && ss && ss->client_socket && !ioa_socket_tobeclosed(ss->client_socket)) {
  3468. allocation* a = get_allocation_ss(ss);
  3469. relay_endpoint_session* newelem = NULL;
  3470. ioa_socket_handle rtcp_s = NULL;
  3471. if (in_reservation_token) {
  3472. ioa_socket_handle s = NULL;
  3473. if ((get_ioa_socket_from_reservation(server->e, in_reservation_token,&s,(u08bits*)ss->realm_options.name) < 0)||
  3474. !s ||
  3475. ioa_socket_tobeclosed(s)) {
  3476. IOA_CLOSE_SOCKET(s);
  3477. *err_code = 404;
  3478. *reason = (const u08bits *)"Cannot find reserved socket";
  3479. return -1;
  3480. }
  3481. int family = get_ioa_socket_address_family(s);
  3482. newelem = get_relay_session_ss(ss,family);
  3483. if(newelem->s != s) {
  3484. IOA_CLOSE_SOCKET(newelem->s);
  3485. ns_bzero(newelem, sizeof(relay_endpoint_session));
  3486. newelem->s = s;
  3487. }
  3488. addr_debug_print(server->verbose, get_local_addr_from_ioa_socket(newelem->s), "Local relay addr (RTCP)");
  3489. } else {
  3490. newelem = get_relay_session_ss(ss,get_family(address_family));
  3491. IOA_CLOSE_SOCKET(newelem->s);
  3492. ns_bzero(newelem, sizeof(relay_endpoint_session));
  3493. newelem->s = NULL;
  3494. int res = create_relay_ioa_sockets(server->e,
  3495. ss->client_socket,
  3496. address_family, transport,
  3497. even_port, &(newelem->s), &rtcp_s, out_reservation_token,
  3498. err_code, reason, acb, ss);
  3499. if (res < 0) {
  3500. if(!(*err_code))
  3501. *err_code = 508;
  3502. if(!(*reason))
  3503. *reason = (const u08bits *)"Cannot create socket";
  3504. IOA_CLOSE_SOCKET(newelem->s);
  3505. IOA_CLOSE_SOCKET(rtcp_s);
  3506. return -1;
  3507. }
  3508. }
  3509. if (newelem->s == NULL) {
  3510. IOA_CLOSE_SOCKET(rtcp_s);
  3511. *err_code = 508;
  3512. *reason = (const u08bits *)"Cannot create relay socket";
  3513. return -1;
  3514. }
  3515. set_realm_hash(newelem->s,(u08bits*)ss->realm_options.name);
  3516. if (rtcp_s) {
  3517. if (out_reservation_token && *out_reservation_token) {
  3518. set_realm_hash(rtcp_s,(u08bits*)ss->realm_options.name);
  3519. /* OK */
  3520. } else {
  3521. IOA_CLOSE_SOCKET(newelem->s);
  3522. IOA_CLOSE_SOCKET(rtcp_s);
  3523. *err_code = 500;
  3524. *reason = (const u08bits *)"Wrong reservation tokens (internal error)";
  3525. return -1;
  3526. }
  3527. }
  3528. /* RFC6156: do not use DF when IPv6 is involved: */
  3529. if((get_ioa_socket_address_family(newelem->s) == AF_INET6) ||
  3530. (get_ioa_socket_address_family(ss->client_socket) == AF_INET6))
  3531. set_do_not_use_df(newelem->s);
  3532. if(get_ioa_socket_type(newelem->s) != TCP_SOCKET) {
  3533. if(register_callback_on_ioa_socket(server->e, newelem->s, IOA_EV_READ,peer_input_handler, ss, 0)<0) {
  3534. return -1;
  3535. }
  3536. }
  3537. if (lifetime<1)
  3538. lifetime = STUN_DEFAULT_ALLOCATE_LIFETIME;
  3539. else if(lifetime>STUN_MAX_ALLOCATE_LIFETIME)
  3540. lifetime = STUN_MAX_ALLOCATE_LIFETIME;
  3541. ioa_timer_handle ev = set_ioa_timer(server->e, lifetime, 0,
  3542. client_ss_allocation_timeout_handler, newelem, 0,
  3543. "client_ss_allocation_timeout_handler");
  3544. set_allocation_lifetime_ev(a, server->ctime + lifetime, ev, get_ioa_socket_address_family(newelem->s));
  3545. set_ioa_socket_session(newelem->s, ss);
  3546. }
  3547. return 0;
  3548. }
  3549. static int refresh_relay_connection(turn_turnserver* server,
  3550. ts_ur_super_session *ss, u32bits lifetime, int even_port,
  3551. u64bits in_reservation_token, u64bits *out_reservation_token,
  3552. int *err_code, int family) {
  3553. UNUSED_ARG(even_port);
  3554. UNUSED_ARG(in_reservation_token);
  3555. UNUSED_ARG(out_reservation_token);
  3556. UNUSED_ARG(err_code);
  3557. allocation* a = get_allocation_ss(ss);
  3558. if (server && ss && is_allocation_valid(a)) {
  3559. if (lifetime < 1) {
  3560. lifetime = 1;
  3561. }
  3562. ioa_timer_handle ev = set_ioa_timer(server->e, lifetime, 0,
  3563. client_ss_allocation_timeout_handler, get_relay_session(a,family), 0,
  3564. "refresh_client_ss_allocation_timeout_handler");
  3565. set_allocation_lifetime_ev(a, server->ctime + lifetime, ev, family);
  3566. return 0;
  3567. } else {
  3568. return -1;
  3569. }
  3570. }
  3571. static int read_client_connection(turn_turnserver *server,
  3572. ts_ur_super_session *ss, ioa_net_data *in_buffer,
  3573. int can_resume, int count_usage) {
  3574. FUNCSTART;
  3575. if (!server || !ss || !in_buffer || !(ss->client_socket) || ss->to_be_closed || ioa_socket_tobeclosed(ss->client_socket)) {
  3576. FUNCEND;
  3577. return -1;
  3578. }
  3579. int ret = (int)ioa_network_buffer_get_size(in_buffer->nbh);
  3580. if (ret < 0) {
  3581. FUNCEND;
  3582. return -1;
  3583. }
  3584. if(count_usage) {
  3585. ++(ss->received_packets);
  3586. ss->received_bytes += (u32bits)ioa_network_buffer_get_size(in_buffer->nbh);
  3587. turn_report_session_usage(ss);
  3588. }
  3589. if (eve(server->verbose)) {
  3590. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO,
  3591. "%s: data.buffer=0x%lx, data.len=%ld\n", __FUNCTION__,
  3592. (long)ioa_network_buffer_data(in_buffer->nbh),
  3593. (long)ioa_network_buffer_get_size(in_buffer->nbh));
  3594. }
  3595. u16bits chnum = 0;
  3596. u32bits old_stun_cookie = 0;
  3597. size_t blen = ioa_network_buffer_get_size(in_buffer->nbh);
  3598. size_t orig_blen = blen;
  3599. SOCKET_TYPE st = get_ioa_socket_type(ss->client_socket);
  3600. SOCKET_APP_TYPE sat = get_ioa_socket_app_type(ss->client_socket);
  3601. int is_padding_mandatory = is_stream_socket(st);
  3602. if(sat == HTTP_CLIENT_SOCKET) {
  3603. if(server->verbose) {
  3604. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "%s: HTTP connection input: %s\n", __FUNCTION__, (char*)ioa_network_buffer_data(in_buffer->nbh));
  3605. }
  3606. handle_http_echo(ss->client_socket);
  3607. } else if(sat == HTTPS_CLIENT_SOCKET) {
  3608. //???
  3609. } else if (stun_is_channel_message_str(ioa_network_buffer_data(in_buffer->nbh),
  3610. &blen,
  3611. &chnum,
  3612. is_padding_mandatory)) {
  3613. if(ss->is_tcp_relay) {
  3614. //Forbidden
  3615. FUNCEND;
  3616. return -1;
  3617. }
  3618. int rc = 0;
  3619. if(blen<=orig_blen) {
  3620. ioa_network_buffer_set_size(in_buffer->nbh,blen);
  3621. rc = write_to_peerchannel(ss, chnum, in_buffer);
  3622. }
  3623. if (eve(server->verbose)) {
  3624. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "%s: wrote to peer %d bytes\n",
  3625. __FUNCTION__, (int) rc);
  3626. }
  3627. FUNCEND;
  3628. return 0;
  3629. } else if (stun_is_command_message_full_check_str(ioa_network_buffer_data(in_buffer->nbh), ioa_network_buffer_get_size(in_buffer->nbh), 0, &(ss->enforce_fingerprints))) {
  3630. ioa_network_buffer_handle nbh = ioa_network_buffer_allocate(server->e);
  3631. int resp_constructed = 0;
  3632. u16bits method = stun_get_method_str(ioa_network_buffer_data(in_buffer->nbh),
  3633. ioa_network_buffer_get_size(in_buffer->nbh));
  3634. handle_turn_command(server, ss, in_buffer, nbh, &resp_constructed, can_resume);
  3635. if((method != STUN_METHOD_BINDING) && (method != STUN_METHOD_SEND))
  3636. report_turn_session_info(server,ss,0);
  3637. if(ss->to_be_closed || ioa_socket_tobeclosed(ss->client_socket)) {
  3638. FUNCEND;
  3639. ioa_network_buffer_delete(server->e, nbh);
  3640. return 0;
  3641. }
  3642. if (resp_constructed) {
  3643. if ((server->fingerprint) || ss->enforce_fingerprints) {
  3644. size_t len = ioa_network_buffer_get_size(nbh);
  3645. if (stun_attr_add_fingerprint_str(ioa_network_buffer_data(nbh), &len) < 0) {
  3646. FUNCEND ;
  3647. ioa_network_buffer_delete(server->e, nbh);
  3648. return -1;
  3649. }
  3650. ioa_network_buffer_set_size(nbh, len);
  3651. }
  3652. int ret = write_client_connection(server, ss, nbh, TTL_IGNORE, TOS_IGNORE);
  3653. FUNCEND ;
  3654. return ret;
  3655. } else {
  3656. ioa_network_buffer_delete(server->e, nbh);
  3657. return 0;
  3658. }
  3659. } else if (old_stun_is_command_message_str(ioa_network_buffer_data(in_buffer->nbh), ioa_network_buffer_get_size(in_buffer->nbh), &old_stun_cookie) && !(*(server->no_stun))) {
  3660. ioa_network_buffer_handle nbh = ioa_network_buffer_allocate(server->e);
  3661. int resp_constructed = 0;
  3662. handle_old_stun_command(server, ss, in_buffer, nbh, &resp_constructed, old_stun_cookie);
  3663. if (resp_constructed) {
  3664. int ret = write_client_connection(server, ss, nbh, TTL_IGNORE, TOS_IGNORE);
  3665. FUNCEND ;
  3666. return ret;
  3667. } else {
  3668. ioa_network_buffer_delete(server->e, nbh);
  3669. return 0;
  3670. }
  3671. } else {
  3672. SOCKET_TYPE st = get_ioa_socket_type(ss->client_socket);
  3673. if(is_stream_socket(st)) {
  3674. if(is_http((char*)ioa_network_buffer_data(in_buffer->nbh), ioa_network_buffer_get_size(in_buffer->nbh))) {
  3675. const char *proto = "HTTP";
  3676. ioa_network_buffer_data(in_buffer->nbh)[ioa_network_buffer_get_size(in_buffer->nbh)] = 0;
  3677. if(st==TLS_SOCKET) {
  3678. proto = "HTTPS";
  3679. set_ioa_socket_app_type(ss->client_socket,HTTPS_CLIENT_SOCKET);
  3680. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "%s: %s (%s %s) request: %s\n", __FUNCTION__, proto, get_ioa_socket_cipher(ss->client_socket), get_ioa_socket_ssl_method(ss->client_socket), (char*)ioa_network_buffer_data(in_buffer->nbh));
  3681. if(server->send_https_socket) {
  3682. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "%s socket to be detached: 0x%lx, st=%d, sat=%d\n", __FUNCTION__,(long)ss->client_socket, get_ioa_socket_type(ss->client_socket), get_ioa_socket_app_type(ss->client_socket));
  3683. ioa_socket_handle new_s = detach_ioa_socket(ss->client_socket);
  3684. if(new_s) {
  3685. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "%s new detached socket: 0x%lx, st=%d, sat=%d\n", __FUNCTION__,(long)new_s, get_ioa_socket_type(new_s), get_ioa_socket_app_type(new_s));
  3686. server->send_https_socket(new_s);
  3687. }
  3688. ss->to_be_closed = 1;
  3689. }
  3690. } else {
  3691. set_ioa_socket_app_type(ss->client_socket,HTTP_CLIENT_SOCKET);
  3692. if(server->verbose) {
  3693. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "%s: %s request: %s\n", __FUNCTION__, proto, (char*)ioa_network_buffer_data(in_buffer->nbh));
  3694. }
  3695. handle_http_echo(ss->client_socket);
  3696. }
  3697. return 0;
  3698. }
  3699. }
  3700. }
  3701. //Unrecognized message received, ignore it
  3702. FUNCEND;
  3703. return -1;
  3704. }
  3705. static int attach_socket_to_session(turn_turnserver* server, ioa_socket_handle s, ts_ur_super_session* ss) {
  3706. int ret = -1;
  3707. FUNCSTART;
  3708. if(s && server && ss && !ioa_socket_tobeclosed(s)) {
  3709. if(ss->client_socket != s) {
  3710. IOA_CLOSE_SOCKET(ss->client_socket);
  3711. ss->client_socket = s;
  3712. if(register_callback_on_ioa_socket(server->e, s, IOA_EV_READ,
  3713. client_input_handler, ss, 0)<0) {
  3714. return -1;
  3715. }
  3716. set_ioa_socket_session(s, ss);
  3717. }
  3718. ret = 0;
  3719. }
  3720. FUNCEND;
  3721. return ret;
  3722. }
  3723. int open_client_connection_session(turn_turnserver* server,
  3724. struct socket_message *sm) {
  3725. FUNCSTART;
  3726. if (!server)
  3727. return -1;
  3728. if (!(sm->s))
  3729. return -1;
  3730. ts_ur_super_session* ss = create_new_ss(server);
  3731. ss->client_socket = sm->s;
  3732. if(register_callback_on_ioa_socket(server->e, ss->client_socket, IOA_EV_READ,
  3733. client_input_handler, ss, 0)<0) {
  3734. return -1;
  3735. }
  3736. set_ioa_socket_session(ss->client_socket, ss);
  3737. int at = TURN_MAX_ALLOCATE_TIMEOUT;
  3738. if(*(server->stun_only))
  3739. at = TURN_MAX_ALLOCATE_TIMEOUT_STUN_ONLY;
  3740. IOA_EVENT_DEL(ss->to_be_allocated_timeout_ev);
  3741. ss->to_be_allocated_timeout_ev = set_ioa_timer(server->e,
  3742. at, 0,
  3743. client_to_be_allocated_timeout_handler, ss, 1,
  3744. "client_to_be_allocated_timeout_handler");
  3745. if(sm->nd.nbh) {
  3746. client_input_handler(ss->client_socket,IOA_EV_READ,&(sm->nd),ss,sm->can_resume);
  3747. ioa_network_buffer_delete(server->e, sm->nd.nbh);
  3748. sm->nd.nbh = NULL;
  3749. }
  3750. FUNCEND;
  3751. return 0;
  3752. }
  3753. /////////////// io handlers ///////////////////
  3754. static void peer_input_handler(ioa_socket_handle s, int event_type,
  3755. ioa_net_data *in_buffer, void *arg, int can_resume) {
  3756. if (!(event_type & IOA_EV_READ) || !arg) return;
  3757. if(in_buffer->recv_ttl==0) return;
  3758. UNUSED_ARG(can_resume);
  3759. if(!s || ioa_socket_tobeclosed(s)) return;
  3760. ts_ur_super_session* ss = (ts_ur_super_session*) arg;
  3761. if(!ss) return;
  3762. if(ss->to_be_closed) return;
  3763. if(!(ss->client_socket) || ioa_socket_tobeclosed(ss->client_socket)) return;
  3764. turn_turnserver *server = (turn_turnserver*) (ss->server);
  3765. if (!server) return;
  3766. relay_endpoint_session* elem = get_relay_session_ss(ss, get_ioa_socket_address_family(s));
  3767. if (elem->s == NULL) {
  3768. return;
  3769. }
  3770. int offset = STUN_CHANNEL_HEADER_LENGTH;
  3771. int ilen = min((int)ioa_network_buffer_get_size(in_buffer->nbh),
  3772. (int)(ioa_network_buffer_get_capacity_udp() - offset));
  3773. if (ilen >= 0) {
  3774. allocation* a = get_allocation_ss(ss);
  3775. if (is_allocation_valid(a)) {
  3776. u16bits chnum = 0;
  3777. ioa_network_buffer_handle nbh = NULL;
  3778. turn_permission_info* tinfo = allocation_get_permission(a,
  3779. &(in_buffer->src_addr));
  3780. if (tinfo) {
  3781. chnum = get_turn_channel_number(tinfo, &(in_buffer->src_addr));
  3782. } else if(!(server->server_relay)) {
  3783. return;
  3784. }
  3785. if (chnum) {
  3786. size_t len = (size_t)(ilen);
  3787. nbh = in_buffer->nbh;
  3788. ioa_network_buffer_add_offset_size(nbh,
  3789. 0,
  3790. STUN_CHANNEL_HEADER_LENGTH,
  3791. ioa_network_buffer_get_size(nbh)+STUN_CHANNEL_HEADER_LENGTH);
  3792. ioa_network_buffer_header_init(nbh);
  3793. SOCKET_TYPE st = get_ioa_socket_type(ss->client_socket);
  3794. int do_padding = is_stream_socket(st);
  3795. stun_init_channel_message_str(chnum, ioa_network_buffer_data(nbh), &len, len, do_padding);
  3796. ioa_network_buffer_set_size(nbh,len);
  3797. in_buffer->nbh = NULL;
  3798. if (eve(server->verbose)) {
  3799. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO,
  3800. "%s: send channel 0x%x\n", __FUNCTION__,
  3801. (int) (chnum));
  3802. }
  3803. } else {
  3804. size_t len = 0;
  3805. nbh = ioa_network_buffer_allocate(server->e);
  3806. stun_init_indication_str(STUN_METHOD_DATA, ioa_network_buffer_data(nbh), &len);
  3807. stun_attr_add_str(ioa_network_buffer_data(nbh), &len, STUN_ATTRIBUTE_DATA,
  3808. ioa_network_buffer_data(in_buffer->nbh), (size_t)ilen);
  3809. stun_attr_add_addr_str(ioa_network_buffer_data(nbh), &len,
  3810. STUN_ATTRIBUTE_XOR_PEER_ADDRESS,
  3811. &(in_buffer->src_addr));
  3812. ioa_network_buffer_set_size(nbh,len);
  3813. {
  3814. static const u08bits *field = (const u08bits *) TURN_SOFTWARE;
  3815. static const size_t fsz = sizeof(TURN_SOFTWARE)-1;
  3816. size_t len = ioa_network_buffer_get_size(nbh);
  3817. stun_attr_add_str(ioa_network_buffer_data(nbh), &len, STUN_ATTRIBUTE_SOFTWARE, field, fsz);
  3818. ioa_network_buffer_set_size(nbh, len);
  3819. }
  3820. if ((server->fingerprint) || ss->enforce_fingerprints) {
  3821. size_t len = ioa_network_buffer_get_size(nbh);
  3822. stun_attr_add_fingerprint_str(ioa_network_buffer_data(nbh), &len);
  3823. ioa_network_buffer_set_size(nbh, len);
  3824. }
  3825. }
  3826. if (eve(server->verbose)) {
  3827. u16bits* t = (u16bits*) ioa_network_buffer_data(nbh);
  3828. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Send data: 0x%x\n",
  3829. (int) (nswap16(t[0])));
  3830. }
  3831. write_client_connection(server, ss, nbh, in_buffer->recv_ttl-1, in_buffer->recv_tos);
  3832. }
  3833. }
  3834. }
  3835. static void client_input_handler(ioa_socket_handle s, int event_type,
  3836. ioa_net_data *data, void *arg, int can_resume) {
  3837. if (!arg)
  3838. return;
  3839. UNUSED_ARG(s);
  3840. UNUSED_ARG(event_type);
  3841. ts_ur_super_session* ss = (ts_ur_super_session*)arg;
  3842. turn_turnserver *server = (turn_turnserver*)ss->server;
  3843. if (!server) {
  3844. return;
  3845. }
  3846. if (ss->client_socket != s) {
  3847. return;
  3848. }
  3849. read_client_connection(server, ss, data, can_resume, 1);
  3850. if (ss->to_be_closed) {
  3851. if(server->verbose) {
  3852. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO,
  3853. "session %018llu: client socket to be closed in client handler: ss=0x%lx\n", (unsigned long long)(ss->id), (long)ss);
  3854. }
  3855. set_ioa_socket_tobeclosed(s);
  3856. }
  3857. }
  3858. ///////////////////////////////////////////////////////////
  3859. void init_turn_server(turn_turnserver* server,
  3860. turnserver_id id, int verbose, ioa_engine_handle e, turn_credential_type ct,
  3861. int stun_port, int fingerprint, dont_fragment_option_t dont_fragment,
  3862. get_user_key_cb userkeycb,
  3863. check_new_allocation_quota_cb chquotacb,
  3864. release_allocation_quota_cb raqcb,
  3865. ioa_addr *external_ip,
  3866. vintp check_origin,
  3867. vintp no_tcp_relay,
  3868. vintp no_udp_relay,
  3869. vintp stale_nonce,
  3870. vintp stun_only,
  3871. vintp no_stun,
  3872. turn_server_addrs_list_t *alternate_servers_list,
  3873. turn_server_addrs_list_t *tls_alternate_servers_list,
  3874. turn_server_addrs_list_t *aux_servers_list,
  3875. int self_udp_balance,
  3876. vintp no_multicast_peers, vintp no_loopback_peers,
  3877. ip_range_list_t* ip_whitelist, ip_range_list_t* ip_blacklist,
  3878. send_socket_to_relay_cb send_socket_to_relay,
  3879. vintp secure_stun, vintp mobility, int server_relay,
  3880. send_turn_session_info_cb send_turn_session_info,
  3881. send_https_socket_cb send_https_socket,
  3882. allocate_bps_cb allocate_bps_func,
  3883. int oauth, const char* oauth_server_name) {
  3884. if (!server)
  3885. return;
  3886. ns_bzero(server,sizeof(turn_turnserver));
  3887. server->e = e;
  3888. server->id = id;
  3889. server->ctime = turn_time();
  3890. server->session_id_counter = 0;
  3891. server->sessions_map = ur_map_create();
  3892. server->tcp_relay_connections = ur_map_create();
  3893. server->ct = ct;
  3894. server->userkeycb = userkeycb;
  3895. server->chquotacb = chquotacb;
  3896. server->raqcb = raqcb;
  3897. server->no_multicast_peers = no_multicast_peers;
  3898. server->no_loopback_peers = no_loopback_peers;
  3899. server->secure_stun = secure_stun;
  3900. server->mobility = mobility;
  3901. server->server_relay = server_relay;
  3902. server->send_turn_session_info = send_turn_session_info;
  3903. server->send_https_socket = send_https_socket;
  3904. server->oauth = oauth;
  3905. if(oauth)
  3906. server->oauth_server_name = oauth_server_name;
  3907. if(mobility)
  3908. server->mobile_connections_map = ur_map_create();
  3909. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO,"turn server id=%d created\n",(int)id);
  3910. server->check_origin = check_origin;
  3911. server->no_tcp_relay = no_tcp_relay;
  3912. server->no_udp_relay = no_udp_relay;
  3913. server->alternate_servers_list = alternate_servers_list;
  3914. server->tls_alternate_servers_list = tls_alternate_servers_list;
  3915. server->aux_servers_list = aux_servers_list;
  3916. server->self_udp_balance = self_udp_balance;
  3917. server->stale_nonce = stale_nonce;
  3918. server->stun_only = stun_only;
  3919. server->no_stun = no_stun;
  3920. server->dont_fragment = dont_fragment;
  3921. server->fingerprint = fingerprint;
  3922. if(external_ip) {
  3923. addr_cpy(&(server->external_ip), external_ip);
  3924. server->external_ip_set = 1;
  3925. }
  3926. if (stun_port < 1)
  3927. stun_port = DEFAULT_STUN_PORT;
  3928. server->verbose = verbose;
  3929. server->ip_whitelist = ip_whitelist;
  3930. server->ip_blacklist = ip_blacklist;
  3931. server->send_socket_to_relay = send_socket_to_relay;
  3932. server->allocate_bps_func = allocate_bps_func;
  3933. set_ioa_timer(server->e, 1, 0, timer_timeout_handler, server, 1, "timer_timeout_handler");
  3934. }
  3935. ioa_engine_handle turn_server_get_engine(turn_turnserver *s) {
  3936. if(s)
  3937. return s->e;
  3938. return NULL;
  3939. }
  3940. void set_disconnect_cb(turn_turnserver* server, int(*disconnect)(
  3941. ts_ur_super_session*)) {
  3942. server->disconnect = disconnect;
  3943. }
  3944. //////////////////////////////////////////////////////////////////