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.

gmap.h 11KB

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