License.cpp 8.8 KB
Newer Older
Y
05.11  
yangwei.yao 已提交
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
#include "License.h"

namespace zilliz {
namespace vecwise {
namespace server {

ServerError
LicenseSave(const std::string& path,const int& deviceCount,const std::vector<std::string>& shas)
{
    std::ofstream file(path);

    boost::archive::binary_oarchive oa(file);
    oa << deviceCount;
    for(int i=0;i<deviceCount;i++)
    {
        oa << shas[i];
    }
    file.close();
    return  SERVER_SUCCESS;
}

ServerError
LicenseLoad(const std::string& path,int& deviceCount,std::vector<std::string>& shas)
{
    std::ifstream file(path);

    boost::archive::binary_iarchive ia(file);
    ia >> deviceCount;
    std::string sha;
    for(int i=0;i<deviceCount;i++)
    {
        ia >> sha;
        shas.push_back(sha);
    }
    file.close();
    return  SERVER_SUCCESS;
}

ServerError
Licensefileread(const std::string& path)
{
    std::ifstream fileread;
    fileread.open(path,std::ios::in);
    if(!fileread)
    {
Y
yangwei.yao 已提交
46
        printf("NO License\n");
Y
05.11  
yangwei.yao 已提交
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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
        return  SERVER_UNEXPECTED_ERROR;
    }
    fileread.close();
    return  SERVER_SUCCESS;
}

ServerError
Licensefileread (const std::string& path,std::ifstream& fileread){
    fileread.open(path,std::ios::in);
    if(!fileread)
    {
        printf("Can't open file\n");
        return  SERVER_UNEXPECTED_ERROR;
    }
    return  SERVER_SUCCESS;
}

ServerError
Licensefilewrite (const std::string& path,std::ofstream& filewrite) {
    filewrite.open(path,std::ios::out);
    if(!filewrite)
    {
        printf("Can't write file\n");
        return  SERVER_UNEXPECTED_ERROR;
    }
    return  SERVER_SUCCESS;
}

ServerError
LicenseGetCount(int &deviceCount)
{
    nvmlReturn_t result = nvmlInit();
    if (NVML_SUCCESS != result)
    {
        printf("Failed to initialize NVML: %s\n", nvmlErrorString(result));
        return SERVER_UNEXPECTED_ERROR;
    }
    cudaError_t error_id = cudaGetDeviceCount(&deviceCount);
    if (error_id != cudaSuccess)
    {
        printf("cudaGetDeviceCount returned %d\n-> %s\n", (int)error_id, cudaGetErrorString(error_id));
        printf("Result = FAIL\n");
        return SERVER_UNEXPECTED_ERROR;
    }
    return SERVER_SUCCESS;
}

ServerError
LicenseGetuuid(int& deviceCount, std::vector<std::string>& uuids)
{
    nvmlReturn_t result = nvmlInit();
    if (NVML_SUCCESS != result)
    {
        printf("Failed to initialize NVML: %s\n", nvmlErrorString(result));
        return SERVER_UNEXPECTED_ERROR;
    }
    cudaError_t error_id = cudaGetDeviceCount(&deviceCount);
    if (error_id != cudaSuccess)
    {
        printf("cudaGetDeviceCount returned %d\n-> %s\n", (int)error_id, cudaGetErrorString(error_id));
        printf("Result = FAIL\n");
        return SERVER_UNEXPECTED_ERROR;
    }

    if (deviceCount == 0)
    {
        printf("There are no available device(s) that support CUDA\n");
        return  SERVER_UNEXPECTED_ERROR;
    }

    for (int dev = 0; dev < deviceCount; ++dev)
    {
        nvmlDevice_t device;
        result = nvmlDeviceGetHandleByIndex(dev, &device);
        printf("device id: %d\n", dev);
        if (NVML_SUCCESS != result)
        {
            printf("Failed to get handle for device %i: %s\n", dev, nvmlErrorString(result));
            return SERVER_UNEXPECTED_ERROR;
        }

        char* uuid = (char*)malloc(80);
        unsigned int length = 80;
        nvmlReturn_t err = nvmlDeviceGetUUID(device, uuid, length);
        if(err != NVML_SUCCESS) {
            printf("nvmlDeviceGetUUID error: %d\n", err);
            return SERVER_UNEXPECTED_ERROR;
        }

        printf("\n device: %d, uuid = %s \n", dev, uuid);
        uuids.push_back(std::string(uuid));
        free(uuid);
        uuid = NULL;
    }
    return SERVER_SUCCESS;
}

ServerError
LicenseGetuuidmd5(const int& deviceCount,std::vector<std::string>& uuids,std::vector<std::string>& md5s)
{
    MD5_CTX ctx;
    unsigned char outmd[16];
    char temp[2];
    std::string md5="";
    for(int dev=0;dev<deviceCount;dev++)
    {
        md5.clear();
        memset(outmd,0, sizeof(outmd));
        MD5_Init(&ctx);
        MD5_Update(&ctx,uuids[dev].c_str(),uuids[dev].size());
        MD5_Final(outmd,&ctx);
        for(int i=0;i<16;i++)
        {
            std::sprintf(temp,"%02X",outmd[i]);
            md5+=temp;
        }
        md5s.push_back(md5);
    }
    return SERVER_SUCCESS;
}

ServerError
LicenseGetfilemd5(const std::string& path,std::string& filemd5)
{
//    MD5_CTX ctx;
//    unsigned char outmd[16];
//    char temp[2];
//    char* buffer = (char*)malloc(1024);
//    std::ifstream fileread;
//    Licensefileread(path,fileread);
//    memset(outmd,0,sizeof(outmd));
//    memset(buffer,0,sizeof(buffer));
//    MD5_Init(&ctx);
//    while(!fileread.eof()) {
//        fileread.get(buffer,1024,EOF);
//        MD5_Update(&ctx, buffer, strlen(buffer));
//        memset(buffer, 0, sizeof(buffer));
//    }
//    MD5_Final(outmd,&ctx);
//    for(int i=0;i<16;i++)
//    {
//        std::sprintf(temp,"%02X",outmd[i]);
//        filemd5+=temp;
//    }
//    free(buffer);
//    buffer=NULL;
//    fileread.close();
//    return SERVER_SUCCESS;


    filemd5.clear();

    std::ifstream file(path.c_str(), std::ifstream::binary);
    if (!file)
    {
        return -1;
    }

    MD5_CTX md5Context;
    MD5_Init(&md5Context);

    char buf[1024 * 16];
    while (file.good()) {
        file.read(buf, sizeof(buf));
        MD5_Update(&md5Context, buf, file.gcount());
    }

    unsigned char result[MD5_DIGEST_LENGTH];
    MD5_Final(result, &md5Context);

    char hex[35];
    memset(hex, 0, sizeof(hex));
    for (int i = 0; i < MD5_DIGEST_LENGTH; ++i)
    {
        sprintf(hex + i * 2, "%02X", result[i]);
    }
    hex[32] = '\0';
    filemd5 = std::string(hex);

    return SERVER_SUCCESS;
}

ServerError
LicenseGetuuidsha(const int& deviceCount,std::vector<std::string>& uuids,std::vector<std::string>& shas)
{
    SHA256_CTX ctx;
    unsigned char outmd[32];
    char temp[2];
    std::string sha="";
    for(int dev=0;dev<deviceCount;dev++)
    {
        sha.clear();
        memset(outmd,0, sizeof(outmd));
        SHA256_Init(&ctx);
        SHA256_Update(&ctx,uuids[dev].c_str(),uuids[dev].size());
        SHA256_Final(outmd,&ctx);
        for(int i=0;i<32;i++)
        {
            std::sprintf(temp,"%02X",outmd[i]);
            sha += temp;
        }
        shas.push_back(sha);
    }
    return SERVER_SUCCESS;
}

ServerError
LicenseGetfiletime(const std::string& path,time_t& last_time)
{
    struct stat buf;
    stat(path.c_str(),&buf);
    last_time =buf.st_mtime;
    return SERVER_SUCCESS;
}

ServerError
LicenseGetfilesize(const std::string& path,off_t& file_size)
{
    struct stat buf;
    if(stat(path.c_str(),&buf)==-1)
    {
        printf(" GET fiel_size is wrong");
        return  SERVER_UNEXPECTED_ERROR;
    }
    file_size =buf.st_size;
    return SERVER_SUCCESS;
}

ServerError
LicenseLegalitycheck(const std::string& path)
{
    int deviceCount;
    std::vector<std::string> uuids;
    LicenseGetuuid(deviceCount,uuids);
    std::vector<std::string> shas;
    LicenseGetuuidsha(deviceCount,uuids,shas);


    int deviceCountcheck;
    std::vector<std::string> shascheck;
    LicenseLoad(path,deviceCountcheck,shascheck);

    if(deviceCount!=deviceCountcheck)
    {
        printf("deviceCount is wrong\n");
        return  SERVER_UNEXPECTED_ERROR;
    }
    for(int i=0;i<deviceCount;i++)
    {
        if(shascheck[i]!=shas[i])
        {
            printf("uuidsha %d is wrong\n",i);
            return  SERVER_UNEXPECTED_ERROR;
        }
    }

    return SERVER_SUCCESS;
}

ServerError
LicensefileSave(const std::string& path,const std::string& path2){

    std::string filemd5;
    LicenseGetfilemd5(path,filemd5);
    std::ofstream file(path2);
    time_t last_time;
    LicenseGetfiletime(path,last_time);

    boost::archive::binary_oarchive oa(file);
    oa << filemd5;
    oa << last_time;
    file.close();
    return  SERVER_SUCCESS;
}

ServerError
LicensefileLoad(const std::string& path2,std::string& filemd5,time_t& last_time){

    std::ifstream file(path2);

    boost::archive::binary_iarchive ia(file);
    ia >> filemd5;
    ia >> last_time;
    file.close();
    return  SERVER_SUCCESS;
}

ServerError
LicenseIntegritycheck(const std::string& path,const std::string& path2)
    {
        std::string filemd5;
        LicenseGetfilemd5(path,filemd5);
        time_t  last_time;
        LicenseGetfiletime(path,last_time);

        time_t last_timecheck;
        std::string filemd5check;
        LicensefileLoad(path2,filemd5check,last_timecheck);

        if(filemd5!=filemd5check)
        {
            printf("This file has been modified\n");
            return SERVER_UNEXPECTED_ERROR;
        }
        if(last_time!=last_timecheck)
        {
            printf("last_time is wrong\n");
            return  SERVER_UNEXPECTED_ERROR;
        }

        return SERVER_SUCCESS;
    }

ServerError
LicenseValidate(const std::string& path) {


    return SERVER_SUCCESS;
}



}
}
}