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

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