MCPcopy Create free account
hub / github.com/Icinga/icinga2 / ParseIpMask

Function ParseIpMask

lib/base/utility.cpp:178–218  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

176}
177
178static void ParseIpMask(const String& ip, char mask[16], int *bits)
179{
180 String::SizeType slashp = ip.FindFirstOf("/");
181 String uip;
182
183 if (slashp == String::NPos) {
184 uip = ip;
185 *bits = 0;
186 } else {
187 uip = ip.SubStr(0, slashp);
188 *bits = Convert::ToLong(ip.SubStr(slashp + 1));
189 }
190
191 int proto;
192
193 if (!ParseIp(uip, mask, &proto))
194 BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid IP address specified."));
195
196 if (proto == AF_INET) {
197 if (*bits > 32 || *bits < 0)
198 BOOST_THROW_EXCEPTION(std::invalid_argument("Mask must be between 0 and 32 for IPv4 CIDR masks."));
199
200 *bits += 96;
201 }
202
203 if (slashp == String::NPos)
204 *bits = 128;
205
206 if (*bits > 128 || *bits < 0)
207 BOOST_THROW_EXCEPTION(std::invalid_argument("Mask must be between 0 and 128 for IPv6 CIDR masks."));
208
209 for (int i = 0; i < 16; i++) {
210 int lbits = std::max(0, *bits - i * 8);
211
212 if (lbits >= 8)
213 continue;
214
215 if (mask[i] & (0xff >> lbits))
216 BOOST_THROW_EXCEPTION(std::invalid_argument("Masked-off bits must all be zero."));
217 }
218}
219
220static bool IpMaskCheck(char addr[16], char mask[16], int bits)
221{

Callers 1

CidrMatchMethod · 0.85

Calls 3

ParseIpFunction · 0.85
FindFirstOfMethod · 0.80
SubStrMethod · 0.80

Tested by

no test coverage detected