Revised OpenFlow Library  v0.6.0dev
 All Classes Files Functions Variables Friends Groups Pages
caddress.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 CADDRESS_H
6 #define CADDRESS_H 1
7 
8 #include <arpa/inet.h>
9 
10 #include <string>
11 
12 #include "rofl/common/croflexception.h"
13 #include "rofl/common/cmemory.h"
14 #include "rofl/common/logging.h"
15 
16 namespace rofl {
17 
18 class eAddress : public RoflException {
19 public:
20  eAddress(const std::string& __arg) : RoflException(__arg) {};
21 };
22 
23 class eAddressInval : public eAddress {
24 public:
25  eAddressInval(const std::string& __arg) : eAddress(__arg) {};
26 };
27 
28 class csockaddr; // forward declaration
29 
30 class caddress : public cmemory {
31 public:
32 
36  caddress(
37  size_t size = 0) :
38  rofl::cmemory(size) {};
39 
43  caddress(
44  const rofl::cmemory& addr) :
45  rofl::cmemory(addr) {};
46 
50  caddress(
51  const caddress& addr) { *this = addr; };
52 
56  virtual
57  ~caddress() {};
58 
62  caddress&
63  operator= (
64  const caddress& addr) {
65  if (this == &addr)
66  return *this;
67  cmemory::operator= (addr);
68  return *this;
69  };
70 
74  bool
75  operator< (
76  const caddress& addr) const { return cmemory::operator< (addr); };
77 
81  bool
82  operator> (
83  const caddress& addr) const { return cmemory::operator> (addr); };
84 
88  caddress
89  operator& (
90  const caddress& addr) const { return caddress(cmemory::operator& (addr)); };
91 
95  bool
96  operator== (
97  const caddress& addr) const { return cmemory::operator== (addr); };
98 
102  bool
103  operator!= (
104  const caddress& addr) const { return cmemory::operator!= (addr); };
105 
106 private:
107 
111  virtual uint8_t*
112  resize(size_t size) { return cmemory::resize(size); };
113 
114 public:
115 
119  virtual size_t
120  length() const;
121 
125  virtual void
126  pack(
127  uint8_t* buf, size_t buflen);
128 
132  virtual void
133  unpack(
134  uint8_t* buf, size_t buflen);
135 
136 public:
137 
141  friend std::ostream&
142  operator<< (std::ostream& os, const caddress& addr) {
143  os << rofl::indent(0) << "<caddress >" << std::endl;
144  rofl::indent i(2);
145  os << dynamic_cast<const rofl::cmemory&>( addr );
146  return os;
147  };
148 };
149 
150 
151 
152 class caddress_ll : public caddress {
153 public:
154 
158  caddress_ll() :
159  caddress(ETH_ADDR_LEN) {};
160 
164  caddress_ll(uint8_t* buf, size_t buflen) :
165  caddress(ETH_ADDR_LEN) {
166  if (buflen < ETH_ADDR_LEN)
167  throw eAddressInval("caddress_ll::caddress_ll() buflen too short");
168  cmemory::unpack(buf, buflen);
169  };
170 
174  caddress_ll(
175  const std::string& addr) :
176  caddress(ETH_ADDR_LEN) {
177  str2addr(addr);
178  };
179 
183  caddress_ll(
184  uint64_t mac) :
185  caddress(ETH_ADDR_LEN) {
186  set_mac(mac);
187  };
188 
192  virtual
193  ~caddress_ll() {};
194 
198  caddress_ll(
199  const caddress_ll& addr) { *this = addr; };
200 
204  caddress_ll&
205  operator= (
206  const caddress_ll& addr) {
207  if (this == &addr)
208  return *this;
209  caddress::operator= (addr);
210  return *this;
211  };
212 
216  bool
217  operator< (
218  const caddress_ll& addr) const { return caddress::operator< (addr); };
219 
223  bool
224  operator> (
225  const caddress_ll& addr) const { return caddress::operator> (addr); };
226 
231  operator& (
232  const caddress_ll& addr) const {
233  caddress_ll addr_ll;
234  addr_ll[0] = (*this)[0] & addr[0];
235  addr_ll[1] = (*this)[1] & addr[1];
236  addr_ll[2] = (*this)[2] & addr[2];
237  addr_ll[3] = (*this)[3] & addr[3];
238  addr_ll[4] = (*this)[4] & addr[4];
239  addr_ll[5] = (*this)[5] & addr[5];
240  return addr_ll;
241  };
242 
246  bool
247  operator== (
248  const caddress_ll& addr) const { return caddress::operator== (addr); };
249 
253  bool
254  operator!= (
255  const caddress_ll& addr) const { return caddress::operator!= (addr); };
256 
257 public:
258 
264  bool
265  is_multicast() const;
266 
272  bool
273  is_broadcast() const;
274 
280  bool
281  is_null() const;
282 
288  uint64_t
289  get_mac() const;
290 
294  void
295  set_mac(uint64_t mac);
296 
300  std::string
301  str() const { return addr2str(); };
302 
303 private:
304 
308  void
309  str2addr(
310  const std::string& addr);
311 
315  std::string
316  addr2str() const;
317 
318 public:
319 
320  friend std::ostream&
321  operator<< (std::ostream& os, const caddress_ll& addr) {
322  os << rofl::indent(0) << "<caddress_ll " << addr.addr2str() << " >" << std::endl;
323 #if 0
324  rofl::indent i(2);
325  os << dynamic_cast<const caddress&>( addr );
326 #endif
327  return os;
328  };
329 
330 private:
331 
332  static const size_t ETH_ADDR_LEN = 6;
333 };
334 
335 typedef caddress_ll cmacaddr;
336 
337 
338 
339 
340 class caddress_in : public caddress {
341 public:
342 
346  caddress_in(
347  size_t size = 0) :
348  caddress(size) {};
349 
353  virtual
354  ~caddress_in() {};
355 
359  caddress_in(
360  const caddress_in& addr) { *this = addr; };
361 
365  caddress_in&
366  operator= (
367  const caddress_in& addr) {
368  if (this == &addr)
369  return *this;
370  caddress::operator= (addr);
371  return *this;
372  };
373 
377  bool
378  operator< (
379  const caddress_in& addr) const { return caddress::operator< (addr); };
380 
384  bool
385  operator> (
386  const caddress_in& addr) const { return caddress::operator> (addr); };
387 
391  bool
392  operator== (
393  const caddress_in& addr) const { return caddress::operator== (addr); };
394 
398  bool
399  operator!= (
400  const caddress_in& addr) const { return caddress::operator!= (addr); };
401 
402 public:
403 
404  friend std::ostream&
405  operator<< (std::ostream& os, const caddress_in& addr) {
406  os << rofl::indent(0) << "<caddress_in >" << std::endl;
407  rofl::indent i(2);
408  os << dynamic_cast<const caddress&>( addr );
409  return os;
410  };
411 };
412 
413 
414 
415 class caddress_in4 : public caddress_in {
416 public:
417 
421  caddress_in4() :
422  caddress_in(INET4_ADDR_LEN) {};
423 
427  caddress_in4(
428  const std::string& addr) :
429  caddress_in(INET4_ADDR_LEN) {
430  str2addr(addr);
431  };
432 
436  caddress_in4(
437  struct sockaddr_in* sin, socklen_t salen) :
438  caddress_in(INET4_ADDR_LEN) {
439  if (salen < sizeof(struct sockaddr_in)) {
440  throw eAddressInval("caddress_in4::caddress_in4() invalid struct sockaddr_in");
441  }
442  set_addr_nbo(sin->sin_addr.s_addr);
443  };
444 
448  virtual
449  ~caddress_in4() {};
450 
454  caddress_in4(
455  const caddress_in4& addr) { *this = addr; };
456 
460  caddress_in4&
461  operator= (
462  const caddress_in4& addr) {
463  if (this == &addr)
464  return *this;
465  caddress_in::operator= (addr);
466  return *this;
467  };
468 
472  bool
473  operator< (
474  const caddress_in4& addr) const { return caddress_in::operator< (addr); };
475 
479  bool
480  operator> (
481  const caddress_in4& addr) const { return caddress_in::operator> (addr); };
482 
487  operator& (
488  const caddress_in4& addr) const {
489  caddress_in4 addr_in4;
490  for (unsigned int i = 0; i < INET4_ADDR_LEN; i++) {
491  addr_in4[i] = (*this)[i] & addr[i];
492  }
493  return addr_in4;
494  };
495 
499  bool
500  operator== (
501  const caddress_in4& addr) const { return caddress_in::operator== (addr); };
502 
506  bool
507  operator!= (
508  const caddress_in4& addr) const { return caddress_in::operator!= (addr); };
509 
510 public:
511 
515  uint32_t
516  get_addr_nbo() const {
517  uint8_t* ptr = somem();
518  return *((uint32_t*)ptr);
519  };
520 
524  void
525  set_addr_nbo(uint32_t addr) {
526  uint8_t* ptr = somem();
527  *((uint32_t*)ptr) = addr;
528  };
529 
530 
534  uint32_t
535  get_addr_hbo() const {
536  uint8_t* ptr = somem();
537  return be32toh(*((uint32_t*)ptr));
538  };
539 
543  void
544  set_addr_hbo(uint32_t addr) {
545  uint8_t* ptr = somem();
546  *((uint32_t*)ptr) = htobe32(addr);
547  };
548 
552  std::string
553  str() const { return addr2str(); };
554 
555 private:
556 
560  void
561  str2addr(
562  const std::string& addr);
563 
567  std::string
568  addr2str() const;
569 
570 public:
571 
572  friend std::ostream&
573  operator<< (std::ostream& os, const caddress_in4& addr) {
574  os << rofl::indent(0) << "<caddress_in4 " << addr.addr2str() << " >" << std::endl;
575 #if 0
576  rofl::indent i(2);
577  os << dynamic_cast<const caddress&>( addr );
578 #endif
579  return os;
580  };
581 
582 private:
583 
584  static const size_t INET4_ADDR_LEN = 4;
585 };
586 
587 
588 
589 class caddress_in6 : public caddress_in {
590 public:
591 
595  caddress_in6() :
596  caddress_in(INET6_ADDR_LEN) {};
597 
601  caddress_in6(
602  const std::string& addr) :
603  caddress_in(INET6_ADDR_LEN) {
604  str2addr(addr);
605  };
606 
610  caddress_in6(
611  struct sockaddr_in6* sin6, socklen_t salen) :
612  caddress_in(INET6_ADDR_LEN) {
613  if (salen < sizeof(struct sockaddr_in6)) {
614  throw eAddressInval("caddress_in6::caddress_in6() invalid struct sockaddr_in6");
615  }
616  memcpy(somem(), sin6->sin6_addr.s6_addr, INET6_ADDR_LEN);
617  };
618 
622  virtual
623  ~caddress_in6() {};
624 
628  caddress_in6(
629  const caddress_in6& addr) { *this = addr; };
630 
634  caddress_in6&
635  operator= (
636  const caddress_in6& addr) {
637  if (this == &addr)
638  return *this;
639  caddress_in::operator= (addr);
640  return *this;
641  };
642 
646  bool
647  operator< (
648  const caddress_in6& addr) const { return caddress_in::operator< (addr); };
649 
653  bool
654  operator> (
655  const caddress_in6& addr) const { return caddress_in::operator> (addr); };
656 
661  operator& (
662  const caddress_in6& addr) const {
663  caddress_in6 addr_in6;
664  for (unsigned int i = 0; i < INET6_ADDR_LEN; i++) {
665  addr_in6[i] = (*this)[i] & addr[i];
666  }
667  return addr_in6;
668  };
669 
673  bool
674  operator== (
675  const caddress_in6& addr) const { return caddress_in::operator== (addr); };
676 
680  bool
681  operator!= (
682  const caddress_in6& addr) const { return caddress_in::operator!= (addr); };
683 
687  std::string
688  str() const { return addr2str(); };
689 
690 private:
691 
695  void
696  str2addr(
697  const std::string& addr);
698 
702  std::string
703  addr2str() const;
704 
705 private:
706 
707  friend std::ostream&
708  operator<< (std::ostream& os, const caddress_in6& addr) {
709  os << rofl::indent(0) << "<caddress_in6 " << addr.addr2str() << " >" << std::endl;
710 #if 0
711  rofl::indent i(2);
712  os << dynamic_cast<const caddress&>( addr );
713 #endif
714  return os;
715  };
716 
717 private:
718 
719  static const size_t INET6_ADDR_LEN = 16;
720 };
721 
722 }; // end of namespace rofl
723 
724 #endif
virtual void pack(uint8_t *buf, size_t buflen)
Copies content of this cmemory instance to specified buffer.
Definition: caddress.cc:20
Definition: caddress.h:152
Definition: caddress.h:23
bool is_multicast() const
Check for multicast bit in hardware address.
Definition: caddress.cc:74
bool is_broadcast() const
Check for broadcast hardware address.
Definition: caddress.cc:84
uint64_t get_mac() const
Return the MAC address as a uint64_t value. The MAC address will be transformed to host byte order an...
Definition: caddress.cc:104
Definition: caddress.h:589
Definition: caddress.h:30
Definition: caddress.h:18
Definition: csockaddr.h:38
bool is_null() const
Check for null hardware address.
Definition: caddress.cc:94
Definition: caddress.h:340
bool operator!=(cmemory const &m) const
Comparison operator (unequal).
Definition: cmemory.cc:232
uint8_t * somem() const
Returns pointer to start of allocated memory area.
Definition: cmemory.cc:101
virtual void unpack(uint8_t *buf, size_t buflen)
Copies content of specified buffer into this cmemory instance .
Definition: caddress.cc:31
bool operator==(cmemory const &m) const
Comparison operator.
Definition: cmemory.cc:219
bool operator<(cmemory const &m) const
Less than operator.
Definition: cmemory.cc:129
virtual uint8_t * resize(size_t len)
Resizes allocated memory area by calling C-function realloc().
Definition: cmemory.cc:253
Definition: caddress.h:415
C++ abstraction for malloc'ed memory areas.
Definition: cmemory.h:44
bool operator>(cmemory const &m) const
Less than operator.
Definition: cmemory.cc:152
cmemory & operator=(cmemory const &m)
Assignment operator.
Definition: cmemory.cc:85
Definition: logging.h:76
Definition: croflexception.h:27
virtual void unpack(uint8_t *buf, size_t buflen)
Copies content of specified buffer into this cmemory instance .
Definition: cmemory.cc:304