12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540 |
- //
- // GCDAsyncSocket.m
- //
- // This class is in the public domain.
- // Originally created by Robbie Hanson in Q4 2010.
- // Updated and maintained by Deusty LLC and the Apple development community.
- //
- // https://github.com/robbiehanson/CocoaAsyncSocket
- //
- #import "GCDAsyncSocket.h"
- #if TARGET_OS_IPHONE
- #import <CFNetwork/CFNetwork.h>
- #endif
- #import <TargetConditionals.h>
- #import <arpa/inet.h>
- #import <fcntl.h>
- #import <ifaddrs.h>
- #import <netdb.h>
- #import <netinet/in.h>
- #import <net/if.h>
- #import <sys/socket.h>
- #import <sys/types.h>
- #import <sys/ioctl.h>
- #import <sys/poll.h>
- #import <sys/uio.h>
- #import <sys/un.h>
- #import <unistd.h>
- #if ! __has_feature(objc_arc)
- #warning This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
- // For more information see: https://github.com/robbiehanson/CocoaAsyncSocket/wiki/ARC
- #endif
- #ifndef GCDAsyncSocketLoggingEnabled
- #define GCDAsyncSocketLoggingEnabled 1
- #endif
- #if GCDAsyncSocketLoggingEnabled
- // Logging Enabled - See log level below
- // Logging uses the CocoaLumberjack framework (which is also GCD based).
- // https://github.com/robbiehanson/CocoaLumberjack
- //
- // It allows us to do a lot of logging without significantly slowing down the code.
- #import "DDLog.h"
- #define LogAsync YES
- #define LogContext GCDAsyncSocketLoggingContext
- #ifndef GCDAsyncSocketLogLevel
- #define GCDAsyncSocketLogLevel LOG_LEVEL_VERBOSE
- #endif
- // Log levels : off, error, warn, info, verbose
- static const int logLevel = GCDAsyncSocketLogLevel;
- #define LogObjc(flg, frmt, ...) LOG_OBJC_MAYBE(LogAsync, logLevel, flg, LogContext, frmt, ##__VA_ARGS__)
- #define LogC(flg, frmt, ...) LOG_C_MAYBE(LogAsync, logLevel, flg, LogContext, frmt, ##__VA_ARGS__)
- #define LogError(frmt, ...) LogObjc(LOG_FLAG_ERROR, (@"%@: " frmt), THIS_FILE, ##__VA_ARGS__)
- #define LogWarn(frmt, ...) LogObjc(LOG_FLAG_WARN, (@"%@: " frmt), THIS_FILE, ##__VA_ARGS__)
- #define LogInfo(frmt, ...) LogObjc(LOG_FLAG_INFO, (@"%@: " frmt), THIS_FILE, ##__VA_ARGS__)
- #define LogVerbose(frmt, ...) LogObjc(LOG_FLAG_VERBOSE, (@"%@: " frmt), THIS_FILE, ##__VA_ARGS__)
- #define LogCError(frmt, ...) LogC(LOG_FLAG_ERROR, (@"%@: " frmt), THIS_FILE, ##__VA_ARGS__)
- #define LogCWarn(frmt, ...) LogC(LOG_FLAG_WARN, (@"%@: " frmt), THIS_FILE, ##__VA_ARGS__)
- #define LogCInfo(frmt, ...) LogC(LOG_FLAG_INFO, (@"%@: " frmt), THIS_FILE, ##__VA_ARGS__)
- #define LogCVerbose(frmt, ...) LogC(LOG_FLAG_VERBOSE, (@"%@: " frmt), THIS_FILE, ##__VA_ARGS__)
- #define LogTrace() LogObjc(LOG_FLAG_VERBOSE, @"%@: %@", THIS_FILE, THIS_METHOD)
- #define LogCTrace() LogC(LOG_FLAG_VERBOSE, @"%@: %s", THIS_FILE, __FUNCTION__)
- #else
- // Logging Disabled
- #define LogError(frmt, ...) {}
- #define LogWarn(frmt, ...) {}
- #define LogInfo(frmt, ...) {}
- #define LogVerbose(frmt, ...) {}
- #define LogCError(frmt, ...) {}
- #define LogCWarn(frmt, ...) {}
- #define LogCInfo(frmt, ...) {}
- #define LogCVerbose(frmt, ...) {}
- #define LogTrace() {}
- #define LogCTrace(frmt, ...) {}
- #endif
- /**
- * Seeing a return statements within an inner block
- * can sometimes be mistaken for a return point of the enclosing method.
- * This makes inline blocks a bit easier to read.
- **/
- #define return_from_block return
- /**
- * A socket file descriptor is really just an integer.
- * It represents the index of the socket within the kernel.
- * This makes invalid file descriptor comparisons easier to read.
- **/
- #define SOCKET_NULL -1
- NSString *const GCDAsyncSocketException = @"GCDAsyncSocketException";
- NSString *const GCDAsyncSocketErrorDomain = @"GCDAsyncSocketErrorDomain";
- NSString *const GCDAsyncSocketQueueName = @"GCDAsyncSocket";
- NSString *const GCDAsyncSocketThreadName = @"GCDAsyncSocket-CFStream";
- NSString *const GCDAsyncSocketManuallyEvaluateTrust = @"GCDAsyncSocketManuallyEvaluateTrust";
- #if TARGET_OS_IPHONE
- NSString *const GCDAsyncSocketUseCFStreamForTLS = @"GCDAsyncSocketUseCFStreamForTLS";
- #endif
- NSString *const GCDAsyncSocketSSLPeerID = @"GCDAsyncSocketSSLPeerID";
- NSString *const GCDAsyncSocketSSLProtocolVersionMin = @"GCDAsyncSocketSSLProtocolVersionMin";
- NSString *const GCDAsyncSocketSSLProtocolVersionMax = @"GCDAsyncSocketSSLProtocolVersionMax";
- NSString *const GCDAsyncSocketSSLSessionOptionFalseStart = @"GCDAsyncSocketSSLSessionOptionFalseStart";
- NSString *const GCDAsyncSocketSSLSessionOptionSendOneByteRecord = @"GCDAsyncSocketSSLSessionOptionSendOneByteRecord";
- NSString *const GCDAsyncSocketSSLCipherSuites = @"GCDAsyncSocketSSLCipherSuites";
- NSString *const GCDAsyncSocketSSLALPN = @"GCDAsyncSocketSSLALPN";
- #if !TARGET_OS_IPHONE
- NSString *const GCDAsyncSocketSSLDiffieHellmanParameters = @"GCDAsyncSocketSSLDiffieHellmanParameters";
- #endif
- enum GCDAsyncSocketFlags
- {
- kSocketStarted = 1 << 0, // If set, socket has been started (accepting/connecting)
- kConnected = 1 << 1, // If set, the socket is connected
- kForbidReadsWrites = 1 << 2, // If set, no new reads or writes are allowed
- kReadsPaused = 1 << 3, // If set, reads are paused due to possible timeout
- kWritesPaused = 1 << 4, // If set, writes are paused due to possible timeout
- kDisconnectAfterReads = 1 << 5, // If set, disconnect after no more reads are queued
- kDisconnectAfterWrites = 1 << 6, // If set, disconnect after no more writes are queued
- kSocketCanAcceptBytes = 1 << 7, // If set, we know socket can accept bytes. If unset, it's unknown.
- kReadSourceSuspended = 1 << 8, // If set, the read source is suspended
- kWriteSourceSuspended = 1 << 9, // If set, the write source is suspended
- kQueuedTLS = 1 << 10, // If set, we've queued an upgrade to TLS
- kStartingReadTLS = 1 << 11, // If set, we're waiting for TLS negotiation to complete
- kStartingWriteTLS = 1 << 12, // If set, we're waiting for TLS negotiation to complete
- kSocketSecure = 1 << 13, // If set, socket is using secure communication via SSL/TLS
- kSocketHasReadEOF = 1 << 14, // If set, we have read EOF from socket
- kReadStreamClosed = 1 << 15, // If set, we've read EOF plus prebuffer has been drained
- kDealloc = 1 << 16, // If set, the socket is being deallocated
- #if TARGET_OS_IPHONE
- kAddedStreamsToRunLoop = 1 << 17, // If set, CFStreams have been added to listener thread
- kUsingCFStreamForTLS = 1 << 18, // If set, we're forced to use CFStream instead of SecureTransport
- kSecureSocketHasBytesAvailable = 1 << 19, // If set, CFReadStream has notified us of bytes available
- #endif
- };
- enum GCDAsyncSocketConfig
- {
- kIPv4Disabled = 1 << 0, // If set, IPv4 is disabled
- kIPv6Disabled = 1 << 1, // If set, IPv6 is disabled
- kPreferIPv6 = 1 << 2, // If set, IPv6 is preferred over IPv4
- kAllowHalfDuplexConnection = 1 << 3, // If set, the socket will stay open even if the read stream closes
- };
- #if TARGET_OS_IPHONE
- static NSThread *cfstreamThread; // Used for CFStreams
- static uint64_t cfstreamThreadRetainCount; // setup & teardown
- static dispatch_queue_t cfstreamThreadSetupQueue; // setup & teardown
- #endif
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #pragma mark -
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /**
- * A PreBuffer is used when there is more data available on the socket
- * than is being requested by current read request.
- * In this case we slurp up all data from the socket (to minimize sys calls),
- * and store additional yet unread data in a "prebuffer".
- *
- * The prebuffer is entirely drained before we read from the socket again.
- * In other words, a large chunk of data is written is written to the prebuffer.
- * The prebuffer is then drained via a series of one or more reads (for subsequent read request(s)).
- *
- * A ring buffer was once used for this purpose.
- * But a ring buffer takes up twice as much memory as needed (double the size for mirroring).
- * In fact, it generally takes up more than twice the needed size as everything has to be rounded up to vm_page_size.
- * And since the prebuffer is always completely drained after being written to, a full ring buffer isn't needed.
- *
- * The current design is very simple and straight-forward, while also keeping memory requirements lower.
- **/
- @interface GCDAsyncSocketPreBuffer : NSObject
- {
- uint8_t *preBuffer;
- size_t preBufferSize;
-
- uint8_t *readPointer;
- uint8_t *writePointer;
- }
- - (instancetype)initWithCapacity:(size_t)numBytes NS_DESIGNATED_INITIALIZER;
- - (void)ensureCapacityForWrite:(size_t)numBytes;
- - (size_t)availableBytes;
- - (uint8_t *)readBuffer;
- - (void)getReadBuffer:(uint8_t **)bufferPtr availableBytes:(size_t *)availableBytesPtr;
- - (size_t)availableSpace;
- - (uint8_t *)writeBuffer;
- - (void)getWriteBuffer:(uint8_t **)bufferPtr availableSpace:(size_t *)availableSpacePtr;
- - (void)didRead:(size_t)bytesRead;
- - (void)didWrite:(size_t)bytesWritten;
- - (void)reset;
- @end
- @implementation GCDAsyncSocketPreBuffer
- // Cover the superclass' designated initializer
- - (instancetype)init NS_UNAVAILABLE
- {
- NSAssert(0, @"Use the designated initializer");
- return nil;
- }
- - (instancetype)initWithCapacity:(size_t)numBytes
- {
- if ((self = [super init]))
- {
- preBufferSize = numBytes;
- preBuffer = malloc(preBufferSize);
-
- readPointer = preBuffer;
- writePointer = preBuffer;
- }
- return self;
- }
- - (void)dealloc
- {
- if (preBuffer)
- free(preBuffer);
- }
- - (void)ensureCapacityForWrite:(size_t)numBytes
- {
- size_t availableSpace = [self availableSpace];
-
- if (numBytes > availableSpace)
- {
- size_t additionalBytes = numBytes - availableSpace;
-
- size_t newPreBufferSize = preBufferSize + additionalBytes;
- uint8_t *newPreBuffer = realloc(preBuffer, newPreBufferSize);
-
- size_t readPointerOffset = readPointer - preBuffer;
- size_t writePointerOffset = writePointer - preBuffer;
-
- preBuffer = newPreBuffer;
- preBufferSize = newPreBufferSize;
-
- readPointer = preBuffer + readPointerOffset;
- writePointer = preBuffer + writePointerOffset;
- }
- }
- - (size_t)availableBytes
- {
- return writePointer - readPointer;
- }
- - (uint8_t *)readBuffer
- {
- return readPointer;
- }
- - (void)getReadBuffer:(uint8_t **)bufferPtr availableBytes:(size_t *)availableBytesPtr
- {
- if (bufferPtr) *bufferPtr = readPointer;
- if (availableBytesPtr) *availableBytesPtr = [self availableBytes];
- }
- - (void)didRead:(size_t)bytesRead
- {
- readPointer += bytesRead;
-
- if (readPointer == writePointer)
- {
- // The prebuffer has been drained. Reset pointers.
- readPointer = preBuffer;
- writePointer = preBuffer;
- }
- }
- - (size_t)availableSpace
- {
- return preBufferSize - (writePointer - preBuffer);
- }
- - (uint8_t *)writeBuffer
- {
- return writePointer;
- }
- - (void)getWriteBuffer:(uint8_t **)bufferPtr availableSpace:(size_t *)availableSpacePtr
- {
- if (bufferPtr) *bufferPtr = writePointer;
- if (availableSpacePtr) *availableSpacePtr = [self availableSpace];
- }
- - (void)didWrite:(size_t)bytesWritten
- {
- writePointer += bytesWritten;
- }
- - (void)reset
- {
- readPointer = preBuffer;
- writePointer = preBuffer;
- }
- @end
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #pragma mark -
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /**
- * The GCDAsyncReadPacket encompasses the instructions for any given read.
- * The content of a read packet allows the code to determine if we're:
- * - reading to a certain length
- * - reading to a certain separator
- * - or simply reading the first chunk of available data
- **/
- @interface GCDAsyncReadPacket : NSObject
- {
- @public
- NSMutableData *buffer;
- NSUInteger startOffset;
- NSUInteger bytesDone;
- NSUInteger maxLength;
- NSTimeInterval timeout;
- NSUInteger readLength;
- NSData *term;
- BOOL bufferOwner;
- NSUInteger originalBufferLength;
- long tag;
- }
- - (instancetype)initWithData:(NSMutableData *)d
- startOffset:(NSUInteger)s
- maxLength:(NSUInteger)m
- timeout:(NSTimeInterval)t
- readLength:(NSUInteger)l
- terminator:(NSData *)e
- tag:(long)i NS_DESIGNATED_INITIALIZER;
- - (void)ensureCapacityForAdditionalDataOfLength:(NSUInteger)bytesToRead;
- - (NSUInteger)optimalReadLengthWithDefault:(NSUInteger)defaultValue shouldPreBuffer:(BOOL *)shouldPreBufferPtr;
- - (NSUInteger)readLengthForNonTermWithHint:(NSUInteger)bytesAvailable;
- - (NSUInteger)readLengthForTermWithHint:(NSUInteger)bytesAvailable shouldPreBuffer:(BOOL *)shouldPreBufferPtr;
- - (NSUInteger)readLengthForTermWithPreBuffer:(GCDAsyncSocketPreBuffer *)preBuffer found:(BOOL *)foundPtr;
- - (NSInteger)searchForTermAfterPreBuffering:(ssize_t)numBytes;
- @end
- @implementation GCDAsyncReadPacket
- // Cover the superclass' designated initializer
- - (instancetype)init NS_UNAVAILABLE
- {
- NSAssert(0, @"Use the designated initializer");
- return nil;
- }
- - (instancetype)initWithData:(NSMutableData *)d
- startOffset:(NSUInteger)s
- maxLength:(NSUInteger)m
- timeout:(NSTimeInterval)t
- readLength:(NSUInteger)l
- terminator:(NSData *)e
- tag:(long)i
- {
- if((self = [super init]))
- {
- bytesDone = 0;
- maxLength = m;
- timeout = t;
- readLength = l;
- term = [e copy];
- tag = i;
-
- if (d)
- {
- buffer = d;
- startOffset = s;
- bufferOwner = NO;
- originalBufferLength = [d length];
- }
- else
- {
- if (readLength > 0)
- buffer = [[NSMutableData alloc] initWithLength:readLength];
- else
- buffer = [[NSMutableData alloc] initWithLength:0];
-
- startOffset = 0;
- bufferOwner = YES;
- originalBufferLength = 0;
- }
- }
- return self;
- }
- /**
- * Increases the length of the buffer (if needed) to ensure a read of the given size will fit.
- **/
- - (void)ensureCapacityForAdditionalDataOfLength:(NSUInteger)bytesToRead
- {
- NSUInteger buffSize = [buffer length];
- NSUInteger buffUsed = startOffset + bytesDone;
-
- NSUInteger buffSpace = buffSize - buffUsed;
-
- if (bytesToRead > buffSpace)
- {
- NSUInteger buffInc = bytesToRead - buffSpace;
-
- [buffer increaseLengthBy:buffInc];
- }
- }
- /**
- * This method is used when we do NOT know how much data is available to be read from the socket.
- * This method returns the default value unless it exceeds the specified readLength or maxLength.
- *
- * Furthermore, the shouldPreBuffer decision is based upon the packet type,
- * and whether the returned value would fit in the current buffer without requiring a resize of the buffer.
- **/
- - (NSUInteger)optimalReadLengthWithDefault:(NSUInteger)defaultValue shouldPreBuffer:(BOOL *)shouldPreBufferPtr
- {
- NSUInteger result;
-
- if (readLength > 0)
- {
- // Read a specific length of data
- result = readLength - bytesDone;
-
- // There is no need to prebuffer since we know exactly how much data we need to read.
- // Even if the buffer isn't currently big enough to fit this amount of data,
- // it would have to be resized eventually anyway.
-
- if (shouldPreBufferPtr)
- *shouldPreBufferPtr = NO;
- }
- else
- {
- // Either reading until we find a specified terminator,
- // or we're simply reading all available data.
- //
- // In other words, one of:
- //
- // - readDataToData packet
- // - readDataWithTimeout packet
-
- if (maxLength > 0)
- result = MIN(defaultValue, (maxLength - bytesDone));
- else
- result = defaultValue;
-
- // Since we don't know the size of the read in advance,
- // the shouldPreBuffer decision is based upon whether the returned value would fit
- // in the current buffer without requiring a resize of the buffer.
- //
- // This is because, in all likelyhood, the amount read from the socket will be less than the default value.
- // Thus we should avoid over-allocating the read buffer when we can simply use the pre-buffer instead.
-
- if (shouldPreBufferPtr)
- {
- NSUInteger buffSize = [buffer length];
- NSUInteger buffUsed = startOffset + bytesDone;
-
- NSUInteger buffSpace = buffSize - buffUsed;
-
- if (buffSpace >= result)
- *shouldPreBufferPtr = NO;
- else
- *shouldPreBufferPtr = YES;
- }
- }
-
- return result;
- }
- /**
- * For read packets without a set terminator, returns the amount of data
- * that can be read without exceeding the readLength or maxLength.
- *
- * The given parameter indicates the number of bytes estimated to be available on the socket,
- * which is taken into consideration during the calculation.
- *
- * The given hint MUST be greater than zero.
- **/
- - (NSUInteger)readLengthForNonTermWithHint:(NSUInteger)bytesAvailable
- {
- NSAssert(term == nil, @"This method does not apply to term reads");
- NSAssert(bytesAvailable > 0, @"Invalid parameter: bytesAvailable");
-
- if (readLength > 0)
- {
- // Read a specific length of data
-
- return MIN(bytesAvailable, (readLength - bytesDone));
-
- // No need to avoid resizing the buffer.
- // If the user provided their own buffer,
- // and told us to read a certain length of data that exceeds the size of the buffer,
- // then it is clear that our code will resize the buffer during the read operation.
- //
- // This method does not actually do any resizing.
- // The resizing will happen elsewhere if needed.
- }
- else
- {
- // Read all available data
-
- NSUInteger result = bytesAvailable;
-
- if (maxLength > 0)
- {
- result = MIN(result, (maxLength - bytesDone));
- }
-
- // No need to avoid resizing the buffer.
- // If the user provided their own buffer,
- // and told us to read all available data without giving us a maxLength,
- // then it is clear that our code might resize the buffer during the read operation.
- //
- // This method does not actually do any resizing.
- // The resizing will happen elsewhere if needed.
-
- return result;
- }
- }
- /**
- * For read packets with a set terminator, returns the amount of data
- * that can be read without exceeding the maxLength.
- *
- * The given parameter indicates the number of bytes estimated to be available on the socket,
- * which is taken into consideration during the calculation.
- *
- * To optimize memory allocations, mem copies, and mem moves
- * the shouldPreBuffer boolean value will indicate if the data should be read into a prebuffer first,
- * or if the data can be read directly into the read packet's buffer.
- **/
- - (NSUInteger)readLengthForTermWithHint:(NSUInteger)bytesAvailable shouldPreBuffer:(BOOL *)shouldPreBufferPtr
- {
- NSAssert(term != nil, @"This method does not apply to non-term reads");
- NSAssert(bytesAvailable > 0, @"Invalid parameter: bytesAvailable");
-
-
- NSUInteger result = bytesAvailable;
-
- if (maxLength > 0)
- {
- result = MIN(result, (maxLength - bytesDone));
- }
-
- // Should the data be read into the read packet's buffer, or into a pre-buffer first?
- //
- // One would imagine the preferred option is the faster one.
- // So which one is faster?
- //
- // Reading directly into the packet's buffer requires:
- // 1. Possibly resizing packet buffer (malloc/realloc)
- // 2. Filling buffer (read)
- // 3. Searching for term (memcmp)
- // 4. Possibly copying overflow into prebuffer (malloc/realloc, memcpy)
- //
- // Reading into prebuffer first:
- // 1. Possibly resizing prebuffer (malloc/realloc)
- // 2. Filling buffer (read)
- // 3. Searching for term (memcmp)
- // 4. Copying underflow into packet buffer (malloc/realloc, memcpy)
- // 5. Removing underflow from prebuffer (memmove)
- //
- // Comparing the performance of the two we can see that reading
- // data into the prebuffer first is slower due to the extra memove.
- //
- // However:
- // The implementation of NSMutableData is open source via core foundation's CFMutableData.
- // Decreasing the length of a mutable data object doesn't cause a realloc.
- // In other words, the capacity of a mutable data object can grow, but doesn't shrink.
- //
- // This means the prebuffer will rarely need a realloc.
- // The packet buffer, on the other hand, may often need a realloc.
- // This is especially true if we are the buffer owner.
- // Furthermore, if we are constantly realloc'ing the packet buffer,
- // and then moving the overflow into the prebuffer,
- // then we're consistently over-allocating memory for each term read.
- // And now we get into a bit of a tradeoff between speed and memory utilization.
- //
- // The end result is that the two perform very similarly.
- // And we can answer the original question very simply by another means.
- //
- // If we can read all the data directly into the packet's buffer without resizing it first,
- // then we do so. Otherwise we use the prebuffer.
-
- if (shouldPreBufferPtr)
- {
- NSUInteger buffSize = [buffer length];
- NSUInteger buffUsed = startOffset + bytesDone;
-
- if ((buffSize - buffUsed) >= result)
- *shouldPreBufferPtr = NO;
- else
- *shouldPreBufferPtr = YES;
- }
-
- return result;
- }
- /**
- * For read packets with a set terminator,
- * returns the amount of data that can be read from the given preBuffer,
- * without going over a terminator or the maxLength.
- *
- * It is assumed the terminator has not already been read.
- **/
- - (NSUInteger)readLengthForTermWithPreBuffer:(GCDAsyncSocketPreBuffer *)preBuffer found:(BOOL *)foundPtr
- {
- NSAssert(term != nil, @"This method does not apply to non-term reads");
- NSAssert([preBuffer availableBytes] > 0, @"Invoked with empty pre buffer!");
-
- // We know that the terminator, as a whole, doesn't exist in our own buffer.
- // But it is possible that a _portion_ of it exists in our buffer.
- // So we're going to look for the terminator starting with a portion of our own buffer.
- //
- // Example:
- //
- // term length = 3 bytes
- // bytesDone = 5 bytes
- // preBuffer length = 5 bytes
- //
- // If we append the preBuffer to our buffer,
- // it would look like this:
- //
- // ---------------------
- // |B|B|B|B|B|P|P|P|P|P|
- // ---------------------
- //
- // So we start our search here:
- //
- // ---------------------
- // |B|B|B|B|B|P|P|P|P|P|
- // -------^-^-^---------
- //
- // And move forwards...
- //
- // ---------------------
- // |B|B|B|B|B|P|P|P|P|P|
- // ---------^-^-^-------
- //
- // Until we find the terminator or reach the end.
- //
- // ---------------------
- // |B|B|B|B|B|P|P|P|P|P|
- // ---------------^-^-^-
-
- BOOL found = NO;
-
- NSUInteger termLength = [term length];
- NSUInteger preBufferLength = [preBuffer availableBytes];
-
- if ((bytesDone + preBufferLength) < termLength)
- {
- // Not enough data for a full term sequence yet
- return preBufferLength;
- }
-
- NSUInteger maxPreBufferLength;
- if (maxLength > 0) {
- maxPreBufferLength = MIN(preBufferLength, (maxLength - bytesDone));
-
- // Note: maxLength >= termLength
- }
- else {
- maxPreBufferLength = preBufferLength;
- }
-
- uint8_t seq[termLength];
- const void *termBuf = [term bytes];
-
- NSUInteger bufLen = MIN(bytesDone, (termLength - 1));
- uint8_t *buf = (uint8_t *)[buffer mutableBytes] + startOffset + bytesDone - bufLen;
-
- NSUInteger preLen = termLength - bufLen;
- const uint8_t *pre = [preBuffer readBuffer];
-
- NSUInteger loopCount = bufLen + maxPreBufferLength - termLength + 1; // Plus one. See example above.
-
- NSUInteger result = maxPreBufferLength;
-
- NSUInteger i;
- for (i = 0; i < loopCount; i++)
- {
- if (bufLen > 0)
- {
- // Combining bytes from buffer and preBuffer
-
- memcpy(seq, buf, bufLen);
- memcpy(seq + bufLen, pre, preLen);
-
- if (memcmp(seq, termBuf, termLength) == 0)
- {
- result = preLen;
- found = YES;
- break;
- }
-
- buf++;
- bufLen--;
- preLen++;
- }
- else
- {
- // Comparing directly from preBuffer
-
- if (memcmp(pre, termBuf, termLength) == 0)
- {
- NSUInteger preOffset = pre - [preBuffer readBuffer]; // pointer arithmetic
-
- result = preOffset + termLength;
- found = YES;
- break;
- }
-
- pre++;
- }
- }
-
- // There is no need to avoid resizing the buffer in this particular situation.
-
- if (foundPtr) *foundPtr = found;
- return result;
- }
- /**
- * For read packets with a set terminator, scans the packet buffer for the term.
- * It is assumed the terminator had not been fully read prior to the new bytes.
- *
- * If the term is found, the number of excess bytes after the term are returned.
- * If the term is not found, this method will return -1.
- *
- * Note: A return value of zero means the term was found at the very end.
- *
- * Prerequisites:
- * The given number of bytes have been added to the end of our buffer.
- * Our bytesDone variable has NOT been changed due to the prebuffered bytes.
- **/
- - (NSInteger)searchForTermAfterPreBuffering:(ssize_t)numBytes
- {
- NSAssert(term != nil, @"This method does not apply to non-term reads");
-
- // The implementation of this method is very similar to the above method.
- // See the above method for a discussion of the algorithm used here.
-
- uint8_t *buff = [buffer mutableBytes];
- NSUInteger buffLength = bytesDone + numBytes;
-
- const void *termBuff = [term bytes];
- NSUInteger termLength = [term length];
-
- // Note: We are dealing with unsigned integers,
- // so make sure the math doesn't go below zero.
-
- NSUInteger i = ((buffLength - numBytes) >= termLength) ? (buffLength - numBytes - termLength + 1) : 0;
-
- while (i + termLength <= buffLength)
- {
- uint8_t *subBuffer = buff + startOffset + i;
-
- if (memcmp(subBuffer, termBuff, termLength) == 0)
- {
- return buffLength - (i + termLength);
- }
-
- i++;
- }
-
- return -1;
- }
- @end
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #pragma mark -
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /**
- * The GCDAsyncWritePacket encompasses the instructions for any given write.
- **/
- @interface GCDAsyncWritePacket : NSObject
- {
- @public
- NSData *buffer;
- NSUInteger bytesDone;
- long tag;
- NSTimeInterval timeout;
- }
- - (instancetype)initWithData:(NSData *)d timeout:(NSTimeInterval)t tag:(long)i NS_DESIGNATED_INITIALIZER;
- @end
- @implementation GCDAsyncWritePacket
- // Cover the superclass' designated initializer
- - (instancetype)init NS_UNAVAILABLE
- {
- NSAssert(0, @"Use the designated initializer");
- return nil;
- }
- - (instancetype)initWithData:(NSData *)d timeout:(NSTimeInterval)t tag:(long)i
- {
- if((self = [super init]))
- {
- buffer = d; // Retain not copy. For performance as documented in header file.
- bytesDone = 0;
- timeout = t;
- tag = i;
- }
- return self;
- }
- @end
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #pragma mark -
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /**
- * The GCDAsyncSpecialPacket encompasses special instructions for interruptions in the read/write queues.
- * This class my be altered to support more than just TLS in the future.
- **/
- @interface GCDAsyncSpecialPacket : NSObject
- {
- @public
- NSDictionary *tlsSettings;
- }
- - (instancetype)initWithTLSSettings:(NSDictionary <NSString*,NSObject*>*)settings NS_DESIGNATED_INITIALIZER;
- @end
- @implementation GCDAsyncSpecialPacket
- // Cover the superclass' designated initializer
- - (instancetype)init NS_UNAVAILABLE
- {
- NSAssert(0, @"Use the designated initializer");
- return nil;
- }
- - (instancetype)initWithTLSSettings:(NSDictionary <NSString*,NSObject*>*)settings
- {
- if((self = [super init]))
- {
- tlsSettings = [settings copy];
- }
- return self;
- }
- @end
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #pragma mark -
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- @implementation GCDAsyncSocket
- {
- uint32_t flags;
- uint16_t config;
-
- __weak id<GCDAsyncSocketDelegate> delegate;
- dispatch_queue_t delegateQueue;
-
- int socket4FD;
- int socket6FD;
- int socketUN;
- NSURL *socketUrl;
- int stateIndex;
- NSData * connectInterface4;
- NSData * connectInterface6;
- NSData * connectInterfaceUN;
-
- dispatch_queue_t socketQueue;
-
- dispatch_source_t accept4Source;
- dispatch_source_t accept6Source;
- dispatch_source_t acceptUNSource;
- dispatch_source_t connectTimer;
- dispatch_source_t readSource;
- dispatch_source_t writeSource;
- dispatch_source_t readTimer;
- dispatch_source_t writeTimer;
-
- NSMutableArray *readQueue;
- NSMutableArray *writeQueue;
-
- GCDAsyncReadPacket *currentRead;
- GCDAsyncWritePacket *currentWrite;
-
- unsigned long socketFDBytesAvailable;
-
- GCDAsyncSocketPreBuffer *preBuffer;
-
- #if TARGET_OS_IPHONE
- CFStreamClientContext streamContext;
- CFReadStreamRef readStream;
- CFWriteStreamRef writeStream;
- #endif
- SSLContextRef sslContext;
- GCDAsyncSocketPreBuffer *sslPreBuffer;
- size_t sslWriteCachedLength;
- OSStatus sslErrCode;
- OSStatus lastSSLHandshakeError;
-
- void *IsOnSocketQueueOrTargetQueueKey;
-
- id userData;
- NSTimeInterval alternateAddressDelay;
- }
- - (instancetype)init
- {
- return [self initWithDelegate:nil delegateQueue:NULL socketQueue:NULL];
- }
- - (instancetype)initWithSocketQueue:(dispatch_queue_t)sq
- {
- return [self initWithDelegate:nil delegateQueue:NULL socketQueue:sq];
- }
- - (instancetype)initWithDelegate:(id<GCDAsyncSocketDelegate>)aDelegate delegateQueue:(dispatch_queue_t)dq
- {
- return [self initWithDelegate:aDelegate delegateQueue:dq socketQueue:NULL];
- }
- - (instancetype)initWithDelegate:(id<GCDAsyncSocketDelegate>)aDelegate delegateQueue:(dispatch_queue_t)dq socketQueue:(dispatch_queue_t)sq
- {
- if((self = [super init]))
- {
- delegate = aDelegate;
- delegateQueue = dq;
-
- #if !OS_OBJECT_USE_OBJC
- if (dq) dispatch_retain(dq);
- #endif
-
- socket4FD = SOCKET_NULL;
- socket6FD = SOCKET_NULL;
- socketUN = SOCKET_NULL;
- socketUrl = nil;
- stateIndex = 0;
-
- if (sq)
- {
- NSAssert(sq != dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0),
- @"The given socketQueue parameter must not be a concurrent queue.");
- NSAssert(sq != dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0),
- @"The given socketQueue parameter must not be a concurrent queue.");
- NSAssert(sq != dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
- @"The given socketQueue parameter must not be a concurrent queue.");
-
- socketQueue = sq;
- #if !OS_OBJECT_USE_OBJC
- dispatch_retain(sq);
- #endif
- }
- else
- {
- socketQueue = dispatch_queue_create([GCDAsyncSocketQueueName UTF8String], NULL);
- }
-
- // The dispatch_queue_set_specific() and dispatch_get_specific() functions take a "void *key" parameter.
- // From the documentation:
- //
- // > Keys are only compared as pointers and are never dereferenced.
- // > Thus, you can use a pointer to a static variable for a specific subsystem or
- // > any other value that allows you to identify the value uniquely.
- //
- // We're just going to use the memory address of an ivar.
- // Specifically an ivar that is explicitly named for our purpose to make the code more readable.
- //
- // However, it feels tedious (and less readable) to include the "&" all the time:
- // dispatch_get_specific(&IsOnSocketQueueOrTargetQueueKey)
- //
- // So we're going to make it so it doesn't matter if we use the '&' or not,
- // by assigning the value of the ivar to the address of the ivar.
- // Thus: IsOnSocketQueueOrTargetQueueKey == &IsOnSocketQueueOrTargetQueueKey;
-
- IsOnSocketQueueOrTargetQueueKey = &IsOnSocketQueueOrTargetQueueKey;
-
- void *nonNullUnusedPointer = (__bridge void *)self;
- dispatch_queue_set_specific(socketQueue, IsOnSocketQueueOrTargetQueueKey, nonNullUnusedPointer, NULL);
-
- readQueue = [[NSMutableArray alloc] initWithCapacity:5];
- currentRead = nil;
-
- writeQueue = [[NSMutableArray alloc] initWithCapacity:5];
- currentWrite = nil;
-
- preBuffer = [[GCDAsyncSocketPreBuffer alloc] initWithCapacity:(1024 * 4)];
- alternateAddressDelay = 0.3;
- }
- return self;
- }
- - (void)dealloc
- {
- LogInfo(@"%@ - %@ (start)", THIS_METHOD, self);
-
- // Set dealloc flag.
- // This is used by closeWithError to ensure we don't accidentally retain ourself.
- flags |= kDealloc;
-
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- {
- [self closeWithError:nil];
- }
- else
- {
- dispatch_sync(socketQueue, ^{
- [self closeWithError:nil];
- });
- }
-
- delegate = nil;
-
- #if !OS_OBJECT_USE_OBJC
- if (delegateQueue) dispatch_release(delegateQueue);
- #endif
- delegateQueue = NULL;
-
- #if !OS_OBJECT_USE_OBJC
- if (socketQueue) dispatch_release(socketQueue);
- #endif
- socketQueue = NULL;
-
- LogInfo(@"%@ - %@ (finish)", THIS_METHOD, self);
- }
- #pragma mark -
- + (nullable instancetype)socketFromConnectedSocketFD:(int)socketFD socketQueue:(nullable dispatch_queue_t)sq error:(NSError**)error {
- return [self socketFromConnectedSocketFD:socketFD delegate:nil delegateQueue:NULL socketQueue:sq error:error];
- }
- + (nullable instancetype)socketFromConnectedSocketFD:(int)socketFD delegate:(nullable id<GCDAsyncSocketDelegate>)aDelegate delegateQueue:(nullable dispatch_queue_t)dq error:(NSError**)error {
- return [self socketFromConnectedSocketFD:socketFD delegate:aDelegate delegateQueue:dq socketQueue:NULL error:error];
- }
- + (nullable instancetype)socketFromConnectedSocketFD:(int)socketFD delegate:(nullable id<GCDAsyncSocketDelegate>)aDelegate delegateQueue:(nullable dispatch_queue_t)dq socketQueue:(nullable dispatch_queue_t)sq error:(NSError* __autoreleasing *)error
- {
- __block BOOL errorOccured = NO;
- __block NSError *thisError = nil;
-
- GCDAsyncSocket *socket = [[[self class] alloc] initWithDelegate:aDelegate delegateQueue:dq socketQueue:sq];
-
- dispatch_sync(socket->socketQueue, ^{ @autoreleasepool {
- struct sockaddr addr;
- socklen_t addr_size = sizeof(struct sockaddr);
- int retVal = getpeername(socketFD, (struct sockaddr *)&addr, &addr_size);
- if (retVal)
- {
- NSString *errMsg = NSLocalizedStringWithDefaultValue(@"GCDAsyncSocketOtherError",
- @"GCDAsyncSocket", [NSBundle mainBundle],
- @"Attempt to create socket from socket FD failed. getpeername() failed", nil);
-
- NSDictionary *userInfo = @{NSLocalizedDescriptionKey : errMsg};
- errorOccured = YES;
- thisError = [NSError errorWithDomain:GCDAsyncSocketErrorDomain code:GCDAsyncSocketOtherError userInfo:userInfo];
- return;
- }
-
- if (addr.sa_family == AF_INET)
- {
- socket->socket4FD = socketFD;
- }
- else if (addr.sa_family == AF_INET6)
- {
- socket->socket6FD = socketFD;
- }
- else
- {
- NSString *errMsg = NSLocalizedStringWithDefaultValue(@"GCDAsyncSocketOtherError",
- @"GCDAsyncSocket", [NSBundle mainBundle],
- @"Attempt to create socket from socket FD failed. socket FD is neither IPv4 nor IPv6", nil);
-
- NSDictionary *userInfo = @{NSLocalizedDescriptionKey : errMsg};
-
- errorOccured = YES;
- thisError = [NSError errorWithDomain:GCDAsyncSocketErrorDomain code:GCDAsyncSocketOtherError userInfo:userInfo];
- return;
- }
-
- socket->flags = kSocketStarted;
- [socket didConnect:socket->stateIndex];
- }});
-
- if (error && thisError) {
- *error = thisError;
- }
-
- return errorOccured? nil: socket;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #pragma mark Configuration
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- - (id)delegate
- {
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- {
- return delegate;
- }
- else
- {
- __block id result;
-
- dispatch_sync(socketQueue, ^{
- result = self->delegate;
- });
-
- return result;
- }
- }
- - (void)setDelegate:(id)newDelegate synchronously:(BOOL)synchronously
- {
- dispatch_block_t block = ^{
- self->delegate = newDelegate;
- };
-
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey)) {
- block();
- }
- else {
- if (synchronously)
- dispatch_sync(socketQueue, block);
- else
- dispatch_async(socketQueue, block);
- }
- }
- - (void)setDelegate:(id<GCDAsyncSocketDelegate>)newDelegate
- {
- [self setDelegate:newDelegate synchronously:NO];
- }
- - (void)synchronouslySetDelegate:(id<GCDAsyncSocketDelegate>)newDelegate
- {
- [self setDelegate:newDelegate synchronously:YES];
- }
- - (dispatch_queue_t)delegateQueue
- {
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- {
- return delegateQueue;
- }
- else
- {
- __block dispatch_queue_t result;
-
- dispatch_sync(socketQueue, ^{
- result = self->delegateQueue;
- });
-
- return result;
- }
- }
- - (void)setDelegateQueue:(dispatch_queue_t)newDelegateQueue synchronously:(BOOL)synchronously
- {
- dispatch_block_t block = ^{
-
- #if !OS_OBJECT_USE_OBJC
- if (self->delegateQueue) dispatch_release(self->delegateQueue);
- if (newDelegateQueue) dispatch_retain(newDelegateQueue);
- #endif
-
- self->delegateQueue = newDelegateQueue;
- };
-
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey)) {
- block();
- }
- else {
- if (synchronously)
- dispatch_sync(socketQueue, block);
- else
- dispatch_async(socketQueue, block);
- }
- }
- - (void)setDelegateQueue:(dispatch_queue_t)newDelegateQueue
- {
- [self setDelegateQueue:newDelegateQueue synchronously:NO];
- }
- - (void)synchronouslySetDelegateQueue:(dispatch_queue_t)newDelegateQueue
- {
- [self setDelegateQueue:newDelegateQueue synchronously:YES];
- }
- - (void)getDelegate:(id<GCDAsyncSocketDelegate> *)delegatePtr delegateQueue:(dispatch_queue_t *)delegateQueuePtr
- {
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- {
- if (delegatePtr) *delegatePtr = delegate;
- if (delegateQueuePtr) *delegateQueuePtr = delegateQueue;
- }
- else
- {
- __block id dPtr = NULL;
- __block dispatch_queue_t dqPtr = NULL;
-
- dispatch_sync(socketQueue, ^{
- dPtr = self->delegate;
- dqPtr = self->delegateQueue;
- });
-
- if (delegatePtr) *delegatePtr = dPtr;
- if (delegateQueuePtr) *delegateQueuePtr = dqPtr;
- }
- }
- - (void)setDelegate:(id)newDelegate delegateQueue:(dispatch_queue_t)newDelegateQueue synchronously:(BOOL)synchronously
- {
- dispatch_block_t block = ^{
-
- self->delegate = newDelegate;
-
- #if !OS_OBJECT_USE_OBJC
- if (self->delegateQueue) dispatch_release(self->delegateQueue);
- if (newDelegateQueue) dispatch_retain(newDelegateQueue);
- #endif
-
- self->delegateQueue = newDelegateQueue;
- };
-
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey)) {
- block();
- }
- else {
- if (synchronously)
- dispatch_sync(socketQueue, block);
- else
- dispatch_async(socketQueue, block);
- }
- }
- - (void)setDelegate:(id<GCDAsyncSocketDelegate>)newDelegate delegateQueue:(dispatch_queue_t)newDelegateQueue
- {
- [self setDelegate:newDelegate delegateQueue:newDelegateQueue synchronously:NO];
- }
- - (void)synchronouslySetDelegate:(id<GCDAsyncSocketDelegate>)newDelegate delegateQueue:(dispatch_queue_t)newDelegateQueue
- {
- [self setDelegate:newDelegate delegateQueue:newDelegateQueue synchronously:YES];
- }
- - (BOOL)isIPv4Enabled
- {
- // Note: YES means kIPv4Disabled is OFF
-
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- {
- return ((config & kIPv4Disabled) == 0);
- }
- else
- {
- __block BOOL result;
-
- dispatch_sync(socketQueue, ^{
- result = ((self->config & kIPv4Disabled) == 0);
- });
-
- return result;
- }
- }
- - (void)setIPv4Enabled:(BOOL)flag
- {
- // Note: YES means kIPv4Disabled is OFF
-
- dispatch_block_t block = ^{
-
- if (flag)
- self->config &= ~kIPv4Disabled;
- else
- self->config |= kIPv4Disabled;
- };
-
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- block();
- else
- dispatch_async(socketQueue, block);
- }
- - (BOOL)isIPv6Enabled
- {
- // Note: YES means kIPv6Disabled is OFF
-
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- {
- return ((config & kIPv6Disabled) == 0);
- }
- else
- {
- __block BOOL result;
-
- dispatch_sync(socketQueue, ^{
- result = ((self->config & kIPv6Disabled) == 0);
- });
-
- return result;
- }
- }
- - (void)setIPv6Enabled:(BOOL)flag
- {
- // Note: YES means kIPv6Disabled is OFF
-
- dispatch_block_t block = ^{
-
- if (flag)
- self->config &= ~kIPv6Disabled;
- else
- self->config |= kIPv6Disabled;
- };
-
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- block();
- else
- dispatch_async(socketQueue, block);
- }
- - (BOOL)isIPv4PreferredOverIPv6
- {
- // Note: YES means kPreferIPv6 is OFF
-
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- {
- return ((config & kPreferIPv6) == 0);
- }
- else
- {
- __block BOOL result;
-
- dispatch_sync(socketQueue, ^{
- result = ((self->config & kPreferIPv6) == 0);
- });
-
- return result;
- }
- }
- - (void)setIPv4PreferredOverIPv6:(BOOL)flag
- {
- // Note: YES means kPreferIPv6 is OFF
-
- dispatch_block_t block = ^{
-
- if (flag)
- self->config &= ~kPreferIPv6;
- else
- self->config |= kPreferIPv6;
- };
-
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- block();
- else
- dispatch_async(socketQueue, block);
- }
- - (NSTimeInterval) alternateAddressDelay {
- __block NSTimeInterval delay;
- dispatch_block_t block = ^{
- delay = self->alternateAddressDelay;
- };
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- block();
- else
- dispatch_sync(socketQueue, block);
- return delay;
- }
- - (void) setAlternateAddressDelay:(NSTimeInterval)delay {
- dispatch_block_t block = ^{
- self->alternateAddressDelay = delay;
- };
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- block();
- else
- dispatch_async(socketQueue, block);
- }
- - (id)userData
- {
- __block id result = nil;
-
- dispatch_block_t block = ^{
-
- result = self->userData;
- };
-
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- block();
- else
- dispatch_sync(socketQueue, block);
-
- return result;
- }
- - (void)setUserData:(id)arbitraryUserData
- {
- dispatch_block_t block = ^{
-
- if (self->userData != arbitraryUserData)
- {
- self->userData = arbitraryUserData;
- }
- };
-
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- block();
- else
- dispatch_async(socketQueue, block);
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #pragma mark Accepting
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- - (BOOL)acceptOnPort:(uint16_t)port error:(NSError **)errPtr
- {
- return [self acceptOnInterface:nil port:port error:errPtr];
- }
- - (BOOL)acceptOnInterface:(NSString *)inInterface port:(uint16_t)port error:(NSError **)errPtr
- {
- LogTrace();
-
- // Just in-case interface parameter is immutable.
- NSString *interface = [inInterface copy];
-
- __block BOOL result = NO;
- __block NSError *err = nil;
-
- // CreateSocket Block
- // This block will be invoked within the dispatch block below.
-
- int(^createSocket)(int, NSData*) = ^int (int domain, NSData *interfaceAddr) {
-
- int socketFD = socket(domain, SOCK_STREAM, 0);
-
- if (socketFD == SOCKET_NULL)
- {
- NSString *reason = @"Error in socket() function";
- err = [self errorWithErrno:errno reason:reason];
-
- return SOCKET_NULL;
- }
-
- int status;
-
- // Set socket options
-
- status = fcntl(socketFD, F_SETFL, O_NONBLOCK);
- if (status == -1)
- {
- NSString *reason = @"Error enabling non-blocking IO on socket (fcntl)";
- err = [self errorWithErrno:errno reason:reason];
-
- LogVerbose(@"close(socketFD)");
- close(socketFD);
- return SOCKET_NULL;
- }
-
- int reuseOn = 1;
- status = setsockopt(socketFD, SOL_SOCKET, SO_REUSEADDR, &reuseOn, sizeof(reuseOn));
- if (status == -1)
- {
- NSString *reason = @"Error enabling address reuse (setsockopt)";
- err = [self errorWithErrno:errno reason:reason];
-
- LogVerbose(@"close(socketFD)");
- close(socketFD);
- return SOCKET_NULL;
- }
-
- // Bind socket
-
- status = bind(socketFD, (const struct sockaddr *)[interfaceAddr bytes], (socklen_t)[interfaceAddr length]);
- if (status == -1)
- {
- NSString *reason = @"Error in bind() function";
- err = [self errorWithErrno:errno reason:reason];
-
- LogVerbose(@"close(socketFD)");
- close(socketFD);
- return SOCKET_NULL;
- }
-
- // Listen
-
- status = listen(socketFD, 1024);
- if (status == -1)
- {
- NSString *reason = @"Error in listen() function";
- err = [self errorWithErrno:errno reason:reason];
-
- LogVerbose(@"close(socketFD)");
- close(socketFD);
- return SOCKET_NULL;
- }
-
- return socketFD;
- };
-
- // Create dispatch block and run on socketQueue
-
- dispatch_block_t block = ^{ @autoreleasepool {
-
- if (self->delegate == nil) // Must have delegate set
- {
- NSString *msg = @"Attempting to accept without a delegate. Set a delegate first.";
- err = [self badConfigError:msg];
-
- return_from_block;
- }
-
- if (self->delegateQueue == NULL) // Must have delegate queue set
- {
- NSString *msg = @"Attempting to accept without a delegate queue. Set a delegate queue first.";
- err = [self badConfigError:msg];
-
- return_from_block;
- }
-
- BOOL isIPv4Disabled = (self->config & kIPv4Disabled) ? YES : NO;
- BOOL isIPv6Disabled = (self->config & kIPv6Disabled) ? YES : NO;
-
- if (isIPv4Disabled && isIPv6Disabled) // Must have IPv4 or IPv6 enabled
- {
- NSString *msg = @"Both IPv4 and IPv6 have been disabled. Must enable at least one protocol first.";
- err = [self badConfigError:msg];
-
- return_from_block;
- }
-
- if (![self isDisconnected]) // Must be disconnected
- {
- NSString *msg = @"Attempting to accept while connected or accepting connections. Disconnect first.";
- err = [self badConfigError:msg];
-
- return_from_block;
- }
-
- // Clear queues (spurious read/write requests post disconnect)
- [self->readQueue removeAllObjects];
- [self->writeQueue removeAllObjects];
-
- // Resolve interface from description
-
- NSMutableData *interface4 = nil;
- NSMutableData *interface6 = nil;
-
- [self getInterfaceAddress4:&interface4 address6:&interface6 fromDescription:interface port:port];
-
- if ((interface4 == nil) && (interface6 == nil))
- {
- NSString *msg = @"Unknown interface. Specify valid interface by name (e.g. \"en1\") or IP address.";
- err = [self badParamError:msg];
-
- return_from_block;
- }
-
- if (isIPv4Disabled && (interface6 == nil))
- {
- NSString *msg = @"IPv4 has been disabled and specified interface doesn't support IPv6.";
- err = [self badParamError:msg];
-
- return_from_block;
- }
-
- if (isIPv6Disabled && (interface4 == nil))
- {
- NSString *msg = @"IPv6 has been disabled and specified interface doesn't support IPv4.";
- err = [self badParamError:msg];
-
- return_from_block;
- }
-
- BOOL enableIPv4 = !isIPv4Disabled && (interface4 != nil);
- BOOL enableIPv6 = !isIPv6Disabled && (interface6 != nil);
-
- // Create sockets, configure, bind, and listen
-
- if (enableIPv4)
- {
- LogVerbose(@"Creating IPv4 socket");
- self->socket4FD = createSocket(AF_INET, interface4);
-
- if (self->socket4FD == SOCKET_NULL)
- {
- return_from_block;
- }
- }
-
- if (enableIPv6)
- {
- LogVerbose(@"Creating IPv6 socket");
-
- if (enableIPv4 && (port == 0))
- {
- // No specific port was specified, so we allowed the OS to pick an available port for us.
- // Now we need to make sure the IPv6 socket listens on the same port as the IPv4 socket.
-
- struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)[interface6 mutableBytes];
- addr6->sin6_port = htons([self localPort4]);
- }
-
- self->socket6FD = createSocket(AF_INET6, interface6);
-
- if (self->socket6FD == SOCKET_NULL)
- {
- if (self->socket4FD != SOCKET_NULL)
- {
- LogVerbose(@"close(socket4FD)");
- close(self->socket4FD);
- self->socket4FD = SOCKET_NULL;
- }
-
- return_from_block;
- }
- }
-
- // Create accept sources
-
- if (enableIPv4)
- {
- self->accept4Source = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, self->socket4FD, 0, self->socketQueue);
-
- int socketFD = self->socket4FD;
- dispatch_source_t acceptSource = self->accept4Source;
-
- __weak GCDAsyncSocket *weakSelf = self;
-
- dispatch_source_set_event_handler(self->accept4Source, ^{ @autoreleasepool {
- #pragma clang diagnostic push
- #pragma clang diagnostic warning "-Wimplicit-retain-self"
-
- __strong GCDAsyncSocket *strongSelf = weakSelf;
- if (strongSelf == nil) return_from_block;
-
- LogVerbose(@"event4Block");
-
- unsigned long i = 0;
- unsigned long numPendingConnections = dispatch_source_get_data(acceptSource);
-
- LogVerbose(@"numPendingConnections: %lu", numPendingConnections);
-
- while ([strongSelf doAccept:socketFD] && (++i < numPendingConnections));
-
- #pragma clang diagnostic pop
- }});
-
-
- dispatch_source_set_cancel_handler(self->accept4Source, ^{
- #pragma clang diagnostic push
- #pragma clang diagnostic warning "-Wimplicit-retain-self"
-
- #if !OS_OBJECT_USE_OBJC
- LogVerbose(@"dispatch_release(accept4Source)");
- dispatch_release(acceptSource);
- #endif
-
- LogVerbose(@"close(socket4FD)");
- close(socketFD);
-
- #pragma clang diagnostic pop
- });
-
- LogVerbose(@"dispatch_resume(accept4Source)");
- dispatch_resume(self->accept4Source);
- }
-
- if (enableIPv6)
- {
- self->accept6Source = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, self->socket6FD, 0, self->socketQueue);
-
- int socketFD = self->socket6FD;
- dispatch_source_t acceptSource = self->accept6Source;
-
- __weak GCDAsyncSocket *weakSelf = self;
-
- dispatch_source_set_event_handler(self->accept6Source, ^{ @autoreleasepool {
- #pragma clang diagnostic push
- #pragma clang diagnostic warning "-Wimplicit-retain-self"
-
- __strong GCDAsyncSocket *strongSelf = weakSelf;
- if (strongSelf == nil) return_from_block;
-
- LogVerbose(@"event6Block");
-
- unsigned long i = 0;
- unsigned long numPendingConnections = dispatch_source_get_data(acceptSource);
-
- LogVerbose(@"numPendingConnections: %lu", numPendingConnections);
-
- while ([strongSelf doAccept:socketFD] && (++i < numPendingConnections));
-
- #pragma clang diagnostic pop
- }});
-
- dispatch_source_set_cancel_handler(self->accept6Source, ^{
- #pragma clang diagnostic push
- #pragma clang diagnostic warning "-Wimplicit-retain-self"
-
- #if !OS_OBJECT_USE_OBJC
- LogVerbose(@"dispatch_release(accept6Source)");
- dispatch_release(acceptSource);
- #endif
-
- LogVerbose(@"close(socket6FD)");
- close(socketFD);
-
- #pragma clang diagnostic pop
- });
-
- LogVerbose(@"dispatch_resume(accept6Source)");
- dispatch_resume(self->accept6Source);
- }
-
- self->flags |= kSocketStarted;
-
- result = YES;
- }};
-
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- block();
- else
- dispatch_sync(socketQueue, block);
-
- if (result == NO)
- {
- LogInfo(@"Error in accept: %@", err);
-
- if (errPtr)
- *errPtr = err;
- }
-
- return result;
- }
- - (BOOL)acceptOnUrl:(NSURL *)url error:(NSError **)errPtr
- {
- LogTrace();
-
- __block BOOL result = NO;
- __block NSError *err = nil;
-
- // CreateSocket Block
- // This block will be invoked within the dispatch block below.
-
- int(^createSocket)(int, NSData*) = ^int (int domain, NSData *interfaceAddr) {
-
- int socketFD = socket(domain, SOCK_STREAM, 0);
-
- if (socketFD == SOCKET_NULL)
- {
- NSString *reason = @"Error in socket() function";
- err = [self errorWithErrno:errno reason:reason];
-
- return SOCKET_NULL;
- }
-
- int status;
-
- // Set socket options
-
- status = fcntl(socketFD, F_SETFL, O_NONBLOCK);
- if (status == -1)
- {
- NSString *reason = @"Error enabling non-blocking IO on socket (fcntl)";
- err = [self errorWithErrno:errno reason:reason];
-
- LogVerbose(@"close(socketFD)");
- close(socketFD);
- return SOCKET_NULL;
- }
-
- int reuseOn = 1;
- status = setsockopt(socketFD, SOL_SOCKET, SO_REUSEADDR, &reuseOn, sizeof(reuseOn));
- if (status == -1)
- {
- NSString *reason = @"Error enabling address reuse (setsockopt)";
- err = [self errorWithErrno:errno reason:reason];
-
- LogVerbose(@"close(socketFD)");
- close(socketFD);
- return SOCKET_NULL;
- }
-
- // Bind socket
-
- status = bind(socketFD, (const struct sockaddr *)[interfaceAddr bytes], (socklen_t)[interfaceAddr length]);
- if (status == -1)
- {
- NSString *reason = @"Error in bind() function";
- err = [self errorWithErrno:errno reason:reason];
-
- LogVerbose(@"close(socketFD)");
- close(socketFD);
- return SOCKET_NULL;
- }
-
- // Listen
-
- status = listen(socketFD, 1024);
- if (status == -1)
- {
- NSString *reason = @"Error in listen() function";
- err = [self errorWithErrno:errno reason:reason];
-
- LogVerbose(@"close(socketFD)");
- close(socketFD);
- return SOCKET_NULL;
- }
-
- return socketFD;
- };
-
- // Create dispatch block and run on socketQueue
-
- dispatch_block_t block = ^{ @autoreleasepool {
-
- if (self->delegate == nil) // Must have delegate set
- {
- NSString *msg = @"Attempting to accept without a delegate. Set a delegate first.";
- err = [self badConfigError:msg];
-
- return_from_block;
- }
-
- if (self->delegateQueue == NULL) // Must have delegate queue set
- {
- NSString *msg = @"Attempting to accept without a delegate queue. Set a delegate queue first.";
- err = [self badConfigError:msg];
-
- return_from_block;
- }
-
- if (![self isDisconnected]) // Must be disconnected
- {
- NSString *msg = @"Attempting to accept while connected or accepting connections. Disconnect first.";
- err = [self badConfigError:msg];
-
- return_from_block;
- }
-
- // Clear queues (spurious read/write requests post disconnect)
- [self->readQueue removeAllObjects];
- [self->writeQueue removeAllObjects];
-
- // Remove a previous socket
-
- NSError *error = nil;
- NSFileManager *fileManager = [NSFileManager defaultManager];
- NSString *urlPath = url.path;
- if (urlPath && [fileManager fileExistsAtPath:urlPath]) {
- if (![fileManager removeItemAtURL:url error:&error]) {
- NSString *msg = @"Could not remove previous unix domain socket at given url.";
- err = [self otherError:msg];
-
- return_from_block;
- }
- }
-
- // Resolve interface from description
-
- NSData *interface = [self getInterfaceAddressFromUrl:url];
-
- if (interface == nil)
- {
- NSString *msg = @"Invalid unix domain url. Specify a valid file url that does not exist (e.g. \"file:///tmp/socket\")";
- err = [self badParamError:msg];
-
- return_from_block;
- }
-
- // Create sockets, configure, bind, and listen
-
- LogVerbose(@"Creating unix domain socket");
- self->socketUN = createSocket(AF_UNIX, interface);
-
- if (self->socketUN == SOCKET_NULL)
- {
- return_from_block;
- }
-
- self->socketUrl = url;
-
- // Create accept sources
-
- self->acceptUNSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, self->socketUN, 0, self->socketQueue);
-
- int socketFD = self->socketUN;
- dispatch_source_t acceptSource = self->acceptUNSource;
-
- __weak GCDAsyncSocket *weakSelf = self;
-
- dispatch_source_set_event_handler(self->acceptUNSource, ^{ @autoreleasepool {
-
- __strong GCDAsyncSocket *strongSelf = weakSelf;
-
- LogVerbose(@"eventUNBlock");
-
- unsigned long i = 0;
- unsigned long numPendingConnections = dispatch_source_get_data(acceptSource);
-
- LogVerbose(@"numPendingConnections: %lu", numPendingConnections);
-
- while ([strongSelf doAccept:socketFD] && (++i < numPendingConnections));
- }});
-
- dispatch_source_set_cancel_handler(self->acceptUNSource, ^{
-
- #if !OS_OBJECT_USE_OBJC
- LogVerbose(@"dispatch_release(acceptUNSource)");
- dispatch_release(acceptSource);
- #endif
-
- LogVerbose(@"close(socketUN)");
- close(socketFD);
- });
-
- LogVerbose(@"dispatch_resume(acceptUNSource)");
- dispatch_resume(self->acceptUNSource);
-
- self->flags |= kSocketStarted;
-
- result = YES;
- }};
-
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- block();
- else
- dispatch_sync(socketQueue, block);
-
- if (result == NO)
- {
- LogInfo(@"Error in accept: %@", err);
-
- if (errPtr)
- *errPtr = err;
- }
-
- return result;
- }
- - (BOOL)doAccept:(int)parentSocketFD
- {
- LogTrace();
-
- int socketType;
- int childSocketFD;
- NSData *childSocketAddress;
-
- if (parentSocketFD == socket4FD)
- {
- socketType = 0;
-
- struct sockaddr_in addr;
- socklen_t addrLen = sizeof(addr);
-
- childSocketFD = accept(parentSocketFD, (struct sockaddr *)&addr, &addrLen);
-
- if (childSocketFD == -1)
- {
- LogWarn(@"Accept failed with error: %@", [self errnoError]);
- return NO;
- }
-
- childSocketAddress = [NSData dataWithBytes:&addr length:addrLen];
- }
- else if (parentSocketFD == socket6FD)
- {
- socketType = 1;
-
- struct sockaddr_in6 addr;
- socklen_t addrLen = sizeof(addr);
-
- childSocketFD = accept(parentSocketFD, (struct sockaddr *)&addr, &addrLen);
-
- if (childSocketFD == -1)
- {
- LogWarn(@"Accept failed with error: %@", [self errnoError]);
- return NO;
- }
-
- childSocketAddress = [NSData dataWithBytes:&addr length:addrLen];
- }
- else // if (parentSocketFD == socketUN)
- {
- socketType = 2;
-
- struct sockaddr_un addr;
- socklen_t addrLen = sizeof(addr);
-
- childSocketFD = accept(parentSocketFD, (struct sockaddr *)&addr, &addrLen);
-
- if (childSocketFD == -1)
- {
- LogWarn(@"Accept failed with error: %@", [self errnoError]);
- return NO;
- }
-
- childSocketAddress = [NSData dataWithBytes:&addr length:addrLen];
- }
-
- // Enable non-blocking IO on the socket
-
- int result = fcntl(childSocketFD, F_SETFL, O_NONBLOCK);
- if (result == -1)
- {
- LogWarn(@"Error enabling non-blocking IO on accepted socket (fcntl)");
- LogVerbose(@"close(childSocketFD)");
- close(childSocketFD);
- return NO;
- }
-
- // Prevent SIGPIPE signals
-
- int nosigpipe = 1;
- setsockopt(childSocketFD, SOL_SOCKET, SO_NOSIGPIPE, &nosigpipe, sizeof(nosigpipe));
-
- // Notify delegate
-
- if (delegateQueue)
- {
- __strong id<GCDAsyncSocketDelegate> theDelegate = delegate;
-
- dispatch_async(delegateQueue, ^{ @autoreleasepool {
-
- // Query delegate for custom socket queue
-
- dispatch_queue_t childSocketQueue = NULL;
-
- if ([theDelegate respondsToSelector:@selector(newSocketQueueForConnectionFromAddress:onSocket:)])
- {
- childSocketQueue = [theDelegate newSocketQueueForConnectionFromAddress:childSocketAddress
- onSocket:self];
- }
-
- // Create GCDAsyncSocket instance for accepted socket
-
- GCDAsyncSocket *acceptedSocket = [[[self class] alloc] initWithDelegate:theDelegate
- delegateQueue:self->delegateQueue
- socketQueue:childSocketQueue];
-
- if (socketType == 0)
- acceptedSocket->socket4FD = childSocketFD;
- else if (socketType == 1)
- acceptedSocket->socket6FD = childSocketFD;
- else
- acceptedSocket->socketUN = childSocketFD;
-
- acceptedSocket->flags = (kSocketStarted | kConnected);
-
- // Setup read and write sources for accepted socket
-
- dispatch_async(acceptedSocket->socketQueue, ^{ @autoreleasepool {
-
- [acceptedSocket setupReadAndWriteSourcesForNewlyConnectedSocket:childSocketFD];
- }});
-
- // Notify delegate
-
- if ([theDelegate respondsToSelector:@selector(socket:didAcceptNewSocket:)])
- {
- [theDelegate socket:self didAcceptNewSocket:acceptedSocket];
- }
-
- // Release the socket queue returned from the delegate (it was retained by acceptedSocket)
- #if !OS_OBJECT_USE_OBJC
- if (childSocketQueue) dispatch_release(childSocketQueue);
- #endif
-
- // The accepted socket should have been retained by the delegate.
- // Otherwise it gets properly released when exiting the block.
- }});
- }
-
- return YES;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #pragma mark Connecting
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /**
- * This method runs through the various checks required prior to a connection attempt.
- * It is shared between the connectToHost and connectToAddress methods.
- *
- **/
- - (BOOL)preConnectWithInterface:(NSString *)interface error:(NSError **)errPtr
- {
- NSAssert(dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey), @"Must be dispatched on socketQueue");
-
- if (delegate == nil) // Must have delegate set
- {
- if (errPtr)
- {
- NSString *msg = @"Attempting to connect without a delegate. Set a delegate first.";
- *errPtr = [self badConfigError:msg];
- }
- return NO;
- }
-
- if (delegateQueue == NULL) // Must have delegate queue set
- {
- if (errPtr)
- {
- NSString *msg = @"Attempting to connect without a delegate queue. Set a delegate queue first.";
- *errPtr = [self badConfigError:msg];
- }
- return NO;
- }
-
- if (![self isDisconnected]) // Must be disconnected
- {
- if (errPtr)
- {
- NSString *msg = @"Attempting to connect while connected or accepting connections. Disconnect first.";
- *errPtr = [self badConfigError:msg];
- }
- return NO;
- }
-
- BOOL isIPv4Disabled = (config & kIPv4Disabled) ? YES : NO;
- BOOL isIPv6Disabled = (config & kIPv6Disabled) ? YES : NO;
-
- if (isIPv4Disabled && isIPv6Disabled) // Must have IPv4 or IPv6 enabled
- {
- if (errPtr)
- {
- NSString *msg = @"Both IPv4 and IPv6 have been disabled. Must enable at least one protocol first.";
- *errPtr = [self badConfigError:msg];
- }
- return NO;
- }
-
- if (interface)
- {
- NSMutableData *interface4 = nil;
- NSMutableData *interface6 = nil;
-
- [self getInterfaceAddress4:&interface4 address6:&interface6 fromDescription:interface port:0];
-
- if ((interface4 == nil) && (interface6 == nil))
- {
- if (errPtr)
- {
- NSString *msg = @"Unknown interface. Specify valid interface by name (e.g. \"en1\") or IP address.";
- *errPtr = [self badParamError:msg];
- }
- return NO;
- }
-
- if (isIPv4Disabled && (interface6 == nil))
- {
- if (errPtr)
- {
- NSString *msg = @"IPv4 has been disabled and specified interface doesn't support IPv6.";
- *errPtr = [self badParamError:msg];
- }
- return NO;
- }
-
- if (isIPv6Disabled && (interface4 == nil))
- {
- if (errPtr)
- {
- NSString *msg = @"IPv6 has been disabled and specified interface doesn't support IPv4.";
- *errPtr = [self badParamError:msg];
- }
- return NO;
- }
-
- connectInterface4 = interface4;
- connectInterface6 = interface6;
- }
-
- // Clear queues (spurious read/write requests post disconnect)
- [readQueue removeAllObjects];
- [writeQueue removeAllObjects];
-
- return YES;
- }
- - (BOOL)preConnectWithUrl:(NSURL *)url error:(NSError **)errPtr
- {
- NSAssert(dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey), @"Must be dispatched on socketQueue");
-
- if (delegate == nil) // Must have delegate set
- {
- if (errPtr)
- {
- NSString *msg = @"Attempting to connect without a delegate. Set a delegate first.";
- *errPtr = [self badConfigError:msg];
- }
- return NO;
- }
-
- if (delegateQueue == NULL) // Must have delegate queue set
- {
- if (errPtr)
- {
- NSString *msg = @"Attempting to connect without a delegate queue. Set a delegate queue first.";
- *errPtr = [self badConfigError:msg];
- }
- return NO;
- }
-
- if (![self isDisconnected]) // Must be disconnected
- {
- if (errPtr)
- {
- NSString *msg = @"Attempting to connect while connected or accepting connections. Disconnect first.";
- *errPtr = [self badConfigError:msg];
- }
- return NO;
- }
-
- NSData *interface = [self getInterfaceAddressFromUrl:url];
-
- if (interface == nil)
- {
- if (errPtr)
- {
- NSString *msg = @"Unknown interface. Specify valid interface by name (e.g. \"en1\") or IP address.";
- *errPtr = [self badParamError:msg];
- }
- return NO;
- }
-
- connectInterfaceUN = interface;
-
- // Clear queues (spurious read/write requests post disconnect)
- [readQueue removeAllObjects];
- [writeQueue removeAllObjects];
-
- return YES;
- }
- - (BOOL)connectToHost:(NSString*)host onPort:(uint16_t)port error:(NSError **)errPtr
- {
- return [self connectToHost:host onPort:port withTimeout:-1 error:errPtr];
- }
- - (BOOL)connectToHost:(NSString *)host
- onPort:(uint16_t)port
- withTimeout:(NSTimeInterval)timeout
- error:(NSError **)errPtr
- {
- return [self connectToHost:host onPort:port viaInterface:nil withTimeout:timeout error:errPtr];
- }
- - (BOOL)connectToHost:(NSString *)inHost
- onPort:(uint16_t)port
- viaInterface:(NSString *)inInterface
- withTimeout:(NSTimeInterval)timeout
- error:(NSError **)errPtr
- {
- LogTrace();
-
- // Just in case immutable objects were passed
- NSString *host = [inHost copy];
- NSString *interface = [inInterface copy];
-
- __block BOOL result = NO;
- __block NSError *preConnectErr = nil;
-
- dispatch_block_t block = ^{ @autoreleasepool {
-
- // Check for problems with host parameter
-
- if ([host length] == 0)
- {
- NSString *msg = @"Invalid host parameter (nil or \"\"). Should be a domain name or IP address string.";
- preConnectErr = [self badParamError:msg];
-
- return_from_block;
- }
-
- // Run through standard pre-connect checks
-
- if (![self preConnectWithInterface:interface error:&preConnectErr])
- {
- return_from_block;
- }
-
- // We've made it past all the checks.
- // It's time to start the connection process.
-
- self->flags |= kSocketStarted;
-
- LogVerbose(@"Dispatching DNS lookup...");
-
- // It's possible that the given host parameter is actually a NSMutableString.
- // So we want to copy it now, within this block that will be executed synchronously.
- // This way the asynchronous lookup block below doesn't have to worry about it changing.
-
- NSString *hostCpy = [host copy];
-
- int aStateIndex = self->stateIndex;
- __weak GCDAsyncSocket *weakSelf = self;
-
- dispatch_queue_t globalConcurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
- dispatch_async(globalConcurrentQueue, ^{ @autoreleasepool {
- #pragma clang diagnostic push
- #pragma clang diagnostic warning "-Wimplicit-retain-self"
-
- NSError *lookupErr = nil;
- NSMutableArray *addresses = [[self class] lookupHost:hostCpy port:port error:&lookupErr];
-
- __strong GCDAsyncSocket *strongSelf = weakSelf;
- if (strongSelf == nil) return_from_block;
-
- if (lookupErr)
- {
- dispatch_async(strongSelf->socketQueue, ^{ @autoreleasepool {
-
- [strongSelf lookup:aStateIndex didFail:lookupErr];
- }});
- }
- else
- {
- NSData *address4 = nil;
- NSData *address6 = nil;
-
- for (NSData *address in addresses)
- {
- if (!address4 && [[self class] isIPv4Address:address])
- {
- address4 = address;
- }
- else if (!address6 && [[self class] isIPv6Address:address])
- {
- address6 = address;
- }
- }
-
- dispatch_async(strongSelf->socketQueue, ^{ @autoreleasepool {
-
- [strongSelf lookup:aStateIndex didSucceedWithAddress4:address4 address6:address6];
- }});
- }
-
- #pragma clang diagnostic pop
- }});
-
- [self startConnectTimeout:timeout];
-
- result = YES;
- }};
-
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- block();
- else
- dispatch_sync(socketQueue, block);
-
-
- if (errPtr) *errPtr = preConnectErr;
- return result;
- }
- - (BOOL)connectToAddress:(NSData *)remoteAddr error:(NSError **)errPtr
- {
- return [self connectToAddress:remoteAddr viaInterface:nil withTimeout:-1 error:errPtr];
- }
- - (BOOL)connectToAddress:(NSData *)remoteAddr withTimeout:(NSTimeInterval)timeout error:(NSError **)errPtr
- {
- return [self connectToAddress:remoteAddr viaInterface:nil withTimeout:timeout error:errPtr];
- }
- - (BOOL)connectToAddress:(NSData *)inRemoteAddr
- viaInterface:(NSString *)inInterface
- withTimeout:(NSTimeInterval)timeout
- error:(NSError **)errPtr
- {
- LogTrace();
-
- // Just in case immutable objects were passed
- NSData *remoteAddr = [inRemoteAddr copy];
- NSString *interface = [inInterface copy];
-
- __block BOOL result = NO;
- __block NSError *err = nil;
-
- dispatch_block_t block = ^{ @autoreleasepool {
-
- // Check for problems with remoteAddr parameter
-
- NSData *address4 = nil;
- NSData *address6 = nil;
-
- if ([remoteAddr length] >= sizeof(struct sockaddr))
- {
- const struct sockaddr *sockaddr = (const struct sockaddr *)[remoteAddr bytes];
-
- if (sockaddr->sa_family == AF_INET)
- {
- if ([remoteAddr length] == sizeof(struct sockaddr_in))
- {
- address4 = remoteAddr;
- }
- }
- else if (sockaddr->sa_family == AF_INET6)
- {
- if ([remoteAddr length] == sizeof(struct sockaddr_in6))
- {
- address6 = remoteAddr;
- }
- }
- }
-
- if ((address4 == nil) && (address6 == nil))
- {
- NSString *msg = @"A valid IPv4 or IPv6 address was not given";
- err = [self badParamError:msg];
-
- return_from_block;
- }
-
- BOOL isIPv4Disabled = (self->config & kIPv4Disabled) ? YES : NO;
- BOOL isIPv6Disabled = (self->config & kIPv6Disabled) ? YES : NO;
-
- if (isIPv4Disabled && (address4 != nil))
- {
- NSString *msg = @"IPv4 has been disabled and an IPv4 address was passed.";
- err = [self badParamError:msg];
-
- return_from_block;
- }
-
- if (isIPv6Disabled && (address6 != nil))
- {
- NSString *msg = @"IPv6 has been disabled and an IPv6 address was passed.";
- err = [self badParamError:msg];
-
- return_from_block;
- }
-
- // Run through standard pre-connect checks
-
- if (![self preConnectWithInterface:interface error:&err])
- {
- return_from_block;
- }
-
- // We've made it past all the checks.
- // It's time to start the connection process.
-
- if (![self connectWithAddress4:address4 address6:address6 error:&err])
- {
- return_from_block;
- }
-
- self->flags |= kSocketStarted;
-
- [self startConnectTimeout:timeout];
-
- result = YES;
- }};
-
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- block();
- else
- dispatch_sync(socketQueue, block);
-
- if (result == NO)
- {
- if (errPtr)
- *errPtr = err;
- }
-
- return result;
- }
- - (BOOL)connectToUrl:(NSURL *)url withTimeout:(NSTimeInterval)timeout error:(NSError **)errPtr
- {
- LogTrace();
-
- __block BOOL result = NO;
- __block NSError *err = nil;
-
- dispatch_block_t block = ^{ @autoreleasepool {
-
- // Check for problems with host parameter
-
- if ([url.path length] == 0)
- {
- NSString *msg = @"Invalid unix domain socket url.";
- err = [self badParamError:msg];
-
- return_from_block;
- }
-
- // Run through standard pre-connect checks
-
- if (![self preConnectWithUrl:url error:&err])
- {
- return_from_block;
- }
-
- // We've made it past all the checks.
- // It's time to start the connection process.
-
- self->flags |= kSocketStarted;
-
- // Start the normal connection process
-
- NSError *connectError = nil;
- if (![self connectWithAddressUN:self->connectInterfaceUN error:&connectError])
- {
- [self closeWithError:connectError];
-
- return_from_block;
- }
- [self startConnectTimeout:timeout];
-
- result = YES;
- }};
-
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- block();
- else
- dispatch_sync(socketQueue, block);
-
- if (result == NO)
- {
- if (errPtr)
- *errPtr = err;
- }
-
- return result;
- }
- - (BOOL)connectToNetService:(NSNetService *)netService error:(NSError **)errPtr
- {
- NSArray* addresses = [netService addresses];
- for (NSData* address in addresses)
- {
- BOOL result = [self connectToAddress:address error:errPtr];
- if (result)
- {
- return YES;
- }
- }
-
- return NO;
- }
- - (void)lookup:(int)aStateIndex didSucceedWithAddress4:(NSData *)address4 address6:(NSData *)address6
- {
- LogTrace();
-
- NSAssert(dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey), @"Must be dispatched on socketQueue");
- NSAssert(address4 || address6, @"Expected at least one valid address");
-
- if (aStateIndex != stateIndex)
- {
- LogInfo(@"Ignoring lookupDidSucceed, already disconnected");
-
- // The connect operation has been cancelled.
- // That is, socket was disconnected, or connection has already timed out.
- return;
- }
-
- // Check for problems
-
- BOOL isIPv4Disabled = (config & kIPv4Disabled) ? YES : NO;
- BOOL isIPv6Disabled = (config & kIPv6Disabled) ? YES : NO;
-
- if (isIPv4Disabled && (address6 == nil))
- {
- NSString *msg = @"IPv4 has been disabled and DNS lookup found no IPv6 address.";
-
- [self closeWithError:[self otherError:msg]];
- return;
- }
-
- if (isIPv6Disabled && (address4 == nil))
- {
- NSString *msg = @"IPv6 has been disabled and DNS lookup found no IPv4 address.";
-
- [self closeWithError:[self otherError:msg]];
- return;
- }
-
- // Start the normal connection process
-
- NSError *err = nil;
- if (![self connectWithAddress4:address4 address6:address6 error:&err])
- {
- [self closeWithError:err];
- }
- }
- /**
- * This method is called if the DNS lookup fails.
- * This method is executed on the socketQueue.
- *
- * Since the DNS lookup executed synchronously on a global concurrent queue,
- * the original connection request may have already been cancelled or timed-out by the time this method is invoked.
- * The lookupIndex tells us whether the lookup is still valid or not.
- **/
- - (void)lookup:(int)aStateIndex didFail:(NSError *)error
- {
- LogTrace();
-
- NSAssert(dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey), @"Must be dispatched on socketQueue");
-
-
- if (aStateIndex != stateIndex)
- {
- LogInfo(@"Ignoring lookup:didFail: - already disconnected");
-
- // The connect operation has been cancelled.
- // That is, socket was disconnected, or connection has already timed out.
- return;
- }
-
- [self endConnectTimeout];
- [self closeWithError:error];
- }
- - (BOOL)bindSocket:(int)socketFD toInterface:(NSData *)connectInterface error:(NSError **)errPtr
- {
- // Bind the socket to the desired interface (if needed)
-
- if (connectInterface)
- {
- LogVerbose(@"Binding socket...");
-
- if ([[self class] portFromAddress:connectInterface] > 0)
- {
- // Since we're going to be binding to a specific port,
- // we should turn on reuseaddr to allow us to override sockets in time_wait.
-
- int reuseOn = 1;
- setsockopt(socketFD, SOL_SOCKET, SO_REUSEADDR, &reuseOn, sizeof(reuseOn));
- }
-
- const struct sockaddr *interfaceAddr = (const struct sockaddr *)[connectInterface bytes];
-
- int result = bind(socketFD, interfaceAddr, (socklen_t)[connectInterface length]);
- if (result != 0)
- {
- if (errPtr)
- *errPtr = [self errorWithErrno:errno reason:@"Error in bind() function"];
-
- return NO;
- }
- }
-
- return YES;
- }
- - (int)createSocket:(int)family connectInterface:(NSData *)connectInterface errPtr:(NSError **)errPtr
- {
- int socketFD = socket(family, SOCK_STREAM, 0);
-
- if (socketFD == SOCKET_NULL)
- {
- if (errPtr)
- *errPtr = [self errorWithErrno:errno reason:@"Error in socket() function"];
-
- return socketFD;
- }
-
- if (![self bindSocket:socketFD toInterface:connectInterface error:errPtr])
- {
- [self closeSocket:socketFD];
-
- return SOCKET_NULL;
- }
-
- // Prevent SIGPIPE signals
-
- int nosigpipe = 1;
- setsockopt(socketFD, SOL_SOCKET, SO_NOSIGPIPE, &nosigpipe, sizeof(nosigpipe));
-
- return socketFD;
- }
- - (void)connectSocket:(int)socketFD address:(NSData *)address stateIndex:(int)aStateIndex
- {
- // If there already is a socket connected, we close socketFD and return
- if (self.isConnected)
- {
- [self closeSocket:socketFD];
- return;
- }
-
- // Start the connection process in a background queue
-
- __weak GCDAsyncSocket *weakSelf = self;
-
- dispatch_queue_t globalConcurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
- dispatch_async(globalConcurrentQueue, ^{
- #pragma clang diagnostic push
- #pragma clang diagnostic warning "-Wimplicit-retain-self"
-
- int result = connect(socketFD, (const struct sockaddr *)[address bytes], (socklen_t)[address length]);
- int err = errno;
-
- __strong GCDAsyncSocket *strongSelf = weakSelf;
- if (strongSelf == nil) return_from_block;
-
- dispatch_async(strongSelf->socketQueue, ^{ @autoreleasepool {
-
- if (strongSelf.isConnected)
- {
- [strongSelf closeSocket:socketFD];
- return_from_block;
- }
-
- if (result == 0)
- {
- [self closeUnusedSocket:socketFD];
-
- [strongSelf didConnect:aStateIndex];
- }
- else
- {
- [strongSelf closeSocket:socketFD];
-
- // If there are no more sockets trying to connect, we inform the error to the delegate
- if (strongSelf.socket4FD == SOCKET_NULL && strongSelf.socket6FD == SOCKET_NULL)
- {
- NSError *error = [strongSelf errorWithErrno:err reason:@"Error in connect() function"];
- [strongSelf didNotConnect:aStateIndex error:error];
- }
- }
- }});
-
- #pragma clang diagnostic pop
- });
-
- LogVerbose(@"Connecting...");
- }
- - (void)closeSocket:(int)socketFD
- {
- if (socketFD != SOCKET_NULL &&
- (socketFD == socket6FD || socketFD == socket4FD))
- {
- close(socketFD);
-
- if (socketFD == socket4FD)
- {
- LogVerbose(@"close(socket4FD)");
- socket4FD = SOCKET_NULL;
- }
- else if (socketFD == socket6FD)
- {
- LogVerbose(@"close(socket6FD)");
- socket6FD = SOCKET_NULL;
- }
- }
- }
- - (void)closeUnusedSocket:(int)usedSocketFD
- {
- if (usedSocketFD != socket4FD)
- {
- [self closeSocket:socket4FD];
- }
- else if (usedSocketFD != socket6FD)
- {
- [self closeSocket:socket6FD];
- }
- }
- - (BOOL)connectWithAddress4:(NSData *)address4 address6:(NSData *)address6 error:(NSError **)errPtr
- {
- LogTrace();
-
- NSAssert(dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey), @"Must be dispatched on socketQueue");
-
- LogVerbose(@"IPv4: %@:%hu", [[self class] hostFromAddress:address4], [[self class] portFromAddress:address4]);
- LogVerbose(@"IPv6: %@:%hu", [[self class] hostFromAddress:address6], [[self class] portFromAddress:address6]);
-
- // Determine socket type
-
- BOOL preferIPv6 = (config & kPreferIPv6) ? YES : NO;
-
- // Create and bind the sockets
-
- if (address4)
- {
- LogVerbose(@"Creating IPv4 socket");
-
- socket4FD = [self createSocket:AF_INET connectInterface:connectInterface4 errPtr:errPtr];
- }
-
- if (address6)
- {
- LogVerbose(@"Creating IPv6 socket");
-
- socket6FD = [self createSocket:AF_INET6 connectInterface:connectInterface6 errPtr:errPtr];
- }
-
- if (socket4FD == SOCKET_NULL && socket6FD == SOCKET_NULL)
- {
- return NO;
- }
-
- int socketFD, alternateSocketFD;
- NSData *address, *alternateAddress;
-
- if ((preferIPv6 && socket6FD != SOCKET_NULL) || socket4FD == SOCKET_NULL)
- {
- socketFD = socket6FD;
- alternateSocketFD = socket4FD;
- address = address6;
- alternateAddress = address4;
- }
- else
- {
- socketFD = socket4FD;
- alternateSocketFD = socket6FD;
- address = address4;
- alternateAddress = address6;
- }
- int aStateIndex = stateIndex;
-
- [self connectSocket:socketFD address:address stateIndex:aStateIndex];
-
- if (alternateAddress)
- {
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(alternateAddressDelay * NSEC_PER_SEC)), socketQueue, ^{
- [self connectSocket:alternateSocketFD address:alternateAddress stateIndex:aStateIndex];
- });
- }
-
- return YES;
- }
- - (BOOL)connectWithAddressUN:(NSData *)address error:(NSError **)errPtr
- {
- LogTrace();
-
- NSAssert(dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey), @"Must be dispatched on socketQueue");
-
- // Create the socket
-
- int socketFD;
-
- LogVerbose(@"Creating unix domain socket");
-
- socketUN = socket(AF_UNIX, SOCK_STREAM, 0);
-
- socketFD = socketUN;
-
- if (socketFD == SOCKET_NULL)
- {
- if (errPtr)
- *errPtr = [self errorWithErrno:errno reason:@"Error in socket() function"];
-
- return NO;
- }
-
- // Bind the socket to the desired interface (if needed)
-
- LogVerbose(@"Binding socket...");
-
- int reuseOn = 1;
- setsockopt(socketFD, SOL_SOCKET, SO_REUSEADDR, &reuseOn, sizeof(reuseOn));
- // const struct sockaddr *interfaceAddr = (const struct sockaddr *)[address bytes];
- //
- // int result = bind(socketFD, interfaceAddr, (socklen_t)[address length]);
- // if (result != 0)
- // {
- // if (errPtr)
- // *errPtr = [self errnoErrorWithReason:@"Error in bind() function"];
- //
- // return NO;
- // }
-
- // Prevent SIGPIPE signals
-
- int nosigpipe = 1;
- setsockopt(socketFD, SOL_SOCKET, SO_NOSIGPIPE, &nosigpipe, sizeof(nosigpipe));
-
- // Start the connection process in a background queue
-
- int aStateIndex = stateIndex;
-
- dispatch_queue_t globalConcurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
- dispatch_async(globalConcurrentQueue, ^{
-
- const struct sockaddr *addr = (const struct sockaddr *)[address bytes];
- int result = connect(socketFD, addr, addr->sa_len);
- if (result == 0)
- {
- dispatch_async(self->socketQueue, ^{ @autoreleasepool {
-
- [self didConnect:aStateIndex];
- }});
- }
- else
- {
- // TODO: Bad file descriptor
- perror("connect");
- NSError *error = [self errorWithErrno:errno reason:@"Error in connect() function"];
-
- dispatch_async(self->socketQueue, ^{ @autoreleasepool {
-
- [self didNotConnect:aStateIndex error:error];
- }});
- }
- });
-
- LogVerbose(@"Connecting...");
-
- return YES;
- }
- - (void)didConnect:(int)aStateIndex
- {
- LogTrace();
-
- NSAssert(dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey), @"Must be dispatched on socketQueue");
-
-
- if (aStateIndex != stateIndex)
- {
- LogInfo(@"Ignoring didConnect, already disconnected");
-
- // The connect operation has been cancelled.
- // That is, socket was disconnected, or connection has already timed out.
- return;
- }
-
- flags |= kConnected;
-
- [self endConnectTimeout];
-
- #if TARGET_OS_IPHONE
- // The endConnectTimeout method executed above incremented the stateIndex.
- aStateIndex = stateIndex;
- #endif
-
- // Setup read/write streams (as workaround for specific shortcomings in the iOS platform)
- //
- // Note:
- // There may be configuration options that must be set by the delegate before opening the streams.
- // The primary example is the kCFStreamNetworkServiceTypeVoIP flag, which only works on an unopened stream.
- //
- // Thus we wait until after the socket:didConnectToHost:port: delegate method has completed.
- // This gives the delegate time to properly configure the streams if needed.
-
- dispatch_block_t SetupStreamsPart1 = ^{
- #if TARGET_OS_IPHONE
-
- if (![self createReadAndWriteStream])
- {
- [self closeWithError:[self otherError:@"Error creating CFStreams"]];
- return;
- }
-
- if (![self registerForStreamCallbacksIncludingReadWrite:NO])
- {
- [self closeWithError:[self otherError:@"Error in CFStreamSetClient"]];
- return;
- }
-
- #endif
- };
- dispatch_block_t SetupStreamsPart2 = ^{
- #if TARGET_OS_IPHONE
-
- if (aStateIndex != self->stateIndex)
- {
- // The socket has been disconnected.
- return;
- }
-
- if (![self addStreamsToRunLoop])
- {
- [self closeWithError:[self otherError:@"Error in CFStreamScheduleWithRunLoop"]];
- return;
- }
-
- if (![self openStreams])
- {
- [self closeWithError:[self otherError:@"Error creating CFStreams"]];
- return;
- }
-
- #endif
- };
-
- // Notify delegate
-
- NSString *host = [self connectedHost];
- uint16_t port = [self connectedPort];
- NSURL *url = [self connectedUrl];
-
- __strong id<GCDAsyncSocketDelegate> theDelegate = delegate;
- if (delegateQueue && host != nil && [theDelegate respondsToSelector:@selector(socket:didConnectToHost:port:)])
- {
- SetupStreamsPart1();
-
- dispatch_async(delegateQueue, ^{ @autoreleasepool {
-
- [theDelegate socket:self didConnectToHost:host port:port];
-
- dispatch_async(self->socketQueue, ^{ @autoreleasepool {
-
- SetupStreamsPart2();
- }});
- }});
- }
- else if (delegateQueue && url != nil && [theDelegate respondsToSelector:@selector(socket:didConnectToUrl:)])
- {
- SetupStreamsPart1();
-
- dispatch_async(delegateQueue, ^{ @autoreleasepool {
-
- [theDelegate socket:self didConnectToUrl:url];
-
- dispatch_async(self->socketQueue, ^{ @autoreleasepool {
-
- SetupStreamsPart2();
- }});
- }});
- }
- else
- {
- SetupStreamsPart1();
- SetupStreamsPart2();
- }
-
- // Get the connected socket
-
- int socketFD = (socket4FD != SOCKET_NULL) ? socket4FD : (socket6FD != SOCKET_NULL) ? socket6FD : socketUN;
-
- // Enable non-blocking IO on the socket
-
- int result = fcntl(socketFD, F_SETFL, O_NONBLOCK);
- if (result == -1)
- {
- NSString *errMsg = @"Error enabling non-blocking IO on socket (fcntl)";
- [self closeWithError:[self otherError:errMsg]];
-
- return;
- }
-
- // Setup our read/write sources
-
- [self setupReadAndWriteSourcesForNewlyConnectedSocket:socketFD];
-
- // Dequeue any pending read/write requests
-
- [self maybeDequeueRead];
- [self maybeDequeueWrite];
- }
- - (void)didNotConnect:(int)aStateIndex error:(NSError *)error
- {
- LogTrace();
-
- NSAssert(dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey), @"Must be dispatched on socketQueue");
-
-
- if (aStateIndex != stateIndex)
- {
- LogInfo(@"Ignoring didNotConnect, already disconnected");
-
- // The connect operation has been cancelled.
- // That is, socket was disconnected, or connection has already timed out.
- return;
- }
-
- [self closeWithError:error];
- }
- - (void)startConnectTimeout:(NSTimeInterval)timeout
- {
- if (timeout >= 0.0)
- {
- connectTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, socketQueue);
-
- __weak GCDAsyncSocket *weakSelf = self;
-
- dispatch_source_set_event_handler(connectTimer, ^{ @autoreleasepool {
- #pragma clang diagnostic push
- #pragma clang diagnostic warning "-Wimplicit-retain-self"
-
- __strong GCDAsyncSocket *strongSelf = weakSelf;
- if (strongSelf == nil) return_from_block;
-
- [strongSelf doConnectTimeout];
-
- #pragma clang diagnostic pop
- }});
-
- #if !OS_OBJECT_USE_OBJC
- dispatch_source_t theConnectTimer = connectTimer;
- dispatch_source_set_cancel_handler(connectTimer, ^{
- #pragma clang diagnostic push
- #pragma clang diagnostic warning "-Wimplicit-retain-self"
-
- LogVerbose(@"dispatch_release(connectTimer)");
- dispatch_release(theConnectTimer);
-
- #pragma clang diagnostic pop
- });
- #endif
-
- dispatch_time_t tt = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeout * NSEC_PER_SEC));
- dispatch_source_set_timer(connectTimer, tt, DISPATCH_TIME_FOREVER, 0);
-
- dispatch_resume(connectTimer);
- }
- }
- - (void)endConnectTimeout
- {
- LogTrace();
-
- if (connectTimer)
- {
- dispatch_source_cancel(connectTimer);
- connectTimer = NULL;
- }
-
- // Increment stateIndex.
- // This will prevent us from processing results from any related background asynchronous operations.
- //
- // Note: This should be called from close method even if connectTimer is NULL.
- // This is because one might disconnect a socket prior to a successful connection which had no timeout.
-
- stateIndex++;
-
- if (connectInterface4)
- {
- connectInterface4 = nil;
- }
- if (connectInterface6)
- {
- connectInterface6 = nil;
- }
- }
- - (void)doConnectTimeout
- {
- LogTrace();
-
- [self endConnectTimeout];
- [self closeWithError:[self connectTimeoutError]];
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #pragma mark Disconnecting
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- - (void)closeWithError:(NSError *)error
- {
- LogTrace();
- NSAssert(dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey), @"Must be dispatched on socketQueue");
-
- [self endConnectTimeout];
-
- if (currentRead != nil) [self endCurrentRead];
- if (currentWrite != nil) [self endCurrentWrite];
-
- [readQueue removeAllObjects];
- [writeQueue removeAllObjects];
-
- [preBuffer reset];
-
- #if TARGET_OS_IPHONE
- {
- if (readStream || writeStream)
- {
- [self removeStreamsFromRunLoop];
-
- if (readStream)
- {
- CFReadStreamSetClient(readStream, kCFStreamEventNone, NULL, NULL);
- CFReadStreamClose(readStream);
- CFRelease(readStream);
- readStream = NULL;
- }
- if (writeStream)
- {
- CFWriteStreamSetClient(writeStream, kCFStreamEventNone, NULL, NULL);
- CFWriteStreamClose(writeStream);
- CFRelease(writeStream);
- writeStream = NULL;
- }
- }
- }
- #endif
-
- [sslPreBuffer reset];
- sslErrCode = lastSSLHandshakeError = noErr;
-
- if (sslContext)
- {
- // Getting a linker error here about the SSLx() functions?
- // You need to add the Security Framework to your application.
-
- SSLClose(sslContext);
-
- #if TARGET_OS_IPHONE || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 1080)
- CFRelease(sslContext);
- #else
- SSLDisposeContext(sslContext);
- #endif
-
- sslContext = NULL;
- }
-
- // For some crazy reason (in my opinion), cancelling a dispatch source doesn't
- // invoke the cancel handler if the dispatch source is paused.
- // So we have to unpause the source if needed.
- // This allows the cancel handler to be run, which in turn releases the source and closes the socket.
-
- if (!accept4Source && !accept6Source && !acceptUNSource && !readSource && !writeSource)
- {
- LogVerbose(@"manually closing close");
- if (socket4FD != SOCKET_NULL)
- {
- LogVerbose(@"close(socket4FD)");
- close(socket4FD);
- socket4FD = SOCKET_NULL;
- }
- if (socket6FD != SOCKET_NULL)
- {
- LogVerbose(@"close(socket6FD)");
- close(socket6FD);
- socket6FD = SOCKET_NULL;
- }
-
- if (socketUN != SOCKET_NULL)
- {
- LogVerbose(@"close(socketUN)");
- close(socketUN);
- socketUN = SOCKET_NULL;
- unlink(socketUrl.path.fileSystemRepresentation);
- socketUrl = nil;
- }
- }
- else
- {
- if (accept4Source)
- {
- LogVerbose(@"dispatch_source_cancel(accept4Source)");
- dispatch_source_cancel(accept4Source);
-
- // We never suspend accept4Source
-
- accept4Source = NULL;
- }
-
- if (accept6Source)
- {
- LogVerbose(@"dispatch_source_cancel(accept6Source)");
- dispatch_source_cancel(accept6Source);
-
- // We never suspend accept6Source
-
- accept6Source = NULL;
- }
-
- if (acceptUNSource)
- {
- LogVerbose(@"dispatch_source_cancel(acceptUNSource)");
- dispatch_source_cancel(acceptUNSource);
-
- // We never suspend acceptUNSource
-
- acceptUNSource = NULL;
- }
-
- if (readSource)
- {
- LogVerbose(@"dispatch_source_cancel(readSource)");
- dispatch_source_cancel(readSource);
-
- [self resumeReadSource];
-
- readSource = NULL;
- }
-
- if (writeSource)
- {
- LogVerbose(@"dispatch_source_cancel(writeSource)");
- dispatch_source_cancel(writeSource);
-
- [self resumeWriteSource];
-
- writeSource = NULL;
- }
-
- // The sockets will be closed by the cancel handlers of the corresponding source
-
- socket4FD = SOCKET_NULL;
- socket6FD = SOCKET_NULL;
- socketUN = SOCKET_NULL;
- }
-
- // If the client has passed the connect/accept method, then the connection has at least begun.
- // Notify delegate that it is now ending.
- BOOL shouldCallDelegate = (flags & kSocketStarted) ? YES : NO;
- BOOL isDeallocating = (flags & kDealloc) ? YES : NO;
-
- // Clear stored socket info and all flags (config remains as is)
- socketFDBytesAvailable = 0;
- flags = 0;
- sslWriteCachedLength = 0;
-
- if (shouldCallDelegate)
- {
- __strong id<GCDAsyncSocketDelegate> theDelegate = delegate;
- __strong id theSelf = isDeallocating ? nil : self;
-
- if (delegateQueue && [theDelegate respondsToSelector: @selector(socketDidDisconnect:withError:)])
- {
- dispatch_async(delegateQueue, ^{ @autoreleasepool {
-
- [theDelegate socketDidDisconnect:theSelf withError:error];
- }});
- }
- }
- }
- - (void)disconnect
- {
- dispatch_block_t block = ^{ @autoreleasepool {
-
- if (self->flags & kSocketStarted)
- {
- [self closeWithError:nil];
- }
- }};
-
- // Synchronous disconnection, as documented in the header file
-
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- block();
- else
- dispatch_sync(socketQueue, block);
- }
- - (void)disconnectAfterReading
- {
- dispatch_async(socketQueue, ^{ @autoreleasepool {
-
- if (self->flags & kSocketStarted)
- {
- self->flags |= (kForbidReadsWrites | kDisconnectAfterReads);
- [self maybeClose];
- }
- }});
- }
- - (void)disconnectAfterWriting
- {
- dispatch_async(socketQueue, ^{ @autoreleasepool {
-
- if (self->flags & kSocketStarted)
- {
- self->flags |= (kForbidReadsWrites | kDisconnectAfterWrites);
- [self maybeClose];
- }
- }});
- }
- - (void)disconnectAfterReadingAndWriting
- {
- dispatch_async(socketQueue, ^{ @autoreleasepool {
-
- if (self->flags & kSocketStarted)
- {
- self->flags |= (kForbidReadsWrites | kDisconnectAfterReads | kDisconnectAfterWrites);
- [self maybeClose];
- }
- }});
- }
- /**
- * Closes the socket if possible.
- * That is, if all writes have completed, and we're set to disconnect after writing,
- * or if all reads have completed, and we're set to disconnect after reading.
- **/
- - (void)maybeClose
- {
- NSAssert(dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey), @"Must be dispatched on socketQueue");
-
- BOOL shouldClose = NO;
-
- if (flags & kDisconnectAfterReads)
- {
- if (([readQueue count] == 0) && (currentRead == nil))
- {
- if (flags & kDisconnectAfterWrites)
- {
- if (([writeQueue count] == 0) && (currentWrite == nil))
- {
- shouldClose = YES;
- }
- }
- else
- {
- shouldClose = YES;
- }
- }
- }
- else if (flags & kDisconnectAfterWrites)
- {
- if (([writeQueue count] == 0) && (currentWrite == nil))
- {
- shouldClose = YES;
- }
- }
-
- if (shouldClose)
- {
- [self closeWithError:nil];
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #pragma mark Errors
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- - (NSError *)badConfigError:(NSString *)errMsg
- {
- NSDictionary *userInfo = @{NSLocalizedDescriptionKey : errMsg};
-
- return [NSError errorWithDomain:GCDAsyncSocketErrorDomain code:GCDAsyncSocketBadConfigError userInfo:userInfo];
- }
- - (NSError *)badParamError:(NSString *)errMsg
- {
- NSDictionary *userInfo = @{NSLocalizedDescriptionKey : errMsg};
-
- return [NSError errorWithDomain:GCDAsyncSocketErrorDomain code:GCDAsyncSocketBadParamError userInfo:userInfo];
- }
- + (NSError *)gaiError:(int)gai_error
- {
- NSString *errMsg = [NSString stringWithCString:gai_strerror(gai_error) encoding:NSASCIIStringEncoding];
- NSDictionary *userInfo = @{NSLocalizedDescriptionKey : errMsg};
-
- return [NSError errorWithDomain:@"kCFStreamErrorDomainNetDB" code:gai_error userInfo:userInfo];
- }
- - (NSError *)errorWithErrno:(int)err reason:(NSString *)reason
- {
- NSString *errMsg = [NSString stringWithUTF8String:strerror(err)];
- NSDictionary *userInfo = @{NSLocalizedDescriptionKey : errMsg,
- NSLocalizedFailureReasonErrorKey : reason};
-
- return [NSError errorWithDomain:NSPOSIXErrorDomain code:err userInfo:userInfo];
- }
- - (NSError *)errnoError
- {
- NSString *errMsg = [NSString stringWithUTF8String:strerror(errno)];
- NSDictionary *userInfo = @{NSLocalizedDescriptionKey : errMsg};
-
- return [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:userInfo];
- }
- - (NSError *)sslError:(OSStatus)ssl_error
- {
- NSString *msg = @"Error code definition can be found in Apple's SecureTransport.h";
- NSDictionary *userInfo = @{NSLocalizedRecoverySuggestionErrorKey : msg};
-
- return [NSError errorWithDomain:@"kCFStreamErrorDomainSSL" code:ssl_error userInfo:userInfo];
- }
- - (NSError *)connectTimeoutError
- {
- NSString *errMsg = NSLocalizedStringWithDefaultValue(@"GCDAsyncSocketConnectTimeoutError",
- @"GCDAsyncSocket", [NSBundle mainBundle],
- @"Attempt to connect to host timed out", nil);
-
- NSDictionary *userInfo = @{NSLocalizedDescriptionKey : errMsg};
-
- return [NSError errorWithDomain:GCDAsyncSocketErrorDomain code:GCDAsyncSocketConnectTimeoutError userInfo:userInfo];
- }
- /**
- * Returns a standard AsyncSocket maxed out error.
- **/
- - (NSError *)readMaxedOutError
- {
- NSString *errMsg = NSLocalizedStringWithDefaultValue(@"GCDAsyncSocketReadMaxedOutError",
- @"GCDAsyncSocket", [NSBundle mainBundle],
- @"Read operation reached set maximum length", nil);
-
- NSDictionary *info = @{NSLocalizedDescriptionKey : errMsg};
-
- return [NSError errorWithDomain:GCDAsyncSocketErrorDomain code:GCDAsyncSocketReadMaxedOutError userInfo:info];
- }
- /**
- * Returns a standard AsyncSocket write timeout error.
- **/
- - (NSError *)readTimeoutError
- {
- NSString *errMsg = NSLocalizedStringWithDefaultValue(@"GCDAsyncSocketReadTimeoutError",
- @"GCDAsyncSocket", [NSBundle mainBundle],
- @"Read operation timed out", nil);
-
- NSDictionary *userInfo = @{NSLocalizedDescriptionKey : errMsg};
-
- return [NSError errorWithDomain:GCDAsyncSocketErrorDomain code:GCDAsyncSocketReadTimeoutError userInfo:userInfo];
- }
- /**
- * Returns a standard AsyncSocket write timeout error.
- **/
- - (NSError *)writeTimeoutError
- {
- NSString *errMsg = NSLocalizedStringWithDefaultValue(@"GCDAsyncSocketWriteTimeoutError",
- @"GCDAsyncSocket", [NSBundle mainBundle],
- @"Write operation timed out", nil);
-
- NSDictionary *userInfo = @{NSLocalizedDescriptionKey : errMsg};
-
- return [NSError errorWithDomain:GCDAsyncSocketErrorDomain code:GCDAsyncSocketWriteTimeoutError userInfo:userInfo];
- }
- - (NSError *)connectionClosedError
- {
- NSString *errMsg = NSLocalizedStringWithDefaultValue(@"GCDAsyncSocketClosedError",
- @"GCDAsyncSocket", [NSBundle mainBundle],
- @"Socket closed by remote peer", nil);
-
- NSDictionary *userInfo = @{NSLocalizedDescriptionKey : errMsg};
-
- return [NSError errorWithDomain:GCDAsyncSocketErrorDomain code:GCDAsyncSocketClosedError userInfo:userInfo];
- }
- - (NSError *)otherError:(NSString *)errMsg
- {
- NSDictionary *userInfo = @{NSLocalizedDescriptionKey : errMsg};
-
- return [NSError errorWithDomain:GCDAsyncSocketErrorDomain code:GCDAsyncSocketOtherError userInfo:userInfo];
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #pragma mark Diagnostics
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- - (BOOL)isDisconnected
- {
- __block BOOL result = NO;
-
- dispatch_block_t block = ^{
- result = (self->flags & kSocketStarted) ? NO : YES;
- };
-
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- block();
- else
- dispatch_sync(socketQueue, block);
-
- return result;
- }
- - (BOOL)isConnected
- {
- __block BOOL result = NO;
-
- dispatch_block_t block = ^{
- result = (self->flags & kConnected) ? YES : NO;
- };
-
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- block();
- else
- dispatch_sync(socketQueue, block);
-
- return result;
- }
- - (NSString *)connectedHost
- {
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- {
- if (socket4FD != SOCKET_NULL)
- return [self connectedHostFromSocket4:socket4FD];
- if (socket6FD != SOCKET_NULL)
- return [self connectedHostFromSocket6:socket6FD];
-
- return nil;
- }
- else
- {
- __block NSString *result = nil;
-
- dispatch_sync(socketQueue, ^{ @autoreleasepool {
-
- if (self->socket4FD != SOCKET_NULL)
- result = [self connectedHostFromSocket4:self->socket4FD];
- else if (self->socket6FD != SOCKET_NULL)
- result = [self connectedHostFromSocket6:self->socket6FD];
- }});
-
- return result;
- }
- }
- - (uint16_t)connectedPort
- {
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- {
- if (socket4FD != SOCKET_NULL)
- return [self connectedPortFromSocket4:socket4FD];
- if (socket6FD != SOCKET_NULL)
- return [self connectedPortFromSocket6:socket6FD];
-
- return 0;
- }
- else
- {
- __block uint16_t result = 0;
-
- dispatch_sync(socketQueue, ^{
- // No need for autorelease pool
-
- if (self->socket4FD != SOCKET_NULL)
- result = [self connectedPortFromSocket4:self->socket4FD];
- else if (self->socket6FD != SOCKET_NULL)
- result = [self connectedPortFromSocket6:self->socket6FD];
- });
-
- return result;
- }
- }
- - (NSURL *)connectedUrl
- {
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- {
- if (socketUN != SOCKET_NULL)
- return [self connectedUrlFromSocketUN:socketUN];
-
- return nil;
- }
- else
- {
- __block NSURL *result = nil;
-
- dispatch_sync(socketQueue, ^{ @autoreleasepool {
-
- if (self->socketUN != SOCKET_NULL)
- result = [self connectedUrlFromSocketUN:self->socketUN];
- }});
-
- return result;
- }
- }
- - (NSString *)localHost
- {
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- {
- if (socket4FD != SOCKET_NULL)
- return [self localHostFromSocket4:socket4FD];
- if (socket6FD != SOCKET_NULL)
- return [self localHostFromSocket6:socket6FD];
-
- return nil;
- }
- else
- {
- __block NSString *result = nil;
-
- dispatch_sync(socketQueue, ^{ @autoreleasepool {
-
- if (self->socket4FD != SOCKET_NULL)
- result = [self localHostFromSocket4:self->socket4FD];
- else if (self->socket6FD != SOCKET_NULL)
- result = [self localHostFromSocket6:self->socket6FD];
- }});
-
- return result;
- }
- }
- - (uint16_t)localPort
- {
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- {
- if (socket4FD != SOCKET_NULL)
- return [self localPortFromSocket4:socket4FD];
- if (socket6FD != SOCKET_NULL)
- return [self localPortFromSocket6:socket6FD];
-
- return 0;
- }
- else
- {
- __block uint16_t result = 0;
-
- dispatch_sync(socketQueue, ^{
- // No need for autorelease pool
-
- if (self->socket4FD != SOCKET_NULL)
- result = [self localPortFromSocket4:self->socket4FD];
- else if (self->socket6FD != SOCKET_NULL)
- result = [self localPortFromSocket6:self->socket6FD];
- });
-
- return result;
- }
- }
- - (NSString *)connectedHost4
- {
- if (socket4FD != SOCKET_NULL)
- return [self connectedHostFromSocket4:socket4FD];
-
- return nil;
- }
- - (NSString *)connectedHost6
- {
- if (socket6FD != SOCKET_NULL)
- return [self connectedHostFromSocket6:socket6FD];
-
- return nil;
- }
- - (uint16_t)connectedPort4
- {
- if (socket4FD != SOCKET_NULL)
- return [self connectedPortFromSocket4:socket4FD];
-
- return 0;
- }
- - (uint16_t)connectedPort6
- {
- if (socket6FD != SOCKET_NULL)
- return [self connectedPortFromSocket6:socket6FD];
-
- return 0;
- }
- - (NSString *)localHost4
- {
- if (socket4FD != SOCKET_NULL)
- return [self localHostFromSocket4:socket4FD];
-
- return nil;
- }
- - (NSString *)localHost6
- {
- if (socket6FD != SOCKET_NULL)
- return [self localHostFromSocket6:socket6FD];
-
- return nil;
- }
- - (uint16_t)localPort4
- {
- if (socket4FD != SOCKET_NULL)
- return [self localPortFromSocket4:socket4FD];
-
- return 0;
- }
- - (uint16_t)localPort6
- {
- if (socket6FD != SOCKET_NULL)
- return [self localPortFromSocket6:socket6FD];
-
- return 0;
- }
- - (NSString *)connectedHostFromSocket4:(int)socketFD
- {
- struct sockaddr_in sockaddr4;
- socklen_t sockaddr4len = sizeof(sockaddr4);
-
- if (getpeername(socketFD, (struct sockaddr *)&sockaddr4, &sockaddr4len) < 0)
- {
- return nil;
- }
- return [[self class] hostFromSockaddr4:&sockaddr4];
- }
- - (NSString *)connectedHostFromSocket6:(int)socketFD
- {
- struct sockaddr_in6 sockaddr6;
- socklen_t sockaddr6len = sizeof(sockaddr6);
-
- if (getpeername(socketFD, (struct sockaddr *)&sockaddr6, &sockaddr6len) < 0)
- {
- return nil;
- }
- return [[self class] hostFromSockaddr6:&sockaddr6];
- }
- - (uint16_t)connectedPortFromSocket4:(int)socketFD
- {
- struct sockaddr_in sockaddr4;
- socklen_t sockaddr4len = sizeof(sockaddr4);
-
- if (getpeername(socketFD, (struct sockaddr *)&sockaddr4, &sockaddr4len) < 0)
- {
- return 0;
- }
- return [[self class] portFromSockaddr4:&sockaddr4];
- }
- - (uint16_t)connectedPortFromSocket6:(int)socketFD
- {
- struct sockaddr_in6 sockaddr6;
- socklen_t sockaddr6len = sizeof(sockaddr6);
-
- if (getpeername(socketFD, (struct sockaddr *)&sockaddr6, &sockaddr6len) < 0)
- {
- return 0;
- }
- return [[self class] portFromSockaddr6:&sockaddr6];
- }
- - (NSURL *)connectedUrlFromSocketUN:(int)socketFD
- {
- struct sockaddr_un sockaddr;
- socklen_t sockaddrlen = sizeof(sockaddr);
-
- if (getpeername(socketFD, (struct sockaddr *)&sockaddr, &sockaddrlen) < 0)
- {
- return 0;
- }
- return [[self class] urlFromSockaddrUN:&sockaddr];
- }
- - (NSString *)localHostFromSocket4:(int)socketFD
- {
- struct sockaddr_in sockaddr4;
- socklen_t sockaddr4len = sizeof(sockaddr4);
-
- if (getsockname(socketFD, (struct sockaddr *)&sockaddr4, &sockaddr4len) < 0)
- {
- return nil;
- }
- return [[self class] hostFromSockaddr4:&sockaddr4];
- }
- - (NSString *)localHostFromSocket6:(int)socketFD
- {
- struct sockaddr_in6 sockaddr6;
- socklen_t sockaddr6len = sizeof(sockaddr6);
-
- if (getsockname(socketFD, (struct sockaddr *)&sockaddr6, &sockaddr6len) < 0)
- {
- return nil;
- }
- return [[self class] hostFromSockaddr6:&sockaddr6];
- }
- - (uint16_t)localPortFromSocket4:(int)socketFD
- {
- struct sockaddr_in sockaddr4;
- socklen_t sockaddr4len = sizeof(sockaddr4);
-
- if (getsockname(socketFD, (struct sockaddr *)&sockaddr4, &sockaddr4len) < 0)
- {
- return 0;
- }
- return [[self class] portFromSockaddr4:&sockaddr4];
- }
- - (uint16_t)localPortFromSocket6:(int)socketFD
- {
- struct sockaddr_in6 sockaddr6;
- socklen_t sockaddr6len = sizeof(sockaddr6);
-
- if (getsockname(socketFD, (struct sockaddr *)&sockaddr6, &sockaddr6len) < 0)
- {
- return 0;
- }
- return [[self class] portFromSockaddr6:&sockaddr6];
- }
- - (NSData *)connectedAddress
- {
- __block NSData *result = nil;
-
- dispatch_block_t block = ^{
- if (self->socket4FD != SOCKET_NULL)
- {
- struct sockaddr_in sockaddr4;
- socklen_t sockaddr4len = sizeof(sockaddr4);
-
- if (getpeername(self->socket4FD, (struct sockaddr *)&sockaddr4, &sockaddr4len) == 0)
- {
- result = [[NSData alloc] initWithBytes:&sockaddr4 length:sockaddr4len];
- }
- }
-
- if (self->socket6FD != SOCKET_NULL)
- {
- struct sockaddr_in6 sockaddr6;
- socklen_t sockaddr6len = sizeof(sockaddr6);
-
- if (getpeername(self->socket6FD, (struct sockaddr *)&sockaddr6, &sockaddr6len) == 0)
- {
- result = [[NSData alloc] initWithBytes:&sockaddr6 length:sockaddr6len];
- }
- }
- };
-
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- block();
- else
- dispatch_sync(socketQueue, block);
-
- return result;
- }
- - (NSData *)localAddress
- {
- __block NSData *result = nil;
-
- dispatch_block_t block = ^{
- if (self->socket4FD != SOCKET_NULL)
- {
- struct sockaddr_in sockaddr4;
- socklen_t sockaddr4len = sizeof(sockaddr4);
-
- if (getsockname(self->socket4FD, (struct sockaddr *)&sockaddr4, &sockaddr4len) == 0)
- {
- result = [[NSData alloc] initWithBytes:&sockaddr4 length:sockaddr4len];
- }
- }
-
- if (self->socket6FD != SOCKET_NULL)
- {
- struct sockaddr_in6 sockaddr6;
- socklen_t sockaddr6len = sizeof(sockaddr6);
-
- if (getsockname(self->socket6FD, (struct sockaddr *)&sockaddr6, &sockaddr6len) == 0)
- {
- result = [[NSData alloc] initWithBytes:&sockaddr6 length:sockaddr6len];
- }
- }
- };
-
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- block();
- else
- dispatch_sync(socketQueue, block);
-
- return result;
- }
- - (BOOL)isIPv4
- {
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- {
- return (socket4FD != SOCKET_NULL);
- }
- else
- {
- __block BOOL result = NO;
-
- dispatch_sync(socketQueue, ^{
- result = (self->socket4FD != SOCKET_NULL);
- });
-
- return result;
- }
- }
- - (BOOL)isIPv6
- {
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- {
- return (socket6FD != SOCKET_NULL);
- }
- else
- {
- __block BOOL result = NO;
-
- dispatch_sync(socketQueue, ^{
- result = (self->socket6FD != SOCKET_NULL);
- });
-
- return result;
- }
- }
- - (BOOL)isSecure
- {
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- {
- return (flags & kSocketSecure) ? YES : NO;
- }
- else
- {
- __block BOOL result;
-
- dispatch_sync(socketQueue, ^{
- result = (self->flags & kSocketSecure) ? YES : NO;
- });
-
- return result;
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #pragma mark Utilities
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /**
- * Finds the address of an interface description.
- * An inteface description may be an interface name (en0, en1, lo0) or corresponding IP (192.168.4.34).
- *
- * The interface description may optionally contain a port number at the end, separated by a colon.
- * If a non-zero port parameter is provided, any port number in the interface description is ignored.
- *
- * The returned value is a 'struct sockaddr' wrapped in an NSMutableData object.
- **/
- - (void)getInterfaceAddress4:(NSMutableData **)interfaceAddr4Ptr
- address6:(NSMutableData **)interfaceAddr6Ptr
- fromDescription:(NSString *)interfaceDescription
- port:(uint16_t)port
- {
- NSMutableData *addr4 = nil;
- NSMutableData *addr6 = nil;
-
- NSString *interface = nil;
-
- NSArray *components = [interfaceDescription componentsSeparatedByString:@":"];
- if ([components count] > 0)
- {
- NSString *temp = [components objectAtIndex:0];
- if ([temp length] > 0)
- {
- interface = temp;
- }
- }
- if ([components count] > 1 && port == 0)
- {
- NSString *temp = [components objectAtIndex:1];
- long portL = strtol([temp UTF8String], NULL, 10);
-
- if (portL > 0 && portL <= UINT16_MAX)
- {
- port = (uint16_t)portL;
- }
- }
-
- if (interface == nil)
- {
- // ANY address
-
- struct sockaddr_in sockaddr4;
- memset(&sockaddr4, 0, sizeof(sockaddr4));
-
- sockaddr4.sin_len = sizeof(sockaddr4);
- sockaddr4.sin_family = AF_INET;
- sockaddr4.sin_port = htons(port);
- sockaddr4.sin_addr.s_addr = htonl(INADDR_ANY);
-
- struct sockaddr_in6 sockaddr6;
- memset(&sockaddr6, 0, sizeof(sockaddr6));
-
- sockaddr6.sin6_len = sizeof(sockaddr6);
- sockaddr6.sin6_family = AF_INET6;
- sockaddr6.sin6_port = htons(port);
- sockaddr6.sin6_addr = in6addr_any;
-
- addr4 = [NSMutableData dataWithBytes:&sockaddr4 length:sizeof(sockaddr4)];
- addr6 = [NSMutableData dataWithBytes:&sockaddr6 length:sizeof(sockaddr6)];
- }
- else if ([interface isEqualToString:@"localhost"] || [interface isEqualToString:@"loopback"])
- {
- // LOOPBACK address
-
- struct sockaddr_in sockaddr4;
- memset(&sockaddr4, 0, sizeof(sockaddr4));
-
- sockaddr4.sin_len = sizeof(sockaddr4);
- sockaddr4.sin_family = AF_INET;
- sockaddr4.sin_port = htons(port);
- sockaddr4.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
-
- struct sockaddr_in6 sockaddr6;
- memset(&sockaddr6, 0, sizeof(sockaddr6));
-
- sockaddr6.sin6_len = sizeof(sockaddr6);
- sockaddr6.sin6_family = AF_INET6;
- sockaddr6.sin6_port = htons(port);
- sockaddr6.sin6_addr = in6addr_loopback;
-
- addr4 = [NSMutableData dataWithBytes:&sockaddr4 length:sizeof(sockaddr4)];
- addr6 = [NSMutableData dataWithBytes:&sockaddr6 length:sizeof(sockaddr6)];
- }
- else
- {
- const char *iface = [interface UTF8String];
-
- struct ifaddrs *addrs;
- const struct ifaddrs *cursor;
-
- if ((getifaddrs(&addrs) == 0))
- {
- cursor = addrs;
- while (cursor != NULL)
- {
- if ((addr4 == nil) && (cursor->ifa_addr->sa_family == AF_INET))
- {
- // IPv4
-
- struct sockaddr_in nativeAddr4;
- memcpy(&nativeAddr4, cursor->ifa_addr, sizeof(nativeAddr4));
-
- if (strcmp(cursor->ifa_name, iface) == 0)
- {
- // Name match
-
- nativeAddr4.sin_port = htons(port);
-
- addr4 = [NSMutableData dataWithBytes:&nativeAddr4 length:sizeof(nativeAddr4)];
- }
- else
- {
- char ip[INET_ADDRSTRLEN];
-
- const char *conversion = inet_ntop(AF_INET, &nativeAddr4.sin_addr, ip, sizeof(ip));
-
- if ((conversion != NULL) && (strcmp(ip, iface) == 0))
- {
- // IP match
-
- nativeAddr4.sin_port = htons(port);
-
- addr4 = [NSMutableData dataWithBytes:&nativeAddr4 length:sizeof(nativeAddr4)];
- }
- }
- }
- else if ((addr6 == nil) && (cursor->ifa_addr->sa_family == AF_INET6))
- {
- // IPv6
-
- struct sockaddr_in6 nativeAddr6;
- memcpy(&nativeAddr6, cursor->ifa_addr, sizeof(nativeAddr6));
-
- if (strcmp(cursor->ifa_name, iface) == 0)
- {
- // Name match
-
- nativeAddr6.sin6_port = htons(port);
-
- addr6 = [NSMutableData dataWithBytes:&nativeAddr6 length:sizeof(nativeAddr6)];
- }
- else
- {
- char ip[INET6_ADDRSTRLEN];
-
- const char *conversion = inet_ntop(AF_INET6, &nativeAddr6.sin6_addr, ip, sizeof(ip));
-
- if ((conversion != NULL) && (strcmp(ip, iface) == 0))
- {
- // IP match
-
- nativeAddr6.sin6_port = htons(port);
-
- addr6 = [NSMutableData dataWithBytes:&nativeAddr6 length:sizeof(nativeAddr6)];
- }
- }
- }
-
- cursor = cursor->ifa_next;
- }
-
- freeifaddrs(addrs);
- }
- }
-
- if (interfaceAddr4Ptr) *interfaceAddr4Ptr = addr4;
- if (interfaceAddr6Ptr) *interfaceAddr6Ptr = addr6;
- }
- - (NSData *)getInterfaceAddressFromUrl:(NSURL *)url
- {
- NSString *path = url.path;
- if (path.length == 0) {
- return nil;
- }
-
- struct sockaddr_un nativeAddr;
- nativeAddr.sun_family = AF_UNIX;
- strlcpy(nativeAddr.sun_path, path.fileSystemRepresentation, sizeof(nativeAddr.sun_path));
- nativeAddr.sun_len = (unsigned char)SUN_LEN(&nativeAddr);
- NSData *interface = [NSData dataWithBytes:&nativeAddr length:sizeof(struct sockaddr_un)];
-
- return interface;
- }
- - (void)setupReadAndWriteSourcesForNewlyConnectedSocket:(int)socketFD
- {
- readSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, socketFD, 0, socketQueue);
- writeSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_WRITE, socketFD, 0, socketQueue);
-
- // Setup event handlers
-
- __weak GCDAsyncSocket *weakSelf = self;
-
- dispatch_source_set_event_handler(readSource, ^{ @autoreleasepool {
- #pragma clang diagnostic push
- #pragma clang diagnostic warning "-Wimplicit-retain-self"
-
- __strong GCDAsyncSocket *strongSelf = weakSelf;
- if (strongSelf == nil) return_from_block;
-
- LogVerbose(@"readEventBlock");
-
- strongSelf->socketFDBytesAvailable = dispatch_source_get_data(strongSelf->readSource);
- LogVerbose(@"socketFDBytesAvailable: %lu", strongSelf->socketFDBytesAvailable);
-
- if (strongSelf->socketFDBytesAvailable > 0)
- [strongSelf doReadData];
- else
- [strongSelf doReadEOF];
-
- #pragma clang diagnostic pop
- }});
-
- dispatch_source_set_event_handler(writeSource, ^{ @autoreleasepool {
- #pragma clang diagnostic push
- #pragma clang diagnostic warning "-Wimplicit-retain-self"
-
- __strong GCDAsyncSocket *strongSelf = weakSelf;
- if (strongSelf == nil) return_from_block;
-
- LogVerbose(@"writeEventBlock");
-
- strongSelf->flags |= kSocketCanAcceptBytes;
- [strongSelf doWriteData];
-
- #pragma clang diagnostic pop
- }});
-
- // Setup cancel handlers
-
- __block int socketFDRefCount = 2;
-
- #if !OS_OBJECT_USE_OBJC
- dispatch_source_t theReadSource = readSource;
- dispatch_source_t theWriteSource = writeSource;
- #endif
-
- dispatch_source_set_cancel_handler(readSource, ^{
- #pragma clang diagnostic push
- #pragma clang diagnostic warning "-Wimplicit-retain-self"
-
- LogVerbose(@"readCancelBlock");
-
- #if !OS_OBJECT_USE_OBJC
- LogVerbose(@"dispatch_release(readSource)");
- dispatch_release(theReadSource);
- #endif
-
- if (--socketFDRefCount == 0)
- {
- LogVerbose(@"close(socketFD)");
- close(socketFD);
- }
-
- #pragma clang diagnostic pop
- });
-
- dispatch_source_set_cancel_handler(writeSource, ^{
- #pragma clang diagnostic push
- #pragma clang diagnostic warning "-Wimplicit-retain-self"
-
- LogVerbose(@"writeCancelBlock");
-
- #if !OS_OBJECT_USE_OBJC
- LogVerbose(@"dispatch_release(writeSource)");
- dispatch_release(theWriteSource);
- #endif
-
- if (--socketFDRefCount == 0)
- {
- LogVerbose(@"close(socketFD)");
- close(socketFD);
- }
-
- #pragma clang diagnostic pop
- });
-
- // We will not be able to read until data arrives.
- // But we should be able to write immediately.
-
- socketFDBytesAvailable = 0;
- flags &= ~kReadSourceSuspended;
-
- LogVerbose(@"dispatch_resume(readSource)");
- dispatch_resume(readSource);
-
- flags |= kSocketCanAcceptBytes;
- flags |= kWriteSourceSuspended;
- }
- - (BOOL)usingCFStreamForTLS
- {
- #if TARGET_OS_IPHONE
-
- if ((flags & kSocketSecure) && (flags & kUsingCFStreamForTLS))
- {
- // The startTLS method was given the GCDAsyncSocketUseCFStreamForTLS flag.
-
- return YES;
- }
-
- #endif
-
- return NO;
- }
- - (BOOL)usingSecureTransportForTLS
- {
- // Invoking this method is equivalent to ![self usingCFStreamForTLS] (just more readable)
-
- #if TARGET_OS_IPHONE
-
- if ((flags & kSocketSecure) && (flags & kUsingCFStreamForTLS))
- {
- // The startTLS method was given the GCDAsyncSocketUseCFStreamForTLS flag.
-
- return NO;
- }
-
- #endif
-
- return YES;
- }
- - (void)suspendReadSource
- {
- if (!(flags & kReadSourceSuspended))
- {
- LogVerbose(@"dispatch_suspend(readSource)");
-
- dispatch_suspend(readSource);
- flags |= kReadSourceSuspended;
- }
- }
- - (void)resumeReadSource
- {
- if (flags & kReadSourceSuspended)
- {
- LogVerbose(@"dispatch_resume(readSource)");
-
- dispatch_resume(readSource);
- flags &= ~kReadSourceSuspended;
- }
- }
- - (void)suspendWriteSource
- {
- if (!(flags & kWriteSourceSuspended))
- {
- LogVerbose(@"dispatch_suspend(writeSource)");
-
- dispatch_suspend(writeSource);
- flags |= kWriteSourceSuspended;
- }
- }
- - (void)resumeWriteSource
- {
- if (flags & kWriteSourceSuspended)
- {
- LogVerbose(@"dispatch_resume(writeSource)");
-
- dispatch_resume(writeSource);
- flags &= ~kWriteSourceSuspended;
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #pragma mark Reading
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- - (void)readDataWithTimeout:(NSTimeInterval)timeout tag:(long)tag
- {
- [self readDataWithTimeout:timeout buffer:nil bufferOffset:0 maxLength:0 tag:tag];
- }
- - (void)readDataWithTimeout:(NSTimeInterval)timeout
- buffer:(NSMutableData *)buffer
- bufferOffset:(NSUInteger)offset
- tag:(long)tag
- {
- [self readDataWithTimeout:timeout buffer:buffer bufferOffset:offset maxLength:0 tag:tag];
- }
- - (void)readDataWithTimeout:(NSTimeInterval)timeout
- buffer:(NSMutableData *)buffer
- bufferOffset:(NSUInteger)offset
- maxLength:(NSUInteger)length
- tag:(long)tag
- {
- if (offset > [buffer length]) {
- LogWarn(@"Cannot read: offset > [buffer length]");
- return;
- }
-
- GCDAsyncReadPacket *packet = [[GCDAsyncReadPacket alloc] initWithData:buffer
- startOffset:offset
- maxLength:length
- timeout:timeout
- readLength:0
- terminator:nil
- tag:tag];
-
- dispatch_async(socketQueue, ^{ @autoreleasepool {
-
- LogTrace();
-
- if ((self->flags & kSocketStarted) && !(self->flags & kForbidReadsWrites))
- {
- [self->readQueue addObject:packet];
- [self maybeDequeueRead];
- }
- }});
-
- // Do not rely on the block being run in order to release the packet,
- // as the queue might get released without the block completing.
- }
- - (void)readDataToLength:(NSUInteger)length withTimeout:(NSTimeInterval)timeout tag:(long)tag
- {
- [self readDataToLength:length withTimeout:timeout buffer:nil bufferOffset:0 tag:tag];
- }
- - (void)readDataToLength:(NSUInteger)length
- withTimeout:(NSTimeInterval)timeout
- buffer:(NSMutableData *)buffer
- bufferOffset:(NSUInteger)offset
- tag:(long)tag
- {
- if (length == 0) {
- LogWarn(@"Cannot read: length == 0");
- return;
- }
- if (offset > [buffer length]) {
- LogWarn(@"Cannot read: offset > [buffer length]");
- return;
- }
-
- GCDAsyncReadPacket *packet = [[GCDAsyncReadPacket alloc] initWithData:buffer
- startOffset:offset
- maxLength:0
- timeout:timeout
- readLength:length
- terminator:nil
- tag:tag];
-
- dispatch_async(socketQueue, ^{ @autoreleasepool {
-
- LogTrace();
-
- if ((self->flags & kSocketStarted) && !(self->flags & kForbidReadsWrites))
- {
- [self->readQueue addObject:packet];
- [self maybeDequeueRead];
- }
- }});
-
- // Do not rely on the block being run in order to release the packet,
- // as the queue might get released without the block completing.
- }
- - (void)readDataToData:(NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag
- {
- [self readDataToData:data withTimeout:timeout buffer:nil bufferOffset:0 maxLength:0 tag:tag];
- }
- - (void)readDataToData:(NSData *)data
- withTimeout:(NSTimeInterval)timeout
- buffer:(NSMutableData *)buffer
- bufferOffset:(NSUInteger)offset
- tag:(long)tag
- {
- [self readDataToData:data withTimeout:timeout buffer:buffer bufferOffset:offset maxLength:0 tag:tag];
- }
- - (void)readDataToData:(NSData *)data withTimeout:(NSTimeInterval)timeout maxLength:(NSUInteger)length tag:(long)tag
- {
- [self readDataToData:data withTimeout:timeout buffer:nil bufferOffset:0 maxLength:length tag:tag];
- }
- - (void)readDataToData:(NSData *)data
- withTimeout:(NSTimeInterval)timeout
- buffer:(NSMutableData *)buffer
- bufferOffset:(NSUInteger)offset
- maxLength:(NSUInteger)maxLength
- tag:(long)tag
- {
- if ([data length] == 0) {
- LogWarn(@"Cannot read: [data length] == 0");
- return;
- }
- if (offset > [buffer length]) {
- LogWarn(@"Cannot read: offset > [buffer length]");
- return;
- }
- if (maxLength > 0 && maxLength < [data length]) {
- LogWarn(@"Cannot read: maxLength > 0 && maxLength < [data length]");
- return;
- }
-
- GCDAsyncReadPacket *packet = [[GCDAsyncReadPacket alloc] initWithData:buffer
- startOffset:offset
- maxLength:maxLength
- timeout:timeout
- readLength:0
- terminator:data
- tag:tag];
-
- dispatch_async(socketQueue, ^{ @autoreleasepool {
-
- LogTrace();
-
- if ((self->flags & kSocketStarted) && !(self->flags & kForbidReadsWrites))
- {
- [self->readQueue addObject:packet];
- [self maybeDequeueRead];
- }
- }});
-
- // Do not rely on the block being run in order to release the packet,
- // as the queue might get released without the block completing.
- }
- - (float)progressOfReadReturningTag:(long *)tagPtr bytesDone:(NSUInteger *)donePtr total:(NSUInteger *)totalPtr
- {
- __block float result = 0.0F;
-
- dispatch_block_t block = ^{
-
- if (!self->currentRead || ![self->currentRead isKindOfClass:[GCDAsyncReadPacket class]])
- {
- // We're not reading anything right now.
-
- if (tagPtr != NULL) *tagPtr = 0;
- if (donePtr != NULL) *donePtr = 0;
- if (totalPtr != NULL) *totalPtr = 0;
-
- result = NAN;
- }
- else
- {
- // It's only possible to know the progress of our read if we're reading to a certain length.
- // If we're reading to data, we of course have no idea when the data will arrive.
- // If we're reading to timeout, then we have no idea when the next chunk of data will arrive.
-
- NSUInteger done = self->currentRead->bytesDone;
- NSUInteger total = self->currentRead->readLength;
-
- if (tagPtr != NULL) *tagPtr = self->currentRead->tag;
- if (donePtr != NULL) *donePtr = done;
- if (totalPtr != NULL) *totalPtr = total;
-
- if (total > 0)
- result = (float)done / (float)total;
- else
- result = 1.0F;
- }
- };
-
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- block();
- else
- dispatch_sync(socketQueue, block);
-
- return result;
- }
- /**
- * This method starts a new read, if needed.
- *
- * It is called when:
- * - a user requests a read
- * - after a read request has finished (to handle the next request)
- * - immediately after the socket opens to handle any pending requests
- *
- * This method also handles auto-disconnect post read/write completion.
- **/
- - (void)maybeDequeueRead
- {
- LogTrace();
- NSAssert(dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey), @"Must be dispatched on socketQueue");
-
- // If we're not currently processing a read AND we have an available read stream
- if ((currentRead == nil) && (flags & kConnected))
- {
- if ([readQueue count] > 0)
- {
- // Dequeue the next object in the write queue
- currentRead = [readQueue objectAtIndex:0];
- [readQueue removeObjectAtIndex:0];
-
-
- if ([currentRead isKindOfClass:[GCDAsyncSpecialPacket class]])
- {
- LogVerbose(@"Dequeued GCDAsyncSpecialPacket");
-
- // Attempt to start TLS
- flags |= kStartingReadTLS;
-
- // This method won't do anything unless both kStartingReadTLS and kStartingWriteTLS are set
- [self maybeStartTLS];
- }
- else
- {
- LogVerbose(@"Dequeued GCDAsyncReadPacket");
-
- // Setup read timer (if needed)
- [self setupReadTimerWithTimeout:currentRead->timeout];
-
- // Immediately read, if possible
- [self doReadData];
- }
- }
- else if (flags & kDisconnectAfterReads)
- {
- if (flags & kDisconnectAfterWrites)
- {
- if (([writeQueue count] == 0) && (currentWrite == nil))
- {
- [self closeWithError:nil];
- }
- }
- else
- {
- [self closeWithError:nil];
- }
- }
- else if (flags & kSocketSecure)
- {
- [self flushSSLBuffers];
-
- // Edge case:
- //
- // We just drained all data from the ssl buffers,
- // and all known data from the socket (socketFDBytesAvailable).
- //
- // If we didn't get any data from this process,
- // then we may have reached the end of the TCP stream.
- //
- // Be sure callbacks are enabled so we're notified about a disconnection.
-
- if ([preBuffer availableBytes] == 0)
- {
- if ([self usingCFStreamForTLS]) {
- // Callbacks never disabled
- }
- else {
- [self resumeReadSource];
- }
- }
- }
- }
- }
- - (void)flushSSLBuffers
- {
- LogTrace();
-
- NSAssert((flags & kSocketSecure), @"Cannot flush ssl buffers on non-secure socket");
-
- if ([preBuffer availableBytes] > 0)
- {
- // Only flush the ssl buffers if the prebuffer is empty.
- // This is to avoid growing the prebuffer inifinitely large.
-
- return;
- }
-
- #if TARGET_OS_IPHONE
-
- if ([self usingCFStreamForTLS])
- {
- if ((flags & kSecureSocketHasBytesAvailable) && CFReadStreamHasBytesAvailable(readStream))
- {
- LogVerbose(@"%@ - Flushing ssl buffers into prebuffer...", THIS_METHOD);
-
- CFIndex defaultBytesToRead = (1024 * 4);
-
- [preBuffer ensureCapacityForWrite:defaultBytesToRead];
-
- uint8_t *buffer = [preBuffer writeBuffer];
-
- CFIndex result = CFReadStreamRead(readStream, buffer, defaultBytesToRead);
- LogVerbose(@"%@ - CFReadStreamRead(): result = %i", THIS_METHOD, (int)result);
-
- if (result > 0)
- {
- [preBuffer didWrite:result];
- }
-
- flags &= ~kSecureSocketHasBytesAvailable;
- }
-
- return;
- }
-
- #endif
-
- __block NSUInteger estimatedBytesAvailable = 0;
-
- dispatch_block_t updateEstimatedBytesAvailable = ^{
-
- // Figure out if there is any data available to be read
- //
- // socketFDBytesAvailable <- Number of encrypted bytes we haven't read from the bsd socket
- // [sslPreBuffer availableBytes] <- Number of encrypted bytes we've buffered from bsd socket
- // sslInternalBufSize <- Number of decrypted bytes SecureTransport has buffered
- //
- // We call the variable "estimated" because we don't know how many decrypted bytes we'll get
- // from the encrypted bytes in the sslPreBuffer.
- // However, we do know this is an upper bound on the estimation.
-
- estimatedBytesAvailable = self->socketFDBytesAvailable + [self->sslPreBuffer availableBytes];
-
- size_t sslInternalBufSize = 0;
- SSLGetBufferedReadSize(self->sslContext, &sslInternalBufSize);
-
- estimatedBytesAvailable += sslInternalBufSize;
- };
-
- updateEstimatedBytesAvailable();
-
- if (estimatedBytesAvailable > 0)
- {
- LogVerbose(@"%@ - Flushing ssl buffers into prebuffer...", THIS_METHOD);
-
- BOOL done = NO;
- do
- {
- LogVerbose(@"%@ - estimatedBytesAvailable = %lu", THIS_METHOD, (unsigned long)estimatedBytesAvailable);
-
- // Make sure there's enough room in the prebuffer
-
- [preBuffer ensureCapacityForWrite:estimatedBytesAvailable];
-
- // Read data into prebuffer
-
- uint8_t *buffer = [preBuffer writeBuffer];
- size_t bytesRead = 0;
-
- OSStatus result = SSLRead(sslContext, buffer, (size_t)estimatedBytesAvailable, &bytesRead);
- LogVerbose(@"%@ - read from secure socket = %u", THIS_METHOD, (unsigned)bytesRead);
-
- if (bytesRead > 0)
- {
- [preBuffer didWrite:bytesRead];
- }
-
- LogVerbose(@"%@ - prebuffer.length = %zu", THIS_METHOD, [preBuffer availableBytes]);
-
- if (result != noErr)
- {
- done = YES;
- }
- else
- {
- updateEstimatedBytesAvailable();
- }
-
- } while (!done && estimatedBytesAvailable > 0);
- }
- }
- - (void)doReadData
- {
- LogTrace();
-
- // This method is called on the socketQueue.
- // It might be called directly, or via the readSource when data is available to be read.
-
- if ((currentRead == nil) || (flags & kReadsPaused))
- {
- LogVerbose(@"No currentRead or kReadsPaused");
-
- // Unable to read at this time
-
- if (flags & kSocketSecure)
- {
- // Here's the situation:
- //
- // We have an established secure connection.
- // There may not be a currentRead, but there might be encrypted data sitting around for us.
- // When the user does get around to issuing a read, that encrypted data will need to be decrypted.
- //
- // So why make the user wait?
- // We might as well get a head start on decrypting some data now.
- //
- // The other reason we do this has to do with detecting a socket disconnection.
- // The SSL/TLS protocol has it's own disconnection handshake.
- // So when a secure socket is closed, a "goodbye" packet comes across the wire.
- // We want to make sure we read the "goodbye" packet so we can properly detect the TCP disconnection.
-
- [self flushSSLBuffers];
- }
-
- if ([self usingCFStreamForTLS])
- {
- // CFReadStream only fires once when there is available data.
- // It won't fire again until we've invoked CFReadStreamRead.
- }
- else
- {
- // If the readSource is firing, we need to pause it
- // or else it will continue to fire over and over again.
- //
- // If the readSource is not firing,
- // we want it to continue monitoring the socket.
-
- if (socketFDBytesAvailable > 0)
- {
- [self suspendReadSource];
- }
- }
- return;
- }
-
- BOOL hasBytesAvailable = NO;
- unsigned long estimatedBytesAvailable = 0;
-
- if ([self usingCFStreamForTLS])
- {
- #if TARGET_OS_IPHONE
-
- // Requested CFStream, rather than SecureTransport, for TLS (via GCDAsyncSocketUseCFStreamForTLS)
-
- estimatedBytesAvailable = 0;
- if ((flags & kSecureSocketHasBytesAvailable) && CFReadStreamHasBytesAvailable(readStream))
- hasBytesAvailable = YES;
- else
- hasBytesAvailable = NO;
-
- #endif
- }
- else
- {
- estimatedBytesAvailable = socketFDBytesAvailable;
-
- if (flags & kSocketSecure)
- {
- // There are 2 buffers to be aware of here.
- //
- // We are using SecureTransport, a TLS/SSL security layer which sits atop TCP.
- // We issue a read to the SecureTranport API, which in turn issues a read to our SSLReadFunction.
- // Our SSLReadFunction then reads from the BSD socket and returns the encrypted data to SecureTransport.
- // SecureTransport then decrypts the data, and finally returns the decrypted data back to us.
- //
- // The first buffer is one we create.
- // SecureTransport often requests small amounts of data.
- // This has to do with the encypted packets that are coming across the TCP stream.
- // But it's non-optimal to do a bunch of small reads from the BSD socket.
- // So our SSLReadFunction reads all available data from the socket (optimizing the sys call)
- // and may store excess in the sslPreBuffer.
-
- estimatedBytesAvailable += [sslPreBuffer availableBytes];
-
- // The second buffer is within SecureTransport.
- // As mentioned earlier, there are encrypted packets coming across the TCP stream.
- // SecureTransport needs the entire packet to decrypt it.
- // But if the entire packet produces X bytes of decrypted data,
- // and we only asked SecureTransport for X/2 bytes of data,
- // it must store the extra X/2 bytes of decrypted data for the next read.
- //
- // The SSLGetBufferedReadSize function will tell us the size of this internal buffer.
- // From the documentation:
- //
- // "This function does not block or cause any low-level read operations to occur."
-
- size_t sslInternalBufSize = 0;
- SSLGetBufferedReadSize(sslContext, &sslInternalBufSize);
-
- estimatedBytesAvailable += sslInternalBufSize;
- }
-
- hasBytesAvailable = (estimatedBytesAvailable > 0);
- }
-
- if ((hasBytesAvailable == NO) && ([preBuffer availableBytes] == 0))
- {
- LogVerbose(@"No data available to read...");
-
- // No data available to read.
-
- if (![self usingCFStreamForTLS])
- {
- // Need to wait for readSource to fire and notify us of
- // available data in the socket's internal read buffer.
-
- [self resumeReadSource];
- }
- return;
- }
-
- if (flags & kStartingReadTLS)
- {
- LogVerbose(@"Waiting for SSL/TLS handshake to complete");
-
- // The readQueue is waiting for SSL/TLS handshake to complete.
-
- if (flags & kStartingWriteTLS)
- {
- if ([self usingSecureTransportForTLS] && lastSSLHandshakeError == errSSLWouldBlock)
- {
- // We are in the process of a SSL Handshake.
- // We were waiting for incoming data which has just arrived.
-
- [self ssl_continueSSLHandshake];
- }
- }
- else
- {
- // We are still waiting for the writeQueue to drain and start the SSL/TLS process.
- // We now know data is available to read.
-
- if (![self usingCFStreamForTLS])
- {
- // Suspend the read source or else it will continue to fire nonstop.
-
- [self suspendReadSource];
- }
- }
-
- return;
- }
-
- BOOL done = NO; // Completed read operation
- NSError *error = nil; // Error occurred
-
- NSUInteger totalBytesReadForCurrentRead = 0;
-
- //
- // STEP 1 - READ FROM PREBUFFER
- //
-
- if ([preBuffer availableBytes] > 0)
- {
- // There are 3 types of read packets:
- //
- // 1) Read all available data.
- // 2) Read a specific length of data.
- // 3) Read up to a particular terminator.
-
- NSUInteger bytesToCopy;
-
- if (currentRead->term != nil)
- {
- // Read type #3 - read up to a terminator
-
- bytesToCopy = [currentRead readLengthForTermWithPreBuffer:preBuffer found:&done];
- }
- else
- {
- // Read type #1 or #2
-
- bytesToCopy = [currentRead readLengthForNonTermWithHint:[preBuffer availableBytes]];
- }
-
- // Make sure we have enough room in the buffer for our read.
-
- [currentRead ensureCapacityForAdditionalDataOfLength:bytesToCopy];
-
- // Copy bytes from prebuffer into packet buffer
-
- uint8_t *buffer = (uint8_t *)[currentRead->buffer mutableBytes] + currentRead->startOffset +
- currentRead->bytesDone;
-
- memcpy(buffer, [preBuffer readBuffer], bytesToCopy);
-
- // Remove the copied bytes from the preBuffer
- [preBuffer didRead:bytesToCopy];
-
- LogVerbose(@"copied(%lu) preBufferLength(%zu)", (unsigned long)bytesToCopy, [preBuffer availableBytes]);
-
- // Update totals
-
- currentRead->bytesDone += bytesToCopy;
- totalBytesReadForCurrentRead += bytesToCopy;
-
- // Check to see if the read operation is done
-
- if (currentRead->readLength > 0)
- {
- // Read type #2 - read a specific length of data
-
- done = (currentRead->bytesDone == currentRead->readLength);
- }
- else if (currentRead->term != nil)
- {
- // Read type #3 - read up to a terminator
-
- // Our 'done' variable was updated via the readLengthForTermWithPreBuffer:found: method
-
- if (!done && currentRead->maxLength > 0)
- {
- // We're not done and there's a set maxLength.
- // Have we reached that maxLength yet?
-
- if (currentRead->bytesDone >= currentRead->maxLength)
- {
- error = [self readMaxedOutError];
- }
- }
- }
- else
- {
- // Read type #1 - read all available data
- //
- // We're done as soon as
- // - we've read all available data (in prebuffer and socket)
- // - we've read the maxLength of read packet.
-
- done = ((currentRead->maxLength > 0) && (currentRead->bytesDone == currentRead->maxLength));
- }
-
- }
-
- //
- // STEP 2 - READ FROM SOCKET
- //
-
- BOOL socketEOF = (flags & kSocketHasReadEOF) ? YES : NO; // Nothing more to read via socket (end of file)
- BOOL waiting = !done && !error && !socketEOF && !hasBytesAvailable; // Ran out of data, waiting for more
-
- if (!done && !error && !socketEOF && hasBytesAvailable)
- {
- NSAssert(([preBuffer availableBytes] == 0), @"Invalid logic");
-
- BOOL readIntoPreBuffer = NO;
- uint8_t *buffer = NULL;
- size_t bytesRead = 0;
-
- if (flags & kSocketSecure)
- {
- if ([self usingCFStreamForTLS])
- {
- #if TARGET_OS_IPHONE
-
- // Using CFStream, rather than SecureTransport, for TLS
-
- NSUInteger defaultReadLength = (1024 * 32);
-
- NSUInteger bytesToRead = [currentRead optimalReadLengthWithDefault:defaultReadLength
- shouldPreBuffer:&readIntoPreBuffer];
-
- // Make sure we have enough room in the buffer for our read.
- //
- // We are either reading directly into the currentRead->buffer,
- // or we're reading into the temporary preBuffer.
-
- if (readIntoPreBuffer)
- {
- [preBuffer ensureCapacityForWrite:bytesToRead];
-
- buffer = [preBuffer writeBuffer];
- }
- else
- {
- [currentRead ensureCapacityForAdditionalDataOfLength:bytesToRead];
-
- buffer = (uint8_t *)[currentRead->buffer mutableBytes]
- + currentRead->startOffset
- + currentRead->bytesDone;
- }
-
- // Read data into buffer
-
- CFIndex result = CFReadStreamRead(readStream, buffer, (CFIndex)bytesToRead);
- LogVerbose(@"CFReadStreamRead(): result = %i", (int)result);
-
- if (result < 0)
- {
- error = (__bridge_transfer NSError *)CFReadStreamCopyError(readStream);
- }
- else if (result == 0)
- {
- socketEOF = YES;
- }
- else
- {
- waiting = YES;
- bytesRead = (size_t)result;
- }
-
- // We only know how many decrypted bytes were read.
- // The actual number of bytes read was likely more due to the overhead of the encryption.
- // So we reset our flag, and rely on the next callback to alert us of more data.
- flags &= ~kSecureSocketHasBytesAvailable;
-
- #endif
- }
- else
- {
- // Using SecureTransport for TLS
- //
- // We know:
- // - how many bytes are available on the socket
- // - how many encrypted bytes are sitting in the sslPreBuffer
- // - how many decypted bytes are sitting in the sslContext
- //
- // But we do NOT know:
- // - how many encypted bytes are sitting in the sslContext
- //
- // So we play the regular game of using an upper bound instead.
-
- NSUInteger defaultReadLength = (1024 * 32);
-
- if (defaultReadLength < estimatedBytesAvailable) {
- defaultReadLength = estimatedBytesAvailable + (1024 * 16);
- }
-
- NSUInteger bytesToRead = [currentRead optimalReadLengthWithDefault:defaultReadLength
- shouldPreBuffer:&readIntoPreBuffer];
-
- if (bytesToRead > SIZE_MAX) { // NSUInteger may be bigger than size_t
- bytesToRead = SIZE_MAX;
- }
-
- // Make sure we have enough room in the buffer for our read.
- //
- // We are either reading directly into the currentRead->buffer,
- // or we're reading into the temporary preBuffer.
-
- if (readIntoPreBuffer)
- {
- [preBuffer ensureCapacityForWrite:bytesToRead];
-
- buffer = [preBuffer writeBuffer];
- }
- else
- {
- [currentRead ensureCapacityForAdditionalDataOfLength:bytesToRead];
-
- buffer = (uint8_t *)[currentRead->buffer mutableBytes]
- + currentRead->startOffset
- + currentRead->bytesDone;
- }
-
- // The documentation from Apple states:
- //
- // "a read operation might return errSSLWouldBlock,
- // indicating that less data than requested was actually transferred"
- //
- // However, starting around 10.7, the function will sometimes return noErr,
- // even if it didn't read as much data as requested. So we need to watch out for that.
-
- OSStatus result;
- do
- {
- void *loop_buffer = buffer + bytesRead;
- size_t loop_bytesToRead = (size_t)bytesToRead - bytesRead;
- size_t loop_bytesRead = 0;
-
- result = SSLRead(sslContext, loop_buffer, loop_bytesToRead, &loop_bytesRead);
- LogVerbose(@"read from secure socket = %u", (unsigned)loop_bytesRead);
-
- bytesRead += loop_bytesRead;
-
- } while ((result == noErr) && (bytesRead < bytesToRead));
-
-
- if (result != noErr)
- {
- if (result == errSSLWouldBlock)
- waiting = YES;
- else
- {
- if (result == errSSLClosedGraceful || result == errSSLClosedAbort)
- {
- // We've reached the end of the stream.
- // Handle this the same way we would an EOF from the socket.
- socketEOF = YES;
- sslErrCode = result;
- }
- else
- {
- error = [self sslError:result];
- }
- }
- // It's possible that bytesRead > 0, even if the result was errSSLWouldBlock.
- // This happens when the SSLRead function is able to read some data,
- // but not the entire amount we requested.
-
- if (bytesRead <= 0)
- {
- bytesRead = 0;
- }
- }
-
- // Do not modify socketFDBytesAvailable.
- // It will be updated via the SSLReadFunction().
- }
- }
- else
- {
- // Normal socket operation
-
- NSUInteger bytesToRead;
-
- // There are 3 types of read packets:
- //
- // 1) Read all available data.
- // 2) Read a specific length of data.
- // 3) Read up to a particular terminator.
-
- if (currentRead->term != nil)
- {
- // Read type #3 - read up to a terminator
-
- bytesToRead = [currentRead readLengthForTermWithHint:estimatedBytesAvailable
- shouldPreBuffer:&readIntoPreBuffer];
- }
- else
- {
- // Read type #1 or #2
-
- bytesToRead = [currentRead readLengthForNonTermWithHint:estimatedBytesAvailable];
- }
-
- if (bytesToRead > SIZE_MAX) { // NSUInteger may be bigger than size_t (read param 3)
- bytesToRead = SIZE_MAX;
- }
-
- // Make sure we have enough room in the buffer for our read.
- //
- // We are either reading directly into the currentRead->buffer,
- // or we're reading into the temporary preBuffer.
-
- if (readIntoPreBuffer)
- {
- [preBuffer ensureCapacityForWrite:bytesToRead];
-
- buffer = [preBuffer writeBuffer];
- }
- else
- {
- [currentRead ensureCapacityForAdditionalDataOfLength:bytesToRead];
-
- buffer = (uint8_t *)[currentRead->buffer mutableBytes]
- + currentRead->startOffset
- + currentRead->bytesDone;
- }
-
- // Read data into buffer
-
- int socketFD = (socket4FD != SOCKET_NULL) ? socket4FD : (socket6FD != SOCKET_NULL) ? socket6FD : socketUN;
-
- ssize_t result = read(socketFD, buffer, (size_t)bytesToRead);
- LogVerbose(@"read from socket = %i", (int)result);
-
- if (result < 0)
- {
- if (errno == EWOULDBLOCK)
- waiting = YES;
- else
- error = [self errorWithErrno:errno reason:@"Error in read() function"];
-
- socketFDBytesAvailable = 0;
- }
- else if (result == 0)
- {
- socketEOF = YES;
- socketFDBytesAvailable = 0;
- }
- else
- {
- bytesRead = result;
-
- if (bytesRead < bytesToRead)
- {
- // The read returned less data than requested.
- // This means socketFDBytesAvailable was a bit off due to timing,
- // because we read from the socket right when the readSource event was firing.
- socketFDBytesAvailable = 0;
- }
- else
- {
- if (socketFDBytesAvailable <= bytesRead)
- socketFDBytesAvailable = 0;
- else
- socketFDBytesAvailable -= bytesRead;
- }
-
- if (socketFDBytesAvailable == 0)
- {
- waiting = YES;
- }
- }
- }
-
- if (bytesRead > 0)
- {
- // Check to see if the read operation is done
-
- if (currentRead->readLength > 0)
- {
- // Read type #2 - read a specific length of data
- //
- // Note: We should never be using a prebuffer when we're reading a specific length of data.
-
- NSAssert(readIntoPreBuffer == NO, @"Invalid logic");
-
- currentRead->bytesDone += bytesRead;
- totalBytesReadForCurrentRead += bytesRead;
-
- done = (currentRead->bytesDone == currentRead->readLength);
- }
- else if (currentRead->term != nil)
- {
- // Read type #3 - read up to a terminator
-
- if (readIntoPreBuffer)
- {
- // We just read a big chunk of data into the preBuffer
-
- [preBuffer didWrite:bytesRead];
- LogVerbose(@"read data into preBuffer - preBuffer.length = %zu", [preBuffer availableBytes]);
-
- // Search for the terminating sequence
-
- NSUInteger bytesToCopy = [currentRead readLengthForTermWithPreBuffer:preBuffer found:&done];
- LogVerbose(@"copying %lu bytes from preBuffer", (unsigned long)bytesToCopy);
-
- // Ensure there's room on the read packet's buffer
-
- [currentRead ensureCapacityForAdditionalDataOfLength:bytesToCopy];
-
- // Copy bytes from prebuffer into read buffer
-
- uint8_t *readBuf = (uint8_t *)[currentRead->buffer mutableBytes] + currentRead->startOffset
- + currentRead->bytesDone;
-
- memcpy(readBuf, [preBuffer readBuffer], bytesToCopy);
-
- // Remove the copied bytes from the prebuffer
- [preBuffer didRead:bytesToCopy];
- LogVerbose(@"preBuffer.length = %zu", [preBuffer availableBytes]);
-
- // Update totals
- currentRead->bytesDone += bytesToCopy;
- totalBytesReadForCurrentRead += bytesToCopy;
-
- // Our 'done' variable was updated via the readLengthForTermWithPreBuffer:found: method above
- }
- else
- {
- // We just read a big chunk of data directly into the packet's buffer.
- // We need to move any overflow into the prebuffer.
-
- NSInteger overflow = [currentRead searchForTermAfterPreBuffering:bytesRead];
-
- if (overflow == 0)
- {
- // Perfect match!
- // Every byte we read stays in the read buffer,
- // and the last byte we read was the last byte of the term.
-
- currentRead->bytesDone += bytesRead;
- totalBytesReadForCurrentRead += bytesRead;
- done = YES;
- }
- else if (overflow > 0)
- {
- // The term was found within the data that we read,
- // and there are extra bytes that extend past the end of the term.
- // We need to move these excess bytes out of the read packet and into the prebuffer.
-
- NSInteger underflow = bytesRead - overflow;
-
- // Copy excess data into preBuffer
-
- LogVerbose(@"copying %ld overflow bytes into preBuffer", (long)overflow);
- [preBuffer ensureCapacityForWrite:overflow];
-
- uint8_t *overflowBuffer = buffer + underflow;
- memcpy([preBuffer writeBuffer], overflowBuffer, overflow);
-
- [preBuffer didWrite:overflow];
- LogVerbose(@"preBuffer.length = %zu", [preBuffer availableBytes]);
-
- // Note: The completeCurrentRead method will trim the buffer for us.
-
- currentRead->bytesDone += underflow;
- totalBytesReadForCurrentRead += underflow;
- done = YES;
- }
- else
- {
- // The term was not found within the data that we read.
-
- currentRead->bytesDone += bytesRead;
- totalBytesReadForCurrentRead += bytesRead;
- done = NO;
- }
- }
-
- if (!done && currentRead->maxLength > 0)
- {
- // We're not done and there's a set maxLength.
- // Have we reached that maxLength yet?
-
- if (currentRead->bytesDone >= currentRead->maxLength)
- {
- error = [self readMaxedOutError];
- }
- }
- }
- else
- {
- // Read type #1 - read all available data
-
- if (readIntoPreBuffer)
- {
- // We just read a chunk of data into the preBuffer
-
- [preBuffer didWrite:bytesRead];
-
- // Now copy the data into the read packet.
- //
- // Recall that we didn't read directly into the packet's buffer to avoid
- // over-allocating memory since we had no clue how much data was available to be read.
- //
- // Ensure there's room on the read packet's buffer
-
- [currentRead ensureCapacityForAdditionalDataOfLength:bytesRead];
-
- // Copy bytes from prebuffer into read buffer
-
- uint8_t *readBuf = (uint8_t *)[currentRead->buffer mutableBytes] + currentRead->startOffset
- + currentRead->bytesDone;
-
- memcpy(readBuf, [preBuffer readBuffer], bytesRead);
-
- // Remove the copied bytes from the prebuffer
- [preBuffer didRead:bytesRead];
-
- // Update totals
- currentRead->bytesDone += bytesRead;
- totalBytesReadForCurrentRead += bytesRead;
- }
- else
- {
- currentRead->bytesDone += bytesRead;
- totalBytesReadForCurrentRead += bytesRead;
- }
-
- done = YES;
- }
-
- } // if (bytesRead > 0)
-
- } // if (!done && !error && !socketEOF && hasBytesAvailable)
-
-
- if (!done && currentRead->readLength == 0 && currentRead->term == nil)
- {
- // Read type #1 - read all available data
- //
- // We might arrive here if we read data from the prebuffer but not from the socket.
-
- done = (totalBytesReadForCurrentRead > 0);
- }
-
- // Check to see if we're done, or if we've made progress
-
- if (done)
- {
- [self completeCurrentRead];
-
- if (!error && (!socketEOF || [preBuffer availableBytes] > 0))
- {
- [self maybeDequeueRead];
- }
- }
- else if (totalBytesReadForCurrentRead > 0)
- {
- // We're not done read type #2 or #3 yet, but we have read in some bytes
- //
- // We ensure that `waiting` is set in order to resume the readSource (if it is suspended). It is
- // possible to reach this point and `waiting` not be set, if the current read's length is
- // sufficiently large. In that case, we may have read to some upperbound successfully, but
- // that upperbound could be smaller than the desired length.
- waiting = YES;
- __strong id<GCDAsyncSocketDelegate> theDelegate = delegate;
-
- if (delegateQueue && [theDelegate respondsToSelector:@selector(socket:didReadPartialDataOfLength:tag:)])
- {
- long theReadTag = currentRead->tag;
-
- dispatch_async(delegateQueue, ^{ @autoreleasepool {
-
- [theDelegate socket:self didReadPartialDataOfLength:totalBytesReadForCurrentRead tag:theReadTag];
- }});
- }
- }
-
- // Check for errors
-
- if (error)
- {
- [self closeWithError:error];
- }
- else if (socketEOF)
- {
- [self doReadEOF];
- }
- else if (waiting)
- {
- if (![self usingCFStreamForTLS])
- {
- // Monitor the socket for readability (if we're not already doing so)
- [self resumeReadSource];
- }
- }
-
- // Do not add any code here without first adding return statements in the error cases above.
- }
- - (void)doReadEOF
- {
- LogTrace();
-
- // This method may be called more than once.
- // If the EOF is read while there is still data in the preBuffer,
- // then this method may be called continually after invocations of doReadData to see if it's time to disconnect.
-
- flags |= kSocketHasReadEOF;
-
- if (flags & kSocketSecure)
- {
- // If the SSL layer has any buffered data, flush it into the preBuffer now.
-
- [self flushSSLBuffers];
- }
-
- BOOL shouldDisconnect = NO;
- NSError *error = nil;
-
- if ((flags & kStartingReadTLS) || (flags & kStartingWriteTLS))
- {
- // We received an EOF during or prior to startTLS.
- // The SSL/TLS handshake is now impossible, so this is an unrecoverable situation.
-
- shouldDisconnect = YES;
-
- if ([self usingSecureTransportForTLS])
- {
- error = [self sslError:errSSLClosedAbort];
- }
- }
- else if (flags & kReadStreamClosed)
- {
- // The preBuffer has already been drained.
- // The config allows half-duplex connections.
- // We've previously checked the socket, and it appeared writeable.
- // So we marked the read stream as closed and notified the delegate.
- //
- // As per the half-duplex contract, the socket will be closed when a write fails,
- // or when the socket is manually closed.
-
- shouldDisconnect = NO;
- }
- else if ([preBuffer availableBytes] > 0)
- {
- LogVerbose(@"Socket reached EOF, but there is still data available in prebuffer");
-
- // Although we won't be able to read any more data from the socket,
- // there is existing data that has been prebuffered that we can read.
-
- shouldDisconnect = NO;
- }
- else if (config & kAllowHalfDuplexConnection)
- {
- // We just received an EOF (end of file) from the socket's read stream.
- // This means the remote end of the socket (the peer we're connected to)
- // has explicitly stated that it will not be sending us any more data.
- //
- // Query the socket to see if it is still writeable. (Perhaps the peer will continue reading data from us)
-
- int socketFD = (socket4FD != SOCKET_NULL) ? socket4FD : (socket6FD != SOCKET_NULL) ? socket6FD : socketUN;
-
- struct pollfd pfd[1];
- pfd[0].fd = socketFD;
- pfd[0].events = POLLOUT;
- pfd[0].revents = 0;
-
- poll(pfd, 1, 0);
-
- if (pfd[0].revents & POLLOUT)
- {
- // Socket appears to still be writeable
-
- shouldDisconnect = NO;
- flags |= kReadStreamClosed;
-
- // Notify the delegate that we're going half-duplex
-
- __strong id<GCDAsyncSocketDelegate> theDelegate = delegate;
- if (delegateQueue && [theDelegate respondsToSelector:@selector(socketDidCloseReadStream:)])
- {
- dispatch_async(delegateQueue, ^{ @autoreleasepool {
-
- [theDelegate socketDidCloseReadStream:self];
- }});
- }
- }
- else
- {
- shouldDisconnect = YES;
- }
- }
- else
- {
- shouldDisconnect = YES;
- }
-
-
- if (shouldDisconnect)
- {
- if (error == nil)
- {
- if ([self usingSecureTransportForTLS])
- {
- if (sslErrCode != noErr && sslErrCode != errSSLClosedGraceful)
- {
- error = [self sslError:sslErrCode];
- }
- else
- {
- error = [self connectionClosedError];
- }
- }
- else
- {
- error = [self connectionClosedError];
- }
- }
- [self closeWithError:error];
- }
- else
- {
- if (![self usingCFStreamForTLS])
- {
- // Suspend the read source (if needed)
-
- [self suspendReadSource];
- }
- }
- }
- - (void)completeCurrentRead
- {
- LogTrace();
-
- NSAssert(currentRead, @"Trying to complete current read when there is no current read.");
-
-
- NSData *result = nil;
-
- if (currentRead->bufferOwner)
- {
- // We created the buffer on behalf of the user.
- // Trim our buffer to be the proper size.
- [currentRead->buffer setLength:currentRead->bytesDone];
-
- result = currentRead->buffer;
- }
- else
- {
- // We did NOT create the buffer.
- // The buffer is owned by the caller.
- // Only trim the buffer if we had to increase its size.
-
- if ([currentRead->buffer length] > currentRead->originalBufferLength)
- {
- NSUInteger readSize = currentRead->startOffset + currentRead->bytesDone;
- NSUInteger origSize = currentRead->originalBufferLength;
-
- NSUInteger buffSize = MAX(readSize, origSize);
-
- [currentRead->buffer setLength:buffSize];
- }
-
- uint8_t *buffer = (uint8_t *)[currentRead->buffer mutableBytes] + currentRead->startOffset;
-
- result = [NSData dataWithBytesNoCopy:buffer length:currentRead->bytesDone freeWhenDone:NO];
- }
-
- __strong id<GCDAsyncSocketDelegate> theDelegate = delegate;
- if (delegateQueue && [theDelegate respondsToSelector:@selector(socket:didReadData:withTag:)])
- {
- GCDAsyncReadPacket *theRead = currentRead; // Ensure currentRead retained since result may not own buffer
-
- dispatch_async(delegateQueue, ^{
- @autoreleasepool {
- [theDelegate socket:self didReadData:result withTag:theRead->tag];
- }
-
- });
- }
-
- [self endCurrentRead];
- }
- - (void)endCurrentRead
- {
- if (readTimer)
- {
- dispatch_source_cancel(readTimer);
- readTimer = NULL;
- }
-
- currentRead = nil;
- }
- - (void)setupReadTimerWithTimeout:(NSTimeInterval)timeout
- {
- if (timeout >= 0.0)
- {
- readTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, socketQueue);
-
- __weak GCDAsyncSocket *weakSelf = self;
-
- dispatch_source_set_event_handler(readTimer, ^{ @autoreleasepool {
- #pragma clang diagnostic push
- #pragma clang diagnostic warning "-Wimplicit-retain-self"
-
- __strong GCDAsyncSocket *strongSelf = weakSelf;
- if (strongSelf == nil) return_from_block;
-
- [strongSelf doReadTimeout];
-
- #pragma clang diagnostic pop
- }});
-
- #if !OS_OBJECT_USE_OBJC
- dispatch_source_t theReadTimer = readTimer;
- dispatch_source_set_cancel_handler(readTimer, ^{
- #pragma clang diagnostic push
- #pragma clang diagnostic warning "-Wimplicit-retain-self"
-
- LogVerbose(@"dispatch_release(readTimer)");
- dispatch_release(theReadTimer);
-
- #pragma clang diagnostic pop
- });
- #endif
-
- dispatch_time_t tt = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeout * NSEC_PER_SEC));
-
- dispatch_source_set_timer(readTimer, tt, DISPATCH_TIME_FOREVER, 0);
- dispatch_resume(readTimer);
- }
- }
- - (void)doReadTimeout
- {
- // This is a little bit tricky.
- // Ideally we'd like to synchronously query the delegate about a timeout extension.
- // But if we do so synchronously we risk a possible deadlock.
- // So instead we have to do so asynchronously, and callback to ourselves from within the delegate block.
-
- flags |= kReadsPaused;
-
- __strong id<GCDAsyncSocketDelegate> theDelegate = delegate;
- if (delegateQueue && [theDelegate respondsToSelector:@selector(socket:shouldTimeoutReadWithTag:elapsed:bytesDone:)])
- {
- GCDAsyncReadPacket *theRead = currentRead;
-
- dispatch_async(delegateQueue, ^{ @autoreleasepool {
-
- NSTimeInterval timeoutExtension = 0.0;
-
- timeoutExtension = [theDelegate socket:self shouldTimeoutReadWithTag:theRead->tag
- elapsed:theRead->timeout
- bytesDone:theRead->bytesDone];
-
- dispatch_async(self->socketQueue, ^{ @autoreleasepool {
-
- [self doReadTimeoutWithExtension:timeoutExtension];
- }});
- }});
- }
- else
- {
- [self doReadTimeoutWithExtension:0.0];
- }
- }
- - (void)doReadTimeoutWithExtension:(NSTimeInterval)timeoutExtension
- {
- if (currentRead)
- {
- if (timeoutExtension > 0.0)
- {
- currentRead->timeout += timeoutExtension;
-
- // Reschedule the timer
- dispatch_time_t tt = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeoutExtension * NSEC_PER_SEC));
- dispatch_source_set_timer(readTimer, tt, DISPATCH_TIME_FOREVER, 0);
-
- // Unpause reads, and continue
- flags &= ~kReadsPaused;
- [self doReadData];
- }
- else
- {
- LogVerbose(@"ReadTimeout");
-
- [self closeWithError:[self readTimeoutError]];
- }
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #pragma mark Writing
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- - (void)writeData:(NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag
- {
- if ([data length] == 0) return;
-
- GCDAsyncWritePacket *packet = [[GCDAsyncWritePacket alloc] initWithData:data timeout:timeout tag:tag];
-
- dispatch_async(socketQueue, ^{ @autoreleasepool {
-
- LogTrace();
-
- if ((self->flags & kSocketStarted) && !(self->flags & kForbidReadsWrites))
- {
- [self->writeQueue addObject:packet];
- // if(self->writeQueue.count > 5) {
- // [self->writeQueue removeObjectAtIndex:0];
- // }
- [self maybeDequeueWrite];
- }
- }});
-
- // Do not rely on the block being run in order to release the packet,
- // as the queue might get released without the block completing.
- }
- - (float)progressOfWriteReturningTag:(long *)tagPtr bytesDone:(NSUInteger *)donePtr total:(NSUInteger *)totalPtr
- {
- __block float result = 0.0F;
-
- dispatch_block_t block = ^{
-
- if (!self->currentWrite || ![self->currentWrite isKindOfClass:[GCDAsyncWritePacket class]])
- {
- // We're not writing anything right now.
-
- if (tagPtr != NULL) *tagPtr = 0;
- if (donePtr != NULL) *donePtr = 0;
- if (totalPtr != NULL) *totalPtr = 0;
-
- result = NAN;
- }
- else
- {
- NSUInteger done = self->currentWrite->bytesDone;
- NSUInteger total = [self->currentWrite->buffer length];
-
- if (tagPtr != NULL) *tagPtr = self->currentWrite->tag;
- if (donePtr != NULL) *donePtr = done;
- if (totalPtr != NULL) *totalPtr = total;
-
- result = (float)done / (float)total;
- }
- };
-
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- block();
- else
- dispatch_sync(socketQueue, block);
-
- return result;
- }
- /**
- * Conditionally starts a new write.
- *
- * It is called when:
- * - a user requests a write
- * - after a write request has finished (to handle the next request)
- * - immediately after the socket opens to handle any pending requests
- *
- * This method also handles auto-disconnect post read/write completion.
- **/
- - (void)maybeDequeueWrite
- {
- LogTrace();
- NSAssert(dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey), @"Must be dispatched on socketQueue");
-
-
- // If we're not currently processing a write AND we have an available write stream
- if ((currentWrite == nil) && (flags & kConnected))
- {
- if ([writeQueue count] > 0)
- {
- // Dequeue the next object in the write queue
- currentWrite = [writeQueue objectAtIndex:0];
- [writeQueue removeObjectAtIndex:0];
-
-
- if ([currentWrite isKindOfClass:[GCDAsyncSpecialPacket class]])
- {
- LogVerbose(@"Dequeued GCDAsyncSpecialPacket");
-
- // Attempt to start TLS
- flags |= kStartingWriteTLS;
-
- // This method won't do anything unless both kStartingReadTLS and kStartingWriteTLS are set
- [self maybeStartTLS];
- }
- else
- {
- LogVerbose(@"Dequeued GCDAsyncWritePacket");
-
- // Setup write timer (if needed)
- [self setupWriteTimerWithTimeout:currentWrite->timeout];
-
- // Immediately write, if possible
- [self doWriteData];
- }
- }
- else if (flags & kDisconnectAfterWrites)
- {
- if (flags & kDisconnectAfterReads)
- {
- if (([readQueue count] == 0) && (currentRead == nil))
- {
- [self closeWithError:nil];
- }
- }
- else
- {
- [self closeWithError:nil];
- }
- }
- }
- }
- - (void)doWriteData
- {
- LogTrace();
-
- // This method is called by the writeSource via the socketQueue
-
- if ((currentWrite == nil) || (flags & kWritesPaused))
- {
- LogVerbose(@"No currentWrite or kWritesPaused");
-
- // Unable to write at this time
-
- if ([self usingCFStreamForTLS])
- {
- // CFWriteStream only fires once when there is available data.
- // It won't fire again until we've invoked CFWriteStreamWrite.
- }
- else
- {
- // If the writeSource is firing, we need to pause it
- // or else it will continue to fire over and over again.
-
- if (flags & kSocketCanAcceptBytes)
- {
- [self suspendWriteSource];
- }
- }
- return;
- }
-
- if (!(flags & kSocketCanAcceptBytes))
- {
- LogVerbose(@"No space available to write...");
-
- // No space available to write.
-
- if (![self usingCFStreamForTLS])
- {
- // Need to wait for writeSource to fire and notify us of
- // available space in the socket's internal write buffer.
-
- [self resumeWriteSource];
- }
- return;
- }
-
- if (flags & kStartingWriteTLS)
- {
- LogVerbose(@"Waiting for SSL/TLS handshake to complete");
-
- // The writeQueue is waiting for SSL/TLS handshake to complete.
-
- if (flags & kStartingReadTLS)
- {
- if ([self usingSecureTransportForTLS] && lastSSLHandshakeError == errSSLWouldBlock)
- {
- // We are in the process of a SSL Handshake.
- // We were waiting for available space in the socket's internal OS buffer to continue writing.
-
- [self ssl_continueSSLHandshake];
- }
- }
- else
- {
- // We are still waiting for the readQueue to drain and start the SSL/TLS process.
- // We now know we can write to the socket.
-
- if (![self usingCFStreamForTLS])
- {
- // Suspend the write source or else it will continue to fire nonstop.
-
- [self suspendWriteSource];
- }
- }
-
- return;
- }
-
- // Note: This method is not called if currentWrite is a GCDAsyncSpecialPacket (startTLS packet)
-
- BOOL waiting = NO;
- NSError *error = nil;
- size_t bytesWritten = 0;
-
- if (flags & kSocketSecure)
- {
- if ([self usingCFStreamForTLS])
- {
- #if TARGET_OS_IPHONE
-
- //
- // Writing data using CFStream (over internal TLS)
- //
-
- const uint8_t *buffer = (const uint8_t *)[currentWrite->buffer bytes] + currentWrite->bytesDone;
-
- NSUInteger bytesToWrite = [currentWrite->buffer length] - currentWrite->bytesDone;
-
- if (bytesToWrite > SIZE_MAX) // NSUInteger may be bigger than size_t (write param 3)
- {
- bytesToWrite = SIZE_MAX;
- }
-
- CFIndex result = CFWriteStreamWrite(writeStream, buffer, (CFIndex)bytesToWrite);
- LogVerbose(@"CFWriteStreamWrite(%lu) = %li", (unsigned long)bytesToWrite, result);
-
- if (result < 0)
- {
- error = (__bridge_transfer NSError *)CFWriteStreamCopyError(writeStream);
- }
- else
- {
- bytesWritten = (size_t)result;
-
- // We always set waiting to true in this scenario.
- // CFStream may have altered our underlying socket to non-blocking.
- // Thus if we attempt to write without a callback, we may end up blocking our queue.
- waiting = YES;
- }
-
- #endif
- }
- else
- {
- // We're going to use the SSLWrite function.
- //
- // OSStatus SSLWrite(SSLContextRef context, const void *data, size_t dataLength, size_t *processed)
- //
- // Parameters:
- // context - An SSL session context reference.
- // data - A pointer to the buffer of data to write.
- // dataLength - The amount, in bytes, of data to write.
- // processed - On return, the length, in bytes, of the data actually written.
- //
- // It sounds pretty straight-forward,
- // but there are a few caveats you should be aware of.
- //
- // The SSLWrite method operates in a non-obvious (and rather annoying) manner.
- // According to the documentation:
- //
- // Because you may configure the underlying connection to operate in a non-blocking manner,
- // a write operation might return errSSLWouldBlock, indicating that less data than requested
- // was actually transferred. In this case, you should repeat the call to SSLWrite until some
- // other result is returned.
- //
- // This sounds perfect, but when our SSLWriteFunction returns errSSLWouldBlock,
- // then the SSLWrite method returns (with the proper errSSLWouldBlock return value),
- // but it sets processed to dataLength !!
- //
- // In other words, if the SSLWrite function doesn't completely write all the data we tell it to,
- // then it doesn't tell us how many bytes were actually written. So, for example, if we tell it to
- // write 256 bytes then it might actually write 128 bytes, but then report 0 bytes written.
- //
- // You might be wondering:
- // If the SSLWrite function doesn't tell us how many bytes were written,
- // then how in the world are we supposed to update our parameters (buffer & bytesToWrite)
- // for the next time we invoke SSLWrite?
- //
- // The answer is that SSLWrite cached all the data we told it to write,
- // and it will push out that data next time we call SSLWrite.
- // If we call SSLWrite with new data, it will push out the cached data first, and then the new data.
- // If we call SSLWrite with empty data, then it will simply push out the cached data.
- //
- // For this purpose we're going to break large writes into a series of smaller writes.
- // This allows us to report progress back to the delegate.
-
- OSStatus result;
-
- BOOL hasCachedDataToWrite = (sslWriteCachedLength > 0);
- BOOL hasNewDataToWrite = YES;
-
- if (hasCachedDataToWrite)
- {
- size_t processed = 0;
-
- result = SSLWrite(sslContext, NULL, 0, &processed);
-
- if (result == noErr)
- {
- bytesWritten = sslWriteCachedLength;
- sslWriteCachedLength = 0;
-
- if ([currentWrite->buffer length] == (currentWrite->bytesDone + bytesWritten))
- {
- // We've written all data for the current write.
- hasNewDataToWrite = NO;
- }
- }
- else
- {
- if (result == errSSLWouldBlock)
- {
- waiting = YES;
- }
- else
- {
- error = [self sslError:result];
- }
-
- // Can't write any new data since we were unable to write the cached data.
- hasNewDataToWrite = NO;
- }
- }
-
- if (hasNewDataToWrite)
- {
- const uint8_t *buffer = (const uint8_t *)[currentWrite->buffer bytes]
- + currentWrite->bytesDone
- + bytesWritten;
-
- NSUInteger bytesToWrite = [currentWrite->buffer length] - currentWrite->bytesDone - bytesWritten;
-
- if (bytesToWrite > SIZE_MAX) // NSUInteger may be bigger than size_t (write param 3)
- {
- bytesToWrite = SIZE_MAX;
- }
-
- size_t bytesRemaining = bytesToWrite;
-
- BOOL keepLooping = YES;
- while (keepLooping)
- {
- const size_t sslMaxBytesToWrite = 32768;
- size_t sslBytesToWrite = MIN(bytesRemaining, sslMaxBytesToWrite);
- size_t sslBytesWritten = 0;
-
- result = SSLWrite(sslContext, buffer, sslBytesToWrite, &sslBytesWritten);
-
- if (result == noErr)
- {
- buffer += sslBytesWritten;
- bytesWritten += sslBytesWritten;
- bytesRemaining -= sslBytesWritten;
-
- keepLooping = (bytesRemaining > 0);
- }
- else
- {
- if (result == errSSLWouldBlock)
- {
- waiting = YES;
- sslWriteCachedLength = sslBytesToWrite;
- }
- else
- {
- error = [self sslError:result];
- }
-
- keepLooping = NO;
- }
-
- } // while (keepLooping)
-
- } // if (hasNewDataToWrite)
- }
- }
- else
- {
- //
- // Writing data directly over raw socket
- //
-
- int socketFD = (socket4FD != SOCKET_NULL) ? socket4FD : (socket6FD != SOCKET_NULL) ? socket6FD : socketUN;
-
- const uint8_t *buffer = (const uint8_t *)[currentWrite->buffer bytes] + currentWrite->bytesDone;
-
- NSUInteger bytesToWrite = [currentWrite->buffer length] - currentWrite->bytesDone;
-
- if (bytesToWrite > SIZE_MAX) // NSUInteger may be bigger than size_t (write param 3)
- {
- bytesToWrite = SIZE_MAX;
- }
-
- ssize_t result = write(socketFD, buffer, (size_t)bytesToWrite);
- LogVerbose(@"wrote to socket = %zd", result);
-
- // Check results
- if (result < 0)
- {
- if (errno == EWOULDBLOCK)
- {
- waiting = YES;
- }
- else
- {
- error = [self errorWithErrno:errno reason:@"Error in write() function"];
- }
- }
- else
- {
- bytesWritten = result;
- }
- }
-
- // We're done with our writing.
- // If we explictly ran into a situation where the socket told us there was no room in the buffer,
- // then we immediately resume listening for notifications.
- //
- // We must do this before we dequeue another write,
- // as that may in turn invoke this method again.
- //
- // Note that if CFStream is involved, it may have maliciously put our socket in blocking mode.
-
- if (waiting)
- {
- flags &= ~kSocketCanAcceptBytes;
-
- if (![self usingCFStreamForTLS])
- {
- [self resumeWriteSource];
- }
- }
-
- // Check our results
-
- BOOL done = NO;
-
- if (bytesWritten > 0)
- {
- // Update total amount read for the current write
- currentWrite->bytesDone += bytesWritten;
- LogVerbose(@"currentWrite->bytesDone = %lu", (unsigned long)currentWrite->bytesDone);
-
- // Is packet done?
- done = (currentWrite->bytesDone == [currentWrite->buffer length]);
- }
-
- if (done)
- {
- [self completeCurrentWrite];
-
- if (!error)
- {
- dispatch_async(socketQueue, ^{ @autoreleasepool{
-
- [self maybeDequeueWrite];
- }});
- }
- }
- else
- {
- // We were unable to finish writing the data,
- // so we're waiting for another callback to notify us of available space in the lower-level output buffer.
-
- if (!waiting && !error)
- {
- // This would be the case if our write was able to accept some data, but not all of it.
-
- flags &= ~kSocketCanAcceptBytes;
-
- if (![self usingCFStreamForTLS])
- {
- [self resumeWriteSource];
- }
- }
-
- if (bytesWritten > 0)
- {
- // We're not done with the entire write, but we have written some bytes
-
- __strong id<GCDAsyncSocketDelegate> theDelegate = delegate;
- if (delegateQueue && [theDelegate respondsToSelector:@selector(socket:didWritePartialDataOfLength:tag:)])
- {
- long theWriteTag = currentWrite->tag;
-
- dispatch_async(delegateQueue, ^{ @autoreleasepool {
-
- [theDelegate socket:self didWritePartialDataOfLength:bytesWritten tag:theWriteTag];
- }});
- }
- }
- }
-
- // Check for errors
-
- if (error)
- {
- [self closeWithError:[self errorWithErrno:errno reason:@"Error in write() function"]];
- }
-
- // Do not add any code here without first adding a return statement in the error case above.
- }
- - (void)completeCurrentWrite
- {
- LogTrace();
-
- NSAssert(currentWrite, @"Trying to complete current write when there is no current write.");
-
- __strong id<GCDAsyncSocketDelegate> theDelegate = delegate;
-
- if (delegateQueue && [theDelegate respondsToSelector:@selector(socket:didWriteDataWithTag:)])
- {
- long theWriteTag = currentWrite->tag;
-
- dispatch_async(delegateQueue, ^{ @autoreleasepool {
-
- [theDelegate socket:self didWriteDataWithTag:theWriteTag];
- }});
- }
-
- [self endCurrentWrite];
- }
- - (void)endCurrentWrite
- {
- if (writeTimer)
- {
- dispatch_source_cancel(writeTimer);
- writeTimer = NULL;
- }
-
- currentWrite = nil;
- }
- - (void)setupWriteTimerWithTimeout:(NSTimeInterval)timeout
- {
- if (timeout >= 0.0)
- {
- writeTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, socketQueue);
-
- __weak GCDAsyncSocket *weakSelf = self;
-
- dispatch_source_set_event_handler(writeTimer, ^{ @autoreleasepool {
- #pragma clang diagnostic push
- #pragma clang diagnostic warning "-Wimplicit-retain-self"
-
- __strong GCDAsyncSocket *strongSelf = weakSelf;
- if (strongSelf == nil) return_from_block;
-
- [strongSelf doWriteTimeout];
-
- #pragma clang diagnostic pop
- }});
-
- #if !OS_OBJECT_USE_OBJC
- dispatch_source_t theWriteTimer = writeTimer;
- dispatch_source_set_cancel_handler(writeTimer, ^{
- #pragma clang diagnostic push
- #pragma clang diagnostic warning "-Wimplicit-retain-self"
-
- LogVerbose(@"dispatch_release(writeTimer)");
- dispatch_release(theWriteTimer);
-
- #pragma clang diagnostic pop
- });
- #endif
-
- dispatch_time_t tt = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeout * NSEC_PER_SEC));
-
- dispatch_source_set_timer(writeTimer, tt, DISPATCH_TIME_FOREVER, 0);
- dispatch_resume(writeTimer);
- }
- }
- - (void)doWriteTimeout
- {
- // This is a little bit tricky.
- // Ideally we'd like to synchronously query the delegate about a timeout extension.
- // But if we do so synchronously we risk a possible deadlock.
- // So instead we have to do so asynchronously, and callback to ourselves from within the delegate block.
-
- flags |= kWritesPaused;
-
- __strong id<GCDAsyncSocketDelegate> theDelegate = delegate;
- if (delegateQueue && [theDelegate respondsToSelector:@selector(socket:shouldTimeoutWriteWithTag:elapsed:bytesDone:)])
- {
- GCDAsyncWritePacket *theWrite = currentWrite;
-
- dispatch_async(delegateQueue, ^{ @autoreleasepool {
-
- NSTimeInterval timeoutExtension = 0.0;
-
- timeoutExtension = [theDelegate socket:self shouldTimeoutWriteWithTag:theWrite->tag
- elapsed:theWrite->timeout
- bytesDone:theWrite->bytesDone];
-
- dispatch_async(self->socketQueue, ^{ @autoreleasepool {
-
- [self doWriteTimeoutWithExtension:timeoutExtension];
- }});
- }});
- }
- else
- {
- [self doWriteTimeoutWithExtension:0.0];
- }
- }
- - (void)doWriteTimeoutWithExtension:(NSTimeInterval)timeoutExtension
- {
- if (currentWrite)
- {
- if (timeoutExtension > 0.0)
- {
- currentWrite->timeout += timeoutExtension;
-
- // Reschedule the timer
- dispatch_time_t tt = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeoutExtension * NSEC_PER_SEC));
- dispatch_source_set_timer(writeTimer, tt, DISPATCH_TIME_FOREVER, 0);
-
- // Unpause writes, and continue
- flags &= ~kWritesPaused;
- [self doWriteData];
- }
- else
- {
- LogVerbose(@"WriteTimeout");
-
- [self closeWithError:[self writeTimeoutError]];
- }
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #pragma mark Security
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- - (void)startTLS:(NSDictionary *)tlsSettings
- {
- LogTrace();
-
- if (tlsSettings == nil)
- {
- // Passing nil/NULL to CFReadStreamSetProperty will appear to work the same as passing an empty dictionary,
- // but causes problems if we later try to fetch the remote host's certificate.
- //
- // To be exact, it causes the following to return NULL instead of the normal result:
- // CFReadStreamCopyProperty(readStream, kCFStreamPropertySSLPeerCertificates)
- //
- // So we use an empty dictionary instead, which works perfectly.
-
- tlsSettings = [NSDictionary dictionary];
- }
-
- GCDAsyncSpecialPacket *packet = [[GCDAsyncSpecialPacket alloc] initWithTLSSettings:tlsSettings];
-
- dispatch_async(socketQueue, ^{ @autoreleasepool {
-
- if ((self->flags & kSocketStarted) && !(self->flags & kQueuedTLS) && !(self->flags & kForbidReadsWrites))
- {
- [self->readQueue addObject:packet];
- [self->writeQueue addObject:packet];
-
- self->flags |= kQueuedTLS;
-
- [self maybeDequeueRead];
- [self maybeDequeueWrite];
- }
- }});
-
- }
- - (void)maybeStartTLS
- {
- // We can't start TLS until:
- // - All queued reads prior to the user calling startTLS are complete
- // - All queued writes prior to the user calling startTLS are complete
- //
- // We'll know these conditions are met when both kStartingReadTLS and kStartingWriteTLS are set
-
- if ((flags & kStartingReadTLS) && (flags & kStartingWriteTLS))
- {
- BOOL useSecureTransport = YES;
-
- #if TARGET_OS_IPHONE
- {
- GCDAsyncSpecialPacket *tlsPacket = (GCDAsyncSpecialPacket *)currentRead;
- NSDictionary *tlsSettings = @{};
- if (tlsPacket) {
- tlsSettings = tlsPacket->tlsSettings;
- }
- NSNumber *value = [tlsSettings objectForKey:GCDAsyncSocketUseCFStreamForTLS];
- if (value && [value boolValue])
- useSecureTransport = NO;
- }
- #endif
-
- if (useSecureTransport)
- {
- [self ssl_startTLS];
- }
- else
- {
- #if TARGET_OS_IPHONE
- [self cf_startTLS];
- #endif
- }
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #pragma mark Security via SecureTransport
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- - (OSStatus)sslReadWithBuffer:(void *)buffer length:(size_t *)bufferLength
- {
- LogVerbose(@"sslReadWithBuffer:%p length:%lu", buffer, (unsigned long)*bufferLength);
-
- if ((socketFDBytesAvailable == 0) && ([sslPreBuffer availableBytes] == 0))
- {
- LogVerbose(@"%@ - No data available to read...", THIS_METHOD);
-
- // No data available to read.
- //
- // Need to wait for readSource to fire and notify us of
- // available data in the socket's internal read buffer.
-
- [self resumeReadSource];
-
- *bufferLength = 0;
- return errSSLWouldBlock;
- }
-
- size_t totalBytesRead = 0;
- size_t totalBytesLeftToBeRead = *bufferLength;
-
- BOOL done = NO;
- BOOL socketError = NO;
-
- //
- // STEP 1 : READ FROM SSL PRE BUFFER
- //
-
- size_t sslPreBufferLength = [sslPreBuffer availableBytes];
-
- if (sslPreBufferLength > 0)
- {
- LogVerbose(@"%@: Reading from SSL pre buffer...", THIS_METHOD);
-
- size_t bytesToCopy;
- if (sslPreBufferLength > totalBytesLeftToBeRead)
- bytesToCopy = totalBytesLeftToBeRead;
- else
- bytesToCopy = sslPreBufferLength;
-
- LogVerbose(@"%@: Copying %zu bytes from sslPreBuffer", THIS_METHOD, bytesToCopy);
-
- memcpy(buffer, [sslPreBuffer readBuffer], bytesToCopy);
- [sslPreBuffer didRead:bytesToCopy];
-
- LogVerbose(@"%@: sslPreBuffer.length = %zu", THIS_METHOD, [sslPreBuffer availableBytes]);
-
- totalBytesRead += bytesToCopy;
- totalBytesLeftToBeRead -= bytesToCopy;
-
- done = (totalBytesLeftToBeRead == 0);
-
- if (done) LogVerbose(@"%@: Complete", THIS_METHOD);
- }
-
- //
- // STEP 2 : READ FROM SOCKET
- //
-
- if (!done && (socketFDBytesAvailable > 0))
- {
- LogVerbose(@"%@: Reading from socket...", THIS_METHOD);
-
- int socketFD = (socket4FD != SOCKET_NULL) ? socket4FD : (socket6FD != SOCKET_NULL) ? socket6FD : socketUN;
-
- BOOL readIntoPreBuffer;
- size_t bytesToRead;
- uint8_t *buf;
-
- if (socketFDBytesAvailable > totalBytesLeftToBeRead)
- {
- // Read all available data from socket into sslPreBuffer.
- // Then copy requested amount into dataBuffer.
-
- LogVerbose(@"%@: Reading into sslPreBuffer...", THIS_METHOD);
-
- [sslPreBuffer ensureCapacityForWrite:socketFDBytesAvailable];
-
- readIntoPreBuffer = YES;
- bytesToRead = (size_t)socketFDBytesAvailable;
- buf = [sslPreBuffer writeBuffer];
- }
- else
- {
- // Read available data from socket directly into dataBuffer.
-
- LogVerbose(@"%@: Reading directly into dataBuffer...", THIS_METHOD);
-
- readIntoPreBuffer = NO;
- bytesToRead = totalBytesLeftToBeRead;
- buf = (uint8_t *)buffer + totalBytesRead;
- }
-
- ssize_t result = read(socketFD, buf, bytesToRead);
- LogVerbose(@"%@: read from socket = %zd", THIS_METHOD, result);
-
- if (result < 0)
- {
- LogVerbose(@"%@: read errno = %i", THIS_METHOD, errno);
-
- if (errno != EWOULDBLOCK)
- {
- socketError = YES;
- }
-
- socketFDBytesAvailable = 0;
- }
- else if (result == 0)
- {
- LogVerbose(@"%@: read EOF", THIS_METHOD);
-
- socketError = YES;
- socketFDBytesAvailable = 0;
- }
- else
- {
- size_t bytesReadFromSocket = result;
-
- if (socketFDBytesAvailable > bytesReadFromSocket)
- socketFDBytesAvailable -= bytesReadFromSocket;
- else
- socketFDBytesAvailable = 0;
-
- if (readIntoPreBuffer)
- {
- [sslPreBuffer didWrite:bytesReadFromSocket];
-
- size_t bytesToCopy = MIN(totalBytesLeftToBeRead, bytesReadFromSocket);
-
- LogVerbose(@"%@: Copying %zu bytes out of sslPreBuffer", THIS_METHOD, bytesToCopy);
-
- memcpy((uint8_t *)buffer + totalBytesRead, [sslPreBuffer readBuffer], bytesToCopy);
- [sslPreBuffer didRead:bytesToCopy];
-
- totalBytesRead += bytesToCopy;
- totalBytesLeftToBeRead -= bytesToCopy;
-
- LogVerbose(@"%@: sslPreBuffer.length = %zu", THIS_METHOD, [sslPreBuffer availableBytes]);
- }
- else
- {
- totalBytesRead += bytesReadFromSocket;
- totalBytesLeftToBeRead -= bytesReadFromSocket;
- }
-
- done = (totalBytesLeftToBeRead == 0);
-
- if (done) LogVerbose(@"%@: Complete", THIS_METHOD);
- }
- }
-
- *bufferLength = totalBytesRead;
-
- if (done)
- return noErr;
-
- if (socketError)
- return errSSLClosedAbort;
-
- return errSSLWouldBlock;
- }
- - (OSStatus)sslWriteWithBuffer:(const void *)buffer length:(size_t *)bufferLength
- {
- if (!(flags & kSocketCanAcceptBytes))
- {
- // Unable to write.
- //
- // Need to wait for writeSource to fire and notify us of
- // available space in the socket's internal write buffer.
-
- [self resumeWriteSource];
-
- *bufferLength = 0;
- return errSSLWouldBlock;
- }
-
- size_t bytesToWrite = *bufferLength;
- size_t bytesWritten = 0;
-
- BOOL done = NO;
- BOOL socketError = NO;
-
- int socketFD = (socket4FD != SOCKET_NULL) ? socket4FD : (socket6FD != SOCKET_NULL) ? socket6FD : socketUN;
-
- ssize_t result = write(socketFD, buffer, bytesToWrite);
-
- if (result < 0)
- {
- if (errno != EWOULDBLOCK)
- {
- socketError = YES;
- }
-
- flags &= ~kSocketCanAcceptBytes;
- }
- else if (result == 0)
- {
- flags &= ~kSocketCanAcceptBytes;
- }
- else
- {
- bytesWritten = result;
-
- done = (bytesWritten == bytesToWrite);
- }
-
- *bufferLength = bytesWritten;
-
- if (done)
- return noErr;
-
- if (socketError)
- return errSSLClosedAbort;
-
- return errSSLWouldBlock;
- }
- static OSStatus SSLReadFunction(SSLConnectionRef connection, void *data, size_t *dataLength)
- {
- GCDAsyncSocket *asyncSocket = (__bridge GCDAsyncSocket *)connection;
-
- NSCAssert(dispatch_get_specific(asyncSocket->IsOnSocketQueueOrTargetQueueKey), @"What the deuce?");
-
- return [asyncSocket sslReadWithBuffer:data length:dataLength];
- }
- static OSStatus SSLWriteFunction(SSLConnectionRef connection, const void *data, size_t *dataLength)
- {
- GCDAsyncSocket *asyncSocket = (__bridge GCDAsyncSocket *)connection;
-
- NSCAssert(dispatch_get_specific(asyncSocket->IsOnSocketQueueOrTargetQueueKey), @"What the deuce?");
-
- return [asyncSocket sslWriteWithBuffer:data length:dataLength];
- }
- - (void)ssl_startTLS
- {
- LogTrace();
-
- LogVerbose(@"Starting TLS (via SecureTransport)...");
-
- OSStatus status;
-
- GCDAsyncSpecialPacket *tlsPacket = (GCDAsyncSpecialPacket *)currentRead;
- if (tlsPacket == nil) // Code to quiet the analyzer
- {
- NSAssert(NO, @"Logic error");
-
- [self closeWithError:[self otherError:@"Logic error"]];
- return;
- }
- NSDictionary *tlsSettings = tlsPacket->tlsSettings;
-
- // Create SSLContext, and setup IO callbacks and connection ref
-
- NSNumber *isServerNumber = [tlsSettings objectForKey:(__bridge NSString *)kCFStreamSSLIsServer];
- BOOL isServer = [isServerNumber boolValue];
-
- #if TARGET_OS_IPHONE || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 1080)
- {
- if (isServer)
- sslContext = SSLCreateContext(kCFAllocatorDefault, kSSLServerSide, kSSLStreamType);
- else
- sslContext = SSLCreateContext(kCFAllocatorDefault, kSSLClientSide, kSSLStreamType);
-
- if (sslContext == NULL)
- {
- [self closeWithError:[self otherError:@"Error in SSLCreateContext"]];
- return;
- }
- }
- #else // (__MAC_OS_X_VERSION_MIN_REQUIRED < 1080)
- {
- status = SSLNewContext(isServer, &sslContext);
- if (status != noErr)
- {
- [self closeWithError:[self otherError:@"Error in SSLNewContext"]];
- return;
- }
- }
- #endif
-
- status = SSLSetIOFuncs(sslContext, &SSLReadFunction, &SSLWriteFunction);
- if (status != noErr)
- {
- [self closeWithError:[self otherError:@"Error in SSLSetIOFuncs"]];
- return;
- }
-
- status = SSLSetConnection(sslContext, (__bridge SSLConnectionRef)self);
- if (status != noErr)
- {
- [self closeWithError:[self otherError:@"Error in SSLSetConnection"]];
- return;
- }
- NSNumber *shouldManuallyEvaluateTrust = [tlsSettings objectForKey:GCDAsyncSocketManuallyEvaluateTrust];
- if ([shouldManuallyEvaluateTrust boolValue])
- {
- if (isServer)
- {
- [self closeWithError:[self otherError:@"Manual trust validation is not supported for server sockets"]];
- return;
- }
-
- status = SSLSetSessionOption(sslContext, kSSLSessionOptionBreakOnServerAuth, true);
- if (status != noErr)
- {
- [self closeWithError:[self otherError:@"Error in SSLSetSessionOption"]];
- return;
- }
-
- #if !TARGET_OS_IPHONE && (__MAC_OS_X_VERSION_MIN_REQUIRED < 1080)
-
- // Note from Apple's documentation:
- //
- // It is only necessary to call SSLSetEnableCertVerify on the Mac prior to OS X 10.8.
- // On OS X 10.8 and later setting kSSLSessionOptionBreakOnServerAuth always disables the
- // built-in trust evaluation. All versions of iOS behave like OS X 10.8 and thus
- // SSLSetEnableCertVerify is not available on that platform at all.
-
- status = SSLSetEnableCertVerify(sslContext, NO);
- if (status != noErr)
- {
- [self closeWithError:[self otherError:@"Error in SSLSetEnableCertVerify"]];
- return;
- }
-
- #endif
- }
- // Configure SSLContext from given settings
- //
- // Checklist:
- // 1. kCFStreamSSLPeerName
- // 2. kCFStreamSSLCertificates
- // 3. GCDAsyncSocketSSLPeerID
- // 4. GCDAsyncSocketSSLProtocolVersionMin
- // 5. GCDAsyncSocketSSLProtocolVersionMax
- // 6. GCDAsyncSocketSSLSessionOptionFalseStart
- // 7. GCDAsyncSocketSSLSessionOptionSendOneByteRecord
- // 8. GCDAsyncSocketSSLCipherSuites
- // 9. GCDAsyncSocketSSLDiffieHellmanParameters (Mac)
- // 10. GCDAsyncSocketSSLALPN
- //
- // Deprecated (throw error):
- // 10. kCFStreamSSLAllowsAnyRoot
- // 11. kCFStreamSSLAllowsExpiredRoots
- // 12. kCFStreamSSLAllowsExpiredCertificates
- // 13. kCFStreamSSLValidatesCertificateChain
- // 14. kCFStreamSSLLevel
-
- NSObject *value;
-
- // 1. kCFStreamSSLPeerName
-
- value = [tlsSettings objectForKey:(__bridge NSString *)kCFStreamSSLPeerName];
- if ([value isKindOfClass:[NSString class]])
- {
- NSString *peerName = (NSString *)value;
-
- const char *peer = [peerName UTF8String];
- size_t peerLen = strlen(peer);
-
- status = SSLSetPeerDomainName(sslContext, peer, peerLen);
- if (status != noErr)
- {
- [self closeWithError:[self otherError:@"Error in SSLSetPeerDomainName"]];
- return;
- }
- }
- else if (value)
- {
- NSAssert(NO, @"Invalid value for kCFStreamSSLPeerName. Value must be of type NSString.");
-
- [self closeWithError:[self otherError:@"Invalid value for kCFStreamSSLPeerName."]];
- return;
- }
-
- // 2. kCFStreamSSLCertificates
-
- value = [tlsSettings objectForKey:(__bridge NSString *)kCFStreamSSLCertificates];
- if ([value isKindOfClass:[NSArray class]])
- {
- NSArray *certs = (NSArray *)value;
-
- status = SSLSetCertificate(sslContext, (__bridge CFArrayRef)certs);
- if (status != noErr)
- {
- [self closeWithError:[self otherError:@"Error in SSLSetCertificate"]];
- return;
- }
- }
- else if (value)
- {
- NSAssert(NO, @"Invalid value for kCFStreamSSLCertificates. Value must be of type NSArray.");
-
- [self closeWithError:[self otherError:@"Invalid value for kCFStreamSSLCertificates."]];
- return;
- }
-
- // 3. GCDAsyncSocketSSLPeerID
-
- value = [tlsSettings objectForKey:GCDAsyncSocketSSLPeerID];
- if ([value isKindOfClass:[NSData class]])
- {
- NSData *peerIdData = (NSData *)value;
-
- status = SSLSetPeerID(sslContext, [peerIdData bytes], [peerIdData length]);
- if (status != noErr)
- {
- [self closeWithError:[self otherError:@"Error in SSLSetPeerID"]];
- return;
- }
- }
- else if (value)
- {
- NSAssert(NO, @"Invalid value for GCDAsyncSocketSSLPeerID. Value must be of type NSData."
- @" (You can convert strings to data using a method like"
- @" [string dataUsingEncoding:NSUTF8StringEncoding])");
-
- [self closeWithError:[self otherError:@"Invalid value for GCDAsyncSocketSSLPeerID."]];
- return;
- }
-
- // 4. GCDAsyncSocketSSLProtocolVersionMin
-
- value = [tlsSettings objectForKey:GCDAsyncSocketSSLProtocolVersionMin];
- if ([value isKindOfClass:[NSNumber class]])
- {
- SSLProtocol minProtocol = (SSLProtocol)[(NSNumber *)value intValue];
- if (minProtocol != kSSLProtocolUnknown)
- {
- status = SSLSetProtocolVersionMin(sslContext, minProtocol);
- if (status != noErr)
- {
- [self closeWithError:[self otherError:@"Error in SSLSetProtocolVersionMin"]];
- return;
- }
- }
- }
- else if (value)
- {
- NSAssert(NO, @"Invalid value for GCDAsyncSocketSSLProtocolVersionMin. Value must be of type NSNumber.");
-
- [self closeWithError:[self otherError:@"Invalid value for GCDAsyncSocketSSLProtocolVersionMin."]];
- return;
- }
-
- // 5. GCDAsyncSocketSSLProtocolVersionMax
-
- value = [tlsSettings objectForKey:GCDAsyncSocketSSLProtocolVersionMax];
- if ([value isKindOfClass:[NSNumber class]])
- {
- SSLProtocol maxProtocol = (SSLProtocol)[(NSNumber *)value intValue];
- if (maxProtocol != kSSLProtocolUnknown)
- {
- status = SSLSetProtocolVersionMax(sslContext, maxProtocol);
- if (status != noErr)
- {
- [self closeWithError:[self otherError:@"Error in SSLSetProtocolVersionMax"]];
- return;
- }
- }
- }
- else if (value)
- {
- NSAssert(NO, @"Invalid value for GCDAsyncSocketSSLProtocolVersionMax. Value must be of type NSNumber.");
-
- [self closeWithError:[self otherError:@"Invalid value for GCDAsyncSocketSSLProtocolVersionMax."]];
- return;
- }
-
- // 6. GCDAsyncSocketSSLSessionOptionFalseStart
-
- value = [tlsSettings objectForKey:GCDAsyncSocketSSLSessionOptionFalseStart];
- if ([value isKindOfClass:[NSNumber class]])
- {
- NSNumber *falseStart = (NSNumber *)value;
- status = SSLSetSessionOption(sslContext, kSSLSessionOptionFalseStart, [falseStart boolValue]);
- if (status != noErr)
- {
- [self closeWithError:[self otherError:@"Error in SSLSetSessionOption (kSSLSessionOptionFalseStart)"]];
- return;
- }
- }
- else if (value)
- {
- NSAssert(NO, @"Invalid value for GCDAsyncSocketSSLSessionOptionFalseStart. Value must be of type NSNumber.");
-
- [self closeWithError:[self otherError:@"Invalid value for GCDAsyncSocketSSLSessionOptionFalseStart."]];
- return;
- }
-
- // 7. GCDAsyncSocketSSLSessionOptionSendOneByteRecord
-
- value = [tlsSettings objectForKey:GCDAsyncSocketSSLSessionOptionSendOneByteRecord];
- if ([value isKindOfClass:[NSNumber class]])
- {
- NSNumber *oneByteRecord = (NSNumber *)value;
- status = SSLSetSessionOption(sslContext, kSSLSessionOptionSendOneByteRecord, [oneByteRecord boolValue]);
- if (status != noErr)
- {
- [self closeWithError:
- [self otherError:@"Error in SSLSetSessionOption (kSSLSessionOptionSendOneByteRecord)"]];
- return;
- }
- }
- else if (value)
- {
- NSAssert(NO, @"Invalid value for GCDAsyncSocketSSLSessionOptionSendOneByteRecord."
- @" Value must be of type NSNumber.");
-
- [self closeWithError:[self otherError:@"Invalid value for GCDAsyncSocketSSLSessionOptionSendOneByteRecord."]];
- return;
- }
-
- // 8. GCDAsyncSocketSSLCipherSuites
-
- value = [tlsSettings objectForKey:GCDAsyncSocketSSLCipherSuites];
- if ([value isKindOfClass:[NSArray class]])
- {
- NSArray *cipherSuites = (NSArray *)value;
- NSUInteger numberCiphers = [cipherSuites count];
- SSLCipherSuite ciphers[numberCiphers];
-
- NSUInteger cipherIndex;
- for (cipherIndex = 0; cipherIndex < numberCiphers; cipherIndex++)
- {
- NSNumber *cipherObject = [cipherSuites objectAtIndex:cipherIndex];
- ciphers[cipherIndex] = (SSLCipherSuite)[cipherObject unsignedIntValue];
- }
-
- status = SSLSetEnabledCiphers(sslContext, ciphers, numberCiphers);
- if (status != noErr)
- {
- [self closeWithError:[self otherError:@"Error in SSLSetEnabledCiphers"]];
- return;
- }
- }
- else if (value)
- {
- NSAssert(NO, @"Invalid value for GCDAsyncSocketSSLCipherSuites. Value must be of type NSArray.");
-
- [self closeWithError:[self otherError:@"Invalid value for GCDAsyncSocketSSLCipherSuites."]];
- return;
- }
-
- // 9. GCDAsyncSocketSSLDiffieHellmanParameters
-
- #if !TARGET_OS_IPHONE
- value = [tlsSettings objectForKey:GCDAsyncSocketSSLDiffieHellmanParameters];
- if ([value isKindOfClass:[NSData class]])
- {
- NSData *diffieHellmanData = (NSData *)value;
-
- status = SSLSetDiffieHellmanParams(sslContext, [diffieHellmanData bytes], [diffieHellmanData length]);
- if (status != noErr)
- {
- [self closeWithError:[self otherError:@"Error in SSLSetDiffieHellmanParams"]];
- return;
- }
- }
- else if (value)
- {
- NSAssert(NO, @"Invalid value for GCDAsyncSocketSSLDiffieHellmanParameters. Value must be of type NSData.");
-
- [self closeWithError:[self otherError:@"Invalid value for GCDAsyncSocketSSLDiffieHellmanParameters."]];
- return;
- }
- #endif
- // 10. kCFStreamSSLCertificates
- value = [tlsSettings objectForKey:GCDAsyncSocketSSLALPN];
- if ([value isKindOfClass:[NSArray class]])
- {
- if (@available(iOS 11.0, macOS 10.13, tvOS 11.0, *))
- {
- CFArrayRef protocols = (__bridge CFArrayRef)((NSArray *) value);
- status = SSLSetALPNProtocols(sslContext, protocols);
- if (status != noErr)
- {
- [self closeWithError:[self otherError:@"Error in SSLSetALPNProtocols"]];
- return;
- }
- }
- else
- {
- NSAssert(NO, @"Security option unavailable - GCDAsyncSocketSSLALPN"
- @" - iOS 11.0, macOS 10.13 required");
- [self closeWithError:[self otherError:@"Security option unavailable - GCDAsyncSocketSSLALPN"]];
- }
- }
- else if (value)
- {
- NSAssert(NO, @"Invalid value for GCDAsyncSocketSSLALPN. Value must be of type NSArray.");
-
- [self closeWithError:[self otherError:@"Invalid value for GCDAsyncSocketSSLALPN."]];
- return;
- }
-
- // DEPRECATED checks
-
- // 10. kCFStreamSSLAllowsAnyRoot
-
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Wdeprecated-declarations"
- value = [tlsSettings objectForKey:(__bridge NSString *)kCFStreamSSLAllowsAnyRoot];
- #pragma clang diagnostic pop
- if (value)
- {
- NSAssert(NO, @"Security option unavailable - kCFStreamSSLAllowsAnyRoot"
- @" - You must use manual trust evaluation");
-
- [self closeWithError:[self otherError:@"Security option unavailable - kCFStreamSSLAllowsAnyRoot"]];
- return;
- }
-
- // 11. kCFStreamSSLAllowsExpiredRoots
-
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Wdeprecated-declarations"
- value = [tlsSettings objectForKey:(__bridge NSString *)kCFStreamSSLAllowsExpiredRoots];
- #pragma clang diagnostic pop
- if (value)
- {
- NSAssert(NO, @"Security option unavailable - kCFStreamSSLAllowsExpiredRoots"
- @" - You must use manual trust evaluation");
-
- [self closeWithError:[self otherError:@"Security option unavailable - kCFStreamSSLAllowsExpiredRoots"]];
- return;
- }
-
- // 12. kCFStreamSSLValidatesCertificateChain
-
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Wdeprecated-declarations"
- value = [tlsSettings objectForKey:(__bridge NSString *)kCFStreamSSLValidatesCertificateChain];
- #pragma clang diagnostic pop
- if (value)
- {
- NSAssert(NO, @"Security option unavailable - kCFStreamSSLValidatesCertificateChain"
- @" - You must use manual trust evaluation");
-
- [self closeWithError:[self otherError:@"Security option unavailable - kCFStreamSSLValidatesCertificateChain"]];
- return;
- }
-
- // 13. kCFStreamSSLAllowsExpiredCertificates
-
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Wdeprecated-declarations"
- value = [tlsSettings objectForKey:(__bridge NSString *)kCFStreamSSLAllowsExpiredCertificates];
- #pragma clang diagnostic pop
- if (value)
- {
- NSAssert(NO, @"Security option unavailable - kCFStreamSSLAllowsExpiredCertificates"
- @" - You must use manual trust evaluation");
-
- [self closeWithError:[self otherError:@"Security option unavailable - kCFStreamSSLAllowsExpiredCertificates"]];
- return;
- }
-
- // 14. kCFStreamSSLLevel
-
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Wdeprecated-declarations"
- value = [tlsSettings objectForKey:(__bridge NSString *)kCFStreamSSLLevel];
- #pragma clang diagnostic pop
- if (value)
- {
- NSAssert(NO, @"Security option unavailable - kCFStreamSSLLevel"
- @" - You must use GCDAsyncSocketSSLProtocolVersionMin & GCDAsyncSocketSSLProtocolVersionMax");
-
- [self closeWithError:[self otherError:@"Security option unavailable - kCFStreamSSLLevel"]];
- return;
- }
-
- // Setup the sslPreBuffer
- //
- // Any data in the preBuffer needs to be moved into the sslPreBuffer,
- // as this data is now part of the secure read stream.
-
- sslPreBuffer = [[GCDAsyncSocketPreBuffer alloc] initWithCapacity:(1024 * 4)];
-
- size_t preBufferLength = [preBuffer availableBytes];
-
- if (preBufferLength > 0)
- {
- [sslPreBuffer ensureCapacityForWrite:preBufferLength];
-
- memcpy([sslPreBuffer writeBuffer], [preBuffer readBuffer], preBufferLength);
- [preBuffer didRead:preBufferLength];
- [sslPreBuffer didWrite:preBufferLength];
- }
-
- sslErrCode = lastSSLHandshakeError = noErr;
-
- // Start the SSL Handshake process
-
- [self ssl_continueSSLHandshake];
- }
- - (void)ssl_continueSSLHandshake
- {
- LogTrace();
-
- // If the return value is noErr, the session is ready for normal secure communication.
- // If the return value is errSSLWouldBlock, the SSLHandshake function must be called again.
- // If the return value is errSSLServerAuthCompleted, we ask delegate if we should trust the
- // server and then call SSLHandshake again to resume the handshake or close the connection
- // errSSLPeerBadCert SSL error.
- // Otherwise, the return value indicates an error code.
-
- OSStatus status = SSLHandshake(sslContext);
- lastSSLHandshakeError = status;
-
- if (status == noErr)
- {
- LogVerbose(@"SSLHandshake complete");
-
- flags &= ~kStartingReadTLS;
- flags &= ~kStartingWriteTLS;
-
- flags |= kSocketSecure;
-
- __strong id<GCDAsyncSocketDelegate> theDelegate = delegate;
- if (delegateQueue && [theDelegate respondsToSelector:@selector(socketDidSecure:)])
- {
- dispatch_async(delegateQueue, ^{ @autoreleasepool {
-
- [theDelegate socketDidSecure:self];
- }});
- }
-
- [self endCurrentRead];
- [self endCurrentWrite];
-
- [self maybeDequeueRead];
- [self maybeDequeueWrite];
- }
- else if (status == errSSLPeerAuthCompleted)
- {
- LogVerbose(@"SSLHandshake peerAuthCompleted - awaiting delegate approval");
-
- __block SecTrustRef trust = NULL;
- status = SSLCopyPeerTrust(sslContext, &trust);
- if (status != noErr)
- {
- [self closeWithError:[self sslError:status]];
- return;
- }
-
- int aStateIndex = stateIndex;
- dispatch_queue_t theSocketQueue = socketQueue;
-
- __weak GCDAsyncSocket *weakSelf = self;
-
- void (^comletionHandler)(BOOL) = ^(BOOL shouldTrust){ @autoreleasepool {
- #pragma clang diagnostic push
- #pragma clang diagnostic warning "-Wimplicit-retain-self"
-
- dispatch_async(theSocketQueue, ^{ @autoreleasepool {
-
- if (trust) {
- CFRelease(trust);
- trust = NULL;
- }
-
- __strong GCDAsyncSocket *strongSelf = weakSelf;
- if (strongSelf)
- {
- [strongSelf ssl_shouldTrustPeer:shouldTrust stateIndex:aStateIndex];
- }
- }});
-
- #pragma clang diagnostic pop
- }};
-
- __strong id<GCDAsyncSocketDelegate> theDelegate = delegate;
-
- if (delegateQueue && [theDelegate respondsToSelector:@selector(socket:didReceiveTrust:completionHandler:)])
- {
- dispatch_async(delegateQueue, ^{ @autoreleasepool {
-
- [theDelegate socket:self didReceiveTrust:trust completionHandler:comletionHandler];
- }});
- }
- else
- {
- if (trust) {
- CFRelease(trust);
- trust = NULL;
- }
-
- NSString *msg = @"GCDAsyncSocketManuallyEvaluateTrust specified in tlsSettings,"
- @" but delegate doesn't implement socket:shouldTrustPeer:";
-
- [self closeWithError:[self otherError:msg]];
- return;
- }
- }
- else if (status == errSSLWouldBlock)
- {
- LogVerbose(@"SSLHandshake continues...");
-
- // Handshake continues...
- //
- // This method will be called again from doReadData or doWriteData.
- }
- else
- {
- [self closeWithError:[self sslError:status]];
- }
- }
- - (void)ssl_shouldTrustPeer:(BOOL)shouldTrust stateIndex:(int)aStateIndex
- {
- LogTrace();
-
- if (aStateIndex != stateIndex)
- {
- LogInfo(@"Ignoring ssl_shouldTrustPeer - invalid state (maybe disconnected)");
-
- // One of the following is true
- // - the socket was disconnected
- // - the startTLS operation timed out
- // - the completionHandler was already invoked once
-
- return;
- }
-
- // Increment stateIndex to ensure completionHandler can only be called once.
- stateIndex++;
-
- if (shouldTrust)
- {
- NSAssert(lastSSLHandshakeError == errSSLPeerAuthCompleted, @"ssl_shouldTrustPeer called when last error is %d and not errSSLPeerAuthCompleted", (int)lastSSLHandshakeError);
- [self ssl_continueSSLHandshake];
- }
- else
- {
- [self closeWithError:[self sslError:errSSLPeerBadCert]];
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #pragma mark Security via CFStream
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #if TARGET_OS_IPHONE
- - (void)cf_finishSSLHandshake
- {
- LogTrace();
-
- if ((flags & kStartingReadTLS) && (flags & kStartingWriteTLS))
- {
- flags &= ~kStartingReadTLS;
- flags &= ~kStartingWriteTLS;
-
- flags |= kSocketSecure;
-
- __strong id<GCDAsyncSocketDelegate> theDelegate = delegate;
- if (delegateQueue && [theDelegate respondsToSelector:@selector(socketDidSecure:)])
- {
- dispatch_async(delegateQueue, ^{ @autoreleasepool {
-
- [theDelegate socketDidSecure:self];
- }});
- }
-
- [self endCurrentRead];
- [self endCurrentWrite];
-
- [self maybeDequeueRead];
- [self maybeDequeueWrite];
- }
- }
- - (void)cf_abortSSLHandshake:(NSError *)error
- {
- LogTrace();
-
- if ((flags & kStartingReadTLS) && (flags & kStartingWriteTLS))
- {
- flags &= ~kStartingReadTLS;
- flags &= ~kStartingWriteTLS;
-
- [self closeWithError:error];
- }
- }
- - (void)cf_startTLS
- {
- LogTrace();
-
- LogVerbose(@"Starting TLS (via CFStream)...");
-
- if ([preBuffer availableBytes] > 0)
- {
- NSString *msg = @"Invalid TLS transition. Handshake has already been read from socket.";
-
- [self closeWithError:[self otherError:msg]];
- return;
- }
-
- [self suspendReadSource];
- [self suspendWriteSource];
-
- socketFDBytesAvailable = 0;
- flags &= ~kSocketCanAcceptBytes;
- flags &= ~kSecureSocketHasBytesAvailable;
-
- flags |= kUsingCFStreamForTLS;
-
- if (![self createReadAndWriteStream])
- {
- [self closeWithError:[self otherError:@"Error in CFStreamCreatePairWithSocket"]];
- return;
- }
-
- if (![self registerForStreamCallbacksIncludingReadWrite:YES])
- {
- [self closeWithError:[self otherError:@"Error in CFStreamSetClient"]];
- return;
- }
-
- if (![self addStreamsToRunLoop])
- {
- [self closeWithError:[self otherError:@"Error in CFStreamScheduleWithRunLoop"]];
- return;
- }
-
- NSAssert([currentRead isKindOfClass:[GCDAsyncSpecialPacket class]], @"Invalid read packet for startTLS");
- NSAssert([currentWrite isKindOfClass:[GCDAsyncSpecialPacket class]], @"Invalid write packet for startTLS");
-
- GCDAsyncSpecialPacket *tlsPacket = (GCDAsyncSpecialPacket *)currentRead;
- CFDictionaryRef tlsSettings = (__bridge CFDictionaryRef)tlsPacket->tlsSettings;
-
- // Getting an error concerning kCFStreamPropertySSLSettings ?
- // You need to add the CFNetwork framework to your iOS application.
-
- BOOL r1 = CFReadStreamSetProperty(readStream, kCFStreamPropertySSLSettings, tlsSettings);
- BOOL r2 = CFWriteStreamSetProperty(writeStream, kCFStreamPropertySSLSettings, tlsSettings);
-
- // For some reason, starting around the time of iOS 4.3,
- // the first call to set the kCFStreamPropertySSLSettings will return true,
- // but the second will return false.
- //
- // Order doesn't seem to matter.
- // So you could call CFReadStreamSetProperty and then CFWriteStreamSetProperty, or you could reverse the order.
- // Either way, the first call will return true, and the second returns false.
- //
- // Interestingly, this doesn't seem to affect anything.
- // Which is not altogether unusual, as the documentation seems to suggest that (for many settings)
- // setting it on one side of the stream automatically sets it for the other side of the stream.
- //
- // Although there isn't anything in the documentation to suggest that the second attempt would fail.
- //
- // Furthermore, this only seems to affect streams that are negotiating a security upgrade.
- // In other words, the socket gets connected, there is some back-and-forth communication over the unsecure
- // connection, and then a startTLS is issued.
- // So this mostly affects newer protocols (XMPP, IMAP) as opposed to older protocols (HTTPS).
-
- if (!r1 && !r2) // Yes, the && is correct - workaround for apple bug.
- {
- [self closeWithError:[self otherError:@"Error in CFStreamSetProperty"]];
- return;
- }
-
- if (![self openStreams])
- {
- [self closeWithError:[self otherError:@"Error in CFStreamOpen"]];
- return;
- }
-
- LogVerbose(@"Waiting for SSL Handshake to complete...");
- }
- #endif
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #pragma mark CFStream
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #if TARGET_OS_IPHONE
- + (void)ignore:(id)_
- {}
- + (void)startCFStreamThreadIfNeeded
- {
- LogTrace();
-
- static dispatch_once_t predicate;
- dispatch_once(&predicate, ^{
-
- cfstreamThreadRetainCount = 0;
- cfstreamThreadSetupQueue = dispatch_queue_create("GCDAsyncSocket-CFStreamThreadSetup", DISPATCH_QUEUE_SERIAL);
- });
-
- dispatch_sync(cfstreamThreadSetupQueue, ^{ @autoreleasepool {
-
- if (++cfstreamThreadRetainCount == 1)
- {
- cfstreamThread = [[NSThread alloc] initWithTarget:self
- selector:@selector(cfstreamThread:)
- object:nil];
- [cfstreamThread start];
- }
- }});
- }
- + (void)stopCFStreamThreadIfNeeded
- {
- LogTrace();
-
- // The creation of the cfstreamThread is relatively expensive.
- // So we'd like to keep it available for recycling.
- // However, there's a tradeoff here, because it shouldn't remain alive forever.
- // So what we're going to do is use a little delay before taking it down.
- // This way it can be reused properly in situations where multiple sockets are continually in flux.
-
- int delayInSeconds = 30;
- dispatch_time_t when = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
- dispatch_after(when, cfstreamThreadSetupQueue, ^{ @autoreleasepool {
- #pragma clang diagnostic push
- #pragma clang diagnostic warning "-Wimplicit-retain-self"
-
- if (cfstreamThreadRetainCount == 0)
- {
- LogWarn(@"Logic error concerning cfstreamThread start / stop");
- return_from_block;
- }
-
- if (--cfstreamThreadRetainCount == 0)
- {
- [cfstreamThread cancel]; // set isCancelled flag
-
- // wake up the thread
- [[self class] performSelector:@selector(ignore:)
- onThread:cfstreamThread
- withObject:[NSNull null]
- waitUntilDone:NO];
-
- cfstreamThread = nil;
- }
-
- #pragma clang diagnostic pop
- }});
- }
- + (void)cfstreamThread:(id)unused { @autoreleasepool
- {
- [[NSThread currentThread] setName:GCDAsyncSocketThreadName];
-
- LogInfo(@"CFStreamThread: Started");
-
- // We can't run the run loop unless it has an associated input source or a timer.
- // So we'll just create a timer that will never fire - unless the server runs for decades.
- [NSTimer scheduledTimerWithTimeInterval:[[NSDate distantFuture] timeIntervalSinceNow]
- target:self
- selector:@selector(ignore:)
- userInfo:nil
- repeats:YES];
-
- NSThread *currentThread = [NSThread currentThread];
- NSRunLoop *currentRunLoop = [NSRunLoop currentRunLoop];
-
- BOOL isCancelled = [currentThread isCancelled];
-
- while (!isCancelled && [currentRunLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]])
- {
- isCancelled = [currentThread isCancelled];
- }
-
- LogInfo(@"CFStreamThread: Stopped");
- }}
- + (void)scheduleCFStreams:(GCDAsyncSocket *)asyncSocket
- {
- LogTrace();
- NSAssert([NSThread currentThread] == cfstreamThread, @"Invoked on wrong thread");
-
- CFRunLoopRef runLoop = CFRunLoopGetCurrent();
-
- if (asyncSocket->readStream)
- CFReadStreamScheduleWithRunLoop(asyncSocket->readStream, runLoop, kCFRunLoopDefaultMode);
-
- if (asyncSocket->writeStream)
- CFWriteStreamScheduleWithRunLoop(asyncSocket->writeStream, runLoop, kCFRunLoopDefaultMode);
- }
- + (void)unscheduleCFStreams:(GCDAsyncSocket *)asyncSocket
- {
- LogTrace();
- NSAssert([NSThread currentThread] == cfstreamThread, @"Invoked on wrong thread");
-
- CFRunLoopRef runLoop = CFRunLoopGetCurrent();
-
- if (asyncSocket->readStream)
- CFReadStreamUnscheduleFromRunLoop(asyncSocket->readStream, runLoop, kCFRunLoopDefaultMode);
-
- if (asyncSocket->writeStream)
- CFWriteStreamUnscheduleFromRunLoop(asyncSocket->writeStream, runLoop, kCFRunLoopDefaultMode);
- }
- static void CFReadStreamCallback (CFReadStreamRef stream, CFStreamEventType type, void *pInfo)
- {
- GCDAsyncSocket *asyncSocket = (__bridge GCDAsyncSocket *)pInfo;
-
- switch(type)
- {
- case kCFStreamEventHasBytesAvailable:
- {
- dispatch_async(asyncSocket->socketQueue, ^{ @autoreleasepool {
-
- LogCVerbose(@"CFReadStreamCallback - HasBytesAvailable");
-
- if (asyncSocket->readStream != stream)
- return_from_block;
-
- if ((asyncSocket->flags & kStartingReadTLS) && (asyncSocket->flags & kStartingWriteTLS))
- {
- // If we set kCFStreamPropertySSLSettings before we opened the streams, this might be a lie.
- // (A callback related to the tcp stream, but not to the SSL layer).
-
- if (CFReadStreamHasBytesAvailable(asyncSocket->readStream))
- {
- asyncSocket->flags |= kSecureSocketHasBytesAvailable;
- [asyncSocket cf_finishSSLHandshake];
- }
- }
- else
- {
- asyncSocket->flags |= kSecureSocketHasBytesAvailable;
- [asyncSocket doReadData];
- }
- }});
-
- break;
- }
- default:
- {
- NSError *error = (__bridge_transfer NSError *)CFReadStreamCopyError(stream);
-
- if (error == nil && type == kCFStreamEventEndEncountered)
- {
- error = [asyncSocket connectionClosedError];
- }
-
- dispatch_async(asyncSocket->socketQueue, ^{ @autoreleasepool {
-
- LogCVerbose(@"CFReadStreamCallback - Other");
-
- if (asyncSocket->readStream != stream)
- return_from_block;
-
- if ((asyncSocket->flags & kStartingReadTLS) && (asyncSocket->flags & kStartingWriteTLS))
- {
- [asyncSocket cf_abortSSLHandshake:error];
- }
- else
- {
- [asyncSocket closeWithError:error];
- }
- }});
-
- break;
- }
- }
-
- }
- static void CFWriteStreamCallback (CFWriteStreamRef stream, CFStreamEventType type, void *pInfo)
- {
- GCDAsyncSocket *asyncSocket = (__bridge GCDAsyncSocket *)pInfo;
-
- switch(type)
- {
- case kCFStreamEventCanAcceptBytes:
- {
- dispatch_async(asyncSocket->socketQueue, ^{ @autoreleasepool {
-
- LogCVerbose(@"CFWriteStreamCallback - CanAcceptBytes");
-
- if (asyncSocket->writeStream != stream)
- return_from_block;
-
- if ((asyncSocket->flags & kStartingReadTLS) && (asyncSocket->flags & kStartingWriteTLS))
- {
- // If we set kCFStreamPropertySSLSettings before we opened the streams, this might be a lie.
- // (A callback related to the tcp stream, but not to the SSL layer).
-
- if (CFWriteStreamCanAcceptBytes(asyncSocket->writeStream))
- {
- asyncSocket->flags |= kSocketCanAcceptBytes;
- [asyncSocket cf_finishSSLHandshake];
- }
- }
- else
- {
- asyncSocket->flags |= kSocketCanAcceptBytes;
- [asyncSocket doWriteData];
- }
- }});
-
- break;
- }
- default:
- {
- NSError *error = (__bridge_transfer NSError *)CFWriteStreamCopyError(stream);
-
- if (error == nil && type == kCFStreamEventEndEncountered)
- {
- error = [asyncSocket connectionClosedError];
- }
-
- dispatch_async(asyncSocket->socketQueue, ^{ @autoreleasepool {
-
- LogCVerbose(@"CFWriteStreamCallback - Other");
-
- if (asyncSocket->writeStream != stream)
- return_from_block;
-
- if ((asyncSocket->flags & kStartingReadTLS) && (asyncSocket->flags & kStartingWriteTLS))
- {
- [asyncSocket cf_abortSSLHandshake:error];
- }
- else
- {
- [asyncSocket closeWithError:error];
- }
- }});
-
- break;
- }
- }
-
- }
- - (BOOL)createReadAndWriteStream
- {
- LogTrace();
-
- NSAssert(dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey), @"Must be dispatched on socketQueue");
-
-
- if (readStream || writeStream)
- {
- // Streams already created
- return YES;
- }
-
- int socketFD = (socket4FD != SOCKET_NULL) ? socket4FD : (socket6FD != SOCKET_NULL) ? socket6FD : socketUN;
-
- if (socketFD == SOCKET_NULL)
- {
- // Cannot create streams without a file descriptor
- return NO;
- }
-
- if (![self isConnected])
- {
- // Cannot create streams until file descriptor is connected
- return NO;
- }
-
- LogVerbose(@"Creating read and write stream...");
-
- CFStreamCreatePairWithSocket(NULL, (CFSocketNativeHandle)socketFD, &readStream, &writeStream);
-
- // The kCFStreamPropertyShouldCloseNativeSocket property should be false by default (for our case).
- // But let's not take any chances.
-
- if (readStream)
- CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanFalse);
- if (writeStream)
- CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanFalse);
-
- if ((readStream == NULL) || (writeStream == NULL))
- {
- LogWarn(@"Unable to create read and write stream...");
-
- if (readStream)
- {
- CFReadStreamClose(readStream);
- CFRelease(readStream);
- readStream = NULL;
- }
- if (writeStream)
- {
- CFWriteStreamClose(writeStream);
- CFRelease(writeStream);
- writeStream = NULL;
- }
-
- return NO;
- }
-
- return YES;
- }
- - (BOOL)registerForStreamCallbacksIncludingReadWrite:(BOOL)includeReadWrite
- {
- LogVerbose(@"%@ %@", THIS_METHOD, (includeReadWrite ? @"YES" : @"NO"));
-
- NSAssert(dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey), @"Must be dispatched on socketQueue");
- NSAssert((readStream != NULL && writeStream != NULL), @"Read/Write stream is null");
-
- streamContext.version = 0;
- streamContext.info = (__bridge void *)(self);
- streamContext.retain = nil;
- streamContext.release = nil;
- streamContext.copyDescription = nil;
-
- CFOptionFlags readStreamEvents = kCFStreamEventErrorOccurred | kCFStreamEventEndEncountered;
- if (includeReadWrite)
- readStreamEvents |= kCFStreamEventHasBytesAvailable;
-
- if (!CFReadStreamSetClient(readStream, readStreamEvents, &CFReadStreamCallback, &streamContext))
- {
- return NO;
- }
-
- CFOptionFlags writeStreamEvents = kCFStreamEventErrorOccurred | kCFStreamEventEndEncountered;
- if (includeReadWrite)
- writeStreamEvents |= kCFStreamEventCanAcceptBytes;
-
- if (!CFWriteStreamSetClient(writeStream, writeStreamEvents, &CFWriteStreamCallback, &streamContext))
- {
- return NO;
- }
-
- return YES;
- }
- - (BOOL)addStreamsToRunLoop
- {
- LogTrace();
-
- NSAssert(dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey), @"Must be dispatched on socketQueue");
- NSAssert((readStream != NULL && writeStream != NULL), @"Read/Write stream is null");
-
- if (!(flags & kAddedStreamsToRunLoop))
- {
- LogVerbose(@"Adding streams to runloop...");
-
- [[self class] startCFStreamThreadIfNeeded];
- dispatch_sync(cfstreamThreadSetupQueue, ^{
- [[self class] performSelector:@selector(scheduleCFStreams:)
- onThread:cfstreamThread
- withObject:self
- waitUntilDone:YES];
- });
- flags |= kAddedStreamsToRunLoop;
- }
-
- return YES;
- }
- - (void)removeStreamsFromRunLoop
- {
- LogTrace();
-
- NSAssert(dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey), @"Must be dispatched on socketQueue");
- NSAssert((readStream != NULL && writeStream != NULL), @"Read/Write stream is null");
-
- if (flags & kAddedStreamsToRunLoop)
- {
- LogVerbose(@"Removing streams from runloop...");
-
- dispatch_sync(cfstreamThreadSetupQueue, ^{
- [[self class] performSelector:@selector(unscheduleCFStreams:)
- onThread:cfstreamThread
- withObject:self
- waitUntilDone:YES];
- });
- [[self class] stopCFStreamThreadIfNeeded];
-
- flags &= ~kAddedStreamsToRunLoop;
- }
- }
- - (BOOL)openStreams
- {
- LogTrace();
-
- NSAssert(dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey), @"Must be dispatched on socketQueue");
- NSAssert((readStream != NULL && writeStream != NULL), @"Read/Write stream is null");
-
- CFStreamStatus readStatus = CFReadStreamGetStatus(readStream);
- CFStreamStatus writeStatus = CFWriteStreamGetStatus(writeStream);
-
- if ((readStatus == kCFStreamStatusNotOpen) || (writeStatus == kCFStreamStatusNotOpen))
- {
- LogVerbose(@"Opening read and write stream...");
-
- BOOL r1 = CFReadStreamOpen(readStream);
- BOOL r2 = CFWriteStreamOpen(writeStream);
-
- if (!r1 || !r2)
- {
- LogError(@"Error in CFStreamOpen");
- return NO;
- }
- }
-
- return YES;
- }
- #endif
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #pragma mark Advanced
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /**
- * See header file for big discussion of this method.
- **/
- - (BOOL)autoDisconnectOnClosedReadStream
- {
- // Note: YES means kAllowHalfDuplexConnection is OFF
-
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- {
- return ((config & kAllowHalfDuplexConnection) == 0);
- }
- else
- {
- __block BOOL result;
-
- dispatch_sync(socketQueue, ^{
- result = ((self->config & kAllowHalfDuplexConnection) == 0);
- });
-
- return result;
- }
- }
- /**
- * See header file for big discussion of this method.
- **/
- - (void)setAutoDisconnectOnClosedReadStream:(BOOL)flag
- {
- // Note: YES means kAllowHalfDuplexConnection is OFF
-
- dispatch_block_t block = ^{
-
- if (flag)
- self->config &= ~kAllowHalfDuplexConnection;
- else
- self->config |= kAllowHalfDuplexConnection;
- };
-
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- block();
- else
- dispatch_async(socketQueue, block);
- }
- /**
- * See header file for big discussion of this method.
- **/
- - (void)markSocketQueueTargetQueue:(dispatch_queue_t)socketNewTargetQueue
- {
- void *nonNullUnusedPointer = (__bridge void *)self;
- dispatch_queue_set_specific(socketNewTargetQueue, IsOnSocketQueueOrTargetQueueKey, nonNullUnusedPointer, NULL);
- }
- /**
- * See header file for big discussion of this method.
- **/
- - (void)unmarkSocketQueueTargetQueue:(dispatch_queue_t)socketOldTargetQueue
- {
- dispatch_queue_set_specific(socketOldTargetQueue, IsOnSocketQueueOrTargetQueueKey, NULL, NULL);
- }
- /**
- * See header file for big discussion of this method.
- **/
- - (void)performBlock:(dispatch_block_t)block
- {
- if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- block();
- else
- dispatch_sync(socketQueue, block);
- }
- /**
- * Questions? Have you read the header file?
- **/
- - (int)socketFD
- {
- if (!dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- {
- LogWarn(@"%@ - Method only available from within the context of a performBlock: invocation", THIS_METHOD);
- return SOCKET_NULL;
- }
-
- if (socket4FD != SOCKET_NULL)
- return socket4FD;
- else
- return socket6FD;
- }
- /**
- * Questions? Have you read the header file?
- **/
- - (int)socket4FD
- {
- if (!dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- {
- LogWarn(@"%@ - Method only available from within the context of a performBlock: invocation", THIS_METHOD);
- return SOCKET_NULL;
- }
-
- return socket4FD;
- }
- /**
- * Questions? Have you read the header file?
- **/
- - (int)socket6FD
- {
- if (!dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- {
- LogWarn(@"%@ - Method only available from within the context of a performBlock: invocation", THIS_METHOD);
- return SOCKET_NULL;
- }
-
- return socket6FD;
- }
- #if TARGET_OS_IPHONE
- /**
- * Questions? Have you read the header file?
- **/
- - (CFReadStreamRef)readStream
- {
- if (!dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- {
- LogWarn(@"%@ - Method only available from within the context of a performBlock: invocation", THIS_METHOD);
- return NULL;
- }
-
- if (readStream == NULL)
- [self createReadAndWriteStream];
-
- return readStream;
- }
- /**
- * Questions? Have you read the header file?
- **/
- - (CFWriteStreamRef)writeStream
- {
- if (!dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- {
- LogWarn(@"%@ - Method only available from within the context of a performBlock: invocation", THIS_METHOD);
- return NULL;
- }
-
- if (writeStream == NULL)
- [self createReadAndWriteStream];
-
- return writeStream;
- }
- - (BOOL)enableBackgroundingOnSocketWithCaveat:(BOOL)caveat
- {
- if (![self createReadAndWriteStream])
- {
- // Error occurred creating streams (perhaps socket isn't open)
- return NO;
- }
-
- BOOL r1, r2;
-
- LogVerbose(@"Enabling backgrouding on socket");
-
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Wdeprecated-declarations"
- r1 = CFReadStreamSetProperty(readStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);
- r2 = CFWriteStreamSetProperty(writeStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);
- #pragma clang diagnostic pop
- if (!r1 || !r2)
- {
- return NO;
- }
-
- if (!caveat)
- {
- if (![self openStreams])
- {
- return NO;
- }
- }
-
- return YES;
- }
- /**
- * Questions? Have you read the header file?
- **/
- - (BOOL)enableBackgroundingOnSocket
- {
- LogTrace();
-
- if (!dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- {
- LogWarn(@"%@ - Method only available from within the context of a performBlock: invocation", THIS_METHOD);
- return NO;
- }
-
- return [self enableBackgroundingOnSocketWithCaveat:NO];
- }
- - (BOOL)enableBackgroundingOnSocketWithCaveat // Deprecated in iOS 4.???
- {
- // This method was created as a workaround for a bug in iOS.
- // Apple has since fixed this bug.
- // I'm not entirely sure which version of iOS they fixed it in...
-
- LogTrace();
-
- if (!dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- {
- LogWarn(@"%@ - Method only available from within the context of a performBlock: invocation", THIS_METHOD);
- return NO;
- }
-
- return [self enableBackgroundingOnSocketWithCaveat:YES];
- }
- #endif
- - (SSLContextRef)sslContext
- {
- if (!dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
- {
- LogWarn(@"%@ - Method only available from within the context of a performBlock: invocation", THIS_METHOD);
- return NULL;
- }
-
- return sslContext;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #pragma mark Class Utilities
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- + (NSMutableArray *)lookupHost:(NSString *)host port:(uint16_t)port error:(NSError **)errPtr
- {
- LogTrace();
-
- NSMutableArray *addresses = nil;
- NSError *error = nil;
-
- if ([host isEqualToString:@"localhost"] || [host isEqualToString:@"loopback"])
- {
- // Use LOOPBACK address
- struct sockaddr_in nativeAddr4;
- nativeAddr4.sin_len = sizeof(struct sockaddr_in);
- nativeAddr4.sin_family = AF_INET;
- nativeAddr4.sin_port = htons(port);
- nativeAddr4.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
- memset(&(nativeAddr4.sin_zero), 0, sizeof(nativeAddr4.sin_zero));
-
- struct sockaddr_in6 nativeAddr6;
- nativeAddr6.sin6_len = sizeof(struct sockaddr_in6);
- nativeAddr6.sin6_family = AF_INET6;
- nativeAddr6.sin6_port = htons(port);
- nativeAddr6.sin6_flowinfo = 0;
- nativeAddr6.sin6_addr = in6addr_loopback;
- nativeAddr6.sin6_scope_id = 0;
-
- // Wrap the native address structures
-
- NSData *address4 = [NSData dataWithBytes:&nativeAddr4 length:sizeof(nativeAddr4)];
- NSData *address6 = [NSData dataWithBytes:&nativeAddr6 length:sizeof(nativeAddr6)];
-
- addresses = [NSMutableArray arrayWithCapacity:2];
- [addresses addObject:address4];
- [addresses addObject:address6];
- }
- else
- {
- NSString *portStr = [NSString stringWithFormat:@"%hu", port];
-
- struct addrinfo hints, *res, *res0;
-
- memset(&hints, 0, sizeof(hints));
- hints.ai_family = PF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
-
- int gai_error = getaddrinfo([host UTF8String], [portStr UTF8String], &hints, &res0);
-
- if (gai_error)
- {
- error = [self gaiError:gai_error];
- }
- else
- {
- NSUInteger capacity = 0;
- for (res = res0; res; res = res->ai_next)
- {
- if (res->ai_family == AF_INET || res->ai_family == AF_INET6) {
- capacity++;
- }
- }
-
- addresses = [NSMutableArray arrayWithCapacity:capacity];
-
- for (res = res0; res; res = res->ai_next)
- {
- if (res->ai_family == AF_INET)
- {
- // Found IPv4 address.
- // Wrap the native address structure, and add to results.
-
- NSData *address4 = [NSData dataWithBytes:res->ai_addr length:res->ai_addrlen];
- [addresses addObject:address4];
- }
- else if (res->ai_family == AF_INET6)
- {
- // Fixes connection issues with IPv6
- // https://github.com/robbiehanson/CocoaAsyncSocket/issues/429#issuecomment-222477158
-
- // Found IPv6 address.
- // Wrap the native address structure, and add to results.
-
- struct sockaddr_in6 *sockaddr = (struct sockaddr_in6 *)(void *)res->ai_addr;
- in_port_t *portPtr = &sockaddr->sin6_port;
- if ((portPtr != NULL) && (*portPtr == 0)) {
- *portPtr = htons(port);
- }
- NSData *address6 = [NSData dataWithBytes:res->ai_addr length:res->ai_addrlen];
- [addresses addObject:address6];
- }
- }
- freeaddrinfo(res0);
-
- if ([addresses count] == 0)
- {
- error = [self gaiError:EAI_FAIL];
- }
- }
- }
-
- if (errPtr) *errPtr = error;
- return addresses;
- }
- + (NSString *)hostFromSockaddr4:(const struct sockaddr_in *)pSockaddr4
- {
- char addrBuf[INET_ADDRSTRLEN];
-
- if (inet_ntop(AF_INET, &pSockaddr4->sin_addr, addrBuf, (socklen_t)sizeof(addrBuf)) == NULL)
- {
- addrBuf[0] = '\0';
- }
-
- return [NSString stringWithCString:addrBuf encoding:NSASCIIStringEncoding];
- }
- + (NSString *)hostFromSockaddr6:(const struct sockaddr_in6 *)pSockaddr6
- {
- char addrBuf[INET6_ADDRSTRLEN];
-
- if (inet_ntop(AF_INET6, &pSockaddr6->sin6_addr, addrBuf, (socklen_t)sizeof(addrBuf)) == NULL)
- {
- addrBuf[0] = '\0';
- }
-
- return [NSString stringWithCString:addrBuf encoding:NSASCIIStringEncoding];
- }
- + (uint16_t)portFromSockaddr4:(const struct sockaddr_in *)pSockaddr4
- {
- return ntohs(pSockaddr4->sin_port);
- }
- + (uint16_t)portFromSockaddr6:(const struct sockaddr_in6 *)pSockaddr6
- {
- return ntohs(pSockaddr6->sin6_port);
- }
- + (NSURL *)urlFromSockaddrUN:(const struct sockaddr_un *)pSockaddr
- {
- NSString *path = [NSString stringWithUTF8String:pSockaddr->sun_path];
- return [NSURL fileURLWithPath:path];
- }
- + (NSString *)hostFromAddress:(NSData *)address
- {
- NSString *host;
-
- if ([self getHost:&host port:NULL fromAddress:address])
- return host;
- else
- return nil;
- }
- + (uint16_t)portFromAddress:(NSData *)address
- {
- uint16_t port;
-
- if ([self getHost:NULL port:&port fromAddress:address])
- return port;
- else
- return 0;
- }
- + (BOOL)isIPv4Address:(NSData *)address
- {
- if ([address length] >= sizeof(struct sockaddr))
- {
- const struct sockaddr *sockaddrX = [address bytes];
-
- if (sockaddrX->sa_family == AF_INET) {
- return YES;
- }
- }
-
- return NO;
- }
- + (BOOL)isIPv6Address:(NSData *)address
- {
- if ([address length] >= sizeof(struct sockaddr))
- {
- const struct sockaddr *sockaddrX = [address bytes];
-
- if (sockaddrX->sa_family == AF_INET6) {
- return YES;
- }
- }
-
- return NO;
- }
- + (BOOL)getHost:(NSString **)hostPtr port:(uint16_t *)portPtr fromAddress:(NSData *)address
- {
- return [self getHost:hostPtr port:portPtr family:NULL fromAddress:address];
- }
- + (BOOL)getHost:(NSString **)hostPtr port:(uint16_t *)portPtr family:(sa_family_t *)afPtr fromAddress:(NSData *)address
- {
- if ([address length] >= sizeof(struct sockaddr))
- {
- const struct sockaddr *sockaddrX = [address bytes];
-
- if (sockaddrX->sa_family == AF_INET)
- {
- if ([address length] >= sizeof(struct sockaddr_in))
- {
- struct sockaddr_in sockaddr4;
- memcpy(&sockaddr4, sockaddrX, sizeof(sockaddr4));
-
- if (hostPtr) *hostPtr = [self hostFromSockaddr4:&sockaddr4];
- if (portPtr) *portPtr = [self portFromSockaddr4:&sockaddr4];
- if (afPtr) *afPtr = AF_INET;
-
- return YES;
- }
- }
- else if (sockaddrX->sa_family == AF_INET6)
- {
- if ([address length] >= sizeof(struct sockaddr_in6))
- {
- struct sockaddr_in6 sockaddr6;
- memcpy(&sockaddr6, sockaddrX, sizeof(sockaddr6));
-
- if (hostPtr) *hostPtr = [self hostFromSockaddr6:&sockaddr6];
- if (portPtr) *portPtr = [self portFromSockaddr6:&sockaddr6];
- if (afPtr) *afPtr = AF_INET6;
-
- return YES;
- }
- }
- }
-
- return NO;
- }
- + (NSData *)CRLFData
- {
- return [NSData dataWithBytes:"\x0D\x0A" length:2];
- }
- + (NSData *)CRData
- {
- return [NSData dataWithBytes:"\x0D" length:1];
- }
- + (NSData *)LFData
- {
- return [NSData dataWithBytes:"\x0A" length:1];
- }
- + (NSData *)ZeroData
- {
- return [NSData dataWithBytes:"" length:1];
- }
- @end
|