Marshaller.cpp 11.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
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/tape/aggregator/Marshaller.hpp"
27
28
#include "h/rtcp_constants.h"
#include "h/vdqm_constants.h"
29
30

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


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
//------------------------------------------------------------------------------
// marshallUint8
//------------------------------------------------------------------------------
void castor::tape::aggregator::Marshaller::marshallUint8(uint8_t src,
  char * &dst) throw (castor::exception::Exception) {

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

    ex.getMessage() << "Pointer to destination buffer is NULL";
    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);

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

  src = htonl(src);

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

  dst += sizeof(src);
}


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

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

87
88
89
    ex.getMessage() << "Pointer to destination buffer is NULL";
    throw ex;
  }
90

91
92
93
94
95
  src = htonl(src);

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

  dst += sizeof(src);
96
97
98
}


99
//------------------------------------------------------------------------------
100
// unmarshallInt32
101
//------------------------------------------------------------------------------
102
103
104
105
106
void castor::tape::aggregator::Marshaller::unmarshallUint32(char * &src,
  size_t &srcLen, uint32_t &dst) throw(castor::exception::Exception) {

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

108
109
110
    ex.getMessage() << "Pointer to source buffer is NULL";
    throw ex;
  }
111

112
113
114
115
116
117
118
119
120
121
122
123
124
  if(srcLen < sizeof(dst)) {
    castor::exception::Exception ex(EINVAL);

    ex.getMessage() << "Source buffer length is too small: Expected length: "
      << sizeof(dst) << ": Actual length: " << srcLen;
    throw ex;
  }

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

  src    += sizeof(dst);
  srcLen -= sizeof(dst);
125
126
127
}


128
129
130
//------------------------------------------------------------------------------
// marshallString
//------------------------------------------------------------------------------
131
132
133
134
135
136
137
138
139
void castor::tape::aggregator::Marshaller::marshallString(const char * src,
  char * &dst) throw(castor::exception::Exception) {

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

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

141
  strcpy(dst, src);
142

143
  dst += strlen(src) + 1;
144
145
146
}


147
//------------------------------------------------------------------------------
148
// marshallString
149
//------------------------------------------------------------------------------
150
151
152
153
154
void castor::tape::aggregator::Marshaller::marshallString(
  const std::string &src, char * &dst) throw(castor::exception::Exception) {

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

156
157
158
    ex.getMessage() << "Pointer to destination buffer is NULL";
    throw ex;
  }
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
  strcpy(dst, src.c_str());

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


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

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

    ex.getMessage() << "Pointer to source buffer is NULL";
    throw ex;
  }

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

    ex.getMessage() << "Source buffer length is 0";
    throw ex;
  }

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

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

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

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

    ex.getMessage() << "String terminator of source buffer was not reached";
    throw ex;
  }
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


//-----------------------------------------------------------------------------
// marshallRTCPAckn
//-----------------------------------------------------------------------------
size_t castor::tape::aggregator::Marshaller::marshallRTCPAckn(
  char *dst, const size_t dstLen, const uint32_t status, const char *errorMsg)
  throw (castor::exception::Exception) {

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

    ex.getMessage() << "Pointer to destination buffer is NULL";
    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);

    ex.getMessage() << "Destination buffer length is too small: "
      "Expected minimum length: " << (4*sizeof(uint32_t) + 1)
       << ": Actual length: " << dstLen;
    throw ex;
  }

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

    ex.getMessage() << "Pointer to error message is NULL";
    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;

  marshallUint32(RTCOPY_MAGIC_OLD0, dst); // Magic number
  marshallUint32(VDQM_CLIENTINFO  , dst); // Request type
  marshallUint32(len              , dst); // Length
  marshallUint32(status           , dst); // status code

  memcpy(dst, errorMsg, errorMsg2SendLen);
  *(dst+errorMsg2SendLen) = '\0';

  return 3 * sizeof(uint32_t) + len; // Header + body
}


//-----------------------------------------------------------------------------
// marshallRTCPTapeRequest
//-----------------------------------------------------------------------------
size_t castor::tape::aggregator::Marshaller::marshallRTCPTapeRequest(char *dst,
  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;

  // Calculate the total length of the message
  // 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
  if(sizeof(dstLen) <  totalLen) {
    castor::exception::Exception ex(EMSGSIZE);

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

    throw ex;
  }

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

  return totalLen;
}