RouteRequest.cpp
Go to the documentation of this file.
00001 /*
00002  * RouteRequest.cpp
00003  *
00004  *  Created on: 29.07.2013
00005  *      Author: Günther Cwioro
00006  *
00007  * Structure of a route request:
00008  *
00009  * BROADCAST MAC        // -> ff:ff:ff:ff:ff:ff                                                                                         6Byte
00010  * SOURCE MAC           // MAC of the using interface                                                                           6Byte
00011  * ETH_TYPE_FIELD       // 0x4148 (AH as string)                                                                                        2Byte
00012  * FRAME_TYPE           // 0x52 (R as string)                                                                                           1Byte
00013  * REQUEST ID.          // Every node has a consecutive number for his requests                         4Byte
00014  * MC FLAG                                                                                                                                                      1Byte
00015  * DEST HOST LENGTH                                                                                                                                     4Byte
00016  * DEST HOST                                                                                                                                                    VAR
00017  * SOURCE HOST LENGTH                                                                                                                                   4Byte
00018  * SOURCE HOST                                                                                                                                                  VAR
00019  * MAX HOP COUNT                                                                                                                                                4Byte
00020  * HOB COUNT            // is the amount of node which are involved to process the packet       4Byte
00021  * PATH                         //the list of macs from the hostname_source_ to destination                     VAR (HOB COUNT * 6Byte)
00022  * CRC                                                                                                                                                                  4Byte
00023  *
00024  * Every node only accept one request with the same primary key. This is so to prevent network flooding.
00025  * The primary key of the request is REQUEST_ID + SOURCE_HOST
00026  *
00027  *
00028  * How a route request works:
00029  *
00030  *              (Node1)         (Node3)
00031  * (S)                                           (D)
00032  *              (Node2)         (Node4)
00033  * Request:
00034  * 1) S wants the path to D
00035  * 2) S sends request as broadcast
00036  * 3) N1 and N2 receive the request and insert their mac in the path list then they broadcast the request forward
00037  * 4) N3 and N3 receive the request from N1 or N2 (depends on which node is faster). They do the same like in step 2.
00038  * 5) The D gets the request either from N3 or from N4 and send an route response
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     /*Source Host*/
00061     hostname_source_ = "";
00062     hostname_source_.append((const char*) buffer, rfh->hostname_source_len);
00063     buffer += rfh->hostname_source_len;
00064 
00065     /*Dest Host*/
00066     hostname_destination_ = "";
00067     hostname_destination_.append((const char*) buffer, rfh->hostname_destination_len);
00068     buffer += rfh->hostname_destination_len;
00069 
00070 
00071     /*PATH*/
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         /*INIT FLAGS*/
00081         mc_flag_ = (rfh->flag_field / 128) % 2 == 1;
00082         
00083 
00084         /*CRC */
00085         uint32_t crc = 0;
00086         memcpy((unsigned char*) &crc, (unsigned char*) buffer, 4); // get CRC
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         /* COPY HEADER FIELDS */
00097         memcpy(&this->eh_h_, &(*eh), sizeof(eh_header));
00098         memcpy(&this->header_, &(*rfh), sizeof(rreq_header));
00099 }
00100 
00101 /*
00102 RouteRequest::RouteRequest(std::string sourHost,std::string destHost,uint32_t maximalHops) {
00103 
00104         hostname_destination_ = destHost;
00105         hostname_source_ = sourHost;
00106         this->hop_count_ = 0;
00107         this->hop_limit_ = maximalHops;
00108         id_ = this->req_count_stat++;
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     /*Add the current hop mac to the routing path_l_*/
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     /*FLAG FIELD*/
00147     this->header_.flag_field = 0;
00148     if (mc_flag_)
00149         this->header_.flag_field += 128;
00150 
00151 
00152     /*LEN FIELDS */
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     /*ETHERNET FIELDS*/
00164     //eh_header* eh = (struct eh_header *) buffer;
00165     memcpy(buffer, &this->eh_h_, sizeof (eh_header));
00166     buffer += sizeof (eh_header);
00167 
00168     /*FIXED RF HEADER FIELDS*/
00169     memcpy(buffer, &this->header_, sizeof (rreq_header));
00170     buffer += sizeof (rreq_header);
00171 
00172     /*SOURCE HOST */
00173     memcpy(buffer, this->hostname_source_.data(),
00174             this->hostname_source_.length());
00175     buffer += this->hostname_source_.length();
00176 
00177     /*DEST HOST */
00178     memcpy((unsigned char*)buffer, (unsigned char*) this->hostname_destination_.data(), this->hostname_destination_.length());
00179     buffer += hostname_destination_.length();
00180 
00181 
00182     /*PATH (list of macs from the src to dest)*/
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     /*CRC*/
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     // TODO Auto-generated destructor stub
00208 }
00209 
00210 
00211 


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