|
1 /* |
|
2 * Copyright 2007 Baylor University |
|
3 * |
|
4 * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 * you may not use this file except in compliance with the License. |
|
6 * You may obtain a copy of the License at |
|
7 * |
|
8 * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 * |
|
10 * Unless required by applicable law or agreed to in writing, software |
|
11 * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 * See the License for the specific language governing permissions and |
|
14 * limitations under the License. |
|
15 */ |
|
16 |
|
17 #ifndef _PROPHET_BASE_TLV_H_ |
|
18 #define _PROPHET_BASE_TLV_H_ |
|
19 |
|
20 #include <unistd.h> |
|
21 #include <sys/types.h> |
|
22 |
|
23 |
|
24 namespace prophet |
|
25 { |
|
26 |
|
27 template<class TLV> |
|
28 struct TLVFactory |
|
29 { |
|
30 /** |
|
31 * Factory method for deserializing over-the-wire TLV into |
|
32 * in-memory class representation |
|
33 */ |
|
34 static TLV* deserialize(const u_char* bp, size_t len) |
|
35 { |
|
36 TLV* t = new TLV(); |
|
37 if (t->deserialize(bp,len)) |
|
38 return t; |
|
39 delete t; |
|
40 return NULL; |
|
41 } |
|
42 }; |
|
43 |
|
44 /** |
|
45 * The Prophet I-D (March 2006) dictates five bytecodes for router |
|
46 * state exchange messages. This implementation introduces a sixth |
|
47 * to help differentiate Bundle offers from Bundle responses. |
|
48 * |
|
49 * Each Prophet router is a state machine. Each encounter with |
|
50 * another router requires an exchange of router state. This begins |
|
51 * with a synchronization phase (Hello), followed by an exchange |
|
52 * of route tables (Information Exchange). |
|
53 * |
|
54 * BaseTLV is the abstract base class from which each concrete |
|
55 * class derives its API. |
|
56 */ |
|
57 class BaseTLV |
|
58 { |
|
59 public: |
|
60 /** |
|
61 * Byte codes for TLV types |
|
62 */ |
|
63 typedef enum { |
|
64 UNKNOWN_TLV = 0x00, |
|
65 HELLO_TLV = 0x01, |
|
66 ERROR_TLV = 0x02, |
|
67 RIBD_TLV = 0xA0, |
|
68 RIB_TLV = 0xA1, |
|
69 OFFER_TLV = 0XA2, |
|
70 RESPONSE_TLV = 0XA3, // not in spec |
|
71 } prophet_tlv_t; |
|
72 |
|
73 /** |
|
74 * Pretty print function for prophet_tlv_t. |
|
75 */ |
|
76 static const char* |
|
77 tlv_to_str(prophet_tlv_t tlv) |
|
78 { |
|
79 switch(tlv) { |
|
80 case HELLO_TLV: return "HELLO_TLV"; |
|
81 case RIBD_TLV: return "RIBD_TLV"; |
|
82 case RIB_TLV: return "RIB_TLV"; |
|
83 case OFFER_TLV: return "OFFER_TLV"; |
|
84 case RESPONSE_TLV: return "RESPONSE_TLV"; |
|
85 case ERROR_TLV: |
|
86 case UNKNOWN_TLV: |
|
87 default: return "Unknown TLV type"; |
|
88 } |
|
89 } |
|
90 |
|
91 /** |
|
92 * Destructor |
|
93 */ |
|
94 virtual ~BaseTLV() {} |
|
95 |
|
96 /** |
|
97 * Write out TLV from in-memory representation into |
|
98 * provided buffer, using no more than len bytes, returning |
|
99 * bytes written |
|
100 */ |
|
101 virtual size_t serialize(u_char* bp, size_t len) const = 0; |
|
102 |
|
103 ///@{ Accessors |
|
104 prophet_tlv_t typecode() const { return typecode_; } |
|
105 u_int8_t flags() const { return flags_; } |
|
106 u_int16_t length() const { return length_; } |
|
107 ///@} |
|
108 |
|
109 protected: |
|
110 |
|
111 /** |
|
112 * Constructor is protected to force use of factory |
|
113 */ |
|
114 BaseTLV(prophet_tlv_t typecode = UNKNOWN_TLV, |
|
115 u_int8_t flags = 0, |
|
116 u_int16_t length = 0) |
|
117 : typecode_(typecode), flags_(flags), length_(length) {} |
|
118 |
|
119 /** |
|
120 * Read a TLV in from transport and copy its contents into memory |
|
121 */ |
|
122 virtual bool deserialize(const u_char* bp, size_t len) = 0; |
|
123 |
|
124 prophet_tlv_t typecode_; ///< typecode for this TLV |
|
125 u_int8_t flags_; ///< TLV-specific flags |
|
126 mutable u_int16_t length_; ///< serialized length of TLV, mutable so it |
|
127 /// can be assigned by serialize() const |
|
128 }; // class BaseTLV |
|
129 |
|
130 }; // namespace prophet |
|
131 |
|
132 #endif // _PROPHET_BASE_TLV_H_ |