X-TraceC++Library
|
00001 /* 00002 * Copyright (c) 2005,2006,2007 The Regents of the University of California. 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * * Redistributions of source code must retain the above copyright 00008 * notice, this list of conditions and the following disclaimer. 00009 * * Redistributions in binary form must reproduce the above copyright 00010 * notice, this list of conditions and the following disclaimer in the 00011 * documentation and/or other materials provided with the distribution. 00012 * * Neither the name of the University of California, nor the 00013 * names of its contributors may be used to endorse or promote products 00014 * derived from this software without specific prior written permission. 00015 * 00016 * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF CALIFORNIA ``AS IS'' AND ANY 00017 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00018 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00019 * DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE FOR ANY 00020 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00021 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00022 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00023 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00024 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00025 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00026 */ 00027 00028 00029 #include <cstdlib> 00030 #include <cassert> 00031 #include <cstring> 00032 00033 #include "XtrConstants.h" 00034 #include "XtrOption.h" 00035 00036 #ifndef _XTR_METADATA_H 00037 #define _XTR_METADATA_H 00038 00039 namespace xtr { 00040 /*********************************************************/ 00041 00045 template <size_t max_len> class Id 00046 { 00047 public: 00048 Id() : length(0) {}; 00049 Id(size_t _length); 00050 Id(const u_int8_t* from, size_t len); 00051 virtual ~Id() {}; 00052 00053 bool isLengthValid() const {return isValidLength(length);}; 00054 size_t getLength() const {return length;}; 00055 const u_int8_t* getBytes() const {return id;}; 00056 u_int8_t& operator[](size_t i) {assert(i < length); return id[i];}; 00057 virtual bool isEqual(const Id< max_len>& other) const; 00058 00059 void setLength(size_t _length) {assert(_length <= max_len); length = _length;}; 00060 xtr_result setBytes(const u_int8_t* from, size_t len); 00061 00062 xtr_result pack(u_int8_t *dest, size_t *size) const; 00063 00064 char* toString(char *dest, size_t size) const; 00065 virtual bool isValidLength(size_t len) const {return (len <= max_len);}; 00066 protected: 00067 size_t length; 00068 u_int8_t id[max_len]; 00069 }; 00070 00071 00072 template <size_t max_len> 00073 Id< max_len >::Id(size_t _length) 00074 : length(0) 00075 { 00076 //assert(_length <= max_len); 00077 memset(id, 0, sizeof(id)); 00078 if (isValidLength(_length)) { 00079 length = _length; 00080 } 00081 } 00082 00083 template <size_t max_len> 00084 Id< max_len >::Id(const u_int8_t* from, size_t len) 00085 : length(0) 00086 { 00087 assert(from); 00088 //assert(len <= max_len); 00089 if (isValidLength(len)) { 00090 length = len; 00091 memset(id, 0, sizeof(id)); 00092 memcpy(id, from, len); 00093 } 00094 }; 00095 00096 template <size_t max_len> 00097 xtr_result Id<max_len>::setBytes(const u_int8_t* from, size_t len) { 00098 assert(from); 00099 assert(len <= max_len); 00100 if (isValidLength(len)) { 00101 length = len; 00102 memcpy(id, from, len); 00103 return XTR_SUCCESS; 00104 } else 00105 return XTR_FAIL; 00106 } 00107 00108 template <size_t max_len> 00109 xtr_result Id <max_len>::pack(u_int8_t *dest, size_t *size) const 00110 { 00111 assert(dest); 00112 assert(size); 00113 if (*size < length) { 00114 *size = 0; 00115 return XTR_FAIL; 00116 } 00117 memcpy(dest, id, length); 00118 *size = length; 00119 return XTR_SUCCESS; 00120 } 00121 00122 template <size_t max_len> 00123 bool Id< max_len >::isEqual(const Id< max_len>& other) const 00124 { 00125 unsigned int i; 00126 bool equal = 1; 00127 if (length != other.length) 00128 equal = 0; 00129 for (i = 0; i < length && equal; i++) { 00130 equal &= (id[i] == other.id[i]); 00131 } 00132 return equal; 00133 } 00134 00135 void xtr_btoh(u_int8_t b, char*p); 00136 00137 template <size_t max_len> 00138 char* Id< max_len >::toString(char *dest, size_t size) const 00139 { 00140 char* p = dest; 00141 size_t k = 0; 00142 00143 if (!dest) 00144 return dest; 00145 *dest = 0; 00146 if (length == 0) 00147 return dest; 00148 if (size < length*2 + 1) 00149 return dest; 00150 00151 while ( k < length ) { 00152 xtr_btoh(id[k++], p); 00153 p += 2; 00154 } 00155 *p = 0; 00156 return dest; 00157 } 00158 00161 class OpId : public Id<XTR_MAX_OP_ID_LEN> 00162 { 00163 public: 00172 OpId(size_t len = 4); 00173 00182 OpId(const u_int8_t* from, size_t len); 00183 00185 bool isValidLength(size_t len) const { 00186 return (len == 4 || len == 8); 00187 }; 00188 }; 00189 00190 00194 class TaskId : public Id<XTR_MAX_TASK_ID_LEN> 00195 { 00196 public: 00205 TaskId(size_t len = 4); 00214 TaskId(const u_int8_t* from, size_t len); 00215 00217 bool isValid() const; 00218 00220 bool isValidLength(size_t len) const { 00221 return (len == 4 || len == 8 || len == 12 || len == 20); 00222 } 00223 }; 00224 00225 00226 /*********************************************************/ 00227 00232 class Options { 00233 public: 00234 Options() : reserved_count(0), count(0), length(0), opts(0){}; 00235 Options(const u_int8_t *from, u_int8_t len); 00236 00237 Options(const Options&); 00238 00239 ~Options(); 00240 Options& operator=(const Options& other); 00241 00242 size_t getCount() const {return count;}; 00243 size_t getLength() const {return length;}; 00244 00245 bool isEqual(const Options& other) const; 00246 00255 xtr_result reserve(u_int8_t n); 00256 void clear(); 00257 00258 Option& operator[](size_t i) const {assert(i < count); return *opts[i];}; 00259 00260 xtr_result addOption(const Option& option); 00261 00266 xtr_result removeOptionAt(u_int8_t i); 00267 00275 xtr_result pack(u_int8_t *dest, size_t *size) const; 00276 00277 private: 00278 u_int8_t reserved_count; 00279 u_int8_t count; 00280 u_int8_t length; 00281 Option **opts; 00282 }; 00283 00284 00286 class Metadata 00287 { 00288 public: 00299 Metadata(size_t tlen=4, size_t olen=4) : version(XTR_CURRENT_VERSION), 00300 taskId(tlen), opId(olen) , options() 00301 {initRandom();}; 00302 00308 Metadata(const TaskId &id, size_t opIdLen=4) : version(XTR_CURRENT_VERSION), 00309 taskId(), opId() , options() 00310 {initRandom(); setTaskId(id); setRandomOpId(opIdLen);}; 00311 00317 Metadata(const TaskId &id, const OpId &opId) 00318 : version(XTR_CURRENT_VERSION), taskId(), opId(), options() 00319 {initRandom(); setTaskId(id); setOpId(opId); }; 00320 00321 00329 static Metadata createFromString(const char* s, size_t len) 00330 { 00331 return Metadata(CREATE_FROM_STRING, s, len); 00332 } 00333 00337 static Metadata createFromBytes(const u_int8_t* b, size_t len) 00338 { 00339 return Metadata(CREATE_FROM_BYTES, b, len); 00340 } 00341 00354 static Metadata createRandom(size_t taskIdLen = 4, size_t opIdLen = 4) 00355 { 00356 return Metadata(CREATE_RANDOM, taskIdLen, opIdLen); 00357 } 00358 00367 static size_t sizeInArray(const u_int8_t *to, size_t len); 00368 00369 00375 void clear(); 00376 00380 bool isValid() const {return taskId.isValid();}; 00381 00386 bool isEqual(const Metadata& other) const; 00387 00389 const TaskId& getTaskId() const {return taskId;}; 00390 00392 const OpId& getOpId() const {return opId;}; 00393 Options& getOptions() {return options;}; 00394 00398 xtr_result setTaskId(const TaskId &id); 00399 00406 xtr_result setRandomTaskId(size_t len = 4); 00407 00413 xtr_result setOpId(const OpId &id); 00414 00415 00424 xtr_result setRandomOpId(size_t opIdLen = 4); 00425 00426 xtr_result addOption(const Option& opt) {return options.addOption(opt);}; 00427 00428 /* Setting of some important options. These are shortcuts to actually 00429 * creating and adding the options, but come in handy. */ 00430 00431 /* ChainId:*/ 00435 u_int16_t getChainId() const; 00436 00439 xtr_result setChainId(u_int16_t id); 00440 00443 xtr_result newChainId(); 00444 00445 /* Severity Option Convenience Functions */ 00446 00449 u_int8_t getSeverityThreshold() const; 00450 00459 xtr_result setSeverityThreshold(u_int8_t s); 00460 00466 xtr_result unsetSeverityThreshold(); 00467 00468 /* Serialization related functions */ 00470 size_t sizeAsBytes() const; 00471 00482 xtr_result pack(u_int8_t *to, size_t *len) const; 00483 00489 size_t sizeAsString() const {return sizeAsBytes() * 2;}; 00500 char* toString(char* to, size_t len) const; 00501 00502 private: 00503 typedef enum { 00504 CREATE_RANDOM, 00505 CREATE_FROM_BYTES, 00506 CREATE_FROM_STRING, 00507 } create_t; 00508 00509 /* Used by named constructor createRandom */ 00510 Metadata (create_t t, size_t taskIdLen = 4, size_t opIdLen = 4); 00511 Metadata (create_t t, const void *from, size_t len); 00512 00513 u_int8_t version; 00514 TaskId taskId; 00515 OpId opId; 00516 Options options; 00517 00518 static void initRandom(); 00519 00520 }; 00521 }; //namespace Xtr 00522 00523 #endif // _XTR_METADATA_H