Skip to content

Commit

Permalink
cpu brand
Browse files Browse the repository at this point in the history
  • Loading branch information
gcontini committed Mar 15, 2020
1 parent 784227c commit 6dd7902
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 47 deletions.
12 changes: 5 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ IF(NOT LCC_PROJECTS_BASE_DIR)
ENDIF(NOT LCC_PROJECTS_BASE_DIR)

cmake_policy(SET CMP0048 NEW)
#cmake_policy(SET CMP0091 NEW)
project (licensecc
VERSION 2.0.0
DESCRIPTION "Copy protection and licensing library"
Expand Down Expand Up @@ -81,15 +82,14 @@ else(UNIX)
include(target_arch)
target_architecture( TARGET_ARCHITECTURE )
message(STATUS "architecture detected: ${TARGET_ARCHITECTURE}")

#cmake > 3.15 handle the /MD flag more nicely than this
if(${STATIC_RUNTIME})
string(REPLACE "/MD" "/MT" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
if(STATIC_RUNTIME)
message(STATUS "Static runtime ${STATIC_RUNTIME}")
string(REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
string(REPLACE "/MDd" "/MTd" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
string(REPLACE "/MDd" "/MTd" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
#list(APPEND CMAKE_CXX_FLAGS_DEBUG "/MTd")
SET(Boost_USE_STATIC_RUNTIME ON)
endif(${STATIC_RUNTIME})
endif(STATIC_RUNTIME)
add_definitions("/D _CRT_SECURE_NO_WARNINGS")
list(APPEND EXTERNAL_LIBS "bcrypt" "crypt32" "ws2_32" "iphlpapi")
else(MSVC)
Expand All @@ -116,8 +116,6 @@ add_custom_command(
include_directories( ${LCC_INCLUDE_DIR} "${CMAKE_CURRENT_SOURCE_DIR}/include" )
add_custom_target(project_initialize DEPENDS "${LCC_PROJECT_PUBLIC_KEY}" "${LCC_PROJECT_PRIVATE_KEY}")

message( STATUS "C compiler : " ${CMAKE_C_COMPILER})
message( STATUS "C compiler flags : " ${CMAKE_C_FLAGS})
message( STATUS "CXX compiler : " ${CMAKE_CXX_COMPILER})
message( STATUS "CXX compiler flags : " ${CMAKE_CXX_FLAGS})
message( STATUS "CXX compiler flags debug : " ${CMAKE_CXX_FLAGS_DEBUG})
Expand Down
9 changes: 5 additions & 4 deletions src/inspector/inspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ static LCC_EVENT_TYPE verifyLicense(const string& fname) {

int main(int argc, char* argv[]) {
license::os::CpuInfo cpu;
cout << "CpuVendor :" << cpu.vendor() << endl;
cout << "Virtual machine :" << cpu.cpu_virtual() << endl;
cout << "Cpu Vendor :" << cpu.vendor() << endl;
cout << "Cpu Brand :" << cpu.brand() << endl;
cout << "Cpu is hypervis.:" << cpu.cpu_virtual() << endl;
cout << "Cpu model :0x" << std::hex << ((long)cpu.model()) << std::dec << endl;
cout << "Virt. detail cpu:" << descByVirtDetail.find(cpu.getVirtualizationDetail())->second << endl;
cout << "Virt. detail cpu:" << descByVirtDetail.find(cpu.virtualization_details())->second << endl <<endl;
ExecutionEnvironment execEnv;
cout << "Running in cloud:" << execEnv.is_cloud() << endl;
cout << "Docker :" << execEnv.is_docker() << endl;
Expand All @@ -66,7 +67,7 @@ int main(int argc, char* argv[]) {

cout << "Bios vendor :" << execEnv.bios_vendor() << endl;
cout << "Bios description:" << execEnv.bios_description() << endl;
cout << "System vendor :" << execEnv.sys_vendor() << endl;
cout << "System vendor :" << execEnv.sys_vendor() << endl <<endl;

char hw_identifier[LCC_API_PC_IDENTIFIER_SIZE + 1];
size_t bufSize = LCC_API_PC_IDENTIFIER_SIZE + 1;
Expand Down
2 changes: 2 additions & 0 deletions src/library/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ ELSE(UNIX OR OPENSSL_FOUND)
target_link_libraries(licensecc_static PUBLIC ${EXTERNAL_LIBS})
ENDIF(UNIX OR OPENSSL_FOUND)

#set_property(TARGET licensecc_static PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")

target_include_directories(licensecc_static
PUBLIC
$<INSTALL_INTERFACE:include>
Expand Down
2 changes: 1 addition & 1 deletion src/library/hw_identifier/hw_identifier_facade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ std::string HwIdentifierFacade::generate_user_pc_signature(LCC_API_IDENTIFICATIO
pc_id.set_cloud_provider(exec.getCloudProvider());
} else {
os::CpuInfo cpu;
pc_id.set_virtualization(cpu.getVirtualizationDetail());
pc_id.set_virtualization(cpu.virtualization_details());
}
}
return pc_id.print();
Expand Down
6 changes: 3 additions & 3 deletions src/library/os/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ IF(UNIX OR OPENSSL_FOUND)
cpu_info_common.cpp
linux/cpu_info.cpp
linux/network.cpp
linux/os-linux.cpp)
linux/os_linux.cpp)
ELSE(UNIX)
#windows and openssl
add_library(os OBJECT
Expand All @@ -16,7 +16,7 @@ IF(UNIX OR OPENSSL_FOUND)
execution_environment_common.cpp windows/execution_environment.cpp
windows/isvm/Native.cpp
windows/isvm/BIOSReader.cpp
windows/os-win.cpp
windows/os_win.cpp
windows/network.cpp)
ENDIF(UNIX)
target_include_directories(os PUBLIC ${OPENSSL_INCLUDE_DIR})
Expand All @@ -29,7 +29,7 @@ ELSE(UNIX OR OPENSSL_FOUND)
execution_environment_common.cpp windows/execution_environment.cpp
windows/isvm/Native.cpp
windows/isvm/BIOSReader.cpp
windows/os-win.cpp
windows/os_win.cpp
windows/network.cpp)
ENDIF(UNIX OR OPENSSL_FOUND)

Expand Down
9 changes: 5 additions & 4 deletions src/library/os/cpu_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ typedef enum { BARE_TO_METAL, VMWARE, VIRTUALBOX, V_XEN, KVM, HV, V_OTHER } VIRT
class CpuInfo {
private:
bool is_hypervisor_set() const;

const std::string m_vendor;
const std::string m_brand;
public:
CpuInfo();
virtual ~CpuInfo();
Expand All @@ -28,9 +29,9 @@ class CpuInfo {
*/
bool cpu_virtual() const;
uint32_t model() const;
std::string vendor() const;
// std::string brand() const;
VIRTUALIZATION_DETAIL getVirtualizationDetail() const;
const std::string& vendor() const { return m_vendor; }
const std::string& brand() const { return m_brand; }
VIRTUALIZATION_DETAIL virtualization_details() const;
};

} // namespace os
Expand Down
2 changes: 1 addition & 1 deletion src/library/os/cpu_info_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ bool CpuInfo::cpu_virtual() const {
return is_virtual;
}

VIRTUALIZATION_DETAIL CpuInfo::getVirtualizationDetail() const {
VIRTUALIZATION_DETAIL CpuInfo::virtualization_details() const {
string cpu_vendor = vendor();
auto it = virtual_cpu_names.find(cpu_vendor);
VIRTUALIZATION_DETAIL result = BARE_TO_METAL;
Expand Down
35 changes: 27 additions & 8 deletions src/library/os/linux/cpu_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <cpuid.h>
#include <string>
#include <unordered_set>
#include <memory.h>
#include "../cpu_info.hpp"

namespace license {
Expand All @@ -22,7 +23,32 @@ struct CPUVendorID {
string toString() const { return string(reinterpret_cast<const char *>(this), 12); }
};

CpuInfo::CpuInfo() {}
static string get_cpu_vendor() {
unsigned int level = 0, eax = 0, ebx = 0, ecx = 0, edx = 0;
// hypervisor flag false, try to get the vendor name, see if it's a virtual cpu
__get_cpuid(level, &eax, &ebx, &ecx, &edx);
CPUVendorID vendorID{.ebx = ebx, .edx = edx, .ecx = ecx};
return vendorID.toString();
}

// https://en.wikipedia.org/wiki/CPUID
static string get_cpu_brand() {
string result;
uint32_t brand[0x10];

if (!__get_cpuid_max(0x80000004, NULL)) {
result = "NA";
} else {
memset(brand, 0, sizeof(brand));
__get_cpuid(0x80000002, brand + 0x0, brand + 0x1, brand + 0x2, brand + 0x3);
__get_cpuid(0x80000003, brand + 0x4, brand + 0x5, brand + 0x6, brand + 0x7);
__get_cpuid(0x80000004, brand + 0x8, brand + 0x9, brand + 0xa, brand + 0xb);
result = string(reinterpret_cast<char *>(brand));
}
return result;
}

CpuInfo::CpuInfo() : m_vendor(get_cpu_vendor()), m_brand(get_cpu_brand()) {}

CpuInfo::~CpuInfo() {}
/**
Expand All @@ -46,12 +72,5 @@ uint32_t CpuInfo::model() const {
return (eax & 0x3FFF) | (eax & 0x3FF8000) >> 2 | (ebx & 0xff) << 24;
}

string CpuInfo::vendor() const {
unsigned int level = 0, eax = 0, ebx = 0, ecx = 0, edx = 0;
// hypervisor flag false, try to get the vendor name, see if it's a virtual cpu
__get_cpuid(level, &eax, &ebx, &ecx, &edx);
CPUVendorID vendorID{.ebx = ebx, .edx = edx, .ecx = ecx};
return vendorID.toString();
}
} // namespace os
} /* namespace license */
File renamed without changes.
48 changes: 36 additions & 12 deletions src/library/os/windows/cpu_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,42 @@ namespace license {
namespace os {
using namespace std;

CpuInfo::CpuInfo() {}
static string get_cpu_vendor() {
// hypervisor flag false, try to get the vendor name, see if it's a virtual cpu
int cpui[4] = {0};
__cpuidex(cpui, 0x0, 0x0);

char vendor[13];
memset(vendor, 0, sizeof(vendor));
*reinterpret_cast<int *>(vendor) = cpui[1];
*reinterpret_cast<int *>(vendor + 4) = cpui[3];
*reinterpret_cast<int *>(vendor + 8) = cpui[2];
return string(vendor, 12);
}

static string get_cpu_brand() {
int cpui[4] = {0};
__cpuid(cpui, 0x80000000);
int maxSupported = cpui[0];

char brand[0x41];
memset(brand, 0, sizeof(brand));
string result;
if (maxSupported >= 0x80000004) {
int instruction = 0x80000002;
for (int i = 0; i <= 2; ++i) {
__cpuidex(cpui, instruction + i, 0);
memcpy(&brand[i * sizeof(cpui)], cpui, sizeof(cpui));
}
result = string(brand);
} else {
result = "NA";
}

return result;
}

CpuInfo::CpuInfo() : m_vendor(get_cpu_vendor()), m_brand(get_cpu_brand()) {}

CpuInfo::~CpuInfo() {}
/**
Expand All @@ -38,17 +73,6 @@ uint32_t CpuInfo::model() const {
return (cpui[0] & 0x3FFF) | (cpui[0] & 0x3FF8000) >> 2 | (cpui[1] & 0xff) << 24;
}

string CpuInfo::vendor() const {
// hypervisor flag false, try to get the vendor name, see if it's a virtual cpu
int cpui[4] = {0};
__cpuidex(cpui, 0x0, 0x0);

char vendor[13];
memset(vendor, 0, sizeof(vendor));
*reinterpret_cast<int *>(vendor) = cpui[1];
*reinterpret_cast<int *>(vendor +4) = cpui[3];
*reinterpret_cast<int *>(vendor + 8) = cpui[2];
return string(vendor, 12);
}
} // namespace os
} /* namespace license */
51 changes: 49 additions & 2 deletions src/library/os/windows/execution_environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Created on: Dec 15, 2019
* Author: GC
*/
#include <windows.h>
#include <sys/stat.h>
#include <fstream>
#include <iostream>
Expand Down Expand Up @@ -31,8 +32,54 @@ ExecutionEnvironment::ExecutionEnvironment() {
}
}

//TODO
bool ExecutionEnvironment::is_docker() const { return false; }
#define MAX_UNITS 20
int wine_container() {
DWORD fileMaxLen;
size_t ndrives = 0;
DWORD fileFlags;
char volName[MAX_PATH], fileSysName[MAX_PATH];
DWORD volSerial = 0;
const DWORD dwSize = MAX_PATH;
char szLogicalDrives[MAX_PATH] = {0};

int result = 0;
const DWORD dwResult = GetLogicalDriveStrings(dwSize, szLogicalDrives);

if (dwResult > 0 && dwResult <= MAX_PATH) {
char* szSingleDrive = szLogicalDrives;
while (*szSingleDrive && ndrives < MAX_UNITS) {
// get the next drive
UINT driveType = GetDriveType(szSingleDrive);
if (driveType == DRIVE_FIXED) {
string name = szSingleDrive + string("/var/run/systemd/container");
try {
ifstream systemd_container(name);
if (systemd_container.good()) {
result = 1;
for (string line; getline(systemd_container, line);) {
if (line.find("docker") != string::npos) {
result = 1;
break;
} else if (line.find("lxc") != string::npos) {
result = 2;
break;
}
}
}
} catch (...) {
//no problem,we're just guessing
}
}
}
}
return result;
}

bool ExecutionEnvironment::is_docker() const {
// let's check we're not in linux under wine ;) ...
//int cont = wine_container();
return false;
}
//TODO
bool ExecutionEnvironment::is_container() const { return is_docker(); }
} // namespace os
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ FUNCTION_RETURN getMachineName(unsigned char identifier[6]) {
return result;
}

FUNCTION_RETURN getCpuId(unsigned char identifier[6]) {
return FUNC_RET_NOT_AVAIL;
}

//http://www.ok-soft-gmbh.com/ForStackOverflow/EnumMassStorage.c
//http://stackoverflow.com/questions/3098696/same-code-returns-diffrent-result-on-windows7-32-bit-system
#define MAX_UNITS 30
Expand Down
2 changes: 1 addition & 1 deletion test/library/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ IF(WIN32)
ELSE(WIN32)
add_executable(
test_os_linux
Os_Linux_test.cpp
os_linux_test.cpp
)

target_link_libraries(
Expand Down
File renamed without changes.

0 comments on commit 6dd7902

Please sign in to comment.