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.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 * @file shibsp/util/IPRange.h
24 * Represents a range of IP addresses.
27 #ifndef __shibsp_iprange_h__
28 #define __shibsp_iprange_h__
30 #include <shibsp/base.h>
35 # include <winsock2.h>
36 #elif defined(SHIBSP_HAVE_SYS_SOCKET_H)
37 # include <sys/socket.h>
42 #if defined (_MSC_VER)
43 #pragma warning( push )
44 #pragma warning( disable : 4251 )
48 * Represents a range of IP addresses.
50 class SHIBSP_API IPRange
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
60 IPRange(const std::bitset<32>& address, int maskSize);
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
69 IPRange(const std::bitset<128>& address, int maskSize);
72 * Determines whether the given address is contained in the IP range.
74 * @param address the address to check
76 * @return true iff the address is in the range
78 bool contains(const char* address) const;
81 * Determines whether the given address is contained in the IP range.
83 * @param address the address to check
85 * @return true iff the address is in the range
87 bool contains(const struct sockaddr* address) const;
90 * Parses a CIDR block definition in to an IP range.
92 * @param cidrBlock the CIDR block definition
94 * @return the resultant IP range
96 static IPRange parseCIDRBlock(const char* cidrBlock);
99 /** Number of bits within the address. 32 bits for IPv4 address, 128 bits for IPv6 addresses. */
102 /** The IP network address for the range. */
103 std::bitset<32> m_network4;
105 /** The netmask for the range. */
106 std::bitset<32> m_mask4;
108 /** The IP network address for the range. */
109 std::bitset<128> m_network6;
111 /** The netmask for the range. */
112 std::bitset<128> m_mask6;
115 #if defined (_MSC_VER)
116 #pragma warning( pop )
121 #endif /* __shibsp_iprange_h__ */