ngtcp2.h 195 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914
  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_V2 2
  1574. #define NGTCP2_SETTINGS_VERSION NGTCP2_SETTINGS_V2
  1575. /**
  1576. * @struct
  1577. *
  1578. * :type:`ngtcp2_settings` defines QUIC connection settings.
  1579. */
  1580. typedef struct ngtcp2_settings {
  1581. /**
  1582. * :member:`qlog_write` is a callback function to write qlog.
  1583. * Setting ``NULL`` disables qlog.
  1584. */
  1585. ngtcp2_qlog_write qlog_write;
  1586. /**
  1587. * :member:`cc_algo` specifies congestion control algorithm.
  1588. */
  1589. ngtcp2_cc_algo cc_algo;
  1590. /**
  1591. * :member:`initial_ts` is an initial timestamp given to the
  1592. * library.
  1593. */
  1594. ngtcp2_tstamp initial_ts;
  1595. /**
  1596. * :member:`initial_rtt` is an initial RTT.
  1597. */
  1598. ngtcp2_duration initial_rtt;
  1599. /**
  1600. * :member:`log_printf` is a function that the library uses to write
  1601. * logs. ``NULL`` means no logging output. It is nothing to do
  1602. * with qlog.
  1603. */
  1604. ngtcp2_printf log_printf;
  1605. /**
  1606. * :member:`max_tx_udp_payload_size` is the maximum size of UDP
  1607. * datagram payload that the local endpoint transmits.
  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. /* The following fields have been added since NGTCP2_SETTINGS_V2. */
  1761. /**
  1762. * :member:`pmtud_probes` is the array of UDP datagram payload size
  1763. * to probe during Path MTU Discovery. The discovery is done in the
  1764. * order appeared in this array. The size must be strictly larger
  1765. * than 1200, otherwise the behavior is undefined. The maximum
  1766. * value in this array should be set to
  1767. * :member:`max_tx_udp_payload_size`. If this field is not set, the
  1768. * predefined PMTUD probes are made. This field has been available
  1769. * since v1.4.0.
  1770. */
  1771. const uint16_t *pmtud_probes;
  1772. /**
  1773. * :member:`pmtud_probeslen` is the number of elements that are
  1774. * contained in the array pointed by :member:`pmtud_probes`. This
  1775. * field has been available since v1.4.0.
  1776. */
  1777. size_t pmtud_probeslen;
  1778. } ngtcp2_settings;
  1779. /**
  1780. * @struct
  1781. *
  1782. * :type:`ngtcp2_addr` is the endpoint address.
  1783. */
  1784. typedef struct ngtcp2_addr {
  1785. /**
  1786. * :member:`addr` points to the buffer which contains endpoint
  1787. * address. It must not be ``NULL``.
  1788. */
  1789. ngtcp2_sockaddr *addr;
  1790. /**
  1791. * :member:`addrlen` is the length of :member:`addr`. It must not
  1792. * be longer than sizeof(:type:`ngtcp2_sockaddr_union`).
  1793. */
  1794. ngtcp2_socklen addrlen;
  1795. } ngtcp2_addr;
  1796. /**
  1797. * @struct
  1798. *
  1799. * :type:`ngtcp2_path` is the network endpoints where a packet is sent
  1800. * and received.
  1801. */
  1802. typedef struct ngtcp2_path {
  1803. /**
  1804. * :member:`local` is the address of local endpoint.
  1805. */
  1806. ngtcp2_addr local;
  1807. /**
  1808. * :member:`remote` is the address of remote endpoint.
  1809. */
  1810. ngtcp2_addr remote;
  1811. /**
  1812. * :member:`user_data` is an arbitrary data and opaque to the
  1813. * library.
  1814. *
  1815. * Note that :type:`ngtcp2_path` is generally passed to
  1816. * :type:`ngtcp2_conn` by an application, and :type:`ngtcp2_conn`
  1817. * stores their copies. Unfortunately, there is no way for the
  1818. * application to know when :type:`ngtcp2_conn` finished using a
  1819. * specific :type:`ngtcp2_path` object in mid connection, which
  1820. * means that the application cannot free the data pointed by this
  1821. * field. Therefore, it is advised to use this field only when the
  1822. * data pointed by this field persists in an entire lifetime of the
  1823. * connection.
  1824. */
  1825. void *user_data;
  1826. } ngtcp2_path;
  1827. /**
  1828. * @struct
  1829. *
  1830. * :type:`ngtcp2_path_storage` is a convenient struct to have buffers
  1831. * to store the longest addresses.
  1832. */
  1833. typedef struct ngtcp2_path_storage {
  1834. /**
  1835. * :member:`path` stores network path.
  1836. */
  1837. ngtcp2_path path;
  1838. /**
  1839. * :member:`local_addrbuf` is a buffer to store local address.
  1840. */
  1841. ngtcp2_sockaddr_union local_addrbuf;
  1842. /**
  1843. * :member:`remote_addrbuf` is a buffer to store remote address.
  1844. */
  1845. ngtcp2_sockaddr_union remote_addrbuf;
  1846. } ngtcp2_path_storage;
  1847. /**
  1848. * @struct
  1849. *
  1850. * :type:`ngtcp2_crypto_md` is a wrapper around native message digest
  1851. * object.
  1852. */
  1853. typedef struct ngtcp2_crypto_md {
  1854. /**
  1855. * :member:`native_handle` is a pointer to an underlying message
  1856. * digest object.
  1857. */
  1858. void *native_handle;
  1859. } ngtcp2_crypto_md;
  1860. /**
  1861. * @struct
  1862. *
  1863. * :type:`ngtcp2_crypto_aead` is a wrapper around native AEAD object.
  1864. */
  1865. typedef struct ngtcp2_crypto_aead {
  1866. /**
  1867. * :member:`native_handle` is a pointer to an underlying AEAD
  1868. * object.
  1869. */
  1870. void *native_handle;
  1871. /**
  1872. * :member:`max_overhead` is the number of additional bytes which
  1873. * AEAD encryption needs on encryption.
  1874. */
  1875. size_t max_overhead;
  1876. } ngtcp2_crypto_aead;
  1877. /**
  1878. * @struct
  1879. *
  1880. * :type:`ngtcp2_crypto_cipher` is a wrapper around native cipher
  1881. * object.
  1882. */
  1883. typedef struct ngtcp2_crypto_cipher {
  1884. /**
  1885. * :member:`native_handle` is a pointer to an underlying cipher
  1886. * object.
  1887. */
  1888. void *native_handle;
  1889. } ngtcp2_crypto_cipher;
  1890. /**
  1891. * @struct
  1892. *
  1893. * :type:`ngtcp2_crypto_aead_ctx` is a wrapper around native AEAD
  1894. * cipher context object. It should be initialized with a specific
  1895. * key. ngtcp2 library reuses this context object to encrypt or
  1896. * decrypt multiple packets.
  1897. */
  1898. typedef struct ngtcp2_crypto_aead_ctx {
  1899. /**
  1900. * :member:`native_handle` is a pointer to an underlying AEAD
  1901. * context object.
  1902. */
  1903. void *native_handle;
  1904. } ngtcp2_crypto_aead_ctx;
  1905. /**
  1906. * @struct
  1907. *
  1908. * :type:`ngtcp2_crypto_cipher_ctx` is a wrapper around native cipher
  1909. * context object. It should be initialized with a specific key.
  1910. * ngtcp2 library reuses this context object to encrypt or decrypt
  1911. * multiple packet headers.
  1912. */
  1913. typedef struct ngtcp2_crypto_cipher_ctx {
  1914. /**
  1915. * :member:`native_handle` is a pointer to an underlying cipher
  1916. * context object.
  1917. */
  1918. void *native_handle;
  1919. } ngtcp2_crypto_cipher_ctx;
  1920. /**
  1921. * @struct
  1922. *
  1923. * :type:`ngtcp2_crypto_ctx` is a convenient structure to bind all
  1924. * crypto related objects in one place. Use
  1925. * `ngtcp2_crypto_ctx_initial` to initialize this struct for Initial
  1926. * packet encryption. For Handshake and 1-RTT packets, use
  1927. * `ngtcp2_crypto_ctx_tls`. For 0-RTT packets, use
  1928. * `ngtcp2_crypto_ctx_tls_early`.
  1929. */
  1930. typedef struct ngtcp2_crypto_ctx {
  1931. /**
  1932. * :member:`aead` is AEAD object.
  1933. */
  1934. ngtcp2_crypto_aead aead;
  1935. /**
  1936. * :member:`md` is message digest object.
  1937. */
  1938. ngtcp2_crypto_md md;
  1939. /**
  1940. * :member:`hp` is header protection cipher.
  1941. */
  1942. ngtcp2_crypto_cipher hp;
  1943. /**
  1944. * :member:`max_encryption` is the number of encryption which this
  1945. * key can be used with.
  1946. */
  1947. uint64_t max_encryption;
  1948. /**
  1949. * :member:`max_decryption_failure` is the number of decryption
  1950. * failure with this key.
  1951. */
  1952. uint64_t max_decryption_failure;
  1953. } ngtcp2_crypto_ctx;
  1954. /**
  1955. * @function
  1956. *
  1957. * `ngtcp2_transport_params_encode` encodes |params| in |dest| of
  1958. * length |destlen|.
  1959. *
  1960. * If |dest| is NULL, and |destlen| is zero, this function just
  1961. * returns the number of bytes required to store the encoded transport
  1962. * parameters.
  1963. *
  1964. * This function returns the number of bytes written, or one of the
  1965. * following negative error codes:
  1966. *
  1967. * :macro:`NGTCP2_ERR_NOBUF`
  1968. * Buffer is too small.
  1969. */
  1970. NGTCP2_EXTERN ngtcp2_ssize ngtcp2_transport_params_encode_versioned(
  1971. uint8_t *dest, size_t destlen, int transport_params_version,
  1972. const ngtcp2_transport_params *params);
  1973. /**
  1974. * @function
  1975. *
  1976. * `ngtcp2_transport_params_decode` decodes transport parameters in
  1977. * |data| of length |datalen|, and stores the result in the object
  1978. * pointed by |params|.
  1979. *
  1980. * If an optional parameter is missing, the default value is assigned.
  1981. *
  1982. * The following fields may point to somewhere inside the buffer
  1983. * pointed by |data| of length |datalen|:
  1984. *
  1985. * - :member:`ngtcp2_transport_params.version_info.available_versions
  1986. * <ngtcp2_version_info.available_versions>`
  1987. *
  1988. * This function returns 0 if it succeeds, or one of the following
  1989. * negative error codes:
  1990. *
  1991. * :macro:`NGTCP2_ERR_MALFORMED_TRANSPORT_PARAM`
  1992. * The input is malformed.
  1993. */
  1994. NGTCP2_EXTERN int
  1995. ngtcp2_transport_params_decode_versioned(int transport_params_version,
  1996. ngtcp2_transport_params *params,
  1997. const uint8_t *data, size_t datalen);
  1998. /**
  1999. * @function
  2000. *
  2001. * `ngtcp2_transport_params_decode_new` decodes transport parameters
  2002. * in |data| of length |datalen|, and stores the result in the object
  2003. * allocated dynamically. The pointer to the allocated object is
  2004. * assigned to |*pparams|. Unlike `ngtcp2_transport_params_decode`,
  2005. * all direct and indirect fields are also allocated dynamically if
  2006. * needed.
  2007. *
  2008. * |mem| is a memory allocator to allocate memory. If |mem| is
  2009. * ``NULL``, the memory allocator returned by `ngtcp2_mem_default()`
  2010. * is used.
  2011. *
  2012. * If the optional parameters are missing, the default value is
  2013. * assigned.
  2014. *
  2015. * `ngtcp2_transport_params_del` frees the memory allocated by this
  2016. * function.
  2017. *
  2018. * This function returns 0 if it succeeds, or one of the following
  2019. * negative error codes:
  2020. *
  2021. * :macro:`NGTCP2_ERR_MALFORMED_TRANSPORT_PARAM`
  2022. * The input is malformed.
  2023. * :macro:`NGTCP2_ERR_NOMEM`
  2024. * Out of memory.
  2025. */
  2026. NGTCP2_EXTERN int
  2027. ngtcp2_transport_params_decode_new(ngtcp2_transport_params **pparams,
  2028. const uint8_t *data, size_t datalen,
  2029. const ngtcp2_mem *mem);
  2030. /**
  2031. * @function
  2032. *
  2033. * `ngtcp2_transport_params_del` frees the |params| which must be
  2034. * dynamically allocated by `ngtcp2_transport_params_decode_new`.
  2035. *
  2036. * |mem| is a memory allocator that allocated |params|. If |mem| is
  2037. * ``NULL``, the memory allocator returned by `ngtcp2_mem_default()`
  2038. * is used.
  2039. *
  2040. * If |params| is ``NULL``, this function does nothing.
  2041. */
  2042. NGTCP2_EXTERN void ngtcp2_transport_params_del(ngtcp2_transport_params *params,
  2043. const ngtcp2_mem *mem);
  2044. /**
  2045. * @struct
  2046. *
  2047. * :type:`ngtcp2_version_cid` is a convenient struct to store the
  2048. * result of `ngtcp2_pkt_decode_version_cid`.
  2049. */
  2050. typedef struct ngtcp2_version_cid {
  2051. /**
  2052. * :member:`version` stores QUIC version.
  2053. */
  2054. uint32_t version;
  2055. /**
  2056. * :member:`dcid` points to the Destination Connection ID.
  2057. */
  2058. const uint8_t *dcid;
  2059. /**
  2060. * :member:`dcidlen` is the length of the Destination Connection ID
  2061. * pointed by :member:`dcid`.
  2062. */
  2063. size_t dcidlen;
  2064. /**
  2065. * :member:`scid` points to the Source Connection ID.
  2066. */
  2067. const uint8_t *scid;
  2068. /**
  2069. * :member:`scidlen` is the length of the Source Connection ID
  2070. * pointed by :member:`scid`.
  2071. */
  2072. size_t scidlen;
  2073. } ngtcp2_version_cid;
  2074. /**
  2075. * @function
  2076. *
  2077. * `ngtcp2_pkt_decode_version_cid` extracts QUIC version, Destination
  2078. * Connection ID and Source Connection ID from the packet pointed by
  2079. * |data| of length |datalen|. This function can handle Connection ID
  2080. * up to 255 bytes unlike `ngtcp2_pkt_decode_hd_long` or
  2081. * `ngtcp2_pkt_decode_hd_short` which are only capable of handling
  2082. * Connection ID less than or equal to :macro:`NGTCP2_MAX_CIDLEN`.
  2083. * Longer Connection ID is only valid if the version is unsupported
  2084. * QUIC version.
  2085. *
  2086. * If the given packet is Long header packet, this function extracts
  2087. * the version from the packet, and assigns it to
  2088. * :member:`dest->version <ngtcp2_version_cid.version>`. It also
  2089. * extracts the pointer to the Destination Connection ID and its
  2090. * length, and assigns them to :member:`dest->dcid
  2091. * <ngtcp2_version_cid.dcid>` and :member:`dest->dcidlen
  2092. * <ngtcp2_version_cid.dcidlen>` respectively. Similarly, it extracts
  2093. * the pointer to the Source Connection ID and its length, and assigns
  2094. * them to :member:`dest->scid <ngtcp2_version_cid.scid>` and
  2095. * :member:`dest->scidlen <ngtcp2_version_cid.scidlen>` respectively.
  2096. * |short_dcidlen| is ignored.
  2097. *
  2098. * If the given packet is Short header packet, :member:`dest->version
  2099. * <ngtcp2_version_cid.version>` will be 0, :member:`dest->scid
  2100. * <ngtcp2_version_cid.scid>` will be ``NULL``, and
  2101. * :member:`dest->scidlen <ngtcp2_version_cid.scidlen>` will be 0.
  2102. * Because the Short header packet does not have the length of
  2103. * Destination Connection ID, the caller has to pass the length in
  2104. * |short_dcidlen|. This function extracts the pointer to the
  2105. * Destination Connection ID, and assigns it to :member:`dest->dcid
  2106. * <ngtcp2_version_cid.dcid>`. |short_dcidlen| is assigned to
  2107. * :member:`dest->dcidlen <ngtcp2_version_cid.dcidlen>`.
  2108. *
  2109. * If Version Negotiation is required, this function returns
  2110. * :macro:`NGTCP2_ERR_VERSION_NEGOTIATION`. Unlike the other error
  2111. * cases, all fields of |dest| are assigned as described above.
  2112. *
  2113. * This function returns 0 if it succeeds. Otherwise, one of the
  2114. * following negative error code:
  2115. *
  2116. * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
  2117. * The function could not decode the packet header.
  2118. * :macro:`NGTCP2_ERR_VERSION_NEGOTIATION`
  2119. * Version Negotiation packet should be sent.
  2120. */
  2121. NGTCP2_EXTERN int ngtcp2_pkt_decode_version_cid(ngtcp2_version_cid *dest,
  2122. const uint8_t *data,
  2123. size_t datalen,
  2124. size_t short_dcidlen);
  2125. /**
  2126. * @function
  2127. *
  2128. * `ngtcp2_pkt_decode_hd_long` decodes QUIC long packet header in
  2129. * |pkt| of length |pktlen|. This function only parses the input just
  2130. * before packet number field.
  2131. *
  2132. * This function does not verify that length field is correct. In
  2133. * other words, this function succeeds even if length > |pktlen|.
  2134. *
  2135. * This function can handle Connection ID up to
  2136. * :macro:`NGTCP2_MAX_CIDLEN`. Consider to use
  2137. * `ngtcp2_pkt_decode_version_cid` to get longer Connection ID.
  2138. *
  2139. * This function handles Version Negotiation specially. If version
  2140. * field is 0, |pkt| must contain Version Negotiation packet. Version
  2141. * Negotiation packet has random type in wire format. For
  2142. * convenience, this function sets
  2143. * :enum:`ngtcp2_pkt_type.NGTCP2_PKT_VERSION_NEGOTIATION` to
  2144. * :member:`dest->type <ngtcp2_pkt_hd.type>`, clears
  2145. * :macro:`NGTCP2_PKT_FLAG_LONG_FORM` flag from :member:`dest->flags
  2146. * <ngtcp2_pkt_hd.flags>`, and sets 0 to :member:`dest->len
  2147. * <ngtcp2_pkt_hd.len>`. Version Negotiation packet occupies a single
  2148. * packet.
  2149. *
  2150. * It stores the result in the object pointed by |dest|, and returns
  2151. * the number of bytes decoded to read the packet header if it
  2152. * succeeds, or one of the following error codes:
  2153. *
  2154. * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
  2155. * Packet is too short; or it is not a long header
  2156. */
  2157. NGTCP2_EXTERN ngtcp2_ssize ngtcp2_pkt_decode_hd_long(ngtcp2_pkt_hd *dest,
  2158. const uint8_t *pkt,
  2159. size_t pktlen);
  2160. /**
  2161. * @function
  2162. *
  2163. * `ngtcp2_pkt_decode_hd_short` decodes QUIC short header in |pkt| of
  2164. * length |pktlen|. Short header packet does not encode the length of
  2165. * Connection ID, thus we need the input from the outside. |dcidlen|
  2166. * is the length of Destination Connection ID in packet header. This
  2167. * function only parses the input just before packet number field.
  2168. * This function can handle Connection ID up to
  2169. * :macro:`NGTCP2_MAX_CIDLEN`. Consider to use
  2170. * `ngtcp2_pkt_decode_version_cid` to get longer Connection ID. It
  2171. * stores the result in the object pointed by |dest|, and returns the
  2172. * number of bytes decoded to read the packet header if it succeeds,
  2173. * or one of the following error codes:
  2174. *
  2175. * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
  2176. * Packet is too short; or it is not a short header
  2177. */
  2178. NGTCP2_EXTERN ngtcp2_ssize ngtcp2_pkt_decode_hd_short(ngtcp2_pkt_hd *dest,
  2179. const uint8_t *pkt,
  2180. size_t pktlen,
  2181. size_t dcidlen);
  2182. /**
  2183. * @function
  2184. *
  2185. * `ngtcp2_pkt_write_stateless_reset` writes Stateless Reset packet in
  2186. * the buffer pointed by |dest| whose length is |destlen|.
  2187. * |stateless_reset_token| is a pointer to the Stateless Reset Token,
  2188. * and its length must be :macro:`NGTCP2_STATELESS_RESET_TOKENLEN`
  2189. * bytes long. |rand| specifies the random octets preceding Stateless
  2190. * Reset Token. The length of |rand| is specified by |randlen| which
  2191. * must be at least :macro:`NGTCP2_MIN_STATELESS_RESET_RANDLEN` bytes
  2192. * long.
  2193. *
  2194. * If |randlen| is too long to write them all in the buffer, |rand| is
  2195. * written to the buffer as much as possible, and is truncated.
  2196. *
  2197. * This function returns the number of bytes written to the buffer, or
  2198. * one of the following negative error codes:
  2199. *
  2200. * :macro:`NGTCP2_ERR_NOBUF`
  2201. * Buffer is too small.
  2202. * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
  2203. * |randlen| is strictly less than
  2204. * :macro:`NGTCP2_MIN_STATELESS_RESET_RANDLEN`.
  2205. */
  2206. NGTCP2_EXTERN ngtcp2_ssize ngtcp2_pkt_write_stateless_reset(
  2207. uint8_t *dest, size_t destlen, const uint8_t *stateless_reset_token,
  2208. const uint8_t *rand, size_t randlen);
  2209. /**
  2210. * @function
  2211. *
  2212. * `ngtcp2_pkt_write_version_negotiation` writes Version Negotiation
  2213. * packet in the buffer pointed by |dest| whose length is |destlen|.
  2214. * |unused_random| should be generated randomly. |dcid| is a
  2215. * Connection ID which appeared in a packet as a Source Connection ID
  2216. * sent by client which caused version negotiation. Similarly, |scid|
  2217. * is a Connection ID which appeared in a packet as a Destination
  2218. * Connection ID sent by client. |sv| is a list of supported
  2219. * versions, and |nsv| specifies the number of supported versions
  2220. * included in |sv|.
  2221. *
  2222. * This function returns the number of bytes written to the buffer, or
  2223. * one of the following negative error codes:
  2224. *
  2225. * :macro:`NGTCP2_ERR_NOBUF`
  2226. * Buffer is too small.
  2227. */
  2228. NGTCP2_EXTERN ngtcp2_ssize ngtcp2_pkt_write_version_negotiation(
  2229. uint8_t *dest, size_t destlen, uint8_t unused_random, const uint8_t *dcid,
  2230. size_t dcidlen, const uint8_t *scid, size_t scidlen, const uint32_t *sv,
  2231. size_t nsv);
  2232. /**
  2233. * @struct
  2234. *
  2235. * :type:`ngtcp2_conn` represents a single QUIC connection.
  2236. */
  2237. typedef struct ngtcp2_conn ngtcp2_conn;
  2238. /**
  2239. * @functypedef
  2240. *
  2241. * :type:`ngtcp2_client_initial` is invoked when client application
  2242. * asks TLS stack to produce first TLS cryptographic handshake data.
  2243. *
  2244. * This implementation of this callback must get the first handshake
  2245. * data from TLS stack, and pass it to ngtcp2 library using
  2246. * `ngtcp2_conn_submit_crypto_data` function. Make sure that before
  2247. * calling `ngtcp2_conn_submit_crypto_data` function, client
  2248. * application must create initial packet protection keys and IVs, and
  2249. * provide them to ngtcp2 library using
  2250. * `ngtcp2_conn_install_initial_key`.
  2251. *
  2252. * This callback function must return 0 if it succeeds, or
  2253. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` which makes the library call
  2254. * return immediately.
  2255. */
  2256. typedef int (*ngtcp2_client_initial)(ngtcp2_conn *conn, void *user_data);
  2257. /**
  2258. * @functypedef
  2259. *
  2260. * :type:`ngtcp2_recv_client_initial` is invoked when server receives
  2261. * Initial packet from client. An server application must implement
  2262. * this callback, and generate initial keys and IVs for both
  2263. * transmission and reception. Install them using
  2264. * `ngtcp2_conn_install_initial_key`. |dcid| is the Destination
  2265. * Connection ID in Initial packet received from client. It is used
  2266. * to derive initial packet protection keys.
  2267. *
  2268. * The callback function must return 0 if it succeeds. If an error
  2269. * occurs, return :macro:`NGTCP2_ERR_CALLBACK_FAILURE` which makes the
  2270. * library call return immediately.
  2271. */
  2272. typedef int (*ngtcp2_recv_client_initial)(ngtcp2_conn *conn,
  2273. const ngtcp2_cid *dcid,
  2274. void *user_data);
  2275. /**
  2276. * @enum
  2277. *
  2278. * :type:`ngtcp2_encryption_level` is QUIC encryption level.
  2279. */
  2280. typedef enum ngtcp2_encryption_level {
  2281. /**
  2282. * :enum:`NGTCP2_ENCRYPTION_LEVEL_INITIAL` is Initial encryption
  2283. * level.
  2284. */
  2285. NGTCP2_ENCRYPTION_LEVEL_INITIAL,
  2286. /**
  2287. * :enum:`NGTCP2_ENCRYPTION_LEVEL_HANDSHAKE` is Handshake encryption
  2288. * level.
  2289. */
  2290. NGTCP2_ENCRYPTION_LEVEL_HANDSHAKE,
  2291. /**
  2292. * :enum:`NGTCP2_ENCRYPTION_LEVEL_1RTT` is 1-RTT encryption level.
  2293. */
  2294. NGTCP2_ENCRYPTION_LEVEL_1RTT,
  2295. /**
  2296. * :enum:`NGTCP2_ENCRYPTION_LEVEL_0RTT` is 0-RTT encryption level.
  2297. */
  2298. NGTCP2_ENCRYPTION_LEVEL_0RTT
  2299. } ngtcp2_encryption_level;
  2300. /**
  2301. * @functypedef
  2302. *
  2303. * :type`ngtcp2_recv_crypto_data` is invoked when crypto data is
  2304. * received. The received data is pointed by |data|, and its length
  2305. * is |datalen|. The |offset| specifies the offset where |data| is
  2306. * positioned. |user_data| is the arbitrary pointer passed to
  2307. * `ngtcp2_conn_client_new` or `ngtcp2_conn_server_new`. The ngtcp2
  2308. * library ensures that the crypto data is passed to the application
  2309. * in the increasing order of |offset|. |datalen| is always strictly
  2310. * greater than 0. |encryption_level| indicates the encryption level
  2311. * where this data is received. Crypto data can never be received in
  2312. * :enum:`ngtcp2_encryption_level.NGTCP2_ENCRYPTION_LEVEL_0RTT`.
  2313. *
  2314. * The application should provide the given data to TLS stack.
  2315. *
  2316. * The callback function must return 0 if it succeeds, or one of the
  2317. * following negative error codes:
  2318. *
  2319. * - :macro:`NGTCP2_ERR_CRYPTO`
  2320. * - :macro:`NGTCP2_ERR_REQUIRED_TRANSPORT_PARAM`
  2321. * - :macro:`NGTCP2_ERR_MALFORMED_TRANSPORT_PARAM`
  2322. * - :macro:`NGTCP2_ERR_TRANSPORT_PARAM`
  2323. * - :macro:`NGTCP2_ERR_PROTO`
  2324. * - :macro:`NGTCP2_ERR_VERSION_NEGOTIATION_FAILURE`
  2325. * - :macro:`NGTCP2_ERR_NOMEM`
  2326. * - :macro:`NGTCP2_ERR_CALLBACK_FAILURE`
  2327. *
  2328. * If the other value is returned, it is treated as
  2329. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE`.
  2330. *
  2331. * If application encounters fatal error, return
  2332. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` which makes the library call
  2333. * return immediately.
  2334. */
  2335. typedef int (*ngtcp2_recv_crypto_data)(ngtcp2_conn *conn,
  2336. ngtcp2_encryption_level encryption_level,
  2337. uint64_t offset, const uint8_t *data,
  2338. size_t datalen, void *user_data);
  2339. /**
  2340. * @functypedef
  2341. *
  2342. * :type:`ngtcp2_handshake_completed` is invoked when QUIC
  2343. * cryptographic handshake has completed.
  2344. *
  2345. * The callback function must return 0 if it succeeds. Returning
  2346. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
  2347. * immediately.
  2348. */
  2349. typedef int (*ngtcp2_handshake_completed)(ngtcp2_conn *conn, void *user_data);
  2350. /**
  2351. * @functypedef
  2352. *
  2353. * :type:`ngtcp2_handshake_confirmed` is invoked when QUIC
  2354. * cryptographic handshake is confirmed. The handshake confirmation
  2355. * means that both endpoints agree that handshake has finished.
  2356. *
  2357. * The callback function must return 0 if it succeeds. Returning
  2358. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
  2359. * immediately.
  2360. */
  2361. typedef int (*ngtcp2_handshake_confirmed)(ngtcp2_conn *conn, void *user_data);
  2362. /**
  2363. * @functypedef
  2364. *
  2365. * :type:`ngtcp2_recv_version_negotiation` is invoked when Version
  2366. * Negotiation packet is received. |hd| is the pointer to the QUIC
  2367. * packet header object. The vector |sv| of |nsv| elements contains
  2368. * the QUIC version the server supports. Since Version Negotiation is
  2369. * only sent by server, this callback function is used by client only.
  2370. *
  2371. * The callback function must return 0 if it succeeds, or
  2372. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` which makes the library call
  2373. * return immediately.
  2374. */
  2375. typedef int (*ngtcp2_recv_version_negotiation)(ngtcp2_conn *conn,
  2376. const ngtcp2_pkt_hd *hd,
  2377. const uint32_t *sv, size_t nsv,
  2378. void *user_data);
  2379. /**
  2380. * @functypedef
  2381. *
  2382. * :type:`ngtcp2_recv_retry` is invoked when Retry packet is received.
  2383. * This callback is client use only.
  2384. *
  2385. * Application must regenerate packet protection key, IV, and header
  2386. * protection key for Initial packets using the Destination Connection
  2387. * ID obtained by :member:`hd->scid <ngtcp2_pkt_hd.scid>`, and install
  2388. * them by calling `ngtcp2_conn_install_initial_key`.
  2389. *
  2390. * 0-RTT data accepted by the ngtcp2 library will be automatically
  2391. * retransmitted as 0-RTT data by the library.
  2392. *
  2393. * The callback function must return 0 if it succeeds. Returning
  2394. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
  2395. * immediately.
  2396. */
  2397. typedef int (*ngtcp2_recv_retry)(ngtcp2_conn *conn, const ngtcp2_pkt_hd *hd,
  2398. void *user_data);
  2399. /**
  2400. * @functypedef
  2401. *
  2402. * :type:`ngtcp2_encrypt` is invoked when the ngtcp2 library asks the
  2403. * application to encrypt packet payload. The packet payload to
  2404. * encrypt is passed as |plaintext| of length |plaintextlen|. The
  2405. * AEAD cipher is |aead|. |aead_ctx| is the AEAD cipher context
  2406. * object which is initialized with the specific encryption key. The
  2407. * nonce is passed as |nonce| of length |noncelen|. The Additional
  2408. * Authenticated Data is passed as |aad| of length |aadlen|.
  2409. *
  2410. * The implementation of this callback must encrypt |plaintext| using
  2411. * the negotiated cipher suite, and write the ciphertext into the
  2412. * buffer pointed by |dest|. |dest| has enough capacity to store the
  2413. * ciphertext and any additional AEAD tag data.
  2414. *
  2415. * |dest| and |plaintext| may point to the same buffer.
  2416. *
  2417. * The callback function must return 0 if it succeeds, or
  2418. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` which makes the library call
  2419. * return immediately.
  2420. */
  2421. typedef int (*ngtcp2_encrypt)(uint8_t *dest, const ngtcp2_crypto_aead *aead,
  2422. const ngtcp2_crypto_aead_ctx *aead_ctx,
  2423. const uint8_t *plaintext, size_t plaintextlen,
  2424. const uint8_t *nonce, size_t noncelen,
  2425. const uint8_t *aad, size_t aadlen);
  2426. /**
  2427. * @functypedef
  2428. *
  2429. * :type:`ngtcp2_decrypt` is invoked when the ngtcp2 library asks the
  2430. * application to decrypt packet payload. The packet payload to
  2431. * decrypt is passed as |ciphertext| of length |ciphertextlen|. The
  2432. * AEAD cipher is |aead|. |aead_ctx| is the AEAD cipher context
  2433. * object which is initialized with the specific decryption key. The
  2434. * nonce is passed as |nonce| of length |noncelen|. The Additional
  2435. * Authenticated Data is passed as |aad| of length |aadlen|.
  2436. *
  2437. * The implementation of this callback must decrypt |ciphertext| using
  2438. * the negotiated cipher suite, and write the ciphertext into the
  2439. * buffer pointed by |dest|. |dest| has enough capacity to store the
  2440. * cleartext.
  2441. *
  2442. * |dest| and |ciphertext| may point to the same buffer.
  2443. *
  2444. * The callback function must return 0 if it succeeds. If TLS stack
  2445. * fails to decrypt data, return :macro:`NGTCP2_ERR_DECRYPT`. For any
  2446. * other errors, return :macro:`NGTCP2_ERR_CALLBACK_FAILURE` which
  2447. * makes the library call return immediately.
  2448. */
  2449. typedef int (*ngtcp2_decrypt)(uint8_t *dest, const ngtcp2_crypto_aead *aead,
  2450. const ngtcp2_crypto_aead_ctx *aead_ctx,
  2451. const uint8_t *ciphertext, size_t ciphertextlen,
  2452. const uint8_t *nonce, size_t noncelen,
  2453. const uint8_t *aad, size_t aadlen);
  2454. /**
  2455. * @functypedef
  2456. *
  2457. * :type:`ngtcp2_hp_mask` is invoked when the ngtcp2 library asks the
  2458. * application to produce a mask to encrypt or decrypt packet header.
  2459. * The encryption cipher is |hp|. |hp_ctx| is the cipher context
  2460. * object which is initialized with the specific header protection
  2461. * key. The sample is passed as |sample| which is
  2462. * :macro:`NGTCP2_HP_SAMPLELEN` bytes long.
  2463. *
  2464. * The implementation of this callback must produce a mask using the
  2465. * header protection cipher suite specified by QUIC specification, and
  2466. * write the result into the buffer pointed by |dest|. The length of
  2467. * the mask must be at least :macro:`NGTCP2_HP_MASKLEN`. The library
  2468. * only uses the first :macro:`NGTCP2_HP_MASKLEN` bytes of the
  2469. * produced mask. The buffer pointed by |dest| is guaranteed to have
  2470. * at least :macro:`NGTCP2_HP_SAMPLELEN` bytes available for
  2471. * convenience.
  2472. *
  2473. * The callback function must return 0 if it succeeds, or
  2474. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` which makes the library call
  2475. * return immediately.
  2476. */
  2477. typedef int (*ngtcp2_hp_mask)(uint8_t *dest, const ngtcp2_crypto_cipher *hp,
  2478. const ngtcp2_crypto_cipher_ctx *hp_ctx,
  2479. const uint8_t *sample);
  2480. /**
  2481. * @macrosection
  2482. *
  2483. * STREAM frame data flags
  2484. */
  2485. /**
  2486. * @macro
  2487. *
  2488. * :macro:`NGTCP2_STREAM_DATA_FLAG_NONE` indicates no flag set.
  2489. */
  2490. #define NGTCP2_STREAM_DATA_FLAG_NONE 0x00u
  2491. /**
  2492. * @macro
  2493. *
  2494. * :macro:`NGTCP2_STREAM_DATA_FLAG_FIN` indicates that this chunk of
  2495. * data is final piece of an incoming stream.
  2496. */
  2497. #define NGTCP2_STREAM_DATA_FLAG_FIN 0x01u
  2498. /**
  2499. * @macro
  2500. *
  2501. * :macro:`NGTCP2_STREAM_DATA_FLAG_0RTT` indicates that this chunk of
  2502. * data contains data received in 0-RTT packet, and the handshake has
  2503. * not completed yet, which means that the data might be replayed.
  2504. */
  2505. #define NGTCP2_STREAM_DATA_FLAG_0RTT 0x02u
  2506. /**
  2507. * @functypedef
  2508. *
  2509. * :type:`ngtcp2_recv_stream_data` is invoked when stream data is
  2510. * received. The stream is specified by |stream_id|. |flags| is the
  2511. * bitwise-OR of zero or more of :macro:`NGTCP2_STREAM_DATA_FLAG_*
  2512. * <NGTCP2_STREAM_DATA_FLAG_NONE>`. If |flags| &
  2513. * :macro:`NGTCP2_STREAM_DATA_FLAG_FIN` is nonzero, this portion of
  2514. * the data is the last data in this stream. |offset| is the offset
  2515. * where this data begins. The library ensures that data is passed to
  2516. * the application in the non-decreasing order of |offset| without any
  2517. * overlap. The data is passed as |data| of length |datalen|.
  2518. * |datalen| may be 0 if and only if |fin| is nonzero.
  2519. *
  2520. * If :macro:`NGTCP2_STREAM_DATA_FLAG_0RTT` is set in |flags|, it
  2521. * indicates that a part of or whole data was received in 0-RTT
  2522. * packet, and a handshake has not completed yet.
  2523. *
  2524. * The callback function must return 0 if it succeeds, or
  2525. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` which makes the library return
  2526. * immediately.
  2527. */
  2528. typedef int (*ngtcp2_recv_stream_data)(ngtcp2_conn *conn, uint32_t flags,
  2529. int64_t stream_id, uint64_t offset,
  2530. const uint8_t *data, size_t datalen,
  2531. void *user_data, void *stream_user_data);
  2532. /**
  2533. * @functypedef
  2534. *
  2535. * :type:`ngtcp2_stream_open` is a callback function which is called
  2536. * when remote stream is opened by a remote endpoint. This function
  2537. * is not called if stream is opened by implicitly (we might
  2538. * reconsider this behaviour later).
  2539. *
  2540. * The implementation of this callback should return 0 if it succeeds.
  2541. * Returning :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library
  2542. * call return immediately.
  2543. */
  2544. typedef int (*ngtcp2_stream_open)(ngtcp2_conn *conn, int64_t stream_id,
  2545. void *user_data);
  2546. /**
  2547. * @macrosection
  2548. *
  2549. * Stream close flags
  2550. */
  2551. /**
  2552. * @macro
  2553. *
  2554. * :macro:`NGTCP2_STREAM_CLOSE_FLAG_NONE` indicates no flag set.
  2555. */
  2556. #define NGTCP2_STREAM_CLOSE_FLAG_NONE 0x00u
  2557. /**
  2558. * @macro
  2559. *
  2560. * :macro:`NGTCP2_STREAM_CLOSE_FLAG_APP_ERROR_CODE_SET` indicates that
  2561. * app_error_code parameter is set.
  2562. */
  2563. #define NGTCP2_STREAM_CLOSE_FLAG_APP_ERROR_CODE_SET 0x01u
  2564. /**
  2565. * @functypedef
  2566. *
  2567. * :type:`ngtcp2_stream_close` is invoked when a stream is closed.
  2568. * This callback is not called when QUIC connection is closed before
  2569. * existing streams are closed. |flags| is the bitwise-OR of zero or
  2570. * more of :macro:`NGTCP2_STREAM_CLOSE_FLAG_*
  2571. * <NGTCP2_STREAM_CLOSE_FLAG_NONE>`. |app_error_code| indicates the
  2572. * error code of this closure if
  2573. * :macro:`NGTCP2_STREAM_CLOSE_FLAG_APP_ERROR_CODE_SET` is set in
  2574. * |flags|. If it is not set, the stream was closed without any error
  2575. * code, which generally means success.
  2576. *
  2577. * |app_error_code| is the first application error code sent by a
  2578. * local endpoint, or received from a remote endpoint. If a stream is
  2579. * closed cleanly, no application error code is exchanged. Since QUIC
  2580. * stack does not know the application error code which indicates "no
  2581. * errors", |app_error_code| is set to 0 and
  2582. * :macro:`NGTCP2_STREAM_CLOSE_FLAG_APP_ERROR_CODE_SET` is not set in
  2583. * |flags| in this case.
  2584. *
  2585. * The implementation of this callback should return 0 if it succeeds.
  2586. * Returning :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library
  2587. * call return immediately.
  2588. */
  2589. typedef int (*ngtcp2_stream_close)(ngtcp2_conn *conn, uint32_t flags,
  2590. int64_t stream_id, uint64_t app_error_code,
  2591. void *user_data, void *stream_user_data);
  2592. /**
  2593. * @functypedef
  2594. *
  2595. * :type:`ngtcp2_stream_reset` is invoked when a stream identified by
  2596. * |stream_id| is reset by a remote endpoint.
  2597. *
  2598. * The implementation of this callback should return 0 if it succeeds.
  2599. * Returning :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library
  2600. * call return immediately.
  2601. */
  2602. typedef int (*ngtcp2_stream_reset)(ngtcp2_conn *conn, int64_t stream_id,
  2603. uint64_t final_size, uint64_t app_error_code,
  2604. void *user_data, void *stream_user_data);
  2605. /**
  2606. * @functypedef
  2607. *
  2608. * :type:`ngtcp2_acked_stream_data_offset` is a callback function
  2609. * which is called when stream data in range [|offset|, |offset| +
  2610. * |datalen|) is acknowledged, and application can free the portion of
  2611. * data. For a given |stream_id|, this callback is called
  2612. * sequentially in increasing order of |offset| without any overlap.
  2613. * |datalen| is normally strictly greater than 0. One exception is
  2614. * that when a STREAM frame has fin flag set and 0 length data, this
  2615. * callback is invoked with |datalen| == 0.
  2616. *
  2617. * If a stream is closed prematurely, and stream data is still
  2618. * in-flight, this callback function is not called for those data.
  2619. * After :member:`ngtcp2_callbacks.stream_close` is called for a
  2620. * particular stream, |conn| does not touch data for the closed stream
  2621. * again, and application can free all unacknowledged stream data.
  2622. *
  2623. * The implementation of this callback should return 0 if it succeeds.
  2624. * Returning :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library
  2625. * call return immediately.
  2626. */
  2627. typedef int (*ngtcp2_acked_stream_data_offset)(
  2628. ngtcp2_conn *conn, int64_t stream_id, uint64_t offset, uint64_t datalen,
  2629. void *user_data, void *stream_user_data);
  2630. /**
  2631. * @functypedef
  2632. *
  2633. * :type:`ngtcp2_recv_stateless_reset` is a callback function which is
  2634. * called when Stateless Reset packet is received. The stateless
  2635. * reset details are given in |sr|.
  2636. *
  2637. * The implementation of this callback should return 0 if it succeeds.
  2638. * Returning :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library
  2639. * call return immediately.
  2640. */
  2641. typedef int (*ngtcp2_recv_stateless_reset)(ngtcp2_conn *conn,
  2642. const ngtcp2_pkt_stateless_reset *sr,
  2643. void *user_data);
  2644. /**
  2645. * @functypedef
  2646. *
  2647. * :type:`ngtcp2_extend_max_streams` is a callback function which is
  2648. * called every time max stream ID is strictly extended.
  2649. * |max_streams| is the cumulative number of streams which an endpoint
  2650. * can open.
  2651. *
  2652. * The callback function must return 0 if it succeeds. Returning
  2653. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
  2654. * immediately.
  2655. */
  2656. typedef int (*ngtcp2_extend_max_streams)(ngtcp2_conn *conn,
  2657. uint64_t max_streams, void *user_data);
  2658. /**
  2659. * @functypedef
  2660. *
  2661. * :type:`ngtcp2_extend_max_stream_data` is a callback function which
  2662. * is invoked when max stream data is extended. |stream_id|
  2663. * identifies the stream. |max_data| is a cumulative number of bytes
  2664. * an endpoint can send on this stream.
  2665. *
  2666. * The callback function must return 0 if it succeeds. Returning
  2667. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
  2668. * immediately.
  2669. */
  2670. typedef int (*ngtcp2_extend_max_stream_data)(ngtcp2_conn *conn,
  2671. int64_t stream_id,
  2672. uint64_t max_data, void *user_data,
  2673. void *stream_user_data);
  2674. /**
  2675. * @functypedef
  2676. *
  2677. * :type:`ngtcp2_rand` is a callback function to get random data of
  2678. * length |destlen|. Application must fill random |destlen| bytes to
  2679. * the buffer pointed by |dest|. The generated data is used only in
  2680. * non-cryptographic context.
  2681. */
  2682. typedef void (*ngtcp2_rand)(uint8_t *dest, size_t destlen,
  2683. const ngtcp2_rand_ctx *rand_ctx);
  2684. /**
  2685. * @functypedef
  2686. *
  2687. * :type:`ngtcp2_get_new_connection_id` is a callback function to ask
  2688. * an application for new connection ID. Application must generate
  2689. * new unused connection ID with the exact |cidlen| bytes, and store
  2690. * it in |cid|. It also has to generate a stateless reset token, and
  2691. * store it in |token|. The length of stateless reset token is
  2692. * :macro:`NGTCP2_STATELESS_RESET_TOKENLEN` and it is guaranteed that
  2693. * the buffer pointed by |token| has the sufficient space to store the
  2694. * token.
  2695. *
  2696. * The callback function must return 0 if it succeeds. Returning
  2697. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
  2698. * immediately.
  2699. */
  2700. typedef int (*ngtcp2_get_new_connection_id)(ngtcp2_conn *conn, ngtcp2_cid *cid,
  2701. uint8_t *token, size_t cidlen,
  2702. void *user_data);
  2703. /**
  2704. * @functypedef
  2705. *
  2706. * :type:`ngtcp2_remove_connection_id` is a callback function which
  2707. * notifies the application that connection ID |cid| is no longer used
  2708. * by a remote endpoint. This Connection ID was previously offered by
  2709. * a local endpoint, and a remote endpoint could use it as Destination
  2710. * Connection ID when sending QUIC packet.
  2711. *
  2712. * The callback function must return 0 if it succeeds. Returning
  2713. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
  2714. * immediately.
  2715. */
  2716. typedef int (*ngtcp2_remove_connection_id)(ngtcp2_conn *conn,
  2717. const ngtcp2_cid *cid,
  2718. void *user_data);
  2719. /**
  2720. * @functypedef
  2721. *
  2722. * :type:`ngtcp2_update_key` is a callback function which tells the
  2723. * application that it must generate new packet protection keying
  2724. * materials and AEAD cipher context objects with new keys. The
  2725. * current set of secrets are given as |current_rx_secret| and
  2726. * |current_tx_secret| of length |secretlen|. They are decryption and
  2727. * encryption secrets respectively.
  2728. *
  2729. * The application must generate new secrets and keys for both
  2730. * encryption and decryption. It must write decryption secret and IV
  2731. * to the buffer pointed by |rx_secret| and |rx_iv| respectively. It
  2732. * also must create new AEAD cipher context object with new decryption
  2733. * key and initialize |rx_aead_ctx| with it. Similarly, write
  2734. * encryption secret and IV to the buffer pointed by |tx_secret| and
  2735. * |tx_iv|. Create new AEAD cipher context object with new encryption
  2736. * key and initialize |tx_aead_ctx| with it. All given buffers have
  2737. * the enough capacity to store secret, key and IV.
  2738. *
  2739. * The callback function must return 0 if it succeeds. Returning
  2740. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
  2741. * immediately.
  2742. */
  2743. typedef int (*ngtcp2_update_key)(
  2744. ngtcp2_conn *conn, uint8_t *rx_secret, uint8_t *tx_secret,
  2745. ngtcp2_crypto_aead_ctx *rx_aead_ctx, uint8_t *rx_iv,
  2746. ngtcp2_crypto_aead_ctx *tx_aead_ctx, uint8_t *tx_iv,
  2747. const uint8_t *current_rx_secret, const uint8_t *current_tx_secret,
  2748. size_t secretlen, void *user_data);
  2749. /**
  2750. * @macrosection
  2751. *
  2752. * Path validation related macros
  2753. */
  2754. /**
  2755. * @macro
  2756. *
  2757. * :macro:`NGTCP2_PATH_VALIDATION_FLAG_NONE` indicates no flag set.
  2758. */
  2759. #define NGTCP2_PATH_VALIDATION_FLAG_NONE 0x00u
  2760. /**
  2761. * @macro
  2762. *
  2763. * :macro:`NGTCP2_PATH_VALIDATION_FLAG_PREFERRED_ADDR` indicates the
  2764. * validation involving server preferred address. This flag is only
  2765. * set for client.
  2766. */
  2767. #define NGTCP2_PATH_VALIDATION_FLAG_PREFERRED_ADDR 0x01u
  2768. /**
  2769. * @macro
  2770. *
  2771. * :macro:`NGTCP2_PATH_VALIDATION_FLAG_NEW_TOKEN` indicates that
  2772. * server should send NEW_TOKEN frame for the new remote address.
  2773. * This flag is only set for server.
  2774. */
  2775. #define NGTCP2_PATH_VALIDATION_FLAG_NEW_TOKEN 0x02u
  2776. /**
  2777. * @functypedef
  2778. *
  2779. * :type:`ngtcp2_path_validation` is a callback function which tells
  2780. * an application the outcome of path validation. |flags| is zero or
  2781. * more of :macro:`NGTCP2_PATH_VALIDATION_FLAG_*
  2782. * <NGTCP2_PATH_VALIDATION_FLAG_NONE>`. |path| is the path that was
  2783. * validated. |old_path| is the path that is previously used before a
  2784. * local endpoint has migrated to |path| if |old_path| is not NULL.
  2785. * If |res| is
  2786. * :enum:`ngtcp2_path_validation_result.NGTCP2_PATH_VALIDATION_RESULT_SUCCESS`,
  2787. * the path validation succeeded. If |res| is
  2788. * :enum:`ngtcp2_path_validation_result.NGTCP2_PATH_VALIDATION_RESULT_FAILURE`,
  2789. * the path validation failed.
  2790. *
  2791. * The callback function must return 0 if it succeeds. Returning
  2792. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
  2793. * immediately.
  2794. */
  2795. typedef int (*ngtcp2_path_validation)(ngtcp2_conn *conn, uint32_t flags,
  2796. const ngtcp2_path *path,
  2797. const ngtcp2_path *old_path,
  2798. ngtcp2_path_validation_result res,
  2799. void *user_data);
  2800. /**
  2801. * @functypedef
  2802. *
  2803. * :type:`ngtcp2_select_preferred_addr` is a callback function which
  2804. * asks a client application to choose server address from preferred
  2805. * addresses |paddr| received from server. An application should
  2806. * write a network path for a selected preferred address in |dest|.
  2807. * More specifically, the selected preferred address must be set to
  2808. * :member:`dest->remote <ngtcp2_path.remote>`, a client source
  2809. * address must be set to :member:`dest->local <ngtcp2_path.local>`.
  2810. * If a client source address does not change for the new server
  2811. * address, leave :member:`dest->local <ngtcp2_path.local>`
  2812. * unmodified, or copy the value of :member:`local
  2813. * <ngtcp2_path.local>` field of the current network path obtained
  2814. * from `ngtcp2_conn_get_path()`. Both :member:`dest->local.addr
  2815. * <ngtcp2_addr.addr>` and :member:`dest->remote.addr
  2816. * <ngtcp2_addr.addr>` point to buffers which are at least
  2817. * sizeof(:type:`ngtcp2_sockaddr_union`) bytes long, respectively. If
  2818. * an application denies the preferred addresses, just leave |dest|
  2819. * unmodified (or set :member:`dest->remote.addrlen
  2820. * <ngtcp2_addr.addrlen>` to 0), and return 0.
  2821. *
  2822. * The callback function must return 0 if it succeeds. Returning
  2823. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
  2824. * immediately.
  2825. */
  2826. typedef int (*ngtcp2_select_preferred_addr)(ngtcp2_conn *conn,
  2827. ngtcp2_path *dest,
  2828. const ngtcp2_preferred_addr *paddr,
  2829. void *user_data);
  2830. /**
  2831. * @enum
  2832. *
  2833. * :type:`ngtcp2_connection_id_status_type` defines a set of status
  2834. * for Destination Connection ID.
  2835. */
  2836. typedef enum ngtcp2_connection_id_status_type {
  2837. /**
  2838. * :enum:`NGTCP2_CONNECTION_ID_STATUS_TYPE_ACTIVATE` indicates that
  2839. * a local endpoint starts using new Destination Connection ID.
  2840. */
  2841. NGTCP2_CONNECTION_ID_STATUS_TYPE_ACTIVATE,
  2842. /**
  2843. * :enum:`NGTCP2_CONNECTION_ID_STATUS_TYPE_DEACTIVATE` indicates
  2844. * that a local endpoint stops using a given Destination Connection
  2845. * ID.
  2846. */
  2847. NGTCP2_CONNECTION_ID_STATUS_TYPE_DEACTIVATE
  2848. } ngtcp2_connection_id_status_type;
  2849. /**
  2850. * @functypedef
  2851. *
  2852. * :type:`ngtcp2_connection_id_status` is a callback function which is
  2853. * called when the status of Destination Connection ID changes.
  2854. *
  2855. * |token| is the associated stateless reset token, and it is ``NULL``
  2856. * if no token is present.
  2857. *
  2858. * |type| is the one of the value defined in
  2859. * :type:`ngtcp2_connection_id_status_type`. The new value might be
  2860. * added in the future release.
  2861. *
  2862. * The callback function must return 0 if it succeeds. Returning
  2863. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
  2864. * immediately.
  2865. */
  2866. typedef int (*ngtcp2_connection_id_status)(
  2867. ngtcp2_conn *conn, ngtcp2_connection_id_status_type type, uint64_t seq,
  2868. const ngtcp2_cid *cid, const uint8_t *token, void *user_data);
  2869. /**
  2870. * @functypedef
  2871. *
  2872. * :type:`ngtcp2_recv_new_token` is a callback function which is
  2873. * called when new token is received from server. This callback is
  2874. * client use only.
  2875. *
  2876. * |token| is the received token of length |tokenlen| bytes long.
  2877. *
  2878. * The callback function must return 0 if it succeeds. Returning
  2879. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
  2880. * immediately.
  2881. */
  2882. typedef int (*ngtcp2_recv_new_token)(ngtcp2_conn *conn, const uint8_t *token,
  2883. size_t tokenlen, void *user_data);
  2884. /**
  2885. * @functypedef
  2886. *
  2887. * :type:`ngtcp2_delete_crypto_aead_ctx` is a callback function which
  2888. * must delete the native object pointed by
  2889. * :member:`aead_ctx->native_handle
  2890. * <ngtcp2_crypto_aead_ctx.native_handle>`.
  2891. */
  2892. typedef void (*ngtcp2_delete_crypto_aead_ctx)(ngtcp2_conn *conn,
  2893. ngtcp2_crypto_aead_ctx *aead_ctx,
  2894. void *user_data);
  2895. /**
  2896. * @functypedef
  2897. *
  2898. * :type:`ngtcp2_delete_crypto_cipher_ctx` is a callback function
  2899. * which must delete the native object pointed by
  2900. * :member:`cipher_ctx->native_handle
  2901. * <ngtcp2_crypto_cipher_ctx.native_handle>`.
  2902. */
  2903. typedef void (*ngtcp2_delete_crypto_cipher_ctx)(
  2904. ngtcp2_conn *conn, ngtcp2_crypto_cipher_ctx *cipher_ctx, void *user_data);
  2905. /**
  2906. * @macrosection
  2907. *
  2908. * DATAGRAM frame flags
  2909. */
  2910. /**
  2911. * @macro
  2912. *
  2913. * :macro:`NGTCP2_DATAGRAM_FLAG_NONE` indicates no flag set.
  2914. */
  2915. #define NGTCP2_DATAGRAM_FLAG_NONE 0x00u
  2916. /**
  2917. * @macro
  2918. *
  2919. * :macro:`NGTCP2_DATAGRAM_FLAG_0RTT` indicates that DATAGRAM frame is
  2920. * received in 0-RTT packet, and the handshake has not completed yet,
  2921. * which means that the data might be replayed.
  2922. */
  2923. #define NGTCP2_DATAGRAM_FLAG_0RTT 0x01u
  2924. /**
  2925. * @functypedef
  2926. *
  2927. * :type:`ngtcp2_recv_datagram` is invoked when DATAGRAM frame is
  2928. * received. |flags| is bitwise-OR of zero or more of
  2929. * :macro:`NGTCP2_DATAGRAM_FLAG_* <NGTCP2_DATAGRAM_FLAG_NONE>`.
  2930. *
  2931. * If :macro:`NGTCP2_DATAGRAM_FLAG_0RTT` is set in |flags|, it
  2932. * indicates that DATAGRAM frame was received in 0-RTT packet, and a
  2933. * handshake has not completed yet.
  2934. *
  2935. * The callback function must return 0 if it succeeds, or
  2936. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` which makes the library return
  2937. * immediately.
  2938. */
  2939. typedef int (*ngtcp2_recv_datagram)(ngtcp2_conn *conn, uint32_t flags,
  2940. const uint8_t *data, size_t datalen,
  2941. void *user_data);
  2942. /**
  2943. * @functypedef
  2944. *
  2945. * :type:`ngtcp2_ack_datagram` is invoked when a packet which contains
  2946. * DATAGRAM frame which is identified by |dgram_id| is acknowledged.
  2947. * |dgram_id| is the valued passed to `ngtcp2_conn_writev_datagram`.
  2948. *
  2949. * The callback function must return 0 if it succeeds, or
  2950. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` which makes the library return
  2951. * immediately.
  2952. */
  2953. typedef int (*ngtcp2_ack_datagram)(ngtcp2_conn *conn, uint64_t dgram_id,
  2954. void *user_data);
  2955. /**
  2956. * @functypedef
  2957. *
  2958. * :type:`ngtcp2_lost_datagram` is invoked when a packet which
  2959. * contains DATAGRAM frame which is identified by |dgram_id| is
  2960. * declared lost. |dgram_id| is the valued passed to
  2961. * `ngtcp2_conn_writev_datagram`. Note that the loss might be
  2962. * spurious, and DATAGRAM frame might be acknowledged later.
  2963. *
  2964. * The callback function must return 0 if it succeeds, or
  2965. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` which makes the library return
  2966. * immediately.
  2967. */
  2968. typedef int (*ngtcp2_lost_datagram)(ngtcp2_conn *conn, uint64_t dgram_id,
  2969. void *user_data);
  2970. /**
  2971. * @functypedef
  2972. *
  2973. * :type:`ngtcp2_get_path_challenge_data` is a callback function to
  2974. * ask an application for new data that is sent in PATH_CHALLENGE
  2975. * frame. Application must generate new unpredictable, exactly
  2976. * :macro:`NGTCP2_PATH_CHALLENGE_DATALEN` bytes of random data, and
  2977. * store them into the buffer pointed by |data|.
  2978. *
  2979. * The callback function must return 0 if it succeeds. Returning
  2980. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
  2981. * immediately.
  2982. */
  2983. typedef int (*ngtcp2_get_path_challenge_data)(ngtcp2_conn *conn, uint8_t *data,
  2984. void *user_data);
  2985. /**
  2986. * @functypedef
  2987. *
  2988. * :type:`ngtcp2_stream_stop_sending` is invoked when a stream is no
  2989. * longer read by a local endpoint before it receives all stream data.
  2990. * This function is called at most once per stream. |app_error_code|
  2991. * is the error code passed to `ngtcp2_conn_shutdown_stream_read` or
  2992. * `ngtcp2_conn_shutdown_stream`.
  2993. *
  2994. * The callback function must return 0 if it succeeds. Returning
  2995. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
  2996. * immediately.
  2997. */
  2998. typedef int (*ngtcp2_stream_stop_sending)(ngtcp2_conn *conn, int64_t stream_id,
  2999. uint64_t app_error_code,
  3000. void *user_data,
  3001. void *stream_user_data);
  3002. /**
  3003. * @functypedef
  3004. *
  3005. * :type:`ngtcp2_version_negotiation` is invoked when the compatible
  3006. * version negotiation takes place. For client, it is called when it
  3007. * sees a change in version field of a long header packet. This
  3008. * callback function might be called multiple times for client. For
  3009. * server, it is called once when the version is negotiated.
  3010. *
  3011. * The implementation of this callback must install new Initial keys
  3012. * for |version| and Destination Connection ID |client_dcid| from
  3013. * client. Use `ngtcp2_conn_install_vneg_initial_key` to install
  3014. * keys.
  3015. *
  3016. * The callback function must return 0 if it succeeds. Returning
  3017. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
  3018. * immediately.
  3019. */
  3020. typedef int (*ngtcp2_version_negotiation)(ngtcp2_conn *conn, uint32_t version,
  3021. const ngtcp2_cid *client_dcid,
  3022. void *user_data);
  3023. /**
  3024. * @functypedef
  3025. *
  3026. * :type:`ngtcp2_recv_key` is invoked when new key is installed to
  3027. * |conn| during QUIC cryptographic handshake.
  3028. *
  3029. * The callback function must return 0 if it succeeds. Returning
  3030. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
  3031. * immediately.
  3032. */
  3033. typedef int (*ngtcp2_recv_key)(ngtcp2_conn *conn, ngtcp2_encryption_level level,
  3034. void *user_data);
  3035. /**
  3036. * @functypedef
  3037. *
  3038. * :type:`ngtcp2_tls_early_data_rejected` is invoked when early data
  3039. * was rejected by server during TLS handshake, or client decided not
  3040. * to attempt early data.
  3041. *
  3042. * The callback function must return 0 if it succeeds. Returning
  3043. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
  3044. * immediately.
  3045. */
  3046. typedef int (*ngtcp2_tls_early_data_rejected)(ngtcp2_conn *conn,
  3047. void *user_data);
  3048. #define NGTCP2_CALLBACKS_V1 1
  3049. #define NGTCP2_CALLBACKS_VERSION NGTCP2_CALLBACKS_V1
  3050. /**
  3051. * @struct
  3052. *
  3053. * :type:`ngtcp2_callbacks` holds a set of callback functions.
  3054. */
  3055. typedef struct ngtcp2_callbacks {
  3056. /**
  3057. * :member:`client_initial` is a callback function which is invoked
  3058. * when client asks TLS stack to produce first TLS cryptographic
  3059. * handshake message. This callback function must be specified for
  3060. * a client application.
  3061. */
  3062. ngtcp2_client_initial client_initial;
  3063. /**
  3064. * :member:`recv_client_initial` is a callback function which is
  3065. * invoked when a server receives the first Initial packet from
  3066. * client. This callback function must be specified for a server
  3067. * application.
  3068. */
  3069. ngtcp2_recv_client_initial recv_client_initial;
  3070. /**
  3071. * :member:`recv_crypto_data` is a callback function which is
  3072. * invoked when cryptographic data (CRYPTO frame, in other words,
  3073. * TLS message) is received. This callback function must be
  3074. * specified.
  3075. */
  3076. ngtcp2_recv_crypto_data recv_crypto_data;
  3077. /**
  3078. * :member:`handshake_completed` is a callback function which is
  3079. * invoked when QUIC cryptographic handshake has completed. This
  3080. * callback function is optional.
  3081. */
  3082. ngtcp2_handshake_completed handshake_completed;
  3083. /**
  3084. * :member:`recv_version_negotiation` is a callback function which
  3085. * is invoked when Version Negotiation packet is received by a
  3086. * client. This callback function is optional.
  3087. */
  3088. ngtcp2_recv_version_negotiation recv_version_negotiation;
  3089. /**
  3090. * :member:`encrypt` is a callback function which is invoked to
  3091. * encrypt a QUIC packet. This callback function must be specified.
  3092. */
  3093. ngtcp2_encrypt encrypt;
  3094. /**
  3095. * :member:`decrypt` is a callback function which is invoked to
  3096. * decrypt a QUIC packet. This callback function must be specified.
  3097. */
  3098. ngtcp2_decrypt decrypt;
  3099. /**
  3100. * :member:`hp_mask` is a callback function which is invoked to get
  3101. * a mask to encrypt or decrypt QUIC packet header. This callback
  3102. * function must be specified.
  3103. */
  3104. ngtcp2_hp_mask hp_mask;
  3105. /**
  3106. * :member:`recv_stream_data` is a callback function which is
  3107. * invoked when stream data, which includes application data, is
  3108. * received. This callback function is optional.
  3109. */
  3110. ngtcp2_recv_stream_data recv_stream_data;
  3111. /**
  3112. * :member:`acked_stream_data_offset` is a callback function which
  3113. * is invoked when stream data, which includes application data, is
  3114. * acknowledged by a remote endpoint. It tells an application the
  3115. * largest offset of acknowledged stream data without a gap so that
  3116. * application can free memory for the data up to that offset. This
  3117. * callback function is optional.
  3118. */
  3119. ngtcp2_acked_stream_data_offset acked_stream_data_offset;
  3120. /**
  3121. * :member:`stream_open` is a callback function which is invoked
  3122. * when new remote stream is opened by a remote endpoint. This
  3123. * callback function is optional.
  3124. */
  3125. ngtcp2_stream_open stream_open;
  3126. /**
  3127. * :member:`stream_close` is a callback function which is invoked
  3128. * when a stream is closed. This callback function is optional.
  3129. */
  3130. ngtcp2_stream_close stream_close;
  3131. /**
  3132. * :member:`recv_stateless_reset` is a callback function which is
  3133. * invoked when Stateless Reset packet is received. This callback
  3134. * function is optional.
  3135. */
  3136. ngtcp2_recv_stateless_reset recv_stateless_reset;
  3137. /**
  3138. * :member:`recv_retry` is a callback function which is invoked when
  3139. * a client receives Retry packet. For client, this callback
  3140. * function must be specified. Server never receive Retry packet.
  3141. */
  3142. ngtcp2_recv_retry recv_retry;
  3143. /**
  3144. * :member:`extend_max_local_streams_bidi` is a callback function
  3145. * which is invoked when the number of bidirectional stream which a
  3146. * local endpoint can open is increased. This callback function is
  3147. * optional.
  3148. */
  3149. ngtcp2_extend_max_streams extend_max_local_streams_bidi;
  3150. /**
  3151. * :member:`extend_max_local_streams_uni` is a callback function
  3152. * which is invoked when the number of unidirectional stream which a
  3153. * local endpoint can open is increased. This callback function is
  3154. * optional.
  3155. */
  3156. ngtcp2_extend_max_streams extend_max_local_streams_uni;
  3157. /**
  3158. * :member:`rand` is a callback function which is invoked when the
  3159. * library needs random data. This callback function must be
  3160. * specified.
  3161. */
  3162. ngtcp2_rand rand;
  3163. /**
  3164. * :member:`get_new_connection_id` is a callback function which is
  3165. * invoked when the library needs new connection ID. This callback
  3166. * function must be specified.
  3167. */
  3168. ngtcp2_get_new_connection_id get_new_connection_id;
  3169. /**
  3170. * :member:`remove_connection_id` is a callback function which
  3171. * notifies an application that connection ID is no longer used by a
  3172. * remote endpoint. This callback function is optional.
  3173. */
  3174. ngtcp2_remove_connection_id remove_connection_id;
  3175. /**
  3176. * :member:`update_key` is a callback function which is invoked when
  3177. * the library tells an application that it must update keying
  3178. * materials, and install new keys. This callback function must be
  3179. * specified.
  3180. */
  3181. ngtcp2_update_key update_key;
  3182. /**
  3183. * :member:`path_validation` is a callback function which is invoked
  3184. * when path validation completed. This callback function is
  3185. * optional.
  3186. */
  3187. ngtcp2_path_validation path_validation;
  3188. /**
  3189. * :member:`select_preferred_addr` is a callback function which is
  3190. * invoked when the library asks a client to select preferred
  3191. * address presented by a server. If not set, client ignores
  3192. * preferred addresses. This callback function is optional.
  3193. */
  3194. ngtcp2_select_preferred_addr select_preferred_addr;
  3195. /**
  3196. * :member:`stream_reset` is a callback function which is invoked
  3197. * when a stream is reset by a remote endpoint. This callback
  3198. * function is optional.
  3199. */
  3200. ngtcp2_stream_reset stream_reset;
  3201. /**
  3202. * :member:`extend_max_remote_streams_bidi` is a callback function
  3203. * which is invoked when the number of bidirectional streams which a
  3204. * remote endpoint can open is increased. This callback function is
  3205. * optional.
  3206. */
  3207. ngtcp2_extend_max_streams extend_max_remote_streams_bidi;
  3208. /**
  3209. * :member:`extend_max_remote_streams_uni` is a callback function
  3210. * which is invoked when the number of unidirectional streams which
  3211. * a remote endpoint can open is increased. This callback function
  3212. * is optional.
  3213. */
  3214. ngtcp2_extend_max_streams extend_max_remote_streams_uni;
  3215. /**
  3216. * :member:`extend_max_stream_data` is callback function which is
  3217. * invoked when the maximum offset of stream data that a local
  3218. * endpoint can send is increased. This callback function is
  3219. * optional.
  3220. */
  3221. ngtcp2_extend_max_stream_data extend_max_stream_data;
  3222. /**
  3223. * :member:`dcid_status` is a callback function which is invoked
  3224. * when the new Destination Connection ID is activated, or the
  3225. * activated Destination Connection ID is now deactivated. This
  3226. * callback function is optional.
  3227. */
  3228. ngtcp2_connection_id_status dcid_status;
  3229. /**
  3230. * :member:`handshake_confirmed` is a callback function which is
  3231. * invoked when both endpoints agree that handshake has finished.
  3232. * This field is ignored by server because
  3233. * :member:`handshake_completed` also indicates the handshake
  3234. * confirmation for server. This callback function is optional.
  3235. */
  3236. ngtcp2_handshake_confirmed handshake_confirmed;
  3237. /**
  3238. * :member:`recv_new_token` is a callback function which is invoked
  3239. * when new token is received from server. This field is ignored by
  3240. * server. This callback function is optional.
  3241. */
  3242. ngtcp2_recv_new_token recv_new_token;
  3243. /**
  3244. * :member:`delete_crypto_aead_ctx` is a callback function which
  3245. * deletes a given AEAD cipher context object. This callback
  3246. * function must be specified.
  3247. */
  3248. ngtcp2_delete_crypto_aead_ctx delete_crypto_aead_ctx;
  3249. /**
  3250. * :member:`delete_crypto_cipher_ctx` is a callback function which
  3251. * deletes a given cipher context object. This callback function
  3252. * must be specified.
  3253. */
  3254. ngtcp2_delete_crypto_cipher_ctx delete_crypto_cipher_ctx;
  3255. /**
  3256. * :member:`recv_datagram` is a callback function which is invoked
  3257. * when DATAGRAM frame is received. This callback function is
  3258. * optional.
  3259. */
  3260. ngtcp2_recv_datagram recv_datagram;
  3261. /**
  3262. * :member:`ack_datagram` is a callback function which is invoked
  3263. * when a QUIC packet containing DATAGRAM frame is acknowledged by a
  3264. * remote endpoint. This callback function is optional.
  3265. */
  3266. ngtcp2_ack_datagram ack_datagram;
  3267. /**
  3268. * :member:`lost_datagram` is a callback function which is invoked
  3269. * when a QUIC packet containing DATAGRAM frame is declared lost.
  3270. * This callback function is optional.
  3271. */
  3272. ngtcp2_lost_datagram lost_datagram;
  3273. /**
  3274. * :member:`get_path_challenge_data` is a callback function which is
  3275. * invoked when the library needs new data sent along with
  3276. * PATH_CHALLENGE frame. This callback must be specified.
  3277. */
  3278. ngtcp2_get_path_challenge_data get_path_challenge_data;
  3279. /**
  3280. * :member:`stream_stop_sending` is a callback function which is
  3281. * invoked when a local endpoint no longer reads from a stream
  3282. * before it receives all stream data. This callback function is
  3283. * optional.
  3284. */
  3285. ngtcp2_stream_stop_sending stream_stop_sending;
  3286. /**
  3287. * :member:`version_negotiation` is a callback function which is
  3288. * invoked when the compatible version negotiation takes place.
  3289. * This callback function must be specified.
  3290. */
  3291. ngtcp2_version_negotiation version_negotiation;
  3292. /**
  3293. * :member:`recv_rx_key` is a callback function which is invoked
  3294. * when a new key for decrypting packets is installed during QUIC
  3295. * cryptographic handshake. It is not called for
  3296. * :enum:`ngtcp2_encryption_level.NGTCP2_ENCRYPTION_LEVEL_INITIAL`.
  3297. */
  3298. ngtcp2_recv_key recv_rx_key;
  3299. /**
  3300. * :member:`recv_tx_key` is a callback function which is invoked
  3301. * when a new key for encrypting packets is installed during QUIC
  3302. * cryptographic handshake. It is not called for
  3303. * :enum:`ngtcp2_encryption_level.NGTCP2_ENCRYPTION_LEVEL_INITIAL`.
  3304. */
  3305. ngtcp2_recv_key recv_tx_key;
  3306. /**
  3307. * :member:`tls_early_data_rejected` is a callback function which is
  3308. * invoked when server rejected early data during TLS handshake, or
  3309. * client decided not to attempt early data. This callback function
  3310. * is only used by client.
  3311. */
  3312. ngtcp2_tls_early_data_rejected tls_early_data_rejected;
  3313. } ngtcp2_callbacks;
  3314. /**
  3315. * @function
  3316. *
  3317. * `ngtcp2_pkt_write_connection_close` writes Initial packet
  3318. * containing CONNECTION_CLOSE frame with the given |error_code| and
  3319. * the optional |reason| of length |reasonlen| to the buffer pointed
  3320. * by |dest| of length |destlen|. All encryption parameters are for
  3321. * Initial packet encryption. The packet number is always 0.
  3322. *
  3323. * The primary use case of this function is for server to send
  3324. * CONNECTION_CLOSE frame in Initial packet to close connection
  3325. * without committing any state when validating Retry token fails.
  3326. *
  3327. * This function returns the number of bytes written if it succeeds,
  3328. * or one of the following negative error codes:
  3329. *
  3330. * :macro:`NGTCP2_ERR_NOBUF`
  3331. * Buffer is too small.
  3332. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE`
  3333. * Callback function failed.
  3334. */
  3335. NGTCP2_EXTERN ngtcp2_ssize ngtcp2_pkt_write_connection_close(
  3336. uint8_t *dest, size_t destlen, uint32_t version, const ngtcp2_cid *dcid,
  3337. const ngtcp2_cid *scid, uint64_t error_code, const uint8_t *reason,
  3338. size_t reasonlen, ngtcp2_encrypt encrypt, const ngtcp2_crypto_aead *aead,
  3339. const ngtcp2_crypto_aead_ctx *aead_ctx, const uint8_t *iv,
  3340. ngtcp2_hp_mask hp_mask, const ngtcp2_crypto_cipher *hp,
  3341. const ngtcp2_crypto_cipher_ctx *hp_ctx);
  3342. /**
  3343. * @function
  3344. *
  3345. * `ngtcp2_pkt_write_retry` writes Retry packet in the buffer pointed
  3346. * by |dest| whose length is |destlen|. |dcid| is the Connection ID
  3347. * which appeared in a packet as a Source Connection ID sent by
  3348. * client. |scid| is a server chosen Source Connection ID. |odcid|
  3349. * specifies Original Destination Connection ID which appeared in a
  3350. * packet as a Destination Connection ID sent by client. |token|
  3351. * specifies Retry Token, and |tokenlen| specifies its length. |aead|
  3352. * must be AEAD_AES_128_GCM. |aead_ctx| must be initialized with
  3353. * :macro:`NGTCP2_RETRY_KEY` as an encryption key.
  3354. *
  3355. * This function returns the number of bytes written to the buffer, or
  3356. * one of the following negative error codes:
  3357. *
  3358. * :macro:`NGTCP2_ERR_NOBUF`
  3359. * Buffer is too small.
  3360. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE`
  3361. * Callback function failed.
  3362. * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
  3363. * :member:`odcid->datalen <ngtcp2_cid.datalen>` is less than
  3364. * :macro:`NGTCP2_MIN_INITIAL_DCIDLEN`.
  3365. */
  3366. NGTCP2_EXTERN ngtcp2_ssize ngtcp2_pkt_write_retry(
  3367. uint8_t *dest, size_t destlen, uint32_t version, const ngtcp2_cid *dcid,
  3368. const ngtcp2_cid *scid, const ngtcp2_cid *odcid, const uint8_t *token,
  3369. size_t tokenlen, ngtcp2_encrypt encrypt, const ngtcp2_crypto_aead *aead,
  3370. const ngtcp2_crypto_aead_ctx *aead_ctx);
  3371. /**
  3372. * @function
  3373. *
  3374. * `ngtcp2_accept` is used by server implementation, and decides
  3375. * whether packet |pkt| of length |pktlen| from client is acceptable
  3376. * for the very first packet to a connection.
  3377. *
  3378. * If |dest| is not ``NULL`` and the function returns 0, the decoded
  3379. * packet header is stored in the object pointed by |dest|.
  3380. *
  3381. * This function returns 0 if it succeeds, or one of the following
  3382. * negative error codes:
  3383. *
  3384. * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
  3385. * The packet is not acceptable for the very first packet to a new
  3386. * connection; or the function failed to parse the packet header.
  3387. */
  3388. NGTCP2_EXTERN int ngtcp2_accept(ngtcp2_pkt_hd *dest, const uint8_t *pkt,
  3389. size_t pktlen);
  3390. /**
  3391. * @function
  3392. *
  3393. * `ngtcp2_conn_client_new` creates new :type:`ngtcp2_conn`, and
  3394. * initializes it as client. On success, it stores the pointer to the
  3395. * newly allocated object in |*pconn|. |dcid| is a randomized
  3396. * Destination Connection ID which must be longer than or equal to
  3397. * :macro:`NGTCP2_MIN_INITIAL_DCIDLEN`. |scid| is a Source Connection
  3398. * ID chosen by client. |client_chosen_version| is a QUIC version
  3399. * that a client chooses. |path| is the network path where this QUIC
  3400. * connection is being established, and must not be ``NULL``.
  3401. * |callbacks|, |settings|, and |params| must not be ``NULL``, and the
  3402. * function makes a copy of each of them. |params| is a local QUIC
  3403. * transport parameters, and sent to a remote endpoint during
  3404. * handshake. |user_data| is the arbitrary pointer which is passed to
  3405. * the user-defined callback functions. If |mem| is ``NULL``, the
  3406. * memory allocator returned by `ngtcp2_mem_default()` is used.
  3407. *
  3408. * Call `ngtcp2_conn_del` to free memory allocated for |*pconn|.
  3409. *
  3410. * This function returns 0 if it succeeds, or one of the following
  3411. * negative error codes:
  3412. *
  3413. * :macro:`NGTCP2_ERR_NOMEM`
  3414. * Out of memory.
  3415. */
  3416. NGTCP2_EXTERN int ngtcp2_conn_client_new_versioned(
  3417. ngtcp2_conn **pconn, const ngtcp2_cid *dcid, const ngtcp2_cid *scid,
  3418. const ngtcp2_path *path, uint32_t client_chosen_version,
  3419. int callbacks_version, const ngtcp2_callbacks *callbacks,
  3420. int settings_version, const ngtcp2_settings *settings,
  3421. int transport_params_version, const ngtcp2_transport_params *params,
  3422. const ngtcp2_mem *mem, void *user_data);
  3423. /**
  3424. * @function
  3425. *
  3426. * `ngtcp2_conn_server_new` creates new :type:`ngtcp2_conn`, and
  3427. * initializes it as server. On success, it stores the pointer to the
  3428. * newly allocated object in |*pconn|. |dcid| is a Destination
  3429. * Connection ID, and is usually the Connection ID that appears in
  3430. * client Initial packet as Source Connection ID. |scid| is a Source
  3431. * Connection ID chosen by server. |path| is the network path where
  3432. * this QUIC connection is being established, and must not be
  3433. * ``NULL``. |client_chosen_version| is a QUIC version that a client
  3434. * chooses. |callbacks|, |settings|, and |params| must not be
  3435. * ``NULL``, and the function makes a copy of each of them. |params|
  3436. * is a local QUIC transport parameters, and sent to a remote endpoint
  3437. * during handshake. |user_data| is the arbitrary pointer which is
  3438. * passed to the user-defined callback functions. If |mem| is
  3439. * ``NULL``, the memory allocator returned by `ngtcp2_mem_default()`
  3440. * is used.
  3441. *
  3442. * Call `ngtcp2_conn_del` to free memory allocated for |*pconn|.
  3443. *
  3444. * This function returns 0 if it succeeds, or one of the following
  3445. * negative error codes:
  3446. *
  3447. * :macro:`NGTCP2_ERR_NOMEM`
  3448. * Out of memory.
  3449. */
  3450. NGTCP2_EXTERN int ngtcp2_conn_server_new_versioned(
  3451. ngtcp2_conn **pconn, const ngtcp2_cid *dcid, const ngtcp2_cid *scid,
  3452. const ngtcp2_path *path, uint32_t client_chosen_version,
  3453. int callbacks_version, const ngtcp2_callbacks *callbacks,
  3454. int settings_version, const ngtcp2_settings *settings,
  3455. int transport_params_version, const ngtcp2_transport_params *params,
  3456. const ngtcp2_mem *mem, void *user_data);
  3457. /**
  3458. * @function
  3459. *
  3460. * `ngtcp2_conn_del` frees resources allocated for |conn|. It also
  3461. * frees memory pointed by |conn|.
  3462. */
  3463. NGTCP2_EXTERN void ngtcp2_conn_del(ngtcp2_conn *conn);
  3464. /**
  3465. * @function
  3466. *
  3467. * `ngtcp2_conn_read_pkt` decrypts QUIC packet given in |pkt| of
  3468. * length |pktlen| and processes it. |path| is the network path the
  3469. * packet is delivered and must not be ``NULL``. |pi| is packet
  3470. * metadata and may be ``NULL``. This function performs QUIC handshake
  3471. * as well.
  3472. *
  3473. * This function must not be called from inside the callback
  3474. * functions.
  3475. *
  3476. * This function returns 0 if it succeeds, or one of the following
  3477. * negative error codes:
  3478. *
  3479. * :macro:`NGTCP2_ERR_RETRY`
  3480. * Server must perform address validation by sending Retry packet
  3481. * (see `ngtcp2_crypto_write_retry` and `ngtcp2_pkt_write_retry`),
  3482. * and discard the connection state. Client application does not
  3483. * get this error code.
  3484. * :macro:`NGTCP2_ERR_DROP_CONN`
  3485. * Server application must drop the connection silently (without
  3486. * sending any CONNECTION_CLOSE frame), and discard connection
  3487. * state. Client application does not get this error code.
  3488. * :macro:`NGTCP2_ERR_DRAINING`
  3489. * A connection has entered the draining state, and no further
  3490. * packet transmission is allowed.
  3491. * :macro:`NGTCP2_ERR_CLOSING`
  3492. * A connection has entered the closing state, and no further
  3493. * packet transmission is allowed. Calling
  3494. * `ngtcp2_conn_write_connection_close` makes a connection enter
  3495. * this state.
  3496. * :macro:`NGTCP2_ERR_CRYPTO`
  3497. * An error happened in TLS stack. `ngtcp2_conn_get_tls_alert`
  3498. * returns TLS alert if set.
  3499. *
  3500. * If any other negative error is returned, call
  3501. * `ngtcp2_conn_write_connection_close` to get terminal packet, and
  3502. * sending it makes QUIC connection enter the closing state.
  3503. */
  3504. NGTCP2_EXTERN int
  3505. ngtcp2_conn_read_pkt_versioned(ngtcp2_conn *conn, const ngtcp2_path *path,
  3506. int pkt_info_version, const ngtcp2_pkt_info *pi,
  3507. const uint8_t *pkt, size_t pktlen,
  3508. ngtcp2_tstamp ts);
  3509. /**
  3510. * @function
  3511. *
  3512. * `ngtcp2_conn_write_pkt` is equivalent to calling
  3513. * `ngtcp2_conn_writev_stream` with -1 as |stream_id|, no stream data,
  3514. * and :macro:`NGTCP2_WRITE_STREAM_FLAG_NONE` as flags.
  3515. */
  3516. NGTCP2_EXTERN ngtcp2_ssize ngtcp2_conn_write_pkt_versioned(
  3517. ngtcp2_conn *conn, ngtcp2_path *path, int pkt_info_version,
  3518. ngtcp2_pkt_info *pi, uint8_t *dest, size_t destlen, ngtcp2_tstamp ts);
  3519. /**
  3520. * @function
  3521. *
  3522. * `ngtcp2_conn_tls_handshake_completed` tells |conn| that the TLS
  3523. * stack declares TLS handshake completion. This does not mean QUIC
  3524. * handshake has completed. The library needs extra conditions to be
  3525. * met.
  3526. */
  3527. NGTCP2_EXTERN void ngtcp2_conn_tls_handshake_completed(ngtcp2_conn *conn);
  3528. /**
  3529. * @function
  3530. *
  3531. * `ngtcp2_conn_get_handshake_completed` returns nonzero if QUIC
  3532. * handshake has completed.
  3533. */
  3534. NGTCP2_EXTERN int ngtcp2_conn_get_handshake_completed(ngtcp2_conn *conn);
  3535. /**
  3536. * @function
  3537. *
  3538. * `ngtcp2_conn_install_initial_key` installs packet protection keying
  3539. * materials for Initial packets. |rx_aead_ctx| is AEAD cipher
  3540. * context object, and must be initialized with a decryption key.
  3541. * |rx_iv| is IV of length |rx_ivlen| for decryption. |rx_hp_ctx| is
  3542. * a packet header protection cipher context object for decryption.
  3543. * Similarly, |tx_aead_ctx|, |tx_iv| and |tx_hp_ctx| are for
  3544. * encrypting outgoing packets, and are the same length with the
  3545. * decryption counterpart . If they have already been set, they are
  3546. * overwritten.
  3547. *
  3548. * |ivlen| must be the minimum length of AEAD nonce, or 8 bytes if
  3549. * that is larger.
  3550. *
  3551. * If this function succeeds, |conn| takes ownership of |rx_aead_ctx|,
  3552. * |rx_hp_ctx|, |tx_aead_ctx|, and |tx_hp_ctx|.
  3553. * :member:`ngtcp2_callbacks.delete_crypto_aead_ctx` and
  3554. * :member:`ngtcp2_callbacks.delete_crypto_cipher_ctx` will be called
  3555. * to delete these objects when they are no longer used. If this
  3556. * function fails, the caller is responsible to delete them.
  3557. *
  3558. * After receiving Retry packet, a Destination Connection ID that
  3559. * client sends in Initial packet most likely changes. In that case,
  3560. * client application must generate these keying materials again based
  3561. * on new Destination Connection ID, and install them again with this
  3562. * function.
  3563. *
  3564. * This function returns 0 if it succeeds, or one of the following
  3565. * negative error codes:
  3566. *
  3567. * :macro:`NGTCP2_ERR_NOMEM`
  3568. * Out of memory.
  3569. */
  3570. NGTCP2_EXTERN int ngtcp2_conn_install_initial_key(
  3571. ngtcp2_conn *conn, const ngtcp2_crypto_aead_ctx *rx_aead_ctx,
  3572. const uint8_t *rx_iv, const ngtcp2_crypto_cipher_ctx *rx_hp_ctx,
  3573. const ngtcp2_crypto_aead_ctx *tx_aead_ctx, const uint8_t *tx_iv,
  3574. const ngtcp2_crypto_cipher_ctx *tx_hp_ctx, size_t ivlen);
  3575. /**
  3576. * @function
  3577. *
  3578. * `ngtcp2_conn_install_vneg_initial_key` installs packet protection
  3579. * keying materials for Initial packets on compatible version
  3580. * negotiation for |version|. |rx_aead_ctx| is AEAD cipher context
  3581. * object, and must be initialized with a decryption key. |rx_iv| is
  3582. * IV of length |rx_ivlen| for decryption. |rx_hp_ctx| is a packet
  3583. * header protection cipher context object for decryption. Similarly,
  3584. * |tx_aead_ctx|, |tx_iv| and |tx_hp_ctx| are for encrypting outgoing
  3585. * packets, and are the same length with the decryption counterpart.
  3586. * If they have already been set, they are overwritten.
  3587. *
  3588. * |ivlen| must be the minimum length of AEAD nonce, or 8 bytes if
  3589. * that is larger.
  3590. *
  3591. * If this function succeeds, |conn| takes ownership of |rx_aead_ctx|,
  3592. * |rx_hp_ctx|, |tx_aead_ctx|, and |tx_hp_ctx|.
  3593. * :member:`ngtcp2_callbacks.delete_crypto_aead_ctx` and
  3594. * :member:`ngtcp2_callbacks.delete_crypto_cipher_ctx` will be called
  3595. * to delete these objects when they are no longer used. If this
  3596. * function fails, the caller is responsible to delete them.
  3597. *
  3598. * This function returns 0 if it succeeds, or one of the following
  3599. * negative error codes:
  3600. *
  3601. * :macro:`NGTCP2_ERR_NOMEM`
  3602. * Out of memory.
  3603. */
  3604. NGTCP2_EXTERN int ngtcp2_conn_install_vneg_initial_key(
  3605. ngtcp2_conn *conn, uint32_t version,
  3606. const ngtcp2_crypto_aead_ctx *rx_aead_ctx, const uint8_t *rx_iv,
  3607. const ngtcp2_crypto_cipher_ctx *rx_hp_ctx,
  3608. const ngtcp2_crypto_aead_ctx *tx_aead_ctx, const uint8_t *tx_iv,
  3609. const ngtcp2_crypto_cipher_ctx *tx_hp_ctx, size_t ivlen);
  3610. /**
  3611. * @function
  3612. *
  3613. * `ngtcp2_conn_install_rx_handshake_key` installs packet protection
  3614. * keying materials for decrypting incoming Handshake packets.
  3615. * |aead_ctx| is AEAD cipher context object which must be initialized
  3616. * with a decryption key. |iv| is IV of length |ivlen|. |hp_ctx| is
  3617. * a packet header protection cipher context object.
  3618. *
  3619. * |ivlen| must be the minimum length of AEAD nonce, or 8 bytes if
  3620. * that is larger.
  3621. *
  3622. * If this function succeeds, |conn| takes ownership of |aead_ctx|,
  3623. * and |hp_ctx|. :member:`ngtcp2_callbacks.delete_crypto_aead_ctx`
  3624. * and :member:`ngtcp2_callbacks.delete_crypto_cipher_ctx` will be
  3625. * called to delete these objects when they are no longer used. If
  3626. * this function fails, the caller is responsible to delete them.
  3627. *
  3628. * This function returns 0 if it succeeds, or one of the following
  3629. * negative error codes:
  3630. *
  3631. * :macro:`NGTCP2_ERR_NOMEM`
  3632. * Out of memory.
  3633. */
  3634. NGTCP2_EXTERN int ngtcp2_conn_install_rx_handshake_key(
  3635. ngtcp2_conn *conn, const ngtcp2_crypto_aead_ctx *aead_ctx,
  3636. const uint8_t *iv, size_t ivlen, const ngtcp2_crypto_cipher_ctx *hp_ctx);
  3637. /**
  3638. * @function
  3639. *
  3640. * `ngtcp2_conn_install_tx_handshake_key` installs packet protection
  3641. * keying materials for encrypting outgoing Handshake packets.
  3642. * |aead_ctx| is AEAD cipher context object which must be initialized
  3643. * with an encryption key. |iv| is IV of length |ivlen|. |hp_ctx| is
  3644. * a packet header protection cipher context object.
  3645. *
  3646. * |ivlen| must be the minimum length of AEAD nonce, or 8 bytes if
  3647. * that is larger.
  3648. *
  3649. * If this function succeeds, |conn| takes ownership of |aead_ctx| and
  3650. * |hp_ctx|. :member:`ngtcp2_callbacks.delete_crypto_aead_ctx` and
  3651. * :member:`ngtcp2_callbacks.delete_crypto_cipher_ctx` will be called
  3652. * to delete these objects when they are no longer used. If this
  3653. * function fails, the caller is responsible to delete them.
  3654. *
  3655. * This function returns 0 if it succeeds, or one of the following
  3656. * negative error codes:
  3657. *
  3658. * :macro:`NGTCP2_ERR_NOMEM`
  3659. * Out of memory.
  3660. */
  3661. NGTCP2_EXTERN int ngtcp2_conn_install_tx_handshake_key(
  3662. ngtcp2_conn *conn, const ngtcp2_crypto_aead_ctx *aead_ctx,
  3663. const uint8_t *iv, size_t ivlen, const ngtcp2_crypto_cipher_ctx *hp_ctx);
  3664. /**
  3665. * @function
  3666. *
  3667. * `ngtcp2_conn_install_0rtt_key` installs packet protection AEAD
  3668. * cipher context object |aead_ctx|, IV |iv| of length |ivlen|, and
  3669. * packet header protection cipher context object |hp_ctx| to encrypt
  3670. * (for client) or decrypt (for server) 0-RTT packets.
  3671. *
  3672. * |ivlen| must be the minimum length of AEAD nonce, or 8 bytes if
  3673. * that is larger.
  3674. *
  3675. * If this function succeeds, |conn| takes ownership of |aead_ctx| and
  3676. * |hp_ctx|. :member:`ngtcp2_callbacks.delete_crypto_aead_ctx` and
  3677. * :member:`ngtcp2_callbacks.delete_crypto_cipher_ctx` will be called
  3678. * to delete these objects when they are no longer used. If this
  3679. * function fails, the caller is responsible to delete them.
  3680. *
  3681. * This function returns 0 if it succeeds, or one of the following
  3682. * negative error codes:
  3683. *
  3684. * :macro:`NGTCP2_ERR_NOMEM`
  3685. * Out of memory.
  3686. */
  3687. NGTCP2_EXTERN int ngtcp2_conn_install_0rtt_key(
  3688. ngtcp2_conn *conn, const ngtcp2_crypto_aead_ctx *aead_ctx,
  3689. const uint8_t *iv, size_t ivlen, const ngtcp2_crypto_cipher_ctx *hp_ctx);
  3690. /**
  3691. * @function
  3692. *
  3693. * `ngtcp2_conn_install_rx_key` installs packet protection keying
  3694. * materials for decrypting 1-RTT packets. |secret| of length
  3695. * |secretlen| is the decryption secret which is used to derive keying
  3696. * materials passed to this function. |aead_ctx| is AEAD cipher
  3697. * context object which must be initialized with a decryption key.
  3698. * |iv| is IV of length |ivlen|. |hp_ctx| is a packet header
  3699. * protection cipher context object.
  3700. *
  3701. * |ivlen| must be the minimum length of AEAD nonce, or 8 bytes if
  3702. * that is larger.
  3703. *
  3704. * If this function succeeds, |conn| takes ownership of |aead_ctx| and
  3705. * |hp_ctx|. :member:`ngtcp2_callbacks.delete_crypto_aead_ctx` and
  3706. * :member:`ngtcp2_callbacks.delete_crypto_cipher_ctx` will be called
  3707. * to delete these objects when they are no longer used. If this
  3708. * function fails, the caller is responsible to delete them.
  3709. *
  3710. * This function returns 0 if it succeeds, or one of the following
  3711. * negative error codes:
  3712. *
  3713. * :macro:`NGTCP2_ERR_NOMEM`
  3714. * Out of memory.
  3715. */
  3716. NGTCP2_EXTERN int ngtcp2_conn_install_rx_key(
  3717. ngtcp2_conn *conn, const uint8_t *secret, size_t secretlen,
  3718. const ngtcp2_crypto_aead_ctx *aead_ctx, const uint8_t *iv, size_t ivlen,
  3719. const ngtcp2_crypto_cipher_ctx *hp_ctx);
  3720. /**
  3721. * @function
  3722. *
  3723. * `ngtcp2_conn_install_tx_key` installs packet protection keying
  3724. * materials for encrypting 1-RTT packets. |secret| of length
  3725. * |secretlen| is the encryption secret which is used to derive keying
  3726. * materials passed to this function. |aead_ctx| is AEAD cipher
  3727. * context object which must be initialized with an encryption key.
  3728. * |iv| is IV of length |ivlen|. |hp_ctx| is a packet header
  3729. * protection cipher context object.
  3730. *
  3731. * |ivlen| must be the minimum length of AEAD nonce, or 8 bytes if
  3732. * that is larger.
  3733. *
  3734. * If this function succeeds, |conn| takes ownership of |aead_ctx| and
  3735. * |hp_ctx|. :member:`ngtcp2_callbacks.delete_crypto_aead_ctx` and
  3736. * :member:`ngtcp2_callbacks.delete_crypto_cipher_ctx` will be called
  3737. * to delete these objects when they are no longer used. If this
  3738. * function fails, the caller is responsible to delete them.
  3739. *
  3740. * This function returns 0 if it succeeds, or one of the following
  3741. * negative error codes:
  3742. *
  3743. * :macro:`NGTCP2_ERR_NOMEM`
  3744. * Out of memory.
  3745. */
  3746. NGTCP2_EXTERN int ngtcp2_conn_install_tx_key(
  3747. ngtcp2_conn *conn, const uint8_t *secret, size_t secretlen,
  3748. const ngtcp2_crypto_aead_ctx *aead_ctx, const uint8_t *iv, size_t ivlen,
  3749. const ngtcp2_crypto_cipher_ctx *hp_ctx);
  3750. /**
  3751. * @function
  3752. *
  3753. * `ngtcp2_conn_initiate_key_update` initiates the key update.
  3754. *
  3755. * This function returns 0 if it succeeds, or one of the following
  3756. * negative error codes:
  3757. *
  3758. * :macro:`NGTCP2_ERR_INVALID_STATE`
  3759. * The previous key update has not been confirmed yet; or key
  3760. * update is too frequent; or new keys are not available yet.
  3761. */
  3762. NGTCP2_EXTERN int ngtcp2_conn_initiate_key_update(ngtcp2_conn *conn,
  3763. ngtcp2_tstamp ts);
  3764. /**
  3765. * @function
  3766. *
  3767. * `ngtcp2_conn_set_tls_error` sets the TLS related error |liberr| in
  3768. * |conn|. |liberr| must be one of ngtcp2 library error codes (which
  3769. * is defined as NGTCP2_ERR_* macro, such as
  3770. * :macro:`NGTCP2_ERR_DECRYPT`). In general, error code should be
  3771. * propagated via return value, but sometimes ngtcp2 API is called
  3772. * inside callback function of TLS stack, and it does not allow to
  3773. * return ngtcp2 error code directly. In this case, implementation
  3774. * can set the error code (e.g.,
  3775. * :macro:`NGTCP2_ERR_MALFORMED_TRANSPORT_PARAM`) using this function.
  3776. *
  3777. * See also `ngtcp2_conn_get_tls_error`.
  3778. */
  3779. NGTCP2_EXTERN void ngtcp2_conn_set_tls_error(ngtcp2_conn *conn, int liberr);
  3780. /**
  3781. * @function
  3782. *
  3783. * `ngtcp2_conn_get_tls_error` returns the value set by
  3784. * `ngtcp2_conn_set_tls_error`. If no value is set, this function
  3785. * returns 0.
  3786. */
  3787. NGTCP2_EXTERN int ngtcp2_conn_get_tls_error(ngtcp2_conn *conn);
  3788. /**
  3789. * @function
  3790. *
  3791. * `ngtcp2_conn_set_tls_alert` sets a TLS alert |alert| generated by a
  3792. * TLS stack of a local endpoint to |conn|.
  3793. *
  3794. * See also `ngtcp2_conn_get_tls_alert`.
  3795. */
  3796. NGTCP2_EXTERN void ngtcp2_conn_set_tls_alert(ngtcp2_conn *conn, uint8_t alert);
  3797. /**
  3798. * @function
  3799. *
  3800. * `ngtcp2_conn_get_tls_alert` returns the value set by
  3801. * `ngtcp2_conn_set_tls_alert`. If no value is set, this function
  3802. * returns 0.
  3803. */
  3804. NGTCP2_EXTERN uint8_t ngtcp2_conn_get_tls_alert(ngtcp2_conn *conn);
  3805. /**
  3806. * @function
  3807. *
  3808. * `ngtcp2_conn_set_keep_alive_timeout` sets keep-alive timeout. If
  3809. * nonzero value is given, after a connection is idle at least in a
  3810. * given amount of time, a keep-alive packet is sent. If UINT64_MAX
  3811. * is set, keep-alive functionality is disabled, and this is the
  3812. * default. Specifying 0 in |timeout| is reserved for a future
  3813. * extension, and for now it is treated as if UINT64_MAX is given.
  3814. */
  3815. NGTCP2_EXTERN void ngtcp2_conn_set_keep_alive_timeout(ngtcp2_conn *conn,
  3816. ngtcp2_duration timeout);
  3817. /**
  3818. * @function
  3819. *
  3820. * `ngtcp2_conn_get_expiry` returns the next expiry time. It returns
  3821. * ``UINT64_MAX`` if there is no next expiry.
  3822. *
  3823. * Call `ngtcp2_conn_handle_expiry` and then
  3824. * `ngtcp2_conn_writev_stream` (or `ngtcp2_conn_writev_datagram`) when
  3825. * the expiry time has passed.
  3826. */
  3827. NGTCP2_EXTERN ngtcp2_tstamp ngtcp2_conn_get_expiry(ngtcp2_conn *conn);
  3828. /**
  3829. * @function
  3830. *
  3831. * `ngtcp2_conn_handle_expiry` handles expired timer.
  3832. */
  3833. NGTCP2_EXTERN int ngtcp2_conn_handle_expiry(ngtcp2_conn *conn,
  3834. ngtcp2_tstamp ts);
  3835. /**
  3836. * @function
  3837. *
  3838. * `ngtcp2_conn_get_pto` returns Probe Timeout (PTO).
  3839. */
  3840. NGTCP2_EXTERN ngtcp2_duration ngtcp2_conn_get_pto(ngtcp2_conn *conn);
  3841. /**
  3842. * @function
  3843. *
  3844. * `ngtcp2_conn_decode_and_set_remote_transport_params` decodes QUIC
  3845. * transport parameters from the buffer pointed by |data| of length
  3846. * |datalen|, and sets the result to |conn|.
  3847. *
  3848. * This function returns 0 if it succeeds, or one of the following
  3849. * negative error codes:
  3850. *
  3851. * :macro:`NGTCP2_ERR_REQUIRED_TRANSPORT_PARAM`
  3852. * The required parameter is missing.
  3853. * :macro:`NGTCP2_ERR_MALFORMED_TRANSPORT_PARAM`
  3854. * The input is malformed.
  3855. * :macro:`NGTCP2_ERR_TRANSPORT_PARAM`
  3856. * Failed to validate the remote QUIC transport parameters.
  3857. * :macro:`NGTCP2_ERR_VERSION_NEGOTIATION_FAILURE`
  3858. * Version negotiation failure.
  3859. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE`
  3860. * User callback failed
  3861. */
  3862. NGTCP2_EXTERN int ngtcp2_conn_decode_and_set_remote_transport_params(
  3863. ngtcp2_conn *conn, const uint8_t *data, size_t datalen);
  3864. /**
  3865. * @function
  3866. *
  3867. * `ngtcp2_conn_get_remote_transport_params` returns a pointer to the
  3868. * remote QUIC transport parameters. If no remote transport
  3869. * parameters are set, it returns NULL.
  3870. */
  3871. NGTCP2_EXTERN const ngtcp2_transport_params *
  3872. ngtcp2_conn_get_remote_transport_params(ngtcp2_conn *conn);
  3873. /**
  3874. * @function
  3875. *
  3876. * `ngtcp2_conn_encode_0rtt_transport_params` encodes the QUIC
  3877. * transport parameters that are used for 0-RTT data in the buffer
  3878. * pointed by |dest| of length |destlen|. It includes at least the
  3879. * following fields:
  3880. *
  3881. * - :member:`ngtcp2_transport_params.initial_max_streams_bidi`
  3882. * - :member:`ngtcp2_transport_params.initial_max_streams_uni`
  3883. * - :member:`ngtcp2_transport_params.initial_max_stream_data_bidi_local`
  3884. * - :member:`ngtcp2_transport_params.initial_max_stream_data_bidi_remote`
  3885. * - :member:`ngtcp2_transport_params.initial_max_stream_data_uni`
  3886. * - :member:`ngtcp2_transport_params.initial_max_data`
  3887. * - :member:`ngtcp2_transport_params.active_connection_id_limit`
  3888. * - :member:`ngtcp2_transport_params.max_datagram_frame_size`
  3889. *
  3890. * If |conn| is initialized as server, the following additional fields
  3891. * are also included:
  3892. *
  3893. * - :member:`ngtcp2_transport_params.max_idle_timeout`
  3894. * - :member:`ngtcp2_transport_params.max_udp_payload_size`
  3895. * - :member:`ngtcp2_transport_params.disable_active_migration`
  3896. *
  3897. * If |conn| is initialized as client, these parameters are
  3898. * synthesized from the remote transport parameters received from
  3899. * server. Otherwise, it is the local transport parameters that are
  3900. * set by the local endpoint.
  3901. *
  3902. * This function returns the number of bytes written, or one of the
  3903. * following negative error codes:
  3904. *
  3905. * :macro:`NGTCP2_ERR_NOBUF`
  3906. * Buffer is too small.
  3907. */
  3908. NGTCP2_EXTERN
  3909. ngtcp2_ssize ngtcp2_conn_encode_0rtt_transport_params(ngtcp2_conn *conn,
  3910. uint8_t *dest,
  3911. size_t destlen);
  3912. /**
  3913. * @function
  3914. *
  3915. * `ngtcp2_conn_decode_and_set_0rtt_transport_params` decodes QUIC
  3916. * transport parameters from |data| of length |datalen|, which is
  3917. * assumed to be the parameters received from the server in the
  3918. * previous connection, and sets it to |conn|. These parameters are
  3919. * used to send 0-RTT data. QUIC requires that client application
  3920. * should remember transport parameters along with a session ticket.
  3921. *
  3922. * At least following fields should be included:
  3923. *
  3924. * - :member:`ngtcp2_transport_params.initial_max_streams_bidi`
  3925. * - :member:`ngtcp2_transport_params.initial_max_streams_uni`
  3926. * - :member:`ngtcp2_transport_params.initial_max_stream_data_bidi_local`
  3927. * - :member:`ngtcp2_transport_params.initial_max_stream_data_bidi_remote`
  3928. * - :member:`ngtcp2_transport_params.initial_max_stream_data_uni`
  3929. * - :member:`ngtcp2_transport_params.initial_max_data`
  3930. * - :member:`ngtcp2_transport_params.active_connection_id_limit`
  3931. * - :member:`ngtcp2_transport_params.max_datagram_frame_size` (if
  3932. * DATAGRAM extension was negotiated)
  3933. *
  3934. * This function must only be used by client.
  3935. *
  3936. * This function returns 0 if it succeeds, or one of the following
  3937. * negative error codes:
  3938. *
  3939. * :macro:`NGTCP2_ERR_NOMEM`
  3940. * Out of memory.
  3941. * :macro:`NGTCP2_ERR_MALFORMED_TRANSPORT_PARAM`
  3942. * The input is malformed.
  3943. */
  3944. NGTCP2_EXTERN int ngtcp2_conn_decode_and_set_0rtt_transport_params(
  3945. ngtcp2_conn *conn, const uint8_t *data, size_t datalen);
  3946. /**
  3947. * @function
  3948. *
  3949. * `ngtcp2_conn_set_local_transport_params` sets the local transport
  3950. * parameters |params|. This function can only be called by server.
  3951. * Although the local transport parameters are passed to
  3952. * `ngtcp2_conn_server_new`, server might want to update them after
  3953. * ALPN is chosen. In that case, server can update the transport
  3954. * parameters with this function. Server must call this function
  3955. * before calling `ngtcp2_conn_install_tx_handshake_key`.
  3956. *
  3957. * This function returns 0 if it succeeds, or one of the following
  3958. * negative error codes:
  3959. *
  3960. * :macro:`NGTCP2_ERR_INVALID_STATE`
  3961. * `ngtcp2_conn_install_tx_handshake_key` has been called.
  3962. */
  3963. NGTCP2_EXTERN int ngtcp2_conn_set_local_transport_params_versioned(
  3964. ngtcp2_conn *conn, int transport_params_version,
  3965. const ngtcp2_transport_params *params);
  3966. /**
  3967. * @function
  3968. *
  3969. * `ngtcp2_conn_get_local_transport_params` returns a pointer to the
  3970. * local QUIC transport parameters.
  3971. */
  3972. NGTCP2_EXTERN const ngtcp2_transport_params *
  3973. ngtcp2_conn_get_local_transport_params(ngtcp2_conn *conn);
  3974. /**
  3975. * @function
  3976. *
  3977. * `ngtcp2_conn_encode_local_transport_params` encodes the local QUIC
  3978. * transport parameters in |dest| of length |destlen|.
  3979. *
  3980. * This function returns the number of bytes written, or one of the
  3981. * following negative error codes:
  3982. *
  3983. * :macro:`NGTCP2_ERR_NOBUF`
  3984. * Buffer is too small.
  3985. */
  3986. NGTCP2_EXTERN ngtcp2_ssize ngtcp2_conn_encode_local_transport_params(
  3987. ngtcp2_conn *conn, uint8_t *dest, size_t destlen);
  3988. /**
  3989. * @function
  3990. *
  3991. * `ngtcp2_conn_open_bidi_stream` opens new bidirectional stream. The
  3992. * |stream_user_data| is the user data specific to the stream. The
  3993. * stream ID of the opened stream is stored in |*pstream_id|.
  3994. *
  3995. * Application can call this function before handshake completes. For
  3996. * 0-RTT packet, application can call this function after calling
  3997. * `ngtcp2_conn_decode_and_set_0rtt_transport_params`. For 1-RTT
  3998. * packet, application can call this function after calling
  3999. * `ngtcp2_conn_decode_and_set_remote_transport_params` and
  4000. * `ngtcp2_conn_install_tx_key`. If ngtcp2 crypto support library is
  4001. * used, application can call this function after calling
  4002. * `ngtcp2_crypto_derive_and_install_tx_key` for 1-RTT packet.
  4003. *
  4004. * This function returns 0 if it succeeds, or one of the following
  4005. * negative error codes:
  4006. *
  4007. * :macro:`NGTCP2_ERR_NOMEM`
  4008. * Out of memory
  4009. * :macro:`NGTCP2_ERR_STREAM_ID_BLOCKED`
  4010. * The remote endpoint does not allow |stream_id| yet.
  4011. */
  4012. NGTCP2_EXTERN int ngtcp2_conn_open_bidi_stream(ngtcp2_conn *conn,
  4013. int64_t *pstream_id,
  4014. void *stream_user_data);
  4015. /**
  4016. * @function
  4017. *
  4018. * `ngtcp2_conn_open_uni_stream` opens new unidirectional stream. The
  4019. * |stream_user_data| is the user data specific to the stream. The
  4020. * stream ID of the opened stream is stored in |*pstream_id|.
  4021. *
  4022. * Application can call this function before handshake completes. For
  4023. * 0-RTT packet, application can call this function after calling
  4024. * `ngtcp2_conn_decode_and_set_0rtt_transport_params`. For 1-RTT
  4025. * packet, application can call this function after calling
  4026. * `ngtcp2_conn_decode_and_set_remote_transport_params` and
  4027. * `ngtcp2_conn_install_tx_key`. If ngtcp2 crypto support library is
  4028. * used, application can call this function after calling
  4029. * `ngtcp2_crypto_derive_and_install_tx_key` for 1-RTT packet.
  4030. *
  4031. * This function returns 0 if it succeeds, or one of the following
  4032. * negative error codes:
  4033. *
  4034. * :macro:`NGTCP2_ERR_NOMEM`
  4035. * Out of memory
  4036. * :macro:`NGTCP2_ERR_STREAM_ID_BLOCKED`
  4037. * The remote endpoint does not allow |stream_id| yet.
  4038. */
  4039. NGTCP2_EXTERN int ngtcp2_conn_open_uni_stream(ngtcp2_conn *conn,
  4040. int64_t *pstream_id,
  4041. void *stream_user_data);
  4042. /**
  4043. * @function
  4044. *
  4045. * `ngtcp2_conn_shutdown_stream` closes a stream denoted by
  4046. * |stream_id| abruptly. |app_error_code| is one of application error
  4047. * codes, and indicates the reason of shutdown. Successful call of
  4048. * this function does not immediately erase the state of the stream.
  4049. * The actual deletion is done when the remote endpoint sends
  4050. * acknowledgement. Calling this function is equivalent to call
  4051. * `ngtcp2_conn_shutdown_stream_read`, and
  4052. * `ngtcp2_conn_shutdown_stream_write` sequentially with the following
  4053. * differences. If |stream_id| refers to a local unidirectional
  4054. * stream, this function only shutdowns write side of the stream. If
  4055. * |stream_id| refers to a remote unidirectional stream, this function
  4056. * only shutdowns read side of the stream.
  4057. *
  4058. * |flags| is currently unused, and should be set to 0.
  4059. *
  4060. * This function returns 0 if a stream denoted by |stream_id| is not
  4061. * found.
  4062. *
  4063. * This function returns 0 if it succeeds, or one of the following
  4064. * negative error codes:
  4065. *
  4066. * :macro:`NGTCP2_ERR_NOMEM`
  4067. * Out of memory
  4068. */
  4069. NGTCP2_EXTERN int ngtcp2_conn_shutdown_stream(ngtcp2_conn *conn, uint32_t flags,
  4070. int64_t stream_id,
  4071. uint64_t app_error_code);
  4072. /**
  4073. * @function
  4074. *
  4075. * `ngtcp2_conn_shutdown_stream_write` closes write-side of a stream
  4076. * denoted by |stream_id| abruptly. |app_error_code| is one of
  4077. * application error codes, and indicates the reason of shutdown. If
  4078. * this function succeeds, no further application data is sent to the
  4079. * remote endpoint. It discards all data which has not been
  4080. * acknowledged yet.
  4081. *
  4082. * |flags| is currently unused, and should be set to 0.
  4083. *
  4084. * This function returns 0 if a stream denoted by |stream_id| is not
  4085. * found.
  4086. *
  4087. * This function returns 0 if it succeeds, or one of the following
  4088. * negative error codes:
  4089. *
  4090. * :macro:`NGTCP2_ERR_NOMEM`
  4091. * Out of memory
  4092. * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
  4093. * |stream_id| refers to a remote unidirectional stream.
  4094. */
  4095. NGTCP2_EXTERN int ngtcp2_conn_shutdown_stream_write(ngtcp2_conn *conn,
  4096. uint32_t flags,
  4097. int64_t stream_id,
  4098. uint64_t app_error_code);
  4099. /**
  4100. * @function
  4101. *
  4102. * `ngtcp2_conn_shutdown_stream_read` closes read-side of a stream
  4103. * denoted by |stream_id| abruptly. |app_error_code| is one of
  4104. * application error codes, and indicates the reason of shutdown. If
  4105. * this function succeeds, no further application data is forwarded to
  4106. * an application layer.
  4107. *
  4108. * |flags| is currently unused, and should be set to 0.
  4109. *
  4110. * This function returns 0 if a stream denoted by |stream_id| is not
  4111. * found.
  4112. *
  4113. * This function returns 0 if it succeeds, or one of the following
  4114. * negative error codes:
  4115. *
  4116. * :macro:`NGTCP2_ERR_NOMEM`
  4117. * Out of memory
  4118. * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
  4119. * |stream_id| refers to a local unidirectional stream.
  4120. */
  4121. NGTCP2_EXTERN int ngtcp2_conn_shutdown_stream_read(ngtcp2_conn *conn,
  4122. uint32_t flags,
  4123. int64_t stream_id,
  4124. uint64_t app_error_code);
  4125. /**
  4126. * @macrosection
  4127. *
  4128. * Write stream data flags
  4129. */
  4130. /**
  4131. * @macro
  4132. *
  4133. * :macro:`NGTCP2_WRITE_STREAM_FLAG_NONE` indicates no flag set.
  4134. */
  4135. #define NGTCP2_WRITE_STREAM_FLAG_NONE 0x00u
  4136. /**
  4137. * @macro
  4138. *
  4139. * :macro:`NGTCP2_WRITE_STREAM_FLAG_MORE` indicates that more data may
  4140. * come, and should be coalesced into the same packet if possible.
  4141. */
  4142. #define NGTCP2_WRITE_STREAM_FLAG_MORE 0x01u
  4143. /**
  4144. * @macro
  4145. *
  4146. * :macro:`NGTCP2_WRITE_STREAM_FLAG_FIN` indicates that a passed data
  4147. * is the final part of a stream.
  4148. */
  4149. #define NGTCP2_WRITE_STREAM_FLAG_FIN 0x02u
  4150. /**
  4151. * @function
  4152. *
  4153. * `ngtcp2_conn_write_stream` is just like
  4154. * `ngtcp2_conn_writev_stream`. The only difference is that it
  4155. * conveniently accepts a single buffer.
  4156. */
  4157. NGTCP2_EXTERN ngtcp2_ssize ngtcp2_conn_write_stream_versioned(
  4158. ngtcp2_conn *conn, ngtcp2_path *path, int pkt_info_version,
  4159. ngtcp2_pkt_info *pi, uint8_t *dest, size_t destlen, ngtcp2_ssize *pdatalen,
  4160. uint32_t flags, int64_t stream_id, const uint8_t *data, size_t datalen,
  4161. ngtcp2_tstamp ts);
  4162. /**
  4163. * @function
  4164. *
  4165. * `ngtcp2_conn_writev_stream` writes a packet containing stream data
  4166. * of a stream denoted by |stream_id|. The buffer of the packet is
  4167. * pointed by |dest| of length |destlen|. This function performs QUIC
  4168. * handshake as well.
  4169. *
  4170. * |destlen| should be at least
  4171. * :member:`ngtcp2_settings.max_tx_udp_payload_size`. It must be at
  4172. * least :macro:`NGTCP2_MAX_UDP_PAYLOAD_SIZE`.
  4173. *
  4174. * Specifying -1 to |stream_id| means no new stream data to send.
  4175. *
  4176. * If |path| is not ``NULL``, this function stores the network path
  4177. * with which the packet should be sent. Each addr field
  4178. * (:member:`ngtcp2_path.local` and :member:`ngtcp2_path.remote`) must
  4179. * point to the buffer which should be at least
  4180. * sizeof(:type:`sockaddr_union`) bytes long. The assignment might
  4181. * not be done if nothing is written to |dest|.
  4182. *
  4183. * If |pi| is not ``NULL``, this function stores packet metadata in it
  4184. * if it succeeds. The metadata includes ECN markings. When calling
  4185. * this function again after it returns
  4186. * :macro:`NGTCP2_ERR_WRITE_MORE`, caller must pass the same |pi| to
  4187. * this function.
  4188. *
  4189. * Stream data is specified as vector of data |datav|. |datavcnt|
  4190. * specifies the number of :type:`ngtcp2_vec` that |datav| includes.
  4191. *
  4192. * If all given data is encoded as STREAM frame in |dest|, and if
  4193. * |flags| & :macro:`NGTCP2_WRITE_STREAM_FLAG_FIN` is nonzero, fin
  4194. * flag is set to outgoing STREAM frame. Otherwise, fin flag in
  4195. * STREAM frame is not set.
  4196. *
  4197. * This packet may contain frames other than STREAM frame. The packet
  4198. * might not contain STREAM frame if other frames occupy the packet.
  4199. * In that case, |*pdatalen| would be -1 if |pdatalen| is not
  4200. * ``NULL``.
  4201. *
  4202. * Empty data is treated specially, and it is only accepted if no
  4203. * data, including the empty data, is submitted to a stream or
  4204. * :macro:`NGTCP2_WRITE_STREAM_FLAG_FIN` is set in |flags|. If 0
  4205. * length STREAM frame is successfully serialized, |*pdatalen| would
  4206. * be 0.
  4207. *
  4208. * The number of data encoded in STREAM frame is stored in |*pdatalen|
  4209. * if it is not ``NULL``. The caller must keep the portion of data
  4210. * covered by |*pdatalen| bytes in tact until
  4211. * :member:`ngtcp2_callbacks.acked_stream_data_offset` indicates that
  4212. * they are acknowledged by a remote endpoint or the stream is closed.
  4213. *
  4214. * If the given stream data is small (e.g., few bytes), the packet
  4215. * might be severely under filled. Too many small packet might
  4216. * increase overall packet processing costs. Unless there are
  4217. * retransmissions, by default, application can only send 1 STREAM
  4218. * frame in one QUIC packet. In order to include more than 1 STREAM
  4219. * frame in one QUIC packet, specify
  4220. * :macro:`NGTCP2_WRITE_STREAM_FLAG_MORE` in |flags|. This is
  4221. * analogous to ``MSG_MORE`` flag in :manpage:`send(2)`. If the
  4222. * :macro:`NGTCP2_WRITE_STREAM_FLAG_MORE` is used, there are 4
  4223. * outcomes:
  4224. *
  4225. * - The function returns the written length of packet just like
  4226. * without :macro:`NGTCP2_WRITE_STREAM_FLAG_MORE`. This is because
  4227. * packet is nearly full, and the library decided to make a complete
  4228. * packet. |*pdatalen| might be -1 or >= 0. It may return 0 which
  4229. * indicates that no packet transmission is possible at the moment
  4230. * for some reason.
  4231. *
  4232. * - The function returns :macro:`NGTCP2_ERR_WRITE_MORE`. In this
  4233. * case, |*pdatalen| >= 0 is asserted. It indicates that
  4234. * application can still call this function with different stream
  4235. * data (or `ngtcp2_conn_writev_datagram` if it has data to send in
  4236. * unreliable datagram) to pack them into the same packet.
  4237. * Application has to specify the same |conn|, |path|, |pi|, |dest|,
  4238. * |destlen|, and |ts| parameters, otherwise the behaviour is
  4239. * undefined. The application can change |flags|.
  4240. *
  4241. * - The function returns one of the following negative error codes:
  4242. * :macro:`NGTCP2_ERR_STREAM_DATA_BLOCKED`,
  4243. * :macro:`NGTCP2_ERR_STREAM_NOT_FOUND`, or
  4244. * :macro:`NGTCP2_ERR_STREAM_SHUT_WR`. In this case, |*pdatalen| ==
  4245. * -1 is asserted. Application can still write the stream data of
  4246. * the other streams by calling this function (or
  4247. * `ngtcp2_conn_writev_datagram` if it has data to send in
  4248. * unreliable datagram) to pack them into the same packet.
  4249. * Application has to specify the same |conn|, |path|, |pi|, |dest|,
  4250. * |destlen|, and |ts| parameters, otherwise the behaviour is
  4251. * undefined. The application can change |flags|.
  4252. *
  4253. * - The other negative error codes might be returned just like
  4254. * without :macro:`NGTCP2_WRITE_STREAM_FLAG_MORE`. These errors
  4255. * should be treated as a connection error.
  4256. *
  4257. * When application uses :macro:`NGTCP2_WRITE_STREAM_FLAG_MORE` at
  4258. * least once, it must not call other ngtcp2 API functions
  4259. * (application can still call `ngtcp2_conn_write_connection_close` to
  4260. * handle error from this function. It can also call
  4261. * `ngtcp2_conn_shutdown_stream_read`,
  4262. * `ngtcp2_conn_shutdown_stream_write`, and
  4263. * `ngtcp2_conn_shutdown_stream`), just keep calling this function (or
  4264. * `ngtcp2_conn_writev_datagram`) until it returns 0, a positive
  4265. * number (which indicates a complete packet is ready), or the error
  4266. * codes other than :macro:`NGTCP2_ERR_WRITE_MORE`,
  4267. * :macro:`NGTCP2_ERR_STREAM_DATA_BLOCKED`,
  4268. * :macro:`NGTCP2_ERR_STREAM_NOT_FOUND`, and
  4269. * :macro:`NGTCP2_ERR_STREAM_SHUT_WR`. If there is no stream data to
  4270. * include, call this function with |stream_id| as -1 to stop
  4271. * coalescing and write a packet.
  4272. *
  4273. * This function returns 0 if it cannot write any frame because buffer
  4274. * is too small, or packet is congestion limited. Application should
  4275. * keep reading and wait for congestion window to grow.
  4276. *
  4277. * This function must not be called from inside the callback
  4278. * functions.
  4279. *
  4280. * `ngtcp2_conn_update_pkt_tx_time` must be called after this
  4281. * function. Application may call this function multiple times before
  4282. * calling `ngtcp2_conn_update_pkt_tx_time`.
  4283. *
  4284. * This function returns the number of bytes written in |dest| if it
  4285. * succeeds, or one of the following negative error codes:
  4286. *
  4287. * :macro:`NGTCP2_ERR_NOMEM`
  4288. * Out of memory
  4289. * :macro:`NGTCP2_ERR_STREAM_NOT_FOUND`
  4290. * Stream does not exist
  4291. * :macro:`NGTCP2_ERR_STREAM_SHUT_WR`
  4292. * Stream is half closed (local); or stream is being reset.
  4293. * :macro:`NGTCP2_ERR_PKT_NUM_EXHAUSTED`
  4294. * Packet number is exhausted, and cannot send any more packet.
  4295. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE`
  4296. * User callback failed
  4297. * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
  4298. * The total length of stream data is too large.
  4299. * :macro:`NGTCP2_ERR_STREAM_DATA_BLOCKED`
  4300. * Stream is blocked because of flow control.
  4301. * :macro:`NGTCP2_ERR_WRITE_MORE`
  4302. * (Only when :macro:`NGTCP2_WRITE_STREAM_FLAG_MORE` is specified)
  4303. * Application can call this function to pack more stream data
  4304. * into the same packet. See above to know how it works.
  4305. *
  4306. * If any other negative error is returned, call
  4307. * `ngtcp2_conn_write_connection_close` to get terminal packet, and
  4308. * sending it makes QUIC connection enter the closing state.
  4309. */
  4310. NGTCP2_EXTERN ngtcp2_ssize ngtcp2_conn_writev_stream_versioned(
  4311. ngtcp2_conn *conn, ngtcp2_path *path, int pkt_info_version,
  4312. ngtcp2_pkt_info *pi, uint8_t *dest, size_t destlen, ngtcp2_ssize *pdatalen,
  4313. uint32_t flags, int64_t stream_id, const ngtcp2_vec *datav, size_t datavcnt,
  4314. ngtcp2_tstamp ts);
  4315. /**
  4316. * @macrosection
  4317. *
  4318. * Write datagram flags
  4319. */
  4320. /**
  4321. * @macro
  4322. *
  4323. * :macro:`NGTCP2_WRITE_DATAGRAM_FLAG_NONE` indicates no flag set.
  4324. */
  4325. #define NGTCP2_WRITE_DATAGRAM_FLAG_NONE 0x00u
  4326. /**
  4327. * @macro
  4328. *
  4329. * :macro:`NGTCP2_WRITE_DATAGRAM_FLAG_MORE` indicates that more data
  4330. * may come, and should be coalesced into the same packet if possible.
  4331. */
  4332. #define NGTCP2_WRITE_DATAGRAM_FLAG_MORE 0x01u
  4333. /**
  4334. * @function
  4335. *
  4336. * `ngtcp2_conn_write_datagram` is just like
  4337. * `ngtcp2_conn_writev_datagram`. The only difference is that it
  4338. * conveniently accepts a single buffer.
  4339. */
  4340. NGTCP2_EXTERN ngtcp2_ssize ngtcp2_conn_write_datagram_versioned(
  4341. ngtcp2_conn *conn, ngtcp2_path *path, int pkt_info_version,
  4342. ngtcp2_pkt_info *pi, uint8_t *dest, size_t destlen, int *paccepted,
  4343. uint32_t flags, uint64_t dgram_id, const uint8_t *data, size_t datalen,
  4344. ngtcp2_tstamp ts);
  4345. /**
  4346. * @function
  4347. *
  4348. * `ngtcp2_conn_writev_datagram` writes a packet containing unreliable
  4349. * data in DATAGRAM frame. The buffer of the packet is pointed by
  4350. * |dest| of length |destlen|. This function performs QUIC handshake
  4351. * as well.
  4352. *
  4353. * |destlen| should be at least
  4354. * :member:`ngtcp2_settings.max_tx_udp_payload_size`. It must be at
  4355. * least :macro:`NGTCP2_MAX_UDP_PAYLOAD_SIZE`.
  4356. *
  4357. * For |path| and |pi| parameters, refer to
  4358. * `ngtcp2_conn_writev_stream`.
  4359. *
  4360. * Stream data is specified as vector of data |datav|. |datavcnt|
  4361. * specifies the number of :type:`ngtcp2_vec` that |datav| includes.
  4362. *
  4363. * If the given data is written to the buffer, nonzero value is
  4364. * assigned to |*paccepted| if it is not NULL. The data in DATAGRAM
  4365. * frame cannot be fragmented; writing partial data is not possible.
  4366. *
  4367. * |dgram_id| is an opaque identifier which should uniquely identify
  4368. * the given DATAGRAM data. It is passed to
  4369. * :member:`ngtcp2_callbacks.ack_datagram` callback when a packet that
  4370. * contains DATAGRAM frame is acknowledged. It is also passed to
  4371. * :member:`ngtcp2_callbacks.lost_datagram` callback when a packet
  4372. * that contains DATAGRAM frame is declared lost. If an application
  4373. * uses neither of those callbacks, it can sets 0 to this parameter.
  4374. *
  4375. * This function might write other frames other than DATAGRAM frame,
  4376. * just like `ngtcp2_conn_writev_stream`.
  4377. *
  4378. * If the function returns 0, it means that no more data cannot be
  4379. * sent because of congestion control limit; or, data does not fit
  4380. * into the provided buffer; or, a local endpoint, as a server, is
  4381. * unable to send data because of its amplification limit. In this
  4382. * case, |*paccepted| is assigned zero if it is not NULL.
  4383. *
  4384. * If :macro:`NGTCP2_WRITE_DATAGRAM_FLAG_MORE` is set in |flags|,
  4385. * there are 3 outcomes:
  4386. *
  4387. * - The function returns the written length of packet just like
  4388. * without :macro:`NGTCP2_WRITE_DATAGRAM_FLAG_MORE`. This is
  4389. * because packet is nearly full and the library decided to make a
  4390. * complete packet. |*paccepted| might be zero or nonzero.
  4391. *
  4392. * - The function returns :macro:`NGTCP2_ERR_WRITE_MORE`. In this
  4393. * case, |*paccepted| != 0 is asserted. This indicates that
  4394. * application can call this function with another unreliable data
  4395. * (or `ngtcp2_conn_writev_stream` if it has stream data to send) to
  4396. * pack them into the same packet. Application has to specify the
  4397. * same |conn|, |path|, |pi|, |dest|, |destlen|, and |ts|
  4398. * parameters, otherwise the behaviour is undefined. The
  4399. * application can change |flags|.
  4400. *
  4401. * - The other error might be returned just like without
  4402. * :macro:`NGTCP2_WRITE_DATAGRAM_FLAG_MORE`.
  4403. *
  4404. * When application sees :macro:`NGTCP2_ERR_WRITE_MORE`, it must not
  4405. * call other ngtcp2 API functions (application can still call
  4406. * `ngtcp2_conn_write_connection_close` to handle error from this
  4407. * function. It can also call `ngtcp2_conn_shutdown_stream_read`,
  4408. * `ngtcp2_conn_shutdown_stream_write`, and
  4409. * `ngtcp2_conn_shutdown_stream`). Just keep calling this function
  4410. * (or `ngtcp2_conn_writev_stream`) until it returns a positive number
  4411. * (which indicates a complete packet is ready).
  4412. *
  4413. * This function returns the number of bytes written in |dest| if it
  4414. * succeeds, or one of the following negative error codes:
  4415. *
  4416. * :macro:`NGTCP2_ERR_NOMEM`
  4417. * Out of memory
  4418. * :macro:`NGTCP2_ERR_PKT_NUM_EXHAUSTED`
  4419. * Packet number is exhausted, and cannot send any more packet.
  4420. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE`
  4421. * User callback failed
  4422. * :macro:`NGTCP2_ERR_WRITE_MORE`
  4423. * (Only when :macro:`NGTCP2_WRITE_DATAGRAM_FLAG_MORE` is
  4424. * specified) Application can call this function to pack more data
  4425. * into the same packet. See above to know how it works.
  4426. * :macro:`NGTCP2_ERR_INVALID_STATE`
  4427. * A remote endpoint did not express the DATAGRAM frame support.
  4428. * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
  4429. * The provisional DATAGRAM frame size exceeds the maximum
  4430. * DATAGRAM frame size that a remote endpoint can receive.
  4431. *
  4432. * If any other negative error is returned, call
  4433. * `ngtcp2_conn_write_connection_close` to get terminal packet, and
  4434. * sending it makes QUIC connection enter the closing state.
  4435. */
  4436. NGTCP2_EXTERN ngtcp2_ssize ngtcp2_conn_writev_datagram_versioned(
  4437. ngtcp2_conn *conn, ngtcp2_path *path, int pkt_info_version,
  4438. ngtcp2_pkt_info *pi, uint8_t *dest, size_t destlen, int *paccepted,
  4439. uint32_t flags, uint64_t dgram_id, const ngtcp2_vec *datav, size_t datavcnt,
  4440. ngtcp2_tstamp ts);
  4441. /**
  4442. * @function
  4443. *
  4444. * `ngtcp2_conn_in_closing_period` returns nonzero if |conn| is in the
  4445. * closing period.
  4446. */
  4447. NGTCP2_EXTERN int ngtcp2_conn_in_closing_period(ngtcp2_conn *conn);
  4448. /**
  4449. * @function
  4450. *
  4451. * `ngtcp2_conn_in_draining_period` returns nonzero if |conn| is in
  4452. * the draining period.
  4453. */
  4454. NGTCP2_EXTERN int ngtcp2_conn_in_draining_period(ngtcp2_conn *conn);
  4455. /**
  4456. * @function
  4457. *
  4458. * `ngtcp2_conn_extend_max_stream_offset` extends the maximum stream
  4459. * data that a remote endpoint can send by |datalen|. |stream_id|
  4460. * specifies the stream ID. This function only extends stream-level
  4461. * flow control window.
  4462. *
  4463. * This function returns 0 if a stream denoted by |stream_id| is not
  4464. * found.
  4465. *
  4466. * This function returns 0 if it succeeds, or one of the following
  4467. * negative error codes:
  4468. *
  4469. * :macro:`NGTCP2_ERR_NOMEM`
  4470. * Out of memory.
  4471. * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
  4472. * |stream_id| refers to a local unidirectional stream.
  4473. */
  4474. NGTCP2_EXTERN int ngtcp2_conn_extend_max_stream_offset(ngtcp2_conn *conn,
  4475. int64_t stream_id,
  4476. uint64_t datalen);
  4477. /**
  4478. * @function
  4479. *
  4480. * `ngtcp2_conn_extend_max_offset` extends max data offset by
  4481. * |datalen|. This function only extends connection-level flow
  4482. * control window.
  4483. */
  4484. NGTCP2_EXTERN void ngtcp2_conn_extend_max_offset(ngtcp2_conn *conn,
  4485. uint64_t datalen);
  4486. /**
  4487. * @function
  4488. *
  4489. * `ngtcp2_conn_extend_max_streams_bidi` extends the number of maximum
  4490. * remote bidirectional streams that a remote endpoint can open by
  4491. * |n|.
  4492. *
  4493. * The library does not increase maximum stream limit automatically.
  4494. * The exception is when a stream is closed without
  4495. * :member:`ngtcp2_callbacks.stream_open` callback being called. In
  4496. * this case, stream limit is increased automatically.
  4497. */
  4498. NGTCP2_EXTERN void ngtcp2_conn_extend_max_streams_bidi(ngtcp2_conn *conn,
  4499. size_t n);
  4500. /**
  4501. * @function
  4502. *
  4503. * `ngtcp2_conn_extend_max_streams_uni` extends the number of maximum
  4504. * remote unidirectional streams that a remote endpoint can open by
  4505. * |n|.
  4506. *
  4507. * The library does not increase maximum stream limit automatically.
  4508. * The exception is when a stream is closed without
  4509. * :member:`ngtcp2_callbacks.stream_open` callback being called. In
  4510. * this case, stream limit is increased automatically.
  4511. */
  4512. NGTCP2_EXTERN void ngtcp2_conn_extend_max_streams_uni(ngtcp2_conn *conn,
  4513. size_t n);
  4514. /**
  4515. * @function
  4516. *
  4517. * `ngtcp2_conn_get_dcid` returns the non-NULL pointer to the current
  4518. * Destination Connection ID. If no Destination Connection ID is
  4519. * present, the return value is not ``NULL``, and its :member:`datalen
  4520. * <ngtcp2_cid.datalen>` field is 0.
  4521. */
  4522. NGTCP2_EXTERN const ngtcp2_cid *ngtcp2_conn_get_dcid(ngtcp2_conn *conn);
  4523. /**
  4524. * @function
  4525. *
  4526. * `ngtcp2_conn_get_client_initial_dcid` returns the non-NULL pointer
  4527. * to the Destination Connection ID that client sent in its Initial
  4528. * packet. If the Destination Connection ID is not present, the
  4529. * return value is not ``NULL``, and its :member:`datalen
  4530. * <ngtcp2_cid.datalen>` field is 0.
  4531. */
  4532. NGTCP2_EXTERN const ngtcp2_cid *
  4533. ngtcp2_conn_get_client_initial_dcid(ngtcp2_conn *conn);
  4534. /**
  4535. * @function
  4536. *
  4537. * `ngtcp2_conn_get_scid` writes the all Source Connection IDs which a
  4538. * local endpoint has provided to a remote endpoint, and are not
  4539. * retired in |dest|. If |dest| is NULL, this function does not write
  4540. * anything, and returns the number of Source Connection IDs that
  4541. * would otherwise be written to the provided buffer. The buffer
  4542. * pointed by |dest| must have sizeof(:type:`ngtcp2_cid`) * n bytes
  4543. * available, where n is the return value of `ngtcp2_conn_get_scid`
  4544. * with |dest| == NULL.
  4545. */
  4546. NGTCP2_EXTERN size_t ngtcp2_conn_get_scid(ngtcp2_conn *conn, ngtcp2_cid *dest);
  4547. /**
  4548. * @struct
  4549. *
  4550. * :type:`ngtcp2_cid_token` is the convenient struct to store
  4551. * Connection ID, its associated path, and stateless reset token.
  4552. */
  4553. typedef struct ngtcp2_cid_token {
  4554. /**
  4555. * :member:`seq` is the sequence number of this Connection ID.
  4556. */
  4557. uint64_t seq;
  4558. /**
  4559. * :member:`cid` is Connection ID.
  4560. */
  4561. ngtcp2_cid cid;
  4562. /**
  4563. * :member:`ps` is the path which this Connection ID is associated
  4564. * with.
  4565. */
  4566. ngtcp2_path_storage ps;
  4567. /**
  4568. * :member:`token` is the stateless reset token for this Connection
  4569. * ID.
  4570. */
  4571. uint8_t token[NGTCP2_STATELESS_RESET_TOKENLEN];
  4572. /**
  4573. * :member:`token_present` is nonzero if token contains stateless
  4574. * reset token.
  4575. */
  4576. uint8_t token_present;
  4577. } ngtcp2_cid_token;
  4578. /**
  4579. * @function
  4580. *
  4581. * `ngtcp2_conn_get_active_dcid` writes the all active Destination
  4582. * Connection IDs and their tokens to |dest|. Before handshake
  4583. * completes, this function returns 0. If |dest| is NULL, this
  4584. * function does not write anything, and returns the number of
  4585. * Destination Connection IDs that would otherwise be written to the
  4586. * provided buffer. The buffer pointed by |dest| must have
  4587. * sizeof(:type:`ngtcp2_cid_token`) * n bytes available, where n is
  4588. * the return value of `ngtcp2_conn_get_active_dcid` with |dest| ==
  4589. * NULL.
  4590. */
  4591. NGTCP2_EXTERN size_t ngtcp2_conn_get_active_dcid(ngtcp2_conn *conn,
  4592. ngtcp2_cid_token *dest);
  4593. /**
  4594. * @function
  4595. *
  4596. * `ngtcp2_conn_get_client_chosen_version` returns the client chosen
  4597. * version.
  4598. */
  4599. NGTCP2_EXTERN uint32_t ngtcp2_conn_get_client_chosen_version(ngtcp2_conn *conn);
  4600. /**
  4601. * @function
  4602. *
  4603. * `ngtcp2_conn_get_negotiated_version` returns the negotiated
  4604. * version.
  4605. *
  4606. * Until the version is negotiated, this function returns 0.
  4607. */
  4608. NGTCP2_EXTERN uint32_t ngtcp2_conn_get_negotiated_version(ngtcp2_conn *conn);
  4609. /**
  4610. * @function
  4611. *
  4612. * `ngtcp2_conn_tls_early_data_rejected` tells |conn| that early data
  4613. * was rejected by a server during TLS handshake, or client decided
  4614. * not to attempt early data for some reason. |conn| discards the
  4615. * following connection states:
  4616. *
  4617. * - Any opened streams.
  4618. * - Stream identifier allocations.
  4619. * - Max data extended by `ngtcp2_conn_extend_max_offset`.
  4620. * - Max bidi streams extended by `ngtcp2_conn_extend_max_streams_bidi`.
  4621. * - Max uni streams extended by `ngtcp2_conn_extend_max_streams_uni`.
  4622. *
  4623. * Application which wishes to retransmit early data, it has to open
  4624. * streams, and send stream data again.
  4625. *
  4626. * This function returns 0 if it succeeds, or one of the following
  4627. * negative error codes:
  4628. *
  4629. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE`
  4630. * User callback failed
  4631. */
  4632. NGTCP2_EXTERN int ngtcp2_conn_tls_early_data_rejected(ngtcp2_conn *conn);
  4633. /**
  4634. * @function
  4635. *
  4636. * `ngtcp2_conn_get_tls_early_data_rejected` returns nonzero if
  4637. * `ngtcp2_conn_tls_early_data_rejected` has been called.
  4638. */
  4639. NGTCP2_EXTERN int ngtcp2_conn_get_tls_early_data_rejected(ngtcp2_conn *conn);
  4640. /**
  4641. * @function
  4642. *
  4643. * `ngtcp2_conn_get_conn_info` assigns connection statistics data to
  4644. * |*cinfo|.
  4645. */
  4646. NGTCP2_EXTERN void ngtcp2_conn_get_conn_info_versioned(ngtcp2_conn *conn,
  4647. int conn_info_version,
  4648. ngtcp2_conn_info *cinfo);
  4649. /**
  4650. * @function
  4651. *
  4652. * `ngtcp2_conn_submit_crypto_data` submits crypto data |data| of
  4653. * length |datalen| to the library for transmission.
  4654. * |encryption_level| specifies the encryption level of data.
  4655. *
  4656. * The library makes a copy of the buffer pointed by |data| of length
  4657. * |datalen|. Application can discard |data|.
  4658. */
  4659. NGTCP2_EXTERN int
  4660. ngtcp2_conn_submit_crypto_data(ngtcp2_conn *conn,
  4661. ngtcp2_encryption_level encryption_level,
  4662. const uint8_t *data, const size_t datalen);
  4663. /**
  4664. * @function
  4665. *
  4666. * `ngtcp2_conn_submit_new_token` submits address validation token.
  4667. * It is sent in NEW_TOKEN frame. Only server can call this function.
  4668. * |tokenlen| must not be 0.
  4669. *
  4670. * This function makes a copy of the buffer pointed by |token| of
  4671. * length |tokenlen|.
  4672. *
  4673. * This function returns 0 if it succeeds, or one of the following
  4674. * negative error codes:
  4675. *
  4676. * :macro:`NGTCP2_ERR_NOMEM`
  4677. * Out of memory.
  4678. */
  4679. NGTCP2_EXTERN int ngtcp2_conn_submit_new_token(ngtcp2_conn *conn,
  4680. const uint8_t *token,
  4681. size_t tokenlen);
  4682. /**
  4683. * @function
  4684. *
  4685. * `ngtcp2_conn_set_local_addr` sets local endpoint address |addr| to
  4686. * the current path of |conn|. This function is provided for testing
  4687. * purpose only.
  4688. */
  4689. NGTCP2_EXTERN void ngtcp2_conn_set_local_addr(ngtcp2_conn *conn,
  4690. const ngtcp2_addr *addr);
  4691. /**
  4692. * @function
  4693. *
  4694. * `ngtcp2_conn_set_path_user_data` sets the |path_user_data| to the
  4695. * current path (see :member:`ngtcp2_path.user_data`).
  4696. */
  4697. NGTCP2_EXTERN void ngtcp2_conn_set_path_user_data(ngtcp2_conn *conn,
  4698. void *path_user_data);
  4699. /**
  4700. * @function
  4701. *
  4702. * `ngtcp2_conn_get_path` returns the current path.
  4703. */
  4704. NGTCP2_EXTERN const ngtcp2_path *ngtcp2_conn_get_path(ngtcp2_conn *conn);
  4705. /**
  4706. * @function
  4707. *
  4708. * `ngtcp2_conn_get_max_tx_udp_payload_size` returns the maximum UDP
  4709. * payload size that this local endpoint would send. This is the
  4710. * value of :member:`ngtcp2_settings.max_tx_udp_payload_size` that is
  4711. * passed to `ngtcp2_conn_client_new` or `ngtcp2_conn_server_new`.
  4712. */
  4713. NGTCP2_EXTERN size_t ngtcp2_conn_get_max_tx_udp_payload_size(ngtcp2_conn *conn);
  4714. /**
  4715. * @function
  4716. *
  4717. * `ngtcp2_conn_get_path_max_tx_udp_payload_size` returns the maximum
  4718. * UDP payload size for the current path. If
  4719. * :member:`ngtcp2_settings.no_tx_udp_payload_size_shaping` is set to
  4720. * nonzero, this function is equivalent to
  4721. * `ngtcp2_conn_get_max_tx_udp_payload_size`. Otherwise, it returns
  4722. * the maximum UDP payload size that is probed for the current path.
  4723. */
  4724. NGTCP2_EXTERN size_t
  4725. ngtcp2_conn_get_path_max_tx_udp_payload_size(ngtcp2_conn *conn);
  4726. /**
  4727. * @function
  4728. *
  4729. * `ngtcp2_conn_initiate_immediate_migration` starts connection
  4730. * migration to the given |path|. Only client can initiate migration.
  4731. * This function does immediate migration; while the path validation
  4732. * is nonetheless performed, this function does not wait for it to
  4733. * succeed.
  4734. *
  4735. * This function returns 0 if it succeeds, or one of the following
  4736. * negative error codes:
  4737. *
  4738. * :macro:`NGTCP2_ERR_INVALID_STATE`
  4739. * Migration is disabled; or handshake is not yet confirmed; or
  4740. * client is migrating to server's preferred address.
  4741. * :macro:`NGTCP2_ERR_CONN_ID_BLOCKED`
  4742. * No unused connection ID is available.
  4743. * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
  4744. * :member:`local <ngtcp2_path.local>` field of |path| equals the
  4745. * current local address.
  4746. * :macro:`NGTCP2_ERR_NOMEM`
  4747. * Out of memory
  4748. */
  4749. NGTCP2_EXTERN int ngtcp2_conn_initiate_immediate_migration(
  4750. ngtcp2_conn *conn, const ngtcp2_path *path, ngtcp2_tstamp ts);
  4751. /**
  4752. * @function
  4753. *
  4754. * `ngtcp2_conn_initiate_migration` starts connection migration to the
  4755. * given |path|. Only client can initiate migration. Unlike
  4756. * `ngtcp2_conn_initiate_immediate_migration`, this function starts a
  4757. * path validation with a new path, and migrate to the new path after
  4758. * successful path validation.
  4759. *
  4760. * This function returns 0 if it succeeds, or one of the following
  4761. * negative error codes:
  4762. *
  4763. * :macro:`NGTCP2_ERR_INVALID_STATE`
  4764. * Migration is disabled; or handshake is not yet confirmed; or
  4765. * client is migrating to server's preferred address.
  4766. * :macro:`NGTCP2_ERR_CONN_ID_BLOCKED`
  4767. * No unused connection ID is available.
  4768. * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
  4769. * :member:`local <ngtcp2_path.local>` field of |path| equals the
  4770. * current local address.
  4771. * :macro:`NGTCP2_ERR_NOMEM`
  4772. * Out of memory
  4773. */
  4774. NGTCP2_EXTERN int ngtcp2_conn_initiate_migration(ngtcp2_conn *conn,
  4775. const ngtcp2_path *path,
  4776. ngtcp2_tstamp ts);
  4777. /**
  4778. * @function
  4779. *
  4780. * `ngtcp2_conn_get_max_data_left` returns the number of bytes that
  4781. * this local endpoint can send in this connection without violating
  4782. * connection-level flow control.
  4783. */
  4784. NGTCP2_EXTERN uint64_t ngtcp2_conn_get_max_data_left(ngtcp2_conn *conn);
  4785. /**
  4786. * @function
  4787. *
  4788. * `ngtcp2_conn_get_max_stream_data_left` returns the number of bytes
  4789. * that this local endpoint can send to a stream identified by
  4790. * |stream_id| without violating stream-level flow control. If no
  4791. * such stream is found, this function returns 0.
  4792. */
  4793. NGTCP2_EXTERN uint64_t ngtcp2_conn_get_max_stream_data_left(ngtcp2_conn *conn,
  4794. int64_t stream_id);
  4795. /**
  4796. * @function
  4797. *
  4798. * `ngtcp2_conn_get_streams_bidi_left` returns the number of
  4799. * bidirectional streams which the local endpoint can open without
  4800. * violating stream concurrency limit.
  4801. */
  4802. NGTCP2_EXTERN uint64_t ngtcp2_conn_get_streams_bidi_left(ngtcp2_conn *conn);
  4803. /**
  4804. * @function
  4805. *
  4806. * `ngtcp2_conn_get_streams_uni_left` returns the number of
  4807. * unidirectional streams which the local endpoint can open without
  4808. * violating stream concurrency limit.
  4809. */
  4810. NGTCP2_EXTERN uint64_t ngtcp2_conn_get_streams_uni_left(ngtcp2_conn *conn);
  4811. /**
  4812. * @function
  4813. *
  4814. * `ngtcp2_conn_get_cwnd_left` returns the cwnd minus the number of
  4815. * bytes in flight on the current path. If the former is smaller than
  4816. * the latter, this function returns 0.
  4817. */
  4818. NGTCP2_EXTERN uint64_t ngtcp2_conn_get_cwnd_left(ngtcp2_conn *conn);
  4819. /**
  4820. * @function
  4821. *
  4822. * `ngtcp2_conn_set_initial_crypto_ctx` sets |ctx| for Initial packet
  4823. * encryption. The passed data will be passed to
  4824. * :type:`ngtcp2_encrypt`, :type:`ngtcp2_decrypt` and
  4825. * :type:`ngtcp2_hp_mask` callbacks.
  4826. */
  4827. NGTCP2_EXTERN void
  4828. ngtcp2_conn_set_initial_crypto_ctx(ngtcp2_conn *conn,
  4829. const ngtcp2_crypto_ctx *ctx);
  4830. /**
  4831. * @function
  4832. *
  4833. * `ngtcp2_conn_get_initial_crypto_ctx` returns
  4834. * :type:`ngtcp2_crypto_ctx` object for Initial packet encryption.
  4835. */
  4836. NGTCP2_EXTERN const ngtcp2_crypto_ctx *
  4837. ngtcp2_conn_get_initial_crypto_ctx(ngtcp2_conn *conn);
  4838. /**
  4839. * @function
  4840. *
  4841. * `ngtcp2_conn_set_crypto_ctx` sets |ctx| for Handshake/1-RTT packet
  4842. * encryption. The passed data will be passed to
  4843. * :type:`ngtcp2_encrypt`, :type:`ngtcp2_decrypt` and
  4844. * :type:`ngtcp2_hp_mask` callbacks.
  4845. */
  4846. NGTCP2_EXTERN void ngtcp2_conn_set_crypto_ctx(ngtcp2_conn *conn,
  4847. const ngtcp2_crypto_ctx *ctx);
  4848. /**
  4849. * @function
  4850. *
  4851. * `ngtcp2_conn_get_crypto_ctx` returns :type:`ngtcp2_crypto_ctx`
  4852. * object for Handshake/1-RTT packet encryption.
  4853. */
  4854. NGTCP2_EXTERN const ngtcp2_crypto_ctx *
  4855. ngtcp2_conn_get_crypto_ctx(ngtcp2_conn *conn);
  4856. /**
  4857. * @function
  4858. *
  4859. * `ngtcp2_conn_set_0rtt_crypto_ctx` sets |ctx| for 0-RTT packet
  4860. * encryption. The passed data will be passed to
  4861. * :type:`ngtcp2_encrypt`, :type:`ngtcp2_decrypt` and
  4862. * :type:`ngtcp2_hp_mask` callbacks.
  4863. */
  4864. NGTCP2_EXTERN void
  4865. ngtcp2_conn_set_0rtt_crypto_ctx(ngtcp2_conn *conn,
  4866. const ngtcp2_crypto_ctx *ctx);
  4867. /**
  4868. * @function
  4869. *
  4870. * `ngtcp2_conn_get_0rtt_crypto_ctx` returns :type:`ngtcp2_crypto_ctx`
  4871. * object for 0-RTT packet encryption.
  4872. */
  4873. NGTCP2_EXTERN const ngtcp2_crypto_ctx *
  4874. ngtcp2_conn_get_0rtt_crypto_ctx(ngtcp2_conn *conn);
  4875. /**
  4876. * @function
  4877. *
  4878. * `ngtcp2_conn_get_tls_native_handle` returns TLS native handle set
  4879. * by `ngtcp2_conn_set_tls_native_handle`.
  4880. */
  4881. NGTCP2_EXTERN void *ngtcp2_conn_get_tls_native_handle(ngtcp2_conn *conn);
  4882. /**
  4883. * @function
  4884. *
  4885. * `ngtcp2_conn_set_tls_native_handle` sets TLS native handle
  4886. * |tls_native_handle| to |conn|. Internally, it is used as an opaque
  4887. * pointer.
  4888. */
  4889. NGTCP2_EXTERN void ngtcp2_conn_set_tls_native_handle(ngtcp2_conn *conn,
  4890. void *tls_native_handle);
  4891. /**
  4892. * @function
  4893. *
  4894. * `ngtcp2_conn_set_retry_aead` sets |aead| and |aead_ctx| for Retry
  4895. * integrity tag verification. |aead| must be AEAD_AES_128_GCM.
  4896. * |aead_ctx| must be initialized with :macro:`NGTCP2_RETRY_KEY` as
  4897. * encryption key. This function must be called if |conn| is
  4898. * initialized as client. Server does not verify the tag, and has no
  4899. * need to call this function.
  4900. *
  4901. * |conn| takes ownership of |aead_ctx|.
  4902. * :member:`ngtcp2_callbacks.delete_crypto_aead_ctx` will be called to
  4903. * delete this object when it is no longer used.
  4904. */
  4905. NGTCP2_EXTERN void
  4906. ngtcp2_conn_set_retry_aead(ngtcp2_conn *conn, const ngtcp2_crypto_aead *aead,
  4907. const ngtcp2_crypto_aead_ctx *aead_ctx);
  4908. /**
  4909. * @enum
  4910. *
  4911. * :type:`ngtcp2_ccerr_type` defines connection error type.
  4912. */
  4913. typedef enum ngtcp2_ccerr_type {
  4914. /**
  4915. * :enum:`NGTCP2_CCERR_TYPE_TRANSPORT` indicates the QUIC transport
  4916. * error, and the error code is QUIC transport error code.
  4917. */
  4918. NGTCP2_CCERR_TYPE_TRANSPORT,
  4919. /**
  4920. * :enum:`NGTCP2_CCERR_TYPE_APPLICATION` indicates an application
  4921. * error, and the error code is application error code.
  4922. */
  4923. NGTCP2_CCERR_TYPE_APPLICATION,
  4924. /**
  4925. * :enum:`NGTCP2_CCERR_TYPE_VERSION_NEGOTIATION` is a special case
  4926. * of QUIC transport error, and it indicates that client receives
  4927. * Version Negotiation packet.
  4928. */
  4929. NGTCP2_CCERR_TYPE_VERSION_NEGOTIATION,
  4930. /**
  4931. * :enum:`NGTCP2_CCERR_TYPE_IDLE_CLOSE` is a special case of QUIC
  4932. * transport error, and it indicates that connection is closed
  4933. * because of idle timeout.
  4934. */
  4935. NGTCP2_CCERR_TYPE_IDLE_CLOSE,
  4936. /**
  4937. * :enum:`NGTCP2_CCERR_TYPE_DROP_CONN` is a special case of QUIC
  4938. * transport error, and it indicates that connection should be
  4939. * dropped without sending a CONNECTION_CLOSE frame.
  4940. */
  4941. NGTCP2_CCERR_TYPE_DROP_CONN,
  4942. /**
  4943. * :enum:`NGTCP2_CCERR_TYPE_RETRY` is a special case of QUIC
  4944. * transport error, and it indicates that RETRY packet should be
  4945. * sent to a client.
  4946. */
  4947. NGTCP2_CCERR_TYPE_RETRY
  4948. } ngtcp2_ccerr_type;
  4949. /**
  4950. * @struct
  4951. *
  4952. * :type:`ngtcp2_ccerr` contains connection error code, its type, a
  4953. * frame type that caused this error, and the optional reason phrase.
  4954. */
  4955. typedef struct ngtcp2_ccerr {
  4956. /**
  4957. * :member:`type` is the type of this error.
  4958. */
  4959. ngtcp2_ccerr_type type;
  4960. /**
  4961. * :member:`error_code` is the error code for connection closure.
  4962. * Its interpretation depends on :member:`type`.
  4963. */
  4964. uint64_t error_code;
  4965. /**
  4966. * :member:`frame_type` is the type of QUIC frame which triggers
  4967. * this connection error. This field is set to 0 if the frame type
  4968. * is unknown.
  4969. */
  4970. uint64_t frame_type;
  4971. /**
  4972. * :member:`reason` points to the buffer which contains a reason
  4973. * phrase. It may be NULL if there is no reason phrase. If it is
  4974. * received from a remote endpoint, it is truncated to at most 1024
  4975. * bytes.
  4976. */
  4977. const uint8_t *reason;
  4978. /**
  4979. * :member:`reasonlen` is the length of data pointed by
  4980. * :member:`reason`.
  4981. */
  4982. size_t reasonlen;
  4983. } ngtcp2_ccerr;
  4984. /**
  4985. * @function
  4986. *
  4987. * `ngtcp2_ccerr_default` initializes |ccerr| with the default values.
  4988. * It sets the following fields:
  4989. *
  4990. * - :member:`type <ngtcp2_ccerr.type>` =
  4991. * :enum:`ngtcp2_ccerr_type.NGTCP2_CCERR_TYPE_TRANSPORT`
  4992. * - :member:`error_code <ngtcp2_ccerr.error_code>` =
  4993. * :macro:`NGTCP2_NO_ERROR`.
  4994. * - :member:`frame_type <ngtcp2_ccerr.frame_type>` = 0
  4995. * - :member:`reason <ngtcp2_ccerr.reason>` = NULL
  4996. * - :member:`reasonlen <ngtcp2_ccerr.reasonlen>` = 0
  4997. */
  4998. NGTCP2_EXTERN void ngtcp2_ccerr_default(ngtcp2_ccerr *ccerr);
  4999. /**
  5000. * @function
  5001. *
  5002. * `ngtcp2_ccerr_set_transport_error` sets :member:`ccerr->type
  5003. * <ngtcp2_ccerr.type>` to
  5004. * :enum:`ngtcp2_ccerr_type.NGTCP2_CCERR_TYPE_TRANSPORT`, and
  5005. * :member:`ccerr->error_code <ngtcp2_ccerr.error_code>` to
  5006. * |error_code|. |reason| is the reason phrase of length |reasonlen|.
  5007. * This function does not make a copy of the reason phrase.
  5008. */
  5009. NGTCP2_EXTERN void ngtcp2_ccerr_set_transport_error(ngtcp2_ccerr *ccerr,
  5010. uint64_t error_code,
  5011. const uint8_t *reason,
  5012. size_t reasonlen);
  5013. /**
  5014. * @function
  5015. *
  5016. * `ngtcp2_ccerr_set_liberr` sets type and error_code based on
  5017. * |liberr|.
  5018. *
  5019. * |reason| is the reason phrase of length |reasonlen|. This function
  5020. * does not make a copy of the reason phrase.
  5021. *
  5022. * If |liberr| is :macro:`NGTCP2_ERR_RECV_VERSION_NEGOTIATION`,
  5023. * :member:`ccerr->type <ngtcp2_ccerr.type>` is set to
  5024. * :enum:`ngtcp2_ccerr_type.NGTCP2_CCERR_TYPE_VERSION_NEGOTIATION`,
  5025. * and :member:`ccerr->error_code <ngtcp2_ccerr.error_code>` to
  5026. * :macro:`NGTCP2_NO_ERROR`.
  5027. *
  5028. * If |liberr| is :macro:`NGTCP2_ERR_IDLE_CLOSE`, :member:`ccerr->type
  5029. * <ngtcp2_ccerr.type>` is set to
  5030. * :enum:`ngtcp2_ccerr_type.NGTCP2_CCERR_TYPE_IDLE_CLOSE`, and
  5031. * :member:`ccerr->error_code <ngtcp2_ccerr.error_code>` to
  5032. * :macro:`NGTCP2_NO_ERROR`.
  5033. *
  5034. * If |liberr| is :macro:`NGTCP2_ERR_DROP_CONN`, :member:`ccerr->type
  5035. * <ngtcp2_ccerr.type>` is set to
  5036. * :enum:`ngtcp2_ccerr_type.NGTCP2_CCERR_TYPE_DROP_CONN`, and
  5037. * :member:`ccerr->error_code <ngtcp2_ccerr.error_code>` to
  5038. * :macro:`NGTCP2_NO_ERROR`.
  5039. *
  5040. * If |liberr| is :macro:`NGTCP2_ERR_RETRY`, :member:`ccerr->type
  5041. * <ngtcp2_ccerr.type>` is set to
  5042. * :enum:`ngtcp2_ccerr_type.NGTCP2_CCERR_TYPE_RETRY`, and
  5043. * :member:`ccerr->error_type <ngtcp2_ccerr.error_code>` to
  5044. * :macro:`NGTCP2_NO_ERROR`.
  5045. *
  5046. * Otherwise, :member:`ccerr->type <ngtcp2_ccerr.type>` is set to
  5047. * :enum:`ngtcp2_ccerr_type.NGTCP2_CCERR_TYPE_TRANSPORT`, and
  5048. * :member:`ccerr->error_code <ngtcp2_ccerr.error_code>` is set to an
  5049. * error code inferred by |liberr| (see
  5050. * `ngtcp2_err_infer_quic_transport_error_code`).
  5051. */
  5052. NGTCP2_EXTERN void ngtcp2_ccerr_set_liberr(ngtcp2_ccerr *ccerr, int liberr,
  5053. const uint8_t *reason,
  5054. size_t reasonlen);
  5055. /**
  5056. * @function
  5057. *
  5058. * `ngtcp2_ccerr_set_tls_alert` sets :member:`ccerr->type
  5059. * <ngtcp2_ccerr.type>` to
  5060. * :enum:`ngtcp2_ccerr_type.NGTCP2_CCERR_TYPE_TRANSPORT`, and
  5061. * :member:`ccerr->error_code <ngtcp2_ccerr.error_code>` to bitwise-OR
  5062. * of :macro:`NGTCP2_CRYPTO_ERROR` and |tls_alert|. |reason| is the
  5063. * reason phrase of length |reasonlen|. This function does not make a
  5064. * copy of the reason phrase.
  5065. */
  5066. NGTCP2_EXTERN void ngtcp2_ccerr_set_tls_alert(ngtcp2_ccerr *ccerr,
  5067. uint8_t tls_alert,
  5068. const uint8_t *reason,
  5069. size_t reasonlen);
  5070. /**
  5071. * @function
  5072. *
  5073. * `ngtcp2_ccerr_set_application_error` sets :member:`ccerr->type
  5074. * <ngtcp2_ccerr.type>` to
  5075. * :enum:`ngtcp2_ccerr_type.NGTCP2_CCERR_TYPE_APPLICATION`, and
  5076. * :member:`ccerr->error_code <ngtcp2_ccerr.error_code>` to
  5077. * |error_code|. |reason| is the reason phrase of length |reasonlen|.
  5078. * This function does not make a copy of the reason phrase.
  5079. */
  5080. NGTCP2_EXTERN void ngtcp2_ccerr_set_application_error(ngtcp2_ccerr *ccerr,
  5081. uint64_t error_code,
  5082. const uint8_t *reason,
  5083. size_t reasonlen);
  5084. /**
  5085. * @function
  5086. *
  5087. * `ngtcp2_conn_write_connection_close` writes a packet which contains
  5088. * CONNECTION_CLOSE frame(s) (type 0x1c or 0x1d) in the buffer pointed
  5089. * by |dest| whose capacity is |destlen|.
  5090. *
  5091. * For client, |destlen| should be at least
  5092. * :macro:`NGTCP2_MAX_UDP_PAYLOAD_SIZE`.
  5093. *
  5094. * If |path| is not ``NULL``, this function stores the network path
  5095. * with which the packet should be sent. Each addr field must point
  5096. * to the buffer which should be at least
  5097. * sizeof(:type:`ngtcp2_sockaddr_union`) bytes long. The assignment
  5098. * might not be done if nothing is written to |dest|.
  5099. *
  5100. * If |pi| is not ``NULL``, this function stores packet metadata in it
  5101. * if it succeeds. The metadata includes ECN markings.
  5102. *
  5103. * If :member:`ccerr->type <ngtcp2_ccerr.type>` ==
  5104. * :enum:`ngtcp2_ccerr_type.NGTCP2_CCERR_TYPE_TRANSPORT`, this
  5105. * function sends CONNECTION_CLOSE (type 0x1c) frame. If
  5106. * :member:`ccerr->type <ngtcp2_ccerr.type>` ==
  5107. * :enum:`ngtcp2_ccerr_type.NGTCP2_CCERR_TYPE_APPLICATION`, it sends
  5108. * CONNECTION_CLOSE (type 0x1d) frame. Otherwise, it does not produce
  5109. * any data, and returns 0.
  5110. *
  5111. * |destlen| could be shorten by some factors (e.g., server side
  5112. * amplification limit). This function returns
  5113. * :macro:`NGTCP2_ERR_NOBUF` if the resulting buffer is too small even
  5114. * if the given buffer has enough space.
  5115. *
  5116. * This function must not be called from inside the callback
  5117. * functions.
  5118. *
  5119. * At the moment, successful call to this function makes connection
  5120. * close. We may change this behaviour in the future to allow
  5121. * graceful shutdown.
  5122. *
  5123. * This function returns the number of bytes written in |dest| if it
  5124. * succeeds, or one of the following negative error codes:
  5125. *
  5126. * :macro:`NGTCP2_ERR_NOMEM`
  5127. * Out of memory
  5128. * :macro:`NGTCP2_ERR_NOBUF`
  5129. * Buffer is too small
  5130. * :macro:`NGTCP2_ERR_INVALID_STATE`
  5131. * The current state does not allow sending CONNECTION_CLOSE
  5132. * frame.
  5133. * :macro:`NGTCP2_ERR_PKT_NUM_EXHAUSTED`
  5134. * Packet number is exhausted, and cannot send any more packet.
  5135. * :macro:`NGTCP2_ERR_CALLBACK_FAILURE`
  5136. * User callback failed
  5137. */
  5138. NGTCP2_EXTERN ngtcp2_ssize ngtcp2_conn_write_connection_close_versioned(
  5139. ngtcp2_conn *conn, ngtcp2_path *path, int pkt_info_version,
  5140. ngtcp2_pkt_info *pi, uint8_t *dest, size_t destlen,
  5141. const ngtcp2_ccerr *ccerr, ngtcp2_tstamp ts);
  5142. /**
  5143. * @function
  5144. *
  5145. * `ngtcp2_conn_get_ccerr` returns the received connection close
  5146. * error. If no connection error is received, it returns
  5147. * :type:`ngtcp2_ccerr` that is initialized by `ngtcp2_ccerr_default`.
  5148. */
  5149. NGTCP2_EXTERN const ngtcp2_ccerr *ngtcp2_conn_get_ccerr(ngtcp2_conn *conn);
  5150. /**
  5151. * @function
  5152. *
  5153. * `ngtcp2_conn_is_local_stream` returns nonzero if |stream_id|
  5154. * denotes a locally initiated stream.
  5155. */
  5156. NGTCP2_EXTERN int ngtcp2_conn_is_local_stream(ngtcp2_conn *conn,
  5157. int64_t stream_id);
  5158. /**
  5159. * @function
  5160. *
  5161. * `ngtcp2_conn_is_server` returns nonzero if |conn| is initialized as
  5162. * server.
  5163. */
  5164. NGTCP2_EXTERN int ngtcp2_conn_is_server(ngtcp2_conn *conn);
  5165. /**
  5166. * @function
  5167. *
  5168. * `ngtcp2_conn_after_retry` returns nonzero if |conn| as a client has
  5169. * received Retry packet from server, and successfully validated it.
  5170. */
  5171. NGTCP2_EXTERN int ngtcp2_conn_after_retry(ngtcp2_conn *conn);
  5172. /**
  5173. * @function
  5174. *
  5175. * `ngtcp2_conn_set_stream_user_data` sets |stream_user_data| to the
  5176. * stream identified by |stream_id|.
  5177. *
  5178. * This function returns 0 if it succeeds, or one of the following
  5179. * negative error codes:
  5180. *
  5181. * :macro:`NGTCP2_ERR_STREAM_NOT_FOUND`
  5182. * Stream does not exist
  5183. */
  5184. NGTCP2_EXTERN int ngtcp2_conn_set_stream_user_data(ngtcp2_conn *conn,
  5185. int64_t stream_id,
  5186. void *stream_user_data);
  5187. /**
  5188. * @function
  5189. *
  5190. * `ngtcp2_conn_update_pkt_tx_time` sets the time instant of the next
  5191. * packet transmission to pace packets. This function must be called
  5192. * after (multiple invocation of) `ngtcp2_conn_writev_stream`. If
  5193. * packet aggregation (e.g., packet batching, GSO) is used, call this
  5194. * function after all aggregated datagrams are sent, which indicates
  5195. * multiple invocation of `ngtcp2_conn_writev_stream`.
  5196. */
  5197. NGTCP2_EXTERN void ngtcp2_conn_update_pkt_tx_time(ngtcp2_conn *conn,
  5198. ngtcp2_tstamp ts);
  5199. /**
  5200. * @function
  5201. *
  5202. * `ngtcp2_conn_get_send_quantum` returns the maximum number of bytes
  5203. * that can be sent in one go without packet spacing.
  5204. */
  5205. NGTCP2_EXTERN size_t ngtcp2_conn_get_send_quantum(ngtcp2_conn *conn);
  5206. /**
  5207. * @function
  5208. *
  5209. * `ngtcp2_conn_get_stream_loss_count` returns the number of packets
  5210. * that contain STREAM frame for a stream identified by |stream_id|
  5211. * and are declared to be lost. The number may include the spurious
  5212. * losses. If no stream identified by |stream_id| is found, this
  5213. * function returns 0.
  5214. */
  5215. NGTCP2_EXTERN size_t ngtcp2_conn_get_stream_loss_count(ngtcp2_conn *conn,
  5216. int64_t stream_id);
  5217. /**
  5218. * @function
  5219. *
  5220. * `ngtcp2_strerror` returns the text representation of |liberr|.
  5221. * |liberr| must be one of ngtcp2 library error codes (which is
  5222. * defined as :macro:`NGTCP2_ERR_* <NGTCP2_ERR_INVALID_ARGUMENT>`
  5223. * macros).
  5224. */
  5225. NGTCP2_EXTERN const char *ngtcp2_strerror(int liberr);
  5226. /**
  5227. * @function
  5228. *
  5229. * `ngtcp2_err_is_fatal` returns nonzero if |liberr| is a fatal error.
  5230. * |liberr| must be one of ngtcp2 library error codes (which is
  5231. * defined as :macro:`NGTCP2_ERR_* <NGTCP2_ERR_INVALID_ARGUMENT>`
  5232. * macros).
  5233. */
  5234. NGTCP2_EXTERN int ngtcp2_err_is_fatal(int liberr);
  5235. /**
  5236. * @function
  5237. *
  5238. * `ngtcp2_err_infer_quic_transport_error_code` returns a QUIC
  5239. * transport error code which corresponds to |liberr|. |liberr| must
  5240. * be one of ngtcp2 library error codes (which is defined as
  5241. * :macro:`NGTCP2_ERR_* <NGTCP2_ERR_INVALID_ARGUMENT>` macros).
  5242. */
  5243. NGTCP2_EXTERN uint64_t ngtcp2_err_infer_quic_transport_error_code(int liberr);
  5244. /**
  5245. * @function
  5246. *
  5247. * `ngtcp2_addr_init` initializes |dest| with the given arguments and
  5248. * returns |dest|.
  5249. */
  5250. NGTCP2_EXTERN ngtcp2_addr *ngtcp2_addr_init(ngtcp2_addr *dest,
  5251. const ngtcp2_sockaddr *addr,
  5252. ngtcp2_socklen addrlen);
  5253. /**
  5254. * @function
  5255. *
  5256. * `ngtcp2_addr_copy_byte` copies |addr| of length |addrlen| into the
  5257. * buffer pointed by :member:`dest->addr <ngtcp2_addr.addr>`.
  5258. * :member:`dest->addrlen <ngtcp2_addr.addrlen>` is updated to have
  5259. * |addrlen|. This function assumes that :member:`dest->addr
  5260. * <ngtcp2_addr.addr>` points to a buffer which has a sufficient
  5261. * capacity to store the copy.
  5262. */
  5263. NGTCP2_EXTERN void ngtcp2_addr_copy_byte(ngtcp2_addr *dest,
  5264. const ngtcp2_sockaddr *addr,
  5265. ngtcp2_socklen addrlen);
  5266. /**
  5267. * @function
  5268. *
  5269. * `ngtcp2_path_storage_init` initializes |ps| with the given
  5270. * arguments. This function copies |local_addr| and |remote_addr|.
  5271. */
  5272. NGTCP2_EXTERN void ngtcp2_path_storage_init(ngtcp2_path_storage *ps,
  5273. const ngtcp2_sockaddr *local_addr,
  5274. ngtcp2_socklen local_addrlen,
  5275. const ngtcp2_sockaddr *remote_addr,
  5276. ngtcp2_socklen remote_addrlen,
  5277. void *user_data);
  5278. /**
  5279. * @function
  5280. *
  5281. * `ngtcp2_path_storage_zero` initializes |ps| with the zero length
  5282. * addresses.
  5283. */
  5284. NGTCP2_EXTERN void ngtcp2_path_storage_zero(ngtcp2_path_storage *ps);
  5285. /**
  5286. * @function
  5287. *
  5288. * `ngtcp2_settings_default` initializes |settings| with the default
  5289. * values. First this function fills |settings| with 0, and set the
  5290. * default value to the following fields:
  5291. *
  5292. * * :type:`cc_algo <ngtcp2_settings.cc_algo>` =
  5293. * :enum:`ngtcp2_cc_algo.NGTCP2_CC_ALGO_CUBIC`
  5294. * * :type:`initial_rtt <ngtcp2_settings.initial_rtt>` =
  5295. * :macro:`NGTCP2_DEFAULT_INITIAL_RTT`
  5296. * * :type:`ack_thresh <ngtcp2_settings.ack_thresh>` = 2
  5297. * * :type:`max_tx_udp_payload_size
  5298. * <ngtcp2_settings.max_tx_udp_payload_size>` = 1452
  5299. * * :type:`handshake_timeout <ngtcp2_settings.handshake_timeout>` =
  5300. * ``UINT64_MAX``
  5301. */
  5302. NGTCP2_EXTERN void ngtcp2_settings_default_versioned(int settings_version,
  5303. ngtcp2_settings *settings);
  5304. /**
  5305. * @function
  5306. *
  5307. * `ngtcp2_transport_params_default` initializes |params| with the
  5308. * default values. First this function fills |params| with 0, and set
  5309. * the default value to the following fields:
  5310. *
  5311. * * :type:`max_udp_payload_size
  5312. * <ngtcp2_transport_params.max_udp_payload_size>` =
  5313. * :macro:`NGTCP2_DEFAULT_MAX_RECV_UDP_PAYLOAD_SIZE`
  5314. * * :type:`ack_delay_exponent
  5315. * <ngtcp2_transport_params.ack_delay_exponent>` =
  5316. * :macro:`NGTCP2_DEFAULT_ACK_DELAY_EXPONENT`
  5317. * * :type:`max_ack_delay <ngtcp2_transport_params.max_ack_delay>` =
  5318. * :macro:`NGTCP2_DEFAULT_MAX_ACK_DELAY`
  5319. * * :type:`active_connection_id_limit
  5320. * <ngtcp2_transport_params.active_connection_id_limit>` =
  5321. * :macro:`NGTCP2_DEFAULT_ACTIVE_CONNECTION_ID_LIMIT`
  5322. */
  5323. NGTCP2_EXTERN void
  5324. ngtcp2_transport_params_default_versioned(int transport_params_version,
  5325. ngtcp2_transport_params *params);
  5326. /**
  5327. * @function
  5328. *
  5329. * `ngtcp2_mem_default` returns the default, system standard memory
  5330. * allocator.
  5331. */
  5332. NGTCP2_EXTERN const ngtcp2_mem *ngtcp2_mem_default(void);
  5333. /**
  5334. * @macrosection
  5335. *
  5336. * ngtcp2_info macros
  5337. */
  5338. /**
  5339. * @macro
  5340. *
  5341. * :macro:`NGTCP2_VERSION_AGE` is the age of :type:`ngtcp2_info`
  5342. */
  5343. #define NGTCP2_VERSION_AGE 1
  5344. /**
  5345. * @struct
  5346. *
  5347. * :type:`ngtcp2_info` is what `ngtcp2_version` returns. It holds
  5348. * information about the particular ngtcp2 version.
  5349. */
  5350. typedef struct ngtcp2_info {
  5351. /**
  5352. * :member:`age` is the age of this struct. This instance of ngtcp2
  5353. * sets it to :macro:`NGTCP2_VERSION_AGE` but a future version may
  5354. * bump it and add more struct fields at the bottom
  5355. */
  5356. int age;
  5357. /**
  5358. * :member:`version_num` is the :macro:`NGTCP2_VERSION_NUM` number
  5359. * (since :member:`age` ==1)
  5360. */
  5361. int version_num;
  5362. /**
  5363. * :member:`version_str` points to the :macro:`NGTCP2_VERSION`
  5364. * string (since :member:`age` ==1)
  5365. */
  5366. const char *version_str;
  5367. /* -------- the above fields all exist when age == 1 */
  5368. } ngtcp2_info;
  5369. /**
  5370. * @function
  5371. *
  5372. * `ngtcp2_version` returns a pointer to a :type:`ngtcp2_info` struct
  5373. * with version information about the run-time library in use. The
  5374. * |least_version| argument can be set to a 24 bit numerical value for
  5375. * the least accepted version number, and if the condition is not met,
  5376. * this function will return a ``NULL``. Pass in 0 to skip the
  5377. * version checking.
  5378. */
  5379. NGTCP2_EXTERN const ngtcp2_info *ngtcp2_version(int least_version);
  5380. /**
  5381. * @function
  5382. *
  5383. * `ngtcp2_is_bidi_stream` returns nonzero if |stream_id| denotes
  5384. * bidirectional stream.
  5385. */
  5386. NGTCP2_EXTERN int ngtcp2_is_bidi_stream(int64_t stream_id);
  5387. /**
  5388. * @function
  5389. *
  5390. * `ngtcp2_path_copy` copies |src| into |dest|. This function assumes
  5391. * that |dest| has enough buffer to store the deep copy of
  5392. * :member:`src->local <ngtcp2_path.local>` and :member:`src->remote
  5393. * <ngtcp2_path.remote>`.
  5394. */
  5395. NGTCP2_EXTERN void ngtcp2_path_copy(ngtcp2_path *dest, const ngtcp2_path *src);
  5396. /**
  5397. * @function
  5398. *
  5399. * `ngtcp2_path_eq` returns nonzero if |a| and |b| shares the same
  5400. * local and remote addresses.
  5401. */
  5402. NGTCP2_EXTERN int ngtcp2_path_eq(const ngtcp2_path *a, const ngtcp2_path *b);
  5403. /**
  5404. * @function
  5405. *
  5406. * `ngtcp2_is_supported_version` returns nonzero if the library
  5407. * supports QUIC version |version|.
  5408. */
  5409. NGTCP2_EXTERN int ngtcp2_is_supported_version(uint32_t version);
  5410. /**
  5411. * @function
  5412. *
  5413. * `ngtcp2_is_reserved_version` returns nonzero if |version| is a
  5414. * reserved version.
  5415. */
  5416. NGTCP2_EXTERN int ngtcp2_is_reserved_version(uint32_t version);
  5417. /**
  5418. * @function
  5419. *
  5420. * `ngtcp2_select_version` selects and returns a version from the
  5421. * version set |offered_versions| of |offered_versionslen| elements.
  5422. * |preferred_versions| of |preferred_versionslen| elements specifies
  5423. * the preference of versions, which is sorted in the order of
  5424. * preference. All versions included in |preferred_versions| must be
  5425. * supported by the library, that is, passing any version in the array
  5426. * to `ngtcp2_is_supported_version` must return nonzero. This
  5427. * function is intended to be used by client when it receives Version
  5428. * Negotiation packet. If no version is selected, this function
  5429. * returns 0.
  5430. */
  5431. NGTCP2_EXTERN uint32_t ngtcp2_select_version(const uint32_t *preferred_versions,
  5432. size_t preferred_versionslen,
  5433. const uint32_t *offered_versions,
  5434. size_t offered_versionslen);
  5435. /*
  5436. * Versioned function wrappers
  5437. */
  5438. /*
  5439. * `ngtcp2_conn_read_pkt` is a wrapper around
  5440. * `ngtcp2_conn_read_pkt_versioned` to set the correct struct version.
  5441. */
  5442. #define ngtcp2_conn_read_pkt(CONN, PATH, PI, PKT, PKTLEN, TS) \
  5443. ngtcp2_conn_read_pkt_versioned((CONN), (PATH), NGTCP2_PKT_INFO_VERSION, \
  5444. (PI), (PKT), (PKTLEN), (TS))
  5445. /*
  5446. * `ngtcp2_conn_write_pkt` is a wrapper around
  5447. * `ngtcp2_conn_write_pkt_versioned` to set the correct struct
  5448. * version.
  5449. */
  5450. #define ngtcp2_conn_write_pkt(CONN, PATH, PI, DEST, DESTLEN, TS) \
  5451. ngtcp2_conn_write_pkt_versioned((CONN), (PATH), NGTCP2_PKT_INFO_VERSION, \
  5452. (PI), (DEST), (DESTLEN), (TS))
  5453. /*
  5454. * `ngtcp2_conn_write_stream` is a wrapper around
  5455. * `ngtcp2_conn_write_stream_versioned` to set the correct struct
  5456. * version.
  5457. */
  5458. #define ngtcp2_conn_write_stream(CONN, PATH, PI, DEST, DESTLEN, PDATALEN, \
  5459. FLAGS, STREAM_ID, DATA, DATALEN, TS) \
  5460. ngtcp2_conn_write_stream_versioned( \
  5461. (CONN), (PATH), NGTCP2_PKT_INFO_VERSION, (PI), (DEST), (DESTLEN), \
  5462. (PDATALEN), (FLAGS), (STREAM_ID), (DATA), (DATALEN), (TS))
  5463. /*
  5464. * `ngtcp2_conn_writev_stream` is a wrapper around
  5465. * `ngtcp2_conn_writev_stream_versioned` to set the correct struct
  5466. * version.
  5467. */
  5468. #define ngtcp2_conn_writev_stream(CONN, PATH, PI, DEST, DESTLEN, PDATALEN, \
  5469. FLAGS, STREAM_ID, DATAV, DATAVCNT, TS) \
  5470. ngtcp2_conn_writev_stream_versioned( \
  5471. (CONN), (PATH), NGTCP2_PKT_INFO_VERSION, (PI), (DEST), (DESTLEN), \
  5472. (PDATALEN), (FLAGS), (STREAM_ID), (DATAV), (DATAVCNT), (TS))
  5473. /*
  5474. * `ngtcp2_conn_write_datagram` is a wrapper around
  5475. * `ngtcp2_conn_write_datagram_versioned` to set the correct struct
  5476. * version.
  5477. */
  5478. #define ngtcp2_conn_write_datagram(CONN, PATH, PI, DEST, DESTLEN, PACCEPTED, \
  5479. FLAGS, DGRAM_ID, DATA, DATALEN, TS) \
  5480. ngtcp2_conn_write_datagram_versioned( \
  5481. (CONN), (PATH), NGTCP2_PKT_INFO_VERSION, (PI), (DEST), (DESTLEN), \
  5482. (PACCEPTED), (FLAGS), (DGRAM_ID), (DATA), (DATALEN), (TS))
  5483. /*
  5484. * `ngtcp2_conn_writev_datagram` is a wrapper around
  5485. * `ngtcp2_conn_writev_datagram_versioned` to set the correct struct
  5486. * version.
  5487. */
  5488. #define ngtcp2_conn_writev_datagram(CONN, PATH, PI, DEST, DESTLEN, PACCEPTED, \
  5489. FLAGS, DGRAM_ID, DATAV, DATAVCNT, TS) \
  5490. ngtcp2_conn_writev_datagram_versioned( \
  5491. (CONN), (PATH), NGTCP2_PKT_INFO_VERSION, (PI), (DEST), (DESTLEN), \
  5492. (PACCEPTED), (FLAGS), (DGRAM_ID), (DATAV), (DATAVCNT), (TS))
  5493. /*
  5494. * `ngtcp2_conn_write_connection_close` is a wrapper around
  5495. * `ngtcp2_conn_write_connection_close_versioned` to set the correct
  5496. * struct version.
  5497. */
  5498. #define ngtcp2_conn_write_connection_close(CONN, PATH, PI, DEST, DESTLEN, \
  5499. CCERR, TS) \
  5500. ngtcp2_conn_write_connection_close_versioned( \
  5501. (CONN), (PATH), NGTCP2_PKT_INFO_VERSION, (PI), (DEST), (DESTLEN), \
  5502. (CCERR), (TS))
  5503. /*
  5504. * `ngtcp2_transport_params_encode` is a wrapper around
  5505. * `ngtcp2_transport_params_encode_versioned` to set the correct
  5506. * struct version.
  5507. */
  5508. #define ngtcp2_transport_params_encode(DEST, DESTLEN, PARAMS) \
  5509. ngtcp2_transport_params_encode_versioned( \
  5510. (DEST), (DESTLEN), NGTCP2_TRANSPORT_PARAMS_VERSION, (PARAMS))
  5511. /*
  5512. * `ngtcp2_transport_params_decode` is a wrapper around
  5513. * `ngtcp2_transport_params_decode_versioned` to set the correct
  5514. * struct version.
  5515. */
  5516. #define ngtcp2_transport_params_decode(PARAMS, DATA, DATALEN) \
  5517. ngtcp2_transport_params_decode_versioned(NGTCP2_TRANSPORT_PARAMS_VERSION, \
  5518. (PARAMS), (DATA), (DATALEN))
  5519. /*
  5520. * `ngtcp2_conn_client_new` is a wrapper around
  5521. * `ngtcp2_conn_client_new_versioned` to set the correct struct
  5522. * version.
  5523. */
  5524. #define ngtcp2_conn_client_new(PCONN, DCID, SCID, PATH, VERSION, CALLBACKS, \
  5525. SETTINGS, PARAMS, MEM, USER_DATA) \
  5526. ngtcp2_conn_client_new_versioned( \
  5527. (PCONN), (DCID), (SCID), (PATH), (VERSION), NGTCP2_CALLBACKS_VERSION, \
  5528. (CALLBACKS), NGTCP2_SETTINGS_VERSION, (SETTINGS), \
  5529. NGTCP2_TRANSPORT_PARAMS_VERSION, (PARAMS), (MEM), (USER_DATA))
  5530. /*
  5531. * `ngtcp2_conn_server_new` is a wrapper around
  5532. * `ngtcp2_conn_server_new_versioned` to set the correct struct
  5533. * version.
  5534. */
  5535. #define ngtcp2_conn_server_new(PCONN, DCID, SCID, PATH, VERSION, CALLBACKS, \
  5536. SETTINGS, PARAMS, MEM, USER_DATA) \
  5537. ngtcp2_conn_server_new_versioned( \
  5538. (PCONN), (DCID), (SCID), (PATH), (VERSION), NGTCP2_CALLBACKS_VERSION, \
  5539. (CALLBACKS), NGTCP2_SETTINGS_VERSION, (SETTINGS), \
  5540. NGTCP2_TRANSPORT_PARAMS_VERSION, (PARAMS), (MEM), (USER_DATA))
  5541. /*
  5542. * `ngtcp2_conn_set_local_transport_params` is a wrapper around
  5543. * `ngtcp2_conn_set_local_transport_params_versioned` to set the
  5544. * correct struct version.
  5545. */
  5546. #define ngtcp2_conn_set_local_transport_params(CONN, PARAMS) \
  5547. ngtcp2_conn_set_local_transport_params_versioned( \
  5548. (CONN), NGTCP2_TRANSPORT_PARAMS_VERSION, (PARAMS))
  5549. /*
  5550. * `ngtcp2_transport_params_default` is a wrapper around
  5551. * `ngtcp2_transport_params_default_versioned` to set the correct
  5552. * struct version.
  5553. */
  5554. #define ngtcp2_transport_params_default(PARAMS) \
  5555. ngtcp2_transport_params_default_versioned(NGTCP2_TRANSPORT_PARAMS_VERSION, \
  5556. (PARAMS))
  5557. /*
  5558. * `ngtcp2_conn_get_conn_info` is a wrapper around
  5559. * `ngtcp2_conn_get_conn_info_versioned` to set the correct struct
  5560. * version.
  5561. */
  5562. #define ngtcp2_conn_get_conn_info(CONN, CINFO) \
  5563. ngtcp2_conn_get_conn_info_versioned((CONN), NGTCP2_CONN_INFO_VERSION, (CINFO))
  5564. /*
  5565. * `ngtcp2_settings_default` is a wrapper around
  5566. * `ngtcp2_settings_default_versioned` to set the correct struct
  5567. * version.
  5568. */
  5569. #define ngtcp2_settings_default(SETTINGS) \
  5570. ngtcp2_settings_default_versioned(NGTCP2_SETTINGS_VERSION, (SETTINGS))
  5571. #ifdef _MSC_VER
  5572. # pragma warning(pop)
  5573. #endif
  5574. #ifdef __cplusplus
  5575. }
  5576. #endif
  5577. #endif /* NGTCP2_H */