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
|
/*
* Copyright (C) 2010-2011 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou(at)doubango[dot]org>
*
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DOUBANGO is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DOUBANGO.
*
*/
/**@file tcomp_compartment.c
* @brief SigComp compartment.
* An application-specific grouping of messages that relate to a peer endpoint. Depending on the signaling protocol, this grouping may
* relate to application concepts such as "session", "dialog", "connection", or "association". The application allocates state
* memory on a per-compartment basis, and determines when a compartment should be created or closed.
*
* @author Mamadou Diop <diopmamadou(at)yahoo.fr>
*
*/
#include "tcomp_compartment.h"
#include "tsk_debug.h"
#include <assert.h>
static void _tcomp_compartment_freeState(tcomp_compartment_t *compartment, tcomp_state_t **lpState);
tcomp_compartment_t* tcomp_compartment_create(uint64_t id, uint32_t sigCompParameters, tsk_bool_t useOnlyACKedStates)
{
tcomp_compartment_t *compartment;
if((compartment = tsk_object_new(tcomp_compartment_def_t))){
/*
+---+---+---+---+---+---+---+---+
| cpb | dms | sms |
+---+---+---+---+---+---+---+---+
| SigComp_version |
+---+---+---+---+---+---+---+---+
*/
// I always assume that remote params are equal to local params
/* Identifier */
compartment->identifier = id;
/* Remote parameters */
compartment->remote_parameters = tcomp_params_create();
tcomp_params_setParameters(compartment->remote_parameters, sigCompParameters);
/* Local parameters */
compartment->local_parameters = tcomp_params_create();
tcomp_params_setParameters(compartment->local_parameters, sigCompParameters);
/* Total size */
compartment->total_memory_size = compartment->total_memory_left = compartment->local_parameters->smsValue;
/* Empty list. */
compartment->nacks = tsk_list_create();
/* Empty list. */
compartment->local_states = tsk_list_create();
/* Whether to use only ACKed states */
compartment->useOnlyACKedStates = useOnlyACKedStates;
}
else{
TSK_DEBUG_ERROR("Null Compartment");
}
return compartment;
}
int tcomp_compartment_setUseOnlyACKedStates(tcomp_compartment_t* self, tsk_bool_t useOnlyACKedStates)
{
if(!self){
TSK_DEBUG_ERROR("Invalid parameter");
return -1;
}
self->useOnlyACKedStates = useOnlyACKedStates;
return 0;
}
/**Sets remote parameters
*/
void tcomp_compartment_setRemoteParams(tcomp_compartment_t *compartment, tcomp_params_t *lpParams)
{
if(!compartment){
TSK_DEBUG_ERROR("Invalid parameter");
return;
}
/* CPB||DMS||SMS [1-BYTE] */
if(tcomp_params_hasCpbDmsSms(lpParams)){
tcomp_params_setCpbCode(compartment->remote_parameters, lpParams->cpbCode);
tcomp_params_setDmsCode(compartment->remote_parameters, lpParams->dmsCode);
tcomp_params_setSmsCode(compartment->remote_parameters, lpParams->smsCode);
}
/* SigComp version */
if(lpParams->SigComp_version){
compartment->remote_parameters->SigComp_version = lpParams->SigComp_version;
}
/*
* Returned states
* FIXME: check states about quota
* FIXME: not tic tac
* FIXME: what about returned feedback?
*/
if(lpParams->returnedStates && tcomp_buffer_getSize(lpParams->returnedStates)){
TSK_OBJECT_SAFE_FREE(compartment->remote_parameters->returnedStates);
/* swap */
compartment->remote_parameters->returnedStates = lpParams->returnedStates;
lpParams->returnedStates = 0;
}
}
/**Sets requested feedback
*/
void tcomp_compartment_setReqFeedback(tcomp_compartment_t *compartment, tcomp_buffer_handle_t *feedback)
{
if(!compartment){
TSK_DEBUG_ERROR("Invalid parameter.");
return;
}
tsk_safeobj_lock(compartment);
/* Delete old */
TSK_OBJECT_SAFE_FREE(compartment->lpReqFeedback);
compartment->lpReqFeedback = tcomp_buffer_create(tcomp_buffer_getBuffer(feedback), tcomp_buffer_getSize(feedback));
tsk_safeobj_unlock(compartment);
}
/**Sets returned feedback
*/
void tcomp_compartment_setRetFeedback(tcomp_compartment_t *compartment, tcomp_buffer_handle_t *feedback)
{
if(!compartment){
TSK_DEBUG_ERROR("Invalid parameter.");
return;
}
tsk_safeobj_lock(compartment);
// Delete old
TSK_OBJECT_SAFE_FREE(compartment->lpRetFeedback);
compartment->lpRetFeedback = tcomp_buffer_create(tcomp_buffer_getBuffer(feedback), tcomp_buffer_getSize(feedback));
/*
* ACK STATE ==> Returned feedback contains the partial state-id.
*/
if(compartment->compressorData/* && !compartment->compressorData_isStream*/){
tcomp_buffer_handle_t *stateid = tcomp_buffer_create(tcomp_buffer_getBufferAtPos(feedback, 1), tcomp_buffer_getSize(feedback)-1);
compartment->ackGhost(compartment->compressorData, stateid);
TSK_OBJECT_SAFE_FREE(stateid);
}
tsk_safeobj_unlock(compartment);
}
/**Clears are compartment from the history.
*/
void tcomp_compartment_clearStates(tcomp_compartment_t *compartment)
{
if(!compartment){
TSK_DEBUG_ERROR("NULL sigcomp compartment.");
return;
}
tsk_safeobj_lock(compartment);
tsk_list_clear_items(compartment->local_states);
compartment->total_memory_left = compartment->total_memory_size;
tsk_safeobj_unlock(compartment);
}
/**Removes a state from the compartment by priority.
*/
void tcomp_compartment_freeStateByPriority(tcomp_compartment_t *compartment)
{
tcomp_state_t *lpState;
tsk_list_item_t *item;
if(!compartment){
TSK_DEBUG_ERROR("Invalid parameter.");
return;
}
tsk_safeobj_lock(compartment);
lpState = 0;
item = 0;
/*
* The order in which the existing state items are freed is determined by the state_retention_priority, which is set when the
* state items are created. The state_retention_priority of 65535 is reserved for locally available states; these states must always be
* freed first. Apart from this special case, states with the lowest state_retention_priority are always freed first. In the event of
* a tie, then the state item created first in the compartment is also the first to be freed.
*/
tsk_list_foreach(item, compartment->local_states){
tcomp_state_t *curr = item->data;
if(!curr){
continue;
}
/* Local state ? */
if(curr->retention_priority == 65535){
lpState = curr;
break;
}
/* Lower priority? */
if(!lpState || curr->retention_priority < lpState->retention_priority){
lpState = curr;
continue;
}
}
if(lpState){
compartment->total_memory_left += TCOMP_GET_STATE_SIZE(lpState);
tsk_list_remove_item_by_data(compartment->local_states, lpState);
}
tsk_safeobj_unlock(compartment);
}
/**Removes a state from the compartment.
*/
static void _tcomp_compartment_freeState(tcomp_compartment_t *compartment, tcomp_state_t **lpState)
{
if(!compartment){
TSK_DEBUG_ERROR("Invalid parameter.");
return;
}
tsk_safeobj_lock(compartment);
compartment->total_memory_left += TCOMP_GET_STATE_SIZE(*lpState);
tsk_list_remove_item_by_data(compartment->local_states, *lpState);
*lpState = tsk_null;
tsk_safeobj_unlock(compartment);
}
/**Remove states
* Called by UDVM after STATE_FREE instruction
*/
void tcomp_compartment_freeStates(tcomp_compartment_t *compartment, tcomp_tempstate_to_free_t **tempStates, uint8_t size)
{
uint8_t i;
tcomp_state_t *lpState;
tsk_list_item_t *item;
if(!compartment){
TSK_DEBUG_ERROR("Invalid parameter.");
return;
}
if(!tempStates || !size){
return;
}
lpState = tsk_null;
item = tsk_null;
for (i = 0; i < size; i++){
/* lock */
tsk_safeobj_lock(compartment);
tsk_list_foreach(item, compartment->local_states){
tcomp_state_t *curr = item->data;
/* Compare current state with provided partial state */
if(tcomp_buffer_startsWith(curr->identifier, tempStates[i]->identifier)){
/*
* If more than one state item in the compartment matches the partial state identifier,
* then the state free request is ignored.
*/
TSK_DEBUG_INFO("Request to free state with usage_count=%d", curr->usage_count);
if(tcomp_state_dec_usage_count(curr) == 0){
if(lpState){
lpState = tsk_null;
break;
}
else{
lpState = curr;
}
}
}
}
/* unlock */
tsk_safeobj_unlock(compartment);
if(lpState){
_tcomp_compartment_freeState(compartment, &lpState);
}
}
}
/**Adds a state to the compartment.
*/
void tcomp_compartment_addState(tcomp_compartment_t *compartment, tcomp_state_t **lpState)
{
tsk_list_item_t *item;
int32_t usage_count = 0;
const tcomp_buffer_handle_t *identifier;
if(!compartment || !lpState || !*lpState){
TSK_DEBUG_ERROR("Invalid parameter.");
return;
}
tsk_safeobj_lock(compartment);
tcomp_state_makeValid(*lpState);
identifier = (*lpState)->identifier;
// check if the state exist
tsk_list_foreach(item, compartment->local_states){
if(tcomp_buffer_startsWith(((tcomp_state_t*)item->data)->identifier, (*lpState)->identifier)){
*lpState = ((tcomp_state_t*)item->data); // override
usage_count = tcomp_state_inc_usage_count(*lpState);
break;
}
}
if(usage_count == 0){ // alread exist?
compartment->total_memory_left -= TCOMP_GET_STATE_SIZE(*lpState);
usage_count = tcomp_state_inc_usage_count(*lpState);
tsk_list_push_back_data(compartment->local_states, ((void**) lpState));
}
TSK_DEBUG_INFO("SigComp - Add new state with usage_count=%d and id=", usage_count);
tcomp_buffer_print(identifier);
*lpState = tsk_null;
tsk_safeobj_unlock(compartment);
}
/**Finds a state.
*/
uint32_t tcomp_compartment_findState(tcomp_compartment_t *compartment, const tcomp_buffer_handle_t *partial_identifier, tcomp_state_t **lpState)
{
uint32_t count = 0;
tsk_list_item_t *item;
if(!compartment){
TSK_DEBUG_ERROR("Invalid parameter");
return 0;
}
tsk_safeobj_lock(compartment);
tsk_list_foreach(item, compartment->local_states){
tcomp_state_t *curr = item->data;
if(tcomp_buffer_startsWith(curr->identifier, partial_identifier)){
*lpState = curr; // override
count++;
}
}
tsk_safeobj_unlock(compartment);
return count;
}
/**Removes a Ghost state.
*/
void tcomp_compartment_freeGhostState(tcomp_compartment_t *compartment)
{
if(!compartment){
TSK_DEBUG_ERROR("Invalid parameter.");
return;
}
tsk_safeobj_lock(compartment);
if(compartment->compressorData){
compartment->freeGhostState(compartment->compressorData);
}
else{
TSK_DEBUG_WARN("No compression data to free.");
}
tsk_safeobj_unlock(compartment);
}
/**Adds a NACK to the compartment.
*/
void tcomp_compartment_addNack(tcomp_compartment_t *compartment, const uint8_t nackId[TSK_SHA1_DIGEST_SIZE])
{
tcomp_buffer_handle_t *id;
if(!compartment){
TSK_DEBUG_ERROR("Invalid parameter.");
return;
}
#if 0
{
int i;
TSK_DEBUG_INFO("Save NACK with id:");
for(i = 0; i < TSK_SHA1_DIGEST_SIZE; ++i){
printf("%x ", nackId[i]);
}
printf("\n");
}
#endif
tsk_safeobj_lock(compartment);
if(compartment->nacks_history_count >= NACK_MAX_HISTORY_SIZE){
tsk_list_remove_last_item(compartment->nacks);
compartment->nacks_history_count--;
}
id = tcomp_buffer_create(nackId, TSK_SHA1_DIGEST_SIZE);
tsk_list_push_back_data(compartment->nacks, ((void**) &id));
compartment->nacks_history_count++;
tsk_safeobj_unlock(compartment);
}
/**Checks if the NACK exist.
*/
tsk_bool_t tcomp_compartment_hasNack(tcomp_compartment_t *compartment, const tcomp_buffer_handle_t *nackId)
{
tsk_bool_t ret = tsk_false;
tsk_list_item_t *item;
if(!compartment){
TSK_DEBUG_ERROR("Invalid parameter.");
return tsk_false;
}
tsk_safeobj_lock(compartment);
item = tsk_null;
tsk_list_foreach(item, compartment->nacks){
tcomp_buffer_handle_t *curr = item->data;
if(tcomp_buffer_equals(curr, nackId)){
TSK_DEBUG_INFO("SigComp - Nack found.");
ret = tsk_true;
break;
}
}
tsk_safeobj_unlock(compartment);
return ret;
}
//========================================================
// State object definition
//
static tsk_object_t* tcomp_compartment_ctor(tsk_object_t* self, va_list * app)
{
tcomp_compartment_t *compartment = self;
if(compartment){
/* Initialize safeobject */
tsk_safeobj_init(compartment);
}
return self;
}
static tsk_object_t* tcomp_compartment_dtor(tsk_object_t* self)
{
tcomp_compartment_t *compartment = self;
if(compartment){
/* Deinitialize safeobject */
tsk_safeobj_deinit(compartment);
/* Delete feedbacks */
TSK_OBJECT_SAFE_FREE(compartment->lpReqFeedback);
TSK_OBJECT_SAFE_FREE(compartment->lpRetFeedback);
/* Delete Nacks */
TSK_OBJECT_SAFE_FREE(compartment->nacks);
/* Delete Compressor data */
TSK_OBJECT_SAFE_FREE(compartment->compressorData);
compartment->ackGhost = tsk_null;
compartment->freeGhostState = tsk_null;
/* Delete params */
TSK_OBJECT_SAFE_FREE(compartment->local_parameters);
TSK_OBJECT_SAFE_FREE(compartment->remote_parameters);
/* Delete NACKS */
TSK_OBJECT_SAFE_FREE(compartment->nacks);
/* Delete local states */
TSK_OBJECT_SAFE_FREE(compartment->local_states);
}
else{
TSK_DEBUG_ERROR("Null Compartment");
}
return self;
}
static int tcomp_compartment_cmp(const tsk_object_t *obj1, const tsk_object_t *obj2)
{
const tcomp_compartment_t *compartment1 = obj1;
const tcomp_compartment_t *compartment2 = obj2;
int64_t res = (compartment1->identifier - compartment2->identifier);
return res > 0 ? (int)1 : (res < 0 ? (int)-1 : (int)0);
}
static const tsk_object_def_t tsk_compartment_def_s =
{
sizeof(tcomp_compartment_t),
tcomp_compartment_ctor,
tcomp_compartment_dtor,
tcomp_compartment_cmp
};
const tsk_object_def_t *tcomp_compartment_def_t = &tsk_compartment_def_s;
|