Marshaller.cpp 36.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/******************************************************************************
 *                      Marshaller.cpp
 *
 * This file is part of the Castor project.
 * See http://castor.web.cern.ch/castor
 *
 * Copyright (C) 2003  CERN
 * This program 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 2
 * of the License, or (at your option) any later version.
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 *
 *
 *
23
 * @author Nicola.Bessone@cern.ch Steven.Murray@cern.ch
24
25
 *****************************************************************************/

26
#include "castor/exception/Internal.hpp"
27
#include "castor/tape/aggregator/Marshaller.hpp"
28
29
#include "h/rtcp_constants.h"
#include "h/vdqm_constants.h"
30

31
32
#include <errno.h>
#include <iostream>
33
34
35
#include <string.h>


36
37
38
39
//------------------------------------------------------------------------------
// marshallUint8
//------------------------------------------------------------------------------
void castor::tape::aggregator::Marshaller::marshallUint8(uint8_t src,
40
  char * &dst) throw(castor::exception::Exception) {
41

42
  marshallInteger(src, dst);
43
}
44
45


46
47
48
49
50
//------------------------------------------------------------------------------
// unmarshallUint8
//------------------------------------------------------------------------------
void castor::tape::aggregator::Marshaller::unmarshallUint8(const char * &src,
  size_t &srcLen, uint8_t &dst) throw(castor::exception::Exception) {
51

52
  unmarshallInteger(src, srcLen, dst);
53
54
55
56
57
58
59
}


//------------------------------------------------------------------------------
// marshallUint16
//------------------------------------------------------------------------------
void castor::tape::aggregator::Marshaller::marshallUint16(uint16_t src,
60
  char * &dst) throw(castor::exception::Exception) {
61

62
  marshallInteger(src, dst);
63
}
64
65


66
67
68
69
70
//------------------------------------------------------------------------------
// unmarshallUint16
//------------------------------------------------------------------------------
void castor::tape::aggregator::Marshaller::unmarshallUint16(const char * &src,
  size_t &srcLen, uint16_t &dst) throw(castor::exception::Exception) {
71

72
  unmarshallInteger(src, srcLen, dst);
73
74
75
}


76
77
78
//------------------------------------------------------------------------------
// marshallUint32
//------------------------------------------------------------------------------
79
void castor::tape::aggregator::Marshaller::marshallUint32(uint32_t src,
80
  char * &dst) throw(castor::exception::Exception) {
81

82
  marshallInteger(src, dst);
83
84
85
}


86
//------------------------------------------------------------------------------
87
// unmarshallUint32
88
//------------------------------------------------------------------------------
89
void castor::tape::aggregator::Marshaller::unmarshallUint32(const char * &src,
90
91
  size_t &srcLen, uint32_t &dst) throw(castor::exception::Exception) {

92
93
  unmarshallInteger(src, srcLen, dst);
}
94
95


96
97
98
99
//------------------------------------------------------------------------------
// marshallInt32
//------------------------------------------------------------------------------
void castor::tape::aggregator::Marshaller::marshallInt32(int32_t src,
100
  char * &dst) throw(castor::exception::Exception) {
101

102
103
  marshallInteger(src, dst);
}
104
105


106
107
108
109
110
//------------------------------------------------------------------------------
// unmarshallInt32
//------------------------------------------------------------------------------
void castor::tape::aggregator::Marshaller::unmarshallInt32(const char * &src,
  size_t &srcLen, int32_t &dst) throw(castor::exception::Exception) {
111

112
  unmarshallInteger(src, srcLen, dst);
113
114
115
116
117
118
119
}


//------------------------------------------------------------------------------
// marshallUint64
//------------------------------------------------------------------------------
void castor::tape::aggregator::Marshaller::marshallUint64(uint64_t src,
120
  char * &dst) throw(castor::exception::Exception) {
121

122
  marshallInteger(src, dst);
123
124
125
126
127
128
129
130
131
}


//------------------------------------------------------------------------------
// unmarshallUint64
//------------------------------------------------------------------------------
void castor::tape::aggregator::Marshaller::unmarshallUint64(const char * &src,
  size_t &srcLen, uint64_t &dst) throw(castor::exception::Exception) {

132
  unmarshallInteger(src, srcLen, dst);
133
134
135
}


136
137
138
//------------------------------------------------------------------------------
// marshallString
//------------------------------------------------------------------------------
139
140
141
142
143
144
void castor::tape::aggregator::Marshaller::marshallString(const char * src,
  char * &dst) throw(castor::exception::Exception) {

  if(dst == NULL) {
    castor::exception::Exception ex(EINVAL);

145
146
    ex.getMessage() << __PRETTY_FUNCTION__
      << ": Pointer to destination buffer is NULL";
147
148
    throw ex;
  }
149

150
  strcpy(dst, src);
151

152
  dst += strlen(src) + 1;
153
154
155
}


156
//------------------------------------------------------------------------------
157
// marshallString
158
//------------------------------------------------------------------------------
159
160
161
162
163
void castor::tape::aggregator::Marshaller::marshallString(
  const std::string &src, char * &dst) throw(castor::exception::Exception) {

  if(dst == NULL) {
    castor::exception::Exception ex(EINVAL);
164

165
166
    ex.getMessage() << __PRETTY_FUNCTION__
      << ": Pointer to destination buffer is NULL";
167
168
    throw ex;
  }
169

170
171
172
173
174
175
176
177
178
  strcpy(dst, src.c_str());

  dst += strlen(src.c_str()) + 1;
}


//------------------------------------------------------------------------------
// unmarshallString
//------------------------------------------------------------------------------
179
void castor::tape::aggregator::Marshaller::unmarshallString(const char * &src,
180
  size_t &srcLen, char *dst, const size_t dstLen)
181
  throw(castor::exception::Exception) {
182
183
184
185

  if(src == NULL) {
    castor::exception::Exception ex(EINVAL);

186
187
    ex.getMessage() << __PRETTY_FUNCTION__
      << ": Pointer to source buffer is NULL";
188
189
190
191
192
193
    throw ex;
  }

  if(srcLen == 0) {
    castor::exception::Exception ex(EINVAL);

194
195
    ex.getMessage() << __PRETTY_FUNCTION__
      << ": Source buffer length is 0";
196
197
198
199
200
201
    throw ex;
  }

  if(dst == NULL) {
    castor::exception::Exception ex(EINVAL);

202
203
    ex.getMessage() << __PRETTY_FUNCTION__
      << ": Pointer to destination buffer is NULL";
204
205
206
207
208
209
    throw ex;
  }

  if(dstLen == 0) {
    castor::exception::Exception ex(EINVAL);

210
211
    ex.getMessage() << __PRETTY_FUNCTION__
      << ": Destination buffer length is 0";
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
    throw ex;
  }

  // Calculate the maximum number of bytes that could be unmarshalled
  size_t maxlen = 0;
  if(srcLen < dstLen) {
    maxlen = srcLen;
  } else {
    maxlen = dstLen;
  }

  bool strTerminatorReached = false;

  // While there are potential bytes to copy and the string terminator has not
  // been reached
  for(size_t i=0; i<maxlen && !strTerminatorReached; i++) {
    // If the string terminator has been reached
    if((*dst++ = *src++) == '\0') {
      strTerminatorReached = true;
    }

    srcLen--;
  }

  // If all potential bytes were copied but the string terminator was not
  // reached
  if(!strTerminatorReached) {
    castor::exception::Exception ex(EINVAL);

241
242
    ex.getMessage() << __PRETTY_FUNCTION__
      << ": String terminator of source buffer was not reached";
243
244
    throw ex;
  }
245
}
246
247
248


//-----------------------------------------------------------------------------
249
// marshallMessageHeader
250
//-----------------------------------------------------------------------------
251
252
253
254
255
256
257
258
259
260
261
262
263
size_t castor::tape::aggregator::Marshaller::marshallMessageHeader(
  char *const dst, const size_t dstLen, const MessageHeader &src)
  throw(castor::exception::Exception) {

  if(dst == NULL) {
    castor::exception::Exception ex(EINVAL);

    ex.getMessage() << __PRETTY_FUNCTION__
      << ": Pointer to destination buffer is NULL";
    throw ex;
  }

  // Calculate the length of the message header
264
  const uint32_t totalLen = 3 * sizeof(uint32_t);  // magic + reqType + len
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279

  // Check that the message header buffer is big enough
  if(totalLen > dstLen) {
    castor::exception::Exception ex(EMSGSIZE);

    ex.getMessage() << __PRETTY_FUNCTION__
      << ": Buffer too small for message header: "
      "Required size: " << totalLen << " Actual size: " << dstLen;

    throw ex;
  }

  // Marshall the message header
  char *p = dst;
  marshallUint32(src.magic  , p);
280
  marshallUint32(src.reqType, p);
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
  marshallUint32(src.len    , p);

  // Calculate the number of bytes actually marshalled
  const size_t nbBytesMarshalled = p - dst;

  // Check that the number of bytes marshalled was what was expected
  if(totalLen != nbBytesMarshalled) {
    castor::exception::Internal ie;

    ie.getMessage() << __PRETTY_FUNCTION__
      << ": Mismatch between the expected total length of the "
         "message header and the actual number of bytes marshalled"
         ": Expected: " << totalLen
      << ": Marshalled: " << nbBytesMarshalled;

    throw ie;
  }

  return totalLen;
}


//-----------------------------------------------------------------------------
// unmarshallMessageHeader
//-----------------------------------------------------------------------------
void castor::tape::aggregator::Marshaller::unmarshallMessageHeader(
  const char * &src, size_t &srcLen, MessageHeader &dst)
  throw(castor::exception::Exception) {

  unmarshallUint32(src, srcLen, dst.magic);
311
  unmarshallUint32(src, srcLen, dst.reqType);
312
313
314
315
316
  unmarshallUint32(src, srcLen, dst.len);
}


//-----------------------------------------------------------------------------
317
// marshallRcpJobRequestMsgBody
318
//-----------------------------------------------------------------------------
319
320
size_t castor::tape::aggregator::Marshaller::marshallRcpJobRequestMsgBody(
  char *const dst, const size_t dstLen, const RcpJobRequestMsgBody &src)
321
  throw(castor::exception::Exception) {
322
323
324
325

  if(dst == NULL) {
    castor::exception::Exception ex(EINVAL);

326
327
    ex.getMessage() << __PRETTY_FUNCTION__
      << ": Pointer to destination buffer is NULL";
328
329
330
331
332
    throw ex;
  }

  // Calculate the length of the message body
  const uint32_t len =
333
334
335
336
337
    4*sizeof(uint32_t)          +
    strlen(src.clientHost)      +
    strlen(src.deviceGroupName) +
    strlen(src.driveName)       +
    strlen(src.clientUserName)  +
338
339
340
    4; // 4 = the number of string termination characters

  // Calculate the total length of the message (header + body)
341
  // Message header = magic + reqType + len = 3 * sizeof(uint32_t)
342
343
344
  const size_t totalLen = 3 * sizeof(uint32_t) + len;

  // Check that the message buffer is big enough
345
  if(totalLen > dstLen) {
346
347
    castor::exception::Exception ex(EMSGSIZE);

348
    ex.getMessage() << __PRETTY_FUNCTION__
349
350
351
      << ": Buffer too small for job submission request message"
         ": Required size: " << totalLen
      << ": Actual size: " << dstLen;
352
353
354
355
356
357

    throw ex;
  }

  // Marshall the whole message (header + body)
  char *p = dst;
358
359
360
  marshallUint32(RTCOPY_MAGIC_OLD0  , p); // Magic number
  marshallUint32(VDQM_CLIENTINFO    , p); // Request type
  marshallUint32(len                , p); // Length of message body
361
  marshallUint32(src.tapeRequestId  , p);
362
363
364
365
366
367
368
  marshallUint32(src.clientPort     , p);
  marshallUint32(src.clientEuid     , p);
  marshallUint32(src.clientEgid     , p);
  marshallString(src.clientHost     , p);
  marshallString(src.deviceGroupName, p);
  marshallString(src.driveName      , p);
  marshallString(src.clientUserName , p);
369
370
371
372
373
374
375
376

  // Calculate the number of bytes actually marshalled
  const size_t nbBytesMarshalled = p - dst;

  // Check that the number of bytes marshalled was what was expected
  if(totalLen != nbBytesMarshalled) {
    castor::exception::Internal ie;

377
378
    ie.getMessage() << __PRETTY_FUNCTION__
      << ": Mismatch between the expected total length of the "
379
380
381
382
         "RCP job submission request message and the actual number of bytes "
         "marshalled"
         ": Expected: " << totalLen
      << ": Marshalled: " << nbBytesMarshalled;
383
384
385
386
387
388
389
390

    throw ie;
  }

  return totalLen;
}


391
//-----------------------------------------------------------------------------
392
// unmarshallRcpJobRequestMsgBodyBody
393
//-----------------------------------------------------------------------------
394
395
void castor::tape::aggregator::Marshaller::unmarshallRcpJobRequestMsgBodyBody(
  const char * &src, size_t &srcLen, RcpJobRequestMsgBody &dst)
396
397
  throw(castor::exception::Exception) {

398
  unmarshallUint32(src, srcLen, dst.tapeRequestId);
399
400
401
  unmarshallUint32(src, srcLen, dst.clientPort);
  unmarshallUint32(src, srcLen, dst.clientEuid);
  unmarshallUint32(src, srcLen, dst.clientEgid);
402
403
404
405
  unmarshallString(src, srcLen, dst.clientHost);
  unmarshallString(src, srcLen, dst.deviceGroupName);
  unmarshallString(src, srcLen, dst.driveName);
  unmarshallString(src, srcLen, dst.clientUserName);
406
407
408
}


409
//-----------------------------------------------------------------------------
410
// marshallRcpJobReplyMsgBody
411
//-----------------------------------------------------------------------------
412
413
size_t castor::tape::aggregator::Marshaller::marshallRcpJobReplyMsgBody(
  char *const dst, const size_t dstLen, const RcpJobReplyMsgBody &src)
414
415
  throw(castor::exception::Exception) {

416
417
418
  if(dst == NULL) {
    castor::exception::Exception ex(EINVAL);

419
420
    ex.getMessage() << __PRETTY_FUNCTION__
      << ": Pointer to destination buffer is NULL";
421
422
423
424
    throw ex;
  }

  // Minimum buffer length = magic number + request type + length + status code
425
426
427
  // + terminating null character
  const size_t minimumLen = 4*sizeof(uint32_t) + 1;
  if(dstLen < minimumLen) {
428
429
    castor::exception::Exception ex(EINVAL);

430
    ex.getMessage() << __PRETTY_FUNCTION__
431
432
433
      << ": Destination buffer length is too small"
         ": Expected minimum: " << minimumLen
      << ": Actual: " << dstLen;
434
435
436
    throw ex;
  }

437
438
439
  // Calculate the length of the error message to be sent taking into account
  // the fact that the error message should be right trimmed if it is too large
  // to fit in the destination buffer.
440
  const size_t headerLen = 3 * sizeof(uint32_t); // magic + reqType + len
441
442
443
444
  // free space = total - header - status - terminating null character
  const size_t freeSpace = dstLen - headerLen - sizeof(uint32_t) - 1;
  const size_t errLenToSend = strlen(src.errorMessage) < freeSpace ?
    strlen(src.errorMessage) : freeSpace;
445

446
447
448
  // Calculate the length of the message body
  // Message body = status + error message + terminating null character
  const uint32_t len = sizeof(src.status) + errLenToSend + 1;
449

450
451
  // Calculate the total length of the message (header + body)
  const size_t totalLen = headerLen + len;
452

453
454
455
456
  char *p = dst;
  marshallUint32(RTCOPY_MAGIC_OLD0, p); // Magic number
  marshallUint32(VDQM_CLIENTINFO  , p); // Request type
  marshallUint32(len              , p); // Length
457
  marshallUint32(src.status       , p); // status code
458

459
460
461
  strncpy(p, src.errorMessage, errLenToSend);
  *(p+errLenToSend) = '0';
  p = p + errLenToSend + 1;
462

463
464
465
466
467
468
469
  // Calculate the number of bytes actually marshalled
  const size_t nbBytesMarshalled = p - dst;

  // Check that the number of bytes marshalled was what was expected
  if(totalLen != nbBytesMarshalled) {
    castor::exception::Internal ie;

470
471
    ie.getMessage() << __PRETTY_FUNCTION__
      << ": Mismatch between the expected total length of the "
472
473
474
         "RCP job reply message and the actual number of bytes marshalled"
         ": Expected: " << totalLen
      << ": Marshalled: " << nbBytesMarshalled;
475
476
477
478
479

    throw ie;
  }

  return totalLen;
480
481
482
483
}


//-----------------------------------------------------------------------------
484
// unmarshallRcpJobReplyMsgBodyBody
485
//-----------------------------------------------------------------------------
486
487
void castor::tape::aggregator::Marshaller::unmarshallRcpJobReplyMsgBodyBody(
  const char * &src, size_t &srcLen, RcpJobReplyMsgBody &dst)
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
  throw(castor::exception::Exception) {

  // Unmarshall the status number
  Marshaller::unmarshallUint32(src, srcLen, dst.status);

  // The error message will be right trimmed if it is too large
  const size_t maxBytesToUnmarshall = srcLen < sizeof(dst.errorMessage) ?
    srcLen : sizeof(dst.errorMessage);

  // Unmarshall the error message
  if(maxBytesToUnmarshall > 0) {
    strncpy(dst.errorMessage, src, maxBytesToUnmarshall);
    dst.errorMessage[maxBytesToUnmarshall - 1] = '\0';
    src    = src + strlen(dst.errorMessage) + 1;
    srcLen = srcLen - strlen(dst.errorMessage) - 1;
  } else {
    dst.errorMessage[0] = '\0';
    // No need to update src or srcLen
  }
}


//-----------------------------------------------------------------------------
511
// marshallRtcpTapeRequestMsgBody
512
//-----------------------------------------------------------------------------
513
514
size_t castor::tape::aggregator::Marshaller::marshallRtcpTapeRequestMsgBody(
  char *dst, const size_t dstLen, const RtcpTapeRequestMsgBody &src)
515
  throw(castor::exception::Exception) {
516
517

  // Calculate the length of the message body
518
519
520
521
522
523
524
525
526
  const uint32_t len =
    14 * sizeof(uint32_t)     +
    strlen(src.vid)           +
    strlen(src.vsn)           +
    strlen(src.label)         +
    strlen(src.devtype)       +
    strlen(src.density)       +
    strlen(src.unit)          +
    sizeof(Cuuid_t)           +
527
    4 * sizeof(uint32_t)      + // 4 uint32_t's of RtcpErrorAppendix
528
529
    strlen(src.err.errmsgtxt) +
    7;                          // 7 = number of null terminator characters
530

531
  // Calculate the total length of the message (header + body)
532
  // Message header = magic + reqType + len = 3 * sizeof(uint32_t)
533
534
535
  const size_t totalLen = 3 * sizeof(uint32_t) + len;

  // Check that the message buffer is big enough
536
  if(totalLen > dstLen) {
537
538
    castor::exception::Exception ex(EMSGSIZE);

539
540
    ex.getMessage() << __PRETTY_FUNCTION__
      << ": Buffer too small for tape request message: "
541
542
543
544
545
      "Required size: " << totalLen << " Actual size: " << dstLen;

    throw ex;
  }

546
  // Marshall the whole message (header + body)
547
  char *p = dst;
548
549
550
551
552
553
554
555
556
  marshallUint32(RTCOPY_MAGIC                          , p);
  marshallUint32(RTCP_TAPEERR_REQ                      , p);
  marshallUint32(len                                   , p);
  marshallString(src.vid                               , p);
  marshallString(src.vsn                               , p);
  marshallString(src.label                             , p);
  marshallString(src.devtype                           , p);
  marshallString(src.density                           , p);
  marshallString(src.unit                              , p);
557
558
  marshallUint32(src.volReqId                          , p);
  marshallUint32(src.jobId                             , p);
559
560
561
562
563
  marshallUint32(src.mode                              , p);
  marshallUint32(src.start_file                        , p);
  marshallUint32(src.end_file                          , p);
  marshallUint32(src.side                              , p);
  marshallUint32(src.tprc                              , p);
564
565
566
567
568
569
570
  marshallUint32(src.tStartRequest                     , p);
  marshallUint32(src.tEndRequest                       , p);
  marshallUint32(src.tStartRtcpd                       , p);
  marshallUint32(src.tStartMount                       , p);
  marshallUint32(src.tEndMount                         , p);
  marshallUint32(src.tStartUnmount                     , p);
  marshallUint32(src.tEndUnmount                       , p);
571
572
573
574
575
576
577
578
579
580
581
582
583
584
  marshallUint32(src.rtcpReqId.time_low                , p);
  marshallUint16(src.rtcpReqId.time_mid                , p);
  marshallUint16(src.rtcpReqId.time_hi_and_version     , p);
  marshallUint8(src.rtcpReqId.clock_seq_hi_and_reserved, p);
  marshallUint8(src.rtcpReqId.clock_seq_low            , p);
  marshallUint8(src.rtcpReqId.node[0]                  , p);
  marshallUint8(src.rtcpReqId.node[1]                  , p);
  marshallUint8(src.rtcpReqId.node[2]                  , p);
  marshallUint8(src.rtcpReqId.node[3]                  , p);
  marshallUint8(src.rtcpReqId.node[4]                  , p);
  marshallUint8(src.rtcpReqId.node[5]                  , p);
  marshallString(src.err.errmsgtxt                     , p);
  marshallUint32(src.err.severity                      , p);
  marshallUint32(src.err.errorcode                     , p);
585
586
  marshallUint32(src.err.maxTpRetry                   , p);
  marshallUint32(src.err.maxCpRetry                   , p);
587

588
589
590
591
592
593
594
  // Calculate the number of bytes actually marshalled
  const size_t nbBytesMarshalled = p - dst;

  // Check that the number of bytes marshalled was what was expected
  if(totalLen != nbBytesMarshalled) {
    castor::exception::Internal ie;

595
596
    ie.getMessage() << __PRETTY_FUNCTION__
      << ": Mismatch between the expected total length of the "
597
598
599
         "RTCP tape request message and the actual number of bytes marshalled"
         ": Expected: " << totalLen
      << ": Marshalled: " << nbBytesMarshalled;
600
601
602
603

    throw ie;
  }

604
605
  return totalLen;
}
606
607
608


//-----------------------------------------------------------------------------
609
// unmarshallRtcpTapeRequestMsgBodyBody
610
//-----------------------------------------------------------------------------
611
612
void castor::tape::aggregator::Marshaller::unmarshallRtcpTapeRequestMsgBodyBody(
  const char * &src, size_t &srcLen, RtcpTapeRequestMsgBody &dst)
613
614
  throw(castor::exception::Exception) {

615
616
617
618
619
620
  unmarshallString(src, srcLen, dst.vid);
  unmarshallString(src, srcLen, dst.vsn);
  unmarshallString(src, srcLen, dst.label);
  unmarshallString(src, srcLen, dst.devtype);
  unmarshallString(src, srcLen, dst.density);
  unmarshallString(src, srcLen, dst.unit);
621
622
  unmarshallUint32(src, srcLen, dst.volReqId);
  unmarshallUint32(src, srcLen, dst.jobId);
623
624
625
626
627
  unmarshallUint32(src, srcLen, dst.mode);
  unmarshallUint32(src, srcLen, dst.start_file);
  unmarshallUint32(src, srcLen, dst.end_file);
  unmarshallUint32(src, srcLen, dst.side);
  unmarshallUint32(src, srcLen, dst.tprc);
628
629
630
631
632
633
634
  unmarshallUint32(src, srcLen, dst.tStartRequest);
  unmarshallUint32(src, srcLen, dst.tEndRequest);
  unmarshallUint32(src, srcLen, dst.tStartRtcpd);
  unmarshallUint32(src, srcLen, dst.tStartMount);
  unmarshallUint32(src, srcLen, dst.tEndMount);
  unmarshallUint32(src, srcLen, dst.tStartUnmount);
  unmarshallUint32(src, srcLen, dst.tEndUnmount);
635
636
637
638
639
640
641
642
643
644
645
646
647
648
  unmarshallUint32(src, srcLen, dst.rtcpReqId.time_low);
  unmarshallUint16(src, srcLen, dst.rtcpReqId.time_mid);
  unmarshallUint16(src, srcLen, dst.rtcpReqId.time_hi_and_version);
  unmarshallUint8(src, srcLen, dst.rtcpReqId.clock_seq_hi_and_reserved);
  unmarshallUint8(src, srcLen, dst.rtcpReqId.clock_seq_low);
  unmarshallUint8(src, srcLen, dst.rtcpReqId.node[0]);
  unmarshallUint8(src, srcLen, dst.rtcpReqId.node[1]);
  unmarshallUint8(src, srcLen, dst.rtcpReqId.node[2]);
  unmarshallUint8(src, srcLen, dst.rtcpReqId.node[3]);
  unmarshallUint8(src, srcLen, dst.rtcpReqId.node[4]);
  unmarshallUint8(src, srcLen, dst.rtcpReqId.node[5]);
  unmarshallString(src, srcLen, dst.err.errmsgtxt);
  unmarshallUint32(src, srcLen, dst.err.severity);
  unmarshallUint32(src, srcLen, dst.err.errorcode);
649
650
  unmarshallInt32(src, srcLen, dst.err.maxTpRetry);
  unmarshallInt32(src, srcLen, dst.err.maxCpRetry);
651
}
652
653
654


//-----------------------------------------------------------------------------
655
// marshallRtcpAcknowledgeMsgBody
656
//-----------------------------------------------------------------------------
657
658
size_t castor::tape::aggregator::Marshaller::marshallRtcpAcknowledgeMsgBody(
  char *const dst, const size_t dstLen, const RtcpAcknowledgeMsgBody &src)
659
660
661
662
663
664
665
666
667
668
669
670
671
  throw(castor::exception::Exception) {
  castor::exception::Internal ie;

  if(dst == NULL) {
    castor::exception::Exception ex(EINVAL);

    ex.getMessage() << __PRETTY_FUNCTION__
      << ": Pointer to destination buffer is NULL";
    throw ex;
  }

  // Calculate the total length of the message (there is no separate header and
  // body)
672
  const uint32_t totalLen = 3 * sizeof(uint32_t); // magic + reqType + status
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688

  // Check that the message buffer is big enough
  if(totalLen > dstLen) {
    castor::exception::Exception ex(EMSGSIZE);

    ex.getMessage() << __PRETTY_FUNCTION__
      << ": Buffer too small for RTCP acknowledge message"
         ": Required size: " << totalLen
      << ": Actual size: " << dstLen;

    throw ex;
  }

  // Marshall the message
  char *p = dst;
  marshallUint32(src.magic  , p);
689
  marshallUint32(src.reqType, p);
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
  marshallUint32(src.status , p);

  // Calculate the number of bytes actually marshalled
  const size_t nbBytesMarshalled = p - dst;

  // Check that the number of bytes marshalled was what was expected
  if(totalLen != nbBytesMarshalled) {
    castor::exception::Internal ie;

    ie.getMessage() << __PRETTY_FUNCTION__
      << ": Mismatch between the expected total length of the "
         "RTCP acknowledge message and the actual number of bytes "
         "marshalled"
         ": Expected: " << totalLen
      << ": Marshalled: " << nbBytesMarshalled;

    throw ie;
  }

  return totalLen;
}


//-----------------------------------------------------------------------------
714
// unmarshallRtcpAcknowledgeMsgBodyBody
715
//-----------------------------------------------------------------------------
716
717
void castor::tape::aggregator::Marshaller::unmarshallRtcpAcknowledgeMsgBodyBody(
  const char * &src, size_t &srcLen, RtcpAcknowledgeMsgBody &dst)
718
719
720
  throw(castor::exception::Exception) {

  unmarshallUint32(src, srcLen, dst.magic);
721
  unmarshallUint32(src, srcLen, dst.reqType);
722
723
  unmarshallUint32(src, srcLen, dst.status);
}
724
725
726


//-----------------------------------------------------------------------------
727
// marshallRtcpFileRequestMsgBody
728
//-----------------------------------------------------------------------------
729
730
size_t castor::tape::aggregator::Marshaller::marshallRtcpFileRequestMsgBody(
  char *dst, const size_t dstLen, const RtcpFileRequestMsgBody &src)
731
  throw(castor::exception::Exception) {
732
733
734

  // Calculate the length of the message body
  const uint32_t len =
735
    // Fields before segAttr
736
737
738
739
740
741
    strlen(src.filePath)  +
    strlen(src.tapePath)  +
    strlen(src.recfm)     +
    strlen(src.fid)       +
    strlen(src.ifce)      +
    strlen(src.stageId)   +
742
    24 * sizeof(uint32_t) + // Number of 32-bit integers before segAttr
Steven Murray's avatar
Steven Murray committed
743
    4                     + // 4 = blockId[4]
744
745
746
747
    8 * sizeof(uint64_t)  + // Number of 64-bit integers before segAttr
    6                     + // Number of string terminators before segAttr

    // segAttr
748
749
750
751
    strlen(src.segAttr.nameServerHostName) +
    strlen(src.segAttr.segmCksumAlgorithm) +
    sizeof(uint32_t)          + // 1 uint32_t of RtcpSegmentAttributes
    sizeof(uint64_t)          + // 1 uint64_t of RtcpSegmentAttributes
752
753
754
    2                         + // Number of segAttr string terminators

    // stgRedId
755
    sizeof(Cuuid_t)           +
756
757

    // err
758
    4 * sizeof(uint32_t)      + // 4 uint32_t's of RtcpErrorAppendix
759
    strlen(src.err.errmsgtxt) +
760
    1;                          // err.errmsgtxt string terminator
761
762

  // Calculate the total length of the message (header + body)
763
  // Message header = magic + reqType + len = 3 * sizeof(uint32_t)
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
  const size_t totalLen = 3 * sizeof(uint32_t) + len;

  // Check that the message buffer is big enough
  if(totalLen > dstLen) {
    castor::exception::Exception ex(EMSGSIZE);

    ex.getMessage() << __PRETTY_FUNCTION__
      << ": Buffer too small for file request message: "
      "Required size: " << totalLen << " Actual size: " << dstLen;

    throw ex;
  }

  // Marshall the whole message (header + body)
  char *p = dst;

  marshallUint32(RTCOPY_MAGIC                         , p);
  marshallUint32(RTCP_FILEERR_REQ                     , p);
  marshallUint32(len                                  , p);
  marshallString(src.filePath                         , p);
  marshallString(src.tapePath                         , p);
  marshallString(src.recfm                            , p);
  marshallString(src.fid                              , p);
  marshallString(src.ifce                             , p);
  marshallString(src.stageId                          , p);
789
790
791
  marshallUint32(src.volReqId                         , p);
  marshallUint32(src.jobId                            , p);
  marshallUint32(src.stageSubReqId                    , p);
792
  marshallUint32(src.umask                            , p);
793
  marshallUint32(src.positionMethod                   , p);
794
795
  marshallUint32(src.tapeFseq                         , p);
  marshallUint32(src.diskFseq                         , p);
796
  marshallUint32(src.blockSize                        , p);
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
  marshallUint32(src.recordLength                     , p);
  marshallUint32(src.retention                        , p);
  marshallUint32(src.defAlloc                         , p);
  marshallUint32(src.rtcpErrAction                    , p);
  marshallUint32(src.tpErrAction                      , p);
  marshallUint32(src.convert                          , p);
  marshallUint32(src.checkFid                         , p);
  marshallUint32(src.concat                           , p);
  marshallUint32(src.procStatus                       , p);
  marshallUint32(src.cprc                             , p);
  marshallUint32(src.tStartPosition                   , p);
  marshallUint32(src.tEndPosition                     , p);
  marshallUint32(src.tStartTransferDisk               , p);
  marshallUint32(src.tEndTransferDisk                 , p);
  marshallUint32(src.tStartTransferTape               , p);
  marshallUint32(src.tEndTransferTape                 , p);
  marshallUint8(src.blockId[0]                        , p);
  marshallUint8(src.blockId[1]                        , p);
  marshallUint8(src.blockId[2]                        , p);
  marshallUint8(src.blockId[3]                        , p);
  marshallUint64(src.offset                           , p);
  marshallUint64(src.bytesIn                          , p);
  marshallUint64(src.bytesOut                         , p);
  marshallUint64(src.hostBytes                        , p);
  marshallUint64(src.nbRecs                           , p);
  marshallUint64(src.maxNbRec                         , p);
  marshallUint64(src.maxSize                          , p);
  marshallUint64(src.startSize                        , p);
  marshallString(src.segAttr.nameServerHostName       , p);
  marshallString(src.segAttr.segmCksumAlgorithm       , p);
  marshallUint32(src.segAttr.segmCksum                , p);
  marshallUint64(src.segAttr.castorFileId             , p);
  marshallUint32(src.stgReqId.time_low                , p);
  marshallUint16(src.stgReqId.time_mid                , p);
  marshallUint16(src.stgReqId.time_hi_and_version     , p);
  marshallUint8(src.stgReqId.clock_seq_hi_and_reserved, p);
  marshallUint8(src.stgReqId.clock_seq_low            , p);
  marshallUint8(src.stgReqId.node[0]                  , p);
  marshallUint8(src.stgReqId.node[1]                  , p);
  marshallUint8(src.stgReqId.node[2]                  , p);
  marshallUint8(src.stgReqId.node[3]                  , p);
  marshallUint8(src.stgReqId.node[4]                  , p);
  marshallUint8(src.stgReqId.node[5]                  , p);
  marshallString(src.err.errmsgtxt                    , p);
  marshallUint32(src.err.severity                     , p);
  marshallUint32(src.err.errorcode                    , p);
Steven Murray's avatar
Steven Murray committed
843
844
  marshallUint32(src.err.maxTpRetry                   , p);
  marshallUint32(src.err.maxCpRetry                   , p);
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866

  // Calculate the number of bytes actually marshalled
  const size_t nbBytesMarshalled = p - dst;

  // Check that the number of bytes marshalled was what was expected
  if(totalLen != nbBytesMarshalled) {
    castor::exception::Internal ie;

    ie.getMessage() << __PRETTY_FUNCTION__
      << ": Mismatch between the expected total length of the "
         "RTCP file request message and the actual number of bytes marshalled"
         ": Expected: " << totalLen
      << ": Marshalled: " << nbBytesMarshalled;

    throw ie;
  }

  return totalLen;
}


//-----------------------------------------------------------------------------
867
// unmarshallRtcpFileRequestMsgBodyBody
868
//-----------------------------------------------------------------------------
869
870
void castor::tape::aggregator::Marshaller::unmarshallRtcpFileRequestMsgBodyBody(
  const char * &src, size_t &srcLen, RtcpFileRequestMsgBody &dst)
871
872
873
874
875
876
877
878
  throw(castor::exception::Exception) {

  unmarshallString(src, srcLen, dst.filePath);
  unmarshallString(src, srcLen, dst.tapePath);
  unmarshallString(src, srcLen, dst.recfm);
  unmarshallString(src, srcLen, dst.fid);
  unmarshallString(src, srcLen, dst.ifce);
  unmarshallString(src, srcLen, dst.stageId);
879
880
881
  unmarshallUint32(src, srcLen, dst.volReqId);
  unmarshallInt32(src, srcLen, dst.jobId);
  unmarshallInt32(src, srcLen, dst.stageSubReqId);
882
  unmarshallUint32(src, srcLen, dst.umask);
883
  unmarshallUint32(src, srcLen, dst.positionMethod);
884
885
  unmarshallUint32(src, srcLen, dst.tapeFseq);
  unmarshallUint32(src, srcLen, dst.diskFseq);
886
887
888
889
890
891
892
893
  unmarshallInt32(src, srcLen, dst.blockSize);
  unmarshallInt32(src, srcLen, dst.recordLength);
  unmarshallInt32(src, srcLen, dst.retention);
  unmarshallInt32(src, srcLen, dst.defAlloc);
  unmarshallInt32(src, srcLen, dst.rtcpErrAction);
  unmarshallInt32(src, srcLen, dst.tpErrAction);
  unmarshallInt32(src, srcLen, dst.convert);
  unmarshallInt32(src, srcLen, dst.checkFid);
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
  unmarshallUint32(src, srcLen, dst.concat);
  unmarshallUint32(src, srcLen, dst.procStatus);
  unmarshallUint32(src, srcLen, dst.cprc);
  unmarshallUint32(src, srcLen, dst.tStartPosition);
  unmarshallUint32(src, srcLen, dst.tEndPosition);
  unmarshallUint32(src, srcLen, dst.tStartTransferDisk);
  unmarshallUint32(src, srcLen, dst.tEndTransferDisk);
  unmarshallUint32(src, srcLen, dst.tStartTransferTape);
  unmarshallUint32(src, srcLen, dst.tEndTransferTape);
  unmarshallUint8(src, srcLen, dst.blockId[0]);
  unmarshallUint8(src, srcLen, dst.blockId[1]);
  unmarshallUint8(src, srcLen, dst.blockId[2]);
  unmarshallUint8(src, srcLen, dst.blockId[3]);
  unmarshallUint64(src, srcLen, dst.offset);
  unmarshallUint64(src, srcLen, dst.bytesIn);
  unmarshallUint64(src, srcLen, dst.bytesOut);
  unmarshallUint64(src, srcLen, dst.hostBytes);
  unmarshallUint64(src, srcLen, dst.nbRecs);
  unmarshallUint64(src, srcLen, dst.maxNbRec);
  unmarshallUint64(src, srcLen, dst.maxSize);
  unmarshallUint64(src, srcLen, dst.startSize);
  unmarshallString(src, srcLen, dst.segAttr.nameServerHostName);
  unmarshallString(src, srcLen, dst.segAttr.segmCksumAlgorithm);
  unmarshallUint32(src, srcLen, dst.segAttr.segmCksum);
  unmarshallUint64(src, srcLen, dst.segAttr.castorFileId);
  unmarshallUint32(src, srcLen, dst.stgReqId.time_low);
  unmarshallUint16(src, srcLen, dst.stgReqId.time_mid);
  unmarshallUint16(src, srcLen, dst.stgReqId.time_hi_and_version);
  unmarshallUint8(src, srcLen, dst.stgReqId.clock_seq_hi_and_reserved);
  unmarshallUint8(src, srcLen, dst.stgReqId.clock_seq_low);
  unmarshallUint8(src, srcLen, dst.stgReqId.node[0]);
  unmarshallUint8(src, srcLen, dst.stgReqId.node[1]);
  unmarshallUint8(src, srcLen, dst.stgReqId.node[2]);
  unmarshallUint8(src, srcLen, dst.stgReqId.node[3]);
  unmarshallUint8(src, srcLen, dst.stgReqId.node[4]);
  unmarshallUint8(src, srcLen, dst.stgReqId.node[5]);
  unmarshallString(src, srcLen, dst.err.errmsgtxt);
  unmarshallUint32(src, srcLen, dst.err.severity);
  unmarshallUint32(src, srcLen, dst.err.errorcode);
933
934
  unmarshallInt32(src, srcLen, dst.err.maxTpRetry);
  unmarshallInt32(src, srcLen, dst.err.maxCpRetry);
935
}
Steven Murray's avatar
Steven Murray committed
936
937
938
939
940
941
942


//-----------------------------------------------------------------------------
// marshallRtcpNoMoreRequestsMessage
//-----------------------------------------------------------------------------
size_t castor::tape::aggregator::Marshaller::marshallRtcpNoMoreRequestsMessage(
  char *dst, const size_t dstLen)
943
  throw(castor::exception::Exception) {
Steven Murray's avatar
Steven Murray committed
944
945
946
947
948

  // Calculate the length of the message body
  const uint32_t len = 0;

  // Calculate the total length of the message (header + body)
949
  // Message header = magic + reqType + len = 3 * sizeof(uint32_t)
Steven Murray's avatar
Steven Murray committed
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
  const size_t totalLen = 3 * sizeof(uint32_t) + len;

  // Check that the message buffer is big enough
  if(totalLen > dstLen) {
    castor::exception::Exception ex(EMSGSIZE);

    ex.getMessage() << __PRETTY_FUNCTION__
      << ": Buffer too small for file request message: "
      "Required size: " << totalLen << " Actual size: " << dstLen;

    throw ex;
  }

  // Marshall the whole message (header + body)
  char *p = dst;
  marshallUint32(RTCOPY_MAGIC   , p); // Magic number
  marshallUint32(RTCP_NOMORE_REQ, p); // Request type
  marshallUint32(len            , p); // Length of message body

  // Calculate the number of bytes actually marshalled
  const size_t nbBytesMarshalled = p - dst;

  // Check that the number of bytes marshalled was what was expected
  if(totalLen != nbBytesMarshalled) {
    castor::exception::Internal ie;

    ie.getMessage() << __PRETTY_FUNCTION__
      << ": Mismatch between the expected total length of the "
         "RTCP no more requests message and the actual number of bytes "
         "marshalled"
         ": Expected: " << totalLen
      << ": Marshalled: " << nbBytesMarshalled;

    throw ie;
  }

  return totalLen;
}