00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <linux/if_ether.h>
00034
00035 #include "RouteResponse.h"
00036
00037 #include "defines.h"
00038 #include "functions.h"
00039
00040 RouteResponse::RouteResponse(unsigned char* buffer)
00041 {
00042
00043 unsigned char* buffer_start = buffer;
00044
00045 unsigned char* mac_list_pos = buffer + 29;
00046 ethhdr* eh = (struct ethhdr *) buffer;
00047 buffer += 15;
00048
00049
00050
00051 memcpy(&this->eh_, &(*eh), sizeof (ethhdr));
00052
00053
00054
00055
00056
00057 uint32_t num_from_network;
00058 memcpy((unsigned char*) &num_from_network, (unsigned char*) buffer, 4);
00059 request_id_ = ntohl(num_from_network);
00060 buffer += 4;
00061
00062
00063 memcpy((unsigned char*) &mc_flag_, (unsigned char*) buffer, 1);
00064 buffer += 1;
00065
00066
00067 memcpy((unsigned char*) &root_distance, (unsigned char*) buffer, 1);
00068 buffer += 1;
00069
00070
00071 memcpy((unsigned char*) &num_from_network, (unsigned char*) buffer, 4);
00072 hop_count_ = ntohl(num_from_network);
00073 buffer += 4;
00074
00075
00076 memcpy((unsigned char*) &num_from_network, (unsigned char*) buffer, 4);
00077 current_hop_ = ntohl(num_from_network);
00078 current_hop_++;
00079 buffer += 4;
00080
00081
00082 for (uint32_t i = 0; i < hop_count_; i++)
00083 {
00084 mac currentMac;
00085 memcpy((unsigned char*) currentMac.mac_adr, (unsigned char*) buffer, 6);
00086 path_l_.push_back(currentMac);
00087 buffer += 6;
00088 }
00089
00090
00091
00092 memcpy((unsigned char*) &num_from_network, (unsigned char*) buffer, 4);
00093 uint32_t hostname_source_Len = ntohl(num_from_network);
00094 buffer += 4;
00095
00096
00097 hostname_source_ = "";
00098 for (uint32_t i = 0; i < hostname_source_Len; i++)
00099 {
00100 hostname_source_.append((const char*) buffer, 1);
00101 buffer++;
00102 }
00103
00104
00105 uint32_t crc = 0;
00106 memcpy((unsigned char*) &num_from_network, (unsigned char*) buffer, 4);
00107 crc = ntohl(num_from_network);
00108
00109 std::string crc_data_string = "";
00110 crc_data_string.append((const char*) buffer_start, RouteResponse::HEADER_FIXED_LEN - 4 + hostname_source_.length() + 6 * hop_count_);
00111
00112 if (crc == (uint32_t) GetCrc32(crc_data_string))
00113 correct_crc_ = true;
00114 else
00115 correct_crc_ = false;
00116
00117
00118 unsigned char* mac_current_hop_Pos = mac_list_pos;
00119 uint32_t relativeMacPostion;
00120 if (hop_count_ >= current_hop_)
00121 relativeMacPostion = hop_count_ - current_hop_;
00122 else
00123 relativeMacPostion = 0;
00124
00125 mac_current_hop_Pos += (relativeMacPostion * 6);
00126
00127 memcpy((void*) mac_current_hop_, (unsigned char*) mac_current_hop_Pos, 6);
00128
00129
00130 mac_current_hop_Pos = mac_list_pos;
00131 if (hop_count_ - 1 >= current_hop_)
00132 relativeMacPostion = hop_count_ - 1 - current_hop_;
00133 else
00134 relativeMacPostion = 0;
00135
00136 mac_current_hop_Pos += (relativeMacPostion * 6);
00137
00138 memcpy((void*) mac_previous_hop_, (unsigned char*) mac_current_hop_Pos, 6);
00139
00140
00141
00142
00143 mac_current_hop_Pos = mac_list_pos;
00144 if (hop_count_ + 1 >= current_hop_)
00145 relativeMacPostion = hop_count_ + 1 - current_hop_;
00146 else
00147 relativeMacPostion = 0;
00148
00149 mac_current_hop_Pos += (relativeMacPostion * 6);
00150
00151 memcpy((void*) mac_next_hop_, (unsigned char*) mac_current_hop_Pos, 6);
00152
00153
00154 }
00155
00156 RouteResponse::RouteResponse(RouteRequest request, unsigned char macDestination[6], uint8_t root_distance)
00157 {
00158
00159 hostname_source_ = request.hostname_source_;
00160 this->root_distance = root_distance;
00161 this->hop_count_ = request.header_.hop_count + 1;
00162 request_id_ = request.header_.id;
00163 current_hop_ = 1;
00164 mc_flag_ = request.mc_flag_;
00165
00166 this->path_l_ = request.path_l_;
00167 mac dMac;
00168 memcpy((void*) dMac.mac_adr, (void*) macDestination, 6);
00169 path_l_.push_back(dMac);
00170
00171 }
00172
00173 std::string RouteResponse::getResponseAsNetworkString(unsigned char source_mac[6])
00174 {
00175
00176
00177 unsigned char* buffer = new unsigned char[ETHER_MAX_LEN];
00178 unsigned char* eth_head = buffer;
00179 unsigned char* eth_data = buffer + 14;
00180 uint32_t int_htonl;
00181 uint32_t buffer_offset = 0;
00182
00183
00184 memcpy(buffer, bcast_mac, ETH_ALEN);
00185
00186 memcpy(buffer + 6, source_mac, ETH_ALEN);
00187
00188 uint16_t tf = htons(ETH_TYPE);
00189 memcpy(eth_head + 12, &tf, 2);
00190 buffer_offset += 14;
00191
00192
00193 uint8_t ff = FRAME_TYPE_REPLY;
00194 memcpy(eth_data, &ff, 1);
00195 eth_data++;
00196 buffer_offset += 1;
00197
00198
00199 int_htonl = htonl(request_id_);
00200 memcpy(eth_data, &int_htonl, sizeof (uint32_t));
00201 eth_data += sizeof (uint32_t);
00202 buffer_offset += sizeof (uint32_t);
00203
00204
00205 memcpy(eth_data, &mc_flag_, 1);
00206 eth_data++;
00207 buffer_offset += 1;
00208
00209
00210 memcpy(eth_data, &root_distance, 1);
00211 eth_data++;
00212 buffer_offset += 1;
00213
00214
00215 int_htonl = htonl(this->hop_count_);
00216 memcpy(eth_data, &int_htonl, sizeof (uint32_t));
00217 eth_data += sizeof (uint32_t);
00218 buffer_offset += sizeof (uint32_t);
00219
00220
00221 int_htonl = htonl(current_hop_);
00222 memcpy(eth_data, &int_htonl, sizeof (uint32_t));
00223 eth_data += sizeof (uint32_t);
00224 buffer_offset += sizeof (uint32_t);
00225
00226
00227
00228 for (std::list<mac>::iterator it = this->path_l_.begin(); it != path_l_.end(); ++it)
00229 {
00230 mac & tmp(*it);
00231 memcpy(eth_data, (unsigned char*) tmp.mac_adr, ETH_ALEN);
00232 eth_data += ETH_ALEN;
00233 buffer_offset += ETH_ALEN;
00234 }
00235
00236
00237
00238 int_htonl = htonl(hostname_source_.length());
00239 memcpy(eth_data, &int_htonl, sizeof (uint32_t));
00240 eth_data += sizeof (uint32_t);
00241 buffer_offset += sizeof (uint32_t);
00242
00243
00244 memcpy(eth_data, hostname_source_.data(), hostname_source_.length());
00245 eth_data += hostname_source_.length();
00246 buffer_offset += hostname_source_.length();
00247
00248
00249 std::string crc_string = std::string((const char*) buffer, buffer_offset);
00250 uint32_t crc = GetCrc32(crc_string);
00251 int_htonl = htonl(crc);
00252 memcpy(eth_data, &int_htonl, sizeof (uint32_t));
00253 eth_data += sizeof (uint32_t);
00254 buffer_offset += sizeof (uint32_t);
00255
00256 std::string res = std::string((const char*) buffer, buffer_offset);
00257 delete [] buffer;
00258 return res;
00259 }
00260
00261 RouteResponse::~RouteResponse()
00262 {
00263
00264 }
00265
00266 int RouteResponse::GetCrc32(const std::string& my_string)
00267 {
00268 boost::crc_32_type result;
00269 result.process_bytes(my_string.data(), my_string.length());
00270 return result.checksum();
00271 }
00272