AckRoutedFrame.cpp
Go to the documentation of this file.
00001 /*
00002  * AckRoutedFrame.h
00003  *
00004  *  Created on: 29.07.2013
00005  *      Author: Günther Cwioro
00006  *
00007  */
00008 
00009 #include <string>
00010 
00011 #include "AckRoutedFrame.h"
00012 #include "structs.h"
00013 #include "EthernetFrame.h"
00014 #include "defines.h"
00015 
00016 AckRoutedFrame::~AckRoutedFrame()
00017 {
00018     // TODO Auto-generated destructor stub
00019 }
00020 
00021 AckRoutedFrame::AckRoutedFrame(RoutedFrame rf)
00022 {
00023     header_.frame_id = rf.header_.frame_id;
00024     mc_group_ = rf.mc_g_name_;
00025     cr_flag = false;
00026 
00027     mc_flag = mc_group_.compare("") != 0;
00028 
00029 
00030     if (mc_flag)
00031     {
00032 
00033         /* If its a multicast frame the fields packet_id and route_id will be used to store the packet_id and the packet_squence_num*/
00034         hostname_source_ = rf.hostname_source_;
00035         header_.frame_id = rf.header_.packet_id;
00036         header_.route_id = rf.header_.packet_sequence_num;
00037     }
00038 }
00039 
00040 AckRoutedFrame::AckRoutedFrame(unsigned char* buffer)
00041 {
00042     unsigned char* buffer_start = buffer;
00043     buffer_str_len_ = 0;
00044 
00045     /*ETHERNET HEADER*/
00046     eh_header* eh = (struct eh_header *) buffer;
00047     buffer += sizeof (eh_header);
00048     buffer_str_len_ += sizeof (eh_header);
00049 
00050     /*FRAME HEADER*/
00051     ack_rf_header* rfh = (struct ack_rf_header *) buffer;
00052     buffer += sizeof (ack_rf_header);
00053     buffer_str_len_ += sizeof (ack_rf_header);
00054 
00055     /*SOURCE HOST*/
00056     hostname_source_ = "";
00057     hostname_source_.append((const char*) buffer, rfh->hostname_source_len);
00058     buffer += rfh->hostname_source_len;
00059     buffer_str_len_ += hostname_source_.length();
00060 
00061     /*MC GROUP HOST*/
00062     mc_group_ = "";
00063     mc_group_.append((const char*) buffer, rfh->mc_group_len);
00064     buffer += rfh->mc_group_len;
00065     buffer_str_len_ += rfh->mc_group_len;
00066 
00067 
00068 
00069     /*INIT FLAGS*/
00070     cr_flag = (rfh->flag_field / 128) % 2 == 1;
00071     mc_flag = (rfh->flag_field / 64) % 2 == 1;
00072 
00073 
00074     /*CRC */
00075     uint32_t crc = 0;
00076     memcpy((unsigned char*) &crc, (unsigned char*) buffer, 4); // get CRC
00077 
00078     std::string crc_data_string = "";
00079     uint16_t dynamic_field_len = +rfh->hostname_source_len + rfh->mc_group_len;
00080     crc_data_string.append((const char*) buffer_start,
00081                            this->HEADER_FIXED_LEN + dynamic_field_len);
00082     correct_crc_ = (crc == (uint32_t) GetCrc32(crc_data_string));
00083     buffer_str_len_++;
00084 
00085 
00086 
00087 
00088     /* COPY HEADER FIELDS */
00089     memcpy(&this->eh_h_, &(*eh), sizeof (eh_header));
00090     memcpy(&this->header_, &(*rfh), sizeof (ack_rf_header));
00091 
00092 
00093 
00094 }
00095 
00096 std::string AckRoutedFrame::getFrameAsNetworkString(uint32_t route_id, unsigned char next_hop[6], string source_host, unsigned char source[6])
00097 {
00098     if (mc_flag)
00099     {
00100         memcpy(header_.mac_destination_, bcast_mac, ETH_ALEN);
00101     }
00102     else
00103     {
00104         hostname_source_ = source_host;
00105         header_.route_id = route_id;
00106         memcpy(header_.mac_destination_, next_hop, ETH_ALEN);
00107 
00108     }
00109 
00110     memcpy(eh_h_.eh_source, source, ETH_ALEN);
00111     header_.frame_type = FRAME_TYPE_TRANSPORT_ACK;
00112 
00113 
00114     /*FLAG FIELD*/
00115     this->header_.flag_field = 0;
00116     if (cr_flag)
00117         this->header_.flag_field += 128;
00118     if (mc_flag)
00119         this->header_.flag_field += 64;
00120 
00121     /*LEN FIELDS */
00122     this->header_.hostname_source_len = hostname_source_.length();
00123     this->header_.mc_group_len = mc_group_.length();
00124 
00125     unsigned char f_buffer[ETHER_MAX_LEN]; // = unsigned char[ETHER_MAX_LEN];
00126     unsigned char* buffer = f_buffer;
00127     unsigned char* buffer_start = f_buffer;
00128 
00129     /*ETHERNET FIELDS*/
00130     //eh_header* eh = (struct eh_header *) buffer;
00131     memcpy(buffer, &this->eh_h_, sizeof (eh_header));
00132     buffer += sizeof (eh_header);
00133 
00134     /*FIXED RF HEADER FIELDS*/
00135     memcpy(buffer, &this->header_, sizeof (ack_rf_header));
00136     buffer += sizeof (ack_rf_header);
00137 
00138     /*SOURCE HOST */
00139     memcpy(buffer, this->hostname_source_.data(),
00140            this->hostname_source_.length());
00141     buffer += this->hostname_source_.length();
00142 
00143     /*MC GROUP  */
00144     memcpy(buffer, this->mc_group_.data(),
00145            this->mc_group_.length());
00146     buffer += this->mc_group_.length();
00147 
00148 
00149 
00150     /*CRC*/
00151     int dynamic_field_len = this->hostname_source_.length() + this->mc_group_.length();
00152     std::string crc_string = std::string((const char*) buffer_start, this->HEADER_FIXED_LEN + dynamic_field_len);
00153     uint32_t crc = GetCrc32(crc_string);
00154     memcpy(buffer, &crc, sizeof (uint32_t));
00155     buffer += sizeof (uint32_t);
00156 
00157 
00158 
00159 
00160     return string((const char*) buffer_start, this->HEADER_FIXED_LEN + dynamic_field_len + sizeof (crc));
00161 
00162 
00163 }
00164 
00165 std::string AckRoutedFrame::getFrameAsNetworkString(routing_entry r, unsigned char src[6])
00166 {
00167     return this->getFrameAsNetworkString(r.id,r.previous_hop,r.hostname_source,src);
00168 }
00169 
00170 
00171 stc_frame AckRoutedFrame::getFrameStruct()
00172 {
00173     stc_frame stc;
00174     stc.frame_id = header_.frame_id;
00175     memcpy(stc.mac, header_.mac_destination_, ETH_ALEN);
00176     stc.mc_group = mc_group_;
00177     stc.hostname_source = hostname_source_;
00178 
00179 
00180   
00181     stc.retransmitted = 0;
00182 
00183     return stc;
00184 }


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