gcontini
2020-02-10 205c71575c272a685011c641a33d26cf66cf60a0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
 * ethernet.cpp
 *
 *  Created on: Jan 11, 2020
 *      Author: devel
 */
 
 
#include <array>
#include <vector>
 
#include <licensecc/datatypes.h>
#include <licensecc_properties.h>
#include "../base/base.h"
#include "../os/network.hpp"
#include "pc_identifier.hpp"
#include "ethernet.hpp"
 
namespace license {
namespace pc_identifier {
using namespace std;
 
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(adapters);
    if (result_adapterInfos != FUNC_RET_OK) {
        return result_adapterInfos;
    }
    if (adapters.size() == 0) {
        return FUNC_RET_NOT_AVAIL;
    }
 
    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[0] = identifier[0] & 0x1F;
        data.push_back(identifier);
        }
 
    return result_adapterInfos;
}
 
Ethernet::Ethernet(bool useIp) : use_ip(useIp) {}
 
Ethernet::~Ethernet() {}
 
LCC_API_IDENTIFICATION_STRATEGY Ethernet::identification_strategy() const { return STRATEGY_ETHERNET; }
 
FUNCTION_RETURN Ethernet::identify_pc(PcIdentifier &pc_id) const {
    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]);
    }
    return result;
}
 
std::vector<PcIdentifier> Ethernet::alternative_ids() const {
    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) {
        identifiers.resize(data.size());
        for (auto &it : data) {
            PcIdentifier pc_id;
            pc_id.set_identification_strategy(identification_strategy());
            pc_id.set_data(it);
            identifiers.push_back(pc_id);
        }
    }
    return identifiers;
}
 
LCC_EVENT_TYPE Ethernet::validate_identifier(const PcIdentifier &identifier) const {
    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) {
        result = validate_identifier(identifier, data);
    }
    return result;
}
 
}  // namespace pc_identifier
} /* namespace license */