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