File: acsid.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 (281 lines) | stat: -rw-r--r-- 6,659 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
/*
	acsid.c: Implementation of the ACS Custody ID database.

	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"
#include "sdrhash.h"

static Sdr      acsSdr = NULL;

static void consume_cid(AcsCustodyId *cid)
{
	unsigned int newid = 0;

	sdr_peek(acsSdr, cid->id, acsConstants->id);
	newid = cid->id + 1;
	sdr_poke(acsSdr, acsConstants->id, newid);
}


int get_or_make_custody_id(const char *sourceEid,
		const BpTimestamp *creationTime, unsigned int fragmentOffset,
		unsigned int fragmentLength, AcsCustodyId *cid)
{
	AcsBundleId	bid;
	Address		cbidAddr;
	AcsCbidEntry	cbid;
	Object		cbidObj;
	int		rc;
	Object		hashEntry;

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

	if ((acsSdr = getAcssdr()) == NULL)
	{
		ACSLOG_DEBUG("get_or_make_custody_id: ACS not initialized, skipping.");
		return -2;
	}

	memset(&bid, 0, sizeof(bid));
	strncpy(bid.sourceEid, sourceEid, MAX_EID_LEN);
	bid.sourceEid[MAX_EID_LEN - 1] = '\0';
	bid.creationTime.seconds = creationTime->seconds;
	bid.creationTime.count = creationTime->count;
	bid.fragmentOffset = fragmentOffset;
	bid.fragmentLength = fragmentLength;

	CHKERR(sdr_begin_xn(acsSdr));

	rc = sdr_hash_retrieve(acsSdr, acsConstants->bidHash,
			(char *)(&bid), &cbidAddr, &hashEntry);
	if (rc == -1)
	{
		ACSLOG_ERROR("Couldn't search for (%s,%u,%u,%u,%u) in bidHash",
				bid.sourceEid, bid.creationTime.seconds,
				bid.creationTime.count,
				bid.fragmentOffset, bid.fragmentLength);
		sdr_cancel_xn(acsSdr);
		return -1;
	}

	/* If a CID already exists, return it. */
	if (rc == 1)
	{
		if (cid != NULL)
		{
			sdr_peek(acsSdr, cbid, cbidAddr);
			cid->id = cbid.custodyId.id;
		}
		sdr_exit_xn(acsSdr);
		return 1;
	}

	/* No CID exists; create one. */
	memcpy(&cbid.bundleId, &bid, sizeof(bid));
	consume_cid(&cbid.custodyId);

	cbidObj = sdr_stow(acsSdr, cbid);

	/* Add a ref to the bidHash to this cbid. */
	if (sdr_hash_insert(acsSdr, acsConstants->bidHash,
				(char *)(&cbid.bundleId), cbidObj, NULL) < 0)
	{
		ACSLOG_ERROR("Couldn't insert new CBID in bidHash");
		sdr_cancel_xn(acsSdr);
		return -1;
	}

	/* Add a ref to the cidHash to this cbid. */
	if (sdr_hash_insert(acsSdr, acsConstants->cidHash,
				(char *)(&cbid.custodyId), cbidObj, NULL) < 0)
	{
		ACSLOG_ERROR("Couldn't insert new CBID in cidHash");
		sdr_cancel_xn(acsSdr);
		return -1;
	}

	if(cid != NULL)
	{
		cid->id = cbid.custodyId.id;
	}

	return sdr_end_xn(acsSdr);
}

int get_bundle_id(AcsCustodyId *custodyId, AcsBundleId *id)
{
	int		rc;
	Address		cbidAddr;
	AcsCbidEntry	cbid;
	Object		hashEntry;

	if (acsAttach() < 0)
	{
		ACSLOG_ERROR("get_bundle_id: Couldn't attach to ACS.");
		return -1;
	}

	if ((acsSdr = getAcssdr()) == NULL)
	{
		ACSLOG_ERROR("get_bundle_id: ACS not initialized.");
		return -1;
	}

	CHKERR(sdr_begin_xn(acsSdr));
	rc = sdr_hash_retrieve(acsSdr, acsConstants->cidHash,
			(char *)(custodyId), &cbidAddr, &hashEntry);
	if (rc == 1)
	{
		sdr_peek(acsSdr, cbid, cbidAddr);
	}
	sdr_exit_xn(acsSdr);

	/* Error searching hash table. */
	if (rc == -1)
	{
		ACSLOG_ERROR("Couldn't search for (%u) in cidHash", custodyId->id);
		return -1;
	}

	/* Hash table searched, but no matching custody ID found. */
	if (rc == 0)
	{
		ACSLOG_WARN("Couldn't find cid (%u) in cidHash", custodyId->id);
		return 1;
	}

	/* Matching custody ID found.  Assign outputs if requested by caller. */
	if(id != NULL)
	{
		memcpy(id, &cbid.bundleId, sizeof(cbid.bundleId));
	}

	return 0;
}

int destroy_custody_id(AcsBundleId *bundleId)
{
	Address		cbidAddr;
	AcsCbidEntry	cbid;
	int		rc;
	Object		hashEntry;

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

	if ((acsSdr = getAcssdr()) == NULL)
	{
		ACSLOG_DEBUG("destroy_custody_id: ACS not initialized, skipping.");
		return -2;
	}

	/* Lookup the cbid. */
	CHKERR(sdr_begin_xn(acsSdr));
	rc = sdr_hash_retrieve(acsSdr, acsConstants->bidHash,
			(char *)(bundleId), &cbidAddr, &hashEntry);
	if (rc == -1)
	{
		ACSLOG_ERROR("Couldn't search for (%s,%u,%u,%u,%u) in bidHash"
				" to destroy",
				bundleId->sourceEid, bundleId->creationTime.seconds,
				bundleId->creationTime.count, bundleId->fragmentOffset,
				bundleId->fragmentLength);
		sdr_exit_xn(acsSdr);
		return -1;
	}
	if (rc == 0)
	{
		ACSLOG_WARN("Couldn't find (%s,%u,%u,%u,%u) in bidHash to destroy",
				bundleId->sourceEid, bundleId->creationTime.seconds,
				bundleId->creationTime.count, bundleId->fragmentOffset,
				bundleId->fragmentLength);
		sdr_exit_xn(acsSdr);
		return 0;
	}
	sdr_peek(acsSdr, cbid, cbidAddr);

	/* Found the CBID; destroy the entries in cidHash and bidHash. */
	rc = sdr_hash_remove(acsSdr, acsConstants->cidHash,
				(char *)(&cbid.custodyId), NULL);
	if (rc != 1)
	{
		ACSLOG_ERROR("Couldn't delete (%u) from cidHash", cbid.custodyId.id);
		sdr_cancel_xn(acsSdr);
		return -1;
	}
	rc = sdr_hash_remove(acsSdr, acsConstants->bidHash,
				(char *)(&cbid.bundleId), NULL);
	if (rc != 1)
	{
		ACSLOG_ERROR("Couldn't delete (%s,%u,%u,%u,%u) from bidHash",
				cbid.bundleId.sourceEid, cbid.bundleId.creationTime.seconds,
				cbid.bundleId.creationTime.count, cbid.bundleId.fragmentOffset,
				cbid.bundleId.fragmentLength);
		sdr_cancel_xn(acsSdr);
		return -1;
	}

	/* Hash table entries destroyed; destroy the CBID Object in SDR they
	 * pointed to. */
	sdr_free(acsSdr, cbidAddr);

	/* Cleanup */
	if (sdr_end_xn(acsSdr) != 0)
	{
		ACSLOG_ERROR("Couldn't destroy custody ID.");
		return -1;
	}
	return 1;
}

int destroyAcsMetadata(Bundle *bundle)
{
	AcsBundleId 	bid;
	char 			*dictionary;
	char			*sourceEid;

	memset(&bid, 0, sizeof(bid));

	/* Get the source EID */
	if ((dictionary = retrieveDictionary(bundle))
			== (char *) bundle)
	{
		putErrmsg("Can't retrieve dictionary.", NULL);
		return -1;
	}

	if (printEid(&(bundle->id.source), dictionary, &sourceEid) < 0)
	{
		putErrmsg("Can't get sourceEid.", NULL);
		releaseDictionary(dictionary);
		return -1;
	}
	releaseDictionary(dictionary);
	strncpy(bid.sourceEid, sourceEid, MAX_EID_LEN);
	bid.sourceEid[MAX_EID_LEN - 1] = '\0';
	MRELEASE(sourceEid);

	/* Get the rest of the bundle ID */
	bid.creationTime.seconds = bundle->id.creationTime.seconds;
	bid.creationTime.count = bundle->id.creationTime.count;
	bid.fragmentOffset = bundle->id.fragmentOffset;
	bid.fragmentLength = bundle->totalAduLength == 
							0 ? 0 : bundle->payload.length;

	/* Destroy the metadata */
	return destroy_custody_id(&bid);
}