licentivo-sdk 0.11.6 → 0.11.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/lib/licentivo.rb +5 -2
- data/native/device_display.h +74 -0
- data/native/feature_tasks.h +90 -9
- data/native/licentivo.c +30 -6
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ee9b78c44cd62588e1588122283867c217ead94a495a3d84dbdd6c0ecc7d53d6
|
|
4
|
+
data.tar.gz: 6ec6e1204465a4d6fdf3a996d9ede12ac318a57e941dc00d591dae48008f8577
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9a32bb1afdeb755ae385bfd39812372c99f8819dee1b665e31f183e876827f3f1cdfd6d89ed614fb9c052462bfa648956c56aadeba9defd45c2f2b8c94ed92a3
|
|
7
|
+
data.tar.gz: 4d631f09d63290225e70035bcac3e6ef17d2ffb408017ae3d798e596e0c86a82cce5c7fa708084f4b12b583b51bac96d0c1d777cc692b9b38262826f8ee5bba2
|
data/README.md
CHANGED
|
@@ -13,10 +13,10 @@ Requirements: Ruby 3.1+, Fiddle, C11 compiler and libcurl development files. Sou
|
|
|
13
13
|
Install the public SDK package in your existing application project:
|
|
14
14
|
|
|
15
15
|
```sh
|
|
16
|
-
gem install licentivo-sdk --version 0.11.
|
|
16
|
+
gem install licentivo-sdk --version 0.11.8
|
|
17
17
|
```
|
|
18
18
|
|
|
19
|
-
For Bundler, use `gem "licentivo-sdk", "= 0.11.
|
|
19
|
+
For Bundler, use `gem "licentivo-sdk", "= 0.11.8"` in your Gemfile.
|
|
20
20
|
|
|
21
21
|
No SDK source download, extraction or local path dependency is required.
|
|
22
22
|
|
data/lib/licentivo.rb
CHANGED
|
@@ -4,7 +4,7 @@ require 'json'
|
|
|
4
4
|
require 'monitor'
|
|
5
5
|
|
|
6
6
|
module Licentivo
|
|
7
|
-
VERSION = '0.11.
|
|
7
|
+
VERSION = '0.11.8'
|
|
8
8
|
|
|
9
9
|
class Error < StandardError
|
|
10
10
|
attr_reader :code, :http_status, :request_id, :retryable, :retry_after_seconds
|
|
@@ -159,7 +159,10 @@ module Licentivo
|
|
|
159
159
|
end
|
|
160
160
|
def retry_pending_logouts = checked(:retry_pending_logouts)
|
|
161
161
|
def logout_pending? = @lock.synchronize { Native.call(:logout_pending, active) != 0 }
|
|
162
|
-
def status
|
|
162
|
+
def status
|
|
163
|
+
result = read_json(:status)
|
|
164
|
+
@signed_out ? result.merge(signed_out_status) : result
|
|
165
|
+
end
|
|
163
166
|
def feature_status(feature) = @signed_out ? signed_out_status : read_json(:feature_status, Native.input(feature))
|
|
164
167
|
def reserve(feature, quantity, job_id) = read_json(:reserve, Native.input(feature), integer(quantity), Native.input(job_id))
|
|
165
168
|
def consume(feature, quantity, job_id) = read_json(:consume, Native.input(feature), integer(quantity), Native.input(job_id))
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
#ifndef LICENTIVO_DEVICE_DISPLAY_H
|
|
2
|
+
#define LICENTIVO_DEVICE_DISPLAY_H
|
|
3
|
+
|
|
4
|
+
#if defined(_WIN32) && !defined(PROCESSOR_ARCHITECTURE_ARM64)
|
|
5
|
+
#define PROCESSOR_ARCHITECTURE_ARM64 12
|
|
6
|
+
#endif
|
|
7
|
+
|
|
8
|
+
#ifndef _WIN32
|
|
9
|
+
#include <sys/statvfs.h>
|
|
10
|
+
#ifdef __APPLE__
|
|
11
|
+
#include <sys/sysctl.h>
|
|
12
|
+
#endif
|
|
13
|
+
#endif
|
|
14
|
+
|
|
15
|
+
/* Optional, cached display information. Never part of the licensing identity. */
|
|
16
|
+
#ifdef _WIN32
|
|
17
|
+
static void display_registry(const wchar_t*path,const wchar_t*value,char*out,size_t size){
|
|
18
|
+
HKEY key;out[0]=0;if(RegOpenKeyExW(HKEY_LOCAL_MACHINE,path,0,KEY_READ|KEY_WOW64_64KEY,&key)!=ERROR_SUCCESS)return;
|
|
19
|
+
wchar_t text_value[512]={0};DWORD bytes=sizeof(text_value),type_value=0;
|
|
20
|
+
if(RegQueryValueExW(key,value,NULL,&type_value,(BYTE*)text_value,&bytes)==ERROR_SUCCESS&&(type_value==REG_SZ||type_value==REG_EXPAND_SZ)){text_value[511]=0;WideCharToMultiByte(CP_UTF8,0,text_value,-1,out,(int)size,NULL,NULL);}RegCloseKey(key);
|
|
21
|
+
}
|
|
22
|
+
#endif
|
|
23
|
+
static void display_text(cJSON*info,const char*key,const char*value,size_t limit){
|
|
24
|
+
char clean[1024];size_t used=0,count=0;
|
|
25
|
+
for(size_t i=0;value[i]&&count<limit&&used+4<sizeof(clean);){
|
|
26
|
+
unsigned char ch=(unsigned char)value[i];size_t n=ch<0x80?1:ch>=0xc2&&ch<=0xdf?2:ch>=0xe0&&ch<=0xef?3:ch>=0xf0&&ch<=0xf4?4:0;
|
|
27
|
+
if(!n){i++;continue;}int valid=1;for(size_t k=1;k<n;k++)if(!value[i+k]||((unsigned char)value[i+k]&0xc0)!=0x80)valid=0;if(!valid){i++;continue;}
|
|
28
|
+
if(!(n==1&&(ch<32||ch==127))&&!(n==2&&ch==0xc2&&(unsigned char)value[i+1]<0xa0)){memcpy(clean+used,value+i,n);used+=n;count++;}i+=n;
|
|
29
|
+
}
|
|
30
|
+
while(used&&isspace((unsigned char)clean[used-1]))used--;
|
|
31
|
+
clean[used]=0;
|
|
32
|
+
cJSON_AddStringToObject(info,key,clean);
|
|
33
|
+
}
|
|
34
|
+
static void collect_display_details(cJSON*cfg){
|
|
35
|
+
char system[512]={0},cpu[512]={0},gpu[1024]={0},arch[40]={0};uint64_t memory=0,storage=0;
|
|
36
|
+
#ifdef _WIN32
|
|
37
|
+
const wchar_t*version=L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion";
|
|
38
|
+
char product[256],build[64],edition[64];display_registry(version,L"ProductName",product,sizeof(product));display_registry(version,L"CurrentBuildNumber",build,sizeof(build));display_registry(version,L"DisplayVersion",edition,sizeof(edition));
|
|
39
|
+
if(atoi(build)>=22000){char*ten=strstr(product,"Windows 10");if(ten)ten[9]='1';}
|
|
40
|
+
snprintf(system,sizeof(system),"%s %s (%s)",product,edition,build);
|
|
41
|
+
display_registry(L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",L"ProcessorNameString",cpu,sizeof(cpu));
|
|
42
|
+
ULONGLONG kb=0;typedef BOOL(WINAPI*display_memory_fn)(PULONGLONG);
|
|
43
|
+
display_memory_fn read_memory=(display_memory_fn)GetProcAddress(GetModuleHandleW(L"kernel32.dll"),"GetPhysicallyInstalledSystemMemory");if(read_memory&&read_memory(&kb))memory=kb/1024;
|
|
44
|
+
SYSTEM_INFO native;GetNativeSystemInfo(&native);snprintf(arch,sizeof(arch),"%s",native.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64?"x64":native.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_ARM64?"arm64":"x86");
|
|
45
|
+
wchar_t drive[MAX_PATH];UINT n=GetSystemDirectoryW(drive,MAX_PATH);if(n&&n<MAX_PATH){drive[3]=0;ULARGE_INTEGER free_value,total,available;if(GetDiskFreeSpaceExW(drive,&available,&total,&free_value))storage=total.QuadPart/1073741824;}
|
|
46
|
+
HKEY video;if(RegOpenKeyExW(HKEY_LOCAL_MACHINE,L"SYSTEM\\CurrentControlSet\\Control\\Video",0,KEY_READ,&video)==ERROR_SUCCESS){
|
|
47
|
+
for(DWORD i=0;i<32;i++){wchar_t name[128];DWORD count=128;if(RegEnumKeyExW(video,i,name,&count,NULL,NULL,NULL,NULL)!=ERROR_SUCCESS)break;wchar_t path[256];swprintf(path,256,L"SYSTEM\\CurrentControlSet\\Control\\Video\\%ls\\0000",name);char model[256];display_registry(path,L"DriverDesc",model,sizeof(model));if(model[0]&&!strstr(gpu,model)&&strlen(gpu)+strlen(model)+4<sizeof(gpu)){if(gpu[0])strcat(gpu," / ");strcat(gpu,model);}}
|
|
48
|
+
RegCloseKey(video);
|
|
49
|
+
}
|
|
50
|
+
#else
|
|
51
|
+
struct statvfs disk;if(statvfs("/",&disk)==0)storage=(uint64_t)disk.f_blocks*disk.f_frsize/1073741824;
|
|
52
|
+
#if defined(__aarch64__) || defined(__arm64__)
|
|
53
|
+
strcpy(arch,"arm64");
|
|
54
|
+
#elif defined(__x86_64__)
|
|
55
|
+
strcpy(arch,"x64");
|
|
56
|
+
#elif defined(__arm__)
|
|
57
|
+
strcpy(arch,"arm");
|
|
58
|
+
#else
|
|
59
|
+
strcpy(arch,"x86");
|
|
60
|
+
#endif
|
|
61
|
+
#ifdef __APPLE__
|
|
62
|
+
size_t size=sizeof(cpu);if(sysctlbyname("machdep.cpu.brand_string",cpu,&size,NULL,0)!=0)cpu[0]=0;
|
|
63
|
+
uint64_t bytes=0;size=sizeof(bytes);if(sysctlbyname("hw.memsize",&bytes,&size,NULL,0)==0)memory=bytes/1048576;
|
|
64
|
+
char version[128]={0};size=sizeof(version);if(sysctlbyname("kern.osproductversion",version,&size,NULL,0)==0)snprintf(system,sizeof(system),"macOS %s",version);
|
|
65
|
+
#else
|
|
66
|
+
FILE*file=fopen("/etc/os-release","r");char line[4096];if(file){while(fgets(line,sizeof(line),file)){if(!strncmp(line,"PRETTY_NAME=",12)){char*value=line+12;while(*value=='"'||isspace((unsigned char)*value))value++;size_t length=strcspn(value,"\r\n");while(length&&(value[length-1]=='"'||isspace((unsigned char)value[length-1])))length--;snprintf(system,sizeof(system),"%.*s",(int)length,value);break;}}fclose(file);}
|
|
67
|
+
char implementer[64]={0},part[64]={0};file=fopen("/proc/cpuinfo","r");if(file){while(fgets(line,sizeof(line),file)){char*value=strchr(line,':');if(!value)continue;value++;while(isspace((unsigned char)*value))value++;if(!strncmp(line,"model name",10)||!strncmp(line,"Hardware",8)||!strncmp(line,"Processor",9)){snprintf(cpu,sizeof(cpu),"%s",value);break;}if(!implementer[0]&&!strncmp(line,"CPU implementer",15))snprintf(implementer,sizeof(implementer),"%s",value);if(!part[0]&&!strncmp(line,"CPU part",8))snprintf(part,sizeof(part),"%s",value);}fclose(file);}if(!cpu[0]&&part[0])snprintf(cpu,sizeof(cpu),"ARM CPU (implementer %s, part %s)",implementer,part);
|
|
68
|
+
long pages=sysconf(_SC_PHYS_PAGES),page_size=sysconf(_SC_PAGESIZE);if(pages>0&&page_size>0)memory=(uint64_t)pages*(uint64_t)page_size/1048576;
|
|
69
|
+
#endif
|
|
70
|
+
#endif
|
|
71
|
+
cJSON*info=cJSON_CreateObject();display_text(info,"os_version",system,160);display_text(info,"cpu",cpu,160);display_text(info,"gpu",gpu,240);display_text(info,"system_type",arch,40);cJSON_AddNumberToObject(info,"memory_mb",(double)memory);cJSON_AddNumberToObject(info,"storage_gb",(double)storage);
|
|
72
|
+
cJSON_DeleteItemFromObjectCaseSensitive(cfg,"device_info");cJSON_AddItemToObject(cfg,"device_info",info);
|
|
73
|
+
}
|
|
74
|
+
#endif
|
data/native/feature_tasks.h
CHANGED
|
@@ -44,12 +44,35 @@ static void task_leave(ln_client*c){view_lock(c);c->task_busy=0;view_unlock(c);}
|
|
|
44
44
|
static char*task_path(ln_client*c,const char*id){
|
|
45
45
|
if(!id||!id[0]||strlen(id)>80)return NULL;
|
|
46
46
|
for(const unsigned char*p=(const unsigned char*)id;*p;p++)if(*p<33||*p>126)return NULL;
|
|
47
|
-
char source[65],digest[65],input[256];lock(c);
|
|
48
|
-
|
|
49
|
-
if(ok
|
|
47
|
+
char source[65],digest[65],legacy_digest[65],input[256];lock(c);
|
|
48
|
+
int ok=source_hash(c,source);const char*connection=text(c->cfg,"launch_connection_id");
|
|
49
|
+
if(!ok){unlock(c);return NULL;}
|
|
50
|
+
if(ok){snprintf(input,sizeof(input),"%s\n%s",source,id);ok=hash(input,legacy_digest);}
|
|
51
|
+
if(connection&&connection[0]){
|
|
52
|
+
cJSON*claims=cache_valid(c)?claims_from(c->cached):NULL;
|
|
53
|
+
const char*origin=text(c->cfg,"base_url"),*product=text(claims,"product_id"),*license=text(claims,"license_id"),*dev=text(claims,"device_id");
|
|
54
|
+
ok=claims&&origin&&product&&license&&dev;
|
|
55
|
+
if(ok){size_t size=strlen(origin)+strlen(product)+strlen(license)+strlen(dev)+32;char*scope=malloc(size);ok=scope!=NULL;if(ok){snprintf(scope,size,"LicentivoTask/v2\n%s\n%s\n%s\n%s",origin,product,license,dev);ok=hash(scope,source);free(scope);}}
|
|
56
|
+
cJSON_Delete(claims);
|
|
57
|
+
}
|
|
58
|
+
unlock(c);
|
|
50
59
|
if(!ok)return NULL;snprintf(input,sizeof(input),"%s\n%s",source,id);if(!hash(input,digest))return NULL;
|
|
51
60
|
const char*cache=text(c->cfg,"cache_path");if(!cache)return NULL;
|
|
52
|
-
char*path=malloc(strlen(cache)+78);if(path)sprintf(path,"%s.task-%s.json",cache,digest);
|
|
61
|
+
char*path=malloc(strlen(cache)+78);if(!path)return NULL;sprintf(path,"%s.task-%s.json",cache,digest);
|
|
62
|
+
if(strcmp(digest,legacy_digest)){
|
|
63
|
+
struct stat info;if(stat(path,&info)!=0&&errno==ENOENT){
|
|
64
|
+
char*legacy=malloc(strlen(cache)+78);if(!legacy){free(path);return NULL;}sprintf(legacy,"%s.task-%s.json",cache,legacy_digest);
|
|
65
|
+
if(stat(legacy,&info)==0){
|
|
66
|
+
#ifdef _WIN32
|
|
67
|
+
ok=state_windows_move(legacy,path);
|
|
68
|
+
#else
|
|
69
|
+
ok=rename(legacy,path)==0;
|
|
70
|
+
#endif
|
|
71
|
+
}else ok=errno==ENOENT;
|
|
72
|
+
free(legacy);if(!ok){free(path);return NULL;}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return path;
|
|
53
76
|
}
|
|
54
77
|
static cJSON*task_read(const char*path,const char*id){
|
|
55
78
|
struct stat info;if(stat(path,&info)==0){if(info.st_size>4096)return NULL;char*raw=readfile(path,4096);cJSON*task=raw?ln_json(raw):NULL;free(raw);int64_t version;
|
|
@@ -68,10 +91,66 @@ static int task_finish(ln_client*c,const char*path,cJSON*task,int succeeded){
|
|
|
68
91
|
if(!ok)return task_error(c,"RESERVATION_CLOSED");task_state(task,expected);
|
|
69
92
|
return write_state(path,task)?1:task_error(c,"TASK_JOURNAL_ERROR");
|
|
70
93
|
}
|
|
94
|
+
/* Renew only the original reservation. The worker owns no business callback
|
|
95
|
+
* and never changes the task journal or decides the business outcome. */
|
|
96
|
+
typedef struct {
|
|
97
|
+
ln_client*client;const char*feature,*operation,*reservation;int64_t quantity;double deadline;int started;
|
|
98
|
+
#ifdef _WIN32
|
|
99
|
+
HANDLE thread,event;
|
|
100
|
+
#else
|
|
101
|
+
pthread_t thread;pthread_mutex_t mutex;pthread_cond_t condition;int stop;
|
|
102
|
+
#endif
|
|
103
|
+
} task_renewal;
|
|
104
|
+
static int task_renewal_wait(task_renewal*r,double seconds){
|
|
105
|
+
#ifdef _WIN32
|
|
106
|
+
return WaitForSingleObject(r->event,(DWORD)(seconds*1000))==WAIT_TIMEOUT;
|
|
107
|
+
#else
|
|
108
|
+
struct timespec until;clock_gettime(CLOCK_REALTIME,&until);int64_t nanos=(int64_t)(seconds*1e9)+until.tv_nsec;until.tv_sec+=(time_t)(nanos/1000000000);until.tv_nsec=(long)(nanos%1000000000);
|
|
109
|
+
pthread_mutex_lock(&r->mutex);int result=0;while(!r->stop&&result!=ETIMEDOUT)result=pthread_cond_timedwait(&r->condition,&r->mutex,&until);int active=!r->stop;pthread_mutex_unlock(&r->mutex);return active;
|
|
110
|
+
#endif
|
|
111
|
+
}
|
|
112
|
+
#ifdef _WIN32
|
|
113
|
+
static DWORD WINAPI task_renewal_run(LPVOID argument){
|
|
114
|
+
#else
|
|
115
|
+
static void*task_renewal_run(void*argument){
|
|
116
|
+
#endif
|
|
117
|
+
task_renewal*r=argument;double until=r->deadline,interval=60;
|
|
118
|
+
for(;;){
|
|
119
|
+
double remaining=until-task_wall_time();if(remaining<=0)break;double delay=remaining/3;if(delay<.01)delay=.01;if(delay>interval)delay=interval;
|
|
120
|
+
if(!task_renewal_wait(r,delay))break;
|
|
121
|
+
char*raw=ln_reserve(r->client,r->feature,r->quantity,r->operation);cJSON*hold=raw?ln_json(raw):NULL;ln_free_string(raw);
|
|
122
|
+
if(!hold){interval=5;continue;}
|
|
123
|
+
const char*status=text(hold,"status"),*id=text(hold,"reservation_id"),*operation=text(hold,"operation_id");double renewed;
|
|
124
|
+
int valid=status&&!strcmp(status,"pending")&&id&&!strcmp(id,r->reservation)&&operation&&!strcmp(operation,r->operation)&&task_deadline(text(hold,"expires_at"),&renewed);
|
|
125
|
+
cJSON_Delete(hold);if(!valid)break;until=renewed;interval=60;
|
|
126
|
+
}
|
|
127
|
+
return 0;
|
|
128
|
+
}
|
|
129
|
+
static int task_renewal_start(task_renewal*r,ln_client*c,cJSON*task,double deadline){
|
|
130
|
+
r->client=c;r->feature=text(task,"feature");r->operation=text(task,"operation_id");r->reservation=text(task,"reservation_id");num(task,"quantity",&r->quantity);r->deadline=deadline;
|
|
131
|
+
#ifdef _WIN32
|
|
132
|
+
r->event=CreateEventW(NULL,TRUE,FALSE,NULL);if(!r->event)return 0;
|
|
133
|
+
r->thread=CreateThread(NULL,0,task_renewal_run,r,0,NULL);if(!r->thread){CloseHandle(r->event);return 0;}
|
|
134
|
+
#else
|
|
135
|
+
if(pthread_mutex_init(&r->mutex,NULL))return 0;
|
|
136
|
+
if(pthread_cond_init(&r->condition,NULL)){pthread_mutex_destroy(&r->mutex);return 0;}
|
|
137
|
+
if(pthread_create(&r->thread,NULL,task_renewal_run,r)){pthread_cond_destroy(&r->condition);pthread_mutex_destroy(&r->mutex);return 0;}
|
|
138
|
+
#endif
|
|
139
|
+
r->started=1;return 1;
|
|
140
|
+
}
|
|
141
|
+
static void task_renewal_stop(task_renewal*r){
|
|
142
|
+
if(!r->started)return;
|
|
143
|
+
#ifdef _WIN32
|
|
144
|
+
SetEvent(r->event);WaitForSingleObject(r->thread,INFINITE);CloseHandle(r->thread);CloseHandle(r->event);
|
|
145
|
+
#else
|
|
146
|
+
pthread_mutex_lock(&r->mutex);r->stop=1;pthread_cond_signal(&r->condition);pthread_mutex_unlock(&r->mutex);pthread_join(r->thread,NULL);pthread_cond_destroy(&r->condition);pthread_mutex_destroy(&r->mutex);
|
|
147
|
+
#endif
|
|
148
|
+
r->started=0;
|
|
149
|
+
}
|
|
71
150
|
int ln_run_metered_feature(ln_client*c,const char*feature,int64_t quantity,const char*id,ln_feature_action action,void*context){
|
|
72
151
|
if(!c||!feature||!action||quantity<=0)return c?task_error(c,"TASK_INPUT_INVALID"):0;
|
|
73
152
|
if(!task_enter(c))return task_error(c,"TASK_BUSY");
|
|
74
|
-
int ok=0;char*path=task_path(c,id);cJSON*task=path?task_read(path,id):NULL;
|
|
153
|
+
task_renewal renewal={0};int ok=0;char*path=task_path(c,id);cJSON*task=path?task_read(path,id):NULL;
|
|
75
154
|
if(!task){task_error(c,"TASK_JOURNAL_ERROR");goto done;}
|
|
76
155
|
const char*state=text(task,"state");
|
|
77
156
|
if(state[0]){int64_t old_quantity;if(!text(task,"feature")||strcmp(text(task,"feature"),feature)||!num(task,"quantity",&old_quantity)||old_quantity!=quantity){task_error(c,"OPERATION_ID_CONFLICT");goto done;}}
|
|
@@ -92,14 +171,16 @@ int ln_run_metered_feature(ln_client*c,const char*feature,int64_t quantity,const
|
|
|
92
171
|
cJSON_ReplaceItemInObjectCaseSensitive(task,"reservation_id",cJSON_CreateString(text(hold,"reservation_id")));cJSON_Delete(hold);task_state(task,"business_running");
|
|
93
172
|
if(!write_state(path,task)){raw=ln_cancel(c,text(task,"reservation_id"),id);ln_free_string(raw);task_error(c,"TASK_JOURNAL_ERROR");goto done;}
|
|
94
173
|
int allowed=ln_check_feature(c,feature),unexpired=valid_deadline&&deadline>task_wall_time();
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
174
|
+
int renewing=allowed&&unexpired&&task_renewal_start(&renewal,c,task,deadline);
|
|
175
|
+
if(!renewing||!action(context)){
|
|
176
|
+
const char*failure=!allowed?"FEATURE_NOT_ENTITLED":!unexpired?"RESERVATION_EXPIRED":!renewing?"RESERVATION_RENEWAL_UNAVAILABLE":"BUSINESS_ACTION_FAILED";
|
|
177
|
+
task_state(task,"cancel_pending");int saved=write_state(path,task);task_renewal_stop(&renewal);if(saved)task_finish(c,path,task,0);task_error(c,failure);goto done;
|
|
98
178
|
}
|
|
99
179
|
task_state(task,"commit_pending");if(!write_state(path,task)){task_error(c,"BUSINESS_COMPLETED_JOURNAL_ERROR");goto done;}
|
|
180
|
+
task_renewal_stop(&renewal);
|
|
100
181
|
ok=task_finish(c,path,task,1);
|
|
101
182
|
done:
|
|
102
|
-
cJSON_Delete(task);free(path);task_leave(c);return ok;
|
|
183
|
+
task_renewal_stop(&renewal);cJSON_Delete(task);free(path);task_leave(c);return ok;
|
|
103
184
|
}
|
|
104
185
|
int ln_resolve_metered_feature(ln_client*c,const char*id,int succeeded){
|
|
105
186
|
if(!c)return 0;if(!task_enter(c))return task_error(c,"TASK_BUSY");
|
data/native/licentivo.c
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
#define _POSIX_C_SOURCE 200809L
|
|
2
|
+
#ifdef __APPLE__
|
|
3
|
+
#define _DARWIN_C_SOURCE 1
|
|
4
|
+
#endif
|
|
2
5
|
#ifdef _WIN32
|
|
3
6
|
#ifndef _WIN32_WINNT
|
|
4
7
|
#define _WIN32_WINNT 0x0600
|
|
@@ -163,7 +166,7 @@ static int advanced_claims(const cJSON*c){
|
|
|
163
166
|
if(get(x,"app_version")&&!app_version(text(x,"app_version")))return 0;
|
|
164
167
|
if(!num(c,"issued_at",&issued)||!num(c,"expires_at",&expires))return 0;
|
|
165
168
|
const cJSON*policy=get(c,"runtime_policy");int64_t heartbeat=0;if(!strcmp(model,"floating")&&(!uuid(text(x,"session_id"))||session<=issued||expires>session||!num(policy,"heartbeat_seconds",&heartbeat)||heartbeat==0||!strcmp(text(policy,"offline_mode")?text(policy,"offline_mode"):"","permanent")))return 0;
|
|
166
|
-
if(get(x,"launch_connection_id")||get(x,"environment")||get(x,"binding_version")){int64_t binding=0,h=0,o=0;const char*env=text(x,"environment"),*mode=text(policy,"offline_mode");if(!uuid(text(x,"launch_connection_id"))||!env||(strcmp(env,"live")&&strcmp(env,"sandbox"))||!num(x,"binding_version",&binding)||binding<1
|
|
169
|
+
if(get(x,"launch_connection_id")||get(x,"environment")||get(x,"binding_version")){int64_t binding=0,h=0,o=0;const char*env=text(x,"environment"),*mode=text(policy,"offline_mode");if(!uuid(text(x,"launch_connection_id"))||!env||(strcmp(env,"live")&&strcmp(env,"sandbox"))||!num(x,"binding_version",&binding)||binding<1||!uuid(text(x,"session_id"))||session<=issued||expires>session||!num(policy,"heartbeat_seconds",&h)||h<600||!mode||!strcmp(mode,"permanent")||!num(policy,"offline_seconds",&o)||o>86400)return 0;}
|
|
167
170
|
if(released>issued||(maintenance>0&&(!text(x,"app_version")||released==0||released>maintenance)))return 0;
|
|
168
171
|
return 1;
|
|
169
172
|
}
|
|
@@ -299,7 +302,7 @@ static void view_unlock(ln_client*c){
|
|
|
299
302
|
static void publish(ln_client*c,int online){view_lock(c);cJSON_Delete(c->view);c->view=cJSON_Duplicate(c->cached,1);c->view_wall=c->wall;c->view_tick=c->tick;c->view_online=online;snprintf(c->view_error.code,sizeof(c->view_error.code),"%s",c->error);snprintf(c->view_error.request_id,sizeof(c->view_error.request_id),"%s",c->request_id);c->view_error.http_status=(int)c->http_status;c->view_error.retryable=c->retryable;c->view_error.retry_after_seconds=c->retry_after;view_unlock(c);}
|
|
300
303
|
static cJSON*local_claims(ln_client*c){if(is_denied(c))return NULL;view_lock(c);cJSON*envelope=cJSON_Duplicate(c->view,1);int64_t wall=c->view_wall;double tick=c->view_tick;view_unlock(c);int valid=envelope&&client_context(c,envelope)&&(!wall||time(NULL)>=wall+monotonic()-tick-300)&&ln_verify(envelope,get(c->cfg,"trusted_keys"),text(c->cfg,"product_id"),text(c->cfg,"device_id"),time(NULL));cJSON*claims=valid?claims_from(envelope):NULL;cJSON_Delete(envelope);return claims;}
|
|
301
304
|
int ln_get_error_info(ln_client*c,ln_error_info*out){if(!c||!out)return 0;view_lock(c);*out=c->view_error;view_unlock(c);return 1;}
|
|
302
|
-
char*ln_status(ln_client*c){if(!c)return NULL;cJSON*claims=local_claims(c),*status=cJSON_CreateObject();view_lock(c);ln_error_info e=c->view_error;int online=c->view_online;view_unlock(c);const char*state=claims?(online?"active":"offline_valid"):"verification_required";if(!strcmp(e.code,"LICENSE_EXPIRED"))state="expired";if(!strcmp(e.code,"LICENSE_REVOKED")||!strcmp(e.code,"LICENSE_SUSPENDED"))state="revoked";if(!strcmp(e.code,"DEVICE_RELEASED")||!strcmp(e.code,"DEVICE_CREDENTIAL_REVOKED"))state="released";if(!strcmp(e.code,"FEATURE_QUOTA_EXCEEDED"))state="quota_exhausted";int64_t expires=0;num(claims,"expires_at",&expires);cJSON_AddStringToObject(status,"state",state);cJSON_AddBoolToObject(status,"allowed",claims!=NULL);cJSON_AddStringToObject(status,"code",e.code);cJSON_AddStringToObject(status,"request_id",e.request_id);cJSON_AddBoolToObject(status,"retryable",e.retryable);cJSON_AddNumberToObject(status,"lease_valid_until",(double)expires);char*out=cJSON_PrintUnformatted(status);cJSON_Delete(status);cJSON_Delete(claims);return out;}
|
|
305
|
+
char*ln_status(ln_client*c){if(!c)return NULL;cJSON*claims=local_claims(c),*status=cJSON_CreateObject();view_lock(c);ln_error_info e=c->view_error;int online=c->view_online;view_unlock(c);const char*state=claims?(online?"active":"offline_valid"):"verification_required";if(!strcmp(e.code,"LICENSE_EXPIRED"))state="expired";if(!strcmp(e.code,"LICENSE_REVOKED")||!strcmp(e.code,"LICENSE_SUSPENDED"))state="revoked";if(!strcmp(e.code,"DEVICE_RELEASED")||!strcmp(e.code,"DEVICE_CREDENTIAL_REVOKED"))state="released";if(!strcmp(e.code,"FEATURE_QUOTA_EXCEEDED"))state="quota_exhausted";int64_t expires=0;num(claims,"expires_at",&expires);cJSON_AddStringToObject(status,"state",state);cJSON_AddBoolToObject(status,"allowed",claims!=NULL);cJSON_AddStringToObject(status,"code",e.code);cJSON_AddStringToObject(status,"request_id",e.request_id);cJSON_AddBoolToObject(status,"retryable",e.retryable);cJSON_AddNumberToObject(status,"lease_valid_until",(double)expires);cJSON*info=cJSON_CreateObject();const char*id=text(c->cfg,"device_id");char code[10];snprintf(code,sizeof(code),"%.4s-%.4s",id,id+60);for(char*p=code;*p;p++)*p=(char)toupper((unsigned char)*p);cJSON_AddStringToObject(info,"name",text(c->cfg,"device_name"));cJSON_AddStringToObject(info,"code",code);cJSON_AddStringToObject(info,"platform",text(get(c->cfg,"device_profile"),"platform"));const cJSON*display=get(c->cfg,"device_info");for(const cJSON*field=display?display->child:NULL;field;field=field->next)cJSON_AddItemToObject(info,field->string,cJSON_Duplicate(field,1));cJSON_AddItemToObject(status,"device",info);char*out=cJSON_PrintUnformatted(status);cJSON_Delete(status);cJSON_Delete(claims);return out;}
|
|
303
306
|
char*ln_feature_status(ln_client*c,const char*feature){
|
|
304
307
|
if(!c||!feature)return NULL;
|
|
305
308
|
char*raw=ln_status(c);cJSON*status=raw?ln_json(raw):NULL;ln_free_string(raw);
|
|
@@ -314,6 +317,27 @@ char*ln_feature_status(ln_client*c,const char*feature){
|
|
|
314
317
|
}
|
|
315
318
|
void ln_on_status_changed(ln_client*c,ln_status_callback callback,void*data){if(!c)return;view_lock(c);c->callback=callback;c->callback_data=data;view_unlock(c);}
|
|
316
319
|
static void notify(ln_client*c){view_lock(c);ln_status_callback callback=c->callback;void*data=c->callback_data;view_unlock(c);if(callback){char*status=ln_status(c);if(status){callback(status,data);free(status);}}}
|
|
320
|
+
static void default_device_name(cJSON*cfg){
|
|
321
|
+
const char*given=text(cfg,"device_name");if(given){const unsigned char*p=(const unsigned char*)given;while(*p&&isspace(*p))p++;if(*p)return;}
|
|
322
|
+
char name[1024]={0};
|
|
323
|
+
#ifdef _WIN32
|
|
324
|
+
wchar_t wide[256];DWORD size=256;if(GetComputerNameExW(ComputerNameDnsHostname,wide,&size))WideCharToMultiByte(CP_UTF8,0,wide,-1,name,sizeof(name),NULL,NULL);
|
|
325
|
+
#else
|
|
326
|
+
if(gethostname(name,sizeof(name)-1)!=0)name[0]=0;
|
|
327
|
+
#endif
|
|
328
|
+
char clean[512];size_t used=0,count=0;
|
|
329
|
+
for(size_t i=0;name[i]&&count<120;){
|
|
330
|
+
unsigned char ch=(unsigned char)name[i];size_t n=ch<0x80?1:ch>=0xc2&&ch<=0xdf?2:ch>=0xe0&&ch<=0xef?3:ch>=0xf0&&ch<=0xf4?4:0;
|
|
331
|
+
int valid=n>0;for(size_t k=1;k<n;k++)if(!name[i+k]||((unsigned char)name[i+k]&0xc0)!=0x80)valid=0;
|
|
332
|
+
if(!valid){used=0;break;}
|
|
333
|
+
if((n==3&&((ch==0xe0&&(unsigned char)name[i+1]<0xa0)||(ch==0xed&&(unsigned char)name[i+1]>=0xa0)))||(n==4&&((ch==0xf0&&(unsigned char)name[i+1]<0x90)||(ch==0xf4&&(unsigned char)name[i+1]>=0x90)))){used=0;break;}
|
|
334
|
+
if(!(n==1&&(ch<32||ch==127))&&!(n==2&&ch==0xc2&&(unsigned char)name[i+1]<0xa0)){memcpy(clean+used,name+i,n);used+=n;count++;}i+=n;
|
|
335
|
+
}
|
|
336
|
+
clean[used]=0;
|
|
337
|
+
if(!used){const char*os=text(get(cfg,"device_profile"),"platform");snprintf(clean,sizeof(clean),"%s",!strcmp(os,"windows")?"Windows":!strcmp(os,"darwin")?"macOS":"Linux");}
|
|
338
|
+
cJSON_DeleteItemFromObjectCaseSensitive(cfg,"device_name");cJSON_AddStringToObject(cfg,"device_name",clean);
|
|
339
|
+
}
|
|
340
|
+
#include "device_display.h"
|
|
317
341
|
ln_client*ln_init(const char*config){if(!ensure_curl())return NULL;cJSON*cfg=ln_json(config);if(!cfg)return NULL;if(!text(cfg,"license_key"))cJSON_AddStringToObject(cfg,"license_key","");const char*base=text(cfg,"base_url");if(!base||!base[0]){cJSON_DeleteItemFromObjectCaseSensitive(cfg,"base_url");cJSON_AddStringToObject(cfg,"base_url","https://www.licentivo.com");base=text(cfg,"base_url");}if(base&&strlen(base)>0&&base[strlen(base)-1]=='/'){char*copy=malloc(strlen(base));if(!copy){cJSON_Delete(cfg);return NULL;}memcpy(copy,base,strlen(base)-1);copy[strlen(base)-1]=0;cJSON_ReplaceItemInObjectCaseSensitive(cfg,"base_url",cJSON_CreateString(copy));free(copy);}const char*cache=text(cfg,"cache_path");if(!cache||!cache[0]){char scope[65],context[512],path[2048];snprintf(context,sizeof(context),"%s\n%s",text(cfg,"base_url")?text(cfg,"base_url"):"",text(cfg,"product_id")?text(cfg,"product_id"):"");if(!hash(context,scope)){cJSON_Delete(cfg);return NULL;}
|
|
318
342
|
#ifdef _WIN32
|
|
319
343
|
const char*root=getenv("LOCALAPPDATA");
|
|
@@ -321,7 +345,7 @@ ln_client*ln_init(const char*config){if(!ensure_curl())return NULL;cJSON*cfg=ln_
|
|
|
321
345
|
const char*root=getenv("XDG_CACHE_HOME");
|
|
322
346
|
#endif
|
|
323
347
|
if(!root||!root[0]){root=getenv("HOME");}
|
|
324
|
-
if(!root||!root[0]){cJSON_Delete(cfg);return NULL;}snprintf(path,sizeof(path),"%s/Licentivo/%s/lease.json",root,scope);cJSON_DeleteItemFromObjectCaseSensitive(cfg,"cache_path");cJSON_AddStringToObject(cfg,"cache_path",path);}int64_t timeout=10;if(get(cfg,"timeout_seconds")&&(!num(cfg,"timeout_seconds",&timeout)||timeout<1||timeout>120)){cJSON_Delete(cfg);return NULL;}char dev[65];cJSON*profile=machine_profile(text(cfg,"product_id"),dev);if(!profile){cJSON_Delete(cfg);return NULL;}cJSON_DeleteItemFromObjectCaseSensitive(cfg,"device_profile");cJSON_AddItemToObject(cfg,"device_profile",profile);cJSON_DeleteItemFromObjectCaseSensitive(cfg,"device_id");cJSON_AddStringToObject(cfg,"device_id",dev);const char*secret=text(cfg,"license_key");const cJSON*keys=get(cfg,"trusted_keys");int valid=origin(cfg)&&device(text(cfg,"device_id"))&&uuid(text(cfg,"product_id"))&&secret&&(!secret[0]||(strlen(secret)==50&&(!strncmp(secret,"lv_lic_",7)||!strncmp(secret,"lv_tmp_",7))))&&cJSON_IsObject(keys)&&keys->child;for(const cJSON*k=keys?keys->child:NULL;k;k=k->next){unsigned char raw[32];if(!cJSON_IsString(k)||b64(k->valuestring,raw,32)!=32)valid=0;}if(!valid){cJSON_Delete(cfg);return NULL;}ln_client*c=calloc(1,sizeof(*c));if(!c){cJSON_Delete(cfg);return NULL;}if(!request_uuid(c->session)){cJSON_Delete(cfg);free(c);return NULL;}c->cfg=cfg;c->interval=3600;c->tick=monotonic();
|
|
348
|
+
if(!root||!root[0]){cJSON_Delete(cfg);return NULL;}snprintf(path,sizeof(path),"%s/Licentivo/%s/lease.json",root,scope);cJSON_DeleteItemFromObjectCaseSensitive(cfg,"cache_path");cJSON_AddStringToObject(cfg,"cache_path",path);}int64_t timeout=10;if(get(cfg,"timeout_seconds")&&(!num(cfg,"timeout_seconds",&timeout)||timeout<1||timeout>120)){cJSON_Delete(cfg);return NULL;}char dev[65];cJSON*profile=machine_profile(text(cfg,"product_id"),dev);if(!profile){cJSON_Delete(cfg);return NULL;}cJSON_DeleteItemFromObjectCaseSensitive(cfg,"device_profile");cJSON_AddItemToObject(cfg,"device_profile",profile);cJSON_DeleteItemFromObjectCaseSensitive(cfg,"device_id");cJSON_AddStringToObject(cfg,"device_id",dev);default_device_name(cfg);collect_display_details(cfg);const char*secret=text(cfg,"license_key");const cJSON*keys=get(cfg,"trusted_keys");int valid=origin(cfg)&&device(text(cfg,"device_id"))&&uuid(text(cfg,"product_id"))&&secret&&(!secret[0]||(strlen(secret)==50&&(!strncmp(secret,"lv_lic_",7)||!strncmp(secret,"lv_tmp_",7))))&&cJSON_IsObject(keys)&&keys->child;for(const cJSON*k=keys?keys->child:NULL;k;k=k->next){unsigned char raw[32];if(!cJSON_IsString(k)||b64(k->valuestring,raw,32)!=32)valid=0;}if(!valid){cJSON_Delete(cfg);return NULL;}ln_client*c=calloc(1,sizeof(*c));if(!c){cJSON_Delete(cfg);return NULL;}if(!request_uuid(c->session)){cJSON_Delete(cfg);free(c);return NULL;}c->cfg=cfg;c->interval=3600;c->tick=monotonic();
|
|
325
349
|
#ifdef _WIN32
|
|
326
350
|
InitializeCriticalSection(&c->mutex);InitializeCriticalSection(&c->view_mutex);
|
|
327
351
|
#else
|
|
@@ -395,7 +419,7 @@ static int prepare_credential(ln_client*c){
|
|
|
395
419
|
static int accept_credential(ln_client*c,const cJSON*result){
|
|
396
420
|
if(logout_binding_blocked(c,result))return 0;
|
|
397
421
|
if(cJSON_IsTrue(get(c->logout_state,"signed_out"))){cJSON_ReplaceItemInObjectCaseSensitive(c->logout_state,"signed_out",cJSON_CreateBool(0));if(!save_logout_state(c))return 0;}
|
|
398
|
-
if(get(result,"logout_ticket")){if(!prepare_credential(c))return 0;cJSON_DeleteItemFromObjectCaseSensitive(c->auth,"logout_ticket");cJSON_AddItemToObject(c->auth,"logout_ticket",cJSON_Duplicate(get(result,"logout_ticket"),1));c->auth_dirty=1;if(!save_credential(c))return 0;}if(!temporary(c)||(text(c->auth,"device_key")&&text(c->auth,"device_key")[0]))return 1;
|
|
422
|
+
if(get(result,"logout_ticket")){if(!prepare_credential(c))return 0;cJSON_DeleteItemFromObjectCaseSensitive(c->auth,"logout_ticket");cJSON_AddItemToObject(c->auth,"logout_ticket",cJSON_Duplicate(get(result,"logout_ticket"),1));c->auth_dirty=1;if(!save_credential(c))return 0;}if(!temporary(c)||(text(c->auth,"device_key")&&text(c->auth,"device_key")[0]&&(!text(result,"device_key")||!text(result,"device_key")[0])))return 1;
|
|
399
423
|
const char*k=text(result,"device_key");if(!credential_secret(k,"lv_dev_")){if(strncmp(text(c->cfg,"license_key"),"lv_tmp_",7)&&!(text(c->cfg,"identity_token")&&text(c->cfg,"identity_token")[0]))return 1;strcpy(c->error,"DEVICE_CREDENTIAL_MISSING");return 0;}if(!prepare_credential(c))return 0;cJSON_ReplaceItemInObjectCaseSensitive(c->auth,"device_key",cJSON_CreateString(k));c->auth_dirty=1;return save_credential(c);
|
|
400
424
|
}
|
|
401
425
|
struct buffer{char data[LIMIT+1];size_t size;int oversized;};
|
|
@@ -409,7 +433,7 @@ static void sleepms(int ms){
|
|
|
409
433
|
}
|
|
410
434
|
static size_t receive_header(char*data,size_t size,size_t n,void*arg){size_t length=size*n;ln_client*c=arg;if(length>12&&!strncmp(data,"Retry-After:",12)){char value[32];size_t count=length-12;if(count>31)count=31;memcpy(value,data+12,count);value[count]=0;long seconds=strtol(value,NULL,10);if(seconds>0)c->retry_after=seconds>86400?86400:(int)seconds;}return length;}
|
|
411
435
|
static int authorization_rejected(const char*code){return revocation_error(code)||!strcmp(code,"PRODUCT_DISABLED")||!strcmp(code,"TENANT_SUSPENDED")||!strcmp(code,"LICENSE_INVALID")||!strcmp(code,"DEVICE_CREDENTIAL_MISMATCH")||!strcmp(code,"LAUNCH_ENVIRONMENT_MISMATCH")||!strcmp(code,"SESSION_EXPIRED")||!strcmp(code,"VERSION_NOT_ENTITLED")||!strcmp(code,"VERSION_NOT_PUBLISHED")||!strcmp(code,"MAINTENANCE_EXPIRED")||!strcmp(code,"MAINTENANCE_STATE_INVALID")||!strcmp(code,"FEATURE_NOT_ENTITLED")||!strcmp(code,"HARDWARE_ID_REQUIRED")||!strcmp(code,"VIRTUAL_MACHINE_NOT_ALLOWED")||!strcmp(code,"SDK_UPGRADE_REQUIRED")||!strcmp(code,"LAUNCH_POLICY_UNSUPPORTED")||!strcmp(code,"RUNTIME_WINDOW_INVALID");}
|
|
412
|
-
static cJSON*request_extra(ln_client*c,const char*op,const char*activation,int*transient,const char*operation_id,const cJSON*extra){*transient=0;c->error[0]=0;c->request_id[0]=0;c->retryable=0;c->http_status=0;c->retry_after=0;if(!prepare_credential(c))return NULL;if(!strcmp(op,"activate")&&temporary(c)&&!text(c->auth,"device_key")[0]&&!c->cached)operation_id=text(c->auth,"request_id");unsigned char random[16];if(!random_bytes(random,16)){strcpy(c->error,"RANDOM_SOURCE_FAILED");return NULL;}char idem[120]="Idempotency-Key: ";size_t used=strlen(idem);for(int i=0;i<16;i++)sprintf(idem+used+i*2,"%02x",random[i]);if(operation_id){if(strlen(operation_id)>80||strpbrk(operation_id," \t\r\n")){strcpy(c->error,"INVALID_OPERATION_ID");return NULL;}snprintf(idem,sizeof(idem),"Idempotency-Key: %s",operation_id);}cJSON*body=cJSON_CreateObject();const char*names[]={"product_id","license_key","device_id","device_name","app_version"};for(int i=0;i<5;i++){const char*s=text(c->cfg,names[i]);cJSON_AddStringToObject(body,names[i],s?s:"");}cJSON_ReplaceItemInObjectCaseSensitive(body,"license_key",cJSON_CreateString(runtime_key(c)));cJSON_AddStringToObject(body,"activation_id",activation);cJSON_AddStringToObject(body,"session_id",c->session);cJSON_AddItemToObject(body,"device_profile",cJSON_Duplicate(get(c->cfg,"device_profile"),1));if(extra)for(const cJSON*x=extra->child;x;x=x->next)cJSON_AddItemToObject(body,x->string,cJSON_Duplicate(x,1));if(!get(c->auth,"logout_ticket"))cJSON_AddBoolToObject(body,"request_logout_ticket",1);if(text(c->cfg,"launch_connection_id")){cJSON_AddStringToObject(body,"launch_connection_id",text(c->cfg,"launch_connection_id"));cJSON_AddStringToObject(body,"environment",text(c->cfg,"environment")?text(c->cfg,"environment"):"");}if(!runtime_key(c)[0]&&text(c->cfg,"identity_token")){const char*identity_names[]={"identity_token","launch_connection_id","environment"};for(int i=0;i<3;i++){const char*v=text(c->cfg,identity_names[i]);cJSON_DeleteItemFromObjectCaseSensitive(body,identity_names[i]);cJSON_AddStringToObject(body,identity_names[i],v?v:"");}op="launch/exchange";}char*json=cJSON_PrintUnformatted(body);cJSON_Delete(body);const char*base=text(c->cfg,"base_url");char*url=malloc(strlen(base)+strlen(op)+20);if(!url||!json){free(url);free(json);return NULL;}strcpy(url,base);size_t len=strlen(url);if(len&&url[len-1]=='/')url[len-1]=0;strcat(url,"/api/v1/");strcat(url,op);struct curl_slist*headers=NULL;headers=curl_slist_append(headers,"Content-Type: application/json");headers=curl_slist_append(headers,idem);cJSON*result=NULL;
|
|
436
|
+
static cJSON*request_extra(ln_client*c,const char*op,const char*activation,int*transient,const char*operation_id,const cJSON*extra){*transient=0;c->error[0]=0;c->request_id[0]=0;c->retryable=0;c->http_status=0;c->retry_after=0;if(!prepare_credential(c))return NULL;if(!strcmp(op,"activate")&&temporary(c)&&!text(c->auth,"device_key")[0]&&!c->cached)operation_id=text(c->auth,"request_id");unsigned char random[16];if(!random_bytes(random,16)){strcpy(c->error,"RANDOM_SOURCE_FAILED");return NULL;}char idem[120]="Idempotency-Key: ";size_t used=strlen(idem);for(int i=0;i<16;i++)sprintf(idem+used+i*2,"%02x",random[i]);if(operation_id){if(strlen(operation_id)>80||strpbrk(operation_id," \t\r\n")){strcpy(c->error,"INVALID_OPERATION_ID");return NULL;}snprintf(idem,sizeof(idem),"Idempotency-Key: %s",operation_id);}cJSON*body=cJSON_CreateObject();const char*names[]={"product_id","license_key","device_id","device_name","app_version"};for(int i=0;i<5;i++){const char*s=text(c->cfg,names[i]);cJSON_AddStringToObject(body,names[i],s?s:"");}cJSON_ReplaceItemInObjectCaseSensitive(body,"license_key",cJSON_CreateString(runtime_key(c)));cJSON_AddStringToObject(body,"activation_id",activation);cJSON_AddStringToObject(body,"session_id",c->session);cJSON_AddItemToObject(body,"device_profile",cJSON_Duplicate(get(c->cfg,"device_profile"),1));cJSON_AddItemToObject(body,"device_info",cJSON_Duplicate(get(c->cfg,"device_info"),1));if(extra)for(const cJSON*x=extra->child;x;x=x->next)cJSON_AddItemToObject(body,x->string,cJSON_Duplicate(x,1));if(!get(c->auth,"logout_ticket"))cJSON_AddBoolToObject(body,"request_logout_ticket",1);if(text(c->cfg,"launch_connection_id")){cJSON_AddStringToObject(body,"launch_connection_id",text(c->cfg,"launch_connection_id"));cJSON_AddStringToObject(body,"environment",text(c->cfg,"environment")?text(c->cfg,"environment"):"");}if(!runtime_key(c)[0]&&text(c->cfg,"identity_token")){const char*identity_names[]={"identity_token","launch_connection_id","environment"};for(int i=0;i<3;i++){const char*v=text(c->cfg,identity_names[i]);cJSON_DeleteItemFromObjectCaseSensitive(body,identity_names[i]);cJSON_AddStringToObject(body,identity_names[i],v?v:"");}op="launch/exchange";}char*json=cJSON_PrintUnformatted(body);cJSON_Delete(body);const char*base=text(c->cfg,"base_url");char*url=malloc(strlen(base)+strlen(op)+20);if(!url||!json){free(url);free(json);return NULL;}strcpy(url,base);size_t len=strlen(url);if(len&&url[len-1]=='/')url[len-1]=0;strcat(url,"/api/v1/");strcat(url,op);struct curl_slist*headers=NULL;headers=curl_slist_append(headers,"Content-Type: application/json");headers=curl_slist_append(headers,idem);cJSON*result=NULL;
|
|
413
437
|
for(int attempt=0;attempt<2;attempt++){if(attempt)sleepms(200);struct buffer b={{0},0,0};CURL*curl=curl_easy_init();if(!curl)break;curl_easy_setopt(curl,CURLOPT_URL,url);curl_easy_setopt(curl,CURLOPT_POSTFIELDS,json);curl_easy_setopt(curl,CURLOPT_HTTPHEADER,headers);curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,receive);curl_easy_setopt(curl,CURLOPT_WRITEDATA,&b);int64_t timeout=10;num(c->cfg,"timeout_seconds",&timeout);curl_easy_setopt(curl,CURLOPT_TIMEOUT,(long)timeout);const char*proxy=text(c->cfg,"proxy_url");if(proxy&&proxy[0])curl_easy_setopt(curl,CURLOPT_PROXY,proxy);curl_easy_setopt(curl,CURLOPT_HEADERFUNCTION,receive_header);curl_easy_setopt(curl,CURLOPT_HEADERDATA,c);curl_easy_setopt(curl,CURLOPT_CONNECTTIMEOUT,5L);curl_easy_setopt(curl,CURLOPT_NOSIGNAL,1L);curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,0L);curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,1L);curl_easy_setopt(curl,CURLOPT_SSL_VERIFYHOST,2L);const char*ca=getenv("LICENTIVO_CA_BUNDLE");if(ca&&ca[0])curl_easy_setopt(curl,CURLOPT_CAINFO,ca);CURLcode err=curl_easy_perform(curl);long status=0;curl_easy_getinfo(curl,CURLINFO_RESPONSE_CODE,&status);curl_easy_cleanup(curl);if(b.oversized){strcpy(c->error,"RESPONSE_TOO_LARGE");*transient=0;break;}c->http_status=status;if(err!=CURLE_OK){*transient=1;c->retryable=1;strcpy(c->error,"SERVER_UNAVAILABLE");continue;}*transient=0;if(memchr(b.data,0,b.size)){strcpy(c->error,"INVALID_JSON");break;}cJSON*env=ln_json(b.data);if(!env){strcpy(c->error,"UNEXPECTED_RESPONSE");c->retryable=status==429||status>=500;*transient=c->retryable;if(c->retry_after>0)c->retry_at=(int64_t)time(NULL)+c->retry_after;if(status>=500)continue;break;}const char*trace=text(env,"request_id");snprintf(c->request_id,sizeof(c->request_id),"%s",trace?trace:"");if(status!=200||get(env,"error")){const char*code=text(get(env,"error"),"code");snprintf(c->error,sizeof(c->error),"%s",code?code:"UNEXPECTED_RESPONSE");c->retryable=status==429||status>=500;*transient=c->retryable;if(c->retry_after>0)c->retry_at=(int64_t)time(NULL)+c->retry_after;cJSON_Delete(env);if(status>=500)continue;break;}result=cJSON_Duplicate(get(env,"data"),1);cJSON_Delete(env);break;}
|
|
414
438
|
curl_slist_free_all(headers);free(url);free(json);if(!result){if(revocation_error(c->error))deny_runtime(c);else if(authorization_rejected(c->error))invalidate(c);}return result;
|
|
415
439
|
}
|
|
@@ -471,7 +495,7 @@ char*ln_offline_request(ln_client*c,const char*operation){
|
|
|
471
495
|
char id[37];if(!request_uuid(id)){free(activation);unlock(c);return NULL;}
|
|
472
496
|
cJSON*body=cJSON_CreateObject();const char*names[]={"product_id","license_key","device_id","device_name","app_version"};
|
|
473
497
|
for(int i=0;i<5;i++){const char*v=text(c->cfg,names[i]);cJSON_AddStringToObject(body,names[i],v?v:"");}
|
|
474
|
-
cJSON_ReplaceItemInObjectCaseSensitive(body,"license_key",cJSON_CreateString(runtime_key(c)));cJSON_AddStringToObject(body,"activation_id",activation?activation:"");cJSON_AddStringToObject(body,"session_id",c->session);cJSON_AddItemToObject(body,"device_profile",cJSON_Duplicate(get(c->cfg,"device_profile"),1));cJSON_AddBoolToObject(body,"request_logout_ticket",1);cJSON_AddNumberToObject(body,"version",1);cJSON_AddStringToObject(body,"request_id",id);cJSON_AddNumberToObject(body,"created_at",(double)time(NULL));cJSON_AddStringToObject(body,"operation",operation);
|
|
498
|
+
cJSON_ReplaceItemInObjectCaseSensitive(body,"license_key",cJSON_CreateString(runtime_key(c)));cJSON_AddStringToObject(body,"activation_id",activation?activation:"");cJSON_AddStringToObject(body,"session_id",c->session);cJSON_AddItemToObject(body,"device_profile",cJSON_Duplicate(get(c->cfg,"device_profile"),1));cJSON_AddItemToObject(body,"device_info",cJSON_Duplicate(get(c->cfg,"device_info"),1));cJSON_AddBoolToObject(body,"request_logout_ticket",1);cJSON_AddNumberToObject(body,"version",1);cJSON_AddStringToObject(body,"request_id",id);cJSON_AddNumberToObject(body,"created_at",(double)time(NULL));cJSON_AddStringToObject(body,"operation",operation);
|
|
475
499
|
const char*connection=text(c->cfg,"launch_connection_id"),*identity=text(c->cfg,"identity_token"),*environment=text(c->cfg,"environment"),*device_key=text(c->auth,"device_key");
|
|
476
500
|
if(connection&&connection[0]){cJSON_AddStringToObject(body,"launch_connection_id",connection);cJSON_AddStringToObject(body,"environment",environment?environment:"");}
|
|
477
501
|
if((!device_key||!device_key[0])&&identity&&identity[0])cJSON_AddStringToObject(body,"identity_token",identity);
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: licentivo-sdk
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.11.
|
|
4
|
+
version: 0.11.8
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Licentivo
|
|
@@ -31,6 +31,7 @@ files:
|
|
|
31
31
|
- examples/vectors.rb
|
|
32
32
|
- ext/licentivo_native/extconf.rb
|
|
33
33
|
- lib/licentivo.rb
|
|
34
|
+
- native/device_display.h
|
|
34
35
|
- native/device_logout.h
|
|
35
36
|
- native/feature_tasks.h
|
|
36
37
|
- native/licentivo.c
|