@Override @XmlElement(name = "value") protected String getValueString() { return String.valueOf(NetUtils.getUnsignedShort(port)); }
public String stringify(Object value) { if (value == null) { return null; } switch (this) { case DL_DST: case DL_SRC: return HexEncode.bytesToHexStringFormat((byte[]) value); case DL_TYPE: case DL_VLAN: return ((Integer) NetUtils.getUnsignedShort((Short) value)).toString(); case NW_SRC: case NW_DST: return ((InetAddress) value).getHostAddress(); case NW_TOS: return ((Integer) NetUtils.getUnsignedByte((Byte) value)).toString(); case TP_SRC: case TP_DST: return ((Integer) NetUtils.getUnsignedShort((Short) value)).toString(); default: break; } return value.toString(); }
/** * Checks if the passed IP address in string form is a valid v4 or v6 * address. The address may specify a mask at the end as "/MMM" * * @param cidr * the v4 or v6 address as IP/MMM * @return */ public static boolean isIPAddressValid(String cidr) { return NetUtils.isIPv4AddressValid(cidr) || NetUtils.isIPv6AddressValid(cidr); }
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (!(obj instanceof NwDst)) { return false; } NwDst other = (NwDst) obj; // Equality to be checked against prefix addresses int thisMaskLen = (this.mask == null) ? ((this.address instanceof Inet4Address) ? 32 : 128) : NetUtils .getSubnetMaskLength(this.mask); int otherMaskLen = (other.mask == null) ? ((other.address instanceof Inet4Address) ? 32 : 128) : NetUtils .getSubnetMaskLength(other.mask); return NetUtils.getSubnetPrefix(address, thisMaskLen).equals( NetUtils.getSubnetPrefix(other.address, otherMaskLen)); } }
if (isAny(testAddress) || isAny(filterAddress)) { return false; .getSubnetMaskLength(testMask); int filterMaskLen = (filterMask == null) ? ((testAddress instanceof Inet4Address) ? 32 : 128) : NetUtils .getSubnetMaskLength(filterMask); InetAddress prefix1 = getSubnetPrefix(testAddress, filterMaskLen); InetAddress prefix2 = getSubnetPrefix(filterAddress, filterMaskLen); return (!prefix1.equals(prefix2));
.isZeroMAC(ofMatch.getDataLayerSource())) { byte srcMac[] = ofMatch.getDataLayerSource(); salMatch.setField(new MatchField(MatchType.DL_SRC, && !NetUtils.isZeroMAC(ofMatch .getDataLayerDestination())) { byte dstMac[] = ofMatch.getDataLayerDestination(); .getInetAddress(ofMatch.getNetworkSource()), NetUtils.getInetNetworkMask( ofMatch.getNetworkSourceMaskLen(), false)); NetUtils.getInetAddress(ofMatch .getNetworkDestination()), NetUtils.getInetNetworkMask( ofMatch.getNetworkDestinationMaskLen(), false)); int dscp = NetUtils.getUnsignedByte(ofMatch .getNetworkTypeOfService()); byte tos = (byte) (dscp >> 2); .isZeroMAC(ofMatch.getDataLayerSource())) { byte srcMac[] = v6Match.getDataLayerSource(); salMatch.setField(new MatchField(MatchType.DL_SRC, && !NetUtils.isZeroMAC(ofMatch .getDataLayerDestination())) {
InetAddress dIP = NetUtils.getInetAddress(pkt.getDestinationAddress()); if (dIP == null) { return;
InetAddress address = NetUtils.getInetAddress(match.getNetworkSource()); InetAddress mask = NetUtils.getInetNetworkMask(match.getNetworkSourceMaskLen(), false); this.setNetworkSource(address, mask); } else { InetAddress address = NetUtils.getInetAddress(match.getNetworkDestination()); InetAddress mask = NetUtils.getInetNetworkMask(match.getNetworkDestinationMaskLen(), false); this.setNetworkDestination(address, mask); } else { && !NetUtils.isZeroMAC(match.getDataLayerSource())) { this.setDataLayerSource(match.getDataLayerSource(), null); } else { && !NetUtils.isZeroMAC(match.getDataLayerDestination())) { this.setDataLayerDestination(match.getDataLayerDestination(), null); } else {
if (NetUtils.isIPv4AddressValid(nwSrc)) { ipsrctype = EtherIPType.V4; } else if (NetUtils.isIPv6AddressValid(nwSrc)) { ipsrctype = EtherIPType.V6; } else { if (NetUtils.isIPv4AddressValid(nwDst)) { ipdsttype = EtherIPType.V4; } else if (NetUtils.isIPv6AddressValid(nwDst)) { ipdsttype = EtherIPType.V6; } else { if (!NetUtils.isIPv4AddressValid(sstr.group(1))) { return new Status(StatusCode.BADREQUEST, String.format("IP source address %s is not valid", sstr.group(1))); if (!NetUtils.isIPv4AddressValid(sstr.group(1))) { return new Status(StatusCode.BADREQUEST, String.format( "IP destination address %s is not valid", sstr.group(1))); if (!NetUtils.isIPAddressValid(sstr.group(1))) { return new Status(StatusCode.BADREQUEST, String.format( "IP destination address %s is not valid", sstr.group(1)));
public int hashCode(Object v, Object m) { final int prime = 31; int result = 1; result = prime * result + this.calculateConsistentHashCode(); switch (this) { case DL_SRC: case DL_DST: result = prime * result + ((v == null)? 0 : NetUtils.byteArray4ToInt((byte[])v)); result = prime * result + ((m == null)? 0 : NetUtils.byteArray4ToInt((byte[])m)); break; case NW_SRC: case NW_DST: // Hash code has to take into account only prefix address InetAddress ip = (InetAddress) v; int maskLen = (m == null) ? ((ip instanceof Inet4Address) ? 32 : 128) : NetUtils .getSubnetMaskLength((InetAddress) m); InetAddress prefix = NetUtils.getSubnetPrefix(ip, maskLen); result = prime * result + ((v == null)? 0 : prefix.hashCode()); break; default: result = prime * result + ((v == null)? 0 : v.hashCode()); result = prime * result + ((m == null)? 0 : m.hashCode()); } return result; } public boolean equalValues(Object a, Object b) {
public void setNetworkDestination(InetAddress address, InetAddress mask) { if (address instanceof Inet6Address) { this.nwDst = (Inet6Address) address; if (mask == null) { this.nwDstState = MatchFieldState.MATCH_FIELD_ONLY; this.match_len += (address instanceof Inet6Address) ? 20 : 8; } else { this.dstIPv6SubnetMaskbits = (short)NetUtils.getSubnetMaskLength(mask); this.nwDstState = MatchFieldState.MATCH_FIELD_WITH_MASK; this.match_len += (address instanceof Inet6Address) ? 36 : 12; } } else { this.setNetworkDestination(NetUtils.byteArray4ToInt(address.getAddress())); this.wildcards ^= (((1 << 6) - 1) << 14); if (mask == null) { this.nwDstState = MatchFieldState.MATCH_FIELD_ONLY; this.match_len += 8; } else { this.nwDstState = MatchFieldState.MATCH_FIELD_WITH_MASK; this.match_len += 12; this.wildcards |= ((32 - NetUtils.getSubnetMaskLength(mask)) << 14); } } }
@Override public boolean isValid() { int intProtocol = NetUtils.getUnsignedByte(protocol); return intProtocol >= 0 && intProtocol <= MAX; }
if (!NetUtils.isIPv4AddressValid(ipAddress)) { throw new NotAcceptableException("Invalid ip address "+ipAddress);
private Status validateSubnetAddress() { if (!NetUtils.isIPAddressValid(subnet)) { return new Status(StatusCode.BADREQUEST, String.format("Invalid Subnet configuration: Invalid address: %s", subnet)); } if ((this.getIPMaskLen() == 0) || (this.getIPMaskLen() == 32)) { return new Status(StatusCode.BADREQUEST, String.format("Invalid Subnet configuration: Invalid mask: /%s", this.getIPMaskLen())); } //checks that address doesn't start with 0 or 255 String address = subnet.split("/")[0]; if (address.startsWith("0.") || address.startsWith("255.")) { return new Status(StatusCode.BADREQUEST, String.format("Invalid Subnet configuration: Invalid address: %s", address)); } byte[] bytePrefix = NetUtils.getSubnetPrefix(this.getIPAddress(), this.getIPMaskLen()).getAddress(); long prefix = BitBufferHelper.getLong(bytePrefix); if (prefix == 0) { return new Status(StatusCode.BADREQUEST, "Invalid network source address: subnet zero"); } //check that host is not set to all 0's or 1's long hostAddress = BitBufferHelper.getLong(this.getIPAddress().getAddress()) - prefix; if (hostAddress == 0 || hostAddress == Math.pow(2, 32-this.getIPMaskLen()) - 1) { return new Status(StatusCode.BADREQUEST, String.format("Invalid subnet gateway address: /%s", subnet)); } return new Status(StatusCode.SUCCESS); }
/** * Returns the prefix size in bits of the specified subnet mask. Example: * For the subnet mask 255.255.255.128 it returns 25 while for 255.0.0.0 it * returns 8. If the passed subnetMask object is null, 0 is returned * * @param subnetMask * the subnet mask as InetAddress * @return the prefix length as number of bits */ public static int getSubnetMaskLength(InetAddress subnetMask) { return subnetMask == null ? 0 : NetUtils.getSubnetMaskLength(subnetMask.getAddress()); }
public int valueHashCode(Object o) { if (o == null) { return 0; } switch (this) { case DL_SRC: case DL_DST: return NetUtils.byteArray4ToInt((byte[])o); default: return o.hashCode(); } }
public Inet6Address getNetworkDestinationMask() { return (this.nwDstState == MatchFieldState.MATCH_FIELD_WITH_MASK) ? (Inet6Address) NetUtils.getInetNetworkMask( this.dstIPv6SubnetMaskbits, true) : null; }
public HostNodeConnector toHostNodeConnector() { try { NodeConnector n = this.getPort(); InetAddress ip = InetAddress.getByAddress(NetUtils.intToByteArray4(this.getIpv4Address())); byte[] macAddr = NetUtils.longToByteArray6(this.getMacAddress()); HostNodeConnector nc = new HostNodeConnector(macAddr, ip, n, (short) 0); return nc; } catch (Exception e) { return null; } }
InetAddress ip = NetUtils.parseInetAddress(parts[0]); InetAddress mask = null; int maskLen = 0; maskLen = (ip instanceof Inet6Address) ? 128 : 32; mask = NetUtils.getInetNetworkMask(maskLen, ip instanceof Inet6Address); match.setField(MatchType.NW_SRC, ip, mask); InetAddress ip = NetUtils.parseInetAddress(parts[0]); InetAddress mask = null; int maskLen = 0; maskLen = (ip instanceof Inet6Address) ? 128 : 32; mask = NetUtils.getInetNetworkMask(maskLen, ip instanceof Inet6Address); match.setField(MatchType.NW_DST, ip, mask);
byte[] dataLayerAddress = NetUtils.getBroadcastMACAddr(); host = new HostNodeConnector(dataLayerAddress, decodeIPFromId(arphost.getHostId()), nodeConnector, (short) 0);