libDwmRDAP-0.1.4
DwmIpv4CountryDb.hh
Go to the documentation of this file.
1 //===========================================================================
2 // @(#) $Name:$
3 // @(#) $Id: DwmIpv4CountryDb.hh 9123 2017-04-15 01:55:02Z dwm $
4 //===========================================================================
5 // Copyright (c) Daniel W. McRobb 2017
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 _DWMIPV4COUNTRYDB_HH_
43 #define _DWMIPV4COUNTRYDB_HH_
44 
45 #include <iostream>
46 #include <mutex>
47 #include <string>
48 #include <tuple>
49 
50 #include "DwmIpv4Prefix.hh"
51 #include "DwmRDAPIpv4Routes.hh"
52 #include "DwmReadable.hh"
53 #include "DwmWritable.hh"
54 #include "DwmTimeValue64.hh"
55 #include "DwmRDAPResponse.hh"
56 
57 namespace Dwm {
58 
59  //--------------------------------------------------------------------------
62  //--------------------------------------------------------------------------
64  : public Dwm::Readable, public Dwm::Writable,
65  public GZReadable, public GZWritable,
66  public BZ2Readable, public BZ2Writable
67  {
68  public:
69  //------------------------------------------------------------------------
71  //------------------------------------------------------------------------
73 
74  //------------------------------------------------------------------------
76  //------------------------------------------------------------------------
77  Ipv4CountryDbValue(const std::string & code,
78  const TimeValue64 & lastChanged,
79  const TimeValue64 & lastUpdated = TimeValue64(true));
80 
81  //------------------------------------------------------------------------
83  //------------------------------------------------------------------------
84  Ipv4CountryDbValue(const Json::Value & rdapResponse);
85 
86  //------------------------------------------------------------------------
88  //------------------------------------------------------------------------
89  const std::string & Code() const;
90 
91  //------------------------------------------------------------------------
93  //------------------------------------------------------------------------
94  const std::string & Code(const std::string code);
95 
96  //------------------------------------------------------------------------
98  //------------------------------------------------------------------------
99  const TimeValue64 & LastUpdated() const;
100 
101  //------------------------------------------------------------------------
103  //------------------------------------------------------------------------
104  const TimeValue64 & LastUpdated(const TimeValue64 & lastUpdated);
105 
106  //------------------------------------------------------------------------
109  //------------------------------------------------------------------------
110  const TimeValue64 & LastChanged() const;
111 
112  //------------------------------------------------------------------------
116  //------------------------------------------------------------------------
117  const TimeValue64 & LastChanged(const TimeValue64 & lastUpdated);
118 
119  //------------------------------------------------------------------------
121  //------------------------------------------------------------------------
122  std::istream & Read(std::istream & is);
123 
124  //------------------------------------------------------------------------
127  //------------------------------------------------------------------------
128  ssize_t Read(int fd);
129 
130  //------------------------------------------------------------------------
133  //------------------------------------------------------------------------
134  size_t Read(FILE * f);
135 
136  //------------------------------------------------------------------------
139  //------------------------------------------------------------------------
140  int Read(gzFile gzf);
141 
142  //------------------------------------------------------------------------
145  //------------------------------------------------------------------------
146  int BZRead(BZFILE *bzf);
147 
148  //------------------------------------------------------------------------
150  //------------------------------------------------------------------------
151  std::ostream & Write(std::ostream & os) const;
152 
153  //------------------------------------------------------------------------
156  //------------------------------------------------------------------------
157  ssize_t Write(int fd) const;
158 
159  //------------------------------------------------------------------------
162  //------------------------------------------------------------------------
163  size_t Write(FILE *f) const;
164 
165  //------------------------------------------------------------------------
168  //------------------------------------------------------------------------
169  int Write(gzFile gzf) const;
170 
171  //------------------------------------------------------------------------
174  //------------------------------------------------------------------------
175  int BZWrite(BZFILE *bzf) const;
176 
177  //------------------------------------------------------------------------
180  //------------------------------------------------------------------------
181  uint32_t StreamedLength() const;
182 
183  //------------------------------------------------------------------------
185  //------------------------------------------------------------------------
186  bool operator == (const Ipv4CountryDbValue & val) const;
187 
188  Json::Value Json() const;
189 
190  private:
191  // <countryCode, lastUpdated, lastChanged>
192  std::tuple<std::string,TimeValue64,TimeValue64> _data;
193  };
194 
195  //--------------------------------------------------------------------------
197  //--------------------------------------------------------------------------
199  : public RDAP::Ipv4Routes<Ipv4CountryDbValue>
200  {
201  public:
202  //------------------------------------------------------------------------
205  //------------------------------------------------------------------------
206  Ipv4CountryDb(const std::string & fn = "/usr/local/etc/ipv4country.db");
207 
208  bool Add(const RDAP::IPv4Response & rdapResponse);
209 
210  bool Update(const RDAP::IPv4Response & rdapResponse);
211 
212  //------------------------------------------------------------------------
214  //------------------------------------------------------------------------
215  bool Reload();
216 
217  //------------------------------------------------------------------------
219  //------------------------------------------------------------------------
220  bool Save() const;
221 
222  //------------------------------------------------------------------------
224  //------------------------------------------------------------------------
225  void PrintJson(std::ostream & os) const;
226 
227  void CreateCountryAggregates();
228 
229  std::vector<Ipv4Prefix>
230  PrefixesForCountries(const std::vector<std::string> & countries);
231 
232  std::vector<Ipv4Prefix>
233  PrefixesForCountries(const std::string & regExpression,
234  bool minimalSet = true);
235 
236  void AggregateAdjacents();
237 
238  private:
239  std::string _filename;
240 
241  void CreateCountryAggregates(uint8_t maslken);
242  static void CoalesceAdjacentPrefixes(std::vector<Ipv4Prefix> & vp);
243  static void CoalesceContainedPrefixes(std::vector<Ipv4Prefix> & vp);
244  };
245 
246 } // namespace Dwm
247 
248 #endif // _DWMIPV4COUNTRYDB_HH_
Ipv4CountryDbValue()
Default constructor.
const std::string & Code() const
Returns the country code.
std::ostream & Write(std::ostream &os) const
Writes the value to an ostream. Returns the ostream.
const TimeValue64 & LastChanged() const
Returns the last-changed timestamp.
Encapsulates a database to map Ipv4Prefix to Ipv4CountryDbValue.
Definition: DwmIpv4CountryDb.hh:198
uint32_t StreamedLength() const
Returns the number of bytes that would be written if one of the Write() members were called...
const TimeValue64 & LastUpdated() const
Returns the last-updated timestamp.
Dwm::RDAP::Ipv4Routes class template definition.
Dwm::RDAP::Response class definition.
Encapsulate an RDAP ip query response.
Definition: DwmRDAPResponse.hh:70
Encapsulates a value stored in a Ipv4CountryDb.
Definition: DwmIpv4CountryDb.hh:63
This template class provides an associative container keyed by IPv4 addresses, with longest-match sea...
Definition: DwmRDAPIpv4Routes.hh:83
Definition: DwmCountryCode.hh:53
int BZWrite(BZFILE *bzf) const
Writes the value to a BZFILE.
int BZRead(BZFILE *bzf)
Reads the value from a BZFILE.
std::istream & Read(std::istream &is)
Reads the value from an istream. Returns the istream.
bool operator==(const Ipv4CountryDbValue &val) const
operator ==