public boolean containsIp(@Nonnull Ip ip) { long wildcardIpAsLong = getIp().asLong(); long wildcardMask = getWildcard().asLong(); long ipAsLong = ip.asLong(); long maskedIpAsLong = ipAsLong | wildcardMask; long maskedWildcard = wildcardIpAsLong | wildcardMask; return maskedIpAsLong == maskedWildcard; }
@Override public void enterO_area(O_areaContext ctx) { Ip areaIp = Ip.parse(ctx.area.getText()); Map<Long, OspfArea> areas = _currentRoutingInstance.getOspfAreas(); _currentArea = areas.computeIfAbsent(areaIp.asLong(), OspfArea::new); }
void addPrefix(Prefix prefix, String device) { int prefixLength = prefix.getPrefixLength(); long bits = prefix.getStartIp().asLong(); Set<String> devices = new HashSet<>(); devices.add(device); _root.addPrefix(prefix, devices, bits, prefixLength, 0); } }
public IpWildcard(Ip address, Ip wildcardMask) { // Canonicalize the address before passing it to parent, so that #equals works. super(Ip.create(address.asLong() & ~wildcardMask.asLong()), wildcardMask); if (!wildcardMask.valid()) { throw new BatfishException("Invalid wildcard: " + wildcardMask); } }
Ip computeInstancesIfaceIp() { Long generatedIp = _cidrBlock.getStartIp().asLong() + 1L; _allocatedIps.add(generatedIp); _lastGeneratedIp = generatedIp; return Ip.create(generatedIp); }
/** Retrieve an immutable copy of the routes currently available for the given prefix. */ @Nonnull Set<R> getRoutes(Prefix p) { RibTreeNode<R> node = findRouteNode(p.getStartIp().asLong(), p.getPrefixLength(), 0); if (node == null) { return ImmutableSet.of(); } return ImmutableSet.copyOf(node._routes); }
/** * Converts an IPv4 address into a {@link BitSet} useful for prefix matching. The highest bit of * the address is the lowest bit of the bitset: the address 128.0.0.0 when converted to a {@link * BitSet} has only the lowest bit set. */ // visible for testing static BitSet getAddressBits(Ip address) { return BitSet.valueOf(new long[] {Integer.reverse((int) address.asLong()) & 0xffffffffL}); }
@Override public void enterRo_area(Ro_areaContext ctx) { long area; if (ctx.area_int != null) { area = toLong(ctx.area_int); } else if (ctx.area_ip != null) { area = toIp(ctx.area_ip).asLong(); } else { throw new BatfishException("Missing area"); } _currentOspfArea = area; }
/** * Returns the last ip in the prefix that is not a broadcast address. When the prefix is /32 or * /31, returns the getEndIp(), otherwise, returns the ip before getEndIp(). */ public Ip getLastHostIp() { if (_prefixLength >= Prefix.MAX_PREFIX_LENGTH - 1) { return getEndIp(); } else { Ip broadcastIp = getEndIp(); return Ip.create(broadcastIp.asLong() - 1); } }
public void addPrefix(Prefix prefix) { int prefixLength = prefix.getPrefixLength(); long bits = prefix.getStartIp().asLong(); _root.addPrefix(prefix, bits, prefixLength, 0); }
@Override public Boolean visitShiftIpAddressIntoSubnet(ShiftIpAddressIntoSubnet step) { IpField field = step.getIpField(); Ip oldValue = get(field); Prefix targetSubnet = step.getSubnet(); Prefix currentSubnetPrefix = Prefix.create(oldValue, targetSubnet.getPrefixLength()); long offset = oldValue.asLong() - currentSubnetPrefix.getStartIp().asLong(); Ip newValue = Ip.create(targetSubnet.getStartIp().asLong() + offset); return set(step.getType(), field, oldValue, newValue); } }
@Override public void exitRo_area_stub(Ro_area_stubContext ctx) { OspfProcess proc = _currentOspfProcess; long area = (ctx.area_int != null) ? toLong(ctx.area_int) : toIp(ctx.area_ip).asLong(); StubSettings settings = proc.getStubs().computeIfAbsent(area, a -> new StubSettings()); if (ctx.no_summary != null) { settings.setNoSummary(true); } }
RibDelta<R> clearRoutes(Prefix prefix) { long bits = prefix.getStartIp().asLong(); RibTreeNode<R> node = findRouteNode(bits, prefix.getPrefixLength(), 0); if (node == null) { return null; } RibDelta<R> delta = RibDelta.<R>builder().remove(node._routes, REPLACE).build(); node._routes.clear(); return delta; }
@Override public Void visitAssignIpAddressFromPool( AssignIpAddressFromPool assignIpAddressFromPool) { IpField ipField = assignIpAddressFromPool.getIpField(); BDDInteger var = getIpSpaceToBDD(ipField).getBDDInteger(); BDD bdd = var.geq(assignIpAddressFromPool.getPoolStart().asLong()) .and(var.leq(assignIpAddressFromPool.getPoolEnd().asLong())); ranges.merge(ipField, bdd, BDD::or); return null; }
public boolean includesPrefixRange(PrefixRange argPrefixRange) { Prefix prefix = getPrefix(); SubRange lengthRange = getLengthRange(); int prefixLength = prefix.getPrefixLength(); long maskedPrefixAsLong = prefix.getStartIp().getNetworkAddress(prefixLength).asLong(); Prefix argPrefix = argPrefixRange.getPrefix(); SubRange argLengthRange = argPrefixRange.getLengthRange(); long argMaskedPrefixAsLong = argPrefix.getStartIp().getNetworkAddress(prefixLength).asLong(); return maskedPrefixAsLong == argMaskedPrefixAsLong && lengthRange.getStart() <= argLengthRange.getStart() && lengthRange.getEnd() >= argLengthRange.getEnd(); }
@Test public void testGetBitAtPosition() { assertThat(getBitAtPosition(0L, 0), is(false)); assertThat(getBitAtPosition(0L, 31), is(false)); assertThat(getBitAtPosition(1L, 31), is(true)); assertThat(getBitAtPosition(0xFF000000L, 7), is(true)); assertThat(getBitAtPosition(0xFF000000L, 8), is(false)); for (int i = 0; i < 32; i++) { assertThat(getBitAtPosition(Ip.MAX.asLong(), i), equalTo(true)); } }