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