d4198bc72961f5232be036e13b7503f33ea7629a
[shibboleth/cpp-sp.git] / shibsp / util / IPRange.h
1 /**
2  * Licensed to the University Corporation for Advanced Internet
3  * Development, Inc. (UCAID) under one or more contributor license
4  * agreements. See the NOTICE file distributed with this work for
5  * additional information regarding copyright ownership.
6  *
7  * UCAID licenses this file to you under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */
20
21 /**
22  * @file shibsp/util/IPRange.h
23  * 
24  * Represents a range of IP addresses.
25  */
26
27 #ifndef __shibsp_iprange_h__
28 #define __shibsp_iprange_h__
29
30 #include <shibsp/base.h>
31
32 #include <bitset>
33
34 #ifdef WIN32
35 # include <winsock.h>
36 #elif defined(SHIBSP_HAVE_SYS_SOCKET_H)
37 # include <sys/socket.h>
38 #endif
39
40 namespace shibsp {
41
42 #if defined (_MSC_VER)
43     #pragma warning( push )
44     #pragma warning( disable : 4251 )
45 #endif
46
47     /**
48      * Represents a range of IP addresses.
49      */
50     class SHIBSP_API IPRange
51     {
52     public:
53         /**
54          * Constructor.
55          *
56          * @param address address to base the range on; may be the network address or the
57          *                address of a host within the network
58          * @param maskSize the number of bits in the netmask
59          */
60         IPRange(const std::bitset<32>& address, int maskSize);
61
62         /**
63          * Constructor.
64          *
65          * @param address address to base the range on; may be the network address or the
66          *                address of a host within the network
67          * @param maskSize the number of bits in the netmask
68          */
69         IPRange(const std::bitset<128>& address, int maskSize);
70
71         /**
72          * Determines whether the given address is contained in the IP range.
73          *
74          * @param address the address to check
75          *
76          * @return true iff the address is in the range
77          */
78         bool contains(const char* address) const;
79
80         /**
81          * Determines whether the given address is contained in the IP range.
82          *
83          * @param address the address to check
84          *
85          * @return true iff the address is in the range
86          */
87         bool contains(const struct sockaddr* address) const;
88
89         /**
90          * Parses a CIDR block definition in to an IP range.
91          *
92          * @param cidrBlock the CIDR block definition
93          *
94          * @return the resultant IP range
95          */
96         static IPRange parseCIDRBlock(const char* cidrBlock);
97
98     private:
99         /** Number of bits within the address.  32 bits for IPv4 address, 128 bits for IPv6 addresses. */
100         int m_addressLength;
101
102         /** The IP network address for the range. */
103         std::bitset<32> m_network4;
104
105         /** The netmask for the range. */
106         std::bitset<32> m_mask4;
107
108         /** The IP network address for the range. */
109         std::bitset<128> m_network6;
110
111         /** The netmask for the range. */
112         std::bitset<128> m_mask6;
113     };
114
115 #if defined (_MSC_VER)
116     #pragma warning( pop )
117 #endif
118
119 };
120
121 #endif /* __shibsp_iprange_h__ */