File: cteb.c

package info (click to toggle)
ion 3.2.1%2Bdfsg-1.1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 23,768 kB
  • ctags: 11,049
  • sloc: ansic: 141,798; sh: 22,848; makefile: 7,818; python: 1,638; sql: 311; perl: 197; awk: 178; xml: 50; java: 19
file content (377 lines) | stat: -rw-r--r-- 9,122 bytes parent folder | download | duplicates (2)
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
/*
 *	cteb.c:		implementation of the Custody Transfer
 *			Enhancement Block (CTEB).
 *
 *  Copyright (c) 2010-2011, Regents of the University of Colorado.
 *  This work was supported by NASA contracts NNJ05HE10G, NNC06CB40C, and
 *  NNC07CB47C.
 *
 *	Author: Andrew Jenkins, University of Colorado
 */

#include "bpP.h"
#include "acsP.h"
#include "cteb.h"


int cteb_offer(ExtensionBlock *blk, Bundle *bundle)
{
	/* If the bundle doesn't request custody transfer, there is no benefit
	 * in attaching a CTEB. */
	if ((bundle->bundleProcFlags & BDL_IS_CUSTODIAL) == 0)
	{
		return 0;
	}

	/* If the ACS system is not initialized, we will be unable to get a
	 * custody ID in cteb_processOnDequeue() so we should not reserve space
	 * for a CTEB. */
	if (getAcssdr() == NULL)
	{
		return 0;
	}

	blk->blkProcFlags = BLK_MUST_BE_COPIED;
	blk->dataLength = 0;	/*	Will know length at dequeue.	*/
	blk->length = 3;	/*	Just to keep block alive.	*/
	blk->size = 0;
	blk->object = 0;
	return 0;
}

void cteb_release(ExtensionBlock *sdrBlk)
{
	Sdr bpSdr = getIonsdr();

	assert(sdr_in_xn(bpSdr) != 0);

	if(sdrBlk->object != 0)
	{
		sdr_free(bpSdr, sdrBlk->object);
		sdrBlk->object = 0;
		sdrBlk->size = 0;
	}
	
	return;
}

/* Store the CTEB to SDR so it can be used when accepting custody.
 * Returns -1 on failure, 0 on success, per Extensions API. */
int cteb_record(ExtensionBlock *sdrBlk, AcqExtBlock *ramBlk)
{
	Sdr bpSdr = getIonsdr();

	assert(sdr_in_xn(bpSdr) != 0);

	sdrBlk->object = sdr_insert(bpSdr, ramBlk->object, ramBlk->size);
	if(sdrBlk->object == 0)
	{
		putErrmsg("Can't store CTEB scratchpad in SDR",
				itoa(ramBlk->size));
		return -1;
	}
	sdrBlk->size = ramBlk->size;
	return 0;
}


/* Copies the CTEB scratchpad from oldBlk to newBlk, in case the bundle
 * containing oldBlk is duplicated during processing.  Returns 0 on success,
 * -1 on system error per the Extensions API. */
int cteb_copy(ExtensionBlock *newBlk, ExtensionBlock *oldBlk)
{
	Sdr bpSdr = getIonsdr();
	CtebScratchpad cteb;

	assert(sdr_in_xn(bpSdr) != 0);

	if(oldBlk->object == 0) return 0;

	sdr_peek(bpSdr, cteb, oldBlk->object);
	newBlk->object = sdr_stow(bpSdr, cteb);
	if(newBlk->object == 0) {
		putErrmsg("Can't copy CTEB", itoa(sizeof(CtebScratchpad)));
		return -1;
	}
	newBlk->size = sizeof(CtebScratchpad);
	
	return 0;
}

/* FIXME: This can probably be processOnEnqueue. Is that more efficient? */
int cteb_processOnDequeue(ExtensionBlock *blk, Bundle *bundle, void *ctxt)
{
	char	*ctebBytes;
	char	*dictionary;
	Sdnv	custodyIdSdnv;
	char	*custodianEid;
	int		custodianEidLen;
	char	*sourceEid;
	int		i = 0;
	int		result;
	AcsCustodyId cid;

	assert((bundle->bundleProcFlags & BDL_IS_CUSTODIAL) != 0);

	if(bundle->custodyTaken == 0)
	{
		/* Bundle is custodial, but we're not the custodian.
		 * We should copy the old CTEB. */
		putErrmsg("FIXME: Bundle is custodial but we're not custodian",
				NULL);
		return 0;
	}

	/* Construct our own CTEB for attaching. */

	/* Get a string representing the current custodian EID. */
	dictionary = retrieveDictionary(bundle);
	if ((void *)(dictionary) == (void *)(bundle))	/* Indicates error. */
	{
		putErrmsg("Can't get dictionary from bundle.", NULL);
        return -1;
	}
	if (printEid(&bundle->custodian, dictionary, &custodianEid) < 0)
	{
		putErrmsg("Can't print custodian EID.", NULL);
		if (dictionary)
		{
			MRELEASE(dictionary);
		}
		return -1;
	}
	if (printEid(&bundle->id.source, dictionary, &sourceEid) < 0)
	{
		putErrmsg("Can't print source EID.", NULL);
		MRELEASE(custodianEid);
		if (dictionary)
		{
			MRELEASE(dictionary);
		}
		return -1;
	}
	if (dictionary)
	{
		MRELEASE(dictionary);
	}
	if (custodianEid == NULL || sourceEid == NULL)
	{
		putErrmsg("Can't get custodian or source EID for CTEB.", NULL);
		return -1;
	}
	custodianEidLen = strlen(custodianEid);

	/* Get a custody identifier for this bundle. */
	if(get_or_make_custody_id(sourceEid, &bundle->id.creationTime,
			bundle->id.fragmentOffset, bundle->totalAduLength == 0 ?
			0 : bundle->payload.length, &cid) < 0)
	{
		ACSLOG_INFO("Can't get a custody ID for the CTEB, is ACS initialized?");

        /* Destroy this extension block */
        	suppressExtensionBlock(blk);
		MRELEASE(custodianEid);
		MRELEASE(sourceEid);
		return 0;
	}

	/* Encode the SDNVs that will be stuffed in the CTEB. */
	encodeSdnv(&custodyIdSdnv, cid.id);

	/* Allocate memory for our own CTEB. */
	blk->dataLength = custodyIdSdnv.length + custodianEidLen;
	ctebBytes = MTAKE(blk->dataLength);
	if (ctebBytes == NULL)
	{
		putErrmsg("Can't construct CTEB text.", itoa(blk->dataLength));
		MRELEASE(custodianEid);
		MRELEASE(sourceEid);
		return -1;
	}

	/* Serialize the CTEB. */
	memcpy(ctebBytes + i, custodyIdSdnv.text, custodyIdSdnv.length);
	i += custodyIdSdnv.length;
	memcpy(ctebBytes + i, custodianEid, custodianEidLen);
	i += custodianEidLen;
	assert(i == blk->dataLength);
	result = serializeExtBlk(blk, NULL, ctebBytes);
	MRELEASE(ctebBytes);
	MRELEASE(custodianEid);
	MRELEASE(sourceEid);
	return result;
}

/* Acquires a CTEB into a CtebScratchpad object at blk->object.
 * 
 * Returns -1 on system error, 0 if the block isn't valid, 1 if a valid
 * CTEB is acquired per Extensions API.
 */
int cteb_acquire(AcqExtBlock *blk, AcqWorkArea *wk)
{
	CtebScratchpad 	cteb;
	Bundle			*bundle = &wk->bundle;
	unsigned char	*cursor;
	char			*custodianEid;
	char			*bundleCustodianEid;
	int				bytesRemaining = blk->dataLength;

	if (bytesRemaining < 3)
	{
		return 0;		/* Malformed. */
	}

	/* Parse into a scratchpad on the stack. */
	cursor = blk->bytes + (blk->length - blk->dataLength);
	extractSmallSdnv(&cteb.id, &cursor, &bytesRemaining);

	/* FIXME: Handle fragments by continuing to acquire here. */

	custodianEid = MTAKE(bytesRemaining + 1);
	if(custodianEid == NULL)
	{
		putErrmsg("Can't acquire CTEB custodian EID.",
				itoa(bytesRemaining));
		return -1;		/* Error acquiring. */
	}
	memcpy(custodianEid, cursor, bytesRemaining);
	custodianEid[bytesRemaining] = '\0';
	cursor += bytesRemaining;
	bytesRemaining = 0;


	/* Verify the CTEB is still valid. */
	if (printEid(&bundle->custodian, wk->dictionary, &bundleCustodianEid)
			< 0)
	{
		putErrmsg("Can't print custodian EID string.", NULL);
		MRELEASE(custodianEid);
		return -1;
	}
	if(strcmp(bundleCustodianEid, custodianEid) != 0)
	{
		putErrmsg("Stale CTEB purged", bundleCustodianEid);
		MRELEASE(bundleCustodianEid);	
		MRELEASE(custodianEid);
		return 0;		/* Invalid.	*/
	}
	MRELEASE(bundleCustodianEid);	
	MRELEASE(custodianEid);


	/* Store the scratchpad. */
	blk->object = MTAKE(sizeof(CtebScratchpad));
	if(blk->object == NULL)
	{
		putErrmsg("Can't MTAKE for storing CTEB.",
				itoa(sizeof(CtebScratchpad)));
		return -1;		/* Error acquiring. */
	}
	memcpy(blk->object, &cteb, sizeof(CtebScratchpad));
	blk->size = sizeof(CtebScratchpad);
	return 1;
}

/* Release the CTEB scratchpad in working memory. */
void cteb_clear(AcqExtBlock *blk)
{
	if(blk->object == 0) return;

	MRELEASE(blk->object);
	blk->size = 0;
}

static int loadCtebScratchpadFromWm(Bundle *bundle, AcqWorkArea *work,
			CtebScratchpad *cteb)
{
	int				beforeOrAfter;
	LystElt			extLystElt;
	AcqExtBlock		*acqExtBlk;

	for(beforeOrAfter = 0; beforeOrAfter < 2; beforeOrAfter++)
	{
		for(extLystElt = lyst_first(work->extBlocks[beforeOrAfter]);
			extLystElt;
			extLystElt = lyst_next(extLystElt))
		{
			acqExtBlk = (AcqExtBlock *)(lyst_data(extLystElt));
			if(acqExtBlk->type == EXTENSION_TYPE_CTEB)
			{
				/* Store CTEB scratchpad to cteb */
				memcpy(cteb, acqExtBlk->object,
						sizeof(CtebScratchpad));
				return 0;
			}
		}
	}
	return -1;
}

static int loadCtebScratchpadFromSdr(Sdr sdr, Bundle *bundle, AcqWorkArea *work,
			CtebScratchpad *cteb)
{
	int				beforeOrAfter;
	Object          extElt = 0;
	Object			blkAddr;
	ExtensionBlock  blk;

	assert(sdr_in_xn(sdr) != 0);

	/* Find the first CTEB in the bundle. */
	for (beforeOrAfter = 0; beforeOrAfter < 2 && extElt == 0;
			beforeOrAfter++)
	{
		extElt = findExtensionBlock(bundle, EXTENSION_TYPE_CTEB,
				beforeOrAfter);
	}

	if (extElt == 0)
	{
		/* Didn't find any CTEBs in this bundle. */
		return -1;
	}
	
	/* Load the CTEB's associated scratchpad. */
	blkAddr = sdr_list_data(sdr, extElt);
	sdr_stage(sdr, (char *)(&blk), blkAddr, sizeof(ExtensionBlock));

	/* If there is a CTEB, but it doesn't have a scratchpad, then it
	 * isn't valid:  Any bundle agent properly implementing CTEB would
	 * ensure there's only one CTEB, so there is no valid CTEB on this
	 * bundle.  Don't bother looking further. */
	if (blk.object == 0)
	{
		return -1;
	}

	sdr_stage(sdr, (char *)(cteb), blk.object, blk.size);
	return 0;
}

int loadCtebScratchpad(Sdr sdr, Bundle *bundle, AcqWorkArea *work,
			CtebScratchpad *cteb)
{
	int rc = 0;

	CHKERR(sdr_begin_xn(sdr));

	/* If the bundle hasn't been recorded, the CTEB is in the acquisition 
	 * working memory. */
	if (work != NULL)
	{
		if (loadCtebScratchpadFromWm(bundle, work, cteb) < 0)
		{
			rc = -1;
		}
	}
	else
	{
		if (loadCtebScratchpadFromSdr(sdr, bundle, work, cteb) < 0)
		{
			rc = -1;
		}
	}

	sdr_exit_xn(sdr);
	return rc;
}