ngtcp2.h 194 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868
  1. /*
  2. * ngtcp2
  3. *
  4. * Copyright (c) 2017 ngtcp2 contributors
  5. * Copyright (c) 2017 nghttp2 contributors
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining
  8. * a copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sublicense, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be
  16. * included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. */
  26. #ifndef NGTCP2_H
  27. #define NGTCP2_H
  28. /* Define WIN32 when build target is Win32 API (borrowed from
  29. libcurl) */
  30. #if (defined(_WIN32) || defined(__WIN32__)) && !defined(WIN32)
  31. # define WIN32
  32. #endif
  33. #ifdef _MSC_VER
  34. # pragma warning(push)
  35. # pragma warning(disable : 4324)
  36. #endif
  37. #include <stdlib.h>
  38. #if defined(_MSC_VER) && (_MSC_VER < 1800)
  39. /* MSVC < 2013 does not have inttypes.h because it is not C99
  40. compliant. See compiler macros and version number in
  41. https://sourceforge.net/p/predef/wiki/Compilers/ */
  42. # include <stdint.h>
  43. #else /* !defined(_MSC_VER) || (_MSC_VER >= 1800) */
  44. # include <inttypes.h>
  45. #endif /* !defined(_MSC_VER) || (_MSC_VER >= 1800) */
  46. #include <sys/types.h>
  47. #include <stdarg.h>
  48. #include <stddef.h>
  49. #ifndef NGTCP2_USE_GENERIC_SOCKADDR
  50. # ifdef WIN32
  51. # ifndef WIN32_LEAN_AND_MEAN
  52. # define WIN32_LEAN_AND_MEAN
  53. # endif /* WIN32_LEAN_AND_MEAN */
  54. # include <ws2tcpip.h>
  55. # else /* !WIN32 */
  56. # include <sys/socket.h>
  57. # include <netinet/in.h>
  58. # endif /* !WIN32 */
  59. #endif /* NGTCP2_USE_GENERIC_SOCKADDR */
  60. #include <ngtcp2/version.h>
  61. #ifdef NGTCP2_STATICLIB
  62. # define NGTCP2_EXTERN
  63. #elif defined(WIN32)
  64. # ifdef BUILDING_NGTCP2
  65. # define NGTCP2_EXTERN __declspec(dllexport)
  66. # else /* !BUILDING_NGTCP2 */
  67. # define NGTCP2_EXTERN __declspec(dllimport)
  68. # endif /* !BUILDING_NGTCP2 */
  69. #else /* !defined(WIN32) */
  70. # ifdef BUILDING_NGTCP2
  71. # define NGTCP2_EXTERN __attribute__((visibility("default")))
  72. # else /* !BUILDING_NGTCP2 */
  73. # define NGTCP2_EXTERN
  74. # endif /* !BUILDING_NGTCP2 */
  75. #endif /* !defined(WIN32) */
  76. #ifdef _MSC_VER
  77. # define NGTCP2_ALIGN(N) __declspec(align(N))
  78. #else /* !_MSC_VER */
  79. # define NGTCP2_ALIGN(N) __attribute__((aligned(N)))
  80. #endif /* !_MSC_VER */
  81. #ifdef __cplusplus
  82. extern "C" {
  83. #endif
  84. /**
  85. * @typedef
  86. *
  87. * :type:`ngtcp2_ssize` is signed counterpart of size_t.
  88. */
  89. typedef ptrdiff_t ngtcp2_ssize;
  90. /**
  91. * @functypedef
  92. *
  93. * :type:`ngtcp2_malloc` is a custom memory allocator to replace
  94. * :manpage:`malloc(3)`. The |user_data| is
  95. * :member:`ngtcp2_mem.user_data`.
  96. */
  97. typedef void *(*ngtcp2_malloc)(size_t size, void *user_data);
  98. /**
  99. * @functypedef
  100. *
  101. * :type:`ngtcp2_free` is a custom memory allocator to replace
  102. * :manpage:`free(3)`. The |user_data| is
  103. * :member:`ngtcp2_mem.user_data`.
  104. */
  105. typedef void (*ngtcp2_free)(void *ptr, void *user_data);
  106. /**
  107. * @functypedef
  108. *
  109. * :type:`ngtcp2_calloc` is a custom memory allocator to replace
  110. * :manpage:`calloc(3)`. The |user_data| is the
  111. * :member:`ngtcp2_mem.user_data`.
  112. */
  113. typedef void *(*ngtcp2_calloc)(size_t nmemb, size_t size, void *user_data);
  114. /**
  115. * @functypedef
  116. *
  117. * :type:`ngtcp2_realloc` is a custom memory allocator to replace
  118. * :manpage:`realloc(3)`. The |user_data| is the
  119. * :member:`ngtcp2_mem.user_data`.
  120. */
  121. typedef void *(*ngtcp2_realloc)(void *ptr, size_t size, void *user_data);
  122. /**
  123. * @struct
  124. *
  125. * :type:`ngtcp2_mem` is a custom memory allocator. The
  126. * :member:`user_data` field is passed to each allocator function.
  127. * This can be used, for example, to achieve per-connection memory
  128. * pool.
  129. *
  130. * In the following example code, ``my_malloc``, ``my_free``,
  131. * ``my_calloc`` and ``my_realloc`` are the replacement of the
  132. * standard allocators :manpage:`malloc(3)`, :manpage:`free(3)`,
  133. * :manpage:`calloc(3)` and :manpage:`realloc(3)` respectively::
  134. *
  135. * void *my_malloc_cb(size_t size, void *user_data) {
  136. * (void)user_data;
  137. * return my_malloc(size);
  138. * }
  139. *
  140. * void my_free_cb(void *ptr, void *user_data) {
  141. * (void)user_data;
  142. * my_free(ptr);
  143. * }
  144. *
  145. * void *my_calloc_cb(size_t nmemb, size_t size, void *user_data) {
  146. * (void)user_data;
  147. * return my_calloc(nmemb, size);
  148. * }
  149. *
  150. * void *my_realloc_cb(void *ptr, size_t size, void *user_data) {
  151. * (void)user_data;
  152. * return my_realloc(ptr, size);
  153. * }
  154. *
  155. * void conn_new() {
  156. * ngtcp2_mem mem = {NULL, my_malloc_cb, my_free_cb, my_calloc_cb,
  157. * my_realloc_cb};
  158. *
  159. * ...
  160. * }
  161. */
  162. typedef struct ngtcp2_mem {
  163. /**
  164. * :member:`user_data` is an arbitrary user supplied data. This
  165. * is passed to each allocator function.
  166. */
  167. void *user_data;
  168. /**
  169. * :member:`malloc` is a custom allocator function to replace
  170. * :manpage:`malloc(3)`.
  171. */
  172. ngtcp2_malloc malloc;
  173. /**
  174. * :member:`free` is a custom allocator function to replace
  175. * :manpage:`free(3)`.
  176. */
  177. ngtcp2_free free;
  178. /**
  179. * :member:`calloc` is a custom allocator function to replace
  180. * :manpage:`calloc(3)`.
  181. */
  182. ngtcp2_calloc calloc;
  183. /**
  184. * :member:`realloc` is a custom allocator function to replace
  185. * :manpage:`realloc(3)`.
  186. */
  187. ngtcp2_realloc realloc;
  188. } ngtcp2_mem;
  189. /**
  190. * @macrosection
  191. *
  192. * Time related macros
  193. */
  194. /**
  195. * @macro
  196. *
  197. * :macro:`NGTCP2_SECONDS` is a count of tick which corresponds to 1
  198. * second.
  199. */
  200. #define NGTCP2_SECONDS ((ngtcp2_duration)1000000000ULL)
  201. /**
  202. * @macro
  203. *
  204. * :macro:`NGTCP2_MILLISECONDS` is a count of tick which corresponds
  205. * to 1 millisecond.
  206. */
  207. #define NGTCP2_MILLISECONDS ((ngtcp2_duration)1000000ULL)
  208. /**
  209. * @macro
  210. *
  211. * :macro:`NGTCP2_MICROSECONDS` is a count of tick which corresponds
  212. * to 1 microsecond.
  213. */
  214. #define NGTCP2_MICROSECONDS ((ngtcp2_duration)1000ULL)
  215. /**
  216. * @macro
  217. *
  218. * :macro:`NGTCP2_NANOSECONDS` is a count of tick which corresponds to
  219. * 1 nanosecond.
  220. */
  221. #define NGTCP2_NANOSECONDS ((ngtcp2_duration)1ULL)
  222. /**
  223. * @macrosection
  224. *
  225. * QUIC protocol version macros
  226. */
  227. /**
  228. * @macro
  229. *
  230. * :macro:`NGTCP2_PROTO_VER_V1` is the QUIC version 1.
  231. */
  232. #define NGTCP2_PROTO_VER_V1 ((uint32_t)0x00000001u)
  233. /**
  234. * @macro
  235. *
  236. * :macro:`NGTCP2_PROTO_VER_V2` is the QUIC version 2. See
  237. * :rfc:`9369`.
  238. */
  239. #define NGTCP2_PROTO_VER_V2 ((uint32_t)0x6b3343cfu)
  240. /**
  241. * @macro
  242. *
  243. * :macro:`NGTCP2_PROTO_VER_MAX` is the highest QUIC version that this
  244. * library supports. Deprecated since v1.1.0.
  245. */
  246. #define NGTCP2_PROTO_VER_MAX NGTCP2_PROTO_VER_V1
  247. /**
  248. * @macro
  249. *
  250. * :macro:`NGTCP2_PROTO_VER_MIN` is the lowest QUIC version that this
  251. * library supports. Deprecated since v1.1.0.
  252. */
  253. #define NGTCP2_PROTO_VER_MIN NGTCP2_PROTO_VER_V1
  254. /**
  255. * @macro
  256. *
  257. * :macro:`NGTCP2_RESERVED_VERSION_MASK` is the bit mask of reserved
  258. * version.
  259. */
  260. #define NGTCP2_RESERVED_VERSION_MASK 0x0a0a0a0au
  261. /**
  262. * @macrosection
  263. *
  264. * UDP datagram related macros
  265. */
  266. /**
  267. * @macro
  268. *
  269. * :macro:`NGTCP2_MAX_UDP_PAYLOAD_SIZE` is the default maximum UDP
  270. * datagram payload size that the local endpoint transmits.
  271. */
  272. #define NGTCP2_MAX_UDP_PAYLOAD_SIZE 1200
  273. /**
  274. * @macro
  275. *
  276. * :macro:`NGTCP2_MAX_PMTUD_UDP_PAYLOAD_SIZE` is the maximum UDP
  277. * datagram payload size that Path MTU Discovery can discover.
  278. */
  279. #define NGTCP2_MAX_PMTUD_UDP_PAYLOAD_SIZE 1452
  280. /**
  281. * @macrosection
  282. *
  283. * QUIC specific macros
  284. */
  285. /**
  286. * @macro
  287. *
  288. * :macro:`NGTCP2_MAX_VARINT` is the maximum value which can be
  289. * encoded in variable-length integer encoding.
  290. */
  291. #define NGTCP2_MAX_VARINT ((1ULL << 62) - 1)
  292. /**
  293. * @macro
  294. *
  295. * :macro:`NGTCP2_STATELESS_RESET_TOKENLEN` is the length of Stateless
  296. * Reset Token.
  297. */
  298. #define NGTCP2_STATELESS_RESET_TOKENLEN 16
  299. /**
  300. * @macro
  301. *
  302. * :macro:`NGTCP2_MIN_STATELESS_RESET_RANDLEN` is the minimum length
  303. * of random bytes (Unpredictable Bits) in Stateless Reset packet.
  304. */
  305. #define NGTCP2_MIN_STATELESS_RESET_RANDLEN 5
  306. /**
  307. * @macro
  308. *
  309. * :macro:`NGTCP2_PATH_CHALLENGE_DATALEN` is the length of
  310. * PATH_CHALLENGE data.
  311. */
  312. #define NGTCP2_PATH_CHALLENGE_DATALEN 8
  313. /**
  314. * @macro
  315. *
  316. * :macro:`NGTCP2_RETRY_KEY_V1` is an encryption key to create
  317. * integrity tag of Retry packet. It is used for QUIC v1.
  318. */
  319. #define NGTCP2_RETRY_KEY_V1 \
  320. "\xbe\x0c\x69\x0b\x9f\x66\x57\x5a\x1d\x76\x6b\x54\xe3\x68\xc8\x4e"
  321. /**
  322. * @macro
  323. *
  324. * :macro:`NGTCP2_RETRY_NONCE_V1` is nonce used when generating
  325. * integrity tag of Retry packet. It is used for QUIC v1.
  326. */
  327. #define NGTCP2_RETRY_NONCE_V1 "\x46\x15\x99\xd3\x5d\x63\x2b\xf2\x23\x98\x25\xbb"
  328. /**
  329. * @macro
  330. *
  331. * :macro:`NGTCP2_RETRY_KEY_V2` is an encryption key to create
  332. * integrity tag of Retry packet. It is used for QUIC v2. See
  333. * :rfc:`9369`.
  334. */
  335. #define NGTCP2_RETRY_KEY_V2 \
  336. "\x8f\xb4\xb0\x1b\x56\xac\x48\xe2\x60\xfb\xcb\xce\xad\x7c\xcc\x92"
  337. /**
  338. * @macro
  339. *
  340. * :macro:`NGTCP2_RETRY_NONCE_V2` is nonce used when generating
  341. * integrity tag of Retry packet. It is used for QUIC v2. See
  342. * :rfc:`9369`.
  343. */
  344. #define NGTCP2_RETRY_NONCE_V2 "\xd8\x69\x69\xbc\x2d\x7c\x6d\x99\x90\xef\xb0\x4a"
  345. /**
  346. * @macro
  347. *
  348. * :macro:`NGTCP2_HP_MASKLEN` is the length of header protection mask.
  349. */
  350. #define NGTCP2_HP_MASKLEN 5
  351. /**
  352. * @macro
  353. *
  354. * :macro:`NGTCP2_HP_SAMPLELEN` is the number bytes sampled when
  355. * encrypting a packet header.
  356. */
  357. #define NGTCP2_HP_SAMPLELEN 16
  358. /**
  359. * @macro
  360. *
  361. * :macro:`NGTCP2_DEFAULT_INITIAL_RTT` is a default initial RTT.
  362. */
  363. #define NGTCP2_DEFAULT_INITIAL_RTT (333 * NGTCP2_MILLISECONDS)
  364. /**
  365. * @macro
  366. *
  367. * :macro:`NGTCP2_MAX_CIDLEN` is the maximum length of Connection ID.
  368. */
  369. #define NGTCP2_MAX_CIDLEN 20
  370. /**
  371. * @macro
  372. *
  373. * :macro:`NGTCP2_MIN_CIDLEN` is the minimum length of Connection ID.
  374. */
  375. #define NGTCP2_MIN_CIDLEN 1
  376. /**
  377. * @macro
  378. *
  379. * :macro:`NGTCP2_MIN_INITIAL_DCIDLEN` is the minimum length of
  380. * Destination Connection ID in Client Initial packet if it does not
  381. * bear token from Retry packet.
  382. */
  383. #define NGTCP2_MIN_INITIAL_DCIDLEN 8
  384. /**
  385. * @macrosection
  386. *
  387. * ECN related macros
  388. */
  389. /**
  390. * @macro
  391. *
  392. * :macro:`NGTCP2_ECN_NOT_ECT` indicates no ECN marking.
  393. */
  394. #define NGTCP2_ECN_NOT_ECT 0x0
  395. /**
  396. * @macro
  397. *
  398. * :macro:`NGTCP2_ECN_ECT_1` is ECT(1) codepoint.
  399. */
  400. #define NGTCP2_ECN_ECT_1 0x1
  401. /**
  402. * @macro
  403. *
  404. * :macro:`NGTCP2_ECN_ECT_0` is ECT(0) codepoint.
  405. */
  406. #define NGTCP2_ECN_ECT_0 0x2
  407. /**
  408. * @macro
  409. *
  410. * :macro:`NGTCP2_ECN_CE` is CE codepoint.
  411. */
  412. #define NGTCP2_ECN_CE 0x3
  413. /**
  414. * @macro
  415. *
  416. * :macro:`NGTCP2_ECN_MASK` is a bit mask to get ECN marking.
  417. */
  418. #define NGTCP2_ECN_MASK 0x3
  419. #define NGTCP2_PKT_INFO_V1 1
  420. #define NGTCP2_PKT_INFO_VERSION NGTCP2_PKT_INFO_V1
  421. /**
  422. * @struct
  423. *
  424. * :type:`ngtcp2_pkt_info` is a packet metadata.
  425. */
  426. typedef struct NGTCP2_ALIGN(8) ngtcp2_pkt_info {
  427. /**
  428. * :member:`ecn` is ECN marking, and when it is passed to
  429. * `ngtcp2_conn_read_pkt()`, it should be either
  430. * :macro:`NGTCP2_ECN_NOT_ECT`, :macro:`NGTCP2_ECN_ECT_1`,
  431. * :macro:`NGTCP2_ECN_ECT_0`, or :macro:`NGTCP2_ECN_CE`.
  432. */
  433. uint8_t ecn;
  434. } ngtcp2_pkt_info;
  435. /**
  436. * @macrosection
  437. *
  438. * ngtcp2 library error codes
  439. */
  440. /**
  441. * @macro
  442. *
  443. * :macro:`NGTCP2_ERR_INVALID_ARGUMENT` indicates that a passed
  444. * argument is invalid.
  445. */
  446. #define NGTCP2_ERR_INVALID_ARGUMENT -201
  447. /**
  448. * @macro
  449. *
  450. * :macro:`NGTCP2_ERR_NOBUF` indicates that a provided buffer does not
  451. * have enough space to store data.
  452. */
  453. #define NGTCP2_ERR_NOBUF -202
  454. /**
  455. * @macro
  456. *
  457. * :macro:`NGTCP2_ERR_PROTO` indicates a general protocol error.
  458. */
  459. #define NGTCP2_ERR_PROTO -203
  460. /**
  461. * @macro
  462. *
  463. * :macro:`NGTCP2_ERR_INVALID_STATE` indicates that a requested
  464. * operation is not allowed at the current connection state.
  465. */
  466. #define NGTCP2_ERR_INVALID_STATE -204
  467. /**
  468. * @macro
  469. *
  470. * :macro:`NGTCP2_ERR_ACK_FRAME` indicates that an invalid ACK frame
  471. * is received.
  472. */
  473. #define NGTCP2_ERR_ACK_FRAME -205
  474. /**
  475. * @macro
  476. *
  477. * :macro:`NGTCP2_ERR_STREAM_ID_BLOCKED` indicates that there is no
  478. * spare stream ID available.
  479. */
  480. #define NGTCP2_ERR_STREAM_ID_BLOCKED -206
  481. /**
  482. * @macro
  483. *
  484. * :macro:`NGTCP2_ERR_STREAM_IN_USE` indicates that a stream ID is
  485. * already in use.
  486. */
  487. #define NGTCP2_ERR_STREAM_IN_USE -207
  488. /**
  489. * @macro
  490. *
  491. * :macro:`NGTCP2_ERR_STREAM_DATA_BLOCKED` indicates that stream data
  492. * cannot be sent because of flow control.
  493. */
  494. #define NGTCP2_ERR_STREAM_DATA_BLOCKED -208
  495. /**
  496. * @macro
  497. *
  498. * :macro:`NGTCP2_ERR_FLOW_CONTROL` indicates flow control error.
  499. */
  500. #define NGTCP2_ERR_FLOW_CONTROL -209
  501. /**
  502. * @macro
  503. *
  504. * :macro:`NGTCP2_ERR_CONNECTION_ID_LIMIT` indicates that the number
  505. * of received Connection ID exceeds acceptable limit.
  506. */
  507. #define NGTCP2_ERR_CONNECTION_ID_LIMIT -210
  508. /**
  509. * @macro
  510. *
  511. * :macro:`NGTCP2_ERR_STREAM_LIMIT` indicates that a remote endpoint
  512. * opens more streams that is permitted.
  513. */
  514. #define NGTCP2_ERR_STREAM_LIMIT -211
  515. /**
  516. * @macro
  517. *
  518. * :macro:`NGTCP2_ERR_FINAL_SIZE` indicates that inconsistent final
  519. * size of a stream.
  520. */
  521. #define NGTCP2_ERR_FINAL_SIZE -212
  522. /**
  523. * @macro
  524. *
  525. * :macro:`NGTCP2_ERR_CRYPTO` indicates crypto (TLS) related error.
  526. */
  527. #define NGTCP2_ERR_CRYPTO -213
  528. /**
  529. * @macro
  530. *
  531. * :macro:`NGTCP2_ERR_PKT_NUM_EXHAUSTED` indicates that packet number
  532. * is exhausted.
  533. */
  534. #define NGTCP2_ERR_PKT_NUM_EXHAUSTED -214
  535. /**
  536. * @macro
  537. *
  538. * :macro:`NGTCP2_ERR_REQUIRED_TRANSPORT_PARAM` indicates that a
  539. * required transport parameter is missing.
  540. */
  541. #define NGTCP2_ERR_REQUIRED_TRANSPORT_PARAM -215
  542. /**
  543. * @macro
  544. *
  545. * :macro:`NGTCP2_ERR_MALFORMED_TRANSPORT_PARAM` indicates that a
  546. * transport parameter is malformed.
  547. */
  548. #define NGTCP2_ERR_MALFORMED_TRANSPORT_PARAM -216
  549. /**
  550. * @macro
  551. *
  552. * :macro:`NGTCP2_ERR_FRAME_ENCODING` indicates there is an error in
  553. * frame encoding.
  554. */
  555. #define NGTCP2_ERR_FRAME_ENCODING -217
  556. /**
  557. * @macro
  558. *
  559. * :macro:`NGTCP2_ERR_DECRYPT` indicates a decryption failure.
  560. */
  561. #define NGTCP2_ERR_DECRYPT -218
  562. /**
  563. * @macro
  564. *
  565. * :macro:`NGTCP2_ERR_STREAM_SHUT_WR` indicates no more data can be
  566. * sent to a stream.
  567. */
  568. #define NGTCP2_ERR_STREAM_SHUT_WR -219
  569. /**
  570. * @macro
  571. *
  572. * :macro:`NGTCP2_ERR_STREAM_NOT_FOUND` indicates that a stream was
  573. * not found.
  574. */
  575. #define NGTCP2_ERR_STREAM_NOT_FOUND -220
  576. /**
  577. * @macro
  578. *
  579. * :macro:`NGTCP2_ERR_STREAM_STATE` indicates that a requested
  580. * operation is not allowed at the current stream state.
  581. */
  582. #define NGTCP2_ERR_STREAM_STATE -221
  583. /**
  584. * @macro
  585. *
  586. * :macro:`NGTCP2_ERR_RECV_VERSION_NEGOTIATION` indicates that Version
  587. * Negotiation packet was received.
  588. */
  589. #define NGTCP2_ERR_RECV_VERSION_NEGOTIATION -222
  590. /**
  591. * @macro
  592. *
  593. * :macro:`NGTCP2_ERR_CLOSING` indicates that connection is in closing
  594. * state.
  595. */
  596. #define NGTCP2_ERR_CLOSING -223
  597. /**
  598. * @macro
  599. *
  600. * :macro:`NGTCP2_ERR_DRAINING` indicates that connection is in
  601. * draining state.
  602. */
  603. #define NGTCP2_ERR_DRAINING -224
  604. /**
  605. * @macro
  606. *
  607. * :macro:`NGTCP2_ERR_TRANSPORT_PARAM` indicates a general transport
  608. * parameter error.
  609. */
  610. #define NGTCP2_ERR_TRANSPORT_PARAM -225
  611. /**
  612. * @macro
  613. *
  614. * :macro:`NGTCP2_ERR_DISCARD_PKT` indicates a packet was discarded.
  615. */
  616. #define NGTCP2_ERR_DISCARD_PKT -226
  617. /**
  618. * @macro
  619. *
  620. * :macro:`NGTCP2_ERR_CONN_ID_BLOCKED` indicates that there is no
  621. * spare Connection ID available.
  622. */
  623. #define NGTCP2_ERR_CONN_ID_BLOCKED -227
  624. /**
  625. * @macro
  626. *
  627. * :macro:`NGTCP2_ERR_INTERNAL` indicates an internal error.
  628. */
  629. #define NGTCP2_ERR_INTERNAL -228
  630. /**
  631. * @macro
  632. *
  633. * :macro:`NGTCP2_ERR_CRYPTO_BUFFER_EXCEEDED` indicates that a crypto
  634. * buffer exceeded.
  635. */
  636. #define NGTCP2_ERR_CRYPTO_BUFFER_EXCEEDED -229
  637. /**
  638. * @macro
  639. *
  640. * :macro:`NGTCP2_ERR_WRITE_MORE` indicates
  641. * :macro:`NGTCP2_WRITE_STREAM_FLAG_MORE` is used and a function call
  642. * succeeded.
  643. */
  644. #define NGTCP2_ERR_WRITE_MORE -230
  645. /**
  646. * @macro
  647. *
  648. * :macro:`NGTCP2_ERR_RETRY` indicates that server should send Retry
  649. * packet.
  650. */
  651. #define NGTCP2_ERR_RETRY -231
  652. /**
  653. * @macro
  654. *
  655. * :macro:`NGTCP2_ERR_DROP_CONN` indicates that an endpoint should
  656. * drop connection immediately.
  657. */
  658. #define NGTCP2_ERR_DROP_CONN -232
  659. /**
  660. * @macro
  661. *
  662. * :macro:`NGTCP2_ERR_AEAD_LIMIT_REACHED` indicates AEAD encryption
  663. * limit is reached and key update is not available. An endpoint
  664. * should drop connection immediately.
  665. */
  666. #define NGTCP2_ERR_AEAD_LIMIT_REACHED -233
  667. /**
  668. * @macro
  669. *
  670. * :macro:`NGTCP2_ERR_NO_VIABLE_PATH` indicates that path validation
  671. * could not probe that a path is capable of sending UDP datagram
  672. * payload of size at least 1200 bytes.
  673. */
  674. #define NGTCP2_ERR_NO_VIABLE_PATH -234
  675. /**
  676. * @macro
  677. *
  678. * :macro:`NGTCP2_ERR_VERSION_NEGOTIATION` indicates that server
  679. * should send Version Negotiation packet.
  680. */
  681. #define NGTCP2_ERR_VERSION_NEGOTIATION -235
  682. /**
  683. * @macro
  684. *
  685. * :macro:`NGTCP2_ERR_HANDSHAKE_TIMEOUT` indicates that QUIC
  686. * connection is not established before the specified deadline.
  687. */
  688. #define NGTCP2_ERR_HANDSHAKE_TIMEOUT -236
  689. /**
  690. * @macro
  691. *
  692. * :macro:`NGTCP2_ERR_VERSION_NEGOTIATION_FAILURE` indicates the
  693. * version negotiation failed.
  694. */
  695. #define NGTCP2_ERR_VERSION_NEGOTIATION_FAILURE -237
  696. /**
  697. * @macro
  698. *
  699. * :macro:`NGTCP2_ERR_IDLE_CLOSE` indicates the connection should be
  700. * closed silently because of idle timeout.
  701. */
  702. #define NGTCP2_ERR_IDLE_CLOSE -238
  703. /**
  704. * @macro
  705. *
  706. * :macro:`NGTCP2_ERR_FATAL` indicates that error codes less than this
  707. * value is fatal error. When this error is returned, an endpoint
  708. * should close connection immediately.
  709. */
  710. #define NGTCP2_ERR_FATAL -500
  711. /**
  712. * @macro
  713. *
  714. * :macro:`NGTCP2_ERR_NOMEM` indicates out of memory.
  715. */
  716. #define NGTCP2_ERR_NOMEM -501
  717. /**
  718. * @macro
  719. *
  720. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` indicates that user defined
  721. * callback function failed.
  722. */
  723. #define NGTCP2_ERR_CALLBACK_FAILURE -502
  724. /**
  725. * @macrosection
  726. *
  727. * QUIC packet header flags
  728. */
  729. /**
  730. * @macro
  731. *
  732. * :macro:`NGTCP2_PKT_FLAG_NONE` indicates no flag set.
  733. */
  734. #define NGTCP2_PKT_FLAG_NONE 0x00u
  735. /**
  736. * @macro
  737. *
  738. * :macro:`NGTCP2_PKT_FLAG_LONG_FORM` indicates the Long header packet
  739. * header.
  740. */
  741. #define NGTCP2_PKT_FLAG_LONG_FORM 0x01u
  742. /**
  743. * @macro
  744. *
  745. * :macro:`NGTCP2_PKT_FLAG_FIXED_BIT_CLEAR` indicates that Fixed Bit
  746. * (aka QUIC bit) is not set.
  747. */
  748. #define NGTCP2_PKT_FLAG_FIXED_BIT_CLEAR 0x02u
  749. /**
  750. * @macro
  751. *
  752. * :macro:`NGTCP2_PKT_FLAG_KEY_PHASE` indicates Key Phase bit set.
  753. */
  754. #define NGTCP2_PKT_FLAG_KEY_PHASE 0x04u
  755. /**
  756. * @enum
  757. *
  758. * :type:`ngtcp2_pkt_type` defines QUIC version-independent QUIC
  759. * packet types.
  760. */
  761. typedef enum ngtcp2_pkt_type {
  762. /**
  763. * :enum:`NGTCP2_PKT_VERSION_NEGOTIATION` is defined by libngtcp2
  764. * for convenience.
  765. */
  766. NGTCP2_PKT_VERSION_NEGOTIATION = 0x80,
  767. /**
  768. * :enum:`NGTCP2_PKT_STATELESS_RESET` is defined by libngtcp2 for
  769. * convenience.
  770. */
  771. NGTCP2_PKT_STATELESS_RESET = 0x81,
  772. /**
  773. * :enum:`NGTCP2_PKT_INITIAL` indicates Initial packet.
  774. */
  775. NGTCP2_PKT_INITIAL = 0x10,
  776. /**
  777. * :enum:`NGTCP2_PKT_0RTT` indicates 0-RTT packet.
  778. */
  779. NGTCP2_PKT_0RTT = 0x11,
  780. /**
  781. * :enum:`NGTCP2_PKT_HANDSHAKE` indicates Handshake packet.
  782. */
  783. NGTCP2_PKT_HANDSHAKE = 0x12,
  784. /**
  785. * :enum:`NGTCP2_PKT_RETRY` indicates Retry packet.
  786. */
  787. NGTCP2_PKT_RETRY = 0x13,
  788. /**
  789. * :enum:`NGTCP2_PKT_1RTT` is defined by libngtcp2 for convenience.
  790. */
  791. NGTCP2_PKT_1RTT = 0x40
  792. } ngtcp2_pkt_type;
  793. /**
  794. * @macrosection
  795. *
  796. * QUIC transport error code
  797. */
  798. /**
  799. * @macro
  800. *
  801. * :macro:`NGTCP2_NO_ERROR` is QUIC transport error code ``NO_ERROR``.
  802. */
  803. #define NGTCP2_NO_ERROR 0x0u
  804. /**
  805. * @macro
  806. *
  807. * :macro:`NGTCP2_INTERNAL_ERROR` is QUIC transport error code
  808. * ``INTERNAL_ERROR``.
  809. */
  810. #define NGTCP2_INTERNAL_ERROR 0x1u
  811. /**
  812. * @macro
  813. *
  814. * :macro:`NGTCP2_CONNECTION_REFUSED` is QUIC transport error code
  815. * ``CONNECTION_REFUSED``.
  816. */
  817. #define NGTCP2_CONNECTION_REFUSED 0x2u
  818. /**
  819. * @macro
  820. *
  821. * :macro:`NGTCP2_FLOW_CONTROL_ERROR` is QUIC transport error code
  822. * ``FLOW_CONTROL_ERROR``.
  823. */
  824. #define NGTCP2_FLOW_CONTROL_ERROR 0x3u
  825. /**
  826. * @macro
  827. *
  828. * :macro:`NGTCP2_STREAM_LIMIT_ERROR` is QUIC transport error code
  829. * ``STREAM_LIMIT_ERROR``.
  830. */
  831. #define NGTCP2_STREAM_LIMIT_ERROR 0x4u
  832. /**
  833. * @macro
  834. *
  835. * :macro:`NGTCP2_STREAM_STATE_ERROR` is QUIC transport error code
  836. * ``STREAM_STATE_ERROR``.
  837. */
  838. #define NGTCP2_STREAM_STATE_ERROR 0x5u
  839. /**
  840. * @macro
  841. *
  842. * :macro:`NGTCP2_FINAL_SIZE_ERROR` is QUIC transport error code
  843. * ``FINAL_SIZE_ERROR``.
  844. */
  845. #define NGTCP2_FINAL_SIZE_ERROR 0x6u
  846. /**
  847. * @macro
  848. *
  849. * :macro:`NGTCP2_FRAME_ENCODING_ERROR` is QUIC transport error code
  850. * ``FRAME_ENCODING_ERROR``.
  851. */
  852. #define NGTCP2_FRAME_ENCODING_ERROR 0x7u
  853. /**
  854. * @macro
  855. *
  856. * :macro:`NGTCP2_TRANSPORT_PARAMETER_ERROR` is QUIC transport error
  857. * code ``TRANSPORT_PARAMETER_ERROR``.
  858. */
  859. #define NGTCP2_TRANSPORT_PARAMETER_ERROR 0x8u
  860. /**
  861. * @macro
  862. *
  863. * :macro:`NGTCP2_CONNECTION_ID_LIMIT_ERROR` is QUIC transport error
  864. * code ``CONNECTION_ID_LIMIT_ERROR``.
  865. */
  866. #define NGTCP2_CONNECTION_ID_LIMIT_ERROR 0x9u
  867. /**
  868. * @macro
  869. *
  870. * :macro:`NGTCP2_PROTOCOL_VIOLATION` is QUIC transport error code
  871. * ``PROTOCOL_VIOLATION``.
  872. */
  873. #define NGTCP2_PROTOCOL_VIOLATION 0xau
  874. /**
  875. * @macro
  876. *
  877. * :macro:`NGTCP2_INVALID_TOKEN` is QUIC transport error code
  878. * ``INVALID_TOKEN``.
  879. */
  880. #define NGTCP2_INVALID_TOKEN 0xbu
  881. /**
  882. * @macro
  883. *
  884. * :macro:`NGTCP2_APPLICATION_ERROR` is QUIC transport error code
  885. * ``APPLICATION_ERROR``.
  886. */
  887. #define NGTCP2_APPLICATION_ERROR 0xcu
  888. /**
  889. * @macro
  890. *
  891. * :macro:`NGTCP2_CRYPTO_BUFFER_EXCEEDED` is QUIC transport error code
  892. * ``CRYPTO_BUFFER_EXCEEDED``.
  893. */
  894. #define NGTCP2_CRYPTO_BUFFER_EXCEEDED 0xdu
  895. /**
  896. * @macro
  897. *
  898. * :macro:`NGTCP2_KEY_UPDATE_ERROR` is QUIC transport error code
  899. * ``KEY_UPDATE_ERROR``.
  900. */
  901. #define NGTCP2_KEY_UPDATE_ERROR 0xeu
  902. /**
  903. * @macro
  904. *
  905. * :macro:`NGTCP2_AEAD_LIMIT_REACHED` is QUIC transport error code
  906. * ``AEAD_LIMIT_REACHED``.
  907. */
  908. #define NGTCP2_AEAD_LIMIT_REACHED 0xfu
  909. /**
  910. * @macro
  911. *
  912. * :macro:`NGTCP2_NO_VIABLE_PATH` is QUIC transport error code
  913. * ``NO_VIABLE_PATH``.
  914. */
  915. #define NGTCP2_NO_VIABLE_PATH 0x10u
  916. /**
  917. * @macro
  918. *
  919. * :macro:`NGTCP2_CRYPTO_ERROR` is QUIC transport error code
  920. * ``CRYPTO_ERROR``.
  921. */
  922. #define NGTCP2_CRYPTO_ERROR 0x100u
  923. /**
  924. * @macro
  925. *
  926. * :macro:`NGTCP2_VERSION_NEGOTIATION_ERROR` is QUIC transport error
  927. * code ``VERSION_NEGOTIATION_ERROR``. See :rfc:`9368`.
  928. */
  929. #define NGTCP2_VERSION_NEGOTIATION_ERROR 0x11
  930. /**
  931. * @enum
  932. *
  933. * :type:`ngtcp2_path_validation_result` defines path validation
  934. * result code.
  935. */
  936. typedef enum ngtcp2_path_validation_result {
  937. /**
  938. * :enum:`NGTCP2_PATH_VALIDATION_RESULT_SUCCESS` indicates
  939. * successful validation.
  940. */
  941. NGTCP2_PATH_VALIDATION_RESULT_SUCCESS,
  942. /**
  943. * :enum:`NGTCP2_PATH_VALIDATION_RESULT_FAILURE` indicates
  944. * validation failure.
  945. */
  946. NGTCP2_PATH_VALIDATION_RESULT_FAILURE,
  947. /**
  948. * :enum:`NGTCP2_PATH_VALIDATION_RESULT_ABORTED` indicates that path
  949. * validation was aborted.
  950. */
  951. NGTCP2_PATH_VALIDATION_RESULT_ABORTED
  952. } ngtcp2_path_validation_result;
  953. /**
  954. * @typedef
  955. *
  956. * :type:`ngtcp2_tstamp` is a timestamp with nanosecond resolution.
  957. * ``UINT64_MAX`` is an invalid value, and it is often used to
  958. * indicate that no value is set.
  959. */
  960. typedef uint64_t ngtcp2_tstamp;
  961. /**
  962. * @typedef
  963. *
  964. * :type:`ngtcp2_duration` is a period of time in nanosecond
  965. * resolution. ``UINT64_MAX`` is an invalid value, and it is often
  966. * used to indicate that no value is set.
  967. */
  968. typedef uint64_t ngtcp2_duration;
  969. /**
  970. * @struct
  971. *
  972. * :type:`ngtcp2_cid` holds a Connection ID.
  973. */
  974. typedef struct ngtcp2_cid {
  975. /**
  976. * :member:`datalen` is the length of Connection ID.
  977. */
  978. size_t datalen;
  979. /**
  980. * :member:`data` is the buffer to store Connection ID.
  981. */
  982. uint8_t data[NGTCP2_MAX_CIDLEN];
  983. } ngtcp2_cid;
  984. /**
  985. * @struct
  986. *
  987. * :type:`ngtcp2_vec` is struct iovec compatible structure to
  988. * reference arbitrary array of bytes.
  989. */
  990. typedef struct ngtcp2_vec {
  991. /**
  992. * :member:`base` points to the data.
  993. */
  994. uint8_t *base;
  995. /**
  996. * :member:`len` is the number of bytes which the buffer pointed by
  997. * base contains.
  998. */
  999. size_t len;
  1000. } ngtcp2_vec;
  1001. /**
  1002. * @function
  1003. *
  1004. * `ngtcp2_cid_init` initializes Connection ID |cid| with the byte
  1005. * string pointed by |data| and its length is |datalen|. |datalen|
  1006. * must be at most :macro:`NGTCP2_MAX_CIDLEN`.
  1007. */
  1008. NGTCP2_EXTERN void ngtcp2_cid_init(ngtcp2_cid *cid, const uint8_t *data,
  1009. size_t datalen);
  1010. /**
  1011. * @function
  1012. *
  1013. * `ngtcp2_cid_eq` returns nonzero if |a| and |b| share the same
  1014. * Connection ID.
  1015. */
  1016. NGTCP2_EXTERN int ngtcp2_cid_eq(const ngtcp2_cid *a, const ngtcp2_cid *b);
  1017. /**
  1018. * @struct
  1019. *
  1020. * :type:`ngtcp2_pkt_hd` represents QUIC packet header.
  1021. */
  1022. typedef struct ngtcp2_pkt_hd {
  1023. /**
  1024. * :member:`dcid` is Destination Connection ID.
  1025. */
  1026. ngtcp2_cid dcid;
  1027. /**
  1028. * :member:`scid` is Source Connection ID.
  1029. */
  1030. ngtcp2_cid scid;
  1031. /**
  1032. * :member:`pkt_num` is a packet number.
  1033. */
  1034. int64_t pkt_num;
  1035. /**
  1036. * :member:`token` contains token. Only Initial packet may contain
  1037. * token. NULL if no token is present.
  1038. */
  1039. const uint8_t *token;
  1040. /**
  1041. * :member:`tokenlen` is the length of :member:`token`. 0 if no
  1042. * token is present.
  1043. */
  1044. size_t tokenlen;
  1045. /**
  1046. * :member:`pkt_numlen` is the number of bytes spent to encode
  1047. * :member:`pkt_num`.
  1048. */
  1049. size_t pkt_numlen;
  1050. /**
  1051. * :member:`len` is the sum of :member:`pkt_numlen` and the length
  1052. * of QUIC packet payload.
  1053. */
  1054. size_t len;
  1055. /**
  1056. * :member:`version` is QUIC version.
  1057. */
  1058. uint32_t version;
  1059. /**
  1060. * :member:`type` is a type of QUIC packet. This field does not
  1061. * have a QUIC packet type defined for a specific QUIC version.
  1062. * Instead, it contains version independent packet type defined by
  1063. * this library. See :type:`ngtcp2_pkt_type`.
  1064. */
  1065. uint8_t type;
  1066. /**
  1067. * :member:`flags` is zero or more of :macro:`NGTCP2_PKT_FLAG_*
  1068. * <NGTCP2_PKT_FLAG_NONE>`.
  1069. */
  1070. uint8_t flags;
  1071. } ngtcp2_pkt_hd;
  1072. /**
  1073. * @struct
  1074. *
  1075. * :type:`ngtcp2_pkt_stateless_reset` represents Stateless Reset.
  1076. */
  1077. typedef struct ngtcp2_pkt_stateless_reset {
  1078. /**
  1079. * :member:`stateless_reset_token` contains stateless reset token.
  1080. */
  1081. uint8_t stateless_reset_token[NGTCP2_STATELESS_RESET_TOKENLEN];
  1082. /**
  1083. * :member:`rand` points a buffer which contains random bytes
  1084. * section.
  1085. */
  1086. const uint8_t *rand;
  1087. /**
  1088. * :member:`randlen` is the number of random bytes.
  1089. */
  1090. size_t randlen;
  1091. } ngtcp2_pkt_stateless_reset;
  1092. /**
  1093. * @macrosection
  1094. *
  1095. * QUIC transport parameters related macros
  1096. */
  1097. /**
  1098. * @macro
  1099. *
  1100. * :macro:`NGTCP2_DEFAULT_MAX_RECV_UDP_PAYLOAD_SIZE` is the default
  1101. * value of max_udp_payload_size transport parameter.
  1102. */
  1103. #define NGTCP2_DEFAULT_MAX_RECV_UDP_PAYLOAD_SIZE 65527
  1104. /**
  1105. * @macro
  1106. *
  1107. * :macro:`NGTCP2_DEFAULT_ACK_DELAY_EXPONENT` is a default value of
  1108. * scaling factor of ACK Delay field in ACK frame.
  1109. */
  1110. #define NGTCP2_DEFAULT_ACK_DELAY_EXPONENT 3
  1111. /**
  1112. * @macro
  1113. *
  1114. * :macro:`NGTCP2_DEFAULT_MAX_ACK_DELAY` is a default value of the
  1115. * maximum amount of time in nanoseconds by which endpoint delays
  1116. * sending acknowledgement.
  1117. */
  1118. #define NGTCP2_DEFAULT_MAX_ACK_DELAY (25 * NGTCP2_MILLISECONDS)
  1119. /**
  1120. * @macro
  1121. *
  1122. * :macro:`NGTCP2_DEFAULT_ACTIVE_CONNECTION_ID_LIMIT` is the default
  1123. * value of active_connection_id_limit transport parameter value if
  1124. * omitted.
  1125. */
  1126. #define NGTCP2_DEFAULT_ACTIVE_CONNECTION_ID_LIMIT 2
  1127. /**
  1128. * @macro
  1129. *
  1130. * :macro:`NGTCP2_TLSEXT_QUIC_TRANSPORT_PARAMETERS_V1` is TLS
  1131. * extension type of quic_transport_parameters.
  1132. */
  1133. #define NGTCP2_TLSEXT_QUIC_TRANSPORT_PARAMETERS_V1 0x39u
  1134. #ifdef NGTCP2_USE_GENERIC_SOCKADDR
  1135. # ifndef NGTCP2_AF_INET
  1136. # error NGTCP2_AF_INET must be defined
  1137. # endif /* !NGTCP2_AF_INET */
  1138. # ifndef NGTCP2_AF_INET6
  1139. # error NGTCP2_AF_INET6 must be defined
  1140. # endif /* !NGTCP2_AF_INET6 */
  1141. typedef unsigned short int ngtcp2_sa_family;
  1142. typedef uint16_t ngtcp2_in_port;
  1143. typedef struct ngtcp2_sockaddr {
  1144. ngtcp2_sa_family sa_family;
  1145. uint8_t sa_data[14];
  1146. } ngtcp2_sockaddr;
  1147. typedef struct ngtcp2_in_addr {
  1148. uint32_t s_addr;
  1149. } ngtcp2_in_addr;
  1150. typedef struct ngtcp2_sockaddr_in {
  1151. ngtcp2_sa_family sin_family;
  1152. ngtcp2_in_port sin_port;
  1153. ngtcp2_in_addr sin_addr;
  1154. uint8_t sin_zero[8];
  1155. } ngtcp2_sockaddr_in;
  1156. typedef struct ngtcp2_in6_addr {
  1157. uint8_t in6_addr[16];
  1158. } ngtcp2_in6_addr;
  1159. typedef struct ngtcp2_sockaddr_in6 {
  1160. ngtcp2_sa_family sin6_family;
  1161. ngtcp2_in_port sin6_port;
  1162. uint32_t sin6_flowinfo;
  1163. ngtcp2_in6_addr sin6_addr;
  1164. uint32_t sin6_scope_id;
  1165. } ngtcp2_sockaddr_in6;
  1166. typedef uint32_t ngtcp2_socklen;
  1167. #else /* !NGTCP2_USE_GENERIC_SOCKADDR */
  1168. # define NGTCP2_AF_INET AF_INET
  1169. # define NGTCP2_AF_INET6 AF_INET6
  1170. /**
  1171. * @typedef
  1172. *
  1173. * :type:`ngtcp2_sockaddr` is typedefed to struct sockaddr. If
  1174. * :macro:`NGTCP2_USE_GENERIC_SOCKADDR` is defined, it is typedefed to
  1175. * the generic struct sockaddr defined in ngtcp2.h.
  1176. */
  1177. typedef struct sockaddr ngtcp2_sockaddr;
  1178. /**
  1179. * @typedef
  1180. *
  1181. * :type:`ngtcp2_sockaddr_in` is typedefed to struct sockaddr_in. If
  1182. * :macro:`NGTCP2_USE_GENERIC_SOCKADDR` is defined, it is typedefed to
  1183. * the generic struct sockaddr_in defined in ngtcp2.h.
  1184. */
  1185. typedef struct sockaddr_in ngtcp2_sockaddr_in;
  1186. /**
  1187. * @typedef
  1188. *
  1189. * :type:`ngtcp2_sockaddr_in6` is typedefed to struct sockaddr_in6.
  1190. * If :macro:`NGTCP2_USE_GENERIC_SOCKADDR` is defined, it is typedefed
  1191. * to the generic struct sockaddr_in6 defined in ngtcp2.h.
  1192. */
  1193. typedef struct sockaddr_in6 ngtcp2_sockaddr_in6;
  1194. /**
  1195. * @typedef
  1196. *
  1197. * :type:`ngtcp2_socklen` is typedefed to socklen_t. If
  1198. * :macro:`NGTCP2_USE_GENERIC_SOCKADDR` is defined, it is typedefed to
  1199. * uint32_t.
  1200. */
  1201. typedef socklen_t ngtcp2_socklen;
  1202. #endif /* !NGTCP2_USE_GENERIC_SOCKADDR */
  1203. /**
  1204. * @struct
  1205. *
  1206. * :type:`ngtcp2_sockaddr_union` conveniently includes all supported
  1207. * address types.
  1208. */
  1209. typedef union ngtcp2_sockaddr_union {
  1210. ngtcp2_sockaddr sa;
  1211. ngtcp2_sockaddr_in in;
  1212. ngtcp2_sockaddr_in6 in6;
  1213. } ngtcp2_sockaddr_union;
  1214. /**
  1215. * @struct
  1216. *
  1217. * :type:`ngtcp2_preferred_addr` represents preferred address
  1218. * structure.
  1219. */
  1220. typedef struct ngtcp2_preferred_addr {
  1221. /**
  1222. * :member:`cid` is a Connection ID.
  1223. */
  1224. ngtcp2_cid cid;
  1225. /**
  1226. * :member:`ipv4` contains IPv4 address and port.
  1227. */
  1228. ngtcp2_sockaddr_in ipv4;
  1229. /**
  1230. * :member:`ipv6` contains IPv6 address and port.
  1231. */
  1232. ngtcp2_sockaddr_in6 ipv6;
  1233. /**
  1234. * :member:`ipv4_present` indicates that :member:`ipv4` contains
  1235. * IPv4 address and port.
  1236. */
  1237. uint8_t ipv4_present;
  1238. /**
  1239. * :member:`ipv6_present` indicates that :member:`ipv6` contains
  1240. * IPv6 address and port.
  1241. */
  1242. uint8_t ipv6_present;
  1243. /**
  1244. * :member:`stateless_reset_token` contains stateless reset token.
  1245. */
  1246. uint8_t stateless_reset_token[NGTCP2_STATELESS_RESET_TOKENLEN];
  1247. } ngtcp2_preferred_addr;
  1248. /**
  1249. * @struct
  1250. *
  1251. * :type:`ngtcp2_version_info` represents version_information
  1252. * structure. See :rfc:`9368`.
  1253. */
  1254. typedef struct ngtcp2_version_info {
  1255. /**
  1256. * :member:`chosen_version` is the version chosen by the sender.
  1257. */
  1258. uint32_t chosen_version;
  1259. /**
  1260. * :member:`available_versions` points the wire image of
  1261. * available_versions field. The each version is therefore in
  1262. * network byte order.
  1263. */
  1264. const uint8_t *available_versions;
  1265. /**
  1266. * :member:`available_versionslen` is the number of bytes pointed by
  1267. * :member:`available_versions`, not the number of versions
  1268. * included.
  1269. */
  1270. size_t available_versionslen;
  1271. } ngtcp2_version_info;
  1272. #define NGTCP2_TRANSPORT_PARAMS_V1 1
  1273. #define NGTCP2_TRANSPORT_PARAMS_VERSION NGTCP2_TRANSPORT_PARAMS_V1
  1274. /**
  1275. * @struct
  1276. *
  1277. * :type:`ngtcp2_transport_params` represents QUIC transport
  1278. * parameters.
  1279. */
  1280. typedef struct ngtcp2_transport_params {
  1281. /**
  1282. * :member:`preferred_addr` contains preferred address if
  1283. * :member:`preferred_addr_present` is nonzero.
  1284. */
  1285. ngtcp2_preferred_addr preferred_addr;
  1286. /**
  1287. * :member:`original_dcid` is the Destination Connection ID field
  1288. * from the first Initial packet from client. Server must specify
  1289. * this field and set :member:`original_dcid_present` to nonzero.
  1290. * It is expected that application knows the original Destination
  1291. * Connection ID even if it sends Retry packet, for example, by
  1292. * including it in retry token. Otherwise, application should not
  1293. * specify this field.
  1294. */
  1295. ngtcp2_cid original_dcid;
  1296. /**
  1297. * :member:`initial_scid` is the Source Connection ID field from the
  1298. * first Initial packet the local endpoint sends. Application
  1299. * should not specify this field. If :member:`initial_scid_present`
  1300. * is set to nonzero, it indicates this field is set.
  1301. */
  1302. ngtcp2_cid initial_scid;
  1303. /**
  1304. * :member:`retry_scid` is the Source Connection ID field from Retry
  1305. * packet. Only server uses this field. If server application
  1306. * received Initial packet with retry token from client, and server
  1307. * successfully verified its token, server application must set
  1308. * Destination Connection ID field from the Initial packet to this
  1309. * field, and set :member:`retry_scid_present` to nonzero. Server
  1310. * application must verify that the Destination Connection ID from
  1311. * Initial packet was sent in Retry packet by, for example,
  1312. * including the Connection ID in a token, or including it in AAD
  1313. * when encrypting a token.
  1314. */
  1315. ngtcp2_cid retry_scid;
  1316. /**
  1317. * :member:`initial_max_stream_data_bidi_local` is the size of flow
  1318. * control window of locally initiated stream. This is the number
  1319. * of bytes that the remote endpoint can send, and the local
  1320. * endpoint must ensure that it has enough buffer to receive them.
  1321. */
  1322. uint64_t initial_max_stream_data_bidi_local;
  1323. /**
  1324. * :member:`initial_max_stream_data_bidi_remote` is the size of flow
  1325. * control window of remotely initiated stream. This is the number
  1326. * of bytes that the remote endpoint can send, and the local
  1327. * endpoint must ensure that it has enough buffer to receive them.
  1328. */
  1329. uint64_t initial_max_stream_data_bidi_remote;
  1330. /**
  1331. * :member:`initial_max_stream_data_uni` is the size of flow control
  1332. * window of remotely initiated unidirectional stream. This is the
  1333. * number of bytes that the remote endpoint can send, and the local
  1334. * endpoint must ensure that it has enough buffer to receive them.
  1335. */
  1336. uint64_t initial_max_stream_data_uni;
  1337. /**
  1338. * :member:`initial_max_data` is the connection level flow control
  1339. * window.
  1340. */
  1341. uint64_t initial_max_data;
  1342. /**
  1343. * :member:`initial_max_streams_bidi` is the number of concurrent
  1344. * streams that the remote endpoint can create.
  1345. */
  1346. uint64_t initial_max_streams_bidi;
  1347. /**
  1348. * :member:`initial_max_streams_uni` is the number of concurrent
  1349. * unidirectional streams that the remote endpoint can create.
  1350. */
  1351. uint64_t initial_max_streams_uni;
  1352. /**
  1353. * :member:`max_idle_timeout` is a duration during which sender
  1354. * allows quiescent. 0 means no idle timeout. It must not be
  1355. * UINT64_MAX.
  1356. */
  1357. ngtcp2_duration max_idle_timeout;
  1358. /**
  1359. * :member:`max_udp_payload_size` is the maximum UDP payload size
  1360. * that the local endpoint can receive.
  1361. */
  1362. uint64_t max_udp_payload_size;
  1363. /**
  1364. * :member:`active_connection_id_limit` is the maximum number of
  1365. * Connection ID that sender can store.
  1366. */
  1367. uint64_t active_connection_id_limit;
  1368. /**
  1369. * :member:`ack_delay_exponent` is the exponent used in ACK Delay
  1370. * field in ACK frame.
  1371. */
  1372. uint64_t ack_delay_exponent;
  1373. /**
  1374. * :member:`max_ack_delay` is the maximum acknowledgement delay by
  1375. * which the local endpoint will delay sending acknowledgements. It
  1376. * must be strictly less than (1 << 14) milliseconds.
  1377. * Sub-millisecond part is dropped when sending it in a QUIC
  1378. * transport parameter.
  1379. */
  1380. ngtcp2_duration max_ack_delay;
  1381. /**
  1382. * :member:`max_datagram_frame_size` is the maximum size of DATAGRAM
  1383. * frame that the local endpoint willingly receives. Specifying 0
  1384. * disables DATAGRAM support. See :rfc:`9221`.
  1385. */
  1386. uint64_t max_datagram_frame_size;
  1387. /**
  1388. * :member:`stateless_reset_token_present` is nonzero if
  1389. * :member:`stateless_reset_token` field is set.
  1390. */
  1391. uint8_t stateless_reset_token_present;
  1392. /**
  1393. * :member:`disable_active_migration` is nonzero if the local
  1394. * endpoint does not support active connection migration.
  1395. */
  1396. uint8_t disable_active_migration;
  1397. /**
  1398. * :member:`original_dcid_present` is nonzero if
  1399. * :member:`original_dcid` field is set.
  1400. */
  1401. uint8_t original_dcid_present;
  1402. /**
  1403. * :member:`initial_scid_present` is nonzero if
  1404. * :member:`initial_scid` field is set.
  1405. */
  1406. uint8_t initial_scid_present;
  1407. /**
  1408. * :member:`retry_scid_present` is nonzero if :member:`retry_scid`
  1409. * field is set.
  1410. */
  1411. uint8_t retry_scid_present;
  1412. /**
  1413. * :member:`preferred_addr_present` is nonzero if
  1414. * :member:`preferred_address` is set.
  1415. */
  1416. uint8_t preferred_addr_present;
  1417. /**
  1418. * :member:`stateless_reset_token` contains stateless reset token.
  1419. */
  1420. uint8_t stateless_reset_token[NGTCP2_STATELESS_RESET_TOKENLEN];
  1421. /**
  1422. * :member:`grease_quic_bit` is nonzero if sender supports "Greasing
  1423. * the QUIC Bit" extension. See :rfc:`9287`.
  1424. */
  1425. uint8_t grease_quic_bit;
  1426. /**
  1427. * :member:`version_info` contains version_information field if
  1428. * :member:`version_info_present` is nonzero. Application should
  1429. * not specify this field.
  1430. */
  1431. ngtcp2_version_info version_info;
  1432. /**
  1433. * :member:`version_info_present` is nonzero if
  1434. * :member:`version_info` is set. Application should not specify
  1435. * this field.
  1436. */
  1437. uint8_t version_info_present;
  1438. } ngtcp2_transport_params;
  1439. #define NGTCP2_CONN_INFO_V1 1
  1440. #define NGTCP2_CONN_INFO_VERSION NGTCP2_CONN_INFO_V1
  1441. /**
  1442. * @struct
  1443. *
  1444. * :type:`ngtcp2_conn_info` holds various connection statistics.
  1445. */
  1446. typedef struct ngtcp2_conn_info {
  1447. /**
  1448. * :member:`latest_rtt` is the latest RTT sample which is not
  1449. * adjusted by acknowledgement delay.
  1450. */
  1451. ngtcp2_duration latest_rtt;
  1452. /**
  1453. * :member:`min_rtt` is the minimum RTT seen so far. It is not
  1454. * adjusted by acknowledgement delay.
  1455. */
  1456. ngtcp2_duration min_rtt;
  1457. /**
  1458. * :member:`smoothed_rtt` is the smoothed RTT.
  1459. */
  1460. ngtcp2_duration smoothed_rtt;
  1461. /**
  1462. * :member:`rttvar` is a mean deviation of observed RTT.
  1463. */
  1464. ngtcp2_duration rttvar;
  1465. /**
  1466. * :member:`cwnd` is the size of congestion window.
  1467. */
  1468. uint64_t cwnd;
  1469. /**
  1470. * :member:`ssthresh` is slow start threshold.
  1471. */
  1472. uint64_t ssthresh;
  1473. /**
  1474. * :member:`bytes_in_flight` is the number in bytes of all sent
  1475. * packets which have not been acknowledged.
  1476. */
  1477. uint64_t bytes_in_flight;
  1478. } ngtcp2_conn_info;
  1479. /**
  1480. * @enum
  1481. *
  1482. * :type:`ngtcp2_cc_algo` defines congestion control algorithms.
  1483. */
  1484. typedef enum ngtcp2_cc_algo {
  1485. /**
  1486. * :enum:`NGTCP2_CC_ALGO_RENO` represents Reno.
  1487. */
  1488. NGTCP2_CC_ALGO_RENO = 0x00,
  1489. /**
  1490. * :enum:`NGTCP2_CC_ALGO_CUBIC` represents Cubic.
  1491. */
  1492. NGTCP2_CC_ALGO_CUBIC = 0x01,
  1493. /**
  1494. * :enum:`NGTCP2_CC_ALGO_BBR` represents BBR v2.
  1495. */
  1496. NGTCP2_CC_ALGO_BBR = 0x02
  1497. } ngtcp2_cc_algo;
  1498. /**
  1499. * @functypedef
  1500. *
  1501. * :type:`ngtcp2_printf` is a callback function for logging.
  1502. * |user_data| is the same object passed to `ngtcp2_conn_client_new`
  1503. * or `ngtcp2_conn_server_new`.
  1504. */
  1505. typedef void (*ngtcp2_printf)(void *user_data, const char *format, ...);
  1506. /**
  1507. * @macrosection
  1508. *
  1509. * QLog related macros
  1510. */
  1511. /**
  1512. * @macro
  1513. *
  1514. * :macro:`NGTCP2_QLOG_WRITE_FLAG_NONE` indicates no flag set.
  1515. */
  1516. #define NGTCP2_QLOG_WRITE_FLAG_NONE 0x00u
  1517. /**
  1518. * @macro
  1519. *
  1520. * :macro:`NGTCP2_QLOG_WRITE_FLAG_FIN` indicates that this is the
  1521. * final call to :type:`ngtcp2_qlog_write` in the current connection.
  1522. */
  1523. #define NGTCP2_QLOG_WRITE_FLAG_FIN 0x01u
  1524. /**
  1525. * @struct
  1526. *
  1527. * :type:`ngtcp2_rand_ctx` is a wrapper around native random number
  1528. * generator. It is opaque to the ngtcp2 library. This might be
  1529. * useful if application needs to specify random number generator per
  1530. * thread or per connection.
  1531. */
  1532. typedef struct ngtcp2_rand_ctx {
  1533. /**
  1534. * :member:`native_handle` is a pointer to an underlying random
  1535. * number generator.
  1536. */
  1537. void *native_handle;
  1538. } ngtcp2_rand_ctx;
  1539. /**
  1540. * @functypedef
  1541. *
  1542. * :type:`ngtcp2_qlog_write` is a callback function which is called to
  1543. * write qlog |data| of length |datalen| bytes. |flags| is bitwise OR
  1544. * of zero or more of :macro:`NGTCP2_QLOG_WRITE_FLAG_*
  1545. * <NGTCP2_QLOG_WRITE_FLAG_NONE>`. If
  1546. * :macro:`NGTCP2_QLOG_WRITE_FLAG_FIN` is set, |datalen| may be 0.
  1547. */
  1548. typedef void (*ngtcp2_qlog_write)(void *user_data, uint32_t flags,
  1549. const void *data, size_t datalen);
  1550. /**
  1551. * @enum
  1552. *
  1553. * :type:`ngtcp2_token_type` defines the type of token.
  1554. */
  1555. typedef enum ngtcp2_token_type {
  1556. /**
  1557. * :enum:`NGTCP2_TOKEN_TYPE_UNKNOWN` indicates that the type of
  1558. * token is unknown.
  1559. */
  1560. NGTCP2_TOKEN_TYPE_UNKNOWN,
  1561. /**
  1562. * :enum:`NGTCP2_TOKEN_TYPE_RETRY` indicates that a token comes from
  1563. * Retry packet.
  1564. */
  1565. NGTCP2_TOKEN_TYPE_RETRY,
  1566. /**
  1567. * :enum:`NGTCP2_TOKEN_TYPE_NEW_TOKEN` indicates that a token comes
  1568. * from NEW_TOKEN frame.
  1569. */
  1570. NGTCP2_TOKEN_TYPE_NEW_TOKEN
  1571. } ngtcp2_token_type;
  1572. #define NGTCP2_SETTINGS_V1 1
  1573. #define NGTCP2_SETTINGS_VERSION NGTCP2_SETTINGS_V1
  1574. /**
  1575. * @struct
  1576. *
  1577. * :type:`ngtcp2_settings` defines QUIC connection settings.
  1578. */
  1579. typedef struct ngtcp2_settings {
  1580. /**
  1581. * :member:`qlog_write` is a callback function to write qlog.
  1582. * Setting ``NULL`` disables qlog.
  1583. */
  1584. ngtcp2_qlog_write qlog_write;
  1585. /**
  1586. * :member:`cc_algo` specifies congestion control algorithm.
  1587. */
  1588. ngtcp2_cc_algo cc_algo;
  1589. /**
  1590. * :member:`initial_ts` is an initial timestamp given to the
  1591. * library.
  1592. */
  1593. ngtcp2_tstamp initial_ts;
  1594. /**
  1595. * :member:`initial_rtt` is an initial RTT.
  1596. */
  1597. ngtcp2_duration initial_rtt;
  1598. /**
  1599. * :member:`log_printf` is a function that the library uses to write
  1600. * logs. ``NULL`` means no logging output. It is nothing to do
  1601. * with qlog.
  1602. */
  1603. ngtcp2_printf log_printf;
  1604. /**
  1605. * :member:`max_tx_udp_payload_size` is the maximum size of UDP
  1606. * datagram payload that the local endpoint transmits. It is used
  1607. * by congestion controller to compute congestion window.
  1608. */
  1609. size_t max_tx_udp_payload_size;
  1610. /**
  1611. * :member:`token` is a token from Retry packet or NEW_TOKEN frame.
  1612. *
  1613. * Server sets this field if it received the token in Client Initial
  1614. * packet and successfully validated. It should also set
  1615. * :member:`token_type` field.
  1616. *
  1617. * Client sets this field if it intends to send token in its Initial
  1618. * packet.
  1619. *
  1620. * `ngtcp2_conn_server_new` and `ngtcp2_conn_client_new` make a copy
  1621. * of token.
  1622. *
  1623. * Set NULL if there is no token.
  1624. */
  1625. const uint8_t *token;
  1626. /**
  1627. * :member:`tokenlen` is the length of :member:`token`. Set 0 if
  1628. * there is no token.
  1629. */
  1630. size_t tokenlen;
  1631. /**
  1632. * :member:`token_type` is the type of token. Server application
  1633. * should set this field.
  1634. */
  1635. ngtcp2_token_type token_type;
  1636. /**
  1637. * :member:`rand_ctx` is an optional random number generator to be
  1638. * passed to :type:`ngtcp2_rand` callback.
  1639. */
  1640. ngtcp2_rand_ctx rand_ctx;
  1641. /**
  1642. * :member:`max_window` is the maximum connection-level flow control
  1643. * window if connection-level window auto-tuning is enabled. The
  1644. * connection-level window auto tuning is enabled if nonzero value
  1645. * is specified in this field. The initial value of window size is
  1646. * :member:`ngtcp2_transport_params.initial_max_data`. The window
  1647. * size is scaled up to the value specified in this field.
  1648. */
  1649. uint64_t max_window;
  1650. /**
  1651. * :member:`max_stream_window` is the maximum stream-level flow
  1652. * control window if stream-level window auto-tuning is enabled.
  1653. * The stream-level window auto-tuning is enabled if nonzero value
  1654. * is specified in this field. The initial value of window size is
  1655. * :member:`ngtcp2_transport_params.initial_max_stream_data_bidi_remote`,
  1656. * :member:`ngtcp2_transport_params.initial_max_stream_data_bidi_local`,
  1657. * or :member:`ngtcp2_transport_params.initial_max_stream_data_uni`,
  1658. * depending on the type of stream. The window size is scaled up to
  1659. * the value specified in this field.
  1660. */
  1661. uint64_t max_stream_window;
  1662. /**
  1663. * :member:`ack_thresh` is the minimum number of the received ACK
  1664. * eliciting packets that trigger the immediate acknowledgement from
  1665. * the local endpoint.
  1666. */
  1667. size_t ack_thresh;
  1668. /**
  1669. * :member:`no_tx_udp_payload_size_shaping`, if set to nonzero,
  1670. * instructs the library not to limit the UDP payload size to
  1671. * :macro:`NGTCP2_MAX_UDP_PAYLOAD_SIZE` (which can be extended by
  1672. * Path MTU Discovery), and instead use the minimum size among the
  1673. * given buffer size, :member:`max_tx_udp_payload_size`, and the
  1674. * received max_udp_payload_size QUIC transport parameter.
  1675. */
  1676. uint8_t no_tx_udp_payload_size_shaping;
  1677. /**
  1678. * :member:`handshake_timeout` is the period of time before giving
  1679. * up QUIC connection establishment. If QUIC handshake is not
  1680. * complete within this period, `ngtcp2_conn_handle_expiry` returns
  1681. * :macro:`NGTCP2_ERR_HANDSHAKE_TIMEOUT` error. The deadline is
  1682. * :member:`initial_ts` + :member:`handshake_timeout`. If this
  1683. * field is set to ``UINT64_MAX``, no handshake timeout is set.
  1684. */
  1685. ngtcp2_duration handshake_timeout;
  1686. /**
  1687. * :member:`preferred_versions` is the array of versions that are
  1688. * preferred by the local endpoint. All versions set in this array
  1689. * must be supported by the library, and compatible to QUIC v1. The
  1690. * reserved versions are not allowed. They are sorted in the order
  1691. * of preference.
  1692. *
  1693. * On compatible version negotiation, server will negotiate one of
  1694. * those versions contained in this array if there is some overlap
  1695. * between these versions and the versions offered by the client.
  1696. * If there is no overlap, but the client chosen version is
  1697. * supported by the library, the server chooses the client chosen
  1698. * version as the negotiated version. This version set corresponds
  1699. * to Offered Versions described in :rfc:`9368`, and it should be
  1700. * included in Version Negotiation packet.
  1701. *
  1702. * Client uses this field and :member:`original_version` to prevent
  1703. * version downgrade attack if it reacted upon Version Negotiation
  1704. * packet. If this field is specified, client must include
  1705. * |client_chosen_version| passed to `ngtcp2_conn_client_new` unless
  1706. * |client_chosen_version| is a reserved version.
  1707. */
  1708. const uint32_t *preferred_versions;
  1709. /**
  1710. * :member:`preferred_versionslen` is the number of versions that
  1711. * are contained in the array pointed by
  1712. * :member:`preferred_versions`.
  1713. */
  1714. size_t preferred_versionslen;
  1715. /**
  1716. * :member:`available_versions` is the array of versions that are
  1717. * going to be set in :member:`available_versions
  1718. * <ngtcp2_version_info.available_versions>` field of outgoing
  1719. * version_information QUIC transport parameter.
  1720. *
  1721. * For server, this corresponds to Fully-Deployed Versions described
  1722. * in :rfc:`9368`. If this field is not set, it is set to
  1723. * :member:`preferred_versions` internally if
  1724. * :member:`preferred_versionslen` is not zero. If this field is
  1725. * not set, and :member:`preferred_versionslen` is zero, this field
  1726. * is set to :macro:`NGTCP2_PROTO_VER_V1` internally.
  1727. *
  1728. * Client must include |client_chosen_version| passed to
  1729. * `ngtcp2_conn_client_new` in this array if this field is set and
  1730. * |client_chosen_version| is not a reserved version. If this field
  1731. * is not set, |client_chosen_version| passed to
  1732. * `ngtcp2_conn_client_new` will be set in this field internally
  1733. * unless |client_chosen_version| is a reserved version.
  1734. */
  1735. const uint32_t *available_versions;
  1736. /**
  1737. * :member:`available_versionslen` is the number of versions that
  1738. * are contained in the array pointed by
  1739. * :member:`available_versions`.
  1740. */
  1741. size_t available_versionslen;
  1742. /**
  1743. * :member:`original_version` is the original version that client
  1744. * initially used to make a connection attempt. If it is set, and
  1745. * it differs from |client_chosen_version| passed to
  1746. * `ngtcp2_conn_client_new`, the library assumes that client reacted
  1747. * upon Version Negotiation packet. Server does not use this field.
  1748. */
  1749. uint32_t original_version;
  1750. /**
  1751. * :member:`no_pmtud`, if set to nonzero, disables Path MTU
  1752. * Discovery.
  1753. */
  1754. uint8_t no_pmtud;
  1755. /**
  1756. * :member:`pkt_num` is the initial packet number for each packet
  1757. * number space. It must be in range [0, INT32_MAX], inclusive.
  1758. */
  1759. uint32_t initial_pkt_num;
  1760. } ngtcp2_settings;
  1761. /**
  1762. * @struct
  1763. *
  1764. * :type:`ngtcp2_addr` is the endpoint address.
  1765. */
  1766. typedef struct ngtcp2_addr {
  1767. /**
  1768. * :member:`addr` points to the buffer which contains endpoint
  1769. * address. It must not be ``NULL``.
  1770. */
  1771. ngtcp2_sockaddr *addr;
  1772. /**
  1773. * :member:`addrlen` is the length of :member:`addr`. It must not
  1774. * be longer than sizeof(:type:`ngtcp2_sockaddr_union`).
  1775. */
  1776. ngtcp2_socklen addrlen;
  1777. } ngtcp2_addr;
  1778. /**
  1779. * @struct
  1780. *
  1781. * :type:`ngtcp2_path` is the network endpoints where a packet is sent
  1782. * and received.
  1783. */
  1784. typedef struct ngtcp2_path {
  1785. /**
  1786. * :member:`local` is the address of local endpoint.
  1787. */
  1788. ngtcp2_addr local;
  1789. /**
  1790. * :member:`remote` is the address of remote endpoint.
  1791. */
  1792. ngtcp2_addr remote;
  1793. /**
  1794. * :member:`user_data` is an arbitrary data and opaque to the
  1795. * library.
  1796. *
  1797. * Note that :type:`ngtcp2_path` is generally passed to
  1798. * :type:`ngtcp2_conn` by an application, and :type:`ngtcp2_conn`
  1799. * stores their copies. Unfortunately, there is no way for the
  1800. * application to know when :type:`ngtcp2_conn` finished using a
  1801. * specific :type:`ngtcp2_path` object in mid connection, which
  1802. * means that the application cannot free the data pointed by this
  1803. * field. Therefore, it is advised to use this field only when the
  1804. * data pointed by this field persists in an entire lifetime of the
  1805. * connection.
  1806. */
  1807. void *user_data;
  1808. } ngtcp2_path;
  1809. /**
  1810. * @struct
  1811. *
  1812. * :type:`ngtcp2_path_storage` is a convenient struct to have buffers
  1813. * to store the longest addresses.
  1814. */
  1815. typedef struct ngtcp2_path_storage {
  1816. /**
  1817. * :member:`path` stores network path.
  1818. */
  1819. ngtcp2_path path;
  1820. /**
  1821. * :member:`local_addrbuf` is a buffer to store local address.
  1822. */
  1823. ngtcp2_sockaddr_union local_addrbuf;
  1824. /**
  1825. * :member:`remote_addrbuf` is a buffer to store remote address.
  1826. */
  1827. ngtcp2_sockaddr_union remote_addrbuf;
  1828. } ngtcp2_path_storage;
  1829. /**
  1830. * @struct
  1831. *
  1832. * :type:`ngtcp2_crypto_md` is a wrapper around native message digest
  1833. * object.
  1834. */
  1835. typedef struct ngtcp2_crypto_md {
  1836. /**
  1837. * :member:`native_handle` is a pointer to an underlying message
  1838. * digest object.
  1839. */
  1840. void *native_handle;
  1841. } ngtcp2_crypto_md;
  1842. /**
  1843. * @struct
  1844. *
  1845. * :type:`ngtcp2_crypto_aead` is a wrapper around native AEAD object.
  1846. */
  1847. typedef struct ngtcp2_crypto_aead {
  1848. /**
  1849. * :member:`native_handle` is a pointer to an underlying AEAD
  1850. * object.
  1851. */
  1852. void *native_handle;
  1853. /**
  1854. * :member:`max_overhead` is the number of additional bytes which
  1855. * AEAD encryption needs on encryption.
  1856. */
  1857. size_t max_overhead;
  1858. } ngtcp2_crypto_aead;
  1859. /**
  1860. * @struct
  1861. *
  1862. * :type:`ngtcp2_crypto_cipher` is a wrapper around native cipher
  1863. * object.
  1864. */
  1865. typedef struct ngtcp2_crypto_cipher {
  1866. /**
  1867. * :member:`native_handle` is a pointer to an underlying cipher
  1868. * object.
  1869. */
  1870. void *native_handle;
  1871. } ngtcp2_crypto_cipher;
  1872. /**
  1873. * @struct
  1874. *
  1875. * :type:`ngtcp2_crypto_aead_ctx` is a wrapper around native AEAD
  1876. * cipher context object. It should be initialized with a specific
  1877. * key. ngtcp2 library reuses this context object to encrypt or
  1878. * decrypt multiple packets.
  1879. */
  1880. typedef struct ngtcp2_crypto_aead_ctx {
  1881. /**
  1882. * :member:`native_handle` is a pointer to an underlying AEAD
  1883. * context object.
  1884. */
  1885. void *native_handle;
  1886. } ngtcp2_crypto_aead_ctx;
  1887. /**
  1888. * @struct
  1889. *
  1890. * :type:`ngtcp2_crypto_cipher_ctx` is a wrapper around native cipher
  1891. * context object. It should be initialized with a specific key.
  1892. * ngtcp2 library reuses this context object to encrypt or decrypt
  1893. * multiple packet headers.
  1894. */
  1895. typedef struct ngtcp2_crypto_cipher_ctx {
  1896. /**
  1897. * :member:`native_handle` is a pointer to an underlying cipher
  1898. * context object.
  1899. */
  1900. void *native_handle;
  1901. } ngtcp2_crypto_cipher_ctx;
  1902. /**
  1903. * @struct
  1904. *
  1905. * :type:`ngtcp2_crypto_ctx` is a convenient structure to bind all
  1906. * crypto related objects in one place. Use
  1907. * `ngtcp2_crypto_ctx_initial` to initialize this struct for Initial
  1908. * packet encryption. For Handshake and 1-RTT packets, use
  1909. * `ngtcp2_crypto_ctx_tls`. For 0-RTT packets, use
  1910. * `ngtcp2_crypto_ctx_tls_early`.
  1911. */
  1912. typedef struct ngtcp2_crypto_ctx {
  1913. /**
  1914. * :member:`aead` is AEAD object.
  1915. */
  1916. ngtcp2_crypto_aead aead;
  1917. /**
  1918. * :member:`md` is message digest object.
  1919. */
  1920. ngtcp2_crypto_md md;
  1921. /**
  1922. * :member:`hp` is header protection cipher.
  1923. */
  1924. ngtcp2_crypto_cipher hp;
  1925. /**
  1926. * :member:`max_encryption` is the number of encryption which this
  1927. * key can be used with.
  1928. */
  1929. uint64_t max_encryption;
  1930. /**
  1931. * :member:`max_decryption_failure` is the number of decryption
  1932. * failure with this key.
  1933. */
  1934. uint64_t max_decryption_failure;
  1935. } ngtcp2_crypto_ctx;
  1936. /**
  1937. * @function
  1938. *
  1939. * `ngtcp2_transport_params_encode` encodes |params| in |dest| of
  1940. * length |destlen|.
  1941. *
  1942. * If |dest| is NULL, and |destlen| is zero, this function just
  1943. * returns the number of bytes required to store the encoded transport
  1944. * parameters.
  1945. *
  1946. * This function returns the number of bytes written, or one of the
  1947. * following negative error codes:
  1948. *
  1949. * :macro:`NGTCP2_ERR_NOBUF`
  1950. * Buffer is too small.
  1951. */
  1952. NGTCP2_EXTERN ngtcp2_ssize ngtcp2_transport_params_encode_versioned(
  1953. uint8_t *dest, size_t destlen, int transport_params_version,
  1954. const ngtcp2_transport_params *params);
  1955. /**
  1956. * @function
  1957. *
  1958. * `ngtcp2_transport_params_decode` decodes transport parameters in
  1959. * |data| of length |datalen|, and stores the result in the object
  1960. * pointed by |params|.
  1961. *
  1962. * If an optional parameter is missing, the default value is assigned.
  1963. *
  1964. * The following fields may point to somewhere inside the buffer
  1965. * pointed by |data| of length |datalen|:
  1966. *
  1967. * - :member:`ngtcp2_transport_params.version_info.available_versions
  1968. * <ngtcp2_version_info.available_versions>`
  1969. *
  1970. * This function returns 0 if it succeeds, or one of the following
  1971. * negative error codes:
  1972. *
  1973. * :macro:`NGTCP2_ERR_MALFORMED_TRANSPORT_PARAM`
  1974. * The input is malformed.
  1975. */
  1976. NGTCP2_EXTERN int
  1977. ngtcp2_transport_params_decode_versioned(int transport_params_version,
  1978. ngtcp2_transport_params *params,
  1979. const uint8_t *data, size_t datalen);
  1980. /**
  1981. * @function
  1982. *
  1983. * `ngtcp2_transport_params_decode_new` decodes transport parameters
  1984. * in |data| of length |datalen|, and stores the result in the object
  1985. * allocated dynamically. The pointer to the allocated object is
  1986. * assigned to |*pparams|. Unlike `ngtcp2_transport_params_decode`,
  1987. * all direct and indirect fields are also allocated dynamically if
  1988. * needed.
  1989. *
  1990. * |mem| is a memory allocator to allocate memory. If |mem| is
  1991. * ``NULL``, the memory allocator returned by `ngtcp2_mem_default()`
  1992. * is used.
  1993. *
  1994. * If the optional parameters are missing, the default value is
  1995. * assigned.
  1996. *
  1997. * `ngtcp2_transport_params_del` frees the memory allocated by this
  1998. * function.
  1999. *
  2000. * This function returns 0 if it succeeds, or one of the following
  2001. * negative error codes:
  2002. *
  2003. * :macro:`NGTCP2_ERR_MALFORMED_TRANSPORT_PARAM`
  2004. * The input is malformed.
  2005. * :macro:`NGTCP2_ERR_NOMEM`
  2006. * Out of memory.
  2007. */
  2008. NGTCP2_EXTERN int
  2009. ngtcp2_transport_params_decode_new(ngtcp2_transport_params **pparams,
  2010. const uint8_t *data, size_t datalen,
  2011. const ngtcp2_mem *mem);
  2012. /**
  2013. * @function
  2014. *
  2015. * `ngtcp2_transport_params_del` frees the |params| which must be
  2016. * dynamically allocated by `ngtcp2_transport_params_decode_new`.
  2017. *
  2018. * |mem| is a memory allocator that allocated |params|. If |mem| is
  2019. * ``NULL``, the memory allocator returned by `ngtcp2_mem_default()`
  2020. * is used.
  2021. *
  2022. * If |params| is ``NULL``, this function does nothing.
  2023. */
  2024. NGTCP2_EXTERN void ngtcp2_transport_params_del(ngtcp2_transport_params *params,
  2025. const ngtcp2_mem *mem);
  2026. /**
  2027. * @struct
  2028. *
  2029. * :type:`ngtcp2_version_cid` is a convenient struct to store the
  2030. * result of `ngtcp2_pkt_decode_version_cid`.
  2031. */
  2032. typedef struct ngtcp2_version_cid {
  2033. /**
  2034. * :member:`version` stores QUIC version.
  2035. */
  2036. uint32_t version;
  2037. /**
  2038. * :member:`dcid` points to the Destination Connection ID.
  2039. */
  2040. const uint8_t *dcid;
  2041. /**
  2042. * :member:`dcidlen` is the length of the Destination Connection ID
  2043. * pointed by :member:`dcid`.
  2044. */
  2045. size_t dcidlen;
  2046. /**
  2047. * :member:`scid` points to the Source Connection ID.
  2048. */
  2049. const uint8_t *scid;
  2050. /**
  2051. * :member:`scidlen` is the length of the Source Connection ID
  2052. * pointed by :member:`scid`.
  2053. */
  2054. size_t scidlen;
  2055. } ngtcp2_version_cid;
  2056. /**
  2057. * @function
  2058. *
  2059. * `ngtcp2_pkt_decode_version_cid` extracts QUIC version, Destination
  2060. * Connection ID and Source Connection ID from the packet pointed by
  2061. * |data| of length |datalen|. This function can handle Connection ID
  2062. * up to 255 bytes unlike `ngtcp2_pkt_decode_hd_long` or
  2063. * `ngtcp2_pkt_decode_hd_short` which are only capable of handling
  2064. * Connection ID less than or equal to :macro:`NGTCP2_MAX_CIDLEN`.
  2065. * Longer Connection ID is only valid if the version is unsupported
  2066. * QUIC version.
  2067. *
  2068. * If the given packet is Long header packet, this function extracts
  2069. * the version from the packet, and assigns it to
  2070. * :member:`dest->version <ngtcp2_version_cid.version>`. It also
  2071. * extracts the pointer to the Destination Connection ID and its
  2072. * length, and assigns them to :member:`dest->dcid
  2073. * <ngtcp2_version_cid.dcid>` and :member:`dest->dcidlen
  2074. * <ngtcp2_version_cid.dcidlen>` respectively. Similarly, it extracts
  2075. * the pointer to the Source Connection ID and its length, and assigns
  2076. * them to :member:`dest->scid <ngtcp2_version_cid.scid>` and
  2077. * :member:`dest->scidlen <ngtcp2_version_cid.scidlen>` respectively.
  2078. * |short_dcidlen| is ignored.
  2079. *
  2080. * If the given packet is Short header packet, :member:`dest->version
  2081. * <ngtcp2_version_cid.version>` will be 0, :member:`dest->scid
  2082. * <ngtcp2_version_cid.scid>` will be ``NULL``, and
  2083. * :member:`dest->scidlen <ngtcp2_version_cid.scidlen>` will be 0.
  2084. * Because the Short header packet does not have the length of
  2085. * Destination Connection ID, the caller has to pass the length in
  2086. * |short_dcidlen|. This function extracts the pointer to the
  2087. * Destination Connection ID, and assigns it to :member:`dest->dcid
  2088. * <ngtcp2_version_cid.dcid>`. |short_dcidlen| is assigned to
  2089. * :member:`dest->dcidlen <ngtcp2_version_cid.dcidlen>`.
  2090. *
  2091. * If Version Negotiation is required, this function returns
  2092. * :macro:`NGTCP2_ERR_VERSION_NEGOTIATION`. Unlike the other error
  2093. * cases, all fields of |dest| are assigned as described above.
  2094. *
  2095. * This function returns 0 if it succeeds. Otherwise, one of the
  2096. * following negative error code:
  2097. *
  2098. * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
  2099. * The function could not decode the packet header.
  2100. * :macro:`NGTCP2_ERR_VERSION_NEGOTIATION`
  2101. * Version Negotiation packet should be sent.
  2102. */
  2103. NGTCP2_EXTERN int ngtcp2_pkt_decode_version_cid(ngtcp2_version_cid *dest,
  2104. const uint8_t *data,
  2105. size_t datalen,
  2106. size_t short_dcidlen);
  2107. /**
  2108. * @function
  2109. *
  2110. * `ngtcp2_pkt_decode_hd_long` decodes QUIC long packet header in
  2111. * |pkt| of length |pktlen|. This function only parses the input just
  2112. * before packet number field.
  2113. *
  2114. * This function does not verify that length field is correct. In
  2115. * other words, this function succeeds even if length > |pktlen|.
  2116. *
  2117. * This function can handle Connection ID up to
  2118. * :macro:`NGTCP2_MAX_CIDLEN`. Consider to use
  2119. * `ngtcp2_pkt_decode_version_cid` to get longer Connection ID.
  2120. *
  2121. * This function handles Version Negotiation specially. If version
  2122. * field is 0, |pkt| must contain Version Negotiation packet. Version
  2123. * Negotiation packet has random type in wire format. For
  2124. * convenience, this function sets
  2125. * :enum:`ngtcp2_pkt_type.NGTCP2_PKT_VERSION_NEGOTIATION` to
  2126. * :member:`dest->type <ngtcp2_pkt_hd.type>`, clears
  2127. * :macro:`NGTCP2_PKT_FLAG_LONG_FORM` flag from :member:`dest->flags
  2128. * <ngtcp2_pkt_hd.flags>`, and sets 0 to :member:`dest->len
  2129. * <ngtcp2_pkt_hd.len>`. Version Negotiation packet occupies a single
  2130. * packet.
  2131. *
  2132. * It stores the result in the object pointed by |dest|, and returns
  2133. * the number of bytes decoded to read the packet header if it
  2134. * succeeds, or one of the following error codes:
  2135. *
  2136. * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
  2137. * Packet is too short; or it is not a long header
  2138. */
  2139. NGTCP2_EXTERN ngtcp2_ssize ngtcp2_pkt_decode_hd_long(ngtcp2_pkt_hd *dest,
  2140. const uint8_t *pkt,
  2141. size_t pktlen);
  2142. /**
  2143. * @function
  2144. *
  2145. * `ngtcp2_pkt_decode_hd_short` decodes QUIC short header in |pkt| of
  2146. * length |pktlen|. Short header packet does not encode the length of
  2147. * Connection ID, thus we need the input from the outside. |dcidlen|
  2148. * is the length of Destination Connection ID in packet header. This
  2149. * function only parses the input just before packet number field.
  2150. * This function can handle Connection ID up to
  2151. * :macro:`NGTCP2_MAX_CIDLEN`. Consider to use
  2152. * `ngtcp2_pkt_decode_version_cid` to get longer Connection ID. It
  2153. * stores the result in the object pointed by |dest|, and returns the
  2154. * number of bytes decoded to read the packet header if it succeeds,
  2155. * or one of the following error codes:
  2156. *
  2157. * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
  2158. * Packet is too short; or it is not a short header
  2159. */
  2160. NGTCP2_EXTERN ngtcp2_ssize ngtcp2_pkt_decode_hd_short(ngtcp2_pkt_hd *dest,
  2161. const uint8_t *pkt,
  2162. size_t pktlen,
  2163. size_t dcidlen);
  2164. /**
  2165. * @function
  2166. *
  2167. * `ngtcp2_pkt_write_stateless_reset` writes Stateless Reset packet in
  2168. * the buffer pointed by |dest| whose length is |destlen|.
  2169. * |stateless_reset_token| is a pointer to the Stateless Reset Token,
  2170. * and its length must be :macro:`NGTCP2_STATELESS_RESET_TOKENLEN`
  2171. * bytes long. |rand| specifies the random octets preceding Stateless
  2172. * Reset Token. The length of |rand| is specified by |randlen| which
  2173. * must be at least :macro:`NGTCP2_MIN_STATELESS_RESET_RANDLEN` bytes
  2174. * long.
  2175. *
  2176. * If |randlen| is too long to write them all in the buffer, |rand| is
  2177. * written to the buffer as much as possible, and is truncated.
  2178. *
  2179. * This function returns the number of bytes written to the buffer, or
  2180. * one of the following negative error codes:
  2181. *
  2182. * :macro:`NGTCP2_ERR_NOBUF`
  2183. * Buffer is too small.
  2184. * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
  2185. * |randlen| is strictly less than
  2186. * :macro:`NGTCP2_MIN_STATELESS_RESET_RANDLEN`.
  2187. */
  2188. NGTCP2_EXTERN ngtcp2_ssize ngtcp2_pkt_write_stateless_reset(
  2189. uint8_t *dest, size_t destlen, const uint8_t *stateless_reset_token,
  2190. const uint8_t *rand, size_t randlen);
  2191. /**
  2192. * @function
  2193. *
  2194. * `ngtcp2_pkt_write_version_negotiation` writes Version Negotiation
  2195. * packet in the buffer pointed by |dest| whose length is |destlen|.
  2196. * |unused_random| should be generated randomly. |dcid| is a
  2197. * Connection ID which appeared in a packet as a Source Connection ID
  2198. * sent by client which caused version negotiation. Similarly, |scid|
  2199. * is a Connection ID which appeared in a packet as a Destination
  2200. * Connection ID sent by client. |sv| is a list of supported
  2201. * versions, and |nsv| specifies the number of supported versions
  2202. * included in |sv|.
  2203. *
  2204. * This function returns the number of bytes written to the buffer, or
  2205. * one of the following negative error codes:
  2206. *
  2207. * :macro:`NGTCP2_ERR_NOBUF`
  2208. * Buffer is too small.
  2209. */
  2210. NGTCP2_EXTERN ngtcp2_ssize ngtcp2_pkt_write_version_negotiation(
  2211. uint8_t *dest, size_t destlen, uint8_t unused_random, const uint8_t *dcid,
  2212. size_t dcidlen, const uint8_t *scid, size_t scidlen, const uint32_t *sv,
  2213. size_t nsv);
  2214. /**
  2215. * @struct
  2216. *
  2217. * :type:`ngtcp2_conn` represents a single QUIC connection.
  2218. */
  2219. typedef struct ngtcp2_conn ngtcp2_conn;
  2220. /**
  2221. * @functypedef
  2222. *
  2223. * :type:`ngtcp2_client_initial` is invoked when client application
  2224. * asks TLS stack to produce first TLS cryptographic handshake data.
  2225. *
  2226. * This implementation of this callback must get the first handshake
  2227. * data from TLS stack, and pass it to ngtcp2 library using
  2228. * `ngtcp2_conn_submit_crypto_data` function. Make sure that before
  2229. * calling `ngtcp2_conn_submit_crypto_data` function, client
  2230. * application must create initial packet protection keys and IVs, and
  2231. * provide them to ngtcp2 library using
  2232. * `ngtcp2_conn_install_initial_key`.
  2233. *
  2234. * This callback function must return 0 if it succeeds, or
  2235. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` which makes the library call
  2236. * return immediately.
  2237. */
  2238. typedef int (*ngtcp2_client_initial)(ngtcp2_conn *conn, void *user_data);
  2239. /**
  2240. * @functypedef
  2241. *
  2242. * :type:`ngtcp2_recv_client_initial` is invoked when server receives
  2243. * Initial packet from client. An server application must implement
  2244. * this callback, and generate initial keys and IVs for both
  2245. * transmission and reception. Install them using
  2246. * `ngtcp2_conn_install_initial_key`. |dcid| is the Destination
  2247. * Connection ID in Initial packet received from client. It is used
  2248. * to derive initial packet protection keys.
  2249. *
  2250. * The callback function must return 0 if it succeeds. If an error
  2251. * occurs, return :macro:`NGTCP2_ERR_CALLBACK_FAILURE` which makes the
  2252. * library call return immediately.
  2253. */
  2254. typedef int (*ngtcp2_recv_client_initial)(ngtcp2_conn *conn,
  2255. const ngtcp2_cid *dcid,
  2256. void *user_data);
  2257. /**
  2258. * @enum
  2259. *
  2260. * :type:`ngtcp2_encryption_level` is QUIC encryption level.
  2261. */
  2262. typedef enum ngtcp2_encryption_level {
  2263. /**
  2264. * :enum:`NGTCP2_ENCRYPTION_LEVEL_INITIAL` is Initial encryption
  2265. * level.
  2266. */
  2267. NGTCP2_ENCRYPTION_LEVEL_INITIAL,
  2268. /**
  2269. * :enum:`NGTCP2_ENCRYPTION_LEVEL_HANDSHAKE` is Handshake encryption
  2270. * level.
  2271. */
  2272. NGTCP2_ENCRYPTION_LEVEL_HANDSHAKE,
  2273. /**
  2274. * :enum:`NGTCP2_ENCRYPTION_LEVEL_1RTT` is 1-RTT encryption level.
  2275. */
  2276. NGTCP2_ENCRYPTION_LEVEL_1RTT,
  2277. /**
  2278. * :enum:`NGTCP2_ENCRYPTION_LEVEL_0RTT` is 0-RTT encryption level.
  2279. */
  2280. NGTCP2_ENCRYPTION_LEVEL_0RTT
  2281. } ngtcp2_encryption_level;
  2282. /**
  2283. * @functypedef
  2284. *
  2285. * :type`ngtcp2_recv_crypto_data` is invoked when crypto data is
  2286. * received. The received data is pointed by |data|, and its length
  2287. * is |datalen|. The |offset| specifies the offset where |data| is
  2288. * positioned. |user_data| is the arbitrary pointer passed to
  2289. * `ngtcp2_conn_client_new` or `ngtcp2_conn_server_new`. The ngtcp2
  2290. * library ensures that the crypto data is passed to the application
  2291. * in the increasing order of |offset|. |datalen| is always strictly
  2292. * greater than 0. |encryption_level| indicates the encryption level
  2293. * where this data is received. Crypto data can never be received in
  2294. * :enum:`ngtcp2_encryption_level.NGTCP2_ENCRYPTION_LEVEL_0RTT`.
  2295. *
  2296. * The application should provide the given data to TLS stack.
  2297. *
  2298. * The callback function must return 0 if it succeeds, or one of the
  2299. * following negative error codes:
  2300. *
  2301. * - :macro:`NGTCP2_ERR_CRYPTO`
  2302. * - :macro:`NGTCP2_ERR_REQUIRED_TRANSPORT_PARAM`
  2303. * - :macro:`NGTCP2_ERR_MALFORMED_TRANSPORT_PARAM`
  2304. * - :macro:`NGTCP2_ERR_TRANSPORT_PARAM`
  2305. * - :macro:`NGTCP2_ERR_PROTO`
  2306. * - :macro:`NGTCP2_ERR_VERSION_NEGOTIATION_FAILURE`
  2307. * - :macro:`NGTCP2_ERR_NOMEM`
  2308. * - :macro:`NGTCP2_ERR_CALLBACK_FAILURE`
  2309. *
  2310. * If the other value is returned, it is treated as
  2311. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE`.
  2312. *
  2313. * If application encounters fatal error, return
  2314. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` which makes the library call
  2315. * return immediately.
  2316. */
  2317. typedef int (*ngtcp2_recv_crypto_data)(ngtcp2_conn *conn,
  2318. ngtcp2_encryption_level encryption_level,
  2319. uint64_t offset, const uint8_t *data,
  2320. size_t datalen, void *user_data);
  2321. /**
  2322. * @functypedef
  2323. *
  2324. * :type:`ngtcp2_handshake_completed` is invoked when QUIC
  2325. * cryptographic handshake has completed.
  2326. *
  2327. * The callback function must return 0 if it succeeds. Returning
  2328. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
  2329. * immediately.
  2330. */
  2331. typedef int (*ngtcp2_handshake_completed)(ngtcp2_conn *conn, void *user_data);
  2332. /**
  2333. * @functypedef
  2334. *
  2335. * :type:`ngtcp2_handshake_confirmed` is invoked when QUIC
  2336. * cryptographic handshake is confirmed. The handshake confirmation
  2337. * means that both endpoints agree that handshake has finished.
  2338. *
  2339. * The callback function must return 0 if it succeeds. Returning
  2340. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
  2341. * immediately.
  2342. */
  2343. typedef int (*ngtcp2_handshake_confirmed)(ngtcp2_conn *conn, void *user_data);
  2344. /**
  2345. * @functypedef
  2346. *
  2347. * :type:`ngtcp2_recv_version_negotiation` is invoked when Version
  2348. * Negotiation packet is received. |hd| is the pointer to the QUIC
  2349. * packet header object. The vector |sv| of |nsv| elements contains
  2350. * the QUIC version the server supports. Since Version Negotiation is
  2351. * only sent by server, this callback function is used by client only.
  2352. *
  2353. * The callback function must return 0 if it succeeds, or
  2354. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` which makes the library call
  2355. * return immediately.
  2356. */
  2357. typedef int (*ngtcp2_recv_version_negotiation)(ngtcp2_conn *conn,
  2358. const ngtcp2_pkt_hd *hd,
  2359. const uint32_t *sv, size_t nsv,
  2360. void *user_data);
  2361. /**
  2362. * @functypedef
  2363. *
  2364. * :type:`ngtcp2_recv_retry` is invoked when Retry packet is received.
  2365. * This callback is client use only.
  2366. *
  2367. * Application must regenerate packet protection key, IV, and header
  2368. * protection key for Initial packets using the Destination Connection
  2369. * ID obtained by :member:`hd->scid <ngtcp2_pkt_hd.scid>`, and install
  2370. * them by calling `ngtcp2_conn_install_initial_key`.
  2371. *
  2372. * 0-RTT data accepted by the ngtcp2 library will be automatically
  2373. * retransmitted as 0-RTT data by the library.
  2374. *
  2375. * The callback function must return 0 if it succeeds. Returning
  2376. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
  2377. * immediately.
  2378. */
  2379. typedef int (*ngtcp2_recv_retry)(ngtcp2_conn *conn, const ngtcp2_pkt_hd *hd,
  2380. void *user_data);
  2381. /**
  2382. * @functypedef
  2383. *
  2384. * :type:`ngtcp2_encrypt` is invoked when the ngtcp2 library asks the
  2385. * application to encrypt packet payload. The packet payload to
  2386. * encrypt is passed as |plaintext| of length |plaintextlen|. The
  2387. * AEAD cipher is |aead|. |aead_ctx| is the AEAD cipher context
  2388. * object which is initialized with the specific encryption key. The
  2389. * nonce is passed as |nonce| of length |noncelen|. The Additional
  2390. * Authenticated Data is passed as |aad| of length |aadlen|.
  2391. *
  2392. * The implementation of this callback must encrypt |plaintext| using
  2393. * the negotiated cipher suite, and write the ciphertext into the
  2394. * buffer pointed by |dest|. |dest| has enough capacity to store the
  2395. * ciphertext and any additional AEAD tag data.
  2396. *
  2397. * |dest| and |plaintext| may point to the same buffer.
  2398. *
  2399. * The callback function must return 0 if it succeeds, or
  2400. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` which makes the library call
  2401. * return immediately.
  2402. */
  2403. typedef int (*ngtcp2_encrypt)(uint8_t *dest, const ngtcp2_crypto_aead *aead,
  2404. const ngtcp2_crypto_aead_ctx *aead_ctx,
  2405. const uint8_t *plaintext, size_t plaintextlen,
  2406. const uint8_t *nonce, size_t noncelen,
  2407. const uint8_t *aad, size_t aadlen);
  2408. /**
  2409. * @functypedef
  2410. *
  2411. * :type:`ngtcp2_decrypt` is invoked when the ngtcp2 library asks the
  2412. * application to decrypt packet payload. The packet payload to
  2413. * decrypt is passed as |ciphertext| of length |ciphertextlen|. The
  2414. * AEAD cipher is |aead|. |aead_ctx| is the AEAD cipher context
  2415. * object which is initialized with the specific decryption key. The
  2416. * nonce is passed as |nonce| of length |noncelen|. The Additional
  2417. * Authenticated Data is passed as |aad| of length |aadlen|.
  2418. *
  2419. * The implementation of this callback must decrypt |ciphertext| using
  2420. * the negotiated cipher suite, and write the ciphertext into the
  2421. * buffer pointed by |dest|. |dest| has enough capacity to store the
  2422. * cleartext.
  2423. *
  2424. * |dest| and |ciphertext| may point to the same buffer.
  2425. *
  2426. * The callback function must return 0 if it succeeds. If TLS stack
  2427. * fails to decrypt data, return :macro:`NGTCP2_ERR_DECRYPT`. For any
  2428. * other errors, return :macro:`NGTCP2_ERR_CALLBACK_FAILURE` which
  2429. * makes the library call return immediately.
  2430. */
  2431. typedef int (*ngtcp2_decrypt)(uint8_t *dest, const ngtcp2_crypto_aead *aead,
  2432. const ngtcp2_crypto_aead_ctx *aead_ctx,
  2433. const uint8_t *ciphertext, size_t ciphertextlen,
  2434. const uint8_t *nonce, size_t noncelen,
  2435. const uint8_t *aad, size_t aadlen);
  2436. /**
  2437. * @functypedef
  2438. *
  2439. * :type:`ngtcp2_hp_mask` is invoked when the ngtcp2 library asks the
  2440. * application to produce a mask to encrypt or decrypt packet header.
  2441. * The encryption cipher is |hp|. |hp_ctx| is the cipher context
  2442. * object which is initialized with the specific header protection
  2443. * key. The sample is passed as |sample| which is
  2444. * :macro:`NGTCP2_HP_SAMPLELEN` bytes long.
  2445. *
  2446. * The implementation of this callback must produce a mask using the
  2447. * header protection cipher suite specified by QUIC specification, and
  2448. * write the result into the buffer pointed by |dest|. The length of
  2449. * the mask must be at least :macro:`NGTCP2_HP_MASKLEN`. The library
  2450. * only uses the first :macro:`NGTCP2_HP_MASKLEN` bytes of the
  2451. * produced mask. The buffer pointed by |dest| is guaranteed to have
  2452. * at least :macro:`NGTCP2_HP_SAMPLELEN` bytes available for
  2453. * convenience.
  2454. *
  2455. * The callback function must return 0 if it succeeds, or
  2456. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` which makes the library call
  2457. * return immediately.
  2458. */
  2459. typedef int (*ngtcp2_hp_mask)(uint8_t *dest, const ngtcp2_crypto_cipher *hp,
  2460. const ngtcp2_crypto_cipher_ctx *hp_ctx,
  2461. const uint8_t *sample);
  2462. /**
  2463. * @macrosection
  2464. *
  2465. * STREAM frame data flags
  2466. */
  2467. /**
  2468. * @macro
  2469. *
  2470. * :macro:`NGTCP2_STREAM_DATA_FLAG_NONE` indicates no flag set.
  2471. */
  2472. #define NGTCP2_STREAM_DATA_FLAG_NONE 0x00u
  2473. /**
  2474. * @macro
  2475. *
  2476. * :macro:`NGTCP2_STREAM_DATA_FLAG_FIN` indicates that this chunk of
  2477. * data is final piece of an incoming stream.
  2478. */
  2479. #define NGTCP2_STREAM_DATA_FLAG_FIN 0x01u
  2480. /**
  2481. * @macro
  2482. *
  2483. * :macro:`NGTCP2_STREAM_DATA_FLAG_0RTT` indicates that this chunk of
  2484. * data contains data received in 0-RTT packet, and the handshake has
  2485. * not completed yet, which means that the data might be replayed.
  2486. */
  2487. #define NGTCP2_STREAM_DATA_FLAG_0RTT 0x02u
  2488. /**
  2489. * @functypedef
  2490. *
  2491. * :type:`ngtcp2_recv_stream_data` is invoked when stream data is
  2492. * received. The stream is specified by |stream_id|. |flags| is the
  2493. * bitwise-OR of zero or more of :macro:`NGTCP2_STREAM_DATA_FLAG_*
  2494. * <NGTCP2_STREAM_DATA_FLAG_NONE>`. If |flags| &
  2495. * :macro:`NGTCP2_STREAM_DATA_FLAG_FIN` is nonzero, this portion of
  2496. * the data is the last data in this stream. |offset| is the offset
  2497. * where this data begins. The library ensures that data is passed to
  2498. * the application in the non-decreasing order of |offset| without any
  2499. * overlap. The data is passed as |data| of length |datalen|.
  2500. * |datalen| may be 0 if and only if |fin| is nonzero.
  2501. *
  2502. * If :macro:`NGTCP2_STREAM_DATA_FLAG_0RTT` is set in |flags|, it
  2503. * indicates that a part of or whole data was received in 0-RTT
  2504. * packet, and a handshake has not completed yet.
  2505. *
  2506. * The callback function must return 0 if it succeeds, or
  2507. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` which makes the library return
  2508. * immediately.
  2509. */
  2510. typedef int (*ngtcp2_recv_stream_data)(ngtcp2_conn *conn, uint32_t flags,
  2511. int64_t stream_id, uint64_t offset,
  2512. const uint8_t *data, size_t datalen,
  2513. void *user_data, void *stream_user_data);
  2514. /**
  2515. * @functypedef
  2516. *
  2517. * :type:`ngtcp2_stream_open` is a callback function which is called
  2518. * when remote stream is opened by a remote endpoint. This function
  2519. * is not called if stream is opened by implicitly (we might
  2520. * reconsider this behaviour later).
  2521. *
  2522. * The implementation of this callback should return 0 if it succeeds.
  2523. * Returning :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library
  2524. * call return immediately.
  2525. */
  2526. typedef int (*ngtcp2_stream_open)(ngtcp2_conn *conn, int64_t stream_id,
  2527. void *user_data);
  2528. /**
  2529. * @macrosection
  2530. *
  2531. * Stream close flags
  2532. */
  2533. /**
  2534. * @macro
  2535. *
  2536. * :macro:`NGTCP2_STREAM_CLOSE_FLAG_NONE` indicates no flag set.
  2537. */
  2538. #define NGTCP2_STREAM_CLOSE_FLAG_NONE 0x00u
  2539. /**
  2540. * @macro
  2541. *
  2542. * :macro:`NGTCP2_STREAM_CLOSE_FLAG_APP_ERROR_CODE_SET` indicates that
  2543. * app_error_code parameter is set.
  2544. */
  2545. #define NGTCP2_STREAM_CLOSE_FLAG_APP_ERROR_CODE_SET 0x01u
  2546. /**
  2547. * @functypedef
  2548. *
  2549. * :type:`ngtcp2_stream_close` is invoked when a stream is closed.
  2550. * This callback is not called when QUIC connection is closed before
  2551. * existing streams are closed. |flags| is the bitwise-OR of zero or
  2552. * more of :macro:`NGTCP2_STREAM_CLOSE_FLAG_*
  2553. * <NGTCP2_STREAM_CLOSE_FLAG_NONE>`. |app_error_code| indicates the
  2554. * error code of this closure if
  2555. * :macro:`NGTCP2_STREAM_CLOSE_FLAG_APP_ERROR_CODE_SET` is set in
  2556. * |flags|. If it is not set, the stream was closed without any error
  2557. * code, which generally means success.
  2558. *
  2559. * |app_error_code| is the first application error code sent by a
  2560. * local endpoint, or received from a remote endpoint. If a stream is
  2561. * closed cleanly, no application error code is exchanged. Since QUIC
  2562. * stack does not know the application error code which indicates "no
  2563. * errors", |app_error_code| is set to 0 and
  2564. * :macro:`NGTCP2_STREAM_CLOSE_FLAG_APP_ERROR_CODE_SET` is not set in
  2565. * |flags| in this case.
  2566. *
  2567. * The implementation of this callback should return 0 if it succeeds.
  2568. * Returning :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library
  2569. * call return immediately.
  2570. */
  2571. typedef int (*ngtcp2_stream_close)(ngtcp2_conn *conn, uint32_t flags,
  2572. int64_t stream_id, uint64_t app_error_code,
  2573. void *user_data, void *stream_user_data);
  2574. /**
  2575. * @functypedef
  2576. *
  2577. * :type:`ngtcp2_stream_reset` is invoked when a stream identified by
  2578. * |stream_id| is reset by a remote endpoint.
  2579. *
  2580. * The implementation of this callback should return 0 if it succeeds.
  2581. * Returning :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library
  2582. * call return immediately.
  2583. */
  2584. typedef int (*ngtcp2_stream_reset)(ngtcp2_conn *conn, int64_t stream_id,
  2585. uint64_t final_size, uint64_t app_error_code,
  2586. void *user_data, void *stream_user_data);
  2587. /**
  2588. * @functypedef
  2589. *
  2590. * :type:`ngtcp2_acked_stream_data_offset` is a callback function
  2591. * which is called when stream data in range [|offset|, |offset| +
  2592. * |datalen|) is acknowledged, and application can free the portion of
  2593. * data. For a given |stream_id|, this callback is called
  2594. * sequentially in increasing order of |offset| without any overlap.
  2595. * |datalen| is normally strictly greater than 0. One exception is
  2596. * that when a STREAM frame has fin flag set and 0 length data, this
  2597. * callback is invoked with |datalen| == 0.
  2598. *
  2599. * If a stream is closed prematurely, and stream data is still
  2600. * in-flight, this callback function is not called for those data.
  2601. * After :member:`ngtcp2_callbacks.stream_close` is called for a
  2602. * particular stream, |conn| does not touch data for the closed stream
  2603. * again, and application can free all unacknowledged stream data.
  2604. *
  2605. * The implementation of this callback should return 0 if it succeeds.
  2606. * Returning :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library
  2607. * call return immediately.
  2608. */
  2609. typedef int (*ngtcp2_acked_stream_data_offset)(
  2610. ngtcp2_conn *conn, int64_t stream_id, uint64_t offset, uint64_t datalen,
  2611. void *user_data, void *stream_user_data);
  2612. /**
  2613. * @functypedef
  2614. *
  2615. * :type:`ngtcp2_recv_stateless_reset` is a callback function which is
  2616. * called when Stateless Reset packet is received. The stateless
  2617. * reset details are given in |sr|.
  2618. *
  2619. * The implementation of this callback should return 0 if it succeeds.
  2620. * Returning :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library
  2621. * call return immediately.
  2622. */
  2623. typedef int (*ngtcp2_recv_stateless_reset)(ngtcp2_conn *conn,
  2624. const ngtcp2_pkt_stateless_reset *sr,
  2625. void *user_data);
  2626. /**
  2627. * @functypedef
  2628. *
  2629. * :type:`ngtcp2_extend_max_streams` is a callback function which is
  2630. * called every time max stream ID is strictly extended.
  2631. * |max_streams| is the cumulative number of streams which an endpoint
  2632. * can open.
  2633. *
  2634. * The callback function must return 0 if it succeeds. Returning
  2635. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
  2636. * immediately.
  2637. */
  2638. typedef int (*ngtcp2_extend_max_streams)(ngtcp2_conn *conn,
  2639. uint64_t max_streams, void *user_data);
  2640. /**
  2641. * @functypedef
  2642. *
  2643. * :type:`ngtcp2_extend_max_stream_data` is a callback function which
  2644. * is invoked when max stream data is extended. |stream_id|
  2645. * identifies the stream. |max_data| is a cumulative number of bytes
  2646. * an endpoint can send on this stream.
  2647. *
  2648. * The callback function must return 0 if it succeeds. Returning
  2649. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
  2650. * immediately.
  2651. */
  2652. typedef int (*ngtcp2_extend_max_stream_data)(ngtcp2_conn *conn,
  2653. int64_t stream_id,
  2654. uint64_t max_data, void *user_data,
  2655. void *stream_user_data);
  2656. /**
  2657. * @functypedef
  2658. *
  2659. * :type:`ngtcp2_rand` is a callback function to get random data of
  2660. * length |destlen|. Application must fill random |destlen| bytes to
  2661. * the buffer pointed by |dest|. The generated data is used only in
  2662. * non-cryptographic context.
  2663. */
  2664. typedef void (*ngtcp2_rand)(uint8_t *dest, size_t destlen,
  2665. const ngtcp2_rand_ctx *rand_ctx);
  2666. /**
  2667. * @functypedef
  2668. *
  2669. * :type:`ngtcp2_get_new_connection_id` is a callback function to ask
  2670. * an application for new connection ID. Application must generate
  2671. * new unused connection ID with the exact |cidlen| bytes, and store
  2672. * it in |cid|. It also has to generate a stateless reset token, and
  2673. * store it in |token|. The length of stateless reset token is
  2674. * :macro:`NGTCP2_STATELESS_RESET_TOKENLEN` and it is guaranteed that
  2675. * the buffer pointed by |token| has the sufficient space to store the
  2676. * token.
  2677. *
  2678. * The callback function must return 0 if it succeeds. Returning
  2679. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
  2680. * immediately.
  2681. */
  2682. typedef int (*ngtcp2_get_new_connection_id)(ngtcp2_conn *conn, ngtcp2_cid *cid,
  2683. uint8_t *token, size_t cidlen,
  2684. void *user_data);
  2685. /**
  2686. * @functypedef
  2687. *
  2688. * :type:`ngtcp2_remove_connection_id` is a callback function which
  2689. * notifies the application that connection ID |cid| is no longer used
  2690. * by a remote endpoint. This Connection ID was previously offered by
  2691. * a local endpoint, and a remote endpoint could use it as Destination
  2692. * Connection ID when sending QUIC packet.
  2693. *
  2694. * The callback function must return 0 if it succeeds. Returning
  2695. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
  2696. * immediately.
  2697. */
  2698. typedef int (*ngtcp2_remove_connection_id)(ngtcp2_conn *conn,
  2699. const ngtcp2_cid *cid,
  2700. void *user_data);
  2701. /**
  2702. * @functypedef
  2703. *
  2704. * :type:`ngtcp2_update_key` is a callback function which tells the
  2705. * application that it must generate new packet protection keying
  2706. * materials and AEAD cipher context objects with new keys. The
  2707. * current set of secrets are given as |current_rx_secret| and
  2708. * |current_tx_secret| of length |secretlen|. They are decryption and
  2709. * encryption secrets respectively.
  2710. *
  2711. * The application must generate new secrets and keys for both
  2712. * encryption and decryption. It must write decryption secret and IV
  2713. * to the buffer pointed by |rx_secret| and |rx_iv| respectively. It
  2714. * also must create new AEAD cipher context object with new decryption
  2715. * key and initialize |rx_aead_ctx| with it. Similarly, write
  2716. * encryption secret and IV to the buffer pointed by |tx_secret| and
  2717. * |tx_iv|. Create new AEAD cipher context object with new encryption
  2718. * key and initialize |tx_aead_ctx| with it. All given buffers have
  2719. * the enough capacity to store secret, key and IV.
  2720. *
  2721. * The callback function must return 0 if it succeeds. Returning
  2722. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
  2723. * immediately.
  2724. */
  2725. typedef int (*ngtcp2_update_key)(
  2726. ngtcp2_conn *conn, uint8_t *rx_secret, uint8_t *tx_secret,
  2727. ngtcp2_crypto_aead_ctx *rx_aead_ctx, uint8_t *rx_iv,
  2728. ngtcp2_crypto_aead_ctx *tx_aead_ctx, uint8_t *tx_iv,
  2729. const uint8_t *current_rx_secret, const uint8_t *current_tx_secret,
  2730. size_t secretlen, void *user_data);
  2731. /**
  2732. * @macrosection
  2733. *
  2734. * Path validation related macros
  2735. */
  2736. /**
  2737. * @macro
  2738. *
  2739. * :macro:`NGTCP2_PATH_VALIDATION_FLAG_NONE` indicates no flag set.
  2740. */
  2741. #define NGTCP2_PATH_VALIDATION_FLAG_NONE 0x00u
  2742. /**
  2743. * @macro
  2744. *
  2745. * :macro:`NGTCP2_PATH_VALIDATION_FLAG_PREFERRED_ADDR` indicates the
  2746. * validation involving server preferred address. This flag is only
  2747. * set for client.
  2748. */
  2749. #define NGTCP2_PATH_VALIDATION_FLAG_PREFERRED_ADDR 0x01u
  2750. /**
  2751. * @macro
  2752. *
  2753. * :macro:`NGTCP2_PATH_VALIDATION_FLAG_NEW_TOKEN` indicates that
  2754. * server should send NEW_TOKEN frame for the new remote address.
  2755. * This flag is only set for server.
  2756. */
  2757. #define NGTCP2_PATH_VALIDATION_FLAG_NEW_TOKEN 0x02u
  2758. /**
  2759. * @functypedef
  2760. *
  2761. * :type:`ngtcp2_path_validation` is a callback function which tells
  2762. * an application the outcome of path validation. |flags| is zero or
  2763. * more of :macro:`NGTCP2_PATH_VALIDATION_FLAG_*
  2764. * <NGTCP2_PATH_VALIDATION_FLAG_NONE>`. |path| is the path that was
  2765. * validated. |old_path| is the path that is previously used before a
  2766. * local endpoint has migrated to |path| if |old_path| is not NULL.
  2767. * If |res| is
  2768. * :enum:`ngtcp2_path_validation_result.NGTCP2_PATH_VALIDATION_RESULT_SUCCESS`,
  2769. * the path validation succeeded. If |res| is
  2770. * :enum:`ngtcp2_path_validation_result.NGTCP2_PATH_VALIDATION_RESULT_FAILURE`,
  2771. * the path validation failed.
  2772. *
  2773. * The callback function must return 0 if it succeeds. Returning
  2774. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
  2775. * immediately.
  2776. */
  2777. typedef int (*ngtcp2_path_validation)(ngtcp2_conn *conn, uint32_t flags,
  2778. const ngtcp2_path *path,
  2779. const ngtcp2_path *old_path,
  2780. ngtcp2_path_validation_result res,
  2781. void *user_data);
  2782. /**
  2783. * @functypedef
  2784. *
  2785. * :type:`ngtcp2_select_preferred_addr` is a callback function which
  2786. * asks a client application to choose server address from preferred
  2787. * addresses |paddr| received from server. An application should
  2788. * write a network path for a selected preferred address in |dest|.
  2789. * More specifically, the selected preferred address must be set to
  2790. * :member:`dest->remote <ngtcp2_path.remote>`, a client source
  2791. * address must be set to :member:`dest->local <ngtcp2_path.local>`.
  2792. * If a client source address does not change for the new server
  2793. * address, leave :member:`dest->local <ngtcp2_path.local>`
  2794. * unmodified, or copy the value of :member:`local
  2795. * <ngtcp2_path.local>` field of the current network path obtained
  2796. * from `ngtcp2_conn_get_path()`. Both :member:`dest->local.addr
  2797. * <ngtcp2_addr.addr>` and :member:`dest->remote.addr
  2798. * <ngtcp2_addr.addr>` point to buffers which are at least
  2799. * sizeof(:type:`ngtcp2_sockaddr_union`) bytes long, respectively. If
  2800. * an application denies the preferred addresses, just leave |dest|
  2801. * unmodified (or set :member:`dest->remote.addrlen
  2802. * <ngtcp2_addr.addrlen>` to 0), and return 0.
  2803. *
  2804. * The callback function must return 0 if it succeeds. Returning
  2805. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
  2806. * immediately.
  2807. */
  2808. typedef int (*ngtcp2_select_preferred_addr)(ngtcp2_conn *conn,
  2809. ngtcp2_path *dest,
  2810. const ngtcp2_preferred_addr *paddr,
  2811. void *user_data);
  2812. /**
  2813. * @enum
  2814. *
  2815. * :type:`ngtcp2_connection_id_status_type` defines a set of status
  2816. * for Destination Connection ID.
  2817. */
  2818. typedef enum ngtcp2_connection_id_status_type {
  2819. /**
  2820. * :enum:`NGTCP2_CONNECTION_ID_STATUS_TYPE_ACTIVATE` indicates that
  2821. * a local endpoint starts using new Destination Connection ID.
  2822. */
  2823. NGTCP2_CONNECTION_ID_STATUS_TYPE_ACTIVATE,
  2824. /**
  2825. * :enum:`NGTCP2_CONNECTION_ID_STATUS_TYPE_DEACTIVATE` indicates
  2826. * that a local endpoint stops using a given Destination Connection
  2827. * ID.
  2828. */
  2829. NGTCP2_CONNECTION_ID_STATUS_TYPE_DEACTIVATE
  2830. } ngtcp2_connection_id_status_type;
  2831. /**
  2832. * @functypedef
  2833. *
  2834. * :type:`ngtcp2_connection_id_status` is a callback function which is
  2835. * called when the status of Destination Connection ID changes.
  2836. *
  2837. * |token| is the associated stateless reset token, and it is ``NULL``
  2838. * if no token is present.
  2839. *
  2840. * |type| is the one of the value defined in
  2841. * :type:`ngtcp2_connection_id_status_type`. The new value might be
  2842. * added in the future release.
  2843. *
  2844. * The callback function must return 0 if it succeeds. Returning
  2845. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
  2846. * immediately.
  2847. */
  2848. typedef int (*ngtcp2_connection_id_status)(
  2849. ngtcp2_conn *conn, ngtcp2_connection_id_status_type type, uint64_t seq,
  2850. const ngtcp2_cid *cid, const uint8_t *token, void *user_data);
  2851. /**
  2852. * @functypedef
  2853. *
  2854. * :type:`ngtcp2_recv_new_token` is a callback function which is
  2855. * called when new token is received from server. This callback is
  2856. * client use only.
  2857. *
  2858. * |token| is the received token of length |tokenlen| bytes long.
  2859. *
  2860. * The callback function must return 0 if it succeeds. Returning
  2861. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
  2862. * immediately.
  2863. */
  2864. typedef int (*ngtcp2_recv_new_token)(ngtcp2_conn *conn, const uint8_t *token,
  2865. size_t tokenlen, void *user_data);
  2866. /**
  2867. * @functypedef
  2868. *
  2869. * :type:`ngtcp2_delete_crypto_aead_ctx` is a callback function which
  2870. * must delete the native object pointed by
  2871. * :member:`aead_ctx->native_handle
  2872. * <ngtcp2_crypto_aead_ctx.native_handle>`.
  2873. */
  2874. typedef void (*ngtcp2_delete_crypto_aead_ctx)(ngtcp2_conn *conn,
  2875. ngtcp2_crypto_aead_ctx *aead_ctx,
  2876. void *user_data);
  2877. /**
  2878. * @functypedef
  2879. *
  2880. * :type:`ngtcp2_delete_crypto_cipher_ctx` is a callback function
  2881. * which must delete the native object pointed by
  2882. * :member:`cipher_ctx->native_handle
  2883. * <ngtcp2_crypto_cipher_ctx.native_handle>`.
  2884. */
  2885. typedef void (*ngtcp2_delete_crypto_cipher_ctx)(
  2886. ngtcp2_conn *conn, ngtcp2_crypto_cipher_ctx *cipher_ctx, void *user_data);
  2887. /**
  2888. * @macrosection
  2889. *
  2890. * DATAGRAM frame flags
  2891. */
  2892. /**
  2893. * @macro
  2894. *
  2895. * :macro:`NGTCP2_DATAGRAM_FLAG_NONE` indicates no flag set.
  2896. */
  2897. #define NGTCP2_DATAGRAM_FLAG_NONE 0x00u
  2898. /**
  2899. * @macro
  2900. *
  2901. * :macro:`NGTCP2_DATAGRAM_FLAG_0RTT` indicates that DATAGRAM frame is
  2902. * received in 0-RTT packet, and the handshake has not completed yet,
  2903. * which means that the data might be replayed.
  2904. */
  2905. #define NGTCP2_DATAGRAM_FLAG_0RTT 0x01u
  2906. /**
  2907. * @functypedef
  2908. *
  2909. * :type:`ngtcp2_recv_datagram` is invoked when DATAGRAM frame is
  2910. * received. |flags| is bitwise-OR of zero or more of
  2911. * :macro:`NGTCP2_DATAGRAM_FLAG_* <NGTCP2_DATAGRAM_FLAG_NONE>`.
  2912. *
  2913. * If :macro:`NGTCP2_DATAGRAM_FLAG_0RTT` is set in |flags|, it
  2914. * indicates that DATAGRAM frame was received in 0-RTT packet, and a
  2915. * handshake has not completed yet.
  2916. *
  2917. * The callback function must return 0 if it succeeds, or
  2918. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` which makes the library return
  2919. * immediately.
  2920. */
  2921. typedef int (*ngtcp2_recv_datagram)(ngtcp2_conn *conn, uint32_t flags,
  2922. const uint8_t *data, size_t datalen,
  2923. void *user_data);
  2924. /**
  2925. * @functypedef
  2926. *
  2927. * :type:`ngtcp2_ack_datagram` is invoked when a packet which contains
  2928. * DATAGRAM frame which is identified by |dgram_id| is acknowledged.
  2929. * |dgram_id| is the valued passed to `ngtcp2_conn_writev_datagram`.
  2930. *
  2931. * The callback function must return 0 if it succeeds, or
  2932. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` which makes the library return
  2933. * immediately.
  2934. */
  2935. typedef int (*ngtcp2_ack_datagram)(ngtcp2_conn *conn, uint64_t dgram_id,
  2936. void *user_data);
  2937. /**
  2938. * @functypedef
  2939. *
  2940. * :type:`ngtcp2_lost_datagram` is invoked when a packet which
  2941. * contains DATAGRAM frame which is identified by |dgram_id| is
  2942. * declared lost. |dgram_id| is the valued passed to
  2943. * `ngtcp2_conn_writev_datagram`. Note that the loss might be
  2944. * spurious, and DATAGRAM frame might be acknowledged later.
  2945. *
  2946. * The callback function must return 0 if it succeeds, or
  2947. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` which makes the library return
  2948. * immediately.
  2949. */
  2950. typedef int (*ngtcp2_lost_datagram)(ngtcp2_conn *conn, uint64_t dgram_id,
  2951. void *user_data);
  2952. /**
  2953. * @functypedef
  2954. *
  2955. * :type:`ngtcp2_get_path_challenge_data` is a callback function to
  2956. * ask an application for new data that is sent in PATH_CHALLENGE
  2957. * frame. Application must generate new unpredictable, exactly
  2958. * :macro:`NGTCP2_PATH_CHALLENGE_DATALEN` bytes of random data, and
  2959. * store them into the buffer pointed by |data|.
  2960. *
  2961. * The callback function must return 0 if it succeeds. Returning
  2962. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
  2963. * immediately.
  2964. */
  2965. typedef int (*ngtcp2_get_path_challenge_data)(ngtcp2_conn *conn, uint8_t *data,
  2966. void *user_data);
  2967. /**
  2968. * @functypedef
  2969. *
  2970. * :type:`ngtcp2_stream_stop_sending` is invoked when a stream is no
  2971. * longer read by a local endpoint before it receives all stream data.
  2972. * This function is called at most once per stream. |app_error_code|
  2973. * is the error code passed to `ngtcp2_conn_shutdown_stream_read` or
  2974. * `ngtcp2_conn_shutdown_stream`.
  2975. *
  2976. * The callback function must return 0 if it succeeds. Returning
  2977. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
  2978. * immediately.
  2979. */
  2980. typedef int (*ngtcp2_stream_stop_sending)(ngtcp2_conn *conn, int64_t stream_id,
  2981. uint64_t app_error_code,
  2982. void *user_data,
  2983. void *stream_user_data);
  2984. /**
  2985. * @functypedef
  2986. *
  2987. * :type:`ngtcp2_version_negotiation` is invoked when the compatible
  2988. * version negotiation takes place. For client, it is called when it
  2989. * sees a change in version field of a long header packet. This
  2990. * callback function might be called multiple times for client. For
  2991. * server, it is called once when the version is negotiated.
  2992. *
  2993. * The implementation of this callback must install new Initial keys
  2994. * for |version| and Destination Connection ID |client_dcid| from
  2995. * client. Use `ngtcp2_conn_install_vneg_initial_key` to install
  2996. * keys.
  2997. *
  2998. * The callback function must return 0 if it succeeds. Returning
  2999. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
  3000. * immediately.
  3001. */
  3002. typedef int (*ngtcp2_version_negotiation)(ngtcp2_conn *conn, uint32_t version,
  3003. const ngtcp2_cid *client_dcid,
  3004. void *user_data);
  3005. /**
  3006. * @functypedef
  3007. *
  3008. * :type:`ngtcp2_recv_key` is invoked when new key is installed to
  3009. * |conn| during QUIC cryptographic handshake.
  3010. *
  3011. * The callback function must return 0 if it succeeds. Returning
  3012. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
  3013. * immediately.
  3014. */
  3015. typedef int (*ngtcp2_recv_key)(ngtcp2_conn *conn, ngtcp2_encryption_level level,
  3016. void *user_data);
  3017. /**
  3018. * @functypedef
  3019. *
  3020. * :type:`ngtcp2_tls_early_data_rejected` is invoked when early data
  3021. * was rejected by server during TLS handshake, or client decided not
  3022. * to attempt early data.
  3023. *
  3024. * The callback function must return 0 if it succeeds. Returning
  3025. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
  3026. * immediately.
  3027. */
  3028. typedef int (*ngtcp2_tls_early_data_rejected)(ngtcp2_conn *conn,
  3029. void *user_data);
  3030. #define NGTCP2_CALLBACKS_V1 1
  3031. #define NGTCP2_CALLBACKS_VERSION NGTCP2_CALLBACKS_V1
  3032. /**
  3033. * @struct
  3034. *
  3035. * :type:`ngtcp2_callbacks` holds a set of callback functions.
  3036. */
  3037. typedef struct ngtcp2_callbacks {
  3038. /**
  3039. * :member:`client_initial` is a callback function which is invoked
  3040. * when client asks TLS stack to produce first TLS cryptographic
  3041. * handshake message. This callback function must be specified for
  3042. * a client application.
  3043. */
  3044. ngtcp2_client_initial client_initial;
  3045. /**
  3046. * :member:`recv_client_initial` is a callback function which is
  3047. * invoked when a server receives the first Initial packet from
  3048. * client. This callback function must be specified for a server
  3049. * application.
  3050. */
  3051. ngtcp2_recv_client_initial recv_client_initial;
  3052. /**
  3053. * :member:`recv_crypto_data` is a callback function which is
  3054. * invoked when cryptographic data (CRYPTO frame, in other words,
  3055. * TLS message) is received. This callback function must be
  3056. * specified.
  3057. */
  3058. ngtcp2_recv_crypto_data recv_crypto_data;
  3059. /**
  3060. * :member:`handshake_completed` is a callback function which is
  3061. * invoked when QUIC cryptographic handshake has completed. This
  3062. * callback function is optional.
  3063. */
  3064. ngtcp2_handshake_completed handshake_completed;
  3065. /**
  3066. * :member:`recv_version_negotiation` is a callback function which
  3067. * is invoked when Version Negotiation packet is received by a
  3068. * client. This callback function is optional.
  3069. */
  3070. ngtcp2_recv_version_negotiation recv_version_negotiation;
  3071. /**
  3072. * :member:`encrypt` is a callback function which is invoked to
  3073. * encrypt a QUIC packet. This callback function must be specified.
  3074. */
  3075. ngtcp2_encrypt encrypt;
  3076. /**
  3077. * :member:`decrypt` is a callback function which is invoked to
  3078. * decrypt a QUIC packet. This callback function must be specified.
  3079. */
  3080. ngtcp2_decrypt decrypt;
  3081. /**
  3082. * :member:`hp_mask` is a callback function which is invoked to get
  3083. * a mask to encrypt or decrypt QUIC packet header. This callback
  3084. * function must be specified.
  3085. */
  3086. ngtcp2_hp_mask hp_mask;
  3087. /**
  3088. * :member:`recv_stream_data` is a callback function which is
  3089. * invoked when stream data, which includes application data, is
  3090. * received. This callback function is optional.
  3091. */
  3092. ngtcp2_recv_stream_data recv_stream_data;
  3093. /**
  3094. * :member:`acked_stream_data_offset` is a callback function which
  3095. * is invoked when stream data, which includes application data, is
  3096. * acknowledged by a remote endpoint. It tells an application the
  3097. * largest offset of acknowledged stream data without a gap so that
  3098. * application can free memory for the data up to that offset. This
  3099. * callback function is optional.
  3100. */
  3101. ngtcp2_acked_stream_data_offset acked_stream_data_offset;
  3102. /**
  3103. * :member:`stream_open` is a callback function which is invoked
  3104. * when new remote stream is opened by a remote endpoint. This
  3105. * callback function is optional.
  3106. */
  3107. ngtcp2_stream_open stream_open;
  3108. /**
  3109. * :member:`stream_close` is a callback function which is invoked
  3110. * when a stream is closed. This callback function is optional.
  3111. */
  3112. ngtcp2_stream_close stream_close;
  3113. /**
  3114. * :member:`recv_stateless_reset` is a callback function which is
  3115. * invoked when Stateless Reset packet is received. This callback
  3116. * function is optional.
  3117. */
  3118. ngtcp2_recv_stateless_reset recv_stateless_reset;
  3119. /**
  3120. * :member:`recv_retry` is a callback function which is invoked when
  3121. * a client receives Retry packet. For client, this callback
  3122. * function must be specified. Server never receive Retry packet.
  3123. */
  3124. ngtcp2_recv_retry recv_retry;
  3125. /**
  3126. * :member:`extend_max_local_streams_bidi` is a callback function
  3127. * which is invoked when the number of bidirectional stream which a
  3128. * local endpoint can open is increased. This callback function is
  3129. * optional.
  3130. */
  3131. ngtcp2_extend_max_streams extend_max_local_streams_bidi;
  3132. /**
  3133. * :member:`extend_max_local_streams_uni` is a callback function
  3134. * which is invoked when the number of unidirectional stream which a
  3135. * local endpoint can open is increased. This callback function is
  3136. * optional.
  3137. */
  3138. ngtcp2_extend_max_streams extend_max_local_streams_uni;
  3139. /**
  3140. * :member:`rand` is a callback function which is invoked when the
  3141. * library needs random data. This callback function must be
  3142. * specified.
  3143. */
  3144. ngtcp2_rand rand;
  3145. /**
  3146. * :member:`get_new_connection_id` is a callback function which is
  3147. * invoked when the library needs new connection ID. This callback
  3148. * function must be specified.
  3149. */
  3150. ngtcp2_get_new_connection_id get_new_connection_id;
  3151. /**
  3152. * :member:`remove_connection_id` is a callback function which
  3153. * notifies an application that connection ID is no longer used by a
  3154. * remote endpoint. This callback function is optional.
  3155. */
  3156. ngtcp2_remove_connection_id remove_connection_id;
  3157. /**
  3158. * :member:`update_key` is a callback function which is invoked when
  3159. * the library tells an application that it must update keying
  3160. * materials, and install new keys. This callback function must be
  3161. * specified.
  3162. */
  3163. ngtcp2_update_key update_key;
  3164. /**
  3165. * :member:`path_validation` is a callback function which is invoked
  3166. * when path validation completed. This callback function is
  3167. * optional.
  3168. */
  3169. ngtcp2_path_validation path_validation;
  3170. /**
  3171. * :member:`select_preferred_addr` is a callback function which is
  3172. * invoked when the library asks a client to select preferred
  3173. * address presented by a server. If not set, client ignores
  3174. * preferred addresses. This callback function is optional.
  3175. */
  3176. ngtcp2_select_preferred_addr select_preferred_addr;
  3177. /**
  3178. * :member:`stream_reset` is a callback function which is invoked
  3179. * when a stream is reset by a remote endpoint. This callback
  3180. * function is optional.
  3181. */
  3182. ngtcp2_stream_reset stream_reset;
  3183. /**
  3184. * :member:`extend_max_remote_streams_bidi` is a callback function
  3185. * which is invoked when the number of bidirectional streams which a
  3186. * remote endpoint can open is increased. This callback function is
  3187. * optional.
  3188. */
  3189. ngtcp2_extend_max_streams extend_max_remote_streams_bidi;
  3190. /**
  3191. * :member:`extend_max_remote_streams_uni` is a callback function
  3192. * which is invoked when the number of unidirectional streams which
  3193. * a remote endpoint can open is increased. This callback function
  3194. * is optional.
  3195. */
  3196. ngtcp2_extend_max_streams extend_max_remote_streams_uni;
  3197. /**
  3198. * :member:`extend_max_stream_data` is callback function which is
  3199. * invoked when the maximum offset of stream data that a local
  3200. * endpoint can send is increased. This callback function is
  3201. * optional.
  3202. */
  3203. ngtcp2_extend_max_stream_data extend_max_stream_data;
  3204. /**
  3205. * :member:`dcid_status` is a callback function which is invoked
  3206. * when the new Destination Connection ID is activated, or the
  3207. * activated Destination Connection ID is now deactivated. This
  3208. * callback function is optional.
  3209. */
  3210. ngtcp2_connection_id_status dcid_status;
  3211. /**
  3212. * :member:`handshake_confirmed` is a callback function which is
  3213. * invoked when both endpoints agree that handshake has finished.
  3214. * This field is ignored by server because
  3215. * :member:`handshake_completed` also indicates the handshake
  3216. * confirmation for server. This callback function is optional.
  3217. */
  3218. ngtcp2_handshake_confirmed handshake_confirmed;
  3219. /**
  3220. * :member:`recv_new_token` is a callback function which is invoked
  3221. * when new token is received from server. This field is ignored by
  3222. * server. This callback function is optional.
  3223. */
  3224. ngtcp2_recv_new_token recv_new_token;
  3225. /**
  3226. * :member:`delete_crypto_aead_ctx` is a callback function which
  3227. * deletes a given AEAD cipher context object. This callback
  3228. * function must be specified.
  3229. */
  3230. ngtcp2_delete_crypto_aead_ctx delete_crypto_aead_ctx;
  3231. /**
  3232. * :member:`delete_crypto_cipher_ctx` is a callback function which
  3233. * deletes a given cipher context object. This callback function
  3234. * must be specified.
  3235. */
  3236. ngtcp2_delete_crypto_cipher_ctx delete_crypto_cipher_ctx;
  3237. /**
  3238. * :member:`recv_datagram` is a callback function which is invoked
  3239. * when DATAGRAM frame is received. This callback function is
  3240. * optional.
  3241. */
  3242. ngtcp2_recv_datagram recv_datagram;
  3243. /**
  3244. * :member:`ack_datagram` is a callback function which is invoked
  3245. * when a QUIC packet containing DATAGRAM frame is acknowledged by a
  3246. * remote endpoint. This callback function is optional.
  3247. */
  3248. ngtcp2_ack_datagram ack_datagram;
  3249. /**
  3250. * :member:`lost_datagram` is a callback function which is invoked
  3251. * when a QUIC packet containing DATAGRAM frame is declared lost.
  3252. * This callback function is optional.
  3253. */
  3254. ngtcp2_lost_datagram lost_datagram;
  3255. /**
  3256. * :member:`get_path_challenge_data` is a callback function which is
  3257. * invoked when the library needs new data sent along with
  3258. * PATH_CHALLENGE frame. This callback must be specified.
  3259. */
  3260. ngtcp2_get_path_challenge_data get_path_challenge_data;
  3261. /**
  3262. * :member:`stream_stop_sending` is a callback function which is
  3263. * invoked when a local endpoint no longer reads from a stream
  3264. * before it receives all stream data. This callback function is
  3265. * optional.
  3266. */
  3267. ngtcp2_stream_stop_sending stream_stop_sending;
  3268. /**
  3269. * :member:`version_negotiation` is a callback function which is
  3270. * invoked when the compatible version negotiation takes place.
  3271. * This callback function must be specified.
  3272. */
  3273. ngtcp2_version_negotiation version_negotiation;
  3274. /**
  3275. * :member:`recv_rx_key` is a callback function which is invoked
  3276. * when a new key for decrypting packets is installed during QUIC
  3277. * cryptographic handshake. It is not called for
  3278. * :enum:`ngtcp2_encryption_level.NGTCP2_ENCRYPTION_LEVEL_INITIAL`.
  3279. */
  3280. ngtcp2_recv_key recv_rx_key;
  3281. /**
  3282. * :member:`recv_tx_key` is a callback function which is invoked
  3283. * when a new key for encrypting packets is installed during QUIC
  3284. * cryptographic handshake. It is not called for
  3285. * :enum:`ngtcp2_encryption_level.NGTCP2_ENCRYPTION_LEVEL_INITIAL`.
  3286. */
  3287. ngtcp2_recv_key recv_tx_key;
  3288. /**
  3289. * :member:`tls_early_data_rejected` is a callback function which is
  3290. * invoked when server rejected early data during TLS handshake, or
  3291. * client decided not to attempt early data. This callback function
  3292. * is only used by client.
  3293. */
  3294. ngtcp2_tls_early_data_rejected tls_early_data_rejected;
  3295. } ngtcp2_callbacks;
  3296. /**
  3297. * @function
  3298. *
  3299. * `ngtcp2_pkt_write_connection_close` writes Initial packet
  3300. * containing CONNECTION_CLOSE frame with the given |error_code| and
  3301. * the optional |reason| of length |reasonlen| to the buffer pointed
  3302. * by |dest| of length |destlen|. All encryption parameters are for
  3303. * Initial packet encryption. The packet number is always 0.
  3304. *
  3305. * The primary use case of this function is for server to send
  3306. * CONNECTION_CLOSE frame in Initial packet to close connection
  3307. * without committing any state when validating Retry token fails.
  3308. *
  3309. * This function returns the number of bytes written if it succeeds,
  3310. * or one of the following negative error codes:
  3311. *
  3312. * :macro:`NGTCP2_ERR_NOBUF`
  3313. * Buffer is too small.
  3314. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE`
  3315. * Callback function failed.
  3316. */
  3317. NGTCP2_EXTERN ngtcp2_ssize ngtcp2_pkt_write_connection_close(
  3318. uint8_t *dest, size_t destlen, uint32_t version, const ngtcp2_cid *dcid,
  3319. const ngtcp2_cid *scid, uint64_t error_code, const uint8_t *reason,
  3320. size_t reasonlen, ngtcp2_encrypt encrypt, const ngtcp2_crypto_aead *aead,
  3321. const ngtcp2_crypto_aead_ctx *aead_ctx, const uint8_t *iv,
  3322. ngtcp2_hp_mask hp_mask, const ngtcp2_crypto_cipher *hp,
  3323. const ngtcp2_crypto_cipher_ctx *hp_ctx);
  3324. /**
  3325. * @function
  3326. *
  3327. * `ngtcp2_pkt_write_retry` writes Retry packet in the buffer pointed
  3328. * by |dest| whose length is |destlen|. |dcid| is the Connection ID
  3329. * which appeared in a packet as a Source Connection ID sent by
  3330. * client. |scid| is a server chosen Source Connection ID. |odcid|
  3331. * specifies Original Destination Connection ID which appeared in a
  3332. * packet as a Destination Connection ID sent by client. |token|
  3333. * specifies Retry Token, and |tokenlen| specifies its length. |aead|
  3334. * must be AEAD_AES_128_GCM. |aead_ctx| must be initialized with
  3335. * :macro:`NGTCP2_RETRY_KEY` as an encryption key.
  3336. *
  3337. * This function returns the number of bytes written to the buffer, or
  3338. * one of the following negative error codes:
  3339. *
  3340. * :macro:`NGTCP2_ERR_NOBUF`
  3341. * Buffer is too small.
  3342. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE`
  3343. * Callback function failed.
  3344. * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
  3345. * :member:`odcid->datalen <ngtcp2_cid.datalen>` is less than
  3346. * :macro:`NGTCP2_MIN_INITIAL_DCIDLEN`.
  3347. */
  3348. NGTCP2_EXTERN ngtcp2_ssize ngtcp2_pkt_write_retry(
  3349. uint8_t *dest, size_t destlen, uint32_t version, const ngtcp2_cid *dcid,
  3350. const ngtcp2_cid *scid, const ngtcp2_cid *odcid, const uint8_t *token,
  3351. size_t tokenlen, ngtcp2_encrypt encrypt, const ngtcp2_crypto_aead *aead,
  3352. const ngtcp2_crypto_aead_ctx *aead_ctx);
  3353. /**
  3354. * @function
  3355. *
  3356. * `ngtcp2_accept` is used by server implementation, and decides
  3357. * whether packet |pkt| of length |pktlen| from client is acceptable
  3358. * for the very first packet to a connection.
  3359. *
  3360. * If |dest| is not ``NULL`` and the function returns 0, the decoded
  3361. * packet header is stored in the object pointed by |dest|.
  3362. *
  3363. * This function returns 0 if it succeeds, or one of the following
  3364. * negative error codes:
  3365. *
  3366. * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
  3367. * The packet is not acceptable for the very first packet to a new
  3368. * connection; or the function failed to parse the packet header.
  3369. */
  3370. NGTCP2_EXTERN int ngtcp2_accept(ngtcp2_pkt_hd *dest, const uint8_t *pkt,
  3371. size_t pktlen);
  3372. /**
  3373. * @function
  3374. *
  3375. * `ngtcp2_conn_client_new` creates new :type:`ngtcp2_conn`, and
  3376. * initializes it as client. On success, it stores the pointer to the
  3377. * newly allocated object in |*pconn|. |dcid| is a randomized
  3378. * Destination Connection ID which must be longer than or equal to
  3379. * :macro:`NGTCP2_MIN_INITIAL_DCIDLEN`. |scid| is a Source Connection
  3380. * ID chosen by client. |client_chosen_version| is a QUIC version
  3381. * that a client chooses. |path| is the network path where this QUIC
  3382. * connection is being established, and must not be ``NULL``.
  3383. * |callbacks|, |settings|, and |params| must not be ``NULL``, and the
  3384. * function makes a copy of each of them. |params| is a local QUIC
  3385. * transport parameters, and sent to a remote endpoint during
  3386. * handshake. |user_data| is the arbitrary pointer which is passed to
  3387. * the user-defined callback functions. If |mem| is ``NULL``, the
  3388. * memory allocator returned by `ngtcp2_mem_default()` is used.
  3389. *
  3390. * Call `ngtcp2_conn_del` to free memory allocated for |*pconn|.
  3391. *
  3392. * This function returns 0 if it succeeds, or one of the following
  3393. * negative error codes:
  3394. *
  3395. * :macro:`NGTCP2_ERR_NOMEM`
  3396. * Out of memory.
  3397. */
  3398. NGTCP2_EXTERN int ngtcp2_conn_client_new_versioned(
  3399. ngtcp2_conn **pconn, const ngtcp2_cid *dcid, const ngtcp2_cid *scid,
  3400. const ngtcp2_path *path, uint32_t client_chosen_version,
  3401. int callbacks_version, const ngtcp2_callbacks *callbacks,
  3402. int settings_version, const ngtcp2_settings *settings,
  3403. int transport_params_version, const ngtcp2_transport_params *params,
  3404. const ngtcp2_mem *mem, void *user_data);
  3405. /**
  3406. * @function
  3407. *
  3408. * `ngtcp2_conn_server_new` creates new :type:`ngtcp2_conn`, and
  3409. * initializes it as server. On success, it stores the pointer to the
  3410. * newly allocated object in |*pconn|. |dcid| is a Destination
  3411. * Connection ID, and is usually the Connection ID that appears in
  3412. * client Initial packet as Source Connection ID. |scid| is a Source
  3413. * Connection ID chosen by server. |path| is the network path where
  3414. * this QUIC connection is being established, and must not be
  3415. * ``NULL``. |client_chosen_version| is a QUIC version that a client
  3416. * chooses. |callbacks|, |settings|, and |params| must not be
  3417. * ``NULL``, and the function makes a copy of each of them. |params|
  3418. * is a local QUIC transport parameters, and sent to a remote endpoint
  3419. * during handshake. |user_data| is the arbitrary pointer which is
  3420. * passed to the user-defined callback functions. If |mem| is
  3421. * ``NULL``, the memory allocator returned by `ngtcp2_mem_default()`
  3422. * is used.
  3423. *
  3424. * Call `ngtcp2_conn_del` to free memory allocated for |*pconn|.
  3425. *
  3426. * This function returns 0 if it succeeds, or one of the following
  3427. * negative error codes:
  3428. *
  3429. * :macro:`NGTCP2_ERR_NOMEM`
  3430. * Out of memory.
  3431. */
  3432. NGTCP2_EXTERN int ngtcp2_conn_server_new_versioned(
  3433. ngtcp2_conn **pconn, const ngtcp2_cid *dcid, const ngtcp2_cid *scid,
  3434. const ngtcp2_path *path, uint32_t client_chosen_version,
  3435. int callbacks_version, const ngtcp2_callbacks *callbacks,
  3436. int settings_version, const ngtcp2_settings *settings,
  3437. int transport_params_version, const ngtcp2_transport_params *params,
  3438. const ngtcp2_mem *mem, void *user_data);
  3439. /**
  3440. * @function
  3441. *
  3442. * `ngtcp2_conn_del` frees resources allocated for |conn|. It also
  3443. * frees memory pointed by |conn|.
  3444. */
  3445. NGTCP2_EXTERN void ngtcp2_conn_del(ngtcp2_conn *conn);
  3446. /**
  3447. * @function
  3448. *
  3449. * `ngtcp2_conn_read_pkt` decrypts QUIC packet given in |pkt| of
  3450. * length |pktlen| and processes it. |path| is the network path the
  3451. * packet is delivered and must not be ``NULL``. |pi| is packet
  3452. * metadata and may be ``NULL``. This function performs QUIC handshake
  3453. * as well.
  3454. *
  3455. * This function must not be called from inside the callback
  3456. * functions.
  3457. *
  3458. * This function returns 0 if it succeeds, or one of the following
  3459. * negative error codes:
  3460. *
  3461. * :macro:`NGTCP2_ERR_RETRY`
  3462. * Server must perform address validation by sending Retry packet
  3463. * (see `ngtcp2_crypto_write_retry` and `ngtcp2_pkt_write_retry`),
  3464. * and discard the connection state. Client application does not
  3465. * get this error code.
  3466. * :macro:`NGTCP2_ERR_DROP_CONN`
  3467. * Server application must drop the connection silently (without
  3468. * sending any CONNECTION_CLOSE frame), and discard connection
  3469. * state. Client application does not get this error code.
  3470. * :macro:`NGTCP2_ERR_DRAINING`
  3471. * A connection has entered the draining state, and no further
  3472. * packet transmission is allowed.
  3473. * :macro:`NGTCP2_ERR_CLOSING`
  3474. * A connection has entered the closing state, and no further
  3475. * packet transmission is allowed. Calling
  3476. * `ngtcp2_conn_write_connection_close` makes a connection enter
  3477. * this state.
  3478. * :macro:`NGTCP2_ERR_CRYPTO`
  3479. * An error happened in TLS stack. `ngtcp2_conn_get_tls_alert`
  3480. * returns TLS alert if set.
  3481. *
  3482. * If any other negative error is returned, call
  3483. * `ngtcp2_conn_write_connection_close` to get terminal packet, and
  3484. * sending it makes QUIC connection enter the closing state.
  3485. */
  3486. NGTCP2_EXTERN int
  3487. ngtcp2_conn_read_pkt_versioned(ngtcp2_conn *conn, const ngtcp2_path *path,
  3488. int pkt_info_version, const ngtcp2_pkt_info *pi,
  3489. const uint8_t *pkt, size_t pktlen,
  3490. ngtcp2_tstamp ts);
  3491. /**
  3492. * @function
  3493. *
  3494. * `ngtcp2_conn_write_pkt` is equivalent to calling
  3495. * `ngtcp2_conn_writev_stream` with -1 as |stream_id|, no stream data,
  3496. * and :macro:`NGTCP2_WRITE_STREAM_FLAG_NONE` as flags.
  3497. */
  3498. NGTCP2_EXTERN ngtcp2_ssize ngtcp2_conn_write_pkt_versioned(
  3499. ngtcp2_conn *conn, ngtcp2_path *path, int pkt_info_version,
  3500. ngtcp2_pkt_info *pi, uint8_t *dest, size_t destlen, ngtcp2_tstamp ts);
  3501. /**
  3502. * @function
  3503. *
  3504. * `ngtcp2_conn_tls_handshake_completed` tells |conn| that the TLS
  3505. * stack declares TLS handshake completion. This does not mean QUIC
  3506. * handshake has completed. The library needs extra conditions to be
  3507. * met.
  3508. */
  3509. NGTCP2_EXTERN void ngtcp2_conn_tls_handshake_completed(ngtcp2_conn *conn);
  3510. /**
  3511. * @function
  3512. *
  3513. * `ngtcp2_conn_get_handshake_completed` returns nonzero if QUIC
  3514. * handshake has completed.
  3515. */
  3516. NGTCP2_EXTERN int ngtcp2_conn_get_handshake_completed(ngtcp2_conn *conn);
  3517. /**
  3518. * @function
  3519. *
  3520. * `ngtcp2_conn_install_initial_key` installs packet protection keying
  3521. * materials for Initial packets. |rx_aead_ctx| is AEAD cipher
  3522. * context object, and must be initialized with a decryption key.
  3523. * |rx_iv| is IV of length |rx_ivlen| for decryption. |rx_hp_ctx| is
  3524. * a packet header protection cipher context object for decryption.
  3525. * Similarly, |tx_aead_ctx|, |tx_iv| and |tx_hp_ctx| are for
  3526. * encrypting outgoing packets, and are the same length with the
  3527. * decryption counterpart . If they have already been set, they are
  3528. * overwritten.
  3529. *
  3530. * |ivlen| must be the minimum length of AEAD nonce, or 8 bytes if
  3531. * that is larger.
  3532. *
  3533. * If this function succeeds, |conn| takes ownership of |rx_aead_ctx|,
  3534. * |rx_hp_ctx|, |tx_aead_ctx|, and |tx_hp_ctx|.
  3535. * :member:`ngtcp2_callbacks.delete_crypto_aead_ctx` and
  3536. * :member:`ngtcp2_callbacks.delete_crypto_cipher_ctx` will be called
  3537. * to delete these objects when they are no longer used. If this
  3538. * function fails, the caller is responsible to delete them.
  3539. *
  3540. * After receiving Retry packet, a Destination Connection ID that
  3541. * client sends in Initial packet most likely changes. In that case,
  3542. * client application must generate these keying materials again based
  3543. * on new Destination Connection ID, and install them again with this
  3544. * function.
  3545. *
  3546. * This function returns 0 if it succeeds, or one of the following
  3547. * negative error codes:
  3548. *
  3549. * :macro:`NGTCP2_ERR_NOMEM`
  3550. * Out of memory.
  3551. */
  3552. NGTCP2_EXTERN int ngtcp2_conn_install_initial_key(
  3553. ngtcp2_conn *conn, const ngtcp2_crypto_aead_ctx *rx_aead_ctx,
  3554. const uint8_t *rx_iv, const ngtcp2_crypto_cipher_ctx *rx_hp_ctx,
  3555. const ngtcp2_crypto_aead_ctx *tx_aead_ctx, const uint8_t *tx_iv,
  3556. const ngtcp2_crypto_cipher_ctx *tx_hp_ctx, size_t ivlen);
  3557. /**
  3558. * @function
  3559. *
  3560. * `ngtcp2_conn_install_vneg_initial_key` installs packet protection
  3561. * keying materials for Initial packets on compatible version
  3562. * negotiation for |version|. |rx_aead_ctx| is AEAD cipher context
  3563. * object, and must be initialized with a decryption key. |rx_iv| is
  3564. * IV of length |rx_ivlen| for decryption. |rx_hp_ctx| is a packet
  3565. * header protection cipher context object for decryption. Similarly,
  3566. * |tx_aead_ctx|, |tx_iv| and |tx_hp_ctx| are for encrypting outgoing
  3567. * packets, and are the same length with the decryption counterpart.
  3568. * If they have already been set, they are overwritten.
  3569. *
  3570. * |ivlen| must be the minimum length of AEAD nonce, or 8 bytes if
  3571. * that is larger.
  3572. *
  3573. * If this function succeeds, |conn| takes ownership of |rx_aead_ctx|,
  3574. * |rx_hp_ctx|, |tx_aead_ctx|, and |tx_hp_ctx|.
  3575. * :member:`ngtcp2_callbacks.delete_crypto_aead_ctx` and
  3576. * :member:`ngtcp2_callbacks.delete_crypto_cipher_ctx` will be called
  3577. * to delete these objects when they are no longer used. If this
  3578. * function fails, the caller is responsible to delete them.
  3579. *
  3580. * This function returns 0 if it succeeds, or one of the following
  3581. * negative error codes:
  3582. *
  3583. * :macro:`NGTCP2_ERR_NOMEM`
  3584. * Out of memory.
  3585. */
  3586. NGTCP2_EXTERN int ngtcp2_conn_install_vneg_initial_key(
  3587. ngtcp2_conn *conn, uint32_t version,
  3588. const ngtcp2_crypto_aead_ctx *rx_aead_ctx, const uint8_t *rx_iv,
  3589. const ngtcp2_crypto_cipher_ctx *rx_hp_ctx,
  3590. const ngtcp2_crypto_aead_ctx *tx_aead_ctx, const uint8_t *tx_iv,
  3591. const ngtcp2_crypto_cipher_ctx *tx_hp_ctx, size_t ivlen);
  3592. /**
  3593. * @function
  3594. *
  3595. * `ngtcp2_conn_install_rx_handshake_key` installs packet protection
  3596. * keying materials for decrypting incoming Handshake packets.
  3597. * |aead_ctx| is AEAD cipher context object which must be initialized
  3598. * with a decryption key. |iv| is IV of length |ivlen|. |hp_ctx| is
  3599. * a packet header protection cipher context object.
  3600. *
  3601. * |ivlen| must be the minimum length of AEAD nonce, or 8 bytes if
  3602. * that is larger.
  3603. *
  3604. * If this function succeeds, |conn| takes ownership of |aead_ctx|,
  3605. * and |hp_ctx|. :member:`ngtcp2_callbacks.delete_crypto_aead_ctx`
  3606. * and :member:`ngtcp2_callbacks.delete_crypto_cipher_ctx` will be
  3607. * called to delete these objects when they are no longer used. If
  3608. * this function fails, the caller is responsible to delete them.
  3609. *
  3610. * This function returns 0 if it succeeds, or one of the following
  3611. * negative error codes:
  3612. *
  3613. * :macro:`NGTCP2_ERR_NOMEM`
  3614. * Out of memory.
  3615. */
  3616. NGTCP2_EXTERN int ngtcp2_conn_install_rx_handshake_key(
  3617. ngtcp2_conn *conn, const ngtcp2_crypto_aead_ctx *aead_ctx,
  3618. const uint8_t *iv, size_t ivlen, const ngtcp2_crypto_cipher_ctx *hp_ctx);
  3619. /**
  3620. * @function
  3621. *
  3622. * `ngtcp2_conn_install_tx_handshake_key` installs packet protection
  3623. * keying materials for encrypting outgoing Handshake packets.
  3624. * |aead_ctx| is AEAD cipher context object which must be initialized
  3625. * with an encryption key. |iv| is IV of length |ivlen|. |hp_ctx| is
  3626. * a packet header protection cipher context object.
  3627. *
  3628. * |ivlen| must be the minimum length of AEAD nonce, or 8 bytes if
  3629. * that is larger.
  3630. *
  3631. * If this function succeeds, |conn| takes ownership of |aead_ctx| and
  3632. * |hp_ctx|. :member:`ngtcp2_callbacks.delete_crypto_aead_ctx` and
  3633. * :member:`ngtcp2_callbacks.delete_crypto_cipher_ctx` will be called
  3634. * to delete these objects when they are no longer used. If this
  3635. * function fails, the caller is responsible to delete them.
  3636. *
  3637. * This function returns 0 if it succeeds, or one of the following
  3638. * negative error codes:
  3639. *
  3640. * :macro:`NGTCP2_ERR_NOMEM`
  3641. * Out of memory.
  3642. */
  3643. NGTCP2_EXTERN int ngtcp2_conn_install_tx_handshake_key(
  3644. ngtcp2_conn *conn, const ngtcp2_crypto_aead_ctx *aead_ctx,
  3645. const uint8_t *iv, size_t ivlen, const ngtcp2_crypto_cipher_ctx *hp_ctx);
  3646. /**
  3647. * @function
  3648. *
  3649. * `ngtcp2_conn_install_0rtt_key` installs packet protection AEAD
  3650. * cipher context object |aead_ctx|, IV |iv| of length |ivlen|, and
  3651. * packet header protection cipher context object |hp_ctx| to encrypt
  3652. * (for client) or decrypt (for server) 0-RTT packets.
  3653. *
  3654. * |ivlen| must be the minimum length of AEAD nonce, or 8 bytes if
  3655. * that is larger.
  3656. *
  3657. * If this function succeeds, |conn| takes ownership of |aead_ctx| and
  3658. * |hp_ctx|. :member:`ngtcp2_callbacks.delete_crypto_aead_ctx` and
  3659. * :member:`ngtcp2_callbacks.delete_crypto_cipher_ctx` will be called
  3660. * to delete these objects when they are no longer used. If this
  3661. * function fails, the caller is responsible to delete them.
  3662. *
  3663. * This function returns 0 if it succeeds, or one of the following
  3664. * negative error codes:
  3665. *
  3666. * :macro:`NGTCP2_ERR_NOMEM`
  3667. * Out of memory.
  3668. */
  3669. NGTCP2_EXTERN int ngtcp2_conn_install_0rtt_key(
  3670. ngtcp2_conn *conn, const ngtcp2_crypto_aead_ctx *aead_ctx,
  3671. const uint8_t *iv, size_t ivlen, const ngtcp2_crypto_cipher_ctx *hp_ctx);
  3672. /**
  3673. * @function
  3674. *
  3675. * `ngtcp2_conn_install_rx_key` installs packet protection keying
  3676. * materials for decrypting 1-RTT packets. |secret| of length
  3677. * |secretlen| is the decryption secret which is used to derive keying
  3678. * materials passed to this function. |aead_ctx| is AEAD cipher
  3679. * context object which must be initialized with a decryption key.
  3680. * |iv| is IV of length |ivlen|. |hp_ctx| is a packet header
  3681. * protection cipher context object.
  3682. *
  3683. * |ivlen| must be the minimum length of AEAD nonce, or 8 bytes if
  3684. * that is larger.
  3685. *
  3686. * If this function succeeds, |conn| takes ownership of |aead_ctx| and
  3687. * |hp_ctx|. :member:`ngtcp2_callbacks.delete_crypto_aead_ctx` and
  3688. * :member:`ngtcp2_callbacks.delete_crypto_cipher_ctx` will be called
  3689. * to delete these objects when they are no longer used. If this
  3690. * function fails, the caller is responsible to delete them.
  3691. *
  3692. * This function returns 0 if it succeeds, or one of the following
  3693. * negative error codes:
  3694. *
  3695. * :macro:`NGTCP2_ERR_NOMEM`
  3696. * Out of memory.
  3697. */
  3698. NGTCP2_EXTERN int ngtcp2_conn_install_rx_key(
  3699. ngtcp2_conn *conn, const uint8_t *secret, size_t secretlen,
  3700. const ngtcp2_crypto_aead_ctx *aead_ctx, const uint8_t *iv, size_t ivlen,
  3701. const ngtcp2_crypto_cipher_ctx *hp_ctx);
  3702. /**
  3703. * @function
  3704. *
  3705. * `ngtcp2_conn_install_tx_key` installs packet protection keying
  3706. * materials for encrypting 1-RTT packets. |secret| of length
  3707. * |secretlen| is the encryption secret which is used to derive keying
  3708. * materials passed to this function. |aead_ctx| is AEAD cipher
  3709. * context object which must be initialized with an encryption key.
  3710. * |iv| is IV of length |ivlen|. |hp_ctx| is a packet header
  3711. * protection cipher context object.
  3712. *
  3713. * |ivlen| must be the minimum length of AEAD nonce, or 8 bytes if
  3714. * that is larger.
  3715. *
  3716. * If this function succeeds, |conn| takes ownership of |aead_ctx| and
  3717. * |hp_ctx|. :member:`ngtcp2_callbacks.delete_crypto_aead_ctx` and
  3718. * :member:`ngtcp2_callbacks.delete_crypto_cipher_ctx` will be called
  3719. * to delete these objects when they are no longer used. If this
  3720. * function fails, the caller is responsible to delete them.
  3721. *
  3722. * This function returns 0 if it succeeds, or one of the following
  3723. * negative error codes:
  3724. *
  3725. * :macro:`NGTCP2_ERR_NOMEM`
  3726. * Out of memory.
  3727. */
  3728. NGTCP2_EXTERN int ngtcp2_conn_install_tx_key(
  3729. ngtcp2_conn *conn, const uint8_t *secret, size_t secretlen,
  3730. const ngtcp2_crypto_aead_ctx *aead_ctx, const uint8_t *iv, size_t ivlen,
  3731. const ngtcp2_crypto_cipher_ctx *hp_ctx);
  3732. /**
  3733. * @function
  3734. *
  3735. * `ngtcp2_conn_initiate_key_update` initiates the key update.
  3736. *
  3737. * This function returns 0 if it succeeds, or one of the following
  3738. * negative error codes:
  3739. *
  3740. * :macro:`NGTCP2_ERR_INVALID_STATE`
  3741. * The previous key update has not been confirmed yet; or key
  3742. * update is too frequent; or new keys are not available yet.
  3743. */
  3744. NGTCP2_EXTERN int ngtcp2_conn_initiate_key_update(ngtcp2_conn *conn,
  3745. ngtcp2_tstamp ts);
  3746. /**
  3747. * @function
  3748. *
  3749. * `ngtcp2_conn_set_tls_error` sets the TLS related error |liberr| in
  3750. * |conn|. |liberr| must be one of ngtcp2 library error codes (which
  3751. * is defined as NGTCP2_ERR_* macro, such as
  3752. * :macro:`NGTCP2_ERR_DECRYPT`). In general, error code should be
  3753. * propagated via return value, but sometimes ngtcp2 API is called
  3754. * inside callback function of TLS stack, and it does not allow to
  3755. * return ngtcp2 error code directly. In this case, implementation
  3756. * can set the error code (e.g.,
  3757. * :macro:`NGTCP2_ERR_MALFORMED_TRANSPORT_PARAM`) using this function.
  3758. *
  3759. * See also `ngtcp2_conn_get_tls_error`.
  3760. */
  3761. NGTCP2_EXTERN void ngtcp2_conn_set_tls_error(ngtcp2_conn *conn, int liberr);
  3762. /**
  3763. * @function
  3764. *
  3765. * `ngtcp2_conn_get_tls_error` returns the value set by
  3766. * `ngtcp2_conn_set_tls_error`. If no value is set, this function
  3767. * returns 0.
  3768. */
  3769. NGTCP2_EXTERN int ngtcp2_conn_get_tls_error(ngtcp2_conn *conn);
  3770. /**
  3771. * @function
  3772. *
  3773. * `ngtcp2_conn_set_tls_alert` sets a TLS alert |alert| generated by a
  3774. * TLS stack of a local endpoint to |conn|.
  3775. *
  3776. * See also `ngtcp2_conn_get_tls_alert`.
  3777. */
  3778. NGTCP2_EXTERN void ngtcp2_conn_set_tls_alert(ngtcp2_conn *conn, uint8_t alert);
  3779. /**
  3780. * @function
  3781. *
  3782. * `ngtcp2_conn_get_tls_alert` returns the value set by
  3783. * `ngtcp2_conn_set_tls_alert`. If no value is set, this function
  3784. * returns 0.
  3785. */
  3786. NGTCP2_EXTERN uint8_t ngtcp2_conn_get_tls_alert(ngtcp2_conn *conn);
  3787. /**
  3788. * @function
  3789. *
  3790. * `ngtcp2_conn_set_keep_alive_timeout` sets keep-alive timeout. If
  3791. * nonzero value is given, after a connection is idle at least in a
  3792. * given amount of time, a keep-alive packet is sent. If UINT64_MAX
  3793. * is set, keep-alive functionality is disabled, and this is the
  3794. * default. Specifying 0 in |timeout| is reserved for a future
  3795. * extension, and for now it is treated as if UINT64_MAX is given.
  3796. */
  3797. NGTCP2_EXTERN void ngtcp2_conn_set_keep_alive_timeout(ngtcp2_conn *conn,
  3798. ngtcp2_duration timeout);
  3799. /**
  3800. * @function
  3801. *
  3802. * `ngtcp2_conn_get_expiry` returns the next expiry time. It returns
  3803. * ``UINT64_MAX`` if there is no next expiry.
  3804. *
  3805. * Call `ngtcp2_conn_handle_expiry` and then
  3806. * `ngtcp2_conn_writev_stream` (or `ngtcp2_conn_writev_datagram`) when
  3807. * the expiry time has passed.
  3808. */
  3809. NGTCP2_EXTERN ngtcp2_tstamp ngtcp2_conn_get_expiry(ngtcp2_conn *conn);
  3810. /**
  3811. * @function
  3812. *
  3813. * `ngtcp2_conn_handle_expiry` handles expired timer.
  3814. */
  3815. NGTCP2_EXTERN int ngtcp2_conn_handle_expiry(ngtcp2_conn *conn,
  3816. ngtcp2_tstamp ts);
  3817. /**
  3818. * @function
  3819. *
  3820. * `ngtcp2_conn_get_pto` returns Probe Timeout (PTO).
  3821. */
  3822. NGTCP2_EXTERN ngtcp2_duration ngtcp2_conn_get_pto(ngtcp2_conn *conn);
  3823. /**
  3824. * @function
  3825. *
  3826. * `ngtcp2_conn_decode_and_set_remote_transport_params` decodes QUIC
  3827. * transport parameters from the buffer pointed by |data| of length
  3828. * |datalen|, and sets the result to |conn|.
  3829. *
  3830. * This function returns 0 if it succeeds, or one of the following
  3831. * negative error codes:
  3832. *
  3833. * :macro:`NGTCP2_ERR_REQUIRED_TRANSPORT_PARAM`
  3834. * The required parameter is missing.
  3835. * :macro:`NGTCP2_ERR_MALFORMED_TRANSPORT_PARAM`
  3836. * The input is malformed.
  3837. * :macro:`NGTCP2_ERR_TRANSPORT_PARAM`
  3838. * Failed to validate the remote QUIC transport parameters.
  3839. * :macro:`NGTCP2_ERR_VERSION_NEGOTIATION_FAILURE`
  3840. * Version negotiation failure.
  3841. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE`
  3842. * User callback failed
  3843. */
  3844. NGTCP2_EXTERN int ngtcp2_conn_decode_and_set_remote_transport_params(
  3845. ngtcp2_conn *conn, const uint8_t *data, size_t datalen);
  3846. /**
  3847. * @function
  3848. *
  3849. * `ngtcp2_conn_get_remote_transport_params` returns a pointer to the
  3850. * remote QUIC transport parameters. If no remote transport
  3851. * parameters are set, it returns NULL.
  3852. */
  3853. NGTCP2_EXTERN const ngtcp2_transport_params *
  3854. ngtcp2_conn_get_remote_transport_params(ngtcp2_conn *conn);
  3855. /**
  3856. * @function
  3857. *
  3858. * `ngtcp2_conn_encode_0rtt_transport_params` encodes the QUIC
  3859. * transport parameters that are used for 0-RTT data in the buffer
  3860. * pointed by |dest| of length |destlen|. It includes at least the
  3861. * following fields:
  3862. *
  3863. * - :member:`ngtcp2_transport_params.initial_max_streams_bidi`
  3864. * - :member:`ngtcp2_transport_params.initial_max_streams_uni`
  3865. * - :member:`ngtcp2_transport_params.initial_max_stream_data_bidi_local`
  3866. * - :member:`ngtcp2_transport_params.initial_max_stream_data_bidi_remote`
  3867. * - :member:`ngtcp2_transport_params.initial_max_stream_data_uni`
  3868. * - :member:`ngtcp2_transport_params.initial_max_data`
  3869. * - :member:`ngtcp2_transport_params.active_connection_id_limit`
  3870. * - :member:`ngtcp2_transport_params.max_datagram_frame_size`
  3871. *
  3872. * If |conn| is initialized as server, the following additional fields
  3873. * are also included:
  3874. *
  3875. * - :member:`ngtcp2_transport_params.max_idle_timeout`
  3876. * - :member:`ngtcp2_transport_params.max_udp_payload_size`
  3877. * - :member:`ngtcp2_transport_params.disable_active_migration`
  3878. *
  3879. * If |conn| is initialized as client, these parameters are
  3880. * synthesized from the remote transport parameters received from
  3881. * server. Otherwise, it is the local transport parameters that are
  3882. * set by the local endpoint.
  3883. *
  3884. * This function returns the number of bytes written, or one of the
  3885. * following negative error codes:
  3886. *
  3887. * :macro:`NGTCP2_ERR_NOBUF`
  3888. * Buffer is too small.
  3889. */
  3890. NGTCP2_EXTERN
  3891. ngtcp2_ssize ngtcp2_conn_encode_0rtt_transport_params(ngtcp2_conn *conn,
  3892. uint8_t *dest,
  3893. size_t destlen);
  3894. /**
  3895. * @function
  3896. *
  3897. * `ngtcp2_conn_decode_and_set_0rtt_transport_params` decodes QUIC
  3898. * transport parameters from |data| of length |datalen|, which is
  3899. * assumed to be the parameters received from the server in the
  3900. * previous connection, and sets it to |conn|. These parameters are
  3901. * used to send 0-RTT data. QUIC requires that client application
  3902. * should remember transport parameters along with a session ticket.
  3903. *
  3904. * At least following fields should be included:
  3905. *
  3906. * - :member:`ngtcp2_transport_params.initial_max_streams_bidi`
  3907. * - :member:`ngtcp2_transport_params.initial_max_streams_uni`
  3908. * - :member:`ngtcp2_transport_params.initial_max_stream_data_bidi_local`
  3909. * - :member:`ngtcp2_transport_params.initial_max_stream_data_bidi_remote`
  3910. * - :member:`ngtcp2_transport_params.initial_max_stream_data_uni`
  3911. * - :member:`ngtcp2_transport_params.initial_max_data`
  3912. * - :member:`ngtcp2_transport_params.active_connection_id_limit`
  3913. * - :member:`ngtcp2_transport_params.max_datagram_frame_size` (if
  3914. * DATAGRAM extension was negotiated)
  3915. *
  3916. * This function must only be used by client.
  3917. *
  3918. * This function returns 0 if it succeeds, or one of the following
  3919. * negative error codes:
  3920. *
  3921. * :macro:`NGTCP2_ERR_NOMEM`
  3922. * Out of memory.
  3923. * :macro:`NGTCP2_ERR_MALFORMED_TRANSPORT_PARAM`
  3924. * The input is malformed.
  3925. */
  3926. NGTCP2_EXTERN int ngtcp2_conn_decode_and_set_0rtt_transport_params(
  3927. ngtcp2_conn *conn, const uint8_t *data, size_t datalen);
  3928. /**
  3929. * @function
  3930. *
  3931. * `ngtcp2_conn_set_local_transport_params` sets the local transport
  3932. * parameters |params|. This function can only be called by server.
  3933. * Although the local transport parameters are passed to
  3934. * `ngtcp2_conn_server_new`, server might want to update them after
  3935. * ALPN is chosen. In that case, server can update the transport
  3936. * parameters with this function. Server must call this function
  3937. * before calling `ngtcp2_conn_install_tx_handshake_key`.
  3938. *
  3939. * This function returns 0 if it succeeds, or one of the following
  3940. * negative error codes:
  3941. *
  3942. * :macro:`NGTCP2_ERR_INVALID_STATE`
  3943. * `ngtcp2_conn_install_tx_handshake_key` has been called.
  3944. */
  3945. NGTCP2_EXTERN int ngtcp2_conn_set_local_transport_params_versioned(
  3946. ngtcp2_conn *conn, int transport_params_version,
  3947. const ngtcp2_transport_params *params);
  3948. /**
  3949. * @function
  3950. *
  3951. * `ngtcp2_conn_get_local_transport_params` returns a pointer to the
  3952. * local QUIC transport parameters.
  3953. */
  3954. NGTCP2_EXTERN const ngtcp2_transport_params *
  3955. ngtcp2_conn_get_local_transport_params(ngtcp2_conn *conn);
  3956. /**
  3957. * @function
  3958. *
  3959. * `ngtcp2_conn_encode_local_transport_params` encodes the local QUIC
  3960. * transport parameters in |dest| of length |destlen|.
  3961. *
  3962. * This function returns the number of bytes written, or one of the
  3963. * following negative error codes:
  3964. *
  3965. * :macro:`NGTCP2_ERR_NOBUF`
  3966. * Buffer is too small.
  3967. */
  3968. NGTCP2_EXTERN ngtcp2_ssize ngtcp2_conn_encode_local_transport_params(
  3969. ngtcp2_conn *conn, uint8_t *dest, size_t destlen);
  3970. /**
  3971. * @function
  3972. *
  3973. * `ngtcp2_conn_open_bidi_stream` opens new bidirectional stream. The
  3974. * |stream_user_data| is the user data specific to the stream. The
  3975. * stream ID of the opened stream is stored in |*pstream_id|.
  3976. *
  3977. * Application can call this function before handshake completes. For
  3978. * 0-RTT packet, application can call this function after calling
  3979. * `ngtcp2_conn_decode_and_set_0rtt_transport_params`. For 1-RTT
  3980. * packet, application can call this function after calling
  3981. * `ngtcp2_conn_decode_and_set_remote_transport_params` and
  3982. * `ngtcp2_conn_install_tx_key`. If ngtcp2 crypto support library is
  3983. * used, application can call this function after calling
  3984. * `ngtcp2_crypto_derive_and_install_tx_key` for 1-RTT packet.
  3985. *
  3986. * This function returns 0 if it succeeds, or one of the following
  3987. * negative error codes:
  3988. *
  3989. * :macro:`NGTCP2_ERR_NOMEM`
  3990. * Out of memory
  3991. * :macro:`NGTCP2_ERR_STREAM_ID_BLOCKED`
  3992. * The remote endpoint does not allow |stream_id| yet.
  3993. */
  3994. NGTCP2_EXTERN int ngtcp2_conn_open_bidi_stream(ngtcp2_conn *conn,
  3995. int64_t *pstream_id,
  3996. void *stream_user_data);
  3997. /**
  3998. * @function
  3999. *
  4000. * `ngtcp2_conn_open_uni_stream` opens new unidirectional stream. The
  4001. * |stream_user_data| is the user data specific to the stream. The
  4002. * stream ID of the opened stream is stored in |*pstream_id|.
  4003. *
  4004. * Application can call this function before handshake completes. For
  4005. * 0-RTT packet, application can call this function after calling
  4006. * `ngtcp2_conn_decode_and_set_0rtt_transport_params`. For 1-RTT
  4007. * packet, application can call this function after calling
  4008. * `ngtcp2_conn_decode_and_set_remote_transport_params` and
  4009. * `ngtcp2_conn_install_tx_key`. If ngtcp2 crypto support library is
  4010. * used, application can call this function after calling
  4011. * `ngtcp2_crypto_derive_and_install_tx_key` for 1-RTT packet.
  4012. *
  4013. * This function returns 0 if it succeeds, or one of the following
  4014. * negative error codes:
  4015. *
  4016. * :macro:`NGTCP2_ERR_NOMEM`
  4017. * Out of memory
  4018. * :macro:`NGTCP2_ERR_STREAM_ID_BLOCKED`
  4019. * The remote endpoint does not allow |stream_id| yet.
  4020. */
  4021. NGTCP2_EXTERN int ngtcp2_conn_open_uni_stream(ngtcp2_conn *conn,
  4022. int64_t *pstream_id,
  4023. void *stream_user_data);
  4024. /**
  4025. * @function
  4026. *
  4027. * `ngtcp2_conn_shutdown_stream` closes a stream denoted by
  4028. * |stream_id| abruptly. |app_error_code| is one of application error
  4029. * codes, and indicates the reason of shutdown. Successful call of
  4030. * this function does not immediately erase the state of the stream.
  4031. * The actual deletion is done when the remote endpoint sends
  4032. * acknowledgement. Calling this function is equivalent to call
  4033. * `ngtcp2_conn_shutdown_stream_read`, and
  4034. * `ngtcp2_conn_shutdown_stream_write` sequentially with the following
  4035. * differences. If |stream_id| refers to a local unidirectional
  4036. * stream, this function only shutdowns write side of the stream. If
  4037. * |stream_id| refers to a remote unidirectional stream, this function
  4038. * only shutdowns read side of the stream.
  4039. *
  4040. * |flags| is currently unused, and should be set to 0.
  4041. *
  4042. * This function returns 0 if a stream denoted by |stream_id| is not
  4043. * found.
  4044. *
  4045. * This function returns 0 if it succeeds, or one of the following
  4046. * negative error codes:
  4047. *
  4048. * :macro:`NGTCP2_ERR_NOMEM`
  4049. * Out of memory
  4050. */
  4051. NGTCP2_EXTERN int ngtcp2_conn_shutdown_stream(ngtcp2_conn *conn, uint32_t flags,
  4052. int64_t stream_id,
  4053. uint64_t app_error_code);
  4054. /**
  4055. * @function
  4056. *
  4057. * `ngtcp2_conn_shutdown_stream_write` closes write-side of a stream
  4058. * denoted by |stream_id| abruptly. |app_error_code| is one of
  4059. * application error codes, and indicates the reason of shutdown. If
  4060. * this function succeeds, no further application data is sent to the
  4061. * remote endpoint. It discards all data which has not been
  4062. * acknowledged yet.
  4063. *
  4064. * |flags| is currently unused, and should be set to 0.
  4065. *
  4066. * This function returns 0 if a stream denoted by |stream_id| is not
  4067. * found.
  4068. *
  4069. * This function returns 0 if it succeeds, or one of the following
  4070. * negative error codes:
  4071. *
  4072. * :macro:`NGTCP2_ERR_NOMEM`
  4073. * Out of memory
  4074. * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
  4075. * |stream_id| refers to a remote unidirectional stream.
  4076. */
  4077. NGTCP2_EXTERN int ngtcp2_conn_shutdown_stream_write(ngtcp2_conn *conn,
  4078. uint32_t flags,
  4079. int64_t stream_id,
  4080. uint64_t app_error_code);
  4081. /**
  4082. * @function
  4083. *
  4084. * `ngtcp2_conn_shutdown_stream_read` closes read-side of a stream
  4085. * denoted by |stream_id| abruptly. |app_error_code| is one of
  4086. * application error codes, and indicates the reason of shutdown. If
  4087. * this function succeeds, no further application data is forwarded to
  4088. * an application layer.
  4089. *
  4090. * |flags| is currently unused, and should be set to 0.
  4091. *
  4092. * This function returns 0 if a stream denoted by |stream_id| is not
  4093. * found.
  4094. *
  4095. * This function returns 0 if it succeeds, or one of the following
  4096. * negative error codes:
  4097. *
  4098. * :macro:`NGTCP2_ERR_NOMEM`
  4099. * Out of memory
  4100. * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
  4101. * |stream_id| refers to a local unidirectional stream.
  4102. */
  4103. NGTCP2_EXTERN int ngtcp2_conn_shutdown_stream_read(ngtcp2_conn *conn,
  4104. uint32_t flags,
  4105. int64_t stream_id,
  4106. uint64_t app_error_code);
  4107. /**
  4108. * @macrosection
  4109. *
  4110. * Write stream data flags
  4111. */
  4112. /**
  4113. * @macro
  4114. *
  4115. * :macro:`NGTCP2_WRITE_STREAM_FLAG_NONE` indicates no flag set.
  4116. */
  4117. #define NGTCP2_WRITE_STREAM_FLAG_NONE 0x00u
  4118. /**
  4119. * @macro
  4120. *
  4121. * :macro:`NGTCP2_WRITE_STREAM_FLAG_MORE` indicates that more data may
  4122. * come, and should be coalesced into the same packet if possible.
  4123. */
  4124. #define NGTCP2_WRITE_STREAM_FLAG_MORE 0x01u
  4125. /**
  4126. * @macro
  4127. *
  4128. * :macro:`NGTCP2_WRITE_STREAM_FLAG_FIN` indicates that a passed data
  4129. * is the final part of a stream.
  4130. */
  4131. #define NGTCP2_WRITE_STREAM_FLAG_FIN 0x02u
  4132. /**
  4133. * @function
  4134. *
  4135. * `ngtcp2_conn_write_stream` is just like
  4136. * `ngtcp2_conn_writev_stream`. The only difference is that it
  4137. * conveniently accepts a single buffer.
  4138. */
  4139. NGTCP2_EXTERN ngtcp2_ssize ngtcp2_conn_write_stream_versioned(
  4140. ngtcp2_conn *conn, ngtcp2_path *path, int pkt_info_version,
  4141. ngtcp2_pkt_info *pi, uint8_t *dest, size_t destlen, ngtcp2_ssize *pdatalen,
  4142. uint32_t flags, int64_t stream_id, const uint8_t *data, size_t datalen,
  4143. ngtcp2_tstamp ts);
  4144. /**
  4145. * @function
  4146. *
  4147. * `ngtcp2_conn_writev_stream` writes a packet containing stream data
  4148. * of a stream denoted by |stream_id|. The buffer of the packet is
  4149. * pointed by |dest| of length |destlen|. This function performs QUIC
  4150. * handshake as well.
  4151. *
  4152. * |destlen| should be at least
  4153. * :member:`ngtcp2_settings.max_tx_udp_payload_size`.
  4154. *
  4155. * Specifying -1 to |stream_id| means no new stream data to send.
  4156. *
  4157. * If |path| is not ``NULL``, this function stores the network path
  4158. * with which the packet should be sent. Each addr field
  4159. * (:member:`ngtcp2_path.local` and :member:`ngtcp2_path.remote`) must
  4160. * point to the buffer which should be at least
  4161. * sizeof(:type:`sockaddr_union`) bytes long. The assignment might
  4162. * not be done if nothing is written to |dest|.
  4163. *
  4164. * If |pi| is not ``NULL``, this function stores packet metadata in it
  4165. * if it succeeds. The metadata includes ECN markings. When calling
  4166. * this function again after it returns
  4167. * :macro:`NGTCP2_ERR_WRITE_MORE`, caller must pass the same |pi| to
  4168. * this function.
  4169. *
  4170. * Stream data is specified as vector of data |datav|. |datavcnt|
  4171. * specifies the number of :type:`ngtcp2_vec` that |datav| includes.
  4172. *
  4173. * If all given data is encoded as STREAM frame in |dest|, and if
  4174. * |flags| & :macro:`NGTCP2_WRITE_STREAM_FLAG_FIN` is nonzero, fin
  4175. * flag is set to outgoing STREAM frame. Otherwise, fin flag in
  4176. * STREAM frame is not set.
  4177. *
  4178. * This packet may contain frames other than STREAM frame. The packet
  4179. * might not contain STREAM frame if other frames occupy the packet.
  4180. * In that case, |*pdatalen| would be -1 if |pdatalen| is not
  4181. * ``NULL``.
  4182. *
  4183. * If |flags| & :macro:`NGTCP2_WRITE_STREAM_FLAG_FIN` is nonzero, and
  4184. * 0 length STREAM frame is successfully serialized, |*pdatalen| would
  4185. * be 0.
  4186. *
  4187. * The number of data encoded in STREAM frame is stored in |*pdatalen|
  4188. * if it is not ``NULL``. The caller must keep the portion of data
  4189. * covered by |*pdatalen| bytes in tact until
  4190. * :member:`ngtcp2_callbacks.acked_stream_data_offset` indicates that
  4191. * they are acknowledged by a remote endpoint or the stream is closed.
  4192. *
  4193. * If the given stream data is small (e.g., few bytes), the packet
  4194. * might be severely under filled. Too many small packet might
  4195. * increase overall packet processing costs. Unless there are
  4196. * retransmissions, by default, application can only send 1 STREAM
  4197. * frame in one QUIC packet. In order to include more than 1 STREAM
  4198. * frame in one QUIC packet, specify
  4199. * :macro:`NGTCP2_WRITE_STREAM_FLAG_MORE` in |flags|. This is
  4200. * analogous to ``MSG_MORE`` flag in :manpage:`send(2)`. If the
  4201. * :macro:`NGTCP2_WRITE_STREAM_FLAG_MORE` is used, there are 4
  4202. * outcomes:
  4203. *
  4204. * - The function returns the written length of packet just like
  4205. * without :macro:`NGTCP2_WRITE_STREAM_FLAG_MORE`. This is because
  4206. * packet is nearly full, and the library decided to make a complete
  4207. * packet. |*pdatalen| might be -1 or >= 0. It may return 0 which
  4208. * indicates that no packet transmission is possible at the moment
  4209. * for some reason.
  4210. *
  4211. * - The function returns :macro:`NGTCP2_ERR_WRITE_MORE`. In this
  4212. * case, |*pdatalen| >= 0 is asserted. It indicates that
  4213. * application can still call this function with different stream
  4214. * data (or `ngtcp2_conn_writev_datagram` if it has data to send in
  4215. * unreliable datagram) to pack them into the same packet.
  4216. * Application has to specify the same |conn|, |path|, |pi|, |dest|,
  4217. * |destlen|, and |ts| parameters, otherwise the behaviour is
  4218. * undefined. The application can change |flags|.
  4219. *
  4220. * - The function returns one of the following negative error codes:
  4221. * :macro:`NGTCP2_ERR_STREAM_DATA_BLOCKED`,
  4222. * :macro:`NGTCP2_ERR_STREAM_NOT_FOUND`, or
  4223. * :macro:`NGTCP2_ERR_STREAM_SHUT_WR`. In this case, |*pdatalen| ==
  4224. * -1 is asserted. Application can still write the stream data of
  4225. * the other streams by calling this function (or
  4226. * `ngtcp2_conn_writev_datagram` if it has data to send in
  4227. * unreliable datagram) to pack them into the same packet.
  4228. * Application has to specify the same |conn|, |path|, |pi|, |dest|,
  4229. * |destlen|, and |ts| parameters, otherwise the behaviour is
  4230. * undefined. The application can change |flags|.
  4231. *
  4232. * - The other negative error codes might be returned just like
  4233. * without :macro:`NGTCP2_WRITE_STREAM_FLAG_MORE`. These errors
  4234. * should be treated as a connection error.
  4235. *
  4236. * When application uses :macro:`NGTCP2_WRITE_STREAM_FLAG_MORE` at
  4237. * least once, it must not call other ngtcp2 API functions
  4238. * (application can still call `ngtcp2_conn_write_connection_close` to
  4239. * handle error from this function. It can also call
  4240. * `ngtcp2_conn_shutdown_stream_read`,
  4241. * `ngtcp2_conn_shutdown_stream_write`, and
  4242. * `ngtcp2_conn_shutdown_stream`), just keep calling this function (or
  4243. * `ngtcp2_conn_writev_datagram`) until it returns 0, a positive
  4244. * number (which indicates a complete packet is ready), or the error
  4245. * codes other than :macro:`NGTCP2_ERR_WRITE_MORE`,
  4246. * :macro:`NGTCP2_ERR_STREAM_DATA_BLOCKED`,
  4247. * :macro:`NGTCP2_ERR_STREAM_NOT_FOUND`, and
  4248. * :macro:`NGTCP2_ERR_STREAM_SHUT_WR`. If there is no stream data to
  4249. * include, call this function with |stream_id| as -1 to stop
  4250. * coalescing and write a packet.
  4251. *
  4252. * This function returns 0 if it cannot write any frame because buffer
  4253. * is too small, or packet is congestion limited. Application should
  4254. * keep reading and wait for congestion window to grow.
  4255. *
  4256. * This function must not be called from inside the callback
  4257. * functions.
  4258. *
  4259. * `ngtcp2_conn_update_pkt_tx_time` must be called after this
  4260. * function. Application may call this function multiple times before
  4261. * calling `ngtcp2_conn_update_pkt_tx_time`.
  4262. *
  4263. * This function returns the number of bytes written in |dest| if it
  4264. * succeeds, or one of the following negative error codes:
  4265. *
  4266. * :macro:`NGTCP2_ERR_NOMEM`
  4267. * Out of memory
  4268. * :macro:`NGTCP2_ERR_STREAM_NOT_FOUND`
  4269. * Stream does not exist
  4270. * :macro:`NGTCP2_ERR_STREAM_SHUT_WR`
  4271. * Stream is half closed (local); or stream is being reset.
  4272. * :macro:`NGTCP2_ERR_PKT_NUM_EXHAUSTED`
  4273. * Packet number is exhausted, and cannot send any more packet.
  4274. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE`
  4275. * User callback failed
  4276. * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
  4277. * The total length of stream data is too large.
  4278. * :macro:`NGTCP2_ERR_STREAM_DATA_BLOCKED`
  4279. * Stream is blocked because of flow control.
  4280. * :macro:`NGTCP2_ERR_WRITE_MORE`
  4281. * (Only when :macro:`NGTCP2_WRITE_STREAM_FLAG_MORE` is specified)
  4282. * Application can call this function to pack more stream data
  4283. * into the same packet. See above to know how it works.
  4284. *
  4285. * If any other negative error is returned, call
  4286. * `ngtcp2_conn_write_connection_close` to get terminal packet, and
  4287. * sending it makes QUIC connection enter the closing state.
  4288. */
  4289. NGTCP2_EXTERN ngtcp2_ssize ngtcp2_conn_writev_stream_versioned(
  4290. ngtcp2_conn *conn, ngtcp2_path *path, int pkt_info_version,
  4291. ngtcp2_pkt_info *pi, uint8_t *dest, size_t destlen, ngtcp2_ssize *pdatalen,
  4292. uint32_t flags, int64_t stream_id, const ngtcp2_vec *datav, size_t datavcnt,
  4293. ngtcp2_tstamp ts);
  4294. /**
  4295. * @macrosection
  4296. *
  4297. * Write datagram flags
  4298. */
  4299. /**
  4300. * @macro
  4301. *
  4302. * :macro:`NGTCP2_WRITE_DATAGRAM_FLAG_NONE` indicates no flag set.
  4303. */
  4304. #define NGTCP2_WRITE_DATAGRAM_FLAG_NONE 0x00u
  4305. /**
  4306. * @macro
  4307. *
  4308. * :macro:`NGTCP2_WRITE_DATAGRAM_FLAG_MORE` indicates that more data
  4309. * may come, and should be coalesced into the same packet if possible.
  4310. */
  4311. #define NGTCP2_WRITE_DATAGRAM_FLAG_MORE 0x01u
  4312. /**
  4313. * @function
  4314. *
  4315. * `ngtcp2_conn_write_datagram` is just like
  4316. * `ngtcp2_conn_writev_datagram`. The only difference is that it
  4317. * conveniently accepts a single buffer.
  4318. */
  4319. NGTCP2_EXTERN ngtcp2_ssize ngtcp2_conn_write_datagram_versioned(
  4320. ngtcp2_conn *conn, ngtcp2_path *path, int pkt_info_version,
  4321. ngtcp2_pkt_info *pi, uint8_t *dest, size_t destlen, int *paccepted,
  4322. uint32_t flags, uint64_t dgram_id, const uint8_t *data, size_t datalen,
  4323. ngtcp2_tstamp ts);
  4324. /**
  4325. * @function
  4326. *
  4327. * `ngtcp2_conn_writev_datagram` writes a packet containing unreliable
  4328. * data in DATAGRAM frame. The buffer of the packet is pointed by
  4329. * |dest| of length |destlen|. This function performs QUIC handshake
  4330. * as well.
  4331. *
  4332. * |destlen| should be at least
  4333. * :member:`ngtcp2_settings.max_tx_udp_payload_size`.
  4334. *
  4335. * For |path| and |pi| parameters, refer to
  4336. * `ngtcp2_conn_writev_stream`.
  4337. *
  4338. * Stream data is specified as vector of data |datav|. |datavcnt|
  4339. * specifies the number of :type:`ngtcp2_vec` that |datav| includes.
  4340. *
  4341. * If the given data is written to the buffer, nonzero value is
  4342. * assigned to |*paccepted| if it is not NULL. The data in DATAGRAM
  4343. * frame cannot be fragmented; writing partial data is not possible.
  4344. *
  4345. * |dgram_id| is an opaque identifier which should uniquely identify
  4346. * the given DATAGRAM data. It is passed to
  4347. * :member:`ngtcp2_callbacks.ack_datagram` callback when a packet that
  4348. * contains DATAGRAM frame is acknowledged. It is also passed to
  4349. * :member:`ngtcp2_callbacks.lost_datagram` callback when a packet
  4350. * that contains DATAGRAM frame is declared lost. If an application
  4351. * uses neither of those callbacks, it can sets 0 to this parameter.
  4352. *
  4353. * This function might write other frames other than DATAGRAM frame,
  4354. * just like `ngtcp2_conn_writev_stream`.
  4355. *
  4356. * If the function returns 0, it means that no more data cannot be
  4357. * sent because of congestion control limit; or, data does not fit
  4358. * into the provided buffer; or, a local endpoint, as a server, is
  4359. * unable to send data because of its amplification limit. In this
  4360. * case, |*paccepted| is assigned zero if it is not NULL.
  4361. *
  4362. * If :macro:`NGTCP2_WRITE_DATAGRAM_FLAG_MORE` is set in |flags|,
  4363. * there are 3 outcomes:
  4364. *
  4365. * - The function returns the written length of packet just like
  4366. * without :macro:`NGTCP2_WRITE_DATAGRAM_FLAG_MORE`. This is
  4367. * because packet is nearly full and the library decided to make a
  4368. * complete packet. |*paccepted| might be zero or nonzero.
  4369. *
  4370. * - The function returns :macro:`NGTCP2_ERR_WRITE_MORE`. In this
  4371. * case, |*paccepted| != 0 is asserted. This indicates that
  4372. * application can call this function with another unreliable data
  4373. * (or `ngtcp2_conn_writev_stream` if it has stream data to send) to
  4374. * pack them into the same packet. Application has to specify the
  4375. * same |conn|, |path|, |pi|, |dest|, |destlen|, and |ts|
  4376. * parameters, otherwise the behaviour is undefined. The
  4377. * application can change |flags|.
  4378. *
  4379. * - The other error might be returned just like without
  4380. * :macro:`NGTCP2_WRITE_DATAGRAM_FLAG_MORE`.
  4381. *
  4382. * When application sees :macro:`NGTCP2_ERR_WRITE_MORE`, it must not
  4383. * call other ngtcp2 API functions (application can still call
  4384. * `ngtcp2_conn_write_connection_close` to handle error from this
  4385. * function. It can also call `ngtcp2_conn_shutdown_stream_read`,
  4386. * `ngtcp2_conn_shutdown_stream_write`, and
  4387. * `ngtcp2_conn_shutdown_stream`). Just keep calling this function
  4388. * (or `ngtcp2_conn_writev_stream`) until it returns a positive number
  4389. * (which indicates a complete packet is ready).
  4390. *
  4391. * This function returns the number of bytes written in |dest| if it
  4392. * succeeds, or one of the following negative error codes:
  4393. *
  4394. * :macro:`NGTCP2_ERR_NOMEM`
  4395. * Out of memory
  4396. * :macro:`NGTCP2_ERR_PKT_NUM_EXHAUSTED`
  4397. * Packet number is exhausted, and cannot send any more packet.
  4398. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE`
  4399. * User callback failed
  4400. * :macro:`NGTCP2_ERR_WRITE_MORE`
  4401. * (Only when :macro:`NGTCP2_WRITE_DATAGRAM_FLAG_MORE` is
  4402. * specified) Application can call this function to pack more data
  4403. * into the same packet. See above to know how it works.
  4404. * :macro:`NGTCP2_ERR_INVALID_STATE`
  4405. * A remote endpoint did not express the DATAGRAM frame support.
  4406. * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
  4407. * The provisional DATAGRAM frame size exceeds the maximum
  4408. * DATAGRAM frame size that a remote endpoint can receive.
  4409. *
  4410. * If any other negative error is returned, call
  4411. * `ngtcp2_conn_write_connection_close` to get terminal packet, and
  4412. * sending it makes QUIC connection enter the closing state.
  4413. */
  4414. NGTCP2_EXTERN ngtcp2_ssize ngtcp2_conn_writev_datagram_versioned(
  4415. ngtcp2_conn *conn, ngtcp2_path *path, int pkt_info_version,
  4416. ngtcp2_pkt_info *pi, uint8_t *dest, size_t destlen, int *paccepted,
  4417. uint32_t flags, uint64_t dgram_id, const ngtcp2_vec *datav, size_t datavcnt,
  4418. ngtcp2_tstamp ts);
  4419. /**
  4420. * @function
  4421. *
  4422. * `ngtcp2_conn_in_closing_period` returns nonzero if |conn| is in the
  4423. * closing period.
  4424. */
  4425. NGTCP2_EXTERN int ngtcp2_conn_in_closing_period(ngtcp2_conn *conn);
  4426. /**
  4427. * @function
  4428. *
  4429. * `ngtcp2_conn_in_draining_period` returns nonzero if |conn| is in
  4430. * the draining period.
  4431. */
  4432. NGTCP2_EXTERN int ngtcp2_conn_in_draining_period(ngtcp2_conn *conn);
  4433. /**
  4434. * @function
  4435. *
  4436. * `ngtcp2_conn_extend_max_stream_offset` extends the maximum stream
  4437. * data that a remote endpoint can send by |datalen|. |stream_id|
  4438. * specifies the stream ID. This function only extends stream-level
  4439. * flow control window.
  4440. *
  4441. * This function returns 0 if a stream denoted by |stream_id| is not
  4442. * found.
  4443. *
  4444. * This function returns 0 if it succeeds, or one of the following
  4445. * negative error codes:
  4446. *
  4447. * :macro:`NGTCP2_ERR_NOMEM`
  4448. * Out of memory.
  4449. * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
  4450. * |stream_id| refers to a local unidirectional stream.
  4451. */
  4452. NGTCP2_EXTERN int ngtcp2_conn_extend_max_stream_offset(ngtcp2_conn *conn,
  4453. int64_t stream_id,
  4454. uint64_t datalen);
  4455. /**
  4456. * @function
  4457. *
  4458. * `ngtcp2_conn_extend_max_offset` extends max data offset by
  4459. * |datalen|. This function only extends connection-level flow
  4460. * control window.
  4461. */
  4462. NGTCP2_EXTERN void ngtcp2_conn_extend_max_offset(ngtcp2_conn *conn,
  4463. uint64_t datalen);
  4464. /**
  4465. * @function
  4466. *
  4467. * `ngtcp2_conn_extend_max_streams_bidi` extends the number of maximum
  4468. * remote bidirectional streams that a remote endpoint can open by
  4469. * |n|.
  4470. *
  4471. * The library does not increase maximum stream limit automatically.
  4472. * The exception is when a stream is closed without
  4473. * :member:`ngtcp2_callbacks.stream_open` callback being called. In
  4474. * this case, stream limit is increased automatically.
  4475. */
  4476. NGTCP2_EXTERN void ngtcp2_conn_extend_max_streams_bidi(ngtcp2_conn *conn,
  4477. size_t n);
  4478. /**
  4479. * @function
  4480. *
  4481. * `ngtcp2_conn_extend_max_streams_uni` extends the number of maximum
  4482. * remote unidirectional streams that a remote endpoint can open by
  4483. * |n|.
  4484. *
  4485. * The library does not increase maximum stream limit automatically.
  4486. * The exception is when a stream is closed without
  4487. * :member:`ngtcp2_callbacks.stream_open` callback being called. In
  4488. * this case, stream limit is increased automatically.
  4489. */
  4490. NGTCP2_EXTERN void ngtcp2_conn_extend_max_streams_uni(ngtcp2_conn *conn,
  4491. size_t n);
  4492. /**
  4493. * @function
  4494. *
  4495. * `ngtcp2_conn_get_dcid` returns the non-NULL pointer to the current
  4496. * Destination Connection ID. If no Destination Connection ID is
  4497. * present, the return value is not ``NULL``, and its :member:`datalen
  4498. * <ngtcp2_cid.datalen>` field is 0.
  4499. */
  4500. NGTCP2_EXTERN const ngtcp2_cid *ngtcp2_conn_get_dcid(ngtcp2_conn *conn);
  4501. /**
  4502. * @function
  4503. *
  4504. * `ngtcp2_conn_get_client_initial_dcid` returns the non-NULL pointer
  4505. * to the Destination Connection ID that client sent in its Initial
  4506. * packet. If the Destination Connection ID is not present, the
  4507. * return value is not ``NULL``, and its :member:`datalen
  4508. * <ngtcp2_cid.datalen>` field is 0.
  4509. */
  4510. NGTCP2_EXTERN const ngtcp2_cid *
  4511. ngtcp2_conn_get_client_initial_dcid(ngtcp2_conn *conn);
  4512. /**
  4513. * @function
  4514. *
  4515. * `ngtcp2_conn_get_scid` writes the all Source Connection IDs which a
  4516. * local endpoint has provided to a remote endpoint, and are not
  4517. * retired in |dest|. If |dest| is NULL, this function does not write
  4518. * anything, and returns the number of Source Connection IDs that
  4519. * would otherwise be written to the provided buffer. The buffer
  4520. * pointed by |dest| must have sizeof(:type:`ngtcp2_cid`) * n bytes
  4521. * available, where n is the return value of `ngtcp2_conn_get_scid`
  4522. * with |dest| == NULL.
  4523. */
  4524. NGTCP2_EXTERN size_t ngtcp2_conn_get_scid(ngtcp2_conn *conn, ngtcp2_cid *dest);
  4525. /**
  4526. * @struct
  4527. *
  4528. * :type:`ngtcp2_cid_token` is the convenient struct to store
  4529. * Connection ID, its associated path, and stateless reset token.
  4530. */
  4531. typedef struct ngtcp2_cid_token {
  4532. /**
  4533. * :member:`seq` is the sequence number of this Connection ID.
  4534. */
  4535. uint64_t seq;
  4536. /**
  4537. * :member:`cid` is Connection ID.
  4538. */
  4539. ngtcp2_cid cid;
  4540. /**
  4541. * :member:`ps` is the path which this Connection ID is associated
  4542. * with.
  4543. */
  4544. ngtcp2_path_storage ps;
  4545. /**
  4546. * :member:`token` is the stateless reset token for this Connection
  4547. * ID.
  4548. */
  4549. uint8_t token[NGTCP2_STATELESS_RESET_TOKENLEN];
  4550. /**
  4551. * :member:`token_present` is nonzero if token contains stateless
  4552. * reset token.
  4553. */
  4554. uint8_t token_present;
  4555. } ngtcp2_cid_token;
  4556. /**
  4557. * @function
  4558. *
  4559. * `ngtcp2_conn_get_active_dcid` writes the all active Destination
  4560. * Connection IDs and their tokens to |dest|. Before handshake
  4561. * completes, this function returns 0. If |dest| is NULL, this
  4562. * function does not write anything, and returns the number of
  4563. * Destination Connection IDs that would otherwise be written to the
  4564. * provided buffer. The buffer pointed by |dest| must have
  4565. * sizeof(:type:`ngtcp2_cid_token`) * n bytes available, where n is
  4566. * the return value of `ngtcp2_conn_get_active_dcid` with |dest| ==
  4567. * NULL.
  4568. */
  4569. NGTCP2_EXTERN size_t ngtcp2_conn_get_active_dcid(ngtcp2_conn *conn,
  4570. ngtcp2_cid_token *dest);
  4571. /**
  4572. * @function
  4573. *
  4574. * `ngtcp2_conn_get_client_chosen_version` returns the client chosen
  4575. * version.
  4576. */
  4577. NGTCP2_EXTERN uint32_t ngtcp2_conn_get_client_chosen_version(ngtcp2_conn *conn);
  4578. /**
  4579. * @function
  4580. *
  4581. * `ngtcp2_conn_get_negotiated_version` returns the negotiated
  4582. * version.
  4583. *
  4584. * Until the version is negotiated, this function returns 0.
  4585. */
  4586. NGTCP2_EXTERN uint32_t ngtcp2_conn_get_negotiated_version(ngtcp2_conn *conn);
  4587. /**
  4588. * @function
  4589. *
  4590. * `ngtcp2_conn_tls_early_data_rejected` tells |conn| that early data
  4591. * was rejected by a server during TLS handshake, or client decided
  4592. * not to attempt early data for some reason. |conn| discards the
  4593. * following connection states:
  4594. *
  4595. * - Any opened streams.
  4596. * - Stream identifier allocations.
  4597. * - Max data extended by `ngtcp2_conn_extend_max_offset`.
  4598. * - Max bidi streams extended by `ngtcp2_conn_extend_max_streams_bidi`.
  4599. * - Max uni streams extended by `ngtcp2_conn_extend_max_streams_uni`.
  4600. *
  4601. * Application which wishes to retransmit early data, it has to open
  4602. * streams, and send stream data again.
  4603. *
  4604. * This function returns 0 if it succeeds, or one of the following
  4605. * negative error codes:
  4606. *
  4607. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE`
  4608. * User callback failed
  4609. */
  4610. NGTCP2_EXTERN int ngtcp2_conn_tls_early_data_rejected(ngtcp2_conn *conn);
  4611. /**
  4612. * @function
  4613. *
  4614. * `ngtcp2_conn_get_tls_early_data_rejected` returns nonzero if
  4615. * `ngtcp2_conn_tls_early_data_rejected` has been called.
  4616. */
  4617. NGTCP2_EXTERN int ngtcp2_conn_get_tls_early_data_rejected(ngtcp2_conn *conn);
  4618. /**
  4619. * @function
  4620. *
  4621. * `ngtcp2_conn_get_conn_info` assigns connection statistics data to
  4622. * |*cinfo|.
  4623. */
  4624. NGTCP2_EXTERN void ngtcp2_conn_get_conn_info_versioned(ngtcp2_conn *conn,
  4625. int conn_info_version,
  4626. ngtcp2_conn_info *cinfo);
  4627. /**
  4628. * @function
  4629. *
  4630. * `ngtcp2_conn_submit_crypto_data` submits crypto data |data| of
  4631. * length |datalen| to the library for transmission.
  4632. * |encryption_level| specifies the encryption level of data.
  4633. *
  4634. * The library makes a copy of the buffer pointed by |data| of length
  4635. * |datalen|. Application can discard |data|.
  4636. */
  4637. NGTCP2_EXTERN int
  4638. ngtcp2_conn_submit_crypto_data(ngtcp2_conn *conn,
  4639. ngtcp2_encryption_level encryption_level,
  4640. const uint8_t *data, const size_t datalen);
  4641. /**
  4642. * @function
  4643. *
  4644. * `ngtcp2_conn_submit_new_token` submits address validation token.
  4645. * It is sent in NEW_TOKEN frame. Only server can call this function.
  4646. * |tokenlen| must not be 0.
  4647. *
  4648. * This function makes a copy of the buffer pointed by |token| of
  4649. * length |tokenlen|.
  4650. *
  4651. * This function returns 0 if it succeeds, or one of the following
  4652. * negative error codes:
  4653. *
  4654. * :macro:`NGTCP2_ERR_NOMEM`
  4655. * Out of memory.
  4656. */
  4657. NGTCP2_EXTERN int ngtcp2_conn_submit_new_token(ngtcp2_conn *conn,
  4658. const uint8_t *token,
  4659. size_t tokenlen);
  4660. /**
  4661. * @function
  4662. *
  4663. * `ngtcp2_conn_set_local_addr` sets local endpoint address |addr| to
  4664. * the current path of |conn|. This function is provided for testing
  4665. * purpose only.
  4666. */
  4667. NGTCP2_EXTERN void ngtcp2_conn_set_local_addr(ngtcp2_conn *conn,
  4668. const ngtcp2_addr *addr);
  4669. /**
  4670. * @function
  4671. *
  4672. * `ngtcp2_conn_set_path_user_data` sets the |path_user_data| to the
  4673. * current path (see :member:`ngtcp2_path.user_data`).
  4674. */
  4675. NGTCP2_EXTERN void ngtcp2_conn_set_path_user_data(ngtcp2_conn *conn,
  4676. void *path_user_data);
  4677. /**
  4678. * @function
  4679. *
  4680. * `ngtcp2_conn_get_path` returns the current path.
  4681. */
  4682. NGTCP2_EXTERN const ngtcp2_path *ngtcp2_conn_get_path(ngtcp2_conn *conn);
  4683. /**
  4684. * @function
  4685. *
  4686. * `ngtcp2_conn_get_max_tx_udp_payload_size` returns the maximum UDP
  4687. * payload size that this local endpoint would send. This is the
  4688. * value of :member:`ngtcp2_settings.max_tx_udp_payload_size` that is
  4689. * passed to `ngtcp2_conn_client_new` or `ngtcp2_conn_server_new`.
  4690. */
  4691. NGTCP2_EXTERN size_t ngtcp2_conn_get_max_tx_udp_payload_size(ngtcp2_conn *conn);
  4692. /**
  4693. * @function
  4694. *
  4695. * `ngtcp2_conn_get_path_max_tx_udp_payload_size` returns the maximum
  4696. * UDP payload size for the current path. If
  4697. * :member:`ngtcp2_settings.no_tx_udp_payload_size_shaping` is set to
  4698. * nonzero, this function is equivalent to
  4699. * `ngtcp2_conn_get_max_tx_udp_payload_size`. Otherwise, it returns
  4700. * the maximum UDP payload size that is probed for the current path.
  4701. */
  4702. NGTCP2_EXTERN size_t
  4703. ngtcp2_conn_get_path_max_tx_udp_payload_size(ngtcp2_conn *conn);
  4704. /**
  4705. * @function
  4706. *
  4707. * `ngtcp2_conn_initiate_immediate_migration` starts connection
  4708. * migration to the given |path|. Only client can initiate migration.
  4709. * This function does immediate migration; while the path validation
  4710. * is nonetheless performed, this function does not wait for it to
  4711. * succeed.
  4712. *
  4713. * This function returns 0 if it succeeds, or one of the following
  4714. * negative error codes:
  4715. *
  4716. * :macro:`NGTCP2_ERR_INVALID_STATE`
  4717. * Migration is disabled; or handshake is not yet confirmed; or
  4718. * client is migrating to server's preferred address.
  4719. * :macro:`NGTCP2_ERR_CONN_ID_BLOCKED`
  4720. * No unused connection ID is available.
  4721. * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
  4722. * :member:`local <ngtcp2_path.local>` field of |path| equals the
  4723. * current local address.
  4724. * :macro:`NGTCP2_ERR_NOMEM`
  4725. * Out of memory
  4726. */
  4727. NGTCP2_EXTERN int ngtcp2_conn_initiate_immediate_migration(
  4728. ngtcp2_conn *conn, const ngtcp2_path *path, ngtcp2_tstamp ts);
  4729. /**
  4730. * @function
  4731. *
  4732. * `ngtcp2_conn_initiate_migration` starts connection migration to the
  4733. * given |path|. Only client can initiate migration. Unlike
  4734. * `ngtcp2_conn_initiate_immediate_migration`, this function starts a
  4735. * path validation with a new path, and migrate to the new path after
  4736. * successful path validation.
  4737. *
  4738. * This function returns 0 if it succeeds, or one of the following
  4739. * negative error codes:
  4740. *
  4741. * :macro:`NGTCP2_ERR_INVALID_STATE`
  4742. * Migration is disabled; or handshake is not yet confirmed; or
  4743. * client is migrating to server's preferred address.
  4744. * :macro:`NGTCP2_ERR_CONN_ID_BLOCKED`
  4745. * No unused connection ID is available.
  4746. * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
  4747. * :member:`local <ngtcp2_path.local>` field of |path| equals the
  4748. * current local address.
  4749. * :macro:`NGTCP2_ERR_NOMEM`
  4750. * Out of memory
  4751. */
  4752. NGTCP2_EXTERN int ngtcp2_conn_initiate_migration(ngtcp2_conn *conn,
  4753. const ngtcp2_path *path,
  4754. ngtcp2_tstamp ts);
  4755. /**
  4756. * @function
  4757. *
  4758. * `ngtcp2_conn_get_max_data_left` returns the number of bytes that
  4759. * this local endpoint can send in this connection without violating
  4760. * connection-level flow control.
  4761. */
  4762. NGTCP2_EXTERN uint64_t ngtcp2_conn_get_max_data_left(ngtcp2_conn *conn);
  4763. /**
  4764. * @function
  4765. *
  4766. * `ngtcp2_conn_get_max_stream_data_left` returns the number of bytes
  4767. * that this local endpoint can send to a stream identified by
  4768. * |stream_id| without violating stream-level flow control. If no
  4769. * such stream is found, this function returns 0.
  4770. */
  4771. NGTCP2_EXTERN uint64_t ngtcp2_conn_get_max_stream_data_left(ngtcp2_conn *conn,
  4772. int64_t stream_id);
  4773. /**
  4774. * @function
  4775. *
  4776. * `ngtcp2_conn_get_streams_bidi_left` returns the number of
  4777. * bidirectional streams which the local endpoint can open without
  4778. * violating stream concurrency limit.
  4779. */
  4780. NGTCP2_EXTERN uint64_t ngtcp2_conn_get_streams_bidi_left(ngtcp2_conn *conn);
  4781. /**
  4782. * @function
  4783. *
  4784. * `ngtcp2_conn_get_streams_uni_left` returns the number of
  4785. * unidirectional streams which the local endpoint can open without
  4786. * violating stream concurrency limit.
  4787. */
  4788. NGTCP2_EXTERN uint64_t ngtcp2_conn_get_streams_uni_left(ngtcp2_conn *conn);
  4789. /**
  4790. * @function
  4791. *
  4792. * `ngtcp2_conn_get_cwnd_left` returns the cwnd minus the number of
  4793. * bytes in flight on the current path. If the former is smaller than
  4794. * the latter, this function returns 0.
  4795. */
  4796. NGTCP2_EXTERN uint64_t ngtcp2_conn_get_cwnd_left(ngtcp2_conn *conn);
  4797. /**
  4798. * @function
  4799. *
  4800. * `ngtcp2_conn_set_initial_crypto_ctx` sets |ctx| for Initial packet
  4801. * encryption. The passed data will be passed to
  4802. * :type:`ngtcp2_encrypt`, :type:`ngtcp2_decrypt` and
  4803. * :type:`ngtcp2_hp_mask` callbacks.
  4804. */
  4805. NGTCP2_EXTERN void
  4806. ngtcp2_conn_set_initial_crypto_ctx(ngtcp2_conn *conn,
  4807. const ngtcp2_crypto_ctx *ctx);
  4808. /**
  4809. * @function
  4810. *
  4811. * `ngtcp2_conn_get_initial_crypto_ctx` returns
  4812. * :type:`ngtcp2_crypto_ctx` object for Initial packet encryption.
  4813. */
  4814. NGTCP2_EXTERN const ngtcp2_crypto_ctx *
  4815. ngtcp2_conn_get_initial_crypto_ctx(ngtcp2_conn *conn);
  4816. /**
  4817. * @function
  4818. *
  4819. * `ngtcp2_conn_set_crypto_ctx` sets |ctx| for Handshake/1-RTT packet
  4820. * encryption. The passed data will be passed to
  4821. * :type:`ngtcp2_encrypt`, :type:`ngtcp2_decrypt` and
  4822. * :type:`ngtcp2_hp_mask` callbacks.
  4823. */
  4824. NGTCP2_EXTERN void ngtcp2_conn_set_crypto_ctx(ngtcp2_conn *conn,
  4825. const ngtcp2_crypto_ctx *ctx);
  4826. /**
  4827. * @function
  4828. *
  4829. * `ngtcp2_conn_get_crypto_ctx` returns :type:`ngtcp2_crypto_ctx`
  4830. * object for Handshake/1-RTT packet encryption.
  4831. */
  4832. NGTCP2_EXTERN const ngtcp2_crypto_ctx *
  4833. ngtcp2_conn_get_crypto_ctx(ngtcp2_conn *conn);
  4834. /**
  4835. * @function
  4836. *
  4837. * `ngtcp2_conn_set_0rtt_crypto_ctx` sets |ctx| for 0-RTT packet
  4838. * encryption. The passed data will be passed to
  4839. * :type:`ngtcp2_encrypt`, :type:`ngtcp2_decrypt` and
  4840. * :type:`ngtcp2_hp_mask` callbacks.
  4841. */
  4842. NGTCP2_EXTERN void
  4843. ngtcp2_conn_set_0rtt_crypto_ctx(ngtcp2_conn *conn,
  4844. const ngtcp2_crypto_ctx *ctx);
  4845. /**
  4846. * @function
  4847. *
  4848. * `ngtcp2_conn_get_0rtt_crypto_ctx` returns :type:`ngtcp2_crypto_ctx`
  4849. * object for 0-RTT packet encryption.
  4850. */
  4851. NGTCP2_EXTERN const ngtcp2_crypto_ctx *
  4852. ngtcp2_conn_get_0rtt_crypto_ctx(ngtcp2_conn *conn);
  4853. /**
  4854. * @function
  4855. *
  4856. * `ngtcp2_conn_get_tls_native_handle` returns TLS native handle set
  4857. * by `ngtcp2_conn_set_tls_native_handle`.
  4858. */
  4859. NGTCP2_EXTERN void *ngtcp2_conn_get_tls_native_handle(ngtcp2_conn *conn);
  4860. /**
  4861. * @function
  4862. *
  4863. * `ngtcp2_conn_set_tls_native_handle` sets TLS native handle
  4864. * |tls_native_handle| to |conn|. Internally, it is used as an opaque
  4865. * pointer.
  4866. */
  4867. NGTCP2_EXTERN void ngtcp2_conn_set_tls_native_handle(ngtcp2_conn *conn,
  4868. void *tls_native_handle);
  4869. /**
  4870. * @function
  4871. *
  4872. * `ngtcp2_conn_set_retry_aead` sets |aead| and |aead_ctx| for Retry
  4873. * integrity tag verification. |aead| must be AEAD_AES_128_GCM.
  4874. * |aead_ctx| must be initialized with :macro:`NGTCP2_RETRY_KEY` as
  4875. * encryption key. This function must be called if |conn| is
  4876. * initialized as client. Server does not verify the tag, and has no
  4877. * need to call this function.
  4878. *
  4879. * |conn| takes ownership of |aead_ctx|.
  4880. * :member:`ngtcp2_callbacks.delete_crypto_aead_ctx` will be called to
  4881. * delete this object when it is no longer used.
  4882. */
  4883. NGTCP2_EXTERN void
  4884. ngtcp2_conn_set_retry_aead(ngtcp2_conn *conn, const ngtcp2_crypto_aead *aead,
  4885. const ngtcp2_crypto_aead_ctx *aead_ctx);
  4886. /**
  4887. * @enum
  4888. *
  4889. * :type:`ngtcp2_ccerr_type` defines connection error type.
  4890. */
  4891. typedef enum ngtcp2_ccerr_type {
  4892. /**
  4893. * :enum:`NGTCP2_CCERR_TYPE_TRANSPORT` indicates the QUIC transport
  4894. * error, and the error code is QUIC transport error code.
  4895. */
  4896. NGTCP2_CCERR_TYPE_TRANSPORT,
  4897. /**
  4898. * :enum:`NGTCP2_CCERR_TYPE_APPLICATION` indicates an application
  4899. * error, and the error code is application error code.
  4900. */
  4901. NGTCP2_CCERR_TYPE_APPLICATION,
  4902. /**
  4903. * :enum:`NGTCP2_CCERR_TYPE_VERSION_NEGOTIATION` is a special case
  4904. * of QUIC transport error, and it indicates that client receives
  4905. * Version Negotiation packet.
  4906. */
  4907. NGTCP2_CCERR_TYPE_VERSION_NEGOTIATION,
  4908. /**
  4909. * :enum:`NGTCP2_CCERR_TYPE_IDLE_CLOSE` is a special case of QUIC
  4910. * transport error, and it indicates that connection is closed
  4911. * because of idle timeout.
  4912. */
  4913. NGTCP2_CCERR_TYPE_IDLE_CLOSE
  4914. } ngtcp2_ccerr_type;
  4915. /**
  4916. * @struct
  4917. *
  4918. * :type:`ngtcp2_ccerr` contains connection error code, its type, a
  4919. * frame type that caused this error, and the optional reason phrase.
  4920. */
  4921. typedef struct ngtcp2_ccerr {
  4922. /**
  4923. * :member:`type` is the type of this error.
  4924. */
  4925. ngtcp2_ccerr_type type;
  4926. /**
  4927. * :member:`error_code` is the error code for connection closure.
  4928. * Its interpretation depends on :member:`type`.
  4929. */
  4930. uint64_t error_code;
  4931. /**
  4932. * :member:`frame_type` is the type of QUIC frame which triggers
  4933. * this connection error. This field is set to 0 if the frame type
  4934. * is unknown.
  4935. */
  4936. uint64_t frame_type;
  4937. /**
  4938. * :member:`reason` points to the buffer which contains a reason
  4939. * phrase. It may be NULL if there is no reason phrase. If it is
  4940. * received from a remote endpoint, it is truncated to at most 1024
  4941. * bytes.
  4942. */
  4943. const uint8_t *reason;
  4944. /**
  4945. * :member:`reasonlen` is the length of data pointed by
  4946. * :member:`reason`.
  4947. */
  4948. size_t reasonlen;
  4949. } ngtcp2_ccerr;
  4950. /**
  4951. * @function
  4952. *
  4953. * `ngtcp2_ccerr_default` initializes |ccerr| with the default values.
  4954. * It sets the following fields:
  4955. *
  4956. * - :member:`type <ngtcp2_ccerr.type>` =
  4957. * :enum:`ngtcp2_ccerr_type.NGTCP2_CCERR_TYPE_TRANSPORT`
  4958. * - :member:`error_code <ngtcp2_ccerr.error_code>` =
  4959. * :macro:`NGTCP2_NO_ERROR`.
  4960. * - :member:`frame_type <ngtcp2_ccerr.frame_type>` = 0
  4961. * - :member:`reason <ngtcp2_ccerr.reason>` = NULL
  4962. * - :member:`reasonlen <ngtcp2_ccerr.reasonlen>` = 0
  4963. */
  4964. NGTCP2_EXTERN void ngtcp2_ccerr_default(ngtcp2_ccerr *ccerr);
  4965. /**
  4966. * @function
  4967. *
  4968. * `ngtcp2_ccerr_set_transport_error` sets :member:`ccerr->type
  4969. * <ngtcp2_ccerr.type>` to
  4970. * :enum:`ngtcp2_ccerr_type.NGTCP2_CCERR_TYPE_TRANSPORT`, and
  4971. * :member:`ccerr->error_code <ngtcp2_ccerr.error_code>` to
  4972. * |error_code|. |reason| is the reason phrase of length |reasonlen|.
  4973. * This function does not make a copy of the reason phrase.
  4974. */
  4975. NGTCP2_EXTERN void ngtcp2_ccerr_set_transport_error(ngtcp2_ccerr *ccerr,
  4976. uint64_t error_code,
  4977. const uint8_t *reason,
  4978. size_t reasonlen);
  4979. /**
  4980. * @function
  4981. *
  4982. * `ngtcp2_ccerr_set_liberr` sets type and error_code based on
  4983. * |liberr|.
  4984. *
  4985. * |reason| is the reason phrase of length |reasonlen|. This function
  4986. * does not make a copy of the reason phrase.
  4987. *
  4988. * If |liberr| is :macro:`NGTCP2_ERR_RECV_VERSION_NEGOTIATION`,
  4989. * :member:`ccerr->type <ngtcp2_ccerr.type>` is set to
  4990. * :enum:`ngtcp2_ccerr_type.NGTCP2_CCERR_TYPE_VERSION_NEGOTIATION`,
  4991. * and :member:`ccerr->error_code <ngtcp2_ccerr.error_code>` to
  4992. * :macro:`NGTCP2_NO_ERROR`.
  4993. *
  4994. * If |liberr| is :macro:`NGTCP2_ERR_IDLE_CLOSE`, :member:`ccerr->type
  4995. * <ngtcp2_ccerr.type>` is set to
  4996. * :enum:`ngtcp2_ccerr_type.NGTCP2_CCERR_TYPE_IDLE_CLOSE`, and
  4997. * :member:`ccerr->error_code <ngtcp2_ccerr.error_code>` to
  4998. * :macro:`NGTCP2_NO_ERROR`.
  4999. *
  5000. * Otherwise, :member:`ccerr->type <ngtcp2_ccerr.type>` is set to
  5001. * :enum:`ngtcp2_ccerr_type.NGTCP2_CCERR_TYPE_TRANSPORT`, and
  5002. * :member:`ccerr->error_code <ngtcp2_ccerr.error_code>` is set to an
  5003. * error code inferred by |liberr| (see
  5004. * `ngtcp2_err_infer_quic_transport_error_code`).
  5005. */
  5006. NGTCP2_EXTERN void ngtcp2_ccerr_set_liberr(ngtcp2_ccerr *ccerr, int liberr,
  5007. const uint8_t *reason,
  5008. size_t reasonlen);
  5009. /**
  5010. * @function
  5011. *
  5012. * `ngtcp2_ccerr_set_tls_alert` sets :member:`ccerr->type
  5013. * <ngtcp2_ccerr.type>` to
  5014. * :enum:`ngtcp2_ccerr_type.NGTCP2_CCERR_TYPE_TRANSPORT`, and
  5015. * :member:`ccerr->error_code <ngtcp2_ccerr.error_code>` to bitwise-OR
  5016. * of :macro:`NGTCP2_CRYPTO_ERROR` and |tls_alert|. |reason| is the
  5017. * reason phrase of length |reasonlen|. This function does not make a
  5018. * copy of the reason phrase.
  5019. */
  5020. NGTCP2_EXTERN void ngtcp2_ccerr_set_tls_alert(ngtcp2_ccerr *ccerr,
  5021. uint8_t tls_alert,
  5022. const uint8_t *reason,
  5023. size_t reasonlen);
  5024. /**
  5025. * @function
  5026. *
  5027. * `ngtcp2_ccerr_set_application_error` sets :member:`ccerr->type
  5028. * <ngtcp2_ccerr.type>` to
  5029. * :enum:`ngtcp2_ccerr_type.NGTCP2_CCERR_TYPE_APPLICATION`, and
  5030. * :member:`ccerr->error_code <ngtcp2_ccerr.error_code>` to
  5031. * |error_code|. |reason| is the reason phrase of length |reasonlen|.
  5032. * This function does not make a copy of the reason phrase.
  5033. */
  5034. NGTCP2_EXTERN void ngtcp2_ccerr_set_application_error(ngtcp2_ccerr *ccerr,
  5035. uint64_t error_code,
  5036. const uint8_t *reason,
  5037. size_t reasonlen);
  5038. /**
  5039. * @function
  5040. *
  5041. * `ngtcp2_conn_write_connection_close` writes a packet which contains
  5042. * CONNECTION_CLOSE frame(s) (type 0x1c or 0x1d) in the buffer pointed
  5043. * by |dest| whose capacity is |destlen|.
  5044. *
  5045. * For client, |destlen| should be at least
  5046. * :macro:`NGTCP2_MAX_UDP_PAYLOAD_SIZE`.
  5047. *
  5048. * If |path| is not ``NULL``, this function stores the network path
  5049. * with which the packet should be sent. Each addr field must point
  5050. * to the buffer which should be at least
  5051. * sizeof(:type:`ngtcp2_sockaddr_union`) bytes long. The assignment
  5052. * might not be done if nothing is written to |dest|.
  5053. *
  5054. * If |pi| is not ``NULL``, this function stores packet metadata in it
  5055. * if it succeeds. The metadata includes ECN markings.
  5056. *
  5057. * If :member:`ccerr->type <ngtcp2_ccerr.type>` ==
  5058. * :enum:`ngtcp2_ccerr_type.NGTCP2_CCERR_TYPE_TRANSPORT`, this
  5059. * function sends CONNECTION_CLOSE (type 0x1c) frame. If
  5060. * :member:`ccerr->type <ngtcp2_ccerr.type>` ==
  5061. * :enum:`ngtcp2_ccerr_type.NGTCP2_CCERR_TYPE_APPLICATION`, it sends
  5062. * CONNECTION_CLOSE (type 0x1d) frame. Otherwise, it does not produce
  5063. * any data, and returns 0.
  5064. *
  5065. * |destlen| could be shorten by some factors (e.g., server side
  5066. * amplification limit). This function returns
  5067. * :macro:`NGTCP2_ERR_NOBUF` if the resulting buffer is too small even
  5068. * if the given buffer has enough space.
  5069. *
  5070. * This function must not be called from inside the callback
  5071. * functions.
  5072. *
  5073. * At the moment, successful call to this function makes connection
  5074. * close. We may change this behaviour in the future to allow
  5075. * graceful shutdown.
  5076. *
  5077. * This function returns the number of bytes written in |dest| if it
  5078. * succeeds, or one of the following negative error codes:
  5079. *
  5080. * :macro:`NGTCP2_ERR_NOMEM`
  5081. * Out of memory
  5082. * :macro:`NGTCP2_ERR_NOBUF`
  5083. * Buffer is too small
  5084. * :macro:`NGTCP2_ERR_INVALID_STATE`
  5085. * The current state does not allow sending CONNECTION_CLOSE
  5086. * frame.
  5087. * :macro:`NGTCP2_ERR_PKT_NUM_EXHAUSTED`
  5088. * Packet number is exhausted, and cannot send any more packet.
  5089. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE`
  5090. * User callback failed
  5091. */
  5092. NGTCP2_EXTERN ngtcp2_ssize ngtcp2_conn_write_connection_close_versioned(
  5093. ngtcp2_conn *conn, ngtcp2_path *path, int pkt_info_version,
  5094. ngtcp2_pkt_info *pi, uint8_t *dest, size_t destlen,
  5095. const ngtcp2_ccerr *ccerr, ngtcp2_tstamp ts);
  5096. /**
  5097. * @function
  5098. *
  5099. * `ngtcp2_conn_get_ccerr` returns the received connection close
  5100. * error. If no connection error is received, it returns
  5101. * :type:`ngtcp2_ccerr` that is initialized by `ngtcp2_ccerr_default`.
  5102. */
  5103. NGTCP2_EXTERN const ngtcp2_ccerr *ngtcp2_conn_get_ccerr(ngtcp2_conn *conn);
  5104. /**
  5105. * @function
  5106. *
  5107. * `ngtcp2_conn_is_local_stream` returns nonzero if |stream_id|
  5108. * denotes a locally initiated stream.
  5109. */
  5110. NGTCP2_EXTERN int ngtcp2_conn_is_local_stream(ngtcp2_conn *conn,
  5111. int64_t stream_id);
  5112. /**
  5113. * @function
  5114. *
  5115. * `ngtcp2_conn_is_server` returns nonzero if |conn| is initialized as
  5116. * server.
  5117. */
  5118. NGTCP2_EXTERN int ngtcp2_conn_is_server(ngtcp2_conn *conn);
  5119. /**
  5120. * @function
  5121. *
  5122. * `ngtcp2_conn_after_retry` returns nonzero if |conn| as a client has
  5123. * received Retry packet from server, and successfully validated it.
  5124. */
  5125. NGTCP2_EXTERN int ngtcp2_conn_after_retry(ngtcp2_conn *conn);
  5126. /**
  5127. * @function
  5128. *
  5129. * `ngtcp2_conn_set_stream_user_data` sets |stream_user_data| to the
  5130. * stream identified by |stream_id|.
  5131. *
  5132. * This function returns 0 if it succeeds, or one of the following
  5133. * negative error codes:
  5134. *
  5135. * :macro:`NGTCP2_ERR_STREAM_NOT_FOUND`
  5136. * Stream does not exist
  5137. */
  5138. NGTCP2_EXTERN int ngtcp2_conn_set_stream_user_data(ngtcp2_conn *conn,
  5139. int64_t stream_id,
  5140. void *stream_user_data);
  5141. /**
  5142. * @function
  5143. *
  5144. * `ngtcp2_conn_update_pkt_tx_time` sets the time instant of the next
  5145. * packet transmission to pace packets. This function must be called
  5146. * after (multiple invocation of) `ngtcp2_conn_writev_stream`. If
  5147. * packet aggregation (e.g., packet batching, GSO) is used, call this
  5148. * function after all aggregated datagrams are sent, which indicates
  5149. * multiple invocation of `ngtcp2_conn_writev_stream`.
  5150. */
  5151. NGTCP2_EXTERN void ngtcp2_conn_update_pkt_tx_time(ngtcp2_conn *conn,
  5152. ngtcp2_tstamp ts);
  5153. /**
  5154. * @function
  5155. *
  5156. * `ngtcp2_conn_get_send_quantum` returns the maximum number of bytes
  5157. * that can be sent in one go without packet spacing.
  5158. */
  5159. NGTCP2_EXTERN size_t ngtcp2_conn_get_send_quantum(ngtcp2_conn *conn);
  5160. /**
  5161. * @function
  5162. *
  5163. * `ngtcp2_conn_get_stream_loss_count` returns the number of packets
  5164. * that contain STREAM frame for a stream identified by |stream_id|
  5165. * and are declared to be lost. The number may include the spurious
  5166. * losses. If no stream identified by |stream_id| is found, this
  5167. * function returns 0.
  5168. */
  5169. NGTCP2_EXTERN size_t ngtcp2_conn_get_stream_loss_count(ngtcp2_conn *conn,
  5170. int64_t stream_id);
  5171. /**
  5172. * @function
  5173. *
  5174. * `ngtcp2_strerror` returns the text representation of |liberr|.
  5175. * |liberr| must be one of ngtcp2 library error codes (which is
  5176. * defined as :macro:`NGTCP2_ERR_* <NGTCP2_ERR_INVALID_ARGUMENT>`
  5177. * macros).
  5178. */
  5179. NGTCP2_EXTERN const char *ngtcp2_strerror(int liberr);
  5180. /**
  5181. * @function
  5182. *
  5183. * `ngtcp2_err_is_fatal` returns nonzero if |liberr| is a fatal error.
  5184. * |liberr| must be one of ngtcp2 library error codes (which is
  5185. * defined as :macro:`NGTCP2_ERR_* <NGTCP2_ERR_INVALID_ARGUMENT>`
  5186. * macros).
  5187. */
  5188. NGTCP2_EXTERN int ngtcp2_err_is_fatal(int liberr);
  5189. /**
  5190. * @function
  5191. *
  5192. * `ngtcp2_err_infer_quic_transport_error_code` returns a QUIC
  5193. * transport error code which corresponds to |liberr|. |liberr| must
  5194. * be one of ngtcp2 library error codes (which is defined as
  5195. * :macro:`NGTCP2_ERR_* <NGTCP2_ERR_INVALID_ARGUMENT>` macros).
  5196. */
  5197. NGTCP2_EXTERN uint64_t ngtcp2_err_infer_quic_transport_error_code(int liberr);
  5198. /**
  5199. * @function
  5200. *
  5201. * `ngtcp2_addr_init` initializes |dest| with the given arguments and
  5202. * returns |dest|.
  5203. */
  5204. NGTCP2_EXTERN ngtcp2_addr *ngtcp2_addr_init(ngtcp2_addr *dest,
  5205. const ngtcp2_sockaddr *addr,
  5206. ngtcp2_socklen addrlen);
  5207. /**
  5208. * @function
  5209. *
  5210. * `ngtcp2_addr_copy_byte` copies |addr| of length |addrlen| into the
  5211. * buffer pointed by :member:`dest->addr <ngtcp2_addr.addr>`.
  5212. * :member:`dest->addrlen <ngtcp2_addr.addrlen>` is updated to have
  5213. * |addrlen|. This function assumes that :member:`dest->addr
  5214. * <ngtcp2_addr.addr>` points to a buffer which has a sufficient
  5215. * capacity to store the copy.
  5216. */
  5217. NGTCP2_EXTERN void ngtcp2_addr_copy_byte(ngtcp2_addr *dest,
  5218. const ngtcp2_sockaddr *addr,
  5219. ngtcp2_socklen addrlen);
  5220. /**
  5221. * @function
  5222. *
  5223. * `ngtcp2_path_storage_init` initializes |ps| with the given
  5224. * arguments. This function copies |local_addr| and |remote_addr|.
  5225. */
  5226. NGTCP2_EXTERN void ngtcp2_path_storage_init(ngtcp2_path_storage *ps,
  5227. const ngtcp2_sockaddr *local_addr,
  5228. ngtcp2_socklen local_addrlen,
  5229. const ngtcp2_sockaddr *remote_addr,
  5230. ngtcp2_socklen remote_addrlen,
  5231. void *user_data);
  5232. /**
  5233. * @function
  5234. *
  5235. * `ngtcp2_path_storage_zero` initializes |ps| with the zero length
  5236. * addresses.
  5237. */
  5238. NGTCP2_EXTERN void ngtcp2_path_storage_zero(ngtcp2_path_storage *ps);
  5239. /**
  5240. * @function
  5241. *
  5242. * `ngtcp2_settings_default` initializes |settings| with the default
  5243. * values. First this function fills |settings| with 0, and set the
  5244. * default value to the following fields:
  5245. *
  5246. * * :type:`cc_algo <ngtcp2_settings.cc_algo>` =
  5247. * :enum:`ngtcp2_cc_algo.NGTCP2_CC_ALGO_CUBIC`
  5248. * * :type:`initial_rtt <ngtcp2_settings.initial_rtt>` =
  5249. * :macro:`NGTCP2_DEFAULT_INITIAL_RTT`
  5250. * * :type:`ack_thresh <ngtcp2_settings.ack_thresh>` = 2
  5251. * * :type:`max_tx_udp_payload_size
  5252. * <ngtcp2_settings.max_tx_udp_payload_size>` = 1452
  5253. * * :type:`handshake_timeout <ngtcp2_settings.handshake_timeout>` =
  5254. * ``UINT64_MAX``
  5255. */
  5256. NGTCP2_EXTERN void ngtcp2_settings_default_versioned(int settings_version,
  5257. ngtcp2_settings *settings);
  5258. /**
  5259. * @function
  5260. *
  5261. * `ngtcp2_transport_params_default` initializes |params| with the
  5262. * default values. First this function fills |params| with 0, and set
  5263. * the default value to the following fields:
  5264. *
  5265. * * :type:`max_udp_payload_size
  5266. * <ngtcp2_transport_params.max_udp_payload_size>` =
  5267. * :macro:`NGTCP2_DEFAULT_MAX_RECV_UDP_PAYLOAD_SIZE`
  5268. * * :type:`ack_delay_exponent
  5269. * <ngtcp2_transport_params.ack_delay_exponent>` =
  5270. * :macro:`NGTCP2_DEFAULT_ACK_DELAY_EXPONENT`
  5271. * * :type:`max_ack_delay <ngtcp2_transport_params.max_ack_delay>` =
  5272. * :macro:`NGTCP2_DEFAULT_MAX_ACK_DELAY`
  5273. * * :type:`active_connection_id_limit
  5274. * <ngtcp2_transport_params.active_connection_id_limit>` =
  5275. * :macro:`NGTCP2_DEFAULT_ACTIVE_CONNECTION_ID_LIMIT`
  5276. */
  5277. NGTCP2_EXTERN void
  5278. ngtcp2_transport_params_default_versioned(int transport_params_version,
  5279. ngtcp2_transport_params *params);
  5280. /**
  5281. * @function
  5282. *
  5283. * `ngtcp2_mem_default` returns the default, system standard memory
  5284. * allocator.
  5285. */
  5286. NGTCP2_EXTERN const ngtcp2_mem *ngtcp2_mem_default(void);
  5287. /**
  5288. * @macrosection
  5289. *
  5290. * ngtcp2_info macros
  5291. */
  5292. /**
  5293. * @macro
  5294. *
  5295. * :macro:`NGTCP2_VERSION_AGE` is the age of :type:`ngtcp2_info`
  5296. */
  5297. #define NGTCP2_VERSION_AGE 1
  5298. /**
  5299. * @struct
  5300. *
  5301. * :type:`ngtcp2_info` is what `ngtcp2_version` returns. It holds
  5302. * information about the particular ngtcp2 version.
  5303. */
  5304. typedef struct ngtcp2_info {
  5305. /**
  5306. * :member:`age` is the age of this struct. This instance of ngtcp2
  5307. * sets it to :macro:`NGTCP2_VERSION_AGE` but a future version may
  5308. * bump it and add more struct fields at the bottom
  5309. */
  5310. int age;
  5311. /**
  5312. * :member:`version_num` is the :macro:`NGTCP2_VERSION_NUM` number
  5313. * (since :member:`age` ==1)
  5314. */
  5315. int version_num;
  5316. /**
  5317. * :member:`version_str` points to the :macro:`NGTCP2_VERSION`
  5318. * string (since :member:`age` ==1)
  5319. */
  5320. const char *version_str;
  5321. /* -------- the above fields all exist when age == 1 */
  5322. } ngtcp2_info;
  5323. /**
  5324. * @function
  5325. *
  5326. * `ngtcp2_version` returns a pointer to a :type:`ngtcp2_info` struct
  5327. * with version information about the run-time library in use. The
  5328. * |least_version| argument can be set to a 24 bit numerical value for
  5329. * the least accepted version number, and if the condition is not met,
  5330. * this function will return a ``NULL``. Pass in 0 to skip the
  5331. * version checking.
  5332. */
  5333. NGTCP2_EXTERN const ngtcp2_info *ngtcp2_version(int least_version);
  5334. /**
  5335. * @function
  5336. *
  5337. * `ngtcp2_is_bidi_stream` returns nonzero if |stream_id| denotes
  5338. * bidirectional stream.
  5339. */
  5340. NGTCP2_EXTERN int ngtcp2_is_bidi_stream(int64_t stream_id);
  5341. /**
  5342. * @function
  5343. *
  5344. * `ngtcp2_path_copy` copies |src| into |dest|. This function assumes
  5345. * that |dest| has enough buffer to store the deep copy of
  5346. * :member:`src->local <ngtcp2_path.local>` and :member:`src->remote
  5347. * <ngtcp2_path.remote>`.
  5348. */
  5349. NGTCP2_EXTERN void ngtcp2_path_copy(ngtcp2_path *dest, const ngtcp2_path *src);
  5350. /**
  5351. * @function
  5352. *
  5353. * `ngtcp2_path_eq` returns nonzero if |a| and |b| shares the same
  5354. * local and remote addresses.
  5355. */
  5356. NGTCP2_EXTERN int ngtcp2_path_eq(const ngtcp2_path *a, const ngtcp2_path *b);
  5357. /**
  5358. * @function
  5359. *
  5360. * `ngtcp2_is_supported_version` returns nonzero if the library
  5361. * supports QUIC version |version|.
  5362. */
  5363. NGTCP2_EXTERN int ngtcp2_is_supported_version(uint32_t version);
  5364. /**
  5365. * @function
  5366. *
  5367. * `ngtcp2_is_reserved_version` returns nonzero if |version| is a
  5368. * reserved version.
  5369. */
  5370. NGTCP2_EXTERN int ngtcp2_is_reserved_version(uint32_t version);
  5371. /**
  5372. * @function
  5373. *
  5374. * `ngtcp2_select_version` selects and returns a version from the
  5375. * version set |offered_versions| of |offered_versionslen| elements.
  5376. * |preferred_versions| of |preferred_versionslen| elements specifies
  5377. * the preference of versions, which is sorted in the order of
  5378. * preference. All versions included in |preferred_versions| must be
  5379. * supported by the library, that is, passing any version in the array
  5380. * to `ngtcp2_is_supported_version` must return nonzero. This
  5381. * function is intended to be used by client when it receives Version
  5382. * Negotiation packet. If no version is selected, this function
  5383. * returns 0.
  5384. */
  5385. NGTCP2_EXTERN uint32_t ngtcp2_select_version(const uint32_t *preferred_versions,
  5386. size_t preferred_versionslen,
  5387. const uint32_t *offered_versions,
  5388. size_t offered_versionslen);
  5389. /*
  5390. * Versioned function wrappers
  5391. */
  5392. /*
  5393. * `ngtcp2_conn_read_pkt` is a wrapper around
  5394. * `ngtcp2_conn_read_pkt_versioned` to set the correct struct version.
  5395. */
  5396. #define ngtcp2_conn_read_pkt(CONN, PATH, PI, PKT, PKTLEN, TS) \
  5397. ngtcp2_conn_read_pkt_versioned((CONN), (PATH), NGTCP2_PKT_INFO_VERSION, \
  5398. (PI), (PKT), (PKTLEN), (TS))
  5399. /*
  5400. * `ngtcp2_conn_write_pkt` is a wrapper around
  5401. * `ngtcp2_conn_write_pkt_versioned` to set the correct struct
  5402. * version.
  5403. */
  5404. #define ngtcp2_conn_write_pkt(CONN, PATH, PI, DEST, DESTLEN, TS) \
  5405. ngtcp2_conn_write_pkt_versioned((CONN), (PATH), NGTCP2_PKT_INFO_VERSION, \
  5406. (PI), (DEST), (DESTLEN), (TS))
  5407. /*
  5408. * `ngtcp2_conn_write_stream` is a wrapper around
  5409. * `ngtcp2_conn_write_stream_versioned` to set the correct struct
  5410. * version.
  5411. */
  5412. #define ngtcp2_conn_write_stream(CONN, PATH, PI, DEST, DESTLEN, PDATALEN, \
  5413. FLAGS, STREAM_ID, DATA, DATALEN, TS) \
  5414. ngtcp2_conn_write_stream_versioned( \
  5415. (CONN), (PATH), NGTCP2_PKT_INFO_VERSION, (PI), (DEST), (DESTLEN), \
  5416. (PDATALEN), (FLAGS), (STREAM_ID), (DATA), (DATALEN), (TS))
  5417. /*
  5418. * `ngtcp2_conn_writev_stream` is a wrapper around
  5419. * `ngtcp2_conn_writev_stream_versioned` to set the correct struct
  5420. * version.
  5421. */
  5422. #define ngtcp2_conn_writev_stream(CONN, PATH, PI, DEST, DESTLEN, PDATALEN, \
  5423. FLAGS, STREAM_ID, DATAV, DATAVCNT, TS) \
  5424. ngtcp2_conn_writev_stream_versioned( \
  5425. (CONN), (PATH), NGTCP2_PKT_INFO_VERSION, (PI), (DEST), (DESTLEN), \
  5426. (PDATALEN), (FLAGS), (STREAM_ID), (DATAV), (DATAVCNT), (TS))
  5427. /*
  5428. * `ngtcp2_conn_write_datagram` is a wrapper around
  5429. * `ngtcp2_conn_write_datagram_versioned` to set the correct struct
  5430. * version.
  5431. */
  5432. #define ngtcp2_conn_write_datagram(CONN, PATH, PI, DEST, DESTLEN, PACCEPTED, \
  5433. FLAGS, DGRAM_ID, DATA, DATALEN, TS) \
  5434. ngtcp2_conn_write_datagram_versioned( \
  5435. (CONN), (PATH), NGTCP2_PKT_INFO_VERSION, (PI), (DEST), (DESTLEN), \
  5436. (PACCEPTED), (FLAGS), (DGRAM_ID), (DATA), (DATALEN), (TS))
  5437. /*
  5438. * `ngtcp2_conn_writev_datagram` is a wrapper around
  5439. * `ngtcp2_conn_writev_datagram_versioned` to set the correct struct
  5440. * version.
  5441. */
  5442. #define ngtcp2_conn_writev_datagram(CONN, PATH, PI, DEST, DESTLEN, PACCEPTED, \
  5443. FLAGS, DGRAM_ID, DATAV, DATAVCNT, TS) \
  5444. ngtcp2_conn_writev_datagram_versioned( \
  5445. (CONN), (PATH), NGTCP2_PKT_INFO_VERSION, (PI), (DEST), (DESTLEN), \
  5446. (PACCEPTED), (FLAGS), (DGRAM_ID), (DATAV), (DATAVCNT), (TS))
  5447. /*
  5448. * `ngtcp2_conn_write_connection_close` is a wrapper around
  5449. * `ngtcp2_conn_write_connection_close_versioned` to set the correct
  5450. * struct version.
  5451. */
  5452. #define ngtcp2_conn_write_connection_close(CONN, PATH, PI, DEST, DESTLEN, \
  5453. CCERR, TS) \
  5454. ngtcp2_conn_write_connection_close_versioned( \
  5455. (CONN), (PATH), NGTCP2_PKT_INFO_VERSION, (PI), (DEST), (DESTLEN), \
  5456. (CCERR), (TS))
  5457. /*
  5458. * `ngtcp2_transport_params_encode` is a wrapper around
  5459. * `ngtcp2_transport_params_encode_versioned` to set the correct
  5460. * struct version.
  5461. */
  5462. #define ngtcp2_transport_params_encode(DEST, DESTLEN, PARAMS) \
  5463. ngtcp2_transport_params_encode_versioned( \
  5464. (DEST), (DESTLEN), NGTCP2_TRANSPORT_PARAMS_VERSION, (PARAMS))
  5465. /*
  5466. * `ngtcp2_transport_params_decode` is a wrapper around
  5467. * `ngtcp2_transport_params_decode_versioned` to set the correct
  5468. * struct version.
  5469. */
  5470. #define ngtcp2_transport_params_decode(PARAMS, DATA, DATALEN) \
  5471. ngtcp2_transport_params_decode_versioned(NGTCP2_TRANSPORT_PARAMS_VERSION, \
  5472. (PARAMS), (DATA), (DATALEN))
  5473. /*
  5474. * `ngtcp2_conn_client_new` is a wrapper around
  5475. * `ngtcp2_conn_client_new_versioned` to set the correct struct
  5476. * version.
  5477. */
  5478. #define ngtcp2_conn_client_new(PCONN, DCID, SCID, PATH, VERSION, CALLBACKS, \
  5479. SETTINGS, PARAMS, MEM, USER_DATA) \
  5480. ngtcp2_conn_client_new_versioned( \
  5481. (PCONN), (DCID), (SCID), (PATH), (VERSION), NGTCP2_CALLBACKS_VERSION, \
  5482. (CALLBACKS), NGTCP2_SETTINGS_VERSION, (SETTINGS), \
  5483. NGTCP2_TRANSPORT_PARAMS_VERSION, (PARAMS), (MEM), (USER_DATA))
  5484. /*
  5485. * `ngtcp2_conn_server_new` is a wrapper around
  5486. * `ngtcp2_conn_server_new_versioned` to set the correct struct
  5487. * version.
  5488. */
  5489. #define ngtcp2_conn_server_new(PCONN, DCID, SCID, PATH, VERSION, CALLBACKS, \
  5490. SETTINGS, PARAMS, MEM, USER_DATA) \
  5491. ngtcp2_conn_server_new_versioned( \
  5492. (PCONN), (DCID), (SCID), (PATH), (VERSION), NGTCP2_CALLBACKS_VERSION, \
  5493. (CALLBACKS), NGTCP2_SETTINGS_VERSION, (SETTINGS), \
  5494. NGTCP2_TRANSPORT_PARAMS_VERSION, (PARAMS), (MEM), (USER_DATA))
  5495. /*
  5496. * `ngtcp2_conn_set_local_transport_params` is a wrapper around
  5497. * `ngtcp2_conn_set_local_transport_params_versioned` to set the
  5498. * correct struct version.
  5499. */
  5500. #define ngtcp2_conn_set_local_transport_params(CONN, PARAMS) \
  5501. ngtcp2_conn_set_local_transport_params_versioned( \
  5502. (CONN), NGTCP2_TRANSPORT_PARAMS_VERSION, (PARAMS))
  5503. /*
  5504. * `ngtcp2_transport_params_default` is a wrapper around
  5505. * `ngtcp2_transport_params_default_versioned` to set the correct
  5506. * struct version.
  5507. */
  5508. #define ngtcp2_transport_params_default(PARAMS) \
  5509. ngtcp2_transport_params_default_versioned(NGTCP2_TRANSPORT_PARAMS_VERSION, \
  5510. (PARAMS))
  5511. /*
  5512. * `ngtcp2_conn_get_conn_info` is a wrapper around
  5513. * `ngtcp2_conn_get_conn_info_versioned` to set the correct struct
  5514. * version.
  5515. */
  5516. #define ngtcp2_conn_get_conn_info(CONN, CINFO) \
  5517. ngtcp2_conn_get_conn_info_versioned((CONN), NGTCP2_CONN_INFO_VERSION, (CINFO))
  5518. /*
  5519. * `ngtcp2_settings_default` is a wrapper around
  5520. * `ngtcp2_settings_default_versioned` to set the correct struct
  5521. * version.
  5522. */
  5523. #define ngtcp2_settings_default(SETTINGS) \
  5524. ngtcp2_settings_default_versioned(NGTCP2_SETTINGS_VERSION, (SETTINGS))
  5525. #ifdef _MSC_VER
  5526. # pragma warning(pop)
  5527. #endif
  5528. #ifdef __cplusplus
  5529. }
  5530. #endif
  5531. #endif /* NGTCP2_H */