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.h 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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_H
  19. #define ANDNA_H
  20. #include "andna_cache.h"
  21. #include "pkts.h"
  22. #define MY_NAMESERV "nameserver 127.0.0.1"
  23. #define MY_NAMESERV_IPV6 "nameserver ::1"
  24. #define ETC_RESOLV_CONF "/etc/resolv.conf"
  25. #define ETC_RESOLV_CONF_BAK "/etc/resolv.conf.bak"
  26. /* How many different andna pkt can be flooded simultaneusly */
  27. #define ANDNA_MAX_FLOODS (ANDNA_MAX_QUEUE*3+1)
  28. /* How many new hash_gnodes are supported in the andna hash_gnode mutation */
  29. #define ANDNA_MAX_NEW_GNODES 1024
  30. /*
  31. * These arrays keeps the latest reg_pkt and counter_check IDs to drop pkts
  32. * alreay received during the floods. These arrays are a FIFO, so the
  33. * last pkt_id will be always at the 0 position, while the first one will be
  34. * at the last position
  35. */
  36. int last_reg_pkt_id[ANDNA_MAX_FLOODS];
  37. int last_counter_pkt_id[ANDNA_MAX_FLOODS];
  38. int last_spread_acache_pkt_id[ANDNA_MAX_FLOODS];
  39. /*\
  40. * *** ANDNA hash notes ***
  41. *
  42. * In ANDNA there are three type of hashes: MD5, 32bit, 32bit hash of a MD5
  43. * hash. These hashes are generally applied on hostnames.
  44. *
  45. * The andna_hash() function, defined in andna.c, is used to calculate
  46. * the IP of a hash_node/hash_gnode/counter_node. It makes a MD5 digest of the
  47. * input data. If we are working on ipv4, then a 32bit hash is applied to the
  48. * previously calculated MD5 digest. The result is the IP of the hash_gnode.
  49. * If we are in ipv6, we'll use directly the MD5 digest as the hash_gnode IP.
  50. *
  51. * In all the other cases we'll use directly the MD5 hash of the hostname,
  52. * f.e. the hname hash of the registration and resolution packets is a MD5.
  53. * The only exceptions are the lcl_cache and the rh_cache, which use
  54. * internally a 32bit hash to speed up the hname lookups.
  55. *
  56. * The general guideline for new implementation is to always use big hashes
  57. * (i.e. MD5) where we might get collisions (f.e in an andna_cache), and to
  58. * use small hashes where we are safe (f.e. in the rhc_cache).
  59. *
  60. \*/
  61. /*\
  62. *
  63. * * * * ANDNA requests/replies pkt stuff * * *
  64. *
  65. \*/
  66. #define ANDNA_HOOK_TIMEOUT 8 /* seconds */
  67. #define ANDNA_REV_RESOLVE_RQ_TIMEOUT 60
  68. /* * * andna pkt flags * * */
  69. #define ANDNA_PKT_UPDATE 1 /* Update the hostname */
  70. #define ANDNA_PKT_FORWARD (1<<1) /* Forward this pkt, plz */
  71. #define ANDNA_PKT_REV_RESOLVE (1<<2) /* Give me your hostnames */
  72. #define ANDNA_PKT_JUST_CHECK (1<<3) /* Check only, don't update
  73. anything */
  74. #define ANDNA_PKT_SNSD_DEL (1<<4) /* SNSD delete request */
  75. /*
  76. * andna_reg_pkt
  77. *
  78. * Andna registration request pkt used to send the registration and update
  79. * requests to the hash_gnode, backup_gnode and counter_gnode.
  80. * When the pkt is sent to a counter_gnode, a second `rip', which is the ip
  81. * of the hash_gnode who is contacting the counter_gnode, is appended at the
  82. * end of the pkt.
  83. *
  84. * When the packet is sent to a hash_gnode, at the end of the packet is
  85. * included a packed snsd_service linked list. It is the list of snsd_records
  86. * that have to be registered. However the packet forwarded to the counter
  87. * node won't keep this part.
  88. */
  89. struct andna_reg_pkt {
  90. u_int rip[MAX_IP_INT]; /* register_node ip */
  91. u_int hash[MAX_IP_INT]; /* md5 hash of the host name to
  92. register. */
  93. char pubkey[ANDNA_PKEY_LEN]; /* public key of the register
  94. node. */
  95. u_short hname_updates; /* number of updates already
  96. made for the hostname */
  97. char sign[ANDNA_SIGNATURE_LEN]; /* RSA signature of the
  98. entire pkt (excluding
  99. `sign' itself and `flags'
  100. */
  101. char flags;
  102. } _PACKED_;
  103. #define ANDNA_REG_PKT_SZ (sizeof(struct andna_reg_pkt))
  104. #define ANDNA_REG_SIGNED_BLOCK_SZ (ANDNA_REG_PKT_SZ - ANDNA_SIGNATURE_LEN - \
  105. sizeof(char))
  106. INT_INFO andna_reg_pkt_iinfo = { 1, /* `rip' and `hash' aren't considered */
  107. {INT_TYPE_16BIT},
  108. {MAX_IP_SZ * 2 + ANDNA_PKEY_LEN},
  109. {1},
  110. };
  111. /*
  112. * andna_resolve_rq_pkt
  113. *
  114. * The andna resolve request pkt is used to resolve hostnames, IPs and MX
  115. * hostnames.
  116. */
  117. struct andna_resolve_rq_pkt {
  118. u_int rip[MAX_IP_INT]; /* the ip of the requester node */
  119. char flags;
  120. u_int hash[MAX_IP_INT]; /* md5 hash of the hostname to
  121. resolve. */
  122. int service; /* the snsd service of the hname */
  123. u_char proto; /* the protocol of `service' */
  124. } _PACKED_;
  125. #define ANDNA_RESOLVE_RQ_PKT_SZ (sizeof(struct andna_resolve_rq_pkt))
  126. INT_INFO andna_resolve_rq_pkt_iinfo = { 1, /* `rip' and `hash' are ignored */
  127. {INT_TYPE_32BIT},
  128. {MAX_IP_SZ * 2 + sizeof(char)},
  129. {1},
  130. };
  131. /*
  132. * The reply to the resolve request
  133. */
  134. struct andna_resolve_reply_pkt {
  135. uint32_t timestamp; /* the difference between the current
  136. time and the last time the resolved
  137. hname was updated */
  138. /*
  139. * the rest of the pkt is a pack of one snsd_service llist:
  140. * char service[SNSD_SERVICE_LLIST_PACK_SZ(service)];
  141. */
  142. } _PACKED_;
  143. #define ANDNA_RESOLVE_REPLY_PKT_SZ (sizeof(struct andna_resolve_reply_pkt))
  144. INT_INFO andna_resolve_reply_pkt_iinfo = { 1, /* `ip' is ignored */
  145. {INT_TYPE_32BIT},
  146. {0},
  147. {1}
  148. };
  149. /*
  150. * The reply to the reverse resolve request is just the packed local cache.
  151. */
  152. /*
  153. * single_acache
  154. *
  155. * The single_acache pkt is used to get from an old hash_gnode a single
  156. * andna_cache, which has the wanted `hash'. Its propagation method is similar
  157. * to that of andna_resolve_rq_pkt, but each new hash_gnode, which receives
  158. * the pkt, adds in the body pkt its ip. The added ips are used as excluded
  159. * hash_gnode by find_hash_gnode(). In this way each time an old hash_gnode
  160. * receives the pkt, can verify if it is, at that current time, the true old
  161. * hash_gnode by excluding the hash_gnodes listed in the pkt body. If it
  162. * notices that there's an hash_gnode older than it, it will append its ip in
  163. * the pkt body and will forward it to that older hash_gnode. And so on, until
  164. * the pkt reaches a true old hash_gnode, or cannot be forwarded anymore since
  165. * there are no more older hash_gnodes.
  166. */
  167. struct single_acache_hdr {
  168. u_int rip[MAX_IP_INT]; /* the ip of the requester node */
  169. u_int hash[MAX_IP_INT];
  170. u_short hgnodes; /* Number of hgnodes in the
  171. body. */
  172. u_char flags;
  173. } _PACKED_;
  174. INT_INFO single_acache_hdr_iinfo = { 1, /* `rip' and `hash' are ignored */
  175. {INT_TYPE_16BIT},
  176. {MAX_IP_SZ * 2},
  177. {1},
  178. };
  179. /*
  180. * The single_acache body is:
  181. * struct {
  182. * u_int hgnode[MAX_IP_INT];
  183. * } body[new_hash_gnode_hdr.hgnodes];
  184. */
  185. #define SINGLE_ACACHE_PKT_SZ(hgnodes) (sizeof(struct single_acache_hdr)+\
  186. MAX_IP_SZ*(hgnodes))
  187. /*
  188. * The single_acache_reply is just an andna_cache_pkt with a single cache.
  189. */
  190. /*
  191. * Tell the node, which receives the pkt, to send a ANDNA_GET_SINGLE_ACACHE
  192. * request to fetch the andna_cache for the `hash' included in the pkt.
  193. */
  194. struct spread_acache_pkt {
  195. u_int hash[MAX_IP_INT];
  196. } _PACKED_;
  197. #define SPREAD_ACACHE_PKT_SZ (sizeof(struct spread_acache_pkt))
  198. INT_INFO spread_acache_pkt_info = { 0, {0}, {0}, {0} };
  199. /*\
  200. *
  201. * * * * Function declaration * * *
  202. *
  203. \*/
  204. int andna_load_caches(void);
  205. int andna_save_caches(void);
  206. void andna_init(void);
  207. void andna_close(void);
  208. void andna_resolvconf_modify(void);
  209. void andna_resolvconf_restore(void);
  210. int andna_register_hname(lcl_cache * alcl, snsd_service * snsd_delete);
  211. int andna_recv_reg_rq(PACKET rpkt);
  212. int andna_check_counter(PACKET pkt);
  213. int andna_recv_check_counter(PACKET rpkt);
  214. snsd_service *andna_resolve_hash(u_int hname_hash[MAX_IP_INT], int service,
  215. u_char proto, int *records);
  216. snsd_service *andna_resolve_hname(char *hname, int service, u_char proto,
  217. int *records);
  218. int andna_recv_resolve_rq(PACKET rpkt);
  219. lcl_cache *andna_reverse_resolve(inet_prefix ip);
  220. int andna_recv_rev_resolve_rq(PACKET rpkt);
  221. int spread_single_acache(u_int hash[MAX_IP_INT]);
  222. int recv_spread_single_acache(PACKET rpkt);
  223. andna_cache *get_single_andna_c(u_int hash[MAX_IP_INT],
  224. u_int hash_gnode[MAX_IP_INT]);
  225. int put_single_acache(PACKET rpkt);
  226. int put_andna_cache(PACKET rq_pkt);
  227. int put_counter_cache(PACKET rq_pkt);
  228. void *andna_hook(void *);
  229. void andna_update_hnames(int only_new_hname);
  230. void *andna_maintain_hnames_active(void *null);
  231. void *andna_main(void *);
  232. #endif /*ANDNA_H */