Go to the documentation of this file.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
00034
00035
00036
00037
00038
00039
00040
00041 #include <list>
00042 #include <string>
00043
00044 #include "RouteRequest.h"
00045 #include "defines.h"
00046 #include "EthernetFrame.h"
00047
00048
00049 RouteRequest::RouteRequest(unsigned char* buffer) {
00050
00051 unsigned char* buffer_start = buffer;
00052
00053 eh_header* eh = (struct eh_header *) buffer;
00054 buffer += sizeof (eh_header);
00055 rreq_header* rfh = (struct rreq_header *) buffer;
00056 buffer += sizeof (rreq_header);
00057
00058
00059
00060
00061 hostname_source_ = "";
00062 hostname_source_.append((const char*) buffer, rfh->hostname_source_len);
00063 buffer += rfh->hostname_source_len;
00064
00065
00066 hostname_destination_ = "";
00067 hostname_destination_.append((const char*) buffer, rfh->hostname_destination_len);
00068 buffer += rfh->hostname_destination_len;
00069
00070
00071
00072 for (uint32_t i = 0; i < rfh->hop_count; i++) {
00073 mac current_mac;
00074 memcpy((unsigned char*) current_mac.mac_adr, (unsigned char*) buffer, 6);
00075 path_l_.push_back(current_mac);
00076
00077 buffer += 6;
00078 }
00079
00080
00081 mc_flag_ = (rfh->flag_field / 128) % 2 == 1;
00082
00083
00084
00085 uint32_t crc = 0;
00086 memcpy((unsigned char*) &crc, (unsigned char*) buffer, 4);
00087 uint16_t dynamic_field_len = + rfh->hostname_source_len + rfh->hostname_destination_len+ path_l_.size() * 6;
00088 std::string crc_data_string = "";
00089 crc_data_string.append((const char*) buffer_start,
00090 this->HEADER_FIXED_LEN + dynamic_field_len) ;
00091 correct_crc_ = (crc == (uint32_t) GetCrc32(crc_data_string));
00092
00093
00094 buffer_str_len_ = this->HEADER_FIXED_LEN +dynamic_field_len;
00095
00096
00097 memcpy(&this->eh_h_, &(*eh), sizeof(eh_header));
00098 memcpy(&this->header_, &(*rfh), sizeof(rreq_header));
00099 }
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 RouteRequest::RouteRequest(std::string my_hostname, std::string destination, uint16_t max_hops, bool is_multicast) {
00112 hostname_destination_ = destination;
00113 hostname_source_ = my_hostname;
00114 header_.hop_count = 0;
00115 header_.hop_limit = max_hops;
00116 header_.id = req_count_stat++;
00117 header_.frame_type = FRAME_TYPE_REQUEST;
00118
00119 mc_flag_ = is_multicast;
00120 }
00121
00122 RouteRequest::RouteRequest(route_request req) {
00123 hostname_destination_ = req.hostname_destination;
00124 hostname_source_ = req.hostname_source;
00125 header_.hop_limit = req.hop_limit;
00126 mc_flag_ = req.is_mc;
00127 header_.hop_count = 0;
00128 header_.id = req.id;
00129 header_.frame_type = FRAME_TYPE_REQUEST;
00130 }
00131
00132 std::string RouteRequest::getRequestAsNetworkString(unsigned char source_mac[6]) {
00133
00134
00135
00136 mac currentHopMac;
00137 memcpy(currentHopMac.mac_adr, source_mac, 6);
00138 memcpy( eh_h_.eh_source, source_mac, 6);
00139 memcpy(eh_h_.eh_dest, bcast_mac, 6);
00140 path_l_.push_back(currentHopMac);
00141 header_.hop_count++;
00142
00143 this->header_.hop_count = this->path_l_.size();
00144
00145
00146
00147 this->header_.flag_field = 0;
00148 if (mc_flag_)
00149 this->header_.flag_field += 128;
00150
00151
00152
00153 this->header_.hostname_source_len = hostname_source_.length();
00154 this->header_.hostname_destination_len = hostname_destination_.length();
00155
00156 unsigned char a_buffer[ETHER_MAX_LEN];
00157 unsigned char* buffer_start = a_buffer;
00158 unsigned char* buffer = a_buffer;
00159
00160
00161
00162
00163
00164
00165 memcpy(buffer, &this->eh_h_, sizeof (eh_header));
00166 buffer += sizeof (eh_header);
00167
00168
00169 memcpy(buffer, &this->header_, sizeof (rreq_header));
00170 buffer += sizeof (rreq_header);
00171
00172
00173 memcpy(buffer, this->hostname_source_.data(),
00174 this->hostname_source_.length());
00175 buffer += this->hostname_source_.length();
00176
00177
00178 memcpy((unsigned char*)buffer, (unsigned char*) this->hostname_destination_.data(), this->hostname_destination_.length());
00179 buffer += hostname_destination_.length();
00180
00181
00182
00183 for (std::list<mac>::iterator it = this->path_l_.begin(); it != path_l_.end(); ++it) {
00184 memcpy(buffer, (unsigned char*) (*it).mac_adr, ETH_ALEN);
00185
00186 buffer += ETH_ALEN;
00187 }
00188
00189
00190
00191 int dynamic_field_len = this->hostname_source_.length() + this->hostname_destination_.length() + 6 * path_l_.size();
00192 std::string crc_string = std::string((const char*) buffer_start, this->HEADER_FIXED_LEN + dynamic_field_len);
00193 uint32_t crc = GetCrc32(crc_string);
00194 memcpy(buffer, &crc, sizeof (uint32_t));
00195 buffer += sizeof (uint32_t);
00196
00197
00198
00199 return std::string((const char*) buffer_start, this->HEADER_FIXED_LEN + dynamic_field_len + sizeof (crc));
00200
00201
00202
00203
00204 }
00205
00206 RouteRequest::~RouteRequest() {
00207
00208 }
00209
00210
00211