libDwm-0.6.0
DwmIpv4Routes.hh
Go to the documentation of this file.
1 //===========================================================================
2 // @(#) $DwmPath: dwm/libDwm/tags/libDwm-0.6.0/include/DwmIpv4Routes.hh 8955 $
3 // @(#) $Id: DwmIpv4Routes.hh 8955 2017-04-02 08:21:20Z dwm $
4 //===========================================================================
5 // Copyright (c) Daniel W. McRobb 1999-2005, 2016
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions
10 // are met:
11 //
12 // 1. Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 // 2. Redistributions in binary form must reproduce the above copyright
15 // notice, this list of conditions and the following disclaimer in the
16 // documentation and/or other materials provided with the distribution.
17 // 3. The names of the authors and copyright holders may not be used to
18 // endorse or promote products derived from this software without
19 // specific prior written permission.
20 //
21 // IN NO EVENT SHALL DANIEL W. MCROBB BE LIABLE TO ANY PARTY FOR
22 // DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
23 // INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE,
24 // EVEN IF DANIEL W. MCROBB HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
25 // DAMAGE.
26 //
27 // THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND
28 // DANIEL W. MCROBB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
29 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. DANIEL W. MCROBB MAKES NO
30 // REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER
31 // IMPLIED OR EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 // WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE,
33 // OR THAT THE USE OF THIS SOFTWARE WILL NOT INFRINGE ANY PATENT,
34 // TRADEMARK OR OTHER RIGHTS.
35 //===========================================================================
36 
37 //---------------------------------------------------------------------------
40 //---------------------------------------------------------------------------
41 
42 #ifndef _DWMIPV4ROUTES_HH_
43 #define _DWMIPV4ROUTES_HH_
44 
45 extern "C" {
46 #include <assert.h>
47 }
48 
49 #include <algorithm>
50 #include <unordered_map>
51 #include <vector>
52 
53 #include "DwmPortability.hh"
54 #include "DwmIO.hh"
55 #include "DwmGZIO.hh"
56 #include "DwmBZ2IO.hh"
57 #include "DwmIpv4Prefix.hh"
58 #include "DwmOperators.hh"
59 #include "DwmReadable.hh"
60 #include "DwmWritable.hh"
61 #include "DwmGZReadable.hh"
62 #include "DwmGZWritable.hh"
63 #include "DwmBZ2Readable.hh"
64 #include "DwmBZ2Writable.hh"
65 
66 namespace Dwm {
67  struct OurIpv4AddressHash
68  {
69  inline size_t operator () (const Dwm::Ipv4Address & addr) const
70  {
71  return(addr.Raw());
72  }
73  };
74 } // namespace Dwm
75 
76 namespace Dwm {
77 
78  //--------------------------------------------------------------------------
116  //--------------------------------------------------------------------------
117  template <typename _valueT>
119  : public Readable, public Writable,
120  public GZReadable, public GZWritable,
121  public BZ2Readable, public BZ2Writable
122  {
123  public:
124  typedef std::unordered_map<Ipv4Address, _valueT,
125  OurIpv4AddressHash> _RepSubType;
126  typedef typename _RepSubType::const_iterator const_iterator;
127 
128  //------------------------------------------------------------------------
130  //------------------------------------------------------------------------
132  : _hashMaps(33)
133  {
134  for (uint8_t i = 0; i < 33; ++i) {
135  _hashMaps[i].max_load_factor(.08);
136  }
137  }
138 
139  //------------------------------------------------------------------------
141  //------------------------------------------------------------------------
142  void Clear()
143  {
144  for (uint8_t i = 0; i < 33; ++i)
145  _hashMaps[i].clear();
146  return;
147  }
148 
149  //------------------------------------------------------------------------
151  //------------------------------------------------------------------------
152  bool Empty() const
153  {
154  bool rc = true;
155  for (uint8_t i = 0; i < 33; ++i) {
156  if (! _hashMaps[i].empty()) {
157  rc = false;
158  break;
159  }
160  }
161  return(rc);
162  }
163 
164  //------------------------------------------------------------------------
167  //------------------------------------------------------------------------
168  bool Add(const Ipv4Prefix & prefix, const _valueT & value)
169  {
170  bool rc = false;
171 
172  typename _RepSubType::iterator iter =
173  _hashMaps[prefix.MaskLength()].find(prefix.Network());
174  if (iter == _hashMaps[prefix.MaskLength()].end()) {
175  _hashMaps[prefix.MaskLength()][prefix.Network()] = value;
176  rc = true;
177  }
178  return(rc);
179  }
180 
181  //------------------------------------------------------------------------
183  //------------------------------------------------------------------------
184  _valueT & operator [] (const Ipv4Prefix & prefix)
185  {
186  return(_hashMaps[prefix.MaskLength()][prefix.Network()]);
187  }
188 
189  //------------------------------------------------------------------------
192  //------------------------------------------------------------------------
193  bool Delete(const Ipv4Prefix & prefix)
194  {
195  bool rc = false;
196  typename _RepSubType::iterator iter =
197  _hashMaps[prefix.MaskLength()].find(prefix.Network());
198  if (iter != _hashMaps[prefix.MaskLength()].end()) {
199  _hashMaps[prefix.MaskLength()].erase(iter);
200  rc = true;
201  }
202  return(rc);
203  }
204 
205  //------------------------------------------------------------------------
209  //------------------------------------------------------------------------
210  bool Find(const Ipv4Prefix & prefix, _valueT & match) const
211  {
212  bool rc = false;
213  if (! _hashMaps[prefix.MaskLength()].empty()) {
214  typename _RepSubType::const_iterator iter =
215  _hashMaps[prefix.MaskLength()].find(prefix.Network());
216  if (iter != _hashMaps[prefix.MaskLength()].end()) {
217  match = iter->second;
218  rc = true;
219  }
220  }
221  return(rc);
222  }
223 
224  //------------------------------------------------------------------------
228  //------------------------------------------------------------------------
229  bool FindLongest(const Ipv4Address & ipAddr,
230  std::pair<Ipv4Prefix,_valueT> & match) const
231  {
232  bool rc = false;
233 
234  Ipv4Prefix lp(ipAddr, 32);
235 
236  typename _RepSubType::const_iterator iter;
237  for (int8_t i = 32; i >= 0; --i) {
238  if (_hashMaps[i].empty())
239  continue;
240  lp.MaskLength(i);
241  iter = _hashMaps[i].find(lp.Network());
242  if (iter != _hashMaps[i].end()) {
243  match.first = lp;
244  match.second = iter->second;
245  rc = true;
246  break;
247  }
248  }
249  return(rc);
250  }
251 
252  //------------------------------------------------------------------------
259  //------------------------------------------------------------------------
260  bool
261  FindLongest(const Ipv4Address & ipAddr,
262  std::pair<Ipv4Prefix, const _valueT *> & match) const
263  {
264  bool rc = false;
265  Ipv4Prefix lp(ipAddr, 32);
266  typename _RepSubType::const_iterator iter;
267  for (int8_t i = 32; i >= 0; --i) {
268  if (_hashMaps[i].empty())
269  continue;
270  lp.MaskLength(i);
271  iter = _hashMaps[i].find(lp.Network());
272  if (iter != _hashMaps[i].end()) {
273  match.first = lp;
274  match.second = &(iter->second);
275  rc = true;
276  break;
277  }
278  }
279  return(rc);
280  }
281 
282  //------------------------------------------------------------------------
286  //------------------------------------------------------------------------
287  bool Find(const Ipv4Address & ipAddr,
288  std::vector<std::pair<Ipv4Prefix,_valueT> > & matches) const
289  {
290  if (! matches.empty())
291  matches.clear();
292 
293  typename _RepSubType::const_iterator iter;
294 
295  for (int8_t i = 32; i >= 0; --i) {
296  if (_hashMaps[i].empty())
297  continue;
298  Ipv4Prefix prefix(ipAddr, i);
299  iter = _hashMaps[i].find(prefix.Network());
300  if (iter != _hashMaps[i].end()) {
301  std::pair<Ipv4Prefix,_valueT> match(prefix, iter->second);
302  matches.push_back(match);
303  }
304  }
305  return(! matches.empty());
306  }
307 
308  //------------------------------------------------------------------------
310  //------------------------------------------------------------------------
311  void MaxLoadFactor(float loadFactor)
312  {
313  for (int8_t i = 32; i >= 0; --i) {
314  _hashMaps[i].max_load_factor(loadFactor);
315  }
316  return;
317  }
318 
319  //------------------------------------------------------------------------
323  //------------------------------------------------------------------------
324  bool operator == (const Ipv4Routes<_valueT> & r) const
325  {
326  for (int8_t i = 32; i >= 0; --i) {
327  if (_hashMaps[i] != r._hashMaps[i])
328  return(false);
329  }
330  return(true);
331  }
332 
333  //------------------------------------------------------------------------
337  //------------------------------------------------------------------------
338  bool operator != (const Ipv4Routes<_valueT> & r) const
339  {
340  return(! (*this == r));
341  }
342 
343  //------------------------------------------------------------------------
345  //------------------------------------------------------------------------
346  uint32_t Size() const
347  {
348  uint32_t rc = 0;
349  for (uint8_t i = 0; i < 33; ++i)
350  rc += _hashMaps[i].size();
351  return(rc);
352  }
353 
354  //------------------------------------------------------------------------
356  //------------------------------------------------------------------------
357  void HashSizes(std::vector<std::pair<uint8_t, uint32_t> > & sizes) const
358  {
359  if (! sizes.empty())
360  sizes.clear();
361  for (uint8_t i = 0; i < 33; ++i) {
362  if (! _hashMaps[i].empty()) {
363  sizes.push_back(std::pair<uint8_t,uint32_t>(i,_hashMaps[i].size()));
364  }
365  }
366  return;
367  }
368 
369  //------------------------------------------------------------------------
371  //------------------------------------------------------------------------
372  uint32_t StreamedLength() const
373  {
374  return(IO::StreamedLength(_hashMaps));
375  }
376 
377  //------------------------------------------------------------------------
379  //------------------------------------------------------------------------
380  std::istream & Read(std::istream & is)
381  {
382  return(IO::Read(is, _hashMaps));
383  }
384 
385  //------------------------------------------------------------------------
387  //------------------------------------------------------------------------
388  std::ostream & Write(std::ostream & os) const
389  {
390  return(IO::Write(os, _hashMaps));
391  }
392 
393  //------------------------------------------------------------------------
396  //------------------------------------------------------------------------
397  size_t Read(FILE *f)
398  {
399  return(IO::Read(f, _hashMaps));
400  }
401 
402  //------------------------------------------------------------------------
405  //------------------------------------------------------------------------
406  size_t Write(FILE *f) const
407  {
408  return(IO::Write(f, _hashMaps));
409  }
410 
411  //------------------------------------------------------------------------
414  //------------------------------------------------------------------------
415  ssize_t Read(int fd)
416  {
417  return(IO::Read(fd, _hashMaps));
418  }
419 
420  //------------------------------------------------------------------------
423  //------------------------------------------------------------------------
424  ssize_t Write(int fd) const
425  {
426  return(IO::Write(fd, _hashMaps));
427  }
428 
429  //------------------------------------------------------------------------
432  //------------------------------------------------------------------------
433  int Read(gzFile gzf)
434  {
435  return(GZIO::Read(gzf, _hashMaps));
436  }
437 
438  //------------------------------------------------------------------------
441  //------------------------------------------------------------------------
442  int Write(gzFile gzf) const
443  {
444  return(GZIO::Write(gzf, _hashMaps));
445  }
446 
447  //------------------------------------------------------------------------
450  //------------------------------------------------------------------------
451  int BZRead(BZFILE *bzf)
452  {
453  return(BZ2IO::BZRead(bzf, _hashMaps));
454  }
455 
456  //------------------------------------------------------------------------
459  //------------------------------------------------------------------------
460  int BZWrite(BZFILE *bzf) const
461  {
462  return(BZ2IO::BZWrite(bzf, _hashMaps));
463  }
464 
465  void SortByKey(std::vector<std::pair<Ipv4Prefix,_valueT>> & target,
466  bool ascending = true) const
467  {
468  if (! target.empty())
469  target.clear();
470  if (! this->_hashMaps.empty()) {
471  target.resize(this->Size());
472  typename std::vector<_RepSubType>::const_iterator iter =
473  this->_hashMaps.begin();
474  uint32_t pfx = 0;
475  for (uint8_t hashNum = 0; hashNum < 33; ++hashNum) {
476  if (! this->_hashMaps[hashNum].empty()) {
477  typename _RepSubType::const_iterator hiter =
478  this->_hashMaps[hashNum].begin();
479  for ( ; hiter != iter->end(); ++hiter) {
480  target[pfx].first = Ipv4Prefix(hiter->first, hashNum);
481  target[pfx].second = hiter->second;
482  ++pfx;
483  }
484  }
485  }
486  if (! target.empty()) {
487  if (ascending) {
488  std::sort(target.begin(), target.end(), KeyLess());
489  }
490  else {
491  std::sort(target.begin(), target.end(), KeyGreater());
492  }
493  }
494  }
495 
496  return;
497  }
498 
499  //------------------------------------------------------------------------
505  //------------------------------------------------------------------------
506  void SortByValue(std::vector<std::pair<Ipv4Prefix,_valueT> > & target)
507  {
508  if (! target.empty())
509  target.clear();
510  if (! this->_hashMaps.empty()) {
511  target.resize(this->Size());
512  typename std::vector<_RepSubType>::const_iterator iter =
513  this->_hashMaps.begin();
514  uint32_t pfx = 0;
515  for (uint8_t hashNum = 0; hashNum < 33; ++hashNum) {
516  if (! this->_hashMaps[hashNum].empty()) {
517  typename _RepSubType::const_iterator hiter =
518  this->_hashMaps[hashNum].begin();
519  for ( ; hiter != iter->end(); ++hiter) {
520  target[pfx].first = Ipv4Prefix(hiter->first, hashNum);
521  target[pfx].second = hiter->second;
522  ++pfx;
523  }
524  }
525  }
526  if (! target.empty())
527  std::sort(target.begin(), target.end(), ValueGreater());
528  }
529 
530  return;
531  }
532 
533  //------------------------------------------------------------------------
536  //------------------------------------------------------------------------
537  uint32_t AddressesCovered() const
538  {
539  uint32_t rc = 0;
540  for (int8_t hashNum = 32; hashNum > 1; --hashNum) {
541  typename _RepSubType::const_iterator hiter =
542  _hashMaps[hashNum].begin();
543  for ( ; hiter != _hashMaps[hashNum].end(); ++hiter) {
544  bool foundWider = false;
545  for (int8_t widerHash = hashNum - 1; widerHash > 0; --widerHash) {
546  Ipv4Prefix widerPfx(hiter->first, widerHash);
547  if (_hashMaps[widerHash].find(widerPfx.Network())
548  != _hashMaps[widerHash].end()) {
549  // found wider match, don't count
550  foundWider = true;
551  break;
552  }
553  }
554  if (! foundWider) {
555  rc += ((uint32_t)1 << (32 - hashNum));
556  }
557  }
558  }
559  return rc;
560  }
561 
562  struct KeyGreater
563  {
564  public:
565  bool operator () (const std::pair<Ipv4Prefix,_valueT> & e1,
566  const std::pair<Ipv4Prefix,_valueT> & e2) const
567  {
568  return(e1.first > e2.first);
569  }
570  };
571 
572  struct KeyLess
573  {
574  public:
575  bool operator () (const std::pair<Ipv4Prefix,_valueT> & e1,
576  const std::pair<Ipv4Prefix,_valueT> & e2) const
577  {
578  return(e1.first < e2.first);
579  }
580  };
581 
582  struct ValueGreater
583  {
584  public:
585  bool operator () (const std::pair<Ipv4Prefix,_valueT> & e1,
586  const std::pair<Ipv4Prefix,_valueT> & e2) const
587  {
588  return(e1.second > e2.second);
589  }
590  };
591 
592 
593  protected:
594  std::vector<_RepSubType> _hashMaps;
595  };
596 
597 
598 } // namespace Dwm
599 
600 #endif // _DWMIPV4ROUTES_HH_
601 
602 //---------------------------- emacs settings -----------------------------
603 // Local Variables:
604 // mode: C++/la
605 // tab-width: 2
606 // indent-tabs-mode: nil
607 // c-basic-offset: 2
608 // End:
609 //-------------------------------------------------------------------------
uint32_t StreamedLength() const
Returns the number of bytes that should be written if one of the Write() members is called...
Definition: DwmIpv4Routes.hh:372
This class is a pure virtual class, defining an interface for classes that can write their contents t...
Definition: DwmGZWritable.hh:57
bool FindLongest(const Ipv4Address &ipAddr, std::pair< Ipv4Prefix, const _valueT *> &match) const
Finds the longest match for ipAddr.
Definition: DwmIpv4Routes.hh:261
bool Empty() const
Returns true if there are no entries.
Definition: DwmIpv4Routes.hh:152
Dwm::Writable pure virtual class definition.
Dwm::BZ2Readable pure virtual class definition.
ssize_t Write(int fd) const
Writes the routes to a file descriptor.
Definition: DwmIpv4Routes.hh:424
Dwm::GZReadable pure virtual class definition.
std::ostream & Write(std::ostream &os) const
Writes the routes to an ostream. Returns the ostream.
Definition: DwmIpv4Routes.hh:388
Dwm::BZ2IO class definition.
This class encapsulates an IPv4 address.
Definition: DwmIpv4Address.hh:62
Dwm::Readable pure virtual class definition.
void Clear()
Clears all entries.
Definition: DwmIpv4Routes.hh:142
bool FindLongest(const Ipv4Address &ipAddr, std::pair< Ipv4Prefix, _valueT > &match) const
Finds the longest match for ipAddr.
Definition: DwmIpv4Routes.hh:229
Dwm::GZIO class definition.
bool Add(const Ipv4Prefix &prefix, const _valueT &value)
Adds an entry.
Definition: DwmIpv4Routes.hh:168
bool Find(const Ipv4Prefix &prefix, _valueT &match) const
Find the entry for the given prefix.
Definition: DwmIpv4Routes.hh:210
This class encapsulates an IPv4 address and netmask.
Definition: DwmIpv4Prefix.hh:69
uint32_t Size() const
Returns the number of routes.
Definition: DwmIpv4Routes.hh:346
void SortByValue(std::vector< std::pair< Ipv4Prefix, _valueT > > &target)
Sorts the contained pair<Ipv4Prefix,_valueT> values into a vector, in descending order by the value s...
Definition: DwmIpv4Routes.hh:506
Miscellaneous operators.
static int Write(gzFile gzf, char c)
Writes c to gzf.
This class is a pure virtual class, defining an interface for classes that can read their contents fr...
Definition: DwmBZ2Readable.hh:57
static int Read(gzFile gzf, char &c)
Reads from gzf.
size_t Read(FILE *f)
Reades the routes from a FILE pointer.
Definition: DwmIpv4Routes.hh:397
static std::ostream & Write(std::ostream &os, char c)
Writes c to os. Returns os.
bool operator!=(const std::unordered_map< _keyT, _valueT, _Hash, _Pred, _Alloc > &a, const std::unordered_map< _keyT, _valueT, _Hash, _Pred, _Alloc > &b)
operator != for unordered_map.
Definition: DwmOperators.hh:85
Dwm::BZ2Writable pure virtual class definition.
Dwm::Ipv4Prefix class definition.
static std::istream & Read(std::istream &is, char &c)
Reads c from is. Returns is.
Definition: DwmBZ2IO.hh:67
ssize_t Read(int fd)
Reads the routes from a file descriptor.
Definition: DwmIpv4Routes.hh:415
uint8_t MaskLength() const
Returns the length of the netmask (number of significant bits).
bool Delete(const Ipv4Prefix &prefix)
Deletes the entry for prefix.
Definition: DwmIpv4Routes.hh:193
int BZRead(BZFILE *bzf)
Reads the routes from a BZFILE pointer.
Definition: DwmIpv4Routes.hh:451
Ipv4Address Network() const
Returns the network portion of the prefix.
Definition: DwmIpv4Prefix.hh:124
uint32_t AddressesCovered() const
Returns the number of addresses covered by the contained prefixes, not including 0/0.
Definition: DwmIpv4Routes.hh:537
static int BZRead(BZFILE *bzf, char &c)
Reads from bzf.
This class is a pure virtual class, defining an interface for classes that can read their contents fr...
Definition: DwmGZReadable.hh:57
bool operator==(const std::unordered_map< _keyT, _valueT, _Hash, _Pred, _Alloc > &a, const std::unordered_map< _keyT, _valueT, _Hash, _Pred, _Alloc > &b)
operator == for unordered_map.
Definition: DwmOperators.hh:58
Dwm::IO class definition.
std::istream & Read(std::istream &is)
Reads the routes from an istream. Returns the istream.
Definition: DwmIpv4Routes.hh:380
int BZWrite(BZFILE *bzf) const
Writes the routes to a BZFILE pointer.
Definition: DwmIpv4Routes.hh:460
This class defines an interface for classes that can write their contents to an ostream, file descriptor or FILE pointer.
Definition: DwmWritable.hh:58
static int BZWrite(BZFILE *bzf, char c)
Writes c to bzf.
static uint32_t StreamedLength(char c)
Returns the number of bytes that would be written if we called Write() for a char.
Definition: DwmIO.hh:92
bool Find(const Ipv4Address &ipAddr, std::vector< std::pair< Ipv4Prefix, _valueT > > &matches) const
Finds all matches for ipAddr.
Definition: DwmIpv4Routes.hh:287
This class is a pure virtual class, defining an interface for classes that can write their contents t...
Definition: DwmBZ2Writable.hh:57
Dwm::GZWritable pure virtual class definition.
This class defines an interface for classes that can read their contents from an istream, file descriptor or FILE pointer.
Definition: DwmReadable.hh:58
ipv4addr_t Raw() const
Returns an ipv4addr_t representation (32-bit value in network byte order).
Definition: DwmIpv4Address.hh:87
int Write(gzFile gzf) const
Writes the routes to a gzFile.
Definition: DwmIpv4Routes.hh:442
This template class provides an associative container keyed by IPv4 addresses, with longest-match sea...
Definition: DwmIpv4Routes.hh:118
int Read(gzFile gzf)
Reads the routes from a gzFile.
Definition: DwmIpv4Routes.hh:433
size_t Write(FILE *f) const
Writes the routes to a FILE pointer.
Definition: DwmIpv4Routes.hh:406
A configure target for dealing with OS differences.