Revised OpenFlow Library  v0.6.0dev
 All Classes Files Functions Variables Friends Groups Pages
fipv4frame.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #ifndef FIPV4FRAME_H
6 #define FIPV4FRAME_H 1
7 
8 #include <map>
9 #include <inttypes.h>
10 #include <endian.h>
11 #ifndef htobe16
12  #include "../endian_conversion.h"
13 #endif
14 
15 #include "../fframe.h"
16 #include "../caddress.h"
17 
18 namespace rofl
19 {
20 
21 // error classes
22 class eIPv4FrameBase : public eFrameBase {}; // base error class for cpppoepacket
23 class eIPv4FrameTagNotFound : public eIPv4FrameBase {}; // pppoe tag not found
24 class eIPv4FrameInvalidSyntax : public eIPv4FrameBase {}; // frame has invalid syntax
25 class eIPv4FrameInval : public eIPv4FrameBase {}; // invalid parameter
26 
27 
31 class fipv4frame : public fframe {
32 public: // static
33 
34  /* IPv4 constants and definitions */
35  // IPv4 ethernet types
36  enum ipv4_ether_t {
37  IPV4_ETHER = 0x0800,
38  };
39 
40  enum ipv4_ip_proto_t {
41  IPV4_IP_PROTO = 4,
42  };
43 
44  // IPv4 header
45  struct ipv4_hdr_t {
46  uint8_t ihlvers; // IP header length in 32bit words,
47  // TODO: check for options and padding
48  uint8_t tos;
49  uint16_t length;
50  uint16_t ident;
51  uint16_t offset_flags;
52  uint8_t ttl;
53  uint8_t proto;
54  uint16_t checksum;
55  uint32_t src;
56  uint32_t dst;
57  uint8_t data[0];
58  } __attribute__((packed));
59 
60  enum ipv4_flag_t {
61  bit_reserved = (1 << 0),
62  bit_dont_fragment = (1 << 1),
63  bit_more_fragments = (1 << 2),
64  };
65 
66 #if 0
67  // IPv4 header
68  struct ipv4_hdr_t {
69  #if __BYTE_ORDER == __LITTLE_ENDIAN
70  uint8_t ihl : 4; // IP header length in 32bit words,
71  // TODO: check for options and padding
72  uint8_t version : 4;
73 
74  #elif __BYTE_ORDER == __BIG_ENDIAN
75  uint8_t version : 4;
76  uint8_t ihl : 4; // IP header length in 32bit words,
77  // TODO: check for options and padding
78  #endif
79 
80  uint8_t tos;
81  uint16_t length;
82  uint16_t ident;
83  #if __BYTE_ORDER == __LITTLE_ENDIAN
84  uint16_t offset : 13;
85  uint16_t flags : 3;
86  #elif __BYTE_ORDER == __BIG_ENDIAN
87  uint16_t flags : 3;
88  uint16_t offset : 13;
89  #endif
90  uint8_t ttl;
91  uint8_t proto;
92  uint16_t checksum;
93  uint32_t src;
94  uint32_t dst;
95  uint8_t data[0];
96  } __attribute__((packed));
97 #endif
98 
99 #if 0
100  enum ip_flag_t {
101  IP_FLAG_RSVD = (1 << 1),
102  IP_FLAG_DONT_FRAGMENT = (1 << 2),
103  IP_FLAG_MORE_FRAGMENTS = (1 << 3),
104  };
105 #endif
106 
107  enum ip_proto_t {
108  IPPROTO_ICMP = 1,
109  IPPROTO_TCP = 6,
110  IPPROTO_UDP = 17,
111  };
112  /* IPv4 definitions */
113 
114 public: // methods
115 
116 
120  fipv4frame(
121  uint8_t* data,
122  size_t datalen);
123 
124 
128  virtual
129  ~fipv4frame();
130 
131 
132 #if 0
133 
136  struct fipv4frame::ipv4_option_hdr_t*
137  option_insert(enum fpppoeframe::pppoe_tag_t type, char *fmt, ...);
138 
142  struct fipv4frame::ipv4_option_hdr_t*
143  option_find(enum fpppoeframe::pppoe_tag_t) throw (ePPPoEFrameTagNotFound);
144 #endif
145 
149  void
151 
152 public: // overloaded from fframe
153 
156  virtual bool
157  complete() const;
158 
161  virtual size_t
162  need_bytes() const;
163 
167  virtual void
168  validate(uint16_t total_len = 0) const;
169 
173  virtual void
174  initialize();
175 
179  virtual void
181  uint8_t *data, size_t datalen) throw (eFrameOutOfRange);
182 
186  virtual uint8_t*
187  payload() const throw (eFrameNoPayload);
188 
192  virtual size_t
193  payloadlen() const throw (eFrameNoPayload);
194 
195 
199  virtual const char*
200  c_str();
201 
202 public:
203 
206  void
207  set_ipv4_ihl(uint8_t ihl = 5);
208 
211  uint8_t
212  get_ipv4_ihl() const;
213 
216  void
217  set_ipv4_version(uint8_t version = 4);
218 
221  uint8_t
222  get_ipv4_version() const;
223 
226  void
227  set_ipv4_src(uint32_t src);
228 
231  void
232  set_ipv4_src(caddress_in4 const& src);
233 
236  caddress_in4
237  get_ipv4_src() const;
238 
241  void
242  set_ipv4_dst(uint32_t dst);
243 
246  void
247  set_ipv4_dst(caddress_in4 const& dst);
248 
251  caddress_in4
252  get_ipv4_dst() const;
253 
256  void
257  set_ipv4_dscp(uint8_t dscp);
258 
261  uint8_t
262  get_ipv4_dscp() const;
263 
266  void
267  set_ipv4_ecn(uint8_t ecn);
268 
271  uint8_t
272  get_ipv4_ecn() const;
273 
276  void
277  set_ipv4_ttl(uint8_t ttl);
278 
281  uint8_t
282  get_ipv4_ttl();
283 
286  void
287  dec_ipv4_ttl();
288 
291  void
292  set_ipv4_proto(uint8_t proto);
293 
296  uint8_t
297  get_ipv4_proto();
298 
301  uint16_t
302  get_ipv4_length();
303 
306  void
307  set_ipv4_length(uint16_t length);
308 
311  bool
312  has_DF_bit_set();
313 
316  void
317  set_DF_bit();
318 
321  void
322  clear_DF_bit();
323 
326  bool
327  has_MF_bit_set();
328 
331  void
332  set_MF_bit();
333 
336  void
337  clear_MF_bit();
338 
339 public: // data structures
340 
341  // pointer to pppoe header
342  struct ipv4_hdr_t *ipv4_hdr;
343 
344  // map of options
345  //std::map<enum fipv4frame::ipv4_option_t, struct fipv4frame::ipv4_option_hdr_t*> ipv4_options;
346 
347  // payload data
348  uint8_t *ipv4data;
349 
350  // ppp data length
351  size_t ipv4datalen;
352 
353 private: // methods
354 
355 
356 private: // data structures
357 
358  //< info string
359  std::string info;
360 
361 public:
362 
363  friend std::ostream&
364  operator<< (std::ostream& os, fipv4frame const& frame) {
365  os << dynamic_cast<fframe const&>( frame );
366  os << indent(2) << "<fipv4frame ";
367  os << "src:" << frame.get_ipv4_src() << " ";
368  os << "dst:" << frame.get_ipv4_dst() << " ";
369  os << "dscp:" << (int)frame.get_ipv4_dscp() << " ";
370  os << "ecn:" << (int)frame.get_ipv4_ecn() << " ";
371  os << ">" << std::endl;
372  return os;
373  };
374 };
375 
376 }; // end of namespace
377 
378 #endif
void set_ipv4_src(uint32_t src)
Definition: fipv4frame.cc:210
virtual void initialize()
Definition: fipv4frame.cc:31
virtual size_t need_bytes() const
Definition: fipv4frame.cc:73
virtual size_t payloadlen() const
Definition: fipv4frame.cc:115
Definition: fipv4frame.h:45
virtual void payload_insert(uint8_t *data, size_t datalen)
Definition: fipv4frame.cc:91
virtual void validate(uint16_t total_len=0) const
Definition: fipv4frame.cc:126
Definition: fframe.h:19
Definition: fipv4frame.h:23
void ipv4_calc_checksum()
Definition: fipv4frame.cc:175
Definition: fipv4frame.h:25
virtual uint8_t * payload() const
Definition: fipv4frame.cc:105
Definition: fipv4frame.h:22
Definition: fipv4frame.h:31
virtual const char * c_str()
Definition: fipv4frame.cc:136
virtual bool complete() const
Definition: fipv4frame.cc:60
fipv4frame(uint8_t *data, size_t datalen)
Definition: fipv4frame.cc:9
virtual ~fipv4frame()
Definition: fipv4frame.cc:23
Definition: fframe.h:31
void set_ipv4_dst(uint32_t dst)
Definition: fipv4frame.cc:232
Definition: fipv4frame.h:24