RouteResponse.cpp
Go to the documentation of this file.
00001 /*
00002  *
00003  * RouteResponse.h
00004  *
00005  *  Created on: 25.07.2013
00006  *      Author: Günther Cwioro
00007  *
00008  *
00009  * Structure of route response:
00010  *
00011  * BROADCAST MAC        // -> ff:ff:ff:ff:ff:ff                                                                                         6Byte
00012  * SOURCE MAC           // MAC of the using interface                                                                           6Byte
00013  * ETH_TYPE_FIELD       // 0x4148 (AH as string)                                                                                        2Byte
00014  * FRAME_TYPE           // 0x72 (r as string)                                                                                           1Byte
00015  * REQUEST ID.          // Every node has a consecutive number for his requests                         4Byte
00016  * MC FLAG                                                                                                                                                              1Byte
00017  * ROOT DISTANCE        // distancte to the mc root                                                                                     1Byte
00018  * HOB COUNT            // is the amount of node which are involved to process the packet       4Byte
00019  * CURRENT HOP          // hop to be next to send                                                                                       4Byte
00020  * PATH                         //the list of macs from the source to destination                                       HOB COUNT * 6Byte
00021  * SOURCE HOST LENGTH                                                                                                                                   4Byte
00022  * SOURCE HOST                                                                                                                                                  VAR
00023  * CRC                                                                                                                                                                  4Byte
00024  *
00025  *
00026 
00027  *
00028  * Every node which receives an router response, checks first if he was involved in the route request and then checks if his mac is the current hop mac and forwards the response to the source.
00029  * The Response takes the route backwards of the route request to the source.
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     /*ETHERNET HEADER*/
00045     unsigned char* mac_list_pos = buffer + 29; //points to the first mac in path_l_;
00046     ethhdr* eh = (struct ethhdr *) buffer;
00047     buffer += 15; // 14 eth_head + 1 frame_Type
00048 
00049 
00050 
00051     memcpy(&this->eh_, &(*eh), sizeof (ethhdr));
00052 
00053 
00054  
00055 
00056     /*REQUEST ID*/
00057     uint32_t num_from_network; //help var to convert the integers from bigE to littleE
00058     memcpy((unsigned char*) &num_from_network, (unsigned char*) buffer, 4);
00059     request_id_ = ntohl(num_from_network);
00060     buffer += 4;
00061 
00062     /*MC FLAG*/
00063     memcpy((unsigned char*) &mc_flag_, (unsigned char*) buffer, 1);
00064     buffer += 1;
00065 
00066     /*ROOT DISTANCE*/
00067     memcpy((unsigned char*) &root_distance, (unsigned char*) buffer, 1);
00068     buffer += 1;
00069 
00070     /*HOP COUNT*/
00071     memcpy((unsigned char*) &num_from_network, (unsigned char*) buffer, 4);
00072     hop_count_ = ntohl(num_from_network);
00073     buffer += 4;
00074 
00075     /*CURRENT HOP*/
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     /*PATH*/
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     /*SRC HOST LEN*/
00092     memcpy((unsigned char*) &num_from_network, (unsigned char*) buffer, 4); // get frameId
00093     uint32_t hostname_source_Len = ntohl(num_from_network);
00094     buffer += 4;
00095 
00096     /*SRC HOST*/
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     /*CRC */
00105     uint32_t crc = 0;
00106     memcpy((unsigned char*) &num_from_network, (unsigned char*) buffer, 4); // get CRC
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     /*INIT mac_current_hop_ */
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     /*INIT mac_previous_hop_ */
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     /*INIT mac_next_hop_ */
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     //  flag_field_ = RouteRequest::createFlagField(true,false);
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     /* Build Ethernet header*/
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     /*PACKET TYPE*/
00193     uint8_t ff = FRAME_TYPE_REPLY;
00194     memcpy(eth_data, &ff, 1);
00195     eth_data++;
00196     buffer_offset += 1;
00197 
00198     /*REQ ID*/
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     /*MC FLAG*/
00205     memcpy(eth_data, &mc_flag_, 1);
00206     eth_data++;
00207     buffer_offset += 1;
00208 
00209     /*ROOT DISTANCE*/
00210     memcpy(eth_data, &root_distance, 1);
00211     eth_data++;
00212     buffer_offset += 1;
00213 
00214     /*HOP COUNT*/
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     /*CURRENT HOP*/
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     /*MAC LIST*/
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     /*SOURCE HOST LENGTH*/
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     /*SOURCE HOST*/
00244     memcpy(eth_data, hostname_source_.data(), hostname_source_.length());
00245     eth_data += hostname_source_.length();
00246     buffer_offset += hostname_source_.length();
00247 
00248     /*CRC*/
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     // TODO Auto-generated destructor stub
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 


adhoc_communication
Author(s): Guenter Cwioro , Torsten Andre
autogenerated on Thu Aug 27 2015 11:56:40