developer.texi 356 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930
  1. @c ***********************************************************************
  2. @node GNUnet Developer Handbook
  3. @chapter GNUnet Developer Handbook
  4. This book is intended to be an introduction for programmers that want to
  5. extend the GNUnet framework. GNUnet is more than a simple peer-to-peer
  6. application.
  7. For developers, GNUnet is:
  8. @itemize @bullet
  9. @item developed by a community that believes in the GNU philosophy
  10. @item Free Software (Free as in Freedom), licensed under the
  11. GNU Affero General Public License
  12. (@uref{https://www.gnu.org/licenses/licenses.html#AGPL})
  13. @item A set of standards, including coding conventions and
  14. architectural rules
  15. @item A set of layered protocols, both specifying the communication
  16. between peers as well as the communication between components
  17. of a single peer
  18. @item A set of libraries with well-defined APIs suitable for
  19. writing extensions
  20. @end itemize
  21. In particular, the architecture specifies that a peer consists of many
  22. processes communicating via protocols. Processes can be written in almost
  23. any language.
  24. @code{C}, @code{Java} and @code{Guile} APIs exist for accessing existing
  25. services and for writing extensions.
  26. It is possible to write extensions in other languages by
  27. implementing the necessary IPC protocols.
  28. GNUnet can be extended and improved along many possible dimensions, and
  29. anyone interested in Free Software and Freedom-enhancing Networking is
  30. welcome to join the effort. This Developer Handbook attempts to provide
  31. an initial introduction to some of the key design choices and central
  32. components of the system.
  33. This part of the GNUnet documentation is far from complete,
  34. and we welcome informed contributions, be it in the form of
  35. new chapters, sections or insightful comments.
  36. @menu
  37. * Developer Introduction::
  38. * Internal dependencies::
  39. * Code overview::
  40. * System Architecture::
  41. * Subsystem stability::
  42. * Naming conventions and coding style guide::
  43. * Build-system::
  44. * Developing extensions for GNUnet using the gnunet-ext template::
  45. * Writing testcases::
  46. * Building GNUnet and its dependencies::
  47. * TESTING library::
  48. * Performance regression analysis with Gauger::
  49. * TESTBED Subsystem::
  50. * libgnunetutil::
  51. * Automatic Restart Manager (ARM)::
  52. * TRANSPORT Subsystem::
  53. * NAT library::
  54. * Distance-Vector plugin::
  55. * SMTP plugin::
  56. * Bluetooth plugin::
  57. * WLAN plugin::
  58. * ATS Subsystem::
  59. * CORE Subsystem::
  60. * CADET Subsystem::
  61. * NSE Subsystem::
  62. * HOSTLIST Subsystem::
  63. * IDENTITY Subsystem::
  64. * NAMESTORE Subsystem::
  65. * PEERINFO Subsystem::
  66. * PEERSTORE Subsystem::
  67. * SET Subsystem::
  68. * STATISTICS Subsystem::
  69. * Distributed Hash Table (DHT)::
  70. * GNU Name System (GNS)::
  71. * GNS Namecache::
  72. * REVOCATION Subsystem::
  73. * File-sharing (FS) Subsystem::
  74. * REGEX Subsystem::
  75. * REST Subsystem::
  76. * RPS Subsystem::
  77. @end menu
  78. @node Developer Introduction
  79. @section Developer Introduction
  80. This Developer Handbook is intended as first introduction to GNUnet for
  81. new developers that want to extend the GNUnet framework. After the
  82. introduction, each of the GNUnet subsystems (directories in the
  83. @file{src/} tree) is (supposed to be) covered in its own chapter. In
  84. addition to this documentation, GNUnet developers should be aware of the
  85. services available on the GNUnet server to them.
  86. New developers can have a look a the GNUnet tutorials for C and java
  87. available in the @file{src/} directory of the repository or under the
  88. following links:
  89. @c ** FIXME: Link to files in source, not online.
  90. @c ** FIXME: Where is the Java tutorial?
  91. @itemize @bullet
  92. @item @xref{Top, Introduction,, gnunet-c-tutorial, The GNUnet C Tutorial}.
  93. @item @uref{https://docs.gnunet.org/tutorial/gnunet-tutorial.html, GNUnet C tutorial}
  94. @item GNUnet Java tutorial
  95. @end itemize
  96. In addition to the GNUnet Reference Documentation you are reading,
  97. the GNUnet server at @uref{https://gnunet.org} contains
  98. various resources for GNUnet developers and those
  99. who aspire to become regular contributors.
  100. They are all conveniently reachable via the "Developer"
  101. entry in the navigation menu. Some additional tools (such as static
  102. analysis reports) require a special developer access to perform certain
  103. operations. If you want (or require) access, you should contact
  104. @uref{http://grothoff.org/christian/, Christian Grothoff},
  105. GNUnet's maintainer.
  106. @c FIXME: A good part of this belongs on the website or should be
  107. @c extended in subsections explaining usage of this. A simple list
  108. @c is just taking space people have to read.
  109. The public subsystems on the GNUnet server that help developers are:
  110. @itemize @bullet
  111. @item The version control system (git) keeps our code and enables
  112. distributed development.
  113. It is publicly accessible at @uref{https://git.gnunet.org/}.
  114. Only developers with write access can commit code, everyone else is
  115. encouraged to submit patches to the GNUnet-developers mailinglist:
  116. @uref{https://lists.gnu.org/mailman/listinfo/gnunet-developers, https://lists.gnu.org/mailman/listinfo/gnunet-developers}
  117. @item The bugtracking system (Mantis).
  118. We use it to track feature requests, open bug reports and their
  119. resolutions.
  120. It can be accessed at
  121. @uref{https://bugs.gnunet.org/, https://bugs.gnunet.org/}.
  122. Anyone can report bugs.
  123. @item The current quality of our automated test suite is assessed using
  124. code coverage analysis. Testcases that
  125. improve our code coverage are always welcome.
  126. @item We try to automatically find bugs using a static analysis using
  127. various tools. Note that not everything that is flagged by the
  128. analysis is a bug, sometimes even good code can be marked as possibly
  129. problematic. Nevertheless, developers are encouraged to at least be
  130. aware of all issues in their code that are listed.
  131. @item We use Gauger for automatic performance regression visualization.
  132. @c FIXME: LINK!
  133. Details on how to use Gauger are here.
  134. @end itemize
  135. @c ***********************************************************************
  136. @menu
  137. * Project overview::
  138. @end menu
  139. @node Project overview
  140. @subsection Project overview
  141. The GNUnet project consists at this point of several sub-projects. This
  142. section is supposed to give an initial overview about the various
  143. sub-projects. Note that this description also lists projects that are far
  144. from complete, including even those that have literally not a single line
  145. of code in them yet.
  146. GNUnet sub-projects in order of likely relevance are currently:
  147. @table @asis
  148. @item @command{gnunet}
  149. Core of the P2P framework, including file-sharing, VPN and
  150. chat applications; this is what the Developer Handbook covers mostly
  151. @item @command{gnunet-gtk}
  152. Gtk+-based user interfaces, including:
  153. @itemize @bullet
  154. @item @command{gnunet-fs-gtk} (file-sharing),
  155. @item @command{gnunet-statistics-gtk} (statistics over time),
  156. @item @command{gnunet-peerinfo-gtk}
  157. (information about current connections and known peers),
  158. @item @command{gnunet-namestore-gtk} (GNS record editor),
  159. @item @command{gnunet-conversation-gtk} (voice chat GUI) and
  160. @item @command{gnunet-setup} (setup tool for "everything")
  161. @end itemize
  162. @item @command{gnunet-fuse}
  163. Mounting directories shared via GNUnet's file-sharing
  164. on GNU/Linux distributions
  165. @item @command{gnunet-update}
  166. Installation and update tool
  167. @item @command{gnunet-ext}
  168. Template for starting 'external' GNUnet projects
  169. @item @command{gnunet-java}
  170. Java APIs for writing GNUnet services and applications
  171. @item @command{gnunet-java-ext}
  172. @item @command{eclectic}
  173. Code to run GNUnet nodes on testbeds for research, development,
  174. testing and evaluation
  175. @c ** FIXME: Solve the status and location of gnunet-qt
  176. @item @command{gnunet-qt}
  177. Qt-based GNUnet GUI (is it deprecated?)
  178. @item @command{gnunet-cocoa}
  179. cocoa-based GNUnet GUI (is it deprecated?)
  180. @item @command{gnunet-guile}
  181. Guile bindings for GNUnet
  182. @item @command{gnunet-python}
  183. Python bindings for GNUnet
  184. @end table
  185. We are also working on various supporting libraries and tools:
  186. @c ** FIXME: What about gauger, and what about libmwmodem?
  187. @table @asis
  188. @item @command{libextractor}
  189. GNU libextractor (meta data extraction)
  190. @item @command{libmicrohttpd}
  191. GNU libmicrohttpd (embedded HTTP(S) server library)
  192. @item @command{gauger}
  193. Tool for performance regression analysis
  194. @item @command{monkey}
  195. Tool for automated debugging of distributed systems
  196. @item @command{libmwmodem}
  197. Library for accessing satellite connection quality reports
  198. @item @command{libgnurl}
  199. gnURL (feature-restricted variant of cURL/libcurl)
  200. @item @command{www}
  201. the gnunet.org website (Jinja2 based)
  202. @item @command{bibliography}
  203. Our collected bibliography, papers, references, and so forth
  204. @item @command{gnunet-videos-}
  205. Videos about and around GNUnet activities
  206. @end table
  207. Finally, there are various external projects (see links for a list of
  208. those that have a public website) which build on top of the GNUnet
  209. framework.
  210. @c ***********************************************************************
  211. @node Internal dependencies
  212. @section Internal dependencies
  213. This section tries to give an overview of what processes a typical GNUnet
  214. peer running a particular application would consist of. All of the
  215. processes listed here should be automatically started by
  216. @command{gnunet-arm -s}.
  217. The list is given as a rough first guide to users for failure diagnostics.
  218. Ideally, end-users should never have to worry about these internal
  219. dependencies.
  220. In terms of internal dependencies, a minimum file-sharing system consists
  221. of the following GNUnet processes (in order of dependency):
  222. @itemize @bullet
  223. @item gnunet-service-arm
  224. @item gnunet-service-resolver (required by all)
  225. @item gnunet-service-statistics (required by all)
  226. @item gnunet-service-peerinfo
  227. @item gnunet-service-transport (requires peerinfo)
  228. @item gnunet-service-core (requires transport)
  229. @item gnunet-daemon-hostlist (requires core)
  230. @item gnunet-daemon-topology (requires hostlist, peerinfo)
  231. @item gnunet-service-datastore
  232. @item gnunet-service-dht (requires core)
  233. @item gnunet-service-identity
  234. @item gnunet-service-fs (requires identity, mesh, dht, datastore, core)
  235. @end itemize
  236. @noindent
  237. A minimum VPN system consists of the following GNUnet processes (in
  238. order of dependency):
  239. @itemize @bullet
  240. @item gnunet-service-arm
  241. @item gnunet-service-resolver (required by all)
  242. @item gnunet-service-statistics (required by all)
  243. @item gnunet-service-peerinfo
  244. @item gnunet-service-transport (requires peerinfo)
  245. @item gnunet-service-core (requires transport)
  246. @item gnunet-daemon-hostlist (requires core)
  247. @item gnunet-service-dht (requires core)
  248. @item gnunet-service-mesh (requires dht, core)
  249. @item gnunet-service-dns (requires dht)
  250. @item gnunet-service-regex (requires dht)
  251. @item gnunet-service-vpn (requires regex, dns, mesh, dht)
  252. @end itemize
  253. @noindent
  254. A minimum GNS system consists of the following GNUnet processes (in
  255. order of dependency):
  256. @itemize @bullet
  257. @item gnunet-service-arm
  258. @item gnunet-service-resolver (required by all)
  259. @item gnunet-service-statistics (required by all)
  260. @item gnunet-service-peerinfo
  261. @item gnunet-service-transport (requires peerinfo)
  262. @item gnunet-service-core (requires transport)
  263. @item gnunet-daemon-hostlist (requires core)
  264. @item gnunet-service-dht (requires core)
  265. @item gnunet-service-mesh (requires dht, core)
  266. @item gnunet-service-dns (requires dht)
  267. @item gnunet-service-regex (requires dht)
  268. @item gnunet-service-vpn (requires regex, dns, mesh, dht)
  269. @item gnunet-service-identity
  270. @item gnunet-service-namestore (requires identity)
  271. @item gnunet-service-gns (requires vpn, dns, dht, namestore, identity)
  272. @end itemize
  273. @c ***********************************************************************
  274. @node Code overview
  275. @section Code overview
  276. This section gives a brief overview of the GNUnet source code.
  277. Specifically, we sketch the function of each of the subdirectories in
  278. the @file{gnunet/src/} directory. The order given is roughly bottom-up
  279. (in terms of the layers of the system).
  280. @table @asis
  281. @item @file{util/} --- libgnunetutil
  282. Library with general utility functions, all
  283. GNUnet binaries link against this library. Anything from memory
  284. allocation and data structures to cryptography and inter-process
  285. communication. The goal is to provide an OS-independent interface and
  286. more 'secure' or convenient implementations of commonly used primitives.
  287. The API is spread over more than a dozen headers, developers should study
  288. those closely to avoid duplicating existing functions.
  289. @pxref{libgnunetutil}.
  290. @item @file{hello/} --- libgnunethello
  291. HELLO messages are used to
  292. describe under which addresses a peer can be reached (for example,
  293. protocol, IP, port). This library manages parsing and generating of HELLO
  294. messages.
  295. @item @file{block/} --- libgnunetblock
  296. The DHT and other components of GNUnet
  297. store information in units called 'blocks'. Each block has a type and the
  298. type defines a particular format and how that binary format is to be
  299. linked to a hash code (the key for the DHT and for databases). The block
  300. library is a wrapper around block plugins which provide the necessary
  301. functions for each block type.
  302. @item @file{statistics/} --- statistics service
  303. The statistics service enables associating
  304. values (of type uint64_t) with a component name and a string. The main
  305. uses is debugging (counting events), performance tracking and user
  306. entertainment (what did my peer do today?).
  307. @item @file{arm/} --- Automatic Restart Manager (ARM)
  308. The automatic-restart-manager (ARM) service
  309. is the GNUnet master service. Its role is to start gnunet-services, to
  310. re-start them when they crashed and finally to shut down the system when
  311. requested.
  312. @item @file{peerinfo/} --- peerinfo service
  313. The peerinfo service keeps track of which peers are known
  314. to the local peer and also tracks the validated addresses for each peer
  315. (in the form of a HELLO message) for each of those peers. The peer is not
  316. necessarily connected to all peers known to the peerinfo service.
  317. Peerinfo provides persistent storage for peer identities --- peers are
  318. not forgotten just because of a system restart.
  319. @item @file{datacache/} --- libgnunetdatacache
  320. The datacache library provides (temporary) block storage for the DHT.
  321. Existing plugins can store blocks in Sqlite, Postgres or MySQL databases.
  322. All data stored in the cache is lost when the peer is stopped or
  323. restarted (datacache uses temporary tables).
  324. @item @file{datastore/} --- datastore service
  325. The datastore service stores file-sharing blocks in
  326. databases for extended periods of time. In contrast to the datacache, data
  327. is not lost when peers restart. However, quota restrictions may still
  328. cause old, expired or low-priority data to be eventually discarded.
  329. Existing plugins can store blocks in Sqlite, Postgres or MySQL databases.
  330. @item @file{template/} --- service template
  331. Template for writing a new service. Does nothing.
  332. @item @file{ats/} --- Automatic Transport Selection
  333. The automatic transport selection (ATS) service
  334. is responsible for deciding which address (i.e.
  335. which transport plugin) should be used for communication with other peers,
  336. and at what bandwidth.
  337. @item @file{nat/} --- libgnunetnat
  338. Library that provides basic functions for NAT traversal.
  339. The library supports NAT traversal with
  340. manual hole-punching by the user, UPnP and ICMP-based autonomous NAT
  341. traversal. The library also includes an API for testing if the current
  342. configuration works and the @code{gnunet-nat-server} which provides an
  343. external service to test the local configuration.
  344. @item @file{fragmentation/} --- libgnunetfragmentation
  345. Some transports (UDP and WLAN, mostly) have restrictions on the maximum
  346. transfer unit (MTU) for packets. The fragmentation library can be used to
  347. break larger packets into chunks of at most 1k and transmit the resulting
  348. fragments reliably (with acknowledgment, retransmission, timeouts,
  349. etc.).
  350. @item @file{transport/} --- transport service
  351. The transport service is responsible for managing the
  352. basic P2P communication. It uses plugins to support P2P communication
  353. over TCP, UDP, HTTP, HTTPS and other protocols.The transport service
  354. validates peer addresses, enforces bandwidth restrictions, limits the
  355. total number of connections and enforces connectivity restrictions (i.e.
  356. friends-only).
  357. @item @file{peerinfo-tool/} --- gnunet-peerinfo
  358. This directory contains the gnunet-peerinfo binary which can be used to
  359. inspect the peers and HELLOs known to the peerinfo service.
  360. @item @file{core/}
  361. The core service is responsible for establishing encrypted, authenticated
  362. connections with other peers, encrypting and decrypting messages and
  363. forwarding messages to higher-level services that are interested in them.
  364. @item @file{testing/} --- libgnunettesting
  365. The testing library allows starting (and stopping) peers
  366. for writing testcases.
  367. It also supports automatic generation of configurations for peers
  368. ensuring that the ports and paths are disjoint. libgnunettesting is also
  369. the foundation for the testbed service
  370. @item @file{testbed/} --- testbed service
  371. The testbed service is used for creating small or large scale deployments
  372. of GNUnet peers for evaluation of protocols.
  373. It facilitates peer deployments on multiple
  374. hosts (for example, in a cluster) and establishing various network
  375. topologies (both underlay and overlay).
  376. @item @file{nse/} --- Network Size Estimation
  377. The network size estimation (NSE) service
  378. implements a protocol for (securely) estimating the current size of the
  379. P2P network.
  380. @item @file{dht/} --- distributed hash table
  381. The distributed hash table (DHT) service provides a
  382. distributed implementation of a hash table to store blocks under hash
  383. keys in the P2P network.
  384. @item @file{hostlist/} --- hostlist service
  385. The hostlist service allows learning about
  386. other peers in the network by downloading HELLO messages from an HTTP
  387. server, can be configured to run such an HTTP server and also implements
  388. a P2P protocol to advertise and automatically learn about other peers
  389. that offer a public hostlist server.
  390. @item @file{topology/} --- topology service
  391. The topology service is responsible for
  392. maintaining the mesh topology. It tries to maintain connections to friends
  393. (depending on the configuration) and also tries to ensure that the peer
  394. has a decent number of active connections at all times. If necessary, new
  395. connections are added. All peers should run the topology service,
  396. otherwise they may end up not being connected to any other peer (unless
  397. some other service ensures that core establishes the required
  398. connections). The topology service also tells the transport service which
  399. connections are permitted (for friend-to-friend networking)
  400. @item @file{fs/} --- file-sharing
  401. The file-sharing (FS) service implements GNUnet's
  402. file-sharing application. Both anonymous file-sharing (using gap) and
  403. non-anonymous file-sharing (using dht) are supported.
  404. @item @file{cadet/} --- cadet service
  405. The CADET service provides a general-purpose routing abstraction to create
  406. end-to-end encrypted tunnels in mesh networks. We wrote a paper
  407. documenting key aspects of the design.
  408. @item @file{tun/} --- libgnunettun
  409. Library for building IPv4, IPv6 packets and creating
  410. checksums for UDP, TCP and ICMP packets. The header
  411. defines C structs for common Internet packet formats and in particular
  412. structs for interacting with TUN (virtual network) interfaces.
  413. @item @file{mysql/} --- libgnunetmysql
  414. Library for creating and executing prepared MySQL
  415. statements and to manage the connection to the MySQL database.
  416. Essentially a lightweight wrapper for the interaction between GNUnet
  417. components and libmysqlclient.
  418. @item @file{dns/}
  419. Service that allows intercepting and modifying DNS requests of
  420. the local machine. Currently used for IPv4-IPv6 protocol translation
  421. (DNS-ALG) as implemented by "pt/" and for the GNUnet naming system. The
  422. service can also be configured to offer an exit service for DNS traffic.
  423. @item @file{vpn/} --- VPN service
  424. The virtual public network (VPN) service provides a virtual
  425. tunnel interface (VTUN) for IP routing over GNUnet.
  426. Needs some other peers to run an "exit" service to work.
  427. Can be activated using the "gnunet-vpn" tool or integrated with DNS using
  428. the "pt" daemon.
  429. @item @file{exit/}
  430. Daemon to allow traffic from the VPN to exit this
  431. peer to the Internet or to specific IP-based services of the local peer.
  432. Currently, an exit service can only be restricted to IPv4 or IPv6, not to
  433. specific ports and or IP address ranges. If this is not acceptable,
  434. additional firewall rules must be added manually. exit currently only
  435. works for normal UDP, TCP and ICMP traffic; DNS queries need to leave the
  436. system via a DNS service.
  437. @item @file{pt/}
  438. protocol translation daemon. This daemon enables 4-to-6,
  439. 6-to-4, 4-over-6 or 6-over-4 transitions for the local system. It
  440. essentially uses "DNS" to intercept DNS replies and then maps results to
  441. those offered by the VPN, which then sends them using mesh to some daemon
  442. offering an appropriate exit service.
  443. @item @file{identity/}
  444. Management of egos (alter egos) of a user; identities are
  445. essentially named ECC private keys and used for zones in the GNU name
  446. system and for namespaces in file-sharing, but might find other uses later
  447. @item @file{revocation/}
  448. Key revocation service, can be used to revoke the
  449. private key of an identity if it has been compromised
  450. @item @file{namecache/}
  451. Cache for resolution results for the GNU name system;
  452. data is encrypted and can be shared among users,
  453. loss of the data should ideally only result in a
  454. performance degradation (persistence not required)
  455. @item @file{namestore/}
  456. Database for the GNU name system with per-user private information,
  457. persistence required
  458. @item @file{gns/}
  459. GNU name system, a GNU approach to DNS and PKI.
  460. @item @file{dv/}
  461. A plugin for distance-vector (DV)-based routing.
  462. DV consists of a service and a transport plugin to provide peers
  463. with the illusion of a direct P2P connection for connections
  464. that use multiple (typically up to 3) hops in the actual underlay network.
  465. @item @file{regex/}
  466. Service for the (distributed) evaluation of regular expressions.
  467. @item @file{scalarproduct/}
  468. The scalar product service offers an API to perform a secure multiparty
  469. computation which calculates a scalar product between two peers
  470. without exposing the private input vectors of the peers to each other.
  471. @item @file{consensus/}
  472. The consensus service will allow a set of peers to agree
  473. on a set of values via a distributed set union computation.
  474. @item @file{rest/}
  475. The rest API allows access to GNUnet services using RESTful interaction.
  476. The services provide plugins that can exposed by the rest server.
  477. @c FIXME: Where did this disappear to?
  478. @c @item @file{experimentation/}
  479. @c The experimentation daemon coordinates distributed
  480. @c experimentation to evaluate transport and ATS properties.
  481. @end table
  482. @c ***********************************************************************
  483. @node System Architecture
  484. @section System Architecture
  485. @c FIXME: For those irritated by the textflow, we are missing images here,
  486. @c in the short term we should add them back, in the long term this should
  487. @c work without images or have images with alt-text.
  488. GNUnet developers like LEGOs. The blocks are indestructible, can be
  489. stacked together to construct complex buildings and it is generally easy
  490. to swap one block for a different one that has the same shape. GNUnet's
  491. architecture is based on LEGOs:
  492. @image{images/service_lego_block,5in,,picture of a LEGO block stack - 3 APIs upon IPC/network protocol provided by a service}
  493. This chapter documents the GNUnet LEGO system, also known as GNUnet's
  494. system architecture.
  495. The most common GNUnet component is a service. Services offer an API (or
  496. several, depending on what you count as "an API") which is implemented as
  497. a library. The library communicates with the main process of the service
  498. using a service-specific network protocol. The main process of the service
  499. typically doesn't fully provide everything that is needed --- it has holes
  500. to be filled by APIs to other services.
  501. A special kind of component in GNUnet are user interfaces and daemons.
  502. Like services, they have holes to be filled by APIs of other services.
  503. Unlike services, daemons do not implement their own network protocol and
  504. they have no API:
  505. @image{images/daemon_lego_block,5in,,A daemon in GNUnet is a component that does not offer an API for others to build upon}
  506. The GNUnet system provides a range of services, daemons and user
  507. interfaces, which are then combined into a layered GNUnet instance (also
  508. known as a peer).
  509. @image{images/service_stack,5in,,A GNUnet peer consists of many layers of services}
  510. Note that while it is generally possible to swap one service for another
  511. compatible service, there is often only one implementation. However,
  512. during development we often have a "new" version of a service in parallel
  513. with an "old" version. While the "new" version is not working, developers
  514. working on other parts of the service can continue their development by
  515. simply using the "old" service. Alternative design ideas can also be
  516. easily investigated by swapping out individual components. This is
  517. typically achieved by simply changing the name of the "BINARY" in the
  518. respective configuration section.
  519. Key properties of GNUnet services are that they must be separate
  520. processes and that they must protect themselves by applying tight error
  521. checking against the network protocol they implement (thereby achieving a
  522. certain degree of robustness).
  523. On the other hand, the APIs are implemented to tolerate failures of the
  524. service, isolating their host process from errors by the service. If the
  525. service process crashes, other services and daemons around it should not
  526. also fail, but instead wait for the service process to be restarted by
  527. ARM.
  528. @c ***********************************************************************
  529. @node Subsystem stability
  530. @section Subsystem stability
  531. This section documents the current stability of the various GNUnet
  532. subsystems. Stability here describes the expected degree of compatibility
  533. with future versions of GNUnet. For each subsystem we distinguish between
  534. compatibility on the P2P network level (communication protocol between
  535. peers), the IPC level (communication between the service and the service
  536. library) and the API level (stability of the API). P2P compatibility is
  537. relevant in terms of which applications are likely going to be able to
  538. communicate with future versions of the network. IPC communication is
  539. relevant for the implementation of language bindings that re-implement the
  540. IPC messages. Finally, API compatibility is relevant to developers that
  541. hope to be able to avoid changes to applications build on top of the APIs
  542. of the framework.
  543. The following table summarizes our current view of the stability of the
  544. respective protocols or APIs:
  545. @multitable @columnfractions .20 .20 .20 .20
  546. @headitem Subsystem @tab P2P @tab IPC @tab C API
  547. @item util @tab n/a @tab n/a @tab stable
  548. @item arm @tab n/a @tab stable @tab stable
  549. @item ats @tab n/a @tab unstable @tab testing
  550. @item block @tab n/a @tab n/a @tab stable
  551. @item cadet @tab testing @tab testing @tab testing
  552. @item consensus @tab experimental @tab experimental @tab experimental
  553. @item core @tab stable @tab stable @tab stable
  554. @item datacache @tab n/a @tab n/a @tab stable
  555. @item datastore @tab n/a @tab stable @tab stable
  556. @item dht @tab stable @tab stable @tab stable
  557. @item dns @tab stable @tab stable @tab stable
  558. @item dv @tab testing @tab testing @tab n/a
  559. @item exit @tab testing @tab n/a @tab n/a
  560. @item fragmentation @tab stable @tab n/a @tab stable
  561. @item fs @tab stable @tab stable @tab stable
  562. @item gns @tab stable @tab stable @tab stable
  563. @item hello @tab n/a @tab n/a @tab testing
  564. @item hostlist @tab stable @tab stable @tab n/a
  565. @item identity @tab stable @tab stable @tab n/a
  566. @item multicast @tab experimental @tab experimental @tab experimental
  567. @item mysql @tab stable @tab n/a @tab stable
  568. @item namestore @tab n/a @tab stable @tab stable
  569. @item nat @tab n/a @tab n/a @tab stable
  570. @item nse @tab stable @tab stable @tab stable
  571. @item peerinfo @tab n/a @tab stable @tab stable
  572. @item psyc @tab experimental @tab experimental @tab experimental
  573. @item pt @tab n/a @tab n/a @tab n/a
  574. @item regex @tab stable @tab stable @tab stable
  575. @item revocation @tab stable @tab stable @tab stable
  576. @item social @tab experimental @tab experimental @tab experimental
  577. @item statistics @tab n/a @tab stable @tab stable
  578. @item testbed @tab n/a @tab testing @tab testing
  579. @item testing @tab n/a @tab n/a @tab testing
  580. @item topology @tab n/a @tab n/a @tab n/a
  581. @item transport @tab stable @tab stable @tab stable
  582. @item tun @tab n/a @tab n/a @tab stable
  583. @item vpn @tab testing @tab n/a @tab n/a
  584. @end multitable
  585. Here is a rough explanation of the values:
  586. @table @samp
  587. @item stable
  588. No incompatible changes are planned at this time; for IPC/APIs, if
  589. there are incompatible changes, they will be minor and might only require
  590. minimal changes to existing code; for P2P, changes will be avoided if at
  591. all possible for the 0.10.x-series
  592. @item testing
  593. No incompatible changes are
  594. planned at this time, but the code is still known to be in flux; so while
  595. we have no concrete plans, our expectation is that there will still be
  596. minor modifications; for P2P, changes will likely be extensions that
  597. should not break existing code
  598. @item unstable
  599. Changes are planned and will happen; however, they
  600. will not be totally radical and the result should still resemble what is
  601. there now; nevertheless, anticipated changes will break protocol/API
  602. compatibility
  603. @item experimental
  604. Changes are planned and the result may look nothing like
  605. what the API/protocol looks like today
  606. @item unknown
  607. Someone should think about where this subsystem headed
  608. @item n/a
  609. This subsystem does not have an API/IPC-protocol/P2P-protocol
  610. @end table
  611. @c ***********************************************************************
  612. @node Naming conventions and coding style guide
  613. @section Naming conventions and coding style guide
  614. Here you can find some rules to help you write code for GNUnet.
  615. @c ***********************************************************************
  616. @menu
  617. * Naming conventions::
  618. * Coding style::
  619. @end menu
  620. @node Naming conventions
  621. @subsection Naming conventions
  622. @c ***********************************************************************
  623. @menu
  624. * include files::
  625. * binaries::
  626. * logging::
  627. * configuration::
  628. * exported symbols::
  629. * private (library-internal) symbols (including structs and macros)::
  630. * testcases::
  631. * performance tests::
  632. * src/ directories::
  633. @end menu
  634. @node include files
  635. @subsubsection include files
  636. @itemize @bullet
  637. @item _lib: library without need for a process
  638. @item _service: library that needs a service process
  639. @item _plugin: plugin definition
  640. @item _protocol: structs used in network protocol
  641. @item exceptions:
  642. @itemize @bullet
  643. @item gnunet_config.h --- generated
  644. @item platform.h --- first included
  645. @item plibc.h --- external library
  646. @item gnunet_common.h --- fundamental routines
  647. @item gnunet_directories.h --- generated
  648. @item gettext.h --- external library
  649. @end itemize
  650. @end itemize
  651. @c ***********************************************************************
  652. @node binaries
  653. @subsubsection binaries
  654. @itemize @bullet
  655. @item gnunet-service-xxx: service process (has listen socket)
  656. @item gnunet-daemon-xxx: daemon process (no listen socket)
  657. @item gnunet-helper-xxx[-yyy]: SUID helper for module xxx
  658. @item gnunet-yyy: command-line tool for end-users
  659. @item libgnunet_plugin_xxx_yyy.so: plugin for API xxx
  660. @item libgnunetxxx.so: library for API xxx
  661. @end itemize
  662. @c ***********************************************************************
  663. @node logging
  664. @subsubsection logging
  665. @itemize @bullet
  666. @item services and daemons use their directory name in
  667. @code{GNUNET_log_setup} (i.e. 'core') and log using
  668. plain 'GNUNET_log'.
  669. @item command-line tools use their full name in
  670. @code{GNUNET_log_setup} (i.e. 'gnunet-publish') and log using
  671. plain 'GNUNET_log'.
  672. @item service access libraries log using
  673. '@code{GNUNET_log_from}' and use '@code{DIRNAME-api}' for the
  674. component (i.e. 'core-api')
  675. @item pure libraries (without associated service) use
  676. '@code{GNUNET_log_from}' with the component set to their
  677. library name (without lib or '@file{.so}'),
  678. which should also be their directory name (i.e. '@file{nat}')
  679. @item plugins should use '@code{GNUNET_log_from}'
  680. with the directory name and the plugin name combined to produce
  681. the component name (i.e. 'transport-tcp').
  682. @item logging should be unified per-file by defining a
  683. @code{LOG} macro with the appropriate arguments,
  684. along these lines:
  685. @example
  686. #define LOG(kind,...)
  687. GNUNET_log_from (kind, "example-api",__VA_ARGS__)
  688. @end example
  689. @end itemize
  690. @c ***********************************************************************
  691. @node configuration
  692. @subsubsection configuration
  693. @itemize @bullet
  694. @item paths (that are substituted in all filenames) are in PATHS
  695. (have as few as possible)
  696. @item all options for a particular module (@file{src/MODULE})
  697. are under @code{[MODULE]}
  698. @item options for a plugin of a module
  699. are under @code{[MODULE-PLUGINNAME]}
  700. @end itemize
  701. @c ***********************************************************************
  702. @node exported symbols
  703. @subsubsection exported symbols
  704. @itemize @bullet
  705. @item must start with @code{GNUNET_modulename_} and be defined in
  706. @file{modulename.c}
  707. @item exceptions: those defined in @file{gnunet_common.h}
  708. @end itemize
  709. @c ***********************************************************************
  710. @node private (library-internal) symbols (including structs and macros)
  711. @subsubsection private (library-internal) symbols (including structs and macros)
  712. @itemize @bullet
  713. @item must NOT start with any prefix
  714. @item must not be exported in a way that linkers could use them or@ other
  715. libraries might see them via headers; they must be either
  716. declared/defined in C source files or in headers that are in the
  717. respective directory under @file{src/modulename/} and NEVER be declared
  718. in @file{src/include/}.
  719. @end itemize
  720. @node testcases
  721. @subsubsection testcases
  722. @itemize @bullet
  723. @item must be called @file{test_module-under-test_case-description.c}
  724. @item "case-description" maybe omitted if there is only one test
  725. @end itemize
  726. @c ***********************************************************************
  727. @node performance tests
  728. @subsubsection performance tests
  729. @itemize @bullet
  730. @item must be called @file{perf_module-under-test_case-description.c}
  731. @item "case-description" maybe omitted if there is only one performance
  732. test
  733. @item Must only be run if @code{HAVE_BENCHMARKS} is satisfied
  734. @end itemize
  735. @c ***********************************************************************
  736. @node src/ directories
  737. @subsubsection src/ directories
  738. @itemize @bullet
  739. @item gnunet-NAME: end-user applications (i.e., gnunet-search, gnunet-arm)
  740. @item gnunet-service-NAME: service processes with accessor library (i.e.,
  741. gnunet-service-arm)
  742. @item libgnunetNAME: accessor library (_service.h-header) or standalone
  743. library (_lib.h-header)
  744. @item gnunet-daemon-NAME: daemon process without accessor library (i.e.,
  745. gnunet-daemon-hostlist) and no GNUnet management port
  746. @item libgnunet_plugin_DIR_NAME: loadable plugins (i.e.,
  747. libgnunet_plugin_transport_tcp)
  748. @end itemize
  749. @cindex Coding style
  750. @node Coding style
  751. @subsection Coding style
  752. @c XXX: Adjust examples to GNU Standards!
  753. @itemize @bullet
  754. @item We follow the GNU Coding Standards (@pxref{Top, The GNU Coding Standards,, standards, The GNU Coding Standards});
  755. @item Indentation is done with spaces, two per level, no tabs;
  756. @item C99 struct initialization is fine;
  757. @item declare only one variable per line, for example:
  758. @noindent
  759. instead of
  760. @example
  761. int i,j;
  762. @end example
  763. @noindent
  764. write:
  765. @example
  766. int i;
  767. int j;
  768. @end example
  769. @c TODO: include actual example from a file in source
  770. @noindent
  771. This helps keep diffs small and forces developers to think precisely about
  772. the type of every variable.
  773. Note that @code{char *} is different from @code{const char*} and
  774. @code{int} is different from @code{unsigned int} or @code{uint32_t}.
  775. Each variable type should be chosen with care.
  776. @item While @code{goto} should generally be avoided, having a
  777. @code{goto} to the end of a function to a block of clean up
  778. statements (free, close, etc.) can be acceptable.
  779. @item Conditions should be written with constants on the left (to avoid
  780. accidental assignment) and with the @code{true} target being either the
  781. @code{error} case or the significantly simpler continuation. For example:
  782. @example
  783. if (0 != stat ("filename,"
  784. &sbuf))
  785. @{
  786. error();
  787. @}
  788. else
  789. @{
  790. /* handle normal case here */
  791. @}
  792. @end example
  793. @noindent
  794. instead of
  795. @example
  796. if (stat ("filename," &sbuf) == 0) @{
  797. /* handle normal case here */
  798. @} else @{
  799. error();
  800. @}
  801. @end example
  802. @noindent
  803. If possible, the error clause should be terminated with a @code{return} (or
  804. @code{goto} to some cleanup routine) and in this case, the @code{else} clause
  805. should be omitted:
  806. @example
  807. if (0 != stat ("filename",
  808. &sbuf))
  809. @{
  810. error();
  811. return;
  812. @}
  813. /* handle normal case here */
  814. @end example
  815. This serves to avoid deep nesting. The 'constants on the left' rule
  816. applies to all constants (including. @code{GNUNET_SCHEDULER_NO_TASK}),
  817. NULL, and enums). With the two above rules (constants on left, errors in
  818. 'true' branch), there is only one way to write most branches correctly.
  819. @item Combined assignments and tests are allowed if they do not hinder
  820. code clarity. For example, one can write:
  821. @example
  822. if (NULL == (value = lookup_function()))
  823. @{
  824. error();
  825. return;
  826. @}
  827. @end example
  828. @item Use @code{break} and @code{continue} wherever possible to avoid
  829. deep(er) nesting. Thus, we would write:
  830. @example
  831. next = head;
  832. while (NULL != (pos = next))
  833. @{
  834. next = pos->next;
  835. if (! should_free (pos))
  836. continue;
  837. GNUNET_CONTAINER_DLL_remove (head,
  838. tail,
  839. pos);
  840. GNUNET_free (pos);
  841. @}
  842. @end example
  843. instead of
  844. @example
  845. next = head; while (NULL != (pos = next)) @{
  846. next = pos->next;
  847. if (should_free (pos)) @{
  848. /* unnecessary nesting! */
  849. GNUNET_CONTAINER_DLL_remove (head, tail, pos);
  850. GNUNET_free (pos);
  851. @}
  852. @}
  853. @end example
  854. @item We primarily use @code{for} and @code{while} loops.
  855. A @code{while} loop is used if the method for advancing in the loop is
  856. not a straightforward increment operation. In particular, we use:
  857. @example
  858. next = head;
  859. while (NULL != (pos = next))
  860. @{
  861. next = pos->next;
  862. if (! should_free (pos))
  863. continue;
  864. GNUNET_CONTAINER_DLL_remove (head,
  865. tail,
  866. pos);
  867. GNUNET_free (pos);
  868. @}
  869. @end example
  870. to free entries in a list (as the iteration changes the structure of the
  871. list due to the free; the equivalent @code{for} loop does no longer
  872. follow the simple @code{for} paradigm of @code{for(INIT;TEST;INC)}).
  873. However, for loops that do follow the simple @code{for} paradigm we do
  874. use @code{for}, even if it involves linked lists:
  875. @example
  876. /* simple iteration over a linked list */
  877. for (pos = head;
  878. NULL != pos;
  879. pos = pos->next)
  880. @{
  881. use (pos);
  882. @}
  883. @end example
  884. @item The first argument to all higher-order functions in GNUnet must be
  885. declared to be of type @code{void *} and is reserved for a closure. We do
  886. not use inner functions, as trampolines would conflict with setups that
  887. use non-executable stacks.
  888. The first statement in a higher-order function, which unusually should
  889. be part of the variable declarations, should assign the
  890. @code{cls} argument to the precise expected type. For example:
  891. @example
  892. int
  893. callback (void *cls,
  894. char *args)
  895. @{
  896. struct Foo *foo = cls;
  897. int other_variables;
  898. /* rest of function */
  899. @}
  900. @end example
  901. @item As shown in the example above, after the return type of a
  902. function there should be a break. Each parameter should
  903. be on a new line.
  904. @item It is good practice to write complex @code{if} expressions instead
  905. of using deeply nested @code{if} statements. However, except for addition
  906. and multiplication, all operators should use parens. This is fine:
  907. @example
  908. if ( (1 == foo) ||
  909. ( (0 == bar) &&
  910. (x != y) ) )
  911. return x;
  912. @end example
  913. However, this is not:
  914. @example
  915. if (1 == foo)
  916. return x;
  917. if (0 == bar && x != y)
  918. return x;
  919. @end example
  920. @noindent
  921. Note that splitting the @code{if} statement above is debatable as the
  922. @code{return x} is a very trivial statement. However, once the logic after
  923. the branch becomes more complicated (and is still identical), the "or"
  924. formulation should be used for sure.
  925. @item There should be two empty lines between the end of the function and
  926. the comments describing the following function. There should be a single
  927. empty line after the initial variable declarations of a function. If a
  928. function has no local variables, there should be no initial empty line. If
  929. a long function consists of several complex steps, those steps might be
  930. separated by an empty line (possibly followed by a comment describing the
  931. following step). The code should not contain empty lines in arbitrary
  932. places; if in doubt, it is likely better to NOT have an empty line (this
  933. way, more code will fit on the screen).
  934. @end itemize
  935. @c ***********************************************************************
  936. @node Build-system
  937. @section Build-system
  938. If you have code that is likely not to compile or build rules you might
  939. want to not trigger for most developers, use @code{if HAVE_EXPERIMENTAL}
  940. in your @file{Makefile.am}.
  941. Then it is OK to (temporarily) add non-compiling (or known-to-not-port)
  942. code.
  943. If you want to compile all testcases but NOT run them, run configure with
  944. the @code{--enable-test-suppression} option.
  945. If you want to run all testcases, including those that take a while, run
  946. configure with the @code{--enable-expensive-testcases} option.
  947. If you want to compile and run benchmarks, run configure with the
  948. @code{--enable-benchmarks} option.
  949. If you want to obtain code coverage results, run configure with the
  950. @code{--enable-coverage} option and run the @file{coverage.sh} script in
  951. the @file{contrib/} directory.
  952. @cindex gnunet-ext
  953. @node Developing extensions for GNUnet using the gnunet-ext template
  954. @section Developing extensions for GNUnet using the gnunet-ext template
  955. For developers who want to write extensions for GNUnet we provide the
  956. gnunet-ext template to provide an easy to use skeleton.
  957. gnunet-ext contains the build environment and template files for the
  958. development of GNUnet services, command line tools, APIs and tests.
  959. First of all you have to obtain gnunet-ext from git:
  960. @example
  961. git clone https://git.gnunet.org/gnunet-ext.git
  962. @end example
  963. The next step is to bootstrap and configure it. For configure you have to
  964. provide the path containing GNUnet with
  965. @code{--with-gnunet=/path/to/gnunet} and the prefix where you want the
  966. install the extension using @code{--prefix=/path/to/install}:
  967. @example
  968. ./bootstrap
  969. ./configure --prefix=/path/to/install --with-gnunet=/path/to/gnunet
  970. @end example
  971. When your GNUnet installation is not included in the default linker search
  972. path, you have to add @code{/path/to/gnunet} to the file
  973. @file{/etc/ld.so.conf} and run @code{ldconfig} or your add it to the
  974. environmental variable @code{LD_LIBRARY_PATH} by using
  975. @example
  976. export LD_LIBRARY_PATH=/path/to/gnunet/lib
  977. @end example
  978. @cindex writing testcases
  979. @node Writing testcases
  980. @section Writing testcases
  981. Ideally, any non-trivial GNUnet code should be covered by automated
  982. testcases. Testcases should reside in the same place as the code that is
  983. being tested. The name of source files implementing tests should begin
  984. with @code{test_} followed by the name of the file that contains
  985. the code that is being tested.
  986. Testcases in GNUnet should be integrated with the autotools build system.
  987. This way, developers and anyone building binary packages will be able to
  988. run all testcases simply by running @code{make check}. The final
  989. testcases shipped with the distribution should output at most some brief
  990. progress information and not display debug messages by default. The
  991. success or failure of a testcase must be indicated by returning zero
  992. (success) or non-zero (failure) from the main method of the testcase.
  993. The integration with the autotools is relatively straightforward and only
  994. requires modifications to the @file{Makefile.am} in the directory
  995. containing the testcase. For a testcase testing the code in @file{foo.c}
  996. the @file{Makefile.am} would contain the following lines:
  997. @example
  998. check_PROGRAMS = test_foo
  999. TESTS = $(check_PROGRAMS)
  1000. test_foo_SOURCES = test_foo.c
  1001. test_foo_LDADD = $(top_builddir)/src/util/libgnunetutil.la
  1002. @end example
  1003. Naturally, other libraries used by the testcase may be specified in the
  1004. @code{LDADD} directive as necessary.
  1005. Often testcases depend on additional input files, such as a configuration
  1006. file. These support files have to be listed using the @code{EXTRA_DIST}
  1007. directive in order to ensure that they are included in the distribution.
  1008. Example:
  1009. @example
  1010. EXTRA_DIST = test_foo_data.conf
  1011. @end example
  1012. Executing @code{make check} will run all testcases in the current
  1013. directory and all subdirectories. Testcases can be compiled individually
  1014. by running @code{make test_foo} and then invoked directly using
  1015. @code{./test_foo}. Note that due to the use of plugins in GNUnet, it is
  1016. typically necessary to run @code{make install} before running any
  1017. testcases. Thus the canonical command @code{make check install} has to be
  1018. changed to @code{make install check} for GNUnet.
  1019. @c ***********************************************************************
  1020. @cindex Building GNUnet
  1021. @node Building GNUnet and its dependencies
  1022. @section Building GNUnet and its dependencies
  1023. In the following section we will outline how to build GNUnet and
  1024. some of its dependencies. We will assume a fair amount of knowledge
  1025. for building applications under UNIX-like systems. Furthermore we
  1026. assume that the build environment is sane and that you are aware of
  1027. any implications actions in this process could have.
  1028. Instructions here can be seen as notes for developers (an extension to
  1029. the 'HACKING' section in README) as well as package maintainers.
  1030. @b{Users should rely on the available binary packages.}
  1031. We will use Debian as an example Operating System environment. Substitute
  1032. accordingly with your own Operating System environment.
  1033. For the full list of dependencies, consult the appropriate, up-to-date
  1034. section in the @file{README} file.
  1035. First, we need to build or install (depending on your OS) the following
  1036. packages. If you build them from source, build them in this exact order:
  1037. @example
  1038. libgpgerror, libgcrypt, libnettle, libunbound, GnuTLS (with libunbound
  1039. support)
  1040. @end example
  1041. After we have build and installed those packages, we continue with
  1042. packages closer to GNUnet in this step: libgnurl (our libcurl fork),
  1043. GNU libmicrohttpd, and GNU libextractor. Again, if your package manager
  1044. provides one of these packages, use the packages provided from it
  1045. unless you have good reasons (package version too old, conflicts, etc).
  1046. We advise against compiling widely used packages such as GnuTLS
  1047. yourself if your OS provides a variant already unless you take care
  1048. of maintenance of the packages then.
  1049. In the optimistic case, this command will give you all the dependencies
  1050. on Debian, Debian derived systems or any Linux Operating System using
  1051. the apt package manager:
  1052. @example
  1053. sudo apt-get install libgnurl libmicrohttpd libextractor
  1054. @end example
  1055. From experience we know that at the very least libgnurl is not
  1056. available in some environments. You could substitute libgnurl
  1057. with libcurl, but we recommend to install libgnurl, as it gives
  1058. you a predefined libcurl with the small set GNUnet requires.
  1059. libgnurl has been developed to co-exist with libcurl installations,
  1060. installing it will cause no filename or namespace collisions.
  1061. @cindex libgnurl
  1062. @cindex compiling libgnurl
  1063. GNUnet and some of its function depend on a limited subset of cURL/libcurl.
  1064. Rather than trying to enforce a certain configuration on the world, we
  1065. opted to maintain a microfork of it that ensures we can link against the
  1066. right set of features. We called this specialized set of libcurl
  1067. ``libgnurl''. It is fully ABI compatible with libcurl and currently used
  1068. by GNUnet and some of its dependencies.
  1069. We download libgnurl and its digital signature from the GNU fileserver,
  1070. assuming @env{TMPDIR} exists.
  1071. Note: TMPDIR might be @file{/tmp}, @env{TMPDIR}, @env{TMP} or any other
  1072. location. For consistency we assume @env{TMPDIR} points to @file{/tmp}
  1073. for the remainder of this section.
  1074. @example
  1075. cd \$TMPDIR
  1076. wget https://ftp.gnu.org/gnu/gnunet/gnurl-7.60.0.tar.Z
  1077. wget https://ftp.gnu.org/gnu/gnunet/gnurl-7.60.0.tar.Z.sig
  1078. @end example
  1079. Next, verify the digital signature of the file:
  1080. @example
  1081. gpg --verify gnurl-7.60.0.tar.Z.sig
  1082. @end example
  1083. If gpg fails, you might try with @command{gpg2} on your OS. If the error
  1084. states that ``the key can not be found'' or it is unknown, you have to
  1085. retrieve the key (A88C8ADD129828D7EAC02E52E22F9BBFEE348588) from a
  1086. keyserver first:
  1087. @example
  1088. gpg --keyserver pgp.mit.edu --recv-keys A88C8ADD129828D7EAC02E52E22F9BBFEE348588
  1089. @end example
  1090. and rerun the verification command.
  1091. libgnurl will require the following packages to be present at runtime:
  1092. gnutls (with DANE support / libunbound), libidn, zlib and at compile time:
  1093. libtool, groff, perl, pkg-config, and python 2.7.
  1094. Once you have verified that all the required packages are present on your
  1095. system, we can proceed to compile libgnurl:
  1096. @example
  1097. tar -xvf gnurl-7.60.0.tar.Z
  1098. cd gnurl-7.60.0
  1099. sh configure --disable-ntlm-wb
  1100. make
  1101. make -C tests test
  1102. sudo make install
  1103. @end example
  1104. After you've compiled and installed libgnurl, we can proceed to building
  1105. GNUnet.
  1106. First, in addition to the GNUnet sources you might require downloading the
  1107. latest version of various dependencies, depending on how recent the
  1108. software versions in your distribution of GNU/Linux are.
  1109. Most distributions do not include sufficiently recent versions of these
  1110. dependencies.
  1111. Thus, a typically installation on a "modern" GNU/Linux distribution
  1112. requires you to install the following dependencies (ideally in this
  1113. order):
  1114. @itemize @bullet
  1115. @item libgpgerror and libgcrypt
  1116. @item libnettle and libunbound (possibly from distribution), GnuTLS
  1117. @item libgnurl (read the README)
  1118. @item GNU libmicrohttpd
  1119. @item GNU libextractor
  1120. @end itemize
  1121. Make sure to first install the various mandatory and optional
  1122. dependencies including development headers from your distribution.
  1123. Other dependencies that you should strongly consider to install is a
  1124. database (MySQL, sqlite or Postgres).
  1125. The following instructions will assume that you installed at least sqlite.
  1126. For most distributions you should be able to find pre-build packages for
  1127. the database. Again, make sure to install the client libraries @b{and} the
  1128. respective development headers (if they are packaged separately) as well.
  1129. You can find specific, detailed instructions for installing of the
  1130. dependencies (and possibly the rest of the GNUnet installation) in the
  1131. platform-specific descriptions, which can be found in the Index.
  1132. Please consult them now.
  1133. If your distribution is not listed, please study the build
  1134. instructions for Debian stable, carefully as you try to install the
  1135. dependencies for your own distribution.
  1136. Contributing additional instructions for further platforms is always
  1137. appreciated.
  1138. Please take in mind that operating system development tends to move at
  1139. a rather fast speed. Due to this you should be aware that some of
  1140. the instructions could be outdated by the time you are reading this.
  1141. If you find a mistake, please tell us about it (or even better: send
  1142. a patch to the documentation to fix it!).
  1143. Before proceeding further, please double-check the dependency list.
  1144. Note that in addition to satisfying the dependencies, you might have to
  1145. make sure that development headers for the various libraries are also
  1146. installed.
  1147. There maybe files for other distributions, or you might be able to find
  1148. equivalent packages for your distribution.
  1149. While it is possible to build and install GNUnet without having root
  1150. access, we will assume that you have full control over your system in
  1151. these instructions.
  1152. First, you should create a system user @emph{gnunet} and an additional
  1153. group @emph{gnunetdns}. On the GNU/Linux distributions Debian and Ubuntu,
  1154. type:
  1155. @example
  1156. sudo adduser --system --home /var/lib/gnunet --group \
  1157. --disabled-password gnunet
  1158. sudo addgroup --system gnunetdns
  1159. @end example
  1160. @noindent
  1161. On other Unixes and GNU systems, this should have the same effect:
  1162. @example
  1163. sudo useradd --system --groups gnunet --home-dir /var/lib/gnunet
  1164. sudo addgroup --system gnunetdns
  1165. @end example
  1166. Now compile and install GNUnet using:
  1167. @example
  1168. tar xvf gnunet-@value{VERSION}.tar.gz
  1169. cd gnunet-@value{VERSION}
  1170. ./configure --with-sudo=sudo --with-nssdir=/lib
  1171. make
  1172. sudo make install
  1173. @end example
  1174. If you want to be able to enable DEBUG-level log messages, add
  1175. @code{--enable-logging=verbose} to the end of the
  1176. @command{./configure} command.
  1177. @code{DEBUG}-level log messages are in English only and
  1178. should only be useful for developers (or for filing
  1179. really detailed bug reports).
  1180. @noindent
  1181. Next, edit the file @file{/etc/gnunet.conf} to contain the following:
  1182. @example
  1183. [arm]
  1184. START_SYSTEM_SERVICES = YES
  1185. START_USER_SERVICES = NO
  1186. @end example
  1187. @noindent
  1188. You may need to update your @code{ld.so} cache to include
  1189. files installed in @file{/usr/local/lib}:
  1190. @example
  1191. # ldconfig
  1192. @end example
  1193. @noindent
  1194. Then, switch from user @code{root} to user @code{gnunet} to start
  1195. the peer:
  1196. @example
  1197. # su -s /bin/sh - gnunet
  1198. $ gnunet-arm -c /etc/gnunet.conf -s
  1199. @end example
  1200. You may also want to add the last line in the gnunet user's @file{crontab}
  1201. prefixed with @code{@@reboot} so that it is executed whenever the system
  1202. is booted:
  1203. @example
  1204. @@reboot /usr/local/bin/gnunet-arm -c /etc/gnunet.conf -s
  1205. @end example
  1206. @noindent
  1207. This will only start the system-wide GNUnet services.
  1208. Type @command{exit} to get back your root shell.
  1209. Now, you need to configure the per-user part. For each
  1210. user that should get access to GNUnet on the system, run
  1211. (replace alice with your username):
  1212. @example
  1213. sudo adduser alice gnunet
  1214. @end example
  1215. @noindent
  1216. to allow them to access the system-wide GNUnet services. Then, each
  1217. user should create a configuration file @file{~/.config/gnunet.conf}
  1218. with the lines:
  1219. @example
  1220. [arm]
  1221. START_SYSTEM_SERVICES = NO
  1222. START_USER_SERVICES = YES
  1223. DEFAULTSERVICES = gns
  1224. @end example
  1225. @noindent
  1226. and start the per-user services using
  1227. @example
  1228. $ gnunet-arm -c ~/.config/gnunet.conf -s
  1229. @end example
  1230. @noindent
  1231. Again, adding a @code{crontab} entry to autostart the peer is advised:
  1232. @example
  1233. @@reboot /usr/local/bin/gnunet-arm -c $HOME/.config/gnunet.conf -s
  1234. @end example
  1235. @noindent
  1236. Note that some GNUnet services (such as SOCKS5 proxies) may need a
  1237. system-wide TCP port for each user.
  1238. For those services, systems with more than one user may require each user
  1239. to specify a different port number in their personal configuration file.
  1240. Finally, the user should perform the basic initial setup for the GNU Name
  1241. System (GNS) certificate authority. This is done by running:
  1242. @example
  1243. $ gnunet-gns-proxy-setup-ca
  1244. @end example
  1245. @noindent
  1246. The first generates the default zones, whereas the second setups the GNS
  1247. Certificate Authority with the user's browser. Now, to activate GNS in the
  1248. normal DNS resolution process, you need to edit your
  1249. @file{/etc/nsswitch.conf} where you should find a line like this:
  1250. @example
  1251. hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4
  1252. @end example
  1253. @noindent
  1254. The exact details may differ a bit, which is fine. Add the text
  1255. @emph{"gns [NOTFOUND=return]"} after @emph{"files"}.
  1256. Keep in mind that we included a backslash ("\") here just for
  1257. markup reasons. You should write the text below on @b{one line}
  1258. and @b{without} the "\":
  1259. @example
  1260. hosts: files gns [NOTFOUND=return] mdns4_minimal \
  1261. [NOTFOUND=return] dns mdns4
  1262. @end example
  1263. @c FIXME: Document new behavior.
  1264. You might want to make sure that @file{/lib/libnss_gns.so.2} exists on
  1265. your system, it should have been created during the installation.
  1266. @c **********************************************************************
  1267. @cindex TESTING library
  1268. @node TESTING library
  1269. @section TESTING library
  1270. The TESTING library is used for writing testcases which involve starting a
  1271. single or multiple peers. While peers can also be started by testcases
  1272. using the ARM subsystem, using TESTING library provides an elegant way to
  1273. do this. The configurations of the peers are auto-generated from a given
  1274. template to have non-conflicting port numbers ensuring that peers'
  1275. services do not run into bind errors. This is achieved by testing ports'
  1276. availability by binding a listening socket to them before allocating them
  1277. to services in the generated configurations.
  1278. An another advantage while using TESTING is that it shortens the testcase
  1279. startup time as the hostkeys for peers are copied from a pre-computed set
  1280. of hostkeys instead of generating them at peer startup which may take a
  1281. considerable amount of time when starting multiple peers or on an embedded
  1282. processor.
  1283. TESTING also allows for certain services to be shared among peers. This
  1284. feature is invaluable when testing with multiple peers as it helps to
  1285. reduce the number of services run per each peer and hence the total
  1286. number of processes run per testcase.
  1287. TESTING library only handles creating, starting and stopping peers.
  1288. Features useful for testcases such as connecting peers in a topology are
  1289. not available in TESTING but are available in the TESTBED subsystem.
  1290. Furthermore, TESTING only creates peers on the localhost, however by
  1291. using TESTBED testcases can benefit from creating peers across multiple
  1292. hosts.
  1293. @menu
  1294. * API::
  1295. * Finer control over peer stop::
  1296. * Helper functions::
  1297. * Testing with multiple processes::
  1298. @end menu
  1299. @cindex TESTING API
  1300. @node API
  1301. @subsection API
  1302. TESTING abstracts a group of peers as a TESTING system. All peers in a
  1303. system have common hostname and no two services of these peers have a
  1304. same port or a UNIX domain socket path.
  1305. TESTING system can be created with the function
  1306. @code{GNUNET_TESTING_system_create()} which returns a handle to the
  1307. system. This function takes a directory path which is used for generating
  1308. the configurations of peers, an IP address from which connections to the
  1309. peers' services should be allowed, the hostname to be used in peers'
  1310. configuration, and an array of shared service specifications of type
  1311. @code{struct GNUNET_TESTING_SharedService}.
  1312. The shared service specification must specify the name of the service to
  1313. share, the configuration pertaining to that shared service and the
  1314. maximum number of peers that are allowed to share a single instance of
  1315. the shared service.
  1316. TESTING system created with @code{GNUNET_TESTING_system_create()} chooses
  1317. ports from the default range @code{12000} - @code{56000} while
  1318. auto-generating configurations for peers.
  1319. This range can be customised with the function
  1320. @code{GNUNET_TESTING_system_create_with_portrange()}. This function is
  1321. similar to @code{GNUNET_TESTING_system_create()} except that it take 2
  1322. additional parameters --- the start and end of the port range to use.
  1323. A TESTING system is destroyed with the function
  1324. @code{GNUNET_TESTING_system_destory()}. This function takes the handle of
  1325. the system and a flag to remove the files created in the directory used
  1326. to generate configurations.
  1327. A peer is created with the function
  1328. @code{GNUNET_TESTING_peer_configure()}. This functions takes the system
  1329. handle, a configuration template from which the configuration for the peer
  1330. is auto-generated and the index from where the hostkey for the peer has to
  1331. be copied from. When successful, this function returns a handle to the
  1332. peer which can be used to start and stop it and to obtain the identity of
  1333. the peer. If unsuccessful, a NULL pointer is returned with an error
  1334. message. This function handles the generated configuration to have
  1335. non-conflicting ports and paths.
  1336. Peers can be started and stopped by calling the functions
  1337. @code{GNUNET_TESTING_peer_start()} and @code{GNUNET_TESTING_peer_stop()}
  1338. respectively. A peer can be destroyed by calling the function
  1339. @code{GNUNET_TESTING_peer_destroy}. When a peer is destroyed, the ports
  1340. and paths in allocated in its configuration are reclaimed for usage in new
  1341. peers.
  1342. @c ***********************************************************************
  1343. @node Finer control over peer stop
  1344. @subsection Finer control over peer stop
  1345. Using @code{GNUNET_TESTING_peer_stop()} is normally fine for testcases.
  1346. However, calling this function for each peer is inefficient when trying to
  1347. shutdown multiple peers as this function sends the termination signal to
  1348. the given peer process and waits for it to terminate. It would be faster
  1349. in this case to send the termination signals to the peers first and then
  1350. wait on them. This is accomplished by the functions
  1351. @code{GNUNET_TESTING_peer_kill()} which sends a termination signal to the
  1352. peer, and the function @code{GNUNET_TESTING_peer_wait()} which waits on
  1353. the peer.
  1354. Further finer control can be achieved by choosing to stop a peer
  1355. asynchronously with the function @code{GNUNET_TESTING_peer_stop_async()}.
  1356. This function takes a callback parameter and a closure for it in addition
  1357. to the handle to the peer to stop. The callback function is called with
  1358. the given closure when the peer is stopped. Using this function
  1359. eliminates blocking while waiting for the peer to terminate.
  1360. An asynchronous peer stop can be canceled by calling the function
  1361. @code{GNUNET_TESTING_peer_stop_async_cancel()}. Note that calling this
  1362. function does not prevent the peer from terminating if the termination
  1363. signal has already been sent to it. It does, however, cancels the
  1364. callback to be called when the peer is stopped.
  1365. @c ***********************************************************************
  1366. @node Helper functions
  1367. @subsection Helper functions
  1368. Most of the testcases can benefit from an abstraction which configures a
  1369. peer and starts it. This is provided by the function
  1370. @code{GNUNET_TESTING_peer_run()}. This function takes the testing
  1371. directory pathname, a configuration template, a callback and its closure.
  1372. This function creates a peer in the given testing directory by using the
  1373. configuration template, starts the peer and calls the given callback with
  1374. the given closure.
  1375. The function @code{GNUNET_TESTING_peer_run()} starts the ARM service of
  1376. the peer which starts the rest of the configured services. A similar
  1377. function @code{GNUNET_TESTING_service_run} can be used to just start a
  1378. single service of a peer. In this case, the peer's ARM service is not
  1379. started; instead, only the given service is run.
  1380. @c ***********************************************************************
  1381. @node Testing with multiple processes
  1382. @subsection Testing with multiple processes
  1383. When testing GNUnet, the splitting of the code into a services and clients
  1384. often complicates testing. The solution to this is to have the testcase
  1385. fork @code{gnunet-service-arm}, ask it to start the required server and
  1386. daemon processes and then execute appropriate client actions (to test the
  1387. client APIs or the core module or both). If necessary, multiple ARM
  1388. services can be forked using different ports (!) to simulate a network.
  1389. However, most of the time only one ARM process is needed. Note that on
  1390. exit, the testcase should shutdown ARM with a @code{TERM} signal (to give
  1391. it the chance to cleanly stop its child processes).
  1392. The following code illustrates spawning and killing an ARM process from a
  1393. testcase:
  1394. @example
  1395. static void run (void *cls,
  1396. char *const *args,
  1397. const char *cfgfile,
  1398. const struct GNUNET_CONFIGURATION_Handle *cfg) @{
  1399. struct GNUNET_OS_Process *arm_pid;
  1400. arm_pid = GNUNET_OS_start_process (NULL,
  1401. NULL,
  1402. "gnunet-service-arm",
  1403. "gnunet-service-arm",
  1404. "-c",
  1405. cfgname,
  1406. NULL);
  1407. /* do real test work here */
  1408. if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
  1409. GNUNET_log_strerror
  1410. (GNUNET_ERROR_TYPE_WARNING, "kill");
  1411. GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid));
  1412. GNUNET_OS_process_close (arm_pid); @}
  1413. GNUNET_PROGRAM_run (argc, argv,
  1414. "NAME-OF-TEST",
  1415. "nohelp",
  1416. options,
  1417. &run,
  1418. cls);
  1419. @end example
  1420. An alternative way that works well to test plugins is to implement a
  1421. mock-version of the environment that the plugin expects and then to
  1422. simply load the plugin directly.
  1423. @c ***********************************************************************
  1424. @node Performance regression analysis with Gauger
  1425. @section Performance regression analysis with Gauger
  1426. To help avoid performance regressions, GNUnet uses Gauger. Gauger is a
  1427. simple logging tool that allows remote hosts to send performance data to
  1428. a central server, where this data can be analyzed and visualized. Gauger
  1429. shows graphs of the repository revisions and the performance data recorded
  1430. for each revision, so sudden performance peaks or drops can be identified
  1431. and linked to a specific revision number.
  1432. In the case of GNUnet, the buildbots log the performance data obtained
  1433. during the tests after each build. The data can be accessed on GNUnet's
  1434. Gauger page.
  1435. The menu on the left allows to select either the results of just one
  1436. build bot (under "Hosts") or review the data from all hosts for a given
  1437. test result (under "Metrics"). In case of very different absolute value
  1438. of the results, for instance arm vs. amd64 machines, the option
  1439. "Normalize" on a metric view can help to get an idea about the
  1440. performance evolution across all hosts.
  1441. Using Gauger in GNUnet and having the performance of a module tracked over
  1442. time is very easy. First of course, the testcase must generate some
  1443. consistent metric, which makes sense to have logged. Highly volatile or
  1444. random dependent metrics probably are not ideal candidates for meaningful
  1445. regression detection.
  1446. To start logging any value, just include @code{gauger.h} in your testcase
  1447. code. Then, use the macro @code{GAUGER()} to make the Buildbots log
  1448. whatever value is of interest for you to @code{gnunet.org}'s Gauger
  1449. server. No setup is necessary as most Buildbots have already everything
  1450. in place and new metrics are created on demand. To delete a metric, you
  1451. need to contact a member of the GNUnet development team (a file will need
  1452. to be removed manually from the respective directory).
  1453. The code in the test should look like this:
  1454. @example
  1455. [other includes]
  1456. #include <gauger.h>
  1457. int main (int argc, char *argv[]) @{
  1458. [run test, generate data]
  1459. GAUGER("YOUR_MODULE",
  1460. "METRIC_NAME",
  1461. (float)value,
  1462. "UNIT"); @}
  1463. @end example
  1464. Where:
  1465. @table @asis
  1466. @item @strong{YOUR_MODULE} is a category in the gauger page and should be
  1467. the name of the module or subsystem like "Core" or "DHT"
  1468. @item @strong{METRIC} is
  1469. the name of the metric being collected and should be concise and
  1470. descriptive, like "PUT operations in sqlite-datastore".
  1471. @item @strong{value} is the value
  1472. of the metric that is logged for this run.
  1473. @item @strong{UNIT} is the unit in
  1474. which the value is measured, for instance "kb/s" or "kb of RAM/node".
  1475. @end table
  1476. If you wish to use Gauger for your own project, you can grab a copy of the
  1477. latest stable release or check out Gauger's Subversion repository.
  1478. @cindex TESTBED Subsystem
  1479. @node TESTBED Subsystem
  1480. @section TESTBED Subsystem
  1481. The TESTBED subsystem facilitates testing and measuring of multi-peer
  1482. deployments on a single host or over multiple hosts.
  1483. The architecture of the testbed module is divided into the following:
  1484. @itemize @bullet
  1485. @item Testbed API: An API which is used by the testing driver programs. It
  1486. provides with functions for creating, destroying, starting, stopping
  1487. peers, etc.
  1488. @item Testbed service (controller): A service which is started through the
  1489. Testbed API. This service handles operations to create, destroy, start,
  1490. stop peers, connect them, modify their configurations.
  1491. @item Testbed helper: When a controller has to be started on a host, the
  1492. testbed API starts the testbed helper on that host which in turn starts
  1493. the controller. The testbed helper receives a configuration for the
  1494. controller through its stdin and changes it to ensure the controller
  1495. doesn't run into any port conflict on that host.
  1496. @end itemize
  1497. The testbed service (controller) is different from the other GNUnet
  1498. services in that it is not started by ARM and is not supposed to be run
  1499. as a daemon. It is started by the testbed API through a testbed helper.
  1500. In a typical scenario involving multiple hosts, a controller is started
  1501. on each host. Controllers take up the actual task of creating peers,
  1502. starting and stopping them on the hosts they run.
  1503. While running deployments on a single localhost the testbed API starts the
  1504. testbed helper directly as a child process. When running deployments on
  1505. remote hosts the testbed API starts Testbed Helpers on each remote host
  1506. through remote shell. By default testbed API uses SSH as a remote shell.
  1507. This can be changed by setting the environmental variable
  1508. GNUNET_TESTBED_RSH_CMD to the required remote shell program. This
  1509. variable can also contain parameters which are to be passed to the remote
  1510. shell program. For e.g:
  1511. @example
  1512. export GNUNET_TESTBED_RSH_CMD="ssh -o BatchMode=yes \
  1513. -o NoHostAuthenticationForLocalhost=yes %h"
  1514. @end example
  1515. Substitutions are allowed in the command string above,
  1516. this allows for substitutions through placemarks which begin with a `%'.
  1517. At present the following substitutions are supported
  1518. @itemize @bullet
  1519. @item %h: hostname
  1520. @item %u: username
  1521. @item %p: port
  1522. @end itemize
  1523. Note that the substitution placemark is replaced only when the
  1524. corresponding field is available and only once. Specifying
  1525. @example
  1526. %u@@%h
  1527. @end example
  1528. doesn't work either. If you want to user username substitutions for
  1529. @command{SSH}, use the argument @code{-l} before the
  1530. username substitution.
  1531. For example:
  1532. @example
  1533. ssh -l %u -p %p %h
  1534. @end example
  1535. The testbed API and the helper communicate through the helpers stdin and
  1536. stdout. As the helper is started through a remote shell on remote hosts
  1537. any output messages from the remote shell interfere with the communication
  1538. and results in a failure while starting the helper. For this reason, it is
  1539. suggested to use flags to make the remote shells produce no output
  1540. messages and to have password-less logins. The default remote shell, SSH,
  1541. the default options are:
  1542. @example
  1543. -o BatchMode=yes -o NoHostBasedAuthenticationForLocalhost=yes"
  1544. @end example
  1545. Password-less logins should be ensured by using SSH keys.
  1546. Since the testbed API executes the remote shell as a non-interactive
  1547. shell, certain scripts like .bashrc, .profiler may not be executed. If
  1548. this is the case testbed API can be forced to execute an interactive
  1549. shell by setting up the environmental variable
  1550. @code{GNUNET_TESTBED_RSH_CMD_SUFFIX} to a shell program.
  1551. An example could be:
  1552. @example
  1553. export GNUNET_TESTBED_RSH_CMD_SUFFIX="sh -lc"
  1554. @end example
  1555. The testbed API will then execute the remote shell program as:
  1556. @example
  1557. $GNUNET_TESTBED_RSH_CMD -p $port $dest $GNUNET_TESTBED_RSH_CMD_SUFFIX \
  1558. gnunet-helper-testbed
  1559. @end example
  1560. On some systems, problems may arise while starting testbed helpers if
  1561. GNUnet is installed into a custom location since the helper may not be
  1562. found in the standard path. This can be addressed by setting the variable
  1563. `@code{HELPER_BINARY_PATH}' to the path of the testbed helper.
  1564. Testbed API will then use this path to start helper binaries both
  1565. locally and remotely.
  1566. Testbed API can accessed by including the
  1567. @file{gnunet_testbed_service.h} file and linking with
  1568. @code{-lgnunettestbed}.
  1569. @c ***********************************************************************
  1570. @menu
  1571. * Supported Topologies::
  1572. * Hosts file format::
  1573. * Topology file format::
  1574. * Testbed Barriers::
  1575. * TESTBED Caveats::
  1576. @end menu
  1577. @node Supported Topologies
  1578. @subsection Supported Topologies
  1579. While testing multi-peer deployments, it is often needed that the peers
  1580. are connected in some topology. This requirement is addressed by the
  1581. function @code{GNUNET_TESTBED_overlay_connect()} which connects any given
  1582. two peers in the testbed.
  1583. The API also provides a helper function
  1584. @code{GNUNET_TESTBED_overlay_configure_topology()} to connect a given set
  1585. of peers in any of the following supported topologies:
  1586. @itemize @bullet
  1587. @item @code{GNUNET_TESTBED_TOPOLOGY_CLIQUE}: All peers are connected with
  1588. each other
  1589. @item @code{GNUNET_TESTBED_TOPOLOGY_LINE}: Peers are connected to form a
  1590. line
  1591. @item @code{GNUNET_TESTBED_TOPOLOGY_RING}: Peers are connected to form a
  1592. ring topology
  1593. @item @code{GNUNET_TESTBED_TOPOLOGY_2D_TORUS}: Peers are connected to
  1594. form a 2 dimensional torus topology. The number of peers may not be a
  1595. perfect square, in that case the resulting torus may not have the uniform
  1596. poloidal and toroidal lengths
  1597. @item @code{GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI}: Topology is generated
  1598. to form a random graph. The number of links to be present should be given
  1599. @item @code{GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD}: Peers are connected to
  1600. form a 2D Torus with some random links among them. The number of random
  1601. links are to be given
  1602. @item @code{GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING}: Peers are
  1603. connected to form a ring with some random links among them. The number of
  1604. random links are to be given
  1605. @item @code{GNUNET_TESTBED_TOPOLOGY_SCALE_FREE}: Connects peers in a
  1606. topology where peer connectivity follows power law - new peers are
  1607. connected with high probability to well connected peers.
  1608. (See Emergence of Scaling in Random Networks. Science 286,
  1609. 509-512, 1999
  1610. (@uref{https://git.gnunet.org/bibliography.git/plain/docs/emergence_of_scaling_in_random_networks__barabasi_albert_science_286__1999.pdf, pdf}))
  1611. @item @code{GNUNET_TESTBED_TOPOLOGY_FROM_FILE}: The topology information
  1612. is loaded from a file. The path to the file has to be given.
  1613. @xref{Topology file format}, for the format of this file.
  1614. @item @code{GNUNET_TESTBED_TOPOLOGY_NONE}: No topology
  1615. @end itemize
  1616. The above supported topologies can be specified respectively by setting
  1617. the variable @code{OVERLAY_TOPOLOGY} to the following values in the
  1618. configuration passed to Testbed API functions
  1619. @code{GNUNET_TESTBED_test_run()} and
  1620. @code{GNUNET_TESTBED_run()}:
  1621. @itemize @bullet
  1622. @item @code{CLIQUE}
  1623. @item @code{RING}
  1624. @item @code{LINE}
  1625. @item @code{2D_TORUS}
  1626. @item @code{RANDOM}
  1627. @item @code{SMALL_WORLD}
  1628. @item @code{SMALL_WORLD_RING}
  1629. @item @code{SCALE_FREE}
  1630. @item @code{FROM_FILE}
  1631. @item @code{NONE}
  1632. @end itemize
  1633. Topologies @code{RANDOM}, @code{SMALL_WORLD} and @code{SMALL_WORLD_RING}
  1634. require the option @code{OVERLAY_RANDOM_LINKS} to be set to the number of
  1635. random links to be generated in the configuration. The option will be
  1636. ignored for the rest of the topologies.
  1637. Topology @code{SCALE_FREE} requires the options
  1638. @code{SCALE_FREE_TOPOLOGY_CAP} to be set to the maximum number of peers
  1639. which can connect to a peer and @code{SCALE_FREE_TOPOLOGY_M} to be set to
  1640. how many peers a peer should be at least connected to.
  1641. Similarly, the topology @code{FROM_FILE} requires the option
  1642. @code{OVERLAY_TOPOLOGY_FILE} to contain the path of the file containing
  1643. the topology information. This option is ignored for the rest of the
  1644. topologies. @xref{Topology file format}, for the format of this file.
  1645. @c ***********************************************************************
  1646. @node Hosts file format
  1647. @subsection Hosts file format
  1648. The testbed API offers the function
  1649. @code{GNUNET_TESTBED_hosts_load_from_file()} to load from a given file
  1650. details about the hosts which testbed can use for deploying peers.
  1651. This function is useful to keep the data about hosts
  1652. separate instead of hard coding them in code.
  1653. Another helper function from testbed API, @code{GNUNET_TESTBED_run()}
  1654. also takes a hosts file name as its parameter. It uses the above
  1655. function to populate the hosts data structures and start controllers to
  1656. deploy peers.
  1657. These functions require the hosts file to be of the following format:
  1658. @itemize @bullet
  1659. @item Each line is interpreted to have details about a host
  1660. @item Host details should include the username to use for logging into the
  1661. host, the hostname of the host and the port number to use for the remote
  1662. shell program. All thee values should be given.
  1663. @item These details should be given in the following format:
  1664. @example
  1665. <username>@@<hostname>:<port>
  1666. @end example
  1667. @end itemize
  1668. Note that having canonical hostnames may cause problems while resolving
  1669. the IP addresses (See this bug). Hence it is advised to provide the hosts'
  1670. IP numerical addresses as hostnames whenever possible.
  1671. @c ***********************************************************************
  1672. @node Topology file format
  1673. @subsection Topology file format
  1674. A topology file describes how peers are to be connected. It should adhere
  1675. to the following format for testbed to parse it correctly.
  1676. Each line should begin with the target peer id. This should be followed by
  1677. a colon(`:') and origin peer ids separated by `|'. All spaces except for
  1678. newline characters are ignored. The API will then try to connect each
  1679. origin peer to the target peer.
  1680. For example, the following file will result in 5 overlay connections:
  1681. [2->1], [3->1],[4->3], [0->3], [2->0]@
  1682. @code{@ 1:2|3@ 3:4| 0@ 0: 2@ }
  1683. @c ***********************************************************************
  1684. @node Testbed Barriers
  1685. @subsection Testbed Barriers
  1686. The testbed subsystem's barriers API facilitates coordination among the
  1687. peers run by the testbed and the experiment driver. The concept is
  1688. similar to the barrier synchronisation mechanism found in parallel
  1689. programming or multi-threading paradigms - a peer waits at a barrier upon
  1690. reaching it until the barrier is reached by a predefined number of peers.
  1691. This predefined number of peers required to cross a barrier is also called
  1692. quorum. We say a peer has reached a barrier if the peer is waiting for the
  1693. barrier to be crossed. Similarly a barrier is said to be reached if the
  1694. required quorum of peers reach the barrier. A barrier which is reached is
  1695. deemed as crossed after all the peers waiting on it are notified.
  1696. The barriers API provides the following functions:
  1697. @itemize @bullet
  1698. @item @strong{@code{GNUNET_TESTBED_barrier_init()}:} function to
  1699. initialize a barrier in the experiment
  1700. @item @strong{@code{GNUNET_TESTBED_barrier_cancel()}:} function to cancel
  1701. a barrier which has been initialized before
  1702. @item @strong{@code{GNUNET_TESTBED_barrier_wait()}:} function to signal
  1703. barrier service that the caller has reached a barrier and is waiting for
  1704. it to be crossed
  1705. @item @strong{@code{GNUNET_TESTBED_barrier_wait_cancel()}:} function to
  1706. stop waiting for a barrier to be crossed
  1707. @end itemize
  1708. Among the above functions, the first two, namely
  1709. @code{GNUNET_TESTBED_barrier_init()} and
  1710. @code{GNUNET_TESTBED_barrier_cancel()} are used by experiment drivers. All
  1711. barriers should be initialised by the experiment driver by calling
  1712. @code{GNUNET_TESTBED_barrier_init()}. This function takes a name to
  1713. identify the barrier, the quorum required for the barrier to be crossed
  1714. and a notification callback for notifying the experiment driver when the
  1715. barrier is crossed. @code{GNUNET_TESTBED_barrier_cancel()} cancels an
  1716. initialised barrier and frees the resources allocated for it. This
  1717. function can be called upon a initialised barrier before it is crossed.
  1718. The remaining two functions @code{GNUNET_TESTBED_barrier_wait()} and
  1719. @code{GNUNET_TESTBED_barrier_wait_cancel()} are used in the peer's
  1720. processes. @code{GNUNET_TESTBED_barrier_wait()} connects to the local
  1721. barrier service running on the same host the peer is running on and
  1722. registers that the caller has reached the barrier and is waiting for the
  1723. barrier to be crossed. Note that this function can only be used by peers
  1724. which are started by testbed as this function tries to access the local
  1725. barrier service which is part of the testbed controller service. Calling
  1726. @code{GNUNET_TESTBED_barrier_wait()} on an uninitialised barrier results
  1727. in failure. @code{GNUNET_TESTBED_barrier_wait_cancel()} cancels the
  1728. notification registered by @code{GNUNET_TESTBED_barrier_wait()}.
  1729. @c ***********************************************************************
  1730. @menu
  1731. * Implementation::
  1732. @end menu
  1733. @node Implementation
  1734. @subsubsection Implementation
  1735. Since barriers involve coordination between experiment driver and peers,
  1736. the barrier service in the testbed controller is split into two
  1737. components. The first component responds to the message generated by the
  1738. barrier API used by the experiment driver (functions
  1739. @code{GNUNET_TESTBED_barrier_init()} and
  1740. @code{GNUNET_TESTBED_barrier_cancel()}) and the second component to the
  1741. messages generated by barrier API used by peers (functions
  1742. @code{GNUNET_TESTBED_barrier_wait()} and
  1743. @code{GNUNET_TESTBED_barrier_wait_cancel()}).
  1744. Calling @code{GNUNET_TESTBED_barrier_init()} sends a
  1745. @code{GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_INIT} message to the master
  1746. controller. The master controller then registers a barrier and calls
  1747. @code{GNUNET_TESTBED_barrier_init()} for each its subcontrollers. In this
  1748. way barrier initialisation is propagated to the controller hierarchy.
  1749. While propagating initialisation, any errors at a subcontroller such as
  1750. timeout during further propagation are reported up the hierarchy back to
  1751. the experiment driver.
  1752. Similar to @code{GNUNET_TESTBED_barrier_init()},
  1753. @code{GNUNET_TESTBED_barrier_cancel()} propagates
  1754. @code{GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_CANCEL} message which causes
  1755. controllers to remove an initialised barrier.
  1756. The second component is implemented as a separate service in the binary
  1757. `gnunet-service-testbed' which already has the testbed controller service.
  1758. Although this deviates from the gnunet process architecture of having one
  1759. service per binary, it is needed in this case as this component needs
  1760. access to barrier data created by the first component. This component
  1761. responds to @code{GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_WAIT} messages from
  1762. local peers when they call @code{GNUNET_TESTBED_barrier_wait()}. Upon
  1763. receiving @code{GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_WAIT} message, the
  1764. service checks if the requested barrier has been initialised before and
  1765. if it was not initialised, an error status is sent through
  1766. @code{GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_STATUS} message to the local
  1767. peer and the connection from the peer is terminated. If the barrier is
  1768. initialised before, the barrier's counter for reached peers is incremented
  1769. and a notification is registered to notify the peer when the barrier is
  1770. reached. The connection from the peer is left open.
  1771. When enough peers required to attain the quorum send
  1772. @code{GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_WAIT} messages, the controller
  1773. sends a @code{GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_STATUS} message to its
  1774. parent informing that the barrier is crossed. If the controller has
  1775. started further subcontrollers, it delays this message until it receives
  1776. a similar notification from each of those subcontrollers. Finally, the
  1777. barriers API at the experiment driver receives the
  1778. @code{GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_STATUS} when the barrier is
  1779. reached at all the controllers.
  1780. The barriers API at the experiment driver responds to the
  1781. @code{GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_STATUS} message by echoing it
  1782. back to the master controller and notifying the experiment controller
  1783. through the notification callback that a barrier has been crossed. The
  1784. echoed @code{GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_STATUS} message is
  1785. propagated by the master controller to the controller hierarchy. This
  1786. propagation triggers the notifications registered by peers at each of the
  1787. controllers in the hierarchy. Note the difference between this downward
  1788. propagation of the @code{GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_STATUS}
  1789. message from its upward propagation --- the upward propagation is needed
  1790. for ensuring that the barrier is reached by all the controllers and the
  1791. downward propagation is for triggering that the barrier is crossed.
  1792. @cindex TESTBED Caveats
  1793. @node TESTBED Caveats
  1794. @subsection TESTBED Caveats
  1795. This section documents a few caveats when using the GNUnet testbed
  1796. subsystem.
  1797. @c ***********************************************************************
  1798. @menu
  1799. * CORE must be started::
  1800. * ATS must want the connections::
  1801. @end menu
  1802. @node CORE must be started
  1803. @subsubsection CORE must be started
  1804. A uncomplicated issue is bug #3993
  1805. (@uref{https://bugs.gnunet.org/view.php?id=3993, https://bugs.gnunet.org/view.php?id=3993}):
  1806. Your configuration MUST somehow ensure that for each peer the
  1807. @code{CORE} service is started when the peer is setup, otherwise
  1808. @code{TESTBED} may fail to connect peers when the topology is initialized,
  1809. as @code{TESTBED} will start some @code{CORE} services but not
  1810. necessarily all (but it relies on all of them running). The easiest way
  1811. is to set
  1812. @example
  1813. [core]
  1814. IMMEDIATE_START = YES
  1815. @end example
  1816. @noindent
  1817. in the configuration file.
  1818. Alternatively, having any service that directly or indirectly depends on
  1819. @code{CORE} being started with @code{IMMEDIATE_START} will also do.
  1820. This issue largely arises if users try to over-optimize by not
  1821. starting any services with @code{IMMEDIATE_START}.
  1822. @c ***********************************************************************
  1823. @node ATS must want the connections
  1824. @subsubsection ATS must want the connections
  1825. When TESTBED sets up connections, it only offers the respective HELLO
  1826. information to the TRANSPORT service. It is then up to the ATS service to
  1827. @strong{decide} to use the connection. The ATS service will typically
  1828. eagerly establish any connection if the number of total connections is
  1829. low (relative to bandwidth). Details may further depend on the
  1830. specific ATS backend that was configured. If ATS decides to NOT establish
  1831. a connection (even though TESTBED provided the required information), then
  1832. that connection will count as failed for TESTBED. Note that you can
  1833. configure TESTBED to tolerate a certain number of connection failures
  1834. (see '-e' option of gnunet-testbed-profiler). This issue largely arises
  1835. for dense overlay topologies, especially if you try to create cliques
  1836. with more than 20 peers.
  1837. @cindex libgnunetutil
  1838. @node libgnunetutil
  1839. @section libgnunetutil
  1840. libgnunetutil is the fundamental library that all GNUnet code builds upon.
  1841. Ideally, this library should contain most of the platform dependent code
  1842. (except for user interfaces and really special needs that only few
  1843. applications have). It is also supposed to offer basic services that most
  1844. if not all GNUnet binaries require. The code of libgnunetutil is in the
  1845. @file{src/util/} directory. The public interface to the library is in the
  1846. gnunet_util.h header. The functions provided by libgnunetutil fall
  1847. roughly into the following categories (in roughly the order of importance
  1848. for new developers):
  1849. @itemize @bullet
  1850. @item logging (common_logging.c)
  1851. @item memory allocation (common_allocation.c)
  1852. @item endianess conversion (common_endian.c)
  1853. @item internationalization (common_gettext.c)
  1854. @item String manipulation (string.c)
  1855. @item file access (disk.c)
  1856. @item buffered disk IO (bio.c)
  1857. @item time manipulation (time.c)
  1858. @item configuration parsing (configuration.c)
  1859. @item command-line handling (getopt*.c)
  1860. @item cryptography (crypto_*.c)
  1861. @item data structures (container_*.c)
  1862. @item CPS-style scheduling (scheduler.c)
  1863. @item Program initialization (program.c)
  1864. @item Networking (network.c, client.c, server*.c, service.c)
  1865. @item message queuing (mq.c)
  1866. @item bandwidth calculations (bandwidth.c)
  1867. @item Other OS-related (os*.c, plugin.c, signal.c)
  1868. @item Pseudonym management (pseudonym.c)
  1869. @end itemize
  1870. It should be noted that only developers that fully understand this entire
  1871. API will be able to write good GNUnet code.
  1872. Ideally, porting GNUnet should only require porting the gnunetutil
  1873. library. More testcases for the gnunetutil APIs are therefore a great
  1874. way to make porting of GNUnet easier.
  1875. @menu
  1876. * Logging::
  1877. * Interprocess communication API (IPC)::
  1878. * Cryptography API::
  1879. * Message Queue API::
  1880. * Service API::
  1881. * Optimizing Memory Consumption of GNUnet's (Multi-) Hash Maps::
  1882. * CONTAINER_MDLL API::
  1883. @end menu
  1884. @cindex Logging
  1885. @cindex log levels
  1886. @node Logging
  1887. @subsection Logging
  1888. GNUnet is able to log its activity, mostly for the purposes of debugging
  1889. the program at various levels.
  1890. @file{gnunet_common.h} defines several @strong{log levels}:
  1891. @table @asis
  1892. @item ERROR for errors
  1893. (really problematic situations, often leading to crashes)
  1894. @item WARNING for warnings
  1895. (troubling situations that might have negative consequences, although
  1896. not fatal)
  1897. @item INFO for various information.
  1898. Used somewhat rarely, as GNUnet statistics is used to hold and display
  1899. most of the information that users might find interesting.
  1900. @item DEBUG for debugging.
  1901. Does not produce much output on normal builds, but when extra logging is
  1902. enabled at compile time, a staggering amount of data is outputted under
  1903. this log level.
  1904. @end table
  1905. Normal builds of GNUnet (configured with @code{--enable-logging[=yes]})
  1906. are supposed to log nothing under DEBUG level. The
  1907. @code{--enable-logging=verbose} configure option can be used to create a
  1908. build with all logging enabled. However, such build will produce large
  1909. amounts of log data, which is inconvenient when one tries to hunt down a
  1910. specific problem.
  1911. To mitigate this problem, GNUnet provides facilities to apply a filter to
  1912. reduce the logs:
  1913. @table @asis
  1914. @item Logging by default When no log levels are configured in any other
  1915. way (see below), GNUnet will default to the WARNING log level. This
  1916. mostly applies to GNUnet command line utilities, services and daemons;
  1917. tests will always set log level to WARNING or, if
  1918. @code{--enable-logging=verbose} was passed to configure, to DEBUG. The
  1919. default level is suggested for normal operation.
  1920. @item The -L option Most GNUnet executables accept an "-L loglevel" or
  1921. "--log=loglevel" option. If used, it makes the process set a global log
  1922. level to "loglevel". Thus it is possible to run some processes
  1923. with -L DEBUG, for example, and others with -L ERROR to enable specific
  1924. settings to diagnose problems with a particular process.
  1925. @item Configuration files. Because GNUnet
  1926. service and daemon processes are usually launched by gnunet-arm, it is not
  1927. possible to pass different custom command line options directly to every
  1928. one of them. The options passed to @code{gnunet-arm} only affect
  1929. gnunet-arm and not the rest of GNUnet. However, one can specify a
  1930. configuration key "OPTIONS" in the section that corresponds to a service
  1931. or a daemon, and put a value of "-L loglevel" there. This will make the
  1932. respective service or daemon set its log level to "loglevel" (as the
  1933. value of OPTIONS will be passed as a command-line argument).
  1934. To specify the same log level for all services without creating separate
  1935. "OPTIONS" entries in the configuration for each one, the user can specify
  1936. a config key "GLOBAL_POSTFIX" in the [arm] section of the configuration
  1937. file. The value of GLOBAL_POSTFIX will be appended to all command lines
  1938. used by the ARM service to run other services. It can contain any option
  1939. valid for all GNUnet commands, thus in particular the "-L loglevel"
  1940. option. The ARM service itself is, however, unaffected by GLOBAL_POSTFIX;
  1941. to set log level for it, one has to specify "OPTIONS" key in the [arm]
  1942. section.
  1943. @item Environment variables.
  1944. Setting global per-process log levels with "-L loglevel" does not offer
  1945. sufficient log filtering granularity, as one service will call interface
  1946. libraries and supporting libraries of other GNUnet services, potentially
  1947. producing lots of debug log messages from these libraries. Also, changing
  1948. the config file is not always convenient (especially when running the
  1949. GNUnet test suite).@ To fix that, and to allow GNUnet to use different
  1950. log filtering at runtime without re-compiling the whole source tree, the
  1951. log calls were changed to be configurable at run time. To configure them
  1952. one has to define environment variables "GNUNET_FORCE_LOGFILE",
  1953. "GNUNET_LOG" and/or "GNUNET_FORCE_LOG":
  1954. @itemize @bullet
  1955. @item "GNUNET_LOG" only affects the logging when no global log level is
  1956. configured by any other means (that is, the process does not explicitly
  1957. set its own log level, there are no "-L loglevel" options on command line
  1958. or in configuration files), and can be used to override the default
  1959. WARNING log level.
  1960. @item "GNUNET_FORCE_LOG" will completely override any other log
  1961. configuration options given.
  1962. @item "GNUNET_FORCE_LOGFILE" will completely override the location of the
  1963. file to log messages to. It should contain a relative or absolute file
  1964. name. Setting GNUNET_FORCE_LOGFILE is equivalent to passing
  1965. "--log-file=logfile" or "-l logfile" option (see below). It supports "[]"
  1966. format in file names, but not "@{@}" (see below).
  1967. @end itemize
  1968. Because environment variables are inherited by child processes when they
  1969. are launched, starting or re-starting the ARM service with these
  1970. variables will propagate them to all other services.
  1971. "GNUNET_LOG" and "GNUNET_FORCE_LOG" variables must contain a specially
  1972. formatted @strong{logging definition} string, which looks like this:@
  1973. @c FIXME: Can we close this with [/component] instead?
  1974. @example
  1975. [component];[file];[function];[from_line[-to_line]];loglevel[/component...]
  1976. @end example
  1977. That is, a logging definition consists of definition entries, separated by
  1978. slashes ('/'). If only one entry is present, there is no need to add a
  1979. slash to its end (although it is not forbidden either).@ All definition
  1980. fields (component, file, function, lines and loglevel) are mandatory, but
  1981. (except for the loglevel) they can be empty. An empty field means
  1982. "match anything". Note that even if fields are empty, the semicolon (';')
  1983. separators must be present.@ The loglevel field is mandatory, and must
  1984. contain one of the log level names (ERROR, WARNING, INFO or DEBUG).@
  1985. The lines field might contain one non-negative number, in which case it
  1986. matches only one line, or a range "from_line-to_line", in which case it
  1987. matches any line in the interval [from_line;to_line] (that is, including
  1988. both start and end line).@ GNUnet mostly defaults component name to the
  1989. name of the service that is implemented in a process ('transport',
  1990. 'core', 'peerinfo', etc), but logging calls can specify custom component
  1991. names using @code{GNUNET_log_from}.@ File name and function name are
  1992. provided by the compiler (__FILE__ and __FUNCTION__ built-ins).
  1993. Component, file and function fields are interpreted as non-extended
  1994. regular expressions (GNU libc regex functions are used). Matching is
  1995. case-sensitive, "^" and "$" will match the beginning and the end of the
  1996. text. If a field is empty, its contents are automatically replaced with
  1997. a ".*" regular expression, which matches anything. Matching is done in
  1998. the default way, which means that the expression matches as long as it's
  1999. contained anywhere in the string. Thus "GNUNET_" will match both
  2000. "GNUNET_foo" and "BAR_GNUNET_BAZ". Use '^' and/or '$' to make sure that
  2001. the expression matches at the start and/or at the end of the string.
  2002. The semicolon (';') can't be escaped, and GNUnet will not use it in
  2003. component names (it can't be used in function names and file names
  2004. anyway).
  2005. @end table
  2006. Every logging call in GNUnet code will be (at run time) matched against
  2007. the log definitions passed to the process. If a log definition fields are
  2008. matching the call arguments, then the call log level is compared the the
  2009. log level of that definition. If the call log level is less or equal to
  2010. the definition log level, the call is allowed to proceed. Otherwise the
  2011. logging call is forbidden, and nothing is logged. If no definitions
  2012. matched at all, GNUnet will use the global log level or (if a global log
  2013. level is not specified) will default to WARNING (that is, it will allow
  2014. the call to proceed, if its level is less or equal to the global log
  2015. level or to WARNING).
  2016. That is, definitions are evaluated from left to right, and the first
  2017. matching definition is used to allow or deny the logging call. Thus it is
  2018. advised to place narrow definitions at the beginning of the logdef
  2019. string, and generic definitions - at the end.
  2020. Whether a call is allowed or not is only decided the first time this
  2021. particular call is made. The evaluation result is then cached, so that
  2022. any attempts to make the same call later will be allowed or disallowed
  2023. right away. Because of that runtime log level evaluation should not
  2024. significantly affect the process performance.
  2025. Log definition parsing is only done once, at the first call to
  2026. @code{GNUNET_log_setup ()} made by the process (which is usually done soon after
  2027. it starts).
  2028. At the moment of writing there is no way to specify logging definitions
  2029. from configuration files, only via environment variables.
  2030. At the moment GNUnet will stop processing a log definition when it
  2031. encounters an error in definition formatting or an error in regular
  2032. expression syntax, and will not report the failure in any way.
  2033. @c ***********************************************************************
  2034. @menu
  2035. * Examples::
  2036. * Log files::
  2037. * Updated behavior of GNUNET_log::
  2038. @end menu
  2039. @node Examples
  2040. @subsubsection Examples
  2041. @table @asis
  2042. @item @code{GNUNET_FORCE_LOG=";;;;DEBUG" gnunet-arm -s} Start GNUnet
  2043. process tree, running all processes with DEBUG level (one should be
  2044. careful with it, as log files will grow at alarming rate!)
  2045. @item @code{GNUNET_FORCE_LOG="core;;;;DEBUG" gnunet-arm -s} Start GNUnet
  2046. process tree, running the core service under DEBUG level (everything else
  2047. will use configured or default level).
  2048. @item Start GNUnet process tree, allowing any logging calls from
  2049. gnunet-service-transport_validation.c (everything else will use
  2050. configured or default level).
  2051. @example
  2052. GNUNET_FORCE_LOG=";gnunet-service-transport_validation.c;;; DEBUG" \
  2053. gnunet-arm -s
  2054. @end example
  2055. @item Start GNUnet process tree, allowing any logging calls from
  2056. gnunet-gnunet-service-fs_push.c (everything else will use configured or
  2057. default level).
  2058. @example
  2059. GNUNET_FORCE_LOG="fs;gnunet-service-fs_push.c;;;DEBUG" gnunet-arm -s
  2060. @end example
  2061. @item Start GNUnet process tree, allowing any logging calls from the
  2062. GNUNET_NETWORK_socket_select function (everything else will use
  2063. configured or default level).
  2064. @example
  2065. GNUNET_FORCE_LOG=";;GNUNET_NETWORK_socket_select;;DEBUG" gnunet-arm -s
  2066. @end example
  2067. @item Start GNUnet process tree, allowing any logging calls from the
  2068. components that have "transport" in their names, and are made from
  2069. function that have "send" in their names. Everything else will be allowed
  2070. to be logged only if it has WARNING level.
  2071. @example
  2072. GNUNET_FORCE_LOG="transport.*;;.*send.*;;DEBUG/;;;;WARNING" gnunet-arm -s
  2073. @end example
  2074. @end table
  2075. On Windows, one can use batch files to run GNUnet processes with special
  2076. environment variables, without affecting the whole system. Such batch
  2077. file will look like this:
  2078. @example
  2079. set GNUNET_FORCE_LOG=;;do_transmit;;DEBUG@ gnunet-arm -s
  2080. @end example
  2081. (note the absence of double quotes in the environment variable definition,
  2082. as opposed to earlier examples, which use the shell).
  2083. Another limitation, on Windows, GNUNET_FORCE_LOGFILE @strong{MUST} be set
  2084. in order to GNUNET_FORCE_LOG to work.
  2085. @cindex Log files
  2086. @node Log files
  2087. @subsubsection Log files
  2088. GNUnet can be told to log everything into a file instead of stderr (which
  2089. is the default) using the "--log-file=logfile" or "-l logfile" option.
  2090. This option can also be passed via command line, or from the "OPTION" and
  2091. "GLOBAL_POSTFIX" configuration keys (see above). The file name passed
  2092. with this option is subject to GNUnet filename expansion. If specified in
  2093. "GLOBAL_POSTFIX", it is also subject to ARM service filename expansion,
  2094. in particular, it may contain "@{@}" (left and right curly brace)
  2095. sequence, which will be replaced by ARM with the name of the service.
  2096. This is used to keep logs from more than one service separate, while only
  2097. specifying one template containing "@{@}" in GLOBAL_POSTFIX.
  2098. As part of a secondary file name expansion, the first occurrence of "[]"
  2099. sequence ("left square brace" followed by "right square brace") in the
  2100. file name will be replaced with a process identifier or the process when
  2101. it initializes its logging subsystem. As a result, all processes will log
  2102. into different files. This is convenient for isolating messages of a
  2103. particular process, and prevents I/O races when multiple processes try to
  2104. write into the file at the same time. This expansion is done
  2105. independently of "@{@}" expansion that ARM service does (see above).
  2106. The log file name that is specified via "-l" can contain format characters
  2107. from the 'strftime' function family. For example, "%Y" will be replaced
  2108. with the current year. Using "basename-%Y-%m-%d.log" would include the
  2109. current year, month and day in the log file. If a GNUnet process runs for
  2110. long enough to need more than one log file, it will eventually clean up
  2111. old log files. Currently, only the last three log files (plus the current
  2112. log file) are preserved. So once the fifth log file goes into use (so
  2113. after 4 days if you use "%Y-%m-%d" as above), the first log file will be
  2114. automatically deleted. Note that if your log file name only contains "%Y",
  2115. then log files would be kept for 4 years and the logs from the first year
  2116. would be deleted once year 5 begins. If you do not use any date-related
  2117. string format codes, logs would never be automatically deleted by GNUnet.
  2118. @c ***********************************************************************
  2119. @node Updated behavior of GNUNET_log
  2120. @subsubsection Updated behavior of GNUNET_log
  2121. It's currently quite common to see constructions like this all over the
  2122. code:
  2123. @example
  2124. #if MESH_DEBUG
  2125. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
  2126. #endif
  2127. @end example
  2128. The reason for the #if is not to avoid displaying the message when
  2129. disabled (GNUNET_ERROR_TYPE takes care of that), but to avoid the
  2130. compiler including it in the binary at all, when compiling GNUnet for
  2131. platforms with restricted storage space / memory (MIPS routers,
  2132. ARM plug computers / dev boards, etc).
  2133. This presents several problems: the code gets ugly, hard to write and it
  2134. is very easy to forget to include the #if guards, creating non-consistent
  2135. code. A new change in GNUNET_log aims to solve these problems.
  2136. @strong{This change requires to @file{./configure} with at least
  2137. @code{--enable-logging=verbose} to see debug messages.}
  2138. Here is an example of code with dense debug statements:
  2139. @example
  2140. switch (restrict_topology) @{
  2141. case GNUNET_TESTING_TOPOLOGY_CLIQUE:#if VERBOSE_TESTING
  2142. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Blacklisting all but clique
  2143. topology\n")); #endif unblacklisted_connections = create_clique (pg,
  2144. &remove_connections, BLACKLIST, GNUNET_NO); break; case
  2145. GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING: #if VERBOSE_TESTING GNUNET_log
  2146. (GNUNET_ERROR_TYPE_DEBUG, _("Blacklisting all but small world (ring)
  2147. topology\n")); #endif unblacklisted_connections = create_small_world_ring
  2148. (pg,&remove_connections, BLACKLIST); break;
  2149. @end example
  2150. Pretty hard to follow, huh?
  2151. From now on, it is not necessary to include the #if / #endif statements to
  2152. achieve the same behavior. The @code{GNUNET_log} and @code{GNUNET_log_from}
  2153. macros take
  2154. care of it for you, depending on the configure option:
  2155. @itemize @bullet
  2156. @item If @code{--enable-logging} is set to @code{no}, the binary will
  2157. contain no log messages at all.
  2158. @item If @code{--enable-logging} is set to @code{yes}, the binary will
  2159. contain no DEBUG messages, and therefore running with @command{-L DEBUG}
  2160. will have
  2161. no effect. Other messages (ERROR, WARNING, INFO, etc) will be included.
  2162. @item If @code{--enable-logging} is set to @code{verbose}, or
  2163. @code{veryverbose} the binary will contain DEBUG messages (still, it will
  2164. be necessary to run with @command{-L DEBUG} or set the DEBUG config option
  2165. to show
  2166. them).
  2167. @end itemize
  2168. If you are a developer:
  2169. @itemize @bullet
  2170. @item please make sure that you @code{./configure
  2171. --enable-logging=@{verbose,veryverbose@}}, so you can see DEBUG messages.
  2172. @item please remove the @code{#if} statements around @code{GNUNET_log
  2173. (GNUNET_ERROR_TYPE_DEBUG, ...)} lines, to improve the readability of your
  2174. code.
  2175. @end itemize
  2176. Since now activating DEBUG automatically makes it VERBOSE and activates
  2177. @strong{all} debug messages by default, you probably want to use the
  2178. @uref{https://docs.gnunet.org/#Logging, https://docs.gnunet.org/#Logging}
  2179. functionality to filter only relevant messages.
  2180. A suitable configuration could be:
  2181. @example
  2182. $ export GNUNET_FORCE_LOG="^YOUR_SUBSYSTEM$;;;;DEBUG/;;;;WARNING"
  2183. @end example
  2184. Which will behave almost like enabling DEBUG in that subsystem before the
  2185. change. Of course you can adapt it to your particular needs, this is only
  2186. a quick example.
  2187. @cindex Interprocess communication API
  2188. @cindex ICP
  2189. @node Interprocess communication API (IPC)
  2190. @subsection Interprocess communication API (IPC)
  2191. In GNUnet a variety of new message types might be defined and used in
  2192. interprocess communication, in this tutorial we use the
  2193. @code{struct AddressLookupMessage} as a example to introduce how to
  2194. construct our own message type in GNUnet and how to implement the message
  2195. communication between service and client.
  2196. (Here, a client uses the @code{struct AddressLookupMessage} as a request
  2197. to ask the server to return the address of any other peer connecting to
  2198. the service.)
  2199. @c ***********************************************************************
  2200. @menu
  2201. * Define new message types::
  2202. * Define message struct::
  2203. * Client - Establish connection::
  2204. * Client - Initialize request message::
  2205. * Client - Send request and receive response::
  2206. * Server - Startup service::
  2207. * Server - Add new handles for specified messages::
  2208. * Server - Process request message::
  2209. * Server - Response to client::
  2210. * Server - Notification of clients::
  2211. * Conversion between Network Byte Order (Big Endian) and Host Byte Order::
  2212. @end menu
  2213. @node Define new message types
  2214. @subsubsection Define new message types
  2215. First of all, you should define the new message type in
  2216. @file{gnunet_protocols.h}:
  2217. @example
  2218. // Request to look addresses of peers in server.
  2219. #define GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_LOOKUP 29
  2220. // Response to the address lookup request.
  2221. #define GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY 30
  2222. @end example
  2223. @c ***********************************************************************
  2224. @node Define message struct
  2225. @subsubsection Define message struct
  2226. After the type definition, the specified message structure should also be
  2227. described in the header file, e.g. transport.h in our case.
  2228. @example
  2229. struct AddressLookupMessage @{
  2230. struct GNUNET_MessageHeader header;
  2231. int32_t numeric_only GNUNET_PACKED;
  2232. struct GNUNET_TIME_AbsoluteNBO timeout;
  2233. uint32_t addrlen GNUNET_PACKED;
  2234. /* followed by 'addrlen' bytes of the actual address, then
  2235. followed by the 0-terminated name of the transport */ @};
  2236. GNUNET_NETWORK_STRUCT_END
  2237. @end example
  2238. Please note @code{GNUNET_NETWORK_STRUCT_BEGIN} and @code{GNUNET_PACKED}
  2239. which both ensure correct alignment when sending structs over the network.
  2240. @menu
  2241. @end menu
  2242. @c ***********************************************************************
  2243. @node Client - Establish connection
  2244. @subsubsection Client - Establish connection
  2245. At first, on the client side, the underlying API is employed to create a
  2246. new connection to a service, in our example the transport service would be
  2247. connected.
  2248. @example
  2249. struct GNUNET_CLIENT_Connection *client;
  2250. client = GNUNET_CLIENT_connect ("transport", cfg);
  2251. @end example
  2252. @c ***********************************************************************
  2253. @node Client - Initialize request message
  2254. @subsubsection Client - Initialize request message
  2255. When the connection is ready, we initialize the message. In this step,
  2256. all the fields of the message should be properly initialized, namely the
  2257. size, type, and some extra user-defined data, such as timeout, name of
  2258. transport, address and name of transport.
  2259. @example
  2260. struct AddressLookupMessage *msg;
  2261. size_t len = sizeof (struct AddressLookupMessage)
  2262. + addressLen
  2263. + strlen (nameTrans)
  2264. + 1;
  2265. msg->header->size = htons (len);
  2266. msg->header->type = htons
  2267. (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_LOOKUP);
  2268. msg->timeout = GNUNET_TIME_absolute_hton (abs_timeout);
  2269. msg->addrlen = htonl (addressLen);
  2270. char *addrbuf = (char *) &msg[1];
  2271. memcpy (addrbuf, address, addressLen);
  2272. char *tbuf = &addrbuf[addressLen];
  2273. memcpy (tbuf, nameTrans, strlen (nameTrans) + 1);
  2274. @end example
  2275. Note that, here the functions @code{htonl}, @code{htons} and
  2276. @code{GNUNET_TIME_absolute_hton} are applied to convert little endian
  2277. into big endian, about the usage of the big/small endian order and the
  2278. corresponding conversion function please refer to Introduction of
  2279. Big Endian and Little Endian.
  2280. @c ***********************************************************************
  2281. @node Client - Send request and receive response
  2282. @subsubsection Client - Send request and receive response
  2283. @b{FIXME: This is very outdated, see the tutorial for the current API!}
  2284. Next, the client would send the constructed message as a request to the
  2285. service and wait for the response from the service. To accomplish this
  2286. goal, there are a number of API calls that can be used. In this example,
  2287. @code{GNUNET_CLIENT_transmit_and_get_response} is chosen as the most
  2288. appropriate function to use.
  2289. @example
  2290. GNUNET_CLIENT_transmit_and_get_response
  2291. (client, msg->header, timeout, GNUNET_YES, &address_response_processor,
  2292. arp_ctx);
  2293. @end example
  2294. the argument @code{address_response_processor} is a function with
  2295. @code{GNUNET_CLIENT_MessageHandler} type, which is used to process the
  2296. reply message from the service.
  2297. @node Server - Startup service
  2298. @subsubsection Server - Startup service
  2299. After receiving the request message, we run a standard GNUnet service
  2300. startup sequence using @code{GNUNET_SERVICE_run}, as follows,
  2301. @example
  2302. int main(int argc, char**argv) @{
  2303. GNUNET_SERVICE_run(argc, argv, "transport"
  2304. GNUNET_SERVICE_OPTION_NONE, &run, NULL)); @}
  2305. @end example
  2306. @c ***********************************************************************
  2307. @node Server - Add new handles for specified messages
  2308. @subsubsection Server - Add new handles for specified messages
  2309. in the function above the argument @code{run} is used to initiate
  2310. transport service,and defined like this:
  2311. @example
  2312. static void run (void *cls,
  2313. struct GNUNET_SERVER_Handle *serv,
  2314. const struct GNUNET_CONFIGURATION_Handle *cfg) @{
  2315. GNUNET_SERVER_add_handlers (serv, handlers); @}
  2316. @end example
  2317. Here, @code{GNUNET_SERVER_add_handlers} must be called in the run
  2318. function to add new handlers in the service. The parameter
  2319. @code{handlers} is a list of @code{struct GNUNET_SERVER_MessageHandler}
  2320. to tell the service which function should be called when a particular
  2321. type of message is received, and should be defined in this way:
  2322. @example
  2323. static struct GNUNET_SERVER_MessageHandler handlers[] = @{
  2324. @{&handle_start,
  2325. NULL,
  2326. GNUNET_MESSAGE_TYPE_TRANSPORT_START,
  2327. 0@},
  2328. @{&handle_send,
  2329. NULL,
  2330. GNUNET_MESSAGE_TYPE_TRANSPORT_SEND,
  2331. 0@},
  2332. @{&handle_try_connect,
  2333. NULL,
  2334. GNUNET_MESSAGE_TYPE_TRANSPORT_TRY_CONNECT,
  2335. sizeof (struct TryConnectMessage)
  2336. @},
  2337. @{&handle_address_lookup,
  2338. NULL,
  2339. GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_LOOKUP,
  2340. 0@},
  2341. @{NULL,
  2342. NULL,
  2343. 0,
  2344. 0@}
  2345. @};
  2346. @end example
  2347. As shown, the first member of the struct in the first area is a callback
  2348. function, which is called to process the specified message types, given
  2349. as the third member. The second parameter is the closure for the callback
  2350. function, which is set to @code{NULL} in most cases, and the last
  2351. parameter is the expected size of the message of this type, usually we
  2352. set it to 0 to accept variable size, for special cases the exact size of
  2353. the specified message also can be set. In addition, the terminator sign
  2354. depicted as @code{@{NULL, NULL, 0, 0@}} is set in the last area.
  2355. @c ***********************************************************************
  2356. @node Server - Process request message
  2357. @subsubsection Server - Process request message
  2358. After the initialization of transport service, the request message would
  2359. be processed. Before handling the main message data, the validity of this
  2360. message should be checked out, e.g., to check whether the size of message
  2361. is correct.
  2362. @example
  2363. size = ntohs (message->size);
  2364. if (size < sizeof (struct AddressLookupMessage)) @{
  2365. GNUNET_break_op (0);
  2366. GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
  2367. return; @}
  2368. @end example
  2369. Note that, opposite to the construction method of the request message in
  2370. the client, in the server the function @code{nothl} and @code{ntohs}
  2371. should be employed during the extraction of the data from the message, so
  2372. that the data in big endian order can be converted back into little
  2373. endian order. See more in detail please refer to Introduction of
  2374. Big Endian and Little Endian.
  2375. Moreover in this example, the name of the transport stored in the message
  2376. is a 0-terminated string, so we should also check whether the name of the
  2377. transport in the received message is 0-terminated:
  2378. @example
  2379. nameTransport = (const char *) &address[addressLen];
  2380. if (nameTransport[size - sizeof
  2381. (struct AddressLookupMessage)
  2382. - addressLen - 1] != '\0') @{
  2383. GNUNET_break_op (0);
  2384. GNUNET_SERVER_receive_done (client,
  2385. GNUNET_SYSERR);
  2386. return; @}
  2387. @end example
  2388. Here, @code{GNUNET_SERVER_receive_done} should be called to tell the
  2389. service that the request is done and can receive the next message. The
  2390. argument @code{GNUNET_SYSERR} here indicates that the service didn't
  2391. understand the request message, and the processing of this request would
  2392. be terminated.
  2393. In comparison to the aforementioned situation, when the argument is equal
  2394. to @code{GNUNET_OK}, the service would continue to process the request
  2395. message.
  2396. @c ***********************************************************************
  2397. @node Server - Response to client
  2398. @subsubsection Server - Response to client
  2399. Once the processing of current request is done, the server should give the
  2400. response to the client. A new @code{struct AddressLookupMessage} would be
  2401. produced by the server in a similar way as the client did and sent to the
  2402. client, but here the type should be
  2403. @code{GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY} rather than
  2404. @code{GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_LOOKUP} in client.
  2405. @example
  2406. struct AddressLookupMessage *msg;
  2407. size_t len = sizeof (struct AddressLookupMessage)
  2408. + addressLen
  2409. + strlen (nameTrans) + 1;
  2410. msg->header->size = htons (len);
  2411. msg->header->type = htons
  2412. (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
  2413. // ...
  2414. struct GNUNET_SERVER_TransmitContext *tc;
  2415. tc = GNUNET_SERVER_transmit_context_create (client);
  2416. GNUNET_SERVER_transmit_context_append_data
  2417. (tc,
  2418. NULL,
  2419. 0,
  2420. GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
  2421. GNUNET_SERVER_transmit_context_run (tc, rtimeout);
  2422. @end example
  2423. Note that, there are also a number of other APIs provided to the service
  2424. to send the message.
  2425. @c ***********************************************************************
  2426. @node Server - Notification of clients
  2427. @subsubsection Server - Notification of clients
  2428. Often a service needs to (repeatedly) transmit notifications to a client
  2429. or a group of clients. In these cases, the client typically has once
  2430. registered for a set of events and then needs to receive a message
  2431. whenever such an event happens (until the client disconnects). The use of
  2432. a notification context can help manage message queues to clients and
  2433. handle disconnects. Notification contexts can be used to send
  2434. individualized messages to a particular client or to broadcast messages
  2435. to a group of clients. An individualized notification might look like
  2436. this:
  2437. @example
  2438. GNUNET_SERVER_notification_context_unicast(nc,
  2439. client,
  2440. msg,
  2441. GNUNET_YES);
  2442. @end example
  2443. Note that after processing the original registration message for
  2444. notifications, the server code still typically needs to call
  2445. @code{GNUNET_SERVER_receive_done} so that the client can transmit further
  2446. messages to the server.
  2447. @c ***********************************************************************
  2448. @node Conversion between Network Byte Order (Big Endian) and Host Byte Order
  2449. @subsubsection Conversion between Network Byte Order (Big Endian) and Host Byte Order
  2450. @c %** subsub? it's a referenced page on the ipc document.
  2451. Here we can simply comprehend big endian and little endian as Network Byte
  2452. Order and Host Byte Order respectively. What is the difference between
  2453. both two?
  2454. Usually in our host computer we store the data byte as Host Byte Order,
  2455. for example, we store a integer in the RAM which might occupies 4 Byte,
  2456. as Host Byte Order the higher Byte would be stored at the lower address
  2457. of RAM, and the lower Byte would be stored at the higher address of RAM.
  2458. However, contrast to this, Network Byte Order just take the totally
  2459. opposite way to store the data, says, it will store the lower Byte at the
  2460. lower address, and the higher Byte will stay at higher address.
  2461. For the current communication of network, we normally exchange the
  2462. information by surveying the data package, every two host wants to
  2463. communicate with each other must send and receive data package through
  2464. network. In order to maintain the identity of data through the
  2465. transmission in the network, the order of the Byte storage must changed
  2466. before sending and after receiving the data.
  2467. There ten convenient functions to realize the conversion of Byte Order in
  2468. GNUnet, as following:
  2469. @table @asis
  2470. @item uint16_t htons(uint16_t hostshort) Convert host byte order to net
  2471. byte order with short int
  2472. @item uint32_t htonl(uint32_t hostlong) Convert host byte
  2473. order to net byte order with long int
  2474. @item uint16_t ntohs(uint16_t netshort)
  2475. Convert net byte order to host byte order with short int
  2476. @item uint32_t
  2477. ntohl(uint32_t netlong) Convert net byte order to host byte order with
  2478. long int
  2479. @item unsigned long long GNUNET_ntohll (unsigned long long netlonglong)
  2480. Convert net byte order to host byte order with long long int
  2481. @item unsigned long long GNUNET_htonll (unsigned long long hostlonglong)
  2482. Convert host byte order to net byte order with long long int
  2483. @item struct GNUNET_TIME_RelativeNBO GNUNET_TIME_relative_hton
  2484. (struct GNUNET_TIME_Relative a) Convert relative time to network byte
  2485. order.
  2486. @item struct GNUNET_TIME_Relative GNUNET_TIME_relative_ntoh
  2487. (struct GNUNET_TIME_RelativeNBO a) Convert relative time from network
  2488. byte order.
  2489. @item struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton
  2490. (struct GNUNET_TIME_Absolute a) Convert relative time to network byte
  2491. order.
  2492. @item struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh
  2493. (struct GNUNET_TIME_AbsoluteNBO a) Convert relative time from network
  2494. byte order.
  2495. @end table
  2496. @cindex Cryptography API
  2497. @node Cryptography API
  2498. @subsection Cryptography API
  2499. The gnunetutil APIs provides the cryptographic primitives used in GNUnet.
  2500. GNUnet uses 2048 bit RSA keys for the session key exchange and for signing
  2501. messages by peers and most other public-key operations. Most researchers
  2502. in cryptography consider 2048 bit RSA keys as secure and practically
  2503. unbreakable for a long time. The API provides functions to create a fresh
  2504. key pair, read a private key from a file (or create a new file if the
  2505. file does not exist), encrypt, decrypt, sign, verify and extraction of
  2506. the public key into a format suitable for network transmission.
  2507. For the encryption of files and the actual data exchanged between peers
  2508. GNUnet uses 256-bit AES encryption. Fresh, session keys are negotiated
  2509. for every new connection.@ Again, there is no published technique to
  2510. break this cipher in any realistic amount of time. The API provides
  2511. functions for generation of keys, validation of keys (important for
  2512. checking that decryptions using RSA succeeded), encryption and decryption.
  2513. GNUnet uses SHA-512 for computing one-way hash codes. The API provides
  2514. functions to compute a hash over a block in memory or over a file on disk.
  2515. The crypto API also provides functions for randomizing a block of memory,
  2516. obtaining a single random number and for generating a permutation of the
  2517. numbers 0 to n-1. Random number generation distinguishes between WEAK and
  2518. STRONG random number quality; WEAK random numbers are pseudo-random
  2519. whereas STRONG random numbers use entropy gathered from the operating
  2520. system.
  2521. Finally, the crypto API provides a means to deterministically generate a
  2522. 1024-bit RSA key from a hash code. These functions should most likely not
  2523. be used by most applications; most importantly,
  2524. GNUNET_CRYPTO_rsa_key_create_from_hash does not create an RSA-key that
  2525. should be considered secure for traditional applications of RSA.
  2526. @cindex Message Queue API
  2527. @node Message Queue API
  2528. @subsection Message Queue API
  2529. @strong{ Introduction }@
  2530. Often, applications need to queue messages that
  2531. are to be sent to other GNUnet peers, clients or services. As all of
  2532. GNUnet's message-based communication APIs, by design, do not allow
  2533. messages to be queued, it is common to implement custom message queues
  2534. manually when they are needed. However, writing very similar code in
  2535. multiple places is tedious and leads to code duplication.
  2536. MQ (for Message Queue) is an API that provides the functionality to
  2537. implement and use message queues. We intend to eventually replace all of
  2538. the custom message queue implementations in GNUnet with MQ.
  2539. @strong{ Basic Concepts }@
  2540. The two most important entities in MQ are queues and envelopes.
  2541. Every queue is backed by a specific implementation (e.g. for mesh, stream,
  2542. connection, server client, etc.) that will actually deliver the queued
  2543. messages. For convenience,@ some queues also allow to specify a list of
  2544. message handlers. The message queue will then also wait for incoming
  2545. messages and dispatch them appropriately.
  2546. An envelope holds the the memory for a message, as well as metadata
  2547. (Where is the envelope queued? What should happen after it has been
  2548. sent?). Any envelope can only be queued in one message queue.
  2549. @strong{ Creating Queues }@
  2550. The following is a list of currently available message queues. Note that
  2551. to avoid layering issues, message queues for higher level APIs are not
  2552. part of @code{libgnunetutil}, but@ the respective API itself provides the
  2553. queue implementation.
  2554. @table @asis
  2555. @item @code{GNUNET_MQ_queue_for_connection_client}
  2556. Transmits queued messages over a @code{GNUNET_CLIENT_Connection} handle.
  2557. Also supports receiving with message handlers.
  2558. @item @code{GNUNET_MQ_queue_for_server_client}
  2559. Transmits queued messages over a @code{GNUNET_SERVER_Client} handle. Does
  2560. not support incoming message handlers.
  2561. @item @code{GNUNET_MESH_mq_create} Transmits queued messages over a
  2562. @code{GNUNET_MESH_Tunnel} handle. Does not support incoming message
  2563. handlers.
  2564. @item @code{GNUNET_MQ_queue_for_callbacks} This is the most general
  2565. implementation. Instead of delivering and receiving messages with one of
  2566. GNUnet's communication APIs, implementation callbacks are called. Refer to
  2567. "Implementing Queues" for a more detailed explanation.
  2568. @end table
  2569. @strong{ Allocating Envelopes }@
  2570. A GNUnet message (as defined by the GNUNET_MessageHeader) has three
  2571. parts: The size, the type, and the body.
  2572. MQ provides macros to allocate an envelope containing a message
  2573. conveniently, automatically setting the size and type fields of the
  2574. message.
  2575. Consider the following simple message, with the body consisting of a
  2576. single number value.
  2577. @c why the empty code function?
  2578. @code{}
  2579. @example
  2580. struct NumberMessage @{
  2581. /** Type: GNUNET_MESSAGE_TYPE_EXAMPLE_1 */
  2582. struct GNUNET_MessageHeader header;
  2583. uint32_t number GNUNET_PACKED;
  2584. @};
  2585. @end example
  2586. An envelope containing an instance of the NumberMessage can be
  2587. constructed like this:
  2588. @example
  2589. struct GNUNET_MQ_Envelope *ev;
  2590. struct NumberMessage *msg;
  2591. ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_EXAMPLE_1);
  2592. msg->number = htonl (42);
  2593. @end example
  2594. In the above code, @code{GNUNET_MQ_msg} is a macro. The return value is
  2595. the newly allocated envelope. The first argument must be a pointer to some
  2596. @code{struct} containing a @code{struct GNUNET_MessageHeader header}
  2597. field, while the second argument is the desired message type, in host
  2598. byte order.
  2599. The @code{msg} pointer now points to an allocated message, where the
  2600. message type and the message size are already set. The message's size is
  2601. inferred from the type of the @code{msg} pointer: It will be set to
  2602. 'sizeof(*msg)', properly converted to network byte order.
  2603. If the message body's size is dynamic, the the macro
  2604. @code{GNUNET_MQ_msg_extra} can be used to allocate an envelope whose
  2605. message has additional space allocated after the @code{msg} structure.
  2606. If no structure has been defined for the message,
  2607. @code{GNUNET_MQ_msg_header_extra} can be used to allocate additional space
  2608. after the message header. The first argument then must be a pointer to a
  2609. @code{GNUNET_MessageHeader}.
  2610. @strong{Envelope Properties}@
  2611. A few functions in MQ allow to set additional properties on envelopes:
  2612. @table @asis
  2613. @item @code{GNUNET_MQ_notify_sent} Allows to specify a function that will
  2614. be called once the envelope's message has been sent irrevocably.
  2615. An envelope can be canceled precisely up to the@ point where the notify
  2616. sent callback has been called.
  2617. @item @code{GNUNET_MQ_disable_corking} No corking will be used when
  2618. sending the message. Not every@ queue supports this flag, per default,
  2619. envelopes are sent with corking.@
  2620. @end table
  2621. @strong{Sending Envelopes}@
  2622. Once an envelope has been constructed, it can be queued for sending with
  2623. @code{GNUNET_MQ_send}.
  2624. Note that in order to avoid memory leaks, an envelope must either be sent
  2625. (the queue will free it) or destroyed explicitly with
  2626. @code{GNUNET_MQ_discard}.
  2627. @strong{Canceling Envelopes}@
  2628. An envelope queued with @code{GNUNET_MQ_send} can be canceled with
  2629. @code{GNUNET_MQ_cancel}. Note that after the notify sent callback has
  2630. been called, canceling a message results in undefined behavior.
  2631. Thus it is unsafe to cancel an envelope that does not have a notify sent
  2632. callback. When canceling an envelope, it is not necessary@ to call
  2633. @code{GNUNET_MQ_discard}, and the envelope can't be sent again.
  2634. @strong{ Implementing Queues }@
  2635. @code{TODO}
  2636. @cindex Service API
  2637. @node Service API
  2638. @subsection Service API
  2639. Most GNUnet code lives in the form of services. Services are processes
  2640. that offer an API for other components of the system to build on. Those
  2641. other components can be command-line tools for users, graphical user
  2642. interfaces or other services. Services provide their API using an IPC
  2643. protocol. For this, each service must listen on either a TCP port or a
  2644. UNIX domain socket; for this, the service implementation uses the server
  2645. API. This use of server is exposed directly to the users of the service
  2646. API. Thus, when using the service API, one is usually also often using
  2647. large parts of the server API. The service API provides various
  2648. convenience functions, such as parsing command-line arguments and the
  2649. configuration file, which are not found in the server API.
  2650. The dual to the service/server API is the client API, which can be used to
  2651. access services.
  2652. The most common way to start a service is to use the
  2653. @code{GNUNET_SERVICE_run} function from the program's main function.
  2654. @code{GNUNET_SERVICE_run} will then parse the command line and
  2655. configuration files and, based on the options found there,
  2656. start the server. It will then give back control to the main
  2657. program, passing the server and the configuration to the
  2658. @code{GNUNET_SERVICE_Main} callback. @code{GNUNET_SERVICE_run}
  2659. will also take care of starting the scheduler loop.
  2660. If this is inappropriate (for example, because the scheduler loop
  2661. is already running), @code{GNUNET_SERVICE_start} and
  2662. related functions provide an alternative to @code{GNUNET_SERVICE_run}.
  2663. When starting a service, the service_name option is used to determine
  2664. which sections in the configuration file should be used to configure the
  2665. service. A typical value here is the name of the @file{src/}
  2666. sub-directory, for example @file{statistics}.
  2667. The same string would also be given to
  2668. @code{GNUNET_CLIENT_connect} to access the service.
  2669. Once a service has been initialized, the program should use the
  2670. @code{GNUNET_SERVICE_Main} callback to register message handlers
  2671. using @code{GNUNET_SERVER_add_handlers}.
  2672. The service will already have registered a handler for the
  2673. "TEST" message.
  2674. @findex GNUNET_SERVICE_Options
  2675. The option bitfield (@code{enum GNUNET_SERVICE_Options})
  2676. determines how a service should behave during shutdown.
  2677. There are three key strategies:
  2678. @table @asis
  2679. @item instant (@code{GNUNET_SERVICE_OPTION_NONE})
  2680. Upon receiving the shutdown
  2681. signal from the scheduler, the service immediately terminates the server,
  2682. closing all existing connections with clients.
  2683. @item manual (@code{GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN})
  2684. The service does nothing by itself
  2685. during shutdown. The main program will need to take the appropriate
  2686. action by calling GNUNET_SERVER_destroy or GNUNET_SERVICE_stop (depending
  2687. on how the service was initialized) to terminate the service. This method
  2688. is used by gnunet-service-arm and rather uncommon.
  2689. @item soft (@code{GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN})
  2690. Upon receiving the shutdown signal from the scheduler,
  2691. the service immediately tells the server to stop
  2692. listening for incoming clients. Requests from normal existing clients are
  2693. still processed and the server/service terminates once all normal clients
  2694. have disconnected. Clients that are not expected to ever disconnect (such
  2695. as clients that monitor performance values) can be marked as 'monitor'
  2696. clients using GNUNET_SERVER_client_mark_monitor. Those clients will
  2697. continue to be processed until all 'normal' clients have disconnected.
  2698. Then, the server will terminate, closing the monitor connections.
  2699. This mode is for example used by 'statistics', allowing existing 'normal'
  2700. clients to set (possibly persistent) statistic values before terminating.
  2701. @end table
  2702. @c ***********************************************************************
  2703. @node Optimizing Memory Consumption of GNUnet's (Multi-) Hash Maps
  2704. @subsection Optimizing Memory Consumption of GNUnet's (Multi-) Hash Maps
  2705. A commonly used data structure in GNUnet is a (multi-)hash map. It is most
  2706. often used to map a peer identity to some data structure, but also to map
  2707. arbitrary keys to values (for example to track requests in the distributed
  2708. hash table or in file-sharing). As it is commonly used, the DHT is
  2709. actually sometimes responsible for a large share of GNUnet's overall
  2710. memory consumption (for some processes, 30% is not uncommon). The
  2711. following text documents some API quirks (and their implications for
  2712. applications) that were recently introduced to minimize the footprint of
  2713. the hash map.
  2714. @c ***********************************************************************
  2715. @menu
  2716. * Analysis::
  2717. * Solution::
  2718. * Migration::
  2719. * Conclusion::
  2720. * Availability::
  2721. @end menu
  2722. @node Analysis
  2723. @subsubsection Analysis
  2724. The main reason for the "excessive" memory consumption by the hash map is
  2725. that GNUnet uses 512-bit cryptographic hash codes --- and the
  2726. (multi-)hash map also uses the same 512-bit 'struct GNUNET_HashCode'. As
  2727. a result, storing just the keys requires 64 bytes of memory for each key.
  2728. As some applications like to keep a large number of entries in the hash
  2729. map (after all, that's what maps are good for), 64 bytes per hash is
  2730. significant: keeping a pointer to the value and having a linked list for
  2731. collisions consume between 8 and 16 bytes, and 'malloc' may add about the
  2732. same overhead per allocation, putting us in the 16 to 32 byte per entry
  2733. ballpark. Adding a 64-byte key then triples the overall memory
  2734. requirement for the hash map.
  2735. To make things "worse", most of the time storing the key in the hash map
  2736. is not required: it is typically already in memory elsewhere! In most
  2737. cases, the values stored in the hash map are some application-specific
  2738. struct that _also_ contains the hash. Here is a simplified example:
  2739. @example
  2740. struct MyValue @{
  2741. struct GNUNET_HashCode key;
  2742. unsigned int my_data; @};
  2743. // ...
  2744. val = GNUNET_malloc (sizeof (struct MyValue));
  2745. val->key = key;
  2746. val->my_data = 42;
  2747. GNUNET_CONTAINER_multihashmap_put (map, &key, val, ...);
  2748. @end example
  2749. This is a common pattern as later the entries might need to be removed,
  2750. and at that time it is convenient to have the key immediately at hand:
  2751. @example
  2752. GNUNET_CONTAINER_multihashmap_remove (map, &val->key, val);
  2753. @end example
  2754. Note that here we end up with two times 64 bytes for the key, plus maybe
  2755. 64 bytes total for the rest of the 'struct MyValue' and the map entry in
  2756. the hash map. The resulting redundant storage of the key increases
  2757. overall memory consumption per entry from the "optimal" 128 bytes to 192
  2758. bytes. This is not just an extreme example: overheads in practice are
  2759. actually sometimes close to those highlighted in this example. This is
  2760. especially true for maps with a significant number of entries, as there
  2761. we tend to really try to keep the entries small.
  2762. @c ***********************************************************************
  2763. @node Solution
  2764. @subsubsection Solution
  2765. The solution that has now been implemented is to @strong{optionally}
  2766. allow the hash map to not make a (deep) copy of the hash but instead have
  2767. a pointer to the hash/key in the entry. This reduces the memory
  2768. consumption for the key from 64 bytes to 4 to 8 bytes. However, it can
  2769. also only work if the key is actually stored in the entry (which is the
  2770. case most of the time) and if the entry does not modify the key (which in
  2771. all of the code I'm aware of has been always the case if there key is
  2772. stored in the entry). Finally, when the client stores an entry in the
  2773. hash map, it @strong{must} provide a pointer to the key within the entry,
  2774. not just a pointer to a transient location of the key. If
  2775. the client code does not meet these requirements, the result is a dangling
  2776. pointer and undefined behavior of the (multi-)hash map API.
  2777. @c ***********************************************************************
  2778. @node Migration
  2779. @subsubsection Migration
  2780. To use the new feature, first check that the values contain the respective
  2781. key (and never modify it). Then, all calls to
  2782. @code{GNUNET_CONTAINER_multihashmap_put} on the respective map must be
  2783. audited and most likely changed to pass a pointer into the value's struct.
  2784. For the initial example, the new code would look like this:
  2785. @example
  2786. struct MyValue @{
  2787. struct GNUNET_HashCode key;
  2788. unsigned int my_data; @};
  2789. // ...
  2790. val = GNUNET_malloc (sizeof (struct MyValue));
  2791. val->key = key; val->my_data = 42;
  2792. GNUNET_CONTAINER_multihashmap_put (map, &val->key, val, ...);
  2793. @end example
  2794. Note that @code{&val} was changed to @code{&val->key} in the argument to
  2795. the @code{put} call. This is critical as often @code{key} is on the stack
  2796. or in some other transient data structure and thus having the hash map
  2797. keep a pointer to @code{key} would not work. Only the key inside of
  2798. @code{val} has the same lifetime as the entry in the map (this must of
  2799. course be checked as well). Naturally, @code{val->key} must be
  2800. initialized before the @code{put} call. Once all @code{put} calls have
  2801. been converted and double-checked, you can change the call to create the
  2802. hash map from
  2803. @example
  2804. map =
  2805. GNUNET_CONTAINER_multihashmap_create (SIZE, GNUNET_NO);
  2806. @end example
  2807. to
  2808. @example
  2809. map = GNUNET_CONTAINER_multihashmap_create (SIZE, GNUNET_YES);
  2810. @end example
  2811. If everything was done correctly, you now use about 60 bytes less memory
  2812. per entry in @code{map}. However, if now (or in the future) any call to
  2813. @code{put} does not ensure that the given key is valid until the entry is
  2814. removed from the map, undefined behavior is likely to be observed.
  2815. @c ***********************************************************************
  2816. @node Conclusion
  2817. @subsubsection Conclusion
  2818. The new optimization can is often applicable and can result in a
  2819. reduction in memory consumption of up to 30% in practice. However, it
  2820. makes the code less robust as additional invariants are imposed on the
  2821. multi hash map client. Thus applications should refrain from enabling the
  2822. new mode unless the resulting performance increase is deemed significant
  2823. enough. In particular, it should generally not be used in new code (wait
  2824. at least until benchmarks exist).
  2825. @c ***********************************************************************
  2826. @node Availability
  2827. @subsubsection Availability
  2828. The new multi hash map code was committed in SVN 24319 (which made its
  2829. way into GNUnet version 0.9.4).
  2830. Various subsystems (transport, core, dht, file-sharing) were
  2831. previously audited and modified to take advantage of the new capability.
  2832. In particular, memory consumption of the file-sharing service is expected
  2833. to drop by 20-30% due to this change.
  2834. @cindex CONTAINER_MDLL API
  2835. @node CONTAINER_MDLL API
  2836. @subsection CONTAINER_MDLL API
  2837. This text documents the GNUNET_CONTAINER_MDLL API. The
  2838. GNUNET_CONTAINER_MDLL API is similar to the GNUNET_CONTAINER_DLL API in
  2839. that it provides operations for the construction and manipulation of
  2840. doubly-linked lists. The key difference to the (simpler) DLL-API is that
  2841. the MDLL-version allows a single element (instance of a "struct") to be
  2842. in multiple linked lists at the same time.
  2843. Like the DLL API, the MDLL API stores (most of) the data structures for
  2844. the doubly-linked list with the respective elements; only the 'head' and
  2845. 'tail' pointers are stored "elsewhere" --- and the application needs to
  2846. provide the locations of head and tail to each of the calls in the
  2847. MDLL API. The key difference for the MDLL API is that the "next" and
  2848. "previous" pointers in the struct can no longer be simply called "next"
  2849. and "prev" --- after all, the element may be in multiple doubly-linked
  2850. lists, so we cannot just have one "next" and one "prev" pointer!
  2851. The solution is to have multiple fields that must have a name of the
  2852. format "next_XX" and "prev_XX" where "XX" is the name of one of the
  2853. doubly-linked lists. Here is a simple example:
  2854. @example
  2855. struct MyMultiListElement @{
  2856. struct MyMultiListElement *next_ALIST;
  2857. struct MyMultiListElement *prev_ALIST;
  2858. struct MyMultiListElement *next_BLIST;
  2859. struct MyMultiListElement *prev_BLIST;
  2860. void
  2861. *data;
  2862. @};
  2863. @end example
  2864. Note that by convention, we use all-uppercase letters for the list names.
  2865. In addition, the program needs to have a location for the head and tail
  2866. pointers for both lists, for example:
  2867. @example
  2868. static struct MyMultiListElement *head_ALIST;
  2869. static struct MyMultiListElement *tail_ALIST;
  2870. static struct MyMultiListElement *head_BLIST;
  2871. static struct MyMultiListElement *tail_BLIST;
  2872. @end example
  2873. Using the MDLL-macros, we can now insert an element into the ALIST:
  2874. @example
  2875. GNUNET_CONTAINER_MDLL_insert (ALIST, head_ALIST, tail_ALIST, element);
  2876. @end example
  2877. Passing "ALIST" as the first argument to MDLL specifies which of the
  2878. next/prev fields in the 'struct MyMultiListElement' should be used. The
  2879. extra "ALIST" argument and the "_ALIST" in the names of the
  2880. next/prev-members are the only differences between the MDDL and DLL-API.
  2881. Like the DLL-API, the MDLL-API offers functions for inserting (at head,
  2882. at tail, after a given element) and removing elements from the list.
  2883. Iterating over the list should be done by directly accessing the
  2884. "next_XX" and/or "prev_XX" members.
  2885. @cindex Automatic Restart Manager
  2886. @cindex ARM
  2887. @node Automatic Restart Manager (ARM)
  2888. @section Automatic Restart Manager (ARM)
  2889. GNUnet's Automated Restart Manager (ARM) is the GNUnet service responsible
  2890. for system initialization and service babysitting. ARM starts and halts
  2891. services, detects configuration changes and restarts services impacted by
  2892. the changes as needed. It's also responsible for restarting services in
  2893. case of crashes and is planned to incorporate automatic debugging for
  2894. diagnosing service crashes providing developers insights about crash
  2895. reasons. The purpose of this document is to give GNUnet developer an idea
  2896. about how ARM works and how to interact with it.
  2897. @menu
  2898. * Basic functionality::
  2899. * Key configuration options::
  2900. * ARM - Availability::
  2901. * Reliability::
  2902. @end menu
  2903. @c ***********************************************************************
  2904. @node Basic functionality
  2905. @subsection Basic functionality
  2906. @itemize @bullet
  2907. @item ARM source code can be found under "src/arm".@ Service processes are
  2908. managed by the functions in "gnunet-service-arm.c" which is controlled
  2909. with "gnunet-arm.c" (main function in that file is ARM's entry point).
  2910. @item The functions responsible for communicating with ARM , starting and
  2911. stopping services -including ARM service itself- are provided by the
  2912. ARM API "arm_api.c".@ Function: GNUNET_ARM_connect() returns to the caller
  2913. an ARM handle after setting it to the caller's context (configuration and
  2914. scheduler in use). This handle can be used afterwards by the caller to
  2915. communicate with ARM. Functions GNUNET_ARM_start_service() and
  2916. GNUNET_ARM_stop_service() are used for starting and stopping services
  2917. respectively.
  2918. @item A typical example of using these basic ARM services can be found in
  2919. file test_arm_api.c. The test case connects to ARM, starts it, then uses
  2920. it to start a service "resolver", stops the "resolver" then stops "ARM".
  2921. @end itemize
  2922. @c ***********************************************************************
  2923. @node Key configuration options
  2924. @subsection Key configuration options
  2925. Configurations for ARM and services should be available in a .conf file
  2926. (As an example, see test_arm_api_data.conf). When running ARM, the
  2927. configuration file to use should be passed to the command:
  2928. @example
  2929. $ gnunet-arm -s -c configuration_to_use.conf
  2930. @end example
  2931. If no configuration is passed, the default configuration file will be used
  2932. (see GNUNET_PREFIX/share/gnunet/defaults.conf which is created from
  2933. contrib/defaults.conf).@ Each of the services is having a section starting
  2934. by the service name between square brackets, for example: "[arm]".
  2935. The following options configure how ARM configures or interacts with the
  2936. various services:
  2937. @table @asis
  2938. @item PORT Port number on which the service is listening for incoming TCP
  2939. connections. ARM will start the services should it notice a request at
  2940. this port.
  2941. @item HOSTNAME Specifies on which host the service is deployed. Note
  2942. that ARM can only start services that are running on the local system
  2943. (but will not check that the hostname matches the local machine name).
  2944. This option is used by the @code{gnunet_client_lib.h} implementation to
  2945. determine which system to connect to. The default is "localhost".
  2946. @item BINARY The name of the service binary file.
  2947. @item OPTIONS To be passed to the service.
  2948. @item PREFIX A command to pre-pend to the actual command, for example,
  2949. running a service with "valgrind" or "gdb"
  2950. @item DEBUG Run in debug mode (much verbosity).
  2951. @item START_ON_DEMAND ARM will listen to UNIX domain socket and/or TCP port of
  2952. the service and start the service on-demand.
  2953. @item IMMEDIATE_START ARM will always start this service when the peer
  2954. is started.
  2955. @item ACCEPT_FROM IPv4 addresses the service accepts connections from.
  2956. @item ACCEPT_FROM6 IPv6 addresses the service accepts connections from.
  2957. @end table
  2958. Options that impact the operation of ARM overall are in the "[arm]"
  2959. section. ARM is a normal service and has (except for START_ON_DEMAND) all of the
  2960. options that other services do. In addition, ARM has the
  2961. following options:
  2962. @table @asis
  2963. @item GLOBAL_PREFIX Command to be pre-pended to all services that are
  2964. going to run.
  2965. @item GLOBAL_POSTFIX Global option that will be supplied to all the
  2966. services that are going to run.
  2967. @end table
  2968. @c ***********************************************************************
  2969. @node ARM - Availability
  2970. @subsection ARM - Availability
  2971. As mentioned before, one of the features provided by ARM is starting
  2972. services on demand. Consider the example of one service "client" that
  2973. wants to connect to another service a "server". The "client" will ask ARM
  2974. to run the "server". ARM starts the "server". The "server" starts
  2975. listening to incoming connections. The "client" will establish a
  2976. connection with the "server". And then, they will start to communicate
  2977. together.@ One problem with that scheme is that it's slow!@
  2978. The "client" service wants to communicate with the "server" service at
  2979. once and is not willing wait for it to be started and listening to
  2980. incoming connections before serving its request.@ One solution for that
  2981. problem will be that ARM starts all services as default services. That
  2982. solution will solve the problem, yet, it's not quite practical, for some
  2983. services that are going to be started can never be used or are going to
  2984. be used after a relatively long time.@
  2985. The approach followed by ARM to solve this problem is as follows:
  2986. @itemize @bullet
  2987. @item For each service having a PORT field in the configuration file and
  2988. that is not one of the default services ( a service that accepts incoming
  2989. connections from clients), ARM creates listening sockets for all addresses
  2990. associated with that service.
  2991. @item The "client" will immediately establish a connection with
  2992. the "server".
  2993. @item ARM --- pretending to be the "server" --- will listen on the
  2994. respective port and notice the incoming connection from the "client"
  2995. (but not accept it), instead
  2996. @item Once there is an incoming connection, ARM will start the "server",
  2997. passing on the listen sockets (now, the service is started and can do its
  2998. work).
  2999. @item Other client services now can directly connect directly to the
  3000. "server".
  3001. @end itemize
  3002. @c ***********************************************************************
  3003. @node Reliability
  3004. @subsection Reliability
  3005. One of the features provided by ARM, is the automatic restart of crashed
  3006. services.@ ARM needs to know which of the running services died. Function
  3007. "gnunet-service-arm.c/maint_child_death()" is responsible for that. The
  3008. function is scheduled to run upon receiving a SIGCHLD signal. The
  3009. function, then, iterates ARM's list of services running and monitors
  3010. which service has died (crashed). For all crashing services, ARM restarts
  3011. them.@
  3012. Now, considering the case of a service having a serious problem causing it
  3013. to crash each time it's started by ARM. If ARM keeps blindly restarting
  3014. such a service, we are going to have the pattern:
  3015. start-crash-restart-crash-restart-crash and so forth!! Which is of course
  3016. not practical.@
  3017. For that reason, ARM schedules the service to be restarted after waiting
  3018. for some delay that grows exponentially with each crash/restart of that
  3019. service.@ To clarify the idea, considering the following example:
  3020. @itemize @bullet
  3021. @item Service S crashed.
  3022. @item ARM receives the SIGCHLD and inspects its list of services to find
  3023. the dead one(s).
  3024. @item ARM finds S dead and schedules it for restarting after "backoff"
  3025. time which is initially set to 1ms. ARM will double the backoff time
  3026. correspondent to S (now backoff(S) = 2ms)
  3027. @item Because there is a severe problem with S, it crashed again.
  3028. @item Again ARM receives the SIGCHLD and detects that it's S again that's
  3029. crashed. ARM schedules it for restarting but after its new backoff time
  3030. (which became 2ms), and doubles its backoff time (now backoff(S) = 4).
  3031. @item and so on, until backoff(S) reaches a certain threshold
  3032. (@code{EXPONENTIAL_BACKOFF_THRESHOLD} is set to half an hour),
  3033. after reaching it, backoff(S) will remain half an hour,
  3034. hence ARM won't be busy for a lot of time trying to restart a
  3035. problematic service.
  3036. @end itemize
  3037. @cindex TRANSPORT Subsystem
  3038. @node TRANSPORT Subsystem
  3039. @section TRANSPORT Subsystem
  3040. This chapter documents how the GNUnet transport subsystem works. The
  3041. GNUnet transport subsystem consists of three main components: the
  3042. transport API (the interface used by the rest of the system to access the
  3043. transport service), the transport service itself (most of the interesting
  3044. functions, such as choosing transports, happens here) and the transport
  3045. plugins. A transport plugin is a concrete implementation for how two
  3046. GNUnet peers communicate; many plugins exist, for example for
  3047. communication via TCP, UDP, HTTP, HTTPS and others. Finally, the
  3048. transport subsystem uses supporting code, especially the NAT/UPnP
  3049. library to help with tasks such as NAT traversal.
  3050. Key tasks of the transport service include:
  3051. @itemize @bullet
  3052. @item Create our HELLO message, notify clients and neighbours if our HELLO
  3053. changes (using NAT library as necessary)
  3054. @item Validate HELLOs from other peers (send PING), allow other peers to
  3055. validate our HELLO's addresses (send PONG)
  3056. @item Upon request, establish connections to other peers (using address
  3057. selection from ATS subsystem) and maintain them (again using PINGs and
  3058. PONGs) as long as desired
  3059. @item Accept incoming connections, give ATS service the opportunity to
  3060. switch communication channels
  3061. @item Notify clients about peers that have connected to us or that have
  3062. been disconnected from us
  3063. @item If a (stateful) connection goes down unexpectedly (without explicit
  3064. DISCONNECT), quickly attempt to recover (without notifying clients) but do
  3065. notify clients quickly if reconnecting fails
  3066. @item Send (payload) messages arriving from clients to other peers via
  3067. transport plugins and receive messages from other peers, forwarding
  3068. those to clients
  3069. @item Enforce inbound traffic limits (using flow-control if it is
  3070. applicable); outbound traffic limits are enforced by CORE, not by us (!)
  3071. @item Enforce restrictions on P2P connection as specified by the blacklist
  3072. configuration and blacklisting clients
  3073. @end itemize
  3074. Note that the term "clients" in the list above really refers to the
  3075. GNUnet-CORE service, as CORE is typically the only client of the
  3076. transport service.
  3077. @menu
  3078. * Address validation protocol::
  3079. @end menu
  3080. @node Address validation protocol
  3081. @subsection Address validation protocol
  3082. This section documents how the GNUnet transport service validates
  3083. connections with other peers. It is a high-level description of the
  3084. protocol necessary to understand the details of the implementation. It
  3085. should be noted that when we talk about PING and PONG messages in this
  3086. section, we refer to transport-level PING and PONG messages, which are
  3087. different from core-level PING and PONG messages (both in implementation
  3088. and function).
  3089. The goal of transport-level address validation is to minimize the chances
  3090. of a successful man-in-the-middle attack against GNUnet peers on the
  3091. transport level. Such an attack would not allow the adversary to decrypt
  3092. the P2P transmissions, but a successful attacker could at least measure
  3093. traffic volumes and latencies (raising the adversaries capabilities by
  3094. those of a global passive adversary in the worst case). The scenarios we
  3095. are concerned about is an attacker, Mallory, giving a @code{HELLO} to
  3096. Alice that claims to be for Bob, but contains Mallory's IP address
  3097. instead of Bobs (for some transport).
  3098. Mallory would then forward the traffic to Bob (by initiating a
  3099. connection to Bob and claiming to be Alice). As a further
  3100. complication, the scheme has to work even if say Alice is behind a NAT
  3101. without traversal support and hence has no address of her own (and thus
  3102. Alice must always initiate the connection to Bob).
  3103. An additional constraint is that @code{HELLO} messages do not contain a
  3104. cryptographic signature since other peers must be able to edit
  3105. (i.e. remove) addresses from the @code{HELLO} at any time (this was
  3106. not true in GNUnet 0.8.x). A basic @strong{assumption} is that each peer
  3107. knows the set of possible network addresses that it @strong{might}
  3108. be reachable under (so for example, the external IP address of the
  3109. NAT plus the LAN address(es) with the respective ports).
  3110. The solution is the following. If Alice wants to validate that a given
  3111. address for Bob is valid (i.e. is actually established @strong{directly}
  3112. with the intended target), she sends a PING message over that connection
  3113. to Bob. Note that in this case, Alice initiated the connection so only
  3114. Alice knows which address was used for sure (Alice may be behind NAT, so
  3115. whatever address Bob sees may not be an address Alice knows she has).
  3116. Bob checks that the address given in the @code{PING} is actually one
  3117. of Bob's addresses (ie: does not belong to Mallory), and if it is,
  3118. sends back a @code{PONG} (with a signature that says that Bob
  3119. owns/uses the address from the @code{PING}).
  3120. Alice checks the signature and is happy if it is valid and the address
  3121. in the @code{PONG} is the address Alice used.
  3122. This is similar to the 0.8.x protocol where the @code{HELLO} contained a
  3123. signature from Bob for each address used by Bob.
  3124. Here, the purpose code for the signature is
  3125. @code{GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN}. After this, Alice will
  3126. remember Bob's address and consider the address valid for a while (12h in
  3127. the current implementation). Note that after this exchange, Alice only
  3128. considers Bob's address to be valid, the connection itself is not
  3129. considered 'established'. In particular, Alice may have many addresses
  3130. for Bob that Alice considers valid.
  3131. The @code{PONG} message is protected with a nonce/challenge against replay
  3132. attacks (@uref{http://en.wikipedia.org/wiki/Replay_attack, replay})
  3133. and uses an expiration time for the signature (but those are almost
  3134. implementation details).
  3135. @cindex NAT library
  3136. @node NAT library
  3137. @section NAT library
  3138. The goal of the GNUnet NAT library is to provide a general-purpose API for
  3139. NAT traversal @strong{without} third-party support. So protocols that
  3140. involve contacting a third peer to help establish a connection between
  3141. two peers are outside of the scope of this API. That does not mean that
  3142. GNUnet doesn't support involving a third peer (we can do this with the
  3143. distance-vector transport or using application-level protocols), it just
  3144. means that the NAT API is not concerned with this possibility. The API is
  3145. written so that it will work for IPv6-NAT in the future as well as
  3146. current IPv4-NAT. Furthermore, the NAT API is always used, even for peers
  3147. that are not behind NAT --- in that case, the mapping provided is simply
  3148. the identity.
  3149. NAT traversal is initiated by calling @code{GNUNET_NAT_register}. Given a
  3150. set of addresses that the peer has locally bound to (TCP or UDP), the NAT
  3151. library will return (via callback) a (possibly longer) list of addresses
  3152. the peer @strong{might} be reachable under. Internally, depending on the
  3153. configuration, the NAT library will try to punch a hole (using UPnP) or
  3154. just "know" that the NAT was manually punched and generate the respective
  3155. external IP address (the one that should be globally visible) based on
  3156. the given information.
  3157. The NAT library also supports ICMP-based NAT traversal. Here, the other
  3158. peer can request connection-reversal by this peer (in this special case,
  3159. the peer is even allowed to configure a port number of zero). If the NAT
  3160. library detects a connection-reversal request, it returns the respective
  3161. target address to the client as well. It should be noted that
  3162. connection-reversal is currently only intended for TCP, so other plugins
  3163. @strong{must} pass @code{NULL} for the reversal callback. Naturally, the
  3164. NAT library also supports requesting connection reversal from a remote
  3165. peer (@code{GNUNET_NAT_run_client}).
  3166. Once initialized, the NAT handle can be used to test if a given address is
  3167. possibly a valid address for this peer (@code{GNUNET_NAT_test_address}).
  3168. This is used for validating our addresses when generating PONGs.
  3169. Finally, the NAT library contains an API to test if our NAT configuration
  3170. is correct. Using @code{GNUNET_NAT_test_start} @strong{before} binding to
  3171. the respective port, the NAT library can be used to test if the
  3172. configuration works. The test function act as a local client, initialize
  3173. the NAT traversal and then contact a @code{gnunet-nat-server} (running by
  3174. default on @code{gnunet.org}) and ask for a connection to be established.
  3175. This way, it is easy to test if the current NAT configuration is valid.
  3176. @node Distance-Vector plugin
  3177. @section Distance-Vector plugin
  3178. The Distance Vector (DV) transport is a transport mechanism that allows
  3179. peers to act as relays for each other, thereby connecting peers that would
  3180. otherwise be unable to connect. This gives a larger connection set to
  3181. applications that may work better with more peers to choose from (for
  3182. example, File Sharing and/or DHT).
  3183. The Distance Vector transport essentially has two functions. The first is
  3184. "gossiping" connection information about more distant peers to directly
  3185. connected peers. The second is taking messages intended for non-directly
  3186. connected peers and encapsulating them in a DV wrapper that contains the
  3187. required information for routing the message through forwarding peers. Via
  3188. gossiping, optimal routes through the known DV neighborhood are discovered
  3189. and utilized and the message encapsulation provides some benefits in
  3190. addition to simply getting the message from the correct source to the
  3191. proper destination.
  3192. The gossiping function of DV provides an up to date routing table of
  3193. peers that are available up to some number of hops. We call this a
  3194. fisheye view of the network (like a fish, nearby objects are known while
  3195. more distant ones unknown). Gossip messages are sent only to directly
  3196. connected peers, but they are sent about other knowns peers within the
  3197. "fisheye distance". Whenever two peers connect, they immediately gossip
  3198. to each other about their appropriate other neighbors. They also gossip
  3199. about the newly connected peer to previously
  3200. connected neighbors. In order to keep the routing tables up to date,
  3201. disconnect notifications are propagated as gossip as well (because
  3202. disconnects may not be sent/received, timeouts are also used remove
  3203. stagnant routing table entries).
  3204. Routing of messages via DV is straightforward. When the DV transport is
  3205. notified of a message destined for a non-direct neighbor, the appropriate
  3206. forwarding peer is selected, and the base message is encapsulated in a DV
  3207. message which contains information about the initial peer and the intended
  3208. recipient. At each forwarding hop, the initial peer is validated (the
  3209. forwarding peer ensures that it has the initial peer in its neighborhood,
  3210. otherwise the message is dropped). Next the base message is
  3211. re-encapsulated in a new DV message for the next hop in the forwarding
  3212. chain (or delivered to the current peer, if it has arrived at the
  3213. destination).
  3214. Assume a three peer network with peers Alice, Bob and Carol. Assume that
  3215. @example
  3216. Alice <-> Bob and Bob <-> Carol
  3217. @end example
  3218. @noindent
  3219. are direct (e.g. over TCP or UDP transports) connections, but that
  3220. Alice cannot directly connect to Carol.
  3221. This may be the case due to NAT or firewall restrictions, or perhaps
  3222. based on one of the peers respective configurations. If the Distance
  3223. Vector transport is enabled on all three peers, it will automatically
  3224. discover (from the gossip protocol) that Alice and Carol can connect via
  3225. Bob and provide a "virtual" Alice <-> Carol connection. Routing between
  3226. Alice and Carol happens as follows; Alice creates a message destined for
  3227. Carol and notifies the DV transport about it. The DV transport at Alice
  3228. looks up Carol in the routing table and finds that the message must be
  3229. sent through Bob for Carol. The message is encapsulated setting Alice as
  3230. the initiator and Carol as the destination and sent to Bob. Bob receives
  3231. the messages, verifies that both Alice and Carol are known to Bob, and
  3232. re-wraps the message in a new DV message for Carol.
  3233. The DV transport at Carol receives this message, unwraps the original
  3234. message, and delivers it to Carol as though it came directly from Alice.
  3235. @cindex SMTP plugin
  3236. @node SMTP plugin
  3237. @section SMTP plugin
  3238. @c TODO: Update!
  3239. This section describes the new SMTP transport plugin for GNUnet as it
  3240. exists in the 0.7.x and 0.8.x branch. SMTP support is currently not
  3241. available in GNUnet 0.9.x. This page also describes the transport layer
  3242. abstraction (as it existed in 0.7.x and 0.8.x) in more detail and gives
  3243. some benchmarking results. The performance results presented are quite
  3244. old and maybe outdated at this point.
  3245. For the readers in the year 2019, you will notice by the mention of
  3246. version 0.7, 0.8, and 0.9 that this section has to be taken with your
  3247. usual grain of salt and be updated eventually.
  3248. @itemize @bullet
  3249. @item Why use SMTP for a peer-to-peer transport?
  3250. @item SMTPHow does it work?
  3251. @item How do I configure my peer?
  3252. @item How do I test if it works?
  3253. @item How fast is it?
  3254. @item Is there any additional documentation?
  3255. @end itemize
  3256. @menu
  3257. * Why use SMTP for a peer-to-peer transport?::
  3258. * How does it work?::
  3259. * How do I configure my peer?::
  3260. * How do I test if it works?::
  3261. * How fast is it?::
  3262. @end menu
  3263. @node Why use SMTP for a peer-to-peer transport?
  3264. @subsection Why use SMTP for a peer-to-peer transport?
  3265. There are many reasons why one would not want to use SMTP:
  3266. @itemize @bullet
  3267. @item SMTP is using more bandwidth than TCP, UDP or HTTP
  3268. @item SMTP has a much higher latency.
  3269. @item SMTP requires significantly more computation (encoding and decoding
  3270. time) for the peers.
  3271. @item SMTP is significantly more complicated to configure.
  3272. @item SMTP may be abused by tricking GNUnet into sending mail to@
  3273. non-participating third parties.
  3274. @end itemize
  3275. So why would anybody want to use SMTP?
  3276. @itemize @bullet
  3277. @item SMTP can be used to contact peers behind NAT boxes (in virtual
  3278. private networks).
  3279. @item SMTP can be used to circumvent policies that limit or prohibit
  3280. peer-to-peer traffic by masking as "legitimate" traffic.
  3281. @item SMTP uses E-mail addresses which are independent of a specific IP,
  3282. which can be useful to address peers that use dynamic IP addresses.
  3283. @item SMTP can be used to initiate a connection (e.g. initial address
  3284. exchange) and peers can then negotiate the use of a more efficient
  3285. protocol (e.g. TCP) for the actual communication.
  3286. @end itemize
  3287. In summary, SMTP can for example be used to send a message to a peer
  3288. behind a NAT box that has a dynamic IP to tell the peer to establish a
  3289. TCP connection to a peer outside of the private network. Even an
  3290. extraordinary overhead for this first message would be irrelevant in this
  3291. type of situation.
  3292. @node How does it work?
  3293. @subsection How does it work?
  3294. When a GNUnet peer needs to send a message to another GNUnet peer that has
  3295. advertised (only) an SMTP transport address, GNUnet base64-encodes the
  3296. message and sends it in an E-mail to the advertised address. The
  3297. advertisement contains a filter which is placed in the E-mail header,
  3298. such that the receiving host can filter the tagged E-mails and forward it
  3299. to the GNUnet peer process. The filter can be specified individually by
  3300. each peer and be changed over time. This makes it impossible to censor
  3301. GNUnet E-mail messages by searching for a generic filter.
  3302. @node How do I configure my peer?
  3303. @subsection How do I configure my peer?
  3304. First, you need to configure @code{procmail} to filter your inbound E-mail
  3305. for GNUnet traffic. The GNUnet messages must be delivered into a pipe, for
  3306. example @code{/tmp/gnunet.smtp}. You also need to define a filter that is
  3307. used by @command{procmail} to detect GNUnet messages. You are free to
  3308. choose whichever filter you like, but you should make sure that it does
  3309. not occur in your other E-mail. In our example, we will use
  3310. @code{X-mailer: GNUnet}. The @code{~/.procmailrc} configuration file then
  3311. looks like this:
  3312. @example
  3313. :0:
  3314. * ^X-mailer: GNUnet
  3315. /tmp/gnunet.smtp
  3316. # where do you want your other e-mail delivered to
  3317. # (default: /var/spool/mail/)
  3318. :0: /var/spool/mail/
  3319. @end example
  3320. After adding this file, first make sure that your regular E-mail still
  3321. works (e.g. by sending an E-mail to yourself). Then edit the GNUnet
  3322. configuration. In the section @code{SMTP} you need to specify your E-mail
  3323. address under @code{EMAIL}, your mail server (for outgoing mail) under
  3324. @code{SERVER}, the filter (X-mailer: GNUnet in the example) under
  3325. @code{FILTER} and the name of the pipe under @code{PIPE}.@ The completed
  3326. section could then look like this:
  3327. @example
  3328. EMAIL = me@@mail.gnu.org MTU = 65000 SERVER = mail.gnu.org:25 FILTER =
  3329. "X-mailer: GNUnet" PIPE = /tmp/gnunet.smtp
  3330. @end example
  3331. Finally, you need to add @code{smtp} to the list of @code{TRANSPORTS} in
  3332. the @code{GNUNETD} section. GNUnet peers will use the E-mail address that
  3333. you specified to contact your peer until the advertisement times out.
  3334. Thus, if you are not sure if everything works properly or if you are not
  3335. planning to be online for a long time, you may want to configure this
  3336. timeout to be short, e.g. just one hour. For this, set
  3337. @code{HELLOEXPIRES} to @code{1} in the @code{GNUNETD} section.
  3338. This should be it, but you may probably want to test it first.
  3339. @node How do I test if it works?
  3340. @subsection How do I test if it works?
  3341. Any transport can be subjected to some rudimentary tests using the
  3342. @code{gnunet-transport-check} tool. The tool sends a message to the local
  3343. node via the transport and checks that a valid message is received. While
  3344. this test does not involve other peers and can not check if firewalls or
  3345. other network obstacles prohibit proper operation, this is a great
  3346. testcase for the SMTP transport since it tests pretty much nearly all of
  3347. the functionality.
  3348. @code{gnunet-transport-check} should only be used without running
  3349. @code{gnunetd} at the same time. By default, @code{gnunet-transport-check}
  3350. tests all transports that are specified in the configuration file. But
  3351. you can specifically test SMTP by giving the option
  3352. @code{--transport=smtp}.
  3353. Note that this test always checks if a transport can receive and send.
  3354. While you can configure most transports to only receive or only send
  3355. messages, this test will only work if you have configured the transport
  3356. to send and receive messages.
  3357. @node How fast is it?
  3358. @subsection How fast is it?
  3359. We have measured the performance of the UDP, TCP and SMTP transport layer
  3360. directly and when used from an application using the GNUnet core.
  3361. Measuring just the transport layer gives the better view of the actual
  3362. overhead of the protocol, whereas evaluating the transport from the
  3363. application puts the overhead into perspective from a practical point of
  3364. view.
  3365. The loopback measurements of the SMTP transport were performed on three
  3366. different machines spanning a range of modern SMTP configurations. We
  3367. used a PIII-800 running RedHat 7.3 with the Purdue Computer Science
  3368. configuration which includes filters for spam. We also used a Xenon 2 GHZ
  3369. with a vanilla RedHat 8.0 sendmail configuration. Furthermore, we used
  3370. qmail on a PIII-1000 running Sorcerer GNU Linux (SGL). The numbers for
  3371. UDP and TCP are provided using the SGL configuration. The qmail benchmark
  3372. uses qmail's internal filtering whereas the sendmail benchmarks relies on
  3373. procmail to filter and deliver the mail. We used the transport layer to
  3374. send a message of b bytes (excluding transport protocol headers) directly
  3375. to the local machine. This way, network latency and packet loss on the
  3376. wire have no impact on the timings. n messages were sent sequentially over
  3377. the transport layer, sending message i+1 after the i-th message was
  3378. received. All messages were sent over the same connection and the time to
  3379. establish the connection was not taken into account since this overhead is
  3380. minuscule in practice --- as long as a connection is used for a
  3381. significant number of messages.
  3382. @multitable @columnfractions .20 .15 .15 .15 .15 .15
  3383. @headitem Transport @tab UDP @tab TCP @tab SMTP (Purdue sendmail)
  3384. @tab SMTP (RH 8.0) @tab SMTP (SGL qmail)
  3385. @item 11 bytes @tab 31 ms @tab 55 ms @tab 781 s @tab 77 s @tab 24 s
  3386. @item 407 bytes @tab 37 ms @tab 62 ms @tab 789 s @tab 78 s @tab 25 s
  3387. @item 1,221 bytes @tab 46 ms @tab 73 ms @tab 804 s @tab 78 s @tab 25 s
  3388. @end multitable
  3389. The benchmarks show that UDP and TCP are, as expected, both significantly
  3390. faster compared with any of the SMTP services. Among the SMTP
  3391. implementations, there can be significant differences depending on the
  3392. SMTP configuration. Filtering with an external tool like procmail that
  3393. needs to re-parse its configuration for each mail can be very expensive.
  3394. Applying spam filters can also significantly impact the performance of
  3395. the underlying SMTP implementation. The microbenchmark shows that SMTP
  3396. can be a viable solution for initiating peer-to-peer sessions: a couple of
  3397. seconds to connect to a peer are probably not even going to be noticed by
  3398. users. The next benchmark measures the possible throughput for a
  3399. transport. Throughput can be measured by sending multiple messages in
  3400. parallel and measuring packet loss. Note that not only UDP but also the
  3401. TCP transport can actually loose messages since the TCP implementation
  3402. drops messages if the @code{write} to the socket would block. While the
  3403. SMTP protocol never drops messages itself, it is often so
  3404. slow that only a fraction of the messages can be sent and received in the
  3405. given time-bounds. For this benchmark we report the message loss after
  3406. allowing t time for sending m messages. If messages were not sent (or
  3407. received) after an overall timeout of t, they were considered lost. The
  3408. benchmark was performed using two Xeon 2 GHZ machines running RedHat 8.0
  3409. with sendmail. The machines were connected with a direct 100 MBit Ethernet
  3410. connection.@ Figures udp1200, tcp1200 and smtp-MTUs show that the
  3411. throughput for messages of size 1,200 octets is 2,343 kbps, 3,310 kbps
  3412. and 6 kbps for UDP, TCP and SMTP respectively. The high per-message
  3413. overhead of SMTP can be improved by increasing the MTU, for example, an
  3414. MTU of 12,000 octets improves the throughput to 13 kbps as figure
  3415. smtp-MTUs shows. Our research paper) has some more details on the
  3416. benchmarking results.
  3417. @cindex Bluetooth plugin
  3418. @node Bluetooth plugin
  3419. @section Bluetooth plugin
  3420. This page describes the new Bluetooth transport plugin for GNUnet. The
  3421. plugin is still in the testing stage so don't expect it to work
  3422. perfectly. If you have any questions or problems just post them here or
  3423. ask on the IRC channel.
  3424. @itemize @bullet
  3425. @item What do I need to use the Bluetooth plugin transport?
  3426. @item BluetoothHow does it work?
  3427. @item What possible errors should I be aware of?
  3428. @item How do I configure my peer?
  3429. @item How can I test it?
  3430. @end itemize
  3431. @menu
  3432. * What do I need to use the Bluetooth plugin transport?::
  3433. * How does it work2?::
  3434. * What possible errors should I be aware of?::
  3435. * How do I configure my peer2?::
  3436. * How can I test it?::
  3437. * The implementation of the Bluetooth transport plugin::
  3438. @end menu
  3439. @node What do I need to use the Bluetooth plugin transport?
  3440. @subsection What do I need to use the Bluetooth plugin transport?
  3441. If you are a GNU/Linux user and you want to use the Bluetooth
  3442. transport plugin you should install the
  3443. @command{BlueZ development libraries} (if they aren't already
  3444. installed).
  3445. For instructions about how to install the libraries you should
  3446. check out the BlueZ site
  3447. (@uref{http://www.bluez.org/, http://www.bluez.org}). If you don't know if
  3448. you have the necessary libraries, don't worry, just run the GNUnet
  3449. configure script and you will be able to see a notification at the end
  3450. which will warn you if you don't have the necessary libraries.
  3451. If you are a Windows user you should have installed the
  3452. @emph{MinGW}/@emph{MSys2} with the latest updates (especially the
  3453. @emph{ws2bth} header). If this is your first build of GNUnet on Windows
  3454. you should check out the SBuild repository. It will semi-automatically
  3455. assembles a @emph{MinGW}/@emph{MSys2} installation with a lot of extra
  3456. packages which are needed for the GNUnet build. So this will ease your
  3457. work!@ Finally you just have to be sure that you have the correct drivers
  3458. for your Bluetooth device installed and that your device is on and in a
  3459. discoverable mode. The Windows Bluetooth Stack supports only the RFCOMM
  3460. protocol so we cannot turn on your device programatically!
  3461. @c FIXME: Change to unique title
  3462. @node How does it work2?
  3463. @subsection How does it work2?
  3464. The Bluetooth transport plugin uses virtually the same code as the WLAN
  3465. plugin and only the helper binary is different. The helper takes a single
  3466. argument, which represents the interface name and is specified in the
  3467. configuration file. Here are the basic steps that are followed by the
  3468. helper binary used on GNU/Linux:
  3469. @itemize @bullet
  3470. @item it verifies if the name corresponds to a Bluetooth interface name
  3471. @item it verifies if the interface is up (if it is not, it tries to bring
  3472. it up)
  3473. @item it tries to enable the page and inquiry scan in order to make the
  3474. device discoverable and to accept incoming connection requests
  3475. @emph{The above operations require root access so you should start the
  3476. transport plugin with root privileges.}
  3477. @item it finds an available port number and registers a SDP service which
  3478. will be used to find out on which port number is the server listening on
  3479. and switch the socket in listening mode
  3480. @item it sends a HELLO message with its address
  3481. @item finally it forwards traffic from the reading sockets to the STDOUT
  3482. and from the STDIN to the writing socket
  3483. @end itemize
  3484. Once in a while the device will make an inquiry scan to discover the
  3485. nearby devices and it will send them randomly HELLO messages for peer
  3486. discovery.
  3487. @node What possible errors should I be aware of?
  3488. @subsection What possible errors should I be aware of?
  3489. @emph{This section is dedicated for GNU/Linux users}
  3490. Well there are many ways in which things could go wrong but I will try to
  3491. present some tools that you could use to debug and some scenarios.
  3492. @itemize @bullet
  3493. @item @code{bluetoothd -n -d} : use this command to enable logging in the
  3494. foreground and to print the logging messages
  3495. @item @code{hciconfig}: can be used to configure the Bluetooth devices.
  3496. If you run it without any arguments it will print information about the
  3497. state of the interfaces. So if you receive an error that the device
  3498. couldn't be brought up you should try to bring it manually and to see if
  3499. it works (use @code{hciconfig -a hciX up}). If you can't and the
  3500. Bluetooth address has the form 00:00:00:00:00:00 it means that there is
  3501. something wrong with the D-Bus daemon or with the Bluetooth daemon. Use
  3502. @code{bluetoothd} tool to see the logs
  3503. @item @code{sdptool} can be used to control and interrogate SDP servers.
  3504. If you encounter problems regarding the SDP server (like the SDP server is
  3505. down) you should check out if the D-Bus daemon is running correctly and to
  3506. see if the Bluetooth daemon started correctly(use @code{bluetoothd} tool).
  3507. Also, sometimes the SDP service could work but somehow the device couldn't
  3508. register its service. Use @code{sdptool browse [dev-address]} to see if
  3509. the service is registered. There should be a service with the name of the
  3510. interface and GNUnet as provider.
  3511. @item @code{hcitool} : another useful tool which can be used to configure
  3512. the device and to send some particular commands to it.
  3513. @item @code{hcidump} : could be used for low level debugging
  3514. @end itemize
  3515. @c FIXME: A more unique name
  3516. @node How do I configure my peer2?
  3517. @subsection How do I configure my peer2?
  3518. On GNU/Linux, you just have to be sure that the interface name
  3519. corresponds to the one that you want to use.
  3520. Use the @code{hciconfig} tool to check that.
  3521. By default it is set to hci0 but you can change it.
  3522. A basic configuration looks like this:
  3523. @example
  3524. [transport-bluetooth]
  3525. # Name of the interface (typically hciX)
  3526. INTERFACE = hci0
  3527. # Real hardware, no testing
  3528. TESTMODE = 0 TESTING_IGNORE_KEYS = ACCEPT_FROM;
  3529. @end example
  3530. In order to use the Bluetooth transport plugin when the transport service
  3531. is started, you must add the plugin name to the default transport service
  3532. plugins list. For example:
  3533. @example
  3534. [transport] ... PLUGINS = dns bluetooth ...
  3535. @end example
  3536. If you want to use only the Bluetooth plugin set
  3537. @emph{PLUGINS = bluetooth}
  3538. On Windows, you cannot specify which device to use. The only thing that
  3539. you should do is to add @emph{bluetooth} on the plugins list of the
  3540. transport service.
  3541. @node How can I test it?
  3542. @subsection How can I test it?
  3543. If you have two Bluetooth devices on the same machine and you are using
  3544. GNU/Linux you must:
  3545. @itemize @bullet
  3546. @item create two different file configuration (one which will use the
  3547. first interface (@emph{hci0}) and the other which will use the second
  3548. interface (@emph{hci1})). Let's name them @emph{peer1.conf} and
  3549. @emph{peer2.conf}.
  3550. @item run @emph{gnunet-peerinfo -c peerX.conf -s} in order to generate the
  3551. peers private keys. The @strong{X} must be replace with 1 or 2.
  3552. @item run @emph{gnunet-arm -c peerX.conf -s -i=transport} in order to
  3553. start the transport service. (Make sure that you have "bluetooth" on the
  3554. transport plugins list if the Bluetooth transport service doesn't start.)
  3555. @item run @emph{gnunet-peerinfo -c peer1.conf -s} to get the first peer's
  3556. ID. If you already know your peer ID (you saved it from the first
  3557. command), this can be skipped.
  3558. @item run @emph{gnunet-transport -c peer2.conf -p=PEER1_ID -s} to start
  3559. sending data for benchmarking to the other peer.
  3560. @end itemize
  3561. This scenario will try to connect the second peer to the first one and
  3562. then start sending data for benchmarking.
  3563. On Windows you cannot test the plugin functionality using two Bluetooth
  3564. devices from the same machine because after you install the drivers there
  3565. will occur some conflicts between the Bluetooth stacks. (At least that is
  3566. what happened on my machine : I wasn't able to use the Bluesoleil stack and
  3567. the WINDCOMM one in the same time).
  3568. If you have two different machines and your configuration files are good
  3569. you can use the same scenario presented on the beginning of this section.
  3570. Another way to test the plugin functionality is to create your own
  3571. application which will use the GNUnet framework with the Bluetooth
  3572. transport service.
  3573. @node The implementation of the Bluetooth transport plugin
  3574. @subsection The implementation of the Bluetooth transport plugin
  3575. This page describes the implementation of the Bluetooth transport plugin.
  3576. First I want to remind you that the Bluetooth transport plugin uses
  3577. virtually the same code as the WLAN plugin and only the helper binary is
  3578. different. Also the scope of the helper binary from the Bluetooth
  3579. transport plugin is the same as the one used for the WLAN transport
  3580. plugin: it accesses the interface and then it forwards traffic in both
  3581. directions between the Bluetooth interface and stdin/stdout of the
  3582. process involved.
  3583. The Bluetooth plugin transport could be used both on GNU/Linux and Windows
  3584. platforms.
  3585. @itemize @bullet
  3586. @item Linux functionality
  3587. @item Windows functionality
  3588. @item Pending Features
  3589. @end itemize
  3590. @menu
  3591. * Linux functionality::
  3592. * THE INITIALIZATION::
  3593. * THE LOOP::
  3594. * Details about the broadcast implementation::
  3595. * Windows functionality::
  3596. * Pending features::
  3597. @end menu
  3598. @node Linux functionality
  3599. @subsubsection Linux functionality
  3600. In order to implement the plugin functionality on GNU/Linux I
  3601. used the BlueZ stack.
  3602. For the communication with the other devices I used the RFCOMM
  3603. protocol. Also I used the HCI protocol to gain some control over the
  3604. device. The helper binary takes a single argument (the name of the
  3605. Bluetooth interface) and is separated in two stages:
  3606. @c %** 'THE INITIALIZATION' should be in bigger letters or stand out, not
  3607. @c %** starting a new section?
  3608. @node THE INITIALIZATION
  3609. @subsubsection THE INITIALIZATION
  3610. @itemize @bullet
  3611. @item first, it checks if we have root privileges
  3612. (@emph{Remember that we need to have root privileges in order to be able
  3613. to bring the interface up if it is down or to change its state.}).
  3614. @item second, it verifies if the interface with the given name exists.
  3615. @strong{If the interface with that name exists and it is a Bluetooth
  3616. interface:}
  3617. @item it creates a RFCOMM socket which will be used for listening and call
  3618. the @emph{open_device} method
  3619. On the @emph{open_device} method:
  3620. @itemize @bullet
  3621. @item creates a HCI socket used to send control events to the the device
  3622. @item searches for the device ID using the interface name
  3623. @item saves the device MAC address
  3624. @item checks if the interface is down and tries to bring it UP
  3625. @item checks if the interface is in discoverable mode and tries to make it
  3626. discoverable
  3627. @item closes the HCI socket and binds the RFCOMM one
  3628. @item switches the RFCOMM socket in listening mode
  3629. @item registers the SDP service (the service will be used by the other
  3630. devices to get the port on which this device is listening on)
  3631. @end itemize
  3632. @item drops the root privileges
  3633. @strong{If the interface is not a Bluetooth interface the helper exits
  3634. with a suitable error}
  3635. @end itemize
  3636. @c %** Same as for @node entry above
  3637. @node THE LOOP
  3638. @subsubsection THE LOOP
  3639. The helper binary uses a list where it saves all the connected neighbour
  3640. devices (@emph{neighbours.devices}) and two buffers (@emph{write_pout} and
  3641. @emph{write_std}). The first message which is send is a control message
  3642. with the device's MAC address in order to announce the peer presence to
  3643. the neighbours. Here are a short description of what happens in the main
  3644. loop:
  3645. @itemize @bullet
  3646. @item Every time when it receives something from the STDIN it processes
  3647. the data and saves the message in the first buffer (@emph{write_pout}).
  3648. When it has something in the buffer, it gets the destination address from
  3649. the buffer, searches the destination address in the list (if there is no
  3650. connection with that device, it creates a new one and saves it to the
  3651. list) and sends the message.
  3652. @item Every time when it receives something on the listening socket it
  3653. accepts the connection and saves the socket on a list with the reading
  3654. sockets. @item Every time when it receives something from a reading
  3655. socket it parses the message, verifies the CRC and saves it in the
  3656. @emph{write_std} buffer in order to be sent later to the STDOUT.
  3657. @end itemize
  3658. So in the main loop we use the select function to wait until one of the
  3659. file descriptor saved in one of the two file descriptors sets used is
  3660. ready to use. The first set (@emph{rfds}) represents the reading set and
  3661. it could contain the list with the reading sockets, the STDIN file
  3662. descriptor or the listening socket. The second set (@emph{wfds}) is the
  3663. writing set and it could contain the sending socket or the STDOUT file
  3664. descriptor. After the select function returns, we check which file
  3665. descriptor is ready to use and we do what is supposed to do on that kind
  3666. of event. @emph{For example:} if it is the listening socket then we
  3667. accept a new connection and save the socket in the reading list; if it is
  3668. the STDOUT file descriptor, then we write to STDOUT the message from the
  3669. @emph{write_std} buffer.
  3670. To find out on which port a device is listening on we connect to the local
  3671. SDP server and search the registered service for that device.
  3672. @emph{You should be aware of the fact that if the device fails to connect
  3673. to another one when trying to send a message it will attempt one more
  3674. time. If it fails again, then it skips the message.}
  3675. @emph{Also you should know that the transport Bluetooth plugin has
  3676. support for @strong{broadcast messages}.}
  3677. @node Details about the broadcast implementation
  3678. @subsubsection Details about the broadcast implementation
  3679. First I want to point out that the broadcast functionality for the CONTROL
  3680. messages is not implemented in a conventional way. Since the inquiry scan
  3681. time is too big and it will take some time to send a message to all the
  3682. discoverable devices I decided to tackle the problem in a different way.
  3683. Here is how I did it:
  3684. @itemize @bullet
  3685. @item If it is the first time when I have to broadcast a message I make an
  3686. inquiry scan and save all the devices' addresses to a vector.
  3687. @item After the inquiry scan ends I take the first address from the list
  3688. and I try to connect to it. If it fails, I try to connect to the next one.
  3689. If it succeeds, I save the socket to a list and send the message to the
  3690. device.
  3691. @item When I have to broadcast another message, first I search on the list
  3692. for a new device which I'm not connected to. If there is no new device on
  3693. the list I go to the beginning of the list and send the message to the
  3694. old devices. After 5 cycles I make a new inquiry scan to check out if
  3695. there are new discoverable devices and save them to the list. If there
  3696. are no new discoverable devices I reset the cycling counter and go again
  3697. through the old list and send messages to the devices saved in it.
  3698. @end itemize
  3699. @strong{Therefore}:
  3700. @itemize @bullet
  3701. @item every time when I have a broadcast message I look up on the list
  3702. for a new device and send the message to it
  3703. @item if I reached the end of the list for 5 times and I'm connected to
  3704. all the devices from the list I make a new inquiry scan.
  3705. @emph{The number of the list's cycles after an inquiry scan could be
  3706. increased by redefining the MAX_LOOPS variable}
  3707. @item when there are no new devices I send messages to the old ones.
  3708. @end itemize
  3709. Doing so, the broadcast control messages will reach the devices but with
  3710. delay.
  3711. @emph{NOTICE:} When I have to send a message to a certain device first I
  3712. check on the broadcast list to see if we are connected to that device. If
  3713. not we try to connect to it and in case of success we save the address and
  3714. the socket on the list. If we are already connected to that device we
  3715. simply use the socket.
  3716. @node Windows functionality
  3717. @subsubsection Windows functionality
  3718. For Windows I decided to use the Microsoft Bluetooth stack which has the
  3719. advantage of coming standard from Windows XP SP2. The main disadvantage is
  3720. that it only supports the RFCOMM protocol so we will not be able to have
  3721. a low level control over the Bluetooth device. Therefore it is the user
  3722. responsibility to check if the device is up and in the discoverable mode.
  3723. Also there are no tools which could be used for debugging in order to read
  3724. the data coming from and going to a Bluetooth device, which obviously
  3725. hindered my work. Another thing that slowed down the implementation of the
  3726. plugin (besides that I wasn't too accommodated with the win32 API) was that
  3727. there were some bugs on MinGW regarding the Bluetooth. Now they are solved
  3728. but you should keep in mind that you should have the latest updates
  3729. (especially the @emph{ws2bth} header).
  3730. Besides the fact that it uses the Windows Sockets, the Windows
  3731. implementation follows the same principles as the GNU/Linux one:
  3732. @itemize @bullet
  3733. @item It has a initalization part where it initializes the
  3734. Windows Sockets, creates a RFCOMM socket which will be binded and switched
  3735. to the listening mode and registers a SDP service. In the Microsoft
  3736. Bluetooth API there are two ways to work with the SDP:
  3737. @itemize @bullet
  3738. @item an easy way which works with very simple service records
  3739. @item a hard way which is useful when you need to update or to delete the
  3740. record
  3741. @end itemize
  3742. @end itemize
  3743. Since I only needed the SDP service to find out on which port the device
  3744. is listening on and that did not change, I decided to use the easy way.
  3745. In order to register the service I used the @emph{WSASetService} function
  3746. and I generated the @emph{Universally Unique Identifier} with the
  3747. @emph{guidgen.exe} Windows's tool.
  3748. In the loop section the only difference from the GNU/Linux implementation
  3749. is that I used the @code{GNUNET_NETWORK} library for
  3750. functions like @emph{accept}, @emph{bind}, @emph{connect} or
  3751. @emph{select}. I decided to use the
  3752. @code{GNUNET_NETWORK} library because I also needed to interact
  3753. with the STDIN and STDOUT handles and on Windows
  3754. the select function is only defined for sockets,
  3755. and it will not work for arbitrary file handles.
  3756. Another difference between GNU/Linux and Windows implementation is that in
  3757. GNU/Linux, the Bluetooth address is represented in 48 bits
  3758. while in Windows is represented in 64 bits.
  3759. Therefore I had to do some changes on @emph{plugin_transport_wlan} header.
  3760. Also, currently on Windows the Bluetooth plugin doesn't have support for
  3761. broadcast messages. When it receives a broadcast message it will skip it.
  3762. @node Pending features
  3763. @subsubsection Pending features
  3764. @itemize @bullet
  3765. @item Implement the broadcast functionality on Windows @emph{(currently
  3766. working on)}
  3767. @item Implement a testcase for the helper :@ @emph{The testcase
  3768. consists of a program which emulates the plugin and uses the helper. It
  3769. will simulate connections, disconnections and data transfers.}
  3770. @end itemize
  3771. If you have a new idea about a feature of the plugin or suggestions about
  3772. how I could improve the implementation you are welcome to comment or to
  3773. contact me.
  3774. @node WLAN plugin
  3775. @section WLAN plugin
  3776. This section documents how the wlan transport plugin works. Parts which
  3777. are not implemented yet or could be better implemented are described at
  3778. the end.
  3779. @cindex ATS Subsystem
  3780. @node ATS Subsystem
  3781. @section ATS Subsystem
  3782. ATS stands for "automatic transport selection", and the function of ATS in
  3783. GNUnet is to decide on which address (and thus transport plugin) should
  3784. be used for two peers to communicate, and what bandwidth limits should be
  3785. imposed on such an individual connection. To help ATS make an informed
  3786. decision, higher-level services inform the ATS service about their
  3787. requirements and the quality of the service rendered. The ATS service
  3788. also interacts with the transport service to be appraised of working
  3789. addresses and to communicate its resource allocation decisions. Finally,
  3790. the ATS service's operation can be observed using a monitoring API.
  3791. The main logic of the ATS service only collects the available addresses,
  3792. their performance characteristics and the applications requirements, but
  3793. does not make the actual allocation decision. This last critical step is
  3794. left to an ATS plugin, as we have implemented (currently three) different
  3795. allocation strategies which differ significantly in their performance and
  3796. maturity, and it is still unclear if any particular plugin is generally
  3797. superior.
  3798. @cindex CORE Subsystem
  3799. @node CORE Subsystem
  3800. @section CORE Subsystem
  3801. The CORE subsystem in GNUnet is responsible for securing link-layer
  3802. communications between nodes in the GNUnet overlay network. CORE builds
  3803. on the TRANSPORT subsystem which provides for the actual, insecure,
  3804. unreliable link-layer communication (for example, via UDP or WLAN), and
  3805. then adds fundamental security to the connections:
  3806. @itemize @bullet
  3807. @item confidentiality with so-called perfect forward secrecy; we use
  3808. ECDHE
  3809. (@uref{http://en.wikipedia.org/wiki/Elliptic_curve_Diffie%E2%80%93Hellman, Elliptic-curve Diffie---Hellman})
  3810. powered by Curve25519
  3811. (@uref{http://cr.yp.to/ecdh.html, Curve25519}) for the key
  3812. exchange and then use symmetric encryption, encrypting with both AES-256
  3813. (@uref{http://en.wikipedia.org/wiki/Rijndael, AES-256}) and
  3814. Twofish (@uref{http://en.wikipedia.org/wiki/Twofish, Twofish})
  3815. @item @uref{http://en.wikipedia.org/wiki/Authentication, authentication}
  3816. is achieved by signing the ephemeral keys using Ed25519
  3817. (@uref{http://ed25519.cr.yp.to/, Ed25519}), a deterministic
  3818. variant of ECDSA
  3819. (@uref{http://en.wikipedia.org/wiki/ECDSA, ECDSA})
  3820. @item integrity protection (using SHA-512
  3821. (@uref{http://en.wikipedia.org/wiki/SHA-2, SHA-512}) to do
  3822. encrypt-then-MAC
  3823. (@uref{http://en.wikipedia.org/wiki/Authenticated_encryption, encrypt-then-MAC}))
  3824. @item Replay
  3825. (@uref{http://en.wikipedia.org/wiki/Replay_attack, replay})
  3826. protection (using nonces, timestamps, challenge-response,
  3827. message counters and ephemeral keys)
  3828. @item liveness (keep-alive messages, timeout)
  3829. @end itemize
  3830. @menu
  3831. * Limitations::
  3832. * When is a peer "connected"?::
  3833. * libgnunetcore::
  3834. * The CORE Client-Service Protocol::
  3835. * The CORE Peer-to-Peer Protocol::
  3836. @end menu
  3837. @cindex core subsystem limitations
  3838. @node Limitations
  3839. @subsection Limitations
  3840. CORE does not perform
  3841. @uref{http://en.wikipedia.org/wiki/Routing, routing}; using CORE it is
  3842. only possible to communicate with peers that happen to already be
  3843. "directly" connected with each other. CORE also does not have an
  3844. API to allow applications to establish such "direct" connections --- for
  3845. this, applications can ask TRANSPORT, but TRANSPORT might not be able to
  3846. establish a "direct" connection. The TOPOLOGY subsystem is responsible for
  3847. trying to keep a few "direct" connections open at all times. Applications
  3848. that need to talk to particular peers should use the CADET subsystem, as
  3849. it can establish arbitrary "indirect" connections.
  3850. Because CORE does not perform routing, CORE must only be used directly by
  3851. applications that either perform their own routing logic (such as
  3852. anonymous file-sharing) or that do not require routing, for example
  3853. because they are based on flooding the network. CORE communication is
  3854. unreliable and delivery is possibly out-of-order. Applications that
  3855. require reliable communication should use the CADET service. Each
  3856. application can only queue one message per target peer with the CORE
  3857. service at any time; messages cannot be larger than approximately
  3858. 63 kilobytes. If messages are small, CORE may group multiple messages
  3859. (possibly from different applications) prior to encryption. If permitted
  3860. by the application (using the @uref{http://baus.net/on-tcp_cork/, cork}
  3861. option), CORE may delay transmissions to facilitate grouping of multiple
  3862. small messages. If cork is not enabled, CORE will transmit the message as
  3863. soon as TRANSPORT allows it (TRANSPORT is responsible for limiting
  3864. bandwidth and congestion control). CORE does not allow flow control;
  3865. applications are expected to process messages at line-speed. If flow
  3866. control is needed, applications should use the CADET service.
  3867. @cindex when is a peer connected
  3868. @node When is a peer "connected"?
  3869. @subsection When is a peer "connected"?
  3870. In addition to the security features mentioned above, CORE also provides
  3871. one additional key feature to applications using it, and that is a
  3872. limited form of protocol-compatibility checking. CORE distinguishes
  3873. between TRANSPORT-level connections (which enable communication with other
  3874. peers) and application-level connections. Applications using the CORE API
  3875. will (typically) learn about application-level connections from CORE, and
  3876. not about TRANSPORT-level connections. When a typical application uses
  3877. CORE, it will specify a set of message types
  3878. (from @code{gnunet_protocols.h}) that it understands. CORE will then
  3879. notify the application about connections it has with other peers if and
  3880. only if those applications registered an intersecting set of message
  3881. types with their CORE service. Thus, it is quite possible that CORE only
  3882. exposes a subset of the established direct connections to a particular
  3883. application --- and different applications running above CORE might see
  3884. different sets of connections at the same time.
  3885. A special case are applications that do not register a handler for any
  3886. message type.
  3887. CORE assumes that these applications merely want to monitor connections
  3888. (or "all" messages via other callbacks) and will notify those applications
  3889. about all connections. This is used, for example, by the
  3890. @code{gnunet-core} command-line tool to display the active connections.
  3891. Note that it is also possible that the TRANSPORT service has more active
  3892. connections than the CORE service, as the CORE service first has to
  3893. perform a key exchange with connecting peers before exchanging information
  3894. about supported message types and notifying applications about the new
  3895. connection.
  3896. @cindex libgnunetcore
  3897. @node libgnunetcore
  3898. @subsection libgnunetcore
  3899. The CORE API (defined in @file{gnunet_core_service.h}) is the basic
  3900. messaging API used by P2P applications built using GNUnet. It provides
  3901. applications the ability to send and receive encrypted messages to the
  3902. peer's "directly" connected neighbours.
  3903. As CORE connections are generally "direct" connections,@ applications must
  3904. not assume that they can connect to arbitrary peers this way, as "direct"
  3905. connections may not always be possible. Applications using CORE are
  3906. notified about which peers are connected. Creating new "direct"
  3907. connections must be done using the TRANSPORT API.
  3908. The CORE API provides unreliable, out-of-order delivery. While the
  3909. implementation tries to ensure timely, in-order delivery, both message
  3910. losses and reordering are not detected and must be tolerated by the
  3911. application. Most important, the core will NOT perform retransmission if
  3912. messages could not be delivered.
  3913. Note that CORE allows applications to queue one message per connected
  3914. peer. The rate at which each connection operates is influenced by the
  3915. preferences expressed by local application as well as restrictions
  3916. imposed by the other peer. Local applications can express their
  3917. preferences for particular connections using the "performance" API of the
  3918. ATS service.
  3919. Applications that require more sophisticated transmission capabilities
  3920. such as TCP-like behavior, or if you intend to send messages to arbitrary
  3921. remote peers, should use the CADET API.
  3922. The typical use of the CORE API is to connect to the CORE service using
  3923. @code{GNUNET_CORE_connect}, process events from the CORE service (such as
  3924. peers connecting, peers disconnecting and incoming messages) and send
  3925. messages to connected peers using
  3926. @code{GNUNET_CORE_notify_transmit_ready}. Note that applications must
  3927. cancel pending transmission requests if they receive a disconnect event
  3928. for a peer that had a transmission pending; furthermore, queuing more
  3929. than one transmission request per peer per application using the
  3930. service is not permitted.
  3931. The CORE API also allows applications to monitor all communications of the
  3932. peer prior to encryption (for outgoing messages) or after decryption (for
  3933. incoming messages). This can be useful for debugging, diagnostics or to
  3934. establish the presence of cover traffic (for anonymity). As monitoring
  3935. applications are often not interested in the payload, the monitoring
  3936. callbacks can be configured to only provide the message headers (including
  3937. the message type and size) instead of copying the full data stream to the
  3938. monitoring client.
  3939. The init callback of the @code{GNUNET_CORE_connect} function is called
  3940. with the hash of the public key of the peer. This public key is used to
  3941. identify the peer globally in the GNUnet network. Applications are
  3942. encouraged to check that the provided hash matches the hash that they are
  3943. using (as theoretically the application may be using a different
  3944. configuration file with a different private key, which would result in
  3945. hard to find bugs).
  3946. As with most service APIs, the CORE API isolates applications from crashes
  3947. of the CORE service. If the CORE service crashes, the application will see
  3948. disconnect events for all existing connections. Once the connections are
  3949. re-established, the applications will be receive matching connect events.
  3950. @cindex core clinet-service protocol
  3951. @node The CORE Client-Service Protocol
  3952. @subsection The CORE Client-Service Protocol
  3953. This section describes the protocol between an application using the CORE
  3954. service (the client) and the CORE service process itself.
  3955. @menu
  3956. * Setup2::
  3957. * Notifications::
  3958. * Sending::
  3959. @end menu
  3960. @node Setup2
  3961. @subsubsection Setup2
  3962. When a client connects to the CORE service, it first sends a
  3963. @code{InitMessage} which specifies options for the connection and a set of
  3964. message type values which are supported by the application. The options
  3965. bitmask specifies which events the client would like to be notified about.
  3966. The options include:
  3967. @table @asis
  3968. @item GNUNET_CORE_OPTION_NOTHING No notifications
  3969. @item GNUNET_CORE_OPTION_STATUS_CHANGE Peers connecting and disconnecting
  3970. @item GNUNET_CORE_OPTION_FULL_INBOUND All inbound messages (after
  3971. decryption) with full payload
  3972. @item GNUNET_CORE_OPTION_HDR_INBOUND Just the @code{MessageHeader}
  3973. of all inbound messages
  3974. @item GNUNET_CORE_OPTION_FULL_OUTBOUND All outbound
  3975. messages (prior to encryption) with full payload
  3976. @item GNUNET_CORE_OPTION_HDR_OUTBOUND Just the @code{MessageHeader} of all
  3977. outbound messages
  3978. @end table
  3979. Typical applications will only monitor for connection status changes.
  3980. The CORE service responds to the @code{InitMessage} with an
  3981. @code{InitReplyMessage} which contains the peer's identity. Afterwards,
  3982. both CORE and the client can send messages.
  3983. @node Notifications
  3984. @subsubsection Notifications
  3985. The CORE will send @code{ConnectNotifyMessage}s and
  3986. @code{DisconnectNotifyMessage}s whenever peers connect or disconnect from
  3987. the CORE (assuming their type maps overlap with the message types
  3988. registered by the client). When the CORE receives a message that matches
  3989. the set of message types specified during the @code{InitMessage} (or if
  3990. monitoring is enabled in for inbound messages in the options), it sends a
  3991. @code{NotifyTrafficMessage} with the peer identity of the sender and the
  3992. decrypted payload. The same message format (except with
  3993. @code{GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND} for the message type) is
  3994. used to notify clients monitoring outbound messages; here, the peer
  3995. identity given is that of the receiver.
  3996. @node Sending
  3997. @subsubsection Sending
  3998. When a client wants to transmit a message, it first requests a
  3999. transmission slot by sending a @code{SendMessageRequest} which specifies
  4000. the priority, deadline and size of the message. Note that these values
  4001. may be ignored by CORE. When CORE is ready for the message, it answers
  4002. with a @code{SendMessageReady} response. The client can then transmit the
  4003. payload with a @code{SendMessage} message. Note that the actual message
  4004. size in the @code{SendMessage} is allowed to be smaller than the size in
  4005. the original request. A client may at any time send a fresh
  4006. @code{SendMessageRequest}, which then superceeds the previous
  4007. @code{SendMessageRequest}, which is then no longer valid. The client can
  4008. tell which @code{SendMessageRequest} the CORE service's
  4009. @code{SendMessageReady} message is for as all of these messages contain a
  4010. "unique" request ID (based on a counter incremented by the client
  4011. for each request).
  4012. @cindex CORE Peer-to-Peer Protocol
  4013. @node The CORE Peer-to-Peer Protocol
  4014. @subsection The CORE Peer-to-Peer Protocol
  4015. @menu
  4016. * Creating the EphemeralKeyMessage::
  4017. * Establishing a connection::
  4018. * Encryption and Decryption::
  4019. * Type maps::
  4020. @end menu
  4021. @cindex EphemeralKeyMessage creation
  4022. @node Creating the EphemeralKeyMessage
  4023. @subsubsection Creating the EphemeralKeyMessage
  4024. When the CORE service starts, each peer creates a fresh ephemeral (ECC)
  4025. public-private key pair and signs the corresponding
  4026. @code{EphemeralKeyMessage} with its long-term key (which we usually call
  4027. the peer's identity; the hash of the public long term key is what results
  4028. in a @code{struct GNUNET_PeerIdentity} in all GNUnet APIs. The ephemeral
  4029. key is ONLY used for an ECDHE
  4030. (@uref{http://en.wikipedia.org/wiki/Elliptic_curve_Diffie%E2%80%93Hellman, Elliptic-curve Diffie---Hellman})
  4031. exchange by the CORE service to establish symmetric session keys. A peer
  4032. will use the same @code{EphemeralKeyMessage} for all peers for
  4033. @code{REKEY_FREQUENCY}, which is usually 12 hours. After that time, it
  4034. will create a fresh ephemeral key (forgetting the old one) and broadcast
  4035. the new @code{EphemeralKeyMessage} to all connected peers, resulting in
  4036. fresh symmetric session keys. Note that peers independently decide on
  4037. when to discard ephemeral keys; it is not a protocol violation to discard
  4038. keys more often. Ephemeral keys are also never stored to disk; restarting
  4039. a peer will thus always create a fresh ephemeral key. The use of ephemeral
  4040. keys is what provides @uref{http://en.wikipedia.org/wiki/Forward_secrecy, forward secrecy}.
  4041. Just before transmission, the @code{EphemeralKeyMessage} is patched to
  4042. reflect the current sender_status, which specifies the current state of
  4043. the connection from the point of view of the sender. The possible values
  4044. are:
  4045. @itemize @bullet
  4046. @item @code{KX_STATE_DOWN} Initial value, never used on the network
  4047. @item @code{KX_STATE_KEY_SENT} We sent our ephemeral key, do not know the
  4048. key of the other peer
  4049. @item @code{KX_STATE_KEY_RECEIVED} This peer has received a valid
  4050. ephemeral key of the other peer, but we are waiting for the other peer to
  4051. confirm it's authenticity (ability to decode) via challenge-response.
  4052. @item @code{KX_STATE_UP} The connection is fully up from the point of
  4053. view of the sender (now performing keep-alives)
  4054. @item @code{KX_STATE_REKEY_SENT} The sender has initiated a rekeying
  4055. operation; the other peer has so far failed to confirm a working
  4056. connection using the new ephemeral key
  4057. @end itemize
  4058. @node Establishing a connection
  4059. @subsubsection Establishing a connection
  4060. Peers begin their interaction by sending a @code{EphemeralKeyMessage} to
  4061. the other peer once the TRANSPORT service notifies the CORE service about
  4062. the connection.
  4063. A peer receiving an @code{EphemeralKeyMessage} with a status
  4064. indicating that the sender does not have the receiver's ephemeral key, the
  4065. receiver's @code{EphemeralKeyMessage} is sent in response.
  4066. Additionally, if the receiver has not yet confirmed the authenticity of
  4067. the sender, it also sends an (encrypted)@code{PingMessage} with a
  4068. challenge (and the identity of the target) to the other peer. Peers
  4069. receiving a @code{PingMessage} respond with an (encrypted)
  4070. @code{PongMessage} which includes the challenge. Peers receiving a
  4071. @code{PongMessage} check the challenge, and if it matches set the
  4072. connection to @code{KX_STATE_UP}.
  4073. @node Encryption and Decryption
  4074. @subsubsection Encryption and Decryption
  4075. All functions related to the key exchange and encryption/decryption of
  4076. messages can be found in @file{gnunet-service-core_kx.c} (except for the
  4077. cryptographic primitives, which are in @file{util/crypto*.c}).
  4078. Given the key material from ECDHE, a Key derivation function
  4079. (@uref{https://en.wikipedia.org/wiki/Key_derivation_function, Key derivation function})
  4080. is used to derive two pairs of encryption and decryption keys for AES-256
  4081. and TwoFish, as well as initialization vectors and authentication keys
  4082. (for HMAC
  4083. (@uref{https://en.wikipedia.org/wiki/HMAC, HMAC})).
  4084. The HMAC is computed over the encrypted payload.
  4085. Encrypted messages include an iv_seed and the HMAC in the header.
  4086. Each encrypted message in the CORE service includes a sequence number and
  4087. a timestamp in the encrypted payload. The CORE service remembers the
  4088. largest observed sequence number and a bit-mask which represents which of
  4089. the previous 32 sequence numbers were already used.
  4090. Messages with sequence numbers lower than the largest observed sequence
  4091. number minus 32 are discarded. Messages with a timestamp that is less
  4092. than @code{REKEY_TOLERANCE} off (5 minutes) are also discarded. This of
  4093. course means that system clocks need to be reasonably synchronized for
  4094. peers to be able to communicate. Additionally, as the ephemeral key
  4095. changes every 12 hours, a peer would not even be able to decrypt messages
  4096. older than 12 hours.
  4097. @node Type maps
  4098. @subsubsection Type maps
  4099. Once an encrypted connection has been established, peers begin to exchange
  4100. type maps. Type maps are used to allow the CORE service to determine which
  4101. (encrypted) connections should be shown to which applications. A type map
  4102. is an array of 65536 bits representing the different types of messages
  4103. understood by applications using the CORE service. Each CORE service
  4104. maintains this map, simply by setting the respective bit for each message
  4105. type supported by any of the applications using the CORE service. Note
  4106. that bits for message types embedded in higher-level protocols (such as
  4107. MESH) will not be included in these type maps.
  4108. Typically, the type map of a peer will be sparse. Thus, the CORE service
  4109. attempts to compress its type map using @code{gzip}-style compression
  4110. ("deflate") prior to transmission. However, if the compression fails to
  4111. compact the map, the map may also be transmitted without compression
  4112. (resulting in @code{GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP} or
  4113. @code{GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP} messages respectively).
  4114. Upon receiving a type map, the respective CORE service notifies
  4115. applications about the connection to the other peer if they support any
  4116. message type indicated in the type map (or no message type at all).
  4117. If the CORE service experience a connect or disconnect event from an
  4118. application, it updates its type map (setting or unsetting the respective
  4119. bits) and notifies its neighbours about the change.
  4120. The CORE services of the neighbours then in turn generate connect and
  4121. disconnect events for the peer that sent the type map for their respective
  4122. applications. As CORE messages may be lost, the CORE service confirms
  4123. receiving a type map by sending back a
  4124. @code{GNUNET_MESSAGE_TYPE_CORE_CONFIRM_TYPE_MAP}. If such a confirmation
  4125. (with the correct hash of the type map) is not received, the sender will
  4126. retransmit the type map (with exponential back-off).
  4127. @cindex CADET Subsystem
  4128. @cindex CADET
  4129. @cindex cadet
  4130. @node CADET Subsystem
  4131. @section CADET Subsystem
  4132. The CADET subsystem in GNUnet is responsible for secure end-to-end
  4133. communications between nodes in the GNUnet overlay network. CADET builds
  4134. on the CORE subsystem which provides for the link-layer communication and
  4135. then adds routing, forwarding and additional security to the connections.
  4136. CADET offers the same cryptographic services as CORE, but on an
  4137. end-to-end level. This is done so peers retransmitting traffic on behalf
  4138. of other peers cannot access the payload data.
  4139. @itemize @bullet
  4140. @item CADET provides confidentiality with so-called perfect forward
  4141. secrecy; we use ECDHE powered by Curve25519 for the key exchange and then
  4142. use symmetric encryption, encrypting with both AES-256 and Twofish
  4143. @item authentication is achieved by signing the ephemeral keys using
  4144. Ed25519, a deterministic variant of ECDSA
  4145. @item integrity protection (using SHA-512 to do encrypt-then-MAC, although
  4146. only 256 bits are sent to reduce overhead)
  4147. @item replay protection (using nonces, timestamps, challenge-response,
  4148. message counters and ephemeral keys)
  4149. @item liveness (keep-alive messages, timeout)
  4150. @end itemize
  4151. Additional to the CORE-like security benefits, CADET offers other
  4152. properties that make it a more universal service than CORE.
  4153. @itemize @bullet
  4154. @item CADET can establish channels to arbitrary peers in GNUnet. If a
  4155. peer is not immediately reachable, CADET will find a path through the
  4156. network and ask other peers to retransmit the traffic on its behalf.
  4157. @item CADET offers (optional) reliability mechanisms. In a reliable
  4158. channel traffic is guaranteed to arrive complete, unchanged and in-order.
  4159. @item CADET takes care of flow and congestion control mechanisms, not
  4160. allowing the sender to send more traffic than the receiver or the network
  4161. are able to process.
  4162. @end itemize
  4163. @menu
  4164. * libgnunetcadet::
  4165. @end menu
  4166. @cindex libgnunetcadet
  4167. @node libgnunetcadet
  4168. @subsection libgnunetcadet
  4169. The CADET API (defined in @file{gnunet_cadet_service.h}) is the
  4170. messaging API used by P2P applications built using GNUnet.
  4171. It provides applications the ability to send and receive encrypted
  4172. messages to any peer participating in GNUnet.
  4173. The API is heavily base on the CORE API.
  4174. CADET delivers messages to other peers in "channels".
  4175. A channel is a permanent connection defined by a destination peer
  4176. (identified by its public key) and a port number.
  4177. Internally, CADET tunnels all channels towards a destination peer
  4178. using one session key and relays the data on multiple "connections",
  4179. independent from the channels.
  4180. Each channel has optional parameters, the most important being the
  4181. reliability flag.
  4182. Should a message get lost on TRANSPORT/CORE level, if a channel is
  4183. created with as reliable, CADET will retransmit the lost message and
  4184. deliver it in order to the destination application.
  4185. @pindex GNUNET_CADET_connect
  4186. To communicate with other peers using CADET, it is necessary to first
  4187. connect to the service using @code{GNUNET_CADET_connect}.
  4188. This function takes several parameters in form of callbacks, to allow the
  4189. client to react to various events, like incoming channels or channels that
  4190. terminate, as well as specify a list of ports the client wishes to listen
  4191. to (at the moment it is not possible to start listening on further ports
  4192. once connected, but nothing prevents a client to connect several times to
  4193. CADET, even do one connection per listening port).
  4194. The function returns a handle which has to be used for any further
  4195. interaction with the service.
  4196. @pindex GNUNET_CADET_channel_create
  4197. To connect to a remote peer, a client has to call the
  4198. @code{GNUNET_CADET_channel_create} function. The most important parameters
  4199. given are the remote peer's identity (it public key) and a port, which
  4200. specifies which application on the remote peer to connect to, similar to
  4201. TCP/UDP ports. CADET will then find the peer in the GNUnet network and
  4202. establish the proper low-level connections and do the necessary key
  4203. exchanges to assure and authenticated, secure and verified communication.
  4204. Similar to @code{GNUNET_CADET_connect},@code{GNUNET_CADET_create_channel}
  4205. returns a handle to interact with the created channel.
  4206. @pindex GNUNET_CADET_notify_transmit_ready
  4207. For every message the client wants to send to the remote application,
  4208. @code{GNUNET_CADET_notify_transmit_ready} must be called, indicating the
  4209. channel on which the message should be sent and the size of the message
  4210. (but not the message itself!). Once CADET is ready to send the message,
  4211. the provided callback will fire, and the message contents are provided to
  4212. this callback.
  4213. Please note the CADET does not provide an explicit notification of when a
  4214. channel is connected. In loosely connected networks, like big wireless
  4215. mesh networks, this can take several seconds, even minutes in the worst
  4216. case. To be alerted when a channel is online, a client can call
  4217. @code{GNUNET_CADET_notify_transmit_ready} immediately after
  4218. @code{GNUNET_CADET_create_channel}. When the callback is activated, it
  4219. means that the channel is online. The callback can give 0 bytes to CADET
  4220. if no message is to be sent, this is OK.
  4221. @pindex GNUNET_CADET_notify_transmit_cancel
  4222. If a transmission was requested but before the callback fires it is no
  4223. longer needed, it can be canceled with
  4224. @code{GNUNET_CADET_notify_transmit_ready_cancel}, which uses the handle
  4225. given back by @code{GNUNET_CADET_notify_transmit_ready}.
  4226. As in the case of CORE, only one message can be requested at a time: a
  4227. client must not call @code{GNUNET_CADET_notify_transmit_ready} again until
  4228. the callback is called or the request is canceled.
  4229. @pindex GNUNET_CADET_channel_destroy
  4230. When a channel is no longer needed, a client can call
  4231. @code{GNUNET_CADET_channel_destroy} to get rid of it.
  4232. Note that CADET will try to transmit all pending traffic before notifying
  4233. the remote peer of the destruction of the channel, including
  4234. retransmitting lost messages if the channel was reliable.
  4235. Incoming channels, channels being closed by the remote peer, and traffic
  4236. on any incoming or outgoing channels are given to the client when CADET
  4237. executes the callbacks given to it at the time of
  4238. @code{GNUNET_CADET_connect}.
  4239. @pindex GNUNET_CADET_disconnect
  4240. Finally, when an application no longer wants to use CADET, it should call
  4241. @code{GNUNET_CADET_disconnect}, but first all channels and pending
  4242. transmissions must be closed (otherwise CADET will complain).
  4243. @cindex NSE Subsystem
  4244. @node NSE Subsystem
  4245. @section NSE Subsystem
  4246. NSE stands for @dfn{Network Size Estimation}. The NSE subsystem provides
  4247. other subsystems and users with a rough estimate of the number of peers
  4248. currently participating in the GNUnet overlay.
  4249. The computed value is not a precise number as producing a precise number
  4250. in a decentralized, efficient and secure way is impossible.
  4251. While NSE's estimate is inherently imprecise, NSE also gives the expected
  4252. range. For a peer that has been running in a stable network for a
  4253. while, the real network size will typically (99.7% of the time) be in the
  4254. range of [2/3 estimate, 3/2 estimate]. We will now give an overview of the
  4255. algorithm used to calculate the estimate;
  4256. all of the details can be found in this technical report.
  4257. @c FIXME: link to the report.
  4258. @menu
  4259. * Motivation::
  4260. * Principle::
  4261. * libgnunetnse::
  4262. * The NSE Client-Service Protocol::
  4263. * The NSE Peer-to-Peer Protocol::
  4264. @end menu
  4265. @node Motivation
  4266. @subsection Motivation
  4267. Some subsystems, like DHT, need to know the size of the GNUnet network to
  4268. optimize some parameters of their own protocol. The decentralized nature
  4269. of GNUnet makes efficient and securely counting the exact number of peers
  4270. infeasible. Although there are several decentralized algorithms to count
  4271. the number of peers in a system, so far there is none to do so securely.
  4272. Other protocols may allow any malicious peer to manipulate the final
  4273. result or to take advantage of the system to perform
  4274. @dfn{Denial of Service} (DoS) attacks against the network.
  4275. GNUnet's NSE protocol avoids these drawbacks.
  4276. @menu
  4277. * Security::
  4278. @end menu
  4279. @cindex NSE security
  4280. @cindex nse security
  4281. @node Security
  4282. @subsubsection Security
  4283. The NSE subsystem is designed to be resilient against these attacks.
  4284. It uses @uref{http://en.wikipedia.org/wiki/Proof-of-work_system, proofs of work}
  4285. to prevent one peer from impersonating a large number of participants,
  4286. which would otherwise allow an adversary to artificially inflate the
  4287. estimate.
  4288. The DoS protection comes from the time-based nature of the protocol:
  4289. the estimates are calculated periodically and out-of-time traffic is
  4290. either ignored or stored for later retransmission by benign peers.
  4291. In particular, peers cannot trigger global network communication at will.
  4292. @cindex NSE principle
  4293. @cindex nse principle
  4294. @node Principle
  4295. @subsection Principle
  4296. The algorithm calculates the estimate by finding the globally closest
  4297. peer ID to a random, time-based value.
  4298. The idea is that the closer the ID is to the random value, the more
  4299. "densely packed" the ID space is, and therefore, more peers are in the
  4300. network.
  4301. @menu
  4302. * Example::
  4303. * Algorithm::
  4304. * Target value::
  4305. * Timing::
  4306. * Controlled Flooding::
  4307. * Calculating the estimate::
  4308. @end menu
  4309. @node Example
  4310. @subsubsection Example
  4311. Suppose all peers have IDs between 0 and 100 (our ID space), and the
  4312. random value is 42.
  4313. If the closest peer has the ID 70 we can imagine that the average
  4314. "distance" between peers is around 30 and therefore the are around 3
  4315. peers in the whole ID space. On the other hand, if the closest peer has
  4316. the ID 44, we can imagine that the space is rather packed with peers,
  4317. maybe as much as 50 of them.
  4318. Naturally, we could have been rather unlucky, and there is only one peer
  4319. and happens to have the ID 44. Thus, the current estimate is calculated
  4320. as the average over multiple rounds, and not just a single sample.
  4321. @node Algorithm
  4322. @subsubsection Algorithm
  4323. Given that example, one can imagine that the job of the subsystem is to
  4324. efficiently communicate the ID of the closest peer to the target value
  4325. to all the other peers, who will calculate the estimate from it.
  4326. @node Target value
  4327. @subsubsection Target value
  4328. The target value itself is generated by hashing the current time, rounded
  4329. down to an agreed value. If the rounding amount is 1h (default) and the
  4330. time is 12:34:56, the time to hash would be 12:00:00. The process is
  4331. repeated each rounding amount (in this example would be every hour).
  4332. Every repetition is called a round.
  4333. @node Timing
  4334. @subsubsection Timing
  4335. The NSE subsystem has some timing control to avoid everybody broadcasting
  4336. its ID all at one. Once each peer has the target random value, it
  4337. compares its own ID to the target and calculates the hypothetical size of
  4338. the network if that peer were to be the closest.
  4339. Then it compares the hypothetical size with the estimate from the previous
  4340. rounds. For each value there is an associated point in the period,
  4341. let's call it "broadcast time". If its own hypothetical estimate
  4342. is the same as the previous global estimate, its "broadcast time" will be
  4343. in the middle of the round. If its bigger it will be earlier and if its
  4344. smaller (the most likely case) it will be later. This ensures that the
  4345. peers closest to the target value start broadcasting their ID the first.
  4346. @node Controlled Flooding
  4347. @subsubsection Controlled Flooding
  4348. When a peer receives a value, first it verifies that it is closer than the
  4349. closest value it had so far, otherwise it answers the incoming message
  4350. with a message containing the better value. Then it checks a proof of
  4351. work that must be included in the incoming message, to ensure that the
  4352. other peer's ID is not made up (otherwise a malicious peer could claim to
  4353. have an ID of exactly the target value every round). Once validated, it
  4354. compares the broadcast time of the received value with the current time
  4355. and if it's not too early, sends the received value to its neighbors.
  4356. Otherwise it stores the value until the correct broadcast time comes.
  4357. This prevents unnecessary traffic of sub-optimal values, since a better
  4358. value can come before the broadcast time, rendering the previous one
  4359. obsolete and saving the traffic that would have been used to broadcast it
  4360. to the neighbors.
  4361. @node Calculating the estimate
  4362. @subsubsection Calculating the estimate
  4363. Once the closest ID has been spread across the network each peer gets the
  4364. exact distance between this ID and the target value of the round and
  4365. calculates the estimate with a mathematical formula described in the tech
  4366. report. The estimate generated with this method for a single round is not
  4367. very precise. Remember the case of the example, where the only peer is the
  4368. ID 44 and we happen to generate the target value 42, thinking there are
  4369. 50 peers in the network. Therefore, the NSE subsystem remembers the last
  4370. 64 estimates and calculates an average over them, giving a result of which
  4371. usually has one bit of uncertainty (the real size could be half of the
  4372. estimate or twice as much). Note that the actual network size is
  4373. calculated in powers of two of the raw input, thus one bit of uncertainty
  4374. means a factor of two in the size estimate.
  4375. @cindex libgnunetnse
  4376. @node libgnunetnse
  4377. @subsection libgnunetnse
  4378. The NSE subsystem has the simplest API of all services, with only two
  4379. calls: @code{GNUNET_NSE_connect} and @code{GNUNET_NSE_disconnect}.
  4380. The connect call gets a callback function as a parameter and this function
  4381. is called each time the network agrees on an estimate. This usually is
  4382. once per round, with some exceptions: if the closest peer has a late
  4383. local clock and starts spreading its ID after everyone else agreed on a
  4384. value, the callback might be activated twice in a round, the second value
  4385. being always bigger than the first. The default round time is set to
  4386. 1 hour.
  4387. The disconnect call disconnects from the NSE subsystem and the callback
  4388. is no longer called with new estimates.
  4389. @menu
  4390. * Results::
  4391. * libgnunetnse - Examples::
  4392. @end menu
  4393. @node Results
  4394. @subsubsection Results
  4395. The callback provides two values: the average and the
  4396. @uref{http://en.wikipedia.org/wiki/Standard_deviation, standard deviation}
  4397. of the last 64 rounds. The values provided by the callback function are
  4398. logarithmic, this means that the real estimate numbers can be obtained by
  4399. calculating 2 to the power of the given value (2average). From a
  4400. statistics point of view this means that:
  4401. @itemize @bullet
  4402. @item 68% of the time the real size is included in the interval
  4403. [(2average-stddev), 2]
  4404. @item 95% of the time the real size is included in the interval
  4405. [(2average-2*stddev, 2^average+2*stddev]
  4406. @item 99.7% of the time the real size is included in the interval
  4407. [(2average-3*stddev, 2average+3*stddev]
  4408. @end itemize
  4409. The expected standard variation for 64 rounds in a network of stable size
  4410. is 0.2. Thus, we can say that normally:
  4411. @itemize @bullet
  4412. @item 68% of the time the real size is in the range [-13%, +15%]
  4413. @item 95% of the time the real size is in the range [-24%, +32%]
  4414. @item 99.7% of the time the real size is in the range [-34%, +52%]
  4415. @end itemize
  4416. As said in the introduction, we can be quite sure that usually the real
  4417. size is between one third and three times the estimate. This can of
  4418. course vary with network conditions.
  4419. Thus, applications may want to also consider the provided standard
  4420. deviation value, not only the average (in particular, if the standard
  4421. variation is very high, the average maybe meaningless: the network size is
  4422. changing rapidly).
  4423. @node libgnunetnse - Examples
  4424. @subsubsection libgnunetnse -Examples
  4425. Let's close with a couple examples.
  4426. @table @asis
  4427. @item Average: 10, std dev: 1 Here the estimate would be
  4428. 2^10 = 1024 peers. (The range in which we can be 95% sure is:
  4429. [2^8, 2^12] = [256, 4096]. We can be very (>99.7%) sure that the network
  4430. is not a hundred peers and absolutely sure that it is not a million peers,
  4431. but somewhere around a thousand.)
  4432. @item Average 22, std dev: 0.2 Here the estimate would be
  4433. 2^22 = 4 Million peers. (The range in which we can be 99.7% sure
  4434. is: [2^21.4, 2^22.6] = [2.8M, 6.3M]. We can be sure that the network size
  4435. is around four million, with absolutely way of it being 1 million.)
  4436. @end table
  4437. To put this in perspective, if someone remembers the LHC Higgs boson
  4438. results, were announced with "5 sigma" and "6 sigma" certainties. In this
  4439. case a 5 sigma minimum would be 2 million and a 6 sigma minimum,
  4440. 1.8 million.
  4441. @node The NSE Client-Service Protocol
  4442. @subsection The NSE Client-Service Protocol
  4443. As with the API, the client-service protocol is very simple, only has 2
  4444. different messages, defined in @code{src/nse/nse.h}:
  4445. @itemize @bullet
  4446. @item @code{GNUNET_MESSAGE_TYPE_NSE_START}@ This message has no parameters
  4447. and is sent from the client to the service upon connection.
  4448. @item @code{GNUNET_MESSAGE_TYPE_NSE_ESTIMATE}@ This message is sent from
  4449. the service to the client for every new estimate and upon connection.
  4450. Contains a timestamp for the estimate, the average and the standard
  4451. deviation for the respective round.
  4452. @end itemize
  4453. When the @code{GNUNET_NSE_disconnect} API call is executed, the client
  4454. simply disconnects from the service, with no message involved.
  4455. @cindex NSE Peer-to-Peer Protocol
  4456. @node The NSE Peer-to-Peer Protocol
  4457. @subsection The NSE Peer-to-Peer Protocol
  4458. @pindex GNUNET_MESSAGE_TYPE_NSE_P2P_FLOOD
  4459. The NSE subsystem only has one message in the P2P protocol, the
  4460. @code{GNUNET_MESSAGE_TYPE_NSE_P2P_FLOOD} message.
  4461. This message key contents are the timestamp to identify the round
  4462. (differences in system clocks may cause some peers to send messages way
  4463. too early or way too late, so the timestamp allows other peers to
  4464. identify such messages easily), the
  4465. @uref{http://en.wikipedia.org/wiki/Proof-of-work_system, proof of work}
  4466. used to make it difficult to mount a
  4467. @uref{http://en.wikipedia.org/wiki/Sybil_attack, Sybil attack}, and the
  4468. public key, which is used to verify the signature on the message.
  4469. Every peer stores a message for the previous, current and next round. The
  4470. messages for the previous and current round are given to peers that
  4471. connect to us. The message for the next round is simply stored until our
  4472. system clock advances to the next round. The message for the current round
  4473. is what we are flooding the network with right now.
  4474. At the beginning of each round the peer does the following:
  4475. @itemize @bullet
  4476. @item calculates its own distance to the target value
  4477. @item creates, signs and stores the message for the current round (unless
  4478. it has a better message in the "next round" slot which came early in the
  4479. previous round)
  4480. @item calculates, based on the stored round message (own or received) when
  4481. to start flooding it to its neighbors
  4482. @end itemize
  4483. Upon receiving a message the peer checks the validity of the message
  4484. (round, proof of work, signature). The next action depends on the
  4485. contents of the incoming message:
  4486. @itemize @bullet
  4487. @item if the message is worse than the current stored message, the peer
  4488. sends the current message back immediately, to stop the other peer from
  4489. spreading suboptimal results
  4490. @item if the message is better than the current stored message, the peer
  4491. stores the new message and calculates the new target time to start
  4492. spreading it to its neighbors (excluding the one the message came from)
  4493. @item if the message is for the previous round, it is compared to the
  4494. message stored in the "previous round slot", which may then be updated
  4495. @item if the message is for the next round, it is compared to the message
  4496. stored in the "next round slot", which again may then be updated
  4497. @end itemize
  4498. Finally, when it comes to send the stored message for the current round to
  4499. the neighbors there is a random delay added for each neighbor, to avoid
  4500. traffic spikes and minimize cross-messages.
  4501. @cindex HOSTLIST Subsystem
  4502. @node HOSTLIST Subsystem
  4503. @section HOSTLIST Subsystem
  4504. Peers in the GNUnet overlay network need address information so that they
  4505. can connect with other peers. GNUnet uses so called HELLO messages to
  4506. store and exchange peer addresses.
  4507. GNUnet provides several methods for peers to obtain this information:
  4508. @itemize @bullet
  4509. @item out-of-band exchange of HELLO messages (manually, using for example
  4510. gnunet-peerinfo)
  4511. @item HELLO messages shipped with GNUnet (automatic with distribution)
  4512. @item UDP neighbor discovery in LAN (IPv4 broadcast, IPv6 multicast)
  4513. @item topology gossiping (learning from other peers we already connected
  4514. to), and
  4515. @item the HOSTLIST daemon covered in this section, which is particularly
  4516. relevant for bootstrapping new peers.
  4517. @end itemize
  4518. New peers have no existing connections (and thus cannot learn from gossip
  4519. among peers), may not have other peers in their LAN and might be started
  4520. with an outdated set of HELLO messages from the distribution.
  4521. In this case, getting new peers to connect to the network requires either
  4522. manual effort or the use of a HOSTLIST to obtain HELLOs.
  4523. @menu
  4524. * HELLOs::
  4525. * Overview for the HOSTLIST subsystem::
  4526. * Interacting with the HOSTLIST daemon::
  4527. * Hostlist security address validation::
  4528. * The HOSTLIST daemon::
  4529. * The HOSTLIST server::
  4530. * The HOSTLIST client::
  4531. * Usage::
  4532. @end menu
  4533. @node HELLOs
  4534. @subsection HELLOs
  4535. The basic information peers require to connect to other peers are
  4536. contained in so called HELLO messages you can think of as a business card.
  4537. Besides the identity of the peer (based on the cryptographic public key) a
  4538. HELLO message may contain address information that specifies ways to
  4539. contact a peer. By obtaining HELLO messages, a peer can learn how to
  4540. contact other peers.
  4541. @node Overview for the HOSTLIST subsystem
  4542. @subsection Overview for the HOSTLIST subsystem
  4543. The HOSTLIST subsystem provides a way to distribute and obtain contact
  4544. information to connect to other peers using a simple HTTP GET request.
  4545. It's implementation is split in three parts, the main file for the daemon
  4546. itself (@file{gnunet-daemon-hostlist.c}), the HTTP client used to download
  4547. peer information (@file{hostlist-client.c}) and the server component used
  4548. to provide this information to other peers (@file{hostlist-server.c}).
  4549. The server is basically a small HTTP web server (based on GNU
  4550. libmicrohttpd) which provides a list of HELLOs known to the local peer for
  4551. download. The client component is basically a HTTP client
  4552. (based on libcurl) which can download hostlists from one or more websites.
  4553. The hostlist format is a binary blob containing a sequence of HELLO
  4554. messages. Note that any HTTP server can theoretically serve a hostlist,
  4555. the build-in hostlist server makes it simply convenient to offer this
  4556. service.
  4557. @menu
  4558. * Features::
  4559. * HOSTLIST - Limitations::
  4560. @end menu
  4561. @node Features
  4562. @subsubsection Features
  4563. The HOSTLIST daemon can:
  4564. @itemize @bullet
  4565. @item provide HELLO messages with validated addresses obtained from
  4566. PEERINFO to download for other peers
  4567. @item download HELLO messages and forward these message to the TRANSPORT
  4568. subsystem for validation
  4569. @item advertises the URL of this peer's hostlist address to other peers
  4570. via gossip
  4571. @item automatically learn about hostlist servers from the gossip of other
  4572. peers
  4573. @end itemize
  4574. @node HOSTLIST - Limitations
  4575. @subsubsection HOSTLIST - Limitations
  4576. The HOSTLIST daemon does not:
  4577. @itemize @bullet
  4578. @item verify the cryptographic information in the HELLO messages
  4579. @item verify the address information in the HELLO messages
  4580. @end itemize
  4581. @node Interacting with the HOSTLIST daemon
  4582. @subsection Interacting with the HOSTLIST daemon
  4583. The HOSTLIST subsystem is currently implemented as a daemon, so there is
  4584. no need for the user to interact with it and therefore there is no
  4585. command line tool and no API to communicate with the daemon. In the
  4586. future, we can envision changing this to allow users to manually trigger
  4587. the download of a hostlist.
  4588. Since there is no command line interface to interact with HOSTLIST, the
  4589. only way to interact with the hostlist is to use STATISTICS to obtain or
  4590. modify information about the status of HOSTLIST:
  4591. @example
  4592. $ gnunet-statistics -s hostlist
  4593. @end example
  4594. @noindent
  4595. In particular, HOSTLIST includes a @strong{persistent} value in statistics
  4596. that specifies when the hostlist server might be queried next. As this
  4597. value is exponentially increasing during runtime, developers may want to
  4598. reset or manually adjust it. Note that HOSTLIST (but not STATISTICS) needs
  4599. to be shutdown if changes to this value are to have any effect on the
  4600. daemon (as HOSTLIST does not monitor STATISTICS for changes to the
  4601. download frequency).
  4602. @node Hostlist security address validation
  4603. @subsection Hostlist security address validation
  4604. Since information obtained from other parties cannot be trusted without
  4605. validation, we have to distinguish between @emph{validated} and
  4606. @emph{not validated} addresses. Before using (and so trusting)
  4607. information from other parties, this information has to be double-checked
  4608. (validated). Address validation is not done by HOSTLIST but by the
  4609. TRANSPORT service.
  4610. The HOSTLIST component is functionally located between the PEERINFO and
  4611. the TRANSPORT subsystem. When acting as a server, the daemon obtains valid
  4612. (@emph{validated}) peer information (HELLO messages) from the PEERINFO
  4613. service and provides it to other peers. When acting as a client, it
  4614. contacts the HOSTLIST servers specified in the configuration, downloads
  4615. the (unvalidated) list of HELLO messages and forwards these information
  4616. to the TRANSPORT server to validate the addresses.
  4617. @cindex HOSTLIST daemon
  4618. @node The HOSTLIST daemon
  4619. @subsection The HOSTLIST daemon
  4620. The hostlist daemon is the main component of the HOSTLIST subsystem. It is
  4621. started by the ARM service and (if configured) starts the HOSTLIST client
  4622. and server components.
  4623. @pindex GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT
  4624. If the daemon provides a hostlist itself it can advertise it's own
  4625. hostlist to other peers. To do so it sends a
  4626. @code{GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT} message to other peers
  4627. when they connect to this peer on the CORE level. This hostlist
  4628. advertisement message contains the URL to access the HOSTLIST HTTP
  4629. server of the sender. The daemon may also subscribe to this type of
  4630. message from CORE service, and then forward these kind of message to the
  4631. HOSTLIST client. The client then uses all available URLs to download peer
  4632. information when necessary.
  4633. When starting, the HOSTLIST daemon first connects to the CORE subsystem
  4634. and if hostlist learning is enabled, registers a CORE handler to receive
  4635. this kind of messages. Next it starts (if configured) the client and
  4636. server. It passes pointers to CORE connect and disconnect and receive
  4637. handlers where the client and server store their functions, so the daemon
  4638. can notify them about CORE events.
  4639. To clean up on shutdown, the daemon has a cleaning task, shutting down all
  4640. subsystems and disconnecting from CORE.
  4641. @cindex HOSTLIST server
  4642. @node The HOSTLIST server
  4643. @subsection The HOSTLIST server
  4644. The server provides a way for other peers to obtain HELLOs. Basically it
  4645. is a small web server other peers can connect to and download a list of
  4646. HELLOs using standard HTTP; it may also advertise the URL of the hostlist
  4647. to other peers connecting on CORE level.
  4648. @menu
  4649. * The HTTP Server::
  4650. * Advertising the URL::
  4651. @end menu
  4652. @node The HTTP Server
  4653. @subsubsection The HTTP Server
  4654. During startup, the server starts a web server listening on the port
  4655. specified with the HTTPPORT value (default 8080). In addition it connects
  4656. to the PEERINFO service to obtain peer information. The HOSTLIST server
  4657. uses the GNUNET_PEERINFO_iterate function to request HELLO information for
  4658. all peers and adds their information to a new hostlist if they are
  4659. suitable (expired addresses and HELLOs without addresses are both not
  4660. suitable) and the maximum size for a hostlist is not exceeded
  4661. (MAX_BYTES_PER_HOSTLISTS = 500000).
  4662. When PEERINFO finishes (with a last NULL callback), the server destroys
  4663. the previous hostlist response available for download on the web server
  4664. and replaces it with the updated hostlist. The hostlist format is
  4665. basically a sequence of HELLO messages (as obtained from PEERINFO) without
  4666. any special tokenization. Since each HELLO message contains a size field,
  4667. the response can easily be split into separate HELLO messages by the
  4668. client.
  4669. A HOSTLIST client connecting to the HOSTLIST server will receive the
  4670. hostlist as a HTTP response and the the server will terminate the
  4671. connection with the result code @code{HTTP 200 OK}.
  4672. The connection will be closed immediately if no hostlist is available.
  4673. @node Advertising the URL
  4674. @subsubsection Advertising the URL
  4675. The server also advertises the URL to download the hostlist to other peers
  4676. if hostlist advertisement is enabled.
  4677. When a new peer connects and has hostlist learning enabled, the server
  4678. sends a @code{GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT} message to this
  4679. peer using the CORE service.
  4680. @cindex HOSTLIST client
  4681. @node The HOSTLIST client
  4682. @subsection The HOSTLIST client
  4683. The client provides the functionality to download the list of HELLOs from
  4684. a set of URLs.
  4685. It performs a standard HTTP request to the URLs configured and learned
  4686. from advertisement messages received from other peers. When a HELLO is
  4687. downloaded, the HOSTLIST client forwards the HELLO to the TRANSPORT
  4688. service for validation.
  4689. The client supports two modes of operation:
  4690. @itemize @bullet
  4691. @item download of HELLOs (bootstrapping)
  4692. @item learning of URLs
  4693. @end itemize
  4694. @menu
  4695. * Bootstrapping::
  4696. * Learning::
  4697. @end menu
  4698. @node Bootstrapping
  4699. @subsubsection Bootstrapping
  4700. For bootstrapping, it schedules a task to download the hostlist from the
  4701. set of known URLs.
  4702. The downloads are only performed if the number of current
  4703. connections is smaller than a minimum number of connections
  4704. (at the moment 4).
  4705. The interval between downloads increases exponentially; however, the
  4706. exponential growth is limited if it becomes longer than an hour.
  4707. At that point, the frequency growth is capped at
  4708. (#number of connections * 1h).
  4709. Once the decision has been taken to download HELLOs, the daemon chooses a
  4710. random URL from the list of known URLs. URLs can be configured in the
  4711. configuration or be learned from advertisement messages.
  4712. The client uses a HTTP client library (libcurl) to initiate the download
  4713. using the libcurl multi interface.
  4714. Libcurl passes the data to the callback_download function which
  4715. stores the data in a buffer if space is available and the maximum size for
  4716. a hostlist download is not exceeded (MAX_BYTES_PER_HOSTLISTS = 500000).
  4717. When a full HELLO was downloaded, the HOSTLIST client offers this
  4718. HELLO message to the TRANSPORT service for validation.
  4719. When the download is finished or failed, statistical information about the
  4720. quality of this URL is updated.
  4721. @cindex HOSTLIST learning
  4722. @node Learning
  4723. @subsubsection Learning
  4724. The client also manages hostlist advertisements from other peers. The
  4725. HOSTLIST daemon forwards @code{GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT}
  4726. messages to the client subsystem, which extracts the URL from the message.
  4727. Next, a test of the newly obtained URL is performed by triggering a
  4728. download from the new URL. If the URL works correctly, it is added to the
  4729. list of working URLs.
  4730. The size of the list of URLs is restricted, so if an additional server is
  4731. added and the list is full, the URL with the worst quality ranking
  4732. (determined through successful downloads and number of HELLOs e.g.) is
  4733. discarded. During shutdown the list of URLs is saved to a file for
  4734. persistance and loaded on startup. URLs from the configuration file are
  4735. never discarded.
  4736. @node Usage
  4737. @subsection Usage
  4738. To start HOSTLIST by default, it has to be added to the DEFAULTSERVICES
  4739. section for the ARM services. This is done in the default configuration.
  4740. For more information on how to configure the HOSTLIST subsystem see the
  4741. installation handbook:@
  4742. Configuring the hostlist to bootstrap@
  4743. Configuring your peer to provide a hostlist
  4744. @cindex IDENTITY Subsystem
  4745. @node IDENTITY Subsystem
  4746. @section IDENTITY Subsystem
  4747. Identities of "users" in GNUnet are called egos.
  4748. Egos can be used as pseudonyms ("fake names") or be tied to an
  4749. organization (for example, "GNU") or even the actual identity of a human.
  4750. GNUnet users are expected to have many egos. They might have one tied to
  4751. their real identity, some for organizations they manage, and more for
  4752. different domains where they want to operate under a pseudonym.
  4753. The IDENTITY service allows users to manage their egos. The identity
  4754. service manages the private keys egos of the local user; it does not
  4755. manage identities of other users (public keys). Public keys for other
  4756. users need names to become manageable. GNUnet uses the
  4757. @dfn{GNU Name System} (GNS) to give names to other users and manage their
  4758. public keys securely. This chapter is about the IDENTITY service,
  4759. which is about the management of private keys.
  4760. On the network, an ego corresponds to an ECDSA key (over Curve25519,
  4761. using RFC 6979, as required by GNS). Thus, users can perform actions
  4762. under a particular ego by using (signing with) a particular private key.
  4763. Other users can then confirm that the action was really performed by that
  4764. ego by checking the signature against the respective public key.
  4765. The IDENTITY service allows users to associate a human-readable name with
  4766. each ego. This way, users can use names that will remind them of the
  4767. purpose of a particular ego.
  4768. The IDENTITY service will store the respective private keys and
  4769. allows applications to access key information by name.
  4770. Users can change the name that is locally (!) associated with an ego.
  4771. Egos can also be deleted, which means that the private key will be removed
  4772. and it thus will not be possible to perform actions with that ego in the
  4773. future.
  4774. Additionally, the IDENTITY subsystem can associate service functions with
  4775. egos.
  4776. For example, GNS requires the ego that should be used for the shorten
  4777. zone. GNS will ask IDENTITY for an ego for the "gns-short" service.
  4778. The IDENTITY service has a mapping of such service strings to the name of
  4779. the ego that the user wants to use for this service, for example
  4780. "my-short-zone-ego".
  4781. Finally, the IDENTITY API provides access to a special ego, the
  4782. anonymous ego. The anonymous ego is special in that its private key is not
  4783. really private, but fixed and known to everyone.
  4784. Thus, anyone can perform actions as anonymous. This can be useful as with
  4785. this trick, code does not have to contain a special case to distinguish
  4786. between anonymous and pseudonymous egos.
  4787. @menu
  4788. * libgnunetidentity::
  4789. * The IDENTITY Client-Service Protocol::
  4790. @end menu
  4791. @cindex libgnunetidentity
  4792. @node libgnunetidentity
  4793. @subsection libgnunetidentity
  4794. @menu
  4795. * Connecting to the service::
  4796. * Operations on Egos::
  4797. * The anonymous Ego::
  4798. * Convenience API to lookup a single ego::
  4799. * Associating egos with service functions::
  4800. @end menu
  4801. @node Connecting to the service
  4802. @subsubsection Connecting to the service
  4803. First, typical clients connect to the identity service using
  4804. @code{GNUNET_IDENTITY_connect}. This function takes a callback as a
  4805. parameter.
  4806. If the given callback parameter is non-null, it will be invoked to notify
  4807. the application about the current state of the identities in the system.
  4808. @itemize @bullet
  4809. @item First, it will be invoked on all known egos at the time of the
  4810. connection. For each ego, a handle to the ego and the user's name for the
  4811. ego will be passed to the callback. Furthermore, a @code{void **} context
  4812. argument will be provided which gives the client the opportunity to
  4813. associate some state with the ego.
  4814. @item Second, the callback will be invoked with NULL for the ego, the name
  4815. and the context. This signals that the (initial) iteration over all egos
  4816. has completed.
  4817. @item Then, the callback will be invoked whenever something changes about
  4818. an ego.
  4819. If an ego is renamed, the callback is invoked with the ego handle of the
  4820. ego that was renamed, and the new name. If an ego is deleted, the callback
  4821. is invoked with the ego handle and a name of NULL. In the deletion case,
  4822. the application should also release resources stored in the context.
  4823. @item When the application destroys the connection to the identity service
  4824. using @code{GNUNET_IDENTITY_disconnect}, the callback is again invoked
  4825. with the ego and a name of NULL (equivalent to deletion of the egos).
  4826. This should again be used to clean up the per-ego context.
  4827. @end itemize
  4828. The ego handle passed to the callback remains valid until the callback is
  4829. invoked with a name of NULL, so it is safe to store a reference to the
  4830. ego's handle.
  4831. @node Operations on Egos
  4832. @subsubsection Operations on Egos
  4833. Given an ego handle, the main operations are to get its associated private
  4834. key using @code{GNUNET_IDENTITY_ego_get_private_key} or its associated
  4835. public key using @code{GNUNET_IDENTITY_ego_get_public_key}.
  4836. The other operations on egos are pretty straightforward.
  4837. Using @code{GNUNET_IDENTITY_create}, an application can request the
  4838. creation of an ego by specifying the desired name.
  4839. The operation will fail if that name is
  4840. already in use. Using @code{GNUNET_IDENTITY_rename} the name of an
  4841. existing ego can be changed. Finally, egos can be deleted using
  4842. @code{GNUNET_IDENTITY_delete}. All of these operations will trigger
  4843. updates to the callback given to the @code{GNUNET_IDENTITY_connect}
  4844. function of all applications that are connected with the identity service
  4845. at the time. @code{GNUNET_IDENTITY_cancel} can be used to cancel the
  4846. operations before the respective continuations would be called.
  4847. It is not guaranteed that the operation will not be completed anyway,
  4848. only the continuation will no longer be called.
  4849. @node The anonymous Ego
  4850. @subsubsection The anonymous Ego
  4851. A special way to obtain an ego handle is to call
  4852. @code{GNUNET_IDENTITY_ego_get_anonymous}, which returns an ego for the
  4853. "anonymous" user --- anyone knows and can get the private key for this
  4854. user, so it is suitable for operations that are supposed to be anonymous
  4855. but require signatures (for example, to avoid a special path in the code).
  4856. The anonymous ego is always valid and accessing it does not require a
  4857. connection to the identity service.
  4858. @node Convenience API to lookup a single ego
  4859. @subsubsection Convenience API to lookup a single ego
  4860. As applications commonly simply have to lookup a single ego, there is a
  4861. convenience API to do just that. Use @code{GNUNET_IDENTITY_ego_lookup} to
  4862. lookup a single ego by name. Note that this is the user's name for the
  4863. ego, not the service function. The resulting ego will be returned via a
  4864. callback and will only be valid during that callback. The operation can
  4865. be canceled via @code{GNUNET_IDENTITY_ego_lookup_cancel}
  4866. (cancellation is only legal before the callback is invoked).
  4867. @node Associating egos with service functions
  4868. @subsubsection Associating egos with service functions
  4869. The @code{GNUNET_IDENTITY_set} function is used to associate a particular
  4870. ego with a service function. The name used by the service and the ego are
  4871. given as arguments.
  4872. Afterwards, the service can use its name to lookup the associated ego
  4873. using @code{GNUNET_IDENTITY_get}.
  4874. @node The IDENTITY Client-Service Protocol
  4875. @subsection The IDENTITY Client-Service Protocol
  4876. A client connecting to the identity service first sends a message with
  4877. type
  4878. @code{GNUNET_MESSAGE_TYPE_IDENTITY_START} to the service. After that, the
  4879. client will receive information about changes to the egos by receiving
  4880. messages of type @code{GNUNET_MESSAGE_TYPE_IDENTITY_UPDATE}.
  4881. Those messages contain the private key of the ego and the user's name of
  4882. the ego (or zero bytes for the name to indicate that the ego was deleted).
  4883. A special bit @code{end_of_list} is used to indicate the end of the
  4884. initial iteration over the identity service's egos.
  4885. The client can trigger changes to the egos by sending @code{CREATE},
  4886. @code{RENAME} or @code{DELETE} messages.
  4887. The CREATE message contains the private key and the desired name.@
  4888. The RENAME message contains the old name and the new name.@
  4889. The DELETE message only needs to include the name of the ego to delete.@
  4890. The service responds to each of these messages with a @code{RESULT_CODE}
  4891. message which indicates success or error of the operation, and possibly
  4892. a human-readable error message.
  4893. Finally, the client can bind the name of a service function to an ego by
  4894. sending a @code{SET_DEFAULT} message with the name of the service function
  4895. and the private key of the ego.
  4896. Such bindings can then be resolved using a @code{GET_DEFAULT} message,
  4897. which includes the name of the service function. The identity service
  4898. will respond to a GET_DEFAULT request with a SET_DEFAULT message
  4899. containing the respective information, or with a RESULT_CODE to
  4900. indicate an error.
  4901. @cindex NAMESTORE Subsystem
  4902. @node NAMESTORE Subsystem
  4903. @section NAMESTORE Subsystem
  4904. The NAMESTORE subsystem provides persistent storage for local GNS zone
  4905. information. All local GNS zone information are managed by NAMESTORE. It
  4906. provides both the functionality to administer local GNS information (e.g.
  4907. delete and add records) as well as to retrieve GNS information (e.g to
  4908. list name information in a client).
  4909. NAMESTORE does only manage the persistent storage of zone information
  4910. belonging to the user running the service: GNS information from other
  4911. users obtained from the DHT are stored by the NAMECACHE subsystem.
  4912. NAMESTORE uses a plugin-based database backend to store GNS information
  4913. with good performance. Here sqlite, MySQL and PostgreSQL are supported
  4914. database backends.
  4915. NAMESTORE clients interact with the IDENTITY subsystem to obtain
  4916. cryptographic information about zones based on egos as described with the
  4917. IDENTITY subsystem, but internally NAMESTORE refers to zones using the
  4918. ECDSA private key.
  4919. In addition, it collaborates with the NAMECACHE subsystem and
  4920. stores zone information when local information are modified in the
  4921. GNS cache to increase look-up performance for local information.
  4922. NAMESTORE provides functionality to look-up and store records, to iterate
  4923. over a specific or all zones and to monitor zones for changes. NAMESTORE
  4924. functionality can be accessed using the NAMESTORE api or the NAMESTORE
  4925. command line tool.
  4926. @menu
  4927. * libgnunetnamestore::
  4928. @end menu
  4929. @cindex libgnunetnamestore
  4930. @node libgnunetnamestore
  4931. @subsection libgnunetnamestore
  4932. To interact with NAMESTORE clients first connect to the NAMESTORE service
  4933. using the @code{GNUNET_NAMESTORE_connect} passing a configuration handle.
  4934. As a result they obtain a NAMESTORE handle, they can use for operations,
  4935. or NULL is returned if the connection failed.
  4936. To disconnect from NAMESTORE, clients use
  4937. @code{GNUNET_NAMESTORE_disconnect} and specify the handle to disconnect.
  4938. NAMESTORE internally uses the ECDSA private key to refer to zones. These
  4939. private keys can be obtained from the IDENTITY subsytem.
  4940. Here @emph{egos} @emph{can be used to refer to zones or the default ego
  4941. assigned to the GNS subsystem can be used to obtained the master zone's
  4942. private key.}
  4943. @menu
  4944. * Editing Zone Information::
  4945. * Iterating Zone Information::
  4946. * Monitoring Zone Information::
  4947. @end menu
  4948. @node Editing Zone Information
  4949. @subsubsection Editing Zone Information
  4950. NAMESTORE provides functions to lookup records stored under a label in a
  4951. zone and to store records under a label in a zone.
  4952. To store (and delete) records, the client uses the
  4953. @code{GNUNET_NAMESTORE_records_store} function and has to provide
  4954. namestore handle to use, the private key of the zone, the label to store
  4955. the records under, the records and number of records plus an callback
  4956. function.
  4957. After the operation is performed NAMESTORE will call the provided
  4958. callback function with the result GNUNET_SYSERR on failure
  4959. (including timeout/queue drop/failure to validate), GNUNET_NO if content
  4960. was already there or not found GNUNET_YES (or other positive value) on
  4961. success plus an additional error message.
  4962. Records are deleted by using the store command with 0 records to store.
  4963. It is important to note, that records are not merged when records exist
  4964. with the label.
  4965. So a client has first to retrieve records, merge with existing records
  4966. and then store the result.
  4967. To perform a lookup operation, the client uses the
  4968. @code{GNUNET_NAMESTORE_records_store} function. Here it has to pass the
  4969. namestore handle, the private key of the zone and the label. It also has
  4970. to provide a callback function which will be called with the result of
  4971. the lookup operation:
  4972. the zone for the records, the label, and the records including the
  4973. number of records included.
  4974. A special operation is used to set the preferred nickname for a zone.
  4975. This nickname is stored with the zone and is automatically merged with
  4976. all labels and records stored in a zone. Here the client uses the
  4977. @code{GNUNET_NAMESTORE_set_nick} function and passes the private key of
  4978. the zone, the nickname as string plus a the callback with the result of
  4979. the operation.
  4980. @node Iterating Zone Information
  4981. @subsubsection Iterating Zone Information
  4982. A client can iterate over all information in a zone or all zones managed
  4983. by NAMESTORE.
  4984. Here a client uses the @code{GNUNET_NAMESTORE_zone_iteration_start}
  4985. function and passes the namestore handle, the zone to iterate over and a
  4986. callback function to call with the result.
  4987. If the client wants to iterate over all the WHAT!? FIXME, it passes NULL for the zone.
  4988. A @code{GNUNET_NAMESTORE_ZoneIterator} handle is returned to be used to
  4989. continue iteration.
  4990. NAMESTORE calls the callback for every result and expects the client to
  4991. call @code{GNUNET_NAMESTORE_zone_iterator_next} to continue to iterate or
  4992. @code{GNUNET_NAMESTORE_zone_iterator_stop} to interrupt the iteration.
  4993. When NAMESTORE reached the last item it will call the callback with a
  4994. NULL value to indicate.
  4995. @node Monitoring Zone Information
  4996. @subsubsection Monitoring Zone Information
  4997. Clients can also monitor zones to be notified about changes. Here the
  4998. clients uses the @code{GNUNET_NAMESTORE_zone_monitor_start} function and
  4999. passes the private key of the zone and and a callback function to call
  5000. with updates for a zone.
  5001. The client can specify to obtain zone information first by iterating over
  5002. the zone and specify a synchronization callback to be called when the
  5003. client and the namestore are synced.
  5004. On an update, NAMESTORE will call the callback with the private key of the
  5005. zone, the label and the records and their number.
  5006. To stop monitoring, the client calls
  5007. @code{GNUNET_NAMESTORE_zone_monitor_stop} and passes the handle obtained
  5008. from the function to start the monitoring.
  5009. @cindex PEERINFO Subsystem
  5010. @node PEERINFO Subsystem
  5011. @section PEERINFO Subsystem
  5012. The PEERINFO subsystem is used to store verified (validated) information
  5013. about known peers in a persistent way. It obtains these addresses for
  5014. example from TRANSPORT service which is in charge of address validation.
  5015. Validation means that the information in the HELLO message are checked by
  5016. connecting to the addresses and performing a cryptographic handshake to
  5017. authenticate the peer instance stating to be reachable with these
  5018. addresses.
  5019. Peerinfo does not validate the HELLO messages itself but only stores them
  5020. and gives them to interested clients.
  5021. As future work, we think about moving from storing just HELLO messages to
  5022. providing a generic persistent per-peer information store.
  5023. More and more subsystems tend to need to store per-peer information in
  5024. persistent way.
  5025. To not duplicate this functionality we plan to provide a PEERSTORE
  5026. service providing this functionality.
  5027. @menu
  5028. * PEERINFO - Features::
  5029. * PEERINFO - Limitations::
  5030. * DeveloperPeer Information::
  5031. * Startup::
  5032. * Managing Information::
  5033. * Obtaining Information::
  5034. * The PEERINFO Client-Service Protocol::
  5035. * libgnunetpeerinfo::
  5036. @end menu
  5037. @node PEERINFO - Features
  5038. @subsection PEERINFO - Features
  5039. @itemize @bullet
  5040. @item Persistent storage
  5041. @item Client notification mechanism on update
  5042. @item Periodic clean up for expired information
  5043. @item Differentiation between public and friend-only HELLO
  5044. @end itemize
  5045. @node PEERINFO - Limitations
  5046. @subsection PEERINFO - Limitations
  5047. @itemize @bullet
  5048. @item Does not perform HELLO validation
  5049. @end itemize
  5050. @node DeveloperPeer Information
  5051. @subsection DeveloperPeer Information
  5052. The PEERINFO subsystem stores these information in the form of HELLO
  5053. messages you can think of as business cards.
  5054. These HELLO messages contain the public key of a peer and the addresses
  5055. a peer can be reached under.
  5056. The addresses include an expiration date describing how long they are
  5057. valid. This information is updated regularly by the TRANSPORT service by
  5058. revalidating the address.
  5059. If an address is expired and not renewed, it can be removed from the
  5060. HELLO message.
  5061. Some peer do not want to have their HELLO messages distributed to other
  5062. peers, especially when GNUnet's friend-to-friend modus is enabled.
  5063. To prevent this undesired distribution. PEERINFO distinguishes between
  5064. @emph{public} and @emph{friend-only} HELLO messages.
  5065. Public HELLO messages can be freely distributed to other (possibly
  5066. unknown) peers (for example using the hostlist, gossiping, broadcasting),
  5067. whereas friend-only HELLO messages may not be distributed to other peers.
  5068. Friend-only HELLO messages have an additional flag @code{friend_only} set
  5069. internally. For public HELLO message this flag is not set.
  5070. PEERINFO does and cannot not check if a client is allowed to obtain a
  5071. specific HELLO type.
  5072. The HELLO messages can be managed using the GNUnet HELLO library.
  5073. Other GNUnet systems can obtain these information from PEERINFO and use
  5074. it for their purposes.
  5075. Clients are for example the HOSTLIST component providing these
  5076. information to other peers in form of a hostlist or the TRANSPORT
  5077. subsystem using these information to maintain connections to other peers.
  5078. @node Startup
  5079. @subsection Startup
  5080. During startup the PEERINFO services loads persistent HELLOs from disk.
  5081. First PEERINFO parses the directory configured in the HOSTS value of the
  5082. @code{PEERINFO} configuration section to store PEERINFO information.
  5083. For all files found in this directory valid HELLO messages are extracted.
  5084. In addition it loads HELLO messages shipped with the GNUnet distribution.
  5085. These HELLOs are used to simplify network bootstrapping by providing
  5086. valid peer information with the distribution.
  5087. The use of these HELLOs can be prevented by setting the
  5088. @code{USE_INCLUDED_HELLOS} in the @code{PEERINFO} configuration section to
  5089. @code{NO}. Files containing invalid information are removed.
  5090. @node Managing Information
  5091. @subsection Managing Information
  5092. The PEERINFO services stores information about known PEERS and a single
  5093. HELLO message for every peer.
  5094. A peer does not need to have a HELLO if no information are available.
  5095. HELLO information from different sources, for example a HELLO obtained
  5096. from a remote HOSTLIST and a second HELLO stored on disk, are combined
  5097. and merged into one single HELLO message per peer which will be given to
  5098. clients. During this merge process the HELLO is immediately written to
  5099. disk to ensure persistence.
  5100. PEERINFO in addition periodically scans the directory where information
  5101. are stored for empty HELLO messages with expired TRANSPORT addresses.
  5102. This periodic task scans all files in the directory and recreates the
  5103. HELLO messages it finds.
  5104. Expired TRANSPORT addresses are removed from the HELLO and if the
  5105. HELLO does not contain any valid addresses, it is discarded and removed
  5106. from the disk.
  5107. @node Obtaining Information
  5108. @subsection Obtaining Information
  5109. When a client requests information from PEERINFO, PEERINFO performs a
  5110. lookup for the respective peer or all peers if desired and transmits this
  5111. information to the client.
  5112. The client can specify if friend-only HELLOs have to be included or not
  5113. and PEERINFO filters the respective HELLO messages before transmitting
  5114. information.
  5115. To notify clients about changes to PEERINFO information, PEERINFO
  5116. maintains a list of clients interested in this notifications.
  5117. Such a notification occurs if a HELLO for a peer was updated (due to a
  5118. merge for example) or a new peer was added.
  5119. @node The PEERINFO Client-Service Protocol
  5120. @subsection The PEERINFO Client-Service Protocol
  5121. To connect and disconnect to and from the PEERINFO Service PEERINFO
  5122. utilizes the util client/server infrastructure, so no special messages
  5123. types are used here.
  5124. To add information for a peer, the plain HELLO message is transmitted to
  5125. the service without any wrapping. All pieces of information required are
  5126. stored within the HELLO message.
  5127. The PEERINFO service provides a message handler accepting and processing
  5128. these HELLO messages.
  5129. When obtaining PEERINFO information using the iterate functionality
  5130. specific messages are used. To obtain information for all peers, a
  5131. @code{struct ListAllPeersMessage} with message type
  5132. @code{GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL} and a flag
  5133. include_friend_only to indicate if friend-only HELLO messages should be
  5134. included are transmitted. If information for a specific peer is required
  5135. a @code{struct ListAllPeersMessage} with
  5136. @code{GNUNET_MESSAGE_TYPE_PEERINFO_GET} containing the peer identity is
  5137. used.
  5138. For both variants the PEERINFO service replies for each HELLO message it
  5139. wants to transmit with a @code{struct ListAllPeersMessage} with type
  5140. @code{GNUNET_MESSAGE_TYPE_PEERINFO_INFO} containing the plain HELLO.
  5141. The final message is @code{struct GNUNET_MessageHeader} with type
  5142. @code{GNUNET_MESSAGE_TYPE_PEERINFO_INFO}. If the client receives this
  5143. message, it can proceed with the next request if any is pending.
  5144. @node libgnunetpeerinfo
  5145. @subsection libgnunetpeerinfo
  5146. The PEERINFO API consists mainly of three different functionalities:
  5147. @itemize @bullet
  5148. @item maintaining a connection to the service
  5149. @item adding new information to the PEERINFO service
  5150. @item retrieving information from the PEERINFO service
  5151. @end itemize
  5152. @menu
  5153. * Connecting to the PEERINFO Service::
  5154. * Adding Information to the PEERINFO Service::
  5155. * Obtaining Information from the PEERINFO Service::
  5156. @end menu
  5157. @node Connecting to the PEERINFO Service
  5158. @subsubsection Connecting to the PEERINFO Service
  5159. To connect to the PEERINFO service the function
  5160. @code{GNUNET_PEERINFO_connect} is used, taking a configuration handle as
  5161. an argument, and to disconnect from PEERINFO the function
  5162. @code{GNUNET_PEERINFO_disconnect}, taking the PEERINFO
  5163. handle returned from the connect function has to be called.
  5164. @node Adding Information to the PEERINFO Service
  5165. @subsubsection Adding Information to the PEERINFO Service
  5166. @code{GNUNET_PEERINFO_add_peer} adds a new peer to the PEERINFO subsystem
  5167. storage. This function takes the PEERINFO handle as an argument, the HELLO
  5168. message to store and a continuation with a closure to be called with the
  5169. result of the operation.
  5170. The @code{GNUNET_PEERINFO_add_peer} returns a handle to this operation
  5171. allowing to cancel the operation with the respective cancel function
  5172. @code{GNUNET_PEERINFO_add_peer_cancel}. To retrieve information from
  5173. PEERINFO you can iterate over all information stored with PEERINFO or you
  5174. can tell PEERINFO to notify if new peer information are available.
  5175. @node Obtaining Information from the PEERINFO Service
  5176. @subsubsection Obtaining Information from the PEERINFO Service
  5177. To iterate over information in PEERINFO you use
  5178. @code{GNUNET_PEERINFO_iterate}.
  5179. This function expects the PEERINFO handle, a flag if HELLO messages
  5180. intended for friend only mode should be included, a timeout how long the
  5181. operation should take and a callback with a callback closure to be called
  5182. for the results.
  5183. If you want to obtain information for a specific peer, you can specify
  5184. the peer identity, if this identity is NULL, information for all peers are
  5185. returned. The function returns a handle to allow to cancel the operation
  5186. using @code{GNUNET_PEERINFO_iterate_cancel}.
  5187. To get notified when peer information changes, you can use
  5188. @code{GNUNET_PEERINFO_notify}.
  5189. This function expects a configuration handle and a flag if friend-only
  5190. HELLO messages should be included. The PEERINFO service will notify you
  5191. about every change and the callback function will be called to notify you
  5192. about changes. The function returns a handle to cancel notifications
  5193. with @code{GNUNET_PEERINFO_notify_cancel}.
  5194. @cindex PEERSTORE Subsystem
  5195. @node PEERSTORE Subsystem
  5196. @section PEERSTORE Subsystem
  5197. GNUnet's PEERSTORE subsystem offers persistent per-peer storage for other
  5198. GNUnet subsystems. GNUnet subsystems can use PEERSTORE to persistently
  5199. store and retrieve arbitrary data.
  5200. Each data record stored with PEERSTORE contains the following fields:
  5201. @itemize @bullet
  5202. @item subsystem: Name of the subsystem responsible for the record.
  5203. @item peerid: Identity of the peer this record is related to.
  5204. @item key: a key string identifying the record.
  5205. @item value: binary record value.
  5206. @item expiry: record expiry date.
  5207. @end itemize
  5208. @menu
  5209. * Functionality::
  5210. * Architecture::
  5211. * libgnunetpeerstore::
  5212. @end menu
  5213. @node Functionality
  5214. @subsection Functionality
  5215. Subsystems can store any type of value under a (subsystem, peerid, key)
  5216. combination. A "replace" flag set during store operations forces the
  5217. PEERSTORE to replace any old values stored under the same
  5218. (subsystem, peerid, key) combination with the new value.
  5219. Additionally, an expiry date is set after which the record is *possibly*
  5220. deleted by PEERSTORE.
  5221. Subsystems can iterate over all values stored under any of the following
  5222. combination of fields:
  5223. @itemize @bullet
  5224. @item (subsystem)
  5225. @item (subsystem, peerid)
  5226. @item (subsystem, key)
  5227. @item (subsystem, peerid, key)
  5228. @end itemize
  5229. Subsystems can also request to be notified about any new values stored
  5230. under a (subsystem, peerid, key) combination by sending a "watch"
  5231. request to PEERSTORE.
  5232. @node Architecture
  5233. @subsection Architecture
  5234. PEERSTORE implements the following components:
  5235. @itemize @bullet
  5236. @item PEERSTORE service: Handles store, iterate and watch operations.
  5237. @item PEERSTORE API: API to be used by other subsystems to communicate and
  5238. issue commands to the PEERSTORE service.
  5239. @item PEERSTORE plugins: Handles the persistent storage. At the moment,
  5240. only an "sqlite" plugin is implemented.
  5241. @end itemize
  5242. @cindex libgnunetpeerstore
  5243. @node libgnunetpeerstore
  5244. @subsection libgnunetpeerstore
  5245. libgnunetpeerstore is the library containing the PEERSTORE API. Subsystems
  5246. wishing to communicate with the PEERSTORE service use this API to open a
  5247. connection to PEERSTORE. This is done by calling
  5248. @code{GNUNET_PEERSTORE_connect} which returns a handle to the newly
  5249. created connection.
  5250. This handle has to be used with any further calls to the API.
  5251. To store a new record, the function @code{GNUNET_PEERSTORE_store} is to
  5252. be used which requires the record fields and a continuation function that
  5253. will be called by the API after the STORE request is sent to the
  5254. PEERSTORE service.
  5255. Note that calling the continuation function does not mean that the record
  5256. is successfully stored, only that the STORE request has been successfully
  5257. sent to the PEERSTORE service.
  5258. @code{GNUNET_PEERSTORE_store_cancel} can be called to cancel the STORE
  5259. request only before the continuation function has been called.
  5260. To iterate over stored records, the function
  5261. @code{GNUNET_PEERSTORE_iterate} is
  5262. to be used. @emph{peerid} and @emph{key} can be set to NULL. An iterator
  5263. callback function will be called with each matching record found and a
  5264. NULL record at the end to signal the end of result set.
  5265. @code{GNUNET_PEERSTORE_iterate_cancel} can be used to cancel the ITERATE
  5266. request before the iterator callback is called with a NULL record.
  5267. To be notified with new values stored under a (subsystem, peerid, key)
  5268. combination, the function @code{GNUNET_PEERSTORE_watch} is to be used.
  5269. This will register the watcher with the PEERSTORE service, any new
  5270. records matching the given combination will trigger the callback
  5271. function passed to @code{GNUNET_PEERSTORE_watch}. This continues until
  5272. @code{GNUNET_PEERSTORE_watch_cancel} is called or the connection to the
  5273. service is destroyed.
  5274. After the connection is no longer needed, the function
  5275. @code{GNUNET_PEERSTORE_disconnect} can be called to disconnect from the
  5276. PEERSTORE service.
  5277. Any pending ITERATE or WATCH requests will be destroyed.
  5278. If the @code{sync_first} flag is set to @code{GNUNET_YES}, the API will
  5279. delay the disconnection until all pending STORE requests are sent to
  5280. the PEERSTORE service, otherwise, the pending STORE requests will be
  5281. destroyed as well.
  5282. @cindex SET Subsystem
  5283. @node SET Subsystem
  5284. @section SET Subsystem
  5285. The SET service implements efficient set operations between two peers
  5286. over a mesh tunnel.
  5287. Currently, set union and set intersection are the only supported
  5288. operations. Elements of a set consist of an @emph{element type} and
  5289. arbitrary binary @emph{data}.
  5290. The size of an element's data is limited to around 62 KB.
  5291. @menu
  5292. * Local Sets::
  5293. * Set Modifications::
  5294. * Set Operations::
  5295. * Result Elements::
  5296. * libgnunetset::
  5297. * The SET Client-Service Protocol::
  5298. * The SET Intersection Peer-to-Peer Protocol::
  5299. * The SET Union Peer-to-Peer Protocol::
  5300. @end menu
  5301. @node Local Sets
  5302. @subsection Local Sets
  5303. Sets created by a local client can be modified and reused for multiple
  5304. operations. As each set operation requires potentially expensive special
  5305. auxiliary data to be computed for each element of a set, a set can only
  5306. participate in one type of set operation (i.e. union or intersection).
  5307. The type of a set is determined upon its creation.
  5308. If a the elements of a set are needed for an operation of a different
  5309. type, all of the set's element must be copied to a new set of appropriate
  5310. type.
  5311. @node Set Modifications
  5312. @subsection Set Modifications
  5313. Even when set operations are active, one can add to and remove elements
  5314. from a set.
  5315. However, these changes will only be visible to operations that have been
  5316. created after the changes have taken place. That is, every set operation
  5317. only sees a snapshot of the set from the time the operation was started.
  5318. This mechanism is @emph{not} implemented by copying the whole set, but by
  5319. attaching @emph{generation information} to each element and operation.
  5320. @node Set Operations
  5321. @subsection Set Operations
  5322. Set operations can be started in two ways: Either by accepting an
  5323. operation request from a remote peer, or by requesting a set operation
  5324. from a remote peer.
  5325. Set operations are uniquely identified by the involved @emph{peers}, an
  5326. @emph{application id} and the @emph{operation type}.
  5327. The client is notified of incoming set operations by @emph{set listeners}.
  5328. A set listener listens for incoming operations of a specific operation
  5329. type and application id.
  5330. Once notified of an incoming set request, the client can accept the set
  5331. request (providing a local set for the operation) or reject it.
  5332. @node Result Elements
  5333. @subsection Result Elements
  5334. The SET service has three @emph{result modes} that determine how an
  5335. operation's result set is delivered to the client:
  5336. @itemize @bullet
  5337. @item @strong{Full Result Set.} All elements of set resulting from the set
  5338. operation are returned to the client.
  5339. @item @strong{Added Elements.} Only elements that result from the
  5340. operation and are not already in the local peer's set are returned.
  5341. Note that for some operations (like set intersection) this result mode
  5342. will never return any elements.
  5343. This can be useful if only the remove peer is actually interested in
  5344. the result of the set operation.
  5345. @item @strong{Removed Elements.} Only elements that are in the local
  5346. peer's initial set but not in the operation's result set are returned.
  5347. Note that for some operations (like set union) this result mode will
  5348. never return any elements. This can be useful if only the remove peer is
  5349. actually interested in the result of the set operation.
  5350. @end itemize
  5351. @cindex libgnunetset
  5352. @node libgnunetset
  5353. @subsection libgnunetset
  5354. @menu
  5355. * Sets::
  5356. * Listeners::
  5357. * Operations::
  5358. * Supplying a Set::
  5359. * The Result Callback::
  5360. @end menu
  5361. @node Sets
  5362. @subsubsection Sets
  5363. New sets are created with @code{GNUNET_SET_create}. Both the local peer's
  5364. configuration (as each set has its own client connection) and the
  5365. operation type must be specified.
  5366. The set exists until either the client calls @code{GNUNET_SET_destroy} or
  5367. the client's connection to the service is disrupted.
  5368. In the latter case, the client is notified by the return value of
  5369. functions dealing with sets. This return value must always be checked.
  5370. Elements are added and removed with @code{GNUNET_SET_add_element} and
  5371. @code{GNUNET_SET_remove_element}.
  5372. @node Listeners
  5373. @subsubsection Listeners
  5374. Listeners are created with @code{GNUNET_SET_listen}. Each time time a
  5375. remote peer suggests a set operation with an application id and operation
  5376. type matching a listener, the listener's callback is invoked.
  5377. The client then must synchronously call either @code{GNUNET_SET_accept}
  5378. or @code{GNUNET_SET_reject}. Note that the operation will not be started
  5379. until the client calls @code{GNUNET_SET_commit}
  5380. (see Section "Supplying a Set").
  5381. @node Operations
  5382. @subsubsection Operations
  5383. Operations to be initiated by the local peer are created with
  5384. @code{GNUNET_SET_prepare}. Note that the operation will not be started
  5385. until the client calls @code{GNUNET_SET_commit}
  5386. (see Section "Supplying a Set").
  5387. @node Supplying a Set
  5388. @subsubsection Supplying a Set
  5389. To create symmetry between the two ways of starting a set operation
  5390. (accepting and initiating it), the operation handles returned by
  5391. @code{GNUNET_SET_accept} and @code{GNUNET_SET_prepare} do not yet have a
  5392. set to operate on, thus they can not do any work yet.
  5393. The client must call @code{GNUNET_SET_commit} to specify a set to use for
  5394. an operation. @code{GNUNET_SET_commit} may only be called once per set
  5395. operation.
  5396. @node The Result Callback
  5397. @subsubsection The Result Callback
  5398. Clients must specify both a result mode and a result callback with
  5399. @code{GNUNET_SET_accept} and @code{GNUNET_SET_prepare}. The result
  5400. callback with a status indicating either that an element was received, or
  5401. the operation failed or succeeded.
  5402. The interpretation of the received element depends on the result mode.
  5403. The callback needs to know which result mode it is used in, as the
  5404. arguments do not indicate if an element is part of the full result set,
  5405. or if it is in the difference between the original set and the final set.
  5406. @node The SET Client-Service Protocol
  5407. @subsection The SET Client-Service Protocol
  5408. @menu
  5409. * Creating Sets::
  5410. * Listeners2::
  5411. * Initiating Operations::
  5412. * Modifying Sets::
  5413. * Results and Operation Status::
  5414. * Iterating Sets::
  5415. @end menu
  5416. @node Creating Sets
  5417. @subsubsection Creating Sets
  5418. For each set of a client, there exists a client connection to the service.
  5419. Sets are created by sending the @code{GNUNET_SERVICE_SET_CREATE} message
  5420. over a new client connection. Multiple operations for one set are
  5421. multiplexed over one client connection, using a request id supplied by
  5422. the client.
  5423. @node Listeners2
  5424. @subsubsection Listeners2
  5425. Each listener also requires a seperate client connection. By sending the
  5426. @code{GNUNET_SERVICE_SET_LISTEN} message, the client notifies the service
  5427. of the application id and operation type it is interested in. A client
  5428. rejects an incoming request by sending @code{GNUNET_SERVICE_SET_REJECT}
  5429. on the listener's client connection.
  5430. In contrast, when accepting an incoming request, a
  5431. @code{GNUNET_SERVICE_SET_ACCEPT} message must be sent over the@ set that
  5432. is supplied for the set operation.
  5433. @node Initiating Operations
  5434. @subsubsection Initiating Operations
  5435. Operations with remote peers are initiated by sending a
  5436. @code{GNUNET_SERVICE_SET_EVALUATE} message to the service. The@ client
  5437. connection that this message is sent by determines the set to use.
  5438. @node Modifying Sets
  5439. @subsubsection Modifying Sets
  5440. Sets are modified with the @code{GNUNET_SERVICE_SET_ADD} and
  5441. @code{GNUNET_SERVICE_SET_REMOVE} messages.
  5442. @c %@menu
  5443. @c %* Results and Operation Status::
  5444. @c %* Iterating Sets::
  5445. @c %@end menu
  5446. @node Results and Operation Status
  5447. @subsubsection Results and Operation Status
  5448. The service notifies the client of result elements and success/failure of
  5449. a set operation with the @code{GNUNET_SERVICE_SET_RESULT} message.
  5450. @node Iterating Sets
  5451. @subsubsection Iterating Sets
  5452. All elements of a set can be requested by sending
  5453. @code{GNUNET_SERVICE_SET_ITER_REQUEST}. The server responds with
  5454. @code{GNUNET_SERVICE_SET_ITER_ELEMENT} and eventually terminates the
  5455. iteration with @code{GNUNET_SERVICE_SET_ITER_DONE}.
  5456. After each received element, the client
  5457. must send @code{GNUNET_SERVICE_SET_ITER_ACK}. Note that only one set
  5458. iteration may be active for a set at any given time.
  5459. @node The SET Intersection Peer-to-Peer Protocol
  5460. @subsection The SET Intersection Peer-to-Peer Protocol
  5461. The intersection protocol operates over CADET and starts with a
  5462. GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST being sent by the peer
  5463. initiating the operation to the peer listening for inbound requests.
  5464. It includes the number of elements of the initiating peer, which is used
  5465. to decide which side will send a Bloom filter first.
  5466. The listening peer checks if the operation type and application
  5467. identifier are acceptable for its current state.
  5468. If not, it responds with a GNUNET_MESSAGE_TYPE_SET_RESULT and a status of
  5469. GNUNET_SET_STATUS_FAILURE (and terminates the CADET channel).
  5470. If the application accepts the request, the listener sends back a
  5471. @code{GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_ELEMENT_INFO} if it has
  5472. more elements in the set than the client.
  5473. Otherwise, it immediately starts with the Bloom filter exchange.
  5474. If the initiator receives a
  5475. @code{GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_ELEMENT_INFO} response,
  5476. it beings the Bloom filter exchange, unless the set size is indicated to
  5477. be zero, in which case the intersection is considered finished after
  5478. just the initial handshake.
  5479. @menu
  5480. * The Bloom filter exchange::
  5481. * Salt::
  5482. @end menu
  5483. @node The Bloom filter exchange
  5484. @subsubsection The Bloom filter exchange
  5485. In this phase, each peer transmits a Bloom filter over the remaining
  5486. keys of the local set to the other peer using a
  5487. @code{GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF} message. This
  5488. message additionally includes the number of elements left in the sender's
  5489. set, as well as the XOR over all of the keys in that set.
  5490. The number of bits 'k' set per element in the Bloom filter is calculated
  5491. based on the relative size of the two sets.
  5492. Furthermore, the size of the Bloom filter is calculated based on 'k' and
  5493. the number of elements in the set to maximize the amount of data filtered
  5494. per byte transmitted on the wire (while avoiding an excessively high
  5495. number of iterations).
  5496. The receiver of the message removes all elements from its local set that
  5497. do not pass the Bloom filter test.
  5498. It then checks if the set size of the sender and the XOR over the keys
  5499. match what is left of its own set. If they do, it sends a
  5500. @code{GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_DONE} back to indicate
  5501. that the latest set is the final result.
  5502. Otherwise, the receiver starts another Bloom filter exchange, except
  5503. this time as the sender.
  5504. @node Salt
  5505. @subsubsection Salt
  5506. Bloomfilter operations are probabilistic: With some non-zero probability
  5507. the test may incorrectly say an element is in the set, even though it is
  5508. not.
  5509. To mitigate this problem, the intersection protocol iterates exchanging
  5510. Bloom filters using a different random 32-bit salt in each iteration (the
  5511. salt is also included in the message).
  5512. With different salts, set operations may fail for different elements.
  5513. Merging the results from the executions, the probability of failure drops
  5514. to zero.
  5515. The iterations terminate once both peers have established that they have
  5516. sets of the same size, and where the XOR over all keys computes the same
  5517. 512-bit value (leaving a failure probability of 2-511).
  5518. @node The SET Union Peer-to-Peer Protocol
  5519. @subsection The SET Union Peer-to-Peer Protocol
  5520. The SET union protocol is based on Eppstein's efficient set reconciliation
  5521. without prior context. You should read this paper first if you want to
  5522. understand the protocol.
  5523. The union protocol operates over CADET and starts with a
  5524. GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST being sent by the peer
  5525. initiating the operation to the peer listening for inbound requests.
  5526. It includes the number of elements of the initiating peer, which is
  5527. currently not used.
  5528. The listening peer checks if the operation type and application
  5529. identifier are acceptable for its current state. If not, it responds with
  5530. a @code{GNUNET_MESSAGE_TYPE_SET_RESULT} and a status of
  5531. @code{GNUNET_SET_STATUS_FAILURE} (and terminates the CADET channel).
  5532. If the application accepts the request, it sends back a strata estimator
  5533. using a message of type GNUNET_MESSAGE_TYPE_SET_UNION_P2P_SE. The
  5534. initiator evaluates the strata estimator and initiates the exchange of
  5535. invertible Bloom filters, sending a GNUNET_MESSAGE_TYPE_SET_UNION_P2P_IBF.
  5536. During the IBF exchange, if the receiver cannot invert the Bloom filter or
  5537. detects a cycle, it sends a larger IBF in response (up to a defined
  5538. maximum limit; if that limit is reached, the operation fails).
  5539. Elements decoded while processing the IBF are transmitted to the other
  5540. peer using GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENTS, or requested from the
  5541. other peer using GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENT_REQUESTS messages,
  5542. depending on the sign observed during decoding of the IBF.
  5543. Peers respond to a GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENT_REQUESTS message
  5544. with the respective element in a GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENTS
  5545. message. If the IBF fully decodes, the peer responds with a
  5546. GNUNET_MESSAGE_TYPE_SET_UNION_P2P_DONE message instead of another
  5547. GNUNET_MESSAGE_TYPE_SET_UNION_P2P_IBF.
  5548. All Bloom filter operations use a salt to mingle keys before hashing them
  5549. into buckets, such that future iterations have a fresh chance of
  5550. succeeding if they failed due to collisions before.
  5551. @cindex STATISTICS Subsystem
  5552. @node STATISTICS Subsystem
  5553. @section STATISTICS Subsystem
  5554. In GNUnet, the STATISTICS subsystem offers a central place for all
  5555. subsystems to publish unsigned 64-bit integer run-time statistics.
  5556. Keeping this information centrally means that there is a unified way for
  5557. the user to obtain data on all subsystems, and individual subsystems do
  5558. not have to always include a custom data export method for performance
  5559. metrics and other statistics. For example, the TRANSPORT system uses
  5560. STATISTICS to update information about the number of directly connected
  5561. peers and the bandwidth that has been consumed by the various plugins.
  5562. This information is valuable for diagnosing connectivity and performance
  5563. issues.
  5564. Following the GNUnet service architecture, the STATISTICS subsystem is
  5565. divided into an API which is exposed through the header
  5566. @strong{gnunet_statistics_service.h} and the STATISTICS service
  5567. @strong{gnunet-service-statistics}. The @strong{gnunet-statistics}
  5568. command-line tool can be used to obtain (and change) information about
  5569. the values stored by the STATISTICS service. The STATISTICS service does
  5570. not communicate with other peers.
  5571. Data is stored in the STATISTICS service in the form of tuples
  5572. @strong{(subsystem, name, value, persistence)}. The subsystem determines
  5573. to which other GNUnet's subsystem the data belongs. name is the name
  5574. through which value is associated. It uniquely identifies the record
  5575. from among other records belonging to the same subsystem.
  5576. In some parts of the code, the pair @strong{(subsystem, name)} is called
  5577. a @strong{statistic} as it identifies the values stored in the STATISTCS
  5578. service.The persistence flag determines if the record has to be preserved
  5579. across service restarts. A record is said to be persistent if this flag
  5580. is set for it; if not, the record is treated as a non-persistent record
  5581. and it is lost after service restart. Persistent records are written to
  5582. and read from the file @strong{statistics.data} before shutdown
  5583. and upon startup. The file is located in the HOME directory of the peer.
  5584. An anomaly of the STATISTICS service is that it does not terminate
  5585. immediately upon receiving a shutdown signal if it has any clients
  5586. connected to it. It waits for all the clients that are not monitors to
  5587. close their connections before terminating itself.
  5588. This is to prevent the loss of data during peer shutdown --- delaying the
  5589. STATISTICS service shutdown helps other services to store important data
  5590. to STATISTICS during shutdown.
  5591. @menu
  5592. * libgnunetstatistics::
  5593. * The STATISTICS Client-Service Protocol::
  5594. @end menu
  5595. @cindex libgnunetstatistics
  5596. @node libgnunetstatistics
  5597. @subsection libgnunetstatistics
  5598. @strong{libgnunetstatistics} is the library containing the API for the
  5599. STATISTICS subsystem. Any process requiring to use STATISTICS should use
  5600. this API by to open a connection to the STATISTICS service.
  5601. This is done by calling the function @code{GNUNET_STATISTICS_create()}.
  5602. This function takes the subsystem's name which is trying to use STATISTICS
  5603. and a configuration.
  5604. All values written to STATISTICS with this connection will be placed in
  5605. the section corresponding to the given subsystem's name.
  5606. The connection to STATISTICS can be destroyed with the function
  5607. @code{GNUNET_STATISTICS_destroy()}. This function allows for the
  5608. connection to be destroyed immediately or upon transferring all
  5609. pending write requests to the service.
  5610. Note: STATISTICS subsystem can be disabled by setting @code{DISABLE = YES}
  5611. under the @code{[STATISTICS]} section in the configuration. With such a
  5612. configuration all calls to @code{GNUNET_STATISTICS_create()} return
  5613. @code{NULL} as the STATISTICS subsystem is unavailable and no other
  5614. functions from the API can be used.
  5615. @menu
  5616. * Statistics retrieval::
  5617. * Setting statistics and updating them::
  5618. * Watches::
  5619. @end menu
  5620. @node Statistics retrieval
  5621. @subsubsection Statistics retrieval
  5622. Once a connection to the statistics service is obtained, information
  5623. about any other system which uses statistics can be retrieved with the
  5624. function GNUNET_STATISTICS_get().
  5625. This function takes the connection handle, the name of the subsystem
  5626. whose information we are interested in (a @code{NULL} value will
  5627. retrieve information of all available subsystems using STATISTICS), the
  5628. name of the statistic we are interested in (a @code{NULL} value will
  5629. retrieve all available statistics), a continuation callback which is
  5630. called when all of requested information is retrieved, an iterator
  5631. callback which is called for each parameter in the retrieved information
  5632. and a closure for the aforementioned callbacks. The library then invokes
  5633. the iterator callback for each value matching the request.
  5634. Call to @code{GNUNET_STATISTICS_get()} is asynchronous and can be
  5635. canceled with the function @code{GNUNET_STATISTICS_get_cancel()}.
  5636. This is helpful when retrieving statistics takes too long and especially
  5637. when we want to shutdown and cleanup everything.
  5638. @node Setting statistics and updating them
  5639. @subsubsection Setting statistics and updating them
  5640. So far we have seen how to retrieve statistics, here we will learn how we
  5641. can set statistics and update them so that other subsystems can retrieve
  5642. them.
  5643. A new statistic can be set using the function
  5644. @code{GNUNET_STATISTICS_set()}.
  5645. This function takes the name of the statistic and its value and a flag to
  5646. make the statistic persistent.
  5647. The value of the statistic should be of the type @code{uint64_t}.
  5648. The function does not take the name of the subsystem; it is determined
  5649. from the previous @code{GNUNET_STATISTICS_create()} invocation. If
  5650. the given statistic is already present, its value is overwritten.
  5651. An existing statistics can be updated, i.e its value can be increased or
  5652. decreased by an amount with the function
  5653. @code{GNUNET_STATISTICS_update()}.
  5654. The parameters to this function are similar to
  5655. @code{GNUNET_STATISTICS_set()}, except that it takes the amount to be
  5656. changed as a type @code{int64_t} instead of the value.
  5657. The library will combine multiple set or update operations into one
  5658. message if the client performs requests at a rate that is faster than the
  5659. available IPC with the STATISTICS service. Thus, the client does not have
  5660. to worry about sending requests too quickly.
  5661. @node Watches
  5662. @subsubsection Watches
  5663. As interesting feature of STATISTICS lies in serving notifications
  5664. whenever a statistic of our interest is modified.
  5665. This is achieved by registering a watch through the function
  5666. @code{GNUNET_STATISTICS_watch()}.
  5667. The parameters of this function are similar to those of
  5668. @code{GNUNET_STATISTICS_get()}.
  5669. Changes to the respective statistic's value will then cause the given
  5670. iterator callback to be called.
  5671. Note: A watch can only be registered for a specific statistic. Hence
  5672. the subsystem name and the parameter name cannot be @code{NULL} in a
  5673. call to @code{GNUNET_STATISTICS_watch()}.
  5674. A registered watch will keep notifying any value changes until
  5675. @code{GNUNET_STATISTICS_watch_cancel()} is called with the same
  5676. parameters that are used for registering the watch.
  5677. @node The STATISTICS Client-Service Protocol
  5678. @subsection The STATISTICS Client-Service Protocol
  5679. @menu
  5680. * Statistics retrieval2::
  5681. * Setting and updating statistics::
  5682. * Watching for updates::
  5683. @end menu
  5684. @node Statistics retrieval2
  5685. @subsubsection Statistics retrieval2
  5686. To retrieve statistics, the client transmits a message of type
  5687. @code{GNUNET_MESSAGE_TYPE_STATISTICS_GET} containing the given subsystem
  5688. name and statistic parameter to the STATISTICS service.
  5689. The service responds with a message of type
  5690. @code{GNUNET_MESSAGE_TYPE_STATISTICS_VALUE} for each of the statistics
  5691. parameters that match the client request for the client. The end of
  5692. information retrieved is signaled by the service by sending a message of
  5693. type @code{GNUNET_MESSAGE_TYPE_STATISTICS_END}.
  5694. @node Setting and updating statistics
  5695. @subsubsection Setting and updating statistics
  5696. The subsystem name, parameter name, its value and the persistence flag are
  5697. communicated to the service through the message
  5698. @code{GNUNET_MESSAGE_TYPE_STATISTICS_SET}.
  5699. When the service receives a message of type
  5700. @code{GNUNET_MESSAGE_TYPE_STATISTICS_SET}, it retrieves the subsystem
  5701. name and checks for a statistic parameter with matching the name given in
  5702. the message.
  5703. If a statistic parameter is found, the value is overwritten by the new
  5704. value from the message; if not found then a new statistic parameter is
  5705. created with the given name and value.
  5706. In addition to just setting an absolute value, it is possible to perform a
  5707. relative update by sending a message of type
  5708. @code{GNUNET_MESSAGE_TYPE_STATISTICS_SET} with an update flag
  5709. (@code{GNUNET_STATISTICS_SETFLAG_RELATIVE}) signifying that the value in
  5710. the message should be treated as an update value.
  5711. @node Watching for updates
  5712. @subsubsection Watching for updates
  5713. The function registers the watch at the service by sending a message of
  5714. type @code{GNUNET_MESSAGE_TYPE_STATISTICS_WATCH}. The service then sends
  5715. notifications through messages of type
  5716. @code{GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE} whenever the statistic
  5717. parameter's value is changed.
  5718. @cindex DHT
  5719. @cindex Distributed Hash Table
  5720. @node Distributed Hash Table (DHT)
  5721. @section Distributed Hash Table (DHT)
  5722. GNUnet includes a generic distributed hash table that can be used by
  5723. developers building P2P applications in the framework.
  5724. This section documents high-level features and how developers are
  5725. expected to use the DHT.
  5726. We have a research paper detailing how the DHT works.
  5727. Also, Nate's thesis includes a detailed description and performance
  5728. analysis (in chapter 6).
  5729. Key features of GNUnet's DHT include:
  5730. @itemize @bullet
  5731. @item stores key-value pairs with values up to (approximately) 63k in size
  5732. @item works with many underlay network topologies (small-world, random
  5733. graph), underlay does not need to be a full mesh / clique
  5734. @item support for extended queries (more than just a simple 'key'),
  5735. filtering duplicate replies within the network (bloomfilter) and content
  5736. validation (for details, please read the subsection on the block library)
  5737. @item can (optionally) return paths taken by the PUT and GET operations
  5738. to the application
  5739. @item provides content replication to handle churn
  5740. @end itemize
  5741. GNUnet's DHT is randomized and unreliable. Unreliable means that there is
  5742. no strict guarantee that a value stored in the DHT is always
  5743. found --- values are only found with high probability.
  5744. While this is somewhat true in all P2P DHTs, GNUnet developers should be
  5745. particularly wary of this fact (this will help you write secure,
  5746. fault-tolerant code). Thus, when writing any application using the DHT,
  5747. you should always consider the possibility that a value stored in the
  5748. DHT by you or some other peer might simply not be returned, or returned
  5749. with a significant delay.
  5750. Your application logic must be written to tolerate this (naturally, some
  5751. loss of performance or quality of service is expected in this case).
  5752. @menu
  5753. * Block library and plugins::
  5754. * libgnunetdht::
  5755. * The DHT Client-Service Protocol::
  5756. * The DHT Peer-to-Peer Protocol::
  5757. @end menu
  5758. @node Block library and plugins
  5759. @subsection Block library and plugins
  5760. @menu
  5761. * What is a Block?::
  5762. * The API of libgnunetblock::
  5763. * Queries::
  5764. * Sample Code::
  5765. * Conclusion2::
  5766. @end menu
  5767. @node What is a Block?
  5768. @subsubsection What is a Block?
  5769. Blocks are small (< 63k) pieces of data stored under a key (struct
  5770. GNUNET_HashCode). Blocks have a type (enum GNUNET_BlockType) which defines
  5771. their data format. Blocks are used in GNUnet as units of static data
  5772. exchanged between peers and stored (or cached) locally.
  5773. Uses of blocks include file-sharing (the files are broken up into blocks),
  5774. the VPN (DNS information is stored in blocks) and the DHT (all
  5775. information in the DHT and meta-information for the maintenance of the
  5776. DHT are both stored using blocks).
  5777. The block subsystem provides a few common functions that must be
  5778. available for any type of block.
  5779. @cindex libgnunetblock API
  5780. @node The API of libgnunetblock
  5781. @subsubsection The API of libgnunetblock
  5782. The block library requires for each (family of) block type(s) a block
  5783. plugin (implementing @file{gnunet_block_plugin.h}) that provides basic
  5784. functions that are needed by the DHT (and possibly other subsystems) to
  5785. manage the block.
  5786. These block plugins are typically implemented within their respective
  5787. subsystems.
  5788. The main block library is then used to locate, load and query the
  5789. appropriate block plugin.
  5790. Which plugin is appropriate is determined by the block type (which is
  5791. just a 32-bit integer). Block plugins contain code that specifies which
  5792. block types are supported by a given plugin. The block library loads all
  5793. block plugins that are installed at the local peer and forwards the
  5794. application request to the respective plugin.
  5795. The central functions of the block APIs (plugin and main library) are to
  5796. allow the mapping of blocks to their respective key (if possible) and the
  5797. ability to check that a block is well-formed and matches a given
  5798. request (again, if possible).
  5799. This way, GNUnet can avoid storing invalid blocks, storing blocks under
  5800. the wrong key and forwarding blocks in response to a query that they do
  5801. not answer.
  5802. One key function of block plugins is that it allows GNUnet to detect
  5803. duplicate replies (via the Bloom filter). All plugins MUST support
  5804. detecting duplicate replies (by adding the current response to the
  5805. Bloom filter and rejecting it if it is encountered again).
  5806. If a plugin fails to do this, responses may loop in the network.
  5807. @node Queries
  5808. @subsubsection Queries
  5809. The query format for any block in GNUnet consists of four main components.
  5810. First, the type of the desired block must be specified. Second, the query
  5811. must contain a hash code. The hash code is used for lookups in hash
  5812. tables and databases and must not be unique for the block (however, if
  5813. possible a unique hash should be used as this would be best for
  5814. performance).
  5815. Third, an optional Bloom filter can be specified to exclude known results;
  5816. replies that hash to the bits set in the Bloom filter are considered
  5817. invalid. False-positives can be eliminated by sending the same query
  5818. again with a different Bloom filter mutator value, which parameterizes
  5819. the hash function that is used.
  5820. Finally, an optional application-specific "eXtended query" (xquery) can
  5821. be specified to further constrain the results. It is entirely up to
  5822. the type-specific plugin to determine whether or not a given block
  5823. matches a query (type, hash, Bloom filter, and xquery).
  5824. Naturally, not all xquery's are valid and some types of blocks may not
  5825. support Bloom filters either, so the plugin also needs to check if the
  5826. query is valid in the first place.
  5827. Depending on the results from the plugin, the DHT will then discard the
  5828. (invalid) query, forward the query, discard the (invalid) reply, cache the
  5829. (valid) reply, and/or forward the (valid and non-duplicate) reply.
  5830. @node Sample Code
  5831. @subsubsection Sample Code
  5832. The source code in @strong{plugin_block_test.c} is a good starting point
  5833. for new block plugins --- it does the minimal work by implementing a
  5834. plugin that performs no validation at all.
  5835. The respective @strong{Makefile.am} shows how to build and install a
  5836. block plugin.
  5837. @node Conclusion2
  5838. @subsubsection Conclusion2
  5839. In conclusion, GNUnet subsystems that want to use the DHT need to define a
  5840. block format and write a plugin to match queries and replies. For testing,
  5841. the @code{GNUNET_BLOCK_TYPE_TEST} block type can be used; it accepts
  5842. any query as valid and any reply as matching any query.
  5843. This type is also used for the DHT command line tools.
  5844. However, it should NOT be used for normal applications due to the lack
  5845. of error checking that results from this primitive implementation.
  5846. @cindex libgnunetdht
  5847. @node libgnunetdht
  5848. @subsection libgnunetdht
  5849. The DHT API itself is pretty simple and offers the usual GET and PUT
  5850. functions that work as expected. The specified block type refers to the
  5851. block library which allows the DHT to run application-specific logic for
  5852. data stored in the network.
  5853. @menu
  5854. * GET::
  5855. * PUT::
  5856. * MONITOR::
  5857. * DHT Routing Options::
  5858. @end menu
  5859. @node GET
  5860. @subsubsection GET
  5861. When using GET, the main consideration for developers (other than the
  5862. block library) should be that after issuing a GET, the DHT will
  5863. continuously cause (small amounts of) network traffic until the operation
  5864. is explicitly canceled.
  5865. So GET does not simply send out a single network request once; instead,
  5866. the DHT will continue to search for data. This is needed to achieve good
  5867. success rates and also handles the case where the respective PUT
  5868. operation happens after the GET operation was started.
  5869. Developers should not cancel an existing GET operation and then
  5870. explicitly re-start it to trigger a new round of network requests;
  5871. this is simply inefficient, especially as the internal automated version
  5872. can be more efficient, for example by filtering results in the network
  5873. that have already been returned.
  5874. If an application that performs a GET request has a set of replies that it
  5875. already knows and would like to filter, it can call@
  5876. @code{GNUNET_DHT_get_filter_known_results} with an array of hashes over
  5877. the respective blocks to tell the DHT that these results are not
  5878. desired (any more).
  5879. This way, the DHT will filter the respective blocks using the block
  5880. library in the network, which may result in a significant reduction in
  5881. bandwidth consumption.
  5882. @node PUT
  5883. @subsubsection PUT
  5884. @c inconsistent use of ``must'' above it's written ``MUST''
  5885. In contrast to GET operations, developers @strong{must} manually re-run
  5886. PUT operations periodically (if they intend the content to continue to be
  5887. available). Content stored in the DHT expires or might be lost due to
  5888. churn.
  5889. Furthermore, GNUnet's DHT typically requires multiple rounds of PUT
  5890. operations before a key-value pair is consistently available to all
  5891. peers (the DHT randomizes paths and thus storage locations, and only
  5892. after multiple rounds of PUTs there will be a sufficient number of
  5893. replicas in large DHTs). An explicit PUT operation using the DHT API will
  5894. only cause network traffic once, so in order to ensure basic availability
  5895. and resistance to churn (and adversaries), PUTs must be repeated.
  5896. While the exact frequency depends on the application, a rule of thumb is
  5897. that there should be at least a dozen PUT operations within the content
  5898. lifetime. Content in the DHT typically expires after one day, so
  5899. DHT PUT operations should be repeated at least every 1-2 hours.
  5900. @node MONITOR
  5901. @subsubsection MONITOR
  5902. The DHT API also allows applications to monitor messages crossing the
  5903. local DHT service.
  5904. The types of messages used by the DHT are GET, PUT and RESULT messages.
  5905. Using the monitoring API, applications can choose to monitor these
  5906. requests, possibly limiting themselves to requests for a particular block
  5907. type.
  5908. The monitoring API is not only useful for diagnostics, it can also be
  5909. used to trigger application operations based on PUT operations.
  5910. For example, an application may use PUTs to distribute work requests to
  5911. other peers.
  5912. The workers would then monitor for PUTs that give them work, instead of
  5913. looking for work using GET operations.
  5914. This can be beneficial, especially if the workers have no good way to
  5915. guess the keys under which work would be stored.
  5916. Naturally, additional protocols might be needed to ensure that the desired
  5917. number of workers will process the distributed workload.
  5918. @node DHT Routing Options
  5919. @subsubsection DHT Routing Options
  5920. There are two important options for GET and PUT requests:
  5921. @table @asis
  5922. @item GNUNET_DHT_RO_DEMULITPLEX_EVERYWHERE This option means that all
  5923. peers should process the request, even if their peer ID is not closest to
  5924. the key. For a PUT request, this means that all peers that a request
  5925. traverses may make a copy of the data.
  5926. Similarly for a GET request, all peers will check their local database
  5927. for a result. Setting this option can thus significantly improve caching
  5928. and reduce bandwidth consumption --- at the expense of a larger DHT
  5929. database. If in doubt, we recommend that this option should be used.
  5930. @item GNUNET_DHT_RO_RECORD_ROUTE This option instructs the DHT to record
  5931. the path that a GET or a PUT request is taking through the overlay
  5932. network. The resulting paths are then returned to the application with
  5933. the respective result. This allows the receiver of a result to construct
  5934. a path to the originator of the data, which might then be used for
  5935. routing. Naturally, setting this option requires additional bandwidth
  5936. and disk space, so applications should only set this if the paths are
  5937. needed by the application logic.
  5938. @item GNUNET_DHT_RO_FIND_PEER This option is an internal option used by
  5939. the DHT's peer discovery mechanism and should not be used by applications.
  5940. @item GNUNET_DHT_RO_BART This option is currently not implemented. It may
  5941. in the future offer performance improvements for clique topologies.
  5942. @end table
  5943. @node The DHT Client-Service Protocol
  5944. @subsection The DHT Client-Service Protocol
  5945. @menu
  5946. * PUTting data into the DHT::
  5947. * GETting data from the DHT::
  5948. * Monitoring the DHT::
  5949. @end menu
  5950. @node PUTting data into the DHT
  5951. @subsubsection PUTting data into the DHT
  5952. To store (PUT) data into the DHT, the client sends a
  5953. @code{struct GNUNET_DHT_ClientPutMessage} to the service.
  5954. This message specifies the block type, routing options, the desired
  5955. replication level, the expiration time, key,
  5956. value and a 64-bit unique ID for the operation. The service responds with
  5957. a @code{struct GNUNET_DHT_ClientPutConfirmationMessage} with the same
  5958. 64-bit unique ID. Note that the service sends the confirmation as soon as
  5959. it has locally processed the PUT request. The PUT may still be
  5960. propagating through the network at this time.
  5961. In the future, we may want to change this to provide (limited) feedback
  5962. to the client, for example if we detect that the PUT operation had no
  5963. effect because the same key-value pair was already stored in the DHT.
  5964. However, changing this would also require additional state and messages
  5965. in the P2P interaction.
  5966. @node GETting data from the DHT
  5967. @subsubsection GETting data from the DHT
  5968. To retrieve (GET) data from the DHT, the client sends a
  5969. @code{struct GNUNET_DHT_ClientGetMessage} to the service. The message
  5970. specifies routing options, a replication level (for replicating the GET,
  5971. not the content), the desired block type, the key, the (optional)
  5972. extended query and unique 64-bit request ID.
  5973. Additionally, the client may send any number of
  5974. @code{struct GNUNET_DHT_ClientGetResultSeenMessage}s to notify the
  5975. service about results that the client is already aware of.
  5976. These messages consist of the key, the unique 64-bit ID of the request,
  5977. and an arbitrary number of hash codes over the blocks that the client is
  5978. already aware of. As messages are restricted to 64k, a client that
  5979. already knows more than about a thousand blocks may need to send
  5980. several of these messages. Naturally, the client should transmit these
  5981. messages as quickly as possible after the original GET request such that
  5982. the DHT can filter those results in the network early on. Naturally, as
  5983. these messages are sent after the original request, it is conceivable
  5984. that the DHT service may return blocks that match those already known
  5985. to the client anyway.
  5986. In response to a GET request, the service will send @code{struct
  5987. GNUNET_DHT_ClientResultMessage}s to the client. These messages contain the
  5988. block type, expiration, key, unique ID of the request and of course the
  5989. value (a block). Depending on the options set for the respective
  5990. operations, the replies may also contain the path the GET and/or the PUT
  5991. took through the network.
  5992. A client can stop receiving replies either by disconnecting or by sending
  5993. a @code{struct GNUNET_DHT_ClientGetStopMessage} which must contain the
  5994. key and the 64-bit unique ID of the original request. Using an
  5995. explicit "stop" message is more common as this allows a client to run
  5996. many concurrent GET operations over the same connection with the DHT
  5997. service --- and to stop them individually.
  5998. @node Monitoring the DHT
  5999. @subsubsection Monitoring the DHT
  6000. To begin monitoring, the client sends a
  6001. @code{struct GNUNET_DHT_MonitorStartStop} message to the DHT service.
  6002. In this message, flags can be set to enable (or disable) monitoring of
  6003. GET, PUT and RESULT messages that pass through a peer. The message can
  6004. also restrict monitoring to a particular block type or a particular key.
  6005. Once monitoring is enabled, the DHT service will notify the client about
  6006. any matching event using @code{struct GNUNET_DHT_MonitorGetMessage}s for
  6007. GET events, @code{struct GNUNET_DHT_MonitorPutMessage} for PUT events
  6008. and @code{struct GNUNET_DHT_MonitorGetRespMessage} for RESULTs. Each of
  6009. these messages contains all of the information about the event.
  6010. @node The DHT Peer-to-Peer Protocol
  6011. @subsection The DHT Peer-to-Peer Protocol
  6012. @menu
  6013. * Routing GETs or PUTs::
  6014. * PUTting data into the DHT2::
  6015. * GETting data from the DHT2::
  6016. @end menu
  6017. @node Routing GETs or PUTs
  6018. @subsubsection Routing GETs or PUTs
  6019. When routing GETs or PUTs, the DHT service selects a suitable subset of
  6020. neighbours for forwarding. The exact number of neighbours can be zero or
  6021. more and depends on the hop counter of the query (initially zero) in
  6022. relation to the (log of) the network size estimate, the desired
  6023. replication level and the peer's connectivity.
  6024. Depending on the hop counter and our network size estimate, the selection
  6025. of the peers maybe randomized or by proximity to the key.
  6026. Furthermore, requests include a set of peers that a request has already
  6027. traversed; those peers are also excluded from the selection.
  6028. @node PUTting data into the DHT2
  6029. @subsubsection PUTting data into the DHT2
  6030. To PUT data into the DHT, the service sends a @code{struct PeerPutMessage}
  6031. of type @code{GNUNET_MESSAGE_TYPE_DHT_P2P_PUT} to the respective
  6032. neighbour.
  6033. In addition to the usual information about the content (type, routing
  6034. options, desired replication level for the content, expiration time, key
  6035. and value), the message contains a fixed-size Bloom filter with
  6036. information about which peers (may) have already seen this request.
  6037. This Bloom filter is used to ensure that DHT messages never loop back to
  6038. a peer that has already processed the request.
  6039. Additionally, the message includes the current hop counter and, depending
  6040. on the routing options, the message may include the full path that the
  6041. message has taken so far.
  6042. The Bloom filter should already contain the identity of the previous hop;
  6043. however, the path should not include the identity of the previous hop and
  6044. the receiver should append the identity of the sender to the path, not
  6045. its own identity (this is done to reduce bandwidth).
  6046. @node GETting data from the DHT2
  6047. @subsubsection GETting data from the DHT2
  6048. A peer can search the DHT by sending @code{struct PeerGetMessage}s of type
  6049. @code{GNUNET_MESSAGE_TYPE_DHT_P2P_GET} to other peers. In addition to the
  6050. usual information about the request (type, routing options, desired
  6051. replication level for the request, the key and the extended query), a GET
  6052. request also contains a hop counter, a Bloom filter over the peers
  6053. that have processed the request already and depending on the routing
  6054. options the full path traversed by the GET.
  6055. Finally, a GET request includes a variable-size second Bloom filter and a
  6056. so-called Bloom filter mutator value which together indicate which
  6057. replies the sender has already seen. During the lookup, each block that
  6058. matches they block type, key and extended query is additionally subjected
  6059. to a test against this Bloom filter.
  6060. The block plugin is expected to take the hash of the block and combine it
  6061. with the mutator value and check if the result is not yet in the Bloom
  6062. filter. The originator of the query will from time to time modify the
  6063. mutator to (eventually) allow false-positives filtered by the Bloom filter
  6064. to be returned.
  6065. Peers that receive a GET request perform a local lookup (depending on
  6066. their proximity to the key and the query options) and forward the request
  6067. to other peers.
  6068. They then remember the request (including the Bloom filter for blocking
  6069. duplicate results) and when they obtain a matching, non-filtered response
  6070. a @code{struct PeerResultMessage} of type
  6071. @code{GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT} is forwarded to the previous
  6072. hop.
  6073. Whenever a result is forwarded, the block plugin is used to update the
  6074. Bloom filter accordingly, to ensure that the same result is never
  6075. forwarded more than once.
  6076. The DHT service may also cache forwarded results locally if the
  6077. "CACHE_RESULTS" option is set to "YES" in the configuration.
  6078. @cindex GNS
  6079. @cindex GNU Name System
  6080. @node GNU Name System (GNS)
  6081. @section GNU Name System (GNS)
  6082. The GNU Name System (GNS) is a decentralized database that enables users
  6083. to securely resolve names to values.
  6084. Names can be used to identify other users (for example, in social
  6085. networking), or network services (for example, VPN services running at a
  6086. peer in GNUnet, or purely IP-based services on the Internet).
  6087. Users interact with GNS by typing in a hostname that ends in a
  6088. top-level domain that is configured in the ``GNS'' section, matches
  6089. an identity of the user or ends in a Base32-encoded public key.
  6090. Videos giving an overview of most of the GNS and the motivations behind
  6091. it is available here and here.
  6092. The remainder of this chapter targets developers that are familiar with
  6093. high level concepts of GNS as presented in these talks.
  6094. @c TODO: Add links to here and here and to these.
  6095. GNS-aware applications should use the GNS resolver to obtain the
  6096. respective records that are stored under that name in GNS.
  6097. Each record consists of a type, value, expiration time and flags.
  6098. The type specifies the format of the value. Types below 65536 correspond
  6099. to DNS record types, larger values are used for GNS-specific records.
  6100. Applications can define new GNS record types by reserving a number and
  6101. implementing a plugin (which mostly needs to convert the binary value
  6102. representation to a human-readable text format and vice-versa).
  6103. The expiration time specifies how long the record is to be valid.
  6104. The GNS API ensures that applications are only given non-expired values.
  6105. The flags are typically irrelevant for applications, as GNS uses them
  6106. internally to control visibility and validity of records.
  6107. Records are stored along with a signature.
  6108. The signature is generated using the private key of the authoritative
  6109. zone. This allows any GNS resolver to verify the correctness of a
  6110. name-value mapping.
  6111. Internally, GNS uses the NAMECACHE to cache information obtained from
  6112. other users, the NAMESTORE to store information specific to the local
  6113. users, and the DHT to exchange data between users.
  6114. A plugin API is used to enable applications to define new GNS
  6115. record types.
  6116. @menu
  6117. * libgnunetgns::
  6118. * libgnunetgnsrecord::
  6119. * GNS plugins::
  6120. * The GNS Client-Service Protocol::
  6121. * Hijacking the DNS-Traffic using gnunet-service-dns::
  6122. * Serving DNS lookups via GNS on W32::
  6123. * Importing DNS Zones into GNS::
  6124. @end menu
  6125. @node libgnunetgns
  6126. @subsection libgnunetgns
  6127. The GNS API itself is extremely simple. Clients first connect to the
  6128. GNS service using @code{GNUNET_GNS_connect}.
  6129. They can then perform lookups using @code{GNUNET_GNS_lookup} or cancel
  6130. pending lookups using @code{GNUNET_GNS_lookup_cancel}.
  6131. Once finished, clients disconnect using @code{GNUNET_GNS_disconnect}.
  6132. @menu
  6133. * Looking up records::
  6134. * Accessing the records::
  6135. * Creating records::
  6136. * Future work::
  6137. @end menu
  6138. @node Looking up records
  6139. @subsubsection Looking up records
  6140. @code{GNUNET_GNS_lookup} takes a number of arguments:
  6141. @table @asis
  6142. @item handle This is simply the GNS connection handle from
  6143. @code{GNUNET_GNS_connect}.
  6144. @item name The client needs to specify the name to
  6145. be resolved. This can be any valid DNS or GNS hostname.
  6146. @item zone The client
  6147. needs to specify the public key of the GNS zone against which the
  6148. resolution should be done.
  6149. Note that a key must be provided, the client should
  6150. look up plausible values using its configuration,
  6151. the identity service and by attempting to interpret the
  6152. TLD as a base32-encoded public key.
  6153. @item type This is the desired GNS or DNS record type
  6154. to look for. While all records for the given name will be returned, this
  6155. can be important if the client wants to resolve record types that
  6156. themselves delegate resolution, such as CNAME, PKEY or GNS2DNS.
  6157. Resolving a record of any of these types will only work if the respective
  6158. record type is specified in the request, as the GNS resolver will
  6159. otherwise follow the delegation and return the records from the
  6160. respective destination, instead of the delegating record.
  6161. @item only_cached This argument should typically be set to
  6162. @code{GNUNET_NO}. Setting it to @code{GNUNET_YES} disables resolution via
  6163. the overlay network.
  6164. @item shorten_zone_key If GNS encounters new names during resolution,
  6165. their respective zones can automatically be learned and added to the
  6166. "shorten zone". If this is desired, clients must pass the private key of
  6167. the shorten zone. If NULL is passed, shortening is disabled.
  6168. @item proc This argument identifies
  6169. the function to call with the result. It is given proc_cls, the number of
  6170. records found (possibly zero) and the array of the records as arguments.
  6171. proc will only be called once. After proc,> has been called, the lookup
  6172. must no longer be canceled.
  6173. @item proc_cls The closure for proc.
  6174. @end table
  6175. @node Accessing the records
  6176. @subsubsection Accessing the records
  6177. The @code{libgnunetgnsrecord} library provides an API to manipulate the
  6178. GNS record array that is given to proc. In particular, it offers
  6179. functions such as converting record values to human-readable
  6180. strings (and back). However, most @code{libgnunetgnsrecord} functions are
  6181. not interesting to GNS client applications.
  6182. For DNS records, the @code{libgnunetdnsparser} library provides
  6183. functions for parsing (and serializing) common types of DNS records.
  6184. @node Creating records
  6185. @subsubsection Creating records
  6186. Creating GNS records is typically done by building the respective record
  6187. information (possibly with the help of @code{libgnunetgnsrecord} and
  6188. @code{libgnunetdnsparser}) and then using the @code{libgnunetnamestore} to
  6189. publish the information. The GNS API is not involved in this
  6190. operation.
  6191. @node Future work
  6192. @subsubsection Future work
  6193. In the future, we want to expand @code{libgnunetgns} to allow
  6194. applications to observe shortening operations performed during GNS
  6195. resolution, for example so that users can receive visual feedback when
  6196. this happens.
  6197. @node libgnunetgnsrecord
  6198. @subsection libgnunetgnsrecord
  6199. The @code{libgnunetgnsrecord} library is used to manipulate GNS
  6200. records (in plaintext or in their encrypted format).
  6201. Applications mostly interact with @code{libgnunetgnsrecord} by using the
  6202. functions to convert GNS record values to strings or vice-versa, or to
  6203. lookup a GNS record type number by name (or vice-versa).
  6204. The library also provides various other functions that are mostly
  6205. used internally within GNS, such as converting keys to names, checking for
  6206. expiration, encrypting GNS records to GNS blocks, verifying GNS block
  6207. signatures and decrypting GNS records from GNS blocks.
  6208. We will now discuss the four commonly used functions of the API.@
  6209. @code{libgnunetgnsrecord} does not perform these operations itself,
  6210. but instead uses plugins to perform the operation.
  6211. GNUnet includes plugins to support common DNS record types as well as
  6212. standard GNS record types.
  6213. @menu
  6214. * Value handling::
  6215. * Type handling::
  6216. @end menu
  6217. @node Value handling
  6218. @subsubsection Value handling
  6219. @code{GNUNET_GNSRECORD_value_to_string} can be used to convert
  6220. the (binary) representation of a GNS record value to a human readable,
  6221. 0-terminated UTF-8 string.
  6222. NULL is returned if the specified record type is not supported by any
  6223. available plugin.
  6224. @code{GNUNET_GNSRECORD_string_to_value} can be used to try to convert a
  6225. human readable string to the respective (binary) representation of
  6226. a GNS record value.
  6227. @node Type handling
  6228. @subsubsection Type handling
  6229. @code{GNUNET_GNSRECORD_typename_to_number} can be used to obtain the
  6230. numeric value associated with a given typename. For example, given the
  6231. typename "A" (for DNS A reocrds), the function will return the number 1.
  6232. A list of common DNS record types is
  6233. @uref{http://en.wikipedia.org/wiki/List_of_DNS_record_types, here}.
  6234. Note that not all DNS record types are supported by GNUnet GNSRECORD
  6235. plugins at this time.
  6236. @code{GNUNET_GNSRECORD_number_to_typename} can be used to obtain the
  6237. typename associated with a given numeric value.
  6238. For example, given the type number 1, the function will return the
  6239. typename "A".
  6240. @node GNS plugins
  6241. @subsection GNS plugins
  6242. Adding a new GNS record type typically involves writing (or extending) a
  6243. GNSRECORD plugin. The plugin needs to implement the
  6244. @code{gnunet_gnsrecord_plugin.h} API which provides basic functions that
  6245. are needed by GNSRECORD to convert typenames and values of the respective
  6246. record type to strings (and back).
  6247. These gnsrecord plugins are typically implemented within their respective
  6248. subsystems.
  6249. Examples for such plugins can be found in the GNSRECORD, GNS and
  6250. CONVERSATION subsystems.
  6251. The @code{libgnunetgnsrecord} library is then used to locate, load and
  6252. query the appropriate gnsrecord plugin.
  6253. Which plugin is appropriate is determined by the record type (which is
  6254. just a 32-bit integer). The @code{libgnunetgnsrecord} library loads all
  6255. block plugins that are installed at the local peer and forwards the
  6256. application request to the plugins. If the record type is not
  6257. supported by the plugin, it should simply return an error code.
  6258. The central functions of the block APIs (plugin and main library) are the
  6259. same four functions for converting between values and strings, and
  6260. typenames and numbers documented in the previous subsection.
  6261. @node The GNS Client-Service Protocol
  6262. @subsection The GNS Client-Service Protocol
  6263. The GNS client-service protocol consists of two simple messages, the
  6264. @code{LOOKUP} message and the @code{LOOKUP_RESULT}. Each @code{LOOKUP}
  6265. message contains a unique 32-bit identifier, which will be included in the
  6266. corresponding response. Thus, clients can send many lookup requests in
  6267. parallel and receive responses out-of-order.
  6268. A @code{LOOKUP} request also includes the public key of the GNS zone,
  6269. the desired record type and fields specifying whether shortening is
  6270. enabled or networking is disabled. Finally, the @code{LOOKUP} message
  6271. includes the name to be resolved.
  6272. The response includes the number of records and the records themselves
  6273. in the format created by @code{GNUNET_GNSRECORD_records_serialize}.
  6274. They can thus be deserialized using
  6275. @code{GNUNET_GNSRECORD_records_deserialize}.
  6276. @node Hijacking the DNS-Traffic using gnunet-service-dns
  6277. @subsection Hijacking the DNS-Traffic using gnunet-service-dns
  6278. This section documents how the gnunet-service-dns (and the
  6279. gnunet-helper-dns) intercepts DNS queries from the local system.
  6280. This is merely one method for how we can obtain GNS queries.
  6281. It is also possible to change @code{resolv.conf} to point to a machine
  6282. running @code{gnunet-dns2gns} or to modify libc's name system switch
  6283. (NSS) configuration to include a GNS resolution plugin.
  6284. The method described in this chapter is more of a last-ditch catch-all
  6285. approach.
  6286. @code{gnunet-service-dns} enables intercepting DNS traffic using policy
  6287. based routing.
  6288. We MARK every outgoing DNS-packet if it was not sent by our application.
  6289. Using a second routing table in the Linux kernel these marked packets are
  6290. then routed through our virtual network interface and can thus be
  6291. captured unchanged.
  6292. Our application then reads the query and decides how to handle it.
  6293. If the query can be addressed via GNS, it is passed to
  6294. @code{gnunet-service-gns} and resolved internally using GNS.
  6295. In the future, a reverse query for an address of the configured virtual
  6296. network could be answered with records kept about previous forward
  6297. queries.
  6298. Queries that are not hijacked by some application using the DNS service
  6299. will be sent to the original recipient.
  6300. The answer to the query will always be sent back through the virtual
  6301. interface with the original nameserver as source address.
  6302. @menu
  6303. * Network Setup Details::
  6304. @end menu
  6305. @node Network Setup Details
  6306. @subsubsection Network Setup Details
  6307. The DNS interceptor adds the following rules to the Linux kernel:
  6308. @example
  6309. iptables -t mangle -I OUTPUT 1 -p udp --sport $LOCALPORT --dport 53 \
  6310. -j ACCEPT iptables -t mangle -I OUTPUT 2 -p udp --dport 53 -j MARK \
  6311. --set-mark 3 ip rule add fwmark 3 table2 ip route add default via \
  6312. $VIRTUALDNS table2
  6313. @end example
  6314. @c FIXME: Rewrite to reflect display which is no longer content by line
  6315. @c FIXME: due to the < 74 characters limit.
  6316. Line 1 makes sure that all packets coming from a port our application
  6317. opened beforehand (@code{$LOCALPORT}) will be routed normally.
  6318. Line 2 marks every other packet to a DNS-Server with mark 3 (chosen
  6319. arbitrarily). The third line adds a routing policy based on this mark
  6320. 3 via the routing table.
  6321. @node Serving DNS lookups via GNS on W32
  6322. @subsection Serving DNS lookups via GNS on W32
  6323. This section documents how the libw32nsp (and
  6324. gnunet-gns-helper-service-w32) do DNS resolutions of DNS queries on the
  6325. local system. This only applies to GNUnet running on W32.
  6326. W32 has a concept of "Namespaces" and "Namespace providers".
  6327. These are used to present various name systems to applications in a
  6328. generic way.
  6329. Namespaces include DNS, mDNS, NLA and others. For each namespace any
  6330. number of providers could be registered, and they are queried in an order
  6331. of priority (which is adjustable).
  6332. Applications can resolve names by using WSALookupService*() family of
  6333. functions.
  6334. However, these are WSA-only facilities. Common BSD socket functions for
  6335. namespace resolutions are gethostbyname and getaddrinfo (among others).
  6336. These functions are implemented internally (by default - by mswsock,
  6337. which also implements the default DNS provider) as wrappers around
  6338. WSALookupService*() functions (see "Sample Code for a Service Provider"
  6339. on MSDN).
  6340. On W32 GNUnet builds a libw32nsp - a namespace provider, which can then be
  6341. installed into the system by using w32nsp-install (and uninstalled by
  6342. w32nsp-uninstall), as described in "Installation Handbook".
  6343. libw32nsp is very simple and has almost no dependencies. As a response to
  6344. NSPLookupServiceBegin(), it only checks that the provider GUID passed to
  6345. it by the caller matches GNUnet DNS Provider GUID,
  6346. then connects to
  6347. gnunet-gns-helper-service-w32 at 127.0.0.1:5353 (hardcoded) and sends the
  6348. name resolution request there, returning the connected socket to the
  6349. caller.
  6350. When the caller invokes NSPLookupServiceNext(), libw32nsp reads a
  6351. completely formed reply from that socket, unmarshalls it, then gives
  6352. it back to the caller.
  6353. At the moment gnunet-gns-helper-service-w32 is implemented to ever give
  6354. only one reply, and subsequent calls to NSPLookupServiceNext() will fail
  6355. with WSA_NODATA (first call to NSPLookupServiceNext() might also fail if
  6356. GNS failed to find the name, or there was an error connecting to it).
  6357. gnunet-gns-helper-service-w32 does most of the processing:
  6358. @itemize @bullet
  6359. @item Maintains a connection to GNS.
  6360. @item Reads GNS config and loads appropriate keys.
  6361. @item Checks service GUID and decides on the type of record to look up,
  6362. refusing to make a lookup outright when unsupported service GUID is
  6363. passed.
  6364. @item Launches the lookup
  6365. @end itemize
  6366. When lookup result arrives, gnunet-gns-helper-service-w32 forms a complete
  6367. reply (including filling a WSAQUERYSETW structure and, possibly, a binary
  6368. blob with a hostent structure for gethostbyname() client), marshalls it,
  6369. and sends it back to libw32nsp. If no records were found, it sends an
  6370. empty header.
  6371. This works for most normal applications that use gethostbyname() or
  6372. getaddrinfo() to resolve names, but fails to do anything with
  6373. applications that use alternative means of resolving names (such as
  6374. sending queries to a DNS server directly by themselves).
  6375. This includes some of well known utilities, like "ping" and "nslookup".
  6376. @node Importing DNS Zones into GNS
  6377. @subsection Importing DNS Zones into GNS
  6378. This section discusses the challenges and problems faced when writing the
  6379. Ascension tool. It also takes a look at possible improvements in the
  6380. future.
  6381. Consider the following diagram that shows the workflow of Ascension:
  6382. @image{images/ascension_ssd,6in,,Ascensions workflow}
  6383. Further the interaction between components of GNUnet are shown in the diagram
  6384. below:
  6385. @center @image{images/ascension_interaction,,6in,Ascensions workflow}
  6386. @menu
  6387. * Conversions between DNS and GNS::
  6388. * DNS Zone Size::
  6389. * Performance::
  6390. @end menu
  6391. @cindex DNS Conversion
  6392. @node Conversions between DNS and GNS
  6393. @subsubsection Conversions between DNS and GNS
  6394. The differences between the two name systems lies in the details and is not
  6395. always transparent. For instance an SRV record is converted to a BOX record
  6396. which is unique to GNS.
  6397. This is done by converting to a BOX record from an existing SRV record:
  6398. @example
  6399. # SRV
  6400. # _service._proto.name. TTL class SRV priority weight port target
  6401. _sip._tcp.example.com. 14000 IN SRV 0 0 5060 www.example.com.
  6402. # BOX
  6403. # TTL BOX flags port protocol recordtype priority weight port target
  6404. 14000 BOX n 5060 6 33 0 0 5060 www.example.com
  6405. @end example
  6406. Other records that need to undergo such transformation is the MX record type,
  6407. as well as the SOA record type.
  6408. Transformation of a SOA record into GNS works as described in the
  6409. following example. Very important to note are the rname and mname keys.
  6410. @example
  6411. # BIND syntax for a clean SOA record
  6412. @ IN SOA master.example.com. hostmaster.example.com. (
  6413. 2017030300 ; serial
  6414. 3600 ; refresh
  6415. 1800 ; retry
  6416. 604800 ; expire
  6417. 600 ) ; ttl
  6418. # Recordline for adding the record
  6419. $ gnunet-namestore -z example.com -a -n @ -t SOA -V \
  6420. rname=master.example.com mname=hostmaster.example.com \
  6421. 2017030300,3600,1800,604800,600 -e 7200s
  6422. @end example
  6423. The transformation of MX records is done in a simple way.
  6424. @example
  6425. # mail.example.com. 3600 IN MX 10 mail.example.com.
  6426. $ gnunet-namestore -z example.com -n mail -R 3600 MX n 10,mail
  6427. @end example
  6428. Finally, one of the biggest struggling points were the NS records that are
  6429. found in top level domain zones. The intended behaviour for those is to add
  6430. GNS2DNS records for those so that gnunet-gns can resolve records for those
  6431. domains on its own. Those require the values from DNS GLUE records, provided
  6432. they are within the same zone.
  6433. The following two examples show one record with a GLUE record and the other one
  6434. does not have a GLUE record. This takes place in the 'com' TLD.
  6435. @example
  6436. # ns1.example.com 86400 IN A 127.0.0.1
  6437. # example.com 86400 IN NS ns1.example.com.
  6438. $ gnunet-namestore -z com -n example -R 86400 GNS2DNS n \
  6439. example.com@@127.0.0.1
  6440. # example.com 86400 IN NS ns1.example.org.
  6441. $ gnunet-namestore -z com -n example -R 86400 GNS2DNS n \
  6442. example.com@@ns1.example.org
  6443. @end example
  6444. As you can see, one of the GNS2DNS records has an IP address listed and the
  6445. other one a DNS name. For the first one there is a GLUE record to do the
  6446. translation directly and the second one will issue another DNS query to figure
  6447. out the IP of ns1.example.org.
  6448. A solution was found by creating a hierarchical zone structure in GNS and linking
  6449. the zones using PKEY records to one another. This allows the resolution of the
  6450. name servers to work within GNS while not taking control over unwanted zones.
  6451. Currently the following record types are supported:
  6452. @itemize @bullet
  6453. @item A
  6454. @item AAAA
  6455. @item CNAME
  6456. @item MX
  6457. @item NS
  6458. @item SRV
  6459. @item TXT
  6460. @end itemize
  6461. This is not due to technical limitations but rather a practical ones. The
  6462. problem occurs with DNSSEC enabled DNS zones. As records within those zones are
  6463. signed periodically, and every new signature is an update to the zone, there are
  6464. many revisions of zones. This results in a problem with bigger zones as there
  6465. are lots of records that have been signed again but no major changes. Also
  6466. trying to add records that are unknown that require a different format take time
  6467. as they cause a CLI call of the namestore. Furthermore certain record types
  6468. need transformation into a GNS compatible format which, depending on the record
  6469. type, takes more time.
  6470. Further a blacklist was added to drop for instance DNSSEC related records. Also
  6471. if a record type is neither in the white list nor the blacklist it is considered
  6472. as a loss of data and a message is shown to the user. This helps with
  6473. transparency and also with contributing, as the not supported record types can
  6474. then be added accordingly.
  6475. @node DNS Zone Size
  6476. @subsubsection DNS Zone Size
  6477. Another very big problem exists with very large zones. When migrating a small
  6478. zone the delay between adding of records and their expiry is negligible. However
  6479. when working with big zones that easily have more than a few million records
  6480. this delay becomes a problem.
  6481. Records will start to expire well before the zone has finished migrating. This
  6482. is usually not a problem but can cause a high CPU load when a peer is restarted
  6483. and the records have expired.
  6484. A good solution has not been found yet. One of the idea that floated around was
  6485. that the records should be added with the s (shadow) flag to keep the records
  6486. resolvable even if they expired. However this would introduce the problem of how
  6487. to detect if a record has been removed from the zone and would require deletion
  6488. of said record(s).
  6489. Another problem that still persists is how to refresh records. Expired records
  6490. are still displayed when calling gnunet-namestore but do not resolve with
  6491. gnunet-gns. Zonemaster will sign the expired records again and make sure that
  6492. the records are still valid. With a recent change this was fixed as gnunet-gns
  6493. to improve the suffix lookup which allows for a fast lookup even with thousands
  6494. of local egos.
  6495. Currently the pace of adding records in general is around 10 records per second.
  6496. Crypto is the upper limit for adding of records. The performance of your machine
  6497. can be tested with the perf_crypto_* tools. There is still a big discrepancy
  6498. between the pace of Ascension and the theoretical limit.
  6499. A performance metric for measuring improvements has not yet been implemented in
  6500. Ascension.
  6501. @node Performance
  6502. @subsubsection Performance
  6503. The performance when migrating a zone using the Ascension tool is limited by a
  6504. handful of factors. First of all ascension is written in Python3 and calls the
  6505. CLI tools of GNUnet. This is comparable to a fork and exec call which costs a
  6506. few CPU cycles. Furthermore all the records that are added to the same
  6507. label are signed using the zones private key. This signing operation is very
  6508. resource heavy and was optimized during development by adding the '-R'
  6509. (Recordline) option to gnunet-namestore which allows to specify multiple records
  6510. using the CLI tool. Assuming that in a TLD zone every domain has at least two
  6511. name servers this halves the amount of signatures needed.
  6512. Another improvement that could be made is with the addition of multiple threads
  6513. or using asynchronous subprocesses when opening the GNUnet CLI tools. This could
  6514. be implemented by simply creating more workers in the program but performance
  6515. improvements were not tested.
  6516. Ascension was tested using different hardware and database backends. Performance
  6517. differences between SQLite and postgresql are marginal and almost non existent.
  6518. What did make a huge impact on record adding performance was the storage medium.
  6519. On a traditional mechanical hard drive adding of records were slow compared to a
  6520. solid state disk.
  6521. In conclusion there are many bottlenecks still around in the program, namely the
  6522. single threaded implementation and inefficient, sequential calls of
  6523. gnunet-namestore. In the future a solution that uses the C API would be cleaner
  6524. and better.
  6525. @cindex GNS Namecache
  6526. @node GNS Namecache
  6527. @section GNS Namecache
  6528. The NAMECACHE subsystem is responsible for caching (encrypted) resolution
  6529. results of the GNU Name System (GNS). GNS makes zone information available
  6530. to other users via the DHT. However, as accessing the DHT for every
  6531. lookup is expensive (and as the DHT's local cache is lost whenever the
  6532. peer is restarted), GNS uses the NAMECACHE as a more persistent cache for
  6533. DHT lookups.
  6534. Thus, instead of always looking up every name in the DHT, GNS first
  6535. checks if the result is already available locally in the NAMECACHE.
  6536. Only if there is no result in the NAMECACHE, GNS queries the DHT.
  6537. The NAMECACHE stores data in the same (encrypted) format as the DHT.
  6538. It thus makes no sense to iterate over all items in the
  6539. NAMECACHE --- the NAMECACHE does not have a way to provide the keys
  6540. required to decrypt the entries.
  6541. Blocks in the NAMECACHE share the same expiration mechanism as blocks in
  6542. the DHT --- the block expires wheneever any of the records in
  6543. the (encrypted) block expires.
  6544. The expiration time of the block is the only information stored in
  6545. plaintext. The NAMECACHE service internally performs all of the required
  6546. work to expire blocks, clients do not have to worry about this.
  6547. Also, given that NAMECACHE stores only GNS blocks that local users
  6548. requested, there is no configuration option to limit the size of the
  6549. NAMECACHE. It is assumed to be always small enough (a few MB) to fit on
  6550. the drive.
  6551. The NAMECACHE supports the use of different database backends via a
  6552. plugin API.
  6553. @menu
  6554. * libgnunetnamecache::
  6555. * The NAMECACHE Client-Service Protocol::
  6556. * The NAMECACHE Plugin API::
  6557. @end menu
  6558. @node libgnunetnamecache
  6559. @subsection libgnunetnamecache
  6560. The NAMECACHE API consists of five simple functions. First, there is
  6561. @code{GNUNET_NAMECACHE_connect} to connect to the NAMECACHE service.
  6562. This returns the handle required for all other operations on the
  6563. NAMECACHE. Using @code{GNUNET_NAMECACHE_block_cache} clients can insert a
  6564. block into the cache.
  6565. @code{GNUNET_NAMECACHE_lookup_block} can be used to lookup blocks that
  6566. were stored in the NAMECACHE. Both operations can be canceled using
  6567. @code{GNUNET_NAMECACHE_cancel}. Note that canceling a
  6568. @code{GNUNET_NAMECACHE_block_cache} operation can result in the block
  6569. being stored in the NAMECACHE --- or not. Cancellation primarily ensures
  6570. that the continuation function with the result of the operation will no
  6571. longer be invoked.
  6572. Finally, @code{GNUNET_NAMECACHE_disconnect} closes the connection to the
  6573. NAMECACHE.
  6574. The maximum size of a block that can be stored in the NAMECACHE is
  6575. @code{GNUNET_NAMECACHE_MAX_VALUE_SIZE}, which is defined to be 63 kB.
  6576. @node The NAMECACHE Client-Service Protocol
  6577. @subsection The NAMECACHE Client-Service Protocol
  6578. All messages in the NAMECACHE IPC protocol start with the
  6579. @code{struct GNUNET_NAMECACHE_Header} which adds a request
  6580. ID (32-bit integer) to the standard message header.
  6581. The request ID is used to match requests with the
  6582. respective responses from the NAMECACHE, as they are allowed to happen
  6583. out-of-order.
  6584. @menu
  6585. * Lookup::
  6586. * Store::
  6587. @end menu
  6588. @node Lookup
  6589. @subsubsection Lookup
  6590. The @code{struct LookupBlockMessage} is used to lookup a block stored in
  6591. the cache.
  6592. It contains the query hash. The NAMECACHE always responds with a
  6593. @code{struct LookupBlockResponseMessage}. If the NAMECACHE has no
  6594. response, it sets the expiration time in the response to zero.
  6595. Otherwise, the response is expected to contain the expiration time, the
  6596. ECDSA signature, the derived key and the (variable-size) encrypted data
  6597. of the block.
  6598. @node Store
  6599. @subsubsection Store
  6600. The @code{struct BlockCacheMessage} is used to cache a block in the
  6601. NAMECACHE.
  6602. It has the same structure as the @code{struct LookupBlockResponseMessage}.
  6603. The service responds with a @code{struct BlockCacheResponseMessage} which
  6604. contains the result of the operation (success or failure).
  6605. In the future, we might want to make it possible to provide an error
  6606. message as well.
  6607. @node The NAMECACHE Plugin API
  6608. @subsection The NAMECACHE Plugin API
  6609. The NAMECACHE plugin API consists of two functions, @code{cache_block} to
  6610. store a block in the database, and @code{lookup_block} to lookup a block
  6611. in the database.
  6612. @menu
  6613. * Lookup2::
  6614. * Store2::
  6615. @end menu
  6616. @node Lookup2
  6617. @subsubsection Lookup2
  6618. The @code{lookup_block} function is expected to return at most one block
  6619. to the iterator, and return @code{GNUNET_NO} if there were no non-expired
  6620. results.
  6621. If there are multiple non-expired results in the cache, the lookup is
  6622. supposed to return the result with the largest expiration time.
  6623. @node Store2
  6624. @subsubsection Store2
  6625. The @code{cache_block} function is expected to try to store the block in
  6626. the database, and return @code{GNUNET_SYSERR} if this was not possible
  6627. for any reason.
  6628. Furthermore, @code{cache_block} is expected to implicitly perform cache
  6629. maintenance and purge blocks from the cache that have expired. Note that
  6630. @code{cache_block} might encounter the case where the database already has
  6631. another block stored under the same key. In this case, the plugin must
  6632. ensure that the block with the larger expiration time is preserved.
  6633. Obviously, this can done either by simply adding new blocks and selecting
  6634. for the most recent expiration time during lookup, or by checking which
  6635. block is more recent during the store operation.
  6636. @cindex REVOCATION Subsystem
  6637. @node REVOCATION Subsystem
  6638. @section REVOCATION Subsystem
  6639. The REVOCATION subsystem is responsible for key revocation of Egos.
  6640. If a user learns that theis private key has been compromised or has lost
  6641. it, they can use the REVOCATION system to inform all of the other users
  6642. that their private key is no longer valid.
  6643. The subsystem thus includes ways to query for the validity of keys and to
  6644. propagate revocation messages.
  6645. @menu
  6646. * Dissemination::
  6647. * Revocation Message Design Requirements::
  6648. * libgnunetrevocation::
  6649. * The REVOCATION Client-Service Protocol::
  6650. * The REVOCATION Peer-to-Peer Protocol::
  6651. @end menu
  6652. @node Dissemination
  6653. @subsection Dissemination
  6654. When a revocation is performed, the revocation is first of all
  6655. disseminated by flooding the overlay network.
  6656. The goal is to reach every peer, so that when a peer needs to check if a
  6657. key has been revoked, this will be purely a local operation where the
  6658. peer looks at its local revocation list. Flooding the network is also the
  6659. most robust form of key revocation --- an adversary would have to control
  6660. a separator of the overlay graph to restrict the propagation of the
  6661. revocation message. Flooding is also very easy to implement --- peers that
  6662. receive a revocation message for a key that they have never seen before
  6663. simply pass the message to all of their neighbours.
  6664. Flooding can only distribute the revocation message to peers that are
  6665. online.
  6666. In order to notify peers that join the network later, the revocation
  6667. service performs efficient set reconciliation over the sets of known
  6668. revocation messages whenever two peers (that both support REVOCATION
  6669. dissemination) connect.
  6670. The SET service is used to perform this operation efficiently.
  6671. @node Revocation Message Design Requirements
  6672. @subsection Revocation Message Design Requirements
  6673. However, flooding is also quite costly, creating O(|E|) messages on a
  6674. network with |E| edges.
  6675. Thus, revocation messages are required to contain a proof-of-work, the
  6676. result of an expensive computation (which, however, is cheap to verify).
  6677. Only peers that have expended the CPU time necessary to provide
  6678. this proof will be able to flood the network with the revocation message.
  6679. This ensures that an attacker cannot simply flood the network with
  6680. millions of revocation messages. The proof-of-work required by GNUnet is
  6681. set to take days on a typical PC to compute; if the ability to quickly
  6682. revoke a key is needed, users have the option to pre-compute revocation
  6683. messages to store off-line and use instantly after their key has expired.
  6684. Revocation messages must also be signed by the private key that is being
  6685. revoked. Thus, they can only be created while the private key is in the
  6686. possession of the respective user. This is another reason to create a
  6687. revocation message ahead of time and store it in a secure location.
  6688. @node libgnunetrevocation
  6689. @subsection libgnunetrevocation
  6690. The REVOCATION API consists of two parts, to query and to issue
  6691. revocations.
  6692. @menu
  6693. * Querying for revoked keys::
  6694. * Preparing revocations::
  6695. * Issuing revocations::
  6696. @end menu
  6697. @node Querying for revoked keys
  6698. @subsubsection Querying for revoked keys
  6699. @code{GNUNET_REVOCATION_query} is used to check if a given ECDSA public
  6700. key has been revoked.
  6701. The given callback will be invoked with the result of the check.
  6702. The query can be canceled using @code{GNUNET_REVOCATION_query_cancel} on
  6703. the return value.
  6704. @node Preparing revocations
  6705. @subsubsection Preparing revocations
  6706. It is often desirable to create a revocation record ahead-of-time and
  6707. store it in an off-line location to be used later in an emergency.
  6708. This is particularly true for GNUnet revocations, where performing the
  6709. revocation operation itself is computationally expensive and thus is
  6710. likely to take some time.
  6711. Thus, if users want the ability to perform revocations quickly in an
  6712. emergency, they must pre-compute the revocation message.
  6713. The revocation API enables this with two functions that are used to
  6714. compute the revocation message, but not trigger the actual revocation
  6715. operation.
  6716. @code{GNUNET_REVOCATION_check_pow} should be used to calculate the
  6717. proof-of-work required in the revocation message. This function takes the
  6718. public key, the required number of bits for the proof of work (which in
  6719. GNUnet is a network-wide constant) and finally a proof-of-work number as
  6720. arguments.
  6721. The function then checks if the given proof-of-work number is a valid
  6722. proof of work for the given public key. Clients preparing a revocation
  6723. are expected to call this function repeatedly (typically with a
  6724. monotonically increasing sequence of numbers of the proof-of-work number)
  6725. until a given number satisfies the check.
  6726. That number should then be saved for later use in the revocation
  6727. operation.
  6728. @code{GNUNET_REVOCATION_sign_revocation} is used to generate the
  6729. signature that is required in a revocation message.
  6730. It takes the private key that (possibly in the future) is to be revoked
  6731. and returns the signature.
  6732. The signature can again be saved to disk for later use, which will then
  6733. allow performing a revocation even without access to the private key.
  6734. @node Issuing revocations
  6735. @subsubsection Issuing revocations
  6736. Given a ECDSA public key, the signature from @code{GNUNET_REVOCATION_sign}
  6737. and the proof-of-work,
  6738. @code{GNUNET_REVOCATION_revoke} can be used to perform the
  6739. actual revocation. The given callback is called upon completion of the
  6740. operation. @code{GNUNET_REVOCATION_revoke_cancel} can be used to stop the
  6741. library from calling the continuation; however, in that case it is
  6742. undefined whether or not the revocation operation will be executed.
  6743. @node The REVOCATION Client-Service Protocol
  6744. @subsection The REVOCATION Client-Service Protocol
  6745. The REVOCATION protocol consists of four simple messages.
  6746. A @code{QueryMessage} containing a public ECDSA key is used to check if a
  6747. particular key has been revoked. The service responds with a
  6748. @code{QueryResponseMessage} which simply contains a bit that says if the
  6749. given public key is still valid, or if it has been revoked.
  6750. The second possible interaction is for a client to revoke a key by
  6751. passing a @code{RevokeMessage} to the service. The @code{RevokeMessage}
  6752. contains the ECDSA public key to be revoked, a signature by the
  6753. corresponding private key and the proof-of-work, The service responds
  6754. with a @code{RevocationResponseMessage} which can be used to indicate
  6755. that the @code{RevokeMessage} was invalid (i.e. proof of work incorrect),
  6756. or otherwise indicates that the revocation has been processed
  6757. successfully.
  6758. @node The REVOCATION Peer-to-Peer Protocol
  6759. @subsection The REVOCATION Peer-to-Peer Protocol
  6760. Revocation uses two disjoint ways to spread revocation information among
  6761. peers.
  6762. First of all, P2P gossip exchanged via CORE-level neighbours is used to
  6763. quickly spread revocations to all connected peers.
  6764. Second, whenever two peers (that both support revocations) connect,
  6765. the SET service is used to compute the union of the respective revocation
  6766. sets.
  6767. In both cases, the exchanged messages are @code{RevokeMessage}s which
  6768. contain the public key that is being revoked, a matching ECDSA signature,
  6769. and a proof-of-work.
  6770. Whenever a peer learns about a new revocation this way, it first
  6771. validates the signature and the proof-of-work, then stores it to disk
  6772. (typically to a file $GNUNET_DATA_HOME/revocation.dat) and finally
  6773. spreads the information to all directly connected neighbours.
  6774. For computing the union using the SET service, the peer with the smaller
  6775. hashed peer identity will connect (as a "client" in the two-party set
  6776. protocol) to the other peer after one second (to reduce traffic spikes
  6777. on connect) and initiate the computation of the set union.
  6778. All revocation services use a common hash to identify the SET operation
  6779. over revocation sets.
  6780. The current implementation accepts revocation set union operations from
  6781. all peers at any time; however, well-behaved peers should only initiate
  6782. this operation once after establishing a connection to a peer with a
  6783. larger hashed peer identity.
  6784. @cindex FS
  6785. @cindex FS Subsystem
  6786. @node File-sharing (FS) Subsystem
  6787. @section File-sharing (FS) Subsystem
  6788. This chapter describes the details of how the file-sharing service works.
  6789. As with all services, it is split into an API (libgnunetfs), the service
  6790. process (gnunet-service-fs) and user interface(s).
  6791. The file-sharing service uses the datastore service to store blocks and
  6792. the DHT (and indirectly datacache) for lookups for non-anonymous
  6793. file-sharing.
  6794. Furthermore, the file-sharing service uses the block library (and the
  6795. block fs plugin) for validation of DHT operations.
  6796. In contrast to many other services, libgnunetfs is rather complex since
  6797. the client library includes a large number of high-level abstractions;
  6798. this is necessary since the Fs service itself largely only operates on
  6799. the block level.
  6800. The FS library is responsible for providing a file-based abstraction to
  6801. applications, including directories, meta data, keyword search,
  6802. verification, and so on.
  6803. The method used by GNUnet to break large files into blocks and to use
  6804. keyword search is called the
  6805. "Encoding for Censorship Resistant Sharing" (ECRS).
  6806. ECRS is largely implemented in the fs library; block validation is also
  6807. reflected in the block FS plugin and the FS service.
  6808. ECRS on-demand encoding is implemented in the FS service.
  6809. NOTE: The documentation in this chapter is quite incomplete.
  6810. @menu
  6811. * Encoding for Censorship-Resistant Sharing (ECRS)::
  6812. * File-sharing persistence directory structure::
  6813. @end menu
  6814. @cindex ECRS
  6815. @cindex Encoding for Censorship-Resistant Sharing
  6816. @node Encoding for Censorship-Resistant Sharing (ECRS)
  6817. @subsection Encoding for Censorship-Resistant Sharing (ECRS)
  6818. When GNUnet shares files, it uses a content encoding that is called ECRS,
  6819. the Encoding for Censorship-Resistant Sharing.
  6820. Most of ECRS is described in the (so far unpublished) research paper
  6821. attached to this page. ECRS obsoletes the previous ESED and ESED II
  6822. encodings which were used in GNUnet before version 0.7.0.
  6823. The rest of this page assumes that the reader is familiar with the
  6824. attached paper. What follows is a description of some minor extensions
  6825. that GNUnet makes over what is described in the paper.
  6826. The reason why these extensions are not in the paper is that we felt
  6827. that they were obvious or trivial extensions to the original scheme and
  6828. thus did not warrant space in the research report.
  6829. @menu
  6830. * Namespace Advertisements::
  6831. * KSBlocks::
  6832. @end menu
  6833. @node Namespace Advertisements
  6834. @subsubsection Namespace Advertisements
  6835. @c %**FIXME: all zeroses -> ?
  6836. An @code{SBlock} with identifier all zeros is a signed
  6837. advertisement for a namespace. This special @code{SBlock} contains
  6838. metadata describing the content of the namespace.
  6839. Instead of the name of the identifier for a potential update, it contains
  6840. the identifier for the root of the namespace.
  6841. The URI should always be empty. The @code{SBlock} is signed with the
  6842. content provider's RSA private key (just like any other SBlock). Peers
  6843. can search for @code{SBlock}s in order to find out more about a namespace.
  6844. @node KSBlocks
  6845. @subsubsection KSBlocks
  6846. GNUnet implements @code{KSBlocks} which are @code{KBlocks} that, instead
  6847. of encrypting a CHK and metadata, encrypt an @code{SBlock} instead.
  6848. In other words, @code{KSBlocks} enable GNUnet to find @code{SBlocks}
  6849. using the global keyword search.
  6850. Usually the encrypted @code{SBlock} is a namespace advertisement.
  6851. The rationale behind @code{KSBlock}s and @code{SBlock}s is to enable
  6852. peers to discover namespaces via keyword searches, and, to associate
  6853. useful information with namespaces. When GNUnet finds @code{KSBlocks}
  6854. during a normal keyword search, it adds the information to an internal
  6855. list of discovered namespaces. Users looking for interesting namespaces
  6856. can then inspect this list, reducing the need for out-of-band discovery
  6857. of namespaces.
  6858. Naturally, namespaces (or more specifically, namespace advertisements) can
  6859. also be referenced from directories, but @code{KSBlock}s should make it
  6860. easier to advertise namespaces for the owner of the pseudonym since they
  6861. eliminate the need to first create a directory.
  6862. Collections are also advertised using @code{KSBlock}s.
  6863. @c https://old.gnunet.org/sites/default/files/ecrs.pdf
  6864. @node File-sharing persistence directory structure
  6865. @subsection File-sharing persistence directory structure
  6866. This section documents how the file-sharing library implements
  6867. persistence of file-sharing operations and specifically the resulting
  6868. directory structure.
  6869. This code is only active if the @code{GNUNET_FS_FLAGS_PERSISTENCE} flag
  6870. was set when calling @code{GNUNET_FS_start}.
  6871. In this case, the file-sharing library will try hard to ensure that all
  6872. major operations (searching, downloading, publishing, unindexing) are
  6873. persistent, that is, can live longer than the process itself.
  6874. More specifically, an operation is supposed to live until it is
  6875. explicitly stopped.
  6876. If @code{GNUNET_FS_stop} is called before an operation has been stopped, a
  6877. @code{SUSPEND} event is generated and then when the process calls
  6878. @code{GNUNET_FS_start} next time, a @code{RESUME} event is generated.
  6879. Additionally, even if an application crashes (segfault, SIGKILL, system
  6880. crash) and hence @code{GNUNET_FS_stop} is never called and no
  6881. @code{SUSPEND} events are generated, operations are still resumed (with
  6882. @code{RESUME} events).
  6883. This is implemented by constantly writing the current state of the
  6884. file-sharing operations to disk.
  6885. Specifically, the current state is always written to disk whenever
  6886. anything significant changes (the exception are block-wise progress in
  6887. publishing and unindexing, since those operations would be slowed down
  6888. significantly and can be resumed cheaply even without detailed
  6889. accounting).
  6890. Note that if the process crashes (or is killed) during a serialization
  6891. operation, FS does not guarantee that this specific operation is
  6892. recoverable (no strict transactional semantics, again for performance
  6893. reasons). However, all other unrelated operations should resume nicely.
  6894. Since we need to serialize the state continuously and want to recover as
  6895. much as possible even after crashing during a serialization operation,
  6896. we do not use one large file for serialization.
  6897. Instead, several directories are used for the various operations.
  6898. When @code{GNUNET_FS_start} executes, the master directories are scanned
  6899. for files describing operations to resume.
  6900. Sometimes, these operations can refer to related operations in child
  6901. directories which may also be resumed at this point.
  6902. Note that corrupted files are cleaned up automatically.
  6903. However, dangling files in child directories (those that are not
  6904. referenced by files from the master directories) are not automatically
  6905. removed.
  6906. Persistence data is kept in a directory that begins with the "STATE_DIR"
  6907. prefix from the configuration file
  6908. (by default, "$SERVICEHOME/persistence/") followed by the name of the
  6909. client as given to @code{GNUNET_FS_start} (for example, "gnunet-gtk")
  6910. followed by the actual name of the master or child directory.
  6911. The names for the master directories follow the names of the operations:
  6912. @itemize @bullet
  6913. @item "search"
  6914. @item "download"
  6915. @item "publish"
  6916. @item "unindex"
  6917. @end itemize
  6918. Each of the master directories contains names (chosen at random) for each
  6919. active top-level (master) operation.
  6920. Note that a download that is associated with a search result is not a
  6921. top-level operation.
  6922. In contrast to the master directories, the child directories are only
  6923. consulted when another operation refers to them.
  6924. For each search, a subdirectory (named after the master search
  6925. synchronization file) contains the search results.
  6926. Search results can have an associated download, which is then stored in
  6927. the general "download-child" directory.
  6928. Downloads can be recursive, in which case children are stored in
  6929. subdirectories mirroring the structure of the recursive download
  6930. (either starting in the master "download" directory or in the
  6931. "download-child" directory depending on how the download was initiated).
  6932. For publishing operations, the "publish-file" directory contains
  6933. information about the individual files and directories that are part of
  6934. the publication.
  6935. However, this directory structure is flat and does not mirror the
  6936. structure of the publishing operation.
  6937. Note that unindex operations cannot have associated child operations.
  6938. @cindex REGEX subsystem
  6939. @node REGEX Subsystem
  6940. @section REGEX Subsystem
  6941. Using the REGEX subsystem, you can discover peers that offer a particular
  6942. service using regular expressions.
  6943. The peers that offer a service specify it using a regular expressions.
  6944. Peers that want to patronize a service search using a string.
  6945. The REGEX subsystem will then use the DHT to return a set of matching
  6946. offerers to the patrons.
  6947. For the technical details, we have Max's defense talk and Max's Master's
  6948. thesis.
  6949. @c An additional publication is under preparation and available to
  6950. @c team members (in Git).
  6951. @c FIXME: Where is the file? Point to it. Assuming that it's szengel2012ms
  6952. @menu
  6953. * How to run the regex profiler::
  6954. @end menu
  6955. @node How to run the regex profiler
  6956. @subsection How to run the regex profiler
  6957. The gnunet-regex-profiler can be used to profile the usage of mesh/regex
  6958. for a given set of regular expressions and strings.
  6959. Mesh/regex allows you to announce your peer ID under a certain regex and
  6960. search for peers matching a particular regex using a string.
  6961. See @uref{https://bib.gnunet.org/full/date.html#2012_5f2, szengel2012ms} for a full
  6962. introduction.
  6963. First of all, the regex profiler uses GNUnet testbed, thus all the
  6964. implications for testbed also apply to the regex profiler
  6965. (for example you need password-less ssh login to the machines listed in
  6966. your hosts file).
  6967. @strong{Configuration}
  6968. Moreover, an appropriate configuration file is needed.
  6969. Generally you can refer to the
  6970. @file{contrib/regex_profiler_infiniband.conf} file in the sourcecode
  6971. of GNUnet for an example configuration.
  6972. In the following paragraph the important details are highlighted.
  6973. Announcing of the regular expressions is done by the
  6974. gnunet-daemon-regexprofiler, therefore you have to make sure it is
  6975. started, by adding it to the START_ON_DEMAND set of ARM:
  6976. @example
  6977. [regexprofiler]
  6978. START_ON_DEMAND = YES
  6979. @end example
  6980. @noindent
  6981. Furthermore you have to specify the location of the binary:
  6982. @example
  6983. [regexprofiler]
  6984. # Location of the gnunet-daemon-regexprofiler binary.
  6985. BINARY = /home/szengel/gnunet/src/mesh/.libs/gnunet-daemon-regexprofiler
  6986. # Regex prefix that will be applied to all regular expressions and
  6987. # search string.
  6988. REGEX_PREFIX = "GNVPN-0001-PAD"
  6989. @end example
  6990. @noindent
  6991. When running the profiler with a large scale deployment, you probably
  6992. want to reduce the workload of each peer.
  6993. Use the following options to do this.
  6994. @example
  6995. [dht]
  6996. # Force network size estimation
  6997. FORCE_NSE = 1
  6998. [dhtcache]
  6999. DATABASE = heap
  7000. # Disable RC-file for Bloom filter? (for benchmarking with limited IO
  7001. # availability)
  7002. DISABLE_BF_RC = YES
  7003. # Disable Bloom filter entirely
  7004. DISABLE_BF = YES
  7005. [nse]
  7006. # Minimize proof-of-work CPU consumption by NSE
  7007. WORKBITS = 1
  7008. @end example
  7009. @noindent
  7010. @strong{Options}
  7011. To finally run the profiler some options and the input data need to be
  7012. specified on the command line.
  7013. @example
  7014. gnunet-regex-profiler -c config-file -d log-file -n num-links \
  7015. -p path-compression-length -s search-delay -t matching-timeout \
  7016. -a num-search-strings hosts-file policy-dir search-strings-file
  7017. @end example
  7018. @noindent
  7019. Where...
  7020. @itemize @bullet
  7021. @item ... @code{config-file} means the configuration file created earlier.
  7022. @item ... @code{log-file} is the file where to write statistics output.
  7023. @item ... @code{num-links} indicates the number of random links between
  7024. started peers.
  7025. @item ... @code{path-compression-length} is the maximum path compression
  7026. length in the DFA.
  7027. @item ... @code{search-delay} time to wait between peers finished linking
  7028. and starting to match strings.
  7029. @item ... @code{matching-timeout} timeout after which to cancel the
  7030. searching.
  7031. @item ... @code{num-search-strings} number of strings in the
  7032. search-strings-file.
  7033. @item ... the @code{hosts-file} should contain a list of hosts for the
  7034. testbed, one per line in the following format:
  7035. @itemize @bullet
  7036. @item @code{user@@host_ip:port}
  7037. @end itemize
  7038. @item ... the @code{policy-dir} is a folder containing text files
  7039. containing one or more regular expressions. A peer is started for each
  7040. file in that folder and the regular expressions in the corresponding file
  7041. are announced by this peer.
  7042. @item ... the @code{search-strings-file} is a text file containing search
  7043. strings, one in each line.
  7044. @end itemize
  7045. @noindent
  7046. You can create regular expressions and search strings for every AS in the
  7047. Internet using the attached scripts. You need one of the
  7048. @uref{http://data.caida.org/datasets/routing/routeviews-prefix2as/, CAIDA routeviews prefix2as}
  7049. data files for this. Run
  7050. @example
  7051. create_regex.py <filename> <output path>
  7052. @end example
  7053. @noindent
  7054. to create the regular expressions and
  7055. @example
  7056. create_strings.py <input path> <outfile>
  7057. @end example
  7058. @noindent
  7059. to create a search strings file from the previously created
  7060. regular expressions.
  7061. @cindex REST subsystem
  7062. @node REST Subsystem
  7063. @section REST Subsystem
  7064. Using the REST subsystem, you can expose REST-based APIs or services.
  7065. The REST service is designed as a pluggable architecture.
  7066. To create a new REST endpoint, simply add a library in the form
  7067. ``plugin_rest_*''.
  7068. The REST service will automatically load all REST plugins on startup.
  7069. @strong{Configuration}
  7070. The REST service can be configured in various ways.
  7071. The reference config file can be found in
  7072. @file{src/rest/rest.conf}:
  7073. @example
  7074. [rest]
  7075. REST_PORT=7776
  7076. REST_ALLOW_HEADERS=Authorization,Accept,Content-Type
  7077. REST_ALLOW_ORIGIN=*
  7078. REST_ALLOW_CREDENTIALS=true
  7079. @end example
  7080. The port as well as
  7081. @deffn{cross-origin resource sharing} (CORS)
  7082. @end deffn
  7083. headers that are supposed to be advertised by the rest service are
  7084. configurable.
  7085. @menu
  7086. * Namespace considerations::
  7087. * Endpoint documentation::
  7088. @end menu
  7089. @node Namespace considerations
  7090. @subsection Namespace considerations
  7091. The @command{gnunet-rest-service} will load all plugins that are installed.
  7092. As such it is important that the endpoint namespaces do not clash.
  7093. For example, plugin X might expose the endpoint ``/xxx'' while plugin Y
  7094. exposes endpoint ``/xxx/yyy''.
  7095. This is a problem if plugin X is also supposed to handle a call
  7096. to ``/xxx/yyy''.
  7097. Currently the REST service will not complain or warn about such clashes,
  7098. so please make sure that endpoints are unambiguous.
  7099. @node Endpoint documentation
  7100. @subsection Endpoint documentation
  7101. This is WIP. Endpoints should be documented appropriately.
  7102. Preferably using annotations.
  7103. @cindex RPS Subsystem
  7104. @node RPS Subsystem
  7105. @section RPS Subsystem
  7106. In literature, Random Peer Sampling (RPS) refers to the problem of
  7107. reliably@footnote{"Reliable" in this context means having no bias,
  7108. neither spatial, nor temporal, nor through malicious activity.} drawing
  7109. random samples from an unstructured p2p network.
  7110. Doing so in a reliable manner is not only hard because of inherent
  7111. problems but also because of possible malicious peers that could try to
  7112. bias the selection.
  7113. It is useful for all kind of gossip protocols that require the selection
  7114. of random peers in the whole network like gathering statistics,
  7115. spreading and aggregating information in the network, load balancing and
  7116. overlay topology management.
  7117. The approach chosen in the RPS service implementation in GNUnet follows
  7118. the @uref{https://bib.gnunet.org/full/date.html\#2009_5f0, Brahms}
  7119. design.
  7120. The current state is "work in progress". There are a lot of things that
  7121. need to be done, primarily finishing the experimental evaluation and a
  7122. re-design of the API.
  7123. The abstract idea is to subscribe to connect to/start the RPS service
  7124. and request random peers that will be returned when they represent a
  7125. random selection from the whole network with high probability.
  7126. An additional feature to the original Brahms-design is the selection of
  7127. sub-groups: The GNUnet implementation of RPS enables clients to ask for
  7128. random peers from a group that is defined by a common shared secret.
  7129. (The secret could of course also be public, depending on the use-case.)
  7130. Another addition to the original protocol was made: The sampler
  7131. mechanism that was introduced in Brahms was slightly adapted and used to
  7132. actually sample the peers and returned to the client.
  7133. This is necessary as the original design only keeps peers connected to
  7134. random other peers in the network. In order to return random peers to
  7135. client requests independently random, they cannot be drawn from the
  7136. connected peers.
  7137. The adapted sampler makes sure that each request for random peers is
  7138. independent from the others.
  7139. @menu
  7140. * Brahms::
  7141. @end menu
  7142. @node Brahms
  7143. @subsection Brahms
  7144. The high-level concept of Brahms is two-fold: Combining push-pull gossip
  7145. with locally fixing a assumed bias using cryptographic min-wise
  7146. permutations.
  7147. The central data structure is the view - a peer's current local sample.
  7148. This view is used to select peers to push to and pull from.
  7149. This simple mechanism can be biased easily. For this reason Brahms
  7150. 'fixes' the bias by using the so-called sampler. A data structure that
  7151. takes a list of elements as input and outputs a random one of them
  7152. independently of the frequency in the input set. Both an element that
  7153. was put into the sampler a single time and an element that was put into
  7154. it a million times have the same probability of being the output.
  7155. This is achieved with exploiting min-wise independent
  7156. permutations. In the RPS service we use HMACs: On the initialisation of a sampler
  7157. element, a key is chosen at random. On each input the HMAC with the
  7158. random key is computed. The sampler element keeps the element with the
  7159. minimal HMAC.
  7160. In order to fix the bias in the view, a fraction of the elements in the
  7161. view are sampled through the sampler from the random stream of peer IDs.
  7162. According to the theoretical analysis of Bortnikov et al. this suffices
  7163. to keep the network connected and having random peers in the view.