1 /*
2 *
3 * Copyright (c) 2003 The Regents of the University of California. All
4 * rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * - Neither the name of the University nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
25 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 */
30
31
32 /* needs
33 * #include <netinet/in.h>
34 * #include <glib.h>
35 */
36 #ifndef COMMON_H_
37 #define COMMON_H_
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <assert.h>
42 #include <limits.h>
43 #include <math.h>
44 #include <string.h>
45
46 #include <sys/types.h>
47 #include <netinet/in.h>
48
49 #include <emrun/emrun.h>
50 #include <link/link.h>
51 #include <link/neighbor.h>
52
53 #include <libmisc/local_types.h>
54 #include <libmisc/misc.h>
55
56 #include <libdev/status_dev.h>
57 #include <glib.h>
58 #include <libdev/glib_dev.h>
59
60 #ifdef __cplusplus
61 extern "C"
62 {
63 #endif
64
65 #define USEC_IN_SEC 1000000
66
67 typedef void *(* VOIDFUNCPTR)(void *);
68 typedef double sample_t;
69
70 /* default udp port to use */
71 #define DEFAULT_UDP_PORT 4444
72
73 /* these defines tell us whether some floating type is a zero, or not */
74 #define IS_FLOAT_ZERO(FLT) (fabs(FLT) < FLT_EPSILON)
75 #define IS_DOUBLE_ZERO(DBL) (fabs(DBL) < DBL_EPSILON)
76
77 /* takes a floating point type, and rounds it to the nearest integer,
78 * which is then returned. casting floating points to integers just "throws
79 * away" everything after decimal */
80 #define ROUND_TO_INT(NUM) ((int)((NUM) + 0.5))
81 #define ROUND_TO_UINT(NUM) ((unsigned int)((NUM) + 0.5))
82 #define ROUND_TO_LONG(NUM) ((long)((NUM) + 0.5))
83 #define ROUND_TO_ULONG(NUM) ((unsigned long)((NUM) + 0.5))
84 #define ROUND_TO_INT64(NUM) ((int64_t)((NUM) + 0.5))
85
86 #ifndef MIN
87 # define MIN(A,B) ((A) < (B) ? (A) : (B)) /* minimum of two numbers */
88 #endif
89
90 #ifndef MAX
91 # define MAX(A,B) ((A) > (B) ? (A) : (B)) /* maximum of two numbers */
92 #endif
93
94 #ifndef EUCLIDEAN_DIST
95 #define EUCLIDEAN_DIST(A,B) \
96 sqrt(pow((double)(B.x-A.x),2) + pow((double)(B.y-A.y),2))
97 #endif
98
99 /* swap a and b, using t as a temporary */
100 #define SWAP(A,B,T) \
101 do \
102 { \
103 (T) = (A); \
104 (A) = (B); \
105 (B) = (T); \
106 } \
107 while(0)
108
109 /* return type of functions.
110 * RES_TRUE - everything was fine
111 * RES_FALSE - function failed, but in a "normal", or expected, manner
112 * (i.e. did not find an item in a table)
113 * RES_ERROR - there was an error (unexpected) during function processing
114 */
115 enum STATUS {RES_TRUE, RES_FALSE, RES_ERROR};
116
117
118 /*
119 * Information about neighbors of a node.
120 * XXX: Currently if_id is communicated around and stored in the
121 * neighbor_list of every node. This shd be eliminated, and only
122 * necessary fields should be passed around.
123 */
124 typedef struct gls_neighbor
125 {
126 /*
127 * NOTE: hash table manipulation functions rely on 'id' being the first
128 * member. so don't change this.
129 */
130 unsigned long int id;
131 // if_id_t if_id; /* Neighbor's network interface ID */
132 /* weight of the path to this node */
133 unsigned long int weight;
134 } gls_neighbor_t;
135
136 /* when udp is used, this structure keeps track of addressing information */
137 struct udp_info
138 {
139 struct in_addr ip;
140 ushort port;
141 } __attribute__((packed));
142
143 typedef struct mygeo {
144 uint16_t x;
145 uint16_t y;
146 } __attribute__((packed)) geo_t;
147
148 static inline float loc_expand(uint16_t loc)
149 {
150 return (float)(loc/100.0);
151 }
152
153 static inline uint16_t loc_trunc(float loc)
154 {
155 return (uint16_t) (loc * 100.0);
156 }
157
158 /*
159 * Node configuration structure
160 */
161
162 struct nodeconf
163 {
164 /* NOTE: hash table manipulation functions rely on 'id' being the first
165 * member. so don't change this. */
166
167 /* id of this node */
168 uint32_t id;
169 /* whenever udp is used, this structure contains informatin about ip/port of
170 * this client */
171 struct udp_info udp;
172 /* geo-info of the node */
173 geo_t geo;
174 /* neighbor list. Implemented as a hash table, not as a list to allow us to
175 * check whether a particular node is already present in the table or not */
176 GHashTable *neighbor_list;
177 /* this is how we compute weights of paths between the two neighbors. If
178 * this flag is set, we assume weight(A,B) == weight(B,A) == cartesian
179 * distance. Otherwise, we expect the weight to be supplied with every
180 * neighbor on the neighborlist, and we'll use that. the "weight(A,B) ==
181 * weight(B,A)" assumption does not hold anymore */
182 gboolean use_cartesian_symmetric_weights;
183 };
184
185 /* ADDR_UDP sounds a little awkward, but it is there since we'd like to identify
186 * clients by both port and ip address. not only by ip address */
187 #define ADDR_GEO 0
188 #define ADDR_NODEID 1
189 //enum GLS_ADDR_TYPE {ADDR_UDP, ADDR_GEO, ADDR_NODEID, ADDR_UNKNOWN};
190
191 /* structure used to represent an address: may take on several forms, depending
192 * on how address is represented - in terms of the node's coordinates, in terms
193 * of node's ip address, or in terms of nodeId */
194
195 typedef struct gls_addr
196 {
197 uint8_t type;
198 union
199 {
200 geo_t geo;
201 ulong nodeid;
202 }
203 addr;
204 } __attribute__((packed)) gls_addr_t;
205
206 #define gls_addr_geo addr.geo
207 #define gls_addr_udp addr.udp
208 #define gls_addr_nodeid addr.nodeid
209
210 /* returns TRUE if 'num' is power of 2, and FALSE otherwise */
211 gboolean uint_is_power_of_2(uint32_t num);
212
213 /* print the list of neighbors for the node described by struct nodeconf */
214 void print_neighborlist(struct nodeconf *cfg, buf_t *bf);
215 /* print geo information */
216 void print_geo(geo_t *geo, buf_t *bf);
217 /* print udp information (ip and mac address) */
218 void print_udp(struct udp_info *udp);
219 /* print complete node configuration structure (all of the above) */
220 void print_nodeconf(struct nodeconf *cfg);
221 /* print complete global table (if applicable) - calls print_nodeconf on every
222 * node */
223 void print_global_table(GHashTable *table);
224
225 /* given a host name, figure out a corresponding ip (assumes only one ip per
226 * host) ip is returned in network byte order*/
227 enum STATUS get_ip_from_host(const char *host, uint32_t *ip);
228 /* get ip of machine we're running on (in network byte order).
229 * assumes only one ip */
230 enum STATUS get_own_ip(uint32_t *ip);
231 /* figures out the ip address and subnet mask of the primary interface and
232 * returns a host number portion of the ip address (IN HOST BYTE ORDER)
233 * NOTE: (for now ?) it is hardcoded so that it looks for eth0 interface */
234 enum STATUS get_own_host_number(uint32_t *num);
235 /* initialize the nodeconf structure with default values */
236 void init_nodeconf(struct nodeconf *cfg);
237 /* destroy data structures associated with a configuration */
238 void destroy_nodeconf(struct nodeconf *cfg);
239
240 /* This function walks the global table, and checks neighborlists: it screams
241 * bloody murder when it finds a nodeId on the neighborlist without a
242 * corresponding entry in the global node table. That's because when this
243 * happens, it means that whoever made up a config file screwed up */
244 void check_neighbor_consistency(GHashTable *nodeTable);
245
246 /* construct a linked list of neighbors from the hash table, which contains
247 * 'struct neighbor' elements. Individual neighbor elements are constructed (and
248 * must be destroyed later, of course) prior to insertion into the linked list.
249 * Pointer to the first element of the linked list is returned */
250 GSList * alloc_neighbor_list_from_table(GHashTable *neighbor_table);
251
252 /* route_table.c */
253 void print_routing_table(GHashTable *rtable);
254
255 #ifdef __cplusplus
256 }
257 #endif
258
259 #endif /* COMMON_H_ */
260
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.