gcontini
2020-02-09 8e1bdfdca2ad2157fd74cedc1a6768a1b1c0849d
src/library/pc_identifier/ethernet.cpp
@@ -4,53 +4,51 @@
 *  Created on: Jan 11, 2020
 *      Author: devel
 */
#include <array>
#include "ethernet.hpp"
#include "../os/os.h"
#include <bits/stdint-uintn.h>
#include <array>
#include <vector>
#include "../../../include/licensecc/datatypes.h"
#include "../../../projects/DEFAULT/include/licensecc/DEFAULT/licensecc_properties.h"
#include "../base/base.h"
#include "../os/network.hpp"
#include "pc_identifier.hpp"
namespace license {
namespace pc_identifier {
using namespace std;
static FUNCTION_RETURN generate_ethernet_pc_id(vector<array<uint8_t, 6>> &data, bool use_mac) {
   OsAdapterInfo *adapterInfos;
   size_t defined_adapters, adapters = 0;
static FUNCTION_RETURN generate_ethernet_pc_id(vector<array<uint8_t, PC_IDENTIFIER_PROPRIETARY_DATA>> &data,
                                    const bool use_ip) {
   vector<os::OsAdapterInfo> adapters;
   FUNCTION_RETURN result_adapterInfos = getAdapterInfos(NULL, &adapters);
   if (result_adapterInfos != FUNC_RET_BUFFER_TOO_SMALL) {
   FUNCTION_RETURN result_adapterInfos = getAdapterInfos(adapters);
   if (result_adapterInfos != FUNC_RET_OK) {
      return result_adapterInfos;
   }
   if (adapters == 0) {
   if (adapters.size() == 0) {
      return FUNC_RET_NOT_AVAIL;
   }
   defined_adapters = adapters;
   data.reserve(adapters);
   adapterInfos = static_cast<OsAdapterInfo *>(malloc(adapters * sizeof(OsAdapterInfo)));
   result_adapterInfos = getAdapterInfos(adapterInfos, &adapters);
   if (result_adapterInfos == FUNC_RET_OK) {
      unsigned int j;
      for (j = 0; j < adapters; j++) {
         unsigned int k;
         array<uint8_t, 6> identifier;
         for (k = 0; k < 6; k++) {
            if (use_mac) {
               identifier[k] = adapterInfos[j].mac_address[k + 2];
            } else {
               // use ip
               if (k < 4) {
                  identifier[k] = adapterInfos[j].ipv4_address[k];
               } else {
                  // padding
                  identifier[k] = 42;
               }
            }
   for (auto &it : adapters) {
      unsigned int k, data_len, data_byte;
      array<uint8_t, PC_IDENTIFIER_PROPRIETARY_DATA> identifier;
      data_len = use_ip ? sizeof(os::OsAdapterInfo::ipv4_address) : sizeof(os::OsAdapterInfo::mac_address);
      for (k = 0; k < PC_IDENTIFIER_PROPRIETARY_DATA; k++) {
         if (k < data_len) {
            identifier[k] = use_ip ? it.ipv4_address[k] : it.mac_address[k];
         } else {
            identifier[k] = 42;
         }
         identifier[6] = identifier[6] & 0x1F;
         data.push_back(identifier);
      }
   }
   free(adapterInfos);
      identifier[0] = identifier[0] & 0x1F;
      data.push_back(identifier);
      }
   return result_adapterInfos;
}
@@ -61,7 +59,7 @@
LCC_API_IDENTIFICATION_STRATEGY Ethernet::identification_strategy() const { return STRATEGY_ETHERNET; }
FUNCTION_RETURN Ethernet::identify_pc(PcIdentifier &pc_id) const {
   vector<array<uint8_t, 6>> data;
   vector<array<uint8_t, PC_IDENTIFIER_PROPRIETARY_DATA>> data;
   FUNCTION_RETURN result = generate_ethernet_pc_id(data, use_ip);
   if (result == FUNC_RET_OK) {
      pc_id.set_data(data[0]);
@@ -70,7 +68,7 @@
}
std::vector<PcIdentifier> Ethernet::alternative_ids() const {
   vector<array<uint8_t, 6>> data;
   vector<array<uint8_t, PC_IDENTIFIER_PROPRIETARY_DATA>> data;
   FUNCTION_RETURN result = generate_ethernet_pc_id(data, use_ip);
   vector<PcIdentifier> identifiers;
   if (result == FUNC_RET_OK) {
@@ -86,14 +84,14 @@
}
LCC_EVENT_TYPE Ethernet::validate_identifier(const PcIdentifier &identifier) const {
   vector<array<uint8_t, 6>> data;
   vector<array<uint8_t, PC_IDENTIFIER_PROPRIETARY_DATA>> data;
   FUNCTION_RETURN generate_ethernet = generate_ethernet_pc_id(data, use_ip);
   LCC_EVENT_TYPE result = IDENTIFIERS_MISMATCH;
   if (generate_ethernet == FUNC_RET_OK) {
      // fixme
      // result = validate_identifier(identifier, data);
      result = validate_identifier(identifier, data);
   }
   return result;
}
}  // namespace pc_identifier
} /* namespace license */