private UnknownIpV6NeighborDiscoveryOption(byte[] rawData, int offset, int length) throws IllegalRawDataException { if (length < 2) { StringBuilder sb = new StringBuilder(100); sb.append("The raw data length must be more than 1. rawData: ") .append(ByteArrays.toHexString(rawData, " ")) .append(", offset: ") .append(offset) .append(", length: ") .append(length); throw new IllegalRawDataException(sb.toString()); } this.type = IpV6NeighborDiscoveryOptionType.getInstance(rawData[offset]); this.length = rawData[1 + offset]; if (length < this.length * 8) { StringBuilder sb = new StringBuilder(100); sb.append("The raw data is too short to build this option(") .append(this.length * 8) .append("). data: ") .append(ByteArrays.toHexString(rawData, " ")) .append(", offset: ") .append(offset) .append(", length: ") .append(length); throw new IllegalRawDataException(sb.toString()); } this.data = ByteArrays.getSubArray(rawData, 2 + offset, this.length * 8 - 2); }
/** * @param snaplen * @param dlt * @param bpfExpression * @param mode * @param netmask * @return a {@link org.pcap4j.core.BpfProgram BpfProgram} object. * @throws PcapNativeException */ public static BpfProgram compileFilter( int snaplen, DataLinkType dlt, String bpfExpression, BpfCompileMode mode, Inet4Address netmask) throws PcapNativeException { if (dlt == null || bpfExpression == null || mode == null || netmask == null) { StringBuilder sb = new StringBuilder(); sb.append("dlt: ") .append(dlt) .append(" bpfExpression: ") .append(bpfExpression) .append(" mode: ") .append(mode) .append(" netmask: ") .append(netmask); throw new NullPointerException(sb.toString()); } bpf_program prog = new bpf_program(); int rc = NativeMappings.pcap_compile_nopcap( snaplen, dlt.value(), prog, bpfExpression, mode.getValue(), ByteArrays.getInt(ByteArrays.toByteArray(netmask), 0)); if (rc < 0) { throw new PcapNativeException("Failed to compile the BPF expression: " + bpfExpression, rc); } return new BpfProgram(prog, bpfExpression); }
private TcpSackOption(byte[] rawData, int offset, int length) throws IllegalRawDataException { if (length < 2) { StringBuilder sb = new StringBuilder(50); sb.append("The raw data length must be more than 1. rawData: ") .append(ByteArrays.toHexString(rawData, " ")) .append(", offset: ") .append(offset) .append(", length: ") .append(length); throw new IllegalRawDataException(sb.toString()); } if (rawData[offset] != kind.value()) { StringBuilder sb = new StringBuilder(100); sb.append("The kind must be: ") .append(kind.valueAsString()) .append(" rawData: ") .append(ByteArrays.toHexString(rawData, " ")) .append(", offset: ") .append(offset) .append(", length: ") .append(length); throw new IllegalRawDataException(sb.toString()); } this.length = rawData[1 + offset]; int lengthFieldAsInt = getLengthAsInt(); if (lengthFieldAsInt < 2) { throw new IllegalRawDataException( "The value of length field must be more than 1 but: " + lengthFieldAsInt); } if ((lengthFieldAsInt - 2) % (INT_SIZE_IN_BYTES * 2) != 0) { StringBuilder sb = new StringBuilder(100); sb.append("The value of length field must be an integer multiple of 8 octets long but: ") .append(lengthFieldAsInt); throw new IllegalRawDataException(sb.toString()); } if (length < lengthFieldAsInt) { StringBuilder sb = new StringBuilder(100); sb.append("rawData is too short. length field: ") .append(lengthFieldAsInt) .append(", rawData: ") .append(ByteArrays.toHexString(rawData, " ")) .append(", offset: ") .append(offset) .append(", length: ") .append(length); throw new IllegalRawDataException(sb.toString()); } for (int i = 2; i < lengthFieldAsInt; i += INT_SIZE_IN_BYTES * 2) { sacks.add( new Sack( ByteArrays.getInt(rawData, i + offset), ByteArrays.getInt(rawData, i + INT_SIZE_IN_BYTES + offset))); } }
private RadiotapDataRate(byte[] rawData, int offset, int length) throws IllegalRawDataException { if (length < LENGTH) { StringBuilder sb = new StringBuilder(200); sb.append("The data is too short to build a RadiotapRate (") .append(LENGTH) .append(" bytes). data: ") .append(ByteArrays.toHexString(rawData, " ")) .append(", offset: ") .append(offset) .append(", length: ") .append(length); throw new IllegalRawDataException(sb.toString()); } this.rate = ByteArrays.getByte(rawData, offset); }
private IcmpV6EchoReplyPacket(byte[] rawData) { this.header = new IcmpV6EchoReplyHeader(rawData); byte[] rawPayload = ByteArrays.getSubArray(rawData, header.length(), rawData.length - header.length()); this.payload = UnknownPacket.newPacket(rawPayload); }
public byte[] getRawData() { byte[] rawData = new byte[length()]; Iterator<TimestampWithAddress> iter = timestampsWithAddresses.iterator(); for (int i = 0; i < rawData.length; i += INT_SIZE_IN_BYTES * 2) { TimestampWithAddress twa = iter.next(); System.arraycopy(ByteArrays.toByteArray(twa.address), 0, rawData, i, INT_SIZE_IN_BYTES); if (twa.timestamp != null) { System.arraycopy( ByteArrays.toByteArray(twa.timestamp), 0, rawData, i + INT_SIZE_IN_BYTES, INT_SIZE_IN_BYTES); } } return rawData; }
/** * A static factory method. This method validates the arguments by {@link * ByteArrays#validateBounds(byte[], int, int)}, which may throw exceptions undocumented here. * * @param rawData rawData * @param offset offset * @param length length * @param cause cause * @return a new IllegalIpV6Option object. */ public static IllegalIpV6Option newInstance( byte[] rawData, int offset, int length, IllegalRawDataException cause) { if (cause == null) { throw new NullPointerException("cause is null."); } ByteArrays.validateBounds(rawData, offset, length); return new IllegalIpV6Option(rawData, offset, length, cause); }
private IcmpV6NeighborAdvertisementHeader(byte[] rawData, int offset, int length) throws IllegalRawDataException { if (length < OPTIONS_OFFSET) { StringBuilder sb = new StringBuilder(120); sb.append("The raw data must be more than ") .append(OPTIONS_OFFSET - 1) .append("bytes") .append(" to build this header. raw data: ") .append(ByteArrays.toHexString(rawData, " ")) .append(", offset: ") .append(offset) .append(", length: ") .append(length); throw new IllegalRawDataException(sb.toString()); } int tmp = ByteArrays.getInt(rawData, R_S_O_RESERVED_OFFSET + offset); this.routerFlag = (tmp & 0x80000000) != 0; this.solicitedFlag = (tmp & 0x40000000) != 0; this.overrideFlag = (tmp & 0x20000000) != 0; this.reserved = 0x1FFFFFFF & tmp; this.targetAddress = ByteArrays.getInet6Address(rawData, TARGET_ADDRESS_OFFSET + offset); this.options = new ArrayList<IpV6NeighborDiscoveryOption>(); int currentOffsetInHeader = OPTIONS_OFFSET; while (currentOffsetInHeader < length) { IpV6NeighborDiscoveryOptionType type = IpV6NeighborDiscoveryOptionType.getInstance(rawData[currentOffsetInHeader + offset]); IpV6NeighborDiscoveryOption newOne; try { newOne = PacketFactories.getFactory( IpV6NeighborDiscoveryOption.class, IpV6NeighborDiscoveryOptionType.class) .newInstance( rawData, currentOffsetInHeader + offset, length - currentOffsetInHeader, type); } catch (Exception e) { break; } options.add(newOne); currentOffsetInHeader += newOne.length(); } }
private IcmpV4RedirectHeader(byte[] rawData, int offset, int length) throws IllegalRawDataException { if (length < ICMPV4_REDIRECT_HEADER_SIZE) { StringBuilder sb = new StringBuilder(80); sb.append("The data is too short to build an ICMPv4 Redirect Header(") .append(ICMPV4_REDIRECT_HEADER_SIZE) .append(" bytes). data: ") .append(ByteArrays.toHexString(rawData, " ")) .append(", offset: ") .append(offset) .append(", length: ") .append(length); throw new IllegalRawDataException(sb.toString()); } this.gatewayInternetAddress = ByteArrays.getInet4Address(rawData, GATEWAY_INTERNET_ADDRESS_OFFSET + offset); }
@Override protected List<byte[]> getRawFields() { List<byte[]> rawFields = new ArrayList<byte[]>(); int tmp = 0x1FFFFFFF & reserved; if (routerFlag) { tmp |= 0x80000000; } if (solicitedFlag) { tmp |= 0x40000000; } if (overrideFlag) { tmp |= 0x20000000; } rawFields.add(ByteArrays.toByteArray(tmp)); rawFields.add(ByteArrays.toByteArray(targetAddress)); for (IpV6NeighborDiscoveryOption o : options) { rawFields.add(o.getRawData()); } return rawFields; }
@Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("[Option Type: ") .append(type) .append("] [Illegal Raw Data: 0x") .append(ByteArrays.toHexString(rawData, "")) .append("] [cause: ") .append(cause) .append("]"); return sb.toString(); }
@Override public byte[] getRawData() { byte[] rawData = new byte[length()]; rawData[0] = kind.value(); rawData[1] = length; int offset = 2; for (Sack sack : sacks) { System.arraycopy( ByteArrays.toByteArray(sack.leftEdge), 0, rawData, offset, INT_SIZE_IN_BYTES); System.arraycopy( ByteArrays.toByteArray(sack.rightEdge), 0, rawData, offset + INT_SIZE_IN_BYTES, INT_SIZE_IN_BYTES); offset += INT_SIZE_IN_BYTES * 2; } return rawData; }
@Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("[Type: ") .append(type) .append("] [Length: ") .append(getLengthAsInt()) .append(" bytes] [Data: 0x") .append(ByteArrays.toHexString(data, "")) .append("]"); return sb.toString(); }
/** * A static factory method. This method validates the arguments by {@link * ByteArrays#validateBounds(byte[], int, int)}, which may throw exceptions undocumented here. * * @param rawData * @param offset * @param length * @return a new IcmpV4RedirectPacket object. * @throws IllegalRawDataException */ public static IcmpV4RedirectPacket newPacket(byte[] rawData, int offset, int length) throws IllegalRawDataException { ByteArrays.validateBounds(rawData, offset, length); IcmpV4RedirectHeader header = new IcmpV4RedirectHeader(rawData, offset, length); int payloadLength = length - header.length(); if (payloadLength > 0) { return new IcmpV4RedirectPacket(header, rawData, offset + header.length(), payloadLength); } else { return new IcmpV4RedirectPacket(header); } }
private IpV4InternetTimestampOptionTimestampsWithAddresses(byte[] rawData, int offset, int length) throws IllegalRawDataException { if ((length % INT_SIZE_IN_BYTES) != 0) { StringBuilder sb = new StringBuilder(100); sb.append("The raw data length must be an integer multiple of 4 octets long." + " rawData: ") .append(ByteArrays.toHexString(rawData, " ")) .append(", offset: ") .append(offset) .append(", length: ") .append(length); throw new IllegalRawDataException(sb.toString()); } this.timestampsWithAddresses = new ArrayList<TimestampWithAddress>(); for (int i = 0; i < length; i += INT_SIZE_IN_BYTES * 2) { Inet4Address address = ByteArrays.getInet4Address(rawData, i + offset); Integer timestamp = null; if (i + INT_SIZE_IN_BYTES < length) { timestamp = ByteArrays.getInt(rawData, i + INT_SIZE_IN_BYTES + offset); } timestampsWithAddresses.add(new TimestampWithAddress(address, timestamp)); } }
/** * A static factory method. This method validates the arguments by {@link * ByteArrays#validateBounds(byte[], int, int)}, which may throw exceptions undocumented here. * * @param rawData * @param offset * @param length * @return a new IpV4InternetTimestampOptionTimestampsWithAddresses object. * @throws IllegalRawDataException */ public static IpV4InternetTimestampOptionTimestampsWithAddresses newInstance( byte[] rawData, int offset, int length) throws IllegalRawDataException { ByteArrays.validateBounds(rawData, offset, length); return new IpV4InternetTimestampOptionTimestampsWithAddresses(rawData, offset, length); }
/** * A static factory method. This method validates the arguments by {@link * ByteArrays#validateBounds(byte[], int, int)}, which may throw exceptions undocumented here. * * @param rawData * @param offset * @param length * @return a new IpV4StrictSourceRouteOption object. * @throws IllegalRawDataException */ public static IpV4StrictSourceRouteOption newInstance(byte[] rawData, int offset, int length) throws IllegalRawDataException { ByteArrays.validateBounds(rawData, offset, length); return new IpV4StrictSourceRouteOption(rawData, offset, length); }
/** * A static factory method. This method validates the arguments by {@link * ByteArrays#validateBounds(byte[], int, int)}, which may throw exceptions undocumented here. * * @param rawData rawData * @param offset offset * @param length length * @return a new UnknownIpV6NeighborDiscoveryOption object. * @throws IllegalRawDataException if parsing the raw data fails. */ public static UnknownIpV6NeighborDiscoveryOption newInstance( byte[] rawData, int offset, int length) throws IllegalRawDataException { ByteArrays.validateBounds(rawData, offset, length); return new UnknownIpV6NeighborDiscoveryOption(rawData, offset, length); }
/** * A static factory method. This method validates the arguments by {@link * ByteArrays#validateBounds(byte[], int, int)}, which may throw exceptions undocumented here. * * @param rawData rawData * @param offset offset * @param length length * @return a new IllegalIpV4InternetTimestampOptionData object. */ public static IllegalIpV4InternetTimestampOptionData newInstance( byte[] rawData, int offset, int length) { ByteArrays.validateBounds(rawData, offset, length); return new IllegalIpV4InternetTimestampOptionData(rawData, offset, length); }
@Override public byte[] getRawData() { return ByteArrays.toByteArray(rate); }
@Override protected List<byte[]> getRawFields() { List<byte[]> rawFields = new ArrayList<byte[]>(); rawFields.add(ByteArrays.toByteArray(gatewayInternetAddress)); return rawFields; }
@Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("[illegal data: ").append(ByteArrays.toHexString(rawData, "")).append("]"); return sb.toString(); }
/** * A static factory method. This method validates the arguments by {@link * ByteArrays#validateBounds(byte[], int, int)}, which may throw exceptions undocumented here. * * @param rawData * @param offset * @param length * @return a new EncryptedPacket object. */ public static EncryptedPacket newPacket(byte[] rawData, int offset, int length) { ByteArrays.validateBounds(rawData, offset, length); return new EncryptedPacket(rawData, offset, length); }
/** * A static factory method. This method validates the arguments by {@link * ByteArrays#validateBounds(byte[], int, int)}, which may throw exceptions undocumented here. * * @param rawData * @param offset * @param length * @return a new IcmpV6NeighborAdvertisementPacket object. * @throws IllegalRawDataException */ public static IcmpV6NeighborAdvertisementPacket newPacket(byte[] rawData, int offset, int length) throws IllegalRawDataException { ByteArrays.validateBounds(rawData, offset, length); return new IcmpV6NeighborAdvertisementPacket(rawData, offset, length); }
private PcapNetworkInterface(pcap_if pif, boolean local) { this.name = pif.name; this.description = pif.description; for (pcap_addr pcapAddr = pif.addresses; pcapAddr != null; pcapAddr = pcapAddr.next) { if (pcapAddr.addr == null && pcapAddr.netmask == null && pcapAddr.broadaddr == null && pcapAddr.dstaddr == null) { logger.warn("Empty pcap_addr on {} ({}). Ignore it.", name, description); continue; } short sa_family = pcapAddr.addr != null ? pcapAddr.addr.getSaFamily() : pcapAddr.netmask != null ? pcapAddr.netmask.getSaFamily() : pcapAddr.broadaddr != null ? pcapAddr.broadaddr.getSaFamily() : pcapAddr.dstaddr != null ? pcapAddr.dstaddr.getSaFamily() /* default */ : Inets.AF_UNSPEC; // Never get here. if (sa_family == Inets.AF_INET) { addresses.add(PcapIpV4Address.newInstance(pcapAddr, sa_family, name)); } else if (sa_family == Inets.AF_INET6) { addresses.add(PcapIpV6Address.newInstance(pcapAddr, sa_family, name)); } else { if (Platform.isLinux() && sa_family == Inets.AF_PACKET) { sockaddr_ll sll = new sockaddr_ll(pcapAddr.addr.getPointer()); byte[] addr = sll.sll_addr; int addrLength = sll.sll_halen & 0xFF; if (addrLength == 6) { linkLayerAddresses.add(ByteArrays.getMacAddress(addr, 0)); } else if (addr.length == 0) { continue; } else { linkLayerAddresses.add( LinkLayerAddress.getByAddress(ByteArrays.getSubArray(addr, 0, addrLength))); } } else if ((Platform.isMac() || Platform.isFreeBSD() || Platform.isOpenBSD()) || Platform.iskFreeBSD() && sa_family == Inets.AF_LINK) { sockaddr_dl sdl = new sockaddr_dl(pcapAddr.addr.getPointer()); byte[] addr = sdl.getAddress(); if (addr.length == 6) { linkLayerAddresses.add(MacAddress.getByAddress(addr)); } else if (addr.length == 0) { continue; } else { linkLayerAddresses.add(LinkLayerAddress.getByAddress(addr)); } } else { logger.warn("{} is not supported address family. Ignore it.", sa_family); } } } if (pif.flags == PCAP_IF_LOOPBACK) { this.loopBack = true; } else { this.loopBack = false; } this.local = local; }
/** * A static factory method. This method validates the arguments by {@link * ByteArrays#validateBounds(byte[], int, int)}, which may throw exceptions undocumented here. * * @param rawData rawData * @param offset offset * @param length length * @return a new RadiotapRate object. * @throws IllegalRawDataException if parsing the raw data fails. */ public static RadiotapDataRate newInstance(byte[] rawData, int offset, int length) throws IllegalRawDataException { ByteArrays.validateBounds(rawData, offset, length); return new RadiotapDataRate(rawData, offset, length); }
/** * A static factory method. This method validates the arguments by {@link * ByteArrays#validateBounds(byte[], int, int)}, which may throw exceptions undocumented here. * * @param rawData * @param offset * @param length * @return a new TcpSackOption object. * @throws IllegalRawDataException */ public static TcpSackOption newInstance(byte[] rawData, int offset, int length) throws IllegalRawDataException { ByteArrays.validateBounds(rawData, offset, length); return new TcpSackOption(rawData, offset, length); }