X-TraceC++Library
XtrOption.h
Go to the documentation of this file.
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 #include <cstdlib>
00029 #include <arpa/inet.h>
00030 #include "XtrConstants.h" 
00031 
00032 #ifndef _XTR_OPTION_H
00033 #define _XTR_OPTION_H
00034 
00035 namespace xtr {
00037 class Option
00038 {
00039 public:
00040 
00056     static Option* createFromBytes(const u_int8_t *b, size_t *size);
00057     
00058     Option() {};
00059     virtual ~Option() {};
00060 
00061 
00064     virtual Option* clone() const = 0;
00065    
00067     virtual u_int8_t getType() const = 0;
00068 
00070     virtual u_int8_t getLength() const = 0;
00071 
00073     virtual u_int8_t getSize() const = 0;
00074 
00081     virtual xtr_result pack(u_int8_t *dest, size_t *size) const = 0;
00082 
00084     bool isEqual(const Option& other) const;
00085 
00086     enum {
00087         NOP = 0,
00088         CHAIN_ID = 0xC1,
00089         LAMPORT  = 0xC0, 
00090         SEVERITY = 0xCE,
00091     };
00092 };
00093 
00094     
00098 class OptionNop : public Option
00099 {
00100 public:
00101     OptionNop() {} ;
00102     OptionNop(const u_int8_t *b, size_t *size);
00103 
00104     /* @override */
00105     OptionNop* clone() const { return new OptionNop(*this); }
00106 
00107     /* @override */
00108     u_int8_t getType() const {return Option::NOP; }
00109     /* @override */
00110     u_int8_t getLength() const {return 0;}
00111     /* @override */
00112     u_int8_t getSize() const {return 1;}
00113 
00114     /* @override */
00115     xtr_result pack(u_int8_t *dest, size_t *size) const;
00116 };
00117 
00118 
00123 class OptionAny: public Option
00124 {
00125 public:
00126     OptionAny() : type(Option::NOP), length(0) {};
00127 
00128     OptionAny(const u_int8_t *b, size_t *size);
00129 
00130     OptionAny(u_int8_t type, u_int8_t length, const u_int8_t *p);
00131 
00132     xtr_result initFromBytes(const u_int8_t *b, size_t *size);
00133 
00134     /* @override */
00135     OptionAny* clone() const { return new OptionAny(*this); }
00136 
00137     /* @override */
00138     u_int8_t getType() const {return type; }
00139     /* @override */
00140     u_int8_t getLength() const {return length;}
00141     /* @override */
00142     u_int8_t getSize() const {return (getType() == Option::NOP)?1:getLength()+2; }
00143 
00144     /* @override */
00145     xtr_result pack(u_int8_t *dest, size_t *size) const;
00146 
00147     xtr_result setType(u_int8_t _type);
00148     xtr_result setLength(u_int8_t _length);
00149 
00150     u_int8_t *getPayload() {return payload;}
00151 
00152 private:
00153     u_int8_t type;
00154     u_int8_t length;
00155     u_int8_t payload[253];
00156 };
00157 
00158 #if 0
00159 
00161 class XtrChainId : public Id<XTR_MAX_CHAIN_ID_LEN>
00162 {
00163 public:
00164     XtrChainId(size_t len = 0) : Id<XTR_MAX_CHAIN_ID_LEN>(len) {};
00165     XtrChainId(const u_int8_t* from, size_t len)
00166         : Id<XTR_MAX_CHAIN_ID_LEN>(from, len);
00167     /* @override */
00168     bool isEqual(const XtrChainId& other) const;
00169 protected:
00170     /* @override */
00171     bool isValidLength(size_t len) const {return (len < XTR_MAX_CHAIN_ID_LEN);}
00172 };
00173 #endif
00174 
00176 class OptionChainId : public Option
00177 {
00178 public:
00180     OptionChainId() : id(0) {};
00181 
00185     OptionChainId(const u_int8_t *b, size_t *size);
00186 
00187     OptionChainId(u_int16_t _id) {id = _id;}
00188 
00189     /* @override */
00190     OptionChainId* clone() const { return new OptionChainId(*this); }
00191 
00192     /* @override */
00193     u_int8_t getType() const {return Option::CHAIN_ID; }
00194     /* @override */
00195     u_int8_t getLength() const {return 2; }
00196     /* @override */
00197     u_int8_t getSize() const {return 4; }
00198 
00199     u_int16_t getId() const {return id;}
00200     void setId(u_int16_t _id) {id = _id;}
00201 
00202     /* @override */
00203     xtr_result pack(u_int8_t *dest, size_t *size) const;
00204     ~OptionChainId() {};
00205 private:
00206     u_int16_t id;
00207 };
00208 
00210 class OptionSeverity : public Option
00211 {
00212 public:
00218     typedef enum {
00219           EMERG = 0,    //Most severe. Will be logged unless the threshold is _NONE
00220           ALERT = 1,
00221        CRITICAL = 2,
00222           ERROR = 3,
00223         WARNING = 4,
00224          NOTICE = 5,
00225            INFO = 6,
00226           DEBUG = 7,    //Least severe. Will be logged if threshold is DEBUG or _ALL
00227            _ALL = 8,    //Only valid for the severity threshold, logs all
00228           _NONE = 255,  //Only valid for the severity threshold, logs none
00229          _UNSET = 254,  //Only valid for the severity threshold, means threshold unset
00230        _DEFAULT = NOTICE,
00231     } Level;
00232 
00233     OptionSeverity() : severity(_DEFAULT) {};
00234     OptionSeverity(const u_int8_t *b, size_t *size);
00235     OptionSeverity(u_int8_t s) : severity(s & 0x7) {};
00236 
00237     /* @override */
00238     OptionSeverity* clone() const { return new OptionSeverity(*this); }
00239 
00240     /* @override */
00241     u_int8_t getType() const {return Option::SEVERITY; }
00242     /* @override */
00243     u_int8_t getLength() const {return 1;}
00244     /* @override */
00245     u_int8_t getSize() const {return 3; }
00246 
00247     u_int8_t getSeverity() const {return severity;}
00248     void setSeverity(u_int8_t s) {severity = (s & 0x7);}
00249 
00250     /* @override */
00251     xtr_result pack(u_int8_t *dest, size_t *size) const;
00252 private:
00253     u_int8_t severity;
00254 };
00255 
00256 /* TODO:
00257     class XtrOptionDestOpenDHT : public Option
00258     class XtrOptionDestTCPv4 : public Option
00259     class XtrOptionDestTCPv6 : public Option
00260     class XtrOptionDestUDPv4 : public Option
00261     class XtrOptionDestUDPv6 : public Option
00262     class XtrOptionDestI3 : public Option
00263 */
00264 
00265 }; //namespace xtr
00266 
00267 #endif //_XTR_OPTION_H