gnunet-service-xdht_neighbours.c 208 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2009-2014 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file dht/gnunet-service-xdht_neighbours.c
  19. * @brief GNUnet DHT service's finger and friend table management code
  20. * @author Supriti Singh
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_block_lib.h"
  25. #include "gnunet_hello_lib.h"
  26. #include "gnunet_constants.h"
  27. #include "gnunet_protocols.h"
  28. #include "gnunet_ats_service.h"
  29. #include "gnunet_core_service.h"
  30. #include "gnunet_datacache_lib.h"
  31. #include "gnunet_transport_service.h"
  32. #include "gnunet_dht_service.h"
  33. #include "gnunet_statistics_service.h"
  34. #include "gnunet-service-xdht.h"
  35. #include "gnunet-service-xdht_clients.h"
  36. #include "gnunet-service-xdht_datacache.h"
  37. #include "gnunet-service-xdht_neighbours.h"
  38. #include "gnunet-service-xdht_routing.h"
  39. #include <fenv.h>
  40. #include "dht.h"
  41. /**
  42. * TODO:
  43. * 1. In X-Vine paper, there is no policy defined for replicating the data to
  44. * recover in case of peer failure. We can do it in Chord way. In R5N, the key
  45. * is hashed and then data is stored according to the key value generated after
  46. * hashing.
  47. */
  48. #define DEBUG(...) \
  49. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
  50. /**
  51. * Maximum possible fingers (including predecessor) of a peer
  52. */
  53. #define MAX_FINGERS 65
  54. /**
  55. * Maximum allowed number of pending messages per friend peer.
  56. */
  57. #define MAXIMUM_PENDING_PER_FRIEND 64
  58. /**
  59. * How long to wait before sending another find finger trail request
  60. */
  61. #define DHT_FIND_FINGER_TRAIL_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
  62. /**
  63. * How long to wait before sending another verify successor message.
  64. */
  65. #define DHT_SEND_VERIFY_SUCCESSOR_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
  66. /**
  67. * How long to wait before sending another verify successor message.
  68. */
  69. #define DHT_SEND_VERIFY_SUCCESSOR_RETRY_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
  70. /**
  71. * How long to wait before retrying notify successor.
  72. */
  73. #define DHT_SEND_NOTIFY_SUCCESSOR_RETRY_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
  74. /**
  75. * How long at most to wait for transmission of a request to a friend ?
  76. */
  77. #define PENDING_MESSAGE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
  78. /**
  79. * Duration for which I may remain congested.
  80. * Note: Its a static value. In future, a peer may do some analysis and calculate
  81. * congestion_timeout based on 'some' parameters.
  82. */
  83. #define CONGESTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
  84. /**
  85. * In case we don't hear back from the current successor, then we can start
  86. * verify successor.
  87. */
  88. #define WAIT_NOTIFY_CONFIRMATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 200)
  89. /**
  90. * Maximum number of trails allowed to go through a friend.
  91. */
  92. #define TRAILS_THROUGH_FRIEND_THRESHOLD 64
  93. /**
  94. * Maximum number of trails stored per finger.
  95. */
  96. #define MAXIMUM_TRAILS_PER_FINGER 4
  97. /**
  98. * Finger map index for predecessor entry in finger table.
  99. */
  100. #define PREDECESSOR_FINGER_ID 64
  101. /**
  102. * FIXME: Its use only at 3 places check if you can remove it.
  103. * To check if a finger is predecessor or not.
  104. */
  105. enum GDS_NEIGHBOURS_finger_type
  106. {
  107. GDS_FINGER_TYPE_PREDECESSOR = 1,
  108. GDS_FINGER_TYPE_NON_PREDECESSOR = 0
  109. };
  110. GNUNET_NETWORK_STRUCT_BEGIN
  111. /**
  112. * P2P PUT message
  113. */
  114. struct PeerPutMessage
  115. {
  116. /**
  117. * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_PUT
  118. */
  119. struct GNUNET_MessageHeader header;
  120. /**
  121. * Processing options
  122. */
  123. uint32_t options GNUNET_PACKED;
  124. /**
  125. * Content type.
  126. */
  127. uint32_t block_type GNUNET_PACKED;
  128. /**
  129. * Hop count
  130. */
  131. uint32_t hop_count GNUNET_PACKED;
  132. /**
  133. * Replication level for this message
  134. * In the current implementation, this value is not used.
  135. */
  136. uint32_t desired_replication_level GNUNET_PACKED;
  137. /**
  138. * Length of the PUT path that follows (if tracked).
  139. */
  140. uint32_t put_path_length GNUNET_PACKED;
  141. /**
  142. * Best known destination (could be my friend or finger) which should
  143. * get this message next.
  144. */
  145. struct GNUNET_PeerIdentity best_known_destination;
  146. /**
  147. * In case best_known_destination is a finger, then trail to reach
  148. * to that finger. Else its default value is 0.
  149. */
  150. struct GNUNET_HashCode intermediate_trail_id;
  151. /**
  152. * When does the content expire?
  153. */
  154. struct GNUNET_TIME_AbsoluteNBO expiration_time;
  155. /**
  156. * The key to store the value under.
  157. */
  158. struct GNUNET_HashCode key GNUNET_PACKED;
  159. /* put path (if tracked) */
  160. /* Payload */
  161. };
  162. /**
  163. * P2P GET message
  164. */
  165. struct PeerGetMessage
  166. {
  167. /**
  168. * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_GET
  169. */
  170. struct GNUNET_MessageHeader header;
  171. /**
  172. * Processing options
  173. */
  174. uint32_t options GNUNET_PACKED;
  175. /**
  176. * Desired content type.
  177. */
  178. uint32_t block_type GNUNET_PACKED;
  179. /**
  180. * Hop count
  181. */
  182. uint32_t hop_count GNUNET_PACKED;
  183. /**
  184. * Desired replication level for this request.
  185. * In the current implementation, this value is not used.
  186. */
  187. uint32_t desired_replication_level GNUNET_PACKED;
  188. /**
  189. * Total number of peers in get path.
  190. */
  191. unsigned int get_path_length;
  192. /**
  193. * Best known destination (could be my friend or finger) which should
  194. * get this message next.
  195. */
  196. struct GNUNET_PeerIdentity best_known_destination;
  197. /**
  198. * In case best_known_destination is a finger, then trail to reach
  199. * to that finger. Else its default value is 0.
  200. */
  201. struct GNUNET_HashCode intermediate_trail_id;
  202. /**
  203. * The key we are looking for.
  204. */
  205. struct GNUNET_HashCode key;
  206. /* Get path. */
  207. /* struct GNUNET_PeerIdentity[]*/
  208. };
  209. /**
  210. * P2P Result message
  211. */
  212. struct PeerGetResultMessage
  213. {
  214. /**
  215. * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_GET_RESULT
  216. */
  217. struct GNUNET_MessageHeader header;
  218. /**
  219. * The type for the data.
  220. */
  221. uint32_t type GNUNET_PACKED;
  222. /**
  223. * Number of peers recorded in the outgoing path from source to the
  224. * stored location of this message.
  225. */
  226. uint32_t put_path_length GNUNET_PACKED;
  227. /**
  228. * Length of the GET path that follows (if tracked).
  229. */
  230. uint32_t get_path_length GNUNET_PACKED;
  231. /**
  232. * Peer which queried for get and should get the result.
  233. */
  234. struct GNUNET_PeerIdentity querying_peer;
  235. /**
  236. * When does the content expire?
  237. */
  238. struct GNUNET_TIME_Absolute expiration_time;
  239. /**
  240. * The key of the corresponding GET request.
  241. */
  242. struct GNUNET_HashCode key;
  243. /* put path (if tracked) */
  244. /* get path (if tracked) */
  245. /* Payload */
  246. };
  247. /**
  248. * P2P Trail setup message
  249. */
  250. struct PeerTrailSetupMessage
  251. {
  252. /**
  253. * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_SETUP
  254. */
  255. struct GNUNET_MessageHeader header;
  256. /**
  257. * Is source_peer trying to setup the trail to a predecessor or any finger.
  258. */
  259. uint32_t is_predecessor;
  260. /**
  261. * Peer closest to this value will be our finger.
  262. */
  263. uint64_t final_destination_finger_value;
  264. /**
  265. * Source peer which wants to setup the trail to one of its finger.
  266. */
  267. struct GNUNET_PeerIdentity source_peer;
  268. /**
  269. * Best known destination (could be my friend or finger) which should
  270. * get this message next.
  271. *
  272. * FIXME: this could be removed if we include trail_source / trail_dest
  273. * in the routing table. This way we save 32 bytes of bandwidth by using
  274. * extra 8 bytes of memory (2 * sizeof (GNUNET_PEER_ID))
  275. */
  276. struct GNUNET_PeerIdentity best_known_destination;
  277. /**
  278. * In case best_known_destination is a finger, then trail id of trail to
  279. * reach to this finger.
  280. */
  281. struct GNUNET_HashCode intermediate_trail_id;
  282. /**
  283. * Trail id for trail which we are trying to setup.
  284. */
  285. struct GNUNET_HashCode trail_id;
  286. /* List of peers which are part of trail setup so far.
  287. * Trail does NOT include source_peer and peer which will be closest to
  288. * ultimate_destination_finger_value.
  289. * struct GNUNET_PeerIdentity trail[]
  290. */
  291. };
  292. /**
  293. * P2P Trail Setup Result message
  294. */
  295. struct PeerTrailSetupResultMessage
  296. {
  297. /**
  298. * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_SETUP_RESULT
  299. */
  300. struct GNUNET_MessageHeader header;
  301. /**
  302. * Finger to which we have found the path.
  303. */
  304. struct GNUNET_PeerIdentity finger_identity;
  305. /**
  306. * Peer which started trail_setup to find trail to finger_identity
  307. */
  308. struct GNUNET_PeerIdentity querying_peer;
  309. /**
  310. * Is the trail setup to querying_peer's predecessor or finger?
  311. */
  312. uint32_t is_predecessor;
  313. /**
  314. * Value to which finger_identity is the closest peer.
  315. */
  316. uint64_t ulitmate_destination_finger_value;
  317. /**
  318. * Identifier of the trail from querying peer to finger_identity, NOT
  319. * including both endpoints.
  320. */
  321. struct GNUNET_HashCode trail_id;
  322. /* List of peers which are part of the trail from querying peer to
  323. * finger_identity, NOT including both endpoints.
  324. * struct GNUNET_PeerIdentity trail[]
  325. */
  326. };
  327. /**
  328. * P2P Verify Successor Message.
  329. */
  330. struct PeerVerifySuccessorMessage
  331. {
  332. /**
  333. * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_VERIFY_SUCCESSOR
  334. */
  335. struct GNUNET_MessageHeader header;
  336. /**
  337. * Peer which wants to verify its successor.
  338. */
  339. struct GNUNET_PeerIdentity source_peer;
  340. /**
  341. * Source Peer's current successor.
  342. */
  343. struct GNUNET_PeerIdentity successor;
  344. /**
  345. * Identifier of trail to reach from source_peer to successor.
  346. */
  347. struct GNUNET_HashCode trail_id;
  348. /* List of the peers which are part of trail to reach from source_peer
  349. * to successor, NOT including them
  350. * struct GNUNET_PeerIdentity trail[]
  351. */
  352. };
  353. /**
  354. * P2P Verify Successor Result Message
  355. */
  356. struct PeerVerifySuccessorResultMessage
  357. {
  358. /**
  359. * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_VERIFY_SUCCESSOR_RESULT
  360. */
  361. struct GNUNET_MessageHeader header;
  362. /**
  363. * Peer which sent the request to verify its successor.
  364. */
  365. struct GNUNET_PeerIdentity querying_peer;
  366. /**
  367. * Successor to which PeerVerifySuccessorMessage was sent.
  368. */
  369. struct GNUNET_PeerIdentity current_successor;
  370. /**
  371. * Current Predecessor of source_successor. It can be same as querying peer
  372. * or different. In case it is different then it can be querying_peer's
  373. * probable successor.
  374. */
  375. struct GNUNET_PeerIdentity probable_successor;
  376. /**
  377. * Trail identifier of trail from querying_peer to current_successor.
  378. */
  379. struct GNUNET_HashCode trail_id;
  380. /**
  381. * Direction in which we are looking at the trail.
  382. */
  383. uint32_t trail_direction;
  384. /* In case probable_successor != querying_peer, then trail to reach from
  385. * querying_peer to probable_successor, NOT including end points.
  386. * struct GNUNET_PeerIdentity trail[]
  387. */
  388. };
  389. /**
  390. * P2P Notify New Successor Message.
  391. */
  392. struct PeerNotifyNewSuccessorMessage
  393. {
  394. /**
  395. * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_NOTIFY_NEW_SUCCESSOR
  396. */
  397. struct GNUNET_MessageHeader header;
  398. /**
  399. * Peer which wants to notify its new successor.
  400. */
  401. struct GNUNET_PeerIdentity source_peer;
  402. /**
  403. * New successor of source_peer.
  404. */
  405. struct GNUNET_PeerIdentity new_successor;
  406. /**
  407. * Unique identifier of the trail from source_peer to new_successor,
  408. * NOT including the endpoints.
  409. */
  410. struct GNUNET_HashCode trail_id;
  411. /* List of peers in trail from source_peer to new_successor,
  412. * NOT including the endpoints.
  413. * struct GNUNET_PeerIdentity trail[]
  414. */
  415. };
  416. /**
  417. * P2P Notify Successor Confirmation message.
  418. */
  419. struct PeerNotifyConfirmationMessage
  420. {
  421. /**
  422. * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_TEARDOWN
  423. */
  424. struct GNUNET_MessageHeader header;
  425. /**
  426. * Unique identifier of the trail.
  427. */
  428. struct GNUNET_HashCode trail_id;
  429. /**
  430. * Direction of trail.
  431. */
  432. uint32_t trail_direction;
  433. };
  434. /**
  435. * P2P Trail Tear Down message.
  436. */
  437. struct PeerTrailTearDownMessage
  438. {
  439. /**
  440. * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_TEARDOWN
  441. */
  442. struct GNUNET_MessageHeader header;
  443. /**
  444. * Unique identifier of the trail.
  445. */
  446. struct GNUNET_HashCode trail_id;
  447. /**
  448. * Direction of trail.
  449. */
  450. uint32_t trail_direction;
  451. };
  452. /**
  453. * P2P Trail Rejection Message.
  454. */
  455. struct PeerTrailRejectionMessage
  456. {
  457. /**
  458. * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_SETUP_REJECTION
  459. */
  460. struct GNUNET_MessageHeader header;
  461. /**
  462. * Peer which wants to set up the trail.
  463. */
  464. struct GNUNET_PeerIdentity source_peer;
  465. /**
  466. * Peer which sent trail rejection message as it it congested.
  467. */
  468. struct GNUNET_PeerIdentity congested_peer;
  469. /**
  470. * Peer identity closest to this value will be finger of
  471. * source_peer.
  472. */
  473. uint64_t ultimate_destination_finger_value;
  474. /**
  475. * Is source_peer trying to setup the trail to its predecessor or finger.
  476. */
  477. uint32_t is_predecessor;
  478. /**
  479. * Identifier for the trail that source peer is trying to setup.
  480. */
  481. struct GNUNET_HashCode trail_id;
  482. /**
  483. * Relative time for which congested_peer will remain congested.
  484. */
  485. struct GNUNET_TIME_Relative congestion_time;
  486. /* Trail_list from source_peer to peer which sent the message for trail setup
  487. * to congested peer. This trail does NOT include source_peer.
  488. struct GNUNET_PeerIdnetity trail[]*/
  489. };
  490. /**
  491. * P2P Add Trail Message.
  492. */
  493. struct PeerAddTrailMessage
  494. {
  495. /**
  496. * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_ADD_TRAIL
  497. */
  498. struct GNUNET_MessageHeader header;
  499. /**
  500. * Source of the routing trail.
  501. */
  502. struct GNUNET_PeerIdentity source_peer;
  503. /**
  504. * Destination of the routing trail.
  505. */
  506. struct GNUNET_PeerIdentity destination_peer;
  507. /**
  508. * Unique identifier of the trail from source_peer to destination_peer,
  509. * NOT including the endpoints.
  510. */
  511. struct GNUNET_HashCode trail_id;
  512. /* Trail from source peer to destination peer, NOT including them.
  513. * struct GNUNET_PeerIdentity trail[]
  514. */
  515. };
  516. GNUNET_NETWORK_STRUCT_END
  517. /**
  518. * Linked list of messages to send to a particular other peer.
  519. */
  520. struct P2PPendingMessage
  521. {
  522. /**
  523. * Pointer to next item in the list
  524. */
  525. struct P2PPendingMessage *next;
  526. /**
  527. * Pointer to previous item in the list
  528. */
  529. struct P2PPendingMessage *prev;
  530. /**
  531. * Message importance level. FIXME: used? useful?
  532. */
  533. unsigned int importance;
  534. /**
  535. * When does this message time out?
  536. */
  537. struct GNUNET_TIME_Absolute timeout;
  538. /**
  539. * Actual message to be sent, allocated at the end of the struct:
  540. * // msg = (cast) &pm[1];
  541. * // memcpy (&pm[1], data, len);
  542. */
  543. const struct GNUNET_MessageHeader *msg;
  544. };
  545. /**
  546. * Entry in friend_peermap.
  547. */
  548. struct FriendInfo
  549. {
  550. /**
  551. * Friend Identity
  552. */
  553. struct GNUNET_PeerIdentity id;
  554. /**
  555. * Number of trails for which this friend is the first hop or if the friend
  556. * is finger.
  557. */
  558. unsigned int trails_count;
  559. /**
  560. * Count of outstanding messages for this friend.
  561. */
  562. unsigned int pending_count;
  563. /**
  564. * In case not 0, then amount of time for which this friend is congested.
  565. */
  566. struct GNUNET_TIME_Absolute congestion_timestamp;
  567. // TODO : Change name of head and tail to pending_messages_list_head and so.
  568. /**
  569. * Head of pending messages to be sent to this friend.
  570. */
  571. struct P2PPendingMessage *head;
  572. /**
  573. * Tail of pending messages to be sent to this friend.
  574. */
  575. struct P2PPendingMessage *tail;
  576. /**
  577. * Core handle for sending messages to this friend.
  578. */
  579. struct GNUNET_CORE_TransmitHandle *th;
  580. };
  581. /**
  582. * An individual element of the trail to reach to a finger.
  583. */
  584. struct Trail_Element
  585. {
  586. /**
  587. * Pointer to next item in the list
  588. */
  589. struct Trail_Element *next;
  590. /**
  591. * Pointer to prev item in the list
  592. */
  593. struct Trail_Element *prev;
  594. /**
  595. * An element in this trail.
  596. */
  597. struct GNUNET_PeerIdentity peer;
  598. };
  599. /**
  600. * Information about an individual trail.
  601. */
  602. struct Trail
  603. {
  604. /**
  605. * Head of trail.
  606. */
  607. struct Trail_Element *trail_head;
  608. /**
  609. * Tail of trail.
  610. */
  611. struct Trail_Element *trail_tail;
  612. /**
  613. * Unique identifier of this trail.
  614. */
  615. struct GNUNET_HashCode trail_id;
  616. /**
  617. * Length of trail pointed
  618. */
  619. unsigned int trail_length;
  620. /**
  621. * Is there a valid trail entry.
  622. */
  623. unsigned int is_present;
  624. };
  625. /**
  626. * An entry in finger_table
  627. */
  628. struct FingerInfo
  629. {
  630. /**
  631. * Finger identity.
  632. */
  633. struct GNUNET_PeerIdentity finger_identity;
  634. /**
  635. * In case not 0, this amount is time to wait for notify successor message.
  636. * Used ONLY for successor. NOT for any other finger.
  637. */
  638. struct GNUNET_TIME_Absolute wait_notify_confirmation;
  639. /**
  640. * Is any finger stored at this finger index.
  641. */
  642. unsigned int is_present;
  643. /**
  644. * Index in finger peer map
  645. */
  646. uint32_t finger_table_index;
  647. /**
  648. * Number of trails setup so far for this finger.
  649. * Should not cross MAXIMUM_TRAILS_PER_FINGER.
  650. */
  651. uint32_t trails_count;
  652. /**
  653. * Array of trails to reach to this finger.
  654. */
  655. struct Trail trail_list[MAXIMUM_TRAILS_PER_FINGER];
  656. };
  657. /**
  658. * Stores information about the peer which is closest to destination_finger_value.
  659. * 'closest' can be either successor or predecessor depending on is_predecessor
  660. * flag.
  661. */
  662. struct Closest_Peer
  663. {
  664. /**
  665. * Destination finger value.
  666. */
  667. uint64_t destination_finger_value;
  668. /**
  669. * Is finger_value a predecessor or any other finger.
  670. */
  671. unsigned int is_predecessor;
  672. /**
  673. * Trail id to reach to peer.
  674. * In case peer is my identity or friend, it is set to 0.
  675. */
  676. struct GNUNET_HashCode trail_id;
  677. /**
  678. * Next destination. In case of friend and my_identity , it is same as next_hop
  679. * In case of finger it is finger identity.
  680. */
  681. struct GNUNET_PeerIdentity best_known_destination;
  682. /**
  683. * In case best_known_destination is a finger, then first friend in the trail
  684. * to reach to it. In other case, same as best_known_destination.
  685. */
  686. struct GNUNET_PeerIdentity next_hop;
  687. /**
  688. * In case finger is the next hop, it contains a valid finger table index
  689. * at which the finger is stored. Else, It contains 65, which is out of range
  690. * of finger table index.
  691. */
  692. unsigned int finger_table_index;
  693. };
  694. /**
  695. * Context for send_verify_successor_task.
  696. */
  697. struct VerifySuccessorContext
  698. {
  699. /**
  700. * Number of times this has been scheduled.
  701. */
  702. unsigned int num_retries_scheduled;
  703. };
  704. /**
  705. * Task that sends FIND FINGER TRAIL requests. This task is started when we have
  706. * get our first friend.
  707. */
  708. static GNUNET_SCHEDULER_TaskIdentifier find_finger_trail_task;
  709. /**
  710. * Task that sends verify successor message. This task is started when we get
  711. * our successor for the first time.
  712. */
  713. static GNUNET_SCHEDULER_TaskIdentifier send_verify_successor_task;
  714. /**
  715. * Task that sends verify successor message. This task is started when we get
  716. * our successor for the first time.
  717. */
  718. static GNUNET_SCHEDULER_TaskIdentifier send_verify_successor_retry_task;
  719. /**
  720. * Task that sends verify successor message. This task is started when we get
  721. * our successor for the first time.
  722. */
  723. static GNUNET_SCHEDULER_TaskIdentifier send_notify_new_successor_retry_task;
  724. /**
  725. * Identity of this peer.
  726. */
  727. static struct GNUNET_PeerIdentity my_identity;
  728. /**
  729. * Peer map of all the friends of a peer
  730. */
  731. static struct GNUNET_CONTAINER_MultiPeerMap *friend_peermap;
  732. /**
  733. * Array of all the fingers.
  734. */
  735. static struct FingerInfo finger_table [MAX_FINGERS];
  736. /**
  737. * Handle to CORE.
  738. */
  739. static struct GNUNET_CORE_Handle *core_api;
  740. /**
  741. * Handle for the statistics service.
  742. */
  743. //extern struct GNUNET_STATISTICS_Handle *GDS_stats;
  744. /**
  745. * The current finger index that we have want to find trail to. We start the
  746. * search with value = 0, i.e. successor and then go to PREDCESSOR_FINGER_ID
  747. * and decrement it. For any index 63 <= index < 0, if finger is same as successor,
  748. * we reset this index to 0.
  749. */
  750. static unsigned int current_search_finger_index;
  751. /**
  752. * Time duration to schedule find finger trail task.
  753. */
  754. static struct GNUNET_TIME_Relative find_finger_trail_task_next_send_time;
  755. /**
  756. * Time duration to schedule verify successor task.
  757. */
  758. static struct GNUNET_TIME_Relative verify_successor_next_send_time;
  759. /**
  760. * Time duration to send verify successor again, if result was not received in time.
  761. */
  762. static struct GNUNET_TIME_Relative verify_successor_retry_time;
  763. /**
  764. * Time duration to retry send_notify_successor.
  765. */
  766. static struct GNUNET_TIME_Relative notify_successor_retry_time;
  767. /**
  768. * Are we waiting for confirmation from our new successor that it got the
  769. * message
  770. */
  771. //static unsigned int waiting_for_notify_confirmation;
  772. /* Below variables are used only for testing, and statistics collection. */
  773. /**
  774. * Should we store our topology predecessor and successor IDs into statistics?
  775. */
  776. unsigned int track_topology;
  777. /**
  778. * Should I be a malicious peer and drop the PUT/GET packets?
  779. * if 0 then NOT malicious.
  780. */
  781. unsigned int act_malicious;
  782. /**
  783. * Count of fingers found. Ideally we should have O(logn) fingers for a
  784. * stable network.
  785. */
  786. static unsigned int total_fingers_found;
  787. /**
  788. * Number of times we found the same successor.
  789. */
  790. static unsigned int successor_times;
  791. /**
  792. * Number of rounds for which we should search for finger.
  793. */
  794. static unsigned int fingers_round_count;
  795. /**
  796. * Called when core is ready to send a message we asked for
  797. * out to the destination.
  798. *
  799. * @param cls the 'struct FriendInfo' of the target friend
  800. * @param size number of bytes available in buf
  801. * @param buf where the callee should write the message
  802. * @return number of bytes written to buf
  803. */
  804. static size_t
  805. core_transmit_notify (void *cls, size_t size, void *buf)
  806. {
  807. struct FriendInfo *peer = cls;
  808. char *cbuf = buf;
  809. struct P2PPendingMessage *pending;
  810. size_t off;
  811. size_t msize;
  812. peer->th = NULL;
  813. while ((NULL != (pending = peer->head)) &&
  814. (0 == GNUNET_TIME_absolute_get_remaining (pending->timeout).rel_value_us))
  815. {
  816. peer->pending_count--;
  817. GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending);
  818. GNUNET_free (pending);
  819. }
  820. if (NULL == pending)
  821. {
  822. /* no messages pending */
  823. return 0;
  824. }
  825. if (NULL == buf)
  826. {
  827. peer->th =
  828. GNUNET_CORE_notify_transmit_ready (core_api, GNUNET_NO,
  829. GNUNET_CORE_PRIO_BEST_EFFORT,
  830. GNUNET_TIME_absolute_get_remaining
  831. (pending->timeout), &peer->id,
  832. ntohs (pending->msg->size),
  833. &core_transmit_notify, peer);
  834. GNUNET_break (NULL != peer->th);
  835. return 0;
  836. }
  837. off = 0;
  838. while ((NULL != (pending = peer->head)) &&
  839. (size - off >= (msize = ntohs (pending->msg->size))))
  840. {
  841. GNUNET_STATISTICS_update (GDS_stats,
  842. gettext_noop
  843. ("# Bytes transmitted to other peers"), msize,
  844. GNUNET_NO);
  845. memcpy (&cbuf[off], pending->msg, msize);
  846. off += msize;
  847. peer->pending_count--;
  848. GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending);
  849. GNUNET_free (pending);
  850. }
  851. if (peer->head != NULL)
  852. {
  853. peer->th =
  854. GNUNET_CORE_notify_transmit_ready (core_api, GNUNET_NO,
  855. GNUNET_CORE_PRIO_BEST_EFFORT,
  856. GNUNET_TIME_absolute_get_remaining
  857. (pending->timeout), &peer->id, msize,
  858. &core_transmit_notify, peer);
  859. GNUNET_break (NULL != peer->th);
  860. }
  861. return off;
  862. }
  863. /**
  864. * Transmit all messages in the friend's message queue.
  865. *
  866. * @param peer message queue to process
  867. */
  868. static void
  869. process_friend_queue (struct FriendInfo *peer)
  870. {
  871. struct P2PPendingMessage *pending;
  872. if (NULL == (pending = peer->head))
  873. {
  874. return;
  875. }
  876. if (NULL != peer->th)
  877. {
  878. return;
  879. }
  880. peer->th =
  881. GNUNET_CORE_notify_transmit_ready (core_api, GNUNET_NO,
  882. pending->importance,
  883. GNUNET_TIME_absolute_get_remaining
  884. (pending->timeout), &peer->id,
  885. ntohs (pending->msg->size),
  886. &core_transmit_notify, peer);
  887. GNUNET_break (NULL != peer->th);
  888. }
  889. #if ENABLE_MALICIOUS
  890. /**
  891. * Set the ENABLE_MALICIOUS value to malicious.
  892. * @param malicious
  893. */
  894. int
  895. GDS_NEIGHBOURS_act_malicious (unsigned int malicious)
  896. {
  897. act_malicious = malicious;
  898. return GNUNET_OK;
  899. }
  900. #endif
  901. /**
  902. * Construct a trail setup message and forward it to target_friend
  903. * @param source_peer Peer which wants to setup the trail
  904. * @param ultimate_destination_finger_value Peer identity closest to this value
  905. * will be finger to @a source_peer
  906. * @param best_known_destination Best known destination (could be finger or friend)
  907. * which should get this message. In case it is
  908. * friend, then it is same as target_friend
  909. * @param target_friend Friend to which message is forwarded now.
  910. * @param trail_length Total number of peers in trail setup so far.
  911. * @param trail_peer_list Trail setup so far
  912. * @param is_predecessor Is @a source_peer looking for trail to a predecessor or not.
  913. * @param trail_id Unique identifier for the trail we are trying to setup.
  914. * @param intermediate_trail_id Trail id of intermediate trail to reach to
  915. * best_known_destination when its a finger. If not
  916. * used then set to 0.
  917. */
  918. void
  919. GDS_NEIGHBOURS_send_trail_setup (struct GNUNET_PeerIdentity source_peer,
  920. uint64_t ultimate_destination_finger_value,
  921. struct GNUNET_PeerIdentity best_known_destination,
  922. struct FriendInfo *target_friend,
  923. unsigned int trail_length,
  924. const struct GNUNET_PeerIdentity *trail_peer_list,
  925. unsigned int is_predecessor,
  926. struct GNUNET_HashCode trail_id,
  927. struct GNUNET_HashCode intermediate_trail_id)
  928. {
  929. struct P2PPendingMessage *pending;
  930. struct PeerTrailSetupMessage *tsm;
  931. struct GNUNET_PeerIdentity *peer_list;
  932. size_t msize;
  933. msize = sizeof (struct PeerTrailSetupMessage) +
  934. (trail_length * sizeof (struct GNUNET_PeerIdentity));
  935. if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
  936. {
  937. GNUNET_break (0);
  938. return;
  939. }
  940. if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
  941. {
  942. GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
  943. 1, GNUNET_NO);
  944. }
  945. pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
  946. pending->timeout = GNUNET_TIME_relative_to_absolute (PENDING_MESSAGE_TIMEOUT);
  947. tsm = (struct PeerTrailSetupMessage *) &pending[1];
  948. pending->msg = &(tsm->header);
  949. tsm->header.size = htons (msize);
  950. tsm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_SETUP);
  951. tsm->final_destination_finger_value = GNUNET_htonll (ultimate_destination_finger_value);
  952. tsm->source_peer = source_peer;
  953. tsm->best_known_destination = best_known_destination;
  954. tsm->is_predecessor = htonl (is_predecessor);
  955. tsm->trail_id = trail_id;
  956. tsm->intermediate_trail_id = intermediate_trail_id;
  957. if (trail_length > 0)
  958. {
  959. peer_list = (struct GNUNET_PeerIdentity *) &tsm[1];
  960. memcpy (peer_list, trail_peer_list, trail_length * sizeof(struct GNUNET_PeerIdentity));
  961. }
  962. GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
  963. target_friend->pending_count++;
  964. process_friend_queue (target_friend);
  965. }
  966. /**
  967. * Construct a trail setup result message and forward it to target friend.
  968. * @param querying_peer Peer which sent the trail setup request and should get
  969. * the result back.
  970. * @param Finger Peer to which the trail has been setup to.
  971. * @param target_friend Friend to which this message should be forwarded.
  972. * @param trail_length Numbers of peers in the trail.
  973. * @param trail_peer_list Peers which are part of the trail from
  974. * querying_peer to Finger, NOT including them.
  975. * @param is_predecessor Is @a Finger predecessor to @a querying_peer ?
  976. * @param ultimate_destination_finger_value Value to which @a finger is the closest
  977. * peer.
  978. * @param trail_id Unique identifier of the trail.
  979. */
  980. void
  981. GDS_NEIGHBOURS_send_trail_setup_result (struct GNUNET_PeerIdentity querying_peer,
  982. struct GNUNET_PeerIdentity finger,
  983. struct FriendInfo *target_friend,
  984. unsigned int trail_length,
  985. const struct GNUNET_PeerIdentity *trail_peer_list,
  986. unsigned int is_predecessor,
  987. uint64_t ultimate_destination_finger_value,
  988. struct GNUNET_HashCode trail_id)
  989. {
  990. struct P2PPendingMessage *pending;
  991. struct PeerTrailSetupResultMessage *tsrm;
  992. struct GNUNET_PeerIdentity *peer_list;
  993. size_t msize;
  994. msize = sizeof (struct PeerTrailSetupResultMessage) +
  995. (trail_length * sizeof (struct GNUNET_PeerIdentity));
  996. if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
  997. {
  998. GNUNET_break (0);
  999. return;
  1000. }
  1001. if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
  1002. {
  1003. GNUNET_STATISTICS_update (GDS_stats,
  1004. gettext_noop ("# P2P messages dropped due to full queue"),
  1005. 1, GNUNET_NO);
  1006. }
  1007. pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
  1008. pending->importance = 0;
  1009. pending->timeout = GNUNET_TIME_relative_to_absolute (PENDING_MESSAGE_TIMEOUT);
  1010. tsrm = (struct PeerTrailSetupResultMessage *) &pending[1];
  1011. pending->msg = &tsrm->header;
  1012. tsrm->header.size = htons (msize);
  1013. tsrm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_SETUP_RESULT);
  1014. tsrm->querying_peer = querying_peer;
  1015. tsrm->finger_identity = finger;
  1016. tsrm->is_predecessor = htonl (is_predecessor);
  1017. tsrm->trail_id = trail_id;
  1018. tsrm->ulitmate_destination_finger_value =
  1019. GNUNET_htonll (ultimate_destination_finger_value);
  1020. peer_list = (struct GNUNET_PeerIdentity *) &tsrm[1];
  1021. memcpy (peer_list, trail_peer_list, trail_length * sizeof (struct GNUNET_PeerIdentity));
  1022. /* Send the message to chosen friend. */
  1023. GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
  1024. target_friend->pending_count++;
  1025. process_friend_queue (target_friend);
  1026. }
  1027. /**
  1028. * Send notify successor confirmation message.
  1029. * @param trail_id Unique Identifier of the trail.
  1030. * @param trail_direction Destination to Source.
  1031. * @param target_friend Friend to get this message next.
  1032. */
  1033. void
  1034. GDS_NEIGHBOURS_send_notify_succcessor_confirmation (struct GNUNET_HashCode trail_id,
  1035. unsigned int trail_direction,
  1036. struct FriendInfo *target_friend)
  1037. {
  1038. struct PeerNotifyConfirmationMessage *ncm;
  1039. struct P2PPendingMessage *pending;
  1040. size_t msize;
  1041. msize = sizeof (struct PeerNotifyConfirmationMessage);
  1042. if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
  1043. {
  1044. GNUNET_break (0);
  1045. return;
  1046. }
  1047. if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
  1048. {
  1049. GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
  1050. 1, GNUNET_NO);
  1051. }
  1052. pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
  1053. pending->importance = 0; /* FIXME */
  1054. pending->timeout = GNUNET_TIME_relative_to_absolute (PENDING_MESSAGE_TIMEOUT);
  1055. ncm = (struct PeerNotifyConfirmationMessage *) &pending[1];
  1056. pending->msg = &ncm->header;
  1057. ncm->header.size = htons (msize);
  1058. ncm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_NOTIFY_SUCCESSOR_CONFIRMATION);
  1059. ncm->trail_id = trail_id;
  1060. ncm->trail_direction = htonl (trail_direction);
  1061. /* Send the message to chosen friend. */
  1062. GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
  1063. target_friend->pending_count++;
  1064. process_friend_queue (target_friend);
  1065. }
  1066. /**
  1067. * Send trail rejection message to target friend
  1068. * @param source_peer Peer which is trying to setup the trail.
  1069. * @param ultimate_destination_finger_value Peer closest to this value will be
  1070. * @a source_peer's finger
  1071. * @param congested_peer Peer which sent this message as it is congested.
  1072. * @param is_predecessor Is source_peer looking for trail to a predecessor or not.
  1073. * @param trail_peer_list Trails seen so far in trail setup before getting rejected
  1074. * by congested_peer. This does NOT include @a source_peer
  1075. * and congested_peer.
  1076. * @param trail_length Total number of peers in trail_peer_list, NOT including
  1077. * @a source_peer and @a congested_peer
  1078. * @param trail_id Unique identifier of this trail.
  1079. * @param congestion_timeout Duration given by congested peer as an estimate of
  1080. * how long it may remain congested.
  1081. */
  1082. void
  1083. GDS_NEIGHBOURS_send_trail_rejection (struct GNUNET_PeerIdentity source_peer,
  1084. uint64_t ultimate_destination_finger_value,
  1085. struct GNUNET_PeerIdentity congested_peer,
  1086. unsigned int is_predecessor,
  1087. const struct GNUNET_PeerIdentity *trail_peer_list,
  1088. unsigned int trail_length,
  1089. struct GNUNET_HashCode trail_id,
  1090. struct FriendInfo *target_friend,
  1091. const struct GNUNET_TIME_Relative congestion_timeout)
  1092. {
  1093. struct PeerTrailRejectionMessage *trm;
  1094. struct P2PPendingMessage *pending;
  1095. struct GNUNET_PeerIdentity *peer_list;
  1096. size_t msize;
  1097. msize = sizeof (struct PeerTrailRejectionMessage) +
  1098. (trail_length * sizeof (struct GNUNET_PeerIdentity));
  1099. if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
  1100. {
  1101. GNUNET_break (0);
  1102. return;
  1103. }
  1104. if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
  1105. {
  1106. GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
  1107. 1, GNUNET_NO);
  1108. }
  1109. pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
  1110. pending->importance = 0;
  1111. pending->timeout = GNUNET_TIME_relative_to_absolute (PENDING_MESSAGE_TIMEOUT);
  1112. trm = (struct PeerTrailRejectionMessage *)&pending[1];
  1113. pending->msg = &trm->header;
  1114. trm->header.size = htons (msize);
  1115. trm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_SETUP_REJECTION);
  1116. trm->source_peer = source_peer;
  1117. trm->congested_peer = congested_peer;
  1118. trm->congestion_time = congestion_timeout;
  1119. trm->is_predecessor = htonl (is_predecessor);
  1120. trm->trail_id = trail_id;
  1121. trm->ultimate_destination_finger_value =
  1122. GNUNET_htonll (ultimate_destination_finger_value);
  1123. peer_list = (struct GNUNET_PeerIdentity *) &trm[1];
  1124. if (trail_length > 0)
  1125. {
  1126. memcpy (peer_list, trail_peer_list, trail_length * sizeof (struct GNUNET_PeerIdentity));
  1127. }
  1128. /* Send the message to chosen friend. */
  1129. GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
  1130. target_friend->pending_count++;
  1131. process_friend_queue (target_friend);
  1132. }
  1133. /**
  1134. * Construct a verify successor message and forward it to target_friend.
  1135. * @param source_peer Peer which wants to verify its successor.
  1136. * @param successor Peer which is @a source_peer's current successor.
  1137. * @param trail_id Unique Identifier of trail from @a source_peer to @a successor,
  1138. * NOT including them.
  1139. * @param trail List of peers which are part of trail to reach from @a source_peer
  1140. * to @a successor, NOT including them.
  1141. * @param trail_length Total number of peers in @a trail.
  1142. * @param target_friend Next friend to get this message.
  1143. */
  1144. void
  1145. GDS_NEIGHBOURS_send_verify_successor_message (struct GNUNET_PeerIdentity source_peer,
  1146. struct GNUNET_PeerIdentity successor,
  1147. struct GNUNET_HashCode trail_id,
  1148. struct GNUNET_PeerIdentity *trail,
  1149. unsigned int trail_length,
  1150. struct FriendInfo *target_friend)
  1151. {
  1152. struct PeerVerifySuccessorMessage *vsm;
  1153. struct P2PPendingMessage *pending;
  1154. struct GNUNET_PeerIdentity *peer_list;
  1155. size_t msize;
  1156. msize = sizeof (struct PeerVerifySuccessorMessage) +
  1157. (trail_length * sizeof (struct GNUNET_PeerIdentity));
  1158. if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
  1159. {
  1160. GNUNET_break (0);
  1161. return;
  1162. }
  1163. if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
  1164. {
  1165. GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
  1166. 1, GNUNET_NO);
  1167. }
  1168. pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
  1169. pending->importance = 0; /* FIXME */
  1170. pending->timeout = GNUNET_TIME_relative_to_absolute (PENDING_MESSAGE_TIMEOUT);
  1171. vsm = (struct PeerVerifySuccessorMessage *) &pending[1];
  1172. pending->msg = &vsm->header;
  1173. vsm->header.size = htons (msize);
  1174. vsm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_VERIFY_SUCCESSOR);
  1175. vsm->source_peer = source_peer;
  1176. vsm->successor = successor;
  1177. vsm->trail_id = trail_id;
  1178. peer_list = (struct GNUNET_PeerIdentity *) &vsm[1];
  1179. memcpy (peer_list, trail, trail_length * sizeof (struct GNUNET_PeerIdentity));
  1180. /* Send the message to chosen friend. */
  1181. GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
  1182. target_friend->pending_count++;
  1183. process_friend_queue (target_friend);
  1184. }
  1185. /**
  1186. * FIXME: In every function we pass target friend except for this one.
  1187. * so, either change everything or this one. also, should se just store
  1188. * the pointer to friend in routing table rather than gnunet_peeridentity.
  1189. * if yes then we should keep friend info in.h andmake lot of changes.
  1190. * Construct a trail teardown message and forward it to target friend.
  1191. *
  1192. * @param trail_id Unique identifier of the trail.
  1193. * @param trail_direction Direction of trail.
  1194. * @param target_friend Friend to get this message.
  1195. */
  1196. void
  1197. GDS_NEIGHBOURS_send_trail_teardown (const struct GNUNET_HashCode *trail_id,
  1198. unsigned int trail_direction,
  1199. const struct GNUNET_PeerIdentity *peer)
  1200. {
  1201. struct PeerTrailTearDownMessage *ttdm;
  1202. struct P2PPendingMessage *pending;
  1203. struct FriendInfo *target_friend;
  1204. size_t msize;
  1205. msize = sizeof (struct PeerTrailTearDownMessage);
  1206. if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
  1207. {
  1208. GNUNET_break (0);
  1209. return;
  1210. }
  1211. if (NULL == (target_friend =
  1212. GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer)))
  1213. {
  1214. /* FIXME: In what case friend can be null. ?*/
  1215. GNUNET_break (0);
  1216. return;
  1217. }
  1218. if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
  1219. {
  1220. GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
  1221. 1, GNUNET_NO);
  1222. }
  1223. pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
  1224. pending->importance = 0; /* FIXME */
  1225. pending->timeout = GNUNET_TIME_relative_to_absolute (PENDING_MESSAGE_TIMEOUT);
  1226. ttdm = (struct PeerTrailTearDownMessage *) &pending[1];
  1227. pending->msg = &ttdm->header;
  1228. ttdm->header.size = htons (msize);
  1229. ttdm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_TEARDOWN);
  1230. ttdm->trail_id = *trail_id;
  1231. ttdm->trail_direction = htonl (trail_direction);
  1232. /* Send the message to chosen friend. */
  1233. GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
  1234. target_friend->pending_count++;
  1235. process_friend_queue (target_friend);
  1236. }
  1237. /**
  1238. * Construct a verify successor result message and send it to target_friend
  1239. * @param querying_peer Peer which sent the verify successor message.
  1240. * @param source_successor Current_successor of @a querying_peer.
  1241. * @param current_predecessor Current predecessor of @a successor. Could be same
  1242. * or different from @a querying_peer.
  1243. * @param trail_id Unique identifier of the trail from @a querying_peer to
  1244. * @a successor, NOT including them.
  1245. * @param trail List of peers which are part of trail from @a querying_peer to
  1246. * @a successor, NOT including them.
  1247. * @param trail_length Total number of peers in @a trail
  1248. * @param trail_direction Direction in which we are sending the message. In this
  1249. * case we are sending result from @a successor to @a querying_peer.
  1250. * @param target_friend Next friend to get this message.
  1251. */
  1252. void
  1253. GDS_NEIGHBOURS_send_verify_successor_result (struct GNUNET_PeerIdentity querying_peer,
  1254. struct GNUNET_PeerIdentity current_successor,
  1255. struct GNUNET_PeerIdentity probable_successor,
  1256. struct GNUNET_HashCode trail_id,
  1257. const struct GNUNET_PeerIdentity *trail,
  1258. unsigned int trail_length,
  1259. enum GDS_ROUTING_trail_direction trail_direction,
  1260. struct FriendInfo *target_friend)
  1261. {
  1262. struct PeerVerifySuccessorResultMessage *vsmr;
  1263. struct P2PPendingMessage *pending;
  1264. struct GNUNET_PeerIdentity *peer_list;
  1265. size_t msize;
  1266. msize = sizeof (struct PeerVerifySuccessorResultMessage) +
  1267. (trail_length * sizeof(struct GNUNET_PeerIdentity));
  1268. if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
  1269. {
  1270. GNUNET_break (0);
  1271. return;
  1272. }
  1273. if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
  1274. {
  1275. GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
  1276. 1, GNUNET_NO);
  1277. }
  1278. pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
  1279. pending->importance = 0; /* FIXME */
  1280. pending->timeout = GNUNET_TIME_relative_to_absolute (PENDING_MESSAGE_TIMEOUT);
  1281. vsmr = (struct PeerVerifySuccessorResultMessage *) &pending[1];
  1282. pending->msg = &vsmr->header;
  1283. vsmr->header.size = htons (msize);
  1284. vsmr->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_VERIFY_SUCCESSOR_RESULT);
  1285. vsmr->querying_peer = querying_peer;
  1286. vsmr->current_successor = current_successor;
  1287. vsmr->probable_successor = probable_successor;
  1288. vsmr->trail_direction = htonl (trail_direction);
  1289. vsmr->trail_id = trail_id;
  1290. peer_list = (struct GNUNET_PeerIdentity *) &vsmr[1];
  1291. memcpy (peer_list, trail, trail_length * sizeof (struct GNUNET_PeerIdentity));
  1292. /* Send the message to chosen friend. */
  1293. GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
  1294. target_friend->pending_count++;
  1295. process_friend_queue (target_friend);
  1296. }
  1297. /**
  1298. * Construct a notify new successor message and send it to target_friend
  1299. * @param source_peer Peer which wants to notify to its new successor that it
  1300. * could be its predecessor.
  1301. * @param successor New successor of @a source_peer
  1302. * @param successor_trail List of peers in Trail to reach from
  1303. * @a source_peer to @a new_successor, NOT including
  1304. * the endpoints.
  1305. * @param successor_trail_length Total number of peers in @a new_successor_trail.
  1306. * @param successor_trail_id Unique identifier of @a new_successor_trail.
  1307. * @param target_friend Next friend to get this message.
  1308. */
  1309. void
  1310. GDS_NEIGHBOURS_send_notify_new_successor (struct GNUNET_PeerIdentity source_peer,
  1311. struct GNUNET_PeerIdentity successor,
  1312. const struct GNUNET_PeerIdentity *successor_trail,
  1313. unsigned int successor_trail_length,
  1314. struct GNUNET_HashCode succesor_trail_id,
  1315. struct FriendInfo *target_friend)
  1316. {
  1317. struct PeerNotifyNewSuccessorMessage *nsm;
  1318. struct P2PPendingMessage *pending;
  1319. struct GNUNET_PeerIdentity *peer_list;
  1320. size_t msize;
  1321. msize = sizeof (struct PeerNotifyNewSuccessorMessage) +
  1322. (successor_trail_length * sizeof(struct GNUNET_PeerIdentity));
  1323. if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
  1324. {
  1325. GNUNET_break (0);
  1326. return;
  1327. }
  1328. if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
  1329. {
  1330. GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
  1331. 1, GNUNET_NO);
  1332. }
  1333. pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
  1334. pending->importance = 0; /* FIXME */
  1335. pending->timeout = GNUNET_TIME_relative_to_absolute (PENDING_MESSAGE_TIMEOUT);
  1336. nsm = (struct PeerNotifyNewSuccessorMessage *) &pending[1];
  1337. pending->msg = &nsm->header;
  1338. nsm->header.size = htons (msize);
  1339. nsm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_NOTIFY_NEW_SUCCESSOR);
  1340. nsm->new_successor = successor;
  1341. nsm->source_peer = source_peer;
  1342. nsm->trail_id = succesor_trail_id;
  1343. peer_list = (struct GNUNET_PeerIdentity *) &nsm[1];
  1344. memcpy (peer_list, successor_trail,
  1345. successor_trail_length * sizeof (struct GNUNET_PeerIdentity));
  1346. /* Send the message to chosen friend. */
  1347. GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
  1348. target_friend->pending_count++;
  1349. process_friend_queue (target_friend);
  1350. }
  1351. /**
  1352. * Construct an add_trail message and send it to target_friend
  1353. * @param source_peer Source of the trail.
  1354. * @param destination_peer Destination of the trail.
  1355. * @param trail_id Unique identifier of the trail from
  1356. * @a source_peer to @a destination_peer, NOT including the endpoints.
  1357. * @param trail List of peers in Trail from @a source_peer to @a destination_peer,
  1358. * NOT including the endpoints.
  1359. * @param trail_length Total number of peers in @a trail.
  1360. * @param target_friend Next friend to get this message.
  1361. */
  1362. void
  1363. GDS_NEIGHBOURS_send_add_trail (struct GNUNET_PeerIdentity source_peer,
  1364. struct GNUNET_PeerIdentity destination_peer,
  1365. struct GNUNET_HashCode trail_id,
  1366. const struct GNUNET_PeerIdentity *trail,
  1367. unsigned int trail_length,
  1368. struct FriendInfo *target_friend)
  1369. {
  1370. struct PeerAddTrailMessage *adm;
  1371. struct GNUNET_PeerIdentity *peer_list;
  1372. struct P2PPendingMessage *pending;
  1373. size_t msize;
  1374. msize = sizeof (struct PeerAddTrailMessage) +
  1375. (trail_length * sizeof(struct GNUNET_PeerIdentity));
  1376. if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
  1377. {
  1378. GNUNET_break (0);
  1379. return;
  1380. }
  1381. if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
  1382. {
  1383. GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
  1384. 1, GNUNET_NO);
  1385. }
  1386. pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
  1387. pending->importance = 0; /* FIXME */
  1388. pending->timeout = GNUNET_TIME_relative_to_absolute (PENDING_MESSAGE_TIMEOUT);
  1389. adm = (struct PeerAddTrailMessage *) &pending[1];
  1390. pending->msg = &adm->header;
  1391. adm->header.size = htons (msize);
  1392. adm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_ADD_TRAIL);
  1393. adm->source_peer = source_peer;
  1394. adm->destination_peer = destination_peer;
  1395. adm->trail_id = trail_id;
  1396. peer_list = (struct GNUNET_PeerIdentity *)&adm[1];
  1397. memcpy (peer_list, trail, sizeof (struct GNUNET_PeerIdentity) * trail_length);
  1398. /* Send the message to chosen friend. */
  1399. GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
  1400. target_friend->pending_count++;
  1401. process_friend_queue (target_friend);
  1402. }
  1403. /**
  1404. * Search my location in trail. In case I am present more than once in the
  1405. * trail (can happen during trail setup), then return my lowest index.
  1406. * @param trail List of peers
  1407. * @return my_index if found
  1408. * trail_length + 1 if an entry is present twice, It is an error.
  1409. * -1 if no entry found.
  1410. */
  1411. static int
  1412. search_my_index (const struct GNUNET_PeerIdentity *trail,
  1413. int trail_length)
  1414. {
  1415. int i;
  1416. int index_seen = trail_length + 1;
  1417. int flag = 0;
  1418. for (i = 0; i < trail_length; i++)
  1419. {
  1420. if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &trail[i]))
  1421. {
  1422. flag = 1;
  1423. if(index_seen == (trail_length + 1))
  1424. index_seen = i;
  1425. else
  1426. {
  1427. DEBUG("Entry is present twice in trail. Its not allowed\n");
  1428. }
  1429. break;
  1430. }
  1431. }
  1432. if (1 == flag)
  1433. return index_seen;
  1434. else
  1435. return -1;
  1436. }
  1437. /**
  1438. * Check if the friend is congested or have reached maximum number of trails
  1439. * it can be part of of.
  1440. * @param friend Friend to be checked.
  1441. * @return #GNUNET_NO if friend is not congested or have not crossed threshold.
  1442. * #GNUNET_YES if friend is either congested or have crossed threshold
  1443. */
  1444. static int
  1445. is_friend_congested (struct FriendInfo *friend)
  1446. {
  1447. if (( friend->trails_count < TRAILS_THROUGH_FRIEND_THRESHOLD) &&
  1448. ((0 == GNUNET_TIME_absolute_get_remaining
  1449. (friend->congestion_timestamp).rel_value_us)))
  1450. return GNUNET_NO;
  1451. else
  1452. return GNUNET_YES;
  1453. }
  1454. /**
  1455. * Select closest finger to value.
  1456. * @param peer1 First peer
  1457. * @param peer2 Second peer
  1458. * @param value Value to be compare
  1459. * @return Closest peer
  1460. */
  1461. static struct GNUNET_PeerIdentity
  1462. select_closest_finger (const struct GNUNET_PeerIdentity *peer1,
  1463. const struct GNUNET_PeerIdentity *peer2,
  1464. uint64_t value)
  1465. {
  1466. uint64_t peer1_value;
  1467. uint64_t peer2_value;
  1468. memcpy (&peer1_value, peer1, sizeof (uint64_t));
  1469. memcpy (&peer2_value, peer2, sizeof (uint64_t));
  1470. peer1_value = GNUNET_ntohll (peer1_value);
  1471. peer2_value = GNUNET_ntohll (peer2_value);
  1472. if (peer1_value == value)
  1473. {
  1474. return *peer1;
  1475. }
  1476. if (peer2_value == value)
  1477. {
  1478. return *peer2;
  1479. }
  1480. if (value < peer1_value && peer1_value < peer2_value)
  1481. {
  1482. return *peer1;
  1483. }
  1484. else if (value < peer2_value && peer2_value < peer1_value)
  1485. {
  1486. return *peer2;
  1487. }
  1488. else if (peer1_value < value && value < peer2_value)
  1489. {
  1490. return *peer2;
  1491. }
  1492. else if (peer2_value < value && value < peer1_value)
  1493. {
  1494. return *peer1;
  1495. }
  1496. else if (peer1_value < peer2_value && peer2_value < value)
  1497. {
  1498. return *peer1;
  1499. }
  1500. else // if (peer2_value < peer1_value && peer1_value < value)
  1501. {
  1502. return *peer2;
  1503. }
  1504. }
  1505. /**
  1506. * Select closest predecessor to value.
  1507. * @param peer1 First peer
  1508. * @param peer2 Second peer
  1509. * @param value Value to be compare
  1510. * @return Peer which precedes value in the network.
  1511. */
  1512. static struct GNUNET_PeerIdentity
  1513. select_closest_predecessor (const struct GNUNET_PeerIdentity *peer1,
  1514. const struct GNUNET_PeerIdentity *peer2,
  1515. uint64_t value)
  1516. {
  1517. uint64_t peer1_value;
  1518. uint64_t peer2_value;
  1519. memcpy (&peer1_value, peer1, sizeof (uint64_t));
  1520. memcpy (&peer2_value, peer2, sizeof (uint64_t));
  1521. peer1_value = GNUNET_ntohll (peer1_value);
  1522. peer2_value = GNUNET_ntohll (peer2_value);
  1523. if (peer1_value == value)
  1524. {
  1525. return *peer1;
  1526. }
  1527. if (peer2_value == value)
  1528. {
  1529. return *peer2;
  1530. }
  1531. if (value < peer1_value && peer1_value < peer2_value)
  1532. {
  1533. return *peer2;
  1534. }
  1535. else if (value < peer2_value && peer2_value < peer1_value)
  1536. {
  1537. return *peer1;
  1538. }
  1539. else if (peer1_value < value && value < peer2_value)
  1540. {
  1541. return *peer1;
  1542. }
  1543. else if (peer2_value < value && value < peer1_value)
  1544. {
  1545. return *peer2;
  1546. }
  1547. else if (peer1_value < peer2_value && peer2_value < value)
  1548. {
  1549. return *peer2;
  1550. }
  1551. else // if (peer2_value < peer1_value && peer1_value < value)
  1552. {
  1553. return *peer1;
  1554. }
  1555. }
  1556. #if 0
  1557. /**
  1558. *
  1559. *
  1560. */
  1561. void
  1562. test_print_trail (struct GNUNET_PeerIdentity *trail,
  1563. unsigned int trail_length)
  1564. {
  1565. struct GNUNET_PeerIdentity print_peer;
  1566. int i;
  1567. FPRINTF (stderr,_("\nSUPU %s, %s, %d,trail_length = %d"),
  1568. __FILE__, __func__,__LINE__,trail_length);
  1569. for (i =0 ; i< trail_length; i++)
  1570. {
  1571. print_peer = trail[i];
  1572. FPRINTF (stderr,_("\nSUPU %s, %s, %d,trail[%d]=%s"),
  1573. __FILE__, __func__,__LINE__,i,GNUNET_i2s(&print_peer));
  1574. }
  1575. }
  1576. #endif
  1577. #if 0
  1578. /**
  1579. * This is a test function to print all the entries of friend table.
  1580. */
  1581. static void
  1582. test_friend_peermap_print ()
  1583. {
  1584. struct FriendInfo *friend;
  1585. struct GNUNET_CONTAINER_MultiPeerMapIterator *friend_iter;
  1586. struct GNUNET_PeerIdentity print_peer;
  1587. struct GNUNET_PeerIdentity key_ret;
  1588. int i;
  1589. print_peer = my_identity;
  1590. FPRINTF (stderr,_("\nSUPU************ FRIEND_PEERMAP of %s"),GNUNET_i2s(&print_peer));
  1591. friend_iter = GNUNET_CONTAINER_multipeermap_iterator_create (friend_peermap);
  1592. for (i = 0; i < GNUNET_CONTAINER_multipeermap_size (friend_peermap); i++)
  1593. {
  1594. if(GNUNET_YES == GNUNET_CONTAINER_multipeermap_iterator_next (friend_iter,
  1595. &key_ret,
  1596. (const void **)&friend))
  1597. {
  1598. memcpy (&print_peer, &key_ret, sizeof (struct GNUNET_PeerIdentity));
  1599. FPRINTF (stderr,_("\nSUPU %s, %s, %d, friend = %s, friend->trails_count = %d"),
  1600. __FILE__, __func__,__LINE__, GNUNET_i2s(&print_peer), friend->trails_count);
  1601. }
  1602. }
  1603. }
  1604. #endif
  1605. #if 0
  1606. /**
  1607. * This is a test function, to print all the entries of finger table.
  1608. */
  1609. static void
  1610. test_finger_table_print()
  1611. {
  1612. struct FingerInfo *finger;
  1613. struct GNUNET_PeerIdentity print_peer;
  1614. //struct Trail *trail;
  1615. int i;
  1616. //int j;
  1617. //int k;
  1618. print_peer = my_identity;
  1619. FPRINTF (stderr,_("\nSUPU************ FINGER_TABLE of %s"),GNUNET_i2s(&print_peer));
  1620. for (i = 0; i < MAX_FINGERS; i++)
  1621. {
  1622. finger = &finger_table[i];
  1623. if (GNUNET_NO == finger->is_present)
  1624. continue;
  1625. print_peer = finger->finger_identity;
  1626. FPRINTF (stderr,_("\nSUPU %s, %s, %d, finger_table[%d] = %s, trails_count = %d"),
  1627. __FILE__, __func__,__LINE__,i,GNUNET_i2s (&print_peer), finger->trails_count);
  1628. #if 0
  1629. for (j = 0; j < finger->trails_count; j++)
  1630. {
  1631. trail = &finger->trail_list[j];
  1632. FPRINTF (stderr,_("\nSUPU %s, %s, %d, trail_id[%d]=%s"),__FILE__, __func__,__LINE__,j, GNUNET_h2s(&trail->trail_id));
  1633. struct Trail_Element *element;
  1634. element = trail->trail_head;
  1635. for (k = 0; k < trail->trail_length; k++)
  1636. {
  1637. print_peer = element->peer;
  1638. FPRINTF (stderr,_("\nSUPU %s, %s, %d,trail[%d] = %s "),__FILE__, __func__,__LINE__,k, GNUNET_i2s(&print_peer));
  1639. element = element->next;
  1640. }
  1641. }
  1642. #endif
  1643. }
  1644. }
  1645. #endif
  1646. /**
  1647. * Select the closest peer among two peers (which should not be same)
  1648. * with respect to value and finger_table_index
  1649. * NOTE: peer1 != peer2
  1650. * @param peer1 First peer
  1651. * @param peer2 Second peer
  1652. * @param value Value relative to which we find the closest
  1653. * @param is_predecessor Is value a predecessor or any other finger.
  1654. * @return Closest peer among two peers.
  1655. */
  1656. static struct GNUNET_PeerIdentity
  1657. select_closest_peer (const struct GNUNET_PeerIdentity *peer1,
  1658. const struct GNUNET_PeerIdentity *peer2,
  1659. uint64_t value,
  1660. unsigned int is_predecessor)
  1661. {
  1662. /* This check is here to ensure that calling function never sends
  1663. same peer value in peer1 and peer2. Remove it later. */
  1664. GNUNET_assert(0 != GNUNET_CRYPTO_cmp_peer_identity (peer1, peer2));
  1665. if (1 == is_predecessor)
  1666. return select_closest_predecessor (peer1, peer2, value);
  1667. // TODO: Change name to something like select_closest_successor!!
  1668. return select_closest_finger (peer1, peer2, value);
  1669. }
  1670. /**
  1671. * Iterate over the list of all the trails of a finger. In case the first
  1672. * friend to reach the finger has reached trail threshold or is congested,
  1673. * then don't select it. In case there multiple available good trails to reach
  1674. * to Finger, choose the one with shortest trail length.
  1675. * Note: We use length as parameter. But we can use any other suitable parameter
  1676. * also.
  1677. * @param finger Finger Finger whose trail we have to select.
  1678. * @return Trail Selected Trail.
  1679. */
  1680. static struct Trail *
  1681. select_finger_trail (struct FingerInfo *finger)
  1682. {
  1683. struct FriendInfo *friend;
  1684. struct Trail *current_finger_trail;
  1685. struct Trail *best_trail = NULL;
  1686. unsigned int i;
  1687. GNUNET_assert (finger->trails_count > 0);
  1688. for (i = 0; i < finger->trails_count; i++)
  1689. {
  1690. current_finger_trail = &finger->trail_list[i];
  1691. /* No trail stored at this index. */
  1692. if (GNUNET_NO == current_finger_trail->is_present)
  1693. continue;
  1694. GNUNET_assert (NULL !=
  1695. (friend =
  1696. GNUNET_CONTAINER_multipeermap_get (friend_peermap,
  1697. &current_finger_trail->trail_head->peer)));
  1698. /* First friend to reach trail is not free. */
  1699. if (GNUNET_YES == is_friend_congested (friend))
  1700. continue;
  1701. if (NULL == best_trail ||
  1702. best_trail->trail_length > current_finger_trail->trail_length)
  1703. {
  1704. best_trail = current_finger_trail;
  1705. }
  1706. }
  1707. return best_trail;
  1708. }
  1709. /**
  1710. * Compare FINGER entry with current successor. If finger's first friend of all
  1711. * its trail is not congested and has not crossed trail threshold, then check
  1712. * if finger peer identity is closer to final_destination_finger_value than
  1713. * current_successor. If yes then update current_successor.
  1714. * @param current_successor[in/out]
  1715. * @return
  1716. */
  1717. static void
  1718. compare_finger_and_current_closest_peer (struct Closest_Peer *current_closest_peer)
  1719. {
  1720. struct FingerInfo *finger;
  1721. struct GNUNET_PeerIdentity closest_peer;
  1722. struct Trail *finger_trail;
  1723. int i;
  1724. /* Iterate over finger table. */
  1725. for (i = 0; i < MAX_FINGERS; i++)
  1726. {
  1727. finger = &finger_table[i];
  1728. if (GNUNET_NO == finger->is_present)
  1729. continue;
  1730. if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger->finger_identity,
  1731. &current_closest_peer->best_known_destination))
  1732. continue;
  1733. /* If I am my own finger, then ignore this finger. */
  1734. if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger->finger_identity,
  1735. &my_identity))
  1736. continue;
  1737. /* If finger is a friend, we have already checked it in previous function. */
  1738. if (NULL != (GNUNET_CONTAINER_multipeermap_get (friend_peermap,
  1739. &finger->finger_identity)))
  1740. {
  1741. continue;
  1742. }
  1743. closest_peer = select_closest_peer (&finger->finger_identity,
  1744. &current_closest_peer->best_known_destination,
  1745. current_closest_peer->destination_finger_value,
  1746. current_closest_peer->is_predecessor);
  1747. if (0 == GNUNET_CRYPTO_cmp_peer_identity(&finger->finger_identity, &closest_peer))
  1748. {
  1749. /* Choose one of the trail to reach to finger. */
  1750. finger_trail = select_finger_trail (finger);
  1751. /* In case no trail found, ignore this finger. */
  1752. if (NULL == finger_trail)
  1753. continue;
  1754. current_closest_peer->best_known_destination = closest_peer;
  1755. current_closest_peer->next_hop = finger_trail->trail_head->peer;
  1756. current_closest_peer->trail_id = finger_trail->trail_id;
  1757. current_closest_peer->finger_table_index = i;
  1758. }
  1759. continue;
  1760. }
  1761. }
  1762. /**
  1763. * Compare friend entry with current successor.
  1764. * If friend identity and current_successor is same, then do nothing.
  1765. * If friend is not congested and has not crossed trail threshold, then check
  1766. * if friend peer identity is closer to final_destination_finger_value than
  1767. * current_successor. If yes then update current_successor.
  1768. * @param cls closure
  1769. * @param key current public key
  1770. * @param value struct Closest_Peer
  1771. * @return #GNUNET_YES if we should continue to iterate,
  1772. * #GNUNET_NO if not.
  1773. */
  1774. static int
  1775. compare_friend_and_current_closest_peer (void *cls,
  1776. const struct GNUNET_PeerIdentity *key,
  1777. void *value)
  1778. {
  1779. struct FriendInfo *friend = value;
  1780. struct Closest_Peer *current_closest_peer = cls;
  1781. struct GNUNET_PeerIdentity closest_peer;
  1782. /* Friend is either congested or has crossed threshold. */
  1783. if (GNUNET_YES == is_friend_congested (friend))
  1784. return GNUNET_YES;
  1785. /* If current_closest_peer and friend identity are same, then do nothing.*/
  1786. if (0 == GNUNET_CRYPTO_cmp_peer_identity (&friend->id,
  1787. &current_closest_peer->best_known_destination))
  1788. {
  1789. GNUNET_break (0);
  1790. return GNUNET_YES;
  1791. }
  1792. closest_peer = select_closest_peer (&friend->id,
  1793. &current_closest_peer->best_known_destination,
  1794. current_closest_peer->destination_finger_value,
  1795. current_closest_peer->is_predecessor);
  1796. /* Is friend the closest successor? */
  1797. if (0 == GNUNET_CRYPTO_cmp_peer_identity(&friend->id, &closest_peer))
  1798. {
  1799. current_closest_peer->best_known_destination = friend->id;
  1800. current_closest_peer->next_hop = friend->id;
  1801. }
  1802. return GNUNET_YES;
  1803. }
  1804. /**
  1805. * Initialize current_successor to my_identity.
  1806. * @param my_identity My peer identity
  1807. * @return Updated closest_peer
  1808. */
  1809. static struct Closest_Peer
  1810. init_closest_peer (struct GNUNET_PeerIdentity my_identity,
  1811. uint64_t destination_finger_value,
  1812. unsigned int is_predecessor)
  1813. {
  1814. struct Closest_Peer current_closest_peer;
  1815. memset (&current_closest_peer.trail_id, 0, sizeof(struct GNUNET_HashCode));
  1816. current_closest_peer.destination_finger_value = destination_finger_value;
  1817. current_closest_peer.is_predecessor = is_predecessor;
  1818. current_closest_peer.next_hop = my_identity;
  1819. current_closest_peer.best_known_destination = my_identity;
  1820. current_closest_peer.finger_table_index = 65; //65 is a for non valid finger table index.
  1821. return current_closest_peer;
  1822. }
  1823. /**
  1824. * Find locally best known peer, among your own identity, friend and finger list,
  1825. * which is closest to given destination_finger_value.
  1826. *
  1827. * NOTE: In case a friend is also a finger, then it is always chosen as friend
  1828. * not a finger.
  1829. * @param destination_finger_value Peer closest to this value will be the next destination.
  1830. * @param is_predecessor Are we looking for predecessor or finger?
  1831. * @return Closest_Peer that contains all the relevant field to reach to
  1832. * @a destination_finger_value
  1833. */
  1834. static struct Closest_Peer
  1835. find_local_best_known_next_hop (uint64_t destination_finger_value,
  1836. unsigned int is_predecessor)
  1837. {
  1838. struct Closest_Peer current_closest_peer;
  1839. /* Initialize current_successor to my_identity. */
  1840. current_closest_peer = init_closest_peer (my_identity,
  1841. destination_finger_value,
  1842. is_predecessor);
  1843. /* Compare each friend entry with current_successor and update current_successor
  1844. * with friend if its closest. */
  1845. GNUNET_assert
  1846. (GNUNET_SYSERR !=
  1847. GNUNET_CONTAINER_multipeermap_iterate (friend_peermap,
  1848. &compare_friend_and_current_closest_peer,
  1849. &current_closest_peer));
  1850. /* Compare each finger entry with current_successor and update current_successor
  1851. * with finger if its closest. */
  1852. compare_finger_and_current_closest_peer (&current_closest_peer);
  1853. return current_closest_peer;
  1854. }
  1855. /**
  1856. * Construct a Put message and send it to target_peer.
  1857. * @param key Key for the content
  1858. * @param block_type Type of the block
  1859. * @param options Routing options
  1860. * @param desired_replication_level Desired replication count
  1861. * @param best_known_dest Peer to which this message should reach eventually,
  1862. * as it is best known destination to me.
  1863. * @param intermediate_trail_id Trail id in case
  1864. * @param target_peer Peer to which this message will be forwarded.
  1865. * @param hop_count Number of hops traversed so far.
  1866. * @param put_path_length Total number of peers in @a put_path
  1867. * @param put_path Number of peers traversed so far
  1868. * @param expiration_time When does the content expire
  1869. * @param data Content to store
  1870. * @param data_size Size of content @a data in bytes
  1871. */
  1872. void
  1873. GDS_NEIGHBOURS_send_put (const struct GNUNET_HashCode *key,
  1874. enum GNUNET_BLOCK_Type block_type,
  1875. enum GNUNET_DHT_RouteOption options,
  1876. uint32_t desired_replication_level,
  1877. struct GNUNET_PeerIdentity best_known_dest,
  1878. struct GNUNET_HashCode intermediate_trail_id,
  1879. struct GNUNET_PeerIdentity *target_peer,
  1880. uint32_t hop_count,
  1881. uint32_t put_path_length,
  1882. struct GNUNET_PeerIdentity *put_path,
  1883. struct GNUNET_TIME_Absolute expiration_time,
  1884. const void *data, size_t data_size)
  1885. {
  1886. struct PeerPutMessage *ppm;
  1887. struct P2PPendingMessage *pending;
  1888. struct FriendInfo *target_friend;
  1889. struct GNUNET_PeerIdentity *pp;
  1890. size_t msize;
  1891. msize = put_path_length * sizeof (struct GNUNET_PeerIdentity) + data_size +
  1892. sizeof (struct PeerPutMessage);
  1893. if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
  1894. {
  1895. put_path_length = 0;
  1896. msize = data_size + sizeof (struct PeerPutMessage);
  1897. }
  1898. if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
  1899. {
  1900. DEBUG("msize = %lu\n",msize);
  1901. GNUNET_break (0);
  1902. return;
  1903. }
  1904. GNUNET_assert (NULL !=
  1905. (target_friend =
  1906. GNUNET_CONTAINER_multipeermap_get (friend_peermap, target_peer)));
  1907. pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
  1908. pending->timeout = expiration_time;
  1909. ppm = (struct PeerPutMessage *) &pending[1];
  1910. pending->msg = &ppm->header;
  1911. ppm->header.size = htons (msize);
  1912. ppm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_PUT);
  1913. ppm->options = htonl (options);
  1914. ppm->block_type = htonl (block_type);
  1915. ppm->hop_count = htonl (hop_count + 1);
  1916. ppm->desired_replication_level = htonl (desired_replication_level);
  1917. ppm->expiration_time = GNUNET_TIME_absolute_hton (expiration_time);
  1918. ppm->best_known_destination = best_known_dest;
  1919. ppm->intermediate_trail_id = intermediate_trail_id;
  1920. ppm->key = *key;
  1921. pp = (struct GNUNET_PeerIdentity *) &ppm[1];
  1922. ppm->put_path_length = htonl (put_path_length);
  1923. if(put_path_length > 0)
  1924. {
  1925. memcpy (pp, put_path,
  1926. sizeof (struct GNUNET_PeerIdentity) * put_path_length);
  1927. }
  1928. memcpy (&pp[put_path_length], data, data_size);
  1929. GNUNET_assert (NULL != target_friend);
  1930. GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
  1931. target_friend->pending_count++;
  1932. process_friend_queue (target_friend);
  1933. }
  1934. /**
  1935. * Handle the put request from the client.
  1936. * @param key Key for the content
  1937. * @param block_type Type of the block
  1938. * @param options Routing options
  1939. * @param desired_replication_level Desired replication count
  1940. * @param expiration_time When does the content expire
  1941. * @param data Content to store
  1942. * @param data_size Size of content @a data in bytes
  1943. */
  1944. void
  1945. GDS_NEIGHBOURS_handle_put (const struct GNUNET_HashCode *key,
  1946. enum GNUNET_BLOCK_Type block_type,
  1947. enum GNUNET_DHT_RouteOption options,
  1948. uint32_t desired_replication_level,
  1949. struct GNUNET_TIME_Absolute expiration_time,
  1950. const void *data, size_t data_size)
  1951. {
  1952. struct GNUNET_PeerIdentity best_known_dest;
  1953. struct GNUNET_HashCode intermediate_trail_id;
  1954. struct GNUNET_PeerIdentity next_hop;
  1955. uint64_t key_value;
  1956. struct Closest_Peer successor;
  1957. memcpy (&key_value, key, sizeof (uint64_t));
  1958. key_value = GNUNET_ntohll (key_value);
  1959. successor = find_local_best_known_next_hop (key_value,
  1960. GDS_FINGER_TYPE_NON_PREDECESSOR);
  1961. best_known_dest = successor.best_known_destination;
  1962. next_hop = successor.next_hop;
  1963. intermediate_trail_id = successor.trail_id;
  1964. if (0 == GNUNET_CRYPTO_cmp_peer_identity (&best_known_dest, &my_identity))
  1965. {
  1966. DEBUG("\n PUT_REQUEST_SUCCESSFUL for key = %s",GNUNET_h2s(key));
  1967. /* I am the destination. */
  1968. GDS_DATACACHE_handle_put (expiration_time, key, 0, NULL,
  1969. block_type,data_size,data);
  1970. GDS_CLIENTS_process_put (options, block_type, 0,
  1971. ntohl (desired_replication_level),
  1972. 1, &my_identity, expiration_time, //FIXME: GNUNETnthoh something on expiration time.
  1973. key, data, data_size);
  1974. return;
  1975. }
  1976. /* In case we are sending the request to a finger, then send across all of its
  1977. trail.*/
  1978. #if ENABLE_MALICIOUS
  1979. if (0 != GNUNET_CRYPTO_cmp_peer_identity (&successor.best_known_destination,
  1980. &successor.next_hop))
  1981. {
  1982. struct FingerInfo *next_hop_finger;
  1983. unsigned int i;
  1984. next_hop_finger = &finger_table[successor.finger_table_index];
  1985. for (i = 0; i < next_hop_finger->trails_count; i++)
  1986. {
  1987. if (GNUNET_YES == next_hop_finger->trail_list[i].is_present)
  1988. {
  1989. if(0 == next_hop_finger->trail_list[i].trail_length)
  1990. {
  1991. GDS_NEIGHBOURS_send_put (key, block_type, options, desired_replication_level,
  1992. best_known_dest, intermediate_trail_id, &next_hop,
  1993. 0, 1, &my_identity, expiration_time,
  1994. data, data_size);
  1995. return;
  1996. }
  1997. next_hop = next_hop_finger->trail_list[i].trail_head->peer;
  1998. GDS_NEIGHBOURS_send_put (key, block_type, options, desired_replication_level,
  1999. best_known_dest,
  2000. next_hop_finger->trail_list[i].trail_id,
  2001. &next_hop, 0, 1, &my_identity,
  2002. expiration_time,
  2003. data, data_size);
  2004. }
  2005. }
  2006. return;
  2007. }
  2008. #endif
  2009. GDS_NEIGHBOURS_send_put (key, block_type, options, desired_replication_level,
  2010. best_known_dest, intermediate_trail_id, &next_hop,
  2011. 0, 1, &my_identity, expiration_time,
  2012. data, data_size);
  2013. }
  2014. /**
  2015. * Construct a Get message and send it to target_peer.
  2016. * @param key Key for the content
  2017. * @param block_type Type of the block
  2018. * @param options Routing options
  2019. * @param desired_replication_level Desired replication count
  2020. * @param best_known_dest Peer which should get this message. Same as target peer
  2021. * if best_known_dest is a friend else its a finger.
  2022. * @param intermediate_trail_id Trail id to reach to @a best_known_dest
  2023. * in case it is a finger else set to 0.
  2024. * @param target_peer Peer to which this message will be forwarded.
  2025. * @param hop_count Number of hops traversed so far.
  2026. * @param data Content to store
  2027. * @param data_size Size of content @a data in bytes
  2028. * @param get_path_length Total number of peers in @a get_path
  2029. * @param get_path Number of peers traversed so far
  2030. */
  2031. void
  2032. GDS_NEIGHBOURS_send_get (const struct GNUNET_HashCode *key,
  2033. enum GNUNET_BLOCK_Type block_type,
  2034. enum GNUNET_DHT_RouteOption options,
  2035. uint32_t desired_replication_level,
  2036. struct GNUNET_PeerIdentity best_known_dest,
  2037. struct GNUNET_HashCode intermediate_trail_id,
  2038. struct GNUNET_PeerIdentity *target_peer,
  2039. uint32_t hop_count,
  2040. uint32_t get_path_length,
  2041. struct GNUNET_PeerIdentity *get_path)
  2042. {
  2043. struct PeerGetMessage *pgm;
  2044. struct P2PPendingMessage *pending;
  2045. struct FriendInfo *target_friend;
  2046. struct GNUNET_PeerIdentity *gp;
  2047. size_t msize;
  2048. msize = sizeof (struct PeerGetMessage) +
  2049. (get_path_length * sizeof (struct GNUNET_PeerIdentity));
  2050. if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
  2051. {
  2052. GNUNET_break (0);
  2053. return;
  2054. }
  2055. GNUNET_assert (NULL !=
  2056. (target_friend =
  2057. GNUNET_CONTAINER_multipeermap_get (friend_peermap, target_peer)));
  2058. pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
  2059. pending->timeout = GNUNET_TIME_relative_to_absolute (PENDING_MESSAGE_TIMEOUT);
  2060. pending->importance = 0; /* FIXME */
  2061. pgm = (struct PeerGetMessage *) &pending[1];
  2062. pending->msg = &pgm->header;
  2063. pgm->header.size = htons (msize);
  2064. pgm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_GET);
  2065. pgm->get_path_length = htonl (get_path_length);
  2066. pgm->best_known_destination = best_known_dest;
  2067. pgm->key = *key;
  2068. pgm->intermediate_trail_id = intermediate_trail_id;
  2069. pgm->hop_count = htonl (hop_count + 1);
  2070. pgm->get_path_length = htonl (get_path_length);
  2071. gp = (struct GNUNET_PeerIdentity *) &pgm[1];
  2072. memcpy (gp, get_path,
  2073. sizeof (struct GNUNET_PeerIdentity) * get_path_length);
  2074. GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
  2075. target_friend->pending_count++;
  2076. process_friend_queue (target_friend);
  2077. }
  2078. /**
  2079. * Handle the get request from the client file. If I am destination do
  2080. * datacache put and return. Else find the target friend and forward message
  2081. * to it.
  2082. * @param key Key for the content
  2083. * @param block_type Type of the block
  2084. * @param options Routing options
  2085. * @param desired_replication_level Desired replication count
  2086. */
  2087. void
  2088. GDS_NEIGHBOURS_handle_get(const struct GNUNET_HashCode *key,
  2089. enum GNUNET_BLOCK_Type block_type,
  2090. enum GNUNET_DHT_RouteOption options,
  2091. uint32_t desired_replication_level)
  2092. {
  2093. struct Closest_Peer successor;
  2094. struct GNUNET_PeerIdentity best_known_dest;
  2095. struct GNUNET_HashCode intermediate_trail_id;
  2096. uint64_t key_value;
  2097. memcpy (&key_value, key, sizeof (uint64_t));
  2098. key_value = GNUNET_ntohll (key_value);
  2099. successor = find_local_best_known_next_hop (key_value,
  2100. GDS_FINGER_TYPE_NON_PREDECESSOR);
  2101. best_known_dest = successor.best_known_destination;
  2102. intermediate_trail_id = successor.trail_id;
  2103. /* I am the destination. I have the data. */
  2104. if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity,
  2105. &best_known_dest))
  2106. {
  2107. GDS_DATACACHE_handle_get (key,block_type, NULL, 0,
  2108. NULL, 0, 1, &my_identity, NULL,&my_identity);
  2109. return;
  2110. }
  2111. #if ENABLE_MALICIOUS
  2112. struct GNUNET_PeerIdentity next_hop;
  2113. if (0 != GNUNET_CRYPTO_cmp_peer_identity (&successor.best_known_destination,
  2114. &successor.next_hop))
  2115. {
  2116. struct FingerInfo *next_hop_finger;
  2117. unsigned int i;
  2118. next_hop_finger = &finger_table[successor.finger_table_index];
  2119. for (i = 0; i < next_hop_finger->trails_count; i++)
  2120. {
  2121. if (GNUNET_YES == next_hop_finger->trail_list[i].is_present)
  2122. {
  2123. if(0 == next_hop_finger->trail_list[i].trail_length)
  2124. {
  2125. GDS_NEIGHBOURS_send_get (key, block_type, options,
  2126. desired_replication_level,
  2127. best_known_dest,intermediate_trail_id,
  2128. &successor.next_hop,
  2129. 0, 1, &my_identity);
  2130. return;
  2131. }
  2132. next_hop = next_hop_finger->trail_list[i].trail_head->peer;
  2133. GDS_NEIGHBOURS_send_get (key, block_type, options, desired_replication_level,
  2134. best_known_dest,
  2135. next_hop_finger->trail_list[i].trail_id,
  2136. &next_hop, 0, 1, &my_identity);
  2137. }
  2138. }
  2139. return;
  2140. }
  2141. #endif
  2142. GDS_NEIGHBOURS_send_get (key, block_type, options, desired_replication_level,
  2143. best_known_dest,intermediate_trail_id, &successor.next_hop,
  2144. 0, 1, &my_identity);
  2145. }
  2146. /**
  2147. * Send the get result to requesting client.
  2148. *
  2149. * @param key Key of the requested data.
  2150. * @param type Block type
  2151. * @param target_peer Next peer to forward the message to.
  2152. * @param source_peer Peer which has the data for the key.
  2153. * @param put_path_length Number of peers in @a put_path
  2154. * @param put_path Path taken to put the data at its stored location.
  2155. * @param get_path_length Number of peers in @a get_path
  2156. * @param get_path Path taken to reach to the location of the key.
  2157. * @param expiration When will this result expire?
  2158. * @param data Payload to store
  2159. * @param data_size Size of the @a data
  2160. */
  2161. void
  2162. GDS_NEIGHBOURS_send_get_result (const struct GNUNET_HashCode *key,
  2163. enum GNUNET_BLOCK_Type type,
  2164. const struct GNUNET_PeerIdentity *target_peer,
  2165. const struct GNUNET_PeerIdentity *source_peer,
  2166. unsigned int put_path_length,
  2167. const struct GNUNET_PeerIdentity *put_path,
  2168. unsigned int get_path_length,
  2169. const struct GNUNET_PeerIdentity *get_path,
  2170. struct GNUNET_TIME_Absolute expiration,
  2171. const void *data, size_t data_size)
  2172. {
  2173. struct PeerGetResultMessage *get_result;
  2174. struct GNUNET_PeerIdentity *paths;
  2175. struct P2PPendingMessage *pending;
  2176. struct FriendInfo *target_friend;
  2177. int current_path_index;
  2178. size_t msize;
  2179. msize = (put_path_length + get_path_length )* sizeof (struct GNUNET_PeerIdentity) +
  2180. data_size +
  2181. sizeof (struct PeerGetResultMessage);
  2182. if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
  2183. {
  2184. put_path_length = 0;
  2185. msize = msize - put_path_length * sizeof (struct GNUNET_PeerIdentity);
  2186. }
  2187. if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
  2188. {
  2189. GNUNET_break(0);
  2190. return;
  2191. }
  2192. current_path_index = 0;
  2193. if(get_path_length > 0)
  2194. {
  2195. current_path_index = search_my_index(get_path, get_path_length);
  2196. if (-1 == current_path_index)
  2197. {
  2198. GNUNET_break (0);
  2199. return;
  2200. }
  2201. if ((get_path_length + 1) == current_path_index)
  2202. {
  2203. DEBUG ("Peer found twice in get path. Not allowed \n");
  2204. GNUNET_break (0);
  2205. return;
  2206. }
  2207. }
  2208. if (0 == current_path_index)
  2209. {
  2210. DEBUG ("GET_RESULT TO CLIENT KEY = %s, Peer = %s",GNUNET_h2s(key),GNUNET_i2s(&my_identity));
  2211. GDS_CLIENTS_handle_reply (expiration, key, get_path_length,
  2212. get_path, put_path_length,
  2213. put_path, type, data_size, data);
  2214. return;
  2215. }
  2216. pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
  2217. pending->timeout = GNUNET_TIME_relative_to_absolute (PENDING_MESSAGE_TIMEOUT);
  2218. pending->importance = 0;
  2219. get_result = (struct PeerGetResultMessage *)&pending[1];
  2220. pending->msg = &get_result->header;
  2221. get_result->header.size = htons (msize);
  2222. get_result->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_GET_RESULT);
  2223. get_result->key = *key;
  2224. get_result->querying_peer = *source_peer;
  2225. get_result->expiration_time = expiration;
  2226. get_result->get_path_length = htonl (get_path_length);
  2227. get_result->put_path_length = htonl (put_path_length);
  2228. paths = (struct GNUNET_PeerIdentity *)&get_result[1];
  2229. memcpy (paths, put_path,
  2230. put_path_length * sizeof (struct GNUNET_PeerIdentity));
  2231. memcpy (&paths[put_path_length], get_path,
  2232. get_path_length * sizeof (struct GNUNET_PeerIdentity));
  2233. memcpy (&paths[put_path_length + get_path_length], data, data_size);
  2234. GNUNET_assert (NULL !=
  2235. (target_friend =
  2236. GNUNET_CONTAINER_multipeermap_get (friend_peermap,
  2237. &get_path[current_path_index - 1])));
  2238. GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
  2239. target_friend->pending_count++;
  2240. process_friend_queue (target_friend);
  2241. }
  2242. /**
  2243. * Randomly choose one of your friends (which is not congested and have not crossed
  2244. * trail threshold) from the friend_peermap
  2245. * @return Friend Randomly chosen friend.
  2246. * NULL in case friend peermap is empty, or all the friends are either
  2247. * congested or have crossed trail threshold.
  2248. */
  2249. static struct FriendInfo *
  2250. select_random_friend ()
  2251. {
  2252. unsigned int current_size;
  2253. uint32_t index;
  2254. unsigned int j = 0;
  2255. struct GNUNET_CONTAINER_MultiPeerMapIterator *iter;
  2256. struct GNUNET_PeerIdentity key_ret;
  2257. struct FriendInfo *friend;
  2258. current_size = GNUNET_CONTAINER_multipeermap_size (friend_peermap);
  2259. /* No friends.*/
  2260. if (0 == current_size)
  2261. return NULL;
  2262. index = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, current_size);
  2263. iter = GNUNET_CONTAINER_multipeermap_iterator_create (friend_peermap);
  2264. /* Iterate till you don't reach to index. */
  2265. for (j = 0; j < index ; j++)
  2266. GNUNET_assert (GNUNET_YES ==
  2267. GNUNET_CONTAINER_multipeermap_iterator_next (iter, NULL, NULL));
  2268. do
  2269. {
  2270. /* Reset the index in friend peermap to 0 as we reached to the end. */
  2271. if (j == current_size)
  2272. {
  2273. j = 0;
  2274. GNUNET_CONTAINER_multipeermap_iterator_destroy (iter);
  2275. iter = GNUNET_CONTAINER_multipeermap_iterator_create (friend_peermap);
  2276. }
  2277. /* Get the friend stored at the index, j*/
  2278. GNUNET_assert (GNUNET_YES ==
  2279. GNUNET_CONTAINER_multipeermap_iterator_next (iter,
  2280. &key_ret,
  2281. (const void **)&friend));
  2282. /* This friend is not congested and has not crossed trail threshold. */
  2283. if ((friend->trails_count < TRAILS_THROUGH_FRIEND_THRESHOLD) &&
  2284. (0 == GNUNET_TIME_absolute_get_remaining (friend->congestion_timestamp).rel_value_us))
  2285. {
  2286. break;
  2287. }
  2288. friend = NULL;
  2289. j++;
  2290. } while (j != index);
  2291. GNUNET_CONTAINER_multipeermap_iterator_destroy (iter);
  2292. return friend;
  2293. }
  2294. /**
  2295. * Compute 64 bit value of finger_identity corresponding to a finger index using
  2296. * chord formula.
  2297. * For all fingers, n.finger[i] = n + pow (2,i),
  2298. * For predecessor, n.finger[PREDECESSOR_FINGER_ID] = n - 1, where
  2299. * n = my_identity, i = finger_index, n.finger[i] = 64 bit finger value
  2300. * @param finger_index Index corresponding to which we calculate 64 bit value.
  2301. * @return 64 bit value.
  2302. */
  2303. static uint64_t
  2304. compute_finger_identity_value (unsigned int finger_index)
  2305. {
  2306. uint64_t my_id64;
  2307. memcpy (&my_id64, &my_identity, sizeof (uint64_t));
  2308. my_id64 = GNUNET_ntohll (my_id64);
  2309. /* Are we looking for immediate predecessor? */
  2310. if (PREDECESSOR_FINGER_ID == finger_index)
  2311. return (my_id64 - 1);
  2312. else
  2313. {
  2314. uint64_t add = (uint64_t)1 << finger_index;
  2315. return (my_id64 + add);
  2316. }
  2317. }
  2318. /*
  2319. * Choose a random friend. Calculate the next finger identity to search,from
  2320. * current_search_finger_index. Start looking for the trail to reach to
  2321. * finger identity through this random friend.
  2322. *
  2323. * @param cls closure for this task
  2324. * @param tc the context under which the task is running
  2325. */
  2326. static void
  2327. send_find_finger_trail_message (void *cls,
  2328. const struct GNUNET_SCHEDULER_TaskContext *tc)
  2329. {
  2330. struct FriendInfo *target_friend;
  2331. struct GNUNET_HashCode trail_id;
  2332. struct GNUNET_HashCode intermediate_trail_id;
  2333. unsigned int is_predecessor = 0;
  2334. uint64_t finger_id_value;
  2335. /* Schedule another send_find_finger_trail_message task. After one round of
  2336. * finger search, this time is exponentially backoff. */
  2337. find_finger_trail_task_next_send_time.rel_value_us =
  2338. find_finger_trail_task_next_send_time.rel_value_us +
  2339. GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
  2340. DHT_FIND_FINGER_TRAIL_INTERVAL.rel_value_us);
  2341. find_finger_trail_task =
  2342. GNUNET_SCHEDULER_add_delayed (find_finger_trail_task_next_send_time,
  2343. &send_find_finger_trail_message,
  2344. NULL);
  2345. /* No space in my routing table. (Source and destination peers also store entries
  2346. * in their routing table). */
  2347. if (GNUNET_YES == GDS_ROUTING_threshold_reached())
  2348. return;
  2349. target_friend = select_random_friend ();
  2350. if (NULL == target_friend)
  2351. {
  2352. return;
  2353. }
  2354. finger_id_value = compute_finger_identity_value (current_search_finger_index);
  2355. if (PREDECESSOR_FINGER_ID == current_search_finger_index)
  2356. is_predecessor = 1;
  2357. /* Generate a unique trail id for trail we are trying to setup. */
  2358. GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_STRONG,
  2359. &trail_id, sizeof (trail_id));
  2360. memset(&intermediate_trail_id, 0, sizeof (struct GNUNET_HashCode));
  2361. GDS_NEIGHBOURS_send_trail_setup (my_identity, finger_id_value,
  2362. target_friend->id, target_friend, 0, NULL,
  2363. is_predecessor, trail_id,
  2364. intermediate_trail_id);
  2365. }
  2366. /**
  2367. * In case there are already maximum number of possible trails to reach to a
  2368. * finger, then check if the new trail's length is lesser than any of the
  2369. * existing trails.
  2370. * If yes then replace that old trail by new trail.
  2371. *
  2372. * Note: Here we are taking length as a parameter to choose the best possible
  2373. * trail, but there could be other parameters also like:
  2374. * 1. duration of existence of a trail - older the better.
  2375. * 2. if the new trail is completely disjoint than the
  2376. * other trails, then may be choosing it is better.
  2377. *
  2378. * @param finger Finger
  2379. * @param new_finger_trail List of peers to reach from me to @a finger, NOT
  2380. * including the endpoints.
  2381. * @param new_finger_trail_length Total number of peers in @a new_finger_trail
  2382. * @param new_finger_trail_id Unique identifier of @a new_finger_trail.
  2383. */
  2384. static void
  2385. select_and_replace_trail (struct FingerInfo *finger,
  2386. const struct GNUNET_PeerIdentity *new_trail,
  2387. unsigned int new_trail_length,
  2388. struct GNUNET_HashCode new_trail_id)
  2389. {
  2390. struct Trail *current_trail;
  2391. unsigned int largest_trail_length;
  2392. unsigned int largest_trail_index;
  2393. struct Trail_Element *trail_element;
  2394. struct GNUNET_PeerIdentity *next_hop;
  2395. unsigned int i;
  2396. largest_trail_length = new_trail_length;
  2397. largest_trail_index = MAXIMUM_TRAILS_PER_FINGER + 1;
  2398. GNUNET_assert (MAXIMUM_TRAILS_PER_FINGER == finger->trails_count);
  2399. for (i = 0; i < finger->trails_count; i++)
  2400. {
  2401. current_trail = &finger->trail_list[i];
  2402. GNUNET_assert (GNUNET_YES == current_trail->is_present);
  2403. if (current_trail->trail_length > largest_trail_length)
  2404. {
  2405. largest_trail_length = current_trail->trail_length;
  2406. largest_trail_index = i;
  2407. }
  2408. }
  2409. /* New trail is not better than existing ones. Send trail teardown. */
  2410. if (largest_trail_index == (MAXIMUM_TRAILS_PER_FINGER + 1))
  2411. {
  2412. next_hop = GDS_ROUTING_get_next_hop (new_trail_id, GDS_ROUTING_SRC_TO_DEST);
  2413. GDS_ROUTING_remove_trail (new_trail_id);
  2414. GDS_NEIGHBOURS_send_trail_teardown (&new_trail_id,
  2415. GDS_ROUTING_SRC_TO_DEST,
  2416. next_hop);
  2417. return;
  2418. }
  2419. /* Send trail teardown message across the replaced trail. */
  2420. struct Trail *replace_trail = &finger->trail_list[largest_trail_index];
  2421. next_hop = GDS_ROUTING_get_next_hop (replace_trail->trail_id, GDS_ROUTING_SRC_TO_DEST);
  2422. GNUNET_assert (GNUNET_YES == GDS_ROUTING_remove_trail (replace_trail->trail_id));
  2423. GDS_NEIGHBOURS_send_trail_teardown (&replace_trail->trail_id,
  2424. GDS_ROUTING_SRC_TO_DEST,
  2425. next_hop);
  2426. /* Free the trail. */
  2427. while (NULL != (trail_element = replace_trail->trail_head))
  2428. {
  2429. GNUNET_CONTAINER_DLL_remove (replace_trail->trail_head,
  2430. replace_trail->trail_tail, trail_element);
  2431. GNUNET_free_non_null (trail_element);
  2432. }
  2433. /* Add new trial at that location. */
  2434. replace_trail->is_present = GNUNET_YES;
  2435. replace_trail->trail_length = new_trail_length;
  2436. replace_trail->trail_id = new_trail_id;
  2437. for (i = 0; i < new_trail_length; i++)
  2438. {
  2439. struct Trail_Element *element = GNUNET_new (struct Trail_Element);
  2440. element->peer = new_trail[i];
  2441. GNUNET_CONTAINER_DLL_insert_tail (replace_trail->trail_head,
  2442. replace_trail->trail_tail,
  2443. element);
  2444. }
  2445. /* FIXME: URGENT Are we adding the trail back to the list. */
  2446. }
  2447. /**
  2448. * Check if the new trail to reach to finger is unique or do we already have
  2449. * such a trail present for finger.
  2450. * @param existing_finger Finger identity
  2451. * @param new_trail New trail to reach @a existing_finger
  2452. * @param trail_length Total number of peers in new_trail.
  2453. * @return #GNUNET_YES if the new trail is unique
  2454. * #GNUNET_NO if same trail is already present.
  2455. */
  2456. static int
  2457. is_new_trail_unique (struct FingerInfo *existing_finger,
  2458. const struct GNUNET_PeerIdentity *new_trail,
  2459. unsigned int trail_length)
  2460. {
  2461. struct Trail *current_trail;
  2462. struct Trail_Element *trail_element;
  2463. int i;
  2464. int j;
  2465. GNUNET_assert (existing_finger->trails_count > 0);
  2466. /* Iterate over list of trails. */
  2467. for (i = 0; i < existing_finger->trails_count; i++)
  2468. {
  2469. current_trail = &(existing_finger->trail_list[i]);
  2470. if(GNUNET_NO == current_trail->is_present)
  2471. continue;
  2472. /* New trail and existing trail length are not same. */
  2473. if (current_trail->trail_length != trail_length)
  2474. {
  2475. return GNUNET_YES;
  2476. }
  2477. trail_element = current_trail->trail_head;
  2478. for (j = 0; j < current_trail->trail_length; j++)
  2479. {
  2480. if (0 != GNUNET_CRYPTO_cmp_peer_identity (&new_trail[j],
  2481. &trail_element->peer))
  2482. {
  2483. return GNUNET_YES;
  2484. }
  2485. trail_element = trail_element->next;
  2486. }
  2487. }
  2488. return GNUNET_NO;
  2489. }
  2490. /**
  2491. * FIXME; In case of multiple trails, we may have a case where a trail from in
  2492. * between has been removed, then we should try to find a free slot , not simply
  2493. * add a trail at then end of the list.
  2494. * Add a new trail at a free slot in trail array of existing finger.
  2495. * @param existing_finger Finger
  2496. * @param new_finger_trail New trail from me to finger, NOT including endpoints
  2497. * @param new_finger_trail_length Total number of peers in @a new_finger_trail
  2498. * @param new_finger_trail_id Unique identifier of the trail.
  2499. */
  2500. static void
  2501. add_new_trail (struct FingerInfo *existing_finger,
  2502. const struct GNUNET_PeerIdentity *new_trail,
  2503. unsigned int new_trail_length,
  2504. struct GNUNET_HashCode new_trail_id)
  2505. {
  2506. struct FriendInfo *friend;
  2507. struct Trail *trail;
  2508. unsigned int i;
  2509. int free_slot = -1;
  2510. if (GNUNET_NO == is_new_trail_unique (existing_finger, new_trail,
  2511. new_trail_length))
  2512. return;
  2513. for (i = 0; i < existing_finger->trails_count; i++)
  2514. {
  2515. if (GNUNET_NO == existing_finger->trail_list[i].is_present)
  2516. {
  2517. free_slot = i;
  2518. break;
  2519. }
  2520. }
  2521. if (-1 == free_slot)
  2522. free_slot = i;
  2523. trail = &existing_finger->trail_list[free_slot];
  2524. GNUNET_assert (GNUNET_NO == trail->is_present);
  2525. trail->trail_id = new_trail_id;
  2526. trail->trail_length = new_trail_length;
  2527. existing_finger->trails_count++;
  2528. trail->is_present = GNUNET_YES;
  2529. if (0 == new_trail_length)
  2530. {
  2531. friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
  2532. &existing_finger->finger_identity);
  2533. }
  2534. else
  2535. {
  2536. friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
  2537. &new_trail[0]);
  2538. }
  2539. friend->trails_count++;
  2540. for (i = 0; i < new_trail_length; i++)
  2541. {
  2542. struct Trail_Element *element;
  2543. element = GNUNET_new (struct Trail_Element);
  2544. element->peer = new_trail[i];
  2545. GNUNET_CONTAINER_DLL_insert_tail (trail->trail_head,
  2546. trail->trail_tail,
  2547. element);
  2548. }
  2549. existing_finger->trail_list[free_slot].trail_head = trail->trail_head;
  2550. existing_finger->trail_list[free_slot].trail_tail = trail->trail_tail;
  2551. existing_finger->trail_list[free_slot].trail_length = new_trail_length;
  2552. existing_finger->trail_list[free_slot].trail_id = new_trail_id;
  2553. existing_finger->trail_list[free_slot].is_present = GNUNET_YES;
  2554. }
  2555. #if 0
  2556. /**
  2557. * FIXME; In case of multiple trails, we may have a case where a trail from in
  2558. * between has been removed, then we should try to find a free slot , not simply
  2559. * add a trail at then end of the list.
  2560. * Add a new trail at a free slot in trail array of existing finger.
  2561. * @param existing_finger Finger
  2562. * @param new_finger_trail New trail from me to finger, NOT including endpoints
  2563. * @param new_finger_trail_length Total number of peers in @a new_finger_trail
  2564. * @param new_finger_trail_id Unique identifier of the trail.
  2565. */
  2566. static void
  2567. add_new_trail (struct FingerInfo *existing_finger,
  2568. const struct GNUNET_PeerIdentity *new_trail,
  2569. unsigned int new_trail_length,
  2570. struct GNUNET_HashCode new_trail_id)
  2571. {
  2572. struct Trail *trail;
  2573. struct FriendInfo *first_friend;
  2574. int i;
  2575. int index;
  2576. if (GNUNET_NO == is_new_trail_unique (existing_finger, new_trail,
  2577. new_trail_length))
  2578. return;
  2579. index = existing_finger->trails_count;
  2580. trail = &existing_finger->trail_list[index];
  2581. GNUNET_assert (GNUNET_NO == trail->is_present);
  2582. trail->trail_id = new_trail_id;
  2583. trail->trail_length = new_trail_length;
  2584. existing_finger->trails_count++;
  2585. trail->is_present = GNUNET_YES;
  2586. GNUNET_assert (NULL == (GNUNET_CONTAINER_multipeermap_get (friend_peermap,
  2587. &existing_finger->finger_identity)));
  2588. /* If finger is a friend then we never call this function. */
  2589. GNUNET_assert (new_trail_length > 0);
  2590. first_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
  2591. &new_trail[0]);
  2592. first_friend->trails_count++;
  2593. for (i = 0; i < new_trail_length; i++)
  2594. {
  2595. struct Trail_Element *element;
  2596. element = GNUNET_new (struct Trail_Element);
  2597. element->peer = new_trail[i];
  2598. GNUNET_CONTAINER_DLL_insert_tail (trail->trail_head,
  2599. trail->trail_tail,
  2600. element);
  2601. }
  2602. /* Do we need to add trail head and trail tail in the trail list itearator.*/
  2603. existing_finger->trail_list[index].trail_head = trail->trail_head;
  2604. existing_finger->trail_list[index].trail_tail = trail->trail_tail;
  2605. existing_finger->trail_list[index].trail_length = new_trail_length;
  2606. existing_finger->trail_list[index].trail_id = new_trail_id;
  2607. existing_finger->trail_list[index].is_present = GNUNET_YES;
  2608. }
  2609. #endif
  2610. /**
  2611. * Get the next hop to send trail teardown message from routing table and
  2612. * then delete the entry from routing table. Send trail teardown message for a
  2613. * specific trail of a finger.
  2614. * @param finger Finger whose trail is to be removed.
  2615. * @param trail List of peers in trail from me to a finger, NOT including
  2616. * endpoints.
  2617. */
  2618. static void
  2619. send_trail_teardown (struct FingerInfo *finger,
  2620. struct Trail *trail)
  2621. {
  2622. struct FriendInfo *friend;
  2623. struct GNUNET_PeerIdentity *next_hop;
  2624. next_hop = GDS_ROUTING_get_next_hop (trail->trail_id,
  2625. GDS_ROUTING_SRC_TO_DEST);
  2626. if (NULL == next_hop)
  2627. {
  2628. // DEBUG(" NO ENTRY FOUND IN %s ROUTING TABLE for trail id %s, line=%d,traillength = %d",
  2629. // GNUNET_i2s(&my_identity), GNUNET_h2s(&trail->trail_id), __LINE__,trail->trail_length);
  2630. return;
  2631. }
  2632. GNUNET_assert (0 != GNUNET_CRYPTO_cmp_peer_identity (&finger->finger_identity,
  2633. &my_identity));
  2634. GNUNET_assert(GNUNET_YES == trail->is_present);
  2635. if (trail->trail_length > 0)
  2636. {
  2637. friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
  2638. &trail->trail_head->peer);
  2639. }
  2640. else
  2641. {
  2642. friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
  2643. &finger->finger_identity);
  2644. }
  2645. if(NULL == friend)
  2646. {
  2647. DEBUG ("\n LINE NO: = %d, Friend not found for trail id %s of peer %s trail length = %d",
  2648. __LINE__,GNUNET_h2s(&trail->trail_id), GNUNET_i2s(&my_identity),trail->trail_length);
  2649. return;
  2650. }
  2651. if (0 != GNUNET_CRYPTO_cmp_peer_identity (next_hop, &friend->id)
  2652. && (0 == trail->trail_length))
  2653. {
  2654. DEBUG ("\n LINE NO: = %d, Friend not found for trail id %s of peer %s trail length = %d",
  2655. __LINE__,GNUNET_h2s(&trail->trail_id), GNUNET_i2s(&my_identity),trail->trail_length);
  2656. return;
  2657. }
  2658. GNUNET_assert (GNUNET_YES == GDS_ROUTING_remove_trail (trail->trail_id));
  2659. friend->trails_count--;
  2660. GDS_NEIGHBOURS_send_trail_teardown (&trail->trail_id,
  2661. GDS_ROUTING_SRC_TO_DEST,
  2662. &friend->id);
  2663. }
  2664. /**
  2665. * Send trail teardown message across all the trails to reach to finger.
  2666. * @param finger Finger whose all the trail should be freed.
  2667. */
  2668. static void
  2669. send_all_finger_trails_teardown (struct FingerInfo *finger)
  2670. {
  2671. unsigned int i;
  2672. for (i = 0; i < finger->trails_count; i++)
  2673. {
  2674. struct Trail *trail;
  2675. trail = &finger->trail_list[i];
  2676. if (GNUNET_YES == trail->is_present)
  2677. {
  2678. send_trail_teardown (finger, trail);
  2679. trail->is_present = GNUNET_NO;
  2680. }
  2681. }
  2682. }
  2683. /**
  2684. * Free a specific trail
  2685. * @param trail List of peers to be freed.
  2686. */
  2687. static void
  2688. free_trail (struct Trail *trail)
  2689. {
  2690. struct Trail_Element *trail_element;
  2691. while (NULL != (trail_element = trail->trail_head))
  2692. {
  2693. GNUNET_CONTAINER_DLL_remove (trail->trail_head,
  2694. trail->trail_tail,
  2695. trail_element);
  2696. GNUNET_free_non_null (trail_element);
  2697. }
  2698. trail->trail_head = NULL;
  2699. trail->trail_tail = NULL;
  2700. }
  2701. /**
  2702. * Free finger and its trail.
  2703. * @param finger Finger to be freed.
  2704. * @param finger_table_index Index at which finger is stored.
  2705. */
  2706. static void
  2707. free_finger (struct FingerInfo *finger, unsigned int finger_table_index)
  2708. {
  2709. struct Trail *trail;
  2710. unsigned int i;
  2711. for (i = 0; i < finger->trails_count; i++)
  2712. {
  2713. trail = &finger->trail_list[i];
  2714. if (GNUNET_NO == trail->is_present)
  2715. continue;
  2716. if (trail->trail_length > 0)
  2717. free_trail (trail);
  2718. trail->is_present = GNUNET_NO;
  2719. }
  2720. finger->is_present = GNUNET_NO;
  2721. memset ((void *)&finger_table[finger_table_index], 0, sizeof (finger_table[finger_table_index]));
  2722. }
  2723. /**
  2724. * Add a new entry in finger table at finger_table_index.
  2725. * In case I am my own finger, then we don't have a trail. In case of a friend,
  2726. * we have a trail with unique id and '0' trail length.
  2727. * In case a finger is a friend, then increment the trails count of the friend.
  2728. * @param finger_identity Peer Identity of new finger
  2729. * @param finger_trail Trail to reach from me to finger (excluding both end points).
  2730. * @param finger_trail_length Total number of peers in @a finger_trail.
  2731. * @param trail_id Unique identifier of the trail.
  2732. * @param finger_table_index Index in finger table.
  2733. */
  2734. static void
  2735. add_new_finger (struct GNUNET_PeerIdentity finger_identity,
  2736. const struct GNUNET_PeerIdentity *finger_trail,
  2737. unsigned int finger_trail_length,
  2738. struct GNUNET_HashCode trail_id,
  2739. unsigned int finger_table_index)
  2740. {
  2741. struct FingerInfo *new_entry;
  2742. struct FriendInfo *first_trail_hop;
  2743. struct Trail *trail;
  2744. unsigned int i;
  2745. new_entry = GNUNET_new (struct FingerInfo);
  2746. new_entry->finger_identity = finger_identity;
  2747. new_entry->finger_table_index = finger_table_index;
  2748. new_entry->is_present = GNUNET_YES;
  2749. /* If the new entry is my own identity. */
  2750. if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity,
  2751. &finger_identity))
  2752. {
  2753. new_entry->trails_count = 0;
  2754. finger_table[finger_table_index] = *new_entry;
  2755. GNUNET_free (new_entry);
  2756. return;
  2757. }
  2758. /* Finger is a friend. */
  2759. if (0 == finger_trail_length)
  2760. {
  2761. new_entry->trail_list[0].trail_id = trail_id;
  2762. new_entry->trails_count = 1;
  2763. new_entry->trail_list[0].is_present = GNUNET_YES;
  2764. new_entry->trail_list[0].trail_length = 0;
  2765. new_entry->trail_list[0].trail_head = NULL;
  2766. new_entry->trail_list[0].trail_tail = NULL;
  2767. finger_table[finger_table_index] = *new_entry;
  2768. GNUNET_assert (NULL !=
  2769. (first_trail_hop =
  2770. GNUNET_CONTAINER_multipeermap_get (friend_peermap,
  2771. &finger_identity)));
  2772. first_trail_hop->trails_count++;
  2773. GNUNET_free (new_entry);
  2774. return;
  2775. }
  2776. GNUNET_assert (NULL !=
  2777. (first_trail_hop =
  2778. GNUNET_CONTAINER_multipeermap_get (friend_peermap,
  2779. &finger_trail[0])));
  2780. new_entry->trails_count = 1;
  2781. first_trail_hop->trails_count++;
  2782. /* Copy the finger trail into trail. */
  2783. trail = &new_entry->trail_list[0];
  2784. for(i = 0; i < finger_trail_length; i++)
  2785. {
  2786. struct Trail_Element *element = GNUNET_new (struct Trail_Element);
  2787. element->next = NULL;
  2788. element->prev = NULL;
  2789. element->peer = finger_trail[i];
  2790. GNUNET_CONTAINER_DLL_insert_tail (trail->trail_head,
  2791. trail->trail_tail,
  2792. element);
  2793. }
  2794. /* Add trail to trail list. */
  2795. trail->trail_length = finger_trail_length;
  2796. trail->trail_id = trail_id;
  2797. trail->is_present = GNUNET_YES;
  2798. finger_table[finger_table_index] = *new_entry;
  2799. GNUNET_free (new_entry);
  2800. }
  2801. /**
  2802. * Periodic task to verify current successor. There can be multiple trails to reach
  2803. * to successor, choose the shortest one and send verify successor message
  2804. * across that trail.
  2805. * @param cls closure for this task
  2806. * @param tc the context under which the task is running
  2807. */
  2808. static void
  2809. send_verify_successor_message (void *cls,
  2810. const struct GNUNET_SCHEDULER_TaskContext *tc)
  2811. {
  2812. struct FriendInfo *target_friend;
  2813. struct GNUNET_HashCode trail_id;
  2814. struct Trail *trail;
  2815. struct Trail_Element *element;
  2816. unsigned int trail_length;
  2817. unsigned int i = 0;
  2818. struct FingerInfo *successor;
  2819. successor = &finger_table[0];
  2820. /* This task will be scheduled when the result for Verify Successor is received. */
  2821. send_verify_successor_task = GNUNET_SCHEDULER_NO_TASK;
  2822. /* When verify successor is being called for first time *for current context*
  2823. * cls will be NULL. If send_verify_successor_retry_task is not NO_TASK, we
  2824. * must cancel the retry task scheduled for verify_successor of previous
  2825. * context.
  2826. */
  2827. if (NULL == cls)
  2828. {
  2829. /* FIXME: Here we are scheduling a new verify successor task, as we
  2830. got a new successor. But a send verify successor task may be in progress.
  2831. 1. We need to be sure that this is indeed a new successor. As this function
  2832. is called even if we add a new trail to reach t old successor.
  2833. 2. Assuming the new successor is different, then verify successor message
  2834. * to old successor may be following stages.
  2835. * --> Waiting for verify successor result. Don't wait anymore. there is
  2836. * no trail to reach from old successor to me, hence, routing
  2837. * lookup will fail.
  2838. * --> Waiting for notify confirmation. again don't wait for it. notify
  2839. * confirmation will not succeded.
  2840. */
  2841. if (send_verify_successor_retry_task != GNUNET_SCHEDULER_NO_TASK)
  2842. {
  2843. /* FIXME: Are we scheduling retry task as soon as we send verify message.
  2844. If yes then here before making this task, first check if the message
  2845. is for the same peer again. */
  2846. struct VerifySuccessorContext *old_ctx =
  2847. GNUNET_SCHEDULER_cancel(send_verify_successor_retry_task);
  2848. /* old_ctx must not be NULL, as the retry task had been scheduled */
  2849. GNUNET_assert(NULL != old_ctx);
  2850. GNUNET_free(old_ctx);
  2851. /* FIXME: Why don't we reset the task to NO_TASK here? */
  2852. }
  2853. struct VerifySuccessorContext *ctx;
  2854. ctx = GNUNET_new(struct VerifySuccessorContext);
  2855. ctx->num_retries_scheduled++;
  2856. send_verify_successor_retry_task =
  2857. GNUNET_SCHEDULER_add_delayed (verify_successor_retry_time,
  2858. &send_verify_successor_message,
  2859. ctx);
  2860. }
  2861. else
  2862. {
  2863. /* This is a retry attempt for verify_successor for a previous context */
  2864. struct VerifySuccessorContext *ctx;
  2865. ctx = cls;
  2866. ctx->num_retries_scheduled++;
  2867. send_verify_successor_retry_task =
  2868. GNUNET_SCHEDULER_add_delayed (verify_successor_retry_time,
  2869. &send_verify_successor_message,
  2870. ctx);
  2871. }
  2872. /* Among all the trails to reach to successor, select first one which is present.*/
  2873. for (i = 0; i < successor->trails_count; i++)
  2874. {
  2875. trail = &successor->trail_list[i];
  2876. if(GNUNET_YES == trail->is_present)
  2877. break;
  2878. }
  2879. /* No valid trail found to reach to successor. */
  2880. if (i == successor->trails_count)
  2881. return;
  2882. GNUNET_assert(0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity,
  2883. &successor->finger_identity));
  2884. /* Trail stored at this index. */
  2885. GNUNET_assert (GNUNET_YES == trail->is_present);
  2886. trail_id = trail->trail_id;
  2887. if (NULL == GDS_ROUTING_get_next_hop(trail_id,GDS_ROUTING_SRC_TO_DEST))
  2888. {
  2889. DEBUG(" NO ENTRY FOUND IN %s ROUTING TABLE for trail id %s, line",
  2890. GNUNET_i2s(&my_identity), GNUNET_h2s(&trail->trail_id), __LINE__);
  2891. GNUNET_break(0);
  2892. return;
  2893. }
  2894. trail_length = trail->trail_length;
  2895. if (trail_length > 0)
  2896. {
  2897. /* Copy the trail into peer list. */
  2898. struct GNUNET_PeerIdentity peer_list[trail_length];
  2899. element = trail->trail_head;
  2900. for(i = 0; i < trail_length; i++)
  2901. {
  2902. peer_list[i] = element->peer;
  2903. element = element->next;
  2904. }
  2905. GNUNET_assert (NULL != (target_friend =
  2906. GNUNET_CONTAINER_multipeermap_get (friend_peermap,
  2907. &peer_list[0])));
  2908. GDS_NEIGHBOURS_send_verify_successor_message (my_identity,
  2909. successor->finger_identity,
  2910. trail_id, peer_list, trail_length,
  2911. target_friend);
  2912. }
  2913. else
  2914. {
  2915. GNUNET_assert (NULL != (target_friend =
  2916. GNUNET_CONTAINER_multipeermap_get (friend_peermap,
  2917. &successor->finger_identity)));
  2918. GDS_NEIGHBOURS_send_verify_successor_message (my_identity,
  2919. successor->finger_identity,
  2920. trail_id, NULL, 0,
  2921. target_friend);
  2922. }
  2923. }
  2924. /**
  2925. * FIXME: should this be a periodic task, incrementing the search finger index?
  2926. * Update the current search finger index.
  2927. * @a finger_identity
  2928. * @a finger_table_index
  2929. */
  2930. static void
  2931. update_current_search_finger_index (unsigned int finger_table_index)
  2932. {
  2933. struct FingerInfo *successor;
  2934. /* FIXME correct this: only move current index periodically */
  2935. if (finger_table_index != current_search_finger_index)
  2936. return;
  2937. successor = &finger_table[0];
  2938. GNUNET_assert (GNUNET_YES == successor->is_present);
  2939. /* We were looking for immediate successor. */
  2940. if (0 == current_search_finger_index)
  2941. {
  2942. current_search_finger_index = PREDECESSOR_FINGER_ID;
  2943. if (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &successor->finger_identity))
  2944. {
  2945. if (GNUNET_SCHEDULER_NO_TASK == send_verify_successor_task)
  2946. {
  2947. send_verify_successor_task =
  2948. GNUNET_SCHEDULER_add_now (&send_verify_successor_message, NULL);
  2949. }
  2950. }
  2951. return;
  2952. }
  2953. current_search_finger_index = current_search_finger_index - 1;
  2954. return;
  2955. }
  2956. /**
  2957. * Get the least significant bit set in val.
  2958. *
  2959. * @param val Value
  2960. * @return Position of first bit set, 65 in case of error.
  2961. */
  2962. static unsigned int
  2963. find_set_bit (uint64_t val)
  2964. {
  2965. uint64_t i;
  2966. unsigned int pos;
  2967. i = 1;
  2968. pos = 0;
  2969. while (!(i & val))
  2970. {
  2971. i = i << 1;
  2972. pos++;
  2973. if (pos > 63)
  2974. {
  2975. GNUNET_break (0);
  2976. return 65;
  2977. }
  2978. }
  2979. if (val/i != 1)
  2980. return 65; /* Some other bit was set to 1 as well. */
  2981. return pos;
  2982. }
  2983. /**
  2984. * Calculate finger_table_index from initial 64 bit finger identity value that
  2985. * we send in trail setup message.
  2986. * @param ultimate_destination_finger_value Value that we calculated from our
  2987. * identity and finger_table_index.
  2988. * @param is_predecessor Is the entry for predecessor or not?
  2989. * @return finger_table_index Value between 0 <= finger_table_index <= 64
  2990. * finger_table_index > PREDECESSOR_FINGER_ID, if error occurs.
  2991. */
  2992. static unsigned int
  2993. get_finger_table_index (uint64_t ultimate_destination_finger_value,
  2994. unsigned int is_predecessor)
  2995. {
  2996. uint64_t my_id64;
  2997. uint64_t diff;
  2998. unsigned int finger_table_index;
  2999. memcpy (&my_id64, &my_identity, sizeof (uint64_t));
  3000. my_id64 = GNUNET_ntohll (my_id64);
  3001. /* Is this a predecessor finger? */
  3002. if (1 == is_predecessor)
  3003. {
  3004. diff = my_id64 - ultimate_destination_finger_value;
  3005. if (1 == diff)
  3006. finger_table_index = PREDECESSOR_FINGER_ID;
  3007. else
  3008. finger_table_index = PREDECESSOR_FINGER_ID + 1; //error value
  3009. }
  3010. else
  3011. {
  3012. diff = ultimate_destination_finger_value - my_id64;
  3013. finger_table_index = find_set_bit (diff);
  3014. }
  3015. return finger_table_index;
  3016. }
  3017. /**
  3018. * Remove finger and its associated data structures from finger table.
  3019. * @param existing_finger Finger to be removed which is in finger table.
  3020. * @param finger_table_index Index in finger table where @a existing_finger
  3021. * is stored.
  3022. */
  3023. static void
  3024. remove_existing_finger (struct FingerInfo *existing_finger,
  3025. unsigned int finger_table_index)
  3026. {
  3027. GNUNET_assert (GNUNET_YES == existing_finger->is_present);
  3028. /* If I am my own finger, then we have no trails. */
  3029. if (0 == GNUNET_CRYPTO_cmp_peer_identity (&existing_finger->finger_identity,
  3030. &my_identity))
  3031. {
  3032. existing_finger->is_present = GNUNET_NO;
  3033. memset ((void *)&finger_table[finger_table_index], 0,
  3034. sizeof (finger_table[finger_table_index]));
  3035. return;
  3036. }
  3037. /* For all other fingers, send trail teardown across all the trails to reach
  3038. finger, and free the finger. */
  3039. send_all_finger_trails_teardown (existing_finger);
  3040. free_finger (existing_finger, finger_table_index);
  3041. }
  3042. /**
  3043. * Check if there is already an entry in finger_table at finger_table_index.
  3044. * We get the finger_table_index from 64bit finger value we got from the network.
  3045. * -- If yes, then select the closest finger.
  3046. * -- If new and existing finger are same, then check if you can store more
  3047. * trails.
  3048. * -- If yes then add trail, else keep the best trails to reach to the
  3049. * finger.
  3050. * -- If the new finger is closest, remove the existing entry, send trail
  3051. * teardown message across all the trails to reach the existing entry.
  3052. * Add the new finger.
  3053. * -- If new and existing finger are different, and existing finger is closest
  3054. * then do nothing.
  3055. * -- Update current_search_finger_index.
  3056. * @param finger_identity Peer Identity of new finger
  3057. * @param finger_trail Trail to reach the new finger
  3058. * @param finger_trail_length Total number of peers in @a new_finger_trail.
  3059. * @param is_predecessor Is this entry for predecessor in finger_table?
  3060. * @param finger_value 64 bit value of finger identity that we got from network.
  3061. * @param finger_trail_id Unique identifier of @finger_trail.
  3062. */
  3063. static void
  3064. finger_table_add (struct GNUNET_PeerIdentity finger_identity,
  3065. const struct GNUNET_PeerIdentity *finger_trail,
  3066. unsigned int finger_trail_length,
  3067. unsigned int is_predecessor,
  3068. uint64_t finger_value,
  3069. struct GNUNET_HashCode finger_trail_id)
  3070. {
  3071. struct FingerInfo *existing_finger;
  3072. struct GNUNET_PeerIdentity closest_peer;
  3073. struct FingerInfo *successor;
  3074. unsigned int finger_table_index;
  3075. /* Get the finger_table_index corresponding to finger_value we got from network.*/
  3076. finger_table_index = get_finger_table_index (finger_value, is_predecessor);
  3077. /* Invalid finger_table_index. */
  3078. if ((finger_table_index > PREDECESSOR_FINGER_ID))
  3079. {
  3080. GNUNET_break_op (0);
  3081. return;
  3082. }
  3083. /* Check if new entry is same as successor. */
  3084. if ((0 != finger_table_index) &&
  3085. (PREDECESSOR_FINGER_ID != finger_table_index))
  3086. {
  3087. successor = &finger_table[0];
  3088. if (GNUNET_NO == successor->is_present)
  3089. {
  3090. GNUNET_break (0); //ASSERTION FAILS HERE. FIXME
  3091. return;
  3092. }
  3093. if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger_identity,
  3094. &successor->finger_identity))
  3095. {
  3096. if (0 == fingers_round_count)
  3097. {
  3098. find_finger_trail_task_next_send_time =
  3099. GNUNET_TIME_STD_BACKOFF(find_finger_trail_task_next_send_time);
  3100. }
  3101. else
  3102. fingers_round_count--;
  3103. current_search_finger_index = 0;
  3104. GNUNET_STATISTICS_update (GDS_stats,
  3105. gettext_noop
  3106. ("# FINGERS_COUNT"), (int64_t) total_fingers_found,
  3107. GNUNET_NO);
  3108. total_fingers_found = 0;
  3109. return;
  3110. }
  3111. struct FingerInfo prev_finger;
  3112. prev_finger = finger_table[finger_table_index - 1];
  3113. if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger_identity,
  3114. &prev_finger.finger_identity))
  3115. {
  3116. current_search_finger_index--;
  3117. return;
  3118. }
  3119. }
  3120. total_fingers_found++;
  3121. existing_finger = &finger_table[finger_table_index];
  3122. /* No entry present in finger_table for given finger map index. */
  3123. if (GNUNET_NO == existing_finger->is_present)
  3124. {
  3125. /* Shorten the trail if possible. */
  3126. add_new_finger (finger_identity, finger_trail,
  3127. finger_trail_length,
  3128. finger_trail_id, finger_table_index);
  3129. update_current_search_finger_index (finger_table_index);
  3130. return;
  3131. }
  3132. /* If existing entry and finger identity are not same. */
  3133. if (0 != GNUNET_CRYPTO_cmp_peer_identity (&(existing_finger->finger_identity),
  3134. &finger_identity))
  3135. {
  3136. closest_peer = select_closest_peer (&existing_finger->finger_identity,
  3137. &finger_identity,
  3138. finger_value,
  3139. is_predecessor);
  3140. /* If the new finger is the closest peer. */
  3141. if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger_identity, &closest_peer))
  3142. {
  3143. remove_existing_finger (existing_finger, finger_table_index);
  3144. add_new_finger (finger_identity, finger_trail, finger_trail_length,
  3145. finger_trail_id, finger_table_index);
  3146. }
  3147. else
  3148. {
  3149. /* Existing finger is the closest one. We need to send trail teardown
  3150. across the trail setup in routing table of all the peers. */
  3151. if (0 != GNUNET_CRYPTO_cmp_peer_identity (&finger_identity, &my_identity))
  3152. {
  3153. if (finger_trail_length > 0)
  3154. GDS_NEIGHBOURS_send_trail_teardown (&finger_trail_id,
  3155. GDS_ROUTING_SRC_TO_DEST,
  3156. &finger_trail[0]);
  3157. else
  3158. GDS_NEIGHBOURS_send_trail_teardown (&finger_trail_id,
  3159. GDS_ROUTING_SRC_TO_DEST,
  3160. &finger_identity);
  3161. }
  3162. }
  3163. }
  3164. else
  3165. {
  3166. /* If both new and existing entry are same as my_identity, then do nothing. */
  3167. if (0 == GNUNET_CRYPTO_cmp_peer_identity (&(existing_finger->finger_identity),
  3168. &my_identity))
  3169. {
  3170. return;
  3171. }
  3172. /* If there is space to store more trails. */
  3173. if (existing_finger->trails_count < MAXIMUM_TRAILS_PER_FINGER)
  3174. add_new_trail (existing_finger, finger_trail,
  3175. finger_trail_length, finger_trail_id);
  3176. else
  3177. select_and_replace_trail (existing_finger, finger_trail,
  3178. finger_trail_length, finger_trail_id);
  3179. }
  3180. update_current_search_finger_index (finger_table_index);
  3181. return;
  3182. }
  3183. /**
  3184. * Core handler for P2P put messages.
  3185. * @param cls closure
  3186. * @param peer sender of the request
  3187. * @param message message
  3188. * @return #GNUNET_OK to keep the connection open,
  3189. * #GNUNET_SYSERR to close it (signal serious error)
  3190. */
  3191. static int
  3192. handle_dht_p2p_put (void *cls, const struct GNUNET_PeerIdentity *peer,
  3193. const struct GNUNET_MessageHeader *message)
  3194. {
  3195. struct PeerPutMessage *put;
  3196. struct GNUNET_PeerIdentity *put_path;
  3197. struct GNUNET_PeerIdentity current_best_known_dest;
  3198. struct GNUNET_PeerIdentity best_known_dest;
  3199. struct GNUNET_HashCode received_intermediate_trail_id;
  3200. struct GNUNET_HashCode intermediate_trail_id;
  3201. struct GNUNET_PeerIdentity *next_hop;
  3202. struct GNUNET_PeerIdentity *next_routing_hop;
  3203. enum GNUNET_DHT_RouteOption options;
  3204. struct GNUNET_HashCode test_key;
  3205. void *payload;
  3206. size_t msize;
  3207. uint32_t putlen;
  3208. uint32_t hop_count;
  3209. size_t payload_size;
  3210. uint64_t key_value;
  3211. msize = ntohs (message->size);
  3212. if (msize < sizeof (struct PeerPutMessage))
  3213. {
  3214. GNUNET_break_op (0);
  3215. return GNUNET_OK;
  3216. }
  3217. put = (struct PeerPutMessage *) message;
  3218. putlen = ntohl (put->put_path_length);
  3219. if ((msize <
  3220. sizeof (struct PeerPutMessage) +
  3221. putlen * sizeof (struct GNUNET_PeerIdentity)) ||
  3222. (putlen >
  3223. GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
  3224. {
  3225. GNUNET_break_op (0);
  3226. return GNUNET_OK;
  3227. }
  3228. #if ENABLE_MALICIOUS
  3229. if(1 == act_malicious)
  3230. {
  3231. DEBUG("\n I AM MALICIOUS PUT_REQUEST_DROPPED for key = %ss",GNUNET_h2s(&put->key));
  3232. return GNUNET_OK;
  3233. }
  3234. #endif
  3235. GNUNET_STATISTICS_update (GDS_stats,
  3236. gettext_noop
  3237. ("# Bytes received from other peers"), (int64_t) msize,
  3238. GNUNET_NO);
  3239. current_best_known_dest = put->best_known_destination;
  3240. put_path = (struct GNUNET_PeerIdentity *) &put[1];
  3241. payload = &put_path[putlen];
  3242. options = ntohl (put->options);
  3243. received_intermediate_trail_id = put->intermediate_trail_id;
  3244. hop_count = ntohl(put->hop_count);
  3245. payload_size = msize - (sizeof (struct PeerPutMessage) +
  3246. putlen * sizeof (struct GNUNET_PeerIdentity));
  3247. hop_count++;
  3248. switch (GNUNET_BLOCK_get_key (GDS_block_context, ntohl (put->block_type),
  3249. payload, payload_size, &test_key))
  3250. {
  3251. case GNUNET_YES:
  3252. if (0 != memcmp (&test_key, &put->key, sizeof (struct GNUNET_HashCode)))
  3253. {
  3254. char *put_s = GNUNET_strdup (GNUNET_h2s_full (&put->key));
  3255. GNUNET_break_op (0);
  3256. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  3257. "PUT with key `%s' for block with key %s\n",
  3258. put_s, GNUNET_h2s_full (&test_key));
  3259. GNUNET_free (put_s);
  3260. return GNUNET_OK;
  3261. }
  3262. break;
  3263. case GNUNET_NO:
  3264. GNUNET_break_op (0);
  3265. return GNUNET_OK;
  3266. case GNUNET_SYSERR:
  3267. /* cannot verify, good luck */
  3268. break;
  3269. }
  3270. if (ntohl (put->block_type) == GNUNET_BLOCK_TYPE_REGEX) /* FIXME: do for all tpyes */
  3271. {
  3272. switch (GNUNET_BLOCK_evaluate (GDS_block_context,
  3273. ntohl (put->block_type),
  3274. NULL, /* query */
  3275. NULL, 0, /* bloom filer */
  3276. NULL, 0, /* xquery */
  3277. payload, payload_size))
  3278. {
  3279. case GNUNET_BLOCK_EVALUATION_OK_MORE:
  3280. case GNUNET_BLOCK_EVALUATION_OK_LAST:
  3281. break;
  3282. case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
  3283. case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
  3284. case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
  3285. case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
  3286. case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
  3287. case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
  3288. default:
  3289. GNUNET_break_op (0);
  3290. return GNUNET_OK;
  3291. }
  3292. }
  3293. /* Check if you are already a part of put path. */
  3294. unsigned int i;
  3295. for (i = 0; i < putlen; i++)
  3296. {
  3297. if(0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &put_path[i]))
  3298. {
  3299. putlen = i;
  3300. break;
  3301. }
  3302. }
  3303. /* Add yourself to the list. */
  3304. struct GNUNET_PeerIdentity pp[putlen + 1];
  3305. //if (0 != (options & GNUNET_DHT_RO_RECORD_ROUTE))
  3306. if (1)
  3307. {
  3308. memcpy (pp, put_path, putlen * sizeof (struct GNUNET_PeerIdentity));
  3309. pp[putlen] = my_identity;
  3310. putlen++;
  3311. }
  3312. else
  3313. putlen = 0;
  3314. memcpy (&key_value, &(put->key), sizeof (uint64_t));
  3315. struct Closest_Peer successor;
  3316. key_value = GNUNET_ntohll (key_value);
  3317. successor = find_local_best_known_next_hop (key_value,
  3318. GDS_FINGER_TYPE_NON_PREDECESSOR);
  3319. next_hop = GNUNET_new (struct GNUNET_PeerIdentity);
  3320. *next_hop = successor.next_hop;
  3321. intermediate_trail_id = successor.trail_id;
  3322. best_known_dest = successor.best_known_destination;
  3323. if (0 != (GNUNET_CRYPTO_cmp_peer_identity (&current_best_known_dest, &my_identity)))
  3324. {
  3325. next_routing_hop = GDS_ROUTING_get_next_hop (received_intermediate_trail_id,
  3326. GDS_ROUTING_SRC_TO_DEST);
  3327. if (NULL != next_routing_hop)
  3328. {
  3329. next_hop = next_routing_hop;
  3330. intermediate_trail_id = received_intermediate_trail_id;
  3331. best_known_dest = current_best_known_dest;
  3332. }
  3333. }
  3334. GDS_CLIENTS_process_put (options,
  3335. ntohl (put->block_type),
  3336. hop_count,
  3337. ntohl (put->desired_replication_level),
  3338. putlen, pp,
  3339. GNUNET_TIME_absolute_ntoh (put->expiration_time),
  3340. &put->key,
  3341. payload,
  3342. payload_size);
  3343. /* I am the final destination */
  3344. if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &best_known_dest))
  3345. {
  3346. DEBUG("\n PUT_REQUEST_SUCCESSFUL for key = %s",GNUNET_h2s(&put->key));
  3347. GDS_DATACACHE_handle_put (GNUNET_TIME_absolute_ntoh (put->expiration_time),
  3348. &(put->key),putlen, pp, ntohl (put->block_type),
  3349. payload_size, payload);
  3350. }
  3351. else
  3352. {
  3353. #if ENABLE_MALICIOUS
  3354. if (0 != GNUNET_CRYPTO_cmp_peer_identity (&successor.best_known_destination,
  3355. &successor.next_hop))
  3356. {
  3357. struct FingerInfo *next_hop_finger;
  3358. unsigned int i;
  3359. next_hop_finger = &finger_table[successor.finger_table_index];
  3360. for (i = 0; i < next_hop_finger->trails_count; i++)
  3361. {
  3362. if (GNUNET_YES == next_hop_finger->trail_list[i].is_present)
  3363. {
  3364. if(0 == next_hop_finger->trail_list[i].trail_length)
  3365. {
  3366. GDS_NEIGHBOURS_send_put (&put->key,
  3367. ntohl (put->block_type),ntohl (put->options),
  3368. ntohl (put->desired_replication_level),
  3369. best_known_dest, intermediate_trail_id, next_hop,
  3370. hop_count, putlen, pp,
  3371. GNUNET_TIME_absolute_ntoh (put->expiration_time),
  3372. payload, payload_size);
  3373. return GNUNET_OK;
  3374. }
  3375. next_hop = &next_hop_finger->trail_list[i].trail_head->peer;
  3376. GDS_NEIGHBOURS_send_put (&put->key,
  3377. ntohl (put->block_type),ntohl (put->options),
  3378. ntohl (put->desired_replication_level),
  3379. best_known_dest,
  3380. next_hop_finger->trail_list[i].trail_id,
  3381. next_hop, hop_count, putlen, pp,
  3382. GNUNET_TIME_absolute_ntoh (put->expiration_time),
  3383. payload, payload_size);
  3384. }
  3385. }
  3386. return GNUNET_OK;
  3387. }
  3388. #endif
  3389. GDS_NEIGHBOURS_send_put (&put->key,
  3390. ntohl (put->block_type),ntohl (put->options),
  3391. ntohl (put->desired_replication_level),
  3392. best_known_dest, intermediate_trail_id, next_hop,
  3393. hop_count, putlen, pp,
  3394. GNUNET_TIME_absolute_ntoh (put->expiration_time),
  3395. payload, payload_size);
  3396. }
  3397. return GNUNET_OK;
  3398. }
  3399. /**
  3400. * FIXME: Check for loop in the request. If you already are part of get path,
  3401. * then you need to reset the get path length.
  3402. * Core handler for p2p get requests.
  3403. *
  3404. * @param cls closure
  3405. * @param peer sender of the request
  3406. * @param message message
  3407. * @return #GNUNET_OK to keep the connection open,
  3408. * #GNUNET_SYSERR to close it (signal serious error)
  3409. */
  3410. static int
  3411. handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
  3412. const struct GNUNET_MessageHeader *message)
  3413. {
  3414. const struct PeerGetMessage *get;
  3415. const struct GNUNET_PeerIdentity *get_path;
  3416. struct GNUNET_PeerIdentity best_known_dest;
  3417. struct GNUNET_PeerIdentity current_best_known_dest;
  3418. struct GNUNET_HashCode intermediate_trail_id;
  3419. struct GNUNET_HashCode received_intermediate_trail_id;
  3420. struct Closest_Peer successor;
  3421. struct GNUNET_PeerIdentity *next_hop;
  3422. struct GNUNET_PeerIdentity *next_routing_hop;
  3423. uint32_t get_length;
  3424. uint64_t key_value;
  3425. uint32_t hop_count;
  3426. size_t msize;
  3427. msize = ntohs (message->size);
  3428. if (msize < sizeof (struct PeerGetMessage))
  3429. {
  3430. GNUNET_break_op (0);
  3431. return GNUNET_YES;
  3432. }
  3433. get = (const struct PeerGetMessage *)message;
  3434. get_length = ntohl (get->get_path_length);
  3435. if ((msize <
  3436. sizeof (struct PeerGetMessage) +
  3437. get_length * sizeof (struct GNUNET_PeerIdentity)) ||
  3438. (get_length >
  3439. GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
  3440. {
  3441. GNUNET_break_op (0);
  3442. return GNUNET_YES;
  3443. }
  3444. #if ENABLE_MALICIOUS
  3445. if(1 == act_malicious)
  3446. {
  3447. DEBUG("I am malicious,GET_REQUEST_DROPPED for key = %s. \n",GNUNET_h2s(&get->key));
  3448. return GNUNET_OK;
  3449. }
  3450. #endif
  3451. current_best_known_dest = get->best_known_destination;
  3452. received_intermediate_trail_id = get->intermediate_trail_id;
  3453. get_path = (const struct GNUNET_PeerIdentity *)&get[1];
  3454. hop_count = get->hop_count;
  3455. hop_count++;
  3456. GNUNET_STATISTICS_update (GDS_stats,
  3457. gettext_noop
  3458. ("# Bytes received from other peers"), msize,
  3459. GNUNET_NO);
  3460. memcpy (&key_value, &(get->key), sizeof (uint64_t));
  3461. key_value = GNUNET_ntohll (key_value);
  3462. /* Check if you are already a part of get path. */
  3463. unsigned int i;
  3464. for (i = 0; i < get_length; i++)
  3465. {
  3466. if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &get_path[i]))
  3467. {
  3468. get_length = i;
  3469. break;
  3470. }
  3471. }
  3472. /* Add yourself in the get path. */
  3473. struct GNUNET_PeerIdentity gp[get_length + 1];
  3474. memcpy (gp, get_path, get_length * sizeof (struct GNUNET_PeerIdentity));
  3475. gp[get_length] = my_identity;
  3476. get_length = get_length + 1;
  3477. GDS_CLIENTS_process_get (get->options, get->block_type, hop_count,
  3478. get->desired_replication_level, get->get_path_length,
  3479. gp, &get->key);
  3480. successor = find_local_best_known_next_hop (key_value,
  3481. GDS_FINGER_TYPE_NON_PREDECESSOR);
  3482. next_hop = GNUNET_new (struct GNUNET_PeerIdentity);
  3483. *next_hop = successor.next_hop;
  3484. best_known_dest = successor.best_known_destination;
  3485. intermediate_trail_id = successor.trail_id;
  3486. /* I am not the final destination. I am part of trail to reach final dest. */
  3487. if (0 != (GNUNET_CRYPTO_cmp_peer_identity (&current_best_known_dest, &my_identity)))
  3488. {
  3489. next_routing_hop = GDS_ROUTING_get_next_hop (received_intermediate_trail_id,
  3490. GDS_ROUTING_SRC_TO_DEST);
  3491. if (NULL != next_routing_hop)
  3492. {
  3493. next_hop = next_routing_hop;
  3494. best_known_dest = current_best_known_dest;
  3495. intermediate_trail_id = received_intermediate_trail_id;
  3496. }
  3497. }
  3498. /* I am the final destination. */
  3499. if (0 == GNUNET_CRYPTO_cmp_peer_identity(&my_identity, &best_known_dest))
  3500. {
  3501. if (1 == get_length)
  3502. {
  3503. DEBUG("\n GET_REQUEST DONE for key = %s",GNUNET_h2s(&get->key));
  3504. GDS_DATACACHE_handle_get (&(get->key),(get->block_type), NULL, 0,
  3505. NULL, 0, 1, &my_identity, NULL,&my_identity);
  3506. }
  3507. else
  3508. {
  3509. GDS_DATACACHE_handle_get (&(get->key),(get->block_type), NULL, 0, NULL, 0,
  3510. get_length, gp, &gp[get_length - 2],
  3511. &my_identity);
  3512. }
  3513. }
  3514. else
  3515. {
  3516. #if ENABLE_MALICIOUS
  3517. if (0 != GNUNET_CRYPTO_cmp_peer_identity (&successor.best_known_destination,
  3518. &successor.next_hop))
  3519. {
  3520. struct FingerInfo *next_hop_finger;
  3521. unsigned int i;
  3522. next_hop_finger = &finger_table[successor.finger_table_index];
  3523. for (i = 0; i < next_hop_finger->trails_count; i++)
  3524. {
  3525. if(0 == next_hop_finger->trail_list[i].trail_length)
  3526. {
  3527. GDS_NEIGHBOURS_send_get (&(get->key), get->block_type, get->options,
  3528. get->desired_replication_level, best_known_dest,
  3529. intermediate_trail_id, next_hop, hop_count,
  3530. get_length, gp);
  3531. return GNUNET_OK;
  3532. }
  3533. if (GNUNET_YES == next_hop_finger->trail_list[i].is_present)
  3534. {
  3535. next_hop = &next_hop_finger->trail_list[i].trail_head->peer;
  3536. GDS_NEIGHBOURS_send_get (&(get->key), get->block_type, get->options,
  3537. get->desired_replication_level, best_known_dest,
  3538. next_hop_finger->trail_list[i].trail_id,
  3539. next_hop, hop_count,
  3540. get_length, gp);
  3541. }
  3542. }
  3543. return GNUNET_OK;
  3544. }
  3545. #endif
  3546. GDS_NEIGHBOURS_send_get (&(get->key), get->block_type, get->options,
  3547. get->desired_replication_level, best_known_dest,
  3548. intermediate_trail_id, next_hop, hop_count,
  3549. get_length, gp);
  3550. }
  3551. return GNUNET_YES;
  3552. }
  3553. /**
  3554. * Core handler for get result
  3555. * @param cls closure
  3556. * @param peer sender of the request
  3557. * @param message message
  3558. * @return #GNUNET_OK to keep the connection open,
  3559. * #GNUNET_SYSERR to close it (signal serious error)
  3560. */
  3561. static int
  3562. handle_dht_p2p_get_result (void *cls, const struct GNUNET_PeerIdentity *peer,
  3563. const struct GNUNET_MessageHeader *message)
  3564. {
  3565. const struct PeerGetResultMessage *get_result;
  3566. const struct GNUNET_PeerIdentity *get_path;
  3567. const struct GNUNET_PeerIdentity *put_path;
  3568. const void *payload;
  3569. size_t payload_size;
  3570. size_t msize;
  3571. unsigned int getlen;
  3572. unsigned int putlen;
  3573. int current_path_index;
  3574. msize = ntohs (message->size);
  3575. if (msize < sizeof (struct PeerGetResultMessage))
  3576. {
  3577. GNUNET_break_op (0);
  3578. return GNUNET_YES;
  3579. }
  3580. get_result = (const struct PeerGetResultMessage *)message;
  3581. getlen = ntohl (get_result->get_path_length);
  3582. putlen = ntohl (get_result->put_path_length);
  3583. if ((msize <
  3584. sizeof (struct PeerGetResultMessage) +
  3585. getlen * sizeof (struct GNUNET_PeerIdentity) +
  3586. putlen * sizeof (struct GNUNET_PeerIdentity)) ||
  3587. (getlen >
  3588. GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity) ||
  3589. (putlen >
  3590. GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity))))
  3591. {
  3592. GNUNET_break_op (0);
  3593. return GNUNET_YES;
  3594. }
  3595. DEBUG("GET_RESULT FOR DATA_SIZE = %lu\n",msize);
  3596. GNUNET_STATISTICS_update (GDS_stats,
  3597. gettext_noop
  3598. ("# Bytes received from other peers"), msize,
  3599. GNUNET_NO);
  3600. put_path = (const struct GNUNET_PeerIdentity *) &get_result[1];
  3601. get_path = &put_path[putlen];
  3602. payload = (const void *) &get_path[getlen];
  3603. payload_size = msize - (sizeof (struct PeerGetResultMessage) +
  3604. (getlen + putlen) * sizeof (struct GNUNET_PeerIdentity));
  3605. if (0 == (GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &(get_path[0]))))
  3606. {
  3607. GDS_CLIENTS_handle_reply (get_result->expiration_time, &(get_result->key),
  3608. getlen, get_path, putlen,
  3609. put_path, get_result->type, payload_size, payload);
  3610. return GNUNET_YES;
  3611. }
  3612. else
  3613. {
  3614. current_path_index = search_my_index (get_path, getlen);
  3615. if (-1 == current_path_index )
  3616. {
  3617. DEBUG ("No entry found in get path.\n");
  3618. GNUNET_break (0);
  3619. return GNUNET_SYSERR;
  3620. }
  3621. if((getlen + 1) == current_path_index)
  3622. {
  3623. DEBUG("Present twice in get path. Not allowed. \n");
  3624. GNUNET_break (0);
  3625. return GNUNET_SYSERR;
  3626. }
  3627. GDS_NEIGHBOURS_send_get_result (&(get_result->key), get_result->type,
  3628. &get_path[current_path_index - 1],
  3629. &(get_result->querying_peer), putlen, put_path,
  3630. getlen, get_path, get_result->expiration_time,
  3631. payload, payload_size);
  3632. return GNUNET_YES;
  3633. }
  3634. return GNUNET_SYSERR;
  3635. }
  3636. /**
  3637. * Find the next hop to pass trail setup message. First find the local best known
  3638. * hop from your own identity, friends and finger. If you were part of trail,
  3639. * then get the next hop from routing table. Compare next_hop from routing table
  3640. * and local best known hop, and return the closest one to final_dest_finger_val
  3641. * @param final_dest_finger_val 64 bit value of finger identity
  3642. * @param intermediate_trail_id If you are part of trail to reach to some other
  3643. * finger, then it is the trail id to reach to
  3644. * that finger, else set to 0.
  3645. * @param is_predecessor Are we looking for closest successor or predecessor.
  3646. * @param source Source of trail setup message.
  3647. * @param current_dest In case you are part of trail, then finger to which
  3648. * we should forward the message. Else my own identity
  3649. * @return Closest Peer for @a final_dest_finger_val
  3650. */
  3651. static struct Closest_Peer
  3652. get_local_best_known_next_hop (uint64_t final_dest_finger_val,
  3653. struct GNUNET_HashCode intermediate_trail_id,
  3654. unsigned int is_predecessor,
  3655. struct GNUNET_PeerIdentity source,
  3656. struct GNUNET_PeerIdentity *current_dest)
  3657. {
  3658. struct Closest_Peer peer;
  3659. peer = find_local_best_known_next_hop (final_dest_finger_val, is_predecessor);
  3660. /* Am I just a part of a trail towards a finger (current_destination)? */
  3661. if (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity, current_dest) &&
  3662. 0 != GNUNET_CRYPTO_cmp_peer_identity (&peer.best_known_destination,
  3663. current_dest))
  3664. {
  3665. struct GNUNET_PeerIdentity closest_peer;
  3666. /* Select best successor among one found locally and current_destination
  3667. * that we got from network.*/
  3668. closest_peer = select_closest_peer (&peer.best_known_destination,
  3669. current_dest,
  3670. final_dest_finger_val,
  3671. is_predecessor);
  3672. /* Is current dest (end point of the trail of which I am a part) closest_peer? */
  3673. if (0 == GNUNET_CRYPTO_cmp_peer_identity (current_dest, &closest_peer))
  3674. {
  3675. struct GNUNET_PeerIdentity *next_hop;
  3676. next_hop = GDS_ROUTING_get_next_hop (intermediate_trail_id,
  3677. GDS_ROUTING_SRC_TO_DEST);
  3678. /* next_hop NULL is a valid case. This intermediate trail id is set by
  3679. some other finger, and while this trail setup is in progress, that other
  3680. peer might have found a better trail ,and send trail teardown message
  3681. across the network. In case we got the trail teardown message first,
  3682. then next_hop will be NULL. A possible solution could be to keep track
  3683. * of all removed trail id, and be sure that there is no other reason . */
  3684. if(NULL != next_hop)
  3685. {
  3686. peer.next_hop = *next_hop;
  3687. peer.best_known_destination = *current_dest;
  3688. peer.trail_id = intermediate_trail_id;
  3689. }
  3690. }
  3691. }
  3692. return peer;
  3693. }
  3694. /*
  3695. * Core handle for PeerTrailSetupMessage.
  3696. * @param cls closure
  3697. * @param message message
  3698. * @param peer peer identity this notification is about
  3699. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  3700. */
  3701. static int
  3702. handle_dht_p2p_trail_setup (void *cls, const struct GNUNET_PeerIdentity *peer,
  3703. const struct GNUNET_MessageHeader *message)
  3704. {
  3705. const struct PeerTrailSetupMessage *trail_setup;
  3706. const struct GNUNET_PeerIdentity *trail_peer_list;
  3707. struct GNUNET_PeerIdentity current_dest;
  3708. struct FriendInfo *target_friend;
  3709. struct GNUNET_PeerIdentity source;
  3710. struct GNUNET_HashCode intermediate_trail_id;
  3711. struct GNUNET_HashCode trail_id;
  3712. unsigned int is_predecessor;
  3713. uint32_t trail_length;
  3714. uint64_t final_dest_finger_val;
  3715. int i;
  3716. size_t msize;
  3717. msize = ntohs (message->size);
  3718. if (msize < sizeof (struct PeerTrailSetupMessage))
  3719. {
  3720. GNUNET_break_op (0);
  3721. return GNUNET_SYSERR;
  3722. }
  3723. trail_setup = (const struct PeerTrailSetupMessage *) message;
  3724. if ((msize - sizeof (struct PeerTrailSetupMessage)) %
  3725. sizeof (struct GNUNET_PeerIdentity) != 0)
  3726. {
  3727. GNUNET_break_op (0);
  3728. return GNUNET_OK;
  3729. }
  3730. trail_length = (msize - sizeof (struct PeerTrailSetupMessage))/
  3731. sizeof (struct GNUNET_PeerIdentity);
  3732. GNUNET_STATISTICS_update (GDS_stats,
  3733. gettext_noop
  3734. ("# Bytes received from other peers"), msize,
  3735. GNUNET_NO);
  3736. trail_peer_list = (const struct GNUNET_PeerIdentity *)&trail_setup[1];
  3737. current_dest = trail_setup->best_known_destination;
  3738. trail_id = trail_setup->trail_id;
  3739. final_dest_finger_val =
  3740. GNUNET_ntohll (trail_setup->final_destination_finger_value);
  3741. source = trail_setup->source_peer;
  3742. is_predecessor = ntohl (trail_setup->is_predecessor);
  3743. intermediate_trail_id = trail_setup->intermediate_trail_id;
  3744. /* Did the friend insert its ID in the trail list? */
  3745. if (trail_length > 0 &&
  3746. 0 != memcmp (&trail_peer_list[trail_length-1], peer, sizeof (struct GNUNET_PeerIdentity)))
  3747. {
  3748. GNUNET_break_op (0);
  3749. return GNUNET_SYSERR;
  3750. }
  3751. /* If I was the source and got the message back, then set trail length to 0.*/
  3752. if (0 == GNUNET_CRYPTO_cmp_peer_identity(&my_identity, &source))
  3753. {
  3754. trail_length = 0;
  3755. }
  3756. /* Check if you are present in the trail seen so far? */
  3757. for (i = 0; i < trail_length ; i++)
  3758. {
  3759. if(0 == GNUNET_CRYPTO_cmp_peer_identity(&trail_peer_list[i],&my_identity))
  3760. {
  3761. /* We will add ourself later in code, if NOT destination. */
  3762. trail_length = i;
  3763. break;
  3764. }
  3765. }
  3766. /* Is my routing table full? */
  3767. if (GNUNET_YES == GDS_ROUTING_threshold_reached())
  3768. {
  3769. if (trail_length > 0)
  3770. target_friend =
  3771. GNUNET_CONTAINER_multipeermap_get (friend_peermap,
  3772. &trail_peer_list[trail_length - 1]);
  3773. else
  3774. target_friend =
  3775. GNUNET_CONTAINER_multipeermap_get (friend_peermap,
  3776. &source);
  3777. if(NULL == target_friend)
  3778. {
  3779. DEBUG ("\n friend not found");
  3780. GNUNET_break(0);
  3781. return GNUNET_OK;
  3782. }
  3783. GDS_NEIGHBOURS_send_trail_rejection (source, final_dest_finger_val,
  3784. my_identity, is_predecessor,
  3785. trail_peer_list, trail_length,
  3786. trail_id, target_friend,
  3787. CONGESTION_TIMEOUT);
  3788. return GNUNET_OK;
  3789. }
  3790. /* Get the next hop to forward the trail setup request. */
  3791. struct Closest_Peer next_peer =
  3792. get_local_best_known_next_hop (final_dest_finger_val,
  3793. intermediate_trail_id,
  3794. is_predecessor,
  3795. source,
  3796. &current_dest);
  3797. /* Am I the final destination? */
  3798. if (0 == (GNUNET_CRYPTO_cmp_peer_identity (&next_peer.best_known_destination,
  3799. &my_identity)))
  3800. {
  3801. if(0 == GNUNET_CRYPTO_cmp_peer_identity (&source, &my_identity))
  3802. {
  3803. finger_table_add (my_identity, NULL, 0, is_predecessor,
  3804. final_dest_finger_val, trail_id);
  3805. return GNUNET_OK;
  3806. }
  3807. if (trail_length > 0)
  3808. target_friend =
  3809. GNUNET_CONTAINER_multipeermap_get (friend_peermap,
  3810. &trail_peer_list[trail_length-1]);
  3811. else
  3812. target_friend =
  3813. GNUNET_CONTAINER_multipeermap_get (friend_peermap, &source);
  3814. if (NULL == target_friend)
  3815. {
  3816. GNUNET_break_op (0);
  3817. return GNUNET_SYSERR;
  3818. }
  3819. GDS_ROUTING_add (trail_id, target_friend->id, my_identity);
  3820. GDS_NEIGHBOURS_send_trail_setup_result (source,
  3821. my_identity,
  3822. target_friend, trail_length,
  3823. trail_peer_list,
  3824. is_predecessor,
  3825. final_dest_finger_val,trail_id);
  3826. }
  3827. else /* I'm not the final destination. */
  3828. {
  3829. target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
  3830. &next_peer.next_hop);
  3831. if(NULL == target_friend)
  3832. {
  3833. DEBUG ("\n target friend not found for peer = %s", GNUNET_i2s(&next_peer.next_hop));
  3834. GNUNET_break (0);
  3835. return GNUNET_OK;
  3836. }
  3837. if (0 != GNUNET_CRYPTO_cmp_peer_identity(&my_identity, &source))
  3838. {
  3839. /* Add yourself to list of peers. */
  3840. struct GNUNET_PeerIdentity peer_list[trail_length + 1];
  3841. memcpy (peer_list, trail_peer_list,
  3842. trail_length * sizeof (struct GNUNET_PeerIdentity));
  3843. peer_list[trail_length] = my_identity;
  3844. GDS_NEIGHBOURS_send_trail_setup (source,
  3845. final_dest_finger_val,
  3846. next_peer.best_known_destination,
  3847. target_friend, trail_length + 1, peer_list,
  3848. is_predecessor, trail_id,
  3849. next_peer.trail_id);
  3850. }
  3851. else
  3852. GDS_NEIGHBOURS_send_trail_setup (source,
  3853. final_dest_finger_val,
  3854. next_peer.best_known_destination,
  3855. target_friend, 0, NULL,
  3856. is_predecessor, trail_id,
  3857. next_peer.trail_id);
  3858. }
  3859. return GNUNET_OK;
  3860. }
  3861. /**
  3862. * Core handle for p2p trail setup result messages.
  3863. * @param closure
  3864. * @param message message
  3865. * @param peer sender of this message.
  3866. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  3867. */
  3868. static int
  3869. handle_dht_p2p_trail_setup_result(void *cls, const struct GNUNET_PeerIdentity *peer,
  3870. const struct GNUNET_MessageHeader *message)
  3871. {
  3872. const struct PeerTrailSetupResultMessage *trail_result;
  3873. const struct GNUNET_PeerIdentity *trail_peer_list;
  3874. struct GNUNET_PeerIdentity next_hop;
  3875. struct FriendInfo *target_friend;
  3876. struct GNUNET_PeerIdentity querying_peer;
  3877. struct GNUNET_PeerIdentity finger_identity;
  3878. uint32_t trail_length;
  3879. uint64_t ulitmate_destination_finger_value;
  3880. uint32_t is_predecessor;
  3881. struct GNUNET_HashCode trail_id;
  3882. int my_index;
  3883. size_t msize;
  3884. msize = ntohs (message->size);
  3885. if (msize < sizeof (struct PeerTrailSetupResultMessage))
  3886. {
  3887. GNUNET_break_op (0);
  3888. return GNUNET_YES;
  3889. }
  3890. trail_result = (const struct PeerTrailSetupResultMessage *) message;
  3891. if ((msize - sizeof (struct PeerTrailSetupResultMessage)) %
  3892. sizeof (struct GNUNET_PeerIdentity) != 0)
  3893. {
  3894. GNUNET_break_op (0);
  3895. return GNUNET_OK;
  3896. }
  3897. trail_length = (msize - sizeof (struct PeerTrailSetupResultMessage))/
  3898. sizeof (struct GNUNET_PeerIdentity);
  3899. GNUNET_STATISTICS_update (GDS_stats,
  3900. gettext_noop
  3901. ("# Bytes received from other peers"), msize,
  3902. GNUNET_NO);
  3903. is_predecessor = ntohl (trail_result->is_predecessor);
  3904. querying_peer = trail_result->querying_peer;
  3905. finger_identity = trail_result->finger_identity;
  3906. trail_id = trail_result->trail_id;
  3907. trail_peer_list = (const struct GNUNET_PeerIdentity *) &trail_result[1];
  3908. ulitmate_destination_finger_value =
  3909. GNUNET_ntohll (trail_result->ulitmate_destination_finger_value);
  3910. /* Am I the one who initiated the query? */
  3911. if (0 == (GNUNET_CRYPTO_cmp_peer_identity (&querying_peer, &my_identity)))
  3912. {
  3913. /* Check that you got the message from the correct peer. */
  3914. if (trail_length > 0)
  3915. {
  3916. GNUNET_assert(0 == GNUNET_CRYPTO_cmp_peer_identity (&trail_peer_list[0],
  3917. peer));
  3918. }
  3919. else
  3920. {
  3921. GNUNET_assert(0 == GNUNET_CRYPTO_cmp_peer_identity (&finger_identity,
  3922. peer));
  3923. }
  3924. GDS_ROUTING_add (trail_id, my_identity, *peer);
  3925. finger_table_add (finger_identity, trail_peer_list, trail_length,
  3926. is_predecessor, ulitmate_destination_finger_value, trail_id);
  3927. return GNUNET_YES;
  3928. }
  3929. /* Get my location in the trail. */
  3930. my_index = search_my_index (trail_peer_list, trail_length);
  3931. if (-1 == my_index)
  3932. {
  3933. DEBUG ("Not found in trail\n");
  3934. GNUNET_break_op(0);
  3935. return GNUNET_SYSERR;
  3936. }
  3937. //TODO; return -2.
  3938. if ((trail_length + 1) == my_index)
  3939. {
  3940. DEBUG ("Found twice in trail.\n");
  3941. GNUNET_break_op(0);
  3942. return GNUNET_SYSERR;
  3943. }
  3944. //TODO; Refactor code here and above to check if sender peer is correct
  3945. if (my_index == 0)
  3946. {
  3947. if(trail_length > 1)
  3948. GNUNET_assert(0 == GNUNET_CRYPTO_cmp_peer_identity (&trail_peer_list[1],
  3949. peer));
  3950. else
  3951. GNUNET_assert(0 == GNUNET_CRYPTO_cmp_peer_identity (&finger_identity,
  3952. peer));
  3953. next_hop = trail_result->querying_peer;
  3954. }
  3955. else
  3956. {
  3957. if(my_index == trail_length - 1)
  3958. {
  3959. GNUNET_assert(0 ==
  3960. GNUNET_CRYPTO_cmp_peer_identity (&finger_identity,
  3961. peer));
  3962. }
  3963. else
  3964. GNUNET_assert(0 ==
  3965. GNUNET_CRYPTO_cmp_peer_identity (&trail_peer_list[my_index + 1],
  3966. peer));
  3967. next_hop = trail_peer_list[my_index - 1];
  3968. }
  3969. target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_hop);
  3970. if (NULL == target_friend)
  3971. {
  3972. GNUNET_break_op (0);
  3973. return GNUNET_SYSERR;
  3974. }
  3975. GDS_ROUTING_add (trail_id, next_hop, *peer);
  3976. GDS_NEIGHBOURS_send_trail_setup_result (querying_peer, finger_identity,
  3977. target_friend, trail_length, trail_peer_list,
  3978. is_predecessor,
  3979. ulitmate_destination_finger_value,
  3980. trail_id);
  3981. return GNUNET_OK;
  3982. }
  3983. /**
  3984. * Invert the trail.
  3985. * @param trail Trail to be inverted
  3986. * @param trail_length Total number of peers in the trail.
  3987. * @return Updated trail
  3988. */
  3989. static struct GNUNET_PeerIdentity *
  3990. invert_trail (const struct GNUNET_PeerIdentity *trail,
  3991. unsigned int trail_length)
  3992. {
  3993. int i;
  3994. int j;
  3995. struct GNUNET_PeerIdentity *inverted_trail;
  3996. inverted_trail = GNUNET_malloc (sizeof(struct GNUNET_PeerIdentity) *
  3997. trail_length);
  3998. i = 0;
  3999. j = trail_length - 1;
  4000. while (i < trail_length)
  4001. {
  4002. inverted_trail[i] = trail[j];
  4003. i++;
  4004. j--;
  4005. }
  4006. GNUNET_assert(NULL != GNUNET_CONTAINER_multipeermap_get(friend_peermap,
  4007. &inverted_trail[0]));
  4008. return inverted_trail;
  4009. }
  4010. /**
  4011. * Return the shortest trail among all the trails to reach to finger from me.
  4012. * @param finger Finger
  4013. * @param shortest_trail_length[out] Trail length of shortest trail from me
  4014. * to @a finger
  4015. * @return Shortest trail.
  4016. */
  4017. static struct GNUNET_PeerIdentity *
  4018. get_shortest_trail (struct FingerInfo *finger,
  4019. unsigned int *trail_length)
  4020. {
  4021. struct Trail *trail;
  4022. unsigned int flag = 0;
  4023. unsigned int shortest_trail_index = 0;
  4024. int shortest_trail_length = -1;
  4025. struct Trail_Element *trail_element;
  4026. struct GNUNET_PeerIdentity *trail_list;
  4027. unsigned int i;
  4028. trail = GNUNET_new (struct Trail);
  4029. /* Get the shortest trail to reach to current successor. */
  4030. for (i = 0; i < finger->trails_count; i++)
  4031. {
  4032. trail = &finger->trail_list[i];
  4033. if (0 == flag)
  4034. {
  4035. shortest_trail_index = i;
  4036. shortest_trail_length = trail->trail_length;
  4037. flag = 1;
  4038. continue;
  4039. }
  4040. if (shortest_trail_length > trail->trail_length)
  4041. {
  4042. shortest_trail_index = i;
  4043. shortest_trail_length = trail->trail_length;
  4044. }
  4045. continue;
  4046. }
  4047. /* Copy the shortest trail and return. */
  4048. trail = &finger->trail_list[shortest_trail_index];
  4049. trail_element = trail->trail_head;
  4050. trail_list = GNUNET_malloc (sizeof(struct GNUNET_PeerIdentity)*
  4051. shortest_trail_length);
  4052. for(i = 0; i < shortest_trail_length; i++,trail_element = trail_element->next)
  4053. {
  4054. trail_list[i] = trail_element->peer;
  4055. }
  4056. GNUNET_assert(shortest_trail_length != -1);
  4057. *trail_length = shortest_trail_length;
  4058. return trail_list;
  4059. }
  4060. /**
  4061. * Check if trail_1 and trail_2 have any common element. If yes then join
  4062. * them at common element. trail_1 always preceeds trail_2 in joined trail.
  4063. * @param trail_1 Trail from source to me, NOT including endpoints.
  4064. * @param trail_1_len Total number of peers @a trail_1
  4065. * @param trail_2 Trail from me to current predecessor, NOT including endpoints.
  4066. * @param trail_2_len Total number of peers @a trail_2
  4067. * @param joined_trail_len Total number of peers in combined trail of trail_1
  4068. * trail_2.
  4069. * @return Joined trail.
  4070. */
  4071. static struct GNUNET_PeerIdentity *
  4072. check_for_duplicate_entries (const struct GNUNET_PeerIdentity *trail_1,
  4073. unsigned int trail_1_len,
  4074. struct GNUNET_PeerIdentity *trail_2,
  4075. unsigned int trail_2_len,
  4076. unsigned int *joined_trail_len)
  4077. {
  4078. struct GNUNET_PeerIdentity *joined_trail;
  4079. unsigned int i;
  4080. unsigned int j;
  4081. unsigned int k;
  4082. for (i = 0; i < trail_1_len; i++)
  4083. {
  4084. for (j = 0; j < trail_2_len; j++)
  4085. {
  4086. if(0 != GNUNET_CRYPTO_cmp_peer_identity (&trail_1[i],&trail_2[j]))
  4087. continue;
  4088. *joined_trail_len = i + (trail_2_len - j);
  4089. joined_trail = GNUNET_malloc (*joined_trail_len *
  4090. sizeof(struct GNUNET_PeerIdentity));
  4091. /* Copy all the elements from 0 to i into joined_trail. */
  4092. for(k = 0; k < ( i+1); k++)
  4093. {
  4094. joined_trail[k] = trail_1[k];
  4095. }
  4096. /* Increment j as entry stored is same as entry stored at i*/
  4097. j = j+1;
  4098. /* Copy all the elements from j to trail_2_len-1 to joined trail.*/
  4099. while(k <= (*joined_trail_len - 1))
  4100. {
  4101. joined_trail[k] = trail_2[j];
  4102. j++;
  4103. k++;
  4104. }
  4105. return joined_trail;
  4106. }
  4107. }
  4108. /* Here you should join the trails. */
  4109. *joined_trail_len = trail_1_len + trail_2_len + 1;
  4110. joined_trail = GNUNET_malloc (*joined_trail_len *
  4111. sizeof(struct GNUNET_PeerIdentity));
  4112. for(i = 0; i < trail_1_len;i++)
  4113. {
  4114. joined_trail[i] = trail_1[i];
  4115. }
  4116. joined_trail[i] = my_identity;
  4117. i++;
  4118. for (j = 0; i < *joined_trail_len; i++,j++)
  4119. {
  4120. joined_trail[i] = trail_2[j];
  4121. }
  4122. return joined_trail;
  4123. }
  4124. /**
  4125. * Return the trail from source to my current predecessor. Check if source
  4126. * is already part of the this trail, if yes then return the shorten trail.
  4127. * @param current_trail Trail from source to me, NOT including the endpoints.
  4128. * @param current_trail_length Number of peers in @a current_trail.
  4129. * @param trail_src_to_curr_pred_length[out] Number of peers in trail from
  4130. * source to my predecessor, NOT including
  4131. * the endpoints.
  4132. * @return Trail from source to my predecessor.
  4133. */
  4134. static struct GNUNET_PeerIdentity *
  4135. get_trail_src_to_curr_pred (struct GNUNET_PeerIdentity source_peer,
  4136. const struct GNUNET_PeerIdentity *trail_src_to_me,
  4137. unsigned int trail_src_to_me_len,
  4138. unsigned int *trail_src_to_curr_pred_length)
  4139. {
  4140. struct GNUNET_PeerIdentity *trail_me_to_curr_pred;
  4141. struct GNUNET_PeerIdentity *trail_src_to_curr_pred;
  4142. unsigned int trail_me_to_curr_pred_length;
  4143. struct FingerInfo *current_predecessor;
  4144. int i;
  4145. unsigned int j;
  4146. unsigned int len;
  4147. current_predecessor = &finger_table[PREDECESSOR_FINGER_ID];
  4148. /* Check if trail_src_to_me contains current_predecessor. */
  4149. for (i = 0; i < trail_src_to_me_len; i++)
  4150. {
  4151. if (0 != GNUNET_CRYPTO_cmp_peer_identity(&trail_src_to_me[i],
  4152. &current_predecessor->finger_identity))
  4153. continue;
  4154. *trail_src_to_curr_pred_length = i;
  4155. if(0 == i)
  4156. return NULL;
  4157. trail_src_to_curr_pred = GNUNET_malloc (*trail_src_to_curr_pred_length *
  4158. sizeof(struct GNUNET_PeerIdentity));
  4159. for (j = 0; j < i; j++)
  4160. trail_src_to_curr_pred[j] = trail_src_to_me[j];
  4161. return trail_src_to_curr_pred;
  4162. }
  4163. trail_me_to_curr_pred = get_shortest_trail (current_predecessor,
  4164. &trail_me_to_curr_pred_length);
  4165. /* Check if trail contains the source_peer. */
  4166. for (i = trail_me_to_curr_pred_length - 1; i >= 0; i--)
  4167. {
  4168. if (0 != GNUNET_CRYPTO_cmp_peer_identity (&source_peer,
  4169. &trail_me_to_curr_pred[i]))
  4170. continue;
  4171. /* Source is NOT part of trail. */
  4172. i++;
  4173. /* Source is the last element in the trail to reach to my pred.
  4174. Source is direct friend of the pred. */
  4175. if (trail_me_to_curr_pred_length == i)
  4176. {
  4177. *trail_src_to_curr_pred_length = 0;
  4178. GNUNET_free_non_null (trail_me_to_curr_pred);
  4179. return NULL;
  4180. }
  4181. *trail_src_to_curr_pred_length = trail_me_to_curr_pred_length - i;
  4182. trail_src_to_curr_pred = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity)*
  4183. *trail_src_to_curr_pred_length);
  4184. for (j = 0; j < *trail_src_to_curr_pred_length; i++,j++)
  4185. trail_src_to_curr_pred[j] = trail_me_to_curr_pred[i];
  4186. GNUNET_free_non_null (trail_me_to_curr_pred);
  4187. return trail_src_to_curr_pred;
  4188. }
  4189. trail_src_to_curr_pred = check_for_duplicate_entries (trail_src_to_me,
  4190. trail_src_to_me_len,
  4191. trail_me_to_curr_pred,
  4192. trail_me_to_curr_pred_length,
  4193. &len);
  4194. *trail_src_to_curr_pred_length = len;
  4195. GNUNET_free_non_null(trail_me_to_curr_pred);
  4196. return trail_src_to_curr_pred;
  4197. }
  4198. /**
  4199. * Add finger as your predecessor. To add, first generate a new trail id, invert
  4200. * the trail to get the trail from me to finger, add an entry in your routing
  4201. * table, send add trail message to peers which are part of trail from me to
  4202. * finger and add finger in finger table.
  4203. * @param finger
  4204. * @param trail
  4205. * @param trail_length
  4206. */
  4207. static void
  4208. update_predecessor (struct GNUNET_PeerIdentity finger,
  4209. struct GNUNET_PeerIdentity *trail,
  4210. unsigned int trail_length)
  4211. {
  4212. struct GNUNET_HashCode trail_to_new_predecessor_id;
  4213. struct GNUNET_PeerIdentity *trail_to_new_predecessor;
  4214. struct FriendInfo *target_friend;
  4215. /* Generate trail id for trail from me to new predecessor = finger. */
  4216. GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_STRONG,
  4217. &trail_to_new_predecessor_id,
  4218. sizeof (trail_to_new_predecessor_id));
  4219. if (0 == trail_length)
  4220. {
  4221. trail_to_new_predecessor = NULL;
  4222. GDS_ROUTING_add (trail_to_new_predecessor_id, my_identity, finger);
  4223. target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, &finger);
  4224. if (NULL == target_friend)
  4225. {
  4226. GNUNET_break (0);
  4227. return;
  4228. }
  4229. }
  4230. else
  4231. {
  4232. /* Invert the trail to get the trail from me to finger, NOT including the
  4233. endpoints.*/
  4234. GNUNET_assert(NULL != GNUNET_CONTAINER_multipeermap_get(friend_peermap,
  4235. &trail[trail_length-1]));
  4236. trail_to_new_predecessor = invert_trail (trail, trail_length);
  4237. /* Add an entry in your routing table. */
  4238. GDS_ROUTING_add (trail_to_new_predecessor_id,
  4239. my_identity,
  4240. trail_to_new_predecessor[0]);
  4241. GNUNET_assert (NULL != (target_friend =
  4242. GNUNET_CONTAINER_multipeermap_get (friend_peermap,
  4243. &trail_to_new_predecessor[0])));
  4244. }
  4245. /* Add entry in routing table of all peers that are part of trail from me
  4246. to finger, including finger. */
  4247. GDS_NEIGHBOURS_send_add_trail (my_identity,
  4248. finger,
  4249. trail_to_new_predecessor_id,
  4250. trail_to_new_predecessor,
  4251. trail_length,
  4252. target_friend);
  4253. add_new_finger (finger, trail_to_new_predecessor, trail_length,
  4254. trail_to_new_predecessor_id, PREDECESSOR_FINGER_ID);
  4255. GNUNET_free_non_null(trail_to_new_predecessor);
  4256. }
  4257. /*
  4258. * Check if you already have a predecessor. If not then add finger as your
  4259. * predecessor. If you have predecessor, then compare two peer identites.
  4260. * If finger is correct predecessor, then remove the old entry, add finger in
  4261. * finger table and send add_trail message to add the trail in the routing
  4262. * table of all peers which are part of trail to reach from me to finger.
  4263. * @param finger New peer which may be our predecessor.
  4264. * @param trail List of peers to reach from @finger to me.
  4265. * @param trail_length Total number of peer in @a trail.
  4266. */
  4267. static void
  4268. compare_and_update_predecessor (struct GNUNET_PeerIdentity finger,
  4269. struct GNUNET_PeerIdentity *trail,
  4270. unsigned int trail_length)
  4271. {
  4272. struct FingerInfo *current_predecessor;
  4273. struct GNUNET_PeerIdentity closest_peer;
  4274. uint64_t predecessor_value;
  4275. unsigned int is_predecessor = 1;
  4276. current_predecessor = &finger_table[PREDECESSOR_FINGER_ID];
  4277. GNUNET_assert (0 != GNUNET_CRYPTO_cmp_peer_identity (&finger, &my_identity));
  4278. /* No predecessor. Add finger as your predecessor. */
  4279. if (GNUNET_NO == current_predecessor->is_present)
  4280. {
  4281. update_predecessor (finger, trail, trail_length);
  4282. return;
  4283. }
  4284. if (0 == GNUNET_CRYPTO_cmp_peer_identity (&current_predecessor->finger_identity,
  4285. &finger))
  4286. {
  4287. return;
  4288. }
  4289. predecessor_value = compute_finger_identity_value (PREDECESSOR_FINGER_ID);
  4290. closest_peer = select_closest_peer (&finger,
  4291. &current_predecessor->finger_identity,
  4292. predecessor_value, is_predecessor);
  4293. /* Finger is the closest predecessor. Remove the existing one and add the new
  4294. one. */
  4295. if (0 == GNUNET_CRYPTO_cmp_peer_identity(&closest_peer, &finger))
  4296. {
  4297. remove_existing_finger (current_predecessor, PREDECESSOR_FINGER_ID);
  4298. update_predecessor (finger, trail, trail_length);
  4299. return;
  4300. }
  4301. return;
  4302. }
  4303. /*
  4304. * Core handle for p2p verify successor messages.
  4305. * @param cls closure
  4306. * @param message message
  4307. * @param peer peer identity this notification is about
  4308. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  4309. */
  4310. static int
  4311. handle_dht_p2p_verify_successor(void *cls,
  4312. const struct GNUNET_PeerIdentity *peer,
  4313. const struct GNUNET_MessageHeader *message)
  4314. {
  4315. const struct PeerVerifySuccessorMessage *vsm;
  4316. struct GNUNET_HashCode trail_id;
  4317. struct GNUNET_PeerIdentity successor;
  4318. struct GNUNET_PeerIdentity source_peer;
  4319. struct GNUNET_PeerIdentity *trail;
  4320. struct GNUNET_PeerIdentity *next_hop;
  4321. struct FingerInfo current_predecessor;
  4322. struct FriendInfo *target_friend;
  4323. unsigned int trail_src_to_curr_pred_len = 0;
  4324. struct GNUNET_PeerIdentity *trail_src_to_curr_pred;
  4325. unsigned int trail_length;
  4326. size_t msize;
  4327. msize = ntohs (message->size);
  4328. if (msize < sizeof (struct PeerVerifySuccessorMessage))
  4329. {
  4330. GNUNET_break_op (0);
  4331. return GNUNET_YES;
  4332. }
  4333. vsm = (const struct PeerVerifySuccessorMessage *) message;
  4334. trail_length = (msize - sizeof (struct PeerVerifySuccessorMessage))/
  4335. sizeof (struct GNUNET_PeerIdentity);
  4336. if ((msize - sizeof (struct PeerVerifySuccessorMessage)) %
  4337. sizeof (struct GNUNET_PeerIdentity) != 0)
  4338. {
  4339. GNUNET_break_op (0);
  4340. return GNUNET_OK;
  4341. }
  4342. GNUNET_STATISTICS_update (GDS_stats,
  4343. gettext_noop
  4344. ("# Bytes received from other peers"), msize,
  4345. GNUNET_NO);
  4346. trail_id = vsm->trail_id;
  4347. source_peer = vsm->source_peer;
  4348. successor = vsm->successor;
  4349. trail = (struct GNUNET_PeerIdentity *)&vsm[1];
  4350. /* I am NOT the successor of source_peer. Pass the message to next_hop on
  4351. * the trail. */
  4352. if(0 != (GNUNET_CRYPTO_cmp_peer_identity (&successor, &my_identity)))
  4353. {
  4354. next_hop = GDS_ROUTING_get_next_hop (trail_id, GDS_ROUTING_SRC_TO_DEST);
  4355. if (NULL == next_hop)
  4356. {
  4357. return GNUNET_OK;
  4358. }
  4359. target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop);
  4360. if(NULL == target_friend)
  4361. {
  4362. GNUNET_break_op(0);
  4363. return GNUNET_OK;
  4364. }
  4365. GDS_NEIGHBOURS_send_verify_successor_message (source_peer, successor,
  4366. trail_id, trail, trail_length,
  4367. target_friend);
  4368. return GNUNET_OK;
  4369. }
  4370. /* I am the destination of this message. */
  4371. /* Check if the source_peer could be our predecessor and if yes then update
  4372. * it. */
  4373. compare_and_update_predecessor (source_peer, trail, trail_length);
  4374. current_predecessor = finger_table[PREDECESSOR_FINGER_ID];
  4375. /* Is source of this message NOT my predecessor. */
  4376. if (0 != (GNUNET_CRYPTO_cmp_peer_identity (&current_predecessor.finger_identity,
  4377. &source_peer)))
  4378. {
  4379. trail_src_to_curr_pred =
  4380. get_trail_src_to_curr_pred (source_peer,
  4381. trail,
  4382. trail_length,
  4383. &trail_src_to_curr_pred_len);
  4384. }
  4385. else
  4386. {
  4387. trail_src_to_curr_pred_len = trail_length;
  4388. unsigned int i;
  4389. trail_src_to_curr_pred =
  4390. GNUNET_malloc (sizeof(struct GNUNET_PeerIdentity)
  4391. *trail_src_to_curr_pred_len);
  4392. for(i = 0; i < trail_src_to_curr_pred_len; i++)
  4393. {
  4394. trail_src_to_curr_pred[i] = trail[i];
  4395. }
  4396. }
  4397. GNUNET_assert (NULL !=
  4398. (target_friend =
  4399. GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer)));
  4400. GDS_NEIGHBOURS_send_verify_successor_result (source_peer, my_identity,
  4401. current_predecessor.finger_identity,
  4402. trail_id, trail_src_to_curr_pred,
  4403. trail_src_to_curr_pred_len,
  4404. GDS_ROUTING_DEST_TO_SRC,
  4405. target_friend);
  4406. GNUNET_free_non_null(trail_src_to_curr_pred);
  4407. return GNUNET_OK;
  4408. }
  4409. /**
  4410. * If the trail from me to my probable successor contains a friend not
  4411. * at index 0, then we can shorten the trail.
  4412. * @param probable_successor Peer which is our probable successor
  4413. * @param trail_me_to_probable_successor Peers in path from me to my probable
  4414. * successor, NOT including the endpoints.
  4415. * @param trail_me_to_probable_successor_len Total number of peers in
  4416. * @a trail_me_to_probable_succesor.
  4417. * @return Updated trail, if any friend found.
  4418. * Else the trail_me_to_probable_successor.
  4419. */
  4420. struct GNUNET_PeerIdentity *
  4421. check_trail_me_to_probable_succ (struct GNUNET_PeerIdentity probable_successor,
  4422. const struct GNUNET_PeerIdentity *trail_me_to_probable_successor,
  4423. unsigned int trail_me_to_probable_successor_len,
  4424. unsigned int *trail_to_new_successor_length)
  4425. {
  4426. unsigned int i;
  4427. unsigned int j;
  4428. struct GNUNET_PeerIdentity *trail_to_new_successor;
  4429. /* Probable successor is a friend */
  4430. if (NULL != GNUNET_CONTAINER_multipeermap_get (friend_peermap,
  4431. &probable_successor))
  4432. {
  4433. trail_to_new_successor = NULL;
  4434. *trail_to_new_successor_length = 0;
  4435. return trail_to_new_successor;
  4436. }
  4437. /* Is there any friend of yours in this trail. */
  4438. if(trail_me_to_probable_successor_len > 1)
  4439. {
  4440. for (i = trail_me_to_probable_successor_len - 1; i > 0; i--)
  4441. {
  4442. if (NULL == GNUNET_CONTAINER_multipeermap_get (friend_peermap,
  4443. &trail_me_to_probable_successor[i]))
  4444. continue;
  4445. *trail_to_new_successor_length = (trail_me_to_probable_successor_len - i);
  4446. trail_to_new_successor = GNUNET_malloc (sizeof(struct GNUNET_PeerIdentity)*
  4447. *trail_to_new_successor_length);
  4448. for(j = 0; j < *trail_to_new_successor_length; i++,j++)
  4449. {
  4450. trail_to_new_successor[j] = trail_me_to_probable_successor[i];
  4451. }
  4452. return trail_to_new_successor;
  4453. }
  4454. }
  4455. *trail_to_new_successor_length = trail_me_to_probable_successor_len;
  4456. return (struct GNUNET_PeerIdentity*)trail_me_to_probable_successor;
  4457. }
  4458. // TODO: Move up
  4459. struct SendNotifyContext
  4460. {
  4461. struct GNUNET_PeerIdentity source_peer;
  4462. struct GNUNET_PeerIdentity successor;
  4463. struct GNUNET_PeerIdentity *successor_trail;
  4464. unsigned int successor_trail_length;
  4465. struct GNUNET_HashCode succesor_trail_id;
  4466. struct FriendInfo *target_friend;
  4467. unsigned int num_retries_scheduled;
  4468. };
  4469. void
  4470. send_notify_new_successor (void *cls,
  4471. const struct GNUNET_SCHEDULER_TaskContext
  4472. * tc);
  4473. /**
  4474. * Check if the peer which sent us verify successor result message is still ours
  4475. * successor or not. If not, then compare existing successor and probable successor.
  4476. * In case probable successor is the correct successor, remove the existing
  4477. * successor. Add probable successor as new successor. Send notify new successor
  4478. * message to new successor.
  4479. * @param curr_succ Peer to which we sent the verify successor message. It may
  4480. * or may not be our real current successor, as we may have few iterations of
  4481. * find finger trail task.
  4482. * @param probable_successor Peer which should be our successor accroding to @a
  4483. * curr_succ
  4484. * @param trail List of peers to reach from me to @a probable successor, NOT including
  4485. * endpoints.
  4486. * @param trail_length Total number of peers in @a trail.
  4487. */
  4488. static void
  4489. compare_and_update_successor (struct GNUNET_PeerIdentity curr_succ,
  4490. struct GNUNET_PeerIdentity probable_successor,
  4491. const struct GNUNET_PeerIdentity *trail,
  4492. unsigned int trail_length)
  4493. {
  4494. struct FingerInfo *current_successor;
  4495. struct GNUNET_PeerIdentity closest_peer;
  4496. struct GNUNET_HashCode trail_id;
  4497. struct GNUNET_PeerIdentity *trail_me_to_probable_succ;
  4498. struct FriendInfo *target_friend;
  4499. unsigned int trail_me_to_probable_succ_len;
  4500. unsigned int is_predecessor = 0;
  4501. uint64_t successor_value;
  4502. current_successor = &finger_table[0];
  4503. successor_value = compute_finger_identity_value(0);
  4504. /* If probable successor is same as current_successor, do nothing. */
  4505. if(0 == GNUNET_CRYPTO_cmp_peer_identity (&probable_successor,
  4506. &current_successor->finger_identity))
  4507. {
  4508. if ((NULL != GDS_stats))
  4509. {
  4510. char *my_id_str;
  4511. uint64_t succ;
  4512. char *key;
  4513. uint64_t my_id;
  4514. memcpy (&my_id, &my_identity, sizeof(uint64_t));
  4515. my_id_str = GNUNET_strdup (GNUNET_i2s_full (&my_identity));
  4516. memcpy(&succ, &current_successor->finger_identity, sizeof(uint64_t));
  4517. succ = GNUNET_ntohll(succ);
  4518. GNUNET_asprintf (&key, "XDHT:%s:", my_id_str);
  4519. GNUNET_free (my_id_str);
  4520. GNUNET_STATISTICS_set (GDS_stats, key, succ, 0);
  4521. GNUNET_free (key);
  4522. }
  4523. if (send_verify_successor_task == GNUNET_SCHEDULER_NO_TASK)
  4524. send_verify_successor_task =
  4525. GNUNET_SCHEDULER_add_delayed(verify_successor_next_send_time,
  4526. &send_verify_successor_message,
  4527. NULL);
  4528. return;
  4529. }
  4530. closest_peer = select_closest_peer (&probable_successor,
  4531. &current_successor->finger_identity,
  4532. successor_value, is_predecessor);
  4533. /* If the current_successor in the finger table is closest, then do nothing. */
  4534. if (0 == GNUNET_CRYPTO_cmp_peer_identity (&closest_peer ,
  4535. &current_successor->finger_identity))
  4536. {
  4537. //FIXME: Is this a good place to return the stats.
  4538. if ((NULL != GDS_stats))
  4539. {
  4540. char *my_id_str;
  4541. uint64_t succ;
  4542. char *key;
  4543. my_id_str = GNUNET_strdup (GNUNET_i2s_full (&my_identity));
  4544. memcpy(&succ, &current_successor->finger_identity, sizeof(uint64_t));
  4545. GNUNET_asprintf (&key, "XDHT:%s:", my_id_str);
  4546. GNUNET_free (my_id_str);
  4547. GNUNET_STATISTICS_set (GDS_stats, key, succ, 0);
  4548. GNUNET_free (key);
  4549. }
  4550. if(0 == successor_times)
  4551. {
  4552. // successor_times = 3;
  4553. verify_successor_next_send_time =
  4554. GNUNET_TIME_STD_BACKOFF (verify_successor_next_send_time);
  4555. }
  4556. else
  4557. successor_times--;
  4558. if (send_verify_successor_task == GNUNET_SCHEDULER_NO_TASK)
  4559. send_verify_successor_task =
  4560. GNUNET_SCHEDULER_add_delayed(verify_successor_next_send_time,
  4561. &send_verify_successor_message,
  4562. NULL);
  4563. return;
  4564. }
  4565. /* Probable successor is the closest peer.*/
  4566. if(trail_length > 0)
  4567. {
  4568. GNUNET_assert(NULL != GNUNET_CONTAINER_multipeermap_get(friend_peermap,
  4569. &trail[0]));
  4570. }
  4571. else
  4572. {
  4573. GNUNET_assert(NULL != GNUNET_CONTAINER_multipeermap_get(friend_peermap,
  4574. &probable_successor));
  4575. }
  4576. trail_me_to_probable_succ_len = 0;
  4577. trail_me_to_probable_succ =
  4578. check_trail_me_to_probable_succ (probable_successor,
  4579. trail, trail_length,
  4580. &trail_me_to_probable_succ_len);
  4581. /* Remove the existing successor. */
  4582. remove_existing_finger (current_successor, 0);
  4583. /* Generate a new trail id to reach to your new successor. */
  4584. GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_STRONG,
  4585. &trail_id, sizeof (trail_id));
  4586. if (trail_me_to_probable_succ_len > 0)
  4587. {
  4588. GDS_ROUTING_add (trail_id, my_identity, trail_me_to_probable_succ[0]);
  4589. GNUNET_assert (NULL !=
  4590. (target_friend =
  4591. GNUNET_CONTAINER_multipeermap_get (friend_peermap,
  4592. &trail_me_to_probable_succ[0])));
  4593. }
  4594. else
  4595. {
  4596. GDS_ROUTING_add (trail_id, my_identity, probable_successor);
  4597. GNUNET_assert (NULL !=
  4598. (target_friend =
  4599. GNUNET_CONTAINER_multipeermap_get (friend_peermap,
  4600. &probable_successor)));
  4601. }
  4602. add_new_finger (probable_successor, trail_me_to_probable_succ,
  4603. trail_me_to_probable_succ_len, trail_id, 0);
  4604. struct SendNotifyContext *notify_ctx;
  4605. notify_ctx = GNUNET_new(struct SendNotifyContext);
  4606. notify_ctx->source_peer = my_identity;
  4607. notify_ctx->successor = probable_successor;
  4608. notify_ctx->successor_trail =
  4609. GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity) * trail_me_to_probable_succ_len);
  4610. memcpy(notify_ctx->successor_trail, trail_me_to_probable_succ,
  4611. sizeof(struct GNUNET_PeerIdentity) * trail_me_to_probable_succ_len);
  4612. notify_ctx->successor_trail_length = trail_me_to_probable_succ_len;
  4613. notify_ctx->succesor_trail_id = trail_id;
  4614. notify_ctx->target_friend = target_friend;
  4615. notify_ctx->num_retries_scheduled = 0;
  4616. // TODO: Check if we should verify before schedule if already scheduled.
  4617. GNUNET_SCHEDULER_add_now(&send_notify_new_successor, (void*)notify_ctx);
  4618. return;
  4619. }
  4620. void
  4621. send_notify_new_successor (void *cls,
  4622. const struct GNUNET_SCHEDULER_TaskContext
  4623. * tc)
  4624. {
  4625. struct SendNotifyContext *ctx = (struct SendNotifyContext *) cls;
  4626. GDS_NEIGHBOURS_send_notify_new_successor (ctx->source_peer,
  4627. ctx->successor,
  4628. ctx->successor_trail,
  4629. ctx->successor_trail_length,
  4630. ctx->succesor_trail_id,
  4631. ctx->target_friend);
  4632. if (0 == ctx->num_retries_scheduled &&
  4633. send_notify_new_successor_retry_task != GNUNET_SCHEDULER_NO_TASK)
  4634. {
  4635. // Result from previous notify successos hasn't arrived, so the retry task
  4636. // hasn't been cancelled! Already a new notify successor must be called.
  4637. // We will cancel the retry request.
  4638. struct SendNotifyContext *old_notify_ctx;
  4639. old_notify_ctx = GNUNET_SCHEDULER_cancel(send_notify_new_successor_retry_task);
  4640. GNUNET_free (old_notify_ctx->successor_trail);
  4641. GNUNET_free (old_notify_ctx);
  4642. send_notify_new_successor_retry_task = GNUNET_SCHEDULER_NO_TASK;
  4643. }
  4644. ctx->num_retries_scheduled++;
  4645. send_notify_new_successor_retry_task = GNUNET_SCHEDULER_add_delayed(notify_successor_retry_time,
  4646. &send_notify_new_successor,
  4647. cls);
  4648. }
  4649. /*
  4650. * Core handle for p2p verify successor result messages.
  4651. * @param cls closure
  4652. * @param message message
  4653. * @param peer peer identity this notification is about
  4654. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  4655. */
  4656. static int
  4657. handle_dht_p2p_verify_successor_result(void *cls,
  4658. const struct GNUNET_PeerIdentity *peer,
  4659. const struct GNUNET_MessageHeader *message)
  4660. {
  4661. const struct PeerVerifySuccessorResultMessage *vsrm;
  4662. enum GDS_ROUTING_trail_direction trail_direction;
  4663. struct GNUNET_PeerIdentity querying_peer;
  4664. struct GNUNET_HashCode trail_id;
  4665. struct GNUNET_PeerIdentity *next_hop;
  4666. struct FriendInfo *target_friend;
  4667. struct GNUNET_PeerIdentity probable_successor;
  4668. struct GNUNET_PeerIdentity current_successor;
  4669. const struct GNUNET_PeerIdentity *trail;
  4670. unsigned int trail_length;
  4671. size_t msize;
  4672. msize = ntohs (message->size);
  4673. if (msize < sizeof (struct PeerVerifySuccessorResultMessage))
  4674. {
  4675. GNUNET_break_op (0);
  4676. return GNUNET_YES;
  4677. }
  4678. vsrm = (const struct PeerVerifySuccessorResultMessage *) message;
  4679. if ((msize - sizeof (struct PeerVerifySuccessorResultMessage)) %
  4680. sizeof (struct GNUNET_PeerIdentity) != 0)
  4681. {
  4682. GNUNET_break_op (0);
  4683. return GNUNET_OK;
  4684. }
  4685. trail_length = (msize - sizeof (struct PeerVerifySuccessorResultMessage))/
  4686. sizeof (struct GNUNET_PeerIdentity);
  4687. GNUNET_STATISTICS_update (GDS_stats,
  4688. gettext_noop
  4689. ("# Bytes received from other peers"), msize,
  4690. GNUNET_NO);
  4691. trail = (const struct GNUNET_PeerIdentity *) &vsrm[1];
  4692. querying_peer = vsrm->querying_peer;
  4693. trail_direction = ntohl (vsrm->trail_direction);
  4694. trail_id = vsrm->trail_id;
  4695. probable_successor = vsrm->probable_successor;
  4696. current_successor = vsrm->current_successor;
  4697. /* I am the querying_peer. */
  4698. if(0 == (GNUNET_CRYPTO_cmp_peer_identity (&querying_peer, &my_identity)))
  4699. {
  4700. /* Cancel Retry Task */
  4701. if (GNUNET_SCHEDULER_NO_TASK != send_verify_successor_retry_task)
  4702. {
  4703. struct VerifySuccessorContext *ctx;
  4704. ctx = GNUNET_SCHEDULER_cancel(send_verify_successor_retry_task);
  4705. GNUNET_free(ctx);
  4706. send_verify_successor_retry_task = GNUNET_SCHEDULER_NO_TASK;
  4707. }
  4708. compare_and_update_successor (current_successor,
  4709. probable_successor, trail, trail_length);
  4710. return GNUNET_OK;
  4711. }
  4712. /*If you are not the querying peer then pass on the message */
  4713. if(NULL == (next_hop =
  4714. GDS_ROUTING_get_next_hop (trail_id, trail_direction)))
  4715. {
  4716. /* Here it may happen that source peer has found a new successor, and removed
  4717. the trail, Hence no entry found in the routing table. Fail silently.*/
  4718. DEBUG(" NO ENTRY FOUND IN %s ROUTING TABLE for trail id %s, line",
  4719. GNUNET_i2s(&my_identity), GNUNET_h2s(&trail_id), __LINE__);
  4720. GNUNET_break_op(0);
  4721. return GNUNET_OK;
  4722. }
  4723. if (NULL == (target_friend =
  4724. GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop)))
  4725. {
  4726. GNUNET_break_op(0);
  4727. return GNUNET_OK;
  4728. }
  4729. GDS_NEIGHBOURS_send_verify_successor_result (querying_peer,
  4730. vsrm->current_successor,
  4731. probable_successor, trail_id,
  4732. trail,
  4733. trail_length,
  4734. trail_direction, target_friend);
  4735. return GNUNET_OK;
  4736. }
  4737. /*
  4738. * Core handle for p2p notify new successor messages.
  4739. * @param cls closure
  4740. * @param message message
  4741. * @param peer peer identity this notification is about
  4742. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  4743. */
  4744. static int
  4745. handle_dht_p2p_notify_new_successor(void *cls,
  4746. const struct GNUNET_PeerIdentity *peer,
  4747. const struct GNUNET_MessageHeader *message)
  4748. {
  4749. const struct PeerNotifyNewSuccessorMessage *nsm;
  4750. struct GNUNET_PeerIdentity *trail;
  4751. struct GNUNET_PeerIdentity source;
  4752. struct GNUNET_PeerIdentity new_successor;
  4753. struct GNUNET_HashCode trail_id;
  4754. struct GNUNET_PeerIdentity next_hop;
  4755. struct FriendInfo *target_friend;
  4756. int my_index;
  4757. size_t msize;
  4758. uint32_t trail_length;
  4759. msize = ntohs (message->size);
  4760. if (msize < sizeof (struct PeerNotifyNewSuccessorMessage))
  4761. {
  4762. GNUNET_break_op (0);
  4763. return GNUNET_YES;
  4764. }
  4765. nsm = (const struct PeerNotifyNewSuccessorMessage *) message;
  4766. if ((msize - sizeof (struct PeerNotifyNewSuccessorMessage)) %
  4767. sizeof (struct GNUNET_PeerIdentity) != 0)
  4768. {
  4769. GNUNET_break_op (0);
  4770. return GNUNET_OK;
  4771. }
  4772. trail_length = (msize - sizeof (struct PeerNotifyNewSuccessorMessage))/
  4773. sizeof (struct GNUNET_PeerIdentity);
  4774. GNUNET_STATISTICS_update (GDS_stats,
  4775. gettext_noop
  4776. ("# Bytes received from other peers"), msize,
  4777. GNUNET_NO);
  4778. trail = (struct GNUNET_PeerIdentity *) &nsm[1];
  4779. source = nsm->source_peer;
  4780. new_successor = nsm->new_successor;
  4781. trail_id = nsm->trail_id;
  4782. /* I am the new_successor to source_peer. */
  4783. if ( 0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &new_successor))
  4784. {
  4785. if(trail_length > 0)
  4786. GNUNET_assert(0 == GNUNET_CRYPTO_cmp_peer_identity(&trail[trail_length - 1],
  4787. peer));
  4788. else
  4789. GNUNET_assert(0 == GNUNET_CRYPTO_cmp_peer_identity(&source, peer));
  4790. compare_and_update_predecessor (source, trail, trail_length);
  4791. target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer);
  4792. GDS_NEIGHBOURS_send_notify_succcessor_confirmation (trail_id,
  4793. GDS_ROUTING_DEST_TO_SRC,
  4794. target_friend);
  4795. return GNUNET_OK;
  4796. }
  4797. GNUNET_assert(trail_length > 0);
  4798. /* I am part of trail to reach to successor. */
  4799. my_index = search_my_index (trail, trail_length);
  4800. if (-1 == my_index)
  4801. {
  4802. DEBUG ("No entry found in trail\n");
  4803. GNUNET_break_op (0);
  4804. return GNUNET_SYSERR;
  4805. }
  4806. if((trail_length + 1) == my_index)
  4807. {
  4808. DEBUG ("Found twice in trail.\n");
  4809. GNUNET_break_op (0);
  4810. return GNUNET_SYSERR;
  4811. }
  4812. if ((trail_length-1) == my_index)
  4813. next_hop = new_successor;
  4814. else
  4815. next_hop = trail[my_index + 1];
  4816. GDS_ROUTING_add(trail_id, *peer, next_hop);
  4817. target_friend =
  4818. GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_hop);
  4819. if (NULL == target_friend)
  4820. {
  4821. GNUNET_break(0);
  4822. return GNUNET_OK;
  4823. }
  4824. GDS_NEIGHBOURS_send_notify_new_successor (source, new_successor, trail,
  4825. trail_length,
  4826. trail_id, target_friend);
  4827. return GNUNET_OK;
  4828. }
  4829. /**
  4830. * Core handler for P2P notify successor message
  4831. * @param cls closure
  4832. * @param message message
  4833. * @param peer peer identity this notification is about
  4834. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  4835. */
  4836. static int
  4837. handle_dht_p2p_notify_succ_confirmation (void *cls,
  4838. const struct GNUNET_PeerIdentity *peer,
  4839. const struct GNUNET_MessageHeader *message)
  4840. {
  4841. const struct PeerNotifyConfirmationMessage *notify_confirmation;
  4842. enum GDS_ROUTING_trail_direction trail_direction;
  4843. struct GNUNET_HashCode trail_id;
  4844. struct FriendInfo *target_friend;
  4845. struct GNUNET_PeerIdentity *next_hop;
  4846. size_t msize;
  4847. msize = ntohs (message->size);
  4848. if (msize != sizeof (struct PeerNotifyConfirmationMessage))
  4849. {
  4850. GNUNET_break_op (0);
  4851. return GNUNET_OK;
  4852. }
  4853. GNUNET_STATISTICS_update (GDS_stats,
  4854. gettext_noop
  4855. ("# Bytes received from other peers"), msize,
  4856. GNUNET_NO);
  4857. notify_confirmation = (const struct PeerNotifyConfirmationMessage *) message;
  4858. trail_direction = ntohl (notify_confirmation->trail_direction);
  4859. trail_id = notify_confirmation->trail_id;
  4860. next_hop = GDS_ROUTING_get_next_hop (trail_id, trail_direction);
  4861. if (NULL == next_hop)
  4862. {
  4863. /* The source of notify new successor, might have found even a better
  4864. successor. In that case it send a trail teardown message, and hence,
  4865. the next hop is NULL. */
  4866. //Fixme: Add some print to confirm the above theory.
  4867. return GNUNET_OK;
  4868. }
  4869. /* I peer which sent the notify successor message to the successor. */
  4870. if (0 == GNUNET_CRYPTO_cmp_peer_identity (next_hop, &my_identity))
  4871. {
  4872. /*
  4873. * Schedule another round of verify sucessor with your current successor
  4874. * which may or may not be source of this message. This message is used
  4875. * only to ensure that we have a path setup to reach to our successor.
  4876. */
  4877. // TODO: cancel schedule of notify_successor_retry_task
  4878. if (send_notify_new_successor_retry_task != GNUNET_SCHEDULER_NO_TASK)
  4879. {
  4880. struct SendNotifyContext *notify_ctx;
  4881. notify_ctx = GNUNET_SCHEDULER_cancel(send_notify_new_successor_retry_task);
  4882. GNUNET_free (notify_ctx->successor_trail);
  4883. GNUNET_free (notify_ctx);
  4884. send_notify_new_successor_retry_task = GNUNET_SCHEDULER_NO_TASK;
  4885. }
  4886. if (send_verify_successor_task == GNUNET_SCHEDULER_NO_TASK)
  4887. {
  4888. verify_successor_next_send_time.rel_value_us =
  4889. DHT_SEND_VERIFY_SUCCESSOR_INTERVAL.rel_value_us +
  4890. GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
  4891. DHT_SEND_VERIFY_SUCCESSOR_INTERVAL.rel_value_us);
  4892. send_verify_successor_task =
  4893. GNUNET_SCHEDULER_add_delayed(verify_successor_next_send_time,
  4894. &send_verify_successor_message,
  4895. NULL);
  4896. }
  4897. }
  4898. else
  4899. {
  4900. target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop);
  4901. if (NULL == target_friend)
  4902. {
  4903. DEBUG ("\n friend not found, line number = %d",__LINE__);
  4904. return GNUNET_SYSERR;
  4905. }
  4906. GDS_NEIGHBOURS_send_notify_succcessor_confirmation (trail_id,
  4907. GDS_ROUTING_DEST_TO_SRC,
  4908. target_friend);
  4909. }
  4910. return GNUNET_OK;
  4911. }
  4912. /**
  4913. * Core handler for P2P trail rejection message
  4914. * @param cls closure
  4915. * @param message message
  4916. * @param peer peer identity this notification is about
  4917. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  4918. */
  4919. static int
  4920. handle_dht_p2p_trail_setup_rejection (void *cls,
  4921. const struct GNUNET_PeerIdentity *peer,
  4922. const struct GNUNET_MessageHeader *message)
  4923. {
  4924. const struct PeerTrailRejectionMessage *trail_rejection;
  4925. unsigned int trail_length;
  4926. const struct GNUNET_PeerIdentity *trail_peer_list;
  4927. struct FriendInfo *target_friend;
  4928. struct GNUNET_TIME_Relative congestion_timeout;
  4929. struct GNUNET_HashCode trail_id;
  4930. struct GNUNET_PeerIdentity next_peer;
  4931. struct GNUNET_PeerIdentity source;
  4932. uint64_t ultimate_destination_finger_value;
  4933. unsigned int is_predecessor;
  4934. size_t msize;
  4935. msize = ntohs (message->size);
  4936. if (msize < sizeof (struct PeerTrailRejectionMessage))
  4937. {
  4938. GNUNET_break_op (0);
  4939. return GNUNET_YES;
  4940. }
  4941. trail_rejection = (const struct PeerTrailRejectionMessage *) message;
  4942. if ((msize - sizeof (struct PeerTrailRejectionMessage)) %
  4943. sizeof (struct GNUNET_PeerIdentity) != 0)
  4944. {
  4945. GNUNET_break_op (0);
  4946. return GNUNET_OK;
  4947. }
  4948. trail_length = (msize - sizeof (struct PeerTrailRejectionMessage))/
  4949. sizeof (struct GNUNET_PeerIdentity);
  4950. GNUNET_STATISTICS_update (GDS_stats,
  4951. gettext_noop
  4952. ("# Bytes received from other peers"), msize,
  4953. GNUNET_NO);
  4954. trail_peer_list = (const struct GNUNET_PeerIdentity *)&trail_rejection[1];
  4955. is_predecessor = ntohl (trail_rejection->is_predecessor);
  4956. congestion_timeout = trail_rejection->congestion_time;
  4957. source = trail_rejection->source_peer;
  4958. trail_id = trail_rejection->trail_id;
  4959. ultimate_destination_finger_value =
  4960. GNUNET_ntohll (trail_rejection->ultimate_destination_finger_value);
  4961. /* First set the congestion time of the friend that sent you this message. */
  4962. target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer);
  4963. if (NULL == target_friend)
  4964. {
  4965. DEBUG ("\nLINE = %d ,No friend found.",__LINE__);
  4966. GNUNET_break(0);
  4967. return GNUNET_OK;
  4968. }
  4969. target_friend->congestion_timestamp =
  4970. GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(),
  4971. congestion_timeout);
  4972. /* I am the source peer which wants to setup the trail. Do nothing.
  4973. * send_find_finger_trail_task is scheduled periodically.*/
  4974. if(0 == (GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &source)))
  4975. return GNUNET_OK;
  4976. /* If I am congested then pass this message to peer before me in trail. */
  4977. if(GNUNET_YES == GDS_ROUTING_threshold_reached())
  4978. {
  4979. /* First remove yourself from the trail. */
  4980. unsigned int new_trail_length = trail_length - 1;
  4981. struct GNUNET_PeerIdentity trail[new_trail_length];
  4982. memcpy (trail, trail_peer_list, new_trail_length * sizeof(struct GNUNET_PeerIdentity));
  4983. if (0 == trail_length)
  4984. next_peer = source;
  4985. else
  4986. next_peer = trail[new_trail_length-1];
  4987. target_friend =
  4988. GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_peer);
  4989. if (NULL == target_friend)
  4990. {
  4991. DEBUG ("\nLINE = %d ,No friend found.",__LINE__);
  4992. GNUNET_break(0);
  4993. return GNUNET_OK;
  4994. }
  4995. GDS_NEIGHBOURS_send_trail_rejection (source,
  4996. ultimate_destination_finger_value,
  4997. my_identity, is_predecessor,
  4998. trail, new_trail_length, trail_id,
  4999. target_friend, CONGESTION_TIMEOUT);
  5000. return GNUNET_OK;
  5001. }
  5002. struct Closest_Peer successor;
  5003. successor = find_local_best_known_next_hop (ultimate_destination_finger_value, is_predecessor);
  5004. /* Am I the final destination? */
  5005. if (0 == (GNUNET_CRYPTO_cmp_peer_identity (&successor.best_known_destination,
  5006. &my_identity)))
  5007. {
  5008. /*Here you are already part of trail. Copy the trail removing yourself. */
  5009. unsigned int new_trail_length = trail_length - 1;
  5010. struct GNUNET_PeerIdentity trail[new_trail_length];
  5011. memcpy (trail, trail_peer_list, new_trail_length * sizeof(struct GNUNET_PeerIdentity));
  5012. if (0 == new_trail_length)
  5013. next_peer = source;
  5014. else
  5015. {
  5016. next_peer = trail[new_trail_length-1];
  5017. }
  5018. target_friend =
  5019. GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_peer);
  5020. if (NULL == target_friend)
  5021. {
  5022. DEBUG ("\nLINE = %d ,No friend found.",__LINE__);
  5023. GNUNET_break(0);
  5024. return GNUNET_OK;
  5025. }
  5026. GDS_NEIGHBOURS_send_trail_setup_result (source,
  5027. my_identity,
  5028. target_friend, new_trail_length,
  5029. trail,
  5030. is_predecessor,
  5031. ultimate_destination_finger_value,
  5032. trail_id);
  5033. }
  5034. else
  5035. {
  5036. /* Here I was already part of trail. So no need to add. */
  5037. target_friend =
  5038. GNUNET_CONTAINER_multipeermap_get (friend_peermap,
  5039. &successor.next_hop);
  5040. if (NULL == target_friend)
  5041. {
  5042. DEBUG ("\nLINE = %d ,No friend found.",__LINE__);
  5043. GNUNET_break(0);
  5044. return GNUNET_OK;
  5045. }
  5046. GDS_NEIGHBOURS_send_trail_setup (source,
  5047. ultimate_destination_finger_value,
  5048. successor.best_known_destination,
  5049. target_friend, trail_length, trail_peer_list,
  5050. is_predecessor, trail_id,
  5051. successor.trail_id);
  5052. }
  5053. return GNUNET_OK;
  5054. }
  5055. /**
  5056. * Core handler for trail teardown message.
  5057. * @param cls closure
  5058. * @param message message
  5059. * @param peer sender of this messsage.
  5060. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  5061. */
  5062. static int
  5063. handle_dht_p2p_trail_teardown (void *cls, const struct GNUNET_PeerIdentity *peer,
  5064. const struct GNUNET_MessageHeader *message)
  5065. {
  5066. const struct PeerTrailTearDownMessage *trail_teardown;
  5067. enum GDS_ROUTING_trail_direction trail_direction;
  5068. struct GNUNET_HashCode trail_id;
  5069. struct GNUNET_PeerIdentity *next_hop;
  5070. size_t msize;
  5071. msize = ntohs (message->size);
  5072. /* Here we pass only the trail id. */
  5073. if (msize != sizeof (struct PeerTrailTearDownMessage))
  5074. {
  5075. GNUNET_break_op (0);
  5076. return GNUNET_OK;
  5077. }
  5078. GNUNET_STATISTICS_update (GDS_stats,
  5079. gettext_noop
  5080. ("# Bytes received from other peers"), msize,
  5081. GNUNET_NO);
  5082. trail_teardown = (const struct PeerTrailTearDownMessage *) message;
  5083. trail_direction = ntohl (trail_teardown->trail_direction);
  5084. trail_id = trail_teardown->trail_id;
  5085. /* Check if peer is the real peer from which we should get this message.*/
  5086. /* Get the prev_hop for this trail by getting the next hop in opposite direction. */
  5087. #if 0
  5088. GNUNET_assert (NULL != (prev_hop =
  5089. GDS_ROUTING_get_next_hop (trail_id, !trail_direction)));
  5090. if (0 != GNUNET_CRYPTO_cmp_peer_identity (prev_hop, peer))
  5091. {
  5092. GNUNET_break (0);
  5093. return GNUNET_SYSERR;
  5094. }
  5095. #endif
  5096. next_hop = GDS_ROUTING_get_next_hop (trail_id, trail_direction);
  5097. if (NULL == next_hop)
  5098. {
  5099. DEBUG(" NO ENTRY FOUND IN %s ROUTING TABLE for trail id %s, line",
  5100. GNUNET_i2s(&my_identity), GNUNET_h2s(&trail_id), __LINE__);
  5101. GNUNET_break (0);
  5102. return GNUNET_SYSERR;
  5103. }
  5104. /* I am the next hop, which means I am the final destination. */
  5105. if (0 == GNUNET_CRYPTO_cmp_peer_identity (next_hop, &my_identity))
  5106. {
  5107. GNUNET_assert (GNUNET_YES == GDS_ROUTING_remove_trail (trail_id));
  5108. return GNUNET_OK;
  5109. }
  5110. else
  5111. {
  5112. /* If not final destination, then send a trail teardown message to next hop.*/
  5113. GNUNET_assert (NULL != GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop));
  5114. GNUNET_assert (GNUNET_YES == GDS_ROUTING_remove_trail (trail_id));
  5115. GDS_NEIGHBOURS_send_trail_teardown (&trail_id, trail_direction, next_hop);
  5116. }
  5117. return GNUNET_OK;
  5118. }
  5119. /**
  5120. * Core handle for p2p add trail message.
  5121. * @param cls closure
  5122. * @param message message
  5123. * @param peer peer identity this notification is about
  5124. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  5125. */
  5126. static int
  5127. handle_dht_p2p_add_trail (void *cls, const struct GNUNET_PeerIdentity *peer,
  5128. const struct GNUNET_MessageHeader *message)
  5129. {
  5130. const struct PeerAddTrailMessage *add_trail;
  5131. const struct GNUNET_PeerIdentity *trail;
  5132. struct GNUNET_HashCode trail_id;
  5133. struct GNUNET_PeerIdentity destination_peer;
  5134. struct GNUNET_PeerIdentity source_peer;
  5135. struct GNUNET_PeerIdentity next_hop;
  5136. unsigned int trail_length;
  5137. unsigned int my_index;
  5138. size_t msize;
  5139. msize = ntohs (message->size);
  5140. /* In this message we pass the whole trail from source to destination as we
  5141. * are adding that trail.*/
  5142. //FIXME: failed when run with 1000 pears. check why.
  5143. if (msize < sizeof (struct PeerAddTrailMessage))
  5144. {
  5145. GNUNET_break_op (0);
  5146. return GNUNET_OK;
  5147. }
  5148. add_trail = (const struct PeerAddTrailMessage *) message;
  5149. trail_length = (msize - sizeof (struct PeerAddTrailMessage))/
  5150. sizeof (struct GNUNET_PeerIdentity);
  5151. if ((msize - sizeof (struct PeerAddTrailMessage)) %
  5152. sizeof (struct GNUNET_PeerIdentity) != 0)
  5153. {
  5154. GNUNET_break_op (0);
  5155. return GNUNET_OK;
  5156. }
  5157. GNUNET_STATISTICS_update (GDS_stats,
  5158. gettext_noop
  5159. ("# Bytes received from other peers"), msize,
  5160. GNUNET_NO);
  5161. trail = (const struct GNUNET_PeerIdentity *)&add_trail[1];
  5162. destination_peer = add_trail->destination_peer;
  5163. source_peer = add_trail->source_peer;
  5164. trail_id = add_trail->trail_id;
  5165. /* I am not the destination of the trail. */
  5166. if (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &destination_peer))
  5167. {
  5168. struct FriendInfo *target_friend;
  5169. /* Get my location in the trail. */
  5170. my_index = search_my_index (trail, trail_length);
  5171. if (-1 == my_index)
  5172. {
  5173. GNUNET_break_op (0);
  5174. return GNUNET_SYSERR;
  5175. }
  5176. if((trail_length + 1) == my_index)
  5177. {
  5178. DEBUG ("Found twice in trail.\n");
  5179. GNUNET_break_op (0);
  5180. return GNUNET_SYSERR;
  5181. }
  5182. if ((trail_length - 1) == my_index)
  5183. {
  5184. next_hop = destination_peer;
  5185. }
  5186. else
  5187. {
  5188. next_hop = trail[my_index + 1];
  5189. }
  5190. /* Add in your routing table. */
  5191. GNUNET_assert (GNUNET_OK == GDS_ROUTING_add (trail_id, *peer, next_hop));
  5192. //GNUNET_assert (GNUNET_OK == GDS_ROUTING_add (trail_id, next_hop, *peer));
  5193. GNUNET_assert (NULL !=
  5194. (target_friend =
  5195. GNUNET_CONTAINER_multipeermap_get (friend_peermap, &next_hop)));
  5196. GDS_NEIGHBOURS_send_add_trail (source_peer, destination_peer, trail_id,
  5197. trail, trail_length, target_friend);
  5198. return GNUNET_OK;
  5199. }
  5200. /* I am the destination. Add an entry in routing table. */
  5201. GNUNET_assert (GNUNET_OK == GDS_ROUTING_add (trail_id, *peer, my_identity));
  5202. return GNUNET_OK;
  5203. }
  5204. /**
  5205. * Free the finger trail in which the first friend to reach to a finger is
  5206. * disconnected_friend. Also remove entry from routing table for that particular
  5207. * trail id.
  5208. * @param disconnected_friend PeerIdentity of friend which got disconnected
  5209. * @param remove_finger Finger whose trail we need to check if it has
  5210. * disconnected_friend as the first hop.
  5211. * @return Total number of trails in which disconnected_friend was the first
  5212. * hop.
  5213. */
  5214. static int
  5215. remove_matching_trails (const struct GNUNET_PeerIdentity *disconnected_friend,
  5216. struct FingerInfo *finger)
  5217. {
  5218. struct GNUNET_PeerIdentity *next_hop;
  5219. struct FriendInfo *remove_friend;
  5220. struct Trail *current_trail;
  5221. unsigned int matching_trails_count = 0;
  5222. int i;
  5223. /* Iterate over all the trails of finger. */
  5224. for (i = 0; i < finger->trails_count; i++)
  5225. {
  5226. current_trail = &finger->trail_list[i];
  5227. if (GNUNET_NO == current_trail->is_present)
  5228. continue;
  5229. /* First friend to reach to finger is disconnected_peer. */
  5230. if (0 == GNUNET_CRYPTO_cmp_peer_identity (&current_trail->trail_head->peer,
  5231. disconnected_friend))
  5232. {
  5233. remove_friend =
  5234. GNUNET_CONTAINER_multipeermap_get (friend_peermap,
  5235. disconnected_friend);
  5236. GNUNET_assert (NULL != remove_friend);
  5237. next_hop = GDS_ROUTING_get_next_hop (current_trail->trail_id,
  5238. GDS_ROUTING_SRC_TO_DEST);
  5239. /* Here it may happen that as all the peers got disconnected, the entry in
  5240. routing table for that particular trail has been removed, because the
  5241. previously disconnected peer was either a next hop or prev hop of that
  5242. peer. */
  5243. if (NULL != next_hop)
  5244. {
  5245. GNUNET_assert (0 == (GNUNET_CRYPTO_cmp_peer_identity (disconnected_friend,
  5246. next_hop)));
  5247. GNUNET_assert (GNUNET_YES == GDS_ROUTING_remove_trail (current_trail->trail_id));
  5248. }
  5249. matching_trails_count++;
  5250. free_trail (current_trail);
  5251. current_trail->is_present = GNUNET_NO;
  5252. }
  5253. }
  5254. return matching_trails_count;
  5255. }
  5256. /**
  5257. * Iterate over finger_table entries.
  5258. * 0. Ignore finger which is my_identity or if no valid entry present at
  5259. * that finger index.
  5260. * 1. If disconnected_friend is a finger, then remove the routing entry from
  5261. your own table. Free the trail.
  5262. * 2. Check if disconnected_friend is the first friend in the trail to reach to a finger.
  5263. * 2.1 Remove all the trails and entry from routing table in which disconnected
  5264. * friend is the first friend in the trail. If disconnected_friend is the
  5265. * first friend in all the trails to reach finger, then remove the finger.
  5266. * @param disconnected_friend Peer identity of friend which got disconnected.
  5267. */
  5268. static void
  5269. remove_matching_fingers (const struct GNUNET_PeerIdentity *disconnected_peer)
  5270. {
  5271. struct FingerInfo *current_finger;
  5272. int removed_trails_count;
  5273. int i;
  5274. /* Iterate over finger table entries. */
  5275. for (i = 0; i < MAX_FINGERS; i++)
  5276. {
  5277. current_finger = &finger_table[i];
  5278. /* No finger stored at this trail index or I am the finger. */
  5279. if ((GNUNET_NO == current_finger->is_present) ||
  5280. (0 == GNUNET_CRYPTO_cmp_peer_identity (&current_finger->finger_identity,
  5281. &my_identity)))
  5282. continue;
  5283. /* Is disconnected_peer a finger? */
  5284. if (0 == GNUNET_CRYPTO_cmp_peer_identity (disconnected_peer,
  5285. &current_finger->finger_identity))
  5286. {
  5287. remove_existing_finger (current_finger, i);
  5288. }
  5289. /* If finger is a friend but not disconnected_friend, then continue. */
  5290. if (NULL != GNUNET_CONTAINER_multipeermap_get (friend_peermap,
  5291. &current_finger->finger_identity))
  5292. continue;
  5293. /* Iterate over the list of trails to reach remove_finger. Check if
  5294. * disconnected_friend is the first friend in any of the trail. */
  5295. removed_trails_count = remove_matching_trails (disconnected_peer,
  5296. current_finger);
  5297. current_finger->trails_count =
  5298. current_finger->trails_count - removed_trails_count;
  5299. if (0 == current_finger->trails_count)
  5300. {
  5301. current_finger->is_present = GNUNET_NO;
  5302. memset ((void *)&finger_table[i], 0, sizeof (finger_table[i]));
  5303. }
  5304. }
  5305. }
  5306. /**
  5307. * Method called whenever a peer disconnects.
  5308. *
  5309. * @param cls closure
  5310. * @param peer peer identity this notification is about
  5311. */
  5312. static void
  5313. handle_core_disconnect (void *cls,
  5314. const struct GNUNET_PeerIdentity *peer)
  5315. {
  5316. struct FriendInfo *remove_friend;
  5317. struct P2PPendingMessage *pos;
  5318. unsigned int discarded;
  5319. /* If disconnected to own identity, then return. */
  5320. if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
  5321. return;
  5322. if(NULL == (remove_friend =
  5323. GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer)))
  5324. {
  5325. DEBUG("\n friend already disconnected.");
  5326. return;
  5327. }
  5328. remove_matching_fingers (peer);
  5329. GNUNET_assert (GNUNET_SYSERR != GDS_ROUTING_remove_trail_by_peer (peer));
  5330. GNUNET_assert (GNUNET_YES ==
  5331. GNUNET_CONTAINER_multipeermap_remove (friend_peermap,
  5332. peer,
  5333. remove_friend));
  5334. /* Remove all the messages queued in pending list of this peer is discarded.*/
  5335. if (remove_friend->th != NULL)
  5336. {
  5337. GNUNET_CORE_notify_transmit_ready_cancel(remove_friend->th);
  5338. remove_friend->th = NULL;
  5339. }
  5340. discarded = 0;
  5341. while (NULL != (pos = remove_friend->head))
  5342. {
  5343. GNUNET_CONTAINER_DLL_remove (remove_friend->head, remove_friend->tail, pos);
  5344. discarded++;
  5345. GNUNET_free (pos);
  5346. }
  5347. GNUNET_STATISTICS_update (GDS_stats,
  5348. gettext_noop
  5349. ("# Queued messages discarded (peer disconnected)"),
  5350. discarded, GNUNET_NO);
  5351. //GNUNET_free (remove_friend);
  5352. if (0 != GNUNET_CONTAINER_multipeermap_size (friend_peermap))
  5353. return;
  5354. if (GNUNET_SCHEDULER_NO_TASK != find_finger_trail_task)
  5355. {
  5356. GNUNET_SCHEDULER_cancel (find_finger_trail_task);
  5357. find_finger_trail_task = GNUNET_SCHEDULER_NO_TASK;
  5358. }
  5359. else
  5360. GNUNET_break (0);
  5361. }
  5362. /**
  5363. * Method called whenever a peer connects.
  5364. *
  5365. * @param cls closure
  5366. * @param peer_identity peer identity this notification is about
  5367. */
  5368. static void
  5369. handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer_identity)
  5370. {
  5371. struct FriendInfo *friend;
  5372. /* Check for connect to self message */
  5373. if (0 == memcmp (&my_identity, peer_identity, sizeof (struct GNUNET_PeerIdentity)))
  5374. return;
  5375. /* If peer already exists in our friend_peermap, then exit. */
  5376. if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (friend_peermap,
  5377. peer_identity))
  5378. {
  5379. GNUNET_break (0);
  5380. return;
  5381. }
  5382. friend = GNUNET_new (struct FriendInfo);
  5383. friend->id = *peer_identity;
  5384. GNUNET_assert (GNUNET_OK ==
  5385. GNUNET_CONTAINER_multipeermap_put (friend_peermap,
  5386. peer_identity, friend,
  5387. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
  5388. /* FIXME: now we are not making a distinction between fingers which are friends
  5389. * also.But later, we should add a congestion timestamp on the friend, so that it is
  5390. * selected after some time out. This is to ensure that both peers have added
  5391. * each other as their friend. */
  5392. /* Got a first connection, good time to start with FIND FINGER TRAIL requests...*/
  5393. if (GNUNET_SCHEDULER_NO_TASK == find_finger_trail_task)
  5394. {
  5395. find_finger_trail_task = GNUNET_SCHEDULER_add_now (&send_find_finger_trail_message, NULL);
  5396. }
  5397. }
  5398. /**
  5399. * To be called on core init/fail.
  5400. *
  5401. * @param cls service closure
  5402. * @param identity the public identity of this peer
  5403. */
  5404. static void
  5405. core_init (void *cls,
  5406. const struct GNUNET_PeerIdentity *identity)
  5407. {
  5408. my_identity = *identity;
  5409. }
  5410. /**
  5411. * Initialize finger table entries.
  5412. */
  5413. static void
  5414. finger_table_init ()
  5415. {
  5416. memset (&finger_table, 0, sizeof (finger_table));
  5417. }
  5418. /**
  5419. * Initialize neighbours subsystem.
  5420. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  5421. */
  5422. int
  5423. GDS_NEIGHBOURS_init (void)
  5424. {
  5425. static struct GNUNET_CORE_MessageHandler core_handlers[] = {
  5426. {&handle_dht_p2p_put, GNUNET_MESSAGE_TYPE_XDHT_P2P_PUT, 0},
  5427. {&handle_dht_p2p_get, GNUNET_MESSAGE_TYPE_XDHT_P2P_GET, 0},
  5428. {&handle_dht_p2p_get_result, GNUNET_MESSAGE_TYPE_XDHT_P2P_GET_RESULT, 0},
  5429. {&handle_dht_p2p_trail_setup, GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_SETUP, 0},
  5430. {&handle_dht_p2p_trail_setup_result, GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_SETUP_RESULT, 0},
  5431. {&handle_dht_p2p_verify_successor, GNUNET_MESSAGE_TYPE_XDHT_P2P_VERIFY_SUCCESSOR, 0},
  5432. {&handle_dht_p2p_verify_successor_result, GNUNET_MESSAGE_TYPE_XDHT_P2P_VERIFY_SUCCESSOR_RESULT, 0},
  5433. {&handle_dht_p2p_notify_new_successor, GNUNET_MESSAGE_TYPE_XDHT_P2P_NOTIFY_NEW_SUCCESSOR, 0},
  5434. {&handle_dht_p2p_trail_setup_rejection, GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_SETUP_REJECTION, 0},
  5435. {&handle_dht_p2p_trail_teardown, GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_TEARDOWN,
  5436. sizeof (struct PeerTrailTearDownMessage)},
  5437. {&handle_dht_p2p_add_trail, GNUNET_MESSAGE_TYPE_XDHT_P2P_ADD_TRAIL, 0},
  5438. {&handle_dht_p2p_notify_succ_confirmation, GNUNET_MESSAGE_TYPE_XDHT_P2P_NOTIFY_SUCCESSOR_CONFIRMATION,
  5439. sizeof (struct PeerNotifyConfirmationMessage)},
  5440. {NULL, 0, 0}
  5441. };
  5442. #if ENABLE_MALICIOUS
  5443. act_malicious = 0;
  5444. #endif
  5445. core_api =
  5446. GNUNET_CORE_connect (GDS_cfg, NULL, &core_init, &handle_core_connect,
  5447. &handle_core_disconnect, NULL, GNUNET_NO, NULL,
  5448. GNUNET_NO, core_handlers);
  5449. if (NULL == core_api)
  5450. return GNUNET_SYSERR;
  5451. //TODO: check size of this peer map?
  5452. friend_peermap = GNUNET_CONTAINER_multipeermap_create (256, GNUNET_NO);
  5453. finger_table_init ();
  5454. successor_times = 10;
  5455. fingers_round_count = 5;
  5456. find_finger_trail_task_next_send_time.rel_value_us =
  5457. DHT_FIND_FINGER_TRAIL_INTERVAL.rel_value_us +
  5458. GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
  5459. DHT_FIND_FINGER_TRAIL_INTERVAL.rel_value_us);
  5460. verify_successor_next_send_time.rel_value_us =
  5461. DHT_SEND_VERIFY_SUCCESSOR_INTERVAL.rel_value_us +
  5462. GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
  5463. DHT_SEND_VERIFY_SUCCESSOR_INTERVAL.rel_value_us);
  5464. verify_successor_retry_time.rel_value_us =
  5465. DHT_SEND_VERIFY_SUCCESSOR_RETRY_INTERVAL.rel_value_us +
  5466. GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
  5467. DHT_SEND_VERIFY_SUCCESSOR_RETRY_INTERVAL.rel_value_us);
  5468. notify_successor_retry_time.rel_value_us =
  5469. DHT_SEND_NOTIFY_SUCCESSOR_RETRY_INTERVAL.rel_value_us +
  5470. GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
  5471. DHT_SEND_NOTIFY_SUCCESSOR_RETRY_INTERVAL.rel_value_us);
  5472. return GNUNET_OK;
  5473. }
  5474. /**
  5475. * Free the memory held up by trails of a finger.
  5476. */
  5477. static void
  5478. delete_finger_table_entries()
  5479. {
  5480. unsigned int i;
  5481. unsigned int j;
  5482. for(i = 0; i < MAX_FINGERS; i++)
  5483. {
  5484. if(GNUNET_YES == finger_table[i].is_present)
  5485. {
  5486. for(j = 0; j < finger_table[i].trails_count; j++)
  5487. free_trail(&finger_table[i].trail_list[j]);
  5488. }
  5489. }
  5490. }
  5491. /**
  5492. * Shutdown neighbours subsystem.
  5493. */
  5494. void
  5495. GDS_NEIGHBOURS_done (void)
  5496. {
  5497. if (NULL == core_api)
  5498. return;
  5499. GNUNET_CORE_disconnect (core_api);
  5500. core_api = NULL;
  5501. delete_finger_table_entries();
  5502. GNUNET_assert (0 == GNUNET_CONTAINER_multipeermap_size (friend_peermap));
  5503. GNUNET_CONTAINER_multipeermap_destroy (friend_peermap);
  5504. friend_peermap = NULL;
  5505. if (GNUNET_SCHEDULER_NO_TASK != find_finger_trail_task)
  5506. {
  5507. GNUNET_SCHEDULER_cancel (find_finger_trail_task);
  5508. find_finger_trail_task = GNUNET_SCHEDULER_NO_TASK;
  5509. }
  5510. if (GNUNET_SCHEDULER_NO_TASK != send_verify_successor_task)
  5511. {
  5512. GNUNET_SCHEDULER_cancel (send_verify_successor_task);
  5513. send_verify_successor_task = GNUNET_SCHEDULER_NO_TASK;
  5514. }
  5515. if (GNUNET_SCHEDULER_NO_TASK != send_verify_successor_retry_task)
  5516. {
  5517. struct VerifySuccessorContext *ctx;
  5518. ctx = GNUNET_SCHEDULER_cancel (send_verify_successor_retry_task);
  5519. GNUNET_free(ctx);
  5520. send_verify_successor_retry_task = GNUNET_SCHEDULER_NO_TASK;
  5521. }
  5522. if (send_notify_new_successor_retry_task != GNUNET_SCHEDULER_NO_TASK)
  5523. {
  5524. struct SendNotifyContext *notify_ctx;
  5525. notify_ctx = GNUNET_SCHEDULER_cancel(send_notify_new_successor_retry_task);
  5526. GNUNET_free (notify_ctx->successor_trail);
  5527. GNUNET_free (notify_ctx);
  5528. send_notify_new_successor_retry_task = GNUNET_SCHEDULER_NO_TASK;
  5529. }
  5530. }
  5531. /**
  5532. * Get my identity
  5533. *
  5534. * @return my identity
  5535. */
  5536. struct GNUNET_PeerIdentity
  5537. GDS_NEIGHBOURS_get_my_id (void)
  5538. {
  5539. return my_identity;
  5540. }
  5541. /* end of gnunet-service-xdht_neighbours.c */