File: acsappend.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 (464 lines) | stat: -rw-r--r-- 13,049 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
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
/*
	acsappend.c: Append a bundle ID to an aggregate custody signal.

	Authors: Andrew Jenkins, Sebastian Kuzminsky,
				University of Colorado at Boulder

	Copyright (c) 2008-2011, Regents of the University of Colorado.
	This work was supported by NASA contracts NNJ05HE10G, NNC06CB40C, and
	NNC07CB47C.
											*/

#include "acsP.h"

static Sdr		acsSdr = NULL;

static int appendToSdrAcsFills(Object fills, const CtebScratchpad *cteb)
{
	unsigned int id = cteb->id;

	Object   curFillLElt;
	Object   curFillAddr;
	AcsFill  curFill;
	Object   nextFillLElt;
	Object   nextFillAddr;
	AcsFill  nextFill;
	Object   newFillAddr;
	AcsFill  newFill;
	unsigned int curFillUntil;	/* Last sequence number covered by curId. */

	ASSERT_ACSSDR_XN;

	/* If we insert a new sequence element into the list, it will be this. */
	newFill.start = id;
	newFill.length = 1;

	/* If the list is empty, then add newFill. */
	if (sdr_list_length(acsSdr, fills) == 0) {
		newFillAddr = sdr_malloc(acsSdr, sizeof(newFill));
		if (newFillAddr == 0)
		{
			ACSLOG_WARN("Couldn't sdr_malloc() new fill %u (%s)",
				id, strerror(errno));
			return -1;			/* Error */
		}
		sdr_poke(acsSdr, newFillAddr, newFill);

		if (sdr_list_insert_first(acsSdr, fills, newFillAddr) == 0)
		{
			ACSLOG_WARN("Couldn't insert new fill %u into list (%s)",
				id, strerror(errno));
			/* Stale newFill object will be backed out of SDR on
			 * sdr_end_xn() */
			return -1;			/* Error */
		}
		return 0;
	}

	for(curFillLElt = sdr_list_first(acsSdr, fills);
		curFillLElt;
		curFillLElt = sdr_list_next(acsSdr, curFillLElt))
	{
		curFillAddr = sdr_list_data(acsSdr, curFillLElt);
		if (curFillAddr == 0) {
			ACSLOG_ERROR("Can't read ACS list element (%s)", strerror(errno));
			return -1;
		}
		sdr_peek(acsSdr, curFill, curFillAddr);

		if (curFill.start <= id)
		{
			curFillUntil = curFill.start + curFill.length - 1;

			if (curFillUntil >= id)
			{
				return -2;		/* Already added */
			}

			nextFillLElt = sdr_list_next(acsSdr, curFillLElt);
			if (nextFillLElt != 0) {
				nextFillAddr = sdr_list_data(acsSdr, nextFillLElt);
				if(nextFillAddr == 0) {
					ACSLOG_ERROR("Couldn't read next ACS list element");
					return -1;
				}
				sdr_peek(acsSdr, nextFill, nextFillAddr);
			} else {
				nextFillAddr = 0;
			}

			/* If the current fill is just one too small on the right, 
			 * we should extend it. */
			if (curFillUntil + 1 == id)
			{
				/* We need to extend curFill.length by one.  But if that means
				 * we completely fill the hole between curFill and nextFill,
				 * then we should smush nextFill into curFill, too. */
				if (nextFillAddr && nextFill.start == id + 1)
				{
					curFill.length += 1 + nextFill.length;
					sdr_list_delete(acsSdr, nextFillLElt, NULL, NULL);
					sdr_free(acsSdr, nextFillAddr);
				} else {
					curFill.length++;
				}

				sdr_poke(acsSdr, curFillAddr, curFill);
				return 0;
			}

			/* If the next fill is just one too small on the left, we should
			 * extend it. */
			if (nextFillAddr && nextFill.start == id + 1)
			{
				nextFill.start = id;
				nextFill.length++;
				sdr_poke(acsSdr, nextFillAddr, nextFill);
				return 0;
			}

			/* If the next fill is after our id or doesn't exist, then
			 * we should insert this id in between current and next. */
			if (nextFillAddr == 0 || nextFill.start > id)
			{
				newFillAddr = sdr_malloc(acsSdr, sizeof(newFill));
				if (newFillAddr == 0)
				{
					ACSLOG_WARN("Couldn't sdr_malloc() new fill %u (%s)",
							id, strerror(errno));
					return -1;			/* Error */
				}
				sdr_poke(acsSdr, newFillAddr, newFill);

				if (sdr_list_insert_after(acsSdr, curFillLElt, newFillAddr) == 0)
				{
					ACSLOG_WARN("Couldn't insert new fill %u into list (%s)",
							id, strerror(errno));
					/* Stale newFill object will be backed out of SDR on
					 * sdr_end_xn() */
					return -1;			/* Error */
				}
				return 0;
			}

			/* If here, curFill.start <= id, but this wasn't the appropriate
			 * place to insert this sequence, because nextFill->id is
			 * also <= id. */
			continue;
		}

		/* curFill.id > id, and curFill.id wasn't ever <= id, so we need to
		 * insert a new id at the head of the list. */
		if (curFill.start == id + 1) {
			/* Extend curFill to the left by 1. */
			curFill.start = id;
			curFill.length++;
			sdr_poke(acsSdr, curFillAddr, curFill);
			return 0;
		} else {
			/* Make a new object at the beginning of the list. */
			newFillAddr = sdr_malloc(acsSdr, sizeof(newFill));
			if (newFillAddr == 0)
			{
				ACSLOG_WARN("Couldn't sdr_malloc() new fill %u (%s)",
						id, strerror(errno));
				return -1;			/* Error */
			}
			sdr_poke(acsSdr, newFillAddr, newFill);

			if (sdr_list_insert_first(acsSdr, fills, newFillAddr) == 0)
			{
				ACSLOG_WARN("Couldn't insert new fill %u into list (%s)",
						id, strerror(errno));
				/* Stale newFill object will be backed out of SDR on
				 * sdr_end_xn() */
				return -1;			/* Error */
			}
			return 0;
		}
	}

	ACSLOG_ERROR("Hit bottom of appendToAcsFills for %u", id);
	return -1;
}

static int newSdrAcsSignal(Object acsSignals, Object pendingCustAddr,
	BpCtReason reasonCode, unsigned char succeeded, const CtebScratchpad *cteb)
{
	SdrAcsSignal	newAcsSignal;
	Address			newAcsSignalAddr;
	Object			newAcsSignalLElt;

	ASSERT_ACSSDR_XN;

	newAcsSignalAddr = sdr_malloc(acsSdr, sizeof(newAcsSignal));
	if (newAcsSignalAddr == 0)
	{
		ACSLOG_WARN("Couldn't sdr_malloc() new ACS signal %s, %d, (%s)",
				succeeded ? "success" : "fail", reasonCode, strerror(errno));
		return -1;			/* Error */
	}

	newAcsSignal.succeeded  = succeeded;
	newAcsSignal.reasonCode = reasonCode;
	newAcsSignal.acsFills   = sdr_list_create(acsSdr);
	newAcsSignal.acsDue		= 0;
	newAcsSignal.pendingCustAddr = pendingCustAddr;
	newAcsSignal.serializedZco = 0;
	newAcsSignal.serializedZcoLength = 0;
	if(newAcsSignal.acsFills == 0) {
		ACSLOG_WARN("Couldn't allocate new ACS fills list (%s)", strerror(errno));
		return -1;
	}
	sdr_poke(acsSdr, newAcsSignalAddr, newAcsSignal);

	newAcsSignalLElt = sdr_list_insert(acsSdr, acsSignals, newAcsSignalAddr, cmpSdrAcsSignals,
			&newAcsSignal);

	if(newAcsSignalLElt == 0)
	{
		ACSLOG_WARN("Couldn't insert new ACS signal %s, %d, into list (%s)",
				succeeded ? "success" : "fail", reasonCode, strerror(errno));
		return -1;			/* Error */
	}

	return appendToSdrAcsFills(newAcsSignal.acsFills, cteb);
}

int appendToSdrAcsSignals(Object acsSignals, Object pendingCustAddr, 
	BpCtReason reasonCode, unsigned char succeeded,
	const CtebScratchpad *cteb)
{
	Object			curAcsSignalAddr;
	SdrAcsSignal	curAcsSignal;

	ASSERT_ACSSDR_XN;

	if(findSdrAcsSignal(acsSignals, reasonCode, succeeded,
							&curAcsSignalAddr) != 0)
	{
		ACSLOG_DEBUG("Found existing custody signal (%s,%d)",
			succeeded ? "SUCCESS" : "FAIL", reasonCode);
		sdr_peek(acsSdr, curAcsSignal, curAcsSignalAddr);
		return appendToSdrAcsFills(curAcsSignal.acsFills, cteb);
	}

	ACSLOG_DEBUG("Making new custody signal for (%s,%d)",
			succeeded ? "SUCCESS" : "FAIL", reasonCode);

	/* previous < (reasonCode, succeeded) < curAcsSignal; insert here */
	return newSdrAcsSignal(acsSignals, pendingCustAddr, reasonCode,
						succeeded, cteb);
}

static char *printSdrAcs(Object acsSignalAddr, const char *custodianEid)
{
	char	*reprAcs = MTAKE(MAX_REPRACS_LEN + 4);
	char	*cursor = reprAcs;
	int 	rc = 0;
	int		reprAcsLeft = MAX_REPRACS_LEN;
	int		first = 1;

	SdrAcsSignal		acsSignal;
	Object				fillsLElt;
	Object 				fillsAddr;
	SdrAcsFill		 	fill;

	if(reprAcs == NULL) {
		ACSLOG_ERROR("Couldn't print ACS (%s)", strerror(errno));
		return NULL;
	}

	ASSERT_ACSSDR_XN;

	sdr_peek(acsSdr, acsSignal, acsSignalAddr);

	rc = snprintf(cursor, reprAcsLeft, "%s: ",
				acsSignal.succeeded ? "SACK" : "SNACK");
	reprAcsLeft -= rc;
	cursor += rc;

	if(custodianEid != NULL)
	{
		rc = snprintf(cursor, reprAcsLeft, "to %s, ", custodianEid);
		reprAcsLeft -= rc;
		cursor += rc;
	}

	for(fillsLElt = sdr_list_first(acsSdr, acsSignal.acsFills);
		fillsLElt;
		fillsLElt = sdr_list_next(acsSdr, fillsLElt))
	{
		fillsAddr = sdr_list_data(acsSdr, fillsLElt);
		if(fillsAddr == 0)
		{
			ACSLOG_ERROR("Can't get list to print SDR ACS (%s)", strerror(errno));
			MRELEASE(reprAcs);
			return NULL;
		}
		sdr_peek(acsSdr, fill, fillsAddr);

		rc = snprintf(cursor, reprAcsLeft, "%s%u(%u)", first ? "" : " +",
					fill.start, fill.length);
		if (rc < 0 || rc >= reprAcsLeft) {
			sprintf(cursor, "...");
			return reprAcs;
		}
		reprAcsLeft -= rc;
		cursor += rc;
		first = 0;
	}

	return reprAcs;
}

static void printSdrAcsSignal(int loglevel, Object acsSignals, BpCtReason reasonCode,
				unsigned char succeeded, const char *custodianEid)
{
	Object signalAddr;
	char *reprAcsSignal;

	CHKVOID(sdr_begin_xn(acsSdr));
	if (findSdrAcsSignal(acsSignals, reasonCode, succeeded,
				&signalAddr))
	{
		reprAcsSignal = printSdrAcs(signalAddr, custodianEid);
		ACSLOG(loglevel, reprAcsSignal, NULL);
		MRELEASE(reprAcsSignal);
	}

	sdr_exit_xn(acsSdr);
}

static void printAcsInformation(int loglevel, const char *note, Bundle *bundle,
		char *dictionary, int succeeded, BpCtReason reasonCode)
{
	char *currentCustodianEid;
	char *sourceEid;

	if (printEid(&bundle->id.source, dictionary, &sourceEid) < 0)
	{
		putErrmsg("Couldn't print source of bundle.", NULL);
		return;
	}
	if (printEid(&bundle->custodian, dictionary, &currentCustodianEid) < 0)
	{
		putErrmsg("Couldn't print source of bundle.", NULL);
		MRELEASE(sourceEid);
		return;
	}

	if (bundle->bundleProcFlags & BDL_IS_FRAGMENT)
	{
		ACSLOG(loglevel, "%s: %s,%u.%u(%u:%u) (reason: %d, current custodian: %s)",
			succeeded ? "SACK" : "SNACK", sourceEid, 
			bundle->id.creationTime.seconds, bundle->id.creationTime.count,
			bundle->id.fragmentOffset, bundle->payload.length,
			reasonCode, currentCustodianEid);
	} else {
		ACSLOG(loglevel, "%s: %s,%u.%u (reason: %d, current custodian: %s)",
			succeeded ? "SACK" : "SNACK", sourceEid, 
			bundle->id.creationTime.seconds, bundle->id.creationTime.count,
			reasonCode, currentCustodianEid);
	}
	MRELEASE(sourceEid);
	MRELEASE(currentCustodianEid);
}

int	offerNoteAcs(Bundle *bundle, AcqWorkArea *work, char *dictionary,
				 int succeeded, BpCtReason reasonCode)
{
	Object				pendingCustAddr;
	SdrAcsPendingCust	pendingCust;
	Object				pendingCustodians;
	int					rc;
	char				*currentCustodianEid;
	Sdr					bpSdr = getIonsdr();
	CtebScratchpad		cteb;

	if (acsAttach() < 0)
	{
		//Couldn't offerNoteAcs(): ACS SDR not available.
		return 0;
	}

	if ((acsSdr = getAcssdr()) == NULL)
	{
		putErrmsg("Couldn't offerNoteAcs(): Can't get ACS SDR.", NULL);
		return 0;
	}

	if (printEid(&bundle->custodian, dictionary, &currentCustodianEid) < 0)
	{
		ACSLOG_ERROR("Couldn't print current custodian of a bundle.");
		return 0;		/* Couldn't note, so caller should send normal CT */
	}

	/* To prevent deadlock, take bpSdr before acsSdr. */
	CHKZERO(sdr_begin_xn(bpSdr));
	CHKZERO(sdr_begin_xn(acsSdr));

	/* Find the valid CTEB for this bundle; else, we can't ACS. */
	if(loadCtebScratchpad(bpSdr, bundle, work, &cteb) < 0)
	{
		MRELEASE(currentCustodianEid);
		sdr_exit_xn(acsSdr);
		sdr_exit_xn(bpSdr);
		return 0;
	}

	/* Find the custodian information associated with this EID */
	pendingCustodians = getPendingCustodians();
	pendingCustAddr = getOrMakeCustodianByEid(pendingCustodians, currentCustodianEid);
	sdr_peek(acsSdr, pendingCust, pendingCustAddr);

	printAcsInformation(ACSLOGLEVEL_DEBUG, "Appending ACS", bundle, dictionary, succeeded, reasonCode);
	rc = appendToSdrAcsSignals(pendingCust.signals, pendingCustAddr,
				reasonCode, succeeded, &cteb);
	if (rc == -2) {
		ACSLOG_INFO("Duplicate ACS signalled for %s, ignored.", pendingCust.eid);
		printSdrAcsSignal(ACSLOGLEVEL_INFO, pendingCust.signals, reasonCode, 
				succeeded, pendingCust.eid);
		MRELEASE(currentCustodianEid);
		sdr_exit_xn(acsSdr);
		sdr_exit_xn(bpSdr);
		return 1;		/* A dup is successfully noted */
	}
	if (rc == -1) {
		ACSLOG_ERROR("Problem signalling ACS for %s (%s)", pendingCust.eid,
				strerror(errno));
		MRELEASE(currentCustodianEid);
		sdr_cancel_xn(acsSdr);
		sdr_exit_xn(bpSdr);
		return 0;		/* Couldn't note, so caller should send normal CT */
	}
	printSdrAcsSignal(ACSLOGLEVEL_DEBUG, pendingCust.signals, reasonCode,
				succeeded, pendingCust.eid);

	if (trySendAcs(&pendingCust, reasonCode, succeeded, &cteb) != 0)
	{
		/* We failed to attempt sending an ACS handling this new signal
		 * (either itself containing the signal, or carrying over this signal
		 * to a new, empty ACS).  We should revert the SDR changes we made
		 * and return 0 so that the caller will send a normal custody signal. */
		MRELEASE(currentCustodianEid);
		sdr_cancel_xn(acsSdr);
		sdr_cancel_xn(bpSdr);
		return 0;
	}

	MRELEASE(currentCustodianEid);

	if(sdr_end_xn(acsSdr) < 0)
	{
		putSysErrmsg("Can't commit new ACS", pendingCust.eid);
		sdr_cancel_xn(bpSdr);
		return 0;
	}
	if(sdr_end_xn(bpSdr) < 0)
	{
		putSysErrmsg("Can't transmit new ACS", pendingCust.eid);
		return 0;
	}
	return 1;			/* Successfully noted */
}