The Netsukuku Project  0.0.9
An Alternative routing method
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
gmap.h
Go to the documentation of this file.
1 /* This file is part of Netsukuku
2  * (c) Copyright 2004 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 GMAP_H
20 #define GMAP_H
21 
22 #include "llist.c"
23 #include "map.h"
24 
25 /* * * Groupnode stuff * * */
26 #define GMAP_ME MAP_ME /*1*/
27 #define GMAP_VOID MAP_VOID /*(1<<1)*/
28 #define GMAP_HGNODE (1<<2) /*Hooked Gnode. We already hooked at
29  this gnode */
30 #define GMAP_FULL (1<<3) /*The gnode is full!! aaahh, run away!*/
31 
32 /* This is the holy external_map. Each struct corresponds to a groupnode.
33  * This groupnode cointains MAXGROUPNODE nodes if we are at level 1 or
34  * MAXGROUPNODE groups. The map is equal to the int_map, in fact, a map_node
35  * is embedded in a map_gnode.
36  * This int_map uses the QSPN_MAP_STYLEII (see qspn.h). */
37 typedef struct
38 {
39  /*
40  * The gnode_map starts here. Note that it is a normal map. (See map.h).
41  * It is here, at the top of the struct to allow to manipulate a map_gnode
42  * as a map_node with the help of the magic cast. The cast is heavily
43  * used in qspn.c
44  */
46 
47  u_char flags;
48  u_char seeds; /*The number of active static nodes connected to this
49  gnode minus one (the root_node is not counted).
50  If seeds == MAXGROUPNODE-1, the gnode is full ^_^*/
51  u_int gcount; /*The total number of nodes which are inside this
52  gnode*/
53 } map_gnode;
54 
56  { INT_TYPE_32BIT },
57  { MAP_NODE_PACK_SZ+sizeof(u_char)*2 },
58  { 1 }
59  };
60 #define MAP_GNODE_PACK_SZ (MAP_NODE_PACK_SZ+sizeof(u_char)*2+sizeof(int))
61 
62 
63 /*
64  * * * * Levels notes * * *
65  *
66  * These are the levels of the external_map. Note that the 0 level is never used
67  * for the ext_map because it corresponds to the internal map. Btw the 0 level is
68  * counted so the number of LEVELS includes it too.
69  * But we have to add another extra level: the last exiled level. It is also never
70  * used but it is vital, cause, its gnode 0 includes the entire Netsukuku, the other
71  * gnodes aren't used, it is a mere symbol. We call it the unity level.
72  *
73  * All the structs/arrays related to the external map, and the ext_map itself, don't
74  * use the EXTRA_LEVELS, thus, they lack of the zero level. To retrieve the position
75  * in the array from the level the _EL macro must be used. In other words:
76  * because the arrays goes from 0 to n-1 we refer to the levels as the arrays,
77  * so the level 1 is the level 0, the level 2 is the level 1, and so on.
78  * These arrays/structs are: quadg.gnode, rblock, ext_map, qspn_gnode_count.
79  */
80 #define ZERO_LEVEL 1
81 #define UNITY_LEVEL 1
82 #define EXTRA_LEVELS (ZERO_LEVEL + UNITY_LEVEL)
83 /* To use the right level. */
84 #define _EL(level) ((level)-1)
85 /* And to restore it. */
86 #define _NL(level) ((level)+1)
87 
88 /*
89  * Using MAXGROUPNODE = 2^8; IPV4_LEVELS = 3; ips = 2^32;
90  * ips/(MAXGROUPNODE^IPV4_LEVELS) == 256;
91  * If we use IPV4_LEVELS = 3, we almost cover all the ips, but the division gives
92  * 256. So there are only 256 groups in the last level (3), in fact:
93  * ips/(256 * (MAXGROUPNODE^3)) == 1
94  * And to include them we use the unity level, thus IPV4_LEVELS is equal to 3+1.
95  * This means that the unity level is the one which has only one group node which includes
96  * the entire network.
97  * Sadly we cannot use all this ips, because there are the banned classes (MULTICAST,
98  * ZERONET), the kernel will sput on us.
99  *
100  * For the ipv6 we have IPV6_LEVELS = 16, ips = 2^128; so:
101  * ips/(MAXGROUPNODE^16) == 1
102  */
103 #define IPV4_LEVELS (2+EXTRA_LEVELS)
104 
105 #define IPV6_LEVELS (14+EXTRA_LEVELS)
106 
107 #define MAX_LEVELS IPV6_LEVELS
108 #ifdef DEBUG
109 #define GET_LEVELS(family) \
110 ({ \
111  if((family) != AF_INET && (family) != AF_INET6) \
112  fatal("GET_LEVELS: family not specified!"); \
113  (family) == AF_INET ? IPV4_LEVELS : IPV6_LEVELS; \
114  })
115 #else
116 #define GET_LEVELS(family) ({ (family)==AF_INET ? IPV4_LEVELS : IPV6_LEVELS; })
117 #endif
118 
119 #define FAMILY_LVLS (GET_LEVELS(my_family))
120 
121 /* NODES_PER_LEVEL: returns the maximum number of nodes which can reside in
122  * a gnode of the `lvl'th level */
123 #define NODES_PER_LEVEL(lvl) ((1<<(MAXGROUPNODE_BITS*(lvl))))
124 
125 /* Struct used to keep all the quadro_group ids of a node. (The node is part of this
126  * quadro groups) */
127 typedef struct {
128  u_char levels; /*How many levels we have*/
129  int gid[MAX_LEVELS]; /*Group ids. Each element is the gid of the quadrogroup in the
130  relative level. (ex: gid[n] is the gid of the quadropgroup a
131  the n-th level)*/
132  map_gnode *gnode[MAX_LEVELS-ZERO_LEVEL]; /*Each element is a pointer to the relative
133  gnode in the ext_map.*/
134  inet_prefix ipstart[MAX_LEVELS]; /*The ipstart of each quadg.gid in their respective levels*/
135 }quadro_group;
136 
137 /* Note: this is the int_info of the a packed quadro_group struct, which
138  * hasnt't the `map_gnode *gnode' pointers. The ipstart structs must be also
139  * packed with pack_inet_prefix() */
141  { INT_TYPE_32BIT },
142  { sizeof(u_char) },
143  { MAX_LEVELS }
144  };
145 #define QUADRO_GROUP_PACK_SZ (sizeof(u_char) + sizeof(int)*MAX_LEVELS + \
146  + INET_PREFIX_PACK_SZ * MAX_LEVELS)
147 
148 /*These are the flags passed to iptoquadg()*/
149 #define QUADG_IPSTART 1
150 #define QUADG_GID (1<<1)
151 #define QUADG_GNODE (1<<2)
152 
153 /* This block is used to send the ext_map */
155 {
156  char quadg[QUADRO_GROUP_PACK_SZ]; /* The packed me.cur_quadg */
157 
158  size_t ext_map_sz; /*It's the sum of all the gmaps_sz.
159  The size of a single map is:
160  (ext_map_sz/(MAP_GNODE_PACK_SZ*
161  (quadg.levels-EXTRA_LEVELS)); */
162  size_t rblock_sz[MAX_LEVELS]; /*The size of the rblock of each gmap*/
163  size_t total_rblock_sz; /*The sum of all rblock_sz*/
164 }_PACKED_;
165 
166 /* Note: You have to consider the quadro_group struct when convert between
167  * endianness */
169  { INT_TYPE_32BIT, INT_TYPE_32BIT, INT_TYPE_32BIT },
171  QUADRO_GROUP_PACK_SZ+sizeof(size_t),
172  QUADRO_GROUP_PACK_SZ+(sizeof(size_t)*(MAX_LEVELS+1)) },
173  { 1, MAX_LEVELS, 1 }
174  };
175 
176 /* The ext_map_block is:
177  * struct ext_map_hdr hdr;
178  * char ext_map[ext_map_sz];
179  * char rnode_blocks[total_rblock_sz];
180  */
181 #define EXT_MAP_BLOCK_SZ(ext_map_sz, rblock_sz) (sizeof(struct ext_map_hdr)+(ext_map_sz)+(rblock_sz))
182 
183 /*
184  * This struct is used by the root_node to describe all the rnodes which
185  * doesn't belongs to our same gnode.
186  */
187 typedef struct {
189  quadro_group quadg; /* quadg.gnode[level] may be set to 0
190  * if that gnode doesn't belong to the
191  * same upper level of me.cur_quadg:
192  * quadg.gid[level+1] != me.cur_quadg.gid[level+1]
193  */
194 }ext_rnode;
195 
196 /*This cache keeps the list of all the ext_rnode used.*/
199 
200  ext_rnode *e; /*The pointer to the ext_rnode struct*/
201  int rnode_pos; /*The ext_rnode position in the
202  array of rnodes of the root_node */
203 };
205 
206 /* * * Functions' declaration * * */
207 inline int get_groups(int family, int lvl);
208 int is_group_invalid(int *gids, int gid, int lvl, int family);
209 
210 int pos_from_gnode(map_gnode *gnode, map_gnode *map);
211 map_gnode * gnode_from_pos(int pos, map_gnode *map);
212 void rnodetoip(u_int mapstart, u_int maprnode, inet_prefix ipstart, inet_prefix *ret);
213 const char *rnode_to_ipstr(u_int mapstart, u_int maprnode, inet_prefix ipstart);
214 int iptogid(inet_prefix *ip, int level);
215 void iptogids(inet_prefix *ip, int *gid, int levels);
216 void gidtoipstart(int *gid, u_char total_levels, u_char levels, int family,
217  inet_prefix *ip);
218 void iptoquadg(inet_prefix ip, map_gnode **ext_map, quadro_group *qg, char flags);
219 
220 void quadg_setflags(quadro_group *qg, char flags);
221 void quadg_free(quadro_group *qg);
222 void quadg_destroy(quadro_group *qg);
223 void gnode_inc_seeds(quadro_group *qg, int level);
224 void gnode_dec_seeds(quadro_group *qg, int level);
225 void pack_quadro_group(quadro_group *qg, char *pack);
226 void unpack_quadro_group(quadro_group *qg, char *pack);
227 
228 int free_gids(quadro_group *qg, int level, map_gnode **ext_map, map_node *int_map);
229 int void_gids(quadro_group *qg, int level, map_gnode **ext_map, map_node *int_map);
230 
231 int random_ip(inet_prefix *ipstart, int final_level, int final_gid,
232  int total_levels, map_gnode **ext_map, int only_free_gnode,
233  inet_prefix *new_ip, int my_family);
234 void gnodetoip(quadro_group *quadg, int gnodeid, u_char level, inet_prefix *ip);
235 int gids_cmp(int *gids_a, int *gids_b, int lvl, int max_lvl);
236 int quadg_gids_cmp(quadro_group a, quadro_group b, int lvl);
237 int ip_gids_cmp(inet_prefix a, inet_prefix b, int lvl);
239 void e_rnode_del(ext_rnode_cache **erc_head, u_int *counter, ext_rnode_cache *erc);
240 void e_rnode_add(ext_rnode_cache **erc, ext_rnode *e_rnode, int rnode_pos, u_int *counter);
241 ext_rnode_cache *e_rnode_init(u_int *counter);
242 void e_rnode_free(ext_rnode_cache **erc, u_int *counter);
244 void erc_update_rnodepos(ext_rnode_cache *erc, map_node *root_node, int old_rnode_pos);
245 void erc_reorder_rnodepos(ext_rnode_cache **erc, u_int *erc_counter, map_node *root_node);
246 ext_rnode_cache *erc_find_gnode(ext_rnode_cache *erc, map_gnode *gnode, u_char level);
247 
248 map_gnode *init_gmap(int groups);
249 void reset_gmap(map_gnode *gmap, int groups);
250 map_gnode **init_extmap(u_char levels, int groups);
251 void free_extmap(map_gnode **ext_map, u_char levels, int groups);
252 void reset_extmap(map_gnode **ext_map, u_char levels, int groups);
253 
254 int g_rnode_find(map_gnode *gnode, map_gnode *n);
255 int extmap_find_level(map_gnode **ext_map, map_gnode *gnode, u_char max_level);
256 void gmap_node_del(map_gnode *gnode);
257 
258 int merge_ext_maps(map_gnode **base, map_gnode **new, quadro_group base_root,
259  quadro_group new_root);
260 
261 int verify_ext_map_hdr(struct ext_map_hdr *emap_hdr, quadro_group *quadg);
262 void free_extmap_rblock(map_rnode **rblock, u_char levels);
263 void pack_map_gnode(map_gnode *gnode, char *pack);
264 void unpack_map_gnode(map_gnode *gnode, char *pack);
265 char *pack_extmap(map_gnode **ext_map, int maxgroupnode, quadro_group *quadg, size_t *pack_sz);
266 map_gnode **unpack_extmap(char *package, quadro_group *quadg);
267 int save_extmap(map_gnode **ext_map, int maxgroupnode, quadro_group *quadg, char *file);
268 map_gnode **load_extmap(char *file, quadro_group *quadg);
269 
270 #endif /*GMAP_H*/
void reset_gmap(map_gnode *gmap, int groups)
Definition: gmap.c:926
int random_ip(inet_prefix *ipstart, int final_level, int final_gid, int total_levels, map_gnode **ext_map, int only_free_gnode, inet_prefix *new_ip, int my_family)
Definition: gmap.c:531
void erc_update_rnodepos(ext_rnode_cache *erc, map_node *root_node, int old_rnode_pos)
Definition: gmap.c:779
map_node node
Definition: gmap.h:188
void unpack_quadro_group(quadro_group *qg, char *pack)
Definition: gmap.c:350
int iptogid(inet_prefix *ip, int level)
Definition: gmap.c:159
void gmap_node_del(map_gnode *gnode)
Definition: gmap.c:1023
Definition: gmap.h:154
static const int_info quadro_group_iinfo
Definition: gmap.h:140
u_char levels
Definition: gmap.h:128
char * pack_extmap(map_gnode **ext_map, int maxgroupnode, quadro_group *quadg, size_t *pack_sz)
Definition: gmap.c:1320
#define QUADRO_GROUP_PACK_SZ
Definition: gmap.h:145
map_gnode ** init_extmap(u_char levels, int groups)
Definition: gmap.c:941
void pack_quadro_group(quadro_group *qg, char *pack)
Definition: gmap.c:323
int free_gids(quadro_group *qg, int level, map_gnode **ext_map, map_node *int_map)
Definition: gmap.c:496
void quadg_setflags(quadro_group *qg, char flags)
Definition: gmap.c:264
void erc_reorder_rnodepos(ext_rnode_cache **erc, u_int *erc_counter, map_node *root_node)
Definition: gmap.c:812
Definition: map.h:74
int merge_ext_maps(map_gnode **base, map_gnode **new, quadro_group base_root, quadro_group new_root)
Definition: gmap.c:1113
int ip_gids_cmp(inet_prefix a, inet_prefix b, int lvl)
Definition: gmap.c:709
quadro_group quadg
Definition: gmap.h:189
int is_group_invalid(int *gids, int gid, int lvl, int family)
Definition: gmap.c:45
ext_rnode_cache * erc_find(ext_rnode_cache *erc, ext_rnode *e_rnode)
Definition: gmap.c:842
Definition: map.h:125
u_char flags
Definition: gmap.h:47
map_gnode * gnode_from_pos(int pos, map_gnode *map)
Definition: gmap.c:119
void gidtoipstart(int *gid, u_char total_levels, u_char levels, int family, inet_prefix *ip)
Definition: gmap.c:203
#define MAX_LEVELS
Definition: gmap.h:107
void unpack_map_gnode(map_gnode *gnode, char *pack)
Definition: gmap.c:1291
void free_extmap_rblock(map_rnode **rblock, u_char levels)
Definition: gmap.c:1248
void rnodetoip(u_int mapstart, u_int maprnode, inet_prefix ipstart, inet_prefix *ret)
Definition: gmap.c:130
Definition: inet.h:73
Definition: gmap.h:127
int get_groups(int family, int lvl)
Definition: gmap.c:34
void e_rnode_add(ext_rnode_cache **erc, ext_rnode *e_rnode, int rnode_pos, u_int *counter)
Definition: gmap.c:745
int gids_cmp(int *gids_a, int *gids_b, int lvl, int max_lvl)
Definition: gmap.c:679
int extmap_find_level(map_gnode **ext_map, map_gnode *gnode, u_char max_level)
Definition: gmap.c:1006
size_t ext_map_sz
Definition: gmap.h:158
void gnode_inc_seeds(quadro_group *qg, int level)
Definition: gmap.c:291
u_char seeds
Definition: gmap.h:48
struct ext_map_hdr _PACKED_
int pos_from_gnode(map_gnode *gnode, map_gnode *map)
Definition: gmap.c:109
void gnodetoip(quadro_group *quadg, int gnodeid, u_char level, inet_prefix *ip)
Definition: gmap.c:656
size_t total_rblock_sz
Definition: gmap.h:163
map_gnode ** unpack_extmap(char *package, quadro_group *quadg)
Definition: gmap.c:1382
Definition: gmap.h:197
void pack_map_gnode(map_gnode *gnode, char *pack)
Definition: gmap.c:1264
const char * rnode_to_ipstr(u_int mapstart, u_int maprnode, inet_prefix ipstart)
Definition: gmap.c:144
int rnode_pos
Definition: gmap.h:201
int save_extmap(map_gnode **ext_map, int maxgroupnode, quadro_group *quadg, char *file)
Definition: gmap.c:1433
ext_rnode_cache * erc_find_gnode(ext_rnode_cache *erc, map_gnode *gnode, u_char level)
Definition: gmap.c:888
char quadg[(sizeof(u_char)+sizeof(int)*(14+(1+1))++(sizeof(u_char)+sizeof(u_short)+sizeof(u_char)+(4 *sizeof(int)))*(14+(1+1)))]
Definition: gmap.h:156
#define ZERO_LEVEL
Definition: gmap.h:80
map_node * int_map
Definition: qspn-empiric.h:122
ext_rnode * e
Definition: gmap.h:200
void iptoquadg(inet_prefix ip, map_gnode **ext_map, quadro_group *qg, char flags)
Definition: gmap.c:237
int verify_ext_map_hdr(struct ext_map_hdr *emap_hdr, quadro_group *quadg)
Definition: gmap.c:1229
int my_family
Definition: inet.h:141
static const int_info ext_map_hdr_iinfo
Definition: gmap.h:168
map_gnode * init_gmap(int groups)
Definition: gmap.c:910
void gnode_dec_seeds(quadro_group *qg, int level)
Definition: gmap.c:307
ext_rnode_cache * e_rnode_init(u_int *counter)
Definition: gmap.c:724
size_t rblock_sz[(14+(1+1))]
Definition: gmap.h:162
int void_gids(quadro_group *qg, int level, map_gnode **ext_map, map_node *int_map)
Definition: gmap.c:508
#define INT_INFO
Definition: endianness.h:90
void reset_extmap(map_gnode **ext_map, u_char levels, int groups)
Definition: gmap.c:983
map_gnode ** load_extmap(char *file, quadro_group *quadg)
Definition: gmap.c:1456
int quadg_gids_cmp(quadro_group a, quadro_group b, int lvl)
Definition: gmap.c:696
void quadg_free(quadro_group *qg)
Definition: gmap.c:274
static const int_info map_gnode_iinfo
Definition: gmap.h:55
#define LLIST_HDR(_struct)
Definition: llist.c:44
int family
Definition: if.c:34
int flags
Definition: if.c:39
void free_extmap(map_gnode **ext_map, u_char levels, int groups)
Definition: gmap.c:962
#define MAP_NODE_PACK_SZ
Definition: map.h:142
void quadg_destroy(quadro_group *qg)
Definition: gmap.c:279
ext_rnode_cache * e_rnode_find(ext_rnode_cache *erc, quadro_group *qg, int level)
Definition: gmap.c:867
Definition: gmap.h:187
#define INT_TYPE_32BIT
Definition: endianness.h:35
void iptogids(inet_prefix *ip, int *gid, int levels)
Definition: gmap.c:185
int g_rnode_find(map_gnode *gnode, map_gnode *n)
Definition: gmap.c:995
Definition: gmap.h:37
void e_rnode_free(ext_rnode_cache **erc, u_int *counter)
Definition: gmap.c:730
map_node g
Definition: gmap.h:45
void e_rnode_del(ext_rnode_cache **erc_head, u_int *counter, ext_rnode_cache *erc)
Definition: gmap.c:756
u_int gcount
Definition: gmap.h:51