licentivo-sdk 0.11.7 → 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 +1 -1
- data/native/feature_tasks.h +90 -9
- data/native/licentivo.c +2 -2
- metadata +1 -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
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
|
@@ -166,7 +166,7 @@ static int advanced_claims(const cJSON*c){
|
|
|
166
166
|
if(get(x,"app_version")&&!app_version(text(x,"app_version")))return 0;
|
|
167
167
|
if(!num(c,"issued_at",&issued)||!num(c,"expires_at",&expires))return 0;
|
|
168
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;
|
|
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
|
|
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;}
|
|
170
170
|
if(released>issued||(maintenance>0&&(!text(x,"app_version")||released==0||released>maintenance)))return 0;
|
|
171
171
|
return 1;
|
|
172
172
|
}
|
|
@@ -419,7 +419,7 @@ static int prepare_credential(ln_client*c){
|
|
|
419
419
|
static int accept_credential(ln_client*c,const cJSON*result){
|
|
420
420
|
if(logout_binding_blocked(c,result))return 0;
|
|
421
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;}
|
|
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]))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;
|
|
423
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);
|
|
424
424
|
}
|
|
425
425
|
struct buffer{char data[LIMIT+1];size_t size;int oversized;};
|