Marshaller.cpp 17.6 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
23
24
25
/******************************************************************************
 *                      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.
 *
 *
 *
 *
 * @author Steven Murray Steven.Murray@cern.ch
 *****************************************************************************/

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

#include <arpa/inet.h>
32
33
#include <errno.h>
#include <iostream>
34
35
36
#include <string.h>


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

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

46
47
    ex.getMessage() << __PRETTY_FUNCTION__
      << ": Pointer to destination buffer is NULL";
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
    throw ex;
  }

  src = htonl(src);

  memcpy(dst, &src, sizeof(src));

  dst += sizeof(src);
}


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

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

68
69
    ex.getMessage() << __PRETTY_FUNCTION__
      << ": Pointer to destination buffer is NULL";
70
71
72
73
74
75
76
77
78
79
80
    throw ex;
  }

  src = htonl(src);

  memcpy(dst, &src, sizeof(src));

  dst += sizeof(src);
}


81
82
83
//------------------------------------------------------------------------------
// marshallUint32
//------------------------------------------------------------------------------
84
85
void castor::tape::aggregator::Marshaller::marshallUint32(uint32_t src,
  char * &dst) throw (castor::exception::Exception) {
86

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

90
91
    ex.getMessage() << __PRETTY_FUNCTION__
      << ": Pointer to destination buffer is NULL";
92
93
    throw ex;
  }
94

95
96
97
98
99
  src = htonl(src);

  memcpy(dst, &src, sizeof(src));

  dst += sizeof(src);
100
101
102
}


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

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

112
113
    ex.getMessage() << __PRETTY_FUNCTION__
      << ": Pointer to source buffer is NULL";
114
115
    throw ex;
  }
116

117
118
119
  if(srcLen < sizeof(dst)) {
    castor::exception::Exception ex(EINVAL);

120
121
    ex.getMessage() << __PRETTY_FUNCTION__
      << ": Source buffer length is too small: Expected length: "
122
123
124
125
126
127
128
129
130
      << sizeof(dst) << ": Actual length: " << srcLen;
    throw ex;
  }

  memcpy(&dst, src, sizeof(dst));
  dst = ntohl(dst);

  src    += sizeof(dst);
  srcLen -= sizeof(dst);
131
132
133
}


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

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

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

148
  strcpy(dst, src);
149

150
  dst += strlen(src) + 1;
151
152
153
}


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

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

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

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

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


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

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

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

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

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

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

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

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

208
209
    ex.getMessage() << __PRETTY_FUNCTION__
      << ": Destination buffer length is 0";
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
    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);

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


//-----------------------------------------------------------------------------
247
// marshallRcpJobRequest
248
//-----------------------------------------------------------------------------
249
250
251
252
253
254
255
size_t castor::tape::aggregator::Marshaller::marshallRcpJobRequest(
  char *const dst, const size_t dstLen, const RcpJobRequest &request)
  throw (castor::exception::Exception) {

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

256
257
    ex.getMessage() << __PRETTY_FUNCTION__
      << ": Pointer to destination buffer is NULL";
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
    throw ex;
  }

  // Calculate the length of the message body
  const uint32_t len =
    4*sizeof(uint32_t)              +
    strlen(request.clientHost)      +
    strlen(request.deviceGroupName) +
    strlen(request.tapeDriveName)   +
    strlen(request.clientUserName)  +
    4; // 4 = the number of string termination characters

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

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

278
279
    ex.getMessage() << __PRETTY_FUNCTION__
      << ": Buffer too small for job submission request message: "
280
281
282
283
284
285
286
      "Required size: " << totalLen << " Actual size: " << dstLen;

    throw ex;
  }

  // Marshall the whole message (header + body)
  char *p = dst;
287
288
289
290
291
292
293
294
295
296
297
  marshallUint32(RTCOPY_MAGIC_OLD0      , p);
  marshallUint32(VDQM_CLIENTINFO        , p);
  marshallUint32(len                    , p);
  marshallUint32(request.tapeRequestID  , p);
  marshallUint32(request.clientPort     , p);
  marshallUint32(request.clientEuid     , p);
  marshallUint32(request.clientEgid     , p);
  marshallString(request.clientHost     , p);
  marshallString(request.deviceGroupName, p);
  marshallString(request.tapeDriveName  , p);
  marshallString(request.clientUserName , p);
298
299
300
301
302
303
304
305

  // 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;

306
307
    ie.getMessage() << __PRETTY_FUNCTION__
      << ": Mismatch between the expected total length of the "
308
309
310
311
312
313
314
315
316
317
318
      "RCP job submission request message and the actual number of bytes "
      "marshalled: Expected length: " << totalLen << " Marshalled: "
      << nbBytesMarshalled;

    throw ie;
  }

  return totalLen;
}


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
//-----------------------------------------------------------------------------
// unmarshallRcpJobReply
//-----------------------------------------------------------------------------
void castor::tape::aggregator::Marshaller::unmarshallRcpJobReply(
  const char * &src, size_t &srcLen, RcpJobReply &dst)
  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
  }
}


346
347
348
349
350
//-----------------------------------------------------------------------------
// marshallRtcpAckn
//-----------------------------------------------------------------------------
size_t castor::tape::aggregator::Marshaller::marshallRtcpAckn(char *const dst,
  const size_t dstLen, const uint32_t status, const char *errorMsg)
351
352
353
354
355
  throw (castor::exception::Exception) {

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

356
357
    ex.getMessage() << __PRETTY_FUNCTION__
      << ": Pointer to destination buffer is NULL";
358
359
360
361
362
363
364
365
    throw ex;
  }

  // Minimum buffer length = magic number + request type + length + status code
  // + the string termination character '\0'
  if(dstLen < 4*sizeof(uint32_t) + 1) {
    castor::exception::Exception ex(EINVAL);

366
367
    ex.getMessage() << __PRETTY_FUNCTION__
      << ": Destination buffer length is too small: "
368
369
370
371
372
373
374
375
      "Expected minimum length: " << (4*sizeof(uint32_t) + 1)
       << ": Actual length: " << dstLen;
    throw ex;
  }

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

376
377
    ex.getMessage() << __PRETTY_FUNCTION__
      << ": Pointer to error message is NULL";
378
379
380
381
382
383
384
385
386
387
388
389
    throw ex;
  }

  const size_t errorMsgLen      = strlen(errorMsg);
  const size_t maxErrorMsgLen   = dstLen - 4*sizeof(uint32_t) - 1;
  const size_t errorMsg2SendLen = errorMsgLen > maxErrorMsgLen ? maxErrorMsgLen
    : errorMsgLen;

  // Length of message body equals the size fo the status code plus the length
  // of the error message string plus the size of the string termination
  // characater '\0'
  const uint32_t len = sizeof(uint32_t) + errorMsg2SendLen + 1;
390
391
392
393
394
395
 
  char *p = dst;
  marshallUint32(RTCOPY_MAGIC_OLD0, p); // Magic number
  marshallUint32(VDQM_CLIENTINFO  , p); // Request type
  marshallUint32(len              , p); // Length
  marshallUint32(status           , p); // status code
396

397
398
399
  // Marshall the error message without the terminatiing null byte
  memcpy(p, errorMsg, errorMsg2SendLen);
  p += errorMsg2SendLen;
400

401
402
403
  // Marshall the terminatiing null byte of the error message
  *p = '\0';
  p += 1;
404

405
406
407
408
409
410
411
412
413
414
415
  // Calculate the number of bytes actually marshalled
  const size_t nbBytesMarshalled = p - dst;

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

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

416
417
    ie.getMessage() << __PRETTY_FUNCTION__
      << ": Mismatch between the expected total length of the "
418
419
420
421
422
423
424
      "RTCP acknowledge message and the actual number of bytes marshalled: "
      "Expected length: " << totalLen << " Marshalled: " << nbBytesMarshalled;

    throw ie;
  }

  return totalLen;
425
426
427
428
}


//-----------------------------------------------------------------------------
429
// marshallRtcpTapeRequest
430
//-----------------------------------------------------------------------------
431
size_t castor::tape::aggregator::Marshaller::marshallRtcpTapeRequest(char *dst,
432
433
434
435
436
437
438
439
440
441
  const size_t dstLen, const RtcpTapeRequest &request)
  throw (castor::exception::Exception) {

  // Calculate the length of the message body
  const uint32_t len = 14 * sizeof(uint32_t) + strlen(request.vid) +
    strlen(request.vsn) + strlen(request.label) + strlen(request.devtype) +
    strlen(request.density) + strlen(request.unit) + 6 + sizeof(Cuuid_t) +
    // Plus the size of the err member which is of type rtcpErrMsg_t
    4 * sizeof(uint32_t) + strlen(request.err.errmsgtxt) + 1;

442
  // Calculate the total length of the message (header + body)
443
444
445
446
  // Message header = magic + reqtype + len = 3 * sizeof(uint32_t)
  const size_t totalLen = 3 * sizeof(uint32_t) + len;

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

450
451
    ex.getMessage() << __PRETTY_FUNCTION__
      << ": Buffer too small for tape request message: "
452
453
454
455
456
      "Required size: " << totalLen << " Actual size: " << dstLen;

    throw ex;
  }

457
  // Marshall the whole message (header + body)
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
  char *p = dst;
  marshallUint32(RTCOPY_MAGIC                              , p);
  marshallUint32(RTCP_TAPEERR_REQ                          , p);
  marshallUint32(len                                       , p);
  marshallString(request.vid                               , p);
  marshallString(request.vsn                               , p);
  marshallString(request.label                             , p);
  marshallString(request.devtype                           , p);
  marshallString(request.density                           , p);
  marshallString(request.unit                              , p);
  marshallUint32(request.VolReqID                          , p);
  marshallUint32(request.jobID                             , p);
  marshallUint32(request.mode                              , p);
  marshallUint32(request.start_file                        , p);
  marshallUint32(request.end_file                          , p);
  marshallUint32(request.side                              , p);
  marshallUint32(request.tprc                              , p);
  marshallUint32(request.TStartRequest                     , p);
  marshallUint32(request.TEndRequest                       , p);
  marshallUint32(request.TStartRtcpd                       , p);
  marshallUint32(request.TStartMount                       , p);
  marshallUint32(request.TEndMount                         , p);
  marshallUint32(request.TStartUnmount                     , p);
  marshallUint32(request.TEndUnmount                       , p);
  marshallUint32(request.rtcpReqId.time_low                , p);
  marshallUint16(request.rtcpReqId.time_mid                , p);
  marshallUint16(request.rtcpReqId.time_hi_and_version     , p);
  marshallUint8(request.rtcpReqId.clock_seq_hi_and_reserved, p);
  marshallUint8(request.rtcpReqId.clock_seq_low            , p);
  marshallUint8(request.rtcpReqId.node[0]                  , p);
  marshallUint8(request.rtcpReqId.node[1]                  , p);
  marshallUint8(request.rtcpReqId.node[2]                  , p);
  marshallUint8(request.rtcpReqId.node[3]                  , p);
  marshallUint8(request.rtcpReqId.node[4]                  , p);
  marshallUint8(request.rtcpReqId.node[5]                  , p);
  marshallString(request.err.errmsgtxt                     , p);
  marshallUint32(request.err.severity                      , p);
  marshallUint32(request.err.errorcode                     , p);
  marshallUint32(request.err.max_tpretry                   , p);
  marshallUint32(request.err.max_cpretry                   , p);

499
500
501
502
503
504
505
  // 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;

506
507
    ie.getMessage() << __PRETTY_FUNCTION__
      << ": Mismatch between the expected total length of the "
508
509
510
511
512
513
      "RTCP tape request message and the actual number of bytes marshalled: "
      "Expected length: " << totalLen << " Marshalled: " << nbBytesMarshalled;

    throw ie;
  }

514
515
  return totalLen;
}
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530


//-----------------------------------------------------------------------------
// unmarshallRtcpTapeRequest
//-----------------------------------------------------------------------------
void castor::tape::aggregator::Marshaller::unmarshallRtcpTapeRequest(
  const char * &src, size_t &srcLen, RtcpTapeRequest &request)
  throw(castor::exception::Exception) {
  castor::exception::Internal ie;

  ie.getMessage() << __PRETTY_FUNCTION__
    << ": Not implemented";

  throw ie;
}