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_cache.h 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. #ifndef ANDNA_CACHE_H
  19. #define ANDNA_CACHE_H
  20. #include "inet.h"
  21. #include "crypto.h"
  22. #include "endianness.h"
  23. #include "llist.c"
  24. #include "snsd_cache.h"
  25. /*
  26. * ANDNA definitions
  27. */
  28. #define ANDNA_MAX_BACKUP_GNODES 2
  29. #define ANDNA_MAX_QUEUE 5
  30. #define ANDNA_MAX_HNAME_LEN 512 /* (null terminator included) */
  31. #define ANDNA_MAX_HOSTNAMES 256 /* Max number of hnames per node */
  32. #define ANDNA_MAX_RHC_HNAMES 512 /* Max number of hnames kept in
  33. the resolved_hnames cache* */
  34. #define ANDNA_EXPIRATION_TIME 259200 /* 3 days (in seconds) */
  35. #define ANDNA_MIN_UPDATE_TIME 3600 /* The minum amount of time to
  36. be waited before sending an
  37. update of the hname. */
  38. #define ANDNA_PRIVKEY_BITS 1024
  39. #define ANDNA_SKEY_MAX_LEN 900
  40. #define ANDNA_PKEY_LEN 140
  41. #define ANDNA_HASH_SZ (MAX_IP_SZ)
  42. #define ANDNA_SIGNATURE_LEN 128
  43. /* Returns the number of nodes to be used in a backup_gnode */
  44. #define ANDNA_BACKUP_NODES(seeds) ({(seeds) > 8 ? \
  45. ((seeds)*32)/MAXGROUPNODE : (seeds);})
  46. #ifdef DEBUG
  47. #undef ANDNA_EXPIRATION_TIME
  48. #define ANDNA_EXPIRATION_TIME 100
  49. #undef ANDNA_MIN_UPDATE_TIME
  50. #define ANDNA_MIN_UPDATE_TIME 2
  51. #endif
  52. /*
  53. * * * Cache stuff * * *
  54. */
  55. /* * andna_cache flags * */
  56. #define ANDNA_BACKUP 1 /* We are a backup_node */
  57. #define ANDNA_COUNTER (1<<1) /* We are a counter_node */
  58. #define ANDNA_ROUNDED (1<<2) /* We are a rounded_hash_node */
  59. #define ANDNA_FULL (1<<3) /* Queue full */
  60. #define ANDNA_UPDATING (1<<4) /* The hname is being updated
  61. right now */
  62. /*
  63. * andna_cache_queue
  64. *
  65. * The queue of the andna_cache. (see below).
  66. */
  67. struct andna_cache_queue {
  68. LLIST_HDR(struct andna_cache_queue);
  69. time_t timestamp;
  70. u_short hname_updates; /* numbers of hname's updates */
  71. char pubkey[ANDNA_PKEY_LEN];
  72. u_short snsd_counter; /* # of `snsd' nodes */
  73. snsd_service *service;
  74. };
  75. typedef struct andna_cache_queue andna_cache_queue;
  76. /*
  77. * andna_cache
  78. *
  79. * It keeps the entries of the hostnames registered by other nodes.
  80. */
  81. struct andna_cache {
  82. LLIST_HDR(struct andna_cache);
  83. u_int hash[MAX_IP_INT]; /* hostname's hash */
  84. char flags;
  85. u_short queue_counter;
  86. andna_cache_queue *acq; /* The queue of the registration.
  87. The first is the active one */
  88. };
  89. typedef struct andna_cache andna_cache;
  90. /* part of the counter cache, see below */
  91. struct counter_c_hashes {
  92. LLIST_HDR(struct counter_c_hashes);
  93. time_t timestamp;
  94. u_short hname_updates;
  95. int hash[MAX_IP_INT];
  96. };
  97. typedef struct counter_c_hashes counter_c_hashes;
  98. INT_INFO counter_c_hashes_body_iinfo = { 2,
  99. {INT_TYPE_32BIT, INT_TYPE_16BIT},
  100. {0, sizeof(time_t)}
  101. ,
  102. {1, 1}
  103. };
  104. /*
  105. * counter_c
  106. * Counter node's cache.
  107. *
  108. * All the infos regarding a particular register_node are stored here. For
  109. * example, we need to know how many hostnames he already registered.
  110. */
  111. struct counter_c {
  112. LLIST_HDR(struct counter_c);
  113. char pubkey[ANDNA_PKEY_LEN];
  114. char flags;
  115. u_short hashes; /* The number of hashes in cch */
  116. counter_c_hashes *cch; /* The hashes of the hnames */
  117. };
  118. typedef struct counter_c counter_c;
  119. INT_INFO counter_c_body_iinfo = { 1,
  120. {INT_TYPE_16BIT},
  121. {ANDNA_PKEY_LEN + sizeof(char)},
  122. {1}
  123. };
  124. /*
  125. * lcl_cache_keyring
  126. *
  127. * The lcl keyring is used to store the RSA keys used to complete some of the
  128. * ANDNA requests, (f.e. registering or updating a hname).
  129. */
  130. typedef struct {
  131. u_int skey_len;
  132. u_int pkey_len;
  133. u_char *privkey; /* secret key packed */
  134. u_char *pubkey; /* pubkey packed */
  135. RSA *priv_rsa; /* key pair unpacked */
  136. } lcl_cache_keyring;
  137. /*
  138. * lcl_cache
  139. *
  140. * The Local Andna Cache keeps all the hostnames which have been register by
  141. * localhost (ourself).
  142. */
  143. struct lcl_cache {
  144. LLIST_HDR(struct lcl_cache);
  145. char *hostname; /* The registered hostname */
  146. u_int hash; /* 32bit hash of the md5 hash
  147. of the hname */
  148. u_short hname_updates; /* How many updates we've done
  149. for this hostname */
  150. time_t timestamp; /* the last time when the hname
  151. was updated. If it is 0, the
  152. hname has still to be
  153. registered */
  154. u_short snsd_counter; /* # of `snsds' */
  155. snsd_service *service;
  156. char flags;
  157. };
  158. typedef struct lcl_cache lcl_cache;
  159. /*
  160. * resolved_hnames_cache
  161. *
  162. * This cache keeps info on the already resolved hostnames, so we won't have
  163. * to resolve them soon again.
  164. * In order to optimize the search we order the linked list by the time
  165. * of hname resolution. The last hname which has been searched/resolved is
  166. * always moved at the head of the llist, in this way, at the end of the llist
  167. * there is the hname which has been searched for the first time but has been
  168. * ignored until now.
  169. * When the cache is full, the hname which is at the end of the llist is
  170. * removed to empty new space.
  171. * The hname which have the `timestamp' expired are removed too.
  172. */
  173. struct resolved_hnames_cache {
  174. LLIST_HDR(struct resolved_hnames_cache);
  175. u_int hash; /* 32bit hash of the md5 hash of the
  176. hname */
  177. char flags;
  178. time_t timestamp; /* the last time when the hname
  179. was updated. With this we know that
  180. at timestamp+ANDNA_EXPIRATION_TIME
  181. this cache will expire. */
  182. u_short snsd_counter;
  183. snsd_service *service;
  184. };
  185. typedef struct resolved_hnames_cache rh_cache;
  186. /*
  187. * * * * Global vars * * *
  188. */
  189. andna_cache *andna_c;
  190. int andna_c_counter;
  191. counter_c *andna_counter_c;
  192. int cc_counter;
  193. lcl_cache_keyring lcl_keyring;
  194. lcl_cache *andna_lcl;
  195. int lcl_counter;
  196. rh_cache *andna_rhc;
  197. int rhc_counter;
  198. /*
  199. *
  200. * * * * Package stuff * * *
  201. *
  202. */
  203. /*
  204. * * * * lcl cache package * * *
  205. */
  206. struct lcl_keyring_pkt_hdr {
  207. u_int skey_len;
  208. u_int pkey_len;
  209. } _PACKED_;
  210. /*
  211. * the rest of the pkt is:
  212. *
  213. * char privkey[hdr.skey_len];
  214. * char pubkey[hdr.pkey_len];
  215. */
  216. INT_INFO lcl_keyring_pkt_hdr_iinfo = { 2,
  217. {INT_TYPE_32BIT, INT_TYPE_32BIT},
  218. {0, sizeof(u_int)}
  219. ,
  220. {1, 1}
  221. };
  222. #define LCL_KEYRING_HDR_PACK_SZ(khdr) (sizeof(struct lcl_keyring_pkt_hdr) + \
  223. (khdr)->skey_len + (khdr)->pkey_len)
  224. /*
  225. * The local cache pkt is used to pack the entire local cache to save it in a
  226. * file or to send it to a node.
  227. */
  228. struct lcl_cache_pkt_hdr {
  229. u_short tot_caches; /* How many lcl structs there
  230. are in the pkt's body */
  231. } _PACKED_;
  232. INT_INFO lcl_cache_pkt_hdr_iinfo = { 1, {INT_TYPE_16BIT}, {0}, {1} };
  233. #define LCL_CACHE_HDR_PACK_SZ (sizeof(struct lcl_cache_pkt_hdr))
  234. /*
  235. * The body is:
  236. *
  237. * struct lcl_cache_pkt_body {
  238. * u_short hname_updates;
  239. * time_t timestamp;
  240. * char hostname[strlen(hostname)+1]; * null terminated *
  241. * } body[ hdr.tot_caches ];
  242. *
  243. */
  244. #define LCL_CACHE_BODY_PACK_SZ(hname_len) ((hname_len) + sizeof(u_short) \
  245. + sizeof(time_t))
  246. INT_INFO lcl_cache_pkt_body_iinfo = { 2, {INT_TYPE_16BIT, INT_TYPE_32BIT},
  247. {0, sizeof(u_short)}
  248. ,
  249. {1, 1}
  250. };
  251. /*
  252. * * * * andna cache package * * *
  253. */
  254. /*
  255. * the body of the acq_pkt is:
  256. * struct acq_pkt_body {
  257. * time_t timestamp;
  258. * u_short hname_updates;
  259. * char pubkey[ANDNA_PKEY_LEN];
  260. *
  261. * u_short snsd_counter;
  262. * char snsd_service_pack[SNSD_SERVICE_PACK_SZ];
  263. * };
  264. */
  265. INT_INFO acq_body_iinfo = { 3,
  266. {INT_TYPE_32BIT, INT_TYPE_16BIT, INT_TYPE_16BIT}
  267. ,
  268. {0, sizeof(time_t),
  269. sizeof(time_t) + sizeof(u_short) + ANDNA_PKEY_LEN}
  270. ,
  271. {1, 1, 1}
  272. };
  273. #define ACQ_BODY_PACK_SZ (sizeof(time_t) + sizeof(u_short)*2 + \
  274. ANDNA_PKEY_LEN)
  275. #define ACQ_PACK_SZ(snsd_pack_sz) (ACQ_BODY_PACK_SZ + (snsd_pack_sz))
  276. struct andna_cache_pkt_hdr {
  277. u_short tot_caches;
  278. } _PACKED_;
  279. INT_INFO andna_cache_pkt_hdr_iinfo = { 1, {INT_TYPE_16BIT}, {0}, {1} };
  280. /*
  281. * The body is:
  282. * struct andna_cache_pack {
  283. * u_int hash[MAX_IP_INT];
  284. * char flags;
  285. * u_short queue_counter;
  286. * char acq_pack[ACQ_PACK_SZ*queue_counter];
  287. * } acache_pack[hdr.tot_caches];
  288. */
  289. INT_INFO andna_cache_body_iinfo = { 1,
  290. {INT_TYPE_16BIT},
  291. {MAX_IP_SZ + sizeof(char)},
  292. {1}
  293. };
  294. #define ACACHE_BODY_PACK_SZ (ANDNA_HASH_SZ + sizeof(char) + \
  295. sizeof(u_short))
  296. #define ACACHE_PACK_SZ(acq_pack_sz) ((acq_pack_sz) + ACACHE_BODY_PACK_SZ)
  297. /*
  298. * If the acache pack will be sent on a network packet, the `acq->timestamp'
  299. * will be the difference of the current time with the same `acq->timestamp',
  300. * in this way the node which receives the packet will add its current time to
  301. * `acq->timestamp'. This is necessary because the sending and receiving node
  302. * don't have the clock synced. Note that the rtt isn't considered because it
  303. * is generally very small and the ANDNA times don't need an accurate
  304. * precision, f.e. the expiration time is three days long.
  305. * If the pack is saved on a file, then `acq->timestamp' remains the same.
  306. * Problem: if the clock is changed, acq->timestamp will refer to the old
  307. * clock.
  308. */
  309. #define ACACHE_PACK_FILE 1
  310. #define ACACHE_PACK_PKT 2
  311. /*
  312. * The counter cache pkt is similar to the andna_cache_pkt, it is completely
  313. * arranged in the same way.
  314. */
  315. struct counter_c_pkt_hdr {
  316. u_short tot_caches;
  317. } _PACKED_;
  318. INT_INFO counter_c_pkt_hdr_iinfo = { 1, {INT_TYPE_16BIT}, {0}, {1} };
  319. #define COUNTER_CACHE_HASHES_PACK_SZ (sizeof(time_t) + sizeof(u_short) + \
  320. ANDNA_HASH_SZ)
  321. #define COUNTER_CACHE_BODY_PACK_SZ (ANDNA_PKEY_LEN + sizeof(char) + \
  322. sizeof(u_short))
  323. #define COUNTER_CACHE_PACK_SZ(hashes) ((COUNTER_CACHE_HASHES_PACK_SZ*(hashes))\
  324. + COUNTER_CACHE_BODY_PACK_SZ)
  325. /*
  326. * * * * Resolved hostnames cache pkt. * * *
  327. */
  328. struct rh_cache_pkt_hdr {
  329. u_short tot_caches; /* How many lcl structs there
  330. are in the pkt's hdr */
  331. } _PACKED_;
  332. INT_INFO rh_cache_pkt_hdr_iinfo = { 1, {INT_TYPE_16BIT}, {0}, {1} };
  333. /*
  334. * The body is:
  335. * struct rh_cache_pkt_body {
  336. * u_int hash;
  337. * char flags;
  338. * time_t timestamp;
  339. *
  340. * u_short snsd_counter;
  341. * char snsd_service_pack[SNSD_SERVICE_PACK_SZ];
  342. * } body[ hdr.tot_caches ];
  343. */
  344. #define RH_CACHE_BODY_PACK_SZ(snsd_pack_sz) (sizeof(u_int)+sizeof(char)+ \
  345. sizeof(time_t)+sizeof(u_short)+\
  346. (snsd_pack_sz))
  347. INT_INFO rh_cache_pkt_body_iinfo = { 3,
  348. {INT_TYPE_32BIT, INT_TYPE_32BIT, INT_TYPE_16BIT},
  349. {0, sizeof(u_int) + sizeof(char),
  350. sizeof(u_int) + sizeof(char) + sizeof(time_t)}
  351. ,
  352. {1, 1, 1}
  353. };
  354. /*
  355. * * * Functions' declaration * * *
  356. */
  357. void andna_caches_init(int family);
  358. void lcl_new_keyring(lcl_cache_keyring * keyring);
  359. void lcl_destroy_keyring(lcl_cache_keyring * keyring);
  360. lcl_cache *lcl_cache_new(char *hname);
  361. void lcl_cache_free(lcl_cache * alcl);
  362. void lcl_cache_destroy(lcl_cache * head, int *counter);
  363. lcl_cache *lcl_cache_find_hname(lcl_cache * head, char *hname);
  364. lcl_cache *lcl_cache_find_hash(lcl_cache * alcl, u_int hash);
  365. lcl_cache *lcl_get_registered_hnames(lcl_cache * alcl);
  366. andna_cache_queue *ac_queue_findpubk(andna_cache * ac, char *pubk);
  367. andna_cache_queue *ac_queue_add(andna_cache * ac, char *pubkey);
  368. void ac_queue_del(andna_cache * ac, andna_cache_queue * acq);
  369. void ac_queue_del_expired(andna_cache * ac);
  370. void ac_queue_destroy(andna_cache * ac);
  371. andna_cache *andna_cache_findhash(int hash[MAX_IP_INT]);
  372. andna_cache *andna_cache_gethash(int hash[MAX_IP_INT]);
  373. andna_cache *andna_cache_addhash(int hash[MAX_IP_INT]);
  374. int andna_cache_del_ifexpired(andna_cache * ac);
  375. void andna_cache_del_expired(void);
  376. void andna_cache_destroy(void);
  377. counter_c_hashes *cc_hashes_add(counter_c * cc, int hash[MAX_IP_INT]);
  378. void cc_hashes_del(counter_c * cc, counter_c_hashes * cch);
  379. int counter_c_del_ifexpired(counter_c * cc);
  380. void cc_hashes_del_expired(counter_c * cc);
  381. void cc_hashes_destroy(counter_c * cc);
  382. counter_c_hashes *cc_findhash(counter_c * cc, int hash[MAX_IP_INT]);
  383. counter_c *counter_c_findpubk(char *pubk);
  384. counter_c *counter_c_add(inet_prefix * rip, char *pubkey);
  385. void counter_c_del_expired(void);
  386. void counter_c_destroy(void);
  387. rh_cache *rh_cache_new(char *hname, time_t timestamp);
  388. rh_cache *rh_cache_add_hash(u_int hash, time_t timestamp);
  389. rh_cache *rh_cache_add(char *hname, time_t timestamp);
  390. rh_cache *rh_cache_find_hash(u_int hash);
  391. rh_cache *rh_cache_find_hname(char *hname);
  392. void rh_cache_del(rh_cache * rhc);
  393. void rh_cache_del_expired(void);
  394. void rh_cache_flush(void);
  395. char *pack_lcl_keyring(lcl_cache_keyring * keyring, size_t * pack_sz);
  396. int unpack_lcl_keyring(lcl_cache_keyring * keyring, char *pack,
  397. size_t pack_sz);
  398. char *pack_lcl_cache(lcl_cache * local_cache, size_t * pack_sz);
  399. lcl_cache *unpack_lcl_cache(char *pack, size_t pack_sz, int *counter);
  400. char *pack_andna_cache(andna_cache * acache, size_t * pack_sz,
  401. int pack_type);
  402. andna_cache *unpack_andna_cache(char *pack, size_t pack_sz, int *counter,
  403. int pack_type);
  404. char *pack_counter_cache(counter_c * countercache, size_t * pack_sz);
  405. counter_c *unpack_counter_cache(char *pack, size_t pack_sz, int *counter);
  406. char *pack_rh_cache(rh_cache * rhcache, size_t * pack_sz);
  407. rh_cache *unpack_rh_cache(char *pack, size_t pack_sz, int *counter);
  408. int save_lcl_keyring(lcl_cache_keyring * keyring, char *file);
  409. int load_lcl_keyring(lcl_cache_keyring * keyring, char *file);
  410. int save_lcl_cache(lcl_cache * lcl, char *file);
  411. lcl_cache *load_lcl_cache(char *file, int *counter);
  412. int save_andna_cache(andna_cache * acache, char *file);
  413. andna_cache *load_andna_cache(char *file, int *counter);
  414. int save_counter_c(counter_c * countercache, char *file);
  415. counter_c *load_counter_c(char *file, int *counter);
  416. int save_rh_cache(rh_cache * rh, char *file);
  417. rh_cache *load_rh_cache(char *file, int *counter);
  418. int load_hostnames(char *file, lcl_cache ** old_alcl_head,
  419. int *old_alcl_counter);
  420. int load_snsd(char *file, lcl_cache * alcl_head);
  421. int add_resolv_conf(char *hname, char *file);
  422. int del_resolv_conf(char *hname, char *file);
  423. #endif /*ANDNA_CACHE_H */