뭐... 요넘도 마찮가지로 byte array에서 ip4 header만 추출하는 넘임..

Ip4Header.java
- source ip, destination ip, protocol 을 추출

import org.jnetpcap.packet.format.FormatUtils;


public class Ip4Header {


public final static int TCP = 6;

public final static int UDP = 17;

String version;

int     headerLength;

String srcIP;

String dstIP;

int protocol;


public String getVersion() {

return version;

}

public int getHeaderLength() {

return headerLength;

}

public String getSrcIP() {

return srcIP;

}

public String getDstIP() {

return dstIP;

}

public int getProtocol() {

return protocol;

}


public boolean makeHeader(byte[] buf) {

try {

byte[] bTmp = new byte[1];

System.arraycopy(buf, 14, bTmp, 0, bTmp.length);

if(!"4".equals(StringUtil.ascii2hexstr(bTmp).substring(0, 1))) return false;

this.version = StringUtil.ascii2hexstr(bTmp).substring(0, 1);

this.headerLength = Integer.parseInt(StringUtil.ascii2hexstr(bTmp).substring(1, 2)) * 4;

byte[] protocol = new byte[1];

byte[] dstIP = new byte[4];

byte[] srcIP = new byte[4];

System.arraycopy(buf, 23, protocol, 0, protocol.length);

System.arraycopy(buf, 26, srcIP, 0, srcIP.length);

System.arraycopy(buf, 30, dstIP, 0, dstIP.length);

this.protocol = protocol[0];

this.srcIP = FormatUtils.ip(srcIP);

this.dstIP = FormatUtils.ip(dstIP);

return true;

} catch(Exception e) {

e.printStackTrace();

return false;

}

}

}



캡쳐된 패킷 버퍼를 byte[] 로 전환해서 위 클래스에 할당하면 알아서 추출해준다.
byte[] dst = new byte[buffer.capacity()];
buffer.get(dst); 

 
Posted by 짱가쟁이