gcontini
2020-03-14 35087e2c3f200639cf32c96e81cdbb08a5acb8eb
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
97
98
99
100
101
102
103
104
105
106
107
108
/*#ifdef _MSC_VER
#include <Windows.h>
#endif*/
#include "../../base/logger.h"
#include "../os.h"
 
#include <licensecc/datatypes.h>
#include <iphlpapi.h>
#include <stdio.h>
//#pragma comment(lib, "IPHLPAPI.lib")
 
unsigned char* unbase64(const char* ascii, int len, int *flen);
 
FUNCTION_RETURN getMachineName(unsigned char identifier[6]) {
    FUNCTION_RETURN result = FUNC_RET_ERROR;
    char buffer[MAX_COMPUTERNAME_LENGTH + 1];
    int bufsize = MAX_COMPUTERNAME_LENGTH + 1;
    const BOOL cmpName = GetComputerName(buffer, (unsigned long*)&bufsize);
    if (cmpName) {
        strncpy((char*)identifier, buffer, 6);
        result = FUNC_RET_OK;
    }
    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
//bug check return with diskinfos == null func_ret_ok
FUNCTION_RETURN getDiskInfos(DiskInfo * diskInfos, size_t * disk_info_size) {
    DWORD fileMaxLen;
    int ndrives = 0;
    DWORD fileFlags;
    char volName[MAX_PATH], fileSysName[MAX_PATH];
    DWORD volSerial = 0;
    const DWORD dwSize = MAX_PATH;
    char szLogicalDrives[MAX_PATH] = { 0 };
 
 
    FUNCTION_RETURN return_value = FUNC_RET_NOT_AVAIL;
    const DWORD dwResult = GetLogicalDriveStrings(dwSize, szLogicalDrives);
 
    if (dwResult > 0 && dwResult <= MAX_PATH) {
        return_value = FUNC_RET_OK;
        char* szSingleDrive = szLogicalDrives;
        while (*szSingleDrive && ndrives < MAX_UNITS) {
 
            // get the next drive
            UINT driveType = GetDriveType(szSingleDrive);
            if (driveType == DRIVE_FIXED) {
                BOOL success = GetVolumeInformation(szSingleDrive, volName, MAX_PATH,
                                                    &volSerial, &fileMaxLen, &fileFlags, fileSysName,
                                                    MAX_PATH);
                if (success) {
                    LOG_INFO("drive         : %s", szSingleDrive);
                    LOG_INFO("Volume Name   : %s", volName);
                    LOG_INFO("Volume Serial : 0x%x", volSerial);
                    LOG_DEBUG("Max file length : %d", fileMaxLen);
                    LOG_DEBUG("Filesystem      : %s", fileSysName);
                    if (diskInfos != NULL) {
                        if (ndrives < (int)*disk_info_size) {
                            diskInfos[ndrives].id = ndrives;
                            strncpy(diskInfos[ndrives].device, volName, cmin(MAX_PATH, sizeof(volName)) - 1);
                            strncpy(diskInfos[ndrives].label, fileSysName,
                                    cmin(sizeof(diskInfos[ndrives].label), sizeof(fileSysName)) - 1);
                            memcpy(diskInfos[ndrives].disk_sn, &volSerial, sizeof(DWORD));
                            diskInfos[ndrives].preferred = (szSingleDrive[0] == 'C');
                        } else {
                            return_value = FUNC_RET_BUFFER_TOO_SMALL;
                        }
                    }
                    ndrives++;
                } else {
                    LOG_WARN("Unable to retrieve information of '%s'", szSingleDrive);
                }
            } else {
                LOG_INFO("This volume is not fixed : %s, type: %d",    szSingleDrive);
            }
            szSingleDrive += strlen(szSingleDrive) + 1;
        }
    }
    if (diskInfos == NULL || *disk_info_size == 0) {
        if (ndrives > 0) {
            return_value = FUNC_RET_OK;
        } else {
            return_value = FUNC_RET_NOT_AVAIL;
            LOG_INFO("No fixed drive was detected");
        }
        *disk_info_size = ndrives;
    } else {
        *disk_info_size = cmin(ndrives, *disk_info_size);
    }
    return return_value;
}
 
 
FUNCTION_RETURN getModuleName(char buffer[MAX_PATH]) {
    FUNCTION_RETURN result = FUNC_RET_OK;
    const DWORD wres = GetModuleFileName(NULL, buffer, MAX_PATH);
    if (wres == 0) {
        result = FUNC_RET_ERROR;
    }
    return result;
}