00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef _PROPHET_BASE_TLV_H_
00018 #define _PROPHET_BASE_TLV_H_
00019
00020 #include <unistd.h>
00021 #include <sys/types.h>
00022 #include <stddef.h>
00023
00024
00025 namespace prophet
00026 {
00027
00028 template<class TLV>
00029 struct TLVFactory
00030 {
00035 static TLV* deserialize(const u_char* bp, size_t len)
00036 {
00037 TLV* t = new TLV();
00038 if (t->deserialize(bp,len))
00039 return t;
00040 delete t;
00041 return NULL;
00042 }
00043 };
00044
00058 class BaseTLV
00059 {
00060 public:
00064 typedef enum {
00065 UNKNOWN_TLV = 0x00,
00066 HELLO_TLV = 0x01,
00067 ERROR_TLV = 0x02,
00068 RIBD_TLV = 0xA0,
00069 RIB_TLV = 0xA1,
00070 OFFER_TLV = 0XA2,
00071 RESPONSE_TLV = 0XA3,
00072 } prophet_tlv_t;
00073
00077 static const char*
00078 tlv_to_str(prophet_tlv_t tlv)
00079 {
00080 switch(tlv) {
00081 case HELLO_TLV: return "HELLO_TLV";
00082 case RIBD_TLV: return "RIBD_TLV";
00083 case RIB_TLV: return "RIB_TLV";
00084 case OFFER_TLV: return "OFFER_TLV";
00085 case RESPONSE_TLV: return "RESPONSE_TLV";
00086 case ERROR_TLV:
00087 case UNKNOWN_TLV:
00088 default: return "Unknown TLV type";
00089 }
00090 }
00091
00095 virtual ~BaseTLV() {}
00096
00102 virtual size_t serialize(u_char* bp, size_t len) const = 0;
00103
00105 prophet_tlv_t typecode() const { return typecode_; }
00106 u_int8_t flags() const { return flags_; }
00107 u_int16_t length() const { return length_; }
00109
00110 protected:
00111
00115 BaseTLV(prophet_tlv_t typecode = UNKNOWN_TLV,
00116 u_int8_t flags = 0,
00117 u_int16_t length = 0)
00118 : typecode_(typecode), flags_(flags), length_(length) {}
00119
00123 virtual bool deserialize(const u_char* bp, size_t len) = 0;
00124
00125 prophet_tlv_t typecode_;
00126 u_int8_t flags_;
00127 mutable u_int16_t length_;
00128
00129 };
00130
00131 };
00132
00133 #endif // _PROPHET_BASE_TLV_H_