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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. {
  91. u_int rip[MAX_IP_INT]; /* register_node ip */
  92. u_int hash[MAX_IP_INT]; /* md5 hash of the host name to
  93. register. */
  94. char pubkey[ANDNA_PKEY_LEN]; /* public key of the register
  95. node. */
  96. u_short hname_updates; /* number of updates already
  97. made for the hostname */
  98. char sign[ANDNA_SIGNATURE_LEN]; /* RSA signature of the
  99. entire pkt (excluding
  100. `sign' itself and `flags'
  101. */
  102. char flags;
  103. } _PACKED_;
  104. #define ANDNA_REG_PKT_SZ (sizeof(struct andna_reg_pkt))
  105. #define ANDNA_REG_SIGNED_BLOCK_SZ (ANDNA_REG_PKT_SZ - ANDNA_SIGNATURE_LEN - \
  106. sizeof(char))
  107. INT_INFO andna_reg_pkt_iinfo = { 1, /* `rip' and `hash' aren't considered */
  108. { INT_TYPE_16BIT },
  109. { MAX_IP_SZ*2 + ANDNA_PKEY_LEN },
  110. { 1 },
  111. };
  112. /*
  113. * andna_resolve_rq_pkt
  114. *
  115. * The andna resolve request pkt is used to resolve hostnames, IPs and MX
  116. * hostnames.
  117. */
  118. struct andna_resolve_rq_pkt
  119. {
  120. u_int rip[MAX_IP_INT]; /* the ip of the requester node */
  121. char flags;
  122. u_int hash[MAX_IP_INT]; /* md5 hash of the hostname to
  123. resolve. */
  124. int service; /* the snsd service of the hname */
  125. u_char proto; /* the protocol of `service' */
  126. } _PACKED_;
  127. #define ANDNA_RESOLVE_RQ_PKT_SZ (sizeof(struct andna_resolve_rq_pkt))
  128. INT_INFO andna_resolve_rq_pkt_iinfo = { 1, /* `rip' and `hash' are ignored */
  129. { INT_TYPE_32BIT },
  130. { MAX_IP_SZ*2+sizeof(char) },
  131. { 1 },
  132. };
  133. /*
  134. * The reply to the resolve request
  135. */
  136. struct andna_resolve_reply_pkt
  137. {
  138. uint32_t timestamp; /* the difference between the current
  139. time and the last time the resolved
  140. hname was updated */
  141. /*
  142. * the rest of the pkt is a pack of one snsd_service llist:
  143. * char service[SNSD_SERVICE_LLIST_PACK_SZ(service)];
  144. */
  145. } _PACKED_;
  146. #define ANDNA_RESOLVE_REPLY_PKT_SZ (sizeof(struct andna_resolve_reply_pkt))
  147. INT_INFO andna_resolve_reply_pkt_iinfo = { 1, /* `ip' is ignored */
  148. { INT_TYPE_32BIT },
  149. { 0 },
  150. { 1 }
  151. };
  152. /*
  153. * The reply to the reverse resolve request is just the packed local cache.
  154. */
  155. /*
  156. * single_acache
  157. *
  158. * The single_acache pkt is used to get from an old hash_gnode a single
  159. * andna_cache, which has the wanted `hash'. Its propagation method is similar
  160. * to that of andna_resolve_rq_pkt, but each new hash_gnode, which receives
  161. * the pkt, adds in the body pkt its ip. The added ips are used as excluded
  162. * hash_gnode by find_hash_gnode(). In this way each time an old hash_gnode
  163. * receives the pkt, can verify if it is, at that current time, the true old
  164. * hash_gnode by excluding the hash_gnodes listed in the pkt body. If it
  165. * notices that there's an hash_gnode older than it, it will append its ip in
  166. * the pkt body and will forward it to that older hash_gnode. And so on, until
  167. * the pkt reaches a true old hash_gnode, or cannot be forwarded anymore since
  168. * there are no more older hash_gnodes.
  169. */
  170. struct single_acache_hdr
  171. {
  172. u_int rip[MAX_IP_INT]; /* the ip of the requester node */
  173. u_int hash[MAX_IP_INT];
  174. u_short hgnodes; /* Number of hgnodes in the
  175. body. */
  176. u_char flags;
  177. } _PACKED_;
  178. INT_INFO single_acache_hdr_iinfo = { 1, /* `rip' and `hash' are ignored */
  179. { INT_TYPE_16BIT },
  180. { MAX_IP_SZ*2 },
  181. { 1 },
  182. };
  183. /*
  184. * The single_acache body is:
  185. * struct {
  186. * u_int hgnode[MAX_IP_INT];
  187. * } body[new_hash_gnode_hdr.hgnodes];
  188. */
  189. #define SINGLE_ACACHE_PKT_SZ(hgnodes) (sizeof(struct single_acache_hdr)+\
  190. MAX_IP_SZ*(hgnodes))
  191. /*
  192. * The single_acache_reply is just an andna_cache_pkt with a single cache.
  193. */
  194. /*
  195. * Tell the node, which receives the pkt, to send a ANDNA_GET_SINGLE_ACACHE
  196. * request to fetch the andna_cache for the `hash' included in the pkt.
  197. */
  198. struct spread_acache_pkt
  199. {
  200. u_int hash[MAX_IP_INT];
  201. } _PACKED_;
  202. #define SPREAD_ACACHE_PKT_SZ (sizeof(struct spread_acache_pkt))
  203. INT_INFO spread_acache_pkt_info = { 0, { 0 }, { 0 }, { 0 } };
  204. /*\
  205. *
  206. * * * * Function declaration * * *
  207. *
  208. \*/
  209. int andna_load_caches(void);
  210. int andna_save_caches(void);
  211. void andna_init(void);
  212. void andna_close(void);
  213. void andna_resolvconf_modify(void);
  214. void andna_resolvconf_restore(void);
  215. int andna_register_hname(lcl_cache *alcl, snsd_service *snsd_delete);
  216. int andna_recv_reg_rq(PACKET rpkt);
  217. int andna_check_counter(PACKET pkt);
  218. int andna_recv_check_counter(PACKET rpkt);
  219. snsd_service *andna_resolve_hash(u_int hname_hash[MAX_IP_INT], int service,
  220. u_char proto, int *records);
  221. snsd_service *andna_resolve_hname(char *hname, int service, u_char proto,
  222. int *records);
  223. int andna_recv_resolve_rq(PACKET rpkt);
  224. lcl_cache *andna_reverse_resolve(inet_prefix ip);
  225. int andna_recv_rev_resolve_rq(PACKET rpkt);
  226. int spread_single_acache(u_int hash[MAX_IP_INT]);
  227. int recv_spread_single_acache(PACKET rpkt);
  228. andna_cache *get_single_andna_c(u_int hash[MAX_IP_INT], u_int hash_gnode[MAX_IP_INT]);
  229. int put_single_acache(PACKET rpkt);
  230. int put_andna_cache(PACKET rq_pkt);
  231. int put_counter_cache(PACKET rq_pkt);
  232. void *andna_hook(void *);
  233. void andna_update_hnames(int only_new_hname);
  234. void *andna_maintain_hnames_active(void *null);
  235. void *andna_main(void *);
  236. #endif /*ANDNA_H*/