HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NET_IpValidator.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: NET_IpValidator.h
7  *
8  * COMMENTS:
9  *
10  */
11 
12 #ifndef __NET_IPVALIDATOR_H__
13 #define __NET_IPVALIDATOR_H__
14 
15 #include "NET_API.h"
16 
17 #include <UT/UT_Array.h>
18 #include <UT/UT_IpAddress.h>
19 #include <UT/UT_StringArray.h>
20 #include <UT/UT_StringHolder.h>
21 #include <UT/UT_WorkBuffer.h>
22 
23 #include "NET_ServerInfo.h"
24 
25 /// Validates ip addresses based on a set of standard ip subnet masks and custom
26 /// format ip masks. Note: the custom format has been deprecated for the
27 /// standard format.
29 {
30 public:
32  myCustomMask(),
33  myMasksV4(),
34  myMasksV6(),
35  myServer(server)
36  {}
37  NET_IpValidator(const NET_IServerInfo& server, const UT_StringArray& masks) :
38  myServer(server)
39  {
40  setMasks(masks);
41  }
42 
43  /// Check if the provided ip matches one of the ip masks.
44  bool match(const UT_IpAddressV4& ip) const;
45  /// Check if the provided ip matches one of the ip masks.
46  bool match(const UT_IpAddressV6& ip) const
47  {
48  if (!myMasksV6.isEmpty())
49  {
50  UT_IpNetworkV6 host_network(ip, 128);
51  for (auto&& net : myMasksV6)
52  {
53  // If the network address is a host address then simply check
54  // the two addresses.
55  if (net.isHost())
56  {
57  if (net.address() == ip)
58  return true;
59  }
60  else if (host_network.isSubnetOf(net))
61  return true;
62  }
63  }
64  // If no v6 masks were provided and the address is not v4 mapped then
65  // report it as a match.
66  else if (!ip.isV4Mapped())
67  {
68  return true;
69  }
70 
71  // If the ip6 is an ip4 mapped to ip6 then try the ipv6 masks and the
72  // fallback to converting the address to ip4. This allows both ip4 and
73  // ip6 masks to be given for ip4 addresses.
74  if (ip.isV4Mapped())
75  {
76  return match(ip.toV4());
77  }
78 
79  return false;
80  }
81  /// Check if the provided ip matches on of the ip masks based on the ip
82  /// address type.
83  bool match(const UT_IpAddress& ip) const
84  {
85  if (ip.isV4())
86  return match(ip.toV4());
87  return match(ip.toV6());
88  }
89  /// Set the ip masks to the provided list. Note that the previous list
90  /// of masks are cleared.
91  void setMasks(const UT_StringArray& masks)
92  {
93  myCustomMask.clear();
94  myMasksV4.clear();
95  myMasksV6.clear();
96  addMasks(masks);
97  }
98  /// Set the ip masks to the provided mask. Note that the previous list
99  /// of masks are cleared.
101  {
102  myCustomMask.clear();
103  myMasksV4.clear();
104  myMasksV6.clear();
105  addMask(mask);
106  }
107  /// Add the provided mask to the current list of masks.
108  void addMask(const UT_StringHolder& mask);
109  /// Add the provided list of masks to the current list of masks.
110  void addMasks(const UT_StringArray& masks)
111  {
112  for (auto&& mask : masks)
113  addMask(mask);
114  }
116  {
117  UT_WorkBuffer wbuf;
118  for (auto&& mask : myMasksV4)
119  {
120  if (!wbuf.isEmpty())
121  wbuf.append(',');
122  wbuf.append(mask.toString());
123  }
124  if (!myCustomMask.isEmpty())
125  {
126  if (!wbuf.isEmpty())
127  wbuf.append(',');
128  wbuf.append(myCustomMask);
129  }
130  UT_StringHolder str(std::move(wbuf));
131  return str;
132  }
134  {
135  UT_WorkBuffer wbuf;
136  for (auto&& mask : myMasksV6)
137  {
138  if (!wbuf.isEmpty())
139  wbuf.append(',');
140  wbuf.append(mask.toString());
141  }
142  UT_StringHolder str(std::move(wbuf));
143  return str;
144  }
145 
146 private:
147  // Old custom format for ip mask which is only supported for ipv4
148  UT_StringHolder myCustomMask;
149 
150  UT_Array<UT_IpNetworkV4> myMasksV4;
151  UT_Array<UT_IpNetworkV6> myMasksV6;
152 
153  const NET_IServerInfo& myServer;
154 };
155 
156 #endif // __NET_IPVALIDATOR_H__
157 
void setMask(const UT_StringHolder &mask)
SYS_NO_DISCARD_RESULT bool isV4Mapped() const
Definition: UT_IpAddress.h:188
bool match(const UT_IpAddressV6 &ip) const
Check if the provided ip matches one of the ip masks.
void addMasks(const UT_StringArray &masks)
Add the provided list of masks to the current list of masks.
This represents a Ipv4 address.
Definition: UT_IpAddress.h:34
NET_IpValidator(const NET_IServerInfo &server)
void setMasks(const UT_StringArray &masks)
Adapter to retrieve basic server information.
#define NET_API
Definition: NET_API.h:9
This represents either an Ipv4 address or an Ipv6 address.
Definition: UT_IpAddress.h:230
SYS_NO_DISCARD_RESULT bool isSubnetOf(const UT_IpNetworkV6 &net) const
SYS_NO_DISCARD_RESULT UT_IpAddressV4 toV4() const
Definition: UT_IpAddress.h:218
GLint GLuint mask
Definition: glcorearb.h:124
SYS_FORCE_INLINE bool isEmpty() const
SYS_NO_DISCARD_RESULT UT_IpAddressV4 toV4() const
Definition: UT_IpAddress.h:352
SYS_NO_DISCARD_RESULT bool isV4() const
Definition: UT_IpAddress.h:318
UT_StringHolder toStringV6() const
This represents a Ipv6 address.
Definition: UT_IpAddress.h:137
SYS_NO_DISCARD_RESULT UT_IpAddressV6 toV6() const
Definition: UT_IpAddress.h:358
bool match(const UT_IpAddress &ip) const
SYS_FORCE_INLINE void append(char character)
UT_StringHolder toStringV4() const
NET_IpValidator(const NET_IServerInfo &server, const UT_StringArray &masks)