Checks if a netmask is expressable as a prefixlen.
(netmask, masklen)
| 1243 | |
| 1244 | |
| 1245 | def _checkNetmask(netmask, masklen): |
| 1246 | """Checks if a netmask is expressable as a prefixlen.""" |
| 1247 | |
| 1248 | num = long(netmask) |
| 1249 | bits = masklen |
| 1250 | |
| 1251 | # remove zero bits at the end |
| 1252 | while (num & 1) == 0 and bits != 0: |
| 1253 | num = num >> 1 |
| 1254 | bits -= 1 |
| 1255 | if bits == 0: |
| 1256 | break |
| 1257 | # now check if the rest consists only of ones |
| 1258 | while bits > 0: |
| 1259 | if (num & 1) == 0: |
| 1260 | raise ValueError, "Netmask %s can't be expressed as an prefix." % (hex(netmask)) |
| 1261 | num = num >> 1 |
| 1262 | bits -= 1 |
| 1263 | |
| 1264 | |
| 1265 | def _checkNetaddrWorksWithPrefixlen(net, prefixlen, version): |
no outgoing calls
no test coverage detected