forked from lorabasics/basicstation
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcups.c
More file actions
573 lines (529 loc) · 22.2 KB
/
cups.c
File metadata and controls
573 lines (529 loc) · 22.2 KB
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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/*
* --- Revised 3-Clause BSD License ---
* Copyright Semtech Corporation 2022. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of the Semtech corporation nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL SEMTECH CORPORATION. BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "s2conf.h"
#include "sys.h"
#include "uj.h"
#include "kwcrc.h"
#include "cups.h"
#include "tc.h"
#include "mbedtls/version.h"
#include "mbedtls/ecdsa.h"
#include "mbedtls/error.h"
#include "mbedtls/sha512.h"
#include "mbedtls/bignum.h"
#include "tls.h"
#define FAIL_CNT_THRES 6
#define SIGCRC_LEN 4
static tmr_t cups_sync_tmr;
static cups_t* CUPS;
static int cups_credset = SYS_CRED_REG;
static int cups_failCnt;
static s1_t cstateLast;
struct cups_sig {
mbedtls_sha512_context sha;
u1_t signature[128];
u1_t hash[64];
u1_t len;
union {
u4_t keycrc;
u1_t keycrcb[4];
};
};
static int cups_verifySig (cups_sig_t* sig) {
int verified = 0;
dbuf_t key;
int keyid = -1;
// Ensure PSA crypto is initialized for mbedtls 3.x ECDSA operations
tls_ensurePsaInit();
while ( (key = sys_sigKey(++keyid)).buf != NULL && !verified ) {
if ( key.bufsize != 64 )
continue;
#if MBEDTLS_VERSION_NUMBER >= 0x03000000
// mbedtls 3.x: ecp_keypair members are now private
mbedtls_ecp_group grp;
mbedtls_ecp_point Q;
mbedtls_ecdsa_context ecdsa;
mbedtls_ecp_group_init(&grp);
mbedtls_ecp_point_init(&Q);
mbedtls_ecdsa_init(&ecdsa);
// Build uncompressed point format: 04 || X || Y
u1_t pubkey[65];
pubkey[0] = 0x04;
memcpy(pubkey + 1, key.buf, 64);
int ret;
if ((ret = mbedtls_ecp_group_load (&grp, MBEDTLS_ECP_DP_SECP256R1) ) ||
(ret = mbedtls_ecp_point_read_binary (&grp, &Q, pubkey, sizeof(pubkey)) ) ||
(ret = mbedtls_ecp_set_public_key (MBEDTLS_ECP_DP_SECP256R1, &ecdsa, &Q) ) ||
(ret = mbedtls_ecdsa_read_signature (&ecdsa, sig->hash, sizeof(sig->hash), sig->signature, sig->len))) {
verified = 0;
} else {
verified = 1;
}
mbedtls_ecp_group_free(&grp);
mbedtls_ecp_point_free(&Q);
mbedtls_ecdsa_free(&ecdsa);
#else
// mbedtls 2.x: direct access to ecp_keypair members
mbedtls_ecp_keypair k;
mbedtls_ecp_keypair_init(&k);
mbedtls_ecdsa_context ecdsa;
mbedtls_ecdsa_init(&ecdsa);
int ret;
if ((ret = mbedtls_ecp_group_load (&k.grp, MBEDTLS_ECP_DP_SECP256R1) ) ||
(ret = mbedtls_mpi_read_binary (&k.Q.X, (u1_t*)key.buf, 32) ) ||
(ret = mbedtls_mpi_read_binary (&k.Q.Y, (u1_t*)key.buf+32, 32) ) ||
(ret = mbedtls_mpi_lset (&k.Q.Z, 1) ) ||
(ret = mbedtls_ecp_check_pubkey (&k.grp, &k.Q) ) ||
(ret = mbedtls_ecdsa_from_keypair (&ecdsa, &k) ) ||
(ret = mbedtls_ecdsa_read_signature (&ecdsa, sig->hash, sizeof(sig->hash), sig->signature, sig->len ))
) {
verified = 0;
} else {
verified = 1;
}
mbedtls_ecp_keypair_free(&k);
mbedtls_ecdsa_free(&ecdsa);
#endif
LOG(MOD_CUP|INFO, "ECDSA key#%d -> %s", keyid, verified? "VERIFIED" : "NOT verified");
}
sys_sigKey(-1); // Release memory
if (!verified) {
LOG(MOD_CUP|WARNING, "No key could verify signature. Tried %d keys", keyid);
}
return verified;
}
static void cups_ondone (tmr_t* tmr) {
if( CUPS == NULL ) {
sys_triggerCUPS(0);
return;
}
str_t msg="", detail="";
ustime_t ahead = CUPS_RESYNC_INTV;
u1_t log = 1;
if( CUPS->cstate != CUPS_DONE ) {
// Someting went wrong - retry again more quickly
msg = "Interaction with CUPS failed%s - retrying in %~T";
if( cups_failCnt > FAIL_CNT_THRES ||
CUPS->cstate == CUPS_ERR_REJECTED ||
CUPS->cstate == CUPS_ERR_NOURI ) {
// Rotate and try REG/BAK/BOOT sets of config files
cups_credset = (cups_credset+1) % (SYS_CRED_BOOT+1);
}
cups_failCnt += 1;
if (CUPS->cstate == CUPS_ERR_NOURI)
log = 0; // already logged
} else {
// Successful interaction with CUPS
u1_t uflags = CUPS->uflags;
if( uflags & UPDATE_FLAG(UPDATE) ) {
LOG(MOD_CUP|INFO, "CUPS provided update.bin");
u1_t run_update = 0;
if( (uflags & UPDATE_FLAG(SIGNATURE)) ) {
LOG(MOD_CUP|INFO, "CUPS provided signature len=%d keycrc=%08X", CUPS->sig->len, CUPS->sig->keycrc);
assert( CUPS->sig );
mbedtls_sha512_finish( &CUPS->sig->sha, CUPS->sig->hash );
mbedtls_sha512_free ( &CUPS->sig->sha );
run_update = cups_verifySig(CUPS->sig);
} else {
dbuf_t key = sys_sigKey(0);
if ( key.buf == NULL ) {
LOG(MOD_CUP|INFO, "No Key. No Sig. UPDATE.");
run_update = 1;
} else {
LOG(MOD_CUP|ERROR, "Keyfile present, but no signature provided. Aborting update.");
sys_sigKey(-1);
}
}
if (run_update) {
LOG(MOD_CUP|INFO, "Running update.bin as background process");
sys_runUpdate();
} else {
LOG(MOD_CUP|INFO, "Aborting update.");
sys_abortUpdate();
}
}
if( uflags & (UPDATE_FLAG(TC_URI)|UPDATE_FLAG(TC_CRED)) ) {
// Any change here - restart TC connection
str_t s = ((uflags & UPDATE_FLAG(TC_URI )) == UPDATE_FLAG(TC_URI ) ? "uri" :
(uflags & UPDATE_FLAG(TC_CRED)) == UPDATE_FLAG(TC_CRED) ? "credentials" :
"uri/credentials");
LOG(MOD_CUP|INFO, "CUPS provided TC updates (%s) %s", s, sys_noTC ? "" : "- restarting TC engine");
sys_stopTC();
}
if( uflags & (UPDATE_FLAG(CUPS_URI)|UPDATE_FLAG(CUPS_CRED)) ) {
// Any change here - contact new CUPS immediately
detail = ((uflags & UPDATE_FLAG(CUPS_URI )) == UPDATE_FLAG(CUPS_URI ) ? "uri" :
(uflags & UPDATE_FLAG(CUPS_CRED)) == UPDATE_FLAG(CUPS_CRED) ? "credentials" :
"uri/credentials");
msg = "CUPS provided CUPS updates (%s) - reconnecting in %~T";
// Reconnect right away - using new settings
} else {
detail = uflags ? "" : " (no updates)";
msg = "Interaction with CUPS done%s - next regular check in %~T";
ahead = CUPS_OKSYNC_INTV;
}
cups_credset = SYS_CRED_REG;
cups_failCnt = 0;
}
if( TC && sys_statusTC() == TC_MUXS_CONNECTED )
ahead = CUPS_OKSYNC_INTV;
cups_free(CUPS);
CUPS = NULL;
if (log)
LOG(MOD_CUP|INFO, msg, detail, ahead);
sys_startTC();
rt_setTimer(&cups_sync_tmr, rt_micros_ahead(ahead));
}
static void cups_done (cups_t* cups, s1_t cstate) {
cups->cstate = cstate;
http_free(&cups->hc);
rt_yieldTo(&cups->timeout, cups_ondone);
sys_inState(SYSIS_CUPS_DONE);
}
static void cups_timeout (tmr_t* tmr) {
cups_t* cups = timeout2cups(tmr);
LOG(MOD_CUP|ERROR, "CUPS timed out");
cups_done(cups, CUPS_ERR_TIMEOUT);
}
static int sizelen (int cstate) {
assert(cstate >= CUPS_FEED_CUPS_URI && cstate <= CUPS_FEED_UPDATE);
return 1<<((cstate - CUPS_FEED_CUPS_URI)>>1); // 1,2,4 for URI,CRED,SIG/UPDATE
}
static void cups_update_info (conn_t* _conn, int ev) {
cups_t* cups = conn2cups(_conn);
if( ev == HTTPEV_CONNECTED ) {
dbuf_t cupsuri;
dbuf_str(cupsuri, sys_uri(SYS_CRED_CUPS, cups_credset));
LOG(MOD_CUP|VERBOSE, "Retrieving update-info from CUPS%s %s...", sys_credset2str(cups_credset), cupsuri.buf);
struct uri_info cui;
uri_parse(&cupsuri, &cui, 0); // Does not write
dbuf_t b = http_getReqbuf(&cups->hc);
xprintf(&b,
"POST /update-info HTTP/1.1\r\n"
"Host: %*s\r\n"
"Content-Type: application/json\r\n"
"Content-Length: 00000\r\n"
"%s\r\n",
cui.hostportEnd-cui.hostportBeg, cupsuri.buf+cui.hostportBeg,
cups->hc.c.authtoken ? cups->hc.c.authtoken : "");
doff_t bodybeg = b.pos;
xputs(&b, "{", -1); // note: uj_encOpen would create a comma since b.pos>0
str_t version = sys_version();
uj_encKVn(&b,
"router", '6', sys_eui(),
"cupsUri", 's', sys_uri(SYS_CRED_CUPS, SYS_CRED_REG),
"tcUri", 's', sys_uri(SYS_CRED_TC, SYS_CRED_REG),
"cupsCredCrc",'u', sys_crcCred(SYS_CRED_CUPS, SYS_CRED_REG),
"tcCredCrc", 'u', sys_crcCred(SYS_CRED_TC, SYS_CRED_REG),
"station", 's', CFG_version " " CFG_bdate,
"model", 's', CFG_platform,
"package", 's', version,
// "os", 's', sys_osversion(),
NULL);
uj_encKey (&b, "keys");
uj_encOpen (&b, '[');
int keyid = -1;
u4_t crc = 0;
while( (crc = sys_crcSigkey(++keyid)) > 0 ) {
uj_encUint(&b, (uL_t)crc);
}
uj_encClose(&b, ']');
uj_encClose(&b, '}');
http_setContentLength(b.buf, b.pos-bodybeg);
LOG(MOD_CUP|DEBUG, "CUPS Request: %.*s", b.pos-bodybeg, b.buf+bodybeg);
http_request(&cups->hc, &b);
return;
}
if( ev == HTTPEV_RESPONSE ) {
dbuf_t body = http_getBody(&cups->hc);
s1_t cstate = cups->cstate;
if( cups->cstate == CUPS_HTTP_REQ_PEND ) {
// Hdr is only present in very first chunk
dbuf_t hdr = http_getHdr(&cups->hc);
int status = http_getStatus(&cups->hc);
if( status != 200 ) {
dbuf_t msg = http_statusText(&hdr);
LOG(MOD_CUP|VERBOSE, "Failed to retrieve TCURI from CUPS: (%d) %.*s", status, msg.bufsize, msg.buf);
cups->cstate = CUPS_ERR_REJECTED;
http_close(&cups->hc);
return;
}
if( cups_credset == SYS_CRED_REG )
sys_backupConfig(SYS_CRED_CUPS);
// We expect both CUPS/TC URI segments to fit into HTTP buffer
u1_t cupsuri_len = body.buf[0];
u1_t tcuri_len = body.buf[1+cupsuri_len];
body.pos = 2+cupsuri_len+tcuri_len; // after both URI segments
if( body.bufsize < 2 || 1+body.pos > body.bufsize ) { // need one more byte for \0
LOG(MOD_CUP|ERROR, "Malformed CUPS response: URI segments lengths (%u) exceed available data (%u)", body.pos, body.bufsize);
goto proto_err;
}
sys_resetConfigUpdate();
if( cupsuri_len ) {
str_t uri = (str_t)body.buf+1;
body.buf[1+cupsuri_len] = 0;
sys_saveUri(SYS_CRED_CUPS, uri);
LOG(MOD_CUP|INFO, "[Segment] CUPS URI: %s", uri);
cups->uflags |= UPDATE_FLAG(CUPS_URI);
}
if( tcuri_len ) {
char* uri = (char*)body.buf+2+cupsuri_len;
char save = uri[tcuri_len];
uri[tcuri_len] = 0;
sys_saveUri(SYS_CRED_TC, uri);
LOG(MOD_CUP|INFO, "[Segment] TC URI: %s", uri);
uri[tcuri_len] = save;
cups->uflags |= UPDATE_FLAG(TC_URI);
}
cups->cstate = cstate = CUPS_FEED_CUPS_CRED;
cups->temp_n = 0;
}
assert(cstate > CUPS_HTTP_REQ_PEND || cstate < CUPS_DONE);
// Rewind timeout every time we get some data
rt_setTimer(&cups->timeout, rt_micros_ahead(CUPS_CONN_TIMEOUT));
int segm_len = cups->segm_len;
while( cups->temp_n < 4 ) {
// Assemble length of a section in temp
if( body.pos >= body.bufsize ) {
// Not enough data in body buffer
get_more:
if( !http_getMore(&cups->hc) ) {
LOG(MOD_CUP|ERROR, "Unexpected end of data");
proto_err:
LOG(MOD_CUP|ERROR, "CUPS Protocol error. Closing connection.");
cups_done(cups, CUPS_ERR_FAILED);
}
return; // waiting for next chunk
}
cups->temp[cups->temp_n++] = body.buf[body.pos++];
if( cups->temp_n == sizelen(cstate) ) {
if( (segm_len = rt_rlsbf4(cups->temp)) == 0 ) {
next_cstate:
if( (cups->cstate = cstate += 1) == CUPS_DONE ) {
sys_commitConfigUpdate();
http_close(&cups->hc);
return;
}
cups->temp_n = 0;
continue;
}
if( segm_len < 0 ) {
LOG(MOD_CUP|ERROR, "Segment %d length not allowed (must be <2GB): 0x%08x bytes", cstate-CUPS_FEED_CUPS_URI, segm_len);
goto proto_err;
}
cups->segm_off = 0;
cups->segm_len = segm_len;
cups->temp_n = 4;
memset(&cups->temp, 0, sizeof(cups->temp));
if( cstate == CUPS_FEED_CUPS_CRED ) {
sys_credStart(SYS_CRED_CUPS, segm_len);
cups->uflags |= UPDATE_FLAG(CUPS_CRED);
LOG(MOD_CUP|INFO, "[Segment] CUPS Credentials (%d bytes)", segm_len);
}
else if( cstate == CUPS_FEED_TC_CRED ) {
sys_credStart(SYS_CRED_TC, segm_len);
cups->uflags |= UPDATE_FLAG(TC_CRED);
LOG(MOD_CUP|INFO, "[Segment] TC Credentials (%d bytes)", segm_len);
} else if( cstate == CUPS_FEED_SIGNATURE ) {
LOG(MOD_CUP|INFO, "[Segment] FW Signature (%d bytes)", segm_len);
rt_free(cups->sig);
if( segm_len < 8 || segm_len > sizeof(cups->sig->signature) + SIGCRC_LEN ) {
LOG(MOD_CUP|ERROR, "Illegal signature segment length (must be 8-%d bytes): %d", sizeof(cups->sig->signature) + SIGCRC_LEN, segm_len);
goto proto_err;
}
cups->sig = rt_malloc(cups_sig_t);
} else { // cstate == CUPS_FEED_UPDATE
assert(cstate == CUPS_FEED_UPDATE);
sys_commitConfigUpdate();
sys_updateStart(segm_len);
LOG(MOD_CUP|INFO, "[Segment] FW Update (%d bytes)", segm_len);
}
}
}
check_segm:
if( cups->segm_off >= segm_len ) {
// Segment finished
if( cstate == CUPS_FEED_CUPS_CRED ) {
sys_credComplete(SYS_CRED_CUPS, cups->segm_len);
LOG(MOD_CUP|INFO, "[Segment] CUPS Credentials update completed (%d bytes)", cups->segm_len);
}
else if( cstate == CUPS_FEED_TC_CRED ) {
sys_credComplete(SYS_CRED_TC, cups->segm_len);
LOG(MOD_CUP|INFO, "[Segment] TC Credentials update completed (%d bytes)", cups->segm_len);
} else if( cstate == CUPS_FEED_SIGNATURE ) {
cups->uflags |= UPDATE_FLAG(SIGNATURE);
cups->sig->len = cups->segm_len - SIGCRC_LEN;
mbedtls_sha512_init(&cups->sig->sha);
mbedtls_sha512_starts(&cups->sig->sha, 0);
}
else { // cstate == CUPS_FEED_UPDATE
if( sys_updateCommit(cups->segm_len) ) {
cups->uflags |= UPDATE_FLAG(UPDATE);
LOG(MOD_CUP|INFO, "[Segment] Update committed (%d bytes)", cups->segm_len);
} else {
LOG(MOD_CUP|ERROR, "[Segment] Update received (%d bytes) but failed to write (ignored)", cups->segm_len);
}
}
goto next_cstate;
}
if( body.pos >= body.bufsize )
goto get_more;
int segm_off = cups->segm_off;
int dlen = min(segm_len - segm_off, body.bufsize - body.pos);
u1_t* data = (u1_t*)&body.buf[body.pos];
if( cstate == CUPS_FEED_CUPS_CRED ) {
sys_credWrite(SYS_CRED_CUPS, data, segm_off, dlen);
}
else if( cstate == CUPS_FEED_TC_CRED ) {
sys_credWrite(SYS_CRED_TC, data, segm_off, dlen);
}
else if( cstate == CUPS_FEED_SIGNATURE ) {
// CRC of signature comes just after length
int siglen = dlen;
if( segm_off < SIGCRC_LEN ) { // CRC before signature
int d = min(SIGCRC_LEN-segm_off, siglen);
memcpy(cups->sig->keycrcb + segm_off, data, d);
segm_off += d;
data += d;
siglen -= d;
}
if (siglen > 0 && segm_off + siglen - SIGCRC_LEN <= sizeof(cups->sig->signature)) {
memcpy(cups->sig->signature+segm_off-SIGCRC_LEN, data, siglen);
}
} else {
assert(cstate == CUPS_FEED_UPDATE);
if( cups->sig != NULL ) {
mbedtls_sha512_update(&cups->sig->sha, data, dlen);
}
sys_updateWrite(data, segm_off, dlen);
}
body.pos += dlen;
cups->segm_off += dlen;
goto check_segm;
}
if( ev == HTTPEV_CLOSED ) {
if( cups->cstate >= CUPS_INI && cups->cstate < CUPS_DONE )
cups->cstate = CUPS_ERR_CLOSED; // unexpected close
cups_done(cups, cups->cstate);
return;
}
LOG(MOD_CUP|INFO, "cups_update_info - Unknown event: %d", ev);
}
cups_t* cups_ini () {
assert(CUPS_BUFSZ > MAX_HOSTNAME_LEN + MAX_PORT_LEN + 2);
cups_t* cups = rt_malloc(cups_t);
http_ini(&cups->hc, CUPS_BUFSZ);
rt_iniTimer(&cups->timeout, cups_timeout);
cups->cstate = CUPS_INI;
return cups;
}
void cups_free (cups_t* cups) {
if( cups == NULL )
return;
http_free(&cups->hc);
rt_clrTimer(&cups->timeout);
cstateLast = cups->cstate;
cups->cstate = CUPS_ERR_DEAD;
if (cups->sig) {
mbedtls_sha512_free(&cups->sig->sha);
rt_free(cups->sig);
}
rt_free(cups);
}
void cups_start (cups_t* cups) {
assert(cups->cstate == CUPS_INI);
str_t cupsuri = sys_uri(SYS_CRED_CUPS, cups_credset);
if( cupsuri == NULL ) {
LOG(MOD_CUP|ERROR, "No CUPS%s URI configured", sys_credset2str(cups_credset));
cups_done(cups, CUPS_ERR_NOURI);
return;
}
LOG(MOD_CUP|INFO, "Connecting to CUPS%s ... %s (try #%d)",
sys_credset2str(cups_credset), cupsuri, cups_failCnt+1);
log_flushIO();
// Use HTTP buffer as temp place for host/port strings
// Gets destroyed when reading response.
char* hostname = (char*)cups->hc.c.rbuf;
char* port = (char*)cups->hc.c.rbuf + MAX_HOSTNAME_LEN + 1;
int ok;
if( (ok = uri_checkHostPortUri(cupsuri, "http", hostname, MAX_HOSTNAME_LEN, port, MAX_PORT_LEN)) == URI_BAD ) {
LOG(MOD_CUP|ERROR,"Bad CUPS URI: %s", cupsuri);
goto errexit;
}
if( ok == URI_TLS && !conn_setup_tls(&cups->hc.c, SYS_CRED_CUPS, cups_credset, hostname) ) {
goto errexit;
}
if( !http_connect(&cups->hc, hostname, port) ) {
LOG(MOD_CUP|ERROR, "CUPS connect failed - URI: %s", cupsuri);
goto errexit;
}
rt_setTimerCb(&cups->timeout, rt_micros_ahead(CUPS_CONN_TIMEOUT), cups_timeout);
cups->hc.c.evcb = (evcb_t)cups_update_info;
cups->cstate = CUPS_HTTP_REQ_PEND;
return;
errexit:
cups_done(cups, CUPS_ERR_FAILED);
return;
}
static void delayedCUPSstart(tmr_t* tmr) {
LOG(MOD_CUP|INFO, "Starting a CUPS session now.");
cups_start(CUPS);
}
void sys_triggerCUPS (int delay) {
if( CUPS != NULL || sys_noCUPS )
return; // interaction pending
#if defined(CFG_cups_exclusive)
if( !sys_noTC ) {
LOG(MOD_CUP|INFO, "Stopping TC in favor of CUPS");
sys_stopTC();
}
#endif // defined(CFG_cups_exclusive)
if( delay < 0 ) {
delay = CUPS_RESYNC_INTV/1000000;
}
LOG(MOD_CUP|INFO, "Starting a CUPS session in %d seconds.", delay);
sys_inState(SYSIS_CUPS_INTERACT);
CUPS = cups_ini();
rt_clrTimer(&cups_sync_tmr);
rt_setTimerCb(&CUPS->timeout, rt_seconds_ahead(delay), delayedCUPSstart);
}
void sys_iniCUPS () {
rt_iniTimer(&cups_sync_tmr, cups_ondone);
}
void sys_clearCUPS () {
rt_clrTimer(&cups_sync_tmr);
}
void sys_delayCUPS () {
if( sys_statusCUPS() < 0 ) {
LOG(MOD_CUP|INFO, "Next CUPS interaction delayed by %~T.", CUPS_OKSYNC_INTV);
rt_setTimer(&cups_sync_tmr, rt_micros_ahead(CUPS_OKSYNC_INTV));
}
}
s1_t sys_statusCUPS () {
return CUPS ? CUPS->cstate : cstateLast;
}