123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605 |
- {==============================================================================|
- | Project : Ararat Synapse | 009.010.002 |
- |==============================================================================|
- | Content: Library base |
- |==============================================================================|
- | Copyright (c)1999-2021, Lukas Gebauer |
- | All rights reserved. |
- | |
- | Redistribution and use in source and binary forms, with or without |
- | modification, are permitted provided that the following conditions are met: |
- | |
- | Redistributions of source code must retain the above copyright notice, this |
- | list of conditions and the following disclaimer. |
- | |
- | Redistributions in binary form must reproduce the above copyright notice, |
- | this list of conditions and the following disclaimer in the documentation |
- | and/or other materials provided with the distribution. |
- | |
- | Neither the name of Lukas Gebauer nor the names of its contributors may |
- | be used to endorse or promote products derived from this software without |
- | specific prior written permission. |
- | |
- | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
- | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
- | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
- | ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR |
- | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
- | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
- | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
- | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
- | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
- | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
- | DAMAGE. |
- |==============================================================================|
- | The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
- | Portions created by Lukas Gebauer are Copyright (c)1999-2021. |
- | All Rights Reserved. |
- |==============================================================================|
- | Contributor(s): |
- |==============================================================================|
- | History: see HISTORY.HTM from distribution package |
- | (Found at URL: http://www.ararat.cz/synapse/) |
- |==============================================================================}
- {
- Special thanks to Gregor Ibic <[email protected]>
- (Intelicom d.o.o., http://www.intelicom.si)
- for good inspiration about SSL programming.
- }
- {$DEFINE ONCEWINSOCK}
- {Note about define ONCEWINSOCK:
- If you remove this compiler directive, then socket interface is loaded and
- initialized on constructor of TBlockSocket class for each socket separately.
- Socket interface is used only if your need it.
- If you leave this directive here, then socket interface is loaded and
- initialized only once at start of your program! It boost performace on high
- count of created and destroyed sockets. It eliminate possible small resource
- leak on Windows systems too.
- }
- {$DEFINE RAISEEXCEPT}
- {When you enable this define, then is Raiseexcept property is on by default
- }
- {:@abstract(Synapse's library core)
- Core with implementation basic socket classes.
- }
- {$IFDEF FPC}
- {$MODE DELPHI}
- {$ENDIF}
- {$IFDEF VER125}
- {$DEFINE BCB}
- {$ENDIF}
- {$IFDEF BCB}
- {$ObjExportAll On}
- {$ENDIF}
- {$Q-}
- {$H+}
- {$M+}
- {$TYPEDADDRESS OFF}
- //old Delphi does not have MSWINDOWS define.
- {$IFDEF WIN32}
- {$IFNDEF MSWINDOWS}
- {$DEFINE MSWINDOWS}
- {$ENDIF}
- {$ENDIF}
- {$IFDEF UNICODE}
- {$WARN IMPLICIT_STRING_CAST OFF}
- {$WARN IMPLICIT_STRING_CAST_LOSS OFF}
- {$ENDIF}
- unit blcksock;
- interface
- uses
- SysUtils, Classes,
- synafpc, synabyte,
- synsock, synautil, synacode, synaip
- {$IFDEF NEXTGEN}
- , System.Generics.Collections,
- System.Generics.Defaults
- {$ENDIF}
- {$IFDEF CIL}
- ,System.Net
- ,System.Net.Sockets
- ,System.Text
- {$ENDIF}
- ;
- const
- SynapseRelease = '40';
- cLocalhost = '127.0.0.1';
- cAnyHost = '0.0.0.0';
- cBroadcast = '255.255.255.255';
- c6Localhost = '::1';
- c6AnyHost = '::0';
- c6Broadcast = 'ffff::1';
- cAnyPort = '0';
- CR = #$0d;
- LF = #$0a;
- CRLF = CR + LF;
- c64k = 65536;
- type
- {:@abstract(Exception clas used by Synapse)
- When you enable generating of exceptions, this exception is raised by
- Synapse's units.}
- ESocketBindError = class(Exception);
- { ESynapseError }
- ESynapseError = class(Exception)
- private
- FErrorCode: Integer;
- FErrorMessage: string;
- public
- constructor CreateErrorCode(AErrorCode:Integer; const AErrorDesc: string);
- published
- {:Code of error. Value depending on used operating system}
- property ErrorCode: Integer read FErrorCode Write FErrorCode;
- {:Human readable description of error.}
- property ErrorMessage: string read FErrorMessage Write FErrorMessage;
- end;
- ESynProtocolError = class(ESynapseError);
- EResetByPeer = class (ESynapseError);
- ECouldNotBindSocket = class (ESynapseError);
- EConnectionResetByPeer = class (ESynapseError);
- ESockectIsnotConnected = class (ESynapseError);
- EConnectionTimedOut = class (ESynapseError);
- EConnectionRefused = class (ESynapseError);
- ECantAssignAddress = class (ESynapseError);
- ESocketMinus2 = class (ESynapseError);
- {:Types of OnStatus events}
- THookSocketReason = (
- {:Resolving is begin. Resolved IP and port is in parameter in format like:
- 'localhost.somewhere.com:25'.}
- HR_ResolvingBegin,
- {:Resolving is done. Resolved IP and port is in parameter in format like:
- 'localhost.somewhere.com:25'. It is always same as in HR_ResolvingBegin!}
- HR_ResolvingEnd,
- {:Socket created by CreateSocket method. It reporting Family of created
- socket too!}
- HR_SocketCreate,
- {:Socket closed by CloseSocket method.}
- HR_SocketClose,
- {:Socket binded to IP and Port. Binded IP and Port is in parameter in format
- like: 'localhost.somewhere.com:25'.}
- HR_Bind,
- {:Socket connected to IP and Port. Connected IP and Port is in parameter in
- format like: 'localhost.somewhere.com:25'.}
- HR_Connect,
- {:Called when CanRead method is used with @True result.}
- HR_CanRead,
- {:Called when CanWrite method is used with @True result.}
- HR_CanWrite,
- {:Socket is swithed to Listen mode. (TCP socket only)}
- HR_Listen,
- {:Socket Accepting client connection. (TCP socket only)}
- HR_Accept,
- {:report count of bytes readed from socket. Number is in parameter string.
- If you need is in integer, you must use StrToInt function!}
- HR_ReadCount,
- {:report count of bytes writed to socket. Number is in parameter string. If
- you need is in integer, you must use StrToInt function!}
- HR_WriteCount,
- {:If is limiting of bandwidth on, then this reason is called when sending or
- receiving is stopped for satisfy bandwidth limit. Parameter is count of
- waiting milliseconds.}
- HR_Wait,
- {:report situation where communication error occured. When raiseexcept is
- @true, then exception is called after this Hook reason.}
- HR_Error
- );
- {:Procedural type for OnStatus event. Sender is calling TBlockSocket object,
- Reason is one of set Status events and value is optional data.}
- THookSocketStatus = procedure(Sender: TObject; Reason: THookSocketReason;
- const Value: string) of object;
- {:This procedural type is used for DataFilter hooks.}
- THookDataFilter = procedure(Sender: TObject; var Value: string) of object;
- {:This procedural type is used for hook OnCreateSocket. By this hook you can
- insert your code after initialisation of socket. (you can set special socket
- options, etc.)}
- THookCreateSocket = procedure(Sender: TObject) of object;
- {:This procedural type is used for monitoring of communication.}
- THookMonitor = procedure(Sender: TObject; Writing: Boolean;
- const Buffer: TMemory; Len: Integer) of object;
- {:This procedural type is used for hook OnAfterConnect. By this hook you can
- insert your code after TCP socket has been sucessfully connected.}
- THookAfterConnect = procedure(Sender: TObject) of object;
- {:This procedural type is used for hook OnVerifyCert. By this hook you can
- insert your additional certificate verification code. Usefull to verify server
- CN against URL. }
- THookVerifyCert = function(Sender: TObject):boolean of object;
- {:This procedural type is used for hook OnHeartbeat. By this hook you can
- call your code repeately during long socket operations.
- You must enable heartbeats by @Link(HeartbeatRate) property!}
- THookHeartbeat = procedure(Sender: TObject) of object;
- {:Specify family of socket.}
- TSocketFamily = (
- {:Default mode. Socket family is defined by target address for connection.
- It allows instant access to IPv4 and IPv6 nodes. When you need IPv6 address
- as destination, then is used IPv6 mode. othervise is used IPv4 mode.
- However this mode not working properly with preliminary IPv6 supports!}
- SF_Any,
- {:Turn this class to pure IPv4 mode. This mode is totally compatible with
- previous Synapse releases.}
- SF_IP4,
- {:Turn to only IPv6 mode.}
- SF_IP6
- );
- {:specify possible values of SOCKS modes.}
- TSocksType = (
- ST_Socks5,
- ST_Socks4
- );
- {:Specify requested SSL/TLS version for secure connection.}
- TSSLType = (
- LT_all,
- LT_SSLv2,
- LT_SSLv3,
- LT_TLSv1,
- LT_TLSv1_1,
- LT_TLSv1_2,
- LT_TLSv1_3,
- LT_SSHv2
- );
- {:Specify type of socket delayed option.}
- TSynaOptionType = (
- SOT_Linger,
- SOT_RecvBuff,
- SOT_SendBuff,
- SOT_NonBlock,
- SOT_RecvTimeout,
- SOT_SendTimeout,
- SOT_Reuse,
- SOT_TTL,
- SOT_Broadcast,
- SOT_MulticastTTL,
- SOT_MulticastLoop,
- SOT_NoDelay // TCP_NODELAY
- );
- {:@abstract(this object is used for remember delayed socket option set.)}
- TSynaOption = record
- Option: TSynaOptionType;
- Enabled: Boolean;
- Value: Integer;
- end;
- TCustomSSL = class;
- TSSLClass = class of TCustomSSL;
- TBlockSocket = class;
- {$IFDEF NEXTGEN}
- TOptionList = TList<TSynaOption>;
- TSocketList = TList<TBlockSocket>;
- {$ELSE}
- TOptionList = TArray<TSynaOption>;
- TSocketList = TList;
- {$ENDIF}
- {:@abstract(Basic IP object.)
- This is parent class for other class with protocol implementations. Do not
- use this class directly! Use @link(TICMPBlockSocket), @link(TRAWBlockSocket),
- @link(TTCPBlockSocket) or @link(TUDPBlockSocket) instead.}
- TBlockSocket = class(TObject)
- private
- FOnStatus: THookSocketStatus;
- FOnReadFilter: THookDataFilter;
- FOnCreateSocket: THookCreateSocket;
- FOnMonitor: THookMonitor;
- FOnHeartbeat: THookHeartbeat;
- FLocalSin: TVarSin;
- FRemoteSin: TVarSin;
- FTag: integer;
- FBuffer: TSynaBytes;
- FRaiseExcept: Boolean;
- FNonBlockMode: Boolean;
- FMaxLineLength: Integer;
- FMaxSendBandwidth: Integer;
- FNextSend: UInt32;
- FMaxRecvBandwidth: Integer;
- FNextRecv: UInt32;
- FConvertLineEnd: Boolean;
- FLastCR: Boolean;
- FLastLF: Boolean;
- FBinded: Boolean;
- FFamily: TSocketFamily;
- FFamilySave: TSocketFamily;
- FIP6used: Boolean;
- FPreferIP4: Boolean;
- FDelayedOptions: TOptionList;
- FInterPacketTimeout: Boolean;
- {$IFNDEF CIL}
- FFDSet: TFDSet;
- {$ENDIF}
- FRecvCounter: int64;
- FSendCounter: int64;
- FSendMaxChunk: int64;
- FStopFlag: Boolean;
- FNonblockSendTimeout: Integer;
- FHeartbeatRate: integer;
- FConnectionTimeout: integer;
- {$IFNDEF ONCEWINSOCK}
- FWsaDataOnce: TWSADATA;
- {$ENDIF}
- FSocket: TSocket;
- FLastError: Integer;
- FLastErrorDesc: string;
- FOwner: TObject;
- function GetSizeRecvBuffer: Integer;
- procedure SetSizeRecvBuffer(Size: Integer);
- function GetSizeSendBuffer: Integer;
- procedure SetSizeSendBuffer(Size: Integer);
- procedure SetNonBlockMode(Value: Boolean);
- procedure SetTTL(TTL: integer);
- function GetTTL:integer;
- procedure SetFamily(Value: TSocketFamily); virtual;
- procedure SetSocket(Value: TSocket); virtual;
- function GetWsaData: TWSAData;
- function FamilyToAF(f: TSocketFamily): TAddrFamily;
- procedure SetNagleMode(Value: Boolean);
- procedure SetDelayedOption(const Value: TSynaOption);
- procedure DelayedOption(const Value: TSynaOption);
- procedure ProcessDelayedOptions;
- procedure InternalCreateSocket(Sin: TVarSin);
- procedure SetSin(var Sin: TVarSin; const IP, Port: string);
- function GetSinIP(Sin: TVarSin): string;
- function GetSinPort(Sin: TVarSin): Integer;
- procedure DoStatus(Reason: THookSocketReason; const Value: string);
- procedure DoReadFilter(Buffer: TMemory; var Len: Integer);
- procedure DoMonitor(Writing: Boolean; const Buffer: TMemory; Len: Integer);
- procedure DoCreateSocket;
- procedure DoHeartbeat;
- procedure LimitBandwidth(Length: Integer; MaxB: integer; var Next: UInt32);
- procedure SetBandwidth(Value: Integer);
- function TestStopFlag: Boolean;
- procedure InternalSendStream(const Stream: TStream; WithSize, Indy: boolean); virtual;
- function InternalCanRead(Timeout: Integer): Boolean; virtual;
- function InternalCanWrite(Timeout: Integer): Boolean; virtual;
- protected
- FDisconnected: Boolean;
- public
- constructor Create;
- {:Create object and load all necessary socket library. What library is
- loaded is described by STUB parameter. If STUB is empty string, then is
- loaded default libraries.}
- constructor CreateAlternate(Stub: string);
- destructor Destroy; override;
- {:If @link(family) is not SF_Any, then create socket with type defined in
- @link(Family) property. If family is SF_Any, then do nothing! (socket is
- created automaticly when you know what type of socket you need to create.
- (i.e. inside @link(Connect) or @link(Bind) call.) When socket is created,
- then is aplyed all stored delayed socket options.}
- procedure CreateSocket;
- {:It create socket. Address resolving of Value tells what type of socket is
- created. If Value is resolved as IPv4 IP, then is created IPv4 socket. If
- value is resolved as IPv6 address, then is created IPv6 socket.}
- procedure CreateSocketByName(const Value: string);
- {:Destroy socket in use. This method is also automatically called from
- object destructor.}
- procedure CloseSocket; virtual;
- {:Abort any work on Socket and destroy them.}
- procedure AbortSocket; virtual;
- {:Connects socket to local IP address and PORT. IP address may be numeric or
- symbolic ('192.168.74.50', 'cosi.nekde.cz', 'ff08::1'). The same for PORT
- - it may be number or mnemonic port ('23', 'telnet').
- If port value is '0', system chooses itself and conects unused port in the
- range 1024 to 4096 (this depending by operating system!). Structure
- LocalSin is filled after calling this method.
- Note: If you call this on non-created socket, then socket is created
- automaticly.
- Warning: when you call : Bind('0.0.0.0','0'); then is nothing done! In this
- case is used implicit system bind instead.}
- procedure Bind(const IP, Port: string);
- {:Connects socket to remote IP address and PORT. The same rules as with
- @link(BIND) method are valid. The only exception is that PORT with 0 value
- will not be connected!
- Structures LocalSin and RemoteSin will be filled with valid values.
- When you call this on non-created socket, then socket is created
- automaticly. Type of created socket is by @link(Family) property. If is
- used SF_IP4, then is created socket for IPv4. If is used SF_IP6, then is
- created socket for IPv6. When you have family on SF_Any (default!), then
- type of created socket is determined by address resolving of destination
- address. (Not work properly on prilimitary winsock IPv6 support!)}
- procedure Connect(const IP, Port: string); virtual;
- {:Sets socket to receive mode for new incoming connections. It is necessary
- to use @link(TBlockSocket.BIND) function call before this method to select
- receiving port!}
- procedure Listen; virtual;
- {:Waits until new incoming connection comes. After it comes a new socket is
- automatically created (socket handler is returned by this function as
- result).}
- function Accept: TSocket; virtual;
- {:Sends data of LENGTH from BUFFER address via connected socket. System
- automatically splits data to packets.}
- function SendBuffer(const Buffer: Tmemory; Length: Integer): Integer; virtual;
- {:One data BYTE is sent via connected socket.}
- procedure SendByte(Data: Byte); virtual;
- {:Send data string via connected socket. Any terminator is not added! If you
- need send true string with CR-LF termination, you must add CR-LF characters
- to sended string! Because any termination is not added automaticly, you can
- use this function for sending any binary data in binary string.}
- procedure SendString(Data: TSynaBytes); virtual;
- {:Send integer as four bytes to socket.}
- procedure SendInteger(Data: integer); virtual;
- {:Send data as one block to socket. Each block begin with 4 bytes with
- length of data in block. This 4 bytes is added automaticly by this
- function.}
- procedure SendBlock(const Data: string); virtual;
- {:Send data from stream to socket.}
- procedure SendStreamRaw(const Stream: TStream); virtual;
- {:Send content of stream to socket. It using @link(SendBlock) method}
- procedure SendStream(const Stream: TStream); virtual;
- {:Send content of stream to socket. It using @link(SendBlock) method and
- this is compatible with streams in Indy library.}
- procedure SendStreamIndy(const Stream: TStream); virtual;
- {:Note: This is low-level receive function. You must be sure if data is
- waiting for read before call this function for avoid deadlock!
- Waits until allocated buffer is filled by received data. Returns number of
- data received, which equals to LENGTH value under normal operation. If it
- is not equal the communication channel is possibly broken.
- On stream oriented sockets if is received 0 bytes, it mean 'socket is
- closed!"
- On datagram socket is readed first waiting datagram.}
- function RecvBuffer(Buffer: TMemory; Length: Integer): Integer; virtual;
- {:Note: This is high-level receive function. It using internal
- @link(LineBuffer) and you can combine this function freely with other
- high-level functions!
- Method waits until data is received. If no data is received within TIMEOUT
- (in milliseconds) period, @link(LastError) is set to WSAETIMEDOUT. Methods
- serves for reading any size of data (i.e. one megabyte...). This method is
- preffered for reading from stream sockets (like TCP).}
- function RecvBufferEx(Buffer: Tmemory; Len: Integer;
- Timeout: Integer): Integer; virtual;
- {:Similar to @link(RecvBufferEx), but readed data is stored in binary
- string, not in memory buffer.}
- function RecvBufferStr(Len: Integer; Timeout: Integer): TSynaBytes; virtual;
- {:Note: This is high-level receive function. It using internal
- @link(LineBuffer) and you can combine this function freely with other
- high-level functions.
- Waits until one data byte is received which is also returned as function
- result. If no data is received within TIMEOUT (in milliseconds)period,
- @link(LastError) is set to WSAETIMEDOUT and result have value 0.}
- function RecvByte(Timeout: Integer): Byte; virtual;
- {:Note: This is high-level receive function. It using internal
- @link(LineBuffer) and you can combine this function freely with other
- high-level functions.
- Waits until one four bytes are received and return it as one Ineger Value.
- If no data is received within TIMEOUT (in milliseconds)period,
- @link(LastError) is set to WSAETIMEDOUT and result have value 0.}
- function RecvInteger(Timeout: Integer): Integer; virtual;
- {:Note: This is high-level receive function. It using internal
- @link(LineBuffer) and you can combine this function freely with other
- high-level functions.
- Method waits until data string is received. This string is terminated by
- CR-LF characters. The resulting string is returned without this termination
- (CR-LF)! If @link(ConvertLineEnd) is used, then CR-LF sequence may not be
- exactly CR-LF. See @link(ConvertLineEnd) description. If no data is
- received within TIMEOUT (in milliseconds) period, @link(LastError) is set
- to WSAETIMEDOUT. You may also specify maximum length of reading data by
- @link(MaxLineLength) property.}
- function RecvString(Timeout: Integer): string; virtual;
- {:Note: This is high-level receive function. It using internal
- @link(LineBuffer) and you can combine this function freely with other
- high-level functions.
- Method waits until data string is received. This string is terminated by
- Terminator string. The resulting string is returned without this
- termination. If no data is received within TIMEOUT (in milliseconds)
- period, @link(LastError) is set to WSAETIMEDOUT. You may also specify
- maximum length of reading data by @link(MaxLineLength) property.}
- function RecvTerminated(Timeout: Integer; const Terminator: string): string; virtual;
- {:Note: This is high-level receive function. It using internal
- @link(LineBuffer) and you can combine this function freely with other
- high-level functions.
- Method reads all data waiting for read. If no data is received within
- TIMEOUT (in milliseconds) period, @link(LastError) is set to WSAETIMEDOUT.
- Methods serves for reading unknown size of data. Because before call this
- function you don't know size of received data, returned data is stored in
- dynamic size binary string. This method is preffered for reading from
- stream sockets (like TCP). It is very goot for receiving datagrams too!
- (UDP protocol)}
- function RecvPacket(Timeout: Integer): TSynaBytes; virtual;
- {:Read one block of data from socket. Each block begin with 4 bytes with
- length of data in block. This function read first 4 bytes for get lenght,
- then it wait for reported count of bytes.}
- function RecvBlock(Timeout: Integer): string; virtual;
- {:Read all data from socket to stream until socket is closed (or any error
- occured.)}
- procedure RecvStreamRaw(const Stream: TStream; Timeout: Integer); virtual;
- {:Read requested count of bytes from socket to stream.}
- procedure RecvStreamSize(const Stream: TStream; Timeout: Integer; Size: int64);
- {:Receive data to stream. It using @link(RecvBlock) method.}
- procedure RecvStream(const Stream: TStream; Timeout: Integer); virtual;
- {:Receive data to stream. This function is compatible with similar function
- in Indy library. It using @link(RecvBlock) method.}
- procedure RecvStreamIndy(const Stream: TStream; Timeout: Integer); virtual;
- {:Same as @link(RecvBuffer), but readed data stays in system input buffer.
- Warning: this function not respect data in @link(LineBuffer)! Is not
- recommended to use this function!}
- function PeekBuffer(Buffer: TMemory; Length: Integer): Integer; virtual;
- {:Same as @link(RecvByte), but readed data stays in input system buffer.
- Warning: this function not respect data in @link(LineBuffer)! Is not
- recommended to use this function!}
- function PeekByte(Timeout: Integer): Byte; virtual;
- {:On stream sockets it returns number of received bytes waiting for picking.
- 0 is returned when there is no such data. On datagram socket it returns
- length of the first waiting datagram. Returns 0 if no datagram is waiting.}
- function WaitingData: Integer; virtual;
- {:Same as @link(WaitingData), but if exists some of data in @link(Linebuffer),
- return their length instead.}
- function WaitingDataEx: Integer;
- {:Clear all waiting data for read from buffers.}
- procedure Purge;
- {:Sets linger. Enabled linger means that the system waits another LINGER
- (in milliseconds) time for delivery of sent data. This function is only for
- stream type of socket! (TCP)}
- procedure SetLinger(Enable: Boolean; Linger: Integer);
- {:Actualize values in @link(LocalSin).}
- procedure GetSinLocal;
- {:Actualize values in @link(RemoteSin).}
- procedure GetSinRemote;
- {:Actualize values in @link(LocalSin) and @link(RemoteSin).}
- procedure GetSins;
- {:Reset @link(LastError) and @link(LastErrorDesc) to non-error state.}
- procedure ResetLastError;
- {:If you "manually" call Socket API functions, forward their return code as
- parameter to this function, which evaluates it, eventually calls
- GetLastError and found error code returns and stores to @link(LastError).}
- function SockCheck(SockResult: Integer): Integer; virtual;
- {:If @link(LastError) contains some error code and @link(RaiseExcept)
- property is @true, raise adequate exception.}
- procedure ExceptCheck;
- {:Returns local computer name as numerical or symbolic value. It try get
- fully qualified domain name. Name is returned in the format acceptable by
- functions demanding IP as input parameter.}
- function LocalName: string;
- {:Try resolve name to all possible IP address. i.e. If you pass as name
- result of @link(LocalName) method, you get all IP addresses used by local
- system.}
- procedure ResolveNameToIP(const Name: string; const IPList: TStrings);
- {:Try resolve name to primary IP address. i.e. If you pass as name result of
- @link(LocalName) method, you get primary IP addresses used by local system.}
- function ResolveName(const Name: string): string;
- {:Try resolve IP to their primary domain name. If IP not have domain name,
- then is returned original IP.}
- function ResolveIPToName(IP: string): string;
- {:Try resolve symbolic port name to port number. (i.e. 'Echo' to 8)}
- function ResolvePort(const Port: string): Word;
- {:Set information about remote side socket. It is good for seting remote
- side for sending UDP packet, etc.}
- procedure SetRemoteSin(const IP, Port: string);
- {:Picks IP socket address from @link(LocalSin).}
- function GetLocalSinIP: string; virtual;
- {:Picks IP socket address from @link(RemoteSin).}
- function GetRemoteSinIP: string; virtual;
- {:Picks socket PORT number from @link(LocalSin).}
- function GetLocalSinPort: Integer; virtual;
- {:Picks socket PORT number from @link(RemoteSin).}
- function GetRemoteSinPort: Integer; virtual;
- {:Return @TRUE, if you can read any data from socket or is incoming
- connection on TCP based socket. Status is tested for time Timeout (in
- milliseconds). If value in Timeout is 0, status is only tested and
- continue. If value in Timeout is -1, run is breaked and waiting for read
- data maybe forever.
- This function is need only on special cases, when you need use
- @link(RecvBuffer) function directly! read functioms what have timeout as
- calling parameter, calling this function internally.}
- function CanRead(Timeout: Integer): Boolean; virtual;
- {:Same as @link(CanRead), but additionally return @TRUE if is some data in
- @link(LineBuffer).}
- function CanReadEx(Timeout: Integer): Boolean; virtual;
- {:Return @TRUE, if you can to socket write any data (not full sending
- buffer). Status is tested for time Timeout (in milliseconds). If value in
- Timeout is 0, status is only tested and continue. If value in Timeout is
- -1, run is breaked and waiting for write data maybe forever.
- This function is need only on special cases!}
- function CanWrite(Timeout: Integer): Boolean; virtual;
- {:Same as @link(SendBuffer), but send datagram to address from
- @link(RemoteSin). Usefull for sending reply to datagram received by
- function @link(RecvBufferFrom).}
- function SendBufferTo(const Buffer: TMemory; Length: Integer): Integer; virtual;
- {:Note: This is low-lever receive function. You must be sure if data is
- waiting for read before call this function for avoid deadlock!
- Receives first waiting datagram to allocated buffer. If there is no waiting
- one, then waits until one comes. Returns length of datagram stored in
- BUFFER. If length exceeds buffer datagram is truncated. After this
- @link(RemoteSin) structure contains information about sender of UDP packet.}
- function RecvBufferFrom(Buffer: TMemory; Length: Integer): Integer; virtual;
- {$IFNDEF CIL}
- {:This function is for check for incoming data on set of sockets. Whitch
- sockets is checked is decribed by SocketList Tlist with TBlockSocket
- objects. TList may have maximal number of objects defined by FD_SETSIZE
- constant. Return @TRUE, if you can from some socket read any data or is
- incoming connection on TCP based socket. Status is tested for time Timeout
- (in milliseconds). If value in Timeout is 0, status is only tested and
- continue. If value in Timeout is -1, run is breaked and waiting for read
- data maybe forever. If is returned @TRUE, CanReadList TList is filled by all
- TBlockSocket objects what waiting for read.}
- function GroupCanRead(const SocketList: TSocketList; Timeout: Integer;
- const CanReadList: TSocketList): Boolean;
- {$ENDIF}
- {:By this method you may turn address reuse mode for local @link(bind). It
- is good specially for UDP protocol. Using this with TCP protocol is
- hazardous!}
- procedure EnableReuse(Value: Boolean);
- {:Try set timeout for all sending and receiving operations, if socket
- provider can do it. (It not supported by all socket providers!)}
- procedure SetTimeout(Timeout: Integer);
- {:Try set timeout for all sending operations, if socket provider can do it.
- (It not supported by all socket providers!)}
- procedure SetSendTimeout(Timeout: Integer);
- {:Try set timeout for all receiving operations, if socket provider can do
- it. (It not supported by all socket providers!)}
- procedure SetRecvTimeout(Timeout: Integer);
- function GetSendTimeout: Integer;
- function GetRecvTimeout: integer;
- {:Return value of socket type.}
- function GetSocketType: integer; Virtual;
- {:Return value of protocol type for socket creation.}
- function GetSocketProtocol: integer; Virtual;
- {:WSA structure with information about socket provider. On non-windows
- platforms this structure is simulated!}
- property WSAData: TWSADATA read GetWsaData;
- {:FDset structure prepared for usage with this socket.}
- property FDset: TFDSet read FFDset;
- {:Structure describing local socket side.}
- property LocalSin: TVarSin read FLocalSin write FLocalSin;
- {:Structure describing remote socket side.}
- property RemoteSin: TVarSin read FRemoteSin write FRemoteSin;
- {:Socket handler. Suitable for "manual" calls to socket API or manual
- connection of socket to a previously created socket (i.e by Accept method
- on TCP socket)}
- property Socket: TSocket read FSocket write SetSocket;
- {:Last socket operation error code. Error codes are described in socket
- documentation. Human readable error description is stored in
- @link(LastErrorDesc) property.}
- property LastError: Integer read FLastError;
- {:Human readable error description of @link(LastError) code.}
- property LastErrorDesc: string read FLastErrorDesc;
- {:Buffer used by all high-level receiving functions. This buffer is used for
- optimized reading of data from socket. In normal cases you not need access
- to this buffer directly!}
- property LineBuffer: TSynaBytes read FBuffer write FBuffer;
- {:Size of Winsock receive buffer. If it is not supported by socket provider,
- it return as size one kilobyte.}
- property SizeRecvBuffer: Integer read GetSizeRecvBuffer write SetSizeRecvBuffer;
- {:Size of Winsock send buffer. If it is not supported by socket provider, it
- return as size one kilobyte.}
- property SizeSendBuffer: Integer read GetSizeSendBuffer write SetSizeSendBuffer;
- {:If @True, turn class to non-blocking mode. Not all functions are working
- properly in this mode, you must know exactly what you are doing! However
- when you have big experience with non-blocking programming, then you can
- optimise your program by non-block mode!}
- property NonBlockMode: Boolean read FNonBlockMode Write SetNonBlockMode;
- {:Set Time-to-live value. (if system supporting it!)}
- property TTL: Integer read GetTTL Write SetTTL;
- {:If is @true, then class in in IPv6 mode.}
- property IP6used: Boolean read FIP6used;
- {:Return count of received bytes on this socket from begin of current
- connection.}
- property RecvCounter: int64 read FRecvCounter;
- {:Return count of sended bytes on this socket from begin of current
- connection.}
- property SendCounter: int64 read FSendCounter;
- published
- {:Return descriptive string for given error code. This is class function.
- You may call it without created object!}
- class function GetErrorDesc(ErrorCode: Integer): string;
- {:Return descriptive string for @link(LastError).}
- function GetErrorDescEx: string; virtual;
- {:this value is for free use.}
- property Tag: Integer read FTag write FTag;
- {:If @true, winsock errors raises exception. Otherwise is setted
- @link(LastError) value only and you must check it from your program! Default
- value is @false.}
- property RaiseExcept: Boolean read FRaiseExcept write FRaiseExcept;
- {:Define maximum length in bytes of @link(LineBuffer) for high-level
- receiving functions. If this functions try to read more data then this
- limit, error is returned! If value is 0 (default), no limitation is used.
- This is very good protection for stupid attacks to your server by sending
- lot of data without proper terminator... until all your memory is allocated
- by LineBuffer!
- Note: This maximum length is checked only in functions, what read unknown
- number of bytes! (like @link(RecvString) or @link(RecvTerminated))}
- property MaxLineLength: Integer read FMaxLineLength Write FMaxLineLength;
- {:Define maximal bandwidth for all sending operations in bytes per second.
- If value is 0 (default), bandwidth limitation is not used.}
- property MaxSendBandwidth: Integer read FMaxSendBandwidth Write FMaxSendBandwidth;
- {:Define maximal bandwidth for all receiving operations in bytes per second.
- If value is 0 (default), bandwidth limitation is not used.}
- property MaxRecvBandwidth: Integer read FMaxRecvBandwidth Write FMaxRecvBandwidth;
- {:Define maximal bandwidth for all sending and receiving operations in bytes
- per second. If value is 0 (default), bandwidth limitation is not used.}
- property MaxBandwidth: Integer Write SetBandwidth;
- {:Do a conversion of non-standard line terminators to CRLF. (Off by default)
- If @True, then terminators like sigle CR, single LF or LFCR are converted
- to CRLF internally. This have effect only in @link(RecvString) method!}
- property ConvertLineEnd: Boolean read FConvertLineEnd Write FConvertLineEnd;
- {:Specified Family of this socket. When you are using Windows preliminary
- support for IPv6, then I recommend to set this property!}
- property Family: TSocketFamily read FFamily Write SetFamily;
- {:When resolving of domain name return both IPv4 and IPv6 addresses, then
- specify if is used IPv4 (dafault - @true) or IPv6.}
- property PreferIP4: Boolean read FPreferIP4 Write FPreferIP4;
- {:By default (@true) is all timeouts used as timeout between two packets in
- reading operations. If you set this to @false, then Timeouts is for overall
- reading operation!}
- property InterPacketTimeout: Boolean read FInterPacketTimeout Write FInterPacketTimeout;
- {:All sended datas was splitted by this value.}
- property SendMaxChunk: int64 read FSendMaxChunk Write FSendMaxChunk;
- {:By setting this property to @true you can stop any communication. You can
- use this property for soft abort of communication.}
- property StopFlag: Boolean read FStopFlag Write FStopFlag;
- {:Timeout for data sending by non-blocking socket mode.}
- property NonblockSendTimeout: Integer read FNonblockSendTimeout Write FNonblockSendTimeout;
- property NagleMode: Boolean write SetNagleMode; // True (Default) - TCP_NODELAY OFF
- // False - TCP_NODELAY ON
- {:Timeout for @link(Connect) call. Default value 0 means default system timeout.
- Non-zero value means timeout in millisecond.}
- property ConnectionTimeout: Integer read FConnectionTimeout write FConnectionTimeout;
- {:This event is called by various reasons. It is good for monitoring socket,
- create gauges for data transfers, etc.}
- property OnStatus: THookSocketStatus read FOnStatus write FOnStatus;
- {:this event is good for some internal thinks about filtering readed datas.
- It is used by telnet client by example.}
- property OnReadFilter: THookDataFilter read FOnReadFilter write FOnReadFilter;
- {:This event is called after real socket creation for setting special socket
- options, because you not know when socket is created. (it is depended on
- Ipv4, IPv6 or automatic mode)}
- property OnCreateSocket: THookCreateSocket read FOnCreateSocket write FOnCreateSocket;
- {:This event is good for monitoring content of readed or writed datas.}
- property OnMonitor: THookMonitor read FOnMonitor write FOnMonitor;
- {:This event is good for calling your code during long socket operations.
- (Example, for refresing UI if class in not called within the thread.)
- Rate of heartbeats can be modified by @link(HeartbeatRate) property.}
- property OnHeartbeat: THookHeartbeat read FOnHeartbeat write FOnHeartbeat;
- {:Specify typical rate of @link(OnHeartbeat) event and @link(StopFlag) testing.
- Default value 0 disabling heartbeats! Value is in milliseconds.
- Real rate can be higher or smaller then this value, because it depending
- on real socket operations too!
- Note: Each heartbeat slowing socket processing.}
- property HeartbeatRate: integer read FHeartbeatRate Write FHeartbeatRate;
- {:What class own this socket? Used by protocol implementation classes.}
- property Owner: TObject read FOwner Write FOwner;
- end;
- {:@abstract(Support for SOCKS4 and SOCKS5 proxy)
- Layer with definition all necessary properties and functions for
- implementation SOCKS proxy client. Do not use this class directly.}
- TSocksBlockSocket = class(TBlockSocket)
- private
- FSocksIP: string;
- FSocksPort: string;
- FSocksTimeout: integer;
- FSocksUsername: string;
- FSocksPassword: string;
- FUsingSocks: Boolean;
- FSocksResolver: Boolean;
- FSocksLastError: integer;
- FSocksResponseIP: string;
- FSocksResponsePort: string;
- FSocksLocalIP: string;
- FSocksLocalPort: string;
- FSocksRemoteIP: string;
- FSocksRemotePort: string;
- FBypassFlag: Boolean;
- FSocksType: TSocksType;
- function SocksCode(IP: string; const Port: string): string;
- function SocksDecode(const Value: string): integer;
- public
- constructor Create;
- {:Open connection to SOCKS proxy and if @link(SocksUsername) is set, do
- authorisation to proxy. This is needed only in special cases! (it is called
- internally!)}
- function SocksOpen: Boolean;
- {:Send specified request to SOCKS proxy. This is needed only in special
- cases! (it is called internally!)}
- function SocksRequest(Cmd: Byte; const IP, Port: string): Boolean;
- {:Receive response to previosly sended request. This is needed only in
- special cases! (it is called internally!)}
- function SocksResponse: Boolean;
- {:Is @True when class is using SOCKS proxy.}
- property UsingSocks: Boolean read FUsingSocks;
- {:If SOCKS proxy failed, here is error code returned from SOCKS proxy.}
- property SocksLastError: integer read FSocksLastError;
- published
- {:Address of SOCKS server. If value is empty string, SOCKS support is
- disabled. Assingning any value to this property enable SOCKS mode.
- Warning: You cannot combine this mode with HTTP-tunneling mode!}
- property SocksIP: string read FSocksIP write FSocksIP;
- {:Port of SOCKS server. Default value is '1080'.}
- property SocksPort: string read FSocksPort write FSocksPort;
- {:If you need authorisation on SOCKS server, set username here.}
- property SocksUsername: string read FSocksUsername write FSocksUsername;
- {:If you need authorisation on SOCKS server, set password here.}
- property SocksPassword: string read FSocksPassword write FSocksPassword;
- {:Specify timeout for communicatin with SOCKS server. Default is one minute.}
- property SocksTimeout: integer read FSocksTimeout write FSocksTimeout;
- {:If @True, all symbolic names of target hosts is not translated to IP's
- locally, but resolving is by SOCKS proxy. Default is @True.}
- property SocksResolver: Boolean read FSocksResolver write FSocksResolver;
- {:Specify SOCKS type. By default is used SOCKS5, but you can use SOCKS4 too.
- When you select SOCKS4, then if @link(SOCKSResolver) is enabled, then is
- used SOCKS4a. Othervise is used pure SOCKS4.}
- property SocksType: TSocksType read FSocksType write FSocksType;
- end;
- {:@abstract(Implementation of TCP socket.)
- Supported features: IPv4, IPv6, SSL/TLS or SSH (depending on used plugin),
- SOCKS5 proxy (outgoing connections and limited incomming), SOCKS4/4a proxy
- (outgoing connections and limited incomming), TCP through HTTP proxy tunnel.}
- TTCPBlockSocket = class(TSocksBlockSocket)
- private
- FOnAfterConnect: THookAfterConnect;
- FSSL: TCustomSSL;
- FHTTPTunnelIP: string;
- FHTTPTunnelPort: string;
- FHTTPTunnel: Boolean;
- FHTTPTunnelRemoteIP: string;
- FHTTPTunnelRemotePort: string;
- FHTTPTunnelUser: string;
- FHTTPTunnelPass: string;
- FHTTPTunnelTimeout: integer;
- procedure SocksDoConnect(const IP, Port: string);
- procedure HTTPTunnelDoConnect(IP, Port: string);
- procedure DoAfterConnect;
- public
- {:Create TCP socket class with default plugin for SSL/TSL/SSH implementation
- (see @link(SSLImplementation))}
- constructor Create;
- {:Create TCP socket class with desired plugin for SSL/TSL/SSH implementation}
- constructor CreateWithSSL(SSLPlugin: TSSLClass);
- destructor Destroy; override;
- {:See @link(TBlockSocket.CloseSocket)}
- procedure CloseSocket; override;
- {:See @link(TBlockSocket.WaitingData)}
- function WaitingData: Integer; override;
- {:Sets socket to receive mode for new incoming connections. It is necessary
- to use @link(TBlockSocket.BIND) function call before this method to select
- receiving port!
- If you use SOCKS, activate incoming TCP connection by this proxy. (By BIND
- method of SOCKS.)}
- procedure Listen; override;
- {:Waits until new incoming connection comes. After it comes a new socket is
- automatically created (socket handler is returned by this function as
- result).
- If you use SOCKS, new socket is not created! In this case is used same
- socket as socket for listening! So, you can accept only one connection in
- SOCKS mode.}
- function Accept: TSocket; override;
- {:Connects socket to remote IP address and PORT. The same rules as with
- @link(TBlockSocket.BIND) method are valid. The only exception is that PORT
- with 0 value will not be connected. After call to this method
- a communication channel between local and remote socket is created. Local
- socket is assigned automatically if not controlled by previous call to
- @link(TBlockSocket.BIND) method. Structures @link(TBlockSocket.LocalSin)
- and @link(TBlockSocket.RemoteSin) will be filled with valid values.
- If you use SOCKS, activate outgoing TCP connection by SOCKS proxy specified
- in @link(TSocksBlockSocket.SocksIP). (By CONNECT method of SOCKS.)
- If you use HTTP-tunnel mode, activate outgoing TCP connection by HTTP
- tunnel specified in @link(HTTPTunnelIP). (By CONNECT method of HTTP
- protocol.)
- Note: If you call this on non-created socket, then socket is created
- automaticly.}
- procedure Connect(const IP, Port: string); override;
- {:If you need upgrade existing TCP connection to SSL/TLS (or SSH2, if plugin
- allows it) mode, then call this method. This method switch this class to
- SSL mode and do SSL/TSL handshake.}
- procedure SSLDoConnect;
- {:By this method you can downgrade existing SSL/TLS connection to normal TCP
- connection.}
- procedure SSLDoShutdown;
- {:If you need use this component as SSL/TLS TCP server, then after accepting
- of inbound connection you need start SSL/TLS session by this method. Before
- call this function, you must have assigned all neeeded certificates and
- keys!}
- function SSLAcceptConnection: Boolean;
- {:See @link(TBlockSocket.GetLocalSinIP)}
- function GetLocalSinIP: string; override;
- {:See @link(TBlockSocket.GetRemoteSinIP)}
- function GetRemoteSinIP: string; override;
- {:See @link(TBlockSocket.GetLocalSinPort)}
- function GetLocalSinPort: Integer; override;
- {:See @link(TBlockSocket.GetRemoteSinPort)}
- function GetRemoteSinPort: Integer; override;
- {:See @link(TBlockSocket.SendBuffer)}
- function SendBuffer(const Buffer: TMemory; Length: Integer): Integer; override;
- {:See @link(TBlockSocket.RecvBuffer)}
- function RecvBuffer(Buffer: TMemory; Len: Integer): Integer; override;
- {:Return value of socket type. For TCP return SOCK_STREAM.}
- function GetSocketType: integer; override;
- {:Return value of protocol type for socket creation. For TCP return
- IPPROTO_TCP.}
- function GetSocketProtocol: integer; override;
- function Connected: boolean;
- {:Class implementing SSL/TLS support. It is allways some descendant
- of @link(TCustomSSL) class. When programmer not select some SSL plugin
- class, then is used @link(TSSLNone)}
- property SSL: TCustomSSL read FSSL;
- {:@True if is used HTTP tunnel mode.}
- property HTTPTunnel: Boolean read FHTTPTunnel;
- property Disconnected: Boolean read FDisconnected write FDisconnected;
- published
- {:Return descriptive string for @link(LastError). On case of error
- in SSL/TLS subsystem, it returns right error description.}
- function GetErrorDescEx: string; override;
- {:Specify IP address of HTTP proxy. Assingning non-empty value to this
- property enable HTTP-tunnel mode. This mode is for tunnelling any outgoing
- TCP connection through HTTP proxy server. (If policy on HTTP proxy server
- allow this!) Warning: You cannot combine this mode with SOCK5 mode!}
- property HTTPTunnelIP: string read FHTTPTunnelIP Write FHTTPTunnelIP;
- {:Specify port of HTTP proxy for HTTP-tunneling.}
- property HTTPTunnelPort: string read FHTTPTunnelPort Write FHTTPTunnelPort;
- {:Specify authorisation username for access to HTTP proxy in HTTP-tunnel
- mode. If you not need authorisation, then let this property empty.}
- property HTTPTunnelUser: string read FHTTPTunnelUser Write FHTTPTunnelUser;
- {:Specify authorisation password for access to HTTP proxy in HTTP-tunnel
- mode.}
- property HTTPTunnelPass: string read FHTTPTunnelPass Write FHTTPTunnelPass;
- {:Specify timeout for communication with HTTP proxy in HTTPtunnel mode.}
- property HTTPTunnelTimeout: integer read FHTTPTunnelTimeout Write FHTTPTunnelTimeout;
- {:This event is called after sucessful TCP socket connection.}
- property OnAfterConnect: THookAfterConnect read FOnAfterConnect write FOnAfterConnect;
- end;
- {:@abstract(Datagram based communication)
- This class implementing datagram based communication instead default stream
- based communication style.}
- TDgramBlockSocket = class(TSocksBlockSocket)
- protected
- FUseConnect: Boolean;
- public
- {:Fill @link(TBlockSocket.RemoteSin) structure. This address is used for
- sending data.}
- procedure Connect(const IP, Port: string); override;
- {:Silently redirected to @link(TBlockSocket.SendBufferTo).}
- function SendBuffer(const Buffer: TMemory; Length: Integer): Integer; override;
- {:Silently redirected to @link(TBlockSocket.RecvBufferFrom).}
- function RecvBuffer(Buffer: TMemory; Length: Integer): Integer; override;
- {:Specify if connect should called on the underlying socket.}
- property UseConnect: Boolean read FUseConnect Write FUseConnect;
- end;
- {:@abstract(Implementation of UDP socket.)
- NOTE: in this class is all receiving redirected to RecvBufferFrom. You can
- use for reading any receive function. Preffered is RecvPacket! Similary all
- sending is redirected to SendbufferTo. You can use for sending UDP packet any
- sending function, like SendString.
- Supported features: IPv4, IPv6, unicasts, broadcasts, multicasts, SOCKS5
- proxy (only unicasts! Outgoing and incomming.)}
- TUDPBlockSocket = class(TDgramBlockSocket)
- protected
- FSocksControlSock: TTCPBlockSocket;
- function UdpAssociation: Boolean;
- procedure SetMulticastTTL(TTL: integer);
- function GetMulticastTTL:integer;
- public
- destructor Destroy; override;
- {:Enable or disable sending of broadcasts. If seting OK, result is @true.
- This method is not supported in SOCKS5 mode! IPv6 does not support
- broadcasts! In this case you must use Multicasts instead.}
- procedure EnableBroadcast(Value: Boolean);
- {:See @link(TBlockSocket.SendBufferTo)}
- function SendBufferTo(const Buffer: TMemory; Length: Integer): Integer; override;
- {:See @link(TBlockSocket.RecvBufferFrom)}
- function RecvBufferFrom(Buffer: TMemory; Length: Integer): Integer; override;
- {$IFNDEF CIL}
- {:Add this socket to given multicast group. You cannot use Multicasts in
- SOCKS mode!}
- procedure AddMulticast(const MCastIP:string);
- {:Remove this socket from given multicast group.}
- procedure DropMulticast(const MCastIP:string);
- {$ENDIF}
- {:All sended multicast datagrams is loopbacked to your interface too. (you
- can read your sended datas.) You can disable this feature by this function.
- This function not working on some Windows systems!}
- procedure EnableMulticastLoop(Value: Boolean);
- {:Return value of socket type. For UDP return SOCK_DGRAM.}
- function GetSocketType: integer; override;
- {:Return value of protocol type for socket creation. For UDP return
- IPPROTO_UDP.}
- function GetSocketProtocol: integer; override;
- {:Set Time-to-live value for multicasts packets. It define number of routers
- for transfer of datas. If you set this to 1 (dafault system value), then
- multicasts packet goes only to you local network. If you need transport
- multicast packet to worldwide, then increase this value, but be carefull,
- lot of routers on internet does not transport multicasts packets!}
- property MulticastTTL: Integer read GetMulticastTTL Write SetMulticastTTL;
- end;
- {:@abstract(Implementation of RAW ICMP socket.)
- For this object you must have rights for creating RAW sockets!}
- TICMPBlockSocket = class(TDgramBlockSocket)
- public
- {:Return value of socket type. For RAW and ICMP return SOCK_RAW.}
- function GetSocketType: integer; override;
- {:Return value of protocol type for socket creation. For ICMP returns
- IPPROTO_ICMP or IPPROTO_ICMPV6}
- function GetSocketProtocol: integer; override;
- end;
- {:@abstract(Implementation of RAW socket.)
- For this object you must have rights for creating RAW sockets!}
- TRAWBlockSocket = class(TBlockSocket)
- public
- {:Return value of socket type. For RAW and ICMP return SOCK_RAW.}
- function GetSocketType: integer; override;
- {:Return value of protocol type for socket creation. For RAW returns
- IPPROTO_RAW.}
- function GetSocketProtocol: integer; override;
- end;
- {:@abstract(Implementation of PGM-message socket.)
- Not all systems supports this protocol!}
- TPGMMessageBlockSocket = class(TBlockSocket)
- public
- {:Return value of socket type. For PGM-message return SOCK_RDM.}
- function GetSocketType: integer; override;
- {:Return value of protocol type for socket creation. For PGM-message returns
- IPPROTO_RM.}
- function GetSocketProtocol: integer; override;
- end;
- {:@abstract(Implementation of PGM-stream socket.)
- Not all systems supports this protocol!}
- TPGMStreamBlockSocket = class(TBlockSocket)
- public
- {:Return value of socket type. For PGM-stream return SOCK_STREAM.}
- function GetSocketType: integer; override;
- {:Return value of protocol type for socket creation. For PGM-stream returns
- IPPROTO_RM.}
- function GetSocketProtocol: integer; override;
- end;
- {:@abstract(Parent class for all SSL plugins.)
- This is abstract class defining interface for other SSL plugins.
- Instance of this class will be created for each @link(TTCPBlockSocket).
- Warning: not all methods and propertis can work in all existing SSL plugins!
- Please, read documentation of used SSL plugin.}
- TCustomSSL = class(TObject)
- private
- FOnVerifyCert: THookVerifyCert;
- FCertCA: string;
- FTrustCertificate: string;
- FTrustCertificateFile: string;
- FUsername: string;
- FPassword: string;
- FSSHChannelType: string;
- FSSHChannelArg1: string;
- FSSHChannelArg2: string;
- FCertComplianceLevel: integer;
- FSNIHost: string;
- procedure ReturnError;
- procedure SetCertCAFile(const Value: string); virtual;
- protected
- FCiphers: string;
- FPrivateKey: string;
- FSSLEnabled: Boolean;
- FSocket: TTCPBlockSocket;
- FKeyPassword: string;
- FSSLType: TSSLType;
- FVerifyCert: Boolean;
- FCertificateFile: string;
- FCertCAFile: string;
- FPFXfile: string;
- FPFX: string;
- FPrivateKeyFile: string;
- FLastErrorDesc: string;
- FLastError: integer;
- FCertificate: string;
- function DoVerifyCert:boolean;
- function CreateSelfSignedCert(Host: string): Boolean; virtual;
- public
- {: Create plugin class. it is called internally from @link(TTCPBlockSocket)}
- constructor Create(const Value: TTCPBlockSocket); virtual;
- {: Assign settings (certificates and configuration) from another SSL plugin
- class.}
- procedure Assign(const Value: TCustomSSL); virtual;
- {: return description of used plugin. It usually return name and version
- of used SSL library.}
- function LibVersion: string; virtual;
- {: return name of used plugin.}
- function LibName: string; virtual;
- {: Do not call this directly. It is used internally by @link(TTCPBlockSocket)!
- Here is needed code for start SSL connection.}
- function Connect: boolean; virtual;
- {: Do not call this directly. It is used internally by @link(TTCPBlockSocket)!
- Here is needed code for acept new SSL connection.}
- function Accept: boolean; virtual;
- {: Do not call this directly. It is used internally by @link(TTCPBlockSocket)!
- Here is needed code for hard shutdown of SSL connection. (for example,
- before socket is closed)}
- function Shutdown: boolean; virtual;
- {: Do not call this directly. It is used internally by @link(TTCPBlockSocket)!
- Here is needed code for soft shutdown of SSL connection. (for example,
- when you need to continue with unprotected connection.)}
- function BiShutdown: boolean; virtual;
- {: Do not call this directly. It is used internally by @link(TTCPBlockSocket)!
- Here is needed code for sending some datas by SSL connection.}
- function SendBuffer(Buffer: TMemory; Len: Integer): Integer; virtual;
- {: Do not call this directly. It is used internally by @link(TTCPBlockSocket)!
- Here is needed code for receiving some datas by SSL connection.}
- function RecvBuffer(Buffer: TMemory; Len: Integer): Integer; virtual;
- {: Do not call this directly. It is used internally by @link(TTCPBlockSocket)!
- Here is needed code for getting count of datas what waiting for read.
- If SSL plugin not allows this, then it should return 0.}
- function WaitingData: Integer; virtual;
- {:Return string with identificator of SSL/TLS version of existing
- connection.}
- function GetSSLVersion: string; virtual;
- {:Return subject of remote SSL peer.}
- function GetPeerSubject: string; virtual;
- {:Return Serial number if remote X509 certificate.}
- function GetPeerSerialNo: integer; virtual;
- {:Return issuer certificate of remote SSL peer.}
- function GetPeerIssuer: string; virtual;
- {:Return peer name from remote side certificate. This is good for verify,
- if certificate is generated for remote side IP name.}
- function GetPeerName: string; virtual;
- {:Returns has of peer name from remote side certificate. This is good
- for fast remote side authentication.}
- function GetPeerNameHash: cardinal; virtual;
- {:Return fingerprint of remote SSL peer.}
- function GetPeerFingerprint: string; virtual;
- function GetPeerFingerprintDigest(const ADigest: string): string; virtual; abstract;
- {:Return all detailed information about certificate from remote side of
- SSL/TLS connection. Result string can be multilined! Each plugin can return
- this informations in different format!}
- function GetCertInfo: string; virtual;
- {:Return currently used Cipher.}
- function GetCipherName: string; virtual;
- {:Return currently used number of bits in current Cipher algorythm.}
- function GetCipherBits: integer; virtual;
- {:Return number of bits in current Cipher algorythm.}
- function GetCipherAlgBits: integer; virtual;
- {:Return result value of verify remote side certificate. Look to OpenSSL
- documentation for possible values. For example 0 is successfuly verified
- certificate, or 18 is self-signed certificate.}
- function GetVerifyCert: integer; virtual;
- {: Resurn @true if SSL mode is enabled on existing cvonnection.}
- property SSLEnabled: Boolean read FSSLEnabled;
- {:Return error code of last SSL operation. 0 is OK.}
- property LastError: integer read FLastError;
- {:Return error description of last SSL operation.}
- property LastErrorDesc: string read FLastErrorDesc;
- published
- {:Here you can specify requested SSL/TLS mode. Default is autodetection, but
- on some servers autodetection not working properly. In this case you must
- specify requested SSL/TLS mode by your hand!}
- property SSLType: TSSLType read FSSLType write FSSLType;
- {:Password for decrypting of encoded certificate or key.}
- property KeyPassword: string read FKeyPassword write FKeyPassword;
- {:Username for possible credentials.}
- property Username: string read FUsername write FUsername;
- {:password for possible credentials.}
- property Password: string read FPassword write FPassword;
- {:By this property you can modify default set of SSL/TLS ciphers.}
- property Ciphers: string read FCiphers write FCiphers;
- {:Used for loading certificate from disk file. See to plugin documentation
- if this method is supported and how!}
- property CertificateFile: string read FCertificateFile write FCertificateFile;
- {:Used for loading private key from disk file. See to plugin documentation
- if this method is supported and how!}
- property PrivateKeyFile: string read FPrivateKeyFile write FPrivateKeyFile;
- {:Used for loading certificate from binary string. See to plugin documentation
- if this method is supported and how!}
- property Certificate: string read FCertificate write FCertificate;
- {:Used for loading private key from binary string. See to plugin documentation
- if this method is supported and how!}
- property PrivateKey: string read FPrivateKey write FPrivateKey;
- {:Used for loading PFX from binary string. See to plugin documentation
- if this method is supported and how!}
- property PFX: string read FPFX write FPFX;
- {:Used for loading PFX from disk file. See to plugin documentation
- if this method is supported and how!}
- property PFXfile: string read FPFXfile write FPFXfile;
- {:Used for loading trusted certificates from disk file. See to plugin documentation
- if this method is supported and how!}
- property TrustCertificateFile: string read FTrustCertificateFile write FTrustCertificateFile;
- {:Used for loading trusted certificates from binary string. See to plugin documentation
- if this method is supported and how!}
- property TrustCertificate: string read FTrustCertificate write FTrustCertificate;
- {:Used for loading CA certificates from binary string. See to plugin documentation
- if this method is supported and how!}
- property CertCA: string read FCertCA write FCertCA;
- {:Used for loading CA certificates from disk file. See to plugin documentation
- if this method is supported and how!}
- property CertCAFile: string read FCertCAFile write SetCertCAFile;
- {:If @true, then is verified client certificate. (it is good for writing
- SSL/TLS servers.) When you are not server, but you are client, then if this
- property is @true, verify servers certificate.}
- property VerifyCert: Boolean read FVerifyCert write FVerifyCert;
- {:channel type for possible SSH connections}
- property SSHChannelType: string read FSSHChannelType write FSSHChannelType;
- {:First argument of channel type for possible SSH connections}
- property SSHChannelArg1: string read FSSHChannelArg1 write FSSHChannelArg1;
- {:Second argument of channel type for possible SSH connections}
- property SSHChannelArg2: string read FSSHChannelArg2 write FSSHChannelArg2;
- {: Level of standards compliance level
- (CryptLib: values in cryptlib.pas, -1: use default value ) }
- property CertComplianceLevel:integer read FCertComplianceLevel write FCertComplianceLevel;
- {:This event is called when verifying the server certificate immediatally after
- a successfull verification in the ssl library.}
- property OnVerifyCert: THookVerifyCert read FOnVerifyCert write FOnVerifyCert;
- {: Server Name Identification. Host name to send to server. If empty the host name
- found in URL will be used, which should be the normal use (http Header Host = SNI Host).
- The value is cleared after the connection is established.
- (SNI support requires OpenSSL 0.9.8k or later. Cryptlib not supported, yet ) }
- property SNIHost:string read FSNIHost write FSNIHost;
- end;
- {:@abstract(Default SSL plugin with no SSL support.)
- Dummy SSL plugin implementation for applications without SSL/TLS support.}
- TSSLNone = class (TCustomSSL)
- public
- {:See @inherited}
- function LibVersion: string; override;
- {:See @inherited}
- function LibName: string; override;
- end;
- {:@abstract(Record with definition of IP packet header.)
- For reading data from ICMP or RAW sockets.}
- TIPHeader = record
- VerLen: Byte;
- TOS: Byte;
- TotalLen: Word;
- Identifer: Word;
- FragOffsets: Word;
- TTL: Byte;
- Protocol: Byte;
- CheckSum: Word;
- SourceIp: UInt32;
- DestIp: UInt32;
- Options: UInt32;
- end;
- {:@abstract(Parent class of application protocol implementations.)
- By this class is defined common properties.}
- TSynaClient = Class(TObject)
- protected
- FTargetHost: string;
- FTargetPort: string;
- FIPInterface: string;
- FTimeout: integer;
- FUserName: string;
- FPassword: string;
- public
- constructor Create;
- published
- {:Specify terget server IP (or symbolic name). Default is 'localhost'.}
- property TargetHost: string read FTargetHost Write FTargetHost;
- {:Specify terget server port (or symbolic name).}
- property TargetPort: string read FTargetPort Write FTargetPort;
- {:Defined local socket address. (outgoing IP address). By default is used
- '0.0.0.0' as wildcard for default IP.}
- property IPInterface: string read FIPInterface Write FIPInterface;
- {:Specify default timeout for socket operations.}
- property Timeout: integer read FTimeout Write FTimeout;
- {:If protocol need user authorization, then fill here username.}
- property UserName: string read FUserName Write FUserName;
- {:If protocol need user authorization, then fill here password.}
- property Password: string read FPassword Write FPassword;
- end;
- var
- {:Selected SSL plugin. Default is @link(TSSLNone).
- Do not change this value directly!!!
- Just add your plugin unit to your project uses instead. Each plugin unit have
- initialization code what modify this variable.}
- SSLImplementation: TSSLClass = TSSLNone;
- implementation
- {$IFDEF ONCEWINSOCK}
- var
- WsaDataOnce: TWSADATA;
- e: ESynapseError;
- { ESynapseError }
- constructor ESynapseError.CreateErrorCode(AErrorCode: Integer;
- const AErrorDesc: string);
- var
- Z: string;
- begin
- Z := SysUtils.Trim(AErrorDesc);
- inherited Create(Z);
- FErrorCode := AErrorCode;
- FErrorMessage := Z;
- end;
- {$ENDIF}
- constructor TBlockSocket.Create;
- begin
- CreateAlternate('');
- end;
- constructor TBlockSocket.CreateAlternate(Stub: string);
- {$IFNDEF ONCEWINSOCK}
- var
- e: ESynapseError;
- {$ENDIF}
- begin
- inherited Create;
- //FDelayedOptions := TOptionList.Create;
- FRaiseExcept := False;
- {$IFDEF RAISEEXCEPT}
- FRaiseExcept := True;
- {$ENDIF}
- FSocket := INVALID_SOCKET;
- FBuffer := '';
- FLastCR := False;
- FLastLF := False;
- FBinded := False;
- FNonBlockMode := False;
- FMaxLineLength := 0;
- FMaxSendBandwidth := 0;
- FNextSend := 0;
- FMaxRecvBandwidth := 0;
- FNextRecv := 0;
- FConvertLineEnd := False;
- FFamily := SF_Any;
- FFamilySave := SF_Any;
- FIP6used := False;
- FPreferIP4 := True;
- FInterPacketTimeout := True;
- FRecvCounter := 0;
- FSendCounter := 0;
- FSendMaxChunk := c64k;
- FStopFlag := False;
- FNonblockSendTimeout := 15000;
- FHeartbeatRate := 0;
- FConnectionTimeout := 0;
- FOwner := nil;
- {$IFNDEF ONCEWINSOCK}
- if Stub = '' then
- Stub := DLLStackName;
- if not InitSocketInterface(Stub) then
- begin
- e := ESynapseError.Create('Error loading Socket interface (' + Stub + ')!');
- e.ErrorCode := 0;
- e.ErrorMessage := 'Error loading Socket interface (' + Stub + ')!';
- raise e;
- end;
- SockCheck(synsock.WSAStartup(WinsockLevel, FWsaDataOnce));
- ExceptCheck;
- {$ENDIF}
- end;
- destructor TBlockSocket.Destroy;
- //var
- //n: integer;
- // p: TSynaOption;
- begin
- CloseSocket;
- {$IFNDEF ONCEWINSOCK}
- synsock.WSACleanup;
- DestroySocketInterface;
- {$ENDIF}
- {for n := FDelayedOptions.Count - 1 downto 0 do
- begin
- p := TSynaOption(FDelayedOptions[n]);
- FreeAndNil(p);
- end;}
- //FreeAndNil(FDelayedOptions);
- Finalize(FDelayedOptions);
- inherited Destroy;
- end;
- function TBlockSocket.FamilyToAF(f: TSocketFamily): TAddrFamily;
- begin
- case f of
- SF_ip4:
- Result := AF_INET;
- SF_ip6:
- Result := AF_INET6;
- else
- Result := AF_UNSPEC;
- end;
- end;
- procedure TBlockSocket.SetDelayedOption(const Value: TSynaOption);
- var
- li: TLinger;
- x: integer;
- buf: TMemory;
- {$IFNDEF MSWINDOWS}
- {$IFNDEF ULTIBO}
- timeval: TTimeval;
- {$ENDIF}
- {$ENDIF}
- begin
- case value.Option of
- SOT_Linger:
- begin
- {$IFDEF CIL}
- li := TLinger.Create(Value.Enabled, Value.Value div 1000);
- synsock.SetSockOptObj(FSocket, integer(SOL_SOCKET), integer(SO_LINGER), li);
- {$ELSE}
- li.l_onoff := Ord(Value.Enabled);
- li.l_linger := Value.Value div 1000;
- buf := @li;
- SockCheck(synsock.SetSockOpt(FSocket, integer(SOL_SOCKET), integer(SO_LINGER), buf, SizeOf(li)));
- ExceptCheck;
- {$ENDIF}
- end;
- SOT_RecvBuff:
- begin
- {$IFDEF CIL}
- buf := System.BitConverter.GetBytes(value.Value);
- {$ELSE}
- buf := @Value.Value;
- {$ENDIF}
- SockCheck(synsock.SetSockOpt(FSocket, integer(SOL_SOCKET), integer(SO_RCVBUF),
- buf, SizeOf(Value.Value)));
- ExceptCheck;
- end;
- SOT_SendBuff:
- begin
- {$IFDEF CIL}
- buf := System.BitConverter.GetBytes(value.Value);
- {$ELSE}
- buf := @Value.Value;
- {$ENDIF}
- SockCheck(synsock.SetSockOpt(FSocket, integer(SOL_SOCKET), integer(SO_SNDBUF),
- buf, SizeOf(Value.Value)));
- ExceptCheck;
- end;
- SOT_NonBlock:
- begin
- FNonBlockMode := Value.Enabled;
- x := Ord(FNonBlockMode);
- SockCheck(synsock.IoctlSocket(FSocket, FIONBIO, x));
- ExceptCheck;
- end;
- SOT_RecvTimeout:
- begin
- {$IFDEF CIL}
- buf := System.BitConverter.GetBytes(value.Value);
- synsock.SetSockOpt(FSocket, integer(SOL_SOCKET), integer(SO_RCVTIMEO),
- buf, SizeOf(Value.Value));
- {$ELSE}
- {$IFDEF MSWINDOWS}
- buf := @Value.Value;
- SockCheck(synsock.SetSockOpt(FSocket, integer(SOL_SOCKET), integer(SO_RCVTIMEO),
- buf, SizeOf(Value.Value)));
- ExceptCheck;
- {$ELSE}
- {$IFDEF ULTIBO}
- buf := @Value.Value;
- synsock.SetSockOpt(FSocket, integer(SOL_SOCKET), integer(SO_RCVTIMEO),
- buf, SizeOf(Value.Value));
- {$ELSE}
- timeval.tv_sec:=Value.Value div 1000;
- timeval.tv_usec:=(Value.Value mod 1000) * 1000;
- synsock.SetSockOpt(FSocket, integer(SOL_SOCKET), integer(SO_RCVTIMEO),
- @timeval, SizeOf(timeval));
- {$ENDIF}
- {$ENDIF}
- {$ENDIF}
- end;
- SOT_SendTimeout:
- begin
- {$IFDEF CIL}
- buf := System.BitConverter.GetBytes(value.Value);
- {$ELSE}
- {$IFDEF MSWINDOWS}
- buf := @Value.Value;
- SockCheck(synsock.SetSockOpt(FSocket, integer(SOL_SOCKET), integer(SO_SNDTIMEO),
- buf, SizeOf(Value.Value)));
- ExceptCheck;
- {$ELSE}
- {$IFDEF ULTIBO}
- buf := @Value.Value;
- synsock.SetSockOpt(FSocket, integer(SOL_SOCKET), integer(SO_SNDTIMEO),
- buf, SizeOf(Value.Value));
- {$ELSE}
- timeval.tv_sec:=Value.Value div 1000;
- timeval.tv_usec:=(Value.Value mod 1000) * 1000;
- synsock.SetSockOpt(FSocket, integer(SOL_SOCKET), integer(SO_SNDTIMEO),
- @timeval, SizeOf(timeval));
- {$ENDIF}
- {$ENDIF}
- {$ENDIF}
- end;
- SOT_Reuse:
- begin
- x := Ord(Value.Enabled);
- {$IFDEF CIL}
- buf := System.BitConverter.GetBytes(x);
- {$ELSE}
- buf := @x;
- {$ENDIF}
- SockCheck(synsock.SetSockOpt(FSocket, integer(SOL_SOCKET), integer(SO_REUSEADDR), buf, SizeOf(x)));
- ExceptCheck;
- end;
- SOT_TTL:
- begin
- {$IFDEF CIL}
- buf := System.BitConverter.GetBytes(value.Value);
- {$ELSE}
- buf := @Value.Value;
- {$ENDIF}
- if FIP6Used then
- SockCheck(synsock.SetSockOpt(FSocket, integer(IPPROTO_IPV6), integer(IPV6_UNICAST_HOPS),
- buf, SizeOf(Value.Value)))
- else
- SockCheck(synsock.SetSockOpt(FSocket, integer(IPPROTO_IP), integer(IP_TTL),
- buf, SizeOf(Value.Value)));
- ExceptCheck;
- end;
- SOT_Broadcast:
- begin
- //#todo1 broadcasty na IP6
- x := Ord(Value.Enabled);
- {$IFDEF CIL}
- buf := System.BitConverter.GetBytes(x);
- {$ELSE}
- buf := @x;
- {$ENDIF}
- SockCheck(synsock.SetSockOpt(FSocket, integer(SOL_SOCKET), integer(SO_BROADCAST), buf, SizeOf(x)));
- ExceptCheck;
- end;
- SOT_MulticastTTL:
- begin
- {$IFDEF CIL}
- buf := System.BitConverter.GetBytes(value.Value);
- {$ELSE}
- buf := @Value.Value;
- {$ENDIF}
- if FIP6Used then
- SockCheck(synsock.SetSockOpt(FSocket, integer(IPPROTO_IPV6), integer(IPV6_MULTICAST_HOPS),
- buf, SizeOf(Value.Value)))
- else
- SockCheck(synsock.SetSockOpt(FSocket, integer(IPPROTO_IP), integer(IP_MULTICAST_TTL),
- buf, SizeOf(Value.Value)));
- ExceptCheck;
- end;
- SOT_MulticastLoop:
- begin
- x := Ord(Value.Enabled);
- {$IFDEF CIL}
- buf := System.BitConverter.GetBytes(x);
- {$ELSE}
- buf := @x;
- {$ENDIF}
- if FIP6Used then
- SockCheck(synsock.SetSockOpt(FSocket, integer(IPPROTO_IPV6), integer(IPV6_MULTICAST_LOOP), buf, SizeOf(x)))
- else
- SockCheck(synsock.SetSockOpt(FSocket, integer(IPPROTO_IP), integer(IP_MULTICAST_LOOP), buf, SizeOf(x)));
- ExceptCheck;
- end;
- SOT_NoDelay:
- begin
- {$IFDEF CIL}
- buf := System.BitConverter.GetBytes(x);
- {$ELSE}
- buf := @x;
- {$ENDIF}
- x := Ord(Value.Enabled);
- SockCheck(synsock.SetSockOpt(FSocket, integer(IPPROTO_IP), integer(TCP_NODELAY), buf, SizeOf(x)));
- ExceptCheck;
- end;
- end;
- //FreeAndNil(Value);
- end;
- procedure TBlockSocket.DelayedOption(const Value: TSynaOption);
- begin
- if FSocket = INVALID_SOCKET then
- begin
- SetLength(FDelayedOptions, Length(FDelayedOptions) + 1);
- FDelayedOptions[High(FDelayedOptions)] := Value;
- end
- else
- SetDelayedOption(Value);
- end;
- procedure TBlockSocket.ProcessDelayedOptions;
- var
- n: integer;
- d: TSynaOption;
- begin
- for n := 0 to High(FDelayedOptions) do
- begin
- d := TSynaOption(FDelayedOptions[n]);
- SetDelayedOption(d);
- end;
- Finalize(FDelayedOptions);
- end;
- procedure TBlockSocket.SetSin(var Sin: TVarSin; const IP, Port: string);
- var
- f: TSocketFamily;
- begin
- DoStatus(HR_ResolvingBegin, IP + ':' + Port);
- ResetLastError;
- //if socket exists, then use their type, else use users selection
- f := SF_Any;
- if (FSocket = INVALID_SOCKET) and (FFamily = SF_any) then
- begin
- if IsIP(IP) then
- f := SF_IP4
- else
- if IsIP6(IP) then
- f := SF_IP6;
- end
- else
- f := FFamily;
- FLastError := synsock.SetVarSin(sin, ip, port, FamilyToAF(f),
- GetSocketprotocol, GetSocketType, FPreferIP4);
- DoStatus(HR_ResolvingEnd, GetSinIP(sin) + ':' + IntTostr(GetSinPort(sin)));
- end;
- function TBlockSocket.GetSendTimeout: Integer;
- var
- l: integer;
- begin
- l:=SizeOf(Integer);
- SockCheck(synsock.GetSockOpt(FSocket, SOL_SOCKET, SO_SNDTIMEO, @Result, l));
- ExceptCheck;
- end;
- function TBlockSocket.GetSinIP(Sin: TVarSin): string;
- begin
- Result := synsock.GetSinIP(sin);
- end;
- function TBlockSocket.GetSinPort(Sin: TVarSin): Integer;
- begin
- Result := synsock.GetSinPort(sin);
- end;
- procedure TBlockSocket.CreateSocket;
- var
- sin: TVarSin;
- begin
- //dummy for SF_Any Family mode
- ResetLastError;
- if (FFamily <> SF_Any) and (FSocket = INVALID_SOCKET) then
- begin
- {$IFDEF CIL}
- if FFamily = SF_IP6 then
- sin := TVarSin.Create(IPAddress.Parse('::0'), 0)
- else
- sin := TVarSin.Create(IPAddress.Parse('0.0.0.0'), 0);
- {$ELSE}
- FillChar(Sin, Sizeof(Sin), 0);
- if FFamily = SF_IP6 then
- sin.sin_family := AF_INET6
- else
- sin.sin_family := AF_INET;
- {$ENDIF}
- InternalCreateSocket(Sin);
- end;
- end;
- procedure TBlockSocket.CreateSocketByName(const Value: string);
- var
- sin: TVarSin;
- begin
- ResetLastError;
- if FSocket = INVALID_SOCKET then
- begin
- SetSin(sin, value, '0');
- if FLastError = 0 then
- InternalCreateSocket(Sin);
- end;
- end;
- procedure TBlockSocket.InternalCreateSocket(Sin: TVarSin);
- begin
- FStopFlag := False;
- FRecvCounter := 0;
- FSendCounter := 0;
- ResetLastError;
- if FSocket = INVALID_SOCKET then
- begin
- FBuffer := '';
- FBinded := False;
- FIP6Used := Sin.AddressFamily = AF_INET6;
- FSocket := synsock.Socket(integer(Sin.AddressFamily), GetSocketType, GetSocketProtocol);
- if FSocket = INVALID_SOCKET then
- FLastError := synsock.WSAGetLastError;
- {$IFNDEF CIL}
- FD_ZERO(FFDSet);
- FD_SET(FSocket, FFDSet);
- {$ENDIF}
- ExceptCheck;
- if FIP6used then
- DoStatus(HR_SocketCreate, 'IPv6')
- else
- DoStatus(HR_SocketCreate, 'IPv4');
- ProcessDelayedOptions;
- DoCreateSocket;
- end;
- end;
- procedure TBlockSocket.CloseSocket;
- begin
- AbortSocket;
- end;
- procedure TBlockSocket.AbortSocket;
- //var
- // n: integer;
- // p: TSynaOption;
- begin
- if FSocket <> INVALID_SOCKET then
- synsock.CloseSocket(FSocket);
- FSocket := INVALID_SOCKET;
- {for n := FDelayedOptions.Count - 1 downto 0 do
- begin
- p := TSynaOption(FDelayedOptions[n]);
- FreeAndNil(p);
- end;
- FDelayedOptions.Clear;}
- Finalize(FDelayedOptions);
- FFamily := FFamilySave;
- DoStatus(HR_SocketClose, '');
- FDisconnected := False;
- end;
- procedure TBlockSocket.Bind(const IP, Port: string);
- var
- Sin: TVarSin;
- begin
- ResetLastError;
- if (FSocket <> INVALID_SOCKET)
- or not((FFamily = SF_ANY) and (IP = cAnyHost) and (Port = cAnyPort)) then
- begin
- SetSin(Sin, IP, Port);
- if FLastError = 0 then
- begin
- if FSocket = INVALID_SOCKET then
- InternalCreateSocket(Sin);
- SockCheck(synsock.Bind(FSocket, Sin));
- GetSinLocal;
- FBuffer := '';
- FBinded := True;
- end;
- ExceptCheck;
- DoStatus(HR_Bind, IP + ':' + Port);
- end;
- end;
- procedure TBlockSocket.Connect(const IP, Port: string);
- var
- Sin: TVarSin;
- b: boolean;
- {$IFDEF MSWINDOWS}
- lError: Integer;
- {$ENDIF}
- begin
- SetSin(Sin, IP, Port);
- if FLastError = 0 then
- begin
- if FSocket = INVALID_SOCKET then
- InternalCreateSocket(Sin);
- if FConnectionTimeout > 0 then
- begin
- // connect in non-blocking mode
- b := NonBlockMode;
- NonBlockMode := true;
- SockCheck(synsock.Connect(FSocket, Sin));
- if (FLastError = WSAEINPROGRESS) OR (FLastError = WSAEWOULDBLOCK) then
- if not CanWrite(FConnectionTimeout) then
- FLastError := WSAETIMEDOUT;
- {$IFDEF MSWINDOWS}
- lError := FLastError;
- {$ENDIF}
- NonBlockMode := b;
- {$IFDEF MSWINDOWS}
- FLastError := lError;
- {$ENDIF}
- end
- else
- SockCheck(synsock.Connect(FSocket, Sin));
- if FLastError = 0 then
- GetSins;
- FBuffer := '';
- FLastCR := False;
- FLastLF := False;
- end;
- ExceptCheck;
- DoStatus(HR_Connect, IP + ':' + Port);
- end;
- procedure TBlockSocket.Listen;
- begin
- SockCheck(synsock.Listen(FSocket, SOMAXCONN));
- GetSins;
- ExceptCheck;
- DoStatus(HR_Listen, '');
- end;
- function TBlockSocket.Accept: TSocket;
- begin
- Result := synsock.Accept(FSocket, FRemoteSin);
- /// SockCheck(Result);
- ExceptCheck;
- DoStatus(HR_Accept, '');
- end;
- procedure TBlockSocket.GetSinLocal;
- begin
- synsock.GetSockName(FSocket, FLocalSin);
- end;
- procedure TBlockSocket.GetSinRemote;
- begin
- synsock.GetPeerName(FSocket, FRemoteSin);
- end;
- procedure TBlockSocket.GetSins;
- begin
- GetSinLocal;
- GetSinRemote;
- end;
- procedure TBlockSocket.SetBandwidth(Value: Integer);
- begin
- MaxSendBandwidth := Value;
- MaxRecvBandwidth := Value;
- end;
- procedure TBlockSocket.LimitBandwidth(Length: Integer; MaxB: integer; var Next: UInt32);
- var
- x: UInt32;
- y: UInt32;
- n: integer;
- begin
- if FStopFlag then
- exit;
- if MaxB > 0 then
- begin
- y := GetTick;
- if Next > y then
- begin
- x := Next - y;
- if x > 0 then
- begin
- DoStatus(HR_Wait, IntToStr(x));
- sleep(x mod 250);
- for n := 1 to x div 250 do
- if FStopFlag then
- Break
- else
- sleep(250);
- end;
- end;
- Next := GetTick + Trunc((Length / MaxB) * 1000);
- end;
- end;
- function TBlockSocket.TestStopFlag: Boolean;
- begin
- DoHeartbeat;
- Result := FStopFlag;
- if Result then
- begin
- FStopFlag := False;
- FLastError := WSAECONNABORTED;
- ExceptCheck;
- end;
- end;
- function TBlockSocket.SendBuffer(const Buffer: TMemory; Length: Integer): Integer;
- {$IFNDEF CIL}
- var
- x, y: integer;
- l, r: integer;
- p: Pointer;
- {$ENDIF}
- begin
- Result := 0;
- if TestStopFlag then
- Exit;
- DoMonitor(True, Buffer, Length);
- {$IFDEF CIL}
- Result := synsock.Send(FSocket, Buffer, Length, 0);
- {$ELSE}
- l := Length;
- x := 0;
- while x < l do
- begin
- y := l - x;
- if y > FSendMaxChunk then
- y := FSendMaxChunk;
- if y > 0 then
- begin
- LimitBandwidth(y, FMaxSendBandwidth, FNextsend);
- p := IncPoint(Buffer, x);
- r := synsock.Send(FSocket, p, y, MSG_NOSIGNAL);
- SockCheck(r);
- if FLastError = WSAEWOULDBLOCK then
- begin
- if CanWrite(FNonblockSendTimeout) then
- begin
- r := synsock.Send(FSocket, p, y, MSG_NOSIGNAL);
- SockCheck(r);
- end
- else
- FLastError := WSAETIMEDOUT;
- end;
- if FLastError <> 0 then
- Break;
- Inc(x, r);
- Inc(Result, r);
- Inc(FSendCounter, r);
- DoStatus(HR_WriteCount, IntToStr(r));
- end
- else
- break;
- end;
- {$ENDIF}
- ExceptCheck;
- end;
- procedure TBlockSocket.SendByte(Data: Byte);
- {$IFDEF CIL}
- var
- buf: TMemory;
- {$ENDIF}
- begin
- {$IFDEF CIL}
- setlength(buf, 1);
- buf[0] := Data;
- SendBuffer(buf, 1);
- {$ELSE}
- SendBuffer(@Data, 1);
- {$ENDIF}
- end;
- procedure TBlockSocket.SendString(Data: TSynaBytes);
- var
- buf: TMemory;
- count: Integer;
- begin
- {$IFDEF CIL}
- buf := BytesOf(Data);
- count := Length(Data);
- {$ELSE}
- {$IFDEF UNICODE}
- buf := TSynaBytes(Data).Data; //TSynaByte(Data)
- count := Data.Length; // avoid conversion
- {$ELSE}
- buf := Pointer(data);
- count := Length(Data);
- {$ENDIF}
- {$ENDIF}
- SendBuffer(buf, count);
- end;
- procedure TBlockSocket.SendInteger(Data: integer);
- var
- buf: TMemory;
- begin
- {$IFDEF CIL}
- buf := System.BitConverter.GetBytes(Data);
- {$ELSE}
- buf := @Data;
- {$ENDIF}
- SendBuffer(buf, SizeOf(Data));
- end;
- procedure TBlockSocket.SendBlock(const Data: string);
- var
- i: integer;
- begin
- i := SwapBytes(Length(data));
- SendString(Codelongint(i) + Data);
- end;
- procedure TBlockSocket.InternalSendStream(const Stream: TStream; WithSize, Indy: boolean);
- var
- l: int64;
- yr: integer;
- s: string;
- b: boolean;
- {$IFDEF CIL}
- buf: TMemory;
- {$ENDIF}
- begin
- b := true;
- l := 0;
- if WithSize then
- begin
- l := Stream.Size - Stream.Position;;
- if not Indy then
- l := synsock.HToNL(l);
- end;
- repeat
- {$IFDEF CIL}
- Setlength(buf, FSendMaxChunk);
- yr := Stream.read(buf, FSendMaxChunk);
- if yr > 0 then
- begin
- if WithSize and b then
- begin
- b := false;
- SendString(CodeLongInt(l));
- end;
- SendBuffer(buf, yr);
- if FLastError <> 0 then
- break;
- end
- {$ELSE}
- Setlength(s, FSendMaxChunk);
- yr := Stream.read(Pointer(s)^, FSendMaxChunk);
- if yr > 0 then
- begin
- SetLength(s, yr);
- if WithSize and b then
- begin
- b := false;
- SendString(CodeLongInt(l) + s);
- end
- else
- SendString(s);
- if FLastError <> 0 then
- break;
- end
- {$ENDIF}
- until yr <= 0;
- end;
- procedure TBlockSocket.SendStreamRaw(const Stream: TStream);
- begin
- InternalSendStream(Stream, false, false);
- end;
- procedure TBlockSocket.SendStreamIndy(const Stream: TStream);
- begin
- InternalSendStream(Stream, true, true);
- end;
- procedure TBlockSocket.SendStream(const Stream: TStream);
- begin
- InternalSendStream(Stream, true, false);
- end;
- function TBlockSocket.RecvBuffer(Buffer: TMemory; Length: Integer): Integer;
- begin
- Result := 0;
- if TestStopFlag then
- Exit;
- LimitBandwidth(Length, FMaxRecvBandwidth, FNextRecv);
- Result := synsock.Recv(FSocket, Buffer, Length, MSG_NOSIGNAL);
- if Result = 0 then
- FLastError := WSAECONNRESET
- else
- SockCheck(Result);
- ExceptCheck;
- if Result > 0 then
- begin
- Inc(FRecvCounter, Result);
- DoStatus(HR_ReadCount, IntToStr(Result));
- DoMonitor(False, Buffer, Result);
- DoReadFilter(Buffer, Result);
- end;
- end;
- function TBlockSocket.RecvBufferEx(Buffer: TMemory; Len: Integer;
- Timeout: Integer): Integer;
- var
- s: TSynaBytes;
- rl, l: integer;
- ti: UInt32;
- {$IFDEF CIL}
- n: integer;
- b: TMemory;
- {$ENDIF}
- begin
- ResetLastError;
- Result := 0;
- if Len > 0 then
- begin
- rl := 0;
- repeat
- ti := GetTick;
- s := RecvPacket(Timeout);
- l := s.length;
- if (rl + l) > Len then
- l := Len - rl;
- {$IFDEF CIL}
- b := BytesOf(s);
- for n := 0 to l do
- Buffer[rl + n] := b[n];
- {$ELSE}
- Move({$IFNDEF UNICODE}Pointer(s)^{$ELSE}s.Bytes[0]{$ENDIF},
- IncPoint(Buffer, rl)^, l);
- {$ENDIF}
- rl := rl + l;
- if FLastError <> 0 then
- Break;
- if rl >= Len then
- Break;
- if not FInterPacketTimeout then
- begin
- Timeout := Timeout - integer(TickDelta(ti, GetTick));
- if Timeout <= 0 then
- begin
- FLastError := WSAETIMEDOUT;
- Break;
- end;
- end;
- until False;
- DeleteInternal(s, 1, l);
- FBuffer := s;
- Result := rl;
- end;
- end;
- function TBlockSocket.RecvBufferStr(Len: Integer; Timeout: Integer): TSynaBytes;
- var
- x: integer;
- buf: TBytes;
- begin
- Result := '';
- if Len > 0 then
- begin
- Setlength(Buf, Len);
- x := RecvBufferEx(@buf[0], Len, Timeout);
- if FLastError = 0 then
- begin
- SetLength(Buf, x);
- {$IFDEF UNICODE}
- Result.Length := x;
- Move(Buf[0], Result.Bytes[0], x);
- {$ELSE}
- Result := StringOf(Buf);
- {$ENDIF}
- end
- else
- Result := '';
- Setlength(Buf, 0);
- end;
- end;
- function TBlockSocket.RecvPacket(Timeout: Integer): TSynaBytes;
- var
- x: Integer;
- buf: TBytes;
- begin
- Result := '';
- ResetLastError;
- if FBuffer <> '' then
- begin
- Result := FBuffer;
- FBuffer := '';
- end
- else
- begin
- {$IFDEF MSWINDOWS}
- //not drain CPU on large downloads...
- Sleep(0);
- {$ENDIF}
- x := WaitingData;
- if x > 0 then
- begin
- SetLength(Buf, x);
- x := RecvBuffer(Buf, x);
- if x >= 0 then
- begin
- SetLength(Buf, x);
- {$IFDEF UNICODE} Result := TSynaBytes(buf); {$ELSE} Result := StringOf(buf); {$ENDIF}
- end;
- end
- else
- begin
- if CanRead(Timeout) then
- begin
- x := WaitingData;
- if x = 0 then
- FLastError := WSAECONNRESET;
- if x > 0 then
- begin
- SetLength(Buf, x);
- x := RecvBuffer(Buf, x);
- if x >= 0 then
- begin
- SetLength(Buf, x);
- {$IFDEF UNICODE} Result := TSynaBytes(buf); {$ELSE} Result := StringOf(buf); {$ENDIF}
- end;
- SetLength(Buf, 0);
- end;
- end
- else
- FLastError := WSAETIMEDOUT;
- end;
- end;
- if FConvertLineEnd and (Result <> '') then
- begin
- if FLastCR and (Result[1] = LF) then
- DeleteInternal(Result, 1, 1);
- if FLastLF and (Result[1] = CR) then
- DeleteInternal(Result, 1, 1);
- FLastCR := False;
- FLastLF := False;
- end;
- ExceptCheck;
- end;
- function TBlockSocket.RecvByte(Timeout: Integer): Byte;
- begin
- Result := 0;
- ResetLastError;
- if FBuffer = '' then
- FBuffer := RecvPacket(Timeout);
- if (FLastError = 0) and (FBuffer <> '') then
- begin
- Result := Ord(FBuffer[1]);
- {$IFNDEF UNICODE}
- Delete(FBuffer, 1, 1);
- {$ELSE}
- FBuffer.Delete(1, 1); // TEST!
- {$ENDIF}
- end;
- ExceptCheck;
- end;
- function TBlockSocket.RecvInteger(Timeout: Integer): Integer;
- var
- s: string;
- begin
- Result := 0;
- s := RecvBufferStr(4, Timeout);
- if FLastError = 0 then
- Result := (ord(s[1]) + ord(s[2]) * 256) + (ord(s[3]) + ord(s[4]) * 256) * 65536;
- end;
- function TBlockSocket.RecvTerminated(Timeout: Integer; const Terminator: string): string;
- var
- x: Integer;
- s: TSynaBytes;
- l: Integer;
- CorCRLF: Boolean;
- t: string;
- tl: integer;
- ti: UInt32;
- begin
- ResetLastError;
- Result := '';
- l := Length(Terminator);
- if l = 0 then
- Exit;
- tl := l;
- CorCRLF := FConvertLineEnd and (Terminator = CRLF);
- s := '';
- x := 0;
- repeat
- //get rest of FBuffer or incomming new data...
- ti := GetTick;
- s := s + RecvPacket(Timeout);
- if FLastError <> 0 then
- Break;
- x := 0;
- if s.Length > 0 then
- if CorCRLF then
- begin
- t := '';
- x := PosCRLF(s, t);
- tl := t.Length;
- if t = CR then
- FLastCR := True;
- if t = LF then
- FLastLF := True;
- end
- else
- begin
- x := pos(Terminator, s);
- tl := l;
- end;
- if (FMaxLineLength <> 0) and (s.Length > FMaxLineLength) then
- begin
- FLastError := WSAENOBUFS;
- Break;
- end;
- if x > 0 then
- Break;
- if not FInterPacketTimeout then
- begin
- Timeout := Timeout - integer(TickDelta(ti, GetTick));
- if Timeout <= 0 then
- begin
- FLastError := WSAETIMEDOUT;
- Break;
- end;
- end;
- until False;
- if x > 0 then
- begin
- Result := Copy(s, 1, x - 1);
- DeleteInternal(s, 1, x + tl - 1);
- end;
- FBuffer := s;
- ExceptCheck;
- end;
- function TBlockSocket.RecvString(Timeout: Integer): string;
- var
- s: string;
- begin
- Result := '';
- s := RecvTerminated(Timeout, CRLF);
- if FLastError = 0 then
- Result := s;
- end;
- function TBlockSocket.RecvBlock(Timeout: Integer): string;
- var
- x: integer;
- begin
- Result := '';
- x := RecvInteger(Timeout);
- if FLastError = 0 then
- Result := RecvBufferStr(x, Timeout);
- end;
- procedure TBlockSocket.RecvStreamRaw(const Stream: TStream; Timeout: Integer);
- var
- s: string;
- begin
- repeat
- s := RecvPacket(Timeout);
- if FLastError = 0 then
- WriteStrToStream(Stream, s);
- until FLastError <> 0;
- end;
- procedure TBlockSocket.RecvStreamSize(const Stream: TStream; Timeout: Integer; Size: int64);
- var
- s: TSynaBytes;
- n: int64;
- {$IFDEF CIL}
- buf: TMemory;
- {$ENDIF}
- begin
- n := Size div int64(FSendMaxChunk);
- while n > 0 do
- begin
- {$IFDEF CIL}
- SetLength(buf, FSendMaxChunk);
- RecvBufferEx(buf, FSendMaxChunk, Timeout);
- if FLastError <> 0 then
- Exit;
- Stream.Write(buf, FSendMaxChunk);
- {$ELSE}
- s := RecvBufferStr(FSendMaxChunk, Timeout);
- if FLastError <> 0 then
- Exit;
- WriteStrToStream(Stream, s);
- {$ENDIF}
- dec(n);
- end;
- n := Size mod int64(FSendMaxChunk);
- if n > 0 then
- begin
- {$IFDEF CIL}
- SetLength(buf, n);
- RecvBufferEx(buf, n, Timeout);
- if FLastError <> 0 then
- Exit;
- Stream.Write(buf, n);
- {$ELSE}
- s := RecvBufferStr(n, Timeout);
- if FLastError <> 0 then
- Exit;
- WriteStrToStream(Stream, s);
- {$ENDIF}
- end;
- end;
- procedure TBlockSocket.RecvStreamIndy(const Stream: TStream; Timeout: Integer);
- var
- x: integer;
- begin
- x := RecvInteger(Timeout);
- x := synsock.NToHL(x);
- if FLastError = 0 then
- RecvStreamSize(Stream, Timeout, x);
- end;
- procedure TBlockSocket.RecvStream(const Stream: TStream; Timeout: Integer);
- var
- x: integer;
- begin
- x := RecvInteger(Timeout);
- if FLastError = 0 then
- RecvStreamSize(Stream, Timeout, x);
- end;
- function TBlockSocket.PeekBuffer(Buffer: TMemory; Length: Integer): Integer;
- begin
- {$IFNDEF CIL}
- // Result := synsock.Recv(FSocket, Buffer^, Length, MSG_PEEK + MSG_NOSIGNAL);
- Result := synsock.Recv(FSocket, Buffer, Length, MSG_PEEK + MSG_NOSIGNAL);
- SockCheck(Result);
- ExceptCheck;
- {$ENDIF}
- end;
- function TBlockSocket.PeekByte(Timeout: Integer): Byte;
- var
- s: string;
- begin
- {$IFNDEF CIL}
- Result := 0;
- if CanRead(Timeout) then
- begin
- SetLength(s, 1);
- PeekBuffer(Pointer(s), 1);
- if s <> '' then
- Result := Ord(s[1]);
- end
- else
- FLastError := WSAETIMEDOUT;
- ExceptCheck;
- {$ENDIF}
- end;
- procedure TBlockSocket.ResetLastError;
- begin
- FLastError := 0;
- FLastErrorDesc := '';
- end;
- function TBlockSocket.SockCheck(SockResult: Integer): Integer;
- begin
- ResetLastError;
- if SockResult = integer(SOCKET_ERROR) then
- begin
- FLastError := synsock.WSAGetLastError;
- if FLastError <= WSABASEERR then
- Inc(FLastError, WSABASEERR);
- FLastErrorDesc := GetErrorDescEx;
- end;
- Result := FLastError;
- end;
- procedure TBlockSocket.ExceptCheck;
- var
- e: ESynapseError;
- begin
- FLastErrorDesc := GetErrorDescEx;
- if (LastError <> 0) and (LastError <> WSAEINPROGRESS)
- and (LastError <> WSAEWOULDBLOCK) then
- begin
- DoStatus(HR_Error, IntToStr(FLastError) + ',' + FLastErrorDesc);
- FDisconnected := True;
- if FRaiseExcept then
- begin
- if FLastError = 104 then
- e := EResetByPeer.Create(Format('Synapse TCP/IP socket error. Reset by peer %d: %s',
- [FLastError, FLastErrorDesc]))
- else if FLastError = 10098 then
- e := ECouldNotBindSocket.Create(Format('Synapse TCP/IP socket error. Could not bind socket %d: %s',
- [FLastError, FLastErrorDesc]))
- else if FLastError = 10054 then
- e := EConnectionResetByPeer.Create(Format('Synapse TCP/IP socket error. Connection reset by peer %d: %s',
- [FLastError, FLastErrorDesc]))
- else if FLastError = 10057 then
- e := ESockectIsnotConnected.Create(Format('Synapse TCP/IP socket error. Socket is not connected %d: %s',
- [FLastError, FLastErrorDesc]))
- else if FLastError = 10060 then
- e := EConnectionTimedOut.Create(Format('Synapse TCP/IP socket error. Connection timed out %d: %s',
- [FLastError, FLastErrorDesc]))
- else if FLastError = 10061 then
- e := EConnectionRefused.Create(Format('Synapse TCP/IP socket error. Connection refused %d: %s',
- [FLastError, FLastErrorDesc]))
- else if FLastError = 10049 then
- e := ECantAssignAddress.Create(Format('Synapse TCP/IP socket error. Can''t assign requested address %d: %s',
- [FLastError, FLastErrorDesc]))
- else if FLastError = -2 then
- e := ESocketMinus2.Create(Format('Synapse TCP/IP socket error %d: %s',
- [FLastError, FLastErrorDesc]))
- else e := ESynapseError.Create(Format('Synapse TCP/IP socket error %d: %s',
- [FLastError, FLastErrorDesc]));
- e.ErrorCode := FLastError;
- e.ErrorMessage := FLastErrorDesc;
- raise e;
- end;
- end;
- end;
- function TBlockSocket.WaitingData: Integer;
- var
- x: Integer;
- begin
- Result := 0;
- if synsock.IoctlSocket(FSocket, FIONREAD, x) = 0 then
- Result := x;
- if Result > c64k then
- Result := c64k;
- end;
- function TBlockSocket.WaitingDataEx: Integer;
- begin
- if FBuffer <> '' then
- Result := FBuffer.Length
- else
- Result := WaitingData;
- end;
- procedure TBlockSocket.Purge;
- begin
- Sleep(1);
- try
- while (FBuffer.Length > 0) or (WaitingData > 0) do
- begin
- RecvPacket(0);
- if FLastError <> 0 then
- break;
- end;
- except
- on exception do;
- end;
- ResetLastError;
- end;
- procedure TBlockSocket.SetLinger(Enable: Boolean; Linger: Integer);
- var
- d: TSynaOption;
- begin
- //d := TSynaOption.Create;
- d.Option := SOT_Linger;
- d.Enabled := Enable;
- d.Value := Linger;
- DelayedOption(d);
- end;
- function TBlockSocket.LocalName: string;
- begin
- Result := synsock.GetHostName;
- if Result = '' then
- Result := '127.0.0.1';
- end;
- procedure TBlockSocket.ResolveNameToIP(const Name: string; const IPList:
- TStrings);
- begin
- IPList.Clear;
- synsock.ResolveNameToIP(Name, FamilyToAF(FFamily), GetSocketprotocol, GetSocketType, IPList);
- if IPList.Count = 0 then
- IPList.Add(cAnyHost);
- end;
- function TBlockSocket.ResolveName(const Name: string): string;
- var
- l: TStringList;
- begin
- l := TStringList.Create;
- try
- ResolveNameToIP(Name, l);
- Result := l[0];
- finally
- FreeAndNil(l);
- end;
- end;
- function TBlockSocket.ResolvePort(const Port: string): Word;
- begin
- Result := synsock.ResolvePort(Port, FamilyToAF(FFamily), GetSocketProtocol, GetSocketType);
- end;
- function TBlockSocket.ResolveIPToName(IP: string): string;
- begin
- if not IsIP(IP) and not IsIp6(IP) then
- IP := ResolveName(IP);
- Result := synsock.ResolveIPToName(IP, FamilyToAF(FFamily), GetSocketProtocol, GetSocketType);
- end;
- procedure TBlockSocket.SetRemoteSin(const IP, Port: string);
- begin
- SetSin(FRemoteSin, IP, Port);
- end;
- function TBlockSocket.GetLocalSinIP: string;
- begin
- Result := GetSinIP(FLocalSin);
- end;
- function TBlockSocket.GetRecvTimeout: integer;
- var
- l: integer;
- begin
- l:=SizeOf(Integer);
- SockCheck(synsock.GetSockOpt(FSocket, SOL_SOCKET, SO_RCVTIMEO, @Result, l));
- ExceptCheck;
- end;
- function TBlockSocket.GetRemoteSinIP: string;
- begin
- Result := GetSinIP(FRemoteSin);
- end;
- function TBlockSocket.GetLocalSinPort: Integer;
- begin
- Result := GetSinPort(FLocalSin);
- end;
- function TBlockSocket.GetRemoteSinPort: Integer;
- begin
- Result := GetSinPort(FRemoteSin);
- end;
- function TBlockSocket.InternalCanRead(Timeout: Integer): Boolean;
- {$IFDEF CIL}
- begin
- Result := FSocket.Poll(Timeout * 1000, SelectMode.SelectRead);
- {$ELSE}
- var
- TimeVal: PTimeVal;
- TimeV: TTimeVal;
- x: Integer;
- FDSet: TFDSet;
- begin
- TimeV.tv_usec := (Timeout mod 1000) * 1000;
- TimeV.tv_sec := Timeout div 1000;
- TimeVal := @TimeV;
- if Timeout = -1 then
- TimeVal := nil;
- FDSet := FFdSet;
- x := synsock.Select(integer(FSocket + 1), @FDSet, nil, nil, TimeVal);
- SockCheck(x);
- if FLastError <> 0 then
- x := 0;
- Result := x > 0;
- {$ENDIF}
- end;
- function TBlockSocket.CanRead(Timeout: Integer): Boolean;
- var
- ti, tr: Integer;
- n: integer;
- begin
- if (FHeartbeatRate <> 0) and (Timeout <> -1) then
- begin
- ti := Timeout div FHeartbeatRate;
- tr := Timeout mod FHeartbeatRate;
- end
- else
- begin
- ti := 0;
- tr := Timeout;
- end;
- Result := InternalCanRead(tr);
- if not Result then
- for n := 0 to ti do
- begin
- DoHeartbeat;
- if FStopFlag then
- begin
- Result := False;
- FStopFlag := False;
- Break;
- end;
- Result := InternalCanRead(FHeartbeatRate);
- if Result then
- break;
- end;
- ExceptCheck;
- if Result then
- DoStatus(HR_CanRead, '');
- end;
- function TBlockSocket.InternalCanWrite(Timeout: Integer): Boolean;
- {$IFDEF CIL}
- begin
- Result := FSocket.Poll(Timeout * 1000, SelectMode.SelectWrite);
- {$ELSE}
- var
- TimeVal: PTimeVal;
- TimeV: TTimeVal;
- x: Integer;
- FDSet: TFDSet;
- begin
- TimeV.tv_usec := (Timeout mod 1000) * 1000;
- TimeV.tv_sec := Timeout div 1000;
- TimeVal := @TimeV;
- if Timeout = -1 then
- TimeVal := nil;
- FDSet := FFdSet;
- x := synsock.Select(FSocket + 1, nil, @FDSet, nil, TimeVal);
- SockCheck(x);
- if FLastError <> 0 then
- x := 0;
- Result := x > 0;
- {$ENDIF}
- end;
- function TBlockSocket.CanWrite(Timeout: Integer): Boolean;
- var
- ti, tr: Integer;
- n: integer;
- begin
- if (FHeartbeatRate <> 0) and (Timeout <> -1) then
- begin
- ti := Timeout div FHeartbeatRate;
- tr := Timeout mod FHeartbeatRate;
- end
- else
- begin
- ti := 0;
- tr := Timeout;
- end;
- Result := InternalCanWrite(tr);
- if not Result then
- for n := 0 to ti do
- begin
- DoHeartbeat;
- if FStopFlag then
- begin
- Result := False;
- FStopFlag := False;
- Break;
- end;
- Result := InternalCanWrite(FHeartbeatRate);
- if Result then
- break;
- end;
- ExceptCheck;
- if Result then
- DoStatus(HR_CanWrite, '');
- end;
- function TBlockSocket.CanReadEx(Timeout: Integer): Boolean;
- begin
- if FBuffer <> '' then
- Result := True
- else
- Result := CanRead(Timeout);
- end;
- function TBlockSocket.SendBufferTo(const Buffer: TMemory; Length: Integer): Integer;
- begin
- Result := 0;
- if TestStopFlag then
- Exit;
- DoMonitor(True, Buffer, Length);
- LimitBandwidth(Length, FMaxSendBandwidth, FNextsend);
- Result := synsock.SendTo(FSocket, Buffer, Length, MSG_NOSIGNAL, FRemoteSin);
- SockCheck(Result);
- ExceptCheck;
- Inc(FSendCounter, Result);
- DoStatus(HR_WriteCount, IntToStr(Result));
- end;
- function TBlockSocket.RecvBufferFrom(Buffer: TMemory; Length: Integer): Integer;
- begin
- Result := 0;
- if TestStopFlag then
- Exit;
- LimitBandwidth(Length, FMaxRecvBandwidth, FNextRecv);
- Result := synsock.RecvFrom(FSocket, Buffer, Length, MSG_NOSIGNAL, FRemoteSin);
- SockCheck(Result);
- ExceptCheck;
- Inc(FRecvCounter, Result);
- DoStatus(HR_ReadCount, IntToStr(Result));
- DoMonitor(False, Buffer, Result);
- end;
- function TBlockSocket.GetSizeRecvBuffer: Integer;
- var
- l: Integer;
- {$IFDEF CIL}
- buf: TMemory;
- {$ENDIF}
- begin
- {$IFDEF CIL}
- setlength(buf, 4);
- SockCheck(synsock.GetSockOpt(FSocket, integer(SOL_SOCKET), integer(SO_RCVBUF), buf, l));
- Result := System.BitConverter.ToInt32(buf,0);
- {$ELSE}
- l := SizeOf(Result);
- SockCheck(synsock.GetSockOpt(FSocket, SOL_SOCKET, SO_RCVBUF, @Result, l));
- if FLastError <> 0 then
- Result := 1024;
- ExceptCheck;
- {$ENDIF}
- end;
- procedure TBlockSocket.SetSizeRecvBuffer(Size: Integer);
- var
- d: TSynaOption;
- begin
- //d := TSynaOption.Create;
- d.Option := SOT_RecvBuff;
- d.Value := Size;
- DelayedOption(d);
- end;
- function TBlockSocket.GetSizeSendBuffer: Integer;
- var
- l: Integer;
- {$IFDEF CIL}
- buf: TMemory;
- {$ENDIF}
- begin
- {$IFDEF CIL}
- setlength(buf, 4);
- SockCheck(synsock.GetSockOpt(FSocket, integer(SOL_SOCKET), integer(SO_SNDBUF), buf, l));
- Result := System.BitConverter.ToInt32(buf,0);
- {$ELSE}
- l := SizeOf(Result);
- SockCheck(synsock.GetSockOpt(FSocket, SOL_SOCKET, SO_SNDBUF, @Result, l));
- if FLastError <> 0 then
- Result := 1024;
- ExceptCheck;
- {$ENDIF}
- end;
- procedure TBlockSocket.SetSizeSendBuffer(Size: Integer);
- var
- d: TSynaOption;
- begin
- //d := TSynaOption.Create;
- d.Option := SOT_SendBuff;
- d.Value := Size;
- DelayedOption(d);
- end;
- procedure TBlockSocket.SetNagleMode(Value: Boolean);
- var
- d: TSynaOption;
- begin
- //d := TSynaOption.Create;
- d.Option := SOT_NoDelay;
- d.Enabled := Value;
- DelayedOption(d);
- end;
- procedure TBlockSocket.SetNonBlockMode(Value: Boolean);
- var
- d: TSynaOption;
- begin
- //d := TSynaOption.Create;
- d.Option := SOT_nonblock;
- d.Enabled := Value;
- DelayedOption(d);
- end;
- procedure TBlockSocket.SetTimeout(Timeout: Integer);
- begin
- SetSendTimeout(Timeout);
- SetRecvTimeout(Timeout);
- end;
- procedure TBlockSocket.SetSendTimeout(Timeout: Integer);
- var
- d: TSynaOption;
- begin
- //d := TSynaOption.Create;
- d.Option := SOT_sendtimeout;
- d.Value := Timeout;
- DelayedOption(d);
- end;
- procedure TBlockSocket.SetRecvTimeout(Timeout: Integer);
- var
- d: TSynaOption;
- begin
- //d := TSynaOption.Create;
- d.Option := SOT_recvtimeout;
- d.Value := Timeout;
- DelayedOption(d);
- end;
- {$IFNDEF CIL}
- function TBlockSocket.GroupCanRead(const SocketList: TSocketList; Timeout: Integer;
- const CanReadList: TSocketList): boolean;
- var
- FDSet: TFDSet;
- TimeVal: PTimeVal;
- TimeV: TTimeVal;
- x, n: Integer;
- Max: Integer;
- begin
- TimeV.tv_usec := (Timeout mod 1000) * 1000;
- TimeV.tv_sec := Timeout div 1000;
- TimeVal := @TimeV;
- if Timeout = -1 then
- TimeVal := nil;
- FD_ZERO(FDSet);
- Max := 0;
- for n := 0 to SocketList.Count - 1 do
- if TObject(SocketList.Items[n]) is TBlockSocket then
- begin
- if TBlockSocket(SocketList.Items[n]).Socket > Max then
- Max := TBlockSocket(SocketList.Items[n]).Socket;
- FD_SET(TBlockSocket(SocketList.Items[n]).Socket, FDSet);
- end;
- x := synsock.Select(Max + 1, @FDSet, nil, nil, TimeVal);
- SockCheck(x);
- ExceptCheck;
- if FLastError <> 0 then
- x := 0;
- Result := x > 0;
- CanReadList.Clear;
- if Result then
- for n := 0 to SocketList.Count - 1 do
- if TObject(SocketList.Items[n]) is TBlockSocket then
- if FD_ISSET(TBlockSocket(SocketList.Items[n]).Socket, FDSet) then
- CanReadList.Add(TBlockSocket(SocketList.Items[n]));
- end;
- {$ENDIF}
- procedure TBlockSocket.EnableReuse(Value: Boolean);
- var
- d: TSynaOption;
- begin
- //d := TSynaOption.Create;
- d.Option := SOT_reuse;
- d.Enabled := Value;
- DelayedOption(d);
- end;
- procedure TBlockSocket.SetTTL(TTL: integer);
- var
- d: TSynaOption;
- begin
- //d := TSynaOption.Create;
- d.Option := SOT_TTL;
- d.Value := TTL;
- DelayedOption(d);
- end;
- function TBlockSocket.GetTTL:integer;
- var
- l: Integer;
- begin
- {$IFNDEF CIL}
- l := SizeOf(Result);
- if FIP6Used then
- SockCheck(synsock.GetSockOpt(FSocket, IPPROTO_IPV6, IPV6_UNICAST_HOPS, @Result, l))
- else
- SockCheck(synsock.GetSockOpt(FSocket, IPPROTO_IP, IP_TTL, @Result, l));
- ExceptCheck;
- {$ENDIF}
- end;
- procedure TBlockSocket.SetFamily(Value: TSocketFamily);
- begin
- FFamily := Value;
- FFamilySave := Value;
- end;
- procedure TBlockSocket.SetSocket(Value: TSocket);
- begin
- FRecvCounter := 0;
- FSendCounter := 0;
- FSocket := Value;
- {$IFNDEF CIL}
- FD_ZERO(FFDSet);
- FD_SET(FSocket, FFDSet);
- {$ENDIF}
- GetSins;
- FIP6Used := FRemoteSin.AddressFamily = AF_INET6;
- end;
- function TBlockSocket.GetWsaData: TWSAData;
- begin
- {$IFDEF ONCEWINSOCK}
- Result := WsaDataOnce;
- {$ELSE}
- Result := FWsaDataOnce;
- {$ENDIF}
- end;
- function TBlockSocket.GetSocketType: integer;
- begin
- Result := 0;
- end;
- function TBlockSocket.GetSocketProtocol: integer;
- begin
- Result := integer(IPPROTO_IP);
- end;
- procedure TBlockSocket.DoStatus(Reason: THookSocketReason; const Value: string);
- begin
- if assigned(OnStatus) then
- OnStatus(Self, Reason, Value);
- if Reason = HR_SocketClose then
- FDisconnected := True;
- end;
- procedure TBlockSocket.DoReadFilter(Buffer: TMemory; var Len: Integer);
- var
- s: string;
- begin
- if assigned(OnReadFilter) then
- if Len > 0 then
- begin
- {$IFDEF CIL}
- s := StringOf(Buffer);
- {$ELSE}
- SetLength(s, Len);
- Move(Buffer^, Pointer(s)^, Len);
- {$ENDIF}
- OnReadFilter(Self, s);
- if s.Length > Len then
- SetLength(s, Len);
- Len := s.Length;
- {$IFDEF CIL}
- Buffer := BytesOf(s);
- {$ELSE}
- Move(Pointer(s)^, Buffer^, Len);
- {$ENDIF}
- end;
- end;
- procedure TBlockSocket.DoCreateSocket;
- begin
- if assigned(OnCreateSocket) then
- OnCreateSocket(Self);
- end;
- procedure TBlockSocket.DoMonitor(Writing: Boolean; const Buffer: TMemory; Len: Integer);
- begin
- if assigned(OnMonitor) then
- begin
- OnMonitor(Self, Writing, Buffer, Len);
- end;
- end;
- procedure TBlockSocket.DoHeartbeat;
- begin
- if assigned(OnHeartbeat) and (FHeartbeatRate <> 0) then
- begin
- OnHeartbeat(Self);
- end;
- end;
- function TBlockSocket.GetErrorDescEx: string;
- begin
- Result := GetErrorDesc(FLastError);
- end;
- class function TBlockSocket.GetErrorDesc(ErrorCode: Integer): string;
- begin
- {$IFDEF CIL}
- if ErrorCode = 0 then
- Result := ''
- else
- begin
- Result := WSAGetLastErrorDesc;
- if Result = '' then
- Result := 'Other Winsock error (' + IntToStr(ErrorCode) + ')';
- end;
- {$ELSE}
- case ErrorCode of
- 0:
- Result := '';
- WSAEINTR: {10004}
- Result := 'Interrupted system call';
- WSAEBADF: {10009}
- Result := 'Bad file number';
- WSAEACCES: {10013}
- Result := 'Permission denied';
- WSAEFAULT: {10014}
- Result := 'Bad address';
- WSAEINVAL: {10022}
- Result := 'Invalid argument';
- WSAEMFILE: {10024}
- Result := 'Too many open files';
- WSAEWOULDBLOCK: {10035}
- Result := 'Operation would block';
- WSAEINPROGRESS: {10036}
- Result := 'Operation now in progress';
- WSAEALREADY: {10037}
- Result := 'Operation already in progress';
- WSAENOTSOCK: {10038}
- Result := 'Socket operation on nonsocket';
- WSAEDESTADDRREQ: {10039}
- Result := 'Destination address required';
- WSAEMSGSIZE: {10040}
- Result := 'Message too long';
- WSAEPROTOTYPE: {10041}
- Result := 'Protocol wrong type for Socket';
- WSAENOPROTOOPT: {10042}
- Result := 'Protocol not available';
- WSAEPROTONOSUPPORT: {10043}
- Result := 'Protocol not supported';
- WSAESOCKTNOSUPPORT: {10044}
- Result := 'Socket not supported';
- WSAEOPNOTSUPP: {10045}
- Result := 'Operation not supported on Socket';
- WSAEPFNOSUPPORT: {10046}
- Result := 'Protocol family not supported';
- WSAEAFNOSUPPORT: {10047}
- Result := 'Address family not supported';
- WSAEADDRINUSE: {10048}
- Result := 'Address already in use';
- WSAEADDRNOTAVAIL: {10049}
- Result := 'Can''t assign requested address';
- WSAENETDOWN: {10050}
- Result := 'Network is down';
- WSAENETUNREACH: {10051}
- Result := 'Network is unreachable';
- WSAENETRESET: {10052}
- Result := 'Network dropped connection on reset';
- WSAECONNABORTED: {10053}
- Result := 'Software caused connection abort';
- WSAECONNRESET: {10054}
- Result := 'Connection reset by peer';
- WSAENOBUFS: {10055}
- Result := 'No Buffer space available';
- WSAEISCONN: {10056}
- Result := 'Socket is already connected';
- WSAENOTCONN: {10057}
- Result := 'Socket is not connected';
- WSAESHUTDOWN: {10058}
- Result := 'Can''t send after Socket shutdown';
- WSAETOOMANYREFS: {10059}
- Result := 'Too many references:can''t splice';
- WSAETIMEDOUT: {10060}
- Result := 'Connection timed out';
- WSAECONNREFUSED: {10061}
- Result := 'Connection refused';
- WSAELOOP: {10062}
- Result := 'Too many levels of symbolic links';
- WSAENAMETOOLONG: {10063}
- Result := 'File name is too long';
- WSAEHOSTDOWN: {10064}
- Result := 'Host is down';
- WSAEHOSTUNREACH: {10065}
- Result := 'No route to host';
- WSAENOTEMPTY: {10066}
- Result := 'Directory is not empty';
- WSAEPROCLIM: {10067}
- Result := 'Too many processes';
- WSAEUSERS: {10068}
- Result := 'Too many users';
- WSAEDQUOT: {10069}
- Result := 'Disk quota exceeded';
- WSAESTALE: {10070}
- Result := 'Stale NFS file handle';
- WSAEREMOTE: {10071}
- Result := 'Too many levels of remote in path';
- WSASYSNOTREADY: {10091}
- Result := 'Network subsystem is unusable';
- WSAVERNOTSUPPORTED: {10092}
- Result := 'Winsock DLL cannot support this application';
- WSANOTINITIALISED: {10093}
- Result := 'Winsock not initialized';
- WSAEDISCON: {10101}
- Result := 'Disconnect';
- WSAHOST_NOT_FOUND: {11001}
- Result := 'Host not found';
- WSATRY_AGAIN: {11002}
- Result := 'Non authoritative - host not found';
- WSANO_RECOVERY: {11003}
- Result := 'Non recoverable error';
- WSANO_DATA: {11004}
- Result := 'Valid name, no data record of requested type'
- else
- Result := SysErrorMessage(ErrorCode) // 'Other Winsock error (' + IntToStr(ErrorCode) + ')';
- end;
- {$ENDIF}
- end;
- {======================================================================}
- constructor TSocksBlockSocket.Create;
- begin
- inherited Create;
- FSocksIP:= '';
- FSocksPort:= '1080';
- FSocksTimeout:= 60000;
- FSocksUsername:= '';
- FSocksPassword:= '';
- FUsingSocks := False;
- FSocksResolver := True;
- FSocksLastError := 0;
- FSocksResponseIP := '';
- FSocksResponsePort := '';
- FSocksLocalIP := '';
- FSocksLocalPort := '';
- FSocksRemoteIP := '';
- FSocksRemotePort := '';
- FBypassFlag := False;
- FSocksType := ST_Socks5;
- end;
- function TSocksBlockSocket.SocksOpen: boolean;
- var
- Buf: string;
- n: integer;
- begin
- Result := False;
- FUsingSocks := False;
- if FSocksType <> ST_Socks5 then
- begin
- FUsingSocks := True;
- Result := True;
- end
- else
- begin
- FBypassFlag := True;
- try
- if FSocksUsername = '' then
- Buf := #5 + #1 + #0
- else
- Buf := #5 + #2 + #2 +#0;
- SendString(Buf);
- Buf := RecvBufferStr(2, FSocksTimeout);
- if Buf.Length < 2 then
- Exit;
- if Buf[1] <> #5 then
- Exit;
- n := Ord(Buf[2]);
- case n of
- 0: //not need authorisation
- ;
- 2:
- begin
- buf := #1 + Char(Length(FSocksUsername)) + FSocksUsername +
- Char(Length(FSocksPassword)) + FSocksPassword;
- SendString(Buf);
- Buf := RecvBufferStr(2, FSocksTimeout);
- if Length(Buf) < 2 then
- Exit;
- if Buf[2] <> #0 then
- Exit;
- end;
- else
- //other authorisation is not supported!
- Exit;
- end;
- FUsingSocks := True;
- Result := True;
- finally
- FBypassFlag := False;
- end;
- end;
- end;
- function TSocksBlockSocket.SocksRequest(Cmd: Byte;
- const IP, Port: string): Boolean;
- var
- buf: string;
- begin
- FBypassFlag := True;
- try
- if FSocksType <> ST_Socks5 then
- Buf := #4 + Char(Cmd) + SocksCode(IP, Port)
- else
- Buf := #5 + Char(Cmd) + #0 + SocksCode(IP, Port);
- SendString(Buf);
- Result := FLastError = 0;
- finally
- FBypassFlag := False;
- end;
- end;
- function TSocksBlockSocket.SocksResponse: Boolean;
- var
- Buf, s, m, z: string;
- x: integer;
- begin
- Result := False;
- FBypassFlag := True;
- try
- FSocksResponseIP := '';
- FSocksResponsePort := '';
- FSocksLastError := -1;
- if FSocksType <> ST_Socks5 then
- begin
- Buf := RecvBufferStr(8, FSocksTimeout);
- if FLastError <> 0 then
- Exit;
- if Buf[1] <> #0 then
- Exit;
- FSocksLastError := Ord(Buf[2]);
- end
- else
- begin
- Buf := RecvBufferStr(4, FSocksTimeout);
- if FLastError <> 0 then
- Exit;
- if Buf[1] <> #5 then
- Exit;
- case Ord(Buf[4]) of
- 1:
- s := RecvBufferStr(4, FSocksTimeout);
- 3:
- begin
- x := RecvByte(FSocksTimeout);
- if FLastError <> 0 then
- Exit;
- s := Char(x) + RecvBufferStr(x, FSocksTimeout);
- end;
- 4:
- s := RecvBufferStr(16, FSocksTimeout);
- else
- Exit;
- end;
- Buf := Buf + s + RecvBufferStr(2, FSocksTimeout);
- if FLastError <> 0 then
- Exit;
- FSocksLastError := Ord(Buf[2]);
- end;
- //---
- if ((FSocksLastError <> 0) and (FSocksLastError <> $5A{90})) then
- begin
- case FSocksLastError of // http://en.wikipedia.org/wiki/SOCKS
- // v4
- $5a: m := 'request granted';
- $5b: m := 'request rejected or failed';
- $5c: m := 'request failed because client is not running identd (or not reachable from the server)';
- $5d: m := 'request failed because client''s identd could not confirm the user ID string in the request';
- // v5'
- $00: m := 'request grant`ed';
- $01: m := 'general failure';
- $02: m := 'connection not allowed by ruleset';
- $03: m := 'network unreachable';
- $04: m := 'host unreachable';
- $05: m := 'connection refused by destination host';
- $06: m := 'TTL expired';
- $07: m := 'command not supported / protocol error';
- $08: m := 'address type not supported';
- else
- m := '';
- end;
- z := SysUtils.Format('Error 0x%x', [FSocksLastError]);
- if m<>'' then
- z := z + ' ' + m;
- Exit;
- end;
- SocksDecode(Buf);
- Result := True;
- finally
- FBypassFlag := False;
- end;
- end;
- function TSocksBlockSocket.SocksCode(IP: string; const Port: string): string;
- var
- ip6: TIp6Bytes;
- n: integer;
- begin
- if FSocksType <> ST_Socks5 then
- begin
- Result := CodeInt(ResolvePort(Port));
- if not FSocksResolver then
- IP := ResolveName(IP);
- if IsIP(IP) then
- begin
- Result := Result + IPToID(IP);
- Result := Result + FSocksUsername + #0;
- end
- else
- begin
- Result := Result + IPToID('0.0.0.1');
- Result := Result + FSocksUsername + #0;
- Result := Result + IP + #0;
- end;
- end
- else
- begin
- if not FSocksResolver then
- IP := ResolveName(IP);
- if IsIP(IP) then
- Result := #1 + IPToID(IP)
- else
- if IsIP6(IP) then
- begin
- ip6 := StrToIP6(IP);
- Result := #4;
- for n := 0 to 15 do
- Result := Result + Char(ip6[n]);
- end
- else
- Result := #3 + Char(Length(IP)) + IP;
- Result := Result + CodeInt(ResolvePort(Port));
- end;
- end;
- function TSocksBlockSocket.SocksDecode(const Value: string): integer;
- var
- Atyp: Byte;
- y, n: integer;
- w: Word;
- ip6: TIp6Bytes;
- begin
- FSocksResponsePort := '0';
- Result := 0;
- if FSocksType <> ST_Socks5 then
- begin
- if Length(Value) < 8 then
- Exit;
- Result := 3;
- w := DecodeInt(Value, Result);
- FSocksResponsePort := IntToStr(w);
- FSocksResponseIP := Format('%d.%d.%d.%d',
- [Ord(Value[5]), Ord(Value[6]), Ord(Value[7]), Ord(Value[8])]);
- Result := 9;
- end
- else
- begin
- if Length(Value) < 4 then
- Exit;
- Atyp := Ord(Value[4]);
- Result := 5;
- case Atyp of
- 1:
- begin
- if Length(Value) < 10 then
- Exit;
- FSocksResponseIP := Format('%d.%d.%d.%d',
- [Ord(Value[5]), Ord(Value[6]), Ord(Value[7]), Ord(Value[8])]);
- Result := 9;
- end;
- 3:
- begin
- y := Ord(Value[5]);
- if Length(Value) < (5 + y + 2) then
- Exit;
- for n := 6 to 6 + y - 1 do
- FSocksResponseIP := FSocksResponseIP + Value[n];
- Result := 5 + y + 1;
- end;
- 4:
- begin
- if Length(Value) < 22 then
- Exit;
- for n := 0 to 15 do
- ip6[n] := ord(Value[n + 5]);
- FSocksResponseIP := IP6ToStr(ip6);
- Result := 21;
- end;
- else
- Exit;
- end;
- w := DecodeInt(Value, Result);
- FSocksResponsePort := IntToStr(w);
- Result := Result + 2;
- end;
- end;
- {======================================================================}
- procedure TDgramBlockSocket.Connect(const IP, Port: string);
- begin
- SetRemoteSin(IP, Port);
- InternalCreateSocket(FRemoteSin);
- if UseConnect then
- begin
- SockCheck(synsock.Connect(FSocket, FRemoteSin));
- if FLastError = 0 then
- GetSins;
- end;
- FBuffer := '';
- DoStatus(HR_Connect, IP + ':' + Port);
- end;
- function TDgramBlockSocket.RecvBuffer(Buffer: TMemory; Length: Integer): Integer;
- begin
- Result := RecvBufferFrom(Buffer, Length);
- end;
- function TDgramBlockSocket.SendBuffer(const Buffer: TMemory; Length: Integer): Integer;
- begin
- Result := SendBufferTo(Buffer, Length);
- end;
- {======================================================================}
- destructor TUDPBlockSocket.Destroy;
- begin
- if Assigned(FSocksControlSock) then
- FreeAndNil(FSocksControlSock);
- inherited;
- end;
- procedure TUDPBlockSocket.EnableBroadcast(Value: Boolean);
- var
- d: TSynaOption;
- begin
- //d := TSynaOption.Create;
- d.Option := SOT_Broadcast;
- d.Enabled := Value;
- DelayedOption(d);
- end;
- function TUDPBlockSocket.UdpAssociation: Boolean;
- var
- b: Boolean;
- begin
- Result := True;
- FUsingSocks := False;
- if FSocksIP <> '' then
- begin
- Result := False;
- if not Assigned(FSocksControlSock) then
- FSocksControlSock := TTCPBlockSocket.Create;
- FSocksControlSock.CloseSocket;
- FSocksControlSock.CreateSocketByName(FSocksIP);
- FSocksControlSock.Connect(FSocksIP, FSocksPort);
- if FSocksControlSock.LastError <> 0 then
- Exit;
- // if not assigned local port, assign it!
- if not FBinded then
- Bind(cAnyHost, cAnyPort);
- //open control TCP connection to SOCKS
- FSocksControlSock.FSocksUsername := FSocksUsername;
- FSocksControlSock.FSocksPassword := FSocksPassword;
- b := FSocksControlSock.SocksOpen;
- if b then
- b := FSocksControlSock.SocksRequest(3, GetLocalSinIP, IntToStr(GetLocalSinPort));
- if b then
- b := FSocksControlSock.SocksResponse;
- if not b and (FLastError = 0) then
- FLastError := WSANO_RECOVERY;
- FUsingSocks :=FSocksControlSock.UsingSocks;
- FSocksRemoteIP := FSocksControlSock.FSocksResponseIP;
- FSocksRemotePort := FSocksControlSock.FSocksResponsePort;
- Result := b and (FLastError = 0);
- end;
- end;
- function TUDPBlockSocket.SendBufferTo(const Buffer: TMemory; Length: Integer): Integer;
- var
- SIp: string;
- SPort: integer;
- Buf: string;
- begin
- Result := 0;
- FUsingSocks := False;
- if (FSocksIP <> '') and (not UdpAssociation) then
- FLastError := WSANO_RECOVERY
- else
- begin
- if FUsingSocks then
- begin
- {$IFNDEF CIL}
- Sip := GetRemoteSinIp;
- SPort := GetRemoteSinPort;
- SetRemoteSin(FSocksRemoteIP, FSocksRemotePort);
- SetLength(Buf,Length);
- Move(Buffer^, Pointer(Buf)^, Length);
- Buf := #0 + #0 + #0 + SocksCode(Sip, IntToStr(SPort)) + Buf;
- Result := inherited SendBufferTo(Pointer(Buf), buf.Length);
- SetRemoteSin(Sip, IntToStr(SPort));
- {$ENDIF}
- end
- else
- Result := inherited SendBufferTo(Buffer, Length);
- end;
- end;
- function TUDPBlockSocket.RecvBufferFrom(Buffer: TMemory; Length: Integer): Integer;
- var
- Buf: string;
- x: integer;
- begin
- Result := inherited RecvBufferFrom(Buffer, Length);
- if FUsingSocks then
- begin
- {$IFNDEF CIL}
- SetLength(Buf, Result);
- Move(Buffer^, Pointer(Buf)^, Result);
- x := SocksDecode(Buf);
- Result := Result - x + 1;
- Buf := Copy(Buf, x, Result);
- Move(Pointer(Buf)^, Buffer^, Result);
- SetRemoteSin(FSocksResponseIP, FSocksResponsePort);
- {$ENDIF}
- end;
- end;
- {$IFNDEF CIL}
- procedure TUDPBlockSocket.AddMulticast(const MCastIP: string);
- var
- Multicast: TIP_mreq;
- Multicast6: TIPv6_mreq;
- n: integer;
- ip6: Tip6bytes;
- begin
- if FIP6Used then
- begin
- ip6 := StrToIp6(MCastIP);
- for n := 0 to 15 do
- Multicast6.ipv6mr_multiaddr.{$IFDEF POSIX}s6_addr{$ELSE}u6_addr8{$ENDIF}[n] := Ip6[n];
- Multicast6.ipv6mr_interface := 0;
- SockCheck(synsock.SetSockOpt(FSocket, IPPROTO_IPV6, IPV6_JOIN_GROUP,
- Pointer(@Multicast6), SizeOf(Multicast6)));
- end
- else
- begin
- Multicast.imr_multiaddr.S_addr := swapbytes(strtoip(MCastIP));
- // Multicast.imr_interface.S_addr := INADDR_ANY;
- Multicast.imr_interface.S_addr := FLocalSin.sin_addr.S_addr;
- SockCheck(synsock.SetSockOpt(FSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP,
- Pointer(@Multicast), SizeOf(Multicast)));
- end;
- ExceptCheck;
- end;
- procedure TUDPBlockSocket.DropMulticast(const MCastIP: string);
- var
- Multicast: TIP_mreq;
- Multicast6: TIPv6_mreq;
- n: integer;
- ip6: Tip6bytes;
- begin
- if FIP6Used then
- begin
- ip6 := StrToIp6(MCastIP);
- for n := 0 to 15 do
- Multicast6.ipv6mr_multiaddr.{$IFDEF POSIX}s6_addr{$ELSE}u6_addr8{$ENDIF}[n] := Ip6[n];
- Multicast6.ipv6mr_interface := 0;
- SockCheck(synsock.SetSockOpt(FSocket, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
- Pointer(@Multicast6), SizeOf(Multicast6)));
- end
- else
- begin
- Multicast.imr_multiaddr.S_addr := swapbytes(strtoip(MCastIP));
- // Multicast.imr_interface.S_addr := INADDR_ANY;
- Multicast.imr_interface.S_addr := FLocalSin.sin_addr.S_addr;
- SockCheck(synsock.SetSockOpt(FSocket, IPPROTO_IP, IP_DROP_MEMBERSHIP,
- Pointer(@Multicast), SizeOf(Multicast)));
- end;
- ExceptCheck;
- end;
- {$ENDIF}
- procedure TUDPBlockSocket.SetMulticastTTL(TTL: integer);
- var
- d: TSynaOption;
- begin
- //d := TSynaOption.Create;
- d.Option := SOT_MulticastTTL;
- d.Value := TTL;
- DelayedOption(d);
- end;
- function TUDPBlockSocket.GetMulticastTTL:integer;
- var
- l: Integer;
- begin
- {$IFNDEF CIL}
- l := SizeOf(Result);
- if FIP6Used then
- SockCheck(synsock.GetSockOpt(FSocket, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, @Result, l))
- else
- SockCheck(synsock.GetSockOpt(FSocket, IPPROTO_IP, IP_MULTICAST_TTL, @Result, l));
- ExceptCheck;
- {$ENDIF}
- end;
- procedure TUDPBlockSocket.EnableMulticastLoop(Value: Boolean);
- var
- d: TSynaOption;
- begin
- //d := TSynaOption.Create;
- d.Option := SOT_MulticastLoop;
- d.Enabled := Value;
- DelayedOption(d);
- end;
- function TUDPBlockSocket.GetSocketType: integer;
- begin
- Result := integer(SOCK_DGRAM);
- end;
- function TUDPBlockSocket.GetSocketProtocol: integer;
- begin
- Result := integer(IPPROTO_UDP);
- end;
- {======================================================================}
- constructor TTCPBlockSocket.CreateWithSSL(SSLPlugin: TSSLClass);
- begin
- inherited Create;
- FSSL := SSLPlugin.Create(self);
- FHTTPTunnelIP := '';
- FHTTPTunnelPort := '';
- FHTTPTunnel := False;
- FHTTPTunnelRemoteIP := '';
- FHTTPTunnelRemotePort := '';
- FHTTPTunnelUser := '';
- FHTTPTunnelPass := '';
- FHTTPTunnelTimeout := 30000;
- end;
- constructor TTCPBlockSocket.Create;
- begin
- CreateWithSSL(SSLImplementation);
- end;
- destructor TTCPBlockSocket.Destroy;
- begin
- inherited Destroy;
- FreeAndNil(FSSL);
- end;
- function TTCPBlockSocket.GetErrorDescEx: string;
- begin
- Result := inherited GetErrorDescEx;
- if (FLastError = WSASYSNOTREADY) and (self.SSL.LastError <> 0) then
- begin
- Result := self.SSL.LastErrorDesc;
- end;
- end;
- const
- SHUT_RDWR = 2;
- procedure TTCPBlockSocket.CloseSocket;
- begin
- if FSSL.SSLEnabled then
- FSSL.Shutdown;
- if (FSocket <> INVALID_SOCKET) and (FLastError = 0) then
- begin
- SockCheck(Synsock.Shutdown(FSocket, SHUT_RDWR));
- //ExceptCheck;
- Purge;
- SetLinger(True, 0);
- end;
- inherited CloseSocket;
- end;
- procedure TTCPBlockSocket.DoAfterConnect;
- begin
- if Assigned(OnAfterConnect) then
- begin
- OnAfterConnect(Self);
- end;
- end;
- function TTCPBlockSocket.WaitingData: Integer;
- begin
- Result := 0;
- if FSSL.SSLEnabled and (FSocket <> INVALID_SOCKET) then
- Result := FSSL.WaitingData;
- if Result = 0 then
- Result := inherited WaitingData;
- end;
- procedure TTCPBlockSocket.Listen;
- var
- b: Boolean;
- Sip,SPort: string;
- begin
- if FSocksIP = '' then
- begin
- inherited Listen;
- end
- else
- begin
- Sip := GetLocalSinIP;
- if Sip = cAnyHost then
- Sip := LocalName;
- SPort := IntToStr(GetLocalSinPort);
- inherited Connect(FSocksIP, FSocksPort);
- b := SocksOpen;
- if b then
- b := SocksRequest(2, Sip, SPort);
- if b then
- b := SocksResponse;
- if not b and (FLastError = 0) then
- FLastError := WSANO_RECOVERY;
- FSocksLocalIP := FSocksResponseIP;
- if FSocksLocalIP = cAnyHost then
- FSocksLocalIP := FSocksIP;
- FSocksLocalPort := FSocksResponsePort;
- FSocksRemoteIP := '';
- FSocksRemotePort := '';
- ExceptCheck;
- DoStatus(HR_Listen, '');
- end;
- end;
- function TTCPBlockSocket.Accept: TSocket;
- begin
- if FUsingSocks then
- begin
- if not SocksResponse and (FLastError = 0) then
- FLastError := WSANO_RECOVERY;
- FSocksRemoteIP := FSocksResponseIP;
- FSocksRemotePort := FSocksResponsePort;
- Result := FSocket;
- ExceptCheck;
- DoStatus(HR_Accept, '');
- end
- else
- begin
- result := inherited Accept;
- end;
- end;
- procedure TTCPBlockSocket.Connect(const IP, Port: string);
- begin
- FDisconnected := False;
- if FSocksIP <> '' then
- SocksDoConnect(IP, Port)
- else
- if FHTTPTunnelIP <> '' then
- HTTPTunnelDoConnect(IP, Port)
- else
- inherited Connect(IP, Port);
- if FLasterror = 0 then
- DoAfterConnect;
- end;
- function TTCPBlockSocket.Connected: boolean;
- begin
- Result := (FSocket <> INVALID_SOCKET) and not FDisconnected;
- {$IFNDEF UNIX}
- if Result then
- begin
- CanRead(0);
- Result := not FDisconnected;
- end;
- {$ENDIF}
- end;
- procedure TTCPBlockSocket.SocksDoConnect(const IP, Port: string);
- var
- b: Boolean;
- begin
- inherited Connect(FSocksIP, FSocksPort);
- if FLastError = 0 then
- begin
- b := SocksOpen;
- if b then
- b := SocksRequest(1, IP, Port);
- if b then
- b := SocksResponse;
- if not b and (FLastError = 0) then
- FLastError := WSASYSNOTREADY;
- FSocksLocalIP := FSocksResponseIP;
- FSocksLocalPort := FSocksResponsePort;
- FSocksRemoteIP := IP;
- FSocksRemotePort := Port;
- end;
- ExceptCheck;
- DoStatus(HR_Connect, IP + ':' + Port);
- end;
- procedure TTCPBlockSocket.HTTPTunnelDoConnect(IP, Port: string);
- //bugfixed by Mike Green ([email protected])
- var
- s: string;
- begin
- Port := IntToStr(ResolvePort(Port));
- inherited Connect(FHTTPTunnelIP, FHTTPTunnelPort);
- if FLastError <> 0 then
- Exit;
- FHTTPTunnel := False;
- if IsIP6(IP) then
- IP := '[' + IP + ']';
- SendString('CONNECT ' + IP + ':' + Port + ' HTTP/1.0' + CRLF);
- if FHTTPTunnelUser <> '' then
- Sendstring('Proxy-Authorization: Basic ' +
- EncodeBase64(FHTTPTunnelUser + ':' + FHTTPTunnelPass) + CRLF);
- SendString(CRLF);
- repeat
- s := RecvTerminated(FHTTPTunnelTimeout, #$0a);
- if FLastError <> 0 then
- Break;
- if (Pos('HTTP/', s) = 1) and (Length(s) > 11) then
- FHTTPTunnel := s[10] = '2';
- until (s = '') or (s = #$0d);
- if (FLasterror = 0) and not FHTTPTunnel then
- FLastError := WSAECONNREFUSED;
- FHTTPTunnelRemoteIP := IP;
- FHTTPTunnelRemotePort := Port;
- ExceptCheck;
- end;
- procedure TTCPBlockSocket.SSLDoConnect;
- begin
- ResetLastError;
- if not FSSL.Connect then
- FLastError := WSASYSNOTREADY;
- ExceptCheck;
- end;
- procedure TTCPBlockSocket.SSLDoShutdown;
- begin
- ResetLastError;
- FSSL.BiShutdown;
- end;
- function TTCPBlockSocket.GetLocalSinIP: string;
- begin
- if FUsingSocks then
- Result := FSocksLocalIP
- else
- Result := inherited GetLocalSinIP;
- end;
- function TTCPBlockSocket.GetRemoteSinIP: string;
- begin
- if FUsingSocks then
- Result := FSocksRemoteIP
- else
- if FHTTPTunnel then
- Result := FHTTPTunnelRemoteIP
- else
- Result := inherited GetRemoteSinIP;
- end;
- function TTCPBlockSocket.GetLocalSinPort: Integer;
- begin
- if FUsingSocks then
- Result := StrToIntDef(FSocksLocalPort, 0)
- else
- Result := inherited GetLocalSinPort;
- end;
- function TTCPBlockSocket.GetRemoteSinPort: Integer;
- begin
- if FUsingSocks then
- Result := ResolvePort(FSocksRemotePort)
- else
- if FHTTPTunnel then
- Result := StrToIntDef(FHTTPTunnelRemotePort, 0)
- else
- Result := inherited GetRemoteSinPort;
- end;
- function TTCPBlockSocket.RecvBuffer(Buffer: TMemory; Len: Integer): Integer;
- begin
- if FSSL.SSLEnabled then
- begin
- Result := 0;
- if TestStopFlag then
- Exit;
- ResetLastError;
- LimitBandwidth(Len, FMaxRecvBandwidth, FNextRecv);
- Result := FSSL.RecvBuffer(Buffer, Len);
- if FSSL.LastError <> 0 then
- FLastError := WSASYSNOTREADY;
- ExceptCheck;
- Inc(FRecvCounter, Result);
- DoStatus(HR_ReadCount, IntToStr(Result));
- DoMonitor(False, Buffer, Result);
- DoReadFilter(Buffer, Result);
- end
- else
- Result := inherited RecvBuffer(Buffer, Len);
- end;
- function TTCPBlockSocket.SendBuffer(const Buffer: TMemory; Length: Integer): Integer;
- var
- x, y: integer;
- l, r: integer;
- {$IFNDEF CIL}
- p: Pointer;
- {$ENDIF}
- begin
- if FSSL.SSLEnabled then
- begin
- Result := 0;
- if TestStopFlag then
- Exit;
- ResetLastError;
- DoMonitor(True, Buffer, Length);
- {$IFDEF CIL}
- Result := FSSL.SendBuffer(Buffer, Length);
- if FSSL.LastError <> 0 then
- FLastError := WSASYSNOTREADY;
- Inc(FSendCounter, Result);
- DoStatus(HR_WriteCount, IntToStr(Result));
- {$ELSE}
- l := Length;
- x := 0;
- while x < l do
- begin
- y := l - x;
- if y > FSendMaxChunk then
- y := FSendMaxChunk;
- if y > 0 then
- begin
- LimitBandwidth(y, FMaxSendBandwidth, FNextsend);
- p := IncPoint(Buffer, x);
- r := FSSL.SendBuffer(p, y);
- if FSSL.LastError <> 0 then
- FLastError := WSASYSNOTREADY;
- if Flasterror <> 0 then
- Break;
- Inc(x, r);
- Inc(Result, r);
- Inc(FSendCounter, r);
- DoStatus(HR_WriteCount, IntToStr(r));
- end
- else
- break;
- end;
- {$ENDIF}
- ExceptCheck;
- end
- else
- Result := inherited SendBuffer(Buffer, Length);
- end;
- function TTCPBlockSocket.SSLAcceptConnection: Boolean;
- begin
- ResetLastError;
- if not FSSL.Accept then
- FLastError := WSASYSNOTREADY;
- ExceptCheck;
- Result := FLastError = 0;
- end;
- function TTCPBlockSocket.GetSocketType: integer;
- begin
- Result := integer(SOCK_STREAM);
- end;
- function TTCPBlockSocket.GetSocketProtocol: integer;
- begin
- Result := integer(IPPROTO_TCP);
- end;
- {======================================================================}
- function TICMPBlockSocket.GetSocketType: integer;
- begin
- Result := integer(SOCK_RAW);
- end;
- function TICMPBlockSocket.GetSocketProtocol: integer;
- begin
- if FIP6Used then
- Result := integer(IPPROTO_ICMPV6)
- else
- Result := integer(IPPROTO_ICMP);
- end;
- {======================================================================}
- function TRAWBlockSocket.GetSocketType: integer;
- begin
- Result := integer(SOCK_RAW);
- end;
- function TRAWBlockSocket.GetSocketProtocol: integer;
- begin
- Result := integer(IPPROTO_RAW);
- end;
- {======================================================================}
- function TPGMmessageBlockSocket.GetSocketType: integer;
- begin
- Result := integer(SOCK_RDM);
- end;
- function TPGMmessageBlockSocket.GetSocketProtocol: integer;
- begin
- Result := integer(IPPROTO_RM);
- end;
- {======================================================================}
- function TPGMstreamBlockSocket.GetSocketType: integer;
- begin
- Result := integer(SOCK_STREAM);
- end;
- function TPGMstreamBlockSocket.GetSocketProtocol: integer;
- begin
- Result := integer(IPPROTO_RM);
- end;
- {======================================================================}
- constructor TSynaClient.Create;
- begin
- inherited Create;
- FIPInterface := cAnyHost;
- FTargetHost := cLocalhost;
- FTargetPort := cAnyPort;
- FTimeout := 5000;
- FUsername := '';
- FPassword := '';
- end;
- {======================================================================}
- constructor TCustomSSL.Create(const Value: TTCPBlockSocket);
- begin
- inherited Create;
- FSocket := Value;
- FSSLEnabled := False;
- FUsername := '';
- FPassword := '';
- FLastError := 0;
- FLastErrorDesc := '';
- FVerifyCert := False;
- FSSLType := LT_all;
- FKeyPassword := '';
- FCiphers := '';
- FCertificateFile := '';
- FPrivateKeyFile := '';
- FCertCAFile := '';
- FCertCA := '';
- FTrustCertificate := '';
- FTrustCertificateFile := '';
- FCertificate := '';
- FPrivateKey := '';
- FPFX := '';
- FPFXfile := '';
- FSSHChannelType := '';
- FSSHChannelArg1 := '';
- FSSHChannelArg2 := '';
- FCertComplianceLevel := -1; //default
- FSNIHost := '';
- end;
- procedure TCustomSSL.Assign(const Value: TCustomSSL);
- begin
- FUsername := Value.Username;
- FPassword := Value.Password;
- FVerifyCert := Value.VerifyCert;
- FSSLType := Value.SSLType;
- FKeyPassword := Value.KeyPassword;
- FCiphers := Value.Ciphers;
- FCertificateFile := Value.CertificateFile;
- FPrivateKeyFile := Value.PrivateKeyFile;
- FCertCAFile := Value.CertCAFile;
- FCertCA := Value.CertCA;
- FTrustCertificate := Value.TrustCertificate;
- FTrustCertificateFile := Value.TrustCertificateFile;
- FCertificate := Value.Certificate;
- FPrivateKey := Value.PrivateKey;
- FPFX := Value.PFX;
- FPFXfile := Value.PFXfile;
- FCertComplianceLevel := Value.CertComplianceLevel;
- FSNIHost := Value.FSNIHost;
- end;
- procedure TCustomSSL.ReturnError;
- begin
- FLastError := -1;
- FLastErrorDesc := 'SSL/TLS support is not compiled!';
- end;
- function TCustomSSL.LibVersion: string;
- begin
- Result := '';
- end;
- function TCustomSSL.LibName: string;
- begin
- Result := '';
- end;
- function TCustomSSL.CreateSelfSignedCert(Host: string): Boolean;
- begin
- Result := False;
- end;
- function TCustomSSL.Connect: boolean;
- begin
- ReturnError;
- Result := False;
- end;
- function TCustomSSL.Accept: boolean;
- begin
- ReturnError;
- Result := False;
- end;
- function TCustomSSL.Shutdown: boolean;
- begin
- ReturnError;
- Result := False;
- end;
- function TCustomSSL.BiShutdown: boolean;
- begin
- ReturnError;
- Result := False;
- end;
- function TCustomSSL.SendBuffer(Buffer: TMemory; Len: Integer): Integer;
- begin
- ReturnError;
- Result := integer(SOCKET_ERROR);
- end;
- procedure TCustomSSL.SetCertCAFile(const Value: string);
- begin
- FCertCAFile := Value;
- end;
- function TCustomSSL.RecvBuffer(Buffer: TMemory; Len: Integer): Integer;
- begin
- ReturnError;
- Result := integer(SOCKET_ERROR);
- end;
- function TCustomSSL.WaitingData: Integer;
- begin
- ReturnError;
- Result := 0;
- end;
- function TCustomSSL.GetSSLVersion: string;
- begin
- Result := '';
- end;
- function TCustomSSL.GetPeerSubject: string;
- begin
- Result := '';
- end;
- function TCustomSSL.GetPeerSerialNo: integer;
- begin
- Result := -1;
- end;
- function TCustomSSL.GetPeerName: string;
- begin
- Result := '';
- end;
- function TCustomSSL.GetPeerNameHash: cardinal;
- begin
- Result := 0;
- end;
- function TCustomSSL.GetPeerIssuer: string;
- begin
- Result := '';
- end;
- function TCustomSSL.GetPeerFingerprint: string;
- begin
- Result := '';
- end;
- function TCustomSSL.GetCertInfo: string;
- begin
- Result := '';
- end;
- function TCustomSSL.GetCipherName: string;
- begin
- Result := '';
- end;
- function TCustomSSL.GetCipherBits: integer;
- begin
- Result := 0;
- end;
- function TCustomSSL.GetCipherAlgBits: integer;
- begin
- Result := 0;
- end;
- function TCustomSSL.GetVerifyCert: integer;
- begin
- Result := 1;
- end;
- function TCustomSSL.DoVerifyCert:boolean;
- begin
- if assigned(OnVerifyCert) then
- begin
- result:=OnVerifyCert(Self);
- end
- else
- result:=true;
- end;
- {======================================================================}
- function TSSLNone.LibVersion: string;
- begin
- Result := 'Without SSL support';
- end;
- function TSSLNone.LibName: string;
- begin
- Result := 'ssl_none';
- end;
- {======================================================================}
- initialization
- begin
- {$IFDEF ONCEWINSOCK}
- if not InitSocketInterface(DLLStackName) then
- begin
- e := ESynapseError.Create('Error loading Socket interface (' + DLLStackName + ')!');
- e.ErrorCode := 0;
- e.ErrorMessage := 'Error loading Socket interface (' + DLLStackName + ')!';
- raise e;
- end;
- synsock.WSAStartup(WinsockLevel, WsaDataOnce);
- {$ENDIF}
- end;
- finalization
- begin
- {$IFDEF ONCEWINSOCK}
- synsock.WSACleanup;
- DestroySocketInterface;
- {$ENDIF}
- end;
- end.
|