-
Notifications
You must be signed in to change notification settings - Fork 1
/
aodv.c
executable file
·151 lines (122 loc) · 4.01 KB
/
aodv.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
140
141
142
143
144
145
146
147
148
149
150
151
/*---------------------------------------------------------------------------*/
/*----------------------------------#DEFINE----------------------------------*/
/*---------------------------------------------------------------------------*/
#define ROUTE_REQ_PACKET_LEN 35
#define ROUTE_REPLY_PACKET_LEN 45
#define DATA_PACKET_LEN 22+11 //header + payload
#define DATA_PAYLOAD_LEN 11 //payload
/*---------------------------------------------------------------------------*/
/*----------------------------------STRUCTS----------------------------------*/
/*---------------------------------------------------------------------------*/
//ROUTING TABLES
struct ROUTING_TABLE_ROW{
int DEST;
int NEXT;
int ACTIVE;
int EXP;
};
struct ROUTING_DISCOVERY_TABLE_ROW{
int REQ_ID;
int SRC;
int DEST;
int SND;
int HOPS;
};
//PACKET TYPES
struct ROUTE_REQ {
int REQ_ID;
int SRC;
int DEST;
};
struct ROUTE_REPLY {
int REQ_ID;
int SRC;
int DEST;
int HOPS;
};
struct DATA {
int DEST;
char PAYLOAD[DATA_PAYLOAD_LEN];
};
//HELPER-STRUCT
struct ROUTE_REQ_INFO {
int REQ_ID;
int SRC;
int DEST;
int SND;
};
/*---------------------------------------------------------------------------*/
//HELPER METHODS
static void routeReqToPacket(struct ROUTE_REQ rreq, char* packet){
sprintf(packet, "ROUTE_REQ;");
sprintf(packet + strlen(packet), "REQ_ID:%2d;", rreq.REQ_ID);
sprintf(packet + strlen(packet), "SRC:%2d;", rreq.SRC);
sprintf(packet + strlen(packet), "DEST:%2d;", rreq.DEST);
}
static void routeReplyToPacket(struct ROUTE_REPLY rreply, char* packet){
sprintf(packet, "ROUTE_REPLY;");
sprintf(packet + strlen(packet), "REQ_ID:%2d;", rreply.REQ_ID);
sprintf(packet + strlen(packet), "SRC:%2d;", rreply.SRC);
sprintf(packet + strlen(packet), "DEST:%2d;", rreply.DEST);
sprintf(packet + strlen(packet), "HOPS:%2d;", rreply.HOPS);
}
static void dataToPacket(struct DATA data, char* packet){
sprintf(packet, "DATA;");
sprintf(packet + strlen(packet), "DEST:%2d;", data.DEST);
sprintf(packet + strlen(packet), "PAYLOAD:%s", data.PAYLOAD);
}
//ROUTE_REPLY;REQ_ID:xx;SRC:yy;DEST:zz;HOPS:vv;
static char packetToRouteReply(char* packet, struct ROUTE_REPLY * rreply){
static char reqId[2];
static char src[2];
static char dest[2];
static char hops[2];
if( strncmp(packet,"ROUTE_REPLY;", 12) == 0){
reqId[0] = packet[19];
reqId[1] = packet[20];
src[0] = packet[26];
src[1] = packet[27];
dest[0] = packet[34];
dest[1] = packet[35];
hops[0] = packet[42];
hops[1] = packet[43];
rreply->REQ_ID = atoi(reqId);
rreply->SRC = atoi(src);
rreply->DEST = atoi(dest);
rreply->HOPS = atoi(hops);
return 1;
}
return 0;
}
//DATA;DEST:xx;PAYLOAD:etcetcetc....;
static char packetToData(char* packet, struct DATA * data_msg){
static char dest[2];
if( strncmp(packet, "DATA;", 5) == 0){
//strncpy(dest, packet+10, 2);
dest[0] = packet[10];
dest[1] = packet[11];
data_msg->DEST = atoi(dest);
strncpy(data_msg->PAYLOAD, packet+21, DATA_PAYLOAD_LEN);
return 1;
}return 0;
}
//ROUTE_REQ;REQ_ID:xx;SRC:yy;DEST:zz;
static char packetToRouteReq(char* packet, struct ROUTE_REQ * rreq){
static char reqId[2];
static char src[2];
static char dest[2];
if( strncmp(packet,"ROUTE_REQ;", 10) == 0){
reqId[0] = packet[17];
reqId[0] = packet[18];
src[0] = packet[24];
src[0] = packet[25];
dest[0] = packet[32];
dest[1] = packet[33];
rreq->REQ_ID = atoi(reqId);
rreq->SRC = atoi(src);
rreq->DEST = atoi(dest);
return 1;
}
return 0;
}
/*---------------------------------------------------------------------------*/