Beacon.cpp
Go to the documentation of this file.
00001 /*
00002  * Beacon.h
00003  *
00004  *  Created on: 19.07.2013
00005  *      Author: Günther Cwioro
00006  *
00007  * Hello frame structure:
00008  *
00009  * BROADCAST MAC        // -> ff:ff:ff:ff:ff:ff                                                                                         6Byte
00010  * SOURCE MAC           // MAC of the using interface                                                                           6Byte
00011  * ETH_TYPE_FIELD       // 0x5555 (UU as string)                                                                                        2Byte
00012  * FRAME_TYPE           -> H                                                                                                                            1Byte
00013  * HOSTNAME LENGTH                                                                                                                                              4Byte
00014  * HOSTNAME                                                                                                                                                             VAR
00015  * CRC                                                                                                                                                                  4Byte
00016  */
00017 
00018 
00019 #include "Beacon.h"
00020 #include "functions.h"
00021 
00022 Beacon::Beacon(unsigned char* source, std::string hname)
00023 {
00024     memcpy(mac_source_, source, 6);
00025 
00026 
00027     this->hostname_ = hname;
00028 }
00029 
00030 Beacon::Beacon(unsigned char* buffer)
00031 {
00032 
00033     
00034     unsigned char* buffer_start = buffer;
00035     /*ETHERNET HEADER*/
00036     eh_ = (struct ethhdr *) buffer;
00037     memcpy(mac_source_, eh_->h_source, 6);
00038     buffer += 15; // 14 eth_head + 1 frame_Type
00039     uint32_t num_from_network;
00040 
00041     /* HOSTNAME LEN */
00042     memcpy((unsigned char*) &num_from_network, (unsigned char*) buffer, 4); // get hostname length
00043     uint32_t hostnameLength = ntohl(num_from_network);
00044     buffer += 4;
00045 
00046     /*HOSTNAME*/
00047     hostname_ = "";  
00048     hostname_.append((const char*) buffer, hostnameLength);
00049     buffer+=hostnameLength;
00050     
00051 
00052     /*CRC */
00053     uint32_t crc = 0;
00054     memcpy((unsigned char*) &num_from_network, (unsigned char*) buffer, 4); // get CRC
00055     crc = ntohl(num_from_network);
00056 
00057     std::string crc_data_string = "";
00058     crc_data_string.append((const char*) buffer_start, Beacon::HEADER_FIXED_LEN - 4 + hostname_.length());
00059 
00060     if (crc == (uint32_t) GetCrc32(crc_data_string))
00061         correct_crc_ = true;
00062     else
00063         correct_crc_ = false;
00064 
00065 
00066 }
00067 
00068 Beacon::~Beacon()
00069 {
00070     // TODO Auto-generated destructor stub
00071 }
00072 
00073 std::string Beacon::getFrameAsNetworkString()
00074 {
00075     unsigned char* buffer = new unsigned char[ETHER_MAX_LEN];
00076     unsigned char* eth_head = buffer;
00077     unsigned char* eth_data = buffer + 14;
00078     uint32_t int_htonl;
00079     uint32_t buffer_offset = 0;
00080 
00081     /* Build Ethernet header*/
00082     eh_ = (struct ethhdr *) eth_head;
00083     memcpy((void *) eh_->h_dest, (void*) bcast_mac, ETH_ALEN);
00084     memcpy((void *) eh_->h_source, (void*) mac_source_, ETH_ALEN);
00085    
00086     uint16_t tf = htons(ETH_TYPE);
00087     memcpy(eth_head + 12, &tf, 2);
00088     buffer_offset += 14;
00089 
00090     /*PACKET TYPE*/
00091     uint8_t ff = FRAME_TYPE_BEACON;
00092     memcpy(eth_data, &ff, 1);
00093     eth_data++;
00094     buffer_offset += 1;
00095     /*HOST LENGTH*/
00096     int_htonl = htonl(hostname_.length());
00097     memcpy(eth_data, &int_htonl, sizeof (uint32_t));
00098     eth_data += sizeof (uint32_t);
00099     buffer_offset += sizeof (uint32_t);
00100 
00101     /*HOST*/
00102     memcpy(eth_data, hostname_.data(), hostname_.length());
00103     eth_data += hostname_.length();
00104     buffer_offset += hostname_.length();
00105 
00106     /*CRC*/
00107     std::string crc_string = std::string((const char*) buffer, buffer_offset);
00108     uint32_t crc = GetCrc32(crc_string);
00109     int_htonl = htonl(crc);
00110     memcpy(eth_data, &int_htonl, sizeof (uint32_t));
00111     eth_data += sizeof (uint32_t);
00112     buffer_offset += sizeof (uint32_t);
00113 
00114 
00115     std::string res = std::string((const char*) buffer, buffer_offset);
00116     delete [] buffer;
00117     return res;
00118 }
00119 
00120 int Beacon::GetCrc32(const std::string& my_string)
00121 {
00122     boost::crc_32_type result;
00123     result.process_bytes(my_string.data(), my_string.length());
00124     return result.checksum();
00125 }


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