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.

andna.c 68KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589
  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. * --
  19. * andna.c:
  20. * Here there are all the functions that send, receive and exec ANDNA packets.
  21. * All the threads of the ANDNA daemon and the main andna functions are here
  22. * too.
  23. */
  24. #include "includes.h"
  25. #include "inet.h"
  26. #include "endianness.h"
  27. #include "map.h"
  28. #include "gmap.h"
  29. #include "bmap.h"
  30. #include "route.h"
  31. #include "request.h"
  32. #include "tracer.h"
  33. #include "qspn.h"
  34. #include "radar.h"
  35. #include "netsukuku.h"
  36. #include "daemon.h"
  37. #include "crypto.h"
  38. #include "snsd_cache.h"
  39. #include "andna_cache.h"
  40. #include "andna.h"
  41. #include "andns.h"
  42. #include "dns_wrapper.h"
  43. #include "hash.h"
  44. #include "common.h"
  45. /*
  46. *
  47. * * * * (de)-Initialization functions * * *
  48. *
  49. */
  50. /*
  51. * andna_load_caches
  52. *
  53. * loads all the ANDNA caches
  54. */
  55. int
  56. andna_load_caches(void)
  57. {
  58. int ret = 0;
  59. if (file_exist(server_opt.lclkey_file) &&
  60. (!load_lcl_keyring(&lcl_keyring, server_opt.lclkey_file)))
  61. debug(DBG_NORMAL, "Andna LCL Keyring loaded");
  62. if (file_exist(server_opt.lcl_file) &&
  63. (andna_lcl = load_lcl_cache(server_opt.lcl_file, &lcl_counter)))
  64. debug(DBG_NORMAL, "Andna Local Cache loaded");
  65. if (file_exist(server_opt.andna_cache_file) &&
  66. (andna_c = load_andna_cache(server_opt.andna_cache_file,
  67. &andna_c_counter)))
  68. debug(DBG_NORMAL, "Andna cache loaded");
  69. if (file_exist(server_opt.counter_c_file) &&
  70. (andna_counter_c = load_counter_c(server_opt.counter_c_file,
  71. &cc_counter)))
  72. debug(DBG_NORMAL, "Counter cache loaded");
  73. if (file_exist(server_opt.rhc_file) &&
  74. (andna_rhc = load_rh_cache(server_opt.rhc_file, &rhc_counter)))
  75. debug(DBG_NORMAL, "Resolved hostnames cache loaded");
  76. if (file_exist(server_opt.andna_hnames_file) &&
  77. !(load_hostnames(server_opt.andna_hnames_file,
  78. &andna_lcl, &lcl_counter)))
  79. debug(DBG_NORMAL, "Hostnames file loaded");
  80. if (file_exist(server_opt.snsd_nodes_file) &&
  81. !(ret = load_snsd(server_opt.snsd_nodes_file, andna_lcl)))
  82. debug(DBG_NORMAL, "SNSD nodes loaded");
  83. else if (ret == -2)
  84. fatal("Malformed %s file", server_opt.snsd_nodes_file);
  85. return 0;
  86. }
  87. int
  88. andna_save_caches(void)
  89. {
  90. debug(DBG_NORMAL, "Saving the andna local cache");
  91. save_lcl_cache(andna_lcl, server_opt.lcl_file);
  92. debug(DBG_NORMAL, "Saving the andna cache");
  93. save_andna_cache(andna_c, server_opt.andna_cache_file);
  94. debug(DBG_NORMAL, "Saving the andna counter cache");
  95. save_counter_c(andna_counter_c, server_opt.counter_c_file);
  96. debug(DBG_NORMAL, "Saving the resolved hnames cache");
  97. save_rh_cache(andna_rhc, server_opt.rhc_file);
  98. return 0;
  99. }
  100. /*
  101. * andna_resolvconf_modify: modifies /etc/resolv.conf. See add_resolv_conf().
  102. */
  103. void
  104. andna_resolvconf_modify(void)
  105. {
  106. int ret;
  107. char *my_nameserv;
  108. if (!server_opt.disable_resolvconf) {
  109. loginfo("Modifying /etc/resolv.conf");
  110. my_nameserv =
  111. my_family == AF_INET ? MY_NAMESERV : MY_NAMESERV_IPV6;
  112. ret = add_resolv_conf(my_nameserv, ETC_RESOLV_CONF);
  113. if (ret < 0)
  114. error("It wasn't possible to modify %s, you have to add "
  115. "\"%s\" by yourself", ETC_RESOLV_CONF, my_nameserv);
  116. } else
  117. loginfo
  118. ("Modification of /etc/resolv.conf is disabled: do it by yourself.");
  119. }
  120. void
  121. andna_resolvconf_restore(void)
  122. {
  123. char *my_nameserv;
  124. /*
  125. * If there are valid nameserver in /etc/resolv.conf (excluding
  126. * "127.0.0.1", do not restore the backup. Probably /etc/resolv.conf
  127. * has been edited manually. Damn you, user! Why can't we code you ;)
  128. */
  129. reset_andns_ns();
  130. if (collect_resolv_conf(ETC_RESOLV_CONF) != -1)
  131. return;
  132. reset_andns_ns();
  133. my_nameserv = my_family == AF_INET ? MY_NAMESERV : MY_NAMESERV_IPV6;
  134. del_resolv_conf(my_nameserv, ETC_RESOLV_CONF);
  135. }
  136. void
  137. andna_init(void)
  138. {
  139. /* register the andna's ops in the pkt_op_table */
  140. add_pkt_op(ANDNA_REGISTER_HNAME, SKT_TCP, andna_tcp_port,
  141. andna_recv_reg_rq);
  142. add_pkt_op(ANDNA_CHECK_COUNTER, SKT_TCP, andna_tcp_port,
  143. andna_recv_check_counter);
  144. add_pkt_op(ANDNA_RESOLVE_HNAME, SKT_UDP, andna_udp_port,
  145. andna_recv_resolve_rq);
  146. add_pkt_op(ANDNA_RESOLVE_REPLY, SKT_UDP, andna_udp_port, 0);
  147. add_pkt_op(ANDNA_RESOLVE_IP, SKT_TCP, andna_tcp_port,
  148. andna_recv_rev_resolve_rq);
  149. add_pkt_op(ANDNA_REV_RESOLVE_REPLY, SKT_TCP, andna_tcp_port, 0);
  150. add_pkt_op(ANDNA_GET_ANDNA_CACHE, SKT_TCP, andna_tcp_port,
  151. put_andna_cache);
  152. add_pkt_op(ANDNA_PUT_ANDNA_CACHE, SKT_TCP, andna_tcp_port, 0);
  153. add_pkt_op(ANDNA_GET_COUNT_CACHE, SKT_TCP, andna_tcp_port,
  154. put_counter_cache);
  155. add_pkt_op(ANDNA_PUT_COUNT_CACHE, SKT_TCP, andna_tcp_port, 0);
  156. add_pkt_op(ANDNA_GET_SINGLE_ACACHE, SKT_UDP, andna_udp_port,
  157. put_single_acache);
  158. add_pkt_op(ANDNA_SPREAD_SACACHE, SKT_UDP, andna_udp_port,
  159. recv_spread_single_acache);
  160. if (!server_opt.disable_resolvconf)
  161. /* Restore resolv.conf if our backup is still there */
  162. andna_resolvconf_restore();
  163. pkt_queue_init();
  164. andna_caches_init(my_family);
  165. snsd_cache_init(my_family);
  166. /* Load the good old caches */
  167. andna_load_caches();
  168. if (!lcl_keyring.priv_rsa) {
  169. /*
  170. * No keyring was loaded, generate a new one save it
  171. */
  172. lcl_new_keyring(&lcl_keyring);
  173. debug(DBG_NORMAL, "Saving the new andna local keyring");
  174. save_lcl_keyring(&lcl_keyring, server_opt.lclkey_file);
  175. /**/}
  176. /* Init ANDNS */
  177. if (andns_init(restricted_mode, ETC_RESOLV_CONF, my_family) < 0)
  178. if (andns_init(restricted_mode, ETC_RESOLV_CONF_BAK, my_family) <
  179. 0) {
  180. error("In %s there isn't a single Internet nameserver.",
  181. ETC_RESOLV_CONF);
  182. loginfo("Internet hostname resolution is disabled");
  183. }
  184. setzero(last_reg_pkt_id, sizeof(int) * ANDNA_MAX_FLOODS);
  185. setzero(last_counter_pkt_id, sizeof(int) * ANDNA_MAX_FLOODS);
  186. setzero(last_spread_acache_pkt_id, sizeof(int) * ANDNA_MAX_FLOODS);
  187. /* Modify /etc/resolv.conf if requested */
  188. andna_resolvconf_modify();
  189. }
  190. void
  191. andna_close(void)
  192. {
  193. andna_save_caches();
  194. if (!server_opt.disable_resolvconf)
  195. andna_resolvconf_restore();
  196. andns_close();
  197. lcl_destroy_keyring(&lcl_keyring);
  198. lcl_cache_destroy(andna_lcl, &lcl_counter);
  199. andna_cache_destroy();
  200. counter_c_destroy();
  201. rh_cache_flush();
  202. pkt_queue_close();
  203. }
  204. /*
  205. *
  206. * * * * Hash_node search functions * * *
  207. *
  208. */
  209. /*
  210. * andna_hash_by_family
  211. *
  212. * If `family' is equal to AF_INET, in `hash' it stores the
  213. * 32bit hash of the `msg', otherwise it just copies `msg' to `hash_ip'.
  214. * Note that this function is used to hash other hashes, so it operates on the
  215. * ANDNA_HASH_SZ fixed length.
  216. */
  217. void
  218. andna_hash_by_family(int family, void *msg, u_int hash[MAX_IP_INT])
  219. {
  220. setzero(hash, ANDNA_HASH_SZ);
  221. if (family == AF_INET)
  222. hash[0] = fnv_32_buf((u_char *) msg, ANDNA_HASH_SZ, FNV1_32_INIT);
  223. else
  224. memcpy(hash, msg, ANDNA_HASH_SZ);
  225. }
  226. /*
  227. * andna_hash
  228. *
  229. * This function makes a digest of `msg' which is `len' bytes big and
  230. * stores it in `hash'.
  231. * If `family'is equal to AF_INET, in `ip_hash' it stores the 32bit hash
  232. * of the `hash', otherwise it just copies `hash' to `ip_hash'.
  233. *
  234. * Note: `hash' is a single string of `MAX_IP_INT'*4 bytes, it is the MD5 hash
  235. * of `msg', therefore do not attempt to convert it to network order.
  236. */
  237. void
  238. andna_hash(int family, void *msg, int len, u_int hash[MAX_IP_INT],
  239. u_int ip_hash[MAX_IP_INT])
  240. {
  241. hash_md5(msg, len, (u_char *) hash);
  242. andna_hash_by_family(family, (u_char *) hash, ip_hash);
  243. }
  244. /*
  245. * is_hgnode_excluded: it converts the `qg'->gid to an ip, then it searches a
  246. * member of `excluded_hgnode', which has all its gids, from the level `lvl` to
  247. * FAMILY_LVLS, equal to the ip. If it is found 1 is returned, otherwise 0
  248. * will be the return value.
  249. * This function is utilised by find_hash_gnode() to exclude from the search
  250. * the hash_gnodes not wanted.
  251. * All the `excluded_hgnode[x]' which are a null pointer are skipped.
  252. */
  253. int
  254. is_hgnode_excluded(quadro_group * qg, u_int ** excluded_hgnode,
  255. int tot_excluded_hgnodes, int lvl)
  256. {
  257. int i, e, x, total_levels = FAMILY_LVLS;
  258. inet_prefix ip;
  259. for (e = 0; e < tot_excluded_hgnodes; e++) {
  260. x = 0;
  261. if (!excluded_hgnode[e])
  262. continue;
  263. inet_setip_raw(&ip, excluded_hgnode[e], my_family);
  264. for (i = lvl; i < total_levels; i++) {
  265. #ifdef DEBUG
  266. debug(DBG_INSANE,
  267. "is_hgnode_excluded: l %d, qg->gid %d, ipgid %d", i,
  268. qg->gid[i], iptogid(&ip, i));
  269. #endif
  270. if (qg->gid[i] != iptogid(&ip, i)) {
  271. x = 1;
  272. break;
  273. }
  274. }
  275. if (!x)
  276. return 1;
  277. }
  278. return 0;
  279. }
  280. /*
  281. * is_hgnodeip_excluded: is a wrapper of is_hgnode_excluded() which takes as
  282. * first argv an inet_prefix instead of a quadro_group
  283. */
  284. int
  285. is_hgnodeip_excluded(inet_prefix * hgnodeip, u_int ** excluded_hgnode,
  286. int tot_excluded_hgnodes)
  287. {
  288. quadro_group qg;
  289. iptoquadg(*hgnodeip, 0, &qg, QUADG_GID);
  290. return is_hgnode_excluded(&qg, excluded_hgnode, tot_excluded_hgnodes,
  291. 0);
  292. }
  293. /*
  294. * random_gid_level_0: chooses a random gid of level 0, which is up, and
  295. * stores it in `qg'->gid[0], then convert it to an ip and stores it in `to'.
  296. * If `exclude_me' isn't zero, it won't choose ourself as the gid of level 0.
  297. * If the gid is found 0 is returned otherwise -1 is the return value.
  298. * If the gid found is also a MAP_ME node, 2 is returned.
  299. */
  300. int
  301. random_gid_level_0(quadro_group * qg, inet_prefix * to, int exclude_me)
  302. {
  303. int x, e, i;
  304. /*
  305. * Set `e' and `i' to a rand value from 0 to MAXGROUPNODE-1.
  306. * In the first for go from `e' to MAXGROUPNODE-1, if nothing is found
  307. * continue in the second for and go from `i' to 0. If nothing is
  308. * found return -1.
  309. */
  310. for (x = 0, e = i = rand_range(0, MAXGROUPNODE - 1); e < MAXGROUPNODE;
  311. e++) {
  312. if (!(me.int_map[e].flags & MAP_VOID)) {
  313. if (exclude_me && (me.int_map[e].flags & MAP_ME))
  314. continue;
  315. qg->gid[0] = e;
  316. x = 1;
  317. break;
  318. }
  319. }
  320. if (!x)
  321. for (x = 0; i >= 0; i--) {
  322. if (!(me.int_map[i].flags & MAP_VOID)) {
  323. if (exclude_me && (me.int_map[i].flags & MAP_ME))
  324. continue;
  325. qg->gid[0] = i;
  326. x = 1;
  327. break;
  328. }
  329. }
  330. if (x) {
  331. gidtoipstart(qg->gid, me.cur_quadg.levels, me.cur_quadg.levels,
  332. my_family, to);
  333. debug(DBG_NOISE, "find_hashgnode: Internal found: gid0 %d, to "
  334. "%s!", qg->gid[0], inet_to_str(*to));
  335. return me.int_map[qg->gid[0]].flags & MAP_ME ? 2 : 0;
  336. }
  337. return -1;
  338. }
  339. /*
  340. * find_hash_gnode_recurse: it recurse itself to create multiple sub tree. The
  341. * tree is started by find_hash_gnode()
  342. */
  343. int
  344. find_hash_gnode_recurse(quadro_group qg, int level, inet_prefix * to,
  345. u_int ** excluded_hgnode, int tot_excluded_hgnodes,
  346. int exclude_me)
  347. {
  348. int gid, i, e, steps, err, ret;
  349. map_gnode *gnode;
  350. if (!level)
  351. return random_gid_level_0(&qg, to, exclude_me);
  352. /*
  353. * This is how the ip nearer to `hash' is found:
  354. * - find_hash_gnode() calls, for the first time,
  355. * find_hash_gnode_recurse(). The hash's ip is converted as a
  356. * quadro_group and stored in the argv `qg' and the parameter `level'
  357. * is set to the number of total levels available minus one.
  358. * - if `level' is equal to 0 choose a random gid of level 0 an
  359. * convert `qg' in the inet_prefix format, store it in `to' and
  360. * return.
  361. * loop1:
  362. * - If the gnode `gq'.gid[level] is down increment or decrement
  363. * (alternatively) `gq'.gid[level] and continue the loop1.
  364. * If (it is up) {
  365. * if (`gq'.gid[level] is a gnode where we belong) {
  366. * call recursively find_hash_gnode_recurse,
  367. * giving the new modified `qg' and `level'-1 as
  368. * new parametres.
  369. * If (the return value is not an error) {
  370. * return now with that value because in
  371. * a sub tree, the hash_gnode was already
  372. * found.
  373. * }
  374. * } otherwise {
  375. * return and stor into `to' the ip of the
  376. * border_node that will be used to forward the
  377. * pkt to the hash_gnode.
  378. * }
  379. * }
  380. * - return -1
  381. */
  382. #if 0 /* TOO NOISY */
  383. debug(DBG_INSANE, "find_hashgnode: lvl %d, start gid %d", level,
  384. qg.gid[level]);
  385. #endif
  386. /* the maximum steps required to complete the for. */
  387. steps =
  388. qg.gid[level] >=
  389. MAXGROUPNODE / 2 ? qg.gid[level] : MAXGROUPNODE - qg.gid[level];
  390. for (i = 0, e = 1, gid = qg.gid[level]; i < steps;
  391. e & 1 ? i++ : i, e = (~(e & 1)) & 1) {
  392. /* `i' is incremented only when `e' is odd, while `e'
  393. * is always alternated between 0 and 1. */
  394. if (!(e & 1) && (qg.gid[level] + i < MAXGROUPNODE))
  395. gid = qg.gid[level] + i;
  396. else if (qg.gid[level] - i >= 0)
  397. gid = qg.gid[level] - i;
  398. else
  399. continue;
  400. gnode = gnode_from_pos(gid, me.ext_map[_EL(level)]);
  401. if (!(gnode->g.flags & MAP_VOID) && !(gnode->flags & GMAP_VOID)) {
  402. qg.gid[level] = gid;
  403. #if 0 /* TOO NOISY */
  404. debug(DBG_NOISE, "find_hashgnode: lvl %d gid %d", level,
  405. qg.gid[level]);
  406. #endif
  407. if (!quadg_gids_cmp(qg, me.cur_quadg, level)) {
  408. /* Is this hash_gnode not wanted ? */
  409. if (excluded_hgnode && level == 1 &&
  410. is_hgnode_excluded(&qg, excluded_hgnode,
  411. tot_excluded_hgnodes, 1))
  412. continue; /* yea, exclude it */
  413. ret =
  414. find_hash_gnode_recurse(qg, level - 1, to,
  415. excluded_hgnode,
  416. tot_excluded_hgnodes,
  417. exclude_me);
  418. if (ret != -1)
  419. /* We have found it in some sub trees. */
  420. return ret;
  421. } else {
  422. /* Check if it is excluded */
  423. if (excluded_hgnode &&
  424. is_hgnode_excluded(&qg, excluded_hgnode,
  425. tot_excluded_hgnodes, level))
  426. continue; /* Yes, it is */
  427. err = get_gw_ips(me.int_map, me.ext_map, me.bnode_map,
  428. me.bmap_nodes, &me.cur_quadg,
  429. gnode, level, 0, to, 0, 1);
  430. debug(DBG_NOISE,
  431. "find_hashgnode: ext_found, err %d, to %s!", err,
  432. inet_to_str(*to));
  433. if (err >= 0)
  434. /*
  435. * Forward the pkt to the found
  436. * border_node, which will forward it
  437. * to the hash_gnode
  438. */
  439. return 1;
  440. }
  441. }
  442. } /* for(...) */
  443. #if 0 /* TOO NOISY */
  444. debug(DBG_INSANE, "find_hashgnode: Exausted: lvl %d gid %d", level,
  445. qg.gid[level]);
  446. #endif
  447. /* We didn't find anything in the this level, so
  448. * returns from this tree */
  449. return -1;
  450. }
  451. /*
  452. * find_hash_gnode: It stores in `to' the ip of the node nearer to the
  453. * `hash'_gnode and returns 0. If we aren't part of the hash_gnode, it sets in
  454. * `to' the ip of the bnode_gnode that will route the pkt and returns 1.
  455. * If the found hash_gnode is the MAP_ME node itself then 2 is returned.
  456. * If either a hash_gnode and a bnode_gnode aren't found, -1 is returned.
  457. * All the hash_gnodes included in `excluded_hgnode' will be excluded by the
  458. * algorithm.
  459. * If `exclude_me' is set to 1, it will not return ourself as the hash_gnode.
  460. */
  461. int
  462. find_hash_gnode(u_int hash[MAX_IP_INT], inet_prefix * to,
  463. u_int ** excluded_hgnode, int tot_excluded_hgnodes,
  464. int exclude_me)
  465. {
  466. int total_levels;
  467. quadro_group qg;
  468. total_levels = FAMILY_LVLS;
  469. /* Hash to ip and quadro_group conversion */
  470. inet_setip(to, hash, my_family);
  471. inet_htonl(to->data, to->family);
  472. iptoquadg(*to, me.ext_map, &qg, QUADG_GID | QUADG_GNODE);
  473. return find_hash_gnode_recurse(qg, total_levels - 1, to,
  474. excluded_hgnode, tot_excluded_hgnodes,
  475. exclude_me);
  476. }
  477. /*
  478. *
  479. * * * * Packets * * *
  480. *
  481. */
  482. /*
  483. * andna_flood_pkt: Sends the `rpkt' pkt to all the rnodes of our same gnode
  484. * and exclude the the rpkt->from node if `exclude_rfrom' is non zero.
  485. * The return value of flood_pkt_send() is returned.
  486. */
  487. int
  488. andna_flood_pkt(PACKET * rpkt, int exclude_rfrom)
  489. {
  490. PACKET flood_pkt;
  491. int real_from_rpos = -1, ret;
  492. int (*exclude_function) (TRACER_PKT_EXCLUDE_VARS);
  493. pkt_copy(&flood_pkt, rpkt);
  494. flood_pkt.sk = 0;
  495. /* be sure that the other nodes don't reply to rfrom again */
  496. flood_pkt.hdr.flags &= ~ASYNC_REPLY;
  497. flood_pkt.hdr.flags |= BCAST_PKT;
  498. /* If the pkt was sent from an our rnode, ignore it while flooding */
  499. if (exclude_rfrom)
  500. real_from_rpos = ip_to_rfrom(flood_pkt.from, 0, 0, 0);
  501. if (real_from_rpos < 0 || !exclude_rfrom) {
  502. exclude_function = exclude_glevel;
  503. real_from_rpos = -1;
  504. } else
  505. exclude_function = exclude_from_and_glevel;
  506. ret =
  507. flood_pkt_send(exclude_function, 1, -1, real_from_rpos, flood_pkt);
  508. return ret;
  509. }
  510. /*
  511. * andna_find_flood_pkt_id: Search in the `ids_array' a member which is equal
  512. * to `pkt_id', if it is found its array position is returned otherwise -1
  513. * will be the return value
  514. */
  515. int
  516. andna_find_flood_pkt_id(int *ids_array, int pkt_id)
  517. {
  518. int i;
  519. for (i = 0; i < ANDNA_MAX_FLOODS; i++)
  520. if (ids_array[i] == pkt_id)
  521. return i;
  522. return -1;
  523. }
  524. /*
  525. * andna_add_flood_pkt_id: If the `pkt_id' is already present in the
  526. * `ids_array', 1 is returned, otherwise `pkt_id' is added at the 0 position
  527. * of the array. All the array elements, except the last one, will be
  528. * preserved and shifted of one position like a FIFO.
  529. */
  530. int
  531. andna_add_flood_pkt_id(int *ids_array, int pkt_id)
  532. {
  533. int i;
  534. if ((i = andna_find_flood_pkt_id(ids_array, pkt_id)) < 0) {
  535. int tmp_array[ANDNA_MAX_FLOODS - 1];
  536. /* Shift the array of one position to free the position 0
  537. * where the new id will be added */
  538. memcpy(tmp_array, ids_array, sizeof(int) * (ANDNA_MAX_FLOODS - 1));
  539. memcpy(&ids_array[1], tmp_array,
  540. sizeof(int) * (ANDNA_MAX_FLOODS - 1));
  541. ids_array[0] = pkt_id;
  542. return 0;
  543. }
  544. return 1;
  545. }
  546. /*
  547. *
  548. * * * * Hostname registration * * *
  549. *
  550. */
  551. /*
  552. * andna_register_hname
  553. *
  554. * Registers or updates the `alcl->hostname' hostname. It also registers the
  555. * snsd records present in `alcl->service'.
  556. *
  557. * If `snsd_delete' is not null, the registration request becomes a deletion
  558. * request: it will ask to andna to delete all the snsd records equal to
  559. * `snsd_delete'.
  560. */
  561. int
  562. andna_register_hname(lcl_cache * alcl, snsd_service * snsd_delete)
  563. {
  564. PACKET pkt, rpkt;
  565. struct andna_reg_pkt req;
  566. u_int hash_gnode[MAX_IP_INT];
  567. inet_prefix to;
  568. snsd_service *snsd;
  569. char *sign = 0, *buf;
  570. const char *ntop;
  571. int ret = 0;
  572. ssize_t err, pkt_sz = 0;
  573. time_t cur_t;
  574. setzero(&req, sizeof(req));
  575. setzero(&pkt, sizeof(pkt));
  576. setzero(&rpkt, sizeof(rpkt));
  577. cur_t = time(0);
  578. if (alcl->flags & ANDNA_UPDATING)
  579. /* we are already updating this node! */
  580. return 0;
  581. alcl->flags |= ANDNA_UPDATING;
  582. if (alcl->timestamp) {
  583. if (cur_t > alcl->timestamp &&
  584. (cur_t - alcl->timestamp) < ANDNA_MIN_UPDATE_TIME)
  585. /* We have too wait a little more before sending an
  586. * update */
  587. return -1;
  588. req.flags |= ANDNA_PKT_UPDATE;
  589. req.hname_updates = ++alcl->hname_updates;
  590. }
  591. /* Don't register the hname while we are (re)-hooking,
  592. * our IP might change */
  593. while (me.cur_node->flags & MAP_HNODE)
  594. sleep(1);
  595. /*
  596. * Filling the request structure
  597. */
  598. inet_copy_ipdata(req.rip, &me.cur_ip);
  599. andna_hash(my_family, alcl->hostname, strlen(alcl->hostname),
  600. req.hash, hash_gnode);
  601. memcpy(req.pubkey, lcl_keyring.pubkey, ANDNA_PKEY_LEN);
  602. /* Convert the pkt from host to network order */
  603. ints_host_to_network((void *) &req, andna_reg_pkt_iinfo);
  604. /* Sign the packet */
  605. sign = (char *) rsa_sign((u_char *) & req, ANDNA_REG_SIGNED_BLOCK_SZ,
  606. lcl_keyring.priv_rsa, 0);
  607. memcpy(req.sign, sign, ANDNA_SIGNATURE_LEN);
  608. /* Find the hash_gnode that corresponds to the hash `hash_gnode' */
  609. if ((err = find_hash_gnode(hash_gnode, &to, 0, 0, 1)) < 0) {
  610. debug(DBG_SOFT, "andna_register_hname: hash_gnode not found ;(");
  611. ERROR_FINISH(ret, -1, finish);
  612. } else if (err == 1)
  613. req.flags |= ANDNA_PKT_FORWARD;
  614. if (snsd_delete) {
  615. snsd = snsd_delete;
  616. req.flags |= ANDNA_PKT_SNSD_DEL;
  617. } else
  618. snsd = alcl->service;
  619. ntop = inet_to_str(to);
  620. debug(DBG_INSANE, "Quest %s to %s", rq_to_str(ANDNA_REGISTER_HNAME),
  621. ntop);
  622. /* Fill the packet and send the request */
  623. pkt_sz = ANDNA_REG_PKT_SZ + SNSD_SERVICE_LLIST_PACK_SZ(alcl->service);
  624. pkt_addto(&pkt, &to);
  625. pkt_addcompress(&pkt);
  626. pkt_fill_hdr(&pkt.hdr, ASYNC_REPLY, 0, ANDNA_REGISTER_HNAME, pkt_sz);
  627. pkt.msg = buf = xmalloc(pkt_sz);
  628. memcpy(pkt.msg, &req, ANDNA_REG_PKT_SZ);
  629. buf += ANDNA_REG_PKT_SZ;
  630. if (snsd_pack_all_services(buf, pkt_sz - ANDNA_REG_PKT_SZ, snsd) < 0) {
  631. debug(DBG_SOFT, "andna_register_hname: couldn't pack "
  632. "alcl->service");
  633. ERROR_FINISH(ret, -1, finish);
  634. }
  635. err =
  636. send_rq(&pkt, 0, ANDNA_REGISTER_HNAME, 0, ACK_AFFERMATIVE, 1,
  637. &rpkt);
  638. if (err < 0) {
  639. error("andna_register_hname(): Registration of \"%s\" to %s "
  640. "failed.", alcl->hostname, ntop);
  641. ERROR_FINISH(ret, -1, finish);
  642. }
  643. /* Ehi, we've registered it! Update the hname timestamp */
  644. alcl->timestamp = cur_t;
  645. finish:
  646. alcl->flags &= ~ANDNA_UPDATING;
  647. if (sign)
  648. xfree(sign);
  649. pkt_free(&pkt, 1);
  650. pkt_free(&rpkt, 0);
  651. return ret;
  652. }
  653. /*
  654. * andna_recv_reg_rq
  655. *
  656. * It takes care of a registration request. If we aren't
  657. * the rightful hash_gnode, we forward the request (again), otherwise the
  658. * request is examined. If it is valid the hostname is registered or updated
  659. * in the andna_cache.
  660. */
  661. int
  662. andna_recv_reg_rq(PACKET rpkt)
  663. {
  664. PACKET pkt, rpkt_local_copy;
  665. struct andna_reg_pkt *req;
  666. u_int hash_gnode[MAX_IP_INT], *excluded_hgnode[1];
  667. inet_prefix rfrom, to;
  668. RSA *pubkey = 0;
  669. andna_cache_queue *acq = 0;
  670. andna_cache *ac = 0;
  671. snsd_service *snsd_unpacked = 0;
  672. snsd_node *snd = 0;
  673. time_t cur_t;
  674. u_short snsd_counter;
  675. int ret = 0, err;
  676. size_t unpacked_sz, packed_sz;
  677. char *ntop = 0, *rfrom_ntop = 0, *snsd_pack;
  678. u_char forwarded_pkt = 0;
  679. const u_char *pk;
  680. pkt_copy(&rpkt_local_copy, &rpkt);
  681. req = (struct andna_reg_pkt *) rpkt_local_copy.msg;
  682. if (rpkt.hdr.sz < ANDNA_REG_PKT_SZ ||
  683. rpkt.hdr.sz > SNSD_SERVICE_MAX_LLIST_PACK_SZ)
  684. ERROR_FINISH(ret, -1, finish);
  685. packed_sz = rpkt.hdr.sz - ANDNA_REG_PKT_SZ;
  686. snsd_pack = rpkt_local_copy.msg + ANDNA_REG_PKT_SZ;
  687. if (rpkt.hdr.flags & BCAST_PKT)
  688. /* The pkt we received has been only forwarded to us */
  689. forwarded_pkt = 1;
  690. /* Check if we already received this pkt during the flood */
  691. if (forwarded_pkt &&
  692. andna_add_flood_pkt_id(last_reg_pkt_id, rpkt.hdr.id)) {
  693. debug(DBG_INSANE, "Dropped 0x%0x andna pkt, we already "
  694. "received it", rpkt.hdr.id);
  695. ERROR_FINISH(ret, 0, finish);
  696. }
  697. /* Save the real sender of the request */
  698. inet_setip(&rfrom, req->rip, my_family);
  699. ntop = xstrdup(inet_to_str(rpkt.from));
  700. rfrom_ntop = xstrdup(inet_to_str(rfrom));
  701. debug(DBG_SOFT, "Received %s%sfrom %s, rfrom %s",
  702. rq_to_str(rpkt.hdr.op), forwarded_pkt ? " (forwarded) " : " ",
  703. ntop, rfrom_ntop);
  704. memcpy(&pkt, &rpkt, sizeof(PACKET));
  705. inet_copy(&pkt.from, &rfrom);
  706. /* Send the replies in UDP, they are so tiny */
  707. pkt_addsk(&pkt, my_family, 0, SKT_UDP);
  708. /* Verify the signature */
  709. pk = (const u_char *) req->pubkey;
  710. pubkey = get_rsa_pub(&pk, ANDNA_PKEY_LEN);
  711. if (!pubkey || !verify_sign((u_char *) req, ANDNA_REG_SIGNED_BLOCK_SZ,
  712. (u_char *) req->sign, ANDNA_SIGNATURE_LEN,
  713. pubkey)) {
  714. /* Bad, bad signature */
  715. debug(DBG_SOFT, "Invalid signature of the 0x%x reg request",
  716. rpkt.hdr.id);
  717. if (!forwarded_pkt)
  718. pkt_err(pkt, E_INVALID_SIGNATURE, 0);
  719. ERROR_FINISH(ret, -1, finish);
  720. }
  721. /* Revert the packet from network to host order */
  722. ints_network_to_host((void *) req, andna_reg_pkt_iinfo);
  723. /* If we don't belong to the gnode of `rfrom', then we have to exclude
  724. * it from the find_hash_gnode search, since we have received the
  725. * pkt from that gnode and it is likely that `rfrom' is the only node
  726. * of that gnode.
  727. */
  728. excluded_hgnode[0] = ip_gids_cmp(rfrom, me.cur_ip, 1) ? rfrom.data : 0;
  729. /*
  730. * Are we the hash_gnode for req->hash? Check also if we have to
  731. * continue to forward the pkt to make it reach the real hash_gnode
  732. */
  733. andna_hash_by_family(my_family, (u_char *) req->hash, hash_gnode);
  734. if ((err =
  735. find_hash_gnode(hash_gnode, &to, excluded_hgnode, 1, 0)) < 0) {
  736. debug(DBG_SOFT,
  737. "We are not the right (rounded)hash_gnode. "
  738. "Rejecting the 0x%x reg request", rpkt.hdr.id);
  739. if (!forwarded_pkt)
  740. pkt_err(pkt, E_ANDNA_WRONG_HASH_GNODE, 0);
  741. ERROR_FINISH(ret, -1, finish);
  742. } else if (err == 1) {
  743. if (!(req->flags & ANDNA_PKT_FORWARD)) {
  744. if (!forwarded_pkt)
  745. pkt_err(pkt, E_ANDNA_WRONG_HASH_GNODE, 0);
  746. ERROR_FINISH(ret, -1, finish);
  747. }
  748. /* Continue to forward the received pkt */
  749. debug(DBG_SOFT, "The reg request pkt will be forwarded to: %s",
  750. inet_to_str(to));
  751. ret = forward_pkt(rpkt, to);
  752. goto finish;
  753. }
  754. /* Are we a new hash_gnode ? */
  755. if (time(0) - me.uptime < (ANDNA_EXPIRATION_TIME / 3) &&
  756. !(ac = andna_cache_findhash((int *) req->hash))) {
  757. /*
  758. * We are a new hash_gnode and we haven't this hostname in our
  759. * andna_cache, so we have to check if there is an
  760. * old hash_gnode which has already registered this hostname.
  761. */
  762. if ((ac = get_single_andna_c(req->hash, hash_gnode))) {
  763. /*
  764. * The hostname was already registered, so we save it
  765. * in our andna_cache.
  766. */
  767. clist_add(&andna_c, &andna_c_counter, ac);
  768. /* Spread it in our gnode */
  769. spread_single_acache(req->hash);
  770. }
  771. }
  772. /* Ask the counter_node if it is ok to register/update the hname */
  773. if (!(req->flags & ANDNA_PKT_SNSD_DEL)) {
  774. if (andna_check_counter(rpkt) == -1) {
  775. debug(DBG_SOFT, "Registration rq 0x%x rejected: %s",
  776. rpkt.hdr.id, rq_strerror(E_ANDNA_CHECK_COUNTER));
  777. if (!forwarded_pkt)
  778. pkt_err(pkt, E_ANDNA_CHECK_COUNTER, 0);
  779. ERROR_FINISH(ret, -1, finish);
  780. }
  781. }
  782. /***
  783. * Finally, let's register/update the hname
  784. */
  785. cur_t = time(0);
  786. ac = andna_cache_addhash((int *) req->hash);
  787. acq = ac_queue_add(ac, req->pubkey);
  788. if (!acq) {
  789. debug(DBG_SOFT, "Registration rq 0x%x rejected: %s",
  790. rpkt.hdr.id, rq_strerror(E_ANDNA_QUEUE_FULL));
  791. if (!forwarded_pkt)
  792. pkt_err(pkt, E_ANDNA_QUEUE_FULL, 0);
  793. ERROR_FINISH(ret, -1, finish);
  794. }
  795. /***/
  796. /**
  797. * Check if the counter of number of updates matches with the pkt */
  798. if (acq->hname_updates > req->hname_updates) {
  799. debug(DBG_SOFT, "Registration rq 0x%x rejected: hname_updates"
  800. " mismatch %d > %d", rpkt.hdr.id,
  801. acq->hname_updates, req->hname_updates);
  802. if (!forwarded_pkt)
  803. pkt_err(pkt, E_ANDNA_HUPDATE_MISMATCH, 0);
  804. ERROR_FINISH(ret, -1, finish);
  805. }
  806. /**/
  807. /**
  808. * Has the registration request been sent too early ? */
  809. if (cur_t > acq->timestamp &&
  810. (cur_t - acq->timestamp) < ANDNA_MIN_UPDATE_TIME) {
  811. debug(DBG_SOFT, "Registration rq 0x%x rejected: %s",
  812. rpkt.hdr.id, rq_strerror(E_ANDNA_UPDATE_TOO_EARLY));
  813. if (!forwarded_pkt)
  814. pkt_err(pkt, E_ANDNA_UPDATE_TOO_EARLY, 0);
  815. ERROR_FINISH(ret, -1, finish);
  816. }
  817. /**/
  818. /*
  819. * SNSD services registration
  820. */
  821. if (acq != ac->acq) {
  822. /*
  823. * This request has been queued in the acq. We store the main
  824. * ip and discard the rest of the snsd records
  825. */
  826. snsd_add_mainip(&snsd_unpacked, &snsd_counter,
  827. SNSD_MAX_QUEUE_RECORDS,
  828. (uintptr_t*)rfrom.data);
  829. } else {
  830. /*
  831. * Register the snsd records
  832. */
  833. /* unpack the snsd services */
  834. snsd_unpacked = snsd_unpack_all_service(snsd_pack, packed_sz,
  835. &unpacked_sz,
  836. &snsd_counter);
  837. if ((!snsd_unpacked && req->flags & ANDNA_PKT_SNSD_DEL) ||
  838. (snsd_counter > SNSD_MAX_RECORDS - 1)) {
  839. debug(DBG_SOFT,
  840. "Registration rq 0x%x rejected: couldn't unpack"
  841. " the snsd llist", rpkt.hdr.id);
  842. if (!forwarded_pkt)
  843. pkt_err(pkt, E_INVALID_REQUEST, 0);
  844. ERROR_FINISH(ret, -1, finish);
  845. }
  846. if (req->flags & ANDNA_PKT_SNSD_DEL) {
  847. /*
  848. * Delete, from the andna_cache, all the snsd records
  849. * which are equal to those included in the
  850. * registration pkt.
  851. */
  852. snsd_record_del_selected(&acq->service, &acq->snsd_counter,
  853. snsd_unpacked);
  854. } else {
  855. /*
  856. * Register the snsd services in our andna_cache
  857. */
  858. /* Add the mainip and update its IP */
  859. snd = snsd_add_mainip(&snsd_unpacked, &snsd_counter,
  860. SNSD_MAX_RECORDS,
  861. (uintptr_t*)rfrom.data);
  862. inet_copy_ipdata_raw((u_int*)snd->record, &rfrom);
  863. /**
  864. * Set the mainip flag only to the real mainip record,
  865. * in this way we're sure that there is only one. */
  866. snsd_unset_all_flags(snsd_unpacked, SNSD_NODE_MAIN_IP);
  867. snd->flags |= SNSD_NODE_MAIN_IP;
  868. /**/}
  869. }
  870. /*
  871. * substitute the old snsd records with the new ones
  872. */
  873. snsd_service_llist_del(&acq->service);
  874. acq->service = snsd_unpacked;
  875. acq->snsd_counter = snsd_counter;
  876. /*
  877. * Final registration step: touch the hname timestamp
  878. */
  879. acq->hname_updates = req->hname_updates + 1;
  880. acq->timestamp = cur_t;
  881. /* Reply to the requester: <<Yes, don't worry, it worked.>> */
  882. if (!forwarded_pkt) {
  883. debug(DBG_SOFT, "Registration rq 0x%x accepted.", rpkt.hdr.id);
  884. pkt.msg = 0;
  885. pkt_fill_hdr(&pkt.hdr, ASYNC_REPLIED, rpkt.hdr.id, ACK_AFFERMATIVE,
  886. 0);
  887. ret = forward_pkt(pkt, rfrom);
  888. }
  889. /**
  890. * Broadcast the request to the entire gnode of level 1 to let the
  891. * other nodes register the hname.
  892. */
  893. if (!forwarded_pkt)
  894. andna_add_flood_pkt_id(last_reg_pkt_id, rpkt.hdr.id);
  895. andna_flood_pkt(&rpkt, 1);
  896. /**/ finish:
  897. if (ntop)
  898. xfree(ntop);
  899. if (rfrom_ntop)
  900. xfree(rfrom_ntop);
  901. if (pubkey)
  902. RSA_free(pubkey);
  903. pkt_free(&rpkt_local_copy, 0);
  904. return ret;
  905. }
  906. /*
  907. * andna_check_counter
  908. *
  909. * asks the counter_node if it is ok to accept the registration request in
  910. * `pkt' and register the requested hname.
  911. *
  912. * If -1 is returned the answer is no.
  913. */
  914. int
  915. andna_check_counter(PACKET pkt)
  916. {
  917. PACKET rpkt;
  918. int ret = 0, err;
  919. u_int rip_hash[MAX_IP_INT], hash_gnode[MAX_IP_INT];
  920. struct andna_reg_pkt *req;
  921. const char *ntop;
  922. u_char forwarded_pkt = 0;
  923. setzero(&rpkt, sizeof(PACKET));
  924. req = (struct andna_reg_pkt *) pkt.msg;
  925. pkt.msg = 0;
  926. if (pkt.hdr.flags & BCAST_PKT)
  927. forwarded_pkt = 1;
  928. /* Calculate the hash of the IP of the sender node. This hash will
  929. * be used to reach its counter node. */
  930. andna_hash(my_family, req->rip, MAX_IP_SZ, rip_hash, hash_gnode);
  931. /* Find a hash_gnode for the rip_hash */
  932. req->flags &= ~ANDNA_PKT_FORWARD;
  933. if ((err = find_hash_gnode(hash_gnode, &pkt.to, 0, 0, 1)) < 0) {
  934. debug(DBG_INSANE, "andna_check_counter: Couldn't find a decent"
  935. " counter_gnode");
  936. ERROR_FINISH(ret, -1, finish);
  937. } else if (err == 1)
  938. req->flags |= ANDNA_PKT_FORWARD;
  939. ntop = inet_to_str(pkt.to);
  940. debug(DBG_INSANE, "Quest %s to %s", rq_to_str(ANDNA_CHECK_COUNTER),
  941. ntop);
  942. pkt.hdr.flags |= ASYNC_REPLY;
  943. /* Append our ip in the pkt */
  944. pkt.hdr.sz = ANDNA_REG_PKT_SZ + MAX_IP_SZ;
  945. pkt.msg = xmalloc(pkt.hdr.sz);
  946. memcpy(pkt.msg, req, ANDNA_REG_PKT_SZ);
  947. inet_copy_ipdata((u_int *) (pkt.msg + ANDNA_REG_PKT_SZ), &me.cur_ip);
  948. /* If we are checking a registration pkt which has been only
  949. * forwarded to us (and already registered), we tell the counter_node
  950. * to just check the request. We don't want it to update its
  951. * hname_updates counter */
  952. if (forwarded_pkt) {
  953. req = (struct andna_reg_pkt *) pkt.msg;
  954. req->flags |= ANDNA_PKT_JUST_CHECK;
  955. /* Adjust the flags */
  956. pkt.hdr.flags &= ~BCAST_PKT;
  957. }
  958. /* Throw it */
  959. pkt.sk = 0; /* Create a new connection */
  960. ret =
  961. send_rq(&pkt, 0, ANDNA_CHECK_COUNTER, 0, ACK_AFFERMATIVE, 1,
  962. &rpkt);
  963. ret = (ret < 0) ? -1 : ret;
  964. finish:
  965. pkt_free(&pkt, 1);
  966. pkt_free(&rpkt, 0);
  967. return ret;
  968. }
  969. int
  970. andna_recv_check_counter(PACKET rpkt)
  971. {
  972. PACKET pkt, rpkt_local_copy;
  973. struct andna_reg_pkt *req;
  974. inet_prefix rfrom, to;
  975. RSA *pubkey = 0;
  976. counter_c *cc;
  977. counter_c_hashes *cch;
  978. u_int rip_hash[MAX_IP_INT], hash_gnode[MAX_IP_INT],
  979. *excluded_hgnode[1];
  980. int ret = 0, err, old_updates;
  981. char *ntop = 0, *rfrom_ntop = 0, *buf;
  982. u_char forwarded_pkt = 0, just_check = 0;
  983. const u_char *pk;
  984. pkt_copy(&rpkt_local_copy, &rpkt);
  985. ntop = xstrdup(inet_to_str(rpkt.from));
  986. req = (struct andna_reg_pkt *) rpkt_local_copy.msg;
  987. if (rpkt.hdr.sz != ANDNA_REG_PKT_SZ + MAX_IP_SZ) {
  988. debug(DBG_SOFT,
  989. ERROR_MSG "Malformed check_counter pkt from %s: %d != %d",
  990. ERROR_FUNC, ntop, rpkt.hdr.sz, ANDNA_REG_PKT_SZ + MAX_IP_SZ);
  991. ERROR_FINISH(ret, -1, finish);
  992. }
  993. if (rpkt.hdr.flags & BCAST_PKT)
  994. /* The pkt we received has been only forwarded to us */
  995. forwarded_pkt = 1;
  996. if (req->flags & ANDNA_PKT_JUST_CHECK)
  997. just_check = 1;
  998. /* Check if we already received this pkt during the flood */
  999. if (forwarded_pkt &&
  1000. andna_add_flood_pkt_id(last_counter_pkt_id, rpkt.hdr.id)) {
  1001. debug(DBG_INSANE, "Dropped 0x%0x andna pkt, we already "
  1002. "received it", rpkt.hdr.id);
  1003. ERROR_FINISH(ret, 0, finish);
  1004. }
  1005. /* Save the real sender of the request */
  1006. buf = rpkt.msg + ANDNA_REG_PKT_SZ;
  1007. inet_setip(&rfrom, (u_int *) buf, my_family);
  1008. rfrom_ntop = xstrdup(inet_to_str(rfrom));
  1009. debug(DBG_SOFT, "Received %s%sfrom %s, rfrom %s",
  1010. rq_to_str(rpkt.hdr.op), forwarded_pkt ? " (forwarded) " : " ",
  1011. ntop, rfrom_ntop);
  1012. memcpy(&pkt, &rpkt, sizeof(PACKET));
  1013. inet_copy(&pkt.from, &rfrom);
  1014. /* Reply to rfrom using a UDP sk, since the replies are very small */
  1015. pkt_addsk(&pkt, my_family, 0, SKT_UDP);
  1016. /* Verify the signature */
  1017. pk = (const u_char *) req->pubkey;
  1018. pubkey = get_rsa_pub(&pk, ANDNA_PKEY_LEN);
  1019. if (!pubkey || !verify_sign((u_char *) req, ANDNA_REG_SIGNED_BLOCK_SZ,
  1020. (u_char *) req->sign, ANDNA_SIGNATURE_LEN,
  1021. pubkey)) {
  1022. /* Bad signature */
  1023. debug(DBG_SOFT, "Invalid signature of the 0x%x check "
  1024. "counter request", rpkt.hdr.id);
  1025. if (!forwarded_pkt)
  1026. pkt_err(pkt, E_INVALID_SIGNATURE, 0);
  1027. ERROR_FINISH(ret, -1, finish);
  1028. }
  1029. /* Revert the packet from network to host order */
  1030. ints_network_to_host((void *) req, andna_reg_pkt_iinfo);
  1031. /* If don't belong to the gnode of `rfrom', then we have to exclude
  1032. * it from the find_hash_gnode search, since we have received the
  1033. * pkt from that gnode and it is likely that `rfrom' is the only node
  1034. * of that gnode.
  1035. */
  1036. excluded_hgnode[0] = ip_gids_cmp(rfrom, me.cur_ip, 1) ? rfrom.data : 0;
  1037. debug(DBG_INSANE, "excluded_hgnode: %x ", excluded_hgnode[0]);
  1038. /*
  1039. * Check if we are the real counter node or if we have to continue to
  1040. * forward the pkt
  1041. */
  1042. andna_hash(my_family, req->rip, MAX_IP_SZ, rip_hash, hash_gnode);
  1043. if ((err =
  1044. find_hash_gnode(hash_gnode, &to, excluded_hgnode, 1, 0)) < 0) {
  1045. debug(DBG_SOFT,
  1046. "We are not the real (rounded)hash_gnode. "
  1047. "Rejecting the 0x%x check_counter request", rpkt.hdr.id);
  1048. if (!forwarded_pkt)
  1049. pkt_err(pkt, E_ANDNA_WRONG_HASH_GNODE, 0);
  1050. ERROR_FINISH(ret, -1, finish);
  1051. } else if (err == 1) {
  1052. if (!(req->flags & ANDNA_PKT_FORWARD)) {
  1053. if (!forwarded_pkt)
  1054. pkt_err(pkt, E_ANDNA_WRONG_HASH_GNODE, 0);
  1055. ERROR_FINISH(ret, -1, finish);
  1056. }
  1057. /* Continue to forward the received pkt */
  1058. debug(DBG_SOFT, "The check_counter rq pkt will be forwarded "
  1059. "to: %s", inet_to_str(to));
  1060. ret = forward_pkt(rpkt, to);
  1061. goto finish;
  1062. }
  1063. /* Finally, let's register/update the hname */
  1064. cc = counter_c_add(&rfrom, req->pubkey);
  1065. if (!just_check)
  1066. cch = cc_hashes_add(cc, (int *) req->hash);
  1067. else
  1068. cch = cc_findhash(cc, (int *) req->hash);
  1069. if (!cch) {
  1070. debug(DBG_SOFT, "Request %s (0x%x) rejected: %s",
  1071. rq_to_str(rpkt.hdr.op), rpkt.hdr.id,
  1072. rq_strerror(E_ANDNA_TOO_MANY_HNAME));
  1073. if (!forwarded_pkt)
  1074. pkt_err(pkt, E_ANDNA_TOO_MANY_HNAME, 0);
  1075. ERROR_FINISH(ret, -1, finish);
  1076. }
  1077. old_updates = cch->hname_updates;
  1078. old_updates -= ! !just_check;
  1079. if (old_updates > req->hname_updates) {
  1080. debug(DBG_SOFT, "Request %s (0x%x) rejected: hname_updates"
  1081. " mismatch %d > %d", rq_to_str(rpkt.hdr.op), rpkt.hdr.id,
  1082. old_updates, req->hname_updates);
  1083. if (!forwarded_pkt)
  1084. pkt_err(pkt, E_ANDNA_HUPDATE_MISMATCH, 0);
  1085. ERROR_FINISH(ret, -1, finish);
  1086. } else if (!just_check) {
  1087. /* Touch the hname */
  1088. cch->hname_updates = req->hname_updates + 1;
  1089. cch->timestamp = time(0);
  1090. }
  1091. /* Report the successful result to rfrom */
  1092. if (!forwarded_pkt || just_check) {
  1093. debug(DBG_SOFT, "Check_counter rq 0x%x accepted.", rpkt.hdr.id);
  1094. pkt_fill_hdr(&pkt.hdr, ASYNC_REPLIED, rpkt.hdr.id,
  1095. ACK_AFFERMATIVE, 0);
  1096. pkt.msg = 0;
  1097. ret = forward_pkt(pkt, rfrom);
  1098. }
  1099. /*
  1100. * Broadcast the request to the entire gnode of level 1 to let the
  1101. * other counter_nodes register the hname.
  1102. */
  1103. if (!just_check) {
  1104. if (!forwarded_pkt)
  1105. andna_add_flood_pkt_id(last_counter_pkt_id, rpkt.hdr.id);
  1106. andna_flood_pkt(&rpkt, forwarded_pkt);
  1107. }
  1108. finish:
  1109. if (ntop)
  1110. xfree(ntop);
  1111. if (rfrom_ntop)
  1112. xfree(rfrom_ntop);
  1113. if (pubkey)
  1114. RSA_free(pubkey);
  1115. pkt_free(&rpkt_local_copy, 0);
  1116. return ret;
  1117. }
  1118. /*
  1119. *
  1120. * * * * Hostname/IP resolution * * *
  1121. *
  1122. */
  1123. /*
  1124. * andna_resolve_hash_locally
  1125. *
  1126. * It tries to resolve the given md5 hname-hash by searching in the
  1127. * local andna caches.
  1128. * It uses the same arguments of `andna_resolve_hash' see below).
  1129. */
  1130. snsd_service *
  1131. andna_resolve_hash_locally(u_int hname_hash[MAX_IP_INT], int service,
  1132. u_char proto, int *records)
  1133. {
  1134. struct andna_resolve_rq_pkt req;
  1135. lcl_cache *lcl;
  1136. rh_cache *rhc;
  1137. andna_cache *ac;
  1138. snsd_service *ret;
  1139. u_int hash;
  1140. setzero(&req, sizeof(req));
  1141. hash = fnv_32_buf(hname_hash, ANDNA_HASH_SZ, FNV1_32_INIT);
  1142. #ifndef ANDNA_DEBUG
  1143. /*
  1144. * Search the hostname in the local cache first. Maybe we are so
  1145. * dumb that we are trying to resolve the same ip we registered.
  1146. */
  1147. if ((lcl = lcl_cache_find_hash(andna_lcl, hash))) {
  1148. u_short fake_counter = 0;
  1149. *records = lcl->snsd_counter;
  1150. ret = snsd_service_llist_copy(lcl->service, service, proto);
  1151. /* Add our current main ip */
  1152. if (service == SNSD_ALL_SERVICE ||
  1153. service == SNSD_DEFAULT_SERVICE || !ret)
  1154. snsd_add_mainip(&ret, &fake_counter,
  1155. SNSD_MAX_RECORDS,
  1156. (uintptr_t*)me.cur_ip.data);
  1157. return ret;
  1158. }
  1159. /*
  1160. * Last try before asking to ANDNA: let's see if we have it in
  1161. * the resolved_hnames cache
  1162. */
  1163. if ((rhc = rh_cache_find_hash(hash))) {
  1164. *records = rhc->snsd_counter;
  1165. ret = snsd_service_llist_copy(rhc->service, service, proto);
  1166. if (!ret && (service != SNSD_ALL_SERVICE) &&
  1167. (service != SNSD_DEFAULT_SERVICE))
  1168. /* The specific service hasn't been found, fallback to
  1169. * SNSD_DEFAULT_SERVICE */
  1170. ret = snsd_service_llist_copy(rhc->service,
  1171. SNSD_DEFAULT_SERVICE, 0);
  1172. if (ret)
  1173. return ret;
  1174. }
  1175. #endif
  1176. /*
  1177. * If we manage an andna_cache, it's better to peek at it.
  1178. */
  1179. if ((ac = andna_cache_gethash((int *) hname_hash))) {
  1180. *records = ac->acq->snsd_counter;
  1181. ret = snsd_service_llist_copy(ac->acq->service, service, proto);
  1182. if (!ret && (service != SNSD_ALL_SERVICE) &&
  1183. (service != SNSD_DEFAULT_SERVICE))
  1184. /* The specific service hasn't been found, fallback to
  1185. * SNSD_DEFAULT_SERVICE */
  1186. ret = snsd_service_llist_copy(ac->acq->service,
  1187. SNSD_DEFAULT_SERVICE, 0);
  1188. if (ret)
  1189. return ret;
  1190. }
  1191. return 0;
  1192. }
  1193. /*
  1194. * andna_resolve_hash
  1195. *
  1196. * It returns a snsd_service llist (see snsd.h) which contains the snsd
  1197. * records of the resolved hostname. Among them there's at least the mainip
  1198. * record which can be found using snsd_find_mainip().
  1199. *
  1200. * `hname_hash' is the full MD5 hash of the hostname we want to resolve.
  1201. *
  1202. * `service' specifies the service number of the resolution. If it is equal to
  1203. * -1 the resolution will return all the registered snds records.
  1204. *
  1205. * `proto' is the protocol of the `service', it must be specified in the
  1206. * proto_to_8bit() format.
  1207. *
  1208. * In `*records' the number of records stored in the returned snsd_service
  1209. * llist is written.
  1210. *
  1211. * It returns 0 on error
  1212. */
  1213. snsd_service *
  1214. andna_resolve_hash(u_int hname_hash[MAX_IP_INT], int service,
  1215. u_char proto, int *records)
  1216. {
  1217. PACKET pkt, rpkt;
  1218. struct andna_resolve_rq_pkt req;
  1219. struct andna_resolve_reply_pkt *reply;
  1220. rh_cache *rhc;
  1221. snsd_service *sns_dup;
  1222. u_int hash_gnode[MAX_IP_INT], hash32;
  1223. inet_prefix to;
  1224. snsd_service *sns, *snsd_unpacked, *ret = 0;
  1225. const char *ntop;
  1226. char *snsd_packed;
  1227. u_short snsd_counter = 0;
  1228. size_t packed_sz, unpacked_sz;
  1229. ssize_t err;
  1230. *records = 0;
  1231. setzero(&req, sizeof(req));
  1232. setzero(&pkt, sizeof(pkt));
  1233. setzero(&rpkt, sizeof(pkt));
  1234. hash32 =
  1235. fnv_32_buf((u_char *) hname_hash, ANDNA_HASH_SZ, FNV1_32_INIT);
  1236. /* Try to resolve the hostname locally */
  1237. sns = andna_resolve_hash_locally(hname_hash, service, proto, records);
  1238. if (sns)
  1239. return sns;
  1240. /*
  1241. * Fill the request structure.
  1242. */
  1243. req.service = service;
  1244. req.proto = proto;
  1245. inet_copy_ipdata(req.rip, &me.cur_ip);
  1246. memcpy(req.hash, hname_hash, ANDNA_HASH_SZ);
  1247. andna_hash_by_family(my_family, hname_hash, hash_gnode);
  1248. /*
  1249. * Ok, we have to ask to someone for the resolution.
  1250. * Let's see to whom we have to send the pkt
  1251. */
  1252. if ((err = find_hash_gnode(hash_gnode, &to, 0, 0, 1)) < 0)
  1253. ERROR_FINISH(ret, 0, finish);
  1254. else if (err == 1)
  1255. req.flags |= ANDNA_PKT_FORWARD;
  1256. ntop = inet_to_str(to);
  1257. debug(DBG_INSANE, "Quest %s to %s", rq_to_str(ANDNA_RESOLVE_HNAME),
  1258. ntop);
  1259. /*
  1260. * Fill the packet and send the request
  1261. */
  1262. /* host -> network order */
  1263. ints_host_to_network(&req, andna_resolve_rq_pkt_iinfo);
  1264. pkt_addto(&pkt, &to);
  1265. pkt.pkt_flags |= PKT_SET_LOWDELAY;
  1266. pkt.hdr.flags |= ASYNC_REPLY;
  1267. pkt.hdr.sz = ANDNA_RESOLVE_RQ_PKT_SZ;
  1268. pkt.msg = xmalloc(pkt.hdr.sz);
  1269. memcpy(pkt.msg, &req, pkt.hdr.sz);
  1270. setzero(&rpkt, sizeof(PACKET));
  1271. err =
  1272. send_rq(&pkt, 0, ANDNA_RESOLVE_HNAME, 0, ANDNA_RESOLVE_REPLY, 1,
  1273. &rpkt);
  1274. if (err < 0) {
  1275. debug(DBG_NORMAL, ERROR_MSG "Resolution of 0x%x failed.",
  1276. ERROR_FUNC, pkt.hdr.id);
  1277. ERROR_FINISH(ret, 0, finish);
  1278. }
  1279. if (rpkt.hdr.sz <= ANDNA_RESOLVE_REPLY_PKT_SZ ||
  1280. rpkt.hdr.sz > (SNSD_SERVICE_MAX_PACK_SZ +
  1281. ANDNA_RESOLVE_REPLY_PKT_SZ))
  1282. ERROR_FINISH(ret, 0, finish);
  1283. /*
  1284. * Take the ip we need from the replied pkt
  1285. */
  1286. reply = (struct andna_resolve_reply_pkt *) rpkt.msg;
  1287. /* network -> host order */
  1288. ints_network_to_host(reply, andna_resolve_reply_pkt_iinfo);
  1289. /* Unpack the received snsd records */
  1290. snsd_packed = rpkt.msg + sizeof(struct andna_resolve_reply_pkt);
  1291. packed_sz = rpkt.hdr.sz - sizeof(struct andna_resolve_reply_pkt);
  1292. if (service == -1)
  1293. snsd_unpacked = snsd_unpack_all_service(snsd_packed, packed_sz,
  1294. &unpacked_sz,
  1295. &snsd_counter);
  1296. else
  1297. snsd_unpacked = snsd_unpack_service(snsd_packed, packed_sz,
  1298. &unpacked_sz, &snsd_counter);
  1299. if (!snsd_unpacked ||
  1300. ((service == SNSD_ALL_SERVICE ||
  1301. service == SNSD_DEFAULT_SERVICE) &&
  1302. !snsd_find_mainip(snsd_unpacked))) {
  1303. debug(DBG_SOFT, ERROR_MSG "Malformed resolution reply (0x%x)",
  1304. ERROR_FUNC, rpkt.hdr.id);
  1305. ERROR_FINISH(ret, 0, finish);
  1306. }
  1307. *records = snsd_counter;
  1308. ret = snsd_unpacked;
  1309. /*
  1310. * Add the hostname in the resolved_hnames cache since it was
  1311. * successful resolved ;)
  1312. */
  1313. reply->timestamp = time(0) - reply->timestamp;
  1314. rhc = rh_cache_add_hash(hash32, reply->timestamp);
  1315. sns_dup = snsd_service_llist_copy(snsd_unpacked, SNSD_ALL_SERVICE, 0);
  1316. snsd_service_llist_merge(&rhc->service, &rhc->snsd_counter, sns_dup);
  1317. finish:
  1318. pkt_free(&pkt, 1);
  1319. pkt_free(&rpkt, 0);
  1320. return ret;
  1321. }
  1322. /*
  1323. * andna_resolve_hname
  1324. *
  1325. * It is a wrapper to andna_resolve_hash() (see above): it hashes the given
  1326. * `hname' string and calls andna_resolve_hash.
  1327. *
  1328. * It returns 0 on error
  1329. */
  1330. snsd_service *
  1331. andna_resolve_hname(char *hname, int service, u_char proto, int *records)
  1332. {
  1333. u_int hname_hash[MAX_IP_INT];
  1334. hash_md5((u_char *) hname, strlen(hname), (u_char *) hname_hash);
  1335. return andna_resolve_hash(hname_hash, service, proto, records);
  1336. }
  1337. /*
  1338. * andna_recv_resolve_rq
  1339. *
  1340. * replies to a hostname resolve request by giving the snsd records associated
  1341. * to the hostname.
  1342. */
  1343. int
  1344. andna_recv_resolve_rq(PACKET rpkt)
  1345. {
  1346. PACKET pkt, rpkt_local_copy;
  1347. struct andna_resolve_rq_pkt *req;
  1348. struct andna_resolve_reply_pkt reply;
  1349. andna_cache *ac;
  1350. snsd_service *sns;
  1351. u_int hash_gnode[MAX_IP_INT];
  1352. inet_prefix rfrom, to;
  1353. u_short service;
  1354. size_t pack_sz;
  1355. int ret = 0, err;
  1356. char *ntop = 0, *rfrom_ntop = 0, *buf;
  1357. u_char spread_the_acache = 0;
  1358. if (rpkt.hdr.sz != ANDNA_RESOLVE_RQ_PKT_SZ)
  1359. ERROR_FINISH(ret, -1, finish);
  1360. pkt_copy(&rpkt_local_copy, &rpkt);
  1361. req = (struct andna_resolve_rq_pkt *) rpkt_local_copy.msg;
  1362. /* Save the real sender of the request */
  1363. inet_setip(&rfrom, req->rip, my_family);
  1364. ntop = xstrdup(inet_to_str(rpkt.from));
  1365. rfrom_ntop = xstrdup(inet_to_str(rfrom));
  1366. debug(DBG_NOISE, "Andna Resolve request 0x%x from: %s, real from: %s",
  1367. rpkt.hdr.id, ntop, rfrom_ntop);
  1368. memcpy(&pkt, &rpkt, sizeof(PACKET));
  1369. inet_copy(&pkt.from, &rfrom);
  1370. pkt_addsk(&pkt, my_family, 0, SKT_UDP);
  1371. pkt_addcompress(&pkt);
  1372. pkt_addlowdelay(&pkt);
  1373. pkt_addlowdelay(&rpkt);
  1374. /* network -> host order conversion of the rpkt_local_copy.smg */
  1375. ints_network_to_host((void *) req, andna_resolve_rq_pkt_iinfo);
  1376. /*
  1377. * Are we the right hash_gnode or have we to still forward the pkt ?
  1378. */
  1379. andna_hash_by_family(my_family, (u_char *) req->hash, hash_gnode);
  1380. if ((err = find_hash_gnode(hash_gnode, &to, 0, 0, 0)) < 0) {
  1381. debug(DBG_SOFT, "We are not the real (rounded)hash_gnode. "
  1382. "Rejecting the 0x%x resolve_hname request", rpkt.hdr.id);
  1383. pkt_err(pkt, E_ANDNA_WRONG_HASH_GNODE, 0);
  1384. ERROR_FINISH(ret, -1, finish);
  1385. } else if (err == 1) {
  1386. if (!(req->flags & ANDNA_PKT_FORWARD)) {
  1387. pkt_err(pkt, E_ANDNA_WRONG_HASH_GNODE, 0);
  1388. ERROR_FINISH(ret, -1, finish);
  1389. }
  1390. /* Continue to forward the received pkt */
  1391. debug(DBG_SOFT, "The resolve_hame rq pkt will be forwarded "
  1392. "to: %s", inet_to_str(to));
  1393. ret = forward_pkt(rpkt, to);
  1394. goto finish;
  1395. }
  1396. /*
  1397. * Search the hostname to resolve in the andna_cache
  1398. */
  1399. if (!(ac = andna_cache_gethash((int *) req->hash))) {
  1400. /* We don't have that hname in our andna_cache */
  1401. if (time(0) - me.uptime < (ANDNA_EXPIRATION_TIME / 2)) {
  1402. /*
  1403. * We are a new hash_gnode, let's see if there is
  1404. * an old hash_gnode which has this hostname.
  1405. */
  1406. if ((ac = get_single_andna_c(req->hash, hash_gnode))) {
  1407. /*
  1408. * We got the andna_cache from the old
  1409. * hash_gnode. Save it in our andna_cache, then
  1410. * reply to `rfrom' and diffuse it in our gnode
  1411. */
  1412. clist_add(&andna_c, &andna_c_counter, ac);
  1413. spread_the_acache = 1;
  1414. goto reply_resolve_rq;
  1415. }
  1416. }
  1417. /* Nothing to do, there isn't it, bye. */
  1418. debug(DBG_SOFT, "Request %s (0x%x) rejected: %s",
  1419. rq_to_str(rpkt.hdr.op), rpkt.hdr.id,
  1420. rq_strerror(E_ANDNA_NO_HNAME));
  1421. pkt_err(pkt, E_ANDNA_NO_HNAME, 0);
  1422. ERROR_FINISH(ret, -1, finish);
  1423. }
  1424. reply_resolve_rq:
  1425. /*
  1426. * Send back the ip associated to the hname
  1427. */
  1428. debug(DBG_SOFT, "Resolve request 0x%x accepted", rpkt.hdr.id);
  1429. /* Write the reply */
  1430. setzero(&reply, sizeof(reply));
  1431. reply.timestamp = time(0) - ac->acq->timestamp;
  1432. /* host -> network order */
  1433. ints_host_to_network((void *) &reply, andna_resolve_reply_pkt_iinfo);
  1434. pack_sz = sizeof(reply);
  1435. pack_sz += req->service == SNSD_ALL_SERVICE ?
  1436. SNSD_SERVICE_LLIST_PACK_SZ(ac->acq->service) :
  1437. SNSD_SERVICE_SINGLE_PACK_SZ(ac->acq->service);
  1438. pkt_fill_hdr(&pkt.hdr, ASYNC_REPLIED, rpkt.hdr.id, ANDNA_RESOLVE_REPLY,
  1439. pack_sz);
  1440. pkt.msg = buf = xmalloc(pkt.hdr.sz);
  1441. memcpy(pkt.msg, &reply, sizeof(reply));
  1442. buf += sizeof(reply);
  1443. pack_sz -= sizeof(reply);
  1444. if (req->service == SNSD_ALL_SERVICE)
  1445. /* Pack all the registered snsd records */
  1446. ret = snsd_pack_all_services(buf, pack_sz, ac->acq->service);
  1447. else {
  1448. /* Pack the snsd records of the specified service number */
  1449. service = (u_short) req->service;
  1450. sns = snsd_find_service(ac->acq->service, service, req->proto);
  1451. if (!sns && service != SNSD_DEFAULT_SERVICE) {
  1452. /*
  1453. * The specified service and proto record hasn't been
  1454. * found, fallback to SNSD_DEFAULT_SERVICE
  1455. */
  1456. sns = snsd_find_service(ac->acq->service,
  1457. SNSD_DEFAULT_SERVICE, 0);
  1458. }
  1459. /* pack what we found */
  1460. ret = snsd_pack_service(buf, pack_sz, sns);
  1461. }
  1462. if (ret < 0) {
  1463. debug(DBG_NORMAL, "Cannot pack the services for the 0x%x "
  1464. "resolve request", rpkt.hdr.id);
  1465. goto finish;
  1466. }
  1467. /* Forward it */
  1468. ret = forward_pkt(pkt, rfrom);
  1469. pkt_free(&pkt, 0);
  1470. if (spread_the_acache) {
  1471. /* Spread the received andna_cache in our gnode */
  1472. spread_single_acache(req->hash);
  1473. }
  1474. finish:
  1475. if (ntop)
  1476. xfree(ntop);
  1477. if (rfrom_ntop)
  1478. xfree(rfrom_ntop);
  1479. pkt_free(&rpkt_local_copy, 0);
  1480. return ret;
  1481. }
  1482. /*
  1483. * andna_reverse_resolve
  1484. *
  1485. * It returns the copy of the local cache of a remote node.
  1486. * The ip of the node is specified in `ip'.
  1487. *
  1488. * On error 0 is returned.
  1489. */
  1490. lcl_cache *
  1491. andna_reverse_resolve(inet_prefix ip)
  1492. {
  1493. PACKET pkt, rpkt;
  1494. lcl_cache *unpacked_lcl = 0, *ret = 0;
  1495. inet_prefix to;
  1496. const char *ntop;
  1497. int tmp_counter;
  1498. ssize_t err;
  1499. setzero(&pkt, sizeof(PACKET));
  1500. setzero(&rpkt, sizeof(PACKET));
  1501. inet_copy(&to, &ip);
  1502. ntop = inet_to_str(to);
  1503. debug(DBG_INSANE, "Quest %s to %s", rq_to_str(ANDNA_RESOLVE_IP), ntop);
  1504. /* We have been asked to reverse resolve our same IP */
  1505. if (!memcmp(to.data, me.cur_ip.data, MAX_IP_SZ) ||
  1506. LOOPBACK(htonl(to.data[0])))
  1507. return lcl_get_registered_hnames(andna_lcl);
  1508. /*
  1509. * Fill the packet and send the request
  1510. */
  1511. pkt_addto(&pkt, &to);
  1512. pkt_addfrom(&rpkt, &to);
  1513. pkt_addtimeout(&pkt, ANDNA_REV_RESOLVE_RQ_TIMEOUT, 1, 1);
  1514. err =
  1515. send_rq(&pkt, 0, ANDNA_RESOLVE_IP, 0, ANDNA_REV_RESOLVE_REPLY, 1,
  1516. &rpkt);
  1517. if (err < 0) {
  1518. error(ERROR_MSG "Reverse resolution of the %s "
  1519. "ip failed.", ERROR_FUNC, inet_to_str(to));
  1520. ERROR_FINISH(ret, 0, finish);
  1521. }
  1522. unpacked_lcl = unpack_lcl_cache(rpkt.msg, rpkt.hdr.sz, &tmp_counter);
  1523. if (tmp_counter > ANDNA_MAX_HOSTNAMES || tmp_counter < 0)
  1524. ERROR_FINISH(ret, 0, finish);
  1525. ret = unpacked_lcl;
  1526. finish:
  1527. pkt_free(&pkt, 1);
  1528. pkt_free(&rpkt, 1);
  1529. return ret;
  1530. }
  1531. /*
  1532. * andna_recv_rev_resolve_rq
  1533. *
  1534. * It replies to a reverse hostname resolve request which asks all the
  1535. * hostnames associated with a given ip.
  1536. */
  1537. int
  1538. andna_recv_rev_resolve_rq(PACKET rpkt)
  1539. {
  1540. PACKET pkt;
  1541. const char *ntop;
  1542. int ret = 0, err;
  1543. lcl_cache *alcl = andna_lcl;
  1544. setzero(&pkt, sizeof(PACKET));
  1545. ntop = inet_to_str(rpkt.from);
  1546. debug(DBG_INSANE,
  1547. "Andna reverse resolve request received 0x%x from %s",
  1548. rpkt.hdr.id, ntop);
  1549. /*
  1550. * Build the reply pkt
  1551. */
  1552. pkt_fill_hdr(&pkt.hdr, 0, rpkt.hdr.id, ANDNA_REV_RESOLVE_REPLY, 0);
  1553. /* Build the list of registered hnames */
  1554. if (!(pkt.msg = pack_lcl_cache(alcl, &pkt.hdr.sz))) {
  1555. pkt_err(rpkt, E_ANDNA_NO_HNAME, 0);
  1556. ERROR_FINISH(ret, -1, finish);
  1557. }
  1558. debug(DBG_INSANE, "Reverse resolve request 0x%x accepted",
  1559. rpkt.hdr.id);
  1560. /*
  1561. * Send it.
  1562. */
  1563. pkt_addto(&pkt, &rpkt.from);
  1564. pkt_addsk(&pkt, my_family, rpkt.sk, rpkt.sk_type);
  1565. pkt_addcompress(&pkt);
  1566. err = send_rq(&pkt, 0, ANDNA_REV_RESOLVE_REPLY, rpkt.hdr.id, 0, 0, 0);
  1567. if (err < 0)
  1568. ERROR_FINISH(ret, -1, finish);
  1569. finish:
  1570. pkt_free(&pkt, 0);
  1571. return ret;
  1572. }
  1573. /*
  1574. *
  1575. * * * * Andna caches transfer * * *
  1576. *
  1577. */
  1578. /*
  1579. * get_single_andna_c
  1580. *
  1581. * It sends the ANDNA_GET_SINGLE_ACACHE request to the old `hash_gnode' of `hash'
  1582. * to retrieve the andna_cache that contains the information about `hash'.
  1583. */
  1584. andna_cache *
  1585. get_single_andna_c(u_int hash[MAX_IP_INT], u_int hash_gnode[MAX_IP_INT])
  1586. {
  1587. PACKET pkt, rpkt;
  1588. struct single_acache_hdr req_hdr;
  1589. andna_cache *ret = 0;
  1590. u_int *new_hgnodes[1];
  1591. size_t pack_sz;
  1592. int err, counter;
  1593. const char *ntop;
  1594. char *pack;
  1595. setzero(&pkt, sizeof(PACKET));
  1596. setzero(&rpkt, sizeof(PACKET));
  1597. setzero(&req_hdr, sizeof(struct single_acache_hdr));
  1598. /*
  1599. * Find the old hash_gnode that corresponds to the hash `hash_gnode',
  1600. * but exclude from the search ourself 'cause we are a new hash_gnode
  1601. * and we are trying to reach the new one.
  1602. */
  1603. new_hgnodes[0] = me.cur_ip.data;
  1604. if ((err =
  1605. find_hash_gnode(hash_gnode, &pkt.to, new_hgnodes, 1, 1)) < 0) {
  1606. debug(DBG_SOFT, "get_single_andna_c: old hash_gnode not found");
  1607. ERROR_FINISH(ret, 0, finish);
  1608. } else if (err == 1)
  1609. req_hdr.flags |= ANDNA_PKT_FORWARD;
  1610. req_hdr.hgnodes = 1;
  1611. inet_copy_ipdata(req_hdr.rip, &me.cur_ip);
  1612. memcpy(req_hdr.hash, hash, MAX_IP_SZ);
  1613. /* host -> network order */
  1614. ints_host_to_network((void *) &req_hdr, single_acache_hdr_iinfo);
  1615. /* Pack the request */
  1616. pkt.hdr.flags |= ASYNC_REPLY;
  1617. pkt.hdr.sz = SINGLE_ACACHE_PKT_SZ(1);
  1618. pkt.msg = xmalloc(pkt.hdr.sz);
  1619. memcpy(pkt.msg, &req_hdr, pkt.hdr.sz);
  1620. /* Append our ip at the end of the pkt */
  1621. inet_copy_ipdata((u_int *) (pkt.msg +
  1622. sizeof(struct single_acache_hdr)),
  1623. &me.cur_ip);
  1624. ntop = inet_to_str(pkt.to);
  1625. debug(DBG_INSANE, "Quest %s to %s", rq_to_str(ANDNA_GET_SINGLE_ACACHE),
  1626. ntop);
  1627. err =
  1628. send_rq(&pkt, 0, ANDNA_GET_SINGLE_ACACHE, 0, ACK_AFFERMATIVE, 1,
  1629. &rpkt);
  1630. if (err < 0)
  1631. ERROR_FINISH(ret, 0, finish);
  1632. /* Unpack the waited reply */
  1633. pack_sz = rpkt.hdr.sz;
  1634. pack = rpkt.msg;
  1635. ret = unpack_andna_cache(pack, pack_sz, &counter, ACACHE_PACK_PKT);
  1636. if (!ret && counter < 0) {
  1637. error("get_single_acache(): Malformed andna_cache.");
  1638. ERROR_FINISH(ret, 0, finish);
  1639. }
  1640. finish:
  1641. pkt_free(&pkt, 1);
  1642. pkt_free(&rpkt, 0);
  1643. return ret;
  1644. }
  1645. /*
  1646. * put_single_acache
  1647. *
  1648. * It replies to a get_single_acache request as described
  1649. * in andna.h (near the single_acache_hdr struct).
  1650. */
  1651. int
  1652. put_single_acache(PACKET rpkt)
  1653. {
  1654. PACKET pkt, rpkt_local_copy;
  1655. struct single_acache_hdr *req_hdr;
  1656. u_int hash_gnode[MAX_IP_INT], **new_hgnodes = 0;
  1657. inet_prefix rfrom, to;
  1658. andna_cache *ac, *ac_tmp = 0;
  1659. char *buf;
  1660. char *ntop = 0, *rfrom_ntop = 0;
  1661. int ret = 0, i;
  1662. ssize_t err = 0;
  1663. size_t pkt_sz = 0;
  1664. pkt_copy(&rpkt_local_copy, &rpkt);
  1665. req_hdr = (struct single_acache_hdr *) rpkt_local_copy.msg;
  1666. /* Save the real sender of the request */
  1667. inet_setip(&rfrom, req_hdr->rip, my_family);
  1668. ntop = xstrdup(inet_to_str(rpkt.from));
  1669. rfrom_ntop = xstrdup(inet_to_str(rfrom));
  1670. debug(DBG_NOISE, "Andna get single cache request 0x%x from: %s, real "
  1671. "from: %s", rpkt.hdr.id, ntop, rfrom_ntop);
  1672. memcpy(&pkt, &rpkt, sizeof(PACKET));
  1673. inet_copy(&pkt.from, &rfrom);
  1674. pkt_addsk(&pkt, my_family, 0, SKT_UDP);
  1675. pkt_addcompress(&pkt);
  1676. /* network -> host order */
  1677. ints_network_to_host(req_hdr, single_acache_hdr_iinfo);
  1678. /* Verify the validity of the header */
  1679. if (rpkt.hdr.sz != SINGLE_ACACHE_PKT_SZ(req_hdr->hgnodes) ||
  1680. req_hdr->hgnodes > ANDNA_MAX_NEW_GNODES) {
  1681. debug(DBG_NOISE, "Malformed GET_SINGLE_ACACHE pkt: %d, %d, %d",
  1682. rpkt.hdr.sz, SINGLE_ACACHE_PKT_SZ(req_hdr->hgnodes),
  1683. req_hdr->hgnodes);
  1684. pkt_err(pkt, E_INVALID_REQUEST, 0);
  1685. ERROR_FINISH(ret, -1, finish);
  1686. }
  1687. /* Unpack the hash_gnodes to exclude */
  1688. new_hgnodes = xmalloc(sizeof(u_int *) * (req_hdr->hgnodes + 1));
  1689. buf = rpkt.msg + sizeof(struct single_acache_hdr);
  1690. for (i = 0; i < req_hdr->hgnodes; i++) {
  1691. new_hgnodes[i] = (u_int *) buf;
  1692. inet_ntohl(new_hgnodes[i], me.cur_ip.family);
  1693. buf += MAX_IP_SZ;
  1694. }
  1695. /*
  1696. * Check if we are the destined old hash_gnode or if we have to still
  1697. * forward the pkt
  1698. */
  1699. andna_hash_by_family(my_family, (u_char *) req_hdr->hash, hash_gnode);
  1700. if ((err = find_hash_gnode(hash_gnode, &to, new_hgnodes,
  1701. req_hdr->hgnodes, 0)) < 0) {
  1702. debug(DBG_SOFT, "We are not the real (rounded)hash_gnode. "
  1703. "Rejecting the 0x%x get_single_acache request", rpkt.hdr.id);
  1704. pkt_err(pkt, E_ANDNA_WRONG_HASH_GNODE, 0);
  1705. ERROR_FINISH(ret, -1, finish);
  1706. } else if (err == 1) {
  1707. /* Continue to forward the received pkt */
  1708. debug(DBG_SOFT, "The 0x%x rq pkt will be forwarded "
  1709. "to: %s", rpkt.hdr.id, inet_to_str(to));
  1710. ret = forward_pkt(rpkt, to);
  1711. goto finish;
  1712. }
  1713. /*
  1714. * Search in our andna_cache if we have what `rfrom' wants.
  1715. */
  1716. if (!(ac = andna_cache_gethash((int *) req_hdr->hash))) {
  1717. /*
  1718. * Nothing found! Maybe it's because we have an uptime less than
  1719. * (ANDNA_EXPIRATION_TIME/2) and so we are a new hash_gnode,
  1720. * therefore we have to forward the pkt to an older hash_gnode.
  1721. */
  1722. if (time(0) - me.uptime < (ANDNA_EXPIRATION_TIME / 2)) {
  1723. new_hgnodes[req_hdr->hgnodes] = me.cur_ip.data;
  1724. if ((err = find_hash_gnode(hash_gnode, &to, new_hgnodes,
  1725. req_hdr->hgnodes + 1, 1)) < 0) {
  1726. /* An older hash_gnode doesn't exist */
  1727. debug(DBG_SOFT,
  1728. "put_single_andna_c: old hash_gnode not found");
  1729. ERROR_FINISH(ret, -1, finish);
  1730. }
  1731. /*
  1732. * Append our ip at the end of the pkt and forward it
  1733. * to the older hash_gnode.
  1734. */
  1735. memcpy(&pkt, &rpkt, sizeof(PACKET));
  1736. pkt.sk = 0;
  1737. pkt.hdr.sz = rpkt.hdr.sz + MAX_IP_SZ;
  1738. pkt.msg = xmalloc(pkt.hdr.sz);
  1739. memcpy(pkt.msg, rpkt.msg, rpkt.hdr.sz);
  1740. inet_copy_ipdata((u_int *) (pkt.msg + rpkt.hdr.sz),
  1741. &me.cur_ip);
  1742. debug(DBG_SOFT, "The 0x%x rq pkt will be forwarded "
  1743. "to an older hgnode: %s", rpkt.hdr.id, inet_to_str(to));
  1744. ret = forward_pkt(pkt, to);
  1745. pkt_free(&pkt, 1);
  1746. goto finish;
  1747. }
  1748. /*
  1749. * We are neither a new hash_gnode so the
  1750. * get_andna_single_cache propagation ends here and we reply to
  1751. * `rfrom': <<That andna_cache doesn't exist, bye bye!>>
  1752. */
  1753. debug(DBG_SOFT, "Request %s (0x%x) rejected: %s",
  1754. rq_to_str(rpkt.hdr.op), rpkt.hdr.id,
  1755. rq_strerror(E_ANDNA_NO_HNAME));
  1756. pkt_err(pkt, E_ANDNA_NO_HNAME, 0);
  1757. ERROR_FINISH(ret, -1, finish);
  1758. }
  1759. /*
  1760. * We found the andna_cache! Let's reply.
  1761. * Pack the `ac' andna_cache and send it to `rfrom'
  1762. */
  1763. setzero(&pkt, sizeof(PACKET));
  1764. pkt_fill_hdr(&pkt.hdr, ASYNC_REPLIED, rpkt.hdr.id, ACK_AFFERMATIVE, 0);
  1765. pkt_addto(&pkt, &rfrom);
  1766. pkt_addsk(&pkt, my_family, 0, SKT_TCP);
  1767. pkt_addport(&pkt, rpkt.port);
  1768. pkt_addcompress(&pkt);
  1769. /* Exctract the `ac' cache from the llist, so we can pack it alone */
  1770. ac_tmp = list_dup(ac);
  1771. pkt.msg = pack_andna_cache(ac_tmp, &pkt_sz, ACACHE_PACK_PKT);
  1772. pkt.hdr.sz = pkt_sz;
  1773. debug(DBG_INSANE, "Reply put_single_acache to %s", ntop);
  1774. err = send_rq(&pkt, 0, ACK_AFFERMATIVE, rpkt.hdr.id, 0, 0, 0);
  1775. pkt_free(&pkt, 0);
  1776. if (err < 0) {
  1777. error("put_andna_cache(): Cannot send the put_single_acache "
  1778. "reply to %s.", ntop);
  1779. ERROR_FINISH(ret, -1, finish);
  1780. }
  1781. finish:
  1782. if (ac_tmp)
  1783. xfree(ac_tmp);
  1784. if (new_hgnodes)
  1785. xfree(new_hgnodes);
  1786. if (ntop)
  1787. xfree(ntop);
  1788. if (rfrom_ntop)
  1789. xfree(rfrom_ntop);
  1790. pkt_free(&rpkt_local_copy, 0);
  1791. return ret;
  1792. }
  1793. /*
  1794. * spread_single_acache
  1795. *
  1796. * It tells to all the rnodes to get the single andna_cache of `hash'
  1797. * using get_single_andna_c()
  1798. */
  1799. int
  1800. spread_single_acache(u_int hash[MAX_IP_INT])
  1801. {
  1802. PACKET pkt;
  1803. struct spread_acache_pkt req;
  1804. setzero(&pkt, sizeof(PACKET));
  1805. memcpy(req.hash, hash, MAX_IP_SZ);
  1806. ints_host_to_network(&req, spread_acache_pkt_info);
  1807. pkt_fill_hdr(&pkt.hdr, 0, 0, ANDNA_SPREAD_SACACHE,
  1808. SPREAD_ACACHE_PKT_SZ);
  1809. pkt.msg = xmalloc(pkt.hdr.sz);
  1810. memcpy(pkt.msg, &req, pkt.hdr.sz);
  1811. debug(DBG_NOISE, "Spreading the single andna_cache 0x%x", pkt.hdr.id);
  1812. andna_add_flood_pkt_id(last_spread_acache_pkt_id, pkt.hdr.id);
  1813. return andna_flood_pkt(&pkt, 0);
  1814. }
  1815. /*
  1816. * recv_spread_single_acache
  1817. *
  1818. * It receives and execute the ANDNA_SPREAD_SACACHE request.
  1819. */
  1820. int
  1821. recv_spread_single_acache(PACKET rpkt)
  1822. {
  1823. PACKET rpkt_local_copy;
  1824. struct spread_acache_pkt *req;
  1825. andna_cache *ac;
  1826. u_int hash_gnode[MAX_IP_INT];
  1827. int ret = 0;
  1828. pkt_copy(&rpkt_local_copy, &rpkt);
  1829. req = (struct spread_acache_pkt *) rpkt_local_copy.msg;
  1830. if (rpkt.hdr.sz != SPREAD_ACACHE_PKT_SZ)
  1831. ERROR_FINISH(ret, -1, finish);
  1832. /* network -> host order */
  1833. ints_network_to_host(req, spread_acache_pkt_info);
  1834. /* Check if we already received this pkt during the flood */
  1835. if (andna_add_flood_pkt_id(last_spread_acache_pkt_id, rpkt.hdr.id)) {
  1836. debug(DBG_INSANE, "Dropped 0x%0x andna pkt, we already "
  1837. "received it", rpkt.hdr.id);
  1838. ERROR_FINISH(ret, 0, finish);
  1839. }
  1840. if (time(0) - me.uptime > (ANDNA_EXPIRATION_TIME / 2) ||
  1841. (ac = andna_cache_findhash((int *) req->hash))) {
  1842. /* We don't need to get the andna_cache from an old
  1843. * hash_gnode, since we currently are one of them! */
  1844. debug(DBG_NOISE, "recv_spread_single_acache: We are an old "
  1845. "hash_gnode, dropping 0x%x", rpkt.hdr.id);
  1846. ERROR_FINISH(ret, -1, finish);
  1847. }
  1848. debug(DBG_NOISE, "Received the spreaded andna_cache 0x%x",
  1849. rpkt.hdr.id);
  1850. /*
  1851. * Do as the rpkt tells us to do: retrieve the andna_hash for
  1852. * req->hash from the old hash_gnode and store it.
  1853. */
  1854. andna_hash_by_family(my_family, (u_char *) req->hash, hash_gnode);
  1855. if ((ac = get_single_andna_c(req->hash, hash_gnode))) {
  1856. /* Save it in our andna_cache. */
  1857. clist_add(&andna_c, &andna_c_counter, ac);
  1858. } else {
  1859. debug(DBG_NOISE, "recv_spread_single_acache: (0x%x) "
  1860. "get_single_andna_c request failed", rpkt.hdr.id);
  1861. ERROR_FINISH(ret, -1, finish);
  1862. }
  1863. /* Continue to forward the pkt in the gnode */
  1864. andna_flood_pkt(&rpkt, 1);
  1865. finish:
  1866. pkt_free(&rpkt_local_copy, 0);
  1867. return ret;
  1868. }
  1869. /*
  1870. * get_andna_cache
  1871. *
  1872. * sends the ANDNA_GET_ANDNA_CACHE request to rnode `dst_rnode' to retrieve its
  1873. * andna_cache.
  1874. */
  1875. andna_cache *
  1876. get_andna_cache(map_node * dst_rnode, int *counter)
  1877. {
  1878. PACKET pkt, rpkt;
  1879. andna_cache *ret = 0;
  1880. size_t pack_sz;
  1881. int err;
  1882. char *pack;
  1883. setzero(&pkt, sizeof(PACKET));
  1884. setzero(&rpkt, sizeof(PACKET));
  1885. if (rnl_fill_rq(dst_rnode, &pkt) < 0)
  1886. ERROR_FINISH(ret, 0, finish);
  1887. if (server_opt.dbg_lvl) {
  1888. const char *ntop;
  1889. ntop = inet_to_str(pkt.to);
  1890. debug(DBG_INSANE, "Quest %s to %s",
  1891. rq_to_str(ANDNA_GET_ANDNA_CACHE), ntop);
  1892. }
  1893. pkt_addtimeout(&pkt, ANDNA_HOOK_TIMEOUT, 1, 1);
  1894. err = send_rq(&pkt, 0, ANDNA_GET_ANDNA_CACHE, 0,
  1895. ANDNA_PUT_ANDNA_CACHE, 1, &rpkt);
  1896. if (err < 0) {
  1897. ret = 0;
  1898. goto finish;
  1899. }
  1900. pack_sz = rpkt.hdr.sz;
  1901. pack = rpkt.msg;
  1902. ret = unpack_andna_cache(pack, pack_sz, counter, ACACHE_PACK_PKT);
  1903. if (!ret && counter < 0)
  1904. error("get_andna_cache(): Malformed or empty andna_cache. "
  1905. "Cannot load it");
  1906. finish:
  1907. pkt_free(&pkt, 0);
  1908. pkt_free(&rpkt, 0);
  1909. return ret;
  1910. }
  1911. /*
  1912. * put_andna_cache
  1913. *
  1914. * replies to a ANDNA_GET_ANDNA_CACHE request, sending back the complete
  1915. * andna cache
  1916. */
  1917. int
  1918. put_andna_cache(PACKET rq_pkt)
  1919. {
  1920. PACKET pkt;
  1921. const char *ntop;
  1922. int ret = 0;
  1923. ssize_t err = 0;
  1924. size_t pkt_sz = 0;
  1925. ntop = inet_to_str(rq_pkt.from);
  1926. setzero(&pkt, sizeof(PACKET));
  1927. pkt_addto(&pkt, &rq_pkt.from);
  1928. pkt_addsk(&pkt, my_family, rq_pkt.sk, rq_pkt.sk_type);
  1929. pkt_addcompress(&pkt);
  1930. pkt.msg = pack_andna_cache(andna_c, &pkt_sz, ACACHE_PACK_PKT);
  1931. pkt.hdr.sz = pkt_sz;
  1932. debug(DBG_INSANE, "Reply %s to %s", re_to_str(ANDNA_PUT_ANDNA_CACHE),
  1933. ntop);
  1934. err = send_rq(&pkt, 0, ANDNA_PUT_ANDNA_CACHE, rq_pkt.hdr.id, 0, 0, 0);
  1935. if (err < 0) {
  1936. error
  1937. ("put_andna_cache(): Cannot send the ANDNA_PUT_ANDNA_CACHE reply to %s.",
  1938. ntop);
  1939. ERROR_FINISH(ret, -1, finish);
  1940. }
  1941. finish:
  1942. pkt_free(&pkt, 0);
  1943. return ret;
  1944. }
  1945. /*
  1946. * get_counter_cache
  1947. *
  1948. * sends the ANDNA_GET_COUNT_CACHE request to rnode `dst_rnode' to retrieve its
  1949. * counter_cache.
  1950. */
  1951. counter_c *
  1952. get_counter_cache(map_node * dst_rnode, int *counter)
  1953. {
  1954. PACKET pkt, rpkt;
  1955. counter_c *ccache, *ret = 0;
  1956. size_t pack_sz;
  1957. int err;
  1958. char *pack;
  1959. setzero(&pkt, sizeof(PACKET));
  1960. setzero(&rpkt, sizeof(PACKET));
  1961. if (rnl_fill_rq(dst_rnode, &pkt) < 0)
  1962. ERROR_FINISH(ret, 0, finish);
  1963. if (server_opt.dbg_lvl) {
  1964. const char *ntop;
  1965. ntop = inet_to_str(pkt.to);
  1966. debug(DBG_INSANE, "Quest %s to %s",
  1967. rq_to_str(ANDNA_GET_COUNT_CACHE), ntop);
  1968. }
  1969. pkt_addtimeout(&pkt, ANDNA_HOOK_TIMEOUT, 1, 1);
  1970. err = send_rq(&pkt, 0, ANDNA_GET_COUNT_CACHE, 0,
  1971. ANDNA_PUT_COUNT_CACHE, 1, &rpkt);
  1972. if (err < 0) {
  1973. ret = 0;
  1974. goto finish;
  1975. }
  1976. pack_sz = rpkt.hdr.sz;
  1977. pack = rpkt.msg;
  1978. ret = ccache = unpack_counter_cache(pack, pack_sz, counter);
  1979. if (!ccache && counter < 0)
  1980. error(ERROR_MSG "Malformed counter_cache. Cannot load it",
  1981. ERROR_FUNC);
  1982. finish:
  1983. pkt_free(&pkt, 0);
  1984. pkt_free(&rpkt, 0);
  1985. return ret;
  1986. }
  1987. /*
  1988. * put_counter_cache
  1989. *
  1990. * replies to a ANDNA_GET_COUNT_CACHE request, sending back the
  1991. * complete andna cache.
  1992. */
  1993. int
  1994. put_counter_cache(PACKET rq_pkt)
  1995. {
  1996. PACKET pkt;
  1997. const char *ntop;
  1998. int ret = 0;
  1999. ssize_t err = 0;
  2000. size_t pkt_sz = 0;
  2001. ntop = inet_to_str(rq_pkt.from);
  2002. setzero(&pkt, sizeof(PACKET));
  2003. pkt_addto(&pkt, &rq_pkt.from);
  2004. pkt_addsk(&pkt, my_family, rq_pkt.sk, rq_pkt.sk_type);
  2005. pkt_addcompress(&pkt);
  2006. pkt.msg = pack_counter_cache(andna_counter_c, &pkt_sz);
  2007. pkt.hdr.sz = pkt_sz;
  2008. debug(DBG_INSANE, "Reply %s to %s", re_to_str(ANDNA_PUT_COUNT_CACHE),
  2009. ntop);
  2010. err = send_rq(&pkt, 0, ANDNA_PUT_COUNT_CACHE, rq_pkt.hdr.id, 0, 0, 0);
  2011. if (err < 0) {
  2012. error
  2013. ("put_counter_cache(): Cannot send the ANDNA_PUT_COUNT_CACHE reply to %s.",
  2014. ntop);
  2015. ERROR_FINISH(ret, -1, finish);
  2016. }
  2017. finish:
  2018. pkt_free(&pkt, 0);
  2019. return ret;
  2020. }
  2021. /*
  2022. *
  2023. * * * * ANDNA threads * * *
  2024. *
  2025. */
  2026. /*
  2027. * andna_hook
  2028. *
  2029. * The andna_hook gets the andna_cache and the counter_node cache from
  2030. * the nearest rnodes.
  2031. */
  2032. void *
  2033. andna_hook(void *null)
  2034. {
  2035. inet_prefix to;
  2036. map_node *node;
  2037. int e = 0, i;
  2038. setzero(&to, sizeof(inet_prefix));
  2039. /* Block these requests */
  2040. op_filter_set(ANDNA_SPREAD_SACACHE);
  2041. loginfo("Starting the ANDNA hook.");
  2042. if (!me.cur_node->links) {
  2043. /* nothing to do */
  2044. debug(DBG_NORMAL, "There are no nodes, skipping the ANDNA hook.");
  2045. goto finish;
  2046. }
  2047. /*
  2048. * Send the GET_ANDNA_CACHE request to the nearest rnode we have, if it
  2049. * fails send it to the second rnode and so on...
  2050. */
  2051. for (i = 0, e = 0; i < me.cur_node->links; i++) {
  2052. node = (map_node *) me.cur_node->r_node[i].r_node;
  2053. if (!node || node->flags & MAP_ERNODE)
  2054. continue;
  2055. andna_c = get_andna_cache(node, &andna_c_counter);
  2056. if (andna_c) {
  2057. e = 1;
  2058. break;
  2059. }
  2060. }
  2061. if (!e)
  2062. loginfo
  2063. ("None of the rnodes in this area gave me the andna_cache.");
  2064. /*
  2065. * Send the GET_COUNT_CACHE request to the nearest rnode we have, if it
  2066. * fails send it to the second rnode and so on...
  2067. */
  2068. for (i = 0, e = 0; i < me.cur_node->links; i++) {
  2069. node = (map_node *) me.cur_node->r_node[i].r_node;
  2070. if (!node || node->flags & MAP_ERNODE)
  2071. continue;
  2072. andna_counter_c = get_counter_cache(node, &cc_counter);
  2073. if (andna_counter_c) {
  2074. e = 1;
  2075. break;
  2076. }
  2077. }
  2078. if (!e)
  2079. loginfo
  2080. ("None of the rnodes in this area gave me the counter_cache.");
  2081. finish:
  2082. /* Un-block these requests */
  2083. op_filter_clr(ANDNA_SPREAD_SACACHE);
  2084. loginfo("ANDNA hook completed");
  2085. return 0;
  2086. }
  2087. /*
  2088. * andna_min_update_retry
  2089. *
  2090. * waits ANDNA_MIN_UPDATE_TIME seconds and then updates `void_alcl'.
  2091. */
  2092. void *
  2093. andna_min_update_retry(void *void_alcl)
  2094. {
  2095. lcl_cache *alcl = (lcl_cache *) void_alcl;
  2096. int ret;
  2097. sleep(ANDNA_MIN_UPDATE_TIME);
  2098. ret = andna_register_hname(alcl, 0);
  2099. if (!ret)
  2100. loginfo("Hostname \"%s\" registered/updated "
  2101. "successfully", alcl->hostname);
  2102. return 0;
  2103. }
  2104. /*
  2105. * andna_update_hnames
  2106. *
  2107. * updates/registers all the hostnames present in the local cache.
  2108. * If `only_new_hname' is not zero, it registers only the new hostnames
  2109. * added in the local cache.
  2110. */
  2111. void
  2112. andna_update_hnames(int only_new_hname)
  2113. {
  2114. pthread_t thread;
  2115. lcl_cache *alcl = andna_lcl;
  2116. int ret, updates = 0;
  2117. list_for(alcl) {
  2118. if (only_new_hname && alcl->timestamp)
  2119. /* don't register old hnames */
  2120. continue;
  2121. ret = andna_register_hname(alcl, 0);
  2122. if (!ret) {
  2123. loginfo("Hostname \"%s\" registered/updated "
  2124. "successfully", alcl->hostname);
  2125. updates++;
  2126. } else {
  2127. /* Wait ANDNA_MIN_UPDATE_TIME seconds then retry to
  2128. * update it */
  2129. pthread_create(&thread, 0, andna_min_update_retry,
  2130. (void *) alcl);
  2131. pthread_detach(thread);
  2132. }
  2133. }
  2134. if (updates)
  2135. save_lcl_cache(andna_lcl, server_opt.lcl_file);
  2136. }
  2137. /*
  2138. * andna_maintain_hnames_active
  2139. *
  2140. * periodically registers and keep up to date the hostnames of the local cache.
  2141. */
  2142. void *
  2143. andna_maintain_hnames_active(void *null)
  2144. {
  2145. lcl_cache *alcl;
  2146. int ret, updates;
  2147. /* Wait a bit before trying to register the hname. The first QSPN must
  2148. * be already sent */
  2149. while (time(0) - me.uptime < QSPN_WAIT_ROUND / 2)
  2150. sleep(1);
  2151. for (;;) {
  2152. updates = 0;
  2153. alcl = andna_lcl;
  2154. /** If we don't have rnodes, it's useless to try
  2155. * anything */
  2156. while (!me.cur_node->links)
  2157. sleep(2);
  2158. /**/ list_for(alcl) {
  2159. ret = andna_register_hname(alcl, 0);
  2160. if (!ret) {
  2161. loginfo("Hostname \"%s\" registered/updated "
  2162. "successfully", alcl->hostname);
  2163. updates++;
  2164. }
  2165. }
  2166. if (updates)
  2167. save_lcl_cache(andna_lcl, server_opt.lcl_file);
  2168. sleep((ANDNA_EXPIRATION_TIME / 2) + rand_range(1, 10));
  2169. }
  2170. return 0;
  2171. }
  2172. void *
  2173. andna_main(void *null)
  2174. {
  2175. struct udp_daemon_argv ud_argv;
  2176. u_short *port;
  2177. pthread_t thread;
  2178. pthread_attr_t t_attr;
  2179. pthread_attr_init(&t_attr);
  2180. pthread_attr_setdetachstate(&t_attr, PTHREAD_CREATE_DETACHED);
  2181. setzero(&ud_argv, sizeof(struct udp_daemon_argv));
  2182. port = xmalloc(sizeof(u_short));
  2183. pthread_mutex_init(&udp_daemon_lock, 0);
  2184. pthread_mutex_init(&tcp_daemon_lock, 0);
  2185. debug(DBG_SOFT, "Evoking the andna udp daemon.");
  2186. ud_argv.port = andna_udp_port;
  2187. ud_argv.flags |= UDP_THREAD_FOR_EACH_PKT;
  2188. pthread_mutex_lock(&udp_daemon_lock);
  2189. pthread_create(&thread, &t_attr, udp_daemon, (void *) &ud_argv);
  2190. pthread_mutex_lock(&udp_daemon_lock);
  2191. pthread_mutex_unlock(&udp_daemon_lock);
  2192. debug(DBG_SOFT, "Evoking the andna tcp daemon.");
  2193. *port = andna_tcp_port;
  2194. pthread_mutex_lock(&tcp_daemon_lock);
  2195. pthread_create(&thread, &t_attr, tcp_daemon, (void *) port);
  2196. pthread_mutex_lock(&tcp_daemon_lock);
  2197. pthread_mutex_unlock(&tcp_daemon_lock);
  2198. /*
  2199. * Start the ANDNA hook
  2200. */
  2201. pthread_create(&thread, &t_attr, andna_hook, 0);
  2202. /*
  2203. * DNS wrapper
  2204. */
  2205. debug(DBG_SOFT, "Evoking the DNS wrapper daemon.");
  2206. pthread_create(&thread, &t_attr, dns_wrapper_thread, 0);
  2207. /*
  2208. * Start the hostnames updater and register
  2209. */
  2210. andna_maintain_hnames_active(0);
  2211. xfree(port);
  2212. return 0;
  2213. }