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.

map.h 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* This file is part of Netsukuku system
  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 MAP_H
  19. #define MAP_H
  20. #include "includes.h"
  21. #include "inet.h"
  22. /* Generic map defines */
  23. #define MAXGROUPNODE_BITS 8 /* 2^MAXGROUPNODE_BITS == MAXGROUPNODE */
  24. #define MAXGROUPNODE (1<<MAXGROUPNODE_BITS)
  25. #define MAXROUTES 20
  26. #define MAXLINKS MAXROUTES
  27. /*** flags ***/
  28. #define MAP_ME 1 /*The root_node, in other words, me ;) */
  29. #define MAP_VOID (1<<1) /*It indicates a non existent node */
  30. #define MAP_HNODE (1<<2) /*Hooking node. The node is currently
  31. hooking */
  32. #define MAP_BNODE (1<<3) /*The node is a border_node. If this
  33. flag is set to a root_node, this means
  34. that we are a bnode at the root_node's
  35. level */
  36. #define MAP_ERNODE (1<<4) /*It is an External Rnode */
  37. #define MAP_GNODE (1<<5) /*It is a gnode */
  38. #define MAP_RNODE (1<<6) /*If a node has this set, it is one of the rnodes */
  39. #define MAP_UPDATE (1<<7) /*If it is set, the corresponding route
  40. in the krnl will be updated */
  41. #define QSPN_CLOSED (1<<8) /*This flag is set only to the rnodes,
  42. it puts a link in a QSPN_CLOSED state */
  43. #define QSPN_OPENED (1<<9) /*It puts a link in a QSPN_OPEN state */
  44. #define QSPN_OLD (1<<10) /*If a node isn't updated by the current
  45. qspn_round it is marked with QSPN_ROUND.
  46. If in the next qspn_round the same node
  47. isn't updated it is removed from the map. */
  48. #define QSPN_STARTER (1<<11) /*The root node is marked with this flag
  49. if it is a qspn_starter */
  50. #define QSPN_OPENER (1<<12) /*If the root_node sent a new qspn_open
  51. it is a qspn_opener */
  52. #define MAP_IGW (1<<13) /*This node is an Internet gateway */
  53. /*\
  54. * *** Map notes ***
  55. *
  56. * The map is an array of MAXGROUPNODE map_node structs. It is a generic map
  57. * and it is used to keep the qspn_map, the internal map and the external map.
  58. * The position in the map of each struct corresponds to its relative ip.
  59. * For example, if the map goes from 192.128.1.0 to 192.128.3.0, the map will
  60. * have 512 structs, the first one will correspond to 192.168.1.0, the 50th to
  61. * 192.168.1.50 and so on.
  62. * Note: because MAXGROUPNODE is 256, we can use an u_char for the index of the
  63. * array.
  64. *
  65. \*/
  66. /* map_rnode is what map_node.r_node points to. (read struct map_node below) */
  67. typedef struct {
  68. int *r_node; /*It's the pointer to the struct of the
  69. r_node in the map */
  70. u_int trtt;
  71. /*
  72. * node <-> root_node total rtt: The rtt to reach the root_node
  73. * starting from the node which uses this rnode (in millisec).
  74. * Cuz I've explained it in such a bad way I make an example:
  75. * map_node node_A; From node_A "node_A.links"th routes to the root_node
  76. * start. So I have "node_A.links"th node_A.r_node[s], each of them is a
  77. * different route to reach the root_node.
  78. * With the node_A.r_node[route_number_to_follow].trtt I can get the rtt
  79. * needed to reach the root_node starting from the node_A using the
  80. * route_number_to_follow. Gotcha? I hope so.
  81. * Note: The trtt is mainly used to sort the routes
  82. */
  83. } map_rnode;
  84. /* Note: This int_info is used for the pack of a map_rnode struct (see
  85. * get_rnode_block()).
  86. * Since the r_node pointer, in the pack, is an integer, we add it in the
  87. * int_info as a normal 32bit int. */
  88. INT_INFO map_rnode_iinfo = { 2,
  89. {INT_TYPE_32BIT, INT_TYPE_32BIT},
  90. {0, sizeof(int)},
  91. {1, 1}
  92. };
  93. #define MAP_RNODE_PACK_SZ (sizeof(int *)+sizeof(u_int))
  94. /*
  95. * ****) The qspn int_map (****
  96. *
  97. * - map_node.r_node points to the r_node of the root_node to be used as
  98. * gateway to reach map_node. So map_node.r_node stores only the gateway
  99. * needed to reach map_node from the root_node.
  100. * The only execption is the root_node itself. The root_node's
  101. * map_node.r_node keeps all its rnodes as a normal (non qspn) map would.
  102. *
  103. * The only exception is the root_node. Its rnodes have a different meaning:
  104. * they are its effective rnodes, so each map_node.r_node points to the node
  105. * which is the real rnode of the root_node.
  106. * The root_node at level 0 may have also rnode of a different gnode
  107. * (it is a border node).
  108. * To store these external rnodes in root_node.r_node[x], the
  109. * root_node.r_node[x].r_node will point to the relative ext_rnode struct
  110. * (see gmap.h) and the MAP_GNODE | MAP_ERNODE flags will be set in
  111. * root_node.r_node[x].flags.
  112. * The rnodes of the root_node of 0 level are updated by the radar(),
  113. * instead the root_nodes of greater levels are updated by the qspn.
  114. */
  115. typedef struct {
  116. u_short flags;
  117. u_int brdcast; /*Pkt_id of the last brdcast_pkt sent by this node */
  118. u_short links; /*Number of r_nodes */
  119. map_rnode *r_node; /*These structs will be kept in ascending
  120. order considering their rnode_t.rtt */
  121. } map_node;
  122. /* Note: This int_info is used for the pack of a map_rnode struct (see
  123. * pack_map()) */
  124. INT_INFO map_node_iinfo = { 3,
  125. {INT_TYPE_16BIT, INT_TYPE_32BIT, INT_TYPE_16BIT},
  126. {0, sizeof(short), sizeof(short) + sizeof(int)},
  127. {1, 1, 1}
  128. };
  129. #define MAP_NODE_PACK_SZ (sizeof(u_short)*2 + sizeof(u_int))
  130. #define MAXRNODEBLOCK (MAXLINKS * MAXGROUPNODE * sizeof(map_rnode))
  131. #define MAXRNODEBLOCK_PACK_SZ (MAXLINKS * MAXGROUPNODE * MAP_RNODE_PACK_SZ)
  132. #define INTMAP_END(mapstart) ((sizeof(map_node)*MAXGROUPNODE)+(mapstart))
  133. /*This block is used to send/save the int_map and the bnode_map*/
  134. struct int_map_hdr {
  135. u_char root_node;
  136. size_t int_map_sz;
  137. size_t rblock_sz;
  138. } _PACKED_;
  139. INT_INFO int_map_hdr_iinfo = { 2,
  140. {INT_TYPE_32BIT, INT_TYPE_32BIT},
  141. {sizeof(char), sizeof(char) + sizeof(size_t)}
  142. ,
  143. {1, 1}
  144. };
  145. /*
  146. * The int_map_block is:
  147. * struct int_map_hdr hdr;
  148. * char map_node[int_map_sz];
  149. * char map_rnode[rblock_sz];
  150. */
  151. #define INT_MAP_BLOCK_SZ(int_map_sz, rblock_sz) (sizeof(struct int_map_hdr)+(int_map_sz)+(rblock_sz))
  152. /*conversion macro*/
  153. #define UINT2NODE(i) ((map_node *)(uintptr_t)(i))
  154. /*
  155. * * * Functions' declaration * * *
  156. */
  157. /*conversion functions*/
  158. int pos_from_node(map_node * node, map_node * map);
  159. map_node *node_from_pos(int pos, map_node * map);
  160. void postoip(u_int map_pos, inet_prefix ipstart, inet_prefix * ret);
  161. void maptoip(uintptr_t mapstart, uintptr_t mapoff, inet_prefix ipstart,
  162. inet_prefix * ret);
  163. int iptomap(uintptr_t mapstart, inet_prefix ip, inet_prefix ipstart,
  164. map_node ** ret);
  165. map_node *init_map(size_t len);
  166. void free_map(map_node * map, size_t count);
  167. void map_node_del(map_node * node);
  168. void reset_int_map(map_node * map, int maxgroupnode);
  169. map_rnode *rnode_insert(map_rnode * buf, size_t pos, map_rnode * new);
  170. map_rnode *map_rnode_insert(map_node * node, size_t pos, map_rnode * new);
  171. map_rnode *rnode_add(map_node * node, map_rnode * new);
  172. void rnode_swap(map_rnode * one, map_rnode * two);
  173. void rnode_del(map_node * node, size_t pos);
  174. void rnode_destroy(map_node * node);
  175. int rnode_find(map_node * node, void *n);
  176. int rnode_trtt_compar(const void *a, const void *b);
  177. void rnode_trtt_order(map_node * node);
  178. void map_routes_order(map_node * map);
  179. u_int get_route_trtt(map_node * node, u_short route);
  180. void rnode_set_trtt(map_node * node);
  181. void rnode_recurse_trtt(map_rnode * rnode, int route,
  182. struct timeval *trtt);
  183. void node_recurse_trtt(map_node * node);
  184. void map_set_trtt(map_node * map);
  185. map_node *get_gw_node(map_node * node, u_short route);
  186. int merge_maps(map_node * base, map_node * new, map_node * base_root,
  187. map_node * new_root);
  188. int mod_rnode_addr(map_rnode * node, int *map_start, int *new_start);
  189. int get_rnode_block(int *map, map_node * node, map_rnode * rblock,
  190. int rstart);
  191. map_rnode *map_get_rblock(map_node * map, int *addr_map, int maxgroupnode,
  192. int *count);
  193. int store_rnode_block(int *map, map_node * node, map_rnode * rblock,
  194. int rstart);
  195. int map_store_rblock(map_node * map, int *addr_map, int maxgroupnode,
  196. map_rnode * rblock);
  197. int verify_int_map_hdr(struct int_map_hdr *imap_hdr, int maxgroupnode,
  198. int maxrnodeblock);
  199. void pack_map_node(map_node * node, char *pack);
  200. void unpack_map_node(map_node * node, char *pack);
  201. char *pack_map(map_node * map, int *addr_map, int maxgroupnode,
  202. map_node * root_node, size_t * pack_sz);
  203. map_node *unpack_map(char *pack, int *addr_map, map_node ** new_root,
  204. int maxgroupnode, int maxrnodeblock);
  205. int save_map(map_node * map, map_node * root_node, char *file);
  206. map_node *load_map(char *file, map_node ** new_root);
  207. #endif /*MAP_H */