/** * 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); }
/** * Convert ip segment to hex. * * @param ipSegment * the ip segment * @return the string */ public static String convertIpSegmentToHex(String ipSegment) { return convertIpSegmentToHex(Integer.valueOf(ipSegment)); }
/** * 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 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 short key * * * @return the short key */ public String getShortKey() { int sourcePort = 0; int destinationPort = 0; if(Constants.PROTOCOL_UDP == ipv4Packet.getProtocol()) { sourcePort = udpPacket.getSourcePort(); destinationPort = udpPacket.getDestinationPort(); } else if (Constants.PROTOCOL_TCP == ipv4Packet.getProtocol()) { sourcePort = tcpPacket.getSourcePort(); destinationPort = tcpPacket.getDestinationPort(); } return PcapUtils.getShortSessionKey(ipv4Packet.getSourceAddress().getHostAddress(), ipv4Packet.getDestinationAddress().getHostAddress(), ipv4Packet.getProtocol(), sourcePort, destinationPort); }
/** * 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 * @param ipId * the ip id * @param fragmentOffset * the fragment offset * @return the session key */ public static String getSessionKey(String srcIp, String dstIp, int protocol, int srcPort, int dstPort, int ipId, int fragmentOffset) { 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).append(keySeperator).append(ipId) .append(keySeperator).append(fragmentOffset); return sb.toString(); }
public void nextTuple() { // System.out.println("nextTuple of PcapSimulatorSpout"); ipAddr.setLength(0); String srcAddr = ipAddr.append(randomIpSegmentGenerator.nextInt(255)).append('.').append(randomIpSegmentGenerator.nextInt(255)) .append('.').append(randomIpSegmentGenerator.nextInt(255)).append('.').append(randomIpSegmentGenerator.nextInt(255)).toString(); ipAddr.setLength(0); String dstAddr = ipAddr.append(randomIpSegmentGenerator.nextInt(255)).append('.').append(randomIpSegmentGenerator.nextInt(255)) .append('.').append(randomIpSegmentGenerator.nextInt(255)).append('.').append(randomIpSegmentGenerator.nextInt(255)).toString(); String key = PcapUtils.getSessionKey(srcAddr, dstAddr, String.valueOf(randomProtocolGenerator.nextInt(255)), String.valueOf(randomPortGenerator.nextInt(64000)), String.valueOf(randomPortGenerator.nextInt(64000)), "0", "0"); jsonDoc = jsonDocs[randomJsonGenerator.nextInt(8)]; ts = System.currentTimeMillis() + randomPortGenerator.nextInt(); randomPcapGenerator.nextBytes(pcap); collector.emit(new Values(srcAddr, key.toString(), jsonDoc, ts, pcap)); collector.emit("pcap_index_stream", new Values(jsonDoc, key)); collector.emit("pcap_header_stream", new Values(jsonDoc, key)); collector.emit("pcap_data_stream", new Values(key.toString(), ts, pcap)); }
sb.append(convertIpv4IpToHex(srcIp)).append(SESSION_KEY_SEPERATOR) .append(convertIpv4IpToHex(dstIp)).append(SESSION_KEY_SEPERATOR) .append(protocol == null ? "0" : protocol) .append(SESSION_KEY_SEPERATOR).append(srcPort == null ? "0" : srcPort)
/** * 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(); }
String sessionKey = PcapUtils.getSessionKey(srcIp, dstIp, protocol, srcPort, dstPort); LOGGER.info("sessionKey =" + sessionKey);
/** * Gets the key. * * * @return the key */ public String getKey() { int sourcePort = 0; int destinationPort = 0; if (Constants.PROTOCOL_UDP == ipv4Packet.getProtocol()) { sourcePort = udpPacket.getSourcePort(); destinationPort = udpPacket.getDestinationPort(); } else if (Constants.PROTOCOL_TCP == ipv4Packet.getProtocol()) { sourcePort = tcpPacket.getSourcePort(); destinationPort = tcpPacket.getDestinationPort(); } return PcapUtils.getSessionKey(ipv4Packet.getSourceAddress().getHostAddress(), ipv4Packet.getDestinationAddress().getHostAddress(), ipv4Packet.getProtocol(), sourcePort, destinationPort, ipv4Packet.getId(), ipv4Packet.getFragmentOffset()); }