/** * Convert ip segment to hex. * * @param ipSegment * the ip segment * @return the string */ public static String convertIpSegmentToHex(int ipSegment) { return convertToHex(ipSegment, 2); }
/** * Gets the json doc. * * * @return the json doc */ public String getJsonDoc() { return getJsonDocUsingSBAppend(); }
/** * Gets the json doc. * * * @return the json doc */ public String getJsonIndexDoc() { return getJsonIndexDocUsingSBAppend(); }
int i = 1; for (byte[] pcap : pcaps) { is = new PcapByteInputStream(pcap); if (i == 1) { os = new PcapByteOutputStream(unsortedBaos, is.getGlobalHeader()); writePacket(is, os); i++; closeInput(is); unsortedBaos.close(); closeOutput(os); sort(baos, unsortedBaos.toByteArray());
/** * Gets the session key. * * @param srcIp * the src ip * @param dstIp * the dst ip * @param protocol * the protocol * @param srcPort * the src port * @param dstPort * the dst port * @return the session key */ public static String getSessionKey(String srcIp, String dstIp, String protocol, String srcPort, String dstPort) { return getSessionKey(srcIp, dstIp, protocol, srcPort, dstPort, null, null); }
/** * Write packet. * * @param is * the is * @param os * the os * * @throws IOException * Signals that an I/O exception has occurred. */ private static void writePacket(PcapByteInputStream is, PcapByteOutputStream os) throws IOException { PcapPacket packet = null; try { while (true) { packet = is.getPacket(); if (packet == null) { break; } os.write(packet); } } catch (EOFException e) { //LOG.debug("Ignorable exception in writePacket", e); } }
/** * Merge byte array1 with byte array2, and write to output byte array. It * doesn't hurt original pcap dump byte arrays. * * @param baos * the baos * @param pcaps * the pcaps * * @throws IOException * if there are no source byte arrays, have no read and/or write * permissions, or anything else. */ public static void merge(ByteArrayOutputStream baos, byte[]... pcaps) // $codepro.audit.disable // overloadedMethods throws IOException { merge(baos, Arrays.asList(pcaps)); }
private IEEE_802_1Q get802_1qTag(Buffer data) { // reference http://en.wikipedia.org/wiki/EtherType & // http://en.wikipedia.org/wiki/IEEE_802.1Q byte[] b802_1qTag = new byte[2]; data.gets(b802_1qTag, 0, 2); BitSet bits = BitSet.valueOf(b802_1qTag); int pcp = convertBitToInt(bits.get(0, 3)); int dei = convertBitToInt(bits.get(3, 4)); int vid = convertBitToInt(bits.get(4, 16)); return new IEEE_802_1Q(pcp, dei, vid); }
/** * Read packet. * * @param magicNumber * the magic number * @return the pcap packet * @throws IOException Signals that an I/O exception * has occurred. * @throws EOFException the EOF exception * @throws IOException * Signals that an I/O exception has occurred. */ private PcapPacket readPacket(int magicNumber) throws IOException { PacketHeader packetHeader = readPacketHeader(magicNumber); Buffer packetData = readPacketData(packetHeader.getInclLen()); return new PcapPacket(packetHeader, packetData); }
/** * Convert ip segment to hex. * * @param ipSegment * the ip segment * @return the string */ public static String convertIpSegmentToHex(String ipSegment) { return convertIpSegmentToHex(Integer.valueOf(ipSegment)); }
/** * Instantiates a new pcap byte output stream. * * @param baos * the baos */ public PcapByteOutputStream(ByteArrayOutputStream baos) { this.baos = baos; list = new ArrayList<Byte>(); createGlobalHeader(); }
/** * Instantiates a new pcap byte output stream. * * @param baos * the baos * @param header * the header */ public PcapByteOutputStream(ByteArrayOutputStream baos, GlobalHeader header) { this.baos = baos; list = new ArrayList<Byte>(); copyGlobalHeader(header); }
/** * Method close. * * * @throws IOException * * @see org.krakenapps.pcap.PcapOutputStream#close() * @see * org.krakenapps.pcap.PcapOutputStream#close() */ public void close() throws IOException { flush(); baos.close(); // $codepro.audit.disable closeInFinally } }
/** * Close input. * * @param is * the is */ private static void closeInput(PcapByteInputStream is) { if (is == null) { return; } try { is.close(); // $codepro.audit.disable closeInFinally } catch (IOException e) { LOG.error("Failed to close input stream", e); } }
/** * Close output. * * @param os * the os */ private static void closeOutput(PcapByteOutputStream os) { if (os == null) { return; } try { os.close(); } catch (IOException e) { LOG.error("Failed to close output stream", e); } }
/** * Opens pcap file input stream. * * @param pcap * the byte array to be read * * @throws IOException * Signals that an I/O exception has occurred. */ public PcapByteInputStream(byte[] pcap) throws IOException { is = new DataInputStream(new ByteArrayInputStream(pcap)); // $codepro.audit.disable // closeWhereCreated readGlobalHeader(); }
/** * Reads a packet from pcap byte array. * * @return the packet throws IOException the stream has been closed and the * contained input stream does not support reading after close, or * another I/O error occurs. * @throws IOException Signals that an I/O * exception has occurred. * @see * org.krakenapps.pcap.PcapInputStream#getPacket() * @throws IOException * Signals that an I/O exception has occurred. */ public PcapPacket getPacket() throws IOException { return readPacket(globalHeader.getMagicNumber()); }
/** * Gets the short session key. (5-tuple only) * * @param srcIp * the src ip * @param dstIp * the dst ip * @param protocol * the protocol * @param srcPort * the src port * @param dstPort * the dst port * @return the session key */ public static String getShortSessionKey(String srcIp, String dstIp, int protocol, int srcPort, int dstPort) { String keySeperator = "-"; StringBuffer sb = new StringBuffer(40); sb.append(convertIpv4IpToHex(srcIp)).append(keySeperator) .append(convertIpv4IpToHex(dstIp)).append(keySeperator) .append(protocol).append(keySeperator).append(srcPort) .append(keySeperator).append(dstPort); return sb.toString(); }
/** * Gets the pcaps. * * @return the pcaps * @throws IOException * Signals that an I/O exception has occurred. */ public byte[] getPcaps() throws IOException { if (pcaps.size() == 1) { return pcaps.get(0); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); PcapMerger.merge(baos, pcaps); return baos.toByteArray(); }
/** * Convert ipv4 ip to hex. * * @param ipAddress * the ip address * @return the string */ public static String convertIpv4IpToHex(String ipAddress) { StringBuffer hexIp = new StringBuffer(64); String[] ipSegments = ipAddress.split("\\."); for (String ipSegment : ipSegments) { hexIp.append(convertIpSegmentToHex(ipSegment)); } return hexIp.toString(); }