package com.lenovo.lps.jpcap;
import java.net.Inet4Address;
import java.net.InetAddress;
import com.lenovo.lps.log.Log;
import com.lenovo.lps.psb.PushServer;
import jpcap.JpcapCaptor;
import jpcap.NetworkInterface;
import jpcap.NetworkInterfaceAddress;
public class TTLInitSniffer extends Thread{
private final static Log log = Log.getInstance(TTLInitSniffer.class);
private int deviceIndex = 1 ;
private NetworkInterface device;
private JpcapCaptor captor;
public TTLInitSniffer(int deviceIndex){
this.deviceIndex = deviceIndex;
}
public void run(){
device = JpcapCaptor.getDeviceList()[deviceIndex];
if(device != null){
try {
log.info("TTLInitSniffer.run: begin to open device "+device.name+", detail:"+device.description);
captor=JpcapCaptor.openDevice(device,2000,false,5);
/*
InetAddress thisIP=null;
for(NetworkInterfaceAddress addr:device.addresses){
if(addr.address instanceof Inet4Address){
thisIP=addr.address;
log.info("TTLInitSniffer.run: local ip "+ PushServer.HOST);
break;
}
}
*/
//captor.setFilter("icmp and dst host "+thisIP.getHostAddress(),true);
captor.setFilter("(udp or icmp) and dst host "+PushServer.HOST,true);
TTLInitNotifier notifier = new TTLInitNotifier();
log.info("TTLInitSniffer.run: begin to capture packets");
captor.loopPacket(-1, notifier);
} catch (Exception e) {
log.error("TTLInitSniffer.run: unexpected exception:"+e.getMessage(),e.getCause());
captor.close();
}
}
}
public static void main(String[] args){
if(args.length<1){
NetworkInterface[] networkInterfaces = JpcapCaptor.getDeviceList();
int networkInterfaceNum = 0;
if(networkInterfaces != null){
System.out.println("NetworkInterface List:");
for(NetworkInterface networkInterface: networkInterfaces){
System.out.println(networkInterfaceNum+" "+networkInterface.name +" "+networkInterface.description);
networkInterfaceNum++;
}
}
System.out.println("Usage: java TTLInitSniffer <device index (e.g., 0, 1..)> ");
System.exit(0);
}
TTLInitSniffer sniffer = new TTLInitSniffer(Integer.parseInt(args[0]));
sniffer.start();
}
}
package com.lenovo.lps.jpcap;
import java.util.HashMap;
import jpcap.PacketReceiver;
import jpcap.packet.IPPacket;
import jpcap.packet.Packet;
import com.lenovo.lps.log.Log;
import com.lenovo.lps.psb.ps.udp.vo.PushUDPUser;
public class TTLInitNotifier implements PacketReceiver {
private final static Log log = Log.getInstance(TTLInitNotifier.class);
public static HashMap<String,Integer> ttl_init_list = new HashMap<String,Integer>();
@Override
public void receivePacket(Packet packet) {
//log.info("TTLInitNotifier.receivePacket receiver a packet!");
IPPacket ipPacket = (IPPacket)packet;
String src_ip = ipPacket.src_ip.getHostAddress();
//log.info("TTLInitNotifier.receivePacket src_ip "+src_ip);
int current_ttl_init = ipPacket.hop_limit;
//log.info("TTLInitNotifier.receivePacket current_ttl_init "+current_ttl_init);
if((64-current_ttl_init) > 0){
current_ttl_init = 64-current_ttl_init;
}else if(128-current_ttl_init > 0){
current_ttl_init = 128-current_ttl_init;
}else if(256-current_ttl_init > 0){
current_ttl_init = 256-current_ttl_init;
}else{
current_ttl_init = PushUDPUser.TTL_INIT;
}
if(ttl_init_list.containsKey(src_ip) ){
int last_ttl_init = ttl_init_list.get(src_ip);
//log.info("TTLInitNotifier.receivePacket last_ttl_init "+last_ttl_init);
if (last_ttl_init != current_ttl_init){
ttl_init_list.put(src_ip, current_ttl_init);
notify("Duplicate",src_ip, current_ttl_init, last_ttl_init);
}
}else{
ttl_init_list.put(src_ip, current_ttl_init);
notify("New",src_ip, current_ttl_init, 0);
}
}
private void notify(String type, String src_ip, int current_ttl_init, int last_ttl_init){
log.info("TTLInitNotifier.notify("+type+") src_ip:"+src_ip+" current_ttl_init:"+current_ttl_init+" last_ttl_init:"+last_ttl_init+"!");
}
}