forked from wujingbang/aodv-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
st_rreq.c
executable file
·139 lines (113 loc) · 3.56 KB
/
st_rreq.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/***************************************************************************
Modified by Miguel Catalan Cid - [email protected] - Version: Mon Jan 1 2010
***************************************************************************/
//MCC - ST_RREQ
#include "st_rreq.h"
extern u_int8_t g_aodv_gateway;
extern u_int32_t g_broadcast_ip;
extern u_int8_t g_hopcnt_routing;
extern u_int32_t g_null_ip;
extern u_int8_t g_routing_metric;
extern u_int32_t g_mesh_ip;
void convert_rreq_to_host_st(rreq_st * tmp_rreq) {
tmp_rreq->dst_id = ntohl(tmp_rreq->dst_id);
tmp_rreq->path_metric = ntohl(tmp_rreq->path_metric);
}
void convert_rreq_to_network_st(rreq_st * tmp_rreq) {
tmp_rreq->dst_id = htonl(tmp_rreq->dst_id);
tmp_rreq->path_metric = htonl(tmp_rreq->path_metric);
}
/*
* Generates a ST-RREQ
*/
int gen_st_rreq(void) {
aodv_route *tmp_route;
rreq_st *out_rreq;
u_int8_t out_ttl;
if (!g_aodv_gateway) {
#ifdef DEBUG
printk( "kernel_aodv: Shouldn't be sending st_rreq when not gateway\n");
#endif
return 0;
}
// Allocate memory for the rreq message
if ((out_rreq = (rreq_st *) kmalloc(sizeof(rreq_st), GFP_ATOMIC)) == NULL) {
#ifdef DEBUG
printk( "AODV: Can't allocate new rreq\n");
#endif
return 0;
}
tmp_route = find_aodv_route(g_mesh_ip, g_mesh_ip, 0);
if (tmp_route == NULL) {
#ifdef DEBUG
printk( "AODV: Can't get route to myself!!\n");
#endif
kfree(out_rreq);
return -EHOSTUNREACH;
}
tmp_route->dst_id = tmp_route->dst_id +1;
out_rreq->dst_id = tmp_route->dst_id;
out_rreq->u = TRUE;
out_ttl = NET_DIAMETER;
// Fill in the package
out_rreq->gw_ip = g_mesh_ip;
out_rreq->type = ST_RREQ_MESSAGE;
out_rreq->num_hops = 0;
out_rreq->path_metric = 0;
out_rreq->j = 0;
out_rreq->r = 0;
out_rreq->d = 1;
out_rreq->reserved = 1;
out_rreq->second_reserved = 0; // set to 1 for hopcnt routing
out_rreq->g = 0;
convert_rreq_to_network_st(out_rreq);
local_broadcast(out_ttl, out_rreq, sizeof(rreq_st));
kfree(out_rreq);
insert_timer_simple(TASK_ST, ST_INTERVAL, g_mesh_ip);
update_timer_queue();
return 0;
}
int recv_rreq_st(task * tmp_packet) {
rreq_st *tmp_rreq;
aodv_neigh *tmp_neigh;
tmp_rreq = tmp_packet->data;
convert_rreq_to_host_st(tmp_rreq);
tmp_neigh = find_aodv_neigh(tmp_packet->src_ip);
if (tmp_neigh == NULL) {
#ifdef DEBUG
printk( "Ignoring RREQ_BG received from unknown neighbor\n");
#endif
return 1;
}
//Update neighbor timelife
delete_timer(tmp_neigh->ip, tmp_neigh->ip, NO_TOS, TASK_NEIGHBOR);
insert_timer_simple(TASK_NEIGHBOR, HELLO_INTERVAL * (1 + ALLOWED_HELLO_LOSS) + 100, tmp_neigh->ip);
update_timer_queue();
tmp_neigh->lifetime = HELLO_INTERVAL * (1 + ALLOWED_HELLO_LOSS) + 20
+ getcurrtime();
if ((g_mesh_ip == tmp_rreq->gw_ip)) {
#ifdef DEBUG
printk("I'm the generator of the incoming ST-RREQ - Discarted\n");
#endif
return 1;
}
tmp_rreq->num_hops++;
if (g_routing_metric == HOPS)
tmp_rreq->path_metric = tmp_rreq->num_hops;
else {
if (tmp_neigh->recv_rate == 0 || tmp_neigh->send_rate == 0 || tmp_neigh->etx_metric == 0){
#ifdef DEBUG
printk("Metric too high - ST-RREQ Discarted\n");
#endif
return 1;
}
tmp_rreq->path_metric +=(PROBE_PACKET*1000)/(tmp_neigh->recv_rate*tmp_neigh->etx_metric); //ETT in microseconds
}
if (update_gw(tmp_rreq->gw_ip, tmp_rreq->dst_id, tmp_rreq->num_hops,
tmp_rreq->path_metric)) {
convert_rreq_to_network_st(tmp_rreq);
local_broadcast(tmp_packet->ttl - 1, tmp_rreq, sizeof(rreq_st));
return 0;
}
return 1;
}