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
|
/*
ltpcli.c: BP LTP-based convergence-layer input
daemon, designed to serve as an input
duct.
Author: Scott Burleigh, JPL
Copyright (c) 2007, California Institute of Technology.
ALL RIGHTS RESERVED. U.S. Government Sponsorship
acknowledged.
*/
#include "ltpcla.h"
#include "ipnfw.h"
#include "dtn2fw.h"
static void interruptThread()
{
isignal(SIGTERM, interruptThread);
ionKillMainThread("ltpcli");
}
/* * * Receiver thread functions * * */
typedef struct
{
VInduct *vduct;
int running;
} ReceiverThreadParms;
static int acquireRedBundles(AcqWorkArea *work, Object zco,
uvast senderEngineNbr)
{
if (bpBeginAcq(work, 0, NULL) < 0)
{
putErrmsg("Can't begin acquisition of bundle(s).", NULL);
return -1;
}
if (bpLoadAcq(work, zco) < 0)
{
putErrmsg("Can't continue bundle acquisition.", NULL);
return -1;
}
if (bpEndAcq(work) < 0)
{
putErrmsg("Can't end acquisition of bundle(s).", NULL);
return -1;
}
return 0;
}
static int handleGreenSegment(AcqWorkArea *work, LtpSessionId *sessionId,
unsigned char endOfBlock, unsigned int offset,
unsigned int length, Object zco, unsigned int *buflen, char **buffer)
{
Sdr sdr = getIonsdr();
static LtpSessionId currentSessionId = { 0, 0 };
static unsigned int currentOffset = 0;
unsigned int fillLength;
ZcoReader reader;
int result;
if (zco == 0) /* Import session canceled. */
{
bpCancelAcq(work);
currentSessionId.sourceEngineId = 0;
currentSessionId.sessionNbr = 0;
currentOffset = 0;
return 0;
}
if (zco_source_data_length(sdr, zco) != length)
{
return 0; /* Just discard the segment. */
}
if (sessionId->sourceEngineId != currentSessionId.sourceEngineId
|| sessionId->sessionNbr != currentSessionId.sessionNbr)
{
/* Did not receive end-of-block segment for the
* block that was being received. Discard the
* partially received bundle in the work area,
* if any. */
bpCancelAcq(work);
currentSessionId.sourceEngineId = 0;
currentSessionId.sessionNbr = 0;
currentOffset = 0;
}
if (currentOffset == 0)
{
/* Start new green bundle acquisition. */
if (bpBeginAcq(work, 0, NULL) < 0)
{
putErrmsg("Can't begin acquisition of bundle.", NULL);
return -1;
}
currentSessionId.sourceEngineId = sessionId->sourceEngineId;
currentSessionId.sessionNbr = sessionId->sessionNbr;
}
if (offset < currentOffset) /* Out of order. */
{
return 0; /* Just discard the segment. */
}
if (offset > currentOffset)
{
/* Convergence layer must not deliver incomplete
* bundles to BP. Practically speaking, this
* gap in segment sequence must be treated as
* malformation of the bundle. */
work->malformed = 1;
/* But continue bundle acquisition anyway, in
* case the incomplete bundle is useful for some
* diagnostic purpose. */
fillLength = offset - currentOffset;
if (fillLength > *buflen)
{
/* Make buffer big enough for the fill
* data. */
if (*buffer)
{
MRELEASE(*buffer);
*buflen = 0;
}
*buffer = MTAKE(fillLength);
if (*buffer == NULL)
{
/* Gap is too large to fill.
* Might be a DOS attack; cancel
* acquisition. */
bpCancelAcq(work);
currentSessionId.sourceEngineId = 0;
currentSessionId.sessionNbr = 0;
currentOffset = 0;
return 0;
}
*buflen = fillLength;
}
memset(*buffer, 0, fillLength);
if (bpContinueAcq(work, *buffer, (int) fillLength) < 0)
{
putErrmsg("Can't insert bundle fill data.", NULL);
return -1;
}
currentOffset += fillLength;
}
if (length > *buflen)
{
/* Make buffer big enough for the green data. */
if (*buffer)
{
MRELEASE(*buffer);
*buflen = 0;
}
*buffer = MTAKE(length);
if (*buffer == NULL)
{
/* Segment is too large. Might be a
* DOS attack; cancel acquisition. */
bpCancelAcq(work);
currentSessionId.sourceEngineId = 0;
currentSessionId.sessionNbr = 0;
currentOffset = 0;
return 0;
}
*buflen = length;
}
/* Extract data from segment ZCO so that it can be
* appended to the bundle acquisition ZCO. */
zco_start_receiving(zco, &reader);
CHKERR(sdr_begin_xn(sdr));
result = zco_receive_source(sdr, &reader, length, *buffer);
if (sdr_end_xn(sdr) < 0 || result < 0)
{
putErrmsg("Failed reading green segment data.", NULL);
return -1;
}
if (bpContinueAcq(work, *buffer, (int) length) < 0)
{
putErrmsg("Can't continue bundle acquisition.", NULL);
return -1;
}
currentOffset += length;
if (endOfBlock)
{
if (bpEndAcq(work) < 0)
{
putErrmsg("Can't end acquisition of bundle.", NULL);
return -1;
}
currentSessionId.sourceEngineId = 0;
currentSessionId.sessionNbr = 0;
currentOffset = 0;
}
return 0;
}
static void *handleNotices(void *parm)
{
/* Main loop for LTP notice reception and handling. */
Sdr sdr = getIonsdr();
ReceiverThreadParms *rtp = (ReceiverThreadParms *) parm;
char *procName = "ltpcli";
AcqWorkArea *redWork;
AcqWorkArea *greenWork;
LtpNoticeType type;
LtpSessionId sessionId;
unsigned char reasonCode;
unsigned char endOfBlock;
unsigned int dataOffset;
unsigned int dataLength;
Object data; /* ZCO reference. */
unsigned int greenBuflen = 0;
char *greenBuffer = NULL;
snooze(1); /* Let main thread become interruptable. */
if (ltp_open(BpLtpClientId) < 0)
{
putErrmsg("ltpcli can't open client access.",
itoa(BpLtpClientId));
ionKillMainThread(procName);
return NULL;
}
redWork = bpGetAcqArea(rtp->vduct);
greenWork = bpGetAcqArea(rtp->vduct);
if (redWork == NULL || greenWork == NULL)
{
ltp_close(BpLtpClientId);
putErrmsg("ltpcli can't get acquisition work areas", NULL);
ionKillMainThread(procName);
return NULL;
}
/* Can now start receiving notices. On failure, take
* down the CLI. */
while (rtp->running)
{
if (ltp_get_notice(BpLtpClientId, &type, &sessionId,
&reasonCode, &endOfBlock, &dataOffset,
&dataLength, &data) < 0)
{
putErrmsg("Can't get LTP notice.", NULL);
ionKillMainThread(procName);
rtp->running = 0;
continue;
}
switch (type)
{
case LtpExportSessionComplete: /* Xmit success. */
if (data == 0) /* Ignore it. */
{
break; /* Out of switch. */
}
if (bpHandleXmitSuccess(data, 0) < 0)
{
putErrmsg("Crashed on xmit success.", NULL);
ionKillMainThread(procName);
rtp->running = 0;
break; /* Out of switch. */
}
CHKNULL(sdr_begin_xn(sdr));
zco_destroy(sdr, data);
if (sdr_end_xn(sdr) < 0)
{
putErrmsg("Crashed on data cleanup.", NULL);
ionKillMainThread(procName);
rtp->running = 0;
}
break; /* Out of switch. */
case LtpExportSessionCanceled: /* Xmit failure. */
if (data == 0) /* Ignore it. */
{
break; /* Out of switch. */
}
if (bpHandleXmitFailure(data) < 0)
{
putErrmsg("Crashed on xmit failure.", NULL);
ionKillMainThread(procName);
rtp->running = 0;
break; /* Out of switch. */
}
CHKNULL(sdr_begin_xn(sdr));
zco_destroy(sdr, data);
if (sdr_end_xn(sdr) < 0)
{
putErrmsg("Crashed on data cleanup.", NULL);
ionKillMainThread(procName);
rtp->running = 0;
}
break; /* Out of switch. */
case LtpImportSessionCanceled:
/* None of the red data for the import
* session (if any) have been received
* yet, so nothing to discard. In case
* part or all of the import session was
* green data, force deletion of retained
* data. */
sessionId.sourceEngineId = 0;
sessionId.sessionNbr = 0;
if (handleGreenSegment(greenWork, &sessionId,
0, 0, 0, 0, NULL, NULL) < 0)
{
putErrmsg("Can't cancel green session.", NULL);
ionKillMainThread(procName);
rtp->running = 0;
}
break; /* Out of switch. */
case LtpRecvRedPart:
if (!endOfBlock)
{
/* Block is partially red and
* partially green. Too risky
* to wait for green EOB before
* clearing the work area, so
* just discard the data. */
CHKNULL(sdr_begin_xn(sdr));
zco_destroy(sdr, data);
if (sdr_end_xn(sdr) < 0)
{
putErrmsg("Crashed: partially red.",
NULL);
ionKillMainThread(procName);
rtp->running = 0;
}
break; /* Out of switch. */
}
if (acquireRedBundles(redWork, data,
sessionId.sourceEngineId) < 0)
{
putErrmsg("Can't acquire bundle(s).", NULL);
ionKillMainThread(procName);
rtp->running = 0;
}
break; /* Out of switch. */
case LtpRecvGreenSegment:
if (handleGreenSegment(greenWork, &sessionId,
endOfBlock, dataOffset, dataLength,
data, &greenBuflen, &greenBuffer) < 0)
{
putErrmsg("Can't handle green segment.", NULL);
ionKillMainThread(procName);
rtp->running = 0;
}
/* Discard the ZCO in any case. */
CHKNULL(sdr_begin_xn(sdr));
zco_destroy(sdr, data);
if (sdr_end_xn(sdr) < 0)
{
putErrmsg("Crashed: green segment.", NULL);
ionKillMainThread(procName);
rtp->running = 0;
}
break; /* Out of switch. */
default:
break; /* Out of switch. */
}
/* Make sure other tasks have a chance to run. */
sm_TaskYield();
}
writeErrmsgMemos();
writeMemo("[i] ltpcli receiver thread has ended.");
/* Free resources. */
if (greenBuffer)
{
MRELEASE(greenBuffer);
}
bpReleaseAcqArea(greenWork);
bpReleaseAcqArea(redWork);
ltp_close(BpLtpClientId);
return NULL;
}
/* * * Main thread functions * * * */
#if defined (VXWORKS) || defined (RTEMS) || defined (bionic)
int ltpcli(int a1, int a2, int a3, int a4, int a5,
int a6, int a7, int a8, int a9, int a10)
{
char *ductName = (char *) a1;
#else
int main(int argc, char *argv[])
{
char *ductName = (argc > 1 ? argv[1] : NULL);
#endif
VInduct *vduct;
PsmAddress vductElt;
ReceiverThreadParms rtp;
pthread_t receiverThread;
if (ductName == NULL)
{
PUTS("Usage: ltpcli <local engine number>]");
return 0;
}
if (bpAttach() < 0)
{
putErrmsg("ltpcli can't attach to BP.", NULL);
return -1;
}
findInduct("ltp", ductName, &vduct, &vductElt);
if (vductElt == 0)
{
putErrmsg("No such ltp duct.", ductName);
return -1;
}
if (vduct->cliPid != ERROR && vduct->cliPid != sm_TaskIdSelf())
{
putErrmsg("CLI task is already started for this duct.",
itoa(vduct->cliPid));
return -1;
}
/* All command-line arguments are now validated. */
if (ltp_attach() < 0)
{
putErrmsg("ltpcli can't initialize LTP.", NULL);
return -1;
}
/* Set up signal handling; SIGTERM is shutdown signal. */
ionNoteMainThread("ltpcli");
isignal(SIGTERM, interruptThread);
/* Start the receiver thread. */
rtp.vduct = vduct;
rtp.running = 1;
if (pthread_begin(&receiverThread, NULL, handleNotices, &rtp))
{
putSysErrmsg("ltpcli can't create receiver thread", NULL);
return 1;
}
/* Now sleep until interrupted by SIGTERM, at which point
* it's time to stop the induct. */
writeMemo("[i] ltpcli is running.");
ionPauseMainThread(-1);
/* Time to shut down. */
rtp.running = 0;
/* Stop the receiver thread by interrupting client access. */
ltp_interrupt(BpLtpClientId);
pthread_join(receiverThread, NULL);
writeErrmsgMemos();
writeMemo("[i] ltpcli duct has ended.");
ionDetach();
return 0;
}
|