You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tracer.c 42KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544
  1. /* This file is part of Netsukuku
  2. * (c) Copyright 2005 Andrea Lo Pumo aka AlpT <alpt@freaknet.org>
  3. *
  4. * This source code is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as published
  6. * by the Free Software Foundation; either version 2 of the License,
  7. * or (at your option) any later version.
  8. *
  9. * This source code is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. * Please refer to the GNU Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Public License along with
  15. * this source code; if not, write to:
  16. * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include "includes.h"
  19. #include "common.h"
  20. #include "request.h"
  21. #include "pkts.h"
  22. #include "bmap.h"
  23. #include "radar.h"
  24. #include "route.h"
  25. #include "radar.h"
  26. #include "rehook.h"
  27. #include "tracer.h"
  28. #include "qspn.h"
  29. #include "igs.h"
  30. #include "netsukuku.h"
  31. char *tracer_pack_pkt(brdcast_hdr *bcast_hdr, tracer_hdr *trcr_hdr, tracer_chunk *tracer,
  32. char *bblocks, size_t bblocks_sz, int new_bblocks);
  33. /*
  34. * ip_to_rfrom: If `rip_quadg' is null, it converts the `rip' ip in a
  35. * quadro_group that is stored in `new_quadg' (if it is not null), otherwise it
  36. * uses `rip_quadg' itself.
  37. * The flags passed to iptoquadg are orred whith `quadg_flags'.
  38. * The rnode position of the root_node of level 0 which corresponds to
  39. * the given ip is returned, if it isn't found -1 is returned.
  40. */
  41. int ip_to_rfrom(inet_prefix rip, quadro_group *rip_quadg,
  42. quadro_group *new_quadg, char quadg_flags)
  43. {
  44. quadro_group qdg, *quadg;
  45. map_node *from;
  46. ext_rnode_cache *erc;
  47. int ret, external_node=0;
  48. quadg=&qdg;
  49. if(rip_quadg) {
  50. quadg=rip_quadg;
  51. } else {
  52. quadg_flags|=QUADG_GID|QUADG_GNODE;
  53. iptoquadg(rip, me.ext_map, quadg, quadg_flags);
  54. if(new_quadg)
  55. memcpy(new_quadg, quadg, sizeof(quadro_group));
  56. }
  57. if(quadg_gids_cmp(me.cur_quadg, *quadg, 1))
  58. external_node=1;
  59. if(!external_node) {
  60. iptomap((u_int)me.int_map, rip, me.cur_quadg.ipstart[1], &from);
  61. ret=rnode_find(me.cur_node, from);
  62. } else {
  63. erc=e_rnode_find(me.cur_erc, quadg, 0);
  64. ret = !erc ? -1 : erc->rnode_pos;
  65. }
  66. return ret;
  67. }
  68. /*
  69. * tracer_verify_pkt: It checks the validity of `tracer': The last entry
  70. * in the tracer must be a node present in our r_nodes.
  71. * Instead of using iptoquadg it uses `rip_quadg' if it isn't null.
  72. */
  73. int tracer_verify_pkt(tracer_chunk *tracer, u_short hops, inet_prefix rip,
  74. quadro_group *rip_quadg, int level)
  75. {
  76. quadro_group qdg, *quadg;
  77. map_node *from, *real_from, *real_gfrom;
  78. int retries=0, ret;
  79. from=real_from=real_gfrom=0;
  80. quadg=&qdg;
  81. if(!rip_quadg)
  82. iptoquadg(rip, me.ext_map, quadg, QUADG_GID|QUADG_GNODE);
  83. else
  84. quadg=rip_quadg;
  85. if(!quadg_gids_cmp(*quadg, me.cur_quadg, level))
  86. return 0;
  87. /*
  88. * Now, let's check if we are part of the bcast_hdr->g_node of
  89. * bcast_hdr->level. If not let's drop it! Why the hell this pkt is
  90. * here?
  91. */
  92. if(quadg_gids_cmp(*quadg, me.cur_quadg, level+1)) {
  93. debug(DBG_INSANE, "%s:%d", ERROR_POS);
  94. return -1;
  95. }
  96. /*
  97. * `from' has to be absolutely one of our rnodes
  98. */
  99. if(!level) {
  100. iptomap((u_int)me.int_map, rip, me.cur_quadg.ipstart[1], &real_from);
  101. from = node_from_pos(tracer[hops-1].node, me.int_map);
  102. } else {
  103. real_gfrom = &quadg->gnode[_EL(level)]->g;
  104. from = node_from_pos(quadg->gid[0], me.int_map);
  105. }
  106. /* Look for the `from' node in the int_map. */
  107. if((real_from && real_from == from) || from) {
  108. /* Is `from' in our rnodes? */
  109. for(retries=0;
  110. (ret=rnode_find(me.cur_node, from)) == -1 && !retries;
  111. retries++)
  112. radar_wait_new_scan();
  113. if(ret != -1)
  114. return 0;
  115. }
  116. /* `from' is a gnode, look in the ext_map */
  117. if(level) {
  118. /* Look in ext_map */
  119. from=(map_node *)gnode_from_pos(tracer[hops-1].node,
  120. me.ext_map[_EL(level)]);
  121. if(!from || (real_gfrom && real_gfrom != from)) {
  122. debug(DBG_INSANE, "%s:%d", ERROR_POS);
  123. return -1;
  124. }
  125. ret=g_rnode_find(me.cur_quadg.gnode[_EL(level)], (map_gnode *)from);
  126. if(ret == -1) {
  127. debug(DBG_INSANE, "%s:%d gnode: %d, level: %d",
  128. ERROR_POS, tracer[hops-1].node, level);
  129. return -1;
  130. }
  131. }
  132. return 0;
  133. }
  134. /*
  135. * tracer_add_entry: Append our entry `node' to the tracer pkt `tracer' wich has
  136. * `hops'. It returns the modified tracer pkt in a newly mallocated struct and
  137. * it increments the `*hops'.
  138. * If `tracer' is null it will return the new tracer_pkt.
  139. * On errors it returns NULL.
  140. */
  141. tracer_chunk *
  142. tracer_add_entry(void *void_map, void *void_node, tracer_chunk *tracer,
  143. u_int *hops, u_char level)
  144. {
  145. tracer_chunk *t;
  146. map_node *from;
  147. map_rnode *rfrom=0;
  148. map_node *map, *node;
  149. map_gnode **ext_map, *gnode;
  150. int pos, new_entry_pos, last_entry_node, nhops;
  151. map=(map_node *)void_map;
  152. node=(map_node *)void_node;
  153. ext_map=(map_gnode **)void_map;
  154. gnode=(map_gnode *)void_node;
  155. (*hops)++;
  156. nhops=*hops;
  157. new_entry_pos=nhops-1;
  158. t=xzalloc(sizeof(tracer_chunk) * nhops);
  159. if(tracer || nhops > 1) {
  160. /*
  161. * In the tracer_pkt there are already some chunks, we copy
  162. * them in the new pkt.
  163. */
  164. memcpy(t, tracer, sizeof(tracer_chunk) * (nhops-1));
  165. /*
  166. * We add, in the new entry, the rtt there is from me to the
  167. * node of the the last entry of the old tracer pkt.
  168. */
  169. last_entry_node=tracer[nhops-2].node;
  170. if(!level) {
  171. from=node_from_pos(last_entry_node, map);
  172. /* check if `from' is in our rnodes */
  173. if((pos=rnode_find(me.cur_node, from)) == -1) {
  174. debug(DBG_INSANE, "%s:%d lvl: %d last_entry_node: %d",
  175. ERROR_POS, level, last_entry_node);
  176. return 0;
  177. }
  178. rfrom=&me.cur_node->r_node[pos];
  179. } else {
  180. from=(map_node*)gnode_from_pos(last_entry_node,
  181. ext_map[_EL(level)]);
  182. /* check if `from' is in our rnodes */
  183. if((pos=g_rnode_find(me.cur_quadg.gnode[_EL(level)],
  184. (map_gnode *)from) == -1)) {
  185. debug(DBG_INSANE, "%s:%d lvl: %d last_entry_node: %d",
  186. ERROR_POS, level, last_entry_node);
  187. return 0;
  188. }
  189. rfrom=&me.cur_quadg.gnode[_EL(level)]->g.r_node[pos];
  190. }
  191. t[new_entry_pos].rtt=rfrom->trtt;
  192. }
  193. /* Fill the new entry in the tracer_pkt */
  194. if(!level) {
  195. t[new_entry_pos].gcount = 1;
  196. t[new_entry_pos].node=pos_from_node(node, map);
  197. } else {
  198. t[new_entry_pos].gcount = qspn_gnode_count[_EL(level)];
  199. t[new_entry_pos].node=pos_from_gnode(gnode, ext_map[_EL(level)]);
  200. }
  201. return t;
  202. }
  203. /*
  204. * tracer_add_rtt: Increments the rtt of the `hop'th `tracer' chunk by adding
  205. * the rtt of the rnode who is in the `rpos' postion in me.cur_node->r_node.
  206. * It returns the new rtt value on success.
  207. */
  208. int tracer_add_rtt(int rpos, tracer_chunk *tracer, u_short hop)
  209. {
  210. tracer[hop].rtt+=me.cur_node->r_node[rpos].trtt;
  211. return tracer[hop].rtt;
  212. }
  213. /*
  214. * tracer_get_trtt: It stores in `trtt' the total round trip time needed to
  215. * reach the `tracer[0].node' from the me.cur_node.
  216. * me.cur_node->r_node[`from_rnode_pos'] is the rnode who forwarded us the pkt.
  217. * If it succeeds 0 is returned.
  218. */
  219. int tracer_get_trtt(int from_rnode_pos, tracer_hdr *trcr_hdr,
  220. tracer_chunk *tracer, u_int *trtt)
  221. {
  222. int hops, i;
  223. u_int trtt_ms=0;
  224. *trtt=0;
  225. hops = trcr_hdr->hops;
  226. if(!hops)
  227. return -1;
  228. /* Add the rtt of me -> from */
  229. trtt_ms+=me.cur_node->r_node[from_rnode_pos].trtt;
  230. for(i=hops-1; i >= 0; i--)
  231. trtt_ms+=tracer[i].rtt;
  232. *trtt=trtt_ms;
  233. return 0;
  234. }
  235. /*
  236. * tracer_update_gcount: it updates `gcount_counter' by adding the sum of
  237. * gcounts present in `tracer'.
  238. * It then updates the map_gnode.gcount counter of the gnodes present in the
  239. * `ext_map' of the `level'th level.
  240. * It ignores all the tracer chunks < `first_hop'.
  241. */
  242. void tracer_update_gcount(tracer_hdr *trcr_hdr, tracer_chunk *tracer,
  243. int first_hop, u_int *gcount_counter,
  244. map_node *int_map, map_gnode **ext_map, int level)
  245. {
  246. map_node *node=0;
  247. map_gnode *gnode;
  248. u_int hops;
  249. int i;
  250. hops = trcr_hdr->hops;
  251. if(!hops || first_hop >= hops || first_hop < 0)
  252. return;
  253. for(i=first_hop; i>=0; i--) {
  254. if(level) {
  255. gnode=gnode_from_pos(tracer[i].node, ext_map[_EL(level)]);
  256. qspn_dec_gcount(gcount_counter, level+1, gnode->gcount);
  257. gnode->gcount=tracer[i].gcount;
  258. } else
  259. node = node_from_pos(tracer[i].node, int_map);
  260. if(level || (!level && node->flags & MAP_VOID))
  261. qspn_inc_gcount(gcount_counter, level+1, tracer[i].gcount);
  262. }
  263. }
  264. /*
  265. * tracer_build_bentry: It builds the bnode_block to be added in the bnode's
  266. * entry in the tracer pkt. It stores in `bnodechunk' the pointer to the
  267. * first bnode_chunk and returns a pointer to the bnode_hdr.
  268. * `bnode_hdr' and `bnode_chunk' are on the same block of allocated memory.
  269. * The number of bnode_chunks is stored in `bnode_links'.
  270. * On errors it returns a NULL pointer.
  271. */
  272. bnode_hdr *tracer_build_bentry(void *void_map, void *void_node,
  273. quadro_group *node_quadg, bnode_chunk **bnodechunk,
  274. int *bnode_links, u_char level)
  275. {
  276. map_node *int_map, *node;
  277. map_gnode **ext_map, *gnode;
  278. map_gnode *gn;
  279. bnode_hdr *bhdr;
  280. bnode_chunk *bchunk;
  281. int i, bm, node_pos;
  282. size_t bblock_sz;
  283. u_char lvl;
  284. char *bblock;
  285. u_char *bnode_gid;
  286. int_map=(map_node *)void_map;
  287. node=(map_node *)void_node;
  288. ext_map=(map_gnode **)void_map;
  289. gnode=(map_gnode *)void_node;
  290. if(level == me.cur_quadg.levels-1)
  291. goto error;
  292. if(!level)
  293. node_pos=pos_from_node(node, int_map);
  294. else
  295. node_pos=pos_from_gnode(gnode, ext_map[_EL(level)]);
  296. bm=map_find_bnode(me.bnode_map[level], me.bmap_nodes[level], node_pos);
  297. if(bm==-1)
  298. goto error;
  299. /*This will never happen, but we know the universe is fucking bastard*/
  300. if(!me.bnode_map[level][bm].links)
  301. goto error;
  302. bblock_sz = BNODEBLOCK_SZ(level+1, me.bnode_map[level][bm].links);
  303. bblock=xzalloc(bblock_sz);
  304. bhdr=(bnode_hdr *)bblock;
  305. bhdr->bnode_levels=level+1;
  306. bnode_gid=(u_char *)(bblock + sizeof(bnode_hdr));
  307. bchunk=(bnode_chunk *)(bnode_gid + sizeof(u_char)*bhdr->bnode_levels);
  308. for(i=0; i<bhdr->bnode_levels; i++)
  309. bnode_gid[i] = node_quadg->gid[i];
  310. /* Fill the bnode chunks */
  311. for(i=0; i < me.bnode_map[level][bm].links; i++) {
  312. gn=(map_gnode *)me.bnode_map[level][bm].r_node[i].r_node;
  313. lvl=extmap_find_level(me.ext_map, gn, me.cur_quadg.levels);
  314. if(lvl != level+1)
  315. continue;
  316. bchunk[i].gnode=pos_from_gnode(gn, me.ext_map[_EL(lvl)]);
  317. bchunk[i].level=lvl;
  318. bchunk[i].rtt=me.bnode_map[level][bm].r_node[i].trtt;
  319. bhdr->links++;
  320. debug(DBG_INSANE, "tracer_build_bentry: lvl %d bchunk[%d].gnode:"
  321. " %d", level, i, bchunk[i].gnode);
  322. }
  323. if(!bhdr->links) {
  324. xfree(bblock);
  325. goto error;
  326. }
  327. /* Reduce the size of the bblock to its effective size. Initially we
  328. * allocated it considering all the `me.bnode_map[level][bm].links'
  329. * links, but if bhdr->links is lesser than
  330. * me.bnode_map[level][bm].links that means they are not all added in
  331. * the chunks.
  332. */
  333. if(bhdr->links < me.bnode_map[level][bm].links) {
  334. bblock_sz = BNODEBLOCK_SZ(bhdr->bnode_levels, bhdr->links);
  335. bblock = xrealloc(bblock, bblock_sz);
  336. bhdr=(bnode_hdr *)bblock;
  337. bchunk=(bnode_chunk *)(bblock + BNODE_HDR_SZ(bhdr->bnode_levels));
  338. }
  339. *bnode_links=bhdr->links;
  340. *bnodechunk=bchunk;
  341. return bhdr;
  342. error:
  343. *bnode_links=0;
  344. *bnodechunk=0;
  345. return 0;
  346. }
  347. /*
  348. * tracer_pkt_build:
  349. * It builds a tracer_pkt and stores it in `pkt'.
  350. *
  351. * If `trcr_hdr' or `tracer' are null, it will build a brand new tracer_pkt,
  352. * otherwise it will append in the `tracer' the new entry.
  353. * Tracer_pkt_build will append also the old bblock:
  354. * `old_bblocks' is the number of bblocks,
  355. * `old_bblock_buf' is the block of the old bblock and it is `old_bblock_sz' big.
  356. * If `old_bblocks' is 0 or `old_bblock_buf' and `old_bblock_sz' are null
  357. * they are ignored.
  358. *
  359. * The `pkt.hdr.op' is set to `rq', `pkt.hdr.id' to `rq_id' and the
  360. * `bcast_hdr.sub_id' to `bcast_sub_id'.
  361. *
  362. * The packet shall be sent with flood_pkt_send.
  363. * It returns -1 on errors.
  364. */
  365. int tracer_pkt_build(u_char rq, int rq_id, int bcast_sub_id,
  366. int gnode_id, u_char gnode_level,
  367. brdcast_hdr *bcast_hdr, tracer_hdr *trcr_hdr, tracer_chunk *tracer,
  368. u_short old_bblocks, char *old_bblock_buf, size_t old_bblock_sz,
  369. PACKET *pkt)
  370. {
  371. brdcast_hdr bh;
  372. tracer_hdr th;
  373. tracer_chunk *new_tracer=0;
  374. bnode_hdr *new_bhdr=0;
  375. bnode_chunk *new_bchunk=0;
  376. map_node *root_node, *upper_root_node=0;
  377. char *igw_pack=0;
  378. void *void_map, *void_node, *p;
  379. size_t new_bblock_sz=0, total_bblock_sz=0, igw_pack_sz=0;
  380. u_int hops=0;
  381. int new_bblock_links=0, new_bblocks=0, tot_new_bblocks=0, tot_bblocks=0;
  382. if(!trcr_hdr || !tracer || !bcast_hdr) {
  383. /* Brand new tracer packet */
  384. bcast_hdr=&bh;
  385. setzero(bcast_hdr, sizeof(brdcast_hdr));
  386. bcast_hdr->gttl=MAXGROUPNODE-1;
  387. bcast_hdr->level=gnode_level+1;
  388. bcast_hdr->g_node=gnode_id;
  389. trcr_hdr=&th;
  390. setzero(trcr_hdr, sizeof(tracer_hdr));
  391. }
  392. hops=trcr_hdr->hops;
  393. setzero(pkt, sizeof(PACKET));
  394. pkt->hdr.op=rq;
  395. pkt->hdr.id=rq_id;
  396. pkt->hdr.flags|=BCAST_PKT;
  397. bcast_hdr->flags|=BCAST_TRACER_PKT;
  398. if(!gnode_level) {
  399. void_map=(void *)me.int_map;
  400. root_node=me.cur_node;
  401. void_node=(void *)root_node;
  402. } else {
  403. void_map=(void *)me.ext_map;
  404. root_node=&me.cur_quadg.gnode[_EL(gnode_level)]->g;
  405. void_node=(void *)root_node;
  406. }
  407. if(gnode_level < me.cur_quadg.levels)
  408. upper_root_node=&me.cur_quadg.gnode[_EL(gnode_level+1)]->g;
  409. /*
  410. * Time to append our entry in the tracer_pkt
  411. */
  412. new_tracer=tracer_add_entry(void_map, void_node, tracer, &hops,
  413. gnode_level);
  414. if(!new_tracer) {
  415. debug(DBG_NOISE, "tracer_pkt_build: Cannot add the new"
  416. " entry in the tracer_pkt");
  417. return -1;
  418. }
  419. if(rq == QSPN_OPEN && !trcr_hdr->first_qspn_open_chunk)
  420. trcr_hdr->first_qspn_open_chunk=hops;
  421. /* If we are a bnode we have to append the bnode_block too. */
  422. if(me.cur_node->flags & MAP_BNODE &&
  423. gnode_level < me.cur_quadg.levels-1 &&
  424. upper_root_node->flags & MAP_BNODE) {
  425. new_bhdr=tracer_build_bentry(void_map, void_node,&me.cur_quadg,
  426. &new_bchunk, &new_bblock_links, gnode_level);
  427. if(new_bhdr) {
  428. tot_new_bblocks=1;
  429. new_bblock_sz=BNODEBLOCK_SZ(new_bhdr->bnode_levels,
  430. new_bblock_links);
  431. bcast_hdr->flags|=BCAST_TRACER_BBLOCK;
  432. trcr_hdr->flags|=TRCR_BBLOCK;
  433. }
  434. }
  435. if(restricted_mode &&
  436. ((!gnode_level && server_opt.share_internet && me.inet_connected) ||
  437. (gnode_level && me.igws_counter[gnode_level-1]))) {
  438. igw_pack=igw_build_bentry(gnode_level, &igw_pack_sz, &new_bblocks);
  439. /* Append the igw_pack after the new bblock */
  440. if(igw_pack) {
  441. total_bblock_sz = new_bblock_sz + igw_pack_sz;
  442. new_bhdr=xrealloc(new_bhdr, total_bblock_sz);
  443. tot_new_bblocks += new_bblocks;
  444. bcast_hdr->flags|=BCAST_TRACER_BBLOCK;
  445. trcr_hdr->flags|=TRCR_IGW;
  446. p=(char *)new_bhdr + new_bblock_sz;
  447. memcpy(p, igw_pack, igw_pack_sz);
  448. }
  449. new_bblock_sz+=igw_pack_sz;
  450. }
  451. /*
  452. * If in the old tracer_pkt is present a bblock, we append it after the
  453. * new entry.
  454. */
  455. if(old_bblocks && old_bblock_buf && old_bblock_sz) {
  456. total_bblock_sz = new_bblock_sz + old_bblock_sz;
  457. new_bhdr=xrealloc(new_bhdr, total_bblock_sz);
  458. p=(char *)new_bhdr + new_bblock_sz;
  459. memcpy(p, old_bblock_buf, old_bblock_sz);
  460. bcast_hdr->flags|=BCAST_TRACER_BBLOCK;
  461. new_bblock_sz+=old_bblock_sz;
  462. }
  463. tot_bblocks=tot_new_bblocks+old_bblocks;
  464. /*
  465. * Here we are really building the pkt, packing all the stuff into a
  466. * single bullet.
  467. */
  468. trcr_hdr->hops=hops;
  469. bcast_hdr->sub_id=bcast_sub_id;
  470. bcast_hdr->sz=TRACERPKT_SZ(hops)+new_bblock_sz;
  471. pkt->hdr.sz=BRDCAST_SZ(bcast_hdr->sz);
  472. pkt_addcompress(pkt);
  473. pkt_addtimeout(pkt, TRACER_RQ_TIMEOUT, 0, 1);
  474. pkt_addnonblock(pkt);
  475. pkt->msg=tracer_pack_pkt(bcast_hdr, trcr_hdr, new_tracer,
  476. (char *)new_bhdr, new_bblock_sz, tot_bblocks);
  477. /* Yea, finished */
  478. if(new_tracer)
  479. xfree(new_tracer);
  480. if(new_bhdr)
  481. xfree(new_bhdr);
  482. return 0;
  483. }
  484. /*
  485. * tracer_pack_pkt: it packs the tracer packet.
  486. *
  487. * If `new_bblocks' isn't zero, the first `new_bblocks'# bblocks contained in
  488. * `bblocks' are converted to network order.
  489. */
  490. char *tracer_pack_pkt(brdcast_hdr *bcast_hdr, tracer_hdr *trcr_hdr, tracer_chunk *tracer,
  491. char *bblocks, size_t bblocks_sz, int new_bblocks)
  492. {
  493. bnode_hdr *bhdr;
  494. bnode_chunk *bchunk;
  495. size_t pkt_sz;
  496. char *msg, *buf;
  497. int i, e;
  498. pkt_sz=BRDCAST_SZ(TRACERPKT_SZ(trcr_hdr->hops) + bblocks_sz);
  499. buf=msg=xzalloc(pkt_sz);
  500. /* add broadcast header */
  501. memcpy(buf, bcast_hdr, sizeof(brdcast_hdr));
  502. ints_host_to_network(buf, brdcast_hdr_iinfo);
  503. buf+=sizeof(brdcast_hdr);
  504. /* add the tracer header */
  505. memcpy(buf, trcr_hdr, sizeof(tracer_hdr));
  506. ints_host_to_network(buf, tracer_hdr_iinfo);
  507. buf+=sizeof(tracer_hdr);
  508. /* add the tracer chunks and convert them to network order */
  509. for(i=0; i<trcr_hdr->hops; i++) {
  510. memcpy(buf, &tracer[i], sizeof(tracer_chunk));
  511. ints_host_to_network(buf, tracer_chunk_iinfo);
  512. buf+=sizeof(tracer_chunk);
  513. }
  514. /* add the bnode blocks */
  515. if(bblocks_sz && bblocks) {
  516. /* copy the whole block */
  517. memcpy(buf, bblocks, bblocks_sz);
  518. /* and convert it to network order */
  519. for(e=0; e<new_bblocks; e++) {
  520. bhdr=(bnode_hdr *)buf;
  521. bchunk=(bnode_chunk *)((char *)buf+sizeof(bnode_hdr)+
  522. sizeof(u_char)*bhdr->bnode_levels);
  523. for(i=0; i<bhdr->links; i++)
  524. ints_host_to_network(&bchunk[i], bnode_chunk_iinfo);
  525. buf+=BNODEBLOCK_SZ(bhdr->bnode_levels, bhdr->links);
  526. ints_host_to_network(bhdr, bnode_hdr_iinfo);
  527. }
  528. }
  529. return msg;
  530. }
  531. /*
  532. * tracer_unpack_pkt: Given a packet `rpkt' it scomposes the rpkt.msg in
  533. * `new_bcast_hdr', `new_tracer_hdr', `new_tracer', 'new_bhdr', and
  534. * `new_block_sz'.
  535. * If the `new_rip_quadg' pointer is not null, the quadro_group of the
  536. * `rpk.from' ip is stored in it.
  537. * It returns 0 if the packet is valid, otherwise -1 is returned.
  538. * Note that rpkt.msg will be modified during the unpacking.
  539. */
  540. int tracer_unpack_pkt(PACKET rpkt, brdcast_hdr **new_bcast_hdr,
  541. tracer_hdr **new_tracer_hdr, tracer_chunk **new_tracer,
  542. bnode_hdr **new_bhdr, size_t *new_bblock_sz,
  543. quadro_group *new_rip_quadg, int *real_from_rpos)
  544. {
  545. brdcast_hdr *bcast_hdr;
  546. tracer_hdr *trcr_hdr;
  547. tracer_chunk *tracer;
  548. bnode_hdr *bhdr=0;
  549. quadro_group rip_quadg;
  550. size_t bblock_sz=0, tracer_sz=0;
  551. int level, i;
  552. bcast_hdr=BRDCAST_HDR_PTR(rpkt.msg);
  553. ints_network_to_host(bcast_hdr, brdcast_hdr_iinfo);
  554. trcr_hdr=TRACER_HDR_PTR(rpkt.msg);
  555. ints_network_to_host(trcr_hdr, tracer_hdr_iinfo);
  556. tracer=TRACER_CHUNK_PTR(rpkt.msg);
  557. *new_bcast_hdr=0;
  558. *new_tracer_hdr=0;
  559. *new_tracer=0;
  560. *new_bhdr=0;
  561. *new_bblock_sz=0;
  562. *real_from_rpos=0;
  563. tracer_sz=BRDCAST_SZ(TRACERPKT_SZ(trcr_hdr->hops));
  564. if(tracer_sz > rpkt.hdr.sz || !trcr_hdr->hops ||
  565. trcr_hdr->hops > MAXGROUPNODE) {
  566. debug(DBG_INSANE, "%s:%d messed tracer pkt: %d, %d, %d",
  567. ERROR_POS, tracer_sz, rpkt.hdr.sz,
  568. trcr_hdr->hops);
  569. return -1;
  570. }
  571. if(rpkt.hdr.op == QSPN_CLOSE)
  572. /* It can be non-zero only if it is a QSPN_OPEN */
  573. trcr_hdr->first_qspn_open_chunk=0;
  574. /* Convert the tracer chunks to host order */
  575. for(i=0; i<trcr_hdr->hops; i++)
  576. ints_network_to_host(&tracer[i], tracer_chunk_iinfo);
  577. if(rpkt.hdr.sz > tracer_sz) {
  578. /* There is also a bnode block in the tracer pkt */
  579. bblock_sz=rpkt.hdr.sz-tracer_sz;
  580. bhdr=(bnode_hdr *)(rpkt.msg+tracer_sz);
  581. if((!(trcr_hdr->flags & TRCR_BBLOCK) && !(trcr_hdr->flags & TRCR_IGW)) ||
  582. !(bcast_hdr->flags & BCAST_TRACER_BBLOCK)) {
  583. debug(DBG_INSANE, ERROR_MSG "trcr_flags: %d flags: %d",
  584. ERROR_POS, trcr_hdr->flags,
  585. bcast_hdr->flags);
  586. return -1;
  587. }
  588. }
  589. if((level=bcast_hdr->level) > 0)
  590. level--;
  591. if(!(rpkt.hdr.flags & BCAST_PKT) || !(bcast_hdr->flags & BCAST_TRACER_PKT) ||
  592. level > FAMILY_LVLS) {
  593. debug(DBG_INSANE, "%s:%d", ERROR_POS);
  594. return -1;
  595. }
  596. /* Convert the ip in quadro_group */
  597. iptoquadg(rpkt.from, me.ext_map, &rip_quadg, QUADG_GID|QUADG_GNODE);
  598. memcpy(new_rip_quadg, &rip_quadg, sizeof(quadro_group));
  599. if(tracer_verify_pkt(tracer, trcr_hdr->hops, rpkt.from, &rip_quadg, level))
  600. return -1;
  601. *real_from_rpos=ip_to_rfrom(rpkt.from, &rip_quadg, 0, 0);
  602. if(*real_from_rpos < 0) {
  603. debug(DBG_INSANE, "%s:%d", ERROR_POS);
  604. return -1;
  605. }
  606. *new_bcast_hdr=bcast_hdr;
  607. *new_tracer_hdr=trcr_hdr;
  608. *new_tracer=tracer;
  609. *new_bhdr=bhdr;
  610. *new_bblock_sz=bblock_sz;
  611. return 0;
  612. }
  613. /*
  614. * tracer_split_bblock: It searches from bnode_block_start to
  615. * bnode_block_start+bblock_sz for bnode blocks.
  616. * It puts the address of the found bblock_hdr in the `bbl_hdr' (bnode block list)
  617. * and the address pointing to the start of the bnode_chunk in the `bbl'. The
  618. * total size of all the valid bblocks considered is stored in `*bblock_found_sz'.
  619. * It then returns the number of bblocks found.
  620. *
  621. * During the splitting the bblock is modified 'cause it is converted in host
  622. * order.
  623. * Remember to xfree bbl_hdr and bbl after using tracer_split_bblock too.
  624. * On error zero is returned.
  625. */
  626. u_short tracer_split_bblock(void *bnode_block_start, size_t bblock_sz, bnode_hdr ***bbl_hdr,
  627. bnode_chunk ****bbl, size_t *bblock_found_sz)
  628. {
  629. bnode_hdr *bblock_hdr;
  630. bnode_chunk *bblock;
  631. bnode_hdr **bblist_hdr=0;
  632. bnode_chunk ***bblist=0;
  633. u_char *bnode_gid;
  634. size_t bsz=0;
  635. int loop,e,p,x=0;
  636. *bblock_found_sz=0;
  637. if(!bblock_sz)
  638. return 0;
  639. for(loop=0; loop <= 1; loop++) {
  640. /*
  641. * The second `for' below acts in different ways for different
  642. * values of `loop'.
  643. * When `loop' == 0 it just counts how many valid bblocks there
  644. * are, then it allocs the right amount of memory for
  645. * `bblist_hdr' and `bblist'.
  646. * When `loop' == 1 it fills the `bblist_hdr' and `bblist'
  647. * arrays.
  648. *
  649. * If we use just one loop we are forced to xrealloc
  650. * `bblist_hdr' and `bblist' many times, because we don't know
  651. * how many bblocks thereare. The malloc operation are slow,
  652. * therefore to use only one xmalloc we prefer to count first.
  653. */
  654. for(e=0, x=0; e < bblock_sz; ) {
  655. bblock_hdr=(void *)((char *)bnode_block_start + e);
  656. if(!loop)
  657. ints_network_to_host(bblock_hdr, bnode_hdr_iinfo);
  658. bnode_gid = (u_char *)bblock_hdr+sizeof(bnode_hdr);
  659. bblock = (bnode_chunk *)((char *)bnode_gid +
  660. (bblock_hdr->bnode_levels*sizeof(u_char)));
  661. if(bblock_hdr->links <= 0 || bblock_hdr->links >= MAXGROUPNODE) {
  662. e+=BNODEBLOCK_SZ(bblock_hdr->bnode_levels, 0);
  663. continue;
  664. }
  665. bsz=BNODEBLOCK_SZ(bblock_hdr->bnode_levels, bblock_hdr->links);
  666. /*Are we going far away the end of the buffer?*/
  667. if(bblock_sz-e < bsz)
  668. break;
  669. if(loop) {
  670. bblist_hdr[x]=bblock_hdr;
  671. bblist[x]=xmalloc(sizeof(bnode_chunk *) * bblock_hdr->links);
  672. for(p=0; p<bblock_hdr->links; p++) {
  673. bblist[x][p]=&bblock[p];
  674. ints_network_to_host(&bblock[p], bnode_chunk_iinfo);
  675. }
  676. }
  677. if(!loop)
  678. (*bblock_found_sz)+=bsz;
  679. x++;
  680. e+=bsz;
  681. }
  682. if(!loop) {
  683. if(!x)
  684. return 0;
  685. bblist_hdr=xmalloc(sizeof(bnode_hdr *) * x);
  686. bblist=xmalloc(sizeof(bnode_chunk *) * x);
  687. }
  688. }
  689. *bbl_hdr=bblist_hdr;
  690. *bbl=bblist;
  691. return x;
  692. }
  693. /*
  694. * tracer_store_bblock: stores in the bnode map the chunks of the bblock
  695. * starting at `bnode_block_start'.
  696. * In `*bblocks_found' it stores the number of bblocks considered and stores in
  697. * `bblocks_found_block' these bblocks. The `bblocks_found_block' remains in
  698. * host order.
  699. * Remember to xfree(bblocks_found_block);
  700. * On error -1 is returned.
  701. */
  702. int tracer_store_bblock(u_char level, tracer_hdr *trcr_hdr, tracer_chunk *tracer,
  703. void *bnode_block_start, size_t bblock_sz,
  704. u_short *bblocks_found, char **bblocks_found_block,
  705. size_t *bblock_found_sz)
  706. {
  707. map_node *node;
  708. map_gnode *gnode;
  709. void *void_node;
  710. bnode_hdr **bblist_hdr=0;
  711. bnode_chunk ***bblist=0;
  712. map_rnode rn;
  713. int i, e, o, f, p, bm, igws_found=0;
  714. u_short bb;
  715. size_t found_block_sz, bsz, x;
  716. char *found_block;
  717. u_char *bnode_gid, bnode, blevel;
  718. /*
  719. * Split the block
  720. */
  721. bb=tracer_split_bblock(bnode_block_start, bblock_sz, &bblist_hdr,
  722. &bblist, &found_block_sz);
  723. *bblocks_found = bb;
  724. if(!bb) {
  725. /* The bblock was malformed -_- */
  726. debug(DBG_NORMAL, ERROR_MSG "malformed bnode block", ERROR_POS);
  727. *bblock_found_sz = 0;
  728. *bblocks_found_block = 0;
  729. return -1;
  730. }
  731. /*
  732. * Store the received bnode blocks
  733. */
  734. igws_found=x=0;
  735. *bblocks_found_block=found_block=xmalloc(found_block_sz);
  736. for(i=0; i<bb; i++) {
  737. bnode_gid=(u_char *)bblist_hdr[i] + sizeof(bnode_hdr);
  738. /* We update only the bmaps which are at
  739. * levels where our gnodes are in common with
  740. * those of the bnode, which sent us this
  741. * bblock */
  742. for(o=level, f=0; o >= 0; o--)
  743. if(bnode_gid[o] != me.cur_quadg.gid[o]) {
  744. f=1;
  745. break;
  746. }
  747. if(!f) {
  748. /*
  749. * bnode_gid is equal to me.cur_quadg.gid, so this
  750. * bnode block was sent by ourself, skip it.
  751. */
  752. debug(DBG_NORMAL, ERROR_MSG "skipping the %d bnode,"
  753. "it was built by us!", ERROR_POS, i);
  754. goto discard_bblock;
  755. }
  756. /*
  757. * Check if this bblock is an IGW. If it is, store it in
  758. * me.igws
  759. */
  760. if(bblist[i][0]->level >= FAMILY_LVLS+1) {
  761. if(restricted_mode &&
  762. (igws_found < MAX_IGW_PER_QSPN_CHUNK &&
  763. trcr_hdr->flags & TRCR_IGW)) {
  764. if(server_opt.use_shared_inet)
  765. igw_store_bblock(bblist_hdr[i],
  766. bblist[i][0], level);
  767. igws_found++;
  768. goto skip_bmap;
  769. } else {
  770. debug(DBG_NOISE, ERROR_MSG "Malforded bblock entry",
  771. ERROR_POS);
  772. goto discard_bblock;
  773. }
  774. }
  775. if(!(trcr_hdr->flags & TRCR_BBLOCK)) {
  776. debug(DBG_NOISE, ERROR_MSG "Malforded bblock entry", ERROR_POS);
  777. goto discard_bblock;
  778. }
  779. for(blevel=o; blevel < bblist_hdr[i]->bnode_levels; blevel++) {
  780. bnode=bnode_gid[blevel];
  781. if(!blevel) {
  782. node=node_from_pos(bnode, me.int_map);
  783. node->flags|=MAP_BNODE;
  784. node->flags&=~QSPN_OLD;
  785. void_node=(void *)node;
  786. } else {
  787. gnode=gnode_from_pos(bnode, me.ext_map[_EL(blevel)]);
  788. gnode->g.flags|=MAP_BNODE;
  789. gnode->g.flags&=~QSPN_OLD;
  790. void_node=(void *)&gnode->g;
  791. }
  792. /* Let's check if we have this bnode in the bmap, if not let's
  793. * add it */
  794. bm=map_find_bnode(me.bnode_map[blevel], me.bmap_nodes[blevel],
  795. bnode);
  796. if(bm==-1)
  797. bm=map_add_bnode(&me.bnode_map[blevel],
  798. &me.bmap_nodes[blevel],
  799. bnode, 0);
  800. /* This bnode has the BMAP_UPDATE
  801. * flag set, thus this is the first
  802. * time we update him during this new
  803. * qspn_round and for this reason
  804. * delete all its rnodes */
  805. if(me.bnode_map[blevel][bm].flags & BMAP_UPDATE) {
  806. rnode_destroy(&me.bnode_map[blevel][bm]);
  807. me.bnode_map[blevel][bm].flags&=~BMAP_UPDATE;
  808. }
  809. /* Store the rnodes of the bnode */
  810. for(e=0; e < bblist_hdr[i]->links; e++) {
  811. setzero(&rn, sizeof(map_rnode));
  812. debug(DBG_INSANE, "Bnode %d new link %d: gid %d lvl %d",
  813. bnode, e, bblist[i][e]->gnode,
  814. bblist[i][e]->level);
  815. gnode=gnode_from_pos(bblist[i][e]->gnode,
  816. me.ext_map[_EL(bblist[i][e]->level)]);
  817. gnode->g.flags&=~QSPN_OLD;
  818. rn.r_node=(int *)gnode;
  819. rn.trtt=bblist[i][e]->rtt;
  820. if((p=rnode_find(&me.bnode_map[blevel][bm], gnode)) > 0) {
  821. /* Overwrite the current rnode */
  822. map_rnode_insert(&me.bnode_map[blevel][bm],p,&rn);
  823. } else
  824. /* Add a new rnode */
  825. rnode_add(&me.bnode_map[blevel][bm], &rn);
  826. }
  827. }
  828. skip_bmap:
  829. /* Copy the found bblock in `bblocks_found_block' and converts
  830. * it in network order */
  831. bsz=BNODEBLOCK_SZ(bblist_hdr[i]->bnode_levels, bblist_hdr[i]->links);
  832. memcpy(found_block+x, bblist_hdr[i], bsz);
  833. x+=bsz;
  834. discard_bblock:
  835. xfree(bblist[i]);
  836. }
  837. *bblock_found_sz=x;
  838. xfree(bblist_hdr);
  839. xfree(bblist);
  840. return 0;
  841. }
  842. /*
  843. * tracer_check_node_collision: if a collision is detected between me and the
  844. * (g)node `node', new_rehook shall be called and 1 returned.
  845. * `tr_node' is the gid of `node'.
  846. */
  847. int tracer_check_node_collision(tracer_hdr *trcr, int hop, map_node *node,
  848. int tr_node, int tr_gcount, int level)
  849. {
  850. map_gnode *gnode;
  851. int probable_collision=0;
  852. u_int gcount;
  853. map_node *root_node;
  854. gnode=(map_gnode *)node;
  855. if(!level) {
  856. gcount=0;
  857. root_node=me.cur_node;
  858. } else {
  859. gcount=tr_gcount;
  860. root_node=&me.cur_quadg.gnode[_EL(level)]->g;
  861. }
  862. if(node == root_node &&
  863. (
  864. (
  865. trcr->first_qspn_open_chunk &&
  866. !(
  867. (trcr->first_qspn_open_chunk-1 == hop &&
  868. (root_node->flags & QSPN_OPENER)) ||
  869. (hop < trcr->first_qspn_open_chunk-1)
  870. )
  871. ) ||
  872. (
  873. !trcr->first_qspn_open_chunk &&
  874. !(!hop && (root_node->flags & QSPN_STARTER))
  875. )
  876. )
  877. )
  878. probable_collision=1;
  879. if(probable_collision) {
  880. loginfo("%s collision detected! Checking rehook status...",
  881. !level ? "node" : "gnode");
  882. debug(DBG_NORMAL,"collision info: i: %d, starter %d opener %d",
  883. hop, me.cur_node->flags & QSPN_STARTER,
  884. me.cur_node->flags & QSPN_OPENER);
  885. new_rehook(gnode, tr_node, level, gcount);
  886. return 1;
  887. }
  888. return 0;
  889. }
  890. /*
  891. * tracer_store_pkt: This is the main function used to keep the int/ext_map's
  892. * karma in peace.
  893. * It updates the internal or external map with the given tracer pkt.
  894. *
  895. * `rip' is the rnode ip. It is the last node who forwarded the tracer pkt to
  896. * us. `rip_quadg' is a quadro_group struct related to it.
  897. *
  898. * `trcr_hdr' is the header of the tracer pkt, while `tracer' points at the
  899. * start of its body.
  900. *
  901. * The bnode blocks (if any) are unpacked and used to update the data of the
  902. * bordering gnodes. Read the tracer_store_bblock() description (above) to
  903. * know the meaning of the other arguments.
  904. */
  905. int tracer_store_pkt(inet_prefix rip, quadro_group *rip_quadg, u_char level,
  906. tracer_hdr *trcr_hdr, tracer_chunk *tracer,
  907. void *bnode_block_start, size_t bblock_sz,
  908. u_short *bblocks_found, char **bblocks_found_block,
  909. size_t *bblock_found_sz)
  910. {
  911. map_node *from, *node, *root_node;
  912. map_gnode *gfrom, *gnode=0;
  913. map_rnode rnn;
  914. int i, e, x, f, diff, from_rnode_pos, skip_rfrom;
  915. int gfrom_rnode_pos, from_tpos;
  916. u_int hops, trtt_ms=0;
  917. hops = trcr_hdr->hops;
  918. /* Nothing to store */
  919. if(hops <= 0)
  920. return 0;
  921. from_tpos = hops-1;
  922. if(!level) {
  923. from = node_from_pos(tracer[from_tpos].node, me.int_map);
  924. root_node = me.cur_node;
  925. } else {
  926. gfrom = gnode_from_pos(tracer[from_tpos].node, me.ext_map[_EL(level)]);
  927. from = &gfrom->g;
  928. root_node = &me.cur_quadg.gnode[_EL(level)]->g;
  929. }
  930. from_rnode_pos = rnode_find(root_node, from);
  931. /* It's alive, keep it young */
  932. from->flags&=~QSPN_OLD;
  933. if(bblock_sz && level != me.cur_quadg.levels-1) {
  934. /* Well, well, we have to take care of bnode blocks, split the
  935. * bblock. */
  936. tracer_store_bblock(level, trcr_hdr, tracer, bnode_block_start,
  937. bblock_sz, bblocks_found, bblocks_found_block,
  938. bblock_found_sz);
  939. }
  940. /*
  941. * * Store the qspn routes to reach all the nodes of the tracer pkt *
  942. */
  943. skip_rfrom=0;
  944. node=root_node;
  945. if(!level) {
  946. /* We skip the node at hops-1 which it is the `from' node. The radar()
  947. * takes care of him. */
  948. skip_rfrom = 1;
  949. } else if(from == root_node) {
  950. /* If tracer[hops-1].node is our gnode then we can skip it */
  951. skip_rfrom = 1;
  952. from_tpos = hops-2;
  953. from_rnode_pos=ip_to_rfrom(rip, rip_quadg, 0, 0);
  954. if(hops > 1) {
  955. map_rnode rnn;
  956. /*
  957. * hops-2 is an rnode of hops-1, which is our gnode,
  958. * so we update the `gfrom' and `from' vars and let
  959. * them point to hops-2.
  960. */
  961. gfrom=gnode_from_pos(tracer[hops-2].node,
  962. me.ext_map[_EL(level)]);
  963. from = &gfrom->g;
  964. from->flags|=MAP_GNODE | MAP_RNODE;
  965. gfrom_rnode_pos=rnode_find(root_node, gfrom);
  966. if(gfrom_rnode_pos == -1) {
  967. gfrom_rnode_pos=root_node->links;
  968. /*
  969. * Add an rnode in the root_node which point to
  970. * `gfrom', because it is our new (g)rnode.
  971. */
  972. setzero(&rnn, sizeof(map_rnode));
  973. rnn.r_node=(int *)gfrom;
  974. rnode_add(root_node, &rnn);
  975. }
  976. root_node->r_node[gfrom_rnode_pos].trtt=tracer[hops-2].rtt;
  977. }
  978. /* we are using the real from, so the root node is the one
  979. * at level 0 */
  980. node=me.cur_node;
  981. } else if(me.cur_node->flags & MAP_BNODE) {
  982. /* If we are a bnode which borders on the `from' [g]node, then we
  983. * can skip it. */
  984. i=map_find_bnode_rnode(me.bnode_map[level-1], me.bmap_nodes[level-1], from);
  985. if(i != -1)
  986. skip_rfrom = 1;
  987. }
  988. if(from_tpos >= 0) { /* Is there a rnode in the tracer ? */
  989. /* Update `qspn_gnode_count' */
  990. tracer_update_gcount(trcr_hdr, tracer, from_tpos,
  991. qspn_gnode_count, me.int_map, me.ext_map, level);
  992. /* Let's see if we have to rehook */
  993. new_rehook((map_gnode *)from, tracer[from_tpos].node, level,
  994. tracer[from_tpos].gcount);
  995. }
  996. /* We add in the total rtt the first rtt which is me -> from */
  997. trtt_ms=node->r_node[from_rnode_pos].trtt;
  998. /* If we are skipping the rfrom, remember to sum its rtt */
  999. if(skip_rfrom)
  1000. trtt_ms+=tracer[hops-1].rtt;
  1001. for(i=(hops-skip_rfrom)-1; i >= 0; i--) {
  1002. if(i)
  1003. trtt_ms+=tracer[i].rtt;
  1004. if(!level)
  1005. node=node_from_pos(tracer[i].node, me.int_map);
  1006. else {
  1007. gnode=gnode_from_pos(tracer[i].node, me.ext_map[_EL(level)]);
  1008. node=&gnode->g;
  1009. if(tracer[i].gcount == NODES_PER_LEVEL(level))
  1010. /* The gnode is full */
  1011. gnode->g.flags|=GMAP_FULL;
  1012. }
  1013. if(tracer_check_node_collision(trcr_hdr, i, node, tracer[i].node,
  1014. tracer[i].gcount, level))
  1015. break;
  1016. node->flags&=~QSPN_OLD;
  1017. if(node->flags & MAP_VOID) {
  1018. /* Ehi, we hadn't this node in the map. Add it. */
  1019. node->flags&=~MAP_VOID;
  1020. node->flags|=MAP_UPDATE;
  1021. if(level)
  1022. gnode->flags&=~GMAP_VOID;
  1023. gnode_inc_seeds(&me.cur_quadg, level);
  1024. debug(DBG_INSANE, "TRCR_STORE: node %d added", tracer[i].node);
  1025. }
  1026. /* update the rtt of the node */
  1027. for(e=0,f=0; e < node->links; e++) {
  1028. if(node->r_node[e].r_node == (int *)from) {
  1029. diff=abs(node->r_node[e].trtt - trtt_ms);
  1030. if(diff >= RTT_DELTA) {
  1031. node->r_node[e].trtt = trtt_ms;
  1032. node->flags|=MAP_UPDATE;
  1033. }
  1034. f=1;
  1035. break;
  1036. }
  1037. }
  1038. if(!f) {
  1039. /*If the `node' doesn't have `from' in his r_nodes... let's add it*/
  1040. setzero(&rnn, sizeof(map_rnode));
  1041. rnn.r_node=(int *)from;
  1042. rnn.trtt=trtt_ms;
  1043. rnode_add(node, &rnn);
  1044. node->flags|=MAP_UPDATE;
  1045. }
  1046. /* ok, now the kernel needs a refresh of the routing table */
  1047. if(node->flags & MAP_UPDATE) {
  1048. rnode_trtt_order(node);
  1049. if(node->links > MAXROUTES) {
  1050. /*
  1051. * If we have too many routes we purge the worst
  1052. * ones.
  1053. */
  1054. for(x=MAXROUTES; x < node->links; x++)
  1055. rnode_del(node, x);
  1056. }
  1057. debug(DBG_INSANE, "TRCR_STORE: krnl_update node %d", tracer[i].node);
  1058. rt_update_node(0, node, 0, 0, 0, level);
  1059. node->flags&=~MAP_UPDATE;
  1060. }
  1061. }
  1062. return 0;
  1063. }
  1064. /*
  1065. * flood_pkt_send: This functions is used to propagate packets, in a broadcast
  1066. * manner, in a entire gnode of a specified level.
  1067. * It sends the `pkt' to all the nodes excluding the excluded nodes. It knows
  1068. * if a node is excluded by calling the `is_node_excluded' function. The
  1069. * second argument to this function is the node who sent the pkt and it must be
  1070. * always excluded. The third argument is the position of the node being processed
  1071. * in the r_node array of me.cur_node. The other arguments are described in
  1072. * tracer.h.
  1073. * If `is_node_excluded' returns a non 0 value, the node is considered as excluded.
  1074. * The `from_rpos' argument is the node who sent the `pkt'.
  1075. * It returns the number of pkts sent or -1 on errors. Note that the total pkt sent
  1076. * should be == me.cur_node->links-the_excluded_nodes.
  1077. * Note that `level', `sub_id', and `from_rpos' are vars used only by
  1078. * is_node_excluded() (see tracer.h).
  1079. */
  1080. int flood_pkt_send(int(*is_node_excluded)(TRACER_PKT_EXCLUDE_VARS), u_char level,
  1081. int sub_id, int from_rpos, PACKET pkt)
  1082. {
  1083. ext_rnode *e_rnode;
  1084. map_node *dst_node, *node;
  1085. ssize_t err;
  1086. const char *ntop;
  1087. int i, e=0;
  1088. /*
  1089. * Forward the pkt to all our r_nodes (excluding the excluded;)
  1090. */
  1091. for(i=0; i < me.cur_node->links; i++) {
  1092. node=(map_node *)me.cur_node->r_node[i].r_node;
  1093. if(node->flags & MAP_ERNODE) {
  1094. e_rnode=(ext_rnode *)node;
  1095. dst_node=(map_node *)e_rnode->quadg.gnode[_EL(level-1)];
  1096. } else {
  1097. e_rnode=0;
  1098. dst_node=node;
  1099. }
  1100. if(!dst_node)
  1101. continue;
  1102. if(is_node_excluded(e_rnode, dst_node, from_rpos, i, level, sub_id))
  1103. continue;
  1104. /* Get the socket associated to the rnode */
  1105. if(rnl_fill_rq(node, &pkt) < 0)
  1106. continue;
  1107. if(server_opt.dbg_lvl)
  1108. debug(DBG_INSANE, "flood_pkt_send(0x%x): %s to %s"
  1109. " lvl %d", pkt.hdr.id,
  1110. rq_to_str(pkt.hdr.op),
  1111. inet_to_str(pkt.to), level-1);
  1112. /* Let's send the pkt */
  1113. err=rnl_send_rq(node, &pkt, 0, pkt.hdr.op, pkt.hdr.id,
  1114. 0, 0, 0);
  1115. if(err==-1) {
  1116. ntop=inet_to_str(pkt.to);
  1117. error(ERROR_MSG "Cannot send the %s request"
  1118. " with id: %d to %s",
  1119. ERROR_FUNC, rq_to_str(pkt.hdr.op),
  1120. pkt.hdr.id, ntop);
  1121. } else
  1122. e++;
  1123. }
  1124. pkt_free(&pkt, 0);
  1125. return e;
  1126. }
  1127. /* * * Exclude functions * * *
  1128. * These exclude function are used in conjunction with flood_pkt_send.
  1129. * They return 1 if the node has to be excluded, otherwise 0.
  1130. */
  1131. /*
  1132. * exclude_glevel: Exclude `node' if it doesn't belong to the gid (`excl_gid') of
  1133. * the level (`excl_level') specified.
  1134. */
  1135. int exclude_glevel(TRACER_PKT_EXCLUDE_VARS)
  1136. {
  1137. /* If `node' is null we can exclude it, because it isn't a gnode
  1138. * of ours levels */
  1139. if(!node)
  1140. return 1;
  1141. /* Ehi, if the node isn't even an external rnode, we don't exclude it. */
  1142. if(!(node->flags & MAP_ERNODE))
  1143. return 0;
  1144. /* Reach the sky */
  1145. if(excl_level == me.cur_quadg.levels)
  1146. return 0;
  1147. return quadg_gids_cmp(e_rnode->quadg, me.cur_quadg, excl_level);
  1148. }
  1149. /* Exclude the `from' node */
  1150. int exclude_from(TRACER_PKT_EXCLUDE_VARS)
  1151. {
  1152. if(pos == from_rpos)
  1153. return 1;
  1154. return 0;
  1155. }
  1156. /* Exclude all the nodes, except the from node */
  1157. int exclude_all_but_notfrom(TRACER_PKT_EXCLUDE_VARS)
  1158. {
  1159. if(!exclude_from(TRACER_PKT_EXCLUDE_VARS_NAME))
  1160. return 1;
  1161. return 0;
  1162. }
  1163. int exclude_from_and_glevel(TRACER_PKT_EXCLUDE_VARS)
  1164. {
  1165. if(exclude_glevel(TRACER_PKT_EXCLUDE_VARS_NAME) ||
  1166. exclude_from(TRACER_PKT_EXCLUDE_VARS_NAME))
  1167. return 1;
  1168. return 0;
  1169. }
  1170. /*
  1171. * tracer_pkt_recv: It receive a TRACER_PKT or a TRACER_PKT_CONNECT, analyzes
  1172. * the received pkt, adds the new entry in it and forward the pkt to all
  1173. * the r_nodes.
  1174. */
  1175. int tracer_pkt_recv(PACKET rpkt)
  1176. {
  1177. PACKET pkt;
  1178. brdcast_hdr *bcast_hdr;
  1179. tracer_hdr *trcr_hdr;
  1180. tracer_chunk *tracer;
  1181. bnode_hdr *bhdr=0;
  1182. map_node *from, *tracer_starter, *root_node;
  1183. map_gnode *gfrom;
  1184. quadro_group rip_quadg;
  1185. int(*exclude_function)(TRACER_PKT_EXCLUDE_VARS);
  1186. int ret_err, gid, real_from_rpos;
  1187. u_int hops;
  1188. size_t bblock_sz=0, old_bblock_sz;
  1189. u_short old_bblocks_found=0;
  1190. u_char level, orig_lvl;
  1191. const char *ntop=0;
  1192. char *old_bblock=0;
  1193. void *void_map;
  1194. ret_err=tracer_unpack_pkt(rpkt, &bcast_hdr, &trcr_hdr, &tracer,
  1195. &bhdr, &bblock_sz, &rip_quadg, &real_from_rpos);
  1196. if(ret_err) {
  1197. ntop=inet_to_str(rpkt.from);
  1198. debug(DBG_NOISE, "tracer_pkt_recv(): The %s sent an invalid "
  1199. "tracer_pkt here.", ntop);
  1200. return -1;
  1201. }
  1202. hops=trcr_hdr->hops;
  1203. gid=bcast_hdr->g_node;
  1204. level=orig_lvl=bcast_hdr->level;
  1205. if(!level || level == 1) {
  1206. level=0;
  1207. root_node = me.cur_node;
  1208. from = node_from_pos(tracer[hops-1].node, me.int_map);
  1209. tracer_starter = node_from_pos(tracer[0].node, me.int_map);
  1210. void_map=me.int_map;
  1211. } else {
  1212. level--;
  1213. root_node = &me.cur_quadg.gnode[_EL(level)]->g;
  1214. gfrom = gnode_from_pos(tracer[hops-1].node, me.ext_map[_EL(level)]);
  1215. from = &gfrom->g;
  1216. tracer_starter = (map_node *)gnode_from_pos(tracer[0].node, me.ext_map[_EL(level)]);
  1217. void_map = me.ext_map;
  1218. }
  1219. if(server_opt.dbg_lvl) {
  1220. ntop=inet_to_str(rpkt.from);
  1221. debug(DBG_NOISE, "Tracer_pkt(0x%x, lvl %d) received from %s",
  1222. rpkt.hdr.id, level, ntop);
  1223. }
  1224. /*
  1225. * This is the check for the broadcast id. If it is <= tracer_starter->brdcast
  1226. * the pkt is an old broadcast that still dance around.
  1227. */
  1228. if(rpkt.hdr.id <= tracer_starter->brdcast) {
  1229. debug(DBG_NOISE, "tracer_pkt_recv(): Received from %s an old "
  1230. "tracer_pkt broadcast: 0x%x, cur: 0x%x", ntop,
  1231. rpkt.hdr.id, tracer_starter->brdcast);
  1232. return -1;
  1233. } else
  1234. tracer_starter->brdcast=rpkt.hdr.id;
  1235. /*
  1236. * Time to update our map
  1237. */
  1238. if(rpkt.hdr.op == TRACER_PKT) { /*This check is made because tracer_pkt_recv
  1239. handles also TRACER_PKT_CONNECT pkts*/
  1240. ret_err=tracer_store_pkt(rpkt.from, &rip_quadg, level,
  1241. trcr_hdr, tracer, (void *)bhdr,
  1242. bblock_sz, &old_bblocks_found, &old_bblock,
  1243. &old_bblock_sz);
  1244. if(ret_err) {
  1245. ntop=inet_to_str(rpkt.from);
  1246. debug(DBG_NORMAL, "tracer_pkt_recv(): Cannot store the"
  1247. " tracer_pkt received from %s", ntop);
  1248. }
  1249. }
  1250. /*
  1251. * Drop the pkt if it is bound to the contigual qspn starters and we
  1252. * aren't a qspn_starter
  1253. */
  1254. if(bcast_hdr->flags & BCAST_TRACER_STARTERS &&
  1255. !(root_node->flags & QSPN_STARTER))
  1256. return 0;
  1257. /*The forge of the packet.*/
  1258. if((!level || ((me.cur_node->flags & MAP_BNODE) &&
  1259. (root_node->flags & MAP_BNODE))) &&
  1260. from != root_node) {
  1261. tracer_pkt_build(rpkt.hdr.op, rpkt.hdr.id, bcast_hdr->sub_id, /*IDs*/
  1262. gid, level,
  1263. bcast_hdr, trcr_hdr, tracer, /*Received tracer_pkt*/
  1264. old_bblocks_found, old_bblock, old_bblock_sz, /*bnode_block*/
  1265. &pkt); /*Where the pkt is built*/
  1266. } else {
  1267. /* Increment the rtt of the last gnode chunk */
  1268. ret_err=tracer_add_rtt(real_from_rpos, tracer, hops-1);
  1269. if(ret_err < 0)
  1270. debug(DBG_NOISE, "tracer_add_rtt(0x%x) hop %d failed",
  1271. rpkt.hdr.id, hops-1);
  1272. pkt_copy(&pkt, &rpkt);
  1273. pkt_clear(&pkt);
  1274. }
  1275. /*... forward the tracer_pkt to our r_nodes*/
  1276. exclude_function=exclude_from_and_glevel;
  1277. flood_pkt_send(exclude_function, orig_lvl, real_from_rpos,
  1278. real_from_rpos, pkt);
  1279. if(old_bblock)
  1280. xfree(old_bblock);
  1281. return 0;
  1282. }
  1283. /*
  1284. * tracer_pkt_start: It sends only a normal tracer_pkt. This is useful after
  1285. * the hook, to let all the other nodes know we are alive and to give them
  1286. * the right route.
  1287. */
  1288. int tracer_pkt_start(u_char level)
  1289. {
  1290. PACKET pkt;
  1291. int root_node_pos;
  1292. if(tracer_pkt_start_mutex)
  1293. return 0;
  1294. else
  1295. tracer_pkt_start_mutex=1;
  1296. if(!level || level == 1) {
  1297. level=0;
  1298. root_node_pos=pos_from_node(me.cur_node, me.int_map);
  1299. } else
  1300. root_node_pos=pos_from_gnode(me.cur_quadg.gnode[_EL(level)],
  1301. me.ext_map[_EL(level)]);
  1302. me.cur_node->brdcast++;
  1303. tracer_pkt_build(TRACER_PKT, me.cur_node->brdcast, root_node_pos,/*IDs*/
  1304. me.cur_quadg.gid[level+1], level, /*GnodeID and level*/
  1305. 0, 0, 0, /*Received tracer_pkt*/
  1306. 0, 0, 0, /*bnode_block*/
  1307. &pkt); /*Where the pkt is built*/
  1308. /*Diffuse the packet in all the universe!*/
  1309. debug(DBG_INSANE, "Tracer_pkt 0x%x starting.", pkt.hdr.id);
  1310. flood_pkt_send(exclude_from_and_glevel, level+1, -1, -1, pkt);
  1311. tracer_pkt_start_mutex=0;
  1312. return 0;
  1313. }