Shahar Hadas
2020-05-10 d0b85cf10415a378c11d385066bb27116f2c2df1
Fix network detection for Windows OS (#94)

* Fix Warnings

Fix some of the compile warnings on invalid castings

* Improve parallels detection

Parallels Desktop (MAC) is now properly detected and identified (not as Other)

* Fix network detection for Windows OS

- Proper usage of GetAdaptersInfo

Co-authored-by: Gabriele Contini <gcontini@users.noreply.github.com>
10个文件已修改
77 ■■■■■ 已修改文件
.gitignore 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
include/licensecc/datatypes.h 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/inspector/inspector.cpp 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/base/base64.cpp 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/base/file_utils.cpp 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/hw_identifier/hw_identifier_facade.cpp 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/os/execution_environment_common.cpp 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/os/windows/network.cpp 55 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/os/windows/signature_verifier.cpp 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/functional/generate-license.cpp 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
.gitignore
@@ -21,6 +21,7 @@
# Cmake generated files
build/*
!build/.gitkeep 
install/*
#eclipse files
.cproject
include/licensecc/datatypes.h
@@ -138,7 +138,7 @@
    int license_version;  // license file version
} LicenseInfo;
typedef enum { BARE_TO_METAL, VMWARE, VIRTUALBOX, V_XEN, KVM, HV, V_OTHER } LCC_API_VIRTUALIZATION_DETAIL;
typedef enum { BARE_TO_METAL, VMWARE, VIRTUALBOX, V_XEN, KVM, HV, PARALLELS, V_OTHER } LCC_API_VIRTUALIZATION_DETAIL;
typedef enum {
    PROV_UNKNOWN = 0,
src/inspector/inspector.cpp
@@ -26,6 +26,7 @@
                                                     {V_XEN, "XEN"},
                                                     {KVM, "KVM"},
                                                     {HV, "Microsoft Hypervisor"},
                                                     {PARALLELS, "Parallels Desktop"},
                                                     {V_OTHER, "Other type of vm"}};
const unordered_map<int, string> descByVirt = {{LCC_API_VIRTUALIZATION_SUMMARY::NONE, "No virtualization"},
src/library/base/base64.cpp
@@ -49,7 +49,7 @@
    const unsigned char* bin = (const unsigned char*)binaryData;
    int rc = 0;  // result counter
    int byteNo;  // I need this after the loop
    unsigned int byteNo;  // I need this after the loop
    int modulusLen = len % 3;
    int pad = ((modulusLen & 1) << 1) + ((modulusLen & 2) >> 1);  // 2 gives 1 and 1 gives 2, but 0 gives 0.
@@ -109,7 +109,7 @@
    const unsigned char* safeAsciiPtr = (const unsigned char*)tmp_str.c_str();
    std::vector<uint8_t> bin;
    int cb = 0;
    int charNo;
    unsigned int charNo;
    int pad = 0;
    size_t len = tmp_str.size();
src/library/base/file_utils.cpp
@@ -37,7 +37,7 @@
    ifstream in(filename, std::ios::binary);
    if (in) {
        string contents;
        size_t index = in.seekg(0, ios::end).tellg();
        size_t index = (size_t)in.seekg(0, ios::end).tellg();
        size_t limited_size = min(index, max_size);
        contents.resize(limited_size);
        in.seekg(0, ios::beg);
src/library/hw_identifier/hw_identifier_facade.cpp
@@ -29,6 +29,7 @@
        result = strategy->validate_identifier(pc_id);
    } catch (logic_error& e) {
        LOG_ERROR("Error validating identifier %s: %s", str_code.c_str(), e.what());
        ((void)(e));
    }
    return result;
}
src/library/os/execution_environment_common.cpp
@@ -20,11 +20,12 @@
const unordered_map<string, LCC_API_VIRTUALIZATION_DETAIL> virtual_cpu_names{
    {"bhyve bhyve ", V_OTHER}, {"KVM", KVM},       {"MICROSOFT", HV},        {" lrpepyh vr", HV},
    {"prl hyperv  ", V_OTHER}, {"VMWARE", VMWARE}, {"XenVMMXenVMM", V_XEN}, {"ACRNACRNACRN", V_OTHER},
    {"prl hyperv  ", PARALLELS}, {"VMWARE", VMWARE}, {"XenVMMXenVMM", V_XEN}, {"ACRNACRNACRN", V_OTHER},
    {"VBOX", VIRTUALBOX}};
const unordered_map<string, LCC_API_VIRTUALIZATION_DETAIL> vm_vendors{{"VMWARE", VMWARE},
                                                                      {"MICROSOFT", HV},
                                                                      {"MICROSOFT", HV},
                                                                      {"PARALLELS", PARALLELS},
                                                                      {"VITRUAL MACHINE", V_OTHER},
                                                                      {"INNOTEK GMBH", VIRTUALBOX},
                                                                      {"POWERVM", V_OTHER},
src/library/os/windows/network.cpp
@@ -15,7 +15,7 @@
#include <iphlpapi.h>
#include <unordered_map>
#include <stdio.h>
//#pragma comment(lib, "IPHLPAPI.lib")
#pragma comment(lib, "IPHLPAPI.lib")
#include "../../base/string_utils.h"
#include "../../base/logger.h"
@@ -50,34 +50,50 @@
    unordered_map<string, OsAdapterInfo> adapterByName;
    FUNCTION_RETURN f_return = FUNC_RET_OK;
    DWORD dwStatus;
    int adapter_info_size;
    PIP_ADAPTER_INFO pAdapterInfo;
    DWORD dwBufLen = sizeof(IP_ADAPTER_INFO);
    unsigned int i = 3;
    do {
    // Make an initial call to GetAdaptersInfo to get the necessary size into the ulOutBufLen variable
    pAdapterInfo = (PIP_ADAPTER_INFO)malloc(dwBufLen);
    if (pAdapterInfo == nullptr) {
        return FUNC_RET_ERROR;
    }
    dwStatus = GetAdaptersInfo(     // Call GetAdapterInfo
        pAdapterInfo,  // [out] buffer to receive data
        &dwBufLen  // [in] size of receive data buffer
    );
    // Incase the buffer was too small, reallocate with the returned dwBufLen
    if (dwStatus == ERROR_BUFFER_OVERFLOW) {
        free(pAdapterInfo);
        pAdapterInfo = (PIP_ADAPTER_INFO)malloc(dwBufLen);
        // Will only fail if buffer cannot be allocated (out of memory)
        if (pAdapterInfo == nullptr) {
            return FUNC_RET_BUFFER_TOO_SMALL;
        }
        dwStatus = GetAdaptersInfo(     // Call GetAdapterInfo
            pAdapterInfo,  // [out] buffer to receive data
            &dwBufLen  // [in] size of receive data buffer
        );
        if (dwStatus != NO_ERROR && pAdapterInfo != nullptr) {
            free(pAdapterInfo);
            pAdapterInfo = nullptr;
        switch (dwStatus) {
            case NO_ERROR:
                break;
            case ERROR_BUFFER_OVERFLOW:
                free(pAdapterInfo);
                return FUNC_RET_BUFFER_TOO_SMALL;
            default:
                free(pAdapterInfo);
                return FUNC_RET_ERROR;
        }
    } while (dwStatus == ERROR_BUFFER_OVERFLOW && i-- > 0);
    if (dwStatus == ERROR_BUFFER_OVERFLOW) {
        return FUNC_RET_ERROR;
    }
    adapter_info_size = dwBufLen / sizeof(IP_ADAPTER_INFO);
    if (adapter_info_size == 0) {
        return FUNC_RET_NOT_AVAIL;
    }
    PIP_ADAPTER_INFO pAdapter = pAdapterInfo;
    i = 0;
    FUNCTION_RETURN result = FUNC_RET_OK;
    while (pAdapter) {
        OsAdapterInfo ai = {};
@@ -86,12 +102,7 @@
        memcpy(ai.mac_address, pAdapter->Address, 8);
        translate(pAdapter->IpAddressList.IpAddress.String, ai.ipv4_address);
        ai.type = IFACE_TYPE_ETHERNET;
        i++;
        pAdapter = pAdapter->Next;
        if (i == adapter_info_size) {
            result = FUNC_RET_BUFFER_TOO_SMALL;
            break;
        }
        adapterByName[string(ai.description)] = ai;
    }
    free(pAdapterInfo);
src/library/os/windows/signature_verifier.cpp
@@ -67,7 +67,7 @@
    cout << (len & 0x80) << endl;
    if ((len & 0x80) > 0) {
        size_t blen = len & 0x7F;
        for (int i = 0; i < blen; i++) {
        for (size_t i = 0; i < blen; i++) {
            result += (*(ptr++) << (i * 8));
        }
    } else {
@@ -99,7 +99,7 @@
    if (expected_length < length) {
        return FUNC_RET_ERROR;
    }
    for (int i = 0; i < length; i++) {
    for (size_t i = 0; i < length; i++) {
        location[i] = *(ptr++);
    }
    return FUNC_RET_OK;
test/functional/generate-license.cpp
@@ -43,7 +43,7 @@
    ss << " --" PARAM_LICENSE_OUTPUT " " << license_fname_s;
    ss << " --" PARAM_PROJECT_FOLDER " " << LCC_TEST_LICENSES_PROJECT;
    for (int i = 0; i < other_args.size(); i++) {
    for (size_t i = 0; i < other_args.size(); i++) {
        ss << " " << other_args[i];
    }
    cout << "executing :" << ss.str() << endl;