1 /*
2 
3                           Firewall Builder
4 
5                  Copyright (C) 2008 NetCitadel, LLC
6 
7   Author:  Vadim Kurland     vadim@fwbuilder.org
8 
9   $Id$
10 
11 
12   This program is free software which we release under the GNU General Public
13   License. You may redistribute and/or modify this program under the terms
14   of that license as published by the Free Software Foundation; either
15   version 2 of the License, or (at your option) any later version.
16 
17   This program is distributed in the hope that it will be useful,
18   but WITHOUT ANY WARRANTY; without even the implied warranty of
19   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20   GNU General Public License for more details.
21 
22   To get a copy of the GNU General Public License, write to the Free Software
23   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24 
25 */
26 
27 /*
28  * This class is a holder of a pair address / netmask.
29  * It can act both as a container for the address/netmask configuration
30  * data (such as for an interface) or as a network.
31  *
32  * TODO(vadim): need better name. InetNetwork ? InetAddrMaskPair ?
33  */
34 
35 #ifndef __INETADDRMASK_HH_FLAG__
36 #define __INETADDRMASK_HH_FLAG__
37 
38 #include <string>
39 #include <vector>
40 
41 #ifndef _WIN32
42 #  include <sys/types.h>
43 #  include <sys/socket.h>
44 #  include <netinet/in.h>
45 #  include <arpa/inet.h>
46 #else
47 #  include <winsock2.h>
48 #endif
49 
50 #include "fwbuilder/FWException.h"
51 #include "fwbuilder/InetAddr.h"
52 
53 namespace libfwbuilder
54 {
55 
56     class InetAddrMask;
57 
58     std::vector<InetAddrMask> getOverlap(const InetAddrMask &n1,
59                                          const InetAddrMask &n2);
60     std::vector<InetAddrMask> substract(const InetAddrMask &n1,
61                                         const InetAddrMask &n2);
62     std::vector<InetAddrMask> convertAddressRange(const InetAddr &start,
63                                                   const InetAddr &end);
64     bool _convert_range_to_networks(const InetAddr &start,
65                                     const InetAddr &end,
66                                     std::vector<InetAddrMask> &res);
67 
68 class InetAddrMask
69 {
70 
71 protected:
72 
73     InetAddr* address;
74     InetAddr* netmask;
75     InetAddr* broadcast_address;
76     InetAddr* network_address;
77     InetAddr* last_host;
78 
79     explicit InetAddrMask(bool no_address);
80 
81 public:
82 
83     InetAddrMask();
84     InetAddrMask(const InetAddr&, const InetAddr&);
85     InetAddrMask(const std::string &s) throw(FWException);
86     InetAddrMask(const InetAddrMask&);
87     virtual ~InetAddrMask();
88     void setNetworkAndBroadcastAddress();
89 
getAddressPtr()90     virtual const InetAddr* getAddressPtr() const { return address; }
getNetmaskPtr()91     virtual const InetAddr* getNetmaskPtr() const { return netmask; }
92 
getNetworkAddressPtr()93     virtual const InetAddr* getNetworkAddressPtr() const {
94         return network_address; }
getBroadcastAddressPtr()95     virtual const InetAddr* getBroadcastAddressPtr() const {
96         return broadcast_address; }
97 
getFirstHostPtr()98     const InetAddr* getFirstHostPtr() const {
99         return network_address;}
getLastHostPtr()100     const InetAddr* getLastHostPtr() const{
101         return last_host;}
102 
103     virtual void setAddress(const InetAddr &a);
104     virtual void setNetmask(const InetAddr &nm);
105 
106     virtual unsigned int dimension()  const;
107 
108     bool isAny();
109 
110     InetAddrMask& operator=(const InetAddrMask &o);
111     bool operator<(const InetAddrMask &b);
112 
113     friend bool operator==(const InetAddrMask &a, const InetAddrMask &b);
114     friend bool operator<(const InetAddrMask &a, const InetAddrMask &b);
115 
toString()116     virtual std::string toString() const
117     {
118         return address->toString()+"/"+netmask->toString();
119     }
120 
121     bool belongs(const InetAddr &) const;
122 
123     /**
124      * calculates overlapping part of two networks n1 and
125      * n2. Overlapping part is defined as in sets: if we think of
126      * networks as sets of addresses, then intersection contains all
127      * addresses that belong to both networks
128      */
129     friend std::vector<InetAddrMask> getOverlap(const InetAddrMask &n1,
130                                                 const InetAddrMask &n2);
131 
132     /**
133      * substract network n2 from the network n1. The meaning of this
134      * operation is opposite to getOverlap: it returns all addresses
135      * that belong to n1 but do not belong to n2
136      */
137     friend std::vector<InetAddrMask> substract(const InetAddrMask &n1,
138                                                const InetAddrMask &n2);
139 
140     /**
141      * converts address range (defined by its start and end) to a
142      * bunch of networks
143      */
144     friend std::vector<InetAddrMask> convertAddressRange(const InetAddr &start,
145                                                          const InetAddr &end);
146 };
147 
148 }
149 
150 #endif
151 
152